Browse code

- regular expression support in modparam

Jan Janak authored on 20/03/2003 15:40:06
Showing 4 changed files
... ...
@@ -32,6 +32,7 @@
32 32
  * 2003-01-23  mhomed added (jiri)
33 33
  * 2003-03-19  replaced all mallocs/frees with pkg_malloc/pkg_free (andrei)
34 34
  * 2003-03-19  Added support for route type in find_export (janakj)
35
+ * 2003-03-20  Regex support in modparam (janakj)
35 36
  */
36 37
 
37 38
 
... ...
@@ -372,12 +373,12 @@ module_stm:	LOADMODULE STRING	{ DBG("loading module %s\n", $2);
372 373
 								}
373 374
 		 | LOADMODULE error	{ yyerror("string expected");  }
374 375
                  | MODPARAM LPAREN STRING COMMA STRING COMMA STRING RPAREN {
375
-			 if (set_mod_param($3, $5, STR_PARAM, $7) != 0) {
376
+			 if (set_mod_param_regex($3, $5, STR_PARAM, $7) != 0) {
376 377
 				 yyerror("Can't set module parameter");
377 378
 			 }
378 379
 		   }
379 380
                  | MODPARAM LPAREN STRING COMMA STRING COMMA NUMBER RPAREN {
380
-			 if (set_mod_param($3, $5, INT_PARAM, (void*)$7) != 0) {
381
+			 if (set_mod_param_regex($3, $5, INT_PARAM, (void*)$7) != 0) {
381 382
 				 yyerror("Can't set module parameter");
382 383
 			 }
383 384
 		   }
... ...
@@ -24,11 +24,18 @@
24 24
  * You should have received a copy of the GNU General Public License 
25 25
  * along with this program; if not, write to the Free Software 
26 26
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27
+ *
28
+ * History:
29
+ * -------
30
+ * 2003-03-20  regex support in modparam (janakj)
27 31
  */
28 32
 
29 33
 
30 34
 #include "modparam.h"
31 35
 #include "dprint.h"
36
+#include "mem/mem.h"
37
+#include <sys/types.h>
38
+#include <regex.h>
32 39
 #include <string.h>
33 40
 
34 41
 
... ...
@@ -64,3 +71,75 @@ int set_mod_param(char* _mod, char* _name, modparam_t _type, void* _val)
64 71
 
65 72
 	return 0;
66 73
 }
74
+
75
+
76
+int set_mod_param_regex(char* regex, char* name, modparam_t type, void* val)
77
+{
78
+	struct sr_module* t;
79
+	param_export_t* param;
80
+	regex_t preg;
81
+	int mod_found, len;
82
+	char* reg;
83
+
84
+	len = strlen(regex);
85
+	reg = pkg_malloc(len + 2 + 1);
86
+	if (reg == 0) {
87
+		LOG(L_ERR, "set_mod_param_regex(): No memory left\n");
88
+		return -1;
89
+	}
90
+	reg[0] = '^';
91
+	memcpy(reg + 1, regex, len);
92
+	reg[len + 1] = '$';
93
+	reg[len + 2] = '\0';
94
+	
95
+	if (regcomp(&preg, reg, REG_EXTENDED | REG_NOSUB | REG_ICASE)) {
96
+		LOG(L_ERR, "set_mod_param_regex(): Error while compiling regular expression\n");
97
+		pkg_free(reg);
98
+		return -2;
99
+	}
100
+	
101
+	mod_found = 0;
102
+
103
+	for(t = modules; t; t = t->next) {
104
+		if (regexec(&preg, t->exports->name, 0, 0, 0) == 0) {
105
+			DBG("set_mod_param_regex: %s matches module %s\n", regex, t->exports->name);
106
+			mod_found = 1;
107
+			for(param=t->exports->params;param && param->name ; param++) {
108
+				if ((strcmp(name, param->name) == 0) &&
109
+				    (param->type == type)) {
110
+					DBG("set_mod_param_regex: found <%s> in module %s [%s]\n",
111
+					    name, t->exports->name, t->path);
112
+
113
+					switch(type) {
114
+					case STR_PARAM:
115
+						*((char**)(param->param_pointer)) = strdup((char*)val);
116
+						break;
117
+						
118
+					case INT_PARAM:
119
+						*((int*)(param->param_pointer)) = (int)(long)val;
120
+						break;
121
+					}
122
+
123
+					break;
124
+				}
125
+			}
126
+			if (!param || !param->name) {
127
+				LOG(L_ERR, "set_mod_param_regex: parameter <%s> not found in module <%s>\n",
128
+				    name, t->exports->name);
129
+				regfree(&preg);
130
+				pkg_free(reg);
131
+				return -3;
132
+			}
133
+		}
134
+	}
135
+
136
+	regfree(&preg);
137
+	if (!mod_found) {
138
+		LOG(L_ERR, "set_mod_param_regex: No module matching %s found\n|", regex);
139
+		pkg_free(reg);
140
+		return -4;
141
+	}
142
+
143
+	pkg_free(reg);
144
+	return 0;
145
+}
... ...
@@ -34,4 +34,6 @@
34 34
 
35 35
 int set_mod_param(char* _mod, char* _name, modparam_t _type, void* _val);
36 36
 
37
+int set_mod_param_regex(char* regex, char* name, modparam_t type, void* val);
38
+
37 39
 #endif
... ...
@@ -244,7 +244,6 @@ void* find_param_export(char* mod, char* name, modparam_t type)
244 244
 }
245 245
 
246 246
 
247
-
248 247
 /* finds a module, given a pointer to a module function *
249 248
  * returns pointer to module, & if  c!=0, *c=pointer to the
250 249
  * function cmd_export structure*/