read.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* read.c - Command to read variables from user. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2006,2007,2008 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/dl.h>
  20. #include <grub/misc.h>
  21. #include <grub/mm.h>
  22. #include <grub/env.h>
  23. #include <grub/term.h>
  24. #include <grub/types.h>
  25. #include <grub/command.h>
  26. #include <grub/i18n.h>
  27. static char *
  28. grub_getline (void)
  29. {
  30. int i;
  31. char *line;
  32. char *tmp;
  33. char c;
  34. i = 0;
  35. line = grub_malloc (1 + i + sizeof('\0'));
  36. if (! line)
  37. return NULL;
  38. while (1)
  39. {
  40. c = grub_getkey ();
  41. if ((c == '\n') || (c == '\r'))
  42. break;
  43. line[i] = c;
  44. if (grub_isprint (c))
  45. grub_putchar (c);
  46. i++;
  47. tmp = grub_realloc (line, 1 + i + sizeof('\0'));
  48. if (! tmp)
  49. {
  50. grub_free (line);
  51. return NULL;
  52. }
  53. line = tmp;
  54. }
  55. line[i] = '\0';
  56. return line;
  57. }
  58. static grub_err_t
  59. grub_cmd_read (grub_command_t cmd __attribute__ ((unused)), int argc, char **args)
  60. {
  61. char *line = grub_getline ();
  62. if (! line)
  63. return grub_errno;
  64. if (argc > 0)
  65. grub_env_set (args[0], line);
  66. grub_free (line);
  67. return 0;
  68. }
  69. static grub_command_t cmd;
  70. GRUB_MOD_INIT(read)
  71. {
  72. cmd = grub_register_command ("read", grub_cmd_read,
  73. N_("[ENVVAR]"),
  74. N_("Set variable with user input."));
  75. }
  76. GRUB_MOD_FINI(read)
  77. {
  78. grub_unregister_command (cmd);
  79. }