File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / confuse / examples / env.c
Revision 1.1.1.1 (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: /*
    2:  * Example of how an application can allow free-form key=value strings as settings,
    3:  * e.g., custom environment variables the program should set for child processes.
    4:  *
    5:  * env.conf:
    6:  *     env {
    7:  *          foo = bar
    8:  *          baz = foo
    9:  *     }
   10:  */
   11: #include <err.h>
   12: #include <confuse.h>
   13: #include <stdio.h>
   14: 
   15: int main(int argc, char *argv[])
   16: {
   17: 	const char *file = "env.conf";
   18: 	cfg_opt_t opts[] = {
   19: 		CFG_SEC("env", NULL, CFGF_KEYSTRVAL),
   20: 		CFG_END()
   21: 	};
   22: 	cfg_t *cfg, *sec;
   23: 	int rc;
   24: 
   25: 	if (argc > 1)
   26: 		file = argv[1];
   27: 
   28: 	cfg = cfg_init(opts, 0);
   29: 	if (!cfg)
   30: 		err(1, "Failed cfg_init()");
   31: 
   32: 	rc = cfg_parse(cfg, file);
   33: 	if (rc != CFG_SUCCESS) {
   34: 		if (rc == CFG_FILE_ERROR)
   35: 			err(1, "Failed opening %s", file);
   36: 
   37: 		errx(1, "Failed parsing %s", file);
   38: 	}
   39: 
   40: 	sec = cfg_getsec(cfg, "env");
   41: 	if (sec) {
   42: 		unsigned int i;
   43: 
   44: 		for (i = 0; i < cfg_num(sec); i++) {
   45: 			cfg_opt_t *opt = cfg_getnopt(sec, i);
   46: 
   47: 			printf("%s = \"%s\"\n", cfg_opt_name(opt), cfg_opt_getstr(opt));
   48: 		}
   49: 	}
   50: 
   51: //	cfg_print(cfg, stdout);
   52: 	cfg_free(cfg);
   53: 
   54: 	return 0;
   55: }

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