From 8b3591237771d6aa903870507f84032f59973a44 Mon Sep 17 00:00:00 2001 From: Gavin Romig-Koch Date: Mon, 10 Aug 2009 15:25:12 -0400 Subject: Task #2: collect config info from the configuration file --- FIXME | 4 ++ fastback.conf | 7 +++ fastback.cpp | 165 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 175 insertions(+), 1 deletion(-) create mode 100644 FIXME create mode 100644 fastback.conf diff --git a/FIXME b/FIXME new file mode 100644 index 0000000..eb8515b --- /dev/null +++ b/FIXME @@ -0,0 +1,4 @@ + +* config file comments can only be on a line by themselves +* config file strings can't have nested double quotes + \ No newline at end of file diff --git a/fastback.conf b/fastback.conf new file mode 100644 index 0000000..aa25d25 --- /dev/null +++ b/fastback.conf @@ -0,0 +1,7 @@ +# this is a config file for fastback + +URL = ftp://test.example.com + +# ERROR = 103 +# error + \ No newline at end of file diff --git a/fastback.cpp b/fastback.cpp index d024dd3..4c464bf 100644 --- a/fastback.cpp +++ b/fastback.cpp @@ -36,6 +36,8 @@ The program keeps a log of all files uploaded, where they were uploaded to, encr #ifndef _GNU_SOURCE #define _GNU_SOURCE #endif +#include +#include #include #include #include @@ -92,11 +94,163 @@ static struct argp fastback_argp = { "FILE" }; + +static char* fastback_URL; + +static void +option_parse(const char* line, + size_t var_start, size_t var_end, + size_t value_start, size_t value_end) +{ + const char option1[] = "URL"; + size_t option_length = strlen(option1); + + if (option_length == (var_end - var_start) + && memcmp(option1,line+var_start,option_length) == 0) + { + int value_length = value_end - value_start; + fastback_URL = (char*)malloc(value_length + 1); + memcpy(fastback_URL, line+value_start, value_length); + fastback_URL[value_length] = 0; + } + + else + { + int var_length = var_end - var_start; + char* var = (char*)malloc(var_length + 1); + memcpy(var, line+var_start, var_length); + var[var_length] = 0; + + fprintf(stderr, "error: unknown config option: %s\n", var); + free(var); + } +} + + + +static void +error_regerror (int errcode, regex_t *compiled, const char* msg) +{ + size_t length = regerror (errcode, compiled, NULL, 0); + char *buffer = (char*)malloc (length); + (void) regerror (errcode, compiled, buffer, length); + fprintf(stderr, "%s: %s\n", msg, buffer); + free(buffer); +} + +static int +parse_config(const char* filename) +{ + int syntax_error = 0; + int err; + regex_t regexp; + FILE* file; + char *line; + size_t linesize; + ssize_t readcount; + + const char pattern[] = + "^[[:space:]]*" + "\\(\\|#.*\\|" + "\\([[:alpha:]][[:alnum:]]*\\)[[:space:]]*=" + "[[:space:]]*\\(\"\\([^\"]*\\)\"\\|\\([^[:space:]]*\\)\\)[[:space:]]*" + "\\)$"; + const int debug_pattern = 0; + + err = regcomp( ®exp, pattern, 0); + if (err) + { + error_regerror(err, ®exp, "error from regcomp"); + exit(4); + } + + size_t matchsize = regexp.re_nsub + 1; + regmatch_t* matchptr = (regmatch_t*)malloc(sizeof(regmatch_t) * matchsize); + + file = fopen(filename,"r"); + if (!file) + { + perror("error opening file"); + exit(4); + } + + line = 0; + linesize = 0; + readcount = getline(&line, &linesize, file); + while (readcount != -1) + { + /* trim the newline */ + if (readcount != 0 && line[readcount-1] == '\n') + line[readcount-1] = 0; + + err = regexec( ®exp, line, matchsize, matchptr, 0); + if (err == 0) + { + if (debug_pattern) + { + int i; + fprintf(stderr,"line: %s\n", line); + for (i=0; i < matchsize; i++) + if (matchptr[i].rm_so == -1) + fprintf(stderr,"match (%d): no match\n", i); + else + { + int j; + char buf[1000]; + for(j=0; j < (matchptr[i].rm_eo - matchptr[i].rm_so); j++) + buf[j] = line[matchptr[i].rm_so + j]; + buf[j] = 0; + if (j >= 1000) + { + fprintf(stderr,"J too large\n"); + exit(4); + } + printf("match (%d): \"%s\"\n", i, buf); + } + } + + if (matchptr[2].rm_so != -1) + { + if (matchptr[4].rm_so != -1) + option_parse(line, + matchptr[2].rm_so, matchptr[2].rm_eo, + matchptr[4].rm_so, matchptr[4].rm_eo); + + else if (matchptr[5].rm_so != -1) + option_parse(line, + matchptr[2].rm_so, matchptr[2].rm_eo, + matchptr[5].rm_so, matchptr[5].rm_eo); + else + fprintf(stderr,"config var set to nothing\n"); + } + } + else if (err == REG_NOMATCH) + { + fprintf(stderr,"error invalid config line: %s\n", line); + syntax_error = 1; + } + else + { + error_regerror(err, ®exp, "error from regcomp"); + exit(4); + } + + readcount = getline(&line, &linesize, file); + } + + fclose(file); + return syntax_error; +} + + + + static void cleanup() { free(fastback_file); free(fastback_ticket); + free(fastback_URL); } @@ -114,12 +268,21 @@ main(int argc, char** argv) exit(2); } - if (! fastback_file) + if (!fastback_file) { printf("error: fastback file not set\n"); exit(2); } + if (parse_config("fastback.conf")) + exit(2); + + if (fastback_URL) + printf("fastback URL: %s\n", fastback_URL); + else + printf("fastback URL not set\n"); + + if (fastback_file) printf("fastback file: %s\n", fastback_file); -- cgit