Browse code

core: new preprocessor directive $!defenv ID=ENVVAR

- define an ID to the value of an environment variable with the name ENVVAR
- it can also be just $!defenv ENVVAR and the ID is set to ENVVAR name
- example:
#!defenv SHELL
* if environment variable $SHELL is '/bin/bash', then it is like
#!define SHELL /bin/bash
* variant:
#!defenv ENVSHELL=SHELL
* then it is like
#!define ENVSHELL /bin/bash
- it is a simplified alternative of using #!substdef with $env(NAME) in
replacement part

Daniel-Constantin Mierla authored on 19/11/2020 15:16:35
Showing 2 changed files
... ...
@@ -125,7 +125,7 @@
125 125
 
126 126
 /* start conditions */
127 127
 %x STRING1 STRING2 STR_BETWEEN COMMENT COMMENT_LN ATTR SELECT AVP_PVAR PVAR_P
128
-%x PVARID INCLF IMPTF EVRTNAME CFGPRINTMODE CFGPRINTLOADMOD
128
+%x PVARID INCLF IMPTF EVRTNAME CFGPRINTMODE CFGPRINTLOADMOD DEFENV_ID
129 129
 %x LINECOMMENT DEFINE_ID DEFINE_EOL DEFINE_DATA IFDEF_ID IFDEF_EOL IFDEF_SKIP
130 130
 
131 131
 /* config script types : #!SER  or #!KAMAILIO or #!MAX_COMPAT */
... ...
@@ -551,6 +551,7 @@ IFNDEF       ifndef
551 551
 ENDIF        endif
552 552
 TRYDEF       "trydefine"|"trydef"
553 553
 REDEF        "redefine"|"redef"
554
+DEFENV       defenv
554 555
 
555 556
 /* else is already defined */
556 557
 
... ...
@@ -1413,6 +1414,27 @@ IMPORTFILE      "import_file"
1413 1414
 				ksr_cfg_print_initial_state();
1414 1415
 }
1415 1416
 
1417
+<INITIAL,CFGPRINTMODE>{PREP_START}{DEFENV}  { count();
1418
+			ksr_cfg_print_part(yytext);
1419
+			state = DEFINE_S;
1420
+			BEGIN(DEFENV_ID);
1421
+}
1422
+
1423
+<DEFENV_ID>[ \t]*      { /* eat the whitespace */
1424
+				count();
1425
+				ksr_cfg_print_part(yytext);
1426
+			}
1427
+<DEFENV_ID>[^ \t\r\n]+   { /* get the define id of environment variable */
1428
+				count();
1429
+				ksr_cfg_print_part(yytext);
1430
+				if(pp_define_env(yytext, yyleng) < 0) {
1431
+					LM_CRIT("error at %s line %d\n", (finame)?finame:"cfg", line);
1432
+					exit(-1);
1433
+				}
1434
+				state = INITIAL;
1435
+				ksr_cfg_print_initial_state();
1436
+}
1437
+
1416 1438
 <CFGPRINTMODE>{LOADMODULE}	{ count(); printf("%s", yytext);
1417 1439
 				BEGIN(CFGPRINTLOADMOD);
1418 1440
 			}
... ...
@@ -2000,6 +2022,47 @@ int pp_define_set(int len, char *text)
2000 2022
 	return 0;
2001 2023
 }
2002 2024
 
2025
+int pp_define_env(const char * text, int len)
2026
+{
2027
+	char *r;
2028
+	str defname;
2029
+	str defvalue;
2030
+
2031
+	r = strchr(text, '=');
2032
+
2033
+	defname.s = (char*)text;
2034
+	if(r == NULL) {
2035
+		defname.len = len;
2036
+		r = (char*)text;
2037
+	} else {
2038
+		defname.len = r - text;
2039
+		r++;
2040
+		if(strlen(r) == 0) {
2041
+			LM_ERR("invalid defenv id [%s]\n", (char*)text);
2042
+			return -1;
2043
+		}
2044
+	}
2045
+	defvalue.s = getenv(r);
2046
+
2047
+	if(defvalue.s == NULL) {
2048
+		LM_ERR("env variable not defined [%s]\n", (char*)text);
2049
+		return -1;
2050
+	}
2051
+	defvalue.len = strlen(defvalue.s);
2052
+
2053
+	pp_define_set_type(0);
2054
+	if(pp_define(defname.len, defname.s)<0) {
2055
+		LM_ERR("cannot set define name [%s]\n", (char*)text);
2056
+		return -1;
2057
+	}
2058
+	if(pp_define_set(defvalue.len, defvalue.s)<0) {
2059
+		LM_ERR("cannot set define value [%s]\n", (char*)text);
2060
+		return -1;
2061
+	}
2062
+
2063
+	return 0;
2064
+}
2065
+
2003 2066
 str *pp_define_get(int len, const char * text)
2004 2067
 {
2005 2068
 	str var = {(char *)text, len};
... ...
@@ -42,6 +42,7 @@ int  pp_define(int len, const char *text);
42 42
 int  pp_define_set(int len, char *text);
43 43
 int  pp_define_set_type(int type);
44 44
 str *pp_define_get(int len, const char * text);
45
+int  pp_define_env(const char * text, int len);
45 46
 
46 47
 void pp_ifdef_level_update(int val);
47 48
 int pp_ifdef_level_check(void);