regexec.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*-
  2. * This code is derived from OpenBSD's libc/regex, original license follows:
  3. *
  4. * Copyright (c) 1992, 1993, 1994 Henry Spencer.
  5. * Copyright (c) 1992, 1993, 1994
  6. * The Regents of the University of California. All rights reserved.
  7. *
  8. * This code is derived from software contributed to Berkeley by
  9. * Henry Spencer.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * 3. Neither the name of the University nor the names of its contributors
  20. * may be used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  29. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  30. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33. * SUCH DAMAGE.
  34. *
  35. * @(#)regexec.c 8.3 (Berkeley) 3/20/94
  36. */
  37. /*
  38. * the outer shell of llvm_regexec()
  39. *
  40. * This file includes engine.inc *twice*, after muchos fiddling with the
  41. * macros that code uses. This lets the same code operate on two different
  42. * representations for state sets.
  43. */
  44. #include <sys/types.h>
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47. #include <string.h>
  48. #include <limits.h>
  49. #include <ctype.h>
  50. #include "regex_impl.h"
  51. #include "regutils.h"
  52. #include "regex2.h"
  53. /* macros for manipulating states, small version */
  54. /* FIXME: 'states' is assumed as 'long' on small version. */
  55. #define states1 long /* for later use in llvm_regexec() decision */
  56. #define states states1
  57. #define CLEAR(v) ((v) = 0)
  58. #define SET0(v, n) ((v) &= ~((unsigned long)1 << (n)))
  59. #define SET1(v, n) ((v) |= (unsigned long)1 << (n))
  60. #define ISSET(v, n) (((v) & ((unsigned long)1 << (n))) != 0)
  61. #define ASSIGN(d, s) ((d) = (s))
  62. #define EQ(a, b) ((a) == (b))
  63. #define STATEVARS long dummy /* dummy version */
  64. #define STATESETUP(m, n) /* nothing */
  65. #define STATETEARDOWN(m) /* nothing */
  66. #define SETUP(v) ((v) = 0)
  67. #define onestate long
  68. #define INIT(o, n) ((o) = (unsigned long)1 << (n))
  69. #define INC(o) ((o) = (unsigned long)(o) << 1)
  70. #define ISSTATEIN(v, o) (((v) & (o)) != 0)
  71. /* some abbreviations; note that some of these know variable names! */
  72. /* do "if I'm here, I can also be there" etc without branches */
  73. #define FWD(dst, src, n) ((dst) |= ((unsigned long)(src)&(here)) << (n))
  74. #define BACK(dst, src, n) ((dst) |= ((unsigned long)(src)&(here)) >> (n))
  75. #define ISSETBACK(v, n) (((v) & ((unsigned long)here >> (n))) != 0)
  76. /* function names */
  77. #define SNAMES /* engine.inc looks after details */
  78. #include "regengine.inc"
  79. /* now undo things */
  80. #undef states
  81. #undef CLEAR
  82. #undef SET0
  83. #undef SET1
  84. #undef ISSET
  85. #undef ASSIGN
  86. #undef EQ
  87. #undef STATEVARS
  88. #undef STATESETUP
  89. #undef STATETEARDOWN
  90. #undef SETUP
  91. #undef onestate
  92. #undef INIT
  93. #undef INC
  94. #undef ISSTATEIN
  95. #undef FWD
  96. #undef BACK
  97. #undef ISSETBACK
  98. #undef SNAMES
  99. /* macros for manipulating states, large version */
  100. #define states char *
  101. #define CLEAR(v) memset(v, 0, m->g->nstates)
  102. #define SET0(v, n) ((v)[n] = 0)
  103. #define SET1(v, n) ((v)[n] = 1)
  104. #define ISSET(v, n) ((v)[n])
  105. #define ASSIGN(d, s) memmove(d, s, m->g->nstates)
  106. #define EQ(a, b) (memcmp(a, b, m->g->nstates) == 0)
  107. #define STATEVARS long vn; char *space
  108. #define STATESETUP(m, nv) { (m)->space = malloc((nv)*(m)->g->nstates); \
  109. if ((m)->space == NULL) return(REG_ESPACE); \
  110. (m)->vn = 0; }
  111. #define STATETEARDOWN(m) { free((m)->space); }
  112. #define SETUP(v) ((v) = &m->space[m->vn++ * m->g->nstates])
  113. #define onestate long
  114. #define INIT(o, n) ((o) = (n))
  115. #define INC(o) ((o)++)
  116. #define ISSTATEIN(v, o) ((v)[o])
  117. /* some abbreviations; note that some of these know variable names! */
  118. /* do "if I'm here, I can also be there" etc without branches */
  119. #define FWD(dst, src, n) ((dst)[here+(n)] |= (src)[here])
  120. #define BACK(dst, src, n) ((dst)[here-(n)] |= (src)[here])
  121. #define ISSETBACK(v, n) ((v)[here - (n)])
  122. /* function names */
  123. #define LNAMES /* flag */
  124. #include "regengine.inc"
  125. /*
  126. - llvm_regexec - interface for matching
  127. *
  128. * We put this here so we can exploit knowledge of the state representation
  129. * when choosing which matcher to call. Also, by this point the matchers
  130. * have been prototyped.
  131. */
  132. int /* 0 success, REG_NOMATCH failure */
  133. llvm_regexec(const llvm_regex_t *preg, const char *string, size_t nmatch,
  134. llvm_regmatch_t pmatch[], int eflags)
  135. {
  136. struct re_guts *g = preg->re_g;
  137. #ifdef REDEBUG
  138. # define GOODFLAGS(f) (f)
  139. #else
  140. # define GOODFLAGS(f) ((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND))
  141. #endif
  142. if (preg->re_magic != MAGIC1 || g->magic != MAGIC2)
  143. return(REG_BADPAT);
  144. assert(!(g->iflags&REGEX_BAD));
  145. if (g->iflags&REGEX_BAD) /* backstop for no-debug case */
  146. return(REG_BADPAT);
  147. eflags = GOODFLAGS(eflags);
  148. if (g->nstates <= (long)(CHAR_BIT*sizeof(states1)) && !(eflags&REG_LARGE))
  149. return(smatcher(g, string, nmatch, pmatch, eflags));
  150. else
  151. return(lmatcher(g, string, nmatch, pmatch, eflags));
  152. }