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