]>
Commit | Line | Data |
---|---|---|
c906108c SS |
1 | /* Simulator option handling. |
2 | Copyright (C) 1996, 1997 Free Software Foundation, Inc. | |
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, | |
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}, | |
169 | 'm', "MEMORY SIZE", "Specify memory size", | |
170 | standard_option_handler }, | |
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 | ||
c906108c SS |
208 | { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL } |
209 | }; | |
210 | ||
211 | static SIM_RC | |
212 | standard_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt, | |
213 | char *arg, int is_command) | |
214 | { | |
215 | int i,n; | |
216 | ||
217 | switch ((STANDARD_OPTIONS) opt) | |
218 | { | |
219 | case OPTION_VERBOSE: | |
220 | STATE_VERBOSE_P (sd) = 1; | |
221 | break; | |
222 | ||
223 | #ifdef SIM_HAVE_BIENDIAN | |
224 | case OPTION_ENDIAN: | |
225 | if (strcmp (arg, "big") == 0) | |
226 | { | |
227 | if (WITH_TARGET_BYTE_ORDER == LITTLE_ENDIAN) | |
228 | { | |
229 | sim_io_eprintf (sd, "Simulator compiled for little endian only.\n"); | |
230 | return SIM_RC_FAIL; | |
231 | } | |
232 | /* FIXME:wip: Need to set something in STATE_CONFIG. */ | |
233 | current_target_byte_order = BIG_ENDIAN; | |
234 | } | |
235 | else if (strcmp (arg, "little") == 0) | |
236 | { | |
237 | if (WITH_TARGET_BYTE_ORDER == BIG_ENDIAN) | |
238 | { | |
239 | sim_io_eprintf (sd, "Simulator compiled for big endian only.\n"); | |
240 | return SIM_RC_FAIL; | |
241 | } | |
242 | /* FIXME:wip: Need to set something in STATE_CONFIG. */ | |
243 | current_target_byte_order = LITTLE_ENDIAN; | |
244 | } | |
245 | else | |
246 | { | |
247 | sim_io_eprintf (sd, "Invalid endian specification `%s'\n", arg); | |
248 | return SIM_RC_FAIL; | |
249 | } | |
250 | break; | |
251 | #endif | |
252 | ||
253 | case OPTION_ENVIRONMENT: | |
254 | if (strcmp (arg, "user") == 0) | |
255 | STATE_ENVIRONMENT (sd) = USER_ENVIRONMENT; | |
256 | else if (strcmp (arg, "virtual") == 0) | |
257 | STATE_ENVIRONMENT (sd) = VIRTUAL_ENVIRONMENT; | |
258 | else if (strcmp (arg, "operating") == 0) | |
259 | STATE_ENVIRONMENT (sd) = OPERATING_ENVIRONMENT; | |
260 | else | |
261 | { | |
262 | sim_io_eprintf (sd, "Invalid environment specification `%s'\n", arg); | |
263 | return SIM_RC_FAIL; | |
264 | } | |
265 | if (WITH_ENVIRONMENT != ALL_ENVIRONMENT | |
266 | && WITH_ENVIRONMENT != STATE_ENVIRONMENT (sd)) | |
267 | { | |
268 | char *type; | |
269 | switch (WITH_ENVIRONMENT) | |
270 | { | |
271 | case USER_ENVIRONMENT: type = "user"; break; | |
272 | case VIRTUAL_ENVIRONMENT: type = "virtual"; break; | |
273 | case OPERATING_ENVIRONMENT: type = "operating"; break; | |
274 | } | |
275 | sim_io_eprintf (sd, "Simulator compiled for the %s environment only.\n", | |
276 | type); | |
277 | return SIM_RC_FAIL; | |
278 | } | |
279 | break; | |
280 | ||
281 | case OPTION_ALIGNMENT: | |
282 | if (strcmp (arg, "strict") == 0) | |
283 | { | |
284 | if (WITH_ALIGNMENT == 0 || WITH_ALIGNMENT == STRICT_ALIGNMENT) | |
285 | { | |
286 | current_alignment = STRICT_ALIGNMENT; | |
287 | break; | |
288 | } | |
289 | } | |
290 | else if (strcmp (arg, "nonstrict") == 0) | |
291 | { | |
292 | if (WITH_ALIGNMENT == 0 || WITH_ALIGNMENT == NONSTRICT_ALIGNMENT) | |
293 | { | |
294 | current_alignment = NONSTRICT_ALIGNMENT; | |
295 | break; | |
296 | } | |
297 | } | |
298 | else if (strcmp (arg, "forced") == 0) | |
299 | { | |
300 | if (WITH_ALIGNMENT == 0 || WITH_ALIGNMENT == FORCED_ALIGNMENT) | |
301 | { | |
302 | current_alignment = FORCED_ALIGNMENT; | |
303 | break; | |
304 | } | |
305 | } | |
306 | else | |
307 | { | |
308 | sim_io_eprintf (sd, "Invalid alignment specification `%s'\n", arg); | |
309 | return SIM_RC_FAIL; | |
310 | } | |
311 | switch (WITH_ALIGNMENT) | |
312 | { | |
313 | case STRICT_ALIGNMENT: | |
314 | sim_io_eprintf (sd, "Simulator compiled for strict alignment only.\n"); | |
315 | break; | |
316 | case NONSTRICT_ALIGNMENT: | |
317 | sim_io_eprintf (sd, "Simulator compiled for nonstrict alignment only.\n"); | |
318 | break; | |
319 | case FORCED_ALIGNMENT: | |
320 | sim_io_eprintf (sd, "Simulator compiled for forced alignment only.\n"); | |
321 | break; | |
322 | } | |
323 | return SIM_RC_FAIL; | |
324 | ||
325 | case OPTION_DEBUG: | |
326 | if (! WITH_DEBUG) | |
327 | sim_io_eprintf (sd, "Debugging not compiled in, `-D' ignored\n"); | |
328 | else | |
329 | { | |
330 | for (n = 0; n < MAX_NR_PROCESSORS; ++n) | |
331 | for (i = 0; i < MAX_DEBUG_VALUES; ++i) | |
332 | CPU_DEBUG_FLAGS (STATE_CPU (sd, n))[i] = 1; | |
333 | } | |
334 | break; | |
335 | ||
336 | case OPTION_DEBUG_INSN : | |
337 | if (! WITH_DEBUG) | |
338 | sim_io_eprintf (sd, "Debugging not compiled in, `--debug-insn' ignored\n"); | |
339 | else | |
340 | { | |
341 | for (n = 0; n < MAX_NR_PROCESSORS; ++n) | |
342 | CPU_DEBUG_FLAGS (STATE_CPU (sd, n))[DEBUG_INSN_IDX] = 1; | |
343 | } | |
344 | break; | |
345 | ||
346 | case OPTION_DEBUG_FILE : | |
347 | if (! WITH_DEBUG) | |
348 | sim_io_eprintf (sd, "Debugging not compiled in, `--debug-file' ignored\n"); | |
349 | else | |
350 | { | |
351 | FILE *f = fopen (arg, "w"); | |
352 | ||
353 | if (f == NULL) | |
354 | { | |
355 | sim_io_eprintf (sd, "Unable to open debug output file `%s'\n", arg); | |
356 | return SIM_RC_FAIL; | |
357 | } | |
358 | for (n = 0; n < MAX_NR_PROCESSORS; ++n) | |
359 | CPU_DEBUG_FILE (STATE_CPU (sd, n)) = f; | |
360 | } | |
361 | break; | |
362 | ||
363 | #ifdef SIM_H8300 /* FIXME: Can be moved to h8300 dir. */ | |
828c9ae6 MS |
364 | case OPTION_H8300H: |
365 | set_h8300h (bfd_mach_h8300h); | |
a8cdafbd AV |
366 | break; |
367 | case OPTION_H8300S: | |
828c9ae6 MS |
368 | set_h8300h (bfd_mach_h8300s); |
369 | break; | |
370 | case OPTION_H8300SX: | |
371 | set_h8300h (bfd_mach_h8300sx); | |
c906108c SS |
372 | break; |
373 | #endif | |
374 | ||
375 | #ifdef SIM_HAVE_FLATMEM | |
376 | case OPTION_MEM_SIZE: | |
377 | { | |
378 | unsigned long ul = strtol (arg, NULL, 0); | |
379 | /* 16384: some minimal amount */ | |
380 | if (! isdigit (arg[0]) || ul < 16384) | |
381 | { | |
382 | sim_io_eprintf (sd, "Invalid memory size `%s'", arg); | |
383 | return SIM_RC_FAIL; | |
384 | } | |
385 | STATE_MEM_SIZE (sd) = ul; | |
386 | } | |
387 | break; | |
388 | #endif | |
389 | ||
390 | case OPTION_DO_COMMAND: | |
391 | sim_do_command (sd, arg); | |
392 | break; | |
393 | ||
394 | case OPTION_ARCHITECTURE: | |
395 | { | |
396 | const struct bfd_arch_info *ap = bfd_scan_arch (arg); | |
397 | if (ap == NULL) | |
398 | { | |
399 | sim_io_eprintf (sd, "Architecture `%s' unknown\n", arg); | |
400 | return SIM_RC_FAIL; | |
401 | } | |
402 | STATE_ARCHITECTURE (sd) = ap; | |
403 | break; | |
404 | } | |
405 | ||
406 | case OPTION_ARCHITECTURE_INFO: | |
407 | { | |
408 | const char **list = bfd_arch_list(); | |
409 | const char **lp; | |
410 | if (list == NULL) | |
411 | abort (); | |
412 | sim_io_printf (sd, "Possible architectures:"); | |
413 | for (lp = list; *lp != NULL; lp++) | |
414 | sim_io_printf (sd, " %s", *lp); | |
415 | sim_io_printf (sd, "\n"); | |
416 | free (list); | |
417 | break; | |
418 | } | |
419 | ||
420 | case OPTION_TARGET: | |
421 | { | |
422 | STATE_TARGET (sd) = xstrdup (arg); | |
423 | break; | |
424 | } | |
425 | ||
43e526b9 JM |
426 | case OPTION_LOAD_LMA: |
427 | { | |
428 | STATE_LOAD_AT_LMA_P (sd) = 1; | |
429 | break; | |
430 | } | |
431 | ||
432 | case OPTION_LOAD_VMA: | |
433 | { | |
434 | STATE_LOAD_AT_LMA_P (sd) = 0; | |
435 | break; | |
436 | } | |
437 | ||
c906108c SS |
438 | case OPTION_HELP: |
439 | sim_print_help (sd, is_command); | |
440 | if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE) | |
441 | exit (0); | |
442 | /* FIXME: 'twould be nice to do something similar if gdb. */ | |
443 | break; | |
444 | } | |
445 | ||
446 | return SIM_RC_OK; | |
447 | } | |
448 | ||
449 | /* Add the standard option list to the simulator. */ | |
450 | ||
451 | SIM_RC | |
452 | standard_install (SIM_DESC sd) | |
453 | { | |
454 | SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER); | |
455 | if (sim_add_option_table (sd, NULL, standard_options) != SIM_RC_OK) | |
456 | return SIM_RC_FAIL; | |
43e526b9 JM |
457 | #ifdef SIM_HANDLES_LMA |
458 | STATE_LOAD_AT_LMA_P (sd) = SIM_HANDLES_LMA; | |
459 | #endif | |
c906108c SS |
460 | return SIM_RC_OK; |
461 | } | |
462 | ||
463 | /* Return non-zero if arg is a duplicate argument. | |
464 | If ARG is NULL, initialize. */ | |
465 | ||
466 | #define ARG_HASH_SIZE 97 | |
467 | #define ARG_HASH(a) ((256 * (unsigned char) a[0] + (unsigned char) a[1]) % ARG_HASH_SIZE) | |
468 | ||
469 | static int | |
470 | dup_arg_p (arg) | |
471 | char *arg; | |
472 | { | |
473 | int hash; | |
474 | static char **arg_table = NULL; | |
475 | ||
476 | if (arg == NULL) | |
477 | { | |
478 | if (arg_table == NULL) | |
479 | arg_table = (char **) xmalloc (ARG_HASH_SIZE * sizeof (char *)); | |
480 | memset (arg_table, 0, ARG_HASH_SIZE * sizeof (char *)); | |
481 | return 0; | |
482 | } | |
483 | ||
484 | hash = ARG_HASH (arg); | |
485 | while (arg_table[hash] != NULL) | |
486 | { | |
487 | if (strcmp (arg, arg_table[hash]) == 0) | |
488 | return 1; | |
489 | /* We assume there won't be more than ARG_HASH_SIZE arguments so we | |
490 | don't check if the table is full. */ | |
491 | if (++hash == ARG_HASH_SIZE) | |
492 | hash = 0; | |
493 | } | |
494 | arg_table[hash] = arg; | |
495 | return 0; | |
496 | } | |
497 | ||
498 | /* Called by sim_open to parse the arguments. */ | |
499 | ||
500 | SIM_RC | |
501 | sim_parse_args (sd, argv) | |
502 | SIM_DESC sd; | |
503 | char **argv; | |
504 | { | |
505 | int c, i, argc, num_opts; | |
506 | char *p, *short_options; | |
507 | /* The `val' option struct entry is dynamically assigned for options that | |
508 | only come in the long form. ORIG_VAL is used to get the original value | |
509 | back. */ | |
510 | int *orig_val; | |
511 | struct option *lp, *long_options; | |
512 | const struct option_list *ol; | |
513 | const OPTION *opt; | |
514 | OPTION_HANDLER **handlers; | |
515 | sim_cpu **opt_cpu; | |
7c070881 | 516 | SIM_RC result = SIM_RC_OK; |
c906108c SS |
517 | |
518 | /* Count the number of arguments. */ | |
519 | for (argc = 0; argv[argc] != NULL; ++argc) | |
520 | continue; | |
521 | ||
522 | /* Count the number of options. */ | |
523 | num_opts = 0; | |
524 | for (ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next) | |
525 | for (opt = ol->options; OPTION_VALID_P (opt); ++opt) | |
526 | ++num_opts; | |
527 | for (i = 0; i < MAX_NR_PROCESSORS; ++i) | |
528 | for (ol = CPU_OPTIONS (STATE_CPU (sd, i)); ol != NULL; ol = ol->next) | |
529 | for (opt = ol->options; OPTION_VALID_P (opt); ++opt) | |
530 | ++num_opts; | |
531 | ||
532 | /* Initialize duplicate argument checker. */ | |
533 | (void) dup_arg_p (NULL); | |
534 | ||
535 | /* Build the option table for getopt. */ | |
536 | ||
537 | long_options = NZALLOC (struct option, num_opts + 1); | |
538 | lp = long_options; | |
539 | short_options = NZALLOC (char, num_opts * 3 + 1); | |
540 | p = short_options; | |
541 | handlers = NZALLOC (OPTION_HANDLER *, OPTION_START + num_opts); | |
542 | orig_val = NZALLOC (int, OPTION_START + num_opts); | |
543 | opt_cpu = NZALLOC (sim_cpu *, OPTION_START + num_opts); | |
544 | ||
545 | /* Set '+' as first char so argument permutation isn't done. This | |
546 | is done to stop getopt_long returning options that appear after | |
547 | the target program. Such options should be passed unchanged into | |
548 | the program image. */ | |
549 | *p++ = '+'; | |
550 | ||
551 | for (i = OPTION_START, ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next) | |
552 | for (opt = ol->options; OPTION_VALID_P (opt); ++opt) | |
553 | { | |
554 | if (dup_arg_p (opt->opt.name)) | |
555 | continue; | |
556 | if (opt->shortopt != 0) | |
557 | { | |
558 | *p++ = opt->shortopt; | |
559 | if (opt->opt.has_arg == required_argument) | |
560 | *p++ = ':'; | |
561 | else if (opt->opt.has_arg == optional_argument) | |
562 | { *p++ = ':'; *p++ = ':'; } | |
563 | handlers[(unsigned char) opt->shortopt] = opt->handler; | |
564 | if (opt->opt.val != 0) | |
565 | orig_val[(unsigned char) opt->shortopt] = opt->opt.val; | |
566 | else | |
567 | orig_val[(unsigned char) opt->shortopt] = opt->shortopt; | |
568 | } | |
569 | if (opt->opt.name != NULL) | |
570 | { | |
571 | *lp = opt->opt; | |
572 | /* Dynamically assign `val' numbers for long options. */ | |
573 | lp->val = i++; | |
574 | handlers[lp->val] = opt->handler; | |
575 | orig_val[lp->val] = opt->opt.val; | |
576 | opt_cpu[lp->val] = NULL; | |
577 | ++lp; | |
578 | } | |
579 | } | |
580 | ||
581 | for (c = 0; c < MAX_NR_PROCESSORS; ++c) | |
582 | { | |
583 | sim_cpu *cpu = STATE_CPU (sd, c); | |
584 | for (ol = CPU_OPTIONS (cpu); ol != NULL; ol = ol->next) | |
585 | for (opt = ol->options; OPTION_VALID_P (opt); ++opt) | |
586 | { | |
587 | #if 0 /* Each option is prepended with --<cpuname>- so this greatly cuts down | |
588 | on the need for dup_arg_p checking. Maybe in the future it'll be | |
589 | needed so this is just commented out, and not deleted. */ | |
590 | if (dup_arg_p (opt->opt.name)) | |
591 | continue; | |
592 | #endif | |
593 | /* Don't allow short versions of cpu specific options for now. */ | |
594 | if (opt->shortopt != 0) | |
595 | { | |
596 | sim_io_eprintf (sd, "internal error, short cpu specific option"); | |
7c070881 SC |
597 | result = SIM_RC_FAIL; |
598 | break; | |
c906108c SS |
599 | } |
600 | if (opt->opt.name != NULL) | |
601 | { | |
602 | char *name; | |
603 | *lp = opt->opt; | |
604 | /* Prepend --<cpuname>- to the option. */ | |
605 | asprintf (&name, "%s-%s", CPU_NAME (cpu), lp->name); | |
606 | lp->name = name; | |
607 | /* Dynamically assign `val' numbers for long options. */ | |
608 | lp->val = i++; | |
609 | handlers[lp->val] = opt->handler; | |
610 | orig_val[lp->val] = opt->opt.val; | |
611 | opt_cpu[lp->val] = cpu; | |
612 | ++lp; | |
613 | } | |
614 | } | |
615 | } | |
616 | ||
617 | /* Terminate the short and long option lists. */ | |
618 | *p = 0; | |
619 | lp->name = NULL; | |
620 | ||
621 | /* Ensure getopt is initialized. */ | |
622 | optind = 0; | |
623 | ||
624 | while (1) | |
625 | { | |
626 | int longind, optc; | |
627 | ||
628 | optc = getopt_long (argc, argv, short_options, long_options, &longind); | |
629 | if (optc == -1) | |
630 | { | |
631 | if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE) | |
632 | STATE_PROG_ARGV (sd) = dupargv (argv + optind); | |
633 | break; | |
634 | } | |
635 | if (optc == '?') | |
7c070881 SC |
636 | { |
637 | result = SIM_RC_FAIL; | |
638 | break; | |
639 | } | |
c906108c SS |
640 | |
641 | if ((*handlers[optc]) (sd, opt_cpu[optc], orig_val[optc], optarg, 0/*!is_command*/) == SIM_RC_FAIL) | |
7c070881 SC |
642 | { |
643 | result = SIM_RC_FAIL; | |
644 | break; | |
645 | } | |
c906108c SS |
646 | } |
647 | ||
7c070881 SC |
648 | zfree (long_options); |
649 | zfree (short_options); | |
650 | zfree (handlers); | |
651 | zfree (opt_cpu); | |
652 | zfree (orig_val); | |
653 | return result; | |
c906108c SS |
654 | } |
655 | ||
656 | /* Utility of sim_print_help to print a list of option tables. */ | |
657 | ||
658 | static void | |
659 | print_help (SIM_DESC sd, sim_cpu *cpu, const struct option_list *ol, int is_command) | |
660 | { | |
661 | const OPTION *opt; | |
662 | ||
663 | for ( ; ol != NULL; ol = ol->next) | |
664 | for (opt = ol->options; OPTION_VALID_P (opt); ++opt) | |
665 | { | |
666 | const int indent = 30; | |
667 | int comma, len; | |
668 | const OPTION *o; | |
669 | ||
670 | if (dup_arg_p (opt->opt.name)) | |
671 | continue; | |
672 | ||
673 | if (opt->doc == NULL) | |
674 | continue; | |
675 | ||
676 | if (opt->doc_name != NULL && opt->doc_name [0] == '\0') | |
677 | continue; | |
678 | ||
679 | sim_io_printf (sd, " "); | |
680 | ||
681 | comma = 0; | |
682 | len = 2; | |
683 | ||
684 | /* list any short options (aliases) for the current OPT */ | |
685 | if (!is_command) | |
686 | { | |
687 | o = opt; | |
688 | do | |
689 | { | |
690 | if (o->shortopt != '\0') | |
691 | { | |
692 | sim_io_printf (sd, "%s-%c", comma ? ", " : "", o->shortopt); | |
693 | len += (comma ? 2 : 0) + 2; | |
694 | if (o->arg != NULL) | |
695 | { | |
696 | if (o->opt.has_arg == optional_argument) | |
697 | { | |
698 | sim_io_printf (sd, "[%s]", o->arg); | |
699 | len += 1 + strlen (o->arg) + 1; | |
700 | } | |
701 | else | |
702 | { | |
703 | sim_io_printf (sd, " %s", o->arg); | |
704 | len += 1 + strlen (o->arg); | |
705 | } | |
706 | } | |
707 | comma = 1; | |
708 | } | |
709 | ++o; | |
710 | } | |
711 | while (OPTION_VALID_P (o) && o->doc == NULL); | |
712 | } | |
713 | ||
714 | /* list any long options (aliases) for the current OPT */ | |
715 | o = opt; | |
716 | do | |
717 | { | |
718 | const char *name; | |
719 | const char *cpu_prefix = cpu ? CPU_NAME (cpu) : NULL; | |
720 | if (o->doc_name != NULL) | |
721 | name = o->doc_name; | |
722 | else | |
723 | name = o->opt.name; | |
724 | if (name != NULL) | |
725 | { | |
726 | sim_io_printf (sd, "%s%s%s%s%s", | |
727 | comma ? ", " : "", | |
728 | is_command ? "" : "--", | |
729 | cpu ? cpu_prefix : "", | |
730 | cpu ? "-" : "", | |
731 | name); | |
732 | len += ((comma ? 2 : 0) | |
733 | + (is_command ? 0 : 2) | |
734 | + strlen (name)); | |
735 | if (o->arg != NULL) | |
736 | { | |
737 | if (o->opt.has_arg == optional_argument) | |
738 | { | |
c4093a6a | 739 | sim_io_printf (sd, "[=%s]", o->arg); |
c906108c SS |
740 | len += 2 + strlen (o->arg) + 1; |
741 | } | |
742 | else | |
743 | { | |
744 | sim_io_printf (sd, " %s", o->arg); | |
745 | len += 1 + strlen (o->arg); | |
746 | } | |
747 | } | |
748 | comma = 1; | |
749 | } | |
750 | ++o; | |
751 | } | |
752 | while (OPTION_VALID_P (o) && o->doc == NULL); | |
753 | ||
754 | if (len >= indent) | |
755 | { | |
756 | sim_io_printf (sd, "\n%*s", indent, ""); | |
757 | } | |
758 | else | |
759 | sim_io_printf (sd, "%*s", indent - len, ""); | |
760 | ||
761 | /* print the description, word wrap long lines */ | |
762 | { | |
763 | const char *chp = opt->doc; | |
764 | unsigned doc_width = 80 - indent; | |
765 | while (strlen (chp) >= doc_width) /* some slack */ | |
766 | { | |
767 | const char *end = chp + doc_width - 1; | |
768 | while (end > chp && !isspace (*end)) | |
769 | end --; | |
770 | if (end == chp) | |
771 | end = chp + doc_width - 1; | |
e158f0a0 AC |
772 | /* The cast should be ok - its distances between to |
773 | points in a string. */ | |
774 | sim_io_printf (sd, "%.*s\n%*s", (int) (end - chp), chp, indent, | |
775 | ""); | |
c906108c SS |
776 | chp = end; |
777 | while (isspace (*chp) && *chp != '\0') | |
778 | chp++; | |
779 | } | |
780 | sim_io_printf (sd, "%s\n", chp); | |
781 | } | |
782 | } | |
783 | } | |
784 | ||
785 | /* Print help messages for the options. */ | |
786 | ||
787 | void | |
788 | sim_print_help (sd, is_command) | |
789 | SIM_DESC sd; | |
790 | int is_command; | |
791 | { | |
792 | if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE) | |
793 | sim_io_printf (sd, "Usage: %s [options] program [program args]\n", | |
794 | STATE_MY_NAME (sd)); | |
795 | ||
796 | /* Initialize duplicate argument checker. */ | |
797 | (void) dup_arg_p (NULL); | |
798 | ||
799 | if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE) | |
800 | sim_io_printf (sd, "Options:\n"); | |
801 | else | |
802 | sim_io_printf (sd, "Commands:\n"); | |
803 | ||
804 | print_help (sd, NULL, STATE_OPTIONS (sd), is_command); | |
805 | sim_io_printf (sd, "\n"); | |
806 | ||
807 | /* Print cpu-specific options. */ | |
808 | { | |
809 | int i; | |
810 | ||
811 | for (i = 0; i < MAX_NR_PROCESSORS; ++i) | |
812 | { | |
813 | sim_cpu *cpu = STATE_CPU (sd, i); | |
814 | if (CPU_OPTIONS (cpu) == NULL) | |
815 | continue; | |
816 | sim_io_printf (sd, "CPU %s specific options:\n", CPU_NAME (cpu)); | |
817 | print_help (sd, cpu, CPU_OPTIONS (cpu), is_command); | |
818 | sim_io_printf (sd, "\n"); | |
819 | } | |
820 | } | |
821 | ||
822 | sim_io_printf (sd, "Note: Depending on the simulator configuration some %ss\n", | |
823 | STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE ? "option" : "command"); | |
824 | sim_io_printf (sd, " may not be applicable\n"); | |
825 | ||
826 | if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE) | |
827 | { | |
828 | sim_io_printf (sd, "\n"); | |
829 | sim_io_printf (sd, "program args Arguments to pass to simulated program.\n"); | |
830 | sim_io_printf (sd, " Note: Very few simulators support this.\n"); | |
831 | } | |
832 | } | |
833 | ||
834 | /* Utility of sim_args_command to find the closest match for a command. | |
835 | Commands that have "-" in them can be specified as separate words. | |
836 | e.g. sim memory-region 0x800000,0x4000 | |
837 | or sim memory region 0x800000,0x4000 | |
838 | If CPU is non-null, use its option table list, otherwise use the main one. | |
839 | *PARGI is where to start looking in ARGV. It is updated to point past | |
840 | the found option. */ | |
841 | ||
842 | static const OPTION * | |
843 | find_match (SIM_DESC sd, sim_cpu *cpu, char *argv[], int *pargi) | |
844 | { | |
845 | const struct option_list *ol; | |
846 | const OPTION *opt; | |
847 | /* most recent option match */ | |
848 | const OPTION *matching_opt = NULL; | |
849 | int matching_argi = -1; | |
850 | ||
851 | if (cpu) | |
852 | ol = CPU_OPTIONS (cpu); | |
853 | else | |
854 | ol = STATE_OPTIONS (sd); | |
855 | ||
856 | /* Skip passed elements specified by *PARGI. */ | |
857 | argv += *pargi; | |
858 | ||
859 | for ( ; ol != NULL; ol = ol->next) | |
860 | for (opt = ol->options; OPTION_VALID_P (opt); ++opt) | |
861 | { | |
862 | int argi = 0; | |
863 | const char *name = opt->opt.name; | |
864 | if (name == NULL) | |
865 | continue; | |
866 | while (argv [argi] != NULL | |
867 | && strncmp (name, argv [argi], strlen (argv [argi])) == 0) | |
868 | { | |
869 | name = &name [strlen (argv[argi])]; | |
870 | if (name [0] == '-') | |
871 | { | |
872 | /* leading match ...<a-b-c>-d-e-f - continue search */ | |
873 | name ++; /* skip `-' */ | |
874 | argi ++; | |
875 | continue; | |
876 | } | |
877 | else if (name [0] == '\0') | |
878 | { | |
879 | /* exact match ...<a-b-c-d-e-f> - better than before? */ | |
880 | if (argi > matching_argi) | |
881 | { | |
882 | matching_argi = argi; | |
883 | matching_opt = opt; | |
884 | } | |
885 | break; | |
886 | } | |
887 | else | |
888 | break; | |
889 | } | |
890 | } | |
891 | ||
892 | *pargi = matching_argi; | |
893 | return matching_opt; | |
894 | } | |
895 | ||
896 | SIM_RC | |
897 | sim_args_command (SIM_DESC sd, char *cmd) | |
898 | { | |
899 | /* something to do? */ | |
900 | if (cmd == NULL) | |
901 | return SIM_RC_OK; /* FIXME - perhaphs help would be better */ | |
902 | ||
903 | if (cmd [0] == '-') | |
904 | { | |
905 | /* user specified -<opt> ... form? */ | |
906 | char **argv = buildargv (cmd); | |
907 | SIM_RC rc = sim_parse_args (sd, argv); | |
908 | freeargv (argv); | |
909 | return rc; | |
910 | } | |
911 | else | |
912 | { | |
913 | char **argv = buildargv (cmd); | |
914 | const OPTION *matching_opt = NULL; | |
915 | int matching_argi; | |
916 | sim_cpu *cpu; | |
917 | ||
918 | if (argv [0] == NULL) | |
919 | return SIM_RC_OK; /* FIXME - perhaphs help would be better */ | |
920 | ||
921 | /* First check for a cpu selector. */ | |
922 | { | |
923 | char *cpu_name = xstrdup (argv[0]); | |
924 | char *hyphen = strchr (cpu_name, '-'); | |
925 | if (hyphen) | |
926 | *hyphen = 0; | |
927 | cpu = sim_cpu_lookup (sd, cpu_name); | |
928 | if (cpu) | |
929 | { | |
930 | /* If <cpuname>-<command>, point argv[0] at <command>. */ | |
931 | if (hyphen) | |
932 | { | |
933 | matching_argi = 0; | |
934 | argv[0] += hyphen - cpu_name + 1; | |
935 | } | |
936 | else | |
937 | matching_argi = 1; | |
938 | matching_opt = find_match (sd, cpu, argv, &matching_argi); | |
939 | /* If hyphen found restore argv[0]. */ | |
940 | if (hyphen) | |
941 | argv[0] -= hyphen - cpu_name + 1; | |
942 | } | |
943 | free (cpu_name); | |
944 | } | |
945 | ||
946 | /* If that failed, try the main table. */ | |
947 | if (matching_opt == NULL) | |
948 | { | |
949 | matching_argi = 0; | |
950 | matching_opt = find_match (sd, NULL, argv, &matching_argi); | |
951 | } | |
952 | ||
953 | if (matching_opt != NULL) | |
954 | { | |
955 | switch (matching_opt->opt.has_arg) | |
956 | { | |
957 | case no_argument: | |
958 | if (argv [matching_argi + 1] == NULL) | |
959 | matching_opt->handler (sd, cpu, matching_opt->opt.val, | |
960 | NULL, 1/*is_command*/); | |
961 | else | |
962 | sim_io_eprintf (sd, "Command `%s' takes no arguments\n", | |
963 | matching_opt->opt.name); | |
964 | break; | |
965 | case optional_argument: | |
966 | if (argv [matching_argi + 1] == NULL) | |
967 | matching_opt->handler (sd, cpu, matching_opt->opt.val, | |
968 | NULL, 1/*is_command*/); | |
969 | else if (argv [matching_argi + 2] == NULL) | |
970 | matching_opt->handler (sd, cpu, matching_opt->opt.val, | |
971 | argv [matching_argi + 1], 1/*is_command*/); | |
972 | else | |
973 | sim_io_eprintf (sd, "Command `%s' requires no more than one argument\n", | |
974 | matching_opt->opt.name); | |
975 | break; | |
976 | case required_argument: | |
977 | if (argv [matching_argi + 1] == NULL) | |
978 | sim_io_eprintf (sd, "Command `%s' requires an argument\n", | |
979 | matching_opt->opt.name); | |
980 | else if (argv [matching_argi + 2] == NULL) | |
981 | matching_opt->handler (sd, cpu, matching_opt->opt.val, | |
982 | argv [matching_argi + 1], 1/*is_command*/); | |
983 | else | |
984 | sim_io_eprintf (sd, "Command `%s' requires only one argument\n", | |
985 | matching_opt->opt.name); | |
986 | } | |
987 | freeargv (argv); | |
988 | return SIM_RC_OK; | |
989 | } | |
990 | ||
991 | freeargv (argv); | |
992 | } | |
993 | ||
994 | /* didn't find anything that remotly matched */ | |
995 | return SIM_RC_FAIL; | |
996 | } |