regexp.h 7.0 KB

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