[PKT_SCHED]: Cleanup qdisc creation and alignment macros
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / scripts / kconfig / kxgettext.c
blob1c88d7c6d5a718ca9850bab1bf410f2439c51fab
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 #define LKC_DIRECT_LINK
11 #include "lkc.h"
13 static char *escape(const char* text, char *bf, int len)
15 char *bfp = bf;
16 int multiline = strchr(text, '\n') != NULL;
18 *bfp++ = '"';
19 --len;
21 if (multiline) {
22 *bfp++ = '"';
23 *bfp++ = '\n';
24 *bfp++ = '"';
25 len -= 3;
28 while (*text != '\0' && len > 1) {
29 if (*text == '"')
30 *bfp++ = '\\';
31 else if (*text == '\n') {
32 *bfp++ = '\\';
33 *bfp++ = 'n';
34 *bfp++ = '"';
35 *bfp++ = '\n';
36 *bfp++ = '"';
37 len -= 5;
38 ++text;
39 goto next;
41 *bfp++ = *text++;
42 next:
43 --len;
46 if (multiline)
47 bfp -= 3;
49 *bfp++ = '"';
50 *bfp = '\0';
52 return bf;
55 struct file_line {
56 struct file_line *next;
57 char* file;
58 int lineno;
61 static struct file_line *file_line__new(char *file, int lineno)
63 struct file_line *self = malloc(sizeof(*self));
65 if (self == NULL)
66 goto out;
68 self->file = file;
69 self->lineno = lineno;
70 self->next = NULL;
71 out:
72 return self;
75 struct message {
76 const char *msg;
77 const char *option;
78 struct message *next;
79 struct file_line *files;
82 static struct message *message__list;
84 static struct message *message__new(const char *msg, char *option, char *file, int lineno)
86 struct message *self = malloc(sizeof(*self));
88 if (self == NULL)
89 goto out;
91 self->files = file_line__new(file, lineno);
92 if (self->files == NULL)
93 goto out_fail;
95 self->msg = strdup(msg);
96 if (self->msg == NULL)
97 goto out_fail_msg;
99 self->option = option;
100 self->next = NULL;
101 out:
102 return self;
103 out_fail_msg:
104 free(self->files);
105 out_fail:
106 free(self);
107 self = NULL;
108 goto out;
111 static struct message *mesage__find(const char *msg)
113 struct message *m = message__list;
115 while (m != NULL) {
116 if (strcmp(m->msg, msg) == 0)
117 break;
118 m = m->next;
121 return m;
124 static int message__add_file_line(struct message *self, char *file, int lineno)
126 int rc = -1;
127 struct file_line *fl = file_line__new(file, lineno);
129 if (fl == NULL)
130 goto out;
132 fl->next = self->files;
133 self->files = fl;
134 rc = 0;
135 out:
136 return rc;
139 static int message__add(const char *msg, char *option, char *file, int lineno)
141 int rc = 0;
142 char bf[16384];
143 char *escaped = escape(msg, bf, sizeof(bf));
144 struct message *m = mesage__find(escaped);
146 if (m != NULL)
147 rc = message__add_file_line(m, file, lineno);
148 else {
149 m = message__new(escaped, option, file, lineno);
151 if (m != NULL) {
152 m->next = message__list;
153 message__list = m;
154 } else
155 rc = -1;
157 return rc;
160 void menu_build_message_list(struct menu *menu)
162 struct menu *child;
164 message__add(menu_get_prompt(menu), NULL,
165 menu->file == NULL ? "Root Menu" : menu->file->name,
166 menu->lineno);
168 if (menu->sym != NULL && menu->sym->help != NULL)
169 message__add(menu->sym->help, menu->sym->name,
170 menu->file == NULL ? "Root Menu" : menu->file->name,
171 menu->lineno);
173 for (child = menu->list; child != NULL; child = child->next)
174 if (child->prompt != NULL)
175 menu_build_message_list(child);
178 static void message__print_file_lineno(struct message *self)
180 struct file_line *fl = self->files;
182 printf("\n#: %s:%d", fl->file, fl->lineno);
183 fl = fl->next;
185 while (fl != NULL) {
186 printf(", %s:%d", fl->file, fl->lineno);
187 fl = fl->next;
190 if (self->option != NULL)
191 printf(", %s:00000", self->option);
193 putchar('\n');
196 static void message__print_gettext_msgid_msgstr(struct message *self)
198 message__print_file_lineno(self);
200 printf("msgid %s\n"
201 "msgstr \"\"\n", self->msg);
204 void menu__xgettext(void)
206 struct message *m = message__list;
208 while (m != NULL) {
209 message__print_gettext_msgid_msgstr(m);
210 m = m->next;
214 int main(int ac, char **av)
216 conf_parse(av[1]);
218 menu_build_message_list(menu_get_root_menu(NULL));
219 menu__xgettext();
220 return 0;