kxgettext.c 4.1 KB

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