2
0
mirror of https://github.com/xcat2/xNBA.git synced 2025-04-13 16:57:25 +00:00

[ifmgmt] Use generic option-parsing library

Total cost: 66 bytes

Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
Michael Brown 2010-11-21 22:14:28 +00:00
parent 9e9c9adf10
commit 398a6e9a50
5 changed files with 219 additions and 97 deletions

View File

@ -1,26 +1,97 @@
/*
* Copyright (C) 2010 Michael Brown <mbrown@fensystems.co.uk>.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <ipxe/netdevice.h>
#include <ipxe/command.h>
#include <ipxe/parseopt.h>
#include <hci/ifmgmt_cmd.h>
#include <pxe_call.h>
FILE_LICENCE ( GPL2_OR_LATER );
/** @file
*
* PXE commands
*
*/
/** "startpxe" command descriptor */
static struct command_descriptor startpxe_cmd =
COMMAND_DESC ( struct ifcommon_options, ifcommon_opts, 0, MAX_ARGUMENTS,
"[<interface>]", "" );
/**
* "startpxe" payload
*
* @v netdev Network device
* @ret rc Return status code
*/
static int startpxe_payload ( struct net_device *netdev ) {
if ( netdev_is_open ( netdev ) )
pxe_activate ( netdev );
return 0;
}
/**
* The "startpxe" command
*
* @v argc Argument count
* @v argv Argument list
* @ret rc Return status code
*/
static int startpxe_exec ( int argc, char **argv ) {
return ifcommon_exec ( argc, argv, startpxe_payload,
"Activate PXE on" );
return ifcommon_exec ( argc, argv, &startpxe_cmd, startpxe_payload, 0 );
}
/** "stoppxe" options */
struct stoppxe_options {};
/** "stoppxe" option list */
static struct option_descriptor stoppxe_opts[] = {};
/** "stoppxe" command descriptor */
static struct command_descriptor stoppxe_cmd =
COMMAND_DESC ( struct stoppxe_options, stoppxe_opts, 0, 0,
"", "" );
/**
* The "stoppxe" command
*
* @v argc Argument count
* @v argv Argument list
* @ret rc Return status code
*/
static int stoppxe_exec ( int argc __unused, char **argv __unused ) {
struct stoppxe_options opts;
int rc;
/* Parse options */
if ( ( rc = parse_options ( argc, argv, &stoppxe_cmd, &opts ) ) != 0 )
return rc;
pxe_deactivate();
return 0;
}
/** PXE commands */
struct command pxe_commands[] __command = {
{
.name = "startpxe",

View File

@ -19,9 +19,11 @@
FILE_LICENCE ( GPL2_OR_LATER );
#include <stdio.h>
#include <errno.h>
#include <getopt.h>
#include <ipxe/netdevice.h>
#include <ipxe/command.h>
#include <ipxe/parseopt.h>
#include <usr/ifmgmt.h>
#include <hci/ifmgmt_cmd.h>
@ -31,136 +33,138 @@ FILE_LICENCE ( GPL2_OR_LATER );
*
*/
/** Options shared by all if<xxx> commands */
static struct option ifcommon_longopts[] = {
{ "help", 0, NULL, 'h' },
{ NULL, 0, NULL, 0 },
};
/**
* Print syntax of if<xxx> command
*
* @v argv Command arguments
* @v verb Verb describing the action of the command
*/
static void ifcommon_syntax ( char **argv, const char *verb ) {
printf ( "Usage:\n"
" %s [<interface>] [<interface>...]\n"
"\n"
"%s the specified network interfaces\n",
argv[0], verb );
}
/**
* Execute if<xxx> command over all network devices
*
* @v payload Command to execute
* @ret rc Exit code
*/
static int ifcommon_do_all ( int ( * payload ) ( struct net_device * ) ) {
struct net_device *netdev;
int rc = 0;
/* Execute payload for each network device */
for_each_netdev ( netdev ) {
if ( payload ( netdev ) != 0 )
rc = 1;
}
return rc;
}
/**
* Execute if<xxx> command over list of network devices
*
* @v payload Command to execute
* @ret rc Exit code
*/
static int ifcommon_do_list ( int ( * payload ) ( struct net_device * ),
char **list, unsigned int count ) {
const char *netdev_name;
struct net_device *netdev;
int rc = 0;
while ( count-- ) {
netdev_name = *(list++);
netdev = find_netdev ( netdev_name );
if ( ! netdev ) {
printf ( "%s: no such interface\n", netdev_name );
rc = 1;
continue;
}
if ( payload ( netdev ) != 0 )
rc = 1;
}
return rc;
}
/** "if<xxx>" command options */
struct option_descriptor ifcommon_opts[0];
/**
* Execute if<xxx> command
*
* @v argc Argument count
* @v argv Argument list
* @v cmd Command descriptor
* @v payload Command to execute
* @v verb Verb describing the action of the command
* @ret rc Exit code
* @ret rc Return status code
*/
int ifcommon_exec ( int argc, char **argv,
struct command_descriptor *cmd,
int ( * payload ) ( struct net_device * ),
const char *verb ) {
int c;
int stop_on_first_success ) {
struct ifcommon_options opts;
struct net_device *netdev;
int rc;
/* Parse options */
while ( ( c = getopt_long ( argc, argv, "h", ifcommon_longopts,
NULL ) ) >= 0 ) {
switch ( c ) {
case 'h':
/* Display help text */
default:
/* Unrecognised/invalid option */
ifcommon_syntax ( argv, verb );
return 1;
if ( ( rc = parse_options ( argc, argv, cmd, &opts ) ) != 0 )
return rc;
if ( optind != argc ) {
/* Treat arguments as a list of interfaces to try */
while ( optind != argc ) {
if ( ( rc = parse_netdev ( argv[optind++],
&netdev ) ) != 0 ) {
continue;
}
if ( ( ( rc = payload ( netdev ) ) == 0 ) &&
stop_on_first_success ) {
return 0;
}
}
} else {
/* Try all interfaces */
rc = -ENODEV;
for_each_netdev ( netdev ) {
if ( ( ( rc = payload ( netdev ) ) == 0 ) &&
stop_on_first_success ) {
return 0;
}
}
}
if ( optind == argc ) {
return ifcommon_do_all ( payload );
} else {
return ifcommon_do_list ( payload, &argv[optind],
( argc - optind ) );
}
return rc;
}
/* "ifopen" command */
/** "ifopen" command descriptor */
static struct command_descriptor ifopen_cmd =
COMMAND_DESC ( struct ifcommon_options, ifcommon_opts, 0, MAX_ARGUMENTS,
"[<interface>...]",
"Open network interface(s)" );
/**
* "ifopen" payload
*
* @v netdev Network device
* @ret rc Return status code
*/
static int ifopen_payload ( struct net_device *netdev ) {
return ifopen ( netdev );
}
/**
* The "ifopen" command
*
* @v argc Argument count
* @v argv Argument list
* @ret rc Return status code
*/
static int ifopen_exec ( int argc, char **argv ) {
return ifcommon_exec ( argc, argv, ifopen_payload, "Open" );
return ifcommon_exec ( argc, argv, &ifopen_cmd, ifopen_payload, 0 );
}
/* "ifclose" command */
/** "ifclose" command descriptor */
static struct command_descriptor ifclose_cmd =
COMMAND_DESC ( struct ifcommon_options, ifcommon_opts, 0, MAX_ARGUMENTS,
"[<interface>...]",
"Close network interface(s)" );
/**
* "ifclose" payload
*
* @v netdev Network device
* @ret rc Return status code
*/
static int ifclose_payload ( struct net_device *netdev ) {
ifclose ( netdev );
return 0;
}
/**
* The "ifclose" command
*
* @v argc Argument count
* @v argv Argument list
* @ret rc Return status code
*/
static int ifclose_exec ( int argc, char **argv ) {
return ifcommon_exec ( argc, argv, ifclose_payload, "Close" );
return ifcommon_exec ( argc, argv, &ifclose_cmd, ifclose_payload, 0 );
}
/* "ifstat" command */
/** "ifstat" command descriptor */
static struct command_descriptor ifstat_cmd =
COMMAND_DESC ( struct ifcommon_options, ifcommon_opts, 0, MAX_ARGUMENTS,
"[<interface>...]",
"Show network interface(s)" );
/**
* "ifstat" payload
*
* @v netdev Network device
* @ret rc Return status code
*/
static int ifstat_payload ( struct net_device *netdev ) {
ifstat ( netdev );
return 0;
}
/**
* The "ifstat" command
*
* @v argc Argument count
* @v argv Argument list
* @ret rc Return status code
*/
static int ifstat_exec ( int argc, char **argv ) {
return ifcommon_exec ( argc, argv,
ifstat_payload, "Display status of" );
return ifcommon_exec ( argc, argv, &ifstat_cmd, ifstat_payload, 0 );
}
/** Interface management commands */

View File

@ -21,11 +21,28 @@ FILE_LICENCE ( GPL2_OR_LATER );
#include <ipxe/netdevice.h>
#include <ipxe/net80211.h>
#include <ipxe/command.h>
#include <ipxe/parseopt.h>
#include <usr/iwmgmt.h>
#include <hci/ifmgmt_cmd.h>
/* "iwstat" command */
/** @file
*
* Wireless interface management commands
*
*/
/** "iwstat" command descriptor */
static struct command_descriptor iwstat_cmd =
COMMAND_DESC ( struct ifcommon_options, ifcommon_opts, 0, MAX_ARGUMENTS,
"[<interface>...]",
"Show wireless interface(s)" );
/**
* "iwstat" payload
*
* @v netdev Network device
* @ret rc Return status code
*/
static int iwstat_payload ( struct net_device *netdev ) {
struct net80211_device *dev = net80211_get ( netdev );
@ -35,13 +52,29 @@ static int iwstat_payload ( struct net_device *netdev ) {
return 0;
}
/**
* The "iwstat" command
*
* @v argc Argument count
* @v argv Argument list
* @ret rc Return status code
*/
static int iwstat_exec ( int argc, char **argv ) {
return ifcommon_exec ( argc, argv,
iwstat_payload, "Display wireless status of" );
return ifcommon_exec ( argc, argv, &iwstat_cmd, iwstat_payload, 0 );
}
/* "iwlist" command */
/** "iwlist" command descriptor */
static struct command_descriptor iwlist_cmd =
COMMAND_DESC ( struct ifcommon_options, ifcommon_opts, 0, MAX_ARGUMENTS,
"[<interface>...]",
"List wireless networks" );
/**
* "iwlist" payload
*
* @v netdev Network device
* @ret rc Return status code
*/
static int iwlist_payload ( struct net_device *netdev ) {
struct net80211_device *dev = net80211_get ( netdev );
@ -51,9 +84,15 @@ static int iwlist_payload ( struct net_device *netdev ) {
return 0;
}
/**
* The "iwlist" command
*
* @v argc Argument count
* @v argv Argument list
* @ret rc Return status code
*/
static int iwlist_exec ( int argc, char **argv ) {
return ifcommon_exec ( argc, argv, iwlist_payload,
"List wireless networks available via" );
return ifcommon_exec ( argc, argv, &iwlist_cmd, iwlist_payload, 0 );
}
/** Wireless interface management commands */

View File

@ -21,10 +21,17 @@
FILE_LICENCE ( GPL2_OR_LATER );
#include <ipxe/parseopt.h>
struct net_device;
struct ifcommon_options {};
extern struct option_descriptor ifcommon_opts[0];
extern int ifcommon_exec ( int argc, char **argv,
struct command_descriptor *cmd,
int ( * payload ) ( struct net_device * ),
const char *verb );
int stop_on_first_success );
#endif /* _IFMGMT_CMD_H */

View File

@ -229,6 +229,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
#define ERRFILE_linux_smbios ( ERRFILE_OTHER | 0x001a0000 )
#define ERRFILE_lotest ( ERRFILE_OTHER | 0x001b0000 )
#define ERRFILE_config_cmd ( ERRFILE_OTHER | 0x001c0000 )
#define ERRFILE_ifmgmt_cmd ( ERRFILE_OTHER | 0x001d0000 )
/** @} */