mirror of https://github.com/ipxe/ipxe.git
[ifmgmt] Use generic option-parsing library
Total cost: 66 bytes Signed-off-by: Michael Brown <mcb30@ipxe.org>pull/1/head
parent
9e9c9adf10
commit
398a6e9a50
|
@ -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/netdevice.h>
|
||||||
#include <ipxe/command.h>
|
#include <ipxe/command.h>
|
||||||
|
#include <ipxe/parseopt.h>
|
||||||
#include <hci/ifmgmt_cmd.h>
|
#include <hci/ifmgmt_cmd.h>
|
||||||
#include <pxe_call.h>
|
#include <pxe_call.h>
|
||||||
|
|
||||||
FILE_LICENCE ( GPL2_OR_LATER );
|
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 ) {
|
static int startpxe_payload ( struct net_device *netdev ) {
|
||||||
|
|
||||||
if ( netdev_is_open ( netdev ) )
|
if ( netdev_is_open ( netdev ) )
|
||||||
pxe_activate ( netdev );
|
pxe_activate ( netdev );
|
||||||
|
|
||||||
return 0;
|
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 ) {
|
static int startpxe_exec ( int argc, char **argv ) {
|
||||||
return ifcommon_exec ( argc, argv, startpxe_payload,
|
return ifcommon_exec ( argc, argv, &startpxe_cmd, startpxe_payload, 0 );
|
||||||
"Activate PXE on" );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** "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 ) {
|
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();
|
pxe_deactivate();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** PXE commands */
|
||||||
struct command pxe_commands[] __command = {
|
struct command pxe_commands[] __command = {
|
||||||
{
|
{
|
||||||
.name = "startpxe",
|
.name = "startpxe",
|
||||||
|
|
|
@ -19,9 +19,11 @@
|
||||||
FILE_LICENCE ( GPL2_OR_LATER );
|
FILE_LICENCE ( GPL2_OR_LATER );
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <errno.h>
|
||||||
#include <getopt.h>
|
#include <getopt.h>
|
||||||
#include <ipxe/netdevice.h>
|
#include <ipxe/netdevice.h>
|
||||||
#include <ipxe/command.h>
|
#include <ipxe/command.h>
|
||||||
|
#include <ipxe/parseopt.h>
|
||||||
#include <usr/ifmgmt.h>
|
#include <usr/ifmgmt.h>
|
||||||
#include <hci/ifmgmt_cmd.h>
|
#include <hci/ifmgmt_cmd.h>
|
||||||
|
|
||||||
|
@ -31,136 +33,138 @@ FILE_LICENCE ( GPL2_OR_LATER );
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/** Options shared by all if<xxx> commands */
|
/** "if<xxx>" command options */
|
||||||
static struct option ifcommon_longopts[] = {
|
struct option_descriptor ifcommon_opts[0];
|
||||||
{ "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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute if<xxx> command
|
* Execute if<xxx> command
|
||||||
*
|
*
|
||||||
* @v argc Argument count
|
* @v argc Argument count
|
||||||
* @v argv Argument list
|
* @v argv Argument list
|
||||||
|
* @v cmd Command descriptor
|
||||||
* @v payload Command to execute
|
* @v payload Command to execute
|
||||||
* @v verb Verb describing the action of the command
|
* @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,
|
int ifcommon_exec ( int argc, char **argv,
|
||||||
|
struct command_descriptor *cmd,
|
||||||
int ( * payload ) ( struct net_device * ),
|
int ( * payload ) ( struct net_device * ),
|
||||||
const char *verb ) {
|
int stop_on_first_success ) {
|
||||||
int c;
|
struct ifcommon_options opts;
|
||||||
|
struct net_device *netdev;
|
||||||
|
int rc;
|
||||||
|
|
||||||
/* Parse options */
|
/* Parse options */
|
||||||
while ( ( c = getopt_long ( argc, argv, "h", ifcommon_longopts,
|
if ( ( rc = parse_options ( argc, argv, cmd, &opts ) ) != 0 )
|
||||||
NULL ) ) >= 0 ) {
|
return rc;
|
||||||
switch ( c ) {
|
|
||||||
case 'h':
|
if ( optind != argc ) {
|
||||||
/* Display help text */
|
/* Treat arguments as a list of interfaces to try */
|
||||||
default:
|
while ( optind != argc ) {
|
||||||
/* Unrecognised/invalid option */
|
if ( ( rc = parse_netdev ( argv[optind++],
|
||||||
ifcommon_syntax ( argv, verb );
|
&netdev ) ) != 0 ) {
|
||||||
return 1;
|
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 rc;
|
||||||
return ifcommon_do_all ( payload );
|
|
||||||
} else {
|
|
||||||
return ifcommon_do_list ( payload, &argv[optind],
|
|
||||||
( argc - optind ) );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* "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 ) {
|
static int ifopen_payload ( struct net_device *netdev ) {
|
||||||
return ifopen ( 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 ) {
|
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 ) {
|
static int ifclose_payload ( struct net_device *netdev ) {
|
||||||
ifclose ( netdev );
|
ifclose ( netdev );
|
||||||
return 0;
|
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 ) {
|
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 ) {
|
static int ifstat_payload ( struct net_device *netdev ) {
|
||||||
ifstat ( netdev );
|
ifstat ( netdev );
|
||||||
return 0;
|
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 ) {
|
static int ifstat_exec ( int argc, char **argv ) {
|
||||||
return ifcommon_exec ( argc, argv,
|
return ifcommon_exec ( argc, argv, &ifstat_cmd, ifstat_payload, 0 );
|
||||||
ifstat_payload, "Display status of" );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Interface management commands */
|
/** Interface management commands */
|
||||||
|
|
|
@ -21,11 +21,28 @@ FILE_LICENCE ( GPL2_OR_LATER );
|
||||||
#include <ipxe/netdevice.h>
|
#include <ipxe/netdevice.h>
|
||||||
#include <ipxe/net80211.h>
|
#include <ipxe/net80211.h>
|
||||||
#include <ipxe/command.h>
|
#include <ipxe/command.h>
|
||||||
|
#include <ipxe/parseopt.h>
|
||||||
#include <usr/iwmgmt.h>
|
#include <usr/iwmgmt.h>
|
||||||
#include <hci/ifmgmt_cmd.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 ) {
|
static int iwstat_payload ( struct net_device *netdev ) {
|
||||||
struct net80211_device *dev = net80211_get ( netdev );
|
struct net80211_device *dev = net80211_get ( netdev );
|
||||||
|
|
||||||
|
@ -35,13 +52,29 @@ static int iwstat_payload ( struct net_device *netdev ) {
|
||||||
return 0;
|
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 ) {
|
static int iwstat_exec ( int argc, char **argv ) {
|
||||||
return ifcommon_exec ( argc, argv,
|
return ifcommon_exec ( argc, argv, &iwstat_cmd, iwstat_payload, 0 );
|
||||||
iwstat_payload, "Display wireless status of" );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* "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 ) {
|
static int iwlist_payload ( struct net_device *netdev ) {
|
||||||
struct net80211_device *dev = net80211_get ( netdev );
|
struct net80211_device *dev = net80211_get ( netdev );
|
||||||
|
|
||||||
|
@ -51,9 +84,15 @@ static int iwlist_payload ( struct net_device *netdev ) {
|
||||||
return 0;
|
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 ) {
|
static int iwlist_exec ( int argc, char **argv ) {
|
||||||
return ifcommon_exec ( argc, argv, iwlist_payload,
|
return ifcommon_exec ( argc, argv, &iwlist_cmd, iwlist_payload, 0 );
|
||||||
"List wireless networks available via" );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Wireless interface management commands */
|
/** Wireless interface management commands */
|
||||||
|
|
|
@ -21,10 +21,17 @@
|
||||||
|
|
||||||
FILE_LICENCE ( GPL2_OR_LATER );
|
FILE_LICENCE ( GPL2_OR_LATER );
|
||||||
|
|
||||||
|
#include <ipxe/parseopt.h>
|
||||||
|
|
||||||
struct net_device;
|
struct net_device;
|
||||||
|
|
||||||
|
struct ifcommon_options {};
|
||||||
|
|
||||||
|
extern struct option_descriptor ifcommon_opts[0];
|
||||||
|
|
||||||
extern int ifcommon_exec ( int argc, char **argv,
|
extern int ifcommon_exec ( int argc, char **argv,
|
||||||
|
struct command_descriptor *cmd,
|
||||||
int ( * payload ) ( struct net_device * ),
|
int ( * payload ) ( struct net_device * ),
|
||||||
const char *verb );
|
int stop_on_first_success );
|
||||||
|
|
||||||
#endif /* _IFMGMT_CMD_H */
|
#endif /* _IFMGMT_CMD_H */
|
||||||
|
|
|
@ -229,6 +229,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
|
||||||
#define ERRFILE_linux_smbios ( ERRFILE_OTHER | 0x001a0000 )
|
#define ERRFILE_linux_smbios ( ERRFILE_OTHER | 0x001a0000 )
|
||||||
#define ERRFILE_lotest ( ERRFILE_OTHER | 0x001b0000 )
|
#define ERRFILE_lotest ( ERRFILE_OTHER | 0x001b0000 )
|
||||||
#define ERRFILE_config_cmd ( ERRFILE_OTHER | 0x001c0000 )
|
#define ERRFILE_config_cmd ( ERRFILE_OTHER | 0x001c0000 )
|
||||||
|
#define ERRFILE_ifmgmt_cmd ( ERRFILE_OTHER | 0x001d0000 )
|
||||||
|
|
||||||
/** @} */
|
/** @} */
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue