Kaynağa Gözat

[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>
tags/v1.20.1
Michael Brown 10 yıl önce
ebeveyn
işleme
55e85ad1ee

+ 15
- 6
src/arch/i386/hci/commands/pxe_cmd.c Dosyayı Görüntüle

@@ -31,18 +31,21 @@ FILE_LICENCE ( GPL2_OR_LATER );
31 31
  *
32 32
  */
33 33
 
34
-/** "startpxe" command descriptor */
35
-static struct command_descriptor startpxe_cmd =
36
-	COMMAND_DESC ( struct ifcommon_options, ifcommon_opts, 0, MAX_ARGUMENTS,
37
-		       "[<interface>]" );
34
+/** "startpxe" options */
35
+struct startpxe_options {};
36
+
37
+/** "startpxe" option list */
38
+static struct option_descriptor startpxe_opts[] = {};
38 39
 
39 40
 /**
40 41
  * "startpxe" payload
41 42
  *
42 43
  * @v netdev		Network device
44
+ * @v opts		Command options
43 45
  * @ret rc		Return status code
44 46
  */
45
-static int startpxe_payload ( struct net_device *netdev ) {
47
+static int startpxe_payload ( struct net_device *netdev,
48
+			      struct startpxe_options *opts __unused ) {
46 49
 
47 50
 	if ( netdev_is_open ( netdev ) )
48 51
 		pxe_activate ( netdev );
@@ -50,6 +53,12 @@ static int startpxe_payload ( struct net_device *netdev ) {
50 53
 	return 0;
51 54
 }
52 55
 
56
+/** "startpxe" command descriptor */
57
+static struct ifcommon_command_descriptor startpxe_cmd =
58
+	IFCOMMON_COMMAND_DESC ( struct startpxe_options, startpxe_opts,
59
+				0, MAX_ARGUMENTS, "[<interface>]",
60
+				startpxe_payload, 0 );
61
+
53 62
 /**
54 63
  * The "startpxe" command
55 64
  *
@@ -58,7 +67,7 @@ static int startpxe_payload ( struct net_device *netdev ) {
58 67
  * @ret rc		Return status code
59 68
  */
60 69
 static int startpxe_exec ( int argc, char **argv ) {
61
-	return ifcommon_exec ( argc, argv, &startpxe_cmd, startpxe_payload, 0 );
70
+	return ifcommon_exec ( argc, argv, &startpxe_cmd );
62 71
 }
63 72
 
64 73
 /** "stoppxe" options */

+ 23
- 4
src/hci/commands/autoboot_cmd.c Dosyayı Görüntüle

@@ -33,10 +33,29 @@ FILE_LICENCE ( GPL2_OR_LATER );
33 33
  *
34 34
  */
35 35
 
36
+/** "autoboot" options */
37
+struct autoboot_options {};
38
+
39
+/** "autoboot" option list */
40
+static struct option_descriptor autoboot_opts[] = {};
41
+
42
+/**
43
+ * "autoboot" payload
44
+ *
45
+ * @v netdev		Network device
46
+ * @v opts		Command options
47
+ * @ret rc		Return status code
48
+ */
49
+static int autoboot_payload ( struct net_device *netdev,
50
+			      struct autoboot_options *opts __unused ) {
51
+	return netboot ( netdev );
52
+}
53
+
36 54
 /** "autoboot" command descriptor */
37
-static struct command_descriptor autoboot_cmd =
38
-	COMMAND_DESC ( struct ifcommon_options, ifcommon_opts, 0, MAX_ARGUMENTS,
39
-		       "[<interface>...]" );
55
+static struct ifcommon_command_descriptor autoboot_cmd =
56
+	IFCOMMON_COMMAND_DESC ( struct autoboot_options, autoboot_opts,
57
+				0, MAX_ARGUMENTS, "[<interface>...]",
58
+				autoboot_payload, 0 );
40 59
 
41 60
 /**
42 61
  * "autoboot" command
@@ -46,7 +65,7 @@ static struct command_descriptor autoboot_cmd =
46 65
  * @ret rc		Return status code
47 66
  */
48 67
 static int autoboot_exec ( int argc, char **argv ) {
49
-	return ifcommon_exec ( argc, argv, &autoboot_cmd, netboot, 0 );
68
+	return ifcommon_exec ( argc, argv, &autoboot_cmd );
50 69
 }
51 70
 
52 71
 /** Booting commands */

+ 15
- 6
src/hci/commands/dhcp_cmd.c Dosyayı Görüntüle

@@ -41,18 +41,21 @@ FILE_LICENCE ( GPL2_OR_LATER );
41 41
  *
42 42
  */
43 43
 
44
-/** "dhcp" command descriptor */
45
-static struct command_descriptor dhcp_cmd =
46
-	COMMAND_DESC ( struct ifcommon_options, ifcommon_opts, 0, MAX_ARGUMENTS,
47
-		       "[<interface>...]" );
44
+/** "dhcp" options */
45
+struct dhcp_options {};
46
+
47
+/** "dhcp" option list */
48
+static struct option_descriptor dhcp_opts[] = {};
48 49
 
49 50
 /**
50 51
  * Execute "dhcp" command for a network device
51 52
  *
52 53
  * @v netdev		Network device
54
+ * @v opts		Command options
53 55
  * @ret rc		Return status code
54 56
  */
55
-static int dhcp_payload ( struct net_device *netdev ) {
57
+static int dhcp_payload ( struct net_device *netdev,
58
+			  struct dhcp_options *opts __unused ) {
56 59
 	int rc;
57 60
 
58 61
 	if ( ( rc = dhcp ( netdev ) ) != 0 ) {
@@ -68,6 +71,12 @@ static int dhcp_payload ( struct net_device *netdev ) {
68 71
 	return 0;
69 72
 }
70 73
 
74
+/** "dhcp" command descriptor */
75
+static struct ifcommon_command_descriptor dhcp_cmd =
76
+	IFCOMMON_COMMAND_DESC ( struct dhcp_options, dhcp_opts,
77
+				0, MAX_ARGUMENTS, "[<interface>...]",
78
+				dhcp_payload, 1 );
79
+
71 80
 /**
72 81
  * The "dhcp" command
73 82
  *
@@ -76,7 +85,7 @@ static int dhcp_payload ( struct net_device *netdev ) {
76 85
  * @ret rc		Return status code
77 86
  */
78 87
 static int dhcp_exec ( int argc, char **argv ) {
79
-	return ifcommon_exec ( argc, argv, &dhcp_cmd, dhcp_payload, 1 );
88
+	return ifcommon_exec ( argc, argv, &dhcp_cmd );
80 89
 }
81 90
 
82 91
 /** "pxebs" options */

+ 53
- 30
src/hci/commands/ifmgmt_cmd.c Dosyayı Görüntüle

@@ -34,9 +34,6 @@ FILE_LICENCE ( GPL2_OR_LATER );
34 34
  *
35 35
  */
36 36
 
37
-/** "if<xxx>" command options */
38
-struct option_descriptor ifcommon_opts[0];
39
-
40 37
 /**
41 38
  * Execute if<xxx> command
42 39
  *
@@ -48,16 +45,15 @@ struct option_descriptor ifcommon_opts[0];
48 45
  * @ret rc		Return status code
49 46
  */
50 47
 int ifcommon_exec ( int argc, char **argv,
51
-		    struct command_descriptor *cmd,
52
-		    int ( * payload ) ( struct net_device * ),
53
-		    int stop_on_first_success ) {
54
-	struct ifcommon_options opts;
48
+		    struct ifcommon_command_descriptor *ifcmd ) {
49
+	struct command_descriptor *cmd = &ifcmd->cmd;
50
+	uint8_t opts[cmd->len];
55 51
 	struct net_device *netdev;
56 52
 	int i;
57 53
 	int rc;
58 54
 
59 55
 	/* Parse options */
60
-	if ( ( rc = parse_options ( argc, argv, cmd, &opts ) ) != 0 )
56
+	if ( ( rc = parse_options ( argc, argv, cmd, opts ) ) != 0 )
61 57
 		return rc;
62 58
 
63 59
 	if ( optind != argc ) {
@@ -65,8 +61,8 @@ int ifcommon_exec ( int argc, char **argv,
65 61
 		for ( i = optind ; i < argc ; i++ ) {
66 62
 			if ( ( rc = parse_netdev ( argv[i], &netdev ) ) != 0 )
67 63
 				continue;
68
-			if ( ( ( rc = payload ( netdev ) ) == 0 ) &&
69
-			     stop_on_first_success ) {
64
+			if ( ( ( rc = ifcmd->payload ( netdev, opts ) ) == 0 )
65
+			     && ifcmd->stop_on_first_success ) {
70 66
 				return 0;
71 67
 			}
72 68
 		}
@@ -74,8 +70,8 @@ int ifcommon_exec ( int argc, char **argv,
74 70
 		/* Try all interfaces */
75 71
 		rc = -ENODEV;
76 72
 		for_each_netdev ( netdev ) {
77
-			if ( ( ( rc = payload ( netdev ) ) == 0 ) &&
78
-			     stop_on_first_success ) {
73
+			if ( ( ( rc = ifcmd->payload ( netdev, opts ) ) == 0 )
74
+			     && ifcmd->stop_on_first_success ) {
79 75
 				return 0;
80 76
 			}
81 77
 		}
@@ -84,21 +80,30 @@ int ifcommon_exec ( int argc, char **argv,
84 80
 	return rc;
85 81
 }
86 82
 
87
-/** "ifopen" command descriptor */
88
-static struct command_descriptor ifopen_cmd =
89
-	COMMAND_DESC ( struct ifcommon_options, ifcommon_opts, 0, MAX_ARGUMENTS,
90
-		       "[<interface>...]" );
83
+/** "ifopen" options */
84
+struct ifopen_options {};
85
+
86
+/** "ifopen" option list */
87
+static struct option_descriptor ifopen_opts[] = {};
91 88
 
92 89
 /**
93 90
  * "ifopen" payload
94 91
  *
95 92
  * @v netdev		Network device
93
+ * @v opts		Command options
96 94
  * @ret rc		Return status code
97 95
  */
98
-static int ifopen_payload ( struct net_device *netdev ) {
96
+static int ifopen_payload ( struct net_device *netdev,
97
+			    struct ifopen_options *opts __unused ) {
99 98
 	return ifopen ( netdev );
100 99
 }
101 100
 
101
+/** "ifopen" command descriptor */
102
+static struct ifcommon_command_descriptor ifopen_cmd =
103
+	IFCOMMON_COMMAND_DESC ( struct ifopen_options, ifopen_opts,
104
+				0, MAX_ARGUMENTS, "[<interface>...]",
105
+				ifopen_payload, 0 );
106
+
102 107
 /**
103 108
  * The "ifopen" command
104 109
  *
@@ -107,25 +112,34 @@ static int ifopen_payload ( struct net_device *netdev ) {
107 112
  * @ret rc		Return status code
108 113
  */
109 114
 static int ifopen_exec ( int argc, char **argv ) {
110
-	return ifcommon_exec ( argc, argv, &ifopen_cmd, ifopen_payload, 0 );
115
+	return ifcommon_exec ( argc, argv, &ifopen_cmd );
111 116
 }
112 117
 
113
-/** "ifclose" command descriptor */
114
-static struct command_descriptor ifclose_cmd =
115
-	COMMAND_DESC ( struct ifcommon_options, ifcommon_opts, 0, MAX_ARGUMENTS,
116
-		       "[<interface>...]" );
118
+/** "ifclose" options */
119
+struct ifclose_options {};
120
+
121
+/** "ifclose" option list */
122
+static struct option_descriptor ifclose_opts[] = {};
117 123
 
118 124
 /**
119 125
  * "ifclose" payload
120 126
  *
121 127
  * @v netdev		Network device
128
+ * @v opts		Command options
122 129
  * @ret rc		Return status code
123 130
  */
124
-static int ifclose_payload ( struct net_device *netdev ) {
131
+static int ifclose_payload ( struct net_device *netdev,
132
+			     struct ifclose_options *opts __unused ) {
125 133
 	ifclose ( netdev );
126 134
 	return 0;
127 135
 }
128 136
 
137
+/** "ifclose" command descriptor */
138
+static struct ifcommon_command_descriptor ifclose_cmd =
139
+	IFCOMMON_COMMAND_DESC ( struct ifclose_options, ifclose_opts,
140
+				0, MAX_ARGUMENTS, "[<interface>...]",
141
+				ifclose_payload, 0 );
142
+
129 143
 /**
130 144
  * The "ifclose" command
131 145
  *
@@ -134,25 +148,34 @@ static int ifclose_payload ( struct net_device *netdev ) {
134 148
  * @ret rc		Return status code
135 149
  */
136 150
 static int ifclose_exec ( int argc, char **argv ) {
137
-	return ifcommon_exec ( argc, argv, &ifclose_cmd, ifclose_payload, 0 );
151
+	return ifcommon_exec ( argc, argv, &ifclose_cmd );
138 152
 }
139 153
 
140
-/** "ifstat" command descriptor */
141
-static struct command_descriptor ifstat_cmd =
142
-	COMMAND_DESC ( struct ifcommon_options, ifcommon_opts, 0, MAX_ARGUMENTS,
143
-		       "[<interface>...]" );
154
+/** "ifstat" options */
155
+struct ifstat_options {};
156
+
157
+/** "ifstat" option list */
158
+static struct option_descriptor ifstat_opts[] = {};
144 159
 
145 160
 /**
146 161
  * "ifstat" payload
147 162
  *
148 163
  * @v netdev		Network device
164
+ * @v opts		Command options
149 165
  * @ret rc		Return status code
150 166
  */
151
-static int ifstat_payload ( struct net_device *netdev ) {
167
+static int ifstat_payload ( struct net_device *netdev,
168
+			    struct ifstat_options *opts __unused ) {
152 169
 	ifstat ( netdev );
153 170
 	return 0;
154 171
 }
155 172
 
173
+/** "ifstat" command descriptor */
174
+static struct ifcommon_command_descriptor ifstat_cmd =
175
+	IFCOMMON_COMMAND_DESC ( struct ifstat_options, ifstat_opts,
176
+				0, MAX_ARGUMENTS, "[<interface>...]",
177
+				ifstat_payload, 0 );
178
+
156 179
 /**
157 180
  * The "ifstat" command
158 181
  *
@@ -161,7 +184,7 @@ static int ifstat_payload ( struct net_device *netdev ) {
161 184
  * @ret rc		Return status code
162 185
  */
163 186
 static int ifstat_exec ( int argc, char **argv ) {
164
-	return ifcommon_exec ( argc, argv, &ifstat_cmd, ifstat_payload, 0 );
187
+	return ifcommon_exec ( argc, argv, &ifstat_cmd );
165 188
 }
166 189
 
167 190
 /** Interface management commands */

+ 30
- 12
src/hci/commands/iwmgmt_cmd.c Dosyayı Görüntüle

@@ -32,18 +32,21 @@ FILE_LICENCE ( GPL2_OR_LATER );
32 32
  *
33 33
  */
34 34
 
35
-/** "iwstat" command descriptor */
36
-static struct command_descriptor iwstat_cmd =
37
-	COMMAND_DESC ( struct ifcommon_options, ifcommon_opts, 0, MAX_ARGUMENTS,
38
-		       "[<interface>...]" );
35
+/** "iwstat" options */
36
+struct iwstat_options {};
37
+
38
+/** "iwstat" option list */
39
+static struct option_descriptor iwstat_opts[] = {};
39 40
 
40 41
 /**
41 42
  * "iwstat" payload
42 43
  *
43 44
  * @v netdev		Network device
45
+ * @v opts		Command options
44 46
  * @ret rc		Return status code
45 47
  */
46
-static int iwstat_payload ( struct net_device *netdev ) {
48
+static int iwstat_payload ( struct net_device *netdev,
49
+			    struct iwstat_options *opts __unused ) {
47 50
 	struct net80211_device *dev = net80211_get ( netdev );
48 51
 
49 52
 	if ( dev )
@@ -52,6 +55,12 @@ static int iwstat_payload ( struct net_device *netdev ) {
52 55
 	return 0;
53 56
 }
54 57
 
58
+/** "iwstat" command descriptor */
59
+static struct ifcommon_command_descriptor iwstat_cmd =
60
+	IFCOMMON_COMMAND_DESC ( struct iwstat_options, iwstat_opts,
61
+				0, MAX_ARGUMENTS, "[<interface>...]",
62
+				iwstat_payload, 0 );
63
+
55 64
 /**
56 65
  * The "iwstat" command
57 66
  *
@@ -60,21 +69,24 @@ static int iwstat_payload ( struct net_device *netdev ) {
60 69
  * @ret rc		Return status code
61 70
  */
62 71
 static int iwstat_exec ( int argc, char **argv ) {
63
-	return ifcommon_exec ( argc, argv, &iwstat_cmd, iwstat_payload, 0 );
72
+	return ifcommon_exec ( argc, argv, &iwstat_cmd );
64 73
 }
65 74
 
66
-/** "iwlist" command descriptor */
67
-static struct command_descriptor iwlist_cmd =
68
-	COMMAND_DESC ( struct ifcommon_options, ifcommon_opts, 0, MAX_ARGUMENTS,
69
-		       "[<interface>...]" );
75
+/** "iwlist" options */
76
+struct iwlist_options {};
77
+
78
+/** "iwlist" option list */
79
+static struct option_descriptor iwlist_opts[] = {};
70 80
 
71 81
 /**
72 82
  * "iwlist" payload
73 83
  *
74 84
  * @v netdev		Network device
85
+ * @v opts		Command options
75 86
  * @ret rc		Return status code
76 87
  */
77
-static int iwlist_payload ( struct net_device *netdev ) {
88
+static int iwlist_payload ( struct net_device *netdev,
89
+			    struct iwlist_options *opts __unused ) {
78 90
 	struct net80211_device *dev = net80211_get ( netdev );
79 91
 
80 92
 	if ( dev )
@@ -83,6 +95,12 @@ static int iwlist_payload ( struct net_device *netdev ) {
83 95
 	return 0;
84 96
 }
85 97
 
98
+/** "iwlist" command descriptor */
99
+static struct ifcommon_command_descriptor iwlist_cmd =
100
+	IFCOMMON_COMMAND_DESC ( struct iwlist_options, iwlist_opts,
101
+				0, MAX_ARGUMENTS, "[<interface>...]",
102
+				iwlist_payload, 0 );
103
+
86 104
 /**
87 105
  * The "iwlist" command
88 106
  *
@@ -91,7 +109,7 @@ static int iwlist_payload ( struct net_device *netdev ) {
91 109
  * @ret rc		Return status code
92 110
  */
93 111
 static int iwlist_exec ( int argc, char **argv ) {
94
-	return ifcommon_exec ( argc, argv, &iwlist_cmd, iwlist_payload, 0 );
112
+	return ifcommon_exec ( argc, argv, &iwlist_cmd );
95 113
 }
96 114
 
97 115
 /** Wireless interface management commands */

+ 38
- 5
src/include/hci/ifmgmt_cmd.h Dosyayı Görüntüle

@@ -26,13 +26,46 @@ FILE_LICENCE ( GPL2_OR_LATER );
26 26
 
27 27
 struct net_device;
28 28
 
29
-struct ifcommon_options {};
29
+/** An "if<xxx>" command descriptor */
30
+struct ifcommon_command_descriptor {
31
+	/** Command descriptor */
32
+	struct command_descriptor cmd;
33
+	/** Payload
34
+	 *
35
+	 * @v netdev		Network device
36
+	 * @v opts		Command options
37
+	 * @ret rc		Return status code
38
+	 */
39
+	int ( * payload ) ( struct net_device *netdev, void *opts );
40
+	/** Stop on first success */
41
+	int stop_on_first_success;
42
+};
30 43
 
31
-extern struct option_descriptor ifcommon_opts[0];
44
+/**
45
+ * Construct "if<xxx>" command descriptor
46
+ *
47
+ * @v _struct		Options structure type
48
+ * @v _options		Option descriptor array
49
+ * @v _check_args	Remaining argument checker
50
+ * @v _usage		Command usage
51
+ * @ret _command	Command descriptor
52
+ */
53
+#define IFCOMMON_COMMAND_DESC( _struct, _options, _min_args,		\
54
+			       _max_args, _usage, _payload,		\
55
+			       _stop_on_first_success )			\
56
+	{								\
57
+		.cmd = COMMAND_DESC ( _struct, _options, _min_args,	\
58
+				      _max_args, _usage ),		\
59
+		.payload = ( ( int ( * ) ( struct net_device *netdev,	\
60
+					   void *opts ) )		\
61
+			     ( ( ( ( int ( * ) ( struct net_device *,	\
62
+						 _struct * ) ) NULL )	\
63
+				 == ( typeof ( _payload ) * ) NULL )	\
64
+			       ? _payload : _payload ) ),		\
65
+		.stop_on_first_success = _stop_on_first_success,	\
66
+	}
32 67
 
33 68
 extern int ifcommon_exec (  int argc, char **argv,
34
-			    struct command_descriptor *cmd,
35
-			    int ( * payload ) ( struct net_device * ),
36
-			    int stop_on_first_success );
69
+			    struct ifcommon_command_descriptor *cmd );
37 70
 
38 71
 #endif /* _IFMGMT_CMD_H */

Loading…
İptal
Kaydet