Back to index

php5  5.3.10
dftables.c
Go to the documentation of this file.
00001 /*************************************************
00002 *      Perl-Compatible Regular Expressions       *
00003 *************************************************/
00004 
00005 /* PCRE is a library of functions to support regular expressions whose syntax
00006 and semantics are as close as possible to those of the Perl 5 language.
00007 
00008                        Written by Philip Hazel
00009            Copyright (c) 1997-2008 University of Cambridge
00010 
00011 -----------------------------------------------------------------------------
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided that the following conditions are met:
00014 
00015     * Redistributions of source code must retain the above copyright notice,
00016       this list of conditions and the following disclaimer.
00017 
00018     * Redistributions in binary form must reproduce the above copyright
00019       notice, this list of conditions and the following disclaimer in the
00020       documentation and/or other materials provided with the distribution.
00021 
00022     * Neither the name of the University of Cambridge nor the names of its
00023       contributors may be used to endorse or promote products derived from
00024       this software without specific prior written permission.
00025 
00026 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00027 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00029 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00030 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00031 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00032 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00033 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00034 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00035 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00036 POSSIBILITY OF SUCH DAMAGE.
00037 -----------------------------------------------------------------------------
00038 */
00039 
00040 
00041 /* This is a freestanding support program to generate a file containing
00042 character tables for PCRE. The tables are built according to the current
00043 locale. Now that pcre_maketables is a function visible to the outside world, we
00044 make use of its code from here in order to be consistent. */
00045 
00046 #include "config.h"
00047 
00048 #include <ctype.h>
00049 #include <stdio.h>
00050 #include <string.h>
00051 #include <locale.h>
00052 
00053 #include "pcre_internal.h"
00054 
00055 #define DFTABLES          /* pcre_maketables.c notices this */
00056 #include "pcre_maketables.c"
00057 
00058 
00059 int main(int argc, char **argv)
00060 {
00061 FILE *f;
00062 int i = 1;
00063 const unsigned char *tables;
00064 const unsigned char *base_of_tables;
00065 
00066 /* By default, the default C locale is used rather than what the building user
00067 happens to have set. However, if the -L option is given, set the locale from
00068 the LC_xxx environment variables. */
00069 
00070 if (argc > 1 && strcmp(argv[1], "-L") == 0)
00071   {
00072   setlocale(LC_ALL, "");        /* Set from environment variables */
00073   i++;
00074   }
00075 
00076 if (argc < i + 1)
00077   {
00078   fprintf(stderr, "dftables: one filename argument is required\n");
00079   return 1;
00080   }
00081 
00082 tables = pcre_maketables();
00083 base_of_tables = tables;
00084 
00085 f = fopen(argv[i], "wb");
00086 if (f == NULL)
00087   {
00088   fprintf(stderr, "dftables: failed to open %s for writing\n", argv[1]);
00089   return 1;
00090   }
00091 
00092 /* There are several fprintf() calls here, because gcc in pedantic mode
00093 complains about the very long string otherwise. */
00094 
00095 fprintf(f,
00096   "/*************************************************\n"
00097   "*      Perl-Compatible Regular Expressions       *\n"
00098   "*************************************************/\n\n"
00099   "/* This file was automatically written by the dftables auxiliary\n"
00100   "program. It contains character tables that are used when no external\n"
00101   "tables are passed to PCRE by the application that calls it. The tables\n"
00102   "are used only for characters whose code values are less than 256.\n\n");
00103 fprintf(f,
00104   "The following #includes are present because without them gcc 4.x may remove\n"
00105   "the array definition from the final binary if PCRE is built into a static\n"
00106   "library and dead code stripping is activated. This leads to link errors.\n"
00107   "Pulling in the header ensures that the array gets flagged as \"someone\n"
00108   "outside this compilation unit might reference this\" and so it will always\n"
00109   "be supplied to the linker. */\n\n"
00110   "#ifdef HAVE_CONFIG_H\n"
00111   "#include \"config.h\"\n"
00112   "#endif\n\n"
00113   "#include \"pcre_internal.h\"\n\n");
00114 fprintf(f,
00115   "const unsigned char _pcre_default_tables[] = {\n\n"
00116   "/* This table is a lower casing table. */\n\n");
00117 
00118 fprintf(f, "  ");
00119 for (i = 0; i < 256; i++)
00120   {
00121   if ((i & 7) == 0 && i != 0) fprintf(f, "\n  ");
00122   fprintf(f, "%3d", *tables++);
00123   if (i != 255) fprintf(f, ",");
00124   }
00125 fprintf(f, ",\n\n");
00126 
00127 fprintf(f, "/* This table is a case flipping table. */\n\n");
00128 
00129 fprintf(f, "  ");
00130 for (i = 0; i < 256; i++)
00131   {
00132   if ((i & 7) == 0 && i != 0) fprintf(f, "\n  ");
00133   fprintf(f, "%3d", *tables++);
00134   if (i != 255) fprintf(f, ",");
00135   }
00136 fprintf(f, ",\n\n");
00137 
00138 fprintf(f,
00139   "/* This table contains bit maps for various character classes.\n"
00140   "Each map is 32 bytes long and the bits run from the least\n"
00141   "significant end of each byte. The classes that have their own\n"
00142   "maps are: space, xdigit, digit, upper, lower, word, graph\n"
00143   "print, punct, and cntrl. Other classes are built from combinations. */\n\n");
00144 
00145 fprintf(f, "  ");
00146 for (i = 0; i < cbit_length; i++)
00147   {
00148   if ((i & 7) == 0 && i != 0)
00149     {
00150     if ((i & 31) == 0) fprintf(f, "\n");
00151     fprintf(f, "\n  ");
00152     }
00153   fprintf(f, "0x%02x", *tables++);
00154   if (i != cbit_length - 1) fprintf(f, ",");
00155   }
00156 fprintf(f, ",\n\n");
00157 
00158 fprintf(f,
00159   "/* This table identifies various classes of character by individual bits:\n"
00160   "  0x%02x   white space character\n"
00161   "  0x%02x   letter\n"
00162   "  0x%02x   decimal digit\n"
00163   "  0x%02x   hexadecimal digit\n"
00164   "  0x%02x   alphanumeric or '_'\n"
00165   "  0x%02x   regular expression metacharacter or binary zero\n*/\n\n",
00166   ctype_space, ctype_letter, ctype_digit, ctype_xdigit, ctype_word,
00167   ctype_meta);
00168 
00169 fprintf(f, "  ");
00170 for (i = 0; i < 256; i++)
00171   {
00172   if ((i & 7) == 0 && i != 0)
00173     {
00174     fprintf(f, " /* ");
00175     if (isprint(i-8)) fprintf(f, " %c -", i-8);
00176       else fprintf(f, "%3d-", i-8);
00177     if (isprint(i-1)) fprintf(f, " %c ", i-1);
00178       else fprintf(f, "%3d", i-1);
00179     fprintf(f, " */\n  ");
00180     }
00181   fprintf(f, "0x%02x", *tables++);
00182   if (i != 255) fprintf(f, ",");
00183   }
00184 
00185 fprintf(f, "};/* ");
00186 if (isprint(i-8)) fprintf(f, " %c -", i-8);
00187   else fprintf(f, "%3d-", i-8);
00188 if (isprint(i-1)) fprintf(f, " %c ", i-1);
00189   else fprintf(f, "%3d", i-1);
00190 fprintf(f, " */\n\n/* End of pcre_chartables.c */\n");
00191 
00192 fclose(f);
00193 free((void *)base_of_tables);
00194 return 0;
00195 }
00196 
00197 /* End of dftables.c */