Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  2. /*
  3.  * the outer shell of regexec()
  4.  *
  5.  * This file includes engine.c *twice*, after muchos fiddling with the
  6.  * macros that code uses.  This lets the same code operate on two different
  7.  * representations for state sets.
  8.  */
  9. #include <sys/types.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <limits.h>
  14. #include <ctype.h>
  15. #include <regex.h>
  16.  
  17. #include "utils.h"
  18. #include "regex2.h"
  19.  
  20. static int nope = 0;            /* for use in asserts; shuts lint up */
  21.  
  22. /* macros for manipulating states, small version */
  23. #define states  long
  24. #define states1 states          /* for later use in regexec() decision */
  25. #define CLEAR(v)        ((v) = 0)
  26. #define SET0(v, n)      ((v) &= ~(1 << (n)))
  27. #define SET1(v, n)      ((v) |= 1 << (n))
  28. #define ISSET(v, n)     ((v) & (1 << (n)))
  29. #define ASSIGN(d, s)    ((d) = (s))
  30. #define EQ(a, b)        ((a) == (b))
  31. #define STATEVARS       int dummy       /* dummy version */
  32. #define STATESETUP(m, n)        /* nothing */
  33. #define STATETEARDOWN(m)        /* nothing */
  34. #define SETUP(v)        ((v) = 0)
  35. #define onestate        int
  36. #define INIT(o, n)      ((o) = (unsigned)1 << (n))
  37. #define INC(o)  ((o) <<= 1)
  38. #define ISSTATEIN(v, o) ((v) & (o))
  39. /* some abbreviations; note that some of these know variable names! */
  40. /* do "if I'm here, I can also be there" etc without branches */
  41. #define FWD(dst, src, n)        ((dst) |= ((unsigned)(src)&(here)) << (n))
  42. #define BACK(dst, src, n)       ((dst) |= ((unsigned)(src)&(here)) >> (n))
  43. #define ISSETBACK(v, n) ((v) & ((unsigned)here >> (n)))
  44. /* function names */
  45. #define SNAMES                  /* engine.c looks after details */
  46.  
  47. #include "engine.c"
  48.  
  49. /* now undo things */
  50. #undef  states
  51. #undef  CLEAR
  52. #undef  SET0
  53. #undef  SET1
  54. #undef  ISSET
  55. #undef  ASSIGN
  56. #undef  EQ
  57. #undef  STATEVARS
  58. #undef  STATESETUP
  59. #undef  STATETEARDOWN
  60. #undef  SETUP
  61. #undef  onestate
  62. #undef  INIT
  63. #undef  INC
  64. #undef  ISSTATEIN
  65. #undef  FWD
  66. #undef  BACK
  67. #undef  ISSETBACK
  68. #undef  SNAMES
  69.  
  70. /* macros for manipulating states, large version */
  71. #define states  char *
  72. #define CLEAR(v)        memset(v, 0, m->g->nstates)
  73. #define SET0(v, n)      ((v)[n] = 0)
  74. #define SET1(v, n)      ((v)[n] = 1)
  75. #define ISSET(v, n)     ((v)[n])
  76. #define ASSIGN(d, s)    memcpy(d, s, m->g->nstates)
  77. #define EQ(a, b)        (memcmp(a, b, m->g->nstates) == 0)
  78. #define STATEVARS       int vn; char *space
  79. #define STATESETUP(m, nv)       { (m)->space = malloc((nv)*(m)->g->nstates); \
  80.                                 if ((m)->space == NULL) return(REG_ESPACE); \
  81.                                 (m)->vn = 0; }
  82. #define STATETEARDOWN(m)        { free((m)->space); }
  83. #define SETUP(v)        ((v) = &m->space[m->vn++ * m->g->nstates])
  84. #define onestate        int
  85. #define INIT(o, n)      ((o) = (n))
  86. #define INC(o)  ((o)++)
  87. #define ISSTATEIN(v, o) ((v)[o])
  88. /* some abbreviations; note that some of these know variable names! */
  89. /* do "if I'm here, I can also be there" etc without branches */
  90. #define FWD(dst, src, n)        ((dst)[here+(n)] |= (src)[here])
  91. #define BACK(dst, src, n)       ((dst)[here-(n)] |= (src)[here])
  92. #define ISSETBACK(v, n) ((v)[here - (n)])
  93. /* function names */
  94. #define LNAMES                  /* flag */
  95.  
  96. #include "engine.c"
  97.  
  98. /*
  99.  - regexec - interface for matching
  100.  = extern int regexec(const regex_t *, const char *, size_t, \
  101.  =                                      regmatch_t [], int);
  102.  = #define      REG_NOTBOL      00001
  103.  = #define      REG_NOTEOL      00002
  104.  = #define      REG_STARTEND    00004
  105.  = #define      REG_TRACE       00400   // tracing of execution
  106.  = #define      REG_LARGE       01000   // force large representation
  107.  = #define      REG_BACKR       02000   // force use of backref code
  108.  *
  109.  * We put this here so we can exploit knowledge of the state representation
  110.  * when choosing which matcher to call.  Also, by this point the matchers
  111.  * have been prototyped.
  112.  */
  113. int                             /* 0 success, REG_NOMATCH failure */
  114. regexec(preg, string, nmatch, pmatch, eflags)
  115. const regex_t *preg;
  116. const char *string;
  117. size_t nmatch;
  118. regmatch_t pmatch[];
  119. int eflags;
  120. {
  121.         register struct re_guts *g = preg->re_g;
  122. #ifdef REDEBUG
  123. #       define  GOODFLAGS(f)    (f)
  124. #else
  125. #       define  GOODFLAGS(f)    ((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND))
  126. #endif
  127.  
  128.         if (preg->re_magic != MAGIC1 || g->magic != MAGIC2)
  129.                 return(REG_BADPAT);
  130.         assert(!(g->iflags&BAD));
  131.         if (g->iflags&BAD)              /* backstop for no-debug case */
  132.                 return(REG_BADPAT);
  133.         eflags = GOODFLAGS(eflags);
  134.  
  135.         if (g->nstates <= CHAR_BIT*sizeof(states1) && !(eflags&REG_LARGE))
  136.                 return(smatcher(g, (char *)string, nmatch, pmatch, eflags));
  137.         else
  138.                 return(lmatcher(g, (char *)string, nmatch, pmatch, eflags));
  139. }
  140.