123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- /* lexer.c - The scripting lexer. */
- /*
- * GRUB -- GRand Unified Bootloader
- * Copyright (C) 2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
- *
- * GRUB is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * GRUB is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
- */
- #include <grub/parser.h>
- #include <grub/misc.h>
- #include <grub/mm.h>
- #include <grub/script_sh.h>
- #include "grub_script.tab.h"
- #include "grub_script.yy.h"
- void
- grub_script_lexer_ref (struct grub_lexer_param *state)
- {
- state->refs++;
- }
- void
- grub_script_lexer_deref (struct grub_lexer_param *state)
- {
- state->refs--;
- }
- /* Start recording all characters passing through the lexer. */
- void
- grub_script_lexer_record_start (struct grub_parser_param *parser)
- {
- struct grub_lexer_param *lexer = parser->lexerstate;
- lexer->record = 1;
- lexer->recordpos = 0;
- if (lexer->recording) /* reuse last record */
- return;
- lexer->recordlen = GRUB_LEXER_INITIAL_RECORD_SIZE;
- lexer->recording = grub_malloc (lexer->recordlen);
- if (!lexer->recording)
- {
- grub_script_yyerror (parser, 0);
- lexer->record = 0;
- lexer->recordlen = 0;
- }
- }
- static char *
- compact (char *start, char *end)
- {
- /* Delete '{' and '}' characters and whitespaces. */
- while (*start && grub_isspace (*start)) start++;
- if (*start == '{') start++;
- while (*start && grub_isspace (*start)) start++;
- while (*end && grub_isspace (*end)) end--;
- if (*end == '}') end--;
- while (*end && grub_isspace (*end)) end--;
- end[1] = '\0';
- return start;
- }
- char *
- grub_script_lexer_record_stop (struct grub_parser_param *parser)
- {
- char *ptr;
- char *result;
- struct grub_lexer_param *lexer = parser->lexerstate;
- if (!lexer->record || !lexer->recording)
- return 0;
- /* XXX This is not necessary in BASH. */
- ptr = compact (lexer->recording, lexer->recording + lexer->recordpos - 1);
- lexer->record = 0;
- lexer->recordpos = 0;
- /* This memory would be freed by, grub_script_free. */
- result = grub_script_malloc (parser, grub_strlen (ptr) + 1);
- if (result)
- grub_strcpy (result, ptr);
- return result;
- }
- #define MAX(a,b) ((a) < (b) ? (b) : (a))
- /* Record STR if input recording is enabled. */
- void
- grub_script_lexer_record (struct grub_parser_param *parser, char *str)
- {
- int len;
- char *old;
- struct grub_lexer_param *lexer = parser->lexerstate;
- if (!lexer->record)
- return;
- len = grub_strlen (str);
- if (lexer->recordpos + len + 1 > lexer->recordlen)
- {
- old = lexer->recording;
- lexer->recordlen = MAX (len, lexer->recordlen) * 2;
- lexer->recording = grub_realloc (lexer->recording, lexer->recordlen);
- if (!lexer->recording)
- {
- grub_free (old);
- lexer->record = 0;
- lexer->recordpos = 0;
- lexer->recordlen /= 2;
- grub_script_yyerror (parser, 0);
- return;
- }
- }
- grub_strcpy (lexer->recording + lexer->recordpos, str);
- lexer->recordpos += len;
- }
- /* Append '\n' to SRC, before '\0' */
- static char *
- append_newline (const char *src)
- {
- char *line;
- grub_size_t len;
- len = grub_strlen (src);
- line = grub_malloc (len + 2);
- if (!line)
- return 0;
- grub_strcpy (line, src);
- line[len] = '\n';
- line[len + 1] = '\0';
- return line;
- }
- /* Read next line of input if necessary, and set yyscanner buffers. */
- int
- grub_script_lexer_yywrap (struct grub_parser_param *parserstate)
- {
- int len;
- char *line;
- char *line2;
- YY_BUFFER_STATE buffer;
- struct grub_lexer_param *lexerstate = parserstate->lexerstate;
- if (!lexerstate->refs)
- return 0;
- if (!lexerstate->getline)
- {
- grub_script_yyerror (parserstate, "unexpected end of file");
- return 0;
- }
- line = 0;
- buffer = 0;
- lexerstate->getline (&line, 1, lexerstate->closure);
- if (!line)
- {
- grub_script_yyerror (parserstate, 0); /* XXX this could be for ^C case? */
- return 0;
- }
- len = grub_strlen (line);
- if (line[len - 1] == '\n')
- {
- buffer = yy_scan_string (line, lexerstate->yyscanner);
- }
- else
- {
- line2 = append_newline (line);
- if (line2)
- {
- buffer = yy_scan_string (line2, lexerstate->yyscanner);
- grub_free (line2);
- }
- }
- grub_free (line);
- if (!buffer)
- {
- grub_script_yyerror (parserstate, 0);
- return 0;
- }
- return 1;
- }
- struct grub_lexer_param *
- grub_script_lexer_init (struct grub_parser_param *parser, char *script,
- grub_reader_getline_t getline, void *closure)
- {
- int len;
- char *script2;
- YY_BUFFER_STATE buffer;
- struct grub_lexer_param *lexerstate;
- lexerstate = grub_zalloc (sizeof (*lexerstate));
- if (!lexerstate)
- return 0;
- lexerstate->size = GRUB_LEXER_INITIAL_TEXT_SIZE;
- lexerstate->text = grub_malloc (lexerstate->size);
- if (!lexerstate->text)
- {
- grub_free (lexerstate);
- return 0;
- }
- lexerstate->getline = getline; /* rest are all zeros already */
- lexerstate->closure = closure;
- if (yylex_init (&lexerstate->yyscanner))
- {
- grub_free (lexerstate->text);
- grub_free (lexerstate);
- return 0;
- }
- buffer = 0;
- script = script ? : "\n";
- len = grub_strlen (script);
- if (script[len - 1] == '\n')
- {
- buffer = yy_scan_string (script, lexerstate->yyscanner);
- }
- else
- {
- script2 = append_newline (script);
- if (script2)
- {
- buffer = yy_scan_string (script2, lexerstate->yyscanner);
- grub_free (script2);
- }
- }
- if (!buffer)
- {
- yylex_destroy (lexerstate->yyscanner);
- grub_free (lexerstate->yyscanner);
- grub_free (lexerstate->text);
- grub_free (lexerstate);
- return 0;
- }
- yyset_extra (parser, lexerstate->yyscanner);
- return lexerstate;
- }
- void
- grub_script_lexer_fini (struct grub_lexer_param *lexerstate)
- {
- if (!lexerstate)
- return;
- yylex_destroy (lexerstate->yyscanner);
- grub_free (lexerstate->recording);
- grub_free (lexerstate->text);
- grub_free (lexerstate);
- }
- int
- grub_script_yylex (union YYSTYPE *value,
- struct grub_parser_param *parserstate)
- {
- char *str;
- int token;
- grub_script_arg_type_t type;
- struct grub_lexer_param *lexerstate = parserstate->lexerstate;
- value->arg = 0;
- if (parserstate->err)
- return GRUB_PARSER_TOKEN_BAD;
- if (lexerstate->eof)
- return GRUB_PARSER_TOKEN_EOF;
- /*
- * Words with environment variables, like foo${bar}baz needs
- * multiple tokens to be merged into a single grub_script_arg. We
- * use two variables to achieve this: lexerstate->merge_start and
- * lexerstate->merge_end
- */
- lexerstate->merge_start = 0;
- lexerstate->merge_end = 0;
- do
- {
- /* Empty lexerstate->text. */
- lexerstate->used = 1;
- lexerstate->text[0] = '\0';
- token = yylex (value, lexerstate->yyscanner);
- if (token == GRUB_PARSER_TOKEN_BAD)
- break;
- /* Merging feature uses lexerstate->text instead of yytext. */
- if (lexerstate->merge_start)
- {
- str = lexerstate->text;
- type = lexerstate->type;
- }
- else
- {
- str = yyget_text (lexerstate->yyscanner);
- type = GRUB_SCRIPT_ARG_TYPE_TEXT;
- }
- grub_dprintf("lexer", "token %u text [%s]\n", token, str);
- value->arg = grub_script_arg_add (parserstate, value->arg, type, str);
- }
- while (lexerstate->merge_start && !lexerstate->merge_end);
- if (!value->arg || parserstate->err)
- return GRUB_PARSER_TOKEN_BAD;
- return token;
- }
- void
- grub_script_yyerror (struct grub_parser_param *state, char const *err)
- {
- if (err)
- grub_error (GRUB_ERR_INVALID_COMMAND, err);
- grub_print_error ();
- state->err++;
- }
|