Back to index

glibc  2.9
transbug.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <ctype.h>
00003 #include <errno.h>
00004 #include <locale.h>
00005 #include <regex.h>
00006 #include <stdlib.h>
00007 #include <unistd.h>
00008 #include <string.h>
00009 
00010 /* lowercase chars mapped to uppercase */
00011 static const char casetable[] = {
00012   '\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
00013   '\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
00014   '\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
00015   '\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037',
00016   /* ' '     '!'     '"'     '#'     '$'     '%'     '&'     ''' */
00017   '\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047',
00018   /* '('     ')'     '*'     '+'     ','     '-'     '.'     '/' */
00019   '\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057',
00020   /* '0'     '1'     '2'     '3'     '4'     '5'     '6'     '7' */
00021   '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
00022   /* '8'     '9'     ':'     ';'     '<'     '='     '>'     '?' */
00023   '\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077',
00024   /* '@'     'A'     'B'     'C'     'D'     'E'     'F'     'G' */
00025   '\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
00026   /* 'H'     'I'     'J'     'K'     'L'     'M'     'N'     'O' */
00027   '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
00028   /* 'P'     'Q'     'R'     'S'     'T'     'U'     'V'     'W' */
00029   '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
00030   /* 'X'     'Y'     'Z'     '['     '\'     ']'     '^'     '_' */
00031   '\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137',
00032   /* '`'     'a'     'b'     'c'     'd'     'e'     'f'     'g' */
00033   '\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
00034   /* 'h'     'i'     'j'     'k'     'l'     'm'     'n'     'o' */
00035   '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
00036   /* 'p'     'q'     'r'     's'     't'     'u'     'v'     'w' */
00037   '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
00038   /* 'x'     'y'     'z'     '{'     '|'     '}'     '~' */
00039   '\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177',
00040 
00041   /* Latin 1: */
00042   '\200', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
00043   '\210', '\211', '\212', '\213', '\214', '\215', '\216', '\217',
00044   '\220', '\221', '\222', '\223', '\224', '\225', '\226', '\227',
00045   '\230', '\231', '\232', '\233', '\234', '\235', '\236', '\237',
00046   '\240', '\241', '\242', '\243', '\244', '\245', '\246', '\247',
00047   '\250', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
00048   '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
00049   '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
00050   '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
00051   '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
00052   '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\327',
00053   '\370', '\371', '\372', '\373', '\374', '\375', '\376', '\337',
00054   '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
00055   '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
00056   '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
00057   '\370', '\371', '\372', '\373', '\374', '\375', '\376', '\377',
00058 };
00059 
00060 
00061 static int
00062 run_test (const char *pattern, struct re_registers *regs)
00063 {
00064   static char text[] = "1111AAAA2222bbbb";
00065 
00066   struct re_pattern_buffer pat;
00067 
00068   const char *err;
00069   int res;
00070   int start2;
00071 
00072   memset (&pat, '\0', sizeof (pat));
00073   memset (regs, '\0', 2 * sizeof (regs[0]));
00074   pat.allocated = 0;        /* regex will allocate the buffer */
00075   pat.fastmap = (char *) malloc (256);
00076   if (pat.fastmap == NULL)
00077     {
00078       puts ("out of memory");
00079       exit (1);
00080     }
00081 
00082   pat.translate = (unsigned char *) casetable;
00083 
00084   err = re_compile_pattern (pattern, strlen (pattern), &pat);
00085   if (err != NULL)
00086     {
00087       fprintf (stderr, "/%s/: %s\n", pattern, err);
00088       exit (1);
00089     }
00090   res = re_search (&pat, text, strlen (text), 0, strlen (text), &regs[0]);
00091   if (res < 0)
00092     printf ("search 1: res = %d\n", res);
00093   else
00094     printf ("search 1: res = %d, start = %d, end = %d\n",
00095            res, regs[0].start[0], regs[0].end[0]);
00096 
00097   if (regs[0].end == NULL)
00098     start2 = 8;
00099   else
00100     start2 = regs[0].end[0] + 1;
00101   regs[1] = regs[0];
00102   res = re_search (&pat, text, strlen (text), start2, strlen (text), &regs[1]);
00103   if (res < 0)
00104     printf ("search 2: res = %d\n", res);
00105   else
00106     printf ("search 2: res = %d, start = %d, end = %d\n",
00107            res, regs[1].start[0], regs[1].end[0]);
00108 
00109   return res < 0 ? 1 : 0;
00110 }
00111 
00112 
00113 static int
00114 do_test (void)
00115 {
00116   static const char lower[] = "[[:lower:]]+";
00117   static const char upper[] = "[[:upper:]]+";
00118   struct re_registers regs[4];
00119 
00120   setlocale (LC_ALL, "C");
00121 
00122   (void) re_set_syntax (RE_SYNTAX_GNU_AWK);
00123 
00124   int result;
00125 #define CHECK(exp) \
00126   if (exp) { puts (#exp); result = 1; }
00127 
00128   result = run_test (lower, regs);
00129   result |= run_test (upper, &regs[2]);
00130   if (! result)
00131     {
00132       CHECK (regs[0].start[0] != regs[2].start[0]);
00133       CHECK (regs[0].end[0] != regs[2].end[0]);
00134       CHECK (regs[1].start[0] != regs[3].start[0]);
00135       CHECK (regs[1].end[0] != regs[3].end[0]);
00136     }
00137 
00138   return result;
00139 }
00140 
00141 #define TEST_FUNCTION do_test ()
00142 #include "../test-skeleton.c"