glibc/posix/tst-boost.c

226 lines
4.2 KiB
C
Raw Normal View History

/* Regular expression tests.
Copyright (C) 2003-2022 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
Prefer https to http for gnu.org and fsf.org URLs Also, change sources.redhat.com to sourceware.org. This patch was automatically generated by running the following shell script, which uses GNU sed, and which avoids modifying files imported from upstream: sed -ri ' s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g ' \ $(find $(git ls-files) -prune -type f \ ! -name '*.po' \ ! -name 'ChangeLog*' \ ! -path COPYING ! -path COPYING.LIB \ ! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \ ! -path manual/texinfo.tex ! -path scripts/config.guess \ ! -path scripts/config.sub ! -path scripts/install-sh \ ! -path scripts/mkinstalldirs ! -path scripts/move-if-change \ ! -path INSTALL ! -path locale/programs/charmap-kw.h \ ! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \ ! '(' -name configure \ -execdir test -f configure.ac -o -f configure.in ';' ')' \ ! '(' -name preconfigure \ -execdir test -f preconfigure.ac ';' ')' \ -print) and then by running 'make dist-prepare' to regenerate files built from the altered files, and then executing the following to cleanup: chmod a+x sysdeps/unix/sysv/linux/riscv/configure # Omit irrelevant whitespace and comment-only changes, # perhaps from a slightly-different Autoconf version. git checkout -f \ sysdeps/csky/configure \ sysdeps/hppa/configure \ sysdeps/riscv/configure \ sysdeps/unix/sysv/linux/csky/configure # Omit changes that caused a pre-commit check to fail like this: # remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines git checkout -f \ sysdeps/powerpc/powerpc64/ppc-mcount.S \ sysdeps/unix/sysv/linux/s390/s390-64/syscall.S # Omit change that caused a pre-commit check to fail like this: # remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 05:40:42 +00:00
<https://www.gnu.org/licenses/>. */
#include <sys/types.h>
#include <mcheck.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void
frob_escapes (char *src, int pattern)
{
char *dst;
for (dst = src; *src != '\0'; dst++, src++)
{
if (*src == '\\')
{
switch (src[1])
{
case 't':
src++;
*dst = '\t';
continue;
case 'n':
src++;
*dst = '\n';
continue;
case 'r':
src++;
*dst = '\r';
continue;
case '\\':
case '^':
case '{':
case '|':
case '}':
if (!pattern)
{
src++;
*dst = *src;
continue;
}
break;
}
}
if (src != dst)
*dst = *src;
}
*dst = '\0';
}
int
main (int argc, char **argv)
{
int ret = 0, n;
char *line = NULL;
size_t line_len = 0;
ssize_t len;
FILE *f;
char *pattern, *string;
int flags = REG_EXTENDED;
int eflags = 0;
regex_t re;
regmatch_t rm[20];
mtrace ();
if (argc < 2)
{
fprintf (stderr, "Missing test filename\n");
return 1;
}
f = fopen (argv[1], "r");
if (f == NULL)
{
fprintf (stderr, "Couldn't open %s\n", argv[1]);
return 1;
}
while ((len = getline (&line, &line_len, f)) > 0)
{
char *p, *q;
int i;
if (line[len - 1] == '\n')
line[--len] = '\0';
puts (line);
if (line[0] == ';')
continue;
if (line[0] == '\0')
continue;
if (line[0] == '-')
{
if (strstr (line, "REG_BASIC"))
flags = 0;
else
flags = REG_EXTENDED;
if (strstr (line, "REG_ICASE"))
flags |= REG_ICASE;
if (strstr (line, "REG_NEWLINE"))
flags |= REG_NEWLINE;
eflags = 0;
if (strstr (line, "REG_NOTBOL"))
eflags |= REG_NOTBOL;
if (strstr (line, "REG_NOTEOL"))
eflags |= REG_NOTEOL;
continue;
}
pattern = line + strspn (line, " \t");
if (*pattern == '\0')
continue;
p = pattern + strcspn (pattern, " \t");
if (*p == '\0')
continue;
*p++ = '\0';
string = p + strspn (p, " \t");
if (*string == '\0')
continue;
if (*string == '"')
{
string++;
p = strchr (string, '"');
if (p == NULL)
continue;
*p++ = '\0';
}
else
{
p = string + strcspn (string, " \t");
if (*string == '!')
string = NULL;
else if (*p == '\0')
continue;
else
*p++ = '\0';
}
frob_escapes (pattern, 1);
if (string != NULL)
frob_escapes (string, 0);
n = regcomp (&re, pattern, flags);
if (n != 0)
{
if (string != NULL)
{
char buf[500];
regerror (n, &re, buf, sizeof (buf));
printf ("FAIL regcomp unexpectedly failed: %s\n",
buf);
ret = 1;
}
continue;
}
else if (string == NULL)
{
regfree (&re);
puts ("FAIL regcomp unpexpectedly succeeded");
ret = 1;
continue;
}
if (regexec (&re, string, 20, rm, eflags))
{
for (i = 0; i < 20; ++i)
{
rm[i].rm_so = -1;
rm[i].rm_eo = -1;
}
}
regfree (&re);
for (i = 0; i < 20 && *p != '\0'; ++i)
{
int rm_so, rm_eo;
rm_so = strtol (p, &q, 10);
if (p == q)
break;
p = q;
rm_eo = strtol (p, &q, 10);
if (p == q)
break;
p = q;
if (rm[i].rm_so != rm_so || rm[i].rm_eo != rm_eo)
{
printf ("FAIL rm[%d] %d..%d != expected %d..%d\n",
i, rm[i].rm_so, rm[i].rm_eo, rm_so, rm_eo);
ret = 1;
break;
}
}
}
free (line);
fclose (f);
return ret;
}