Selaa lähdekoodia

[vlan] Use generic option-parsing library

Total saving: 261 bytes.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 14 vuotta sitten
vanhempi
commit
2877af3ff5
1 muutettua tiedostoa jossa 66 lisäystä ja 99 poistoa
  1. 66
    99
      src/hci/commands/vlan_cmd.c

+ 66
- 99
src/hci/commands/vlan_cmd.c Näytä tiedosto

@@ -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/vlan.h>
28 29
 
29 30
 /** @file
@@ -32,136 +33,102 @@ FILE_LICENCE ( GPL2_OR_LATER );
32 33
  *
33 34
  */
34 35
 
35
-static void vcreate_syntax ( char **argv ) {
36
-	printf ( "Usage:\n  %s --tag <tag> [--priority <priority] "
37
-		 "<trunk interface>\n", argv[0] );
38
-}
36
+/** "vcreate" options */
37
+struct vcreate_options {
38
+	/** VLAN tag */
39
+	unsigned int tag;
40
+	/** VLAN default priority */
41
+	unsigned int priority;
42
+};
43
+
44
+/** "vcreate" option list */
45
+static struct option_descriptor vcreate_opts[] = {
46
+	OPTION_DESC ( "tag", 't', required_argument,
47
+		      struct vcreate_options, tag, parse_integer ),
48
+	OPTION_DESC ( "priority", 'p', required_argument,
49
+		      struct vcreate_options, priority, parse_integer ),
50
+};
51
+
52
+/** "vcreate" command descriptor */
53
+static struct command_descriptor vcreate_cmd =
54
+	COMMAND_DESC ( struct vcreate_options, vcreate_opts, 1, 1,
55
+		       "--tag <tag> [--priority <priority>] "
56
+		       "<trunk interface>",
57
+		       "Create a VLAN interface" );
39 58
 
59
+/**
60
+ * "vcreate" command
61
+ *
62
+ * @v argc		Argument count
63
+ * @v argv		Argument list
64
+ * @ret rc		Return status code
65
+ */
40 66
 static int vcreate_exec ( int argc, char **argv ) {
41
-	static struct option vcreate_opts[] = {
42
-		{ "help", 0, NULL, 'h' },
43
-		{ "tag", required_argument, NULL, 't' },
44
-		{ "priority", required_argument, NULL, 'p' },
45
-		{ NULL, 0, NULL, 0 },
46
-	};
47
-	const char *trunk_name;
48
-	const char *tag_text = NULL;
49
-	const char *priority_text = NULL;
67
+	struct vcreate_options opts;
50 68
 	struct net_device *trunk;
51
-	unsigned int tag;
52
-	unsigned int priority;
53
-	char *endp;
54
-	int c;
55 69
 	int rc;
56 70
 
57
-	/* Parse command line */
58
-	while ( ( c = getopt_long ( argc, argv, "ht:p:", vcreate_opts,
59
-				    NULL ) ) >= 0 ) {
60
-		switch ( c ) {
61
-		case 't':
62
-			tag_text = optarg;
63
-			break;
64
-		case 'p':
65
-			priority_text = optarg;
66
-			break;
67
-		case 'h':
68
-			/* Display help text */
69
-		default:
70
-			/* Unrecognised/invalid option */
71
-			vcreate_syntax ( argv );
72
-			return 1;
73
-		}
74
-	}
75
-	if ( optind != ( argc - 1 ) ) {
76
-		vcreate_syntax ( argv );
77
-		return 1;
78
-	}
79
-	trunk_name = argv[optind];
80
-	if ( ! tag_text ) {
81
-		vcreate_syntax ( argv );
82
-		return 1;
83
-	}
71
+	/* Parse options */
72
+	if ( ( rc = parse_options ( argc, argv, &vcreate_cmd, &opts ) ) != 0 )
73
+		return rc;
84 74
 
85
-	/* Identify network device */
86
-	trunk = find_netdev ( trunk_name );
87
-	if ( ! trunk ) {
88
-		printf ( "%s: no such interface\n", trunk_name );
89
-		return 1;
90
-	}
91
-	tag = strtoul ( tag_text, &endp, 10 );
92
-	if ( *endp ) {
93
-		printf ( "%s: invalid tag\n", tag_text );
94
-		return 1;
95
-	}
96
-	if ( priority_text ) {
97
-		priority = strtoul ( priority_text, &endp, 10 );
98
-		if ( *endp ) {
99
-			printf ( "%s: invalid priority\n", priority_text );
100
-			return 1;
101
-		}
102
-	} else {
103
-		priority = 0;
104
-	}
75
+	/* Parse trunk interface */
76
+	if ( ( rc = parse_netdev ( argv[optind], &trunk ) ) != 0 )
77
+		return rc;
105 78
 
106 79
 	/* Create VLAN device */
107
-	if ( ( rc = vlan_create ( trunk, tag, priority ) ) != 0 ) {
80
+	if ( ( rc = vlan_create ( trunk, opts.tag, opts.priority ) ) != 0 ) {
108 81
 		printf ( "Could not create VLAN device: %s\n",
109 82
 			 strerror ( rc ) );
110
-		return 1;
83
+		return rc;
111 84
 	}
112 85
 
113 86
 	return 0;
114 87
 }
115 88
 
116
-static void vdestroy_syntax ( char **argv ) {
117
-	printf ( "Usage:\n  %s <interface>\n", argv[0] );
118
-}
89
+/** "vdestroy" options */
90
+struct vdestroy_options {};
91
+
92
+/** "vdestroy" option list */
93
+static struct option_descriptor vdestroy_opts[] = {};
119 94
 
95
+/** "vdestroy" command descriptor */
96
+static struct command_descriptor vdestroy_cmd =
97
+	COMMAND_DESC ( struct vdestroy_options, vdestroy_opts, 1, 1,
98
+		       "<VLAN interface>",
99
+		       "Destroy a VLAN interface" );
100
+
101
+/**
102
+ * "vdestroy" command
103
+ *
104
+ * @v argc		Argument count
105
+ * @v argv		Argument list
106
+ * @ret rc		Return status code
107
+ */
120 108
 static int vdestroy_exec ( int argc, char **argv ) {
121
-	static struct option vdestroy_opts[] = {
122
-		{ "help", 0, NULL, 'h' },
123
-		{ NULL, 0, NULL, 0 },
124
-	};
125
-	const char *netdev_name;
109
+	struct vdestroy_options opts;
126 110
 	struct net_device *netdev;
127
-	int c;
128 111
 	int rc;
129 112
 
130
-	/* Parse command line */
131
-	while ( ( c = getopt_long ( argc, argv, "h", vdestroy_opts,
132
-				    NULL ) ) >= 0 ) {
133
-		switch ( c ) {
134
-		case 'h':
135
-			/* Display help text */
136
-		default:
137
-			/* Unrecognised/invalid option */
138
-			vdestroy_syntax ( argv );
139
-			return 1;
140
-		}
141
-	}
142
-	if ( optind != ( argc - 1 ) ) {
143
-		vdestroy_syntax ( argv );
144
-		return 1;
145
-	}
146
-	netdev_name = argv[optind];
113
+	/* Parse options */
114
+	if ( ( rc = parse_options ( argc, argv, &vdestroy_cmd, &opts ) ) != 0 )
115
+		return rc;
147 116
 
148
-	/* Identify network device */
149
-	netdev = find_netdev ( netdev_name );
150
-	if ( ! netdev ) {
151
-		printf ( "%s: no such interface\n", netdev_name );
152
-		return 1;
153
-	}
117
+	/* Parse trunk interface */
118
+	if ( ( rc = parse_netdev ( argv[optind], &netdev ) ) != 0 )
119
+		return rc;
154 120
 
155 121
 	/* Destroy VLAN device */
156 122
 	if ( ( rc = vlan_destroy ( netdev ) ) != 0 ) {
157 123
 		printf ( "Could not destroy VLAN device: %s\n",
158 124
 			 strerror ( rc ) );
159
-		return 1;
125
+		return rc;
160 126
 	}
161 127
 
162 128
 	return 0;
163 129
 }
164 130
 
131
+/** VLAN commands */
165 132
 struct command vlan_commands[] __command = {
166 133
 	{
167 134
 		.name = "vcreate",

Loading…
Peruuta
Tallenna