PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/bionic/libc/upstream-netbsd/libc/regex/regexec.c

https://gitlab.com/brian0218/rk3066_r-box_android4.2.2_sdk
C | 234 lines | 115 code | 18 blank | 101 comment | 18 complexity | d53f2444c0099be7aaad6bdc6ef13a24 MD5 | raw file
  1. /* $NetBSD: regexec.c,v 1.22 2012/03/13 21:13:43 christos Exp $ */
  2. /*-
  3. * Copyright (c) 1992, 1993, 1994
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * This code is derived from software contributed to Berkeley by
  7. * Henry Spencer.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * @(#)regexec.c 8.3 (Berkeley) 3/20/94
  34. */
  35. /*-
  36. * Copyright (c) 1992, 1993, 1994 Henry Spencer.
  37. *
  38. * This code is derived from software contributed to Berkeley by
  39. * Henry Spencer.
  40. *
  41. * Redistribution and use in source and binary forms, with or without
  42. * modification, are permitted provided that the following conditions
  43. * are met:
  44. * 1. Redistributions of source code must retain the above copyright
  45. * notice, this list of conditions and the following disclaimer.
  46. * 2. Redistributions in binary form must reproduce the above copyright
  47. * notice, this list of conditions and the following disclaimer in the
  48. * documentation and/or other materials provided with the distribution.
  49. * 3. All advertising materials mentioning features or use of this software
  50. * must display the following acknowledgement:
  51. * This product includes software developed by the University of
  52. * California, Berkeley and its contributors.
  53. * 4. Neither the name of the University nor the names of its contributors
  54. * may be used to endorse or promote products derived from this software
  55. * without specific prior written permission.
  56. *
  57. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  58. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  59. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  60. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  61. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  62. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  63. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  64. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  65. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  66. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  67. * SUCH DAMAGE.
  68. *
  69. * @(#)regexec.c 8.3 (Berkeley) 3/20/94
  70. */
  71. #include <sys/cdefs.h>
  72. #if defined(LIBC_SCCS) && !defined(lint)
  73. #if 0
  74. static char sccsid[] = "@(#)regexec.c 8.3 (Berkeley) 3/20/94";
  75. #else
  76. __RCSID("$NetBSD: regexec.c,v 1.22 2012/03/13 21:13:43 christos Exp $");
  77. #endif
  78. #endif /* LIBC_SCCS and not lint */
  79. /*
  80. * the outer shell of regexec()
  81. *
  82. * This file includes engine.c *twice*, after muchos fiddling with the
  83. * macros that code uses. This lets the same code operate on two different
  84. * representations for state sets.
  85. */
  86. #include "namespace.h"
  87. #include <sys/types.h>
  88. #include <assert.h>
  89. #include <ctype.h>
  90. #include <limits.h>
  91. #include <stdio.h>
  92. #include <stdlib.h>
  93. #include <string.h>
  94. #include <regex.h>
  95. #ifdef __weak_alias
  96. __weak_alias(regexec,_regexec)
  97. #endif
  98. #include "utils.h"
  99. #include "regex2.h"
  100. /* macros for manipulating states, small version */
  101. #define states unsigned long
  102. #define states1 unsigned long /* for later use in regexec() decision */
  103. #define CLEAR(v) ((v) = 0)
  104. #define SET0(v, n) ((v) &= ~((unsigned long)1 << (n)))
  105. #define SET1(v, n) ((v) |= (unsigned long)1 << (n))
  106. #define ISSET(v, n) (((v) & ((unsigned long)1 << (n))) != 0)
  107. #define ASSIGN(d, s) ((d) = (s))
  108. #define EQ(a, b) ((a) == (b))
  109. #define STATEVARS int dummy /* dummy version */
  110. #define STATESETUP(m, n) /* nothing */
  111. #define STATETEARDOWN(m) /* nothing */
  112. #define SETUP(v) ((v) = 0)
  113. #define onestate unsigned long
  114. #define INIT(o, n) ((o) = (unsigned long)1 << (n))
  115. #define INC(o) ((o) <<= 1)
  116. #define ISSTATEIN(v, o) (((v) & (o)) != 0)
  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) |= ((unsigned long)(src)&(here)) << (n))
  120. #define BACK(dst, src, n) ((dst) |= ((unsigned long)(src)&(here)) >> (n))
  121. #define ISSETBACK(v, n) (((v) & ((unsigned long)here >> (n))) != 0)
  122. /* function names */
  123. #define SNAMES /* engine.c looks after details */
  124. #include "engine.c"
  125. /* now undo things */
  126. #undef states
  127. #undef CLEAR
  128. #undef SET0
  129. #undef SET1
  130. #undef ISSET
  131. #undef ASSIGN
  132. #undef EQ
  133. #undef STATEVARS
  134. #undef STATESETUP
  135. #undef STATETEARDOWN
  136. #undef SETUP
  137. #undef onestate
  138. #undef INIT
  139. #undef INC
  140. #undef ISSTATEIN
  141. #undef FWD
  142. #undef BACK
  143. #undef ISSETBACK
  144. #undef SNAMES
  145. /* macros for manipulating states, large version */
  146. #define states char *
  147. #define CLEAR(v) memset(v, 0, (size_t)m->g->nstates)
  148. #define SET0(v, n) ((v)[n] = 0)
  149. #define SET1(v, n) ((v)[n] = 1)
  150. #define ISSET(v, n) ((v)[n])
  151. #define ASSIGN(d, s) memcpy(d, s, (size_t)m->g->nstates)
  152. #define EQ(a, b) (memcmp(a, b, (size_t)m->g->nstates) == 0)
  153. #define STATEVARS int vn; char *space
  154. #define STATESETUP(m, nv) \
  155. if (((m)->space = malloc((size_t)((nv)*(m)->g->nstates))) == NULL) \
  156. return(REG_ESPACE); \
  157. else \
  158. (m)->vn = 0
  159. #define STATETEARDOWN(m) { free((m)->space); m->space = NULL; }
  160. #define SETUP(v) ((v) = &m->space[(size_t)(m->vn++ * m->g->nstates)])
  161. #define onestate int
  162. #define INIT(o, n) ((o) = (int)(n))
  163. #define INC(o) ((o)++)
  164. #define ISSTATEIN(v, o) ((v)[o])
  165. /* some abbreviations; note that some of these know variable names! */
  166. /* do "if I'm here, I can also be there" etc without branches */
  167. #define FWD(dst, src, n) ((dst)[here+(n)] |= (src)[here])
  168. #define BACK(dst, src, n) ((dst)[here-(n)] |= (src)[here])
  169. #define ISSETBACK(v, n) ((v)[here - (n)])
  170. /* function names */
  171. #define LNAMES /* flag */
  172. #include "engine.c"
  173. /*
  174. - regexec - interface for matching
  175. = extern int regexec(const regex_t *, const char *, size_t, \
  176. = regmatch_t [], int);
  177. = #define REG_NOTBOL 00001
  178. = #define REG_NOTEOL 00002
  179. = #define REG_STARTEND 00004
  180. = #define REG_TRACE 00400 // tracing of execution
  181. = #define REG_LARGE 01000 // force large representation
  182. = #define REG_BACKR 02000 // force use of backref code
  183. *
  184. * We put this here so we can exploit knowledge of the state representation
  185. * when choosing which matcher to call. Also, by this point the matchers
  186. * have been prototyped.
  187. */
  188. int /* 0 success, REG_NOMATCH failure */
  189. regexec(
  190. const regex_t *preg,
  191. const char *string,
  192. size_t nmatch,
  193. regmatch_t pmatch[],
  194. int eflags)
  195. {
  196. struct re_guts *g = preg->re_g;
  197. char *s;
  198. #ifdef REDEBUG
  199. # define GOODFLAGS(f) (f)
  200. #else
  201. # define GOODFLAGS(f) ((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND))
  202. #endif
  203. _DIAGASSERT(preg != NULL);
  204. _DIAGASSERT(string != NULL);
  205. if (preg->re_magic != MAGIC1 || g->magic != MAGIC2)
  206. return(REG_BADPAT);
  207. assert(!(g->iflags&BAD));
  208. if (g->iflags&BAD) /* backstop for no-debug case */
  209. return(REG_BADPAT);
  210. eflags = GOODFLAGS(eflags);
  211. s = __UNCONST(string);
  212. if (g->nstates <= (sopno)(CHAR_BIT*sizeof(states1)) && !(eflags&REG_LARGE))
  213. return(smatcher(g, s, nmatch, pmatch, eflags));
  214. else
  215. return(lmatcher(g, s, nmatch, pmatch, eflags));
  216. }