eval.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2009 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/dl.h>
  19. #include <grub/misc.h>
  20. #include <grub/script_sh.h>
  21. #include <grub/command.h>
  22. #include <grub/i18n.h>
  23. #include <grub/term.h>
  24. GRUB_MOD_LICENSE ("GPLv3+");
  25. static grub_err_t
  26. grub_cmd_eval (grub_command_t cmd __attribute__((__unused__)),
  27. int argc, char *argv[])
  28. {
  29. int i;
  30. grub_size_t size = argc; /* +1 for final zero */
  31. char *str, *p;
  32. grub_err_t ret;
  33. if (argc == 0)
  34. return GRUB_ERR_NONE;
  35. for (i = 0; i < argc; i++)
  36. size += grub_strlen (argv[i]);
  37. str = p = grub_malloc (size);
  38. if (!str)
  39. return grub_errno;
  40. for (i = 0; i < argc; i++)
  41. {
  42. p = grub_stpcpy (p, argv[i]);
  43. *p++ = ' ';
  44. }
  45. *--p = '\0';
  46. ret = grub_script_execute_sourcecode (str);
  47. grub_free (str);
  48. return ret;
  49. }
  50. static grub_command_t cmd;
  51. GRUB_MOD_INIT(eval)
  52. {
  53. cmd = grub_register_command ("eval", grub_cmd_eval, N_("STRING ..."),
  54. N_("Evaluate arguments as GRUB commands"));
  55. }
  56. GRUB_MOD_FINI(eval)
  57. {
  58. grub_unregister_command (cmd);
  59. }