david/ipxe
david
/
ipxe
Archived
1
0
Fork 0

[gdb] Use generic option-parsing library

Total saving: 42 bytes.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
Michael Brown 2010-11-21 19:29:58 +00:00
parent 59980a6176
commit 1e2a8aa9c1
2 changed files with 51 additions and 42 deletions

View File

@ -17,8 +17,11 @@
*/ */
#include <stdio.h> #include <stdio.h>
#include <errno.h>
#include <assert.h>
#include <getopt.h> #include <getopt.h>
#include <ipxe/command.h> #include <ipxe/command.h>
#include <ipxe/parseopt.h>
#include <ipxe/gdbstub.h> #include <ipxe/gdbstub.h>
/** @file /** @file
@ -28,71 +31,76 @@
*/ */
/** /**
* "gdbstub" command syntax message * Parse GDB transport name
* *
* @v argv Argument list * @v text Text
* @ret trans GDB transport
* @ret rc Return status code
*/ */
static void gdbstub_syntax ( char **argv ) { static int parse_gdb_transport ( const char *text,
printf ( "Usage:\n" struct gdb_transport **trans ) {
" %s <transport> [<options>...]\n"
"\n" /* Sanity check */
"Start remote debugging using one of the following transports:\n" assert ( text != NULL );
" serial use serial port (if compiled in)\n"
" udp <interface> use UDP over network interface (if compiled in)\n", /* Find transport */
argv[0] ); *trans = find_gdb_transport ( text );
if ( ! *trans ) {
printf ( "\"%s\": no such transport (is it compiled in?)\n",
text );
return -ENOTSUP;
} }
return 0;
}
/** "gdbstub" options */
struct gdbstub_options {};
/** "gdbstub" option list */
static struct option_descriptor gdbstub_opts[] = {};
/** "gdbstub" command descriptor */
static struct command_descriptor gdbstub_cmd =
COMMAND_DESC ( struct gdbstub_options, gdbstub_opts, 1, MAX_ARGUMENTS,
"<transport> [<options>...]",
"Start remote debugging using one of the following "
"transports:\n"
" serial use serial port (if compiled in)\n"
" udp <interface> use UDP over network interface "
"(if compiled in)" );
/** /**
* The "gdbstub" command * The "gdbstub" 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 gdbstub_exec ( int argc, char **argv ) { static int gdbstub_exec ( int argc, char **argv ) {
static struct option longopts[] = { struct gdbstub_options opts;
{ "help", 0, NULL, 'h' },
{ NULL, 0, NULL, 0 },
};
const char *trans_name;
struct gdb_transport *trans; struct gdb_transport *trans;
int c; int rc;
/* Parse options */ /* Parse options */
while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){ if ( ( rc = parse_options ( argc, argv, &gdbstub_cmd, &opts ) ) != 0 )
switch ( c ) { return rc;
case 'h':
/* Display help text */
default:
/* Unrecognised/invalid option */
gdbstub_syntax ( argv );
return 1;
}
}
/* At least one argument */ /* Parse transport name */
if ( optind == argc ) { if ( ( rc = parse_gdb_transport ( argv[optind++], &trans ) ) != 0 )
gdbstub_syntax ( argv ); return rc;
return 1;
}
trans_name = argv[optind++];
/* Initialise transport */ /* Initialise transport */
trans = find_gdb_transport ( trans_name );
if ( !trans ) {
printf ( "%s: no such transport (is it compiled in?)\n", trans_name );
return 1;
}
if ( trans->init ) { if ( trans->init ) {
if ( trans->init ( argc - optind, &argv[optind] ) != 0 ) { if ( ( rc = trans->init ( argc - optind,
return 1; &argv[optind] ) ) != 0 ) {
return rc;
} }
} }
/* Enter GDB stub */ /* Enter GDB stub */
gdbstub_start ( trans ); gdbstub_start ( trans );
return 0; return 0;
} }

View File

@ -231,6 +231,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
#define ERRFILE_config_cmd ( ERRFILE_OTHER | 0x001c0000 ) #define ERRFILE_config_cmd ( ERRFILE_OTHER | 0x001c0000 )
#define ERRFILE_ifmgmt_cmd ( ERRFILE_OTHER | 0x001d0000 ) #define ERRFILE_ifmgmt_cmd ( ERRFILE_OTHER | 0x001d0000 )
#define ERRFILE_fcmgmt_cmd ( ERRFILE_OTHER | 0x001e0000 ) #define ERRFILE_fcmgmt_cmd ( ERRFILE_OTHER | 0x001e0000 )
#define ERRFILE_gdbstub_cmd ( ERRFILE_OTHER | 0x001f0000 )
/** @} */ /** @} */