reread.c
#include "confuse.h"
#include <string.h>
#include <signal.h>
#include <unistd.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_END()
};
static 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),
CFG_END()
};
int ret;
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);
ret =
cfg_parse_buf(cfg, buf);
ret =
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;
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;
}
00001
#include "confuse.h"
00002
#include <string.h>
00003
#include <signal.h>
00004
#include <unistd.h>
00005
00006
cfg_t *cfg = 0;
00007
const char *config_filename =
"./reread.conf";
00008
00009
void read_config(
void)
00010 {
00011
static cfg_opt_t arg_opts[] = {
00012
CFG_STR(
"value",
"default", CFGF_NONE),
00013
CFG_END()
00014 };
00015
static cfg_opt_t opts[] = {
00016
CFG_INT(
"delay", 3, CFGF_NONE),
00017
CFG_STR(
"message",
"This is a message", CFGF_NONE),
00018
CFG_SEC(
"argument", arg_opts, CFGF_MULTI | CFGF_TITLE),
00019
CFG_END()
00020 };
00021
int ret;
00022
00023
char *buf =
"" \
00024
" delay = 3\n" \
00025
"# message = \"asdfasfasfd tersf\"\n" \
00026
" argument one { value = 1 }\n" \
00027
" argument two { value=foo}\n";
00028
00029 cfg_free(cfg);
00030
00031 cfg = cfg_init(opts, 0);
00032 ret = cfg_parse_buf(cfg, buf);
00033 ret = cfg_parse(cfg, config_filename);
00034 }
00035
00036
void sighandler(
int sig)
00037 {
00038 read_config();
00039 signal(SIGHUP, sighandler);
00040 }
00041
00042
static int loop = 1;
00043
00044
void usr1handler(
int sig)
00045 {
00046 loop = 0;
00047 }
00048
00049
int main(
void)
00050 {
00051
unsigned int i;
00052
00053 read_config();
00054 signal(SIGHUP, sighandler);
00055 signal(SIGUSR1, usr1handler);
00056
00057
while(loop) {
00058 printf(
"Message: %s", cfg_getstr(cfg,
"message"));
00059
for(i = 0; i < cfg_size(cfg,
"argument"); i++) {
00060
cfg_t *arg = cfg_getnsec(cfg,
"argument", i);
00061 printf(
", %s", cfg_getstr(arg,
"value"));
00062 }
00063 printf(
"\n");
00064
00065 sleep(cfg_getint(cfg,
"delay"));
00066 }
00067
00068 cfg_free(cfg);
00069 cfg = 0;
00070
00071
return 0;
00072 }