Adding debian version 3.70~pre8+dfsg-1.
[syslinux-debian/hramrach.git] / gpxe / src / hci / commands / ifmgmt_cmd.c
blobf2508e556cd9e3dbcade0c1f5b350efb225edbc4
1 /*
2 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include <stdio.h>
20 #include <getopt.h>
21 #include <gpxe/netdevice.h>
22 #include <gpxe/command.h>
23 #include <usr/ifmgmt.h>
25 /** @file
27 * Network interface management commands
31 /** Options shared by all if<xxx> commands */
32 static struct option ifcommon_longopts[] = {
33 { "help", 0, NULL, 'h' },
34 { NULL, 0, NULL, 0 },
37 /**
38 * Print syntax of if<xxx> command
40 * @v argv Command arguments
41 * @v verb Verb describing the action of the command
43 static void ifcommon_syntax ( char **argv, const char *verb ) {
44 printf ( "Usage:\n"
45 " %s [<interface>] [<interface>...]\n"
46 "\n"
47 "%s the specified network interfaces\n",
48 argv[0], verb );
51 /**
52 * Execute if<xxx> command over all network devices
54 * @v payload Command to execute
55 * @ret rc Exit code
57 static int ifcommon_do_all ( int ( * payload ) ( struct net_device * ) ) {
58 struct net_device *netdev;
59 int rc = 0;
61 /* Execute payload for each network device */
62 for_each_netdev ( netdev ) {
63 if ( payload ( netdev ) != 0 )
64 rc = 1;
66 return rc;
69 /**
70 * Execute if<xxx> command over list of network devices
72 * @v payload Command to execute
73 * @ret rc Exit code
75 static int ifcommon_do_list ( int ( * payload ) ( struct net_device * ),
76 char **list, unsigned int count ) {
77 const char *netdev_name;
78 struct net_device *netdev;
79 int rc = 0;
81 while ( count-- ) {
82 netdev_name = *(list++);
83 netdev = find_netdev ( netdev_name );
84 if ( ! netdev ) {
85 printf ( "%s: no such interface\n", netdev_name );
86 rc = 1;
87 continue;
89 if ( payload ( netdev ) != 0 )
90 rc = 1;
92 return rc;
95 /**
96 * Execute if<xxx> command
98 * @v payload Command to execute
99 * @v verb Verb describing the action of the command
100 * @v argc Argument count
101 * @v argv Argument list
102 * @ret rc Exit code
104 static __attribute__ (( regparm ( 2 ) )) int
105 ifcommon_exec ( int ( * payload ) ( struct net_device * ),
106 const char *verb, int argc, char **argv ) {
107 int c;
109 /* Parse options */
110 while ( ( c = getopt_long ( argc, argv, "h", ifcommon_longopts,
111 NULL ) ) >= 0 ) {
112 switch ( c ) {
113 case 'h':
114 /* Display help text */
115 default:
116 /* Unrecognised/invalid option */
117 ifcommon_syntax ( argv, verb );
118 return 1;
122 if ( optind == argc ) {
123 return ifcommon_do_all ( payload );
124 } else {
125 return ifcommon_do_list ( payload, &argv[optind],
126 ( argc - optind ) );
130 /* "ifopen" command */
132 static int ifopen_payload ( struct net_device *netdev ) {
133 return ifopen ( netdev );
136 static int ifopen_exec ( int argc, char **argv ) {
137 return ifcommon_exec ( ifopen_payload, "Open", argc, argv );
140 /* "ifclose" command */
142 static int ifclose_payload ( struct net_device *netdev ) {
143 ifclose ( netdev );
144 return 0;
147 static int ifclose_exec ( int argc, char **argv ) {
148 return ifcommon_exec ( ifclose_payload, "Close", argc, argv );
151 /* "ifstat" command */
153 static int ifstat_payload ( struct net_device *netdev ) {
154 ifstat ( netdev );
155 return 0;
158 static int ifstat_exec ( int argc, char **argv ) {
159 return ifcommon_exec ( ifstat_payload, "Display status of",
160 argc, argv );
163 /** Interface management commands */
164 struct command ifmgmt_commands[] __command = {
166 .name = "ifopen",
167 .exec = ifopen_exec,
170 .name = "ifclose",
171 .exec = ifclose_exec,
174 .name = "ifstat",
175 .exec = ifstat_exec,