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 --- .gitmodules | 6 ++++ Makefile | 64 ++++++++++++++++++++++++++++++++++++ configure | 89 --------------------------------------------------- reqs/simple-opt | 1 + reqs/simple-xdg-bdirs | 1 + src/conf.c | 0 src/conf.h | 4 --- src/main.c | 65 +++++++++++++++++++++++++++++++++++++ src/opt.c | 36 +++++++++++++-------- src/opt.h | 6 ++-- 10 files changed, 162 insertions(+), 110 deletions(-) create mode 100644 .gitmodules create mode 100644 Makefile delete mode 100755 configure create mode 160000 reqs/simple-opt create mode 160000 reqs/simple-xdg-bdirs delete mode 100644 src/conf.c delete mode 100644 src/conf.h diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..d6a321c --- /dev/null +++ b/.gitmodules @@ -0,0 +1,6 @@ +[submodule "reqs/simple-opt"] + path = reqs/simple-opt + url = https://git.airen-no-jikken.icu/ageha/simple-opt.git +[submodule "reqs/simple-xdg-bdirs"] + path = reqs/simple-xdg-bdirs + url = https://git.airen-no-jikken.icu/ageha/simple-xdg-bdirs.git diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..1f49a2d --- /dev/null +++ b/Makefile @@ -0,0 +1,64 @@ +.POSIX: +.SUFFIXES: + +CC = cc +CFLAGS = -Wall -O2 +CFLAGSDEBUG = -std=c99 -Wall -pedantic -ggdb3 -O0 -DDEBUG +PREFIX = /usr/local + +all: every + +debug: dbg_every + +install: all + mkdir -p $(DESTDIR)$(PREFIX)/bin + mkdir -p $(DESTDIR)$(PREFIX)/share/man/man1 + cp -f every $(DESTDIR)$(PREFIX)/bin + gzip < every.1 > $(DESTDIR)$(PREFIX)/share/man/man1/every.1.gz + +every: build/release build/release/calendar.o build/release/conf.o build/release/main.o build/release/opt.o + $(CC) $(LDFLAGS) -o every build/release/calendar.o build/release/conf.o build/release/main.o build/release/opt.o $(LDLIBS) + +dbg_every: build/debug build/debug/calendar.o build/debug/conf.o build/debug/main.o build/debug/opt.o + $(CC) $(LDFLAGS) -o dbg_every build/debug/calendar.o build/debug/conf.o build/debug/main.o build/debug/opt.o $(LDLIBS) + +build/release: + mkdir -p build/release + +build/debug: + mkdir -p build/debug + +src/version.h: + printf "%s\n%s\n\n%s%s%s\n\n%s\n" \ + "#ifndef EVERY_VERSION_H" \ + "#define EVERY_VERSION_H" \ + "#define VERSION \"every-" "8cb7ab5" "\"" \ + "#endif" > src/version.h + +build/release/calendar.o: src/calendar.c + $(CC) -c $(CFLAGS) -o build/release/calendar.o src/calendar.c +build/release/conf.o: src/conf.c + $(CC) -c $(CFLAGS) -o build/release/conf.o src/conf.c +build/release/main.o: src/main.c src/opt.h \ + src/../reqs/simple-xdg-bdirs/simple-xdg-bdirs.h + $(CC) -c $(CFLAGS) -o build/release/main.o src/main.c +build/release/opt.o: src/opt.c src/version.h src/opt.h \ + src/../reqs/simple-opt/simple-opt.h + $(CC) -c $(CFLAGS) -o build/release/opt.o src/opt.c + +build/debug/calendar.o: src/calendar.c + $(CC) -c $(CFLAGSDEBUG) -o build/debug/calendar.o src/calendar.c +build/debug/conf.o: src/conf.c + $(CC) -c $(CFLAGSDEBUG) -o build/debug/conf.o src/conf.c +build/debug/main.o: src/main.c src/opt.h \ + src/../reqs/simple-xdg-bdirs/simple-xdg-bdirs.h + $(CC) -c $(CFLAGSDEBUG) -o build/debug/main.o src/main.c +build/debug/opt.o: src/opt.c src/version.h src/opt.h \ + src/../reqs/simple-opt/simple-opt.h + $(CC) -c $(CFLAGSDEBUG) -o build/debug/opt.o src/opt.c + +clean: + rm -f src/version.h + rm -f every + rm -f dbg_every + rm -rf build diff --git a/configure b/configure deleted file mode 100755 index 6a73fa4..0000000 --- a/configure +++ /dev/null @@ -1,89 +0,0 @@ -#!/bin/sh - -# kinda brittle, this, but mostly works -# just don't use names with whitespaces - -target='every' -srcdir='src' -sources=`find $srcdir -type f -name '*.c' | sed -e "s/$srcdir\/\(.*\).c/\1.c/"` -objdir='build' -objects=`find $srcdir -type f -name '*.c' | sed -e "s/$srcdir\/\(.*\).c/\1.o/"` - -cc='cc' -cflags='-Wall -O2' -cflagsdebug='-std=c99 -Wall -pedantic -ggdb3 -O0 -DDEBUG' -prefix='/usr/local' - - - -rls_objects=`printf %s "$objects" | sed -e "s/^/$objdir\/release\//" | tr '\n' ' '` -dbg_objects=`printf %s "$objects" | sed -e "s/^/$objdir\/debug\//" | tr '\n' ' '` - -dgen='gcc' -if [ ! `2>/dev/null which gcc` ]; then - if [ ! `2>/dev/null which clang` ]; then - echo 'err: could not find a suitable ' \ - 'compiler for calculating dependencies' - return 1 - fi - dgen='clang' -fi - -if [ -f 'Makefile' ]; then - make clean -fi - -{ - printf %s\\n '.POSIX:' - printf %s\\n '.SUFFIXES:' - printf %s\\n '' - printf %s\\n "CC = $cc" - printf %s\\n "CFLAGS = $cflags" - printf %s\\n "CFLAGSDEBUG = $cflagsdebug" - printf %s\\n "PREFIX = $prefix" - printf %s\\n '' - printf %s\\n "all: $target" - printf %s\\n '' - printf %s\\n "debug: dbg_$target" - printf %s\\n '' - printf %s\\n "install: all" - printf %s\\n ' mkdir -p $(DESTDIR)$(PREFIX)/bin' - printf %s\\n ' mkdir -p $(DESTDIR)$(PREFIX)/share/man/man1' - printf %s\\n " cp -f $target \$(DESTDIR)\$(PREFIX)/bin" - printf %s\\n " gzip < ${target}.1 > \$(DESTDIR)\$(PREFIX)/share/man/man1/${target}.1.gz" - printf %s\\n '' - printf %s\\n "$target: ${objdir}/release $rls_objects" - printf %s\\n " \$(CC) \$(LDFLAGS) -o $target $rls_objects \$(LDLIBS)" - printf %s\\n '' - printf %s\\n "dbg_$target: ${objdir}/debug $dbg_objects" - printf %s\\n " \$(CC) \$(LDFLAGS) -o dbg_$target $dbg_objects \$(LDLIBS)" - printf %s\\n '' - printf %s\\n "${objdir}/release:" - printf %s\\n " mkdir -p ${objdir}/release" - printf %s\\n '' - printf %s\\n "${objdir}/debug:" - printf %s\\n " mkdir -p ${objdir}/debug" - printf %s\\n '' - printf %s\\n 'src/version.h:' - printf %s\\n ' printf "%s\n%s\n\n%s%s%s\n\n%s\n" \' - printf %s\\n ' "#ifndef EVERY_VERSION_H" \' - printf %s\\n ' "#define EVERY_VERSION_H" \' - printf %s\\n " \"#define VERSION \\\"$target-\" \"`git describe --always --tags`\" \"\\\"\" \\" - printf %s\\n ' "#endif" > src/version.h' - printf %s\\n '' - printf %s\\n "$sources" | (while IFS= read -r s; do - $dgen $CFLAGS -MM -MG -MT ${objdir}/release/`printf %s $s | sed -e 's/\.c$/\.o/'` "$srcdir/$s" | sed -e "s/version.h/$srcdir\/version.h/" - printf %s\\n " \$(CC) -c \$(CFLAGS) -o ${objdir}/release/`printf %s $s | sed -e 's/\.c$/\.o/'` ${srcdir}/$s" - done) - printf %s\\n '' - printf %s\\n "$sources" | (while IFS= read -r s; do - $dgen $CFLAGS -MM -MG -MT ${objdir}/debug/`printf %s $s | sed -e 's/\.c$/\.o/'` "$srcdir/$s" | sed -e "s/version.h/$srcdir\/version.h/" - printf %s\\n " \$(CC) -c \$(CFLAGSDEBUG) -o ${objdir}/debug/`printf %s $s | sed -e 's/\.c$/\.o/'` ${srcdir}/$s" - done) - printf %s\\n '' - printf %s\\n 'clean:' - printf %s\\n ' rm -f src/version.h' - printf %s\\n " rm -f $target" - printf %s\\n " rm -f dbg_$target" - printf %s\\n " rm -rf $objdir" -} > Makefile diff --git a/reqs/simple-opt b/reqs/simple-opt new file mode 160000 index 0000000..197d36e --- /dev/null +++ b/reqs/simple-opt @@ -0,0 +1 @@ +Subproject commit 197d36e32ddf85b49e82d5b84b9170f25f844ecb diff --git a/reqs/simple-xdg-bdirs b/reqs/simple-xdg-bdirs new file mode 160000 index 0000000..9eadeef --- /dev/null +++ b/reqs/simple-xdg-bdirs @@ -0,0 +1 @@ +Subproject commit 9eadeef96ff35f12f2a44085e84d42d93c53c026 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