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 372
 								}
373 373
 		 | LOADMODULE error	{ yyerror("string expected");  }
374 374
                  | MODPARAM LPAREN STRING COMMA STRING COMMA STRING RPAREN {
375
-			 if (set_mod_param($3, $5, STR_PARAM, $7) != 0) {
375
+			 if (set_mod_param_regex($3, $5, STR_PARAM, $7) != 0) {
376 376
 				 yyerror("Can't set module parameter");
377 377
 			 }
378 378
 		   }
379 379
                  | MODPARAM LPAREN STRING COMMA STRING COMMA NUMBER RPAREN {
380
-			 if (set_mod_param($3, $5, INT_PARAM, (void*)$7) != 0) {
380
+			 if (set_mod_param_regex($3, $5, INT_PARAM, (void*)$7) != 0) {
381 381
 				 yyerror("Can't set module parameter");
382 382
 			 }
383 383
 		   }
... ...
@@ -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 64
 
65 65
 	return 0;
66 66
 }
67
+
68
+
69
+int set_mod_param_regex(char* regex, char* name, modparam_t type, void* val)
70
+{
71
+	struct sr_module* t;
72
+	param_export_t* param;
73
+	regex_t preg;
74
+	int mod_found, len;
75
+	char* reg;
76
+
77
+	len = strlen(regex);
78
+	reg = pkg_malloc(len + 2 + 1);
79
+	if (reg == 0) {
80
+		LOG(L_ERR, "set_mod_param_regex(): No memory left\n");
81
+		return -1;
82
+	}
83
+	reg[0] = '^';
84
+	memcpy(reg + 1, regex, len);
85
+	reg[len + 1] = '$';
86
+	reg[len + 2] = '\0';
87
+	
88
+	if (regcomp(&preg, reg, REG_EXTENDED | REG_NOSUB | REG_ICASE)) {
89
+		LOG(L_ERR, "set_mod_param_regex(): Error while compiling regular expression\n");
90
+		pkg_free(reg);
91
+		return -2;
92
+	}
93
+	
94
+	mod_found = 0;
95
+
96
+	for(t = modules; t; t = t->next) {
97
+		if (regexec(&preg, t->exports->name, 0, 0, 0) == 0) {
98
+			DBG("set_mod_param_regex: %s matches module %s\n", regex, t->exports->name);
99
+			mod_found = 1;
100
+			for(param=t->exports->params;param && param->name ; param++) {
101
+				if ((strcmp(name, param->name) == 0) &&
102
+				    (param->type == type)) {
103
+					DBG("set_mod_param_regex: found <%s> in module %s [%s]\n",
104
+					    name, t->exports->name, t->path);
105
+
106
+					switch(type) {
107
+					case STR_PARAM:
108
+						*((char**)(param->param_pointer)) = strdup((char*)val);
109
+						break;
110
+						
111
+					case INT_PARAM:
112
+						*((int*)(param->param_pointer)) = (int)(long)val;
113
+						break;
114
+					}
115
+
116
+					break;
117
+				}
118
+			}
119
+			if (!param || !param->name) {
120
+				LOG(L_ERR, "set_mod_param_regex: parameter <%s> not found in module <%s>\n",
121
+				    name, t->exports->name);
122
+				regfree(&preg);
123
+				pkg_free(reg);
124
+				return -3;
125
+			}
126
+		}
127
+	}
128
+
129
+	regfree(&preg);
130
+	if (!mod_found) {
131
+		LOG(L_ERR, "set_mod_param_regex: No module matching %s found\n|", regex);
132
+		pkg_free(reg);
133
+		return -4;
134
+	}
135
+
136
+	pkg_free(reg);
137
+	return 0;
138
+}
... ...
@@ -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*/