Example #1
0
int main(void)
{
	cfg_opt_t sub_opts[] = {
		CFG_BOOL("bool", cfg_false, CFGF_NONE),
		CFG_STR("string", NULL, CFGF_NONE),
		CFG_INT("int", 0, CFGF_NONE),
		CFG_FLOAT("float", 0.0, CFGF_NONE),
		CFG_END()
	};

	cfg_opt_t opts[] = {
		CFG_BOOL_LIST("bool", cfg_false, CFGF_NONE),
		CFG_STR_LIST("string", NULL, CFGF_NONE),
		CFG_INT_LIST("int", 0, CFGF_NONE),
		CFG_FLOAT_LIST("float", "0.0", CFGF_NONE),
		CFG_SEC("sub", sub_opts,
			CFGF_MULTI | CFGF_TITLE | CFGF_NO_TITLE_DUPES),
		CFG_END()
	};

	char *cmd = NULL;
	const char *reply;
	int res;
	int i;

	cfg = cfg_init(opts, CFGF_NONE);

	for (;;) {
		printf("cli> ");
		fflush(stdout);

		if (cmd)
			free(cmd);
		cmd = input_cmd();
		if (!cmd)
			exit(0);
		res = split_cmd(cmd);
		if (res < 0) {
			printf("Parse error\n");
			continue;
		}
		if (cmdc == 0)
			continue;
		for (i = 0; cmds[i].cmd; ++i) {
			if (strcmp(cmdv[0], cmds[i].cmd))
				continue;
			reply = cmds[i].handler(cmdc, cmdv);
			if (!reply)
				exit(0);
			printf("%s", reply);
			break;
		}
		if (!cmds[i].cmd)
			printf("Unknown command\n");
	}

	cfg_free(cfg);
	return 0;
}
Example #2
0
File: conf.c Project: noushi/bmon
	CFG_FLOAT("interval", 1.0f, CFGF_NONE),
	CFG_INT("size", 60, CFGF_NONE),
	CFG_STR("type", "64bit", CFGF_NONE),
	CFG_END()
};

static cfg_opt_t attr_opts[] = {
	CFG_STR("description", "", CFGF_NONE),
	CFG_STR("unit", "", CFGF_NONE),
	CFG_STR("type", "counter", CFGF_NONE),
	CFG_BOOL("history", cfg_false, CFGF_NONE),
	CFG_END()
};

static cfg_opt_t variant_opts[] = {
	CFG_FLOAT_LIST("div", "{}", CFGF_NONE),
	CFG_STR_LIST("txt", "", CFGF_NONE),
	CFG_END()
};

static cfg_opt_t unit_opts[] = {
	CFG_SEC("variant", variant_opts, CFGF_MULTI | CFGF_TITLE),
	CFG_END()
};

static cfg_opt_t global_opts[] = {
	CFG_FLOAT("read_interval", 1.0f, CFGF_NONE),
	CFG_FLOAT("rate_interval", 1.0f, CFGF_NONE),
	CFG_FLOAT("lifetime", 30.0f, CFGF_NONE),
	CFG_FLOAT("history_variance", 0.1f, CFGF_NONE),
	CFG_FLOAT("variance", 0.1f, CFGF_NONE),
Example #3
0
int main(int argc, char **argv)
{
    unsigned int i;
    cfg_t *cfg;
    unsigned n;
    int ret;
    cfg_opt_t proxy_opts[] = {
        CFG_INT("type", 0, CFGF_NONE),
        CFG_STR("host", 0, CFGF_NONE),
        CFG_STR_LIST("exclude", "{localhost, .localnet}", CFGF_NONE),
        CFG_INT("port", 21, CFGF_NONE),
        CFG_END()
    };
    cfg_opt_t bookmark_opts[] = {
        CFG_STR("machine", 0, CFGF_NONE),
        CFG_INT("port", 21, CFGF_NONE),
        CFG_STR("login", 0, CFGF_NONE),
        CFG_STR("password", 0, CFGF_NONE),
        CFG_STR("directory", 0, CFGF_NONE),
        CFG_BOOL("passive-mode", cfg_false, CFGF_NONE),
        CFG_SEC("proxy", proxy_opts, CFGF_NONE),
        CFG_END()
    };
    cfg_opt_t opts[] = {
        CFG_INT("backlog", 42, CFGF_NONE),
        CFG_STR("probe-device", "eth2", CFGF_NONE),
        CFG_SEC("bookmark", bookmark_opts, CFGF_MULTI | CFGF_TITLE),
        CFG_FLOAT_LIST("delays", "{3.567e2, 0.2, -47.11}", CFGF_NONE),
        CFG_FUNC("func", &cb_func),
        CFG_INT_CB("ask-quit", 3, CFGF_NONE, &cb_verify_ask),
        CFG_INT_LIST_CB("ask-quit-array", "{maybe, yes, no}",
                        CFGF_NONE, &cb_verify_ask),
        CFG_FUNC("include", &cfg_include),
        CFG_END()
    };

#ifndef _WIN32
    /* for some reason, MS Visual C++ chokes on this (?) */
    printf("Using %s\n\n", confuse_copyright);
#endif

    cfg = cfg_init(opts, CFGF_NOCASE);

    /* set a validating callback function for bookmark sections */
    cfg_set_validate_func(cfg, "bookmark", &cb_validate_bookmark);

    ret = cfg_parse(cfg, argc > 1 ? argv[1] : "test.conf");
    printf("ret == %d\n", ret);
    if(ret == CFG_FILE_ERROR) {
        perror("test.conf");
        return 1;
    } else if(ret == CFG_PARSE_ERROR) {
        fprintf(stderr, "parse error\n");
        return 2;
    }

    printf("backlog == %ld\n", cfg_getint(cfg, "backlog"));

    printf("probe device is %s\n", cfg_getstr(cfg, "probe-device"));
    cfg_setstr(cfg, "probe-device", "lo");
    printf("probe device is %s\n", cfg_getstr(cfg, "probe-device"));

    n = cfg_size(cfg, "bookmark");
    printf("%d configured bookmarks:\n", n);
    for(i = 0; i < n; i++) {
        cfg_t *pxy;
        cfg_t *bm = cfg_getnsec(cfg, "bookmark", i);
        printf("  bookmark #%u (%s):\n", i+1, cfg_title(bm));
        printf("    machine = %s\n", cfg_getstr(bm, "machine"));
        printf("    port = %d\n", (int)cfg_getint(bm, "port"));
        printf("    login = %s\n", cfg_getstr(bm, "login"));
        printf("    passive-mode = %s\n",
               cfg_getbool(bm, "passive-mode") ? "true" : "false");
        printf("    directory = %s\n", cfg_getstr(bm, "directory"));
        printf("    password = %s\n", cfg_getstr(bm, "password"));

        pxy = cfg_getsec(bm, "proxy");
        if(pxy) {
            int j, m;
            if(cfg_getstr(pxy, "host") == 0) {
                printf("      no proxy host is set, setting it to 'localhost'...\n");
                /* For sections without CFGF_MULTI flag set, there is
                 * also an extended syntax to get an option in a
                 * subsection:
                 */
                cfg_setstr(bm, "proxy|host", "localhost");
            }
            printf("      proxy host is %s\n", cfg_getstr(pxy, "host"));
            printf("      proxy type is %ld\n", cfg_getint(pxy, "type"));
            printf("      proxy port is %ld\n", cfg_getint(pxy, "port"));

            m = cfg_size(pxy, "exclude");
            printf("      got %d hosts to exclude from proxying:\n", m);
            for(j = 0; j < m; j++) {
                printf("        exclude %s\n", cfg_getnstr(pxy, "exclude", j));
            }
        } else
            printf("    no proxy settings configured\n");
    }

    printf("delays are (%d):\n", cfg_size(cfg, "delays"));
    for(i = 0; i < cfg_size(cfg, "delays"); i++)
        printf(" %G\n", cfg_getnfloat(cfg, "delays", i));

    printf("ask-quit == %ld\n", cfg_getint(cfg, "ask-quit"));

    /* Using cfg_setint(), the integer value for the option ask-quit
     * is not verified by the value parsing callback.
     *
     *
     cfg_setint(cfg, "ask-quit", 4);
     printf("ask-quit == %ld\n", cfg_getint(cfg, "ask-quit"));
    */

    /* The following commented line will generate a failed assertion
     * and abort, since the option "foo" is not declared
     *
     *
     printf("foo == %ld\n", cfg_getint(cfg, "foo"));
    */

    cfg_addlist(cfg, "ask-quit-array", 2, 1, 2);

    for(i = 0; i < cfg_size(cfg, "ask-quit-array"); i++)
        printf("ask-quit-array[%d] == %ld\n",
               i, cfg_getnint(cfg, "ask-quit-array", i));

    /* print the parsed values to another file */
    {
        FILE *fp = fopen("test.conf.out", "w");
        cfg_set_print_func(cfg, "func", print_func);
        cfg_set_print_func(cfg, "ask-quit", print_ask);
        cfg_set_print_func(cfg, "ask-quit-array", print_ask);
        cfg_print(cfg, fp);
        fclose(fp);
    }

    cfg_free(cfg);
    return 0;
}