Commit | Line | Data |
---|---|---|
d318976c | 1 | /* Handle set and show GDB commands. |
8926118c | 2 | |
0b302171 | 3 | Copyright (c) 2000-2003, 2007-2012 Free Software Foundation, Inc. |
d318976c FN |
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 | |
a9762ec7 | 7 | the Free Software Foundation; either version 3 of the License, or |
d318976c FN |
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 | |
a9762ec7 | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
d318976c FN |
17 | |
18 | #include "defs.h" | |
dbda9972 | 19 | #include "readline/tilde.h" |
d318976c FN |
20 | #include "value.h" |
21 | #include <ctype.h> | |
d318976c | 22 | #include "gdb_string.h" |
f870a310 | 23 | #include "arch-utils.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 | ||
ebcd3b23 | 31 | /* Prototypes for local functions. */ |
d318976c FN |
32 | |
33 | static int parse_binary_operation (char *); | |
34 | ||
d318976c | 35 | \f |
7f19b9a2 | 36 | static enum auto_boolean |
d318976c FN |
37 | parse_auto_binary_operation (const char *arg) |
38 | { | |
39 | if (arg != NULL && *arg != '\0') | |
40 | { | |
41 | int length = strlen (arg); | |
cdb27c12 | 42 | |
d318976c FN |
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) | |
7f19b9a2 | 49 | return AUTO_BOOLEAN_TRUE; |
d318976c FN |
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) | |
7f19b9a2 | 54 | return AUTO_BOOLEAN_FALSE; |
d318976c FN |
55 | else if (strncmp (arg, "auto", length) == 0 |
56 | || (strncmp (arg, "-1", length) == 0 && length > 1)) | |
7f19b9a2 | 57 | return AUTO_BOOLEAN_AUTO; |
d318976c | 58 | } |
8a3fe4f8 | 59 | error (_("\"on\", \"off\" or \"auto\" expected.")); |
ebcd3b23 | 60 | return AUTO_BOOLEAN_AUTO; /* Pacify GCC. */ |
d318976c FN |
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 | { | |
8a3fe4f8 | 88 | error (_("\"on\" or \"off\" expected.")); |
d318976c FN |
89 | return 0; |
90 | } | |
91 | } | |
92 | \f | |
08546159 AC |
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 | { | |
4d28ad1e AC |
99 | /* If there's no command or value, don't try to print it out. */ |
100 | if (c == NULL || value == NULL) | |
101 | return; | |
08546159 AC |
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: | |
b4b4ac0b | 108 | case var_optional_filename: |
08546159 AC |
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 | ||
ebcd3b23 MS |
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. */ | |
d318976c FN |
123 | |
124 | void | |
125 | do_setshow_command (char *arg, int from_tty, struct cmd_list_element *c) | |
126 | { | |
79a45e25 PA |
127 | struct ui_out *uiout = current_uiout; |
128 | ||
d318976c FN |
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; | |
f870a310 | 158 | ch = parse_escape (get_current_arch (), &p); |
d318976c FN |
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); | |
c24343e2 | 173 | xfree (*(char **) c->var); |
d318976c FN |
174 | *(char **) c->var = new; |
175 | } | |
176 | break; | |
177 | case var_string_noescape: | |
178 | if (arg == NULL) | |
179 | arg = ""; | |
c24343e2 | 180 | xfree (*(char **) c->var); |
1b36a34b | 181 | *(char **) c->var = xstrdup (arg); |
d318976c | 182 | break; |
525226b5 AC |
183 | case var_filename: |
184 | if (arg == NULL) | |
185 | error_no_arg (_("filename to set it to.")); | |
6ace3df1 YQ |
186 | /* FALLTHROUGH */ |
187 | case var_optional_filename: | |
c24343e2 | 188 | xfree (*(char **) c->var); |
cdb27c12 | 189 | |
6ace3df1 YQ |
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 (""); | |
d318976c FN |
203 | break; |
204 | case var_boolean: | |
205 | *(int *) c->var = parse_binary_operation (arg); | |
206 | break; | |
207 | case var_auto_boolean: | |
7f19b9a2 | 208 | *(enum auto_boolean *) c->var = parse_auto_binary_operation (arg); |
d318976c FN |
209 | break; |
210 | case var_uinteger: | |
e2d12c21 | 211 | case var_zuinteger: |
d318976c | 212 | if (arg == NULL) |
e2e0b3e5 | 213 | error_no_arg (_("integer to set it to.")); |
d318976c | 214 | *(unsigned int *) c->var = parse_and_eval_long (arg); |
e2d12c21 | 215 | if (c->var_type == var_uinteger && *(unsigned int *) c->var == 0) |
d318976c FN |
216 | *(unsigned int *) c->var = UINT_MAX; |
217 | break; | |
218 | case var_integer: | |
e2d12c21 | 219 | case var_zinteger: |
d318976c FN |
220 | { |
221 | unsigned int val; | |
cdb27c12 | 222 | |
d318976c | 223 | if (arg == NULL) |
e2e0b3e5 | 224 | error_no_arg (_("integer to set it to.")); |
d318976c | 225 | val = parse_and_eval_long (arg); |
e2d12c21 | 226 | if (val == 0 && c->var_type == var_integer) |
d318976c FN |
227 | *(int *) c->var = INT_MAX; |
228 | else if (val >= INT_MAX) | |
8a3fe4f8 | 229 | error (_("integer %u out of range"), val); |
d318976c FN |
230 | else |
231 | *(int *) c->var = val; | |
232 | break; | |
233 | } | |
d318976c FN |
234 | case var_enum: |
235 | { | |
236 | int i; | |
237 | int len; | |
238 | int nmatches; | |
239 | const char *match = NULL; | |
240 | char *p; | |
241 | ||
ebcd3b23 MS |
242 | /* If no argument was supplied, print an informative error |
243 | message. */ | |
d318976c FN |
244 | if (arg == NULL) |
245 | { | |
f0704234 UW |
246 | char *msg; |
247 | int msg_len = 0; | |
cdb27c12 | 248 | |
f0704234 UW |
249 | for (i = 0; c->enums[i]; i++) |
250 | msg_len += strlen (c->enums[i]) + 2; | |
251 | ||
252 | msg = xmalloc (msg_len); | |
253 | *msg = '\0'; | |
254 | make_cleanup (xfree, msg); | |
255 | ||
d318976c FN |
256 | for (i = 0; c->enums[i]; i++) |
257 | { | |
258 | if (i != 0) | |
259 | strcat (msg, ", "); | |
260 | strcat (msg, c->enums[i]); | |
261 | } | |
ebcd3b23 MS |
262 | error (_("Requires an argument. Valid arguments are %s."), |
263 | msg); | |
d318976c FN |
264 | } |
265 | ||
266 | p = strchr (arg, ' '); | |
267 | ||
268 | if (p) | |
269 | len = p - arg; | |
270 | else | |
271 | len = strlen (arg); | |
272 | ||
273 | nmatches = 0; | |
274 | for (i = 0; c->enums[i]; i++) | |
275 | if (strncmp (arg, c->enums[i], len) == 0) | |
276 | { | |
277 | if (c->enums[i][len] == '\0') | |
278 | { | |
279 | match = c->enums[i]; | |
280 | nmatches = 1; | |
ebcd3b23 | 281 | break; /* Exact match. */ |
d318976c FN |
282 | } |
283 | else | |
284 | { | |
285 | match = c->enums[i]; | |
286 | nmatches++; | |
287 | } | |
288 | } | |
289 | ||
290 | if (nmatches <= 0) | |
8a3fe4f8 | 291 | error (_("Undefined item: \"%s\"."), arg); |
d318976c FN |
292 | |
293 | if (nmatches > 1) | |
8a3fe4f8 | 294 | error (_("Ambiguous item \"%s\"."), arg); |
d318976c FN |
295 | |
296 | *(const char **) c->var = match; | |
297 | } | |
298 | break; | |
299 | default: | |
8a3fe4f8 | 300 | error (_("gdb internal error: bad var_type in do_setshow_command")); |
d318976c FN |
301 | } |
302 | } | |
303 | else if (c->type == show_cmd) | |
304 | { | |
d318976c | 305 | struct cleanup *old_chain; |
f99d8bf4 | 306 | struct ui_file *stb; |
d318976c | 307 | |
f99d8bf4 PA |
308 | stb = mem_fileopen (); |
309 | old_chain = make_cleanup_ui_file_delete (stb); | |
d318976c | 310 | |
552c04a7 TT |
311 | /* Possibly call the pre hook. */ |
312 | if (c->pre_show_hook) | |
313 | (c->pre_show_hook) (c); | |
314 | ||
d318976c FN |
315 | switch (c->var_type) |
316 | { | |
317 | case var_string: | |
3b113db7 | 318 | if (*(char **) c->var) |
f99d8bf4 | 319 | fputstr_filtered (*(char **) c->var, '"', stb); |
d318976c FN |
320 | break; |
321 | case var_string_noescape: | |
b4b4ac0b | 322 | case var_optional_filename: |
d318976c FN |
323 | case var_filename: |
324 | case var_enum: | |
325 | if (*(char **) c->var) | |
f99d8bf4 | 326 | fputs_filtered (*(char **) c->var, stb); |
d318976c FN |
327 | break; |
328 | case var_boolean: | |
f99d8bf4 | 329 | fputs_filtered (*(int *) c->var ? "on" : "off", stb); |
d318976c FN |
330 | break; |
331 | case var_auto_boolean: | |
7f19b9a2 | 332 | switch (*(enum auto_boolean*) c->var) |
d318976c | 333 | { |
7f19b9a2 | 334 | case AUTO_BOOLEAN_TRUE: |
f99d8bf4 | 335 | fputs_filtered ("on", stb); |
d318976c | 336 | break; |
7f19b9a2 | 337 | case AUTO_BOOLEAN_FALSE: |
f99d8bf4 | 338 | fputs_filtered ("off", stb); |
d318976c | 339 | break; |
7f19b9a2 | 340 | case AUTO_BOOLEAN_AUTO: |
f99d8bf4 | 341 | fputs_filtered ("auto", stb); |
d318976c FN |
342 | break; |
343 | default: | |
8e65ff28 | 344 | internal_error (__FILE__, __LINE__, |
9a2b4c1b MS |
345 | _("do_setshow_command: " |
346 | "invalid var_auto_boolean")); | |
d318976c FN |
347 | break; |
348 | } | |
349 | break; | |
350 | case var_uinteger: | |
1e8fb976 | 351 | case var_zuinteger: |
a40a111f AB |
352 | if (c->var_type == var_uinteger |
353 | && *(unsigned int *) c->var == UINT_MAX) | |
f99d8bf4 | 354 | fputs_filtered ("unlimited", stb); |
a40a111f | 355 | else |
f99d8bf4 | 356 | fprintf_filtered (stb, "%u", *(unsigned int *) c->var); |
d318976c FN |
357 | break; |
358 | case var_integer: | |
a40a111f AB |
359 | case var_zinteger: |
360 | if (c->var_type == var_integer | |
361 | && *(int *) c->var == INT_MAX) | |
f99d8bf4 | 362 | fputs_filtered ("unlimited", stb); |
d318976c | 363 | else |
f99d8bf4 | 364 | fprintf_filtered (stb, "%d", *(int *) c->var); |
d318976c FN |
365 | break; |
366 | ||
367 | default: | |
8a3fe4f8 | 368 | error (_("gdb internal error: bad var_type in do_setshow_command")); |
d318976c | 369 | } |
899506a8 AC |
370 | |
371 | ||
372 | /* FIXME: cagney/2005-02-10: Need to split this in half: code to | |
373 | convert the value into a string (esentially the above); and | |
374 | code to print the value out. For the latter there should be | |
375 | MI and CLI specific versions. */ | |
376 | ||
377 | if (ui_out_is_mi_like_p (uiout)) | |
378 | ui_out_field_stream (uiout, "value", stb); | |
08546159 | 379 | else |
335cca0d | 380 | { |
f99d8bf4 | 381 | char *value = ui_file_xstrdup (stb, NULL); |
cdb27c12 | 382 | |
335cca0d | 383 | make_cleanup (xfree, value); |
08546159 AC |
384 | if (c->show_value_func != NULL) |
385 | c->show_value_func (gdb_stdout, from_tty, c, value); | |
386 | else | |
387 | deprecated_show_value_hack (gdb_stdout, from_tty, c, value); | |
899506a8 | 388 | } |
d318976c | 389 | do_cleanups (old_chain); |
d318976c FN |
390 | } |
391 | else | |
8a3fe4f8 | 392 | error (_("gdb internal error: bad cmd_type in do_setshow_command")); |
9f60d481 | 393 | c->func (c, NULL, from_tty); |
9a4105ab AC |
394 | if (c->type == set_cmd && deprecated_set_hook) |
395 | deprecated_set_hook (c); | |
d318976c FN |
396 | } |
397 | ||
398 | /* Show all the settings in a list of show commands. */ | |
399 | ||
400 | void | |
401 | cmd_show_list (struct cmd_list_element *list, int from_tty, char *prefix) | |
402 | { | |
3b31d625 | 403 | struct cleanup *showlist_chain; |
79a45e25 | 404 | struct ui_out *uiout = current_uiout; |
3b31d625 EZ |
405 | |
406 | showlist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "showlist"); | |
d318976c FN |
407 | for (; list != NULL; list = list->next) |
408 | { | |
409 | /* If we find a prefix, run its list, prefixing our output by its | |
410 | prefix (with "show " skipped). */ | |
d318976c FN |
411 | if (list->prefixlist && !list->abbrev_flag) |
412 | { | |
3b31d625 EZ |
413 | struct cleanup *optionlist_chain |
414 | = make_cleanup_ui_out_tuple_begin_end (uiout, "optionlist"); | |
37fc812e | 415 | char *new_prefix = strstr (list->prefixname, "show ") + 5; |
cdb27c12 | 416 | |
37fc812e DJ |
417 | if (ui_out_is_mi_like_p (uiout)) |
418 | ui_out_field_string (uiout, "prefix", new_prefix); | |
419 | cmd_show_list (*list->prefixlist, from_tty, new_prefix); | |
3b31d625 EZ |
420 | /* Close the tuple. */ |
421 | do_cleanups (optionlist_chain); | |
d318976c | 422 | } |
427c3a89 | 423 | else |
d318976c | 424 | { |
db5f229b MS |
425 | if (list->class != no_set_class) |
426 | { | |
427 | struct cleanup *option_chain | |
428 | = make_cleanup_ui_out_tuple_begin_end (uiout, "option"); | |
429 | ||
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 | } | |
d318976c | 440 | } |
d318976c | 441 | } |
3b31d625 EZ |
442 | /* Close the tuple. */ |
443 | do_cleanups (showlist_chain); | |
d318976c FN |
444 | } |
445 |