2008-09-28 Robert Millan <rmh@aybabtu.com>
[grub2/phcoder.git] / kern / parser.c
blobe93185334255364d6451350a69a978d8609e8d75
1 /* parser.c - the part of the parser that can return partial tokens */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2005,2007 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/env.h>
22 #include <grub/misc.h>
23 #include <grub/mm.h>
26 /* All the possible state transitions on the command line. If a
27 transition can not be found, it is assumed that there is no
28 transition and keep_value is assumed to be 1. */
29 static struct grub_parser_state_transition state_transitions[] =
31 { GRUB_PARSER_STATE_TEXT, GRUB_PARSER_STATE_QUOTE, '\'', 0},
32 { GRUB_PARSER_STATE_TEXT, GRUB_PARSER_STATE_DQUOTE, '\"', 0},
33 { GRUB_PARSER_STATE_TEXT, GRUB_PARSER_STATE_VAR, '$', 0},
34 { GRUB_PARSER_STATE_TEXT, GRUB_PARSER_STATE_ESC, '\\', 0},
36 { GRUB_PARSER_STATE_ESC, GRUB_PARSER_STATE_TEXT, 0, 1},
38 { GRUB_PARSER_STATE_QUOTE, GRUB_PARSER_STATE_TEXT, '\'', 0},
40 { GRUB_PARSER_STATE_DQUOTE, GRUB_PARSER_STATE_TEXT, '\"', 0},
41 { GRUB_PARSER_STATE_DQUOTE, GRUB_PARSER_STATE_QVAR, '$', 0},
43 { GRUB_PARSER_STATE_VAR, GRUB_PARSER_STATE_VARNAME2, '{', 0},
44 { GRUB_PARSER_STATE_VAR, GRUB_PARSER_STATE_VARNAME, 0, 1},
45 { GRUB_PARSER_STATE_VARNAME, GRUB_PARSER_STATE_TEXT, ' ', 1},
46 { GRUB_PARSER_STATE_VARNAME2, GRUB_PARSER_STATE_TEXT, '}', 0},
48 { GRUB_PARSER_STATE_QVAR, GRUB_PARSER_STATE_QVARNAME2, '{', 0},
49 { GRUB_PARSER_STATE_QVAR, GRUB_PARSER_STATE_QVARNAME, 0, 1},
50 { GRUB_PARSER_STATE_QVARNAME, GRUB_PARSER_STATE_DQUOTE, ' ', 1},
51 { GRUB_PARSER_STATE_QVARNAME, GRUB_PARSER_STATE_TEXT, '\"', 0},
52 { GRUB_PARSER_STATE_QVARNAME2, GRUB_PARSER_STATE_DQUOTE, '}', 0},
54 { 0, 0, 0, 0}
58 /* Determines the state following STATE, determined by C. */
59 grub_parser_state_t
60 grub_parser_cmdline_state (grub_parser_state_t state, char c, char *result)
62 struct grub_parser_state_transition *transition;
63 struct grub_parser_state_transition *next_match = 0;
64 struct grub_parser_state_transition default_transition;
65 int found = 0;
67 default_transition.to_state = state;
68 default_transition.keep_value = 1;
70 /* Look for a good translation. */
71 for (transition = state_transitions; transition->from_state; transition++)
73 /* An exact match was found, use it. */
74 if (transition->from_state == state && transition->input == c)
76 found = 1;
77 break;
80 /* A less perfect match was found, use this one if no exact
81 match can be found. */
82 if (transition->from_state == state && transition->input == 0)
83 next_match = transition;
86 if (! found)
88 if (next_match)
89 transition = next_match;
90 else
91 transition = &default_transition;
94 if (transition->keep_value)
95 *result = c;
96 else
97 *result = 0;
98 return transition->to_state;
102 grub_err_t
103 grub_parser_split_cmdline (const char *cmdline, grub_err_t (*getline) (char **),
104 int *argc, char ***argv)
106 grub_parser_state_t state = GRUB_PARSER_STATE_TEXT;
107 /* XXX: Fixed size buffer, perhaps this buffer should be dynamically
108 allocated. */
109 char buffer[1024];
110 char *bp = buffer;
111 char *rd = (char *) cmdline;
112 char varname[200];
113 char *vp = varname;
114 char *args;
115 int i;
117 auto int check_varstate (grub_parser_state_t s);
119 int check_varstate (grub_parser_state_t s)
121 return (s == GRUB_PARSER_STATE_VARNAME
122 || s == GRUB_PARSER_STATE_VARNAME2
123 || s == GRUB_PARSER_STATE_QVARNAME
124 || s == GRUB_PARSER_STATE_QVARNAME2);
127 auto void add_var (grub_parser_state_t newstate);
129 void add_var (grub_parser_state_t newstate)
131 char *val;
133 /* Check if a variable was being read in and the end of the name
134 was reached. */
135 if (! (check_varstate (state) && !check_varstate (newstate)))
136 return;
138 *(vp++) = '\0';
139 val = grub_env_get (varname);
140 vp = varname;
141 if (! val)
142 return;
144 /* Insert the contents of the variable in the buffer. */
145 for (; *val; val++)
146 *(bp++) = *val;
149 *argc = 1;
152 if (! *rd)
154 if (getline)
155 getline (&rd);
156 else break;
159 for (; *rd; rd++)
161 grub_parser_state_t newstate;
162 char use;
164 newstate = grub_parser_cmdline_state (state, *rd, &use);
166 /* If a variable was being processed and this character does
167 not describe the variable anymore, write the variable to
168 the buffer. */
169 add_var (newstate);
171 if (check_varstate (newstate))
173 if (use)
174 *(vp++) = use;
176 else
178 if (newstate == GRUB_PARSER_STATE_TEXT
179 && state != GRUB_PARSER_STATE_ESC && use == ' ')
181 /* Don't add more than one argument if multiple
182 spaces are used. */
183 if (bp != buffer && *(bp - 1))
185 *(bp++) = '\0';
186 (*argc)++;
189 else if (use)
190 *(bp++) = use;
192 state = newstate;
194 } while (state != GRUB_PARSER_STATE_TEXT && !check_varstate (state));
195 *(bp++) = '\0';
197 /* A special case for when the last character was part of a
198 variable. */
199 add_var (GRUB_PARSER_STATE_TEXT);
202 /* Reserve memory for the return values. */
203 args = grub_malloc (bp - buffer);
204 if (! args)
205 return grub_errno;
206 grub_memcpy (args, buffer, bp - buffer);
208 *argv = grub_malloc (sizeof (char *) * (*argc + 1));
209 if (! *argv)
211 grub_free (args);
212 return grub_errno;
215 /* The arguments are separated with 0's, setup argv so it points to
216 the right values. */
217 bp = args;
218 for (i = 0; i < *argc; i++)
220 (*argv)[i] = bp;
221 while (*bp)
222 bp++;
223 bp++;
226 (*argc)--;
228 return 0;