OSDN Git Service

Add MS7619SE
[uclinux-h8/uClinux-dist.git] / user / ser / modparam.c
1 /*
2  * $Id: modparam.c,v 1.8 2004/12/03 19:09:31 andrei Exp $
3  *
4  *
5  * Copyright (C) 2001-2003 FhG Fokus
6  *
7  * This file is part of ser, a free SIP server.
8  *
9  * ser is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version
13  *
14  * For a license to use the ser software under conditions
15  * other than those described here, or to purchase support for this
16  * software, please contact iptel.org by e-mail at the following addresses:
17  *    info@iptel.org
18  *
19  * ser is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License 
25  * along with this program; if not, write to the Free Software 
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)
31  * 2004-03-12  extra flag USE_FUNC_PARAM added to modparam type -
32  *             instead of copying the param value, a func is called (bogdan)
33  */
34
35
36 #include "modparam.h"
37 #include "dprint.h"
38 #include "mem/mem.h"
39 #include <sys/types.h>
40 #include <regex.h>
41 #include <string.h>
42
43
44 int set_mod_param(char* _mod, char* _name, modparam_t _type, void* _val)
45 {
46         void* ptr;
47         
48         if (!_mod) {
49                 LOG(L_ERR, "set_mod_param(): Invalid _mod parameter value\n");
50                 return -1;
51         }
52
53         if (!_name) {
54                 LOG(L_ERR, "set_mod_param(): Invalid _name parameter value\n");
55                 return -2;
56         }
57
58         ptr = find_param_export(_mod, _name, _type);
59         if (!ptr) {
60                 LOG(L_ERR, "set_mod_param(): Parameter not found\n");
61                 return -3;
62         }
63
64         switch(_type) {
65         case STR_PARAM:
66                 *((char**)ptr) = strdup((char*)_val);
67                 break;
68
69         case INT_PARAM:
70                 *((int*)ptr) = (int)(long)_val;
71                 break;
72         }
73
74         return 0;
75 }
76
77
78 int set_mod_param_regex(char* regex, char* name, modparam_t type, void* val)
79 {
80         struct sr_module* t;
81         param_export_t* param;
82         regex_t preg;
83         int mod_found, len;
84         char* reg;
85         int n;
86
87         len = strlen(regex);
88         reg = pkg_malloc(len + 2 + 1);
89         if (reg == 0) {
90                 LOG(L_ERR, "set_mod_param_regex(): No memory left\n");
91                 return -1;
92         }
93         reg[0] = '^';
94         memcpy(reg + 1, regex, len);
95         reg[len + 1] = '$';
96         reg[len + 2] = '\0';
97         
98         if (regcomp(&preg, reg, REG_EXTENDED | REG_NOSUB | REG_ICASE)) {
99                 LOG(L_ERR, "set_mod_param_regex(): Error while compiling regular expression\n");
100                 pkg_free(reg);
101                 return -2;
102         }
103         
104         mod_found = 0;
105
106         for(t = modules; t; t = t->next) {
107                 if (regexec(&preg, t->exports->name, 0, 0, 0) == 0) {
108                         DBG("set_mod_param_regex: %s matches module %s\n",
109                                         regex, t->exports->name);
110                         mod_found = 1;
111                         for(param=t->exports->params;param && param->name ; param++) {
112                                 if ((strcmp(name, param->name) == 0) &&
113                                 ( PARAM_TYPE_MASK(param->type) == type)) {
114                                         DBG("set_mod_param_regex: found <%s> in module %s [%s]\n",
115                                                 name, t->exports->name, t->path);
116
117                                         if (param->type&USE_FUNC_PARAM) {
118                                                 n = ((param_func_t)(param->param_pointer))(type, val );
119                                                 if (n<0)
120                                                         return -4;
121                                         } else {
122                                                 switch(type) {
123                                                         case STR_PARAM:
124                                                                 *((char**)(param->param_pointer)) =
125                                                                         strdup((char*)val);
126                                                                 break;
127                                                         case INT_PARAM:
128                                                                 *((int*)(param->param_pointer)) =
129                                                                         (int)(long)val;
130                                                                 break;
131                                                 }
132                                         }
133
134                                         break;
135                                 }
136                         }
137                         if (!param || !param->name) {
138                                 LOG(L_ERR, "set_mod_param_regex: parameter <%s> not found in module <%s>\n",
139                                     name, t->exports->name);
140                                 regfree(&preg);
141                                 pkg_free(reg);
142                                 return -3;
143                         }
144                 }
145         }
146
147         regfree(&preg);
148         if (!mod_found) {
149                 LOG(L_ERR, "set_mod_param_regex: No module matching %s found\n|", regex);
150                 pkg_free(reg);
151                 return -4;
152         }
153
154         pkg_free(reg);
155         return 0;
156 }