Changeset 808 for trunk/ithildin/lib/conf.c
- Timestamp:
- 06/05/07 10:46:00 (5 years ago)
- File:
-
- 1 edited
-
trunk/ithildin/lib/conf.c (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/ithildin/lib/conf.c
r787 r808 15 15 16 16 /* these are parsing functions */ 17 static char *conf_preparse(c har *, char *);18 static conf_list_t *conf_parse(c har *, int, char *);17 static char *conf_preparse(const char *, char *); 18 static conf_list_t *conf_parse(const char *, int, char *); 19 19 static conf_entry_t *merge_into_conf(conf_list_t *, conf_entry_t *, 20 20 conf_list_t *); 21 21 static char *conf_expand_text(char *); 22 static const char *included_from = NULL; 22 23 23 24 #define parse_err(x) log_error("%s:%d %s", file, line, x); … … 26 27 * tree headed at the conf_list structure it returns. The rest of the 27 28 * functions in this file can then be used to look at this data. */ 28 conf_list_t *read_conf(c har *file) {29 conf_list_t *read_conf(const char *file) { 29 30 char *stuff = NULL; 30 31 conf_list_t *list = NULL; 32 char *fname = NULL; 33 34 /* Look for the file. If it is not an absolute path search first in the 35 * same directory as the file it is being included from (if any) and 36 * then relative to our configured path for config files. */ 37 if (is_absolute_path(file)) 38 fname = strdup(file); 39 else { 40 fname = malloc(PATH_MAX); 41 *fname = '\0'; 42 if (included_from != NULL) { 43 snprintf(fname, PATH_MAX, "%s/%s", dirname(included_from), file); 44 if (access(fname, R_OK) && errno == ENOENT) 45 *fname = '\0'; 46 } 47 if (*fname == '\0') 48 snprintf(fname, PATH_MAX, "%s/%s", me.conf_path, file); 49 if (access(fname, R_OK)) { 50 if (errno != ENOENT) 51 log_error("Could not access file %s (%s)", fname, 52 strerror(errno)); 53 else 54 log_error("Could not find config file %s", fname); 55 free(fname); 56 return NULL; 57 } 58 } 31 59 32 60 /* mmap the data in */ 33 if ((stuff = mmap_file(file)) == NULL) 61 if ((stuff = mmap_file(fname)) == NULL) { 62 log_error("Could not open config file %s", file); 63 free(fname); 34 64 return NULL; 65 } 35 66 36 67 /* weed out comments, and bad strings */ 37 if ((stuff = conf_preparse(file, stuff)) == NULL) 68 if ((stuff = conf_preparse(fname, stuff)) == NULL) { 69 free(fname); 38 70 return NULL; 71 } 39 72 40 73 /* now parse */ 41 list = conf_parse(f ile, 1, stuff);74 list = conf_parse(fname, 1, stuff); 42 75 /* 'stuff' is now not necessary one way or the other */ 43 76 free(stuff); 44 45 if (list == NULL) 46 return NULL; 47 48 /* display_tree(0, list);*/ 77 free(fname); 78 49 79 return list; 50 80 } … … 69 99 70 100 /* this is a debugging function whicb prints the data in 'list' out in a 71 * tree-like format to stdout. */101 * * tree-like format to stdout. */ 72 102 void conf_display_tree(int depth, conf_list_t *list) { 73 103 int i; … … 81 111 ep->string); 82 112 else { 83 printf("%s->[%s]\n", (depth ? "\b" : ""), ep->name);113 printf("%s->[%s]\n", (depth ? "\b" : ""), ep->name); 84 114 conf_display_tree(depth + 1, ep->list); 85 115 } … … 237 267 * and handles reading in included files. It also does light sanity-checking 238 268 * to make sure comments and strings are properly terminated. */ 239 static char *conf_preparse(c har *file, char *oldstr) {269 static char *conf_preparse(const char *file, char *oldstr) { 240 270 char *s = oldstr; 241 271 char *newstr = malloc(strlen(oldstr) + 1); … … 344 374 /* do the final parsing of the data, extract name/value pairs and build our 345 375 * tree, and all with a spot of recursion. yum! */ 346 static conf_list_t *conf_parse(c har *file, int line, char *str) {376 static conf_list_t *conf_parse(const char *file, int line, char *str) { 347 377 conf_list_t *list = malloc(sizeof(conf_list_t)); 348 378 conf_entry_t *ent = NULL, *last = NULL; … … 351 381 int startline = 0; 352 382 353 #define swallow_whitespace() do { \354 while (*str) { \355 if (*str == '\n') \356 line++; \357 if (isspace(*str)) \358 str++; \359 else \360 break; \361 } \383 #define swallow_whitespace() do { \ 384 while (*str) { \ 385 if (*str == '\n') \ 386 line++; \ 387 if (isspace(*str)) \ 388 str++; \ 389 else \ 390 break; \ 391 } \ 362 392 } while (0) 363 393 … … 469 499 conf_list_t *inclist; 470 500 501 included_from = file; 471 502 if ((inclist = read_conf(ent->string)) == NULL) { 472 503 parse_err("error in included file:"); … … 479 510 last = merge_into_conf(list, last, inclist); 480 511 } 512 included_from = NULL; 481 513 } else { 482 514 if (last == NULL)
Note: See TracChangeset
for help on using the changeset viewer.
