1 /* lexer.c - The scripting lexer. */
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>
23 #include <grub/script_sh.h>
25 #include "grub_script.tab.h"
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
);
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
));
56 param
->state
= GRUB_PARSER_STATE_TEXT
;
57 param
->getline
= getline
;
61 param
->script
= script
;
66 param
->tokenonhold
= 0;
67 param
->was_newline
= 0;
73 grub_script_lexer_ref (struct grub_lexer_param
*state
)
79 grub_script_lexer_deref (struct grub_lexer_param
*state
)
84 /* Start recording all characters passing through the lexer. */
86 grub_script_lexer_record_start (struct grub_lexer_param
*state
)
89 state
->recordlen
= 100;
90 state
->recording
= grub_malloc (state
->recordlen
);
95 grub_script_lexer_record_stop (struct grub_lexer_param
*state
)
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");
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. */
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
)
129 state
->recording
[state
->recordpos
++] = c
;
132 /* Fetch the next character for the lexer. */
134 nextchar (struct grub_lexer_param
*state
)
137 recordchar (state
, *state
->script
);
142 grub_script_yylex (union YYSTYPE
*yylval
, struct grub_parser_param
*parsestate
)
144 grub_parser_state_t newstate
;
148 struct grub_lexer_param
*state
= parsestate
->lexerstate
;
153 if (state
->tokenonhold
)
155 int token
= state
->tokenonhold
;
156 state
->tokenonhold
= 0;
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
)
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';
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
;
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
,
209 grub_free (state
->newscript
);
210 state
->newscript
= 0;
212 grub_dprintf ("scripting", "token=`\\n'\n");
213 state
->tokenonhold
= '\n';
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
226 if (newstate
== GRUB_PARSER_STATE_TEXT
)
229 switch (*state
->script
)
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");
244 state
->state
= newstate
;
247 grub_dprintf ("scripting", "token=` '\n");
257 grub_dprintf ("scripting", "token=`%c'\n", *state
->script
);
260 state
->tokenonhold
= c
;
269 /* XXX: Use a better size. */
270 buffer
= grub_script_malloc (parsestate
, 2048);
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
))
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
)
313 state
->state
= newstate
;
317 /* A string of text was read in. */
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);
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
342 if (! (check_varstate (newstate
)))
344 if (state
->state
== GRUB_PARSER_STATE_VARNAME2
345 || state
->state
== GRUB_PARSER_STATE_QVARNAME2
)
347 state
->state
= newstate
;
354 state
->state
= newstate
;
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
);
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");
372 if (yylval
->arg
== 0)
374 int token
= state
->tokenonhold
;
375 state
->tokenonhold
= 0;
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
;
404 grub_script_yyerror (struct grub_parser_param
*lex
__attribute__ ((unused
)),
407 grub_printf ("%s\n", err
);