almost done

This commit is contained in:
Liam Waldron 2023-04-11 08:26:02 -04:00
parent 416ef34ffd
commit b2724c2637
5 changed files with 94 additions and 144 deletions

View File

@ -1,3 +1,10 @@
include config.mk include config.mk
all: src/main.c src/printevents.c src/mkevent.c
$(CC) $(CFLAGS) $(LDFLAGS) -c src/main.c
$(CC) $(CFLAGS) $(LDFLAGS) -c src/printevents.c
$(CC) $(CFLAGS) $(LDFLAGS) -c src/mkevent.c
$(CC) $(CFLAGS) $(LDFLAGS) -o src/nc src/main.o src/printevents.o src/mkevent.o
install: src/nc
install src/nc $(PREFIX)/bin

View File

@ -1,133 +0,0 @@
/**
* Copyright (C) 2012-2015 Yecheng Fu <cofyc.jackson at gmail dot com>
* All rights reserved.
*
* Use of this source code is governed by a MIT-style license that can be found
* in the LICENSE file.
*/
#ifndef ARGPARSE_H
#define ARGPARSE_H
/* For c++ compatibility */
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
struct argparse;
struct argparse_option;
typedef int argparse_callback (struct argparse *self,
const struct argparse_option *option);
enum argparse_flag {
ARGPARSE_STOP_AT_NON_OPTION = 1 << 0,
ARGPARSE_IGNORE_UNKNOWN_ARGS = 1 << 1,
};
enum argparse_option_type {
/* special */
ARGPARSE_OPT_END,
ARGPARSE_OPT_GROUP,
/* options with no arguments */
ARGPARSE_OPT_BOOLEAN,
ARGPARSE_OPT_BIT,
/* options with arguments (optional or required) */
ARGPARSE_OPT_INTEGER,
ARGPARSE_OPT_FLOAT,
ARGPARSE_OPT_STRING,
};
enum argparse_option_flags {
OPT_NONEG = 1, /* disable negation */
};
/**
* argparse option
*
* `type`:
* holds the type of the option, you must have an ARGPARSE_OPT_END last in your
* array.
*
* `short_name`:
* the character to use as a short option name, '\0' if none.
*
* `long_name`:
* the long option name, without the leading dash, NULL if none.
*
* `value`:
* stores pointer to the value to be filled.
*
* `help`:
* the short help message associated to what the option does.
* Must never be NULL (except for ARGPARSE_OPT_END).
*
* `callback`:
* function is called when corresponding argument is parsed.
*
* `data`:
* associated data. Callbacks can use it like they want.
*
* `flags`:
* option flags.
*/
struct argparse_option {
enum argparse_option_type type;
const char short_name;
const char *long_name;
void *value;
const char *help;
argparse_callback *callback;
intptr_t data;
int flags;
};
/**
* argpparse
*/
struct argparse {
// user supplied
const struct argparse_option *options;
const char *const *usages;
int flags;
const char *description; // a description after usage
const char *epilog; // a description at the end
// internal context
int argc;
const char **argv;
const char **out;
int cpidx;
const char *optvalue; // current option value
};
// built-in callbacks
int argparse_help_cb(struct argparse *self,
const struct argparse_option *option);
int argparse_help_cb_no_exit(struct argparse *self,
const struct argparse_option *option);
// built-in option macros
#define OPT_END() { ARGPARSE_OPT_END, 0, NULL, NULL, 0, NULL, 0, 0 }
#define OPT_BOOLEAN(...) { ARGPARSE_OPT_BOOLEAN, __VA_ARGS__ }
#define OPT_BIT(...) { ARGPARSE_OPT_BIT, __VA_ARGS__ }
#define OPT_INTEGER(...) { ARGPARSE_OPT_INTEGER, __VA_ARGS__ }
#define OPT_FLOAT(...) { ARGPARSE_OPT_FLOAT, __VA_ARGS__ }
#define OPT_STRING(...) { ARGPARSE_OPT_STRING, __VA_ARGS__ }
#define OPT_GROUP(h) { ARGPARSE_OPT_GROUP, 0, NULL, NULL, h, NULL, 0, 0 }
#define OPT_HELP() OPT_BOOLEAN('h', "help", NULL, \
"show this help message and exit", \
argparse_help_cb, 0, OPT_NONEG)
int argparse_init(struct argparse *self, struct argparse_option *options,
const char *const *usages, int flags);
void argparse_describe(struct argparse *self, const char *description,
const char *epilog);
int argparse_parse(struct argparse *self, int argc, const char **argv);
void argparse_usage(struct argparse *self);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -1,17 +1,28 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <unistd.h>
#include "../include/argparse.h" int main(int argc, char *argv[]) {
int opt;
static const char *const usages[] = { while ((opt = getopt(argc, argv, ":if:nl")) != -1) {
"nc -c -r DATE EVENT_NAME", switch(opt) {
NULL, case 'n':
}; mkevent();
break;
case 'l':
printevents();
break;
case '?':
printf("unknown option: %c\n", optopt);
printf("usage: %c -l -n DATE EVENT_NAME\n", argv[0]);
break;
}
}
#define PERM_READ (1<<0) for (; optind < argc; optind++) {
#define PERM_WRITE (1<<1) printf("extra arguments: %s\n", argv[optind]);
#define PERM_EXEC (1<<2) }
int main (int argc, const char **argv) { return 0;
int }

51
src/main.c.old Normal file
View File

@ -0,0 +1,51 @@
#include <stdio.h> /* for printf */
#include <stdlib.h> /* for exit */
#include <unistd.h> /* for getopt */
int main (int argc, char **argv) {
int c;
int digit_optind = 0;
int aopt = 0, bopt = 0;
char *copt = 0, *dopt = 0;
while ((c = getopt(argc, argv, "abc:d:012")) != -1) {
int this_option_optind = optind ? optind : 1;
switch (c) {
case '0':
case '1':
case '2':
if (digit_optind != 0 && digit_optind != this_option_optind) {
printf ("digits occur in two different argv-elements.\n");
}
digit_optind = this_option_optind;
printf ("option %c\n", c);
break;
case 'a':
printf ("option a\n");
aopt = 1;
break;
case 'b':
printf ("option b\n");
bopt = 1;
break;
case 'c':
printf ("option c with value '%s'\n", optarg);
copt = optarg;
break;
case 'd':
printf ("option d with value '%s'\n", optarg);
dopt = optarg;
break;
case '?':
break;
default:
printf ("?? getopt returned character code 0%o ??\n", c);
}
}
if (optind < argc) {
printf ("non-option ARGV-elements: ");
while (optind < argc) {
printf ("%s ", argv[optind++]);
}
printf ("\n");
}
exit (0);
}

View File

@ -5,3 +5,17 @@
#include "../include/libcolor.h" #include "../include/libcolor.h"
#include "config.h" #include "config.h"
char mkevent(int argc, char *argv[]) {
FILE *fp;
fp = fopen(NC_EVENTS_FILE, "a+");
if (fp == NULL) {
printf(TXT_BOLD COL_RED "events file does not exist\n");
printf("ensure the events file defined in config.h exists\n" COL_RESET TXT_RESET);
exit(1);
}
fprintf(fp, "%s", argv[2]); /* print date */
fprintf(fp, "%s", argv[3]); /* print name */
fclose(fp);
}