kxgettext.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Arnaldo Carvalho de Melo <acme@conectiva.com.br>, 2005
  3. *
  4. * Released under the terms of the GNU GPL v2.0
  5. */
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include "lkc.h"
  9. static char *escape(const char* text, char *bf, int len)
  10. {
  11. char *bfp = bf;
  12. int multiline = strchr(text, '\n') != NULL;
  13. int eol = 0;
  14. int textlen = strlen(text);
  15. if ((textlen > 0) && (text[textlen-1] == '\n'))
  16. eol = 1;
  17. *bfp++ = '"';
  18. --len;
  19. if (multiline) {
  20. *bfp++ = '"';
  21. *bfp++ = '\n';
  22. *bfp++ = '"';
  23. len -= 3;
  24. }
  25. while (*text != '\0' && len > 1) {
  26. if (*text == '"')
  27. *bfp++ = '\\';
  28. else if (*text == '\n') {
  29. *bfp++ = '\\';
  30. *bfp++ = 'n';
  31. *bfp++ = '"';
  32. *bfp++ = '\n';
  33. *bfp++ = '"';
  34. len -= 5;
  35. ++text;
  36. goto next;
  37. }
  38. else if (*text == '\\') {
  39. *bfp++ = '\\';
  40. len--;
  41. }
  42. *bfp++ = *text++;
  43. next:
  44. --len;
  45. }
  46. if (multiline && eol)
  47. bfp -= 3;
  48. *bfp++ = '"';
  49. *bfp = '\0';
  50. return bf;
  51. }
  52. struct file_line {
  53. struct file_line *next;
  54. const char *file;
  55. int lineno;
  56. };
  57. static struct file_line *file_line__new(const char *file, int lineno)
  58. {
  59. struct file_line *self = malloc(sizeof(*self));
  60. if (self == NULL)
  61. goto out;
  62. self->file = file;
  63. self->lineno = lineno;
  64. self->next = NULL;
  65. out:
  66. return self;
  67. }
  68. struct message {
  69. const char *msg;
  70. const char *option;
  71. struct message *next;
  72. struct file_line *files;
  73. };
  74. static struct message *message__list;
  75. static struct message *message__new(const char *msg, char *option,
  76. const char *file, int lineno)
  77. {
  78. struct message *self = malloc(sizeof(*self));
  79. if (self == NULL)
  80. goto out;
  81. self->files = file_line__new(file, lineno);
  82. if (self->files == NULL)
  83. goto out_fail;
  84. self->msg = strdup(msg);
  85. if (self->msg == NULL)
  86. goto out_fail_msg;
  87. self->option = option;
  88. self->next = NULL;
  89. out:
  90. return self;
  91. out_fail_msg:
  92. free(self->files);
  93. out_fail:
  94. free(self);
  95. self = NULL;
  96. goto out;
  97. }
  98. static struct message *mesage__find(const char *msg)
  99. {
  100. struct message *m = message__list;
  101. while (m != NULL) {
  102. if (strcmp(m->msg, msg) == 0)
  103. break;
  104. m = m->next;
  105. }
  106. return m;
  107. }
  108. static int message__add_file_line(struct message *self, const char *file,
  109. int lineno)
  110. {
  111. int rc = -1;
  112. struct file_line *fl = file_line__new(file, lineno);
  113. if (fl == NULL)
  114. goto out;
  115. fl->next = self->files;
  116. self->files = fl;
  117. rc = 0;
  118. out:
  119. return rc;
  120. }
  121. static int message__add(const char *msg, char *option, const char *file,
  122. int lineno)
  123. {
  124. int rc = 0;
  125. char bf[16384];
  126. char *escaped = escape(msg, bf, sizeof(bf));
  127. struct message *m = mesage__find(escaped);
  128. if (m != NULL)
  129. rc = message__add_file_line(m, file, lineno);
  130. else {
  131. m = message__new(escaped, option, file, lineno);
  132. if (m != NULL) {
  133. m->next = message__list;
  134. message__list = m;
  135. } else
  136. rc = -1;
  137. }
  138. return rc;
  139. }
  140. static void menu_build_message_list(struct menu *menu)
  141. {
  142. struct menu *child;
  143. message__add(menu_get_prompt(menu), NULL,
  144. menu->file == NULL ? "Root Menu" : menu->file->name,
  145. menu->lineno);
  146. if (menu->sym != NULL && menu_has_help(menu))
  147. message__add(menu_get_help(menu), menu->sym->name,
  148. menu->file == NULL ? "Root Menu" : menu->file->name,
  149. menu->lineno);
  150. for (child = menu->list; child != NULL; child = child->next)
  151. if (child->prompt != NULL)
  152. menu_build_message_list(child);
  153. }
  154. static void message__print_file_lineno(struct message *self)
  155. {
  156. struct file_line *fl = self->files;
  157. putchar('\n');
  158. if (self->option != NULL)
  159. printf("# %s:00000\n", self->option);
  160. printf("#: %s:%d", fl->file, fl->lineno);
  161. fl = fl->next;
  162. while (fl != NULL) {
  163. printf(", %s:%d", fl->file, fl->lineno);
  164. fl = fl->next;
  165. }
  166. putchar('\n');
  167. }
  168. static void message__print_gettext_msgid_msgstr(struct message *self)
  169. {
  170. message__print_file_lineno(self);
  171. printf("msgid %s\n"
  172. "msgstr \"\"\n", self->msg);
  173. }
  174. static void menu__xgettext(void)
  175. {
  176. struct message *m = message__list;
  177. while (m != NULL) {
  178. /* skip empty lines ("") */
  179. if (strlen(m->msg) > sizeof("\"\""))
  180. message__print_gettext_msgid_msgstr(m);
  181. m = m->next;
  182. }
  183. }
  184. int main(int ac, char **av)
  185. {
  186. conf_parse(av[1]);
  187. menu_build_message_list(menu_get_root_menu(NULL));
  188. menu__xgettext();
  189. return 0;
  190. }