dftables.c 6.8 KB

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