Module: kamailio
Branch: master
Commit: b40a2a42a3f4f3a21f2d0a2f9ec3c18668a05b4d
URL: 
https://github.com/kamailio/kamailio/commit/b40a2a42a3f4f3a21f2d0a2f9ec3c18668a05b4d

Author: Daniel-Constantin Mierla <mico...@gmail.com>
Committer: Daniel-Constantin Mierla <mico...@gmail.com>
Date: 2020-11-19T16:16:35+01:00

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

---

Modified: src/core/cfg.lex
Modified: src/core/ppcfg.h

---

Diff:  
https://github.com/kamailio/kamailio/commit/b40a2a42a3f4f3a21f2d0a2f9ec3c18668a05b4d.diff
Patch: 
https://github.com/kamailio/kamailio/commit/b40a2a42a3f4f3a21f2d0a2f9ec3c18668a05b4d.patch

---

diff --git a/src/core/cfg.lex b/src/core/cfg.lex
index 0d18031692..e6afb549d1 100644
--- a/src/core/cfg.lex
+++ b/src/core/cfg.lex
@@ -125,7 +125,7 @@
 
 /* start conditions */
 %x STRING1 STRING2 STR_BETWEEN COMMENT COMMENT_LN ATTR SELECT AVP_PVAR PVAR_P
-%x PVARID INCLF IMPTF EVRTNAME CFGPRINTMODE CFGPRINTLOADMOD
+%x PVARID INCLF IMPTF EVRTNAME CFGPRINTMODE CFGPRINTLOADMOD DEFENV_ID
 %x LINECOMMENT DEFINE_ID DEFINE_EOL DEFINE_DATA IFDEF_ID IFDEF_EOL IFDEF_SKIP
 
 /* config script types : #!SER  or #!KAMAILIO or #!MAX_COMPAT */
@@ -551,6 +551,7 @@ IFNDEF       ifndef
 ENDIF        endif
 TRYDEF       "trydefine"|"trydef"
 REDEF        "redefine"|"redef"
+DEFENV       defenv
 
 /* else is already defined */
 
@@ -1413,6 +1414,27 @@ IMPORTFILE      "import_file"
                                ksr_cfg_print_initial_state();
 }
 
+<INITIAL,CFGPRINTMODE>{PREP_START}{DEFENV}  { count();
+                       ksr_cfg_print_part(yytext);
+                       state = DEFINE_S;
+                       BEGIN(DEFENV_ID);
+}
+
+<DEFENV_ID>[ \t]*      { /* eat the whitespace */
+                               count();
+                               ksr_cfg_print_part(yytext);
+                       }
+<DEFENV_ID>[^ \t\r\n]+   { /* get the define id of environment variable */
+                               count();
+                               ksr_cfg_print_part(yytext);
+                               if(pp_define_env(yytext, yyleng) < 0) {
+                                       LM_CRIT("error at %s line %d\n", 
(finame)?finame:"cfg", line);
+                                       exit(-1);
+                               }
+                               state = INITIAL;
+                               ksr_cfg_print_initial_state();
+}
+
 <CFGPRINTMODE>{LOADMODULE}     { count(); printf("%s", yytext);
                                BEGIN(CFGPRINTLOADMOD);
                        }
@@ -2000,6 +2022,47 @@ int pp_define_set(int len, char *text)
        return 0;
 }
 
+int pp_define_env(const char * text, int len)
+{
+       char *r;
+       str defname;
+       str defvalue;
+
+       r = strchr(text, '=');
+
+       defname.s = (char*)text;
+       if(r == NULL) {
+               defname.len = len;
+               r = (char*)text;
+       } else {
+               defname.len = r - text;
+               r++;
+               if(strlen(r) == 0) {
+                       LM_ERR("invalid defenv id [%s]\n", (char*)text);
+                       return -1;
+               }
+       }
+       defvalue.s = getenv(r);
+
+       if(defvalue.s == NULL) {
+               LM_ERR("env variable not defined [%s]\n", (char*)text);
+               return -1;
+       }
+       defvalue.len = strlen(defvalue.s);
+
+       pp_define_set_type(0);
+       if(pp_define(defname.len, defname.s)<0) {
+               LM_ERR("cannot set define name [%s]\n", (char*)text);
+               return -1;
+       }
+       if(pp_define_set(defvalue.len, defvalue.s)<0) {
+               LM_ERR("cannot set define value [%s]\n", (char*)text);
+               return -1;
+       }
+
+       return 0;
+}
+
 str *pp_define_get(int len, const char * text)
 {
        str var = {(char *)text, len};
diff --git a/src/core/ppcfg.h b/src/core/ppcfg.h
index 0573b0165d..70627268b6 100644
--- a/src/core/ppcfg.h
+++ b/src/core/ppcfg.h
@@ -42,6 +42,7 @@ int  pp_define(int len, const char *text);
 int  pp_define_set(int len, char *text);
 int  pp_define_set_type(int type);
 str *pp_define_get(int len, const char * text);
+int  pp_define_env(const char * text, int len);
 
 void pp_ifdef_level_update(int val);
 int pp_ifdef_level_check(void);


_______________________________________________
Kamailio (SER) - Development Mailing List
sr-dev@lists.kamailio.org
https://lists.kamailio.org/cgi-bin/mailman/listinfo/sr-dev

Reply via email to