execute.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  1. /* execute.c -- Execute a GRUB script. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2005,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. #include <grub/misc.h>
  20. #include <grub/mm.h>
  21. #include <grub/env.h>
  22. #include <grub/script_sh.h>
  23. #include <grub/command.h>
  24. #include <grub/menu.h>
  25. #include <grub/lib/arg.h>
  26. #include <grub/normal.h>
  27. #include <grub/extcmd.h>
  28. #include <grub/i18n.h>
  29. #include <grub/verify.h>
  30. /* Max digits for a char is 3 (0xFF is 255), similarly for an int it
  31. is sizeof (int) * 3, and one extra for a possible -ve sign. */
  32. #define ERRNO_DIGITS_MAX (sizeof (int) * 3 + 1)
  33. static unsigned long is_continue;
  34. static unsigned long active_loops;
  35. static unsigned long active_breaks;
  36. static unsigned long function_return;
  37. #define GRUB_SCRIPT_SCOPE_MALLOCED 1
  38. #define GRUB_SCRIPT_SCOPE_ARGS_MALLOCED 2
  39. /* Scope for grub script functions. */
  40. struct grub_script_scope
  41. {
  42. unsigned flags;
  43. unsigned shifts;
  44. struct grub_script_argv argv;
  45. };
  46. static struct grub_script_scope *scope = 0;
  47. /* Wildcard translator for GRUB script. */
  48. struct grub_script_wildcard_translator *grub_wildcard_translator;
  49. static char*
  50. wildcard_escape (const char *s)
  51. {
  52. int i;
  53. int len;
  54. char ch;
  55. char *p;
  56. len = grub_strlen (s);
  57. p = grub_malloc (len * 2 + 1);
  58. if (! p)
  59. return NULL;
  60. i = 0;
  61. while ((ch = *s++))
  62. {
  63. if (ch == '*' || ch == '\\' || ch == '?')
  64. p[i++] = '\\';
  65. p[i++] = ch;
  66. }
  67. p[i] = '\0';
  68. return p;
  69. }
  70. static char*
  71. wildcard_unescape (const char *s)
  72. {
  73. int i;
  74. int len;
  75. char ch;
  76. char *p;
  77. len = grub_strlen (s);
  78. p = grub_malloc (len + 1);
  79. if (! p)
  80. return NULL;
  81. i = 0;
  82. while ((ch = *s++))
  83. {
  84. if (ch == '\\')
  85. p[i++] = *s++;
  86. else
  87. p[i++] = ch;
  88. }
  89. p[i] = '\0';
  90. return p;
  91. }
  92. static void
  93. replace_scope (struct grub_script_scope *new_scope)
  94. {
  95. if (scope)
  96. {
  97. scope->argv.argc += scope->shifts;
  98. scope->argv.args -= scope->shifts;
  99. if (scope->flags & GRUB_SCRIPT_SCOPE_ARGS_MALLOCED)
  100. grub_script_argv_free (&scope->argv);
  101. if (scope->flags & GRUB_SCRIPT_SCOPE_MALLOCED)
  102. grub_free (scope);
  103. }
  104. scope = new_scope;
  105. }
  106. grub_err_t
  107. grub_script_break (grub_command_t cmd, int argc, char *argv[])
  108. {
  109. char *p = 0;
  110. unsigned long count;
  111. if (argc == 0)
  112. count = 1;
  113. else if (argc > 1)
  114. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("one argument expected"));
  115. else
  116. {
  117. count = grub_strtoul (argv[0], &p, 10);
  118. if (grub_errno)
  119. return grub_errno;
  120. if (*p != '\0')
  121. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("unrecognized number"));
  122. if (count == 0)
  123. /* TRANSLATORS: 0 is a quantifier. "break" (similar to bash)
  124. can be used e.g. to break 3 loops at once.
  125. But asking it to break 0 loops makes no sense. */
  126. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("can't break 0 loops"));
  127. }
  128. is_continue = grub_strcmp (cmd->name, "break") ? 1 : 0;
  129. active_breaks = count;
  130. if (active_breaks > active_loops)
  131. active_breaks = active_loops;
  132. return GRUB_ERR_NONE;
  133. }
  134. grub_err_t
  135. grub_script_shift (grub_command_t cmd __attribute__((unused)),
  136. int argc, char *argv[])
  137. {
  138. char *p = 0;
  139. unsigned long n = 0;
  140. if (! scope)
  141. return GRUB_ERR_NONE;
  142. if (argc == 0)
  143. n = 1;
  144. else if (argc > 1)
  145. return GRUB_ERR_BAD_ARGUMENT;
  146. else
  147. {
  148. n = grub_strtoul (argv[0], &p, 10);
  149. if (*p != '\0')
  150. return GRUB_ERR_BAD_ARGUMENT;
  151. }
  152. if (n > scope->argv.argc)
  153. return GRUB_ERR_BAD_ARGUMENT;
  154. scope->shifts += n;
  155. scope->argv.argc -= n;
  156. scope->argv.args += n;
  157. return GRUB_ERR_NONE;
  158. }
  159. grub_err_t
  160. grub_script_setparams (grub_command_t cmd __attribute__((unused)),
  161. int argc, char **args)
  162. {
  163. struct grub_script_scope *new_scope;
  164. struct grub_script_argv argv = { 0, 0, 0 };
  165. if (! scope)
  166. return GRUB_ERR_INVALID_COMMAND;
  167. new_scope = grub_malloc (sizeof (*new_scope));
  168. if (! new_scope)
  169. return grub_errno;
  170. if (grub_script_argv_make (&argv, argc, args))
  171. {
  172. grub_free (new_scope);
  173. return grub_errno;
  174. }
  175. new_scope->shifts = 0;
  176. new_scope->argv = argv;
  177. new_scope->flags = GRUB_SCRIPT_SCOPE_MALLOCED |
  178. GRUB_SCRIPT_SCOPE_ARGS_MALLOCED;
  179. replace_scope (new_scope);
  180. return GRUB_ERR_NONE;
  181. }
  182. grub_err_t
  183. grub_script_return (grub_command_t cmd __attribute__((unused)),
  184. int argc, char *argv[])
  185. {
  186. char *p;
  187. unsigned long n;
  188. if (! scope || argc > 1)
  189. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  190. /* TRANSLATORS: It's about not being
  191. inside a function. "return" can be used only
  192. in a function and this error occurs if it's used
  193. anywhere else. */
  194. N_("not in function body"));
  195. if (argc == 0)
  196. {
  197. const char *t;
  198. function_return = 1;
  199. t = grub_env_get ("?");
  200. if (!t)
  201. return GRUB_ERR_NONE;
  202. return grub_strtoul (t, NULL, 10);
  203. }
  204. n = grub_strtoul (argv[0], &p, 10);
  205. if (grub_errno)
  206. return grub_errno;
  207. if (*p != '\0')
  208. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  209. N_("unrecognized number"));
  210. function_return = 1;
  211. return n ? grub_error (n, N_("false")) : GRUB_ERR_NONE;
  212. }
  213. static int
  214. grub_env_special (const char *name)
  215. {
  216. if (grub_isdigit (name[0]) ||
  217. grub_strcmp (name, "#") == 0 ||
  218. grub_strcmp (name, "*") == 0 ||
  219. grub_strcmp (name, "@") == 0)
  220. return 1;
  221. return 0;
  222. }
  223. static char **
  224. grub_script_env_get (const char *name, grub_script_arg_type_t type)
  225. {
  226. unsigned i;
  227. struct grub_script_argv result = { 0, 0, 0 };
  228. if (grub_script_argv_next (&result))
  229. goto fail;
  230. if (! grub_env_special (name))
  231. {
  232. const char *v = grub_env_get (name);
  233. if (v && v[0])
  234. {
  235. if (type == GRUB_SCRIPT_ARG_TYPE_VAR)
  236. {
  237. if (grub_script_argv_split_append (&result, v))
  238. goto fail;
  239. }
  240. else
  241. if (grub_script_argv_append (&result, v, grub_strlen (v)))
  242. goto fail;
  243. }
  244. }
  245. else if (! scope)
  246. {
  247. if (grub_script_argv_append (&result, 0, 0))
  248. goto fail;
  249. }
  250. else if (grub_strcmp (name, "#") == 0)
  251. {
  252. char buffer[ERRNO_DIGITS_MAX + 1];
  253. grub_snprintf (buffer, sizeof (buffer), "%u", scope->argv.argc);
  254. if (grub_script_argv_append (&result, buffer, grub_strlen (buffer)))
  255. goto fail;
  256. }
  257. else if (grub_strcmp (name, "*") == 0)
  258. {
  259. for (i = 0; i < scope->argv.argc; i++)
  260. if (type == GRUB_SCRIPT_ARG_TYPE_VAR)
  261. {
  262. if (i != 0 && grub_script_argv_next (&result))
  263. goto fail;
  264. if (grub_script_argv_split_append (&result, scope->argv.args[i]))
  265. goto fail;
  266. }
  267. else
  268. {
  269. if (i != 0 && grub_script_argv_append (&result, " ", 1))
  270. goto fail;
  271. if (grub_script_argv_append (&result, scope->argv.args[i],
  272. grub_strlen (scope->argv.args[i])))
  273. goto fail;
  274. }
  275. }
  276. else if (grub_strcmp (name, "@") == 0)
  277. {
  278. for (i = 0; i < scope->argv.argc; i++)
  279. {
  280. if (i != 0 && grub_script_argv_next (&result))
  281. goto fail;
  282. if (type == GRUB_SCRIPT_ARG_TYPE_VAR)
  283. {
  284. if (grub_script_argv_split_append (&result, scope->argv.args[i]))
  285. goto fail;
  286. }
  287. else
  288. if (grub_script_argv_append (&result, scope->argv.args[i],
  289. grub_strlen (scope->argv.args[i])))
  290. goto fail;
  291. }
  292. }
  293. else
  294. {
  295. unsigned long num = grub_strtoul (name, 0, 10);
  296. if (num == 0)
  297. ; /* XXX no file name, for now. */
  298. else if (num <= scope->argv.argc)
  299. {
  300. if (type == GRUB_SCRIPT_ARG_TYPE_VAR)
  301. {
  302. if (grub_script_argv_split_append (&result,
  303. scope->argv.args[num - 1]))
  304. goto fail;
  305. }
  306. else
  307. if (grub_script_argv_append (&result, scope->argv.args[num - 1],
  308. grub_strlen (scope->argv.args[num - 1])
  309. ))
  310. goto fail;
  311. }
  312. }
  313. return result.args;
  314. fail:
  315. grub_script_argv_free (&result);
  316. return 0;
  317. }
  318. static grub_err_t
  319. grub_script_env_set (const char *name, const char *val)
  320. {
  321. if (grub_env_special (name))
  322. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  323. N_("invalid variable name `%s'"), name);
  324. return grub_env_set (name, val);
  325. }
  326. struct gettext_context
  327. {
  328. char **allowed_strings;
  329. grub_size_t nallowed_strings;
  330. grub_size_t additional_len;
  331. };
  332. static int
  333. parse_string (const char *str,
  334. int (*hook) (const char *var, grub_size_t varlen,
  335. char **ptr, struct gettext_context *ctx),
  336. struct gettext_context *ctx,
  337. char *put)
  338. {
  339. const char *ptr;
  340. int escaped = 0;
  341. const char *optr;
  342. for (ptr = str; ptr && *ptr; )
  343. switch (*ptr)
  344. {
  345. case '\\':
  346. escaped = !escaped;
  347. if (!escaped && put)
  348. *(put++) = '\\';
  349. ptr++;
  350. break;
  351. case '$':
  352. if (escaped)
  353. {
  354. escaped = 0;
  355. if (put)
  356. *(put++) = *ptr;
  357. ptr++;
  358. break;
  359. }
  360. ptr++;
  361. switch (*ptr)
  362. {
  363. case '{':
  364. {
  365. optr = ptr + 1;
  366. ptr = grub_strchr (optr, '}');
  367. if (!ptr)
  368. break;
  369. if (hook (optr, ptr - optr, &put, ctx))
  370. return 1;
  371. ptr++;
  372. break;
  373. }
  374. case '0' ... '9':
  375. optr = ptr;
  376. while (*ptr >= '0' && *ptr <= '9')
  377. ptr++;
  378. if (hook (optr, ptr - optr, &put, ctx))
  379. return 1;
  380. break;
  381. case 'a' ... 'z':
  382. case 'A' ... 'Z':
  383. case '_':
  384. optr = ptr;
  385. while ((*ptr >= '0' && *ptr <= '9')
  386. || (*ptr >= 'a' && *ptr <= 'z')
  387. || (*ptr >= 'A' && *ptr <= 'Z')
  388. || *ptr == '_')
  389. ptr++;
  390. if (hook (optr, ptr - optr, &put, ctx))
  391. return 1;
  392. break;
  393. case '?':
  394. case '#':
  395. if (hook (ptr, 1, &put, ctx))
  396. return 1;
  397. ptr++;
  398. break;
  399. default:
  400. if (put)
  401. *(put++) = '$';
  402. }
  403. break;
  404. default:
  405. if (escaped && put)
  406. *(put++) = '\\';
  407. escaped = 0;
  408. if (put)
  409. *(put++) = *ptr;
  410. ptr++;
  411. break;
  412. }
  413. if (put)
  414. *(put++) = 0;
  415. return 0;
  416. }
  417. static int
  418. gettext_putvar (const char *str, grub_size_t len,
  419. char **ptr, struct gettext_context *ctx)
  420. {
  421. const char *var;
  422. grub_size_t i;
  423. for (i = 0; i < ctx->nallowed_strings; i++)
  424. if (grub_strncmp (ctx->allowed_strings[i], str, len) == 0
  425. && ctx->allowed_strings[i][len] == 0)
  426. {
  427. break;
  428. }
  429. if (i == ctx->nallowed_strings)
  430. return 0;
  431. /* Enough for any number. */
  432. if (len == 1 && str[0] == '#')
  433. {
  434. grub_snprintf (*ptr, 30, "%u", scope->argv.argc);
  435. *ptr += grub_strlen (*ptr);
  436. return 0;
  437. }
  438. var = grub_env_get (ctx->allowed_strings[i]);
  439. if (var)
  440. *ptr = grub_stpcpy (*ptr, var);
  441. return 0;
  442. }
  443. static int
  444. gettext_save_allow (const char *str, grub_size_t len,
  445. char **ptr __attribute__ ((unused)),
  446. struct gettext_context *ctx)
  447. {
  448. ctx->allowed_strings[ctx->nallowed_strings++] = grub_strndup (str, len);
  449. if (!ctx->allowed_strings[ctx->nallowed_strings - 1])
  450. return 1;
  451. return 0;
  452. }
  453. static int
  454. gettext_getlen (const char *str, grub_size_t len,
  455. char **ptr __attribute__ ((unused)),
  456. struct gettext_context *ctx)
  457. {
  458. const char *var;
  459. grub_size_t i;
  460. for (i = 0; i < ctx->nallowed_strings; i++)
  461. if (grub_strncmp (ctx->allowed_strings[i], str, len) == 0
  462. && ctx->allowed_strings[i][len] == 0)
  463. break;
  464. if (i == ctx->nallowed_strings)
  465. return 0;
  466. /* Enough for any number. */
  467. if (len == 1 && str[0] == '#')
  468. {
  469. ctx->additional_len += 30;
  470. return 0;
  471. }
  472. var = grub_env_get (ctx->allowed_strings[i]);
  473. if (var)
  474. ctx->additional_len += grub_strlen (var);
  475. return 0;
  476. }
  477. static int
  478. gettext_append (struct grub_script_argv *result, const char *orig_str)
  479. {
  480. const char *template;
  481. char *res = 0;
  482. struct gettext_context ctx = {
  483. .allowed_strings = 0,
  484. .nallowed_strings = 0,
  485. .additional_len = 1
  486. };
  487. int rval = 1;
  488. const char *iptr;
  489. grub_size_t dollar_cnt = 0;
  490. for (iptr = orig_str; *iptr; iptr++)
  491. if (*iptr == '$')
  492. dollar_cnt++;
  493. ctx.allowed_strings = grub_malloc (sizeof (ctx.allowed_strings[0]) * dollar_cnt);
  494. if (parse_string (orig_str, gettext_save_allow, &ctx, 0))
  495. goto fail;
  496. template = _(orig_str);
  497. if (parse_string (template, gettext_getlen, &ctx, 0))
  498. goto fail;
  499. res = grub_malloc (grub_strlen (template) + ctx.additional_len);
  500. if (!res)
  501. goto fail;
  502. if (parse_string (template, gettext_putvar, &ctx, res))
  503. goto fail;
  504. char *escaped = 0;
  505. escaped = wildcard_escape (res);
  506. if (grub_script_argv_append (result, escaped, grub_strlen (escaped)))
  507. {
  508. grub_free (escaped);
  509. goto fail;
  510. }
  511. grub_free (escaped);
  512. rval = 0;
  513. fail:
  514. grub_free (res);
  515. {
  516. grub_size_t i;
  517. for (i = 0; i < ctx.nallowed_strings; i++)
  518. grub_free (ctx.allowed_strings[i]);
  519. }
  520. grub_free (ctx.allowed_strings);
  521. return rval;
  522. }
  523. static int
  524. append (struct grub_script_argv *result,
  525. const char *s, int escape_type)
  526. {
  527. int r;
  528. char *p = 0;
  529. if (escape_type == 0)
  530. return grub_script_argv_append (result, s, grub_strlen (s));
  531. if (escape_type > 0)
  532. p = wildcard_escape (s);
  533. else if (escape_type < 0)
  534. p = wildcard_unescape (s);
  535. if (! p)
  536. return 1;
  537. r = grub_script_argv_append (result, p, grub_strlen (p));
  538. grub_free (p);
  539. return r;
  540. }
  541. /* Convert arguments in ARGLIST into ARGV form. */
  542. static int
  543. grub_script_arglist_to_argv (struct grub_script_arglist *arglist,
  544. struct grub_script_argv *argv)
  545. {
  546. int i;
  547. char **values = 0;
  548. struct grub_script_arg *arg = 0;
  549. struct grub_script_argv result = { 0, 0, 0 };
  550. for (; arglist && arglist->arg; arglist = arglist->next)
  551. {
  552. if (grub_script_argv_next (&result))
  553. goto fail;
  554. arg = arglist->arg;
  555. while (arg)
  556. {
  557. switch (arg->type)
  558. {
  559. case GRUB_SCRIPT_ARG_TYPE_VAR:
  560. case GRUB_SCRIPT_ARG_TYPE_DQVAR:
  561. {
  562. int need_cleanup = 0;
  563. values = grub_script_env_get (arg->str, arg->type);
  564. for (i = 0; values && values[i]; i++)
  565. {
  566. if (!need_cleanup)
  567. {
  568. if (i != 0 && grub_script_argv_next (&result))
  569. {
  570. need_cleanup = 1;
  571. goto cleanup;
  572. }
  573. if (arg->type == GRUB_SCRIPT_ARG_TYPE_VAR)
  574. {
  575. int len;
  576. char ch;
  577. char *p;
  578. char *op;
  579. const char *s = values[i];
  580. len = grub_strlen (values[i]);
  581. /* \? -> \\\? */
  582. /* \* -> \\\* */
  583. /* \ -> \\ */
  584. p = grub_malloc (len * 2 + 1);
  585. if (! p)
  586. {
  587. need_cleanup = 1;
  588. goto cleanup;
  589. }
  590. op = p;
  591. while ((ch = *s++))
  592. {
  593. if (ch == '\\')
  594. {
  595. *op++ = '\\';
  596. if (*s == '?' || *s == '*')
  597. *op++ = '\\';
  598. }
  599. *op++ = ch;
  600. }
  601. *op = '\0';
  602. need_cleanup = grub_script_argv_append (&result, p, op - p);
  603. grub_free (p);
  604. /* Fall through to cleanup */
  605. }
  606. else
  607. {
  608. need_cleanup = append (&result, values[i], 1);
  609. /* Fall through to cleanup */
  610. }
  611. }
  612. cleanup:
  613. grub_free (values[i]);
  614. }
  615. grub_free (values);
  616. if (need_cleanup)
  617. goto fail;
  618. break;
  619. }
  620. case GRUB_SCRIPT_ARG_TYPE_BLOCK:
  621. {
  622. char *p;
  623. if (grub_script_argv_append (&result, "{", 1))
  624. goto fail;
  625. p = wildcard_escape (arg->str);
  626. if (!p)
  627. goto fail;
  628. if (grub_script_argv_append (&result, p,
  629. grub_strlen (p)))
  630. {
  631. grub_free (p);
  632. goto fail;
  633. }
  634. grub_free (p);
  635. if (grub_script_argv_append (&result, "}", 1))
  636. goto fail;
  637. }
  638. result.script = arg->script;
  639. break;
  640. case GRUB_SCRIPT_ARG_TYPE_TEXT:
  641. if (arg->str[0] &&
  642. grub_script_argv_append (&result, arg->str,
  643. grub_strlen (arg->str)))
  644. goto fail;
  645. break;
  646. case GRUB_SCRIPT_ARG_TYPE_GETTEXT:
  647. {
  648. if (gettext_append (&result, arg->str))
  649. goto fail;
  650. }
  651. break;
  652. case GRUB_SCRIPT_ARG_TYPE_DQSTR:
  653. case GRUB_SCRIPT_ARG_TYPE_SQSTR:
  654. if (append (&result, arg->str, 1))
  655. goto fail;
  656. break;
  657. }
  658. arg = arg->next;
  659. }
  660. }
  661. if (! result.args[result.argc - 1])
  662. result.argc--;
  663. /* Perform wildcard expansion. */
  664. int j;
  665. int failed = 0;
  666. struct grub_script_argv unexpanded = result;
  667. result.argc = 0;
  668. result.args = 0;
  669. for (i = 0; unexpanded.args[i]; i++)
  670. {
  671. char **expansions = 0;
  672. if (grub_wildcard_translator
  673. && grub_wildcard_translator->expand (unexpanded.args[i],
  674. &expansions))
  675. {
  676. grub_script_argv_free (&unexpanded);
  677. goto fail;
  678. }
  679. if (! expansions)
  680. {
  681. grub_script_argv_next (&result);
  682. append (&result, unexpanded.args[i], -1);
  683. }
  684. else
  685. {
  686. for (j = 0; expansions[j]; j++)
  687. {
  688. failed = (failed || grub_script_argv_next (&result) ||
  689. append (&result, expansions[j], 0));
  690. grub_free (expansions[j]);
  691. }
  692. grub_free (expansions);
  693. if (failed)
  694. {
  695. grub_script_argv_free (&unexpanded);
  696. goto fail;
  697. }
  698. }
  699. }
  700. grub_script_argv_free (&unexpanded);
  701. *argv = result;
  702. return 0;
  703. fail:
  704. grub_script_argv_free (&result);
  705. return 1;
  706. }
  707. static grub_err_t
  708. grub_script_execute_cmd (struct grub_script_cmd *cmd)
  709. {
  710. int ret;
  711. char errnobuf[ERRNO_DIGITS_MAX + 1];
  712. if (cmd == 0)
  713. return 0;
  714. ret = cmd->exec (cmd);
  715. grub_snprintf (errnobuf, sizeof (errnobuf), "%d", ret);
  716. grub_env_set ("?", errnobuf);
  717. return ret;
  718. }
  719. /* Execute a function call. */
  720. grub_err_t
  721. grub_script_function_call (grub_script_function_t func, int argc, char **args)
  722. {
  723. grub_err_t ret = 0;
  724. unsigned long loops = active_loops;
  725. struct grub_script_scope *old_scope;
  726. struct grub_script_scope new_scope;
  727. active_loops = 0;
  728. new_scope.flags = 0;
  729. new_scope.shifts = 0;
  730. new_scope.argv.argc = argc;
  731. new_scope.argv.args = args;
  732. old_scope = scope;
  733. scope = &new_scope;
  734. ret = grub_script_execute (func->func);
  735. function_return = 0;
  736. active_loops = loops;
  737. replace_scope (old_scope); /* free any scopes by setparams */
  738. return ret;
  739. }
  740. /* Helper for grub_script_execute_sourcecode. */
  741. static grub_err_t
  742. grub_script_execute_sourcecode_getline (char **line,
  743. int cont __attribute__ ((unused)),
  744. void *data)
  745. {
  746. const char **source = data;
  747. const char *p;
  748. if (! *source)
  749. {
  750. *line = 0;
  751. return 0;
  752. }
  753. p = grub_strchr (*source, '\n');
  754. if (p)
  755. *line = grub_strndup (*source, p - *source);
  756. else
  757. *line = grub_strdup (*source);
  758. *source = p ? p + 1 : 0;
  759. return 0;
  760. }
  761. /* Execute a source script. */
  762. grub_err_t
  763. grub_script_execute_sourcecode (const char *source)
  764. {
  765. grub_err_t ret = 0;
  766. struct grub_script *parsed_script;
  767. while (source)
  768. {
  769. char *line;
  770. grub_script_execute_sourcecode_getline (&line, 0, &source);
  771. parsed_script = grub_script_parse
  772. (line, grub_script_execute_sourcecode_getline, &source);
  773. if (! parsed_script)
  774. {
  775. ret = grub_errno;
  776. grub_free (line);
  777. break;
  778. }
  779. ret = grub_script_execute (parsed_script);
  780. grub_script_free (parsed_script);
  781. grub_free (line);
  782. }
  783. return ret;
  784. }
  785. /* Execute a source script in new scope. */
  786. grub_err_t
  787. grub_script_execute_new_scope (const char *source, int argc, char **args)
  788. {
  789. grub_err_t ret = 0;
  790. struct grub_script_scope new_scope;
  791. struct grub_script_scope *old_scope;
  792. new_scope.argv.argc = argc;
  793. new_scope.argv.args = args;
  794. new_scope.flags = 0;
  795. old_scope = scope;
  796. scope = &new_scope;
  797. ret = grub_script_execute_sourcecode (source);
  798. scope = old_scope;
  799. return ret;
  800. }
  801. /* Execute a single command line. */
  802. grub_err_t
  803. grub_script_execute_cmdline (struct grub_script_cmd *cmd)
  804. {
  805. struct grub_script_cmdline *cmdline = (struct grub_script_cmdline *) cmd;
  806. grub_command_t grubcmd;
  807. grub_err_t ret = 0;
  808. grub_script_function_t func = 0;
  809. char errnobuf[18];
  810. char *cmdname, *cmdstring;
  811. int argc, offset = 0, cmdlen = 0;
  812. unsigned int i;
  813. char **args;
  814. int invert;
  815. struct grub_script_argv argv = { 0, 0, 0 };
  816. /* Lookup the command. */
  817. if (grub_script_arglist_to_argv (cmdline->arglist, &argv) || ! argv.args[0])
  818. return grub_errno;
  819. for (i = 0; i < argv.argc; i++)
  820. {
  821. cmdlen += grub_strlen (argv.args[i]) + 1;
  822. }
  823. cmdstring = grub_malloc (cmdlen);
  824. if (!cmdstring)
  825. {
  826. return grub_error (GRUB_ERR_OUT_OF_MEMORY,
  827. N_("cannot allocate command buffer"));
  828. }
  829. for (i = 0; i < argv.argc; i++)
  830. {
  831. offset += grub_snprintf (cmdstring + offset, cmdlen - offset, "%s ",
  832. argv.args[i]);
  833. }
  834. cmdstring[cmdlen - 1] = '\0';
  835. grub_verify_string (cmdstring, GRUB_VERIFY_COMMAND);
  836. grub_free (cmdstring);
  837. invert = 0;
  838. argc = argv.argc - 1;
  839. args = argv.args + 1;
  840. cmdname = argv.args[0];
  841. if (grub_strcmp (cmdname, "!") == 0)
  842. {
  843. if (argv.argc < 2 || ! argv.args[1])
  844. {
  845. grub_script_argv_free (&argv);
  846. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  847. N_("no command is specified"));
  848. }
  849. invert = 1;
  850. argc = argv.argc - 2;
  851. args = argv.args + 2;
  852. cmdname = argv.args[1];
  853. }
  854. grubcmd = grub_command_find (cmdname);
  855. if (! grubcmd)
  856. {
  857. grub_errno = GRUB_ERR_NONE;
  858. /* It's not a GRUB command, try all functions. */
  859. func = grub_script_function_find (cmdname);
  860. if (! func)
  861. {
  862. /* As a last resort, try if it is an assignment. */
  863. char *assign = grub_strdup (cmdname);
  864. char *eq = grub_strchr (assign, '=');
  865. if (eq)
  866. {
  867. /* This was set because the command was not found. */
  868. grub_errno = GRUB_ERR_NONE;
  869. /* Create two strings and set the variable. */
  870. *eq = '\0';
  871. eq++;
  872. grub_script_env_set (assign, eq);
  873. }
  874. grub_free (assign);
  875. grub_snprintf (errnobuf, sizeof (errnobuf), "%d", grub_errno);
  876. grub_script_env_set ("?", errnobuf);
  877. grub_script_argv_free (&argv);
  878. grub_print_error ();
  879. return 0;
  880. }
  881. }
  882. /* Execute the GRUB command or function. */
  883. if (grubcmd)
  884. {
  885. if (grub_extractor_level && !(grubcmd->flags
  886. & GRUB_COMMAND_FLAG_EXTRACTOR))
  887. ret = grub_error (GRUB_ERR_EXTRACTOR,
  888. "%s isn't allowed to execute in an extractor",
  889. cmdname);
  890. else if ((grubcmd->flags & GRUB_COMMAND_FLAG_BLOCKS) &&
  891. (grubcmd->flags & GRUB_COMMAND_FLAG_EXTCMD))
  892. ret = grub_extcmd_dispatcher (grubcmd, argc, args, argv.script);
  893. else
  894. ret = (grubcmd->func) (grubcmd, argc, args);
  895. }
  896. else
  897. ret = grub_script_function_call (func, argc, args);
  898. if (invert)
  899. {
  900. if (ret == GRUB_ERR_TEST_FAILURE)
  901. grub_errno = ret = GRUB_ERR_NONE;
  902. else if (ret == GRUB_ERR_NONE)
  903. ret = grub_error (GRUB_ERR_TEST_FAILURE, N_("false"));
  904. else
  905. {
  906. grub_print_error ();
  907. ret = GRUB_ERR_NONE;
  908. }
  909. }
  910. /* Free arguments. */
  911. grub_script_argv_free (&argv);
  912. if (grub_errno == GRUB_ERR_TEST_FAILURE)
  913. grub_errno = GRUB_ERR_NONE;
  914. grub_print_error ();
  915. grub_snprintf (errnobuf, sizeof (errnobuf), "%d", ret);
  916. grub_env_set ("?", errnobuf);
  917. return ret;
  918. }
  919. /* Execute a block of one or more commands. */
  920. grub_err_t
  921. grub_script_execute_cmdlist (struct grub_script_cmd *list)
  922. {
  923. int ret = 0;
  924. struct grub_script_cmd *cmd;
  925. /* Loop over every command and execute it. */
  926. for (cmd = list->next; cmd; cmd = cmd->next)
  927. {
  928. if (active_breaks)
  929. break;
  930. ret = grub_script_execute_cmd (cmd);
  931. if (function_return)
  932. break;
  933. }
  934. return ret;
  935. }
  936. /* Execute an if statement. */
  937. grub_err_t
  938. grub_script_execute_cmdif (struct grub_script_cmd *cmd)
  939. {
  940. int ret;
  941. const char *result;
  942. struct grub_script_cmdif *cmdif = (struct grub_script_cmdif *) cmd;
  943. /* Check if the commands results in a true or a false. The value is
  944. read from the env variable `?'. */
  945. ret = grub_script_execute_cmd (cmdif->exec_to_evaluate);
  946. if (function_return)
  947. return ret;
  948. result = grub_env_get ("?");
  949. grub_errno = GRUB_ERR_NONE;
  950. /* Execute the `if' or the `else' part depending on the value of
  951. `?'. */
  952. if (result && ! grub_strcmp (result, "0"))
  953. return grub_script_execute_cmd (cmdif->exec_on_true);
  954. else
  955. return grub_script_execute_cmd (cmdif->exec_on_false);
  956. }
  957. /* Execute a for statement. */
  958. grub_err_t
  959. grub_script_execute_cmdfor (struct grub_script_cmd *cmd)
  960. {
  961. unsigned i;
  962. grub_err_t result;
  963. struct grub_script_argv argv = { 0, 0, 0 };
  964. struct grub_script_cmdfor *cmdfor = (struct grub_script_cmdfor *) cmd;
  965. if (grub_script_arglist_to_argv (cmdfor->words, &argv))
  966. return grub_errno;
  967. active_loops++;
  968. result = 0;
  969. for (i = 0; i < argv.argc; i++)
  970. {
  971. if (is_continue && active_breaks == 1)
  972. active_breaks = 0;
  973. if (! active_breaks)
  974. {
  975. grub_script_env_set (cmdfor->name->str, argv.args[i]);
  976. result = grub_script_execute_cmd (cmdfor->list);
  977. if (function_return)
  978. break;
  979. }
  980. }
  981. if (active_breaks)
  982. active_breaks--;
  983. active_loops--;
  984. grub_script_argv_free (&argv);
  985. return result;
  986. }
  987. /* Execute a "while" or "until" command. */
  988. grub_err_t
  989. grub_script_execute_cmdwhile (struct grub_script_cmd *cmd)
  990. {
  991. int result;
  992. struct grub_script_cmdwhile *cmdwhile = (struct grub_script_cmdwhile *) cmd;
  993. active_loops++;
  994. do {
  995. result = grub_script_execute_cmd (cmdwhile->cond);
  996. if (function_return)
  997. break;
  998. if (cmdwhile->until ? !result : result)
  999. break;
  1000. result = grub_script_execute_cmd (cmdwhile->list);
  1001. if (function_return)
  1002. break;
  1003. if (active_breaks == 1 && is_continue)
  1004. active_breaks = 0;
  1005. if (active_breaks)
  1006. break;
  1007. } while (1); /* XXX Put a check for ^C here */
  1008. if (active_breaks)
  1009. active_breaks--;
  1010. active_loops--;
  1011. return result;
  1012. }
  1013. /* Execute any GRUB pre-parsed command or script. */
  1014. grub_err_t
  1015. grub_script_execute (struct grub_script *script)
  1016. {
  1017. if (script == 0)
  1018. return 0;
  1019. return grub_script_execute_cmd (script->cmd);
  1020. }