00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include "modparam.h"
00036 #include "dprint.h"
00037 #include "mem/mem.h"
00038 #include <sys/types.h>
00039 #include <regex.h>
00040 #include <string.h>
00041
00042
00043 int set_mod_param_regex(char* regex, char* name, modparam_t type, void* val)
00044 {
00045 struct sr_module* t;
00046 param_export_t* param;
00047 regex_t preg;
00048 int mod_found, len;
00049 char* reg;
00050 int n;
00051
00052 len = strlen(regex);
00053 reg = pkg_malloc(len + 2 + 2 + 1);
00054 if (reg == 0) {
00055 LM_ERR("no pkg memory left\n");
00056 return -1;
00057 }
00058 reg[0] = '^';
00059 reg[1] = '(';
00060 memcpy(reg + 2, regex, len);
00061 reg[len + 2] = ')';
00062 reg[len + 3] = '$';
00063 reg[len + 4] = '\0';
00064
00065 if (regcomp(&preg, reg, REG_EXTENDED | REG_NOSUB | REG_ICASE)) {
00066 LM_ERR("failed to compile regular expression\n");
00067 pkg_free(reg);
00068 return -2;
00069 }
00070
00071 mod_found = 0;
00072
00073 for(t = modules; t; t = t->next) {
00074 if (regexec(&preg, t->exports->name, 0, 0, 0) == 0) {
00075 LM_DBG("%s matches module %s\n",regex, t->exports->name);
00076 mod_found = 1;
00077 for(param=t->exports->params;param && param->name ; param++) {
00078 if ((strcmp(name, param->name) == 0) &&
00079 ( PARAM_TYPE_MASK(param->type) == type)) {
00080 LM_DBG("found <%s> in module %s [%s]\n",
00081 name, t->exports->name, t->path);
00082
00083 if (param->type&USE_FUNC_PARAM) {
00084 n = ((param_func_t)(param->param_pointer))(type, val );
00085 if (n<0)
00086 return -4;
00087 } else {
00088 switch(type) {
00089 case STR_PARAM:
00090 *((char**)(param->param_pointer)) =
00091 strdup((char*)val);
00092 break;
00093 case INT_PARAM:
00094 *((int*)(param->param_pointer)) =
00095 (int)(long)val;
00096 break;
00097 }
00098 }
00099
00100 break;
00101 }
00102 }
00103 if (!param || !param->name) {
00104 LM_ERR("parameter <%s> not found in module <%s>\n",
00105 name, t->exports->name);
00106 regfree(&preg);
00107 pkg_free(reg);
00108 return -3;
00109 }
00110 }
00111 }
00112
00113 regfree(&preg);
00114 if (!mod_found) {
00115 LM_ERR("no module matching %s found\n|", regex);
00116 pkg_free(reg);
00117 return -4;
00118 }
00119
00120 pkg_free(reg);
00121 return 0;
00122 }