1 %option nostdinit noyywrap never-interactive full ecs
2 %option 8bit nodefault yylineno
3 %x COMMAND HELP STRING PARAM ASSIGN_VAL
6 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
7 * Released under the terms of the GNU GPL v2.0.
19 #define START_STRSIZE 16
27 static int text_size, text_asize;
30 struct buffer *parent;
31 YY_BUFFER_STATE state;
34 struct buffer *current_buf;
36 static int last_ts, first_ts;
38 static char *expand_token(const char *in, size_t n);
39 static void append_expanded_string(const char *in);
40 static void zconf_endhelp(void);
41 static void zconf_endfile(void);
43 static void new_string(void)
45 text = xmalloc(START_STRSIZE);
46 text_asize = START_STRSIZE;
51 static void append_string(const char *str, int size)
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 = xrealloc(text, new_size);
58 text_asize = new_size;
60 memcpy(text + text_size, str, size);
65 static void alloc_string(const char *str, int size)
67 text = xmalloc(size + 1);
68 memcpy(text, str, size);
72 static void warn_ignored_character(char chr)
75 "%s:%d:warning: ignoring unsupported character '%c'\n",
76 current_file->name, yylineno, chr);
99 const struct kconf_id *id = kconf_id_lookup(yytext, yyleng);
100 current_pos.file = current_file;
101 current_pos.lineno = yylineno;
102 if (id && id->flags & TF_COMMAND) {
107 alloc_string(yytext, yyleng);
108 yylval.string = text;
112 /* this token includes at least one '$' */
113 yylval.string = expand_token(yytext, yyleng);
114 if (strlen(yylval.string))
118 "=" { BEGIN(ASSIGN_VAL); yylval.flavor = VAR_RECURSIVE; return T_ASSIGN; }
119 ":=" { BEGIN(ASSIGN_VAL); yylval.flavor = VAR_SIMPLE; return T_ASSIGN; }
120 "+=" { BEGIN(ASSIGN_VAL); yylval.flavor = VAR_APPEND; return T_ASSIGN; }
122 . warn_ignored_character(*yytext);
131 alloc_string(yytext, yyleng);
132 yylval.string = text;
135 \n { BEGIN(INITIAL); return T_EOL; }
142 "(" return T_OPEN_PAREN;
143 ")" return T_CLOSE_PAREN;
146 "!=" return T_UNEQUAL;
147 "<=" return T_LESS_EQUAL;
148 ">=" return T_GREATER_EQUAL;
150 ">" return T_GREATER;
156 \n BEGIN(INITIAL); return T_EOL;
158 const struct kconf_id *id = kconf_id_lookup(yytext, yyleng);
159 if (id && id->flags & TF_PARAM) {
163 alloc_string(yytext, yyleng);
164 yylval.string = text;
168 /* this token includes at least one '$' */
169 yylval.string = expand_token(yytext, yyleng);
170 if (strlen(yylval.string))
177 . warn_ignored_character(*yytext);
184 "$".* append_expanded_string(yytext);
186 append_string(yytext, yyleng);
189 append_string(yytext + 1, yyleng - 1);
192 if (str == yytext[0]) {
194 yylval.string = text;
197 append_string(yytext, 1);
201 "%s:%d:warning: multi-line strings not supported\n",
202 zconf_curname(), zconf_lineno());
205 yylval.string = text;
210 yylval.string = text;
218 for (i = 0; i < yyleng; i++) {
219 if (yytext[i] == '\t')
232 append_string(" ", 8);
235 append_string(" ", ts);
243 append_string("\n", 1);
247 if ((yytext[yyleng-1] != ' ') && (yytext[yyleng-1] != '\t'))
251 append_string(yytext, yyleng);
271 static char *expand_token(const char *in, size_t n)
276 const char *rest, *end;
279 append_string(in, n);
281 /* get the whole line because we do not know the end of token. */
282 while ((c = input()) != EOF) {
288 append_string(&c2, 1);
292 out = expand_one_token(&rest);
294 /* push back unused characters to the input stream */
295 end = rest + strlen(rest);
304 static void append_expanded_string(const char *str)
311 res = expand_dollar(&str);
313 /* push back unused characters to the input stream */
314 end = str + strlen(str);
318 append_string(res, strlen(res));
323 void zconf_starthelp(void)
326 last_ts = first_ts = 0;
330 static void zconf_endhelp(void)
332 yylval.string = text;
338 * Try to open specified file with following names:
341 * The latter is used when srctree is separate from objtree
342 * when compiling the firmware.
343 * Return NULL if file is not found.
345 FILE *zconf_fopen(const char *name)
347 char *env, fullname[PATH_MAX+1];
350 f = fopen(name, "r");
351 if (!f && name != NULL && name[0] != '/') {
352 env = getenv(SRCTREE);
354 sprintf(fullname, "%s/%s", env, name);
355 f = fopen(fullname, "r");
361 void zconf_initscan(const char *name)
363 yyin = zconf_fopen(name);
365 fprintf(stderr, "can't find file %s\n", name);
369 current_buf = xmalloc(sizeof(*current_buf));
370 memset(current_buf, 0, sizeof(*current_buf));
372 current_file = file_lookup(name);
376 void zconf_nextfile(const char *name)
379 struct file *file = file_lookup(name);
380 struct buffer *buf = xmalloc(sizeof(*buf));
381 memset(buf, 0, sizeof(*buf));
383 current_buf->state = YY_CURRENT_BUFFER;
384 yyin = zconf_fopen(file->name);
386 fprintf(stderr, "%s:%d: can't open file \"%s\"\n",
387 zconf_curname(), zconf_lineno(), file->name);
390 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
391 buf->parent = current_buf;
394 current_file->lineno = yylineno;
395 file->parent = current_file;
397 for (iter = current_file; iter; iter = iter->parent) {
398 if (!strcmp(iter->name, file->name)) {
400 "Recursive inclusion detected.\n"
402 " current file : %s\n", file->name);
406 fprintf(stderr, " included from: %s:%d\n",
407 iter->name, iter->lineno - 1);
408 } while (strcmp(iter->name, file->name));
417 static void zconf_endfile(void)
419 struct buffer *parent;
421 current_file = current_file->parent;
423 yylineno = current_file->lineno;
425 parent = current_buf->parent;
428 yy_delete_buffer(YY_CURRENT_BUFFER);
429 yy_switch_to_buffer(parent->state);
432 current_buf = parent;
435 int zconf_lineno(void)
437 return current_pos.lineno;
440 const char *zconf_curname(void)
442 return current_pos.file ? current_pos.file->name : "<none>";