- return linebuffer + SERVER_COMMAND_LENGTH;
- }
-
- /* Do history expansion if that is wished. */
- if (history_expansion_p && instream == stdin
- && ISATTY (instream))
- {
- char *history_value;
- int expanded;
-
- *p = '\0'; /* Insert null now. */
- expanded = history_expand (linebuffer, &history_value);
- if (expanded)
- {
- /* Print the changes. */
- printf_unfiltered ("%s\n", history_value);
-
- /* If there was an error, call this function again. */
- if (expanded < 0)
- {
- free (history_value);
- return command_line_input (prompt_arg, repeat, annotation_suffix);
- }
- if (strlen (history_value) > linelength)
- {
- linelength = strlen (history_value) + 1;
- linebuffer = (char *) xrealloc (linebuffer, linelength);
- }
- strcpy (linebuffer, history_value);
- p = linebuffer + strlen (linebuffer);
- free (history_value);
- }
- }
-
- /* If we just got an empty line, and that is supposed
- to repeat the previous command, return the value in the
- global buffer. */
- if (repeat && p == linebuffer)
- return line;
- for (p1 = linebuffer; *p1 == ' ' || *p1 == '\t'; p1++);
- if (repeat && !*p1)
- return line;
-
- *p = 0;
-
- /* Add line to history if appropriate. */
- if (instream == stdin
- && ISATTY (stdin) && *linebuffer)
- add_history (linebuffer);
-
- /* Note: lines consisting solely of comments are added to the command
- history. This is useful when you type a command, and then
- realize you don't want to execute it quite yet. You can comment
- out the command and then later fetch it from the value history
- and remove the '#'. The kill ring is probably better, but some
- people are in the habit of commenting things out. */
- if (*p1 == '#')
- *p1 = '\0'; /* Found a comment. */
-
- /* Save into global buffer if appropriate. */
- if (repeat)
- {
- if (linelength > linesize)
- {
- line = xrealloc (line, linelength);
- linesize = linelength;
- }
- strcpy (line, linebuffer);
- return line;
- }
-
- return linebuffer;
-}
-\f
-
-/* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
- code bodies. This is typically used when we encounter an "else"
- clause for an "if" command. */
-
-static void
-realloc_body_list (command, new_length)
- struct command_line *command;
- int new_length;
-{
- int n;
- struct command_line **body_list;
-
- n = command->body_count;
-
- /* Nothing to do? */
- if (new_length <= n)
- return;
-
- body_list = (struct command_line **)
- xmalloc (sizeof (struct command_line *) * new_length);
-
- memcpy (body_list, command->body_list, sizeof (struct command_line *) * n);
-
- free (command->body_list);
- command->body_list = body_list;
- command->body_count = new_length;
-}
-
-/* Read one line from the input stream. If the command is an "else" or
- "end", return such an indication to the caller. */
-
-static enum misc_command_type
-read_next_line (command)
- struct command_line **command;
-{
- char *p, *p1, *prompt_ptr, control_prompt[256];
- int i = 0;
-
- if (control_level >= 254)
- error ("Control nesting too deep!\n");
-
- /* Set a prompt based on the nesting of the control commands. */
- if (instream == stdin || (instream == 0 && readline_hook != NULL))
- {
- for (i = 0; i < control_level; i++)
- control_prompt[i] = ' ';
- control_prompt[i] = '>';
- control_prompt[i + 1] = '\0';
- prompt_ptr = (char *) &control_prompt[0];
- }
- else
- prompt_ptr = NULL;
-
- p = command_line_input (prompt_ptr, instream == stdin, "commands");
-
- /* Not sure what to do here. */
- if (p == NULL)
- return end_command;
-
- /* Strip leading and trailing whitespace. */
- while (*p == ' ' || *p == '\t')
- p++;
-
- p1 = p + strlen (p);
- while (p1 != p && (p1[-1] == ' ' || p1[-1] == '\t'))
- p1--;
-
- /* Blanks and comments don't really do anything, but we need to
- distinguish them from else, end and other commands which can be
- executed. */
- if (p1 == p || p[0] == '#')
- return nop_command;
-
- /* Is this the end of a simple, while, or if control structure? */
- if (p1 - p == 3 && !strncmp (p, "end", 3))
- return end_command;
-
- /* Is the else clause of an if control structure? */
- if (p1 - p == 4 && !strncmp (p, "else", 4))
- return else_command;
-
- /* Check for while, if, break, continue, etc and build a new command
- line structure for them. */
- if (p1 - p > 5 && !strncmp (p, "while", 5))
- *command = build_command_line (while_control, p + 6);
- else if (p1 - p > 2 && !strncmp (p, "if", 2))
- *command = build_command_line (if_control, p + 3);
- else if (p1 - p == 10 && !strncmp (p, "loop_break", 10))
- {
- *command = (struct command_line *)
- xmalloc (sizeof (struct command_line));
- (*command)->next = NULL;
- (*command)->line = NULL;
- (*command)->control_type = break_control;
- (*command)->body_count = 0;
- (*command)->body_list = NULL;
- }
- else if (p1 - p == 13 && !strncmp (p, "loop_continue", 13))
- {
- *command = (struct command_line *)
- xmalloc (sizeof (struct command_line));
- (*command)->next = NULL;
- (*command)->line = NULL;
- (*command)->control_type = continue_control;
- (*command)->body_count = 0;
- (*command)->body_list = NULL;
- }
- else
- {
- /* A normal command. */
- *command = (struct command_line *)
- xmalloc (sizeof (struct command_line));
- (*command)->next = NULL;
- (*command)->line = savestring (p, p1 - p);
- (*command)->control_type = simple_control;
- (*command)->body_count = 0;
- (*command)->body_list = NULL;
- }
-
- /* Nothing special. */
- return ok_command;
-}
-
-/* Recursively read in the control structures and create a command_line
- structure from them.
-
- The parent_control parameter is the control structure in which the
- following commands are nested. */
-
-static enum command_control_type
-recurse_read_control_structure (current_cmd)
- struct command_line *current_cmd;
-{
- int current_body, i;
- enum misc_command_type val;
- enum command_control_type ret;
- struct command_line **body_ptr, *child_tail, *next;
-
- child_tail = NULL;
- current_body = 1;
-
- /* Sanity checks. */
- if (current_cmd->control_type == simple_control)
- {
- error ("Recursed on a simple control type\n");
- return invalid_control;
- }
-
- if (current_body > current_cmd->body_count)
- {
- error ("Allocated body is smaller than this command type needs\n");
- return invalid_control;
- }
-
- /* Read lines from the input stream and build control structures. */
- while (1)
- {
- dont_repeat ();
-
- next = NULL;
- val = read_next_line (&next);
-
- /* Just skip blanks and comments. */
- if (val == nop_command)
- continue;
-
- if (val == end_command)
- {
- if (current_cmd->control_type == while_control
- || current_cmd->control_type == if_control)
- {
- /* Success reading an entire control structure. */
- ret = simple_control;
- break;
- }
- else
- {
- ret = invalid_control;
- break;
- }
- }
-
- /* Not the end of a control structure. */
- if (val == else_command)
- {
- if (current_cmd->control_type == if_control
- && current_body == 1)
- {
- realloc_body_list (current_cmd, 2);
- current_body = 2;
- child_tail = NULL;
- continue;
- }
- else
- {
- ret = invalid_control;
- break;
- }
- }
-
- if (child_tail)
- {
- child_tail->next = next;
- }
- else
- {
- body_ptr = current_cmd->body_list;
- for (i = 1; i < current_body; i++)
- body_ptr++;
-
- *body_ptr = next;
-
- }
-
- child_tail = next;
-
- /* If the latest line is another control structure, then recurse
- on it. */
- if (next->control_type == while_control
- || next->control_type == if_control)
- {
- control_level++;
- ret = recurse_read_control_structure (next);
- control_level--;
-
- if (ret != simple_control)
- break;
- }
- }
-
- dont_repeat ();
-
- return ret;
-}
-
-/* Read lines from the input stream and accumulate them in a chain of
- struct command_line's, which is then returned. For input from a
- terminal, the special command "end" is used to mark the end of the
- input, and is not included in the returned chain of commands. */
-
-#define END_MESSAGE "End with a line saying just \"end\"."
-
-struct command_line *
-read_command_lines (prompt_arg, from_tty)
- char *prompt_arg;
- int from_tty;
-{
- struct command_line *head, *tail, *next;
- struct cleanup *old_chain;
- enum command_control_type ret;
- enum misc_command_type val;
-
- control_level = 0;
- if (readline_begin_hook)
- {
- /* Note - intentional to merge messages with no newline */
- (*readline_begin_hook) ("%s %s\n", prompt_arg, END_MESSAGE);
- }
- else if (from_tty && input_from_terminal_p ())
- {
- printf_unfiltered ("%s\n%s\n", prompt_arg, END_MESSAGE);
- gdb_flush (gdb_stdout);
- }
-
- head = tail = NULL;
- old_chain = NULL;
-
- while (1)
- {
- val = read_next_line (&next);
-
- /* Ignore blank lines or comments. */
- if (val == nop_command)
- continue;
-
- if (val == end_command)
- {
- ret = simple_control;
- break;
- }
-
- if (val != ok_command)
- {
- ret = invalid_control;
- break;
- }
-
- if (next->control_type == while_control
- || next->control_type == if_control)
- {
- control_level++;
- ret = recurse_read_control_structure (next);
- control_level--;
-
- if (ret == invalid_control)
- break;
- }
-
- if (tail)
- {
- tail->next = next;
- }
- else
- {
- head = next;
- old_chain = make_cleanup ((make_cleanup_func) free_command_lines,
- &head);
- }
- tail = next;
- }
-
- dont_repeat ();
-
- if (head)
- {
- if (ret != invalid_control)
- {
- discard_cleanups (old_chain);
- }
- else
- do_cleanups (old_chain);
- }
-
- if (readline_end_hook)
- {
- (*readline_end_hook) ();
- }
- return (head);
-}
-
-/* Free a chain of struct command_line's. */
-
-void
-free_command_lines (lptr)
- struct command_line **lptr;
-{
- register struct command_line *l = *lptr;
- register struct command_line *next;
- struct command_line **blist;
- int i;
-
- while (l)
- {
- if (l->body_count > 0)
- {
- blist = l->body_list;
- for (i = 0; i < l->body_count; i++, blist++)
- free_command_lines (blist);
- }
- next = l->next;
- free (l->line);
- free ((PTR) l);
- l = next;
- }
-}
-\f
-/* Add an element to the list of info subcommands. */
-
-void
-add_info (name, fun, doc)
- char *name;
- void (*fun) PARAMS ((char *, int));
- char *doc;
-{
- add_cmd (name, no_class, fun, doc, &infolist);
-}
-
-/* Add an alias to the list of info subcommands. */
-
-void
-add_info_alias (name, oldname, abbrev_flag)
- char *name;
- char *oldname;
- int abbrev_flag;
-{
- add_alias_cmd (name, oldname, 0, abbrev_flag, &infolist);
-}
-
-/* The "info" command is defined as a prefix, with allow_unknown = 0.
- Therefore, its own definition is called only for "info" with no args. */
-
-/* ARGSUSED */
-static void
-info_command (arg, from_tty)
- char *arg;
- int from_tty;
-{
- printf_unfiltered ("\"info\" must be followed by the name of an info command.\n");
- help_list (infolist, "info ", -1, gdb_stdout);
-}
-
-/* The "complete" command is used by Emacs to implement completion. */
-
-/* ARGSUSED */
-static void
-complete_command (arg, from_tty)
- char *arg;
- int from_tty;
-{
- int i;
- int argpoint;
- char *completion;
-
- dont_repeat ();
-
- if (arg == NULL)
- arg = "";
- argpoint = strlen (arg);
-
- for (completion = line_completion_function (arg, i = 0, arg, argpoint);
- completion;
- completion = line_completion_function (arg, ++i, arg, argpoint))
- {
- printf_unfiltered ("%s\n", completion);
- free (completion);
- }
-}
-
-/* The "show" command with no arguments shows all the settings. */
-
-/* ARGSUSED */
-static void
-show_command (arg, from_tty)
- char *arg;
- int from_tty;
-{
- cmd_show_list (showlist, from_tty, "");
-}
-\f
-/* Add an element to the list of commands. */
-
-void
-add_com (name, class, fun, doc)
- char *name;
- enum command_class class;
- void (*fun) PARAMS ((char *, int));
- char *doc;
-{
- add_cmd (name, class, fun, doc, &cmdlist);
-}
-
-/* Add an alias or abbreviation command to the list of commands. */
-
-void
-add_com_alias (name, oldname, class, abbrev_flag)
- char *name;
- char *oldname;
- enum command_class class;
- int abbrev_flag;
-{
- add_alias_cmd (name, oldname, class, abbrev_flag, &cmdlist);
-}
-
-void
-error_no_arg (why)
- char *why;
-{
- error ("Argument required (%s).", why);
-}
-
-/* ARGSUSED */
-static void
-help_command (command, from_tty)
- char *command;
- int from_tty; /* Ignored */
-{
- help_cmd (command, gdb_stdout);
-}
-\f
-static void
-validate_comname (comname)
- char *comname;
-{
- register char *p;
-
- if (comname == 0)
- error_no_arg ("name of command to define");
-
- p = comname;
- while (*p)
- {
- if (!isalnum (*p) && *p != '-' && *p != '_')
- error ("Junk in argument list: \"%s\"", p);
- p++;
- }
-}
-
-/* This is just a placeholder in the command data structures. */
-static void
-user_defined_command (ignore, from_tty)
- char *ignore;
- int from_tty;
-{
-}
-
-static void
-define_command (comname, from_tty)
- char *comname;
- int from_tty;
-{
- register struct command_line *cmds;
- register struct cmd_list_element *c, *newc, *hookc = 0;
- char *tem = comname;
- char tmpbuf[128];
-#define HOOK_STRING "hook-"
-#define HOOK_LEN 5
-
- validate_comname (comname);
-
- /* Look it up, and verify that we got an exact match. */
- c = lookup_cmd (&tem, cmdlist, "", -1, 1);
- if (c && !STREQ (comname, c->name))
- c = 0;
-
- if (c)
- {
- if (c->class == class_user || c->class == class_alias)
- tem = "Redefine command \"%s\"? ";
- else
- tem = "Really redefine built-in command \"%s\"? ";
- if (!query (tem, c->name))
- error ("Command \"%s\" not redefined.", c->name);