1 /*************************************************
2 *      Perl-Compatible Regular Expressions       *
3 *************************************************/
4 
5 /*
6 PCRE is a library of functions to support regular expressions whose syntax
7 and semantics are as close as possible to those of the Perl 5 language.
8 
9 Written by: Philip Hazel <ph10@cam.ac.uk>
10 
11            Copyright (c) 1997-2003 University of Cambridge
12 
13 -----------------------------------------------------------------------------
14 Permission is granted to anyone to use this software for any purpose on any
15 computer system, and to redistribute it freely, subject to the following
16 restrictions:
17 
18 1. This software is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
21 
22 2. The origin of this software must not be misrepresented, either by
23    explicit claim or by omission.
24 
25 3. Altered versions must be plainly marked as such, and must not be
26    misrepresented as being the original software.
27 
28 4. If PCRE is embedded in any software that is released under the GNU
29    General Purpose Licence (GPL), then the terms of that licence shall
30    supersede any condition above with which it is incompatible.
31 -----------------------------------------------------------------------------
32 
33 See the file Tech.Notes for some information on the internals.
34 */
35 
36 
37 /* This is a support program to generate the file chartables.c, containing
38 character tables of various kinds. They are built according to the default C
39 locale and used as the default tables by PCRE. Now that pcre_maketables is
40 a function visible to the outside world, we make use of its code from here in
41 order to be consistent. */
42 
43 #include <ctype.h>
44 #include <stdio.h>
45 #include <string.h>
46 
47 #include "internal.h"
48 
49 #define DFTABLES          /* maketables.c notices this */
50 #include "maketables.c"
51 
52 
main(int argc,char ** argv)53 int main(int argc, char **argv)
54 {
55 int i;
56 FILE *f;
57 const unsigned char *tables = pcre_maketables();
58 
59 if (argc != 2)
60   {
61   fprintf(stderr, "dftables: one filename argument is required\n");
62   return 1;
63   }
64 
65 f = fopen(argv[1], "w");
66 if (f == NULL)
67   {
68   fprintf(stderr, "dftables: failed to open %s for writing\n", argv[1]);
69   return 1;
70   }
71 
72 /* There are two fprintf() calls here, because gcc in pedantic mode complains
73 about the very long string otherwise. */
74 
75 fprintf(f,
76   "/*************************************************\n"
77   "*      Perl-Compatible Regular Expressions       *\n"
78   "*************************************************/\n\n"
79   "/* This file is automatically written by the dftables auxiliary \n"
80   "program. If you edit it by hand, you might like to edit the Makefile to \n"
81   "prevent its ever being regenerated.\n\n");
82 fprintf(f,
83   "This file is #included in the compilation of pcre.c to build the default\n"
84   "character tables which are used when no tables are passed to the compile\n"
85   "function. */\n\n"
86   "static unsigned char pcre_default_tables[] = {\n\n"
87   "/* This table is a lower casing table. */\n\n");
88 
89 fprintf(f, "  ");
90 for (i = 0; i < 256; i++)
91   {
92   if ((i & 7) == 0 && i != 0) fprintf(f, "\n  ");
93   fprintf(f, "%3d", *tables++);
94   if (i != 255) fprintf(f, ",");
95   }
96 fprintf(f, ",\n\n");
97 
98 fprintf(f, "/* This table is a case flipping table. */\n\n");
99 
100 fprintf(f, "  ");
101 for (i = 0; i < 256; i++)
102   {
103   if ((i & 7) == 0 && i != 0) fprintf(f, "\n  ");
104   fprintf(f, "%3d", *tables++);
105   if (i != 255) fprintf(f, ",");
106   }
107 fprintf(f, ",\n\n");
108 
109 fprintf(f,
110   "/* This table contains bit maps for various character classes.\n"
111   "Each map is 32 bytes long and the bits run from the least\n"
112   "significant end of each byte. The classes that have their own\n"
113   "maps are: space, xdigit, digit, upper, lower, word, graph\n"
114   "print, punct, and cntrl. Other classes are built from combinations. */\n\n");
115 
116 fprintf(f, "  ");
117 for (i = 0; i < cbit_length; i++)
118   {
119   if ((i & 7) == 0 && i != 0)
120     {
121     if ((i & 31) == 0) fprintf(f, "\n");
122     fprintf(f, "\n  ");
123     }
124   fprintf(f, "0x%02x", *tables++);
125   if (i != cbit_length - 1) fprintf(f, ",");
126   }
127 fprintf(f, ",\n\n");
128 
129 fprintf(f,
130   "/* This table identifies various classes of character by individual bits:\n"
131   "  0x%02x   white space character\n"
132   "  0x%02x   letter\n"
133   "  0x%02x   decimal digit\n"
134   "  0x%02x   hexadecimal digit\n"
135   "  0x%02x   alphanumeric or '_'\n"
136   "  0x%02x   regular expression metacharacter or binary zero\n*/\n\n",
137   ctype_space, ctype_letter, ctype_digit, ctype_xdigit, ctype_word,
138   ctype_meta);
139 
140 fprintf(f, "  ");
141 for (i = 0; i < 256; i++)
142   {
143   if ((i & 7) == 0 && i != 0)
144     {
145     fprintf(f, " /* ");
146     if (isprint(i-8)) fprintf(f, " %c -", i-8);
147       else fprintf(f, "%3d-", i-8);
148     if (isprint(i-1)) fprintf(f, " %c ", i-1);
149       else fprintf(f, "%3d", i-1);
150     fprintf(f, " */\n  ");
151     }
152   fprintf(f, "0x%02x", *tables++);
153   if (i != 255) fprintf(f, ",");
154   }
155 
156 fprintf(f, "};/* ");
157 if (isprint(i-8)) fprintf(f, " %c -", i-8);
158   else fprintf(f, "%3d-", i-8);
159 if (isprint(i-1)) fprintf(f, " %c ", i-1);
160   else fprintf(f, "%3d", i-1);
161 fprintf(f, " */\n\n/* End of chartables.c */\n");
162 
163 fclose(f);
164 return 0;
165 }
166 
167 /* End of dftables.c */
168