Browse Source

[dhcp] Use generic option-parsing library

Total saving: 329 bytes.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 14 years ago
parent
commit
07c6b79102
1 changed files with 35 additions and 120 deletions
  1. 35
    120
      src/hci/commands/dhcp_cmd.c

+ 35
- 120
src/hci/commands/dhcp_cmd.c View File

@@ -30,7 +30,9 @@ FILE_LICENCE ( GPL2_OR_LATER );
30 30
 #include <ipxe/netdevice.h>
31 31
 #include <ipxe/in.h>
32 32
 #include <ipxe/command.h>
33
+#include <ipxe/parseopt.h>
33 34
 #include <usr/dhcpmgmt.h>
35
+#include <hci/ifmgmt_cmd.h>
34 36
 
35 37
 /** @file
36 38
  *
@@ -38,26 +40,19 @@ FILE_LICENCE ( GPL2_OR_LATER );
38 40
  *
39 41
  */
40 42
 
41
-/**
42
- * "dhcp" command syntax message
43
- *
44
- * @v argv		Argument list
45
- */
46
-static void dhcp_syntax ( char **argv ) {
47
-	printf ( "Usage:\n"
48
-		 "  %s [<interface>] [<interface>...]\n"
49
-		 "\n"
50
-		 "Configure a network interface using DHCP\n",
51
-		 argv[0] );
52
-}
43
+/** "dhcp" command descriptor */
44
+static struct command_descriptor dhcp_cmd =
45
+	COMMAND_DESC ( struct ifcommon_options, ifcommon_opts, 0, MAX_ARGUMENTS,
46
+		       "[<interface>] [<interface>...]",
47
+		       "Configure network interface(s) using DHCP" );
53 48
 
54 49
 /**
55 50
  * Execute "dhcp" command for a network device
56 51
  *
57 52
  * @v netdev		Network device
58
- * @ret rc		Exit code
53
+ * @ret rc		Return status code
59 54
  */
60
-static int dhcp_exec_netdev ( struct net_device *netdev ) {
55
+static int dhcp_payload ( struct net_device *netdev ) {
61 56
 	int rc;
62 57
 
63 58
 	if ( ( rc = dhcp ( netdev ) ) != 0 ) {
@@ -67,145 +62,65 @@ static int dhcp_exec_netdev ( struct net_device *netdev ) {
67 62
 		/* Close device on failure, to avoid memory exhaustion */
68 63
 		netdev_close ( netdev );
69 64
 
70
-		return 1;
65
+		return rc;
71 66
 	}
72 67
 
73 68
 	return 0;
74 69
 }
75 70
 
76
-/**
77
- * Execute "dhcp" command for a named network device
78
- *
79
- * @v netdev_name	Network device name
80
- * @ret rc		Exit code
81
- */
82
-static int dhcp_exec_name ( const char *netdev_name ) {
83
-	struct net_device *netdev;
84
-
85
-	netdev = find_netdev ( netdev_name );
86
-	if ( ! netdev ) {
87
-		printf ( "No such interface \"%s\"\n", netdev_name );
88
-		return 1;
89
-	}
90
-
91
-	return dhcp_exec_netdev ( netdev );
92
-}
93
-
94 71
 /**
95 72
  * The "dhcp" command
96 73
  *
97 74
  * @v argc		Argument count
98 75
  * @v argv		Argument list
99
- * @ret rc		Exit code
76
+ * @ret rc		Return status code
100 77
  */
101 78
 static int dhcp_exec ( int argc, char **argv ) {
102
-	static struct option longopts[] = {
103
-		{ "help", 0, NULL, 'h' },
104
-		{ NULL, 0, NULL, 0 },
105
-	};
106
-	const char *netdev_name;
107
-	struct net_device *netdev;
108
-	int c;
109
-	int rc;
110
-
111
-	/* Parse options */
112
-	while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
113
-		switch ( c ) {
114
-		case 'h':
115
-			/* Display help text */
116
-		default:
117
-			/* Unrecognised/invalid option */
118
-			dhcp_syntax ( argv );
119
-			return 1;
120
-		}
121
-	}
79
+	return ifcommon_exec ( argc, argv, &dhcp_cmd, dhcp_payload, 1 );
80
+}
122 81
 
123
-	if ( optind != argc ) {
124
-		/* Treat arguments as a list of interfaces to try */
125
-		while ( optind != argc ) {
126
-			netdev_name = argv[optind++];
127
-			if ( ( rc = dhcp_exec_name ( netdev_name ) ) == 0 )
128
-				return 0;
129
-		}
130
-	} else {
131
-		/* Try all interfaces */
132
-		for_each_netdev ( netdev ) {
133
-			if ( ( rc = dhcp_exec_netdev ( netdev ) ) == 0 )
134
-				return 0;
135
-		}
136
-	}
82
+/** "pxebs" options */
83
+struct pxebs_options {};
137 84
 
138
-	return 1;
139
-}
85
+/** "pxebs" option list */
86
+static struct option_descriptor pxebs_opts[] = {};
140 87
 
141
-/**
142
- * "pxebs" command syntax message
143
- *
144
- * @v argv		Argument list
145
- */
146
-static void pxebs_syntax ( char **argv ) {
147
-	printf ( "Usage:\n"
148
-		 "  %s <interface> <server_type>\n"
149
-		 "\n"
150
-		 "Perform PXE Boot Server discovery\n",
151
-		 argv[0] );
152
-}
88
+/** "pxebs" command descriptor */
89
+static struct command_descriptor pxebs_cmd =
90
+	COMMAND_DESC ( struct pxebs_options, pxebs_opts, 2, 2,
91
+		       "<interface> <server_type>",
92
+		       "Perform PXE Boot Server discovery" );
153 93
 
154 94
 /**
155 95
  * The "pxebs" command
156 96
  *
157 97
  * @v argc		Argument count
158 98
  * @v argv		Argument list
159
- * @ret rc		Exit code
99
+ * @ret rc		Return status code
160 100
  */
161 101
 static int pxebs_exec ( int argc, char **argv ) {
162
-	static struct option longopts[] = {
163
-		{ "help", 0, NULL, 'h' },
164
-		{ NULL, 0, NULL, 0 },
165
-	};
166
-	const char *netdev_txt;
167
-	const char *pxe_type_txt;
102
+	struct pxebs_options opts;
168 103
 	struct net_device *netdev;
169 104
 	unsigned int pxe_type;
170
-	char *end;
171
-	int c;
172 105
 	int rc;
173 106
 
174 107
 	/* Parse options */
175
-	while ( ( c = getopt_long ( argc, argv, "h", longopts, NULL ) ) >= 0 ){
176
-		switch ( c ) {
177
-		case 'h':
178
-			/* Display help text */
179
-		default:
180
-			/* Unrecognised/invalid option */
181
-			pxebs_syntax ( argv );
182
-			return 1;
183
-		}
184
-	}
185
-	if ( optind != ( argc - 2 ) ) {
186
-		pxebs_syntax ( argv );
187
-		return 1;
188
-	}
189
-	netdev_txt = argv[optind];
190
-	pxe_type_txt = argv[ optind + 1 ];
191
-
192
-	/* Parse arguments */
193
-	netdev = find_netdev ( netdev_txt );
194
-	if ( ! netdev ) {
195
-		printf ( "No such interface: %s\n", netdev_txt );
196
-		return 1;
197
-	}
198
-	pxe_type = strtoul ( pxe_type_txt, &end, 0 );
199
-	if ( *end ) {
200
-		printf ( "Bad server type: %s\n", pxe_type_txt );
201
-		return 1;
202
-	}
108
+	if ( ( rc = parse_options ( argc, argv, &pxebs_cmd, &opts ) ) != 0 )
109
+		return rc;
110
+
111
+	/* Parse net device name */
112
+	if ( ( rc = parse_netdev ( argv[optind], &netdev ) ) != 0 )
113
+		return rc;
114
+
115
+	/* Parse boot server type */
116
+	if ( ( rc = parse_integer ( argv[ optind + 1 ], &pxe_type ) ) != 0 )
117
+		return rc;
203 118
 
204 119
 	/* Perform Boot Server Discovery */
205 120
 	if ( ( rc = pxebs ( netdev, pxe_type ) ) != 0 ) {
206 121
 		printf ( "Could not discover boot server on %s: %s\n",
207 122
 			 netdev->name, strerror ( rc ) );
208
-		return 1;
123
+		return rc;
209 124
 	}
210 125
 
211 126
 	return 0;

Loading…
Cancel
Save