kxgettext.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. char* file;
  56. int lineno;
  57. };
  58. static struct file_line *file_line__new(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, 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, char *file, int lineno)
  109. {
  110. int rc = -1;
  111. struct file_line *fl = file_line__new(file, lineno);
  112. if (fl == NULL)
  113. goto out;
  114. fl->next = self->files;
  115. self->files = fl;
  116. rc = 0;
  117. out:
  118. return rc;
  119. }
  120. static int message__add(const char *msg, char *option, char *file, int lineno)
  121. {
  122. int rc = 0;
  123. char bf[16384];
  124. char *escaped = escape(msg, bf, sizeof(bf));
  125. struct message *m = mesage__find(escaped);
  126. if (m != NULL)
  127. rc = message__add_file_line(m, file, lineno);
  128. else {
  129. m = message__new(escaped, option, file, lineno);
  130. if (m != NULL) {
  131. m->next = message__list;
  132. message__list = m;
  133. } else
  134. rc = -1;
  135. }
  136. return rc;
  137. }
  138. void menu_build_message_list(struct menu *menu)
  139. {
  140. struct menu *child;
  141. message__add(menu_get_prompt(menu), NULL,
  142. menu->file == NULL ? "Root Menu" : menu->file->name,
  143. menu->lineno);
  144. if (menu->sym != NULL && menu_has_help(menu))
  145. message__add(menu_get_help(menu), menu->sym->name,
  146. menu->file == NULL ? "Root Menu" : menu->file->name,
  147. menu->lineno);
  148. for (child = menu->list; child != NULL; child = child->next)
  149. if (child->prompt != NULL)
  150. menu_build_message_list(child);
  151. }
  152. static void message__print_file_lineno(struct message *self)
  153. {
  154. struct file_line *fl = self->files;
  155. putchar('\n');
  156. if (self->option != NULL)
  157. printf("# %s:00000\n", self->option);
  158. printf("#: %s:%d", fl->file, fl->lineno);
  159. fl = fl->next;
  160. while (fl != NULL) {
  161. printf(", %s:%d", fl->file, fl->lineno);
  162. fl = fl->next;
  163. }
  164. putchar('\n');
  165. }
  166. static void message__print_gettext_msgid_msgstr(struct message *self)
  167. {
  168. message__print_file_lineno(self);
  169. printf("msgid %s\n"
  170. "msgstr \"\"\n", self->msg);
  171. }
  172. void menu__xgettext(void)
  173. {
  174. struct message *m = message__list;
  175. while (m != NULL) {
  176. /* skip empty lines ("") */
  177. if (strlen(m->msg) > sizeof("\"\""))
  178. message__print_gettext_msgid_msgstr(m);
  179. m = m->next;
  180. }
  181. }
  182. int main(int ac, char **av)
  183. {
  184. conf_parse(av[1]);
  185. menu_build_message_list(menu_get_root_menu(NULL));
  186. menu__xgettext();
  187. return 0;
  188. }