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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
63 void PrintHelp(const char *progname) { | 63 void PrintHelp(const char *progname) { |
64 const Param *p; | 64 const Param *p; |
65 | 65 |
66 printf("\nUsage:\n" | 66 printf("\nUsage:\n" |
67 " %s\n" | 67 " %s\n" |
68 " Prints all parameters with descriptions and current values.\n" | 68 " Prints all parameters with descriptions and current values.\n" |
69 " %s [param1 [param2 [...]]]\n" | 69 " %s [param1 [param2 [...]]]\n" |
70 " Prints the current value(s) of the parameter(s).\n" | 70 " Prints the current value(s) of the parameter(s).\n" |
71 " %s [param1=value1] [param2=value2 [...]]]\n" | 71 " %s [param1=value1] [param2=value2 [...]]]\n" |
72 " Sets the parameter(s) to the specified value(s).\n" | 72 " Sets the parameter(s) to the specified value(s).\n" |
73 " %s [param1?value1] [param2?value2 [...]]]\n" | |
74 " Checks if the parameter(s) all contain the specified value(s).\n" | |
75 "Stops at the first error." | |
73 "\n" | 76 "\n" |
74 "Valid parameters:\n", progname, progname, progname); | 77 "Valid parameters:\n", progname, progname, progname, progname); |
75 for (p = sys_param_list; p->name; p++) | 78 for (p = sys_param_list; p->name; p++) |
76 printf(" %-22s %s\n", p->name, p->desc); | 79 printf(" %-22s %s\n", p->name, p->desc); |
77 } | 80 } |
78 | 81 |
79 | 82 |
80 /* Find the parameter in the list. | 83 /* Find the parameter in the list. |
81 * | 84 * |
82 * Returns the parameter, or NULL if no match. */ | 85 * Returns the parameter, or NULL if no match. */ |
83 const Param* FindParam(const char* name) { | 86 const Param* FindParam(const char* name) { |
84 const Param* p; | 87 const Param* p; |
(...skipping 17 matching lines...) Expand all Loading... | |
102 } else { | 105 } else { |
103 char* e; | 106 char* e; |
104 int i = strtol(value, &e, 0); | 107 int i = strtol(value, &e, 0); |
105 if (!*value || (e && *e)) | 108 if (!*value || (e && *e)) |
106 return 1; | 109 return 1; |
107 return (0 == VbSetSystemPropertyInt(p->name, i) ? 0 : 1); | 110 return (0 == VbSetSystemPropertyInt(p->name, i) ? 0 : 1); |
108 } | 111 } |
109 } | 112 } |
110 | 113 |
111 | 114 |
115 /* Compares the parameter with the expected value. | |
116 * | |
117 * Returns 0 if success (match), non-zero if error (mismatch). */ | |
118 int CheckParam(const Param* p, char* expect) { | |
119 if (p->is_string) { | |
120 char buf[256]; | |
121 const char* v = VbGetSystemPropertyString(p->name, buf, sizeof(buf)); | |
122 if (!v || 0 != strcmp(v, expect)) | |
123 return 1; | |
124 } else { | |
125 char* e; | |
126 int i = strtol(expect, &e, 0); | |
Will Drewry
2011/03/11 02:47:17
nit: long != int, always.
this will likely throw
| |
127 int v = VbGetSystemPropertyInt(p->name); | |
128 if (!*expect || (e && *e)) | |
129 return 1; | |
130 if (v == -1 || i != v) | |
gauravsh
2011/03/11 02:39:35
since you use yoda comparisons everywhere else, mi
| |
131 return 1; | |
132 } | |
133 return 0; | |
134 } | |
135 | |
136 | |
112 /* Print the specified parameter. | 137 /* Print the specified parameter. |
113 * | 138 * |
114 * Returns 0 if success, non-zero if error. */ | 139 * Returns 0 if success, non-zero if error. */ |
115 int PrintParam(const Param* p) { | 140 int PrintParam(const Param* p) { |
116 if (p->is_string) { | 141 if (p->is_string) { |
117 char buf[256]; | 142 char buf[256]; |
118 const char* v = VbGetSystemPropertyString(p->name, buf, sizeof(buf)); | 143 const char* v = VbGetSystemPropertyString(p->name, buf, sizeof(buf)); |
119 if (!v) | 144 if (!v) |
120 return 1; | 145 return 1; |
121 printf("%s", v); | 146 printf("%s", v); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
172 return PrintAllParams(); | 197 return PrintAllParams(); |
173 | 198 |
174 /* Print help if needed */ | 199 /* Print help if needed */ |
175 if (!strcasecmp(argv[1], "-h") || !strcmp(argv[1], "-?")) { | 200 if (!strcasecmp(argv[1], "-h") || !strcmp(argv[1], "-?")) { |
176 PrintHelp(progname); | 201 PrintHelp(progname); |
177 return 0; | 202 return 0; |
178 } | 203 } |
179 | 204 |
180 /* Otherwise, loop through params and get/set them */ | 205 /* Otherwise, loop through params and get/set them */ |
181 for (i = 1; i < argc && retval == 0; i++) { | 206 for (i = 1; i < argc && retval == 0; i++) { |
182 char* name = strtok(argv[i], "="); | 207 int has_set = (NULL != strchr(argv[i], '=')); |
183 char* value = strtok(NULL, "="); | 208 int has_expect = (NULL != strchr(argv[i], '?')); |
209 char* name = strtok(argv[i], "=?"); | |
210 char* value = strtok(NULL, "=?"); | |
184 const Param* p = FindParam(name); | 211 const Param* p = FindParam(name); |
185 if (!p) { | 212 if (!p) { |
186 fprintf(stderr, "Invalid parameter name: %s\n", name); | 213 fprintf(stderr, "Invalid parameter name: %s\n", name); |
187 PrintHelp(progname); | 214 PrintHelp(progname); |
188 return 1; | 215 return 1; |
189 } | 216 } |
190 | 217 |
191 if (i > 1) | 218 if (i > 1) |
192 printf(" "); /* Output params space-delimited */ | 219 printf(" "); /* Output params space-delimited */ |
193 if (value) | 220 if (has_set) |
194 retval = SetParam(p, value); | 221 retval = SetParam(p, value); |
222 else if (has_expect) | |
Will Drewry
2011/03/11 02:47:17
Do you want a check for has_set && has_expect to b
| |
223 retval = CheckParam(p, value); | |
195 else | 224 else |
196 retval = PrintParam(p); | 225 retval = PrintParam(p); |
197 } | 226 } |
198 | 227 |
199 return retval; | 228 return retval; |
200 } | 229 } |
OLD | NEW |