Browse code

core: new preprocessor directives: trydef and redef

- #!trydef - don't define if it is already done
- #!redef - redefine even already defined
- both have the prototype like #!define

Daniel-Constantin Mierla authored on 05/07/2011 21:45:14
Showing 3 changed files
... ...
@@ -553,6 +553,9 @@ DEFINE       "define"|"def"
553 553
 IFDEF        ifdef
554 554
 IFNDEF       ifndef
555 555
 ENDIF        endif
556
+TRYDEF       "trydefine"|"trydef"
557
+REDEF        "redefine"|"redef"
558
+
556 559
 /* else is already defined */
557 560
 
558 561
 EAT_ABLE	[\ \t\b\r]
... ...
@@ -1217,7 +1220,11 @@ IMPORTFILE      "import_file"
1217 1217
 <INITIAL>{COM_LINE}!{MAXCOMPAT_CFG}{CR}	{ count(); 
1218 1218
 												sr_cfg_compat=SR_COMPAT_MAX;}
1219 1219
 
1220
-<INITIAL>{PREP_START}{DEFINE}{EAT_ABLE}+	{	count();
1220
+<INITIAL>{PREP_START}{DEFINE}{EAT_ABLE}+	{	count(); pp_define_set_type(0);
1221
+											state = DEFINE_S; BEGIN(DEFINE_ID); }
1222
+<INITIAL>{PREP_START}{TRYDEF}{EAT_ABLE}+	{	count(); pp_define_set_type(1);
1223
+											state = DEFINE_S; BEGIN(DEFINE_ID); }
1224
+<INITIAL>{PREP_START}{REDEF}{EAT_ABLE}+	{	count(); pp_define_set_type(2);
1221 1225
 											state = DEFINE_S; BEGIN(DEFINE_ID); }
1222 1226
 <DEFINE_ID>{ID}                 {	count();
1223 1227
 									if (pp_define(yyleng, yytext)) return 1;
... ...
@@ -1666,6 +1673,8 @@ static int sr_pop_yy_state()
1666 1666
 #define MAX_DEFINES    256
1667 1667
 static str pp_defines[MAX_DEFINES][2];
1668 1668
 static int pp_num_defines = 0;
1669
+static int pp_define_type = 0;
1670
+static int pp_define_index = -1;
1669 1671
 
1670 1672
 /* pp_ifdef_stack[i] is 1 if the ifdef test at depth i is either
1671 1673
  * ifdef(defined), ifndef(undefined), or the opposite of these
... ...
@@ -1687,23 +1696,55 @@ static int pp_lookup(int len, const char * text)
1687 1687
 	return -1;
1688 1688
 }
1689 1689
 
1690
+int pp_define_set_type(int type)
1691
+{
1692
+	pp_define_type = type;
1693
+	return 0;
1694
+}
1695
+
1690 1696
 int pp_define(int len, const char * text)
1691 1697
 {
1698
+	int ppos;
1699
+
1700
+	LOG(L_DBG, "++++++ attempting to define: %.*s\n", len, text);
1701
+
1692 1702
 	if (pp_num_defines == MAX_DEFINES) {
1693 1703
 		LOG(L_CRIT, "ERROR: too many defines -- adjust MAX_DEFINES\n");
1694 1704
 		return -1;
1695 1705
 	}
1696 1706
 
1697
-	if (pp_lookup(len, text) >= 0) {
1698
-		LOG(L_CRIT, "ERROR: already defined: %.*s\n", len, text);
1699
-		return -1;
1707
+	pp_define_index = -1;
1708
+	ppos = pp_lookup(len, text);
1709
+	if(ppos >= 0) {
1710
+		if(pp_define_type==1) {
1711
+			LOG(L_DBG, "ignoring - already defined: %.*s\n", len, text);
1712
+			pp_define_index = -2;
1713
+			return 0;
1714
+		} else if(pp_define_type==2) {
1715
+			LOG(L_DBG, "redefining: %.*s\n", len, text);
1716
+			pp_define_index = ppos;
1717
+			if(pp_defines[ppos][1].s != NULL) {
1718
+				pkg_free(pp_defines[ppos][1].s);
1719
+				pp_defines[ppos][1].len = 0;
1720
+				pp_defines[ppos][1].s = NULL;
1721
+			}
1722
+			return 0;
1723
+		} else {
1724
+			LOG(L_CRIT, "ERROR: already defined: %.*s\n", len, text);
1725
+			return -1;
1726
+		}
1700 1727
 	}
1701 1728
 
1702 1729
 	pp_defines[pp_num_defines][0].len = len;
1703 1730
 	pp_defines[pp_num_defines][0].s = (char*)pkg_malloc(len+1);
1731
+	if(pp_defines[pp_num_defines][0].s==NULL) {
1732
+		LOG(L_CRIT, "no more memory to define: %.*s\n", len, text);
1733
+		return -1;
1734
+	}
1704 1735
 	memcpy(pp_defines[pp_num_defines][0].s, text, len);
1705 1736
 	pp_defines[pp_num_defines][1].len = 0;
1706 1737
 	pp_defines[pp_num_defines][1].s = NULL;
1738
+	pp_define_index = pp_num_defines;
1707 1739
 	pp_num_defines++;
1708 1740
 
1709 1741
 	return 0;
... ...
@@ -1711,38 +1752,51 @@ int pp_define(int len, const char * text)
1711 1711
 
1712 1712
 int pp_define_set(int len, char *text)
1713 1713
 {
1714
+	int ppos;
1715
+
1716
+	if(pp_define_index == -2) {
1717
+		/* #!trydef that should be ignored */
1718
+		return 0;
1719
+	}
1720
+
1721
+	if(pp_define_index < 0) {
1722
+		/* invalid position in define table */
1723
+		LOG(L_BUG, "BUG: the index in define table not set yet\n");
1724
+		return -1;
1725
+	}
1714 1726
 	if(len<=0) {
1715 1727
 		LOG(L_DBG, "no define value - ignoring\n");
1716 1728
 		return 0;
1717 1729
 	}
1718 1730
 	if (pp_num_defines == MAX_DEFINES) {
1719
-		LOG(L_BUG, "BUG: setting define value, but no define id yet\n");
1731
+		LOG(L_CRIT, "ERROR: too many defines -- adjust MAX_DEFINES\n");
1720 1732
 		return -1;
1721 1733
 	}
1722 1734
 	if (pp_num_defines == 0) {
1723
-		LOG(L_CRIT, "ERROR: too many defines -- adjust MAX_DEFINES\n");
1735
+		LOG(L_BUG, "BUG: setting define value, but no define id yet\n");
1724 1736
 		return -1;
1725 1737
 	}
1726 1738
 
1727
-	if (pp_defines[pp_num_defines-1][0].s == NULL) {
1739
+	ppos = pp_define_index;
1740
+	if (pp_defines[ppos][0].s == NULL) {
1728 1741
 		LOG(L_BUG, "BUG: last define ID is null\n");
1729 1742
 		return -1;
1730 1743
 	}
1731 1744
 
1732
-	if (pp_defines[pp_num_defines-1][1].s != NULL) {
1733
-		LOG(L_BUG, "BUG: ID %.*s redefined\n",
1734
-			pp_defines[pp_num_defines-1][0].len,
1735
-			pp_defines[pp_num_defines-1][0].s);
1745
+	if (pp_defines[ppos][1].s != NULL) {
1746
+		LOG(L_BUG, "BUG: ID %.*s [%d] overwritten\n",
1747
+			pp_defines[ppos][0].len,
1748
+			pp_defines[ppos][0].s, ppos);
1736 1749
 		return -1;
1737 1750
 	}
1738 1751
 
1739
-	pp_defines[pp_num_defines-1][1].len = len;
1740
-	pp_defines[pp_num_defines-1][1].s = text;
1752
+	pp_defines[ppos][1].len = len;
1753
+	pp_defines[ppos][1].s = text;
1741 1754
 	LM_DBG("### setting define ID [%.*s] value [%.*s]\n",
1742
-			pp_defines[pp_num_defines-1][0].len,
1743
-			pp_defines[pp_num_defines-1][0].s,
1744
-			pp_defines[pp_num_defines-1][1].len,
1745
-			pp_defines[pp_num_defines-1][1].s);
1755
+			pp_defines[ppos][0].len,
1756
+			pp_defines[ppos][0].s,
1757
+			pp_defines[ppos][1].len,
1758
+			pp_defines[ppos][1].s);
1746 1759
 	return 0;
1747 1760
 }
1748 1761
 
... ...
@@ -1912,6 +1912,7 @@ int main(int argc, char** argv)
1912 1912
 					if(p) {
1913 1913
 						*p = '\0';
1914 1914
 					}
1915
+					pp_define_set_type(0);
1915 1916
 					if(pp_define(strlen(optarg), optarg)<0) {
1916 1917
 						fprintf(stderr, "error at define param: -A %s\n",
1917 1918
 								optarg);
... ...
@@ -28,6 +28,7 @@ int pp_subst_run(char **data);
28 28
 
29 29
 int  pp_define(int len, const char *text);
30 30
 int  pp_define_set(int len, char *text);
31
+int  pp_define_set_type(int type);
31 32
 
32 33
 #endif /*_PPCFG_H_*/
33 34