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