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