1 /*************************************************
2 * Perl-Compatible Regular Expressions *
3 *************************************************/
4
5 /* PCRE is a library of functions to support regular expressions whose syntax
6 and semantics are as close as possible to those of the Perl 5 language.
7
8 Written by Philip Hazel
9 Original API code Copyright (c) 1997-2012 University of Cambridge
10 New API code Copyright (c) 2016-2018 University of Cambridge
11
12 -----------------------------------------------------------------------------
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions are met:
15
16 * Redistributions of source code must retain the above copyright notice,
17 this list of conditions and the following disclaimer.
18
19 * Redistributions in binary form must reproduce the above copyright
20 notice, this list of conditions and the following disclaimer in the
21 documentation and/or other materials provided with the distribution.
22
23 * Neither the name of the University of Cambridge nor the names of its
24 contributors may be used to endorse or promote products derived from
25 this software without specific prior written permission.
26
27 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 POSSIBILITY OF SUCH DAMAGE.
38 -----------------------------------------------------------------------------
39 */
40
41
42 /* This is a freestanding support program to generate a file containing
43 character tables for PCRE2. The tables are built according to the current
44 locale using the pcre2_maketables() function, which is part of the PCRE2 API.
45 */
46
47 #ifdef HAVE_CONFIG_H
48 #include "config.h"
49 #endif
50
51 #include <ctype.h>
52 #include <stdio.h>
53 #include <string.h>
54 #include <locale.h>
55
56 #define PCRE2_CODE_UNIT_WIDTH 0 /* Must be set, but not relevant here */
57 #include "pcre2_internal.h"
58
59 #define DFTABLES /* pcre2_maketables.c notices this */
60 #include "pcre2_maketables.c"
61
main(int argc,char ** argv)62 int main(int argc, char **argv)
63 {
64 FILE *f;
65 int i = 1;
66 const unsigned char *tables;
67 const unsigned char *base_of_tables;
68
69 /* By default, the default C locale is used rather than what the building user
70 happens to have set. However, if the -L option is given, set the locale from
71 the LC_xxx environment variables. */
72
73 if (argc > 1 && strcmp(argv[1], "-L") == 0)
74 {
75 setlocale(LC_ALL, ""); /* Set from environment variables */
76 i++;
77 }
78
79 if (argc < i + 1)
80 {
81 fprintf(stderr, "dftables: one filename argument is required\n");
82 return 1;
83 }
84
85 tables = maketables();
86 base_of_tables = tables;
87
88 f = fopen(argv[i], "wb");
89 if (f == NULL)
90 {
91 fprintf(stderr, "dftables: failed to open %s for writing\n", argv[1]);
92 return 1;
93 }
94
95 /* There are several fprintf() calls here, because gcc in pedantic mode
96 complains about the very long string otherwise. */
97
98 fprintf(f,
99 "/*************************************************\n"
100 "* Perl-Compatible Regular Expressions *\n"
101 "*************************************************/\n\n"
102 "/* This file was automatically written by the dftables auxiliary\n"
103 "program. It contains character tables that are used when no external\n"
104 "tables are passed to PCRE2 by the application that calls it. The tables\n"
105 "are used only for characters whose code values are less than 256. */\n\n");
106
107 fprintf(f,
108 "/*The dftables program (which is distributed with PCRE2) can be used to\n"
109 "build alternative versions of this file. This is necessary if you are\n"
110 "running in an EBCDIC environment, or if you want to default to a different\n"
111 "encoding, for example ISO-8859-1. When dftables is run, it creates these\n"
112 "tables in the current locale. This happens automatically if PCRE2 is\n"
113 "configured with --enable-rebuild-chartables. */\n\n");
114
115 /* Force config.h in z/OS */
116
117 #if defined NATIVE_ZOS
118 fprintf(f,
119 "/* For z/OS, config.h is forced */\n"
120 "#ifndef HAVE_CONFIG_H\n"
121 "#define HAVE_CONFIG_H 1\n"
122 "#endif\n\n");
123 #endif
124
125 fprintf(f,
126 "/* The following #include is present because without it gcc 4.x may remove\n"
127 "the array definition from the final binary if PCRE2 is built into a static\n"
128 "library and dead code stripping is activated. This leads to link errors.\n"
129 "Pulling in the header ensures that the array gets flagged as \"someone\n"
130 "outside this compilation unit might reference this\" and so it will always\n"
131 "be supplied to the linker. */\n\n");
132
133 fprintf(f,
134 "#ifdef HAVE_CONFIG_H\n"
135 "#include \"config.h\"\n"
136 "#endif\n\n"
137 "#include \"pcre2_internal.h\"\n\n");
138
139 fprintf(f,
140 "const uint8_t PRIV(default_tables)[] = {\n\n"
141 "/* This table is a lower casing table. */\n\n");
142
143 fprintf(f, " ");
144 for (i = 0; i < 256; i++)
145 {
146 if ((i & 7) == 0 && i != 0) fprintf(f, "\n ");
147 fprintf(f, "%3d", *tables++);
148 if (i != 255) fprintf(f, ",");
149 }
150 fprintf(f, ",\n\n");
151
152 fprintf(f, "/* This table is a case flipping table. */\n\n");
153
154 fprintf(f, " ");
155 for (i = 0; i < 256; i++)
156 {
157 if ((i & 7) == 0 && i != 0) fprintf(f, "\n ");
158 fprintf(f, "%3d", *tables++);
159 if (i != 255) fprintf(f, ",");
160 }
161 fprintf(f, ",\n\n");
162
163 fprintf(f,
164 "/* This table contains bit maps for various character classes. Each map is 32\n"
165 "bytes long and the bits run from the least significant end of each byte. The\n"
166 "classes that have their own maps are: space, xdigit, digit, upper, lower, word,\n"
167 "graph print, punct, and cntrl. Other classes are built from combinations. */\n\n");
168
169 fprintf(f, " ");
170 for (i = 0; i < cbit_length; i++)
171 {
172 if ((i & 7) == 0 && i != 0)
173 {
174 if ((i & 31) == 0) fprintf(f, "\n");
175 fprintf(f, "\n ");
176 }
177 fprintf(f, "0x%02x", *tables++);
178 if (i != cbit_length - 1) fprintf(f, ",");
179 }
180 fprintf(f, ",\n\n");
181
182 fprintf(f,
183 "/* This table identifies various classes of character by individual bits:\n"
184 " 0x%02x white space character\n"
185 " 0x%02x letter\n"
186 " 0x%02x decimal digit\n"
187 " 0x%02x hexadecimal digit\n"
188 " 0x%02x alphanumeric or '_'\n*/\n\n",
189 ctype_space, ctype_letter, ctype_digit, ctype_xdigit, ctype_word);
190
191 fprintf(f, " ");
192 for (i = 0; i < 256; i++)
193 {
194 if ((i & 7) == 0 && i != 0)
195 {
196 fprintf(f, " /* ");
197 if (isprint(i-8)) fprintf(f, " %c -", i-8);
198 else fprintf(f, "%3d-", i-8);
199 if (isprint(i-1)) fprintf(f, " %c ", i-1);
200 else fprintf(f, "%3d", i-1);
201 fprintf(f, " */\n ");
202 }
203 fprintf(f, "0x%02x", *tables++);
204 if (i != 255) fprintf(f, ",");
205 }
206
207 fprintf(f, "};/* ");
208 if (isprint(i-8)) fprintf(f, " %c -", i-8);
209 else fprintf(f, "%3d-", i-8);
210 if (isprint(i-1)) fprintf(f, " %c ", i-1);
211 else fprintf(f, "%3d", i-1);
212 fprintf(f, " */\n\n/* End of pcre2_chartables.c */\n");
213
214 fclose(f);
215 free((void *)base_of_tables);
216 return 0;
217 }
218
219 /* End of dftables.c */
220