commit 416ef34ffd3bedf657134201384203836833f5e8 Author: Liam Waldron Date: Sat Apr 1 23:13:39 2023 -0400 init diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..a2861d6 --- /dev/null +++ b/Makefile @@ -0,0 +1,3 @@ +include config.mk + + diff --git a/include/argparse.h b/include/argparse.h new file mode 100644 index 0000000..fd1ddfc --- /dev/null +++ b/include/argparse.h @@ -0,0 +1,133 @@ +/** + * Copyright (C) 2012-2015 Yecheng Fu + * 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 + +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 diff --git a/include/libcolor.h b/include/libcolor.h new file mode 100644 index 0000000..09ff2f9 --- /dev/null +++ b/include/libcolor.h @@ -0,0 +1,17 @@ +/* libcolor.h - header file to define colors */ + +#ifndef LIBCOLOR_H_ +#define LIBCOLOR_H_ + +#define TXT_BOLD "\e[1m" +#define TXT_RESET "\e[m" + +#define COL_RED "\x1b[31m" +#define COL_GREEN "\x1b[32m" +#define COL_YELLOW "\x1b[33m" +#define COL_BLUE "\x1b[34m" +#define COL_MAGENTA "\x1b[35m" +#define COL_CYAN "\x1b[36m" +#define COL_RESET "\x1b[0m" + +#endif diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..cee25f4 --- /dev/null +++ b/src/config.h @@ -0,0 +1,6 @@ +#ifndef CONFIG_H_ +#define CONFIG_H_ + +#define NC_EVENTS_FILE "/etc/nc/events" + +#endif diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..a1e3a4b --- /dev/null +++ b/src/main.c @@ -0,0 +1,17 @@ +#include +#include +#include + +#include "../include/argparse.h" + +static const char *const usages[] = { + "nc -c -r DATE EVENT_NAME", + NULL, +}; + +#define PERM_READ (1<<0) +#define PERM_WRITE (1<<1) +#define PERM_EXEC (1<<2) + +int main (int argc, const char **argv) { + int diff --git a/src/mkevent.c b/src/mkevent.c new file mode 100644 index 0000000..b01520a --- /dev/null +++ b/src/mkevent.c @@ -0,0 +1,7 @@ +#include +#include +#include + +#include "../include/libcolor.h" + +#include "config.h" diff --git a/src/printevents.c b/src/printevents.c new file mode 100644 index 0000000..8c0087e --- /dev/null +++ b/src/printevents.c @@ -0,0 +1,29 @@ +#include +#include +#include + +#include "../include/libcolor.h" + +#include "config.h" + +char printevents() { + FILE *fp; + + char filename[100], contents; + + fp = fopen(NC_EVENTS_FILE, "r"); + 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); + } + + contents = fgetc(fp); + while (contents != EOF) { + printf ("%c", contents); + contents = fgetc(fp); + } + + fclose(fp); + exit(0); +}