Home | History | Annotate | Download | only in src
      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 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 
     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 /* Force config.h in z/OS */
    108 
    109 #if defined NATIVE_ZOS
    110 fprintf(f,
    111   "/* For z/OS, config.h is forced */\n"
    112   "#ifndef HAVE_CONFIG_H\n"
    113   "#define HAVE_CONFIG_H 1\n"
    114   "#endif\n\n");
    115 #endif
    116 
    117 fprintf(f,
    118   "/* The following #includes are present because without them gcc 4.x may remove\n"
    119   "the array definition from the final binary if PCRE2 is built into a static\n"
    120   "library and dead code stripping is activated. This leads to link errors.\n"
    121   "Pulling in the header ensures that the array gets flagged as \"someone\n"
    122   "outside this compilation unit might reference this\" and so it will always\n"
    123   "be supplied to the linker. */\n\n");
    124 
    125 fprintf(f,
    126   "#ifdef HAVE_CONFIG_H\n"
    127   "#include \"config.h\"\n"
    128   "#endif\n\n"
    129   "#include \"pcre2_internal.h\"\n\n");
    130 
    131 fprintf(f,
    132   "const uint8_t PRIV(default_tables)[] = {\n\n"
    133   "/* This table is a lower casing table. */\n\n");
    134 
    135 fprintf(f, "  ");
    136 for (i = 0; i < 256; i++)
    137   {
    138   if ((i & 7) == 0 && i != 0) fprintf(f, "\n  ");
    139   fprintf(f, "%3d", *tables++);
    140   if (i != 255) fprintf(f, ",");
    141   }
    142 fprintf(f, ",\n\n");
    143 
    144 fprintf(f, "/* This table is a case flipping table. */\n\n");
    145 
    146 fprintf(f, "  ");
    147 for (i = 0; i < 256; i++)
    148   {
    149   if ((i & 7) == 0 && i != 0) fprintf(f, "\n  ");
    150   fprintf(f, "%3d", *tables++);
    151   if (i != 255) fprintf(f, ",");
    152   }
    153 fprintf(f, ",\n\n");
    154 
    155 fprintf(f,
    156   "/* This table contains bit maps for various character classes.\n"
    157   "Each map is 32 bytes long and the bits run from the least\n"
    158   "significant end of each byte. The classes that have their own\n"
    159   "maps are: space, xdigit, digit, upper, lower, word, graph\n"
    160   "print, punct, and cntrl. Other classes are built from combinations. */\n\n");
    161 
    162 fprintf(f, "  ");
    163 for (i = 0; i < cbit_length; i++)
    164   {
    165   if ((i & 7) == 0 && i != 0)
    166     {
    167     if ((i & 31) == 0) fprintf(f, "\n");
    168     fprintf(f, "\n  ");
    169     }
    170   fprintf(f, "0x%02x", *tables++);
    171   if (i != cbit_length - 1) fprintf(f, ",");
    172   }
    173 fprintf(f, ",\n\n");
    174 
    175 fprintf(f,
    176   "/* This table identifies various classes of character by individual bits:\n"
    177   "  0x%02x   white space character\n"
    178   "  0x%02x   letter\n"
    179   "  0x%02x   decimal digit\n"
    180   "  0x%02x   hexadecimal digit\n"
    181   "  0x%02x   alphanumeric or '_'\n"
    182   "  0x%02x   regular expression metacharacter or binary zero\n*/\n\n",
    183   ctype_space, ctype_letter, ctype_digit, ctype_xdigit, ctype_word,
    184   ctype_meta);
    185 
    186 fprintf(f, "  ");
    187 for (i = 0; i < 256; i++)
    188   {
    189   if ((i & 7) == 0 && i != 0)
    190     {
    191     fprintf(f, " /* ");
    192     if (isprint(i-8)) fprintf(f, " %c -", i-8);
    193       else fprintf(f, "%3d-", i-8);
    194     if (isprint(i-1)) fprintf(f, " %c ", i-1);
    195       else fprintf(f, "%3d", i-1);
    196     fprintf(f, " */\n  ");
    197     }
    198   fprintf(f, "0x%02x", *tables++);
    199   if (i != 255) fprintf(f, ",");
    200   }
    201 
    202 fprintf(f, "};/* ");
    203 if (isprint(i-8)) fprintf(f, " %c -", i-8);
    204   else fprintf(f, "%3d-", i-8);
    205 if (isprint(i-1)) fprintf(f, " %c ", i-1);
    206   else fprintf(f, "%3d", i-1);
    207 fprintf(f, " */\n\n/* End of pcre2_chartables.c */\n");
    208 
    209 fclose(f);
    210 free((void *)base_of_tables);
    211 return 0;
    212 }
    213 
    214 /* End of dftables.c */
    215