1
/* parser.c - the part of the parser that can return partial tokens */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 2005 Free Software Foundation, Inc.
6
* This program 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 2 of the License, or
9
* (at your option) any later version.
11
* This program 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 this program; if not, write to the Free Software
18
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
#include <grub/parser.h>
23
#include <grub/misc.h>
27
/* All the possible state transitions on the command line. If a
28
transition can not be found, it is assumed that there is no
29
transition and keep_value is assumed to be 1. */
30
static struct grub_parser_state_transition state_transitions[] =
32
{ GRUB_PARSER_STATE_TEXT, GRUB_PARSER_STATE_QUOTE, '\'', 0},
33
{ GRUB_PARSER_STATE_TEXT, GRUB_PARSER_STATE_DQUOTE, '\"', 0},
34
{ GRUB_PARSER_STATE_TEXT, GRUB_PARSER_STATE_VAR, '$', 0},
35
{ GRUB_PARSER_STATE_TEXT, GRUB_PARSER_STATE_ESC, '\\', 0},
37
{ GRUB_PARSER_STATE_ESC, GRUB_PARSER_STATE_TEXT, 0, 1},
39
{ GRUB_PARSER_STATE_QUOTE, GRUB_PARSER_STATE_TEXT, '\'', 0},
41
{ GRUB_PARSER_STATE_DQUOTE, GRUB_PARSER_STATE_TEXT, '\"', 0},
42
{ GRUB_PARSER_STATE_DQUOTE, GRUB_PARSER_STATE_QVAR, '$', 0},
44
{ GRUB_PARSER_STATE_VAR, GRUB_PARSER_STATE_VARNAME2, '{', 0},
45
{ GRUB_PARSER_STATE_VAR, GRUB_PARSER_STATE_VARNAME, 0, 1},
46
{ GRUB_PARSER_STATE_VARNAME, GRUB_PARSER_STATE_TEXT, ' ', 1},
47
{ GRUB_PARSER_STATE_VARNAME2, GRUB_PARSER_STATE_TEXT, '}', 0},
49
{ GRUB_PARSER_STATE_QVAR, GRUB_PARSER_STATE_QVARNAME2, '{', 0},
50
{ GRUB_PARSER_STATE_QVAR, GRUB_PARSER_STATE_QVARNAME, 0, 1},
51
{ GRUB_PARSER_STATE_QVARNAME, GRUB_PARSER_STATE_DQUOTE, ' ', 1},
52
{ GRUB_PARSER_STATE_QVARNAME, GRUB_PARSER_STATE_TEXT, '\"', 0},
53
{ GRUB_PARSER_STATE_QVARNAME2, GRUB_PARSER_STATE_DQUOTE, '}', 0},
59
/* Determines the state following STATE, determined by C. */
61
grub_parser_cmdline_state (grub_parser_state_t state, char c, char *result)
63
struct grub_parser_state_transition *transition;
64
struct grub_parser_state_transition *next_match = 0;
65
struct grub_parser_state_transition default_transition;
68
default_transition.to_state = state;
69
default_transition.keep_value = 1;
71
/* Look for a good translation. */
72
for (transition = state_transitions; transition->from_state; transition++)
74
/* An exact match was found, use it. */
75
if (transition->from_state == state && transition->input == c)
81
/* A less perfect match was found, use this one if no exact
82
match can be found. */
83
if (transition->from_state == state && transition->input == 0)
84
next_match = transition;
90
transition = next_match;
92
transition = &default_transition;
95
if (transition->keep_value)
99
return transition->to_state;
104
grub_parser_split_cmdline (const char *cmdline, grub_err_t (*getline) (char **),
105
int *argc, char ***argv)
107
grub_parser_state_t state = GRUB_PARSER_STATE_TEXT;
108
/* XXX: Fixed size buffer, perhaps this buffer should be dynamically
112
char *rd = (char *) cmdline;
118
auto int check_varstate (grub_parser_state_t s);
120
int check_varstate (grub_parser_state_t s)
122
return (s == GRUB_PARSER_STATE_VARNAME
123
|| s == GRUB_PARSER_STATE_VARNAME2
124
|| s == GRUB_PARSER_STATE_QVARNAME
125
|| s == GRUB_PARSER_STATE_QVARNAME2);
128
auto void add_var (grub_parser_state_t newstate);
130
void add_var (grub_parser_state_t newstate)
134
/* Check if a variable was being read in and the end of the name
136
if (! (check_varstate (state) && !check_varstate (newstate)))
140
val = grub_env_get (varname);
145
/* Insert the contents of the variable in the buffer. */
162
grub_parser_state_t newstate;
165
newstate = grub_parser_cmdline_state (state, *rd, &use);
167
/* If a variable was being processed and this character does
168
not describe the variable anymore, write the variable to
172
if (check_varstate (newstate))
179
if (newstate == GRUB_PARSER_STATE_TEXT
180
&& state != GRUB_PARSER_STATE_ESC && use == ' ')
182
/* Don't add more than one argument if multiple
184
if (bp != buffer && *(bp - 1))
195
} while (state != GRUB_PARSER_STATE_TEXT && !check_varstate (state));
198
/* A special case for when the last character was part of a
200
add_var (GRUB_PARSER_STATE_TEXT);
203
/* Reserve memory for the return values. */
204
args = grub_malloc (bp - buffer);
207
grub_memcpy (args, buffer, bp - buffer);
209
*argv = grub_malloc (sizeof (char *) * (*argc + 1));
216
/* The arguments are separated with 0's, setup argv so it points to
219
for (i = 0; i < *argc; i++)