david/ipxe
david
/
ipxe
Archived
1
0
Fork 0

[cmdline] Allow "if<xxx>" commands to take options

Allow commands implemented using ifcommon_exec() to accept
command-specific options.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
Michael Brown 2013-11-04 16:15:21 +00:00
parent 16d37102ca
commit 55e85ad1ee
6 changed files with 174 additions and 63 deletions

View File

@ -31,18 +31,21 @@ FILE_LICENCE ( GPL2_OR_LATER );
*
*/
/** "startpxe" command descriptor */
static struct command_descriptor startpxe_cmd =
COMMAND_DESC ( struct ifcommon_options, ifcommon_opts, 0, MAX_ARGUMENTS,
"[<interface>]" );
/** "startpxe" options */
struct startpxe_options {};
/** "startpxe" option list */
static struct option_descriptor startpxe_opts[] = {};
/**
* "startpxe" payload
*
* @v netdev Network device
* @v opts Command options
* @ret rc Return status code
*/
static int startpxe_payload ( struct net_device *netdev ) {
static int startpxe_payload ( struct net_device *netdev,
struct startpxe_options *opts __unused ) {
if ( netdev_is_open ( netdev ) )
pxe_activate ( netdev );
@ -50,6 +53,12 @@ static int startpxe_payload ( struct net_device *netdev ) {
return 0;
}
/** "startpxe" command descriptor */
static struct ifcommon_command_descriptor startpxe_cmd =
IFCOMMON_COMMAND_DESC ( struct startpxe_options, startpxe_opts,
0, MAX_ARGUMENTS, "[<interface>]",
startpxe_payload, 0 );
/**
* The "startpxe" command
*
@ -58,7 +67,7 @@ static int startpxe_payload ( struct net_device *netdev ) {
* @ret rc Return status code
*/
static int startpxe_exec ( int argc, char **argv ) {
return ifcommon_exec ( argc, argv, &startpxe_cmd, startpxe_payload, 0 );
return ifcommon_exec ( argc, argv, &startpxe_cmd );
}
/** "stoppxe" options */

View File

@ -33,10 +33,29 @@ FILE_LICENCE ( GPL2_OR_LATER );
*
*/
/** "autoboot" options */
struct autoboot_options {};
/** "autoboot" option list */
static struct option_descriptor autoboot_opts[] = {};
/**
* "autoboot" payload
*
* @v netdev Network device
* @v opts Command options
* @ret rc Return status code
*/
static int autoboot_payload ( struct net_device *netdev,
struct autoboot_options *opts __unused ) {
return netboot ( netdev );
}
/** "autoboot" command descriptor */
static struct command_descriptor autoboot_cmd =
COMMAND_DESC ( struct ifcommon_options, ifcommon_opts, 0, MAX_ARGUMENTS,
"[<interface>...]" );
static struct ifcommon_command_descriptor autoboot_cmd =
IFCOMMON_COMMAND_DESC ( struct autoboot_options, autoboot_opts,
0, MAX_ARGUMENTS, "[<interface>...]",
autoboot_payload, 0 );
/**
* "autoboot" command
@ -46,7 +65,7 @@ static struct command_descriptor autoboot_cmd =
* @ret rc Return status code
*/
static int autoboot_exec ( int argc, char **argv ) {
return ifcommon_exec ( argc, argv, &autoboot_cmd, netboot, 0 );
return ifcommon_exec ( argc, argv, &autoboot_cmd );
}
/** Booting commands */

View File

@ -41,18 +41,21 @@ FILE_LICENCE ( GPL2_OR_LATER );
*
*/
/** "dhcp" command descriptor */
static struct command_descriptor dhcp_cmd =
COMMAND_DESC ( struct ifcommon_options, ifcommon_opts, 0, MAX_ARGUMENTS,
"[<interface>...]" );
/** "dhcp" options */
struct dhcp_options {};
/** "dhcp" option list */
static struct option_descriptor dhcp_opts[] = {};
/**
* Execute "dhcp" command for a network device
*
* @v netdev Network device
* @v opts Command options
* @ret rc Return status code
*/
static int dhcp_payload ( struct net_device *netdev ) {
static int dhcp_payload ( struct net_device *netdev,
struct dhcp_options *opts __unused ) {
int rc;
if ( ( rc = dhcp ( netdev ) ) != 0 ) {
@ -68,6 +71,12 @@ static int dhcp_payload ( struct net_device *netdev ) {
return 0;
}
/** "dhcp" command descriptor */
static struct ifcommon_command_descriptor dhcp_cmd =
IFCOMMON_COMMAND_DESC ( struct dhcp_options, dhcp_opts,
0, MAX_ARGUMENTS, "[<interface>...]",
dhcp_payload, 1 );
/**
* The "dhcp" command
*
@ -76,7 +85,7 @@ static int dhcp_payload ( struct net_device *netdev ) {
* @ret rc Return status code
*/
static int dhcp_exec ( int argc, char **argv ) {
return ifcommon_exec ( argc, argv, &dhcp_cmd, dhcp_payload, 1 );
return ifcommon_exec ( argc, argv, &dhcp_cmd );
}
/** "pxebs" options */

View File

@ -34,9 +34,6 @@ FILE_LICENCE ( GPL2_OR_LATER );
*
*/
/** "if<xxx>" command options */
struct option_descriptor ifcommon_opts[0];
/**
* Execute if<xxx> command
*
@ -48,16 +45,15 @@ struct option_descriptor ifcommon_opts[0];
* @ret rc Return status code
*/
int ifcommon_exec ( int argc, char **argv,
struct command_descriptor *cmd,
int ( * payload ) ( struct net_device * ),
int stop_on_first_success ) {
struct ifcommon_options opts;
struct ifcommon_command_descriptor *ifcmd ) {
struct command_descriptor *cmd = &ifcmd->cmd;
uint8_t opts[cmd->len];
struct net_device *netdev;
int i;
int rc;
/* Parse options */
if ( ( rc = parse_options ( argc, argv, cmd, &opts ) ) != 0 )
if ( ( rc = parse_options ( argc, argv, cmd, opts ) ) != 0 )
return rc;
if ( optind != argc ) {
@ -65,8 +61,8 @@ int ifcommon_exec ( int argc, char **argv,
for ( i = optind ; i < argc ; i++ ) {
if ( ( rc = parse_netdev ( argv[i], &netdev ) ) != 0 )
continue;
if ( ( ( rc = payload ( netdev ) ) == 0 ) &&
stop_on_first_success ) {
if ( ( ( rc = ifcmd->payload ( netdev, opts ) ) == 0 )
&& ifcmd->stop_on_first_success ) {
return 0;
}
}
@ -74,8 +70,8 @@ int ifcommon_exec ( int argc, char **argv,
/* Try all interfaces */
rc = -ENODEV;
for_each_netdev ( netdev ) {
if ( ( ( rc = payload ( netdev ) ) == 0 ) &&
stop_on_first_success ) {
if ( ( ( rc = ifcmd->payload ( netdev, opts ) ) == 0 )
&& ifcmd->stop_on_first_success ) {
return 0;
}
}
@ -84,21 +80,30 @@ int ifcommon_exec ( int argc, char **argv,
return rc;
}
/** "ifopen" command descriptor */
static struct command_descriptor ifopen_cmd =
COMMAND_DESC ( struct ifcommon_options, ifcommon_opts, 0, MAX_ARGUMENTS,
"[<interface>...]" );
/** "ifopen" options */
struct ifopen_options {};
/** "ifopen" option list */
static struct option_descriptor ifopen_opts[] = {};
/**
* "ifopen" payload
*
* @v netdev Network device
* @v opts Command options
* @ret rc Return status code
*/
static int ifopen_payload ( struct net_device *netdev ) {
static int ifopen_payload ( struct net_device *netdev,
struct ifopen_options *opts __unused ) {
return ifopen ( netdev );
}
/** "ifopen" command descriptor */
static struct ifcommon_command_descriptor ifopen_cmd =
IFCOMMON_COMMAND_DESC ( struct ifopen_options, ifopen_opts,
0, MAX_ARGUMENTS, "[<interface>...]",
ifopen_payload, 0 );
/**
* The "ifopen" command
*
@ -107,25 +112,34 @@ static int ifopen_payload ( struct net_device *netdev ) {
* @ret rc Return status code
*/
static int ifopen_exec ( int argc, char **argv ) {
return ifcommon_exec ( argc, argv, &ifopen_cmd, ifopen_payload, 0 );
return ifcommon_exec ( argc, argv, &ifopen_cmd );
}
/** "ifclose" command descriptor */
static struct command_descriptor ifclose_cmd =
COMMAND_DESC ( struct ifcommon_options, ifcommon_opts, 0, MAX_ARGUMENTS,
"[<interface>...]" );
/** "ifclose" options */
struct ifclose_options {};
/** "ifclose" option list */
static struct option_descriptor ifclose_opts[] = {};
/**
* "ifclose" payload
*
* @v netdev Network device
* @v opts Command options
* @ret rc Return status code
*/
static int ifclose_payload ( struct net_device *netdev ) {
static int ifclose_payload ( struct net_device *netdev,
struct ifclose_options *opts __unused ) {
ifclose ( netdev );
return 0;
}
/** "ifclose" command descriptor */
static struct ifcommon_command_descriptor ifclose_cmd =
IFCOMMON_COMMAND_DESC ( struct ifclose_options, ifclose_opts,
0, MAX_ARGUMENTS, "[<interface>...]",
ifclose_payload, 0 );
/**
* The "ifclose" command
*
@ -134,25 +148,34 @@ static int ifclose_payload ( struct net_device *netdev ) {
* @ret rc Return status code
*/
static int ifclose_exec ( int argc, char **argv ) {
return ifcommon_exec ( argc, argv, &ifclose_cmd, ifclose_payload, 0 );
return ifcommon_exec ( argc, argv, &ifclose_cmd );
}
/** "ifstat" command descriptor */
static struct command_descriptor ifstat_cmd =
COMMAND_DESC ( struct ifcommon_options, ifcommon_opts, 0, MAX_ARGUMENTS,
"[<interface>...]" );
/** "ifstat" options */
struct ifstat_options {};
/** "ifstat" option list */
static struct option_descriptor ifstat_opts[] = {};
/**
* "ifstat" payload
*
* @v netdev Network device
* @v opts Command options
* @ret rc Return status code
*/
static int ifstat_payload ( struct net_device *netdev ) {
static int ifstat_payload ( struct net_device *netdev,
struct ifstat_options *opts __unused ) {
ifstat ( netdev );
return 0;
}
/** "ifstat" command descriptor */
static struct ifcommon_command_descriptor ifstat_cmd =
IFCOMMON_COMMAND_DESC ( struct ifstat_options, ifstat_opts,
0, MAX_ARGUMENTS, "[<interface>...]",
ifstat_payload, 0 );
/**
* The "ifstat" command
*
@ -161,7 +184,7 @@ static int ifstat_payload ( struct net_device *netdev ) {
* @ret rc Return status code
*/
static int ifstat_exec ( int argc, char **argv ) {
return ifcommon_exec ( argc, argv, &ifstat_cmd, ifstat_payload, 0 );
return ifcommon_exec ( argc, argv, &ifstat_cmd );
}
/** Interface management commands */

View File

@ -32,18 +32,21 @@ FILE_LICENCE ( GPL2_OR_LATER );
*
*/
/** "iwstat" command descriptor */
static struct command_descriptor iwstat_cmd =
COMMAND_DESC ( struct ifcommon_options, ifcommon_opts, 0, MAX_ARGUMENTS,
"[<interface>...]" );
/** "iwstat" options */
struct iwstat_options {};
/** "iwstat" option list */
static struct option_descriptor iwstat_opts[] = {};
/**
* "iwstat" payload
*
* @v netdev Network device
* @v opts Command options
* @ret rc Return status code
*/
static int iwstat_payload ( struct net_device *netdev ) {
static int iwstat_payload ( struct net_device *netdev,
struct iwstat_options *opts __unused ) {
struct net80211_device *dev = net80211_get ( netdev );
if ( dev )
@ -52,6 +55,12 @@ static int iwstat_payload ( struct net_device *netdev ) {
return 0;
}
/** "iwstat" command descriptor */
static struct ifcommon_command_descriptor iwstat_cmd =
IFCOMMON_COMMAND_DESC ( struct iwstat_options, iwstat_opts,
0, MAX_ARGUMENTS, "[<interface>...]",
iwstat_payload, 0 );
/**
* The "iwstat" command
*
@ -60,21 +69,24 @@ static int iwstat_payload ( struct net_device *netdev ) {
* @ret rc Return status code
*/
static int iwstat_exec ( int argc, char **argv ) {
return ifcommon_exec ( argc, argv, &iwstat_cmd, iwstat_payload, 0 );
return ifcommon_exec ( argc, argv, &iwstat_cmd );
}
/** "iwlist" command descriptor */
static struct command_descriptor iwlist_cmd =
COMMAND_DESC ( struct ifcommon_options, ifcommon_opts, 0, MAX_ARGUMENTS,
"[<interface>...]" );
/** "iwlist" options */
struct iwlist_options {};
/** "iwlist" option list */
static struct option_descriptor iwlist_opts[] = {};
/**
* "iwlist" payload
*
* @v netdev Network device
* @v opts Command options
* @ret rc Return status code
*/
static int iwlist_payload ( struct net_device *netdev ) {
static int iwlist_payload ( struct net_device *netdev,
struct iwlist_options *opts __unused ) {
struct net80211_device *dev = net80211_get ( netdev );
if ( dev )
@ -83,6 +95,12 @@ static int iwlist_payload ( struct net_device *netdev ) {
return 0;
}
/** "iwlist" command descriptor */
static struct ifcommon_command_descriptor iwlist_cmd =
IFCOMMON_COMMAND_DESC ( struct iwlist_options, iwlist_opts,
0, MAX_ARGUMENTS, "[<interface>...]",
iwlist_payload, 0 );
/**
* The "iwlist" command
*
@ -91,7 +109,7 @@ static int iwlist_payload ( struct net_device *netdev ) {
* @ret rc Return status code
*/
static int iwlist_exec ( int argc, char **argv ) {
return ifcommon_exec ( argc, argv, &iwlist_cmd, iwlist_payload, 0 );
return ifcommon_exec ( argc, argv, &iwlist_cmd );
}
/** Wireless interface management commands */

View File

@ -26,13 +26,46 @@ FILE_LICENCE ( GPL2_OR_LATER );
struct net_device;
struct ifcommon_options {};
/** An "if<xxx>" command descriptor */
struct ifcommon_command_descriptor {
/** Command descriptor */
struct command_descriptor cmd;
/** Payload
*
* @v netdev Network device
* @v opts Command options
* @ret rc Return status code
*/
int ( * payload ) ( struct net_device *netdev, void *opts );
/** Stop on first success */
int stop_on_first_success;
};
extern struct option_descriptor ifcommon_opts[0];
/**
* Construct "if<xxx>" command descriptor
*
* @v _struct Options structure type
* @v _options Option descriptor array
* @v _check_args Remaining argument checker
* @v _usage Command usage
* @ret _command Command descriptor
*/
#define IFCOMMON_COMMAND_DESC( _struct, _options, _min_args, \
_max_args, _usage, _payload, \
_stop_on_first_success ) \
{ \
.cmd = COMMAND_DESC ( _struct, _options, _min_args, \
_max_args, _usage ), \
.payload = ( ( int ( * ) ( struct net_device *netdev, \
void *opts ) ) \
( ( ( ( int ( * ) ( struct net_device *, \
_struct * ) ) NULL ) \
== ( typeof ( _payload ) * ) NULL ) \
? _payload : _payload ) ), \
.stop_on_first_success = _stop_on_first_success, \
}
extern int ifcommon_exec ( int argc, char **argv,
struct command_descriptor *cmd,
int ( * payload ) ( struct net_device * ),
int stop_on_first_success );
struct ifcommon_command_descriptor *cmd );
#endif /* _IFMGMT_CMD_H */