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