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