Browse Source

[lotest] Use generic option-parsing library

Total saving: 145 bytes.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 14 years ago
parent
commit
ec42ba366f
1 changed files with 42 additions and 59 deletions
  1. 42
    59
      src/hci/commands/lotest_cmd.c

+ 42
- 59
src/hci/commands/lotest_cmd.c View File

@@ -24,6 +24,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
24 24
 #include <getopt.h>
25 25
 #include <ipxe/netdevice.h>
26 26
 #include <ipxe/command.h>
27
+#include <ipxe/parseopt.h>
27 28
 #include <ipxe/if_ether.h>
28 29
 #include <usr/lotest.h>
29 30
 
@@ -33,81 +34,63 @@ FILE_LICENCE ( GPL2_OR_LATER );
33 34
  *
34 35
  */
35 36
 
36
-static void lotest_syntax ( char **argv ) {
37
-	printf ( "Usage:\n  %s <sending interface> <receiving interface>\n",
38
-		 argv[0] );
39
-}
37
+/** "lotest" options */
38
+struct lotest_options {
39
+	/** MTU */
40
+	unsigned int mtu;
41
+};
42
+
43
+/** "lotest" option list */
44
+static struct option_descriptor lotest_opts[] = {
45
+	OPTION_DESC ( "mtu", 'm', required_argument,
46
+		      struct lotest_options, mtu, parse_integer ),
47
+};
40 48
 
49
+/** "lotest" command descriptor */
50
+static struct command_descriptor lotest_cmd =
51
+	COMMAND_DESC ( struct lotest_options, lotest_opts, 2, 2,
52
+		       "[--mtu <mtu>] <sending_interface> "
53
+		       "<receiving_interface>", "" );
54
+
55
+/**
56
+ * "lotest" command
57
+ *
58
+ * @v argc		Argument count
59
+ * @v argv		Argument list
60
+ * @ret rc		Return status code
61
+ */
41 62
 static int lotest_exec ( int argc, char **argv ) {
42
-	static struct option lotest_opts[] = {
43
-		{ "help", 0, NULL, 'h' },
44
-		{ "mtu", required_argument, NULL, 'm' },
45
-		{ NULL, 0, NULL, 0 },
46
-	};
47
-	const char *sender_name;
48
-	const char *receiver_name;
49
-	const char *mtu_text = NULL;
63
+	struct lotest_options opts;
50 64
 	struct net_device *sender;
51 65
 	struct net_device *receiver;
52
-	char *endp;
53
-	size_t mtu;
54
-	int c;
55 66
 	int rc;
56 67
 
57
-	/* Parse command line */
58
-	while ( ( c = getopt_long ( argc, argv, "hm:", lotest_opts,
59
-				    NULL ) ) >= 0 ) {
60
-		switch ( c ) {
61
-		case 'm':
62
-			mtu_text = optarg;
63
-			break;
64
-		case 'h':
65
-			/* Display help text */
66
-		default:
67
-			/* Unrecognised/invalid option */
68
-			lotest_syntax ( argv );
69
-			return 1;
70
-		}
71
-	}
72
-	if ( optind != ( argc - 2 ) ) {
73
-		lotest_syntax ( argv );
74
-		return 1;
75
-	}
76
-	sender_name = argv[optind];
77
-	receiver_name = argv[optind + 1];
68
+	/* Parse options */
69
+	if ( ( rc = parse_options ( argc, argv, &lotest_cmd, &opts ) ) != 0 )
70
+		return rc;
78 71
 
79
-	/* Identify network devices */
80
-	sender = find_netdev ( sender_name );
81
-	if ( ! sender ) {
82
-		printf ( "%s: no such interface\n", sender_name );
83
-		return 1;
84
-	}
85
-	receiver = find_netdev ( receiver_name );
86
-	if ( ! receiver ) {
87
-		printf ( "%s: no such interface\n", receiver_name );
88
-		return 1;
89
-	}
72
+	/* Parse sending interface name */
73
+	if ( ( rc = parse_netdev ( argv[optind], &sender ) ) != 0 )
74
+		return rc;
90 75
 
91
-	/* Identify MTU */
92
-	if ( mtu_text ) {
93
-		mtu = strtoul ( mtu_text, &endp, 10 );
94
-		if ( *endp ) {
95
-			printf ( "%s: invalid MTU\n", mtu_text );
96
-			return 1;
97
-		}
98
-	} else {
99
-		mtu = ETH_MAX_MTU;
100
-	}
76
+	/* Parse receiving interface name */
77
+	if ( ( rc = parse_netdev ( argv[ optind + 1 ], &receiver ) ) != 0 )
78
+		return rc;
79
+
80
+	/* Use default MTU if none specified */
81
+	if ( ! opts.mtu )
82
+		opts.mtu = ETH_MAX_MTU;
101 83
 
102 84
 	/* Perform loopback test */
103
-	if ( ( rc = loopback_test ( sender, receiver, mtu ) ) != 0 ) {
85
+	if ( ( rc = loopback_test ( sender, receiver, opts.mtu ) ) != 0 ) {
104 86
 		printf ( "Test failed: %s\n", strerror ( rc ) );
105
-		return 1;
87
+		return rc;
106 88
 	}
107 89
 
108 90
 	return 0;
109 91
 }
110 92
 
93
+/** Loopback testing commands */
111 94
 struct command lotest_command __command = {
112 95
 	.name = "lotest",
113 96
 	.exec = lotest_exec,

Loading…
Cancel
Save