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