#include "p11-tests.h" #include #include #include #include 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_fatal("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_fatal("couldn't seek config file: %s", filename); assert(!config_data); config_data = malloc(len + 2); if(!config_data) p11t_msg_fatal("out of memory"); /* And read in one block */ if(fread(config_data, 1, len, f) != len) p11t_msg_fatal("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 = trim(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; }