Fix hang/crash when requesting ZPOOL with incorrect backing device
[grub2/phcoder.git] / script / sh / lexer.c
blob17f18e2032f2b2c69aa4261894fa65f9f3b4262b
1 /* lexer.c - The scripting lexer. */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2005,2006,2007,2008,2009 Free Software Foundation, Inc.
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
20 #include <grub/parser.h>
21 #include <grub/misc.h>
22 #include <grub/mm.h>
23 #include <grub/script_sh.h>
25 #include "grub_script.tab.h"
27 static int
28 check_varstate (grub_parser_state_t state)
30 return (state == GRUB_PARSER_STATE_VARNAME
31 || state == GRUB_PARSER_STATE_VAR
32 || state == GRUB_PARSER_STATE_QVAR
33 || state == GRUB_PARSER_STATE_VARNAME2
34 || state == GRUB_PARSER_STATE_QVARNAME
35 || state == GRUB_PARSER_STATE_QVARNAME2);
38 static int
39 check_textstate (grub_parser_state_t state)
41 return (state == GRUB_PARSER_STATE_TEXT
42 || state == GRUB_PARSER_STATE_ESC
43 || state == GRUB_PARSER_STATE_QUOTE
44 || state == GRUB_PARSER_STATE_DQUOTE);
47 struct grub_lexer_param *
48 grub_script_lexer_init (char *script, grub_reader_getline_t getline)
50 struct grub_lexer_param *param;
52 param = grub_malloc (sizeof (*param));
53 if (! param)
54 return 0;
56 param->state = GRUB_PARSER_STATE_TEXT;
57 param->getline = getline;
58 param->refs = 0;
59 param->done = 0;
60 param->newscript = 0;
61 param->script = script;
62 param->record = 0;
63 param->recording = 0;
64 param->recordpos = 0;
65 param->recordlen = 0;
66 param->tokenonhold = 0;
67 param->was_newline = 0;
69 return param;
72 void
73 grub_script_lexer_ref (struct grub_lexer_param *state)
75 state->refs++;
78 void
79 grub_script_lexer_deref (struct grub_lexer_param *state)
81 state->refs--;
84 /* Start recording all characters passing through the lexer. */
85 void
86 grub_script_lexer_record_start (struct grub_lexer_param *state)
88 state->record = 1;
89 state->recordlen = 100;
90 state->recording = grub_malloc (state->recordlen);
91 state->recordpos = 0;
94 char *
95 grub_script_lexer_record_stop (struct grub_lexer_param *state)
97 state->record = 0;
99 /* Delete the last character, it is a `}'. */
100 if (state->recordpos > 0)
102 if (state->recording[--state->recordpos] != '}')
104 grub_printf ("Internal error while parsing menu entry");
105 for (;;); /* XXX */
107 state->recording[state->recordpos] = '\0';
110 return state->recording;
113 /* When recording is enabled, record the character C as the next item
114 in the character stream. */
115 static void
116 recordchar (struct grub_lexer_param *state, char c)
118 if (state->recordpos == state->recordlen)
120 char *old = state->recording;
121 state->recordlen += 100;
122 state->recording = grub_realloc (state->recording, state->recordlen);
123 if (! state->recording)
125 grub_free (old);
126 state->record = 0;
129 state->recording[state->recordpos++] = c;
132 /* Fetch the next character for the lexer. */
133 static void
134 nextchar (struct grub_lexer_param *state)
136 if (state->record)
137 recordchar (state, *state->script);
138 state->script++;
142 grub_script_yylex (union YYSTYPE *yylval, struct grub_parser_param *parsestate)
144 grub_parser_state_t newstate;
145 char use;
146 char *buffer;
147 char *bp;
148 struct grub_lexer_param *state = parsestate->lexerstate;
149 int firstrun = 1;
151 yylval->arg = 0;
153 if (state->tokenonhold)
155 int token = state->tokenonhold;
156 state->tokenonhold = 0;
157 return token;
160 for (;! state->done; firstrun = 0)
162 if (! state->script || ! *state->script)
164 /* Check if more tokens are requested by the parser. */
165 if (((state->refs && ! parsestate->err)
166 || state->state == GRUB_PARSER_STATE_ESC
167 || state->state == GRUB_PARSER_STATE_QUOTE
168 || state->state == GRUB_PARSER_STATE_DQUOTE)
169 && state->getline)
171 int doexit = 0;
172 if (state->state != GRUB_PARSER_STATE_ESC
173 && state->state != GRUB_PARSER_STATE_QUOTE
174 && state->state != GRUB_PARSER_STATE_DQUOTE
175 && ! state->was_newline)
177 state->was_newline = 1;
178 state->tokenonhold = '\n';
179 break;
181 while (! state->script || ! *state->script)
183 grub_free (state->newscript);
184 state->newscript = 0;
185 state->getline (&state->newscript, 1);
186 state->script = state->newscript;
187 if (! state->script)
189 doexit = 1;
190 break;
193 if (doexit)
194 break;
195 grub_dprintf ("scripting", "token=`\\n'\n");
196 recordchar (state, '\n');
197 if (state->state == GRUB_PARSER_STATE_VARNAME)
198 state->state = GRUB_PARSER_STATE_TEXT;
199 if (state->state == GRUB_PARSER_STATE_QVARNAME)
200 state->state = GRUB_PARSER_STATE_DQUOTE;
201 if (state->state == GRUB_PARSER_STATE_DQUOTE
202 || state->state == GRUB_PARSER_STATE_QUOTE)
203 yylval->arg = grub_script_arg_add (parsestate, yylval->arg,
204 GRUB_SCRIPT_ARG_TYPE_STR,
205 "\n");
207 else
209 grub_free (state->newscript);
210 state->newscript = 0;
211 state->done = 1;
212 grub_dprintf ("scripting", "token=`\\n'\n");
213 state->tokenonhold = '\n';
214 break;
217 state->was_newline = 0;
219 newstate = grub_parser_cmdline_state (state->state, *state->script, &use);
221 /* Check if it is a text. */
222 if (check_textstate (newstate))
224 /* In case the string is not quoted, this can be a one char
225 length symbol. */
226 if (newstate == GRUB_PARSER_STATE_TEXT)
228 int doexit = 0;
229 switch (*state->script)
231 case ' ':
232 while (*state->script)
234 newstate = grub_parser_cmdline_state (state->state,
235 *state->script, &use);
236 if (! (state->state == GRUB_PARSER_STATE_TEXT
237 && *state->script == ' '))
239 grub_dprintf ("scripting", "token=` '\n");
240 if (! firstrun)
241 doexit = 1;
242 break;
244 state->state = newstate;
245 nextchar (state);
247 grub_dprintf ("scripting", "token=` '\n");
248 if (! firstrun)
249 doexit = 1;
250 break;
251 case '{':
252 case '}':
253 case ';':
254 case '\n':
256 char c;
257 grub_dprintf ("scripting", "token=`%c'\n", *state->script);
258 c = *state->script;;
259 nextchar (state);
260 state->tokenonhold = c;
261 doexit = 1;
262 break;
265 if (doexit)
266 break;
269 /* XXX: Use a better size. */
270 buffer = grub_script_malloc (parsestate, 2048);
271 if (! buffer)
272 return 0;
274 bp = buffer;
276 /* Read one token, possible quoted. */
277 while (*state->script)
279 newstate = grub_parser_cmdline_state (state->state,
280 *state->script, &use);
282 /* Check if a variable name starts. */
283 if (check_varstate (newstate))
284 break;
286 /* If the string is not quoted or escaped, stop processing
287 when a special token was found. It will be recognized
288 next time when this function is called. */
289 if (newstate == GRUB_PARSER_STATE_TEXT
290 && state->state != GRUB_PARSER_STATE_ESC
291 && state->state != GRUB_PARSER_STATE_QUOTE
292 && state->state != GRUB_PARSER_STATE_DQUOTE)
294 int breakout = 0;
296 switch (use)
298 case ' ':
299 case '{':
300 case '}':
301 case ';':
302 case '\n':
303 breakout = 1;
305 if (breakout)
306 break;
307 if (use)
308 *(bp++) = use;
310 else if (use)
311 *(bp++) = use;
313 state->state = newstate;
314 nextchar (state);
317 /* A string of text was read in. */
318 *bp = '\0';
319 grub_dprintf ("scripting", "token=`%s'\n", buffer);
320 yylval->arg = grub_script_arg_add (parsestate, yylval->arg,
321 GRUB_SCRIPT_ARG_TYPE_STR, buffer);
324 else if (newstate == GRUB_PARSER_STATE_VAR
325 || newstate == GRUB_PARSER_STATE_QVAR)
327 /* XXX: Use a better size. */
328 buffer = grub_script_malloc (parsestate, 2096);
329 if (! buffer)
330 return 0;
332 bp = buffer;
334 /* This is a variable, read the variable name. */
335 while (*state->script)
337 newstate = grub_parser_cmdline_state (state->state,
338 *state->script, &use);
340 /* Check if this character is not part of the variable name
341 anymore. */
342 if (! (check_varstate (newstate)))
344 if (state->state == GRUB_PARSER_STATE_VARNAME2
345 || state->state == GRUB_PARSER_STATE_QVARNAME2)
346 nextchar (state);
347 state->state = newstate;
348 break;
351 if (use)
352 *(bp++) = use;
353 nextchar (state);
354 state->state = newstate;
357 *bp = '\0';
358 state->state = newstate;
359 yylval->arg = grub_script_arg_add (parsestate, yylval->arg,
360 GRUB_SCRIPT_ARG_TYPE_VAR, buffer);
361 grub_dprintf ("scripting", "vartoken=`%s'\n", buffer);
363 else
365 /* There is either text or a variable name. In the case you
366 arrive here there is a serious problem with the lexer. */
367 grub_error (GRUB_ERR_BAD_ARGUMENT, "Internal error\n");
368 return 0;
372 if (yylval->arg == 0)
374 int token = state->tokenonhold;
375 state->tokenonhold = 0;
376 return token;
379 if (yylval->arg->next == 0 && yylval->arg->type == GRUB_SCRIPT_ARG_TYPE_STR)
381 /* Detect some special tokens. */
382 if (! grub_strcmp (yylval->arg->str, "while"))
383 return GRUB_PARSER_TOKEN_WHILE;
384 else if (! grub_strcmp (yylval->arg->str, "if"))
385 return GRUB_PARSER_TOKEN_IF;
386 else if (! grub_strcmp (yylval->arg->str, "function"))
387 return GRUB_PARSER_TOKEN_FUNCTION;
388 else if (! grub_strcmp (yylval->arg->str, "menuentry"))
389 return GRUB_PARSER_TOKEN_MENUENTRY;
390 else if (! grub_strcmp (yylval->arg->str, "@"))
391 return GRUB_PARSER_TOKEN_MENUENTRY;
392 else if (! grub_strcmp (yylval->arg->str, "else"))
393 return GRUB_PARSER_TOKEN_ELSE;
394 else if (! grub_strcmp (yylval->arg->str, "then"))
395 return GRUB_PARSER_TOKEN_THEN;
396 else if (! grub_strcmp (yylval->arg->str, "fi"))
397 return GRUB_PARSER_TOKEN_FI;
400 return GRUB_PARSER_TOKEN_ARG;
403 void
404 grub_script_yyerror (struct grub_parser_param *lex __attribute__ ((unused)),
405 char const *err)
407 grub_printf ("%s\n", err);