/* * Parse the ifd config file */ int ifd_config_parse(const char *filename) { char buffer[512]; int rc; if ((config_filename = filename) == NULL) config_filename = OPENCT_CONF_PATH; /* If config file doesn't exist, quietly sneak out of here */ if ((config_fd = open(config_filename, O_RDONLY)) < 0) { if (errno == ENOENT) return 0; ct_error("Unable to open %s: %m", filename); return -1; } /* Init parse buffer. */ ct_buf_init(&config_buf, buffer, sizeof(buffer)); config_line = 1; config_top.name = "<config>"; rc = conf_parse_group(&config_top, END_OF_FILE); close(config_fd); config_fd = -1; if (ct_config.debug > 2) conf_dump(&config_top, 0); return rc; }
//解析配置信息,并进行有效性判断 struct conf * conf_create(char *filename) { rstatus_t status; struct conf *cf; //打开配置文件,创建conf结构,并初始化 cf = conf_open(filename); if (cf == NULL) { return NULL; } /* validate configuration file before parsing */ status = conf_pre_validate(cf); //检查配置格式是否符合yml标准 if (status != NC_OK) { goto error; } /* parse the configuration file */ status = conf_parse(cf); //配置解析 if (status != NC_OK) { goto error; } /* validate parsed configuration */ status = conf_post_validate(cf); //重复性判断 if (status != NC_OK) { goto error; } conf_dump(cf); fclose(cf->fh); cf->fh = NULL; return cf; error: log_stderr("nutcracker: configuration file '%s' syntax is invalid", filename); fclose(cf->fh); cf->fh = NULL; conf_destroy(cf); return NULL; }
rmt_conf * conf_create(char *filename) { int ret; rmt_conf *cf; cf = conf_open(filename); if (cf == NULL) { return NULL; } /* validate configuration file before parsing */ ret = conf_pre_validate(cf); if (ret != RMT_OK) { goto error; } conf_organizations_dump(cf); /* parse the configuration file */ ret = conf_parse(cf); if (ret != RMT_OK) { goto error; } /* validate parsed configuration */ ret = conf_post_validate(cf); if (ret != RMT_OK) { goto error; } conf_dump(cf); fclose(cf->fh); cf->fh = NULL; return cf; error: fclose(cf->fh); cf->fh = NULL; conf_destroy(cf); return NULL; }
/* * Debugging - dump the config tree */ static void conf_dump(ifd_conf_node_t * node, int indent) { for (; node; node = node->next) { printf("%*.*s%s", indent, indent, "", node->name); if (node->value) { if (!node->children) printf(" ="); printf(" %s", node->value); } if (node->children) { printf(" %c\n", GROUP_BEGIN); conf_dump(node->children, indent + 2); printf("%*.*s%c", indent, indent, "", GROUP_END); } else { printf("%c", SEMICOLON); } printf("\n"); } }
struct conf * conf_create(char *filename) { rstatus_t status; struct conf *cf; cf = conf_open(filename); if (cf == NULL) { return NULL; } /* validate configuration file before parsing */ status = conf_pre_validate(cf); if (status != NC_OK) { goto error; } /* parse the configuration file */ status = conf_parse(cf); if (status != NC_OK) { goto error; } /* validate parsed configuration */ status = conf_post_validate(cf); if (status != NC_OK) { goto error; } conf_dump(cf); fclose(cf->fh); cf->fh = NULL; return cf; error: fclose(cf->fh); cf->fh = NULL; conf_destroy(cf); return NULL; }
/* gcc conf.c hash.c -DCONF_TEST_MAIN -I../inc */ int main(int argc, char *argv[]) { conf_t conf = {}; if (argc < 2) { fprintf(stderr, "usage: ./a.out <conf_file>\n"); exit(1); } if (conf_init(&conf, argv[1]) != 0) { fprintf(stderr, "conf_init error\n"); exit(1); } conf_dump(&conf); printf("PORT: %d\n", conf_get_int_value(&conf, "porta", 7777)); printf("LOG_NAME: %s\n", conf_get_str_value(&conf, "log_name", "NULL")); conf_free(&conf); exit(0); }
static int print_block_conf(void *key, conf_block_t *cb, void *userptr) { printf("%s {\n", (char *)key); conf_dump(&cb->block); printf("}\n"); return 0; }
int main(int argc, char **argv) { int ch; char conf_path[MAXPATHLEN]; int conf=0; st_cn_t conn; conn = st_connection_alloc(); conn->st_config = conf_create_with_defaults(); (void)strncpy(conf_path, STUNEL_CONFIG, MAXPATHLEN-1); /* Parse command line args */ /* TODO: Add options for port, destination ip, key and user so using conf file is not necessary*/ while ((ch = getopt(argc, argv, "hqvDAf:p:d:i:u:")) != -1) { switch (ch) { case 'A': conf_set_authtype(conn->st_config, STUNEL_AUTH_AGENT); break; case 'f': if (argc == 3) { memset(conf_path, 0, MAXPATHLEN); (void)strncpy(conf_path, optarg, MAXPATHLEN-1); conf_path[MAXPATHLEN - 1]='\0'; conf=1; } break; case 'p': conf_set_port(conn->st_config, atoi(optarg)); break; case 'd': conf_set_address(conn->st_config, optarg); break; case 'i': conf_set_sshkey(conn->st_config, optarg); break; case 'u': conf_set_login(conn->st_config, optarg); break; case 'q': conf_set_log_level(conn->st_config, STUNEL_NORMAL); break; case 'v': conf_set_log_level(conn->st_config, STUNEL_VERBOSE); break; case 'D': conf_set_log_level(conn->st_config, STUNEL_DEBUG); break; case '?': case 'h': default: usage(); } } argc -= optind; argv += optind; if ( conf ) { printf("Reading configuration from file: %s\n", conf_path); conf_destroy(conn->st_config); conn->st_config = conf_get_file(conf_path); } printf("Config file: \n%s\n", conf_dump(conn->st_config)); if (conf_check(conn->st_config)) { st_connection_destroy(conn); exit(1); } printf("User is set to %s, port to %d\n", conf_get_login(conn->st_config), conf_get_port(conn->st_config)); printf("Connecting with libssh\n"); st_ssh_connect(conn); st_connection_destroy(conn); return 0; }