Browse code

core: added #!defexps preprocessor directive

- similar to #!defexp but the resulted value is enclosed in double
quotes

Daniel-Constantin Mierla authored on 22/09/2022 07:34:05
Showing 3 changed files
... ...
@@ -576,6 +576,7 @@ ENDIF        endif
576 576
 TRYDEF       "trydefine"|"trydef"
577 577
 REDEF        "redefine"|"redef"
578 578
 DEFEXP       defexp
579
+DEFEXPS      defexps
579 580
 DEFENV       defenv
580 581
 DEFENVS      defenvs
581 582
 TRYDEFENV    trydefenv
... ...
@@ -1344,6 +1345,10 @@ IMPORTFILE      "import_file"
1344 1345
 											ksr_cfg_print_part(yytext);
1345 1346
 											pp_define_set_type(KSR_PPDEF_DEFEXP);
1346 1347
 											state = DEFINE_S; BEGIN(DEFINE_ID); }
1348
+<INITIAL,CFGPRINTMODE>{PREP_START}{DEFEXPS}{EAT_ABLE}+	{	count();
1349
+											ksr_cfg_print_part(yytext);
1350
+											pp_define_set_type(KSR_PPDEF_DEFEXPS);
1351
+											state = DEFINE_S; BEGIN(DEFINE_ID); }
1347 1352
 <DEFINE_ID>{ID}{MINUS}          {	count();
1348 1353
 									ksr_cfg_print_part(yytext);
1349 1354
 									LM_CRIT(
... ...
@@ -2150,8 +2155,13 @@ int pp_define_set(int len, char *text, int mode)
2150 2155
 		return -1;
2151 2156
 	}
2152 2157
 
2153
-	if(pp_defines[ppos].dtype == KSR_PPDEF_DEFEXP) {
2154
-		sval = pp_defexp_eval(text, len);
2158
+	if(pp_defines[ppos].dtype == KSR_PPDEF_DEFEXP
2159
+			|| pp_defines[ppos].dtype == KSR_PPDEF_DEFEXPS) {
2160
+		if(pp_defines[ppos].dtype == KSR_PPDEF_DEFEXP) {
2161
+			sval = pp_defexp_eval(text, len, 0);
2162
+		} else {
2163
+			sval = pp_defexp_eval(text, len, 1);
2164
+		}
2155 2165
 		if(sval==NULL) {
2156 2166
 			LM_NOTICE("no value returned to set the defexp [%.*s]\n",
2157 2167
 				pp_defines[ppos].name.len, pp_defines[ppos].name.s);
... ...
@@ -461,7 +461,7 @@ end:
461 461
 	snexpr_destroy(e, &vars);
462 462
 }
463 463
 
464
-char *pp_defexp_eval(char *exval, int exlen)
464
+char *pp_defexp_eval(char *exval, int exlen, int qmode)
465 465
 {
466 466
 	str exstr;
467 467
 	struct snexpr_var_list vars = {0};
... ...
@@ -505,13 +505,25 @@ char *pp_defexp_eval(char *exval, int exlen)
505 505
 		sval.len = strlen(result->param.stz.sval);
506 506
 	}
507 507
 
508
-	res = (char*)pkg_malloc(sval.len + 1);
508
+	if(qmode==1) {
509
+		res = (char*)pkg_malloc(sval.len + 3);
510
+	} else {
511
+		res = (char*)pkg_malloc(sval.len + 1);
512
+	}
509 513
 	if(res==NULL) {
510 514
 		PKG_MEM_ERROR;
511 515
 		goto done;
512 516
 	}
513
-	memcpy(res, sval.s, sval.len);
514
-	res[sval.len] = '\0';
517
+	if(qmode==1) {
518
+		res[0] = '"';
519
+		memcpy(res, sval.s+1, sval.len);
520
+		res[sval.len+1] = '"';
521
+		res[sval.len+2] = '\0';
522
+		LM_DBG("expression quoted string result: [%s]\n", res);
523
+	} else {
524
+		memcpy(res, sval.s, sval.len);
525
+		res[sval.len] = '\0';
526
+	}
515 527
 
516 528
 done:
517 529
 	snexpr_result_free(result);
... ...
@@ -35,6 +35,7 @@
35 35
 #define KSR_PPDEF_TRYDEF  1
36 36
 #define KSR_PPDEF_REDEF   2
37 37
 #define KSR_PPDEF_DEFEXP  3
38
+#define KSR_PPDEF_DEFEXPS 4
38 39
 
39 40
 typedef struct ksr_ppdefine {
40 41
 	str name;
... ...
@@ -68,7 +69,7 @@ void ksr_cfg_print_initial_state(void);
68 69
 
69 70
 void pp_ifexp_eval(char *exval, int exlen);
70 71
 void pp_ifexp_state(int state);
71
-char *pp_defexp_eval(char *exval, int exlen);
72
+char *pp_defexp_eval(char *exval, int exlen, int qmode);
72 73
 
73 74
 #endif /*_PPCFG_H_*/
74 75