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