1 /* Simulator option handling.
2 Copyright (C) 1996-2016 Free Software Foundation, Inc.
3 Contributed by Cygnus Support.
5 This file is part of GDB, the GNU debugger.
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 3 of the License, or
10 (at your option) any later version.
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.
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/>. */
32 #include "libiberty.h"
33 #include "sim-options.h"
35 #include "sim-assert.h"
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. */
45 sim_add_option_table (SIM_DESC sd
, sim_cpu
*cpu
, const OPTION
*table
)
47 struct option_list
*ol
= ((struct option_list
*)
48 xmalloc (sizeof (struct option_list
)));
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. */
56 ol
->next
= CPU_OPTIONS (cpu
);
58 CPU_OPTIONS (cpu
) = ol
;
62 ol
->next
= STATE_OPTIONS (sd
);
64 STATE_OPTIONS (sd
) = ol
;
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. */
75 static DECLARE_OPTION_HANDLER (standard_option_handler
);
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
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. */
94 OPTION_DEBUG_INSN
= OPTION_START
,
99 OPTION_ARCHITECTURE_INFO
,
112 static const OPTION standard_options
[] =
114 { {"verbose", no_argument
, NULL
, OPTION_VERBOSE
},
115 'v', NULL
, "Verbose output",
116 standard_option_handler
, NULL
},
118 { {"endian", required_argument
, NULL
, OPTION_ENDIAN
},
119 'E', "big|little", "Set endianness",
120 standard_option_handler
, NULL
},
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
},
131 { {"alignment", required_argument
, NULL
, OPTION_ALIGNMENT
},
132 '\0', "strict|nonstrict|forced", "Set memory access alignment",
133 standard_option_handler
},
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
},
145 { {"do-command", required_argument
, NULL
, OPTION_DO_COMMAND
},
146 '\0', "COMMAND", ""/*undocumented*/,
147 standard_option_handler
},
149 { {"help", no_argument
, NULL
, OPTION_HELP
},
150 'H', NULL
, "Print help information",
151 standard_option_handler
},
152 { {"version", no_argument
, NULL
, OPTION_VERSION
},
153 '\0', NULL
, "Print version information",
154 standard_option_handler
},
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
},
164 standard_option_handler
},
166 { {"target", required_argument
, NULL
, OPTION_TARGET
},
167 '\0', "BFDNAME", "Specify the object-code format for the object files",
168 standard_option_handler
},
170 { {"load-lma", no_argument
, NULL
, OPTION_LOAD_LMA
},
172 "Use VMA or LMA addresses when loading image (default LMA)",
173 standard_option_handler
, "load-{lma,vma}" },
174 { {"load-vma", no_argument
, NULL
, OPTION_LOAD_VMA
},
175 '\0', NULL
, "", standard_option_handler
, "" },
177 { {"sysroot", required_argument
, NULL
, OPTION_SYSROOT
},
179 "Root for system calls with absolute file-names and cwd at start",
180 standard_option_handler
, NULL
},
182 { {NULL
, no_argument
, NULL
, 0}, '\0', NULL
, NULL
, NULL
, NULL
}
186 standard_option_handler (SIM_DESC sd
, sim_cpu
*cpu
, int opt
,
187 char *arg
, int is_command
)
191 switch ((STANDARD_OPTIONS
) opt
)
194 STATE_VERBOSE_P (sd
) = 1;
198 if (strcmp (arg
, "big") == 0)
200 if (WITH_TARGET_BYTE_ORDER
== BFD_ENDIAN_LITTLE
)
202 sim_io_eprintf (sd
, "Simulator compiled for little endian only.\n");
205 /* FIXME:wip: Need to set something in STATE_CONFIG. */
206 current_target_byte_order
= BFD_ENDIAN_BIG
;
208 else if (strcmp (arg
, "little") == 0)
210 if (WITH_TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
)
212 sim_io_eprintf (sd
, "Simulator compiled for big endian only.\n");
215 /* FIXME:wip: Need to set something in STATE_CONFIG. */
216 current_target_byte_order
= BFD_ENDIAN_LITTLE
;
220 sim_io_eprintf (sd
, "Invalid endian specification `%s'\n", arg
);
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
;
234 sim_io_eprintf (sd
, "Invalid environment specification `%s'\n", arg
);
237 if (WITH_ENVIRONMENT
!= ALL_ENVIRONMENT
238 && WITH_ENVIRONMENT
!= STATE_ENVIRONMENT (sd
))
241 switch (WITH_ENVIRONMENT
)
243 case USER_ENVIRONMENT
: type
= "user"; break;
244 case VIRTUAL_ENVIRONMENT
: type
= "virtual"; break;
245 case OPERATING_ENVIRONMENT
: type
= "operating"; break;
247 sim_io_eprintf (sd
, "Simulator compiled for the %s environment only.\n",
253 case OPTION_ALIGNMENT
:
254 if (strcmp (arg
, "strict") == 0)
256 if (WITH_ALIGNMENT
== 0 || WITH_ALIGNMENT
== STRICT_ALIGNMENT
)
258 current_alignment
= STRICT_ALIGNMENT
;
262 else if (strcmp (arg
, "nonstrict") == 0)
264 if (WITH_ALIGNMENT
== 0 || WITH_ALIGNMENT
== NONSTRICT_ALIGNMENT
)
266 current_alignment
= NONSTRICT_ALIGNMENT
;
270 else if (strcmp (arg
, "forced") == 0)
272 if (WITH_ALIGNMENT
== 0 || WITH_ALIGNMENT
== FORCED_ALIGNMENT
)
274 current_alignment
= FORCED_ALIGNMENT
;
280 sim_io_eprintf (sd
, "Invalid alignment specification `%s'\n", arg
);
283 switch (WITH_ALIGNMENT
)
285 case STRICT_ALIGNMENT
:
286 sim_io_eprintf (sd
, "Simulator compiled for strict alignment only.\n");
288 case NONSTRICT_ALIGNMENT
:
289 sim_io_eprintf (sd
, "Simulator compiled for nonstrict alignment only.\n");
291 case FORCED_ALIGNMENT
:
292 sim_io_eprintf (sd
, "Simulator compiled for forced alignment only.\n");
299 sim_io_eprintf (sd
, "Debugging not compiled in, `-D' ignored\n");
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;
308 case OPTION_DEBUG_INSN
:
310 sim_io_eprintf (sd
, "Debugging not compiled in, `--debug-insn' ignored\n");
313 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
314 CPU_DEBUG_FLAGS (STATE_CPU (sd
, n
))[DEBUG_INSN_IDX
] = 1;
318 case OPTION_DEBUG_FILE
:
320 sim_io_eprintf (sd
, "Debugging not compiled in, `--debug-file' ignored\n");
323 FILE *f
= fopen (arg
, "w");
327 sim_io_eprintf (sd
, "Unable to open debug output file `%s'\n", arg
);
330 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
331 CPU_DEBUG_FILE (STATE_CPU (sd
, n
)) = f
;
335 case OPTION_DO_COMMAND
:
336 sim_do_command (sd
, arg
);
339 case OPTION_ARCHITECTURE
:
341 const struct bfd_arch_info
*ap
= bfd_scan_arch (arg
);
344 sim_io_eprintf (sd
, "Architecture `%s' unknown\n", arg
);
347 STATE_ARCHITECTURE (sd
) = ap
;
351 case OPTION_ARCHITECTURE_INFO
:
353 const char **list
= bfd_arch_list ();
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");
367 STATE_TARGET (sd
) = xstrdup (arg
);
371 case OPTION_LOAD_LMA
:
373 STATE_LOAD_AT_LMA_P (sd
) = 1;
377 case OPTION_LOAD_VMA
:
379 STATE_LOAD_AT_LMA_P (sd
) = 0;
384 sim_print_help (sd
, is_command
);
385 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
387 /* FIXME: 'twould be nice to do something similar if gdb. */
391 sim_io_printf (sd
, "GNU simulator %s%s\n", PKGVERSION
, version
);
392 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
397 /* Don't leak memory in the odd event that there's lots of
398 --sysroot=... options. We treat "" specially since this
399 is the statically initialized value and cannot free it. */
400 if (simulator_sysroot
[0] != '\0')
401 free (simulator_sysroot
);
403 simulator_sysroot
= xstrdup (arg
);
405 simulator_sysroot
= "";
412 /* Add the standard option list to the simulator. */
415 standard_install (SIM_DESC sd
)
417 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
418 if (sim_add_option_table (sd
, NULL
, standard_options
) != SIM_RC_OK
)
420 STATE_LOAD_AT_LMA_P (sd
) = 1;
424 /* Return non-zero if arg is a duplicate argument.
425 If ARG is NULL, initialize. */
427 #define ARG_HASH_SIZE 97
428 #define ARG_HASH(a) ((256 * (unsigned char) a[0] + (unsigned char) a[1]) % ARG_HASH_SIZE)
431 dup_arg_p (const char *arg
)
434 static const char **arg_table
= NULL
;
438 if (arg_table
== NULL
)
439 arg_table
= (const char **) xmalloc (ARG_HASH_SIZE
* sizeof (char *));
440 memset (arg_table
, 0, ARG_HASH_SIZE
* sizeof (char *));
444 hash
= ARG_HASH (arg
);
445 while (arg_table
[hash
] != NULL
)
447 if (strcmp (arg
, arg_table
[hash
]) == 0)
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
)
454 arg_table
[hash
] = arg
;
458 /* Called by sim_open to parse the arguments. */
461 sim_parse_args (SIM_DESC sd
, char **argv
)
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
469 struct option
*lp
, *long_options
;
470 const struct option_list
*ol
;
472 OPTION_HANDLER
**handlers
;
474 SIM_RC result
= SIM_RC_OK
;
476 /* Count the number of arguments. */
477 argc
= countargv (argv
);
479 /* Count the number of options. */
481 for (ol
= STATE_OPTIONS (sd
); ol
!= NULL
; ol
= ol
->next
)
482 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
484 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
485 for (ol
= CPU_OPTIONS (STATE_CPU (sd
, i
)); ol
!= NULL
; ol
= ol
->next
)
486 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
489 /* Initialize duplicate argument checker. */
490 (void) dup_arg_p (NULL
);
492 /* Build the option table for getopt. */
494 long_options
= NZALLOC (struct option
, num_opts
+ 1);
496 short_options
= NZALLOC (char, num_opts
* 3 + 1);
498 handlers
= NZALLOC (OPTION_HANDLER
*, OPTION_START
+ num_opts
);
499 orig_val
= NZALLOC (int, OPTION_START
+ num_opts
);
500 opt_cpu
= NZALLOC (sim_cpu
*, OPTION_START
+ num_opts
);
502 /* Set '+' as first char so argument permutation isn't done. This
503 is done to stop getopt_long returning options that appear after
504 the target program. Such options should be passed unchanged into
505 the program image. */
508 for (i
= OPTION_START
, ol
= STATE_OPTIONS (sd
); ol
!= NULL
; ol
= ol
->next
)
509 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
511 if (dup_arg_p (opt
->opt
.name
))
513 if (opt
->shortopt
!= 0)
515 *p
++ = opt
->shortopt
;
516 if (opt
->opt
.has_arg
== required_argument
)
518 else if (opt
->opt
.has_arg
== optional_argument
)
519 { *p
++ = ':'; *p
++ = ':'; }
520 handlers
[(unsigned char) opt
->shortopt
] = opt
->handler
;
521 if (opt
->opt
.val
!= 0)
522 orig_val
[(unsigned char) opt
->shortopt
] = opt
->opt
.val
;
524 orig_val
[(unsigned char) opt
->shortopt
] = opt
->shortopt
;
526 if (opt
->opt
.name
!= NULL
)
529 /* Dynamically assign `val' numbers for long options. */
531 handlers
[lp
->val
] = opt
->handler
;
532 orig_val
[lp
->val
] = opt
->opt
.val
;
533 opt_cpu
[lp
->val
] = NULL
;
538 for (c
= 0; c
< MAX_NR_PROCESSORS
; ++c
)
540 sim_cpu
*cpu
= STATE_CPU (sd
, c
);
541 for (ol
= CPU_OPTIONS (cpu
); ol
!= NULL
; ol
= ol
->next
)
542 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
544 #if 0 /* Each option is prepended with --<cpuname>- so this greatly cuts down
545 on the need for dup_arg_p checking. Maybe in the future it'll be
546 needed so this is just commented out, and not deleted. */
547 if (dup_arg_p (opt
->opt
.name
))
550 /* Don't allow short versions of cpu specific options for now. */
551 if (opt
->shortopt
!= 0)
553 sim_io_eprintf (sd
, "internal error, short cpu specific option");
554 result
= SIM_RC_FAIL
;
557 if (opt
->opt
.name
!= NULL
)
561 /* Prepend --<cpuname>- to the option. */
562 if (asprintf (&name
, "%s-%s", CPU_NAME (cpu
), lp
->name
) < 0)
564 sim_io_eprintf (sd
, "internal error, out of memory");
565 result
= SIM_RC_FAIL
;
569 /* Dynamically assign `val' numbers for long options. */
571 handlers
[lp
->val
] = opt
->handler
;
572 orig_val
[lp
->val
] = opt
->opt
.val
;
573 opt_cpu
[lp
->val
] = cpu
;
579 /* Terminate the short and long option lists. */
583 /* Ensure getopt is initialized. */
590 optc
= getopt_long (argc
, argv
, short_options
, long_options
, &longind
);
593 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
594 STATE_PROG_ARGV (sd
) = dupargv (argv
+ optind
);
599 result
= SIM_RC_FAIL
;
603 if ((*handlers
[optc
]) (sd
, opt_cpu
[optc
], orig_val
[optc
], optarg
, 0/*!is_command*/) == SIM_RC_FAIL
)
605 result
= SIM_RC_FAIL
;
611 free (short_options
);
618 /* Utility of sim_print_help to print a list of option tables. */
621 print_help (SIM_DESC sd
, sim_cpu
*cpu
, const struct option_list
*ol
, int is_command
)
625 for ( ; ol
!= NULL
; ol
= ol
->next
)
626 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
628 const int indent
= 30;
632 if (dup_arg_p (opt
->opt
.name
))
635 if (opt
->doc
== NULL
)
638 if (opt
->doc_name
!= NULL
&& opt
->doc_name
[0] == '\0')
641 sim_io_printf (sd
, " ");
646 /* list any short options (aliases) for the current OPT */
652 if (o
->shortopt
!= '\0')
654 sim_io_printf (sd
, "%s-%c", comma
? ", " : "", o
->shortopt
);
655 len
+= (comma
? 2 : 0) + 2;
658 if (o
->opt
.has_arg
== optional_argument
)
660 sim_io_printf (sd
, "[%s]", o
->arg
);
661 len
+= 1 + strlen (o
->arg
) + 1;
665 sim_io_printf (sd
, " %s", o
->arg
);
666 len
+= 1 + strlen (o
->arg
);
673 while (OPTION_VALID_P (o
) && o
->doc
== NULL
);
676 /* list any long options (aliases) for the current OPT */
681 const char *cpu_prefix
= cpu
? CPU_NAME (cpu
) : NULL
;
682 if (o
->doc_name
!= NULL
)
688 sim_io_printf (sd
, "%s%s%s%s%s",
690 is_command
? "" : "--",
691 cpu
? cpu_prefix
: "",
694 len
+= ((comma
? 2 : 0)
695 + (is_command
? 0 : 2)
699 if (o
->opt
.has_arg
== optional_argument
)
701 sim_io_printf (sd
, "[=%s]", o
->arg
);
702 len
+= 2 + strlen (o
->arg
) + 1;
706 sim_io_printf (sd
, " %s", o
->arg
);
707 len
+= 1 + strlen (o
->arg
);
714 while (OPTION_VALID_P (o
) && o
->doc
== NULL
);
718 sim_io_printf (sd
, "\n%*s", indent
, "");
721 sim_io_printf (sd
, "%*s", indent
- len
, "");
723 /* print the description, word wrap long lines */
725 const char *chp
= opt
->doc
;
726 unsigned doc_width
= 80 - indent
;
727 while (strlen (chp
) >= doc_width
) /* some slack */
729 const char *end
= chp
+ doc_width
- 1;
730 while (end
> chp
&& !isspace (*end
))
733 end
= chp
+ doc_width
- 1;
734 /* The cast should be ok - its distances between to
735 points in a string. */
736 sim_io_printf (sd
, "%.*s\n%*s", (int) (end
- chp
), chp
, indent
,
739 while (isspace (*chp
) && *chp
!= '\0')
742 sim_io_printf (sd
, "%s\n", chp
);
747 /* Print help messages for the options. */
750 sim_print_help (SIM_DESC sd
, int is_command
)
752 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
753 sim_io_printf (sd
, "Usage: %s [options] program [program args]\n",
756 /* Initialize duplicate argument checker. */
757 (void) dup_arg_p (NULL
);
759 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
760 sim_io_printf (sd
, "Options:\n");
762 sim_io_printf (sd
, "Commands:\n");
764 print_help (sd
, NULL
, STATE_OPTIONS (sd
), is_command
);
765 sim_io_printf (sd
, "\n");
767 /* Print cpu-specific options. */
771 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
773 sim_cpu
*cpu
= STATE_CPU (sd
, i
);
774 if (CPU_OPTIONS (cpu
) == NULL
)
776 sim_io_printf (sd
, "CPU %s specific options:\n", CPU_NAME (cpu
));
777 print_help (sd
, cpu
, CPU_OPTIONS (cpu
), is_command
);
778 sim_io_printf (sd
, "\n");
782 sim_io_printf (sd
, "Note: Depending on the simulator configuration some %ss\n",
783 STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
? "option" : "command");
784 sim_io_printf (sd
, " may not be applicable\n");
786 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
788 sim_io_printf (sd
, "\n");
789 sim_io_printf (sd
, "program args Arguments to pass to simulated program.\n");
790 sim_io_printf (sd
, " Note: Very few simulators support this.\n");
794 /* Utility of sim_args_command to find the closest match for a command.
795 Commands that have "-" in them can be specified as separate words.
796 e.g. sim memory-region 0x800000,0x4000
797 or sim memory region 0x800000,0x4000
798 If CPU is non-null, use its option table list, otherwise use the main one.
799 *PARGI is where to start looking in ARGV. It is updated to point past
802 static const OPTION
*
803 find_match (SIM_DESC sd
, sim_cpu
*cpu
, char *argv
[], int *pargi
)
805 const struct option_list
*ol
;
807 /* most recent option match */
808 const OPTION
*matching_opt
= NULL
;
809 int matching_argi
= -1;
812 ol
= CPU_OPTIONS (cpu
);
814 ol
= STATE_OPTIONS (sd
);
816 /* Skip passed elements specified by *PARGI. */
819 for ( ; ol
!= NULL
; ol
= ol
->next
)
820 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
823 const char *name
= opt
->opt
.name
;
826 while (argv
[argi
] != NULL
827 && strncmp (name
, argv
[argi
], strlen (argv
[argi
])) == 0)
829 name
= &name
[strlen (argv
[argi
])];
832 /* leading match ...<a-b-c>-d-e-f - continue search */
833 name
++; /* skip `-' */
837 else if (name
[0] == '\0')
839 /* exact match ...<a-b-c-d-e-f> - better than before? */
840 if (argi
> matching_argi
)
842 matching_argi
= argi
;
852 *pargi
= matching_argi
;
857 complete_option_list (char **ret
, size_t *cnt
, const struct option_list
*ol
,
858 const char *text
, const char *word
)
860 const OPTION
*opt
= NULL
;
862 size_t len
= strlen (word
);
864 for ( ; ol
!= NULL
; ol
= ol
->next
)
865 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
867 const char *name
= opt
->opt
.name
;
869 /* A long option to match against? */
873 /* Does this option actually match? */
874 if (strncmp (name
, word
, len
))
877 ret
= xrealloc (ret
, ++*cnt
* sizeof (ret
[0]));
878 ret
[*cnt
- 2] = xstrdup (name
);
884 /* All leading text is stored in @text, while the current word being
885 completed is stored in @word. Trailing text of @word is not. */
888 sim_complete_command (SIM_DESC sd
, const char *text
, const char *word
)
894 /* Only complete first word for now. */
898 cpu
= STATE_CPU (sd
, 0);
900 ret
= complete_option_list (ret
, &cnt
, CPU_OPTIONS (cpu
), text
, word
);
901 ret
= complete_option_list (ret
, &cnt
, STATE_OPTIONS (sd
), text
, word
);
909 sim_args_command (SIM_DESC sd
, const char *cmd
)
911 /* something to do? */
913 return SIM_RC_OK
; /* FIXME - perhaps help would be better */
917 /* user specified -<opt> ... form? */
918 char **argv
= buildargv (cmd
);
919 SIM_RC rc
= sim_parse_args (sd
, argv
);
925 char **argv
= buildargv (cmd
);
926 const OPTION
*matching_opt
= NULL
;
930 if (argv
[0] == NULL
)
933 return SIM_RC_OK
; /* FIXME - perhaps help would be better */
936 /* First check for a cpu selector. */
938 char *cpu_name
= xstrdup (argv
[0]);
939 char *hyphen
= strchr (cpu_name
, '-');
942 cpu
= sim_cpu_lookup (sd
, cpu_name
);
945 /* If <cpuname>-<command>, point argv[0] at <command>. */
949 argv
[0] += hyphen
- cpu_name
+ 1;
953 matching_opt
= find_match (sd
, cpu
, argv
, &matching_argi
);
954 /* If hyphen found restore argv[0]. */
956 argv
[0] -= hyphen
- cpu_name
+ 1;
961 /* If that failed, try the main table. */
962 if (matching_opt
== NULL
)
965 matching_opt
= find_match (sd
, NULL
, argv
, &matching_argi
);
968 if (matching_opt
!= NULL
)
970 switch (matching_opt
->opt
.has_arg
)
973 if (argv
[matching_argi
+ 1] == NULL
)
974 matching_opt
->handler (sd
, cpu
, matching_opt
->opt
.val
,
975 NULL
, 1/*is_command*/);
977 sim_io_eprintf (sd
, "Command `%s' takes no arguments\n",
978 matching_opt
->opt
.name
);
980 case optional_argument
:
981 if (argv
[matching_argi
+ 1] == NULL
)
982 matching_opt
->handler (sd
, cpu
, matching_opt
->opt
.val
,
983 NULL
, 1/*is_command*/);
984 else if (argv
[matching_argi
+ 2] == NULL
)
985 matching_opt
->handler (sd
, cpu
, matching_opt
->opt
.val
,
986 argv
[matching_argi
+ 1], 1/*is_command*/);
988 sim_io_eprintf (sd
, "Command `%s' requires no more than one argument\n",
989 matching_opt
->opt
.name
);
991 case required_argument
:
992 if (argv
[matching_argi
+ 1] == NULL
)
993 sim_io_eprintf (sd
, "Command `%s' requires an argument\n",
994 matching_opt
->opt
.name
);
995 else if (argv
[matching_argi
+ 2] == NULL
)
996 matching_opt
->handler (sd
, cpu
, matching_opt
->opt
.val
,
997 argv
[matching_argi
+ 1], 1/*is_command*/);
999 sim_io_eprintf (sd
, "Command `%s' requires only one argument\n",
1000 matching_opt
->opt
.name
);
1009 /* didn't find anything that remotly matched */