parser.y 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /* parser.y - The scripting parser. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2005,2006,2007,2008,2009,2010 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. %{
  20. #include <grub/script_sh.h>
  21. #include <grub/mm.h>
  22. #include <grub/misc.h>
  23. #include <grub/i18n.h>
  24. #define YYFREE grub_free
  25. #define YYMALLOC grub_malloc
  26. #define YYLTYPE_IS_TRIVIAL 0
  27. #define YYENABLE_NLS 0
  28. #include "grub_script.tab.h"
  29. #pragma GCC diagnostic ignored "-Wmissing-declarations"
  30. %}
  31. %union {
  32. struct grub_script_cmd *cmd;
  33. struct grub_script_arglist *arglist;
  34. struct grub_script_arg *arg;
  35. char *string;
  36. struct {
  37. unsigned offset;
  38. struct grub_script_mem *memory;
  39. struct grub_script *scripts;
  40. };
  41. }
  42. %token GRUB_PARSER_TOKEN_BAD
  43. %token GRUB_PARSER_TOKEN_EOF 0 "end-of-input"
  44. %token GRUB_PARSER_TOKEN_NEWLINE "\n"
  45. %token GRUB_PARSER_TOKEN_AND "&&"
  46. %token GRUB_PARSER_TOKEN_OR "||"
  47. %token GRUB_PARSER_TOKEN_SEMI2 ";;"
  48. %token GRUB_PARSER_TOKEN_PIPE "|"
  49. %token GRUB_PARSER_TOKEN_AMP "&"
  50. %token GRUB_PARSER_TOKEN_SEMI ";"
  51. %token GRUB_PARSER_TOKEN_LBR "{"
  52. %token GRUB_PARSER_TOKEN_RBR "}"
  53. %token GRUB_PARSER_TOKEN_NOT "!"
  54. %token GRUB_PARSER_TOKEN_LSQBR2 "["
  55. %token GRUB_PARSER_TOKEN_RSQBR2 "]"
  56. %token GRUB_PARSER_TOKEN_LT "<"
  57. %token GRUB_PARSER_TOKEN_GT ">"
  58. %token <arg> GRUB_PARSER_TOKEN_CASE "case"
  59. %token <arg> GRUB_PARSER_TOKEN_DO "do"
  60. %token <arg> GRUB_PARSER_TOKEN_DONE "done"
  61. %token <arg> GRUB_PARSER_TOKEN_ELIF "elif"
  62. %token <arg> GRUB_PARSER_TOKEN_ELSE "else"
  63. %token <arg> GRUB_PARSER_TOKEN_ESAC "esac"
  64. %token <arg> GRUB_PARSER_TOKEN_FI "fi"
  65. %token <arg> GRUB_PARSER_TOKEN_FOR "for"
  66. %token <arg> GRUB_PARSER_TOKEN_IF "if"
  67. %token <arg> GRUB_PARSER_TOKEN_IN "in"
  68. %token <arg> GRUB_PARSER_TOKEN_SELECT "select"
  69. %token <arg> GRUB_PARSER_TOKEN_THEN "then"
  70. %token <arg> GRUB_PARSER_TOKEN_UNTIL "until"
  71. %token <arg> GRUB_PARSER_TOKEN_WHILE "while"
  72. %token <arg> GRUB_PARSER_TOKEN_FUNCTION "function"
  73. %token <arg> GRUB_PARSER_TOKEN_NAME "name"
  74. %token <arg> GRUB_PARSER_TOKEN_WORD "word"
  75. %type <arg> block block0
  76. %type <arglist> word argument arguments0 arguments1
  77. %type <cmd> script_init script
  78. %type <cmd> grubcmd ifclause ifcmd forcmd whilecmd untilcmd
  79. %type <cmd> command commands1 statement
  80. %pure-parser
  81. %lex-param { struct grub_parser_param *state };
  82. %parse-param { struct grub_parser_param *state };
  83. %start script_init
  84. %%
  85. /* It should be possible to do this in a clean way... */
  86. script_init: { state->err = 0; } script { state->parsed = $2; state->err = 0; }
  87. ;
  88. script: newlines0
  89. {
  90. $$ = 0;
  91. }
  92. | script statement delimiter newlines0
  93. {
  94. $$ = grub_script_append_cmd (state, $1, $2);
  95. }
  96. | error
  97. {
  98. $$ = 0;
  99. yyerror (state, N_("Incorrect command"));
  100. yyerrok;
  101. }
  102. ;
  103. newlines0: /* Empty */ | newlines1 ;
  104. newlines1: newlines0 "\n" ;
  105. delimiter: ";"
  106. | "\n"
  107. ;
  108. delimiters0: /* Empty */ | delimiters1 ;
  109. delimiters1: delimiter
  110. | delimiters1 "\n"
  111. ;
  112. word: GRUB_PARSER_TOKEN_NAME { $$ = grub_script_add_arglist (state, 0, $1); }
  113. | GRUB_PARSER_TOKEN_WORD { $$ = grub_script_add_arglist (state, 0, $1); }
  114. ;
  115. statement: command { $$ = $1; }
  116. | function { $$ = 0; }
  117. ;
  118. argument : "case" { $$ = grub_script_add_arglist (state, 0, $1); }
  119. | "do" { $$ = grub_script_add_arglist (state, 0, $1); }
  120. | "done" { $$ = grub_script_add_arglist (state, 0, $1); }
  121. | "elif" { $$ = grub_script_add_arglist (state, 0, $1); }
  122. | "else" { $$ = grub_script_add_arglist (state, 0, $1); }
  123. | "esac" { $$ = grub_script_add_arglist (state, 0, $1); }
  124. | "fi" { $$ = grub_script_add_arglist (state, 0, $1); }
  125. | "for" { $$ = grub_script_add_arglist (state, 0, $1); }
  126. | "if" { $$ = grub_script_add_arglist (state, 0, $1); }
  127. | "in" { $$ = grub_script_add_arglist (state, 0, $1); }
  128. | "select" { $$ = grub_script_add_arglist (state, 0, $1); }
  129. | "then" { $$ = grub_script_add_arglist (state, 0, $1); }
  130. | "until" { $$ = grub_script_add_arglist (state, 0, $1); }
  131. | "while" { $$ = grub_script_add_arglist (state, 0, $1); }
  132. | "function" { $$ = grub_script_add_arglist (state, 0, $1); }
  133. | word { $$ = $1; }
  134. ;
  135. /*
  136. Block parameter is passed to commands in two forms: as unparsed
  137. string and as pre-parsed grub_script object. Passing as grub_script
  138. object makes memory management difficult, because:
  139. (1) Command may want to keep a reference to grub_script objects for
  140. later use, so script framework may not free the grub_script
  141. object after command completes.
  142. (2) Command may get called multiple times with same grub_script
  143. object under loops, so we should not let command implementation
  144. to free the grub_script object.
  145. To solve above problems, we rely on reference counting for
  146. grub_script objects. Commands that want to keep the grub_script
  147. object must take a reference to it.
  148. Other complexity comes with arbitrary nesting of grub_script
  149. objects: a grub_script object may have commands with several block
  150. parameters, and each block parameter may further contain multiple
  151. block parameters nested. We use temporary variable, state->scripts
  152. to collect nested child scripts (that are linked by siblings and
  153. children members), and will build grub_scripts tree from bottom.
  154. */
  155. block: "{"
  156. {
  157. grub_script_lexer_ref (state->lexerstate);
  158. $<offset>$ = grub_script_lexer_record_start (state);
  159. $<memory>$ = grub_script_mem_record (state);
  160. /* save currently known scripts. */
  161. $<scripts>$ = state->scripts;
  162. state->scripts = 0;
  163. }
  164. commands1 delimiters0 "}"
  165. {
  166. char *p;
  167. struct grub_script_mem *memory;
  168. struct grub_script *s = $<scripts>2;
  169. memory = grub_script_mem_record_stop (state, $<memory>2);
  170. if ((p = grub_script_lexer_record_stop (state, $<offset>2)))
  171. *grub_strrchr (p, '}') = '\0';
  172. $$ = grub_script_arg_add (state, 0, GRUB_SCRIPT_ARG_TYPE_BLOCK, p);
  173. if (! $$ || ! ($$->script = grub_script_create ($3, memory)))
  174. grub_script_mem_free (memory);
  175. else {
  176. /* attach nested scripts to $$->script as children */
  177. $$->script->children = state->scripts;
  178. /* restore old scripts; append $$->script to siblings. */
  179. state->scripts = $<scripts>2 ?: $$->script;
  180. if (s) {
  181. while (s->next_siblings)
  182. s = s->next_siblings;
  183. s->next_siblings = $$->script;
  184. }
  185. }
  186. grub_script_lexer_deref (state->lexerstate);
  187. }
  188. ;
  189. block0: /* Empty */ { $$ = 0; }
  190. | block { $$ = $1; }
  191. ;
  192. arguments0: /* Empty */ { $$ = 0; }
  193. | arguments1 { $$ = $1; }
  194. ;
  195. arguments1: argument arguments0
  196. {
  197. if ($1 && $2)
  198. {
  199. $1->next = $2;
  200. $1->argcount += $2->argcount;
  201. $2->argcount = 0;
  202. }
  203. $$ = $1;
  204. }
  205. ;
  206. grubcmd: word arguments0 block0
  207. {
  208. struct grub_script_arglist *x = $2;
  209. if ($3)
  210. x = grub_script_add_arglist (state, $2, $3);
  211. if ($1 && x) {
  212. $1->next = x;
  213. $1->argcount += x->argcount;
  214. x->argcount = 0;
  215. }
  216. $$ = grub_script_create_cmdline (state, $1);
  217. }
  218. ;
  219. /* A single command. */
  220. command: grubcmd { $$ = $1; }
  221. | ifcmd { $$ = $1; }
  222. | forcmd { $$ = $1; }
  223. | whilecmd { $$ = $1; }
  224. | untilcmd { $$ = $1; }
  225. ;
  226. /* A list of commands. */
  227. commands1: newlines0 command
  228. {
  229. $$ = grub_script_append_cmd (state, 0, $2);
  230. }
  231. | commands1 delimiters1 command
  232. {
  233. $$ = grub_script_append_cmd (state, $1, $3);
  234. }
  235. ;
  236. function: "function" "name"
  237. {
  238. grub_script_lexer_ref (state->lexerstate);
  239. state->func_mem = grub_script_mem_record (state);
  240. $<scripts>$ = state->scripts;
  241. state->scripts = 0;
  242. }
  243. newlines0 "{" commands1 delimiters1 "}"
  244. {
  245. struct grub_script *script;
  246. state->func_mem = grub_script_mem_record_stop (state,
  247. state->func_mem);
  248. script = grub_script_create ($6, state->func_mem);
  249. if (! script)
  250. grub_script_mem_free (state->func_mem);
  251. else {
  252. script->children = state->scripts;
  253. if (!grub_script_function_create ($2, script))
  254. grub_script_free (script);
  255. }
  256. state->scripts = $<scripts>3;
  257. grub_script_lexer_deref (state->lexerstate);
  258. }
  259. ;
  260. ifcmd: "if"
  261. {
  262. grub_script_lexer_ref (state->lexerstate);
  263. }
  264. ifclause "fi"
  265. {
  266. $$ = $3;
  267. grub_script_lexer_deref (state->lexerstate);
  268. }
  269. ;
  270. ifclause: commands1 delimiters1 "then" commands1 delimiters1
  271. {
  272. $$ = grub_script_create_cmdif (state, $1, $4, 0);
  273. }
  274. | commands1 delimiters1 "then" commands1 delimiters1 "else" commands1 delimiters1
  275. {
  276. $$ = grub_script_create_cmdif (state, $1, $4, $7);
  277. }
  278. | commands1 delimiters1 "then" commands1 delimiters1 "elif" ifclause
  279. {
  280. $$ = grub_script_create_cmdif (state, $1, $4, $7);
  281. }
  282. ;
  283. forcmd: "for" "name"
  284. {
  285. grub_script_lexer_ref (state->lexerstate);
  286. }
  287. "in" arguments0 delimiters1 "do" commands1 delimiters1 "done"
  288. {
  289. $$ = grub_script_create_cmdfor (state, $2, $5, $8);
  290. grub_script_lexer_deref (state->lexerstate);
  291. }
  292. ;
  293. whilecmd: "while"
  294. {
  295. grub_script_lexer_ref (state->lexerstate);
  296. }
  297. commands1 delimiters1 "do" commands1 delimiters1 "done"
  298. {
  299. $$ = grub_script_create_cmdwhile (state, $3, $6, 0);
  300. grub_script_lexer_deref (state->lexerstate);
  301. }
  302. ;
  303. untilcmd: "until"
  304. {
  305. grub_script_lexer_ref (state->lexerstate);
  306. }
  307. commands1 delimiters1 "do" commands1 delimiters1 "done"
  308. {
  309. $$ = grub_script_create_cmdwhile (state, $3, $6, 1);
  310. grub_script_lexer_deref (state->lexerstate);
  311. }
  312. ;