Arnaud Lacombe | 674eed8 | 2011-06-07 13:34:05 -0400 | [diff] [blame] | 1 | %option nostdinit noyywrap never-interactive full ecs |
Masahiro Yamada | 1849268 | 2018-03-23 02:00:14 +0900 | [diff] [blame] | 2 | %option 8bit nodefault yylineno |
Masahiro Yamada | 979f2b2 | 2018-12-11 20:01:10 +0900 | [diff] [blame^] | 3 | %x ASSIGN_VAL HELP STRING |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | %{ |
| 5 | /* |
| 6 | * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org> |
| 7 | * Released under the terms of the GNU GPL v2.0. |
| 8 | */ |
| 9 | |
Masahiro Yamada | 9ced3bd | 2018-05-28 18:21:49 +0900 | [diff] [blame] | 10 | #include <assert.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <limits.h> |
| 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
| 15 | #include <unistd.h> |
| 16 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include "lkc.h" |
| 18 | |
Masahiro Yamada | cc66bca | 2018-12-11 20:00:49 +0900 | [diff] [blame] | 19 | #define YY_DECL static int yylex1(void) |
| 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #define START_STRSIZE 16 |
| 22 | |
Roman Zippel | a02f057 | 2005-11-08 21:34:53 -0800 | [diff] [blame] | 23 | static struct { |
| 24 | struct file *file; |
| 25 | int lineno; |
| 26 | } current_pos; |
| 27 | |
Masahiro Yamada | 824fa3b | 2018-12-11 20:01:08 +0900 | [diff] [blame] | 28 | static int prev_prev_token = T_EOL; |
Masahiro Yamada | cc66bca | 2018-12-11 20:00:49 +0900 | [diff] [blame] | 29 | static int prev_token = T_EOL; |
Roman Zippel | 7a88488 | 2005-11-08 21:34:51 -0800 | [diff] [blame] | 30 | static char *text; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | static int text_size, text_asize; |
| 32 | |
| 33 | struct buffer { |
Masahiro Yamada | bb66fc6 | 2014-06-10 19:08:13 +0900 | [diff] [blame] | 34 | struct buffer *parent; |
| 35 | YY_BUFFER_STATE state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | struct buffer *current_buf; |
| 39 | |
| 40 | static int last_ts, first_ts; |
| 41 | |
Masahiro Yamada | 104daea | 2018-05-28 18:21:40 +0900 | [diff] [blame] | 42 | static char *expand_token(const char *in, size_t n); |
| 43 | static void append_expanded_string(const char *in); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | static void zconf_endhelp(void); |
Roman Zippel | a02f057 | 2005-11-08 21:34:53 -0800 | [diff] [blame] | 45 | static void zconf_endfile(void); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
Josh Triplett | 6516657 | 2009-10-15 12:13:36 -0700 | [diff] [blame] | 47 | static void new_string(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | { |
Alan Cox | 177acf7 | 2012-11-06 14:32:08 +0000 | [diff] [blame] | 49 | text = xmalloc(START_STRSIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | text_asize = START_STRSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | text_size = 0; |
Roman Zippel | 7a88488 | 2005-11-08 21:34:51 -0800 | [diff] [blame] | 52 | *text = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Josh Triplett | 6516657 | 2009-10-15 12:13:36 -0700 | [diff] [blame] | 55 | static void append_string(const char *str, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | { |
| 57 | int new_size = text_size + size + 1; |
| 58 | if (new_size > text_asize) { |
Roman Zippel | 7a88488 | 2005-11-08 21:34:51 -0800 | [diff] [blame] | 59 | new_size += START_STRSIZE - 1; |
| 60 | new_size &= -START_STRSIZE; |
Masahiro Yamada | d717f24 | 2018-02-09 01:19:07 +0900 | [diff] [blame] | 61 | text = xrealloc(text, new_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | text_asize = new_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | } |
Roman Zippel | 7a88488 | 2005-11-08 21:34:51 -0800 | [diff] [blame] | 64 | memcpy(text + text_size, str, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | text_size += size; |
Roman Zippel | 7a88488 | 2005-11-08 21:34:51 -0800 | [diff] [blame] | 66 | text[text_size] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Josh Triplett | 6516657 | 2009-10-15 12:13:36 -0700 | [diff] [blame] | 69 | static void alloc_string(const char *str, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | { |
Alan Cox | 177acf7 | 2012-11-06 14:32:08 +0000 | [diff] [blame] | 71 | text = xmalloc(size + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | memcpy(text, str, size); |
| 73 | text[size] = 0; |
| 74 | } |
Andreas Ruprecht | c226456 | 2015-07-12 09:41:50 +0200 | [diff] [blame] | 75 | |
| 76 | static void warn_ignored_character(char chr) |
| 77 | { |
| 78 | fprintf(stderr, |
| 79 | "%s:%d:warning: ignoring unsupported character '%c'\n", |
Masahiro Yamada | 77c1c0f | 2018-12-11 20:00:44 +0900 | [diff] [blame] | 80 | current_file->name, yylineno, chr); |
Andreas Ruprecht | c226456 | 2015-07-12 09:41:50 +0200 | [diff] [blame] | 81 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | %} |
| 83 | |
Andreas Ruprecht | c226456 | 2015-07-12 09:41:50 +0200 | [diff] [blame] | 84 | n [A-Za-z0-9_-] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
| 86 | %% |
| 87 | int str = 0; |
| 88 | int ts, i; |
| 89 | |
Masahiro Yamada | 979f2b2 | 2018-12-11 20:01:10 +0900 | [diff] [blame^] | 90 | #.* /* ignore comment */ |
| 91 | [ \t]* /* whitespaces */ |
| 92 | \\\n /* escaped new line */ |
| 93 | \n return T_EOL; |
| 94 | "allnoconfig_y" return T_ALLNOCONFIG_Y; |
| 95 | "bool" return T_BOOL; |
| 96 | "choice" return T_CHOICE; |
| 97 | "comment" return T_COMMENT; |
| 98 | "config" return T_CONFIG; |
| 99 | "def_bool" return T_DEF_BOOL; |
| 100 | "def_tristate" return T_DEF_TRISTATE; |
| 101 | "default" return T_DEFAULT; |
| 102 | "defconfig_list" return T_DEFCONFIG_LIST; |
| 103 | "depends" return T_DEPENDS; |
| 104 | "endchoice" return T_ENDCHOICE; |
| 105 | "endif" return T_ENDIF; |
| 106 | "endmenu" return T_ENDMENU; |
| 107 | "help"|"---help---" return T_HELP; |
| 108 | "hex" return T_HEX; |
| 109 | "if" return T_IF; |
| 110 | "imply" return T_IMPLY; |
| 111 | "int" return T_INT; |
| 112 | "mainmenu" return T_MAINMENU; |
| 113 | "menu" return T_MENU; |
| 114 | "menuconfig" return T_MENUCONFIG; |
| 115 | "modules" return T_MODULES; |
| 116 | "on" return T_ON; |
| 117 | "option" return T_OPTION; |
| 118 | "optional" return T_OPTIONAL; |
| 119 | "prompt" return T_PROMPT; |
| 120 | "range" return T_RANGE; |
| 121 | "select" return T_SELECT; |
| 122 | "source" return T_SOURCE; |
| 123 | "string" return T_STRING; |
| 124 | "tristate" return T_TRISTATE; |
| 125 | "visible" return T_VISIBLE; |
| 126 | "||" return T_OR; |
| 127 | "&&" return T_AND; |
| 128 | "=" return T_EQUAL; |
| 129 | "!=" return T_UNEQUAL; |
| 130 | "<" return T_LESS; |
| 131 | "<=" return T_LESS_EQUAL; |
| 132 | ">" return T_GREATER; |
| 133 | ">=" return T_GREATER_EQUAL; |
| 134 | "!" return T_NOT; |
| 135 | "(" return T_OPEN_PAREN; |
| 136 | ")" return T_CLOSE_PAREN; |
| 137 | ":=" return T_COLON_EQUAL; |
| 138 | "+=" return T_PLUS_EQUAL; |
| 139 | \"|\' { |
| 140 | str = yytext[0]; |
| 141 | new_string(); |
| 142 | BEGIN(STRING); |
| 143 | } |
| 144 | {n}+ { |
| 145 | alloc_string(yytext, yyleng); |
| 146 | yylval.string = text; |
| 147 | return T_WORD; |
| 148 | } |
| 149 | ({n}|$)+ { |
| 150 | /* this token includes at least one '$' */ |
| 151 | yylval.string = expand_token(yytext, yyleng); |
| 152 | if (strlen(yylval.string)) |
| 153 | return T_WORD; |
| 154 | free(yylval.string); |
| 155 | } |
| 156 | . warn_ignored_character(*yytext); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
Masahiro Yamada | 9ced3bd | 2018-05-28 18:21:49 +0900 | [diff] [blame] | 158 | <ASSIGN_VAL>{ |
| 159 | [^[:blank:]\n]+.* { |
| 160 | alloc_string(yytext, yyleng); |
| 161 | yylval.string = text; |
| 162 | return T_ASSIGN_VAL; |
| 163 | } |
| 164 | \n { BEGIN(INITIAL); return T_EOL; } |
| 165 | . |
| 166 | } |
| 167 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | <STRING>{ |
Masahiro Yamada | 104daea | 2018-05-28 18:21:40 +0900 | [diff] [blame] | 169 | "$".* append_expanded_string(yytext); |
Masahiro Yamada | 104daea | 2018-05-28 18:21:40 +0900 | [diff] [blame] | 170 | [^$'"\\\n]+ { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | append_string(yytext, yyleng); |
| 172 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | \\.? { |
| 174 | append_string(yytext + 1, yyleng - 1); |
| 175 | } |
| 176 | \'|\" { |
| 177 | if (str == yytext[0]) { |
Masahiro Yamada | 979f2b2 | 2018-12-11 20:01:10 +0900 | [diff] [blame^] | 178 | BEGIN(INITIAL); |
Masahiro Yamada | 765f4cd | 2018-01-12 00:50:50 +0900 | [diff] [blame] | 179 | yylval.string = text; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | return T_WORD_QUOTE; |
| 181 | } else |
| 182 | append_string(yytext, 1); |
| 183 | } |
| 184 | \n { |
Masahiro Yamada | 9e3e10c | 2018-02-06 09:34:41 +0900 | [diff] [blame] | 185 | fprintf(stderr, |
| 186 | "%s:%d:warning: multi-line strings not supported\n", |
| 187 | zconf_curname(), zconf_lineno()); |
Masahiro Yamada | 21c5ecf | 2018-12-11 20:00:48 +0900 | [diff] [blame] | 188 | unput('\n'); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | BEGIN(INITIAL); |
Masahiro Yamada | 21c5ecf | 2018-12-11 20:00:48 +0900 | [diff] [blame] | 190 | yylval.string = text; |
| 191 | return T_WORD_QUOTE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | } |
| 193 | <<EOF>> { |
| 194 | BEGIN(INITIAL); |
Masahiro Yamada | fbac597 | 2018-12-11 20:00:45 +0900 | [diff] [blame] | 195 | yylval.string = text; |
| 196 | return T_WORD_QUOTE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
| 200 | <HELP>{ |
| 201 | [ \t]+ { |
| 202 | ts = 0; |
| 203 | for (i = 0; i < yyleng; i++) { |
| 204 | if (yytext[i] == '\t') |
| 205 | ts = (ts & ~7) + 8; |
| 206 | else |
| 207 | ts++; |
| 208 | } |
| 209 | last_ts = ts; |
| 210 | if (first_ts) { |
| 211 | if (ts < first_ts) { |
| 212 | zconf_endhelp(); |
| 213 | return T_HELPTEXT; |
| 214 | } |
| 215 | ts -= first_ts; |
| 216 | while (ts > 8) { |
| 217 | append_string(" ", 8); |
| 218 | ts -= 8; |
| 219 | } |
| 220 | append_string(" ", ts); |
| 221 | } |
| 222 | } |
| 223 | [ \t]*\n/[^ \t\n] { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | zconf_endhelp(); |
| 225 | return T_HELPTEXT; |
| 226 | } |
| 227 | [ \t]*\n { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | append_string("\n", 1); |
| 229 | } |
| 230 | [^ \t\n].* { |
EGRY Gabor | f7a4b4c | 2008-01-11 23:55:20 +0100 | [diff] [blame] | 231 | while (yyleng) { |
| 232 | if ((yytext[yyleng-1] != ' ') && (yytext[yyleng-1] != '\t')) |
| 233 | break; |
| 234 | yyleng--; |
| 235 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | append_string(yytext, yyleng); |
| 237 | if (!first_ts) |
| 238 | first_ts = last_ts; |
| 239 | } |
| 240 | <<EOF>> { |
| 241 | zconf_endhelp(); |
| 242 | return T_HELPTEXT; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | <<EOF>> { |
Masahiro Yamada | 0bcc547 | 2018-12-11 20:00:50 +0900 | [diff] [blame] | 247 | BEGIN(INITIAL); |
| 248 | |
Masahiro Yamada | 7236793 | 2018-12-11 20:00:51 +0900 | [diff] [blame] | 249 | if (prev_token != T_EOL && prev_token != T_HELPTEXT) |
| 250 | fprintf(stderr, "%s:%d:warning: no new line at end of file\n", |
| 251 | current_file->name, yylineno); |
| 252 | |
Roman Zippel | a02f057 | 2005-11-08 21:34:53 -0800 | [diff] [blame] | 253 | if (current_file) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | zconf_endfile(); |
Roman Zippel | a02f057 | 2005-11-08 21:34:53 -0800 | [diff] [blame] | 255 | return T_EOL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | } |
| 257 | fclose(yyin); |
| 258 | yyterminate(); |
| 259 | } |
| 260 | |
| 261 | %% |
Masahiro Yamada | cc66bca | 2018-12-11 20:00:49 +0900 | [diff] [blame] | 262 | |
| 263 | /* second stage lexer */ |
| 264 | int yylex(void) |
| 265 | { |
| 266 | int token; |
| 267 | |
| 268 | repeat: |
| 269 | token = yylex1(); |
| 270 | |
Masahiro Yamada | 4b31a32 | 2018-12-11 20:01:09 +0900 | [diff] [blame] | 271 | if (prev_token == T_EOL || prev_token == T_HELPTEXT) { |
| 272 | if (token == T_EOL) { |
| 273 | /* Do not pass unneeded T_EOL to the parser. */ |
| 274 | goto repeat; |
| 275 | } else { |
| 276 | /* |
| 277 | * For the parser, update file/lineno at the first token |
| 278 | * of each statement. Generally, \n is a statement |
| 279 | * terminator in Kconfig, but it is not always true |
| 280 | * because \n could be escaped by a backslash. |
| 281 | */ |
| 282 | current_pos.file = current_file; |
| 283 | current_pos.lineno = yylineno; |
| 284 | } |
| 285 | } |
Masahiro Yamada | cc66bca | 2018-12-11 20:00:49 +0900 | [diff] [blame] | 286 | |
Masahiro Yamada | 824fa3b | 2018-12-11 20:01:08 +0900 | [diff] [blame] | 287 | if (prev_prev_token == T_EOL && prev_token == T_WORD && |
| 288 | (token == T_EQUAL || token == T_COLON_EQUAL || token == T_PLUS_EQUAL)) |
| 289 | BEGIN(ASSIGN_VAL); |
| 290 | |
| 291 | prev_prev_token = prev_token; |
Masahiro Yamada | cc66bca | 2018-12-11 20:00:49 +0900 | [diff] [blame] | 292 | prev_token = token; |
| 293 | |
| 294 | return token; |
| 295 | } |
| 296 | |
Masahiro Yamada | 104daea | 2018-05-28 18:21:40 +0900 | [diff] [blame] | 297 | static char *expand_token(const char *in, size_t n) |
| 298 | { |
| 299 | char *out; |
| 300 | int c; |
| 301 | char c2; |
| 302 | const char *rest, *end; |
| 303 | |
| 304 | new_string(); |
| 305 | append_string(in, n); |
| 306 | |
| 307 | /* get the whole line because we do not know the end of token. */ |
| 308 | while ((c = input()) != EOF) { |
| 309 | if (c == '\n') { |
| 310 | unput(c); |
| 311 | break; |
| 312 | } |
| 313 | c2 = c; |
| 314 | append_string(&c2, 1); |
| 315 | } |
| 316 | |
| 317 | rest = text; |
| 318 | out = expand_one_token(&rest); |
| 319 | |
| 320 | /* push back unused characters to the input stream */ |
| 321 | end = rest + strlen(rest); |
| 322 | while (end > rest) |
| 323 | unput(*--end); |
| 324 | |
| 325 | free(text); |
| 326 | |
| 327 | return out; |
| 328 | } |
| 329 | |
| 330 | static void append_expanded_string(const char *str) |
| 331 | { |
| 332 | const char *end; |
| 333 | char *res; |
| 334 | |
| 335 | str++; |
| 336 | |
| 337 | res = expand_dollar(&str); |
| 338 | |
| 339 | /* push back unused characters to the input stream */ |
| 340 | end = str + strlen(str); |
| 341 | while (end > str) |
| 342 | unput(*--end); |
| 343 | |
| 344 | append_string(res, strlen(res)); |
| 345 | |
| 346 | free(res); |
| 347 | } |
| 348 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | void zconf_starthelp(void) |
| 350 | { |
| 351 | new_string(); |
| 352 | last_ts = first_ts = 0; |
| 353 | BEGIN(HELP); |
| 354 | } |
| 355 | |
| 356 | static void zconf_endhelp(void) |
| 357 | { |
Masahiro Yamada | 765f4cd | 2018-01-12 00:50:50 +0900 | [diff] [blame] | 358 | yylval.string = text; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | BEGIN(INITIAL); |
| 360 | } |
| 361 | |
| 362 | |
| 363 | /* |
| 364 | * Try to open specified file with following names: |
| 365 | * ./name |
| 366 | * $(srctree)/name |
| 367 | * The latter is used when srctree is separate from objtree |
| 368 | * when compiling the kernel. |
| 369 | * Return NULL if file is not found. |
| 370 | */ |
| 371 | FILE *zconf_fopen(const char *name) |
| 372 | { |
| 373 | char *env, fullname[PATH_MAX+1]; |
| 374 | FILE *f; |
| 375 | |
| 376 | f = fopen(name, "r"); |
Marcin Garski | 11de39e | 2007-05-05 22:49:00 +0200 | [diff] [blame] | 377 | if (!f && name != NULL && name[0] != '/') { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | env = getenv(SRCTREE); |
| 379 | if (env) { |
| 380 | sprintf(fullname, "%s/%s", env, name); |
| 381 | f = fopen(fullname, "r"); |
| 382 | } |
| 383 | } |
| 384 | return f; |
| 385 | } |
| 386 | |
| 387 | void zconf_initscan(const char *name) |
| 388 | { |
| 389 | yyin = zconf_fopen(name); |
| 390 | if (!yyin) { |
Masahiro Yamada | 9e3e10c | 2018-02-06 09:34:41 +0900 | [diff] [blame] | 391 | fprintf(stderr, "can't find file %s\n", name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | exit(1); |
| 393 | } |
| 394 | |
Alan Cox | 177acf7 | 2012-11-06 14:32:08 +0000 | [diff] [blame] | 395 | current_buf = xmalloc(sizeof(*current_buf)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | memset(current_buf, 0, sizeof(*current_buf)); |
| 397 | |
| 398 | current_file = file_lookup(name); |
Masahiro Yamada | 1849268 | 2018-03-23 02:00:14 +0900 | [diff] [blame] | 399 | yylineno = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | void zconf_nextfile(const char *name) |
| 403 | { |
Yann E. MORIN | f094f8a | 2011-02-24 19:36:42 +0100 | [diff] [blame] | 404 | struct file *iter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | struct file *file = file_lookup(name); |
Alan Cox | 177acf7 | 2012-11-06 14:32:08 +0000 | [diff] [blame] | 406 | struct buffer *buf = xmalloc(sizeof(*buf)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | memset(buf, 0, sizeof(*buf)); |
| 408 | |
| 409 | current_buf->state = YY_CURRENT_BUFFER; |
Arnaud Lacombe | e82dae9 | 2010-09-04 16:09:26 -0400 | [diff] [blame] | 410 | yyin = zconf_fopen(file->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | if (!yyin) { |
Masahiro Yamada | 9e3e10c | 2018-02-06 09:34:41 +0900 | [diff] [blame] | 412 | fprintf(stderr, "%s:%d: can't open file \"%s\"\n", |
| 413 | zconf_curname(), zconf_lineno(), file->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | exit(1); |
| 415 | } |
| 416 | yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE)); |
| 417 | buf->parent = current_buf; |
| 418 | current_buf = buf; |
| 419 | |
Masahiro Yamada | 1849268 | 2018-03-23 02:00:14 +0900 | [diff] [blame] | 420 | current_file->lineno = yylineno; |
Masahiro Yamada | 379a8eb | 2018-03-23 02:00:13 +0900 | [diff] [blame] | 421 | file->parent = current_file; |
| 422 | |
| 423 | for (iter = current_file; iter; iter = iter->parent) { |
| 424 | if (!strcmp(iter->name, file->name)) { |
Masahiro Yamada | 9e3e10c | 2018-02-06 09:34:41 +0900 | [diff] [blame] | 425 | fprintf(stderr, |
Masahiro Yamada | 32a94b8 | 2018-03-23 02:00:12 +0900 | [diff] [blame] | 426 | "Recursive inclusion detected.\n" |
| 427 | "Inclusion path:\n" |
Masahiro Yamada | 379a8eb | 2018-03-23 02:00:13 +0900 | [diff] [blame] | 428 | " current file : %s\n", file->name); |
| 429 | iter = file; |
Masahiro Yamada | 5ae6fcc | 2018-03-02 16:05:12 +0900 | [diff] [blame] | 430 | do { |
Yann E. MORIN | f094f8a | 2011-02-24 19:36:42 +0100 | [diff] [blame] | 431 | iter = iter->parent; |
Masahiro Yamada | 32a94b8 | 2018-03-23 02:00:12 +0900 | [diff] [blame] | 432 | fprintf(stderr, " included from: %s:%d\n", |
Masahiro Yamada | 5ae6fcc | 2018-03-02 16:05:12 +0900 | [diff] [blame] | 433 | iter->name, iter->lineno - 1); |
Masahiro Yamada | 379a8eb | 2018-03-23 02:00:13 +0900 | [diff] [blame] | 434 | } while (strcmp(iter->name, file->name)); |
Yann E. MORIN | f094f8a | 2011-02-24 19:36:42 +0100 | [diff] [blame] | 435 | exit(1); |
| 436 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | } |
Masahiro Yamada | 379a8eb | 2018-03-23 02:00:13 +0900 | [diff] [blame] | 438 | |
Masahiro Yamada | 1849268 | 2018-03-23 02:00:14 +0900 | [diff] [blame] | 439 | yylineno = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | current_file = file; |
| 441 | } |
| 442 | |
Roman Zippel | a02f057 | 2005-11-08 21:34:53 -0800 | [diff] [blame] | 443 | static void zconf_endfile(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | { |
| 445 | struct buffer *parent; |
| 446 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | current_file = current_file->parent; |
Masahiro Yamada | 1849268 | 2018-03-23 02:00:14 +0900 | [diff] [blame] | 448 | if (current_file) |
| 449 | yylineno = current_file->lineno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | |
| 451 | parent = current_buf->parent; |
| 452 | if (parent) { |
| 453 | fclose(yyin); |
| 454 | yy_delete_buffer(YY_CURRENT_BUFFER); |
| 455 | yy_switch_to_buffer(parent->state); |
| 456 | } |
| 457 | free(current_buf); |
| 458 | current_buf = parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | int zconf_lineno(void) |
| 462 | { |
Roman Zippel | a02f057 | 2005-11-08 21:34:53 -0800 | [diff] [blame] | 463 | return current_pos.lineno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Arnaud Lacombe | 2e7a091 | 2010-09-04 16:03:30 -0400 | [diff] [blame] | 466 | const char *zconf_curname(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | { |
Roman Zippel | a02f057 | 2005-11-08 21:34:53 -0800 | [diff] [blame] | 468 | return current_pos.file ? current_pos.file->name : "<none>"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | } |