regexp.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /* Copyright (C) 1996, 1997, 1998, 1999, 2004, 2008
  2. Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #ifndef _REGEXP_H
  17. #define _REGEXP_H 1
  18. /* The contents of this header file was first standardized in X/Open
  19. System Interface and Headers Issue 2, originally coming from SysV.
  20. In issue 4, version 2, it is marked as TO BE WITDRAWN, and it has
  21. been withdrawn in SUSv3.
  22. This code shouldn't be used in any newly written code. It is
  23. included only for compatibility reasons. Use the POSIX definition
  24. in <regex.h> for portable applications and a reasonable interface. */
  25. #include <features.h>
  26. #include <alloca.h>
  27. #include <regex.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. /* The implementation provided here emulates the needed functionality
  31. by mapping to the POSIX regular expression matcher. The interface
  32. for the here included function is weird (this really is a harmless
  33. word).
  34. The user has to provide six macros before this header file can be
  35. included:
  36. INIT Declarations vor variables which can be used by the
  37. other macros.
  38. GETC() Return the value of the next character in the regular
  39. expression pattern. Successive calls should return
  40. successive characters.
  41. PEEKC() Return the value of the next character in the regular
  42. expression pattern. Immediately successive calls to
  43. PEEKC() should return the same character which should
  44. also be the next character returned by GETC().
  45. UNGETC(c) Cause `c' to be returned by the next call to GETC() and
  46. PEEKC().
  47. RETURN(ptr) Used for normal exit of the `compile' function. `ptr'
  48. is a pointer to the character after the last character of
  49. the compiled regular expression.
  50. ERROR(val) Used for abnormal return from `compile'. `val' is the
  51. error number. The error codes are:
  52. 11 Range endpoint too large.
  53. 16 Bad number.
  54. 25 \digit out of range.
  55. 36 Illegal or missing delimiter.
  56. 41 No remembered search string.
  57. 42 \( \) imbalance.
  58. 43 Too many \(.
  59. 44 More tan two numbers given in \{ \}.
  60. 45 } expected after \.
  61. 46 First number exceeds second in \{ \}.
  62. 49 [ ] imbalance.
  63. 50 Regular expression overflow.
  64. */
  65. __BEGIN_DECLS
  66. #if 0
  67. /* Interface variables. They contain the results of the successful
  68. calls to `setp' and `advance'. */
  69. extern char *loc1;
  70. extern char *loc2;
  71. /* The use of this variable in the `advance' function is not
  72. supported. */
  73. extern char *locs;
  74. #endif
  75. #ifndef __DO_NOT_DEFINE_COMPILE
  76. /* Get and compile the user supplied pattern up to end of line or
  77. string or until EOF is seen, whatever happens first. The result is
  78. placed in the buffer starting at EXPBUF and delimited by ENDBUF.
  79. This function cannot be defined in the libc itself since it depends
  80. on the macros. */
  81. char *
  82. compile (char *__restrict instring, char *__restrict expbuf,
  83. const char *__restrict endbuf, int eof)
  84. {
  85. char *__input_buffer = NULL;
  86. size_t __input_size = 0;
  87. size_t __current_size = 0;
  88. int __ch;
  89. int __error;
  90. INIT
  91. /* Align the expression buffer according to the needs for an object
  92. of type `regex_t'. Then check for minimum size of the buffer for
  93. the compiled regular expression. */
  94. regex_t *__expr_ptr;
  95. # if defined __GNUC__ && __GNUC__ >= 2
  96. const size_t __req = __alignof__ (regex_t *);
  97. # else
  98. /* How shall we find out? We simply guess it and can change it is
  99. this really proofs to be wrong. */
  100. const size_t __req = 8;
  101. # endif
  102. expbuf += __req;
  103. expbuf -= (expbuf - ((char *) 0)) % __req;
  104. if (endbuf < expbuf + sizeof (regex_t))
  105. {
  106. ERROR (50);
  107. }
  108. __expr_ptr = (regex_t *) expbuf;
  109. /* The remaining space in the buffer can be used for the compiled
  110. pattern. */
  111. __expr_ptr->__REPB_PREFIX (buffer) = expbuf + sizeof (regex_t);
  112. __expr_ptr->__REPB_PREFIX (allocated)
  113. = endbuf - (char *) __expr_ptr->__REPB_PREFIX (buffer);
  114. while ((__ch = (GETC ())) != eof)
  115. {
  116. if (__ch == '\0' || __ch == '\n')
  117. {
  118. UNGETC (__ch);
  119. break;
  120. }
  121. if (__current_size + 1 >= __input_size)
  122. {
  123. size_t __new_size = __input_size ? 2 * __input_size : 128;
  124. char *__new_room = (char *) alloca (__new_size);
  125. /* See whether we can use the old buffer. */
  126. if (__new_room + __new_size == __input_buffer)
  127. {
  128. __input_size += __new_size;
  129. __input_buffer = (char *) memcpy (__new_room, __input_buffer,
  130. __current_size);
  131. }
  132. else if (__input_buffer + __input_size == __new_room)
  133. __input_size += __new_size;
  134. else
  135. {
  136. __input_size = __new_size;
  137. __input_buffer = (char *) memcpy (__new_room, __input_buffer,
  138. __current_size);
  139. }
  140. }
  141. __input_buffer[__current_size++] = __ch;
  142. }
  143. if (__current_size)
  144. __input_buffer[__current_size++] = '\0';
  145. else
  146. __input_buffer = "";
  147. /* Now compile the pattern. */
  148. __error = regcomp (__expr_ptr, __input_buffer, REG_NEWLINE);
  149. if (__error != 0)
  150. /* Oh well, we have to translate POSIX error codes. */
  151. switch (__error)
  152. {
  153. case REG_BADPAT:
  154. case REG_ECOLLATE:
  155. case REG_ECTYPE:
  156. case REG_EESCAPE:
  157. case REG_BADRPT:
  158. case REG_EEND:
  159. case REG_ERPAREN:
  160. default:
  161. /* There is no matching error code. */
  162. RETURN (36);
  163. case REG_ESUBREG:
  164. RETURN (25);
  165. case REG_EBRACK:
  166. RETURN (49);
  167. case REG_EPAREN:
  168. RETURN (42);
  169. case REG_EBRACE:
  170. RETURN (44);
  171. case REG_BADBR:
  172. RETURN (46);
  173. case REG_ERANGE:
  174. RETURN (11);
  175. case REG_ESPACE:
  176. case REG_ESIZE:
  177. ERROR (50);
  178. }
  179. /* Everything is ok. */
  180. RETURN ((char *) (__expr_ptr->__REPB_PREFIX (buffer)
  181. + __expr_ptr->__REPB_PREFIX (used)));
  182. }
  183. #endif
  184. #if 0
  185. /* Find the next match in STRING. The compiled regular expression is
  186. found in the buffer starting at EXPBUF. `loc1' will return the
  187. first character matched and `loc2' points to the next unmatched
  188. character. */
  189. extern int step (const char *__restrict __string,
  190. const char *__restrict __expbuf) __THROW;
  191. /* Match the beginning of STRING with the compiled regular expression
  192. in EXPBUF. If the match is successful `loc2' will contain the
  193. position of the first unmatched character. */
  194. extern int advance (const char *__restrict __string,
  195. const char *__restrict __expbuf) __THROW;
  196. #endif
  197. __END_DECLS
  198. #endif /* regexp.h */