summaryrefslogtreecommitdiff
path: root/src/config.c
diff options
context:
space:
mode:
authorStef Walter <stef@memberwebs.com>2008-12-05 02:37:04 +0000
committerStef Walter <stef@memberwebs.com>2008-12-05 02:37:04 +0000
commit01f5a4c3169f19d8648a80e913bc4d570e96346d (patch)
tree0c522a08ad29577ec200d8c47c61b7be5aed59fc /src/config.c
parent8184a9487d71af2eb7af2bd0bab3022d39995633 (diff)
Added config file support.
Diffstat (limited to 'src/config.c')
-rw-r--r--src/config.c110
1 files changed, 110 insertions, 0 deletions
diff --git a/src/config.c b/src/config.c
new file mode 100644
index 0000000..65cc70c
--- /dev/null
+++ b/src/config.c
@@ -0,0 +1,110 @@
+
+#include "config.h"
+
+#include "p11-tests.h"
+
+#include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+static char* config_data = NULL;
+
+static char*
+trim(char* data)
+{
+ char *t;
+ while(*data && isspace(*data))
+ ++data;
+
+ t = data + strlen(data);
+ while(t > data && isspace(*(t - 1)))
+ {
+ t--;
+ *t = 0;
+ }
+
+ return data;
+}
+
+void
+p11t_config_parse(const char* filename)
+{
+ char* name = NULL;
+ char* value = NULL;
+ char* next;
+ FILE* f = NULL;
+ long len;
+ char* p;
+ char* t;
+
+ f = fopen(filename, "r");
+ if(f == NULL)
+ p11t_msg_exit(1, "couldn't open config file: %s", filename);
+
+ /* Figure out size */
+ if(fseek(f, 0, SEEK_END) == -1 || (len = ftell(f)) == -1 || fseek(f, 0, SEEK_SET) == -1)
+ p11t_msg_exit(1, "couldn't seek config file: %s", filename);
+
+ assert(!config_data);
+ config_data = malloc(len + 2);
+ if(!config_data)
+ p11t_msg_exit(1, "out of memory");
+
+ /* And read in one block */
+ if(fread(config_data, 1, len, f) != len)
+ p11t_msg_exit(1, "couldn't read config file: %s", filename);
+
+ fclose(f);
+
+ /* Null terminate the data */
+ config_data[len] = '\n';
+ config_data[len + 1] = 0;
+
+ next = config_data;
+
+ /* Go through lines and process them */
+ while((t = strchr(next, '\n')) != NULL)
+ {
+ *t = 0;
+ p = next; /* Do this before cleaning below */
+ next = t + 1;
+
+ t = trim(p);
+
+ name = NULL;
+ value = NULL;
+
+ /* Empty lines / comments at start / comments without continuation */
+ if(!*t || *p == '#')
+ continue;
+
+ /* Look for the break between name = value on the same line */
+ t = p + strcspn(p, ":=");
+ if(!*t)
+ {
+ fprintf(stderr, "p11-test: invalid config line: %s", p);
+ exit(1);
+ }
+
+ /* Null terminate and split value part */
+ *t = 0;
+ t++;
+
+ name = trim(p);
+ value = t;
+
+ if(name && value)
+ {
+ p11t_module_config(name, value);
+ p11t_session_config(name, value);
+ }
+ }
+}
+
+void
+p11t_config_cleanup(void)
+{
+ free(config_data);
+ config_data = NULL;
+}