Ignore:
Timestamp:
06/05/07 10:46:00 (5 years ago)
Author:
wd
Message:

Another milestone. Builds, starts without errors.. then doesn't take
connections, buy hey.

Also made the config file finder a bit smarter about looking for config
files.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ithildin/lib/conf.c

    r787 r808  
    1515 
    1616/* these are parsing functions */ 
    17 static char *conf_preparse(char *, char *); 
    18 static conf_list_t *conf_parse(char *, int, char *); 
     17static char *conf_preparse(const char *, char *); 
     18static conf_list_t *conf_parse(const char *, int, char *); 
    1919static conf_entry_t *merge_into_conf(conf_list_t *, conf_entry_t *, 
    2020        conf_list_t *); 
    2121static char *conf_expand_text(char *); 
     22static const char *included_from = NULL; 
    2223 
    2324#define parse_err(x) log_error("%s:%d %s", file, line, x); 
     
    2627 * tree headed at the conf_list structure it returns.  The rest of the 
    2728 * functions in this file can then be used to look at this data.  */ 
    28 conf_list_t *read_conf(char *file) { 
     29conf_list_t *read_conf(const char *file) { 
    2930    char *stuff = NULL; 
    3031    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    } 
    3159 
    3260    /* 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); 
    3464        return NULL; 
     65    } 
    3566 
    3667    /* 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); 
    3870        return NULL; 
     71    } 
    3972     
    4073    /* now parse */ 
    41     list = conf_parse(file, 1, stuff); 
     74    list = conf_parse(fname, 1, stuff); 
    4275    /* 'stuff' is now not necessary one way or the other */ 
    4376    free(stuff); 
    44  
    45     if (list == NULL) 
    46         return NULL; 
    47  
    48     /* display_tree(0, list);*/ 
     77    free(fname); 
     78 
    4979    return list; 
    5080} 
     
    6999 
    70100/* 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. */ 
    72102void conf_display_tree(int depth, conf_list_t *list) { 
    73103    int i; 
     
    81111                    ep->string); 
    82112        else { 
    83             printf("%s->[%s]\n", (depth ? "\b" : ""), ep->name); 
     113            printf("%s->[%s]\n", (depth ?  "\b" : ""), ep->name); 
    84114            conf_display_tree(depth + 1, ep->list); 
    85115        } 
     
    237267 * and handles reading in included files.  It also does light sanity-checking 
    238268 * to make sure comments and strings are properly terminated. */ 
    239 static char *conf_preparse(char *file, char *oldstr) { 
     269static char *conf_preparse(const char *file, char *oldstr) { 
    240270    char *s = oldstr; 
    241271    char *newstr = malloc(strlen(oldstr) + 1); 
     
    344374/* do the final parsing of the data, extract name/value pairs and build our 
    345375 * tree, and all with a spot of recursion.  yum! */ 
    346 static conf_list_t *conf_parse(char *file, int line, char *str) { 
     376static conf_list_t *conf_parse(const char *file, int line, char *str) { 
    347377    conf_list_t *list = malloc(sizeof(conf_list_t)); 
    348378    conf_entry_t *ent = NULL, *last = NULL; 
     
    351381    int startline = 0; 
    352382 
    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    }                                                                         \ 
    362392} while (0) 
    363393 
     
    469499                conf_list_t *inclist; 
    470500 
     501                included_from = file; 
    471502                if ((inclist = read_conf(ent->string)) == NULL) { 
    472503                    parse_err("error in included file:"); 
     
    479510                    last = merge_into_conf(list, last, inclist); 
    480511                } 
     512                included_from = NULL; 
    481513            } else { 
    482514                if (last == NULL) 
Note: See TracChangeset for help on using the changeset viewer.