1 /* lexer.c - The scripting lexer. */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2005,2006,2007 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.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_QUOTE
43 || state
== GRUB_PARSER_STATE_DQUOTE
);
46 struct grub_lexer_param
*
47 grub_script_lexer_init (char *script
, grub_err_t (*getline
) (char **))
49 struct grub_lexer_param
*param
;
51 param
= grub_malloc (sizeof (*param
));
55 param
->state
= GRUB_PARSER_STATE_TEXT
;
56 param
->getline
= getline
;
60 param
->script
= script
;
70 grub_script_lexer_ref (struct grub_lexer_param
*state
)
76 grub_script_lexer_deref (struct grub_lexer_param
*state
)
81 /* Start recording all characters passing through the lexer. */
83 grub_script_lexer_record_start (struct grub_lexer_param
*state
)
86 state
->recordlen
= 100;
87 state
->recording
= grub_malloc (state
->recordlen
);
92 grub_script_lexer_record_stop (struct grub_lexer_param
*state
)
96 /* Delete the last character, it is a `}'. */
97 if (state
->recordpos
> 0)
99 if (state
->recording
[--state
->recordpos
] != '}')
101 grub_printf ("Internal error while parsing menu entry");
104 state
->recording
[state
->recordpos
] = '\0';
107 return state
->recording
;
110 /* When recording is enabled, record the character C as the next item
111 in the character stream. */
113 recordchar (struct grub_lexer_param
*state
, char c
)
115 if (state
->recordpos
== state
->recordlen
)
117 char *old
= state
->recording
;
118 state
->recordlen
+= 100;
119 state
->recording
= grub_realloc (state
->recording
, state
->recordlen
);
120 if (! state
->recording
)
126 state
->recording
[state
->recordpos
++] = c
;
129 /* Fetch the next character for the lexer. */
131 nextchar (struct grub_lexer_param
*state
)
134 recordchar (state
, *state
->script
);
139 grub_script_yylex2 (YYSTYPE
*yylval
, struct grub_parser_param
*parsestate
);
142 grub_script_yylex (YYSTYPE
*yylval
, struct grub_parser_param
*parsestate
)
148 r
= grub_script_yylex2 (yylval
, parsestate
);
156 grub_script_yylex2 (YYSTYPE
*yylval
, struct grub_parser_param
*parsestate
)
158 grub_parser_state_t newstate
;
162 struct grub_lexer_param
*state
= parsestate
->lexerstate
;
167 if (! *state
->script
)
169 /* Check if more tokens are requested by the parser. */
171 || state
->state
== GRUB_PARSER_STATE_ESC
)
174 while (!state
->script
|| ! grub_strlen (state
->script
))
176 grub_free (state
->newscript
);
177 state
->newscript
= 0;
178 state
->getline (&state
->newscript
);
179 state
->script
= state
->newscript
;
183 grub_dprintf ("scripting", "token=`\\n'\n");
184 recordchar (state
, '\n');
185 if (state
->state
!= GRUB_PARSER_STATE_ESC
)
190 grub_free (state
->newscript
);
191 state
->newscript
= 0;
193 grub_dprintf ("scripting", "token=`\\n'\n");
198 newstate
= grub_parser_cmdline_state (state
->state
, *state
->script
, &use
);
200 /* Check if it is a text. */
201 if (check_textstate (newstate
))
203 /* In case the string is not quoted, this can be a one char
205 if (newstate
== GRUB_PARSER_STATE_TEXT
)
207 switch (*state
->script
)
210 while (*state
->script
)
212 newstate
= grub_parser_cmdline_state (state
->state
,
213 *state
->script
, &use
);
214 if (! (state
->state
== GRUB_PARSER_STATE_TEXT
215 && *state
->script
== ' '))
217 grub_dprintf ("scripting", "token=` '\n");
220 state
->state
= newstate
;
223 grub_dprintf ("scripting", "token=` '\n");
231 grub_dprintf ("scripting", "token=`%c'\n", *state
->script
);
239 /* XXX: Use a better size. */
240 buffer
= grub_script_malloc (parsestate
, 2048);
246 /* Read one token, possible quoted. */
247 while (*state
->script
)
249 newstate
= grub_parser_cmdline_state (state
->state
,
250 *state
->script
, &use
);
252 /* Check if a variable name starts. */
253 if (check_varstate (newstate
))
256 /* If the string is not quoted or escaped, stop processing
257 when a special token was found. It will be recognized
258 next time when this function is called. */
259 if (newstate
== GRUB_PARSER_STATE_TEXT
260 && state
->state
!= GRUB_PARSER_STATE_ESC
)
280 state
->state
= newstate
;
284 /* A string of text was read in. */
286 grub_dprintf ("scripting", "token=`%s'\n", buffer
);
287 yylval
->string
= buffer
;
289 /* Detect some special tokens. */
290 if (! grub_strcmp (buffer
, "while"))
291 return GRUB_PARSER_TOKEN_WHILE
;
292 else if (! grub_strcmp (buffer
, "if"))
293 return GRUB_PARSER_TOKEN_IF
;
294 else if (! grub_strcmp (buffer
, "function"))
295 return GRUB_PARSER_TOKEN_FUNCTION
;
296 else if (! grub_strcmp (buffer
, "menuentry"))
297 return GRUB_PARSER_TOKEN_MENUENTRY
;
298 else if (! grub_strcmp (buffer
, "@"))
299 return GRUB_PARSER_TOKEN_MENUENTRY
;
300 else if (! grub_strcmp (buffer
, "else"))
301 return GRUB_PARSER_TOKEN_ELSE
;
302 else if (! grub_strcmp (buffer
, "then"))
303 return GRUB_PARSER_TOKEN_THEN
;
304 else if (! grub_strcmp (buffer
, "fi"))
305 return GRUB_PARSER_TOKEN_FI
;
307 return GRUB_PARSER_TOKEN_NAME
;
309 else if (newstate
== GRUB_PARSER_STATE_VAR
310 || newstate
== GRUB_PARSER_STATE_QVAR
)
312 /* XXX: Use a better size. */
313 buffer
= grub_script_malloc (parsestate
, 2096);
319 /* This is a variable, read the variable name. */
320 while (*state
->script
)
322 newstate
= grub_parser_cmdline_state (state
->state
,
323 *state
->script
, &use
);
325 /* Check if this character is not part of the variable name
327 if (! (check_varstate (newstate
)))
329 if (state
->state
== GRUB_PARSER_STATE_VARNAME2
330 || state
->state
== GRUB_PARSER_STATE_QVARNAME2
)
332 state
->state
= newstate
;
339 state
->state
= newstate
;
343 state
->state
= newstate
;
344 yylval
->string
= buffer
;
345 grub_dprintf ("scripting", "vartoken=`%s'\n", buffer
);
347 return GRUB_PARSER_TOKEN_VAR
;
351 /* There is either text or a variable name. In the case you
352 arrive here there is a serious problem with the lexer. */
353 grub_error (GRUB_ERR_BAD_ARGUMENT
, "Internal error\n");
359 grub_script_yyerror (struct grub_parser_param
*lex
__attribute__ ((unused
)),
362 grub_printf ("%s\n", err
);