📄 mconfig.c
字号:
mconfig *mconfig_parse(mconfig *conf, const char *cf) { FILE *f; int current_group = M_CONFIG_GROUP_IGNORE; char buf[255]; int locale_is_not_setup_yet = 1; int ln = 0; char *_filename = NULL; const mconfig_values config_values[] = { /* strings */ {"inputplugin", M_CONFIG_TYPE_STRING, M_CONFIG_VALUE_IGNORE, &(conf->inputplugin)}, {"outputplugin", M_CONFIG_TYPE_STRING, M_CONFIG_VALUE_IGNORE, &(conf->outputplugin)}, {"processorplugin", M_CONFIG_TYPE_STRING, M_CONFIG_VALUE_IGNORE, &(conf->processorplugin)}, {"outputdir", M_CONFIG_TYPE_STRING, M_CONFIG_VALUE_OVERWRITE, &(conf->outputdir)}, {"language", M_CONFIG_TYPE_STRING, M_CONFIG_VALUE_OVERWRITE, &(conf->language)}, /* integers */ {"incremental", M_CONFIG_TYPE_INT, M_CONFIG_VALUE_OVERWRITE, &(conf->incremental)}, {"debug_level", M_CONFIG_TYPE_INT, M_CONFIG_VALUE_OVERWRITE, &(conf->debug_level)}, {"gen_report_threshold", M_CONFIG_TYPE_INT, M_CONFIG_VALUE_OVERWRITE, &(conf->gen_report_threshold)}, {"disable_resolver", M_CONFIG_TYPE_INT, M_CONFIG_VALUE_OVERWRITE, &(conf->disable_resolver)}, /* strings */ {"default_configfile", M_CONFIG_TYPE_STRING, M_CONFIG_VALUE_IGNORE, &(conf->default_configfile)}, {NULL, M_CONFIG_TYPE_INT, 0, NULL} }; #ifdef SYSCONFDIR const char *def_cf_dir = SYSCONFDIR;#else const char *def_cf_dir = "./";#endif const char *def_cf_name = "modlogan.conf"; if (!cf) { _filename = malloc(strlen(def_cf_dir) + strlen(def_cf_name) + 2); strcpy(_filename, def_cf_dir); strcat(_filename, "/"); strcat(_filename, def_cf_name); } else { _filename = malloc(strlen(cf) + 1); strcpy(_filename, cf); } filename = _filename; if (!(f = fopen(filename, "r"))) { fprintf(stderr,"%s %s: %s\n", _("Can't open configfile"), filename, strerror(errno)); free(_filename); filename = NULL; return NULL; } printf("using configfile: %s\n",filename); while (fgets(buf, sizeof(buf)-1, f)) { if (buf[strlen(buf)-1] != '\n') { fprintf(stderr, "%s: %s\n", _("No newline at end of line or line longer then 255 charaters"), buf); free(_filename); filename = NULL; return NULL; } buf[strlen(buf)-1] = '\0'; ln++; linenumber = ln; if (!strlen(buf)) continue; if (*buf == '[') { char *key = buf + 1; char *subkey = NULL; char *c1 = key; while (*c1 && *c1 != ']') c1++; *c1 = '\0'; if ((subkey = strchr(key, '_'))) { *subkey++ = '\0'; } if (!strcmp(key, "global")) { current_group = M_CONFIG_GROUP_GLOBAL; } else if (!strcmp(key, "input")) { if (subkey && conf->inputplugin) { if (strcmp(conf->inputplugin, subkey) != 0) { current_group = M_CONFIG_GROUP_IGNORE; } else { if (!conf->input) if (mplugins_init_input(conf) != 0) return NULL; current_group = M_CONFIG_GROUP_INPUT; } } else { if (!conf->input) if (mplugins_init_input(conf) != 0) return NULL; current_group = M_CONFIG_GROUP_INPUT; } } else if (!strcmp(key, "output")) { if (subkey && conf->outputplugin) { if (strcmp(conf->outputplugin, subkey) != 0) { current_group = M_CONFIG_GROUP_IGNORE; } else { if (!conf->output) if (mplugins_init_output(conf) != 0) return NULL; current_group = M_CONFIG_GROUP_OUTPUT; } } else { if (!conf->output) if (mplugins_init_output(conf) != 0) return NULL; current_group = M_CONFIG_GROUP_OUTPUT; } } else if (!strcmp(key, "processor")) { if (subkey && conf->processorplugin) { if (strcmp(conf->processorplugin, subkey) != 0) { current_group = M_CONFIG_GROUP_IGNORE; } else { if (!conf->processor) if (mplugins_init_processor(conf) != 0) return NULL; current_group = M_CONFIG_GROUP_PROCESSOR; } } else { if (!conf->processor) if (mplugins_init_processor(conf) != 0) return NULL; current_group = M_CONFIG_GROUP_PROCESSOR; } } else { current_group = M_CONFIG_GROUP_UNKNOWN; fprintf(stderr, "%s (%s:%i): %s\n", _("Unknown section in line"), filename, linenumber, buf); } if (current_group != 0 && current_group != M_CONFIG_GROUP_GLOBAL && locale_is_not_setup_yet) { init_locale(conf); locale_is_not_setup_yet = 0; } } else if (*buf == '#') { /* a comment */ } else { char *key, *value, *c1; int i; key = buf; if (!(value = strchr(key, '='))) { fprintf(stderr,"%s (%s:%i): %s\n", _("no '=' found in line"), filename, linenumber, buf); free(_filename); filename = NULL; return NULL; } c1 = value; /* remove whitespaces at the end of the key */ while (c1 > key) { if (!isspace(*(c1-1))) { break; } c1--; } *c1 = '\0'; c1 = value+1; while (*c1) { if (!isspace(*c1)) { break; } c1++; } value = c1; c1 = value + strlen(value); while (c1 > value) { if (!isspace(*(c1-1))) { break; } c1--; } *c1 = '\0'; switch(current_group) { case M_CONFIG_GROUP_GLOBAL: { i = 0; while (config_values[i].string) { if (!strcmp(config_values[i].string, key)) break; i++; } if (config_values[i].string) { int parse_default_file = 0; if (!strcmp(config_values[i].string, "default_configfile")) { if (conf->default_configfile == NULL) { parse_default_file = 1; } } mconfig_insert_value(config_values[i].dest, config_values[i].type, value, config_values[i].value_def); if (conf->default_configfile != NULL && parse_default_file == 1) { mconfig_parse(conf, conf->default_configfile); filename = _filename; } } else { fprintf(stderr, "[global] %s (%s:%i): %s\n", _("Unknown Keyword in line"), filename, linenumber, buf); } break; } case M_CONFIG_GROUP_INPUT: { if (mplugins_input_parse_config(conf, key, value)) { fprintf(stderr, "[input] %s (%s:%i): %s\n", _("Unknown Keyword in line"), filename, linenumber, buf); } break; } case M_CONFIG_GROUP_OUTPUT: { if (mplugins_output_parse_config(conf, key, value)) { fprintf(stderr, "[output] %s (%s:%i): %s\n", _("Unknown Keyword in line"), filename, linenumber, buf); } break; } case M_CONFIG_GROUP_PROCESSOR: { if (mplugins_processor_parse_config(conf, key, value)) { fprintf(stderr, "[processor] %s (%s:%i): %s\n", _("Unknown Keyword in line"), filename, linenumber, buf); } break; } case M_CONFIG_GROUP_IGNORE: break; default: fprintf(stderr, "%s (%s:%i): %s\n", _("Unknown section in line"), filename, linenumber, buf); } } } fclose (f); if (conf->debug_level > 0) fprintf(stderr, "%s: %s \n", filename, _("Reading config - finished")); free(_filename); filename = NULL; return conf;}mconfig *mconfig_init(const char *cf) { mconfig *conf = malloc(sizeof(mconfig)); int no_plug_error = 0; if (!conf) return NULL; memset(conf, 0, sizeof(mconfig)); if (!mconfig_parse(conf, cf)) return NULL;#ifdef NO_PLUGINS if (strcmp(conf->inputplugin, INPUT_PLUGIN) ) { printf("section [global]: inputplugin is not %s\n", INPUT_PLUGIN); no_plug_error = 1; } if (strcmp(conf->processorplugin, PROCESSOR_PLUGIN) ) { printf("section [global]: processorplugin is not %s\n", PROCESSOR_PLUGIN); no_plug_error = 1; } if (strcmp(conf->outputplugin, OUTPUT_PLUGIN) ) { printf("section [global]: outputplugin is not %s\n", OUTPUT_PLUGIN); no_plug_error = 1; } if (no_plug_error) { printf("You are using the 'static' modlogan which linked against the following plugins:\n"); printf("inputplugin -> %s\n", INPUT_PLUGIN); printf("processorplugin -> %s\n", PROCESSOR_PLUGIN); printf("outputplugin -> %s\n", OUTPUT_PLUGIN); printf("Your configfile is stating something different.\n\n"); printf("Please correct the error reported above and rerun modlogan.\n"); return NULL; }#endif if (conf->debug_level > 2) fprintf(stderr, "%s.%d: trying to init the plugins that aren't setup yet\n", __FILE__, __LINE__); /* default values */ if (!conf->input) { if (mplugins_init_input(conf) != 0) return NULL; } if (!conf->output) { if (mplugins_init_output(conf) != 0) return NULL; } if (!conf->processor) { if (mplugins_init_processor(conf) != 0) return NULL; } if (conf->debug_level > 2) fprintf(stderr, "%s.%d: doing some checks and set the default values\n", __FILE__, __LINE__);#ifdef HAVE_LIBADNS conf->adns = NULL; conf->query_hash = mhash_init();#endif if (mconfig_set_defaults(conf) != 0) return NULL; if (mplugins_input_set_defaults(conf) != 0) return NULL; if (mplugins_output_set_defaults(conf) != 0) return NULL; if (mplugins_processor_set_defaults(conf) != 0) return NULL; return conf;}int mconfig_free(mconfig *conf) { if (!conf) return -1; if (conf->outputdir) free(conf->outputdir); if (conf->outputplugin) free(conf->outputplugin); if (conf->inputplugin) free(conf->inputplugin); if (conf->processorplugin) free(conf->processorplugin); if (conf->default_configfile) free(conf->default_configfile);#ifdef HAVE_LIBADNS adns_finish(*(conf->adns)); if (conf->adns) free(conf->adns); if (conf->query_hash) mhash_free(conf->query_hash);#endif free(conf); return 0; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -