support/misc: Adding Vagrant file for provisioning
[buildroot-gz.git] / support / kconfig / kxgettext.c
blob2858738b22d5aeac27154f74af433540839d37a6
1 /*
2 * Arnaldo Carvalho de Melo <acme@conectiva.com.br>, 2005
4 * Released under the terms of the GNU GPL v2.0
5 */
7 #include <stdlib.h>
8 #include <string.h>
10 #include "lkc.h"
12 static char *escape(const char* text, char *bf, int len)
14 char *bfp = bf;
15 int multiline = strchr(text, '\n') != NULL;
16 int eol = 0;
17 int textlen = strlen(text);
19 if ((textlen > 0) && (text[textlen-1] == '\n'))
20 eol = 1;
22 *bfp++ = '"';
23 --len;
25 if (multiline) {
26 *bfp++ = '"';
27 *bfp++ = '\n';
28 *bfp++ = '"';
29 len -= 3;
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;
45 else if (*text == '\\') {
46 *bfp++ = '\\';
47 len--;
49 *bfp++ = *text++;
50 next:
51 --len;
54 if (multiline && eol)
55 bfp -= 3;
57 *bfp++ = '"';
58 *bfp = '\0';
60 return bf;
63 struct file_line {
64 struct file_line *next;
65 const char *file;
66 int lineno;
69 static struct file_line *file_line__new(const char *file, int lineno)
71 struct file_line *self = malloc(sizeof(*self));
73 if (self == NULL)
74 goto out;
76 self->file = file;
77 self->lineno = lineno;
78 self->next = NULL;
79 out:
80 return self;
83 struct message {
84 const char *msg;
85 const char *option;
86 struct message *next;
87 struct file_line *files;
90 static struct message *message__list;
92 static struct message *message__new(const char *msg, char *option,
93 const char *file, int lineno)
95 struct message *self = malloc(sizeof(*self));
97 if (self == NULL)
98 goto out;
100 self->files = file_line__new(file, lineno);
101 if (self->files == NULL)
102 goto out_fail;
104 self->msg = strdup(msg);
105 if (self->msg == NULL)
106 goto out_fail_msg;
108 self->option = option;
109 self->next = NULL;
110 out:
111 return self;
112 out_fail_msg:
113 free(self->files);
114 out_fail:
115 free(self);
116 self = NULL;
117 goto out;
120 static struct message *mesage__find(const char *msg)
122 struct message *m = message__list;
124 while (m != NULL) {
125 if (strcmp(m->msg, msg) == 0)
126 break;
127 m = m->next;
130 return m;
133 static int message__add_file_line(struct message *self, const char *file,
134 int lineno)
136 int rc = -1;
137 struct file_line *fl = file_line__new(file, lineno);
139 if (fl == NULL)
140 goto out;
142 fl->next = self->files;
143 self->files = fl;
144 rc = 0;
145 out:
146 return rc;
149 static int message__add(const char *msg, char *option, const char *file,
150 int lineno)
152 int rc = 0;
153 char bf[16384];
154 char *escaped = escape(msg, bf, sizeof(bf));
155 struct message *m = mesage__find(escaped);
157 if (m != NULL)
158 rc = message__add_file_line(m, file, lineno);
159 else {
160 m = message__new(escaped, option, file, lineno);
162 if (m != NULL) {
163 m->next = message__list;
164 message__list = m;
165 } else
166 rc = -1;
168 return rc;
171 static void menu_build_message_list(struct menu *menu)
173 struct menu *child;
175 message__add(menu_get_prompt(menu), NULL,
176 menu->file == NULL ? "Root Menu" : menu->file->name,
177 menu->lineno);
179 if (menu->sym != NULL && menu_has_help(menu))
180 message__add(menu_get_help(menu), menu->sym->name,
181 menu->file == NULL ? "Root Menu" : menu->file->name,
182 menu->lineno);
184 for (child = menu->list; child != NULL; child = child->next)
185 if (child->prompt != NULL)
186 menu_build_message_list(child);
189 static void message__print_file_lineno(struct message *self)
191 struct file_line *fl = self->files;
193 putchar('\n');
194 if (self->option != NULL)
195 printf("# %s:00000\n", self->option);
197 printf("#: %s:%d", fl->file, fl->lineno);
198 fl = fl->next;
200 while (fl != NULL) {
201 printf(", %s:%d", fl->file, fl->lineno);
202 fl = fl->next;
205 putchar('\n');
208 static void message__print_gettext_msgid_msgstr(struct message *self)
210 message__print_file_lineno(self);
212 printf("msgid %s\n"
213 "msgstr \"\"\n", self->msg);
216 static void menu__xgettext(void)
218 struct message *m = message__list;
220 while (m != NULL) {
221 /* skip empty lines ("") */
222 if (strlen(m->msg) > sizeof("\"\""))
223 message__print_gettext_msgid_msgstr(m);
224 m = m->next;
228 int main(int ac, char **av)
230 conf_parse(av[1]);
232 menu_build_message_list(menu_get_root_menu(NULL));
233 menu__xgettext();
234 return 0;