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