split search

This commit is contained in:
phcoder 2009-09-22 09:27:59 +02:00
parent c44c90db27
commit 063e925fb0
9 changed files with 66 additions and 69 deletions

View File

@ -27,27 +27,8 @@
#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;
@ -63,7 +44,7 @@ search_fs (const char *key, const char *var, int no_floppy, enum options type)
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;
@ -84,27 +65,29 @@ search_fs (const char *key, const char *var, int no_floppy, enum options type)
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)
{
@ -118,6 +101,7 @@ search_fs (const char *key, const char *var, int no_floppy, enum options type)
grub_device_close (dev);
}
}
#endif
if (found)
{
@ -156,45 +140,42 @@ search_fs (const char *key, const char *var, int no_floppy, enum options type)
}
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");
return grub_error (GRUB_ERR_BAD_ARGUMENT, "no argument specified");
if (state[SEARCH_SET].set)
var = state[SEARCH_SET].arg ? state[SEARCH_SET].arg : "root";
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);
}

View File

@ -363,7 +363,8 @@ scsi_mod_LDFLAGS = $(COMMON_LDFLAGS)
# 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 \
@ -437,10 +438,25 @@ help_mod_CFLAGS = $(COMMON_CFLAGS)
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)

View File

@ -108,7 +108,7 @@ util/grub-emu.c_DEPENDENCIES = grub_emu_init.h
grub_emu_SOURCES = commands/minicmd.c commands/cat.c commands/cmp.c \
commands/configfile.c commands/echo.c commands/help.c \
commands/handler.c commands/ls.c commands/test.c \
commands/search.c commands/blocklist.c commands/hexdump.c \
commands/search_file.c commands/search_label.c commands/search_uuid.c commands/search_wrap.c commands/blocklist.c commands/hexdump.c \
commands/gptsync.c commands/probe.c commands/xnu_uuid.c \
commands/password.c commands/keystatus.c \
lib/hexdump.c commands/i386/cpuid.c \

View File

@ -36,7 +36,7 @@ util/grub-emu.c_DEPENDENCIES = grub_emu_init.h
grub_emu_SOURCES = commands/minicmd.c commands/cat.c commands/cmp.c \
commands/configfile.c commands/help.c \
commands/handler.c commands/ls.c commands/test.c \
commands/search.c commands/hexdump.c lib/hexdump.c \
commands/search_file.c commands/search_label.c commands/search_uuid.c commands/search_wrap.c commands/hexdump.c lib/hexdump.c \
commands/halt.c commands/reboot.c commands/keystatus.c \
commands/i386/cpuid.c \
commands/password.c \

View File

@ -62,7 +62,7 @@ util/grub-emu.c_DEPENDENCIES = grub_emu_init.h
grub_emu_SOURCES = commands/minicmd.c commands/cat.c commands/cmp.c \
commands/configfile.c commands/echo.c commands/help.c \
commands/handler.c commands/ls.c commands/test.c \
commands/search.c commands/blocklist.c commands/hexdump.c \
commands/search_file.c commands/search_label.c commands/search_uuid.c commands/search_wrap.c commands/blocklist.c commands/hexdump.c \
lib/hexdump.c commands/halt.c commands/reboot.c \
lib/envblk.c commands/loadenv.c \
commands/gptsync.c commands/probe.c commands/xnu_uuid.c \

View File

@ -120,7 +120,7 @@ util/grub-emu.c_DEPENDENCIES = grub_emu_init.h
grub_emu_SOURCES = commands/minicmd.c commands/cat.c commands/cmp.c \
commands/configfile.c commands/echo.c commands/help.c \
commands/handler.c commands/ls.c commands/test.c \
commands/search.c commands/blocklist.c commands/hexdump.c \
commands/search_file.c commands/search_label.c commands/search_uuid.c commands/search_wrap.c commands/blocklist.c commands/hexdump.c \
lib/hexdump.c commands/i386/pc/halt.c commands/reboot.c \
lib/envblk.c commands/loadenv.c \
commands/gptsync.c commands/probe.c commands/xnu_uuid.c \

View File

@ -42,7 +42,7 @@ grub_mkdevicemap_SOURCES = util/grub-mkdevicemap.c util/deviceiter.c \
util/grub-emu.c_DEPENDENCIES = grub_emu_init.h
grub_emu_SOURCES = commands/minicmd.c commands/cat.c commands/cmp.c \
commands/configfile.c commands/help.c \
commands/search.c commands/handler.c commands/test.c \
commands/search_file.c commands/search_label.c commands/search_uuid.c commands/search_wrap.c commands/handler.c commands/test.c \
commands/ls.c commands/blocklist.c commands/hexdump.c \
lib/hexdump.c commands/halt.c commands/reboot.c \
lib/envblk.c commands/loadenv.c \

View File

@ -100,7 +100,7 @@ grub_ofpathname_SOURCES = util/sparc64/ieee1275/grub-ofpathname.c \
util/grub-emu.c_DEPENDENCIES = grub_emu_init.h
grub_emu_SOURCES = commands/minicmd.c commands/cat.c commands/cmp.c \
commands/configfile.c commands/help.c \
commands/search.c commands/handler.c commands/test.c \
commands/search_file.c commands/search_label.c commands/search_uuid.c commands/search_wrap.c commands/handler.c commands/test.c \
commands/ls.c commands/blocklist.c commands/hexdump.c \
lib/hexdump.c commands/halt.c commands/reboot.c \
lib/envblk.c commands/loadenv.c \

View File

@ -34,7 +34,7 @@ util/grub-emu.c_DEPENDENCIES = grub_emu_init.h
grub_emu_SOURCES = commands/minicmd.c commands/cat.c commands/cmp.c \
commands/configfile.c commands/help.c \
commands/handler.c commands/ls.c commands/test.c \
commands/search.c commands/hexdump.c lib/hexdump.c \
commands/search_file.c commands/search_label.c commands/search_uuid.c commands/search_wrap.c commands/hexdump.c lib/hexdump.c \
commands/halt.c commands/reboot.c \
commands/i386/cpuid.c \
commands/password.c commands/keystatus.c \