parser.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /* parser.c - the part of the parser that can return partial tokens */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2005,2007,2009 Free Software Foundation, Inc.
  5. *
  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.
  10. *
  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.
  15. *
  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/>.
  18. */
  19. #include <grub/parser.h>
  20. #include <grub/env.h>
  21. #include <grub/misc.h>
  22. #include <grub/mm.h>
  23. GRUB_EXPORT(grub_parser_split_cmdline);
  24. GRUB_EXPORT(grub_parser_cmdline_state);
  25. GRUB_EXPORT(grub_parser_execute);
  26. GRUB_EXPORT(grub_parser_class);
  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[] = {
  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},
  35. {GRUB_PARSER_STATE_ESC, GRUB_PARSER_STATE_TEXT, 0, 1},
  36. {GRUB_PARSER_STATE_QUOTE, GRUB_PARSER_STATE_TEXT, '\'', 0},
  37. {GRUB_PARSER_STATE_DQUOTE, GRUB_PARSER_STATE_TEXT, '\"', 0},
  38. {GRUB_PARSER_STATE_DQUOTE, GRUB_PARSER_STATE_QVAR, '$', 0},
  39. {GRUB_PARSER_STATE_VAR, GRUB_PARSER_STATE_VARNAME2, '{', 0},
  40. {GRUB_PARSER_STATE_VAR, GRUB_PARSER_STATE_VARNAME, 0, 1},
  41. {GRUB_PARSER_STATE_VARNAME, GRUB_PARSER_STATE_TEXT, ' ', 1},
  42. {GRUB_PARSER_STATE_VARNAME2, GRUB_PARSER_STATE_TEXT, '}', 0},
  43. {GRUB_PARSER_STATE_QVAR, GRUB_PARSER_STATE_QVARNAME2, '{', 0},
  44. {GRUB_PARSER_STATE_QVAR, GRUB_PARSER_STATE_QVARNAME, 0, 1},
  45. {GRUB_PARSER_STATE_QVARNAME, GRUB_PARSER_STATE_TEXT, '\"', 0},
  46. {GRUB_PARSER_STATE_QVARNAME, GRUB_PARSER_STATE_DQUOTE, ' ', 1},
  47. {GRUB_PARSER_STATE_QVARNAME2, GRUB_PARSER_STATE_DQUOTE, '}', 0},
  48. {0, 0, 0, 0}
  49. };
  50. /* Determines the state following STATE, determined by C. */
  51. grub_parser_state_t
  52. grub_parser_cmdline_state (grub_parser_state_t state, char c, char *result)
  53. {
  54. struct grub_parser_state_transition *transition;
  55. struct grub_parser_state_transition default_transition;
  56. default_transition.to_state = state;
  57. default_transition.keep_value = 1;
  58. /* Look for a good translation. */
  59. for (transition = state_transitions; transition->from_state; transition++)
  60. {
  61. if (transition->from_state != state)
  62. continue;
  63. /* An exact match was found, use it. */
  64. if (transition->input == c)
  65. break;
  66. if (transition->input == ' ' && !grub_isalpha (c)
  67. && !grub_isdigit (c) && c != '_')
  68. break;
  69. /* A less perfect match was found, use this one if no exact
  70. match can be found. */
  71. if (transition->input == 0)
  72. break;
  73. }
  74. if (!transition->from_state)
  75. transition = &default_transition;
  76. if (transition->keep_value)
  77. *result = c;
  78. else
  79. *result = 0;
  80. return transition->to_state;
  81. }
  82. static int
  83. check_varstate (grub_parser_state_t s)
  84. {
  85. return (s == GRUB_PARSER_STATE_VARNAME
  86. || s == GRUB_PARSER_STATE_VARNAME2
  87. || s == GRUB_PARSER_STATE_QVARNAME
  88. || s == GRUB_PARSER_STATE_QVARNAME2);
  89. }
  90. struct add_var_closure
  91. {
  92. char varname[200];
  93. char *vp;
  94. char **bp;
  95. grub_parser_state_t *state;
  96. };
  97. static void
  98. add_var (grub_parser_state_t newstate, struct add_var_closure *c)
  99. {
  100. char *val;
  101. /* Check if a variable was being read in and the end of the name
  102. was reached. */
  103. if (!(check_varstate (*(c->state)) && !check_varstate (newstate)))
  104. return;
  105. *((c->vp)++) = '\0';
  106. val = grub_env_get (c->varname);
  107. c->vp = c->varname;
  108. if (!val)
  109. return;
  110. /* Insert the contents of the variable in the buffer. */
  111. for (; *val; val++)
  112. *((*(c->bp))++) = *val;
  113. }
  114. grub_err_t
  115. grub_parser_split_cmdline (const char *cmdline, grub_reader_getline_t getline,
  116. void *closure, int *argc, char ***argv)
  117. {
  118. grub_parser_state_t state = GRUB_PARSER_STATE_TEXT;
  119. /* XXX: Fixed size buffer, perhaps this buffer should be dynamically
  120. allocated. */
  121. char buffer[1024];
  122. char *bp = buffer;
  123. char *rd = (char *) cmdline;
  124. char *args;
  125. int i;
  126. struct add_var_closure c;
  127. c.vp = c.varname;
  128. c.bp = &bp;
  129. c.state = &state;
  130. *argc = 0;
  131. do
  132. {
  133. if (!rd || !*rd)
  134. {
  135. if (getline)
  136. getline (&rd, 1, closure);
  137. else
  138. break;
  139. }
  140. if (!rd)
  141. break;
  142. for (; *rd; rd++)
  143. {
  144. grub_parser_state_t newstate;
  145. char use;
  146. newstate = grub_parser_cmdline_state (state, *rd, &use);
  147. /* If a variable was being processed and this character does
  148. not describe the variable anymore, write the variable to
  149. the buffer. */
  150. add_var (newstate, &c);
  151. if (check_varstate (newstate))
  152. {
  153. if (use)
  154. *(c.vp++) = use;
  155. }
  156. else
  157. {
  158. if (newstate == GRUB_PARSER_STATE_TEXT
  159. && state != GRUB_PARSER_STATE_ESC && use == ' ')
  160. {
  161. /* Don't add more than one argument if multiple
  162. spaces are used. */
  163. if (bp != buffer && *(bp - 1))
  164. {
  165. *(bp++) = '\0';
  166. (*argc)++;
  167. }
  168. }
  169. else if (use)
  170. *(bp++) = use;
  171. }
  172. state = newstate;
  173. }
  174. }
  175. while (state != GRUB_PARSER_STATE_TEXT && !check_varstate (state));
  176. /* A special case for when the last character was part of a
  177. variable. */
  178. add_var (GRUB_PARSER_STATE_TEXT, &c);
  179. if (bp != buffer && *(bp - 1))
  180. {
  181. *(bp++) = '\0';
  182. (*argc)++;
  183. }
  184. /* Reserve memory for the return values. */
  185. args = grub_malloc (bp - buffer);
  186. if (!args)
  187. return grub_errno;
  188. grub_memcpy (args, buffer, bp - buffer);
  189. *argv = grub_malloc (sizeof (char *) * (*argc + 1));
  190. if (!*argv)
  191. {
  192. grub_free (args);
  193. return grub_errno;
  194. }
  195. /* The arguments are separated with 0's, setup argv so it points to
  196. the right values. */
  197. bp = args;
  198. for (i = 0; i < *argc; i++)
  199. {
  200. (*argv)[i] = bp;
  201. while (*bp)
  202. bp++;
  203. bp++;
  204. }
  205. return 0;
  206. }
  207. struct grub_handler_class grub_parser_class = {
  208. .name = "parser"
  209. };
  210. static grub_err_t
  211. getline (char **line, int cont __attribute__ ((unused)), void *closure)
  212. {
  213. char **source = closure;
  214. char *p;
  215. if (! *source)
  216. {
  217. *line = 0;
  218. return 0;
  219. }
  220. p = grub_strchr (*source, '\n');
  221. if (p)
  222. *line = grub_strndup (*source, p - *source);
  223. else
  224. *line = grub_strdup (*source);
  225. *source = p ? p + 1 : 0;
  226. return 0;
  227. }
  228. grub_err_t
  229. grub_parser_execute (char *source)
  230. {
  231. while (source)
  232. {
  233. char *line;
  234. grub_parser_t parser;
  235. getline (&line, 0, &source);
  236. parser = grub_parser_get_current ();
  237. parser->parse_line (line, getline, &source);
  238. grub_free (line);
  239. }
  240. return grub_errno;
  241. }