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_QUOTE
43 || state
== GRUB_PARSER_STATE_DQUOTE
);
46 struct grub_lexer_param
*
47 grub_script_lexer_init (char *script
, grub_reader_getline_t getline
)
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
;
65 param
->tokenonhold
= 0;
71 grub_script_lexer_ref (struct grub_lexer_param
*state
)
77 grub_script_lexer_deref (struct grub_lexer_param
*state
)
82 /* Start recording all characters passing through the lexer. */
84 grub_script_lexer_record_start (struct grub_lexer_param
*state
)
87 state
->recordlen
= 100;
88 state
->recording
= grub_malloc (state
->recordlen
);
93 grub_script_lexer_record_stop (struct grub_lexer_param
*state
)
97 /* Delete the last character, it is a `}'. */
98 if (state
->recordpos
> 0)
100 if (state
->recording
[--state
->recordpos
] != '}')
102 grub_printf ("Internal error while parsing menu entry");
105 state
->recording
[state
->recordpos
] = '\0';
108 return state
->recording
;
111 /* When recording is enabled, record the character C as the next item
112 in the character stream. */
114 recordchar (struct grub_lexer_param
*state
, char c
)
116 if (state
->recordpos
== state
->recordlen
)
118 char *old
= state
->recording
;
119 state
->recordlen
+= 100;
120 state
->recording
= grub_realloc (state
->recording
, state
->recordlen
);
121 if (! state
->recording
)
127 state
->recording
[state
->recordpos
++] = c
;
130 /* Fetch the next character for the lexer. */
132 nextchar (struct grub_lexer_param
*state
)
135 recordchar (state
, *state
->script
);
140 grub_script_yylex (union YYSTYPE
*yylval
, struct grub_parser_param
*parsestate
)
142 grub_parser_state_t newstate
;
146 struct grub_lexer_param
*state
= parsestate
->lexerstate
;
151 if (state
->tokenonhold
)
153 int token
= state
->tokenonhold
;
154 state
->tokenonhold
= 0;
158 for (;! state
->done
&& (*state
->script
|| firstrun
); firstrun
= 0)
161 if (! *state
->script
)
163 /* Check if more tokens are requested by the parser. */
164 if (((state
->refs
&& ! parsestate
->err
)
165 || state
->state
== GRUB_PARSER_STATE_ESC
)
169 while (!state
->script
|| ! grub_strlen (state
->script
))
171 grub_free (state
->newscript
);
172 state
->newscript
= 0;
173 state
->getline (&state
->newscript
, 1);
174 state
->script
= state
->newscript
;
183 grub_dprintf ("scripting", "token=`\\n'\n");
184 recordchar (state
, '\n');
185 if (state
->state
!= GRUB_PARSER_STATE_ESC
)
187 state
->tokenonhold
= '\n';
193 grub_free (state
->newscript
);
194 state
->newscript
= 0;
196 grub_dprintf ("scripting", "token=`\\n'\n");
197 state
->tokenonhold
= '\n';
202 newstate
= grub_parser_cmdline_state (state
->state
, *state
->script
, &use
);
204 /* Check if it is a text. */
205 if (check_textstate (newstate
))
207 /* In case the string is not quoted, this can be a one char
209 if (newstate
== GRUB_PARSER_STATE_TEXT
)
212 switch (*state
->script
)
215 while (*state
->script
)
217 newstate
= grub_parser_cmdline_state (state
->state
,
218 *state
->script
, &use
);
219 if (! (state
->state
== GRUB_PARSER_STATE_TEXT
220 && *state
->script
== ' '))
222 grub_dprintf ("scripting", "token=` '\n");
227 state
->state
= newstate
;
230 grub_dprintf ("scripting", "token=` '\n");
240 grub_dprintf ("scripting", "token=`%c'\n", *state
->script
);
243 state
->tokenonhold
= c
;
252 /* XXX: Use a better size. */
253 buffer
= grub_script_malloc (parsestate
, 2048);
259 /* Read one token, possible quoted. */
260 while (*state
->script
)
262 newstate
= grub_parser_cmdline_state (state
->state
,
263 *state
->script
, &use
);
265 /* Check if a variable name starts. */
266 if (check_varstate (newstate
))
269 /* If the string is not quoted or escaped, stop processing
270 when a special token was found. It will be recognized
271 next time when this function is called. */
272 if (newstate
== GRUB_PARSER_STATE_TEXT
273 && state
->state
!= GRUB_PARSER_STATE_ESC
)
294 state
->state
= newstate
;
298 /* A string of text was read in. */
300 grub_dprintf ("scripting", "token=`%s'\n", buffer
);
301 yylval
->arg
= grub_script_arg_add (parsestate
, yylval
->arg
,
302 GRUB_SCRIPT_ARG_TYPE_STR
, buffer
);
305 else if (newstate
== GRUB_PARSER_STATE_VAR
306 || newstate
== GRUB_PARSER_STATE_QVAR
)
308 /* XXX: Use a better size. */
309 buffer
= grub_script_malloc (parsestate
, 2096);
315 /* This is a variable, read the variable name. */
316 while (*state
->script
)
318 newstate
= grub_parser_cmdline_state (state
->state
,
319 *state
->script
, &use
);
321 /* Check if this character is not part of the variable name
323 if (! (check_varstate (newstate
)))
325 if (state
->state
== GRUB_PARSER_STATE_VARNAME2
326 || state
->state
== GRUB_PARSER_STATE_QVARNAME2
)
328 state
->state
= newstate
;
335 state
->state
= newstate
;
339 state
->state
= newstate
;
340 yylval
->arg
= grub_script_arg_add (parsestate
, yylval
->arg
,
341 GRUB_SCRIPT_ARG_TYPE_VAR
, buffer
);
342 grub_dprintf ("scripting", "vartoken=`%s'\n", buffer
);
346 /* There is either text or a variable name. In the case you
347 arrive here there is a serious problem with the lexer. */
348 grub_error (GRUB_ERR_BAD_ARGUMENT
, "Internal error\n");
353 if (yylval
->arg
== 0)
355 int token
= state
->tokenonhold
;
356 state
->tokenonhold
= 0;
360 if (yylval
->arg
->next
== 0 && yylval
->arg
->type
== GRUB_SCRIPT_ARG_TYPE_STR
)
362 /* Detect some special tokens. */
363 if (! grub_strcmp (yylval
->arg
->str
, "while"))
364 return GRUB_PARSER_TOKEN_WHILE
;
365 else if (! grub_strcmp (yylval
->arg
->str
, "if"))
366 return GRUB_PARSER_TOKEN_IF
;
367 else if (! grub_strcmp (yylval
->arg
->str
, "function"))
368 return GRUB_PARSER_TOKEN_FUNCTION
;
369 else if (! grub_strcmp (yylval
->arg
->str
, "menuentry"))
370 return GRUB_PARSER_TOKEN_MENUENTRY
;
371 else if (! grub_strcmp (yylval
->arg
->str
, "@"))
372 return GRUB_PARSER_TOKEN_MENUENTRY
;
373 else if (! grub_strcmp (yylval
->arg
->str
, "else"))
374 return GRUB_PARSER_TOKEN_ELSE
;
375 else if (! grub_strcmp (yylval
->arg
->str
, "then"))
376 return GRUB_PARSER_TOKEN_THEN
;
377 else if (! grub_strcmp (yylval
->arg
->str
, "fi"))
378 return GRUB_PARSER_TOKEN_FI
;
381 return GRUB_PARSER_TOKEN_ARG
;
385 grub_script_yyerror (struct grub_parser_param
*lex
__attribute__ ((unused
)),
388 grub_printf ("%s\n", err
);