]> infiniteadaptability.org Git - seeder/commitdiff
...
authoralex <[email protected]>
Sun, 5 Sep 2021 21:10:24 +0000 (14:10 -0700)
committeralex <[email protected]>
Sun, 5 Sep 2021 21:10:24 +0000 (14:10 -0700)
Makefile.am
configure.ac
inc/opt.h
src/main.c
src/opt/config.c
src/opt/env.c [new file with mode: 0644]
src/opt/set.c [new file with mode: 0644]
src/opt/watch.c [new file with mode: 0644]
src/usage.c

index 69bea3dc29057ed15e57407cce8fc80f03db484b..37671894a9a24fad782a0211611cb81938357b01 100644 (file)
@@ -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
 
index a792a5ffe5369ea61761ec2f61152fcbafbc1dcc..dfe518cf9e6f3adee00c44f4e6ed3649d3cb68b5 100644 (file)
@@ -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
index c03415febe104d824d1b099938ab4cdd2128d055..15d1b50dda4c993552d5fd4f508c0e5d173bfe85 100644 (file)
--- a/inc/opt.h
+++ b/inc/opt.h
@@ -1,11 +1,22 @@
 #ifndef __OPT_H_
 #define __OPT_H_
 
+#include<string.h>
+#include<sys/stat.h>
+
 #include<log.h>
 
-#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
index e114384e021535bb1eb515e7e1f86da89fdc5697..c8e4b219374f45f77cf4c5585145190ee2429e56 100644 (file)
@@ -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;
index 3b1d8d4d438523d49043271be3258d6a917936dd..f572ea74ad6af214fa8aa0013973c304404ac711 100644 (file)
@@ -1,7 +1,43 @@
 #include<opt.h>
 
+#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 (file)
index 0000000..7ecd9dc
--- /dev/null
@@ -0,0 +1,18 @@
+#include<opt.h>
+
+#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 (file)
index 0000000..a08c5a6
--- /dev/null
@@ -0,0 +1,30 @@
+#include<opt.h>
+
+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 (file)
index 0000000..1734b05
--- /dev/null
@@ -0,0 +1,21 @@
+#include<opt.h>
+
+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;
+}
index de8396897f1f16985aefb637467aa8e54abb36a2..e189a94eb125b9b466483fbae14b5a1ef1224f85 100644 (file)
@@ -1,4 +1,14 @@
 #include<usage.h>
 
 void usage() {
+       log_err("Usage:\n");
+       log_err("\tseederd [options]\n");
+       log_err("\n");
+       log_err("Options:\n");
+       log_err("\t--config-file=<path>, -c <path>\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=<directory>, -w <directory>\n");
 }