瀏覽代碼

[settings] Use generic option-parsing library

Total cost: 75 bytes.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 14 年之前
父節點
當前提交
9e9c9adf10
共有 3 個檔案被更改,包括 202 行新增40 行删除
  1. 80
    15
      src/hci/commands/config_cmd.c
  2. 121
    25
      src/hci/commands/nvo_cmd.c
  3. 1
    0
      src/include/ipxe/errfile.h

+ 80
- 15
src/hci/commands/config_cmd.c 查看文件

@@ -1,38 +1,103 @@
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 <string.h>
2 20
 #include <stdio.h>
21
+#include <errno.h>
22
+#include <getopt.h>
3 23
 #include <ipxe/command.h>
24
+#include <ipxe/parseopt.h>
4 25
 #include <ipxe/settings.h>
5 26
 #include <ipxe/settings_ui.h>
6 27
 
7 28
 FILE_LICENCE ( GPL2_OR_LATER );
8 29
 
30
+/** @file
31
+ *
32
+ * Configuration UI commands
33
+ *
34
+ */
35
+
36
+/** "config" options */
37
+struct config_options {};
38
+
39
+/** "config" option list */
40
+static struct option_descriptor config_opts[] = {};
41
+
42
+/** "config" command descriptor */
43
+static struct command_descriptor config_cmd =
44
+	COMMAND_DESC ( struct config_options, config_opts, 0, 1,
45
+		       "[<scope>]",
46
+		       "Open the option configuration console" );
47
+
48
+/**
49
+ * Parse settings scope name
50
+ *
51
+ * @v text		Text
52
+ * @ret value		Integer value
53
+ * @ret rc		Return status code
54
+ */
55
+static int parse_settings ( const char *text, struct settings **value ) {
56
+
57
+	/* Sanity check */
58
+	assert ( text != NULL );
59
+
60
+	/* Parse scope name */
61
+	*value = find_settings ( text );
62
+	if ( ! *value ) {
63
+		printf ( "\"%s\": no such scope\n", text );
64
+		return -EINVAL;
65
+	}
66
+
67
+	return 0;
68
+}
69
+
70
+/**
71
+ * "config" command
72
+ *
73
+ * @v argc		Argument count
74
+ * @v argv		Argument list
75
+ * @ret rc		Return status code
76
+ */
9 77
 static int config_exec ( int argc, char **argv ) {
10
-	char *settings_name;
78
+	struct config_options opts;
11 79
 	struct settings *settings;
12 80
 	int rc;
13 81
 
14
-	if ( argc > 2 ) {
15
-		printf ( "Usage: %s [scope]\n"
16
-			 "Opens the option configuration console\n", argv[0] );
17
-		return 1;
18
-	}
82
+	/* Parse options */
83
+	if ( ( rc = parse_options ( argc, argv, &config_cmd, &opts ) ) != 0 )
84
+		return rc;
19 85
 
20
-	settings_name = ( ( argc == 2 ) ? argv[1] : "" );
21
-	settings = find_settings ( settings_name );
22
-	if ( ! settings ) {
23
-		printf ( "No such scope \"%s\"\n", settings_name );
24
-		return 1;
25
-	}
86
+	/* Parse settings option, if present */
87
+	if ( ( rc = parse_settings ( ( ( optind < argc ) ? argv[optind] : "" ),
88
+				     &settings ) ) != 0 )
89
+		return rc;
26 90
 
91
+	/* Run settings UI */
27 92
 	if ( ( rc = settings_ui ( settings ) ) != 0 ) {
28
-		printf ( "Could not save settings: %s\n",
29
-			 strerror ( rc ) );
30
-		return 1;
93
+		printf ( "Could not save settings: %s\n", strerror ( rc ) );
94
+		return rc;
31 95
 	}
32 96
 
33 97
 	return 0;
34 98
 }
35 99
 
100
+/** Configuration UI commands */
36 101
 struct command config_command __command = {
37 102
 	.name = "config",
38 103
 	.exec = config_exec,

+ 121
- 25
src/hci/commands/nvo_cmd.c 查看文件

@@ -1,3 +1,21 @@
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 <stdint.h>
2 20
 #include <stdlib.h>
3 21
 #include <stdio.h>
@@ -6,42 +24,96 @@
6 24
 #include <getopt.h>
7 25
 #include <ipxe/settings.h>
8 26
 #include <ipxe/command.h>
27
+#include <ipxe/parseopt.h>
9 28
 
10 29
 FILE_LICENCE ( GPL2_OR_LATER );
11 30
 
31
+/** @file
32
+ *
33
+ * Non-volatile option commands
34
+ *
35
+ */
36
+
37
+/** "show" options */
38
+struct show_options {};
39
+
40
+/** "show" option list */
41
+static struct option_descriptor show_opts[] = {};
42
+
43
+/** "show" command descriptor */
44
+static struct command_descriptor show_cmd =
45
+	COMMAND_DESC ( struct show_options, show_opts, 1, 1,
46
+		       "<setting>", "" );
47
+
48
+/**
49
+ * "show" command
50
+ *
51
+ * @v argc		Argument count
52
+ * @v argv		Argument list
53
+ * @ret rc		Return status code
54
+ */
12 55
 static int show_exec ( int argc, char **argv ) {
56
+	struct show_options opts;
57
+	const char *name;
13 58
 	char buf[256];
14 59
 	int rc;
15 60
 
16
-	if ( argc != 2 ) {
17
-		printf ( "Syntax: %s <identifier>\n", argv[0] );
18
-		return 1;
19
-	}
61
+	/* Parse options */
62
+	if ( ( rc = parse_options ( argc, argv, &show_cmd, &opts ) ) != 0 )
63
+		return rc;
20 64
 
21
-	if ( ( rc = fetchf_named_setting ( argv[1], buf,
22
-					   sizeof ( buf ) ) ) < 0 ){
65
+	/* Parse setting name */
66
+	name = argv[optind];
67
+
68
+	/* Fetch setting */
69
+	if ( ( rc = fetchf_named_setting ( name, buf,
70
+					   sizeof ( buf ) ) ) < 0 ) {
23 71
 		printf ( "Could not find \"%s\": %s\n",
24
-			 argv[1], strerror ( rc ) );
25
-		return 1;
72
+			 name, strerror ( rc ) );
73
+		return rc;
26 74
 	}
27 75
 
28
-	printf ( "%s = %s\n", argv[1], buf );
76
+	/* Print setting value */
77
+	printf ( "%s = %s\n", name, buf );
78
+
29 79
 	return 0;
30 80
 }
31 81
 
82
+/** "set" options */
83
+struct set_options {};
84
+
85
+/** "set" option list */
86
+static struct option_descriptor set_opts[] = {};
87
+
88
+/** "set" command descriptor */
89
+static struct command_descriptor set_cmd =
90
+	COMMAND_DESC ( struct set_options, set_opts, 2, MAX_ARGUMENTS,
91
+		       "<setting> <value>", "" );
92
+
93
+/**
94
+ * "set" command
95
+ *
96
+ * @v argc		Argument count
97
+ * @v argv		Argument list
98
+ * @ret rc		Return status code
99
+ */
32 100
 static int set_exec ( int argc, char **argv ) {
101
+	struct set_options opts;
102
+	const char *name;
33 103
 	size_t len;
34 104
 	int i;
35 105
 	int rc;
36 106
 
37
-	if ( argc < 3 ) {
38
-		printf ( "Syntax: %s <identifier> <value>\n", argv[0] );
39
-		return 1;
40
-	}
107
+	/* Parse options */
108
+	if ( ( rc = parse_options ( argc, argv, &set_cmd, &opts ) ) != 0 )
109
+		return rc;
110
+
111
+	/* Parse setting name */
112
+	name = argv[optind];
41 113
 
42 114
 	/* Determine total length of command line */
43 115
 	len = 1; /* NUL */
44
-	for ( i = 2 ; i < argc ; i++ )
116
+	for ( i = optind + 1 ; i < argc ; i++ )
45 117
 		len += ( 1 /* possible space */ + strlen ( argv[i] ) );
46 118
 
47 119
 	{
@@ -50,39 +122,63 @@ static int set_exec ( int argc, char **argv ) {
50 122
 
51 123
 		/* Assemble command line */
52 124
 		buf[0] = '\0';
53
-		for ( i = 2 ; i < argc ; i++ ) {
125
+		for ( i = optind + 1 ; i < argc ; i++ ) {
54 126
 			ptr += sprintf ( ptr, "%s%s", ( buf[0] ? " " : "" ),
55 127
 					 argv[i] );
56 128
 		}
57 129
 		assert ( ptr < ( buf + len ) );
58 130
 
59
-		if ( ( rc = storef_named_setting ( argv[1], buf ) ) != 0 ) {
131
+		if ( ( rc = storef_named_setting ( name, buf ) ) != 0 ) {
60 132
 			printf ( "Could not set \"%s\"=\"%s\": %s\n",
61
-				 argv[1], buf, strerror ( rc ) );
62
-			return 1;
133
+				 name, buf, strerror ( rc ) );
134
+			return rc;
63 135
 		}
64 136
 	}
65 137
 
66 138
 	return 0;
67 139
 }
68 140
 
141
+/** "clear" options */
142
+struct clear_options {};
143
+
144
+/** "clear" option list */
145
+static struct option_descriptor clear_opts[] = {};
146
+
147
+/** "clear" command descriptor */
148
+static struct command_descriptor clear_cmd =
149
+	COMMAND_DESC ( struct clear_options, clear_opts, 1, 1,
150
+		       "<setting>", "" );
151
+
152
+/**
153
+ * "clear" command
154
+ *
155
+ * @v argc		Argument count
156
+ * @v argv		Argument list
157
+ * @ret rc		Return status code
158
+ */
69 159
 static int clear_exec ( int argc, char **argv ) {
160
+	struct clear_options opts;
161
+	const char *name;
70 162
 	int rc;
71 163
 
72
-	if ( argc != 2 ) {
73
-		printf ( "Syntax: %s <identifier>\n", argv[0] );
74
-		return 1;
75
-	}
164
+	/* Parse options */
165
+	if ( ( rc = parse_options ( argc, argv, &clear_cmd, &opts ) ) != 0 )
166
+		return rc;
167
+
168
+	/* Parse setting name */
169
+	name = argv[optind];
76 170
 
77
-	if ( ( rc = delete_named_setting ( argv[1] ) ) != 0 ) {
171
+	/* Clear setting */
172
+	if ( ( rc = delete_named_setting ( name ) ) != 0 ) {
78 173
 		printf ( "Could not clear \"%s\": %s\n",
79
-			 argv[1], strerror ( rc ) );
80
-		return 1;
174
+			 name, strerror ( rc ) );
175
+		return rc;
81 176
 	}
82 177
 	
83 178
 	return 0;
84 179
 }
85 180
 
181
+/** Non-volatile option commands */
86 182
 struct command nvo_commands[] __command = {
87 183
 	{
88 184
 		.name = "show",

+ 1
- 0
src/include/ipxe/errfile.h 查看文件

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

Loading…
取消
儲存