remove all trailing whitespace
[grub2/phcoder/solaris.git] / script / sh / lexer.c
blobaa8ac35aa13f774921d40ceb453934d9b36e3c8b
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_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));
52 if (! param)
53 return 0;
55 param->state = GRUB_PARSER_STATE_TEXT;
56 param->getline = getline;
57 param->refs = 0;
58 param->done = 0;
59 param->newscript = 0;
60 param->script = script;
61 param->record = 0;
62 param->recording = 0;
63 param->recordpos = 0;
64 param->recordlen = 0;
65 param->tokenonhold = 0;
67 return param;
70 void
71 grub_script_lexer_ref (struct grub_lexer_param *state)
73 state->refs++;
76 void
77 grub_script_lexer_deref (struct grub_lexer_param *state)
79 state->refs--;
82 /* Start recording all characters passing through the lexer. */
83 void
84 grub_script_lexer_record_start (struct grub_lexer_param *state)
86 state->record = 1;
87 state->recordlen = 100;
88 state->recording = grub_malloc (state->recordlen);
89 state->recordpos = 0;
92 char *
93 grub_script_lexer_record_stop (struct grub_lexer_param *state)
95 state->record = 0;
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");
103 for (;;); /* XXX */
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. */
113 static void
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)
123 grub_free (old);
124 state->record = 0;
127 state->recording[state->recordpos++] = c;
130 /* Fetch the next character for the lexer. */
131 static void
132 nextchar (struct grub_lexer_param *state)
134 if (state->record)
135 recordchar (state, *state->script);
136 state->script++;
140 grub_script_yylex (union YYSTYPE *yylval, struct grub_parser_param *parsestate)
142 grub_parser_state_t newstate;
143 char use;
144 char *buffer;
145 char *bp;
146 struct grub_lexer_param *state = parsestate->lexerstate;
147 int firstrun = 1;
149 yylval->arg = 0;
151 if (state->tokenonhold)
153 int token = state->tokenonhold;
154 state->tokenonhold = 0;
155 return token;
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)
166 && state->getline)
168 int doexit = 0;
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;
175 if (! state->script)
177 doexit = 1;
178 break;
181 if (doexit)
182 break;
183 grub_dprintf ("scripting", "token=`\\n'\n");
184 recordchar (state, '\n');
185 if (state->state != GRUB_PARSER_STATE_ESC)
187 state->tokenonhold = '\n';
188 break;
191 else
193 grub_free (state->newscript);
194 state->newscript = 0;
195 state->done = 1;
196 grub_dprintf ("scripting", "token=`\\n'\n");
197 state->tokenonhold = '\n';
198 break;
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
208 length symbol. */
209 if (newstate == GRUB_PARSER_STATE_TEXT)
211 int doexit = 0;
212 switch (*state->script)
214 case ' ':
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");
223 if (! firstrun)
224 doexit = 1;
225 break;
227 state->state = newstate;
228 nextchar (state);
230 grub_dprintf ("scripting", "token=` '\n");
231 if (! firstrun)
232 doexit = 1;
233 break;
234 case '{':
235 case '}':
236 case ';':
237 case '\n':
239 char c;
240 grub_dprintf ("scripting", "token=`%c'\n", *state->script);
241 c = *state->script;;
242 nextchar (state);
243 state->tokenonhold = c;
244 doexit = 1;
245 break;
248 if (doexit)
249 break;
252 /* XXX: Use a better size. */
253 buffer = grub_script_malloc (parsestate, 2048);
254 if (! buffer)
255 return 0;
257 bp = buffer;
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))
267 break;
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)
275 int breakout = 0;
277 switch (use)
279 case ' ':
280 case '{':
281 case '}':
282 case ';':
283 case '\n':
284 breakout = 1;
286 if (breakout)
287 break;
288 if (use)
289 *(bp++) = use;
291 else if (use)
292 *(bp++) = use;
294 state->state = newstate;
295 nextchar (state);
298 /* A string of text was read in. */
299 *bp = '\0';
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);
310 if (! buffer)
311 return 0;
313 bp = buffer;
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
322 anymore. */
323 if (! (check_varstate (newstate)))
325 if (state->state == GRUB_PARSER_STATE_VARNAME2
326 || state->state == GRUB_PARSER_STATE_QVARNAME2)
327 nextchar (state);
328 state->state = newstate;
329 break;
332 if (use)
333 *(bp++) = use;
334 nextchar (state);
335 state->state = newstate;
338 *bp = '\0';
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);
344 else
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");
349 return 0;
353 if (yylval->arg == 0)
355 int token = state->tokenonhold;
356 state->tokenonhold = 0;
357 return token;
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;
384 void
385 grub_script_yyerror (struct grub_parser_param *lex __attribute__ ((unused)),
386 char const *err)
388 grub_printf ("%s\n", err);