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 #ifdef HAVE_CONFIG_H
00047 #include "config.h"
00048 #endif
00049 
00050 #include <ctype.h>
00051 #include <stdio.h>
00052 #include <string.h>
00053 #include <locale.h>
00054 
00055 #include "pcre_internal.h"
00056 
00057 #define DFTABLES          /* pcre_maketables.c notices this */
00058 #include "pcre_maketables.c"
00059 
00060 
00061 int main(int argc, char **argv)
00062 {
00063 FILE *f;
00064 int i = 1;
00065 const unsigned char *tables;
00066 const unsigned char *base_of_tables;
00067 
00068 /* By default, the default C locale is used rather than what the building user
00069 happens to have set. However, if the -L option is given, set the locale from
00070 the LC_xxx environment variables. */
00071 
00072 if (argc > 1 && strcmp(argv[1], "-L") == 0)
00073   {
00074   setlocale(LC_ALL, "");        /* Set from environment variables */
00075   i++;
00076   }
00077 
00078 if (argc < i + 1)
00079   {
00080   fprintf(stderr, "dftables: one filename argument is required\n");
00081   return 1;
00082   }
00083 
00084 tables = pcre_maketables();
00085 base_of_tables = tables;
00086 
00087 f = fopen(argv[i], "wb");
00088 if (f == NULL)
00089   {
00090   fprintf(stderr, "dftables: failed to open %s for writing\n", argv[1]);
00091   return 1;
00092   }
00093 
00094 /* There are several fprintf() calls here, because gcc in pedantic mode
00095 complains about the very long string otherwise. */
00096 
00097 fprintf(f,
00098   "/*************************************************\n"
00099   "*      Perl-Compatible Regular Expressions       *\n"
00100   "*************************************************/\n\n"
00101   "/* This file was automatically written by the dftables auxiliary\n"
00102   "program. It contains character tables that are used when no external\n"
00103   "tables are passed to PCRE by the application that calls it. The tables\n"
00104   "are used only for characters whose code values are less than 256.\n\n");
00105 fprintf(f,
00106   "The following #includes are present because without them gcc 4.x may remove\n"
00107   "the array definition from the final binary if PCRE is built into a static\n"
00108   "library and dead code stripping is activated. This leads to link errors.\n"
00109   "Pulling in the header ensures that the array gets flagged as \"someone\n"
00110   "outside this compilation unit might reference this\" and so it will always\n"
00111   "be supplied to the linker. */\n\n"
00112   "#ifdef HAVE_CONFIG_H\n"
00113   "#include \"config.h\"\n"
00114   "#endif\n\n"
00115   "#include \"pcre_internal.h\"\n\n");
00116 fprintf(f,
00117   "const unsigned char _pcre_default_tables[] = {\n\n"
00118   "/* This table is a lower casing table. */\n\n");
00119 
00120 fprintf(f, "  ");
00121 for (i = 0; i < 256; i++)
00122   {
00123   if ((i & 7) == 0 && i != 0) fprintf(f, "\n  ");
00124   fprintf(f, "%3d", *tables++);
00125   if (i != 255) fprintf(f, ",");
00126   }
00127 fprintf(f, ",\n\n");
00128 
00129 fprintf(f, "/* This table is a case flipping table. */\n\n");
00130 
00131 fprintf(f, "  ");
00132 for (i = 0; i < 256; i++)
00133   {
00134   if ((i & 7) == 0 && i != 0) fprintf(f, "\n  ");
00135   fprintf(f, "%3d", *tables++);
00136   if (i != 255) fprintf(f, ",");
00137   }
00138 fprintf(f, ",\n\n");
00139 
00140 fprintf(f,
00141   "/* This table contains bit maps for various character classes.\n"
00142   "Each map is 32 bytes long and the bits run from the least\n"
00143   "significant end of each byte. The classes that have their own\n"
00144   "maps are: space, xdigit, digit, upper, lower, word, graph\n"
00145   "print, punct, and cntrl. Other classes are built from combinations. */\n\n");
00146 
00147 fprintf(f, "  ");
00148 for (i = 0; i < cbit_length; i++)
00149   {
00150   if ((i & 7) == 0 && i != 0)
00151     {
00152     if ((i & 31) == 0) fprintf(f, "\n");
00153     fprintf(f, "\n  ");
00154     }
00155   fprintf(f, "0x%02x", *tables++);
00156   if (i != cbit_length - 1) fprintf(f, ",");
00157   }
00158 fprintf(f, ",\n\n");
00159 
00160 fprintf(f,
00161   "/* This table identifies various classes of character by individual bits:\n"
00162   "  0x%02x   white space character\n"
00163   "  0x%02x   letter\n"
00164   "  0x%02x   decimal digit\n"
00165   "  0x%02x   hexadecimal digit\n"
00166   "  0x%02x   alphanumeric or '_'\n"
00167   "  0x%02x   regular expression metacharacter or binary zero\n*/\n\n",
00168   ctype_space, ctype_letter, ctype_digit, ctype_xdigit, ctype_word,
00169   ctype_meta);
00170 
00171 fprintf(f, "  ");
00172 for (i = 0; i < 256; i++)
00173   {
00174   if ((i & 7) == 0 && i != 0)
00175     {
00176     fprintf(f, " /* ");
00177     if (isprint(i-8)) fprintf(f, " %c -", i-8);
00178       else fprintf(f, "%3d-", i-8);
00179     if (isprint(i-1)) fprintf(f, " %c ", i-1);
00180       else fprintf(f, "%3d", i-1);
00181     fprintf(f, " */\n  ");
00182     }
00183   fprintf(f, "0x%02x", *tables++);
00184   if (i != 255) fprintf(f, ",");
00185   }
00186 
00187 fprintf(f, "};/* ");
00188 if (isprint(i-8)) fprintf(f, " %c -", i-8);
00189   else fprintf(f, "%3d-", i-8);
00190 if (isprint(i-1)) fprintf(f, " %c ", i-1);
00191   else fprintf(f, "%3d", i-1);
00192 fprintf(f, " */\n\n/* End of pcre_chartables.c */\n");
00193 
00194 fclose(f);
00195 free((void *)base_of_tables);
00196 return 0;
00197 }
00198 
00199 /* End of dftables.c */

Generated on Tue Jul 5 14:11:56 2011 for ROOT_528-00b_version by  doxygen 1.5.1