genaliases.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* Copyright (C) 1999-2001, 2003, 2005, 2008, 2012 Free Software Foundation, Inc.
  2. This file is part of the GNU LIBICONV Library.
  3. The GNU LIBICONV Library is free software; you can redistribute it
  4. and/or modify it under the terms of the GNU Library General Public
  5. License as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. The GNU LIBICONV Library is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public
  12. License along with the GNU LIBICONV Library; see the file COPYING.LIB.
  13. If not, see <http://www.gnu.org/licenses/>. */
  14. /* Creates the aliases.gperf table. */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. static void emit_alias (FILE* out1, const char* alias, const char* c_name)
  18. {
  19. /* Output alias in upper case. */
  20. const char* s = alias;
  21. for (; *s; s++) {
  22. unsigned char c = * (unsigned char *) s;
  23. if (c >= 0x80)
  24. exit(1);
  25. if (c >= 'a' && c <= 'z')
  26. c -= 'a'-'A';
  27. putc(c, out1);
  28. }
  29. fprintf(out1,", ei_%s\n", c_name);
  30. }
  31. static void emit_encoding (FILE* out1, FILE* out2, const char* const* names, size_t n, const char* c_name)
  32. {
  33. fprintf(out2,"grep 'sizeof(\"");
  34. /* Output *names in upper case. */
  35. {
  36. const char* s = *names;
  37. for (; *s; s++) {
  38. unsigned char c = * (unsigned char *) s;
  39. if (c >= 0x80)
  40. exit(1);
  41. if (c >= 'a' && c <= 'z')
  42. c -= 'a'-'A';
  43. putc(c, out2);
  44. }
  45. }
  46. fprintf(out2,"\")' tmp.h | sed -e 's|^.*\\(stringpool_str[0-9]*\\).*$| (int)(long)\\&((struct stringpool_t *)0)->\\1,|'\n");
  47. for (; n > 0; names++, n--)
  48. emit_alias(out1, *names, c_name);
  49. }
  50. int main (int argc, char* argv[])
  51. {
  52. char* aliases_file_name;
  53. char* canonical_sh_file_name;
  54. char* canonical_local_sh_file_name;
  55. FILE* aliases_file;
  56. FILE* canonical_sh_file;
  57. if (argc != 4) {
  58. fprintf(stderr, "Usage: genaliases aliases.gperf canonical.sh canonical_local.sh\n");
  59. exit(1);
  60. }
  61. aliases_file_name = argv[1];
  62. canonical_sh_file_name = argv[2];
  63. canonical_local_sh_file_name = argv[3];
  64. aliases_file = fopen(aliases_file_name, "w");
  65. if (aliases_file == NULL) {
  66. fprintf(stderr, "Could not open '%s' for writing\n", aliases_file_name);
  67. exit(1);
  68. }
  69. fprintf(aliases_file, "struct alias { int name; unsigned int encoding_index; };\n");
  70. fprintf(aliases_file, "%%struct-type\n");
  71. fprintf(aliases_file, "%%language=ANSI-C\n");
  72. fprintf(aliases_file, "%%define hash-function-name aliases_hash\n");
  73. fprintf(aliases_file, "%%define lookup-function-name aliases_lookup\n");
  74. fprintf(aliases_file, "%%7bit\n");
  75. fprintf(aliases_file, "%%readonly-tables\n");
  76. fprintf(aliases_file, "%%global-table\n");
  77. fprintf(aliases_file, "%%define word-array-name aliases\n");
  78. fprintf(aliases_file, "%%pic\n");
  79. fprintf(aliases_file, "%%%%\n");
  80. #define DEFENCODING(xxx_names,xxx,xxx_ifuncs1,xxx_ifuncs2,xxx_ofuncs1,xxx_ofuncs2) \
  81. { \
  82. static const char* const names[] = BRACIFY xxx_names; \
  83. emit_encoding(aliases_file,canonical_sh_file,names,sizeof(names)/sizeof(names[0]),#xxx); \
  84. }
  85. #define BRACIFY(...) { __VA_ARGS__ }
  86. #define DEFALIAS(xxx_alias,xxx) emit_alias(aliases_file,xxx_alias,#xxx);
  87. canonical_sh_file = fopen(canonical_sh_file_name, "w");
  88. if (canonical_sh_file == NULL) {
  89. fprintf(stderr, "Could not open '%s' for writing\n", canonical_sh_file_name);
  90. exit(1);
  91. }
  92. #include "encodings.def"
  93. if (ferror(canonical_sh_file) || fclose(canonical_sh_file))
  94. exit(1);
  95. canonical_sh_file = fopen(canonical_local_sh_file_name, "w");
  96. if (canonical_sh_file == NULL) {
  97. fprintf(stderr, "Could not open '%s' for writing\n", canonical_local_sh_file_name);
  98. exit(1);
  99. }
  100. #include "encodings_local.def"
  101. if (ferror(canonical_sh_file) || fclose(canonical_sh_file))
  102. exit(1);
  103. #undef DEFALIAS
  104. #undef BRACIFY
  105. #undef DEFENCODING
  106. if (ferror(aliases_file) || fclose(aliases_file))
  107. exit(1);
  108. exit(0);
  109. }