From: Michael Brown Date: Mon, 4 Nov 2013 16:15:21 +0000 (+0000) Subject: [cmdline] Allow "if" commands to take options X-Git-Tag: v1.20.1~1379 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=55e85ad1ee318aa559f071d95741391eaf2dc1de;p=thirdparty%2Fipxe.git [cmdline] Allow "if" commands to take options Allow commands implemented using ifcommon_exec() to accept command-specific options. Signed-off-by: Michael Brown --- diff --git a/src/arch/i386/hci/commands/pxe_cmd.c b/src/arch/i386/hci/commands/pxe_cmd.c index f86f180ce..c7cdb07f8 100644 --- a/src/arch/i386/hci/commands/pxe_cmd.c +++ b/src/arch/i386/hci/commands/pxe_cmd.c @@ -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, - "[]" ); +/** "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, "[]", + 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 */ diff --git a/src/hci/commands/autoboot_cmd.c b/src/hci/commands/autoboot_cmd.c index f3886b1f6..62235a278 100644 --- a/src/hci/commands/autoboot_cmd.c +++ b/src/hci/commands/autoboot_cmd.c @@ -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, - "[...]" ); +static struct ifcommon_command_descriptor autoboot_cmd = + IFCOMMON_COMMAND_DESC ( struct autoboot_options, autoboot_opts, + 0, MAX_ARGUMENTS, "[...]", + 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 */ diff --git a/src/hci/commands/dhcp_cmd.c b/src/hci/commands/dhcp_cmd.c index 279620c32..1b7df7650 100644 --- a/src/hci/commands/dhcp_cmd.c +++ b/src/hci/commands/dhcp_cmd.c @@ -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, - "[...]" ); +/** "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, "[...]", + 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 */ diff --git a/src/hci/commands/ifmgmt_cmd.c b/src/hci/commands/ifmgmt_cmd.c index 350f14d07..771d947f6 100644 --- a/src/hci/commands/ifmgmt_cmd.c +++ b/src/hci/commands/ifmgmt_cmd.c @@ -34,9 +34,6 @@ FILE_LICENCE ( GPL2_OR_LATER ); * */ -/** "if" command options */ -struct option_descriptor ifcommon_opts[0]; - /** * Execute if 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, - "[...]" ); +/** "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, "[...]", + 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, - "[...]" ); +/** "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, "[...]", + 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, - "[...]" ); +/** "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, "[...]", + 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 */ diff --git a/src/hci/commands/iwmgmt_cmd.c b/src/hci/commands/iwmgmt_cmd.c index d91ad1e34..b61ee8c7b 100644 --- a/src/hci/commands/iwmgmt_cmd.c +++ b/src/hci/commands/iwmgmt_cmd.c @@ -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, - "[...]" ); +/** "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, "[...]", + 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, - "[...]" ); +/** "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, "[...]", + 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 */ diff --git a/src/include/hci/ifmgmt_cmd.h b/src/include/hci/ifmgmt_cmd.h index abdb27f43..2fa8ee527 100644 --- a/src/include/hci/ifmgmt_cmd.h +++ b/src/include/hci/ifmgmt_cmd.h @@ -26,13 +26,46 @@ FILE_LICENCE ( GPL2_OR_LATER ); struct net_device; -struct ifcommon_options {}; +/** An "if" 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" 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 */