#include "confuse.h"
#include <string.h>
int cb_func(cfg_t *cfg, cfg_opt_t *opt, int argc, const char **argv)
{
int i;
if(argc == 0) {
cfg_error(cfg, "Too few parameters for the '%s' function",
opt->name);
return -1;
}
printf("cb_func() called with %d parameters:\n", argc);
for(i = 0; i < argc; i++)
printf("parameter %d: '%s'\n", i, argv[i]);
return 0;
}
int cb_verify_ask(cfg_t *cfg, cfg_opt_t *opt, const char *value, void *result)
{
if(strcmp(value, "yes") == 0)
*(long int *)result = 1;
else if(strcmp(value, "no") == 0)
*(long int *)result = 2;
else if(strcmp(value, "maybe") == 0)
*(long int *)result = 3;
else {
cfg_error(cfg, "Invalid value for option %s: %s", opt->name, value);
return -1;
}
return 0;
}
int main(void)
{
unsigned int i;
cfg_t *cfg;
unsigned n;
int ret;
static 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", 0, CFGF_NONE),
CFG_END()
};
static 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),
{"func",CFGT_FUNC,0,0,CFGF_NONE,0,{0,0,cfg_false,0,"func(default, value) func(second,default)"},&cb_func,0,0},
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_END()
};
#ifndef _WIN32
printf("Using %s\n\n", confuse_copyright);
#endif
cfg = cfg_init(opts, CFGF_NOCASE);
ret = cfg_parse(cfg, "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 *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"));
if(cfg_size(bm, "proxy")) {
int j, m;
cfg_t *pxy = cfg_getsec(bm, "proxy");
if(cfg_getstr(pxy, "host") == 0) {
printf("no proxy host is set, setting it to 'localhost'...\n");
cfg_setstr(pxy, "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"));
cfg_setint(cfg, "ask-quit", 4);
printf("ask-quit == %ld\n", cfg_getint(cfg, "ask-quit"));
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));
cfg_free(cfg);
return 0;
}