/* * Return configuration option value from config file */ char *get_conf_option(char *option) { static char line[MAX_CONF_LINE + 1]; char *p = NULL; FILE *fp; fp = open_conf_file(); if (fp == NULL) { fprintf(stderr, "Unable to open conf file %s \n", conf_file); exit(1); } bzero(line, MAX_CONF_LINE); while ( fgets(line, MAX_CONF_LINE, fp) != NULL) { if (line[0] == '#') { bzero(line, MAX_CONF_LINE); continue; } p = strstr(line, option); if (p != NULL) { p = strchr(line, '='); if (p != NULL) { p++; p = str_rm_blank(p); str_rm_newline(p); break; } break; } } fclose(fp); return p; }
// 配置文件操作 dictionary *liao_init_cond(char *conf_file) { dictionary *dict = open_conf_file(conf_file); if (dict == NULL) { //log_error("errror"); return NULL; } return dict; }
dictionary *open_config(char *config_file) { dictionary *dict = open_conf_file(config_file); if (dict == NULL) { log_error("open config file fail:%s", config_file); return NULL; } return dict; }
static int config_init(void) { dictionary *conf; conf = open_conf_file(NULL); if (conf==NULL) { PRINT("cannot parse configure file."); return (CSF_ERR); } parse_conf_file(conf, "server", conf_int_array, conf_str_array); main_conf.cp_ops = get_comm_proto(main_conf.server_protocol); if (main_conf.cp_ops == NULL) CRIT("UNKNOWN COMMUNICATION PROTOCOL!"); close_conf_file(conf); return (CSF_OK); }
int scan_conf_file(struct util_list *list) { char buf[MAX_LINE_LEN], *tmp; int line = 0; FILE *fh; if (open_conf_file(&fh) < 0) return -ENOENT; while (fgets(buf, MAX_LINE_LEN, fh) != NULL) { line++; tmp = buf; while (isblank(*tmp)) tmp++; if (*tmp == '\n') continue; /* * Skip comments, comment must be "# " because # is a valid * EDF character. */ if (strlen(tmp) > 1 && *tmp == '#' && *(tmp + 1) == ' ') continue; /* remove trailing \n */ if (strlen(tmp) && *(tmp + strlen(tmp) - 1) == '\n') *(tmp + strlen(tmp) - 1) = '\0'; if (filetype_valid(tmp, line)) add_filetype(tmp, list); } fclose(fh); free(conf_file); return 0; }
/** * 加载配置文件 */ void init_conf(){ buffer_t *rbuf=alloc_buffer(CONF_READ_BUF); int fd=open_conf_file(); enum PARSE_STATE status=RAW; config_module_t *current_module; command_t *current_command; char_t *module_name=init_char(); char_t *command_key=init_char(); char_t *command_value=init_char(); while(1){ if(has_space(rbuf)){ intptr_t count=read(fd,rbuf->data+rbuf->limit,rbuf->size-rbuf->limit); if(count<=0){ goto CONFIG_END; } rbuf->limit+=count; } while(has_remaining(rbuf)){ char c=*(rbuf->data+rbuf->current); rbuf->current+=1; if(status==RAW&&c==brace_start){ status=MODULE_START; }else if(status==MODULE_START&&!char_is_special(c)){ if(c==brace_start){ add_terminal(module_name); current_module=find_config_module(module_name); if(current_module==NULL){ my_log(ERROR,"config error,please check\n"); goto CONFIG_END; }else{ status=FIND_COMMAND_KEY; } }else if(c==brace_end){ goto CONFIG_END; }else{ add_char(module_name,c); } }else if(status==FIND_COMMAND_KEY&&!char_is_special(c)){ if(c==brace_end){ current_module=NULL; reset_char(module_name); status=MODULE_START; }else{ add_char(command_key,c); status=COMMAND_KEY_START; } }else if(status==COMMAND_KEY_START){ if(!char_is_special(c)){ add_char(command_key,c); }else{ add_terminal(command_key); current_command=find_config_command(command_key,current_module); if(current_command==NULL){ my_log(ERROR,"config error,please check\n"); goto CONFIG_END; } status=COMMAND_VALUE_START; } }else if(status==COMMAND_VALUE_START&&!char_is_special(c)){ if(c!=semicolon){ add_char(command_value,c); }else{ add_terminal(command_value); current_command->set_value(command_value); current_command=NULL; reset_char(command_value); reset_char(command_key); status=FIND_COMMAND_KEY; } } } } CONFIG_END: destroy_buffer(rbuf); }
static int lp_config_init() { /* default */ lp_configs.log_level = 3; lp_configs.max_threads = 128; lp_configs.task_manager_num = 1; lp_configs.ldap_num = 4; lp_configs.mysql_num = 8; lp_configs.thread_stack_size = 2 * 1024; lp_configs.max_connections = 2 * 1024; lp_configs.listening_ip = NULL; lp_configs.listening_port = 389; lp_configs.timeout = 30; /* read configure file */ struct conf_int_config log_conf[] = { {"log_level", &lp_configs.log_level}, {0, 0} }; struct conf_str_config server_str_conf[] = { {"listening", lp_configs.listening_ip}, {0, 0} }; struct conf_int_config server_int_conf[] = { {"port", &lp_configs.listening_port}, {"max_connections", &lp_configs.max_connections}, {"timeout", &lp_configs.timeout}, {0, 0} }; int thread_stack_size = 0; struct conf_int_config thread_conf[] = { {"max_threads", &lp_configs.max_threads}, {"thread_stack_size", &thread_stack_size}, {"task_manager_num", &lp_configs.task_manager_num}, {"ldap_num", &lp_configs.ldap_num}, {"mysql_num", &lp_configs.mysql_num}, {0, 0} }; dictionary *conf = NULL; char *conf_file = (lp_config_file) ? lp_config_file : LP_CONF_FILE; conf = open_conf_file(conf_file); if (conf == NULL) { stk_log_error("read configure file failed! Use Default config!!"); /* stack size in KBytes */ lp_configs.thread_stack_size *= 1024; __show_all_config(); /* DEBUG use LP_ERR, release use LP_OK */ return LP_OK; } parse_conf_file(conf, "thread", thread_conf, NULL); /* stack size in KBytes */ lp_configs.thread_stack_size = (thread_stack_size == 0 ? lp_configs.thread_stack_size : thread_stack_size ) * 1024; parse_conf_file(conf, "server", server_int_conf, server_str_conf); parse_conf_file(conf, "log", log_conf, NULL); __show_all_config(); return LP_OK; }