cfg.c

来自「The Kannel Open Source WAP and SMS gatew」· C语言 代码 · 共 732 行 · 第 1/2 页

C
732
字号
    List *stack;     Octstr *name;    Octstr *value;    Octstr *filename;     CfgGroup *grp;    long equals;    long lineno;    long error_lineno;        loc = loc_inc = NULL;    /*      * expand initial main config file and add it to the recursion      * stack to protect against cycling      */     if ((lines = expand_file(cfg->filename, 1)) == NULL) {         panic(0, "Failed to load main configuration file `%s'. Aborting!",               octstr_get_cstr(cfg->filename));     }     stack = list_create();     list_insert(stack, 0, octstr_duplicate(cfg->filename));     grp = NULL;    lineno = 0;    error_lineno = 0;    while (error_lineno == 0 && (loc = list_extract_first(lines)) != NULL) {         octstr_strip_blanks(loc->line);         if (octstr_len(loc->line) == 0) {             if (grp != NULL && add_group(cfg, grp) == -1) {                 error_lineno = loc->line_no;                 destroy_group(grp);             }             grp = NULL;         } else if (octstr_get_char(loc->line, 0) != '#') {             equals = octstr_search_char(loc->line, '=', 0);             if (equals == -1) {                 error(0, "An equals sign ('=') is missing on line %ld of file %s.",                       loc->line_no, octstr_get_cstr(loc->filename));                 error_lineno = loc->line_no;             } else                           /*              * check for special config directives, like include or conditional              * directives here              */             if (octstr_search(loc->line, octstr_imm("include"), 0) != -1) {                 filename = octstr_copy(loc->line, equals + 1, octstr_len(loc->line));                 parse_value(filename);                  /* check if we are cycling */                 if (list_search(stack, filename, octstr_item_match) != NULL) {                     panic(0, "Recursive include for config file `%s' detected "                              "(on line %ld of file %s).",                           octstr_get_cstr(filename), loc->line_no,                            octstr_get_cstr(loc->filename));                 } else {                         List *files = list_create();                    Octstr *file;                    struct stat filestat;                    /* check if included file is a directory */                    lstat(octstr_get_cstr(filename), &filestat);                    /*                      * is a directory, create a list with files of                     * this directory and load all as part of the                     * whole configuration.                     */                    if (S_ISDIR(filestat.st_mode)) {                        DIR *dh;                        struct dirent *diritem;                        debug("gwlib.cfg", 0, "Loading include dir `%s' "                              "(on line %ld of file %s).",                                octstr_get_cstr(filename), loc->line_no,                                octstr_get_cstr(loc->filename));                         dh = opendir(octstr_get_cstr(filename));                        while ((diritem = readdir(dh))) {                            Octstr *fileitem;                            fileitem = octstr_duplicate(filename);                            octstr_append(fileitem, octstr_create("/"));                            octstr_append(fileitem, octstr_create(diritem->d_name));                            lstat(octstr_get_cstr(fileitem), &filestat);                            if (!S_ISDIR(filestat.st_mode)) {                                list_insert(files, 0, fileitem);                            }                        }                        closedir(dh);                    } 		                        /* is a file, create a list with it */                    else {                        list_insert(files, 0, octstr_duplicate(filename));                    }                    /* include files */                    while ((file = list_extract_first(files)) != NULL) {                        list_insert(stack, 0, octstr_duplicate(file));                         debug("gwlib.cfg", 0, "Loading include file `%s' (on line %ld of file %s).",                                octstr_get_cstr(file), loc->line_no,                                octstr_get_cstr(loc->filename));                         /*                           * expand the given include file and add it to the current                          * processed main while loop                          */                         if ((expand = expand_file(file, 0)) != NULL) {                            while ((loc_inc = list_extract_first(expand)) != NULL)                                 list_insert(lines, 0, loc_inc);                         } else {                             panic(0, "Failed to load whole configuration. Aborting!");                         }                                          list_destroy(expand, NULL);                         cfgloc_destroy(loc_inc);                        octstr_destroy(file);                    }                    list_destroy(files, octstr_destroy_item);                }                 octstr_destroy(filename);             }                           /*              * this is a "normal" line, so process it accodingly              */             else  {                 name = octstr_copy(loc->line, 0, equals);                 octstr_strip_blanks(name);                 value = octstr_copy(loc->line, equals + 1, octstr_len(loc->line));                 parse_value(value);      	    	if (grp == NULL)                    grp = create_group();                                  if (grp->configfile != NULL) {                    octstr_destroy(grp->configfile); grp->configfile=NULL;                }                grp->configfile = octstr_duplicate(cfg->filename);                 cfg_set(grp, name, value);                 octstr_destroy(name);                 octstr_destroy(value);             }         }         cfgloc_destroy(loc);     }    if (grp != NULL && add_group(cfg, grp) == -1) {        error_lineno = 1;         destroy_group(grp);     }    list_destroy(lines, NULL);     list_destroy(stack, octstr_destroy_item);     if (error_lineno != 0) {        error(0, "Error found on line %ld of file `%s'.",  	          error_lineno, octstr_get_cstr(cfg->filename));         return -1;     }    return 0;}CfgGroup *cfg_get_single_group(Cfg *cfg, Octstr *name){    return dict_get(cfg->single_groups, name);}List *cfg_get_multi_group(Cfg *cfg, Octstr *name){    List *list, *copy;    long i;        list = dict_get(cfg->multi_groups, name);    if (list == NULL)    	return NULL;    copy = list_create();    for (i = 0; i < list_len(list); ++i)    	list_append(copy, list_get(list, i));    return copy;}Octstr *cfg_get_group_name(CfgGroup *grp){    return octstr_duplicate(grp->name);}Octstr *cfg_get_configfile(CfgGroup *grp){    return octstr_duplicate(grp->configfile);}Octstr *cfg_get_real(CfgGroup *grp, Octstr *varname, const char *file,     	    	     long line, const char *func){    Octstr *os;    if(grp == NULL)     	panic(0, "Trying to fetch variable `%s' in non-existing group",	      octstr_get_cstr(varname));    if (grp->name != NULL && !is_allowed_in_group(grp->name, varname))    	panic(0, "Trying to fetch variable `%s' in group `%s', not allowed.",	      octstr_get_cstr(varname), octstr_get_cstr(grp->name));    os = dict_get(grp->vars, varname);    if (os == NULL)    	return NULL;    return gw_claim_area_for(octstr_duplicate(os), file, line, func);}int cfg_get_integer(long *n, CfgGroup *grp, Octstr *varname){    Octstr *os;    int ret;        os = cfg_get(grp, varname);    if (os == NULL)    	return -1;    if (octstr_parse_long(n, os, 0, 0) == -1)    	ret = -1;    else    	ret = 0;    octstr_destroy(os);    return ret;}int cfg_get_bool(int *n, CfgGroup *grp, Octstr *varname){    Octstr *os;    os = cfg_get(grp, varname);    if (os == NULL) {	*n = 0;    	return -1;    }    if (octstr_case_compare(os, octstr_imm("true")) == 0	|| octstr_case_compare(os, octstr_imm("yes")) == 0	|| octstr_case_compare(os, octstr_imm("on")) == 0	|| octstr_case_compare(os, octstr_imm("1")) == 0)    {	    	*n = 1;    } else if (octstr_case_compare(os, octstr_imm("false")) == 0	|| octstr_case_compare(os, octstr_imm("no")) == 0	|| octstr_case_compare(os, octstr_imm("off")) == 0	|| octstr_case_compare(os, octstr_imm("0")) == 0)    {	*n = 0;    }    else {	*n = 1;	warning(0, "bool variable set to strange value, assuming 'true'");    }    octstr_destroy(os);    return 0;}List *cfg_get_list(CfgGroup *grp, Octstr *varname){    Octstr *os;    List *list;        os = cfg_get(grp, varname);    if (os == NULL)    	return NULL;    list = octstr_split_words(os);    octstr_destroy(os);    return list;}void cfg_set(CfgGroup *grp, Octstr *varname, Octstr *value){    dict_put(grp->vars, varname, octstr_duplicate(value));}void grp_dump(CfgGroup *grp){    List *names;    Octstr *name;    Octstr *value;    if (grp->name == NULL)	debug("gwlib.cfg", 0, "  dumping group (name not set):");    else	debug("gwlib.cfg", 0, "  dumping group (%s):",	      octstr_get_cstr(grp->name));    names = dict_keys(grp->vars);    while ((name = list_extract_first(names)) != NULL) {	value = cfg_get(grp, name);	debug("gwlib.cfg", 0, "    <%s> = <%s>", 	      octstr_get_cstr(name),	      octstr_get_cstr(value));    	octstr_destroy(value);    	octstr_destroy(name);    }    list_destroy(names, NULL);}void cfg_dump(Cfg *cfg){    CfgGroup *grp;    List *list;    List *names;    Octstr *name;    debug("gwlib.cfg", 0, "Dumping Cfg %p", (void *) cfg);    debug("gwlib.cfg", 0, "  filename = <%s>",     	  octstr_get_cstr(cfg->filename));    names = dict_keys(cfg->single_groups);    while ((name = list_extract_first(names)) != NULL) {	grp = cfg_get_single_group(cfg, name);	if (grp != NULL)	    grp_dump(grp);    	octstr_destroy(name);    }    list_destroy(names, NULL);    names = dict_keys(cfg->multi_groups);    while ((name = list_extract_first(names)) != NULL) {	list = cfg_get_multi_group(cfg, name);	while ((grp = list_extract_first(list)) != NULL)	    grp_dump(grp);	list_destroy(list, NULL);    	octstr_destroy(name);    }    list_destroy(names, NULL);    debug("gwlib.cfg", 0, "Dump ends.");}void cfg_dump_all(void){    #define OCTSTR(name) \        printf("%s = <please consult user doc>\n", #name);    #define SINGLE_GROUP(name, fields) \        printf("#\n#  Single Group\n#\n"); \        printf("group = %s\n", #name); \        fields; \        printf("\n\n");    #define MULTI_GROUP(name, fields) \        printf("#\n#  Multi Group\n#\n"); \        printf("group = %s\n", #name); \        fields; \        printf("\n\n");    #include "cfg.def"}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?