Browse code

core: new param -A to add cfg preprocesor directives

- defines can be specified from command line with option -A
- these defines are available from begining of parsing the config
- examples:
- '-A WITH_AUTH' is like having '#!define WITH_AUTH' at top of cfg
- '-A LISTENIP=127.0.0.1' is like '#!define LISTENIP=127.0.0.1'

Daniel-Constantin Mierla authored on 27/07/2010 21:46:48
Showing 3 changed files
... ...
@@ -166,8 +166,6 @@
166 166
 		struct sr_yy_fname *next;
167 167
 	} *sr_yy_fname_list = 0;
168 168
 
169
-	static int  pp_define(int len, const char * text);
170
-	static int  pp_define_set(int len, char * text);
171 169
 	static str  *pp_define_get(int len, const char * text);
172 170
 	static int  pp_ifdef_type(int pos);
173 171
 	static void pp_ifdef_var(int len, const char * text);
... ...
@@ -1614,7 +1612,7 @@ static int pp_lookup(int len, const char * text)
1614 1614
 	return -1;
1615 1615
 }
1616 1616
 
1617
-static int pp_define(int len, const char * text)
1617
+int pp_define(int len, const char * text)
1618 1618
 {
1619 1619
 	if (pp_num_defines == MAX_DEFINES) {
1620 1620
 		LOG(L_CRIT, "ERROR: too many defines -- adjust MAX_DEFINES\n");
... ...
@@ -1636,7 +1634,7 @@ static int pp_define(int len, const char * text)
1636 1636
 	return 0;
1637 1637
 }
1638 1638
 
1639
-static int  pp_define_set(int len, char *text)
1639
+int  pp_define_set(int len, char *text)
1640 1640
 {
1641 1641
 	if(len<=0) {
1642 1642
 		LOG(L_DBG, "no define value - ignoring\n");
... ...
@@ -183,6 +183,7 @@
183 183
 #include "basex.h" /* init */
184 184
 #include "pvapi_init.h" /* init */
185 185
 #include "pv_core.h" /* register core pvars */
186
+#include "ppcfg.h"
186 187
 
187 188
 #ifdef DEBUG_DMALLOC
188 189
 #include <dmalloc.h>
... ...
@@ -246,7 +247,8 @@ Options:\n\
246 246
     -G file      Create a pgid file\n\
247 247
     -O nr        Script optimization level (debugging option)\n\
248 248
     -a mode      Auto aliases mode: enable with yes or on,\n\
249
-                  disable with no or off\n"
249
+                  disable with no or off\n\
250
+    -A define    Add config pre-processor define (e.g., -A WITH_AUTH)\n"
250 251
 #ifdef STATS
251 252
 "    -s file     File to which statistics is dumped (disabled otherwise)\n"
252 253
 #endif
... ...
@@ -1649,6 +1651,7 @@ int main(int argc, char** argv)
1649 1649
 	struct timeval tval;
1650 1650
 	fd_set fds;
1651 1651
 	int res;
1652
+	char *p;
1652 1653
 
1653 1654
 	/*init*/
1654 1655
 	time(&up_since);
... ...
@@ -1667,7 +1670,7 @@ int main(int argc, char** argv)
1667 1667
 		"DBG_MSG_QA enabled, ser may exit abruptly\n");
1668 1668
 #endif
1669 1669
 
1670
-	options=  ":f:cm:dVhEb:l:L:n:vrRDTN:W:w:t:u:g:P:G:SQ:O:a:"
1670
+	options=  ":f:cm:dVhEb:l:L:n:vrRDTN:W:w:t:u:g:P:G:SQ:O:a:A:"
1671 1671
 #ifdef STATS
1672 1672
 		"s:"
1673 1673
 #endif
... ...
@@ -1749,6 +1752,26 @@ int main(int argc, char** argv)
1749 1749
 					/* user needed for possible shm. pre-init */
1750 1750
 					user=optarg;
1751 1751
 					break;
1752
+			case 'A':
1753
+					p = strchr(optarg, '=');
1754
+					if(p) {
1755
+						*p = '\0';
1756
+					}
1757
+					if(pp_define(strlen(optarg), optarg)<0) {
1758
+						fprintf(stderr, "error at define param: -A %s\n",
1759
+								optarg);
1760
+						goto error;
1761
+					}
1762
+					if(p) {
1763
+						*p = '=';
1764
+						p++;
1765
+						if(pp_define_set(strlen(p), p)<0) {
1766
+							fprintf(stderr, "error at define value: -A %s\n",
1767
+								optarg);
1768
+							goto error;
1769
+						}
1770
+					}
1771
+					break;
1752 1772
 			case 'b':
1753 1773
 			case 'l':
1754 1774
 			case 'n':
... ...
@@ -1864,6 +1887,7 @@ try_again:
1864 1864
 			case 'V':
1865 1865
 			case 'h':
1866 1866
 			case 'O':
1867
+			case 'A':
1867 1868
 					break;
1868 1869
 			case 'E':
1869 1870
 					log_stderr=1;	// use in both getopt switches
... ...
@@ -25,6 +25,9 @@
25 25
 int pp_subst_add(char *data);
26 26
 int pp_subst_run(char **data);
27 27
 
28
+int  pp_define(int len, const char * text);
29
+int  pp_define_set(int len, char * text);
30
+
28 31
 #endif /*_PPCFG_H_*/
29 32
 
30 33
 /* vi: set ts=4 sw=4 tw=79:ai:cindent: */