0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
0004 */
0005 %option nostdinit noyywrap never-interactive full ecs
0006 %option 8bit nodefault yylineno
0007 %x ASSIGN_VAL HELP STRING
0008 %{
0009
0010 #include <assert.h>
0011 #include <limits.h>
0012 #include <stdio.h>
0013 #include <stdlib.h>
0014 #include <string.h>
0015
0016 #include "lkc.h"
0017 #include "parser.tab.h"
0018
0019 #define YY_DECL static int yylex1(void)
0020
0021 #define START_STRSIZE 16
0022
0023 static struct {
0024 struct file *file;
0025 int lineno;
0026 } current_pos;
0027
0028 static int prev_prev_token = T_EOL;
0029 static int prev_token = T_EOL;
0030 static char *text;
0031 static int text_size, text_asize;
0032
0033 struct buffer {
0034 struct buffer *parent;
0035 YY_BUFFER_STATE state;
0036 };
0037
0038 static struct buffer *current_buf;
0039
0040 static int last_ts, first_ts;
0041
0042 static char *expand_token(const char *in, size_t n);
0043 static void append_expanded_string(const char *in);
0044 static void zconf_endhelp(void);
0045 static void zconf_endfile(void);
0046
0047 static void new_string(void)
0048 {
0049 text = xmalloc(START_STRSIZE);
0050 text_asize = START_STRSIZE;
0051 text_size = 0;
0052 *text = 0;
0053 }
0054
0055 static void append_string(const char *str, int size)
0056 {
0057 int new_size = text_size + size + 1;
0058 if (new_size > text_asize) {
0059 new_size += START_STRSIZE - 1;
0060 new_size &= -START_STRSIZE;
0061 text = xrealloc(text, new_size);
0062 text_asize = new_size;
0063 }
0064 memcpy(text + text_size, str, size);
0065 text_size += size;
0066 text[text_size] = 0;
0067 }
0068
0069 static void alloc_string(const char *str, int size)
0070 {
0071 text = xmalloc(size + 1);
0072 memcpy(text, str, size);
0073 text[size] = 0;
0074 }
0075
0076 static void warn_ignored_character(char chr)
0077 {
0078 fprintf(stderr,
0079 "%s:%d:warning: ignoring unsupported character '%c'\n",
0080 current_file->name, yylineno, chr);
0081 }
0082 %}
0083
0084 n [A-Za-z0-9_-]
0085
0086 %%
0087 char open_quote = 0;
0088
0089 #.* /* ignore comment */
0090 [ \t]* /* whitespaces */
0091 \\\n /* escaped new line */
0092 \n return T_EOL;
0093 "bool" return T_BOOL;
0094 "choice" return T_CHOICE;
0095 "comment" return T_COMMENT;
0096 "config" return T_CONFIG;
0097 "def_bool" return T_DEF_BOOL;
0098 "def_tristate" return T_DEF_TRISTATE;
0099 "default" return T_DEFAULT;
0100 "depends" return T_DEPENDS;
0101 "endchoice" return T_ENDCHOICE;
0102 "endif" return T_ENDIF;
0103 "endmenu" return T_ENDMENU;
0104 "help" return T_HELP;
0105 "hex" return T_HEX;
0106 "if" return T_IF;
0107 "imply" return T_IMPLY;
0108 "int" return T_INT;
0109 "mainmenu" return T_MAINMENU;
0110 "menu" return T_MENU;
0111 "menuconfig" return T_MENUCONFIG;
0112 "modules" return T_MODULES;
0113 "on" return T_ON;
0114 "optional" return T_OPTIONAL;
0115 "prompt" return T_PROMPT;
0116 "range" return T_RANGE;
0117 "select" return T_SELECT;
0118 "source" return T_SOURCE;
0119 "string" return T_STRING;
0120 "tristate" return T_TRISTATE;
0121 "visible" return T_VISIBLE;
0122 "||" return T_OR;
0123 "&&" return T_AND;
0124 "=" return T_EQUAL;
0125 "!=" return T_UNEQUAL;
0126 "<" return T_LESS;
0127 "<=" return T_LESS_EQUAL;
0128 ">" return T_GREATER;
0129 ">=" return T_GREATER_EQUAL;
0130 "!" return T_NOT;
0131 "(" return T_OPEN_PAREN;
0132 ")" return T_CLOSE_PAREN;
0133 ":=" return T_COLON_EQUAL;
0134 "+=" return T_PLUS_EQUAL;
0135 \"|\' {
0136 open_quote = yytext[0];
0137 new_string();
0138 BEGIN(STRING);
0139 }
0140 {n}+ {
0141 alloc_string(yytext, yyleng);
0142 yylval.string = text;
0143 return T_WORD;
0144 }
0145 ({n}|$)+ {
0146 /* this token includes at least one '$' */
0147 yylval.string = expand_token(yytext, yyleng);
0148 if (strlen(yylval.string))
0149 return T_WORD;
0150 free(yylval.string);
0151 }
0152 . warn_ignored_character(*yytext);
0153
0154 <ASSIGN_VAL>{
0155 [^[:blank:]\n]+.* {
0156 alloc_string(yytext, yyleng);
0157 yylval.string = text;
0158 return T_ASSIGN_VAL;
0159 }
0160 \n { BEGIN(INITIAL); return T_EOL; }
0161 .
0162 }
0163
0164 <STRING>{
0165 "$".* append_expanded_string(yytext);
0166 [^$'"\\\n]+ {
0167 append_string(yytext, yyleng);
0168 }
0169 \\.? {
0170 append_string(yytext + 1, yyleng - 1);
0171 }
0172 \'|\" {
0173 if (open_quote == yytext[0]) {
0174 BEGIN(INITIAL);
0175 yylval.string = text;
0176 return T_WORD_QUOTE;
0177 } else
0178 append_string(yytext, 1);
0179 }
0180 \n {
0181 fprintf(stderr,
0182 "%s:%d:warning: multi-line strings not supported\n",
0183 zconf_curname(), zconf_lineno());
0184 unput('\n');
0185 BEGIN(INITIAL);
0186 yylval.string = text;
0187 return T_WORD_QUOTE;
0188 }
0189 <<EOF>> {
0190 BEGIN(INITIAL);
0191 yylval.string = text;
0192 return T_WORD_QUOTE;
0193 }
0194 }
0195
0196 <HELP>{
0197 [ \t]+ {
0198 int ts, i;
0199
0200 ts = 0;
0201 for (i = 0; i < yyleng; i++) {
0202 if (yytext[i] == '\t')
0203 ts = (ts & ~7) + 8;
0204 else
0205 ts++;
0206 }
0207 last_ts = ts;
0208 if (first_ts) {
0209 if (ts < first_ts) {
0210 zconf_endhelp();
0211 return T_HELPTEXT;
0212 }
0213 ts -= first_ts;
0214 while (ts > 8) {
0215 append_string(" ", 8);
0216 ts -= 8;
0217 }
0218 append_string(" ", ts);
0219 }
0220 }
0221 [ \t]*\n/[^ \t\n] {
0222 zconf_endhelp();
0223 return T_HELPTEXT;
0224 }
0225 [ \t]*\n {
0226 append_string("\n", 1);
0227 }
0228 [^ \t\n].* {
0229 while (yyleng) {
0230 if ((yytext[yyleng-1] != ' ') && (yytext[yyleng-1] != '\t'))
0231 break;
0232 yyleng--;
0233 }
0234 append_string(yytext, yyleng);
0235 if (!first_ts)
0236 first_ts = last_ts;
0237 }
0238 <<EOF>> {
0239 zconf_endhelp();
0240 return T_HELPTEXT;
0241 }
0242 }
0243
0244 <<EOF>> {
0245 BEGIN(INITIAL);
0246
0247 if (prev_token != T_EOL && prev_token != T_HELPTEXT)
0248 fprintf(stderr, "%s:%d:warning: no new line at end of file\n",
0249 current_file->name, yylineno);
0250
0251 if (current_file) {
0252 zconf_endfile();
0253 return T_EOL;
0254 }
0255 fclose(yyin);
0256 yyterminate();
0257 }
0258
0259 %%
0260
0261 /* second stage lexer */
0262 int yylex(void)
0263 {
0264 int token;
0265
0266 repeat:
0267 token = yylex1();
0268
0269 if (prev_token == T_EOL || prev_token == T_HELPTEXT) {
0270 if (token == T_EOL) {
0271 /* Do not pass unneeded T_EOL to the parser. */
0272 goto repeat;
0273 } else {
0274 /*
0275 * For the parser, update file/lineno at the first token
0276 * of each statement. Generally, \n is a statement
0277 * terminator in Kconfig, but it is not always true
0278 * because \n could be escaped by a backslash.
0279 */
0280 current_pos.file = current_file;
0281 current_pos.lineno = yylineno;
0282 }
0283 }
0284
0285 if (prev_prev_token == T_EOL && prev_token == T_WORD &&
0286 (token == T_EQUAL || token == T_COLON_EQUAL || token == T_PLUS_EQUAL))
0287 BEGIN(ASSIGN_VAL);
0288
0289 prev_prev_token = prev_token;
0290 prev_token = token;
0291
0292 return token;
0293 }
0294
0295 static char *expand_token(const char *in, size_t n)
0296 {
0297 char *out;
0298 int c;
0299 char c2;
0300 const char *rest, *end;
0301
0302 new_string();
0303 append_string(in, n);
0304
0305 /* get the whole line because we do not know the end of token. */
0306 while ((c = input()) != EOF) {
0307 if (c == '\n') {
0308 unput(c);
0309 break;
0310 }
0311 c2 = c;
0312 append_string(&c2, 1);
0313 }
0314
0315 rest = text;
0316 out = expand_one_token(&rest);
0317
0318 /* push back unused characters to the input stream */
0319 end = rest + strlen(rest);
0320 while (end > rest)
0321 unput(*--end);
0322
0323 free(text);
0324
0325 return out;
0326 }
0327
0328 static void append_expanded_string(const char *str)
0329 {
0330 const char *end;
0331 char *res;
0332
0333 str++;
0334
0335 res = expand_dollar(&str);
0336
0337 /* push back unused characters to the input stream */
0338 end = str + strlen(str);
0339 while (end > str)
0340 unput(*--end);
0341
0342 append_string(res, strlen(res));
0343
0344 free(res);
0345 }
0346
0347 void zconf_starthelp(void)
0348 {
0349 new_string();
0350 last_ts = first_ts = 0;
0351 BEGIN(HELP);
0352 }
0353
0354 static void zconf_endhelp(void)
0355 {
0356 yylval.string = text;
0357 BEGIN(INITIAL);
0358 }
0359
0360
0361 /*
0362 * Try to open specified file with following names:
0363 * ./name
0364 * $(srctree)/name
0365 * The latter is used when srctree is separate from objtree
0366 * when compiling the kernel.
0367 * Return NULL if file is not found.
0368 */
0369 FILE *zconf_fopen(const char *name)
0370 {
0371 char *env, fullname[PATH_MAX+1];
0372 FILE *f;
0373
0374 f = fopen(name, "r");
0375 if (!f && name != NULL && name[0] != '/') {
0376 env = getenv(SRCTREE);
0377 if (env) {
0378 snprintf(fullname, sizeof(fullname),
0379 "%s/%s", env, name);
0380 f = fopen(fullname, "r");
0381 }
0382 }
0383 return f;
0384 }
0385
0386 void zconf_initscan(const char *name)
0387 {
0388 yyin = zconf_fopen(name);
0389 if (!yyin) {
0390 fprintf(stderr, "can't find file %s\n", name);
0391 exit(1);
0392 }
0393
0394 current_buf = xmalloc(sizeof(*current_buf));
0395 memset(current_buf, 0, sizeof(*current_buf));
0396
0397 current_file = file_lookup(name);
0398 yylineno = 1;
0399 }
0400
0401 void zconf_nextfile(const char *name)
0402 {
0403 struct file *iter;
0404 struct file *file = file_lookup(name);
0405 struct buffer *buf = xmalloc(sizeof(*buf));
0406 memset(buf, 0, sizeof(*buf));
0407
0408 current_buf->state = YY_CURRENT_BUFFER;
0409 yyin = zconf_fopen(file->name);
0410 if (!yyin) {
0411 fprintf(stderr, "%s:%d: can't open file \"%s\"\n",
0412 zconf_curname(), zconf_lineno(), file->name);
0413 exit(1);
0414 }
0415 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
0416 buf->parent = current_buf;
0417 current_buf = buf;
0418
0419 current_file->lineno = yylineno;
0420 file->parent = current_file;
0421
0422 for (iter = current_file; iter; iter = iter->parent) {
0423 if (!strcmp(iter->name, file->name)) {
0424 fprintf(stderr,
0425 "Recursive inclusion detected.\n"
0426 "Inclusion path:\n"
0427 " current file : %s\n", file->name);
0428 iter = file;
0429 do {
0430 iter = iter->parent;
0431 fprintf(stderr, " included from: %s:%d\n",
0432 iter->name, iter->lineno - 1);
0433 } while (strcmp(iter->name, file->name));
0434 exit(1);
0435 }
0436 }
0437
0438 yylineno = 1;
0439 current_file = file;
0440 }
0441
0442 static void zconf_endfile(void)
0443 {
0444 struct buffer *parent;
0445
0446 current_file = current_file->parent;
0447 if (current_file)
0448 yylineno = current_file->lineno;
0449
0450 parent = current_buf->parent;
0451 if (parent) {
0452 fclose(yyin);
0453 yy_delete_buffer(YY_CURRENT_BUFFER);
0454 yy_switch_to_buffer(parent->state);
0455 }
0456 free(current_buf);
0457 current_buf = parent;
0458 }
0459
0460 int zconf_lineno(void)
0461 {
0462 return current_pos.lineno;
0463 }
0464
0465 const char *zconf_curname(void)
0466 {
0467 return current_pos.file ? current_pos.file->name : "<none>";
0468 }