File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / confuse / examples / reread.c
Revision 1.1.1.2 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Wed Mar 17 00:49:17 2021 UTC (3 years, 3 months ago) by misho
Branches: confuse, MAIN
CVS tags: v3_3, HEAD
confuse 3.3

    1: #include <err.h>
    2: #include <string.h>
    3: #include <signal.h>
    4: #include <unistd.h>
    5: #include <locale.h>
    6: #include "confuse.h"
    7: 
    8: cfg_t *cfg = 0;
    9: const char *config_filename = "./reread.conf";
   10: 
   11: void read_config(void)
   12: {
   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);
   34: }
   35: 
   36: void sighandler(int sig)
   37: {
   38: 	read_config();
   39: 	signal(SIGHUP, sighandler);
   40: }
   41: 
   42: static int loop = 1;
   43: 
   44: void usr1handler(int sig)
   45: {
   46: 	loop = 0;
   47: }
   48: 
   49: int main(void)
   50: {
   51: 	unsigned int i;
   52: 
   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");
   71: 
   72: 		sleep(cfg_getint(cfg, "delay"));
   73: 	}
   74: 
   75: 	cfg_free(cfg);
   76: 	cfg = 0;
   77: 
   78: 	return 0;
   79: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>