Back to index

glibc  2.9
tst-boost.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 void
00029 frob_escapes (char *src, int pattern)
00030 {
00031   char *dst;
00032 
00033   for (dst = src; *src != '\0'; dst++, src++)
00034     {
00035       if (*src == '\\')
00036        {
00037          switch (src[1])
00038            {
00039            case 't':
00040              src++;
00041              *dst = '\t';
00042              continue;
00043            case 'n':
00044              src++;
00045              *dst = '\n';
00046              continue;
00047            case 'r':
00048              src++;
00049              *dst = '\r';
00050              continue;
00051            case '\\':
00052            case '^':
00053            case '{':
00054            case '|':
00055            case '}':
00056              if (!pattern)
00057               {
00058                 src++;
00059                 *dst = *src;
00060                 continue;
00061               }
00062              break;
00063            }
00064        }
00065       if (src != dst)
00066        *dst = *src;
00067     }
00068   *dst = '\0';
00069 }
00070 
00071 int
00072 main (int argc, char **argv)
00073 {
00074   int ret = 0, n;
00075   char *line = NULL;
00076   size_t line_len = 0;
00077   ssize_t len;
00078   FILE *f;
00079   char *pattern, *string;
00080   int flags = REG_EXTENDED;
00081   int eflags = 0;
00082   regex_t re;
00083   regmatch_t rm[20];
00084 
00085   mtrace ();
00086 
00087   if (argc < 2)
00088     {
00089       fprintf (stderr, "Missing test filename\n");
00090       return 1;
00091     }
00092 
00093   f = fopen (argv[1], "r");
00094   if (f == NULL)
00095     {
00096       fprintf (stderr, "Couldn't open %s\n", argv[1]);
00097       return 1;
00098     }
00099 
00100   while ((len = getline (&line, &line_len, f)) > 0)
00101     {
00102       char *p, *q;
00103       int i;
00104 
00105       if (line[len - 1] == '\n')
00106        line[--len] = '\0';
00107 
00108       puts (line);
00109 
00110       if (line[0] == ';')
00111        continue;
00112 
00113       if (line[0] == '\0')
00114        continue;
00115 
00116       if (line[0] == '-')
00117        {
00118          if (strstr (line, "REG_BASIC"))
00119            flags = 0;
00120          else
00121            flags = REG_EXTENDED;
00122          if (strstr (line, "REG_ICASE"))
00123            flags |= REG_ICASE;
00124          if (strstr (line, "REG_NEWLINE"))
00125            flags |= REG_NEWLINE;
00126          eflags = 0;
00127          if (strstr (line, "REG_NOTBOL"))
00128            eflags |= REG_NOTBOL;
00129          if (strstr (line, "REG_NOTEOL"))
00130            eflags |= REG_NOTEOL;
00131          continue;
00132        }
00133 
00134       pattern = line + strspn (line, " \t");
00135       if (*pattern == '\0')
00136        continue;
00137       p = pattern + strcspn (pattern, " \t");
00138       if (*p == '\0')
00139        continue;
00140       *p++ = '\0';
00141 
00142       string = p + strspn (p, " \t");
00143       if (*string == '\0')
00144        continue;
00145       if (*string == '"')
00146        {
00147          string++;
00148          p = strchr (string, '"');
00149          if (p == NULL)
00150            continue;
00151          *p++ = '\0';
00152        }
00153       else
00154        {
00155          p = string + strcspn (string, " \t");
00156          if (*string == '!')
00157            string = NULL;
00158          else if (*p == '\0')
00159            continue;
00160          else
00161            *p++ = '\0';
00162        }
00163 
00164       frob_escapes (pattern, 1);
00165       if (string != NULL)
00166        frob_escapes (string, 0);
00167 
00168       n = regcomp (&re, pattern, flags);
00169       if (n != 0)
00170        {
00171          if (string != NULL)
00172            {
00173              char buf[500];
00174              regerror (n, &re, buf, sizeof (buf));
00175              printf ("FAIL regcomp unexpectedly failed: %s\n",
00176                     buf);
00177              ret = 1;
00178            }
00179          continue;
00180        }
00181       else if (string == NULL)
00182        {
00183          regfree (&re);
00184          puts ("FAIL regcomp unpexpectedly succeeded");
00185          ret = 1;
00186          continue;
00187        }
00188 
00189       if (regexec (&re, string, 20, rm, eflags))
00190        {
00191          for (i = 0; i < 20; ++i)
00192            {
00193              rm[i].rm_so = -1;
00194              rm[i].rm_eo = -1;
00195            }
00196        }
00197 
00198       regfree (&re);
00199 
00200       for (i = 0; i < 20 && *p != '\0'; ++i)
00201        {
00202          int rm_so, rm_eo;
00203 
00204          rm_so = strtol (p, &q, 10);
00205          if (p == q)
00206            break;
00207          p = q;
00208 
00209          rm_eo = strtol (p, &q, 10);
00210          if (p == q)
00211            break;
00212          p = q;
00213 
00214          if (rm[i].rm_so != rm_so || rm[i].rm_eo != rm_eo)
00215            {
00216              printf ("FAIL rm[%d] %d..%d != expected %d..%d\n",
00217                     i, rm[i].rm_so, rm[i].rm_eo, rm_so, rm_eo);
00218              ret = 1;
00219              break;
00220            }
00221        }
00222     }
00223 
00224   free (line);
00225   fclose (f);
00226   return ret;
00227 }