changed zfs files naming to (hdX,Y)/filesystem@@file
[grub2/phcoder.git] / commands / read.c
blob82b30b46156e987c4c8c2885ff7eac72f290805d
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.
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.
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.
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/>.
20 #include <grub/dl.h>
21 #include <grub/misc.h>
22 #include <grub/mm.h>
23 #include <grub/env.h>
24 #include <grub/term.h>
25 #include <grub/types.h>
26 #include <grub/command.h>
28 static char *
29 grub_getline (void)
31 int i;
32 char *line;
33 char *tmp;
34 char c;
36 i = 0;
37 line = grub_malloc (1 + i + sizeof('\0'));
38 if (! line)
39 return NULL;
41 while (1)
43 c = grub_getkey ();
44 if ((c == '\n') || (c == '\r'))
45 break;
47 line[i] = c;
48 if (grub_isprint (c))
49 grub_putchar (c);
50 i++;
51 tmp = grub_realloc (line, 1 + i + sizeof('\0'));
52 if (! tmp)
54 grub_free (line);
55 return NULL;
57 line = tmp;
59 line[i] = '\0';
61 return line;
64 static grub_err_t
65 grub_cmd_read (grub_command_t cmd UNUSED, int argc, char **args)
67 char *line = grub_getline ();
68 if (! line)
69 return grub_errno;
70 if (argc > 0)
71 grub_env_set (args[0], line);
73 grub_free (line);
74 return 0;
77 static grub_command_t cmd;
79 GRUB_MOD_INIT(read)
81 cmd = grub_register_command ("read", grub_cmd_read,
82 "read [ENVVAR]",
83 "Set variable with user input");
86 GRUB_MOD_FINI(read)
88 grub_unregister_command (cmd);