2N/A/* lexer.c - The scripting lexer. */
2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A#include <config.h>
2N/A
2N/A#include <grub/parser.h>
2N/A#include <grub/misc.h>
2N/A#include <grub/mm.h>
2N/A#include <grub/script_sh.h>
2N/A
2N/A#define yytext_ptr char *
2N/A#include "grub_script.tab.h"
2N/A#include "grub_script.yy.h"
2N/A
2N/Avoid
2N/Agrub_script_lexer_ref (struct grub_lexer_param *state)
2N/A{
2N/A state->refs++;
2N/A}
2N/A
2N/Avoid
2N/Agrub_script_lexer_deref (struct grub_lexer_param *state)
2N/A{
2N/A state->refs--;
2N/A}
2N/A
2N/A/* Start recording all characters passing through the lexer. */
2N/Aunsigned
2N/Agrub_script_lexer_record_start (struct grub_parser_param *parser)
2N/A{
2N/A struct grub_lexer_param *lexer = parser->lexerstate;
2N/A
2N/A lexer->record++;
2N/A if (lexer->recording)
2N/A return lexer->recordpos;
2N/A
2N/A lexer->recordpos = 0;
2N/A lexer->recordlen = GRUB_LEXER_INITIAL_RECORD_SIZE;
2N/A lexer->recording = grub_malloc (lexer->recordlen);
2N/A if (!lexer->recording)
2N/A {
2N/A grub_script_yyerror (parser, 0);
2N/A lexer->recordlen = 0;
2N/A }
2N/A return lexer->recordpos;
2N/A}
2N/A
2N/Achar *
2N/Agrub_script_lexer_record_stop (struct grub_parser_param *parser, unsigned offset)
2N/A{
2N/A int count;
2N/A char *result;
2N/A struct grub_lexer_param *lexer = parser->lexerstate;
2N/A
2N/A if (!lexer->record)
2N/A return 0;
2N/A
2N/A lexer->record--;
2N/A if (!lexer->recording)
2N/A return 0;
2N/A
2N/A count = lexer->recordpos - offset;
2N/A result = grub_script_malloc (parser, count + 1);
2N/A if (result) {
2N/A grub_strncpy (result, lexer->recording + offset, count);
2N/A result[count] = '\0';
2N/A }
2N/A
2N/A if (lexer->record == 0)
2N/A {
2N/A grub_free (lexer->recording);
2N/A lexer->recording = 0;
2N/A lexer->recordlen = 0;
2N/A lexer->recordpos = 0;
2N/A }
2N/A return result;
2N/A}
2N/A
2N/A/* Record STR if input recording is enabled. */
2N/Avoid
2N/Agrub_script_lexer_record (struct grub_parser_param *parser, char *str)
2N/A{
2N/A int len;
2N/A char *old;
2N/A struct grub_lexer_param *lexer = parser->lexerstate;
2N/A
2N/A if (!lexer->record || !lexer->recording)
2N/A return;
2N/A
2N/A len = grub_strlen (str);
2N/A if (lexer->recordpos + len + 1 > lexer->recordlen)
2N/A {
2N/A old = lexer->recording;
2N/A if (lexer->recordlen < len)
2N/A lexer->recordlen = len;
2N/A lexer->recordlen *= 2;
2N/A lexer->recording = grub_realloc (lexer->recording, lexer->recordlen);
2N/A if (!lexer->recording)
2N/A {
2N/A grub_free (old);
2N/A lexer->recordpos = 0;
2N/A lexer->recordlen = 0;
2N/A grub_script_yyerror (parser, 0);
2N/A return;
2N/A }
2N/A }
2N/A grub_strcpy (lexer->recording + lexer->recordpos, str);
2N/A lexer->recordpos += len;
2N/A}
2N/A
2N/A/* Read next line of input if necessary, and set yyscanner buffers. */
2N/Aint
2N/Agrub_script_lexer_yywrap (struct grub_parser_param *parserstate,
2N/A const char *input)
2N/A{
2N/A int len = 0;
2N/A char *p = 0;
2N/A char *line = 0;
2N/A YY_BUFFER_STATE buffer;
2N/A struct grub_lexer_param *lexerstate = parserstate->lexerstate;
2N/A
2N/A if (! lexerstate->refs && ! lexerstate->prefix && ! input)
2N/A return 1;
2N/A
2N/A if (! lexerstate->getline && ! input)
2N/A {
2N/A grub_script_yyerror (parserstate, "unexpected end of file");
2N/A return 1;
2N/A }
2N/A
2N/A line = 0;
2N/A if (! input)
2N/A lexerstate->getline (&line, 1);
2N/A else
2N/A line = grub_strdup (input);
2N/A
2N/A /* Ensure '\n' at the end. */
2N/A if (line && line[0] == '\0')
2N/A {
2N/A grub_free (line);
2N/A line = grub_strdup ("\n");
2N/A }
2N/A
2N/A if (line && (len = grub_strlen(line)) && line[len - 1] != '\n')
2N/A {
2N/A p = grub_realloc (line, len + 2);
2N/A if (p)
2N/A {
2N/A p[len++] = '\n';
2N/A p[len] = '\0';
2N/A }
2N/A line = p;
2N/A }
2N/A
2N/A if (! line)
2N/A {
2N/A grub_script_yyerror (parserstate, "out of memory");
2N/A return 1;
2N/A }
2N/A
2N/A /* Prepend any left over unput-text. */
2N/A if (lexerstate->prefix)
2N/A {
2N/A int plen = grub_strlen (lexerstate->prefix);
2N/A
2N/A p = grub_malloc (len + plen + 1);
2N/A if (! p)
2N/A {
2N/A grub_free (line);
2N/A return 1;
2N/A }
2N/A grub_strcpy (p, lexerstate->prefix);
2N/A lexerstate->prefix = 0;
2N/A
2N/A grub_strcpy (p + plen, line);
2N/A grub_free (line);
2N/A
2N/A line = p;
2N/A len = len + plen;
2N/A }
2N/A
2N/A buffer = yy_scan_string (line, lexerstate->yyscanner);
2N/A grub_free (line);
2N/A
2N/A if (! buffer)
2N/A {
2N/A grub_script_yyerror (parserstate, 0);
2N/A return 1;
2N/A }
2N/A return 0;
2N/A}
2N/A
2N/Astruct grub_lexer_param *
2N/Agrub_script_lexer_init (struct grub_parser_param *parser, char *script,
2N/A grub_reader_getline_t getline)
2N/A{
2N/A struct grub_lexer_param *lexerstate;
2N/A
2N/A lexerstate = grub_zalloc (sizeof (*lexerstate));
2N/A if (!lexerstate)
2N/A return 0;
2N/A
2N/A lexerstate->size = GRUB_LEXER_INITIAL_TEXT_SIZE;
2N/A lexerstate->text = grub_malloc (lexerstate->size);
2N/A if (!lexerstate->text)
2N/A {
2N/A grub_free (lexerstate);
2N/A return 0;
2N/A }
2N/A
2N/A lexerstate->getline = getline; /* rest are all zeros already */
2N/A if (yylex_init (&lexerstate->yyscanner))
2N/A {
2N/A grub_free (lexerstate->text);
2N/A grub_free (lexerstate);
2N/A return 0;
2N/A }
2N/A
2N/A yyset_extra (parser, lexerstate->yyscanner);
2N/A parser->lexerstate = lexerstate;
2N/A
2N/A if (grub_script_lexer_yywrap (parser, script ?: "\n"))
2N/A {
2N/A parser->lexerstate = 0;
2N/A yylex_destroy (lexerstate->yyscanner);
2N/A grub_free (lexerstate->yyscanner);
2N/A grub_free (lexerstate->text);
2N/A grub_free (lexerstate);
2N/A return 0;
2N/A }
2N/A
2N/A return lexerstate;
2N/A}
2N/A
2N/Avoid
2N/Agrub_script_lexer_fini (struct grub_lexer_param *lexerstate)
2N/A{
2N/A if (!lexerstate)
2N/A return;
2N/A
2N/A yylex_destroy (lexerstate->yyscanner);
2N/A
2N/A grub_free (lexerstate->recording);
2N/A grub_free (lexerstate->text);
2N/A grub_free (lexerstate);
2N/A}
2N/A
2N/Aint
2N/Agrub_script_yylex (union YYSTYPE *value,
2N/A struct grub_parser_param *parserstate)
2N/A{
2N/A char *str;
2N/A int token;
2N/A grub_script_arg_type_t type;
2N/A struct grub_lexer_param *lexerstate = parserstate->lexerstate;
2N/A
2N/A value->arg = 0;
2N/A if (parserstate->err)
2N/A return GRUB_PARSER_TOKEN_BAD;
2N/A
2N/A if (lexerstate->eof)
2N/A return GRUB_PARSER_TOKEN_EOF;
2N/A
2N/A /*
2N/A * Words with environment variables, like foo${bar}baz needs
2N/A * multiple tokens to be merged into a single grub_script_arg. We
2N/A * use two variables to achieve this: lexerstate->merge_start and
2N/A * lexerstate->merge_end
2N/A */
2N/A
2N/A lexerstate->merge_start = 0;
2N/A lexerstate->merge_end = 0;
2N/A do
2N/A {
2N/A /* Empty lexerstate->text. */
2N/A lexerstate->used = 1;
2N/A lexerstate->text[0] = '\0';
2N/A
2N/A token = yylex (value, lexerstate->yyscanner);
2N/A if (token == GRUB_PARSER_TOKEN_BAD)
2N/A break;
2N/A
2N/A /* Merging feature uses lexerstate->text instead of yytext. */
2N/A if (lexerstate->merge_start)
2N/A {
2N/A str = lexerstate->text;
2N/A type = lexerstate->type;
2N/A }
2N/A else
2N/A {
2N/A str = yyget_text (lexerstate->yyscanner);
2N/A type = GRUB_SCRIPT_ARG_TYPE_TEXT;
2N/A }
2N/A grub_dprintf("lexer", "token %u text [%s]\n", token, str);
2N/A
2N/A value->arg = grub_script_arg_add (parserstate, value->arg, type, str);
2N/A }
2N/A while (lexerstate->merge_start && !lexerstate->merge_end);
2N/A
2N/A if (!value->arg || parserstate->err)
2N/A return GRUB_PARSER_TOKEN_BAD;
2N/A
2N/A return token;
2N/A}
2N/A
2N/Avoid
2N/Agrub_script_yyerror (struct grub_parser_param *state, char const *err)
2N/A{
2N/A if (err)
2N/A grub_error (GRUB_ERR_INVALID_COMMAND, err);
2N/A
2N/A grub_print_error ();
2N/A state->err++;
2N/A}