regexp.h 6.8 KB

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