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