gmo2msg.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * gmo2msg.c - create X/Open message source file for libelf.
  3. * Copyright (C) 1996 - 2005 Michael Riepe
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Library General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Library General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Library General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  18. */
  19. #ifndef lint
  20. static const char rcsid[] = "@(#) $Id: gmo2msg.c,v 1.11 2008/05/23 08:16:46 michael Exp $";
  21. #endif /* lint */
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #include <stdio.h>
  26. #include <libintl.h>
  27. #define DOMAIN "libelf"
  28. static const char *msgs[] = {
  29. #define __err__(a,b) b,
  30. #include <errors.h>
  31. #undef __err__
  32. };
  33. int
  34. main(int argc, char **argv) {
  35. char buf[1024], *lang, *progname, *s;
  36. unsigned i;
  37. FILE *fp;
  38. setlocale(LC_ALL, "");
  39. if (*argv && (progname = strrchr(argv[0], '/'))) {
  40. progname++;
  41. }
  42. else if (!(progname = *argv)) {
  43. progname = "gmo2msg";
  44. }
  45. if (argc <= 1 || !(lang = argv[1])) {
  46. fprintf(stderr, "Usage: gmo2msg <language>\n");
  47. exit(1);
  48. }
  49. /*
  50. * Fool gettext...
  51. */
  52. unlink(DOMAIN ".mo");
  53. unlink("LC_MESSAGES");
  54. unlink(lang);
  55. sprintf(buf, "%s.gmo", lang);
  56. if (link(buf, DOMAIN ".mo") == -1) {
  57. fprintf(stderr, "Cannot link %s to " DOMAIN ".mo\n", buf);
  58. perror("");
  59. exit(1);
  60. }
  61. symlink(".", "LC_MESSAGES");
  62. symlink(".", lang);
  63. textdomain(DOMAIN);
  64. getcwd(buf, sizeof(buf));
  65. bindtextdomain(DOMAIN, buf);
  66. sprintf(buf, "%s.msg", lang);
  67. unlink(buf);
  68. if (!(fp = fopen(buf, "w"))) {
  69. perror(buf);
  70. exit(1);
  71. }
  72. fprintf(fp, "$set 1 Automatically created from %s.gmo by %s\n", lang, progname);
  73. /*
  74. * Translate messages.
  75. */
  76. setlocale(LC_MESSAGES, lang);
  77. if ((s = gettext("")) && (s = strdup(s))) {
  78. s = strtok(s, "\n");
  79. while (s) {
  80. fprintf(fp, "$ %s\n", s);
  81. s = strtok(NULL, "\n");
  82. }
  83. }
  84. /*
  85. * Assume that messages contain printable ASCII characters ONLY.
  86. * That means no tabs, linefeeds etc.
  87. */
  88. for (i = 0; i < sizeof(msgs)/sizeof(*msgs); i++) {
  89. s = gettext(msgs[i]);
  90. if (s != msgs[i] && strcmp(s, msgs[i]) != 0) {
  91. fprintf(fp, "$ \n$ Original message: %s\n", msgs[i]);
  92. fprintf(fp, "%u %s\n", i + 1, s);
  93. }
  94. }
  95. setlocale(LC_MESSAGES, "");
  96. if (fclose(fp)) {
  97. perror("writing output file");
  98. exit(1);
  99. }
  100. /*
  101. * Cleanup.
  102. */
  103. unlink(DOMAIN ".mo");
  104. unlink("LC_MESSAGES");
  105. unlink(lang);
  106. exit(0);
  107. }