Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Simulator option handling. |
9b254dd1 | 2 | Copyright (C) 1996, 1997, 2004, 2007, 2008 Free Software Foundation, Inc. |
c906108c SS |
3 | Contributed by Cygnus Support. |
4 | ||
5 | This file is part of GDB, the GNU debugger. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
4744ac1b JB |
9 | the Free Software Foundation; either version 3 of the License, or |
10 | (at your option) any later version. | |
c906108c SS |
11 | |
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
4744ac1b JB |
17 | You should have received a copy of the GNU General Public License |
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
c906108c SS |
19 | |
20 | #include "sim-main.h" | |
21 | #ifdef HAVE_STRING_H | |
22 | #include <string.h> | |
23 | #else | |
24 | #ifdef HAVE_STRINGS_H | |
25 | #include <strings.h> | |
26 | #endif | |
27 | #endif | |
28 | #ifdef HAVE_STDLIB_H | |
29 | #include <stdlib.h> | |
30 | #endif | |
31 | #include <ctype.h> | |
32 | #include "libiberty.h" | |
33 | #include "sim-options.h" | |
34 | #include "sim-io.h" | |
35 | #include "sim-assert.h" | |
36 | ||
37 | #include "bfd.h" | |
38 | ||
39 | /* Add a set of options to the simulator. | |
40 | TABLE is an array of OPTIONS terminated by a NULL `opt.name' entry. | |
41 | This is intended to be called by modules in their `install' handler. */ | |
42 | ||
43 | SIM_RC | |
44 | sim_add_option_table (SIM_DESC sd, sim_cpu *cpu, const OPTION *table) | |
45 | { | |
46 | struct option_list *ol = ((struct option_list *) | |
47 | xmalloc (sizeof (struct option_list))); | |
48 | ||
49 | /* Note: The list is constructed in the reverse order we're called so | |
50 | later calls will override earlier ones (in case that ever happens). | |
51 | This is the intended behaviour. */ | |
52 | ||
53 | if (cpu) | |
54 | { | |
55 | ol->next = CPU_OPTIONS (cpu); | |
56 | ol->options = table; | |
57 | CPU_OPTIONS (cpu) = ol; | |
58 | } | |
59 | else | |
60 | { | |
61 | ol->next = STATE_OPTIONS (sd); | |
62 | ol->options = table; | |
63 | STATE_OPTIONS (sd) = ol; | |
64 | } | |
65 | ||
66 | return SIM_RC_OK; | |
67 | } | |
68 | ||
69 | /* Standard option table. | |
70 | Modules may specify additional ones. | |
71 | The caller of sim_parse_args may also specify additional options | |
72 | by calling sim_add_option_table first. */ | |
73 | ||
74 | static DECLARE_OPTION_HANDLER (standard_option_handler); | |
75 | ||
76 | /* FIXME: We shouldn't print in --help output options that aren't usable. | |
77 | Some fine tuning will be necessary. One can either move less general | |
78 | options to another table or use a HAVE_FOO macro to ifdef out unavailable | |
79 | options. */ | |
80 | ||
81 | /* ??? One might want to conditionally compile out the entries that | |
82 | aren't enabled. There's a distinction, however, between options a | |
83 | simulator can't support and options that haven't been configured in. | |
84 | Certainly options a simulator can't support shouldn't appear in the | |
85 | output of --help. Whether the same thing applies to options that haven't | |
86 | been configured in or not isn't something I can get worked up over. | |
87 | [Note that conditionally compiling them out might simply involve moving | |
88 | the option to another table.] | |
89 | If you decide to conditionally compile them out as well, delete this | |
90 | comment and add a comment saying that that is the rule. */ | |
91 | ||
92 | typedef enum { | |
93 | OPTION_DEBUG_INSN = OPTION_START, | |
94 | OPTION_DEBUG_FILE, | |
95 | OPTION_DO_COMMAND, | |
96 | OPTION_ARCHITECTURE, | |
97 | OPTION_TARGET, | |
98 | OPTION_ARCHITECTURE_INFO, | |
99 | OPTION_ENVIRONMENT, | |
100 | OPTION_ALIGNMENT, | |
101 | OPTION_VERBOSE, | |
102 | #if defined (SIM_HAVE_BIENDIAN) | |
103 | OPTION_ENDIAN, | |
104 | #endif | |
105 | OPTION_DEBUG, | |
106 | #ifdef SIM_HAVE_FLATMEM | |
107 | OPTION_MEM_SIZE, | |
108 | #endif | |
109 | OPTION_HELP, | |
110 | #ifdef SIM_H8300 /* FIXME: Should be movable to h8300 dir. */ | |
828c9ae6 | 111 | OPTION_H8300H, |
a8cdafbd | 112 | OPTION_H8300S, |
828c9ae6 | 113 | OPTION_H8300SX, |
c906108c | 114 | #endif |
43e526b9 JM |
115 | OPTION_LOAD_LMA, |
116 | OPTION_LOAD_VMA, | |
027e2a04 | 117 | OPTION_SYSROOT |
c906108c SS |
118 | } STANDARD_OPTIONS; |
119 | ||
120 | static const OPTION standard_options[] = | |
121 | { | |
122 | { {"verbose", no_argument, NULL, OPTION_VERBOSE}, | |
123 | 'v', NULL, "Verbose output", | |
124 | standard_option_handler }, | |
125 | ||
126 | #if defined (SIM_HAVE_BIENDIAN) /* ??? && WITH_TARGET_BYTE_ORDER == 0 */ | |
127 | { {"endian", required_argument, NULL, OPTION_ENDIAN}, | |
128 | 'E', "big|little", "Set endianness", | |
129 | standard_option_handler }, | |
130 | #endif | |
131 | ||
132 | #ifdef SIM_HAVE_ENVIRONMENT | |
133 | /* This option isn't supported unless all choices are supported in keeping | |
134 | with the goal of not printing in --help output things the simulator can't | |
135 | do [as opposed to things that just haven't been configured in]. */ | |
136 | { {"environment", required_argument, NULL, OPTION_ENVIRONMENT}, | |
137 | '\0', "user|virtual|operating", "Set running environment", | |
138 | standard_option_handler }, | |
139 | #endif | |
140 | ||
141 | { {"alignment", required_argument, NULL, OPTION_ALIGNMENT}, | |
142 | '\0', "strict|nonstrict|forced", "Set memory access alignment", | |
143 | standard_option_handler }, | |
144 | ||
145 | { {"debug", no_argument, NULL, OPTION_DEBUG}, | |
146 | 'D', NULL, "Print debugging messages", | |
147 | standard_option_handler }, | |
148 | { {"debug-insn", no_argument, NULL, OPTION_DEBUG_INSN}, | |
149 | '\0', NULL, "Print instruction debugging messages", | |
150 | standard_option_handler }, | |
151 | { {"debug-file", required_argument, NULL, OPTION_DEBUG_FILE}, | |
152 | '\0', "FILE NAME", "Specify debugging output file", | |
153 | standard_option_handler }, | |
154 | ||
155 | #ifdef SIM_H8300 /* FIXME: Should be movable to h8300 dir. */ | |
828c9ae6 | 156 | { {"h8300h", no_argument, NULL, OPTION_H8300H}, |
a69146da | 157 | 'h', NULL, "Indicate the CPU is H8/300H", |
a8cdafbd AV |
158 | standard_option_handler }, |
159 | { {"h8300s", no_argument, NULL, OPTION_H8300S}, | |
a69146da | 160 | 'S', NULL, "Indicate the CPU is H8S", |
c906108c | 161 | standard_option_handler }, |
828c9ae6 | 162 | { {"h8300sx", no_argument, NULL, OPTION_H8300SX}, |
a69146da | 163 | 'x', NULL, "Indicate the CPU is H8SX", |
828c9ae6 | 164 | standard_option_handler }, |
c906108c SS |
165 | #endif |
166 | ||
167 | #ifdef SIM_HAVE_FLATMEM | |
168 | { {"mem-size", required_argument, NULL, OPTION_MEM_SIZE}, | |
f40f1a01 NC |
169 | 'm', "<size>[in bytes, Kb (k suffix), Mb (m suffix) or Gb (g suffix)]", |
170 | "Specify memory size", standard_option_handler }, | |
c906108c SS |
171 | #endif |
172 | ||
173 | { {"do-command", required_argument, NULL, OPTION_DO_COMMAND}, | |
174 | '\0', "COMMAND", ""/*undocumented*/, | |
175 | standard_option_handler }, | |
176 | ||
177 | { {"help", no_argument, NULL, OPTION_HELP}, | |
178 | 'H', NULL, "Print help information", | |
179 | standard_option_handler }, | |
180 | ||
181 | { {"architecture", required_argument, NULL, OPTION_ARCHITECTURE}, | |
182 | '\0', "MACHINE", "Specify the architecture to use", | |
183 | standard_option_handler }, | |
184 | { {"architecture-info", no_argument, NULL, OPTION_ARCHITECTURE_INFO}, | |
185 | '\0', NULL, "List supported architectures", | |
186 | standard_option_handler }, | |
187 | { {"info-architecture", no_argument, NULL, OPTION_ARCHITECTURE_INFO}, | |
188 | '\0', NULL, NULL, | |
189 | standard_option_handler }, | |
190 | ||
191 | { {"target", required_argument, NULL, OPTION_TARGET}, | |
192 | '\0', "BFDNAME", "Specify the object-code format for the object files", | |
193 | standard_option_handler }, | |
194 | ||
43e526b9 JM |
195 | #ifdef SIM_HANDLES_LMA |
196 | { {"load-lma", no_argument, NULL, OPTION_LOAD_LMA}, | |
197 | '\0', NULL, | |
198 | #if SIM_HANDLES_LMA | |
199 | "Use VMA or LMA addresses when loading image (default LMA)", | |
200 | #else | |
201 | "Use VMA or LMA addresses when loading image (default VMA)", | |
202 | #endif | |
203 | standard_option_handler, "load-{lma,vma}" }, | |
204 | { {"load-vma", no_argument, NULL, OPTION_LOAD_VMA}, | |
205 | '\0', NULL, "", standard_option_handler, "" }, | |
206 | #endif | |
207 | ||
027e2a04 HPN |
208 | { {"sysroot", required_argument, NULL, OPTION_SYSROOT}, |
209 | '\0', "SYSROOT", | |
210 | "Root for system calls with absolute file-names and cwd at start", | |
211 | standard_option_handler }, | |
212 | ||
c906108c SS |
213 | { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL } |
214 | }; | |
215 | ||
216 | static SIM_RC | |
217 | standard_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt, | |
218 | char *arg, int is_command) | |
219 | { | |
220 | int i,n; | |
221 | ||
222 | switch ((STANDARD_OPTIONS) opt) | |
223 | { | |
224 | case OPTION_VERBOSE: | |
225 | STATE_VERBOSE_P (sd) = 1; | |
226 | break; | |
227 | ||
228 | #ifdef SIM_HAVE_BIENDIAN | |
229 | case OPTION_ENDIAN: | |
230 | if (strcmp (arg, "big") == 0) | |
231 | { | |
232 | if (WITH_TARGET_BYTE_ORDER == LITTLE_ENDIAN) | |
233 | { | |
234 | sim_io_eprintf (sd, "Simulator compiled for little endian only.\n"); | |
235 | return SIM_RC_FAIL; | |
236 | } | |
237 | /* FIXME:wip: Need to set something in STATE_CONFIG. */ | |
238 | current_target_byte_order = BIG_ENDIAN; | |
239 | } | |
240 | else if (strcmp (arg, "little") == 0) | |
241 | { | |
242 | if (WITH_TARGET_BYTE_ORDER == BIG_ENDIAN) | |
243 | { | |
244 | sim_io_eprintf (sd, "Simulator compiled for big endian only.\n"); | |
245 | return SIM_RC_FAIL; | |
246 | } | |
247 | /* FIXME:wip: Need to set something in STATE_CONFIG. */ | |
248 | current_target_byte_order = LITTLE_ENDIAN; | |
249 | } | |
250 | else | |
251 | { | |
252 | sim_io_eprintf (sd, "Invalid endian specification `%s'\n", arg); | |
253 | return SIM_RC_FAIL; | |
254 | } | |
255 | break; | |
256 | #endif | |
257 | ||
258 | case OPTION_ENVIRONMENT: | |
259 | if (strcmp (arg, "user") == 0) | |
260 | STATE_ENVIRONMENT (sd) = USER_ENVIRONMENT; | |
261 | else if (strcmp (arg, "virtual") == 0) | |
262 | STATE_ENVIRONMENT (sd) = VIRTUAL_ENVIRONMENT; | |
263 | else if (strcmp (arg, "operating") == 0) | |
264 | STATE_ENVIRONMENT (sd) = OPERATING_ENVIRONMENT; | |
265 | else | |
266 | { | |
267 | sim_io_eprintf (sd, "Invalid environment specification `%s'\n", arg); | |
268 | return SIM_RC_FAIL; | |
269 | } | |
270 | if (WITH_ENVIRONMENT != ALL_ENVIRONMENT | |
271 | && WITH_ENVIRONMENT != STATE_ENVIRONMENT (sd)) | |
272 | { | |
273 | char *type; | |
274 | switch (WITH_ENVIRONMENT) | |
275 | { | |
276 | case USER_ENVIRONMENT: type = "user"; break; | |
277 | case VIRTUAL_ENVIRONMENT: type = "virtual"; break; | |
278 | case OPERATING_ENVIRONMENT: type = "operating"; break; | |
279 | } | |
280 | sim_io_eprintf (sd, "Simulator compiled for the %s environment only.\n", | |
281 | type); | |
282 | return SIM_RC_FAIL; | |
283 | } | |
284 | break; | |
285 | ||
286 | case OPTION_ALIGNMENT: | |
287 | if (strcmp (arg, "strict") == 0) | |
288 | { | |
289 | if (WITH_ALIGNMENT == 0 || WITH_ALIGNMENT == STRICT_ALIGNMENT) | |
290 | { | |
291 | current_alignment = STRICT_ALIGNMENT; | |
292 | break; | |
293 | } | |
294 | } | |
295 | else if (strcmp (arg, "nonstrict") == 0) | |
296 | { | |
297 | if (WITH_ALIGNMENT == 0 || WITH_ALIGNMENT == NONSTRICT_ALIGNMENT) | |
298 | { | |
299 | current_alignment = NONSTRICT_ALIGNMENT; | |
300 | break; | |
301 | } | |
302 | } | |
303 | else if (strcmp (arg, "forced") == 0) | |
304 | { | |
305 | if (WITH_ALIGNMENT == 0 || WITH_ALIGNMENT == FORCED_ALIGNMENT) | |
306 | { | |
307 | current_alignment = FORCED_ALIGNMENT; | |
308 | break; | |
309 | } | |
310 | } | |
311 | else | |
312 | { | |
313 | sim_io_eprintf (sd, "Invalid alignment specification `%s'\n", arg); | |
314 | return SIM_RC_FAIL; | |
315 | } | |
316 | switch (WITH_ALIGNMENT) | |
317 | { | |
318 | case STRICT_ALIGNMENT: | |
319 | sim_io_eprintf (sd, "Simulator compiled for strict alignment only.\n"); | |
320 | break; | |
321 | case NONSTRICT_ALIGNMENT: | |
322 | sim_io_eprintf (sd, "Simulator compiled for nonstrict alignment only.\n"); | |
323 | break; | |
324 | case FORCED_ALIGNMENT: | |
325 | sim_io_eprintf (sd, "Simulator compiled for forced alignment only.\n"); | |
326 | break; | |
327 | } | |
328 | return SIM_RC_FAIL; | |
329 | ||
330 | case OPTION_DEBUG: | |
331 | if (! WITH_DEBUG) | |
332 | sim_io_eprintf (sd, "Debugging not compiled in, `-D' ignored\n"); | |
333 | else | |
334 | { | |
335 | for (n = 0; n < MAX_NR_PROCESSORS; ++n) | |
336 | for (i = 0; i < MAX_DEBUG_VALUES; ++i) | |
337 | CPU_DEBUG_FLAGS (STATE_CPU (sd, n))[i] = 1; | |
338 | } | |
339 | break; | |
340 | ||
341 | case OPTION_DEBUG_INSN : | |
342 | if (! WITH_DEBUG) | |
343 | sim_io_eprintf (sd, "Debugging not compiled in, `--debug-insn' ignored\n"); | |
344 | else | |
345 | { | |
346 | for (n = 0; n < MAX_NR_PROCESSORS; ++n) | |
347 | CPU_DEBUG_FLAGS (STATE_CPU (sd, n))[DEBUG_INSN_IDX] = 1; | |
348 | } | |
349 | break; | |
350 | ||
351 | case OPTION_DEBUG_FILE : | |
352 | if (! WITH_DEBUG) | |
353 | sim_io_eprintf (sd, "Debugging not compiled in, `--debug-file' ignored\n"); | |
354 | else | |
355 | { | |
356 | FILE *f = fopen (arg, "w"); | |
357 | ||
358 | if (f == NULL) | |
359 | { | |
360 | sim_io_eprintf (sd, "Unable to open debug output file `%s'\n", arg); | |
361 | return SIM_RC_FAIL; | |
362 | } | |
363 | for (n = 0; n < MAX_NR_PROCESSORS; ++n) | |
364 | CPU_DEBUG_FILE (STATE_CPU (sd, n)) = f; | |
365 | } | |
366 | break; | |
367 | ||
368 | #ifdef SIM_H8300 /* FIXME: Can be moved to h8300 dir. */ | |
828c9ae6 MS |
369 | case OPTION_H8300H: |
370 | set_h8300h (bfd_mach_h8300h); | |
a8cdafbd AV |
371 | break; |
372 | case OPTION_H8300S: | |
828c9ae6 MS |
373 | set_h8300h (bfd_mach_h8300s); |
374 | break; | |
375 | case OPTION_H8300SX: | |
376 | set_h8300h (bfd_mach_h8300sx); | |
c906108c SS |
377 | break; |
378 | #endif | |
379 | ||
380 | #ifdef SIM_HAVE_FLATMEM | |
381 | case OPTION_MEM_SIZE: | |
382 | { | |
f40f1a01 NC |
383 | char * endp; |
384 | unsigned long ul = strtol (arg, &endp, 0); | |
385 | ||
386 | switch (* endp) | |
387 | { | |
388 | case 'k': case 'K': size <<= 10; break; | |
389 | case 'm': case 'M': size <<= 20; break; | |
390 | case 'g': case 'G': size <<= 30; break; | |
391 | case ' ': case '\0': case '\t': break; | |
392 | default: | |
393 | if (ul > 0) | |
394 | sim_io_eprintf (sd, "Ignoring strange character at end of memory size: %c\n", * endp); | |
395 | break; | |
396 | } | |
397 | ||
c906108c SS |
398 | /* 16384: some minimal amount */ |
399 | if (! isdigit (arg[0]) || ul < 16384) | |
400 | { | |
401 | sim_io_eprintf (sd, "Invalid memory size `%s'", arg); | |
402 | return SIM_RC_FAIL; | |
403 | } | |
404 | STATE_MEM_SIZE (sd) = ul; | |
405 | } | |
406 | break; | |
407 | #endif | |
408 | ||
409 | case OPTION_DO_COMMAND: | |
410 | sim_do_command (sd, arg); | |
411 | break; | |
412 | ||
413 | case OPTION_ARCHITECTURE: | |
414 | { | |
415 | const struct bfd_arch_info *ap = bfd_scan_arch (arg); | |
416 | if (ap == NULL) | |
417 | { | |
418 | sim_io_eprintf (sd, "Architecture `%s' unknown\n", arg); | |
419 | return SIM_RC_FAIL; | |
420 | } | |
421 | STATE_ARCHITECTURE (sd) = ap; | |
422 | break; | |
423 | } | |
424 | ||
425 | case OPTION_ARCHITECTURE_INFO: | |
426 | { | |
427 | const char **list = bfd_arch_list(); | |
428 | const char **lp; | |
429 | if (list == NULL) | |
430 | abort (); | |
431 | sim_io_printf (sd, "Possible architectures:"); | |
432 | for (lp = list; *lp != NULL; lp++) | |
433 | sim_io_printf (sd, " %s", *lp); | |
434 | sim_io_printf (sd, "\n"); | |
435 | free (list); | |
436 | break; | |
437 | } | |
438 | ||
439 | case OPTION_TARGET: | |
440 | { | |
441 | STATE_TARGET (sd) = xstrdup (arg); | |
442 | break; | |
443 | } | |
444 | ||
43e526b9 JM |
445 | case OPTION_LOAD_LMA: |
446 | { | |
447 | STATE_LOAD_AT_LMA_P (sd) = 1; | |
448 | break; | |
449 | } | |
450 | ||
451 | case OPTION_LOAD_VMA: | |
452 | { | |
453 | STATE_LOAD_AT_LMA_P (sd) = 0; | |
454 | break; | |
455 | } | |
456 | ||
c906108c SS |
457 | case OPTION_HELP: |
458 | sim_print_help (sd, is_command); | |
459 | if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE) | |
460 | exit (0); | |
461 | /* FIXME: 'twould be nice to do something similar if gdb. */ | |
462 | break; | |
027e2a04 HPN |
463 | |
464 | case OPTION_SYSROOT: | |
465 | /* Don't leak memory in the odd event that there's lots of | |
466 | --sysroot=... options. */ | |
467 | if (simulator_sysroot[0] != '\0' && arg[0] != '\0') | |
468 | free (simulator_sysroot); | |
469 | simulator_sysroot = xstrdup (arg); | |
470 | break; | |
c906108c SS |
471 | } |
472 | ||
473 | return SIM_RC_OK; | |
474 | } | |
475 | ||
476 | /* Add the standard option list to the simulator. */ | |
477 | ||
478 | SIM_RC | |
479 | standard_install (SIM_DESC sd) | |
480 | { | |
481 | SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); | |
482 | if (sim_add_option_table (sd, NULL, standard_options) != SIM_RC_OK) | |
483 | return SIM_RC_FAIL; | |
43e526b9 JM |
484 | #ifdef SIM_HANDLES_LMA |
485 | STATE_LOAD_AT_LMA_P (sd) = SIM_HANDLES_LMA; | |
486 | #endif | |
c906108c SS |
487 | return SIM_RC_OK; |
488 | } | |
489 | ||
490 | /* Return non-zero if arg is a duplicate argument. | |
491 | If ARG is NULL, initialize. */ | |
492 | ||
493 | #define ARG_HASH_SIZE 97 | |
494 | #define ARG_HASH(a) ((256 * (unsigned char) a[0] + (unsigned char) a[1]) % ARG_HASH_SIZE) | |
495 | ||
496 | static int | |
497 | dup_arg_p (arg) | |
498 | char *arg; | |
499 | { | |
500 | int hash; | |
501 | static char **arg_table = NULL; | |
502 | ||
503 | if (arg == NULL) | |
504 | { | |
505 | if (arg_table == NULL) | |
506 | arg_table = (char **) xmalloc (ARG_HASH_SIZE * sizeof (char *)); | |
507 | memset (arg_table, 0, ARG_HASH_SIZE * sizeof (char *)); | |
508 | return 0; | |
509 | } | |
510 | ||
511 | hash = ARG_HASH (arg); | |
512 | while (arg_table[hash] != NULL) | |
513 | { | |
514 | if (strcmp (arg, arg_table[hash]) == 0) | |
515 | return 1; | |
516 | /* We assume there won't be more than ARG_HASH_SIZE arguments so we | |
517 | don't check if the table is full. */ | |
518 | if (++hash == ARG_HASH_SIZE) | |
519 | hash = 0; | |
520 | } | |
521 | arg_table[hash] = arg; | |
522 | return 0; | |
523 | } | |
524 | ||
525 | /* Called by sim_open to parse the arguments. */ | |
526 | ||
527 | SIM_RC | |
528 | sim_parse_args (sd, argv) | |
529 | SIM_DESC sd; | |
530 | char **argv; | |
531 | { | |
532 | int c, i, argc, num_opts; | |
533 | char *p, *short_options; | |
534 | /* The `val' option struct entry is dynamically assigned for options that | |
535 | only come in the long form. ORIG_VAL is used to get the original value | |
536 | back. */ | |
537 | int *orig_val; | |
538 | struct option *lp, *long_options; | |
539 | const struct option_list *ol; | |
540 | const OPTION *opt; | |
541 | OPTION_HANDLER **handlers; | |
542 | sim_cpu **opt_cpu; | |
7c070881 | 543 | SIM_RC result = SIM_RC_OK; |
c906108c SS |
544 | |
545 | /* Count the number of arguments. */ | |
546 | for (argc = 0; argv[argc] != NULL; ++argc) | |
547 | continue; | |
548 | ||
549 | /* Count the number of options. */ | |
550 | num_opts = 0; | |
551 | for (ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next) | |
552 | for (opt = ol->options; OPTION_VALID_P (opt); ++opt) | |
553 | ++num_opts; | |
554 | for (i = 0; i < MAX_NR_PROCESSORS; ++i) | |
555 | for (ol = CPU_OPTIONS (STATE_CPU (sd, i)); ol != NULL; ol = ol->next) | |
556 | for (opt = ol->options; OPTION_VALID_P (opt); ++opt) | |
557 | ++num_opts; | |
558 | ||
559 | /* Initialize duplicate argument checker. */ | |
560 | (void) dup_arg_p (NULL); | |
561 | ||
562 | /* Build the option table for getopt. */ | |
563 | ||
564 | long_options = NZALLOC (struct option, num_opts + 1); | |
565 | lp = long_options; | |
566 | short_options = NZALLOC (char, num_opts * 3 + 1); | |
567 | p = short_options; | |
568 | handlers = NZALLOC (OPTION_HANDLER *, OPTION_START + num_opts); | |
569 | orig_val = NZALLOC (int, OPTION_START + num_opts); | |
570 | opt_cpu = NZALLOC (sim_cpu *, OPTION_START + num_opts); | |
571 | ||
572 | /* Set '+' as first char so argument permutation isn't done. This | |
573 | is done to stop getopt_long returning options that appear after | |
574 | the target program. Such options should be passed unchanged into | |
575 | the program image. */ | |
576 | *p++ = '+'; | |
577 | ||
578 | for (i = OPTION_START, ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next) | |
579 | for (opt = ol->options; OPTION_VALID_P (opt); ++opt) | |
580 | { | |
581 | if (dup_arg_p (opt->opt.name)) | |
582 | continue; | |
583 | if (opt->shortopt != 0) | |
584 | { | |
585 | *p++ = opt->shortopt; | |
586 | if (opt->opt.has_arg == required_argument) | |
587 | *p++ = ':'; | |
588 | else if (opt->opt.has_arg == optional_argument) | |
589 | { *p++ = ':'; *p++ = ':'; } | |
590 | handlers[(unsigned char) opt->shortopt] = opt->handler; | |
591 | if (opt->opt.val != 0) | |
592 | orig_val[(unsigned char) opt->shortopt] = opt->opt.val; | |
593 | else | |
594 | orig_val[(unsigned char) opt->shortopt] = opt->shortopt; | |
595 | } | |
596 | if (opt->opt.name != NULL) | |
597 | { | |
598 | *lp = opt->opt; | |
599 | /* Dynamically assign `val' numbers for long options. */ | |
600 | lp->val = i++; | |
601 | handlers[lp->val] = opt->handler; | |
602 | orig_val[lp->val] = opt->opt.val; | |
603 | opt_cpu[lp->val] = NULL; | |
604 | ++lp; | |
605 | } | |
606 | } | |
607 | ||
608 | for (c = 0; c < MAX_NR_PROCESSORS; ++c) | |
609 | { | |
610 | sim_cpu *cpu = STATE_CPU (sd, c); | |
611 | for (ol = CPU_OPTIONS (cpu); ol != NULL; ol = ol->next) | |
612 | for (opt = ol->options; OPTION_VALID_P (opt); ++opt) | |
613 | { | |
614 | #if 0 /* Each option is prepended with --<cpuname>- so this greatly cuts down | |
615 | on the need for dup_arg_p checking. Maybe in the future it'll be | |
616 | needed so this is just commented out, and not deleted. */ | |
617 | if (dup_arg_p (opt->opt.name)) | |
618 | continue; | |
619 | #endif | |
620 | /* Don't allow short versions of cpu specific options for now. */ | |
621 | if (opt->shortopt != 0) | |
622 | { | |
623 | sim_io_eprintf (sd, "internal error, short cpu specific option"); | |
7c070881 SC |
624 | result = SIM_RC_FAIL; |
625 | break; | |
c906108c SS |
626 | } |
627 | if (opt->opt.name != NULL) | |
628 | { | |
629 | char *name; | |
630 | *lp = opt->opt; | |
631 | /* Prepend --<cpuname>- to the option. */ | |
632 | asprintf (&name, "%s-%s", CPU_NAME (cpu), lp->name); | |
633 | lp->name = name; | |
634 | /* Dynamically assign `val' numbers for long options. */ | |
635 | lp->val = i++; | |
636 | handlers[lp->val] = opt->handler; | |
637 | orig_val[lp->val] = opt->opt.val; | |
638 | opt_cpu[lp->val] = cpu; | |
639 | ++lp; | |
640 | } | |
641 | } | |
642 | } | |
643 | ||
644 | /* Terminate the short and long option lists. */ | |
645 | *p = 0; | |
646 | lp->name = NULL; | |
647 | ||
648 | /* Ensure getopt is initialized. */ | |
649 | optind = 0; | |
650 | ||
651 | while (1) | |
652 | { | |
653 | int longind, optc; | |
654 | ||
655 | optc = getopt_long (argc, argv, short_options, long_options, &longind); | |
656 | if (optc == -1) | |
657 | { | |
658 | if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE) | |
659 | STATE_PROG_ARGV (sd) = dupargv (argv + optind); | |
660 | break; | |
661 | } | |
662 | if (optc == '?') | |
7c070881 SC |
663 | { |
664 | result = SIM_RC_FAIL; | |
665 | break; | |
666 | } | |
c906108c SS |
667 | |
668 | if ((*handlers[optc]) (sd, opt_cpu[optc], orig_val[optc], optarg, 0/*!is_command*/) == SIM_RC_FAIL) | |
7c070881 SC |
669 | { |
670 | result = SIM_RC_FAIL; | |
671 | break; | |
672 | } | |
c906108c SS |
673 | } |
674 | ||
7c070881 SC |
675 | zfree (long_options); |
676 | zfree (short_options); | |
677 | zfree (handlers); | |
678 | zfree (opt_cpu); | |
679 | zfree (orig_val); | |
680 | return result; | |
c906108c SS |
681 | } |
682 | ||
683 | /* Utility of sim_print_help to print a list of option tables. */ | |
684 | ||
685 | static void | |
686 | print_help (SIM_DESC sd, sim_cpu *cpu, const struct option_list *ol, int is_command) | |
687 | { | |
688 | const OPTION *opt; | |
689 | ||
690 | for ( ; ol != NULL; ol = ol->next) | |
691 | for (opt = ol->options; OPTION_VALID_P (opt); ++opt) | |
692 | { | |
693 | const int indent = 30; | |
694 | int comma, len; | |
695 | const OPTION *o; | |
696 | ||
697 | if (dup_arg_p (opt->opt.name)) | |
698 | continue; | |
699 | ||
700 | if (opt->doc == NULL) | |
701 | continue; | |
702 | ||
703 | if (opt->doc_name != NULL && opt->doc_name [0] == '\0') | |
704 | continue; | |
705 | ||
706 | sim_io_printf (sd, " "); | |
707 | ||
708 | comma = 0; | |
709 | len = 2; | |
710 | ||
711 | /* list any short options (aliases) for the current OPT */ | |
712 | if (!is_command) | |
713 | { | |
714 | o = opt; | |
715 | do | |
716 | { | |
717 | if (o->shortopt != '\0') | |
718 | { | |
719 | sim_io_printf (sd, "%s-%c", comma ? ", " : "", o->shortopt); | |
720 | len += (comma ? 2 : 0) + 2; | |
721 | if (o->arg != NULL) | |
722 | { | |
723 | if (o->opt.has_arg == optional_argument) | |
724 | { | |
725 | sim_io_printf (sd, "[%s]", o->arg); | |
726 | len += 1 + strlen (o->arg) + 1; | |
727 | } | |
728 | else | |
729 | { | |
730 | sim_io_printf (sd, " %s", o->arg); | |
731 | len += 1 + strlen (o->arg); | |
732 | } | |
733 | } | |
734 | comma = 1; | |
735 | } | |
736 | ++o; | |
737 | } | |
738 | while (OPTION_VALID_P (o) && o->doc == NULL); | |
739 | } | |
740 | ||
741 | /* list any long options (aliases) for the current OPT */ | |
742 | o = opt; | |
743 | do | |
744 | { | |
745 | const char *name; | |
746 | const char *cpu_prefix = cpu ? CPU_NAME (cpu) : NULL; | |
747 | if (o->doc_name != NULL) | |
748 | name = o->doc_name; | |
749 | else | |
750 | name = o->opt.name; | |
751 | if (name != NULL) | |
752 | { | |
753 | sim_io_printf (sd, "%s%s%s%s%s", | |
754 | comma ? ", " : "", | |
755 | is_command ? "" : "--", | |
756 | cpu ? cpu_prefix : "", | |
757 | cpu ? "-" : "", | |
758 | name); | |
759 | len += ((comma ? 2 : 0) | |
760 | + (is_command ? 0 : 2) | |
761 | + strlen (name)); | |
762 | if (o->arg != NULL) | |
763 | { | |
764 | if (o->opt.has_arg == optional_argument) | |
765 | { | |
c4093a6a | 766 | sim_io_printf (sd, "[=%s]", o->arg); |
c906108c SS |
767 | len += 2 + strlen (o->arg) + 1; |
768 | } | |
769 | else | |
770 | { | |
771 | sim_io_printf (sd, " %s", o->arg); | |
772 | len += 1 + strlen (o->arg); | |
773 | } | |
774 | } | |
775 | comma = 1; | |
776 | } | |
777 | ++o; | |
778 | } | |
779 | while (OPTION_VALID_P (o) && o->doc == NULL); | |
780 | ||
781 | if (len >= indent) | |
782 | { | |
783 | sim_io_printf (sd, "\n%*s", indent, ""); | |
784 | } | |
785 | else | |
786 | sim_io_printf (sd, "%*s", indent - len, ""); | |
787 | ||
788 | /* print the description, word wrap long lines */ | |
789 | { | |
790 | const char *chp = opt->doc; | |
791 | unsigned doc_width = 80 - indent; | |
792 | while (strlen (chp) >= doc_width) /* some slack */ | |
793 | { | |
794 | const char *end = chp + doc_width - 1; | |
795 | while (end > chp && !isspace (*end)) | |
796 | end --; | |
797 | if (end == chp) | |
798 | end = chp + doc_width - 1; | |
e158f0a0 AC |
799 | /* The cast should be ok - its distances between to |
800 | points in a string. */ | |
801 | sim_io_printf (sd, "%.*s\n%*s", (int) (end - chp), chp, indent, | |
802 | ""); | |
c906108c SS |
803 | chp = end; |
804 | while (isspace (*chp) && *chp != '\0') | |
805 | chp++; | |
806 | } | |
807 | sim_io_printf (sd, "%s\n", chp); | |
808 | } | |
809 | } | |
810 | } | |
811 | ||
812 | /* Print help messages for the options. */ | |
813 | ||
814 | void | |
815 | sim_print_help (sd, is_command) | |
816 | SIM_DESC sd; | |
817 | int is_command; | |
818 | { | |
819 | if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE) | |
820 | sim_io_printf (sd, "Usage: %s [options] program [program args]\n", | |
821 | STATE_MY_NAME (sd)); | |
822 | ||
823 | /* Initialize duplicate argument checker. */ | |
824 | (void) dup_arg_p (NULL); | |
825 | ||
826 | if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE) | |
827 | sim_io_printf (sd, "Options:\n"); | |
828 | else | |
829 | sim_io_printf (sd, "Commands:\n"); | |
830 | ||
831 | print_help (sd, NULL, STATE_OPTIONS (sd), is_command); | |
832 | sim_io_printf (sd, "\n"); | |
833 | ||
834 | /* Print cpu-specific options. */ | |
835 | { | |
836 | int i; | |
837 | ||
838 | for (i = 0; i < MAX_NR_PROCESSORS; ++i) | |
839 | { | |
840 | sim_cpu *cpu = STATE_CPU (sd, i); | |
841 | if (CPU_OPTIONS (cpu) == NULL) | |
842 | continue; | |
843 | sim_io_printf (sd, "CPU %s specific options:\n", CPU_NAME (cpu)); | |
844 | print_help (sd, cpu, CPU_OPTIONS (cpu), is_command); | |
845 | sim_io_printf (sd, "\n"); | |
846 | } | |
847 | } | |
848 | ||
849 | sim_io_printf (sd, "Note: Depending on the simulator configuration some %ss\n", | |
850 | STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE ? "option" : "command"); | |
851 | sim_io_printf (sd, " may not be applicable\n"); | |
852 | ||
853 | if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE) | |
854 | { | |
855 | sim_io_printf (sd, "\n"); | |
856 | sim_io_printf (sd, "program args Arguments to pass to simulated program.\n"); | |
857 | sim_io_printf (sd, " Note: Very few simulators support this.\n"); | |
858 | } | |
859 | } | |
860 | ||
861 | /* Utility of sim_args_command to find the closest match for a command. | |
862 | Commands that have "-" in them can be specified as separate words. | |
863 | e.g. sim memory-region 0x800000,0x4000 | |
864 | or sim memory region 0x800000,0x4000 | |
865 | If CPU is non-null, use its option table list, otherwise use the main one. | |
866 | *PARGI is where to start looking in ARGV. It is updated to point past | |
867 | the found option. */ | |
868 | ||
869 | static const OPTION * | |
870 | find_match (SIM_DESC sd, sim_cpu *cpu, char *argv[], int *pargi) | |
871 | { | |
872 | const struct option_list *ol; | |
873 | const OPTION *opt; | |
874 | /* most recent option match */ | |
875 | const OPTION *matching_opt = NULL; | |
876 | int matching_argi = -1; | |
877 | ||
878 | if (cpu) | |
879 | ol = CPU_OPTIONS (cpu); | |
880 | else | |
881 | ol = STATE_OPTIONS (sd); | |
882 | ||
883 | /* Skip passed elements specified by *PARGI. */ | |
884 | argv += *pargi; | |
885 | ||
886 | for ( ; ol != NULL; ol = ol->next) | |
887 | for (opt = ol->options; OPTION_VALID_P (opt); ++opt) | |
888 | { | |
889 | int argi = 0; | |
890 | const char *name = opt->opt.name; | |
891 | if (name == NULL) | |
892 | continue; | |
893 | while (argv [argi] != NULL | |
894 | && strncmp (name, argv [argi], strlen (argv [argi])) == 0) | |
895 | { | |
896 | name = &name [strlen (argv[argi])]; | |
897 | if (name [0] == '-') | |
898 | { | |
899 | /* leading match ...<a-b-c>-d-e-f - continue search */ | |
900 | name ++; /* skip `-' */ | |
901 | argi ++; | |
902 | continue; | |
903 | } | |
904 | else if (name [0] == '\0') | |
905 | { | |
906 | /* exact match ...<a-b-c-d-e-f> - better than before? */ | |
907 | if (argi > matching_argi) | |
908 | { | |
909 | matching_argi = argi; | |
910 | matching_opt = opt; | |
911 | } | |
912 | break; | |
913 | } | |
914 | else | |
915 | break; | |
916 | } | |
917 | } | |
918 | ||
919 | *pargi = matching_argi; | |
920 | return matching_opt; | |
921 | } | |
922 | ||
923 | SIM_RC | |
924 | sim_args_command (SIM_DESC sd, char *cmd) | |
925 | { | |
926 | /* something to do? */ | |
927 | if (cmd == NULL) | |
109d3db3 | 928 | return SIM_RC_OK; /* FIXME - perhaps help would be better */ |
c906108c SS |
929 | |
930 | if (cmd [0] == '-') | |
931 | { | |
932 | /* user specified -<opt> ... form? */ | |
933 | char **argv = buildargv (cmd); | |
934 | SIM_RC rc = sim_parse_args (sd, argv); | |
935 | freeargv (argv); | |
936 | return rc; | |
937 | } | |
938 | else | |
939 | { | |
940 | char **argv = buildargv (cmd); | |
941 | const OPTION *matching_opt = NULL; | |
942 | int matching_argi; | |
943 | sim_cpu *cpu; | |
944 | ||
945 | if (argv [0] == NULL) | |
109d3db3 | 946 | return SIM_RC_OK; /* FIXME - perhaps help would be better */ |
c906108c SS |
947 | |
948 | /* First check for a cpu selector. */ | |
949 | { | |
950 | char *cpu_name = xstrdup (argv[0]); | |
951 | char *hyphen = strchr (cpu_name, '-'); | |
952 | if (hyphen) | |
953 | *hyphen = 0; | |
954 | cpu = sim_cpu_lookup (sd, cpu_name); | |
955 | if (cpu) | |
956 | { | |
957 | /* If <cpuname>-<command>, point argv[0] at <command>. */ | |
958 | if (hyphen) | |
959 | { | |
960 | matching_argi = 0; | |
961 | argv[0] += hyphen - cpu_name + 1; | |
962 | } | |
963 | else | |
964 | matching_argi = 1; | |
965 | matching_opt = find_match (sd, cpu, argv, &matching_argi); | |
966 | /* If hyphen found restore argv[0]. */ | |
967 | if (hyphen) | |
968 | argv[0] -= hyphen - cpu_name + 1; | |
969 | } | |
970 | free (cpu_name); | |
971 | } | |
972 | ||
973 | /* If that failed, try the main table. */ | |
974 | if (matching_opt == NULL) | |
975 | { | |
976 | matching_argi = 0; | |
977 | matching_opt = find_match (sd, NULL, argv, &matching_argi); | |
978 | } | |
979 | ||
980 | if (matching_opt != NULL) | |
981 | { | |
982 | switch (matching_opt->opt.has_arg) | |
983 | { | |
984 | case no_argument: | |
985 | if (argv [matching_argi + 1] == NULL) | |
986 | matching_opt->handler (sd, cpu, matching_opt->opt.val, | |
987 | NULL, 1/*is_command*/); | |
988 | else | |
989 | sim_io_eprintf (sd, "Command `%s' takes no arguments\n", | |
990 | matching_opt->opt.name); | |
991 | break; | |
992 | case optional_argument: | |
993 | if (argv [matching_argi + 1] == NULL) | |
994 | matching_opt->handler (sd, cpu, matching_opt->opt.val, | |
995 | NULL, 1/*is_command*/); | |
996 | else if (argv [matching_argi + 2] == NULL) | |
997 | matching_opt->handler (sd, cpu, matching_opt->opt.val, | |
998 | argv [matching_argi + 1], 1/*is_command*/); | |
999 | else | |
1000 | sim_io_eprintf (sd, "Command `%s' requires no more than one argument\n", | |
1001 | matching_opt->opt.name); | |
1002 | break; | |
1003 | case required_argument: | |
1004 | if (argv [matching_argi + 1] == NULL) | |
1005 | sim_io_eprintf (sd, "Command `%s' requires an argument\n", | |
1006 | matching_opt->opt.name); | |
1007 | else if (argv [matching_argi + 2] == NULL) | |
1008 | matching_opt->handler (sd, cpu, matching_opt->opt.val, | |
1009 | argv [matching_argi + 1], 1/*is_command*/); | |
1010 | else | |
1011 | sim_io_eprintf (sd, "Command `%s' requires only one argument\n", | |
1012 | matching_opt->opt.name); | |
1013 | } | |
1014 | freeargv (argv); | |
1015 | return SIM_RC_OK; | |
1016 | } | |
1017 | ||
1018 | freeargv (argv); | |
1019 | } | |
1020 | ||
1021 | /* didn't find anything that remotly matched */ | |
1022 | return SIM_RC_FAIL; | |
1023 | } |