regexp.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /* Copyright (C) 1996, 1997, 1998, 1999, 2004 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  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. /* Interface variables. They contain the results of the successful
  67. calls to `setp' and `advance'. */
  68. extern char *loc1;
  69. extern char *loc2;
  70. /* The use of this variable in the `advance' function is not
  71. supported. */
  72. extern char *locs;
  73. #ifndef __DO_NOT_DEFINE_COMPILE
  74. /* Get and compile the user supplied pattern up to end of line or
  75. string or until EOF is seen, whatever happens first. The result is
  76. placed in the buffer starting at EXPBUF and delimited by ENDBUF.
  77. This function cannot be defined in the libc itself since it depends
  78. on the macros. */
  79. char *
  80. compile (char *__restrict instring, char *__restrict expbuf,
  81. __const char *__restrict endbuf, int eof)
  82. {
  83. char *__input_buffer = NULL;
  84. size_t __input_size = 0;
  85. size_t __current_size = 0;
  86. int __ch;
  87. int __error;
  88. INIT
  89. /* Align the expression buffer according to the needs for an object
  90. of type `regex_t'. Then check for minimum size of the buffer for
  91. the compiled regular expression. */
  92. regex_t *__expr_ptr;
  93. # if defined __GNUC__ && __GNUC__ >= 2
  94. const size_t __req = __alignof__ (regex_t *);
  95. # else
  96. /* How shall we find out? We simply guess it and can change it is
  97. this really proofs to be wrong. */
  98. const size_t __req = 8;
  99. # endif
  100. expbuf += __req;
  101. expbuf -= (expbuf - ((char *) 0)) % __req;
  102. if (endbuf < expbuf + sizeof (regex_t))
  103. {
  104. ERROR (50);
  105. }
  106. __expr_ptr = (regex_t *) expbuf;
  107. /* The remaining space in the buffer can be used for the compiled
  108. pattern. */
  109. __expr_ptr->buffer = expbuf + sizeof (regex_t);
  110. __expr_ptr->allocated = endbuf - (char *) __expr_ptr->buffer;
  111. while ((__ch = (GETC ())) != eof)
  112. {
  113. if (__ch == '\0' || __ch == '\n')
  114. {
  115. UNGETC (__ch);
  116. break;
  117. }
  118. if (__current_size + 1 >= __input_size)
  119. {
  120. size_t __new_size = __input_size ? 2 * __input_size : 128;
  121. char *__new_room = (char *) alloca (__new_size);
  122. /* See whether we can use the old buffer. */
  123. if (__new_room + __new_size == __input_buffer)
  124. {
  125. __input_size += __new_size;
  126. __input_buffer = (char *) memcpy (__new_room, __input_buffer,
  127. __current_size);
  128. }
  129. else if (__input_buffer + __input_size == __new_room)
  130. __input_size += __new_size;
  131. else
  132. {
  133. __input_size = __new_size;
  134. __input_buffer = (char *) memcpy (__new_room, __input_buffer,
  135. __current_size);
  136. }
  137. }
  138. __input_buffer[__current_size++] = __ch;
  139. }
  140. __input_buffer[__current_size++] = '\0';
  141. /* Now compile the pattern. */
  142. __error = regcomp (__expr_ptr, __input_buffer, REG_NEWLINE);
  143. if (__error != 0)
  144. /* Oh well, we have to translate POSIX error codes. */
  145. switch (__error)
  146. {
  147. case REG_BADPAT:
  148. case REG_ECOLLATE:
  149. case REG_ECTYPE:
  150. case REG_EESCAPE:
  151. case REG_BADRPT:
  152. case REG_EEND:
  153. case REG_ERPAREN:
  154. default:
  155. /* There is no matching error code. */
  156. RETURN (36);
  157. case REG_ESUBREG:
  158. RETURN (25);
  159. case REG_EBRACK:
  160. RETURN (49);
  161. case REG_EPAREN:
  162. RETURN (42);
  163. case REG_EBRACE:
  164. RETURN (44);
  165. case REG_BADBR:
  166. RETURN (46);
  167. case REG_ERANGE:
  168. RETURN (11);
  169. case REG_ESPACE:
  170. case REG_ESIZE:
  171. ERROR (50);
  172. }
  173. /* Everything is ok. */
  174. RETURN ((char *) (__expr_ptr->buffer + __expr_ptr->used));
  175. }
  176. #endif
  177. /* Find the next match in STRING. The compiled regular expression is
  178. found in the buffer starting at EXPBUF. `loc1' will return the
  179. first character matched and `loc2' points to the next unmatched
  180. character. */
  181. extern int step (__const char *__restrict __string,
  182. __const char *__restrict __expbuf) __THROW;
  183. /* Match the beginning of STRING with the compiled regular expression
  184. in EXPBUF. If the match is successful `loc2' will contain the
  185. position of the first unmatched character. */
  186. extern int advance (__const char *__restrict __string,
  187. __const char *__restrict __expbuf) __THROW;
  188. __END_DECLS
  189. #endif /* regexp.h */