* util/grub-mkfont.c: Migrate to argp.

* util/grub-mklayout.c: Likewise.
	* util/grub-mkpasswd-pbkdf2.c: Likewise.
	* util/grub-mkrelpath.c: Likewise.
	* util/grub-probe.c: Likewise.
	* util/grub-script-check.c: Likewise.
This commit is contained in:
Vladimir 'phcoder' Serbinenko 2012-02-03 21:45:43 +01:00
parent 4459819a52
commit 8e1e4e3903
8 changed files with 632 additions and 596 deletions

View file

@ -34,48 +34,65 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <argp.h>
#include "progname.h"
static struct option options[] =
{
{"help", no_argument, 0, 'h'},
{"version", no_argument, 0, 'V'},
{"verbose", no_argument, 0, 'v'},
{0, 0, 0, 0}
};
static void
usage (int status)
struct arguments
{
if (status)
fprintf (stderr,
_("Try ``%s --help'' for more information.\n"), program_name);
else
printf (_("\
Usage: %s [PATH]\n\
\n\
Checks GRUB script configuration file for syntax errors.\n\
\n\
-h, --help display this message and exit\n\
-V, --version print version information and exit\n\
-v, --verbose print the script as it is being processed\n\
\n\
Report bugs to <%s>.\n\
"), program_name,
PACKAGE_BUGREPORT);
exit (status);
int verbose;
char *filename;
};
static struct argp_option options[] = {
{"verbose", 'v', 0, 0, N_("Print verbose messages."), 0},
{ 0, 0, 0, 0, 0, 0 }
};
static error_t
argp_parser (int key, char *arg, struct argp_state *state)
{
/* Get the input argument from argp_parse, which we
know is a pointer to our arguments structure. */
struct arguments *arguments = state->input;
char *p;
switch (key)
{
case 'v':
arguments->verbose = 1;
break;
case ARGP_KEY_ARG:
if (state->arg_num == 0)
arguments->filename = xstrdup (arg);
else
{
/* Too many arguments. */
fprintf (stderr, _("Unknown extra argument `%s'.\n"), arg);
argp_usage (state);
}
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp argp = {
options, argp_parser, N_("[PATH]"),
N_("Checks GRUB script configuration file for syntax errors."),
NULL, NULL, NULL
};
int
main (int argc, char *argv[])
{
char *argument;
char *input;
int lineno = 0;
FILE *file = 0;
int verbose = 0;
struct arguments arguments;
int found_input = 0;
struct grub_script *script = NULL;
@ -98,7 +115,7 @@ main (int argc, char *argv[])
return grub_errno;
}
if (verbose)
if (arguments.verbose)
grub_printf("%s", cmdline);
for (i = 0; cmdline[i] != '\0'; i++)
@ -122,52 +139,31 @@ main (int argc, char *argv[])
set_program_name (argv[0]);
grub_util_init_nls ();
memset (&arguments, 0, sizeof (struct arguments));
/* Check for options. */
while (1)
if (argp_parse (&argp, argc, argv, 0, 0, &arguments) != 0)
{
int c = getopt_long (argc, argv, "hvV", options, 0);
if (c == -1)
break;
else
switch (c)
{
case 'h':
usage (0);
break;
case 'V':
printf ("%s (%s) %s\n", program_name, PACKAGE_NAME, PACKAGE_VERSION);
return 0;
case 'v':
verbose = 1;
break;
default:
usage (1);
break;
}
fprintf (stderr, "%s", _("Error in parsing command line arguments\n"));
exit(1);
}
/* Obtain ARGUMENT. */
if (optind >= argc)
if (!arguments.filename)
{
file = 0; /* read from stdin */
}
else if (optind + 1 != argc)
{
fprintf (stderr, _("Unknown extra argument `%s'.\n"), argv[optind + 1]);
usage (1);
}
else
{
argument = argv[optind];
file = fopen (argument, "r");
file = fopen (arguments.filename, "r");
if (! file)
{
fprintf (stderr, "%s: %s: %s\n", program_name, argument, strerror(errno));
usage (1);
char *program = xstrdup(program_name);
fprintf (stderr, "%s: %s: %s\n", program_name,
arguments.filename, strerror(errno));
argp_help (&argp, stderr, ARGP_HELP_STD_USAGE, program);
free(program);
exit(1);
}
}