#include "defs.h"
#include "gdbcmd.h"
#include "symtab.h"
#include "value.h"
#include "top.h"
#include <ctype.h>
#include "gdb_string.h"
#ifdef UI_OUT
#include "ui-out.h"
#endif
#include "gdb_wait.h"
#include "gnu-regex.h"
#ifdef __MSDOS__
# define CANT_FORK
#endif
static void undef_cmd_error (char *, char *);
static void show_user (char *, int);
static void show_user_1 (struct cmd_list_element *, struct ui_file *);
static void make_command (char *, int);
static void shell_escape (char *, int);
static int parse_binary_operation (char *);
static void print_doc_line (struct ui_file *, char *);
static struct cmd_list_element *find_cmd (char *command,
int len,
struct cmd_list_element *clist,
int ignore_help_classes,
int *nfound);
static void apropos_cmd_helper (struct ui_file *, struct cmd_list_element *,
struct re_pattern_buffer *, char *);
static void help_all (struct ui_file *stream);
void apropos_command (char *, int);
void _initialize_command (void);
struct cmd_list_element *
add_cmd (char *name, enum command_class class, void (*fun) (char *, int),
char *doc, struct cmd_list_element **list)
{
register struct cmd_list_element *c
= (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
struct cmd_list_element *p;
delete_cmd (name, list);
if (*list == NULL || STRCMP ((*list)->name, name) >= 0)
{
c->next = *list;
*list = c;
}
else
{
p = *list;
while (p->next && STRCMP (p->next->name, name) <= 0)
{
p = p->next;
}
c->next = p->next;
p->next = c;
}
c->name = name;
c->class = class;
c->function.cfunc = fun;
c->doc = doc;
c->flags = 0;
c->replacement = NULL;
c->hook_pre = NULL;
c->hook_post = NULL;
c->hook_in = 0;
c->prefixlist = NULL;
c->prefixname = NULL;
c->allow_unknown = 0;
c->abbrev_flag = 0;
c->completer = make_symbol_completion_list;
c->completer_quote_characters = gdb_completer_quote_characters;
c->completer_word_break_characters = gdb_completer_word_break_characters;
c->type = not_set_cmd;
c->var = NULL;
c->var_type = var_boolean;
c->enums = NULL;
c->user_commands = NULL;
c->hookee_pre = NULL;
c->hookee_post = NULL;
c->cmd_pointer = NULL;
return c;
}
struct cmd_list_element *
deprecate_cmd (struct cmd_list_element *cmd, char *replacement)
{
cmd->flags |= (CMD_DEPRECATED | DEPRECATED_WARN_USER);
if (replacement != NULL)
cmd->replacement = replacement;
else
cmd->replacement = NULL;
return cmd;
}
#if 0
struct cmd_list_element *
add_abbrev_cmd (char *name, enum command_class class, void (*fun) (char *, int),
char *doc, struct cmd_list_element **list)
{
register struct cmd_list_element *c
= add_cmd (name, class, fun, doc, list);
c->abbrev_flag = 1;
return c;
}
#endif
struct cmd_list_element *
add_alias_cmd (char *name, char *oldname, enum command_class class,
int abbrev_flag, struct cmd_list_element **list)
{
char *copied_name;
register struct cmd_list_element *old;
register struct cmd_list_element *c;
copied_name = (char *) alloca (strlen (oldname) + 1);
strcpy (copied_name, oldname);
old = lookup_cmd (&copied_name, *list, "", 1, 1);
if (old == 0)
{
delete_cmd (name, list);
return 0;
}
c = add_cmd (name, class, old->function.cfunc, old->doc, list);
c->prefixlist = old->prefixlist;
c->prefixname = old->prefixname;
c->allow_unknown = old->allow_unknown;
c->abbrev_flag = abbrev_flag;
c->cmd_pointer = old;
return c;
}
struct cmd_list_element *
add_prefix_cmd (char *name, enum command_class class, void (*fun) (char *, int),
char *doc, struct cmd_list_element **prefixlist,
char *prefixname, int allow_unknown,
struct cmd_list_element **list)
{
register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
c->prefixlist = prefixlist;
c->prefixname = prefixname;
c->allow_unknown = allow_unknown;
return c;
}
struct cmd_list_element *
add_abbrev_prefix_cmd (char *name, enum command_class class,
void (*fun) (char *, int), char *doc,
struct cmd_list_element **prefixlist, char *prefixname,
int allow_unknown, struct cmd_list_element **list)
{
register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
c->prefixlist = prefixlist;
c->prefixname = prefixname;
c->allow_unknown = allow_unknown;
c->abbrev_flag = 1;
return c;
}
void
not_just_help_class_command (char *args, int from_tty)
{
}
static void empty_sfunc (char *, int, struct cmd_list_element *);
static void
empty_sfunc (char *args, int from_tty, struct cmd_list_element *c)
{
}
struct cmd_list_element *
add_set_cmd (char *name,
enum command_class class,
var_types var_type,
void *var,
char *doc,
struct cmd_list_element **list)
{
struct cmd_list_element *c
= add_cmd (name, class, NO_FUNCTION, doc, list);
c->type = set_cmd;
c->var_type = var_type;
c->var = var;
c->function.sfunc = empty_sfunc;
return c;
}
struct cmd_list_element *
add_set_enum_cmd (char *name,
enum command_class class,
const char *enumlist[],
const char **var,
char *doc,
struct cmd_list_element **list)
{
struct cmd_list_element *c
= add_set_cmd (name, class, var_enum, var, doc, list);
c->enums = enumlist;
return c;
}
struct cmd_list_element *
add_set_auto_boolean_cmd (char *name,
enum command_class class,
enum cmd_auto_boolean *var,
char *doc,
struct cmd_list_element **list)
{
static const char *auto_boolean_enums[] = { "on", "off", "auto", NULL };
struct cmd_list_element *c;
c = add_set_cmd (name, class, var_auto_boolean, var, doc, list);
c->enums = auto_boolean_enums;
return c;
}
struct cmd_list_element *
add_show_from_set (struct cmd_list_element *setcmd,
struct cmd_list_element **list)
{
struct cmd_list_element *showcmd =
(struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
struct cmd_list_element *p;
memcpy (showcmd, setcmd, sizeof (struct cmd_list_element));
delete_cmd (showcmd->name, list);
showcmd->type = show_cmd;
if (setcmd->doc[0] == 'S' && setcmd->doc[1] == 'e'
&& setcmd->doc[2] == 't' && setcmd->doc[3] == ' ')
showcmd->doc = concat ("Show ", setcmd->doc + 4, NULL);
else
fprintf_unfiltered (gdb_stderr, "GDB internal error: Bad docstring for set command\n");
if (*list == NULL || STRCMP ((*list)->name, showcmd->name) >= 0)
{
showcmd->next = *list;
*list = showcmd;
}
else
{
p = *list;
while (p->next && STRCMP (p->next->name, showcmd->name) <= 0)
{
p = p->next;
}
showcmd->next = p->next;
p->next = showcmd;
}
return showcmd;
}
void
delete_cmd (char *name, struct cmd_list_element **list)
{
register struct cmd_list_element *c;
struct cmd_list_element *p;
while (*list && STREQ ((*list)->name, name))
{
if ((*list)->hookee_pre)
(*list)->hookee_pre->hook_pre = 0;
if ((*list)->hookee_post)
(*list)->hookee_post->hook_post = 0;
p = (*list)->next;
free ((PTR) * list);
*list = p;
}
if (*list)
for (c = *list; c->next;)
{
if (STREQ (c->next->name, name))
{
if (c->next->hookee_pre)
c->next->hookee_pre->hook_pre = 0;
if (c->next->hookee_post)
c->next->hookee_post->hook_post = 0;
p = c->next->next;
free ((PTR) c->next);
c->next = p;
}
else
c = c->next;
}
}
static void
apropos_cmd_helper (struct ui_file *stream, struct cmd_list_element *commandlist,
struct re_pattern_buffer *regex, char *prefix)
{
register struct cmd_list_element *c;
int returnvalue=1;
for (c=commandlist;c;c=c->next)
{
if (c->name != NULL)
{
returnvalue=re_search(regex,c->name,strlen(c->name),0,strlen(c->name),NULL);
if (returnvalue >= 0)
{
fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
print_doc_line (stream, c->doc);
fputs_filtered ("\n", stream);
returnvalue=0;
}
}
if (c->doc != NULL && returnvalue != 0)
{
if (re_search(regex,c->doc,strlen(c->doc),0,strlen(c->doc),NULL) >=0)
{
fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
print_doc_line (stream, c->doc);
fputs_filtered ("\n", stream);
}
}
if (c->prefixlist != NULL)
{
apropos_cmd_helper(stream,*c->prefixlist,regex,c->prefixname);
}
}
}
void
apropos_command (char *searchstr, int from_tty)
{
extern struct cmd_list_element *cmdlist;
regex_t pattern;
char *pattern_fastmap;
char errorbuffer[512];
pattern_fastmap=calloc(256,sizeof(char));
if (searchstr == NULL)
error("REGEXP string is empty");
if (regcomp(&pattern,searchstr,REG_ICASE) == 0)
{
pattern.fastmap=pattern_fastmap;
re_compile_fastmap(&pattern);
apropos_cmd_helper(gdb_stdout,cmdlist,&pattern,"");
}
else
{
regerror(regcomp(&pattern,searchstr,REG_ICASE),NULL,errorbuffer,512);
error("Error in regular expression:%s",errorbuffer);
}
free(pattern_fastmap);
}
void
help_cmd (char *command, struct ui_file *stream)
{
struct cmd_list_element *c;
extern struct cmd_list_element *cmdlist;
if (!command)
{
help_list (cmdlist, "", all_classes, stream);
return;
}
if (strcmp (command, "all") == 0)
{
help_all (stream);
return;
}
c = lookup_cmd (&command, cmdlist, "", 0, 0);
if (c == 0)
return;
fputs_filtered (c->doc, stream);
fputs_filtered ("\n", stream);
if (c->prefixlist == 0 && c->function.cfunc != NULL)
return;
fprintf_filtered (stream, "\n");
if (c->prefixlist)
help_list (*c->prefixlist, c->prefixname, all_commands, stream);
if (c->function.cfunc == NULL)
help_list (cmdlist, "", c->class, stream);
if (c->hook_pre || c->hook_post)
fprintf_filtered (stream,
"\nThis command has a hook (or hooks) defined:\n");
if (c->hook_pre)
fprintf_filtered (stream,
"\tThis command is run after : %s (pre hook)\n",
c->hook_pre->name);
if (c->hook_post)
fprintf_filtered (stream,
"\tThis command is run before : %s (post hook)\n",
c->hook_post->name);
}
void
help_list (struct cmd_list_element *list, char *cmdtype,
enum command_class class, struct ui_file *stream)
{
int len;
char *cmdtype1, *cmdtype2;
len = strlen (cmdtype);
cmdtype1 = (char *) alloca (len + 1);
cmdtype1[0] = 0;
cmdtype2 = (char *) alloca (len + 4);
cmdtype2[0] = 0;
if (len)
{
cmdtype1[0] = ' ';
strncpy (cmdtype1 + 1, cmdtype, len - 1);
cmdtype1[len] = 0;
strncpy (cmdtype2, cmdtype, len - 1);
strcpy (cmdtype2 + len - 1, " sub");
}
if (class == all_classes)
fprintf_filtered (stream, "List of classes of %scommands:\n\n", cmdtype2);
else
fprintf_filtered (stream, "List of %scommands:\n\n", cmdtype2);
help_cmd_list (list, class, cmdtype, (int) class >= 0, stream);
if (class == all_classes)
fprintf_filtered (stream, "\n\
Type \"help%s\" followed by a class name for a list of commands in that class.",
cmdtype1);
fprintf_filtered (stream, "\n\
Type \"help%s\" followed by %scommand name for full documentation.\n\
Command name abbreviations are allowed if unambiguous.\n",
cmdtype1, cmdtype2);
}
static void
help_all (struct ui_file *stream)
{
struct cmd_list_element *c;
extern struct cmd_list_element *cmdlist;
for (c = cmdlist; c; c = c->next)
{
if (c->abbrev_flag)
continue;
if (c->prefixlist)
help_cmd_list (*c->prefixlist, all_commands, c->prefixname, 0, stream);
else if (c->function.cfunc == NULL)
help_cmd_list (cmdlist, c->class, "", 0, stream);
}
}
static void
print_doc_line (struct ui_file *stream, char *str)
{
static char *line_buffer = 0;
static int line_size;
register char *p;
if (!line_buffer)
{
line_size = 80;
line_buffer = (char *) xmalloc (line_size);
}
p = str;
while (*p && *p != '\n' && *p != '.' && *p != ',')
p++;
if (p - str > line_size - 1)
{
line_size = p - str + 1;
free ((PTR) line_buffer);
line_buffer = (char *) xmalloc (line_size);
}
strncpy (line_buffer, str, p - str);
line_buffer[p - str] = '\0';
if (islower (line_buffer[0]))
line_buffer[0] = toupper (line_buffer[0]);
#ifdef UI_OUT
ui_out_text (uiout, line_buffer);
#else
fputs_filtered (line_buffer, stream);
#endif
}
void
help_cmd_list (struct cmd_list_element *list, enum command_class class,
char *prefix, int recurse, struct ui_file *stream)
{
register struct cmd_list_element *c;
for (c = list; c; c = c->next)
{
if (c->abbrev_flag == 0 &&
(class == all_commands
|| (class == all_classes && c->function.cfunc == NULL)
|| (class == c->class && c->function.cfunc != NULL)))
{
fprintf_filtered (stream, "%s%s -- ", prefix, c->name);
print_doc_line (stream, c->doc);
fputs_filtered ("\n", stream);
}
if (recurse
&& c->prefixlist != 0
&& c->abbrev_flag == 0)
help_cmd_list (*c->prefixlist, class, c->prefixname, 1, stream);
}
}
static struct cmd_list_element *
find_cmd (char *command, int len, struct cmd_list_element *clist,
int ignore_help_classes, int *nfound)
{
struct cmd_list_element *found, *c;
found = (struct cmd_list_element *) NULL;
*nfound = 0;
for (c = clist; c; c = c->next)
if (!strncmp (command, c->name, len)
&& (!ignore_help_classes || c->function.cfunc))
{
found = c;
(*nfound)++;
if (c->name[len] == '\0')
{
*nfound = 1;
break;
}
}
return found;
}
struct cmd_list_element *
lookup_cmd_1 (char **text, struct cmd_list_element *clist,
struct cmd_list_element **result_list, int ignore_help_classes)
{
char *p, *command;
int len, tmp, nfound;
struct cmd_list_element *found, *c;
char *line = *text;
while (**text == ' ' || **text == '\t')
(*text)++;
for (p = *text;
*p && (isalnum (*p) || *p == '-' || *p == '_' ||
(tui_version &&
(*p == '+' || *p == '<' || *p == '>' || *p == '$')) ||
(xdb_commands && (*p == '!' || *p == '/' || *p == '?')));
p++)
;
if (p == *text)
return 0;
len = p - *text;
command = (char *) alloca (len + 1);
for (tmp = 0; tmp < len; tmp++)
{
char x = (*text)[tmp];
command[tmp] = x;
}
command[len] = '\0';
found = 0;
nfound = 0;
found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
if (!found || nfound == 0)
{
for (tmp = 0; tmp < len; tmp++)
{
char x = command[tmp];
command[tmp] = isupper (x) ? tolower (x) : x;
}
found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
}
if (nfound == 0)
return 0;
if (nfound > 1)
{
if (result_list != NULL)
*result_list = 0;
return (struct cmd_list_element *) -1;
}
*text = p;
if (found->cmd_pointer)
{
if (found->flags & DEPRECATED_WARN_USER)
deprecated_cmd_warning (&line);
found = found->cmd_pointer;
}
if (found->prefixlist)
{
c = lookup_cmd_1 (text, *found->prefixlist, result_list,
ignore_help_classes);
if (!c)
{
if (result_list != NULL)
*result_list = clist;
return found;
}
else if (c == (struct cmd_list_element *) -1)
{
if (result_list != NULL)
if (!*result_list)
*result_list = found;
return c;
}
else
{
return c;
}
}
else
{
if (result_list != NULL)
*result_list = clist;
return found;
}
}
static void
undef_cmd_error (char *cmdtype, char *q)
{
error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".",
cmdtype,
q,
*cmdtype ? " " : "",
strlen (cmdtype) - 1,
cmdtype);
}
struct cmd_list_element *
lookup_cmd (char **line, struct cmd_list_element *list, char *cmdtype,
int allow_unknown, int ignore_help_classes)
{
struct cmd_list_element *last_list = 0;
struct cmd_list_element *c =
lookup_cmd_1 (line, list, &last_list, ignore_help_classes);
#if 0
char *ptr = (*line) + strlen (*line) - 1;
while (ptr >= *line && (*ptr == ' ' || *ptr == '\t'))
ptr--;
*(ptr + 1) = '\0';
#endif
if (!c)
{
if (!allow_unknown)
{
if (!*line)
error ("Lack of needed %scommand", cmdtype);
else
{
char *p = *line, *q;
while (isalnum (*p) || *p == '-')
p++;
q = (char *) alloca (p - *line + 1);
strncpy (q, *line, p - *line);
q[p - *line] = '\0';
undef_cmd_error (cmdtype, q);
}
}
else
return 0;
}
else if (c == (struct cmd_list_element *) -1)
{
int local_allow_unknown = (last_list ? last_list->allow_unknown :
allow_unknown);
char *local_cmdtype = last_list ? last_list->prefixname : cmdtype;
struct cmd_list_element *local_list =
(last_list ? *(last_list->prefixlist) : list);
if (local_allow_unknown < 0)
{
if (last_list)
return last_list;
else
return 0;
}
else
{
int amb_len;
char ambbuf[100];
for (amb_len = 0;
((*line)[amb_len] && (*line)[amb_len] != ' '
&& (*line)[amb_len] != '\t');
amb_len++)
;
ambbuf[0] = 0;
for (c = local_list; c; c = c->next)
if (!strncmp (*line, c->name, amb_len))
{
if (strlen (ambbuf) + strlen (c->name) + 6 < (int) sizeof ambbuf)
{
if (strlen (ambbuf))
strcat (ambbuf, ", ");
strcat (ambbuf, c->name);
}
else
{
strcat (ambbuf, "..");
break;
}
}
error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype,
*line, ambbuf);
return 0;
}
}
else
{
while (**line == ' ' || **line == '\t')
(*line)++;
if (c->prefixlist && **line && !c->allow_unknown)
undef_cmd_error (c->prefixname, *line);
return c;
}
return 0;
}
void
deprecated_cmd_warning (char **text)
{
struct cmd_list_element *alias = NULL;
struct cmd_list_element *prefix_cmd = NULL;
struct cmd_list_element *cmd = NULL;
struct cmd_list_element *c;
char *type;
if (!lookup_cmd_composition (*text, &alias, &prefix_cmd, &cmd))
return;
if (!((alias ? (alias->flags & DEPRECATED_WARN_USER) : 0)
|| (cmd->flags & DEPRECATED_WARN_USER) ) )
return;
printf_filtered ("Warning:");
if (alias && !(cmd->flags & CMD_DEPRECATED))
printf_filtered (" '%s', an alias for the", alias->name);
printf_filtered (" command '");
if (prefix_cmd)
printf_filtered ("%s", prefix_cmd->prefixname);
printf_filtered ("%s", cmd->name);
if (alias && (cmd->flags & CMD_DEPRECATED))
printf_filtered ("' (%s) is deprecated.\n", alias->name);
else
printf_filtered ("' is deprecated.\n");
if (alias && !(cmd->flags & CMD_DEPRECATED))
{
if (alias->replacement)
printf_filtered ("Use '%s'.\n\n", alias->replacement);
else
printf_filtered ("No alternative known.\n\n");
}
else
{
if (cmd->replacement)
printf_filtered ("Use '%s'.\n\n", cmd->replacement);
else
printf_filtered ("No alternative known.\n\n");
}
if (alias)
alias->flags &= ~DEPRECATED_WARN_USER;
cmd->flags &= ~DEPRECATED_WARN_USER;
}
int
lookup_cmd_composition (char *text,
struct cmd_list_element **alias,
struct cmd_list_element **prefix_cmd,
struct cmd_list_element **cmd)
{
char *p, *command;
int len, tmp, nfound;
struct cmd_list_element *cur_list;
struct cmd_list_element *prev_cmd;
*alias = NULL;
*prefix_cmd = NULL;
*cmd = NULL;
cur_list = cmdlist;
while (1)
{
prev_cmd = *cmd;
while (*text == ' ' || *text == '\t')
(text)++;
for (p = text;
*p && (isalnum (*p) || *p == '-' || *p == '_' ||
(tui_version &&
(*p == '+' || *p == '<' || *p == '>' || *p == '$')) ||
(xdb_commands && (*p == '!' || *p == '/' || *p == '?')));
p++)
;
if (p == text)
return 0;
len = p - text;
command = (char *) alloca (len + 1);
for (tmp = 0; tmp < len; tmp++)
{
char x = text[tmp];
command[tmp] = x;
}
command[len] = '\0';
*cmd = 0;
nfound = 0;
*cmd = find_cmd (command, len, cur_list, 1, &nfound);
if (!*cmd || nfound == 0)
{
for (tmp = 0; tmp < len; tmp++)
{
char x = command[tmp];
command[tmp] = isupper (x) ? tolower (x) : x;
}
*cmd = find_cmd (command, len, cur_list, 1, &nfound);
}
if (*cmd == (struct cmd_list_element *) -1)
{
return 0;
}
if (*cmd == NULL)
return 0;
else
{
if ((*cmd)->cmd_pointer)
{
*alias = *cmd;
*cmd = (*cmd)->cmd_pointer;
}
*prefix_cmd = prev_cmd;
}
if ((*cmd)->prefixlist)
cur_list = *(*cmd)->prefixlist;
else
return 1;
text = p;
}
}
#if 0
struct cmd_list_element *
lookup_cmd (char **line, struct cmd_list_element *list, char *cmdtype,
int allow_unknown)
{
register char *p;
register struct cmd_list_element *c, *found;
int nfound;
char ambbuf[100];
char *processed_cmd;
int i, cmd_len;
while (**line == ' ' || **line == '\t')
(*line)++;
p = *line + strlen (*line);
while (p != *line && (p[-1] == ' ' || p[-1] == '\t'))
p--;
*p = 0;
p = *line;
while (*p == '-' || isalnum (*p))
p++;
if (p == *line)
{
if (!allow_unknown)
error ("Lack of needed %scommand", cmdtype);
return 0;
}
processed_cmd = (char *) alloca (p - *line + 1);
for (cmd_len = 0; cmd_len < p - *line; cmd_len++)
{
char x = (*line)[cmd_len];
if (isupper (x))
processed_cmd[cmd_len] = tolower (x);
else
processed_cmd[cmd_len] = x;
}
processed_cmd[cmd_len] = '\0';
found = 0;
nfound = 0;
for (c = list; c; c = c->next)
{
if (!strncmp (processed_cmd, c->name, cmd_len))
{
found = c;
nfound++;
if (c->name[cmd_len] == 0)
{
nfound = 1;
break;
}
}
}
if (nfound != 1)
{
if (nfound > 1 && allow_unknown >= 0)
{
ambbuf[0] = 0;
for (c = list; c; c = c->next)
if (!strncmp (processed_cmd, c->name, cmd_len))
{
if (strlen (ambbuf) + strlen (c->name) + 6 < sizeof ambbuf)
{
if (strlen (ambbuf))
strcat (ambbuf, ", ");
strcat (ambbuf, c->name);
}
else
{
strcat (ambbuf, "..");
break;
}
}
error ("Ambiguous %scommand \"%s\": %s.", cmdtype,
processed_cmd, ambbuf);
}
else if (!allow_unknown)
error ("Undefined %scommand: \"%s\".", cmdtype, processed_cmd);
return 0;
}
while (*p == ' ' || *p == '\t')
p++;
*line = p;
if (found->prefixlist && *p)
{
c = lookup_cmd (line, *found->prefixlist, found->prefixname,
found->allow_unknown);
if (c)
return c;
}
return found;
}
#endif
char **
complete_on_cmdlist (struct cmd_list_element *list, char *text, char *word)
{
struct cmd_list_element *ptr;
char **matchlist;
int sizeof_matchlist;
int matches;
int textlen = strlen (text);
sizeof_matchlist = 10;
matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
matches = 0;
for (ptr = list; ptr; ptr = ptr->next)
if (!strncmp (ptr->name, text, textlen)
&& !ptr->abbrev_flag
&& (ptr->function.cfunc
|| ptr->prefixlist))
{
if (matches == sizeof_matchlist)
{
sizeof_matchlist *= 2;
matchlist = (char **) xrealloc ((char *) matchlist,
(sizeof_matchlist
* sizeof (char *)));
}
matchlist[matches] = (char *)
xmalloc (strlen (word) + strlen (ptr->name) + 1);
if (word == text)
strcpy (matchlist[matches], ptr->name);
else if (word > text)
{
strcpy (matchlist[matches], ptr->name + (word - text));
}
else
{
strncpy (matchlist[matches], word, text - word);
matchlist[matches][text - word] = '\0';
strcat (matchlist[matches], ptr->name);
}
++matches;
}
if (matches == 0)
{
free ((PTR) matchlist);
matchlist = 0;
}
else
{
matchlist = (char **) xrealloc ((char *) matchlist, ((matches + 1)
* sizeof (char *)));
matchlist[matches] = (char *) 0;
}
return matchlist;
}
char **
complete_on_enum (const char *enumlist[],
char *text,
char *word)
{
char **matchlist;
int sizeof_matchlist;
int matches;
int textlen = strlen (text);
int i;
const char *name;
sizeof_matchlist = 10;
matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *));
matches = 0;
for (i = 0; (name = enumlist[i]) != NULL; i++)
if (strncmp (name, text, textlen) == 0)
{
if (matches == sizeof_matchlist)
{
sizeof_matchlist *= 2;
matchlist = (char **) xrealloc ((char *) matchlist,
(sizeof_matchlist
* sizeof (char *)));
}
matchlist[matches] = (char *)
xmalloc (strlen (word) + strlen (name) + 1);
if (word == text)
strcpy (matchlist[matches], name);
else if (word > text)
{
strcpy (matchlist[matches], name + (word - text));
}
else
{
strncpy (matchlist[matches], word, text - word);
matchlist[matches][text - word] = '\0';
strcat (matchlist[matches], name);
}
++matches;
}
if (matches == 0)
{
free ((PTR) matchlist);
matchlist = 0;
}
else
{
matchlist = (char **) xrealloc ((char *) matchlist, ((matches + 1)
* sizeof (char *)));
matchlist[matches] = (char *) 0;
}
return matchlist;
}
static enum cmd_auto_boolean
parse_auto_binary_operation (const char *arg)
{
if (arg != NULL && *arg != '\0')
{
int length = strlen (arg);
while (isspace (arg[length - 1]) && length > 0)
length--;
if (strncmp (arg, "on", length) == 0
|| strncmp (arg, "1", length) == 0
|| strncmp (arg, "yes", length) == 0
|| strncmp (arg, "enable", length) == 0)
return CMD_AUTO_BOOLEAN_TRUE;
else if (strncmp (arg, "off", length) == 0
|| strncmp (arg, "0", length) == 0
|| strncmp (arg, "no", length) == 0
|| strncmp (arg, "disable", length) == 0)
return CMD_AUTO_BOOLEAN_FALSE;
else if (strncmp (arg, "auto", length) == 0
|| (strncmp (arg, "-1", length) == 0 && length > 1))
return CMD_AUTO_BOOLEAN_AUTO;
}
error ("\"on\", \"off\" or \"auto\" expected.");
return CMD_AUTO_BOOLEAN_AUTO;
}
static int
parse_binary_operation (char *arg)
{
int length;
if (!arg || !*arg)
return 1;
length = strlen (arg);
while (arg[length - 1] == ' ' || arg[length - 1] == '\t')
length--;
if (strncmp (arg, "on", length) == 0
|| strncmp (arg, "1", length) == 0
|| strncmp (arg, "yes", length) == 0
|| strncmp (arg, "enable", length) == 0)
return 1;
else if (strncmp (arg, "off", length) == 0
|| strncmp (arg, "0", length) == 0
|| strncmp (arg, "no", length) == 0
|| strncmp (arg, "disable", length) == 0)
return 0;
else
{
error ("\"on\" or \"off\" expected.");
return 0;
}
}
void
do_setshow_command (char *arg, int from_tty, struct cmd_list_element *c)
{
if (c->type == set_cmd)
{
switch (c->var_type)
{
case var_string:
{
char *new;
char *p;
char *q;
int ch;
if (arg == NULL)
arg = "";
new = (char *) xmalloc (strlen (arg) + 2);
p = arg;
q = new;
while ((ch = *p++) != '\000')
{
if (ch == '\\')
{
if (*p == 0)
break;
ch = parse_escape (&p);
if (ch == 0)
break;
else if (ch > 0)
*q++ = ch;
}
else
*q++ = ch;
}
#if 0
if (*(p - 1) != '\\')
*q++ = ' ';
#endif
*q++ = '\0';
new = (char *) xrealloc (new, q - new);
if (*(char **) c->var != NULL)
free (*(char **) c->var);
*(char **) c->var = new;
}
break;
case var_string_noescape:
if (arg == NULL)
arg = "";
if (*(char **) c->var != NULL)
free (*(char **) c->var);
*(char **) c->var = savestring (arg, strlen (arg));
break;
case var_filename:
if (arg == NULL)
error_no_arg ("filename to set it to.");
if (*(char **) c->var != NULL)
free (*(char **) c->var);
*(char **) c->var = tilde_expand (arg);
break;
case var_boolean:
*(int *) c->var = parse_binary_operation (arg);
break;
case var_auto_boolean:
*(enum cmd_auto_boolean *) c->var = parse_auto_binary_operation (arg);
break;
case var_uinteger:
if (arg == NULL)
error_no_arg ("integer to set it to.");
*(unsigned int *) c->var = parse_and_eval_long (arg);
if (*(unsigned int *) c->var == 0)
*(unsigned int *) c->var = UINT_MAX;
break;
case var_integer:
{
unsigned int val;
if (arg == NULL)
error_no_arg ("integer to set it to.");
val = parse_and_eval_long (arg);
if (val == 0)
*(int *) c->var = INT_MAX;
else if (val >= INT_MAX)
error ("integer %u out of range", val);
else
*(int *) c->var = val;
break;
}
case var_zinteger:
if (arg == NULL)
error_no_arg ("integer to set it to.");
*(int *) c->var = parse_and_eval_long (arg);
break;
case var_enum:
{
int i;
int len;
int nmatches;
const char *match = NULL;
char *p;
if (arg == NULL)
{
char msg[1024];
strcpy (msg, "Requires an argument. Valid arguments are ");
for (i = 0; c->enums[i]; i++)
{
if (i != 0)
strcat (msg, ", ");
strcat (msg, c->enums[i]);
}
strcat (msg, ".");
error (msg);
}
p = strchr (arg, ' ');
if (p)
len = p - arg;
else
len = strlen (arg);
nmatches = 0;
for (i = 0; c->enums[i]; i++)
if (strncmp (arg, c->enums[i], len) == 0)
{
if (c->enums[i][len] == '\0')
{
match = c->enums[i];
nmatches = 1;
break;
}
else
{
match = c->enums[i];
nmatches++;
}
}
if (nmatches <= 0)
error ("Undefined item: \"%s\".", arg);
if (nmatches > 1)
error ("Ambiguous item \"%s\".", arg);
*(const char **) c->var = match;
}
break;
default:
error ("gdb internal error: bad var_type in do_setshow_command");
}
}
else if (c->type == show_cmd)
{
#ifdef UI_OUT
struct cleanup *old_chain;
struct ui_stream *stb;
int quote;
stb = ui_out_stream_new (uiout);
old_chain = make_cleanup_ui_out_stream_delete (stb);
#endif
print_doc_line (gdb_stdout, c->doc + 5);
#ifdef UI_OUT
ui_out_text (uiout, " is ");
ui_out_wrap_hint (uiout, " ");
quote = 0;
switch (c->var_type)
{
case var_string:
{
unsigned char *p;
if (*(unsigned char **) c->var)
fputstr_filtered (*(unsigned char **) c->var, '"', stb->stream);
quote = 1;
}
break;
case var_string_noescape:
case var_filename:
case var_enum:
if (*(char **) c->var)
fputs_filtered (*(char **) c->var, stb->stream);
quote = 1;
break;
case var_boolean:
fputs_filtered (*(int *) c->var ? "on" : "off", stb->stream);
break;
case var_auto_boolean:
switch (*(enum cmd_auto_boolean*) c->var)
{
case CMD_AUTO_BOOLEAN_TRUE:
fputs_filtered ("on", stb->stream);
break;
case CMD_AUTO_BOOLEAN_FALSE:
fputs_filtered ("off", stb->stream);
break;
case CMD_AUTO_BOOLEAN_AUTO:
fputs_filtered ("auto", stb->stream);
break;
default:
internal_error ("do_setshow_command: invalid var_auto_boolean");
break;
}
break;
case var_uinteger:
if (*(unsigned int *) c->var == UINT_MAX)
{
fputs_filtered ("unlimited", stb->stream);
break;
}
case var_zinteger:
fprintf_filtered (stb->stream, "%u", *(unsigned int *) c->var);
break;
case var_integer:
if (*(int *) c->var == INT_MAX)
{
fputs_filtered ("unlimited", stb->stream);
}
else
fprintf_filtered (stb->stream, "%d", *(int *) c->var);
break;
default:
error ("gdb internal error: bad var_type in do_setshow_command");
}
if (quote)
ui_out_text (uiout, "\"");
ui_out_field_stream (uiout, "value", stb);
if (quote)
ui_out_text (uiout, "\"");
ui_out_text (uiout, ".\n");
do_cleanups (old_chain);
#else
fputs_filtered (" is ", gdb_stdout);
wrap_here (" ");
switch (c->var_type)
{
case var_string:
{
fputs_filtered ("\"", gdb_stdout);
if (*(unsigned char **) c->var)
fputstr_filtered (*(unsigned char **) c->var, '"', gdb_stdout);
fputs_filtered ("\"", gdb_stdout);
}
break;
case var_string_noescape:
case var_filename:
case var_enum:
fputs_filtered ("\"", gdb_stdout);
if (*(char **) c->var)
fputs_filtered (*(char **) c->var, gdb_stdout);
fputs_filtered ("\"", gdb_stdout);
break;
case var_boolean:
fputs_filtered (*(int *) c->var ? "on" : "off", gdb_stdout);
break;
case var_auto_boolean:
switch (*(enum cmd_auto_boolean*) c->var)
{
case CMD_AUTO_BOOLEAN_TRUE:
fputs_filtered ("on", gdb_stdout);
break;
case CMD_AUTO_BOOLEAN_FALSE:
fputs_filtered ("off", gdb_stdout);
break;
case CMD_AUTO_BOOLEAN_AUTO:
fputs_filtered ("auto", gdb_stdout);
break;
default:
internal_error ("do_setshow_command: invalid var_auto_boolean");
break;
}
break;
case var_uinteger:
if (*(unsigned int *) c->var == UINT_MAX)
{
fputs_filtered ("unlimited", gdb_stdout);
break;
}
case var_zinteger:
fprintf_filtered (gdb_stdout, "%u", *(unsigned int *) c->var);
break;
case var_integer:
if (*(int *) c->var == INT_MAX)
{
fputs_filtered ("unlimited", gdb_stdout);
}
else
fprintf_filtered (gdb_stdout, "%d", *(int *) c->var);
break;
default:
error ("gdb internal error: bad var_type in do_setshow_command");
}
fputs_filtered (".\n", gdb_stdout);
#endif
}
else
error ("gdb internal error: bad cmd_type in do_setshow_command");
(*c->function.sfunc) (NULL, from_tty, c);
if (c->type == set_cmd && set_hook)
set_hook (c);
}
void
cmd_show_list (struct cmd_list_element *list, int from_tty, char *prefix)
{
#ifdef UI_OUT
ui_out_list_begin (uiout, "showlist");
#endif
for (; list != NULL; list = list->next)
{
#ifdef UI_OUT
if (list->prefixlist && !list->abbrev_flag)
{
ui_out_list_begin (uiout, "optionlist");
ui_out_field_string (uiout, "prefix", list->prefixname + 5);
cmd_show_list (*list->prefixlist, from_tty, list->prefixname + 5);
ui_out_list_end (uiout);
}
if (list->type == show_cmd)
{
ui_out_list_begin (uiout, "option");
ui_out_text (uiout, prefix);
ui_out_field_string (uiout, "name", list->name);
ui_out_text (uiout, ": ");
do_setshow_command ((char *) NULL, from_tty, list);
ui_out_list_end (uiout);
}
#else
if (list->prefixlist && !list->abbrev_flag)
cmd_show_list (*list->prefixlist, from_tty, list->prefixname + 5);
if (list->type == show_cmd)
{
fputs_filtered (prefix, gdb_stdout);
fputs_filtered (list->name, gdb_stdout);
fputs_filtered (": ", gdb_stdout);
do_setshow_command ((char *) NULL, from_tty, list);
}
#endif
}
#ifdef UI_OUT
ui_out_list_end (uiout);
#endif
}
static void
shell_escape (char *arg, int from_tty)
{
#ifdef CANT_FORK
int rc = system (arg ? arg : "");
if (!arg)
arg = "inferior shell";
if (rc == -1)
{
fprintf_unfiltered (gdb_stderr, "Cannot execute %s: %s\n", arg,
safe_strerror (errno));
gdb_flush (gdb_stderr);
}
else if (rc)
{
fprintf_unfiltered (gdb_stderr, "%s exited with status %d\n", arg, rc);
gdb_flush (gdb_stderr);
}
#ifdef __DJGPP__
chdir (current_directory);
#endif
#else
int rc, status, pid;
char *p, *user_shell;
if ((user_shell = (char *) getenv ("SHELL")) == NULL)
user_shell = "/bin/sh";
if ((p = strrchr (user_shell, '/')) == NULL)
p = user_shell;
else
p++;
if ((pid = fork ()) == 0)
{
if (!arg)
execl (user_shell, p, 0);
else
execl (user_shell, p, "-c", arg, 0);
fprintf_unfiltered (gdb_stderr, "Cannot execute %s: %s\n", user_shell,
safe_strerror (errno));
gdb_flush (gdb_stderr);
_exit (0177);
}
if (pid != -1)
while ((rc = waitpid (pid, &status, 0)) != pid && rc != -1)
;
else
error ("Fork failed");
#endif
}
static void
make_command (char *arg, int from_tty)
{
char *p;
if (arg == 0)
p = "make";
else
{
p = xmalloc (sizeof ("make ") + strlen (arg));
strcpy (p, "make ");
strcpy (p + sizeof ("make ") - 1, arg);
}
shell_escape (p, from_tty);
}
static void
show_user_1 (struct cmd_list_element *c, struct ui_file *stream)
{
register struct command_line *cmdlines;
cmdlines = c->user_commands;
if (!cmdlines)
return;
fputs_filtered ("User command ", stream);
fputs_filtered (c->name, stream);
fputs_filtered (":\n", stream);
#ifdef UI_OUT
print_command_lines (uiout, cmdlines, 1);
fputs_filtered ("\n", stream);
#else
while (cmdlines)
{
print_command_line (cmdlines, 4, stream);
cmdlines = cmdlines->next;
}
fputs_filtered ("\n", stream);
#endif
}
static void
show_user (char *args, int from_tty)
{
struct cmd_list_element *c;
extern struct cmd_list_element *cmdlist;
if (args)
{
c = lookup_cmd (&args, cmdlist, "", 0, 1);
if (c->class != class_user)
error ("Not a user command.");
show_user_1 (c, gdb_stdout);
}
else
{
for (c = cmdlist; c; c = c->next)
{
if (c->class == class_user)
show_user_1 (c, gdb_stdout);
}
}
}
void
_initialize_command (void)
{
add_com ("shell", class_support, shell_escape,
"Execute the rest of the line as a shell command. \n\
With no arguments, run an inferior shell.");
if (xdb_commands)
add_com_alias ("!", "shell", class_support, 0);
add_com ("make", class_support, make_command,
"Run the ``make'' program using the rest of the line as arguments.");
add_cmd ("user", no_class, show_user,
"Show definitions of user defined commands.\n\
Argument is the name of the user defined command.\n\
With no argument, show definitions of all user defined commands.", &showlist);
add_com ("apropos", class_support, apropos_command, "Search for commands matching a REGEXP");
}