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 1220
 <INITIAL>{COM_LINE}!{MAXCOMPAT_CFG}{CR}	{ count(); 
1218 1221
 												sr_cfg_compat=SR_COMPAT_MAX;}
1219 1222
 
1220
-<INITIAL>{PREP_START}{DEFINE}{EAT_ABLE}+	{	count();
1223
+<INITIAL>{PREP_START}{DEFINE}{EAT_ABLE}+	{	count(); pp_define_set_type(0);
1224
+											state = DEFINE_S; BEGIN(DEFINE_ID); }
1225
+<INITIAL>{PREP_START}{TRYDEF}{EAT_ABLE}+	{	count(); pp_define_set_type(1);
1226
+											state = DEFINE_S; BEGIN(DEFINE_ID); }
1227
+<INITIAL>{PREP_START}{REDEF}{EAT_ABLE}+	{	count(); pp_define_set_type(2);
1221 1228
 											state = DEFINE_S; BEGIN(DEFINE_ID); }
1222 1229
 <DEFINE_ID>{ID}                 {	count();
1223 1230
 									if (pp_define(yyleng, yytext)) return 1;
... ...
@@ -1666,6 +1673,8 @@ static int sr_pop_yy_state()
1666 1673
 #define MAX_DEFINES    256
1667 1674
 static str pp_defines[MAX_DEFINES][2];
1668 1675
 static int pp_num_defines = 0;
1676
+static int pp_define_type = 0;
1677
+static int pp_define_index = -1;
1669 1678
 
1670 1679
 /* pp_ifdef_stack[i] is 1 if the ifdef test at depth i is either
1671 1680
  * ifdef(defined), ifndef(undefined), or the opposite of these
... ...
@@ -1687,23 +1696,55 @@ static int pp_lookup(int len, const char * text)
1687 1696
 	return -1;
1688 1697
 }
1689 1698
 
1699
+int pp_define_set_type(int type)
1700
+{
1701
+	pp_define_type = type;
1702
+	return 0;
1703
+}
1704
+
1690 1705
 int pp_define(int len, const char * text)
1691 1706
 {
1707
+	int ppos;
1708
+
1709
+	LOG(L_DBG, "++++++ attempting to define: %.*s\n", len, text);
1710
+
1692 1711
 	if (pp_num_defines == MAX_DEFINES) {
1693 1712
 		LOG(L_CRIT, "ERROR: too many defines -- adjust MAX_DEFINES\n");
1694 1713
 		return -1;
1695 1714
 	}
1696 1715
 
1697
-	if (pp_lookup(len, text) >= 0) {
1698
-		LOG(L_CRIT, "ERROR: already defined: %.*s\n", len, text);
1699
-		return -1;
1716
+	pp_define_index = -1;
1717
+	ppos = pp_lookup(len, text);
1718
+	if(ppos >= 0) {
1719
+		if(pp_define_type==1) {
1720
+			LOG(L_DBG, "ignoring - already defined: %.*s\n", len, text);
1721
+			pp_define_index = -2;
1722
+			return 0;
1723
+		} else if(pp_define_type==2) {
1724
+			LOG(L_DBG, "redefining: %.*s\n", len, text);
1725
+			pp_define_index = ppos;
1726
+			if(pp_defines[ppos][1].s != NULL) {
1727
+				pkg_free(pp_defines[ppos][1].s);
1728
+				pp_defines[ppos][1].len = 0;
1729
+				pp_defines[ppos][1].s = NULL;
1730
+			}
1731
+			return 0;
1732
+		} else {
1733
+			LOG(L_CRIT, "ERROR: already defined: %.*s\n", len, text);
1734
+			return -1;
1735
+		}
1700 1736
 	}
1701 1737
 
1702 1738
 	pp_defines[pp_num_defines][0].len = len;
1703 1739
 	pp_defines[pp_num_defines][0].s = (char*)pkg_malloc(len+1);
1740
+	if(pp_defines[pp_num_defines][0].s==NULL) {
1741
+		LOG(L_CRIT, "no more memory to define: %.*s\n", len, text);
1742
+		return -1;
1743
+	}
1704 1744
 	memcpy(pp_defines[pp_num_defines][0].s, text, len);
1705 1745
 	pp_defines[pp_num_defines][1].len = 0;
1706 1746
 	pp_defines[pp_num_defines][1].s = NULL;
1747
+	pp_define_index = pp_num_defines;
1707 1748
 	pp_num_defines++;
1708 1749
 
1709 1750
 	return 0;
... ...
@@ -1711,38 +1752,51 @@ int pp_define(int len, const char * text)
1711 1752
 
1712 1753
 int pp_define_set(int len, char *text)
1713 1754
 {
1755
+	int ppos;
1756
+
1757
+	if(pp_define_index == -2) {
1758
+		/* #!trydef that should be ignored */
1759
+		return 0;
1760
+	}
1761
+
1762
+	if(pp_define_index < 0) {
1763
+		/* invalid position in define table */
1764
+		LOG(L_BUG, "BUG: the index in define table not set yet\n");
1765
+		return -1;
1766
+	}
1714 1767
 	if(len<=0) {
1715 1768
 		LOG(L_DBG, "no define value - ignoring\n");
1716 1769
 		return 0;
1717 1770
 	}
1718 1771
 	if (pp_num_defines == MAX_DEFINES) {
1719
-		LOG(L_BUG, "BUG: setting define value, but no define id yet\n");
1772
+		LOG(L_CRIT, "ERROR: too many defines -- adjust MAX_DEFINES\n");
1720 1773
 		return -1;
1721 1774
 	}
1722 1775
 	if (pp_num_defines == 0) {
1723
-		LOG(L_CRIT, "ERROR: too many defines -- adjust MAX_DEFINES\n");
1776
+		LOG(L_BUG, "BUG: setting define value, but no define id yet\n");
1724 1777
 		return -1;
1725 1778
 	}
1726 1779
 
1727
-	if (pp_defines[pp_num_defines-1][0].s == NULL) {
1780
+	ppos = pp_define_index;
1781
+	if (pp_defines[ppos][0].s == NULL) {
1728 1782
 		LOG(L_BUG, "BUG: last define ID is null\n");
1729 1783
 		return -1;
1730 1784
 	}
1731 1785
 
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);
1786
+	if (pp_defines[ppos][1].s != NULL) {
1787
+		LOG(L_BUG, "BUG: ID %.*s [%d] overwritten\n",
1788
+			pp_defines[ppos][0].len,
1789
+			pp_defines[ppos][0].s, ppos);
1736 1790
 		return -1;
1737 1791
 	}
1738 1792
 
1739
-	pp_defines[pp_num_defines-1][1].len = len;
1740
-	pp_defines[pp_num_defines-1][1].s = text;
1793
+	pp_defines[ppos][1].len = len;
1794
+	pp_defines[ppos][1].s = text;
1741 1795
 	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);
1796
+			pp_defines[ppos][0].len,
1797
+			pp_defines[ppos][0].s,
1798
+			pp_defines[ppos][1].len,
1799
+			pp_defines[ppos][1].s);
1746 1800
 	return 0;
1747 1801
 }
1748 1802
 
... ...
@@ -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