File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / confuse / tests / 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: /* Test environment varialbe substitution
    2:  */
    3: 
    4: #include "config.h"
    5: #include <string.h>
    6: #include <stdlib.h>
    7: #include "check_confuse.h"
    8: 
    9: static int testconfig(const char *buf, const char *parameter)
   10: {
   11: 	cfg_opt_t opts[] = {
   12: 		CFG_STR("parameter", NULL, CFGF_NONE),
   13: 		CFG_END()
   14: 	};
   15: 	char *param;
   16: 	cfg_t *cfg;
   17: 
   18: 	cfg = cfg_init(opts, CFGF_NONE);
   19: 	if (!cfg)
   20: 		return 0;
   21: 
   22: 	if (cfg_parse_buf(cfg, buf) != CFG_SUCCESS)
   23: 		return 0;
   24: 
   25: 	param = cfg_getstr(cfg, "parameter");
   26: 	if (!param)
   27: 		return 0;
   28: 
   29: 	if (strcmp(param, parameter) != 0)
   30: 		return 0;
   31: 
   32: 	cfg_free(cfg);
   33: 	return 1;
   34: }
   35: 
   36: int main(void)
   37: {
   38: #if defined(HAVE_SETENV) && defined(HAVE_UNSETENV)
   39: 	fail_unless(setenv("MYVAR", "testing", 1) == 0);
   40: 	fail_unless(unsetenv("MYUNSETVAR") == 0);
   41: #elif defined(HAVE__PUTENV)
   42: 	fail_unless(_putenv("MYVAR=testing") == 0);
   43: 	fail_unless(_putenv("MYUNSETVAR=") == 0);
   44: #else
   45: #error "Not sure how to set environment variables."
   46: #endif
   47: 
   48: 	/* Check basic string parsing */
   49: 	fail_unless(testconfig("parameter=\"abc\\ndef\"", "abc\ndef"));
   50: 	fail_unless(testconfig("parameter=\"abc\\adef\"", "abc\adef"));
   51: 	fail_unless(testconfig("parameter=\"abc\\040def\"", "abc def"));
   52: 	fail_unless(testconfig("parameter=\"abc\\x20def\"", "abc def"));
   53: 	fail_unless(testconfig("parameter=\"${}\"", ""));
   54: 
   55: 	/* Check unquoted environment variable handling */
   56: 	fail_unless(testconfig("parameter=${MYVAR}", "testing"));
   57: 	fail_unless(testconfig("parameter=${MYVAR:-default}", "testing"));
   58: 	fail_unless(testconfig("parameter=${MYUNSETVAR}", ""));
   59: 	fail_unless(testconfig("parameter=${MYUNSETVAR:-default}", "default"));
   60: 
   61: 	/* Check quoted environment variable handling */
   62: 	fail_unless(testconfig("parameter=\"${MYVAR}\"", "testing"));
   63: 	fail_unless(testconfig("parameter=\"${MYVAR:-default}\"", "testing"));
   64: 	fail_unless(testconfig("parameter=\"${MYUNSETVAR}\"", ""));
   65: 	fail_unless(testconfig("parameter=\"${MYUNSETVAR:-default}\"", "default"));
   66: 
   67: 	/* Check quoted environment variable handling in the middle of strings */
   68: 	fail_unless(testconfig("parameter=\"text_${MYVAR}\"", "text_testing"));
   69: 	fail_unless(testconfig("parameter=\"${MYVAR}_text\"", "testing_text"));
   70: 	fail_unless(testconfig("parameter=\"start_${MYVAR}_end\"", "start_testing_end"));
   71: 
   72: 	/* Check single quoted environment variable handling */
   73: 	fail_unless(testconfig("parameter='${MYVAR}'", "${MYVAR}"));
   74: 
   75: 	return 0;
   76: }
   77: 
   78: /**
   79:  * Local Variables:
   80:  *  indent-tabs-mode: t
   81:  *  c-file-style: "linux"
   82:  * End:
   83:  */

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