PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/newlib/libc/posix/regexec.c

https://github.com/jaumem/MoSync
C | 184 lines | 105 code | 14 blank | 65 comment | 15 complexity | ad5193270e3aeb37cf9faee187eba2e1 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. * 4. 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. #ifndef _NO_REGEX
  36. #if defined(LIBC_SCCS) && !defined(lint)
  37. static char sccsid[] = "@(#)regexec.c 8.3 (Berkeley) 3/20/94";
  38. #endif /* LIBC_SCCS and not lint */
  39. #include <sys/cdefs.h>
  40. /*
  41. * the outer shell of regexec()
  42. *
  43. * This file includes engine.c *twice*, after muchos fiddling with the
  44. * macros that code uses. This lets the same code operate on two different
  45. * representations for state sets.
  46. */
  47. #include <sys/types.h>
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. #include <string.h>
  51. #include <limits.h>
  52. #include <ctype.h>
  53. #include <regex.h>
  54. #include "utils.h"
  55. #include "regex2.h"
  56. #ifdef REDEBUG
  57. static int nope = 0; /* for use in asserts; shuts lint up */
  58. #endif
  59. /* macros for manipulating states, small version */
  60. #define states long
  61. #define states1 states /* for later use in regexec() decision */
  62. #define CLEAR(v) ((v) = 0)
  63. #define SET0(v, n) ((v) &= ~((unsigned long)1 << (n)))
  64. #define SET1(v, n) ((v) |= (unsigned long)1 << (n))
  65. #define ISSET(v, n) (((v) & ((unsigned long)1 << (n))) != 0)
  66. #define ASSIGN(d, s) ((d) = (s))
  67. #define EQ(a, b) ((a) == (b))
  68. #define STATEVARS long dummy /* dummy version */
  69. #define STATESETUP(m, n) /* nothing */
  70. #define STATETEARDOWN(m) /* nothing */
  71. #define SETUP(v) ((v) = 0)
  72. #define onestate long
  73. #define INIT(o, n) ((o) = (unsigned long)1 << (n))
  74. #define INC(o) ((o) <<= 1)
  75. #define ISSTATEIN(v, o) (((v) & (o)) != 0)
  76. /* some abbreviations; note that some of these know variable names! */
  77. /* do "if I'm here, I can also be there" etc without branches */
  78. #define FWD(dst, src, n) ((dst) |= ((unsigned long)(src)&(here)) << (n))
  79. #define BACK(dst, src, n) ((dst) |= ((unsigned long)(src)&(here)) >> (n))
  80. #define ISSETBACK(v, n) (((v) & ((unsigned long)here >> (n))) != 0)
  81. /* function names */
  82. #define SNAMES /* engine.c looks after details */
  83. #include "engine.c"
  84. /* now undo things */
  85. #undef states
  86. #undef CLEAR
  87. #undef SET0
  88. #undef SET1
  89. #undef ISSET
  90. #undef ASSIGN
  91. #undef EQ
  92. #undef STATEVARS
  93. #undef STATESETUP
  94. #undef STATETEARDOWN
  95. #undef SETUP
  96. #undef onestate
  97. #undef INIT
  98. #undef INC
  99. #undef ISSTATEIN
  100. #undef FWD
  101. #undef BACK
  102. #undef ISSETBACK
  103. #undef SNAMES
  104. /* macros for manipulating states, large version */
  105. #define states char *
  106. #define CLEAR(v) memset(v, 0, m->g->nstates)
  107. #define SET0(v, n) ((v)[n] = 0)
  108. #define SET1(v, n) ((v)[n] = 1)
  109. #define ISSET(v, n) ((v)[n])
  110. #define ASSIGN(d, s) memcpy(d, s, m->g->nstates)
  111. #define EQ(a, b) (memcmp(a, b, m->g->nstates) == 0)
  112. #define STATEVARS long vn; char *space
  113. #define STATESETUP(m, nv) { (m)->space = malloc((nv)*(m)->g->nstates); \
  114. if ((m)->space == NULL) return(REG_ESPACE); \
  115. (m)->vn = 0; }
  116. #define STATETEARDOWN(m) { free((m)->space); }
  117. #define SETUP(v) ((v) = &m->space[m->vn++ * m->g->nstates])
  118. #define onestate long
  119. #define INIT(o, n) ((o) = (n))
  120. #define INC(o) ((o)++)
  121. #define ISSTATEIN(v, o) ((v)[o])
  122. /* some abbreviations; note that some of these know variable names! */
  123. /* do "if I'm here, I can also be there" etc without branches */
  124. #define FWD(dst, src, n) ((dst)[here+(n)] |= (src)[here])
  125. #define BACK(dst, src, n) ((dst)[here-(n)] |= (src)[here])
  126. #define ISSETBACK(v, n) ((v)[here - (n)])
  127. /* function names */
  128. #define LNAMES /* flag */
  129. #include "engine.c"
  130. /*
  131. - regexec - interface for matching
  132. = extern int regexec(const regex_t *, const char *, size_t, \
  133. = regmatch_t [], int);
  134. = #define REG_NOTBOL 00001
  135. = #define REG_NOTEOL 00002
  136. = #define REG_STARTEND 00004
  137. = #define REG_TRACE 00400 // tracing of execution
  138. = #define REG_LARGE 01000 // force large representation
  139. = #define REG_BACKR 02000 // force use of backref code
  140. *
  141. * We put this here so we can exploit knowledge of the state representation
  142. * when choosing which matcher to call. Also, by this point the matchers
  143. * have been prototyped.
  144. */
  145. int /* 0 success, REG_NOMATCH failure */
  146. regexec(preg, string, nmatch, pmatch, eflags)
  147. const regex_t *preg;
  148. const char *string;
  149. size_t nmatch;
  150. regmatch_t pmatch[];
  151. int eflags;
  152. {
  153. struct re_guts *g = preg->re_g;
  154. #ifdef REDEBUG
  155. # define GOODFLAGS(f) (f)
  156. #else
  157. # define GOODFLAGS(f) ((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND))
  158. #endif
  159. if (preg->re_magic != MAGIC1 || g->magic != MAGIC2)
  160. return(REG_BADPAT);
  161. assert(!(g->iflags&BAD));
  162. if (g->iflags&BAD) /* backstop for no-debug case */
  163. return(REG_BADPAT);
  164. eflags = GOODFLAGS(eflags);
  165. if (g->nstates <= CHAR_BIT*sizeof(states1) && !(eflags&REG_LARGE))
  166. return(smatcher(g, (char *)string, nmatch, pmatch, eflags));
  167. else
  168. return(lmatcher(g, (char *)string, nmatch, pmatch, eflags));
  169. }
  170. #endif /* !_NO_REGEX */