Annotation of embedaddon/confuse/examples/reread.c, revision 1.1.1.2
1.1.1.2 ! misho 1: #include <err.h>
1.1 misho 2: #include <string.h>
3: #include <signal.h>
4: #include <unistd.h>
1.1.1.2 ! misho 5: #include <locale.h>
! 6: #include "confuse.h"
1.1 misho 7:
8: cfg_t *cfg = 0;
9: const char *config_filename = "./reread.conf";
10:
11: void read_config(void)
12: {
1.1.1.2 ! misho 13: static cfg_opt_t arg_opts[] = {
! 14: CFG_STR("value", "default", CFGF_NONE),
! 15: CFG_END()
! 16: };
! 17: cfg_opt_t opts[] = {
! 18: CFG_INT("delay", 3, CFGF_NONE),
! 19: CFG_STR("message", "This is a message", CFGF_NONE),
! 20: CFG_SEC("argument", arg_opts, CFGF_MULTI | CFGF_TITLE),
! 21: CFG_END()
! 22: };
! 23:
! 24: char *buf = ""
! 25: " delay = 3\n" "# message = \"asdfasfasfd tersf\"\n" " argument one { value = 1 }\n" " argument two { value=foo}\n";
! 26:
! 27: cfg_free(cfg);
! 28:
! 29: cfg = cfg_init(opts, 0);
! 30: if (cfg_parse_buf(cfg, buf) != CFG_SUCCESS)
! 31: errx(1, "Failed parsing configuration!\n");
! 32:
! 33: cfg_parse(cfg, config_filename);
1.1 misho 34: }
35:
36: void sighandler(int sig)
37: {
1.1.1.2 ! misho 38: read_config();
! 39: signal(SIGHUP, sighandler);
1.1 misho 40: }
41:
42: static int loop = 1;
43:
44: void usr1handler(int sig)
45: {
1.1.1.2 ! misho 46: loop = 0;
1.1 misho 47: }
48:
49: int main(void)
50: {
1.1.1.2 ! misho 51: unsigned int i;
1.1 misho 52:
1.1.1.2 ! misho 53: /* Localize messages & types according to environment, since v2.9 */
! 54: #ifdef LC_MESSAGES
! 55: setlocale(LC_MESSAGES, "");
! 56: setlocale(LC_CTYPE, "");
! 57: #endif
! 58:
! 59: read_config();
! 60: signal(SIGHUP, sighandler);
! 61: signal(SIGUSR1, usr1handler);
! 62:
! 63: while (loop) {
! 64: printf("Message: %s", cfg_getstr(cfg, "message"));
! 65: for (i = 0; i < cfg_size(cfg, "argument"); i++) {
! 66: cfg_t *arg = cfg_getnsec(cfg, "argument", i);
! 67:
! 68: printf(", %s", cfg_getstr(arg, "value"));
! 69: }
! 70: printf("\n");
1.1 misho 71:
1.1.1.2 ! misho 72: sleep(cfg_getint(cfg, "delay"));
! 73: }
1.1 misho 74:
1.1.1.2 ! misho 75: cfg_free(cfg);
! 76: cfg = 0;
1.1 misho 77:
1.1.1.2 ! misho 78: return 0;
1.1 misho 79: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>