summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGavin Romig-Koch <gavin@localhost.localdomain>2009-08-10 15:25:12 -0400
committerGavin Romig-Koch <gavin@localhost.localdomain>2009-08-10 15:25:12 -0400
commit8b3591237771d6aa903870507f84032f59973a44 (patch)
treeb694472dde87c0328b9ecd243a6a09add03d1161
parent25d10eec1658be638ed2fec5ef958fe7acb2dbd1 (diff)
downloadfastback-8b3591237771d6aa903870507f84032f59973a44.tar.gz
fastback-8b3591237771d6aa903870507f84032f59973a44.tar.xz
fastback-8b3591237771d6aa903870507f84032f59973a44.zip
Task #2: collect config info from the configuration file
-rw-r--r--FIXME4
-rw-r--r--fastback.conf7
-rw-r--r--fastback.cpp165
3 files changed, 175 insertions, 1 deletions
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 <sys/types.h>
+#include <regex.h>
#include <stdlib.h>
#include <string.h>
#include <argp.h>
@@ -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( &regexp, pattern, 0);
+ if (err)
+ {
+ error_regerror(err, &regexp, "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( &regexp, 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, &regexp, "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);