Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

regex.c File Reference

#include <ctype.h>
#include "gwlib/gwlib.h"
#include "regex.h"

Include dependency graph for regex.c:

Include dependency graph

Go to the source code of this file.

Functions

void gw_regex_destroy (regex_t *preg)
regex_t * gw_regex_comp_real (const Octstr *pattern, int cflags, const char *file, long line, const char *func)
int gw_regex_exec_real (const regex_t *preg, const Octstr *string, size_t nmatch, regmatch_t pmatch[], int eflags, const char *file, long line, const char *func)
Octstrgw_regex_error (int errcode, const regex_t *preg)
char * pstrdup (const char *s)
char * gw_regex_sub (const char *input, const char *source, size_t nmatch, regmatch_t pmatch[])
int gw_regex_match_real (const Octstr *re, const Octstr *os, const char *file, long line, const char *func)
int gw_regex_match_pre_real (const regex_t *preg, const Octstr *os, const char *file, long line, const char *func)
Octstrgw_regex_subst_real (const Octstr *re, const Octstr *os, const Octstr *rule, const char *file, long line, const char *func)
Octstrgw_regex_subst_pre_real (const regex_t *preg, const Octstr *os, const Octstr *rule, const char *file, long line, const char *func)


Function Documentation

regex_t* gw_regex_comp_real const Octstr pattern,
int  cflags,
const char *  file,
long  line,
const char *  func
 

Definition at line 96 of file regex.c.

References error(), file, and octstr_get_cstr.

Referenced by gw_regex_match_real(), and gw_regex_subst_real().

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 }

Here is the call graph for this function:

void gw_regex_destroy regex_t *  preg  ) 
 

Definition at line 86 of file regex.c.

Referenced by conndata_destroy(), destroy_onetrans(), destroy_oneuser(), gw_regex_match_real(), gw_regex_subst_real(), http_close_proxy(), main(), smsc2_cleanup(), and smscconn_destroy().

00087 {
00088     if (preg == NULL)
00089         return;
00090         
00091     regfree(preg);
00092     gw_free(preg);
00093 }

Octstr* gw_regex_error int  errcode,
const regex_t *  preg
 

Definition at line 138 of file regex.c.

References octstr_create.

Referenced by main().

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 }

int gw_regex_exec_real const regex_t *  preg,
const Octstr string,
size_t  nmatch,
regmatch_t  pmatch[],
int  eflags,
const char *  file,
long  line,
const char *  func
 

Definition at line 117 of file regex.c.

References error(), file, gw_assert, octstr_get_cstr, and string.

Referenced by gw_regex_match_pre_real(), gw_regex_match_real(), gw_regex_subst_pre_real(), and gw_regex_subst_real().

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 }

Here is the call graph for this function:

int gw_regex_match_pre_real const regex_t *  preg,
const Octstr os,
const char *  file,
long  line,
const char *  func
 

Definition at line 270 of file regex.c.

References file, gw_assert, and gw_regex_exec_real().

00272 {
00273     int rc;
00274 
00275     gw_assert(preg != NULL);
00276 
00277     /* execute and match */
00278     rc = gw_regex_exec_real(preg, os, 0, NULL, 0, file, line, func);
00279 
00280     return (rc == 0) ? 1 : 0;
00281 }

Here is the call graph for this function:

int gw_regex_match_real const Octstr re,
const Octstr os,
const char *  file,
long  line,
const char *  func
 

Definition at line 250 of file regex.c.

References file, gw_regex_comp_real(), gw_regex_destroy(), and gw_regex_exec_real().

00252 {
00253     regex_t *regexp;
00254     int rc;
00255 
00256     /* compile */
00257     regexp = gw_regex_comp_real(re, REG_EXTENDED|REG_ICASE, file, line, func);
00258     if (regexp == NULL)
00259         return 0;
00260 
00261     /* execute and match */
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 }

Here is the call graph for this function:

char* gw_regex_sub const char *  input,
const char *  source,
size_t  nmatch,
regmatch_t  pmatch[]
 

Definition at line 179 of file regex.c.

References pstrdup().

Referenced by gw_regex_subst_pre_real(), gw_regex_subst_real(), and main().

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     /* First pass, find the size */
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) {           /* Ordinary character. */
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     /* Now actually fill in the string */
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) {           /* Ordinary character. */
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 }

Here is the call graph for this function:

Octstr* gw_regex_subst_pre_real const regex_t *  preg,
const Octstr os,
const Octstr rule,
const char *  file,
long  line,
const char *  func
 

Definition at line 319 of file regex.c.

References file, gw_assert, gw_regex_exec_real(), gw_regex_sub(), octstr_create, octstr_get_cstr, and result.

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     /* execute and match */
00330     rc = gw_regex_exec_real(preg, os, REGEX_MAX_SUB_MATCH, &pmatch[0], 0, 
00331                             file, line, func);
00332 
00333     /* substitute via rule if matched */
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 }

Here is the call graph for this function:

Octstr* gw_regex_subst_real const Octstr re,
const Octstr os,
const Octstr rule,
const char *  file,
long  line,
const char *  func
 

Definition at line 284 of file regex.c.

References file, gw_regex_comp_real(), gw_regex_destroy(), gw_regex_exec_real(), gw_regex_sub(), octstr_create, octstr_get_cstr, and result.

00286 {
00287     Octstr *result;
00288     regex_t *regexp;
00289     regmatch_t pmatch[REGEX_MAX_SUB_MATCH];
00290     int rc;
00291     char *rsub;
00292 
00293     /* compile */
00294     regexp = gw_regex_comp_real(re, REG_EXTENDED|REG_ICASE, file, line, func);
00295     if (regexp == NULL)
00296         return 0;
00297 
00298     /* execute and match */
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     /* substitute via rule if matched */
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 }

Here is the call graph for this function:

char* pstrdup const char *  s  )  [static]
 

Definition at line 151 of file regex.c.

References res.

Referenced by gw_regex_sub().

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 }

See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.