regex2.h
上传用户:luoyougen
上传日期:2008-05-12
资源大小:23136k
文件大小:8k
源码类别:

VxWorks

开发平台:

C/C++

  1. /* regcomp.c - regular expression handling */
  2. /* Copyright 1984-2001 Wind River Systems, Inc. */
  3. /*
  4. modification history
  5. --------------------
  6. 01d,01oct01,gls  made definition of __P() macro conditional (SPR #28330)
  7. 01c,31mar98,fle  warnings eradication
  8. 01b,30sep96,elp  put in share/ (SPR# 6775).
  9. 01a,10jan95,jcf   created.
  10. */
  11. /*
  12. DESCRIPTION
  13. This library is *not* the original BSD distribution.  Though the changes
  14. were completely cosmetic (file naming, and such), the user of this library
  15. is forewarned.
  16. AUTHOR: Henry Spencer
  17. */
  18. /*-
  19.  * Copyright (c) 1992, 1993, 1994 Henry Spencer.
  20.  * Copyright (c) 1992, 1993, 1994
  21.  * The Regents of the University of California.  All rights reserved.
  22.  *
  23.  * This code is derived from software contributed to Berkeley by
  24.  * Henry Spencer.
  25.  *
  26.  * Redistribution and use in source and binary forms, with or without
  27.  * modification, are permitted provided that the following conditions
  28.  * are met:
  29.  * 1. Redistributions of source code must retain the above copyright
  30.  *    notice, this list of conditions and the following disclaimer.
  31.  * 2. Redistributions in binary form must reproduce the above copyright
  32.  *    notice, this list of conditions and the following disclaimer in the
  33.  *    documentation and/or other materials provided with the distribution.
  34.  * 3. All advertising materials mentioning features or use of this software
  35.  *    must display the following acknowledgement:
  36.  * This product includes software developed by the University of
  37.  * California, Berkeley and its contributors.
  38.  * 4. Neither the name of the University nor the names of its contributors
  39.  *    may be used to endorse or promote products derived from this software
  40.  *    without specific prior written permission.
  41.  *
  42.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  43.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  44.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  45.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  46.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  47.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  48.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  49.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  50.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  51.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  52.  * SUCH DAMAGE.
  53.  *
  54.  * @(#)regex2.h 8.4 (Berkeley) 3/20/94
  55.  */
  56. #ifndef __P
  57. #ifdef __STDC__
  58. #define __P(p)  p
  59. #else
  60. #define __P(p)  ()
  61. #endif
  62. #endif /* __P */
  63. #define POSIX_MISTAKE /* XXX was defined in BSD as extra CFLAG */
  64. /* utility definitions */
  65. #define DUPMAX _POSIX_MAX_INPUT /* XXX WAS _POSIX2_RE_DUP_MAX */
  66. #define INFINITY (DUPMAX + 1)
  67. #define NC (CHAR_MAX - CHAR_MIN + 1)
  68. typedef unsigned char uch;
  69. /* switch off assertions (if not already off) if no REDEBUG */
  70. #ifndef REDEBUG
  71. #ifndef NDEBUG
  72. #define NDEBUG /* no assertions please */
  73. #endif
  74. #endif
  75. #include <assert.h>
  76. /* for old systems with bcopy() but no memmove() */
  77. #ifdef USEBCOPY
  78. #define memmove(d, s, c) bcopy(s, d, c)
  79. #endif
  80. /*
  81.  * First, the stuff that ends up in the outside-world include file
  82.  = typedef off_t regoff_t;
  83.  = typedef struct {
  84.  =  int re_magic;
  85.  =  size_t re_nsub; // number of parenthesized subexpressions
  86.  =  const char *re_endp; // end pointer for REG_PEND
  87.  =  struct re_guts *re_g; // none of your business :-)
  88.  = } regex_t;
  89.  = typedef struct {
  90.  =  regoff_t rm_so; // start of match
  91.  =  regoff_t rm_eo; // end of match
  92.  = } regmatch_t;
  93.  */
  94. /*
  95.  * internals of regex_t
  96.  */
  97. #define MAGIC1 ((('r'^0200)<<8) | 'e')
  98. /*
  99.  * The internal representation is a *strip*, a sequence of
  100.  * operators ending with an endmarker.  (Some terminology etc. is a
  101.  * historical relic of earlier versions which used multiple strips.)
  102.  * Certain oddities in the representation are there to permit running
  103.  * the machinery backwards; in particular, any deviation from sequential
  104.  * flow must be marked at both its source and its destination.  Some
  105.  * fine points:
  106.  *
  107.  * - OPLUS_ and O_PLUS are *inside* the loop they create.
  108.  * - OQUEST_ and O_QUEST are *outside* the bypass they create.
  109.  * - OCH_ and O_CH are *outside* the multi-way branch they create, while
  110.  *   OOR1 and OOR2 are respectively the end and the beginning of one of
  111.  *   the branches.  Note that there is an implicit OOR2 following OCH_
  112.  *   and an implicit OOR1 preceding O_CH.
  113.  *
  114.  * In state representations, an operator's bit is on to signify a state
  115.  * immediately *preceding* "execution" of that operator.
  116.  */
  117. typedef unsigned long sop; /* strip operator */
  118. typedef long sopno;
  119. #define OPRMASK 0xf8000000
  120. #define OPDMASK 0x07ffffff
  121. #define OPSHIFT ((unsigned)27)
  122. #define OP(n) ((n)&OPRMASK)
  123. #define OPND(n) ((n)&OPDMASK)
  124. #define SOP(op, opnd) ((op)|(opnd))
  125. /* operators    meaning operand */
  126. /* (back, fwd are offsets) */
  127. #define OEND (1<<OPSHIFT) /* endmarker - */
  128. #define OCHAR (2<<OPSHIFT) /* character unsigned char */
  129. #define OBOL (3<<OPSHIFT) /* left anchor - */
  130. #define OEOL (4<<OPSHIFT) /* right anchor - */
  131. #define OANY (5<<OPSHIFT) /* . - */
  132. #define OANYOF (6<<OPSHIFT) /* [...] set number */
  133. #define OBACK_ (7<<OPSHIFT) /* begin d paren number */
  134. #define O_BACK (8<<OPSHIFT) /* end d paren number */
  135. #define OPLUS_ (9<<OPSHIFT) /* + prefix fwd to suffix */
  136. #define O_PLUS (10<<OPSHIFT) /* + suffix back to prefix */
  137. #define OQUEST_ (11<<OPSHIFT) /* ? prefix fwd to suffix */
  138. #define O_QUEST (12<<OPSHIFT) /* ? suffix back to prefix */
  139. #define OLPAREN (13<<OPSHIFT) /* ( fwd to ) */
  140. #define ORPAREN (14<<OPSHIFT) /* ) back to ( */
  141. #define OCH_ (15<<OPSHIFT) /* begin choice fwd to OOR2 */
  142. #define OOR1 (16<<OPSHIFT) /* | pt. 1 back to OOR1 or OCH_ */
  143. #define OOR2 (17<<OPSHIFT) /* | pt. 2 fwd to OOR2 or O_CH */
  144. #define O_CH (18<<OPSHIFT) /* end choice back to OOR1 */
  145. #define OBOW (19<<OPSHIFT) /* begin word - */
  146. #define OEOW (20<<OPSHIFT) /* end word - */
  147. /*
  148.  * Structure for [] character-set representation.  Character sets are
  149.  * done as bit vectors, grouped 8 to a byte vector for compactness.
  150.  * The individual set therefore has both a pointer to the byte vector
  151.  * and a mask to pick out the relevant bit of each byte.  A hash code
  152.  * simplifies testing whether two sets could be identical.
  153.  *
  154.  * This will get trickier for multicharacter collating elements.  As
  155.  * preliminary hooks for dealing with such things, we also carry along
  156.  * a string of multi-character elements, and decide the size of the
  157.  * vectors at run time.
  158.  */
  159. typedef struct {
  160. uch *ptr; /* -> uch [csetsize] */
  161. uch mask; /* bit within array */
  162. uch hash; /* hash code */
  163. size_t smultis;
  164. char *multis; /* -> char[smulti]  abcdef */
  165. } cset;
  166. /* note that CHadd and CHsub are unsafe, and CHIN doesn't yield 0/1 */
  167. #define CHadd(cs, c) ((cs)->ptr[(uch)(c)] |= (cs)->mask, (cs)->hash += (c))
  168. #define CHsub(cs, c) ((cs)->ptr[(uch)(c)] &= ~(cs)->mask, (cs)->hash -= (c))
  169. #define CHIN(cs, c) ((cs)->ptr[(uch)(c)] & (cs)->mask)
  170. #define MCadd(p, cs, cp) mcadd(p, cs, cp) /* regcomp() internal fns */
  171. #define MCsub(p, cs, cp) mcsub(p, cs, cp)
  172. #define MCin(p, cs, cp) mcin(p, cs, cp)
  173. /* stuff for character categories */
  174. typedef unsigned char cat_t;
  175. /*
  176.  * main compiled-expression structure
  177.  */
  178. struct re_guts {
  179. int magic;
  180. # define MAGIC2 ((('R'^0200)<<8)|'E')
  181. sop *strip; /* malloced area for strip */
  182. int csetsize; /* number of bits in a cset vector */
  183. int ncsets; /* number of csets in use */
  184. cset *sets; /* -> cset [ncsets] */
  185. uch *setbits; /* -> uch[csetsize][ncsets/CHAR_BIT] */
  186. int cflags; /* copy of regcomp() cflags argument */
  187. sopno nstates; /* = number of sops */
  188. sopno firststate; /* the initial OEND (normally 0) */
  189. sopno laststate; /* the final OEND */
  190. int iflags; /* internal flags */
  191. # define USEBOL 01 /* used ^ */
  192. # define USEEOL 02 /* used $ */
  193. # define BAD 04 /* something wrong */
  194. int nbol; /* number of ^ used */
  195. int neol; /* number of $ used */
  196. int ncategories; /* how many character categories */
  197. cat_t *categories; /* ->catspace[-CHAR_MIN] */
  198. char *must; /* match must contain this string */
  199. int mlen; /* length of must */
  200. size_t nsub; /* copy of re_nsub */
  201. int backrefs; /* does it use back references? */
  202. sopno nplus; /* how deep does it nest +s? */
  203. /* catspace must be last */
  204. cat_t catspace[1]; /* actually [NC] */
  205. };
  206. /* misc utilities */
  207. #ifdef OUT
  208. #undef OUT
  209. #endif /* OUT */
  210. #define OUT (CHAR_MAX+1) /* a non-character value */
  211. #define ISWORD(c) (isalnum(c) || (c) == '_')