--- embedaddon/confuse/doc/html/reread_8c-example.html 2017/01/24 14:48:55 1.1.1.1 +++ embedaddon/confuse/doc/html/reread_8c-example.html 2021/03/17 00:49:17 1.1.1.2 @@ -1,104 +1,174 @@ - - + + - - confuse: reread.c - + + + +confuse: reread.c + + + + + + + +
+
+ + + + + + +
+
confuse +  3.3 +
+
+
+ + + + + + + +
+ +
+
-
- - - - - +
+
+
reread.c
+
+
+
#include <err.h>
+
#include <string.h>
+
#include <signal.h>
+
#include <unistd.h>
+
#include <locale.h>
+
#include "confuse.h"
+
+
cfg_t *cfg = 0;
+
const char *config_filename = "./reread.conf";
+
+
void read_config(void)
+
{
+
static cfg_opt_t arg_opts[] = {
+
CFG_STR("value", "default", CFGF_NONE),
+ +
};
+
cfg_opt_t opts[] = {
+
CFG_INT("delay", 3, CFGF_NONE),
+
CFG_STR("message", "This is a message", CFGF_NONE),
+
CFG_SEC("argument", arg_opts, CFGF_MULTI | CFGF_TITLE),
+ +
};
+
+
char *buf = ""
+
" delay = 3\n" "# message = \"asdfasfasfd tersf\"\n" " argument one { value = 1 }\n" " argument two { value=foo}\n";
+
+
cfg_free(cfg);
+
+
cfg = cfg_init(opts, 0);
+
if (cfg_parse_buf(cfg, buf) != CFG_SUCCESS)
+
errx(1, "Failed parsing configuration!\n");
+
+
cfg_parse(cfg, config_filename);
+
}
+
+
void sighandler(int sig)
+
{
+
read_config();
+
signal(SIGHUP, sighandler);
+
}
+
+
static int loop = 1;
+
+
void usr1handler(int sig)
+
{
+
loop = 0;
+
}
+
+
int main(void)
+
{
+
unsigned int i;
+
+
/* Localize messages & types according to environment, since v2.9 */
+
#ifdef LC_MESSAGES
+
setlocale(LC_MESSAGES, "");
+
setlocale(LC_CTYPE, "");
+
#endif
+
+
read_config();
+
signal(SIGHUP, sighandler);
+
signal(SIGUSR1, usr1handler);
+
+
while (loop) {
+
printf("Message: %s", cfg_getstr(cfg, "message"));
+
for (i = 0; i < cfg_size(cfg, "argument"); i++) {
+
cfg_t *arg = cfg_getnsec(cfg, "argument", i);
+
+
printf(", %s", cfg_getstr(arg, "value"));
+
}
+
printf("\n");
+
+
sleep(cfg_getint(cfg, "delay"));
+
}
+
+
cfg_free(cfg);
+
cfg = 0;
+
+
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 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.
+
#define CFGF_TITLE
option has a title (only applies to sections)
Definition: confuse.h:90
+
DLLIMPORT int __export cfg_parse_buf(cfg_t *cfg, const char *buf)
Same as cfg_parse() above, but takes a character buffer as argument.
Definition: confuse.c:1777
+
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 int __export cfg_parse(cfg_t *cfg, const char *filename)
Parse a configuration file.
Definition: confuse.c:1746
+
Data structure holding information about an option.
Definition: confuse.h:309
+
#define CFG_SEC(name, opts, flags)
Initialize a section.
Definition: confuse.h:527
+
#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
+ + +