0287f454e380a35e7e981a485a3c57b7ef507265
[openwrt/staging/mkresin.git] / scripts / config / zconf.l
1 %option nostdinit noyywrap never-interactive full ecs
2 %option 8bit nodefault perf-report perf-report
3 %option noinput
4 %x COMMAND HELP STRING PARAM
5 %{
6 /*
7 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
8 * Released under the terms of the GNU GPL v2.0.
9 */
10
11 #include <limits.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <unistd.h>
16 #include <glob.h>
17 #include <libgen.h>
18
19 #include "lkc.h"
20
21 #define START_STRSIZE 16
22
23 static struct {
24 struct file *file;
25 int lineno;
26 } current_pos;
27
28 static char *text;
29 static int text_size, text_asize;
30
31 struct buffer {
32 struct buffer *parent;
33 YY_BUFFER_STATE state;
34 };
35
36 struct buffer *current_buf;
37
38 static int last_ts, first_ts;
39
40 static void zconf_endhelp(void);
41 static void zconf_endfile(void);
42
43 static void new_string(void)
44 {
45 text = xmalloc(START_STRSIZE);
46 text_asize = START_STRSIZE;
47 text_size = 0;
48 *text = 0;
49 }
50
51 static void append_string(const char *str, int size)
52 {
53 int new_size = text_size + size + 1;
54 if (new_size > text_asize) {
55 new_size += START_STRSIZE - 1;
56 new_size &= -START_STRSIZE;
57 text = realloc(text, new_size);
58 text_asize = new_size;
59 }
60 memcpy(text + text_size, str, size);
61 text_size += size;
62 text[text_size] = 0;
63 }
64
65 static void alloc_string(const char *str, int size)
66 {
67 text = xmalloc(size + 1);
68 memcpy(text, str, size);
69 text[size] = 0;
70 }
71 %}
72
73 ws [ \n\t]
74 n [A-Za-z0-9_]
75
76 %%
77 int str = 0;
78 int ts, i;
79
80 [ \t]*#.*\n |
81 [ \t]*\n {
82 current_file->lineno++;
83 return T_EOL;
84 }
85 [ \t]*#.*
86
87
88 [ \t]+ {
89 BEGIN(COMMAND);
90 }
91
92 . {
93 unput(yytext[0]);
94 BEGIN(COMMAND);
95 }
96
97
98 <COMMAND>{
99 {n}+ {
100 const struct kconf_id *id = kconf_id_lookup(yytext, yyleng);
101 BEGIN(PARAM);
102 current_pos.file = current_file;
103 current_pos.lineno = current_file->lineno;
104 if (id && id->flags & TF_COMMAND) {
105 zconflval.id = id;
106 return id->token;
107 }
108 alloc_string(yytext, yyleng);
109 zconflval.string = text;
110 return T_WORD;
111 }
112 .
113 \n {
114 BEGIN(INITIAL);
115 current_file->lineno++;
116 return T_EOL;
117 }
118 }
119
120 <PARAM>{
121 "&&" return T_AND;
122 "||" return T_OR;
123 "(" return T_OPEN_PAREN;
124 ")" return T_CLOSE_PAREN;
125 "!" return T_NOT;
126 "=" return T_EQUAL;
127 "!=" return T_UNEQUAL;
128 \"|\' {
129 str = yytext[0];
130 new_string();
131 BEGIN(STRING);
132 }
133 \n BEGIN(INITIAL); current_file->lineno++; return T_EOL;
134 --- /* ignore */
135 ({n}|[-/.])+ {
136 const struct kconf_id *id = kconf_id_lookup(yytext, yyleng);
137 if (id && id->flags & TF_PARAM) {
138 zconflval.id = id;
139 return id->token;
140 }
141 alloc_string(yytext, yyleng);
142 zconflval.string = text;
143 return T_WORD;
144 }
145 #.* /* comment */
146 \\\n current_file->lineno++;
147 .
148 <<EOF>> {
149 BEGIN(INITIAL);
150 }
151 }
152
153 <STRING>{
154 [^'"\\\n]+/\n {
155 append_string(yytext, yyleng);
156 zconflval.string = text;
157 return T_WORD_QUOTE;
158 }
159 [^'"\\\n]+ {
160 append_string(yytext, yyleng);
161 }
162 \\.?/\n {
163 append_string(yytext + 1, yyleng - 1);
164 zconflval.string = text;
165 return T_WORD_QUOTE;
166 }
167 \\.? {
168 append_string(yytext + 1, yyleng - 1);
169 }
170 \'|\" {
171 if (str == yytext[0]) {
172 BEGIN(PARAM);
173 zconflval.string = text;
174 return T_WORD_QUOTE;
175 } else
176 append_string(yytext, 1);
177 }
178 \n {
179 printf("%s:%d:warning: multi-line strings not supported\n", zconf_curname(), zconf_lineno());
180 current_file->lineno++;
181 BEGIN(INITIAL);
182 return T_EOL;
183 }
184 <<EOF>> {
185 BEGIN(INITIAL);
186 }
187 }
188
189 <HELP>{
190 [ \t]+ {
191 ts = 0;
192 for (i = 0; i < yyleng; i++) {
193 if (yytext[i] == '\t')
194 ts = (ts & ~7) + 8;
195 else
196 ts++;
197 }
198 last_ts = ts;
199 if (first_ts) {
200 if (ts < first_ts) {
201 zconf_endhelp();
202 return T_HELPTEXT;
203 }
204 ts -= first_ts;
205 while (ts > 8) {
206 append_string(" ", 8);
207 ts -= 8;
208 }
209 append_string(" ", ts);
210 }
211 }
212 [ \t]*\n/[^ \t\n] {
213 current_file->lineno++;
214 zconf_endhelp();
215 return T_HELPTEXT;
216 }
217 [ \t]*\n {
218 current_file->lineno++;
219 append_string("\n", 1);
220 }
221 [^ \t\n].* {
222 while (yyleng) {
223 if ((yytext[yyleng-1] != ' ') && (yytext[yyleng-1] != '\t'))
224 break;
225 yyleng--;
226 }
227 append_string(yytext, yyleng);
228 if (!first_ts)
229 first_ts = last_ts;
230 }
231 <<EOF>> {
232 zconf_endhelp();
233 return T_HELPTEXT;
234 }
235 }
236
237 <<EOF>> {
238 if (current_file) {
239 zconf_endfile();
240 return T_EOL;
241 }
242 fclose(yyin);
243 yyterminate();
244 }
245
246 %%
247 void zconf_starthelp(void)
248 {
249 new_string();
250 last_ts = first_ts = 0;
251 BEGIN(HELP);
252 }
253
254 static void zconf_endhelp(void)
255 {
256 zconflval.string = text;
257 BEGIN(INITIAL);
258 }
259
260
261 /*
262 * Try to open specified file with following names:
263 * ./name
264 * $(srctree)/name
265 * The latter is used when srctree is separate from objtree
266 * when compiling the kernel.
267 * Return NULL if file is not found.
268 */
269 FILE *zconf_fopen(const char *name)
270 {
271 char *env, fullname[PATH_MAX+1];
272 FILE *f;
273
274 f = fopen(name, "r");
275 if (!f && name != NULL && name[0] != '/') {
276 env = getenv(SRCTREE);
277 if (env) {
278 sprintf(fullname, "%s/%s", env, name);
279 f = fopen(fullname, "r");
280 }
281 }
282 return f;
283 }
284
285 void zconf_initscan(const char *name)
286 {
287 yyin = zconf_fopen(name);
288 if (!yyin) {
289 printf("can't find file %s\n", name);
290 exit(1);
291 }
292
293 current_buf = xmalloc(sizeof(*current_buf));
294 memset(current_buf, 0, sizeof(*current_buf));
295
296 current_file = file_lookup(name);
297 current_file->lineno = 1;
298 }
299
300 static void __zconf_nextfile(const char *name)
301 {
302 struct file *iter;
303 struct file *file = file_lookup(name);
304 struct buffer *buf = xmalloc(sizeof(*buf));
305 memset(buf, 0, sizeof(*buf));
306
307 current_buf->state = YY_CURRENT_BUFFER;
308 yyin = zconf_fopen(file->name);
309 if (!yyin) {
310 printf("%s:%d: can't open file \"%s\"\n",
311 zconf_curname(), zconf_lineno(), file->name);
312 exit(1);
313 }
314 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
315 buf->parent = current_buf;
316 current_buf = buf;
317
318 for (iter = current_file->parent; iter; iter = iter->parent ) {
319 if (!strcmp(current_file->name,iter->name) ) {
320 printf("%s:%d: recursive inclusion detected. "
321 "Inclusion path:\n current file : '%s'\n",
322 zconf_curname(), zconf_lineno(),
323 zconf_curname());
324 iter = current_file->parent;
325 while (iter && \
326 strcmp(iter->name,current_file->name)) {
327 printf(" included from: '%s:%d'\n",
328 iter->name, iter->lineno-1);
329 iter = iter->parent;
330 }
331 if (iter)
332 printf(" included from: '%s:%d'\n",
333 iter->name, iter->lineno+1);
334 exit(1);
335 }
336 }
337 file->lineno = 1;
338 file->parent = current_file;
339 current_file = file;
340 }
341
342 void zconf_nextfile(const char *name)
343 {
344 glob_t gl;
345 int err;
346 int i;
347 char path[PATH_MAX], *p;
348
349 err = glob(name, GLOB_ERR | GLOB_MARK, NULL, &gl);
350
351 /* ignore wildcard patterns that return no result */
352 if (err == GLOB_NOMATCH && strchr(name, '*')) {
353 err = 0;
354 gl.gl_pathc = 0;
355 }
356
357 if (err == GLOB_NOMATCH) {
358 p = strdup(current_file->name);
359 if (p) {
360 snprintf(path, sizeof(path), "%s/%s", dirname(p), name);
361 err = glob(path, GLOB_ERR | GLOB_MARK, NULL, &gl);
362 free(p);
363 }
364 }
365
366 if (err) {
367 const char *reason = "unknown error";
368
369 switch (err) {
370 case GLOB_NOSPACE:
371 reason = "out of memory";
372 break;
373 case GLOB_ABORTED:
374 reason = "read error";
375 break;
376 case GLOB_NOMATCH:
377 reason = "No files found";
378 break;
379 default:
380 break;
381 }
382
383 printf("%s:%d: glob failed: %s \"%s\"\n", zconf_curname(), zconf_lineno(),
384 reason, name);
385
386 exit(1);
387 }
388
389 for (i = 0; i < gl.gl_pathc; i++)
390 __zconf_nextfile(gl.gl_pathv[i]);
391 }
392
393 static void zconf_endfile(void)
394 {
395 struct buffer *parent;
396
397 current_file = current_file->parent;
398
399 parent = current_buf->parent;
400 if (parent) {
401 fclose(yyin);
402 yy_delete_buffer(YY_CURRENT_BUFFER);
403 yy_switch_to_buffer(parent->state);
404 }
405 free(current_buf);
406 current_buf = parent;
407 }
408
409 int zconf_lineno(void)
410 {
411 return current_pos.lineno;
412 }
413
414 const char *zconf_curname(void)
415 {
416 return current_pos.file ? current_pos.file->name : "<none>";
417 }