#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <locale.h>
#define ACB_YES 1
#define ACB_NO 2
#define ACB_ASK 3
int conf_alias(
cfg_t *cfg,
cfg_opt_t *opt,
int argc,
const char **argv)
{
if (argc < 2) {
return -1;
}
printf("got alias '%s' = '%s'\n", argv[0], argv[1]);
return 0;
}
int conf_parse_acb(
cfg_t *cfg,
cfg_opt_t *opt,
const char *value,
void *result)
{
if (strcmp(value, "yes") == 0)
*(int *)result = ACB_YES;
else if (strcmp(value, "no") == 0)
*(int *)result = ACB_NO;
else if (strcmp(value, "ask") == 0)
*(int *)result = ACB_ASK;
else {
return -1;
}
return 0;
}
{
if (value <= 0) {
return -1;
}
return 0;
}
{
cfg_error(cfg,
"missing required option 'host' in bookmark");
return -1;
}
return 0;
}
cfg_t *parse_conf(
const char *filename)
{
};
};
case CFG_FILE_ERROR:
printf("warning: configuration file '%s' could not be read: %s\n", filename, strerror(errno));
printf("continuing with default values...\n\n");
break;
case CFG_PARSE_ERROR:
return 0;
}
return cfg;
}
int main(int argc, char **argv)
{
#ifdef LC_MESSAGES
setlocale(LC_MESSAGES, "");
setlocale(LC_CTYPE, "");
#endif
cfg = parse_conf(argc > 1 ? argv[1] : "ftp.conf");
if (cfg) {
unsigned int i;
printf(
"passive-mode = %s\n",
cfg_getbool(cfg,
"passive-mode") ?
"true" :
"false");
printf(
"remote-completion = %s\n",
cfg_getbool(cfg,
"remote-completion") ?
"true" :
"false");
printf(
"number of bookmarks: %d\n",
cfg_size(cfg,
"bookmark"));
for (i = 0; i <
cfg_size(cfg,
"bookmark"); i++) {
printf(" bookmark #%d: %s:%s@%s:%ld%s\n", i + 1,
}
for (i = 0; i <
cfg_size(cfg,
"xterm-terminals"); i++) {
printf(
"xterm-terminal #%d: %s\n", i + 1,
cfg_getnstr(cfg,
"xterm-terminals", i));
}
printf(
"auto-create-bookmark = %ld\n",
cfg_getint(cfg,
"auto-create-bookmark"));
}
return 0;
}
DLLIMPORT cfg_t *__export cfg_init(cfg_opt_t *opts, cfg_flag_t flags)
Create and initialize a cfg_t structure.
Definition: confuse.c:1816
#define CFGF_NODEFAULT
option has no default value
Definition: confuse.h:91
DLLIMPORT int __export cfg_include(cfg_t *cfg, cfg_opt_t *opt, int argc, const char **argv)
Predefined include-function.
Definition: confuse.c:1997
#define CFG_INT(name, def, flags)
Initialize an integer option.
Definition: confuse.h:421
DLLIMPORT unsigned int __export cfg_size(cfg_t *cfg, const char *name)
Return the number of values this option has.
Definition: confuse.c:406
#define CFG_END()
Terminate list of options.
Definition: confuse.h:574
#define CFG_STR(name, def, flags)
Initialize a string option.
Definition: confuse.h:340
A configuration file parser library.
DLLIMPORT unsigned int __export cfg_opt_size(cfg_opt_t *opt)
Return the number of values this option has.
Definition: confuse.c:399
#define CFGF_TITLE
option has a title (only applies to sections)
Definition: confuse.h:90
#define CFG_BOOL(name, def, flags)
Initialize a boolean option.
Definition: confuse.h:490
DLLIMPORT signed long __export cfg_opt_getnint(cfg_opt_t *opt, unsigned int index)
Returns the value of an integer option, given a cfg_opt_t pointer.
Definition: confuse.c:424
#define CFG_STR_LIST(name, def, flags)
Initialize a string list option.
Definition: confuse.h:345
DLLIMPORT cfg_t *__export cfg_opt_getnsec(cfg_opt_t *opt, unsigned int index)
Returns the value of a section option, given a cfg_opt_t pointer.
Definition: confuse.c:549
DLLIMPORT cfg_validate_callback_t __export cfg_set_validate_func(cfg_t *cfg, const char *name, cfg_validate_callback_t vf)
Register a validating callback function for an option.
Definition: confuse.c:2637
DLLIMPORT cfg_t *__export cfg_getnsec(cfg_t *cfg, const char *name, unsigned int index)
Indexed version of cfg_getsec(), used for sections with the CFGF_MULTI flag set.
Definition: confuse.c:563
DLLIMPORT char *__export cfg_getnstr(cfg_t *cfg, const char *name, unsigned int index)
Indexed version of cfg_getstr(), used for lists.
Definition: confuse.c:514
#define CFG_INT_CB(name, def, flags, cb)
Initialize an integer option with a value parsing callback.
Definition: confuse.h:431
DLLIMPORT int __export cfg_parse(cfg_t *cfg, const char *filename)
Parse a configuration file.
Definition: confuse.c:1746
DLLIMPORT cfg_bool_t __export cfg_getbool(cfg_t *cfg, const char *name)
Returns the value of a boolean option.
Definition: confuse.c:494
Data structure holding information about an option.
Definition: confuse.h:309
#define CFG_FUNC(name, func)
Initialize a function.
Definition: confuse.h:538
#define CFG_SEC(name, opts, flags)
Initialize a section.
Definition: confuse.h:527
const DLLIMPORT char *__export cfg_opt_name(cfg_opt_t *opt)
Return the name of an option.
Definition: confuse.c:387
const DLLIMPORT char *__export cfg_name(cfg_t *cfg)
Return the name of a section.
Definition: confuse.c:380
#define CFGF_NONE
Flags.
Definition: confuse.h:86
#define CFGF_MULTI
option may be specified multiple times (only applies to sections)
Definition: confuse.h:87
Data structure holding information about a "section".
Definition: confuse.h:252
DLLIMPORT long int __export cfg_getint(cfg_t *cfg, const char *name)
Returns the value of an integer option.
Definition: confuse.c:444
DLLIMPORT int __export cfg_free(cfg_t *cfg)
Free a cfg_t context.
Definition: confuse.c:1962
#define CFG_SUCCESS
Return codes from cfg_parse(), cfg_parse_boolean(), and cfg_set*() functions.
Definition: confuse.h:105
DLLIMPORT char *__export cfg_getstr(cfg_t *cfg, const char *name)
Returns the value of a string option.
Definition: confuse.c:519
DLLIMPORT void __export cfg_error(cfg_t *cfg, const char *fmt,...)
Show a parser error.
Definition: confuse.c:1211