david/ipxe
Archived
1
0

[image] Use generic option-parsing library

Total saving: 548 bytes.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
Michael Brown 2010-11-21 20:38:00 +00:00
parent 6832f0688f
commit 0cd6f2c709

View File

@ -26,6 +26,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
#include <getopt.h> #include <getopt.h>
#include <ipxe/image.h> #include <ipxe/image.h>
#include <ipxe/command.h> #include <ipxe/command.h>
#include <ipxe/parseopt.h>
#include <usr/imgmgmt.h> #include <usr/imgmgmt.h>
/** @file /** @file
@ -34,12 +35,6 @@ FILE_LICENCE ( GPL2_OR_LATER );
* *
*/ */
enum image_action {
IMG_FETCH = 0,
IMG_LOAD,
IMG_EXEC,
};
/** /**
* Fill in image command line * Fill in image command line
* *
@ -74,74 +69,61 @@ static int imgfill_cmdline ( struct image *image, unsigned int nargs,
} }
} }
/** /** "imgfetch" options */
* "imgfetch"/"module"/"kernel" command syntax message struct imgfetch_options {
* /** Image name */
* @v argv Argument list const char *name;
*/ };
static void imgfetch_core_syntax ( char **argv, enum image_action action ) {
static const char *actions[] = {
[IMG_FETCH] = "Fetch",
[IMG_LOAD] = "Fetch and load",
[IMG_EXEC] = "Fetch and execute",
};
printf ( "Usage:\n" /** "imgfetch" option list */
" %s [-n|--name <name>] filename [arguments...]\n" static struct option_descriptor imgfetch_opts[] = {
"\n" OPTION_DESC ( "name", 'n', required_argument,
"%s executable/loadable image\n", struct imgfetch_options, name, parse_string ),
argv[0], actions[action] ); };
}
/** "imgfetch" command descriptor */
static struct command_descriptor imgfetch_cmd =
COMMAND_DESC ( struct imgfetch_options, imgfetch_opts, 1, MAX_ARGUMENTS,
"[--name <name>] <image_url> [<arguments>...]",
"Fetch image" );
/** "kernel" command descriptor */
static struct command_descriptor kernel_cmd =
COMMAND_DESC ( struct imgfetch_options, imgfetch_opts, 1, MAX_ARGUMENTS,
"[--name <name>] <image_url> [<arguments>...]",
"Fetch and load image" );
/** "chain" command descriptor */
static struct command_descriptor chain_cmd =
COMMAND_DESC ( struct imgfetch_options, imgfetch_opts, 1, MAX_ARGUMENTS,
"[--name <name>] <image_url> [<arguments>...]",
"Fetch and execute image" );
/** /**
* The "imgfetch"/"module"/"kernel" command body * The "imgfetch" and friends command body
* *
* @v image_type Image type to assign (or NULL)
* @v load Image will be automatically loaded after fetching
* @v argc Argument count * @v argc Argument count
* @v argv Argument list * @v argv Argument list
* @v cmd Command descriptor
* @v image_register Image registration action
* @ret rc Return status code * @ret rc Return status code
*/ */
static int imgfetch_core_exec ( struct image_type *image_type, static int imgfetch_core_exec ( int argc, char **argv,
enum image_action action, struct command_descriptor *cmd,
int argc, char **argv ) { int ( * image_register ) ( struct image * ) ) {
static struct option longopts[] = { struct imgfetch_options opts;
{ "help", 0, NULL, 'h' },
{ "name", required_argument, NULL, 'n' },
{ NULL, 0, NULL, 0 },
};
struct image *image; struct image *image;
const char *name = NULL; char *uri_string;
char *filename;
int ( * image_register ) ( struct image *image );
int c;
int rc; int rc;
/* Parse options */ /* Parse options */
while ( ( c = getopt_long ( argc, argv, "hn:", if ( ( rc = parse_options ( argc, argv, cmd, &opts ) ) != 0 )
longopts, NULL ) ) >= 0 ) { return rc;
switch ( c ) {
case 'n':
/* Set image name */
name = optarg;
break;
case 'h':
/* Display help text */
default:
/* Unrecognised/invalid option */
imgfetch_core_syntax ( argv, action );
return -EINVAL;
}
}
/* Need at least a filename remaining after the options */ /* Parse URI string */
if ( optind == argc ) { uri_string = argv[optind];
imgfetch_core_syntax ( argv, action ); if ( ! opts.name )
return -EINVAL; opts.name = basename ( uri_string );
}
filename = argv[optind++];
if ( ! name )
name = basename ( filename );
/* Allocate image */ /* Allocate image */
image = alloc_image(); image = alloc_image();
@ -151,37 +133,18 @@ static int imgfetch_core_exec ( struct image_type *image_type,
} }
/* Fill in image name */ /* Fill in image name */
if ( name ) { if ( ( rc = image_set_name ( image, opts.name ) ) != 0 )
if ( ( rc = image_set_name ( image, name ) ) != 0 ) return rc;
return rc;
}
/* Set image type (if specified) */
image->type = image_type;
/* Fill in command line */ /* Fill in command line */
if ( ( rc = imgfill_cmdline ( image, ( argc - optind ), if ( ( rc = imgfill_cmdline ( image, ( argc - optind - 1 ),
&argv[optind] ) ) != 0 ) &argv[ optind + 1 ] ) ) != 0 )
return rc; return rc;
/* Fetch the image */ /* Fetch the image */
switch ( action ) { if ( ( rc = imgfetch ( image, uri_string, image_register ) ) != 0 ) {
case IMG_FETCH:
image_register = register_image;
break;
case IMG_LOAD:
image_register = register_and_autoload_image;
break;
case IMG_EXEC:
image_register = register_and_autoexec_image;
break;
default:
assert ( 0 );
return -EINVAL;
}
if ( ( rc = imgfetch ( image, filename, image_register ) ) != 0 ) {
printf ( "Could not fetch %s: %s\n", printf ( "Could not fetch %s: %s\n",
filename, strerror ( rc ) ); uri_string, strerror ( rc ) );
image_put ( image ); image_put ( image );
return rc; return rc;
} }
@ -195,16 +158,12 @@ static int imgfetch_core_exec ( struct image_type *image_type,
* *
* @v argc Argument count * @v argc Argument count
* @v argv Argument list * @v argv Argument list
* @ret rc Exit code * @ret rc Return status code
*/ */
static int imgfetch_exec ( int argc, char **argv ) { static int imgfetch_exec ( int argc, char **argv ) {
int rc;
if ( ( rc = imgfetch_core_exec ( NULL, IMG_FETCH, return imgfetch_core_exec ( argc, argv, &imgfetch_cmd,
argc, argv ) ) != 0 ) register_image );
return rc;
return 0;
} }
/** /**
@ -212,15 +171,12 @@ static int imgfetch_exec ( int argc, char **argv ) {
* *
* @v argc Argument count * @v argc Argument count
* @v argv Argument list * @v argv Argument list
* @ret rc Exit code * @ret rc Return status code
*/ */
static int kernel_exec ( int argc, char **argv ) { static int kernel_exec ( int argc, char **argv ) {
int rc;
if ( ( rc = imgfetch_core_exec ( NULL, IMG_LOAD, argc, argv ) ) != 0 ) return imgfetch_core_exec ( argc, argv, &kernel_cmd,
return rc; register_and_autoload_image );
return 0;
} }
/** /**
@ -228,327 +184,211 @@ static int kernel_exec ( int argc, char **argv ) {
* *
* @v argc Argument count * @v argc Argument count
* @v argv Argument list * @v argv Argument list
* @ret rc Exit code * @ret rc Return status code
*/ */
static int chain_exec ( int argc, char **argv) { static int chain_exec ( int argc, char **argv) {
int rc;
if ( ( rc = imgfetch_core_exec ( NULL, IMG_EXEC, argc, argv ) ) != 0 ) return imgfetch_core_exec ( argc, argv, &chain_cmd,
return rc; register_and_autoexec_image );
return 0;
} }
/** /** "imgload" options */
* "imgload" command syntax message struct imgload_options {};
*
* @v argv Argument list /** "imgload" option list */
*/ static struct option_descriptor imgload_opts[] = {};
static void imgload_syntax ( char **argv ) {
printf ( "Usage:\n" /** "imgload" command descriptor */
" %s <image name>\n" static struct command_descriptor imgload_cmd =
"\n" COMMAND_DESC ( struct imgload_options, imgload_opts, 1, 1,
"Load executable/loadable image\n", "<image>", "Load image" );
argv[0] );
}
/** /**
* The "imgload" command * The "imgload" command
* *
* @v argc Argument count * @v argc Argument count
* @v argv Argument list * @v argv Argument list
* @ret rc Exit code * @ret rc Return status code
*/ */
static int imgload_exec ( int argc, char **argv ) { static int imgload_exec ( int argc, char **argv ) {
static struct option longopts[] = { struct imgload_options opts;
{ "help", 0, NULL, 'h' },
{ NULL, 0, NULL, 0 },
};
struct image *image; struct image *image;
const char *name;
int c;
int rc; int rc;
/* Parse options */ /* Parse options */
while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){ if ( ( rc = parse_options ( argc, argv, &imgload_cmd, &opts ) ) != 0 )
switch ( c ) { return rc;
case 'h':
/* Display help text */
default:
/* Unrecognised/invalid option */
imgload_syntax ( argv );
return 1;
}
}
/* Need exactly one image name remaining after the options */ /* Parse image name */
if ( optind != ( argc - 1 ) ) { if ( ( rc = parse_image ( argv[optind], &image ) ) != 0 )
imgload_syntax ( argv ); return rc;
return 1;
}
name = argv[optind];
/* Load all specified images */ /* Load image */
image = find_image ( name );
if ( ! image ) {
printf ( "No such image: %s\n", name );
return 1;
}
if ( ( rc = imgload ( image ) ) != 0 ) { if ( ( rc = imgload ( image ) ) != 0 ) {
printf ( "Could not load %s: %s\n", name, strerror ( rc ) ); printf ( "Could not load %s: %s\n",
image->name, strerror ( rc ) );
return rc; return rc;
} }
return 0; return 0;
} }
/** /** "imgargs" options */
* "imgargs" command syntax message struct imgargs_options {};
*
* @v argv Argument list /** "imgargs" option list */
*/ static struct option_descriptor imgargs_opts[] = {};
static void imgargs_syntax ( char **argv ) {
printf ( "Usage:\n" /** "imgargs" command descriptor */
" %s <image name> [<arguments>...]\n" static struct command_descriptor imgargs_cmd =
"\n" COMMAND_DESC ( struct imgargs_options, imgargs_opts, 1, MAX_ARGUMENTS,
"Set arguments for executable/loadable image\n", "<image> [<arguments>...]",
argv[0] ); "Set arguments for image" );
}
/** /**
* The "imgargs" command body * The "imgargs" command body
* *
* @v argc Argument count * @v argc Argument count
* @v argv Argument list * @v argv Argument list
* @ret rc Exit code * @ret rc Return status code
*/ */
static int imgargs_exec ( int argc, char **argv ) { static int imgargs_exec ( int argc, char **argv ) {
static struct option longopts[] = { struct imgargs_options opts;
{ "help", 0, NULL, 'h' },
{ NULL, 0, NULL, 0 },
};
struct image *image; struct image *image;
const char *name;
int c;
int rc; int rc;
/* Parse options */ /* Parse options */
while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){ if ( ( rc = parse_options ( argc, argv, &imgargs_cmd, &opts ) ) != 0 )
switch ( c ) {
case 'h':
/* Display help text */
default:
/* Unrecognised/invalid option */
imgargs_syntax ( argv );
return 1;
}
}
/* Need at least an image name remaining after the options */
if ( optind == argc ) {
imgargs_syntax ( argv );
return 1;
}
name = argv[optind++];
/* Fill in command line */
image = find_image ( name );
if ( ! image ) {
printf ( "No such image: %s\n", name );
return 1;
}
if ( ( rc = imgfill_cmdline ( image, ( argc - optind ),
&argv[optind] ) ) != 0 )
return rc; return rc;
/* Parse image name */
if ( ( rc = parse_image ( argv[optind], &image ) ) != 0 )
return rc;
/* Fill in command line */
if ( ( rc = imgfill_cmdline ( image, ( argc - optind - 1 ),
&argv[ optind + 1 ] ) ) != 0 )
return rc;
return 0; return 0;
} }
/** /** "imgexec" options */
* "imgexec" command syntax message struct imgexec_options {};
*
* @v argv Argument list /** "imgexec" option list */
*/ static struct option_descriptor imgexec_opts[] = {};
static void imgexec_syntax ( char **argv ) {
printf ( "Usage:\n" /** "imgexec" command descriptor */
" %s <image name>\n" static struct command_descriptor imgexec_cmd =
"\n" COMMAND_DESC ( struct imgexec_options, imgexec_opts, 0, 1,
"Execute executable/loadable image\n", "[<image>]", "Execute image" );
argv[0] );
}
/** /**
* The "imgexec" command * The "imgexec" command
* *
* @v argc Argument count * @v argc Argument count
* @v argv Argument list * @v argv Argument list
* @ret rc Exit code * @ret rc Return status code
*/ */
static int imgexec_exec ( int argc, char **argv ) { static int imgexec_exec ( int argc, char **argv ) {
static struct option longopts[] = { struct imgexec_options opts;
{ "help", 0, NULL, 'h' },
{ NULL, 0, NULL, 0 },
};
struct image *image; struct image *image;
const char *name = NULL;
int c;
int rc; int rc;
/* Parse options */ /* Parse options */
while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){ if ( ( rc = parse_options ( argc, argv, &imgexec_cmd, &opts ) ) != 0 )
switch ( c ) { return rc;
case 'h':
/* Display help text */
default:
/* Unrecognised/invalid option */
imgexec_syntax ( argv );
return 1;
}
}
/* Need no more than one image name */ /* Parse image name */
if ( optind != argc ) if ( optind < argc ) {
name = argv[optind++]; if ( ( rc = parse_image ( argv[optind], &image ) ) != 0 )
if ( optind != argc ) { return rc;
imgexec_syntax ( argv );
return 1;
}
/* Execute specified image */
if ( name ) {
image = find_image ( name );
if ( ! image ) {
printf ( "No such image: %s\n", name );
return 1;
}
} else { } else {
image = imgautoselect(); image = imgautoselect();
if ( ! image ) { if ( ! image ) {
printf ( "No (unique) loaded image\n" ); printf ( "No (unique) loaded image\n" );
return 1; return -ENOTTY;
} }
} }
/* Execute image */
if ( ( rc = imgexec ( image ) ) != 0 ) { if ( ( rc = imgexec ( image ) ) != 0 ) {
printf ( "Could not execute %s: %s\n", printf ( "Could not execute %s: %s\n",
image->name, strerror ( rc ) ); image->name, strerror ( rc ) );
return 1; return rc;
} }
return 0; return 0;
} }
/** /** "imgstat" options */
* "imgstat" command syntax message struct imgstat_options {};
*
* @v argv Argument list /** "imgstat" option list */
*/ static struct option_descriptor imgstat_opts[] = {};
static void imgstat_syntax ( char **argv ) {
printf ( "Usage:\n" /** "imgstat" command descriptor */
" %s\n" static struct command_descriptor imgstat_cmd =
"\n" COMMAND_DESC ( struct imgstat_options, imgstat_opts, 0, 0,
"List executable/loadable images\n", "", "List images" );
argv[0] );
}
/** /**
* The "imgstat" command * The "imgstat" command
* *
* @v argc Argument count * @v argc Argument count
* @v argv Argument list * @v argv Argument list
* @ret rc Exit code * @ret rc Return status code
*/ */
static int imgstat_exec ( int argc, char **argv ) { static int imgstat_exec ( int argc, char **argv ) {
static struct option longopts[] = { struct imgstat_options opts;
{ "help", 0, NULL, 'h' },
{ NULL, 0, NULL, 0 },
};
struct image *image; struct image *image;
int c; int rc;
/* Parse options */ /* Parse options */
while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){ if ( ( rc = parse_options ( argc, argv, &imgstat_cmd, &opts ) ) != 0 )
switch ( c ) { return rc;
case 'h':
/* Display help text */
default:
/* Unrecognised/invalid option */
imgstat_syntax ( argv );
return 1;
}
}
/* No arguments */
if ( optind != argc ) {
imgstat_syntax ( argv );
return 1;
}
/* Show status of all images */ /* Show status of all images */
for_each_image ( image ) { for_each_image ( image ) {
imgstat ( image ); imgstat ( image );
} }
return 0; return 0;
} }
/** /** "imgfree" options */
* "imgstat" command syntax message struct imgfree_options {};
*
* @v argv Argument list /** "imgfree" option list */
*/ static struct option_descriptor imgfree_opts[] = {};
static void imgfree_syntax ( char **argv ) {
printf ( "Usage:\n" /** "imgfree" command descriptor */
" %s [<image name>]\n" static struct command_descriptor imgfree_cmd =
"\n" COMMAND_DESC ( struct imgfree_options, imgfree_opts, 0, 1,
"Free one or all executable/loadable images\n", "[<image>]", "Free image(s)" );
argv[0] );
}
/** /**
* The "imgfree" command * The "imgfree" command
* *
* @v argc Argument count * @v argc Argument count
* @v argv Argument list * @v argv Argument list
* @ret rc Exit code * @ret rc Return status code
*/ */
static int imgfree_exec ( int argc, char **argv ) { static int imgfree_exec ( int argc, char **argv ) {
static struct option longopts[] = { struct imgfree_options opts;
{ "help", 0, NULL, 'h' },
{ NULL, 0, NULL, 0 },
};
struct image *image; struct image *image;
struct image *tmp; struct image *tmp;
const char *name = NULL; int rc;
int c;
/* Parse options */ /* Parse options */
while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){ if ( ( rc = parse_options ( argc, argv, &imgfree_cmd, &opts ) ) != 0 )
switch ( c ) { return rc;
case 'h':
/* Display help text */
default:
/* Unrecognised/invalid option */
imgfree_syntax ( argv );
return 1;
}
}
/* Need no more than one image name */ if ( optind < argc ) {
if ( optind != argc ) /* Free specified image */
name = argv[optind++]; if ( ( rc = parse_image ( argv[optind], &image ) ) != 0 )
if ( optind != argc ) { return rc;
imgfree_syntax ( argv );
return 1;
}
if ( name ) {
/* Free specified image (may leak) */
image = find_image ( name );
if ( ! image ) {
printf ( "No such image: %s\n", name );
return 1;
}
imgfree ( image ); imgfree ( image );
} else { } else {
/* Free all images */ /* Free all images */
@ -556,6 +396,7 @@ static int imgfree_exec ( int argc, char **argv ) {
imgfree ( image ); imgfree ( image );
} }
} }
return 0; return 0;
} }