2008-01-15 Christian Franke <franke@computer.org>

* util/grub-emu.c: Replace <argp.h> by <getopt.h>.
        (argp_program_version): Remove variable.
        (argp_program_bug_address): Likewise.
        (options): Convert from struct argp_option to struct option.
        (struct arguments): Remove.
        (parse_opt): Remove.
        (usage): New function.
        (main): Replace struct args members by simple variables.
        Replace argp_parse() by getopt_long().
        Add switch to evaluate options.
        Add missing "(...)" around root_dev in prefix string.
This commit is contained in:
robertmh 2008-01-15 11:49:22 +00:00
parent c86f14693a
commit 47bf09a4ed
2 changed files with 99 additions and 74 deletions

View file

@ -1,3 +1,17 @@
2008-01-15 Christian Franke <franke@computer.org>
* util/grub-emu.c: Replace <argp.h> by <getopt.h>.
(argp_program_version): Remove variable.
(argp_program_bug_address): Likewise.
(options): Convert from struct argp_option to struct option.
(struct arguments): Remove.
(parse_opt): Remove.
(usage): New function.
(main): Replace struct args members by simple variables.
Replace argp_parse() by getopt_long().
Add switch to evaluate options.
Add missing "(...)" around root_dev in prefix string.
2008-01-14 Robert Millan <rmh@aybabtu.com> 2008-01-14 Robert Millan <rmh@aybabtu.com>
* kern/powerpc/ieee1275/init.c (grub_exit): Reimplement as a wrapper * kern/powerpc/ieee1275/init.c (grub_exit): Reimplement as a wrapper

View file

@ -1,6 +1,6 @@
/* /*
* GRUB -- GRand Unified Bootloader * GRUB -- GRand Unified Bootloader
* Copyright (C) 2003,2004,2005,2006,2007 Free Software Foundation, Inc. * Copyright (C) 2003,2004,2005,2006,2007,2008 Free Software Foundation, Inc.
* *
* GRUB is free software: you can redistribute it and/or modify * GRUB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -18,7 +18,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <argp.h> #include <getopt.h>
#include <string.h> #include <string.h>
#include <signal.h> #include <signal.h>
#include <sys/types.h> #include <sys/types.h>
@ -90,104 +90,115 @@ grub_machine_fini (void)
} }
const char *argp_program_version = PACKAGE_STRING; static struct option options[] =
const char *argp_program_bug_address = PACKAGE_BUGREPORT; {
static char doc[] = "GRUB emulator"; {"root-device", required_argument, 0, 'r'},
{"device-map", required_argument, 0, 'm'},
static struct argp_option options[] = { {"directory", required_argument, 0, 'd'},
{"root-device", 'r', "DEV", 0, "use DEV as the root device [default=guessed]", 0}, {"hold", optional_argument, 0, 'H'},
{"device-map", 'm', "FILE", 0, "use FILE as the device map", 0}, {"help", no_argument, 0, 'h'},
{"directory", 'd', "DIR", 0, "use GRUB files in the directory DIR", 0}, {"version", no_argument, 0, 'V'},
{"verbose", 'v', 0 , 0, "print verbose messages", 0}, {"verbose", no_argument, 0, 'v'},
{"hold", 'H', "SECONDS", OPTION_ARG_OPTIONAL, "wait until a debugger will attach", 0}, { 0, 0, 0, 0 }
{ 0, 0, 0, 0, 0, 0 }
}; };
struct arguments static int
usage (int status)
{ {
char *root_dev; if (status)
char *dev_map; fprintf (stderr,
char *dir; "Try ``grub-emu --help'' for more information.\n");
int hold; else
}; printf (
"Usage: grub-emu [OPTION]...\n"
static error_t "\n"
parse_opt (int key, char *arg, struct argp_state *state) "GRUB emulator.\n"
{ "\n"
struct arguments *args = state->input; " -r, --root-device=DEV use DEV as the root device [default=guessed]\n"
" -m, --device-map=FILE use FILE as the device map [default=%s]\n"
switch (key) " -d, --directory=DIR use GRUB files in the directory DIR [default=%s]\n"
{ " -v, --verbose print verbose messages\n"
case 'r': " -H, --hold[=SECONDS] wait until a debugger will attach\n"
args->root_dev = arg; " -h, --help display this message and exit\n"
break; " -V, --version print version information and exit\n"
case 'd': "\n"
args->dir = arg; "Report bugs to <%s>.\n", DEFAULT_DEVICE_MAP, DEFAULT_DIRECTORY, PACKAGE_BUGREPORT);
break; return status;
case 'm':
args->dev_map = arg;
break;
case 'v':
verbosity++;
break;
case 'H':
args->hold = arg ? atoi (arg) : -1;
break;
case ARGP_KEY_END:
break;
default:
return ARGP_ERR_UNKNOWN;
} }
return 0;
}
static struct argp argp = {options, parse_opt, 0, doc, 0, 0, 0};
int int
main (int argc, char *argv[]) main (int argc, char *argv[])
{ {
char *dir; char *root_dev = 0;
char *dir = DEFAULT_DIRECTORY;
struct arguments args = char *dev_map = DEFAULT_DEVICE_MAP;
{ volatile int hold = 0;
.dir = DEFAULT_DIRECTORY, int opt;
.dev_map = DEFAULT_DEVICE_MAP,
.hold = 0
};
progname = "grub-emu"; progname = "grub-emu";
argp_parse (&argp, argc, argv, 0, 0, &args); while ((opt = getopt_long (argc, argv, "r:d:m:vH:hV", options, 0)) != -1)
switch (opt)
{
case 'r':
root_dev = optarg;
break;
case 'd':
dir = optarg;
break;
case 'm':
dev_map = optarg;
break;
case 'v':
verbosity++;
break;
case 'H':
hold = (optarg ? atoi (optarg) : -1);
break;
case 'h':
return usage (0);
case 'V':
printf ("%s (%s) %s\n", progname, PACKAGE_NAME, PACKAGE_VERSION);
return 0;
default:
return usage (1);
}
if (optind < argc)
{
fprintf (stderr, "Unknown extra argument `%s'.\n", argv[optind]);
return usage (1);
}
/* Wait until the ARGS.HOLD variable is cleared by an attached debugger. */ /* Wait until the ARGS.HOLD variable is cleared by an attached debugger. */
if (args.hold && verbosity > 0) if (hold && verbosity > 0)
printf ("Run \"gdb %s %d\", and set ARGS.HOLD to zero.\n", printf ("Run \"gdb %s %d\", and set ARGS.HOLD to zero.\n",
progname, (int) getpid ()); progname, (int) getpid ());
while (args.hold) while (hold)
{ {
if (args.hold > 0) if (hold > 0)
args.hold--; hold--;
sleep (1); sleep (1);
} }
/* XXX: This is a bit unportable. */ /* XXX: This is a bit unportable. */
grub_util_biosdisk_init (args.dev_map); grub_util_biosdisk_init (dev_map);
grub_hostfs_init (); grub_hostfs_init ();
grub_init_all (); grub_init_all ();
/* Make sure that there is a root device. */ /* Make sure that there is a root device. */
if (! args.root_dev) if (! root_dev)
{ {
char *device_name = grub_guess_root_device (args.dir ? : DEFAULT_DIRECTORY); char *device_name = grub_guess_root_device (dir);
if (! device_name) if (! device_name)
grub_util_error ("cannot find a device for %s.\n", args.dir ? : DEFAULT_DIRECTORY); grub_util_error ("cannot find a device for %s.\n", dir);
args.root_dev = grub_util_get_grub_dev (device_name); root_dev = grub_util_get_grub_dev (device_name);
if (! args.root_dev) if (! root_dev)
{ {
grub_util_info ("guessing the root device failed, because of `%s'", grub_util_info ("guessing the root device failed, because of `%s'",
grub_errmsg); grub_errmsg);
@ -195,9 +206,9 @@ main (int argc, char *argv[])
} }
} }
dir = grub_get_prefix (args.dir ? : DEFAULT_DIRECTORY); dir = grub_get_prefix (dir);
prefix = xmalloc (strlen (args.root_dev) + strlen (dir) + 1); prefix = xmalloc (strlen (root_dev) + 2 + strlen (dir) + 1);
sprintf (prefix, "%s%s", args.root_dev, dir); sprintf (prefix, "(%s)%s", root_dev, dir);
free (dir); free (dir);
/* Start GRUB! */ /* Start GRUB! */