style fix
[grub2/phcoder.git] / kern / corecmd.c
blob03944f2df5b3b95f0ba5a1058cee1b0d960c31fe
1 /* corecmd.c - critical commands which are registered in kernel */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2009 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/mm.h>
21 #include <grub/dl.h>
22 #include <grub/err.h>
23 #include <grub/env.h>
24 #include <grub/misc.h>
25 #include <grub/term.h>
26 #include <grub/file.h>
27 #include <grub/device.h>
28 #include <grub/command.h>
30 /* set ENVVAR=VALUE */
31 static grub_err_t
32 grub_core_cmd_set (struct grub_command *cmd __attribute__ ((unused)),
33 int argc, char *argv[])
35 char *var;
36 char *val;
38 auto int print_env (struct grub_env_var *env);
40 int print_env (struct grub_env_var *env)
42 grub_printf ("%s=%s\n", env->name, env->value);
43 return 0;
46 if (argc < 1)
48 grub_env_iterate (print_env);
49 return 0;
52 var = argv[0];
53 val = grub_strchr (var, '=');
54 if (! val)
55 return grub_error (GRUB_ERR_BAD_ARGUMENT, "not an assignment");
57 val[0] = 0;
58 grub_env_set (var, val + 1);
59 val[0] = '=';
61 return 0;
64 static grub_err_t
65 grub_core_cmd_unset (struct grub_command *cmd __attribute__ ((unused)),
66 int argc, char *argv[])
68 if (argc < 1)
69 return grub_error (GRUB_ERR_BAD_ARGUMENT,
70 "no environment variable specified");
72 grub_env_unset (argv[0]);
73 return 0;
76 static grub_err_t
77 grub_core_cmd_export (struct grub_command *cmd __attribute__ ((unused)),
78 int argc, char **args)
80 if (argc < 1)
81 return grub_error (GRUB_ERR_BAD_ARGUMENT,
82 "no environment variable specified");
84 grub_env_export (args[0]);
85 return 0;
88 /* insmod MODULE */
89 static grub_err_t
90 grub_core_cmd_insmod (struct grub_command *cmd __attribute__ ((unused)),
91 int argc, char *argv[])
93 char *p;
94 grub_dl_t mod;
96 if (argc == 0)
97 return grub_error (GRUB_ERR_BAD_ARGUMENT, "no module specified");
99 p = grub_strchr (argv[0], '/');
100 if (! p)
101 mod = grub_dl_load (argv[0]);
102 else
103 mod = grub_dl_load_file (argv[0]);
105 if (mod)
106 grub_dl_ref (mod);
108 return 0;
111 static int
112 grub_mini_print_devices (const char *name)
114 grub_printf ("(%s) ", name);
116 return 0;
119 static int
120 grub_mini_print_files (const char *filename,
121 const struct grub_dirhook_info *info)
123 grub_printf ("%s%s ", filename, info->dir ? "/" : "");
125 return 0;
128 /* ls [ARG] */
129 static grub_err_t
130 grub_core_cmd_ls (struct grub_command *cmd __attribute__ ((unused)),
131 int argc, char *argv[])
133 if (argc < 1)
135 grub_device_iterate (grub_mini_print_devices);
136 grub_putchar ('\n');
137 grub_refresh ();
139 else
141 char *device_name;
142 grub_device_t dev;
143 grub_fs_t fs;
144 char *path;
146 device_name = grub_file_get_device_name (argv[0]);
147 dev = grub_device_open (device_name);
148 if (! dev)
149 goto fail;
151 fs = grub_fs_probe (dev);
152 path = grub_strchr (argv[0], ')');
153 if (! path)
154 path = argv[0];
155 else
156 path++;
158 if (! path && ! device_name)
160 grub_error (GRUB_ERR_BAD_ARGUMENT, "invalid argument");
161 goto fail;
164 if (! path)
166 if (grub_errno == GRUB_ERR_UNKNOWN_FS)
167 grub_errno = GRUB_ERR_NONE;
169 grub_printf ("(%s): Filesystem is %s.\n",
170 device_name, fs ? fs->name : "unknown");
172 else if (fs)
174 (fs->dir) (dev, path, grub_mini_print_files);
175 grub_putchar ('\n');
176 grub_refresh ();
179 fail:
180 if (dev)
181 grub_device_close (dev);
183 grub_free (device_name);
186 return grub_errno;
189 void
190 grub_register_core_commands (void)
192 grub_register_command ("set", grub_core_cmd_set,
193 "set [ENVVAR=VALUE]", "set an environment variable");
194 grub_register_command ("unset", grub_core_cmd_unset,
195 "unset ENVVAR", "remove an environment variable");
196 grub_register_command ("export", grub_core_cmd_export,
197 "export ENVVAR", "Export a variable.");
198 grub_register_command ("ls", grub_core_cmd_ls,
199 "ls [ARG]", "list devices or files");
200 grub_register_command ("insmod", grub_core_cmd_insmod,
201 "insmod MODULE", "insert a module");