Fix for PR gdb/209, PR gdb/156:
[external/binutils.git] / gdb / cli / cli-setshow.c
1 /* Handle set and show GDB commands.
2    Copyright 2000, 2001 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 59 Temple Place - Suite 330,
17    Boston, MA 02111-1307, USA.  */
18
19 #include "defs.h"
20 #include "value.h"
21 #include <ctype.h>
22 #if 0
23 #include "gdb_string.h"
24 #endif
25
26 #ifdef UI_OUT
27 #include "ui-out.h"
28 #endif
29
30 #include "cli/cli-decode.h"
31 #include "cli/cli-cmds.h"
32 #include "cli/cli-setshow.h"
33
34 /* Prototypes for local functions */
35
36 static int parse_binary_operation (char *);
37
38 static enum cmd_auto_boolean parse_auto_binary_operation (const char *arg);
39 \f
40 static enum cmd_auto_boolean
41 parse_auto_binary_operation (const char *arg)
42 {
43   if (arg != NULL && *arg != '\0')
44     {
45       int length = strlen (arg);
46       while (isspace (arg[length - 1]) && length > 0)
47         length--;
48       if (strncmp (arg, "on", length) == 0
49           || strncmp (arg, "1", length) == 0
50           || strncmp (arg, "yes", length) == 0
51           || strncmp (arg, "enable", length) == 0)
52         return CMD_AUTO_BOOLEAN_TRUE;
53       else if (strncmp (arg, "off", length) == 0
54                || strncmp (arg, "0", length) == 0
55                || strncmp (arg, "no", length) == 0
56                || strncmp (arg, "disable", length) == 0)
57         return CMD_AUTO_BOOLEAN_FALSE;
58       else if (strncmp (arg, "auto", length) == 0
59                || (strncmp (arg, "-1", length) == 0 && length > 1))
60         return CMD_AUTO_BOOLEAN_AUTO;
61     }
62   error ("\"on\", \"off\" or \"auto\" expected.");
63   return CMD_AUTO_BOOLEAN_AUTO; /* pacify GCC */
64 }
65
66 static int
67 parse_binary_operation (char *arg)
68 {
69   int length;
70
71   if (!arg || !*arg)
72     return 1;
73
74   length = strlen (arg);
75
76   while (arg[length - 1] == ' ' || arg[length - 1] == '\t')
77     length--;
78
79   if (strncmp (arg, "on", length) == 0
80       || strncmp (arg, "1", length) == 0
81       || strncmp (arg, "yes", length) == 0
82       || strncmp (arg, "enable", length) == 0)
83     return 1;
84   else if (strncmp (arg, "off", length) == 0
85            || strncmp (arg, "0", length) == 0
86            || strncmp (arg, "no", length) == 0
87            || strncmp (arg, "disable", length) == 0)
88     return 0;
89   else
90     {
91       error ("\"on\" or \"off\" expected.");
92       return 0;
93     }
94 }
95 \f
96 /* Do a "set" or "show" command.  ARG is NULL if no argument, or the text
97    of the argument, and FROM_TTY is nonzero if this command is being entered
98    directly by the user (i.e. these are just like any other
99    command).  C is the command list element for the command.  */
100
101 void
102 do_setshow_command (char *arg, int from_tty, struct cmd_list_element *c)
103 {
104   if (c->type == set_cmd)
105     {
106       switch (c->var_type)
107         {
108         case var_string:
109           {
110             char *new;
111             char *p;
112             char *q;
113             int ch;
114
115             if (arg == NULL)
116               arg = "";
117             new = (char *) xmalloc (strlen (arg) + 2);
118             p = arg;
119             q = new;
120             while ((ch = *p++) != '\000')
121               {
122                 if (ch == '\\')
123                   {
124                     /* \ at end of argument is used after spaces
125                        so they won't be lost.  */
126                     /* This is obsolete now that we no longer strip
127                        trailing whitespace and actually, the backslash
128                        didn't get here in my test, readline or
129                        something did something funky with a backslash
130                        right before a newline.  */
131                     if (*p == 0)
132                       break;
133                     ch = parse_escape (&p);
134                     if (ch == 0)
135                       break;    /* C loses */
136                     else if (ch > 0)
137                       *q++ = ch;
138                   }
139                 else
140                   *q++ = ch;
141               }
142 #if 0
143             if (*(p - 1) != '\\')
144               *q++ = ' ';
145 #endif
146             *q++ = '\0';
147             new = (char *) xrealloc (new, q - new);
148             if (*(char **) c->var != NULL)
149               xfree (*(char **) c->var);
150             *(char **) c->var = new;
151           }
152           break;
153         case var_string_noescape:
154           if (arg == NULL)
155             arg = "";
156           if (*(char **) c->var != NULL)
157             xfree (*(char **) c->var);
158           *(char **) c->var = savestring (arg, strlen (arg));
159           break;
160         case var_filename:
161           if (arg == NULL)
162             error_no_arg ("filename to set it to.");
163           if (*(char **) c->var != NULL)
164             xfree (*(char **) c->var);
165           *(char **) c->var = tilde_expand (arg);
166           break;
167         case var_boolean:
168           *(int *) c->var = parse_binary_operation (arg);
169           break;
170         case var_auto_boolean:
171           *(enum cmd_auto_boolean *) c->var = parse_auto_binary_operation (arg);
172           break;
173         case var_uinteger:
174           if (arg == NULL)
175             error_no_arg ("integer to set it to.");
176           *(unsigned int *) c->var = parse_and_eval_long (arg);
177           if (*(unsigned int *) c->var == 0)
178             *(unsigned int *) c->var = UINT_MAX;
179           break;
180         case var_integer:
181           {
182             unsigned int val;
183             if (arg == NULL)
184               error_no_arg ("integer to set it to.");
185             val = parse_and_eval_long (arg);
186             if (val == 0)
187               *(int *) c->var = INT_MAX;
188             else if (val >= INT_MAX)
189               error ("integer %u out of range", val);
190             else
191               *(int *) c->var = val;
192             break;
193           }
194         case var_zinteger:
195           if (arg == NULL)
196             error_no_arg ("integer to set it to.");
197           *(int *) c->var = parse_and_eval_long (arg);
198           break;
199         case var_enum:
200           {
201             int i;
202             int len;
203             int nmatches;
204             const char *match = NULL;
205             char *p;
206
207             /* if no argument was supplied, print an informative error message */
208             if (arg == NULL)
209               {
210                 char msg[1024];
211                 strcpy (msg, "Requires an argument. Valid arguments are ");
212                 for (i = 0; c->enums[i]; i++)
213                   {
214                     if (i != 0)
215                       strcat (msg, ", ");
216                     strcat (msg, c->enums[i]);
217                   }
218                 strcat (msg, ".");
219                 error (msg);
220               }
221
222             p = strchr (arg, ' ');
223
224             if (p)
225               len = p - arg;
226             else
227               len = strlen (arg);
228
229             nmatches = 0;
230             for (i = 0; c->enums[i]; i++)
231               if (strncmp (arg, c->enums[i], len) == 0)
232                 {
233                   if (c->enums[i][len] == '\0')
234                     {
235                       match = c->enums[i];
236                       nmatches = 1;
237                       break; /* exact match. */
238                     }
239                   else
240                     {
241                       match = c->enums[i];
242                       nmatches++;
243                     }
244                 }
245
246             if (nmatches <= 0)
247               error ("Undefined item: \"%s\".", arg);
248
249             if (nmatches > 1)
250               error ("Ambiguous item \"%s\".", arg);
251
252             *(const char **) c->var = match;
253           }
254           break;
255         default:
256           error ("gdb internal error: bad var_type in do_setshow_command");
257         }
258     }
259   else if (c->type == show_cmd)
260     {
261 #ifdef UI_OUT
262       struct cleanup *old_chain;
263       struct ui_stream *stb;
264       int quote;
265
266       stb = ui_out_stream_new (uiout);
267       old_chain = make_cleanup_ui_out_stream_delete (stb);
268 #endif /* UI_OUT */
269
270       /* Possibly call the pre hook.  */
271       if (c->pre_show_hook)
272         (c->pre_show_hook) (c);
273
274       /* Print doc minus "show" at start.  */
275       print_doc_line (gdb_stdout, c->doc + 5);
276
277 #ifdef UI_OUT
278       ui_out_text (uiout, " is ");
279       ui_out_wrap_hint (uiout, "    ");
280       quote = 0;
281       switch (c->var_type)
282         {
283         case var_string:
284           {
285             unsigned char *p;
286
287             if (*(unsigned char **) c->var)
288               fputstr_filtered (*(unsigned char **) c->var, '"', stb->stream);
289             quote = 1;
290           }
291           break;
292         case var_string_noescape:
293         case var_filename:
294         case var_enum:
295           if (*(char **) c->var)
296             fputs_filtered (*(char **) c->var, stb->stream);
297           quote = 1;
298           break;
299         case var_boolean:
300           fputs_filtered (*(int *) c->var ? "on" : "off", stb->stream);
301           break;
302         case var_auto_boolean:
303           switch (*(enum cmd_auto_boolean*) c->var)
304             {
305             case CMD_AUTO_BOOLEAN_TRUE:
306               fputs_filtered ("on", stb->stream);
307               break;
308             case CMD_AUTO_BOOLEAN_FALSE:
309               fputs_filtered ("off", stb->stream);
310               break;
311             case CMD_AUTO_BOOLEAN_AUTO:
312               fputs_filtered ("auto", stb->stream);
313               break;
314             default:
315               internal_error (__FILE__, __LINE__,
316                               "do_setshow_command: invalid var_auto_boolean");
317               break;
318             }
319           break;
320         case var_uinteger:
321           if (*(unsigned int *) c->var == UINT_MAX)
322             {
323               fputs_filtered ("unlimited", stb->stream);
324               break;
325             }
326           /* else fall through */
327         case var_zinteger:
328           fprintf_filtered (stb->stream, "%u", *(unsigned int *) c->var);
329           break;
330         case var_integer:
331           if (*(int *) c->var == INT_MAX)
332             {
333               fputs_filtered ("unlimited", stb->stream);
334             }
335           else
336             fprintf_filtered (stb->stream, "%d", *(int *) c->var);
337           break;
338
339         default:
340           error ("gdb internal error: bad var_type in do_setshow_command");
341         }
342       if (quote)
343         ui_out_text (uiout, "\"");
344       ui_out_field_stream (uiout, "value", stb);
345       if (quote)
346         ui_out_text (uiout, "\"");
347       ui_out_text (uiout, ".\n");
348       do_cleanups (old_chain);
349 #else
350       fputs_filtered (" is ", gdb_stdout);
351       wrap_here ("    ");
352       switch (c->var_type)
353         {
354         case var_string:
355           {
356             fputs_filtered ("\"", gdb_stdout);
357             if (*(unsigned char **) c->var)
358               fputstr_filtered (*(unsigned char **) c->var, '"', gdb_stdout);
359             fputs_filtered ("\"", gdb_stdout);
360           }
361           break;
362         case var_string_noescape:
363         case var_filename:
364         case var_enum:
365           fputs_filtered ("\"", gdb_stdout);
366           if (*(char **) c->var)
367             fputs_filtered (*(char **) c->var, gdb_stdout);
368           fputs_filtered ("\"", gdb_stdout);
369           break;
370         case var_boolean:
371           fputs_filtered (*(int *) c->var ? "on" : "off", gdb_stdout);
372           break;
373         case var_auto_boolean:
374           switch (*(enum cmd_auto_boolean*) c->var)
375             {
376             case CMD_AUTO_BOOLEAN_TRUE:
377               fputs_filtered ("on", gdb_stdout);
378               break;
379             case CMD_AUTO_BOOLEAN_FALSE:
380               fputs_filtered ("off", gdb_stdout);
381               break;
382             case CMD_AUTO_BOOLEAN_AUTO:
383               fputs_filtered ("auto", gdb_stdout);
384               break;
385             default:
386               internal_error (__FILE__, __LINE__,
387                               "do_setshow_command: invalid var_auto_boolean");
388               break;
389             }
390           break;
391         case var_uinteger:
392           if (*(unsigned int *) c->var == UINT_MAX)
393             {
394               fputs_filtered ("unlimited", gdb_stdout);
395               break;
396             }
397           /* else fall through */
398         case var_zinteger:
399           fprintf_filtered (gdb_stdout, "%u", *(unsigned int *) c->var);
400           break;
401         case var_integer:
402           if (*(int *) c->var == INT_MAX)
403             {
404               fputs_filtered ("unlimited", gdb_stdout);
405             }
406           else
407             fprintf_filtered (gdb_stdout, "%d", *(int *) c->var);
408           break;
409
410         default:
411           error ("gdb internal error: bad var_type in do_setshow_command");
412         }
413       fputs_filtered (".\n", gdb_stdout);
414 #endif
415     }
416   else
417     error ("gdb internal error: bad cmd_type in do_setshow_command");
418   (*c->function.sfunc) (NULL, from_tty, c);
419   if (c->type == set_cmd && set_hook)
420     set_hook (c);
421 }
422
423 /* Show all the settings in a list of show commands.  */
424
425 void
426 cmd_show_list (struct cmd_list_element *list, int from_tty, char *prefix)
427 {
428 #ifdef UI_OUT
429   ui_out_tuple_begin (uiout, "showlist");
430 #endif
431   for (; list != NULL; list = list->next)
432     {
433       /* If we find a prefix, run its list, prefixing our output by its
434          prefix (with "show " skipped).  */
435 #ifdef UI_OUT
436       if (list->prefixlist && !list->abbrev_flag)
437         {
438           ui_out_tuple_begin (uiout, "optionlist");
439           ui_out_field_string (uiout, "prefix", list->prefixname + 5);
440           cmd_show_list (*list->prefixlist, from_tty, list->prefixname + 5);
441           ui_out_tuple_end (uiout);
442         }
443       if (list->type == show_cmd)
444         {
445           ui_out_tuple_begin (uiout, "option");
446           ui_out_text (uiout, prefix);
447           ui_out_field_string (uiout, "name", list->name);
448           ui_out_text (uiout, ":  ");
449           do_setshow_command ((char *) NULL, from_tty, list);
450           ui_out_tuple_end (uiout);
451         }
452 #else
453       if (list->prefixlist && !list->abbrev_flag)
454         cmd_show_list (*list->prefixlist, from_tty, list->prefixname + 5);
455       if (list->type == show_cmd)
456         {
457           fputs_filtered (prefix, gdb_stdout);
458           fputs_filtered (list->name, gdb_stdout);
459           fputs_filtered (":  ", gdb_stdout);
460           do_setshow_command ((char *) NULL, from_tty, list);
461         }
462 #endif
463     }
464 #ifdef UI_OUT
465   ui_out_tuple_end (uiout);
466 #endif
467 }
468