remove all trailing whitespace
[grub2/phcoder/solaris.git] / commands / search.c
blobcfcc6b9593b647242af7bd8d0563ddede73e7692
1 /* search.c - search devices based on a file or a filesystem label */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2005,2007,2008,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/types.h>
21 #include <grub/misc.h>
22 #include <grub/mm.h>
23 #include <grub/err.h>
24 #include <grub/dl.h>
25 #include <grub/device.h>
26 #include <grub/file.h>
27 #include <grub/env.h>
28 #include <grub/extcmd.h>
30 static const struct grub_arg_option options[] =
32 {"file", 'f', 0, "search devices by a file (default)", 0, 0},
33 {"label", 'l', 0, "search devices by a filesystem label", 0, 0},
34 {"fs-uuid", 'u', 0, "search devices by a filesystem UUID", 0, 0},
35 {"set", 's', GRUB_ARG_OPTION_OPTIONAL, "set a variable to the first device found", "VAR", ARG_TYPE_STRING},
36 {"no-floppy", 'n', 0, "do not probe any floppy drive", 0, 0},
37 {0, 0, 0, 0, 0, 0}
40 static void
41 search_fs (const char *key, const char *var, int no_floppy, int is_uuid)
43 int count = 0;
44 auto int iterate_device (const char *name);
46 int iterate_device (const char *name)
48 grub_device_t dev;
49 int abort = 0;
51 /* Skip floppy drives when requested. */
52 if (no_floppy &&
53 name[0] == 'f' && name[1] == 'd' &&
54 name[2] >= '0' && name[2] <= '9')
55 return 0;
57 dev = grub_device_open (name);
58 if (dev)
60 grub_fs_t fs;
62 fs = grub_fs_probe (dev);
64 #define QUID(x) (is_uuid ? (x)->uuid : (x)->label)
66 if (fs && QUID(fs))
68 char *quid;
70 (QUID(fs)) (dev, &quid);
71 if (grub_errno == GRUB_ERR_NONE && quid)
73 if (grub_strcmp (quid, key) == 0)
75 /* Found! */
76 count++;
77 if (var)
79 grub_env_set (var, name);
80 abort = 1;
82 else
83 grub_printf (" %s", name);
86 grub_free (quid);
90 grub_device_close (dev);
93 grub_errno = GRUB_ERR_NONE;
94 return abort;
97 grub_device_iterate (iterate_device);
99 if (count == 0)
100 grub_error (GRUB_ERR_FILE_NOT_FOUND, "no such device: %s", key);
103 static void
104 search_file (const char *key, const char *var, int no_floppy)
106 int count = 0;
107 char *buf = 0;
108 auto int iterate_device (const char *name);
110 int iterate_device (const char *name)
112 grub_size_t len;
113 char *p;
114 grub_file_t file;
115 int abort = 0;
117 /* Skip floppy drives when requested. */
118 if (no_floppy &&
119 name[0] == 'f' && name[1] == 'd' &&
120 name[2] >= '0' && name[2] <= '9')
121 return 0;
123 len = grub_strlen (name) + 2 + grub_strlen (key) + 1;
124 p = grub_realloc (buf, len);
125 if (! p)
126 return 1;
128 buf = p;
129 grub_sprintf (buf, "(%s)%s", name, key);
131 file = grub_file_open (buf);
132 if (file)
134 /* Found! */
135 count++;
136 if (var)
138 grub_env_set (var, name);
139 abort = 1;
141 else
142 grub_printf (" %s", name);
144 grub_file_close (file);
147 grub_errno = GRUB_ERR_NONE;
148 return abort;
151 grub_device_iterate (iterate_device);
153 grub_free (buf);
155 if (grub_errno == GRUB_ERR_NONE && count == 0)
156 grub_error (GRUB_ERR_FILE_NOT_FOUND, "no such device");
159 static grub_err_t
160 grub_cmd_search (grub_extcmd_t cmd, int argc, char **args)
162 struct grub_arg_list *state = cmd->state;
163 const char *var = 0;
165 if (argc == 0)
166 return grub_error (GRUB_ERR_INVALID_COMMAND, "no argument specified");
168 if (state[3].set)
169 var = state[3].arg ? state[3].arg : "root";
171 if (state[1].set)
172 search_fs (args[0], var, state[4].set, 0);
173 else if (state[2].set)
174 search_fs (args[0], var, state[4].set, 1);
175 else
176 search_file (args[0], var, state[4].set);
178 return grub_errno;
181 static grub_extcmd_t cmd;
183 GRUB_MOD_INIT(search)
185 cmd =
186 grub_register_extcmd ("search", grub_cmd_search,
187 GRUB_COMMAND_FLAG_BOTH,
188 "search [-f|-l|-u|-s] NAME",
189 "Search devices by file, filesystem label or filesystem UUID."
190 " If --set is specified, the first device found is"
191 " set to a variable. If no variable name is"
192 " specified, \"root\" is used.",
193 options);
196 GRUB_MOD_FINI(search)
198 grub_unregister_extcmd (cmd);