Commit dafd0440 dafd044063fc411ada8fd1a77e4ff6d7993b1c14 by Sergey Poznyakoff

Updated by gnulib-sync

1 parent 3e718271
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
17 If not, write to the Free Software Foundation, 17 If not, write to the Free Software Foundation,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 19
20 #if HAVE_CONFIG_H 20 #ifdef HAVE_CONFIG_H
21 # include <config.h> 21 # include <config.h>
22 #endif 22 #endif
23 23
......
1 /* Copyright (C) 1992-2001, 2003, 2004 Free Software Foundation, Inc. 1 /* Copyright (C) 1992-2001, 2003, 2004, 2005 Free Software Foundation, Inc.
2 This file is part of the GNU C Library. 2 This file is part of the GNU C Library.
3 3
4 This program is free software; you can redistribute it and/or modify 4 This program is free software; you can redistribute it and/or modify
...@@ -15,47 +15,32 @@ ...@@ -15,47 +15,32 @@
15 with this program; if not, write to the Free Software Foundation, 15 with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17 17
18 #if HAVE_CONFIG_H 18 #ifdef HAVE_CONFIG_H
19 # include <config.h> 19 # include <config.h>
20 #endif 20 #endif
21 21
22 #if !_LIBC 22 #include "getpass.h"
23 # include "getpass.h"
24 #endif
25 23
26 #if _LIBC 24 #include <stdio.h>
27 # define HAVE_STDIO_EXT_H 1 25
28 #endif 26 #if !defined _WIN32
29 27
30 #include <stdbool.h> 28 #include <stdbool.h>
31 29
32 #include <stdio.h>
33 #if HAVE_STDIO_EXT_H 30 #if HAVE_STDIO_EXT_H
34 # include <stdio_ext.h> 31 # include <stdio_ext.h>
35 #else
36 # define __fsetlocking(stream, type) /* empty */
37 #endif 32 #endif
38 #if !_LIBC 33 #if !HAVE___FSETLOCKING
39 # include "getline.h" 34 # define __fsetlocking(stream, type) /* empty */
40 #endif 35 #endif
41 36
42 #include <termios.h> 37 #if HAVE_TERMIOS_H
43 #include <unistd.h> 38 # include <termios.h>
44
45 #if _LIBC
46 # include <wchar.h>
47 #endif 39 #endif
48 40
49 #if _LIBC 41 #include "getline.h"
50 # define NOTCANCEL_MODE "c"
51 #else
52 # define NOTCANCEL_MODE
53 #endif
54 42
55 #if _LIBC 43 #if USE_UNLOCKED_IO
56 # define flockfile(s) _IO_flockfile (s)
57 # define funlockfile(s) _IO_funlockfile (s)
58 #elif USE_UNLOCKED_IO
59 # include "unlocked-io.h" 44 # include "unlocked-io.h"
60 #else 45 #else
61 # if !HAVE_DECL_FFLUSH_UNLOCKED 46 # if !HAVE_DECL_FFLUSH_UNLOCKED
...@@ -80,18 +65,6 @@ ...@@ -80,18 +65,6 @@
80 # endif 65 # endif
81 #endif 66 #endif
82 67
83 #if _LIBC
84 # include <bits/libc-lock.h>
85 #else
86 # define __libc_cleanup_push(function, arg) /* empty */
87 # define __libc_cleanup_pop(execute) /* empty */
88 #endif
89
90 #if !_LIBC
91 # define __getline getline
92 # define __tcgetattr tcgetattr
93 #endif
94
95 /* It is desirable to use this bit on systems that have it. 68 /* It is desirable to use this bit on systems that have it.
96 The only bit of terminal state we want to twiddle is echoing, which is 69 The only bit of terminal state we want to twiddle is echoing, which is
97 done in software; there is no need to change the state of the terminal 70 done in software; there is no need to change the state of the terminal
...@@ -114,7 +87,7 @@ getpass (const char *prompt) ...@@ -114,7 +87,7 @@ getpass (const char *prompt)
114 FILE *tty; 87 FILE *tty;
115 FILE *in, *out; 88 FILE *in, *out;
116 struct termios s, t; 89 struct termios s, t;
117 bool tty_changed; 90 bool tty_changed = false;
118 static char *buf; 91 static char *buf;
119 static size_t bufsize; 92 static size_t bufsize;
120 ssize_t nread; 93 ssize_t nread;
...@@ -122,7 +95,7 @@ getpass (const char *prompt) ...@@ -122,7 +95,7 @@ getpass (const char *prompt)
122 /* Try to write to and read from the terminal if we can. 95 /* Try to write to and read from the terminal if we can.
123 If we can't open the terminal, use stderr and stdin. */ 96 If we can't open the terminal, use stderr and stdin. */
124 97
125 tty = fopen ("/dev/tty", "w+" NOTCANCEL_MODE); 98 tty = fopen ("/dev/tty", "w+");
126 if (tty == NULL) 99 if (tty == NULL)
127 { 100 {
128 in = stdin; 101 in = stdin;
...@@ -136,39 +109,26 @@ getpass (const char *prompt) ...@@ -136,39 +109,26 @@ getpass (const char *prompt)
136 out = in = tty; 109 out = in = tty;
137 } 110 }
138 111
139 /* Make sure the stream we opened is closed even if the thread is
140 canceled. */
141 __libc_cleanup_push (call_fclose, tty);
142
143 flockfile (out); 112 flockfile (out);
144 113
145 /* Turn echoing off if it is on now. */ 114 /* Turn echoing off if it is on now. */
146 115 #if HAVE_TCGETATTR
147 if (__tcgetattr (fileno (in), &t) == 0) 116 if (tcgetattr (fileno (in), &t) == 0)
148 { 117 {
149 /* Save the old one. */ 118 /* Save the old one. */
150 s = t; 119 s = t;
151 /* Tricky, tricky. */ 120 /* Tricky, tricky. */
152 t.c_lflag &= ~(ECHO|ISIG); 121 t.c_lflag &= ~(ECHO | ISIG);
153 tty_changed = (tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &t) == 0); 122 tty_changed = (tcsetattr (fileno (in), TCSAFLUSH | TCSASOFT, &t) == 0);
154 } 123 }
155 else 124 #endif
156 tty_changed = false;
157 125
158 /* Write the prompt. */ 126 /* Write the prompt. */
159 #ifdef USE_IN_LIBIO 127 fputs_unlocked (prompt, out);
160 if (_IO_fwide (out, 0) > 0)
161 __fwprintf (out, L"%s", prompt);
162 else
163 #endif
164 fputs_unlocked (prompt, out);
165 fflush_unlocked (out); 128 fflush_unlocked (out);
166 129
167 /* Read the password. */ 130 /* Read the password. */
168 nread = __getline (&buf, &bufsize, in); 131 nread = getline (&buf, &bufsize, in);
169
170 #if !_LIBC
171 /* As far as is known, glibc doesn't need this no-op fseek. */
172 132
173 /* According to the C standard, input may not be followed by output 133 /* According to the C standard, input may not be followed by output
174 on the same stream without an intervening call to a file 134 on the same stream without an intervening call to a file
...@@ -180,7 +140,6 @@ getpass (const char *prompt) ...@@ -180,7 +140,6 @@ getpass (const char *prompt)
180 from POSIX version to POSIX version, so play it safe and invoke 140 from POSIX version to POSIX version, so play it safe and invoke
181 fseek even if in != out. */ 141 fseek even if in != out. */
182 fseek (out, 0, SEEK_CUR); 142 fseek (out, 0, SEEK_CUR);
183 #endif
184 143
185 if (buf != NULL) 144 if (buf != NULL)
186 { 145 {
...@@ -193,25 +152,75 @@ getpass (const char *prompt) ...@@ -193,25 +152,75 @@ getpass (const char *prompt)
193 if (tty_changed) 152 if (tty_changed)
194 { 153 {
195 /* Write the newline that was not echoed. */ 154 /* Write the newline that was not echoed. */
196 #ifdef USE_IN_LIBIO 155 putc_unlocked ('\n', out);
197 if (_IO_fwide (out, 0) > 0)
198 putwc_unlocked (L'\n', out);
199 else
200 #endif
201 putc_unlocked ('\n', out);
202 } 156 }
203 } 157 }
204 } 158 }
205 159
206 /* Restore the original setting. */ 160 /* Restore the original setting. */
161 #if HAVE_TCSETATTR
207 if (tty_changed) 162 if (tty_changed)
208 (void) tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &s); 163 tcsetattr (fileno (in), TCSAFLUSH | TCSASOFT, &s);
164 #endif
209 165
210 funlockfile (out); 166 funlockfile (out);
211 167
212 __libc_cleanup_pop (0);
213
214 call_fclose (tty); 168 call_fclose (tty);
215 169
216 return buf; 170 return buf;
217 } 171 }
172
173 #else /* WIN32 */
174
175 /* Windows implementation by Martin Lambers <marlam@marlam.de>,
176 improved by Simon Josefsson. */
177
178 /* For PASS_MAX. */
179 #include <limits.h>
180
181 #ifndef PASS_MAX
182 # define PASS_MAX 512
183 #endif
184
185 char *
186 getpass (const char *prompt)
187 {
188 char getpassbuf[PASS_MAX + 1];
189 size_t i = 0;
190 int c;
191
192 if (prompt)
193 {
194 fputs (prompt, stderr);
195 fflush (stderr);
196 }
197
198 for (;;)
199 {
200 c = _getch ();
201 if (c == '\r')
202 {
203 getpassbuf[i] = '\0';
204 break;
205 }
206 else if (i < PASS_MAX)
207 {
208 getpassbuf[i++] = c;
209 }
210
211 if (i >= PASS_MAX)
212 {
213 getpassbuf[i] = '\0';
214 break;
215 }
216 }
217
218 if (prompt)
219 {
220 fputs ("\r\n", stderr);
221 fflush (stderr);
222 }
223
224 return strdup (getpassbuf);
225 }
226 #endif
......
...@@ -53,12 +53,25 @@ ...@@ -53,12 +53,25 @@
53 ? (t) -1 \ 53 ? (t) -1 \
54 : ~ (~ (t) 0 << (sizeof (t) * CHAR_BIT - 1)))) 54 : ~ (~ (t) 0 << (sizeof (t) * CHAR_BIT - 1))))
55 55
56 /* Return zero if T can be determined to be an unsigned type.
57 Otherwise, return 1.
58 When compiling with GCC, INT_STRLEN_BOUND uses this macro to obtain a
59 tighter bound. Otherwise, it overestimates the true bound by one byte
60 when applied to unsigned types of size 2, 4, 16, ... bytes.
61 The symbol signed_type_or_expr__ is private to this header file. */
62 #if __GNUC__ >= 2
63 # define signed_type_or_expr__(t) TYPE_SIGNED (__typeof__ (t))
64 #else
65 # define signed_type_or_expr__(t) 1
66 #endif
67
56 /* Bound on length of the string representing an integer type or expression T. 68 /* Bound on length of the string representing an integer type or expression T.
57 Subtract 1 for the sign bit if t is signed; log10 (2.0) < 146/485; 69 Subtract 1 for the sign bit if T is signed; log10 (2.0) < 146/485;
58 add 1 for integer division truncation; add 1 more for a minus sign 70 add 1 for integer division truncation; add 1 more for a minus sign
59 if needed. */ 71 if needed. */
60 #define INT_STRLEN_BOUND(t) \ 72 #define INT_STRLEN_BOUND(t) \
61 ((sizeof (t) * CHAR_BIT - 1) * 146 / 485 + 2) 73 ((sizeof (t) * CHAR_BIT - signed_type_or_expr__ (t)) * 146 / 485 \
74 + signed_type_or_expr__ (t) + 1)
62 75
63 /* Bound on buffer size needed to represent an integer type or expression T, 76 /* Bound on buffer size needed to represent an integer type or expression T,
64 including the terminating null. */ 77 including the terminating null. */
......
...@@ -51,10 +51,6 @@ ...@@ -51,10 +51,6 @@
51 # endif 51 # endif
52 #endif 52 #endif
53 53
54 #if defined _LIBC && defined USE_IN_LIBIO
55 # include <wchar.h>
56 #endif
57
58 #include <stddef.h> 54 #include <stddef.h>
59 55
60 #ifndef ELIDE_CODE 56 #ifndef ELIDE_CODE
...@@ -433,12 +429,11 @@ print_and_abort (void) ...@@ -433,12 +429,11 @@ print_and_abort (void)
433 happen because the "memory exhausted" message appears in other places 429 happen because the "memory exhausted" message appears in other places
434 like this and the translation should be reused instead of creating 430 like this and the translation should be reused instead of creating
435 a very similar string which requires a separate translation. */ 431 a very similar string which requires a separate translation. */
436 # if defined _LIBC && defined USE_IN_LIBIO 432 # ifdef _LIBC
437 if (_IO_fwide (stderr, 0) > 0) 433 (void) __fxprintf (NULL, "%s\n", _("memory exhausted"));
438 __fwprintf (stderr, L"%s\n", _("memory exhausted")); 434 # else
439 else 435 fprintf (stderr, "%s\n", _("memory exhausted"));
440 # endif 436 # endif
441 fprintf (stderr, "%s\n", _("memory exhausted"));
442 exit (obstack_exit_failure); 437 exit (obstack_exit_failure);
443 } 438 }
444 439
......
1 /* obstack.h - object stack macros 1 /* obstack.h - object stack macros
2 Copyright (C) 1988-1994,1996-1999,2003,2004 Free Software Foundation, Inc. 2 Copyright (C) 1988-1994,1996-1999,2003,2004,2005
3 3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library. Its master source is NOT part of 4 This file is part of the GNU C Library.
5 the C library, however. The master source lives in /gd/gnu/lib.
6
7 NOTE: The canonical source of this file is maintained with the GNU C Library.
8 Bugs can be reported to bug-glibc@gnu.org.
9 5
10 This program is free software; you can redistribute it and/or modify 6 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by 7 it under the terms of the GNU General Public License as published by
...@@ -464,7 +460,7 @@ __extension__ \ ...@@ -464,7 +460,7 @@ __extension__ \
464 (((const void **) ((h)->next_free += sizeof (void *)))[-1] = (aptr)) 460 (((const void **) ((h)->next_free += sizeof (void *)))[-1] = (aptr))
465 461
466 # define obstack_int_grow_fast(h,aint) \ 462 # define obstack_int_grow_fast(h,aint) \
467 (((int *) ((h)->next_free += sizeof (int)))[-1] = (aptr)) 463 (((int *) ((h)->next_free += sizeof (int)))[-1] = (aint))
468 464
469 # define obstack_blank(h,length) \ 465 # define obstack_blank(h,length) \
470 ( (h)->temp.tempint = (length), \ 466 ( (h)->temp.tempint = (length), \
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
17 17
18 /* written by Jim Meyering */ 18 /* written by Jim Meyering */
19 19
20 #if HAVE_CONFIG_H 20 #ifdef HAVE_CONFIG_H
21 # include <config.h> 21 # include <config.h>
22 #endif 22 #endif
23 #undef realloc 23 #undef realloc
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
17 along with this program; if not, write to the Free Software Foundation, 17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 19
20 #if HAVE_CONFIG_H 20 #ifdef HAVE_CONFIG_H
21 # include <config.h> 21 # include <config.h>
22 #endif 22 #endif
23 23
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
17 along with this program; if not, write to the Free Software Foundation, 17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 19
20 #if HAVE_CONFIG_H 20 #ifdef HAVE_CONFIG_H
21 # include <config.h> 21 # include <config.h>
22 #endif 22 #endif
23 23
......
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
19 19
20 /* Written by Jim Meyering. */ 20 /* Written by Jim Meyering. */
21 21
22 #if HAVE_CONFIG_H 22 #ifdef HAVE_CONFIG_H
23 # include <config.h> 23 # include <config.h>
24 #endif 24 #endif
25 25
......
...@@ -3,7 +3,8 @@ ...@@ -3,7 +3,8 @@
3 # This is a modified version of autoconf's AC_FUNC_FNMATCH. 3 # This is a modified version of autoconf's AC_FUNC_FNMATCH.
4 # This file should be simplified after Autoconf 2.57 is required. 4 # This file should be simplified after Autoconf 2.57 is required.
5 5
6 # Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc. 6 # Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 Free Software
7 # Foundation, Inc.
7 # This file is free software; the Free Software Foundation 8 # This file is free software; the Free Software Foundation
8 # gives unlimited permission to copy and/or distribute it, 9 # gives unlimited permission to copy and/or distribute it,
9 # with or without modifications, as long as this notice is preserved. 10 # with or without modifications, as long as this notice is preserved.
...@@ -27,9 +28,15 @@ AC_DEFUN([_AC_FUNC_FNMATCH_IF], ...@@ -27,9 +28,15 @@ AC_DEFUN([_AC_FUNC_FNMATCH_IF],
27 # include <fnmatch.h> 28 # include <fnmatch.h>
28 # define y(a, b, c) (fnmatch (a, b, c) == 0) 29 # define y(a, b, c) (fnmatch (a, b, c) == 0)
29 # define n(a, b, c) (fnmatch (a, b, c) == FNM_NOMATCH) 30 # define n(a, b, c) (fnmatch (a, b, c) == FNM_NOMATCH)
31 static int
32 fnm (char const *pattern, char const *string, int flags)
33 {
34 return fnmatch (pattern, string, flags);
35 }
30 ], 36 ],
31 [exit 37 [exit
32 (!(y ("a*", "abc", 0) 38 (!((fnm ? fnm : fnmatch) ("a*", "", 0) == FNM_NOMATCH
39 && y ("a*", "abc", 0)
33 && n ("d*/*1", "d/s/1", FNM_PATHNAME) 40 && n ("d*/*1", "d/s/1", FNM_PATHNAME)
34 && y ("a\\\\bc", "abc", 0) 41 && y ("a\\\\bc", "abc", 0)
35 && n ("a\\\\bc", "abc", FNM_NOESCAPE) 42 && n ("a\\\\bc", "abc", FNM_NOESCAPE)
...@@ -50,19 +57,19 @@ AS_IF([test $$2 = yes], [$3], [$4]) ...@@ -50,19 +57,19 @@ AS_IF([test $$2 = yes], [$3], [$4])
50 ])# _AC_FUNC_FNMATCH_IF 57 ])# _AC_FUNC_FNMATCH_IF
51 58
52 59
53 # _AC_LIBOBJ_FNMATCH 60 # _MU_LIBOBJ_FNMATCH
54 # ------------------ 61 # ------------------
55 # Prepare the replacement of fnmatch. 62 # Prepare the replacement of fnmatch.
56 AC_DEFUN([_AC_LIBOBJ_FNMATCH], 63 AC_DEFUN([_MU_LIBOBJ_FNMATCH],
57 [AC_REQUIRE([AC_C_CONST])dnl 64 [AC_REQUIRE([AC_C_CONST])dnl
58 AC_REQUIRE([AC_FUNC_ALLOCA])dnl 65 AC_REQUIRE([AC_FUNC_ALLOCA])dnl
59 AC_REQUIRE([AC_TYPE_MBSTATE_T])dnl 66 AC_REQUIRE([AC_TYPE_MBSTATE_T])dnl
60 AC_CHECK_DECLS([getenv]) 67 AC_CHECK_DECLS([getenv])
61 AC_CHECK_FUNCS([btowc mbsrtowcs mempcpy wmemchr wmemcpy wmempcpy]) 68 AC_CHECK_FUNCS([btowc mbsrtowcs mempcpy wmemchr wmemcpy wmempcpy])
62 AC_CHECK_HEADERS([wchar.h wctype.h]) 69 AC_CHECK_HEADERS([wchar.h wctype.h])
63 AC_LIBOBJ([fnmatch]) 70 MU_LIBOBJ([fnmatch])
64 FNMATCH_H=fnmatch.h 71 FNMATCH_H=fnmatch.h
65 ])# _AC_LIBOBJ_FNMATCH 72 ])# _MU_LIBOBJ_FNMATCH
66 73
67 74
68 AC_DEFUN([gl_FUNC_FNMATCH_POSIX], 75 AC_DEFUN([gl_FUNC_FNMATCH_POSIX],
...@@ -70,7 +77,7 @@ AC_DEFUN([gl_FUNC_FNMATCH_POSIX], ...@@ -70,7 +77,7 @@ AC_DEFUN([gl_FUNC_FNMATCH_POSIX],
70 FNMATCH_H= 77 FNMATCH_H=
71 _AC_FUNC_FNMATCH_IF([POSIX], [ac_cv_func_fnmatch_posix], 78 _AC_FUNC_FNMATCH_IF([POSIX], [ac_cv_func_fnmatch_posix],
72 [rm -f lib/fnmatch.h], 79 [rm -f lib/fnmatch.h],
73 [_AC_LIBOBJ_FNMATCH]) 80 [_MU_LIBOBJ_FNMATCH])
74 if test $ac_cv_func_fnmatch_posix != yes; then 81 if test $ac_cv_func_fnmatch_posix != yes; then
75 dnl We must choose a different name for our function, since on ELF systems 82 dnl We must choose a different name for our function, since on ELF systems
76 dnl a broken fnmatch() in libc.so would override our fnmatch() if it is 83 dnl a broken fnmatch() in libc.so would override our fnmatch() if it is
...@@ -90,7 +97,7 @@ AC_DEFUN([gl_FUNC_FNMATCH_GNU], ...@@ -90,7 +97,7 @@ AC_DEFUN([gl_FUNC_FNMATCH_GNU],
90 FNMATCH_H= 97 FNMATCH_H=
91 _AC_FUNC_FNMATCH_IF([GNU], [ac_cv_func_fnmatch_gnu], 98 _AC_FUNC_FNMATCH_IF([GNU], [ac_cv_func_fnmatch_gnu],
92 [rm -f lib/fnmatch.h], 99 [rm -f lib/fnmatch.h],
93 [_AC_LIBOBJ_FNMATCH]) 100 [_MU_LIBOBJ_FNMATCH])
94 if test $ac_cv_func_fnmatch_gnu != yes; then 101 if test $ac_cv_func_fnmatch_gnu != yes; then
95 dnl We must choose a different name for our function, since on ELF systems 102 dnl We must choose a different name for our function, since on ELF systems
96 dnl a broken fnmatch() in libc.so would override our fnmatch() if it is 103 dnl a broken fnmatch() in libc.so would override our fnmatch() if it is
......
1 # getopt.m4 serial 10 1 # getopt.m4 serial 11
2 dnl Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc. 2 dnl Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation 3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it, 4 dnl gives unlimited permission to copy and/or distribute it,
...@@ -27,8 +27,10 @@ AC_DEFUN([gl_GETOPT_SUBSTITUTE_HEADER], ...@@ -27,8 +27,10 @@ AC_DEFUN([gl_GETOPT_SUBSTITUTE_HEADER],
27 27
28 AC_DEFUN([gl_GETOPT_CHECK_HEADERS], 28 AC_DEFUN([gl_GETOPT_CHECK_HEADERS],
29 [ 29 [
30 GETOPT_H= 30 if test -z "$GETOPT_H"; then
31 AC_CHECK_HEADERS([getopt.h], [], [GETOPT_H=getopt.h]) 31 AC_CHECK_HEADERS([getopt.h], [], [GETOPT_H=getopt.h])
32 fi
33
32 if test -z "$GETOPT_H"; then 34 if test -z "$GETOPT_H"; then
33 AC_CHECK_FUNCS([getopt_long_only], [], [GETOPT_H=getopt.h]) 35 AC_CHECK_FUNCS([getopt_long_only], [], [GETOPT_H=getopt.h])
34 fi 36 fi
......
...@@ -35,7 +35,7 @@ AC_DEFUN([gl_FUNC_GETPASS_GNU], ...@@ -35,7 +35,7 @@ AC_DEFUN([gl_FUNC_GETPASS_GNU],
35 35
36 # Prerequisites of lib/getpass.c. 36 # Prerequisites of lib/getpass.c.
37 AC_DEFUN([gl_PREREQ_GETPASS], [ 37 AC_DEFUN([gl_PREREQ_GETPASS], [
38 AC_CHECK_HEADERS_ONCE(stdio_ext.h) 38 AC_CHECK_HEADERS_ONCE(stdio_ext.h termios.h)
39 AC_CHECK_FUNCS_ONCE(__fsetlocking tcgetattr tcsetattr)
39 AC_CHECK_DECLS_ONCE([fflush_unlocked flockfile fputs_unlocked funlockfile putc_unlocked]) 40 AC_CHECK_DECLS_ONCE([fflush_unlocked flockfile fputs_unlocked funlockfile putc_unlocked])
40 :
41 ]) 41 ])
......
1 # inttypes.m4 serial 1 (gettext-0.11.4) 1 # inttypes.m4 serial 1 (gettext-0.11.4)
2 dnl Copyright (C) 1997-2002 Free Software Foundation, Inc. 2 dnl Copyright (C) 1997-2002 Free Software Foundation, Inc.
3 dnl This file is free software, distributed under the terms of the GNU 3 dnl This file is free software; the Free Software Foundation
4 dnl General Public License. As a special exception to the GNU General 4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl Public License, this file may be distributed as part of a program 5 dnl with or without modifications, as long as this notice is preserved.
6 dnl that contains a configuration script generated by Autoconf, under
7 dnl the same distribution terms as the rest of that program.
8 6
9 dnl From Paul Eggert. 7 dnl From Paul Eggert.
10 8
......
1 # longdouble.m4 serial 1 (gettext-0.12) 1 # longdouble.m4 serial 1 (gettext-0.12)
2 dnl Copyright (C) 2002-2003 Free Software Foundation, Inc. 2 dnl Copyright (C) 2002-2003 Free Software Foundation, Inc.
3 dnl This file is free software, distributed under the terms of the GNU 3 dnl This file is free software; the Free Software Foundation
4 dnl General Public License. As a special exception to the GNU General 4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl Public License, this file may be distributed as part of a program 5 dnl with or without modifications, as long as this notice is preserved.
6 dnl that contains a configuration script generated by Autoconf, under
7 dnl the same distribution terms as the rest of that program.
8 6
9 dnl From Bruno Haible. 7 dnl From Bruno Haible.
10 dnl Test whether the compiler supports the 'long double' type. 8 dnl Test whether the compiler supports the 'long double' type.
......
1 # mbchar.m4 serial 1 1 # mbchar.m4 serial 2
2 dnl Copyright (C) 2005 Free Software Foundation, Inc. 2 dnl Copyright (C) 2005 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation 3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it, 4 dnl gives unlimited permission to copy and/or distribute it,
...@@ -10,5 +10,11 @@ dnl From Bruno Haible. ...@@ -10,5 +10,11 @@ dnl From Bruno Haible.
10 AC_DEFUN([gl_MBCHAR], 10 AC_DEFUN([gl_MBCHAR],
11 [ 11 [
12 AC_REQUIRE([AC_GNU_SOURCE]) 12 AC_REQUIRE([AC_GNU_SOURCE])
13 : 13 dnl The following line is that so the user can test
14 dnl HAVE_WCHAR_H && HAVE_WCTYPE_H before #include "mbchar.h".
15 AC_CHECK_HEADERS_ONCE(wchar.h wctype.h)
16 dnl Compile mbchar.c only if HAVE_WCHAR_H && HAVE_WCTYPE_H.
17 if test $ac_cv_header_wchar_h = yes && test $ac_cv_header_wctype_h = yes; then
18 MU_LIBOBJ([mbchar])
19 fi
14 ]) 20 ])
......
1 # md5.m4 serial 7 1 # md5.m4 serial 8
2 dnl Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc. 2 dnl Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation 3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it, 4 dnl gives unlimited permission to copy and/or distribute it,
...@@ -9,9 +9,6 @@ AC_DEFUN([gl_MD5], ...@@ -9,9 +9,6 @@ AC_DEFUN([gl_MD5],
9 MU_LIBSOURCES([md5.c, md5.h]) 9 MU_LIBSOURCES([md5.c, md5.h])
10 MU_LIBOBJ([md5]) 10 MU_LIBOBJ([md5])
11 11
12 dnl Prerequisites of lib/md5.h.
13 AC_REQUIRE([gl_AC_TYPE_UINT32_T])
14
15 dnl Prerequisites of lib/md5.c. 12 dnl Prerequisites of lib/md5.c.
16 AC_REQUIRE([AC_C_BIGENDIAN]) 13 AC_REQUIRE([AC_C_BIGENDIAN])
17 : 14 :
......
1 # minmax.m4 serial 1 1 # minmax.m4 serial 2
2 dnl Copyright (C) 2005 Free Software Foundation, Inc. 2 dnl Copyright (C) 2005 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation 3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it, 4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved. 5 dnl with or without modifications, as long as this notice is preserved.
6 6
7 AC_PREREQ(2.52)
8
7 AC_DEFUN([gl_MINMAX], 9 AC_DEFUN([gl_MINMAX],
8 [ 10 [
9 AC_REQUIRE([gl_PREREQ_MINMAX]) 11 AC_REQUIRE([gl_PREREQ_MINMAX])
...@@ -17,12 +19,13 @@ AC_DEFUN([gl_PREREQ_MINMAX], ...@@ -17,12 +19,13 @@ AC_DEFUN([gl_PREREQ_MINMAX],
17 ]) 19 ])
18 20
19 dnl gl_MINMAX_IN_HEADER(HEADER) 21 dnl gl_MINMAX_IN_HEADER(HEADER)
22 dnl The parameter has to be a literal header name; it cannot be macro,
23 dnl nor a shell variable. (Because autoheader collects only AC_DEFINE
24 dnl invocations with a literal macro name.)
20 AC_DEFUN([gl_MINMAX_IN_HEADER], 25 AC_DEFUN([gl_MINMAX_IN_HEADER],
21 [ 26 [
22 define([header],[translit([$1],[./-], 27 m4_pushdef([header], AS_TR_SH([$1]))
23 [___])]) 28 m4_pushdef([HEADER], AS_TR_CPP([$1]))
24 define([HEADER],[translit([$1],[abcdefghijklmnopqrstuvwxyz./-],
25 [ABCDEFGHIJKLMNOPQRSTUVWXYZ___])])
26 AC_CACHE_CHECK([whether <$1> defines MIN and MAX], 29 AC_CACHE_CHECK([whether <$1> defines MIN and MAX],
27 [gl_cv_minmax_in_]header, 30 [gl_cv_minmax_in_]header,
28 [AC_TRY_COMPILE([#include <$1> 31 [AC_TRY_COMPILE([#include <$1>
...@@ -33,6 +36,6 @@ int x = MIN (42, 17);], [], ...@@ -33,6 +36,6 @@ int x = MIN (42, 17);], [],
33 AC_DEFINE([HAVE_MINMAX_IN_]HEADER, 1, 36 AC_DEFINE([HAVE_MINMAX_IN_]HEADER, 1,
34 [Define to 1 if <$1> defines the MIN and MAX macros.]) 37 [Define to 1 if <$1> defines the MIN and MAX macros.])
35 fi 38 fi
36 undefine([HEADER]) 39 m4_popdef([HEADER])
37 undefine([header]) 40 m4_popdef([header])
38 ]) 41 ])
......
1 #serial 24 1 #serial 30
2 2
3 # Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2003, 2004, 2005 Free 3 # Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2003, 2004, 2005 Free
4 # Software Foundation, Inc. 4 # Software Foundation, Inc.
...@@ -10,121 +10,155 @@ ...@@ -10,121 +10,155 @@
10 dnl Initially derived from code in GNU grep. 10 dnl Initially derived from code in GNU grep.
11 dnl Mostly written by Jim Meyering. 11 dnl Mostly written by Jim Meyering.
12 12
13 AC_PREREQ([2.50])
14
13 AC_DEFUN([gl_REGEX], 15 AC_DEFUN([gl_REGEX],
14 [ 16 [
15 gl_INCLUDED_REGEX([lib/regex.c]) 17 AC_REQUIRE([AC_SYS_LARGEFILE]) dnl for a sufficently-wide off_t
16 ]) 18 AC_DEFINE([_REGEX_LARGE_OFFSETS], 1,
17 19 [Define if you want regoff_t to be at least as wide POSIX requires.])
18 dnl Usage: gl_INCLUDED_REGEX([lib/regex.c]) 20
19 dnl 21 MU_LIBSOURCES(
20 AC_DEFUN([gl_INCLUDED_REGEX], 22 [regcomp.c, regex.c, regex.h,
21 [ 23 regex_internal.c, regex_internal.h, regexec.c])
22 MU_LIBSOURCES( 24
23 [regcomp.c, regex.c, regex.h, 25 AC_ARG_WITH([included-regex],
24 regex_internal.c, regex_internal.h, regexec.c]) 26 [AC_HELP_STRING([--without-included-regex],
25 27 [don't compile regex; this is the default on
26 dnl Even packages that don't use regex.c can use this macro. 28 systems with recent-enough versions of the GNU C
27 dnl Of course, for them it doesn't do anything. 29 Library (use with caution on other systems)])])
28 30
29 # Assume we'll default to using the included regex.c. 31 case $with_included_regex in
30 ac_use_included_regex=yes 32 yes|no) ac_use_included_regex=$with_included_regex
31 33 ;;
32 # However, if the system regex support is good enough that it passes the 34 '')
33 # the following run test, then default to *not* using the included regex.c. 35 # If the system regex support is good enough that it passes the the
36 # following run test, then default to *not* using the included regex.c.
34 # If cross compiling, assume the test would fail and use the included 37 # If cross compiling, assume the test would fail and use the included
35 # regex.c. The first failing regular expression is from `Spencer ere 38 # regex.c. The first failing regular expression is from `Spencer ere
36 # test #75' in grep-2.3. 39 # test #75' in grep-2.3.
37 AC_CACHE_CHECK([for working re_compile_pattern], 40 AC_CACHE_CHECK([for working re_compile_pattern],
38 [gl_cv_func_working_re_compile_pattern], 41 [gl_cv_func_re_compile_pattern_broken],
39 [AC_RUN_IFELSE( 42 [AC_RUN_IFELSE(
40 [AC_LANG_PROGRAM( 43 [AC_LANG_PROGRAM(
41 [AC_INCLUDES_DEFAULT 44 [AC_INCLUDES_DEFAULT
42 #include <regex.h>], 45 #include <regex.h>],
43 [[static struct re_pattern_buffer regex; 46 [[static struct re_pattern_buffer regex;
44 const char *s; 47 const char *s;
45 struct re_registers regs; 48 struct re_registers regs;
46 re_set_syntax (RE_SYNTAX_POSIX_EGREP); 49 /* Use the POSIX-compliant spelling with leading REG_,
47 memset (&regex, 0, sizeof (regex)); 50 rather than the traditional GNU spelling with leading RE_,
48 s = re_compile_pattern ("a[:@:>@:]b\n", 9, &regex); 51 so that we reject older libc implementations. */
49 /* This should fail with _Invalid character class name_ error. */ 52 re_set_syntax (REG_SYNTAX_POSIX_EGREP);
50 if (!s) 53 memset (&regex, 0, sizeof (regex));
51 exit (1); 54 s = re_compile_pattern ("a[:@:>@:]b\n", 9, &regex);
52 55 /* This should fail with _Invalid character class name_ error. */
53 /* This should succeed, but does not for e.g. glibc-2.1.3. */ 56 if (!s)
54 memset (&regex, 0, sizeof (regex)); 57 exit (1);
55 s = re_compile_pattern ("{1", 2, &regex); 58
56 59 /* This should succeed, but does not for e.g. glibc-2.1.3. */
57 if (s) 60 memset (&regex, 0, sizeof (regex));
58 exit (1); 61 s = re_compile_pattern ("{1", 2, &regex);
59 62
60 /* The following example is derived from a problem report 63 if (s)
61 against gawk from Jorge Stolfi <stolfi@ic.unicamp.br>. */ 64 exit (1);
62 memset (&regex, 0, sizeof (regex)); 65
63 s = re_compile_pattern ("[an\371]*n", 7, &regex); 66 /* The following example is derived from a problem report
64 if (s) 67 against gawk from Jorge Stolfi <stolfi@ic.unicamp.br>. */
65 exit (1); 68 memset (&regex, 0, sizeof (regex));
66 69 s = re_compile_pattern ("[an\371]*n", 7, &regex);
67 /* This should match, but does not for e.g. glibc-2.2.1. */ 70 if (s)
68 if (re_match (&regex, "an", 2, 0, &regs) != 2) 71 exit (1);
69 exit (1); 72
70 73 /* This should match, but does not for e.g. glibc-2.2.1. */
71 memset (&regex, 0, sizeof (regex)); 74 if (re_match (&regex, "an", 2, 0, &regs) != 2)
72 s = re_compile_pattern ("x", 1, &regex); 75 exit (1);
73 if (s) 76
74 exit (1); 77 memset (&regex, 0, sizeof (regex));
75 78 s = re_compile_pattern ("x", 1, &regex);
76 /* The version of regex.c in e.g. GNU libc-2.2.93 did not 79 if (s)
77 work with a negative RANGE argument. */ 80 exit (1);
78 if (re_search (&regex, "wxy", 3, 2, -2, &regs) != 1) 81
79 exit (1); 82 /* The version of regex.c in e.g. GNU libc-2.2.93 did not
80 83 work with a negative RANGE argument. */
81 /* The version of regex.c in older versions of gnulib 84 if (re_search (&regex, "wxy", 3, 2, -2, &regs) != 1)
82 * ignored RE_ICASE. Detect that problem too. */ 85 exit (1);
83 memset (&regex, 0, sizeof (regex)); 86
84 re_set_syntax(RE_SYNTAX_EMACS|RE_ICASE); 87 /* The version of regex.c in older versions of gnulib
85 s = re_compile_pattern ("x", 1, &regex); 88 ignored REG_IGNORE_CASE (which was then called RE_ICASE).
86 if (s) 89 Detect that problem too. */
87 exit (1); 90 memset (&regex, 0, sizeof (regex));
88 91 re_set_syntax (REG_SYNTAX_EMACS | REG_IGNORE_CASE);
89 if (re_search (&regex, "WXY", 3, 0, 3, &regs) < 0) 92 s = re_compile_pattern ("x", 1, &regex);
90 exit (1); 93 if (s)
91 94 exit (1);
92 /* REG_STARTEND was added to glibc on 2004-01-15. 95
93 Reject older versions. */ 96 if (re_search (&regex, "WXY", 3, 0, 3, &regs) < 0)
94 if (! REG_STARTEND) 97 exit (1);
95 exit (1); 98
96 99 /* REG_STARTEND was added to glibc on 2004-01-15.
97 exit (0);]])], 100 Reject older versions. */
98 [gl_cv_func_working_re_compile_pattern=yes], 101 if (! REG_STARTEND)
99 [gl_cv_func_working_re_compile_pattern=no], 102 exit (1);
100 dnl When crosscompiling, assume it is broken. 103
101 [gl_cv_func_working_re_compile_pattern=no])]) 104 /* Reject hosts whose regoff_t values are too narrow.
102 if test $gl_cv_func_working_re_compile_pattern = yes; then 105 These include glibc 2.3.5 on hosts with 64-bit off_t
103 ac_use_included_regex=no 106 and 32-bit int, and Solaris 10 on hosts with 32-bit int
104 fi 107 and _FILE_OFFSET_BITS=64. */
105 108 if (sizeof (regoff_t) < sizeof (off_t))
106 test -n "$1" || AC_MSG_ERROR([missing argument]) 109 exit (1);
107 m4_syscmd([test -f '$1']) 110
108 ifelse(m4_sysval, 0, 111 exit (0);]])],
109 [ 112 [gl_cv_func_re_compile_pattern_broken=no],
110 AC_ARG_WITH([included-regex], 113 [gl_cv_func_re_compile_pattern_broken=yes],
111 [ --without-included-regex don't compile regex; this is the default on 114 dnl When crosscompiling, assume it is broken.
112 systems with recent-enough versions of the GNU C 115 [gl_cv_func_re_compile_pattern_broken=yes])])
113 Library (use with caution on other systems)], 116 ac_use_included_regex=$gl_cv_func_re_compile_pattern_broken
114 [gl_with_regex=$withval], 117 ;;
115 [gl_with_regex=$ac_use_included_regex]) 118 *) AC_MSG_ERROR([Invalid value for --with-included-regex: $with_included_regex])
116 if test "X$gl_with_regex" = Xyes; then 119 ;;
117 MU_LIBOBJ([regex]) 120 esac
118 gl_PREREQ_REGEX 121
119 fi 122 if test $ac_use_included_regex = yes; then
120 ], 123 AC_DEFINE([re_syntax_options], [rpl_re_syntax_options],
121 ) 124 [Define to rpl_re_syntax_options if the replacement should be used.])
122 ] 125 AC_DEFINE([re_set_syntax], [rpl_re_set_syntax],
123 ) 126 [Define to rpl_re_set_syntax if the replacement should be used.])
127 AC_DEFINE([re_compile_pattern], [rpl_re_compile_pattern],
128 [Define to rpl_re_compile_pattern if the replacement should be used.])
129 AC_DEFINE([re_compile_fastmap], [rpl_re_compile_fastmap],
130 [Define to rpl_re_compile_fastmap if the replacement should be used.])
131 AC_DEFINE([re_search], [rpl_re_search],
132 [Define to rpl_re_search if the replacement should be used.])
133 AC_DEFINE([re_search_2], [rpl_re_search_2],
134 [Define to rpl_re_search_2 if the replacement should be used.])
135 AC_DEFINE([re_match], [rpl_re_match],
136 [Define to rpl_re_match if the replacement should be used.])
137 AC_DEFINE([re_match_2], [rpl_re_match_2],
138 [Define to rpl_re_match_2 if the replacement should be used.])
139 AC_DEFINE([re_set_registers], [rpl_re_set_registers],
140 [Define to rpl_re_set_registers if the replacement should be used.])
141 AC_DEFINE([re_comp], [rpl_re_comp],
142 [Define to rpl_re_comp if the replacement should be used.])
143 AC_DEFINE([re_exec], [rpl_re_exec],
144 [Define to rpl_re_exec if the replacement should be used.])
145 AC_DEFINE([regcomp], [rpl_regcomp],
146 [Define to rpl_regcomp if the replacement should be used.])
147 AC_DEFINE([regexec], [rpl_regexec],
148 [Define to rpl_regexec if the replacement should be used.])
149 AC_DEFINE([regerror], [rpl_regerror],
150 [Define to rpl_regerror if the replacement should be used.])
151 AC_DEFINE([regfree], [rpl_regfree],
152 [Define to rpl_regfree if the replacement should be used.])
153 MU_LIBOBJ([regex])
154 gl_PREREQ_REGEX
155 fi
156 ])
124 157
125 # Prerequisites of lib/regex.c and lib/regex_internal.c. 158 # Prerequisites of lib/regex.c and lib/regex_internal.c.
126 AC_DEFUN([gl_PREREQ_REGEX], 159 AC_DEFUN([gl_PREREQ_REGEX],
127 [ 160 [
161 AC_REQUIRE([AC_GNU_SOURCE])
128 AC_REQUIRE([gl_C_RESTRICT]) 162 AC_REQUIRE([gl_C_RESTRICT])
129 AC_REQUIRE([AM_LANGINFO_CODESET]) 163 AC_REQUIRE([AM_LANGINFO_CODESET])
130 AC_CHECK_HEADERS_ONCE([locale.h wchar.h wctype.h]) 164 AC_CHECK_HEADERS_ONCE([locale.h wchar.h wctype.h])
......
1 # signed.m4 serial 1 (gettext-0.10.40) 1 # signed.m4 serial 1 (gettext-0.10.40)
2 dnl Copyright (C) 2001-2002 Free Software Foundation, Inc. 2 dnl Copyright (C) 2001-2002 Free Software Foundation, Inc.
3 dnl This file is free software, distributed under the terms of the GNU 3 dnl This file is free software; the Free Software Foundation
4 dnl General Public License. As a special exception to the GNU General 4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl Public License, this file may be distributed as part of a program 5 dnl with or without modifications, as long as this notice is preserved.
6 dnl that contains a configuration script generated by Autoconf, under
7 dnl the same distribution terms as the rest of that program.
8 6
9 dnl From Bruno Haible. 7 dnl From Bruno Haible.
10 8
......
1 # size_max.m4 serial 2 1 # size_max.m4 serial 3
2 dnl Copyright (C) 2003 Free Software Foundation, Inc. 2 dnl Copyright (C) 2003, 2005 Free Software Foundation, Inc.
3 dnl This file is free software, distributed under the terms of the GNU 3 dnl This file is free software; the Free Software Foundation
4 dnl General Public License. As a special exception to the GNU General 4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl Public License, this file may be distributed as part of a program 5 dnl with or without modifications, as long as this notice is preserved.
6 dnl that contains a configuration script generated by Autoconf, under
7 dnl the same distribution terms as the rest of that program.
8 6
9 dnl From Bruno Haible. 7 dnl From Bruno Haible.
10 8
...@@ -28,9 +26,9 @@ Found it ...@@ -28,9 +26,9 @@ Found it
28 dnl than the type 'unsigned long'. 26 dnl than the type 'unsigned long'.
29 dnl The _AC_COMPUTE_INT macro works up to LONG_MAX, since it uses 'expr', 27 dnl The _AC_COMPUTE_INT macro works up to LONG_MAX, since it uses 'expr',
30 dnl which is guaranteed to work from LONG_MIN to LONG_MAX. 28 dnl which is guaranteed to work from LONG_MIN to LONG_MAX.
31 _AC_COMPUTE_INT([~(size_t)0 / 10], res_hi, 29 _AC_COMPUTE_INT([(size_t)~(size_t)0 / 10], res_hi,
32 [#include <stddef.h>], result=?) 30 [#include <stddef.h>], result=?)
33 _AC_COMPUTE_INT([~(size_t)0 % 10], res_lo, 31 _AC_COMPUTE_INT([(size_t)~(size_t)0 % 10], res_lo,
34 [#include <stddef.h>], result=?) 32 [#include <stddef.h>], result=?)
35 _AC_COMPUTE_INT([sizeof (size_t) <= sizeof (unsigned int)], fits_in_uint, 33 _AC_COMPUTE_INT([sizeof (size_t) <= sizeof (unsigned int)], fits_in_uint,
36 [#include <stddef.h>], result=?) 34 [#include <stddef.h>], result=?)
...@@ -50,7 +48,7 @@ Found it ...@@ -50,7 +48,7 @@ Found it
50 fi 48 fi
51 else 49 else
52 dnl Shouldn't happen, but who knows... 50 dnl Shouldn't happen, but who knows...
53 result='~(size_t)0' 51 result='((size_t)~(size_t)0)'
54 fi 52 fi
55 fi 53 fi
56 AC_MSG_RESULT([$result]) 54 AC_MSG_RESULT([$result])
......
1 # Check for stdbool.h that conforms to C99. 1 # Check for stdbool.h that conforms to C99.
2 2
3 dnl Copyright (C) 2002-2004 Free Software Foundation, Inc. 3 dnl Copyright (C) 2002-2005 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation 4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it, 5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved. 6 dnl with or without modifications, as long as this notice is preserved.
...@@ -28,6 +28,9 @@ AC_DEFUN([AM_STDBOOL_H], ...@@ -28,6 +28,9 @@ AC_DEFUN([AM_STDBOOL_H],
28 AC_SUBST([HAVE__BOOL]) 28 AC_SUBST([HAVE__BOOL])
29 ]) 29 ])
30 30
31 # AM_STDBOOL_H will be renamed to gl_STDBOOL_H in the future.
32 AC_DEFUN([gl_STDBOOL_H], [AM_STDBOOL_H])
33
31 # This macro is only needed in autoconf <= 2.59. Newer versions of autoconf 34 # This macro is only needed in autoconf <= 2.59. Newer versions of autoconf
32 # have this macro built-in. 35 # have this macro built-in.
33 36
...@@ -70,10 +73,12 @@ AC_DEFUN([AC_HEADER_STDBOOL], ...@@ -70,10 +73,12 @@ AC_DEFUN([AC_HEADER_STDBOOL],
70 enum { j = false, k = true, l = false * true, m = true * 256 }; 73 enum { j = false, k = true, l = false * true, m = true * 256 };
71 _Bool n[m]; 74 _Bool n[m];
72 char o[sizeof n == m * sizeof n[0] ? 1 : -1]; 75 char o[sizeof n == m * sizeof n[0] ? 1 : -1];
76 char p[-1 - (_Bool) 0 < 0 && -1 - (bool) 0 < 0 ? 1 : -1];
73 ], 77 ],
74 [ 78 [
75 return (!a + !b + !c + !d + !e + !f + !g + !h + !i + !j + !k + !l 79 /* Refer to every declared value, to avoid compiler optimizations. */
76 + !m + !n + !o); 80 return (!a + !b + !c + !d + !e + !f + !g + !h + !i + !!j + !k + !!l
81 + !m + !n + !o + !p);
77 ], 82 ],
78 [ac_cv_header_stdbool_h=yes], 83 [ac_cv_header_stdbool_h=yes],
79 [ac_cv_header_stdbool_h=no])]) 84 [ac_cv_header_stdbool_h=no])])
......
1 # strcase.m4 serial 2 1 # strcase.m4 serial 3
2 dnl Copyright (C) 2002, 2005 Free Software Foundation, Inc. 2 dnl Copyright (C) 2002, 2005 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation 3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it, 4 dnl gives unlimited permission to copy and/or distribute it,
...@@ -29,7 +29,8 @@ AC_DEFUN([gl_FUNC_STRNCASECMP], ...@@ -29,7 +29,8 @@ AC_DEFUN([gl_FUNC_STRNCASECMP],
29 29
30 # Prerequisites of lib/strcasecmp.c. 30 # Prerequisites of lib/strcasecmp.c.
31 AC_DEFUN([gl_PREREQ_STRCASECMP], [ 31 AC_DEFUN([gl_PREREQ_STRCASECMP], [
32 gl_FUNC_MBRTOWC 32 AC_REQUIRE([gl_FUNC_MBRTOWC])
33 :
33 ]) 34 ])
34 35
35 # Prerequisites of lib/strncasecmp.c. 36 # Prerequisites of lib/strncasecmp.c.
......
1 # wchar_t.m4 serial 1 (gettext-0.12) 1 # wchar_t.m4 serial 1 (gettext-0.12)
2 dnl Copyright (C) 2002-2003 Free Software Foundation, Inc. 2 dnl Copyright (C) 2002-2003 Free Software Foundation, Inc.
3 dnl This file is free software, distributed under the terms of the GNU 3 dnl This file is free software; the Free Software Foundation
4 dnl General Public License. As a special exception to the GNU General 4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl Public License, this file may be distributed as part of a program 5 dnl with or without modifications, as long as this notice is preserved.
6 dnl that contains a configuration script generated by Autoconf, under
7 dnl the same distribution terms as the rest of that program.
8 6
9 dnl From Bruno Haible. 7 dnl From Bruno Haible.
10 dnl Test whether <stddef.h> has the 'wchar_t' type. 8 dnl Test whether <stddef.h> has the 'wchar_t' type.
......
1 # wint_t.m4 serial 1 (gettext-0.12) 1 # wint_t.m4 serial 1 (gettext-0.12)
2 dnl Copyright (C) 2003 Free Software Foundation, Inc. 2 dnl Copyright (C) 2003 Free Software Foundation, Inc.
3 dnl This file is free software, distributed under the terms of the GNU 3 dnl This file is free software; the Free Software Foundation
4 dnl General Public License. As a special exception to the GNU General 4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl Public License, this file may be distributed as part of a program 5 dnl with or without modifications, as long as this notice is preserved.
6 dnl that contains a configuration script generated by Autoconf, under
7 dnl the same distribution terms as the rest of that program.
8 6
9 dnl From Bruno Haible. 7 dnl From Bruno Haible.
10 dnl Test whether <wchar.h> has the 'wint_t' type. 8 dnl Test whether <wchar.h> has the 'wint_t' type.
......
1 # xsize.m4 serial 2 1 # xsize.m4 serial 3
2 dnl Copyright (C) 2003 Free Software Foundation, Inc. 2 dnl Copyright (C) 2003-2004 Free Software Foundation, Inc.
3 dnl This file is free software, distributed under the terms of the GNU 3 dnl This file is free software; the Free Software Foundation
4 dnl General Public License. As a special exception to the GNU General 4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl Public License, this file may be distributed as part of a program 5 dnl with or without modifications, as long as this notice is preserved.
6 dnl that contains a configuration script generated by Autoconf, under
7 dnl the same distribution terms as the rest of that program.
8 6
9 AC_DEFUN([gl_XSIZE], 7 AC_DEFUN([gl_XSIZE],
10 [ 8 [
11 dnl Prerequisites of lib/xsize.h. 9 dnl Prerequisites of lib/xsize.h.
12 AC_REQUIRE([gl_SIZE_MAX]) 10 AC_REQUIRE([gl_SIZE_MAX])
11 AC_REQUIRE([AC_C_INLINE])
13 AC_CHECK_HEADERS(stdint.h) 12 AC_CHECK_HEADERS(stdint.h)
14 ]) 13 ])
......
...@@ -15,10 +15,10 @@ ...@@ -15,10 +15,10 @@
15 15
16 You should have received a copy of the GNU General Public License along 16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, 17 with this program; if not, write to the Free Software Foundation,
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 19
20 #ifdef HAVE_CONFIG_H 20 #ifdef HAVE_CONFIG_H
21 #include <config.h> 21 # include <config.h>
22 #endif 22 #endif
23 23
24 #include <sysexits.h> 24 #include <sysexits.h>
......
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
21 don't have that. */ 21 don't have that. */
22 22
23 #ifdef HAVE_CONFIG_H 23 #ifdef HAVE_CONFIG_H
24 #include <config.h> 24 # include <config.h>
25 #endif 25 #endif
26 26
27 #include <stdlib.h> 27 #include <stdlib.h>
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
15 15
16 You should have received a copy of the GNU General Public License along 16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, 17 with this program; if not, write to the Free Software Foundation,
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 19
20 /* This package emulates glibc `line_wrap_stream' semantics for systems that 20 /* This package emulates glibc `line_wrap_stream' semantics for systems that
21 don't have that. If the system does have it, it is just a wrapper for 21 don't have that. If the system does have it, it is just a wrapper for
...@@ -25,10 +25,6 @@ ...@@ -25,10 +25,6 @@
25 #ifndef _ARGP_FMTSTREAM_H 25 #ifndef _ARGP_FMTSTREAM_H
26 #define _ARGP_FMTSTREAM_H 26 #define _ARGP_FMTSTREAM_H
27 27
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31
32 #include <stdio.h> 28 #include <stdio.h>
33 #include <string.h> 29 #include <string.h>
34 #include <unistd.h> 30 #include <unistd.h>
......
...@@ -15,10 +15,10 @@ ...@@ -15,10 +15,10 @@
15 15
16 You should have received a copy of the GNU General Public License along 16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, 17 with this program; if not, write to the Free Software Foundation,
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 19
20 #ifdef HAVE_CONFIG_H 20 #ifdef HAVE_CONFIG_H
21 #include <config.h> 21 # include <config.h>
22 #endif 22 #endif
23 23
24 #define ARGP_FS_EI 24 #define ARGP_FS_EI
......
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
22 #endif 22 #endif
23 23
24 #ifdef HAVE_CONFIG_H 24 #ifdef HAVE_CONFIG_H
25 #include <config.h> 25 # include <config.h>
26 #endif 26 #endif
27 27
28 #include <alloca.h> 28 #include <alloca.h>
......
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 19
20 #ifdef HAVE_CONFIG_H 20 #ifdef HAVE_CONFIG_H
21 #include <config.h> 21 # include <config.h>
22 #endif 22 #endif
23 23
24 #include <alloca.h> 24 #include <alloca.h>
...@@ -81,8 +81,8 @@ static const struct argp_option argp_default_options[] = ...@@ -81,8 +81,8 @@ static const struct argp_option argp_default_options[] =
81 { 81 {
82 {"help", '?', 0, 0, N_("Give this help list"), -1}, 82 {"help", '?', 0, 0, N_("Give this help list"), -1},
83 {"usage", OPT_USAGE, 0, 0, N_("Give a short usage message"), 0}, 83 {"usage", OPT_USAGE, 0, 0, N_("Give a short usage message"), 0},
84 {"program-name",OPT_PROGNAME,"NAME", OPTION_HIDDEN, N_("Set the program name"), 0}, 84 {"program-name",OPT_PROGNAME,N_("NAME"), OPTION_HIDDEN, N_("Set the program name"), 0},
85 {"HANG", OPT_HANG, "SECS", OPTION_ARG_OPTIONAL | OPTION_HIDDEN, 85 {"HANG", OPT_HANG, N_("SECS"), OPTION_ARG_OPTIONAL | OPTION_HIDDEN,
86 N_("Hang for SECS seconds (default 3600)"), 0}, 86 N_("Hang for SECS seconds (default 3600)"), 0},
87 {NULL, 0, 0, 0, NULL, 0} 87 {NULL, 0, 0, 0, NULL, 0}
88 }; 88 };
......
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 19
20 #ifdef HAVE_CONFIG_H 20 #ifdef HAVE_CONFIG_H
21 #include <config.h> 21 # include <config.h>
22 #endif 22 #endif
23 23
24 #include "argp.h" 24 #include "argp.h"
......
...@@ -15,10 +15,10 @@ ...@@ -15,10 +15,10 @@
15 15
16 You should have received a copy of the GNU General Public License along 16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, 17 with this program; if not, write to the Free Software Foundation,
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 19
20 #ifdef HAVE_CONFIG_H 20 #ifdef HAVE_CONFIG_H
21 #include <config.h> 21 # include <config.h>
22 #endif 22 #endif
23 23
24 #if defined _LIBC || defined HAVE_FEATURES_H 24 #if defined _LIBC || defined HAVE_FEATURES_H
......
...@@ -19,15 +19,22 @@ ...@@ -19,15 +19,22 @@
19 19
20 /* Ported from glibc by Simon Josefsson. */ 20 /* Ported from glibc by Simon Josefsson. */
21 21
22 #if HAVE_CONFIG_H 22 #ifdef HAVE_CONFIG_H
23 # include <config.h> 23 # include <config.h>
24 #endif 24 #endif
25 25
26 #include "getdelim.h"
27
28 #include <limits.h>
26 #include <stdlib.h> 29 #include <stdlib.h>
27 #include <errno.h> 30 #include <errno.h>
28 31
29 #include "getdelim.h" 32 #ifndef SIZE_MAX
30 33 # define SIZE_MAX ((size_t) -1)
34 #endif
35 #ifndef SSIZE_MAX
36 # define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
37 #endif
31 #if !HAVE_FLOCKFILE 38 #if !HAVE_FLOCKFILE
32 # undef flockfile 39 # undef flockfile
33 # define flockfile(x) ((void) 0) 40 # define flockfile(x) ((void) 0)
...@@ -46,9 +53,8 @@ ...@@ -46,9 +53,8 @@
46 ssize_t 53 ssize_t
47 getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp) 54 getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp)
48 { 55 {
49 int result; 56 ssize_t result;
50 ssize_t cur_len = 0; 57 size_t cur_len = 0;
51 ssize_t len;
52 58
53 if (lineptr == NULL || n == NULL || fp == NULL) 59 if (lineptr == NULL || n == NULL || fp == NULL)
54 { 60 {
...@@ -71,20 +77,26 @@ getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp) ...@@ -71,20 +77,26 @@ getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp)
71 77
72 for (;;) 78 for (;;)
73 { 79 {
74 char *t;
75 int i; 80 int i;
76 81
77 i = getc (fp); 82 i = getc (fp);
78 if (i == EOF) 83 if (i == EOF)
79 break; 84 {
85 result = -1;
86 break;
87 }
80 88
81 /* Make enough space for len+1 (for final NUL) bytes. */ 89 /* Make enough space for len+1 (for final NUL) bytes. */
82 if (cur_len + 1 >= *n) 90 if (cur_len + 1 >= *n)
83 { 91 {
84 size_t needed = 2 * (cur_len + 1) + 1; /* Be generous. */ 92 size_t needed_max =
93 SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX;
94 size_t needed = 2 * *n + 1; /* Be generous. */
85 char *new_lineptr; 95 char *new_lineptr;
86 96
87 if (needed < cur_len) 97 if (needed_max < needed)
98 needed = needed_max;
99 if (cur_len + 1 >= needed)
88 { 100 {
89 result = -1; 101 result = -1;
90 goto unlock_return; 102 goto unlock_return;
...@@ -108,7 +120,7 @@ getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp) ...@@ -108,7 +120,7 @@ getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp)
108 break; 120 break;
109 } 121 }
110 (*lineptr)[cur_len] = '\0'; 122 (*lineptr)[cur_len] = '\0';
111 result = cur_len; 123 result = cur_len ? cur_len : result;
112 124
113 unlock_return: 125 unlock_return:
114 funlockfile (fp); 126 funlockfile (fp);
......
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
18 18
19 /* Written by Simon Josefsson. */ 19 /* Written by Simon Josefsson. */
20 20
21 #if HAVE_CONFIG_H 21 #ifdef HAVE_CONFIG_H
22 # include <config.h> 22 # include <config.h>
23 #endif 23 #endif
24 24
......
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 19
20 #ifdef HAVE_CONFIG_H 20 #ifdef HAVE_CONFIG_H
21 #include <config.h> 21 # include <config.h>
22 #endif 22 #endif
23 23
24 #ifdef _LIBC 24 #ifdef _LIBC
......
1 /* Declarations for getopt. 1 /* Declarations for getopt.
2 Copyright (C) 1989-1994,1996-1999,2001,2003,2004 2 Copyright (C) 1989-1994,1996-1999,2001,2003,2004,2005
3 Free Software Foundation, Inc. 3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library. 4 This file is part of the GNU C Library.
5 5
...@@ -34,9 +34,7 @@ ...@@ -34,9 +34,7 @@
34 #if defined __GETOPT_PREFIX && !defined __need_getopt 34 #if defined __GETOPT_PREFIX && !defined __need_getopt
35 # include <stdlib.h> 35 # include <stdlib.h>
36 # include <stdio.h> 36 # include <stdio.h>
37 # if HAVE_UNISTD_H 37 # include <unistd.h>
38 # include <unistd.h>
39 # endif
40 # undef __need_getopt 38 # undef __need_getopt
41 # undef getopt 39 # undef getopt
42 # undef getopt_long 40 # undef getopt_long
......
...@@ -182,21 +182,29 @@ typedef struct mbchar mbchar_t; ...@@ -182,21 +182,29 @@ typedef struct mbchar mbchar_t;
182 #define mb_iseq(mbc, sc) ((mbc).wc_valid && (mbc).wc == (sc)) 182 #define mb_iseq(mbc, sc) ((mbc).wc_valid && (mbc).wc == (sc))
183 #define mb_isnul(mbc) ((mbc).wc_valid && (mbc).wc == 0) 183 #define mb_isnul(mbc) ((mbc).wc_valid && (mbc).wc == 0)
184 #define mb_cmp(mbc1, mbc2) \ 184 #define mb_cmp(mbc1, mbc2) \
185 ((mbc1).wc_valid && (mbc2).wc_valid \ 185 ((mbc1).wc_valid \
186 ? (int) (mbc1).wc - (int) (mbc2).wc \ 186 ? ((mbc2).wc_valid \
187 : (mbc1).bytes == (mbc2).bytes \ 187 ? (int) (mbc1).wc - (int) (mbc2).wc \
188 ? memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes) \ 188 : -1) \
189 : (mbc1).bytes < (mbc2).bytes \ 189 : ((mbc2).wc_valid \
190 ? (memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes) > 0 ? 1 : -1) \ 190 ? 1 \
191 : (memcmp ((mbc1).ptr, (mbc2).ptr, (mbc2).bytes) >= 0 ? 1 : -1)) 191 : (mbc1).bytes == (mbc2).bytes \
192 ? memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes) \
193 : (mbc1).bytes < (mbc2).bytes \
194 ? (memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes) > 0 ? 1 : -1) \
195 : (memcmp ((mbc1).ptr, (mbc2).ptr, (mbc2).bytes) >= 0 ? 1 : -1)))
192 #define mb_casecmp(mbc1, mbc2) \ 196 #define mb_casecmp(mbc1, mbc2) \
193 ((mbc1).wc_valid && (mbc2).wc_valid \ 197 ((mbc1).wc_valid \
194 ? (int) towlower ((mbc1).wc) - (int) towlower ((mbc2).wc) \ 198 ? ((mbc2).wc_valid \
195 : (mbc1).bytes == (mbc2).bytes \ 199 ? (int) towlower ((mbc1).wc) - (int) towlower ((mbc2).wc) \
196 ? memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes) \ 200 : -1) \
197 : (mbc1).bytes < (mbc2).bytes \ 201 : ((mbc2).wc_valid \
198 ? (memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes) > 0 ? 1 : -1) \ 202 ? 1 \
199 : (memcmp ((mbc1).ptr, (mbc2).ptr, (mbc2).bytes) >= 0 ? 1 : -1)) 203 : (mbc1).bytes == (mbc2).bytes \
204 ? memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes) \
205 : (mbc1).bytes < (mbc2).bytes \
206 ? (memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes) > 0 ? 1 : -1) \
207 : (memcmp ((mbc1).ptr, (mbc2).ptr, (mbc2).bytes) >= 0 ? 1 : -1)))
200 #define mb_equal(mbc1, mbc2) \ 208 #define mb_equal(mbc1, mbc2) \
201 ((mbc1).wc_valid && (mbc2).wc_valid \ 209 ((mbc1).wc_valid && (mbc2).wc_valid \
202 ? (mbc1).wc == (mbc2).wc \ 210 ? (mbc1).wc == (mbc2).wc \
......
1 /* md5.c - Functions to compute MD5 message digest of files or memory blocks 1 /* Functions to compute MD5 message digest of files or memory blocks.
2 according to the definition of MD5 in RFC 1321 from April 1992. 2 according to the definition of MD5 in RFC 1321 from April 1992.
3 Copyright (C) 1995, 1996, 2001, 2003, 2004 Free Software Foundation, Inc. 3 Copyright (C) 1995,1996,1997,1999,2000,2001,2005
4 NOTE: The canonical source of this file is maintained with the GNU C 4 Free Software Foundation, Inc.
5 Library. Bugs can be reported to bug-glibc@prep.ai.mit.edu. 5 This file is part of the GNU C Library.
6 6
7 This program is free software; you can redistribute it and/or modify it 7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the 8 under the terms of the GNU General Public License as published by the
...@@ -27,7 +27,9 @@ ...@@ -27,7 +27,9 @@
27 #include "md5.h" 27 #include "md5.h"
28 28
29 #include <stddef.h> 29 #include <stddef.h>
30 #include <stdlib.h>
30 #include <string.h> 31 #include <string.h>
32 #include <sys/types.h>
31 33
32 #if USE_UNLOCKED_IO 34 #if USE_UNLOCKED_IO
33 # include "unlocked-io.h" 35 # include "unlocked-io.h"
...@@ -57,10 +59,8 @@ ...@@ -57,10 +59,8 @@
57 #endif 59 #endif
58 60
59 #define BLOCKSIZE 4096 61 #define BLOCKSIZE 4096
60 /* Ensure that BLOCKSIZE is a multiple of 64. */
61 #if BLOCKSIZE % 64 != 0 62 #if BLOCKSIZE % 64 != 0
62 /* FIXME-someday (soon?): use #error instead of this kludge. */ 63 # error "invalid BLOCKSIZE"
63 "invalid BLOCKSIZE"
64 #endif 64 #endif
65 65
66 /* This array contains the bytes used to pad the buffer to the next 66 /* This array contains the bytes used to pad the buffer to the next
...@@ -90,10 +90,10 @@ md5_init_ctx (struct md5_ctx *ctx) ...@@ -90,10 +90,10 @@ md5_init_ctx (struct md5_ctx *ctx)
90 void * 90 void *
91 md5_read_ctx (const struct md5_ctx *ctx, void *resbuf) 91 md5_read_ctx (const struct md5_ctx *ctx, void *resbuf)
92 { 92 {
93 ((md5_uint32 *) resbuf)[0] = SWAP (ctx->A); 93 ((uint32_t *) resbuf)[0] = SWAP (ctx->A);
94 ((md5_uint32 *) resbuf)[1] = SWAP (ctx->B); 94 ((uint32_t *) resbuf)[1] = SWAP (ctx->B);
95 ((md5_uint32 *) resbuf)[2] = SWAP (ctx->C); 95 ((uint32_t *) resbuf)[2] = SWAP (ctx->C);
96 ((md5_uint32 *) resbuf)[3] = SWAP (ctx->D); 96 ((uint32_t *) resbuf)[3] = SWAP (ctx->D);
97 97
98 return resbuf; 98 return resbuf;
99 } 99 }
...@@ -107,24 +107,22 @@ void * ...@@ -107,24 +107,22 @@ void *
107 md5_finish_ctx (struct md5_ctx *ctx, void *resbuf) 107 md5_finish_ctx (struct md5_ctx *ctx, void *resbuf)
108 { 108 {
109 /* Take yet unprocessed bytes into account. */ 109 /* Take yet unprocessed bytes into account. */
110 md5_uint32 bytes = ctx->buflen; 110 uint32_t bytes = ctx->buflen;
111 size_t pad; 111 size_t size = (bytes < 56) ? 64 / 4 : 64 * 2 / 4;
112 112
113 /* Now count remaining bytes. */ 113 /* Now count remaining bytes. */
114 ctx->total[0] += bytes; 114 ctx->total[0] += bytes;
115 if (ctx->total[0] < bytes) 115 if (ctx->total[0] < bytes)
116 ++ctx->total[1]; 116 ++ctx->total[1];
117 117
118 pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
119 memcpy (&ctx->buffer[bytes], fillbuf, pad);
120
121 /* Put the 64-bit file length in *bits* at the end of the buffer. */ 118 /* Put the 64-bit file length in *bits* at the end of the buffer. */
122 *(md5_uint32 *) &ctx->buffer[bytes + pad] = SWAP (ctx->total[0] << 3); 119 ctx->buffer[size - 2] = SWAP (ctx->total[0] << 3);
123 *(md5_uint32 *) &ctx->buffer[bytes + pad + 4] = SWAP ((ctx->total[1] << 3) | 120 ctx->buffer[size - 1] = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29));
124 (ctx->total[0] >> 29)); 121
122 memcpy (&((char *) ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes);
125 123
126 /* Process last bytes. */ 124 /* Process last bytes. */
127 md5_process_block (ctx->buffer, bytes + pad + 8, ctx); 125 md5_process_block (ctx->buffer, size * 4, ctx);
128 126
129 return md5_read_ctx (ctx, resbuf); 127 return md5_read_ctx (ctx, resbuf);
130 } 128 }
...@@ -146,8 +144,8 @@ md5_stream (FILE *stream, void *resblock) ...@@ -146,8 +144,8 @@ md5_stream (FILE *stream, void *resblock)
146 while (1) 144 while (1)
147 { 145 {
148 /* We read the file in blocks of BLOCKSIZE bytes. One call of the 146 /* We read the file in blocks of BLOCKSIZE bytes. One call of the
149 computation function processes the whole buffer so that with the 147 computation function processes the whole buffer so that with the
150 next round of the loop another block can be read. */ 148 next round of the loop another block can be read. */
151 size_t n; 149 size_t n;
152 sum = 0; 150 sum = 0;
153 151
...@@ -164,8 +162,8 @@ md5_stream (FILE *stream, void *resblock) ...@@ -164,8 +162,8 @@ md5_stream (FILE *stream, void *resblock)
164 if (n == 0) 162 if (n == 0)
165 { 163 {
166 /* Check for the error flag IFF N == 0, so that we don't 164 /* Check for the error flag IFF N == 0, so that we don't
167 exit the loop after a partial read due to e.g., EAGAIN 165 exit the loop after a partial read due to e.g., EAGAIN
168 or EWOULDBLOCK. */ 166 or EWOULDBLOCK. */
169 if (ferror (stream)) 167 if (ferror (stream))
170 return 1; 168 return 1;
171 goto process_partial_block; 169 goto process_partial_block;
...@@ -179,12 +177,12 @@ md5_stream (FILE *stream, void *resblock) ...@@ -179,12 +177,12 @@ md5_stream (FILE *stream, void *resblock)
179 } 177 }
180 178
181 /* Process buffer with BLOCKSIZE bytes. Note that 179 /* Process buffer with BLOCKSIZE bytes. Note that
182 BLOCKSIZE % 64 == 0 180 BLOCKSIZE % 64 == 0
183 */ 181 */
184 md5_process_block (buffer, BLOCKSIZE, &ctx); 182 md5_process_block (buffer, BLOCKSIZE, &ctx);
185 } 183 }
186 184
187 process_partial_block:; 185 process_partial_block:
188 186
189 /* Process any remaining bytes. */ 187 /* Process any remaining bytes. */
190 if (sum > 0) 188 if (sum > 0)
...@@ -225,7 +223,7 @@ md5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx) ...@@ -225,7 +223,7 @@ md5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx)
225 size_t left_over = ctx->buflen; 223 size_t left_over = ctx->buflen;
226 size_t add = 128 - left_over > len ? len : 128 - left_over; 224 size_t add = 128 - left_over > len ? len : 128 - left_over;
227 225
228 memcpy (&ctx->buffer[left_over], buffer, add); 226 memcpy (&((char *) ctx->buffer)[left_over], buffer, add);
229 ctx->buflen += add; 227 ctx->buflen += add;
230 228
231 if (ctx->buflen > 64) 229 if (ctx->buflen > 64)
...@@ -234,7 +232,8 @@ md5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx) ...@@ -234,7 +232,8 @@ md5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx)
234 232
235 ctx->buflen &= 63; 233 ctx->buflen &= 63;
236 /* The regions in the following copy operation cannot overlap. */ 234 /* The regions in the following copy operation cannot overlap. */
237 memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63], 235 memcpy (ctx->buffer,
236 &((char *) ctx->buffer)[(left_over + add) & ~63],
238 ctx->buflen); 237 ctx->buflen);
239 } 238 }
240 239
...@@ -246,8 +245,14 @@ md5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx) ...@@ -246,8 +245,14 @@ md5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx)
246 if (len >= 64) 245 if (len >= 64)
247 { 246 {
248 #if !_STRING_ARCH_unaligned 247 #if !_STRING_ARCH_unaligned
249 # define alignof(type) offsetof (struct { char c; type x; }, x) 248 /* To check alignment gcc has an appropriate operator. Other
250 # define UNALIGNED_P(p) (((size_t) p) % alignof (md5_uint32) != 0) 249 compilers don't. */
250 # if __GNUC__ >= 2
251 # define UNALIGNED_P(p) (((uintptr_t) p) % __alignof__ (uint32_t) != 0)
252 # else
253 # define alignof(type) offsetof (struct { char c; type x; }, x)
254 # define UNALIGNED_P(p) (((size_t) p) % alignof (uint32_t) != 0)
255 # endif
251 if (UNALIGNED_P (buffer)) 256 if (UNALIGNED_P (buffer))
252 while (len > 64) 257 while (len > 64)
253 { 258 {
...@@ -269,13 +274,13 @@ md5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx) ...@@ -269,13 +274,13 @@ md5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx)
269 { 274 {
270 size_t left_over = ctx->buflen; 275 size_t left_over = ctx->buflen;
271 276
272 memcpy (&ctx->buffer[left_over], buffer, len); 277 memcpy (&((char *) ctx->buffer)[left_over], buffer, len);
273 left_over += len; 278 left_over += len;
274 if (left_over >= 64) 279 if (left_over >= 64)
275 { 280 {
276 md5_process_block (ctx->buffer, 64, ctx); 281 md5_process_block (ctx->buffer, 64, ctx);
277 left_over -= 64; 282 left_over -= 64;
278 memcpy (ctx->buffer, &ctx->buffer[64], left_over); 283 memcpy (ctx->buffer, &ctx->buffer[16], left_over);
279 } 284 }
280 ctx->buflen = left_over; 285 ctx->buflen = left_over;
281 } 286 }
...@@ -297,14 +302,14 @@ md5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx) ...@@ -297,14 +302,14 @@ md5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx)
297 void 302 void
298 md5_process_block (const void *buffer, size_t len, struct md5_ctx *ctx) 303 md5_process_block (const void *buffer, size_t len, struct md5_ctx *ctx)
299 { 304 {
300 md5_uint32 correct_words[16]; 305 uint32_t correct_words[16];
301 const md5_uint32 *words = buffer; 306 const uint32_t *words = buffer;
302 size_t nwords = len / sizeof (md5_uint32); 307 size_t nwords = len / sizeof (uint32_t);
303 const md5_uint32 *endp = words + nwords; 308 const uint32_t *endp = words + nwords;
304 md5_uint32 A = ctx->A; 309 uint32_t A = ctx->A;
305 md5_uint32 B = ctx->B; 310 uint32_t B = ctx->B;
306 md5_uint32 C = ctx->C; 311 uint32_t C = ctx->C;
307 md5_uint32 D = ctx->D; 312 uint32_t D = ctx->D;
308 313
309 /* First increment the byte count. RFC 1321 specifies the possible 314 /* First increment the byte count. RFC 1321 specifies the possible
310 length of the file up to 2^64 bits. Here we only compute the 315 length of the file up to 2^64 bits. Here we only compute the
...@@ -317,120 +322,127 @@ md5_process_block (const void *buffer, size_t len, struct md5_ctx *ctx) ...@@ -317,120 +322,127 @@ md5_process_block (const void *buffer, size_t len, struct md5_ctx *ctx)
317 the loop. */ 322 the loop. */
318 while (words < endp) 323 while (words < endp)
319 { 324 {
320 md5_uint32 *cwp = correct_words; 325 uint32_t *cwp = correct_words;
321 md5_uint32 A_save = A; 326 uint32_t A_save = A;
322 md5_uint32 B_save = B; 327 uint32_t B_save = B;
323 md5_uint32 C_save = C; 328 uint32_t C_save = C;
324 md5_uint32 D_save = D; 329 uint32_t D_save = D;
325 330
326 /* First round: using the given function, the context and a constant 331 /* First round: using the given function, the context and a constant
327 the next context is computed. Because the algorithms processing 332 the next context is computed. Because the algorithms processing
328 unit is a 32-bit word and it is determined to work on words in 333 unit is a 32-bit word and it is determined to work on words in
329 little endian byte order we perhaps have to change the byte order 334 little endian byte order we perhaps have to change the byte order
330 before the computation. To reduce the work for the next steps 335 before the computation. To reduce the work for the next steps
331 we store the swapped words in the array CORRECT_WORDS. */ 336 we store the swapped words in the array CORRECT_WORDS. */
332 337
333 #define OP(a, b, c, d, s, T) \ 338 #define OP(a, b, c, d, s, T) \
334 do \ 339 do \
335 { \ 340 { \
336 a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T; \ 341 a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T; \
337 ++words; \ 342 ++words; \
338 a = rol (a, s); \ 343 CYCLIC (a, s); \
339 a += b; \ 344 a += b; \
340 } \ 345 } \
341 while (0) 346 while (0)
342 347
348 /* It is unfortunate that C does not provide an operator for
349 cyclic rotation. Hope the C compiler is smart enough. */
350 #define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
351
343 /* Before we start, one word to the strange constants. 352 /* Before we start, one word to the strange constants.
344 They are defined in RFC 1321 as 353 They are defined in RFC 1321 as
345 354
346 T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64, or 355 T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
347 perl -e 'foreach(1..64){printf "0x%08x\n", int (4294967296 * abs (sin $_))}' 356
357 Here is an equivalent invocation using Perl:
358
359 perl -e 'foreach(1..64){printf "0x%08x\n", int (4294967296 * abs (sin $_))}'
348 */ 360 */
349 361
350 /* Round 1. */ 362 /* Round 1. */
351 OP (A, B, C, D, 7, 0xd76aa478); 363 OP (A, B, C, D, 7, 0xd76aa478);
352 OP (D, A, B, C, 12, 0xe8c7b756); 364 OP (D, A, B, C, 12, 0xe8c7b756);
353 OP (C, D, A, B, 17, 0x242070db); 365 OP (C, D, A, B, 17, 0x242070db);
354 OP (B, C, D, A, 22, 0xc1bdceee); 366 OP (B, C, D, A, 22, 0xc1bdceee);
355 OP (A, B, C, D, 7, 0xf57c0faf); 367 OP (A, B, C, D, 7, 0xf57c0faf);
356 OP (D, A, B, C, 12, 0x4787c62a); 368 OP (D, A, B, C, 12, 0x4787c62a);
357 OP (C, D, A, B, 17, 0xa8304613); 369 OP (C, D, A, B, 17, 0xa8304613);
358 OP (B, C, D, A, 22, 0xfd469501); 370 OP (B, C, D, A, 22, 0xfd469501);
359 OP (A, B, C, D, 7, 0x698098d8); 371 OP (A, B, C, D, 7, 0x698098d8);
360 OP (D, A, B, C, 12, 0x8b44f7af); 372 OP (D, A, B, C, 12, 0x8b44f7af);
361 OP (C, D, A, B, 17, 0xffff5bb1); 373 OP (C, D, A, B, 17, 0xffff5bb1);
362 OP (B, C, D, A, 22, 0x895cd7be); 374 OP (B, C, D, A, 22, 0x895cd7be);
363 OP (A, B, C, D, 7, 0x6b901122); 375 OP (A, B, C, D, 7, 0x6b901122);
364 OP (D, A, B, C, 12, 0xfd987193); 376 OP (D, A, B, C, 12, 0xfd987193);
365 OP (C, D, A, B, 17, 0xa679438e); 377 OP (C, D, A, B, 17, 0xa679438e);
366 OP (B, C, D, A, 22, 0x49b40821); 378 OP (B, C, D, A, 22, 0x49b40821);
367 379
368 /* For the second to fourth round we have the possibly swapped words 380 /* For the second to fourth round we have the possibly swapped words
369 in CORRECT_WORDS. Redefine the macro to take an additional first 381 in CORRECT_WORDS. Redefine the macro to take an additional first
370 argument specifying the function to use. */ 382 argument specifying the function to use. */
371 #undef OP 383 #undef OP
372 #define OP(f, a, b, c, d, k, s, T) \ 384 #define OP(f, a, b, c, d, k, s, T) \
373 do \ 385 do \
374 { \ 386 { \
375 a += f (b, c, d) + correct_words[k] + T; \ 387 a += f (b, c, d) + correct_words[k] + T; \
376 a = rol (a, s); \ 388 CYCLIC (a, s); \
377 a += b; \ 389 a += b; \
378 } \ 390 } \
379 while (0) 391 while (0)
380 392
381 /* Round 2. */ 393 /* Round 2. */
382 OP (FG, A, B, C, D, 1, 5, 0xf61e2562); 394 OP (FG, A, B, C, D, 1, 5, 0xf61e2562);
383 OP (FG, D, A, B, C, 6, 9, 0xc040b340); 395 OP (FG, D, A, B, C, 6, 9, 0xc040b340);
384 OP (FG, C, D, A, B, 11, 14, 0x265e5a51); 396 OP (FG, C, D, A, B, 11, 14, 0x265e5a51);
385 OP (FG, B, C, D, A, 0, 20, 0xe9b6c7aa); 397 OP (FG, B, C, D, A, 0, 20, 0xe9b6c7aa);
386 OP (FG, A, B, C, D, 5, 5, 0xd62f105d); 398 OP (FG, A, B, C, D, 5, 5, 0xd62f105d);
387 OP (FG, D, A, B, C, 10, 9, 0x02441453); 399 OP (FG, D, A, B, C, 10, 9, 0x02441453);
388 OP (FG, C, D, A, B, 15, 14, 0xd8a1e681); 400 OP (FG, C, D, A, B, 15, 14, 0xd8a1e681);
389 OP (FG, B, C, D, A, 4, 20, 0xe7d3fbc8); 401 OP (FG, B, C, D, A, 4, 20, 0xe7d3fbc8);
390 OP (FG, A, B, C, D, 9, 5, 0x21e1cde6); 402 OP (FG, A, B, C, D, 9, 5, 0x21e1cde6);
391 OP (FG, D, A, B, C, 14, 9, 0xc33707d6); 403 OP (FG, D, A, B, C, 14, 9, 0xc33707d6);
392 OP (FG, C, D, A, B, 3, 14, 0xf4d50d87); 404 OP (FG, C, D, A, B, 3, 14, 0xf4d50d87);
393 OP (FG, B, C, D, A, 8, 20, 0x455a14ed); 405 OP (FG, B, C, D, A, 8, 20, 0x455a14ed);
394 OP (FG, A, B, C, D, 13, 5, 0xa9e3e905); 406 OP (FG, A, B, C, D, 13, 5, 0xa9e3e905);
395 OP (FG, D, A, B, C, 2, 9, 0xfcefa3f8); 407 OP (FG, D, A, B, C, 2, 9, 0xfcefa3f8);
396 OP (FG, C, D, A, B, 7, 14, 0x676f02d9); 408 OP (FG, C, D, A, B, 7, 14, 0x676f02d9);
397 OP (FG, B, C, D, A, 12, 20, 0x8d2a4c8a); 409 OP (FG, B, C, D, A, 12, 20, 0x8d2a4c8a);
398 410
399 /* Round 3. */ 411 /* Round 3. */
400 OP (FH, A, B, C, D, 5, 4, 0xfffa3942); 412 OP (FH, A, B, C, D, 5, 4, 0xfffa3942);
401 OP (FH, D, A, B, C, 8, 11, 0x8771f681); 413 OP (FH, D, A, B, C, 8, 11, 0x8771f681);
402 OP (FH, C, D, A, B, 11, 16, 0x6d9d6122); 414 OP (FH, C, D, A, B, 11, 16, 0x6d9d6122);
403 OP (FH, B, C, D, A, 14, 23, 0xfde5380c); 415 OP (FH, B, C, D, A, 14, 23, 0xfde5380c);
404 OP (FH, A, B, C, D, 1, 4, 0xa4beea44); 416 OP (FH, A, B, C, D, 1, 4, 0xa4beea44);
405 OP (FH, D, A, B, C, 4, 11, 0x4bdecfa9); 417 OP (FH, D, A, B, C, 4, 11, 0x4bdecfa9);
406 OP (FH, C, D, A, B, 7, 16, 0xf6bb4b60); 418 OP (FH, C, D, A, B, 7, 16, 0xf6bb4b60);
407 OP (FH, B, C, D, A, 10, 23, 0xbebfbc70); 419 OP (FH, B, C, D, A, 10, 23, 0xbebfbc70);
408 OP (FH, A, B, C, D, 13, 4, 0x289b7ec6); 420 OP (FH, A, B, C, D, 13, 4, 0x289b7ec6);
409 OP (FH, D, A, B, C, 0, 11, 0xeaa127fa); 421 OP (FH, D, A, B, C, 0, 11, 0xeaa127fa);
410 OP (FH, C, D, A, B, 3, 16, 0xd4ef3085); 422 OP (FH, C, D, A, B, 3, 16, 0xd4ef3085);
411 OP (FH, B, C, D, A, 6, 23, 0x04881d05); 423 OP (FH, B, C, D, A, 6, 23, 0x04881d05);
412 OP (FH, A, B, C, D, 9, 4, 0xd9d4d039); 424 OP (FH, A, B, C, D, 9, 4, 0xd9d4d039);
413 OP (FH, D, A, B, C, 12, 11, 0xe6db99e5); 425 OP (FH, D, A, B, C, 12, 11, 0xe6db99e5);
414 OP (FH, C, D, A, B, 15, 16, 0x1fa27cf8); 426 OP (FH, C, D, A, B, 15, 16, 0x1fa27cf8);
415 OP (FH, B, C, D, A, 2, 23, 0xc4ac5665); 427 OP (FH, B, C, D, A, 2, 23, 0xc4ac5665);
416 428
417 /* Round 4. */ 429 /* Round 4. */
418 OP (FI, A, B, C, D, 0, 6, 0xf4292244); 430 OP (FI, A, B, C, D, 0, 6, 0xf4292244);
419 OP (FI, D, A, B, C, 7, 10, 0x432aff97); 431 OP (FI, D, A, B, C, 7, 10, 0x432aff97);
420 OP (FI, C, D, A, B, 14, 15, 0xab9423a7); 432 OP (FI, C, D, A, B, 14, 15, 0xab9423a7);
421 OP (FI, B, C, D, A, 5, 21, 0xfc93a039); 433 OP (FI, B, C, D, A, 5, 21, 0xfc93a039);
422 OP (FI, A, B, C, D, 12, 6, 0x655b59c3); 434 OP (FI, A, B, C, D, 12, 6, 0x655b59c3);
423 OP (FI, D, A, B, C, 3, 10, 0x8f0ccc92); 435 OP (FI, D, A, B, C, 3, 10, 0x8f0ccc92);
424 OP (FI, C, D, A, B, 10, 15, 0xffeff47d); 436 OP (FI, C, D, A, B, 10, 15, 0xffeff47d);
425 OP (FI, B, C, D, A, 1, 21, 0x85845dd1); 437 OP (FI, B, C, D, A, 1, 21, 0x85845dd1);
426 OP (FI, A, B, C, D, 8, 6, 0x6fa87e4f); 438 OP (FI, A, B, C, D, 8, 6, 0x6fa87e4f);
427 OP (FI, D, A, B, C, 15, 10, 0xfe2ce6e0); 439 OP (FI, D, A, B, C, 15, 10, 0xfe2ce6e0);
428 OP (FI, C, D, A, B, 6, 15, 0xa3014314); 440 OP (FI, C, D, A, B, 6, 15, 0xa3014314);
429 OP (FI, B, C, D, A, 13, 21, 0x4e0811a1); 441 OP (FI, B, C, D, A, 13, 21, 0x4e0811a1);
430 OP (FI, A, B, C, D, 4, 6, 0xf7537e82); 442 OP (FI, A, B, C, D, 4, 6, 0xf7537e82);
431 OP (FI, D, A, B, C, 11, 10, 0xbd3af235); 443 OP (FI, D, A, B, C, 11, 10, 0xbd3af235);
432 OP (FI, C, D, A, B, 2, 15, 0x2ad7d2bb); 444 OP (FI, C, D, A, B, 2, 15, 0x2ad7d2bb);
433 OP (FI, B, C, D, A, 9, 21, 0xeb86d391); 445 OP (FI, B, C, D, A, 9, 21, 0xeb86d391);
434 446
435 /* Add the starting values of the context. */ 447 /* Add the starting values of the context. */
436 A += A_save; 448 A += A_save;
......
1 /* md5.h - Declaration of functions and data types used for MD5 sum 1 /* Declaration of functions and data types used for MD5 sum computing
2 computing library functions. 2 library functions.
3 3 Copyright (C) 1995-1997,1999,2000,2001,2004,2005
4 Copyright (C) 1995, 1996, 1999, 2000, 2003, 2004 Free Software 4 Free Software Foundation, Inc.
5 Foundation, Inc. 5 This file is part of the GNU C Library.
6
7 NOTE: The canonical source of this file is maintained with the GNU C
8 Library. Bugs can be reported to bug-glibc@prep.ai.mit.edu.
9 6
10 This program is free software; you can redistribute it and/or modify it 7 This program is free software; you can redistribute it and/or modify it
11 under the terms of the GNU General Public License as published by the 8 under the terms of the GNU General Public License as published by the
...@@ -25,27 +22,55 @@ ...@@ -25,27 +22,55 @@
25 #define _MD5_H 1 22 #define _MD5_H 1
26 23
27 #include <stdio.h> 24 #include <stdio.h>
25 #include <stdint.h>
26
27 #define MD5_DIGEST_SIZE 16
28 #define MD5_BLOCK_SIZE 64
29
30 #ifndef __GNUC_PREREQ
31 # if defined __GNUC__ && defined __GNUC_MINOR__
32 # define __GNUC_PREREQ(maj, min) \
33 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
34 # else
35 # define __GNUC_PREREQ(maj, min) 0
36 # endif
37 #endif
28 38
29 #if HAVE_INTTYPES_H 39 #ifndef __THROW
30 # include <inttypes.h> 40 # if defined __cplusplus && __GNUC_PREREQ (2,8)
41 # define __THROW throw ()
42 # else
43 # define __THROW
44 # endif
31 #endif 45 #endif
32 #if HAVE_STDINT_H || _LIBC 46
33 # include <stdint.h> 47 #ifndef __attribute__
48 # if ! __GNUC_PREREQ (2,8) || __STRICT_ANSI__
49 # define __attribute__(x)
50 # endif
34 #endif 51 #endif
35 52
36 typedef uint32_t md5_uint32; 53 #ifndef _LIBC
54 # define __md5_buffer md5_buffer
55 # define __md5_finish_ctx md5_finish_ctx
56 # define __md5_init_ctx md5_init_ctx
57 # define __md5_process_block md5_process_block
58 # define __md5_process_bytes md5_process_bytes
59 # define __md5_read_ctx md5_read_ctx
60 # define __md5_stream md5_stream
61 #endif
37 62
38 /* Structure to save state of computation between the single steps. */ 63 /* Structure to save state of computation between the single steps. */
39 struct md5_ctx 64 struct md5_ctx
40 { 65 {
41 md5_uint32 A; 66 uint32_t A;
42 md5_uint32 B; 67 uint32_t B;
43 md5_uint32 C; 68 uint32_t C;
44 md5_uint32 D; 69 uint32_t D;
45 70
46 md5_uint32 total[2]; 71 uint32_t total[2];
47 md5_uint32 buflen; 72 uint32_t buflen;
48 char buffer[128]; 73 uint32_t buffer[32];
49 }; 74 };
50 75
51 /* 76 /*
...@@ -55,52 +80,51 @@ struct md5_ctx ...@@ -55,52 +80,51 @@ struct md5_ctx
55 80
56 /* Initialize structure containing state of computation. 81 /* Initialize structure containing state of computation.
57 (RFC 1321, 3.3: Step 3) */ 82 (RFC 1321, 3.3: Step 3) */
58 extern void md5_init_ctx (struct md5_ctx *ctx); 83 extern void __md5_init_ctx (struct md5_ctx *ctx) __THROW;
59 84
60 /* Starting with the result of former calls of this function (or the 85 /* Starting with the result of former calls of this function (or the
61 initialization function update the context for the next LEN bytes 86 initialization function update the context for the next LEN bytes
62 starting at BUFFER. 87 starting at BUFFER.
63 It is necessary that LEN is a multiple of 64!!! */ 88 It is necessary that LEN is a multiple of 64!!! */
64 extern void md5_process_block (const void *buffer, size_t len, 89 extern void __md5_process_block (const void *buffer, size_t len,
65 struct md5_ctx *ctx); 90 struct md5_ctx *ctx) __THROW;
66 91
67 /* Starting with the result of former calls of this function (or the 92 /* Starting with the result of former calls of this function (or the
68 initialization function update the context for the next LEN bytes 93 initialization function update the context for the next LEN bytes
69 starting at BUFFER. 94 starting at BUFFER.
70 It is NOT required that LEN is a multiple of 64. */ 95 It is NOT required that LEN is a multiple of 64. */
71 extern void md5_process_bytes (const void *buffer, size_t len, 96 extern void __md5_process_bytes (const void *buffer, size_t len,
72 struct md5_ctx *ctx); 97 struct md5_ctx *ctx) __THROW;
73 98
74 /* Process the remaining bytes in the buffer and put result from CTX 99 /* Process the remaining bytes in the buffer and put result from CTX
75 in first 16 bytes following RESBUF. The result is always in little 100 in first 16 bytes following RESBUF. The result is always in little
76 endian byte order, so that a byte-wise output yields to the wanted 101 endian byte order, so that a byte-wise output yields to the wanted
77 ASCII representation of the message digest. 102 ASCII representation of the message digest.
78 103
79 IMPORTANT: On some systems it is required that RESBUF be correctly 104 IMPORTANT: On some systems, RESBUF must be aligned to a 32-bit
80 aligned for a 32 bits value. */ 105 boundary. */
81 extern void *md5_finish_ctx (struct md5_ctx *ctx, void *resbuf); 106 extern void *__md5_finish_ctx (struct md5_ctx *ctx, void *resbuf) __THROW;
82 107
83 108
84 /* Put result from CTX in first 16 bytes following RESBUF. The result is 109 /* Put result from CTX in first 16 bytes following RESBUF. The result is
85 always in little endian byte order, so that a byte-wise output yields 110 always in little endian byte order, so that a byte-wise output yields
86 to the wanted ASCII representation of the message digest. 111 to the wanted ASCII representation of the message digest.
87 112
88 IMPORTANT: On some systems it is required that RESBUF is correctly 113 IMPORTANT: On some systems, RESBUF must be aligned to a 32-bit
89 aligned for a 32 bits value. */ 114 boundary. */
90 extern void *md5_read_ctx (const struct md5_ctx *ctx, void *resbuf); 115 extern void *__md5_read_ctx (const struct md5_ctx *ctx, void *resbuf) __THROW;
91 116
92 117
93 /* Compute MD5 message digest for bytes read from STREAM. The 118 /* Compute MD5 message digest for bytes read from STREAM. The
94 resulting message digest number will be written into the 16 bytes 119 resulting message digest number will be written into the 16 bytes
95 beginning at RESBLOCK. */ 120 beginning at RESBLOCK. */
96 extern int md5_stream (FILE *stream, void *resblock); 121 extern int __md5_stream (FILE *stream, void *resblock) __THROW;
97 122
98 /* Compute MD5 message digest for LEN bytes beginning at BUFFER. The 123 /* Compute MD5 message digest for LEN bytes beginning at BUFFER. The
99 result is always in little endian byte order, so that a byte-wise 124 result is always in little endian byte order, so that a byte-wise
100 output yields to the wanted ASCII representation of the message 125 output yields to the wanted ASCII representation of the message
101 digest. */ 126 digest. */
102 extern void *md5_buffer (const char *buffer, size_t len, void *resblock); 127 extern void *__md5_buffer (const char *buffer, size_t len,
103 128 void *resblock) __THROW;
104 #define rol(x,n) ( ((x) << (n)) | ((x) >> (32-(n))) )
105 129
106 #endif 130 #endif /* md5.h */
......
...@@ -21,9 +21,8 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ...@@ -21,9 +21,8 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details. 21 GNU General Public License for more details.
22 22
23 You should have received a copy of the GNU General Public License 23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software 24 along with this program; if not, write to the Free Software Foundation,
25 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 25 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
26 USA. */
27 26
28 #ifdef HAVE_CONFIG_H 27 #ifdef HAVE_CONFIG_H
29 # include <config.h> 28 # include <config.h>
......
...@@ -18,12 +18,11 @@ ...@@ -18,12 +18,11 @@
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 19
20 static reg_errcode_t re_compile_internal (regex_t *preg, const char * pattern, 20 static reg_errcode_t re_compile_internal (regex_t *preg, const char * pattern,
21 int length, reg_syntax_t syntax); 21 Idx length, reg_syntax_t syntax);
22 static void re_compile_fastmap_iter (regex_t *bufp, 22 static void re_compile_fastmap_iter (regex_t *bufp,
23 const re_dfastate_t *init_state, 23 const re_dfastate_t *init_state,
24 char *fastmap); 24 char *fastmap);
25 static reg_errcode_t init_dfa (re_dfa_t *dfa, int pat_len); 25 static reg_errcode_t init_dfa (re_dfa_t *dfa, Idx pat_len);
26 static void init_word_char (re_dfa_t *dfa);
27 #ifdef RE_ENABLE_I18N 26 #ifdef RE_ENABLE_I18N
28 static void free_charset (re_charset_t *cset); 27 static void free_charset (re_charset_t *cset);
29 #endif /* RE_ENABLE_I18N */ 28 #endif /* RE_ENABLE_I18N */
...@@ -33,7 +32,6 @@ static reg_errcode_t create_initial_state (re_dfa_t *dfa); ...@@ -33,7 +32,6 @@ static reg_errcode_t create_initial_state (re_dfa_t *dfa);
33 static void optimize_utf8 (re_dfa_t *dfa); 32 static void optimize_utf8 (re_dfa_t *dfa);
34 #endif 33 #endif
35 static reg_errcode_t analyze (regex_t *preg); 34 static reg_errcode_t analyze (regex_t *preg);
36 static reg_errcode_t create_initial_state (re_dfa_t *dfa);
37 static reg_errcode_t preorder (bin_tree_t *root, 35 static reg_errcode_t preorder (bin_tree_t *root,
38 reg_errcode_t (fn (void *, bin_tree_t *)), 36 reg_errcode_t (fn (void *, bin_tree_t *)),
39 void *extra); 37 void *extra);
...@@ -47,39 +45,31 @@ static bin_tree_t *lower_subexp (reg_errcode_t *err, regex_t *preg, ...@@ -47,39 +45,31 @@ static bin_tree_t *lower_subexp (reg_errcode_t *err, regex_t *preg,
47 static reg_errcode_t calc_first (void *extra, bin_tree_t *node); 45 static reg_errcode_t calc_first (void *extra, bin_tree_t *node);
48 static reg_errcode_t calc_next (void *extra, bin_tree_t *node); 46 static reg_errcode_t calc_next (void *extra, bin_tree_t *node);
49 static reg_errcode_t link_nfa_nodes (void *extra, bin_tree_t *node); 47 static reg_errcode_t link_nfa_nodes (void *extra, bin_tree_t *node);
50 static reg_errcode_t duplicate_node_closure (re_dfa_t *dfa, int top_org_node, 48 static Idx duplicate_node (re_dfa_t *dfa, Idx org_idx, unsigned int constraint);
51 int top_clone_node, int root_node, 49 static Idx search_duplicated_node (const re_dfa_t *dfa, Idx org_node,
52 unsigned int constraint);
53 static reg_errcode_t duplicate_node (int *new_idx, re_dfa_t *dfa, int org_idx,
54 unsigned int constraint);
55 static int search_duplicated_node (re_dfa_t *dfa, int org_node,
56 unsigned int constraint); 50 unsigned int constraint);
57 static reg_errcode_t calc_eclosure (re_dfa_t *dfa); 51 static reg_errcode_t calc_eclosure (re_dfa_t *dfa);
58 static reg_errcode_t calc_eclosure_iter (re_node_set *new_set, re_dfa_t *dfa, 52 static reg_errcode_t calc_eclosure_iter (re_node_set *new_set, re_dfa_t *dfa,
59 int node, int root); 53 Idx node, bool root);
60 static reg_errcode_t calc_inveclosure (re_dfa_t *dfa); 54 static reg_errcode_t calc_inveclosure (re_dfa_t *dfa);
61 static int fetch_number (re_string_t *input, re_token_t *token, 55 static Idx fetch_number (re_string_t *input, re_token_t *token,
62 reg_syntax_t syntax);
63 static void fetch_token (re_token_t *result, re_string_t *input,
64 reg_syntax_t syntax); 56 reg_syntax_t syntax);
65 static int peek_token (re_token_t *token, re_string_t *input, 57 static int peek_token (re_token_t *token, re_string_t *input,
66 reg_syntax_t syntax); 58 reg_syntax_t syntax);
67 static int peek_token_bracket (re_token_t *token, re_string_t *input,
68 reg_syntax_t syntax);
69 static bin_tree_t *parse (re_string_t *regexp, regex_t *preg, 59 static bin_tree_t *parse (re_string_t *regexp, regex_t *preg,
70 reg_syntax_t syntax, reg_errcode_t *err); 60 reg_syntax_t syntax, reg_errcode_t *err);
71 static bin_tree_t *parse_reg_exp (re_string_t *regexp, regex_t *preg, 61 static bin_tree_t *parse_reg_exp (re_string_t *regexp, regex_t *preg,
72 re_token_t *token, reg_syntax_t syntax, 62 re_token_t *token, reg_syntax_t syntax,
73 int nest, reg_errcode_t *err); 63 Idx nest, reg_errcode_t *err);
74 static bin_tree_t *parse_branch (re_string_t *regexp, regex_t *preg, 64 static bin_tree_t *parse_branch (re_string_t *regexp, regex_t *preg,
75 re_token_t *token, reg_syntax_t syntax, 65 re_token_t *token, reg_syntax_t syntax,
76 int nest, reg_errcode_t *err); 66 Idx nest, reg_errcode_t *err);
77 static bin_tree_t *parse_expression (re_string_t *regexp, regex_t *preg, 67 static bin_tree_t *parse_expression (re_string_t *regexp, regex_t *preg,
78 re_token_t *token, reg_syntax_t syntax, 68 re_token_t *token, reg_syntax_t syntax,
79 int nest, reg_errcode_t *err); 69 Idx nest, reg_errcode_t *err);
80 static bin_tree_t *parse_sub_exp (re_string_t *regexp, regex_t *preg, 70 static bin_tree_t *parse_sub_exp (re_string_t *regexp, regex_t *preg,
81 re_token_t *token, reg_syntax_t syntax, 71 re_token_t *token, reg_syntax_t syntax,
82 int nest, reg_errcode_t *err); 72 Idx nest, reg_errcode_t *err);
83 static bin_tree_t *parse_dup_op (bin_tree_t *dup_elem, re_string_t *regexp, 73 static bin_tree_t *parse_dup_op (bin_tree_t *dup_elem, re_string_t *regexp,
84 re_dfa_t *dfa, re_token_t *token, 74 re_dfa_t *dfa, re_token_t *token,
85 reg_syntax_t syntax, reg_errcode_t *err); 75 reg_syntax_t syntax, reg_errcode_t *err);
...@@ -91,52 +81,34 @@ static reg_errcode_t parse_bracket_element (bracket_elem_t *elem, ...@@ -91,52 +81,34 @@ static reg_errcode_t parse_bracket_element (bracket_elem_t *elem,
91 re_token_t *token, int token_len, 81 re_token_t *token, int token_len,
92 re_dfa_t *dfa, 82 re_dfa_t *dfa,
93 reg_syntax_t syntax, 83 reg_syntax_t syntax,
94 int accept_hyphen); 84 bool accept_hyphen);
95 static reg_errcode_t parse_bracket_symbol (bracket_elem_t *elem, 85 static reg_errcode_t parse_bracket_symbol (bracket_elem_t *elem,
96 re_string_t *regexp, 86 re_string_t *regexp,
97 re_token_t *token); 87 re_token_t *token);
98 #ifndef _LIBC
99 # ifdef RE_ENABLE_I18N
100 static reg_errcode_t build_range_exp (re_bitset_ptr_t sbcset,
101 re_charset_t *mbcset, int *range_alloc,
102 bracket_elem_t *start_elem,
103 bracket_elem_t *end_elem);
104 static reg_errcode_t build_collating_symbol (re_bitset_ptr_t sbcset,
105 re_charset_t *mbcset,
106 int *coll_sym_alloc,
107 const unsigned char *name);
108 # else /* not RE_ENABLE_I18N */
109 static reg_errcode_t build_range_exp (re_bitset_ptr_t sbcset,
110 bracket_elem_t *start_elem,
111 bracket_elem_t *end_elem);
112 static reg_errcode_t build_collating_symbol (re_bitset_ptr_t sbcset,
113 const unsigned char *name);
114 # endif /* not RE_ENABLE_I18N */
115 #endif /* not _LIBC */
116 #ifdef RE_ENABLE_I18N 88 #ifdef RE_ENABLE_I18N
117 static reg_errcode_t build_equiv_class (re_bitset_ptr_t sbcset, 89 static reg_errcode_t build_equiv_class (bitset sbcset,
118 re_charset_t *mbcset, 90 re_charset_t *mbcset,
119 int *equiv_class_alloc, 91 Idx *equiv_class_alloc,
120 const unsigned char *name); 92 const unsigned char *name);
121 static reg_errcode_t build_charclass (unsigned RE_TRANSLATE_TYPE trans, 93 static reg_errcode_t build_charclass (unsigned REG_TRANSLATE_TYPE trans,
122 re_bitset_ptr_t sbcset, 94 bitset sbcset,
123 re_charset_t *mbcset, 95 re_charset_t *mbcset,
124 int *char_class_alloc, 96 Idx *char_class_alloc,
125 const unsigned char *class_name, 97 const unsigned char *class_name,
126 reg_syntax_t syntax); 98 reg_syntax_t syntax);
127 #else /* not RE_ENABLE_I18N */ 99 #else /* not RE_ENABLE_I18N */
128 static reg_errcode_t build_equiv_class (re_bitset_ptr_t sbcset, 100 static reg_errcode_t build_equiv_class (bitset sbcset,
129 const unsigned char *name); 101 const unsigned char *name);
130 static reg_errcode_t build_charclass (unsigned RE_TRANSLATE_TYPE trans, 102 static reg_errcode_t build_charclass (unsigned REG_TRANSLATE_TYPE trans,
131 re_bitset_ptr_t sbcset, 103 bitset sbcset,
132 const unsigned char *class_name, 104 const unsigned char *class_name,
133 reg_syntax_t syntax); 105 reg_syntax_t syntax);
134 #endif /* not RE_ENABLE_I18N */ 106 #endif /* not RE_ENABLE_I18N */
135 static bin_tree_t *build_charclass_op (re_dfa_t *dfa, 107 static bin_tree_t *build_charclass_op (re_dfa_t *dfa,
136 unsigned RE_TRANSLATE_TYPE trans, 108 unsigned REG_TRANSLATE_TYPE trans,
137 const unsigned char *class_name, 109 const unsigned char *class_name,
138 const unsigned char *extra, 110 const unsigned char *extra,
139 int non_match, reg_errcode_t *err); 111 bool non_match, reg_errcode_t *err);
140 static bin_tree_t *create_tree (re_dfa_t *dfa, 112 static bin_tree_t *create_tree (re_dfa_t *dfa,
141 bin_tree_t *left, bin_tree_t *right, 113 bin_tree_t *left, bin_tree_t *right,
142 re_token_type_t type); 114 re_token_type_t type);
...@@ -234,24 +206,22 @@ const size_t __re_error_msgid_idx[] attribute_hidden = ...@@ -234,24 +206,22 @@ const size_t __re_error_msgid_idx[] attribute_hidden =
234 compiles PATTERN (of length LENGTH) and puts the result in BUFP. 206 compiles PATTERN (of length LENGTH) and puts the result in BUFP.
235 Returns 0 if the pattern was valid, otherwise an error string. 207 Returns 0 if the pattern was valid, otherwise an error string.
236 208
237 Assumes the `allocated' (and perhaps `buffer') and `translate' fields 209 Assumes the `re_allocated' (and perhaps `re_buffer') and `translate' fields
238 are set in BUFP on entry. */ 210 are set in BUFP on entry. */
239 211
240 const char * 212 const char *
241 re_compile_pattern (pattern, length, bufp) 213 re_compile_pattern (const char *pattern, size_t length,
242 const char *pattern; 214 struct re_pattern_buffer *bufp)
243 size_t length;
244 struct re_pattern_buffer *bufp;
245 { 215 {
246 reg_errcode_t ret; 216 reg_errcode_t ret;
247 217
248 /* And GNU code determines whether or not to get register information 218 /* And GNU code determines whether or not to get register information
249 by passing null for the REGS argument to re_match, etc., not by 219 by passing null for the REGS argument to re_match, etc., not by
250 setting no_sub, unless RE_NO_SUB is set. */ 220 setting re_no_sub, unless REG_NO_SUB is set. */
251 bufp->no_sub = !!(re_syntax_options & RE_NO_SUB); 221 bufp->re_no_sub = !!(re_syntax_options & REG_NO_SUB);
252 222
253 /* Match anchors at newline. */ 223 /* Match anchors at newline. */
254 bufp->newline_anchor = 1; 224 bufp->re_newline_anchor = 1;
255 225
256 ret = re_compile_internal (bufp, pattern, length, re_syntax_options); 226 ret = re_compile_internal (bufp, pattern, length, re_syntax_options);
257 227
...@@ -279,8 +249,7 @@ reg_syntax_t re_syntax_options; ...@@ -279,8 +249,7 @@ reg_syntax_t re_syntax_options;
279 defined in regex.h. We return the old syntax. */ 249 defined in regex.h. We return the old syntax. */
280 250
281 reg_syntax_t 251 reg_syntax_t
282 re_set_syntax (syntax) 252 re_set_syntax (reg_syntax_t syntax)
283 reg_syntax_t syntax;
284 { 253 {
285 reg_syntax_t ret = re_syntax_options; 254 reg_syntax_t ret = re_syntax_options;
286 255
...@@ -292,11 +261,10 @@ weak_alias (__re_set_syntax, re_set_syntax) ...@@ -292,11 +261,10 @@ weak_alias (__re_set_syntax, re_set_syntax)
292 #endif 261 #endif
293 262
294 int 263 int
295 re_compile_fastmap (bufp) 264 re_compile_fastmap (struct re_pattern_buffer *bufp)
296 struct re_pattern_buffer *bufp;
297 { 265 {
298 re_dfa_t *dfa = (re_dfa_t *) bufp->buffer; 266 re_dfa_t *dfa = (re_dfa_t *) bufp->re_buffer;
299 char *fastmap = bufp->fastmap; 267 char *fastmap = bufp->re_fastmap;
300 268
301 memset (fastmap, '\0', sizeof (char) * SBC_MAX); 269 memset (fastmap, '\0', sizeof (char) * SBC_MAX);
302 re_compile_fastmap_iter (bufp, dfa->init_state, fastmap); 270 re_compile_fastmap_iter (bufp, dfa->init_state, fastmap);
...@@ -306,7 +274,7 @@ re_compile_fastmap (bufp) ...@@ -306,7 +274,7 @@ re_compile_fastmap (bufp)
306 re_compile_fastmap_iter (bufp, dfa->init_state_nl, fastmap); 274 re_compile_fastmap_iter (bufp, dfa->init_state_nl, fastmap);
307 if (dfa->init_state != dfa->init_state_begbuf) 275 if (dfa->init_state != dfa->init_state_begbuf)
308 re_compile_fastmap_iter (bufp, dfa->init_state_begbuf, fastmap); 276 re_compile_fastmap_iter (bufp, dfa->init_state_begbuf, fastmap);
309 bufp->fastmap_accurate = 1; 277 bufp->re_fastmap_accurate = 1;
310 return 0; 278 return 0;
311 } 279 }
312 #ifdef _LIBC 280 #ifdef _LIBC
...@@ -315,7 +283,7 @@ weak_alias (__re_compile_fastmap, re_compile_fastmap) ...@@ -315,7 +283,7 @@ weak_alias (__re_compile_fastmap, re_compile_fastmap)
315 283
316 static inline void 284 static inline void
317 __attribute ((always_inline)) 285 __attribute ((always_inline))
318 re_set_fastmap (char *fastmap, int icase, int ch) 286 re_set_fastmap (char *fastmap, bool icase, int ch)
319 { 287 {
320 fastmap[ch] = 1; 288 fastmap[ch] = 1;
321 if (icase) 289 if (icase)
...@@ -326,26 +294,25 @@ re_set_fastmap (char *fastmap, int icase, int ch) ...@@ -326,26 +294,25 @@ re_set_fastmap (char *fastmap, int icase, int ch)
326 Compile fastmap for the initial_state INIT_STATE. */ 294 Compile fastmap for the initial_state INIT_STATE. */
327 295
328 static void 296 static void
329 re_compile_fastmap_iter (bufp, init_state, fastmap) 297 re_compile_fastmap_iter (regex_t *bufp, const re_dfastate_t *init_state,
330 regex_t *bufp; 298 char *fastmap)
331 const re_dfastate_t *init_state;
332 char *fastmap;
333 { 299 {
334 re_dfa_t *dfa = (re_dfa_t *) bufp->buffer; 300 re_dfa_t *dfa = (re_dfa_t *) bufp->re_buffer;
335 int node_cnt; 301 Idx node_cnt;
336 int icase = (dfa->mb_cur_max == 1 && (bufp->syntax & RE_ICASE)); 302 bool icase = (dfa->mb_cur_max == 1 && (bufp->re_syntax & REG_IGNORE_CASE));
337 for (node_cnt = 0; node_cnt < init_state->nodes.nelem; ++node_cnt) 303 for (node_cnt = 0; node_cnt < init_state->nodes.nelem; ++node_cnt)
338 { 304 {
339 int node = init_state->nodes.elems[node_cnt]; 305 Idx node = init_state->nodes.elems[node_cnt];
340 re_token_type_t type = dfa->nodes[node].type; 306 re_token_type_t type = dfa->nodes[node].type;
341 307
342 if (type == CHARACTER) 308 if (type == CHARACTER)
343 { 309 {
344 re_set_fastmap (fastmap, icase, dfa->nodes[node].opr.c); 310 re_set_fastmap (fastmap, icase, dfa->nodes[node].opr.c);
345 #ifdef RE_ENABLE_I18N 311 #ifdef RE_ENABLE_I18N
346 if ((bufp->syntax & RE_ICASE) && dfa->mb_cur_max > 1) 312 if ((bufp->re_syntax & REG_IGNORE_CASE) && dfa->mb_cur_max > 1)
347 { 313 {
348 unsigned char *buf = alloca (dfa->mb_cur_max), *p; 314 unsigned char buf[MB_LEN_MAX];
315 unsigned char *p;
349 wchar_t wc; 316 wchar_t wc;
350 mbstate_t state; 317 mbstate_t state;
351 318
...@@ -360,22 +327,22 @@ re_compile_fastmap_iter (bufp, init_state, fastmap) ...@@ -360,22 +327,22 @@ re_compile_fastmap_iter (bufp, init_state, fastmap)
360 &state) == p - buf 327 &state) == p - buf
361 && (__wcrtomb ((char *) buf, towlower (wc), &state) 328 && (__wcrtomb ((char *) buf, towlower (wc), &state)
362 != (size_t) -1)) 329 != (size_t) -1))
363 re_set_fastmap (fastmap, 0, buf[0]); 330 re_set_fastmap (fastmap, false, buf[0]);
364 } 331 }
365 #endif 332 #endif
366 } 333 }
367 else if (type == SIMPLE_BRACKET) 334 else if (type == SIMPLE_BRACKET)
368 { 335 {
369 int i, j, ch; 336 int i, j, ch;
370 for (i = 0, ch = 0; i < BITSET_UINTS; ++i) 337 for (i = 0, ch = 0; i < BITSET_WORDS; ++i)
371 for (j = 0; j < UINT_BITS; ++j, ++ch) 338 for (j = 0; j < BITSET_WORD_BITS; ++j, ++ch)
372 if (dfa->nodes[node].opr.sbcset[i] & (1 << j)) 339 if (dfa->nodes[node].opr.sbcset[i] & ((bitset_word) 1 << j))
373 re_set_fastmap (fastmap, icase, ch); 340 re_set_fastmap (fastmap, icase, ch);
374 } 341 }
375 #ifdef RE_ENABLE_I18N 342 #ifdef RE_ENABLE_I18N
376 else if (type == COMPLEX_BRACKET) 343 else if (type == COMPLEX_BRACKET)
377 { 344 {
378 int i; 345 Idx i;
379 re_charset_t *cset = dfa->nodes[node].opr.mbcset; 346 re_charset_t *cset = dfa->nodes[node].opr.mbcset;
380 if (cset->non_match || cset->ncoll_syms || cset->nequiv_classes 347 if (cset->non_match || cset->ncoll_syms || cset->nequiv_classes
381 || cset->nranges || cset->nchar_classes) 348 || cset->nranges || cset->nchar_classes)
...@@ -389,13 +356,11 @@ re_compile_fastmap_iter (bufp, init_state, fastmap) ...@@ -389,13 +356,11 @@ re_compile_fastmap_iter (bufp, init_state, fastmap)
389 is a valid collation element, and don't catch 356 is a valid collation element, and don't catch
390 'b' since 'b' is the only collation element 357 'b' since 'b' is the only collation element
391 which starts from 'b'. */ 358 which starts from 'b'. */
392 int j, ch;
393 const int32_t *table = (const int32_t *) 359 const int32_t *table = (const int32_t *)
394 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB); 360 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
395 for (i = 0, ch = 0; i < BITSET_UINTS; ++i) 361 for (i = 0; i < SBC_MAX; ++i)
396 for (j = 0; j < UINT_BITS; ++j, ++ch) 362 if (table[i] < 0)
397 if (table[ch] < 0) 363 re_set_fastmap (fastmap, icase, i);
398 re_set_fastmap (fastmap, icase, ch);
399 } 364 }
400 # else 365 # else
401 if (dfa->mb_cur_max > 1) 366 if (dfa->mb_cur_max > 1)
...@@ -411,11 +376,11 @@ re_compile_fastmap_iter (bufp, init_state, fastmap) ...@@ -411,11 +376,11 @@ re_compile_fastmap_iter (bufp, init_state, fastmap)
411 memset (&state, '\0', sizeof (state)); 376 memset (&state, '\0', sizeof (state));
412 if (__wcrtomb (buf, cset->mbchars[i], &state) != (size_t) -1) 377 if (__wcrtomb (buf, cset->mbchars[i], &state) != (size_t) -1)
413 re_set_fastmap (fastmap, icase, *(unsigned char *) buf); 378 re_set_fastmap (fastmap, icase, *(unsigned char *) buf);
414 if ((bufp->syntax & RE_ICASE) && dfa->mb_cur_max > 1) 379 if ((bufp->re_syntax & REG_IGNORE_CASE) && dfa->mb_cur_max > 1)
415 { 380 {
416 if (__wcrtomb (buf, towlower (cset->mbchars[i]), &state) 381 if (__wcrtomb (buf, towlower (cset->mbchars[i]), &state)
417 != (size_t) -1) 382 != (size_t) -1)
418 re_set_fastmap (fastmap, 0, *(unsigned char *) buf); 383 re_set_fastmap (fastmap, false, *(unsigned char *) buf);
419 } 384 }
420 } 385 }
421 } 386 }
...@@ -428,7 +393,7 @@ re_compile_fastmap_iter (bufp, init_state, fastmap) ...@@ -428,7 +393,7 @@ re_compile_fastmap_iter (bufp, init_state, fastmap)
428 { 393 {
429 memset (fastmap, '\1', sizeof (char) * SBC_MAX); 394 memset (fastmap, '\1', sizeof (char) * SBC_MAX);
430 if (type == END_OF_RE) 395 if (type == END_OF_RE)
431 bufp->can_be_null = 1; 396 bufp->re_can_be_null = 1;
432 return; 397 return;
433 } 398 }
434 } 399 }
...@@ -440,14 +405,14 @@ re_compile_fastmap_iter (bufp, init_state, fastmap) ...@@ -440,14 +405,14 @@ re_compile_fastmap_iter (bufp, init_state, fastmap)
440 PREG is a regex_t *. We do not expect any fields to be initialized, 405 PREG is a regex_t *. We do not expect any fields to be initialized,
441 since POSIX says we shouldn't. Thus, we set 406 since POSIX says we shouldn't. Thus, we set
442 407
443 `buffer' to the compiled pattern; 408 `re_buffer' to the compiled pattern;
444 `used' to the length of the compiled pattern; 409 `re_used' to the length of the compiled pattern;
445 `syntax' to RE_SYNTAX_POSIX_EXTENDED if the 410 `re_syntax' to REG_SYNTAX_POSIX_EXTENDED if the
446 REG_EXTENDED bit in CFLAGS is set; otherwise, to 411 REG_EXTENDED bit in CFLAGS is set; otherwise, to
447 RE_SYNTAX_POSIX_BASIC; 412 REG_SYNTAX_POSIX_BASIC;
448 `newline_anchor' to REG_NEWLINE being set in CFLAGS; 413 `re_newline_anchor' to REG_NEWLINE being set in CFLAGS;
449 `fastmap' to an allocated space for the fastmap; 414 `re_fastmap' to an allocated space for the fastmap;
450 `fastmap_accurate' to zero; 415 `re_fastmap_accurate' to zero;
451 `re_nsub' to the number of subexpressions in PATTERN. 416 `re_nsub' to the number of subexpressions in PATTERN.
452 417
453 PATTERN is the address of the pattern string. 418 PATTERN is the address of the pattern string.
...@@ -471,38 +436,35 @@ re_compile_fastmap_iter (bufp, init_state, fastmap) ...@@ -471,38 +436,35 @@ re_compile_fastmap_iter (bufp, init_state, fastmap)
471 the return codes and their meanings.) */ 436 the return codes and their meanings.) */
472 437
473 int 438 int
474 regcomp (preg, pattern, cflags) 439 regcomp (regex_t *__restrict preg, const char *__restrict pattern, int cflags)
475 regex_t *__restrict preg;
476 const char *__restrict pattern;
477 int cflags;
478 { 440 {
479 reg_errcode_t ret; 441 reg_errcode_t ret;
480 reg_syntax_t syntax = ((cflags & REG_EXTENDED) ? RE_SYNTAX_POSIX_EXTENDED 442 reg_syntax_t syntax = ((cflags & REG_EXTENDED) ? REG_SYNTAX_POSIX_EXTENDED
481 : RE_SYNTAX_POSIX_BASIC); 443 : REG_SYNTAX_POSIX_BASIC);
482 444
483 preg->buffer = NULL; 445 preg->re_buffer = NULL;
484 preg->allocated = 0; 446 preg->re_allocated = 0;
485 preg->used = 0; 447 preg->re_used = 0;
486 448
487 /* Try to allocate space for the fastmap. */ 449 /* Try to allocate space for the fastmap. */
488 preg->fastmap = re_malloc (char, SBC_MAX); 450 preg->re_fastmap = re_malloc (char, SBC_MAX);
489 if (BE (preg->fastmap == NULL, 0)) 451 if (BE (preg->re_fastmap == NULL, 0))
490 return REG_ESPACE; 452 return REG_ESPACE;
491 453
492 syntax |= (cflags & REG_ICASE) ? RE_ICASE : 0; 454 syntax |= (cflags & REG_ICASE) ? REG_IGNORE_CASE : 0;
493 455
494 /* If REG_NEWLINE is set, newlines are treated differently. */ 456 /* If REG_NEWLINE is set, newlines are treated differently. */
495 if (cflags & REG_NEWLINE) 457 if (cflags & REG_NEWLINE)
496 { /* REG_NEWLINE implies neither . nor [^...] match newline. */ 458 { /* REG_NEWLINE implies neither . nor [^...] match newline. */
497 syntax &= ~RE_DOT_NEWLINE; 459 syntax &= ~REG_DOT_NEWLINE;
498 syntax |= RE_HAT_LISTS_NOT_NEWLINE; 460 syntax |= REG_HAT_LISTS_NOT_NEWLINE;
499 /* It also changes the matching behavior. */ 461 /* It also changes the matching behavior. */
500 preg->newline_anchor = 1; 462 preg->re_newline_anchor = 1;
501 } 463 }
502 else 464 else
503 preg->newline_anchor = 0; 465 preg->re_newline_anchor = 0;
504 preg->no_sub = !!(cflags & REG_NOSUB); 466 preg->re_no_sub = !!(cflags & REG_NOSUB);
505 preg->translate = NULL; 467 preg->re_translate = NULL;
506 468
507 ret = re_compile_internal (preg, pattern, strlen (pattern), syntax); 469 ret = re_compile_internal (preg, pattern, strlen (pattern), syntax);
508 470
...@@ -511,7 +473,7 @@ regcomp (preg, pattern, cflags) ...@@ -511,7 +473,7 @@ regcomp (preg, pattern, cflags)
511 if (ret == REG_ERPAREN) 473 if (ret == REG_ERPAREN)
512 ret = REG_EPAREN; 474 ret = REG_EPAREN;
513 475
514 /* We have already checked preg->fastmap != NULL. */ 476 /* We have already checked preg->re_fastmap != NULL. */
515 if (BE (ret == REG_NOERROR, 1)) 477 if (BE (ret == REG_NOERROR, 1))
516 /* Compute the fastmap now, since regexec cannot modify the pattern 478 /* Compute the fastmap now, since regexec cannot modify the pattern
517 buffer. This function never fails in this implementation. */ 479 buffer. This function never fails in this implementation. */
...@@ -519,8 +481,8 @@ regcomp (preg, pattern, cflags) ...@@ -519,8 +481,8 @@ regcomp (preg, pattern, cflags)
519 else 481 else
520 { 482 {
521 /* Some error occurred while compiling the expression. */ 483 /* Some error occurred while compiling the expression. */
522 re_free (preg->fastmap); 484 re_free (preg->re_fastmap);
523 preg->fastmap = NULL; 485 preg->re_fastmap = NULL;
524 } 486 }
525 487
526 return (int) ret; 488 return (int) ret;
...@@ -533,11 +495,8 @@ weak_alias (__regcomp, regcomp) ...@@ -533,11 +495,8 @@ weak_alias (__regcomp, regcomp)
533 from either regcomp or regexec. We don't use PREG here. */ 495 from either regcomp or regexec. We don't use PREG here. */
534 496
535 size_t 497 size_t
536 regerror (errcode, preg, errbuf, errbuf_size) 498 regerror (int errcode, const regex_t *__restrict preg,
537 int errcode; 499 char *__restrict errbuf, size_t errbuf_size)
538 const regex_t *preg;
539 char *errbuf;
540 size_t errbuf_size;
541 { 500 {
542 const char *msg; 501 const char *msg;
543 size_t msg_size; 502 size_t msg_size;
...@@ -585,11 +544,22 @@ weak_alias (__regerror, regerror) ...@@ -585,11 +544,22 @@ weak_alias (__regerror, regerror)
585 static const bitset utf8_sb_map = 544 static const bitset utf8_sb_map =
586 { 545 {
587 /* Set the first 128 bits. */ 546 /* Set the first 128 bits. */
588 # if UINT_MAX == 0xffffffff 547 # if 2 < BITSET_WORDS
589 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff 548 BITSET_WORD_MAX,
590 # else
591 # error "Add case for new unsigned int size"
592 # endif 549 # endif
550 # if 4 < BITSET_WORDS
551 BITSET_WORD_MAX,
552 # endif
553 # if 6 < BITSET_WORDS
554 BITSET_WORD_MAX,
555 # endif
556 # if 8 < BITSET_WORDS
557 # error "Invalid BITSET_WORDS"
558 # endif
559 (BITSET_WORD_MAX
560 >> (SBC_MAX % BITSET_WORD_BITS == 0
561 ? 0
562 : BITSET_WORD_BITS - SBC_MAX % BITSET_WORD_BITS))
593 }; 563 };
594 #endif 564 #endif
595 565
...@@ -597,7 +567,7 @@ static const bitset utf8_sb_map = ...@@ -597,7 +567,7 @@ static const bitset utf8_sb_map =
597 static void 567 static void
598 free_dfa_content (re_dfa_t *dfa) 568 free_dfa_content (re_dfa_t *dfa)
599 { 569 {
600 int i, j; 570 Idx i, j;
601 571
602 if (dfa->nodes) 572 if (dfa->nodes)
603 for (i = 0; i < dfa->nodes_len; ++i) 573 for (i = 0; i < dfa->nodes_len; ++i)
...@@ -645,20 +615,19 @@ free_dfa_content (re_dfa_t *dfa) ...@@ -645,20 +615,19 @@ free_dfa_content (re_dfa_t *dfa)
645 /* Free dynamically allocated space used by PREG. */ 615 /* Free dynamically allocated space used by PREG. */
646 616
647 void 617 void
648 regfree (preg) 618 regfree (regex_t *preg)
649 regex_t *preg;
650 { 619 {
651 re_dfa_t *dfa = (re_dfa_t *) preg->buffer; 620 re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
652 if (BE (dfa != NULL, 1)) 621 if (BE (dfa != NULL, 1))
653 free_dfa_content (dfa); 622 free_dfa_content (dfa);
654 preg->buffer = NULL; 623 preg->re_buffer = NULL;
655 preg->allocated = 0; 624 preg->re_allocated = 0;
656 625
657 re_free (preg->fastmap); 626 re_free (preg->re_fastmap);
658 preg->fastmap = NULL; 627 preg->re_fastmap = NULL;
659 628
660 re_free (preg->translate); 629 re_free (preg->re_translate);
661 preg->translate = NULL; 630 preg->re_translate = NULL;
662 } 631 }
663 #ifdef _LIBC 632 #ifdef _LIBC
664 weak_alias (__regfree, regfree) 633 weak_alias (__regfree, regfree)
...@@ -679,32 +648,31 @@ char * ...@@ -679,32 +648,31 @@ char *
679 regcomp/regexec above without link errors. */ 648 regcomp/regexec above without link errors. */
680 weak_function 649 weak_function
681 # endif 650 # endif
682 re_comp (s) 651 re_comp (const char *s)
683 const char *s;
684 { 652 {
685 reg_errcode_t ret; 653 reg_errcode_t ret;
686 char *fastmap; 654 char *fastmap;
687 655
688 if (!s) 656 if (!s)
689 { 657 {
690 if (!re_comp_buf.buffer) 658 if (!re_comp_buf.re_buffer)
691 return gettext ("No previous regular expression"); 659 return gettext ("No previous regular expression");
692 return 0; 660 return 0;
693 } 661 }
694 662
695 if (re_comp_buf.buffer) 663 if (re_comp_buf.re_buffer)
696 { 664 {
697 fastmap = re_comp_buf.fastmap; 665 fastmap = re_comp_buf.re_fastmap;
698 re_comp_buf.fastmap = NULL; 666 re_comp_buf.re_fastmap = NULL;
699 __regfree (&re_comp_buf); 667 __regfree (&re_comp_buf);
700 memset (&re_comp_buf, '\0', sizeof (re_comp_buf)); 668 memset (&re_comp_buf, '\0', sizeof (re_comp_buf));
701 re_comp_buf.fastmap = fastmap; 669 re_comp_buf.re_fastmap = fastmap;
702 } 670 }
703 671
704 if (re_comp_buf.fastmap == NULL) 672 if (re_comp_buf.re_fastmap == NULL)
705 { 673 {
706 re_comp_buf.fastmap = (char *) malloc (SBC_MAX); 674 re_comp_buf.re_fastmap = (char *) malloc (SBC_MAX);
707 if (re_comp_buf.fastmap == NULL) 675 if (re_comp_buf.re_fastmap == NULL)
708 return (char *) gettext (__re_error_msgid 676 return (char *) gettext (__re_error_msgid
709 + __re_error_msgid_idx[(int) REG_ESPACE]); 677 + __re_error_msgid_idx[(int) REG_ESPACE]);
710 } 678 }
...@@ -713,7 +681,7 @@ re_comp (s) ...@@ -713,7 +681,7 @@ re_comp (s)
713 don't need to initialize the pattern buffer fields which affect it. */ 681 don't need to initialize the pattern buffer fields which affect it. */
714 682
715 /* Match anchors at newlines. */ 683 /* Match anchors at newlines. */
716 re_comp_buf.newline_anchor = 1; 684 re_comp_buf.re_newline_anchor = 1;
717 685
718 ret = re_compile_internal (&re_comp_buf, s, strlen (s), re_syntax_options); 686 ret = re_compile_internal (&re_comp_buf, s, strlen (s), re_syntax_options);
719 687
...@@ -738,40 +706,37 @@ libc_freeres_fn (free_mem) ...@@ -738,40 +706,37 @@ libc_freeres_fn (free_mem)
738 SYNTAX indicate regular expression's syntax. */ 706 SYNTAX indicate regular expression's syntax. */
739 707
740 static reg_errcode_t 708 static reg_errcode_t
741 re_compile_internal (preg, pattern, length, syntax) 709 re_compile_internal (regex_t *preg, const char *pattern, Idx length,
742 regex_t *preg; 710 reg_syntax_t syntax)
743 const char * pattern;
744 int length;
745 reg_syntax_t syntax;
746 { 711 {
747 reg_errcode_t err = REG_NOERROR; 712 reg_errcode_t err = REG_NOERROR;
748 re_dfa_t *dfa; 713 re_dfa_t *dfa;
749 re_string_t regexp; 714 re_string_t regexp;
750 715
751 /* Initialize the pattern buffer. */ 716 /* Initialize the pattern buffer. */
752 preg->fastmap_accurate = 0; 717 preg->re_fastmap_accurate = 0;
753 preg->syntax = syntax; 718 preg->re_syntax = syntax;
754 preg->not_bol = preg->not_eol = 0; 719 preg->re_not_bol = preg->re_not_eol = 0;
755 preg->used = 0; 720 preg->re_used = 0;
756 preg->re_nsub = 0; 721 preg->re_nsub = 0;
757 preg->can_be_null = 0; 722 preg->re_can_be_null = 0;
758 preg->regs_allocated = REGS_UNALLOCATED; 723 preg->re_regs_allocated = REG_UNALLOCATED;
759 724
760 /* Initialize the dfa. */ 725 /* Initialize the dfa. */
761 dfa = (re_dfa_t *) preg->buffer; 726 dfa = (re_dfa_t *) preg->re_buffer;
762 if (BE (preg->allocated < sizeof (re_dfa_t), 0)) 727 if (BE (preg->re_allocated < sizeof (re_dfa_t), 0))
763 { 728 {
764 /* If zero allocated, but buffer is non-null, try to realloc 729 /* If zero allocated, but buffer is non-null, try to realloc
765 enough space. This loses if buffer's address is bogus, but 730 enough space. This loses if buffer's address is bogus, but
766 that is the user's responsibility. If ->buffer is NULL this 731 that is the user's responsibility. If buffer is null this
767 is a simple allocation. */ 732 is a simple allocation. */
768 dfa = re_realloc (preg->buffer, re_dfa_t, 1); 733 dfa = re_realloc (preg->re_buffer, re_dfa_t, 1);
769 if (dfa == NULL) 734 if (dfa == NULL)
770 return REG_ESPACE; 735 return REG_ESPACE;
771 preg->allocated = sizeof (re_dfa_t); 736 preg->re_allocated = sizeof (re_dfa_t);
772 preg->buffer = (unsigned char *) dfa; 737 preg->re_buffer = (unsigned char *) dfa;
773 } 738 }
774 preg->used = sizeof (re_dfa_t); 739 preg->re_used = sizeof (re_dfa_t);
775 740
776 __libc_lock_init (dfa->lock); 741 __libc_lock_init (dfa->lock);
777 742
...@@ -779,8 +744,8 @@ re_compile_internal (preg, pattern, length, syntax) ...@@ -779,8 +744,8 @@ re_compile_internal (preg, pattern, length, syntax)
779 if (BE (err != REG_NOERROR, 0)) 744 if (BE (err != REG_NOERROR, 0))
780 { 745 {
781 free_dfa_content (dfa); 746 free_dfa_content (dfa);
782 preg->buffer = NULL; 747 preg->re_buffer = NULL;
783 preg->allocated = 0; 748 preg->re_allocated = 0;
784 return err; 749 return err;
785 } 750 }
786 #ifdef DEBUG 751 #ifdef DEBUG
...@@ -788,16 +753,16 @@ re_compile_internal (preg, pattern, length, syntax) ...@@ -788,16 +753,16 @@ re_compile_internal (preg, pattern, length, syntax)
788 strncpy (dfa->re_str, pattern, length + 1); 753 strncpy (dfa->re_str, pattern, length + 1);
789 #endif 754 #endif
790 755
791 err = re_string_construct (&regexp, pattern, length, preg->translate, 756 err = re_string_construct (&regexp, pattern, length, preg->re_translate,
792 syntax & RE_ICASE, dfa); 757 syntax & REG_IGNORE_CASE, dfa);
793 if (BE (err != REG_NOERROR, 0)) 758 if (BE (err != REG_NOERROR, 0))
794 { 759 {
795 re_compile_internal_free_return: 760 re_compile_internal_free_return:
796 free_workarea_compile (preg); 761 free_workarea_compile (preg);
797 re_string_destruct (&regexp); 762 re_string_destruct (&regexp);
798 free_dfa_content (dfa); 763 free_dfa_content (dfa);
799 preg->buffer = NULL; 764 preg->re_buffer = NULL;
800 preg->allocated = 0; 765 preg->re_allocated = 0;
801 return err; 766 return err;
802 } 767 }
803 768
...@@ -814,7 +779,7 @@ re_compile_internal (preg, pattern, length, syntax) ...@@ -814,7 +779,7 @@ re_compile_internal (preg, pattern, length, syntax)
814 779
815 #ifdef RE_ENABLE_I18N 780 #ifdef RE_ENABLE_I18N
816 /* If possible, do searching in single byte encoding to speed things up. */ 781 /* If possible, do searching in single byte encoding to speed things up. */
817 if (dfa->is_utf8 && !(syntax & RE_ICASE) && preg->translate == NULL) 782 if (dfa->is_utf8 && !(syntax & REG_IGNORE_CASE) && preg->re_translate == NULL)
818 optimize_utf8 (dfa); 783 optimize_utf8 (dfa);
819 #endif 784 #endif
820 785
...@@ -828,8 +793,8 @@ re_compile_internal (preg, pattern, length, syntax) ...@@ -828,8 +793,8 @@ re_compile_internal (preg, pattern, length, syntax)
828 if (BE (err != REG_NOERROR, 0)) 793 if (BE (err != REG_NOERROR, 0))
829 { 794 {
830 free_dfa_content (dfa); 795 free_dfa_content (dfa);
831 preg->buffer = NULL; 796 preg->re_buffer = NULL;
832 preg->allocated = 0; 797 preg->re_allocated = 0;
833 } 798 }
834 799
835 return err; 800 return err;
...@@ -839,11 +804,9 @@ re_compile_internal (preg, pattern, length, syntax) ...@@ -839,11 +804,9 @@ re_compile_internal (preg, pattern, length, syntax)
839 as the initial length of some arrays. */ 804 as the initial length of some arrays. */
840 805
841 static reg_errcode_t 806 static reg_errcode_t
842 init_dfa (dfa, pat_len) 807 init_dfa (re_dfa_t *dfa, Idx pat_len)
843 re_dfa_t *dfa;
844 int pat_len;
845 { 808 {
846 int table_size; 809 __re_size_t table_size;
847 #ifndef _LIBC 810 #ifndef _LIBC
848 char *codeset_name; 811 char *codeset_name;
849 #endif 812 #endif
...@@ -854,16 +817,14 @@ init_dfa (dfa, pat_len) ...@@ -854,16 +817,14 @@ init_dfa (dfa, pat_len)
854 dfa->str_tree_storage_idx = BIN_TREE_STORAGE_SIZE; 817 dfa->str_tree_storage_idx = BIN_TREE_STORAGE_SIZE;
855 818
856 dfa->nodes_alloc = pat_len + 1; 819 dfa->nodes_alloc = pat_len + 1;
857 dfa->nodes = re_malloc (re_token_t, dfa->nodes_alloc); 820 dfa->nodes = re_xmalloc (re_token_t, dfa->nodes_alloc);
858
859 dfa->states_alloc = pat_len + 1;
860 821
861 /* table_size = 2 ^ ceil(log pat_len) */ 822 /* table_size = 2 ^ ceil(log pat_len) */
862 for (table_size = 1; table_size > 0; table_size <<= 1) 823 for (table_size = 1; table_size <= pat_len; table_size <<= 1)
863 if (table_size > pat_len) 824 if (0 < (Idx) -1 && table_size == 0)
864 break; 825 return REG_ESPACE;
865 826
866 dfa->state_table = calloc (sizeof (struct re_state_table_entry), table_size); 827 dfa->state_table = re_calloc (struct re_state_table_entry, table_size);
867 dfa->state_hash_mask = table_size - 1; 828 dfa->state_hash_mask = table_size - 1;
868 829
869 dfa->mb_cur_max = MB_CUR_MAX; 830 dfa->mb_cur_max = MB_CUR_MAX;
...@@ -906,20 +867,17 @@ init_dfa (dfa, pat_len) ...@@ -906,20 +867,17 @@ init_dfa (dfa, pat_len)
906 { 867 {
907 int i, j, ch; 868 int i, j, ch;
908 869
909 dfa->sb_char = (re_bitset_ptr_t) calloc (sizeof (bitset), 1); 870 dfa->sb_char = re_calloc (bitset_word, BITSET_WORDS);
910 if (BE (dfa->sb_char == NULL, 0)) 871 if (BE (dfa->sb_char == NULL, 0))
911 return REG_ESPACE; 872 return REG_ESPACE;
912 873
913 /* Clear all bits by, then set those corresponding to single 874 /* Set the bits corresponding to single byte chars. */
914 byte chars. */ 875 for (i = 0, ch = 0; i < BITSET_WORDS; ++i)
915 bitset_empty (dfa->sb_char); 876 for (j = 0; j < BITSET_WORD_BITS; ++j, ++ch)
916
917 for (i = 0, ch = 0; i < BITSET_UINTS; ++i)
918 for (j = 0; j < UINT_BITS; ++j, ++ch)
919 { 877 {
920 wint_t wch = __btowc (ch); 878 wint_t wch = __btowc (ch);
921 if (wch != WEOF) 879 if (wch != WEOF)
922 dfa->sb_char[i] |= 1 << j; 880 dfa->sb_char[i] |= (bitset_word) 1 << j;
923 # ifndef _LIBC 881 # ifndef _LIBC
924 if (isascii (ch) && wch != ch) 882 if (isascii (ch) && wch != ch)
925 dfa->map_notascii = 1; 883 dfa->map_notascii = 1;
...@@ -939,24 +897,22 @@ init_dfa (dfa, pat_len) ...@@ -939,24 +897,22 @@ init_dfa (dfa, pat_len)
939 character used by some operators like "\<", "\>", etc. */ 897 character used by some operators like "\<", "\>", etc. */
940 898
941 static void 899 static void
942 init_word_char (dfa) 900 init_word_char (re_dfa_t *dfa)
943 re_dfa_t *dfa;
944 { 901 {
945 int i, j, ch; 902 int i, j, ch;
946 dfa->word_ops_used = 1; 903 dfa->word_ops_used = 1;
947 for (i = 0, ch = 0; i < BITSET_UINTS; ++i) 904 for (i = 0, ch = 0; i < BITSET_WORDS; ++i)
948 for (j = 0; j < UINT_BITS; ++j, ++ch) 905 for (j = 0; j < BITSET_WORD_BITS; ++j, ++ch)
949 if (isalnum (ch) || ch == '_') 906 if (isalnum (ch) || ch == '_')
950 dfa->word_char[i] |= 1 << j; 907 dfa->word_char[i] |= (bitset_word) 1 << j;
951 } 908 }
952 909
953 /* Free the work area which are only used while compiling. */ 910 /* Free the work area which are only used while compiling. */
954 911
955 static void 912 static void
956 free_workarea_compile (preg) 913 free_workarea_compile (regex_t *preg)
957 regex_t *preg;
958 { 914 {
959 re_dfa_t *dfa = (re_dfa_t *) preg->buffer; 915 re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
960 bin_tree_storage_t *storage, *next; 916 bin_tree_storage_t *storage, *next;
961 for (storage = dfa->str_tree_storage; storage; storage = next) 917 for (storage = dfa->str_tree_storage; storage; storage = next)
962 { 918 {
...@@ -973,10 +929,9 @@ free_workarea_compile (preg) ...@@ -973,10 +929,9 @@ free_workarea_compile (preg)
973 /* Create initial states for all contexts. */ 929 /* Create initial states for all contexts. */
974 930
975 static reg_errcode_t 931 static reg_errcode_t
976 create_initial_state (dfa) 932 create_initial_state (re_dfa_t *dfa)
977 re_dfa_t *dfa;
978 { 933 {
979 int first, i; 934 Idx first, i;
980 reg_errcode_t err; 935 reg_errcode_t err;
981 re_node_set init_nodes; 936 re_node_set init_nodes;
982 937
...@@ -995,10 +950,10 @@ create_initial_state (dfa) ...@@ -995,10 +950,10 @@ create_initial_state (dfa)
995 if (dfa->nbackref > 0) 950 if (dfa->nbackref > 0)
996 for (i = 0; i < init_nodes.nelem; ++i) 951 for (i = 0; i < init_nodes.nelem; ++i)
997 { 952 {
998 int node_idx = init_nodes.elems[i]; 953 Idx node_idx = init_nodes.elems[i];
999 re_token_type_t type = dfa->nodes[node_idx].type; 954 re_token_type_t type = dfa->nodes[node_idx].type;
1000 955
1001 int clexp_idx; 956 Idx clexp_idx;
1002 if (type != OP_BACK_REF) 957 if (type != OP_BACK_REF)
1003 continue; 958 continue;
1004 for (clexp_idx = 0; clexp_idx < init_nodes.nelem; ++clexp_idx) 959 for (clexp_idx = 0; clexp_idx < init_nodes.nelem; ++clexp_idx)
...@@ -1014,7 +969,7 @@ create_initial_state (dfa) ...@@ -1014,7 +969,7 @@ create_initial_state (dfa)
1014 969
1015 if (type == OP_BACK_REF) 970 if (type == OP_BACK_REF)
1016 { 971 {
1017 int dest_idx = dfa->edests[node_idx].elems[0]; 972 Idx dest_idx = dfa->edests[node_idx].elems[0];
1018 if (!re_node_set_contains (&init_nodes, dest_idx)) 973 if (!re_node_set_contains (&init_nodes, dest_idx))
1019 { 974 {
1020 re_node_set_merge (&init_nodes, dfa->eclosures + dest_idx); 975 re_node_set_merge (&init_nodes, dfa->eclosures + dest_idx);
...@@ -1056,17 +1011,19 @@ create_initial_state (dfa) ...@@ -1056,17 +1011,19 @@ create_initial_state (dfa)
1056 DFA nodes where needed. */ 1011 DFA nodes where needed. */
1057 1012
1058 static void 1013 static void
1059 optimize_utf8 (dfa) 1014 optimize_utf8 (re_dfa_t *dfa)
1060 re_dfa_t *dfa;
1061 { 1015 {
1062 int node, i, mb_chars = 0, has_period = 0; 1016 Idx node;
1017 int i;
1018 bool mb_chars = false;
1019 bool has_period = false;
1063 1020
1064 for (node = 0; node < dfa->nodes_len; ++node) 1021 for (node = 0; node < dfa->nodes_len; ++node)
1065 switch (dfa->nodes[node].type) 1022 switch (dfa->nodes[node].type)
1066 { 1023 {
1067 case CHARACTER: 1024 case CHARACTER:
1068 if (dfa->nodes[node].opr.c >= 0x80) 1025 if (dfa->nodes[node].opr.c >= 0x80)
1069 mb_chars = 1; 1026 mb_chars = true;
1070 break; 1027 break;
1071 case ANCHOR: 1028 case ANCHOR:
1072 switch (dfa->nodes[node].opr.idx) 1029 switch (dfa->nodes[node].opr.idx)
...@@ -1082,7 +1039,7 @@ optimize_utf8 (dfa) ...@@ -1082,7 +1039,7 @@ optimize_utf8 (dfa)
1082 } 1039 }
1083 break; 1040 break;
1084 case OP_PERIOD: 1041 case OP_PERIOD:
1085 has_period = 1; 1042 has_period = true;
1086 break; 1043 break;
1087 case OP_BACK_REF: 1044 case OP_BACK_REF:
1088 case OP_ALT: 1045 case OP_ALT:
...@@ -1095,9 +1052,18 @@ optimize_utf8 (dfa) ...@@ -1095,9 +1052,18 @@ optimize_utf8 (dfa)
1095 return; 1052 return;
1096 case SIMPLE_BRACKET: 1053 case SIMPLE_BRACKET:
1097 /* Just double check. */ 1054 /* Just double check. */
1098 for (i = 0x80 / UINT_BITS; i < BITSET_UINTS; ++i) 1055 {
1099 if (dfa->nodes[node].opr.sbcset[i]) 1056 int rshift =
1100 return; 1057 (SBC_MAX / 2 % BITSET_WORD_BITS == 0
1058 ? 0
1059 : BITSET_WORD_BITS - SBC_MAX / 2 % BITSET_WORD_BITS);
1060 for (i = SBC_MAX / 2 / BITSET_WORD_BITS; i < BITSET_WORDS; ++i)
1061 {
1062 if (dfa->nodes[node].opr.sbcset[i] >> rshift != 0)
1063 return;
1064 rshift = 0;
1065 }
1066 }
1101 break; 1067 break;
1102 default: 1068 default:
1103 abort (); 1069 abort ();
...@@ -1124,25 +1090,24 @@ optimize_utf8 (dfa) ...@@ -1124,25 +1090,24 @@ optimize_utf8 (dfa)
1124 "eclosure", and "inveclosure". */ 1090 "eclosure", and "inveclosure". */
1125 1091
1126 static reg_errcode_t 1092 static reg_errcode_t
1127 analyze (preg) 1093 analyze (regex_t *preg)
1128 regex_t *preg;
1129 { 1094 {
1130 re_dfa_t *dfa = (re_dfa_t *) preg->buffer; 1095 re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
1131 reg_errcode_t ret; 1096 reg_errcode_t ret;
1132 1097
1133 /* Allocate arrays. */ 1098 /* Allocate arrays. */
1134 dfa->nexts = re_malloc (int, dfa->nodes_alloc); 1099 dfa->nexts = re_malloc (Idx, dfa->nodes_alloc);
1135 dfa->org_indices = re_malloc (int, dfa->nodes_alloc); 1100 dfa->org_indices = re_malloc (Idx, dfa->nodes_alloc);
1136 dfa->edests = re_malloc (re_node_set, dfa->nodes_alloc); 1101 dfa->edests = re_xmalloc (re_node_set, dfa->nodes_alloc);
1137 dfa->eclosures = re_malloc (re_node_set, dfa->nodes_alloc); 1102 dfa->eclosures = re_malloc (re_node_set, dfa->nodes_alloc);
1138 if (BE (dfa->nexts == NULL || dfa->org_indices == NULL || dfa->edests == NULL 1103 if (BE (dfa->nexts == NULL || dfa->org_indices == NULL || dfa->edests == NULL
1139 || dfa->eclosures == NULL, 0)) 1104 || dfa->eclosures == NULL, 0))
1140 return REG_ESPACE; 1105 return REG_ESPACE;
1141 1106
1142 dfa->subexp_map = re_malloc (int, preg->re_nsub); 1107 dfa->subexp_map = re_xmalloc (Idx, preg->re_nsub);
1143 if (dfa->subexp_map != NULL) 1108 if (dfa->subexp_map != NULL)
1144 { 1109 {
1145 int i; 1110 Idx i;
1146 for (i = 0; i < preg->re_nsub; i++) 1111 for (i = 0; i < preg->re_nsub; i++)
1147 dfa->subexp_map[i] = i; 1112 dfa->subexp_map[i] = i;
1148 preorder (dfa->str_tree, optimize_subexps, dfa); 1113 preorder (dfa->str_tree, optimize_subexps, dfa);
...@@ -1172,10 +1137,10 @@ analyze (preg) ...@@ -1172,10 +1137,10 @@ analyze (preg)
1172 1137
1173 /* We only need this during the prune_impossible_nodes pass in regexec.c; 1138 /* We only need this during the prune_impossible_nodes pass in regexec.c;
1174 skip it if p_i_n will not run, as calc_inveclosure can be quadratic. */ 1139 skip it if p_i_n will not run, as calc_inveclosure can be quadratic. */
1175 if ((!preg->no_sub && preg->re_nsub > 0 && dfa->has_plural_match) 1140 if ((!preg->re_no_sub && preg->re_nsub > 0 && dfa->has_plural_match)
1176 || dfa->nbackref) 1141 || dfa->nbackref)
1177 { 1142 {
1178 dfa->inveclosures = re_malloc (re_node_set, dfa->nodes_len); 1143 dfa->inveclosures = re_xmalloc (re_node_set, dfa->nodes_len);
1179 if (BE (dfa->inveclosures == NULL, 0)) 1144 if (BE (dfa->inveclosures == NULL, 0))
1180 return REG_ESPACE; 1145 return REG_ESPACE;
1181 ret = calc_inveclosure (dfa); 1146 ret = calc_inveclosure (dfa);
...@@ -1188,10 +1153,8 @@ analyze (preg) ...@@ -1188,10 +1153,8 @@ analyze (preg)
1188 implement parse tree visits. Instead, we use parent pointers and 1153 implement parse tree visits. Instead, we use parent pointers and
1189 some hairy code in these two functions. */ 1154 some hairy code in these two functions. */
1190 static reg_errcode_t 1155 static reg_errcode_t
1191 postorder (root, fn, extra) 1156 postorder (bin_tree_t *root, reg_errcode_t (fn (void *, bin_tree_t *)),
1192 bin_tree_t *root; 1157 void *extra)
1193 reg_errcode_t (fn (void *, bin_tree_t *));
1194 void *extra;
1195 { 1158 {
1196 bin_tree_t *node, *prev; 1159 bin_tree_t *node, *prev;
1197 1160
...@@ -1222,10 +1185,8 @@ postorder (root, fn, extra) ...@@ -1222,10 +1185,8 @@ postorder (root, fn, extra)
1222 } 1185 }
1223 1186
1224 static reg_errcode_t 1187 static reg_errcode_t
1225 preorder (root, fn, extra) 1188 preorder (bin_tree_t *root, reg_errcode_t (fn (void *, bin_tree_t *)),
1226 bin_tree_t *root; 1189 void *extra)
1227 reg_errcode_t (fn (void *, bin_tree_t *));
1228 void *extra;
1229 { 1190 {
1230 bin_tree_t *node; 1191 bin_tree_t *node;
1231 1192
...@@ -1257,9 +1218,7 @@ preorder (root, fn, extra) ...@@ -1257,9 +1218,7 @@ preorder (root, fn, extra)
1257 re_search_internal to map the inner one's opr.idx to this one's. Adjust 1218 re_search_internal to map the inner one's opr.idx to this one's. Adjust
1258 backreferences as well. Requires a preorder visit. */ 1219 backreferences as well. Requires a preorder visit. */
1259 static reg_errcode_t 1220 static reg_errcode_t
1260 optimize_subexps (extra, node) 1221 optimize_subexps (void *extra, bin_tree_t *node)
1261 void *extra;
1262 bin_tree_t *node;
1263 { 1222 {
1264 re_dfa_t *dfa = (re_dfa_t *) extra; 1223 re_dfa_t *dfa = (re_dfa_t *) extra;
1265 1224
...@@ -1273,15 +1232,15 @@ optimize_subexps (extra, node) ...@@ -1273,15 +1232,15 @@ optimize_subexps (extra, node)
1273 else if (node->token.type == SUBEXP 1232 else if (node->token.type == SUBEXP
1274 && node->left && node->left->token.type == SUBEXP) 1233 && node->left && node->left->token.type == SUBEXP)
1275 { 1234 {
1276 int other_idx = node->left->token.opr.idx; 1235 Idx other_idx = node->left->token.opr.idx;
1277 1236
1278 node->left = node->left->left; 1237 node->left = node->left->left;
1279 if (node->left) 1238 if (node->left)
1280 node->left->parent = node; 1239 node->left->parent = node;
1281 1240
1282 dfa->subexp_map[other_idx] = dfa->subexp_map[node->token.opr.idx]; 1241 dfa->subexp_map[other_idx] = dfa->subexp_map[node->token.opr.idx];
1283 if (other_idx < 8 * sizeof (dfa->used_bkref_map)) 1242 if (other_idx < BITSET_WORD_BITS)
1284 dfa->used_bkref_map &= ~(1 << other_idx); 1243 dfa->used_bkref_map &= ~ ((bitset_word) 1 << other_idx);
1285 } 1244 }
1286 1245
1287 return REG_NOERROR; 1246 return REG_NOERROR;
...@@ -1290,9 +1249,7 @@ optimize_subexps (extra, node) ...@@ -1290,9 +1249,7 @@ optimize_subexps (extra, node)
1290 /* Lowering pass: Turn each SUBEXP node into the appropriate concatenation 1249 /* Lowering pass: Turn each SUBEXP node into the appropriate concatenation
1291 of OP_OPEN_SUBEXP, the body of the SUBEXP (if any) and OP_CLOSE_SUBEXP. */ 1250 of OP_OPEN_SUBEXP, the body of the SUBEXP (if any) and OP_CLOSE_SUBEXP. */
1292 static reg_errcode_t 1251 static reg_errcode_t
1293 lower_subexps (extra, node) 1252 lower_subexps (void *extra, bin_tree_t *node)
1294 void *extra;
1295 bin_tree_t *node;
1296 { 1253 {
1297 regex_t *preg = (regex_t *) extra; 1254 regex_t *preg = (regex_t *) extra;
1298 reg_errcode_t err = REG_NOERROR; 1255 reg_errcode_t err = REG_NOERROR;
...@@ -1314,23 +1271,20 @@ lower_subexps (extra, node) ...@@ -1314,23 +1271,20 @@ lower_subexps (extra, node)
1314 } 1271 }
1315 1272
1316 static bin_tree_t * 1273 static bin_tree_t *
1317 lower_subexp (err, preg, node) 1274 lower_subexp (reg_errcode_t *err, regex_t *preg, bin_tree_t *node)
1318 reg_errcode_t *err;
1319 regex_t *preg;
1320 bin_tree_t *node;
1321 { 1275 {
1322 re_dfa_t *dfa = (re_dfa_t *) preg->buffer; 1276 re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
1323 bin_tree_t *body = node->left; 1277 bin_tree_t *body = node->left;
1324 bin_tree_t *op, *cls, *tree1, *tree; 1278 bin_tree_t *op, *cls, *tree1, *tree;
1325 1279
1326 if (preg->no_sub 1280 if (preg->re_no_sub
1327 /* We do not optimize empty subexpressions, because otherwise we may 1281 /* We do not optimize empty subexpressions, because otherwise we may
1328 have bad CONCAT nodes with NULL children. This is obviously not 1282 have bad CONCAT nodes with NULL children. This is obviously not
1329 very common, so we do not lose much. An example that triggers 1283 very common, so we do not lose much. An example that triggers
1330 this case is the sed "script" /\(\)/x. */ 1284 this case is the sed "script" /\(\)/x. */
1331 && node->left != NULL 1285 && node->left != NULL
1332 && (node->token.opr.idx >= 8 * sizeof (dfa->used_bkref_map) 1286 && ! (node->token.opr.idx < BITSET_WORD_BITS
1333 || !(dfa->used_bkref_map & (1 << node->token.opr.idx)))) 1287 && dfa->used_bkref_map & ((bitset_word) 1 << node->token.opr.idx)))
1334 return node->left; 1288 return node->left;
1335 1289
1336 /* Convert the SUBEXP node to the concatenation of an 1290 /* Convert the SUBEXP node to the concatenation of an
...@@ -1353,9 +1307,7 @@ lower_subexp (err, preg, node) ...@@ -1353,9 +1307,7 @@ lower_subexp (err, preg, node)
1353 /* Pass 1 in building the NFA: compute FIRST and create unlinked automaton 1307 /* Pass 1 in building the NFA: compute FIRST and create unlinked automaton
1354 nodes. Requires a postorder visit. */ 1308 nodes. Requires a postorder visit. */
1355 static reg_errcode_t 1309 static reg_errcode_t
1356 calc_first (extra, node) 1310 calc_first (void *extra, bin_tree_t *node)
1357 void *extra;
1358 bin_tree_t *node;
1359 { 1311 {
1360 re_dfa_t *dfa = (re_dfa_t *) extra; 1312 re_dfa_t *dfa = (re_dfa_t *) extra;
1361 if (node->token.type == CONCAT) 1313 if (node->token.type == CONCAT)
...@@ -1367,7 +1319,7 @@ calc_first (extra, node) ...@@ -1367,7 +1319,7 @@ calc_first (extra, node)
1367 { 1319 {
1368 node->first = node; 1320 node->first = node;
1369 node->node_idx = re_dfa_add_node (dfa, node->token); 1321 node->node_idx = re_dfa_add_node (dfa, node->token);
1370 if (BE (node->node_idx == -1, 0)) 1322 if (BE (node->node_idx == REG_MISSING, 0))
1371 return REG_ESPACE; 1323 return REG_ESPACE;
1372 } 1324 }
1373 return REG_NOERROR; 1325 return REG_NOERROR;
...@@ -1375,9 +1327,7 @@ calc_first (extra, node) ...@@ -1375,9 +1327,7 @@ calc_first (extra, node)
1375 1327
1376 /* Pass 2: compute NEXT on the tree. Preorder visit. */ 1328 /* Pass 2: compute NEXT on the tree. Preorder visit. */
1377 static reg_errcode_t 1329 static reg_errcode_t
1378 calc_next (extra, node) 1330 calc_next (void *extra, bin_tree_t *node)
1379 void *extra;
1380 bin_tree_t *node;
1381 { 1331 {
1382 switch (node->token.type) 1332 switch (node->token.type)
1383 { 1333 {
...@@ -1400,12 +1350,10 @@ calc_next (extra, node) ...@@ -1400,12 +1350,10 @@ calc_next (extra, node)
1400 1350
1401 /* Pass 3: link all DFA nodes to their NEXT node (any order will do). */ 1351 /* Pass 3: link all DFA nodes to their NEXT node (any order will do). */
1402 static reg_errcode_t 1352 static reg_errcode_t
1403 link_nfa_nodes (extra, node) 1353 link_nfa_nodes (void *extra, bin_tree_t *node)
1404 void *extra;
1405 bin_tree_t *node;
1406 { 1354 {
1407 re_dfa_t *dfa = (re_dfa_t *) extra; 1355 re_dfa_t *dfa = (re_dfa_t *) extra;
1408 int idx = node->node_idx; 1356 Idx idx = node->node_idx;
1409 reg_errcode_t err = REG_NOERROR; 1357 reg_errcode_t err = REG_NOERROR;
1410 1358
1411 switch (node->token.type) 1359 switch (node->token.type)
...@@ -1420,7 +1368,7 @@ link_nfa_nodes (extra, node) ...@@ -1420,7 +1368,7 @@ link_nfa_nodes (extra, node)
1420 case OP_DUP_ASTERISK: 1368 case OP_DUP_ASTERISK:
1421 case OP_ALT: 1369 case OP_ALT:
1422 { 1370 {
1423 int left, right; 1371 Idx left, right;
1424 dfa->has_plural_match = 1; 1372 dfa->has_plural_match = 1;
1425 if (node->left != NULL) 1373 if (node->left != NULL)
1426 left = node->left->first->node_idx; 1374 left = node->left->first->node_idx;
...@@ -1430,8 +1378,8 @@ link_nfa_nodes (extra, node) ...@@ -1430,8 +1378,8 @@ link_nfa_nodes (extra, node)
1430 right = node->right->first->node_idx; 1378 right = node->right->first->node_idx;
1431 else 1379 else
1432 right = node->next->node_idx; 1380 right = node->next->node_idx;
1433 assert (left > -1); 1381 assert (REG_VALID_INDEX (left));
1434 assert (right > -1); 1382 assert (REG_VALID_INDEX (right));
1435 err = re_node_set_init_2 (dfa->edests + idx, left, right); 1383 err = re_node_set_init_2 (dfa->edests + idx, left, right);
1436 } 1384 }
1437 break; 1385 break;
...@@ -1462,18 +1410,16 @@ link_nfa_nodes (extra, node) ...@@ -1462,18 +1410,16 @@ link_nfa_nodes (extra, node)
1462 to their own constraint. */ 1410 to their own constraint. */
1463 1411
1464 static reg_errcode_t 1412 static reg_errcode_t
1465 duplicate_node_closure (dfa, top_org_node, top_clone_node, root_node, 1413 duplicate_node_closure (re_dfa_t *dfa, Idx top_org_node,
1466 init_constraint) 1414 Idx top_clone_node, Idx root_node,
1467 re_dfa_t *dfa; 1415 unsigned int init_constraint)
1468 int top_org_node, top_clone_node, root_node;
1469 unsigned int init_constraint;
1470 { 1416 {
1471 reg_errcode_t err; 1417 Idx org_node, clone_node;
1472 int org_node, clone_node, ret; 1418 bool ok;
1473 unsigned int constraint = init_constraint; 1419 unsigned int constraint = init_constraint;
1474 for (org_node = top_org_node, clone_node = top_clone_node;;) 1420 for (org_node = top_org_node, clone_node = top_clone_node;;)
1475 { 1421 {
1476 int org_dest, clone_dest; 1422 Idx org_dest, clone_dest;
1477 if (dfa->nodes[org_node].type == OP_BACK_REF) 1423 if (dfa->nodes[org_node].type == OP_BACK_REF)
1478 { 1424 {
1479 /* If the back reference epsilon-transit, its destination must 1425 /* If the back reference epsilon-transit, its destination must
...@@ -1482,12 +1428,12 @@ duplicate_node_closure (dfa, top_org_node, top_clone_node, root_node, ...@@ -1482,12 +1428,12 @@ duplicate_node_closure (dfa, top_org_node, top_clone_node, root_node,
1482 edests of the back reference. */ 1428 edests of the back reference. */
1483 org_dest = dfa->nexts[org_node]; 1429 org_dest = dfa->nexts[org_node];
1484 re_node_set_empty (dfa->edests + clone_node); 1430 re_node_set_empty (dfa->edests + clone_node);
1485 err = duplicate_node (&clone_dest, dfa, org_dest, constraint); 1431 clone_dest = duplicate_node (dfa, org_dest, constraint);
1486 if (BE (err != REG_NOERROR, 0)) 1432 if (BE (clone_dest == REG_MISSING, 0))
1487 return err; 1433 return REG_ESPACE;
1488 dfa->nexts[clone_node] = dfa->nexts[org_node]; 1434 dfa->nexts[clone_node] = dfa->nexts[org_node];
1489 ret = re_node_set_insert (dfa->edests + clone_node, clone_dest); 1435 ok = re_node_set_insert (dfa->edests + clone_node, clone_dest);
1490 if (BE (ret < 0, 0)) 1436 if (BE (! ok, 0))
1491 return REG_ESPACE; 1437 return REG_ESPACE;
1492 } 1438 }
1493 else if (dfa->edests[org_node].nelem == 0) 1439 else if (dfa->edests[org_node].nelem == 0)
...@@ -1512,19 +1458,19 @@ duplicate_node_closure (dfa, top_org_node, top_clone_node, root_node, ...@@ -1512,19 +1458,19 @@ duplicate_node_closure (dfa, top_org_node, top_clone_node, root_node,
1512 /* ...but if the node is root_node itself, it means the 1458 /* ...but if the node is root_node itself, it means the
1513 epsilon closure have a loop, then tie it to the 1459 epsilon closure have a loop, then tie it to the
1514 destination of the root_node. */ 1460 destination of the root_node. */
1515 ret = re_node_set_insert (dfa->edests + clone_node, 1461 ok = re_node_set_insert (dfa->edests + clone_node,
1516 org_dest); 1462 org_dest);
1517 if (BE (ret < 0, 0)) 1463 if (BE (! ok, 0))
1518 return REG_ESPACE; 1464 return REG_ESPACE;
1519 break; 1465 break;
1520 } 1466 }
1521 constraint |= dfa->nodes[org_node].opr.ctx_type; 1467 constraint |= dfa->nodes[org_node].opr.ctx_type;
1522 } 1468 }
1523 err = duplicate_node (&clone_dest, dfa, org_dest, constraint); 1469 clone_dest = duplicate_node (dfa, org_dest, constraint);
1524 if (BE (err != REG_NOERROR, 0)) 1470 if (BE (clone_dest == REG_MISSING, 0))
1525 return err; 1471 return REG_ESPACE;
1526 ret = re_node_set_insert (dfa->edests + clone_node, clone_dest); 1472 ok = re_node_set_insert (dfa->edests + clone_node, clone_dest);
1527 if (BE (ret < 0, 0)) 1473 if (BE (! ok, 0))
1528 return REG_ESPACE; 1474 return REG_ESPACE;
1529 } 1475 }
1530 else /* dfa->edests[org_node].nelem == 2 */ 1476 else /* dfa->edests[org_node].nelem == 2 */
...@@ -1535,14 +1481,15 @@ duplicate_node_closure (dfa, top_org_node, top_clone_node, root_node, ...@@ -1535,14 +1481,15 @@ duplicate_node_closure (dfa, top_org_node, top_clone_node, root_node,
1535 re_node_set_empty (dfa->edests + clone_node); 1481 re_node_set_empty (dfa->edests + clone_node);
1536 /* Search for a duplicated node which satisfies the constraint. */ 1482 /* Search for a duplicated node which satisfies the constraint. */
1537 clone_dest = search_duplicated_node (dfa, org_dest, constraint); 1483 clone_dest = search_duplicated_node (dfa, org_dest, constraint);
1538 if (clone_dest == -1) 1484 if (clone_dest == REG_MISSING)
1539 { 1485 {
1540 /* There are no such a duplicated node, create a new one. */ 1486 /* There are no such a duplicated node, create a new one. */
1541 err = duplicate_node (&clone_dest, dfa, org_dest, constraint); 1487 reg_errcode_t err;
1542 if (BE (err != REG_NOERROR, 0)) 1488 clone_dest = duplicate_node (dfa, org_dest, constraint);
1543 return err; 1489 if (BE (clone_dest == REG_MISSING, 0))
1544 ret = re_node_set_insert (dfa->edests + clone_node, clone_dest); 1490 return REG_ESPACE;
1545 if (BE (ret < 0, 0)) 1491 ok = re_node_set_insert (dfa->edests + clone_node, clone_dest);
1492 if (BE (! ok, 0))
1546 return REG_ESPACE; 1493 return REG_ESPACE;
1547 err = duplicate_node_closure (dfa, org_dest, clone_dest, 1494 err = duplicate_node_closure (dfa, org_dest, clone_dest,
1548 root_node, constraint); 1495 root_node, constraint);
...@@ -1553,17 +1500,17 @@ duplicate_node_closure (dfa, top_org_node, top_clone_node, root_node, ...@@ -1553,17 +1500,17 @@ duplicate_node_closure (dfa, top_org_node, top_clone_node, root_node,
1553 { 1500 {
1554 /* There are a duplicated node which satisfy the constraint, 1501 /* There are a duplicated node which satisfy the constraint,
1555 use it to avoid infinite loop. */ 1502 use it to avoid infinite loop. */
1556 ret = re_node_set_insert (dfa->edests + clone_node, clone_dest); 1503 ok = re_node_set_insert (dfa->edests + clone_node, clone_dest);
1557 if (BE (ret < 0, 0)) 1504 if (BE (! ok, 0))
1558 return REG_ESPACE; 1505 return REG_ESPACE;
1559 } 1506 }
1560 1507
1561 org_dest = dfa->edests[org_node].elems[1]; 1508 org_dest = dfa->edests[org_node].elems[1];
1562 err = duplicate_node (&clone_dest, dfa, org_dest, constraint); 1509 clone_dest = duplicate_node (dfa, org_dest, constraint);
1563 if (BE (err != REG_NOERROR, 0)) 1510 if (BE (clone_dest == REG_MISSING, 0))
1564 return err; 1511 return REG_ESPACE;
1565 ret = re_node_set_insert (dfa->edests + clone_node, clone_dest); 1512 ok = re_node_set_insert (dfa->edests + clone_node, clone_dest);
1566 if (BE (ret < 0, 0)) 1513 if (BE (! ok, 0))
1567 return REG_ESPACE; 1514 return REG_ESPACE;
1568 } 1515 }
1569 org_node = org_dest; 1516 org_node = org_dest;
...@@ -1575,61 +1522,56 @@ duplicate_node_closure (dfa, top_org_node, top_clone_node, root_node, ...@@ -1575,61 +1522,56 @@ duplicate_node_closure (dfa, top_org_node, top_clone_node, root_node,
1575 /* Search for a node which is duplicated from the node ORG_NODE, and 1522 /* Search for a node which is duplicated from the node ORG_NODE, and
1576 satisfies the constraint CONSTRAINT. */ 1523 satisfies the constraint CONSTRAINT. */
1577 1524
1578 static int 1525 static Idx
1579 search_duplicated_node (dfa, org_node, constraint) 1526 search_duplicated_node (const re_dfa_t *dfa, Idx org_node,
1580 re_dfa_t *dfa; 1527 unsigned int constraint)
1581 int org_node;
1582 unsigned int constraint;
1583 { 1528 {
1584 int idx; 1529 Idx idx;
1585 for (idx = dfa->nodes_len - 1; dfa->nodes[idx].duplicated && idx > 0; --idx) 1530 for (idx = dfa->nodes_len - 1; dfa->nodes[idx].duplicated && idx > 0; --idx)
1586 { 1531 {
1587 if (org_node == dfa->org_indices[idx] 1532 if (org_node == dfa->org_indices[idx]
1588 && constraint == dfa->nodes[idx].constraint) 1533 && constraint == dfa->nodes[idx].constraint)
1589 return idx; /* Found. */ 1534 return idx; /* Found. */
1590 } 1535 }
1591 return -1; /* Not found. */ 1536 return REG_MISSING; /* Not found. */
1592 } 1537 }
1593 1538
1594 /* Duplicate the node whose index is ORG_IDX and set the constraint CONSTRAINT. 1539 /* Duplicate the node whose index is ORG_IDX and set the constraint CONSTRAINT.
1595 The new index will be stored in NEW_IDX and return REG_NOERROR if succeeded, 1540 Return the index of the new node, or REG_MISSING if insufficient storage is
1596 otherwise return the error code. */ 1541 available. */
1597 1542
1598 static reg_errcode_t 1543 static Idx
1599 duplicate_node (new_idx, dfa, org_idx, constraint) 1544 duplicate_node (re_dfa_t *dfa, Idx org_idx, unsigned int constraint)
1600 re_dfa_t *dfa;
1601 int *new_idx, org_idx;
1602 unsigned int constraint;
1603 { 1545 {
1604 int dup_idx = re_dfa_add_node (dfa, dfa->nodes[org_idx]); 1546 Idx dup_idx = re_dfa_add_node (dfa, dfa->nodes[org_idx]);
1605 if (BE (dup_idx == -1, 0)) 1547 if (BE (dup_idx != REG_MISSING, 1))
1606 return REG_ESPACE; 1548 {
1607 dfa->nodes[dup_idx].constraint = constraint; 1549 dfa->nodes[dup_idx].constraint = constraint;
1608 if (dfa->nodes[org_idx].type == ANCHOR) 1550 if (dfa->nodes[org_idx].type == ANCHOR)
1609 dfa->nodes[dup_idx].constraint |= dfa->nodes[org_idx].opr.ctx_type; 1551 dfa->nodes[dup_idx].constraint |= dfa->nodes[org_idx].opr.ctx_type;
1610 dfa->nodes[dup_idx].duplicated = 1; 1552 dfa->nodes[dup_idx].duplicated = 1;
1611 1553
1612 /* Store the index of the original node. */ 1554 /* Store the index of the original node. */
1613 dfa->org_indices[dup_idx] = org_idx; 1555 dfa->org_indices[dup_idx] = org_idx;
1614 *new_idx = dup_idx; 1556 }
1615 return REG_NOERROR; 1557 return dup_idx;
1616 } 1558 }
1617 1559
1618 static reg_errcode_t 1560 static reg_errcode_t
1619 calc_inveclosure (dfa) 1561 calc_inveclosure (re_dfa_t *dfa)
1620 re_dfa_t *dfa;
1621 { 1562 {
1622 int src, idx, ret; 1563 Idx src, idx;
1564 bool ok;
1623 for (idx = 0; idx < dfa->nodes_len; ++idx) 1565 for (idx = 0; idx < dfa->nodes_len; ++idx)
1624 re_node_set_init_empty (dfa->inveclosures + idx); 1566 re_node_set_init_empty (dfa->inveclosures + idx);
1625 1567
1626 for (src = 0; src < dfa->nodes_len; ++src) 1568 for (src = 0; src < dfa->nodes_len; ++src)
1627 { 1569 {
1628 int *elems = dfa->eclosures[src].elems; 1570 Idx *elems = dfa->eclosures[src].elems;
1629 for (idx = 0; idx < dfa->eclosures[src].nelem; ++idx) 1571 for (idx = 0; idx < dfa->eclosures[src].nelem; ++idx)
1630 { 1572 {
1631 ret = re_node_set_insert_last (dfa->inveclosures + elems[idx], src); 1573 ok = re_node_set_insert_last (dfa->inveclosures + elems[idx], src);
1632 if (BE (ret == -1, 0)) 1574 if (BE (! ok, 0))
1633 return REG_ESPACE; 1575 return REG_ESPACE;
1634 } 1576 }
1635 } 1577 }
...@@ -1640,14 +1582,14 @@ calc_inveclosure (dfa) ...@@ -1640,14 +1582,14 @@ calc_inveclosure (dfa)
1640 /* Calculate "eclosure" for all the node in DFA. */ 1582 /* Calculate "eclosure" for all the node in DFA. */
1641 1583
1642 static reg_errcode_t 1584 static reg_errcode_t
1643 calc_eclosure (dfa) 1585 calc_eclosure (re_dfa_t *dfa)
1644 re_dfa_t *dfa;
1645 { 1586 {
1646 int node_idx, incomplete; 1587 Idx node_idx;
1588 bool incomplete;
1647 #ifdef DEBUG 1589 #ifdef DEBUG
1648 assert (dfa->nodes_len > 0); 1590 assert (dfa->nodes_len > 0);
1649 #endif 1591 #endif
1650 incomplete = 0; 1592 incomplete = false;
1651 /* For each nodes, calculate epsilon closure. */ 1593 /* For each nodes, calculate epsilon closure. */
1652 for (node_idx = 0; ; ++node_idx) 1594 for (node_idx = 0; ; ++node_idx)
1653 { 1595 {
...@@ -1657,25 +1599,25 @@ calc_eclosure (dfa) ...@@ -1657,25 +1599,25 @@ calc_eclosure (dfa)
1657 { 1599 {
1658 if (!incomplete) 1600 if (!incomplete)
1659 break; 1601 break;
1660 incomplete = 0; 1602 incomplete = false;
1661 node_idx = 0; 1603 node_idx = 0;
1662 } 1604 }
1663 1605
1664 #ifdef DEBUG 1606 #ifdef DEBUG
1665 assert (dfa->eclosures[node_idx].nelem != -1); 1607 assert (dfa->eclosures[node_idx].nelem != REG_MISSING);
1666 #endif 1608 #endif
1667 1609
1668 /* If we have already calculated, skip it. */ 1610 /* If we have already calculated, skip it. */
1669 if (dfa->eclosures[node_idx].nelem != 0) 1611 if (dfa->eclosures[node_idx].nelem != 0)
1670 continue; 1612 continue;
1671 /* Calculate epsilon closure of `node_idx'. */ 1613 /* Calculate epsilon closure of `node_idx'. */
1672 err = calc_eclosure_iter (&eclosure_elem, dfa, node_idx, 1); 1614 err = calc_eclosure_iter (&eclosure_elem, dfa, node_idx, true);
1673 if (BE (err != REG_NOERROR, 0)) 1615 if (BE (err != REG_NOERROR, 0))
1674 return err; 1616 return err;
1675 1617
1676 if (dfa->eclosures[node_idx].nelem == 0) 1618 if (dfa->eclosures[node_idx].nelem == 0)
1677 { 1619 {
1678 incomplete = 1; 1620 incomplete = true;
1679 re_node_set_free (&eclosure_elem); 1621 re_node_set_free (&eclosure_elem);
1680 } 1622 }
1681 } 1623 }
...@@ -1685,23 +1627,22 @@ calc_eclosure (dfa) ...@@ -1685,23 +1627,22 @@ calc_eclosure (dfa)
1685 /* Calculate epsilon closure of NODE. */ 1627 /* Calculate epsilon closure of NODE. */
1686 1628
1687 static reg_errcode_t 1629 static reg_errcode_t
1688 calc_eclosure_iter (new_set, dfa, node, root) 1630 calc_eclosure_iter (re_node_set *new_set, re_dfa_t *dfa, Idx node, bool root)
1689 re_node_set *new_set;
1690 re_dfa_t *dfa;
1691 int node, root;
1692 { 1631 {
1693 reg_errcode_t err; 1632 reg_errcode_t err;
1694 unsigned int constraint; 1633 unsigned int constraint;
1695 int i, incomplete; 1634 Idx i;
1635 bool incomplete;
1636 bool ok;
1696 re_node_set eclosure; 1637 re_node_set eclosure;
1697 incomplete = 0; 1638 incomplete = false;
1698 err = re_node_set_alloc (&eclosure, dfa->edests[node].nelem + 1); 1639 err = re_node_set_alloc (&eclosure, dfa->edests[node].nelem + 1);
1699 if (BE (err != REG_NOERROR, 0)) 1640 if (BE (err != REG_NOERROR, 0))
1700 return err; 1641 return err;
1701 1642
1702 /* This indicates that we are calculating this node now. 1643 /* This indicates that we are calculating this node now.
1703 We reference this value to avoid infinite loop. */ 1644 We reference this value to avoid infinite loop. */
1704 dfa->eclosures[node].nelem = -1; 1645 dfa->eclosures[node].nelem = REG_MISSING;
1705 1646
1706 constraint = ((dfa->nodes[node].type == ANCHOR) 1647 constraint = ((dfa->nodes[node].type == ANCHOR)
1707 ? dfa->nodes[node].opr.ctx_type : 0); 1648 ? dfa->nodes[node].opr.ctx_type : 0);
...@@ -1711,7 +1652,7 @@ calc_eclosure_iter (new_set, dfa, node, root) ...@@ -1711,7 +1652,7 @@ calc_eclosure_iter (new_set, dfa, node, root)
1711 && dfa->edests[node].nelem 1652 && dfa->edests[node].nelem
1712 && !dfa->nodes[dfa->edests[node].elems[0]].duplicated) 1653 && !dfa->nodes[dfa->edests[node].elems[0]].duplicated)
1713 { 1654 {
1714 int org_node, cur_node; 1655 Idx org_node, cur_node;
1715 org_node = cur_node = node; 1656 org_node = cur_node = node;
1716 err = duplicate_node_closure (dfa, node, node, node, constraint); 1657 err = duplicate_node_closure (dfa, node, node, node, constraint);
1717 if (BE (err != REG_NOERROR, 0)) 1658 if (BE (err != REG_NOERROR, 0))
...@@ -1723,19 +1664,19 @@ calc_eclosure_iter (new_set, dfa, node, root) ...@@ -1723,19 +1664,19 @@ calc_eclosure_iter (new_set, dfa, node, root)
1723 for (i = 0; i < dfa->edests[node].nelem; ++i) 1664 for (i = 0; i < dfa->edests[node].nelem; ++i)
1724 { 1665 {
1725 re_node_set eclosure_elem; 1666 re_node_set eclosure_elem;
1726 int edest = dfa->edests[node].elems[i]; 1667 Idx edest = dfa->edests[node].elems[i];
1727 /* If calculating the epsilon closure of `edest' is in progress, 1668 /* If calculating the epsilon closure of `edest' is in progress,
1728 return intermediate result. */ 1669 return intermediate result. */
1729 if (dfa->eclosures[edest].nelem == -1) 1670 if (dfa->eclosures[edest].nelem == REG_MISSING)
1730 { 1671 {
1731 incomplete = 1; 1672 incomplete = true;
1732 continue; 1673 continue;
1733 } 1674 }
1734 /* If we haven't calculated the epsilon closure of `edest' yet, 1675 /* If we haven't calculated the epsilon closure of `edest' yet,
1735 calculate now. Otherwise use calculated epsilon closure. */ 1676 calculate now. Otherwise use calculated epsilon closure. */
1736 if (dfa->eclosures[edest].nelem == 0) 1677 if (dfa->eclosures[edest].nelem == 0)
1737 { 1678 {
1738 err = calc_eclosure_iter (&eclosure_elem, dfa, edest, 0); 1679 err = calc_eclosure_iter (&eclosure_elem, dfa, edest, false);
1739 if (BE (err != REG_NOERROR, 0)) 1680 if (BE (err != REG_NOERROR, 0))
1740 return err; 1681 return err;
1741 } 1682 }
...@@ -1747,13 +1688,15 @@ calc_eclosure_iter (new_set, dfa, node, root) ...@@ -1747,13 +1688,15 @@ calc_eclosure_iter (new_set, dfa, node, root)
1747 the epsilon closure of this node is also incomplete. */ 1688 the epsilon closure of this node is also incomplete. */
1748 if (dfa->eclosures[edest].nelem == 0) 1689 if (dfa->eclosures[edest].nelem == 0)
1749 { 1690 {
1750 incomplete = 1; 1691 incomplete = true;
1751 re_node_set_free (&eclosure_elem); 1692 re_node_set_free (&eclosure_elem);
1752 } 1693 }
1753 } 1694 }
1754 1695
1755 /* Epsilon closures include itself. */ 1696 /* Epsilon closures include itself. */
1756 re_node_set_insert (&eclosure, node); 1697 ok = re_node_set_insert (&eclosure, node);
1698 if (BE (! ok, 0))
1699 return REG_ESPACE;
1757 if (incomplete && !root) 1700 if (incomplete && !root)
1758 dfa->eclosures[node].nelem = 0; 1701 dfa->eclosures[node].nelem = 0;
1759 else 1702 else
...@@ -1768,10 +1711,7 @@ calc_eclosure_iter (new_set, dfa, node, root) ...@@ -1768,10 +1711,7 @@ calc_eclosure_iter (new_set, dfa, node, root)
1768 We must not use this function inside bracket expressions. */ 1711 We must not use this function inside bracket expressions. */
1769 1712
1770 static void 1713 static void
1771 fetch_token (result, input, syntax) 1714 fetch_token (re_token_t *result, re_string_t *input, reg_syntax_t syntax)
1772 re_token_t *result;
1773 re_string_t *input;
1774 reg_syntax_t syntax;
1775 { 1715 {
1776 re_string_skip_bytes (input, peek_token (result, input, syntax)); 1716 re_string_skip_bytes (input, peek_token (result, input, syntax));
1777 } 1717 }
...@@ -1780,10 +1720,7 @@ fetch_token (result, input, syntax) ...@@ -1780,10 +1720,7 @@ fetch_token (result, input, syntax)
1780 We must not use this function inside bracket expressions. */ 1720 We must not use this function inside bracket expressions. */
1781 1721
1782 static int 1722 static int
1783 peek_token (token, input, syntax) 1723 peek_token (re_token_t *token, re_string_t *input, reg_syntax_t syntax)
1784 re_token_t *token;
1785 re_string_t *input;
1786 reg_syntax_t syntax;
1787 { 1724 {
1788 unsigned char c; 1725 unsigned char c;
1789 1726
...@@ -1833,97 +1770,97 @@ peek_token (token, input, syntax) ...@@ -1833,97 +1770,97 @@ peek_token (token, input, syntax)
1833 switch (c2) 1770 switch (c2)
1834 { 1771 {
1835 case '|': 1772 case '|':
1836 if (!(syntax & RE_LIMITED_OPS) && !(syntax & RE_NO_BK_VBAR)) 1773 if (!(syntax & REG_LIMITED_OPS) && !(syntax & REG_NO_BK_VBAR))
1837 token->type = OP_ALT; 1774 token->type = OP_ALT;
1838 break; 1775 break;
1839 case '1': case '2': case '3': case '4': case '5': 1776 case '1': case '2': case '3': case '4': case '5':
1840 case '6': case '7': case '8': case '9': 1777 case '6': case '7': case '8': case '9':
1841 if (!(syntax & RE_NO_BK_REFS)) 1778 if (!(syntax & REG_NO_BK_REFS))
1842 { 1779 {
1843 token->type = OP_BACK_REF; 1780 token->type = OP_BACK_REF;
1844 token->opr.idx = c2 - '1'; 1781 token->opr.idx = c2 - '1';
1845 } 1782 }
1846 break; 1783 break;
1847 case '<': 1784 case '<':
1848 if (!(syntax & RE_NO_GNU_OPS)) 1785 if (!(syntax & REG_NO_GNU_OPS))
1849 { 1786 {
1850 token->type = ANCHOR; 1787 token->type = ANCHOR;
1851 token->opr.ctx_type = WORD_FIRST; 1788 token->opr.ctx_type = WORD_FIRST;
1852 } 1789 }
1853 break; 1790 break;
1854 case '>': 1791 case '>':
1855 if (!(syntax & RE_NO_GNU_OPS)) 1792 if (!(syntax & REG_NO_GNU_OPS))
1856 { 1793 {
1857 token->type = ANCHOR; 1794 token->type = ANCHOR;
1858 token->opr.ctx_type = WORD_LAST; 1795 token->opr.ctx_type = WORD_LAST;
1859 } 1796 }
1860 break; 1797 break;
1861 case 'b': 1798 case 'b':
1862 if (!(syntax & RE_NO_GNU_OPS)) 1799 if (!(syntax & REG_NO_GNU_OPS))
1863 { 1800 {
1864 token->type = ANCHOR; 1801 token->type = ANCHOR;
1865 token->opr.ctx_type = WORD_DELIM; 1802 token->opr.ctx_type = WORD_DELIM;
1866 } 1803 }
1867 break; 1804 break;
1868 case 'B': 1805 case 'B':
1869 if (!(syntax & RE_NO_GNU_OPS)) 1806 if (!(syntax & REG_NO_GNU_OPS))
1870 { 1807 {
1871 token->type = ANCHOR; 1808 token->type = ANCHOR;
1872 token->opr.ctx_type = NOT_WORD_DELIM; 1809 token->opr.ctx_type = NOT_WORD_DELIM;
1873 } 1810 }
1874 break; 1811 break;
1875 case 'w': 1812 case 'w':
1876 if (!(syntax & RE_NO_GNU_OPS)) 1813 if (!(syntax & REG_NO_GNU_OPS))
1877 token->type = OP_WORD; 1814 token->type = OP_WORD;
1878 break; 1815 break;
1879 case 'W': 1816 case 'W':
1880 if (!(syntax & RE_NO_GNU_OPS)) 1817 if (!(syntax & REG_NO_GNU_OPS))
1881 token->type = OP_NOTWORD; 1818 token->type = OP_NOTWORD;
1882 break; 1819 break;
1883 case 's': 1820 case 's':
1884 if (!(syntax & RE_NO_GNU_OPS)) 1821 if (!(syntax & REG_NO_GNU_OPS))
1885 token->type = OP_SPACE; 1822 token->type = OP_SPACE;
1886 break; 1823 break;
1887 case 'S': 1824 case 'S':
1888 if (!(syntax & RE_NO_GNU_OPS)) 1825 if (!(syntax & REG_NO_GNU_OPS))
1889 token->type = OP_NOTSPACE; 1826 token->type = OP_NOTSPACE;
1890 break; 1827 break;
1891 case '`': 1828 case '`':
1892 if (!(syntax & RE_NO_GNU_OPS)) 1829 if (!(syntax & REG_NO_GNU_OPS))
1893 { 1830 {
1894 token->type = ANCHOR; 1831 token->type = ANCHOR;
1895 token->opr.ctx_type = BUF_FIRST; 1832 token->opr.ctx_type = BUF_FIRST;
1896 } 1833 }
1897 break; 1834 break;
1898 case '\'': 1835 case '\'':
1899 if (!(syntax & RE_NO_GNU_OPS)) 1836 if (!(syntax & REG_NO_GNU_OPS))
1900 { 1837 {
1901 token->type = ANCHOR; 1838 token->type = ANCHOR;
1902 token->opr.ctx_type = BUF_LAST; 1839 token->opr.ctx_type = BUF_LAST;
1903 } 1840 }
1904 break; 1841 break;
1905 case '(': 1842 case '(':
1906 if (!(syntax & RE_NO_BK_PARENS)) 1843 if (!(syntax & REG_NO_BK_PARENS))
1907 token->type = OP_OPEN_SUBEXP; 1844 token->type = OP_OPEN_SUBEXP;
1908 break; 1845 break;
1909 case ')': 1846 case ')':
1910 if (!(syntax & RE_NO_BK_PARENS)) 1847 if (!(syntax & REG_NO_BK_PARENS))
1911 token->type = OP_CLOSE_SUBEXP; 1848 token->type = OP_CLOSE_SUBEXP;
1912 break; 1849 break;
1913 case '+': 1850 case '+':
1914 if (!(syntax & RE_LIMITED_OPS) && (syntax & RE_BK_PLUS_QM)) 1851 if (!(syntax & REG_LIMITED_OPS) && (syntax & REG_BK_PLUS_QM))
1915 token->type = OP_DUP_PLUS; 1852 token->type = OP_DUP_PLUS;
1916 break; 1853 break;
1917 case '?': 1854 case '?':
1918 if (!(syntax & RE_LIMITED_OPS) && (syntax & RE_BK_PLUS_QM)) 1855 if (!(syntax & REG_LIMITED_OPS) && (syntax & REG_BK_PLUS_QM))
1919 token->type = OP_DUP_QUESTION; 1856 token->type = OP_DUP_QUESTION;
1920 break; 1857 break;
1921 case '{': 1858 case '{':
1922 if ((syntax & RE_INTERVALS) && (!(syntax & RE_NO_BK_BRACES))) 1859 if ((syntax & REG_INTERVALS) && (!(syntax & REG_NO_BK_BRACES)))
1923 token->type = OP_OPEN_DUP_NUM; 1860 token->type = OP_OPEN_DUP_NUM;
1924 break; 1861 break;
1925 case '}': 1862 case '}':
1926 if ((syntax & RE_INTERVALS) && (!(syntax & RE_NO_BK_BRACES))) 1863 if ((syntax & REG_INTERVALS) && (!(syntax & REG_NO_BK_BRACES)))
1927 token->type = OP_CLOSE_DUP_NUM; 1864 token->type = OP_CLOSE_DUP_NUM;
1928 break; 1865 break;
1929 default: 1866 default:
...@@ -1946,38 +1883,38 @@ peek_token (token, input, syntax) ...@@ -1946,38 +1883,38 @@ peek_token (token, input, syntax)
1946 switch (c) 1883 switch (c)
1947 { 1884 {
1948 case '\n': 1885 case '\n':
1949 if (syntax & RE_NEWLINE_ALT) 1886 if (syntax & REG_NEWLINE_ALT)
1950 token->type = OP_ALT; 1887 token->type = OP_ALT;
1951 break; 1888 break;
1952 case '|': 1889 case '|':
1953 if (!(syntax & RE_LIMITED_OPS) && (syntax & RE_NO_BK_VBAR)) 1890 if (!(syntax & REG_LIMITED_OPS) && (syntax & REG_NO_BK_VBAR))
1954 token->type = OP_ALT; 1891 token->type = OP_ALT;
1955 break; 1892 break;
1956 case '*': 1893 case '*':
1957 token->type = OP_DUP_ASTERISK; 1894 token->type = OP_DUP_ASTERISK;
1958 break; 1895 break;
1959 case '+': 1896 case '+':
1960 if (!(syntax & RE_LIMITED_OPS) && !(syntax & RE_BK_PLUS_QM)) 1897 if (!(syntax & REG_LIMITED_OPS) && !(syntax & REG_BK_PLUS_QM))
1961 token->type = OP_DUP_PLUS; 1898 token->type = OP_DUP_PLUS;
1962 break; 1899 break;
1963 case '?': 1900 case '?':
1964 if (!(syntax & RE_LIMITED_OPS) && !(syntax & RE_BK_PLUS_QM)) 1901 if (!(syntax & REG_LIMITED_OPS) && !(syntax & REG_BK_PLUS_QM))
1965 token->type = OP_DUP_QUESTION; 1902 token->type = OP_DUP_QUESTION;
1966 break; 1903 break;
1967 case '{': 1904 case '{':
1968 if ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES)) 1905 if ((syntax & REG_INTERVALS) && (syntax & REG_NO_BK_BRACES))
1969 token->type = OP_OPEN_DUP_NUM; 1906 token->type = OP_OPEN_DUP_NUM;
1970 break; 1907 break;
1971 case '}': 1908 case '}':
1972 if ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES)) 1909 if ((syntax & REG_INTERVALS) && (syntax & REG_NO_BK_BRACES))
1973 token->type = OP_CLOSE_DUP_NUM; 1910 token->type = OP_CLOSE_DUP_NUM;
1974 break; 1911 break;
1975 case '(': 1912 case '(':
1976 if (syntax & RE_NO_BK_PARENS) 1913 if (syntax & REG_NO_BK_PARENS)
1977 token->type = OP_OPEN_SUBEXP; 1914 token->type = OP_OPEN_SUBEXP;
1978 break; 1915 break;
1979 case ')': 1916 case ')':
1980 if (syntax & RE_NO_BK_PARENS) 1917 if (syntax & REG_NO_BK_PARENS)
1981 token->type = OP_CLOSE_SUBEXP; 1918 token->type = OP_CLOSE_SUBEXP;
1982 break; 1919 break;
1983 case '[': 1920 case '[':
...@@ -1987,18 +1924,18 @@ peek_token (token, input, syntax) ...@@ -1987,18 +1924,18 @@ peek_token (token, input, syntax)
1987 token->type = OP_PERIOD; 1924 token->type = OP_PERIOD;
1988 break; 1925 break;
1989 case '^': 1926 case '^':
1990 if (!(syntax & (RE_CONTEXT_INDEP_ANCHORS | RE_CARET_ANCHORS_HERE)) && 1927 if (!(syntax & (REG_CONTEXT_INDEP_ANCHORS | REG_CARET_ANCHORS_HERE)) &&
1991 re_string_cur_idx (input) != 0) 1928 re_string_cur_idx (input) != 0)
1992 { 1929 {
1993 char prev = re_string_peek_byte (input, -1); 1930 char prev = re_string_peek_byte (input, -1);
1994 if (!(syntax & RE_NEWLINE_ALT) || prev != '\n') 1931 if (!(syntax & REG_NEWLINE_ALT) || prev != '\n')
1995 break; 1932 break;
1996 } 1933 }
1997 token->type = ANCHOR; 1934 token->type = ANCHOR;
1998 token->opr.ctx_type = LINE_FIRST; 1935 token->opr.ctx_type = LINE_FIRST;
1999 break; 1936 break;
2000 case '$': 1937 case '$':
2001 if (!(syntax & RE_CONTEXT_INDEP_ANCHORS) && 1938 if (!(syntax & REG_CONTEXT_INDEP_ANCHORS) &&
2002 re_string_cur_idx (input) + 1 != re_string_length (input)) 1939 re_string_cur_idx (input) + 1 != re_string_length (input))
2003 { 1940 {
2004 re_token_t next; 1941 re_token_t next;
...@@ -2021,10 +1958,7 @@ peek_token (token, input, syntax) ...@@ -2021,10 +1958,7 @@ peek_token (token, input, syntax)
2021 We must not use this function out of bracket expressions. */ 1958 We must not use this function out of bracket expressions. */
2022 1959
2023 static int 1960 static int
2024 peek_token_bracket (token, input, syntax) 1961 peek_token_bracket (re_token_t *token, re_string_t *input, reg_syntax_t syntax)
2025 re_token_t *token;
2026 re_string_t *input;
2027 reg_syntax_t syntax;
2028 { 1962 {
2029 unsigned char c; 1963 unsigned char c;
2030 if (re_string_eoi (input)) 1964 if (re_string_eoi (input))
...@@ -2044,7 +1978,7 @@ peek_token_bracket (token, input, syntax) ...@@ -2044,7 +1978,7 @@ peek_token_bracket (token, input, syntax)
2044 } 1978 }
2045 #endif /* RE_ENABLE_I18N */ 1979 #endif /* RE_ENABLE_I18N */
2046 1980
2047 if (c == '\\' && (syntax & RE_BACKSLASH_ESCAPE_IN_LISTS) 1981 if (c == '\\' && (syntax & REG_BACKSLASH_ESCAPE_IN_LISTS)
2048 && re_string_cur_idx (input) + 1 < re_string_length (input)) 1982 && re_string_cur_idx (input) + 1 < re_string_length (input))
2049 { 1983 {
2050 /* In this case, '\' escape a character. */ 1984 /* In this case, '\' escape a character. */
...@@ -2074,7 +2008,7 @@ peek_token_bracket (token, input, syntax) ...@@ -2074,7 +2008,7 @@ peek_token_bracket (token, input, syntax)
2074 token->type = OP_OPEN_EQUIV_CLASS; 2008 token->type = OP_OPEN_EQUIV_CLASS;
2075 break; 2009 break;
2076 case ':': 2010 case ':':
2077 if (syntax & RE_CHAR_CLASSES) 2011 if (syntax & REG_CHAR_CLASSES)
2078 { 2012 {
2079 token->type = OP_OPEN_CHAR_CLASS; 2013 token->type = OP_OPEN_CHAR_CLASS;
2080 break; 2014 break;
...@@ -2120,17 +2054,14 @@ peek_token_bracket (token, input, syntax) ...@@ -2120,17 +2054,14 @@ peek_token_bracket (token, input, syntax)
2120 EOR means end of regular expression. */ 2054 EOR means end of regular expression. */
2121 2055
2122 static bin_tree_t * 2056 static bin_tree_t *
2123 parse (regexp, preg, syntax, err) 2057 parse (re_string_t *regexp, regex_t *preg, reg_syntax_t syntax,
2124 re_string_t *regexp; 2058 reg_errcode_t *err)
2125 regex_t *preg;
2126 reg_syntax_t syntax;
2127 reg_errcode_t *err;
2128 { 2059 {
2129 re_dfa_t *dfa = (re_dfa_t *) preg->buffer; 2060 re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
2130 bin_tree_t *tree, *eor, *root; 2061 bin_tree_t *tree, *eor, *root;
2131 re_token_t current_token; 2062 re_token_t current_token;
2132 dfa->syntax = syntax; 2063 dfa->syntax = syntax;
2133 fetch_token (&current_token, regexp, syntax | RE_CARET_ANCHORS_HERE); 2064 fetch_token (&current_token, regexp, syntax | REG_CARET_ANCHORS_HERE);
2134 tree = parse_reg_exp (regexp, preg, &current_token, syntax, 0, err); 2065 tree = parse_reg_exp (regexp, preg, &current_token, syntax, 0, err);
2135 if (BE (*err != REG_NOERROR && tree == NULL, 0)) 2066 if (BE (*err != REG_NOERROR && tree == NULL, 0))
2136 return NULL; 2067 return NULL;
...@@ -2157,15 +2088,10 @@ parse (regexp, preg, syntax, err) ...@@ -2157,15 +2088,10 @@ parse (regexp, preg, syntax, err)
2157 ALT means alternative, which represents the operator `|'. */ 2088 ALT means alternative, which represents the operator `|'. */
2158 2089
2159 static bin_tree_t * 2090 static bin_tree_t *
2160 parse_reg_exp (regexp, preg, token, syntax, nest, err) 2091 parse_reg_exp (re_string_t *regexp, regex_t *preg, re_token_t *token,
2161 re_string_t *regexp; 2092 reg_syntax_t syntax, Idx nest, reg_errcode_t *err)
2162 regex_t *preg;
2163 re_token_t *token;
2164 reg_syntax_t syntax;
2165 int nest;
2166 reg_errcode_t *err;
2167 { 2093 {
2168 re_dfa_t *dfa = (re_dfa_t *) preg->buffer; 2094 re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
2169 bin_tree_t *tree, *branch = NULL; 2095 bin_tree_t *tree, *branch = NULL;
2170 tree = parse_branch (regexp, preg, token, syntax, nest, err); 2096 tree = parse_branch (regexp, preg, token, syntax, nest, err);
2171 if (BE (*err != REG_NOERROR && tree == NULL, 0)) 2097 if (BE (*err != REG_NOERROR && tree == NULL, 0))
...@@ -2173,7 +2099,7 @@ parse_reg_exp (regexp, preg, token, syntax, nest, err) ...@@ -2173,7 +2099,7 @@ parse_reg_exp (regexp, preg, token, syntax, nest, err)
2173 2099
2174 while (token->type == OP_ALT) 2100 while (token->type == OP_ALT)
2175 { 2101 {
2176 fetch_token (token, regexp, syntax | RE_CARET_ANCHORS_HERE); 2102 fetch_token (token, regexp, syntax | REG_CARET_ANCHORS_HERE);
2177 if (token->type != OP_ALT && token->type != END_OF_RE 2103 if (token->type != OP_ALT && token->type != END_OF_RE
2178 && (nest == 0 || token->type != OP_CLOSE_SUBEXP)) 2104 && (nest == 0 || token->type != OP_CLOSE_SUBEXP))
2179 { 2105 {
...@@ -2203,16 +2129,11 @@ parse_reg_exp (regexp, preg, token, syntax, nest, err) ...@@ -2203,16 +2129,11 @@ parse_reg_exp (regexp, preg, token, syntax, nest, err)
2203 CAT means concatenation. */ 2129 CAT means concatenation. */
2204 2130
2205 static bin_tree_t * 2131 static bin_tree_t *
2206 parse_branch (regexp, preg, token, syntax, nest, err) 2132 parse_branch (re_string_t *regexp, regex_t *preg, re_token_t *token,
2207 re_string_t *regexp; 2133 reg_syntax_t syntax, Idx nest, reg_errcode_t *err)
2208 regex_t *preg;
2209 re_token_t *token;
2210 reg_syntax_t syntax;
2211 int nest;
2212 reg_errcode_t *err;
2213 { 2134 {
2214 bin_tree_t *tree, *exp; 2135 bin_tree_t *tree, *exp;
2215 re_dfa_t *dfa = (re_dfa_t *) preg->buffer; 2136 re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
2216 tree = parse_expression (regexp, preg, token, syntax, nest, err); 2137 tree = parse_expression (regexp, preg, token, syntax, nest, err);
2217 if (BE (*err != REG_NOERROR && tree == NULL, 0)) 2138 if (BE (*err != REG_NOERROR && tree == NULL, 0))
2218 return NULL; 2139 return NULL;
...@@ -2248,15 +2169,10 @@ parse_branch (regexp, preg, token, syntax, nest, err) ...@@ -2248,15 +2169,10 @@ parse_branch (regexp, preg, token, syntax, nest, err)
2248 */ 2169 */
2249 2170
2250 static bin_tree_t * 2171 static bin_tree_t *
2251 parse_expression (regexp, preg, token, syntax, nest, err) 2172 parse_expression (re_string_t *regexp, regex_t *preg, re_token_t *token,
2252 re_string_t *regexp; 2173 reg_syntax_t syntax, Idx nest, reg_errcode_t *err)
2253 regex_t *preg;
2254 re_token_t *token;
2255 reg_syntax_t syntax;
2256 int nest;
2257 reg_errcode_t *err;
2258 { 2174 {
2259 re_dfa_t *dfa = (re_dfa_t *) preg->buffer; 2175 re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
2260 bin_tree_t *tree; 2176 bin_tree_t *tree;
2261 switch (token->type) 2177 switch (token->type)
2262 { 2178 {
...@@ -2313,7 +2229,7 @@ parse_expression (regexp, preg, token, syntax, nest, err) ...@@ -2313,7 +2229,7 @@ parse_expression (regexp, preg, token, syntax, nest, err)
2313 dfa->has_mb_node = 1; 2229 dfa->has_mb_node = 1;
2314 break; 2230 break;
2315 case OP_OPEN_DUP_NUM: 2231 case OP_OPEN_DUP_NUM:
2316 if (syntax & RE_CONTEXT_INVALID_DUP) 2232 if (syntax & REG_CONTEXT_INVALID_DUP)
2317 { 2233 {
2318 *err = REG_BADRPT; 2234 *err = REG_BADRPT;
2319 return NULL; 2235 return NULL;
...@@ -2322,12 +2238,12 @@ parse_expression (regexp, preg, token, syntax, nest, err) ...@@ -2322,12 +2238,12 @@ parse_expression (regexp, preg, token, syntax, nest, err)
2322 case OP_DUP_ASTERISK: 2238 case OP_DUP_ASTERISK:
2323 case OP_DUP_PLUS: 2239 case OP_DUP_PLUS:
2324 case OP_DUP_QUESTION: 2240 case OP_DUP_QUESTION:
2325 if (syntax & RE_CONTEXT_INVALID_OPS) 2241 if (syntax & REG_CONTEXT_INVALID_OPS)
2326 { 2242 {
2327 *err = REG_BADRPT; 2243 *err = REG_BADRPT;
2328 return NULL; 2244 return NULL;
2329 } 2245 }
2330 else if (syntax & RE_CONTEXT_INDEP_OPS) 2246 else if (syntax & REG_CONTEXT_INDEP_OPS)
2331 { 2247 {
2332 fetch_token (token, regexp, syntax); 2248 fetch_token (token, regexp, syntax);
2333 return parse_expression (regexp, preg, token, syntax, nest, err); 2249 return parse_expression (regexp, preg, token, syntax, nest, err);
...@@ -2335,7 +2251,7 @@ parse_expression (regexp, preg, token, syntax, nest, err) ...@@ -2335,7 +2251,7 @@ parse_expression (regexp, preg, token, syntax, nest, err)
2335 /* else fall through */ 2251 /* else fall through */
2336 case OP_CLOSE_SUBEXP: 2252 case OP_CLOSE_SUBEXP:
2337 if ((token->type == OP_CLOSE_SUBEXP) && 2253 if ((token->type == OP_CLOSE_SUBEXP) &&
2338 !(syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)) 2254 !(syntax & REG_UNMATCHED_RIGHT_PAREN_ORD))
2339 { 2255 {
2340 *err = REG_ERPAREN; 2256 *err = REG_ERPAREN;
2341 return NULL; 2257 return NULL;
...@@ -2449,7 +2365,7 @@ parse_expression (regexp, preg, token, syntax, nest, err) ...@@ -2449,7 +2365,7 @@ parse_expression (regexp, preg, token, syntax, nest, err)
2449 if (BE (*err != REG_NOERROR && tree == NULL, 0)) 2365 if (BE (*err != REG_NOERROR && tree == NULL, 0))
2450 return NULL; 2366 return NULL;
2451 /* In BRE consecutive duplications are not allowed. */ 2367 /* In BRE consecutive duplications are not allowed. */
2452 if ((syntax & RE_CONTEXT_INVALID_DUP) 2368 if ((syntax & REG_CONTEXT_INVALID_DUP)
2453 && (token->type == OP_DUP_ASTERISK 2369 && (token->type == OP_DUP_ASTERISK
2454 || token->type == OP_OPEN_DUP_NUM)) 2370 || token->type == OP_OPEN_DUP_NUM))
2455 { 2371 {
...@@ -2469,20 +2385,15 @@ parse_expression (regexp, preg, token, syntax, nest, err) ...@@ -2469,20 +2385,15 @@ parse_expression (regexp, preg, token, syntax, nest, err)
2469 */ 2385 */
2470 2386
2471 static bin_tree_t * 2387 static bin_tree_t *
2472 parse_sub_exp (regexp, preg, token, syntax, nest, err) 2388 parse_sub_exp (re_string_t *regexp, regex_t *preg, re_token_t *token,
2473 re_string_t *regexp; 2389 reg_syntax_t syntax, Idx nest, reg_errcode_t *err)
2474 regex_t *preg;
2475 re_token_t *token;
2476 reg_syntax_t syntax;
2477 int nest;
2478 reg_errcode_t *err;
2479 { 2390 {
2480 re_dfa_t *dfa = (re_dfa_t *) preg->buffer; 2391 re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
2481 bin_tree_t *tree; 2392 bin_tree_t *tree;
2482 size_t cur_nsub; 2393 size_t cur_nsub;
2483 cur_nsub = preg->re_nsub++; 2394 cur_nsub = preg->re_nsub++;
2484 2395
2485 fetch_token (token, regexp, syntax | RE_CARET_ANCHORS_HERE); 2396 fetch_token (token, regexp, syntax | REG_CARET_ANCHORS_HERE);
2486 2397
2487 /* The subexpression may be a null string. */ 2398 /* The subexpression may be a null string. */
2488 if (token->type == OP_CLOSE_SUBEXP) 2399 if (token->type == OP_CLOSE_SUBEXP)
...@@ -2495,7 +2406,9 @@ parse_sub_exp (regexp, preg, token, syntax, nest, err) ...@@ -2495,7 +2406,9 @@ parse_sub_exp (regexp, preg, token, syntax, nest, err)
2495 if (BE (*err != REG_NOERROR, 0)) 2406 if (BE (*err != REG_NOERROR, 0))
2496 return NULL; 2407 return NULL;
2497 } 2408 }
2498 dfa->completed_bkref_map |= 1 << cur_nsub; 2409
2410 if (cur_nsub <= '9' - '1')
2411 dfa->completed_bkref_map |= 1 << cur_nsub;
2499 2412
2500 tree = create_tree (dfa, tree, NULL, SUBEXP); 2413 tree = create_tree (dfa, tree, NULL, SUBEXP);
2501 if (BE (tree == NULL, 0)) 2414 if (BE (tree == NULL, 0))
...@@ -2510,23 +2423,18 @@ parse_sub_exp (regexp, preg, token, syntax, nest, err) ...@@ -2510,23 +2423,18 @@ parse_sub_exp (regexp, preg, token, syntax, nest, err)
2510 /* This function parse repetition operators like "*", "+", "{1,3}" etc. */ 2423 /* This function parse repetition operators like "*", "+", "{1,3}" etc. */
2511 2424
2512 static bin_tree_t * 2425 static bin_tree_t *
2513 parse_dup_op (elem, regexp, dfa, token, syntax, err) 2426 parse_dup_op (bin_tree_t *elem, re_string_t *regexp, re_dfa_t *dfa,
2514 bin_tree_t *elem; 2427 re_token_t *token, reg_syntax_t syntax, reg_errcode_t *err)
2515 re_string_t *regexp;
2516 re_dfa_t *dfa;
2517 re_token_t *token;
2518 reg_syntax_t syntax;
2519 reg_errcode_t *err;
2520 { 2428 {
2521 bin_tree_t *tree = NULL, *old_tree = NULL; 2429 bin_tree_t *tree = NULL, *old_tree = NULL;
2522 int i, start, end, start_idx = re_string_cur_idx (regexp); 2430 Idx i, start, end, start_idx = re_string_cur_idx (regexp);
2523 re_token_t start_token = *token; 2431 re_token_t start_token = *token;
2524 2432
2525 if (token->type == OP_OPEN_DUP_NUM) 2433 if (token->type == OP_OPEN_DUP_NUM)
2526 { 2434 {
2527 end = 0; 2435 end = 0;
2528 start = fetch_number (regexp, token, syntax); 2436 start = fetch_number (regexp, token, syntax);
2529 if (start == -1) 2437 if (start == REG_MISSING)
2530 { 2438 {
2531 if (token->type == CHARACTER && token->opr.c == ',') 2439 if (token->type == CHARACTER && token->opr.c == ',')
2532 start = 0; /* We treat "{,m}" as "{0,m}". */ 2440 start = 0; /* We treat "{,m}" as "{0,m}". */
...@@ -2536,17 +2444,17 @@ parse_dup_op (elem, regexp, dfa, token, syntax, err) ...@@ -2536,17 +2444,17 @@ parse_dup_op (elem, regexp, dfa, token, syntax, err)
2536 return NULL; 2444 return NULL;
2537 } 2445 }
2538 } 2446 }
2539 if (BE (start != -2, 1)) 2447 if (BE (start != REG_ERROR, 1))
2540 { 2448 {
2541 /* We treat "{n}" as "{n,n}". */ 2449 /* We treat "{n}" as "{n,n}". */
2542 end = ((token->type == OP_CLOSE_DUP_NUM) ? start 2450 end = ((token->type == OP_CLOSE_DUP_NUM) ? start
2543 : ((token->type == CHARACTER && token->opr.c == ',') 2451 : ((token->type == CHARACTER && token->opr.c == ',')
2544 ? fetch_number (regexp, token, syntax) : -2)); 2452 ? fetch_number (regexp, token, syntax) : REG_ERROR));
2545 } 2453 }
2546 if (BE (start == -2 || end == -2, 0)) 2454 if (BE (start == REG_ERROR || end == REG_ERROR, 0))
2547 { 2455 {
2548 /* Invalid sequence. */ 2456 /* Invalid sequence. */
2549 if (BE (!(syntax & RE_INVALID_INTERVAL_ORD), 0)) 2457 if (BE (!(syntax & REG_INVALID_INTERVAL_ORD), 0))
2550 { 2458 {
2551 if (token->type == END_OF_RE) 2459 if (token->type == END_OF_RE)
2552 *err = REG_EBRACE; 2460 *err = REG_EBRACE;
...@@ -2565,7 +2473,7 @@ parse_dup_op (elem, regexp, dfa, token, syntax, err) ...@@ -2565,7 +2473,7 @@ parse_dup_op (elem, regexp, dfa, token, syntax, err)
2565 return elem; 2473 return elem;
2566 } 2474 }
2567 2475
2568 if (BE (end != -1 && start > end, 0)) 2476 if (BE (end != REG_MISSING && start > end, 0))
2569 { 2477 {
2570 /* First number greater than second. */ 2478 /* First number greater than second. */
2571 *err = REG_BADBR; 2479 *err = REG_BADBR;
...@@ -2575,7 +2483,7 @@ parse_dup_op (elem, regexp, dfa, token, syntax, err) ...@@ -2575,7 +2483,7 @@ parse_dup_op (elem, regexp, dfa, token, syntax, err)
2575 else 2483 else
2576 { 2484 {
2577 start = (token->type == OP_DUP_PLUS) ? 1 : 0; 2485 start = (token->type == OP_DUP_PLUS) ? 1 : 0;
2578 end = (token->type == OP_DUP_QUESTION) ? 1 : -1; 2486 end = (token->type == OP_DUP_QUESTION) ? 1 : REG_MISSING;
2579 } 2487 }
2580 2488
2581 fetch_token (token, regexp, syntax); 2489 fetch_token (token, regexp, syntax);
...@@ -2613,24 +2521,26 @@ parse_dup_op (elem, regexp, dfa, token, syntax, err) ...@@ -2613,24 +2521,26 @@ parse_dup_op (elem, regexp, dfa, token, syntax, err)
2613 if (elem->token.type == SUBEXP) 2521 if (elem->token.type == SUBEXP)
2614 postorder (elem, mark_opt_subexp, (void *) (long) elem->token.opr.idx); 2522 postorder (elem, mark_opt_subexp, (void *) (long) elem->token.opr.idx);
2615 2523
2616 tree = create_tree (dfa, elem, NULL, (end == -1 ? OP_DUP_ASTERISK : OP_ALT)); 2524 tree = create_tree (dfa, elem, NULL,
2525 (end == REG_MISSING ? OP_DUP_ASTERISK : OP_ALT));
2617 if (BE (tree == NULL, 0)) 2526 if (BE (tree == NULL, 0))
2618 goto parse_dup_op_espace; 2527 goto parse_dup_op_espace;
2619 2528
2620 /* This loop is actually executed only when end != -1, 2529 /* This loop is actually executed only when end != REG_MISSING,
2621 to rewrite <re>{0,n} as (<re>(<re>...<re>?)?)?... We have 2530 to rewrite <re>{0,n} as (<re>(<re>...<re>?)?)?... We have
2622 already created the start+1-th copy. */ 2531 already created the start+1-th copy. */
2623 for (i = start + 2; i <= end; ++i) 2532 if ((Idx) -1 < 0 || end != REG_MISSING)
2624 { 2533 for (i = start + 2; i <= end; ++i)
2625 elem = duplicate_tree (elem, dfa); 2534 {
2626 tree = create_tree (dfa, tree, elem, CONCAT); 2535 elem = duplicate_tree (elem, dfa);
2627 if (BE (elem == NULL || tree == NULL, 0)) 2536 tree = create_tree (dfa, tree, elem, CONCAT);
2628 goto parse_dup_op_espace; 2537 if (BE (elem == NULL || tree == NULL, 0))
2629 2538 goto parse_dup_op_espace;
2630 tree = create_tree (dfa, tree, NULL, OP_ALT); 2539
2631 if (BE (tree == NULL, 0)) 2540 tree = create_tree (dfa, tree, NULL, OP_ALT);
2632 goto parse_dup_op_espace; 2541 if (BE (tree == NULL, 0))
2633 } 2542 goto parse_dup_op_espace;
2543 }
2634 2544
2635 if (old_tree) 2545 if (old_tree)
2636 tree = create_tree (dfa, old_tree, tree, CONCAT); 2546 tree = create_tree (dfa, old_tree, tree, CONCAT);
...@@ -2655,15 +2565,11 @@ parse_dup_op (elem, regexp, dfa, token, syntax, err) ...@@ -2655,15 +2565,11 @@ parse_dup_op (elem, regexp, dfa, token, syntax, err)
2655 update it. */ 2565 update it. */
2656 2566
2657 static reg_errcode_t 2567 static reg_errcode_t
2568 build_range_exp (bitset sbcset,
2658 # ifdef RE_ENABLE_I18N 2569 # ifdef RE_ENABLE_I18N
2659 build_range_exp (sbcset, mbcset, range_alloc, start_elem, end_elem) 2570 re_charset_t *mbcset, Idx *range_alloc,
2660 re_charset_t *mbcset; 2571 # endif
2661 int *range_alloc; 2572 bracket_elem_t *start_elem, bracket_elem_t *end_elem)
2662 # else /* not RE_ENABLE_I18N */
2663 build_range_exp (sbcset, start_elem, end_elem)
2664 # endif /* not RE_ENABLE_I18N */
2665 re_bitset_ptr_t sbcset;
2666 bracket_elem_t *start_elem, *end_elem;
2667 { 2573 {
2668 unsigned int start_ch, end_ch; 2574 unsigned int start_ch, end_ch;
2669 /* Equivalence Classes and Character Classes can't be a range start/end. */ 2575 /* Equivalence Classes and Character Classes can't be a range start/end. */
...@@ -2715,14 +2621,13 @@ build_range_exp (sbcset, start_elem, end_elem) ...@@ -2715,14 +2621,13 @@ build_range_exp (sbcset, start_elem, end_elem)
2715 { 2621 {
2716 /* There is not enough space, need realloc. */ 2622 /* There is not enough space, need realloc. */
2717 wchar_t *new_array_start, *new_array_end; 2623 wchar_t *new_array_start, *new_array_end;
2718 int new_nranges; 2624 Idx new_nranges;
2719 2625
2720 /* +1 in case of mbcset->nranges is 0. */ 2626 new_nranges = mbcset->nranges;
2721 new_nranges = 2 * mbcset->nranges + 1;
2722 /* Use realloc since mbcset->range_starts and mbcset->range_ends 2627 /* Use realloc since mbcset->range_starts and mbcset->range_ends
2723 are NULL if *range_alloc == 0. */ 2628 are NULL if *range_alloc == 0. */
2724 new_array_start = re_realloc (mbcset->range_starts, wchar_t, 2629 new_array_start = re_x2realloc (mbcset->range_starts, wchar_t,
2725 new_nranges); 2630 &new_nranges);
2726 new_array_end = re_realloc (mbcset->range_ends, wchar_t, 2631 new_array_end = re_realloc (mbcset->range_ends, wchar_t,
2727 new_nranges); 2632 new_nranges);
2728 2633
...@@ -2776,15 +2681,11 @@ build_range_exp (sbcset, start_elem, end_elem) ...@@ -2776,15 +2681,11 @@ build_range_exp (sbcset, start_elem, end_elem)
2776 pointer argument since we may update it. */ 2681 pointer argument since we may update it. */
2777 2682
2778 static reg_errcode_t 2683 static reg_errcode_t
2684 build_collating_symbol (bitset sbcset,
2779 # ifdef RE_ENABLE_I18N 2685 # ifdef RE_ENABLE_I18N
2780 build_collating_symbol (sbcset, mbcset, coll_sym_alloc, name) 2686 re_charset_t *mbcset, Idx *coll_sym_alloc,
2781 re_charset_t *mbcset; 2687 # endif
2782 int *coll_sym_alloc; 2688 const unsigned char *name)
2783 # else /* not RE_ENABLE_I18N */
2784 build_collating_symbol (sbcset, name)
2785 # endif /* not RE_ENABLE_I18N */
2786 re_bitset_ptr_t sbcset;
2787 const unsigned char *name;
2788 { 2689 {
2789 size_t name_len = strlen ((const char *) name); 2690 size_t name_len = strlen ((const char *) name);
2790 if (BE (name_len != 1, 0)) 2691 if (BE (name_len != 1, 0))
...@@ -2801,12 +2702,8 @@ build_collating_symbol (sbcset, name) ...@@ -2801,12 +2702,8 @@ build_collating_symbol (sbcset, name)
2801 "[[.a-a.]]" etc. */ 2702 "[[.a-a.]]" etc. */
2802 2703
2803 static bin_tree_t * 2704 static bin_tree_t *
2804 parse_bracket_exp (regexp, dfa, token, syntax, err) 2705 parse_bracket_exp (re_string_t *regexp, re_dfa_t *dfa, re_token_t *token,
2805 re_string_t *regexp; 2706 reg_syntax_t syntax, reg_errcode_t *err)
2806 re_dfa_t *dfa;
2807 re_token_t *token;
2808 reg_syntax_t syntax;
2809 reg_errcode_t *err;
2810 { 2707 {
2811 #ifdef _LIBC 2708 #ifdef _LIBC
2812 const unsigned char *collseqmb; 2709 const unsigned char *collseqmb;
...@@ -2822,9 +2719,7 @@ parse_bracket_exp (regexp, dfa, token, syntax, err) ...@@ -2822,9 +2719,7 @@ parse_bracket_exp (regexp, dfa, token, syntax, err)
2822 2719
2823 auto inline int32_t 2720 auto inline int32_t
2824 __attribute ((always_inline)) 2721 __attribute ((always_inline))
2825 seek_collating_symbol_entry (name, name_len) 2722 seek_collating_symbol_entry (const unsigned char *name, size_t name_len)
2826 const unsigned char *name;
2827 size_t name_len;
2828 { 2723 {
2829 int32_t hash = elem_hash ((const char *) name, name_len); 2724 int32_t hash = elem_hash ((const char *) name, name_len);
2830 int32_t elem = hash % table_size; 2725 int32_t elem = hash % table_size;
...@@ -2855,8 +2750,7 @@ parse_bracket_exp (regexp, dfa, token, syntax, err) ...@@ -2855,8 +2750,7 @@ parse_bracket_exp (regexp, dfa, token, syntax, err)
2855 2750
2856 auto inline unsigned int 2751 auto inline unsigned int
2857 __attribute ((always_inline)) 2752 __attribute ((always_inline))
2858 lookup_collation_sequence_value (br_elem) 2753 lookup_collation_sequence_value (bracket_elem_t *br_elem)
2859 bracket_elem_t *br_elem;
2860 { 2754 {
2861 if (br_elem->type == SB_CHAR) 2755 if (br_elem->type == SB_CHAR)
2862 { 2756 {
...@@ -2923,11 +2817,9 @@ parse_bracket_exp (regexp, dfa, token, syntax, err) ...@@ -2923,11 +2817,9 @@ parse_bracket_exp (regexp, dfa, token, syntax, err)
2923 2817
2924 auto inline reg_errcode_t 2818 auto inline reg_errcode_t
2925 __attribute ((always_inline)) 2819 __attribute ((always_inline))
2926 build_range_exp (sbcset, mbcset, range_alloc, start_elem, end_elem) 2820 build_range_exp (bitset sbcset, re_charset_t *mbcset,
2927 re_charset_t *mbcset; 2821 Idx *range_alloc,
2928 int *range_alloc; 2822 bracket_elem_t *start_elem, bracket_elem_t *end_elem)
2929 re_bitset_ptr_t sbcset;
2930 bracket_elem_t *start_elem, *end_elem;
2931 { 2823 {
2932 unsigned int ch; 2824 unsigned int ch;
2933 uint32_t start_collseq; 2825 uint32_t start_collseq;
...@@ -2945,7 +2837,7 @@ parse_bracket_exp (regexp, dfa, token, syntax, err) ...@@ -2945,7 +2837,7 @@ parse_bracket_exp (regexp, dfa, token, syntax, err)
2945 /* Check start/end collation sequence values. */ 2837 /* Check start/end collation sequence values. */
2946 if (BE (start_collseq == UINT_MAX || end_collseq == UINT_MAX, 0)) 2838 if (BE (start_collseq == UINT_MAX || end_collseq == UINT_MAX, 0))
2947 return REG_ECOLLATE; 2839 return REG_ECOLLATE;
2948 if (BE ((syntax & RE_NO_EMPTY_RANGES) && start_collseq > end_collseq, 0)) 2840 if (BE ((syntax & REG_NO_EMPTY_RANGES) && start_collseq > end_collseq, 0))
2949 return REG_ERANGE; 2841 return REG_ERANGE;
2950 2842
2951 /* Got valid collation sequence values, add them as a new entry. 2843 /* Got valid collation sequence values, add them as a new entry.
...@@ -2960,12 +2852,11 @@ parse_bracket_exp (regexp, dfa, token, syntax, err) ...@@ -2960,12 +2852,11 @@ parse_bracket_exp (regexp, dfa, token, syntax, err)
2960 /* There is not enough space, need realloc. */ 2852 /* There is not enough space, need realloc. */
2961 uint32_t *new_array_start; 2853 uint32_t *new_array_start;
2962 uint32_t *new_array_end; 2854 uint32_t *new_array_end;
2963 int new_nranges; 2855 Idx new_nranges;
2964 2856
2965 /* +1 in case of mbcset->nranges is 0. */ 2857 new_nranges = mbcset->nranges;
2966 new_nranges = 2 * mbcset->nranges + 1; 2858 new_array_start = re_x2realloc (mbcset->range_starts, uint32_t,
2967 new_array_start = re_realloc (mbcset->range_starts, uint32_t, 2859 &new_nranges);
2968 new_nranges);
2969 new_array_end = re_realloc (mbcset->range_ends, uint32_t, 2860 new_array_end = re_realloc (mbcset->range_ends, uint32_t,
2970 new_nranges); 2861 new_nranges);
2971 2862
...@@ -3006,11 +2897,8 @@ parse_bracket_exp (regexp, dfa, token, syntax, err) ...@@ -3006,11 +2897,8 @@ parse_bracket_exp (regexp, dfa, token, syntax, err)
3006 2897
3007 auto inline reg_errcode_t 2898 auto inline reg_errcode_t
3008 __attribute ((always_inline)) 2899 __attribute ((always_inline))
3009 build_collating_symbol (sbcset, mbcset, coll_sym_alloc, name) 2900 build_collating_symbol (bitset sbcset, re_charset_t *mbcset,
3010 re_charset_t *mbcset; 2901 Idx *coll_sym_alloc, const unsigned char *name)
3011 int *coll_sym_alloc;
3012 re_bitset_ptr_t sbcset;
3013 const unsigned char *name;
3014 { 2902 {
3015 int32_t elem, idx; 2903 int32_t elem, idx;
3016 size_t name_len = strlen ((const char *) name); 2904 size_t name_len = strlen ((const char *) name);
...@@ -3039,12 +2927,11 @@ parse_bracket_exp (regexp, dfa, token, syntax, err) ...@@ -3039,12 +2927,11 @@ parse_bracket_exp (regexp, dfa, token, syntax, err)
3039 if (BE (*coll_sym_alloc == mbcset->ncoll_syms, 0)) 2927 if (BE (*coll_sym_alloc == mbcset->ncoll_syms, 0))
3040 { 2928 {
3041 /* Not enough, realloc it. */ 2929 /* Not enough, realloc it. */
3042 /* +1 in case of mbcset->ncoll_syms is 0. */ 2930 Idx new_coll_sym_alloc = mbcset->ncoll_syms;
3043 int new_coll_sym_alloc = 2 * mbcset->ncoll_syms + 1;
3044 /* Use realloc since mbcset->coll_syms is NULL 2931 /* Use realloc since mbcset->coll_syms is NULL
3045 if *alloc == 0. */ 2932 if *alloc == 0. */
3046 int32_t *new_coll_syms = re_realloc (mbcset->coll_syms, int32_t, 2933 int32_t *new_coll_syms = re_x2realloc (mbcset->coll_syms, int32_t,
3047 new_coll_sym_alloc); 2934 &new_coll_sym_alloc);
3048 if (BE (new_coll_syms == NULL, 0)) 2935 if (BE (new_coll_syms == NULL, 0))
3049 return REG_ESPACE; 2936 return REG_ESPACE;
3050 mbcset->coll_syms = new_coll_syms; 2937 mbcset->coll_syms = new_coll_syms;
...@@ -3070,13 +2957,13 @@ parse_bracket_exp (regexp, dfa, token, syntax, err) ...@@ -3070,13 +2957,13 @@ parse_bracket_exp (regexp, dfa, token, syntax, err)
3070 re_bitset_ptr_t sbcset; 2957 re_bitset_ptr_t sbcset;
3071 #ifdef RE_ENABLE_I18N 2958 #ifdef RE_ENABLE_I18N
3072 re_charset_t *mbcset; 2959 re_charset_t *mbcset;
3073 int coll_sym_alloc = 0, range_alloc = 0, mbchar_alloc = 0; 2960 Idx coll_sym_alloc = 0, range_alloc = 0, mbchar_alloc = 0;
3074 int equiv_class_alloc = 0, char_class_alloc = 0; 2961 Idx equiv_class_alloc = 0, char_class_alloc = 0;
3075 #endif /* not RE_ENABLE_I18N */ 2962 #endif /* not RE_ENABLE_I18N */
3076 int non_match = 0; 2963 bool non_match = false;
3077 bin_tree_t *work_tree; 2964 bin_tree_t *work_tree;
3078 int token_len; 2965 int token_len;
3079 int first_round = 1; 2966 bool first_round = true;
3080 #ifdef _LIBC 2967 #ifdef _LIBC
3081 collseqmb = (const unsigned char *) 2968 collseqmb = (const unsigned char *)
3082 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_COLLSEQMB); 2969 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_COLLSEQMB);
...@@ -3094,9 +2981,9 @@ parse_bracket_exp (regexp, dfa, token, syntax, err) ...@@ -3094,9 +2981,9 @@ parse_bracket_exp (regexp, dfa, token, syntax, err)
3094 _NL_COLLATE_SYMB_EXTRAMB); 2981 _NL_COLLATE_SYMB_EXTRAMB);
3095 } 2982 }
3096 #endif 2983 #endif
3097 sbcset = (re_bitset_ptr_t) calloc (sizeof (unsigned int), BITSET_UINTS); 2984 sbcset = re_calloc (bitset_word, BITSET_WORDS);
3098 #ifdef RE_ENABLE_I18N 2985 #ifdef RE_ENABLE_I18N
3099 mbcset = (re_charset_t *) calloc (sizeof (re_charset_t), 1); 2986 mbcset = re_calloc (re_charset_t, 1);
3100 #endif /* RE_ENABLE_I18N */ 2987 #endif /* RE_ENABLE_I18N */
3101 #ifdef RE_ENABLE_I18N 2988 #ifdef RE_ENABLE_I18N
3102 if (BE (sbcset == NULL || mbcset == NULL, 0)) 2989 if (BE (sbcset == NULL || mbcset == NULL, 0))
...@@ -3119,8 +3006,8 @@ parse_bracket_exp (regexp, dfa, token, syntax, err) ...@@ -3119,8 +3006,8 @@ parse_bracket_exp (regexp, dfa, token, syntax, err)
3119 #ifdef RE_ENABLE_I18N 3006 #ifdef RE_ENABLE_I18N
3120 mbcset->non_match = 1; 3007 mbcset->non_match = 1;
3121 #endif /* not RE_ENABLE_I18N */ 3008 #endif /* not RE_ENABLE_I18N */
3122 non_match = 1; 3009 non_match = true;
3123 if (syntax & RE_HAT_LISTS_NOT_NEWLINE) 3010 if (syntax & REG_HAT_LISTS_NOT_NEWLINE)
3124 bitset_set (sbcset, '\0'); 3011 bitset_set (sbcset, '\0');
3125 re_string_skip_bytes (regexp, token_len); /* Skip a token. */ 3012 re_string_skip_bytes (regexp, token_len); /* Skip a token. */
3126 token_len = peek_token_bracket (token, regexp, syntax); 3013 token_len = peek_token_bracket (token, regexp, syntax);
...@@ -3141,7 +3028,8 @@ parse_bracket_exp (regexp, dfa, token, syntax, err) ...@@ -3141,7 +3028,8 @@ parse_bracket_exp (regexp, dfa, token, syntax, err)
3141 unsigned char start_name_buf[BRACKET_NAME_BUF_SIZE]; 3028 unsigned char start_name_buf[BRACKET_NAME_BUF_SIZE];
3142 unsigned char end_name_buf[BRACKET_NAME_BUF_SIZE]; 3029 unsigned char end_name_buf[BRACKET_NAME_BUF_SIZE];
3143 reg_errcode_t ret; 3030 reg_errcode_t ret;
3144 int token_len2 = 0, is_range_exp = 0; 3031 int token_len2 = 0;
3032 bool is_range_exp = false;
3145 re_token_t token2; 3033 re_token_t token2;
3146 3034
3147 start_elem.opr.name = start_name_buf; 3035 start_elem.opr.name = start_name_buf;
...@@ -3152,7 +3040,7 @@ parse_bracket_exp (regexp, dfa, token, syntax, err) ...@@ -3152,7 +3040,7 @@ parse_bracket_exp (regexp, dfa, token, syntax, err)
3152 *err = ret; 3040 *err = ret;
3153 goto parse_bracket_exp_free_return; 3041 goto parse_bracket_exp_free_return;
3154 } 3042 }
3155 first_round = 0; 3043 first_round = false;
3156 3044
3157 /* Get information about the next token. We need it in any case. */ 3045 /* Get information about the next token. We need it in any case. */
3158 token_len = peek_token_bracket (token, regexp, syntax); 3046 token_len = peek_token_bracket (token, regexp, syntax);
...@@ -3181,15 +3069,15 @@ parse_bracket_exp (regexp, dfa, token, syntax, err) ...@@ -3181,15 +3069,15 @@ parse_bracket_exp (regexp, dfa, token, syntax, err)
3181 token->type = CHARACTER; 3069 token->type = CHARACTER;
3182 } 3070 }
3183 else 3071 else
3184 is_range_exp = 1; 3072 is_range_exp = true;
3185 } 3073 }
3186 } 3074 }
3187 3075
3188 if (is_range_exp == 1) 3076 if (is_range_exp == true)
3189 { 3077 {
3190 end_elem.opr.name = end_name_buf; 3078 end_elem.opr.name = end_name_buf;
3191 ret = parse_bracket_element (&end_elem, regexp, &token2, token_len2, 3079 ret = parse_bracket_element (&end_elem, regexp, &token2, token_len2,
3192 dfa, syntax, 1); 3080 dfa, syntax, true);
3193 if (BE (ret != REG_NOERROR, 0)) 3081 if (BE (ret != REG_NOERROR, 0))
3194 { 3082 {
3195 *err = ret; 3083 *err = ret;
...@@ -3227,11 +3115,10 @@ parse_bracket_exp (regexp, dfa, token, syntax, err) ...@@ -3227,11 +3115,10 @@ parse_bracket_exp (regexp, dfa, token, syntax, err)
3227 { 3115 {
3228 wchar_t *new_mbchars; 3116 wchar_t *new_mbchars;
3229 /* Not enough, realloc it. */ 3117 /* Not enough, realloc it. */
3230 /* +1 in case of mbcset->nmbchars is 0. */ 3118 mbchar_alloc = mbcset->nmbchars;
3231 mbchar_alloc = 2 * mbcset->nmbchars + 1;
3232 /* Use realloc since array is NULL if *alloc == 0. */ 3119 /* Use realloc since array is NULL if *alloc == 0. */
3233 new_mbchars = re_realloc (mbcset->mbchars, wchar_t, 3120 new_mbchars = re_x2realloc (mbcset->mbchars, wchar_t,
3234 mbchar_alloc); 3121 &mbchar_alloc);
3235 if (BE (new_mbchars == NULL, 0)) 3122 if (BE (new_mbchars == NULL, 0))
3236 goto parse_bracket_exp_espace; 3123 goto parse_bracket_exp_espace;
3237 mbcset->mbchars = new_mbchars; 3124 mbcset->mbchars = new_mbchars;
...@@ -3304,12 +3191,12 @@ parse_bracket_exp (regexp, dfa, token, syntax, err) ...@@ -3304,12 +3191,12 @@ parse_bracket_exp (regexp, dfa, token, syntax, err)
3304 mbc_tree = create_token_tree (dfa, NULL, NULL, &br_token); 3191 mbc_tree = create_token_tree (dfa, NULL, NULL, &br_token);
3305 if (BE (mbc_tree == NULL, 0)) 3192 if (BE (mbc_tree == NULL, 0))
3306 goto parse_bracket_exp_espace; 3193 goto parse_bracket_exp_espace;
3307 for (sbc_idx = 0; sbc_idx < BITSET_UINTS; ++sbc_idx) 3194 for (sbc_idx = 0; sbc_idx < BITSET_WORDS; ++sbc_idx)
3308 if (sbcset[sbc_idx]) 3195 if (sbcset[sbc_idx])
3309 break; 3196 break;
3310 /* If there are no bits set in sbcset, there is no point 3197 /* If there are no bits set in sbcset, there is no point
3311 of having both SIMPLE_BRACKET and COMPLEX_BRACKET. */ 3198 of having both SIMPLE_BRACKET and COMPLEX_BRACKET. */
3312 if (sbc_idx < BITSET_UINTS) 3199 if (sbc_idx < BITSET_WORDS)
3313 { 3200 {
3314 /* Build a tree for simple bracket. */ 3201 /* Build a tree for simple bracket. */
3315 br_token.type = SIMPLE_BRACKET; 3202 br_token.type = SIMPLE_BRACKET;
...@@ -3357,15 +3244,9 @@ parse_bracket_exp (regexp, dfa, token, syntax, err) ...@@ -3357,15 +3244,9 @@ parse_bracket_exp (regexp, dfa, token, syntax, err)
3357 /* Parse an element in the bracket expression. */ 3244 /* Parse an element in the bracket expression. */
3358 3245
3359 static reg_errcode_t 3246 static reg_errcode_t
3360 parse_bracket_element (elem, regexp, token, token_len, dfa, syntax, 3247 parse_bracket_element (bracket_elem_t *elem, re_string_t *regexp,
3361 accept_hyphen) 3248 re_token_t *token, int token_len, re_dfa_t *dfa,
3362 bracket_elem_t *elem; 3249 reg_syntax_t syntax, bool accept_hyphen)
3363 re_string_t *regexp;
3364 re_token_t *token;
3365 int token_len;
3366 re_dfa_t *dfa;
3367 reg_syntax_t syntax;
3368 int accept_hyphen;
3369 { 3250 {
3370 #ifdef RE_ENABLE_I18N 3251 #ifdef RE_ENABLE_I18N
3371 int cur_char_size; 3252 int cur_char_size;
...@@ -3403,10 +3284,8 @@ parse_bracket_element (elem, regexp, token, token_len, dfa, syntax, ...@@ -3403,10 +3284,8 @@ parse_bracket_element (elem, regexp, token, token_len, dfa, syntax,
3403 [=<equivalent_class>=]. */ 3284 [=<equivalent_class>=]. */
3404 3285
3405 static reg_errcode_t 3286 static reg_errcode_t
3406 parse_bracket_symbol (elem, regexp, token) 3287 parse_bracket_symbol (bracket_elem_t *elem, re_string_t *regexp,
3407 bracket_elem_t *elem; 3288 re_token_t *token)
3408 re_string_t *regexp;
3409 re_token_t *token;
3410 { 3289 {
3411 unsigned char ch, delim = token->opr.c; 3290 unsigned char ch, delim = token->opr.c;
3412 int i = 0; 3291 int i = 0;
...@@ -3452,15 +3331,11 @@ parse_bracket_symbol (elem, regexp, token) ...@@ -3452,15 +3331,11 @@ parse_bracket_symbol (elem, regexp, token)
3452 is a pointer argument sinse we may update it. */ 3331 is a pointer argument sinse we may update it. */
3453 3332
3454 static reg_errcode_t 3333 static reg_errcode_t
3334 build_equiv_class (bitset sbcset,
3455 #ifdef RE_ENABLE_I18N 3335 #ifdef RE_ENABLE_I18N
3456 build_equiv_class (sbcset, mbcset, equiv_class_alloc, name) 3336 re_charset_t *mbcset, Idx *equiv_class_alloc,
3457 re_charset_t *mbcset; 3337 #endif
3458 int *equiv_class_alloc; 3338 const unsigned char *name)
3459 #else /* not RE_ENABLE_I18N */
3460 build_equiv_class (sbcset, name)
3461 #endif /* not RE_ENABLE_I18N */
3462 re_bitset_ptr_t sbcset;
3463 const unsigned char *name;
3464 { 3339 {
3465 #if defined _LIBC 3340 #if defined _LIBC
3466 uint32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES); 3341 uint32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
...@@ -3517,12 +3392,11 @@ build_equiv_class (sbcset, name) ...@@ -3517,12 +3392,11 @@ build_equiv_class (sbcset, name)
3517 if (BE (*equiv_class_alloc == mbcset->nequiv_classes, 0)) 3392 if (BE (*equiv_class_alloc == mbcset->nequiv_classes, 0))
3518 { 3393 {
3519 /* Not enough, realloc it. */ 3394 /* Not enough, realloc it. */
3520 /* +1 in case of mbcset->nequiv_classes is 0. */ 3395 Idx new_equiv_class_alloc = mbcset->nequiv_classes;
3521 int new_equiv_class_alloc = 2 * mbcset->nequiv_classes + 1;
3522 /* Use realloc since the array is NULL if *alloc == 0. */ 3396 /* Use realloc since the array is NULL if *alloc == 0. */
3523 int32_t *new_equiv_classes = re_realloc (mbcset->equiv_classes, 3397 int32_t *new_equiv_classes = re_x2realloc (mbcset->equiv_classes,
3524 int32_t, 3398 int32_t,
3525 new_equiv_class_alloc); 3399 &new_equiv_class_alloc);
3526 if (BE (new_equiv_classes == NULL, 0)) 3400 if (BE (new_equiv_classes == NULL, 0))
3527 return REG_ESPACE; 3401 return REG_ESPACE;
3528 mbcset->equiv_classes = new_equiv_classes; 3402 mbcset->equiv_classes = new_equiv_classes;
...@@ -3547,24 +3421,18 @@ build_equiv_class (sbcset, name) ...@@ -3547,24 +3421,18 @@ build_equiv_class (sbcset, name)
3547 is a pointer argument sinse we may update it. */ 3421 is a pointer argument sinse we may update it. */
3548 3422
3549 static reg_errcode_t 3423 static reg_errcode_t
3424 build_charclass (unsigned REG_TRANSLATE_TYPE trans, bitset sbcset,
3550 #ifdef RE_ENABLE_I18N 3425 #ifdef RE_ENABLE_I18N
3551 build_charclass (trans, sbcset, mbcset, char_class_alloc, class_name, syntax) 3426 re_charset_t *mbcset, Idx *char_class_alloc,
3552 re_charset_t *mbcset; 3427 #endif
3553 int *char_class_alloc; 3428 const unsigned char *class_name, reg_syntax_t syntax)
3554 #else /* not RE_ENABLE_I18N */
3555 build_charclass (trans, sbcset, class_name, syntax)
3556 #endif /* not RE_ENABLE_I18N */
3557 unsigned RE_TRANSLATE_TYPE trans;
3558 re_bitset_ptr_t sbcset;
3559 const unsigned char *class_name;
3560 reg_syntax_t syntax;
3561 { 3429 {
3562 int i; 3430 int i;
3563 const char *name = (const char *) class_name; 3431 const char *name = (const char *) class_name;
3564 3432
3565 /* In case of REG_ICASE "upper" and "lower" match the both of 3433 /* In case of REG_ICASE "upper" and "lower" match the both of
3566 upper and lower cases. */ 3434 upper and lower cases. */
3567 if ((syntax & RE_ICASE) 3435 if ((syntax & REG_IGNORE_CASE)
3568 && (strcmp (name, "upper") == 0 || strcmp (name, "lower") == 0)) 3436 && (strcmp (name, "upper") == 0 || strcmp (name, "lower") == 0))
3569 name = "alpha"; 3437 name = "alpha";
3570 3438
...@@ -3573,11 +3441,10 @@ build_charclass (trans, sbcset, class_name, syntax) ...@@ -3573,11 +3441,10 @@ build_charclass (trans, sbcset, class_name, syntax)
3573 if (BE (*char_class_alloc == mbcset->nchar_classes, 0)) 3441 if (BE (*char_class_alloc == mbcset->nchar_classes, 0))
3574 { 3442 {
3575 /* Not enough, realloc it. */ 3443 /* Not enough, realloc it. */
3576 /* +1 in case of mbcset->nchar_classes is 0. */ 3444 Idx new_char_class_alloc = mbcset->nchar_classes;
3577 int new_char_class_alloc = 2 * mbcset->nchar_classes + 1;
3578 /* Use realloc since array is NULL if *alloc == 0. */ 3445 /* Use realloc since array is NULL if *alloc == 0. */
3579 wctype_t *new_char_classes = re_realloc (mbcset->char_classes, wctype_t, 3446 wctype_t *new_char_classes = re_x2realloc (mbcset->char_classes, wctype_t,
3580 new_char_class_alloc); 3447 &new_char_class_alloc);
3581 if (BE (new_char_classes == NULL, 0)) 3448 if (BE (new_char_classes == NULL, 0))
3582 return REG_ESPACE; 3449 return REG_ESPACE;
3583 mbcset->char_classes = new_char_classes; 3450 mbcset->char_classes = new_char_classes;
...@@ -3627,26 +3494,23 @@ build_charclass (trans, sbcset, class_name, syntax) ...@@ -3627,26 +3494,23 @@ build_charclass (trans, sbcset, class_name, syntax)
3627 } 3494 }
3628 3495
3629 static bin_tree_t * 3496 static bin_tree_t *
3630 build_charclass_op (dfa, trans, class_name, extra, non_match, err) 3497 build_charclass_op (re_dfa_t *dfa, unsigned REG_TRANSLATE_TYPE trans,
3631 re_dfa_t *dfa; 3498 const unsigned char *class_name,
3632 unsigned RE_TRANSLATE_TYPE trans; 3499 const unsigned char *extra,
3633 const unsigned char *class_name; 3500 bool non_match, reg_errcode_t *err)
3634 const unsigned char *extra;
3635 int non_match;
3636 reg_errcode_t *err;
3637 { 3501 {
3638 re_bitset_ptr_t sbcset; 3502 re_bitset_ptr_t sbcset;
3639 #ifdef RE_ENABLE_I18N 3503 #ifdef RE_ENABLE_I18N
3640 re_charset_t *mbcset; 3504 re_charset_t *mbcset;
3641 int alloc = 0; 3505 Idx alloc = 0;
3642 #endif /* not RE_ENABLE_I18N */ 3506 #endif /* not RE_ENABLE_I18N */
3643 reg_errcode_t ret; 3507 reg_errcode_t ret;
3644 re_token_t br_token; 3508 re_token_t br_token;
3645 bin_tree_t *tree; 3509 bin_tree_t *tree;
3646 3510
3647 sbcset = (re_bitset_ptr_t) calloc (sizeof (unsigned int), BITSET_UINTS); 3511 sbcset = re_calloc (bitset_word, BITSET_WORDS);
3648 #ifdef RE_ENABLE_I18N 3512 #ifdef RE_ENABLE_I18N
3649 mbcset = (re_charset_t *) calloc (sizeof (re_charset_t), 1); 3513 mbcset = re_calloc (re_charset_t, 1);
3650 #endif /* RE_ENABLE_I18N */ 3514 #endif /* RE_ENABLE_I18N */
3651 3515
3652 #ifdef RE_ENABLE_I18N 3516 #ifdef RE_ENABLE_I18N
...@@ -3663,7 +3527,7 @@ build_charclass_op (dfa, trans, class_name, extra, non_match, err) ...@@ -3663,7 +3527,7 @@ build_charclass_op (dfa, trans, class_name, extra, non_match, err)
3663 { 3527 {
3664 #ifdef RE_ENABLE_I18N 3528 #ifdef RE_ENABLE_I18N
3665 /* 3529 /*
3666 if (syntax & RE_HAT_LISTS_NOT_NEWLINE) 3530 if (syntax & REG_HAT_LISTS_NOT_NEWLINE)
3667 bitset_set(cset->sbcset, '\0'); 3531 bitset_set(cset->sbcset, '\0');
3668 */ 3532 */
3669 mbcset->non_match = 1; 3533 mbcset->non_match = 1;
...@@ -3743,28 +3607,27 @@ build_charclass_op (dfa, trans, class_name, extra, non_match, err) ...@@ -3743,28 +3607,27 @@ build_charclass_op (dfa, trans, class_name, extra, non_match, err)
3743 3607
3744 /* This is intended for the expressions like "a{1,3}". 3608 /* This is intended for the expressions like "a{1,3}".
3745 Fetch a number from `input', and return the number. 3609 Fetch a number from `input', and return the number.
3746 Return -1, if the number field is empty like "{,1}". 3610 Return REG_MISSING if the number field is empty like "{,1}".
3747 Return -2, If an error is occured. */ 3611 Return REG_ERROR if an error occurred. */
3748 3612
3749 static int 3613 static Idx
3750 fetch_number (input, token, syntax) 3614 fetch_number (re_string_t *input, re_token_t *token, reg_syntax_t syntax)
3751 re_string_t *input;
3752 re_token_t *token;
3753 reg_syntax_t syntax;
3754 { 3615 {
3755 int num = -1; 3616 Idx num = REG_MISSING;
3756 unsigned char c; 3617 unsigned char c;
3757 while (1) 3618 while (1)
3758 { 3619 {
3759 fetch_token (token, input, syntax); 3620 fetch_token (token, input, syntax);
3760 c = token->opr.c; 3621 c = token->opr.c;
3761 if (BE (token->type == END_OF_RE, 0)) 3622 if (BE (token->type == END_OF_RE, 0))
3762 return -2; 3623 return REG_ERROR;
3763 if (token->type == OP_CLOSE_DUP_NUM || c == ',') 3624 if (token->type == OP_CLOSE_DUP_NUM || c == ',')
3764 break; 3625 break;
3765 num = ((token->type != CHARACTER || c < '0' || '9' < c || num == -2) 3626 num = ((token->type != CHARACTER || c < '0' || '9' < c
3766 ? -2 : ((num == -1) ? c - '0' : num * 10 + c - '0')); 3627 || num == REG_ERROR)
3767 num = (num > RE_DUP_MAX) ? -2 : num; 3628 ? REG_ERROR
3629 : ((num == REG_MISSING) ? c - '0' : num * 10 + c - '0'));
3630 num = (num > REG_DUP_MAX) ? REG_ERROR : num;
3768 } 3631 }
3769 return num; 3632 return num;
3770 } 3633 }
...@@ -3790,11 +3653,8 @@ free_charset (re_charset_t *cset) ...@@ -3790,11 +3653,8 @@ free_charset (re_charset_t *cset)
3790 /* Create a tree node. */ 3653 /* Create a tree node. */
3791 3654
3792 static bin_tree_t * 3655 static bin_tree_t *
3793 create_tree (dfa, left, right, type) 3656 create_tree (re_dfa_t *dfa, bin_tree_t *left, bin_tree_t *right,
3794 re_dfa_t *dfa; 3657 re_token_type_t type)
3795 bin_tree_t *left;
3796 bin_tree_t *right;
3797 re_token_type_t type;
3798 { 3658 {
3799 re_token_t t; 3659 re_token_t t;
3800 t.type = type; 3660 t.type = type;
...@@ -3802,11 +3662,8 @@ create_tree (dfa, left, right, type) ...@@ -3802,11 +3662,8 @@ create_tree (dfa, left, right, type)
3802 } 3662 }
3803 3663
3804 static bin_tree_t * 3664 static bin_tree_t *
3805 create_token_tree (dfa, left, right, token) 3665 create_token_tree (re_dfa_t *dfa, bin_tree_t *left, bin_tree_t *right,
3806 re_dfa_t *dfa; 3666 const re_token_t *token)
3807 bin_tree_t *left;
3808 bin_tree_t *right;
3809 const re_token_t *token;
3810 { 3667 {
3811 bin_tree_t *tree; 3668 bin_tree_t *tree;
3812 if (BE (dfa->str_tree_storage_idx == BIN_TREE_STORAGE_SIZE, 0)) 3669 if (BE (dfa->str_tree_storage_idx == BIN_TREE_STORAGE_SIZE, 0))
...@@ -3829,7 +3686,7 @@ create_token_tree (dfa, left, right, token) ...@@ -3829,7 +3686,7 @@ create_token_tree (dfa, left, right, token)
3829 tree->token.opt_subexp = 0; 3686 tree->token.opt_subexp = 0;
3830 tree->first = NULL; 3687 tree->first = NULL;
3831 tree->next = NULL; 3688 tree->next = NULL;
3832 tree->node_idx = -1; 3689 tree->node_idx = REG_MISSING;
3833 3690
3834 if (left != NULL) 3691 if (left != NULL)
3835 left->parent = tree; 3692 left->parent = tree;
...@@ -3842,11 +3699,9 @@ create_token_tree (dfa, left, right, token) ...@@ -3842,11 +3699,9 @@ create_token_tree (dfa, left, right, token)
3842 To be called from preorder or postorder. */ 3699 To be called from preorder or postorder. */
3843 3700
3844 static reg_errcode_t 3701 static reg_errcode_t
3845 mark_opt_subexp (extra, node) 3702 mark_opt_subexp (void *extra, bin_tree_t *node)
3846 void *extra;
3847 bin_tree_t *node;
3848 { 3703 {
3849 int idx = (int) (long) extra; 3704 Idx idx = (Idx) (long) extra;
3850 if (node->token.type == SUBEXP && node->token.opr.idx == idx) 3705 if (node->token.type == SUBEXP && node->token.opr.idx == idx)
3851 node->token.opt_subexp = 1; 3706 node->token.opt_subexp = 1;
3852 3707
...@@ -3884,9 +3739,7 @@ free_tree (void *extra, bin_tree_t *node) ...@@ -3884,9 +3739,7 @@ free_tree (void *extra, bin_tree_t *node)
3884 it's easier to duplicate. */ 3739 it's easier to duplicate. */
3885 3740
3886 static bin_tree_t * 3741 static bin_tree_t *
3887 duplicate_tree (root, dfa) 3742 duplicate_tree (const bin_tree_t *root, re_dfa_t *dfa)
3888 const bin_tree_t *root;
3889 re_dfa_t *dfa;
3890 { 3743 {
3891 const bin_tree_t *node; 3744 const bin_tree_t *node;
3892 bin_tree_t *dup_root; 3745 bin_tree_t *dup_root;
......
...@@ -18,31 +18,7 @@ ...@@ -18,31 +18,7 @@
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 19
20 #ifdef HAVE_CONFIG_H 20 #ifdef HAVE_CONFIG_H
21 #include "config.h" 21 # include <config.h>
22 #endif
23
24 #ifdef _AIX
25 #pragma alloca
26 #else
27 # ifndef allocax /* predefined by HP cc +Olibcalls */
28 # ifdef __GNUC__
29 # define alloca(size) __builtin_alloca (size)
30 # else
31 # if HAVE_ALLOCA_H
32 # include <alloca.h>
33 # else
34 # ifdef __hpux
35 void *alloca ();
36 # else
37 # if !defined __OS2__ && !defined WIN32
38 char *alloca ();
39 # else
40 # include <malloc.h> /* OS/2 defines alloca in here */
41 # endif
42 # endif
43 # endif
44 # endif
45 # endif
46 #endif 22 #endif
47 23
48 #ifdef _LIBC 24 #ifdef _LIBC
...@@ -70,10 +46,6 @@ ...@@ -70,10 +46,6 @@
70 # include "../locale/localeinfo.h" 46 # include "../locale/localeinfo.h"
71 #endif 47 #endif
72 48
73 /* POSIX says that <sys/types.h> must be included (by the caller) before
74 <regex.h>. */
75 #include <sys/types.h>
76
77 /* On some systems, limits.h sets RE_DUP_MAX to a lower value than 49 /* On some systems, limits.h sets RE_DUP_MAX to a lower value than
78 GNU regex allows. Include it before <regex.h>, which correctly 50 GNU regex allows. Include it before <regex.h>, which correctly
79 #undefs RE_DUP_MAX and sets it to the right value. */ 51 #undefs RE_DUP_MAX and sets it to the right value. */
......
1 /* Definitions for data structures and routines for the regular 1 /* Definitions for data structures and routines for the regular
2 expression library. 2 expression library.
3 Copyright (C) 1985,1989-93,1995-98,2000,2001,2002,2003 3 Copyright (C) 1985,1989-93,1995-98,2000,2001,2002,2003,2005
4 Free Software Foundation, Inc. 4 Free Software Foundation, Inc.
5 This file is part of the GNU C Library. 5 This file is part of the GNU C Library.
6 6
...@@ -28,15 +28,61 @@ ...@@ -28,15 +28,61 @@
28 extern "C" { 28 extern "C" {
29 #endif 29 #endif
30 30
31 /* POSIX says that <sys/types.h> must be included (by the caller) before 31 /* Define _REGEX_SOURCE to get definitions that are incompatible with
32 <regex.h>. */ 32 POSIX. */
33 #if (!defined _REGEX_SOURCE \
34 && (defined _GNU_SOURCE \
35 || (!defined _POSIX_C_SOURCE && !defined _POSIX_SOURCE \
36 && !defined _XOPEN_SOURCE)))
37 # define _REGEX_SOURCE 1
38 #endif
33 39
34 #if !defined _POSIX_C_SOURCE && !defined _POSIX_SOURCE && defined VMS 40 #if defined _REGEX_SOURCE && defined VMS
35 /* VMS doesn't have `size_t' in <sys/types.h>, even though POSIX says it 41 /* VMS doesn't have `size_t' in <sys/types.h>, even though POSIX says it
36 should be there. */ 42 should be there. */
37 # include <stddef.h> 43 # include <stddef.h>
38 #endif 44 #endif
39 45
46 #ifdef _REGEX_LARGE_OFFSETS
47
48 /* Use types and values that are wide enough to represent signed and
49 unsigned byte offsets in memory. This currently works only when
50 the regex code is used outside of the GNU C library; it is not yet
51 supported within glibc itself, and glibc users should not define
52 _REGEX_LARGE_OFFSETS. */
53
54 /* The type of the offset of a byte within a string.
55 For historical reasons POSIX 1003.1-2004 requires that regoff_t be
56 at least as wide as off_t. This is a bit odd (and many common
57 POSIX platforms set it to the more-sensible ssize_t) but we might
58 as well conform. We don't know of any hosts where ssize_t is wider
59 than off_t, so off_t is safe. */
60 typedef off_t regoff_t;
61
62 /* The type of nonnegative object indexes. Traditionally, GNU regex
63 uses 'int' for these. Code that uses __re_idx_t should work
64 regardless of whether the type is signed. */
65 typedef size_t __re_idx_t;
66
67 /* The type of object sizes. */
68 typedef size_t __re_size_t;
69
70 /* The type of object sizes, in places where the traditional code
71 uses unsigned long int. */
72 typedef size_t __re_long_size_t;
73
74 #else
75
76 /* Use types that are binary-compatible with the traditional GNU regex
77 implementation, which mishandles strings longer than INT_MAX. */
78
79 typedef int regoff_t;
80 typedef int __re_idx_t;
81 typedef unsigned int __re_size_t;
82 typedef unsigned long int __re_long_size_t;
83
84 #endif
85
40 /* The following two types have to be signed and unsigned integer type 86 /* The following two types have to be signed and unsigned integer type
41 wide enough to hold a value of a pointer. For most ANSI compilers 87 wide enough to hold a value of a pointer. For most ANSI compilers
42 ptrdiff_t and size_t should be likely OK. Still size of these two 88 ptrdiff_t and size_t should be likely OK. Still size of these two
...@@ -53,18 +99,18 @@ typedef unsigned long int reg_syntax_t; ...@@ -53,18 +99,18 @@ typedef unsigned long int reg_syntax_t;
53 99
54 /* If this bit is not set, then \ inside a bracket expression is literal. 100 /* If this bit is not set, then \ inside a bracket expression is literal.
55 If set, then such a \ quotes the following character. */ 101 If set, then such a \ quotes the following character. */
56 #define RE_BACKSLASH_ESCAPE_IN_LISTS ((unsigned long int) 1) 102 #define REG_BACKSLASH_ESCAPE_IN_LISTS 1ul
57 103
58 /* If this bit is not set, then + and ? are operators, and \+ and \? are 104 /* If this bit is not set, then + and ? are operators, and \+ and \? are
59 literals. 105 literals.
60 If set, then \+ and \? are operators and + and ? are literals. */ 106 If set, then \+ and \? are operators and + and ? are literals. */
61 #define RE_BK_PLUS_QM (RE_BACKSLASH_ESCAPE_IN_LISTS << 1) 107 #define REG_BK_PLUS_QM (1ul << 1)
62 108
63 /* If this bit is set, then character classes are supported. They are: 109 /* If this bit is set, then character classes are supported. They are:
64 [:alpha:], [:upper:], [:lower:], [:digit:], [:alnum:], [:xdigit:], 110 [:alpha:], [:upper:], [:lower:], [:digit:], [:alnum:], [:xdigit:],
65 [:space:], [:print:], [:punct:], [:graph:], and [:cntrl:]. 111 [:space:], [:print:], [:punct:], [:graph:], and [:cntrl:].
66 If not set, then character classes are not supported. */ 112 If not set, then character classes are not supported. */
67 #define RE_CHAR_CLASSES (RE_BK_PLUS_QM << 1) 113 #define REG_CHAR_CLASSES (1ul << 2)
68 114
69 /* If this bit is set, then ^ and $ are always anchors (outside bracket 115 /* If this bit is set, then ^ and $ are always anchors (outside bracket
70 expressions, of course). 116 expressions, of course).
...@@ -74,11 +120,11 @@ typedef unsigned long int reg_syntax_t; ...@@ -74,11 +120,11 @@ typedef unsigned long int reg_syntax_t;
74 $ is an anchor if it is at the end of a regular expression, or 120 $ is an anchor if it is at the end of a regular expression, or
75 before a close-group or an alternation operator. 121 before a close-group or an alternation operator.
76 122
77 This bit could be (re)combined with RE_CONTEXT_INDEP_OPS, because 123 This bit could be (re)combined with REG_CONTEXT_INDEP_OPS, because
78 POSIX draft 11.2 says that * etc. in leading positions is undefined. 124 POSIX draft 11.2 says that * etc. in leading positions is undefined.
79 We already implemented a previous draft which made those constructs 125 We already implemented a previous draft which made those constructs
80 invalid, though, so we haven't changed the code back. */ 126 invalid, though, so we haven't changed the code back. */
81 #define RE_CONTEXT_INDEP_ANCHORS (RE_CHAR_CLASSES << 1) 127 #define REG_CONTEXT_INDEP_ANCHORS (1ul << 3)
82 128
83 /* If this bit is set, then special characters are always special 129 /* If this bit is set, then special characters are always special
84 regardless of where they are in the pattern. 130 regardless of where they are in the pattern.
...@@ -86,71 +132,70 @@ typedef unsigned long int reg_syntax_t; ...@@ -86,71 +132,70 @@ typedef unsigned long int reg_syntax_t;
86 some contexts; otherwise they are ordinary. Specifically, 132 some contexts; otherwise they are ordinary. Specifically,
87 * + ? and intervals are only special when not after the beginning, 133 * + ? and intervals are only special when not after the beginning,
88 open-group, or alternation operator. */ 134 open-group, or alternation operator. */
89 #define RE_CONTEXT_INDEP_OPS (RE_CONTEXT_INDEP_ANCHORS << 1) 135 #define REG_CONTEXT_INDEP_OPS (1ul << 4)
90 136
91 /* If this bit is set, then *, +, ?, and { cannot be first in an re or 137 /* If this bit is set, then *, +, ?, and { cannot be first in an re or
92 immediately after an alternation or begin-group operator. */ 138 immediately after an alternation or begin-group operator. */
93 #define RE_CONTEXT_INVALID_OPS (RE_CONTEXT_INDEP_OPS << 1) 139 #define REG_CONTEXT_INVALID_OPS (1ul << 5)
94 140
95 /* If this bit is set, then . matches newline. 141 /* If this bit is set, then . matches newline.
96 If not set, then it doesn't. */ 142 If not set, then it doesn't. */
97 #define RE_DOT_NEWLINE (RE_CONTEXT_INVALID_OPS << 1) 143 #define REG_DOT_NEWLINE (1ul << 6)
98 144
99 /* If this bit is set, then . doesn't match NUL. 145 /* If this bit is set, then . doesn't match NUL.
100 If not set, then it does. */ 146 If not set, then it does. */
101 #define RE_DOT_NOT_NULL (RE_DOT_NEWLINE << 1) 147 #define REG_DOT_NOT_NULL (1ul << 7)
102 148
103 /* If this bit is set, nonmatching lists [^...] do not match newline. 149 /* If this bit is set, nonmatching lists [^...] do not match newline.
104 If not set, they do. */ 150 If not set, they do. */
105 #define RE_HAT_LISTS_NOT_NEWLINE (RE_DOT_NOT_NULL << 1) 151 #define REG_HAT_LISTS_NOT_NEWLINE (1ul << 8)
106 152
107 /* If this bit is set, either \{...\} or {...} defines an 153 /* If this bit is set, either \{...\} or {...} defines an
108 interval, depending on RE_NO_BK_BRACES. 154 interval, depending on REG_NO_BK_BRACES.
109 If not set, \{, \}, {, and } are literals. */ 155 If not set, \{, \}, {, and } are literals. */
110 #define RE_INTERVALS (RE_HAT_LISTS_NOT_NEWLINE << 1) 156 #define REG_INTERVALS (1ul << 9)
111 157
112 /* If this bit is set, +, ? and | aren't recognized as operators. 158 /* If this bit is set, +, ? and | aren't recognized as operators.
113 If not set, they are. */ 159 If not set, they are. */
114 #define RE_LIMITED_OPS (RE_INTERVALS << 1) 160 #define REG_LIMITED_OPS (1ul << 10)
115 161
116 /* If this bit is set, newline is an alternation operator. 162 /* If this bit is set, newline is an alternation operator.
117 If not set, newline is literal. */ 163 If not set, newline is literal. */
118 #define RE_NEWLINE_ALT (RE_LIMITED_OPS << 1) 164 #define REG_NEWLINE_ALT (1ul << 11)
119 165
120 /* If this bit is set, then `{...}' defines an interval, and \{ and \} 166 /* If this bit is set, then `{...}' defines an interval, and \{ and \}
121 are literals. 167 are literals.
122 If not set, then `\{...\}' defines an interval. */ 168 If not set, then `\{...\}' defines an interval. */
123 #define RE_NO_BK_BRACES (RE_NEWLINE_ALT << 1) 169 #define REG_NO_BK_BRACES (1ul << 12)
124 170
125 /* If this bit is set, (...) defines a group, and \( and \) are literals. 171 /* If this bit is set, (...) defines a group, and \( and \) are literals.
126 If not set, \(...\) defines a group, and ( and ) are literals. */ 172 If not set, \(...\) defines a group, and ( and ) are literals. */
127 #define RE_NO_BK_PARENS (RE_NO_BK_BRACES << 1) 173 #define REG_NO_BK_PARENS (1ul << 13)
128 174
129 /* If this bit is set, then \<digit> matches <digit>. 175 /* If this bit is set, then \<digit> matches <digit>.
130 If not set, then \<digit> is a back-reference. */ 176 If not set, then \<digit> is a back-reference. */
131 #define RE_NO_BK_REFS (RE_NO_BK_PARENS << 1) 177 #define REG_NO_BK_REFS (1ul << 14)
132 178
133 /* If this bit is set, then | is an alternation operator, and \| is literal. 179 /* If this bit is set, then | is an alternation operator, and \| is literal.
134 If not set, then \| is an alternation operator, and | is literal. */ 180 If not set, then \| is an alternation operator, and | is literal. */
135 #define RE_NO_BK_VBAR (RE_NO_BK_REFS << 1) 181 #define REG_NO_BK_VBAR (1ul << 15)
136 182
137 /* If this bit is set, then an ending range point collating higher 183 /* If this bit is set, then an ending range point collating higher
138 than the starting range point, as in [z-a], is invalid. 184 than the starting range point, as in [z-a], is invalid.
139 If not set, then when ending range point collates higher than the 185 If not set, the containing range is empty and does not match any string. */
140 starting range point, the range is ignored. */ 186 #define REG_NO_EMPTY_RANGES (1ul << 16)
141 #define RE_NO_EMPTY_RANGES (RE_NO_BK_VBAR << 1)
142 187
143 /* If this bit is set, then an unmatched ) is ordinary. 188 /* If this bit is set, then an unmatched ) is ordinary.
144 If not set, then an unmatched ) is invalid. */ 189 If not set, then an unmatched ) is invalid. */
145 #define RE_UNMATCHED_RIGHT_PAREN_ORD (RE_NO_EMPTY_RANGES << 1) 190 #define REG_UNMATCHED_RIGHT_PAREN_ORD (1ul << 17)
146 191
147 /* If this bit is set, succeed as soon as we match the whole pattern, 192 /* If this bit is set, succeed as soon as we match the whole pattern,
148 without further backtracking. */ 193 without further backtracking. */
149 #define RE_NO_POSIX_BACKTRACKING (RE_UNMATCHED_RIGHT_PAREN_ORD << 1) 194 #define REG_NO_POSIX_BACKTRACKING (1ul << 18)
150 195
151 /* If this bit is set, do not process the GNU regex operators. 196 /* If this bit is set, do not process the GNU regex operators.
152 If not set, then the GNU regex operators are recognized. */ 197 If not set, then the GNU regex operators are recognized. */
153 #define RE_NO_GNU_OPS (RE_NO_POSIX_BACKTRACKING << 1) 198 #define REG_NO_GNU_OPS (1ul << 19)
154 199
155 /* If this bit is set, turn on internal regex debugging. 200 /* If this bit is set, turn on internal regex debugging.
156 If not set, and debugging was on, turn it off. 201 If not set, and debugging was on, turn it off.
...@@ -158,29 +203,29 @@ typedef unsigned long int reg_syntax_t; ...@@ -158,29 +203,29 @@ typedef unsigned long int reg_syntax_t;
158 We define this bit always, so that all that's needed to turn on 203 We define this bit always, so that all that's needed to turn on
159 debugging is to recompile regex.c; the calling code can always have 204 debugging is to recompile regex.c; the calling code can always have
160 this bit set, and it won't affect anything in the normal case. */ 205 this bit set, and it won't affect anything in the normal case. */
161 #define RE_DEBUG (RE_NO_GNU_OPS << 1) 206 #define REG_DEBUG (1ul << 20)
162 207
163 /* If this bit is set, a syntactically invalid interval is treated as 208 /* If this bit is set, a syntactically invalid interval is treated as
164 a string of ordinary characters. For example, the ERE 'a{1' is 209 a string of ordinary characters. For example, the ERE 'a{1' is
165 treated as 'a\{1'. */ 210 treated as 'a\{1'. */
166 #define RE_INVALID_INTERVAL_ORD (RE_DEBUG << 1) 211 #define REG_INVALID_INTERVAL_ORD (1ul << 21)
167 212
168 /* If this bit is set, then ignore case when matching. 213 /* If this bit is set, then ignore case when matching.
169 If not set, then case is significant. */ 214 If not set, then case is significant. */
170 #define RE_ICASE (RE_INVALID_INTERVAL_ORD << 1) 215 #define REG_IGNORE_CASE (1ul << 22)
171 216
172 /* This bit is used internally like RE_CONTEXT_INDEP_ANCHORS but only 217 /* This bit is used internally like REG_CONTEXT_INDEP_ANCHORS but only
173 for ^, because it is difficult to scan the regex backwards to find 218 for ^, because it is difficult to scan the regex backwards to find
174 whether ^ should be special. */ 219 whether ^ should be special. */
175 #define RE_CARET_ANCHORS_HERE (RE_ICASE << 1) 220 #define REG_CARET_ANCHORS_HERE (1ul << 23)
176 221
177 /* If this bit is set, then \{ cannot be first in an bre or 222 /* If this bit is set, then \{ cannot be first in an bre or
178 immediately after an alternation or begin-group operator. */ 223 immediately after an alternation or begin-group operator. */
179 #define RE_CONTEXT_INVALID_DUP (RE_CARET_ANCHORS_HERE << 1) 224 #define REG_CONTEXT_INVALID_DUP (1ul << 24)
180 225
181 /* If this bit is set, then no_sub will be set to 1 during 226 /* If this bit is set, then no_sub will be set to 1 during
182 re_compile_pattern. */ 227 re_compile_pattern. */
183 #define RE_NO_SUB (RE_CONTEXT_INVALID_DUP << 1) 228 #define REG_NO_SUB (1ul << 25)
184 229
185 /* This global variable defines the particular regexp syntax to use (for 230 /* This global variable defines the particular regexp syntax to use (for
186 some interfaces). When a regexp is compiled, the syntax used is 231 some interfaces). When a regexp is compiled, the syntax used is
...@@ -192,81 +237,78 @@ extern reg_syntax_t re_syntax_options; ...@@ -192,81 +237,78 @@ extern reg_syntax_t re_syntax_options;
192 (The [[[ comments delimit what gets put into the Texinfo file, so 237 (The [[[ comments delimit what gets put into the Texinfo file, so
193 don't delete them!) */ 238 don't delete them!) */
194 /* [[[begin syntaxes]]] */ 239 /* [[[begin syntaxes]]] */
195 #define RE_SYNTAX_EMACS 0 240 #define REG_SYNTAX_EMACS 0
196 241
197 #define RE_SYNTAX_AWK \ 242 #define REG_SYNTAX_AWK \
198 (RE_BACKSLASH_ESCAPE_IN_LISTS | RE_DOT_NOT_NULL \ 243 (REG_BACKSLASH_ESCAPE_IN_LISTS | REG_DOT_NOT_NULL \
199 | RE_NO_BK_PARENS | RE_NO_BK_REFS \ 244 | REG_NO_BK_PARENS | REG_NO_BK_REFS \
200 | RE_NO_BK_VBAR | RE_NO_EMPTY_RANGES \ 245 | REG_NO_BK_VBAR | REG_NO_EMPTY_RANGES \
201 | RE_DOT_NEWLINE | RE_CONTEXT_INDEP_ANCHORS \ 246 | REG_DOT_NEWLINE | REG_CONTEXT_INDEP_ANCHORS \
202 | RE_UNMATCHED_RIGHT_PAREN_ORD | RE_NO_GNU_OPS) 247 | REG_UNMATCHED_RIGHT_PAREN_ORD | REG_NO_GNU_OPS)
203 248
204 #define RE_SYNTAX_GNU_AWK \ 249 #define REG_SYNTAX_GNU_AWK \
205 ((RE_SYNTAX_POSIX_EXTENDED | RE_BACKSLASH_ESCAPE_IN_LISTS | RE_DEBUG) \ 250 ((REG_SYNTAX_POSIX_EXTENDED | REG_BACKSLASH_ESCAPE_IN_LISTS \
206 & ~(RE_DOT_NOT_NULL | RE_INTERVALS | RE_CONTEXT_INDEP_OPS \ 251 | REG_DEBUG) \
207 | RE_CONTEXT_INVALID_OPS )) 252 & ~(REG_DOT_NOT_NULL | REG_INTERVALS | REG_CONTEXT_INDEP_OPS \
208 253 | REG_CONTEXT_INVALID_OPS ))
209 #define RE_SYNTAX_POSIX_AWK \ 254
210 (RE_SYNTAX_POSIX_EXTENDED | RE_BACKSLASH_ESCAPE_IN_LISTS \ 255 #define REG_SYNTAX_POSIX_AWK \
211 | RE_INTERVALS | RE_NO_GNU_OPS) 256 (REG_SYNTAX_POSIX_EXTENDED | REG_BACKSLASH_ESCAPE_IN_LISTS \
212 257 | REG_INTERVALS | REG_NO_GNU_OPS)
213 #define RE_SYNTAX_GREP \ 258
214 (RE_BK_PLUS_QM | RE_CHAR_CLASSES \ 259 #define REG_SYNTAX_GREP \
215 | RE_HAT_LISTS_NOT_NEWLINE | RE_INTERVALS \ 260 (REG_BK_PLUS_QM | REG_CHAR_CLASSES \
216 | RE_NEWLINE_ALT) 261 | REG_HAT_LISTS_NOT_NEWLINE | REG_INTERVALS \
217 262 | REG_NEWLINE_ALT)
218 #define RE_SYNTAX_EGREP \ 263
219 (RE_CHAR_CLASSES | RE_CONTEXT_INDEP_ANCHORS \ 264 #define REG_SYNTAX_EGREP \
220 | RE_CONTEXT_INDEP_OPS | RE_HAT_LISTS_NOT_NEWLINE \ 265 (REG_CHAR_CLASSES | REG_CONTEXT_INDEP_ANCHORS \
221 | RE_NEWLINE_ALT | RE_NO_BK_PARENS \ 266 | REG_CONTEXT_INDEP_OPS | REG_HAT_LISTS_NOT_NEWLINE \
222 | RE_NO_BK_VBAR) 267 | REG_NEWLINE_ALT | REG_NO_BK_PARENS \
223 268 | REG_NO_BK_VBAR)
224 #define RE_SYNTAX_POSIX_EGREP \ 269
225 (RE_SYNTAX_EGREP | RE_INTERVALS | RE_NO_BK_BRACES \ 270 #define REG_SYNTAX_POSIX_EGREP \
226 | RE_INVALID_INTERVAL_ORD) 271 (REG_SYNTAX_EGREP | REG_INTERVALS | REG_NO_BK_BRACES \
272 | REG_INVALID_INTERVAL_ORD)
227 273
228 /* P1003.2/D11.2, section 4.20.7.1, lines 5078ff. */ 274 /* P1003.2/D11.2, section 4.20.7.1, lines 5078ff. */
229 #define RE_SYNTAX_ED RE_SYNTAX_POSIX_BASIC 275 #define REG_SYNTAX_ED REG_SYNTAX_POSIX_BASIC
230 276
231 #define RE_SYNTAX_SED RE_SYNTAX_POSIX_BASIC 277 #define REG_SYNTAX_SED REG_SYNTAX_POSIX_BASIC
232 278
233 /* Syntax bits common to both basic and extended POSIX regex syntax. */ 279 /* Syntax bits common to both basic and extended POSIX regex syntax. */
234 #define _RE_SYNTAX_POSIX_COMMON \ 280 #define _REG_SYNTAX_POSIX_COMMON \
235 (RE_CHAR_CLASSES | RE_DOT_NEWLINE | RE_DOT_NOT_NULL \ 281 (REG_CHAR_CLASSES | REG_DOT_NEWLINE | REG_DOT_NOT_NULL \
236 | RE_INTERVALS | RE_NO_EMPTY_RANGES) 282 | REG_INTERVALS | REG_NO_EMPTY_RANGES)
237 283
238 #define RE_SYNTAX_POSIX_BASIC \ 284 #define REG_SYNTAX_POSIX_BASIC \
239 (_RE_SYNTAX_POSIX_COMMON | RE_BK_PLUS_QM | RE_CONTEXT_INVALID_DUP) 285 (_REG_SYNTAX_POSIX_COMMON | REG_BK_PLUS_QM | REG_CONTEXT_INVALID_DUP)
240 286
241 /* Differs from ..._POSIX_BASIC only in that RE_BK_PLUS_QM becomes 287 /* Differs from ..._POSIX_BASIC only in that REG_BK_PLUS_QM becomes
242 RE_LIMITED_OPS, i.e., \? \+ \| are not recognized. Actually, this 288 REG_LIMITED_OPS, i.e., \? \+ \| are not recognized. Actually, this
243 isn't minimal, since other operators, such as \`, aren't disabled. */ 289 isn't minimal, since other operators, such as \`, aren't disabled. */
244 #define RE_SYNTAX_POSIX_MINIMAL_BASIC \ 290 #define REG_SYNTAX_POSIX_MINIMAL_BASIC \
245 (_RE_SYNTAX_POSIX_COMMON | RE_LIMITED_OPS) 291 (_REG_SYNTAX_POSIX_COMMON | REG_LIMITED_OPS)
246 292
247 #define RE_SYNTAX_POSIX_EXTENDED \ 293 #define REG_SYNTAX_POSIX_EXTENDED \
248 (_RE_SYNTAX_POSIX_COMMON | RE_CONTEXT_INDEP_ANCHORS \ 294 (_REG_SYNTAX_POSIX_COMMON | REG_CONTEXT_INDEP_ANCHORS \
249 | RE_CONTEXT_INDEP_OPS | RE_NO_BK_BRACES \ 295 | REG_CONTEXT_INDEP_OPS | REG_NO_BK_BRACES \
250 | RE_NO_BK_PARENS | RE_NO_BK_VBAR \ 296 | REG_NO_BK_PARENS | REG_NO_BK_VBAR \
251 | RE_CONTEXT_INVALID_OPS | RE_UNMATCHED_RIGHT_PAREN_ORD) 297 | REG_CONTEXT_INVALID_OPS | REG_UNMATCHED_RIGHT_PAREN_ORD)
252 298
253 /* Differs from ..._POSIX_EXTENDED in that RE_CONTEXT_INDEP_OPS is 299 /* Differs from ..._POSIX_EXTENDED in that REG_CONTEXT_INDEP_OPS is
254 removed and RE_NO_BK_REFS is added. */ 300 removed and REG_NO_BK_REFS is added. */
255 #define RE_SYNTAX_POSIX_MINIMAL_EXTENDED \ 301 #define REG_SYNTAX_POSIX_MINIMAL_EXTENDED \
256 (_RE_SYNTAX_POSIX_COMMON | RE_CONTEXT_INDEP_ANCHORS \ 302 (_REG_SYNTAX_POSIX_COMMON | REG_CONTEXT_INDEP_ANCHORS \
257 | RE_CONTEXT_INVALID_OPS | RE_NO_BK_BRACES \ 303 | REG_CONTEXT_INVALID_OPS | REG_NO_BK_BRACES \
258 | RE_NO_BK_PARENS | RE_NO_BK_REFS \ 304 | REG_NO_BK_PARENS | REG_NO_BK_REFS \
259 | RE_NO_BK_VBAR | RE_UNMATCHED_RIGHT_PAREN_ORD) 305 | REG_NO_BK_VBAR | REG_UNMATCHED_RIGHT_PAREN_ORD)
260 /* [[[end syntaxes]]] */ 306 /* [[[end syntaxes]]] */
261 307
262 /* Maximum number of duplicates an interval can allow. Some systems 308 /* Maximum number of duplicates an interval can allow. This is
263 (erroneously) define this in other header files, but we want our 309 distinct from RE_DUP_MAX, to conform to POSIX name space rules and
264 value, so remove any previous define. */ 310 to avoid collisions with <limits.h>. */
265 #ifdef RE_DUP_MAX 311 #define REG_DUP_MAX 32767
266 # undef RE_DUP_MAX
267 #endif
268 /* If sizeof(int) == 2, then ((1 << 15) - 1) overflows. */
269 #define RE_DUP_MAX (0x7fff)
270 312
271 313
272 /* POSIX `cflags' bits (i.e., information for `regcomp'). */ 314 /* POSIX `cflags' bits (i.e., information for `regcomp'). */
...@@ -277,16 +319,16 @@ extern reg_syntax_t re_syntax_options; ...@@ -277,16 +319,16 @@ extern reg_syntax_t re_syntax_options;
277 319
278 /* If this bit is set, then ignore case when matching. 320 /* If this bit is set, then ignore case when matching.
279 If not set, then case is significant. */ 321 If not set, then case is significant. */
280 #define REG_ICASE (REG_EXTENDED << 1) 322 #define REG_ICASE (1 << 1)
281 323
282 /* If this bit is set, then anchors do not match at newline 324 /* If this bit is set, then anchors do not match at newline
283 characters in the string. 325 characters in the string.
284 If not set, then anchors do match at newlines. */ 326 If not set, then anchors do match at newlines. */
285 #define REG_NEWLINE (REG_ICASE << 1) 327 #define REG_NEWLINE (1 << 2)
286 328
287 /* If this bit is set, then report only success or fail in regexec. 329 /* If this bit is set, then report only success or fail in regexec.
288 If not set, then returns differ between not matching and errors. */ 330 If not set, then returns differ between not matching and errors. */
289 #define REG_NOSUB (REG_NEWLINE << 1) 331 #define REG_NOSUB (1 << 3)
290 332
291 333
292 /* POSIX `eflags' bits (i.e., information for regexec). */ 334 /* POSIX `eflags' bits (i.e., information for regexec). */
...@@ -307,74 +349,131 @@ extern reg_syntax_t re_syntax_options; ...@@ -307,74 +349,131 @@ extern reg_syntax_t re_syntax_options;
307 349
308 350
309 /* If any error codes are removed, changed, or added, update the 351 /* If any error codes are removed, changed, or added, update the
310 `re_error_msg' table in regex.c. */ 352 `__re_error_msgid' table in regcomp.c. */
353
311 typedef enum 354 typedef enum
312 { 355 {
313 #ifdef _XOPEN_SOURCE 356 _REG_ENOSYS = -1, /* This will never happen for this implementation. */
314 REG_ENOSYS = -1, /* This will never happen for this implementation. */ 357 #define REG_ENOSYS _REG_ENOSYS
315 #endif 358
359 _REG_NOERROR, /* Success. */
360 #define REG_NOERROR _REG_NOERROR
316 361
317 REG_NOERROR = 0, /* Success. */ 362 _REG_NOMATCH, /* Didn't find a match (for regexec). */
318 REG_NOMATCH, /* Didn't find a match (for regexec). */ 363 #define REG_NOMATCH _REG_NOMATCH
319 364
320 /* POSIX regcomp return error codes. (In the order listed in the 365 /* POSIX regcomp return error codes. (In the order listed in the
321 standard.) */ 366 standard.) */
322 REG_BADPAT, /* Invalid pattern. */ 367
323 REG_ECOLLATE, /* Inalid collating element. */ 368 _REG_BADPAT, /* Invalid pattern. */
324 REG_ECTYPE, /* Invalid character class name. */ 369 #define REG_BADPAT _REG_BADPAT
325 REG_EESCAPE, /* Trailing backslash. */ 370
326 REG_ESUBREG, /* Invalid back reference. */ 371 _REG_ECOLLATE, /* Inalid collating element. */
327 REG_EBRACK, /* Unmatched left bracket. */ 372 #define REG_ECOLLATE _REG_ECOLLATE
328 REG_EPAREN, /* Parenthesis imbalance. */ 373
329 REG_EBRACE, /* Unmatched \{. */ 374 _REG_ECTYPE, /* Invalid character class name. */
330 REG_BADBR, /* Invalid contents of \{\}. */ 375 #define REG_ECTYPE _REG_ECTYPE
331 REG_ERANGE, /* Invalid range end. */ 376
332 REG_ESPACE, /* Ran out of memory. */ 377 _REG_EESCAPE, /* Trailing backslash. */
333 REG_BADRPT, /* No preceding re for repetition op. */ 378 #define REG_EESCAPE _REG_EESCAPE
379
380 _REG_ESUBREG, /* Invalid back reference. */
381 #define REG_ESUBREG _REG_ESUBREG
382
383 _REG_EBRACK, /* Unmatched left bracket. */
384 #define REG_EBRACK _REG_EBRACK
385
386 _REG_EPAREN, /* Parenthesis imbalance. */
387 #define REG_EPAREN _REG_EPAREN
388
389 _REG_EBRACE, /* Unmatched \{. */
390 #define REG_EBRACE _REG_EBRACE
391
392 _REG_BADBR, /* Invalid contents of \{\}. */
393 #define REG_BADBR _REG_BADBR
394
395 _REG_ERANGE, /* Invalid range end. */
396 #define REG_ERANGE _REG_ERANGE
397
398 _REG_ESPACE, /* Ran out of memory. */
399 #define REG_ESPACE _REG_ESPACE
400
401 _REG_BADRPT, /* No preceding re for repetition op. */
402 #define REG_BADRPT _REG_BADRPT
334 403
335 /* Error codes we've added. */ 404 /* Error codes we've added. */
336 REG_EEND, /* Premature end. */ 405
337 REG_ESIZE, /* Compiled pattern bigger than 2^16 bytes. */ 406 _REG_EEND, /* Premature end. */
338 REG_ERPAREN /* Unmatched ) or \); not returned from regcomp. */ 407 #define REG_EEND _REG_EEND
408
409 _REG_ESIZE, /* Compiled pattern bigger than 2^16 bytes. */
410 #define REG_ESIZE _REG_ESIZE
411
412 _REG_ERPAREN /* Unmatched ) or \); not returned from regcomp. */
413 #define REG_ERPAREN _REG_ERPAREN
414
339 } reg_errcode_t; 415 } reg_errcode_t;
340 416
417 /* In the traditional GNU implementation, regex.h defined member names
418 like `buffer' that POSIX does not allow. These members now have
419 names with leading `re_' (e.g., `re_buffer'). Support the old
420 names only if _REGEX_SOURCE is defined. New programs should use
421 the new names. */
422 #ifdef _REGEX_SOURCE
423 # define _REG_RE_NAME(id) id
424 # define _REG_RM_NAME(id) id
425 #else
426 # define _REG_RE_NAME(id) re_##id
427 # define _REG_RM_NAME(id) rm_##id
428 #endif
429
430 /* The user can specify the type of the re_translate member by
431 defining the macro REG_TRANSLATE_TYPE. In the traditional GNU
432 implementation, this macro was named RE_TRANSLATE_TYPE, but POSIX
433 does not allow this. Support the old name only if _REGEX_SOURCE
434 and if the new name is not defined. New programs should use the new
435 name. */
436 #ifndef REG_TRANSLATE_TYPE
437 # if defined _REGEX_SOURCE && defined RE_TRANSLATE_TYPE
438 # define REG_TRANSLATE_TYPE RE_TRANSLATE_TYPE
439 # else
440 # define REG_TRANSLATE_TYPE char *
441 # endif
442 #endif
443
341 /* This data structure represents a compiled pattern. Before calling 444 /* This data structure represents a compiled pattern. Before calling
342 the pattern compiler, the fields `buffer', `allocated', `fastmap', 445 the pattern compiler), the fields `re_buffer', `re_allocated', `re_fastmap',
343 `translate', and `no_sub' can be set. After the pattern has been 446 `re_translate', and `re_no_sub' can be set. After the pattern has been
344 compiled, the `re_nsub' field is available. All other fields are 447 compiled, the `re_nsub' field is available. All other fields are
345 private to the regex routines. */ 448 private to the regex routines. */
346 449
347 #ifndef RE_TRANSLATE_TYPE
348 # define RE_TRANSLATE_TYPE char *
349 #endif
350
351 struct re_pattern_buffer 450 struct re_pattern_buffer
352 { 451 {
353 /* [[[begin pattern_buffer]]] */ 452 /* [[[begin pattern_buffer]]] */
354 /* Space that holds the compiled pattern. It is declared as 453 /* Space that holds the compiled pattern. It is declared as
355 `unsigned char *' because its elements are 454 `unsigned char *' because its elements are
356 sometimes used as array indexes. */ 455 sometimes used as array indexes. */
357 unsigned char *buffer; 456 unsigned char *_REG_RE_NAME (buffer);
358 457
359 /* Number of bytes to which `buffer' points. */ 458 /* Number of bytes to which `re_buffer' points. */
360 unsigned long int allocated; 459 __re_long_size_t _REG_RE_NAME (allocated);
361 460
362 /* Number of bytes actually used in `buffer'. */ 461 /* Number of bytes actually used in `re_buffer'. */
363 unsigned long int used; 462 __re_long_size_t _REG_RE_NAME (used);
364 463
365 /* Syntax setting with which the pattern was compiled. */ 464 /* Syntax setting with which the pattern was compiled. */
366 reg_syntax_t syntax; 465 reg_syntax_t _REG_RE_NAME (syntax);
367 466
368 /* Pointer to a fastmap, if any, otherwise zero. re_search uses 467 /* Pointer to a fastmap, if any, otherwise zero. re_search uses
369 the fastmap, if there is one, to skip over impossible 468 the fastmap, if there is one, to skip over impossible
370 starting points for matches. */ 469 starting points for matches. */
371 char *fastmap; 470 char *_REG_RE_NAME (fastmap);
372 471
373 /* Either a translate table to apply to all characters before 472 /* Either a translate table to apply to all characters before
374 comparing them, or zero for no translation. The translation 473 comparing them, or zero for no translation. The translation
375 is applied to a pattern when it is compiled and to a string 474 is applied to a pattern when it is compiled and to a string
376 when it is matched. */ 475 when it is matched. */
377 RE_TRANSLATE_TYPE translate; 476 REG_TRANSLATE_TYPE _REG_RE_NAME (translate);
378 477
379 /* Number of subexpressions found by the compiler. */ 478 /* Number of subexpressions found by the compiler. */
380 size_t re_nsub; 479 size_t re_nsub;
...@@ -384,59 +483,55 @@ struct re_pattern_buffer ...@@ -384,59 +483,55 @@ struct re_pattern_buffer
384 whether or not we should use the fastmap, so we don't set 483 whether or not we should use the fastmap, so we don't set
385 this absolutely perfectly; see `re_compile_fastmap' (the 484 this absolutely perfectly; see `re_compile_fastmap' (the
386 `duplicate' case). */ 485 `duplicate' case). */
387 unsigned can_be_null : 1; 486 unsigned int _REG_RE_NAME (can_be_null) : 1;
388 487
389 /* If REGS_UNALLOCATED, allocate space in the `regs' structure 488 /* If REG_UNALLOCATED, allocate space in the `regs' structure
390 for `max (RE_NREGS, re_nsub + 1)' groups. 489 for `max (REG_NREGS, re_nsub + 1)' groups.
391 If REGS_REALLOCATE, reallocate space if necessary. 490 If REG_REALLOCATE, reallocate space if necessary.
392 If REGS_FIXED, use what's there. */ 491 If REG_FIXED, use what's there. */
393 #define REGS_UNALLOCATED 0 492 #define REG_UNALLOCATED 0
394 #define REGS_REALLOCATE 1 493 #define REG_REALLOCATE 1
395 #define REGS_FIXED 2 494 #define REG_FIXED 2
396 unsigned regs_allocated : 2; 495 unsigned int _REG_RE_NAME (regs_allocated) : 2;
397 496
398 /* Set to zero when `regex_compile' compiles a pattern; set to one 497 /* Set to zero when `regex_compile' compiles a pattern; set to one
399 by `re_compile_fastmap' if it updates the fastmap. */ 498 by `re_compile_fastmap' if it updates the fastmap. */
400 unsigned fastmap_accurate : 1; 499 unsigned int _REG_RE_NAME (fastmap_accurate) : 1;
401 500
402 /* If set, `re_match_2' does not return information about 501 /* If set, `re_match_2' does not return information about
403 subexpressions. */ 502 subexpressions. */
404 unsigned no_sub : 1; 503 unsigned int _REG_RE_NAME (no_sub) : 1;
405 504
406 /* If set, a beginning-of-line anchor doesn't match at the 505 /* If set, a beginning-of-line anchor doesn't match at the
407 beginning of the string. */ 506 beginning of the string. */
408 unsigned not_bol : 1; 507 unsigned int _REG_RE_NAME (not_bol) : 1;
409 508
410 /* Similarly for an end-of-line anchor. */ 509 /* Similarly for an end-of-line anchor. */
411 unsigned not_eol : 1; 510 unsigned int _REG_RE_NAME (not_eol) : 1;
412 511
413 /* If true, an anchor at a newline matches. */ 512 /* If true, an anchor at a newline matches. */
414 unsigned newline_anchor : 1; 513 unsigned int _REG_RE_NAME (newline_anchor) : 1;
415 514
416 /* [[[end pattern_buffer]]] */ 515 /* [[[end pattern_buffer]]] */
417 }; 516 };
418 517
419 typedef struct re_pattern_buffer regex_t; 518 typedef struct re_pattern_buffer regex_t;
420 519
421 /* Type for byte offsets within the string. POSIX mandates this. */
422 typedef int regoff_t;
423
424
425 /* This is the structure we store register match data in. See 520 /* This is the structure we store register match data in. See
426 regex.texinfo for a full description of what registers match. */ 521 regex.texinfo for a full description of what registers match. */
427 struct re_registers 522 struct re_registers
428 { 523 {
429 unsigned num_regs; 524 __re_size_t _REG_RM_NAME (num_regs);
430 regoff_t *start; 525 regoff_t *_REG_RM_NAME (start);
431 regoff_t *end; 526 regoff_t *_REG_RM_NAME (end);
432 }; 527 };
433 528
434 529
435 /* If `regs_allocated' is REGS_UNALLOCATED in the pattern buffer, 530 /* If `regs_allocated' is REG_UNALLOCATED in the pattern buffer,
436 `re_match_2' returns information about at least this many registers 531 `re_match_2' returns information about at least this many registers
437 the first time a `regs' structure is passed. */ 532 the first time a `regs' structure is passed. */
438 #ifndef RE_NREGS 533 #ifndef REG_NREGS
439 # define RE_NREGS 30 534 # define REG_NREGS 30
440 #endif 535 #endif
441 536
442 537
...@@ -451,70 +546,57 @@ typedef struct ...@@ -451,70 +546,57 @@ typedef struct
451 546
452 /* Declarations for routines. */ 547 /* Declarations for routines. */
453 548
454 /* To avoid duplicating every routine declaration -- once with a
455 prototype (if we are ANSI), and once without (if we aren't) -- we
456 use the following macro to declare argument types. This
457 unfortunately clutters up the declarations a bit, but I think it's
458 worth it. */
459
460 #if __STDC__
461
462 # define _RE_ARGS(args) args
463
464 #else /* not __STDC__ */
465
466 # define _RE_ARGS(args) ()
467
468 #endif /* not __STDC__ */
469
470 /* Sets the current default syntax to SYNTAX, and return the old syntax. 549 /* Sets the current default syntax to SYNTAX, and return the old syntax.
471 You can also simply assign to the `re_syntax_options' variable. */ 550 You can also simply assign to the `re_syntax_options' variable. */
472 extern reg_syntax_t re_set_syntax _RE_ARGS ((reg_syntax_t syntax)); 551 extern reg_syntax_t re_set_syntax (reg_syntax_t __syntax);
473 552
474 /* Compile the regular expression PATTERN, with length LENGTH 553 /* Compile the regular expression PATTERN, with length LENGTH
475 and syntax given by the global `re_syntax_options', into the buffer 554 and syntax given by the global `re_syntax_options', into the buffer
476 BUFFER. Return NULL if successful, and an error string if not. */ 555 BUFFER. Return NULL if successful, and an error string if not. */
477 extern const char *re_compile_pattern 556 extern const char *re_compile_pattern (const char *__pattern, size_t __length,
478 _RE_ARGS ((const char *pattern, size_t length, 557 struct re_pattern_buffer *__buffer);
479 struct re_pattern_buffer *buffer));
480 558
481 559
482 /* Compile a fastmap for the compiled pattern in BUFFER; used to 560 /* Compile a fastmap for the compiled pattern in BUFFER; used to
483 accelerate searches. Return 0 if successful and -2 if was an 561 accelerate searches. Return 0 if successful and -2 if was an
484 internal error. */ 562 internal error. */
485 extern int re_compile_fastmap _RE_ARGS ((struct re_pattern_buffer *buffer)); 563 extern int re_compile_fastmap (struct re_pattern_buffer *__buffer);
486 564
487 565
488 /* Search in the string STRING (with length LENGTH) for the pattern 566 /* Search in the string STRING (with length LENGTH) for the pattern
489 compiled into BUFFER. Start searching at position START, for RANGE 567 compiled into BUFFER. Start searching at position START, for RANGE
490 characters. Return the starting position of the match, -1 for no 568 characters. Return the starting position of the match, -1 for no
491 match, or -2 for an internal error. Also return register 569 match, or -2 for an internal error. Also return register
492 information in REGS (if REGS and BUFFER->no_sub are nonzero). */ 570 information in REGS (if REGS and BUFFER->re_no_sub are nonzero). */
493 extern int re_search 571 extern regoff_t re_search (struct re_pattern_buffer *__buffer,
494 _RE_ARGS ((struct re_pattern_buffer *buffer, const char *string, 572 const char *__string, __re_idx_t __length,
495 int length, int start, int range, struct re_registers *regs)); 573 __re_idx_t __start, regoff_t __range,
574 struct re_registers *__regs);
496 575
497 576
498 /* Like `re_search', but search in the concatenation of STRING1 and 577 /* Like `re_search', but search in the concatenation of STRING1 and
499 STRING2. Also, stop searching at index START + STOP. */ 578 STRING2. Also, stop searching at index START + STOP. */
500 extern int re_search_2 579 extern regoff_t re_search_2 (struct re_pattern_buffer *__buffer,
501 _RE_ARGS ((struct re_pattern_buffer *buffer, const char *string1, 580 const char *__string1, __re_idx_t __length1,
502 int length1, const char *string2, int length2, 581 const char *__string2, __re_idx_t __length2,
503 int start, int range, struct re_registers *regs, int stop)); 582 __re_idx_t __start, regoff_t __range,
583 struct re_registers *__regs,
584 __re_idx_t __stop);
504 585
505 586
506 /* Like `re_search', but return how many characters in STRING the regexp 587 /* Like `re_search', but return how many characters in STRING the regexp
507 in BUFFER matched, starting at position START. */ 588 in BUFFER matched, starting at position START. */
508 extern int re_match 589 extern regoff_t re_match (struct re_pattern_buffer *__buffer,
509 _RE_ARGS ((struct re_pattern_buffer *buffer, const char *string, 590 const char *__string, __re_idx_t __length,
510 int length, int start, struct re_registers *regs)); 591 __re_idx_t __start, struct re_registers *__regs);
511 592
512 593
513 /* Relates to `re_match' as `re_search_2' relates to `re_search'. */ 594 /* Relates to `re_match' as `re_search_2' relates to `re_search'. */
514 extern int re_match_2 595 extern regoff_t re_match_2 (struct re_pattern_buffer *__buffer,
515 _RE_ARGS ((struct re_pattern_buffer *buffer, const char *string1, 596 const char *__string1, __re_idx_t __length1,
516 int length1, const char *string2, int length2, 597 const char *__string2, __re_idx_t __length2,
517 int start, struct re_registers *regs, int stop)); 598 __re_idx_t __start, struct re_registers *__regs,
599 __re_idx_t __stop);
518 600
519 601
520 /* Set REGS to hold NUM_REGS registers, storing them in STARTS and 602 /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
...@@ -529,15 +611,16 @@ extern int re_match_2 ...@@ -529,15 +611,16 @@ extern int re_match_2
529 Unless this function is called, the first search or match using 611 Unless this function is called, the first search or match using
530 PATTERN_BUFFER will allocate its own register data, without 612 PATTERN_BUFFER will allocate its own register data, without
531 freeing the old data. */ 613 freeing the old data. */
532 extern void re_set_registers 614 extern void re_set_registers (struct re_pattern_buffer *__buffer,
533 _RE_ARGS ((struct re_pattern_buffer *buffer, struct re_registers *regs, 615 struct re_registers *__regs,
534 unsigned num_regs, regoff_t *starts, regoff_t *ends)); 616 __re_size_t __num_regs,
617 regoff_t *__starts, regoff_t *__ends);
535 618
536 #if defined _REGEX_RE_COMP || defined _LIBC 619 #if defined _REGEX_RE_COMP || defined _LIBC
537 # ifndef _CRAY 620 # ifndef _CRAY
538 /* 4.2 bsd compatibility. */ 621 /* 4.2 bsd compatibility. */
539 extern char *re_comp _RE_ARGS ((const char *)); 622 extern char *re_comp (const char *);
540 extern int re_exec _RE_ARGS ((const char *)); 623 extern int re_exec (const char *);
541 # endif 624 # endif
542 #endif 625 #endif
543 626
...@@ -562,20 +645,114 @@ extern int re_exec _RE_ARGS ((const char *)); ...@@ -562,20 +645,114 @@ extern int re_exec _RE_ARGS ((const char *));
562 #endif 645 #endif
563 646
564 /* POSIX compatibility. */ 647 /* POSIX compatibility. */
565 extern int regcomp _RE_ARGS ((regex_t *__restrict __preg, 648 extern int regcomp (regex_t *__restrict __preg,
566 const char *__restrict __pattern, 649 const char *__restrict __pattern,
567 int __cflags)); 650 int __cflags);
651
652 extern int regexec (const regex_t *__restrict __preg,
653 const char *__restrict __string, size_t __nmatch,
654 regmatch_t __pmatch[__restrict_arr],
655 int __eflags);
656
657 extern size_t regerror (int __errcode, const regex_t *__restrict __preg,
658 char *__restrict __errbuf, size_t __errbuf_size);
659
660 extern void regfree (regex_t *__preg);
661
662
663 #ifdef _REGEX_SOURCE
568 664
569 extern int regexec _RE_ARGS ((const regex_t *__restrict __preg, 665 /* Define the POSIX-compatible member names in terms of the
570 const char *__restrict __string, size_t __nmatch, 666 incompatible (and deprecated) names established by _REG_RE_NAME.
571 regmatch_t __pmatch[__restrict_arr], 667 New programs should use the re_* names. */
572 int __eflags));
573 668
574 extern size_t regerror _RE_ARGS ((int __errcode, const regex_t *__preg, 669 # define re_allocated allocated
575 char *__errbuf, size_t __errbuf_size)); 670 # define re_buffer buffer
671 # define re_can_be_null can_be_null
672 # define re_fastmap fastmap
673 # define re_fastmap_accurate fastmap_accurate
674 # define re_newline_anchor newline_anchor
675 # define re_no_sub no_sub
676 # define re_not_bol not_bol
677 # define re_not_eol not_eol
678 # define re_regs_allocated regs_allocated
679 # define re_syntax syntax
680 # define re_translate translate
681 # define re_used used
576 682
577 extern void regfree _RE_ARGS ((regex_t *__preg)); 683 /* Similarly for _REG_RM_NAME. */
684
685 # define rm_end end
686 # define rm_num_regs num_regs
687 # define rm_start start
688
689 /* Undef RE_DUP_MAX first, in case the user has already included a
690 <limits.h> with an incompatible definition.
691
692 On GNU systems, the most common spelling for RE_DUP_MAX's value in
693 <limits.h> is (0x7ffff), so define RE_DUP_MAX to that, not to
694 REG_DUP_MAX. This avoid some duplicate-macro-definition warnings
695 with programs that include <limits.h> after this file.
696
697 New programs should not assume that regex.h defines RE_DUP_MAX; to
698 get the value of RE_DUP_MAX, they should instead include <limits.h>
699 and possibly invoke the sysconf function. */
700
701 # undef RE_DUP_MAX
702 # define RE_DUP_MAX (0x7fff)
703
704 /* Define the following symbols for backward source compatibility.
705 These symbols violate the POSIX name space rules, and new programs
706 should avoid them. */
707
708 # define REGS_FIXED REG_FIXED
709 # define REGS_REALLOCATE REG_REALLOCATE
710 # define REGS_UNALLOCATED REG_UNALLOCATED
711 # define RE_BACKSLASH_ESCAPE_IN_LISTS REG_BACKSLASH_ESCAPE_IN_LISTS
712 # define RE_BK_PLUS_QM REG_BK_PLUS_QM
713 # define RE_CARET_ANCHORS_HERE REG_CARET_ANCHORS_HERE
714 # define RE_CHAR_CLASSES REG_CHAR_CLASSES
715 # define RE_CONTEXT_INDEP_ANCHORS REG_CONTEXT_INDEP_ANCHORS
716 # define RE_CONTEXT_INDEP_OPS REG_CONTEXT_INDEP_OPS
717 # define RE_CONTEXT_INVALID_DUP REG_CONTEXT_INVALID_DUP
718 # define RE_CONTEXT_INVALID_OPS REG_CONTEXT_INVALID_OPS
719 # define RE_DEBUG REG_DEBUG
720 # define RE_DOT_NEWLINE REG_DOT_NEWLINE
721 # define RE_DOT_NOT_NULL REG_DOT_NOT_NULL
722 # define RE_HAT_LISTS_NOT_NEWLINE REG_HAT_LISTS_NOT_NEWLINE
723 # define RE_ICASE REG_IGNORE_CASE /* avoid collision with REG_ICASE */
724 # define RE_INTERVALS REG_INTERVALS
725 # define RE_INVALID_INTERVAL_ORD REG_INVALID_INTERVAL_ORD
726 # define RE_LIMITED_OPS REG_LIMITED_OPS
727 # define RE_NEWLINE_ALT REG_NEWLINE_ALT
728 # define RE_NO_BK_BRACES REG_NO_BK_BRACES
729 # define RE_NO_BK_PARENS REG_NO_BK_PARENS
730 # define RE_NO_BK_REFS REG_NO_BK_REFS
731 # define RE_NO_BK_VBAR REG_NO_BK_VBAR
732 # define RE_NO_EMPTY_RANGES REG_NO_EMPTY_RANGES
733 # define RE_NO_GNU_OPS REG_NO_GNU_OPS
734 # define RE_NO_POSIX_BACKTRACKING REG_NO_POSIX_BACKTRACKING
735 # define RE_NO_SUB REG_NO_SUB
736 # define RE_NREGS REG_NREGS
737 # define RE_SYNTAX_AWK REG_SYNTAX_AWK
738 # define RE_SYNTAX_ED REG_SYNTAX_ED
739 # define RE_SYNTAX_EGREP REG_SYNTAX_EGREP
740 # define RE_SYNTAX_EMACS REG_SYNTAX_EMACS
741 # define RE_SYNTAX_GNU_AWK REG_SYNTAX_GNU_AWK
742 # define RE_SYNTAX_GREP REG_SYNTAX_GREP
743 # define RE_SYNTAX_POSIX_AWK REG_SYNTAX_POSIX_AWK
744 # define RE_SYNTAX_POSIX_BASIC REG_SYNTAX_POSIX_BASIC
745 # define RE_SYNTAX_POSIX_EGREP REG_SYNTAX_POSIX_EGREP
746 # define RE_SYNTAX_POSIX_EXTENDED REG_SYNTAX_POSIX_EXTENDED
747 # define RE_SYNTAX_POSIX_MINIMAL_BASIC REG_SYNTAX_POSIX_MINIMAL_BASIC
748 # define RE_SYNTAX_POSIX_MINIMAL_EXTENDED REG_SYNTAX_POSIX_MINIMAL_EXTENDED
749 # define RE_SYNTAX_SED REG_SYNTAX_SED
750 # define RE_UNMATCHED_RIGHT_PAREN_ORD REG_UNMATCHED_RIGHT_PAREN_ORD
751 # ifndef RE_TRANSLATE_TYPE
752 # define RE_TRANSLATE_TYPE REG_TRANSLATE_TYPE
753 # endif
578 754
755 #endif /* defined _REGEX_SOURCE */
579 756
580 #ifdef __cplusplus 757 #ifdef __cplusplus
581 } 758 }
......
...@@ -17,25 +17,17 @@ ...@@ -17,25 +17,17 @@
17 with this program; if not, write to the Free Software Foundation, 17 with this program; if not, write to the Free Software Foundation,
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 19
20 static void re_string_construct_common (const char *str, int len, 20 static void re_string_construct_common (const char *str, Idx len,
21 re_string_t *pstr, 21 re_string_t *pstr,
22 RE_TRANSLATE_TYPE trans, int icase, 22 REG_TRANSLATE_TYPE trans, bool icase,
23 const re_dfa_t *dfa) internal_function; 23 const re_dfa_t *dfa) internal_function;
24 #ifdef RE_ENABLE_I18N 24 static re_dfastate_t *create_ci_newstate (const re_dfa_t *dfa,
25 static int re_string_skip_chars (re_string_t *pstr, int new_raw_idx,
26 wint_t *last_wc) internal_function;
27 #endif /* RE_ENABLE_I18N */
28 static reg_errcode_t register_state (re_dfa_t *dfa, re_dfastate_t *newstate,
29 unsigned int hash) internal_function;
30 static re_dfastate_t *create_ci_newstate (re_dfa_t *dfa,
31 const re_node_set *nodes, 25 const re_node_set *nodes,
32 unsigned int hash) internal_function; 26 re_hashval_t hash) internal_function;
33 static re_dfastate_t *create_cd_newstate (re_dfa_t *dfa, 27 static re_dfastate_t *create_cd_newstate (const re_dfa_t *dfa,
34 const re_node_set *nodes, 28 const re_node_set *nodes,
35 unsigned int context, 29 unsigned int context,
36 unsigned int hash) internal_function; 30 re_hashval_t hash) internal_function;
37 static unsigned int inline calc_state_hash (const re_node_set *nodes,
38 unsigned int context) internal_function;
39 31
40 /* Functions for string operation. */ 32 /* Functions for string operation. */
41 33
...@@ -43,15 +35,12 @@ static unsigned int inline calc_state_hash (const re_node_set *nodes, ...@@ -43,15 +35,12 @@ static unsigned int inline calc_state_hash (const re_node_set *nodes,
43 re_string_reconstruct before using the object. */ 35 re_string_reconstruct before using the object. */
44 36
45 static reg_errcode_t 37 static reg_errcode_t
46 re_string_allocate (pstr, str, len, init_len, trans, icase, dfa) 38 internal_function
47 re_string_t *pstr; 39 re_string_allocate (re_string_t *pstr, const char *str, Idx len, Idx init_len,
48 const char *str; 40 REG_TRANSLATE_TYPE trans, bool icase, const re_dfa_t *dfa)
49 int len, init_len, icase;
50 RE_TRANSLATE_TYPE trans;
51 const re_dfa_t *dfa;
52 { 41 {
53 reg_errcode_t ret; 42 reg_errcode_t ret;
54 int init_buf_len; 43 Idx init_buf_len;
55 44
56 /* Ensure at least one character fits into the buffers. */ 45 /* Ensure at least one character fits into the buffers. */
57 if (init_len < dfa->mb_cur_max) 46 if (init_len < dfa->mb_cur_max)
...@@ -74,12 +63,9 @@ re_string_allocate (pstr, str, len, init_len, trans, icase, dfa) ...@@ -74,12 +63,9 @@ re_string_allocate (pstr, str, len, init_len, trans, icase, dfa)
74 /* This function allocate the buffers, and initialize them. */ 63 /* This function allocate the buffers, and initialize them. */
75 64
76 static reg_errcode_t 65 static reg_errcode_t
77 re_string_construct (pstr, str, len, trans, icase, dfa) 66 internal_function
78 re_string_t *pstr; 67 re_string_construct (re_string_t *pstr, const char *str, Idx len,
79 const char *str; 68 REG_TRANSLATE_TYPE trans, bool icase, const re_dfa_t *dfa)
80 int len, icase;
81 RE_TRANSLATE_TYPE trans;
82 const re_dfa_t *dfa;
83 { 69 {
84 reg_errcode_t ret; 70 reg_errcode_t ret;
85 memset (pstr, '\0', sizeof (re_string_t)); 71 memset (pstr, '\0', sizeof (re_string_t));
...@@ -140,33 +126,32 @@ re_string_construct (pstr, str, len, trans, icase, dfa) ...@@ -140,33 +126,32 @@ re_string_construct (pstr, str, len, trans, icase, dfa)
140 /* Helper functions for re_string_allocate, and re_string_construct. */ 126 /* Helper functions for re_string_allocate, and re_string_construct. */
141 127
142 static reg_errcode_t 128 static reg_errcode_t
143 re_string_realloc_buffers (pstr, new_buf_len) 129 internal_function
144 re_string_t *pstr; 130 re_string_realloc_buffers (re_string_t *pstr, Idx new_buf_len)
145 int new_buf_len;
146 { 131 {
147 #ifdef RE_ENABLE_I18N 132 #ifdef RE_ENABLE_I18N
148 if (pstr->mb_cur_max > 1) 133 if (pstr->mb_cur_max > 1)
149 { 134 {
150 wint_t *new_array = re_realloc (pstr->wcs, wint_t, new_buf_len); 135 wint_t *new_wcs = re_xrealloc (pstr->wcs, wint_t, new_buf_len);
151 if (BE (new_array == NULL, 0)) 136 if (BE (new_wcs == NULL, 0))
152 return REG_ESPACE; 137 return REG_ESPACE;
153 pstr->wcs = new_array; 138 pstr->wcs = new_wcs;
154 if (pstr->offsets != NULL) 139 if (pstr->offsets != NULL)
155 { 140 {
156 int *new_array = re_realloc (pstr->offsets, int, new_buf_len); 141 Idx *new_offsets = re_xrealloc (pstr->offsets, Idx, new_buf_len);
157 if (BE (new_array == NULL, 0)) 142 if (BE (new_offsets == NULL, 0))
158 return REG_ESPACE; 143 return REG_ESPACE;
159 pstr->offsets = new_array; 144 pstr->offsets = new_offsets;
160 } 145 }
161 } 146 }
162 #endif /* RE_ENABLE_I18N */ 147 #endif /* RE_ENABLE_I18N */
163 if (pstr->mbs_allocated) 148 if (pstr->mbs_allocated)
164 { 149 {
165 unsigned char *new_array = re_realloc (pstr->mbs, unsigned char, 150 unsigned char *new_mbs = re_realloc (pstr->mbs, unsigned char,
166 new_buf_len); 151 new_buf_len);
167 if (BE (new_array == NULL, 0)) 152 if (BE (new_mbs == NULL, 0))
168 return REG_ESPACE; 153 return REG_ESPACE;
169 pstr->mbs = new_array; 154 pstr->mbs = new_mbs;
170 } 155 }
171 pstr->bufs_len = new_buf_len; 156 pstr->bufs_len = new_buf_len;
172 return REG_NOERROR; 157 return REG_NOERROR;
...@@ -174,19 +159,16 @@ re_string_realloc_buffers (pstr, new_buf_len) ...@@ -174,19 +159,16 @@ re_string_realloc_buffers (pstr, new_buf_len)
174 159
175 160
176 static void 161 static void
177 re_string_construct_common (str, len, pstr, trans, icase, dfa) 162 internal_function
178 const char *str; 163 re_string_construct_common (const char *str, Idx len, re_string_t *pstr,
179 int len; 164 REG_TRANSLATE_TYPE trans, bool icase,
180 re_string_t *pstr; 165 const re_dfa_t *dfa)
181 RE_TRANSLATE_TYPE trans;
182 int icase;
183 const re_dfa_t *dfa;
184 { 166 {
185 pstr->raw_mbs = (const unsigned char *) str; 167 pstr->raw_mbs = (const unsigned char *) str;
186 pstr->len = len; 168 pstr->len = len;
187 pstr->raw_len = len; 169 pstr->raw_len = len;
188 pstr->trans = (unsigned RE_TRANSLATE_TYPE) trans; 170 pstr->trans = (unsigned REG_TRANSLATE_TYPE) trans;
189 pstr->icase = icase ? 1 : 0; 171 pstr->icase = icase;
190 pstr->mbs_allocated = (trans != NULL || icase); 172 pstr->mbs_allocated = (trans != NULL || icase);
191 pstr->mb_cur_max = dfa->mb_cur_max; 173 pstr->mb_cur_max = dfa->mb_cur_max;
192 pstr->is_utf8 = dfa->is_utf8; 174 pstr->is_utf8 = dfa->is_utf8;
...@@ -209,8 +191,8 @@ re_string_construct_common (str, len, pstr, trans, icase, dfa) ...@@ -209,8 +191,8 @@ re_string_construct_common (str, len, pstr, trans, icase, dfa)
209 built and starts from PSTR->VALID_LEN. */ 191 built and starts from PSTR->VALID_LEN. */
210 192
211 static void 193 static void
212 build_wcs_buffer (pstr) 194 internal_function
213 re_string_t *pstr; 195 build_wcs_buffer (re_string_t *pstr)
214 { 196 {
215 #ifdef _LIBC 197 #ifdef _LIBC
216 unsigned char buf[MB_LEN_MAX]; 198 unsigned char buf[MB_LEN_MAX];
...@@ -219,7 +201,7 @@ build_wcs_buffer (pstr) ...@@ -219,7 +201,7 @@ build_wcs_buffer (pstr)
219 unsigned char buf[64]; 201 unsigned char buf[64];
220 #endif 202 #endif
221 mbstate_t prev_st; 203 mbstate_t prev_st;
222 int byte_idx, end_idx, remain_len; 204 Idx byte_idx, end_idx, remain_len;
223 size_t mbclen; 205 size_t mbclen;
224 206
225 /* Build the buffers from pstr->valid_len to either pstr->len or 207 /* Build the buffers from pstr->valid_len to either pstr->len or
...@@ -276,12 +258,12 @@ build_wcs_buffer (pstr) ...@@ -276,12 +258,12 @@ build_wcs_buffer (pstr)
276 /* Build wide character buffer PSTR->WCS like build_wcs_buffer, 258 /* Build wide character buffer PSTR->WCS like build_wcs_buffer,
277 but for REG_ICASE. */ 259 but for REG_ICASE. */
278 260
279 static int 261 static reg_errcode_t
280 build_wcs_upper_buffer (pstr) 262 internal_function
281 re_string_t *pstr; 263 build_wcs_upper_buffer (re_string_t *pstr)
282 { 264 {
283 mbstate_t prev_st; 265 mbstate_t prev_st;
284 int src_idx, byte_idx, end_idx, remain_len; 266 Idx src_idx, byte_idx, end_idx, remain_len;
285 size_t mbclen; 267 size_t mbclen;
286 #ifdef _LIBC 268 #ifdef _LIBC
287 char buf[MB_LEN_MAX]; 269 char buf[MB_LEN_MAX];
...@@ -319,7 +301,7 @@ build_wcs_upper_buffer (pstr) ...@@ -319,7 +301,7 @@ build_wcs_upper_buffer (pstr)
319 mbclen = mbrtowc (&wc, 301 mbclen = mbrtowc (&wc,
320 ((const char *) pstr->raw_mbs + pstr->raw_mbs_idx 302 ((const char *) pstr->raw_mbs + pstr->raw_mbs_idx
321 + byte_idx), remain_len, &pstr->cur_state); 303 + byte_idx), remain_len, &pstr->cur_state);
322 if (BE (mbclen + 2 > 2, 1)) 304 if (BE ((size_t) (mbclen + 2) > 2, 1))
323 { 305 {
324 wchar_t wcu = wc; 306 wchar_t wcu = wc;
325 if (iswlower (wc)) 307 if (iswlower (wc))
...@@ -387,7 +369,7 @@ build_wcs_upper_buffer (pstr) ...@@ -387,7 +369,7 @@ build_wcs_upper_buffer (pstr)
387 else 369 else
388 p = (const char *) pstr->raw_mbs + pstr->raw_mbs_idx + src_idx; 370 p = (const char *) pstr->raw_mbs + pstr->raw_mbs_idx + src_idx;
389 mbclen = mbrtowc (&wc, p, remain_len, &pstr->cur_state); 371 mbclen = mbrtowc (&wc, p, remain_len, &pstr->cur_state);
390 if (BE (mbclen + 2 > 2, 1)) 372 if (BE ((size_t) (mbclen + 2) > 2, 1))
391 { 373 {
392 wchar_t wcu = wc; 374 wchar_t wcu = wc;
393 if (iswlower (wc)) 375 if (iswlower (wc))
...@@ -410,7 +392,7 @@ build_wcs_upper_buffer (pstr) ...@@ -410,7 +392,7 @@ build_wcs_upper_buffer (pstr)
410 392
411 if (pstr->offsets == NULL) 393 if (pstr->offsets == NULL)
412 { 394 {
413 pstr->offsets = re_malloc (int, pstr->bufs_len); 395 pstr->offsets = re_xmalloc (Idx, pstr->bufs_len);
414 396
415 if (pstr->offsets == NULL) 397 if (pstr->offsets == NULL)
416 return REG_ESPACE; 398 return REG_ESPACE;
...@@ -492,14 +474,12 @@ build_wcs_upper_buffer (pstr) ...@@ -492,14 +474,12 @@ build_wcs_upper_buffer (pstr)
492 /* Skip characters until the index becomes greater than NEW_RAW_IDX. 474 /* Skip characters until the index becomes greater than NEW_RAW_IDX.
493 Return the index. */ 475 Return the index. */
494 476
495 static int 477 static Idx
496 re_string_skip_chars (pstr, new_raw_idx, last_wc) 478 internal_function
497 re_string_t *pstr; 479 re_string_skip_chars (re_string_t *pstr, Idx new_raw_idx, wint_t *last_wc)
498 int new_raw_idx;
499 wint_t *last_wc;
500 { 480 {
501 mbstate_t prev_st; 481 mbstate_t prev_st;
502 int rawbuf_idx; 482 Idx rawbuf_idx;
503 size_t mbclen; 483 size_t mbclen;
504 wchar_t wc = 0; 484 wchar_t wc = 0;
505 485
...@@ -507,7 +487,7 @@ re_string_skip_chars (pstr, new_raw_idx, last_wc) ...@@ -507,7 +487,7 @@ re_string_skip_chars (pstr, new_raw_idx, last_wc)
507 for (rawbuf_idx = pstr->raw_mbs_idx + pstr->valid_raw_len; 487 for (rawbuf_idx = pstr->raw_mbs_idx + pstr->valid_raw_len;
508 rawbuf_idx < new_raw_idx;) 488 rawbuf_idx < new_raw_idx;)
509 { 489 {
510 int remain_len; 490 Idx remain_len;
511 remain_len = pstr->len - rawbuf_idx; 491 remain_len = pstr->len - rawbuf_idx;
512 prev_st = pstr->cur_state; 492 prev_st = pstr->cur_state;
513 mbclen = mbrtowc (&wc, (const char *) pstr->raw_mbs + rawbuf_idx, 493 mbclen = mbrtowc (&wc, (const char *) pstr->raw_mbs + rawbuf_idx,
...@@ -530,10 +510,10 @@ re_string_skip_chars (pstr, new_raw_idx, last_wc) ...@@ -530,10 +510,10 @@ re_string_skip_chars (pstr, new_raw_idx, last_wc)
530 This function is used in case of REG_ICASE. */ 510 This function is used in case of REG_ICASE. */
531 511
532 static void 512 static void
533 build_upper_buffer (pstr) 513 internal_function
534 re_string_t *pstr; 514 build_upper_buffer (re_string_t *pstr)
535 { 515 {
536 int char_idx, end_idx; 516 Idx char_idx, end_idx;
537 end_idx = (pstr->bufs_len > pstr->len) ? pstr->len : pstr->bufs_len; 517 end_idx = (pstr->bufs_len > pstr->len) ? pstr->len : pstr->bufs_len;
538 518
539 for (char_idx = pstr->valid_len; char_idx < end_idx; ++char_idx) 519 for (char_idx = pstr->valid_len; char_idx < end_idx; ++char_idx)
...@@ -553,10 +533,10 @@ build_upper_buffer (pstr) ...@@ -553,10 +533,10 @@ build_upper_buffer (pstr)
553 /* Apply TRANS to the buffer in PSTR. */ 533 /* Apply TRANS to the buffer in PSTR. */
554 534
555 static void 535 static void
556 re_string_translate_buffer (pstr) 536 internal_function
557 re_string_t *pstr; 537 re_string_translate_buffer (re_string_t *pstr)
558 { 538 {
559 int buf_idx, end_idx; 539 Idx buf_idx, end_idx;
560 end_idx = (pstr->bufs_len > pstr->len) ? pstr->len : pstr->bufs_len; 540 end_idx = (pstr->bufs_len > pstr->len) ? pstr->len : pstr->bufs_len;
561 541
562 for (buf_idx = pstr->valid_len; buf_idx < end_idx; ++buf_idx) 542 for (buf_idx = pstr->valid_len; buf_idx < end_idx; ++buf_idx)
...@@ -574,12 +554,14 @@ re_string_translate_buffer (pstr) ...@@ -574,12 +554,14 @@ re_string_translate_buffer (pstr)
574 convert to upper case in case of REG_ICASE, apply translation. */ 554 convert to upper case in case of REG_ICASE, apply translation. */
575 555
576 static reg_errcode_t 556 static reg_errcode_t
577 re_string_reconstruct (pstr, idx, eflags) 557 internal_function
578 re_string_t *pstr; 558 re_string_reconstruct (re_string_t *pstr, Idx idx, int eflags)
579 int idx, eflags;
580 { 559 {
581 int offset = idx - pstr->raw_mbs_idx; 560 Idx offset;
582 if (BE (offset < 0, 0)) 561
562 if (BE (pstr->raw_mbs_idx <= idx, 0))
563 offset = idx - pstr->raw_mbs_idx;
564 else
583 { 565 {
584 /* Reset buffer. */ 566 /* Reset buffer. */
585 #ifdef RE_ENABLE_I18N 567 #ifdef RE_ENABLE_I18N
...@@ -642,7 +624,7 @@ re_string_reconstruct (pstr, idx, eflags) ...@@ -642,7 +624,7 @@ re_string_reconstruct (pstr, idx, eflags)
642 #ifdef RE_ENABLE_I18N 624 #ifdef RE_ENABLE_I18N
643 if (pstr->mb_cur_max > 1) 625 if (pstr->mb_cur_max > 1)
644 { 626 {
645 int wcs_idx; 627 Idx wcs_idx;
646 wint_t wc = WEOF; 628 wint_t wc = WEOF;
647 629
648 if (pstr->is_utf8) 630 if (pstr->is_utf8)
...@@ -658,8 +640,9 @@ re_string_reconstruct (pstr, idx, eflags) ...@@ -658,8 +640,9 @@ re_string_reconstruct (pstr, idx, eflags)
658 { 640 {
659 mbstate_t cur_state; 641 mbstate_t cur_state;
660 wchar_t wc2; 642 wchar_t wc2;
661 int mlen = raw + pstr->len - p; 643 Idx mlen = raw + pstr->len - p;
662 unsigned char buf[6]; 644 unsigned char buf[6];
645 size_t mbclen;
663 646
664 q = p; 647 q = p;
665 if (BE (pstr->trans != NULL, 0)) 648 if (BE (pstr->trans != NULL, 0))
...@@ -672,14 +655,13 @@ re_string_reconstruct (pstr, idx, eflags) ...@@ -672,14 +655,13 @@ re_string_reconstruct (pstr, idx, eflags)
672 /* XXX Don't use mbrtowc, we know which conversion 655 /* XXX Don't use mbrtowc, we know which conversion
673 to use (UTF-8 -> UCS4). */ 656 to use (UTF-8 -> UCS4). */
674 memset (&cur_state, 0, sizeof (cur_state)); 657 memset (&cur_state, 0, sizeof (cur_state));
675 mlen = (mbrtowc (&wc2, (const char *) p, mlen, 658 mbclen = mbrtowc (&wc2, (const char *) p, mlen,
676 &cur_state) 659 &cur_state);
677 - (raw + offset - p)); 660 if (raw + offset - p <= mbclen && mbclen < (size_t) -2)
678 if (mlen >= 0)
679 { 661 {
680 memset (&pstr->cur_state, '\0', 662 memset (&pstr->cur_state, '\0',
681 sizeof (mbstate_t)); 663 sizeof (mbstate_t));
682 pstr->valid_len = mlen; 664 pstr->valid_len = mbclen - (raw + offset - p);
683 wc = wc2; 665 wc = wc2;
684 } 666 }
685 break; 667 break;
...@@ -693,7 +675,7 @@ re_string_reconstruct (pstr, idx, eflags) ...@@ -693,7 +675,7 @@ re_string_reconstruct (pstr, idx, eflags)
693 for (wcs_idx = 0; wcs_idx < pstr->valid_len; ++wcs_idx) 675 for (wcs_idx = 0; wcs_idx < pstr->valid_len; ++wcs_idx)
694 pstr->wcs[wcs_idx] = WEOF; 676 pstr->wcs[wcs_idx] = WEOF;
695 if (pstr->mbs_allocated) 677 if (pstr->mbs_allocated)
696 memset (pstr->mbs, 255, pstr->valid_len); 678 memset (pstr->mbs, -1, pstr->valid_len);
697 } 679 }
698 pstr->valid_raw_len = pstr->valid_len; 680 pstr->valid_raw_len = pstr->valid_len;
699 pstr->tip_context = ((BE (pstr->word_ops_used != 0, 0) 681 pstr->tip_context = ((BE (pstr->word_ops_used != 0, 0)
...@@ -728,7 +710,7 @@ re_string_reconstruct (pstr, idx, eflags) ...@@ -728,7 +710,7 @@ re_string_reconstruct (pstr, idx, eflags)
728 { 710 {
729 if (pstr->icase) 711 if (pstr->icase)
730 { 712 {
731 int ret = build_wcs_upper_buffer (pstr); 713 reg_errcode_t ret = build_wcs_upper_buffer (pstr);
732 if (BE (ret != REG_NOERROR, 0)) 714 if (BE (ret != REG_NOERROR, 0))
733 return ret; 715 return ret;
734 } 716 }
...@@ -752,11 +734,11 @@ re_string_reconstruct (pstr, idx, eflags) ...@@ -752,11 +734,11 @@ re_string_reconstruct (pstr, idx, eflags)
752 } 734 }
753 735
754 static unsigned char 736 static unsigned char
755 re_string_peek_byte_case (pstr, idx) 737 internal_function __attribute ((pure))
756 const re_string_t *pstr; 738 re_string_peek_byte_case (const re_string_t *pstr, Idx idx)
757 int idx;
758 { 739 {
759 int ch, off; 740 int ch;
741 Idx off;
760 742
761 /* Handle the common (easiest) cases first. */ 743 /* Handle the common (easiest) cases first. */
762 if (BE (!pstr->mbs_allocated, 1)) 744 if (BE (!pstr->mbs_allocated, 1))
...@@ -789,8 +771,8 @@ re_string_peek_byte_case (pstr, idx) ...@@ -789,8 +771,8 @@ re_string_peek_byte_case (pstr, idx)
789 } 771 }
790 772
791 static unsigned char 773 static unsigned char
792 re_string_fetch_byte_case (pstr) 774 internal_function __attribute ((pure))
793 re_string_t *pstr; 775 re_string_fetch_byte_case (re_string_t *pstr)
794 { 776 {
795 if (BE (!pstr->mbs_allocated, 1)) 777 if (BE (!pstr->mbs_allocated, 1))
796 return re_string_fetch_byte (pstr); 778 return re_string_fetch_byte (pstr);
...@@ -798,7 +780,8 @@ re_string_fetch_byte_case (pstr) ...@@ -798,7 +780,8 @@ re_string_fetch_byte_case (pstr)
798 #ifdef RE_ENABLE_I18N 780 #ifdef RE_ENABLE_I18N
799 if (pstr->offsets_needed) 781 if (pstr->offsets_needed)
800 { 782 {
801 int off, ch; 783 Idx off;
784 int ch;
802 785
803 /* For tr_TR.UTF-8 [[:islower:]] there is 786 /* For tr_TR.UTF-8 [[:islower:]] there is
804 [[: CAPITAL LETTER I WITH DOT lower:]] in mbs. Skip 787 [[: CAPITAL LETTER I WITH DOT lower:]] in mbs. Skip
...@@ -826,8 +809,8 @@ re_string_fetch_byte_case (pstr) ...@@ -826,8 +809,8 @@ re_string_fetch_byte_case (pstr)
826 } 809 }
827 810
828 static void 811 static void
829 re_string_destruct (pstr) 812 internal_function
830 re_string_t *pstr; 813 re_string_destruct (re_string_t *pstr)
831 { 814 {
832 #ifdef RE_ENABLE_I18N 815 #ifdef RE_ENABLE_I18N
833 re_free (pstr->wcs); 816 re_free (pstr->wcs);
...@@ -840,12 +823,11 @@ re_string_destruct (pstr) ...@@ -840,12 +823,11 @@ re_string_destruct (pstr)
840 /* Return the context at IDX in INPUT. */ 823 /* Return the context at IDX in INPUT. */
841 824
842 static unsigned int 825 static unsigned int
843 re_string_context_at (input, idx, eflags) 826 internal_function
844 const re_string_t *input; 827 re_string_context_at (const re_string_t *input, Idx idx, int eflags)
845 int idx, eflags;
846 { 828 {
847 int c; 829 int c;
848 if (BE (idx < 0, 0)) 830 if (BE (! REG_VALID_INDEX (idx), 0))
849 /* In this case, we use the value stored in input->tip_context, 831 /* In this case, we use the value stored in input->tip_context,
850 since we can't know the character in input->mbs[-1] here. */ 832 since we can't know the character in input->mbs[-1] here. */
851 return input->tip_context; 833 return input->tip_context;
...@@ -856,15 +838,15 @@ re_string_context_at (input, idx, eflags) ...@@ -856,15 +838,15 @@ re_string_context_at (input, idx, eflags)
856 if (input->mb_cur_max > 1) 838 if (input->mb_cur_max > 1)
857 { 839 {
858 wint_t wc; 840 wint_t wc;
859 int wc_idx = idx; 841 Idx wc_idx = idx;
860 while(input->wcs[wc_idx] == WEOF) 842 while(input->wcs[wc_idx] == WEOF)
861 { 843 {
862 #ifdef DEBUG 844 #ifdef DEBUG
863 /* It must not happen. */ 845 /* It must not happen. */
864 assert (wc_idx >= 0); 846 assert (REG_VALID_INDEX (wc_idx));
865 #endif 847 #endif
866 --wc_idx; 848 --wc_idx;
867 if (wc_idx < 0) 849 if (! REG_VALID_INDEX (wc_idx))
868 return input->tip_context; 850 return input->tip_context;
869 } 851 }
870 wc = input->wcs[wc_idx]; 852 wc = input->wcs[wc_idx];
...@@ -886,26 +868,24 @@ re_string_context_at (input, idx, eflags) ...@@ -886,26 +868,24 @@ re_string_context_at (input, idx, eflags)
886 /* Functions for set operation. */ 868 /* Functions for set operation. */
887 869
888 static reg_errcode_t 870 static reg_errcode_t
889 re_node_set_alloc (set, size) 871 internal_function
890 re_node_set *set; 872 re_node_set_alloc (re_node_set *set, Idx size)
891 int size;
892 { 873 {
893 set->alloc = size; 874 set->alloc = size;
894 set->nelem = 0; 875 set->nelem = 0;
895 set->elems = re_malloc (int, size); 876 set->elems = re_xmalloc (Idx, size);
896 if (BE (set->elems == NULL, 0)) 877 if (BE (set->elems == NULL, 0))
897 return REG_ESPACE; 878 return REG_ESPACE;
898 return REG_NOERROR; 879 return REG_NOERROR;
899 } 880 }
900 881
901 static reg_errcode_t 882 static reg_errcode_t
902 re_node_set_init_1 (set, elem) 883 internal_function
903 re_node_set *set; 884 re_node_set_init_1 (re_node_set *set, Idx elem)
904 int elem;
905 { 885 {
906 set->alloc = 1; 886 set->alloc = 1;
907 set->nelem = 1; 887 set->nelem = 1;
908 set->elems = re_malloc (int, 1); 888 set->elems = re_malloc (Idx, 1);
909 if (BE (set->elems == NULL, 0)) 889 if (BE (set->elems == NULL, 0))
910 { 890 {
911 set->alloc = set->nelem = 0; 891 set->alloc = set->nelem = 0;
...@@ -916,12 +896,11 @@ re_node_set_init_1 (set, elem) ...@@ -916,12 +896,11 @@ re_node_set_init_1 (set, elem)
916 } 896 }
917 897
918 static reg_errcode_t 898 static reg_errcode_t
919 re_node_set_init_2 (set, elem1, elem2) 899 internal_function
920 re_node_set *set; 900 re_node_set_init_2 (re_node_set *set, Idx elem1, Idx elem2)
921 int elem1, elem2;
922 { 901 {
923 set->alloc = 2; 902 set->alloc = 2;
924 set->elems = re_malloc (int, 2); 903 set->elems = re_malloc (Idx, 2);
925 if (BE (set->elems == NULL, 0)) 904 if (BE (set->elems == NULL, 0))
926 return REG_ESPACE; 905 return REG_ESPACE;
927 if (elem1 == elem2) 906 if (elem1 == elem2)
...@@ -947,21 +926,20 @@ re_node_set_init_2 (set, elem1, elem2) ...@@ -947,21 +926,20 @@ re_node_set_init_2 (set, elem1, elem2)
947 } 926 }
948 927
949 static reg_errcode_t 928 static reg_errcode_t
950 re_node_set_init_copy (dest, src) 929 internal_function
951 re_node_set *dest; 930 re_node_set_init_copy (re_node_set *dest, const re_node_set *src)
952 const re_node_set *src;
953 { 931 {
954 dest->nelem = src->nelem; 932 dest->nelem = src->nelem;
955 if (src->nelem > 0) 933 if (src->nelem > 0)
956 { 934 {
957 dest->alloc = dest->nelem; 935 dest->alloc = dest->nelem;
958 dest->elems = re_malloc (int, dest->alloc); 936 dest->elems = re_malloc (Idx, dest->alloc);
959 if (BE (dest->elems == NULL, 0)) 937 if (BE (dest->elems == NULL, 0))
960 { 938 {
961 dest->alloc = dest->nelem = 0; 939 dest->alloc = dest->nelem = 0;
962 return REG_ESPACE; 940 return REG_ESPACE;
963 } 941 }
964 memcpy (dest->elems, src->elems, src->nelem * sizeof (int)); 942 memcpy (dest->elems, src->elems, src->nelem * sizeof dest->elems[0]);
965 } 943 }
966 else 944 else
967 re_node_set_init_empty (dest); 945 re_node_set_init_empty (dest);
...@@ -973,11 +951,11 @@ re_node_set_init_copy (dest, src) ...@@ -973,11 +951,11 @@ re_node_set_init_copy (dest, src)
973 Note: We assume dest->elems is NULL, when dest->alloc is 0. */ 951 Note: We assume dest->elems is NULL, when dest->alloc is 0. */
974 952
975 static reg_errcode_t 953 static reg_errcode_t
976 re_node_set_add_intersect (dest, src1, src2) 954 internal_function
977 re_node_set *dest; 955 re_node_set_add_intersect (re_node_set *dest, const re_node_set *src1,
978 const re_node_set *src1, *src2; 956 const re_node_set *src2)
979 { 957 {
980 int i1, i2, is, id, delta, sbase; 958 Idx i1, i2, is, id, delta, sbase;
981 if (src1->nelem == 0 || src2->nelem == 0) 959 if (src1->nelem == 0 || src2->nelem == 0)
982 return REG_NOERROR; 960 return REG_NOERROR;
983 961
...@@ -985,8 +963,13 @@ re_node_set_add_intersect (dest, src1, src2) ...@@ -985,8 +963,13 @@ re_node_set_add_intersect (dest, src1, src2)
985 conservative estimate. */ 963 conservative estimate. */
986 if (src1->nelem + src2->nelem + dest->nelem > dest->alloc) 964 if (src1->nelem + src2->nelem + dest->nelem > dest->alloc)
987 { 965 {
988 int new_alloc = src1->nelem + src2->nelem + dest->alloc; 966 Idx new_alloc = src1->nelem + src2->nelem + dest->alloc;
989 int *new_elems = re_realloc (dest->elems, int, new_alloc); 967 Idx *new_elems;
968 if (sizeof (Idx) < 3
969 && (new_alloc < dest->alloc
970 || ((Idx) (src1->nelem + src2->nelem) < src1->nelem)))
971 return REG_ESPACE;
972 new_elems = re_xrealloc (dest->elems, Idx, new_alloc);
990 if (BE (new_elems == NULL, 0)) 973 if (BE (new_elems == NULL, 0))
991 return REG_ESPACE; 974 return REG_ESPACE;
992 dest->elems = new_elems; 975 dest->elems = new_elems;
...@@ -1004,25 +987,25 @@ re_node_set_add_intersect (dest, src1, src2) ...@@ -1004,25 +987,25 @@ re_node_set_add_intersect (dest, src1, src2)
1004 if (src1->elems[i1] == src2->elems[i2]) 987 if (src1->elems[i1] == src2->elems[i2])
1005 { 988 {
1006 /* Try to find the item in DEST. Maybe we could binary search? */ 989 /* Try to find the item in DEST. Maybe we could binary search? */
1007 while (id >= 0 && dest->elems[id] > src1->elems[i1]) 990 while (REG_VALID_INDEX (id) && dest->elems[id] > src1->elems[i1])
1008 --id; 991 --id;
1009 992
1010 if (id < 0 || dest->elems[id] != src1->elems[i1]) 993 if (! REG_VALID_INDEX (id) || dest->elems[id] != src1->elems[i1])
1011 dest->elems[--sbase] = src1->elems[i1]; 994 dest->elems[--sbase] = src1->elems[i1];
1012 995
1013 if (--i1 < 0 || --i2 < 0) 996 if (! REG_VALID_INDEX (--i1) || ! REG_VALID_INDEX (--i2))
1014 break; 997 break;
1015 } 998 }
1016 999
1017 /* Lower the highest of the two items. */ 1000 /* Lower the highest of the two items. */
1018 else if (src1->elems[i1] < src2->elems[i2]) 1001 else if (src1->elems[i1] < src2->elems[i2])
1019 { 1002 {
1020 if (--i2 < 0) 1003 if (! REG_VALID_INDEX (--i2))
1021 break; 1004 break;
1022 } 1005 }
1023 else 1006 else
1024 { 1007 {
1025 if (--i1 < 0) 1008 if (! REG_VALID_INDEX (--i1))
1026 break; 1009 break;
1027 } 1010 }
1028 } 1011 }
...@@ -1035,7 +1018,7 @@ re_node_set_add_intersect (dest, src1, src2) ...@@ -1035,7 +1018,7 @@ re_node_set_add_intersect (dest, src1, src2)
1035 DEST elements are already in place; this is more or 1018 DEST elements are already in place; this is more or
1036 less the same loop that is in re_node_set_merge. */ 1019 less the same loop that is in re_node_set_merge. */
1037 dest->nelem += delta; 1020 dest->nelem += delta;
1038 if (delta > 0 && id >= 0) 1021 if (delta > 0 && REG_VALID_INDEX (id))
1039 for (;;) 1022 for (;;)
1040 { 1023 {
1041 if (dest->elems[is] > dest->elems[id]) 1024 if (dest->elems[is] > dest->elems[id])
...@@ -1049,13 +1032,13 @@ re_node_set_add_intersect (dest, src1, src2) ...@@ -1049,13 +1032,13 @@ re_node_set_add_intersect (dest, src1, src2)
1049 { 1032 {
1050 /* Slide from the bottom. */ 1033 /* Slide from the bottom. */
1051 dest->elems[id + delta] = dest->elems[id]; 1034 dest->elems[id + delta] = dest->elems[id];
1052 if (--id < 0) 1035 if (! REG_VALID_INDEX (--id))
1053 break; 1036 break;
1054 } 1037 }
1055 } 1038 }
1056 1039
1057 /* Copy remaining SRC elements. */ 1040 /* Copy remaining SRC elements. */
1058 memcpy (dest->elems, dest->elems + sbase, delta * sizeof (int)); 1041 memcpy (dest->elems, dest->elems + sbase, delta * sizeof dest->elems[0]);
1059 1042
1060 return REG_NOERROR; 1043 return REG_NOERROR;
1061 } 1044 }
...@@ -1064,15 +1047,17 @@ re_node_set_add_intersect (dest, src1, src2) ...@@ -1064,15 +1047,17 @@ re_node_set_add_intersect (dest, src1, src2)
1064 DEST. Return value indicate the error code or REG_NOERROR if succeeded. */ 1047 DEST. Return value indicate the error code or REG_NOERROR if succeeded. */
1065 1048
1066 static reg_errcode_t 1049 static reg_errcode_t
1067 re_node_set_init_union (dest, src1, src2) 1050 internal_function
1068 re_node_set *dest; 1051 re_node_set_init_union (re_node_set *dest, const re_node_set *src1,
1069 const re_node_set *src1, *src2; 1052 const re_node_set *src2)
1070 { 1053 {
1071 int i1, i2, id; 1054 Idx i1, i2, id;
1072 if (src1 != NULL && src1->nelem > 0 && src2 != NULL && src2->nelem > 0) 1055 if (src1 != NULL && src1->nelem > 0 && src2 != NULL && src2->nelem > 0)
1073 { 1056 {
1074 dest->alloc = src1->nelem + src2->nelem; 1057 dest->alloc = src1->nelem + src2->nelem;
1075 dest->elems = re_malloc (int, dest->alloc); 1058 if (sizeof (Idx) < 2 && dest->alloc < src1->nelem)
1059 return REG_ESPACE;
1060 dest->elems = re_xmalloc (Idx, dest->alloc);
1076 if (BE (dest->elems == NULL, 0)) 1061 if (BE (dest->elems == NULL, 0))
1077 return REG_ESPACE; 1062 return REG_ESPACE;
1078 } 1063 }
...@@ -1100,13 +1085,13 @@ re_node_set_init_union (dest, src1, src2) ...@@ -1100,13 +1085,13 @@ re_node_set_init_union (dest, src1, src2)
1100 if (i1 < src1->nelem) 1085 if (i1 < src1->nelem)
1101 { 1086 {
1102 memcpy (dest->elems + id, src1->elems + i1, 1087 memcpy (dest->elems + id, src1->elems + i1,
1103 (src1->nelem - i1) * sizeof (int)); 1088 (src1->nelem - i1) * sizeof dest->elems[0]);
1104 id += src1->nelem - i1; 1089 id += src1->nelem - i1;
1105 } 1090 }
1106 else if (i2 < src2->nelem) 1091 else if (i2 < src2->nelem)
1107 { 1092 {
1108 memcpy (dest->elems + id, src2->elems + i2, 1093 memcpy (dest->elems + id, src2->elems + i2,
1109 (src2->nelem - i2) * sizeof (int)); 1094 (src2->nelem - i2) * sizeof dest->elems[0]);
1110 id += src2->nelem - i2; 1095 id += src2->nelem - i2;
1111 } 1096 }
1112 dest->nelem = id; 1097 dest->nelem = id;
...@@ -1117,17 +1102,23 @@ re_node_set_init_union (dest, src1, src2) ...@@ -1117,17 +1102,23 @@ re_node_set_init_union (dest, src1, src2)
1117 DEST. Return value indicate the error code or REG_NOERROR if succeeded. */ 1102 DEST. Return value indicate the error code or REG_NOERROR if succeeded. */
1118 1103
1119 static reg_errcode_t 1104 static reg_errcode_t
1120 re_node_set_merge (dest, src) 1105 internal_function
1121 re_node_set *dest; 1106 re_node_set_merge (re_node_set *dest, const re_node_set *src)
1122 const re_node_set *src;
1123 { 1107 {
1124 int is, id, sbase, delta; 1108 Idx is, id, sbase, delta;
1125 if (src == NULL || src->nelem == 0) 1109 if (src == NULL || src->nelem == 0)
1126 return REG_NOERROR; 1110 return REG_NOERROR;
1111 if (sizeof (Idx) < 3
1112 && ((Idx) (2 * src->nelem) < src->nelem
1113 || (Idx) (2 * src->nelem + dest->nelem) < dest->nelem))
1114 return REG_ESPACE;
1127 if (dest->alloc < 2 * src->nelem + dest->nelem) 1115 if (dest->alloc < 2 * src->nelem + dest->nelem)
1128 { 1116 {
1129 int new_alloc = 2 * (src->nelem + dest->alloc); 1117 Idx new_alloc = src->nelem + dest->alloc;
1130 int *new_buffer = re_realloc (dest->elems, int, new_alloc); 1118 Idx *new_buffer;
1119 if (sizeof (Idx) < 4 && new_alloc < dest->alloc)
1120 return REG_ESPACE;
1121 new_buffer = re_x2realloc (dest->elems, Idx, &new_alloc);
1131 if (BE (new_buffer == NULL, 0)) 1122 if (BE (new_buffer == NULL, 0))
1132 return REG_ESPACE; 1123 return REG_ESPACE;
1133 dest->elems = new_buffer; 1124 dest->elems = new_buffer;
...@@ -1137,14 +1128,15 @@ re_node_set_merge (dest, src) ...@@ -1137,14 +1128,15 @@ re_node_set_merge (dest, src)
1137 if (BE (dest->nelem == 0, 0)) 1128 if (BE (dest->nelem == 0, 0))
1138 { 1129 {
1139 dest->nelem = src->nelem; 1130 dest->nelem = src->nelem;
1140 memcpy (dest->elems, src->elems, src->nelem * sizeof (int)); 1131 memcpy (dest->elems, src->elems, src->nelem * sizeof dest->elems[0]);
1141 return REG_NOERROR; 1132 return REG_NOERROR;
1142 } 1133 }
1143 1134
1144 /* Copy into the top of DEST the items of SRC that are not 1135 /* Copy into the top of DEST the items of SRC that are not
1145 found in DEST. Maybe we could binary search in DEST? */ 1136 found in DEST. Maybe we could binary search in DEST? */
1146 for (sbase = dest->nelem + 2 * src->nelem, 1137 for (sbase = dest->nelem + 2 * src->nelem,
1147 is = src->nelem - 1, id = dest->nelem - 1; is >= 0 && id >= 0; ) 1138 is = src->nelem - 1, id = dest->nelem - 1;
1139 REG_VALID_INDEX (is) && REG_VALID_INDEX (id); )
1148 { 1140 {
1149 if (dest->elems[id] == src->elems[is]) 1141 if (dest->elems[id] == src->elems[is])
1150 is--, id--; 1142 is--, id--;
...@@ -1154,11 +1146,12 @@ re_node_set_merge (dest, src) ...@@ -1154,11 +1146,12 @@ re_node_set_merge (dest, src)
1154 --id; 1146 --id;
1155 } 1147 }
1156 1148
1157 if (is >= 0) 1149 if (REG_VALID_INDEX (is))
1158 { 1150 {
1159 /* If DEST is exhausted, the remaining items of SRC must be unique. */ 1151 /* If DEST is exhausted, the remaining items of SRC must be unique. */
1160 sbase -= is + 1; 1152 sbase -= is + 1;
1161 memcpy (dest->elems + sbase, src->elems, (is + 1) * sizeof (int)); 1153 memcpy (dest->elems + sbase, src->elems,
1154 (is + 1) * sizeof dest->elems[0]);
1162 } 1155 }
1163 1156
1164 id = dest->nelem - 1; 1157 id = dest->nelem - 1;
...@@ -1183,11 +1176,11 @@ re_node_set_merge (dest, src) ...@@ -1183,11 +1176,11 @@ re_node_set_merge (dest, src)
1183 { 1176 {
1184 /* Slide from the bottom. */ 1177 /* Slide from the bottom. */
1185 dest->elems[id + delta] = dest->elems[id]; 1178 dest->elems[id + delta] = dest->elems[id];
1186 if (--id < 0) 1179 if (! REG_VALID_INDEX (--id))
1187 { 1180 {
1188 /* Copy remaining SRC elements. */ 1181 /* Copy remaining SRC elements. */
1189 memcpy (dest->elems, dest->elems + sbase, 1182 memcpy (dest->elems, dest->elems + sbase,
1190 delta * sizeof (int)); 1183 delta * sizeof dest->elems[0]);
1191 break; 1184 break;
1192 } 1185 }
1193 } 1186 }
...@@ -1198,40 +1191,32 @@ re_node_set_merge (dest, src) ...@@ -1198,40 +1191,32 @@ re_node_set_merge (dest, src)
1198 1191
1199 /* Insert the new element ELEM to the re_node_set* SET. 1192 /* Insert the new element ELEM to the re_node_set* SET.
1200 SET should not already have ELEM. 1193 SET should not already have ELEM.
1201 return -1 if an error is occured, return 1 otherwise. */ 1194 Return true if successful. */
1202 1195
1203 static int 1196 static bool
1204 re_node_set_insert (set, elem) 1197 internal_function
1205 re_node_set *set; 1198 re_node_set_insert (re_node_set *set, Idx elem)
1206 int elem;
1207 { 1199 {
1208 int idx; 1200 Idx idx;
1209 /* In case the set is empty. */ 1201 /* In case the set is empty. */
1210 if (set->alloc == 0) 1202 if (set->alloc == 0)
1211 { 1203 return re_node_set_init_1 (set, elem) == REG_NOERROR;
1212 if (BE (re_node_set_init_1 (set, elem) == REG_NOERROR, 1))
1213 return 1;
1214 else
1215 return -1;
1216 }
1217 1204
1218 if (BE (set->nelem, 0) == 0) 1205 if (BE (set->nelem, 0) == 0)
1219 { 1206 {
1220 /* We already guaranteed above that set->alloc != 0. */ 1207 /* We already guaranteed above that set->alloc != 0. */
1221 set->elems[0] = elem; 1208 set->elems[0] = elem;
1222 ++set->nelem; 1209 ++set->nelem;
1223 return 1; 1210 return true;
1224 } 1211 }
1225 1212
1226 /* Realloc if we need. */ 1213 /* Realloc if we need. */
1227 if (set->alloc == set->nelem) 1214 if (set->alloc == set->nelem)
1228 { 1215 {
1229 int *new_array; 1216 Idx *new_elems = re_x2realloc (set->elems, Idx, &set->alloc);
1230 set->alloc = set->alloc * 2; 1217 if (BE (new_elems == NULL, 0))
1231 new_array = re_realloc (set->elems, int, set->alloc); 1218 return false;
1232 if (BE (new_array == NULL, 0)) 1219 set->elems = new_elems;
1233 return -1;
1234 set->elems = new_array;
1235 } 1220 }
1236 1221
1237 /* Move the elements which follows the new element. Test the 1222 /* Move the elements which follows the new element. Test the
...@@ -1251,59 +1236,56 @@ re_node_set_insert (set, elem) ...@@ -1251,59 +1236,56 @@ re_node_set_insert (set, elem)
1251 /* Insert the new element. */ 1236 /* Insert the new element. */
1252 set->elems[idx] = elem; 1237 set->elems[idx] = elem;
1253 ++set->nelem; 1238 ++set->nelem;
1254 return 1; 1239 return true;
1255 } 1240 }
1256 1241
1257 /* Insert the new element ELEM to the re_node_set* SET. 1242 /* Insert the new element ELEM to the re_node_set* SET.
1258 SET should not already have any element greater than or equal to ELEM. 1243 SET should not already have any element greater than or equal to ELEM.
1259 Return -1 if an error is occured, return 1 otherwise. */ 1244 Return true if successful. */
1260 1245
1261 static int 1246 static bool
1262 re_node_set_insert_last (set, elem) 1247 internal_function
1263 re_node_set *set; 1248 re_node_set_insert_last (re_node_set *set, Idx elem)
1264 int elem;
1265 { 1249 {
1266 /* Realloc if we need. */ 1250 /* Realloc if we need. */
1267 if (set->alloc == set->nelem) 1251 if (set->alloc == set->nelem)
1268 { 1252 {
1269 int *new_array; 1253 Idx *new_elems;
1270 set->alloc = (set->alloc + 1) * 2; 1254 new_elems = re_x2realloc (set->elems, Idx, &set->alloc);
1271 new_array = re_realloc (set->elems, int, set->alloc); 1255 if (BE (new_elems == NULL, 0))
1272 if (BE (new_array == NULL, 0)) 1256 return false;
1273 return -1; 1257 set->elems = new_elems;
1274 set->elems = new_array;
1275 } 1258 }
1276 1259
1277 /* Insert the new element. */ 1260 /* Insert the new element. */
1278 set->elems[set->nelem++] = elem; 1261 set->elems[set->nelem++] = elem;
1279 return 1; 1262 return true;
1280 } 1263 }
1281 1264
1282 /* Compare two node sets SET1 and SET2. 1265 /* Compare two node sets SET1 and SET2.
1283 return 1 if SET1 and SET2 are equivalent, return 0 otherwise. */ 1266 Return true if SET1 and SET2 are equivalent. */
1284 1267
1285 static int 1268 static bool
1286 re_node_set_compare (set1, set2) 1269 internal_function __attribute ((pure))
1287 const re_node_set *set1, *set2; 1270 re_node_set_compare (const re_node_set *set1, const re_node_set *set2)
1288 { 1271 {
1289 int i; 1272 Idx i;
1290 if (set1 == NULL || set2 == NULL || set1->nelem != set2->nelem) 1273 if (set1 == NULL || set2 == NULL || set1->nelem != set2->nelem)
1291 return 0; 1274 return false;
1292 for (i = set1->nelem ; --i >= 0 ; ) 1275 for (i = set1->nelem ; REG_VALID_INDEX (--i) ; )
1293 if (set1->elems[i] != set2->elems[i]) 1276 if (set1->elems[i] != set2->elems[i])
1294 return 0; 1277 return false;
1295 return 1; 1278 return true;
1296 } 1279 }
1297 1280
1298 /* Return (idx + 1) if SET contains the element ELEM, return 0 otherwise. */ 1281 /* Return (idx + 1) if SET contains the element ELEM, return 0 otherwise. */
1299 1282
1300 static int 1283 static Idx
1301 re_node_set_contains (set, elem) 1284 internal_function __attribute ((pure))
1302 const re_node_set *set; 1285 re_node_set_contains (const re_node_set *set, Idx elem)
1303 int elem;
1304 { 1286 {
1305 unsigned int idx, right, mid; 1287 __re_size_t idx, right, mid;
1306 if (set->nelem <= 0) 1288 if (! REG_VALID_NONZERO_INDEX (set->nelem))
1307 return 0; 1289 return 0;
1308 1290
1309 /* Binary search the element. */ 1291 /* Binary search the element. */
...@@ -1321,9 +1303,8 @@ re_node_set_contains (set, elem) ...@@ -1321,9 +1303,8 @@ re_node_set_contains (set, elem)
1321 } 1303 }
1322 1304
1323 static void 1305 static void
1324 re_node_set_remove_at (set, idx) 1306 internal_function
1325 re_node_set *set; 1307 re_node_set_remove_at (re_node_set *set, Idx idx)
1326 int idx;
1327 { 1308 {
1328 if (idx < 0 || idx >= set->nelem) 1309 if (idx < 0 || idx >= set->nelem)
1329 return; 1310 return;
...@@ -1334,32 +1315,31 @@ re_node_set_remove_at (set, idx) ...@@ -1334,32 +1315,31 @@ re_node_set_remove_at (set, idx)
1334 1315
1335 1316
1336 /* Add the token TOKEN to dfa->nodes, and return the index of the token. 1317 /* Add the token TOKEN to dfa->nodes, and return the index of the token.
1337 Or return -1, if an error will be occured. */ 1318 Or return REG_MISSING if an error occurred. */
1338 1319
1339 static int 1320 static Idx
1340 re_dfa_add_node (dfa, token) 1321 internal_function
1341 re_dfa_t *dfa; 1322 re_dfa_add_node (re_dfa_t *dfa, re_token_t token)
1342 re_token_t token;
1343 { 1323 {
1344 int type = token.type; 1324 int type = token.type;
1345 if (BE (dfa->nodes_len >= dfa->nodes_alloc, 0)) 1325 if (BE (dfa->nodes_len >= dfa->nodes_alloc, 0))
1346 { 1326 {
1347 int new_nodes_alloc = dfa->nodes_alloc * 2; 1327 Idx new_nodes_alloc = dfa->nodes_alloc;
1348 int *new_nexts, *new_indices; 1328 Idx *new_nexts, *new_indices;
1349 re_node_set *new_edests, *new_eclosures; 1329 re_node_set *new_edests, *new_eclosures;
1350 1330
1351 re_token_t *new_array = re_realloc (dfa->nodes, re_token_t, 1331 re_token_t *new_nodes = re_x2realloc (dfa->nodes, re_token_t,
1352 new_nodes_alloc); 1332 &new_nodes_alloc);
1353 if (BE (new_array == NULL, 0)) 1333 if (BE (new_nodes == NULL, 0))
1354 return -1; 1334 return REG_MISSING;
1355 dfa->nodes = new_array; 1335 dfa->nodes = new_nodes;
1356 new_nexts = re_realloc (dfa->nexts, int, new_nodes_alloc); 1336 new_nexts = re_realloc (dfa->nexts, Idx, new_nodes_alloc);
1357 new_indices = re_realloc (dfa->org_indices, int, new_nodes_alloc); 1337 new_indices = re_realloc (dfa->org_indices, Idx, new_nodes_alloc);
1358 new_edests = re_realloc (dfa->edests, re_node_set, new_nodes_alloc); 1338 new_edests = re_xrealloc (dfa->edests, re_node_set, new_nodes_alloc);
1359 new_eclosures = re_realloc (dfa->eclosures, re_node_set, new_nodes_alloc); 1339 new_eclosures = re_realloc (dfa->eclosures, re_node_set, new_nodes_alloc);
1360 if (BE (new_nexts == NULL || new_indices == NULL 1340 if (BE (new_nexts == NULL || new_indices == NULL
1361 || new_edests == NULL || new_eclosures == NULL, 0)) 1341 || new_edests == NULL || new_eclosures == NULL, 0))
1362 return -1; 1342 return REG_MISSING;
1363 dfa->nexts = new_nexts; 1343 dfa->nexts = new_nexts;
1364 dfa->org_indices = new_indices; 1344 dfa->org_indices = new_indices;
1365 dfa->edests = new_edests; 1345 dfa->edests = new_edests;
...@@ -1372,19 +1352,18 @@ re_dfa_add_node (dfa, token) ...@@ -1372,19 +1352,18 @@ re_dfa_add_node (dfa, token)
1372 dfa->nodes[dfa->nodes_len].accept_mb = 1352 dfa->nodes[dfa->nodes_len].accept_mb =
1373 (type == OP_PERIOD && dfa->mb_cur_max > 1) || type == COMPLEX_BRACKET; 1353 (type == OP_PERIOD && dfa->mb_cur_max > 1) || type == COMPLEX_BRACKET;
1374 #endif 1354 #endif
1375 dfa->nexts[dfa->nodes_len] = -1; 1355 dfa->nexts[dfa->nodes_len] = REG_MISSING;
1376 re_node_set_init_empty (dfa->edests + dfa->nodes_len); 1356 re_node_set_init_empty (dfa->edests + dfa->nodes_len);
1377 re_node_set_init_empty (dfa->eclosures + dfa->nodes_len); 1357 re_node_set_init_empty (dfa->eclosures + dfa->nodes_len);
1378 return dfa->nodes_len++; 1358 return dfa->nodes_len++;
1379 } 1359 }
1380 1360
1381 static unsigned int inline 1361 static inline re_hashval_t
1382 calc_state_hash (nodes, context) 1362 internal_function
1383 const re_node_set *nodes; 1363 calc_state_hash (const re_node_set *nodes, unsigned int context)
1384 unsigned int context;
1385 { 1364 {
1386 unsigned int hash = nodes->nelem + context; 1365 re_hashval_t hash = nodes->nelem + context;
1387 int i; 1366 Idx i;
1388 for (i = 0 ; i < nodes->nelem ; i++) 1367 for (i = 0 ; i < nodes->nelem ; i++)
1389 hash += nodes->elems[i]; 1368 hash += nodes->elems[i];
1390 return hash; 1369 return hash;
...@@ -1400,15 +1379,17 @@ calc_state_hash (nodes, context) ...@@ -1400,15 +1379,17 @@ calc_state_hash (nodes, context)
1400 optimization. */ 1379 optimization. */
1401 1380
1402 static re_dfastate_t* 1381 static re_dfastate_t*
1403 re_acquire_state (err, dfa, nodes) 1382 internal_function
1404 reg_errcode_t *err; 1383 re_acquire_state (reg_errcode_t *err, re_dfa_t *dfa, const re_node_set *nodes)
1405 re_dfa_t *dfa;
1406 const re_node_set *nodes;
1407 { 1384 {
1408 unsigned int hash; 1385 re_hashval_t hash;
1409 re_dfastate_t *new_state; 1386 re_dfastate_t *new_state;
1410 struct re_state_table_entry *spot; 1387 struct re_state_table_entry *spot;
1411 int i; 1388 Idx i;
1389 #ifdef lint
1390 /* Suppress bogus uninitialized-variable warnings. */
1391 *err = REG_NOERROR;
1392 #endif
1412 if (BE (nodes->nelem == 0, 0)) 1393 if (BE (nodes->nelem == 0, 0))
1413 { 1394 {
1414 *err = REG_NOERROR; 1395 *err = REG_NOERROR;
...@@ -1448,16 +1429,18 @@ re_acquire_state (err, dfa, nodes) ...@@ -1448,16 +1429,18 @@ re_acquire_state (err, dfa, nodes)
1448 optimization. */ 1429 optimization. */
1449 1430
1450 static re_dfastate_t* 1431 static re_dfastate_t*
1451 re_acquire_state_context (err, dfa, nodes, context) 1432 internal_function
1452 reg_errcode_t *err; 1433 re_acquire_state_context (reg_errcode_t *err, re_dfa_t *dfa,
1453 re_dfa_t *dfa; 1434 const re_node_set *nodes, unsigned int context)
1454 const re_node_set *nodes;
1455 unsigned int context;
1456 { 1435 {
1457 unsigned int hash; 1436 re_hashval_t hash;
1458 re_dfastate_t *new_state; 1437 re_dfastate_t *new_state;
1459 struct re_state_table_entry *spot; 1438 struct re_state_table_entry *spot;
1460 int i; 1439 Idx i;
1440 #ifdef lint
1441 /* Suppress bogus uninitialized-variable warnings. */
1442 *err = REG_NOERROR;
1443 #endif
1461 if (nodes->nelem == 0) 1444 if (nodes->nelem == 0)
1462 { 1445 {
1463 *err = REG_NOERROR; 1446 *err = REG_NOERROR;
...@@ -1490,14 +1473,12 @@ re_acquire_state_context (err, dfa, nodes, context) ...@@ -1490,14 +1473,12 @@ re_acquire_state_context (err, dfa, nodes, context)
1490 indicates the error code if failed. */ 1473 indicates the error code if failed. */
1491 1474
1492 static reg_errcode_t 1475 static reg_errcode_t
1493 register_state (dfa, newstate, hash) 1476 internal_function
1494 re_dfa_t *dfa; 1477 register_state (const re_dfa_t *dfa, re_dfastate_t *newstate, re_hashval_t hash)
1495 re_dfastate_t *newstate;
1496 unsigned int hash;
1497 { 1478 {
1498 struct re_state_table_entry *spot; 1479 struct re_state_table_entry *spot;
1499 reg_errcode_t err; 1480 reg_errcode_t err;
1500 int i; 1481 Idx i;
1501 1482
1502 newstate->hash = hash; 1483 newstate->hash = hash;
1503 err = re_node_set_alloc (&newstate->non_eps_nodes, newstate->nodes.nelem); 1484 err = re_node_set_alloc (&newstate->non_eps_nodes, newstate->nodes.nelem);
...@@ -1505,17 +1486,21 @@ register_state (dfa, newstate, hash) ...@@ -1505,17 +1486,21 @@ register_state (dfa, newstate, hash)
1505 return REG_ESPACE; 1486 return REG_ESPACE;
1506 for (i = 0; i < newstate->nodes.nelem; i++) 1487 for (i = 0; i < newstate->nodes.nelem; i++)
1507 { 1488 {
1508 int elem = newstate->nodes.elems[i]; 1489 Idx elem = newstate->nodes.elems[i];
1509 if (!IS_EPSILON_NODE (dfa->nodes[elem].type)) 1490 if (!IS_EPSILON_NODE (dfa->nodes[elem].type))
1510 re_node_set_insert_last (&newstate->non_eps_nodes, elem); 1491 {
1492 bool ok = re_node_set_insert_last (&newstate->non_eps_nodes, elem);
1493 if (BE (! ok, 0))
1494 return REG_ESPACE;
1495 }
1511 } 1496 }
1512 1497
1513 spot = dfa->state_table + (hash & dfa->state_hash_mask); 1498 spot = dfa->state_table + (hash & dfa->state_hash_mask);
1514 if (BE (spot->alloc <= spot->num, 0)) 1499 if (BE (spot->alloc <= spot->num, 0))
1515 { 1500 {
1516 int new_alloc = 2 * spot->num + 2; 1501 Idx new_alloc = spot->num;
1517 re_dfastate_t **new_array = re_realloc (spot->array, re_dfastate_t *, 1502 re_dfastate_t **new_array = re_x2realloc (spot->array, re_dfastate_t *,
1518 new_alloc); 1503 &new_alloc);
1519 if (BE (new_array == NULL, 0)) 1504 if (BE (new_array == NULL, 0))
1520 return REG_ESPACE; 1505 return REG_ESPACE;
1521 spot->array = new_array; 1506 spot->array = new_array;
...@@ -1529,16 +1514,15 @@ register_state (dfa, newstate, hash) ...@@ -1529,16 +1514,15 @@ register_state (dfa, newstate, hash)
1529 Return the new state if succeeded, otherwise return NULL. */ 1514 Return the new state if succeeded, otherwise return NULL. */
1530 1515
1531 static re_dfastate_t * 1516 static re_dfastate_t *
1532 create_ci_newstate (dfa, nodes, hash) 1517 internal_function
1533 re_dfa_t *dfa; 1518 create_ci_newstate (const re_dfa_t *dfa, const re_node_set *nodes,
1534 const re_node_set *nodes; 1519 re_hashval_t hash)
1535 unsigned int hash;
1536 { 1520 {
1537 int i; 1521 Idx i;
1538 reg_errcode_t err; 1522 reg_errcode_t err;
1539 re_dfastate_t *newstate; 1523 re_dfastate_t *newstate;
1540 1524
1541 newstate = (re_dfastate_t *) calloc (sizeof (re_dfastate_t), 1); 1525 newstate = re_calloc (re_dfastate_t, 1);
1542 if (BE (newstate == NULL, 0)) 1526 if (BE (newstate == NULL, 0))
1543 return NULL; 1527 return NULL;
1544 err = re_node_set_init_copy (&newstate->nodes, nodes); 1528 err = re_node_set_init_copy (&newstate->nodes, nodes);
...@@ -1580,16 +1564,15 @@ create_ci_newstate (dfa, nodes, hash) ...@@ -1580,16 +1564,15 @@ create_ci_newstate (dfa, nodes, hash)
1580 Return the new state if succeeded, otherwise return NULL. */ 1564 Return the new state if succeeded, otherwise return NULL. */
1581 1565
1582 static re_dfastate_t * 1566 static re_dfastate_t *
1583 create_cd_newstate (dfa, nodes, context, hash) 1567 internal_function
1584 re_dfa_t *dfa; 1568 create_cd_newstate (const re_dfa_t *dfa, const re_node_set *nodes,
1585 const re_node_set *nodes; 1569 unsigned int context, re_hashval_t hash)
1586 unsigned int context, hash;
1587 { 1570 {
1588 int i, nctx_nodes = 0; 1571 Idx i, nctx_nodes = 0;
1589 reg_errcode_t err; 1572 reg_errcode_t err;
1590 re_dfastate_t *newstate; 1573 re_dfastate_t *newstate;
1591 1574
1592 newstate = (re_dfastate_t *) calloc (sizeof (re_dfastate_t), 1); 1575 newstate = re_calloc (re_dfastate_t, 1);
1593 if (BE (newstate == NULL, 0)) 1576 if (BE (newstate == NULL, 0))
1594 return NULL; 1577 return NULL;
1595 err = re_node_set_init_copy (&newstate->nodes, nodes); 1578 err = re_node_set_init_copy (&newstate->nodes, nodes);
...@@ -1656,8 +1639,8 @@ create_cd_newstate (dfa, nodes, context, hash) ...@@ -1656,8 +1639,8 @@ create_cd_newstate (dfa, nodes, context, hash)
1656 } 1639 }
1657 1640
1658 static void 1641 static void
1659 free_state (state) 1642 internal_function
1660 re_dfastate_t *state; 1643 free_state (re_dfastate_t *state)
1661 { 1644 {
1662 re_node_set_free (&state->non_eps_nodes); 1645 re_node_set_free (&state->non_eps_nodes);
1663 re_node_set_free (&state->inveclosure); 1646 re_node_set_free (&state->inveclosure);
......
...@@ -22,10 +22,15 @@ ...@@ -22,10 +22,15 @@
22 22
23 #include <assert.h> 23 #include <assert.h>
24 #include <ctype.h> 24 #include <ctype.h>
25 #include <stdbool.h>
25 #include <stdio.h> 26 #include <stdio.h>
26 #include <stdlib.h> 27 #include <stdlib.h>
27 #include <string.h> 28 #include <string.h>
28 29
30 #ifndef _LIBC
31 # include "strcase.h"
32 #endif
33
29 #if defined HAVE_LANGINFO_H || defined HAVE_LANGINFO_CODESET || defined _LIBC 34 #if defined HAVE_LANGINFO_H || defined HAVE_LANGINFO_CODESET || defined _LIBC
30 # include <langinfo.h> 35 # include <langinfo.h>
31 #endif 36 #endif
...@@ -87,11 +92,8 @@ ...@@ -87,11 +92,8 @@
87 # define BE(expr, val) __builtin_expect (expr, val) 92 # define BE(expr, val) __builtin_expect (expr, val)
88 #else 93 #else
89 # define BE(expr, val) (expr) 94 # define BE(expr, val) (expr)
90 # define inline
91 #endif 95 #endif
92 96
93 /* Number of bits in a byte. */
94 #define BYTE_BITS 8
95 /* Number of single byte character. */ 97 /* Number of single byte character. */
96 #define SBC_MAX 256 98 #define SBC_MAX 256
97 99
...@@ -114,7 +116,7 @@ ...@@ -114,7 +116,7 @@
114 # define attribute_hidden 116 # define attribute_hidden
115 #endif /* not _LIBC */ 117 #endif /* not _LIBC */
116 118
117 #ifdef __GNUC__ 119 #if __GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
118 # define __attribute(arg) __attribute__ (arg) 120 # define __attribute(arg) __attribute__ (arg)
119 #else 121 #else
120 # define __attribute(arg) 122 # define __attribute(arg)
...@@ -123,26 +125,73 @@ ...@@ -123,26 +125,73 @@
123 extern const char __re_error_msgid[] attribute_hidden; 125 extern const char __re_error_msgid[] attribute_hidden;
124 extern const size_t __re_error_msgid_idx[] attribute_hidden; 126 extern const size_t __re_error_msgid_idx[] attribute_hidden;
125 127
126 /* Number of bits in an unsinged int. */ 128 typedef __re_idx_t Idx;
127 #define UINT_BITS (sizeof (unsigned int) * BYTE_BITS) 129
128 /* Number of unsigned int in an bit_set. */ 130 /* Special return value for failure to match. */
129 #define BITSET_UINTS ((SBC_MAX + UINT_BITS - 1) / UINT_BITS) 131 #define REG_MISSING ((Idx) -1)
130 typedef unsigned int bitset[BITSET_UINTS]; 132
131 typedef unsigned int *re_bitset_ptr_t; 133 /* Special return value for internal error. */
132 typedef const unsigned int *re_const_bitset_ptr_t; 134 #define REG_ERROR ((Idx) -2)
133 135
134 #define bitset_set(set,i) (set[i / UINT_BITS] |= 1 << i % UINT_BITS) 136 /* Test whether N is a valid index, and is not one of the above. */
135 #define bitset_clear(set,i) (set[i / UINT_BITS] &= ~(1 << i % UINT_BITS)) 137 #ifdef _REGEX_LARGE_OFFSETS
136 #define bitset_contain(set,i) (set[i / UINT_BITS] & (1 << i % UINT_BITS)) 138 # define REG_VALID_INDEX(n) ((Idx) (n) < REG_ERROR)
137 #define bitset_empty(set) memset (set, 0, sizeof (unsigned int) * BITSET_UINTS) 139 #else
138 #define bitset_set_all(set) \ 140 # define REG_VALID_INDEX(n) (0 <= (n))
139 memset (set, 255, sizeof (unsigned int) * BITSET_UINTS) 141 #endif
140 #define bitset_copy(dest,src) \ 142
141 memcpy (dest, src, sizeof (unsigned int) * BITSET_UINTS) 143 /* Test whether N is a valid nonzero index. */
142 static inline void bitset_not (bitset set); 144 #ifdef _REGEX_LARGE_OFFSETS
143 static inline void bitset_merge (bitset dest, const bitset src); 145 # define REG_VALID_NONZERO_INDEX(n) ((Idx) ((n) - 1) < (Idx) (REG_ERROR - 1))
144 static inline void bitset_not_merge (bitset dest, const bitset src); 146 #else
145 static inline void bitset_mask (bitset dest, const bitset src); 147 # define REG_VALID_NONZERO_INDEX(n) (0 < (n))
148 #endif
149
150 /* A hash value, suitable for computing hash tables. */
151 typedef __re_size_t re_hashval_t;
152
153 /* An integer used to represent a set of bits. It must be unsigned,
154 and must be at least as wide as unsigned int. */
155 typedef unsigned long int bitset_word;
156
157 /* Maximum value of a bitset word. It must be useful in preprocessor
158 contexts, and must be consistent with bitset_word. */
159 #define BITSET_WORD_MAX ULONG_MAX
160
161 /* Number of bits in a bitset word. Avoid greater-than-32-bit
162 integers and unconditional shifts by more than 31 bits, as they're
163 not portable. */
164 #if BITSET_WORD_MAX == 0xffffffff
165 # define BITSET_WORD_BITS 32
166 #elif BITSET_WORD_MAX >> 31 >> 5 == 1
167 # define BITSET_WORD_BITS 36
168 #elif BITSET_WORD_MAX >> 31 >> 16 == 1
169 # define BITSET_WORD_BITS 48
170 #elif BITSET_WORD_MAX >> 31 >> 28 == 1
171 # define BITSET_WORD_BITS 60
172 #elif BITSET_WORD_MAX >> 31 >> 31 >> 1 == 1
173 # define BITSET_WORD_BITS 64
174 #elif BITSET_WORD_MAX >> 31 >> 31 >> 9 == 1
175 # define BITSET_WORD_BITS 72
176 #elif BITSET_WORD_MAX >> 31 >> 31 >> 31 >> 31 >> 3 == 1
177 # define BITSET_WORD_BITS 128
178 #elif BITSET_WORD_MAX >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 7 == 1
179 # define BITSET_WORD_BITS 256
180 #elif BITSET_WORD_MAX >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 7 > 1
181 # define BITSET_WORD_BITS 257 /* any value > SBC_MAX will do here */
182 # if BITSET_WORD_BITS <= SBC_MAX
183 # error "Invalid SBC_MAX"
184 # endif
185 #else
186 # error "Add case for new bitset_word size"
187 #endif
188
189 /* Number of bitset words in a bitset. */
190 #define BITSET_WORDS ((SBC_MAX + BITSET_WORD_BITS - 1) / BITSET_WORD_BITS)
191
192 typedef bitset_word bitset[BITSET_WORDS];
193 typedef bitset_word *re_bitset_ptr_t;
194 typedef const bitset_word *re_const_bitset_ptr_t;
146 195
147 #define PREV_WORD_CONSTRAINT 0x0001 196 #define PREV_WORD_CONSTRAINT 0x0001
148 #define PREV_NOTWORD_CONSTRAINT 0x0002 197 #define PREV_NOTWORD_CONSTRAINT 0x0002
...@@ -171,9 +220,9 @@ typedef enum ...@@ -171,9 +220,9 @@ typedef enum
171 220
172 typedef struct 221 typedef struct
173 { 222 {
174 int alloc; 223 Idx alloc;
175 int nelem; 224 Idx nelem;
176 int *elems; 225 Idx *elems;
177 } re_node_set; 226 } re_node_set;
178 227
179 typedef enum 228 typedef enum
...@@ -259,19 +308,19 @@ typedef struct ...@@ -259,19 +308,19 @@ typedef struct
259 unsigned int non_match : 1; 308 unsigned int non_match : 1;
260 309
261 /* # of multibyte characters. */ 310 /* # of multibyte characters. */
262 int nmbchars; 311 Idx nmbchars;
263 312
264 /* # of collating symbols. */ 313 /* # of collating symbols. */
265 int ncoll_syms; 314 Idx ncoll_syms;
266 315
267 /* # of equivalence classes. */ 316 /* # of equivalence classes. */
268 int nequiv_classes; 317 Idx nequiv_classes;
269 318
270 /* # of range expressions. */ 319 /* # of range expressions. */
271 int nranges; 320 Idx nranges;
272 321
273 /* # of character classes. */ 322 /* # of character classes. */
274 int nchar_classes; 323 Idx nchar_classes;
275 } re_charset_t; 324 } re_charset_t;
276 #endif /* RE_ENABLE_I18N */ 325 #endif /* RE_ENABLE_I18N */
277 326
...@@ -284,7 +333,7 @@ typedef struct ...@@ -284,7 +333,7 @@ typedef struct
284 #ifdef RE_ENABLE_I18N 333 #ifdef RE_ENABLE_I18N
285 re_charset_t *mbcset; /* for COMPLEX_BRACKET */ 334 re_charset_t *mbcset; /* for COMPLEX_BRACKET */
286 #endif /* RE_ENABLE_I18N */ 335 #endif /* RE_ENABLE_I18N */
287 int idx; /* for BACK_REF */ 336 Idx idx; /* for BACK_REF */
288 re_context_type ctx_type; /* for ANCHOR */ 337 re_context_type ctx_type; /* for ANCHOR */
289 } opr; 338 } opr;
290 #if __GNUC__ >= 2 339 #if __GNUC__ >= 2
...@@ -318,40 +367,40 @@ struct re_string_t ...@@ -318,40 +367,40 @@ struct re_string_t
318 #ifdef RE_ENABLE_I18N 367 #ifdef RE_ENABLE_I18N
319 /* Store the wide character string which is corresponding to MBS. */ 368 /* Store the wide character string which is corresponding to MBS. */
320 wint_t *wcs; 369 wint_t *wcs;
321 int *offsets; 370 Idx *offsets;
322 mbstate_t cur_state; 371 mbstate_t cur_state;
323 #endif 372 #endif
324 /* Index in RAW_MBS. Each character mbs[i] corresponds to 373 /* Index in RAW_MBS. Each character mbs[i] corresponds to
325 raw_mbs[raw_mbs_idx + i]. */ 374 raw_mbs[raw_mbs_idx + i]. */
326 int raw_mbs_idx; 375 Idx raw_mbs_idx;
327 /* The length of the valid characters in the buffers. */ 376 /* The length of the valid characters in the buffers. */
328 int valid_len; 377 Idx valid_len;
329 /* The corresponding number of bytes in raw_mbs array. */ 378 /* The corresponding number of bytes in raw_mbs array. */
330 int valid_raw_len; 379 Idx valid_raw_len;
331 /* The length of the buffers MBS and WCS. */ 380 /* The length of the buffers MBS and WCS. */
332 int bufs_len; 381 Idx bufs_len;
333 /* The index in MBS, which is updated by re_string_fetch_byte. */ 382 /* The index in MBS, which is updated by re_string_fetch_byte. */
334 int cur_idx; 383 Idx cur_idx;
335 /* length of RAW_MBS array. */ 384 /* length of RAW_MBS array. */
336 int raw_len; 385 Idx raw_len;
337 /* This is RAW_LEN - RAW_MBS_IDX + VALID_LEN - VALID_RAW_LEN. */ 386 /* This is RAW_LEN - RAW_MBS_IDX + VALID_LEN - VALID_RAW_LEN. */
338 int len; 387 Idx len;
339 /* End of the buffer may be shorter than its length in the cases such 388 /* End of the buffer may be shorter than its length in the cases such
340 as re_match_2, re_search_2. Then, we use STOP for end of the buffer 389 as re_match_2, re_search_2. Then, we use STOP for end of the buffer
341 instead of LEN. */ 390 instead of LEN. */
342 int raw_stop; 391 Idx raw_stop;
343 /* This is RAW_STOP - RAW_MBS_IDX adjusted through OFFSETS. */ 392 /* This is RAW_STOP - RAW_MBS_IDX adjusted through OFFSETS. */
344 int stop; 393 Idx stop;
345 394
346 /* The context of mbs[0]. We store the context independently, since 395 /* The context of mbs[0]. We store the context independently, since
347 the context of mbs[0] may be different from raw_mbs[0], which is 396 the context of mbs[0] may be different from raw_mbs[0], which is
348 the beginning of the input string. */ 397 the beginning of the input string. */
349 unsigned int tip_context; 398 unsigned int tip_context;
350 /* The translation passed as a part of an argument of re_compile_pattern. */ 399 /* The translation passed as a part of an argument of re_compile_pattern. */
351 unsigned RE_TRANSLATE_TYPE trans; 400 unsigned REG_TRANSLATE_TYPE trans;
352 /* Copy of re_dfa_t's word_char. */ 401 /* Copy of re_dfa_t's word_char. */
353 re_const_bitset_ptr_t word_char; 402 re_const_bitset_ptr_t word_char;
354 /* 1 if REG_ICASE. */ 403 /* true if REG_ICASE. */
355 unsigned char icase; 404 unsigned char icase;
356 unsigned char is_utf8; 405 unsigned char is_utf8;
357 unsigned char map_notascii; 406 unsigned char map_notascii;
...@@ -375,45 +424,20 @@ typedef struct re_dfa_t re_dfa_t; ...@@ -375,45 +424,20 @@ typedef struct re_dfa_t re_dfa_t;
375 # endif 424 # endif
376 #endif 425 #endif
377 426
378 #ifndef RE_NO_INTERNAL_PROTOTYPES
379 static reg_errcode_t re_string_allocate (re_string_t *pstr, const char *str,
380 int len, int init_len,
381 RE_TRANSLATE_TYPE trans, int icase,
382 const re_dfa_t *dfa)
383 internal_function;
384 static reg_errcode_t re_string_construct (re_string_t *pstr, const char *str,
385 int len, RE_TRANSLATE_TYPE trans,
386 int icase, const re_dfa_t *dfa)
387 internal_function;
388 static reg_errcode_t re_string_reconstruct (re_string_t *pstr, int idx,
389 int eflags) internal_function;
390 static reg_errcode_t re_string_realloc_buffers (re_string_t *pstr, 427 static reg_errcode_t re_string_realloc_buffers (re_string_t *pstr,
391 int new_buf_len) 428 Idx new_buf_len)
392 internal_function; 429 internal_function;
393 # ifdef RE_ENABLE_I18N 430 #ifdef RE_ENABLE_I18N
394 static void build_wcs_buffer (re_string_t *pstr) internal_function; 431 static void build_wcs_buffer (re_string_t *pstr) internal_function;
395 static int build_wcs_upper_buffer (re_string_t *pstr) internal_function; 432 static reg_errcode_t build_wcs_upper_buffer (re_string_t *pstr)
396 # endif /* RE_ENABLE_I18N */ 433 internal_function;
434 #endif /* RE_ENABLE_I18N */
397 static void build_upper_buffer (re_string_t *pstr) internal_function; 435 static void build_upper_buffer (re_string_t *pstr) internal_function;
398 static void re_string_translate_buffer (re_string_t *pstr) internal_function; 436 static void re_string_translate_buffer (re_string_t *pstr) internal_function;
399 static void re_string_destruct (re_string_t *pstr) internal_function; 437 static unsigned int re_string_context_at (const re_string_t *input,
400 # ifdef RE_ENABLE_I18N 438 Idx idx, int eflags)
401 static int re_string_elem_size_at (const re_string_t *pstr, int idx)
402 internal_function __attribute ((pure)); 439 internal_function __attribute ((pure));
403 static inline int re_string_char_size_at (const re_string_t *pstr, int idx) 440
404 internal_function __attribute ((pure));
405 static inline wint_t re_string_wchar_at (const re_string_t *pstr, int idx)
406 internal_function __attribute ((pure));
407 # endif /* RE_ENABLE_I18N */
408 static unsigned int re_string_context_at (const re_string_t *input, int idx,
409 int eflags)
410 internal_function __attribute ((pure));
411 static unsigned char re_string_peek_byte_case (const re_string_t *pstr,
412 int idx)
413 internal_function __attribute ((pure));
414 static unsigned char re_string_fetch_byte_case (re_string_t *pstr)
415 internal_function __attribute ((pure));
416 #endif
417 #define re_string_peek_byte(pstr, offset) \ 441 #define re_string_peek_byte(pstr, offset) \
418 ((pstr)->mbs[(pstr)->cur_idx + offset]) 442 ((pstr)->mbs[(pstr)->cur_idx + offset])
419 #define re_string_fetch_byte(pstr) \ 443 #define re_string_fetch_byte(pstr) \
...@@ -431,10 +455,86 @@ static unsigned char re_string_fetch_byte_case (re_string_t *pstr) ...@@ -431,10 +455,86 @@ static unsigned char re_string_fetch_byte_case (re_string_t *pstr)
431 #define re_string_skip_bytes(pstr,idx) ((pstr)->cur_idx += (idx)) 455 #define re_string_skip_bytes(pstr,idx) ((pstr)->cur_idx += (idx))
432 #define re_string_set_index(pstr,idx) ((pstr)->cur_idx = (idx)) 456 #define re_string_set_index(pstr,idx) ((pstr)->cur_idx = (idx))
433 457
458 #include <alloca.h>
459
460 #ifndef _LIBC
461 # if HAVE_ALLOCA
462 /* The OS usually guarantees only one guard page at the bottom of the stack,
463 and a page size can be as small as 4096 bytes. So we cannot safely
464 allocate anything larger than 4096 bytes. Also care for the possibility
465 of a few compiler-allocated temporary stack slots. */
466 # define __libc_use_alloca(n) ((n) < 4032)
467 # else
468 /* alloca is implemented with malloc, so just use malloc. */
469 # define __libc_use_alloca(n) 0
470 # endif
471 #endif
472
434 #define re_malloc(t,n) ((t *) malloc ((n) * sizeof (t))) 473 #define re_malloc(t,n) ((t *) malloc ((n) * sizeof (t)))
474 #define re_xmalloc(t,n) ((t *) re_xnmalloc (n, sizeof (t)))
475 #define re_calloc(t,n) ((t *) calloc (n, sizeof (t)))
435 #define re_realloc(p,t,n) ((t *) realloc (p, (n) * sizeof (t))) 476 #define re_realloc(p,t,n) ((t *) realloc (p, (n) * sizeof (t)))
477 #define re_xrealloc(p,t,n) ((t *) re_xnrealloc (p, n, sizeof (t)))
478 #define re_x2realloc(p,t,pn) ((t *) re_x2nrealloc (p, pn, sizeof (t)))
436 #define re_free(p) free (p) 479 #define re_free(p) free (p)
437 480
481 #ifndef SIZE_MAX
482 # define SIZE_MAX ((size_t) -1)
483 #endif
484
485 /* Return true if an array of N objects, each of size S, cannot exist
486 due to size arithmetic overflow. S must be nonzero. */
487 static inline bool
488 re_alloc_oversized (size_t n, size_t s)
489 {
490 return BE (SIZE_MAX / s < n, 0);
491 }
492
493 /* Return true if an array of (2 * N + 1) objects, each of size S,
494 cannot exist due to size arithmetic overflow. S must be nonzero. */
495 static inline bool
496 re_x2alloc_oversized (size_t n, size_t s)
497 {
498 return BE ((SIZE_MAX / s - 1) / 2 < n, 0);
499 }
500
501 /* Allocate an array of N objects, each with S bytes of memory,
502 dynamically, with error checking. S must be nonzero. */
503 static inline void *
504 re_xnmalloc (size_t n, size_t s)
505 {
506 return re_alloc_oversized (n, s) ? NULL : malloc (n * s);
507 }
508
509 /* Change the size of an allocated block of memory P to an array of N
510 objects each of S bytes, with error checking. S must be nonzero. */
511 static inline void *
512 re_xnrealloc (void *p, size_t n, size_t s)
513 {
514 return re_alloc_oversized (n, s) ? NULL : realloc (p, n * s);
515 }
516
517 /* Reallocate a block of memory P to an array of (2 * (*PN) + 1)
518 objects each of S bytes, with error checking. S must be nonzero.
519 If the allocation is successful, set *PN to the new allocation
520 count and return the resulting pointer. Otherwise, return
521 NULL. */
522 static inline void *
523 re_x2nrealloc (void *p, size_t *pn, size_t s)
524 {
525 if (re_x2alloc_oversized (*pn, s))
526 return NULL;
527 else
528 {
529 /* Add 1 in case *PN is zero. */
530 size_t n1 = 2 * *pn + 1;
531 p = realloc (p, n1 * s);
532 if (BE (p != NULL, 1))
533 *pn = n1;
534 return p;
535 }
536 }
537
438 struct bin_tree_t 538 struct bin_tree_t
439 { 539 {
440 struct bin_tree_t *parent; 540 struct bin_tree_t *parent;
...@@ -447,7 +547,7 @@ struct bin_tree_t ...@@ -447,7 +547,7 @@ struct bin_tree_t
447 547
448 /* `node_idx' is the index in dfa->nodes, if `type' == 0. 548 /* `node_idx' is the index in dfa->nodes, if `type' == 0.
449 Otherwise `type' indicate the type of this node. */ 549 Otherwise `type' indicate the type of this node. */
450 int node_idx; 550 Idx node_idx;
451 }; 551 };
452 typedef struct bin_tree_t bin_tree_t; 552 typedef struct bin_tree_t bin_tree_t;
453 553
...@@ -491,7 +591,7 @@ typedef struct bin_tree_storage_t bin_tree_storage_t; ...@@ -491,7 +591,7 @@ typedef struct bin_tree_storage_t bin_tree_storage_t;
491 591
492 struct re_dfastate_t 592 struct re_dfastate_t
493 { 593 {
494 unsigned int hash; 594 re_hashval_t hash;
495 re_node_set nodes; 595 re_node_set nodes;
496 re_node_set non_eps_nodes; 596 re_node_set non_eps_nodes;
497 re_node_set inveclosure; 597 re_node_set inveclosure;
...@@ -511,8 +611,8 @@ typedef struct re_dfastate_t re_dfastate_t; ...@@ -511,8 +611,8 @@ typedef struct re_dfastate_t re_dfastate_t;
511 611
512 struct re_state_table_entry 612 struct re_state_table_entry
513 { 613 {
514 int num; 614 Idx num;
515 int alloc; 615 Idx alloc;
516 re_dfastate_t **array; 616 re_dfastate_t **array;
517 }; 617 };
518 618
...@@ -520,8 +620,8 @@ struct re_state_table_entry ...@@ -520,8 +620,8 @@ struct re_state_table_entry
520 620
521 typedef struct 621 typedef struct
522 { 622 {
523 int next_idx; 623 Idx next_idx;
524 int alloc; 624 Idx alloc;
525 re_dfastate_t **array; 625 re_dfastate_t **array;
526 } state_array_t; 626 } state_array_t;
527 627
...@@ -529,8 +629,8 @@ typedef struct ...@@ -529,8 +629,8 @@ typedef struct
529 629
530 typedef struct 630 typedef struct
531 { 631 {
532 int node; 632 Idx node;
533 int str_idx; /* The position NODE match at. */ 633 Idx str_idx; /* The position NODE match at. */
534 state_array_t path; 634 state_array_t path;
535 } re_sub_match_last_t; 635 } re_sub_match_last_t;
536 636
...@@ -540,21 +640,20 @@ typedef struct ...@@ -540,21 +640,20 @@ typedef struct
540 640
541 typedef struct 641 typedef struct
542 { 642 {
543 int str_idx; 643 Idx str_idx;
544 int node; 644 Idx node;
545 int next_last_offset;
546 state_array_t *path; 645 state_array_t *path;
547 int alasts; /* Allocation size of LASTS. */ 646 Idx alasts; /* Allocation size of LASTS. */
548 int nlasts; /* The number of LASTS. */ 647 Idx nlasts; /* The number of LASTS. */
549 re_sub_match_last_t **lasts; 648 re_sub_match_last_t **lasts;
550 } re_sub_match_top_t; 649 } re_sub_match_top_t;
551 650
552 struct re_backref_cache_entry 651 struct re_backref_cache_entry
553 { 652 {
554 int node; 653 Idx node;
555 int str_idx; 654 Idx str_idx;
556 int subexp_from; 655 Idx subexp_from;
557 int subexp_to; 656 Idx subexp_to;
558 char more; 657 char more;
559 char unused; 658 char unused;
560 unsigned short int eps_reachable_subexps_map; 659 unsigned short int eps_reachable_subexps_map;
...@@ -572,18 +671,18 @@ typedef struct ...@@ -572,18 +671,18 @@ typedef struct
572 /* EFLAGS of the argument of regexec. */ 671 /* EFLAGS of the argument of regexec. */
573 int eflags; 672 int eflags;
574 /* Where the matching ends. */ 673 /* Where the matching ends. */
575 int match_last; 674 Idx match_last;
576 int last_node; 675 Idx last_node;
577 /* The state log used by the matcher. */ 676 /* The state log used by the matcher. */
578 re_dfastate_t **state_log; 677 re_dfastate_t **state_log;
579 int state_log_top; 678 Idx state_log_top;
580 /* Back reference cache. */ 679 /* Back reference cache. */
581 int nbkref_ents; 680 Idx nbkref_ents;
582 int abkref_ents; 681 Idx abkref_ents;
583 struct re_backref_cache_entry *bkref_ents; 682 struct re_backref_cache_entry *bkref_ents;
584 int max_mb_elem_len; 683 int max_mb_elem_len;
585 int nsub_tops; 684 Idx nsub_tops;
586 int asub_tops; 685 Idx asub_tops;
587 re_sub_match_top_t **sub_tops; 686 re_sub_match_top_t **sub_tops;
588 } re_match_context_t; 687 } re_match_context_t;
589 688
...@@ -591,33 +690,33 @@ typedef struct ...@@ -591,33 +690,33 @@ typedef struct
591 { 690 {
592 re_dfastate_t **sifted_states; 691 re_dfastate_t **sifted_states;
593 re_dfastate_t **limited_states; 692 re_dfastate_t **limited_states;
594 int last_node; 693 Idx last_node;
595 int last_str_idx; 694 Idx last_str_idx;
596 re_node_set limits; 695 re_node_set limits;
597 } re_sift_context_t; 696 } re_sift_context_t;
598 697
599 struct re_fail_stack_ent_t 698 struct re_fail_stack_ent_t
600 { 699 {
601 int idx; 700 Idx idx;
602 int node; 701 Idx node;
603 regmatch_t *regs; 702 regmatch_t *regs;
604 re_node_set eps_via_nodes; 703 re_node_set eps_via_nodes;
605 }; 704 };
606 705
607 struct re_fail_stack_t 706 struct re_fail_stack_t
608 { 707 {
609 int num; 708 Idx num;
610 int alloc; 709 Idx alloc;
611 struct re_fail_stack_ent_t *stack; 710 struct re_fail_stack_ent_t *stack;
612 }; 711 };
613 712
614 struct re_dfa_t 713 struct re_dfa_t
615 { 714 {
616 re_token_t *nodes; 715 re_token_t *nodes;
617 int nodes_alloc; 716 Idx nodes_alloc;
618 int nodes_len; 717 Idx nodes_len;
619 int *nexts; 718 Idx *nexts;
620 int *org_indices; 719 Idx *org_indices;
621 re_node_set *edests; 720 re_node_set *edests;
622 re_node_set *eclosures; 721 re_node_set *eclosures;
623 re_node_set *inveclosures; 722 re_node_set *inveclosures;
...@@ -632,14 +731,13 @@ struct re_dfa_t ...@@ -632,14 +731,13 @@ struct re_dfa_t
632 int str_tree_storage_idx; 731 int str_tree_storage_idx;
633 732
634 /* number of subexpressions `re_nsub' is in regex_t. */ 733 /* number of subexpressions `re_nsub' is in regex_t. */
635 unsigned int state_hash_mask; 734 re_hashval_t state_hash_mask;
636 int states_alloc; 735 Idx init_node;
637 int init_node; 736 Idx nbackref; /* The number of backreference in this dfa. */
638 int nbackref; /* The number of backreference in this dfa. */
639 737
640 /* Bitmap expressing which backreference is used. */ 738 /* Bitmap expressing which backreference is used. */
641 unsigned int used_bkref_map; 739 bitset_word used_bkref_map;
642 unsigned int completed_bkref_map; 740 bitset_word completed_bkref_map;
643 741
644 unsigned int has_plural_match : 1; 742 unsigned int has_plural_match : 1;
645 /* If this dfa has "multibyte node", which is a backreference or 743 /* If this dfa has "multibyte node", which is a backreference or
...@@ -652,51 +750,20 @@ struct re_dfa_t ...@@ -652,51 +750,20 @@ struct re_dfa_t
652 int mb_cur_max; 750 int mb_cur_max;
653 bitset word_char; 751 bitset word_char;
654 reg_syntax_t syntax; 752 reg_syntax_t syntax;
655 int *subexp_map; 753 Idx *subexp_map;
656 #ifdef DEBUG 754 #ifdef DEBUG
657 char* re_str; 755 char* re_str;
658 #endif 756 #endif
659 __libc_lock_define (, lock) 757 __libc_lock_define (, lock)
660 }; 758 };
661 759
662 #ifndef RE_NO_INTERNAL_PROTOTYPES
663 static reg_errcode_t re_node_set_alloc (re_node_set *set, int size) internal_function;
664 static reg_errcode_t re_node_set_init_1 (re_node_set *set, int elem) internal_function;
665 static reg_errcode_t re_node_set_init_2 (re_node_set *set, int elem1,
666 int elem2) internal_function;
667 #define re_node_set_init_empty(set) memset (set, '\0', sizeof (re_node_set)) 760 #define re_node_set_init_empty(set) memset (set, '\0', sizeof (re_node_set))
668 static reg_errcode_t re_node_set_init_copy (re_node_set *dest,
669 const re_node_set *src) internal_function;
670 static reg_errcode_t re_node_set_add_intersect (re_node_set *dest,
671 const re_node_set *src1,
672 const re_node_set *src2) internal_function;
673 static reg_errcode_t re_node_set_init_union (re_node_set *dest,
674 const re_node_set *src1,
675 const re_node_set *src2) internal_function;
676 static reg_errcode_t re_node_set_merge (re_node_set *dest,
677 const re_node_set *src) internal_function;
678 static int re_node_set_insert (re_node_set *set, int elem) internal_function;
679 static int re_node_set_insert_last (re_node_set *set,
680 int elem) internal_function;
681 static int re_node_set_compare (const re_node_set *set1,
682 const re_node_set *set2)
683 internal_function __attribute ((pure));
684 static int re_node_set_contains (const re_node_set *set, int elem)
685 internal_function __attribute ((pure));
686 static void re_node_set_remove_at (re_node_set *set, int idx) internal_function;
687 #define re_node_set_remove(set,id) \ 761 #define re_node_set_remove(set,id) \
688 (re_node_set_remove_at (set, re_node_set_contains (set, id) - 1)) 762 (re_node_set_remove_at (set, re_node_set_contains (set, id) - 1))
689 #define re_node_set_empty(p) ((p)->nelem = 0) 763 #define re_node_set_empty(p) ((p)->nelem = 0)
690 #define re_node_set_free(set) re_free ((set)->elems) 764 #define re_node_set_free(set) re_free ((set)->elems)
691 static int re_dfa_add_node (re_dfa_t *dfa, re_token_t token) internal_function; 765
692 static re_dfastate_t *re_acquire_state (reg_errcode_t *err, re_dfa_t *dfa,
693 const re_node_set *nodes) internal_function;
694 static re_dfastate_t *re_acquire_state_context (reg_errcode_t *err,
695 re_dfa_t *dfa,
696 const re_node_set *nodes,
697 unsigned int context) internal_function;
698 static void free_state (re_dfastate_t *state) internal_function; 766 static void free_state (re_dfastate_t *state) internal_function;
699 #endif
700 767
701 768
702 typedef enum 769 typedef enum
...@@ -721,43 +788,79 @@ typedef struct ...@@ -721,43 +788,79 @@ typedef struct
721 788
722 789
723 /* Inline functions for bitset operation. */ 790 /* Inline functions for bitset operation. */
791
724 static inline void 792 static inline void
725 bitset_not (bitset set) 793 bitset_set (bitset set, Idx i)
726 { 794 {
727 int bitset_i; 795 set[i / BITSET_WORD_BITS] |= (bitset_word) 1 << i % BITSET_WORD_BITS;
728 for (bitset_i = 0; bitset_i < BITSET_UINTS; ++bitset_i)
729 set[bitset_i] = ~set[bitset_i];
730 } 796 }
731 797
732 static inline void 798 static inline void
733 bitset_merge (bitset dest, const bitset src) 799 bitset_clear (bitset set, Idx i)
800 {
801 set[i / BITSET_WORD_BITS] &= ~ ((bitset_word) 1 << i % BITSET_WORD_BITS);
802 }
803
804 static inline bool
805 bitset_contain (const bitset set, Idx i)
806 {
807 return (set[i / BITSET_WORD_BITS] >> i % BITSET_WORD_BITS) & 1;
808 }
809
810 static inline void
811 bitset_empty (bitset set)
734 { 812 {
735 int bitset_i; 813 memset (set, 0, sizeof (bitset));
736 for (bitset_i = 0; bitset_i < BITSET_UINTS; ++bitset_i)
737 dest[bitset_i] |= src[bitset_i];
738 } 814 }
739 815
740 static inline void 816 static inline void
741 bitset_not_merge (bitset dest, const bitset src) 817 bitset_set_all (bitset set)
818 {
819 memset (set, -1, sizeof (bitset_word) * (SBC_MAX / BITSET_WORD_BITS));
820 if (SBC_MAX % BITSET_WORD_BITS != 0)
821 set[BITSET_WORDS - 1] =
822 ((bitset_word) 1 << SBC_MAX % BITSET_WORD_BITS) - 1;
823 }
824
825 static inline void
826 bitset_copy (bitset dest, const bitset src)
827 {
828 memcpy (dest, src, sizeof (bitset));
829 }
830
831 static inline void
832 bitset_not (bitset set)
833 {
834 int i;
835 for (i = 0; i < SBC_MAX / BITSET_WORD_BITS; ++i)
836 set[i] = ~set[i];
837 if (SBC_MAX % BITSET_WORD_BITS != 0)
838 set[BITSET_WORDS - 1] =
839 ((((bitset_word) 1 << SBC_MAX % BITSET_WORD_BITS) - 1)
840 & ~set[BITSET_WORDS - 1]);
841 }
842
843 static inline void
844 bitset_merge (bitset dest, const bitset src)
742 { 845 {
743 int i; 846 int i;
744 for (i = 0; i < BITSET_UINTS; ++i) 847 for (i = 0; i < BITSET_WORDS; ++i)
745 dest[i] |= ~src[i]; 848 dest[i] |= src[i];
746 } 849 }
747 850
748 static inline void 851 static inline void
749 bitset_mask (bitset dest, const bitset src) 852 bitset_mask (bitset dest, const bitset src)
750 { 853 {
751 int bitset_i; 854 int i;
752 for (bitset_i = 0; bitset_i < BITSET_UINTS; ++bitset_i) 855 for (i = 0; i < BITSET_WORDS; ++i)
753 dest[bitset_i] &= src[bitset_i]; 856 dest[i] &= src[i];
754 } 857 }
755 858
756 #if defined RE_ENABLE_I18N && !defined RE_NO_INTERNAL_PROTOTYPES 859 #if defined RE_ENABLE_I18N
757 /* Inline functions for re_string. */ 860 /* Inline functions for re_string. */
758 static inline int 861 static inline int
759 internal_function 862 internal_function __attribute ((pure))
760 re_string_char_size_at (const re_string_t *pstr, int idx) 863 re_string_char_size_at (const re_string_t *pstr, Idx idx)
761 { 864 {
762 int byte_idx; 865 int byte_idx;
763 if (pstr->mb_cur_max == 1) 866 if (pstr->mb_cur_max == 1)
...@@ -769,8 +872,8 @@ re_string_char_size_at (const re_string_t *pstr, int idx) ...@@ -769,8 +872,8 @@ re_string_char_size_at (const re_string_t *pstr, int idx)
769 } 872 }
770 873
771 static inline wint_t 874 static inline wint_t
772 internal_function 875 internal_function __attribute ((pure))
773 re_string_wchar_at (const re_string_t *pstr, int idx) 876 re_string_wchar_at (const re_string_t *pstr, Idx idx)
774 { 877 {
775 if (pstr->mb_cur_max == 1) 878 if (pstr->mb_cur_max == 1)
776 return (wint_t) pstr->mbs[idx]; 879 return (wint_t) pstr->mbs[idx];
...@@ -778,8 +881,8 @@ re_string_wchar_at (const re_string_t *pstr, int idx) ...@@ -778,8 +881,8 @@ re_string_wchar_at (const re_string_t *pstr, int idx)
778 } 881 }
779 882
780 static int 883 static int
781 internal_function 884 internal_function __attribute ((pure))
782 re_string_elem_size_at (const re_string_t *pstr, int idx) 885 re_string_elem_size_at (const re_string_t *pstr, Idx idx)
783 { 886 {
784 #ifdef _LIBC 887 #ifdef _LIBC
785 const unsigned char *p, *extra; 888 const unsigned char *p, *extra;
......
This diff could not be displayed because it is too large.
...@@ -25,119 +25,10 @@ ...@@ -25,119 +25,10 @@
25 #include "strcase.h" 25 #include "strcase.h"
26 26
27 #include <ctype.h> 27 #include <ctype.h>
28 #include <limits.h>
28 29
29 #if HAVE_MBRTOWC 30 #if HAVE_MBRTOWC
30 31 # include "mbuiter.h"
31 #include "strnlen1.h"
32
33 /* Like mbiter.h, except it doesn't look at the entire string. */
34
35 #include "mbchar.h"
36
37 #include <assert.h>
38 #include <stdbool.h>
39 #include <stdlib.h>
40 #include <wchar.h>
41 #include <wctype.h>
42
43 struct mbiter_multi
44 {
45 bool at_end; /* true if the end of the string has been reached */
46 bool in_shift; /* true if next byte may not be interpreted as ASCII */
47 mbstate_t state; /* if in_shift: current shift state */
48 bool next_done; /* true if mbi_avail has already filled the following */
49 struct mbchar cur; /* the current character:
50 const char *cur.ptr pointer to current character
51 The following are only valid after mbi_avail.
52 size_t cur.bytes number of bytes of current character
53 bool cur.wc_valid true if wc is a valid wide character
54 wchar_t cur.wc if wc_valid: the current character
55 */
56 };
57
58 static inline void
59 mbiter_multi_next (struct mbiter_multi *iter)
60 {
61 if (iter->next_done)
62 return;
63 if (iter->in_shift)
64 goto with_shift;
65 /* Handle most ASCII characters quickly, without calling mbrtowc(). */
66 if (is_basic (*iter->cur.ptr))
67 {
68 /* These characters are part of the basic character set. ISO C 99
69 guarantees that their wide character code is identical to their
70 char code. */
71 iter->cur.bytes = 1;
72 iter->cur.wc = *iter->cur.ptr;
73 iter->cur.wc_valid = true;
74 }
75 else
76 {
77 assert (mbsinit (&iter->state));
78 iter->in_shift = true;
79 with_shift:
80 iter->cur.bytes = mbrtowc (&iter->cur.wc, iter->cur.ptr,
81 strnlen1 (iter->cur.ptr, MB_CUR_MAX),
82 &iter->state);
83 if (iter->cur.bytes == (size_t) -1)
84 {
85 /* An invalid multibyte sequence was encountered. */
86 iter->cur.bytes = 1;
87 iter->cur.wc_valid = false;
88 /* Whether to set iter->in_shift = false and reset iter->state
89 or not is not very important; the string is bogus anyway. */
90 }
91 else if (iter->cur.bytes == (size_t) -2)
92 {
93 /* An incomplete multibyte character at the end. */
94 iter->cur.bytes = strlen (iter->cur.ptr) + 1;
95 iter->cur.wc_valid = false;
96 /* Whether to set iter->in_shift = false and reset iter->state
97 or not is not important; the string end is reached anyway. */
98 }
99 else
100 {
101 if (iter->cur.bytes == 0)
102 {
103 /* A null wide character was encountered. */
104 iter->cur.bytes = 1;
105 assert (*iter->cur.ptr == '\0');
106 assert (iter->cur.wc == 0);
107 }
108 iter->cur.wc_valid = true;
109
110 /* When in the initial state, we can go back treating ASCII
111 characters more quickly. */
112 if (mbsinit (&iter->state))
113 iter->in_shift = false;
114 }
115 }
116 iter->next_done = true;
117 }
118
119 static inline void
120 mbiter_multi_reloc (struct mbiter_multi *iter, ptrdiff_t ptrdiff)
121 {
122 iter->cur.ptr += ptrdiff;
123 }
124
125 /* Iteration macros. */
126 typedef struct mbiter_multi mbi_iterator_t;
127 #define mbi_init(iter, startptr) \
128 ((iter).cur.ptr = (startptr), (iter).at_end = false, \
129 (iter).in_shift = false, memset (&(iter).state, '\0', sizeof (mbstate_t)), \
130 (iter).next_done = false)
131 #define mbi_avail(iter) \
132 (!(iter).at_end && (mbiter_multi_next (&(iter)), true))
133 #define mbi_advance(iter) \
134 ((mb_isnul ((iter).cur) ? ((iter).at_end = true) : 0), \
135 (iter).cur.ptr += (iter).cur.bytes, (iter).next_done = false)
136
137 /* Access to the current character. */
138 #define mbi_cur(iter) (iter).cur
139 #define mbi_cur_ptr(iter) (iter).cur.ptr
140
141 #endif 32 #endif
142 33
143 #define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch)) 34 #define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
...@@ -159,59 +50,26 @@ strcasecmp (const char *s1, const char *s2) ...@@ -159,59 +50,26 @@ strcasecmp (const char *s1, const char *s2)
159 #if HAVE_MBRTOWC 50 #if HAVE_MBRTOWC
160 if (MB_CUR_MAX > 1) 51 if (MB_CUR_MAX > 1)
161 { 52 {
162 mbi_iterator_t iter1; 53 mbui_iterator_t iter1;
163 mbi_iterator_t iter2; 54 mbui_iterator_t iter2;
164 55
165 mbi_init (iter1, s1); 56 mbui_init (iter1, s1);
166 mbi_init (iter2, s2); 57 mbui_init (iter2, s2);
167 58
168 while (mbi_avail (iter1) && mbi_avail (iter2)) 59 while (mbui_avail (iter1) && mbui_avail (iter2))
169 { 60 {
170 /* Sort invalid characters after all valid ones. */ 61 int cmp = mb_casecmp (mbui_cur (iter1), mbui_cur (iter2));
171 if (!mbi_cur (iter1).wc_valid) 62
172 { 63 if (cmp != 0)
173 if (!mbi_cur (iter2).wc_valid) 64 return cmp;
174 { 65
175 /* Compare two invalid characters. */ 66 mbui_advance (iter1);
176 int cmp; 67 mbui_advance (iter2);
177
178 if (mbi_cur (iter1).bytes > mbi_cur (iter2).bytes)
179 return 1;
180 if (mbi_cur (iter1).bytes < mbi_cur (iter2).bytes)
181 return -1;
182 cmp = memcmp (mbi_cur_ptr (iter1), mbi_cur_ptr (iter2),
183 mbi_cur (iter1).bytes);
184 if (cmp != 0)
185 return cmp;
186 }
187 else
188 /* mbi_cur (iter1) invalid, mbi_cur (iter2) valid. */
189 return 1;
190 }
191 else
192 {
193 if (!mbi_cur (iter2).wc_valid)
194 /* mbi_cur (iter1) valid, mbi_cur (iter2) invalid. */
195 return -1;
196 else
197 {
198 /* Compare two valid characters. */
199 wchar_t c1 = towlower (mbi_cur (iter1).wc);
200 wchar_t c2 = towlower (mbi_cur (iter2).wc);
201
202 if (c1 > c2)
203 return 1;
204 if (c1 < c2)
205 return -1;
206 }
207 }
208 mbi_advance (iter1);
209 mbi_advance (iter2);
210 } 68 }
211 if (mbi_avail (iter1)) 69 if (mbui_avail (iter1))
212 /* s2 terminated before s1. */ 70 /* s2 terminated before s1. */
213 return 1; 71 return 1;
214 if (mbi_avail (iter2)) 72 if (mbui_avail (iter2))
215 /* s1 terminated before s2. */ 73 /* s1 terminated before s2. */
216 return -1; 74 return -1;
217 return 0; 75 return 0;
...@@ -236,6 +94,12 @@ strcasecmp (const char *s1, const char *s2) ...@@ -236,6 +94,12 @@ strcasecmp (const char *s1, const char *s2)
236 } 94 }
237 while (c1 == c2); 95 while (c1 == c2);
238 96
239 return c1 - c2; 97 if (UCHAR_MAX <= INT_MAX)
98 return c1 - c2;
99 else
100 /* On machines where 'char' and 'int' are types of the same size, the
101 difference of two 'unsigned char' values - including the sign bit -
102 doesn't fit in an 'int'. */
103 return (c1 > c2 ? 1 : c1 < c2 ? -1 : 0);
240 } 104 }
241 } 105 }
......
1 /* strncasecmp.c -- case insensitive string comparator 1 /* strncasecmp.c -- case insensitive string comparator
2 Copyright (C) 1998, 1999 Free Software Foundation, Inc. 2 Copyright (C) 1998, 1999, 2005 Free Software Foundation, Inc.
3 3
4 This program is free software; you can redistribute it and/or modify 4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by 5 it under the terms of the GNU General Public License as published by
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
15 along with this program; if not, write to the Free Software Foundation, 15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17 17
18 #if HAVE_CONFIG_H 18 #ifdef HAVE_CONFIG_H
19 # include <config.h> 19 # include <config.h>
20 #endif 20 #endif
21 21
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
23 #include "strcase.h" 23 #include "strcase.h"
24 24
25 #include <ctype.h> 25 #include <ctype.h>
26 #include <limits.h>
26 27
27 #define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch)) 28 #define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
28 29
...@@ -54,5 +55,11 @@ strncasecmp (const char *s1, const char *s2, size_t n) ...@@ -54,5 +55,11 @@ strncasecmp (const char *s1, const char *s2, size_t n)
54 } 55 }
55 while (c1 == c2); 56 while (c1 == c2);
56 57
57 return c1 - c2; 58 if (UCHAR_MAX <= INT_MAX)
59 return c1 - c2;
60 else
61 /* On machines where 'char' and 'int' are types of the same size, the
62 difference of two 'unsigned char' values - including the sign bit -
63 doesn't fit in an 'int'. */
64 return (c1 > c2 ? 1 : c1 < c2 ? -1 : 0);
58 } 65 }
......
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 19
20 #ifdef HAVE_CONFIG_H 20 #ifdef HAVE_CONFIG_H
21 # include "config.h" 21 # include <config.h>
22 #endif 22 #endif
23 23
24 #include <stdlib.h> 24 #include <stdlib.h>
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
16 with this program; if not, write to the Free Software Foundation, 16 with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ 17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 18
19 #if HAVE_CONFIG_H 19 #ifdef HAVE_CONFIG_H
20 # include <config.h> 20 # include <config.h>
21 #endif 21 #endif
22 #undef strnlen 22 #undef strnlen
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17 USA. */ 17 USA. */
18 18
19 #if HAVE_CONFIG_H 19 #ifdef HAVE_CONFIG_H
20 # include <config.h> 20 # include <config.h>
21 #endif 21 #endif
22 22
......