From 091dada1c730b54d14cbea6f58a1a83d73874367 Mon Sep 17 00:00:00 2001 From: alex Date: Sun, 5 Sep 2021 14:10:24 -0700 Subject: [PATCH] ... --- Makefile.am | 3 +++ configure.ac | 3 ++- inc/opt.h | 13 ++++++++++++- src/main.c | 7 ++++++- src/opt/config.c | 42 +++++++++++++++++++++++++++++++++++++++--- src/opt/env.c | 18 ++++++++++++++++++ src/opt/set.c | 30 ++++++++++++++++++++++++++++++ src/opt/watch.c | 21 +++++++++++++++++++++ src/usage.c | 10 ++++++++++ 9 files changed, 141 insertions(+), 6 deletions(-) create mode 100644 src/opt/env.c create mode 100644 src/opt/set.c create mode 100644 src/opt/watch.c diff --git a/Makefile.am b/Makefile.am index 69bea3d..3767189 100644 --- a/Makefile.am +++ b/Makefile.am @@ -15,7 +15,10 @@ seederd_SOURCES = \ src/log.c \ src/main.c \ src/opt/config.c \ + src/opt/env.c \ src/opt/loglevel.c \ + src/opt/set.c \ + src/opt/watch.c \ src/setup.c \ src/usage.c diff --git a/configure.ac b/configure.ac index a792a5f..dfe518c 100644 --- a/configure.ac +++ b/configure.ac @@ -63,13 +63,14 @@ AC_PROG_CC AC_CHECK_LIB([pthread],[pthread_create]) # Checks for header files. -AC_CHECK_HEADERS([stdlib.h]) +AC_CHECK_HEADERS([stdlib.h string.h]) # Checks for typedefs, structures, and compiler characteristics. AC_TYPE_SIZE_T # Checks for library functions. AC_FUNC_MALLOC +AC_CHECK_FUNCS([atexit]) AC_CONFIG_FILES([Makefile]) AC_OUTPUT diff --git a/inc/opt.h b/inc/opt.h index c03415f..15d1b50 100644 --- a/inc/opt.h +++ b/inc/opt.h @@ -1,11 +1,22 @@ #ifndef __OPT_H_ #define __OPT_H_ +#include +#include + #include -#define OPT_LOADING_CONFIG_FILE_MESSAGE "loading config file %s\n" +#define OPT_MESSAGE_CONFIG_FILE_FORMAT_ERROR "invalid format in config file %s:%d\n" +#define OPT_MESSAGE_LOADING_CONFIG_FILE "loading config file %s\n" +#define OPT_MESSAGE_UNABLE_OPEN_FILE "unable to open file %s\n" +#define OPT_MESSAGE_UNKNOWN_OPTION "unknown option %s\n" +#define OPT_MESSAGE_WATCH_ADD_SUCCESS "watching %s\n" +#define OPT_MESSAGE_WATCH_INVALID_DIRECTORY "watching directory %s failed\n" +int opt_add_watch(char*); int opt_load_config_file(char*); +int opt_load_from_env(); +int opt_set(char*,char*); void opt_set_log_level(enum log_level); #endif diff --git a/src/main.c b/src/main.c index e114384..c8e4b21 100644 --- a/src/main.c +++ b/src/main.c @@ -6,6 +6,7 @@ static struct option long_options[] = { {"help", no_argument, 0, 'h'}, {"quiet", no_argument, &verbose_flag, LOG_LEVEL_SILENT}, {"verbose", no_argument, &verbose_flag, LOG_LEVEL_VERBOSE}, + {"watch", required_argument, 0, 'w'}, {0,0,0,0} }; @@ -13,11 +14,12 @@ int main(int argc, char **argv) { int c; if(defaults()<0) { return EXIT_FAILURE; } + if(opt_load_from_env()<0) { return EXIT_FAILURE; } while(1) { int option_index = 0; - if((c = getopt_long(argc,argv,"c:dhqv",long_options,&option_index))==-1) { break; } + if((c = getopt_long(argc,argv,"c:dhqvw:",long_options,&option_index))==-1) { break; } switch(c) { case 'c': @@ -36,6 +38,9 @@ int main(int argc, char **argv) { case 'v': opt_set_log_level(LOG_LEVEL_VERBOSE); break; + case 'w': + if(opt_add_watch(optarg)<0) { return EXIT_FAILURE; } + break; case '?': default: return EXIT_FAILURE; diff --git a/src/opt/config.c b/src/opt/config.c index 3b1d8d4..f572ea7 100644 --- a/src/opt/config.c +++ b/src/opt/config.c @@ -1,7 +1,43 @@ #include +#define SET_OPT(x,f) { \ + if(strcmp(x,field)==0) { \ + if(f(buf)<0) { goto clean; } \ + } \ +} + int opt_load_config_file(char *path) { - log_info(OPT_LOADING_CONFIG_FILE_MESSAGE,path); - log_err("not implemented\n"); - return -1; + FILE *fp; + char field[100]; + char buf[1024]; + int line, i; + + log_info(OPT_MESSAGE_LOADING_CONFIG_FILE,path); + + fp = fopen(path,"r"); + if(NULL==fp) { + perror("fopen"); + log_err(OPT_MESSAGE_UNABLE_OPEN_FILE,path); + return -1; + } + + line = 0; + while(fgets(buf,1024,fp)!=NULL) { + line++; + // skip lines prefixed with # or empty lines + if(buf[0]=='#') { continue; } + if(buf[0]=='\n') { continue; } + + if(sscanf(buf,"%[^=\n]=%[^\n]",field,buf)!=2) { + log_err(OPT_MESSAGE_CONFIG_FILE_FORMAT_ERROR,path,line); + goto clean; + } + + if((i = opt_set(field,buf))<=0) { goto clean; } + } + + return 1; + clean: + fclose(fp); + return -1; } diff --git a/src/opt/env.c b/src/opt/env.c new file mode 100644 index 0000000..7ecd9dc --- /dev/null +++ b/src/opt/env.c @@ -0,0 +1,18 @@ +#include + +#define CHECK_ENV(x,f) { \ + p = getenv("SEEDER_"x); \ + if(p!=NULL) { \ + if(f(p)<0) { \ + return -1; \ + } \ + } \ +} + +int opt_load_from_env() { + char *p; + + CHECK_ENV("CONFIG",opt_load_config_file); + + return 1; +} diff --git a/src/opt/set.c b/src/opt/set.c new file mode 100644 index 0000000..a08c5a6 --- /dev/null +++ b/src/opt/set.c @@ -0,0 +1,30 @@ +#include + +struct option_lookup_table_entry { + char *key; + int (*function)(char*); +}; + +struct option_lookup_table_entry option_lookup_table[] = { + {"watch",&opt_add_watch}, + {NULL,NULL} +}; + +int opt_set(char *key, char *value) { + struct option_lookup_table_entry p; + size_t i; + + i = 0; + p = option_lookup_table[i]; + while(p.key!=NULL) { + if(strcmp(p.key,key)==0) { + return p.function(value); + } + + i++; + p = option_lookup_table[i]; + } + + log_err(OPT_MESSAGE_UNKNOWN_OPTION,key); + return 0; +} diff --git a/src/opt/watch.c b/src/opt/watch.c new file mode 100644 index 0000000..1734b05 --- /dev/null +++ b/src/opt/watch.c @@ -0,0 +1,21 @@ +#include + +int opt_add_watch(char *directory) { + struct stat st; + + if(stat(directory,&st)!=0) { + perror("stat"); + log_err(OPT_MESSAGE_WATCH_INVALID_DIRECTORY,directory); + return -1; + } + + if(!S_ISDIR(st.st_mode)) { + log_err(OPT_MESSAGE_WATCH_INVALID_DIRECTORY,directory); + return -1; + } + + log_msg(OPT_MESSAGE_WATCH_ADD_SUCCESS,directory); + + log_err("not implemented\n"); + return 1; +} diff --git a/src/usage.c b/src/usage.c index de83968..e189a94 100644 --- a/src/usage.c +++ b/src/usage.c @@ -1,4 +1,14 @@ #include void usage() { + log_err("Usage:\n"); + log_err("\tseederd [options]\n"); + log_err("\n"); + log_err("Options:\n"); + log_err("\t--config-file=, -c \n"); + log_err("\t--daemon, -d\n"); + log_err("\t--help, -h\n"); + log_err("\t--quiet, -q\n"); + log_err("\t--verbose, -v\n"); + log_err("\t--watch=, -w \n"); } -- 2.39.5