OLD | NEW |
---|---|
1 /* Copyright (c) 2011 The Chromium OS Authors. All rights reserved. | 1 /* Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
2 * Use of this source code is governed by a BSD-style license that can be | 2 * Use of this source code is governed by a BSD-style license that can be |
3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
4 * | 4 * |
5 * Chrome OS firmware/system interface utility | 5 * Chrome OS firmware/system interface utility |
6 */ | 6 */ |
7 | 7 |
8 #include <stdio.h> | 8 #include <stdio.h> |
9 #include <stdlib.h> | 9 #include <stdlib.h> |
10 #include <string.h> | 10 #include <string.h> |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
92 for (p = sys_param_list; p->name; p++) | 92 for (p = sys_param_list; p->name; p++) |
93 printf(" %-22s %s\n", p->name, p->desc); | 93 printf(" %-22s %s\n", p->name, p->desc); |
94 } | 94 } |
95 | 95 |
96 | 96 |
97 /* Find the parameter in the list. | 97 /* Find the parameter in the list. |
98 * | 98 * |
99 * Returns the parameter, or NULL if no match. */ | 99 * Returns the parameter, or NULL if no match. */ |
100 const Param* FindParam(const char* name) { | 100 const Param* FindParam(const char* name) { |
101 const Param* p; | 101 const Param* p; |
102 if (!name) | |
103 return NULL; | |
102 for (p = sys_param_list; p->name; p++) { | 104 for (p = sys_param_list; p->name; p++) { |
103 if (!strcasecmp(p->name, name)) | 105 if (!strcasecmp(p->name, name)) |
104 return p; | 106 return p; |
105 } | 107 } |
106 return NULL; | 108 return NULL; |
107 } | 109 } |
108 | 110 |
109 | 111 |
110 /* Set the specified parameter. | 112 /* Set the specified parameter. |
111 * | 113 * |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
213 return PrintAllParams(); | 215 return PrintAllParams(); |
214 | 216 |
215 /* Print help if needed */ | 217 /* Print help if needed */ |
216 if (!strcasecmp(argv[1], "-h") || !strcmp(argv[1], "-?")) { | 218 if (!strcasecmp(argv[1], "-h") || !strcmp(argv[1], "-?")) { |
217 PrintHelp(progname); | 219 PrintHelp(progname); |
218 return 0; | 220 return 0; |
219 } | 221 } |
220 | 222 |
221 /* Otherwise, loop through params and get/set them */ | 223 /* Otherwise, loop through params and get/set them */ |
222 for (i = 1; i < argc && retval == 0; i++) { | 224 for (i = 1; i < argc && retval == 0; i++) { |
223 int has_set = (NULL != strchr(argv[i], '=')); | 225 char* has_set = strchr(argv[i], '='); |
224 int has_expect = (NULL != strchr(argv[i], '?')); | 226 char* has_expect = strchr(argv[i], '?'); |
225 char* name = strtok(argv[i], "=?"); | 227 char* name = strtok(argv[i], "=?"); |
226 char* value = strtok(NULL, "=?"); | 228 char* value = strtok(NULL, "=?"); |
227 const Param* p = FindParam(name); | 229 const Param* p; |
228 if (!p) { | 230 |
229 fprintf(stderr, "Invalid parameter name: %s\n", name); | 231 /* Make sure args are well-formed. '' or '=foo' or '?foo' not allowed. */ |
232 if (!name || has_set == argv[i] || has_expect == argv[i]) { | |
233 fprintf(stderr, "Poorly formed parameter\n"); | |
230 PrintHelp(progname); | 234 PrintHelp(progname); |
231 return 1; | 235 return 1; |
232 } | 236 } |
237 if (!value) | |
238 value=""; /* Allow setting/checking an empty string ('foo=' or 'foo?') */ | |
233 if (has_set && has_expect) { | 239 if (has_set && has_expect) { |
234 fprintf(stderr, "Use either = or ? in a parameter, but not both.\n"); | 240 fprintf(stderr, "Use either = or ? in a parameter, but not both.\n"); |
235 PrintHelp(progname); | 241 PrintHelp(progname); |
petkov
2011/03/22 00:13:36
so this is not a warning but an error?
| |
236 return 1; | 242 return 1; |
237 } | 243 } |
238 | 244 |
245 /* Find the parameter */ | |
246 p = FindParam(name); | |
247 if (!p) { | |
248 fprintf(stderr, "Invalid parameter name: %s\n", name); | |
249 PrintHelp(progname); | |
250 return 1; | |
251 } | |
252 | |
239 if (i > 1) | 253 if (i > 1) |
240 printf(" "); /* Output params space-delimited */ | 254 printf(" "); /* Output params space-delimited */ |
241 if (has_set) | 255 if (has_set) |
242 retval = SetParam(p, value); | 256 retval = SetParam(p, value); |
243 else if (has_expect) | 257 else if (has_expect) |
244 retval = CheckParam(p, value); | 258 retval = CheckParam(p, value); |
245 else | 259 else |
246 retval = PrintParam(p); | 260 retval = PrintParam(p); |
247 } | 261 } |
248 | 262 |
249 return retval; | 263 return retval; |
250 } | 264 } |
OLD | NEW |