Commit dafd0440 dafd044063fc411ada8fd1a77e4ff6d7993b1c14 by Sergey Poznyakoff

Updated by gnulib-sync

1 parent 3e718271
......@@ -17,7 +17,7 @@
If not, write to the Free Software Foundation,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
#if HAVE_CONFIG_H
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
......
/* Copyright (C) 1992-2001, 2003, 2004 Free Software Foundation, Inc.
/* Copyright (C) 1992-2001, 2003, 2004, 2005 Free Software Foundation, Inc.
This file is part of the GNU C Library.
This program is free software; you can redistribute it and/or modify
......@@ -15,47 +15,32 @@
with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
#if HAVE_CONFIG_H
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#if !_LIBC
# include "getpass.h"
#endif
#include "getpass.h"
#if _LIBC
# define HAVE_STDIO_EXT_H 1
#endif
#include <stdio.h>
#if !defined _WIN32
#include <stdbool.h>
#include <stdio.h>
#if HAVE_STDIO_EXT_H
# include <stdio_ext.h>
#else
# define __fsetlocking(stream, type) /* empty */
#endif
#if !_LIBC
# include "getline.h"
#if !HAVE___FSETLOCKING
# define __fsetlocking(stream, type) /* empty */
#endif
#include <termios.h>
#include <unistd.h>
#if _LIBC
# include <wchar.h>
#if HAVE_TERMIOS_H
# include <termios.h>
#endif
#if _LIBC
# define NOTCANCEL_MODE "c"
#else
# define NOTCANCEL_MODE
#endif
#include "getline.h"
#if _LIBC
# define flockfile(s) _IO_flockfile (s)
# define funlockfile(s) _IO_funlockfile (s)
#elif USE_UNLOCKED_IO
#if USE_UNLOCKED_IO
# include "unlocked-io.h"
#else
# if !HAVE_DECL_FFLUSH_UNLOCKED
......@@ -80,18 +65,6 @@
# endif
#endif
#if _LIBC
# include <bits/libc-lock.h>
#else
# define __libc_cleanup_push(function, arg) /* empty */
# define __libc_cleanup_pop(execute) /* empty */
#endif
#if !_LIBC
# define __getline getline
# define __tcgetattr tcgetattr
#endif
/* It is desirable to use this bit on systems that have it.
The only bit of terminal state we want to twiddle is echoing, which is
done in software; there is no need to change the state of the terminal
......@@ -114,7 +87,7 @@ getpass (const char *prompt)
FILE *tty;
FILE *in, *out;
struct termios s, t;
bool tty_changed;
bool tty_changed = false;
static char *buf;
static size_t bufsize;
ssize_t nread;
......@@ -122,7 +95,7 @@ getpass (const char *prompt)
/* Try to write to and read from the terminal if we can.
If we can't open the terminal, use stderr and stdin. */
tty = fopen ("/dev/tty", "w+" NOTCANCEL_MODE);
tty = fopen ("/dev/tty", "w+");
if (tty == NULL)
{
in = stdin;
......@@ -136,39 +109,26 @@ getpass (const char *prompt)
out = in = tty;
}
/* Make sure the stream we opened is closed even if the thread is
canceled. */
__libc_cleanup_push (call_fclose, tty);
flockfile (out);
/* Turn echoing off if it is on now. */
if (__tcgetattr (fileno (in), &t) == 0)
#if HAVE_TCGETATTR
if (tcgetattr (fileno (in), &t) == 0)
{
/* Save the old one. */
s = t;
/* Tricky, tricky. */
t.c_lflag &= ~(ECHO|ISIG);
tty_changed = (tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &t) == 0);
t.c_lflag &= ~(ECHO | ISIG);
tty_changed = (tcsetattr (fileno (in), TCSAFLUSH | TCSASOFT, &t) == 0);
}
else
tty_changed = false;
#endif
/* Write the prompt. */
#ifdef USE_IN_LIBIO
if (_IO_fwide (out, 0) > 0)
__fwprintf (out, L"%s", prompt);
else
#endif
fputs_unlocked (prompt, out);
fputs_unlocked (prompt, out);
fflush_unlocked (out);
/* Read the password. */
nread = __getline (&buf, &bufsize, in);
#if !_LIBC
/* As far as is known, glibc doesn't need this no-op fseek. */
nread = getline (&buf, &bufsize, in);
/* According to the C standard, input may not be followed by output
on the same stream without an intervening call to a file
......@@ -180,7 +140,6 @@ getpass (const char *prompt)
from POSIX version to POSIX version, so play it safe and invoke
fseek even if in != out. */
fseek (out, 0, SEEK_CUR);
#endif
if (buf != NULL)
{
......@@ -193,25 +152,75 @@ getpass (const char *prompt)
if (tty_changed)
{
/* Write the newline that was not echoed. */
#ifdef USE_IN_LIBIO
if (_IO_fwide (out, 0) > 0)
putwc_unlocked (L'\n', out);
else
#endif
putc_unlocked ('\n', out);
putc_unlocked ('\n', out);
}
}
}
/* Restore the original setting. */
#if HAVE_TCSETATTR
if (tty_changed)
(void) tcsetattr (fileno (in), TCSAFLUSH|TCSASOFT, &s);
tcsetattr (fileno (in), TCSAFLUSH | TCSASOFT, &s);
#endif
funlockfile (out);
__libc_cleanup_pop (0);
call_fclose (tty);
return buf;
}
#else /* WIN32 */
/* Windows implementation by Martin Lambers <marlam@marlam.de>,
improved by Simon Josefsson. */
/* For PASS_MAX. */
#include <limits.h>
#ifndef PASS_MAX
# define PASS_MAX 512
#endif
char *
getpass (const char *prompt)
{
char getpassbuf[PASS_MAX + 1];
size_t i = 0;
int c;
if (prompt)
{
fputs (prompt, stderr);
fflush (stderr);
}
for (;;)
{
c = _getch ();
if (c == '\r')
{
getpassbuf[i] = '\0';
break;
}
else if (i < PASS_MAX)
{
getpassbuf[i++] = c;
}
if (i >= PASS_MAX)
{
getpassbuf[i] = '\0';
break;
}
}
if (prompt)
{
fputs ("\r\n", stderr);
fflush (stderr);
}
return strdup (getpassbuf);
}
#endif
......
......@@ -53,12 +53,25 @@
? (t) -1 \
: ~ (~ (t) 0 << (sizeof (t) * CHAR_BIT - 1))))
/* Return zero if T can be determined to be an unsigned type.
Otherwise, return 1.
When compiling with GCC, INT_STRLEN_BOUND uses this macro to obtain a
tighter bound. Otherwise, it overestimates the true bound by one byte
when applied to unsigned types of size 2, 4, 16, ... bytes.
The symbol signed_type_or_expr__ is private to this header file. */
#if __GNUC__ >= 2
# define signed_type_or_expr__(t) TYPE_SIGNED (__typeof__ (t))
#else
# define signed_type_or_expr__(t) 1
#endif
/* Bound on length of the string representing an integer type or expression T.
Subtract 1 for the sign bit if t is signed; log10 (2.0) < 146/485;
Subtract 1 for the sign bit if T is signed; log10 (2.0) < 146/485;
add 1 for integer division truncation; add 1 more for a minus sign
if needed. */
#define INT_STRLEN_BOUND(t) \
((sizeof (t) * CHAR_BIT - 1) * 146 / 485 + 2)
((sizeof (t) * CHAR_BIT - signed_type_or_expr__ (t)) * 146 / 485 \
+ signed_type_or_expr__ (t) + 1)
/* Bound on buffer size needed to represent an integer type or expression T,
including the terminating null. */
......
......@@ -51,10 +51,6 @@
# endif
#endif
#if defined _LIBC && defined USE_IN_LIBIO
# include <wchar.h>
#endif
#include <stddef.h>
#ifndef ELIDE_CODE
......@@ -433,12 +429,11 @@ print_and_abort (void)
happen because the "memory exhausted" message appears in other places
like this and the translation should be reused instead of creating
a very similar string which requires a separate translation. */
# if defined _LIBC && defined USE_IN_LIBIO
if (_IO_fwide (stderr, 0) > 0)
__fwprintf (stderr, L"%s\n", _("memory exhausted"));
else
# ifdef _LIBC
(void) __fxprintf (NULL, "%s\n", _("memory exhausted"));
# else
fprintf (stderr, "%s\n", _("memory exhausted"));
# endif
fprintf (stderr, "%s\n", _("memory exhausted"));
exit (obstack_exit_failure);
}
......
/* obstack.h - object stack macros
Copyright (C) 1988-1994,1996-1999,2003,2004 Free Software Foundation, Inc.
This file is part of the GNU C Library. Its master source is NOT part of
the C library, however. The master source lives in /gd/gnu/lib.
NOTE: The canonical source of this file is maintained with the GNU C Library.
Bugs can be reported to bug-glibc@gnu.org.
Copyright (C) 1988-1994,1996-1999,2003,2004,2005
Free Software Foundation, Inc.
This file is part of the GNU C Library.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -464,7 +460,7 @@ __extension__ \
(((const void **) ((h)->next_free += sizeof (void *)))[-1] = (aptr))
# define obstack_int_grow_fast(h,aint) \
(((int *) ((h)->next_free += sizeof (int)))[-1] = (aptr))
(((int *) ((h)->next_free += sizeof (int)))[-1] = (aint))
# define obstack_blank(h,length) \
( (h)->temp.tempint = (length), \
......
......@@ -17,7 +17,7 @@
/* written by Jim Meyering */
#if HAVE_CONFIG_H
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#undef realloc
......
......@@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
#if HAVE_CONFIG_H
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
......
......@@ -17,7 +17,7 @@
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
#if HAVE_CONFIG_H
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
......
......@@ -19,7 +19,7 @@
/* Written by Jim Meyering. */
#if HAVE_CONFIG_H
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
......
......@@ -3,7 +3,8 @@
# This is a modified version of autoconf's AC_FUNC_FNMATCH.
# This file should be simplified after Autoconf 2.57 is required.
# Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
# Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 Free Software
# Foundation, Inc.
# This file is free software; the Free Software Foundation
# gives unlimited permission to copy and/or distribute it,
# with or without modifications, as long as this notice is preserved.
......@@ -27,9 +28,15 @@ AC_DEFUN([_AC_FUNC_FNMATCH_IF],
# include <fnmatch.h>
# define y(a, b, c) (fnmatch (a, b, c) == 0)
# define n(a, b, c) (fnmatch (a, b, c) == FNM_NOMATCH)
static int
fnm (char const *pattern, char const *string, int flags)
{
return fnmatch (pattern, string, flags);
}
],
[exit
(!(y ("a*", "abc", 0)
(!((fnm ? fnm : fnmatch) ("a*", "", 0) == FNM_NOMATCH
&& y ("a*", "abc", 0)
&& n ("d*/*1", "d/s/1", FNM_PATHNAME)
&& y ("a\\\\bc", "abc", 0)
&& n ("a\\\\bc", "abc", FNM_NOESCAPE)
......@@ -50,19 +57,19 @@ AS_IF([test $$2 = yes], [$3], [$4])
])# _AC_FUNC_FNMATCH_IF
# _AC_LIBOBJ_FNMATCH
# _MU_LIBOBJ_FNMATCH
# ------------------
# Prepare the replacement of fnmatch.
AC_DEFUN([_AC_LIBOBJ_FNMATCH],
AC_DEFUN([_MU_LIBOBJ_FNMATCH],
[AC_REQUIRE([AC_C_CONST])dnl
AC_REQUIRE([AC_FUNC_ALLOCA])dnl
AC_REQUIRE([AC_TYPE_MBSTATE_T])dnl
AC_CHECK_DECLS([getenv])
AC_CHECK_FUNCS([btowc mbsrtowcs mempcpy wmemchr wmemcpy wmempcpy])
AC_CHECK_HEADERS([wchar.h wctype.h])
AC_LIBOBJ([fnmatch])
MU_LIBOBJ([fnmatch])
FNMATCH_H=fnmatch.h
])# _AC_LIBOBJ_FNMATCH
])# _MU_LIBOBJ_FNMATCH
AC_DEFUN([gl_FUNC_FNMATCH_POSIX],
......@@ -70,7 +77,7 @@ AC_DEFUN([gl_FUNC_FNMATCH_POSIX],
FNMATCH_H=
_AC_FUNC_FNMATCH_IF([POSIX], [ac_cv_func_fnmatch_posix],
[rm -f lib/fnmatch.h],
[_AC_LIBOBJ_FNMATCH])
[_MU_LIBOBJ_FNMATCH])
if test $ac_cv_func_fnmatch_posix != yes; then
dnl We must choose a different name for our function, since on ELF systems
dnl a broken fnmatch() in libc.so would override our fnmatch() if it is
......@@ -90,7 +97,7 @@ AC_DEFUN([gl_FUNC_FNMATCH_GNU],
FNMATCH_H=
_AC_FUNC_FNMATCH_IF([GNU], [ac_cv_func_fnmatch_gnu],
[rm -f lib/fnmatch.h],
[_AC_LIBOBJ_FNMATCH])
[_MU_LIBOBJ_FNMATCH])
if test $ac_cv_func_fnmatch_gnu != yes; then
dnl We must choose a different name for our function, since on ELF systems
dnl a broken fnmatch() in libc.so would override our fnmatch() if it is
......
# getopt.m4 serial 10
# getopt.m4 serial 11
dnl Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
......@@ -27,8 +27,10 @@ AC_DEFUN([gl_GETOPT_SUBSTITUTE_HEADER],
AC_DEFUN([gl_GETOPT_CHECK_HEADERS],
[
GETOPT_H=
AC_CHECK_HEADERS([getopt.h], [], [GETOPT_H=getopt.h])
if test -z "$GETOPT_H"; then
AC_CHECK_HEADERS([getopt.h], [], [GETOPT_H=getopt.h])
fi
if test -z "$GETOPT_H"; then
AC_CHECK_FUNCS([getopt_long_only], [], [GETOPT_H=getopt.h])
fi
......
......@@ -35,7 +35,7 @@ AC_DEFUN([gl_FUNC_GETPASS_GNU],
# Prerequisites of lib/getpass.c.
AC_DEFUN([gl_PREREQ_GETPASS], [
AC_CHECK_HEADERS_ONCE(stdio_ext.h)
AC_CHECK_HEADERS_ONCE(stdio_ext.h termios.h)
AC_CHECK_FUNCS_ONCE(__fsetlocking tcgetattr tcsetattr)
AC_CHECK_DECLS_ONCE([fflush_unlocked flockfile fputs_unlocked funlockfile putc_unlocked])
:
])
......
# inttypes.m4 serial 1 (gettext-0.11.4)
dnl Copyright (C) 1997-2002 Free Software Foundation, Inc.
dnl This file is free software, distributed under the terms of the GNU
dnl General Public License. As a special exception to the GNU General
dnl Public License, this file may be distributed as part of a program
dnl that contains a configuration script generated by Autoconf, under
dnl the same distribution terms as the rest of that program.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
dnl with or without modifications, as long as this notice is preserved.
dnl From Paul Eggert.
......
# longdouble.m4 serial 1 (gettext-0.12)
dnl Copyright (C) 2002-2003 Free Software Foundation, Inc.
dnl This file is free software, distributed under the terms of the GNU
dnl General Public License. As a special exception to the GNU General
dnl Public License, this file may be distributed as part of a program
dnl that contains a configuration script generated by Autoconf, under
dnl the same distribution terms as the rest of that program.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
dnl with or without modifications, as long as this notice is preserved.
dnl From Bruno Haible.
dnl Test whether the compiler supports the 'long double' type.
......
# mbchar.m4 serial 1
# mbchar.m4 serial 2
dnl Copyright (C) 2005 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
......@@ -10,5 +10,11 @@ dnl From Bruno Haible.
AC_DEFUN([gl_MBCHAR],
[
AC_REQUIRE([AC_GNU_SOURCE])
:
dnl The following line is that so the user can test
dnl HAVE_WCHAR_H && HAVE_WCTYPE_H before #include "mbchar.h".
AC_CHECK_HEADERS_ONCE(wchar.h wctype.h)
dnl Compile mbchar.c only if HAVE_WCHAR_H && HAVE_WCTYPE_H.
if test $ac_cv_header_wchar_h = yes && test $ac_cv_header_wctype_h = yes; then
MU_LIBOBJ([mbchar])
fi
])
......
# md5.m4 serial 7
# md5.m4 serial 8
dnl Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
......@@ -9,9 +9,6 @@ AC_DEFUN([gl_MD5],
MU_LIBSOURCES([md5.c, md5.h])
MU_LIBOBJ([md5])
dnl Prerequisites of lib/md5.h.
AC_REQUIRE([gl_AC_TYPE_UINT32_T])
dnl Prerequisites of lib/md5.c.
AC_REQUIRE([AC_C_BIGENDIAN])
:
......
# minmax.m4 serial 1
# minmax.m4 serial 2
dnl Copyright (C) 2005 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
dnl with or without modifications, as long as this notice is preserved.
AC_PREREQ(2.52)
AC_DEFUN([gl_MINMAX],
[
AC_REQUIRE([gl_PREREQ_MINMAX])
......@@ -17,12 +19,13 @@ AC_DEFUN([gl_PREREQ_MINMAX],
])
dnl gl_MINMAX_IN_HEADER(HEADER)
dnl The parameter has to be a literal header name; it cannot be macro,
dnl nor a shell variable. (Because autoheader collects only AC_DEFINE
dnl invocations with a literal macro name.)
AC_DEFUN([gl_MINMAX_IN_HEADER],
[
define([header],[translit([$1],[./-],
[___])])
define([HEADER],[translit([$1],[abcdefghijklmnopqrstuvwxyz./-],
[ABCDEFGHIJKLMNOPQRSTUVWXYZ___])])
m4_pushdef([header], AS_TR_SH([$1]))
m4_pushdef([HEADER], AS_TR_CPP([$1]))
AC_CACHE_CHECK([whether <$1> defines MIN and MAX],
[gl_cv_minmax_in_]header,
[AC_TRY_COMPILE([#include <$1>
......@@ -33,6 +36,6 @@ int x = MIN (42, 17);], [],
AC_DEFINE([HAVE_MINMAX_IN_]HEADER, 1,
[Define to 1 if <$1> defines the MIN and MAX macros.])
fi
undefine([HEADER])
undefine([header])
m4_popdef([HEADER])
m4_popdef([header])
])
......
#serial 24
#serial 30
# Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2003, 2004, 2005 Free
# Software Foundation, Inc.
......@@ -10,121 +10,155 @@
dnl Initially derived from code in GNU grep.
dnl Mostly written by Jim Meyering.
AC_PREREQ([2.50])
AC_DEFUN([gl_REGEX],
[
gl_INCLUDED_REGEX([lib/regex.c])
])
dnl Usage: gl_INCLUDED_REGEX([lib/regex.c])
dnl
AC_DEFUN([gl_INCLUDED_REGEX],
[
MU_LIBSOURCES(
[regcomp.c, regex.c, regex.h,
regex_internal.c, regex_internal.h, regexec.c])
dnl Even packages that don't use regex.c can use this macro.
dnl Of course, for them it doesn't do anything.
# Assume we'll default to using the included regex.c.
ac_use_included_regex=yes
# However, if the system regex support is good enough that it passes the
# the following run test, then default to *not* using the included regex.c.
AC_REQUIRE([AC_SYS_LARGEFILE]) dnl for a sufficently-wide off_t
AC_DEFINE([_REGEX_LARGE_OFFSETS], 1,
[Define if you want regoff_t to be at least as wide POSIX requires.])
MU_LIBSOURCES(
[regcomp.c, regex.c, regex.h,
regex_internal.c, regex_internal.h, regexec.c])
AC_ARG_WITH([included-regex],
[AC_HELP_STRING([--without-included-regex],
[don't compile regex; this is the default on
systems with recent-enough versions of the GNU C
Library (use with caution on other systems)])])
case $with_included_regex in
yes|no) ac_use_included_regex=$with_included_regex
;;
'')
# If the system regex support is good enough that it passes the the
# following run test, then default to *not* using the included regex.c.
# If cross compiling, assume the test would fail and use the included
# regex.c. The first failing regular expression is from `Spencer ere
# test #75' in grep-2.3.
AC_CACHE_CHECK([for working re_compile_pattern],
[gl_cv_func_working_re_compile_pattern],
[gl_cv_func_re_compile_pattern_broken],
[AC_RUN_IFELSE(
[AC_LANG_PROGRAM(
[AC_INCLUDES_DEFAULT
#include <regex.h>],
[[static struct re_pattern_buffer regex;
const char *s;
struct re_registers regs;
re_set_syntax (RE_SYNTAX_POSIX_EGREP);
memset (&regex, 0, sizeof (regex));
s = re_compile_pattern ("a[:@:>@:]b\n", 9, &regex);
/* This should fail with _Invalid character class name_ error. */
if (!s)
exit (1);
/* This should succeed, but does not for e.g. glibc-2.1.3. */
memset (&regex, 0, sizeof (regex));
s = re_compile_pattern ("{1", 2, &regex);
if (s)
exit (1);
/* The following example is derived from a problem report
against gawk from Jorge Stolfi <stolfi@ic.unicamp.br>. */
memset (&regex, 0, sizeof (regex));
s = re_compile_pattern ("[an\371]*n", 7, &regex);
if (s)
exit (1);
/* This should match, but does not for e.g. glibc-2.2.1. */
if (re_match (&regex, "an", 2, 0, &regs) != 2)
exit (1);
memset (&regex, 0, sizeof (regex));
s = re_compile_pattern ("x", 1, &regex);
if (s)
exit (1);
/* The version of regex.c in e.g. GNU libc-2.2.93 did not
work with a negative RANGE argument. */
if (re_search (&regex, "wxy", 3, 2, -2, &regs) != 1)
exit (1);
/* The version of regex.c in older versions of gnulib
* ignored RE_ICASE. Detect that problem too. */
memset (&regex, 0, sizeof (regex));
re_set_syntax(RE_SYNTAX_EMACS|RE_ICASE);
s = re_compile_pattern ("x", 1, &regex);
if (s)
exit (1);
if (re_search (&regex, "WXY", 3, 0, 3, &regs) < 0)
exit (1);
/* REG_STARTEND was added to glibc on 2004-01-15.
Reject older versions. */
if (! REG_STARTEND)
exit (1);
exit (0);]])],
[gl_cv_func_working_re_compile_pattern=yes],
[gl_cv_func_working_re_compile_pattern=no],
dnl When crosscompiling, assume it is broken.
[gl_cv_func_working_re_compile_pattern=no])])
if test $gl_cv_func_working_re_compile_pattern = yes; then
ac_use_included_regex=no
fi
test -n "$1" || AC_MSG_ERROR([missing argument])
m4_syscmd([test -f '$1'])
ifelse(m4_sysval, 0,
[
AC_ARG_WITH([included-regex],
[ --without-included-regex don't compile regex; this is the default on
systems with recent-enough versions of the GNU C
Library (use with caution on other systems)],
[gl_with_regex=$withval],
[gl_with_regex=$ac_use_included_regex])
if test "X$gl_with_regex" = Xyes; then
MU_LIBOBJ([regex])
gl_PREREQ_REGEX
fi
],
)
]
)
[AC_LANG_PROGRAM(
[AC_INCLUDES_DEFAULT
#include <regex.h>],
[[static struct re_pattern_buffer regex;
const char *s;
struct re_registers regs;
/* Use the POSIX-compliant spelling with leading REG_,
rather than the traditional GNU spelling with leading RE_,
so that we reject older libc implementations. */
re_set_syntax (REG_SYNTAX_POSIX_EGREP);
memset (&regex, 0, sizeof (regex));
s = re_compile_pattern ("a[:@:>@:]b\n", 9, &regex);
/* This should fail with _Invalid character class name_ error. */
if (!s)
exit (1);
/* This should succeed, but does not for e.g. glibc-2.1.3. */
memset (&regex, 0, sizeof (regex));
s = re_compile_pattern ("{1", 2, &regex);
if (s)
exit (1);
/* The following example is derived from a problem report
against gawk from Jorge Stolfi <stolfi@ic.unicamp.br>. */
memset (&regex, 0, sizeof (regex));
s = re_compile_pattern ("[an\371]*n", 7, &regex);
if (s)
exit (1);
/* This should match, but does not for e.g. glibc-2.2.1. */
if (re_match (&regex, "an", 2, 0, &regs) != 2)
exit (1);
memset (&regex, 0, sizeof (regex));
s = re_compile_pattern ("x", 1, &regex);
if (s)
exit (1);
/* The version of regex.c in e.g. GNU libc-2.2.93 did not
work with a negative RANGE argument. */
if (re_search (&regex, "wxy", 3, 2, -2, &regs) != 1)
exit (1);
/* The version of regex.c in older versions of gnulib
ignored REG_IGNORE_CASE (which was then called RE_ICASE).
Detect that problem too. */
memset (&regex, 0, sizeof (regex));
re_set_syntax (REG_SYNTAX_EMACS | REG_IGNORE_CASE);
s = re_compile_pattern ("x", 1, &regex);
if (s)
exit (1);
if (re_search (&regex, "WXY", 3, 0, 3, &regs) < 0)
exit (1);
/* REG_STARTEND was added to glibc on 2004-01-15.
Reject older versions. */
if (! REG_STARTEND)
exit (1);
/* Reject hosts whose regoff_t values are too narrow.
These include glibc 2.3.5 on hosts with 64-bit off_t
and 32-bit int, and Solaris 10 on hosts with 32-bit int
and _FILE_OFFSET_BITS=64. */
if (sizeof (regoff_t) < sizeof (off_t))
exit (1);
exit (0);]])],
[gl_cv_func_re_compile_pattern_broken=no],
[gl_cv_func_re_compile_pattern_broken=yes],
dnl When crosscompiling, assume it is broken.
[gl_cv_func_re_compile_pattern_broken=yes])])
ac_use_included_regex=$gl_cv_func_re_compile_pattern_broken
;;
*) AC_MSG_ERROR([Invalid value for --with-included-regex: $with_included_regex])
;;
esac
if test $ac_use_included_regex = yes; then
AC_DEFINE([re_syntax_options], [rpl_re_syntax_options],
[Define to rpl_re_syntax_options if the replacement should be used.])
AC_DEFINE([re_set_syntax], [rpl_re_set_syntax],
[Define to rpl_re_set_syntax if the replacement should be used.])
AC_DEFINE([re_compile_pattern], [rpl_re_compile_pattern],
[Define to rpl_re_compile_pattern if the replacement should be used.])
AC_DEFINE([re_compile_fastmap], [rpl_re_compile_fastmap],
[Define to rpl_re_compile_fastmap if the replacement should be used.])
AC_DEFINE([re_search], [rpl_re_search],
[Define to rpl_re_search if the replacement should be used.])
AC_DEFINE([re_search_2], [rpl_re_search_2],
[Define to rpl_re_search_2 if the replacement should be used.])
AC_DEFINE([re_match], [rpl_re_match],
[Define to rpl_re_match if the replacement should be used.])
AC_DEFINE([re_match_2], [rpl_re_match_2],
[Define to rpl_re_match_2 if the replacement should be used.])
AC_DEFINE([re_set_registers], [rpl_re_set_registers],
[Define to rpl_re_set_registers if the replacement should be used.])
AC_DEFINE([re_comp], [rpl_re_comp],
[Define to rpl_re_comp if the replacement should be used.])
AC_DEFINE([re_exec], [rpl_re_exec],
[Define to rpl_re_exec if the replacement should be used.])
AC_DEFINE([regcomp], [rpl_regcomp],
[Define to rpl_regcomp if the replacement should be used.])
AC_DEFINE([regexec], [rpl_regexec],
[Define to rpl_regexec if the replacement should be used.])
AC_DEFINE([regerror], [rpl_regerror],
[Define to rpl_regerror if the replacement should be used.])
AC_DEFINE([regfree], [rpl_regfree],
[Define to rpl_regfree if the replacement should be used.])
MU_LIBOBJ([regex])
gl_PREREQ_REGEX
fi
])
# Prerequisites of lib/regex.c and lib/regex_internal.c.
AC_DEFUN([gl_PREREQ_REGEX],
[
AC_REQUIRE([AC_GNU_SOURCE])
AC_REQUIRE([gl_C_RESTRICT])
AC_REQUIRE([AM_LANGINFO_CODESET])
AC_CHECK_HEADERS_ONCE([locale.h wchar.h wctype.h])
......
# signed.m4 serial 1 (gettext-0.10.40)
dnl Copyright (C) 2001-2002 Free Software Foundation, Inc.
dnl This file is free software, distributed under the terms of the GNU
dnl General Public License. As a special exception to the GNU General
dnl Public License, this file may be distributed as part of a program
dnl that contains a configuration script generated by Autoconf, under
dnl the same distribution terms as the rest of that program.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
dnl with or without modifications, as long as this notice is preserved.
dnl From Bruno Haible.
......
# size_max.m4 serial 2
dnl Copyright (C) 2003 Free Software Foundation, Inc.
dnl This file is free software, distributed under the terms of the GNU
dnl General Public License. As a special exception to the GNU General
dnl Public License, this file may be distributed as part of a program
dnl that contains a configuration script generated by Autoconf, under
dnl the same distribution terms as the rest of that program.
# size_max.m4 serial 3
dnl Copyright (C) 2003, 2005 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
dnl with or without modifications, as long as this notice is preserved.
dnl From Bruno Haible.
......@@ -28,9 +26,9 @@ Found it
dnl than the type 'unsigned long'.
dnl The _AC_COMPUTE_INT macro works up to LONG_MAX, since it uses 'expr',
dnl which is guaranteed to work from LONG_MIN to LONG_MAX.
_AC_COMPUTE_INT([~(size_t)0 / 10], res_hi,
_AC_COMPUTE_INT([(size_t)~(size_t)0 / 10], res_hi,
[#include <stddef.h>], result=?)
_AC_COMPUTE_INT([~(size_t)0 % 10], res_lo,
_AC_COMPUTE_INT([(size_t)~(size_t)0 % 10], res_lo,
[#include <stddef.h>], result=?)
_AC_COMPUTE_INT([sizeof (size_t) <= sizeof (unsigned int)], fits_in_uint,
[#include <stddef.h>], result=?)
......@@ -50,7 +48,7 @@ Found it
fi
else
dnl Shouldn't happen, but who knows...
result='~(size_t)0'
result='((size_t)~(size_t)0)'
fi
fi
AC_MSG_RESULT([$result])
......
# Check for stdbool.h that conforms to C99.
dnl Copyright (C) 2002-2004 Free Software Foundation, Inc.
dnl Copyright (C) 2002-2005 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
dnl with or without modifications, as long as this notice is preserved.
......@@ -28,6 +28,9 @@ AC_DEFUN([AM_STDBOOL_H],
AC_SUBST([HAVE__BOOL])
])
# AM_STDBOOL_H will be renamed to gl_STDBOOL_H in the future.
AC_DEFUN([gl_STDBOOL_H], [AM_STDBOOL_H])
# This macro is only needed in autoconf <= 2.59. Newer versions of autoconf
# have this macro built-in.
......@@ -70,10 +73,12 @@ AC_DEFUN([AC_HEADER_STDBOOL],
enum { j = false, k = true, l = false * true, m = true * 256 };
_Bool n[m];
char o[sizeof n == m * sizeof n[0] ? 1 : -1];
char p[-1 - (_Bool) 0 < 0 && -1 - (bool) 0 < 0 ? 1 : -1];
],
[
return (!a + !b + !c + !d + !e + !f + !g + !h + !i + !j + !k + !l
+ !m + !n + !o);
/* Refer to every declared value, to avoid compiler optimizations. */
return (!a + !b + !c + !d + !e + !f + !g + !h + !i + !!j + !k + !!l
+ !m + !n + !o + !p);
],
[ac_cv_header_stdbool_h=yes],
[ac_cv_header_stdbool_h=no])])
......
# strcase.m4 serial 2
# strcase.m4 serial 3
dnl Copyright (C) 2002, 2005 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
......@@ -29,7 +29,8 @@ AC_DEFUN([gl_FUNC_STRNCASECMP],
# Prerequisites of lib/strcasecmp.c.
AC_DEFUN([gl_PREREQ_STRCASECMP], [
gl_FUNC_MBRTOWC
AC_REQUIRE([gl_FUNC_MBRTOWC])
:
])
# Prerequisites of lib/strncasecmp.c.
......
# wchar_t.m4 serial 1 (gettext-0.12)
dnl Copyright (C) 2002-2003 Free Software Foundation, Inc.
dnl This file is free software, distributed under the terms of the GNU
dnl General Public License. As a special exception to the GNU General
dnl Public License, this file may be distributed as part of a program
dnl that contains a configuration script generated by Autoconf, under
dnl the same distribution terms as the rest of that program.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
dnl with or without modifications, as long as this notice is preserved.
dnl From Bruno Haible.
dnl Test whether <stddef.h> has the 'wchar_t' type.
......
# wint_t.m4 serial 1 (gettext-0.12)
dnl Copyright (C) 2003 Free Software Foundation, Inc.
dnl This file is free software, distributed under the terms of the GNU
dnl General Public License. As a special exception to the GNU General
dnl Public License, this file may be distributed as part of a program
dnl that contains a configuration script generated by Autoconf, under
dnl the same distribution terms as the rest of that program.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
dnl with or without modifications, as long as this notice is preserved.
dnl From Bruno Haible.
dnl Test whether <wchar.h> has the 'wint_t' type.
......
# xsize.m4 serial 2
dnl Copyright (C) 2003 Free Software Foundation, Inc.
dnl This file is free software, distributed under the terms of the GNU
dnl General Public License. As a special exception to the GNU General
dnl Public License, this file may be distributed as part of a program
dnl that contains a configuration script generated by Autoconf, under
dnl the same distribution terms as the rest of that program.
# xsize.m4 serial 3
dnl Copyright (C) 2003-2004 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
dnl with or without modifications, as long as this notice is preserved.
AC_DEFUN([gl_XSIZE],
[
dnl Prerequisites of lib/xsize.h.
AC_REQUIRE([gl_SIZE_MAX])
AC_REQUIRE([AC_C_INLINE])
AC_CHECK_HEADERS(stdint.h)
])
......
......@@ -15,10 +15,10 @@
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
#ifdef HAVE_CONFIG_H
#include <config.h>
# include <config.h>
#endif
#include <sysexits.h>
......
......@@ -21,7 +21,7 @@
don't have that. */
#ifdef HAVE_CONFIG_H
#include <config.h>
# include <config.h>
#endif
#include <stdlib.h>
......
......@@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
/* This package emulates glibc `line_wrap_stream' semantics for systems that
don't have that. If the system does have it, it is just a wrapper for
......@@ -25,10 +25,6 @@
#ifndef _ARGP_FMTSTREAM_H
#define _ARGP_FMTSTREAM_H
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <string.h>
#include <unistd.h>
......
......@@ -15,10 +15,10 @@
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
#ifdef HAVE_CONFIG_H
#include <config.h>
# include <config.h>
#endif
#define ARGP_FS_EI
......
......@@ -22,7 +22,7 @@
#endif
#ifdef HAVE_CONFIG_H
#include <config.h>
# include <config.h>
#endif
#include <alloca.h>
......
......@@ -18,7 +18,7 @@
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
#ifdef HAVE_CONFIG_H
#include <config.h>
# include <config.h>
#endif
#include <alloca.h>
......@@ -81,8 +81,8 @@ static const struct argp_option argp_default_options[] =
{
{"help", '?', 0, 0, N_("Give this help list"), -1},
{"usage", OPT_USAGE, 0, 0, N_("Give a short usage message"), 0},
{"program-name",OPT_PROGNAME,"NAME", OPTION_HIDDEN, N_("Set the program name"), 0},
{"HANG", OPT_HANG, "SECS", OPTION_ARG_OPTIONAL | OPTION_HIDDEN,
{"program-name",OPT_PROGNAME,N_("NAME"), OPTION_HIDDEN, N_("Set the program name"), 0},
{"HANG", OPT_HANG, N_("SECS"), OPTION_ARG_OPTIONAL | OPTION_HIDDEN,
N_("Hang for SECS seconds (default 3600)"), 0},
{NULL, 0, 0, 0, NULL, 0}
};
......
......@@ -18,7 +18,7 @@
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
#ifdef HAVE_CONFIG_H
#include <config.h>
# include <config.h>
#endif
#include "argp.h"
......
......@@ -15,10 +15,10 @@
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
#ifdef HAVE_CONFIG_H
#include <config.h>
# include <config.h>
#endif
#if defined _LIBC || defined HAVE_FEATURES_H
......
......@@ -19,15 +19,22 @@
/* Ported from glibc by Simon Josefsson. */
#if HAVE_CONFIG_H
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "getdelim.h"
#include <limits.h>
#include <stdlib.h>
#include <errno.h>
#include "getdelim.h"
#ifndef SIZE_MAX
# define SIZE_MAX ((size_t) -1)
#endif
#ifndef SSIZE_MAX
# define SSIZE_MAX ((ssize_t) (SIZE_MAX / 2))
#endif
#if !HAVE_FLOCKFILE
# undef flockfile
# define flockfile(x) ((void) 0)
......@@ -46,9 +53,8 @@
ssize_t
getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp)
{
int result;
ssize_t cur_len = 0;
ssize_t len;
ssize_t result;
size_t cur_len = 0;
if (lineptr == NULL || n == NULL || fp == NULL)
{
......@@ -71,20 +77,26 @@ getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp)
for (;;)
{
char *t;
int i;
i = getc (fp);
if (i == EOF)
break;
{
result = -1;
break;
}
/* Make enough space for len+1 (for final NUL) bytes. */
if (cur_len + 1 >= *n)
{
size_t needed = 2 * (cur_len + 1) + 1; /* Be generous. */
size_t needed_max =
SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX;
size_t needed = 2 * *n + 1; /* Be generous. */
char *new_lineptr;
if (needed < cur_len)
if (needed_max < needed)
needed = needed_max;
if (cur_len + 1 >= needed)
{
result = -1;
goto unlock_return;
......@@ -108,7 +120,7 @@ getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp)
break;
}
(*lineptr)[cur_len] = '\0';
result = cur_len;
result = cur_len ? cur_len : result;
unlock_return:
funlockfile (fp);
......
......@@ -18,7 +18,7 @@
/* Written by Simon Josefsson. */
#if HAVE_CONFIG_H
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
......
......@@ -18,7 +18,7 @@
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
#ifdef HAVE_CONFIG_H
#include <config.h>
# include <config.h>
#endif
#ifdef _LIBC
......
/* Declarations for getopt.
Copyright (C) 1989-1994,1996-1999,2001,2003,2004
Copyright (C) 1989-1994,1996-1999,2001,2003,2004,2005
Free Software Foundation, Inc.
This file is part of the GNU C Library.
......@@ -34,9 +34,7 @@
#if defined __GETOPT_PREFIX && !defined __need_getopt
# include <stdlib.h>
# include <stdio.h>
# if HAVE_UNISTD_H
# include <unistd.h>
# endif
# include <unistd.h>
# undef __need_getopt
# undef getopt
# undef getopt_long
......
......@@ -182,21 +182,29 @@ typedef struct mbchar mbchar_t;
#define mb_iseq(mbc, sc) ((mbc).wc_valid && (mbc).wc == (sc))
#define mb_isnul(mbc) ((mbc).wc_valid && (mbc).wc == 0)
#define mb_cmp(mbc1, mbc2) \
((mbc1).wc_valid && (mbc2).wc_valid \
? (int) (mbc1).wc - (int) (mbc2).wc \
: (mbc1).bytes == (mbc2).bytes \
? memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes) \
: (mbc1).bytes < (mbc2).bytes \
? (memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes) > 0 ? 1 : -1) \
: (memcmp ((mbc1).ptr, (mbc2).ptr, (mbc2).bytes) >= 0 ? 1 : -1))
((mbc1).wc_valid \
? ((mbc2).wc_valid \
? (int) (mbc1).wc - (int) (mbc2).wc \
: -1) \
: ((mbc2).wc_valid \
? 1 \
: (mbc1).bytes == (mbc2).bytes \
? memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes) \
: (mbc1).bytes < (mbc2).bytes \
? (memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes) > 0 ? 1 : -1) \
: (memcmp ((mbc1).ptr, (mbc2).ptr, (mbc2).bytes) >= 0 ? 1 : -1)))
#define mb_casecmp(mbc1, mbc2) \
((mbc1).wc_valid && (mbc2).wc_valid \
? (int) towlower ((mbc1).wc) - (int) towlower ((mbc2).wc) \
: (mbc1).bytes == (mbc2).bytes \
? memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes) \
: (mbc1).bytes < (mbc2).bytes \
? (memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes) > 0 ? 1 : -1) \
: (memcmp ((mbc1).ptr, (mbc2).ptr, (mbc2).bytes) >= 0 ? 1 : -1))
((mbc1).wc_valid \
? ((mbc2).wc_valid \
? (int) towlower ((mbc1).wc) - (int) towlower ((mbc2).wc) \
: -1) \
: ((mbc2).wc_valid \
? 1 \
: (mbc1).bytes == (mbc2).bytes \
? memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes) \
: (mbc1).bytes < (mbc2).bytes \
? (memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes) > 0 ? 1 : -1) \
: (memcmp ((mbc1).ptr, (mbc2).ptr, (mbc2).bytes) >= 0 ? 1 : -1)))
#define mb_equal(mbc1, mbc2) \
((mbc1).wc_valid && (mbc2).wc_valid \
? (mbc1).wc == (mbc2).wc \
......
/* md5.c - Functions to compute MD5 message digest of files or memory blocks
/* Functions to compute MD5 message digest of files or memory blocks.
according to the definition of MD5 in RFC 1321 from April 1992.
Copyright (C) 1995, 1996, 2001, 2003, 2004 Free Software Foundation, Inc.
NOTE: The canonical source of this file is maintained with the GNU C
Library. Bugs can be reported to bug-glibc@prep.ai.mit.edu.
Copyright (C) 1995,1996,1997,1999,2000,2001,2005
Free Software Foundation, Inc.
This file is part of the GNU C Library.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
......@@ -27,7 +27,9 @@
#include "md5.h"
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#if USE_UNLOCKED_IO
# include "unlocked-io.h"
......@@ -57,10 +59,8 @@
#endif
#define BLOCKSIZE 4096
/* Ensure that BLOCKSIZE is a multiple of 64. */
#if BLOCKSIZE % 64 != 0
/* FIXME-someday (soon?): use #error instead of this kludge. */
"invalid BLOCKSIZE"
# error "invalid BLOCKSIZE"
#endif
/* This array contains the bytes used to pad the buffer to the next
......@@ -90,10 +90,10 @@ md5_init_ctx (struct md5_ctx *ctx)
void *
md5_read_ctx (const struct md5_ctx *ctx, void *resbuf)
{
((md5_uint32 *) resbuf)[0] = SWAP (ctx->A);
((md5_uint32 *) resbuf)[1] = SWAP (ctx->B);
((md5_uint32 *) resbuf)[2] = SWAP (ctx->C);
((md5_uint32 *) resbuf)[3] = SWAP (ctx->D);
((uint32_t *) resbuf)[0] = SWAP (ctx->A);
((uint32_t *) resbuf)[1] = SWAP (ctx->B);
((uint32_t *) resbuf)[2] = SWAP (ctx->C);
((uint32_t *) resbuf)[3] = SWAP (ctx->D);
return resbuf;
}
......@@ -107,24 +107,22 @@ void *
md5_finish_ctx (struct md5_ctx *ctx, void *resbuf)
{
/* Take yet unprocessed bytes into account. */
md5_uint32 bytes = ctx->buflen;
size_t pad;
uint32_t bytes = ctx->buflen;
size_t size = (bytes < 56) ? 64 / 4 : 64 * 2 / 4;
/* Now count remaining bytes. */
ctx->total[0] += bytes;
if (ctx->total[0] < bytes)
++ctx->total[1];
pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
memcpy (&ctx->buffer[bytes], fillbuf, pad);
/* Put the 64-bit file length in *bits* at the end of the buffer. */
*(md5_uint32 *) &ctx->buffer[bytes + pad] = SWAP (ctx->total[0] << 3);
*(md5_uint32 *) &ctx->buffer[bytes + pad + 4] = SWAP ((ctx->total[1] << 3) |
(ctx->total[0] >> 29));
ctx->buffer[size - 2] = SWAP (ctx->total[0] << 3);
ctx->buffer[size - 1] = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29));
memcpy (&((char *) ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes);
/* Process last bytes. */
md5_process_block (ctx->buffer, bytes + pad + 8, ctx);
md5_process_block (ctx->buffer, size * 4, ctx);
return md5_read_ctx (ctx, resbuf);
}
......@@ -146,8 +144,8 @@ md5_stream (FILE *stream, void *resblock)
while (1)
{
/* We read the file in blocks of BLOCKSIZE bytes. One call of the
computation function processes the whole buffer so that with the
next round of the loop another block can be read. */
computation function processes the whole buffer so that with the
next round of the loop another block can be read. */
size_t n;
sum = 0;
......@@ -164,8 +162,8 @@ md5_stream (FILE *stream, void *resblock)
if (n == 0)
{
/* Check for the error flag IFF N == 0, so that we don't
exit the loop after a partial read due to e.g., EAGAIN
or EWOULDBLOCK. */
exit the loop after a partial read due to e.g., EAGAIN
or EWOULDBLOCK. */
if (ferror (stream))
return 1;
goto process_partial_block;
......@@ -179,12 +177,12 @@ md5_stream (FILE *stream, void *resblock)
}
/* Process buffer with BLOCKSIZE bytes. Note that
BLOCKSIZE % 64 == 0
BLOCKSIZE % 64 == 0
*/
md5_process_block (buffer, BLOCKSIZE, &ctx);
}
process_partial_block:;
process_partial_block:
/* Process any remaining bytes. */
if (sum > 0)
......@@ -225,7 +223,7 @@ md5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx)
size_t left_over = ctx->buflen;
size_t add = 128 - left_over > len ? len : 128 - left_over;
memcpy (&ctx->buffer[left_over], buffer, add);
memcpy (&((char *) ctx->buffer)[left_over], buffer, add);
ctx->buflen += add;
if (ctx->buflen > 64)
......@@ -234,7 +232,8 @@ md5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx)
ctx->buflen &= 63;
/* The regions in the following copy operation cannot overlap. */
memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63],
memcpy (ctx->buffer,
&((char *) ctx->buffer)[(left_over + add) & ~63],
ctx->buflen);
}
......@@ -246,8 +245,14 @@ md5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx)
if (len >= 64)
{
#if !_STRING_ARCH_unaligned
# define alignof(type) offsetof (struct { char c; type x; }, x)
# define UNALIGNED_P(p) (((size_t) p) % alignof (md5_uint32) != 0)
/* To check alignment gcc has an appropriate operator. Other
compilers don't. */
# if __GNUC__ >= 2
# define UNALIGNED_P(p) (((uintptr_t) p) % __alignof__ (uint32_t) != 0)
# else
# define alignof(type) offsetof (struct { char c; type x; }, x)
# define UNALIGNED_P(p) (((size_t) p) % alignof (uint32_t) != 0)
# endif
if (UNALIGNED_P (buffer))
while (len > 64)
{
......@@ -269,13 +274,13 @@ md5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx)
{
size_t left_over = ctx->buflen;
memcpy (&ctx->buffer[left_over], buffer, len);
memcpy (&((char *) ctx->buffer)[left_over], buffer, len);
left_over += len;
if (left_over >= 64)
{
md5_process_block (ctx->buffer, 64, ctx);
left_over -= 64;
memcpy (ctx->buffer, &ctx->buffer[64], left_over);
memcpy (ctx->buffer, &ctx->buffer[16], left_over);
}
ctx->buflen = left_over;
}
......@@ -297,14 +302,14 @@ md5_process_bytes (const void *buffer, size_t len, struct md5_ctx *ctx)
void
md5_process_block (const void *buffer, size_t len, struct md5_ctx *ctx)
{
md5_uint32 correct_words[16];
const md5_uint32 *words = buffer;
size_t nwords = len / sizeof (md5_uint32);
const md5_uint32 *endp = words + nwords;
md5_uint32 A = ctx->A;
md5_uint32 B = ctx->B;
md5_uint32 C = ctx->C;
md5_uint32 D = ctx->D;
uint32_t correct_words[16];
const uint32_t *words = buffer;
size_t nwords = len / sizeof (uint32_t);
const uint32_t *endp = words + nwords;
uint32_t A = ctx->A;
uint32_t B = ctx->B;
uint32_t C = ctx->C;
uint32_t D = ctx->D;
/* First increment the byte count. RFC 1321 specifies the possible
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)
the loop. */
while (words < endp)
{
md5_uint32 *cwp = correct_words;
md5_uint32 A_save = A;
md5_uint32 B_save = B;
md5_uint32 C_save = C;
md5_uint32 D_save = D;
uint32_t *cwp = correct_words;
uint32_t A_save = A;
uint32_t B_save = B;
uint32_t C_save = C;
uint32_t D_save = D;
/* First round: using the given function, the context and a constant
the next context is computed. Because the algorithms processing
unit is a 32-bit word and it is determined to work on words in
little endian byte order we perhaps have to change the byte order
before the computation. To reduce the work for the next steps
we store the swapped words in the array CORRECT_WORDS. */
the next context is computed. Because the algorithms processing
unit is a 32-bit word and it is determined to work on words in
little endian byte order we perhaps have to change the byte order
before the computation. To reduce the work for the next steps
we store the swapped words in the array CORRECT_WORDS. */
#define OP(a, b, c, d, s, T) \
do \
{ \
a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T; \
++words; \
a = rol (a, s); \
CYCLIC (a, s); \
a += b; \
} \
while (0)
/* It is unfortunate that C does not provide an operator for
cyclic rotation. Hope the C compiler is smart enough. */
#define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
/* Before we start, one word to the strange constants.
They are defined in RFC 1321 as
They are defined in RFC 1321 as
T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64, or
perl -e 'foreach(1..64){printf "0x%08x\n", int (4294967296 * abs (sin $_))}'
T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
Here is an equivalent invocation using Perl:
perl -e 'foreach(1..64){printf "0x%08x\n", int (4294967296 * abs (sin $_))}'
*/
/* Round 1. */
OP (A, B, C, D, 7, 0xd76aa478);
OP (A, B, C, D, 7, 0xd76aa478);
OP (D, A, B, C, 12, 0xe8c7b756);
OP (C, D, A, B, 17, 0x242070db);
OP (B, C, D, A, 22, 0xc1bdceee);
OP (A, B, C, D, 7, 0xf57c0faf);
OP (A, B, C, D, 7, 0xf57c0faf);
OP (D, A, B, C, 12, 0x4787c62a);
OP (C, D, A, B, 17, 0xa8304613);
OP (B, C, D, A, 22, 0xfd469501);
OP (A, B, C, D, 7, 0x698098d8);
OP (A, B, C, D, 7, 0x698098d8);
OP (D, A, B, C, 12, 0x8b44f7af);
OP (C, D, A, B, 17, 0xffff5bb1);
OP (B, C, D, A, 22, 0x895cd7be);
OP (A, B, C, D, 7, 0x6b901122);
OP (A, B, C, D, 7, 0x6b901122);
OP (D, A, B, C, 12, 0xfd987193);
OP (C, D, A, B, 17, 0xa679438e);
OP (B, C, D, A, 22, 0x49b40821);
/* For the second to fourth round we have the possibly swapped words
in CORRECT_WORDS. Redefine the macro to take an additional first
argument specifying the function to use. */
in CORRECT_WORDS. Redefine the macro to take an additional first
argument specifying the function to use. */
#undef OP
#define OP(f, a, b, c, d, k, s, T) \
do \
do \
{ \
a += f (b, c, d) + correct_words[k] + T; \
a = rol (a, s); \
CYCLIC (a, s); \
a += b; \
} \
while (0)
/* Round 2. */
OP (FG, A, B, C, D, 1, 5, 0xf61e2562);
OP (FG, D, A, B, C, 6, 9, 0xc040b340);
OP (FG, A, B, C, D, 1, 5, 0xf61e2562);
OP (FG, D, A, B, C, 6, 9, 0xc040b340);
OP (FG, C, D, A, B, 11, 14, 0x265e5a51);
OP (FG, B, C, D, A, 0, 20, 0xe9b6c7aa);
OP (FG, A, B, C, D, 5, 5, 0xd62f105d);
OP (FG, D, A, B, C, 10, 9, 0x02441453);
OP (FG, B, C, D, A, 0, 20, 0xe9b6c7aa);
OP (FG, A, B, C, D, 5, 5, 0xd62f105d);
OP (FG, D, A, B, C, 10, 9, 0x02441453);
OP (FG, C, D, A, B, 15, 14, 0xd8a1e681);
OP (FG, B, C, D, A, 4, 20, 0xe7d3fbc8);
OP (FG, A, B, C, D, 9, 5, 0x21e1cde6);
OP (FG, D, A, B, C, 14, 9, 0xc33707d6);
OP (FG, C, D, A, B, 3, 14, 0xf4d50d87);
OP (FG, B, C, D, A, 8, 20, 0x455a14ed);
OP (FG, A, B, C, D, 13, 5, 0xa9e3e905);
OP (FG, D, A, B, C, 2, 9, 0xfcefa3f8);
OP (FG, C, D, A, B, 7, 14, 0x676f02d9);
OP (FG, B, C, D, A, 4, 20, 0xe7d3fbc8);
OP (FG, A, B, C, D, 9, 5, 0x21e1cde6);
OP (FG, D, A, B, C, 14, 9, 0xc33707d6);
OP (FG, C, D, A, B, 3, 14, 0xf4d50d87);
OP (FG, B, C, D, A, 8, 20, 0x455a14ed);
OP (FG, A, B, C, D, 13, 5, 0xa9e3e905);
OP (FG, D, A, B, C, 2, 9, 0xfcefa3f8);
OP (FG, C, D, A, B, 7, 14, 0x676f02d9);
OP (FG, B, C, D, A, 12, 20, 0x8d2a4c8a);
/* Round 3. */
OP (FH, A, B, C, D, 5, 4, 0xfffa3942);
OP (FH, D, A, B, C, 8, 11, 0x8771f681);
OP (FH, A, B, C, D, 5, 4, 0xfffa3942);
OP (FH, D, A, B, C, 8, 11, 0x8771f681);
OP (FH, C, D, A, B, 11, 16, 0x6d9d6122);
OP (FH, B, C, D, A, 14, 23, 0xfde5380c);
OP (FH, A, B, C, D, 1, 4, 0xa4beea44);
OP (FH, D, A, B, C, 4, 11, 0x4bdecfa9);
OP (FH, C, D, A, B, 7, 16, 0xf6bb4b60);
OP (FH, A, B, C, D, 1, 4, 0xa4beea44);
OP (FH, D, A, B, C, 4, 11, 0x4bdecfa9);
OP (FH, C, D, A, B, 7, 16, 0xf6bb4b60);
OP (FH, B, C, D, A, 10, 23, 0xbebfbc70);
OP (FH, A, B, C, D, 13, 4, 0x289b7ec6);
OP (FH, D, A, B, C, 0, 11, 0xeaa127fa);
OP (FH, C, D, A, B, 3, 16, 0xd4ef3085);
OP (FH, B, C, D, A, 6, 23, 0x04881d05);
OP (FH, A, B, C, D, 9, 4, 0xd9d4d039);
OP (FH, A, B, C, D, 13, 4, 0x289b7ec6);
OP (FH, D, A, B, C, 0, 11, 0xeaa127fa);
OP (FH, C, D, A, B, 3, 16, 0xd4ef3085);
OP (FH, B, C, D, A, 6, 23, 0x04881d05);
OP (FH, A, B, C, D, 9, 4, 0xd9d4d039);
OP (FH, D, A, B, C, 12, 11, 0xe6db99e5);
OP (FH, C, D, A, B, 15, 16, 0x1fa27cf8);
OP (FH, B, C, D, A, 2, 23, 0xc4ac5665);
OP (FH, B, C, D, A, 2, 23, 0xc4ac5665);
/* Round 4. */
OP (FI, A, B, C, D, 0, 6, 0xf4292244);
OP (FI, D, A, B, C, 7, 10, 0x432aff97);
OP (FI, A, B, C, D, 0, 6, 0xf4292244);
OP (FI, D, A, B, C, 7, 10, 0x432aff97);
OP (FI, C, D, A, B, 14, 15, 0xab9423a7);
OP (FI, B, C, D, A, 5, 21, 0xfc93a039);
OP (FI, A, B, C, D, 12, 6, 0x655b59c3);
OP (FI, D, A, B, C, 3, 10, 0x8f0ccc92);
OP (FI, B, C, D, A, 5, 21, 0xfc93a039);
OP (FI, A, B, C, D, 12, 6, 0x655b59c3);
OP (FI, D, A, B, C, 3, 10, 0x8f0ccc92);
OP (FI, C, D, A, B, 10, 15, 0xffeff47d);
OP (FI, B, C, D, A, 1, 21, 0x85845dd1);
OP (FI, A, B, C, D, 8, 6, 0x6fa87e4f);
OP (FI, B, C, D, A, 1, 21, 0x85845dd1);
OP (FI, A, B, C, D, 8, 6, 0x6fa87e4f);
OP (FI, D, A, B, C, 15, 10, 0xfe2ce6e0);
OP (FI, C, D, A, B, 6, 15, 0xa3014314);
OP (FI, C, D, A, B, 6, 15, 0xa3014314);
OP (FI, B, C, D, A, 13, 21, 0x4e0811a1);
OP (FI, A, B, C, D, 4, 6, 0xf7537e82);
OP (FI, A, B, C, D, 4, 6, 0xf7537e82);
OP (FI, D, A, B, C, 11, 10, 0xbd3af235);
OP (FI, C, D, A, B, 2, 15, 0x2ad7d2bb);
OP (FI, B, C, D, A, 9, 21, 0xeb86d391);
OP (FI, C, D, A, B, 2, 15, 0x2ad7d2bb);
OP (FI, B, C, D, A, 9, 21, 0xeb86d391);
/* Add the starting values of the context. */
A += A_save;
......
/* md5.h - Declaration of functions and data types used for MD5 sum
computing library functions.
Copyright (C) 1995, 1996, 1999, 2000, 2003, 2004 Free Software
Foundation, Inc.
NOTE: The canonical source of this file is maintained with the GNU C
Library. Bugs can be reported to bug-glibc@prep.ai.mit.edu.
/* Declaration of functions and data types used for MD5 sum computing
library functions.
Copyright (C) 1995-1997,1999,2000,2001,2004,2005
Free Software Foundation, Inc.
This file is part of the GNU C Library.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
......@@ -25,27 +22,55 @@
#define _MD5_H 1
#include <stdio.h>
#include <stdint.h>
#define MD5_DIGEST_SIZE 16
#define MD5_BLOCK_SIZE 64
#ifndef __GNUC_PREREQ
# if defined __GNUC__ && defined __GNUC_MINOR__
# define __GNUC_PREREQ(maj, min) \
((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
# else
# define __GNUC_PREREQ(maj, min) 0
# endif
#endif
#if HAVE_INTTYPES_H
# include <inttypes.h>
#ifndef __THROW
# if defined __cplusplus && __GNUC_PREREQ (2,8)
# define __THROW throw ()
# else
# define __THROW
# endif
#endif
#if HAVE_STDINT_H || _LIBC
# include <stdint.h>
#ifndef __attribute__
# if ! __GNUC_PREREQ (2,8) || __STRICT_ANSI__
# define __attribute__(x)
# endif
#endif
typedef uint32_t md5_uint32;
#ifndef _LIBC
# define __md5_buffer md5_buffer
# define __md5_finish_ctx md5_finish_ctx
# define __md5_init_ctx md5_init_ctx
# define __md5_process_block md5_process_block
# define __md5_process_bytes md5_process_bytes
# define __md5_read_ctx md5_read_ctx
# define __md5_stream md5_stream
#endif
/* Structure to save state of computation between the single steps. */
struct md5_ctx
{
md5_uint32 A;
md5_uint32 B;
md5_uint32 C;
md5_uint32 D;
md5_uint32 total[2];
md5_uint32 buflen;
char buffer[128];
uint32_t A;
uint32_t B;
uint32_t C;
uint32_t D;
uint32_t total[2];
uint32_t buflen;
uint32_t buffer[32];
};
/*
......@@ -55,52 +80,51 @@ struct md5_ctx
/* Initialize structure containing state of computation.
(RFC 1321, 3.3: Step 3) */
extern void md5_init_ctx (struct md5_ctx *ctx);
extern void __md5_init_ctx (struct md5_ctx *ctx) __THROW;
/* Starting with the result of former calls of this function (or the
initialization function update the context for the next LEN bytes
starting at BUFFER.
It is necessary that LEN is a multiple of 64!!! */
extern void md5_process_block (const void *buffer, size_t len,
struct md5_ctx *ctx);
extern void __md5_process_block (const void *buffer, size_t len,
struct md5_ctx *ctx) __THROW;
/* Starting with the result of former calls of this function (or the
initialization function update the context for the next LEN bytes
starting at BUFFER.
It is NOT required that LEN is a multiple of 64. */
extern void md5_process_bytes (const void *buffer, size_t len,
struct md5_ctx *ctx);
extern void __md5_process_bytes (const void *buffer, size_t len,
struct md5_ctx *ctx) __THROW;
/* Process the remaining bytes in the buffer and put result from CTX
in first 16 bytes following RESBUF. The result is always in little
endian byte order, so that a byte-wise output yields to the wanted
ASCII representation of the message digest.
IMPORTANT: On some systems it is required that RESBUF be correctly
aligned for a 32 bits value. */
extern void *md5_finish_ctx (struct md5_ctx *ctx, void *resbuf);
IMPORTANT: On some systems, RESBUF must be aligned to a 32-bit
boundary. */
extern void *__md5_finish_ctx (struct md5_ctx *ctx, void *resbuf) __THROW;
/* Put result from CTX in first 16 bytes following RESBUF. The result is
always in little endian byte order, so that a byte-wise output yields
to the wanted ASCII representation of the message digest.
IMPORTANT: On some systems it is required that RESBUF is correctly
aligned for a 32 bits value. */
extern void *md5_read_ctx (const struct md5_ctx *ctx, void *resbuf);
IMPORTANT: On some systems, RESBUF must be aligned to a 32-bit
boundary. */
extern void *__md5_read_ctx (const struct md5_ctx *ctx, void *resbuf) __THROW;
/* Compute MD5 message digest for bytes read from STREAM. The
resulting message digest number will be written into the 16 bytes
beginning at RESBLOCK. */
extern int md5_stream (FILE *stream, void *resblock);
extern int __md5_stream (FILE *stream, void *resblock) __THROW;
/* Compute MD5 message digest for LEN bytes beginning at BUFFER. The
result is always in little endian byte order, so that a byte-wise
output yields to the wanted ASCII representation of the message
digest. */
extern void *md5_buffer (const char *buffer, size_t len, void *resblock);
#define rol(x,n) ( ((x) << (n)) | ((x) >> (32-(n))) )
extern void *__md5_buffer (const char *buffer, size_t len,
void *resblock) __THROW;
#endif
#endif /* md5.h */
......
......@@ -21,9 +21,8 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
USA. */
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
#ifdef HAVE_CONFIG_H
# include <config.h>
......
......@@ -18,12 +18,11 @@
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
static reg_errcode_t re_compile_internal (regex_t *preg, const char * pattern,
int length, reg_syntax_t syntax);
Idx length, reg_syntax_t syntax);
static void re_compile_fastmap_iter (regex_t *bufp,
const re_dfastate_t *init_state,
char *fastmap);
static reg_errcode_t init_dfa (re_dfa_t *dfa, int pat_len);
static void init_word_char (re_dfa_t *dfa);
static reg_errcode_t init_dfa (re_dfa_t *dfa, Idx pat_len);
#ifdef RE_ENABLE_I18N
static void free_charset (re_charset_t *cset);
#endif /* RE_ENABLE_I18N */
......@@ -33,7 +32,6 @@ static reg_errcode_t create_initial_state (re_dfa_t *dfa);
static void optimize_utf8 (re_dfa_t *dfa);
#endif
static reg_errcode_t analyze (regex_t *preg);
static reg_errcode_t create_initial_state (re_dfa_t *dfa);
static reg_errcode_t preorder (bin_tree_t *root,
reg_errcode_t (fn (void *, bin_tree_t *)),
void *extra);
......@@ -47,39 +45,31 @@ static bin_tree_t *lower_subexp (reg_errcode_t *err, regex_t *preg,
static reg_errcode_t calc_first (void *extra, bin_tree_t *node);
static reg_errcode_t calc_next (void *extra, bin_tree_t *node);
static reg_errcode_t link_nfa_nodes (void *extra, bin_tree_t *node);
static reg_errcode_t duplicate_node_closure (re_dfa_t *dfa, int top_org_node,
int top_clone_node, int root_node,
unsigned int constraint);
static reg_errcode_t duplicate_node (int *new_idx, re_dfa_t *dfa, int org_idx,
unsigned int constraint);
static int search_duplicated_node (re_dfa_t *dfa, int org_node,
static Idx duplicate_node (re_dfa_t *dfa, Idx org_idx, unsigned int constraint);
static Idx search_duplicated_node (const re_dfa_t *dfa, Idx org_node,
unsigned int constraint);
static reg_errcode_t calc_eclosure (re_dfa_t *dfa);
static reg_errcode_t calc_eclosure_iter (re_node_set *new_set, re_dfa_t *dfa,
int node, int root);
Idx node, bool root);
static reg_errcode_t calc_inveclosure (re_dfa_t *dfa);
static int fetch_number (re_string_t *input, re_token_t *token,
reg_syntax_t syntax);
static void fetch_token (re_token_t *result, re_string_t *input,
static Idx fetch_number (re_string_t *input, re_token_t *token,
reg_syntax_t syntax);
static int peek_token (re_token_t *token, re_string_t *input,
reg_syntax_t syntax);
static int peek_token_bracket (re_token_t *token, re_string_t *input,
reg_syntax_t syntax);
static bin_tree_t *parse (re_string_t *regexp, regex_t *preg,
reg_syntax_t syntax, reg_errcode_t *err);
static bin_tree_t *parse_reg_exp (re_string_t *regexp, regex_t *preg,
re_token_t *token, reg_syntax_t syntax,
int nest, reg_errcode_t *err);
Idx nest, reg_errcode_t *err);
static bin_tree_t *parse_branch (re_string_t *regexp, regex_t *preg,
re_token_t *token, reg_syntax_t syntax,
int nest, reg_errcode_t *err);
Idx nest, reg_errcode_t *err);
static bin_tree_t *parse_expression (re_string_t *regexp, regex_t *preg,
re_token_t *token, reg_syntax_t syntax,
int nest, reg_errcode_t *err);
Idx nest, reg_errcode_t *err);
static bin_tree_t *parse_sub_exp (re_string_t *regexp, regex_t *preg,
re_token_t *token, reg_syntax_t syntax,
int nest, reg_errcode_t *err);
Idx nest, reg_errcode_t *err);
static bin_tree_t *parse_dup_op (bin_tree_t *dup_elem, re_string_t *regexp,
re_dfa_t *dfa, re_token_t *token,
reg_syntax_t syntax, reg_errcode_t *err);
......@@ -91,52 +81,34 @@ static reg_errcode_t parse_bracket_element (bracket_elem_t *elem,
re_token_t *token, int token_len,
re_dfa_t *dfa,
reg_syntax_t syntax,
int accept_hyphen);
bool accept_hyphen);
static reg_errcode_t parse_bracket_symbol (bracket_elem_t *elem,
re_string_t *regexp,
re_token_t *token);
#ifndef _LIBC
# ifdef RE_ENABLE_I18N
static reg_errcode_t build_range_exp (re_bitset_ptr_t sbcset,
re_charset_t *mbcset, int *range_alloc,
bracket_elem_t *start_elem,
bracket_elem_t *end_elem);
static reg_errcode_t build_collating_symbol (re_bitset_ptr_t sbcset,
re_charset_t *mbcset,
int *coll_sym_alloc,
const unsigned char *name);
# else /* not RE_ENABLE_I18N */
static reg_errcode_t build_range_exp (re_bitset_ptr_t sbcset,
bracket_elem_t *start_elem,
bracket_elem_t *end_elem);
static reg_errcode_t build_collating_symbol (re_bitset_ptr_t sbcset,
const unsigned char *name);
# endif /* not RE_ENABLE_I18N */
#endif /* not _LIBC */
#ifdef RE_ENABLE_I18N
static reg_errcode_t build_equiv_class (re_bitset_ptr_t sbcset,
static reg_errcode_t build_equiv_class (bitset sbcset,
re_charset_t *mbcset,
int *equiv_class_alloc,
Idx *equiv_class_alloc,
const unsigned char *name);
static reg_errcode_t build_charclass (unsigned RE_TRANSLATE_TYPE trans,
re_bitset_ptr_t sbcset,
static reg_errcode_t build_charclass (unsigned REG_TRANSLATE_TYPE trans,
bitset sbcset,
re_charset_t *mbcset,
int *char_class_alloc,
Idx *char_class_alloc,
const unsigned char *class_name,
reg_syntax_t syntax);
#else /* not RE_ENABLE_I18N */
static reg_errcode_t build_equiv_class (re_bitset_ptr_t sbcset,
static reg_errcode_t build_equiv_class (bitset sbcset,
const unsigned char *name);
static reg_errcode_t build_charclass (unsigned RE_TRANSLATE_TYPE trans,
re_bitset_ptr_t sbcset,
static reg_errcode_t build_charclass (unsigned REG_TRANSLATE_TYPE trans,
bitset sbcset,
const unsigned char *class_name,
reg_syntax_t syntax);
#endif /* not RE_ENABLE_I18N */
static bin_tree_t *build_charclass_op (re_dfa_t *dfa,
unsigned RE_TRANSLATE_TYPE trans,
unsigned REG_TRANSLATE_TYPE trans,
const unsigned char *class_name,
const unsigned char *extra,
int non_match, reg_errcode_t *err);
bool non_match, reg_errcode_t *err);
static bin_tree_t *create_tree (re_dfa_t *dfa,
bin_tree_t *left, bin_tree_t *right,
re_token_type_t type);
......@@ -234,24 +206,22 @@ const size_t __re_error_msgid_idx[] attribute_hidden =
compiles PATTERN (of length LENGTH) and puts the result in BUFP.
Returns 0 if the pattern was valid, otherwise an error string.
Assumes the `allocated' (and perhaps `buffer') and `translate' fields
Assumes the `re_allocated' (and perhaps `re_buffer') and `translate' fields
are set in BUFP on entry. */
const char *
re_compile_pattern (pattern, length, bufp)
const char *pattern;
size_t length;
struct re_pattern_buffer *bufp;
re_compile_pattern (const char *pattern, size_t length,
struct re_pattern_buffer *bufp)
{
reg_errcode_t ret;
/* And GNU code determines whether or not to get register information
by passing null for the REGS argument to re_match, etc., not by
setting no_sub, unless RE_NO_SUB is set. */
bufp->no_sub = !!(re_syntax_options & RE_NO_SUB);
setting re_no_sub, unless REG_NO_SUB is set. */
bufp->re_no_sub = !!(re_syntax_options & REG_NO_SUB);
/* Match anchors at newline. */
bufp->newline_anchor = 1;
bufp->re_newline_anchor = 1;
ret = re_compile_internal (bufp, pattern, length, re_syntax_options);
......@@ -279,8 +249,7 @@ reg_syntax_t re_syntax_options;
defined in regex.h. We return the old syntax. */
reg_syntax_t
re_set_syntax (syntax)
reg_syntax_t syntax;
re_set_syntax (reg_syntax_t syntax)
{
reg_syntax_t ret = re_syntax_options;
......@@ -292,11 +261,10 @@ weak_alias (__re_set_syntax, re_set_syntax)
#endif
int
re_compile_fastmap (bufp)
struct re_pattern_buffer *bufp;
re_compile_fastmap (struct re_pattern_buffer *bufp)
{
re_dfa_t *dfa = (re_dfa_t *) bufp->buffer;
char *fastmap = bufp->fastmap;
re_dfa_t *dfa = (re_dfa_t *) bufp->re_buffer;
char *fastmap = bufp->re_fastmap;
memset (fastmap, '\0', sizeof (char) * SBC_MAX);
re_compile_fastmap_iter (bufp, dfa->init_state, fastmap);
......@@ -306,7 +274,7 @@ re_compile_fastmap (bufp)
re_compile_fastmap_iter (bufp, dfa->init_state_nl, fastmap);
if (dfa->init_state != dfa->init_state_begbuf)
re_compile_fastmap_iter (bufp, dfa->init_state_begbuf, fastmap);
bufp->fastmap_accurate = 1;
bufp->re_fastmap_accurate = 1;
return 0;
}
#ifdef _LIBC
......@@ -315,7 +283,7 @@ weak_alias (__re_compile_fastmap, re_compile_fastmap)
static inline void
__attribute ((always_inline))
re_set_fastmap (char *fastmap, int icase, int ch)
re_set_fastmap (char *fastmap, bool icase, int ch)
{
fastmap[ch] = 1;
if (icase)
......@@ -326,26 +294,25 @@ re_set_fastmap (char *fastmap, int icase, int ch)
Compile fastmap for the initial_state INIT_STATE. */
static void
re_compile_fastmap_iter (bufp, init_state, fastmap)
regex_t *bufp;
const re_dfastate_t *init_state;
char *fastmap;
re_compile_fastmap_iter (regex_t *bufp, const re_dfastate_t *init_state,
char *fastmap)
{
re_dfa_t *dfa = (re_dfa_t *) bufp->buffer;
int node_cnt;
int icase = (dfa->mb_cur_max == 1 && (bufp->syntax & RE_ICASE));
re_dfa_t *dfa = (re_dfa_t *) bufp->re_buffer;
Idx node_cnt;
bool icase = (dfa->mb_cur_max == 1 && (bufp->re_syntax & REG_IGNORE_CASE));
for (node_cnt = 0; node_cnt < init_state->nodes.nelem; ++node_cnt)
{
int node = init_state->nodes.elems[node_cnt];
Idx node = init_state->nodes.elems[node_cnt];
re_token_type_t type = dfa->nodes[node].type;
if (type == CHARACTER)
{
re_set_fastmap (fastmap, icase, dfa->nodes[node].opr.c);
#ifdef RE_ENABLE_I18N
if ((bufp->syntax & RE_ICASE) && dfa->mb_cur_max > 1)
if ((bufp->re_syntax & REG_IGNORE_CASE) && dfa->mb_cur_max > 1)
{
unsigned char *buf = alloca (dfa->mb_cur_max), *p;
unsigned char buf[MB_LEN_MAX];
unsigned char *p;
wchar_t wc;
mbstate_t state;
......@@ -360,22 +327,22 @@ re_compile_fastmap_iter (bufp, init_state, fastmap)
&state) == p - buf
&& (__wcrtomb ((char *) buf, towlower (wc), &state)
!= (size_t) -1))
re_set_fastmap (fastmap, 0, buf[0]);
re_set_fastmap (fastmap, false, buf[0]);
}
#endif
}
else if (type == SIMPLE_BRACKET)
{
int i, j, ch;
for (i = 0, ch = 0; i < BITSET_UINTS; ++i)
for (j = 0; j < UINT_BITS; ++j, ++ch)
if (dfa->nodes[node].opr.sbcset[i] & (1 << j))
for (i = 0, ch = 0; i < BITSET_WORDS; ++i)
for (j = 0; j < BITSET_WORD_BITS; ++j, ++ch)
if (dfa->nodes[node].opr.sbcset[i] & ((bitset_word) 1 << j))
re_set_fastmap (fastmap, icase, ch);
}
#ifdef RE_ENABLE_I18N
else if (type == COMPLEX_BRACKET)
{
int i;
Idx i;
re_charset_t *cset = dfa->nodes[node].opr.mbcset;
if (cset->non_match || cset->ncoll_syms || cset->nequiv_classes
|| cset->nranges || cset->nchar_classes)
......@@ -389,13 +356,11 @@ re_compile_fastmap_iter (bufp, init_state, fastmap)
is a valid collation element, and don't catch
'b' since 'b' is the only collation element
which starts from 'b'. */
int j, ch;
const int32_t *table = (const int32_t *)
_NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
for (i = 0, ch = 0; i < BITSET_UINTS; ++i)
for (j = 0; j < UINT_BITS; ++j, ++ch)
if (table[ch] < 0)
re_set_fastmap (fastmap, icase, ch);
for (i = 0; i < SBC_MAX; ++i)
if (table[i] < 0)
re_set_fastmap (fastmap, icase, i);
}
# else
if (dfa->mb_cur_max > 1)
......@@ -411,11 +376,11 @@ re_compile_fastmap_iter (bufp, init_state, fastmap)
memset (&state, '\0', sizeof (state));
if (__wcrtomb (buf, cset->mbchars[i], &state) != (size_t) -1)
re_set_fastmap (fastmap, icase, *(unsigned char *) buf);
if ((bufp->syntax & RE_ICASE) && dfa->mb_cur_max > 1)
if ((bufp->re_syntax & REG_IGNORE_CASE) && dfa->mb_cur_max > 1)
{
if (__wcrtomb (buf, towlower (cset->mbchars[i]), &state)
!= (size_t) -1)
re_set_fastmap (fastmap, 0, *(unsigned char *) buf);
re_set_fastmap (fastmap, false, *(unsigned char *) buf);
}
}
}
......@@ -428,7 +393,7 @@ re_compile_fastmap_iter (bufp, init_state, fastmap)
{
memset (fastmap, '\1', sizeof (char) * SBC_MAX);
if (type == END_OF_RE)
bufp->can_be_null = 1;
bufp->re_can_be_null = 1;
return;
}
}
......@@ -440,14 +405,14 @@ re_compile_fastmap_iter (bufp, init_state, fastmap)
PREG is a regex_t *. We do not expect any fields to be initialized,
since POSIX says we shouldn't. Thus, we set
`buffer' to the compiled pattern;
`used' to the length of the compiled pattern;
`syntax' to RE_SYNTAX_POSIX_EXTENDED if the
`re_buffer' to the compiled pattern;
`re_used' to the length of the compiled pattern;
`re_syntax' to REG_SYNTAX_POSIX_EXTENDED if the
REG_EXTENDED bit in CFLAGS is set; otherwise, to
RE_SYNTAX_POSIX_BASIC;
`newline_anchor' to REG_NEWLINE being set in CFLAGS;
`fastmap' to an allocated space for the fastmap;
`fastmap_accurate' to zero;
REG_SYNTAX_POSIX_BASIC;
`re_newline_anchor' to REG_NEWLINE being set in CFLAGS;
`re_fastmap' to an allocated space for the fastmap;
`re_fastmap_accurate' to zero;
`re_nsub' to the number of subexpressions in PATTERN.
PATTERN is the address of the pattern string.
......@@ -471,38 +436,35 @@ re_compile_fastmap_iter (bufp, init_state, fastmap)
the return codes and their meanings.) */
int
regcomp (preg, pattern, cflags)
regex_t *__restrict preg;
const char *__restrict pattern;
int cflags;
regcomp (regex_t *__restrict preg, const char *__restrict pattern, int cflags)
{
reg_errcode_t ret;
reg_syntax_t syntax = ((cflags & REG_EXTENDED) ? RE_SYNTAX_POSIX_EXTENDED
: RE_SYNTAX_POSIX_BASIC);
reg_syntax_t syntax = ((cflags & REG_EXTENDED) ? REG_SYNTAX_POSIX_EXTENDED
: REG_SYNTAX_POSIX_BASIC);
preg->buffer = NULL;
preg->allocated = 0;
preg->used = 0;
preg->re_buffer = NULL;
preg->re_allocated = 0;
preg->re_used = 0;
/* Try to allocate space for the fastmap. */
preg->fastmap = re_malloc (char, SBC_MAX);
if (BE (preg->fastmap == NULL, 0))
preg->re_fastmap = re_malloc (char, SBC_MAX);
if (BE (preg->re_fastmap == NULL, 0))
return REG_ESPACE;
syntax |= (cflags & REG_ICASE) ? RE_ICASE : 0;
syntax |= (cflags & REG_ICASE) ? REG_IGNORE_CASE : 0;
/* If REG_NEWLINE is set, newlines are treated differently. */
if (cflags & REG_NEWLINE)
{ /* REG_NEWLINE implies neither . nor [^...] match newline. */
syntax &= ~RE_DOT_NEWLINE;
syntax |= RE_HAT_LISTS_NOT_NEWLINE;
syntax &= ~REG_DOT_NEWLINE;
syntax |= REG_HAT_LISTS_NOT_NEWLINE;
/* It also changes the matching behavior. */
preg->newline_anchor = 1;
preg->re_newline_anchor = 1;
}
else
preg->newline_anchor = 0;
preg->no_sub = !!(cflags & REG_NOSUB);
preg->translate = NULL;
preg->re_newline_anchor = 0;
preg->re_no_sub = !!(cflags & REG_NOSUB);
preg->re_translate = NULL;
ret = re_compile_internal (preg, pattern, strlen (pattern), syntax);
......@@ -511,7 +473,7 @@ regcomp (preg, pattern, cflags)
if (ret == REG_ERPAREN)
ret = REG_EPAREN;
/* We have already checked preg->fastmap != NULL. */
/* We have already checked preg->re_fastmap != NULL. */
if (BE (ret == REG_NOERROR, 1))
/* Compute the fastmap now, since regexec cannot modify the pattern
buffer. This function never fails in this implementation. */
......@@ -519,8 +481,8 @@ regcomp (preg, pattern, cflags)
else
{
/* Some error occurred while compiling the expression. */
re_free (preg->fastmap);
preg->fastmap = NULL;
re_free (preg->re_fastmap);
preg->re_fastmap = NULL;
}
return (int) ret;
......@@ -533,11 +495,8 @@ weak_alias (__regcomp, regcomp)
from either regcomp or regexec. We don't use PREG here. */
size_t
regerror (errcode, preg, errbuf, errbuf_size)
int errcode;
const regex_t *preg;
char *errbuf;
size_t errbuf_size;
regerror (int errcode, const regex_t *__restrict preg,
char *__restrict errbuf, size_t errbuf_size)
{
const char *msg;
size_t msg_size;
......@@ -585,11 +544,22 @@ weak_alias (__regerror, regerror)
static const bitset utf8_sb_map =
{
/* Set the first 128 bits. */
# if UINT_MAX == 0xffffffff
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff
# else
# error "Add case for new unsigned int size"
# if 2 < BITSET_WORDS
BITSET_WORD_MAX,
# endif
# if 4 < BITSET_WORDS
BITSET_WORD_MAX,
# endif
# if 6 < BITSET_WORDS
BITSET_WORD_MAX,
# endif
# if 8 < BITSET_WORDS
# error "Invalid BITSET_WORDS"
# endif
(BITSET_WORD_MAX
>> (SBC_MAX % BITSET_WORD_BITS == 0
? 0
: BITSET_WORD_BITS - SBC_MAX % BITSET_WORD_BITS))
};
#endif
......@@ -597,7 +567,7 @@ static const bitset utf8_sb_map =
static void
free_dfa_content (re_dfa_t *dfa)
{
int i, j;
Idx i, j;
if (dfa->nodes)
for (i = 0; i < dfa->nodes_len; ++i)
......@@ -645,20 +615,19 @@ free_dfa_content (re_dfa_t *dfa)
/* Free dynamically allocated space used by PREG. */
void
regfree (preg)
regex_t *preg;
regfree (regex_t *preg)
{
re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
if (BE (dfa != NULL, 1))
free_dfa_content (dfa);
preg->buffer = NULL;
preg->allocated = 0;
preg->re_buffer = NULL;
preg->re_allocated = 0;
re_free (preg->fastmap);
preg->fastmap = NULL;
re_free (preg->re_fastmap);
preg->re_fastmap = NULL;
re_free (preg->translate);
preg->translate = NULL;
re_free (preg->re_translate);
preg->re_translate = NULL;
}
#ifdef _LIBC
weak_alias (__regfree, regfree)
......@@ -679,32 +648,31 @@ char *
regcomp/regexec above without link errors. */
weak_function
# endif
re_comp (s)
const char *s;
re_comp (const char *s)
{
reg_errcode_t ret;
char *fastmap;
if (!s)
{
if (!re_comp_buf.buffer)
if (!re_comp_buf.re_buffer)
return gettext ("No previous regular expression");
return 0;
}
if (re_comp_buf.buffer)
if (re_comp_buf.re_buffer)
{
fastmap = re_comp_buf.fastmap;
re_comp_buf.fastmap = NULL;
fastmap = re_comp_buf.re_fastmap;
re_comp_buf.re_fastmap = NULL;
__regfree (&re_comp_buf);
memset (&re_comp_buf, '\0', sizeof (re_comp_buf));
re_comp_buf.fastmap = fastmap;
re_comp_buf.re_fastmap = fastmap;
}
if (re_comp_buf.fastmap == NULL)
if (re_comp_buf.re_fastmap == NULL)
{
re_comp_buf.fastmap = (char *) malloc (SBC_MAX);
if (re_comp_buf.fastmap == NULL)
re_comp_buf.re_fastmap = (char *) malloc (SBC_MAX);
if (re_comp_buf.re_fastmap == NULL)
return (char *) gettext (__re_error_msgid
+ __re_error_msgid_idx[(int) REG_ESPACE]);
}
......@@ -713,7 +681,7 @@ re_comp (s)
don't need to initialize the pattern buffer fields which affect it. */
/* Match anchors at newlines. */
re_comp_buf.newline_anchor = 1;
re_comp_buf.re_newline_anchor = 1;
ret = re_compile_internal (&re_comp_buf, s, strlen (s), re_syntax_options);
......@@ -738,40 +706,37 @@ libc_freeres_fn (free_mem)
SYNTAX indicate regular expression's syntax. */
static reg_errcode_t
re_compile_internal (preg, pattern, length, syntax)
regex_t *preg;
const char * pattern;
int length;
reg_syntax_t syntax;
re_compile_internal (regex_t *preg, const char *pattern, Idx length,
reg_syntax_t syntax)
{
reg_errcode_t err = REG_NOERROR;
re_dfa_t *dfa;
re_string_t regexp;
/* Initialize the pattern buffer. */
preg->fastmap_accurate = 0;
preg->syntax = syntax;
preg->not_bol = preg->not_eol = 0;
preg->used = 0;
preg->re_fastmap_accurate = 0;
preg->re_syntax = syntax;
preg->re_not_bol = preg->re_not_eol = 0;
preg->re_used = 0;
preg->re_nsub = 0;
preg->can_be_null = 0;
preg->regs_allocated = REGS_UNALLOCATED;
preg->re_can_be_null = 0;
preg->re_regs_allocated = REG_UNALLOCATED;
/* Initialize the dfa. */
dfa = (re_dfa_t *) preg->buffer;
if (BE (preg->allocated < sizeof (re_dfa_t), 0))
dfa = (re_dfa_t *) preg->re_buffer;
if (BE (preg->re_allocated < sizeof (re_dfa_t), 0))
{
/* If zero allocated, but buffer is non-null, try to realloc
enough space. This loses if buffer's address is bogus, but
that is the user's responsibility. If ->buffer is NULL this
that is the user's responsibility. If buffer is null this
is a simple allocation. */
dfa = re_realloc (preg->buffer, re_dfa_t, 1);
dfa = re_realloc (preg->re_buffer, re_dfa_t, 1);
if (dfa == NULL)
return REG_ESPACE;
preg->allocated = sizeof (re_dfa_t);
preg->buffer = (unsigned char *) dfa;
preg->re_allocated = sizeof (re_dfa_t);
preg->re_buffer = (unsigned char *) dfa;
}
preg->used = sizeof (re_dfa_t);
preg->re_used = sizeof (re_dfa_t);
__libc_lock_init (dfa->lock);
......@@ -779,8 +744,8 @@ re_compile_internal (preg, pattern, length, syntax)
if (BE (err != REG_NOERROR, 0))
{
free_dfa_content (dfa);
preg->buffer = NULL;
preg->allocated = 0;
preg->re_buffer = NULL;
preg->re_allocated = 0;
return err;
}
#ifdef DEBUG
......@@ -788,16 +753,16 @@ re_compile_internal (preg, pattern, length, syntax)
strncpy (dfa->re_str, pattern, length + 1);
#endif
err = re_string_construct (&regexp, pattern, length, preg->translate,
syntax & RE_ICASE, dfa);
err = re_string_construct (&regexp, pattern, length, preg->re_translate,
syntax & REG_IGNORE_CASE, dfa);
if (BE (err != REG_NOERROR, 0))
{
re_compile_internal_free_return:
free_workarea_compile (preg);
re_string_destruct (&regexp);
free_dfa_content (dfa);
preg->buffer = NULL;
preg->allocated = 0;
preg->re_buffer = NULL;
preg->re_allocated = 0;
return err;
}
......@@ -814,7 +779,7 @@ re_compile_internal (preg, pattern, length, syntax)
#ifdef RE_ENABLE_I18N
/* If possible, do searching in single byte encoding to speed things up. */
if (dfa->is_utf8 && !(syntax & RE_ICASE) && preg->translate == NULL)
if (dfa->is_utf8 && !(syntax & REG_IGNORE_CASE) && preg->re_translate == NULL)
optimize_utf8 (dfa);
#endif
......@@ -828,8 +793,8 @@ re_compile_internal (preg, pattern, length, syntax)
if (BE (err != REG_NOERROR, 0))
{
free_dfa_content (dfa);
preg->buffer = NULL;
preg->allocated = 0;
preg->re_buffer = NULL;
preg->re_allocated = 0;
}
return err;
......@@ -839,11 +804,9 @@ re_compile_internal (preg, pattern, length, syntax)
as the initial length of some arrays. */
static reg_errcode_t
init_dfa (dfa, pat_len)
re_dfa_t *dfa;
int pat_len;
init_dfa (re_dfa_t *dfa, Idx pat_len)
{
int table_size;
__re_size_t table_size;
#ifndef _LIBC
char *codeset_name;
#endif
......@@ -854,16 +817,14 @@ init_dfa (dfa, pat_len)
dfa->str_tree_storage_idx = BIN_TREE_STORAGE_SIZE;
dfa->nodes_alloc = pat_len + 1;
dfa->nodes = re_malloc (re_token_t, dfa->nodes_alloc);
dfa->states_alloc = pat_len + 1;
dfa->nodes = re_xmalloc (re_token_t, dfa->nodes_alloc);
/* table_size = 2 ^ ceil(log pat_len) */
for (table_size = 1; table_size > 0; table_size <<= 1)
if (table_size > pat_len)
break;
for (table_size = 1; table_size <= pat_len; table_size <<= 1)
if (0 < (Idx) -1 && table_size == 0)
return REG_ESPACE;
dfa->state_table = calloc (sizeof (struct re_state_table_entry), table_size);
dfa->state_table = re_calloc (struct re_state_table_entry, table_size);
dfa->state_hash_mask = table_size - 1;
dfa->mb_cur_max = MB_CUR_MAX;
......@@ -906,20 +867,17 @@ init_dfa (dfa, pat_len)
{
int i, j, ch;
dfa->sb_char = (re_bitset_ptr_t) calloc (sizeof (bitset), 1);
dfa->sb_char = re_calloc (bitset_word, BITSET_WORDS);
if (BE (dfa->sb_char == NULL, 0))
return REG_ESPACE;
/* Clear all bits by, then set those corresponding to single
byte chars. */
bitset_empty (dfa->sb_char);
for (i = 0, ch = 0; i < BITSET_UINTS; ++i)
for (j = 0; j < UINT_BITS; ++j, ++ch)
/* Set the bits corresponding to single byte chars. */
for (i = 0, ch = 0; i < BITSET_WORDS; ++i)
for (j = 0; j < BITSET_WORD_BITS; ++j, ++ch)
{
wint_t wch = __btowc (ch);
if (wch != WEOF)
dfa->sb_char[i] |= 1 << j;
dfa->sb_char[i] |= (bitset_word) 1 << j;
# ifndef _LIBC
if (isascii (ch) && wch != ch)
dfa->map_notascii = 1;
......@@ -939,24 +897,22 @@ init_dfa (dfa, pat_len)
character used by some operators like "\<", "\>", etc. */
static void
init_word_char (dfa)
re_dfa_t *dfa;
init_word_char (re_dfa_t *dfa)
{
int i, j, ch;
dfa->word_ops_used = 1;
for (i = 0, ch = 0; i < BITSET_UINTS; ++i)
for (j = 0; j < UINT_BITS; ++j, ++ch)
for (i = 0, ch = 0; i < BITSET_WORDS; ++i)
for (j = 0; j < BITSET_WORD_BITS; ++j, ++ch)
if (isalnum (ch) || ch == '_')
dfa->word_char[i] |= 1 << j;
dfa->word_char[i] |= (bitset_word) 1 << j;
}
/* Free the work area which are only used while compiling. */
static void
free_workarea_compile (preg)
regex_t *preg;
free_workarea_compile (regex_t *preg)
{
re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
bin_tree_storage_t *storage, *next;
for (storage = dfa->str_tree_storage; storage; storage = next)
{
......@@ -973,10 +929,9 @@ free_workarea_compile (preg)
/* Create initial states for all contexts. */
static reg_errcode_t
create_initial_state (dfa)
re_dfa_t *dfa;
create_initial_state (re_dfa_t *dfa)
{
int first, i;
Idx first, i;
reg_errcode_t err;
re_node_set init_nodes;
......@@ -995,10 +950,10 @@ create_initial_state (dfa)
if (dfa->nbackref > 0)
for (i = 0; i < init_nodes.nelem; ++i)
{
int node_idx = init_nodes.elems[i];
Idx node_idx = init_nodes.elems[i];
re_token_type_t type = dfa->nodes[node_idx].type;
int clexp_idx;
Idx clexp_idx;
if (type != OP_BACK_REF)
continue;
for (clexp_idx = 0; clexp_idx < init_nodes.nelem; ++clexp_idx)
......@@ -1014,7 +969,7 @@ create_initial_state (dfa)
if (type == OP_BACK_REF)
{
int dest_idx = dfa->edests[node_idx].elems[0];
Idx dest_idx = dfa->edests[node_idx].elems[0];
if (!re_node_set_contains (&init_nodes, dest_idx))
{
re_node_set_merge (&init_nodes, dfa->eclosures + dest_idx);
......@@ -1056,17 +1011,19 @@ create_initial_state (dfa)
DFA nodes where needed. */
static void
optimize_utf8 (dfa)
re_dfa_t *dfa;
optimize_utf8 (re_dfa_t *dfa)
{
int node, i, mb_chars = 0, has_period = 0;
Idx node;
int i;
bool mb_chars = false;
bool has_period = false;
for (node = 0; node < dfa->nodes_len; ++node)
switch (dfa->nodes[node].type)
{
case CHARACTER:
if (dfa->nodes[node].opr.c >= 0x80)
mb_chars = 1;
mb_chars = true;
break;
case ANCHOR:
switch (dfa->nodes[node].opr.idx)
......@@ -1082,7 +1039,7 @@ optimize_utf8 (dfa)
}
break;
case OP_PERIOD:
has_period = 1;
has_period = true;
break;
case OP_BACK_REF:
case OP_ALT:
......@@ -1095,9 +1052,18 @@ optimize_utf8 (dfa)
return;
case SIMPLE_BRACKET:
/* Just double check. */
for (i = 0x80 / UINT_BITS; i < BITSET_UINTS; ++i)
if (dfa->nodes[node].opr.sbcset[i])
return;
{
int rshift =
(SBC_MAX / 2 % BITSET_WORD_BITS == 0
? 0
: BITSET_WORD_BITS - SBC_MAX / 2 % BITSET_WORD_BITS);
for (i = SBC_MAX / 2 / BITSET_WORD_BITS; i < BITSET_WORDS; ++i)
{
if (dfa->nodes[node].opr.sbcset[i] >> rshift != 0)
return;
rshift = 0;
}
}
break;
default:
abort ();
......@@ -1124,25 +1090,24 @@ optimize_utf8 (dfa)
"eclosure", and "inveclosure". */
static reg_errcode_t
analyze (preg)
regex_t *preg;
analyze (regex_t *preg)
{
re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
reg_errcode_t ret;
/* Allocate arrays. */
dfa->nexts = re_malloc (int, dfa->nodes_alloc);
dfa->org_indices = re_malloc (int, dfa->nodes_alloc);
dfa->edests = re_malloc (re_node_set, dfa->nodes_alloc);
dfa->nexts = re_malloc (Idx, dfa->nodes_alloc);
dfa->org_indices = re_malloc (Idx, dfa->nodes_alloc);
dfa->edests = re_xmalloc (re_node_set, dfa->nodes_alloc);
dfa->eclosures = re_malloc (re_node_set, dfa->nodes_alloc);
if (BE (dfa->nexts == NULL || dfa->org_indices == NULL || dfa->edests == NULL
|| dfa->eclosures == NULL, 0))
return REG_ESPACE;
dfa->subexp_map = re_malloc (int, preg->re_nsub);
dfa->subexp_map = re_xmalloc (Idx, preg->re_nsub);
if (dfa->subexp_map != NULL)
{
int i;
Idx i;
for (i = 0; i < preg->re_nsub; i++)
dfa->subexp_map[i] = i;
preorder (dfa->str_tree, optimize_subexps, dfa);
......@@ -1172,10 +1137,10 @@ analyze (preg)
/* We only need this during the prune_impossible_nodes pass in regexec.c;
skip it if p_i_n will not run, as calc_inveclosure can be quadratic. */
if ((!preg->no_sub && preg->re_nsub > 0 && dfa->has_plural_match)
if ((!preg->re_no_sub && preg->re_nsub > 0 && dfa->has_plural_match)
|| dfa->nbackref)
{
dfa->inveclosures = re_malloc (re_node_set, dfa->nodes_len);
dfa->inveclosures = re_xmalloc (re_node_set, dfa->nodes_len);
if (BE (dfa->inveclosures == NULL, 0))
return REG_ESPACE;
ret = calc_inveclosure (dfa);
......@@ -1188,10 +1153,8 @@ analyze (preg)
implement parse tree visits. Instead, we use parent pointers and
some hairy code in these two functions. */
static reg_errcode_t
postorder (root, fn, extra)
bin_tree_t *root;
reg_errcode_t (fn (void *, bin_tree_t *));
void *extra;
postorder (bin_tree_t *root, reg_errcode_t (fn (void *, bin_tree_t *)),
void *extra)
{
bin_tree_t *node, *prev;
......@@ -1222,10 +1185,8 @@ postorder (root, fn, extra)
}
static reg_errcode_t
preorder (root, fn, extra)
bin_tree_t *root;
reg_errcode_t (fn (void *, bin_tree_t *));
void *extra;
preorder (bin_tree_t *root, reg_errcode_t (fn (void *, bin_tree_t *)),
void *extra)
{
bin_tree_t *node;
......@@ -1257,9 +1218,7 @@ preorder (root, fn, extra)
re_search_internal to map the inner one's opr.idx to this one's. Adjust
backreferences as well. Requires a preorder visit. */
static reg_errcode_t
optimize_subexps (extra, node)
void *extra;
bin_tree_t *node;
optimize_subexps (void *extra, bin_tree_t *node)
{
re_dfa_t *dfa = (re_dfa_t *) extra;
......@@ -1273,15 +1232,15 @@ optimize_subexps (extra, node)
else if (node->token.type == SUBEXP
&& node->left && node->left->token.type == SUBEXP)
{
int other_idx = node->left->token.opr.idx;
Idx other_idx = node->left->token.opr.idx;
node->left = node->left->left;
if (node->left)
node->left->parent = node;
dfa->subexp_map[other_idx] = dfa->subexp_map[node->token.opr.idx];
if (other_idx < 8 * sizeof (dfa->used_bkref_map))
dfa->used_bkref_map &= ~(1 << other_idx);
if (other_idx < BITSET_WORD_BITS)
dfa->used_bkref_map &= ~ ((bitset_word) 1 << other_idx);
}
return REG_NOERROR;
......@@ -1290,9 +1249,7 @@ optimize_subexps (extra, node)
/* Lowering pass: Turn each SUBEXP node into the appropriate concatenation
of OP_OPEN_SUBEXP, the body of the SUBEXP (if any) and OP_CLOSE_SUBEXP. */
static reg_errcode_t
lower_subexps (extra, node)
void *extra;
bin_tree_t *node;
lower_subexps (void *extra, bin_tree_t *node)
{
regex_t *preg = (regex_t *) extra;
reg_errcode_t err = REG_NOERROR;
......@@ -1314,23 +1271,20 @@ lower_subexps (extra, node)
}
static bin_tree_t *
lower_subexp (err, preg, node)
reg_errcode_t *err;
regex_t *preg;
bin_tree_t *node;
lower_subexp (reg_errcode_t *err, regex_t *preg, bin_tree_t *node)
{
re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
bin_tree_t *body = node->left;
bin_tree_t *op, *cls, *tree1, *tree;
if (preg->no_sub
if (preg->re_no_sub
/* We do not optimize empty subexpressions, because otherwise we may
have bad CONCAT nodes with NULL children. This is obviously not
very common, so we do not lose much. An example that triggers
this case is the sed "script" /\(\)/x. */
&& node->left != NULL
&& (node->token.opr.idx >= 8 * sizeof (dfa->used_bkref_map)
|| !(dfa->used_bkref_map & (1 << node->token.opr.idx))))
&& ! (node->token.opr.idx < BITSET_WORD_BITS
&& dfa->used_bkref_map & ((bitset_word) 1 << node->token.opr.idx)))
return node->left;
/* Convert the SUBEXP node to the concatenation of an
......@@ -1353,9 +1307,7 @@ lower_subexp (err, preg, node)
/* Pass 1 in building the NFA: compute FIRST and create unlinked automaton
nodes. Requires a postorder visit. */
static reg_errcode_t
calc_first (extra, node)
void *extra;
bin_tree_t *node;
calc_first (void *extra, bin_tree_t *node)
{
re_dfa_t *dfa = (re_dfa_t *) extra;
if (node->token.type == CONCAT)
......@@ -1367,7 +1319,7 @@ calc_first (extra, node)
{
node->first = node;
node->node_idx = re_dfa_add_node (dfa, node->token);
if (BE (node->node_idx == -1, 0))
if (BE (node->node_idx == REG_MISSING, 0))
return REG_ESPACE;
}
return REG_NOERROR;
......@@ -1375,9 +1327,7 @@ calc_first (extra, node)
/* Pass 2: compute NEXT on the tree. Preorder visit. */
static reg_errcode_t
calc_next (extra, node)
void *extra;
bin_tree_t *node;
calc_next (void *extra, bin_tree_t *node)
{
switch (node->token.type)
{
......@@ -1400,12 +1350,10 @@ calc_next (extra, node)
/* Pass 3: link all DFA nodes to their NEXT node (any order will do). */
static reg_errcode_t
link_nfa_nodes (extra, node)
void *extra;
bin_tree_t *node;
link_nfa_nodes (void *extra, bin_tree_t *node)
{
re_dfa_t *dfa = (re_dfa_t *) extra;
int idx = node->node_idx;
Idx idx = node->node_idx;
reg_errcode_t err = REG_NOERROR;
switch (node->token.type)
......@@ -1420,7 +1368,7 @@ link_nfa_nodes (extra, node)
case OP_DUP_ASTERISK:
case OP_ALT:
{
int left, right;
Idx left, right;
dfa->has_plural_match = 1;
if (node->left != NULL)
left = node->left->first->node_idx;
......@@ -1430,8 +1378,8 @@ link_nfa_nodes (extra, node)
right = node->right->first->node_idx;
else
right = node->next->node_idx;
assert (left > -1);
assert (right > -1);
assert (REG_VALID_INDEX (left));
assert (REG_VALID_INDEX (right));
err = re_node_set_init_2 (dfa->edests + idx, left, right);
}
break;
......@@ -1462,18 +1410,16 @@ link_nfa_nodes (extra, node)
to their own constraint. */
static reg_errcode_t
duplicate_node_closure (dfa, top_org_node, top_clone_node, root_node,
init_constraint)
re_dfa_t *dfa;
int top_org_node, top_clone_node, root_node;
unsigned int init_constraint;
duplicate_node_closure (re_dfa_t *dfa, Idx top_org_node,
Idx top_clone_node, Idx root_node,
unsigned int init_constraint)
{
reg_errcode_t err;
int org_node, clone_node, ret;
Idx org_node, clone_node;
bool ok;
unsigned int constraint = init_constraint;
for (org_node = top_org_node, clone_node = top_clone_node;;)
{
int org_dest, clone_dest;
Idx org_dest, clone_dest;
if (dfa->nodes[org_node].type == OP_BACK_REF)
{
/* 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,
edests of the back reference. */
org_dest = dfa->nexts[org_node];
re_node_set_empty (dfa->edests + clone_node);
err = duplicate_node (&clone_dest, dfa, org_dest, constraint);
if (BE (err != REG_NOERROR, 0))
return err;
clone_dest = duplicate_node (dfa, org_dest, constraint);
if (BE (clone_dest == REG_MISSING, 0))
return REG_ESPACE;
dfa->nexts[clone_node] = dfa->nexts[org_node];
ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
if (BE (ret < 0, 0))
ok = re_node_set_insert (dfa->edests + clone_node, clone_dest);
if (BE (! ok, 0))
return REG_ESPACE;
}
else if (dfa->edests[org_node].nelem == 0)
......@@ -1512,19 +1458,19 @@ duplicate_node_closure (dfa, top_org_node, top_clone_node, root_node,
/* ...but if the node is root_node itself, it means the
epsilon closure have a loop, then tie it to the
destination of the root_node. */
ret = re_node_set_insert (dfa->edests + clone_node,
ok = re_node_set_insert (dfa->edests + clone_node,
org_dest);
if (BE (ret < 0, 0))
if (BE (! ok, 0))
return REG_ESPACE;
break;
}
constraint |= dfa->nodes[org_node].opr.ctx_type;
}
err = duplicate_node (&clone_dest, dfa, org_dest, constraint);
if (BE (err != REG_NOERROR, 0))
return err;
ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
if (BE (ret < 0, 0))
clone_dest = duplicate_node (dfa, org_dest, constraint);
if (BE (clone_dest == REG_MISSING, 0))
return REG_ESPACE;
ok = re_node_set_insert (dfa->edests + clone_node, clone_dest);
if (BE (! ok, 0))
return REG_ESPACE;
}
else /* dfa->edests[org_node].nelem == 2 */
......@@ -1535,14 +1481,15 @@ duplicate_node_closure (dfa, top_org_node, top_clone_node, root_node,
re_node_set_empty (dfa->edests + clone_node);
/* Search for a duplicated node which satisfies the constraint. */
clone_dest = search_duplicated_node (dfa, org_dest, constraint);
if (clone_dest == -1)
if (clone_dest == REG_MISSING)
{
/* There are no such a duplicated node, create a new one. */
err = duplicate_node (&clone_dest, dfa, org_dest, constraint);
if (BE (err != REG_NOERROR, 0))
return err;
ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
if (BE (ret < 0, 0))
reg_errcode_t err;
clone_dest = duplicate_node (dfa, org_dest, constraint);
if (BE (clone_dest == REG_MISSING, 0))
return REG_ESPACE;
ok = re_node_set_insert (dfa->edests + clone_node, clone_dest);
if (BE (! ok, 0))
return REG_ESPACE;
err = duplicate_node_closure (dfa, org_dest, clone_dest,
root_node, constraint);
......@@ -1553,17 +1500,17 @@ duplicate_node_closure (dfa, top_org_node, top_clone_node, root_node,
{
/* There are a duplicated node which satisfy the constraint,
use it to avoid infinite loop. */
ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
if (BE (ret < 0, 0))
ok = re_node_set_insert (dfa->edests + clone_node, clone_dest);
if (BE (! ok, 0))
return REG_ESPACE;
}
org_dest = dfa->edests[org_node].elems[1];
err = duplicate_node (&clone_dest, dfa, org_dest, constraint);
if (BE (err != REG_NOERROR, 0))
return err;
ret = re_node_set_insert (dfa->edests + clone_node, clone_dest);
if (BE (ret < 0, 0))
clone_dest = duplicate_node (dfa, org_dest, constraint);
if (BE (clone_dest == REG_MISSING, 0))
return REG_ESPACE;
ok = re_node_set_insert (dfa->edests + clone_node, clone_dest);
if (BE (! ok, 0))
return REG_ESPACE;
}
org_node = org_dest;
......@@ -1575,61 +1522,56 @@ duplicate_node_closure (dfa, top_org_node, top_clone_node, root_node,
/* Search for a node which is duplicated from the node ORG_NODE, and
satisfies the constraint CONSTRAINT. */
static int
search_duplicated_node (dfa, org_node, constraint)
re_dfa_t *dfa;
int org_node;
unsigned int constraint;
static Idx
search_duplicated_node (const re_dfa_t *dfa, Idx org_node,
unsigned int constraint)
{
int idx;
Idx idx;
for (idx = dfa->nodes_len - 1; dfa->nodes[idx].duplicated && idx > 0; --idx)
{
if (org_node == dfa->org_indices[idx]
&& constraint == dfa->nodes[idx].constraint)
return idx; /* Found. */
}
return -1; /* Not found. */
return REG_MISSING; /* Not found. */
}
/* Duplicate the node whose index is ORG_IDX and set the constraint CONSTRAINT.
The new index will be stored in NEW_IDX and return REG_NOERROR if succeeded,
otherwise return the error code. */
Return the index of the new node, or REG_MISSING if insufficient storage is
available. */
static reg_errcode_t
duplicate_node (new_idx, dfa, org_idx, constraint)
re_dfa_t *dfa;
int *new_idx, org_idx;
unsigned int constraint;
static Idx
duplicate_node (re_dfa_t *dfa, Idx org_idx, unsigned int constraint)
{
int dup_idx = re_dfa_add_node (dfa, dfa->nodes[org_idx]);
if (BE (dup_idx == -1, 0))
return REG_ESPACE;
dfa->nodes[dup_idx].constraint = constraint;
if (dfa->nodes[org_idx].type == ANCHOR)
dfa->nodes[dup_idx].constraint |= dfa->nodes[org_idx].opr.ctx_type;
dfa->nodes[dup_idx].duplicated = 1;
/* Store the index of the original node. */
dfa->org_indices[dup_idx] = org_idx;
*new_idx = dup_idx;
return REG_NOERROR;
Idx dup_idx = re_dfa_add_node (dfa, dfa->nodes[org_idx]);
if (BE (dup_idx != REG_MISSING, 1))
{
dfa->nodes[dup_idx].constraint = constraint;
if (dfa->nodes[org_idx].type == ANCHOR)
dfa->nodes[dup_idx].constraint |= dfa->nodes[org_idx].opr.ctx_type;
dfa->nodes[dup_idx].duplicated = 1;
/* Store the index of the original node. */
dfa->org_indices[dup_idx] = org_idx;
}
return dup_idx;
}
static reg_errcode_t
calc_inveclosure (dfa)
re_dfa_t *dfa;
calc_inveclosure (re_dfa_t *dfa)
{
int src, idx, ret;
Idx src, idx;
bool ok;
for (idx = 0; idx < dfa->nodes_len; ++idx)
re_node_set_init_empty (dfa->inveclosures + idx);
for (src = 0; src < dfa->nodes_len; ++src)
{
int *elems = dfa->eclosures[src].elems;
Idx *elems = dfa->eclosures[src].elems;
for (idx = 0; idx < dfa->eclosures[src].nelem; ++idx)
{
ret = re_node_set_insert_last (dfa->inveclosures + elems[idx], src);
if (BE (ret == -1, 0))
ok = re_node_set_insert_last (dfa->inveclosures + elems[idx], src);
if (BE (! ok, 0))
return REG_ESPACE;
}
}
......@@ -1640,14 +1582,14 @@ calc_inveclosure (dfa)
/* Calculate "eclosure" for all the node in DFA. */
static reg_errcode_t
calc_eclosure (dfa)
re_dfa_t *dfa;
calc_eclosure (re_dfa_t *dfa)
{
int node_idx, incomplete;
Idx node_idx;
bool incomplete;
#ifdef DEBUG
assert (dfa->nodes_len > 0);
#endif
incomplete = 0;
incomplete = false;
/* For each nodes, calculate epsilon closure. */
for (node_idx = 0; ; ++node_idx)
{
......@@ -1657,25 +1599,25 @@ calc_eclosure (dfa)
{
if (!incomplete)
break;
incomplete = 0;
incomplete = false;
node_idx = 0;
}
#ifdef DEBUG
assert (dfa->eclosures[node_idx].nelem != -1);
assert (dfa->eclosures[node_idx].nelem != REG_MISSING);
#endif
/* If we have already calculated, skip it. */
if (dfa->eclosures[node_idx].nelem != 0)
continue;
/* Calculate epsilon closure of `node_idx'. */
err = calc_eclosure_iter (&eclosure_elem, dfa, node_idx, 1);
err = calc_eclosure_iter (&eclosure_elem, dfa, node_idx, true);
if (BE (err != REG_NOERROR, 0))
return err;
if (dfa->eclosures[node_idx].nelem == 0)
{
incomplete = 1;
incomplete = true;
re_node_set_free (&eclosure_elem);
}
}
......@@ -1685,23 +1627,22 @@ calc_eclosure (dfa)
/* Calculate epsilon closure of NODE. */
static reg_errcode_t
calc_eclosure_iter (new_set, dfa, node, root)
re_node_set *new_set;
re_dfa_t *dfa;
int node, root;
calc_eclosure_iter (re_node_set *new_set, re_dfa_t *dfa, Idx node, bool root)
{
reg_errcode_t err;
unsigned int constraint;
int i, incomplete;
Idx i;
bool incomplete;
bool ok;
re_node_set eclosure;
incomplete = 0;
incomplete = false;
err = re_node_set_alloc (&eclosure, dfa->edests[node].nelem + 1);
if (BE (err != REG_NOERROR, 0))
return err;
/* This indicates that we are calculating this node now.
We reference this value to avoid infinite loop. */
dfa->eclosures[node].nelem = -1;
dfa->eclosures[node].nelem = REG_MISSING;
constraint = ((dfa->nodes[node].type == ANCHOR)
? dfa->nodes[node].opr.ctx_type : 0);
......@@ -1711,7 +1652,7 @@ calc_eclosure_iter (new_set, dfa, node, root)
&& dfa->edests[node].nelem
&& !dfa->nodes[dfa->edests[node].elems[0]].duplicated)
{
int org_node, cur_node;
Idx org_node, cur_node;
org_node = cur_node = node;
err = duplicate_node_closure (dfa, node, node, node, constraint);
if (BE (err != REG_NOERROR, 0))
......@@ -1723,19 +1664,19 @@ calc_eclosure_iter (new_set, dfa, node, root)
for (i = 0; i < dfa->edests[node].nelem; ++i)
{
re_node_set eclosure_elem;
int edest = dfa->edests[node].elems[i];
Idx edest = dfa->edests[node].elems[i];
/* If calculating the epsilon closure of `edest' is in progress,
return intermediate result. */
if (dfa->eclosures[edest].nelem == -1)
if (dfa->eclosures[edest].nelem == REG_MISSING)
{
incomplete = 1;
incomplete = true;
continue;
}
/* If we haven't calculated the epsilon closure of `edest' yet,
calculate now. Otherwise use calculated epsilon closure. */
if (dfa->eclosures[edest].nelem == 0)
{
err = calc_eclosure_iter (&eclosure_elem, dfa, edest, 0);
err = calc_eclosure_iter (&eclosure_elem, dfa, edest, false);
if (BE (err != REG_NOERROR, 0))
return err;
}
......@@ -1747,13 +1688,15 @@ calc_eclosure_iter (new_set, dfa, node, root)
the epsilon closure of this node is also incomplete. */
if (dfa->eclosures[edest].nelem == 0)
{
incomplete = 1;
incomplete = true;
re_node_set_free (&eclosure_elem);
}
}
/* Epsilon closures include itself. */
re_node_set_insert (&eclosure, node);
ok = re_node_set_insert (&eclosure, node);
if (BE (! ok, 0))
return REG_ESPACE;
if (incomplete && !root)
dfa->eclosures[node].nelem = 0;
else
......@@ -1768,10 +1711,7 @@ calc_eclosure_iter (new_set, dfa, node, root)
We must not use this function inside bracket expressions. */
static void
fetch_token (result, input, syntax)
re_token_t *result;
re_string_t *input;
reg_syntax_t syntax;
fetch_token (re_token_t *result, re_string_t *input, reg_syntax_t syntax)
{
re_string_skip_bytes (input, peek_token (result, input, syntax));
}
......@@ -1780,10 +1720,7 @@ fetch_token (result, input, syntax)
We must not use this function inside bracket expressions. */
static int
peek_token (token, input, syntax)
re_token_t *token;
re_string_t *input;
reg_syntax_t syntax;
peek_token (re_token_t *token, re_string_t *input, reg_syntax_t syntax)
{
unsigned char c;
......@@ -1833,97 +1770,97 @@ peek_token (token, input, syntax)
switch (c2)
{
case '|':
if (!(syntax & RE_LIMITED_OPS) && !(syntax & RE_NO_BK_VBAR))
if (!(syntax & REG_LIMITED_OPS) && !(syntax & REG_NO_BK_VBAR))
token->type = OP_ALT;
break;
case '1': case '2': case '3': case '4': case '5':
case '6': case '7': case '8': case '9':
if (!(syntax & RE_NO_BK_REFS))
if (!(syntax & REG_NO_BK_REFS))
{
token->type = OP_BACK_REF;
token->opr.idx = c2 - '1';
}
break;
case '<':
if (!(syntax & RE_NO_GNU_OPS))
if (!(syntax & REG_NO_GNU_OPS))
{
token->type = ANCHOR;
token->opr.ctx_type = WORD_FIRST;
}
break;
case '>':
if (!(syntax & RE_NO_GNU_OPS))
if (!(syntax & REG_NO_GNU_OPS))
{
token->type = ANCHOR;
token->opr.ctx_type = WORD_LAST;
}
break;
case 'b':
if (!(syntax & RE_NO_GNU_OPS))
if (!(syntax & REG_NO_GNU_OPS))
{
token->type = ANCHOR;
token->opr.ctx_type = WORD_DELIM;
}
break;
case 'B':
if (!(syntax & RE_NO_GNU_OPS))
if (!(syntax & REG_NO_GNU_OPS))
{
token->type = ANCHOR;
token->opr.ctx_type = NOT_WORD_DELIM;
}
break;
case 'w':
if (!(syntax & RE_NO_GNU_OPS))
if (!(syntax & REG_NO_GNU_OPS))
token->type = OP_WORD;
break;
case 'W':
if (!(syntax & RE_NO_GNU_OPS))
if (!(syntax & REG_NO_GNU_OPS))
token->type = OP_NOTWORD;
break;
case 's':
if (!(syntax & RE_NO_GNU_OPS))
if (!(syntax & REG_NO_GNU_OPS))
token->type = OP_SPACE;
break;
case 'S':
if (!(syntax & RE_NO_GNU_OPS))
if (!(syntax & REG_NO_GNU_OPS))
token->type = OP_NOTSPACE;
break;
case '`':
if (!(syntax & RE_NO_GNU_OPS))
if (!(syntax & REG_NO_GNU_OPS))
{
token->type = ANCHOR;
token->opr.ctx_type = BUF_FIRST;
}
break;
case '\'':
if (!(syntax & RE_NO_GNU_OPS))
if (!(syntax & REG_NO_GNU_OPS))
{
token->type = ANCHOR;
token->opr.ctx_type = BUF_LAST;
}
break;
case '(':
if (!(syntax & RE_NO_BK_PARENS))
if (!(syntax & REG_NO_BK_PARENS))
token->type = OP_OPEN_SUBEXP;
break;
case ')':
if (!(syntax & RE_NO_BK_PARENS))
if (!(syntax & REG_NO_BK_PARENS))
token->type = OP_CLOSE_SUBEXP;
break;
case '+':
if (!(syntax & RE_LIMITED_OPS) && (syntax & RE_BK_PLUS_QM))
if (!(syntax & REG_LIMITED_OPS) && (syntax & REG_BK_PLUS_QM))
token->type = OP_DUP_PLUS;
break;
case '?':
if (!(syntax & RE_LIMITED_OPS) && (syntax & RE_BK_PLUS_QM))
if (!(syntax & REG_LIMITED_OPS) && (syntax & REG_BK_PLUS_QM))
token->type = OP_DUP_QUESTION;
break;
case '{':
if ((syntax & RE_INTERVALS) && (!(syntax & RE_NO_BK_BRACES)))
if ((syntax & REG_INTERVALS) && (!(syntax & REG_NO_BK_BRACES)))
token->type = OP_OPEN_DUP_NUM;
break;
case '}':
if ((syntax & RE_INTERVALS) && (!(syntax & RE_NO_BK_BRACES)))
if ((syntax & REG_INTERVALS) && (!(syntax & REG_NO_BK_BRACES)))
token->type = OP_CLOSE_DUP_NUM;
break;
default:
......@@ -1946,38 +1883,38 @@ peek_token (token, input, syntax)
switch (c)
{
case '\n':
if (syntax & RE_NEWLINE_ALT)
if (syntax & REG_NEWLINE_ALT)
token->type = OP_ALT;
break;
case '|':
if (!(syntax & RE_LIMITED_OPS) && (syntax & RE_NO_BK_VBAR))
if (!(syntax & REG_LIMITED_OPS) && (syntax & REG_NO_BK_VBAR))
token->type = OP_ALT;
break;
case '*':
token->type = OP_DUP_ASTERISK;
break;
case '+':
if (!(syntax & RE_LIMITED_OPS) && !(syntax & RE_BK_PLUS_QM))
if (!(syntax & REG_LIMITED_OPS) && !(syntax & REG_BK_PLUS_QM))
token->type = OP_DUP_PLUS;
break;
case '?':
if (!(syntax & RE_LIMITED_OPS) && !(syntax & RE_BK_PLUS_QM))
if (!(syntax & REG_LIMITED_OPS) && !(syntax & REG_BK_PLUS_QM))
token->type = OP_DUP_QUESTION;
break;
case '{':
if ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
if ((syntax & REG_INTERVALS) && (syntax & REG_NO_BK_BRACES))
token->type = OP_OPEN_DUP_NUM;
break;
case '}':
if ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
if ((syntax & REG_INTERVALS) && (syntax & REG_NO_BK_BRACES))
token->type = OP_CLOSE_DUP_NUM;
break;
case '(':
if (syntax & RE_NO_BK_PARENS)
if (syntax & REG_NO_BK_PARENS)
token->type = OP_OPEN_SUBEXP;
break;
case ')':
if (syntax & RE_NO_BK_PARENS)
if (syntax & REG_NO_BK_PARENS)
token->type = OP_CLOSE_SUBEXP;
break;
case '[':
......@@ -1987,18 +1924,18 @@ peek_token (token, input, syntax)
token->type = OP_PERIOD;
break;
case '^':
if (!(syntax & (RE_CONTEXT_INDEP_ANCHORS | RE_CARET_ANCHORS_HERE)) &&
if (!(syntax & (REG_CONTEXT_INDEP_ANCHORS | REG_CARET_ANCHORS_HERE)) &&
re_string_cur_idx (input) != 0)
{
char prev = re_string_peek_byte (input, -1);
if (!(syntax & RE_NEWLINE_ALT) || prev != '\n')
if (!(syntax & REG_NEWLINE_ALT) || prev != '\n')
break;
}
token->type = ANCHOR;
token->opr.ctx_type = LINE_FIRST;
break;
case '$':
if (!(syntax & RE_CONTEXT_INDEP_ANCHORS) &&
if (!(syntax & REG_CONTEXT_INDEP_ANCHORS) &&
re_string_cur_idx (input) + 1 != re_string_length (input))
{
re_token_t next;
......@@ -2021,10 +1958,7 @@ peek_token (token, input, syntax)
We must not use this function out of bracket expressions. */
static int
peek_token_bracket (token, input, syntax)
re_token_t *token;
re_string_t *input;
reg_syntax_t syntax;
peek_token_bracket (re_token_t *token, re_string_t *input, reg_syntax_t syntax)
{
unsigned char c;
if (re_string_eoi (input))
......@@ -2044,7 +1978,7 @@ peek_token_bracket (token, input, syntax)
}
#endif /* RE_ENABLE_I18N */
if (c == '\\' && (syntax & RE_BACKSLASH_ESCAPE_IN_LISTS)
if (c == '\\' && (syntax & REG_BACKSLASH_ESCAPE_IN_LISTS)
&& re_string_cur_idx (input) + 1 < re_string_length (input))
{
/* In this case, '\' escape a character. */
......@@ -2074,7 +2008,7 @@ peek_token_bracket (token, input, syntax)
token->type = OP_OPEN_EQUIV_CLASS;
break;
case ':':
if (syntax & RE_CHAR_CLASSES)
if (syntax & REG_CHAR_CLASSES)
{
token->type = OP_OPEN_CHAR_CLASS;
break;
......@@ -2120,17 +2054,14 @@ peek_token_bracket (token, input, syntax)
EOR means end of regular expression. */
static bin_tree_t *
parse (regexp, preg, syntax, err)
re_string_t *regexp;
regex_t *preg;
reg_syntax_t syntax;
reg_errcode_t *err;
parse (re_string_t *regexp, regex_t *preg, reg_syntax_t syntax,
reg_errcode_t *err)
{
re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
bin_tree_t *tree, *eor, *root;
re_token_t current_token;
dfa->syntax = syntax;
fetch_token (&current_token, regexp, syntax | RE_CARET_ANCHORS_HERE);
fetch_token (&current_token, regexp, syntax | REG_CARET_ANCHORS_HERE);
tree = parse_reg_exp (regexp, preg, &current_token, syntax, 0, err);
if (BE (*err != REG_NOERROR && tree == NULL, 0))
return NULL;
......@@ -2157,15 +2088,10 @@ parse (regexp, preg, syntax, err)
ALT means alternative, which represents the operator `|'. */
static bin_tree_t *
parse_reg_exp (regexp, preg, token, syntax, nest, err)
re_string_t *regexp;
regex_t *preg;
re_token_t *token;
reg_syntax_t syntax;
int nest;
reg_errcode_t *err;
parse_reg_exp (re_string_t *regexp, regex_t *preg, re_token_t *token,
reg_syntax_t syntax, Idx nest, reg_errcode_t *err)
{
re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
bin_tree_t *tree, *branch = NULL;
tree = parse_branch (regexp, preg, token, syntax, nest, err);
if (BE (*err != REG_NOERROR && tree == NULL, 0))
......@@ -2173,7 +2099,7 @@ parse_reg_exp (regexp, preg, token, syntax, nest, err)
while (token->type == OP_ALT)
{
fetch_token (token, regexp, syntax | RE_CARET_ANCHORS_HERE);
fetch_token (token, regexp, syntax | REG_CARET_ANCHORS_HERE);
if (token->type != OP_ALT && token->type != END_OF_RE
&& (nest == 0 || token->type != OP_CLOSE_SUBEXP))
{
......@@ -2203,16 +2129,11 @@ parse_reg_exp (regexp, preg, token, syntax, nest, err)
CAT means concatenation. */
static bin_tree_t *
parse_branch (regexp, preg, token, syntax, nest, err)
re_string_t *regexp;
regex_t *preg;
re_token_t *token;
reg_syntax_t syntax;
int nest;
reg_errcode_t *err;
parse_branch (re_string_t *regexp, regex_t *preg, re_token_t *token,
reg_syntax_t syntax, Idx nest, reg_errcode_t *err)
{
bin_tree_t *tree, *exp;
re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
tree = parse_expression (regexp, preg, token, syntax, nest, err);
if (BE (*err != REG_NOERROR && tree == NULL, 0))
return NULL;
......@@ -2248,15 +2169,10 @@ parse_branch (regexp, preg, token, syntax, nest, err)
*/
static bin_tree_t *
parse_expression (regexp, preg, token, syntax, nest, err)
re_string_t *regexp;
regex_t *preg;
re_token_t *token;
reg_syntax_t syntax;
int nest;
reg_errcode_t *err;
parse_expression (re_string_t *regexp, regex_t *preg, re_token_t *token,
reg_syntax_t syntax, Idx nest, reg_errcode_t *err)
{
re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
bin_tree_t *tree;
switch (token->type)
{
......@@ -2313,7 +2229,7 @@ parse_expression (regexp, preg, token, syntax, nest, err)
dfa->has_mb_node = 1;
break;
case OP_OPEN_DUP_NUM:
if (syntax & RE_CONTEXT_INVALID_DUP)
if (syntax & REG_CONTEXT_INVALID_DUP)
{
*err = REG_BADRPT;
return NULL;
......@@ -2322,12 +2238,12 @@ parse_expression (regexp, preg, token, syntax, nest, err)
case OP_DUP_ASTERISK:
case OP_DUP_PLUS:
case OP_DUP_QUESTION:
if (syntax & RE_CONTEXT_INVALID_OPS)
if (syntax & REG_CONTEXT_INVALID_OPS)
{
*err = REG_BADRPT;
return NULL;
}
else if (syntax & RE_CONTEXT_INDEP_OPS)
else if (syntax & REG_CONTEXT_INDEP_OPS)
{
fetch_token (token, regexp, syntax);
return parse_expression (regexp, preg, token, syntax, nest, err);
......@@ -2335,7 +2251,7 @@ parse_expression (regexp, preg, token, syntax, nest, err)
/* else fall through */
case OP_CLOSE_SUBEXP:
if ((token->type == OP_CLOSE_SUBEXP) &&
!(syntax & RE_UNMATCHED_RIGHT_PAREN_ORD))
!(syntax & REG_UNMATCHED_RIGHT_PAREN_ORD))
{
*err = REG_ERPAREN;
return NULL;
......@@ -2449,7 +2365,7 @@ parse_expression (regexp, preg, token, syntax, nest, err)
if (BE (*err != REG_NOERROR && tree == NULL, 0))
return NULL;
/* In BRE consecutive duplications are not allowed. */
if ((syntax & RE_CONTEXT_INVALID_DUP)
if ((syntax & REG_CONTEXT_INVALID_DUP)
&& (token->type == OP_DUP_ASTERISK
|| token->type == OP_OPEN_DUP_NUM))
{
......@@ -2469,20 +2385,15 @@ parse_expression (regexp, preg, token, syntax, nest, err)
*/
static bin_tree_t *
parse_sub_exp (regexp, preg, token, syntax, nest, err)
re_string_t *regexp;
regex_t *preg;
re_token_t *token;
reg_syntax_t syntax;
int nest;
reg_errcode_t *err;
parse_sub_exp (re_string_t *regexp, regex_t *preg, re_token_t *token,
reg_syntax_t syntax, Idx nest, reg_errcode_t *err)
{
re_dfa_t *dfa = (re_dfa_t *) preg->buffer;
re_dfa_t *dfa = (re_dfa_t *) preg->re_buffer;
bin_tree_t *tree;
size_t cur_nsub;
cur_nsub = preg->re_nsub++;
fetch_token (token, regexp, syntax | RE_CARET_ANCHORS_HERE);
fetch_token (token, regexp, syntax | REG_CARET_ANCHORS_HERE);
/* The subexpression may be a null string. */
if (token->type == OP_CLOSE_SUBEXP)
......@@ -2495,7 +2406,9 @@ parse_sub_exp (regexp, preg, token, syntax, nest, err)
if (BE (*err != REG_NOERROR, 0))
return NULL;
}
dfa->completed_bkref_map |= 1 << cur_nsub;
if (cur_nsub <= '9' - '1')
dfa->completed_bkref_map |= 1 << cur_nsub;
tree = create_tree (dfa, tree, NULL, SUBEXP);
if (BE (tree == NULL, 0))
......@@ -2510,23 +2423,18 @@ parse_sub_exp (regexp, preg, token, syntax, nest, err)
/* This function parse repetition operators like "*", "+", "{1,3}" etc. */
static bin_tree_t *
parse_dup_op (elem, regexp, dfa, token, syntax, err)
bin_tree_t *elem;
re_string_t *regexp;
re_dfa_t *dfa;
re_token_t *token;
reg_syntax_t syntax;
reg_errcode_t *err;
parse_dup_op (bin_tree_t *elem, re_string_t *regexp, re_dfa_t *dfa,
re_token_t *token, reg_syntax_t syntax, reg_errcode_t *err)
{
bin_tree_t *tree = NULL, *old_tree = NULL;
int i, start, end, start_idx = re_string_cur_idx (regexp);
Idx i, start, end, start_idx = re_string_cur_idx (regexp);
re_token_t start_token = *token;
if (token->type == OP_OPEN_DUP_NUM)
{
end = 0;
start = fetch_number (regexp, token, syntax);
if (start == -1)
if (start == REG_MISSING)
{
if (token->type == CHARACTER && token->opr.c == ',')
start = 0; /* We treat "{,m}" as "{0,m}". */
......@@ -2536,17 +2444,17 @@ parse_dup_op (elem, regexp, dfa, token, syntax, err)
return NULL;
}
}
if (BE (start != -2, 1))
if (BE (start != REG_ERROR, 1))
{
/* We treat "{n}" as "{n,n}". */
end = ((token->type == OP_CLOSE_DUP_NUM) ? start
: ((token->type == CHARACTER && token->opr.c == ',')
? fetch_number (regexp, token, syntax) : -2));
? fetch_number (regexp, token, syntax) : REG_ERROR));
}
if (BE (start == -2 || end == -2, 0))
if (BE (start == REG_ERROR || end == REG_ERROR, 0))
{
/* Invalid sequence. */
if (BE (!(syntax & RE_INVALID_INTERVAL_ORD), 0))
if (BE (!(syntax & REG_INVALID_INTERVAL_ORD), 0))
{
if (token->type == END_OF_RE)
*err = REG_EBRACE;
......@@ -2565,7 +2473,7 @@ parse_dup_op (elem, regexp, dfa, token, syntax, err)
return elem;
}
if (BE (end != -1 && start > end, 0))
if (BE (end != REG_MISSING && start > end, 0))
{
/* First number greater than second. */
*err = REG_BADBR;
......@@ -2575,7 +2483,7 @@ parse_dup_op (elem, regexp, dfa, token, syntax, err)
else
{
start = (token->type == OP_DUP_PLUS) ? 1 : 0;
end = (token->type == OP_DUP_QUESTION) ? 1 : -1;
end = (token->type == OP_DUP_QUESTION) ? 1 : REG_MISSING;
}
fetch_token (token, regexp, syntax);
......@@ -2613,24 +2521,26 @@ parse_dup_op (elem, regexp, dfa, token, syntax, err)
if (elem->token.type == SUBEXP)
postorder (elem, mark_opt_subexp, (void *) (long) elem->token.opr.idx);
tree = create_tree (dfa, elem, NULL, (end == -1 ? OP_DUP_ASTERISK : OP_ALT));
tree = create_tree (dfa, elem, NULL,
(end == REG_MISSING ? OP_DUP_ASTERISK : OP_ALT));
if (BE (tree == NULL, 0))
goto parse_dup_op_espace;
/* This loop is actually executed only when end != -1,
/* This loop is actually executed only when end != REG_MISSING,
to rewrite <re>{0,n} as (<re>(<re>...<re>?)?)?... We have
already created the start+1-th copy. */
for (i = start + 2; i <= end; ++i)
{
elem = duplicate_tree (elem, dfa);
tree = create_tree (dfa, tree, elem, CONCAT);
if (BE (elem == NULL || tree == NULL, 0))
goto parse_dup_op_espace;
tree = create_tree (dfa, tree, NULL, OP_ALT);
if (BE (tree == NULL, 0))
goto parse_dup_op_espace;
}
if ((Idx) -1 < 0 || end != REG_MISSING)
for (i = start + 2; i <= end; ++i)
{
elem = duplicate_tree (elem, dfa);
tree = create_tree (dfa, tree, elem, CONCAT);
if (BE (elem == NULL || tree == NULL, 0))
goto parse_dup_op_espace;
tree = create_tree (dfa, tree, NULL, OP_ALT);
if (BE (tree == NULL, 0))
goto parse_dup_op_espace;
}
if (old_tree)
tree = create_tree (dfa, old_tree, tree, CONCAT);
......@@ -2655,15 +2565,11 @@ parse_dup_op (elem, regexp, dfa, token, syntax, err)
update it. */
static reg_errcode_t
build_range_exp (bitset sbcset,
# ifdef RE_ENABLE_I18N
build_range_exp (sbcset, mbcset, range_alloc, start_elem, end_elem)
re_charset_t *mbcset;
int *range_alloc;
# else /* not RE_ENABLE_I18N */
build_range_exp (sbcset, start_elem, end_elem)
# endif /* not RE_ENABLE_I18N */
re_bitset_ptr_t sbcset;
bracket_elem_t *start_elem, *end_elem;
re_charset_t *mbcset, Idx *range_alloc,
# endif
bracket_elem_t *start_elem, bracket_elem_t *end_elem)
{
unsigned int start_ch, end_ch;
/* Equivalence Classes and Character Classes can't be a range start/end. */
......@@ -2715,14 +2621,13 @@ build_range_exp (sbcset, start_elem, end_elem)
{
/* There is not enough space, need realloc. */
wchar_t *new_array_start, *new_array_end;
int new_nranges;
Idx new_nranges;
/* +1 in case of mbcset->nranges is 0. */
new_nranges = 2 * mbcset->nranges + 1;
new_nranges = mbcset->nranges;
/* Use realloc since mbcset->range_starts and mbcset->range_ends
are NULL if *range_alloc == 0. */
new_array_start = re_realloc (mbcset->range_starts, wchar_t,
new_nranges);
new_array_start = re_x2realloc (mbcset->range_starts, wchar_t,
&new_nranges);
new_array_end = re_realloc (mbcset->range_ends, wchar_t,
new_nranges);
......@@ -2776,15 +2681,11 @@ build_range_exp (sbcset, start_elem, end_elem)
pointer argument since we may update it. */
static reg_errcode_t
build_collating_symbol (bitset sbcset,
# ifdef RE_ENABLE_I18N
build_collating_symbol (sbcset, mbcset, coll_sym_alloc, name)
re_charset_t *mbcset;
int *coll_sym_alloc;
# else /* not RE_ENABLE_I18N */
build_collating_symbol (sbcset, name)
# endif /* not RE_ENABLE_I18N */
re_bitset_ptr_t sbcset;
const unsigned char *name;
re_charset_t *mbcset, Idx *coll_sym_alloc,
# endif
const unsigned char *name)
{
size_t name_len = strlen ((const char *) name);
if (BE (name_len != 1, 0))
......@@ -2801,12 +2702,8 @@ build_collating_symbol (sbcset, name)
"[[.a-a.]]" etc. */
static bin_tree_t *
parse_bracket_exp (regexp, dfa, token, syntax, err)
re_string_t *regexp;
re_dfa_t *dfa;
re_token_t *token;
reg_syntax_t syntax;
reg_errcode_t *err;
parse_bracket_exp (re_string_t *regexp, re_dfa_t *dfa, re_token_t *token,
reg_syntax_t syntax, reg_errcode_t *err)
{
#ifdef _LIBC
const unsigned char *collseqmb;
......@@ -2822,9 +2719,7 @@ parse_bracket_exp (regexp, dfa, token, syntax, err)
auto inline int32_t
__attribute ((always_inline))
seek_collating_symbol_entry (name, name_len)
const unsigned char *name;
size_t name_len;
seek_collating_symbol_entry (const unsigned char *name, size_t name_len)
{
int32_t hash = elem_hash ((const char *) name, name_len);
int32_t elem = hash % table_size;
......@@ -2855,8 +2750,7 @@ parse_bracket_exp (regexp, dfa, token, syntax, err)
auto inline unsigned int
__attribute ((always_inline))
lookup_collation_sequence_value (br_elem)
bracket_elem_t *br_elem;
lookup_collation_sequence_value (bracket_elem_t *br_elem)
{
if (br_elem->type == SB_CHAR)
{
......@@ -2923,11 +2817,9 @@ parse_bracket_exp (regexp, dfa, token, syntax, err)
auto inline reg_errcode_t
__attribute ((always_inline))
build_range_exp (sbcset, mbcset, range_alloc, start_elem, end_elem)
re_charset_t *mbcset;
int *range_alloc;
re_bitset_ptr_t sbcset;
bracket_elem_t *start_elem, *end_elem;
build_range_exp (bitset sbcset, re_charset_t *mbcset,
Idx *range_alloc,
bracket_elem_t *start_elem, bracket_elem_t *end_elem)
{
unsigned int ch;
uint32_t start_collseq;
......@@ -2945,7 +2837,7 @@ parse_bracket_exp (regexp, dfa, token, syntax, err)
/* Check start/end collation sequence values. */
if (BE (start_collseq == UINT_MAX || end_collseq == UINT_MAX, 0))
return REG_ECOLLATE;
if (BE ((syntax & RE_NO_EMPTY_RANGES) && start_collseq > end_collseq, 0))
if (BE ((syntax & REG_NO_EMPTY_RANGES) && start_collseq > end_collseq, 0))
return REG_ERANGE;
/* Got valid collation sequence values, add them as a new entry.
......@@ -2960,12 +2852,11 @@ parse_bracket_exp (regexp, dfa, token, syntax, err)
/* There is not enough space, need realloc. */
uint32_t *new_array_start;
uint32_t *new_array_end;
int new_nranges;
Idx new_nranges;
/* +1 in case of mbcset->nranges is 0. */
new_nranges = 2 * mbcset->nranges + 1;
new_array_start = re_realloc (mbcset->range_starts, uint32_t,
new_nranges);
new_nranges = mbcset->nranges;
new_array_start = re_x2realloc (mbcset->range_starts, uint32_t,
&new_nranges);
new_array_end = re_realloc (mbcset->range_ends, uint32_t,
new_nranges);
......@@ -3006,11 +2897,8 @@ parse_bracket_exp (regexp, dfa, token, syntax, err)
auto inline reg_errcode_t
__attribute ((always_inline))
build_collating_symbol (sbcset, mbcset, coll_sym_alloc, name)
re_charset_t *mbcset;
int *coll_sym_alloc;
re_bitset_ptr_t sbcset;
const unsigned char *name;
build_collating_symbol (bitset sbcset, re_charset_t *mbcset,
Idx *coll_sym_alloc, const unsigned char *name)
{
int32_t elem, idx;
size_t name_len = strlen ((const char *) name);
......@@ -3039,12 +2927,11 @@ parse_bracket_exp (regexp, dfa, token, syntax, err)
if (BE (*coll_sym_alloc == mbcset->ncoll_syms, 0))
{
/* Not enough, realloc it. */
/* +1 in case of mbcset->ncoll_syms is 0. */
int new_coll_sym_alloc = 2 * mbcset->ncoll_syms + 1;
Idx new_coll_sym_alloc = mbcset->ncoll_syms;
/* Use realloc since mbcset->coll_syms is NULL
if *alloc == 0. */
int32_t *new_coll_syms = re_realloc (mbcset->coll_syms, int32_t,
new_coll_sym_alloc);
int32_t *new_coll_syms = re_x2realloc (mbcset->coll_syms, int32_t,
&new_coll_sym_alloc);
if (BE (new_coll_syms == NULL, 0))
return REG_ESPACE;
mbcset->coll_syms = new_coll_syms;
......@@ -3070,13 +2957,13 @@ parse_bracket_exp (regexp, dfa, token, syntax, err)
re_bitset_ptr_t sbcset;
#ifdef RE_ENABLE_I18N
re_charset_t *mbcset;
int coll_sym_alloc = 0, range_alloc = 0, mbchar_alloc = 0;
int equiv_class_alloc = 0, char_class_alloc = 0;
Idx coll_sym_alloc = 0, range_alloc = 0, mbchar_alloc = 0;
Idx equiv_class_alloc = 0, char_class_alloc = 0;
#endif /* not RE_ENABLE_I18N */
int non_match = 0;
bool non_match = false;
bin_tree_t *work_tree;
int token_len;
int first_round = 1;
bool first_round = true;
#ifdef _LIBC
collseqmb = (const unsigned char *)
_NL_CURRENT (LC_COLLATE, _NL_COLLATE_COLLSEQMB);
......@@ -3094,9 +2981,9 @@ parse_bracket_exp (regexp, dfa, token, syntax, err)
_NL_COLLATE_SYMB_EXTRAMB);
}
#endif
sbcset = (re_bitset_ptr_t) calloc (sizeof (unsigned int), BITSET_UINTS);
sbcset = re_calloc (bitset_word, BITSET_WORDS);
#ifdef RE_ENABLE_I18N
mbcset = (re_charset_t *) calloc (sizeof (re_charset_t), 1);
mbcset = re_calloc (re_charset_t, 1);
#endif /* RE_ENABLE_I18N */
#ifdef RE_ENABLE_I18N
if (BE (sbcset == NULL || mbcset == NULL, 0))
......@@ -3119,8 +3006,8 @@ parse_bracket_exp (regexp, dfa, token, syntax, err)
#ifdef RE_ENABLE_I18N
mbcset->non_match = 1;
#endif /* not RE_ENABLE_I18N */
non_match = 1;
if (syntax & RE_HAT_LISTS_NOT_NEWLINE)
non_match = true;
if (syntax & REG_HAT_LISTS_NOT_NEWLINE)
bitset_set (sbcset, '\0');
re_string_skip_bytes (regexp, token_len); /* Skip a token. */
token_len = peek_token_bracket (token, regexp, syntax);
......@@ -3141,7 +3028,8 @@ parse_bracket_exp (regexp, dfa, token, syntax, err)
unsigned char start_name_buf[BRACKET_NAME_BUF_SIZE];
unsigned char end_name_buf[BRACKET_NAME_BUF_SIZE];
reg_errcode_t ret;
int token_len2 = 0, is_range_exp = 0;
int token_len2 = 0;
bool is_range_exp = false;
re_token_t token2;
start_elem.opr.name = start_name_buf;
......@@ -3152,7 +3040,7 @@ parse_bracket_exp (regexp, dfa, token, syntax, err)
*err = ret;
goto parse_bracket_exp_free_return;
}
first_round = 0;
first_round = false;
/* Get information about the next token. We need it in any case. */
token_len = peek_token_bracket (token, regexp, syntax);
......@@ -3181,15 +3069,15 @@ parse_bracket_exp (regexp, dfa, token, syntax, err)
token->type = CHARACTER;
}
else
is_range_exp = 1;
is_range_exp = true;
}
}
if (is_range_exp == 1)
if (is_range_exp == true)
{
end_elem.opr.name = end_name_buf;
ret = parse_bracket_element (&end_elem, regexp, &token2, token_len2,
dfa, syntax, 1);
dfa, syntax, true);
if (BE (ret != REG_NOERROR, 0))
{
*err = ret;
......@@ -3227,11 +3115,10 @@ parse_bracket_exp (regexp, dfa, token, syntax, err)
{
wchar_t *new_mbchars;
/* Not enough, realloc it. */
/* +1 in case of mbcset->nmbchars is 0. */
mbchar_alloc = 2 * mbcset->nmbchars + 1;
mbchar_alloc = mbcset->nmbchars;
/* Use realloc since array is NULL if *alloc == 0. */
new_mbchars = re_realloc (mbcset->mbchars, wchar_t,
mbchar_alloc);
new_mbchars = re_x2realloc (mbcset->mbchars, wchar_t,
&mbchar_alloc);
if (BE (new_mbchars == NULL, 0))
goto parse_bracket_exp_espace;
mbcset->mbchars = new_mbchars;
......@@ -3304,12 +3191,12 @@ parse_bracket_exp (regexp, dfa, token, syntax, err)
mbc_tree = create_token_tree (dfa, NULL, NULL, &br_token);
if (BE (mbc_tree == NULL, 0))
goto parse_bracket_exp_espace;
for (sbc_idx = 0; sbc_idx < BITSET_UINTS; ++sbc_idx)
for (sbc_idx = 0; sbc_idx < BITSET_WORDS; ++sbc_idx)
if (sbcset[sbc_idx])
break;
/* If there are no bits set in sbcset, there is no point
of having both SIMPLE_BRACKET and COMPLEX_BRACKET. */
if (sbc_idx < BITSET_UINTS)
if (sbc_idx < BITSET_WORDS)
{
/* Build a tree for simple bracket. */
br_token.type = SIMPLE_BRACKET;
......@@ -3357,15 +3244,9 @@ parse_bracket_exp (regexp, dfa, token, syntax, err)
/* Parse an element in the bracket expression. */
static reg_errcode_t
parse_bracket_element (elem, regexp, token, token_len, dfa, syntax,
accept_hyphen)
bracket_elem_t *elem;
re_string_t *regexp;
re_token_t *token;
int token_len;
re_dfa_t *dfa;
reg_syntax_t syntax;
int accept_hyphen;
parse_bracket_element (bracket_elem_t *elem, re_string_t *regexp,
re_token_t *token, int token_len, re_dfa_t *dfa,
reg_syntax_t syntax, bool accept_hyphen)
{
#ifdef RE_ENABLE_I18N
int cur_char_size;
......@@ -3403,10 +3284,8 @@ parse_bracket_element (elem, regexp, token, token_len, dfa, syntax,
[=<equivalent_class>=]. */
static reg_errcode_t
parse_bracket_symbol (elem, regexp, token)
bracket_elem_t *elem;
re_string_t *regexp;
re_token_t *token;
parse_bracket_symbol (bracket_elem_t *elem, re_string_t *regexp,
re_token_t *token)
{
unsigned char ch, delim = token->opr.c;
int i = 0;
......@@ -3452,15 +3331,11 @@ parse_bracket_symbol (elem, regexp, token)
is a pointer argument sinse we may update it. */
static reg_errcode_t
build_equiv_class (bitset sbcset,
#ifdef RE_ENABLE_I18N
build_equiv_class (sbcset, mbcset, equiv_class_alloc, name)
re_charset_t *mbcset;
int *equiv_class_alloc;
#else /* not RE_ENABLE_I18N */
build_equiv_class (sbcset, name)
#endif /* not RE_ENABLE_I18N */
re_bitset_ptr_t sbcset;
const unsigned char *name;
re_charset_t *mbcset, Idx *equiv_class_alloc,
#endif
const unsigned char *name)
{
#if defined _LIBC
uint32_t nrules = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
......@@ -3517,12 +3392,11 @@ build_equiv_class (sbcset, name)
if (BE (*equiv_class_alloc == mbcset->nequiv_classes, 0))
{
/* Not enough, realloc it. */
/* +1 in case of mbcset->nequiv_classes is 0. */
int new_equiv_class_alloc = 2 * mbcset->nequiv_classes + 1;
Idx new_equiv_class_alloc = mbcset->nequiv_classes;
/* Use realloc since the array is NULL if *alloc == 0. */
int32_t *new_equiv_classes = re_realloc (mbcset->equiv_classes,
int32_t,
new_equiv_class_alloc);
int32_t *new_equiv_classes = re_x2realloc (mbcset->equiv_classes,
int32_t,
&new_equiv_class_alloc);
if (BE (new_equiv_classes == NULL, 0))
return REG_ESPACE;
mbcset->equiv_classes = new_equiv_classes;
......@@ -3547,24 +3421,18 @@ build_equiv_class (sbcset, name)
is a pointer argument sinse we may update it. */
static reg_errcode_t
build_charclass (unsigned REG_TRANSLATE_TYPE trans, bitset sbcset,
#ifdef RE_ENABLE_I18N
build_charclass (trans, sbcset, mbcset, char_class_alloc, class_name, syntax)
re_charset_t *mbcset;
int *char_class_alloc;
#else /* not RE_ENABLE_I18N */
build_charclass (trans, sbcset, class_name, syntax)
#endif /* not RE_ENABLE_I18N */
unsigned RE_TRANSLATE_TYPE trans;
re_bitset_ptr_t sbcset;
const unsigned char *class_name;
reg_syntax_t syntax;
re_charset_t *mbcset, Idx *char_class_alloc,
#endif
const unsigned char *class_name, reg_syntax_t syntax)
{
int i;
const char *name = (const char *) class_name;
/* In case of REG_ICASE "upper" and "lower" match the both of
upper and lower cases. */
if ((syntax & RE_ICASE)
if ((syntax & REG_IGNORE_CASE)
&& (strcmp (name, "upper") == 0 || strcmp (name, "lower") == 0))
name = "alpha";
......@@ -3573,11 +3441,10 @@ build_charclass (trans, sbcset, class_name, syntax)
if (BE (*char_class_alloc == mbcset->nchar_classes, 0))
{
/* Not enough, realloc it. */
/* +1 in case of mbcset->nchar_classes is 0. */
int new_char_class_alloc = 2 * mbcset->nchar_classes + 1;
Idx new_char_class_alloc = mbcset->nchar_classes;
/* Use realloc since array is NULL if *alloc == 0. */
wctype_t *new_char_classes = re_realloc (mbcset->char_classes, wctype_t,
new_char_class_alloc);
wctype_t *new_char_classes = re_x2realloc (mbcset->char_classes, wctype_t,
&new_char_class_alloc);
if (BE (new_char_classes == NULL, 0))
return REG_ESPACE;
mbcset->char_classes = new_char_classes;
......@@ -3627,26 +3494,23 @@ build_charclass (trans, sbcset, class_name, syntax)
}
static bin_tree_t *
build_charclass_op (dfa, trans, class_name, extra, non_match, err)
re_dfa_t *dfa;
unsigned RE_TRANSLATE_TYPE trans;
const unsigned char *class_name;
const unsigned char *extra;
int non_match;
reg_errcode_t *err;
build_charclass_op (re_dfa_t *dfa, unsigned REG_TRANSLATE_TYPE trans,
const unsigned char *class_name,
const unsigned char *extra,
bool non_match, reg_errcode_t *err)
{
re_bitset_ptr_t sbcset;
#ifdef RE_ENABLE_I18N
re_charset_t *mbcset;
int alloc = 0;
Idx alloc = 0;
#endif /* not RE_ENABLE_I18N */
reg_errcode_t ret;
re_token_t br_token;
bin_tree_t *tree;
sbcset = (re_bitset_ptr_t) calloc (sizeof (unsigned int), BITSET_UINTS);
sbcset = re_calloc (bitset_word, BITSET_WORDS);
#ifdef RE_ENABLE_I18N
mbcset = (re_charset_t *) calloc (sizeof (re_charset_t), 1);
mbcset = re_calloc (re_charset_t, 1);
#endif /* RE_ENABLE_I18N */
#ifdef RE_ENABLE_I18N
......@@ -3663,7 +3527,7 @@ build_charclass_op (dfa, trans, class_name, extra, non_match, err)
{
#ifdef RE_ENABLE_I18N
/*
if (syntax & RE_HAT_LISTS_NOT_NEWLINE)
if (syntax & REG_HAT_LISTS_NOT_NEWLINE)
bitset_set(cset->sbcset, '\0');
*/
mbcset->non_match = 1;
......@@ -3743,28 +3607,27 @@ build_charclass_op (dfa, trans, class_name, extra, non_match, err)
/* This is intended for the expressions like "a{1,3}".
Fetch a number from `input', and return the number.
Return -1, if the number field is empty like "{,1}".
Return -2, If an error is occured. */
Return REG_MISSING if the number field is empty like "{,1}".
Return REG_ERROR if an error occurred. */
static int
fetch_number (input, token, syntax)
re_string_t *input;
re_token_t *token;
reg_syntax_t syntax;
static Idx
fetch_number (re_string_t *input, re_token_t *token, reg_syntax_t syntax)
{
int num = -1;
Idx num = REG_MISSING;
unsigned char c;
while (1)
{
fetch_token (token, input, syntax);
c = token->opr.c;
if (BE (token->type == END_OF_RE, 0))
return -2;
return REG_ERROR;
if (token->type == OP_CLOSE_DUP_NUM || c == ',')
break;
num = ((token->type != CHARACTER || c < '0' || '9' < c || num == -2)
? -2 : ((num == -1) ? c - '0' : num * 10 + c - '0'));
num = (num > RE_DUP_MAX) ? -2 : num;
num = ((token->type != CHARACTER || c < '0' || '9' < c
|| num == REG_ERROR)
? REG_ERROR
: ((num == REG_MISSING) ? c - '0' : num * 10 + c - '0'));
num = (num > REG_DUP_MAX) ? REG_ERROR : num;
}
return num;
}
......@@ -3790,11 +3653,8 @@ free_charset (re_charset_t *cset)
/* Create a tree node. */
static bin_tree_t *
create_tree (dfa, left, right, type)
re_dfa_t *dfa;
bin_tree_t *left;
bin_tree_t *right;
re_token_type_t type;
create_tree (re_dfa_t *dfa, bin_tree_t *left, bin_tree_t *right,
re_token_type_t type)
{
re_token_t t;
t.type = type;
......@@ -3802,11 +3662,8 @@ create_tree (dfa, left, right, type)
}
static bin_tree_t *
create_token_tree (dfa, left, right, token)
re_dfa_t *dfa;
bin_tree_t *left;
bin_tree_t *right;
const re_token_t *token;
create_token_tree (re_dfa_t *dfa, bin_tree_t *left, bin_tree_t *right,
const re_token_t *token)
{
bin_tree_t *tree;
if (BE (dfa->str_tree_storage_idx == BIN_TREE_STORAGE_SIZE, 0))
......@@ -3829,7 +3686,7 @@ create_token_tree (dfa, left, right, token)
tree->token.opt_subexp = 0;
tree->first = NULL;
tree->next = NULL;
tree->node_idx = -1;
tree->node_idx = REG_MISSING;
if (left != NULL)
left->parent = tree;
......@@ -3842,11 +3699,9 @@ create_token_tree (dfa, left, right, token)
To be called from preorder or postorder. */
static reg_errcode_t
mark_opt_subexp (extra, node)
void *extra;
bin_tree_t *node;
mark_opt_subexp (void *extra, bin_tree_t *node)
{
int idx = (int) (long) extra;
Idx idx = (Idx) (long) extra;
if (node->token.type == SUBEXP && node->token.opr.idx == idx)
node->token.opt_subexp = 1;
......@@ -3884,9 +3739,7 @@ free_tree (void *extra, bin_tree_t *node)
it's easier to duplicate. */
static bin_tree_t *
duplicate_tree (root, dfa)
const bin_tree_t *root;
re_dfa_t *dfa;
duplicate_tree (const bin_tree_t *root, re_dfa_t *dfa)
{
const bin_tree_t *node;
bin_tree_t *dup_root;
......
......@@ -18,31 +18,7 @@
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef _AIX
#pragma alloca
#else
# ifndef allocax /* predefined by HP cc +Olibcalls */
# ifdef __GNUC__
# define alloca(size) __builtin_alloca (size)
# else
# if HAVE_ALLOCA_H
# include <alloca.h>
# else
# ifdef __hpux
void *alloca ();
# else
# if !defined __OS2__ && !defined WIN32
char *alloca ();
# else
# include <malloc.h> /* OS/2 defines alloca in here */
# endif
# endif
# endif
# endif
# endif
# include <config.h>
#endif
#ifdef _LIBC
......@@ -70,10 +46,6 @@
# include "../locale/localeinfo.h"
#endif
/* POSIX says that <sys/types.h> must be included (by the caller) before
<regex.h>. */
#include <sys/types.h>
/* On some systems, limits.h sets RE_DUP_MAX to a lower value than
GNU regex allows. Include it before <regex.h>, which correctly
#undefs RE_DUP_MAX and sets it to the right value. */
......
/* Definitions for data structures and routines for the regular
expression library.
Copyright (C) 1985,1989-93,1995-98,2000,2001,2002,2003
Copyright (C) 1985,1989-93,1995-98,2000,2001,2002,2003,2005
Free Software Foundation, Inc.
This file is part of the GNU C Library.
......@@ -28,15 +28,61 @@
extern "C" {
#endif
/* POSIX says that <sys/types.h> must be included (by the caller) before
<regex.h>. */
/* Define _REGEX_SOURCE to get definitions that are incompatible with
POSIX. */
#if (!defined _REGEX_SOURCE \
&& (defined _GNU_SOURCE \
|| (!defined _POSIX_C_SOURCE && !defined _POSIX_SOURCE \
&& !defined _XOPEN_SOURCE)))
# define _REGEX_SOURCE 1
#endif
#if !defined _POSIX_C_SOURCE && !defined _POSIX_SOURCE && defined VMS
#if defined _REGEX_SOURCE && defined VMS
/* VMS doesn't have `size_t' in <sys/types.h>, even though POSIX says it
should be there. */
# include <stddef.h>
#endif
#ifdef _REGEX_LARGE_OFFSETS
/* Use types and values that are wide enough to represent signed and
unsigned byte offsets in memory. This currently works only when
the regex code is used outside of the GNU C library; it is not yet
supported within glibc itself, and glibc users should not define
_REGEX_LARGE_OFFSETS. */
/* The type of the offset of a byte within a string.
For historical reasons POSIX 1003.1-2004 requires that regoff_t be
at least as wide as off_t. This is a bit odd (and many common
POSIX platforms set it to the more-sensible ssize_t) but we might
as well conform. We don't know of any hosts where ssize_t is wider
than off_t, so off_t is safe. */
typedef off_t regoff_t;
/* The type of nonnegative object indexes. Traditionally, GNU regex
uses 'int' for these. Code that uses __re_idx_t should work
regardless of whether the type is signed. */
typedef size_t __re_idx_t;
/* The type of object sizes. */
typedef size_t __re_size_t;
/* The type of object sizes, in places where the traditional code
uses unsigned long int. */
typedef size_t __re_long_size_t;
#else
/* Use types that are binary-compatible with the traditional GNU regex
implementation, which mishandles strings longer than INT_MAX. */
typedef int regoff_t;
typedef int __re_idx_t;
typedef unsigned int __re_size_t;
typedef unsigned long int __re_long_size_t;
#endif
/* The following two types have to be signed and unsigned integer type
wide enough to hold a value of a pointer. For most ANSI compilers
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;
/* If this bit is not set, then \ inside a bracket expression is literal.
If set, then such a \ quotes the following character. */
#define RE_BACKSLASH_ESCAPE_IN_LISTS ((unsigned long int) 1)
#define REG_BACKSLASH_ESCAPE_IN_LISTS 1ul
/* If this bit is not set, then + and ? are operators, and \+ and \? are
literals.
If set, then \+ and \? are operators and + and ? are literals. */
#define RE_BK_PLUS_QM (RE_BACKSLASH_ESCAPE_IN_LISTS << 1)
#define REG_BK_PLUS_QM (1ul << 1)
/* If this bit is set, then character classes are supported. They are:
[:alpha:], [:upper:], [:lower:], [:digit:], [:alnum:], [:xdigit:],
[:space:], [:print:], [:punct:], [:graph:], and [:cntrl:].
If not set, then character classes are not supported. */
#define RE_CHAR_CLASSES (RE_BK_PLUS_QM << 1)
#define REG_CHAR_CLASSES (1ul << 2)
/* If this bit is set, then ^ and $ are always anchors (outside bracket
expressions, of course).
......@@ -74,11 +120,11 @@ typedef unsigned long int reg_syntax_t;
$ is an anchor if it is at the end of a regular expression, or
before a close-group or an alternation operator.
This bit could be (re)combined with RE_CONTEXT_INDEP_OPS, because
This bit could be (re)combined with REG_CONTEXT_INDEP_OPS, because
POSIX draft 11.2 says that * etc. in leading positions is undefined.
We already implemented a previous draft which made those constructs
invalid, though, so we haven't changed the code back. */
#define RE_CONTEXT_INDEP_ANCHORS (RE_CHAR_CLASSES << 1)
#define REG_CONTEXT_INDEP_ANCHORS (1ul << 3)
/* If this bit is set, then special characters are always special
regardless of where they are in the pattern.
......@@ -86,71 +132,70 @@ typedef unsigned long int reg_syntax_t;
some contexts; otherwise they are ordinary. Specifically,
* + ? and intervals are only special when not after the beginning,
open-group, or alternation operator. */
#define RE_CONTEXT_INDEP_OPS (RE_CONTEXT_INDEP_ANCHORS << 1)
#define REG_CONTEXT_INDEP_OPS (1ul << 4)
/* If this bit is set, then *, +, ?, and { cannot be first in an re or
immediately after an alternation or begin-group operator. */
#define RE_CONTEXT_INVALID_OPS (RE_CONTEXT_INDEP_OPS << 1)
#define REG_CONTEXT_INVALID_OPS (1ul << 5)
/* If this bit is set, then . matches newline.
If not set, then it doesn't. */
#define RE_DOT_NEWLINE (RE_CONTEXT_INVALID_OPS << 1)
#define REG_DOT_NEWLINE (1ul << 6)
/* If this bit is set, then . doesn't match NUL.
If not set, then it does. */
#define RE_DOT_NOT_NULL (RE_DOT_NEWLINE << 1)
#define REG_DOT_NOT_NULL (1ul << 7)
/* If this bit is set, nonmatching lists [^...] do not match newline.
If not set, they do. */
#define RE_HAT_LISTS_NOT_NEWLINE (RE_DOT_NOT_NULL << 1)
#define REG_HAT_LISTS_NOT_NEWLINE (1ul << 8)
/* If this bit is set, either \{...\} or {...} defines an
interval, depending on RE_NO_BK_BRACES.
interval, depending on REG_NO_BK_BRACES.
If not set, \{, \}, {, and } are literals. */
#define RE_INTERVALS (RE_HAT_LISTS_NOT_NEWLINE << 1)
#define REG_INTERVALS (1ul << 9)
/* If this bit is set, +, ? and | aren't recognized as operators.
If not set, they are. */
#define RE_LIMITED_OPS (RE_INTERVALS << 1)
#define REG_LIMITED_OPS (1ul << 10)
/* If this bit is set, newline is an alternation operator.
If not set, newline is literal. */
#define RE_NEWLINE_ALT (RE_LIMITED_OPS << 1)
#define REG_NEWLINE_ALT (1ul << 11)
/* If this bit is set, then `{...}' defines an interval, and \{ and \}
are literals.
If not set, then `\{...\}' defines an interval. */
#define RE_NO_BK_BRACES (RE_NEWLINE_ALT << 1)
#define REG_NO_BK_BRACES (1ul << 12)
/* If this bit is set, (...) defines a group, and \( and \) are literals.
If not set, \(...\) defines a group, and ( and ) are literals. */
#define RE_NO_BK_PARENS (RE_NO_BK_BRACES << 1)
#define REG_NO_BK_PARENS (1ul << 13)
/* If this bit is set, then \<digit> matches <digit>.
If not set, then \<digit> is a back-reference. */
#define RE_NO_BK_REFS (RE_NO_BK_PARENS << 1)
#define REG_NO_BK_REFS (1ul << 14)
/* If this bit is set, then | is an alternation operator, and \| is literal.
If not set, then \| is an alternation operator, and | is literal. */
#define RE_NO_BK_VBAR (RE_NO_BK_REFS << 1)
#define REG_NO_BK_VBAR (1ul << 15)
/* If this bit is set, then an ending range point collating higher
than the starting range point, as in [z-a], is invalid.
If not set, then when ending range point collates higher than the
starting range point, the range is ignored. */
#define RE_NO_EMPTY_RANGES (RE_NO_BK_VBAR << 1)
If not set, the containing range is empty and does not match any string. */
#define REG_NO_EMPTY_RANGES (1ul << 16)
/* If this bit is set, then an unmatched ) is ordinary.
If not set, then an unmatched ) is invalid. */
#define RE_UNMATCHED_RIGHT_PAREN_ORD (RE_NO_EMPTY_RANGES << 1)
#define REG_UNMATCHED_RIGHT_PAREN_ORD (1ul << 17)
/* If this bit is set, succeed as soon as we match the whole pattern,
without further backtracking. */
#define RE_NO_POSIX_BACKTRACKING (RE_UNMATCHED_RIGHT_PAREN_ORD << 1)
#define REG_NO_POSIX_BACKTRACKING (1ul << 18)
/* If this bit is set, do not process the GNU regex operators.
If not set, then the GNU regex operators are recognized. */
#define RE_NO_GNU_OPS (RE_NO_POSIX_BACKTRACKING << 1)
#define REG_NO_GNU_OPS (1ul << 19)
/* If this bit is set, turn on internal regex debugging.
If not set, and debugging was on, turn it off.
......@@ -158,29 +203,29 @@ typedef unsigned long int reg_syntax_t;
We define this bit always, so that all that's needed to turn on
debugging is to recompile regex.c; the calling code can always have
this bit set, and it won't affect anything in the normal case. */
#define RE_DEBUG (RE_NO_GNU_OPS << 1)
#define REG_DEBUG (1ul << 20)
/* If this bit is set, a syntactically invalid interval is treated as
a string of ordinary characters. For example, the ERE 'a{1' is
treated as 'a\{1'. */
#define RE_INVALID_INTERVAL_ORD (RE_DEBUG << 1)
#define REG_INVALID_INTERVAL_ORD (1ul << 21)
/* If this bit is set, then ignore case when matching.
If not set, then case is significant. */
#define RE_ICASE (RE_INVALID_INTERVAL_ORD << 1)
#define REG_IGNORE_CASE (1ul << 22)
/* This bit is used internally like RE_CONTEXT_INDEP_ANCHORS but only
/* This bit is used internally like REG_CONTEXT_INDEP_ANCHORS but only
for ^, because it is difficult to scan the regex backwards to find
whether ^ should be special. */
#define RE_CARET_ANCHORS_HERE (RE_ICASE << 1)
#define REG_CARET_ANCHORS_HERE (1ul << 23)
/* If this bit is set, then \{ cannot be first in an bre or
immediately after an alternation or begin-group operator. */
#define RE_CONTEXT_INVALID_DUP (RE_CARET_ANCHORS_HERE << 1)
#define REG_CONTEXT_INVALID_DUP (1ul << 24)
/* If this bit is set, then no_sub will be set to 1 during
re_compile_pattern. */
#define RE_NO_SUB (RE_CONTEXT_INVALID_DUP << 1)
#define REG_NO_SUB (1ul << 25)
/* This global variable defines the particular regexp syntax to use (for
some interfaces). When a regexp is compiled, the syntax used is
......@@ -192,81 +237,78 @@ extern reg_syntax_t re_syntax_options;
(The [[[ comments delimit what gets put into the Texinfo file, so
don't delete them!) */
/* [[[begin syntaxes]]] */
#define RE_SYNTAX_EMACS 0
#define RE_SYNTAX_AWK \
(RE_BACKSLASH_ESCAPE_IN_LISTS | RE_DOT_NOT_NULL \
| RE_NO_BK_PARENS | RE_NO_BK_REFS \
| RE_NO_BK_VBAR | RE_NO_EMPTY_RANGES \
| RE_DOT_NEWLINE | RE_CONTEXT_INDEP_ANCHORS \
| RE_UNMATCHED_RIGHT_PAREN_ORD | RE_NO_GNU_OPS)
#define RE_SYNTAX_GNU_AWK \
((RE_SYNTAX_POSIX_EXTENDED | RE_BACKSLASH_ESCAPE_IN_LISTS | RE_DEBUG) \
& ~(RE_DOT_NOT_NULL | RE_INTERVALS | RE_CONTEXT_INDEP_OPS \
| RE_CONTEXT_INVALID_OPS ))
#define RE_SYNTAX_POSIX_AWK \
(RE_SYNTAX_POSIX_EXTENDED | RE_BACKSLASH_ESCAPE_IN_LISTS \
| RE_INTERVALS | RE_NO_GNU_OPS)
#define RE_SYNTAX_GREP \
(RE_BK_PLUS_QM | RE_CHAR_CLASSES \
| RE_HAT_LISTS_NOT_NEWLINE | RE_INTERVALS \
| RE_NEWLINE_ALT)
#define RE_SYNTAX_EGREP \
(RE_CHAR_CLASSES | RE_CONTEXT_INDEP_ANCHORS \
| RE_CONTEXT_INDEP_OPS | RE_HAT_LISTS_NOT_NEWLINE \
| RE_NEWLINE_ALT | RE_NO_BK_PARENS \
| RE_NO_BK_VBAR)
#define RE_SYNTAX_POSIX_EGREP \
(RE_SYNTAX_EGREP | RE_INTERVALS | RE_NO_BK_BRACES \
| RE_INVALID_INTERVAL_ORD)
#define REG_SYNTAX_EMACS 0
#define REG_SYNTAX_AWK \
(REG_BACKSLASH_ESCAPE_IN_LISTS | REG_DOT_NOT_NULL \
| REG_NO_BK_PARENS | REG_NO_BK_REFS \
| REG_NO_BK_VBAR | REG_NO_EMPTY_RANGES \
| REG_DOT_NEWLINE | REG_CONTEXT_INDEP_ANCHORS \
| REG_UNMATCHED_RIGHT_PAREN_ORD | REG_NO_GNU_OPS)
#define REG_SYNTAX_GNU_AWK \
((REG_SYNTAX_POSIX_EXTENDED | REG_BACKSLASH_ESCAPE_IN_LISTS \
| REG_DEBUG) \
& ~(REG_DOT_NOT_NULL | REG_INTERVALS | REG_CONTEXT_INDEP_OPS \
| REG_CONTEXT_INVALID_OPS ))
#define REG_SYNTAX_POSIX_AWK \
(REG_SYNTAX_POSIX_EXTENDED | REG_BACKSLASH_ESCAPE_IN_LISTS \
| REG_INTERVALS | REG_NO_GNU_OPS)
#define REG_SYNTAX_GREP \
(REG_BK_PLUS_QM | REG_CHAR_CLASSES \
| REG_HAT_LISTS_NOT_NEWLINE | REG_INTERVALS \
| REG_NEWLINE_ALT)
#define REG_SYNTAX_EGREP \
(REG_CHAR_CLASSES | REG_CONTEXT_INDEP_ANCHORS \
| REG_CONTEXT_INDEP_OPS | REG_HAT_LISTS_NOT_NEWLINE \
| REG_NEWLINE_ALT | REG_NO_BK_PARENS \
| REG_NO_BK_VBAR)
#define REG_SYNTAX_POSIX_EGREP \
(REG_SYNTAX_EGREP | REG_INTERVALS | REG_NO_BK_BRACES \
| REG_INVALID_INTERVAL_ORD)
/* P1003.2/D11.2, section 4.20.7.1, lines 5078ff. */
#define RE_SYNTAX_ED RE_SYNTAX_POSIX_BASIC
#define REG_SYNTAX_ED REG_SYNTAX_POSIX_BASIC
#define RE_SYNTAX_SED RE_SYNTAX_POSIX_BASIC
#define REG_SYNTAX_SED REG_SYNTAX_POSIX_BASIC
/* Syntax bits common to both basic and extended POSIX regex syntax. */
#define _RE_SYNTAX_POSIX_COMMON \
(RE_CHAR_CLASSES | RE_DOT_NEWLINE | RE_DOT_NOT_NULL \
| RE_INTERVALS | RE_NO_EMPTY_RANGES)
#define _REG_SYNTAX_POSIX_COMMON \
(REG_CHAR_CLASSES | REG_DOT_NEWLINE | REG_DOT_NOT_NULL \
| REG_INTERVALS | REG_NO_EMPTY_RANGES)
#define RE_SYNTAX_POSIX_BASIC \
(_RE_SYNTAX_POSIX_COMMON | RE_BK_PLUS_QM | RE_CONTEXT_INVALID_DUP)
#define REG_SYNTAX_POSIX_BASIC \
(_REG_SYNTAX_POSIX_COMMON | REG_BK_PLUS_QM | REG_CONTEXT_INVALID_DUP)
/* Differs from ..._POSIX_BASIC only in that RE_BK_PLUS_QM becomes
RE_LIMITED_OPS, i.e., \? \+ \| are not recognized. Actually, this
/* Differs from ..._POSIX_BASIC only in that REG_BK_PLUS_QM becomes
REG_LIMITED_OPS, i.e., \? \+ \| are not recognized. Actually, this
isn't minimal, since other operators, such as \`, aren't disabled. */
#define RE_SYNTAX_POSIX_MINIMAL_BASIC \
(_RE_SYNTAX_POSIX_COMMON | RE_LIMITED_OPS)
#define RE_SYNTAX_POSIX_EXTENDED \
(_RE_SYNTAX_POSIX_COMMON | RE_CONTEXT_INDEP_ANCHORS \
| RE_CONTEXT_INDEP_OPS | RE_NO_BK_BRACES \
| RE_NO_BK_PARENS | RE_NO_BK_VBAR \
| RE_CONTEXT_INVALID_OPS | RE_UNMATCHED_RIGHT_PAREN_ORD)
/* Differs from ..._POSIX_EXTENDED in that RE_CONTEXT_INDEP_OPS is
removed and RE_NO_BK_REFS is added. */
#define RE_SYNTAX_POSIX_MINIMAL_EXTENDED \
(_RE_SYNTAX_POSIX_COMMON | RE_CONTEXT_INDEP_ANCHORS \
| RE_CONTEXT_INVALID_OPS | RE_NO_BK_BRACES \
| RE_NO_BK_PARENS | RE_NO_BK_REFS \
| RE_NO_BK_VBAR | RE_UNMATCHED_RIGHT_PAREN_ORD)
#define REG_SYNTAX_POSIX_MINIMAL_BASIC \
(_REG_SYNTAX_POSIX_COMMON | REG_LIMITED_OPS)
#define REG_SYNTAX_POSIX_EXTENDED \
(_REG_SYNTAX_POSIX_COMMON | REG_CONTEXT_INDEP_ANCHORS \
| REG_CONTEXT_INDEP_OPS | REG_NO_BK_BRACES \
| REG_NO_BK_PARENS | REG_NO_BK_VBAR \
| REG_CONTEXT_INVALID_OPS | REG_UNMATCHED_RIGHT_PAREN_ORD)
/* Differs from ..._POSIX_EXTENDED in that REG_CONTEXT_INDEP_OPS is
removed and REG_NO_BK_REFS is added. */
#define REG_SYNTAX_POSIX_MINIMAL_EXTENDED \
(_REG_SYNTAX_POSIX_COMMON | REG_CONTEXT_INDEP_ANCHORS \
| REG_CONTEXT_INVALID_OPS | REG_NO_BK_BRACES \
| REG_NO_BK_PARENS | REG_NO_BK_REFS \
| REG_NO_BK_VBAR | REG_UNMATCHED_RIGHT_PAREN_ORD)
/* [[[end syntaxes]]] */
/* Maximum number of duplicates an interval can allow. Some systems
(erroneously) define this in other header files, but we want our
value, so remove any previous define. */
#ifdef RE_DUP_MAX
# undef RE_DUP_MAX
#endif
/* If sizeof(int) == 2, then ((1 << 15) - 1) overflows. */
#define RE_DUP_MAX (0x7fff)
/* Maximum number of duplicates an interval can allow. This is
distinct from RE_DUP_MAX, to conform to POSIX name space rules and
to avoid collisions with <limits.h>. */
#define REG_DUP_MAX 32767
/* POSIX `cflags' bits (i.e., information for `regcomp'). */
......@@ -277,16 +319,16 @@ extern reg_syntax_t re_syntax_options;
/* If this bit is set, then ignore case when matching.
If not set, then case is significant. */
#define REG_ICASE (REG_EXTENDED << 1)
#define REG_ICASE (1 << 1)
/* If this bit is set, then anchors do not match at newline
characters in the string.
If not set, then anchors do match at newlines. */
#define REG_NEWLINE (REG_ICASE << 1)
#define REG_NEWLINE (1 << 2)
/* If this bit is set, then report only success or fail in regexec.
If not set, then returns differ between not matching and errors. */
#define REG_NOSUB (REG_NEWLINE << 1)
#define REG_NOSUB (1 << 3)
/* POSIX `eflags' bits (i.e., information for regexec). */
......@@ -307,74 +349,131 @@ extern reg_syntax_t re_syntax_options;
/* If any error codes are removed, changed, or added, update the
`re_error_msg' table in regex.c. */
`__re_error_msgid' table in regcomp.c. */
typedef enum
{
#ifdef _XOPEN_SOURCE
REG_ENOSYS = -1, /* This will never happen for this implementation. */
#endif
_REG_ENOSYS = -1, /* This will never happen for this implementation. */
#define REG_ENOSYS _REG_ENOSYS
_REG_NOERROR, /* Success. */
#define REG_NOERROR _REG_NOERROR
REG_NOERROR = 0, /* Success. */
REG_NOMATCH, /* Didn't find a match (for regexec). */
_REG_NOMATCH, /* Didn't find a match (for regexec). */
#define REG_NOMATCH _REG_NOMATCH
/* POSIX regcomp return error codes. (In the order listed in the
standard.) */
REG_BADPAT, /* Invalid pattern. */
REG_ECOLLATE, /* Inalid collating element. */
REG_ECTYPE, /* Invalid character class name. */
REG_EESCAPE, /* Trailing backslash. */
REG_ESUBREG, /* Invalid back reference. */
REG_EBRACK, /* Unmatched left bracket. */
REG_EPAREN, /* Parenthesis imbalance. */
REG_EBRACE, /* Unmatched \{. */
REG_BADBR, /* Invalid contents of \{\}. */
REG_ERANGE, /* Invalid range end. */
REG_ESPACE, /* Ran out of memory. */
REG_BADRPT, /* No preceding re for repetition op. */
_REG_BADPAT, /* Invalid pattern. */
#define REG_BADPAT _REG_BADPAT
_REG_ECOLLATE, /* Inalid collating element. */
#define REG_ECOLLATE _REG_ECOLLATE
_REG_ECTYPE, /* Invalid character class name. */
#define REG_ECTYPE _REG_ECTYPE
_REG_EESCAPE, /* Trailing backslash. */
#define REG_EESCAPE _REG_EESCAPE
_REG_ESUBREG, /* Invalid back reference. */
#define REG_ESUBREG _REG_ESUBREG
_REG_EBRACK, /* Unmatched left bracket. */
#define REG_EBRACK _REG_EBRACK
_REG_EPAREN, /* Parenthesis imbalance. */
#define REG_EPAREN _REG_EPAREN
_REG_EBRACE, /* Unmatched \{. */
#define REG_EBRACE _REG_EBRACE
_REG_BADBR, /* Invalid contents of \{\}. */
#define REG_BADBR _REG_BADBR
_REG_ERANGE, /* Invalid range end. */
#define REG_ERANGE _REG_ERANGE
_REG_ESPACE, /* Ran out of memory. */
#define REG_ESPACE _REG_ESPACE
_REG_BADRPT, /* No preceding re for repetition op. */
#define REG_BADRPT _REG_BADRPT
/* Error codes we've added. */
REG_EEND, /* Premature end. */
REG_ESIZE, /* Compiled pattern bigger than 2^16 bytes. */
REG_ERPAREN /* Unmatched ) or \); not returned from regcomp. */
_REG_EEND, /* Premature end. */
#define REG_EEND _REG_EEND
_REG_ESIZE, /* Compiled pattern bigger than 2^16 bytes. */
#define REG_ESIZE _REG_ESIZE
_REG_ERPAREN /* Unmatched ) or \); not returned from regcomp. */
#define REG_ERPAREN _REG_ERPAREN
} reg_errcode_t;
/* In the traditional GNU implementation, regex.h defined member names
like `buffer' that POSIX does not allow. These members now have
names with leading `re_' (e.g., `re_buffer'). Support the old
names only if _REGEX_SOURCE is defined. New programs should use
the new names. */
#ifdef _REGEX_SOURCE
# define _REG_RE_NAME(id) id
# define _REG_RM_NAME(id) id
#else
# define _REG_RE_NAME(id) re_##id
# define _REG_RM_NAME(id) rm_##id
#endif
/* The user can specify the type of the re_translate member by
defining the macro REG_TRANSLATE_TYPE. In the traditional GNU
implementation, this macro was named RE_TRANSLATE_TYPE, but POSIX
does not allow this. Support the old name only if _REGEX_SOURCE
and if the new name is not defined. New programs should use the new
name. */
#ifndef REG_TRANSLATE_TYPE
# if defined _REGEX_SOURCE && defined RE_TRANSLATE_TYPE
# define REG_TRANSLATE_TYPE RE_TRANSLATE_TYPE
# else
# define REG_TRANSLATE_TYPE char *
# endif
#endif
/* This data structure represents a compiled pattern. Before calling
the pattern compiler, the fields `buffer', `allocated', `fastmap',
`translate', and `no_sub' can be set. After the pattern has been
the pattern compiler), the fields `re_buffer', `re_allocated', `re_fastmap',
`re_translate', and `re_no_sub' can be set. After the pattern has been
compiled, the `re_nsub' field is available. All other fields are
private to the regex routines. */
#ifndef RE_TRANSLATE_TYPE
# define RE_TRANSLATE_TYPE char *
#endif
struct re_pattern_buffer
{
/* [[[begin pattern_buffer]]] */
/* Space that holds the compiled pattern. It is declared as
`unsigned char *' because its elements are
sometimes used as array indexes. */
unsigned char *buffer;
unsigned char *_REG_RE_NAME (buffer);
/* Number of bytes to which `buffer' points. */
unsigned long int allocated;
/* Number of bytes to which `re_buffer' points. */
__re_long_size_t _REG_RE_NAME (allocated);
/* Number of bytes actually used in `buffer'. */
unsigned long int used;
/* Number of bytes actually used in `re_buffer'. */
__re_long_size_t _REG_RE_NAME (used);
/* Syntax setting with which the pattern was compiled. */
reg_syntax_t syntax;
reg_syntax_t _REG_RE_NAME (syntax);
/* Pointer to a fastmap, if any, otherwise zero. re_search uses
the fastmap, if there is one, to skip over impossible
starting points for matches. */
char *fastmap;
char *_REG_RE_NAME (fastmap);
/* Either a translate table to apply to all characters before
comparing them, or zero for no translation. The translation
is applied to a pattern when it is compiled and to a string
when it is matched. */
RE_TRANSLATE_TYPE translate;
REG_TRANSLATE_TYPE _REG_RE_NAME (translate);
/* Number of subexpressions found by the compiler. */
size_t re_nsub;
......@@ -384,59 +483,55 @@ struct re_pattern_buffer
whether or not we should use the fastmap, so we don't set
this absolutely perfectly; see `re_compile_fastmap' (the
`duplicate' case). */
unsigned can_be_null : 1;
unsigned int _REG_RE_NAME (can_be_null) : 1;
/* If REGS_UNALLOCATED, allocate space in the `regs' structure
for `max (RE_NREGS, re_nsub + 1)' groups.
If REGS_REALLOCATE, reallocate space if necessary.
If REGS_FIXED, use what's there. */
#define REGS_UNALLOCATED 0
#define REGS_REALLOCATE 1
#define REGS_FIXED 2
unsigned regs_allocated : 2;
/* If REG_UNALLOCATED, allocate space in the `regs' structure
for `max (REG_NREGS, re_nsub + 1)' groups.
If REG_REALLOCATE, reallocate space if necessary.
If REG_FIXED, use what's there. */
#define REG_UNALLOCATED 0
#define REG_REALLOCATE 1
#define REG_FIXED 2
unsigned int _REG_RE_NAME (regs_allocated) : 2;
/* Set to zero when `regex_compile' compiles a pattern; set to one
by `re_compile_fastmap' if it updates the fastmap. */
unsigned fastmap_accurate : 1;
unsigned int _REG_RE_NAME (fastmap_accurate) : 1;
/* If set, `re_match_2' does not return information about
subexpressions. */
unsigned no_sub : 1;
unsigned int _REG_RE_NAME (no_sub) : 1;
/* If set, a beginning-of-line anchor doesn't match at the
beginning of the string. */
unsigned not_bol : 1;
unsigned int _REG_RE_NAME (not_bol) : 1;
/* Similarly for an end-of-line anchor. */
unsigned not_eol : 1;
unsigned int _REG_RE_NAME (not_eol) : 1;
/* If true, an anchor at a newline matches. */
unsigned newline_anchor : 1;
unsigned int _REG_RE_NAME (newline_anchor) : 1;
/* [[[end pattern_buffer]]] */
};
typedef struct re_pattern_buffer regex_t;
/* Type for byte offsets within the string. POSIX mandates this. */
typedef int regoff_t;
/* This is the structure we store register match data in. See
regex.texinfo for a full description of what registers match. */
struct re_registers
{
unsigned num_regs;
regoff_t *start;
regoff_t *end;
__re_size_t _REG_RM_NAME (num_regs);
regoff_t *_REG_RM_NAME (start);
regoff_t *_REG_RM_NAME (end);
};
/* If `regs_allocated' is REGS_UNALLOCATED in the pattern buffer,
/* If `regs_allocated' is REG_UNALLOCATED in the pattern buffer,
`re_match_2' returns information about at least this many registers
the first time a `regs' structure is passed. */
#ifndef RE_NREGS
# define RE_NREGS 30
#ifndef REG_NREGS
# define REG_NREGS 30
#endif
......@@ -451,70 +546,57 @@ typedef struct
/* Declarations for routines. */
/* To avoid duplicating every routine declaration -- once with a
prototype (if we are ANSI), and once without (if we aren't) -- we
use the following macro to declare argument types. This
unfortunately clutters up the declarations a bit, but I think it's
worth it. */
#if __STDC__
# define _RE_ARGS(args) args
#else /* not __STDC__ */
# define _RE_ARGS(args) ()
#endif /* not __STDC__ */
/* Sets the current default syntax to SYNTAX, and return the old syntax.
You can also simply assign to the `re_syntax_options' variable. */
extern reg_syntax_t re_set_syntax _RE_ARGS ((reg_syntax_t syntax));
extern reg_syntax_t re_set_syntax (reg_syntax_t __syntax);
/* Compile the regular expression PATTERN, with length LENGTH
and syntax given by the global `re_syntax_options', into the buffer
BUFFER. Return NULL if successful, and an error string if not. */
extern const char *re_compile_pattern
_RE_ARGS ((const char *pattern, size_t length,
struct re_pattern_buffer *buffer));
extern const char *re_compile_pattern (const char *__pattern, size_t __length,
struct re_pattern_buffer *__buffer);
/* Compile a fastmap for the compiled pattern in BUFFER; used to
accelerate searches. Return 0 if successful and -2 if was an
internal error. */
extern int re_compile_fastmap _RE_ARGS ((struct re_pattern_buffer *buffer));
extern int re_compile_fastmap (struct re_pattern_buffer *__buffer);
/* Search in the string STRING (with length LENGTH) for the pattern
compiled into BUFFER. Start searching at position START, for RANGE
characters. Return the starting position of the match, -1 for no
match, or -2 for an internal error. Also return register
information in REGS (if REGS and BUFFER->no_sub are nonzero). */
extern int re_search
_RE_ARGS ((struct re_pattern_buffer *buffer, const char *string,
int length, int start, int range, struct re_registers *regs));
information in REGS (if REGS and BUFFER->re_no_sub are nonzero). */
extern regoff_t re_search (struct re_pattern_buffer *__buffer,
const char *__string, __re_idx_t __length,
__re_idx_t __start, regoff_t __range,
struct re_registers *__regs);
/* Like `re_search', but search in the concatenation of STRING1 and
STRING2. Also, stop searching at index START + STOP. */
extern int re_search_2
_RE_ARGS ((struct re_pattern_buffer *buffer, const char *string1,
int length1, const char *string2, int length2,
int start, int range, struct re_registers *regs, int stop));
extern regoff_t re_search_2 (struct re_pattern_buffer *__buffer,
const char *__string1, __re_idx_t __length1,
const char *__string2, __re_idx_t __length2,
__re_idx_t __start, regoff_t __range,
struct re_registers *__regs,
__re_idx_t __stop);
/* Like `re_search', but return how many characters in STRING the regexp
in BUFFER matched, starting at position START. */
extern int re_match
_RE_ARGS ((struct re_pattern_buffer *buffer, const char *string,
int length, int start, struct re_registers *regs));
extern regoff_t re_match (struct re_pattern_buffer *__buffer,
const char *__string, __re_idx_t __length,
__re_idx_t __start, struct re_registers *__regs);
/* Relates to `re_match' as `re_search_2' relates to `re_search'. */
extern int re_match_2
_RE_ARGS ((struct re_pattern_buffer *buffer, const char *string1,
int length1, const char *string2, int length2,
int start, struct re_registers *regs, int stop));
extern regoff_t re_match_2 (struct re_pattern_buffer *__buffer,
const char *__string1, __re_idx_t __length1,
const char *__string2, __re_idx_t __length2,
__re_idx_t __start, struct re_registers *__regs,
__re_idx_t __stop);
/* Set REGS to hold NUM_REGS registers, storing them in STARTS and
......@@ -529,15 +611,16 @@ extern int re_match_2
Unless this function is called, the first search or match using
PATTERN_BUFFER will allocate its own register data, without
freeing the old data. */
extern void re_set_registers
_RE_ARGS ((struct re_pattern_buffer *buffer, struct re_registers *regs,
unsigned num_regs, regoff_t *starts, regoff_t *ends));
extern void re_set_registers (struct re_pattern_buffer *__buffer,
struct re_registers *__regs,
__re_size_t __num_regs,
regoff_t *__starts, regoff_t *__ends);
#if defined _REGEX_RE_COMP || defined _LIBC
# ifndef _CRAY
/* 4.2 bsd compatibility. */
extern char *re_comp _RE_ARGS ((const char *));
extern int re_exec _RE_ARGS ((const char *));
extern char *re_comp (const char *);
extern int re_exec (const char *);
# endif
#endif
......@@ -562,20 +645,114 @@ extern int re_exec _RE_ARGS ((const char *));
#endif
/* POSIX compatibility. */
extern int regcomp _RE_ARGS ((regex_t *__restrict __preg,
const char *__restrict __pattern,
int __cflags));
extern int regcomp (regex_t *__restrict __preg,
const char *__restrict __pattern,
int __cflags);
extern int regexec (const regex_t *__restrict __preg,
const char *__restrict __string, size_t __nmatch,
regmatch_t __pmatch[__restrict_arr],
int __eflags);
extern size_t regerror (int __errcode, const regex_t *__restrict __preg,
char *__restrict __errbuf, size_t __errbuf_size);
extern void regfree (regex_t *__preg);
#ifdef _REGEX_SOURCE
extern int regexec _RE_ARGS ((const regex_t *__restrict __preg,
const char *__restrict __string, size_t __nmatch,
regmatch_t __pmatch[__restrict_arr],
int __eflags));
/* Define the POSIX-compatible member names in terms of the
incompatible (and deprecated) names established by _REG_RE_NAME.
New programs should use the re_* names. */
extern size_t regerror _RE_ARGS ((int __errcode, const regex_t *__preg,
char *__errbuf, size_t __errbuf_size));
# define re_allocated allocated
# define re_buffer buffer
# define re_can_be_null can_be_null
# define re_fastmap fastmap
# define re_fastmap_accurate fastmap_accurate
# define re_newline_anchor newline_anchor
# define re_no_sub no_sub
# define re_not_bol not_bol
# define re_not_eol not_eol
# define re_regs_allocated regs_allocated
# define re_syntax syntax
# define re_translate translate
# define re_used used
extern void regfree _RE_ARGS ((regex_t *__preg));
/* Similarly for _REG_RM_NAME. */
# define rm_end end
# define rm_num_regs num_regs
# define rm_start start
/* Undef RE_DUP_MAX first, in case the user has already included a
<limits.h> with an incompatible definition.
On GNU systems, the most common spelling for RE_DUP_MAX's value in
<limits.h> is (0x7ffff), so define RE_DUP_MAX to that, not to
REG_DUP_MAX. This avoid some duplicate-macro-definition warnings
with programs that include <limits.h> after this file.
New programs should not assume that regex.h defines RE_DUP_MAX; to
get the value of RE_DUP_MAX, they should instead include <limits.h>
and possibly invoke the sysconf function. */
# undef RE_DUP_MAX
# define RE_DUP_MAX (0x7fff)
/* Define the following symbols for backward source compatibility.
These symbols violate the POSIX name space rules, and new programs
should avoid them. */
# define REGS_FIXED REG_FIXED
# define REGS_REALLOCATE REG_REALLOCATE
# define REGS_UNALLOCATED REG_UNALLOCATED
# define RE_BACKSLASH_ESCAPE_IN_LISTS REG_BACKSLASH_ESCAPE_IN_LISTS
# define RE_BK_PLUS_QM REG_BK_PLUS_QM
# define RE_CARET_ANCHORS_HERE REG_CARET_ANCHORS_HERE
# define RE_CHAR_CLASSES REG_CHAR_CLASSES
# define RE_CONTEXT_INDEP_ANCHORS REG_CONTEXT_INDEP_ANCHORS
# define RE_CONTEXT_INDEP_OPS REG_CONTEXT_INDEP_OPS
# define RE_CONTEXT_INVALID_DUP REG_CONTEXT_INVALID_DUP
# define RE_CONTEXT_INVALID_OPS REG_CONTEXT_INVALID_OPS
# define RE_DEBUG REG_DEBUG
# define RE_DOT_NEWLINE REG_DOT_NEWLINE
# define RE_DOT_NOT_NULL REG_DOT_NOT_NULL
# define RE_HAT_LISTS_NOT_NEWLINE REG_HAT_LISTS_NOT_NEWLINE
# define RE_ICASE REG_IGNORE_CASE /* avoid collision with REG_ICASE */
# define RE_INTERVALS REG_INTERVALS
# define RE_INVALID_INTERVAL_ORD REG_INVALID_INTERVAL_ORD
# define RE_LIMITED_OPS REG_LIMITED_OPS
# define RE_NEWLINE_ALT REG_NEWLINE_ALT
# define RE_NO_BK_BRACES REG_NO_BK_BRACES
# define RE_NO_BK_PARENS REG_NO_BK_PARENS
# define RE_NO_BK_REFS REG_NO_BK_REFS
# define RE_NO_BK_VBAR REG_NO_BK_VBAR
# define RE_NO_EMPTY_RANGES REG_NO_EMPTY_RANGES
# define RE_NO_GNU_OPS REG_NO_GNU_OPS
# define RE_NO_POSIX_BACKTRACKING REG_NO_POSIX_BACKTRACKING
# define RE_NO_SUB REG_NO_SUB
# define RE_NREGS REG_NREGS
# define RE_SYNTAX_AWK REG_SYNTAX_AWK
# define RE_SYNTAX_ED REG_SYNTAX_ED
# define RE_SYNTAX_EGREP REG_SYNTAX_EGREP
# define RE_SYNTAX_EMACS REG_SYNTAX_EMACS
# define RE_SYNTAX_GNU_AWK REG_SYNTAX_GNU_AWK
# define RE_SYNTAX_GREP REG_SYNTAX_GREP
# define RE_SYNTAX_POSIX_AWK REG_SYNTAX_POSIX_AWK
# define RE_SYNTAX_POSIX_BASIC REG_SYNTAX_POSIX_BASIC
# define RE_SYNTAX_POSIX_EGREP REG_SYNTAX_POSIX_EGREP
# define RE_SYNTAX_POSIX_EXTENDED REG_SYNTAX_POSIX_EXTENDED
# define RE_SYNTAX_POSIX_MINIMAL_BASIC REG_SYNTAX_POSIX_MINIMAL_BASIC
# define RE_SYNTAX_POSIX_MINIMAL_EXTENDED REG_SYNTAX_POSIX_MINIMAL_EXTENDED
# define RE_SYNTAX_SED REG_SYNTAX_SED
# define RE_UNMATCHED_RIGHT_PAREN_ORD REG_UNMATCHED_RIGHT_PAREN_ORD
# ifndef RE_TRANSLATE_TYPE
# define RE_TRANSLATE_TYPE REG_TRANSLATE_TYPE
# endif
#endif /* defined _REGEX_SOURCE */
#ifdef __cplusplus
}
......
......@@ -17,25 +17,17 @@
with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
static void re_string_construct_common (const char *str, int len,
static void re_string_construct_common (const char *str, Idx len,
re_string_t *pstr,
RE_TRANSLATE_TYPE trans, int icase,
REG_TRANSLATE_TYPE trans, bool icase,
const re_dfa_t *dfa) internal_function;
#ifdef RE_ENABLE_I18N
static int re_string_skip_chars (re_string_t *pstr, int new_raw_idx,
wint_t *last_wc) internal_function;
#endif /* RE_ENABLE_I18N */
static reg_errcode_t register_state (re_dfa_t *dfa, re_dfastate_t *newstate,
unsigned int hash) internal_function;
static re_dfastate_t *create_ci_newstate (re_dfa_t *dfa,
static re_dfastate_t *create_ci_newstate (const re_dfa_t *dfa,
const re_node_set *nodes,
unsigned int hash) internal_function;
static re_dfastate_t *create_cd_newstate (re_dfa_t *dfa,
re_hashval_t hash) internal_function;
static re_dfastate_t *create_cd_newstate (const re_dfa_t *dfa,
const re_node_set *nodes,
unsigned int context,
unsigned int hash) internal_function;
static unsigned int inline calc_state_hash (const re_node_set *nodes,
unsigned int context) internal_function;
re_hashval_t hash) internal_function;
/* Functions for string operation. */
......@@ -43,15 +35,12 @@ static unsigned int inline calc_state_hash (const re_node_set *nodes,
re_string_reconstruct before using the object. */
static reg_errcode_t
re_string_allocate (pstr, str, len, init_len, trans, icase, dfa)
re_string_t *pstr;
const char *str;
int len, init_len, icase;
RE_TRANSLATE_TYPE trans;
const re_dfa_t *dfa;
internal_function
re_string_allocate (re_string_t *pstr, const char *str, Idx len, Idx init_len,
REG_TRANSLATE_TYPE trans, bool icase, const re_dfa_t *dfa)
{
reg_errcode_t ret;
int init_buf_len;
Idx init_buf_len;
/* Ensure at least one character fits into the buffers. */
if (init_len < dfa->mb_cur_max)
......@@ -74,12 +63,9 @@ re_string_allocate (pstr, str, len, init_len, trans, icase, dfa)
/* This function allocate the buffers, and initialize them. */
static reg_errcode_t
re_string_construct (pstr, str, len, trans, icase, dfa)
re_string_t *pstr;
const char *str;
int len, icase;
RE_TRANSLATE_TYPE trans;
const re_dfa_t *dfa;
internal_function
re_string_construct (re_string_t *pstr, const char *str, Idx len,
REG_TRANSLATE_TYPE trans, bool icase, const re_dfa_t *dfa)
{
reg_errcode_t ret;
memset (pstr, '\0', sizeof (re_string_t));
......@@ -140,33 +126,32 @@ re_string_construct (pstr, str, len, trans, icase, dfa)
/* Helper functions for re_string_allocate, and re_string_construct. */
static reg_errcode_t
re_string_realloc_buffers (pstr, new_buf_len)
re_string_t *pstr;
int new_buf_len;
internal_function
re_string_realloc_buffers (re_string_t *pstr, Idx new_buf_len)
{
#ifdef RE_ENABLE_I18N
if (pstr->mb_cur_max > 1)
{
wint_t *new_array = re_realloc (pstr->wcs, wint_t, new_buf_len);
if (BE (new_array == NULL, 0))
wint_t *new_wcs = re_xrealloc (pstr->wcs, wint_t, new_buf_len);
if (BE (new_wcs == NULL, 0))
return REG_ESPACE;
pstr->wcs = new_array;
pstr->wcs = new_wcs;
if (pstr->offsets != NULL)
{
int *new_array = re_realloc (pstr->offsets, int, new_buf_len);
if (BE (new_array == NULL, 0))
Idx *new_offsets = re_xrealloc (pstr->offsets, Idx, new_buf_len);
if (BE (new_offsets == NULL, 0))
return REG_ESPACE;
pstr->offsets = new_array;
pstr->offsets = new_offsets;
}
}
#endif /* RE_ENABLE_I18N */
if (pstr->mbs_allocated)
{
unsigned char *new_array = re_realloc (pstr->mbs, unsigned char,
new_buf_len);
if (BE (new_array == NULL, 0))
unsigned char *new_mbs = re_realloc (pstr->mbs, unsigned char,
new_buf_len);
if (BE (new_mbs == NULL, 0))
return REG_ESPACE;
pstr->mbs = new_array;
pstr->mbs = new_mbs;
}
pstr->bufs_len = new_buf_len;
return REG_NOERROR;
......@@ -174,19 +159,16 @@ re_string_realloc_buffers (pstr, new_buf_len)
static void
re_string_construct_common (str, len, pstr, trans, icase, dfa)
const char *str;
int len;
re_string_t *pstr;
RE_TRANSLATE_TYPE trans;
int icase;
const re_dfa_t *dfa;
internal_function
re_string_construct_common (const char *str, Idx len, re_string_t *pstr,
REG_TRANSLATE_TYPE trans, bool icase,
const re_dfa_t *dfa)
{
pstr->raw_mbs = (const unsigned char *) str;
pstr->len = len;
pstr->raw_len = len;
pstr->trans = (unsigned RE_TRANSLATE_TYPE) trans;
pstr->icase = icase ? 1 : 0;
pstr->trans = (unsigned REG_TRANSLATE_TYPE) trans;
pstr->icase = icase;
pstr->mbs_allocated = (trans != NULL || icase);
pstr->mb_cur_max = dfa->mb_cur_max;
pstr->is_utf8 = dfa->is_utf8;
......@@ -209,8 +191,8 @@ re_string_construct_common (str, len, pstr, trans, icase, dfa)
built and starts from PSTR->VALID_LEN. */
static void
build_wcs_buffer (pstr)
re_string_t *pstr;
internal_function
build_wcs_buffer (re_string_t *pstr)
{
#ifdef _LIBC
unsigned char buf[MB_LEN_MAX];
......@@ -219,7 +201,7 @@ build_wcs_buffer (pstr)
unsigned char buf[64];
#endif
mbstate_t prev_st;
int byte_idx, end_idx, remain_len;
Idx byte_idx, end_idx, remain_len;
size_t mbclen;
/* Build the buffers from pstr->valid_len to either pstr->len or
......@@ -276,12 +258,12 @@ build_wcs_buffer (pstr)
/* Build wide character buffer PSTR->WCS like build_wcs_buffer,
but for REG_ICASE. */
static int
build_wcs_upper_buffer (pstr)
re_string_t *pstr;
static reg_errcode_t
internal_function
build_wcs_upper_buffer (re_string_t *pstr)
{
mbstate_t prev_st;
int src_idx, byte_idx, end_idx, remain_len;
Idx src_idx, byte_idx, end_idx, remain_len;
size_t mbclen;
#ifdef _LIBC
char buf[MB_LEN_MAX];
......@@ -319,7 +301,7 @@ build_wcs_upper_buffer (pstr)
mbclen = mbrtowc (&wc,
((const char *) pstr->raw_mbs + pstr->raw_mbs_idx
+ byte_idx), remain_len, &pstr->cur_state);
if (BE (mbclen + 2 > 2, 1))
if (BE ((size_t) (mbclen + 2) > 2, 1))
{
wchar_t wcu = wc;
if (iswlower (wc))
......@@ -387,7 +369,7 @@ build_wcs_upper_buffer (pstr)
else
p = (const char *) pstr->raw_mbs + pstr->raw_mbs_idx + src_idx;
mbclen = mbrtowc (&wc, p, remain_len, &pstr->cur_state);
if (BE (mbclen + 2 > 2, 1))
if (BE ((size_t) (mbclen + 2) > 2, 1))
{
wchar_t wcu = wc;
if (iswlower (wc))
......@@ -410,7 +392,7 @@ build_wcs_upper_buffer (pstr)
if (pstr->offsets == NULL)
{
pstr->offsets = re_malloc (int, pstr->bufs_len);
pstr->offsets = re_xmalloc (Idx, pstr->bufs_len);
if (pstr->offsets == NULL)
return REG_ESPACE;
......@@ -492,14 +474,12 @@ build_wcs_upper_buffer (pstr)
/* Skip characters until the index becomes greater than NEW_RAW_IDX.
Return the index. */
static int
re_string_skip_chars (pstr, new_raw_idx, last_wc)
re_string_t *pstr;
int new_raw_idx;
wint_t *last_wc;
static Idx
internal_function
re_string_skip_chars (re_string_t *pstr, Idx new_raw_idx, wint_t *last_wc)
{
mbstate_t prev_st;
int rawbuf_idx;
Idx rawbuf_idx;
size_t mbclen;
wchar_t wc = 0;
......@@ -507,7 +487,7 @@ re_string_skip_chars (pstr, new_raw_idx, last_wc)
for (rawbuf_idx = pstr->raw_mbs_idx + pstr->valid_raw_len;
rawbuf_idx < new_raw_idx;)
{
int remain_len;
Idx remain_len;
remain_len = pstr->len - rawbuf_idx;
prev_st = pstr->cur_state;
mbclen = mbrtowc (&wc, (const char *) pstr->raw_mbs + rawbuf_idx,
......@@ -530,10 +510,10 @@ re_string_skip_chars (pstr, new_raw_idx, last_wc)
This function is used in case of REG_ICASE. */
static void
build_upper_buffer (pstr)
re_string_t *pstr;
internal_function
build_upper_buffer (re_string_t *pstr)
{
int char_idx, end_idx;
Idx char_idx, end_idx;
end_idx = (pstr->bufs_len > pstr->len) ? pstr->len : pstr->bufs_len;
for (char_idx = pstr->valid_len; char_idx < end_idx; ++char_idx)
......@@ -553,10 +533,10 @@ build_upper_buffer (pstr)
/* Apply TRANS to the buffer in PSTR. */
static void
re_string_translate_buffer (pstr)
re_string_t *pstr;
internal_function
re_string_translate_buffer (re_string_t *pstr)
{
int buf_idx, end_idx;
Idx buf_idx, end_idx;
end_idx = (pstr->bufs_len > pstr->len) ? pstr->len : pstr->bufs_len;
for (buf_idx = pstr->valid_len; buf_idx < end_idx; ++buf_idx)
......@@ -574,12 +554,14 @@ re_string_translate_buffer (pstr)
convert to upper case in case of REG_ICASE, apply translation. */
static reg_errcode_t
re_string_reconstruct (pstr, idx, eflags)
re_string_t *pstr;
int idx, eflags;
internal_function
re_string_reconstruct (re_string_t *pstr, Idx idx, int eflags)
{
int offset = idx - pstr->raw_mbs_idx;
if (BE (offset < 0, 0))
Idx offset;
if (BE (pstr->raw_mbs_idx <= idx, 0))
offset = idx - pstr->raw_mbs_idx;
else
{
/* Reset buffer. */
#ifdef RE_ENABLE_I18N
......@@ -642,7 +624,7 @@ re_string_reconstruct (pstr, idx, eflags)
#ifdef RE_ENABLE_I18N
if (pstr->mb_cur_max > 1)
{
int wcs_idx;
Idx wcs_idx;
wint_t wc = WEOF;
if (pstr->is_utf8)
......@@ -658,8 +640,9 @@ re_string_reconstruct (pstr, idx, eflags)
{
mbstate_t cur_state;
wchar_t wc2;
int mlen = raw + pstr->len - p;
Idx mlen = raw + pstr->len - p;
unsigned char buf[6];
size_t mbclen;
q = p;
if (BE (pstr->trans != NULL, 0))
......@@ -672,14 +655,13 @@ re_string_reconstruct (pstr, idx, eflags)
/* XXX Don't use mbrtowc, we know which conversion
to use (UTF-8 -> UCS4). */
memset (&cur_state, 0, sizeof (cur_state));
mlen = (mbrtowc (&wc2, (const char *) p, mlen,
&cur_state)
- (raw + offset - p));
if (mlen >= 0)
mbclen = mbrtowc (&wc2, (const char *) p, mlen,
&cur_state);
if (raw + offset - p <= mbclen && mbclen < (size_t) -2)
{
memset (&pstr->cur_state, '\0',
sizeof (mbstate_t));
pstr->valid_len = mlen;
pstr->valid_len = mbclen - (raw + offset - p);
wc = wc2;
}
break;
......@@ -693,7 +675,7 @@ re_string_reconstruct (pstr, idx, eflags)
for (wcs_idx = 0; wcs_idx < pstr->valid_len; ++wcs_idx)
pstr->wcs[wcs_idx] = WEOF;
if (pstr->mbs_allocated)
memset (pstr->mbs, 255, pstr->valid_len);
memset (pstr->mbs, -1, pstr->valid_len);
}
pstr->valid_raw_len = pstr->valid_len;
pstr->tip_context = ((BE (pstr->word_ops_used != 0, 0)
......@@ -728,7 +710,7 @@ re_string_reconstruct (pstr, idx, eflags)
{
if (pstr->icase)
{
int ret = build_wcs_upper_buffer (pstr);
reg_errcode_t ret = build_wcs_upper_buffer (pstr);
if (BE (ret != REG_NOERROR, 0))
return ret;
}
......@@ -752,11 +734,11 @@ re_string_reconstruct (pstr, idx, eflags)
}
static unsigned char
re_string_peek_byte_case (pstr, idx)
const re_string_t *pstr;
int idx;
internal_function __attribute ((pure))
re_string_peek_byte_case (const re_string_t *pstr, Idx idx)
{
int ch, off;
int ch;
Idx off;
/* Handle the common (easiest) cases first. */
if (BE (!pstr->mbs_allocated, 1))
......@@ -789,8 +771,8 @@ re_string_peek_byte_case (pstr, idx)
}
static unsigned char
re_string_fetch_byte_case (pstr)
re_string_t *pstr;
internal_function __attribute ((pure))
re_string_fetch_byte_case (re_string_t *pstr)
{
if (BE (!pstr->mbs_allocated, 1))
return re_string_fetch_byte (pstr);
......@@ -798,7 +780,8 @@ re_string_fetch_byte_case (pstr)
#ifdef RE_ENABLE_I18N
if (pstr->offsets_needed)
{
int off, ch;
Idx off;
int ch;
/* For tr_TR.UTF-8 [[:islower:]] there is
[[: CAPITAL LETTER I WITH DOT lower:]] in mbs. Skip
......@@ -826,8 +809,8 @@ re_string_fetch_byte_case (pstr)
}
static void
re_string_destruct (pstr)
re_string_t *pstr;
internal_function
re_string_destruct (re_string_t *pstr)
{
#ifdef RE_ENABLE_I18N
re_free (pstr->wcs);
......@@ -840,12 +823,11 @@ re_string_destruct (pstr)
/* Return the context at IDX in INPUT. */
static unsigned int
re_string_context_at (input, idx, eflags)
const re_string_t *input;
int idx, eflags;
internal_function
re_string_context_at (const re_string_t *input, Idx idx, int eflags)
{
int c;
if (BE (idx < 0, 0))
if (BE (! REG_VALID_INDEX (idx), 0))
/* In this case, we use the value stored in input->tip_context,
since we can't know the character in input->mbs[-1] here. */
return input->tip_context;
......@@ -856,15 +838,15 @@ re_string_context_at (input, idx, eflags)
if (input->mb_cur_max > 1)
{
wint_t wc;
int wc_idx = idx;
Idx wc_idx = idx;
while(input->wcs[wc_idx] == WEOF)
{
#ifdef DEBUG
/* It must not happen. */
assert (wc_idx >= 0);
assert (REG_VALID_INDEX (wc_idx));
#endif
--wc_idx;
if (wc_idx < 0)
if (! REG_VALID_INDEX (wc_idx))
return input->tip_context;
}
wc = input->wcs[wc_idx];
......@@ -886,26 +868,24 @@ re_string_context_at (input, idx, eflags)
/* Functions for set operation. */
static reg_errcode_t
re_node_set_alloc (set, size)
re_node_set *set;
int size;
internal_function
re_node_set_alloc (re_node_set *set, Idx size)
{
set->alloc = size;
set->nelem = 0;
set->elems = re_malloc (int, size);
set->elems = re_xmalloc (Idx, size);
if (BE (set->elems == NULL, 0))
return REG_ESPACE;
return REG_NOERROR;
}
static reg_errcode_t
re_node_set_init_1 (set, elem)
re_node_set *set;
int elem;
internal_function
re_node_set_init_1 (re_node_set *set, Idx elem)
{
set->alloc = 1;
set->nelem = 1;
set->elems = re_malloc (int, 1);
set->elems = re_malloc (Idx, 1);
if (BE (set->elems == NULL, 0))
{
set->alloc = set->nelem = 0;
......@@ -916,12 +896,11 @@ re_node_set_init_1 (set, elem)
}
static reg_errcode_t
re_node_set_init_2 (set, elem1, elem2)
re_node_set *set;
int elem1, elem2;
internal_function
re_node_set_init_2 (re_node_set *set, Idx elem1, Idx elem2)
{
set->alloc = 2;
set->elems = re_malloc (int, 2);
set->elems = re_malloc (Idx, 2);
if (BE (set->elems == NULL, 0))
return REG_ESPACE;
if (elem1 == elem2)
......@@ -947,21 +926,20 @@ re_node_set_init_2 (set, elem1, elem2)
}
static reg_errcode_t
re_node_set_init_copy (dest, src)
re_node_set *dest;
const re_node_set *src;
internal_function
re_node_set_init_copy (re_node_set *dest, const re_node_set *src)
{
dest->nelem = src->nelem;
if (src->nelem > 0)
{
dest->alloc = dest->nelem;
dest->elems = re_malloc (int, dest->alloc);
dest->elems = re_malloc (Idx, dest->alloc);
if (BE (dest->elems == NULL, 0))
{
dest->alloc = dest->nelem = 0;
return REG_ESPACE;
}
memcpy (dest->elems, src->elems, src->nelem * sizeof (int));
memcpy (dest->elems, src->elems, src->nelem * sizeof dest->elems[0]);
}
else
re_node_set_init_empty (dest);
......@@ -973,11 +951,11 @@ re_node_set_init_copy (dest, src)
Note: We assume dest->elems is NULL, when dest->alloc is 0. */
static reg_errcode_t
re_node_set_add_intersect (dest, src1, src2)
re_node_set *dest;
const re_node_set *src1, *src2;
internal_function
re_node_set_add_intersect (re_node_set *dest, const re_node_set *src1,
const re_node_set *src2)
{
int i1, i2, is, id, delta, sbase;
Idx i1, i2, is, id, delta, sbase;
if (src1->nelem == 0 || src2->nelem == 0)
return REG_NOERROR;
......@@ -985,8 +963,13 @@ re_node_set_add_intersect (dest, src1, src2)
conservative estimate. */
if (src1->nelem + src2->nelem + dest->nelem > dest->alloc)
{
int new_alloc = src1->nelem + src2->nelem + dest->alloc;
int *new_elems = re_realloc (dest->elems, int, new_alloc);
Idx new_alloc = src1->nelem + src2->nelem + dest->alloc;
Idx *new_elems;
if (sizeof (Idx) < 3
&& (new_alloc < dest->alloc
|| ((Idx) (src1->nelem + src2->nelem) < src1->nelem)))
return REG_ESPACE;
new_elems = re_xrealloc (dest->elems, Idx, new_alloc);
if (BE (new_elems == NULL, 0))
return REG_ESPACE;
dest->elems = new_elems;
......@@ -1004,25 +987,25 @@ re_node_set_add_intersect (dest, src1, src2)
if (src1->elems[i1] == src2->elems[i2])
{
/* Try to find the item in DEST. Maybe we could binary search? */
while (id >= 0 && dest->elems[id] > src1->elems[i1])
while (REG_VALID_INDEX (id) && dest->elems[id] > src1->elems[i1])
--id;
if (id < 0 || dest->elems[id] != src1->elems[i1])
if (! REG_VALID_INDEX (id) || dest->elems[id] != src1->elems[i1])
dest->elems[--sbase] = src1->elems[i1];
if (--i1 < 0 || --i2 < 0)
if (! REG_VALID_INDEX (--i1) || ! REG_VALID_INDEX (--i2))
break;
}
/* Lower the highest of the two items. */
else if (src1->elems[i1] < src2->elems[i2])
{
if (--i2 < 0)
if (! REG_VALID_INDEX (--i2))
break;
}
else
{
if (--i1 < 0)
if (! REG_VALID_INDEX (--i1))
break;
}
}
......@@ -1035,7 +1018,7 @@ re_node_set_add_intersect (dest, src1, src2)
DEST elements are already in place; this is more or
less the same loop that is in re_node_set_merge. */
dest->nelem += delta;
if (delta > 0 && id >= 0)
if (delta > 0 && REG_VALID_INDEX (id))
for (;;)
{
if (dest->elems[is] > dest->elems[id])
......@@ -1049,13 +1032,13 @@ re_node_set_add_intersect (dest, src1, src2)
{
/* Slide from the bottom. */
dest->elems[id + delta] = dest->elems[id];
if (--id < 0)
if (! REG_VALID_INDEX (--id))
break;
}
}
/* Copy remaining SRC elements. */
memcpy (dest->elems, dest->elems + sbase, delta * sizeof (int));
memcpy (dest->elems, dest->elems + sbase, delta * sizeof dest->elems[0]);
return REG_NOERROR;
}
......@@ -1064,15 +1047,17 @@ re_node_set_add_intersect (dest, src1, src2)
DEST. Return value indicate the error code or REG_NOERROR if succeeded. */
static reg_errcode_t
re_node_set_init_union (dest, src1, src2)
re_node_set *dest;
const re_node_set *src1, *src2;
internal_function
re_node_set_init_union (re_node_set *dest, const re_node_set *src1,
const re_node_set *src2)
{
int i1, i2, id;
Idx i1, i2, id;
if (src1 != NULL && src1->nelem > 0 && src2 != NULL && src2->nelem > 0)
{
dest->alloc = src1->nelem + src2->nelem;
dest->elems = re_malloc (int, dest->alloc);
if (sizeof (Idx) < 2 && dest->alloc < src1->nelem)
return REG_ESPACE;
dest->elems = re_xmalloc (Idx, dest->alloc);
if (BE (dest->elems == NULL, 0))
return REG_ESPACE;
}
......@@ -1100,13 +1085,13 @@ re_node_set_init_union (dest, src1, src2)
if (i1 < src1->nelem)
{
memcpy (dest->elems + id, src1->elems + i1,
(src1->nelem - i1) * sizeof (int));
(src1->nelem - i1) * sizeof dest->elems[0]);
id += src1->nelem - i1;
}
else if (i2 < src2->nelem)
{
memcpy (dest->elems + id, src2->elems + i2,
(src2->nelem - i2) * sizeof (int));
(src2->nelem - i2) * sizeof dest->elems[0]);
id += src2->nelem - i2;
}
dest->nelem = id;
......@@ -1117,17 +1102,23 @@ re_node_set_init_union (dest, src1, src2)
DEST. Return value indicate the error code or REG_NOERROR if succeeded. */
static reg_errcode_t
re_node_set_merge (dest, src)
re_node_set *dest;
const re_node_set *src;
internal_function
re_node_set_merge (re_node_set *dest, const re_node_set *src)
{
int is, id, sbase, delta;
Idx is, id, sbase, delta;
if (src == NULL || src->nelem == 0)
return REG_NOERROR;
if (sizeof (Idx) < 3
&& ((Idx) (2 * src->nelem) < src->nelem
|| (Idx) (2 * src->nelem + dest->nelem) < dest->nelem))
return REG_ESPACE;
if (dest->alloc < 2 * src->nelem + dest->nelem)
{
int new_alloc = 2 * (src->nelem + dest->alloc);
int *new_buffer = re_realloc (dest->elems, int, new_alloc);
Idx new_alloc = src->nelem + dest->alloc;
Idx *new_buffer;
if (sizeof (Idx) < 4 && new_alloc < dest->alloc)
return REG_ESPACE;
new_buffer = re_x2realloc (dest->elems, Idx, &new_alloc);
if (BE (new_buffer == NULL, 0))
return REG_ESPACE;
dest->elems = new_buffer;
......@@ -1137,14 +1128,15 @@ re_node_set_merge (dest, src)
if (BE (dest->nelem == 0, 0))
{
dest->nelem = src->nelem;
memcpy (dest->elems, src->elems, src->nelem * sizeof (int));
memcpy (dest->elems, src->elems, src->nelem * sizeof dest->elems[0]);
return REG_NOERROR;
}
/* Copy into the top of DEST the items of SRC that are not
found in DEST. Maybe we could binary search in DEST? */
for (sbase = dest->nelem + 2 * src->nelem,
is = src->nelem - 1, id = dest->nelem - 1; is >= 0 && id >= 0; )
is = src->nelem - 1, id = dest->nelem - 1;
REG_VALID_INDEX (is) && REG_VALID_INDEX (id); )
{
if (dest->elems[id] == src->elems[is])
is--, id--;
......@@ -1154,11 +1146,12 @@ re_node_set_merge (dest, src)
--id;
}
if (is >= 0)
if (REG_VALID_INDEX (is))
{
/* If DEST is exhausted, the remaining items of SRC must be unique. */
sbase -= is + 1;
memcpy (dest->elems + sbase, src->elems, (is + 1) * sizeof (int));
memcpy (dest->elems + sbase, src->elems,
(is + 1) * sizeof dest->elems[0]);
}
id = dest->nelem - 1;
......@@ -1183,11 +1176,11 @@ re_node_set_merge (dest, src)
{
/* Slide from the bottom. */
dest->elems[id + delta] = dest->elems[id];
if (--id < 0)
if (! REG_VALID_INDEX (--id))
{
/* Copy remaining SRC elements. */
memcpy (dest->elems, dest->elems + sbase,
delta * sizeof (int));
delta * sizeof dest->elems[0]);
break;
}
}
......@@ -1198,40 +1191,32 @@ re_node_set_merge (dest, src)
/* Insert the new element ELEM to the re_node_set* SET.
SET should not already have ELEM.
return -1 if an error is occured, return 1 otherwise. */
Return true if successful. */
static int
re_node_set_insert (set, elem)
re_node_set *set;
int elem;
static bool
internal_function
re_node_set_insert (re_node_set *set, Idx elem)
{
int idx;
Idx idx;
/* In case the set is empty. */
if (set->alloc == 0)
{
if (BE (re_node_set_init_1 (set, elem) == REG_NOERROR, 1))
return 1;
else
return -1;
}
return re_node_set_init_1 (set, elem) == REG_NOERROR;
if (BE (set->nelem, 0) == 0)
{
/* We already guaranteed above that set->alloc != 0. */
set->elems[0] = elem;
++set->nelem;
return 1;
return true;
}
/* Realloc if we need. */
if (set->alloc == set->nelem)
{
int *new_array;
set->alloc = set->alloc * 2;
new_array = re_realloc (set->elems, int, set->alloc);
if (BE (new_array == NULL, 0))
return -1;
set->elems = new_array;
Idx *new_elems = re_x2realloc (set->elems, Idx, &set->alloc);
if (BE (new_elems == NULL, 0))
return false;
set->elems = new_elems;
}
/* Move the elements which follows the new element. Test the
......@@ -1251,59 +1236,56 @@ re_node_set_insert (set, elem)
/* Insert the new element. */
set->elems[idx] = elem;
++set->nelem;
return 1;
return true;
}
/* Insert the new element ELEM to the re_node_set* SET.
SET should not already have any element greater than or equal to ELEM.
Return -1 if an error is occured, return 1 otherwise. */
Return true if successful. */
static int
re_node_set_insert_last (set, elem)
re_node_set *set;
int elem;
static bool
internal_function
re_node_set_insert_last (re_node_set *set, Idx elem)
{
/* Realloc if we need. */
if (set->alloc == set->nelem)
{
int *new_array;
set->alloc = (set->alloc + 1) * 2;
new_array = re_realloc (set->elems, int, set->alloc);
if (BE (new_array == NULL, 0))
return -1;
set->elems = new_array;
Idx *new_elems;
new_elems = re_x2realloc (set->elems, Idx, &set->alloc);
if (BE (new_elems == NULL, 0))
return false;
set->elems = new_elems;
}
/* Insert the new element. */
set->elems[set->nelem++] = elem;
return 1;
return true;
}
/* Compare two node sets SET1 and SET2.
return 1 if SET1 and SET2 are equivalent, return 0 otherwise. */
Return true if SET1 and SET2 are equivalent. */
static int
re_node_set_compare (set1, set2)
const re_node_set *set1, *set2;
static bool
internal_function __attribute ((pure))
re_node_set_compare (const re_node_set *set1, const re_node_set *set2)
{
int i;
Idx i;
if (set1 == NULL || set2 == NULL || set1->nelem != set2->nelem)
return 0;
for (i = set1->nelem ; --i >= 0 ; )
return false;
for (i = set1->nelem ; REG_VALID_INDEX (--i) ; )
if (set1->elems[i] != set2->elems[i])
return 0;
return 1;
return false;
return true;
}
/* Return (idx + 1) if SET contains the element ELEM, return 0 otherwise. */
static int
re_node_set_contains (set, elem)
const re_node_set *set;
int elem;
static Idx
internal_function __attribute ((pure))
re_node_set_contains (const re_node_set *set, Idx elem)
{
unsigned int idx, right, mid;
if (set->nelem <= 0)
__re_size_t idx, right, mid;
if (! REG_VALID_NONZERO_INDEX (set->nelem))
return 0;
/* Binary search the element. */
......@@ -1321,9 +1303,8 @@ re_node_set_contains (set, elem)
}
static void
re_node_set_remove_at (set, idx)
re_node_set *set;
int idx;
internal_function
re_node_set_remove_at (re_node_set *set, Idx idx)
{
if (idx < 0 || idx >= set->nelem)
return;
......@@ -1334,32 +1315,31 @@ re_node_set_remove_at (set, idx)
/* Add the token TOKEN to dfa->nodes, and return the index of the token.
Or return -1, if an error will be occured. */
Or return REG_MISSING if an error occurred. */
static int
re_dfa_add_node (dfa, token)
re_dfa_t *dfa;
re_token_t token;
static Idx
internal_function
re_dfa_add_node (re_dfa_t *dfa, re_token_t token)
{
int type = token.type;
if (BE (dfa->nodes_len >= dfa->nodes_alloc, 0))
{
int new_nodes_alloc = dfa->nodes_alloc * 2;
int *new_nexts, *new_indices;
Idx new_nodes_alloc = dfa->nodes_alloc;
Idx *new_nexts, *new_indices;
re_node_set *new_edests, *new_eclosures;
re_token_t *new_array = re_realloc (dfa->nodes, re_token_t,
new_nodes_alloc);
if (BE (new_array == NULL, 0))
return -1;
dfa->nodes = new_array;
new_nexts = re_realloc (dfa->nexts, int, new_nodes_alloc);
new_indices = re_realloc (dfa->org_indices, int, new_nodes_alloc);
new_edests = re_realloc (dfa->edests, re_node_set, new_nodes_alloc);
re_token_t *new_nodes = re_x2realloc (dfa->nodes, re_token_t,
&new_nodes_alloc);
if (BE (new_nodes == NULL, 0))
return REG_MISSING;
dfa->nodes = new_nodes;
new_nexts = re_realloc (dfa->nexts, Idx, new_nodes_alloc);
new_indices = re_realloc (dfa->org_indices, Idx, new_nodes_alloc);
new_edests = re_xrealloc (dfa->edests, re_node_set, new_nodes_alloc);
new_eclosures = re_realloc (dfa->eclosures, re_node_set, new_nodes_alloc);
if (BE (new_nexts == NULL || new_indices == NULL
|| new_edests == NULL || new_eclosures == NULL, 0))
return -1;
return REG_MISSING;
dfa->nexts = new_nexts;
dfa->org_indices = new_indices;
dfa->edests = new_edests;
......@@ -1372,19 +1352,18 @@ re_dfa_add_node (dfa, token)
dfa->nodes[dfa->nodes_len].accept_mb =
(type == OP_PERIOD && dfa->mb_cur_max > 1) || type == COMPLEX_BRACKET;
#endif
dfa->nexts[dfa->nodes_len] = -1;
dfa->nexts[dfa->nodes_len] = REG_MISSING;
re_node_set_init_empty (dfa->edests + dfa->nodes_len);
re_node_set_init_empty (dfa->eclosures + dfa->nodes_len);
return dfa->nodes_len++;
}
static unsigned int inline
calc_state_hash (nodes, context)
const re_node_set *nodes;
unsigned int context;
static inline re_hashval_t
internal_function
calc_state_hash (const re_node_set *nodes, unsigned int context)
{
unsigned int hash = nodes->nelem + context;
int i;
re_hashval_t hash = nodes->nelem + context;
Idx i;
for (i = 0 ; i < nodes->nelem ; i++)
hash += nodes->elems[i];
return hash;
......@@ -1400,15 +1379,17 @@ calc_state_hash (nodes, context)
optimization. */
static re_dfastate_t*
re_acquire_state (err, dfa, nodes)
reg_errcode_t *err;
re_dfa_t *dfa;
const re_node_set *nodes;
internal_function
re_acquire_state (reg_errcode_t *err, re_dfa_t *dfa, const re_node_set *nodes)
{
unsigned int hash;
re_hashval_t hash;
re_dfastate_t *new_state;
struct re_state_table_entry *spot;
int i;
Idx i;
#ifdef lint
/* Suppress bogus uninitialized-variable warnings. */
*err = REG_NOERROR;
#endif
if (BE (nodes->nelem == 0, 0))
{
*err = REG_NOERROR;
......@@ -1448,16 +1429,18 @@ re_acquire_state (err, dfa, nodes)
optimization. */
static re_dfastate_t*
re_acquire_state_context (err, dfa, nodes, context)
reg_errcode_t *err;
re_dfa_t *dfa;
const re_node_set *nodes;
unsigned int context;
internal_function
re_acquire_state_context (reg_errcode_t *err, re_dfa_t *dfa,
const re_node_set *nodes, unsigned int context)
{
unsigned int hash;
re_hashval_t hash;
re_dfastate_t *new_state;
struct re_state_table_entry *spot;
int i;
Idx i;
#ifdef lint
/* Suppress bogus uninitialized-variable warnings. */
*err = REG_NOERROR;
#endif
if (nodes->nelem == 0)
{
*err = REG_NOERROR;
......@@ -1490,14 +1473,12 @@ re_acquire_state_context (err, dfa, nodes, context)
indicates the error code if failed. */
static reg_errcode_t
register_state (dfa, newstate, hash)
re_dfa_t *dfa;
re_dfastate_t *newstate;
unsigned int hash;
internal_function
register_state (const re_dfa_t *dfa, re_dfastate_t *newstate, re_hashval_t hash)
{
struct re_state_table_entry *spot;
reg_errcode_t err;
int i;
Idx i;
newstate->hash = hash;
err = re_node_set_alloc (&newstate->non_eps_nodes, newstate->nodes.nelem);
......@@ -1505,17 +1486,21 @@ register_state (dfa, newstate, hash)
return REG_ESPACE;
for (i = 0; i < newstate->nodes.nelem; i++)
{
int elem = newstate->nodes.elems[i];
Idx elem = newstate->nodes.elems[i];
if (!IS_EPSILON_NODE (dfa->nodes[elem].type))
re_node_set_insert_last (&newstate->non_eps_nodes, elem);
{
bool ok = re_node_set_insert_last (&newstate->non_eps_nodes, elem);
if (BE (! ok, 0))
return REG_ESPACE;
}
}
spot = dfa->state_table + (hash & dfa->state_hash_mask);
if (BE (spot->alloc <= spot->num, 0))
{
int new_alloc = 2 * spot->num + 2;
re_dfastate_t **new_array = re_realloc (spot->array, re_dfastate_t *,
new_alloc);
Idx new_alloc = spot->num;
re_dfastate_t **new_array = re_x2realloc (spot->array, re_dfastate_t *,
&new_alloc);
if (BE (new_array == NULL, 0))
return REG_ESPACE;
spot->array = new_array;
......@@ -1529,16 +1514,15 @@ register_state (dfa, newstate, hash)
Return the new state if succeeded, otherwise return NULL. */
static re_dfastate_t *
create_ci_newstate (dfa, nodes, hash)
re_dfa_t *dfa;
const re_node_set *nodes;
unsigned int hash;
internal_function
create_ci_newstate (const re_dfa_t *dfa, const re_node_set *nodes,
re_hashval_t hash)
{
int i;
Idx i;
reg_errcode_t err;
re_dfastate_t *newstate;
newstate = (re_dfastate_t *) calloc (sizeof (re_dfastate_t), 1);
newstate = re_calloc (re_dfastate_t, 1);
if (BE (newstate == NULL, 0))
return NULL;
err = re_node_set_init_copy (&newstate->nodes, nodes);
......@@ -1580,16 +1564,15 @@ create_ci_newstate (dfa, nodes, hash)
Return the new state if succeeded, otherwise return NULL. */
static re_dfastate_t *
create_cd_newstate (dfa, nodes, context, hash)
re_dfa_t *dfa;
const re_node_set *nodes;
unsigned int context, hash;
internal_function
create_cd_newstate (const re_dfa_t *dfa, const re_node_set *nodes,
unsigned int context, re_hashval_t hash)
{
int i, nctx_nodes = 0;
Idx i, nctx_nodes = 0;
reg_errcode_t err;
re_dfastate_t *newstate;
newstate = (re_dfastate_t *) calloc (sizeof (re_dfastate_t), 1);
newstate = re_calloc (re_dfastate_t, 1);
if (BE (newstate == NULL, 0))
return NULL;
err = re_node_set_init_copy (&newstate->nodes, nodes);
......@@ -1656,8 +1639,8 @@ create_cd_newstate (dfa, nodes, context, hash)
}
static void
free_state (state)
re_dfastate_t *state;
internal_function
free_state (re_dfastate_t *state)
{
re_node_set_free (&state->non_eps_nodes);
re_node_set_free (&state->inveclosure);
......
......@@ -22,10 +22,15 @@
#include <assert.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef _LIBC
# include "strcase.h"
#endif
#if defined HAVE_LANGINFO_H || defined HAVE_LANGINFO_CODESET || defined _LIBC
# include <langinfo.h>
#endif
......@@ -87,11 +92,8 @@
# define BE(expr, val) __builtin_expect (expr, val)
#else
# define BE(expr, val) (expr)
# define inline
#endif
/* Number of bits in a byte. */
#define BYTE_BITS 8
/* Number of single byte character. */
#define SBC_MAX 256
......@@ -114,7 +116,7 @@
# define attribute_hidden
#endif /* not _LIBC */
#ifdef __GNUC__
#if __GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
# define __attribute(arg) __attribute__ (arg)
#else
# define __attribute(arg)
......@@ -123,26 +125,73 @@
extern const char __re_error_msgid[] attribute_hidden;
extern const size_t __re_error_msgid_idx[] attribute_hidden;
/* Number of bits in an unsinged int. */
#define UINT_BITS (sizeof (unsigned int) * BYTE_BITS)
/* Number of unsigned int in an bit_set. */
#define BITSET_UINTS ((SBC_MAX + UINT_BITS - 1) / UINT_BITS)
typedef unsigned int bitset[BITSET_UINTS];
typedef unsigned int *re_bitset_ptr_t;
typedef const unsigned int *re_const_bitset_ptr_t;
#define bitset_set(set,i) (set[i / UINT_BITS] |= 1 << i % UINT_BITS)
#define bitset_clear(set,i) (set[i / UINT_BITS] &= ~(1 << i % UINT_BITS))
#define bitset_contain(set,i) (set[i / UINT_BITS] & (1 << i % UINT_BITS))
#define bitset_empty(set) memset (set, 0, sizeof (unsigned int) * BITSET_UINTS)
#define bitset_set_all(set) \
memset (set, 255, sizeof (unsigned int) * BITSET_UINTS)
#define bitset_copy(dest,src) \
memcpy (dest, src, sizeof (unsigned int) * BITSET_UINTS)
static inline void bitset_not (bitset set);
static inline void bitset_merge (bitset dest, const bitset src);
static inline void bitset_not_merge (bitset dest, const bitset src);
static inline void bitset_mask (bitset dest, const bitset src);
typedef __re_idx_t Idx;
/* Special return value for failure to match. */
#define REG_MISSING ((Idx) -1)
/* Special return value for internal error. */
#define REG_ERROR ((Idx) -2)
/* Test whether N is a valid index, and is not one of the above. */
#ifdef _REGEX_LARGE_OFFSETS
# define REG_VALID_INDEX(n) ((Idx) (n) < REG_ERROR)
#else
# define REG_VALID_INDEX(n) (0 <= (n))
#endif
/* Test whether N is a valid nonzero index. */
#ifdef _REGEX_LARGE_OFFSETS
# define REG_VALID_NONZERO_INDEX(n) ((Idx) ((n) - 1) < (Idx) (REG_ERROR - 1))
#else
# define REG_VALID_NONZERO_INDEX(n) (0 < (n))
#endif
/* A hash value, suitable for computing hash tables. */
typedef __re_size_t re_hashval_t;
/* An integer used to represent a set of bits. It must be unsigned,
and must be at least as wide as unsigned int. */
typedef unsigned long int bitset_word;
/* Maximum value of a bitset word. It must be useful in preprocessor
contexts, and must be consistent with bitset_word. */
#define BITSET_WORD_MAX ULONG_MAX
/* Number of bits in a bitset word. Avoid greater-than-32-bit
integers and unconditional shifts by more than 31 bits, as they're
not portable. */
#if BITSET_WORD_MAX == 0xffffffff
# define BITSET_WORD_BITS 32
#elif BITSET_WORD_MAX >> 31 >> 5 == 1
# define BITSET_WORD_BITS 36
#elif BITSET_WORD_MAX >> 31 >> 16 == 1
# define BITSET_WORD_BITS 48
#elif BITSET_WORD_MAX >> 31 >> 28 == 1
# define BITSET_WORD_BITS 60
#elif BITSET_WORD_MAX >> 31 >> 31 >> 1 == 1
# define BITSET_WORD_BITS 64
#elif BITSET_WORD_MAX >> 31 >> 31 >> 9 == 1
# define BITSET_WORD_BITS 72
#elif BITSET_WORD_MAX >> 31 >> 31 >> 31 >> 31 >> 3 == 1
# define BITSET_WORD_BITS 128
#elif BITSET_WORD_MAX >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 7 == 1
# define BITSET_WORD_BITS 256
#elif BITSET_WORD_MAX >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 31 >> 7 > 1
# define BITSET_WORD_BITS 257 /* any value > SBC_MAX will do here */
# if BITSET_WORD_BITS <= SBC_MAX
# error "Invalid SBC_MAX"
# endif
#else
# error "Add case for new bitset_word size"
#endif
/* Number of bitset words in a bitset. */
#define BITSET_WORDS ((SBC_MAX + BITSET_WORD_BITS - 1) / BITSET_WORD_BITS)
typedef bitset_word bitset[BITSET_WORDS];
typedef bitset_word *re_bitset_ptr_t;
typedef const bitset_word *re_const_bitset_ptr_t;
#define PREV_WORD_CONSTRAINT 0x0001
#define PREV_NOTWORD_CONSTRAINT 0x0002
......@@ -171,9 +220,9 @@ typedef enum
typedef struct
{
int alloc;
int nelem;
int *elems;
Idx alloc;
Idx nelem;
Idx *elems;
} re_node_set;
typedef enum
......@@ -259,19 +308,19 @@ typedef struct
unsigned int non_match : 1;
/* # of multibyte characters. */
int nmbchars;
Idx nmbchars;
/* # of collating symbols. */
int ncoll_syms;
Idx ncoll_syms;
/* # of equivalence classes. */
int nequiv_classes;
Idx nequiv_classes;
/* # of range expressions. */
int nranges;
Idx nranges;
/* # of character classes. */
int nchar_classes;
Idx nchar_classes;
} re_charset_t;
#endif /* RE_ENABLE_I18N */
......@@ -284,7 +333,7 @@ typedef struct
#ifdef RE_ENABLE_I18N
re_charset_t *mbcset; /* for COMPLEX_BRACKET */
#endif /* RE_ENABLE_I18N */
int idx; /* for BACK_REF */
Idx idx; /* for BACK_REF */
re_context_type ctx_type; /* for ANCHOR */
} opr;
#if __GNUC__ >= 2
......@@ -318,40 +367,40 @@ struct re_string_t
#ifdef RE_ENABLE_I18N
/* Store the wide character string which is corresponding to MBS. */
wint_t *wcs;
int *offsets;
Idx *offsets;
mbstate_t cur_state;
#endif
/* Index in RAW_MBS. Each character mbs[i] corresponds to
raw_mbs[raw_mbs_idx + i]. */
int raw_mbs_idx;
Idx raw_mbs_idx;
/* The length of the valid characters in the buffers. */
int valid_len;
Idx valid_len;
/* The corresponding number of bytes in raw_mbs array. */
int valid_raw_len;
Idx valid_raw_len;
/* The length of the buffers MBS and WCS. */
int bufs_len;
Idx bufs_len;
/* The index in MBS, which is updated by re_string_fetch_byte. */
int cur_idx;
Idx cur_idx;
/* length of RAW_MBS array. */
int raw_len;
Idx raw_len;
/* This is RAW_LEN - RAW_MBS_IDX + VALID_LEN - VALID_RAW_LEN. */
int len;
Idx len;
/* End of the buffer may be shorter than its length in the cases such
as re_match_2, re_search_2. Then, we use STOP for end of the buffer
instead of LEN. */
int raw_stop;
Idx raw_stop;
/* This is RAW_STOP - RAW_MBS_IDX adjusted through OFFSETS. */
int stop;
Idx stop;
/* The context of mbs[0]. We store the context independently, since
the context of mbs[0] may be different from raw_mbs[0], which is
the beginning of the input string. */
unsigned int tip_context;
/* The translation passed as a part of an argument of re_compile_pattern. */
unsigned RE_TRANSLATE_TYPE trans;
unsigned REG_TRANSLATE_TYPE trans;
/* Copy of re_dfa_t's word_char. */
re_const_bitset_ptr_t word_char;
/* 1 if REG_ICASE. */
/* true if REG_ICASE. */
unsigned char icase;
unsigned char is_utf8;
unsigned char map_notascii;
......@@ -375,45 +424,20 @@ typedef struct re_dfa_t re_dfa_t;
# endif
#endif
#ifndef RE_NO_INTERNAL_PROTOTYPES
static reg_errcode_t re_string_allocate (re_string_t *pstr, const char *str,
int len, int init_len,
RE_TRANSLATE_TYPE trans, int icase,
const re_dfa_t *dfa)
internal_function;
static reg_errcode_t re_string_construct (re_string_t *pstr, const char *str,
int len, RE_TRANSLATE_TYPE trans,
int icase, const re_dfa_t *dfa)
internal_function;
static reg_errcode_t re_string_reconstruct (re_string_t *pstr, int idx,
int eflags) internal_function;
static reg_errcode_t re_string_realloc_buffers (re_string_t *pstr,
int new_buf_len)
Idx new_buf_len)
internal_function;
# ifdef RE_ENABLE_I18N
#ifdef RE_ENABLE_I18N
static void build_wcs_buffer (re_string_t *pstr) internal_function;
static int build_wcs_upper_buffer (re_string_t *pstr) internal_function;
# endif /* RE_ENABLE_I18N */
static reg_errcode_t build_wcs_upper_buffer (re_string_t *pstr)
internal_function;
#endif /* RE_ENABLE_I18N */
static void build_upper_buffer (re_string_t *pstr) internal_function;
static void re_string_translate_buffer (re_string_t *pstr) internal_function;
static void re_string_destruct (re_string_t *pstr) internal_function;
# ifdef RE_ENABLE_I18N
static int re_string_elem_size_at (const re_string_t *pstr, int idx)
static unsigned int re_string_context_at (const re_string_t *input,
Idx idx, int eflags)
internal_function __attribute ((pure));
static inline int re_string_char_size_at (const re_string_t *pstr, int idx)
internal_function __attribute ((pure));
static inline wint_t re_string_wchar_at (const re_string_t *pstr, int idx)
internal_function __attribute ((pure));
# endif /* RE_ENABLE_I18N */
static unsigned int re_string_context_at (const re_string_t *input, int idx,
int eflags)
internal_function __attribute ((pure));
static unsigned char re_string_peek_byte_case (const re_string_t *pstr,
int idx)
internal_function __attribute ((pure));
static unsigned char re_string_fetch_byte_case (re_string_t *pstr)
internal_function __attribute ((pure));
#endif
#define re_string_peek_byte(pstr, offset) \
((pstr)->mbs[(pstr)->cur_idx + offset])
#define re_string_fetch_byte(pstr) \
......@@ -431,10 +455,86 @@ static unsigned char re_string_fetch_byte_case (re_string_t *pstr)
#define re_string_skip_bytes(pstr,idx) ((pstr)->cur_idx += (idx))
#define re_string_set_index(pstr,idx) ((pstr)->cur_idx = (idx))
#include <alloca.h>
#ifndef _LIBC
# if HAVE_ALLOCA
/* The OS usually guarantees only one guard page at the bottom of the stack,
and a page size can be as small as 4096 bytes. So we cannot safely
allocate anything larger than 4096 bytes. Also care for the possibility
of a few compiler-allocated temporary stack slots. */
# define __libc_use_alloca(n) ((n) < 4032)
# else
/* alloca is implemented with malloc, so just use malloc. */
# define __libc_use_alloca(n) 0
# endif
#endif
#define re_malloc(t,n) ((t *) malloc ((n) * sizeof (t)))
#define re_xmalloc(t,n) ((t *) re_xnmalloc (n, sizeof (t)))
#define re_calloc(t,n) ((t *) calloc (n, sizeof (t)))
#define re_realloc(p,t,n) ((t *) realloc (p, (n) * sizeof (t)))
#define re_xrealloc(p,t,n) ((t *) re_xnrealloc (p, n, sizeof (t)))
#define re_x2realloc(p,t,pn) ((t *) re_x2nrealloc (p, pn, sizeof (t)))
#define re_free(p) free (p)
#ifndef SIZE_MAX
# define SIZE_MAX ((size_t) -1)
#endif
/* Return true if an array of N objects, each of size S, cannot exist
due to size arithmetic overflow. S must be nonzero. */
static inline bool
re_alloc_oversized (size_t n, size_t s)
{
return BE (SIZE_MAX / s < n, 0);
}
/* Return true if an array of (2 * N + 1) objects, each of size S,
cannot exist due to size arithmetic overflow. S must be nonzero. */
static inline bool
re_x2alloc_oversized (size_t n, size_t s)
{
return BE ((SIZE_MAX / s - 1) / 2 < n, 0);
}
/* Allocate an array of N objects, each with S bytes of memory,
dynamically, with error checking. S must be nonzero. */
static inline void *
re_xnmalloc (size_t n, size_t s)
{
return re_alloc_oversized (n, s) ? NULL : malloc (n * s);
}
/* Change the size of an allocated block of memory P to an array of N
objects each of S bytes, with error checking. S must be nonzero. */
static inline void *
re_xnrealloc (void *p, size_t n, size_t s)
{
return re_alloc_oversized (n, s) ? NULL : realloc (p, n * s);
}
/* Reallocate a block of memory P to an array of (2 * (*PN) + 1)
objects each of S bytes, with error checking. S must be nonzero.
If the allocation is successful, set *PN to the new allocation
count and return the resulting pointer. Otherwise, return
NULL. */
static inline void *
re_x2nrealloc (void *p, size_t *pn, size_t s)
{
if (re_x2alloc_oversized (*pn, s))
return NULL;
else
{
/* Add 1 in case *PN is zero. */
size_t n1 = 2 * *pn + 1;
p = realloc (p, n1 * s);
if (BE (p != NULL, 1))
*pn = n1;
return p;
}
}
struct bin_tree_t
{
struct bin_tree_t *parent;
......@@ -447,7 +547,7 @@ struct bin_tree_t
/* `node_idx' is the index in dfa->nodes, if `type' == 0.
Otherwise `type' indicate the type of this node. */
int node_idx;
Idx node_idx;
};
typedef struct bin_tree_t bin_tree_t;
......@@ -491,7 +591,7 @@ typedef struct bin_tree_storage_t bin_tree_storage_t;
struct re_dfastate_t
{
unsigned int hash;
re_hashval_t hash;
re_node_set nodes;
re_node_set non_eps_nodes;
re_node_set inveclosure;
......@@ -511,8 +611,8 @@ typedef struct re_dfastate_t re_dfastate_t;
struct re_state_table_entry
{
int num;
int alloc;
Idx num;
Idx alloc;
re_dfastate_t **array;
};
......@@ -520,8 +620,8 @@ struct re_state_table_entry
typedef struct
{
int next_idx;
int alloc;
Idx next_idx;
Idx alloc;
re_dfastate_t **array;
} state_array_t;
......@@ -529,8 +629,8 @@ typedef struct
typedef struct
{
int node;
int str_idx; /* The position NODE match at. */
Idx node;
Idx str_idx; /* The position NODE match at. */
state_array_t path;
} re_sub_match_last_t;
......@@ -540,21 +640,20 @@ typedef struct
typedef struct
{
int str_idx;
int node;
int next_last_offset;
Idx str_idx;
Idx node;
state_array_t *path;
int alasts; /* Allocation size of LASTS. */
int nlasts; /* The number of LASTS. */
Idx alasts; /* Allocation size of LASTS. */
Idx nlasts; /* The number of LASTS. */
re_sub_match_last_t **lasts;
} re_sub_match_top_t;
struct re_backref_cache_entry
{
int node;
int str_idx;
int subexp_from;
int subexp_to;
Idx node;
Idx str_idx;
Idx subexp_from;
Idx subexp_to;
char more;
char unused;
unsigned short int eps_reachable_subexps_map;
......@@ -572,18 +671,18 @@ typedef struct
/* EFLAGS of the argument of regexec. */
int eflags;
/* Where the matching ends. */
int match_last;
int last_node;
Idx match_last;
Idx last_node;
/* The state log used by the matcher. */
re_dfastate_t **state_log;
int state_log_top;
Idx state_log_top;
/* Back reference cache. */
int nbkref_ents;
int abkref_ents;
Idx nbkref_ents;
Idx abkref_ents;
struct re_backref_cache_entry *bkref_ents;
int max_mb_elem_len;
int nsub_tops;
int asub_tops;
Idx nsub_tops;
Idx asub_tops;
re_sub_match_top_t **sub_tops;
} re_match_context_t;
......@@ -591,33 +690,33 @@ typedef struct
{
re_dfastate_t **sifted_states;
re_dfastate_t **limited_states;
int last_node;
int last_str_idx;
Idx last_node;
Idx last_str_idx;
re_node_set limits;
} re_sift_context_t;
struct re_fail_stack_ent_t
{
int idx;
int node;
Idx idx;
Idx node;
regmatch_t *regs;
re_node_set eps_via_nodes;
};
struct re_fail_stack_t
{
int num;
int alloc;
Idx num;
Idx alloc;
struct re_fail_stack_ent_t *stack;
};
struct re_dfa_t
{
re_token_t *nodes;
int nodes_alloc;
int nodes_len;
int *nexts;
int *org_indices;
Idx nodes_alloc;
Idx nodes_len;
Idx *nexts;
Idx *org_indices;
re_node_set *edests;
re_node_set *eclosures;
re_node_set *inveclosures;
......@@ -632,14 +731,13 @@ struct re_dfa_t
int str_tree_storage_idx;
/* number of subexpressions `re_nsub' is in regex_t. */
unsigned int state_hash_mask;
int states_alloc;
int init_node;
int nbackref; /* The number of backreference in this dfa. */
re_hashval_t state_hash_mask;
Idx init_node;
Idx nbackref; /* The number of backreference in this dfa. */
/* Bitmap expressing which backreference is used. */
unsigned int used_bkref_map;
unsigned int completed_bkref_map;
bitset_word used_bkref_map;
bitset_word completed_bkref_map;
unsigned int has_plural_match : 1;
/* If this dfa has "multibyte node", which is a backreference or
......@@ -652,51 +750,20 @@ struct re_dfa_t
int mb_cur_max;
bitset word_char;
reg_syntax_t syntax;
int *subexp_map;
Idx *subexp_map;
#ifdef DEBUG
char* re_str;
#endif
__libc_lock_define (, lock)
};
#ifndef RE_NO_INTERNAL_PROTOTYPES
static reg_errcode_t re_node_set_alloc (re_node_set *set, int size) internal_function;
static reg_errcode_t re_node_set_init_1 (re_node_set *set, int elem) internal_function;
static reg_errcode_t re_node_set_init_2 (re_node_set *set, int elem1,
int elem2) internal_function;
#define re_node_set_init_empty(set) memset (set, '\0', sizeof (re_node_set))
static reg_errcode_t re_node_set_init_copy (re_node_set *dest,
const re_node_set *src) internal_function;
static reg_errcode_t re_node_set_add_intersect (re_node_set *dest,
const re_node_set *src1,
const re_node_set *src2) internal_function;
static reg_errcode_t re_node_set_init_union (re_node_set *dest,
const re_node_set *src1,
const re_node_set *src2) internal_function;
static reg_errcode_t re_node_set_merge (re_node_set *dest,
const re_node_set *src) internal_function;
static int re_node_set_insert (re_node_set *set, int elem) internal_function;
static int re_node_set_insert_last (re_node_set *set,
int elem) internal_function;
static int re_node_set_compare (const re_node_set *set1,
const re_node_set *set2)
internal_function __attribute ((pure));
static int re_node_set_contains (const re_node_set *set, int elem)
internal_function __attribute ((pure));
static void re_node_set_remove_at (re_node_set *set, int idx) internal_function;
#define re_node_set_remove(set,id) \
(re_node_set_remove_at (set, re_node_set_contains (set, id) - 1))
#define re_node_set_empty(p) ((p)->nelem = 0)
#define re_node_set_free(set) re_free ((set)->elems)
static int re_dfa_add_node (re_dfa_t *dfa, re_token_t token) internal_function;
static re_dfastate_t *re_acquire_state (reg_errcode_t *err, re_dfa_t *dfa,
const re_node_set *nodes) internal_function;
static re_dfastate_t *re_acquire_state_context (reg_errcode_t *err,
re_dfa_t *dfa,
const re_node_set *nodes,
unsigned int context) internal_function;
static void free_state (re_dfastate_t *state) internal_function;
#endif
typedef enum
......@@ -721,43 +788,79 @@ typedef struct
/* Inline functions for bitset operation. */
static inline void
bitset_not (bitset set)
bitset_set (bitset set, Idx i)
{
int bitset_i;
for (bitset_i = 0; bitset_i < BITSET_UINTS; ++bitset_i)
set[bitset_i] = ~set[bitset_i];
set[i / BITSET_WORD_BITS] |= (bitset_word) 1 << i % BITSET_WORD_BITS;
}
static inline void
bitset_merge (bitset dest, const bitset src)
bitset_clear (bitset set, Idx i)
{
set[i / BITSET_WORD_BITS] &= ~ ((bitset_word) 1 << i % BITSET_WORD_BITS);
}
static inline bool
bitset_contain (const bitset set, Idx i)
{
return (set[i / BITSET_WORD_BITS] >> i % BITSET_WORD_BITS) & 1;
}
static inline void
bitset_empty (bitset set)
{
int bitset_i;
for (bitset_i = 0; bitset_i < BITSET_UINTS; ++bitset_i)
dest[bitset_i] |= src[bitset_i];
memset (set, 0, sizeof (bitset));
}
static inline void
bitset_not_merge (bitset dest, const bitset src)
bitset_set_all (bitset set)
{
memset (set, -1, sizeof (bitset_word) * (SBC_MAX / BITSET_WORD_BITS));
if (SBC_MAX % BITSET_WORD_BITS != 0)
set[BITSET_WORDS - 1] =
((bitset_word) 1 << SBC_MAX % BITSET_WORD_BITS) - 1;
}
static inline void
bitset_copy (bitset dest, const bitset src)
{
memcpy (dest, src, sizeof (bitset));
}
static inline void
bitset_not (bitset set)
{
int i;
for (i = 0; i < SBC_MAX / BITSET_WORD_BITS; ++i)
set[i] = ~set[i];
if (SBC_MAX % BITSET_WORD_BITS != 0)
set[BITSET_WORDS - 1] =
((((bitset_word) 1 << SBC_MAX % BITSET_WORD_BITS) - 1)
& ~set[BITSET_WORDS - 1]);
}
static inline void
bitset_merge (bitset dest, const bitset src)
{
int i;
for (i = 0; i < BITSET_UINTS; ++i)
dest[i] |= ~src[i];
for (i = 0; i < BITSET_WORDS; ++i)
dest[i] |= src[i];
}
static inline void
bitset_mask (bitset dest, const bitset src)
{
int bitset_i;
for (bitset_i = 0; bitset_i < BITSET_UINTS; ++bitset_i)
dest[bitset_i] &= src[bitset_i];
int i;
for (i = 0; i < BITSET_WORDS; ++i)
dest[i] &= src[i];
}
#if defined RE_ENABLE_I18N && !defined RE_NO_INTERNAL_PROTOTYPES
#if defined RE_ENABLE_I18N
/* Inline functions for re_string. */
static inline int
internal_function
re_string_char_size_at (const re_string_t *pstr, int idx)
internal_function __attribute ((pure))
re_string_char_size_at (const re_string_t *pstr, Idx idx)
{
int byte_idx;
if (pstr->mb_cur_max == 1)
......@@ -769,8 +872,8 @@ re_string_char_size_at (const re_string_t *pstr, int idx)
}
static inline wint_t
internal_function
re_string_wchar_at (const re_string_t *pstr, int idx)
internal_function __attribute ((pure))
re_string_wchar_at (const re_string_t *pstr, Idx idx)
{
if (pstr->mb_cur_max == 1)
return (wint_t) pstr->mbs[idx];
......@@ -778,8 +881,8 @@ re_string_wchar_at (const re_string_t *pstr, int idx)
}
static int
internal_function
re_string_elem_size_at (const re_string_t *pstr, int idx)
internal_function __attribute ((pure))
re_string_elem_size_at (const re_string_t *pstr, Idx idx)
{
#ifdef _LIBC
const unsigned char *p, *extra;
......
This diff could not be displayed because it is too large.
......@@ -25,119 +25,10 @@
#include "strcase.h"
#include <ctype.h>
#include <limits.h>
#if HAVE_MBRTOWC
#include "strnlen1.h"
/* Like mbiter.h, except it doesn't look at the entire string. */
#include "mbchar.h"
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
#include <wchar.h>
#include <wctype.h>
struct mbiter_multi
{
bool at_end; /* true if the end of the string has been reached */
bool in_shift; /* true if next byte may not be interpreted as ASCII */
mbstate_t state; /* if in_shift: current shift state */
bool next_done; /* true if mbi_avail has already filled the following */
struct mbchar cur; /* the current character:
const char *cur.ptr pointer to current character
The following are only valid after mbi_avail.
size_t cur.bytes number of bytes of current character
bool cur.wc_valid true if wc is a valid wide character
wchar_t cur.wc if wc_valid: the current character
*/
};
static inline void
mbiter_multi_next (struct mbiter_multi *iter)
{
if (iter->next_done)
return;
if (iter->in_shift)
goto with_shift;
/* Handle most ASCII characters quickly, without calling mbrtowc(). */
if (is_basic (*iter->cur.ptr))
{
/* These characters are part of the basic character set. ISO C 99
guarantees that their wide character code is identical to their
char code. */
iter->cur.bytes = 1;
iter->cur.wc = *iter->cur.ptr;
iter->cur.wc_valid = true;
}
else
{
assert (mbsinit (&iter->state));
iter->in_shift = true;
with_shift:
iter->cur.bytes = mbrtowc (&iter->cur.wc, iter->cur.ptr,
strnlen1 (iter->cur.ptr, MB_CUR_MAX),
&iter->state);
if (iter->cur.bytes == (size_t) -1)
{
/* An invalid multibyte sequence was encountered. */
iter->cur.bytes = 1;
iter->cur.wc_valid = false;
/* Whether to set iter->in_shift = false and reset iter->state
or not is not very important; the string is bogus anyway. */
}
else if (iter->cur.bytes == (size_t) -2)
{
/* An incomplete multibyte character at the end. */
iter->cur.bytes = strlen (iter->cur.ptr) + 1;
iter->cur.wc_valid = false;
/* Whether to set iter->in_shift = false and reset iter->state
or not is not important; the string end is reached anyway. */
}
else
{
if (iter->cur.bytes == 0)
{
/* A null wide character was encountered. */
iter->cur.bytes = 1;
assert (*iter->cur.ptr == '\0');
assert (iter->cur.wc == 0);
}
iter->cur.wc_valid = true;
/* When in the initial state, we can go back treating ASCII
characters more quickly. */
if (mbsinit (&iter->state))
iter->in_shift = false;
}
}
iter->next_done = true;
}
static inline void
mbiter_multi_reloc (struct mbiter_multi *iter, ptrdiff_t ptrdiff)
{
iter->cur.ptr += ptrdiff;
}
/* Iteration macros. */
typedef struct mbiter_multi mbi_iterator_t;
#define mbi_init(iter, startptr) \
((iter).cur.ptr = (startptr), (iter).at_end = false, \
(iter).in_shift = false, memset (&(iter).state, '\0', sizeof (mbstate_t)), \
(iter).next_done = false)
#define mbi_avail(iter) \
(!(iter).at_end && (mbiter_multi_next (&(iter)), true))
#define mbi_advance(iter) \
((mb_isnul ((iter).cur) ? ((iter).at_end = true) : 0), \
(iter).cur.ptr += (iter).cur.bytes, (iter).next_done = false)
/* Access to the current character. */
#define mbi_cur(iter) (iter).cur
#define mbi_cur_ptr(iter) (iter).cur.ptr
# include "mbuiter.h"
#endif
#define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
......@@ -159,59 +50,26 @@ strcasecmp (const char *s1, const char *s2)
#if HAVE_MBRTOWC
if (MB_CUR_MAX > 1)
{
mbi_iterator_t iter1;
mbi_iterator_t iter2;
mbui_iterator_t iter1;
mbui_iterator_t iter2;
mbi_init (iter1, s1);
mbi_init (iter2, s2);
mbui_init (iter1, s1);
mbui_init (iter2, s2);
while (mbi_avail (iter1) && mbi_avail (iter2))
while (mbui_avail (iter1) && mbui_avail (iter2))
{
/* Sort invalid characters after all valid ones. */
if (!mbi_cur (iter1).wc_valid)
{
if (!mbi_cur (iter2).wc_valid)
{
/* Compare two invalid characters. */
int cmp;
if (mbi_cur (iter1).bytes > mbi_cur (iter2).bytes)
return 1;
if (mbi_cur (iter1).bytes < mbi_cur (iter2).bytes)
return -1;
cmp = memcmp (mbi_cur_ptr (iter1), mbi_cur_ptr (iter2),
mbi_cur (iter1).bytes);
if (cmp != 0)
return cmp;
}
else
/* mbi_cur (iter1) invalid, mbi_cur (iter2) valid. */
return 1;
}
else
{
if (!mbi_cur (iter2).wc_valid)
/* mbi_cur (iter1) valid, mbi_cur (iter2) invalid. */
return -1;
else
{
/* Compare two valid characters. */
wchar_t c1 = towlower (mbi_cur (iter1).wc);
wchar_t c2 = towlower (mbi_cur (iter2).wc);
if (c1 > c2)
return 1;
if (c1 < c2)
return -1;
}
}
mbi_advance (iter1);
mbi_advance (iter2);
int cmp = mb_casecmp (mbui_cur (iter1), mbui_cur (iter2));
if (cmp != 0)
return cmp;
mbui_advance (iter1);
mbui_advance (iter2);
}
if (mbi_avail (iter1))
if (mbui_avail (iter1))
/* s2 terminated before s1. */
return 1;
if (mbi_avail (iter2))
if (mbui_avail (iter2))
/* s1 terminated before s2. */
return -1;
return 0;
......@@ -236,6 +94,12 @@ strcasecmp (const char *s1, const char *s2)
}
while (c1 == c2);
return c1 - c2;
if (UCHAR_MAX <= INT_MAX)
return c1 - c2;
else
/* On machines where 'char' and 'int' are types of the same size, the
difference of two 'unsigned char' values - including the sign bit -
doesn't fit in an 'int'. */
return (c1 > c2 ? 1 : c1 < c2 ? -1 : 0);
}
}
......
/* strncasecmp.c -- case insensitive string comparator
Copyright (C) 1998, 1999 Free Software Foundation, Inc.
Copyright (C) 1998, 1999, 2005 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -15,7 +15,7 @@
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
#if HAVE_CONFIG_H
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
......@@ -23,6 +23,7 @@
#include "strcase.h"
#include <ctype.h>
#include <limits.h>
#define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
......@@ -54,5 +55,11 @@ strncasecmp (const char *s1, const char *s2, size_t n)
}
while (c1 == c2);
return c1 - c2;
if (UCHAR_MAX <= INT_MAX)
return c1 - c2;
else
/* On machines where 'char' and 'int' are types of the same size, the
difference of two 'unsigned char' values - including the sign bit -
doesn't fit in an 'int'. */
return (c1 > c2 ? 1 : c1 < c2 ? -1 : 0);
}
......
......@@ -18,7 +18,7 @@
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
#ifdef HAVE_CONFIG_H
# include "config.h"
# include <config.h>
#endif
#include <stdlib.h>
......
......@@ -16,7 +16,7 @@
with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
#if HAVE_CONFIG_H
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#undef strnlen
......
......@@ -16,7 +16,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
USA. */
#if HAVE_CONFIG_H
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
......