PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
regex.h File Reference
#include "mb/pg_wchar.h"
Include dependency graph for regex.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  regex_t
 
struct  regmatch_t
 
struct  rm_detail_t
 

Macros

#define _REGEX_H_   /* never again */
 
#define REG_UBACKREF   000001
 
#define REG_ULOOKAROUND   000002
 
#define REG_UBOUNDS   000004
 
#define REG_UBRACES   000010
 
#define REG_UBSALNUM   000020
 
#define REG_UPBOTCH   000040
 
#define REG_UBBS   000100
 
#define REG_UNONPOSIX   000200
 
#define REG_UUNSPEC   000400
 
#define REG_UUNPORT   001000
 
#define REG_ULOCALE   002000
 
#define REG_UEMPTYMATCH   004000
 
#define REG_UIMPOSSIBLE   010000
 
#define REG_USHORTEST   020000
 
#define REG_BASIC   000000 /* BREs (convenience) */
 
#define REG_EXTENDED   000001 /* EREs */
 
#define REG_ADVF   000002 /* advanced features in EREs */
 
#define REG_ADVANCED   000003 /* AREs (which are also EREs) */
 
#define REG_QUOTE   000004 /* no special characters, none */
 
#define REG_NOSPEC   REG_QUOTE /* historical synonym */
 
#define REG_ICASE   000010 /* ignore case */
 
#define REG_NOSUB   000020 /* don't care about subexpressions */
 
#define REG_EXPANDED   000040 /* expanded format, white space & comments */
 
#define REG_NLSTOP   000100 /* \n doesn't match . or [^ ] */
 
#define REG_NLANCH   000200 /* ^ matches after \n, $ before */
 
#define REG_NEWLINE   000300 /* newlines are line terminators */
 
#define REG_PEND   000400 /* ugh -- backward-compatibility hack */
 
#define REG_EXPECT   001000 /* report details on partial/limited matches */
 
#define REG_BOSONLY   002000 /* temporary kludge for BOS-only matches */
 
#define REG_DUMP   004000 /* none of your business :-) */
 
#define REG_FAKE   010000 /* none of your business :-) */
 
#define REG_PROGRESS   020000 /* none of your business :-) */
 
#define REG_NOTBOL   0001 /* BOS is not BOL */
 
#define REG_NOTEOL   0002 /* EOS is not EOL */
 
#define REG_STARTEND   0004 /* backward compatibility kludge */
 
#define REG_FTRACE   0010 /* none of your business */
 
#define REG_MTRACE   0020 /* none of your business */
 
#define REG_SMALL   0040 /* none of your business */
 
#define REG_OKAY   0 /* no errors detected */
 
#define REG_NOMATCH   1 /* failed to match */
 
#define REG_BADPAT   2 /* invalid regexp */
 
#define REG_ECOLLATE   3 /* invalid collating element */
 
#define REG_ECTYPE   4 /* invalid character class */
 
#define REG_EESCAPE   5 /* invalid escape \ sequence */
 
#define REG_ESUBREG   6 /* invalid backreference number */
 
#define REG_EBRACK   7 /* brackets [] not balanced */
 
#define REG_EPAREN   8 /* parentheses () not balanced */
 
#define REG_EBRACE   9 /* braces {} not balanced */
 
#define REG_BADBR   10 /* invalid repetition count(s) */
 
#define REG_ERANGE   11 /* invalid character range */
 
#define REG_ESPACE   12 /* out of memory */
 
#define REG_BADRPT   13 /* quantifier operand invalid */
 
#define REG_ASSERT   15 /* "can't happen" -- you found a bug */
 
#define REG_INVARG   16 /* invalid argument to regex function */
 
#define REG_MIXED   17 /* character widths of regex and string differ */
 
#define REG_BADOPT   18 /* invalid embedded option */
 
#define REG_ETOOBIG   19 /* regular expression is too complex */
 
#define REG_ECOLORS   20 /* too many colors */
 
#define REG_CANCEL   21 /* operation cancelled */
 
#define REG_ATOI   101 /* convert error-code name to number */
 
#define REG_ITOA   102 /* convert error-code number to name */
 
#define REG_PREFIX   (-1) /* identified a common prefix */
 
#define REG_EXACT   (-2) /* identified an exact match */
 

Typedefs

typedef long regoff_t
 

Functions

int pg_regcomp (regex_t *, const pg_wchar *, size_t, int, Oid)
 
int pg_regexec (regex_t *, const pg_wchar *, size_t, size_t, rm_detail_t *, size_t, regmatch_t[], int)
 
int pg_regprefix (regex_t *, pg_wchar **, size_t *)
 
void pg_regfree (regex_t *)
 
size_t pg_regerror (int, const regex_t *, char *, size_t)
 

Macro Definition Documentation

#define _REGEX_H_   /* never again */

Definition at line 2 of file regex.h.

#define REG_ADVF   000002 /* advanced features in EREs */

Definition at line 102 of file regex.h.

Referenced by lexescape(), lexstart(), next(), pg_regcomp(), and prefixes().

#define REG_ASSERT   15 /* "can't happen" -- you found a bug */
#define REG_ATOI   101 /* convert error-code name to number */

Definition at line 159 of file regex.h.

Referenced by pg_regerror().

#define REG_BADBR   10 /* invalid repetition count(s) */

Definition at line 147 of file regex.h.

Referenced by next(), parseqatom(), and scannum().

#define REG_BADOPT   18 /* invalid embedded option */

Definition at line 154 of file regex.h.

Referenced by prefixes().

#define REG_BADPAT   2 /* invalid regexp */

Definition at line 139 of file regex.h.

Referenced by prefixes().

#define REG_BADRPT   13 /* quantifier operand invalid */

Definition at line 150 of file regex.h.

Referenced by next(), parseqatom(), and prefixes().

#define REG_BASIC   000000 /* BREs (convenience) */

Definition at line 100 of file regex.h.

#define REG_BOSONLY   002000 /* temporary kludge for BOS-only matches */

Definition at line 114 of file regex.h.

Referenced by next().

#define REG_CANCEL   21 /* operation cancelled */

Definition at line 157 of file regex.h.

Referenced by cdissect(), copyins(), copyouts(), mergeins(), miss(), moveins(), moveouts(), newarc(), newstate(), and range().

#define REG_DUMP   004000 /* none of your business :-) */

Definition at line 115 of file regex.h.

Referenced by pg_regcomp().

#define REG_EBRACE   9 /* braces {} not balanced */

Definition at line 146 of file regex.h.

Referenced by next().

#define REG_EBRACK   7 /* brackets [] not balanced */

Definition at line 144 of file regex.h.

Referenced by next().

#define REG_ECOLLATE   3 /* invalid collating element */

Definition at line 140 of file regex.h.

Referenced by brackpart(), and element().

#define REG_ECOLORS   20 /* too many colors */

Definition at line 156 of file regex.h.

Referenced by newcolor().

#define REG_ECTYPE   4 /* invalid character class */

Definition at line 141 of file regex.h.

Referenced by brackpart(), and cclass().

#define REG_EESCAPE   5 /* invalid escape \ sequence */

Definition at line 142 of file regex.h.

Referenced by brenext(), lexdigits(), lexescape(), and next().

#define REG_EPAREN   8 /* parentheses () not balanced */

Definition at line 145 of file regex.h.

Referenced by parse(), and parseqatom().

#define REG_ERANGE   11 /* invalid character range */

Definition at line 148 of file regex.h.

Referenced by brackpart(), and range().

#define REG_ESUBREG   6 /* invalid backreference number */

Definition at line 143 of file regex.h.

Referenced by parseqatom().

#define REG_ETOOBIG   19 /* regular expression is too complex */
#define REG_EXACT   (-2) /* identified an exact match */

Definition at line 163 of file regex.h.

Referenced by findprefix(), pg_regprefix(), and regexp_fixed_prefix().

#define REG_EXPANDED   000040 /* expanded format, white space & comments */

Definition at line 108 of file regex.h.

Referenced by brenext(), lexstart(), next(), parse_re_flags(), pg_regcomp(), prefixes(), and skip().

#define REG_EXPECT   001000 /* report details on partial/limited matches */

Definition at line 113 of file regex.h.

Referenced by cfind(), find(), and pg_regexec().

#define REG_EXTENDED   000001 /* EREs */

Definition at line 101 of file regex.h.

Referenced by lexstart(), next(), parse_re_flags(), parseqatom(), pg_regcomp(), and prefixes().

#define REG_FAKE   010000 /* none of your business :-) */

Definition at line 116 of file regex.h.

Referenced by eclass().

#define REG_FTRACE   0010 /* none of your business */

Definition at line 127 of file regex.h.

Referenced by longest(), matchuntil(), and shortest().

#define REG_ICASE   000010 /* ignore case */
#define REG_INVARG   16 /* invalid argument to regex function */

Definition at line 152 of file regex.h.

Referenced by pg_regcomp(), pg_regexec(), and pg_regprefix().

#define REG_ITOA   102 /* convert error-code number to name */

Definition at line 160 of file regex.h.

Referenced by pg_regerror().

#define REG_MIXED   17 /* character widths of regex and string differ */

Definition at line 153 of file regex.h.

Referenced by pg_regexec(), and pg_regprefix().

#define REG_MTRACE   0020 /* none of your business */

Definition at line 128 of file regex.h.

#define REG_NEWLINE   000300 /* newlines are line terminators */

Definition at line 111 of file regex.h.

Referenced by lexstart(), parse_re_flags(), pg_regcomp(), and prefixes().

#define REG_NLANCH   000200 /* ^ matches after \n, $ before */

Definition at line 110 of file regex.h.

Referenced by parse_re_flags(), parseqatom(), pg_regcomp(), and prefixes().

#define REG_NLSTOP   000100 /* \n doesn't match . or [^ ] */

Definition at line 109 of file regex.h.

Referenced by cbracket(), parse_re_flags(), parseqatom(), pg_regcomp(), and prefixes().

#define REG_NOSPEC   REG_QUOTE /* historical synonym */

Definition at line 105 of file regex.h.

#define REG_NOSUB   000020 /* don't care about subexpressions */

Definition at line 107 of file regex.h.

Referenced by next(), NIAddAffix(), and pg_regexec().

#define REG_NOTBOL   0001 /* BOS is not BOL */

Definition at line 124 of file regex.h.

Referenced by longest(), matchuntil(), and shortest().

#define REG_NOTEOL   0002 /* EOS is not EOL */

Definition at line 125 of file regex.h.

Referenced by longest(), matchuntil(), and shortest().

#define REG_PEND   000400 /* ugh -- backward-compatibility hack */

Definition at line 112 of file regex.h.

#define REG_PREFIX   (-1) /* identified a common prefix */

Definition at line 162 of file regex.h.

Referenced by findprefix(), pg_regprefix(), and regexp_fixed_prefix().

#define REG_PROGRESS   020000 /* none of your business :-) */

Definition at line 117 of file regex.h.

Referenced by pg_regcomp().

#define REG_QUOTE   000004 /* no special characters, none */

Definition at line 104 of file regex.h.

Referenced by lexstart(), parse_re_flags(), pg_regcomp(), and prefixes().

#define REG_SMALL   0040 /* none of your business */

Definition at line 129 of file regex.h.

Referenced by newdfa().

#define REG_STARTEND   0004 /* backward compatibility kludge */

Definition at line 126 of file regex.h.

#define REG_UBACKREF   000001

Definition at line 60 of file regex.h.

Referenced by brenext(), lexescape(), and pg_regexec().

#define REG_UBBS   000100

Definition at line 66 of file regex.h.

Referenced by next().

#define REG_UBOUNDS   000004

Definition at line 62 of file regex.h.

Referenced by brenext(), and next().

#define REG_UBRACES   000010

Definition at line 63 of file regex.h.

Referenced by next().

#define REG_UBSALNUM   000020

Definition at line 64 of file regex.h.

Referenced by brenext(), and next().

#define REG_UEMPTYMATCH   004000

Definition at line 71 of file regex.h.

Referenced by analyze().

#define REG_UIMPOSSIBLE   010000

Definition at line 72 of file regex.h.

Referenced by analyze(), pg_regexec(), and pg_regprefix().

#define REG_ULOCALE   002000

Definition at line 70 of file regex.h.

Referenced by element(), lexescape(), and next().

#define REG_ULOOKAROUND   000002

Definition at line 61 of file regex.h.

Referenced by next().

#define REG_UNONPOSIX   000200

Definition at line 67 of file regex.h.

Referenced by brenext(), lexescape(), next(), prefixes(), and skip().

#define REG_UPBOTCH   000040

Definition at line 65 of file regex.h.

Referenced by parseqatom().

#define REG_USHORTEST   020000

Definition at line 73 of file regex.h.

Referenced by pg_regcomp().

#define REG_UUNPORT   001000

Definition at line 69 of file regex.h.

Referenced by brackpart(), and lexescape().

#define REG_UUNSPEC   000400

Definition at line 68 of file regex.h.

Referenced by brenext(), next(), and parsebranch().

Typedef Documentation

Definition at line 48 of file regex.h.

Function Documentation

int pg_regcomp ( regex_t ,
const pg_wchar ,
size_t  ,
int  ,
Oid   
)

Definition at line 316 of file regcomp.c.

References assert, casecmp(), vars::cflags, guts::cflags, cleanst(), vars::cm, guts::cmap, cmp(), CNOERR, COLORLESS, compact(), vars::cv, vars::cv2, debug, DISCARD, dumpnfa(), EOS, vars::err, nfa::final, subre::flags, freev(), GUTSMAGIC, i, guts::info, nfa::init, initcm(), vars::lacons, guts::lacons, LATYPE_IS_AHEAD, lexstart(), guts::magic, makesearch(), MALLOC, markst(), newcvec(), newline(), newnfa(), vars::nfa, nfanode(), nfatree(), vars::nlacons, guts::nlacons, vars::nlcolor, NOTE, vars::now, guts::nsub, vars::nsubexp, vars::nsubs, vars::ntree, guts::ntree, NULL, numst(), okcolors(), optimize(), optst(), parse(), pg_set_regex_collation(), PLAIN, vars::re, regex_t::re_collation, regex_t::re_csize, regex_t::re_fns, regex_t::re_guts, regex_t::re_info, regex_t::re_magic, regex_t::re_nsub, REG_ADVANCED, REG_ADVF, REG_DUMP, REG_ESPACE, REG_EXPANDED, REG_EXTENDED, REG_ICASE, REG_INVARG, REG_NEWLINE, REG_NLANCH, REG_NLSTOP, REG_PROGRESS, REG_QUOTE, REG_USHORTEST, REMAGIC, vars::savenow, vars::savestop, guts::search, SEE, SHORTER, vars::spaceused, specialcolors(), vars::stop, vars::sub10, subcolor(), subre::subno, vars::subs, vars::tree, guts::tree, vars::treechain, vars::treefree, VS, vars::wordchrs, and ZAPCNFA.

Referenced by NIAddAffix(), parse_ident_line(), RE_compile(), and RE_compile_and_cache().

321 {
322  struct vars var;
323  struct vars *v = &var;
324  struct guts *g;
325  int i;
326  size_t j;
327 
328 #ifdef REG_DEBUG
329  FILE *debug = (flags & REG_PROGRESS) ? stdout : (FILE *) NULL;
330 #else
331  FILE *debug = (FILE *) NULL;
332 #endif
333 
334 #define CNOERR() { if (ISERR()) return freev(v, v->err); }
335 
336  /* sanity checks */
337 
338  if (re == NULL || string == NULL)
339  return REG_INVARG;
340  if ((flags & REG_QUOTE) &&
341  (flags & (REG_ADVANCED | REG_EXPANDED | REG_NEWLINE)))
342  return REG_INVARG;
343  if (!(flags & REG_EXTENDED) && (flags & REG_ADVF))
344  return REG_INVARG;
345 
346  /* Initialize locale-dependent support */
347  pg_set_regex_collation(collation);
348 
349  /* initial setup (after which freev() is callable) */
350  v->re = re;
351  v->now = string;
352  v->stop = v->now + len;
353  v->savenow = v->savestop = NULL;
354  v->err = 0;
355  v->cflags = flags;
356  v->nsubexp = 0;
357  v->subs = v->sub10;
358  v->nsubs = 10;
359  for (j = 0; j < v->nsubs; j++)
360  v->subs[j] = NULL;
361  v->nfa = NULL;
362  v->cm = NULL;
363  v->nlcolor = COLORLESS;
364  v->wordchrs = NULL;
365  v->tree = NULL;
366  v->treechain = NULL;
367  v->treefree = NULL;
368  v->cv = NULL;
369  v->cv2 = NULL;
370  v->lacons = NULL;
371  v->nlacons = 0;
372  v->spaceused = 0;
373  re->re_magic = REMAGIC;
374  re->re_info = 0; /* bits get set during parse */
375  re->re_csize = sizeof(chr);
376  re->re_collation = collation;
377  re->re_guts = NULL;
378  re->re_fns = VS(&functions);
379 
380  /* more complex setup, malloced things */
381  re->re_guts = VS(MALLOC(sizeof(struct guts)));
382  if (re->re_guts == NULL)
383  return freev(v, REG_ESPACE);
384  g = (struct guts *) re->re_guts;
385  g->tree = NULL;
386  initcm(v, &g->cmap);
387  v->cm = &g->cmap;
388  g->lacons = NULL;
389  g->nlacons = 0;
390  ZAPCNFA(g->search);
391  v->nfa = newnfa(v, v->cm, (struct nfa *) NULL);
392  CNOERR();
393  /* set up a reasonably-sized transient cvec for getcvec usage */
394  v->cv = newcvec(100, 20);
395  if (v->cv == NULL)
396  return freev(v, REG_ESPACE);
397 
398  /* parsing */
399  lexstart(v); /* also handles prefixes */
400  if ((v->cflags & REG_NLSTOP) || (v->cflags & REG_NLANCH))
401  {
402  /* assign newline a unique color */
403  v->nlcolor = subcolor(v->cm, newline());
404  okcolors(v->nfa, v->cm);
405  }
406  CNOERR();
407  v->tree = parse(v, EOS, PLAIN, v->nfa->init, v->nfa->final);
408  assert(SEE(EOS)); /* even if error; ISERR() => SEE(EOS) */
409  CNOERR();
410  assert(v->tree != NULL);
411 
412  /* finish setup of nfa and its subre tree */
413  specialcolors(v->nfa);
414  CNOERR();
415 #ifdef REG_DEBUG
416  if (debug != NULL)
417  {
418  fprintf(debug, "\n\n\n========= RAW ==========\n");
419  dumpnfa(v->nfa, debug);
420  dumpst(v->tree, debug, 1);
421  }
422 #endif
423  optst(v, v->tree);
424  v->ntree = numst(v->tree, 1);
425  markst(v->tree);
426  cleanst(v);
427 #ifdef REG_DEBUG
428  if (debug != NULL)
429  {
430  fprintf(debug, "\n\n\n========= TREE FIXED ==========\n");
431  dumpst(v->tree, debug, 1);
432  }
433 #endif
434 
435  /* build compacted NFAs for tree and lacons */
436  re->re_info |= nfatree(v, v->tree, debug);
437  CNOERR();
438  assert(v->nlacons == 0 || v->lacons != NULL);
439  for (i = 1; i < v->nlacons; i++)
440  {
441  struct subre *lasub = &v->lacons[i];
442 
443 #ifdef REG_DEBUG
444  if (debug != NULL)
445  fprintf(debug, "\n\n\n========= LA%d ==========\n", i);
446 #endif
447 
448  /* Prepend .* to pattern if it's a lookbehind LACON */
449  nfanode(v, lasub, !LATYPE_IS_AHEAD(lasub->subno), debug);
450  }
451  CNOERR();
452  if (v->tree->flags & SHORTER)
454 
455  /* build compacted NFAs for tree, lacons, fast search */
456 #ifdef REG_DEBUG
457  if (debug != NULL)
458  fprintf(debug, "\n\n\n========= SEARCH ==========\n");
459 #endif
460  /* can sacrifice main NFA now, so use it as work area */
461  (DISCARD) optimize(v->nfa, debug);
462  CNOERR();
463  makesearch(v, v->nfa);
464  CNOERR();
465  compact(v->nfa, &g->search);
466  CNOERR();
467 
468  /* looks okay, package it up */
469  re->re_nsub = v->nsubexp;
470  v->re = NULL; /* freev no longer frees re */
471  g->magic = GUTSMAGIC;
472  g->cflags = v->cflags;
473  g->info = re->re_info;
474  g->nsub = re->re_nsub;
475  g->tree = v->tree;
476  v->tree = NULL;
477  g->ntree = v->ntree;
478  g->compare = (v->cflags & REG_ICASE) ? casecmp : cmp;
479  g->lacons = v->lacons;
480  v->lacons = NULL;
481  g->nlacons = v->nlacons;
482 
483 #ifdef REG_DEBUG
484  if (flags & REG_DUMP)
485  dump(re, stdout);
486 #endif
487 
488  assert(v->err == 0);
489  return freev(v, 0);
490 }
#define REG_USHORTEST
Definition: regex.h:73
static chr newline(void)
struct subre * treefree
Definition: regcomp.c:249
static void optst(struct vars *, struct subre *)
Definition: regcomp.c:1782
#define REG_NLSTOP
Definition: regex.h:109
static void okcolors(struct nfa *, struct colormap *)
static int numst(struct subre *, int)
Definition: regcomp.c:1798
static long optimize(struct nfa *, FILE *)
int ntree
Definition: regcomp.c:250
struct subre * lacons
Definition: regguts.h:474
struct subre * tree
Definition: regguts.h:469
struct state * final
Definition: regguts.h:319
static long nfatree(struct vars *, struct subre *, FILE *)
Definition: regcomp.c:1866
struct subre * treechain
Definition: regcomp.c:248
#define REG_ESPACE
Definition: regex.h:149
int subno
Definition: regguts.h:427
static int freev(struct vars *, int)
Definition: regcomp.c:533
static void specialcolors(struct nfa *)
#define REG_QUOTE
Definition: regex.h:104
const chr * savenow
Definition: regcomp.c:231
static struct cvec * newcvec(int, int)
color nlcolor
Definition: regcomp.c:245
int nlacons
Definition: regguts.h:475
#define REMAGIC
Definition: regguts.h:96
static color subcolor(struct colormap *, chr)
Definition: regguts.h:315
#define REG_ICASE
Definition: regex.h:106
static void makesearch(struct vars *, struct nfa *)
Definition: regcomp.c:562
int nsubexp
Definition: regcomp.c:239
#define MALLOC(n)
Definition: regcustom.h:53
#define REG_PROGRESS
Definition: regex.h:117
struct cnfa search
Definition: regguts.h:470
pg_wchar chr
Definition: regcustom.h:59
struct colormap * cm
Definition: regcomp.c:244
#define ZAPCNFA(cnfa)
Definition: regguts.h:371
static void dumpnfa(struct nfa *, FILE *)
bool debug
Definition: pg_standby.c:46
regex_t * re
Definition: regcomp.c:228
int nlacons
Definition: regcomp.c:254
int magic
Definition: regguts.h:464
#define REG_INVARG
Definition: regex.h:152
static void markst(struct subre *)
Definition: regcomp.c:1832
char flags
Definition: regguts.h:411
struct subre ** subs
Definition: regcomp.c:240
static struct nfa * newnfa(struct vars *, struct colormap *, struct nfa *)
char string[11]
Definition: preproc-type.c:46
struct colormap cmap
Definition: regguts.h:472
#define assert(TEST)
Definition: imath.c:37
#define LATYPE_IS_AHEAD(la)
Definition: regguts.h:104
#define REG_NEWLINE
Definition: regex.h:111
#define REG_ADVANCED
Definition: regex.h:103
#define SEE(t)
Definition: regcomp.c:261
size_t nsubs
Definition: regcomp.c:241
int err
Definition: regcomp.c:233
static long nfanode(struct vars *, struct subre *, int, FILE *)
Definition: regcomp.c:1886
struct nfa * nfa
Definition: regcomp.c:243
#define REG_EXTENDED
Definition: regex.h:101
struct cvec * cv
Definition: regcomp.c:251
struct cvec * cv2
Definition: regcomp.c:252
#define REG_ADVF
Definition: regex.h:102
const chr * savestop
Definition: regcomp.c:232
#define PLAIN
Definition: regcomp.c:278
struct state * wordchrs
Definition: regcomp.c:246
#define REG_DUMP
Definition: regex.h:115
Definition: regguts.h:462
int ntree
Definition: regguts.h:471
#define EOS
Definition: regcomp.c:277
#define NULL
Definition: c.h:226
#define GUTSMAGIC
Definition: regguts.h:465
void pg_set_regex_collation(Oid collation)
const chr * now
Definition: regcomp.c:229
#define REG_NLANCH
Definition: regex.h:110
static const struct fns functions
Definition: regcomp.c:301
#define VS(x)
Definition: regguts.h:61
static int cmp(const chr *, const chr *, size_t)
static void initcm(struct vars *, struct colormap *)
long info
Definition: regguts.h:467
Definition: regguts.h:408
#define NOTE(b)
Definition: regcomp.c:272
#define CNOERR()
const chr * stop
Definition: regcomp.c:230
int cflags
Definition: regguts.h:466
struct state * init
Definition: regguts.h:318
int cflags
Definition: regcomp.c:234
size_t nsub
Definition: regguts.h:468
static void lexstart(struct vars *)
static int casecmp(const chr *, const chr *, size_t)
#define SHORTER
Definition: regguts.h:413
int i
struct subre * sub10[10]
Definition: regcomp.c:242
#define COLORLESS
Definition: regguts.h:137
#define REG_EXPANDED
Definition: regex.h:108
#define DISCARD
Definition: regguts.h:58
struct subre * tree
Definition: regcomp.c:247
static void cleanst(struct vars *)
Definition: regcomp.c:1847
size_t spaceused
Definition: regcomp.c:256
struct subre * lacons
Definition: regcomp.c:253
Definition: regcomp.c:226
static void compact(struct nfa *, struct cnfa *)
static struct subre * parse(struct vars *, int, int, struct state *, struct state *)
Definition: regcomp.c:651
size_t pg_regerror ( int  ,
const regex_t ,
char *  ,
size_t   
)

Definition at line 60 of file regerror.c.

References rerr::code, rerr::explain, rerr::name, REG_ATOI, REG_ITOA, rerrs, and unk.

Referenced by check_ident_usermap(), NIAddAffix(), parse_ident_line(), RE_compile(), RE_compile_and_cache(), RE_wchar_execute(), regexp_fixed_prefix(), and replace_text_regexp().

64 {
65  const struct rerr *r;
66  const char *msg;
67  char convbuf[sizeof(unk) + 50]; /* 50 = plenty for int */
68  size_t len;
69  int icode;
70 
71  switch (errcode)
72  {
73  case REG_ATOI: /* convert name to number */
74  for (r = rerrs; r->code >= 0; r++)
75  if (strcmp(r->name, errbuf) == 0)
76  break;
77  sprintf(convbuf, "%d", r->code); /* -1 for unknown */
78  msg = convbuf;
79  break;
80  case REG_ITOA: /* convert number to name */
81  icode = atoi(errbuf); /* not our problem if this fails */
82  for (r = rerrs; r->code >= 0; r++)
83  if (r->code == icode)
84  break;
85  if (r->code >= 0)
86  msg = r->name;
87  else
88  { /* unknown; tell him the number */
89  sprintf(convbuf, "REG_%u", (unsigned) icode);
90  msg = convbuf;
91  }
92  break;
93  default: /* a real, normal error code */
94  for (r = rerrs; r->code >= 0; r++)
95  if (r->code == errcode)
96  break;
97  if (r->code >= 0)
98  msg = r->explain;
99  else
100  { /* unknown; say so */
101  sprintf(convbuf, unk, errcode);
102  msg = convbuf;
103  }
104  break;
105  }
106 
107  len = strlen(msg) + 1; /* space needed, including NUL */
108  if (errbuf_size > 0)
109  {
110  if (errbuf_size > len)
111  strcpy(errbuf, msg);
112  else
113  { /* truncate to fit */
114  memcpy(errbuf, msg, errbuf_size - 1);
115  errbuf[errbuf_size - 1] = '\0';
116  }
117  }
118 
119  return len;
120 }
int code
Definition: regerror.c:42
static const char unk[]
Definition: regerror.c:37
int errcode(int sqlerrcode)
Definition: elog.c:575
const char * name
Definition: regerror.c:43
static const struct rerr rerrs[]
#define REG_ATOI
Definition: regex.h:159
#define REG_ITOA
Definition: regex.h:160
const char * explain
Definition: regerror.c:44
Definition: regerror.c:40
int pg_regexec ( regex_t ,
const pg_wchar ,
size_t  ,
size_t  ,
rm_detail_t ,
size_t  ,
regmatch_t  [],
int   
)

Definition at line 172 of file regexec.c.

References assert, cfind(), guts::cflags, cleanup(), guts::cmap, subre::cnfa, vars::details, vars::eflags, vars::err, find(), FREE, freedfa(), vars::g, i, guts::info, vars::ladfas, vars::lblastcp, vars::lblastcss, LOCALDFAS, LOCALMAT, MALLOC, guts::nlacons, vars::nmatch, guts::nsub, guts::ntree, NULL, pg_set_regex_collation(), vars::pmatch, vars::re, regex_t::re_collation, regex_t::re_csize, regex_t::re_guts, regex_t::re_magic, REG_ESPACE, REG_EXPECT, REG_INVARG, REG_MIXED, REG_NOMATCH, REG_NOSUB, REG_OKAY, REG_UBACKREF, REG_UIMPOSSIBLE, REMAGIC, vars::search_start, vars::start, vars::stop, vars::subdfas, guts::tree, VS, and zapallsubs().

Referenced by check_ident_usermap(), CheckAffix(), RE_wchar_execute(), and replace_text_regexp().

180 {
181  struct vars var;
182  register struct vars *v = &var;
183  int st;
184  size_t n;
185  size_t i;
186  int backref;
187 
188 #define LOCALMAT 20
189  regmatch_t mat[LOCALMAT];
190 
191 #define LOCALDFAS 40
192  struct dfa *subdfas[LOCALDFAS];
193 
194  /* sanity checks */
195  if (re == NULL || string == NULL || re->re_magic != REMAGIC)
196  return REG_INVARG;
197  if (re->re_csize != sizeof(chr))
198  return REG_MIXED;
199 
200  /* Initialize locale-dependent support */
201  pg_set_regex_collation(re->re_collation);
202 
203  /* setup */
204  v->re = re;
205  v->g = (struct guts *) re->re_guts;
206  if ((v->g->cflags & REG_EXPECT) && details == NULL)
207  return REG_INVARG;
208  if (v->g->info & REG_UIMPOSSIBLE)
209  return REG_NOMATCH;
210  backref = (v->g->info & REG_UBACKREF) ? 1 : 0;
211  v->eflags = flags;
212  if (v->g->cflags & REG_NOSUB)
213  nmatch = 0; /* override client */
214  v->nmatch = nmatch;
215  if (backref)
216  {
217  /* need work area */
218  if (v->g->nsub + 1 <= LOCALMAT)
219  v->pmatch = mat;
220  else
221  v->pmatch = (regmatch_t *) MALLOC((v->g->nsub + 1) *
222  sizeof(regmatch_t));
223  if (v->pmatch == NULL)
224  return REG_ESPACE;
225  v->nmatch = v->g->nsub + 1;
226  }
227  else
228  v->pmatch = pmatch;
229  v->details = details;
230  v->start = (chr *) string;
231  v->search_start = (chr *) string + search_start;
232  v->stop = (chr *) string + len;
233  v->err = 0;
234  v->subdfas = NULL;
235  v->ladfas = NULL;
236  v->lblastcss = NULL;
237  v->lblastcp = NULL;
238  /* below this point, "goto cleanup" will behave sanely */
239 
240  assert(v->g->ntree >= 0);
241  n = (size_t) v->g->ntree;
242  if (n <= LOCALDFAS)
243  v->subdfas = subdfas;
244  else
245  {
246  v->subdfas = (struct dfa **) MALLOC(n * sizeof(struct dfa *));
247  if (v->subdfas == NULL)
248  {
249  st = REG_ESPACE;
250  goto cleanup;
251  }
252  }
253  for (i = 0; i < n; i++)
254  v->subdfas[i] = NULL;
255 
256  assert(v->g->nlacons >= 0);
257  n = (size_t) v->g->nlacons;
258  if (n > 0)
259  {
260  v->ladfas = (struct dfa **) MALLOC(n * sizeof(struct dfa *));
261  if (v->ladfas == NULL)
262  {
263  st = REG_ESPACE;
264  goto cleanup;
265  }
266  for (i = 0; i < n; i++)
267  v->ladfas[i] = NULL;
268  v->lblastcss = (struct sset **) MALLOC(n * sizeof(struct sset *));
269  v->lblastcp = (chr **) MALLOC(n * sizeof(chr *));
270  if (v->lblastcss == NULL || v->lblastcp == NULL)
271  {
272  st = REG_ESPACE;
273  goto cleanup;
274  }
275  for (i = 0; i < n; i++)
276  {
277  v->lblastcss[i] = NULL;
278  v->lblastcp[i] = NULL;
279  }
280  }
281 
282  /* do it */
283  assert(v->g->tree != NULL);
284  if (backref)
285  st = cfind(v, &v->g->tree->cnfa, &v->g->cmap);
286  else
287  st = find(v, &v->g->tree->cnfa, &v->g->cmap);
288 
289  /* copy (portion of) match vector over if necessary */
290  if (st == REG_OKAY && v->pmatch != pmatch && nmatch > 0)
291  {
292  zapallsubs(pmatch, nmatch);
293  n = (nmatch < v->nmatch) ? nmatch : v->nmatch;
294  memcpy(VS(pmatch), VS(v->pmatch), n * sizeof(regmatch_t));
295  }
296 
297  /* clean up */
298 cleanup:
299  if (v->pmatch != pmatch && v->pmatch != mat)
300  FREE(v->pmatch);
301  if (v->subdfas != NULL)
302  {
303  n = (size_t) v->g->ntree;
304  for (i = 0; i < n; i++)
305  {
306  if (v->subdfas[i] != NULL)
307  freedfa(v->subdfas[i]);
308  }
309  if (v->subdfas != subdfas)
310  FREE(v->subdfas);
311  }
312  if (v->ladfas != NULL)
313  {
314  n = (size_t) v->g->nlacons;
315  for (i = 0; i < n; i++)
316  {
317  if (v->ladfas[i] != NULL)
318  freedfa(v->ladfas[i]);
319  }
320  FREE(v->ladfas);
321  }
322  if (v->lblastcss != NULL)
323  FREE(v->lblastcss);
324  if (v->lblastcp != NULL)
325  FREE(v->lblastcp);
326 
327  return st;
328 }
#define REG_UIMPOSSIBLE
Definition: regex.h:72
struct dfa ** ladfas
Definition: regexec.c:116
static int find(struct vars *, struct cnfa *, struct colormap *)
Definition: regexec.c:375
#define REG_EXPECT
Definition: regex.h:113
struct subre * tree
Definition: regguts.h:469
#define REG_ESPACE
Definition: regex.h:149
int nlacons
Definition: regguts.h:475
#define FREE(p)
Definition: regcustom.h:54
#define REMAGIC
Definition: regguts.h:96
rm_detail_t * details
Definition: regexec.c:110
chr * start
Definition: regexec.c:111
#define MALLOC(n)
Definition: regcustom.h:53
Definition: regexec.c:63
struct sset ** lblastcss
Definition: regexec.c:117
chr * search_start
Definition: regexec.c:112
pg_wchar chr
Definition: regcustom.h:59
#define REG_OKAY
Definition: regex.h:137
regex_t * re
Definition: regcomp.c:228
#define REG_INVARG
Definition: regex.h:152
struct colormap cmap
Definition: regguts.h:472
#define assert(TEST)
Definition: imath.c:37
struct guts * g
Definition: regexec.c:106
int err
Definition: regcomp.c:233
#define LOCALMAT
int eflags
Definition: regexec.c:107
static void cleanup(void)
Definition: bootstrap.c:848
Definition: regguts.h:462
int ntree
Definition: regguts.h:471
#define REG_UBACKREF
Definition: regex.h:60
#define LOCALDFAS
struct dfa ** subdfas
Definition: regexec.c:115
#define NULL
Definition: c.h:226
void pg_set_regex_collation(Oid collation)
regmatch_t * pmatch
Definition: regexec.c:109
#define REG_NOSUB
Definition: regex.h:107
static void freedfa(struct dfa *)
#define VS(x)
Definition: regguts.h:61
long info
Definition: regguts.h:467
struct cnfa cnfa
Definition: regguts.h:435
size_t nmatch
Definition: regexec.c:108
static void zapallsubs(regmatch_t *, size_t)
Definition: regexec.c:619
const chr * stop
Definition: regcomp.c:230
int cflags
Definition: regguts.h:466
Definition: regexec.c:45
size_t nsub
Definition: regguts.h:468
int i
#define REG_NOMATCH
Definition: regex.h:138
chr ** lblastcp
Definition: regexec.c:118
#define REG_MIXED
Definition: regex.h:153
Definition: regcomp.c:226
static int cfind(struct vars *, struct cnfa *, struct colormap *)
Definition: regexec.c:466
void pg_regfree ( regex_t )

Definition at line 49 of file regfree.c.

References NULL, and regex_t::re_fns.

Referenced by createTrgmNFA(), load_ident(), and RE_compile_and_cache().

50 {
51  if (re == NULL)
52  return;
53  (*((struct fns *) re->re_fns)->free) (re);
54 }
Definition: regguts.h:445
#define NULL
Definition: c.h:226
int pg_regprefix ( regex_t ,
pg_wchar **  ,
size_t *   
)

Definition at line 46 of file regprefix.c.

References assert, guts::cmap, subre::cnfa, findprefix(), FREE, guts::info, MALLOC, cnfa::nstates, NULL, pg_set_regex_collation(), regex_t::re_collation, regex_t::re_csize, regex_t::re_guts, regex_t::re_magic, REG_ESPACE, REG_EXACT, REG_INVARG, REG_MIXED, REG_NOMATCH, REG_PREFIX, REG_UIMPOSSIBLE, REMAGIC, and guts::tree.

Referenced by regexp_fixed_prefix().

49 {
50  struct guts *g;
51  struct cnfa *cnfa;
52  int st;
53 
54  /* sanity checks */
55  if (string == NULL || slength == NULL)
56  return REG_INVARG;
57  *string = NULL; /* initialize for failure cases */
58  *slength = 0;
59  if (re == NULL || re->re_magic != REMAGIC)
60  return REG_INVARG;
61  if (re->re_csize != sizeof(chr))
62  return REG_MIXED;
63 
64  /* Initialize locale-dependent support */
65  pg_set_regex_collation(re->re_collation);
66 
67  /* setup */
68  g = (struct guts *) re->re_guts;
69  if (g->info & REG_UIMPOSSIBLE)
70  return REG_NOMATCH;
71 
72  /*
73  * This implementation considers only the search NFA for the topmost regex
74  * tree node. Therefore, constraints such as backrefs are not fully
75  * applied, which is allowed per the function's API spec.
76  */
77  assert(g->tree != NULL);
78  cnfa = &g->tree->cnfa;
79 
80  /*
81  * Since a correct NFA should never contain any exit-free loops, it should
82  * not be possible for our traversal to return to a previously visited NFA
83  * state. Hence we need at most nstates chrs in the output string.
84  */
85  *string = (chr *) MALLOC(cnfa->nstates * sizeof(chr));
86  if (*string == NULL)
87  return REG_ESPACE;
88 
89  /* do it */
90  st = findprefix(cnfa, &g->cmap, *string, slength);
91 
92  assert(*slength <= cnfa->nstates);
93 
94  /* clean up */
95  if (st != REG_PREFIX && st != REG_EXACT)
96  {
97  FREE(*string);
98  *string = NULL;
99  *slength = 0;
100  }
101 
102  return st;
103 }
#define REG_UIMPOSSIBLE
Definition: regex.h:72
struct subre * tree
Definition: regguts.h:469
#define REG_ESPACE
Definition: regex.h:149
Definition: regguts.h:354
#define FREE(p)
Definition: regcustom.h:54
#define REMAGIC
Definition: regguts.h:96
#define REG_PREFIX
Definition: regex.h:162
#define MALLOC(n)
Definition: regcustom.h:53
int nstates
Definition: regguts.h:356
pg_wchar chr
Definition: regcustom.h:59
#define REG_INVARG
Definition: regex.h:152
static int findprefix(struct cnfa *cnfa, struct colormap *cm, chr *string, size_t *slength)
Definition: regprefix.c:112
struct colormap cmap
Definition: regguts.h:472
#define assert(TEST)
Definition: imath.c:37
Definition: regguts.h:462
#define NULL
Definition: c.h:226
void pg_set_regex_collation(Oid collation)
long info
Definition: regguts.h:467
struct cnfa cnfa
Definition: regguts.h:435
#define REG_EXACT
Definition: regex.h:163
#define REG_NOMATCH
Definition: regex.h:138
#define REG_MIXED
Definition: regex.h:153