PageRenderTime 61ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/stubdom/newlib-1.16.0/newlib/libc/sys/linux/stdlib/regexec.c

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