Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | // ========== Copyright Header Begin ========================================== |
2 | // | |
3 | // OpenSPARC T2 Processor File: command_opts.cc | |
4 | // Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. | |
5 | // DO NOT ALTER OR REMOVE COPYRIGHT NOTICES. | |
6 | // | |
7 | // The above named program is free software; you can redistribute it and/or | |
8 | // modify it under the terms of the GNU General Public | |
9 | // License version 2 as published by the Free Software Foundation. | |
10 | // | |
11 | // The above named program is distributed in the hope that it will be | |
12 | // useful, but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | // General Public License for more details. | |
15 | // | |
16 | // You should have received a copy of the GNU General Public | |
17 | // License along with this work; if not, write to the Free Software | |
18 | // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. | |
19 | // | |
20 | // ========== Copyright Header End ============================================ | |
21 | /************************************************************************ | |
22 | ** | |
23 | ** Copyright (C) 2007, Sun Microsystems, Inc. | |
24 | ** | |
25 | ** Sun considers its source code as an unpublished, proprietary | |
26 | ** trade secret and it is available only under strict license provisions. | |
27 | ** This copyright notice is placed here only to protect Sun in the event | |
28 | ** the source is deemed a published work. Disassembly, decompilation, | |
29 | ** or other means of reducing the object code to human readable form | |
30 | ** is prohibited by the license agreement under which this code is | |
31 | ** provided to the user or company in possession of this copy. | |
32 | ** | |
33 | *************************************************************************/ | |
34 | ||
35 | #include "command_opts.h" | |
36 | #include "ui.h" | |
37 | ||
38 | Option::Result Option::parse(const std::string& cmd_line_arg, | |
39 | const char* val_str) | |
40 | { | |
41 | Option::Result result = NOT_FOUND; | |
42 | ||
43 | if (cmd_line_arg.substr(1) == name) | |
44 | { | |
45 | if (seen && repeatable == NOT_REPEATABLE) | |
46 | { | |
47 | error_msg = "Duplicate instance of option -" + name; | |
48 | return PARSE_ERROR; | |
49 | } | |
50 | on_off = ON; | |
51 | ||
52 | if (takes_argument()) | |
53 | { | |
54 | if (val_str == NULL) | |
55 | { | |
56 | error_msg = "Missing argument for option -" + name; | |
57 | return PARSE_ERROR; | |
58 | } | |
59 | if (parse_value(val_str) != VALUE_FOUND) | |
60 | return PARSE_ERROR; | |
61 | ||
62 | result = VALUE_FOUND; | |
63 | } | |
64 | else | |
65 | result = ON_OFF_FOUND; | |
66 | ||
67 | seen = true; | |
68 | ||
69 | for (std::list <ExclusiveOptions*>::iterator excl_opt_iter = | |
70 | excl_opts.begin(); | |
71 | excl_opt_iter != excl_opts.end(); | |
72 | ++excl_opt_iter) | |
73 | { | |
74 | ExclusiveOptions* excl_opt = *excl_opt_iter; | |
75 | ||
76 | if (!excl_opt->update(*this)) | |
77 | { | |
78 | error_msg = excl_opt->get_error_msg(); | |
79 | return PARSE_ERROR; | |
80 | } | |
81 | } | |
82 | } | |
83 | return result; | |
84 | } | |
85 | ||
86 | bool ExclusiveOptions::update(Option& found_opt) | |
87 | { | |
88 | for (std::map<std::string,Option*>::iterator opt_iter = options.begin(); | |
89 | opt_iter != options.end(); | |
90 | ++opt_iter) | |
91 | { | |
92 | Option* opt = opt_iter->second; | |
93 | ||
94 | if (opt == &found_opt) | |
95 | continue; | |
96 | ||
97 | if (compatible == INCOMPATIBLE && opt->is_on()) | |
98 | { | |
99 | error_msg = "Incompatible options -" + opt->get_name() + " and -" + | |
100 | found_opt.get_name() + " set"; | |
101 | return false; | |
102 | } | |
103 | opt->on_off = Option::OFF; | |
104 | } | |
105 | ||
106 | return true; | |
107 | } | |
108 | ||
109 | bool CommandOptions::parse(int argc, | |
110 | const char** argv, | |
111 | std::vector<std::string>& positional_args) | |
112 | { | |
113 | error_msg = ""; | |
114 | ||
115 | int ndx; | |
116 | ||
117 | for (ndx = 0; ndx < argc; ++ndx) | |
118 | { | |
119 | if (argv[ndx][0] != '-') | |
120 | break; | |
121 | ||
122 | if (argv[ndx][1] == '-' && argv[ndx][2] == '\0') | |
123 | { | |
124 | ++ndx; | |
125 | break; | |
126 | } | |
127 | ||
128 | const char* val_arg = (ndx != argc - 1) ? argv[ndx+1] : NULL; | |
129 | ||
130 | for (std::map<std::string,Option*>::iterator opt_iter = options.begin(); | |
131 | opt_iter != options.end(); | |
132 | ++opt_iter) | |
133 | { | |
134 | Option* opt = opt_iter->second; | |
135 | ||
136 | Option::Result result = opt->parse(argv[ndx], val_arg); | |
137 | ||
138 | switch (result) | |
139 | { | |
140 | case Option::VALUE_FOUND: | |
141 | ++ndx; | |
142 | /*FALLTHROUGH*/ | |
143 | case Option::ON_OFF_FOUND: | |
144 | goto found_option; | |
145 | case Option::NOT_FOUND: | |
146 | break; | |
147 | case Option::PARSE_ERROR: | |
148 | error_msg = opt->get_error_msg(); | |
149 | return false; | |
150 | default: | |
151 | ui->fatal("Internal error -- bad result code: %d\n", | |
152 | result); | |
153 | exit(-1); | |
154 | break; | |
155 | } | |
156 | } | |
157 | ||
158 | error_msg = "Illegal option: "; | |
159 | error_msg += argv[ndx]; | |
160 | return false; | |
161 | ||
162 | found_option: | |
163 | ; | |
164 | } | |
165 | ||
166 | positional_args.clear(); | |
167 | for (; ndx < argc; ++ndx) | |
168 | positional_args.push_back(argv[ndx]); | |
169 | ||
170 | return true; | |
171 | } | |
172 | ||
173 | #ifdef COMMAND_OPTS_TEST | |
174 | ||
175 | #include <stdio.h> | |
176 | #include <strings.h> | |
177 | #include <sstream> | |
178 | ||
179 | int command_opts_test_max_cpu_ndx = 15; | |
180 | int& CpuSet::max_cpu_ndx = command_opts_test_max_cpu_ndx; | |
181 | ||
182 | class CommandOptionsTestCase | |
183 | { | |
184 | public: | |
185 | virtual ~CommandOptionsTestCase() {} | |
186 | ||
187 | void print_args() | |
188 | { | |
189 | for (int i = 0; i < argc; ++i) | |
190 | ui->output("%s ", argv[i]); | |
191 | ui->output("\n"); | |
192 | } | |
193 | ||
194 | virtual bool run_test() | |
195 | { | |
196 | cmd_options.add(verbose); | |
197 | cmd_options.add(cpu_opt); | |
198 | cmd_options.add(file_opt); | |
199 | ||
200 | cmd_options.add(pa_opt).add(va_opt); | |
201 | excl_opts.add(pa_opt).add(va_opt); | |
202 | ||
203 | bool result = cmd_options.parse(argc, argv, pos_args); | |
204 | ||
205 | if (!result) | |
206 | { | |
207 | ui->error("parse failed\n"); | |
208 | print_results(); | |
209 | return false; | |
210 | } | |
211 | else if (!verify_test()) | |
212 | { | |
213 | ui->error("verify failed\n"); | |
214 | print_results(); | |
215 | return false; | |
216 | } | |
217 | else | |
218 | return true; | |
219 | } | |
220 | ||
221 | virtual void print_results() | |
222 | { | |
223 | ui->error("command line: "); | |
224 | print_args(); | |
225 | ||
226 | cmd_options.print(); | |
227 | int pos_ndx = 0; | |
228 | for (std::vector<std::string>::iterator pos_arg_iter = pos_args.begin(); | |
229 | pos_arg_iter != pos_args.end(); | |
230 | ++pos_arg_iter) | |
231 | ui->output("Arg[%d]: %s\n", pos_ndx++, pos_arg_iter->c_str()); | |
232 | } | |
233 | ||
234 | virtual bool verify_test() = 0; | |
235 | ||
236 | protected: | |
237 | ||
238 | CommandOptionsTestCase(std::string args, | |
239 | CpuOption::Singleton singleton = CpuOption::SINGLETON) | |
240 | : | |
241 | argc(0), | |
242 | verbose("verbose"), | |
243 | cpu_opt(CpuSet(5), singleton), | |
244 | file_opt("file", "a.out"), | |
245 | pa_opt("pa"), | |
246 | va_opt("va") | |
247 | { | |
248 | ||
249 | std::istringstream args_stream(args); | |
250 | ||
251 | std::string buf; | |
252 | while (args_stream >> buf) | |
253 | argv[argc++] = strdup(buf.c_str()); | |
254 | } | |
255 | ||
256 | int argc; | |
257 | const char *argv[20]; | |
258 | ||
259 | CommandOptions cmd_options; | |
260 | Option verbose; | |
261 | CpuOption cpu_opt; | |
262 | StringOption file_opt; | |
263 | Option pa_opt; | |
264 | Option va_opt; | |
265 | ExclusiveOptions excl_opts; | |
266 | ||
267 | std::vector<std::string> pos_args; | |
268 | }; | |
269 | ||
270 | class CommandOptionsTestCase1 : public CommandOptionsTestCase | |
271 | { | |
272 | public: | |
273 | ||
274 | CommandOptionsTestCase1() : | |
275 | CommandOptionsTestCase("-cpu 0") | |
276 | {} | |
277 | ||
278 | virtual ~CommandOptionsTestCase1() {} | |
279 | ||
280 | virtual bool verify_test() | |
281 | { | |
282 | CpuSet expected_cpu_set(0); | |
283 | if (cpu_opt.get_value() != expected_cpu_set) | |
284 | return false; | |
285 | if (file_opt.get_value() != "a.out") | |
286 | return false; | |
287 | return | |
288 | cpu_opt.is_on() && | |
289 | file_opt.is_off() && | |
290 | pa_opt.is_off() && | |
291 | va_opt.is_off() && | |
292 | verbose.is_off(); | |
293 | } | |
294 | }; | |
295 | ||
296 | class CommandOptionsTestCase2 : public CommandOptionsTestCase | |
297 | { | |
298 | public: | |
299 | ||
300 | CommandOptionsTestCase2() : | |
301 | CommandOptionsTestCase("-cpu 0", CpuOption::MULTITUDE) | |
302 | {} | |
303 | ||
304 | virtual ~CommandOptionsTestCase2() {} | |
305 | ||
306 | virtual bool verify_test() | |
307 | { | |
308 | CpuSet expected_cpu_set(0); | |
309 | if (cpu_opt.get_value() != expected_cpu_set) | |
310 | return false; | |
311 | if (file_opt.get_value() != "a.out") | |
312 | return false; | |
313 | return | |
314 | cpu_opt.is_on() && | |
315 | file_opt.is_off() && | |
316 | pa_opt.is_off() && | |
317 | va_opt.is_off() && | |
318 | verbose.is_off(); | |
319 | } | |
320 | }; | |
321 | ||
322 | class CommandOptionsTestCase3 : public CommandOptionsTestCase | |
323 | { | |
324 | public: | |
325 | ||
326 | CommandOptionsTestCase3() : | |
327 | CommandOptionsTestCase("-cpu 0,2", CpuOption::MULTITUDE) | |
328 | {} | |
329 | ||
330 | virtual ~CommandOptionsTestCase3() {} | |
331 | ||
332 | virtual bool verify_test() | |
333 | { | |
334 | CpuSet expected_cpu_set(0); | |
335 | expected_cpu_set.insert(2); | |
336 | ||
337 | if (cpu_opt.get_value() != expected_cpu_set) | |
338 | return false; | |
339 | if (file_opt.get_value() != "a.out") | |
340 | return false; | |
341 | return | |
342 | cpu_opt.is_on() && | |
343 | file_opt.is_off() && | |
344 | pa_opt.is_off() && | |
345 | va_opt.is_off() && | |
346 | verbose.is_off(); | |
347 | } | |
348 | }; | |
349 | ||
350 | class CommandOptionsTestCase4 : public CommandOptionsTestCase | |
351 | { | |
352 | public: | |
353 | ||
354 | CommandOptionsTestCase4() : | |
355 | CommandOptionsTestCase("-pa") | |
356 | {} | |
357 | ||
358 | virtual ~CommandOptionsTestCase4() {} | |
359 | ||
360 | virtual bool verify_test() | |
361 | { | |
362 | CpuSet expected_cpu_set(5); | |
363 | if (cpu_opt.get_value() != expected_cpu_set) | |
364 | return false; | |
365 | if (file_opt.get_value() != "a.out") | |
366 | return false; | |
367 | return | |
368 | cpu_opt.is_off() && | |
369 | file_opt.is_off() && | |
370 | pa_opt.is_on() && | |
371 | va_opt.is_off() && | |
372 | verbose.is_off(); | |
373 | } | |
374 | }; | |
375 | ||
376 | class CommandOptionsTestCase5 : public CommandOptionsTestCase | |
377 | { | |
378 | public: | |
379 | ||
380 | CommandOptionsTestCase5() : | |
381 | CommandOptionsTestCase("-pa -va") | |
382 | {} | |
383 | ||
384 | virtual ~CommandOptionsTestCase5() {} | |
385 | ||
386 | virtual bool verify_test() | |
387 | { | |
388 | CpuSet expected_cpu_set(5); | |
389 | if (cpu_opt.get_value() != expected_cpu_set) | |
390 | return false; | |
391 | if (file_opt.get_value() != "a.out") | |
392 | return false; | |
393 | return | |
394 | cpu_opt.is_off() && | |
395 | file_opt.is_off() && | |
396 | pa_opt.is_off() && | |
397 | va_opt.is_on() && | |
398 | verbose.is_off(); | |
399 | } | |
400 | }; | |
401 | ||
402 | class CommandOptionsTestCase6 : public CommandOptionsTestCase | |
403 | { | |
404 | public: | |
405 | ||
406 | CommandOptionsTestCase6() | |
407 | : | |
408 | CommandOptionsTestCase("-unsigned 0x1234"), | |
409 | unsigned_opt("unsigned", 10) | |
410 | { | |
411 | cmd_options.add(unsigned_opt); | |
412 | } | |
413 | ||
414 | virtual ~CommandOptionsTestCase6() {} | |
415 | ||
416 | virtual bool verify_test() | |
417 | { | |
418 | CpuSet expected_cpu_set(5); | |
419 | if (cpu_opt.get_value() != expected_cpu_set) | |
420 | return false; | |
421 | if (file_opt.get_value() != "a.out") | |
422 | return false; | |
423 | if (unsigned_opt.get_value() != 0x1234) | |
424 | return false; | |
425 | return | |
426 | cpu_opt.is_off() && | |
427 | file_opt.is_off() && | |
428 | pa_opt.is_off() && | |
429 | va_opt.is_off() && | |
430 | verbose.is_off() && | |
431 | unsigned_opt.is_on(); | |
432 | } | |
433 | ||
434 | UnsignedOption unsigned_opt; | |
435 | }; | |
436 | ||
437 | class CommandOptionsTestCase7 : public CommandOptionsTestCase | |
438 | { | |
439 | public: | |
440 | ||
441 | CommandOptionsTestCase7() | |
442 | : | |
443 | CommandOptionsTestCase("-signed -0x1234"), | |
444 | signed_opt("signed", 10) | |
445 | { | |
446 | cmd_options.add(signed_opt); | |
447 | } | |
448 | ||
449 | virtual ~CommandOptionsTestCase7() {} | |
450 | ||
451 | virtual bool verify_test() | |
452 | { | |
453 | CpuSet expected_cpu_set(5); | |
454 | if (cpu_opt.get_value() != expected_cpu_set) | |
455 | return false; | |
456 | if (file_opt.get_value() != "a.out") | |
457 | return false; | |
458 | if (signed_opt.get_value() != -0x1234) | |
459 | return false; | |
460 | return | |
461 | cpu_opt.is_off() && | |
462 | file_opt.is_off() && | |
463 | pa_opt.is_off() && | |
464 | va_opt.is_off() && | |
465 | verbose.is_off() && | |
466 | signed_opt.is_on(); | |
467 | } | |
468 | ||
469 | SignedOption signed_opt; | |
470 | }; | |
471 | ||
472 | int | |
473 | main(int ac, const char *argv[]) | |
474 | { | |
475 | CommandOptionsTestCase1().run_test(); | |
476 | ||
477 | CommandOptionsTestCase2().run_test(); | |
478 | ||
479 | CommandOptionsTestCase3().run_test(); | |
480 | ||
481 | CommandOptionsTestCase4().run_test(); | |
482 | ||
483 | CommandOptionsTestCase5().run_test(); | |
484 | ||
485 | CommandOptionsTestCase6().run_test(); | |
486 | ||
487 | CommandOptionsTestCase7().run_test(); | |
488 | } | |
489 | ||
490 | #endif /* COMMAND_OPTS_TEST */ |