Browse Source

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 15 years ago
parent
commit
95dec43142
3 changed files with 31 additions and 6 deletions
  1. 2 4
      cfg.lex
  2. 26 2
      main.c
  3. 3 0
      ppcfg.h

+ 2 - 4
cfg.lex

@@ -166,8 +166,6 @@
 		struct sr_yy_fname *next;
 	} *sr_yy_fname_list = 0;
 
-	static int  pp_define(int len, const char * text);
-	static int  pp_define_set(int len, char * text);
 	static str  *pp_define_get(int len, const char * text);
 	static int  pp_ifdef_type(int pos);
 	static void pp_ifdef_var(int len, const char * text);
@@ -1614,7 +1612,7 @@ static int pp_lookup(int len, const char * text)
 	return -1;
 }
 
-static int pp_define(int len, const char * text)
+int pp_define(int len, const char * text)
 {
 	if (pp_num_defines == MAX_DEFINES) {
 		LOG(L_CRIT, "ERROR: too many defines -- adjust MAX_DEFINES\n");
@@ -1636,7 +1634,7 @@ static int pp_define(int len, const char * text)
 	return 0;
 }
 
-static int  pp_define_set(int len, char *text)
+int  pp_define_set(int len, char *text)
 {
 	if(len<=0) {
 		LOG(L_DBG, "no define value - ignoring\n");

+ 26 - 2
main.c

@@ -183,6 +183,7 @@
 #include "basex.h" /* init */
 #include "pvapi_init.h" /* init */
 #include "pv_core.h" /* register core pvars */
+#include "ppcfg.h"
 
 #ifdef DEBUG_DMALLOC
 #include <dmalloc.h>
@@ -246,7 +247,8 @@ Options:\n\
     -G file      Create a pgid file\n\
     -O nr        Script optimization level (debugging option)\n\
     -a mode      Auto aliases mode: enable with yes or on,\n\
-                  disable with no or off\n"
+                  disable with no or off\n\
+    -A define    Add config pre-processor define (e.g., -A WITH_AUTH)\n"
 #ifdef STATS
 "    -s file     File to which statistics is dumped (disabled otherwise)\n"
 #endif
@@ -1649,6 +1651,7 @@ int main(int argc, char** argv)
 	struct timeval tval;
 	fd_set fds;
 	int res;
+	char *p;
 
 	/*init*/
 	time(&up_since);
@@ -1667,7 +1670,7 @@ int main(int argc, char** argv)
 		"DBG_MSG_QA enabled, ser may exit abruptly\n");
 #endif
 
-	options=  ":f:cm:dVhEb:l:L:n:vrRDTN:W:w:t:u:g:P:G:SQ:O:a:"
+	options=  ":f:cm:dVhEb:l:L:n:vrRDTN:W:w:t:u:g:P:G:SQ:O:a:A:"
 #ifdef STATS
 		"s:"
 #endif
@@ -1749,6 +1752,26 @@ int main(int argc, char** argv)
 					/* user needed for possible shm. pre-init */
 					user=optarg;
 					break;
+			case 'A':
+					p = strchr(optarg, '=');
+					if(p) {
+						*p = '\0';
+					}
+					if(pp_define(strlen(optarg), optarg)<0) {
+						fprintf(stderr, "error at define param: -A %s\n",
+								optarg);
+						goto error;
+					}
+					if(p) {
+						*p = '=';
+						p++;
+						if(pp_define_set(strlen(p), p)<0) {
+							fprintf(stderr, "error at define value: -A %s\n",
+								optarg);
+							goto error;
+						}
+					}
+					break;
 			case 'b':
 			case 'l':
 			case 'n':
@@ -1864,6 +1887,7 @@ try_again:
 			case 'V':
 			case 'h':
 			case 'O':
+			case 'A':
 					break;
 			case 'E':
 					log_stderr=1;	// use in both getopt switches

+ 3 - 0
ppcfg.h

@@ -25,6 +25,9 @@
 int pp_subst_add(char *data);
 int pp_subst_run(char **data);
 
+int  pp_define(int len, const char * text);
+int  pp_define_set(int len, char * text);
+
 #endif /*_PPCFG_H_*/
 
 /* vi: set ts=4 sw=4 tw=79:ai:cindent: */