- if (!c)
- /* He's typed something unrecognizable. Sigh. */
- list = (char **) 0;
- else if (c == (struct cmd_list_element *) -1)
- {
- /* If we didn't recognize everything up to the thing that
- needs completing, and we don't know what command it is
- yet, we are in trouble. Part of the trouble might be
- that the list of delimiters used by readline includes
- '-', which we use in commands. Check for this. */
- if (p + strlen(text) != tmp_command + rl_point) {
- if (tmp_command[rl_point - strlen(text) - 1] == '-')
- text = p;
- else {
- /* This really should not produce an error. Better would
- be to pretend to hit RETURN here; this would produce a
- response like "Ambiguous command: foo, foobar, etc",
- and leave the line available for re-entry with ^P. Instead,
- this error blows away the user's typed input without
- any way to get it back. */
- error (" Unrecognized command.");
- }
- }
-
- /* He's typed something ambiguous. This is easier. */
- if (result_list)
- list = complete_on_cmdlist (*result_list->prefixlist, text);
- else
- list = complete_on_cmdlist (cmdlist, text);
- }
- else
- {
- /* If we've gotten this far, gdb has recognized a full
- command. There are several possibilities:
-
- 1) We need to complete on the command.
- 2) We need to complete on the possibilities coming after
- the command.
- 2) We need to complete the text of what comes after the
- command. */
-
- if (!*p && *text)
- /* Always (might be longer versions of thie command). */
- list = complete_on_cmdlist (result_list, text);
- else if (!*p && !*text)
- {
- if (c->prefixlist)
- list = complete_on_cmdlist (*c->prefixlist, "");
- else
- list = (*c->completer) ("");
- }
- else
- {
- if (c->prefixlist && !c->allow_unknown)
- {
-#if 0
- /* Something like "info adsfkdj". But error() is not
- the proper response; just return no completions
- instead. */
- *p = '\0';
- error ("\"%s\" command requires a subcommand.",
- tmp_command);
-#else
- list = NULL;
-#endif
- }
- else
- list = (*c->completer) (text);
- }
- }
- }
-
- /* If the debugged program wasn't compiled with symbols, or if we're
- clearly completing on a command and no command matches, return
- NULL. */
- if (!list)
- return ((char *)NULL);
-
- output = list[index];
- if (output)
- index++;
-
- return (output);
-}
-\f
-#ifdef STOP_SIGNAL
-static void
-stop_sig ()
-{
-#if STOP_SIGNAL == SIGTSTP
- signal (SIGTSTP, SIG_DFL);
- sigsetmask (0);
- kill (getpid (), SIGTSTP);
- signal (SIGTSTP, stop_sig);
-#else
- signal (STOP_SIGNAL, stop_sig);
-#endif
- printf ("%s", prompt);
- fflush (stdout);
-
- /* Forget about any previous command -- null line now will do nothing. */
- dont_repeat ();
-}
-#endif /* STOP_SIGNAL */
-
-/* Initialize signal handlers. */
-static void
-do_nothing ()
-{
-}
-
-static void
-init_signals ()
-{
- extern void request_quit ();
-
- signal (SIGINT, request_quit);
-
- /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
- passed to the inferior, which we don't want. It would be
- possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
- on BSD4.3 systems using vfork, that can affect the
- GDB process as well as the inferior (the signal handling tables
- might be in memory, shared between the two). Since we establish
- a handler for SIGQUIT, when we call exec it will set the signal
- to SIG_DFL for us. */
- signal (SIGQUIT, do_nothing);
- if (signal (SIGHUP, do_nothing) != SIG_IGN)
- signal (SIGHUP, disconnect);
- signal (SIGFPE, float_handler);
-}
-\f
-/* Read one line from the command input stream `instream'
- into the local static buffer `linebuffer' (whose current length
- is `linelength').
- The buffer is made bigger as necessary.
- Returns the address of the start of the line.
-
- NULL is returned for end of file.
-
- *If* the instream == stdin & stdin is a terminal, the line read
- is copied into the file line saver (global var char *line,
- length linesize) so that it can be duplicated.
-
- This routine either uses fancy command line editing or
- simple input as the user has requested. */
-
-char *
-command_line_input (prrompt, repeat)
- char *prrompt;
- int repeat;
-{
- static char *linebuffer = 0;
- static int linelength = 0;
- register char *p;
- char *p1;
- char *rl;
- char *local_prompt = prrompt;
- register int c;
- char *nline;
- char got_eof = 0;
-
- if (linebuffer == 0)
- {
- linelength = 80;
- linebuffer = (char *) xmalloc (linelength);
- }
-
- p = linebuffer;
-
- /* Control-C quits instantly if typed while in this loop
- since it should not wait until the user types a newline. */
- immediate_quit++;
-#ifdef STOP_SIGNAL
- signal (STOP_SIGNAL, stop_sig);
-#endif
-
- while (1)
- {
- /* Reports are that some Sys V's don't flush stdout/err on reads
- from stdin, when stdin/out are sockets rather than ttys. So we
- have to do it ourselves, to make emacs-gdb and xxgdb work.
- On other machines, doing this once per input should be a cheap nop. */
- fflush (stdout);
- fflush (stderr);
-
- /* Don't use fancy stuff if not talking to stdin. */
- if (command_editing_p && instream == stdin
- && ISATTY (instream))
- rl = readline (local_prompt);
- else
- rl = gdb_readline (local_prompt);
-
- if (!rl || rl == (char *) EOF)
- {
- got_eof = 1;
- break;
- }
- if (strlen(rl) + 1 + (p - linebuffer) > linelength)
- {
- linelength = strlen(rl) + 1 + (p - linebuffer);
- nline = (char *) xrealloc (linebuffer, linelength);
- p += nline - linebuffer;
- linebuffer = nline;
- }
- p1 = rl;
- /* Copy line. Don't copy null at end. (Leaves line alone
- if this was just a newline) */
- while (*p1)
- *p++ = *p1++;
-
- free (rl); /* Allocated in readline. */
-
- if (p == linebuffer || *(p - 1) != '\\')
- break;
-
- p--; /* Put on top of '\'. */
- local_prompt = (char *) 0;
- }
-
-#ifdef STOP_SIGNAL
- signal (SIGTSTP, SIG_DFL);
-#endif
- immediate_quit--;
-
- if (got_eof)
- return NULL;
-
- /* 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 ("%s\n", history_value);
-
- /* If there was an error, call this function again. */
- if (expanded < 0)
- {
- free (history_value);
- return command_line_input (prrompt, repeat);
- }
- 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)
- {
- if (p == linebuffer)
- return line;
- p1 = linebuffer;
- while (*p1 == ' ' || *p1 == '\t')
- p1++;
- if (!*p1)
- return line;
- }
-
- *p = 0;
-
- /* Add line to history if appropriate. */
- if (instream == stdin
- && ISATTY (stdin) && *linebuffer)
- add_history (linebuffer);
-
- /* Note: lines consisting soley 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. */
- p1 = linebuffer;
- while ((c = *p1++) != '\0')
- {
- if (c == '"')
- while ((c = *p1++) != '"')
- {
- /* Make sure an escaped '"' doesn't make us think the string
- is ended. */
- if (c == '\\')
- parse_escape (&p1);
- if (c == '\0')
- break;
- }
- else if (c == '\'')
- while ((c = *p1++) != '\'')
- {
- /* Make sure an escaped '\'' doesn't make us think the string
- is ended. */
- if (c == '\\')
- parse_escape (&p1);
- if (c == '\0')
- break;
- }
- else if (c == '#')
- {
- /* Found a comment. */
- p1[-1] = '\0';
- break;
- }
- }
-
- /* 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
-/* Read lines from the input stream
- and accumulate them in a chain of struct command_line's
- which is then returned. */
-
-struct command_line *
-read_command_lines ()
-{
- struct command_line *first = 0;
- register struct command_line *next, *tail = 0;
- register char *p, *p1;
- struct cleanup *old_chain = 0;
-
- while (1)
- {
- dont_repeat ();
- p = command_line_input (0, instream == stdin);
- if (p == NULL)
- /* Treat end of file like "end". */
- break;
-
- /* Remove leading and trailing blanks. */
- while (*p == ' ' || *p == '\t') p++;
- p1 = p + strlen (p);
- while (p1 != p && (p1[-1] == ' ' || p1[-1] == '\t')) p1--;
-
- /* Is this "end"? */
- if (p1 - p == 3 && !strncmp (p, "end", 3))
- break;
-
- /* No => add this line to the chain of command lines. */
- next = (struct command_line *) xmalloc (sizeof (struct command_line));
- next->line = savestring (p, p1 - p);
- next->next = 0;
- if (tail)
- {
- tail->next = next;
- }
- else
- {
- /* We just read the first line.
- From now on, arrange to throw away the lines we have
- if we quit or get an error while inside this function. */
- first = next;
- old_chain = make_cleanup (free_command_lines, &first);
- }
- tail = next;
- }
-
- dont_repeat ();
-
- /* Now we are about to return the chain to our caller,
- so freeing it becomes his responsibility. */
- if (first)
- discard_cleanups (old_chain);
- return first;
-}
-
-/* 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;
-
- while (l)
- {
- next = l->next;
- free (l->line);
- free (l);
- l = next;
- }
-}
-\f
-/* Add an element to the list of info subcommands. */
-
-void
-add_info (name, fun, doc)
- char *name;
- void (*fun) ();
- 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 ("\"info\" must be followed by the name of an info command.\n");
- help_list (infolist, "info ", -1, stdout);
-}
-
-/* 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) ();
- 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, 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 (!(*p >= 'A' && *p <= 'Z')
- && !(*p >= 'a' && *p <= 'z')
- && !(*p >= '0' && *p <= '9')
- && *p != '-')
- error ("Junk in argument list: \"%s\"", p);
- p++;
- }
-}
-
-static void
-define_command (comname, from_tty)
- char *comname;
- int from_tty;
-{
- register struct command_line *cmds;
- register struct cmd_list_element *c, *newc;
- char *tem = comname;
- extern void not_just_help_class_command ();
-
- validate_comname (comname);
-
- /* Look it up, and verify that we got an exact match. */
- c = lookup_cmd (&tem, cmdlist, "", -1, 1);
- if (c && 0 != strcmp (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, comname))
- error ("Command \"%s\" not redefined.", comname);
- }
-
- if (from_tty)
- {
- printf ("Type commands for definition of \"%s\".\n\
-End with a line saying just \"end\".\n", comname);
- fflush (stdout);
- }
- comname = savestring (comname, strlen (comname));
-
- cmds = read_command_lines ();
-
- if (c && c->class == class_user)
- free_command_lines (&c->user_commands);
-
- newc = add_cmd (comname, class_user, not_just_help_class_command,
- (c && c->class == class_user)
- ? c->doc : savestring ("User-defined.", 13), &cmdlist);
- newc->user_commands = cmds;
-}
-
-static void
-document_command (comname, from_tty)
- char *comname;
- int from_tty;
-{
- struct command_line *doclines;
- register struct cmd_list_element *c;
- char *tem = comname;
-
- validate_comname (comname);
-
- c = lookup_cmd (&tem, cmdlist, "", 0, 1);
-
- if (c->class != class_user)
- error ("Command \"%s\" is built-in.", comname);
-
- if (from_tty)
- printf ("Type documentation for \"%s\".\n\
-End with a line saying just \"end\".\n", comname);
-
- doclines = read_command_lines ();
-
- if (c->doc) free (c->doc);
-
- {
- register struct command_line *cl1;
- register int len = 0;
-
- for (cl1 = doclines; cl1; cl1 = cl1->next)
- len += strlen (cl1->line) + 1;
-
- c->doc = (char *) xmalloc (len + 1);
- *c->doc = 0;
-
- for (cl1 = doclines; cl1; cl1 = cl1->next)
- {
- strcat (c->doc, cl1->line);
- if (cl1->next)
- strcat (c->doc, "\n");
- }
- }
-
- free_command_lines (&doclines);
-}
-\f
-static void
-print_gnu_advertisement()
-{
- printf ("\
-GDB is free software and you are welcome to distribute copies of it\n\
- under certain conditions; type \"show copying\" to see the conditions.\n\
-There is absolutely no warranty for GDB; type \"show warranty\" for details.\n\
-");
-}
-
-static void
-print_gdb_version ()
-{
- printf_filtered ("\
-GDB %s, Copyright 1992 Free Software Foundation, Inc.",
- version);
-}
-
-/* ARGSUSED */
-static void
-show_version (args, from_tty)
- char *args;
- int from_tty;
-{
- immediate_quit++;
- print_gnu_advertisement ();
- print_gdb_version ();
- printf_filtered ("\n");
- immediate_quit--;
-}
-\f
-/* xgdb calls this to reprint the usual GDB prompt. */
-
-void
-print_prompt ()
-{
- printf ("%s", prompt);
- fflush (stdout);
-}
-\f
-static void
-quit_command (args, from_tty)
- char *args;
- int from_tty;
-{
- if (inferior_pid != 0 && target_has_execution)
- {
- if (query ("The program is running. Quit anyway? "))
- {
- target_kill ();
- }
- else
- error ("Not confirmed.");
- }
- /* Save the history information if it is appropriate to do so. */
- if (write_history_p && history_filename)
- write_history (history_filename);
- exit (0);
-}
-
-/* Returns whether GDB is running on a terminal and whether the user
- desires that questions be asked of them on that terminal. */
-
-int
-input_from_terminal_p ()
-{
- return gdb_has_a_terminal && (instream == stdin) & caution;
-}
-\f
-/* ARGSUSED */
-static void
-pwd_command (args, from_tty)
- char *args;
- int from_tty;
-{
- if (args) error ("The \"pwd\" command does not take an argument: %s", args);
- getcwd (dirbuf, sizeof (dirbuf));
-
- if (strcmp (dirbuf, current_directory))
- printf ("Working directory %s\n (canonically %s).\n",
- current_directory, dirbuf);
- else
- printf ("Working directory %s.\n", current_directory);
-}
-
-static void
-cd_command (dir, from_tty)
- char *dir;
- int from_tty;
-{
- int len;
- int change;
-
- /* If the new directory is absolute, repeat is a no-op; if relative,
- repeat might be useful but is more likely to be a mistake. */
- dont_repeat ();
-
- if (dir == 0)
- error_no_arg ("new working directory");
-
- dir = tilde_expand (dir);
- make_cleanup (free, dir);
-
- if (chdir (dir) < 0)
- perror_with_name (dir);
-
- len = strlen (dir);
- dir = savestring (dir, len - (len > 1 && dir[len-1] == '/'));
- if (dir[0] == '/')
- current_directory = dir;
- else
- {
- current_directory = concat (current_directory, "/", dir, NULL);
- free (dir);
- }
-
- /* Now simplify any occurrences of `.' and `..' in the pathname. */
-
- change = 1;
- while (change)
- {
- char *p;
- change = 0;
-
- for (p = current_directory; *p;)
- {
- if (!strncmp (p, "/./", 2)
- && (p[2] == 0 || p[2] == '/'))
- strcpy (p, p + 2);
- else if (!strncmp (p, "/..", 3)
- && (p[3] == 0 || p[3] == '/')
- && p != current_directory)
- {
- char *q = p;
- while (q != current_directory && q[-1] != '/') q--;
- if (q != current_directory)
- {
- strcpy (q-1, p+3);
- p = q-1;
- }
- }
- else p++;
- }
- }
-
- forget_cached_source_info ();
-
- if (from_tty)
- pwd_command ((char *) 0, 1);
-}
-\f
-/* ARGSUSED */