#include <grub/env.h>
#include <grub/extcmd.h>
-static const struct grub_arg_option options[] =
- {
- {"file", 'f', 0, "search devices by a file", 0, 0},
- {"label", 'l', 0, "search devices by a filesystem label", 0, 0},
- {"fs-uuid", 'u', 0, "search devices by a filesystem UUID", 0, 0},
- {"set", 's', GRUB_ARG_OPTION_OPTIONAL, "set a variable to the first device found", "VAR", ARG_TYPE_STRING},
- {"no-floppy", 'n', 0, "do not probe any floppy drive", 0, 0},
- {0, 0, 0, 0, 0, 0}
- };
-
-enum options
- {
- SEARCH_FILE,
- SEARCH_LABEL,
- SEARCH_FS_UUID,
- SEARCH_SET,
- SEARCH_NO_FLOPPY,
- };
-
-static void
-search_fs (const char *key, const char *var, int no_floppy, enum options type)
+void
+FUNC_NAME (const char *key, const char *var, int no_floppy)
{
int count = 0;
char *buf = NULL;
name[0] == 'f' && name[1] == 'd' && name[2] >= '0' && name[2] <= '9')
return 0;
- if (type == SEARCH_FILE)
+#ifdef DO_SEARCH_FILE
{
grub_size_t len;
char *p;
grub_file_close (file);
}
}
- else
+#else
{
- /* type is SEARCH_FS_UUID or SEARCH_LABEL */
+ /* SEARCH_FS_UUID or SEARCH_LABEL */
grub_device_t dev;
grub_fs_t fs;
- int (*compare_fn) (const char *, const char *);
char *quid;
dev = grub_device_open (name);
if (dev)
{
fs = grub_fs_probe (dev);
- compare_fn =
- (type == SEARCH_FS_UUID) ? grub_strcasecmp : grub_strcmp;
- if (fs && ((type == SEARCH_FS_UUID) ? fs->uuid : fs->label))
+#ifdef DO_SEARCH_FS_UUID
+#define compare_fn grub_strcasecmp
+#define read_fn uuid
+#else
+#define compare_fn grub_strcmp
+#define read_fn label
+#endif
+
+ if (fs && fs->read_fn)
{
- if (type == SEARCH_FS_UUID)
- fs->uuid (dev, &quid);
- else
- fs->label (dev, &quid);
+ fs->read_fn (dev, &quid);
if (grub_errno == GRUB_ERR_NONE && quid)
{
grub_device_close (dev);
}
}
+#endif
if (found)
{
}
static grub_err_t
-grub_cmd_search (grub_extcmd_t cmd, int argc, char **args)
+grub_cmd_do_search (grub_command_t cmd __attribute__ ((unused)), int argc,
+ char **args)
{
- struct grub_arg_list *state = cmd->state;
- const char *var = 0;
-
if (argc == 0)
- return grub_error (GRUB_ERR_INVALID_COMMAND, "no argument specified");
-
- if (state[SEARCH_SET].set)
- var = state[SEARCH_SET].arg ? state[SEARCH_SET].arg : "root";
+ return grub_error (GRUB_ERR_BAD_ARGUMENT, "no argument specified");
- if (state[SEARCH_LABEL].set)
- search_fs (args[0], var, state[SEARCH_NO_FLOPPY].set, SEARCH_LABEL);
- else if (state[SEARCH_FS_UUID].set)
- search_fs (args[0], var, state[SEARCH_NO_FLOPPY].set, SEARCH_FS_UUID);
- else if (state[SEARCH_FILE].set)
- search_fs (args[0], var, state[SEARCH_NO_FLOPPY].set, SEARCH_FILE);
- else
- return grub_error (GRUB_ERR_INVALID_COMMAND, "unspecified search type");
+ FUNC_NAME (args[0], argc == 1 ? 0 : args[1], 0);
return grub_errno;
}
-static grub_extcmd_t cmd;
+static grub_command_t cmd;
-GRUB_MOD_INIT(search)
+#ifdef SEARCH_FILE
+GRUB_MOD_INIT(search_file)
+#elif defined (SEARCH_FS_UUID)
+GRUB_MOD_INIT(search_fs_uuid)
+#else
+GRUB_MOD_INIT(search_fs_label)
+#endif
{
cmd =
- grub_register_extcmd ("search", grub_cmd_search,
- GRUB_COMMAND_FLAG_BOTH,
- "search [-f|-l|-u|-s|-n] NAME",
- "Search devices by file, filesystem label or filesystem UUID."
- " If --set is specified, the first device found is"
- " set to a variable. If no variable name is"
- " specified, \"root\" is used.",
- options);
+ grub_register_command (COMMAND_NAME, grub_cmd_do_search,
+ COMMAND_NAME " NAME [VARIABLE]",
+ "Search devices by " SEARCH_TARGET "."
+ " If VARIABLE is specified, the first device found is"
+ " set to a variable.");
}
-GRUB_MOD_FINI(search)
+#ifdef SEARCH_FILE
+GRUB_MOD_FINI(search_file)
+#elif defined (SEARCH_FS_UUID)
+GRUB_MOD_FINI(search_fs_uuid)
+#else
+GRUB_MOD_FINI(search_fs_label)
+#endif
{
- grub_unregister_extcmd (cmd);
+ grub_unregister_command (cmd);
}
# Commands.
pkglib_MODULES += minicmd.mod extcmd.mod hello.mod handler.mod \
- ls.mod cmp.mod cat.mod help.mod search.mod loopback.mod \
+ ls.mod cmp.mod cat.mod help.mod search_file.mod \
+ search_fs_uuid.mod search_fs_label.mod search.mod loopback.mod \
fs_file.mod fs_uuid.mod configfile.mod echo.mod \
terminfo.mod test.mod blocklist.mod hexdump.mod \
read.mod sleep.mod loadenv.mod crc.mod parttool.mod \
help_mod_LDFLAGS = $(COMMON_LDFLAGS)
# For search.mod.
-search_mod_SOURCES = commands/search.c
+search_mod_SOURCES = commands/search_wrap.c
search_mod_CFLAGS = $(COMMON_CFLAGS)
search_mod_LDFLAGS = $(COMMON_LDFLAGS)
+# For search.mod.
+search_file_mod_SOURCES = commands/search_file.c
+search_file_mod_CFLAGS = $(COMMON_CFLAGS)
+search_file_mod_LDFLAGS = $(COMMON_LDFLAGS)
+
+# For search.mod.
+search_label_mod_SOURCES = commands/search_label.c
+search_label_mod_CFLAGS = $(COMMON_CFLAGS)
+search_label_mod_LDFLAGS = $(COMMON_LDFLAGS)
+
+# For search.mod.
+search_uuid_mod_SOURCES = commands/search_uuid.c
+search_uuid_mod_CFLAGS = $(COMMON_CFLAGS)
+search_uuid_mod_LDFLAGS = $(COMMON_LDFLAGS)
+
# For test.mod.
test_mod_SOURCES = commands/test.c
test_mod_CFLAGS = $(COMMON_CFLAGS)