Back to index

glibc  2.9
bug-regex28.c
Go to the documentation of this file.
00001 /* Test RE_HAT_LISTS_NOT_NEWLINE and RE_DOT_NEWLINE.
00002    Copyright (C) 2007 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Jakub Jelinek <jakub@redhat.com>, 2007.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <regex.h>
00022 #include <stdio.h>
00023 #include <string.h>
00024 
00025 struct tests
00026 {
00027   const char *regex;
00028   const char *string;
00029   reg_syntax_t syntax;
00030   int retval;
00031 } tests[] = {
00032 #define EGREP RE_SYNTAX_EGREP
00033 #define EGREP_NL (RE_SYNTAX_EGREP | RE_DOT_NEWLINE) & ~RE_HAT_LISTS_NOT_NEWLINE
00034   { "a.b", "a\nb", EGREP, -1 },
00035   { "a.b", "a\nb", EGREP_NL, 0 },
00036   { "a[^x]b", "a\nb", EGREP, -1 },
00037   { "a[^x]b", "a\nb", EGREP_NL, 0 },
00038   /* While \S and \W are internally handled as [^[:space:]] and [^[:alnum:]_],
00039      RE_HAT_LISTS_NOT_NEWLINE did not make any difference, so ensure
00040      it doesn't change.  */
00041   { "a\\Sb", "a\nb", EGREP, -1 },
00042   { "a\\Sb", "a\nb", EGREP_NL, -1 },
00043   { "a\\Wb", "a\nb", EGREP, 0 },
00044   { "a\\Wb", "a\nb", EGREP_NL, 0 }
00045 };
00046 
00047 int
00048 main (void)
00049 {
00050   struct re_pattern_buffer r;
00051   size_t i;
00052   int ret = 0;
00053 
00054   for (i = 0; i < sizeof (tests) / sizeof (tests[i]); ++i)
00055     {
00056       re_set_syntax (tests[i].syntax);
00057       memset (&r, 0, sizeof (r));
00058       if (re_compile_pattern (tests[i].regex, strlen (tests[i].regex), &r))
00059        {
00060          printf ("re_compile_pattern %zd failed\n", i);
00061          ret = 1;
00062          continue;
00063        }
00064       size_t len = strlen (tests[i].string);
00065       int rv = re_search (&r, tests[i].string, len, 0, len, NULL);
00066       if (rv != tests[i].retval)
00067        {
00068          printf ("re_search %zd unexpected value %d != %d\n",
00069                 i, rv, tests[i].retval);
00070          ret = 1;
00071        }
00072       regfree (&r);
00073     }
00074   return ret;
00075 }