Explorar el Código

[ifmgmt] Use generic option-parsing library

Total cost: 66 bytes

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown hace 14 años
padre
commit
398a6e9a50

+ 73
- 2
src/arch/i386/hci/commands/pxe_cmd.c Ver fichero

@@ -1,26 +1,97 @@
1
+/*
2
+ * Copyright (C) 2010 Michael Brown <mbrown@fensystems.co.uk>.
3
+ *
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.
8
+ *
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.
13
+ *
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.
17
+ */
18
+
1 19
 #include <ipxe/netdevice.h>
2 20
 #include <ipxe/command.h>
21
+#include <ipxe/parseopt.h>
3 22
 #include <hci/ifmgmt_cmd.h>
4 23
 #include <pxe_call.h>
5 24
 
6 25
 FILE_LICENCE ( GPL2_OR_LATER );
7 26
 
27
+/** @file
28
+ *
29
+ * PXE commands
30
+ *
31
+ */
32
+
33
+/** "startpxe" command descriptor */
34
+static struct command_descriptor startpxe_cmd =
35
+	COMMAND_DESC ( struct ifcommon_options, ifcommon_opts, 0, MAX_ARGUMENTS,
36
+		       "[<interface>]", "" );
37
+
38
+/**
39
+ * "startpxe" payload
40
+ *
41
+ * @v netdev		Network device
42
+ * @ret rc		Return status code
43
+ */
8 44
 static int startpxe_payload ( struct net_device *netdev ) {
45
+
9 46
 	if ( netdev_is_open ( netdev ) )
10 47
 		pxe_activate ( netdev );
48
+
11 49
 	return 0;
12 50
 }
13 51
 
52
+/**
53
+ * The "startpxe" command
54
+ *
55
+ * @v argc		Argument count
56
+ * @v argv		Argument list
57
+ * @ret rc		Return status code
58
+ */
14 59
 static int startpxe_exec ( int argc, char **argv ) {
15
-	return ifcommon_exec ( argc, argv, startpxe_payload,
16
-			       "Activate PXE on" );
60
+	return ifcommon_exec ( argc, argv, &startpxe_cmd, startpxe_payload, 0 );
17 61
 }
18 62
 
63
+/** "stoppxe" options */
64
+struct stoppxe_options {};
65
+
66
+/** "stoppxe" option list */
67
+static struct option_descriptor stoppxe_opts[] = {};
68
+
69
+/** "stoppxe" command descriptor */
70
+static struct command_descriptor stoppxe_cmd =
71
+	COMMAND_DESC ( struct stoppxe_options, stoppxe_opts, 0, 0,
72
+		       "", "" );
73
+
74
+/**
75
+ * The "stoppxe" command
76
+ *
77
+ * @v argc		Argument count
78
+ * @v argv		Argument list
79
+ * @ret rc		Return status code
80
+ */
19 81
 static int stoppxe_exec ( int argc __unused, char **argv __unused ) {
82
+	struct stoppxe_options opts;
83
+	int rc;
84
+
85
+	/* Parse options */
86
+	if ( ( rc = parse_options ( argc, argv, &stoppxe_cmd, &opts ) ) != 0 )
87
+		return rc;
88
+
20 89
 	pxe_deactivate();
90
+
21 91
 	return 0;
22 92
 }
23 93
 
94
+/** PXE commands */
24 95
 struct command pxe_commands[] __command = {
25 96
 	{
26 97
 		.name = "startpxe",

+ 92
- 88
src/hci/commands/ifmgmt_cmd.c Ver fichero

@@ -19,9 +19,11 @@
19 19
 FILE_LICENCE ( GPL2_OR_LATER );
20 20
 
21 21
 #include <stdio.h>
22
+#include <errno.h>
22 23
 #include <getopt.h>
23 24
 #include <ipxe/netdevice.h>
24 25
 #include <ipxe/command.h>
26
+#include <ipxe/parseopt.h>
25 27
 #include <usr/ifmgmt.h>
26 28
 #include <hci/ifmgmt_cmd.h>
27 29
 
@@ -31,136 +33,138 @@ FILE_LICENCE ( GPL2_OR_LATER );
31 33
  *
32 34
  */
33 35
 
34
-/** Options shared by all if<xxx> commands */
35
-static struct option ifcommon_longopts[] = {
36
-	{ "help", 0, NULL, 'h' },
37
-	{ NULL, 0, NULL, 0 },
38
-};
39
-
40
-/**
41
- * Print syntax of if<xxx> command
42
- *
43
- * @v argv		Command arguments
44
- * @v verb		Verb describing the action of the command
45
- */
46
-static void ifcommon_syntax ( char **argv, const char *verb ) {
47
-	printf ( "Usage:\n"
48
-		 "  %s [<interface>] [<interface>...]\n"
49
-		 "\n"
50
-		 "%s the specified network interfaces\n",
51
-		 argv[0], verb );
52
-}
53
-
54
-/**
55
- * Execute if<xxx> command over all network devices
56
- *
57
- * @v payload		Command to execute
58
- * @ret rc		Exit code
59
- */
60
-static int ifcommon_do_all ( int ( * payload ) ( struct net_device * ) ) {
61
-	struct net_device *netdev;
62
-	int rc = 0;
63
-
64
-	/* Execute payload for each network device */
65
-	for_each_netdev ( netdev ) {
66
-		if ( payload ( netdev ) != 0 )
67
-			rc = 1;
68
-	}
69
-	return rc;
70
-}
71
-
72
-/**
73
- * Execute if<xxx> command over list of network devices
74
- *
75
- * @v payload		Command to execute
76
- * @ret rc		Exit code
77
- */
78
-static int ifcommon_do_list ( int ( * payload ) ( struct net_device * ),
79
-			      char **list, unsigned int count ) {
80
-	const char *netdev_name;
81
-	struct net_device *netdev;
82
-	int rc = 0;
83
-
84
-	while ( count-- ) {
85
-		netdev_name = *(list++);
86
-		netdev = find_netdev ( netdev_name );
87
-		if ( ! netdev ) {
88
-			printf ( "%s: no such interface\n", netdev_name );
89
-			rc = 1;
90
-			continue;
91
-		}
92
-		if ( payload ( netdev ) != 0 )
93
-			rc = 1;
94
-	}
95
-	return rc;
96
-}
36
+/** "if<xxx>" command options */
37
+struct option_descriptor ifcommon_opts[0];
97 38
 
98 39
 /**
99 40
  * Execute if<xxx> command
100 41
  *
101 42
  * @v argc		Argument count
102 43
  * @v argv		Argument list
44
+ * @v cmd		Command descriptor
103 45
  * @v payload		Command to execute
104 46
  * @v verb		Verb describing the action of the command
105
- * @ret rc		Exit code
47
+ * @ret rc		Return status code
106 48
  */
107 49
 int ifcommon_exec ( int argc, char **argv,
50
+		    struct command_descriptor *cmd,
108 51
 		    int ( * payload ) ( struct net_device * ),
109
-		    const char *verb ) {
110
-	int c;
52
+		    int stop_on_first_success ) {
53
+	struct ifcommon_options opts;
54
+	struct net_device *netdev;
55
+	int rc;
111 56
 
112 57
 	/* Parse options */
113
-	while ( ( c = getopt_long ( argc, argv, "h", ifcommon_longopts,
114
-				    NULL ) ) >= 0 ) {
115
-		switch ( c ) {
116
-		case 'h':
117
-			/* Display help text */
118
-		default:
119
-			/* Unrecognised/invalid option */
120
-			ifcommon_syntax ( argv, verb );
121
-			return 1;
58
+	if ( ( rc = parse_options ( argc, argv, cmd, &opts ) ) != 0 )
59
+		return rc;
60
+
61
+	if ( optind != argc ) {
62
+		/* Treat arguments as a list of interfaces to try */
63
+		while ( optind != argc ) {
64
+			if ( ( rc = parse_netdev ( argv[optind++],
65
+						   &netdev ) ) != 0 ) {
66
+				continue;
67
+			}
68
+			if ( ( ( rc = payload ( netdev ) ) == 0 ) &&
69
+			     stop_on_first_success ) {
70
+				return 0;
71
+			}
122 72
 		}
123
-	}
124
-
125
-	if ( optind == argc ) {
126
-		return ifcommon_do_all ( payload );
127 73
 	} else {
128
-		return ifcommon_do_list ( payload, &argv[optind],
129
-					  ( argc - optind ) );
74
+		/* Try all interfaces */
75
+		rc = -ENODEV;
76
+		for_each_netdev ( netdev ) {
77
+			if ( ( ( rc = payload ( netdev ) ) == 0 ) &&
78
+			     stop_on_first_success ) {
79
+				return 0;
80
+			}
81
+		}
130 82
 	}
83
+
84
+	return rc;
131 85
 }
132 86
 
133
-/* "ifopen" command */
87
+/** "ifopen" command descriptor */
88
+static struct command_descriptor ifopen_cmd =
89
+	COMMAND_DESC ( struct ifcommon_options, ifcommon_opts, 0, MAX_ARGUMENTS,
90
+		       "[<interface>...]",
91
+		       "Open network interface(s)" );
134 92
 
93
+/**
94
+ * "ifopen" payload
95
+ *
96
+ * @v netdev		Network device
97
+ * @ret rc		Return status code
98
+ */
135 99
 static int ifopen_payload ( struct net_device *netdev ) {
136 100
 	return ifopen ( netdev );
137 101
 }
138 102
 
103
+/**
104
+ * The "ifopen" command
105
+ *
106
+ * @v argc		Argument count
107
+ * @v argv		Argument list
108
+ * @ret rc		Return status code
109
+ */
139 110
 static int ifopen_exec ( int argc, char **argv ) {
140
-	return ifcommon_exec ( argc, argv, ifopen_payload, "Open" );
111
+	return ifcommon_exec ( argc, argv, &ifopen_cmd, ifopen_payload, 0 );
141 112
 }
142 113
 
143
-/* "ifclose" command */
114
+/** "ifclose" command descriptor */
115
+static struct command_descriptor ifclose_cmd =
116
+	COMMAND_DESC ( struct ifcommon_options, ifcommon_opts, 0, MAX_ARGUMENTS,
117
+		       "[<interface>...]",
118
+		       "Close network interface(s)" );
144 119
 
120
+/**
121
+ * "ifclose" payload
122
+ *
123
+ * @v netdev		Network device
124
+ * @ret rc		Return status code
125
+ */
145 126
 static int ifclose_payload ( struct net_device *netdev ) {
146 127
 	ifclose ( netdev );
147 128
 	return 0;
148 129
 }
149 130
 
131
+/**
132
+ * The "ifclose" command
133
+ *
134
+ * @v argc		Argument count
135
+ * @v argv		Argument list
136
+ * @ret rc		Return status code
137
+ */
150 138
 static int ifclose_exec ( int argc, char **argv ) {
151
-	return ifcommon_exec ( argc, argv, ifclose_payload, "Close" );
139
+	return ifcommon_exec ( argc, argv, &ifclose_cmd, ifclose_payload, 0 );
152 140
 }
153 141
 
154
-/* "ifstat" command */
142
+/** "ifstat" command descriptor */
143
+static struct command_descriptor ifstat_cmd =
144
+	COMMAND_DESC ( struct ifcommon_options, ifcommon_opts, 0, MAX_ARGUMENTS,
145
+		       "[<interface>...]",
146
+		       "Show network interface(s)" );
155 147
 
148
+/**
149
+ * "ifstat" payload
150
+ *
151
+ * @v netdev		Network device
152
+ * @ret rc		Return status code
153
+ */
156 154
 static int ifstat_payload ( struct net_device *netdev ) {
157 155
 	ifstat ( netdev );
158 156
 	return 0;
159 157
 }
160 158
 
159
+/**
160
+ * The "ifstat" command
161
+ *
162
+ * @v argc		Argument count
163
+ * @v argv		Argument list
164
+ * @ret rc		Return status code
165
+ */
161 166
 static int ifstat_exec ( int argc, char **argv ) {
162
-	return ifcommon_exec ( argc, argv,
163
-			       ifstat_payload, "Display status of" );
167
+	return ifcommon_exec ( argc, argv, &ifstat_cmd, ifstat_payload, 0 );
164 168
 }
165 169
 
166 170
 /** Interface management commands */

+ 45
- 6
src/hci/commands/iwmgmt_cmd.c Ver fichero

@@ -21,11 +21,28 @@ FILE_LICENCE ( GPL2_OR_LATER );
21 21
 #include <ipxe/netdevice.h>
22 22
 #include <ipxe/net80211.h>
23 23
 #include <ipxe/command.h>
24
+#include <ipxe/parseopt.h>
24 25
 #include <usr/iwmgmt.h>
25 26
 #include <hci/ifmgmt_cmd.h>
26 27
 
27
-/* "iwstat" command */
28
+/** @file
29
+ *
30
+ * Wireless interface management commands
31
+ *
32
+ */
28 33
 
34
+/** "iwstat" command descriptor */
35
+static struct command_descriptor iwstat_cmd =
36
+	COMMAND_DESC ( struct ifcommon_options, ifcommon_opts, 0, MAX_ARGUMENTS,
37
+		       "[<interface>...]",
38
+		       "Show wireless interface(s)" );
39
+
40
+/**
41
+ * "iwstat" payload
42
+ *
43
+ * @v netdev		Network device
44
+ * @ret rc		Return status code
45
+ */
29 46
 static int iwstat_payload ( struct net_device *netdev ) {
30 47
 	struct net80211_device *dev = net80211_get ( netdev );
31 48
 
@@ -35,13 +52,29 @@ static int iwstat_payload ( struct net_device *netdev ) {
35 52
 	return 0;
36 53
 }
37 54
 
55
+/**
56
+ * The "iwstat" command
57
+ *
58
+ * @v argc		Argument count
59
+ * @v argv		Argument list
60
+ * @ret rc		Return status code
61
+ */
38 62
 static int iwstat_exec ( int argc, char **argv ) {
39
-	return ifcommon_exec ( argc, argv,
40
-			       iwstat_payload, "Display wireless status of" );
63
+	return ifcommon_exec ( argc, argv, &iwstat_cmd, iwstat_payload, 0 );
41 64
 }
42 65
 
43
-/* "iwlist" command */
66
+/** "iwlist" command descriptor */
67
+static struct command_descriptor iwlist_cmd =
68
+	COMMAND_DESC ( struct ifcommon_options, ifcommon_opts, 0, MAX_ARGUMENTS,
69
+		       "[<interface>...]",
70
+		       "List wireless networks" );
44 71
 
72
+/**
73
+ * "iwlist" payload
74
+ *
75
+ * @v netdev		Network device
76
+ * @ret rc		Return status code
77
+ */
45 78
 static int iwlist_payload ( struct net_device *netdev ) {
46 79
 	struct net80211_device *dev = net80211_get ( netdev );
47 80
 
@@ -51,9 +84,15 @@ static int iwlist_payload ( struct net_device *netdev ) {
51 84
 	return 0;
52 85
 }
53 86
 
87
+/**
88
+ * The "iwlist" command
89
+ *
90
+ * @v argc		Argument count
91
+ * @v argv		Argument list
92
+ * @ret rc		Return status code
93
+ */
54 94
 static int iwlist_exec ( int argc, char **argv ) {
55
-	return ifcommon_exec ( argc, argv, iwlist_payload,
56
-			       "List wireless networks available via" );
95
+	return ifcommon_exec ( argc, argv, &iwlist_cmd, iwlist_payload, 0 );
57 96
 }
58 97
 
59 98
 /** Wireless interface management commands */

+ 8
- 1
src/include/hci/ifmgmt_cmd.h Ver fichero

@@ -21,10 +21,17 @@
21 21
 
22 22
 FILE_LICENCE ( GPL2_OR_LATER );
23 23
 
24
+#include <ipxe/parseopt.h>
25
+
24 26
 struct net_device;
25 27
 
28
+struct ifcommon_options {};
29
+
30
+extern struct option_descriptor ifcommon_opts[0];
31
+
26 32
 extern int ifcommon_exec (  int argc, char **argv,
33
+			    struct command_descriptor *cmd,
27 34
 			    int ( * payload ) ( struct net_device * ),
28
-			    const char *verb );
35
+			    int stop_on_first_success );
29 36
 
30 37
 #endif /* _IFMGMT_CMD_H */

+ 1
- 0
src/include/ipxe/errfile.h Ver fichero

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

Loading…
Cancelar
Guardar