kxgettext.c 4.0 KB

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