script.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. 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. struct grub_script *s;
  87. struct grub_script *t;
  88. if (! script)
  89. return;
  90. if (script->mem)
  91. grub_script_mem_free (script->mem);
  92. s = script->children;
  93. while (s) {
  94. t = s->next_siblings;
  95. grub_script_unref (s);
  96. s = t;
  97. }
  98. grub_free (script);
  99. }
  100. /* Extend the argument arg with a variable or string of text. If ARG
  101. is zero a new list is created. */
  102. struct grub_script_arg *
  103. grub_script_arg_add (struct grub_parser_param *state,
  104. struct grub_script_arg *arg, grub_script_arg_type_t type,
  105. char *str)
  106. {
  107. struct grub_script_arg *argpart;
  108. struct grub_script_arg *ll;
  109. int len;
  110. argpart =
  111. (struct grub_script_arg *) grub_script_malloc (state, sizeof (*arg));
  112. if (!argpart)
  113. return arg;
  114. argpart->type = type;
  115. argpart->script = 0;
  116. len = grub_strlen (str) + 1;
  117. argpart->str = grub_script_malloc (state, len);
  118. if (!argpart->str)
  119. return arg; /* argpart is freed later, during grub_script_free. */
  120. grub_memcpy (argpart->str, str, len);
  121. argpart->next = 0;
  122. if (!arg)
  123. return argpart;
  124. for (ll = arg; ll->next; ll = ll->next);
  125. ll->next = argpart;
  126. return arg;
  127. }
  128. /* Add the argument ARG to the end of the argument list LIST. If LIST
  129. is zero, a new list will be created. */
  130. struct grub_script_arglist *
  131. grub_script_add_arglist (struct grub_parser_param *state,
  132. struct grub_script_arglist *list,
  133. struct grub_script_arg *arg)
  134. {
  135. struct grub_script_arglist *link;
  136. struct grub_script_arglist *ll;
  137. grub_dprintf ("scripting", "arglist\n");
  138. link =
  139. (struct grub_script_arglist *) grub_script_malloc (state, sizeof (*link));
  140. if (!link)
  141. return list;
  142. link->next = 0;
  143. link->arg = arg;
  144. link->argcount = 0;
  145. if (!list)
  146. {
  147. link->argcount++;
  148. return link;
  149. }
  150. list->argcount++;
  151. /* Look up the last link in the chain. */
  152. for (ll = list; ll->next; ll = ll->next);
  153. ll->next = link;
  154. return list;
  155. }
  156. /* Create a command that describes a single command line. CMDLINE
  157. contains the name of the command that should be executed. ARGLIST
  158. holds all arguments for this command. */
  159. struct grub_script_cmd *
  160. grub_script_create_cmdline (struct grub_parser_param *state,
  161. struct grub_script_arglist *arglist)
  162. {
  163. struct grub_script_cmdline *cmd;
  164. grub_dprintf ("scripting", "cmdline\n");
  165. cmd = grub_script_malloc (state, sizeof (*cmd));
  166. if (!cmd)
  167. return 0;
  168. cmd->cmd.exec = grub_script_execute_cmdline;
  169. cmd->cmd.next = 0;
  170. cmd->arglist = arglist;
  171. return (struct grub_script_cmd *) cmd;
  172. }
  173. /* Create a command that functions as an if statement. If BOOL is
  174. evaluated to true (the value is returned in envvar '?'), the
  175. interpreter will run the command TRUE, otherwise the interpreter
  176. runs the command FALSE. */
  177. struct grub_script_cmd *
  178. grub_script_create_cmdif (struct grub_parser_param *state,
  179. struct grub_script_cmd *exec_to_evaluate,
  180. struct grub_script_cmd *exec_on_true,
  181. struct grub_script_cmd *exec_on_false)
  182. {
  183. struct grub_script_cmdif *cmd;
  184. grub_dprintf ("scripting", "cmdif\n");
  185. cmd = grub_script_malloc (state, sizeof (*cmd));
  186. if (!cmd)
  187. return 0;
  188. cmd->cmd.exec = grub_script_execute_cmdif;
  189. cmd->cmd.next = 0;
  190. cmd->exec_to_evaluate = exec_to_evaluate;
  191. cmd->exec_on_true = exec_on_true;
  192. cmd->exec_on_false = exec_on_false;
  193. return (struct grub_script_cmd *) cmd;
  194. }
  195. /* Create a command that functions as a for statement. */
  196. struct grub_script_cmd *
  197. grub_script_create_cmdfor (struct grub_parser_param *state,
  198. struct grub_script_arg *name,
  199. struct grub_script_arglist *words,
  200. struct grub_script_cmd *list)
  201. {
  202. struct grub_script_cmdfor *cmd;
  203. grub_dprintf ("scripting", "cmdfor\n");
  204. cmd = grub_script_malloc (state, sizeof (*cmd));
  205. if (! cmd)
  206. return 0;
  207. cmd->cmd.exec = grub_script_execute_cmdfor;
  208. cmd->cmd.next = 0;
  209. cmd->name = name;
  210. cmd->words = words;
  211. cmd->list = list;
  212. return (struct grub_script_cmd *) cmd;
  213. }
  214. /* Create a "while" or "until" command. */
  215. struct grub_script_cmd *
  216. grub_script_create_cmdwhile (struct grub_parser_param *state,
  217. struct grub_script_cmd *cond,
  218. struct grub_script_cmd *list,
  219. int is_an_until_loop)
  220. {
  221. struct grub_script_cmdwhile *cmd;
  222. cmd = grub_script_malloc (state, sizeof (*cmd));
  223. if (! cmd)
  224. return 0;
  225. cmd->cmd.exec = grub_script_execute_cmdwhile;
  226. cmd->cmd.next = 0;
  227. cmd->cond = cond;
  228. cmd->list = list;
  229. cmd->until = is_an_until_loop;
  230. return (struct grub_script_cmd *) cmd;
  231. }
  232. /* Create a chain of commands. LAST contains the command that should
  233. be added at the end of LIST's list. If LIST is zero, a new list
  234. will be created. */
  235. struct grub_script_cmd *
  236. grub_script_append_cmd (struct grub_parser_param *state,
  237. struct grub_script_cmd *list,
  238. struct grub_script_cmd *last)
  239. {
  240. struct grub_script_cmd *ptr;
  241. grub_dprintf ("scripting", "append command\n");
  242. if (! last)
  243. return list;
  244. if (! list)
  245. {
  246. list = grub_script_malloc (state, sizeof (*list));
  247. if (! list)
  248. return 0;
  249. list->exec = grub_script_execute_cmdlist;
  250. list->next = last;
  251. }
  252. else
  253. {
  254. ptr = list;
  255. while (ptr->next)
  256. ptr = ptr->next;
  257. ptr->next = last;
  258. }
  259. return list;
  260. }
  261. struct grub_script *
  262. grub_script_create (struct grub_script_cmd *cmd, struct grub_script_mem *mem)
  263. {
  264. struct grub_script *parsed;
  265. parsed = grub_malloc (sizeof (*parsed));
  266. if (! parsed)
  267. return 0;
  268. parsed->mem = mem;
  269. parsed->cmd = cmd;
  270. parsed->refcnt = 0;
  271. parsed->children = 0;
  272. parsed->next_siblings = 0;
  273. return parsed;
  274. }
  275. /* Parse the script passed in SCRIPT and return the parsed
  276. datastructure that is ready to be interpreted. */
  277. struct grub_script *
  278. grub_script_parse (char *script,
  279. grub_reader_getline_t getline, void *getline_data)
  280. {
  281. struct grub_script *parsed;
  282. struct grub_script_mem *membackup;
  283. struct grub_lexer_param *lexstate;
  284. struct grub_parser_param *parsestate;
  285. parsed = grub_script_create (0, 0);
  286. if (!parsed)
  287. return 0;
  288. parsestate = grub_zalloc (sizeof (*parsestate));
  289. if (!parsestate)
  290. {
  291. grub_free (parsed);
  292. return 0;
  293. }
  294. /* Initialize the lexer. */
  295. lexstate = grub_script_lexer_init (parsestate, script,
  296. getline, getline_data);
  297. if (!lexstate)
  298. {
  299. grub_free (parsed);
  300. grub_free (parsestate);
  301. return 0;
  302. }
  303. parsestate->lexerstate = lexstate;
  304. membackup = grub_script_mem_record (parsestate);
  305. /* Parse the script. */
  306. if (grub_script_yyparse (parsestate) || parsestate->err)
  307. {
  308. struct grub_script_mem *memfree;
  309. memfree = grub_script_mem_record_stop (parsestate, membackup);
  310. grub_script_mem_free (memfree);
  311. grub_script_lexer_fini (lexstate);
  312. grub_free (parsestate);
  313. grub_free (parsed);
  314. return 0;
  315. }
  316. parsed->mem = grub_script_mem_record_stop (parsestate, membackup);
  317. parsed->cmd = parsestate->parsed;
  318. parsed->children = parsestate->scripts;
  319. grub_script_lexer_fini (lexstate);
  320. grub_free (parsestate);
  321. return parsed;
  322. }