Home | History | Annotate | Line # | Download | only in cli
cli-setshow.c revision 1.7
      1  1.1  christos /* Handle set and show GDB commands.
      2  1.1  christos 
      3  1.7  christos    Copyright (C) 2000-2017 Free Software Foundation, Inc.
      4  1.1  christos 
      5  1.1  christos    This program is free software; you can redistribute it and/or modify
      6  1.1  christos    it under the terms of the GNU General Public License as published by
      7  1.1  christos    the Free Software Foundation; either version 3 of the License, or
      8  1.1  christos    (at your option) any later version.
      9  1.1  christos 
     10  1.1  christos    This program is distributed in the hope that it will be useful,
     11  1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13  1.1  christos    GNU General Public License for more details.
     14  1.1  christos 
     15  1.1  christos    You should have received a copy of the GNU General Public License
     16  1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     17  1.1  christos 
     18  1.1  christos #include "defs.h"
     19  1.1  christos #include "readline/tilde.h"
     20  1.1  christos #include "value.h"
     21  1.1  christos #include <ctype.h>
     22  1.1  christos #include "arch-utils.h"
     23  1.1  christos #include "observer.h"
     24  1.1  christos 
     25  1.1  christos #include "ui-out.h"
     26  1.1  christos 
     27  1.1  christos #include "cli/cli-decode.h"
     28  1.1  christos #include "cli/cli-cmds.h"
     29  1.1  christos #include "cli/cli-setshow.h"
     30  1.1  christos #include "cli/cli-utils.h"
     31  1.1  christos 
     32  1.1  christos /* Return true if the change of command parameter should be notified.  */
     33  1.1  christos 
     34  1.1  christos static int
     35  1.1  christos notify_command_param_changed_p (int param_changed, struct cmd_list_element *c)
     36  1.1  christos {
     37  1.1  christos   if (param_changed == 0)
     38  1.1  christos     return 0;
     39  1.1  christos 
     40  1.5  christos   if (c->theclass == class_maintenance || c->theclass == class_deprecated
     41  1.5  christos       || c->theclass == class_obscure)
     42  1.1  christos     return 0;
     43  1.1  christos 
     44  1.1  christos   return 1;
     45  1.1  christos }
     46  1.1  christos 
     47  1.1  christos 
     48  1.1  christos static enum auto_boolean
     50  1.1  christos parse_auto_binary_operation (const char *arg)
     51  1.1  christos {
     52  1.1  christos   if (arg != NULL && *arg != '\0')
     53  1.1  christos     {
     54  1.1  christos       int length = strlen (arg);
     55  1.1  christos 
     56  1.1  christos       while (isspace (arg[length - 1]) && length > 0)
     57  1.1  christos 	length--;
     58  1.1  christos       if (strncmp (arg, "on", length) == 0
     59  1.1  christos 	  || strncmp (arg, "1", length) == 0
     60  1.1  christos 	  || strncmp (arg, "yes", length) == 0
     61  1.1  christos 	  || strncmp (arg, "enable", length) == 0)
     62  1.1  christos 	return AUTO_BOOLEAN_TRUE;
     63  1.1  christos       else if (strncmp (arg, "off", length) == 0
     64  1.1  christos 	       || strncmp (arg, "0", length) == 0
     65  1.1  christos 	       || strncmp (arg, "no", length) == 0
     66  1.1  christos 	       || strncmp (arg, "disable", length) == 0)
     67  1.1  christos 	return AUTO_BOOLEAN_FALSE;
     68  1.1  christos       else if (strncmp (arg, "auto", length) == 0
     69  1.1  christos 	       || (strncmp (arg, "-1", length) == 0 && length > 1))
     70  1.1  christos 	return AUTO_BOOLEAN_AUTO;
     71  1.1  christos     }
     72  1.1  christos   error (_("\"on\", \"off\" or \"auto\" expected."));
     73  1.1  christos   return AUTO_BOOLEAN_AUTO; /* Pacify GCC.  */
     74  1.1  christos }
     75  1.1  christos 
     76  1.1  christos /* See cli-setshow.h.  */
     77  1.1  christos 
     78  1.3  christos int
     79  1.1  christos parse_cli_boolean_value (const char *arg)
     80  1.1  christos {
     81  1.1  christos   int length;
     82  1.1  christos 
     83  1.1  christos   if (!arg || !*arg)
     84  1.1  christos     return 1;
     85  1.1  christos 
     86  1.1  christos   length = strlen (arg);
     87  1.1  christos 
     88  1.1  christos   while (arg[length - 1] == ' ' || arg[length - 1] == '\t')
     89  1.1  christos     length--;
     90  1.1  christos 
     91  1.1  christos   if (strncmp (arg, "on", length) == 0
     92  1.1  christos       || strncmp (arg, "1", length) == 0
     93  1.1  christos       || strncmp (arg, "yes", length) == 0
     94  1.1  christos       || strncmp (arg, "enable", length) == 0)
     95  1.1  christos     return 1;
     96  1.1  christos   else if (strncmp (arg, "off", length) == 0
     97  1.1  christos 	   || strncmp (arg, "0", length) == 0
     98  1.1  christos 	   || strncmp (arg, "no", length) == 0
     99  1.1  christos 	   || strncmp (arg, "disable", length) == 0)
    100  1.1  christos     return 0;
    101  1.1  christos   else
    102  1.1  christos     return -1;
    103  1.1  christos }
    104  1.1  christos 
    105  1.1  christos void
    107  1.1  christos deprecated_show_value_hack (struct ui_file *ignore_file,
    108  1.1  christos 			    int ignore_from_tty,
    109  1.1  christos 			    struct cmd_list_element *c,
    110  1.1  christos 			    const char *value)
    111  1.1  christos {
    112  1.1  christos   /* If there's no command or value, don't try to print it out.  */
    113  1.1  christos   if (c == NULL || value == NULL)
    114  1.1  christos     return;
    115  1.1  christos   /* Print doc minus "show" at start.  */
    116  1.1  christos   print_doc_line (gdb_stdout, c->doc + 5);
    117  1.1  christos   switch (c->var_type)
    118  1.1  christos     {
    119  1.1  christos     case var_string:
    120  1.1  christos     case var_string_noescape:
    121  1.1  christos     case var_optional_filename:
    122  1.1  christos     case var_filename:
    123  1.1  christos     case var_enum:
    124  1.1  christos       printf_filtered ((" is \"%s\".\n"), value);
    125  1.1  christos       break;
    126  1.1  christos     default:
    127  1.1  christos       printf_filtered ((" is %s.\n"), value);
    128  1.1  christos       break;
    129  1.1  christos     }
    130  1.1  christos }
    131  1.1  christos 
    132  1.1  christos /* Returns true if ARG is "unlimited".  */
    133  1.1  christos 
    134  1.1  christos static int
    135  1.1  christos is_unlimited_literal (const char *arg)
    136  1.1  christos {
    137  1.1  christos   size_t len = sizeof ("unlimited") - 1;
    138  1.1  christos 
    139  1.1  christos   arg = skip_spaces_const (arg);
    140  1.1  christos 
    141  1.1  christos   return (strncmp (arg, "unlimited", len) == 0
    142  1.1  christos 	  && (isspace (arg[len]) || arg[len] == '\0'));
    143  1.1  christos }
    144  1.1  christos 
    145  1.1  christos 
    146  1.1  christos /* Do a "set" command.  ARG is NULL if no argument, or the
    147  1.1  christos    text of the argument, and FROM_TTY is nonzero if this command is
    148  1.1  christos    being entered directly by the user (i.e. these are just like any
    149  1.1  christos    other command).  C is the command list element for the command.  */
    150  1.3  christos 
    151  1.1  christos void
    152  1.1  christos do_set_command (const char *arg, int from_tty, struct cmd_list_element *c)
    153  1.1  christos {
    154  1.1  christos   /* A flag to indicate the option is changed or not.  */
    155  1.1  christos   int option_changed = 0;
    156  1.1  christos 
    157  1.1  christos   gdb_assert (c->type == set_cmd);
    158  1.1  christos 
    159  1.1  christos   switch (c->var_type)
    160  1.1  christos     {
    161  1.5  christos     case var_string:
    162  1.1  christos       {
    163  1.1  christos 	char *newobj;
    164  1.1  christos 	const char *p;
    165  1.1  christos 	char *q;
    166  1.1  christos 	int ch;
    167  1.1  christos 
    168  1.5  christos 	if (arg == NULL)
    169  1.1  christos 	  arg = "";
    170  1.5  christos 	newobj = (char *) xmalloc (strlen (arg) + 2);
    171  1.1  christos 	p = arg;
    172  1.1  christos 	q = newobj;
    173  1.1  christos 	while ((ch = *p++) != '\000')
    174  1.1  christos 	  {
    175  1.1  christos 	    if (ch == '\\')
    176  1.1  christos 	      {
    177  1.1  christos 		/* \ at end of argument is used after spaces
    178  1.1  christos 		   so they won't be lost.  */
    179  1.1  christos 		/* This is obsolete now that we no longer strip
    180  1.1  christos 		   trailing whitespace and actually, the backslash
    181  1.1  christos 		   didn't get here in my test, readline or
    182  1.1  christos 		   something did something funky with a backslash
    183  1.1  christos 		   right before a newline.  */
    184  1.1  christos 		if (*p == 0)
    185  1.1  christos 		  break;
    186  1.1  christos 		ch = parse_escape (get_current_arch (), &p);
    187  1.1  christos 		if (ch == 0)
    188  1.1  christos 		  break;	/* C loses */
    189  1.1  christos 		else if (ch > 0)
    190  1.1  christos 		  *q++ = ch;
    191  1.1  christos 	      }
    192  1.1  christos 	    else
    193  1.1  christos 	      *q++ = ch;
    194  1.1  christos 	  }
    195  1.1  christos #if 0
    196  1.1  christos 	if (*(p - 1) != '\\')
    197  1.1  christos 	  *q++ = ' ';
    198  1.5  christos #endif
    199  1.1  christos 	*q++ = '\0';
    200  1.1  christos 	newobj = (char *) xrealloc (newobj, q - newobj);
    201  1.5  christos 
    202  1.1  christos 	if (*(char **) c->var == NULL
    203  1.1  christos 	    || strcmp (*(char **) c->var, newobj) != 0)
    204  1.5  christos 	  {
    205  1.1  christos 	    xfree (*(char **) c->var);
    206  1.1  christos 	    *(char **) c->var = newobj;
    207  1.1  christos 
    208  1.1  christos 	    option_changed = 1;
    209  1.5  christos 	  }
    210  1.1  christos 	else
    211  1.1  christos 	  xfree (newobj);
    212  1.1  christos       }
    213  1.1  christos       break;
    214  1.1  christos     case var_string_noescape:
    215  1.1  christos       if (arg == NULL)
    216  1.1  christos 	arg = "";
    217  1.1  christos 
    218  1.1  christos       if (*(char **) c->var == NULL || strcmp (*(char **) c->var, arg) != 0)
    219  1.1  christos 	{
    220  1.1  christos 	  xfree (*(char **) c->var);
    221  1.1  christos 	  *(char **) c->var = xstrdup (arg);
    222  1.1  christos 
    223  1.1  christos 	  option_changed = 1;
    224  1.1  christos 	}
    225  1.1  christos       break;
    226  1.1  christos     case var_filename:
    227  1.1  christos       if (arg == NULL)
    228  1.1  christos 	error_no_arg (_("filename to set it to."));
    229  1.1  christos       /* FALLTHROUGH */
    230  1.1  christos     case var_optional_filename:
    231  1.1  christos       {
    232  1.1  christos 	char *val = NULL;
    233  1.1  christos 
    234  1.1  christos 	if (arg != NULL)
    235  1.3  christos 	  {
    236  1.3  christos 	    /* Clear trailing whitespace of filename.  */
    237  1.1  christos 	    const char *ptr = arg + strlen (arg) - 1;
    238  1.1  christos 	    char *copy;
    239  1.1  christos 
    240  1.3  christos 	    while (ptr >= arg && (*ptr == ' ' || *ptr == '\t'))
    241  1.1  christos 	      ptr--;
    242  1.3  christos 	    copy = xstrndup (arg, ptr + 1 - arg);
    243  1.3  christos 
    244  1.1  christos 	    val = tilde_expand (copy);
    245  1.1  christos 	    xfree (copy);
    246  1.1  christos 	  }
    247  1.1  christos 	else
    248  1.1  christos 	  val = xstrdup ("");
    249  1.1  christos 
    250  1.1  christos 	if (*(char **) c->var == NULL
    251  1.1  christos 	    || strcmp (*(char **) c->var, val) != 0)
    252  1.1  christos 	  {
    253  1.1  christos 	    xfree (*(char **) c->var);
    254  1.1  christos 	    *(char **) c->var = val;
    255  1.1  christos 
    256  1.1  christos 	    option_changed = 1;
    257  1.1  christos 	  }
    258  1.1  christos 	else
    259  1.1  christos 	  xfree (val);
    260  1.1  christos       }
    261  1.1  christos       break;
    262  1.1  christos     case var_boolean:
    263  1.1  christos       {
    264  1.1  christos 	int val = parse_cli_boolean_value (arg);
    265  1.1  christos 
    266  1.1  christos 	if (val < 0)
    267  1.1  christos 	  error (_("\"on\" or \"off\" expected."));
    268  1.1  christos 	if (val != *(int *) c->var)
    269  1.1  christos 	  {
    270  1.1  christos 	    *(int *) c->var = val;
    271  1.1  christos 
    272  1.1  christos 	    option_changed = 1;
    273  1.1  christos 	  }
    274  1.1  christos       }
    275  1.1  christos       break;
    276  1.1  christos     case var_auto_boolean:
    277  1.1  christos       {
    278  1.1  christos 	enum auto_boolean val = parse_auto_binary_operation (arg);
    279  1.1  christos 
    280  1.1  christos 	if (*(enum auto_boolean *) c->var != val)
    281  1.1  christos 	  {
    282  1.1  christos 	    *(enum auto_boolean *) c->var = val;
    283  1.1  christos 
    284  1.1  christos 	    option_changed = 1;
    285  1.1  christos 	  }
    286  1.1  christos       }
    287  1.1  christos       break;
    288  1.1  christos     case var_uinteger:
    289  1.1  christos     case var_zuinteger:
    290  1.1  christos       {
    291  1.1  christos 	LONGEST val;
    292  1.1  christos 
    293  1.1  christos 	if (arg == NULL)
    294  1.1  christos 	  {
    295  1.1  christos 	    if (c->var_type == var_uinteger)
    296  1.1  christos 	      error_no_arg (_("integer to set it to, or \"unlimited\"."));
    297  1.1  christos 	    else
    298  1.1  christos 	      error_no_arg (_("integer to set it to."));
    299  1.1  christos 	  }
    300  1.1  christos 
    301  1.1  christos 	if (c->var_type == var_uinteger && is_unlimited_literal (arg))
    302  1.1  christos 	  val = 0;
    303  1.1  christos 	else
    304  1.1  christos 	  val = parse_and_eval_long (arg);
    305  1.1  christos 
    306  1.1  christos 	if (c->var_type == var_uinteger && val == 0)
    307  1.1  christos 	  val = UINT_MAX;
    308  1.1  christos 	else if (val < 0
    309  1.1  christos 		 /* For var_uinteger, don't let the user set the value
    310  1.1  christos 		    to UINT_MAX directly, as that exposes an
    311  1.1  christos 		    implementation detail to the user interface.  */
    312  1.1  christos 		 || (c->var_type == var_uinteger && val >= UINT_MAX)
    313  1.1  christos 		 || (c->var_type == var_zuinteger && val > UINT_MAX))
    314  1.1  christos 	  error (_("integer %s out of range"), plongest (val));
    315  1.1  christos 
    316  1.1  christos 	if (*(unsigned int *) c->var != val)
    317  1.1  christos 	  {
    318  1.1  christos 	    *(unsigned int *) c->var = val;
    319  1.1  christos 
    320  1.1  christos 	    option_changed = 1;
    321  1.1  christos 	  }
    322  1.1  christos       }
    323  1.1  christos       break;
    324  1.1  christos     case var_integer:
    325  1.1  christos     case var_zinteger:
    326  1.1  christos       {
    327  1.1  christos 	LONGEST val;
    328  1.1  christos 
    329  1.1  christos 	if (arg == NULL)
    330  1.1  christos 	  {
    331  1.1  christos 	    if (c->var_type == var_integer)
    332  1.1  christos 	      error_no_arg (_("integer to set it to, or \"unlimited\"."));
    333  1.1  christos 	    else
    334  1.1  christos 	      error_no_arg (_("integer to set it to."));
    335  1.1  christos 	  }
    336  1.1  christos 
    337  1.1  christos 	if (c->var_type == var_integer && is_unlimited_literal (arg))
    338  1.1  christos 	  val = 0;
    339  1.1  christos 	else
    340  1.1  christos 	  val = parse_and_eval_long (arg);
    341  1.1  christos 
    342  1.1  christos 	if (val == 0 && c->var_type == var_integer)
    343  1.1  christos 	  val = INT_MAX;
    344  1.1  christos 	else if (val < INT_MIN
    345  1.1  christos 		 /* For var_integer, don't let the user set the value
    346  1.1  christos 		    to INT_MAX directly, as that exposes an
    347  1.1  christos 		    implementation detail to the user interface.  */
    348  1.1  christos 		 || (c->var_type == var_integer && val >= INT_MAX)
    349  1.1  christos 		 || (c->var_type == var_zinteger && val > INT_MAX))
    350  1.1  christos 	  error (_("integer %s out of range"), plongest (val));
    351  1.1  christos 
    352  1.1  christos 	if (*(int *) c->var != val)
    353  1.1  christos 	  {
    354  1.1  christos 	    *(int *) c->var = val;
    355  1.1  christos 
    356  1.1  christos 	    option_changed = 1;
    357  1.1  christos 	  }
    358  1.1  christos 	break;
    359  1.1  christos       }
    360  1.1  christos     case var_enum:
    361  1.1  christos       {
    362  1.1  christos 	int i;
    363  1.1  christos 	int len;
    364  1.6  christos 	int nmatches;
    365  1.1  christos 	const char *match = NULL;
    366  1.1  christos 	const char *p;
    367  1.1  christos 
    368  1.1  christos 	/* If no argument was supplied, print an informative error
    369  1.1  christos 	   message.  */
    370  1.1  christos 	if (arg == NULL)
    371  1.1  christos 	  {
    372  1.1  christos 	    char *msg;
    373  1.1  christos 	    int msg_len = 0;
    374  1.1  christos 
    375  1.1  christos 	    for (i = 0; c->enums[i]; i++)
    376  1.6  christos 	      msg_len += strlen (c->enums[i]) + 2;
    377  1.1  christos 
    378  1.1  christos 	    msg = (char *) xmalloc (msg_len);
    379  1.1  christos 	    *msg = '\0';
    380  1.1  christos 	    make_cleanup (xfree, msg);
    381  1.1  christos 
    382  1.1  christos 	    for (i = 0; c->enums[i]; i++)
    383  1.1  christos 	      {
    384  1.1  christos 		if (i != 0)
    385  1.1  christos 		  strcat (msg, ", ");
    386  1.1  christos 		strcat (msg, c->enums[i]);
    387  1.1  christos 	      }
    388  1.1  christos 	    error (_("Requires an argument. Valid arguments are %s."),
    389  1.1  christos 		   msg);
    390  1.1  christos 	  }
    391  1.1  christos 
    392  1.1  christos 	p = strchr (arg, ' ');
    393  1.1  christos 
    394  1.1  christos 	if (p)
    395  1.1  christos 	  len = p - arg;
    396  1.1  christos 	else
    397  1.1  christos 	  len = strlen (arg);
    398  1.1  christos 
    399  1.1  christos 	nmatches = 0;
    400  1.1  christos 	for (i = 0; c->enums[i]; i++)
    401  1.1  christos 	  if (strncmp (arg, c->enums[i], len) == 0)
    402  1.1  christos 	    {
    403  1.1  christos 	      if (c->enums[i][len] == '\0')
    404  1.1  christos 		{
    405  1.1  christos 		  match = c->enums[i];
    406  1.1  christos 		  nmatches = 1;
    407  1.1  christos 		  break; /* Exact match.  */
    408  1.1  christos 		}
    409  1.1  christos 	      else
    410  1.1  christos 		{
    411  1.1  christos 		  match = c->enums[i];
    412  1.1  christos 		  nmatches++;
    413  1.1  christos 		}
    414  1.1  christos 	    }
    415  1.1  christos 
    416  1.1  christos 	if (nmatches <= 0)
    417  1.1  christos 	  error (_("Undefined item: \"%s\"."), arg);
    418  1.1  christos 
    419  1.1  christos 	if (nmatches > 1)
    420  1.1  christos 	  error (_("Ambiguous item \"%s\"."), arg);
    421  1.1  christos 
    422  1.1  christos 	if (*(const char **) c->var != match)
    423  1.1  christos 	  {
    424  1.1  christos 	    *(const char **) c->var = match;
    425  1.1  christos 
    426  1.1  christos 	    option_changed = 1;
    427  1.1  christos 	  }
    428  1.1  christos       }
    429  1.1  christos       break;
    430  1.1  christos     case var_zuinteger_unlimited:
    431  1.1  christos       {
    432  1.1  christos 	LONGEST val;
    433  1.1  christos 
    434  1.1  christos 	if (arg == NULL)
    435  1.1  christos 	  error_no_arg (_("integer to set it to, or \"unlimited\"."));
    436  1.1  christos 
    437  1.1  christos 	if (is_unlimited_literal (arg))
    438  1.1  christos 	  val = -1;
    439  1.1  christos 	else
    440  1.1  christos 	  val = parse_and_eval_long (arg);
    441  1.1  christos 
    442  1.1  christos 	if (val > INT_MAX)
    443  1.1  christos 	  error (_("integer %s out of range"), plongest (val));
    444  1.1  christos 	else if (val < -1)
    445  1.1  christos 	  error (_("only -1 is allowed to set as unlimited"));
    446  1.1  christos 
    447  1.1  christos 	if (*(int *) c->var != val)
    448  1.1  christos 	  {
    449  1.1  christos 	    *(int *) c->var = val;
    450  1.1  christos 	    option_changed = 1;
    451  1.1  christos 	  }
    452  1.1  christos       }
    453  1.1  christos       break;
    454  1.1  christos     default:
    455  1.1  christos       error (_("gdb internal error: bad var_type in do_setshow_command"));
    456  1.1  christos     }
    457  1.1  christos   c->func (c, NULL, from_tty);
    458  1.1  christos 
    459  1.1  christos   if (notify_command_param_changed_p (option_changed, c))
    460  1.1  christos     {
    461  1.1  christos       char *name, *cp;
    462  1.1  christos       struct cmd_list_element **cmds;
    463  1.1  christos       struct cmd_list_element *p;
    464  1.1  christos       int i;
    465  1.1  christos       int length = 0;
    466  1.1  christos 
    467  1.1  christos       /* Compute the whole multi-word command options.  If user types command
    468  1.1  christos 	 'set foo bar baz on', c->name is 'baz', and GDB can't pass "bar" to
    469  1.1  christos 	 command option change notification, because it is confusing.  We can
    470  1.1  christos 	 trace back through field 'prefix' to compute the whole options,
    471  1.1  christos 	 and pass "foo bar baz" to notification.  */
    472  1.1  christos 
    473  1.1  christos       for (i = 0, p = c; p != NULL; i++)
    474  1.1  christos 	{
    475  1.1  christos 	  length += strlen (p->name);
    476  1.1  christos 	  length++;
    477  1.1  christos 
    478  1.6  christos 	  p = p->prefix;
    479  1.6  christos 	}
    480  1.1  christos       cp = name = (char *) xmalloc (length);
    481  1.1  christos       cmds = XNEWVEC (struct cmd_list_element *, i);
    482  1.1  christos 
    483  1.1  christos       /* Track back through filed 'prefix' and cache them in CMDS.  */
    484  1.1  christos       for (i = 0, p = c; p != NULL; i++)
    485  1.1  christos 	{
    486  1.1  christos 	  cmds[i] = p;
    487  1.1  christos 	  p = p->prefix;
    488  1.1  christos 	}
    489  1.1  christos 
    490  1.1  christos       /* Don't trigger any observer notification if prefixlist is not
    491  1.1  christos 	 setlist.  */
    492  1.1  christos       i--;
    493  1.1  christos       if (cmds[i]->prefixlist != &setlist)
    494  1.1  christos 	{
    495  1.1  christos 	  xfree (cmds);
    496  1.1  christos 	  xfree (name);
    497  1.1  christos 
    498  1.1  christos 	  return;
    499  1.1  christos 	}
    500  1.1  christos       /* Traverse them in the reversed order, and copy their names into
    501  1.1  christos 	 NAME.  */
    502  1.1  christos       for (i--; i >= 0; i--)
    503  1.1  christos 	{
    504  1.1  christos 	  memcpy (cp, cmds[i]->name, strlen (cmds[i]->name));
    505  1.1  christos 	  cp += strlen (cmds[i]->name);
    506  1.1  christos 
    507  1.1  christos 	  if (i != 0)
    508  1.1  christos 	    {
    509  1.1  christos 	      cp[0] = ' ';
    510  1.1  christos 	      cp++;
    511  1.1  christos 	    }
    512  1.1  christos 	}
    513  1.1  christos       cp[0] = 0;
    514  1.1  christos 
    515  1.1  christos       xfree (cmds);
    516  1.1  christos 
    517  1.1  christos       switch (c->var_type)
    518  1.1  christos 	{
    519  1.1  christos 	case var_string:
    520  1.1  christos 	case var_string_noescape:
    521  1.1  christos 	case var_filename:
    522  1.1  christos 	case var_optional_filename:
    523  1.1  christos 	case var_enum:
    524  1.1  christos 	  observer_notify_command_param_changed (name, *(char **) c->var);
    525  1.1  christos 	  break;
    526  1.6  christos 	case var_boolean:
    527  1.1  christos 	  {
    528  1.1  christos 	    const char *opt = *(int *) c->var ? "on" : "off";
    529  1.1  christos 
    530  1.1  christos 	    observer_notify_command_param_changed (name, opt);
    531  1.1  christos 	  }
    532  1.1  christos 	  break;
    533  1.1  christos 	case var_auto_boolean:
    534  1.1  christos 	  {
    535  1.1  christos 	    const char *s = auto_boolean_enums[*(enum auto_boolean *) c->var];
    536  1.1  christos 
    537  1.1  christos 	    observer_notify_command_param_changed (name, s);
    538  1.1  christos 	  }
    539  1.1  christos 	  break;
    540  1.1  christos 	case var_uinteger:
    541  1.1  christos 	case var_zuinteger:
    542  1.1  christos 	  {
    543  1.1  christos 	    char s[64];
    544  1.1  christos 
    545  1.1  christos 	    xsnprintf (s, sizeof s, "%u", *(unsigned int *) c->var);
    546  1.1  christos 	    observer_notify_command_param_changed (name, s);
    547  1.1  christos 	  }
    548  1.1  christos 	  break;
    549  1.1  christos 	case var_integer:
    550  1.1  christos 	case var_zinteger:
    551  1.1  christos 	case var_zuinteger_unlimited:
    552  1.1  christos 	  {
    553  1.1  christos 	    char s[64];
    554  1.1  christos 
    555  1.1  christos 	    xsnprintf (s, sizeof s, "%d", *(int *) c->var);
    556  1.1  christos 	    observer_notify_command_param_changed (name, s);
    557  1.1  christos 	  }
    558  1.1  christos 	  break;
    559  1.1  christos 	}
    560  1.1  christos       xfree (name);
    561  1.1  christos     }
    562  1.1  christos }
    563  1.1  christos 
    564  1.1  christos /* Do a "show" command.  ARG is NULL if no argument, or the
    565  1.1  christos    text of the argument, and FROM_TTY is nonzero if this command is
    566  1.1  christos    being entered directly by the user (i.e. these are just like any
    567  1.1  christos    other command).  C is the command list element for the command.  */
    568  1.3  christos 
    569  1.1  christos void
    570  1.1  christos do_show_command (const char *arg, int from_tty, struct cmd_list_element *c)
    571  1.1  christos {
    572  1.1  christos   struct ui_out *uiout = current_uiout;
    573  1.1  christos 
    574  1.7  christos   gdb_assert (c->type == show_cmd);
    575  1.1  christos 
    576  1.1  christos   string_file stb;
    577  1.1  christos 
    578  1.1  christos   /* Possibly call the pre hook.  */
    579  1.1  christos   if (c->pre_show_hook)
    580  1.1  christos     (c->pre_show_hook) (c);
    581  1.1  christos 
    582  1.1  christos   switch (c->var_type)
    583  1.1  christos     {
    584  1.7  christos     case var_string:
    585  1.1  christos       if (*(char **) c->var)
    586  1.1  christos 	stb.putstr (*(char **) c->var, '"');
    587  1.1  christos       break;
    588  1.1  christos     case var_string_noescape:
    589  1.1  christos     case var_optional_filename:
    590  1.1  christos     case var_filename:
    591  1.7  christos     case var_enum:
    592  1.1  christos       if (*(char **) c->var)
    593  1.1  christos 	stb.puts (*(char **) c->var);
    594  1.7  christos       break;
    595  1.1  christos     case var_boolean:
    596  1.1  christos       stb.puts (*(int *) c->var ? "on" : "off");
    597  1.1  christos       break;
    598  1.1  christos     case var_auto_boolean:
    599  1.1  christos       switch (*(enum auto_boolean*) c->var)
    600  1.7  christos 	{
    601  1.1  christos 	case AUTO_BOOLEAN_TRUE:
    602  1.1  christos 	  stb.puts ("on");
    603  1.7  christos 	  break;
    604  1.1  christos 	case AUTO_BOOLEAN_FALSE:
    605  1.1  christos 	  stb.puts ("off");
    606  1.7  christos 	  break;
    607  1.1  christos 	case AUTO_BOOLEAN_AUTO:
    608  1.1  christos 	  stb.puts ("auto");
    609  1.1  christos 	  break;
    610  1.1  christos 	default:
    611  1.1  christos 	  internal_error (__FILE__, __LINE__,
    612  1.1  christos 			  _("do_show_command: "
    613  1.1  christos 			    "invalid var_auto_boolean"));
    614  1.1  christos 	  break;
    615  1.1  christos 	}
    616  1.1  christos       break;
    617  1.1  christos     case var_uinteger:
    618  1.1  christos     case var_zuinteger:
    619  1.7  christos       if (c->var_type == var_uinteger
    620  1.1  christos 	  && *(unsigned int *) c->var == UINT_MAX)
    621  1.7  christos 	stb.puts ("unlimited");
    622  1.1  christos       else
    623  1.1  christos 	stb.printf ("%u", *(unsigned int *) c->var);
    624  1.1  christos       break;
    625  1.1  christos     case var_integer:
    626  1.1  christos     case var_zinteger:
    627  1.7  christos       if (c->var_type == var_integer
    628  1.1  christos 	  && *(int *) c->var == INT_MAX)
    629  1.7  christos 	stb.puts ("unlimited");
    630  1.1  christos       else
    631  1.1  christos 	stb.printf ("%d", *(int *) c->var);
    632  1.1  christos       break;
    633  1.1  christos     case var_zuinteger_unlimited:
    634  1.7  christos       {
    635  1.1  christos 	if (*(int *) c->var == -1)
    636  1.7  christos 	  stb.puts ("unlimited");
    637  1.1  christos 	else
    638  1.1  christos 	  stb.printf ("%d", *(int *) c->var);
    639  1.1  christos       }
    640  1.1  christos       break;
    641  1.1  christos     default:
    642  1.1  christos       error (_("gdb internal error: bad var_type in do_show_command"));
    643  1.1  christos     }
    644  1.1  christos 
    645  1.1  christos 
    646  1.1  christos   /* FIXME: cagney/2005-02-10: Need to split this in half: code to
    647  1.1  christos      convert the value into a string (esentially the above); and
    648  1.1  christos      code to print the value out.  For the latter there should be
    649  1.7  christos      MI and CLI specific versions.  */
    650  1.7  christos 
    651  1.1  christos   if (uiout->is_mi_like_p ())
    652  1.1  christos     uiout->field_stream ("value", stb);
    653  1.1  christos   else
    654  1.7  christos     {
    655  1.1  christos       if (c->show_value_func != NULL)
    656  1.7  christos 	c->show_value_func (gdb_stdout, from_tty, c, stb.c_str ());
    657  1.1  christos       else
    658  1.1  christos 	deprecated_show_value_hack (gdb_stdout, from_tty, c, stb.c_str ());
    659  1.1  christos     }
    660  1.1  christos 
    661  1.1  christos   c->func (c, NULL, from_tty);
    662  1.1  christos }
    663  1.1  christos 
    664  1.1  christos /* Show all the settings in a list of show commands.  */
    665  1.3  christos 
    666  1.1  christos void
    667  1.1  christos cmd_show_list (struct cmd_list_element *list, int from_tty, const char *prefix)
    668  1.1  christos {
    669  1.1  christos   struct cleanup *showlist_chain;
    670  1.1  christos   struct ui_out *uiout = current_uiout;
    671  1.1  christos 
    672  1.1  christos   showlist_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "showlist");
    673  1.1  christos   for (; list != NULL; list = list->next)
    674  1.1  christos     {
    675  1.1  christos       /* If we find a prefix, run its list, prefixing our output by its
    676  1.1  christos          prefix (with "show " skipped).  */
    677  1.1  christos       if (list->prefixlist && !list->abbrev_flag)
    678  1.1  christos 	{
    679  1.6  christos 	  struct cleanup *optionlist_chain
    680  1.1  christos 	    = make_cleanup_ui_out_tuple_begin_end (uiout, "optionlist");
    681  1.7  christos 	  const char *new_prefix = strstr (list->prefixname, "show ") + 5;
    682  1.7  christos 
    683  1.1  christos 	  if (uiout->is_mi_like_p ())
    684  1.1  christos 	    uiout->field_string ("prefix", new_prefix);
    685  1.1  christos 	  cmd_show_list (*list->prefixlist, from_tty, new_prefix);
    686  1.1  christos 	  /* Close the tuple.  */
    687  1.1  christos 	  do_cleanups (optionlist_chain);
    688  1.1  christos 	}
    689  1.5  christos       else
    690  1.1  christos 	{
    691  1.1  christos 	  if (list->theclass != no_set_class)
    692  1.1  christos 	    {
    693  1.1  christos 	      struct cleanup *option_chain
    694  1.7  christos 		= make_cleanup_ui_out_tuple_begin_end (uiout, "option");
    695  1.7  christos 
    696  1.7  christos 	      uiout->text (prefix);
    697  1.1  christos 	      uiout->field_string ("name", list->name);
    698  1.1  christos 	      uiout->text (":  ");
    699  1.1  christos 	      if (list->type == show_cmd)
    700  1.1  christos 		do_show_command ((char *) NULL, from_tty, list);
    701  1.1  christos 	      else
    702  1.1  christos 		cmd_func (list, NULL, from_tty);
    703  1.1  christos 	      /* Close the tuple.  */
    704  1.1  christos 	      do_cleanups (option_chain);
    705  1.1  christos 	    }
    706  1.1  christos 	}
    707  1.1  christos     }
    708  1.1  christos   /* Close the tuple.  */
    709  1.1  christos   do_cleanups (showlist_chain);
    710                }
    711                
    712