regex.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. #ifndef _REGEX_H_
  2. #define _REGEX_H_ /* never again */
  3. #include "tclInt.h"
  4. /*
  5. * regular expressions
  6. *
  7. * Copyright (c) 1998, 1999 Henry Spencer. All rights reserved.
  8. *
  9. * Development of this software was funded, in part, by Cray Research Inc.,
  10. * UUNET Communications Services Inc., Sun Microsystems Inc., and Scriptics
  11. * Corporation, none of whom are responsible for the results. The author
  12. * thanks all of them.
  13. *
  14. * Redistribution and use in source and binary forms -- with or without
  15. * modification -- are permitted for any purpose, provided that
  16. * redistributions in source form retain this entire copyright notice and
  17. * indicate the origin and nature of any modifications.
  18. *
  19. * I'd appreciate being given credit for this package in the documentation of
  20. * software which uses it, but that is not a requirement.
  21. *
  22. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  23. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  24. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  25. * HENRY SPENCER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  26. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  27. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  28. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  29. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  30. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  31. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. *
  34. * Prototypes etc. marked with "^" within comments get gathered up (and
  35. * possibly edited) by the regfwd program and inserted near the bottom of this
  36. * file.
  37. *
  38. * We offer the option of declaring one wide-character version of the RE
  39. * functions as well as the char versions. To do that, define __REG_WIDE_T to
  40. * the type of wide characters (unfortunately, there is no consensus that
  41. * wchar_t is suitable) and __REG_WIDE_COMPILE and __REG_WIDE_EXEC to the
  42. * names to be used for the compile and execute functions (suggestion:
  43. * re_Xcomp and re_Xexec, where X is a letter suggestive of the wide type,
  44. * e.g. re_ucomp and re_uexec for Unicode). For cranky old compilers, it may
  45. * be necessary to do something like:
  46. * #define __REG_WIDE_COMPILE(a,b,c,d) re_Xcomp(a,b,c,d)
  47. * #define __REG_WIDE_EXEC(a,b,c,d,e,f,g) re_Xexec(a,b,c,d,e,f,g)
  48. * rather than just #defining the names as parameterless macros.
  49. *
  50. * For some specialized purposes, it may be desirable to suppress the
  51. * declarations of the "front end" functions, regcomp() and regexec(), or of
  52. * the char versions of the compile and execute functions. To suppress the
  53. * front-end functions, define __REG_NOFRONT. To suppress the char versions,
  54. * define __REG_NOCHAR.
  55. *
  56. * The right place to do those defines (and some others you may want, see
  57. * below) would be <sys/types.h>. If you don't have control of that file, the
  58. * right place to add your own defines to this file is marked below. This is
  59. * normally done automatically, by the makefile and regmkhdr, based on the
  60. * contents of regcustom.h.
  61. */
  62. /*
  63. * voodoo for C++
  64. */
  65. #ifdef __cplusplus
  66. extern "C" {
  67. #endif
  68. /*
  69. * Add your own defines, if needed, here.
  70. */
  71. /*
  72. * Location where a chunk of regcustom.h is automatically spliced into this
  73. * file (working from its prototype, regproto.h).
  74. */
  75. /* --- begin --- */
  76. /* ensure certain things don't sneak in from system headers */
  77. #ifdef __REG_WIDE_T
  78. #undef __REG_WIDE_T
  79. #endif
  80. #ifdef __REG_WIDE_COMPILE
  81. #undef __REG_WIDE_COMPILE
  82. #endif
  83. #ifdef __REG_WIDE_EXEC
  84. #undef __REG_WIDE_EXEC
  85. #endif
  86. #ifdef __REG_REGOFF_T
  87. #undef __REG_REGOFF_T
  88. #endif
  89. #ifdef __REG_NOFRONT
  90. #undef __REG_NOFRONT
  91. #endif
  92. #ifdef __REG_NOCHAR
  93. #undef __REG_NOCHAR
  94. #endif
  95. /* interface types */
  96. #define __REG_WIDE_T Tcl_UniChar
  97. #define __REG_REGOFF_T long /* not really right, but good enough... */
  98. /* names and declarations */
  99. #define __REG_WIDE_COMPILE TclReComp
  100. #define __REG_WIDE_EXEC TclReExec
  101. #define __REG_NOFRONT /* don't want regcomp() and regexec() */
  102. #define __REG_NOCHAR /* or the char versions */
  103. #define regfree TclReFree
  104. #define regerror TclReError
  105. /* --- end --- */
  106. /*
  107. * interface types etc.
  108. */
  109. /*
  110. * regoff_t has to be large enough to hold either off_t or ssize_t, and must
  111. * be signed; it's only a guess that long is suitable, so we offer
  112. * <sys/types.h> an override.
  113. */
  114. #ifdef __REG_REGOFF_T
  115. typedef __REG_REGOFF_T regoff_t;
  116. #else
  117. typedef long regoff_t;
  118. #endif
  119. /*
  120. * other interface types
  121. */
  122. /* the biggie, a compiled RE (or rather, a front end to same) */
  123. typedef struct {
  124. int re_magic; /* magic number */
  125. size_t re_nsub; /* number of subexpressions */
  126. long re_info; /* information about RE */
  127. #define REG_UBACKREF 000001
  128. #define REG_ULOOKAHEAD 000002
  129. #define REG_UBOUNDS 000004
  130. #define REG_UBRACES 000010
  131. #define REG_UBSALNUM 000020
  132. #define REG_UPBOTCH 000040
  133. #define REG_UBBS 000100
  134. #define REG_UNONPOSIX 000200
  135. #define REG_UUNSPEC 000400
  136. #define REG_UUNPORT 001000
  137. #define REG_ULOCALE 002000
  138. #define REG_UEMPTYMATCH 004000
  139. #define REG_UIMPOSSIBLE 010000
  140. #define REG_USHORTEST 020000
  141. int re_csize; /* sizeof(character) */
  142. char *re_endp; /* backward compatibility kludge */
  143. /* the rest is opaque pointers to hidden innards */
  144. char *re_guts; /* `char *' is more portable than `void *' */
  145. char *re_fns;
  146. } regex_t;
  147. /* result reporting (may acquire more fields later) */
  148. typedef struct {
  149. regoff_t rm_so; /* start of substring */
  150. regoff_t rm_eo; /* end of substring */
  151. } regmatch_t;
  152. /* supplementary control and reporting */
  153. typedef struct {
  154. regmatch_t rm_extend; /* see REG_EXPECT */
  155. } rm_detail_t;
  156. /*
  157. * compilation
  158. ^ #ifndef __REG_NOCHAR
  159. ^ int re_comp(regex_t *, const char *, size_t, int);
  160. ^ #endif
  161. ^ #ifndef __REG_NOFRONT
  162. ^ int regcomp(regex_t *, const char *, int);
  163. ^ #endif
  164. ^ #ifdef __REG_WIDE_T
  165. ^ int __REG_WIDE_COMPILE(regex_t *, const __REG_WIDE_T *, size_t, int);
  166. ^ #endif
  167. */
  168. #define REG_BASIC 000000 /* BREs (convenience) */
  169. #define REG_EXTENDED 000001 /* EREs */
  170. #define REG_ADVF 000002 /* advanced features in EREs */
  171. #define REG_ADVANCED 000003 /* AREs (which are also EREs) */
  172. #define REG_QUOTE 000004 /* no special characters, none */
  173. #define REG_NOSPEC REG_QUOTE /* historical synonym */
  174. #define REG_ICASE 000010 /* ignore case */
  175. #define REG_NOSUB 000020 /* don't care about subexpressions */
  176. #define REG_EXPANDED 000040 /* expanded format, white space & comments */
  177. #define REG_NLSTOP 000100 /* \n doesn't match . or [^ ] */
  178. #define REG_NLANCH 000200 /* ^ matches after \n, $ before */
  179. #define REG_NEWLINE 000300 /* newlines are line terminators */
  180. #define REG_PEND 000400 /* ugh -- backward-compatibility hack */
  181. #define REG_EXPECT 001000 /* report details on partial/limited matches */
  182. #define REG_BOSONLY 002000 /* temporary kludge for BOS-only matches */
  183. #define REG_DUMP 004000 /* none of your business :-) */
  184. #define REG_FAKE 010000 /* none of your business :-) */
  185. #define REG_PROGRESS 020000 /* none of your business :-) */
  186. /*
  187. * execution
  188. ^ #ifndef __REG_NOCHAR
  189. ^ int re_exec(regex_t *, const char *, size_t,
  190. ^ rm_detail_t *, size_t, regmatch_t [], int);
  191. ^ #endif
  192. ^ #ifndef __REG_NOFRONT
  193. ^ int regexec(regex_t *, const char *, size_t, regmatch_t [], int);
  194. ^ #endif
  195. ^ #ifdef __REG_WIDE_T
  196. ^ int __REG_WIDE_EXEC(regex_t *, const __REG_WIDE_T *, size_t,
  197. ^ rm_detail_t *, size_t, regmatch_t [], int);
  198. ^ #endif
  199. */
  200. #define REG_NOTBOL 0001 /* BOS is not BOL */
  201. #define REG_NOTEOL 0002 /* EOS is not EOL */
  202. #define REG_STARTEND 0004 /* backward compatibility kludge */
  203. #define REG_FTRACE 0010 /* none of your business */
  204. #define REG_MTRACE 0020 /* none of your business */
  205. #define REG_SMALL 0040 /* none of your business */
  206. /*
  207. * misc generics (may be more functions here eventually)
  208. ^ void regfree(regex_t *);
  209. */
  210. /*
  211. * error reporting
  212. * Be careful if modifying the list of error codes -- the table used by
  213. * regerror() is generated automatically from this file!
  214. *
  215. * Note that there is no wide-char variant of regerror at this time; what kind
  216. * of character is used for error reports is independent of what kind is used
  217. * in matching.
  218. *
  219. ^ extern size_t regerror(int, char *, size_t);
  220. */
  221. #define REG_OKAY 0 /* no errors detected */
  222. #define REG_NOMATCH 1 /* failed to match */
  223. #define REG_BADPAT 2 /* invalid regexp */
  224. #define REG_ECOLLATE 3 /* invalid collating element */
  225. #define REG_ECTYPE 4 /* invalid character class */
  226. #define REG_EESCAPE 5 /* invalid escape \ sequence */
  227. #define REG_ESUBREG 6 /* invalid backreference number */
  228. #define REG_EBRACK 7 /* brackets [] not balanced */
  229. #define REG_EPAREN 8 /* parentheses () not balanced */
  230. #define REG_EBRACE 9 /* braces {} not balanced */
  231. #define REG_BADBR 10 /* invalid repetition count(s) */
  232. #define REG_ERANGE 11 /* invalid character range */
  233. #define REG_ESPACE 12 /* out of memory */
  234. #define REG_BADRPT 13 /* quantifier operand invalid */
  235. #define REG_ASSERT 15 /* "can't happen" -- you found a bug */
  236. #define REG_INVARG 16 /* invalid argument to regex function */
  237. #define REG_MIXED 17 /* character widths of regex and string differ */
  238. #define REG_BADOPT 18 /* invalid embedded option */
  239. #define REG_ETOOBIG 19 /* regular expression is too complex */
  240. #define REG_ECOLORS 20 /* too many colors */
  241. /* two specials for debugging and testing */
  242. #define REG_ATOI 101 /* convert error-code name to number */
  243. #define REG_ITOA 102 /* convert error-code number to name */
  244. /*
  245. * the prototypes, as possibly munched by regfwd
  246. */
  247. /* =====^!^===== begin forwards =====^!^===== */
  248. /* automatically gathered by fwd; do not hand-edit */
  249. /* === regproto.h === */
  250. #ifndef __REG_NOCHAR
  251. int re_comp(regex_t *, const char *, size_t, int);
  252. #endif
  253. #ifndef __REG_NOFRONT
  254. int regcomp(regex_t *, const char *, int);
  255. #endif
  256. #ifdef __REG_WIDE_T
  257. MODULE_SCOPE int __REG_WIDE_COMPILE(regex_t *, const __REG_WIDE_T *, size_t, int);
  258. #endif
  259. #ifndef __REG_NOCHAR
  260. int re_exec(regex_t *, const char *, size_t, rm_detail_t *, size_t, regmatch_t [], int);
  261. #endif
  262. #ifndef __REG_NOFRONT
  263. int regexec(regex_t *, const char *, size_t, regmatch_t [], int);
  264. #endif
  265. #ifdef __REG_WIDE_T
  266. MODULE_SCOPE int __REG_WIDE_EXEC(regex_t *, const __REG_WIDE_T *, size_t, rm_detail_t *, size_t, regmatch_t [], int);
  267. #endif
  268. MODULE_SCOPE void regfree(regex_t *);
  269. MODULE_SCOPE size_t regerror(int, char *, size_t);
  270. /* automatically gathered by fwd; do not hand-edit */
  271. /* =====^!^===== end forwards =====^!^===== */
  272. /*
  273. * more C++ voodoo
  274. */
  275. #ifdef __cplusplus
  276. }
  277. #endif
  278. #endif
  279. /*
  280. * Local Variables:
  281. * mode: c
  282. * c-basic-offset: 4
  283. * fill-column: 78
  284. * End:
  285. */