Changeset 808 for trunk


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.

Location:
trunk/ithildin
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/ithildin/include/ithildin/conf.h

    r578 r808  
    4747 
    4848/* general conf handling calls */ 
    49 extern conf_list_t *read_conf(char *); 
     49char *find_conf_file(const char *); 
     50conf_list_t *read_conf(const char *); 
    5051void destroy_conf_branch(conf_list_t *); 
    5152void conf_display_tree(int, conf_list_t *); 
  • trunk/ithildin/include/ithildin/util.h

    r787 r808  
    1414char *mmap_file(char *); 
    1515char **mmap_file_to_array(char *); 
    16  
    1716char *sfgets(char *, int, FILE *); 
    1817 
    19 /* these are wrappers for malloc() family functions */ 
    20 void *calloc_wrap(size_t number, size_t size, char *file, int line); 
    21 void free_wrap(void *memory, char *file, int line); 
    22 void *malloc_wrap(size_t size, char *file, int line); 
    23 void *realloc_wrap(void *ptr, size_t size, char *file, int line); 
     18bool is_absolute_path(const char *); 
     19char *basename(const char *); 
     20char *dirname(const char *); 
    2421 
    2522/* subtract timeval 1 from timeval 2 */ 
  • 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) 
  • trunk/ithildin/lib/module.c

    r806 r808  
    7070    bool new = false; 
    7171    char *s = NULL; 
     72    char *dname = NULL; 
    7273 
    7374    if (cep->type == CONF_TYPE_LIST) { 
     
    9798    if (clp != NULL) { 
    9899        if ((s = conf_find_entry("config", clp, 1)) != NULL) { 
    99             /* try to access their config file as specified, if it does not 
    100              * come with a path prefix, we try prepending the conf 
    101              * directory, then with the full pathname make sure we can load 
    102              * the conf.  if we can't we won't load the module */ 
    103             m->conffile = malloc(PATH_MAX); 
    104             if (*s != 's') /* XXX: this will suck come windows support time */ 
    105                 snprintf(m->conffile, PATH_MAX, "%s/%s", me.conf_path, s); 
    106             else 
    107                 strlcpy(m->conffile, s, PATH_MAX); 
    108  
    109         } 
    110  
    111         if ((s = conf_find_entry("load", clp, 1)) != NULL && 
    112                 str_conv_bool(s, false)) 
    113             m->flags |= MODULE_FL_AUTOLOAD; 
    114         else 
     100            /* Let the config code sort out the correct path.. */ 
     101            m->conffile = strdup(s); 
     102        } 
     103 
     104        if ((s = conf_find_entry("load", clp, 1)) != NULL) { 
     105            if (str_conv_bool(s, false)) 
     106                m->flags |= MODULE_FL_AUTOLOAD; 
     107        } else 
    115108            m->flags |= MODULE_FL_AUTOLOAD; /* default is to load. */ 
    116109         
     
    128121        /* let's also take a stab at guessing their module config file name. 
    129122         * We try two things: $confdir/modulename.conf and 
    130          * $confdir/modulename/modulename.conf.  If neither works we do not 
    131          * make any further efforts.  We determine the usability of these 
    132          * files through the access() call. */ 
     123         * $confdir/modulename/modulename.conf.  For modules with '.'s in 
     124         * their name we replace the '.'s with path separators and try that 
     125         * way, using the last portion of the module's name as the name of 
     126         * the config file. */ 
     127        s = dname = strdup(m->name); 
     128        while (*s != '\0') { 
     129            if (*s == '.') 
     130                *s = '/'; 
     131            s++; 
     132        } 
     133 
    133134        s = malloc(PATH_MAX); 
    134         snprintf(s, PATH_MAX, "%s/%s.conf", me.conf_path, m->name); 
     135        /* first try the path without the additional directory layer */ 
     136        snprintf(s, PATH_MAX, "%s/%s.conf", me.conf_path, dname); 
    135137        if (access(s, R_OK) == 0) 
    136138            m->conffile = s; 
    137139        else { 
    138             /* don't try the directory method if the module name already 
    139              * contains slashes (indicating it is subdirectory-ized already) */ 
    140             if (!strchr(s, '/')) { 
    141                 snprintf(s, PATH_MAX, "%s/%s/%s.conf", me.conf_path, m->name, 
    142                         m->name); 
    143                 if (access(s, R_OK) == 0) 
    144                     m->conffile = s; 
    145             } 
    146         } 
     140            /* Now try the subdirectory method, this is a little weird since 
     141             * we need to get just the last component of the module's name 
     142             * (if it has dots in it) */ 
     143            snprintf(s, PATH_MAX, "%s/%s/%s.conf", me.conf_path, dname, 
     144                    (strchr(m->name, '.') ? strrchr(m->name, '.') + 1 : 
     145                     m->name)); 
     146            if (access(s, R_OK) == 0) 
     147                m->conffile = s; 
     148        } 
     149        free(dname); 
     150        if (m->conffile == NULL) 
     151            free(s); 
    147152    } 
    148153 
  • trunk/ithildin/lib/util.c

    r787 r808  
    4141char *canonize_make_human(int size, char type); 
    4242 
    43 /* mmap a file into a single string */ 
     43/* memory map a file into a single string (using malloc, though, not 
     44 * mmap...) */ 
    4445char *mmap_file(char *file) { 
    4546    struct stat sb; 
     
    6364 
    6465    fp = fopen(file, "r"); 
    65     if (fp==NULL) { 
     66    if (fp == NULL) { 
    6667        free(data); 
    6768        return NULL; 
     
    127128} 
    128129 
     130#define PATH_SEPARATOR '/' 
     131/* A function to determine whether a filename given is an absolute path or 
     132 * not.  For unix systems this is a trivial check for a '/', on win32 it 
     133 * requires that we examine the path for drive letter/etc */ 
     134bool is_absolute_path(const char *fname) { 
     135    if (*fname == PATH_SEPARATOR) 
     136        return true; 
     137    return false; 
     138} 
     139 
     140/* get the base/dirname of a file (portably).  these are also provided by 
     141 * some operating systems, and we can happily interoperate with those 
     142 * implementations. */ 
     143char bdname_buf[PATH_MAX]; 
     144char *basename(const char *fname) { 
     145    char *s; 
     146 
     147    if (strlen(fname) >= PATH_MAX) { 
     148        errno = EINVAL; 
     149        return NULL; 
     150    } 
     151 
     152    /* chew off any trailing slashes */ 
     153    strlcpy(bdname_buf, fname, PATH_MAX); 
     154    s = bdname_buf + strlen(fname) - 1; 
     155    while (*s == PATH_SEPARATOR && s >= bdname_buf) 
     156        *s-- = '\0'; 
     157 
     158    if ((s = strrchr(bdname_buf, PATH_SEPARATOR)) == NULL) 
     159        /* no path separator, easy.. */ 
     160        return bdname_buf; 
     161    else { 
     162        /* just move the end of the string to the beginning with memcpy */ 
     163        s++; 
     164        memcpy(bdname_buf, s, strlen(s) + 1); 
     165        return bdname_buf; 
     166    } 
     167} 
     168 
     169char *dirname(const char *fname) { 
     170    char *s; 
     171 
     172    if (strlen(fname) >= PATH_MAX) { 
     173        errno = EINVAL; 
     174        return NULL; 
     175    } 
     176 
     177    /* chew off any trailing slashes */ 
     178    strlcpy(bdname_buf, fname, PATH_MAX); 
     179    s = bdname_buf + strlen(fname) - 1; 
     180    while (*s == PATH_SEPARATOR && s >= bdname_buf) 
     181        *s-- = '\0'; 
     182 
     183    if ((s = strrchr(bdname_buf, PATH_SEPARATOR)) == NULL) { 
     184        strcpy(bdname_buf, "."); 
     185        return bdname_buf; 
     186    } else { 
     187        while (*s == PATH_SEPARATOR && s >= bdname_buf) 
     188            *s-- = '\0'; 
     189        if (*bdname_buf == '\0') 
     190            strcpy(bdname_buf, "."); 
     191        return bdname_buf; 
     192    } 
     193} 
     194 
    129195struct timeval *subtract_timeval(struct timeval tv1, struct timeval tv2) { 
    130196    static struct timeval result; 
  • trunk/ithildin/modules/ircd/command.c

    r806 r808  
    3333    /* only do all this stuff if the command doesn't exist. */ 
    3434    if ((cmd = find_command(name)) == NULL || cmd->flags & COMMAND_FL_ALIAS) { 
    35         sprintf(mname, "ircd/commands/%s", name); 
     35        sprintf(mname, "ircd.command.%s", name); 
    3636 
    3737        /* see if this is an alias, if it is warn the user that it is about to 
  • trunk/ithildin/modules/ircd/conf.c

    r806 r808  
    372372        } 
    373373        if (find_command(ctmp->string) == NULL) { 
    374             sprintf(mname, "ircd/commands/%s", ctmp->string); 
     374            sprintf(mname, "ircd.command.%s", ctmp->string); 
    375375            if (load_module(mname, MODULE_FL_CREATE|MODULE_FL_QUIET)) 
    376376                log_debug("loaded command module for %s", ctmp->string); 
     
    472472} 
    473473 
     474/* also handle usermodes and channel modes.. */ 
    474475static void ircd_parse_addons(conf_list_t *conf) { 
    475476    char *s = NULL; 
    476477    char mname[PATH_MAX]; 
    477478 
    478     while ((s = conf_find_entry_next("addon", s, conf, 1)) != NULL) { 
    479         sprintf(mname, "ircd/addons/%s", s); 
    480  
    481         if (module_loaded(mname) != 0) 
    482             continue; 
    483  
    484         if (!load_module(mname, 
    485                     MODULE_FL_CREATE|MODULE_FL_QUIET|MODULE_FL_EXPORT)) 
    486             log_error("unable to load module for addon %s", s); 
    487         else 
    488             log_debug("loaded addon module %s", s); 
    489     } 
     479#define ircd_parse_addons_load(type) do {                                     \ 
     480    s = NULL;                                                                 \ 
     481    while ((s = conf_find_entry_next(type, s, conf, 1)) != NULL) {            \ 
     482        sprintf(mname, "ircd.%s.%s", type, s);                                \ 
     483        if (module_loaded(mname) != 0)                                        \ 
     484            continue;                                                         \ 
     485        if (!load_module(mname, MODULE_FL_CREATE|MODULE_FL_QUIET))            \ 
     486            log_error("unable to load module for %s %s", type, s);            \ 
     487        else                                                                  \ 
     488            log_debug("loaded %s module %s", type, s);                        \ 
     489    }                                                                         \ 
     490} while (0) 
     491    ircd_parse_addons_load("addon"); 
     492    ircd_parse_addons_load("chanmode"); 
     493    ircd_parse_addons_load("usermode"); 
     494#undef ircd_parse_addons_load 
    490495} 
    491496 
  • trunk/ithildin/modules/ircd/ircd.c

    r804 r808  
    297297                PRIVILEGE_FL_BOOL, &ui64,  NULL); 
    298298    } 
    299  
    300     /* Initialize our modes.  Pass the success of the savedata request in to 
    301      * the loader to tell it what to do. */ 
    302     usermode_init(get_module_savedata(savelist, "ircd.umodes", 
    303                 &ircd.umodes)); 
    304     chanmode_init(get_module_savedata(savelist, "ircd.cmodes", 
    305                 &ircd.cmodes)); 
    306299 
    307300    /* setup our events.  started is always created, since it is destroyed 
     
    473466        ircd.mdext.class->iter = class_mdext_iter; 
    474467    } 
     468 
     469    /* Initialize our modes.  Pass the success of the savedata request in to 
     470     * the loader to tell it what to do. */ 
     471    usermode_init(get_module_savedata(savelist, "ircd.umodes", 
     472                &ircd.umodes)); 
     473    chanmode_init(get_module_savedata(savelist, "ircd.cmodes", 
     474                &ircd.cmodes)); 
    475475 
    476476    if (reload) { 
  • trunk/ithildin/modules/ircd/protocol.c

    r806 r808  
    179179    protocol_t *pp; 
    180180 
    181     sprintf(mname, "ircd/protocols/%s", name); 
     181    sprintf(mname, "ircd.protocol.%s", name); 
    182182    if (!load_module(mname, MODULE_FL_CREATE|MODULE_FL_QUIET)) { 
    183183        log_error("unable to load module for protocol %s", name); 
Note: See TracChangeset for help on using the changeset viewer.