blob: 2858738b22d5aeac27154f74af433540839d37a6 [file] [log] [blame]
Arnaldo Carvalho de Melo3b9fa092005-05-05 15:09:46 -07001/*
2 * Arnaldo Carvalho de Melo <acme@conectiva.com.br>, 2005
3 *
4 * Released under the terms of the GNU GPL v2.0
5 */
6
7#include <stdlib.h>
8#include <string.h>
9
Arnaldo Carvalho de Melo3b9fa092005-05-05 15:09:46 -070010#include "lkc.h"
11
12static char *escape(const char* text, char *bf, int len)
13{
14 char *bfp = bf;
15 int multiline = strchr(text, '\n') != NULL;
Egry Gabor964267e2005-09-03 15:55:14 -070016 int eol = 0;
17 int textlen = strlen(text);
18
19 if ((textlen > 0) && (text[textlen-1] == '\n'))
20 eol = 1;
Arnaldo Carvalho de Melo3b9fa092005-05-05 15:09:46 -070021
22 *bfp++ = '"';
23 --len;
24
25 if (multiline) {
26 *bfp++ = '"';
27 *bfp++ = '\n';
28 *bfp++ = '"';
29 len -= 3;
30 }
31
32 while (*text != '\0' && len > 1) {
33 if (*text == '"')
34 *bfp++ = '\\';
35 else if (*text == '\n') {
36 *bfp++ = '\\';
37 *bfp++ = 'n';
38 *bfp++ = '"';
39 *bfp++ = '\n';
40 *bfp++ = '"';
41 len -= 5;
42 ++text;
43 goto next;
44 }
Sam Ravnborg612c2802009-04-08 08:42:48 +020045 else if (*text == '\\') {
46 *bfp++ = '\\';
47 len--;
48 }
Arnaldo Carvalho de Melo3b9fa092005-05-05 15:09:46 -070049 *bfp++ = *text++;
50next:
51 --len;
52 }
53
Egry Gabor964267e2005-09-03 15:55:14 -070054 if (multiline && eol)
Arnaldo Carvalho de Melo3b9fa092005-05-05 15:09:46 -070055 bfp -= 3;
56
57 *bfp++ = '"';
58 *bfp = '\0';
59
60 return bf;
61}
62
63struct file_line {
64 struct file_line *next;
Michal Mareka01ebe12010-09-30 22:17:48 +020065 const char *file;
66 int lineno;
Arnaldo Carvalho de Melo3b9fa092005-05-05 15:09:46 -070067};
68
Michal Mareka01ebe12010-09-30 22:17:48 +020069static struct file_line *file_line__new(const char *file, int lineno)
Arnaldo Carvalho de Melo3b9fa092005-05-05 15:09:46 -070070{
71 struct file_line *self = malloc(sizeof(*self));
72
73 if (self == NULL)
74 goto out;
75
76 self->file = file;
77 self->lineno = lineno;
78 self->next = NULL;
79out:
80 return self;
81}
82
83struct message {
84 const char *msg;
85 const char *option;
86 struct message *next;
87 struct file_line *files;
88};
89
90static struct message *message__list;
91
Michal Mareka01ebe12010-09-30 22:17:48 +020092static struct message *message__new(const char *msg, char *option,
93 const char *file, int lineno)
Arnaldo Carvalho de Melo3b9fa092005-05-05 15:09:46 -070094{
95 struct message *self = malloc(sizeof(*self));
96
97 if (self == NULL)
98 goto out;
99
100 self->files = file_line__new(file, lineno);
101 if (self->files == NULL)
102 goto out_fail;
103
104 self->msg = strdup(msg);
105 if (self->msg == NULL)
106 goto out_fail_msg;
107
108 self->option = option;
109 self->next = NULL;
110out:
111 return self;
112out_fail_msg:
113 free(self->files);
114out_fail:
115 free(self);
116 self = NULL;
117 goto out;
118}
119
120static struct message *mesage__find(const char *msg)
121{
122 struct message *m = message__list;
123
124 while (m != NULL) {
125 if (strcmp(m->msg, msg) == 0)
126 break;
127 m = m->next;
128 }
129
130 return m;
131}
132
Michal Mareka01ebe12010-09-30 22:17:48 +0200133static int message__add_file_line(struct message *self, const char *file,
134 int lineno)
Arnaldo Carvalho de Melo3b9fa092005-05-05 15:09:46 -0700135{
136 int rc = -1;
137 struct file_line *fl = file_line__new(file, lineno);
138
139 if (fl == NULL)
140 goto out;
141
142 fl->next = self->files;
143 self->files = fl;
144 rc = 0;
145out:
146 return rc;
147}
148
Michal Mareka01ebe12010-09-30 22:17:48 +0200149static int message__add(const char *msg, char *option, const char *file,
150 int lineno)
Arnaldo Carvalho de Melo3b9fa092005-05-05 15:09:46 -0700151{
152 int rc = 0;
153 char bf[16384];
154 char *escaped = escape(msg, bf, sizeof(bf));
155 struct message *m = mesage__find(escaped);
156
157 if (m != NULL)
158 rc = message__add_file_line(m, file, lineno);
159 else {
160 m = message__new(escaped, option, file, lineno);
161
162 if (m != NULL) {
163 m->next = message__list;
164 message__list = m;
165 } else
166 rc = -1;
167 }
168 return rc;
169}
170
Trevor Keith4356f482009-09-18 12:49:23 -0700171static void menu_build_message_list(struct menu *menu)
Arnaldo Carvalho de Melo3b9fa092005-05-05 15:09:46 -0700172{
173 struct menu *child;
174
175 message__add(menu_get_prompt(menu), NULL,
176 menu->file == NULL ? "Root Menu" : menu->file->name,
177 menu->lineno);
178
Sam Ravnborg03d29122007-07-21 00:00:36 +0200179 if (menu->sym != NULL && menu_has_help(menu))
180 message__add(menu_get_help(menu), menu->sym->name,
Arnaldo Carvalho de Melo3b9fa092005-05-05 15:09:46 -0700181 menu->file == NULL ? "Root Menu" : menu->file->name,
182 menu->lineno);
183
184 for (child = menu->list; child != NULL; child = child->next)
185 if (child->prompt != NULL)
186 menu_build_message_list(child);
187}
188
189static void message__print_file_lineno(struct message *self)
190{
191 struct file_line *fl = self->files;
192
Egry Gaborc196eff2005-09-03 15:55:12 -0700193 putchar('\n');
194 if (self->option != NULL)
195 printf("# %s:00000\n", self->option);
196
197 printf("#: %s:%d", fl->file, fl->lineno);
Arnaldo Carvalho de Melo3b9fa092005-05-05 15:09:46 -0700198 fl = fl->next;
199
200 while (fl != NULL) {
201 printf(", %s:%d", fl->file, fl->lineno);
202 fl = fl->next;
203 }
204
Arnaldo Carvalho de Melo3b9fa092005-05-05 15:09:46 -0700205 putchar('\n');
206}
207
208static void message__print_gettext_msgid_msgstr(struct message *self)
209{
210 message__print_file_lineno(self);
211
212 printf("msgid %s\n"
213 "msgstr \"\"\n", self->msg);
214}
215
Trevor Keith4356f482009-09-18 12:49:23 -0700216static void menu__xgettext(void)
Arnaldo Carvalho de Melo3b9fa092005-05-05 15:09:46 -0700217{
218 struct message *m = message__list;
219
220 while (m != NULL) {
Sam Ravnborgb70e325c2007-06-10 20:38:27 +0200221 /* skip empty lines ("") */
222 if (strlen(m->msg) > sizeof("\"\""))
223 message__print_gettext_msgid_msgstr(m);
Arnaldo Carvalho de Melo3b9fa092005-05-05 15:09:46 -0700224 m = m->next;
225 }
226}
227
228int main(int ac, char **av)
229{
230 conf_parse(av[1]);
231
232 menu_build_message_list(menu_get_root_menu(NULL));
233 menu__xgettext();
234 return 0;
235}