parser.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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,2021 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/buffer.h>
  21. #include <grub/env.h>
  22. #include <grub/misc.h>
  23. #include <grub/mm.h>
  24. /* All the possible state transitions on the command line. If a
  25. transition can not be found, it is assumed that there is no
  26. transition and keep_value is assumed to be 1. */
  27. static struct grub_parser_state_transition state_transitions[] = {
  28. {GRUB_PARSER_STATE_TEXT, GRUB_PARSER_STATE_QUOTE, '\'', 0},
  29. {GRUB_PARSER_STATE_TEXT, GRUB_PARSER_STATE_DQUOTE, '\"', 0},
  30. {GRUB_PARSER_STATE_TEXT, GRUB_PARSER_STATE_VAR, '$', 0},
  31. {GRUB_PARSER_STATE_TEXT, GRUB_PARSER_STATE_ESC, '\\', 0},
  32. {GRUB_PARSER_STATE_ESC, GRUB_PARSER_STATE_TEXT, 0, 1},
  33. {GRUB_PARSER_STATE_QUOTE, GRUB_PARSER_STATE_TEXT, '\'', 0},
  34. {GRUB_PARSER_STATE_DQUOTE, GRUB_PARSER_STATE_TEXT, '\"', 0},
  35. {GRUB_PARSER_STATE_DQUOTE, GRUB_PARSER_STATE_QVAR, '$', 0},
  36. {GRUB_PARSER_STATE_VAR, GRUB_PARSER_STATE_VARNAME2, '{', 0},
  37. {GRUB_PARSER_STATE_VAR, GRUB_PARSER_STATE_VARNAME, 0, 1},
  38. {GRUB_PARSER_STATE_VARNAME, GRUB_PARSER_STATE_TEXT, ' ', 1},
  39. {GRUB_PARSER_STATE_VARNAME, GRUB_PARSER_STATE_TEXT, '\t', 1},
  40. {GRUB_PARSER_STATE_VARNAME2, GRUB_PARSER_STATE_TEXT, '}', 0},
  41. {GRUB_PARSER_STATE_QVAR, GRUB_PARSER_STATE_QVARNAME2, '{', 0},
  42. {GRUB_PARSER_STATE_QVAR, GRUB_PARSER_STATE_QVARNAME, 0, 1},
  43. {GRUB_PARSER_STATE_QVARNAME, GRUB_PARSER_STATE_TEXT, '\"', 0},
  44. {GRUB_PARSER_STATE_QVARNAME, GRUB_PARSER_STATE_DQUOTE, ' ', 1},
  45. {GRUB_PARSER_STATE_QVARNAME, GRUB_PARSER_STATE_DQUOTE, '\t', 1},
  46. {GRUB_PARSER_STATE_QVARNAME2, GRUB_PARSER_STATE_DQUOTE, '}', 0},
  47. {0, 0, 0, 0}
  48. };
  49. /* Determines the state following STATE, determined by C. */
  50. grub_parser_state_t
  51. grub_parser_cmdline_state (grub_parser_state_t state, char c, char *result)
  52. {
  53. struct grub_parser_state_transition *transition;
  54. struct grub_parser_state_transition default_transition;
  55. default_transition.to_state = state;
  56. default_transition.keep_value = 1;
  57. /* Look for a good translation. */
  58. for (transition = state_transitions; transition->from_state; transition++)
  59. {
  60. if (transition->from_state != state)
  61. continue;
  62. /* An exact match was found, use it. */
  63. if (transition->input == c)
  64. break;
  65. if (grub_isspace (transition->input) && !grub_isalpha (c)
  66. && !grub_isdigit (c) && c != '_')
  67. break;
  68. /* A less perfect match was found, use this one if no exact
  69. match can be found. */
  70. if (transition->input == 0)
  71. break;
  72. }
  73. if (!transition->from_state)
  74. transition = &default_transition;
  75. if (transition->keep_value)
  76. *result = c;
  77. else
  78. *result = 0;
  79. return transition->to_state;
  80. }
  81. /* Helper for grub_parser_split_cmdline. */
  82. static inline 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. static grub_err_t
  91. add_var (grub_buffer_t varname, grub_buffer_t buf,
  92. grub_parser_state_t state, grub_parser_state_t newstate)
  93. {
  94. const char *val;
  95. /* Check if a variable was being read in and the end of the name
  96. was reached. */
  97. if (!(check_varstate (state) && !check_varstate (newstate)))
  98. return GRUB_ERR_NONE;
  99. if (grub_buffer_append_char (varname, '\0') != GRUB_ERR_NONE)
  100. return grub_errno;
  101. val = grub_env_get ((const char *) grub_buffer_peek_data (varname));
  102. grub_buffer_reset (varname);
  103. if (!val)
  104. return GRUB_ERR_NONE;
  105. /* Insert the contents of the variable in the buffer. */
  106. return grub_buffer_append_data (buf, val, grub_strlen (val));
  107. }
  108. static grub_err_t
  109. terminate_arg (grub_buffer_t buffer, int *argc)
  110. {
  111. grub_size_t unread = grub_buffer_get_unread_bytes (buffer);
  112. if (unread == 0)
  113. return GRUB_ERR_NONE;
  114. if (*(const char *) grub_buffer_peek_data_at (buffer, unread - 1) == '\0')
  115. return GRUB_ERR_NONE;
  116. if (grub_buffer_append_char (buffer, '\0') != GRUB_ERR_NONE)
  117. return grub_errno;
  118. (*argc)++;
  119. return GRUB_ERR_NONE;
  120. }
  121. static grub_err_t
  122. process_char (char c, grub_buffer_t buffer, grub_buffer_t varname,
  123. grub_parser_state_t state, int *argc,
  124. grub_parser_state_t *newstate)
  125. {
  126. char use;
  127. *newstate = grub_parser_cmdline_state (state, c, &use);
  128. /*
  129. * If a variable was being processed and this character does
  130. * not describe the variable anymore, write the variable to
  131. * the buffer.
  132. */
  133. if (add_var (varname, buffer, state, *newstate) != GRUB_ERR_NONE)
  134. return grub_errno;
  135. if (check_varstate (*newstate))
  136. {
  137. if (use)
  138. return grub_buffer_append_char (varname, use);
  139. }
  140. else if (*newstate == GRUB_PARSER_STATE_TEXT &&
  141. state != GRUB_PARSER_STATE_ESC && grub_isspace (use))
  142. {
  143. /*
  144. * Don't add more than one argument if multiple
  145. * spaces are used.
  146. */
  147. return terminate_arg (buffer, argc);
  148. }
  149. else if (use)
  150. return grub_buffer_append_char (buffer, use);
  151. return GRUB_ERR_NONE;
  152. }
  153. grub_err_t
  154. grub_parser_split_cmdline (const char *cmdline,
  155. grub_reader_getline_t getline, void *getline_data,
  156. int *argc, char ***argv)
  157. {
  158. grub_parser_state_t state = GRUB_PARSER_STATE_TEXT;
  159. grub_buffer_t buffer, varname;
  160. char *rd = (char *) cmdline;
  161. char *rp = rd;
  162. int i;
  163. *argc = 0;
  164. *argv = NULL;
  165. buffer = grub_buffer_new (1024);
  166. if (buffer == NULL)
  167. return grub_errno;
  168. varname = grub_buffer_new (200);
  169. if (varname == NULL)
  170. goto fail;
  171. do
  172. {
  173. if (rp == NULL || *rp == '\0')
  174. {
  175. if (rd != cmdline)
  176. {
  177. grub_free (rd);
  178. rd = rp = NULL;
  179. }
  180. if (getline)
  181. {
  182. getline (&rd, 1, getline_data);
  183. rp = rd;
  184. }
  185. else
  186. break;
  187. }
  188. if (!rd)
  189. break;
  190. for (; *rp != '\0'; rp++)
  191. {
  192. grub_parser_state_t newstate;
  193. if (process_char (*rp, buffer, varname, state, argc,
  194. &newstate) != GRUB_ERR_NONE)
  195. goto fail;
  196. state = newstate;
  197. }
  198. }
  199. while (state != GRUB_PARSER_STATE_TEXT && !check_varstate (state));
  200. /* A special case for when the last character was part of a
  201. variable. */
  202. if (add_var (varname, buffer, state, GRUB_PARSER_STATE_TEXT) != GRUB_ERR_NONE)
  203. goto fail;
  204. /* Ensure that the last argument is terminated. */
  205. if (terminate_arg (buffer, argc) != GRUB_ERR_NONE)
  206. goto fail;
  207. /* If there are no args, then we're done. */
  208. if (!*argc)
  209. {
  210. grub_errno = GRUB_ERR_NONE;
  211. goto out;
  212. }
  213. *argv = grub_calloc (*argc + 1, sizeof (char *));
  214. if (!*argv)
  215. goto fail;
  216. /* The arguments are separated with 0's, setup argv so it points to
  217. the right values. */
  218. for (i = 0; i < *argc; i++)
  219. {
  220. char *arg;
  221. if (i > 0)
  222. {
  223. if (grub_buffer_advance_read_pos (buffer, 1) != GRUB_ERR_NONE)
  224. goto fail;
  225. }
  226. arg = (char *) grub_buffer_peek_data (buffer);
  227. if (arg == NULL ||
  228. grub_buffer_advance_read_pos (buffer, grub_strlen (arg)) != GRUB_ERR_NONE)
  229. goto fail;
  230. (*argv)[i] = arg;
  231. }
  232. /* Keep memory for the return values. */
  233. grub_buffer_take_data (buffer);
  234. grub_errno = GRUB_ERR_NONE;
  235. out:
  236. if (rd != cmdline)
  237. grub_free (rd);
  238. grub_buffer_free (buffer);
  239. grub_buffer_free (varname);
  240. return grub_errno;
  241. fail:
  242. grub_free (*argv);
  243. *argv = NULL;
  244. *argc = 0;
  245. goto out;
  246. }
  247. /* Helper for grub_parser_execute. */
  248. static grub_err_t
  249. grub_parser_execute_getline (char **line, int cont __attribute__ ((unused)),
  250. void *data)
  251. {
  252. char **source = data;
  253. char *p;
  254. if (!*source)
  255. {
  256. *line = 0;
  257. return 0;
  258. }
  259. p = grub_strchr (*source, '\n');
  260. if (p)
  261. *line = grub_strndup (*source, p - *source);
  262. else
  263. *line = grub_strdup (*source);
  264. *source = p ? p + 1 : 0;
  265. return 0;
  266. }
  267. grub_err_t
  268. grub_parser_execute (char *source)
  269. {
  270. while (source)
  271. {
  272. char *line;
  273. grub_parser_execute_getline (&line, 0, &source);
  274. grub_rescue_parse_line (line, grub_parser_execute_getline, &source);
  275. grub_free (line);
  276. grub_print_error ();
  277. }
  278. return grub_errno;
  279. }