script.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /* script.c -- Functions to create an in memory description of the script. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2005,2006,2007,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. #include <grub/misc.h>
  20. #include <grub/script_sh.h>
  21. #include <grub/parser.h>
  22. #include <grub/mm.h>
  23. /* It is not possible to deallocate the memory when a syntax error was
  24. found. Because of that it is required to keep track of all memory
  25. allocations. The memory is freed in case of an error, or assigned
  26. to the parsed script when parsing was successful.
  27. In case of the normal malloc, some additional bytes are allocated
  28. for this datastructure. All reserved memory is stored in a linked
  29. list so it can be easily freed. The original memory can be found
  30. from &mem. */
  31. struct grub_script_mem
  32. {
  33. struct grub_script_mem *next;
  34. char mem;
  35. };
  36. /* Return malloc'ed memory and keep track of the allocation. */
  37. void *
  38. grub_script_malloc (struct grub_parser_param *state, grub_size_t size)
  39. {
  40. struct grub_script_mem *mem;
  41. mem = (struct grub_script_mem *) grub_malloc (size + sizeof (*mem)
  42. - sizeof (char));
  43. if (!mem)
  44. return 0;
  45. grub_dprintf ("scripting", "malloc %p\n", mem);
  46. mem->next = state->memused;
  47. state->memused = mem;
  48. return (void *) &mem->mem;
  49. }
  50. /* Free all memory described by MEM. */
  51. static void
  52. grub_script_mem_free (struct grub_script_mem *mem)
  53. {
  54. struct grub_script_mem *memfree;
  55. while (mem)
  56. {
  57. memfree = mem->next;
  58. grub_dprintf ("scripting", "free %p\n", mem);
  59. grub_free (mem);
  60. mem = memfree;
  61. }
  62. }
  63. /* Start recording memory usage. Returns the memory that should be
  64. restored when calling stop. */
  65. struct grub_script_mem *
  66. grub_script_mem_record (struct grub_parser_param *state)
  67. {
  68. struct grub_script_mem *mem = state->memused;
  69. state->memused = 0;
  70. return mem;
  71. }
  72. /* Stop recording memory usage. Restore previous recordings using
  73. RESTORE. Return the recorded memory. */
  74. struct grub_script_mem *
  75. grub_script_mem_record_stop (struct grub_parser_param *state,
  76. struct grub_script_mem *restore)
  77. {
  78. struct grub_script_mem *mem = state->memused;
  79. state->memused = restore;
  80. return mem;
  81. }
  82. /* Free the memory reserved for CMD and all of it's children. */
  83. void
  84. grub_script_free (struct grub_script *script)
  85. {
  86. if (!script)
  87. return;
  88. grub_script_mem_free (script->mem);
  89. grub_free (script);
  90. }
  91. /* Extend the argument arg with a variable or string of text. If ARG
  92. is zero a new list is created. */
  93. struct grub_script_arg *
  94. grub_script_arg_add (struct grub_parser_param *state,
  95. struct grub_script_arg *arg, grub_script_arg_type_t type,
  96. char *str)
  97. {
  98. struct grub_script_arg *argpart;
  99. struct grub_script_arg *ll;
  100. int len;
  101. argpart =
  102. (struct grub_script_arg *) grub_script_malloc (state, sizeof (*arg));
  103. if (!argpart)
  104. return arg;
  105. argpart->type = type;
  106. len = grub_strlen (str) + 1;
  107. argpart->str = grub_script_malloc (state, len);
  108. if (!argpart->str)
  109. return arg; /* argpart is freed later, during grub_script_free. */
  110. grub_memcpy (argpart->str, str, len);
  111. argpart->next = 0;
  112. if (!arg)
  113. return argpart;
  114. for (ll = arg; ll->next; ll = ll->next);
  115. ll->next = argpart;
  116. return arg;
  117. }
  118. /* Add the argument ARG to the end of the argument list LIST. If LIST
  119. is zero, a new list will be created. */
  120. struct grub_script_arglist *
  121. grub_script_add_arglist (struct grub_parser_param *state,
  122. struct grub_script_arglist *list,
  123. struct grub_script_arg *arg)
  124. {
  125. struct grub_script_arglist *link;
  126. struct grub_script_arglist *ll;
  127. grub_dprintf ("scripting", "arglist\n");
  128. link =
  129. (struct grub_script_arglist *) grub_script_malloc (state, sizeof (*link));
  130. if (!link)
  131. return list;
  132. link->next = 0;
  133. link->arg = arg;
  134. link->argcount = 0;
  135. if (!list)
  136. {
  137. link->argcount++;
  138. return link;
  139. }
  140. list->argcount++;
  141. /* Look up the last link in the chain. */
  142. for (ll = list; ll->next; ll = ll->next);
  143. ll->next = link;
  144. return list;
  145. }
  146. /* Create a command that describes a single command line. CMDLINE
  147. contains the name of the command that should be executed. ARGLIST
  148. holds all arguments for this command. */
  149. struct grub_script_cmd *
  150. grub_script_create_cmdline (struct grub_parser_param *state,
  151. struct grub_script_arglist *arglist)
  152. {
  153. struct grub_script_cmdline *cmd;
  154. grub_dprintf ("scripting", "cmdline\n");
  155. cmd = grub_script_malloc (state, sizeof (*cmd));
  156. if (!cmd)
  157. return 0;
  158. cmd->cmd.exec = grub_script_execute_cmdline;
  159. cmd->cmd.next = 0;
  160. cmd->arglist = arglist;
  161. return (struct grub_script_cmd *) cmd;
  162. }
  163. /* Create a command that functions as an if statement. If BOOL is
  164. evaluated to true (the value is returned in envvar '?'), the
  165. interpreter will run the command TRUE, otherwise the interpreter
  166. runs the command FALSE. */
  167. struct grub_script_cmd *
  168. grub_script_create_cmdif (struct grub_parser_param *state,
  169. struct grub_script_cmd *exec_to_evaluate,
  170. struct grub_script_cmd *exec_on_true,
  171. struct grub_script_cmd *exec_on_false)
  172. {
  173. struct grub_script_cmdif *cmd;
  174. grub_dprintf ("scripting", "cmdif\n");
  175. cmd = grub_script_malloc (state, sizeof (*cmd));
  176. if (!cmd)
  177. return 0;
  178. cmd->cmd.exec = grub_script_execute_cmdif;
  179. cmd->cmd.next = 0;
  180. cmd->exec_to_evaluate = exec_to_evaluate;
  181. cmd->exec_on_true = exec_on_true;
  182. cmd->exec_on_false = exec_on_false;
  183. return (struct grub_script_cmd *) cmd;
  184. }
  185. /* Create a command that functions as a for statement. */
  186. struct grub_script_cmd *
  187. grub_script_create_cmdfor (struct grub_parser_param *state,
  188. struct grub_script_arg *name,
  189. struct grub_script_arglist *words,
  190. struct grub_script_cmd *list)
  191. {
  192. struct grub_script_cmdfor *cmd;
  193. grub_dprintf ("scripting", "cmdfor\n");
  194. cmd = grub_script_malloc (state, sizeof (*cmd));
  195. if (! cmd)
  196. return 0;
  197. cmd->cmd.exec = grub_script_execute_cmdfor;
  198. cmd->cmd.next = 0;
  199. cmd->name = name;
  200. cmd->words = words;
  201. cmd->list = list;
  202. return (struct grub_script_cmd *) cmd;
  203. }
  204. /* Create a "while" or "until" command. */
  205. struct grub_script_cmd *
  206. grub_script_create_cmdwhile (struct grub_parser_param *state,
  207. struct grub_script_cmd *cond,
  208. struct grub_script_cmd *list,
  209. int is_an_until_loop)
  210. {
  211. struct grub_script_cmdwhile *cmd;
  212. cmd = grub_script_malloc (state, sizeof (*cmd));
  213. if (! cmd)
  214. return 0;
  215. cmd->cmd.exec = grub_script_execute_cmdwhile;
  216. cmd->cmd.next = 0;
  217. cmd->cond = cond;
  218. cmd->list = list;
  219. cmd->until = is_an_until_loop;
  220. return (struct grub_script_cmd *) cmd;
  221. }
  222. /* Create a command that adds a menu entry to the menu. Title is an
  223. argument that is parsed to generate a string that can be used as
  224. the title. The sourcecode for this entry is passed in SOURCECODE.
  225. The options for this entry are passed in OPTIONS. */
  226. struct grub_script_cmd *
  227. grub_script_create_cmdmenu (struct grub_parser_param *state,
  228. struct grub_script_arglist *arglist,
  229. char *sourcecode, int options)
  230. {
  231. struct grub_script_cmd_menuentry *cmd;
  232. cmd = grub_script_malloc (state, sizeof (*cmd));
  233. if (!cmd)
  234. return 0;
  235. cmd->cmd.exec = grub_script_execute_menuentry;
  236. cmd->cmd.next = 0;
  237. cmd->sourcecode = sourcecode;
  238. cmd->arglist = arglist;
  239. cmd->options = options;
  240. return (struct grub_script_cmd *) cmd;
  241. }
  242. /* Create a block of commands. CMD contains the command that should
  243. be added at the end of CMDBLOCK's list. If CMDBLOCK is zero, a new
  244. cmdblock will be created. */
  245. struct grub_script_cmd *
  246. grub_script_add_cmd (struct grub_parser_param *state,
  247. struct grub_script_cmdblock *cmdblock,
  248. struct grub_script_cmd *cmd)
  249. {
  250. struct grub_script_cmd *ptr;
  251. grub_dprintf ("scripting", "cmdblock\n");
  252. if (!cmd)
  253. return (struct grub_script_cmd *) cmdblock;
  254. if (!cmdblock)
  255. {
  256. cmdblock = grub_script_malloc (state, sizeof (*cmdblock));
  257. if (!cmdblock)
  258. return 0;
  259. cmdblock->cmd.exec = grub_script_execute_cmdblock;
  260. cmdblock->cmd.next = 0;
  261. cmdblock->cmdlist = cmd;
  262. cmd->next = 0;
  263. }
  264. else
  265. {
  266. if (!cmdblock->cmdlist)
  267. cmdblock->cmdlist = cmd;
  268. else
  269. {
  270. ptr = cmdblock->cmdlist;
  271. while (ptr->next)
  272. ptr = ptr->next;
  273. ptr->next = cmd;
  274. }
  275. }
  276. return (struct grub_script_cmd *) cmdblock;
  277. }
  278. struct grub_script *
  279. grub_script_create (struct grub_script_cmd *cmd, struct grub_script_mem *mem)
  280. {
  281. struct grub_script *parsed;
  282. parsed = grub_malloc (sizeof (*parsed));
  283. if (!parsed)
  284. {
  285. grub_script_mem_free (mem);
  286. grub_free (cmd);
  287. return 0;
  288. }
  289. parsed->mem = mem;
  290. parsed->cmd = cmd;
  291. return parsed;
  292. }
  293. /* Parse the script passed in SCRIPT and return the parsed
  294. datastructure that is ready to be interpreted. */
  295. struct grub_script *
  296. grub_script_parse (char *script, grub_reader_getline_t getline, void *closure)
  297. {
  298. struct grub_script *parsed;
  299. struct grub_script_mem *membackup;
  300. struct grub_lexer_param *lexstate;
  301. struct grub_parser_param *parsestate;
  302. parsed = grub_malloc (sizeof (*parsed));
  303. if (!parsed)
  304. return 0;
  305. parsestate = grub_zalloc (sizeof (*parsestate));
  306. if (!parsestate)
  307. return 0;
  308. /* Initialize the lexer. */
  309. lexstate = grub_script_lexer_init (parsestate, script, getline, closure);
  310. if (!lexstate)
  311. {
  312. grub_free (parsed);
  313. grub_free (parsestate);
  314. return 0;
  315. }
  316. parsestate->lexerstate = lexstate;
  317. membackup = grub_script_mem_record (parsestate);
  318. /* Parse the script. */
  319. if (grub_script_yyparse (parsestate) || parsestate->err)
  320. {
  321. struct grub_script_mem *memfree;
  322. memfree = grub_script_mem_record_stop (parsestate, membackup);
  323. grub_script_mem_free (memfree);
  324. grub_script_lexer_fini (lexstate);
  325. grub_free (parsestate);
  326. return 0;
  327. }
  328. parsed->mem = grub_script_mem_record_stop (parsestate, membackup);
  329. parsed->cmd = parsestate->parsed;
  330. grub_script_lexer_fini (lexstate);
  331. grub_free (parsestate);
  332. return parsed;
  333. }