From 47b6e46d9bba6835018bdf7432ea742eaca45a03 Mon Sep 17 00:00:00 2001 From: katherine Date: Tue, 10 Dec 2019 23:29:21 -0700 Subject: implement option parsing and calendar locating --- src/conf.c | 0 src/conf.h | 4 ---- src/main.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/opt.c | 36 ++++++++++++++++++++-------------- src/opt.h | 6 +++--- 5 files changed, 90 insertions(+), 21 deletions(-) delete mode 100644 src/conf.c delete mode 100644 src/conf.h (limited to 'src') diff --git a/src/conf.c b/src/conf.c deleted file mode 100644 index e69de29..0000000 diff --git a/src/conf.h b/src/conf.h deleted file mode 100644 index f686a49..0000000 --- a/src/conf.h +++ /dev/null @@ -1,4 +0,0 @@ -#ifndef EVERY_CONF_H -#define EVERY_CONF_H - -#endif diff --git a/src/main.c b/src/main.c index e69de29..625e860 100644 --- a/src/main.c +++ b/src/main.c @@ -0,0 +1,65 @@ +#include "opt.h" + +#include "../reqs/simple-xdg-bdirs/simple-xdg-bdirs.h" + +int main(int argc, char **argv) +{ + enum every_opt_outputmode omode; + FILE *calendar; + + char **rdirs, **cur, *s; + + opt_parse(argc, argv); + + omode = opt_outputmode(); + + if (opt_calpath() != NULL) { + s = opt_calpath(); + + calendar = fopen(s, "r"); + if (calendar == NULL) { + printf("err: could not read calendar at `%s`\n", s); + exit(EXIT_FAILURE); + } + } else { + rdirs = simple_xdg_bdirs_read_dirs(SIMPLE_XDG_BDIRS_CONFIG); + + if (rdirs == NULL) { + printf( + "err: no calendar specified, and default was not found or " + "readable\n" + ); + exit(EXIT_FAILURE); + } + + s = simple_xdg_bdirs_fullpath_read("every/calendar", rdirs); + + for (cur = rdirs; *cur != NULL; cur++) + free(*cur); + free(rdirs); + + if (s == NULL) { + printf( + "err: no calendar specified, and default was not found or " + "readable\n" + ); + exit(EXIT_FAILURE); + } + + calendar = fopen(s, "r"); + + free(s); + + if (calendar == NULL) { + printf( + "err: no calendar specified, and default was not found or " + "readable\n" + ); + exit(EXIT_FAILURE); + } + } + + fclose(calendar); + + return 0; +} diff --git a/src/opt.c b/src/opt.c index 579743d..eb796e7 100644 --- a/src/opt.c +++ b/src/opt.c @@ -5,11 +5,9 @@ #include -#include - const char *set[] = { - [EVERY_OPT_PRINTMODE_CONSOLE] = "console", - [EVERY_OPT_PRINTMODE_SCRIPT] = "script", + [EVERY_OPT_OUTPUTMODE_CONSOLE] = "console", + [EVERY_OPT_OUTPUTMODE_SCRIPT] = "script", NULL, }; @@ -17,9 +15,9 @@ static struct simple_opt options[] = { { SIMPLE_OPT_FLAG, 'h', "help", false, "print this help message and exit" }, { SIMPLE_OPT_FLAG, 'v', "version", false, - "print the version of confconf in use and exit" }, - { SIMPLE_OPT_STRING, 'c', "config", true, - "path to config file", "" }, + "print the version of every in use and exit" }, + { SIMPLE_OPT_STRING, 'c', "calendar", true, + "path to calendar", "" }, { SIMPLE_OPT_STRING_SET, 'm', "output-mode", true, "output mode (default is console)", "(console|script)", set }, @@ -29,8 +27,6 @@ static struct simple_opt options[] = { void opt_parse(int argc, char **argv) { struct simple_opt_result result; - size_t i; - char c, cend; result = simple_opt_parse(argc, argv, options); @@ -43,8 +39,8 @@ void opt_parse(int argc, char **argv) /* help */ if (options[0].was_seen) { simple_opt_print_usage(stdout, 80, argv[0], - "[-c FILE] [-m OUTPUT_MODE]", - "every is a flexible console-based event calendar", + "[-c CALENDAR_FILE] [-m OUTPUT_MODE]", + "every is a flexible, console-based event calendar", options); exit(EXIT_SUCCESS); } @@ -54,14 +50,26 @@ void opt_parse(int argc, char **argv) puts(VERSION); exit(EXIT_SUCCESS); } + + /* bad calendar path*/ + if (options[2].was_seen && options[2].val.v_string[0] == '\0') { + printf("err: could not read calendar at ``\n"); + exit(EXIT_FAILURE); + } } -const char* opt_confpath_str(void) +char* opt_calpath(void) { - return options[2].val.v_string; + if (options[2].was_seen) + return options[2].val.v_string; + + return NULL; } enum every_opt_outputmode opt_outputmode(void) { - return options[3].val.v_string_set_idx; + if (options[3].was_seen) + return options[3].val.v_string_set_idx; + + return EVERY_OPT_OUTPUTMODE_CONSOLE; } diff --git a/src/opt.h b/src/opt.h index 10bc4c9..f751232 100644 --- a/src/opt.h +++ b/src/opt.h @@ -2,13 +2,13 @@ #define EVERY_OPT_H enum every_opt_outputmode { - EVERY_OPT_PRINTMODE_CONSOLE = 0, - EVERY_OPT_PRINTMODE_SCRIPT = 1, + EVERY_OPT_OUTPUTMODE_CONSOLE = 0, + EVERY_OPT_OUTPUTMODE_SCRIPT = 1, }; void opt_parse(int argc, char **argv); -const char* opt_confpath_str(void); +char* opt_calpath(void); enum every_opt_outputmode opt_outputmode(void); #endif -- cgit v1.2.3