d8ec10025c1814250fbc517fa8ce9a5b7b9d3faa
[deliverable/binutils-gdb.git] / gdb / cli / cli-setshow.c
1 /* Handle set and show GDB commands.
2
3 Copyright (c) 2000, 2001, 2002, 2003, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18
19 #include "defs.h"
20 #include "readline/tilde.h"
21 #include "value.h"
22 #include <ctype.h>
23 #include "gdb_string.h"
24 #include "arch-utils.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 \f
37 static enum auto_boolean
38 parse_auto_binary_operation (const char *arg)
39 {
40 if (arg != NULL && *arg != '\0')
41 {
42 int length = strlen (arg);
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 text
120 of the argument, and FROM_TTY is nonzero if this command is being entered
121 directly by the user (i.e. these are just like any other
122 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 if (c->type == set_cmd)
128 {
129 switch (c->var_type)
130 {
131 case var_string:
132 {
133 char *new;
134 char *p;
135 char *q;
136 int ch;
137
138 if (arg == NULL)
139 arg = "";
140 new = (char *) xmalloc (strlen (arg) + 2);
141 p = arg;
142 q = new;
143 while ((ch = *p++) != '\000')
144 {
145 if (ch == '\\')
146 {
147 /* \ at end of argument is used after spaces
148 so they won't be lost. */
149 /* This is obsolete now that we no longer strip
150 trailing whitespace and actually, the backslash
151 didn't get here in my test, readline or
152 something did something funky with a backslash
153 right before a newline. */
154 if (*p == 0)
155 break;
156 ch = parse_escape (get_current_arch (), &p);
157 if (ch == 0)
158 break; /* C loses */
159 else if (ch > 0)
160 *q++ = ch;
161 }
162 else
163 *q++ = ch;
164 }
165 #if 0
166 if (*(p - 1) != '\\')
167 *q++ = ' ';
168 #endif
169 *q++ = '\0';
170 new = (char *) xrealloc (new, q - new);
171 if (*(char **) c->var != NULL)
172 xfree (*(char **) c->var);
173 *(char **) c->var = new;
174 }
175 break;
176 case var_string_noescape:
177 if (arg == NULL)
178 arg = "";
179 if (*(char **) c->var != NULL)
180 xfree (*(char **) c->var);
181 *(char **) c->var = xstrdup (arg);
182 break;
183 case var_optional_filename:
184 if (arg == NULL)
185 arg = "";
186 if (*(char **) c->var != NULL)
187 xfree (*(char **) c->var);
188 *(char **) c->var = xstrdup (arg);
189 break;
190 case var_filename:
191 if (arg == NULL)
192 error_no_arg (_("filename to set it to."));
193 if (*(char **) c->var != NULL)
194 xfree (*(char **) c->var);
195 {
196 /* Clear trailing whitespace of filename. */
197 char *ptr = arg + strlen (arg) - 1;
198 while (ptr >= arg && (*ptr == ' ' || *ptr == '\t'))
199 ptr--;
200 *(ptr + 1) = '\0';
201 }
202 *(char **) c->var = tilde_expand (arg);
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 if (arg == NULL)
221 error_no_arg (_("integer to set it to."));
222 val = parse_and_eval_long (arg);
223 if (val == 0)
224 *(int *) c->var = INT_MAX;
225 else if (val >= INT_MAX)
226 error (_("integer %u out of range"), val);
227 else
228 *(int *) c->var = val;
229 break;
230 }
231 case var_zinteger:
232 if (arg == NULL)
233 error_no_arg (_("integer to set it to."));
234 *(int *) c->var = parse_and_eval_long (arg);
235 break;
236 case var_zuinteger:
237 if (arg == NULL)
238 error_no_arg (_("integer to set it to."));
239 *(unsigned int *) c->var = parse_and_eval_long (arg);
240 break;
241 case var_enum:
242 {
243 int i;
244 int len;
245 int nmatches;
246 const char *match = NULL;
247 char *p;
248
249 /* if no argument was supplied, print an informative error message */
250 if (arg == NULL)
251 {
252 char *msg;
253 int msg_len = 0;
254 for (i = 0; c->enums[i]; i++)
255 msg_len += strlen (c->enums[i]) + 2;
256
257 msg = xmalloc (msg_len);
258 *msg = '\0';
259 make_cleanup (xfree, msg);
260
261 for (i = 0; c->enums[i]; i++)
262 {
263 if (i != 0)
264 strcat (msg, ", ");
265 strcat (msg, c->enums[i]);
266 }
267 error (_("Requires an argument. Valid arguments are %s."), msg);
268 }
269
270 p = strchr (arg, ' ');
271
272 if (p)
273 len = p - arg;
274 else
275 len = strlen (arg);
276
277 nmatches = 0;
278 for (i = 0; c->enums[i]; i++)
279 if (strncmp (arg, c->enums[i], len) == 0)
280 {
281 if (c->enums[i][len] == '\0')
282 {
283 match = c->enums[i];
284 nmatches = 1;
285 break; /* exact match. */
286 }
287 else
288 {
289 match = c->enums[i];
290 nmatches++;
291 }
292 }
293
294 if (nmatches <= 0)
295 error (_("Undefined item: \"%s\"."), arg);
296
297 if (nmatches > 1)
298 error (_("Ambiguous item \"%s\"."), arg);
299
300 *(const char **) c->var = match;
301 }
302 break;
303 default:
304 error (_("gdb internal error: bad var_type in do_setshow_command"));
305 }
306 }
307 else if (c->type == show_cmd)
308 {
309 struct cleanup *old_chain;
310 struct ui_stream *stb;
311
312 stb = ui_out_stream_new (uiout);
313 old_chain = make_cleanup_ui_out_stream_delete (stb);
314
315 /* Possibly call the pre hook. */
316 if (c->pre_show_hook)
317 (c->pre_show_hook) (c);
318
319 switch (c->var_type)
320 {
321 case var_string:
322 if (*(char **) c->var)
323 fputstr_filtered (*(char **) c->var, '"', stb->stream);
324 break;
325 case var_string_noescape:
326 case var_optional_filename:
327 case var_filename:
328 case var_enum:
329 if (*(char **) c->var)
330 fputs_filtered (*(char **) c->var, stb->stream);
331 break;
332 case var_boolean:
333 fputs_filtered (*(int *) c->var ? "on" : "off", stb->stream);
334 break;
335 case var_auto_boolean:
336 switch (*(enum auto_boolean*) c->var)
337 {
338 case AUTO_BOOLEAN_TRUE:
339 fputs_filtered ("on", stb->stream);
340 break;
341 case AUTO_BOOLEAN_FALSE:
342 fputs_filtered ("off", stb->stream);
343 break;
344 case AUTO_BOOLEAN_AUTO:
345 fputs_filtered ("auto", stb->stream);
346 break;
347 default:
348 internal_error (__FILE__, __LINE__,
349 _("do_setshow_command: invalid var_auto_boolean"));
350 break;
351 }
352 break;
353 case var_uinteger:
354 if (*(unsigned int *) c->var == UINT_MAX)
355 {
356 fputs_filtered ("unlimited", stb->stream);
357 break;
358 }
359 /* else fall through */
360 case var_zuinteger:
361 case var_zinteger:
362 fprintf_filtered (stb->stream, "%u", *(unsigned int *) c->var);
363 break;
364 case var_integer:
365 if (*(int *) c->var == INT_MAX)
366 {
367 fputs_filtered ("unlimited", stb->stream);
368 }
369 else
370 fprintf_filtered (stb->stream, "%d", *(int *) c->var);
371 break;
372
373 default:
374 error (_("gdb internal error: bad var_type in do_setshow_command"));
375 }
376
377
378 /* FIXME: cagney/2005-02-10: Need to split this in half: code to
379 convert the value into a string (esentially the above); and
380 code to print the value out. For the latter there should be
381 MI and CLI specific versions. */
382
383 if (ui_out_is_mi_like_p (uiout))
384 ui_out_field_stream (uiout, "value", stb);
385 else
386 {
387 char *value = ui_file_xstrdup (stb->stream, NULL);
388 make_cleanup (xfree, value);
389 if (c->show_value_func != NULL)
390 c->show_value_func (gdb_stdout, from_tty, c, value);
391 else
392 deprecated_show_value_hack (gdb_stdout, from_tty, c, value);
393 }
394 do_cleanups (old_chain);
395 }
396 else
397 error (_("gdb internal error: bad cmd_type in do_setshow_command"));
398 c->func (c, NULL, from_tty);
399 if (c->type == set_cmd && deprecated_set_hook)
400 deprecated_set_hook (c);
401 }
402
403 /* Show all the settings in a list of show commands. */
404
405 void
406 cmd_show_list (struct cmd_list_element *list, int from_tty, char *prefix)
407 {
408 struct cleanup *showlist_chain;
409
410 showlist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "showlist");
411 for (; list != NULL; list = list->next)
412 {
413 /* If we find a prefix, run its list, prefixing our output by its
414 prefix (with "show " skipped). */
415 if (list->prefixlist && !list->abbrev_flag)
416 {
417 struct cleanup *optionlist_chain
418 = make_cleanup_ui_out_tuple_begin_end (uiout, "optionlist");
419 char *new_prefix = strstr (list->prefixname, "show ") + 5;
420 if (ui_out_is_mi_like_p (uiout))
421 ui_out_field_string (uiout, "prefix", new_prefix);
422 cmd_show_list (*list->prefixlist, from_tty, new_prefix);
423 /* Close the tuple. */
424 do_cleanups (optionlist_chain);
425 }
426 else
427 {
428 struct cleanup *option_chain
429 = make_cleanup_ui_out_tuple_begin_end (uiout, "option");
430 ui_out_text (uiout, prefix);
431 ui_out_field_string (uiout, "name", list->name);
432 ui_out_text (uiout, ": ");
433 if (list->type == show_cmd)
434 do_setshow_command ((char *) NULL, from_tty, list);
435 else
436 cmd_func (list, NULL, from_tty);
437 /* Close the tuple. */
438 do_cleanups (option_chain);
439 }
440 }
441 /* Close the tuple. */
442 do_cleanups (showlist_chain);
443 }
444
This page took 0.039689 seconds and 4 git commands to generate.