genaliases.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* Copyright (C) 1999-2001, 2003, 2005, 2008 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, write to the Free Software Foundation, Inc., 51 Franklin Street,
  14. Fifth Floor, Boston, MA 02110-1301, USA. */
  15. /* Creates the aliases.gperf table. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. static void emit_alias (FILE* out1, const char* alias, const char* c_name)
  19. {
  20. /* Output alias in upper case. */
  21. const char* s = alias;
  22. for (; *s; s++) {
  23. unsigned char c = * (unsigned char *) s;
  24. if (c >= 0x80)
  25. exit(1);
  26. if (c >= 'a' && c <= 'z')
  27. c -= 'a'-'A';
  28. putc(c, out1);
  29. }
  30. fprintf(out1,", ei_%s\n", c_name);
  31. }
  32. static void emit_encoding (FILE* out1, FILE* out2, const char* const* names, size_t n, const char* c_name)
  33. {
  34. fprintf(out2,"grep 'sizeof(\"");
  35. /* Output *names in upper case. */
  36. {
  37. const char* s = *names;
  38. for (; *s; s++) {
  39. unsigned char c = * (unsigned char *) s;
  40. if (c >= 0x80)
  41. exit(1);
  42. if (c >= 'a' && c <= 'z')
  43. c -= 'a'-'A';
  44. putc(c, out2);
  45. }
  46. }
  47. fprintf(out2,"\")' tmp.h | sed -e 's|^.*\\(stringpool_str[0-9]*\\).*$| (int)(long)\\&((struct stringpool_t *)0)->\\1,|'\n");
  48. for (; n > 0; names++, n--)
  49. emit_alias(out1, *names, c_name);
  50. }
  51. int main ()
  52. {
  53. FILE* stdout2;
  54. printf("struct alias { int name; unsigned int encoding_index; };\n");
  55. printf("%%struct-type\n");
  56. printf("%%language=ANSI-C\n");
  57. printf("%%define hash-function-name aliases_hash\n");
  58. printf("%%define lookup-function-name aliases_lookup\n");
  59. printf("%%7bit\n");
  60. printf("%%readonly-tables\n");
  61. printf("%%global-table\n");
  62. printf("%%define word-array-name aliases\n");
  63. printf("%%pic\n");
  64. printf("%%%%\n");
  65. #define DEFENCODING(xxx_names,xxx,xxx_ifuncs1,xxx_ifuncs2,xxx_ofuncs1,xxx_ofuncs2) \
  66. { \
  67. static const char* const names[] = BRACIFY xxx_names; \
  68. emit_encoding(stdout,stdout2,names,sizeof(names)/sizeof(names[0]),#xxx); \
  69. }
  70. #define BRACIFY(...) { __VA_ARGS__ }
  71. #define DEFALIAS(xxx_alias,xxx) emit_alias(stdout,xxx_alias,#xxx);
  72. stdout2 = fdopen(3, "w");
  73. if (stdout2 == NULL)
  74. exit(1);
  75. #include "encodings.def"
  76. if (fclose(stdout2))
  77. exit(1);
  78. stdout2 = fdopen(4, "w");
  79. if (stdout2 == NULL)
  80. exit(1);
  81. #include "encodings_local.def"
  82. if (fclose(stdout2))
  83. exit(1);
  84. #undef DEFALIAS
  85. #undef BRACIFY
  86. #undef DEFENCODING
  87. if (ferror(stdout) || fclose(stdout))
  88. exit(1);
  89. exit(0);
  90. }