gdb/
[binutils-gdb.git] / gdb / cli / cli-setshow.c
1 /* Handle set and show GDB commands.
2
3 Copyright (c) 2000-2003, 2007-2012 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
25 #include "ui-out.h"
26
27 #include "cli/cli-decode.h"
28 #include "cli/cli-cmds.h"
29 #include "cli/cli-setshow.h"
30
31 /* Prototypes for local functions. */
32
33 static int parse_binary_operation (char *);
34
35 \f
36 static enum auto_boolean
37 parse_auto_binary_operation (const char *arg)
38 {
39 if (arg != NULL && *arg != '\0')
40 {
41 int length = strlen (arg);
42
43 while (isspace (arg[length - 1]) && length > 0)
44 length--;
45 if (strncmp (arg, "on", length) == 0
46 || strncmp (arg, "1", length) == 0
47 || strncmp (arg, "yes", length) == 0
48 || strncmp (arg, "enable", length) == 0)
49 return AUTO_BOOLEAN_TRUE;
50 else if (strncmp (arg, "off", length) == 0
51 || strncmp (arg, "0", length) == 0
52 || strncmp (arg, "no", length) == 0
53 || strncmp (arg, "disable", length) == 0)
54 return AUTO_BOOLEAN_FALSE;
55 else if (strncmp (arg, "auto", length) == 0
56 || (strncmp (arg, "-1", length) == 0 && length > 1))
57 return AUTO_BOOLEAN_AUTO;
58 }
59 error (_("\"on\", \"off\" or \"auto\" expected."));
60 return AUTO_BOOLEAN_AUTO; /* Pacify GCC. */
61 }
62
63 static int
64 parse_binary_operation (char *arg)
65 {
66 int length;
67
68 if (!arg || !*arg)
69 return 1;
70
71 length = strlen (arg);
72
73 while (arg[length - 1] == ' ' || arg[length - 1] == '\t')
74 length--;
75
76 if (strncmp (arg, "on", length) == 0
77 || strncmp (arg, "1", length) == 0
78 || strncmp (arg, "yes", length) == 0
79 || strncmp (arg, "enable", length) == 0)
80 return 1;
81 else if (strncmp (arg, "off", length) == 0
82 || strncmp (arg, "0", length) == 0
83 || strncmp (arg, "no", length) == 0
84 || strncmp (arg, "disable", length) == 0)
85 return 0;
86 else
87 {
88 error (_("\"on\" or \"off\" expected."));
89 return 0;
90 }
91 }
92 \f
93 void
94 deprecated_show_value_hack (struct ui_file *ignore_file,
95 int ignore_from_tty,
96 struct cmd_list_element *c,
97 const char *value)
98 {
99 /* If there's no command or value, don't try to print it out. */
100 if (c == NULL || value == NULL)
101 return;
102 /* Print doc minus "show" at start. */
103 print_doc_line (gdb_stdout, c->doc + 5);
104 switch (c->var_type)
105 {
106 case var_string:
107 case var_string_noescape:
108 case var_optional_filename:
109 case var_filename:
110 case var_enum:
111 printf_filtered ((" is \"%s\".\n"), value);
112 break;
113 default:
114 printf_filtered ((" is %s.\n"), value);
115 break;
116 }
117 }
118
119 /* Do a "set" or "show" command. ARG is NULL if no argument, or the
120 text of the argument, and FROM_TTY is nonzero if this command is
121 being entered directly by the user (i.e. these are just like any
122 other command). C is the command list element for the command. */
123
124 void
125 do_setshow_command (char *arg, int from_tty, struct cmd_list_element *c)
126 {
127 struct ui_out *uiout = current_uiout;
128
129 if (c->type == set_cmd)
130 {
131 switch (c->var_type)
132 {
133 case var_string:
134 {
135 char *new;
136 char *p;
137 char *q;
138 int ch;
139
140 if (arg == NULL)
141 arg = "";
142 new = (char *) xmalloc (strlen (arg) + 2);
143 p = arg;
144 q = new;
145 while ((ch = *p++) != '\000')
146 {
147 if (ch == '\\')
148 {
149 /* \ at end of argument is used after spaces
150 so they won't be lost. */
151 /* This is obsolete now that we no longer strip
152 trailing whitespace and actually, the backslash
153 didn't get here in my test, readline or
154 something did something funky with a backslash
155 right before a newline. */
156 if (*p == 0)
157 break;
158 ch = parse_escape (get_current_arch (), &p);
159 if (ch == 0)
160 break; /* C loses */
161 else if (ch > 0)
162 *q++ = ch;
163 }
164 else
165 *q++ = ch;
166 }
167 #if 0
168 if (*(p - 1) != '\\')
169 *q++ = ' ';
170 #endif
171 *q++ = '\0';
172 new = (char *) xrealloc (new, q - new);
173 xfree (*(char **) c->var);
174 *(char **) c->var = new;
175 }
176 break;
177 case var_string_noescape:
178 if (arg == NULL)
179 arg = "";
180 xfree (*(char **) c->var);
181 *(char **) c->var = xstrdup (arg);
182 break;
183 case var_filename:
184 if (arg == NULL)
185 error_no_arg (_("filename to set it to."));
186 /* FALLTHROUGH */
187 case var_optional_filename:
188 xfree (*(char **) c->var);
189
190 if (arg != NULL)
191 {
192 /* Clear trailing whitespace of filename. */
193 char *ptr = arg + strlen (arg) - 1;
194
195 while (ptr >= arg && (*ptr == ' ' || *ptr == '\t'))
196 ptr--;
197 *(ptr + 1) = '\0';
198
199 *(char **) c->var = tilde_expand (arg);
200 }
201 else
202 *(char **) c->var = xstrdup ("");
203 break;
204 case var_boolean:
205 *(int *) c->var = parse_binary_operation (arg);
206 break;
207 case var_auto_boolean:
208 *(enum auto_boolean *) c->var = parse_auto_binary_operation (arg);
209 break;
210 case var_uinteger:
211 if (arg == NULL)
212 error_no_arg (_("integer to set it to."));
213 *(unsigned int *) c->var = parse_and_eval_long (arg);
214 if (*(unsigned int *) c->var == 0)
215 *(unsigned int *) c->var = UINT_MAX;
216 break;
217 case var_integer:
218 {
219 unsigned int val;
220
221 if (arg == NULL)
222 error_no_arg (_("integer to set it to."));
223 val = parse_and_eval_long (arg);
224 if (val == 0)
225 *(int *) c->var = INT_MAX;
226 else if (val >= INT_MAX)
227 error (_("integer %u out of range"), val);
228 else
229 *(int *) c->var = val;
230 break;
231 }
232 case var_zinteger:
233 if (arg == NULL)
234 error_no_arg (_("integer to set it to."));
235 *(int *) c->var = parse_and_eval_long (arg);
236 break;
237 case var_zuinteger:
238 if (arg == NULL)
239 error_no_arg (_("integer to set it to."));
240 *(unsigned int *) c->var = parse_and_eval_long (arg);
241 break;
242 case var_enum:
243 {
244 int i;
245 int len;
246 int nmatches;
247 const char *match = NULL;
248 char *p;
249
250 /* If no argument was supplied, print an informative error
251 message. */
252 if (arg == NULL)
253 {
254 char *msg;
255 int msg_len = 0;
256
257 for (i = 0; c->enums[i]; i++)
258 msg_len += strlen (c->enums[i]) + 2;
259
260 msg = xmalloc (msg_len);
261 *msg = '\0';
262 make_cleanup (xfree, msg);
263
264 for (i = 0; c->enums[i]; i++)
265 {
266 if (i != 0)
267 strcat (msg, ", ");
268 strcat (msg, c->enums[i]);
269 }
270 error (_("Requires an argument. Valid arguments are %s."),
271 msg);
272 }
273
274 p = strchr (arg, ' ');
275
276 if (p)
277 len = p - arg;
278 else
279 len = strlen (arg);
280
281 nmatches = 0;
282 for (i = 0; c->enums[i]; i++)
283 if (strncmp (arg, c->enums[i], len) == 0)
284 {
285 if (c->enums[i][len] == '\0')
286 {
287 match = c->enums[i];
288 nmatches = 1;
289 break; /* Exact match. */
290 }
291 else
292 {
293 match = c->enums[i];
294 nmatches++;
295 }
296 }
297
298 if (nmatches <= 0)
299 error (_("Undefined item: \"%s\"."), arg);
300
301 if (nmatches > 1)
302 error (_("Ambiguous item \"%s\"."), arg);
303
304 *(const char **) c->var = match;
305 }
306 break;
307 default:
308 error (_("gdb internal error: bad var_type in do_setshow_command"));
309 }
310 }
311 else if (c->type == show_cmd)
312 {
313 struct cleanup *old_chain;
314 struct ui_file *stb;
315
316 stb = mem_fileopen ();
317 old_chain = make_cleanup_ui_file_delete (stb);
318
319 /* Possibly call the pre hook. */
320 if (c->pre_show_hook)
321 (c->pre_show_hook) (c);
322
323 switch (c->var_type)
324 {
325 case var_string:
326 if (*(char **) c->var)
327 fputstr_filtered (*(char **) c->var, '"', stb);
328 break;
329 case var_string_noescape:
330 case var_optional_filename:
331 case var_filename:
332 case var_enum:
333 if (*(char **) c->var)
334 fputs_filtered (*(char **) c->var, stb);
335 break;
336 case var_boolean:
337 fputs_filtered (*(int *) c->var ? "on" : "off", stb);
338 break;
339 case var_auto_boolean:
340 switch (*(enum auto_boolean*) c->var)
341 {
342 case AUTO_BOOLEAN_TRUE:
343 fputs_filtered ("on", stb);
344 break;
345 case AUTO_BOOLEAN_FALSE:
346 fputs_filtered ("off", stb);
347 break;
348 case AUTO_BOOLEAN_AUTO:
349 fputs_filtered ("auto", stb);
350 break;
351 default:
352 internal_error (__FILE__, __LINE__,
353 _("do_setshow_command: "
354 "invalid var_auto_boolean"));
355 break;
356 }
357 break;
358 case var_uinteger:
359 case var_zuinteger:
360 if (c->var_type == var_uinteger
361 && *(unsigned int *) c->var == UINT_MAX)
362 fputs_filtered ("unlimited", stb);
363 else
364 fprintf_filtered (stb, "%u", *(unsigned int *) c->var);
365 break;
366 case var_integer:
367 case var_zinteger:
368 if (c->var_type == var_integer
369 && *(int *) c->var == INT_MAX)
370 fputs_filtered ("unlimited", stb);
371 else
372 fprintf_filtered (stb, "%d", *(int *) c->var);
373 break;
374
375 default:
376 error (_("gdb internal error: bad var_type in do_setshow_command"));
377 }
378
379
380 /* FIXME: cagney/2005-02-10: Need to split this in half: code to
381 convert the value into a string (esentially the above); and
382 code to print the value out. For the latter there should be
383 MI and CLI specific versions. */
384
385 if (ui_out_is_mi_like_p (uiout))
386 ui_out_field_stream (uiout, "value", stb);
387 else
388 {
389 char *value = ui_file_xstrdup (stb, NULL);
390
391 make_cleanup (xfree, value);
392 if (c->show_value_func != NULL)
393 c->show_value_func (gdb_stdout, from_tty, c, value);
394 else
395 deprecated_show_value_hack (gdb_stdout, from_tty, c, value);
396 }
397 do_cleanups (old_chain);
398 }
399 else
400 error (_("gdb internal error: bad cmd_type in do_setshow_command"));
401 c->func (c, NULL, from_tty);
402 if (c->type == set_cmd && deprecated_set_hook)
403 deprecated_set_hook (c);
404 }
405
406 /* Show all the settings in a list of show commands. */
407
408 void
409 cmd_show_list (struct cmd_list_element *list, int from_tty, char *prefix)
410 {
411 struct cleanup *showlist_chain;
412 struct ui_out *uiout = current_uiout;
413
414 showlist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "showlist");
415 for (; list != NULL; list = list->next)
416 {
417 /* If we find a prefix, run its list, prefixing our output by its
418 prefix (with "show " skipped). */
419 if (list->prefixlist && !list->abbrev_flag)
420 {
421 struct cleanup *optionlist_chain
422 = make_cleanup_ui_out_tuple_begin_end (uiout, "optionlist");
423 char *new_prefix = strstr (list->prefixname, "show ") + 5;
424
425 if (ui_out_is_mi_like_p (uiout))
426 ui_out_field_string (uiout, "prefix", new_prefix);
427 cmd_show_list (*list->prefixlist, from_tty, new_prefix);
428 /* Close the tuple. */
429 do_cleanups (optionlist_chain);
430 }
431 else
432 {
433 if (list->class != no_set_class)
434 {
435 struct cleanup *option_chain
436 = make_cleanup_ui_out_tuple_begin_end (uiout, "option");
437
438 ui_out_text (uiout, prefix);
439 ui_out_field_string (uiout, "name", list->name);
440 ui_out_text (uiout, ": ");
441 if (list->type == show_cmd)
442 do_setshow_command ((char *) NULL, from_tty, list);
443 else
444 cmd_func (list, NULL, from_tty);
445 /* Close the tuple. */
446 do_cleanups (option_chain);
447 }
448 }
449 }
450 /* Close the tuple. */
451 do_cleanups (showlist_chain);
452 }
453