version 1.1, 2017/01/24 14:48:56
|
version 1.1.1.2, 2021/03/17 00:49:17
|
Line 1
|
Line 1
|
#include "confuse.h" | #include <err.h> |
#include <string.h> |
#include <string.h> |
#include <signal.h> |
#include <signal.h> |
#include <unistd.h> |
#include <unistd.h> |
|
#include <locale.h> |
|
#include "confuse.h" |
|
|
cfg_t *cfg = 0; |
cfg_t *cfg = 0; |
const char *config_filename = "./reread.conf"; |
const char *config_filename = "./reread.conf"; |
|
|
void read_config(void) |
void read_config(void) |
{ |
{ |
cfg_opt_t arg_opts[] = { | static cfg_opt_t arg_opts[] = { |
CFG_STR("value", "default", CFGF_NONE), | CFG_STR("value", "default", CFGF_NONE), |
CFG_END() | CFG_END() |
}; | }; |
cfg_opt_t opts[] = { | cfg_opt_t opts[] = { |
CFG_INT("delay", 3, CFGF_NONE), | CFG_INT("delay", 3, CFGF_NONE), |
CFG_STR("message", "This is a message", CFGF_NONE), | CFG_STR("message", "This is a message", CFGF_NONE), |
CFG_SEC("argument", arg_opts, CFGF_MULTI | CFGF_TITLE), | CFG_SEC("argument", arg_opts, CFGF_MULTI | CFGF_TITLE), |
CFG_END() | CFG_END() |
}; | }; |
int ret; | |
|
|
char *buf = "" \ | char *buf = "" |
" delay = 3\n" \ | " delay = 3\n" "# message = \"asdfasfasfd tersf\"\n" " argument one { value = 1 }\n" " argument two { value=foo}\n"; |
"# message = \"asdfasfasfd tersf\"\n" \ | |
" argument one { value = 1 }\n" \ | |
" argument two { value=foo}\n"; | |
|
|
cfg_free(cfg); | cfg_free(cfg); |
|
|
cfg = cfg_init(opts, 0); | cfg = cfg_init(opts, 0); |
ret = cfg_parse_buf(cfg, buf); | if (cfg_parse_buf(cfg, buf) != CFG_SUCCESS) |
ret = cfg_parse(cfg, config_filename); | errx(1, "Failed parsing configuration!\n"); |
| |
| cfg_parse(cfg, config_filename); |
} |
} |
|
|
void sighandler(int sig) |
void sighandler(int sig) |
{ |
{ |
read_config(); | read_config(); |
signal(SIGHUP, sighandler); | signal(SIGHUP, sighandler); |
} |
} |
|
|
static int loop = 1; |
static int loop = 1; |
|
|
void usr1handler(int sig) |
void usr1handler(int sig) |
{ |
{ |
loop = 0; | loop = 0; |
} |
} |
|
|
int main(void) |
int main(void) |
{ |
{ |
unsigned int i; | unsigned int i; |
|
|
read_config(); | /* Localize messages & types according to environment, since v2.9 */ |
signal(SIGHUP, sighandler); | #ifdef LC_MESSAGES |
signal(SIGUSR1, usr1handler); | setlocale(LC_MESSAGES, ""); |
| setlocale(LC_CTYPE, ""); |
| #endif |
|
|
while(loop) | read_config(); |
{ | signal(SIGHUP, sighandler); |
printf("Message: %s", cfg_getstr(cfg, "message")); | signal(SIGUSR1, usr1handler); |
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")); | 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); |
|
|
cfg_free(cfg); | printf(", %s", cfg_getstr(arg, "value")); |
cfg = 0; | } |
| printf("\n"); |
|
|
return 0; | sleep(cfg_getint(cfg, "delay")); |
| } |
| |
| cfg_free(cfg); |
| cfg = 0; |
| |
| return 0; |
} |
} |