Back to index

glibc  2.9
tst-pcre.c
Go to the documentation of this file.
00001 /* Regular expression tests.
00002    Copyright (C) 2003 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
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 <sys/types.h>
00022 #include <mcheck.h>
00023 #include <regex.h>
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 #include <string.h>
00027 
00028 int
00029 main (int argc, char **argv)
00030 {
00031   int ret = 0;
00032   char *line = NULL;
00033   size_t line_len = 0;
00034   ssize_t len;
00035   FILE *f;
00036   char *pattern = NULL, *string = NULL;
00037   regmatch_t rm[20];
00038   size_t pattern_alloced = 0, string_alloced = 0;
00039   int ignorecase = 0;
00040   int pattern_valid = 0, rm_valid = 0;
00041   size_t linenum;
00042 
00043   mtrace ();
00044 
00045   if (argc < 2)
00046     {
00047       fprintf (stderr, "Missing test filename\n");
00048       return 1;
00049     }
00050 
00051   f = fopen (argv[1], "r");
00052   if (f == NULL)
00053     {
00054       fprintf (stderr, "Couldn't open %s\n", argv[1]);
00055       return 1;
00056     }
00057 
00058   if ((len = getline (&line, &line_len, f)) <= 0
00059       || strncmp (line, "# PCRE", 6) != 0)
00060     {
00061       fprintf (stderr, "Not a PCRE test file\n");
00062       fclose (f);
00063       free (line);
00064       return 1;
00065     }
00066 
00067   linenum = 1;
00068 
00069   while ((len = getline (&line, &line_len, f)) > 0)
00070     {
00071       char *p;
00072       unsigned long num;
00073 
00074       ++linenum;
00075 
00076       if (line[len - 1] == '\n')
00077        line[--len] = '\0';
00078 
00079       if (line[0] == '#')
00080        continue;
00081 
00082       if (line[0] == '\0')
00083        {
00084          /* End of test.  */
00085          ignorecase = 0;
00086          pattern_valid = 0;
00087          rm_valid = 0;
00088          continue;
00089        }
00090 
00091       if (line[0] == '/')
00092        {
00093          /* Pattern.  */
00094          p = strrchr (line + 1, '/');
00095 
00096          pattern_valid = 0;
00097          rm_valid = 0;
00098          if (p == NULL)
00099            {
00100              printf ("%zd: Invalid pattern line: %s\n", linenum, line);
00101              ret = 1;
00102              continue;
00103            }
00104 
00105          if (p[1] == 'i' && p[2] == '\0')
00106            ignorecase = 1;
00107          else if (p[1] != '\0')
00108            {
00109              printf ("%zd: Invalid pattern line: %s\n", linenum, line);
00110              ret = 1;
00111              continue;
00112            }
00113 
00114          if (pattern_alloced < (size_t) (p - line))
00115            {
00116              pattern = realloc (pattern, p - line);
00117              if (pattern == NULL)
00118               {
00119                 printf ("%zd: Cannot record pattern: %m\n", linenum);
00120                 ret = 1;
00121                 break;
00122               }
00123              pattern_alloced = p - line;
00124            }
00125 
00126          memcpy (pattern, line + 1, p - line - 1);
00127          pattern[p - line - 1] = '\0';
00128          pattern_valid = 1;
00129          continue;
00130        }
00131 
00132       if (strncmp (line, "    ", 4) == 0)
00133        {
00134          regex_t re;
00135          int n;
00136     
00137          if (!pattern_valid)
00138            {
00139              printf ("%zd: No previous valid pattern %s\n", linenum, line);
00140              continue;
00141            }
00142 
00143          if (string_alloced < (size_t) (len - 3))
00144            {
00145              string = realloc (string, len - 3);
00146              if (string == NULL)
00147               {
00148                 printf ("%zd: Cannot record search string: %m\n", linenum);
00149                 ret = 1;
00150                 break;
00151               }
00152              string_alloced = len - 3;
00153            }
00154 
00155          memcpy (string, line + 4, len - 3);
00156 
00157          n = regcomp (&re, pattern,
00158                      REG_EXTENDED | (ignorecase ? REG_ICASE : 0));
00159          if (n != 0)
00160            {
00161              char buf[500];
00162              regerror (n, &re, buf, sizeof (buf));
00163              printf ("%zd: regcomp failed for %s: %s\n",
00164                     linenum, pattern, buf);
00165              ret = 1;
00166              continue;
00167            }
00168 
00169          if (regexec (&re, string, 20, rm, 0))
00170            {
00171              rm[0].rm_so = -1;
00172              rm[0].rm_eo = -1;
00173            }
00174 
00175          regfree (&re);
00176          rm_valid = 1;
00177          continue;
00178        }
00179 
00180       if (!rm_valid)
00181        {
00182          printf ("%zd: No preceeding pattern or search string\n", linenum);
00183          ret = 1;
00184          continue;
00185        }
00186 
00187       if (strcmp (line, "No match") == 0)
00188        {
00189          if (rm[0].rm_so != -1 || rm[0].rm_eo != -1)
00190            {
00191              printf ("%zd: /%s/ on %s unexpectedly matched %d..%d\n",
00192                     linenum, pattern, string, rm[0].rm_so, rm[0].rm_eo);
00193              ret = 1;
00194            }
00195 
00196          continue;
00197        }
00198 
00199       p = line;
00200       if (*p == ' ')
00201         ++p;
00202 
00203       num = strtoul (p, &p, 10);
00204       if (num >= 20 || *p != ':' || p[1] != ' ')
00205        {
00206          printf ("%zd: Invalid line %s\n", linenum, line);
00207          ret = 1;
00208          continue;
00209        }
00210 
00211       if (rm[num].rm_so == -1 || rm[num].rm_eo == -1)
00212        {
00213          if (strcmp (p + 2, "<unset>") != 0)
00214            {
00215              printf ("%zd: /%s/ on %s unexpectedly failed to match register %ld %d..%d\n",
00216                     linenum, pattern, string, num,
00217                     rm[num].rm_so, rm[num].rm_eo);
00218              ret = 1;
00219            }
00220          continue;
00221        }
00222 
00223       if (rm[num].rm_eo < rm[num].rm_so
00224          || rm[num].rm_eo - rm[num].rm_so != len - (p + 2 - line)
00225          || strncmp (p + 2, string + rm[num].rm_so,
00226                     rm[num].rm_eo - rm[num].rm_so) != 0)
00227        {
00228          printf ("%zd: /%s/ on %s unexpectedly failed to match %s for register %ld %d..%d\n",
00229                 linenum, pattern, string, p + 2, num,
00230                 rm[num].rm_so, rm[num].rm_eo);
00231          ret = 1;
00232          continue;
00233        }
00234     }
00235 
00236   free (pattern);
00237   free (string);
00238   free (line);
00239   fclose (f);
00240   return ret;
00241 }