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