Browse code

core: use a structure for preprocessor defines

- used to be an array of two str
- store type of define operation in structure

Daniel-Constantin Mierla authored on 07/06/2018 09:58:08
Showing 2 changed files
... ...
@@ -1704,7 +1704,7 @@ static int sr_pop_yy_state()
1704 1704
 /* define/ifdef support */
1705 1705
 
1706 1706
 #define MAX_DEFINES    256
1707
-static str pp_defines[MAX_DEFINES][2];
1707
+static ksr_ppdefine_t pp_defines[MAX_DEFINES];
1708 1708
 static int pp_num_defines = 0;
1709 1709
 static int pp_define_type = 0;
1710 1710
 static int pp_define_index = -1;
... ...
@@ -1721,7 +1721,7 @@ str* pp_get_define_name(int idx)
1721 1721
 {
1722 1722
 	if(idx<0 || idx>=pp_num_defines)
1723 1723
 		return NULL;
1724
-	return &pp_defines[idx][0];
1724
+	return &pp_defines[idx].name;
1725 1725
 }
1726 1726
 
1727 1727
 static int pp_lookup(int len, const char * text)
... ...
@@ -1730,7 +1730,7 @@ static int pp_lookup(int len, const char * text)
1730 1730
 	int i;
1731 1731
 
1732 1732
 	for (i=0; i<pp_num_defines; i++)
1733
-		if (STR_EQ(pp_defines[i][0], var))
1733
+		if (STR_EQ(pp_defines[i].name, var))
1734 1734
 			return i;
1735 1735
 
1736 1736
 	return -1;
... ...
@@ -1763,11 +1763,12 @@ int pp_define(int len, const char * text)
1763 1763
 		} else if(pp_define_type==2) {
1764 1764
 			LM_DBG("redefining: %.*s\n", len, text);
1765 1765
 			pp_define_index = ppos;
1766
-			if(pp_defines[ppos][1].s != NULL) {
1767
-				pkg_free(pp_defines[ppos][1].s);
1768
-				pp_defines[ppos][1].len = 0;
1769
-				pp_defines[ppos][1].s = NULL;
1766
+			if(pp_defines[ppos].value.s != NULL) {
1767
+				pkg_free(pp_defines[ppos].value.s);
1768
+				pp_defines[ppos].value.len = 0;
1769
+				pp_defines[ppos].value.s = NULL;
1770 1770
 			}
1771
+			pp_defines[ppos].type = pp_define_type;
1771 1772
 			return 0;
1772 1773
 		} else {
1773 1774
 			LM_CRIT("already defined: %.*s\n", len, text);
... ...
@@ -1775,16 +1776,17 @@ int pp_define(int len, const char * text)
1775 1776
 		}
1776 1777
 	}
1777 1778
 
1778
-	pp_defines[pp_num_defines][0].len = len;
1779
-	pp_defines[pp_num_defines][0].s = (char*)pkg_malloc(len+1);
1780
-	if(pp_defines[pp_num_defines][0].s==NULL) {
1779
+	pp_defines[pp_num_defines].name.len = len;
1780
+	pp_defines[pp_num_defines].name.s = (char*)pkg_malloc(len+1);
1781
+	if(pp_defines[pp_num_defines].name.s==NULL) {
1781 1782
 		LM_CRIT("no more memory to define: %.*s\n", len, text);
1782 1783
 		return -1;
1783 1784
 	}
1784
-	memcpy(pp_defines[pp_num_defines][0].s, text, len);
1785
-	pp_defines[pp_num_defines][0].s[len] = '\0';
1786
-	pp_defines[pp_num_defines][1].len = 0;
1787
-	pp_defines[pp_num_defines][1].s = NULL;
1785
+	memcpy(pp_defines[pp_num_defines].name.s, text, len);
1786
+	pp_defines[pp_num_defines].name.s[len] = '\0';
1787
+	pp_defines[pp_num_defines].value.len = 0;
1788
+	pp_defines[pp_num_defines].value.s = NULL;
1789
+	pp_defines[pp_num_defines].type = pp_define_type;
1788 1790
 	pp_define_index = pp_num_defines;
1789 1791
 	pp_num_defines++;
1790 1792
 
... ...
@@ -1819,25 +1821,25 @@ int pp_define_set(int len, char *text)
1819 1821
 	}
1820 1822
 
1821 1823
 	ppos = pp_define_index;
1822
-	if (pp_defines[ppos][0].s == NULL) {
1824
+	if (pp_defines[ppos].name.s == NULL) {
1823 1825
 		LM_BUG("BUG: last define ID is null\n");
1824 1826
 		return -1;
1825 1827
 	}
1826 1828
 
1827
-	if (pp_defines[ppos][1].s != NULL) {
1829
+	if (pp_defines[ppos].value.s != NULL) {
1828 1830
 		LM_BUG("BUG: ID %.*s [%d] overwritten\n",
1829
-			pp_defines[ppos][0].len,
1830
-			pp_defines[ppos][0].s, ppos);
1831
+			pp_defines[ppos].name.len,
1832
+			pp_defines[ppos].name.s, ppos);
1831 1833
 		return -1;
1832 1834
 	}
1833 1835
 
1834
-	pp_defines[ppos][1].len = len;
1835
-	pp_defines[ppos][1].s = text;
1836
+	pp_defines[ppos].value.len = len;
1837
+	pp_defines[ppos].value.s = text;
1836 1838
 	LM_DBG("### setting define ID [%.*s] value [%.*s]\n",
1837
-			pp_defines[ppos][0].len,
1838
-			pp_defines[ppos][0].s,
1839
-			pp_defines[ppos][1].len,
1840
-			pp_defines[ppos][1].s);
1839
+			pp_defines[ppos].name.len,
1840
+			pp_defines[ppos].name.s,
1841
+			pp_defines[ppos].value.len,
1842
+			pp_defines[ppos].value.s);
1841 1843
 	return 0;
1842 1844
 }
1843 1845
 
... ...
@@ -1848,16 +1850,16 @@ static str *pp_define_get(int len, const char * text)
1848 1850
 
1849 1851
 	for (i=0; i<pp_num_defines; i++)
1850 1852
 	{
1851
-		if (STR_EQ(pp_defines[i][0], var))
1853
+		if (STR_EQ(pp_defines[i].name, var))
1852 1854
 		{
1853
-			if(pp_defines[i][0].s!=NULL)
1855
+			if(pp_defines[i].name.s!=NULL)
1854 1856
 			{
1855 1857
 				LM_DBG("### returning define ID [%.*s] value [%.*s]\n",
1856
-					pp_defines[i][0].len,
1857
-					pp_defines[i][0].s,
1858
-					pp_defines[i][1].len,
1859
-					pp_defines[i][1].s);
1860
-				return &pp_defines[i][1];
1858
+					pp_defines[i].name.len,
1859
+					pp_defines[i].name.s,
1860
+					pp_defines[i].value.len,
1861
+					pp_defines[i].value.s);
1862
+				return &pp_defines[i].value;
1861 1863
 			}
1862 1864
 			return NULL;
1863 1865
 		}
... ...
@@ -25,6 +25,12 @@
25 25
 
26 26
 #include "str.h"
27 27
 
28
+typedef struct ksr_ppdefine {
29
+	str name;
30
+	str value;
31
+	int type;
32
+} ksr_ppdefine_t;
33
+
28 34
 str* pp_get_define_name(int idx);
29 35
 
30 36
 int pp_subst_add(char *data);