cli-setshow.c revision 1.1 1 /* Handle set and show GDB commands.
2
3 Copyright (C) 2000-2014 Free Software Foundation, Inc.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17
18 #include "defs.h"
19 #include "readline/tilde.h"
20 #include "value.h"
21 #include <ctype.h>
22 #include <string.h>
23 #include "arch-utils.h"
24 #include "observer.h"
25
26 #include "ui-out.h"
27
28 #include "cli/cli-decode.h"
29 #include "cli/cli-cmds.h"
30 #include "cli/cli-setshow.h"
31 #include "cli/cli-utils.h"
32
33 /* Return true if the change of command parameter should be notified. */
34
35 static int
36 notify_command_param_changed_p (int param_changed, struct cmd_list_element *c)
37 {
38 if (param_changed == 0)
39 return 0;
40
41 if (c->class == class_maintenance || c->class == class_deprecated
42 || c->class == class_obscure)
43 return 0;
44
45 return 1;
46 }
47
48
49 static enum auto_boolean
51 parse_auto_binary_operation (const char *arg)
52 {
53 if (arg != NULL && *arg != '\0')
54 {
55 int length = strlen (arg);
56
57 while (isspace (arg[length - 1]) && length > 0)
58 length--;
59 if (strncmp (arg, "on", length) == 0
60 || strncmp (arg, "1", length) == 0
61 || strncmp (arg, "yes", length) == 0
62 || strncmp (arg, "enable", length) == 0)
63 return AUTO_BOOLEAN_TRUE;
64 else if (strncmp (arg, "off", length) == 0
65 || strncmp (arg, "0", length) == 0
66 || strncmp (arg, "no", length) == 0
67 || strncmp (arg, "disable", length) == 0)
68 return AUTO_BOOLEAN_FALSE;
69 else if (strncmp (arg, "auto", length) == 0
70 || (strncmp (arg, "-1", length) == 0 && length > 1))
71 return AUTO_BOOLEAN_AUTO;
72 }
73 error (_("\"on\", \"off\" or \"auto\" expected."));
74 return AUTO_BOOLEAN_AUTO; /* Pacify GCC. */
75 }
76
77 /* See cli-setshow.h. */
78
79 int
80 parse_cli_boolean_value (char *arg)
81 {
82 int length;
83
84 if (!arg || !*arg)
85 return 1;
86
87 length = strlen (arg);
88
89 while (arg[length - 1] == ' ' || arg[length - 1] == '\t')
90 length--;
91
92 if (strncmp (arg, "on", length) == 0
93 || strncmp (arg, "1", length) == 0
94 || strncmp (arg, "yes", length) == 0
95 || strncmp (arg, "enable", length) == 0)
96 return 1;
97 else if (strncmp (arg, "off", length) == 0
98 || strncmp (arg, "0", length) == 0
99 || strncmp (arg, "no", length) == 0
100 || strncmp (arg, "disable", length) == 0)
101 return 0;
102 else
103 return -1;
104 }
105
106 void
108 deprecated_show_value_hack (struct ui_file *ignore_file,
109 int ignore_from_tty,
110 struct cmd_list_element *c,
111 const char *value)
112 {
113 /* If there's no command or value, don't try to print it out. */
114 if (c == NULL || value == NULL)
115 return;
116 /* Print doc minus "show" at start. */
117 print_doc_line (gdb_stdout, c->doc + 5);
118 switch (c->var_type)
119 {
120 case var_string:
121 case var_string_noescape:
122 case var_optional_filename:
123 case var_filename:
124 case var_enum:
125 printf_filtered ((" is \"%s\".\n"), value);
126 break;
127 default:
128 printf_filtered ((" is %s.\n"), value);
129 break;
130 }
131 }
132
133 /* Returns true if ARG is "unlimited". */
134
135 static int
136 is_unlimited_literal (const char *arg)
137 {
138 size_t len = sizeof ("unlimited") - 1;
139
140 arg = skip_spaces_const (arg);
141
142 return (strncmp (arg, "unlimited", len) == 0
143 && (isspace (arg[len]) || arg[len] == '\0'));
144 }
145
146
147 /* Do a "set" command. ARG is NULL if no argument, or the
148 text of the argument, and FROM_TTY is nonzero if this command is
149 being entered directly by the user (i.e. these are just like any
150 other command). C is the command list element for the command. */
151
152 void
153 do_set_command (char *arg, int from_tty, struct cmd_list_element *c)
154 {
155 /* A flag to indicate the option is changed or not. */
156 int option_changed = 0;
157
158 gdb_assert (c->type == set_cmd);
159
160 switch (c->var_type)
161 {
162 case var_string:
163 {
164 char *new;
165 const char *p;
166 char *q;
167 int ch;
168
169 if (arg == NULL)
170 arg = "";
171 new = (char *) xmalloc (strlen (arg) + 2);
172 p = arg;
173 q = new;
174 while ((ch = *p++) != '\000')
175 {
176 if (ch == '\\')
177 {
178 /* \ at end of argument is used after spaces
179 so they won't be lost. */
180 /* This is obsolete now that we no longer strip
181 trailing whitespace and actually, the backslash
182 didn't get here in my test, readline or
183 something did something funky with a backslash
184 right before a newline. */
185 if (*p == 0)
186 break;
187 ch = parse_escape (get_current_arch (), &p);
188 if (ch == 0)
189 break; /* C loses */
190 else if (ch > 0)
191 *q++ = ch;
192 }
193 else
194 *q++ = ch;
195 }
196 #if 0
197 if (*(p - 1) != '\\')
198 *q++ = ' ';
199 #endif
200 *q++ = '\0';
201 new = (char *) xrealloc (new, q - new);
202
203 if (*(char **) c->var == NULL
204 || strcmp (*(char **) c->var, new) != 0)
205 {
206 xfree (*(char **) c->var);
207 *(char **) c->var = new;
208
209 option_changed = 1;
210 }
211 else
212 xfree (new);
213 }
214 break;
215 case var_string_noescape:
216 if (arg == NULL)
217 arg = "";
218
219 if (*(char **) c->var == NULL || strcmp (*(char **) c->var, arg) != 0)
220 {
221 xfree (*(char **) c->var);
222 *(char **) c->var = xstrdup (arg);
223
224 option_changed = 1;
225 }
226 break;
227 case var_filename:
228 if (arg == NULL)
229 error_no_arg (_("filename to set it to."));
230 /* FALLTHROUGH */
231 case var_optional_filename:
232 {
233 char *val = NULL;
234
235 if (arg != NULL)
236 {
237 /* Clear trailing whitespace of filename. */
238 char *ptr = arg + strlen (arg) - 1;
239
240 while (ptr >= arg && (*ptr == ' ' || *ptr == '\t'))
241 ptr--;
242 *(ptr + 1) = '\0';
243
244 val = tilde_expand (arg);
245 }
246 else
247 val = xstrdup ("");
248
249 if (*(char **) c->var == NULL
250 || strcmp (*(char **) c->var, val) != 0)
251 {
252 xfree (*(char **) c->var);
253 *(char **) c->var = val;
254
255 option_changed = 1;
256 }
257 else
258 xfree (val);
259 }
260 break;
261 case var_boolean:
262 {
263 int val = parse_cli_boolean_value (arg);
264
265 if (val < 0)
266 error (_("\"on\" or \"off\" expected."));
267 if (val != *(int *) c->var)
268 {
269 *(int *) c->var = val;
270
271 option_changed = 1;
272 }
273 }
274 break;
275 case var_auto_boolean:
276 {
277 enum auto_boolean val = parse_auto_binary_operation (arg);
278
279 if (*(enum auto_boolean *) c->var != val)
280 {
281 *(enum auto_boolean *) c->var = val;
282
283 option_changed = 1;
284 }
285 }
286 break;
287 case var_uinteger:
288 case var_zuinteger:
289 {
290 LONGEST val;
291
292 if (arg == NULL)
293 {
294 if (c->var_type == var_uinteger)
295 error_no_arg (_("integer to set it to, or \"unlimited\"."));
296 else
297 error_no_arg (_("integer to set it to."));
298 }
299
300 if (c->var_type == var_uinteger && is_unlimited_literal (arg))
301 val = 0;
302 else
303 val = parse_and_eval_long (arg);
304
305 if (c->var_type == var_uinteger && val == 0)
306 val = UINT_MAX;
307 else if (val < 0
308 /* For var_uinteger, don't let the user set the value
309 to UINT_MAX directly, as that exposes an
310 implementation detail to the user interface. */
311 || (c->var_type == var_uinteger && val >= UINT_MAX)
312 || (c->var_type == var_zuinteger && val > UINT_MAX))
313 error (_("integer %s out of range"), plongest (val));
314
315 if (*(unsigned int *) c->var != val)
316 {
317 *(unsigned int *) c->var = val;
318
319 option_changed = 1;
320 }
321 }
322 break;
323 case var_integer:
324 case var_zinteger:
325 {
326 LONGEST val;
327
328 if (arg == NULL)
329 {
330 if (c->var_type == var_integer)
331 error_no_arg (_("integer to set it to, or \"unlimited\"."));
332 else
333 error_no_arg (_("integer to set it to."));
334 }
335
336 if (c->var_type == var_integer && is_unlimited_literal (arg))
337 val = 0;
338 else
339 val = parse_and_eval_long (arg);
340
341 if (val == 0 && c->var_type == var_integer)
342 val = INT_MAX;
343 else if (val < INT_MIN
344 /* For var_integer, don't let the user set the value
345 to INT_MAX directly, as that exposes an
346 implementation detail to the user interface. */
347 || (c->var_type == var_integer && val >= INT_MAX)
348 || (c->var_type == var_zinteger && val > INT_MAX))
349 error (_("integer %s out of range"), plongest (val));
350
351 if (*(int *) c->var != val)
352 {
353 *(int *) c->var = val;
354
355 option_changed = 1;
356 }
357 break;
358 }
359 case var_enum:
360 {
361 int i;
362 int len;
363 int nmatches;
364 const char *match = NULL;
365 char *p;
366
367 /* If no argument was supplied, print an informative error
368 message. */
369 if (arg == NULL)
370 {
371 char *msg;
372 int msg_len = 0;
373
374 for (i = 0; c->enums[i]; i++)
375 msg_len += strlen (c->enums[i]) + 2;
376
377 msg = xmalloc (msg_len);
378 *msg = '\0';
379 make_cleanup (xfree, msg);
380
381 for (i = 0; c->enums[i]; i++)
382 {
383 if (i != 0)
384 strcat (msg, ", ");
385 strcat (msg, c->enums[i]);
386 }
387 error (_("Requires an argument. Valid arguments are %s."),
388 msg);
389 }
390
391 p = strchr (arg, ' ');
392
393 if (p)
394 len = p - arg;
395 else
396 len = strlen (arg);
397
398 nmatches = 0;
399 for (i = 0; c->enums[i]; i++)
400 if (strncmp (arg, c->enums[i], len) == 0)
401 {
402 if (c->enums[i][len] == '\0')
403 {
404 match = c->enums[i];
405 nmatches = 1;
406 break; /* Exact match. */
407 }
408 else
409 {
410 match = c->enums[i];
411 nmatches++;
412 }
413 }
414
415 if (nmatches <= 0)
416 error (_("Undefined item: \"%s\"."), arg);
417
418 if (nmatches > 1)
419 error (_("Ambiguous item \"%s\"."), arg);
420
421 if (*(const char **) c->var != match)
422 {
423 *(const char **) c->var = match;
424
425 option_changed = 1;
426 }
427 }
428 break;
429 case var_zuinteger_unlimited:
430 {
431 LONGEST val;
432
433 if (arg == NULL)
434 error_no_arg (_("integer to set it to, or \"unlimited\"."));
435
436 if (is_unlimited_literal (arg))
437 val = -1;
438 else
439 val = parse_and_eval_long (arg);
440
441 if (val > INT_MAX)
442 error (_("integer %s out of range"), plongest (val));
443 else if (val < -1)
444 error (_("only -1 is allowed to set as unlimited"));
445
446 if (*(int *) c->var != val)
447 {
448 *(int *) c->var = val;
449 option_changed = 1;
450 }
451 }
452 break;
453 default:
454 error (_("gdb internal error: bad var_type in do_setshow_command"));
455 }
456 c->func (c, NULL, from_tty);
457 if (deprecated_set_hook)
458 deprecated_set_hook (c);
459
460 if (notify_command_param_changed_p (option_changed, c))
461 {
462 char *name, *cp;
463 struct cmd_list_element **cmds;
464 struct cmd_list_element *p;
465 int i;
466 int length = 0;
467
468 /* Compute the whole multi-word command options. If user types command
469 'set foo bar baz on', c->name is 'baz', and GDB can't pass "bar" to
470 command option change notification, because it is confusing. We can
471 trace back through field 'prefix' to compute the whole options,
472 and pass "foo bar baz" to notification. */
473
474 for (i = 0, p = c; p != NULL; i++)
475 {
476 length += strlen (p->name);
477 length++;
478
479 p = p->prefix;
480 }
481 cp = name = xmalloc (length);
482 cmds = xmalloc (sizeof (struct cmd_list_element *) * i);
483
484 /* Track back through filed 'prefix' and cache them in CMDS. */
485 for (i = 0, p = c; p != NULL; i++)
486 {
487 cmds[i] = p;
488 p = p->prefix;
489 }
490
491 /* Don't trigger any observer notification if prefixlist is not
492 setlist. */
493 i--;
494 if (cmds[i]->prefixlist != &setlist)
495 {
496 xfree (cmds);
497 xfree (name);
498
499 return;
500 }
501 /* Traverse them in the reversed order, and copy their names into
502 NAME. */
503 for (i--; i >= 0; i--)
504 {
505 memcpy (cp, cmds[i]->name, strlen (cmds[i]->name));
506 cp += strlen (cmds[i]->name);
507
508 if (i != 0)
509 {
510 cp[0] = ' ';
511 cp++;
512 }
513 }
514 cp[0] = 0;
515
516 xfree (cmds);
517
518 switch (c->var_type)
519 {
520 case var_string:
521 case var_string_noescape:
522 case var_filename:
523 case var_optional_filename:
524 case var_enum:
525 observer_notify_command_param_changed (name, *(char **) c->var);
526 break;
527 case var_boolean:
528 {
529 char *opt = *(int *) c->var ? "on" : "off";
530
531 observer_notify_command_param_changed (name, opt);
532 }
533 break;
534 case var_auto_boolean:
535 {
536 const char *s = auto_boolean_enums[*(enum auto_boolean *) c->var];
537
538 observer_notify_command_param_changed (name, s);
539 }
540 break;
541 case var_uinteger:
542 case var_zuinteger:
543 {
544 char s[64];
545
546 xsnprintf (s, sizeof s, "%u", *(unsigned int *) c->var);
547 observer_notify_command_param_changed (name, s);
548 }
549 break;
550 case var_integer:
551 case var_zinteger:
552 case var_zuinteger_unlimited:
553 {
554 char s[64];
555
556 xsnprintf (s, sizeof s, "%d", *(int *) c->var);
557 observer_notify_command_param_changed (name, s);
558 }
559 break;
560 }
561 xfree (name);
562 }
563 }
564
565 /* Do a "show" command. ARG is NULL if no argument, or the
566 text of the argument, and FROM_TTY is nonzero if this command is
567 being entered directly by the user (i.e. these are just like any
568 other command). C is the command list element for the command. */
569
570 void
571 do_show_command (char *arg, int from_tty, struct cmd_list_element *c)
572 {
573 struct ui_out *uiout = current_uiout;
574 struct cleanup *old_chain;
575 struct ui_file *stb;
576
577 gdb_assert (c->type == show_cmd);
578
579 stb = mem_fileopen ();
580 old_chain = make_cleanup_ui_file_delete (stb);
581
582 /* Possibly call the pre hook. */
583 if (c->pre_show_hook)
584 (c->pre_show_hook) (c);
585
586 switch (c->var_type)
587 {
588 case var_string:
589 if (*(char **) c->var)
590 fputstr_filtered (*(char **) c->var, '"', stb);
591 break;
592 case var_string_noescape:
593 case var_optional_filename:
594 case var_filename:
595 case var_enum:
596 if (*(char **) c->var)
597 fputs_filtered (*(char **) c->var, stb);
598 break;
599 case var_boolean:
600 fputs_filtered (*(int *) c->var ? "on" : "off", stb);
601 break;
602 case var_auto_boolean:
603 switch (*(enum auto_boolean*) c->var)
604 {
605 case AUTO_BOOLEAN_TRUE:
606 fputs_filtered ("on", stb);
607 break;
608 case AUTO_BOOLEAN_FALSE:
609 fputs_filtered ("off", stb);
610 break;
611 case AUTO_BOOLEAN_AUTO:
612 fputs_filtered ("auto", stb);
613 break;
614 default:
615 internal_error (__FILE__, __LINE__,
616 _("do_show_command: "
617 "invalid var_auto_boolean"));
618 break;
619 }
620 break;
621 case var_uinteger:
622 case var_zuinteger:
623 if (c->var_type == var_uinteger
624 && *(unsigned int *) c->var == UINT_MAX)
625 fputs_filtered ("unlimited", stb);
626 else
627 fprintf_filtered (stb, "%u", *(unsigned int *) c->var);
628 break;
629 case var_integer:
630 case var_zinteger:
631 if (c->var_type == var_integer
632 && *(int *) c->var == INT_MAX)
633 fputs_filtered ("unlimited", stb);
634 else
635 fprintf_filtered (stb, "%d", *(int *) c->var);
636 break;
637 case var_zuinteger_unlimited:
638 {
639 if (*(int *) c->var == -1)
640 fputs_filtered ("unlimited", stb);
641 else
642 fprintf_filtered (stb, "%d", *(int *) c->var);
643 }
644 break;
645 default:
646 error (_("gdb internal error: bad var_type in do_show_command"));
647 }
648
649
650 /* FIXME: cagney/2005-02-10: Need to split this in half: code to
651 convert the value into a string (esentially the above); and
652 code to print the value out. For the latter there should be
653 MI and CLI specific versions. */
654
655 if (ui_out_is_mi_like_p (uiout))
656 ui_out_field_stream (uiout, "value", stb);
657 else
658 {
659 char *value = ui_file_xstrdup (stb, NULL);
660
661 make_cleanup (xfree, value);
662 if (c->show_value_func != NULL)
663 c->show_value_func (gdb_stdout, from_tty, c, value);
664 else
665 deprecated_show_value_hack (gdb_stdout, from_tty, c, value);
666 }
667 do_cleanups (old_chain);
668
669 c->func (c, NULL, from_tty);
670 }
671
672 /* Show all the settings in a list of show commands. */
673
674 void
675 cmd_show_list (struct cmd_list_element *list, int from_tty, char *prefix)
676 {
677 struct cleanup *showlist_chain;
678 struct ui_out *uiout = current_uiout;
679
680 showlist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "showlist");
681 for (; list != NULL; list = list->next)
682 {
683 /* If we find a prefix, run its list, prefixing our output by its
684 prefix (with "show " skipped). */
685 if (list->prefixlist && !list->abbrev_flag)
686 {
687 struct cleanup *optionlist_chain
688 = make_cleanup_ui_out_tuple_begin_end (uiout, "optionlist");
689 char *new_prefix = strstr (list->prefixname, "show ") + 5;
690
691 if (ui_out_is_mi_like_p (uiout))
692 ui_out_field_string (uiout, "prefix", new_prefix);
693 cmd_show_list (*list->prefixlist, from_tty, new_prefix);
694 /* Close the tuple. */
695 do_cleanups (optionlist_chain);
696 }
697 else
698 {
699 if (list->class != no_set_class)
700 {
701 struct cleanup *option_chain
702 = make_cleanup_ui_out_tuple_begin_end (uiout, "option");
703
704 ui_out_text (uiout, prefix);
705 ui_out_field_string (uiout, "name", list->name);
706 ui_out_text (uiout, ": ");
707 if (list->type == show_cmd)
708 do_show_command ((char *) NULL, from_tty, list);
709 else
710 cmd_func (list, NULL, from_tty);
711 /* Close the tuple. */
712 do_cleanups (option_chain);
713 }
714 }
715 }
716 /* Close the tuple. */
717 do_cleanups (showlist_chain);
718 }
719
720