From: Pavel Roskin <proski@gnu.org>
To: grub-devel@gnu.org
Subject: [PATCH] Refactor commands/search.c
Date: Mon, 06 Jul 2009 17:13:03 -0400 [thread overview]
Message-ID: <20090706211303.23809.9322.stgit@mj.roinet.com> (raw)
ChangeLog:
* commands/search.c (search_file): Merge into ...
(search_fs): ... this. On error, don't output what we were
looking for. Accept search type as argument.
(grub_cmd_search): Pass search type to search_fs().
---
commands/search.c | 190 ++++++++++++++++++++++-------------------------------
1 files changed, 80 insertions(+), 110 deletions(-)
diff --git a/commands/search.c b/commands/search.c
index b29eefb..f45d8ab 100644
--- a/commands/search.c
+++ b/commands/search.c
@@ -47,124 +47,94 @@ enum options
};
static void
-search_fs (const char *key, const char *var, int no_floppy, int is_uuid)
-{
- int count = 0;
- auto int iterate_device (const char *name);
-
- int iterate_device (const char *name)
- {
- grub_device_t dev;
- int abort = 0;
-
- /* Skip floppy drives when requested. */
- if (no_floppy &&
- name[0] == 'f' && name[1] == 'd' &&
- name[2] >= '0' && name[2] <= '9')
- return 0;
-
- dev = grub_device_open (name);
- if (dev)
- {
- grub_fs_t fs;
- int (*compare_fn) (const char *, const char *);
- grub_err_t (*quid_fn) (grub_device_t, char **);
-
- fs = grub_fs_probe (dev);
- compare_fn = is_uuid ? grub_strcasecmp : grub_strcmp;
- quid_fn = is_uuid ? fs->uuid : fs->label;
-
- if (fs && quid_fn)
- {
- char *quid;
-
- quid_fn (dev, &quid);
- if (grub_errno == GRUB_ERR_NONE && quid)
- {
- if (compare_fn (quid, key) == 0)
- {
- /* Found! */
- count++;
- if (var)
- {
- grub_env_set (var, name);
- abort = 1;
- }
- else
- grub_printf (" %s", name);
- }
-
- grub_free (quid);
- }
- }
-
- grub_device_close (dev);
- }
-
- grub_errno = GRUB_ERR_NONE;
- return abort;
- }
-
- grub_device_iterate (iterate_device);
-
- if (count == 0)
- grub_error (GRUB_ERR_FILE_NOT_FOUND, "no such device: %s", key);
-}
-
-static void
-search_file (const char *key, const char *var, int no_floppy)
+search_fs (const char *key, const char *var, int no_floppy, enum options type)
{
int count = 0;
char *buf = 0;
- auto int iterate_device (const char *name);
+ auto int iterate_device (const char *name);
int iterate_device (const char *name)
- {
- grub_size_t len;
- char *p;
- grub_file_t file;
- int abort = 0;
-
- /* Skip floppy drives when requested. */
- if (no_floppy &&
- name[0] == 'f' && name[1] == 'd' &&
- name[2] >= '0' && name[2] <= '9')
- return 0;
-
- len = grub_strlen (name) + 2 + grub_strlen (key) + 1;
- p = grub_realloc (buf, len);
- if (! p)
- return 1;
-
- buf = p;
- grub_sprintf (buf, "(%s)%s", name, key);
-
- file = grub_file_open (buf);
- if (file)
- {
- /* Found! */
- count++;
- if (var)
- {
- grub_env_set (var, name);
- abort = 1;
- }
- else
- grub_printf (" %s", name);
-
- grub_file_close (file);
- }
-
- grub_errno = GRUB_ERR_NONE;
- return abort;
- }
+ {
+ int found = 0;
+
+ /* Skip floppy drives when requested. */
+ if (no_floppy &&
+ name[0] == 'f' && name[1] == 'd' &&
+ name[2] >= '0' && name[2] <= '9')
+ return 0;
+
+ if (type == SEARCH_FILE)
+ {
+ grub_size_t len;
+ char *p;
+ grub_file_t file;
+
+ len = grub_strlen (name) + 2 + grub_strlen (key) + 1;
+ p = grub_realloc (buf, len);
+ if (! p)
+ return 1;
+
+ buf = p;
+ grub_sprintf (buf, "(%s)%s", name, key);
+
+ file = grub_file_open (buf);
+ if (file)
+ {
+ found = 1;
+ grub_file_close (file);
+ }
+ }
+ else
+ {
+ grub_device_t dev;
+ grub_fs_t fs;
+ int (*compare_fn) (const char *, const char *);
+ grub_err_t (*quid_fn) (grub_device_t, 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;
+ quid_fn = (type == SEARCH_FS_UUID) ? fs->uuid : fs->label;
+
+ if (fs && quid_fn)
+ {
+ quid_fn (dev, &quid);
+ if (grub_errno == GRUB_ERR_NONE && quid)
+ {
+ if (compare_fn (quid, key) == 0)
+ found = 1;
+
+ grub_free (quid);
+ }
+ }
+
+ grub_device_close (dev);
+ }
+ }
+
+ if (found)
+ {
+ count++;
+ if (var)
+ grub_env_set (var, name);
+ else
+ grub_printf (" %s", name);
+ }
+
+ grub_errno = GRUB_ERR_NONE;
+ return (found && var);
+ }
grub_device_iterate (iterate_device);
grub_free (buf);
if (grub_errno == GRUB_ERR_NONE && count == 0)
- grub_error (GRUB_ERR_FILE_NOT_FOUND, "no such file: %s", key);
+ grub_error (GRUB_ERR_FILE_NOT_FOUND, "no such device");
}
static grub_err_t
@@ -180,11 +150,11 @@ grub_cmd_search (grub_extcmd_t cmd, int argc, char **args)
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, 0);
+ 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, 1);
+ search_fs (args[0], var, state[SEARCH_NO_FLOPPY].set, SEARCH_FS_UUID);
else if (state[SEARCH_FILE].set)
- search_file (args[0], var, state[SEARCH_NO_FLOPPY].set);
+ search_fs (args[0], var, state[SEARCH_NO_FLOPPY].set, SEARCH_FILE);
else
return grub_error (GRUB_ERR_INVALID_COMMAND, "unspecified search type");
next reply other threads:[~2009-07-06 21:13 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-07-06 21:13 Pavel Roskin [this message]
2009-07-07 1:03 ` [PATCH] Refactor commands/search.c Arthur Marsh
2009-07-07 1:46 ` Arthur Marsh
2009-07-07 2:13 ` Pavel Roskin
2009-07-07 3:00 ` Arthur Marsh
2009-07-07 18:42 ` Robert Millan
2009-07-07 20:14 ` Pavel Roskin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20090706211303.23809.9322.stgit@mj.roinet.com \
--to=proski@gnu.org \
--cc=grub-devel@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.