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
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 #include <ctype.h>
00071
00072 #include "gwlib/gwlib.h"
00073 #include "regex.h"
00074
00075
00076
00077
00078
00079 #if defined(HAVE_REGEX) || defined(HAVE_PCRE)
00080
00081
00082
00083
00084
00085
00086 void gw_regex_destroy(regex_t *preg)
00087 {
00088 if (preg == NULL)
00089 return;
00090
00091 regfree(preg);
00092 gw_free(preg);
00093 }
00094
00095
00096 regex_t *gw_regex_comp_real(const Octstr *pattern, int cflags, const char *file,
00097 long line, const char *func)
00098 {
00099 int rc;
00100 regex_t *preg;
00101
00102 preg = gw_malloc(sizeof(regex_t));
00103
00104 if ((rc = regcomp(preg, pattern ? octstr_get_cstr(pattern) : NULL, cflags)) != 0) {
00105 char buffer[512];
00106 regerror(rc, preg, buffer, sizeof(buffer));
00107 error(0, "%s:%ld: %s: regex compilation `%s' failed: %s (Called from %s:%ld:%s.)",
00108 __FILE__, (long) __LINE__, __func__, octstr_get_cstr(pattern), buffer,
00109 (file), (long) (line), (func));
00110 return NULL;
00111 }
00112
00113 return preg;
00114 }
00115
00116
00117 int gw_regex_exec_real(const regex_t *preg, const Octstr *string, size_t nmatch,
00118 regmatch_t pmatch[], int eflags, const char *file, long line,
00119 const char *func)
00120 {
00121 int rc;
00122
00123 gw_assert(preg != NULL);
00124
00125 rc = regexec(preg, string ? octstr_get_cstr(string) : NULL, nmatch, pmatch, eflags);
00126 if (rc != REG_NOMATCH && rc != 0) {
00127 char buffer[512];
00128 regerror(rc, preg, buffer, sizeof(buffer));
00129 error(0, "%s:%ld: %s: regex execution on `%s' failed: %s (Called from %s:%ld:%s.)",
00130 __FILE__, (long) __LINE__, __func__, octstr_get_cstr(string), buffer,
00131 (file), (long) (line), (func));
00132 }
00133
00134 return rc;
00135 }
00136
00137
00138 Octstr *gw_regex_error(int errcode, const regex_t *preg)
00139 {
00140 char errbuf[512];
00141 Octstr *os;
00142
00143 regerror(errcode, preg, errbuf, sizeof(errbuf));
00144 os = octstr_create(errbuf);
00145
00146 return os;
00147 }
00148
00149
00150
00151 static char *pstrdup(const char *s)
00152 {
00153 char *res;
00154 size_t len;
00155
00156 if (s == NULL)
00157 return NULL;
00158 len = strlen(s) + 1;
00159 res = gw_malloc(len);
00160 memcpy(res, s, len);
00161 return res;
00162 }
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179 char *gw_regex_sub(const char *input, const char *source,
00180 size_t nmatch, regmatch_t pmatch[])
00181 {
00182 const char *src = input;
00183 char *dest, *dst;
00184 char c;
00185 size_t no;
00186 int len;
00187
00188 if (!source)
00189 return NULL;
00190 if (!nmatch)
00191 return pstrdup(src);
00192
00193
00194 len = 0;
00195 while ((c = *src++) != '\0') {
00196 if (c == '&')
00197 no = 0;
00198 else if (c == '$' && isdigit(*src))
00199 no = *src++ - '0';
00200 else
00201 no = 10;
00202
00203 if (no > 9) {
00204 if (c == '\\' && (*src == '$' || *src == '&'))
00205 c = *src++;
00206 len++;
00207 }
00208 else if (no < nmatch && pmatch[no].rm_so < pmatch[no].rm_eo) {
00209 len += pmatch[no].rm_eo - pmatch[no].rm_so;
00210 }
00211 }
00212
00213 dest = dst = gw_malloc(len + 1);
00214
00215
00216 src = input;
00217 while ((c = *src++) != '\0') {
00218 if (c == '&')
00219 no = 0;
00220 else if (c == '$' && isdigit(*src))
00221 no = *src++ - '0';
00222 else
00223 no = 10;
00224
00225 if (no > 9) {
00226 if (c == '\\' && (*src == '$' || *src == '&'))
00227 c = *src++;
00228 *dst++ = c;
00229 }
00230 else if (no < nmatch && pmatch[no].rm_so < pmatch[no].rm_eo) {
00231 len = pmatch[no].rm_eo - pmatch[no].rm_so;
00232 memcpy(dst, source + pmatch[no].rm_so, len);
00233 dst += len;
00234 }
00235 }
00236 *dst = '\0';
00237
00238 return dest;
00239 }
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250 int gw_regex_match_real(const Octstr *re, const Octstr *os, const char *file,
00251 long line, const char *func)
00252 {
00253 regex_t *regexp;
00254 int rc;
00255
00256
00257 regexp = gw_regex_comp_real(re, REG_EXTENDED|REG_ICASE, file, line, func);
00258 if (regexp == NULL)
00259 return 0;
00260
00261
00262 rc = gw_regex_exec_real(regexp, os, 0, NULL, 0, file, line, func);
00263
00264 gw_regex_destroy(regexp);
00265
00266 return (rc == 0) ? 1 : 0;
00267 }
00268
00269
00270 int gw_regex_match_pre_real(const regex_t *preg, const Octstr *os, const char *file,
00271 long line, const char *func)
00272 {
00273 int rc;
00274
00275 gw_assert(preg != NULL);
00276
00277
00278 rc = gw_regex_exec_real(preg, os, 0, NULL, 0, file, line, func);
00279
00280 return (rc == 0) ? 1 : 0;
00281 }
00282
00283
00284 Octstr *gw_regex_subst_real(const Octstr *re, const Octstr *os, const Octstr *rule,
00285 const char *file, long line, const char *func)
00286 {
00287 Octstr *result;
00288 regex_t *regexp;
00289 regmatch_t pmatch[REGEX_MAX_SUB_MATCH];
00290 int rc;
00291 char *rsub;
00292
00293
00294 regexp = gw_regex_comp_real(re, REG_EXTENDED|REG_ICASE, file, line, func);
00295 if (regexp == NULL)
00296 return 0;
00297
00298
00299 rc = gw_regex_exec_real(regexp, os, REGEX_MAX_SUB_MATCH, &pmatch[0], 0,
00300 file, line, func);
00301 gw_regex_destroy(regexp);
00302
00303
00304 if (rc != 0)
00305 return NULL;
00306
00307 rsub = gw_regex_sub(octstr_get_cstr(rule), octstr_get_cstr(os),
00308 REGEX_MAX_SUB_MATCH, &pmatch[0]);
00309 if (rsub == NULL)
00310 return NULL;
00311
00312 result = octstr_create(rsub);
00313 gw_free(rsub);
00314
00315 return result;
00316 }
00317
00318
00319 Octstr *gw_regex_subst_pre_real(const regex_t *preg, const Octstr *os, const Octstr *rule,
00320 const char *file, long line, const char *func)
00321 {
00322 Octstr *result;
00323 regmatch_t pmatch[REGEX_MAX_SUB_MATCH];
00324 int rc;
00325 char *rsub;
00326
00327 gw_assert(preg != NULL);
00328
00329
00330 rc = gw_regex_exec_real(preg, os, REGEX_MAX_SUB_MATCH, &pmatch[0], 0,
00331 file, line, func);
00332
00333
00334 if (rc != 0)
00335 return NULL;
00336
00337 rsub = gw_regex_sub(octstr_get_cstr(rule), octstr_get_cstr(os),
00338 REGEX_MAX_SUB_MATCH, &pmatch[0]);
00339 if (rsub == NULL)
00340 return NULL;
00341
00342 result = octstr_create(rsub);
00343 gw_free(rsub);
00344
00345 return result;
00346 }
00347
00348 #endif
00349
See file LICENSE for details about the license agreement for using,
modifying, copying or deriving work from this software.