1 /* readline.c -- a general facility for reading lines of input
2 with emacs style editing and completion. */
4 /* Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
6 This file contains the Readline Library (the Library), a set of
7 routines for providing Emacs style line input to programs that ask
10 The Library is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 The Library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
24 /* Remove these declarations when we have a complete libgnu.a. */
25 /* #define STATIC_MALLOC */
26 #if !defined (STATIC_MALLOC)
27 extern char *xmalloc (), *xrealloc ();
29 static char *xmalloc (), *xrealloc ();
30 #endif /* STATIC_MALLOC */
33 #include <sys/types.h>
41 #if defined (HAVE_UNISTD_H)
45 #define NEW_TTY_DRIVER
46 #define HAVE_BSD_SIGNALS
47 /* #define USE_XON_XOFF */
49 /* Some USG machines have BSD signal handling (sigblock, sigsetmask, etc.) */
50 #if defined (USG) && !defined (hpux)
51 #undef HAVE_BSD_SIGNALS
54 /* System V machines use termio. */
55 #if !defined (_POSIX_VERSION)
56 # if defined (USG) || defined (hpux) || defined (Xenix) || defined (sgi) || defined (DGUX)
57 # undef NEW_TTY_DRIVER
58 # define TERMIO_TTY_DRIVER
63 # endif /* USG || hpux || Xenix || sgi || DUGX */
64 #endif /* !_POSIX_VERSION */
66 /* Posix systems use termios and the Posix signal functions. */
67 #if defined (_POSIX_VERSION)
68 # if !defined (TERMIOS_MISSING)
69 # undef NEW_TTY_DRIVER
70 # define TERMIOS_TTY_DRIVER
72 # endif /* !TERMIOS_MISSING */
73 # define HAVE_POSIX_SIGNALS
74 # if !defined (O_NDELAY)
75 # define O_NDELAY O_NONBLOCK /* Posix-style non-blocking i/o */
76 # endif /* O_NDELAY */
77 #endif /* _POSIX_VERSION */
79 /* Other (BSD) machines use sgtty. */
80 #if defined (NEW_TTY_DRIVER)
84 /* Define _POSIX_VDISABLE if we are not using the `new' tty driver and
85 it is not already defined. It is used both to determine if a
86 special character is disabled and to disable certain special
87 characters. Posix systems should set to 0, USG systems to -1. */
88 #if !defined (NEW_TTY_DRIVER) && !defined (_POSIX_VDISABLE)
89 # if defined (_POSIX_VERSION)
90 # define _POSIX_VDISABLE 0
91 # else /* !_POSIX_VERSION */
92 # define _POSIX_VDISABLE -1
93 # endif /* !_POSIX_VERSION */
94 #endif /* !NEW_TTY_DRIVER && !_POSIX_VDISABLE */
100 #include <sys/stat.h>
102 /* Posix macro to check file in statbuf for directory-ness. */
103 #if defined (S_IFDIR) && !defined (S_ISDIR)
104 #define S_ISDIR(m) (((m)&S_IFMT) == S_IFDIR)
107 /* These next are for filename completion. Perhaps this belongs
108 in a different place. */
110 #if defined (USG) && !defined (isc386) && !defined (sgi)
111 struct passwd *getpwuid (), *getpwent ();
114 /* #define HACK_TERMCAP_MOTION */
116 /* Some standard library routines. */
117 #include "readline.h"
121 #define digit(c) ((c) >= '0' && (c) <= '9')
125 #define isletter(c) (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z'))
129 #define digit_value(c) ((c) - '0')
133 #define member(c, s) ((c) ? index ((s), (c)) : 0)
137 #define isident(c) ((isletter(c) || digit(c) || c == '_'))
141 #define exchange(x, y) {int temp = x; x = y; y = temp;}
144 #if !defined (rindex)
145 extern char *rindex ();
149 extern char *index ();
152 extern char *getenv ();
153 extern char *tilde_expand ();
155 static update_line ();
156 static void output_character_function ();
157 static delete_chars ();
158 static insert_some_chars ();
160 #if defined (VOID_SIGHANDLER)
161 # define sighandler void
163 # define sighandler int
164 #endif /* VOID_SIGHANDLER */
166 /* This typedef is equivalant to the one for Function; it allows us
167 to say SigHandler *foo = signal (SIGKILL, SIG_IGN); */
168 typedef sighandler SigHandler ();
170 /* If on, then readline handles signals in a way that doesn't screw. */
171 #define HANDLE_SIGNALS
174 /* **************************************************************** */
176 /* Line editing input utility */
178 /* **************************************************************** */
180 /* A pointer to the keymap that is currently in use.
181 By default, it is the standard emacs keymap. */
182 Keymap keymap = emacs_standard_keymap;
188 /* The current style of editing. */
189 int rl_editing_mode = emacs_mode;
191 /* Non-zero if the previous command was a kill command. */
192 static int last_command_was_kill = 0;
194 /* The current value of the numeric argument specified by the user. */
195 int rl_numeric_arg = 1;
197 /* Non-zero if an argument was typed. */
198 int rl_explicit_arg = 0;
200 /* Temporary value used while generating the argument. */
203 /* Non-zero means we have been called at least once before. */
204 static int rl_initialized = 0;
206 /* If non-zero, this program is running in an EMACS buffer. */
207 static char *running_in_emacs = (char *)NULL;
209 /* The current offset in the current input line. */
212 /* Mark in the current input line. */
215 /* Length of the current input line. */
218 /* Make this non-zero to return the current input_line. */
221 /* The last function executed by readline. */
222 Function *rl_last_func = (Function *)NULL;
224 /* Top level environment for readline_internal (). */
225 static jmp_buf readline_top_level;
227 /* The streams we interact with. */
228 static FILE *in_stream, *out_stream;
230 /* The names of the streams that we do input and output to. */
231 FILE *rl_instream = stdin, *rl_outstream = stdout;
233 /* Non-zero means echo characters as they are read. */
234 int readline_echoing_p = 1;
236 /* Current prompt. */
239 /* The number of characters read in order to type this complete command. */
240 int rl_key_sequence_length = 0;
242 /* If non-zero, then this is the address of a function to call just
243 before readline_internal () prints the first prompt. */
244 Function *rl_startup_hook = (Function *)NULL;
246 /* If non-zero, then this is the address of a function to call when
247 completing on a directory name. The function is called with
248 the address of a string (the current directory name) as an arg. */
249 Function *rl_symbolic_link_hook = (Function *)NULL;
251 /* What we use internally. You should always refer to RL_LINE_BUFFER. */
252 static char *the_line;
254 /* The character that can generate an EOF. Really read from
255 the terminal driver... just defaulted here. */
256 static int eof_char = CTRL ('D');
258 /* Non-zero makes this the next keystroke to read. */
259 int rl_pending_input = 0;
261 /* Pointer to a useful terminal name. */
262 char *rl_terminal_name = (char *)NULL;
264 /* Line buffer and maintenence. */
265 char *rl_line_buffer = (char *)NULL;
266 int rl_line_buffer_len = 0;
267 #define DEFAULT_BUFFER_SIZE 256
270 /* **************************************************************** */
272 /* `Forward' declarations */
274 /* **************************************************************** */
276 /* Non-zero means do not parse any lines other than comments and
277 parser directives. */
278 static unsigned char parsing_conditionalized_out = 0;
280 /* Caseless strcmp (). */
281 static int stricmp (), strnicmp ();
283 /* Non-zero means to save keys that we dispatch on in a kbd macro. */
284 static int defining_kbd_macro = 0;
287 /* **************************************************************** */
289 /* Top Level Functions */
291 /* **************************************************************** */
293 static void rl_prep_terminal (), rl_deprep_terminal ();
295 /* Read a line of input. Prompt with PROMPT. A NULL PROMPT means
296 none. A return value of NULL means that EOF was encountered. */
301 char *readline_internal ();
306 /* If we are at EOF return a NULL string. */
307 if (rl_pending_input == EOF)
309 rl_pending_input = 0;
310 return ((char *)NULL);
316 #if defined (HANDLE_SIGNALS)
320 value = readline_internal ();
321 rl_deprep_terminal ();
323 #if defined (HANDLE_SIGNALS)
330 /* Read a line of input from the global rl_instream, doing output on
331 the global rl_outstream.
332 If rl_prompt is non-null, then that is our prompt. */
336 int lastc, c, eof_found;
338 in_stream = rl_instream;
339 out_stream = rl_outstream;
345 (*rl_startup_hook) ();
347 if (!readline_echoing_p)
351 fprintf (out_stream, "%s", rl_prompt);
359 #if defined (VI_MODE)
360 if (rl_editing_mode == vi_mode)
361 rl_vi_insertion_mode ();
367 int lk = last_command_was_kill;
368 int code = setjmp (readline_top_level);
373 if (!rl_pending_input)
375 /* Then initialize the argument and number of keys read. */
377 rl_key_sequence_length = 0;
382 /* EOF typed to a non-blank line is a <NL>. */
383 if (c == EOF && rl_end)
386 /* The character eof_char typed to blank line, and not as the
387 previous character is interpreted as EOF. */
388 if (((c == eof_char && lastc != c) || c == EOF) && !rl_end)
395 rl_dispatch (c, keymap);
397 /* If there was no change in last_command_was_kill, then no kill
398 has taken place. Note that if input is pending we are reading
399 a prefix command, so nothing has changed yet. */
400 if (!rl_pending_input)
402 if (lk == last_command_was_kill)
403 last_command_was_kill = 0;
406 #if defined (VI_MODE)
407 /* In vi mode, when you exit insert mode, the cursor moves back
408 over the previous character. We explicitly check for that here. */
409 if (rl_editing_mode == vi_mode && keymap == vi_movement_keymap)
417 /* Restore the original of this history line, iff the line that we
418 are editing was originally in the history, AND the line has changed. */
420 HIST_ENTRY *entry = current_history ();
422 if (entry && rl_undo_list)
424 char *temp = savestring (the_line);
426 entry = replace_history_entry (where_history (), the_line,
428 free_history_entry (entry);
430 strcpy (the_line, temp);
435 /* At any rate, it is highly likely that this line has an undo list. Get
443 return (savestring (the_line));
447 /* **************************************************************** */
449 /* Signal Handling */
451 /* **************************************************************** */
453 #if defined (SIGWINCH)
454 static SigHandler *old_sigwinch = (SigHandler *)NULL;
457 rl_handle_sigwinch (sig)
462 term = rl_terminal_name;
464 if (readline_echoing_p)
467 term = getenv ("TERM");
470 rl_reset_terminal (term);
473 rl_forced_update_display ();
478 old_sigwinch != (SigHandler *)SIG_IGN &&
479 old_sigwinch != (SigHandler *)SIG_DFL)
480 (*old_sigwinch) (sig);
481 #if !defined (VOID_SIGHANDLER)
483 #endif /* VOID_SIGHANDLER */
485 #endif /* SIGWINCH */
487 #if defined (HANDLE_SIGNALS)
488 /* Interrupt handling. */
490 *old_int = (SigHandler *)NULL,
491 *old_tstp = (SigHandler *)NULL,
492 *old_ttou = (SigHandler *)NULL,
493 *old_ttin = (SigHandler *)NULL,
494 *old_cont = (SigHandler *)NULL,
495 *old_alrm = (SigHandler *)NULL;
497 /* Handle an interrupt character. */
499 rl_signal_handler (sig)
502 #if !defined (HAVE_BSD_SIGNALS)
503 /* Since the signal will not be blocked while we are in the signal
504 handler, ignore it until rl_clear_signals resets the catcher. */
506 signal (sig, SIG_IGN);
507 #endif /* !HAVE_BSD_SIGNALS */
516 #if defined (SIGTSTP)
522 rl_clean_up_for_exit ();
523 rl_deprep_terminal ();
525 rl_pending_input = 0;
527 kill (getpid (), sig);
529 #if defined (HAVE_POSIX_SIGNALS)
534 sigprocmask (SIG_SETMASK, &set, (sigset_t *)NULL);
537 #if defined (HAVE_BSD_SIGNALS)
539 #endif /* HAVE_BSD_SIGNALS */
540 #endif /* HAVE_POSIX_SIGNALS */
546 #if !defined (VOID_SIGHANDLER)
548 #endif /* !VOID_SIGHANDLER */
553 old_int = (SigHandler *)signal (SIGINT, rl_signal_handler);
554 if (old_int == (SigHandler *)SIG_IGN)
555 signal (SIGINT, SIG_IGN);
557 old_alrm = (SigHandler *)signal (SIGALRM, rl_signal_handler);
558 if (old_alrm == (SigHandler *)SIG_IGN)
559 signal (SIGALRM, SIG_IGN);
561 #if defined (SIGTSTP)
562 old_tstp = (SigHandler *)signal (SIGTSTP, rl_signal_handler);
563 if (old_tstp == (SigHandler *)SIG_IGN)
564 signal (SIGTSTP, SIG_IGN);
566 #if defined (SIGTTOU)
567 old_ttou = (SigHandler *)signal (SIGTTOU, rl_signal_handler);
568 old_ttin = (SigHandler *)signal (SIGTTIN, rl_signal_handler);
570 if (old_tstp == (SigHandler *)SIG_IGN)
572 signal (SIGTTOU, SIG_IGN);
573 signal (SIGTTIN, SIG_IGN);
577 #if defined (SIGWINCH)
578 old_sigwinch = (SigHandler *)signal (SIGWINCH, rl_handle_sigwinch);
584 signal (SIGINT, old_int);
585 signal (SIGALRM, old_alrm);
587 #if defined (SIGTSTP)
588 signal (SIGTSTP, old_tstp);
591 #if defined (SIGTTOU)
592 signal (SIGTTOU, old_ttou);
593 signal (SIGTTIN, old_ttin);
596 #if defined (SIGWINCH)
597 signal (SIGWINCH, old_sigwinch);
600 #endif /* HANDLE_SIGNALS */
603 /* **************************************************************** */
605 /* Character Input Buffering */
607 /* **************************************************************** */
609 #if defined (USE_XON_XOFF)
610 /* If the terminal was in xoff state when we got to it, then xon_char
611 contains the character that is supposed to start it again. */
612 static int xon_char, xoff_state;
613 #endif /* USE_XON_XOFF */
615 static int pop_index = 0, push_index = 0, ibuffer_len = 511;
616 static unsigned char ibuffer[512];
618 /* Non-null means it is a pointer to a function to run while waiting for
620 Function *rl_event_hook = (Function *)NULL;
622 #define any_typein (push_index != pop_index)
624 /* Add KEY to the buffer of characters to be read. */
631 rl_pending_input = EOF;
633 ibuffer[push_index++] = key;
634 if (push_index >= ibuffer_len)
638 /* Return the amount of space available in the
639 buffer for stuffing characters. */
643 if (pop_index > push_index)
644 return (pop_index - push_index);
646 return (ibuffer_len - (push_index - pop_index));
649 /* Get a key from the buffer of characters to be read.
650 Return the key in KEY.
651 Result is KEY if there was a key, or 0 if there wasn't. */
656 if (push_index == pop_index)
659 *key = ibuffer[pop_index++];
661 if (pop_index >= ibuffer_len)
667 /* Stuff KEY into the *front* of the input buffer.
668 Returns non-zero if successful, zero if there is
669 no space left in the buffer. */
674 if (ibuffer_space ())
678 pop_index = ibuffer_len - 1;
679 ibuffer[pop_index] = key;
685 /* If a character is available to be read, then read it
686 and stuff it into IBUFFER. Otherwise, just return. */
689 int tty = fileno (in_stream);
690 register int tem, result = -1;
694 #if defined (FIONREAD)
695 result = ioctl (tty, FIONREAD, &chars_avail);
702 flags = fcntl (tty, F_GETFL, 0);
704 fcntl (tty, F_SETFL, (flags | O_NDELAY));
705 chars_avail = read (tty, &input, 1);
707 fcntl (tty, F_SETFL, flags);
708 if (chars_avail == -1 && errno == EAGAIN)
712 /* If there's nothing available, don't waste time trying to read
714 if (chars_avail == 0)
717 tem = ibuffer_space ();
719 if (chars_avail > tem)
722 /* One cannot read all of the available input. I can only read a single
723 character at a time, or else programs which require input can be
724 thwarted. If the buffer is larger than one character, I lose.
726 if (tem < ibuffer_len)
731 while (chars_avail--)
732 rl_stuff_char (rl_getc (in_stream));
737 rl_stuff_char (input);
741 static int next_macro_key ();
742 /* Read a key, including pending input. */
748 rl_key_sequence_length++;
750 if (rl_pending_input)
752 c = rl_pending_input;
753 rl_pending_input = 0;
757 /* If input is coming from a macro, then use that. */
758 if (c = next_macro_key ())
761 /* If the user has an event function, then call it periodically. */
764 while (rl_event_hook && !rl_get_char (&c))
772 if (!rl_get_char (&c))
773 c = rl_getc (in_stream);
780 /* I'm beginning to hate the declaration rules for various compilers. */
781 static void add_macro_char (), with_macro_input ();
783 /* Do the command associated with KEY in MAP.
784 If the associated command is really a keymap, then read
785 another key, and dispatch into that map. */
786 rl_dispatch (key, map)
791 if (defining_kbd_macro)
792 add_macro_char (key);
794 if (key > 127 && key < 256)
796 if (map[ESC].type == ISKMAP)
798 map = (Keymap)map[ESC].function;
800 rl_dispatch (key, map);
807 switch (map[key].type)
811 Function *func = map[key].function;
813 if (func != (Function *)NULL)
815 /* Special case rl_do_lowercase_version (). */
816 if (func == rl_do_lowercase_version)
818 rl_dispatch (to_lower (key), map);
822 (*map[key].function)(rl_numeric_arg * rl_arg_sign, key);
824 /* If we have input pending, then the last command was a prefix
825 command. Don't change the state of rl_last_func. Otherwise,
826 remember the last command executed in this variable. */
827 if (!rl_pending_input)
828 rl_last_func = map[key].function;
839 if (map[key].function != (Function *)NULL)
843 rl_key_sequence_length++;
844 newkey = rl_read_key ();
845 rl_dispatch (newkey, (Keymap)map[key].function);
855 if (map[key].function != (Function *)NULL)
859 macro = savestring ((char *)map[key].function);
860 with_macro_input (macro);
868 /* **************************************************************** */
870 /* Hacking Keyboard Macros */
872 /* **************************************************************** */
874 /* The currently executing macro string. If this is non-zero,
875 then it is a malloc ()'ed string where input is coming from. */
876 static char *executing_macro = (char *)NULL;
878 /* The offset in the above string to the next character to be read. */
879 static int executing_macro_index = 0;
881 /* The current macro string being built. Characters get stuffed
882 in here by add_macro_char (). */
883 static char *current_macro = (char *)NULL;
885 /* The size of the buffer allocated to current_macro. */
886 static int current_macro_size = 0;
888 /* The index at which characters are being added to current_macro. */
889 static int current_macro_index = 0;
891 /* A structure used to save nested macro strings.
892 It is a linked list of string/index for each saved macro. */
894 struct saved_macro *next;
899 /* The list of saved macros. */
900 struct saved_macro *macro_list = (struct saved_macro *)NULL;
902 /* Forward declarations of static functions. Thank you C. */
903 static void push_executing_macro (), pop_executing_macro ();
905 /* This one has to be declared earlier in the file. */
906 /* static void add_macro_char (); */
908 /* Set up to read subsequent input from STRING.
909 STRING is free ()'ed when we are done with it. */
911 with_macro_input (string)
914 push_executing_macro ();
915 executing_macro = string;
916 executing_macro_index = 0;
919 /* Return the next character available from a macro, or 0 if
920 there are no macro characters. */
924 if (!executing_macro)
927 if (!executing_macro[executing_macro_index])
929 pop_executing_macro ();
930 return (next_macro_key ());
933 return (executing_macro[executing_macro_index++]);
936 /* Save the currently executing macro on a stack of saved macros. */
938 push_executing_macro ()
940 struct saved_macro *saver;
942 saver = (struct saved_macro *)xmalloc (sizeof (struct saved_macro));
943 saver->next = macro_list;
944 saver->index = executing_macro_index;
945 saver->string = executing_macro;
950 /* Discard the current macro, replacing it with the one
951 on the top of the stack of saved macros. */
953 pop_executing_macro ()
956 free (executing_macro);
958 executing_macro = (char *)NULL;
959 executing_macro_index = 0;
963 struct saved_macro *disposer = macro_list;
964 executing_macro = macro_list->string;
965 executing_macro_index = macro_list->index;
966 macro_list = macro_list->next;
971 /* Add a character to the macro being built. */
976 if (current_macro_index + 1 >= current_macro_size)
979 current_macro = (char *)xmalloc (current_macro_size = 25);
982 (char *)xrealloc (current_macro, current_macro_size += 25);
985 current_macro[current_macro_index++] = c;
986 current_macro[current_macro_index] = '\0';
989 /* Begin defining a keyboard macro.
990 Keystrokes are recorded as they are executed.
991 End the definition with rl_end_kbd_macro ().
992 If a numeric argument was explicitly typed, then append this
993 definition to the end of the existing macro, and start by
994 re-executing the existing macro. */
995 rl_start_kbd_macro (ignore1, ignore2)
996 int ignore1, ignore2;
998 if (defining_kbd_macro)
1001 if (rl_explicit_arg)
1004 with_macro_input (savestring (current_macro));
1007 current_macro_index = 0;
1009 defining_kbd_macro = 1;
1012 /* Stop defining a keyboard macro.
1013 A numeric argument says to execute the macro right now,
1014 that many times, counting the definition as the first time. */
1015 rl_end_kbd_macro (count, ignore)
1018 if (!defining_kbd_macro)
1021 current_macro_index -= (rl_key_sequence_length - 1);
1022 current_macro[current_macro_index] = '\0';
1024 defining_kbd_macro = 0;
1026 rl_call_last_kbd_macro (--count, 0);
1029 /* Execute the most recently defined keyboard macro.
1030 COUNT says how many times to execute it. */
1031 rl_call_last_kbd_macro (count, ignore)
1038 with_macro_input (savestring (current_macro));
1042 /* **************************************************************** */
1044 /* Initializations */
1046 /* **************************************************************** */
1048 /* Initliaze readline (and terminal if not already). */
1051 extern char *rl_display_prompt;
1053 /* If we have never been called before, initialize the
1054 terminal and data structures. */
1055 if (!rl_initialized)
1057 readline_initialize_everything ();
1061 /* Initalize the current line information. */
1062 rl_point = rl_end = 0;
1063 the_line = rl_line_buffer;
1066 /* We aren't done yet. We haven't even gotten started yet! */
1069 /* Tell the history routines what is going on. */
1070 start_using_history ();
1072 /* Make the display buffer match the state of the line. */
1074 extern char *rl_display_prompt;
1075 extern int forced_display;
1079 rl_display_prompt = rl_prompt ? rl_prompt : "";
1083 /* No such function typed yet. */
1084 rl_last_func = (Function *)NULL;
1086 /* Parsing of key-bindings begins in an enabled state. */
1087 parsing_conditionalized_out = 0;
1090 /* Initialize the entire state of the world. */
1091 readline_initialize_everything ()
1093 /* Find out if we are running in Emacs. */
1094 running_in_emacs = getenv ("EMACS");
1096 /* Allocate data structures. */
1097 if (!rl_line_buffer)
1099 (char *)xmalloc (rl_line_buffer_len = DEFAULT_BUFFER_SIZE);
1101 /* Initialize the terminal interface. */
1102 init_terminal_io ((char *)NULL);
1104 /* Bind tty characters to readline functions. */
1105 readline_default_bindings ();
1107 /* Initialize the function names. */
1108 rl_initialize_funmap ();
1110 /* Read in the init file. */
1111 rl_read_init_file ((char *)NULL);
1113 /* If the completion parser's default word break characters haven't
1114 been set yet, then do so now. */
1116 extern char *rl_completer_word_break_characters;
1117 extern char *rl_basic_word_break_characters;
1119 if (rl_completer_word_break_characters == (char *)NULL)
1120 rl_completer_word_break_characters = rl_basic_word_break_characters;
1124 /* If this system allows us to look at the values of the regular
1125 input editing characters, then bind them to their readline
1126 equivalents, iff the characters are not bound to keymaps. */
1127 readline_default_bindings ()
1130 #if defined (NEW_TTY_DRIVER)
1131 struct sgttyb ttybuff;
1132 int tty = fileno (rl_instream);
1134 if (ioctl (tty, TIOCGETP, &ttybuff) != -1)
1138 erase = ttybuff.sg_erase;
1139 kill = ttybuff.sg_kill;
1141 if (erase != -1 && keymap[erase].type == ISFUNC)
1142 keymap[erase].function = rl_rubout;
1144 if (kill != -1 && keymap[kill].type == ISFUNC)
1145 keymap[kill].function = rl_unix_line_discard;
1148 #if defined (TIOCGLTC)
1152 if (ioctl (tty, TIOCGLTC, <) != -1)
1156 erase = lt.t_werasc;
1157 nextc = lt.t_lnextc;
1159 if (erase != -1 && keymap[erase].type == ISFUNC)
1160 keymap[erase].function = rl_unix_word_rubout;
1162 if (nextc != -1 && keymap[nextc].type == ISFUNC)
1163 keymap[nextc].function = rl_quoted_insert;
1166 #endif /* TIOCGLTC */
1167 #else /* not NEW_TTY_DRIVER */
1169 #if defined (TERMIOS_TTY_DRIVER)
1170 struct termios ttybuff;
1172 struct termio ttybuff;
1173 #endif /* TERMIOS_TTY_DRIVER */
1174 int tty = fileno (rl_instream);
1176 #if defined (TERMIOS_TTY_DRIVER)
1177 if (tcgetattr (tty, &ttybuff) != -1)
1179 if (ioctl (tty, TCGETA, &ttybuff) != -1)
1180 #endif /* !TERMIOS_TTY_DRIVER */
1184 erase = ttybuff.c_cc[VERASE];
1185 kill = ttybuff.c_cc[VKILL];
1187 if (erase != _POSIX_VDISABLE &&
1188 keymap[(unsigned char)erase].type == ISFUNC)
1189 keymap[(unsigned char)erase].function = rl_rubout;
1191 if (kill != _POSIX_VDISABLE &&
1192 keymap[(unsigned char)kill].type == ISFUNC)
1193 keymap[(unsigned char)kill].function = rl_unix_line_discard;
1195 #if defined (VLNEXT) && defined (TERMIOS_TTY_DRIVER)
1199 nextc = ttybuff.c_cc[VLNEXT];
1201 if (nextc != _POSIX_VDISABLE &&
1202 keymap[(unsigned char)nextc].type == ISFUNC)
1203 keymap[(unsigned char)nextc].function = rl_quoted_insert;
1205 #endif /* VLNEXT && TERMIOS_TTY_DRIVER */
1207 #if defined (VWERASE)
1211 werase = ttybuff.c_cc[VWERASE];
1213 if (werase != _POSIX_VDISABLE &&
1214 keymap[(unsigned char)werase].type == ISFUNC)
1215 keymap[(unsigned char)werase].function = rl_unix_word_rubout;
1217 #endif /* VWERASE */
1219 #endif /* !NEW_TTY_DRIVER */
1223 /* **************************************************************** */
1225 /* Numeric Arguments */
1227 /* **************************************************************** */
1229 /* Handle C-u style numeric args, as well as M--, and M-digits. */
1231 /* Add the current digit to the argument in progress. */
1232 rl_digit_argument (ignore, key)
1235 rl_pending_input = key;
1239 /* What to do when you abort reading an argument. */
1240 rl_discard_argument ()
1243 rl_clear_message ();
1244 rl_init_argument ();
1247 /* Create a default argument. */
1250 rl_numeric_arg = rl_arg_sign = 1;
1251 rl_explicit_arg = 0;
1254 /* C-u, universal argument. Multiply the current argument by 4.
1255 Read a key. If the key has nothing to do with arguments, then
1256 dispatch on it. If the key is the abort character then abort. */
1257 rl_universal_argument ()
1259 rl_numeric_arg *= 4;
1268 rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg);
1269 key = c = rl_read_key ();
1271 if (keymap[c].type == ISFUNC &&
1272 keymap[c].function == rl_universal_argument)
1274 rl_numeric_arg *= 4;
1280 if (rl_explicit_arg)
1281 rl_numeric_arg = (rl_numeric_arg * 10) + (c - '0');
1283 rl_numeric_arg = (c - '0');
1284 rl_explicit_arg = 1;
1288 if (c == '-' && !rl_explicit_arg)
1295 rl_clear_message ();
1296 rl_dispatch (key, keymap);
1304 /* **************************************************************** */
1308 /* **************************************************************** */
1310 /* This is the stuff that is hard for me. I never seem to write good
1311 display routines in C. Let's see how I do this time. */
1313 /* (PWP) Well... Good for a simple line updater, but totally ignores
1314 the problems of input lines longer than the screen width.
1316 update_line and the code that calls it makes a multiple line,
1317 automatically wrapping line update. Carefull attention needs
1318 to be paid to the vertical position variables.
1320 handling of terminals with autowrap on (incl. DEC braindamage)
1321 could be improved a bit. Right now I just cheat and decrement
1322 screenwidth by one. */
1324 /* Keep two buffers; one which reflects the current contents of the
1325 screen, and the other to draw what we think the new contents should
1326 be. Then compare the buffers, and make whatever changes to the
1327 screen itself that we should. Finally, make the buffer that we
1328 just drew into be the one which reflects the current contents of the
1329 screen, and place the cursor where it belongs.
1331 Commands that want to can fix the display themselves, and then let
1332 this function know that the display has been fixed by setting the
1333 RL_DISPLAY_FIXED variable. This is good for efficiency. */
1335 /* Termcap variables: */
1336 extern char *term_up, *term_dc, *term_cr;
1337 extern int screenheight, screenwidth, terminal_can_insert;
1339 /* What YOU turn on when you have handled all redisplay yourself. */
1340 int rl_display_fixed = 0;
1342 /* The visible cursor position. If you print some text, adjust this. */
1346 /* The last left edge of text that was displayed. This is used when
1347 doing horizontal scrolling. It shifts in thirds of a screenwidth. */
1348 static int last_lmargin = 0;
1350 /* The line display buffers. One is the line currently displayed on
1351 the screen. The other is the line about to be displayed. */
1352 static char *visible_line = (char *)NULL;
1353 static char *invisible_line = (char *)NULL;
1355 /* Number of lines currently on screen minus 1. */
1358 /* A buffer for `modeline' messages. */
1361 /* Non-zero forces the redisplay even if we thought it was unnecessary. */
1362 int forced_display = 0;
1364 /* The stuff that gets printed out before the actual text of the line.
1365 This is usually pointing to rl_prompt. */
1366 char *rl_display_prompt = (char *)NULL;
1368 /* Default and initial buffer size. Can grow. */
1369 static int line_size = 1024;
1371 /* Non-zero means to always use horizontal scrolling in line display. */
1372 static int horizontal_scroll_mode = 0;
1374 /* Non-zero means to display an asterisk at the starts of history lines
1375 which have been modified. */
1376 static int mark_modified_lines = 0;
1378 /* Non-zero means to use a visible bell if one is available rather than
1379 simply ringing the terminal bell. */
1380 static int prefer_visible_bell = 0;
1382 /* I really disagree with this, but my boss (among others) insists that we
1383 support compilers that don't work. I don't think we are gaining by doing
1384 so; what is the advantage in producing better code if we can't use it? */
1385 /* The following two declarations belong inside the
1386 function block, not here. */
1387 static void move_cursor_relative ();
1388 static void output_some_chars ();
1389 static void output_character_function ();
1390 static int compare_strings ();
1392 /* Basic redisplay algorithm. */
1395 register int in, out, c, linenum;
1396 register char *line = invisible_line;
1397 char *prompt_this_line;
1399 int inv_botlin = 0; /* Number of lines in newly drawn buffer. */
1401 extern int readline_echoing_p;
1403 if (!readline_echoing_p)
1406 if (!rl_display_prompt)
1407 rl_display_prompt = "";
1409 if (!invisible_line)
1411 visible_line = (char *)xmalloc (line_size);
1412 invisible_line = (char *)xmalloc (line_size);
1413 line = invisible_line;
1414 for (in = 0; in < line_size; in++)
1416 visible_line[in] = 0;
1417 invisible_line[in] = 1;
1422 /* Draw the line into the buffer. */
1425 /* Mark the line as modified or not. We only do this for history
1428 if (mark_modified_lines && current_history () && rl_undo_list)
1434 /* If someone thought that the redisplay was handled, but the currently
1435 visible line has a different modification state than the one about
1436 to become visible, then correct the callers misconception. */
1437 if (visible_line[0] != invisible_line[0])
1438 rl_display_fixed = 0;
1440 prompt_this_line = rindex (rl_display_prompt, '\n');
1441 if (!prompt_this_line)
1442 prompt_this_line = rl_display_prompt;
1447 output_some_chars (rl_display_prompt,
1448 prompt_this_line - rl_display_prompt);
1451 strncpy (line + out, prompt_this_line, strlen (prompt_this_line));
1452 out += strlen (prompt_this_line);
1455 for (in = 0; in < rl_end; in++)
1457 c = (unsigned char)the_line[in];
1459 if (out + 1 >= line_size)
1462 visible_line = (char *)xrealloc (visible_line, line_size);
1463 invisible_line = (char *)xrealloc (invisible_line, line_size);
1464 line = invisible_line;
1474 line[out++] = c - 128;
1476 #define DISPLAY_TABS
1477 #if defined (DISPLAY_TABS)
1480 register int newout = (out | (int)7) + 1;
1481 while (out < newout)
1489 line[out++] = c + 64;
1504 /* PWP: now is when things get a bit hairy. The visible and invisible
1505 line buffers are really multiple lines, which would wrap every
1506 (screenwidth - 1) characters. Go through each in turn, finding
1507 the changed region and updating it. The line order is top to bottom. */
1509 /* If we can move the cursor up and down, then use multiple lines,
1510 otherwise, let long lines display in a single terminal line, and
1511 horizontally scroll it. */
1513 if (!horizontal_scroll_mode && term_up && *term_up)
1515 int total_screen_chars = (screenwidth * screenheight);
1517 if (!rl_display_fixed || forced_display)
1521 /* If we have more than a screenful of material to display, then
1522 only display a screenful. We should display the last screen,
1523 not the first. I'll fix this in a minute. */
1524 if (out >= total_screen_chars)
1525 out = total_screen_chars - 1;
1527 /* Number of screen lines to display. */
1528 inv_botlin = out / screenwidth;
1530 /* For each line in the buffer, do the updating display. */
1531 for (linenum = 0; linenum <= inv_botlin; linenum++)
1532 update_line (linenum > vis_botlin ? ""
1533 : &visible_line[linenum * screenwidth],
1534 &invisible_line[linenum * screenwidth],
1537 /* We may have deleted some lines. If so, clear the left over
1538 blank ones at the bottom out. */
1539 if (vis_botlin > inv_botlin)
1542 for (; linenum <= vis_botlin; linenum++)
1544 tt = &visible_line[linenum * screenwidth];
1545 move_vert (linenum);
1546 move_cursor_relative (0, tt);
1547 clear_to_eol ((linenum == vis_botlin)?
1548 strlen (tt) : screenwidth);
1551 vis_botlin = inv_botlin;
1553 /* Move the cursor where it should be. */
1554 move_vert (c_pos / screenwidth);
1555 move_cursor_relative (c_pos % screenwidth,
1556 &invisible_line[(c_pos / screenwidth) * screenwidth]);
1559 else /* Do horizontal scrolling. */
1563 /* Always at top line. */
1566 /* If the display position of the cursor would be off the edge
1567 of the screen, start the display of this line at an offset that
1568 leaves the cursor on the screen. */
1569 if (c_pos - last_lmargin > screenwidth - 2)
1570 lmargin = (c_pos / (screenwidth / 3) - 2) * (screenwidth / 3);
1571 else if (c_pos - last_lmargin < 1)
1572 lmargin = ((c_pos - 1) / (screenwidth / 3)) * (screenwidth / 3);
1574 lmargin = last_lmargin;
1576 /* If the first character on the screen isn't the first character
1577 in the display line, indicate this with a special character. */
1579 line[lmargin] = '<';
1581 if (lmargin + screenwidth < out)
1582 line[lmargin + screenwidth - 1] = '>';
1584 if (!rl_display_fixed || forced_display || lmargin != last_lmargin)
1587 update_line (&visible_line[last_lmargin],
1588 &invisible_line[lmargin], 0);
1590 move_cursor_relative (c_pos - lmargin, &invisible_line[lmargin]);
1591 last_lmargin = lmargin;
1594 fflush (out_stream);
1596 /* Swap visible and non-visible lines. */
1598 char *temp = visible_line;
1599 visible_line = invisible_line;
1600 invisible_line = temp;
1601 rl_display_fixed = 0;
1605 /* PWP: update_line() is based on finding the middle difference of each
1606 line on the screen; vis:
1608 /old first difference
1609 /beginning of line | /old last same /old EOL
1611 old: eddie> Oh, my little gruntle-buggy is to me, as lurgid as
1612 new: eddie> Oh, my little buggy says to me, as lurgid as
1614 \beginning of line | \new last same \new end of line
1615 \new first difference
1617 All are character pointers for the sake of speed. Special cases for
1618 no differences, as well as for end of line additions must be handeled.
1620 Could be made even smarter, but this works well enough */
1622 update_line (old, new, current_line)
1623 register char *old, *new;
1626 register char *ofd, *ols, *oe, *nfd, *nls, *ne;
1627 int lendiff, wsatend;
1629 /* Find first difference. */
1630 for (ofd = old, nfd = new;
1631 (ofd - old < screenwidth) && *ofd && (*ofd == *nfd);
1635 /* Move to the end of the screen line. */
1636 for (oe = ofd; ((oe - old) < screenwidth) && *oe; oe++);
1637 for (ne = nfd; ((ne - new) < screenwidth) && *ne; ne++);
1639 /* If no difference, continue to next line. */
1640 if (ofd == oe && nfd == ne)
1643 wsatend = 1; /* flag for trailing whitespace */
1644 ols = oe - 1; /* find last same */
1646 while ((*ols == *nls) && (ols > ofd) && (nls > nfd))
1659 else if (*ols != *nls)
1661 if (*ols) /* don't step past the NUL */
1667 move_vert (current_line);
1668 move_cursor_relative (ofd - old, old);
1670 /* if (len (new) > len (old)) */
1671 lendiff = (nls - nfd) - (ols - ofd);
1673 /* Insert (diff(len(old),len(new)) ch */
1676 if (terminal_can_insert)
1678 extern char *term_IC;
1680 /* Sometimes it is cheaper to print the characters rather than
1681 use the terminal's capabilities. */
1682 if ((2 * (ne - nfd)) < lendiff && !term_IC)
1684 output_some_chars (nfd, (ne - nfd));
1685 last_c_pos += (ne - nfd);
1691 insert_some_chars (nfd, lendiff);
1692 last_c_pos += lendiff;
1696 /* At the end of a line the characters do not have to
1697 be "inserted". They can just be placed on the screen. */
1698 output_some_chars (nfd, lendiff);
1699 last_c_pos += lendiff;
1701 /* Copy (new) chars to screen from first diff to last match. */
1702 if (((nls - nfd) - lendiff) > 0)
1704 output_some_chars (&nfd[lendiff], ((nls - nfd) - lendiff));
1705 last_c_pos += ((nls - nfd) - lendiff);
1710 { /* cannot insert chars, write to EOL */
1711 output_some_chars (nfd, (ne - nfd));
1712 last_c_pos += (ne - nfd);
1715 else /* Delete characters from line. */
1717 /* If possible and inexpensive to use terminal deletion, then do so. */
1718 if (term_dc && (2 * (ne - nfd)) >= (-lendiff))
1721 delete_chars (-lendiff); /* delete (diff) characters */
1723 /* Copy (new) chars to screen from first diff to last match */
1724 if ((nls - nfd) > 0)
1726 output_some_chars (nfd, (nls - nfd));
1727 last_c_pos += (nls - nfd);
1730 /* Otherwise, print over the existing material. */
1733 output_some_chars (nfd, (ne - nfd));
1734 last_c_pos += (ne - nfd);
1735 clear_to_eol ((oe - old) - (ne - new));
1740 /* (PWP) tell the update routines that we have moved onto a
1741 new (empty) line. */
1745 visible_line[0] = '\0';
1747 last_c_pos = last_v_pos = 0;
1748 vis_botlin = last_lmargin = 0;
1751 /* Actually update the display, period. */
1752 rl_forced_update_display ()
1756 register char *temp = visible_line;
1758 while (*temp) *temp++ = '\0';
1765 /* Move the cursor from last_c_pos to NEW, which are buffer indices.
1766 DATA is the contents of the screen line of interest; i.e., where
1767 the movement is being done. */
1769 move_cursor_relative (new, data)
1775 /* It may be faster to output a CR, and then move forwards instead
1776 of moving backwards. */
1777 if (new + 1 < last_c_pos - new)
1779 tputs (term_cr, 1, output_character_function);
1783 if (last_c_pos == new) return;
1785 if (last_c_pos < new)
1787 /* Move the cursor forward. We do it by printing the command
1788 to move the cursor forward if there is one, else print that
1789 portion of the output buffer again. Which is cheaper? */
1791 /* The above comment is left here for posterity. It is faster
1792 to print one character (non-control) than to print a control
1793 sequence telling the terminal to move forward one character.
1794 That kind of control is for people who don't know what the
1795 data is underneath the cursor. */
1796 #if defined (HACK_TERMCAP_MOTION)
1797 extern char *term_forward_char;
1799 if (term_forward_char)
1800 for (i = last_c_pos; i < new; i++)
1801 tputs (term_forward_char, 1, output_character_function);
1803 for (i = last_c_pos; i < new; i++)
1804 putc (data[i], out_stream);
1806 for (i = last_c_pos; i < new; i++)
1807 putc (data[i], out_stream);
1808 #endif /* HACK_TERMCAP_MOTION */
1811 backspace (last_c_pos - new);
1815 /* PWP: move the cursor up or down. */
1819 void output_character_function ();
1820 register int delta, i;
1822 if (last_v_pos == to) return;
1824 if (to > screenheight)
1827 if ((delta = to - last_v_pos) > 0)
1829 for (i = 0; i < delta; i++)
1830 putc ('\n', out_stream);
1831 tputs (term_cr, 1, output_character_function);
1836 if (term_up && *term_up)
1837 for (i = 0; i < -delta; i++)
1838 tputs (term_up, 1, output_character_function);
1840 last_v_pos = to; /* now to is here */
1843 /* Physically print C on out_stream. This is for functions which know
1844 how to optimize the display. */
1850 fprintf (out_stream, "M-");
1854 #if defined (DISPLAY_TABS)
1855 if (c < 32 && c != '\t')
1864 putc (c, out_stream);
1865 fflush (out_stream);
1868 #if defined (DISPLAY_TABS)
1870 rl_character_len (c, pos)
1871 register int c, pos;
1873 if (c < ' ' || c > 126)
1876 return (((pos | (int)7) + 1) - pos);
1885 rl_character_len (c)
1888 if (c < ' ' || c > 126)
1893 #endif /* DISPLAY_TAB */
1895 /* How to print things in the "echo-area". The prompt is treated as a
1897 rl_message (string, arg1, arg2)
1900 sprintf (msg_buf, string, arg1, arg2);
1901 rl_display_prompt = msg_buf;
1905 /* How to clear things from the "echo-area". */
1908 rl_display_prompt = rl_prompt;
1912 /* **************************************************************** */
1914 /* Terminal and Termcap */
1916 /* **************************************************************** */
1918 static char *term_buffer = (char *)NULL;
1919 static char *term_string_buffer = (char *)NULL;
1921 /* Non-zero means this terminal can't really do anything. */
1927 /* Some strings to control terminal actions. These are output by tputs (). */
1928 char *term_goto, *term_clreol, *term_cr, *term_clrpag, *term_backspace;
1930 int screenwidth, screenheight;
1932 /* Non-zero if we determine that the terminal can do character insertion. */
1933 int terminal_can_insert = 0;
1935 /* How to insert characters. */
1936 char *term_im, *term_ei, *term_ic, *term_ip, *term_IC;
1938 /* How to delete characters. */
1939 char *term_dc, *term_DC;
1941 #if defined (HACK_TERMCAP_MOTION)
1942 char *term_forward_char;
1943 #endif /* HACK_TERMCAP_MOTION */
1945 /* How to go up a line. */
1948 /* A visible bell, if the terminal can be made to flash the screen. */
1951 /* Re-initialize the terminal considering that the TERM/TERMCAP variable
1953 rl_reset_terminal (terminal_name)
1954 char *terminal_name;
1956 init_terminal_io (terminal_name);
1959 init_terminal_io (terminal_name)
1960 char *terminal_name;
1962 extern char *tgetstr ();
1963 char *term, *buffer;
1964 #if defined (TIOCGWINSZ)
1965 struct winsize window_size;
1969 term = terminal_name ? terminal_name : getenv ("TERM");
1971 if (!term_string_buffer)
1972 term_string_buffer = (char *)xmalloc (2048);
1975 term_buffer = (char *)xmalloc (2048);
1977 buffer = term_string_buffer;
1979 term_clrpag = term_cr = term_clreol = (char *)NULL;
1984 if (tgetent (term_buffer, term) < 0)
1990 term_im = term_ei = term_ic = term_IC = (char *)NULL;
1991 term_up = term_dc = term_DC = visible_bell = (char *)NULL;
1992 #if defined (HACK_TERMCAP_MOTION)
1993 term_forward_char = (char *)NULL;
1995 terminal_can_insert = 0;
1999 BC = tgetstr ("pc", &buffer);
2000 PC = buffer ? *buffer : 0;
2002 term_backspace = tgetstr ("le", &buffer);
2004 term_cr = tgetstr ("cr", &buffer);
2005 term_clreol = tgetstr ("ce", &buffer);
2006 term_clrpag = tgetstr ("cl", &buffer);
2011 #if defined (HACK_TERMCAP_MOTION)
2012 term_forward_char = tgetstr ("nd", &buffer);
2013 #endif /* HACK_TERMCAP_MOTION */
2016 tty = fileno (rl_instream);
2020 screenwidth = screenheight = 0;
2021 #if defined (TIOCGWINSZ)
2022 if (ioctl (tty, TIOCGWINSZ, &window_size) == 0)
2024 screenwidth = (int) window_size.ws_col;
2025 screenheight = (int) window_size.ws_row;
2029 if (screenwidth <= 0 || screenheight <= 0)
2031 screenwidth = tgetnum ("co");
2032 screenheight = tgetnum ("li");
2037 if (screenwidth <= 0)
2040 if (screenheight <= 0)
2043 term_im = tgetstr ("im", &buffer);
2044 term_ei = tgetstr ("ei", &buffer);
2045 term_IC = tgetstr ("IC", &buffer);
2046 term_ic = tgetstr ("ic", &buffer);
2048 /* "An application program can assume that the terminal can do
2049 character insertion if *any one of* the capabilities `IC',
2050 `im', `ic' or `ip' is provided." But we can't do anything if
2051 only `ip' is provided, so... */
2052 terminal_can_insert = (term_IC || term_im || term_ic);
2054 term_up = tgetstr ("up", &buffer);
2055 term_dc = tgetstr ("dc", &buffer);
2056 term_DC = tgetstr ("DC", &buffer);
2058 visible_bell = tgetstr ("vb", &buffer);
2061 /* A function for the use of tputs () */
2063 output_character_function (c)
2066 putc (c, out_stream);
2069 /* Write COUNT characters from STRING to the output stream. */
2071 output_some_chars (string, count)
2075 fwrite (string, 1, count, out_stream);
2078 /* Delete COUNT characters from the display line. */
2080 delete_chars (count)
2083 if (count > screenwidth)
2086 if (term_DC && *term_DC)
2088 char *tgoto (), *buffer;
2089 buffer = tgoto (term_DC, 0, count);
2090 tputs (buffer, 1, output_character_function);
2094 if (term_dc && *term_dc)
2096 tputs (term_dc, 1, output_character_function);
2100 /* Insert COUNT characters from STRING to the output stream. */
2102 insert_some_chars (string, count)
2106 /* If IC is defined, then we do not have to "enter" insert mode. */
2109 char *tgoto (), *buffer;
2110 buffer = tgoto (term_IC, 0, count);
2111 tputs (buffer, 1, output_character_function);
2112 output_some_chars (string, count);
2118 /* If we have to turn on insert-mode, then do so. */
2119 if (term_im && *term_im)
2120 tputs (term_im, 1, output_character_function);
2122 /* If there is a special command for inserting characters, then
2123 use that first to open up the space. */
2124 if (term_ic && *term_ic)
2126 for (i = count; i--; )
2127 tputs (term_ic, 1, output_character_function);
2130 /* Print the text. */
2131 output_some_chars (string, count);
2133 /* If there is a string to turn off insert mode, we had best use
2135 if (term_ei && *term_ei)
2136 tputs (term_ei, 1, output_character_function);
2140 /* Move the cursor back. */
2147 for (i = 0; i < count; i++)
2148 tputs (term_backspace, 1, output_character_function);
2150 for (i = 0; i < count; i++)
2151 putc ('\b', out_stream);
2154 /* Move to the start of the next line. */
2157 #if defined (NEW_TTY_DRIVER)
2158 tputs (term_cr, 1, output_character_function);
2159 #endif /* NEW_TTY_DRIVER */
2160 putc ('\n', out_stream);
2163 /* Clear to the end of the line. COUNT is the minimum
2164 number of character spaces to clear, */
2165 clear_to_eol (count)
2170 tputs (term_clreol, 1, output_character_function);
2176 /* Do one more character space. */
2179 for (i = 0; i < count; i++)
2180 putc (' ', out_stream);
2187 /* **************************************************************** */
2189 /* Saving and Restoring the TTY */
2191 /* **************************************************************** */
2193 /* Non-zero means that the terminal is in a prepped state. */
2194 static int terminal_prepped = 0;
2196 #if defined (NEW_TTY_DRIVER)
2198 /* Standard flags, including ECHO. */
2199 static int original_tty_flags = 0;
2201 /* Local mode flags, like LPASS8. */
2202 static int local_mode_flags = 0;
2204 /* Terminal characters. This has C-s and C-q in it. */
2205 static struct tchars original_tchars;
2207 /* Local special characters. This has the interrupt characters in it. */
2208 #if defined (TIOCGLTC)
2209 static struct ltchars original_ltchars;
2212 /* We use this to get and set the tty_flags. */
2213 static struct sgttyb the_ttybuff;
2215 /* Put the terminal in CBREAK mode so that we can detect key presses. */
2219 int tty = fileno (rl_instream);
2220 #if defined (HAVE_BSD_SIGNALS)
2222 #endif /* HAVE_BSD_SIGNALS */
2224 if (terminal_prepped)
2227 oldmask = sigblock (sigmask (SIGINT));
2229 /* We always get the latest tty values. Maybe stty changed them. */
2230 ioctl (tty, TIOCGETP, &the_ttybuff);
2231 original_tty_flags = the_ttybuff.sg_flags;
2233 readline_echoing_p = (original_tty_flags & ECHO);
2235 #if defined (TIOCLGET)
2236 ioctl (tty, TIOCLGET, &local_mode_flags);
2240 # define ANYP (EVENP | ODDP)
2243 /* If this terminal doesn't care how the 8th bit is used,
2244 then we can use it for the meta-key. We check by seeing
2245 if BOTH odd and even parity are allowed. */
2246 if (the_ttybuff.sg_flags & ANYP)
2249 the_ttybuff.sg_flags |= PASS8;
2252 /* Hack on local mode flags if we can. */
2253 #if defined (TIOCLGET) && defined (LPASS8)
2256 flags = local_mode_flags | LPASS8;
2257 ioctl (tty, TIOCLSET, &flags);
2259 #endif /* TIOCLGET && LPASS8 */
2262 #if defined (TIOCGETC)
2266 ioctl (tty, TIOCGETC, &original_tchars);
2267 temp = original_tchars;
2269 #if defined (USE_XON_XOFF)
2270 /* Get rid of C-s and C-q.
2271 We remember the value of startc (C-q) so that if the terminal is in
2272 xoff state, the user can xon it by pressing that character. */
2273 xon_char = temp.t_startc;
2277 /* If there is an XON character, bind it to restart the output. */
2279 rl_bind_key (xon_char, rl_restart_output);
2280 #endif /* USE_XON_XOFF */
2282 /* If there is an EOF char, bind eof_char to it. */
2283 if (temp.t_eofc != -1)
2284 eof_char = temp.t_eofc;
2286 #if defined (NO_KILL_INTR)
2287 /* Get rid of C-\ and C-c. */
2288 temp.t_intrc = temp.t_quitc = -1;
2289 #endif /* NO_KILL_INTR */
2291 ioctl (tty, TIOCSETC, &temp);
2293 #endif /* TIOCGETC */
2295 #if defined (TIOCGLTC)
2297 struct ltchars temp;
2299 ioctl (tty, TIOCGLTC, &original_ltchars);
2300 temp = original_ltchars;
2302 /* Make the interrupt keys go away. Just enough to make people
2304 temp.t_dsuspc = -1; /* C-y */
2305 temp.t_lnextc = -1; /* C-v */
2307 ioctl (tty, TIOCSLTC, &temp);
2309 #endif /* TIOCGLTC */
2311 the_ttybuff.sg_flags &= ~(ECHO | CRMOD);
2312 the_ttybuff.sg_flags |= CBREAK;
2313 ioctl (tty, TIOCSETN, &the_ttybuff);
2315 terminal_prepped = 1;
2317 #if defined (HAVE_BSD_SIGNALS)
2318 sigsetmask (oldmask);
2322 /* Restore the terminal to its original state. */
2324 rl_deprep_terminal ()
2326 int tty = fileno (rl_instream);
2327 #if defined (HAVE_BSD_SIGNALS)
2331 if (!terminal_prepped)
2334 oldmask = sigblock (sigmask (SIGINT));
2336 the_ttybuff.sg_flags = original_tty_flags;
2337 ioctl (tty, TIOCSETN, &the_ttybuff);
2338 readline_echoing_p = 1;
2340 #if defined (TIOCLGET)
2341 ioctl (tty, TIOCLSET, &local_mode_flags);
2344 #if defined (TIOCSLTC)
2345 ioctl (tty, TIOCSLTC, &original_ltchars);
2348 #if defined (TIOCSETC)
2349 ioctl (tty, TIOCSETC, &original_tchars);
2351 terminal_prepped = 0;
2353 #if defined (HAVE_BSD_SIGNALS)
2354 sigsetmask (oldmask);
2358 #else /* !defined (NEW_TTY_DRIVER) */
2364 #if !defined (VTIME)
2368 #if defined (TERMIOS_TTY_DRIVER)
2369 static struct termios otio;
2371 static struct termio otio;
2372 #endif /* !TERMIOS_TTY_DRIVER */
2377 int tty = fileno (rl_instream);
2378 #if defined (TERMIOS_TTY_DRIVER)
2382 #endif /* !TERMIOS_TTY_DRIVER */
2384 #if defined (HAVE_POSIX_SIGNALS)
2387 # if defined (HAVE_BSD_SIGNALS)
2389 # endif /* HAVE_BSD_SIGNALS */
2390 #endif /* !HAVE_POSIX_SIGNALS */
2392 if (terminal_prepped)
2395 /* Try to keep this function from being INTerrupted. We can do it
2396 on POSIX and systems with BSD-like signal handling. */
2397 #if defined (HAVE_POSIX_SIGNALS)
2399 sigaddset (&set, SIGINT);
2400 sigprocmask (SIG_BLOCK, &set, &oset);
2401 #else /* !HAVE_POSIX_SIGNALS */
2402 # if defined (HAVE_BSD_SIGNALS)
2403 oldmask = sigblock (sigmask (SIGINT));
2404 # endif /* HAVE_BSD_SIGNALS */
2405 #endif /* !HAVE_POSIX_SIGNALS */
2407 #if defined (TERMIOS_TTY_DRIVER)
2408 tcgetattr (tty, &tio);
2410 ioctl (tty, TCGETA, &tio);
2411 #endif /* !TERMIOS_TTY_DRIVER */
2415 readline_echoing_p = (tio.c_lflag & ECHO);
2417 tio.c_lflag &= ~(ICANON|ECHO);
2419 if (otio.c_cc[VEOF] != _POSIX_VDISABLE)
2420 eof_char = otio.c_cc[VEOF];
2422 #if defined (USE_XON_XOFF)
2424 tio.c_iflag &= ~(IXON|IXOFF|IXANY);
2426 /* `strict' Posix systems do not define IXANY. */
2427 tio.c_iflag &= ~(IXON|IXOFF);
2429 #endif /* USE_XON_XOFF */
2431 /* Only turn this off if we are using all 8 bits. */
2433 tio.c_iflag &= ~(ISTRIP | INPCK);
2435 /* Make sure we differentiate between CR and NL on input. */
2436 tio.c_iflag &= ~(ICRNL | INLCR);
2438 #if !defined (HANDLE_SIGNALS)
2439 tio.c_lflag &= ~ISIG;
2441 tio.c_lflag |= ISIG;
2445 tio.c_cc[VTIME] = 0;
2447 /* Turn off characters that we need on Posix systems with job control,
2448 just to be sure. This includes ^Y and ^V. This should not really
2450 #if defined (TERMIOS_TTY_DRIVER) && defined (_POSIX_JOB_CONTROL)
2452 #if defined (VLNEXT)
2453 tio.c_cc[VLNEXT] = _POSIX_VDISABLE;
2456 #if defined (VDSUSP)
2457 tio.c_cc[VDSUSP] = _POSIX_VDISABLE;
2460 #endif /* POSIX && JOB_CONTROL */
2462 #if defined (TERMIOS_TTY_DRIVER)
2463 tcsetattr (tty, TCSADRAIN, &tio);
2464 tcflow (tty, TCOON); /* Simulate a ^Q. */
2466 ioctl (tty, TCSETAW, &tio);
2467 ioctl (tty, TCXONC, 1); /* Simulate a ^Q. */
2468 #endif /* !TERMIOS_TTY_DRIVER */
2470 terminal_prepped = 1;
2472 #if defined (HAVE_POSIX_SIGNALS)
2473 sigprocmask (SIG_SETMASK, &oset, (sigset_t *)NULL);
2475 # if defined (HAVE_BSD_SIGNALS)
2476 sigsetmask (oldmask);
2477 # endif /* HAVE_BSD_SIGNALS */
2478 #endif /* !HAVE_POSIX_SIGNALS */
2482 rl_deprep_terminal ()
2484 int tty = fileno (rl_instream);
2486 /* Try to keep this function from being INTerrupted. We can do it
2487 on POSIX and systems with BSD-like signal handling. */
2488 #if defined (HAVE_POSIX_SIGNALS)
2490 #else /* !HAVE_POSIX_SIGNALS */
2491 # if defined (HAVE_BSD_SIGNALS)
2493 # endif /* HAVE_BSD_SIGNALS */
2494 #endif /* !HAVE_POSIX_SIGNALS */
2496 if (!terminal_prepped)
2499 #if defined (HAVE_POSIX_SIGNALS)
2501 sigaddset (&set, SIGINT);
2502 sigprocmask (SIG_BLOCK, &set, &oset);
2503 #else /* !HAVE_POSIX_SIGNALS */
2504 # if defined (HAVE_BSD_SIGNALS)
2505 oldmask = sigblock (sigmask (SIGINT));
2506 # endif /* HAVE_BSD_SIGNALS */
2507 #endif /* !HAVE_POSIX_SIGNALS */
2509 #if defined (TERMIOS_TTY_DRIVER)
2510 tcsetattr (tty, TCSADRAIN, &otio);
2511 tcflow (tty, TCOON); /* Simulate a ^Q. */
2512 #else /* TERMIOS_TTY_DRIVER */
2513 ioctl (tty, TCSETAW, &otio);
2514 ioctl (tty, TCXONC, 1); /* Simulate a ^Q. */
2515 #endif /* !TERMIOS_TTY_DRIVER */
2517 terminal_prepped = 0;
2519 #if defined (HAVE_POSIX_SIGNALS)
2520 sigprocmask (SIG_SETMASK, &oset, (sigset_t *)NULL);
2521 #else /* !HAVE_POSIX_SIGNALS */
2522 # if defined (HAVE_BSD_SIGNALS)
2523 sigsetmask (oldmask);
2524 # endif /* HAVE_BSD_SIGNALS */
2525 #endif /* !HAVE_POSIX_SIGNALS */
2527 #endif /* NEW_TTY_DRIVER */
2530 /* **************************************************************** */
2532 /* Utility Functions */
2534 /* **************************************************************** */
2536 /* Return 0 if C is not a member of the class of characters that belong
2537 in words, or 1 if it is. */
2539 int allow_pathname_alphabetic_chars = 0;
2540 char *pathname_alphabetic_chars = "/-_=~.#$";
2546 if (pure_alphabetic (c) || (numeric (c)))
2549 if (allow_pathname_alphabetic_chars)
2550 return ((int)rindex (pathname_alphabetic_chars, c));
2555 /* Return non-zero if C is a numeric character. */
2560 return (c >= '0' && c <= '9');
2563 /* Ring the terminal bell. */
2567 if (readline_echoing_p)
2569 if (prefer_visible_bell && visible_bell)
2570 tputs (visible_bell, 1, output_character_function);
2573 fprintf (stderr, "\007");
2580 /* How to abort things. */
2584 rl_clear_message ();
2585 rl_init_argument ();
2586 rl_pending_input = 0;
2588 defining_kbd_macro = 0;
2589 while (executing_macro)
2590 pop_executing_macro ();
2592 rl_last_func = (Function *)NULL;
2593 longjmp (readline_top_level, 1);
2596 /* Return a copy of the string between FROM and TO.
2597 FROM is inclusive, TO is not. */
2598 #if defined (sun) /* Yes, that's right, some crufty function in sunview is
2599 called rl_copy (). */
2606 register int length;
2609 /* Fix it if the caller is confused. */
2618 copy = (char *)xmalloc (1 + length);
2619 strncpy (copy, the_line + from, length);
2620 copy[length] = '\0';
2624 /* Increase the size of RL_LINE_BUFFER until it has enough space to hold
2627 rl_extend_line_buffer (len)
2630 while (len >= rl_line_buffer_len)
2633 (rl_line_buffer, rl_line_buffer_len += DEFAULT_BUFFER_SIZE);
2635 the_line = rl_line_buffer;
2639 /* **************************************************************** */
2641 /* Insert and Delete */
2643 /* **************************************************************** */
2645 /* Insert a string of text into the line at point. This is the only
2646 way that you should do insertion. rl_insert () calls this
2648 rl_insert_text (string)
2651 extern int doing_an_undo;
2652 register int i, l = strlen (string);
2654 if (rl_end + l >= rl_line_buffer_len)
2655 rl_extend_line_buffer (rl_end + l);
2657 for (i = rl_end; i >= rl_point; i--)
2658 the_line[i + l] = the_line[i];
2659 strncpy (the_line + rl_point, string, l);
2661 /* Remember how to undo this if we aren't undoing something. */
2664 /* If possible and desirable, concatenate the undos. */
2665 if ((strlen (string) == 1) &&
2667 (rl_undo_list->what == UNDO_INSERT) &&
2668 (rl_undo_list->end == rl_point) &&
2669 (rl_undo_list->end - rl_undo_list->start < 20))
2670 rl_undo_list->end++;
2672 rl_add_undo (UNDO_INSERT, rl_point, rl_point + l, (char *)NULL);
2676 the_line[rl_end] = '\0';
2679 /* Delete the string between FROM and TO. FROM is
2680 inclusive, TO is not. */
2681 rl_delete_text (from, to)
2684 extern int doing_an_undo;
2685 register char *text;
2687 /* Fix it if the caller is confused. */
2694 text = rl_copy (from, to);
2695 strncpy (the_line + from, the_line + to, rl_end - to);
2697 /* Remember how to undo this delete. */
2699 rl_add_undo (UNDO_DELETE, from, to, text);
2703 rl_end -= (to - from);
2704 the_line[rl_end] = '\0';
2708 /* **************************************************************** */
2710 /* Readline character functions */
2712 /* **************************************************************** */
2714 /* This is not a gap editor, just a stupid line input routine. No hair
2715 is involved in writing any of the functions, and none should be. */
2719 rl_end is the place in the string that we would place '\0';
2720 i.e., it is always safe to place '\0' there.
2722 rl_point is the place in the string where the cursor is. Sometimes
2723 this is the same as rl_end.
2725 Any command that is called interactively receives two arguments.
2726 The first is a count: the numeric arg pased to this command.
2727 The second is the key which invoked this command.
2731 /* **************************************************************** */
2733 /* Movement Commands */
2735 /* **************************************************************** */
2737 /* Note that if you `optimize' the display for these functions, you cannot
2738 use said functions in other functions which do not do optimizing display.
2739 I.e., you will have to update the data base for rl_redisplay, and you
2740 might as well let rl_redisplay do that job. */
2742 /* Move forward COUNT characters. */
2747 rl_backward (-count);
2751 #if defined (VI_MODE)
2752 if (rl_point == (rl_end - (rl_editing_mode == vi_mode)))
2754 if (rl_point == rl_end)
2755 #endif /* VI_MODE */
2766 /* Move backward COUNT characters. */
2771 rl_forward (-count);
2786 /* Move to the beginning of the line. */
2792 /* Move to the end of the line. */
2798 /* Move forward a word. We do what Emacs does. */
2799 rl_forward_word (count)
2806 rl_backward_word (-count);
2812 if (rl_point == rl_end)
2815 /* If we are not in a word, move forward until we are in one.
2816 Then, move forward until we hit a non-alphabetic character. */
2817 c = the_line[rl_point];
2818 if (!alphabetic (c))
2820 while (++rl_point < rl_end)
2822 c = the_line[rl_point];
2823 if (alphabetic (c)) break;
2826 if (rl_point == rl_end) return;
2827 while (++rl_point < rl_end)
2829 c = the_line[rl_point];
2830 if (!alphabetic (c)) break;
2836 /* Move backward a word. We do what Emacs does. */
2837 rl_backward_word (count)
2844 rl_forward_word (-count);
2853 /* Like rl_forward_word (), except that we look at the characters
2854 just before point. */
2856 c = the_line[rl_point - 1];
2857 if (!alphabetic (c))
2861 c = the_line[rl_point - 1];
2862 if (alphabetic (c)) break;
2868 c = the_line[rl_point - 1];
2869 if (!alphabetic (c))
2877 /* Clear the current line. Numeric argument to C-l does this. */
2880 int curr_line = last_c_pos / screenwidth;
2881 extern char *term_clreol;
2883 move_vert(curr_line);
2884 move_cursor_relative (0, the_line); /* XXX is this right */
2887 tputs (term_clreol, 1, output_character_function);
2889 rl_forced_update_display ();
2890 rl_display_fixed = 1;
2893 /* C-l typed to a line without quoting clears the screen, and then reprints
2894 the prompt and the current input line. Given a numeric arg, redraw only
2895 the current line. */
2898 extern char *term_clrpag;
2900 if (rl_explicit_arg)
2907 tputs (term_clrpag, 1, output_character_function);
2911 rl_forced_update_display ();
2912 rl_display_fixed = 1;
2915 rl_arrow_keys (count, c)
2920 ch = rl_read_key ();
2922 switch (to_upper (ch))
2925 rl_get_previous_history (count);
2929 rl_get_next_history (count);
2937 rl_backward (count);
2946 /* **************************************************************** */
2950 /* **************************************************************** */
2952 /* Insert the character C at the current location, moving point forward. */
2953 rl_insert (count, c)
2962 /* If we can optimize, then do it. But don't let people crash
2963 readline because of extra large arguments. */
2964 if (count > 1 && count < 1024)
2966 string = (char *)alloca (1 + count);
2968 for (i = 0; i < count; i++)
2972 rl_insert_text (string);
2980 string = (char *)alloca (1024 + 1);
2982 for (i = 0; i < 1024; i++)
2987 decreaser = (count > 1024 ? 1024 : count);
2988 string[decreaser] = '\0';
2989 rl_insert_text (string);
2995 /* We are inserting a single character.
2996 If there is pending input, then make a string of all of the
2997 pending characters that are bound to rl_insert, and insert
3004 string = (char *)alloca (ibuffer_len + 1);
3007 while ((t = rl_get_char (&key)) &&
3008 (keymap[key].type == ISFUNC &&
3009 keymap[key].function == rl_insert))
3013 rl_unget_char (key);
3016 rl_insert_text (string);
3021 /* Inserting a single character. */
3022 string = (char *)alloca (2);
3026 rl_insert_text (string);
3030 /* Insert the next typed character verbatim. */
3031 rl_quoted_insert (count)
3034 int c = rl_read_key ();
3035 rl_insert (count, c);
3038 /* Insert a tab character. */
3039 rl_tab_insert (count)
3042 rl_insert (count, '\t');
3045 /* What to do when a NEWLINE is pressed. We accept the whole line.
3046 KEY is the key that invoked this command. I guess it could have
3047 meaning in the future. */
3048 rl_newline (count, key)
3054 #if defined (VI_MODE)
3056 extern int vi_doing_insert;
3057 if (vi_doing_insert)
3059 rl_end_undo_group ();
3060 vi_doing_insert = 0;
3063 #endif /* VI_MODE */
3065 if (readline_echoing_p)
3067 move_vert (vis_botlin);
3070 fflush (out_stream);
3075 rl_clean_up_for_exit ()
3077 if (readline_echoing_p)
3079 move_vert (vis_botlin);
3081 fflush (out_stream);
3082 rl_restart_output ();
3086 /* What to do for some uppercase characters, like meta characters,
3087 and some characters appearing in emacs_ctlx_keymap. This function
3088 is just a stub, you bind keys to it and the code in rl_dispatch ()
3089 is special cased. */
3090 rl_do_lowercase_version (ignore1, ignore2)
3091 int ignore1, ignore2;
3095 /* Rubout the character behind point. */
3113 int orig_point = rl_point;
3114 rl_backward (count);
3115 rl_kill_text (orig_point, rl_point);
3119 int c = the_line[--rl_point];
3120 rl_delete_text (rl_point, rl_point + 1);
3122 if (rl_point == rl_end && alphabetic (c) && last_c_pos)
3125 putc (' ', out_stream);
3128 visible_line[last_c_pos] = '\0';
3134 /* Delete the character under the cursor. Given a numeric argument,
3135 kill that many characters instead. */
3136 rl_delete (count, invoking_key)
3137 int count, invoking_key;
3145 if (rl_point == rl_end)
3153 int orig_point = rl_point;
3155 rl_kill_text (orig_point, rl_point);
3156 rl_point = orig_point;
3159 rl_delete_text (rl_point, rl_point + 1);
3163 /* **************************************************************** */
3167 /* **************************************************************** */
3169 /* The next two functions mimic unix line editing behaviour, except they
3170 save the deleted text on the kill ring. This is safer than not saving
3171 it, and since we have a ring, nobody should get screwed. */
3173 /* This does what C-w does in Unix. We can't prevent people from
3174 using behaviour that they expect. */
3175 rl_unix_word_rubout ()
3177 if (!rl_point) ding ();
3179 int orig_point = rl_point;
3180 while (rl_point && whitespace (the_line[rl_point - 1]))
3182 while (rl_point && !whitespace (the_line[rl_point - 1]))
3184 rl_kill_text (rl_point, orig_point);
3188 /* Here is C-u doing what Unix does. You don't *have* to use these
3189 key-bindings. We have a choice of killing the entire line, or
3190 killing from where we are to the start of the line. We choose the
3191 latter, because if you are a Unix weenie, then you haven't backspaced
3192 into the line at all, and if you aren't, then you know what you are
3194 rl_unix_line_discard ()
3196 if (!rl_point) ding ();
3198 rl_kill_text (rl_point, 0);
3205 /* **************************************************************** */
3207 /* Commands For Typos */
3209 /* **************************************************************** */
3211 /* Random and interesting things in here. */
3213 /* **************************************************************** */
3217 /* **************************************************************** */
3219 /* The three kinds of things that we know how to do. */
3224 /* Uppercase the word at point. */
3225 rl_upcase_word (count)
3228 rl_change_case (count, UpCase);
3231 /* Lowercase the word at point. */
3232 rl_downcase_word (count)
3235 rl_change_case (count, DownCase);
3238 /* Upcase the first letter, downcase the rest. */
3239 rl_capitalize_word (count)
3242 rl_change_case (count, CapCase);
3245 /* The meaty function.
3246 Change the case of COUNT words, performing OP on them.
3247 OP is one of UpCase, DownCase, or CapCase.
3248 If a negative argument is given, leave point where it started,
3249 otherwise, leave it where it moves to. */
3250 rl_change_case (count, op)
3253 register int start = rl_point, end;
3256 rl_forward_word (count);
3266 /* We are going to modify some text, so let's prepare to undo it. */
3267 rl_modifying (start, end);
3269 for (; start < end; start++)
3274 the_line[start] = to_upper (the_line[start]);
3278 the_line[start] = to_lower (the_line[start]);
3284 the_line[start] = to_upper (the_line[start]);
3289 the_line[start] = to_lower (the_line[start]);
3291 if (!pure_alphabetic (the_line[start]))
3302 /* **************************************************************** */
3306 /* **************************************************************** */
3308 /* Transpose the words at point. */
3309 rl_transpose_words (count)
3312 char *word1, *word2;
3313 int w1_beg, w1_end, w2_beg, w2_end;
3314 int orig_point = rl_point;
3318 /* Find the two words. */
3319 rl_forward_word (count);
3321 rl_backward_word (1);
3323 rl_backward_word (count);
3325 rl_forward_word (1);
3328 /* Do some check to make sure that there really are two words. */
3329 if ((w1_beg == w2_beg) || (w2_beg < w1_end))
3332 rl_point = orig_point;
3336 /* Get the text of the words. */
3337 word1 = rl_copy (w1_beg, w1_end);
3338 word2 = rl_copy (w2_beg, w2_end);
3340 /* We are about to do many insertions and deletions. Remember them
3341 as one operation. */
3342 rl_begin_undo_group ();
3344 /* Do the stuff at word2 first, so that we don't have to worry
3345 about word1 moving. */
3347 rl_delete_text (w2_beg, w2_end);
3348 rl_insert_text (word1);
3351 rl_delete_text (w1_beg, w1_end);
3352 rl_insert_text (word2);
3354 /* This is exactly correct since the text before this point has not
3355 changed in length. */
3358 /* I think that does it. */
3359 rl_end_undo_group ();
3360 free (word1); free (word2);
3363 /* Transpose the characters at point. If point is at the end of the line,
3364 then transpose the characters before point. */
3365 rl_transpose_chars (count)
3371 if (!rl_point || rl_end < 2) {
3378 if (rl_point == rl_end)
3380 int t = the_line[rl_point - 1];
3382 the_line[rl_point - 1] = the_line[rl_point - 2];
3383 the_line[rl_point - 2] = t;
3387 int t = the_line[rl_point];
3389 the_line[rl_point] = the_line[rl_point - 1];
3390 the_line[rl_point - 1] = t;
3392 if (count < 0 && rl_point)
3406 /* **************************************************************** */
3408 /* Bogus Flow Control */
3410 /* **************************************************************** */
3412 rl_restart_output (count, key)
3415 int fildes = fileno (rl_outstream);
3416 #if defined (TIOCSTART)
3417 #if defined (apollo)
3418 ioctl (&fildes, TIOCSTART, 0);
3420 ioctl (fildes, TIOCSTART, 0);
3424 # if defined (TERMIOS_TTY_DRIVER)
3425 tcflow (fildes, TCOON);
3427 # if defined (TCXONC)
3428 ioctl (fildes, TCXONC, TCOON);
3429 # endif /* TCXONC */
3430 # endif /* !TERMIOS_TTY_DRIVER */
3431 #endif /* TIOCSTART */
3434 rl_stop_output (count, key)
3437 int fildes = fileno (rl_instream);
3439 #if defined (TIOCSTOP)
3440 # if defined (apollo)
3441 ioctl (&fildes, TIOCSTOP, 0);
3443 ioctl (fildes, TIOCSTOP, 0);
3444 # endif /* apollo */
3446 # if defined (TERMIOS_TTY_DRIVER)
3447 tcflow (fildes, TCOOFF);
3449 # if defined (TCXONC)
3450 ioctl (fildes, TCXONC, TCOON);
3451 # endif /* TCXONC */
3452 # endif /* !TERMIOS_TTY_DRIVER */
3453 #endif /* TIOCSTOP */
3456 /* **************************************************************** */
3458 /* Completion matching, from readline's point of view. */
3460 /* **************************************************************** */
3462 /* Pointer to the generator function for completion_matches ().
3463 NULL means to use filename_entry_function (), the default filename
3465 Function *rl_completion_entry_function = (Function *)NULL;
3467 /* Pointer to alternative function to create matches.
3468 Function is called with TEXT, START, and END.
3469 START and END are indices in RL_LINE_BUFFER saying what the boundaries
3471 If this function exists and returns NULL then call the value of
3472 rl_completion_entry_function to try to match, otherwise use the
3473 array of strings returned. */
3474 Function *rl_attempted_completion_function = (Function *)NULL;
3476 /* Local variable states what happened during the last completion attempt. */
3477 static int completion_changed_buffer = 0;
3479 /* Complete the word at or before point. You have supplied the function
3480 that does the initial simple matching selection algorithm (see
3481 completion_matches ()). The default is to do filename completion. */
3483 rl_complete (ignore, invoking_key)
3484 int ignore, invoking_key;
3486 if (rl_last_func == rl_complete && !completion_changed_buffer)
3487 rl_complete_internal ('?');
3489 rl_complete_internal (TAB);
3492 /* List the possible completions. See description of rl_complete (). */
3493 rl_possible_completions ()
3495 rl_complete_internal ('?');
3498 /* The user must press "y" or "n". Non-zero return means "y" pressed. */
3504 if (c == 'y' || c == 'Y') return (1);
3505 if (c == 'n' || c == 'N') return (0);
3506 if (c == ABORT_CHAR) rl_abort ();
3510 /* Up to this many items will be displayed in response to a
3511 possible-completions call. After that, we ask the user if
3512 she is sure she wants to see them all. */
3513 int rl_completion_query_items = 100;
3515 /* The basic list of characters that signal a break between words for the
3516 completer routine. The contents of this variable is what breaks words
3517 in the shell, i.e. " \t\n\"\\'`@$><=" */
3518 char *rl_basic_word_break_characters = " \t\n\"\\'`@$><=;|&{(";
3520 /* The list of characters that signal a break between words for
3521 rl_complete_internal. The default list is the contents of
3522 rl_basic_word_break_characters. */
3523 char *rl_completer_word_break_characters = (char *)NULL;
3525 /* List of characters that are word break characters, but should be left
3526 in TEXT when it is passed to the completion function. The shell uses
3527 this to help determine what kind of completing to do. */
3528 char *rl_special_prefixes = (char *)NULL;
3530 /* If non-zero, then disallow duplicates in the matches. */
3531 int rl_ignore_completion_duplicates = 1;
3533 /* Non-zero means that the results of the matches are to be treated
3534 as filenames. This is ALWAYS zero on entry, and can only be changed
3535 within a completion entry finder function. */
3536 int rl_filename_completion_desired = 0;
3538 /* This function, if defined, is called by the completer when real
3539 filename completion is done, after all the matching names have been
3540 generated. It is passed a (char**) known as matches in the code below.
3541 It consists of a NULL-terminated array of pointers to potential
3542 matching strings. The 1st element (matches[0]) is the maximal
3543 substring that is common to all matches. This function can re-arrange
3544 the list of matches as required, but all elements of the array must be
3545 free()'d if they are deleted. The main intent of this function is
3546 to implement FIGNORE a la SunOS csh. */
3547 Function *rl_ignore_some_completions_function = (Function *)NULL;
3549 /* Complete the word at or before point.
3550 WHAT_TO_DO says what to do with the completion.
3551 `?' means list the possible completions.
3552 TAB means do standard completion.
3553 `*' means insert all of the possible completions. */
3554 rl_complete_internal (what_to_do)
3557 char *filename_completion_function ();
3558 char **completion_matches (), **matches;
3560 int start, end, delimiter = 0;
3561 char *text, *saved_line_buffer;
3564 saved_line_buffer = savestring (the_line);
3566 saved_line_buffer = (char *)NULL;
3568 if (rl_completion_entry_function)
3569 our_func = rl_completion_entry_function;
3571 our_func = (int (*)())filename_completion_function;
3573 /* Only the completion entry function can change this. */
3574 rl_filename_completion_desired = 0;
3576 /* We now look backwards for the start of a filename/variable word. */
3581 while (--rl_point &&
3582 !rindex (rl_completer_word_break_characters, the_line[rl_point]));
3584 /* If we are at a word break, then advance past it. */
3585 if (rindex (rl_completer_word_break_characters, the_line[rl_point]))
3587 /* If the character that caused the word break was a quoting
3588 character, then remember it as the delimiter. */
3589 if (rindex ("\"'", the_line[rl_point]) && (end - rl_point) > 1)
3590 delimiter = the_line[rl_point];
3592 /* If the character isn't needed to determine something special
3593 about what kind of completion to perform, then advance past it. */
3595 if (!rl_special_prefixes ||
3596 !rindex (rl_special_prefixes, the_line[rl_point]))
3603 text = rl_copy (start, end);
3605 /* If the user wants to TRY to complete, but then wants to give
3606 up and use the default completion function, they set the
3607 variable rl_attempted_completion_function. */
3608 if (rl_attempted_completion_function)
3611 (char **)(*rl_attempted_completion_function) (text, start, end);
3615 our_func = (Function *)NULL;
3616 goto after_usual_completion;
3620 matches = completion_matches (text, our_func);
3622 after_usual_completion:
3633 /* It seems to me that in all the cases we handle we would like
3634 to ignore duplicate possiblilities. Scan for the text to
3635 insert being identical to the other completions. */
3636 if (rl_ignore_completion_duplicates)
3638 char *lowest_common;
3641 /* Sort the items. */
3642 /* It is safe to sort this array, because the lowest common
3643 denominator found in matches[0] will remain in place. */
3644 for (i = 0; matches[i]; i++);
3645 qsort (matches, i, sizeof (char *), compare_strings);
3647 /* Remember the lowest common denominator for it may be unique. */
3648 lowest_common = savestring (matches[0]);
3650 for (i = 0; matches[i + 1]; i++)
3652 if (strcmp (matches[i], matches[i + 1]) == 0)
3655 matches[i] = (char *)-1;
3661 /* We have marked all the dead slots with (char *)-1.
3662 Copy all the non-dead entries into a new array. */
3665 (char **)malloc ((3 + newlen) * sizeof (char *));
3667 for (i = 1, j = 1; matches[i]; i++)
3669 if (matches[i] != (char *)-1)
3670 temp_array[j++] = matches[i];
3673 temp_array[j] = (char *)NULL;
3675 if (matches[0] != (char *)-1)
3680 matches = temp_array;
3683 /* Place the lowest common denominator back in [0]. */
3684 matches[0] = lowest_common;
3686 /* If there is one string left, and it is identical to the
3687 lowest common denominator, then the LCD is the string to
3689 if (j == 2 && strcmp (matches[0], matches[1]) == 0)
3692 matches[1] = (char *)NULL;
3699 /* If we are matching filenames, then here is our chance to
3700 do clever processing by re-examining the list. Call the
3701 ignore function with the array as a parameter. It can
3702 munge the array, deleting matches as it desires. */
3703 if (rl_ignore_some_completions_function &&
3704 our_func == (int (*)())filename_completion_function)
3705 (void)(*rl_ignore_some_completions_function)(matches);
3709 rl_delete_text (start, rl_point);
3711 rl_insert_text (matches[0]);
3714 /* If there are more matches, ring the bell to indicate.
3715 If this was the only match, and we are hacking files,
3716 check the file to see if it was a directory. If so,
3717 add a '/' to the name. If not, and we are at the end
3718 of the line, then add a space. */
3721 ding (); /* There are other matches remaining. */
3725 char temp_string[2];
3727 temp_string[0] = delimiter ? delimiter : ' ';
3728 temp_string[1] = '\0';
3730 if (rl_filename_completion_desired)
3733 char *filename = tilde_expand (matches[0]);
3735 if ((stat (filename, &finfo) == 0) &&
3736 S_ISDIR (finfo.st_mode))
3738 if (the_line[rl_point] != '/')
3739 rl_insert_text ("/");
3743 if (rl_point == rl_end)
3744 rl_insert_text (temp_string);
3750 if (rl_point == rl_end)
3751 rl_insert_text (temp_string);
3760 rl_delete_text (start, rl_point);
3762 rl_begin_undo_group ();
3767 rl_insert_text (matches[i++]);
3768 rl_insert_text (" ");
3773 rl_insert_text (matches[0]);
3774 rl_insert_text (" ");
3776 rl_end_undo_group ();
3782 int len, count, limit, max = 0;
3785 /* Handle simple case first. What if there is only one answer? */
3790 if (rl_filename_completion_desired)
3791 temp = rindex (matches[0], '/');
3793 temp = (char *)NULL;
3801 fprintf (out_stream, "%s", temp);
3806 /* There is more than one answer. Find out how many there are,
3807 and find out what the maximum printed length of a single entry
3809 for (i = 1; matches[i]; i++)
3811 char *temp = (char *)NULL;
3813 /* If we are hacking filenames, then only count the characters
3814 after the last slash in the pathname. */
3815 if (rl_filename_completion_desired)
3816 temp = rindex (matches[i], '/');
3818 temp = (char *)NULL;
3825 if (strlen (temp) > max)
3826 max = strlen (temp);
3831 /* If there are many items, then ask the user if she
3832 really wants to see them all. */
3833 if (len >= rl_completion_query_items)
3836 fprintf (out_stream,
3837 "There are %d possibilities. Do you really", len);
3839 fprintf (out_stream, "wish to see them all? (y or n)");
3840 fflush (out_stream);
3847 /* How many items of MAX length can we fit in the screen window? */
3849 limit = screenwidth / max;
3850 if (limit != 1 && (limit * max == screenwidth))
3853 /* Avoid a possible floating exception. If max > screenwidth,
3854 limit will be 0 and a divide-by-zero fault will result. */
3858 /* How many iterations of the printing loop? */
3859 count = (len + (limit - 1)) / limit;
3861 /* Watch out for special case. If LEN is less than LIMIT, then
3862 just do the inner printing loop. */
3863 if (len < limit) count = 1;
3865 /* Sort the items if they are not already sorted. */
3866 if (!rl_ignore_completion_duplicates)
3867 qsort (matches, len, sizeof (char *), compare_strings);
3869 /* Print the sorted items, up-and-down alphabetically, like
3873 for (i = 1; i < count + 1; i++)
3875 for (j = 0, l = i; j < limit; j++)
3877 if (l > len || !matches[l])
3883 char *temp = (char *)NULL;
3885 if (rl_filename_completion_desired)
3886 temp = rindex (matches[l], '/');
3888 temp = (char *)NULL;
3895 fprintf (out_stream, "%s", temp);
3896 for (k = 0; k < max - strlen (temp); k++)
3897 putc (' ', out_stream);
3913 for (i = 0; matches[i]; i++)
3918 /* Check to see if the line has changed through all of this manipulation. */
3919 if (saved_line_buffer)
3921 if (strcmp (the_line, saved_line_buffer) != 0)
3922 completion_changed_buffer = 1;
3924 completion_changed_buffer = 0;
3926 free (saved_line_buffer);
3930 /* Stupid comparison routine for qsort () ing strings. */
3932 compare_strings (s1, s2)
3935 return (strcmp (*s1, *s2));
3938 /* A completion function for usernames.
3939 TEXT contains a partial username preceded by a random
3940 character (usually `~'). */
3942 username_completion_function (text, state)
3946 static char *username = (char *)NULL;
3947 static struct passwd *entry;
3948 static int namelen, first_char, first_char_loc;
3957 if (first_char == '~')
3962 username = savestring (&text[first_char_loc]);
3963 namelen = strlen (username);
3967 while (entry = getpwent ())
3969 if (strncmp (username, entry->pw_name, namelen) == 0)
3976 return ((char *)NULL);
3980 char *value = (char *)xmalloc (2 + strlen (entry->pw_name));
3984 strcpy (value + first_char_loc, entry->pw_name);
3986 if (first_char == '~')
3987 rl_filename_completion_desired = 1;
3993 /* **************************************************************** */
3995 /* Undo, and Undoing */
3997 /* **************************************************************** */
3999 /* Non-zero tells rl_delete_text and rl_insert_text to not add to
4001 int doing_an_undo = 0;
4003 /* The current undo list for THE_LINE. */
4004 UNDO_LIST *rl_undo_list = (UNDO_LIST *)NULL;
4006 /* Remember how to undo something. Concatenate some undos if that
4008 rl_add_undo (what, start, end, text)
4009 enum undo_code what;
4013 UNDO_LIST *temp = (UNDO_LIST *)xmalloc (sizeof (UNDO_LIST));
4015 temp->start = start;
4018 temp->next = rl_undo_list;
4019 rl_undo_list = temp;
4022 /* Free the existing undo list. */
4025 while (rl_undo_list) {
4026 UNDO_LIST *release = rl_undo_list;
4027 rl_undo_list = rl_undo_list->next;
4029 if (release->what == UNDO_DELETE)
4030 free (release->text);
4036 /* Undo the next thing in the list. Return 0 if there
4037 is nothing to undo, or non-zero if there was. */
4042 int waiting_for_begin = 0;
4050 switch (rl_undo_list->what) {
4052 /* Undoing deletes means inserting some text. */
4054 rl_point = rl_undo_list->start;
4055 rl_insert_text (rl_undo_list->text);
4056 free (rl_undo_list->text);
4059 /* Undoing inserts means deleting some text. */
4061 rl_delete_text (rl_undo_list->start, rl_undo_list->end);
4062 rl_point = rl_undo_list->start;
4065 /* Undoing an END means undoing everything 'til we get to
4068 waiting_for_begin++;
4071 /* Undoing a BEGIN means that we are done with this group. */
4073 if (waiting_for_begin)
4074 waiting_for_begin--;
4082 release = rl_undo_list;
4083 rl_undo_list = rl_undo_list->next;
4086 if (waiting_for_begin)
4092 /* Begin a group. Subsequent undos are undone as an atomic operation. */
4093 rl_begin_undo_group ()
4095 rl_add_undo (UNDO_BEGIN, 0, 0, 0);
4098 /* End an undo group started with rl_begin_undo_group (). */
4099 rl_end_undo_group ()
4101 rl_add_undo (UNDO_END, 0, 0, 0);
4104 /* Save an undo entry for the text from START to END. */
4105 rl_modifying (start, end)
4117 char *temp = rl_copy (start, end);
4118 rl_begin_undo_group ();
4119 rl_add_undo (UNDO_DELETE, start, end, temp);
4120 rl_add_undo (UNDO_INSERT, start, end, (char *)NULL);
4121 rl_end_undo_group ();
4125 /* Revert the current line to its previous state. */
4128 if (!rl_undo_list) ding ();
4130 while (rl_undo_list)
4135 /* Do some undoing of things that were done. */
4136 rl_undo_command (count)
4138 if (count < 0) return; /* Nothing to do. */
4154 /* **************************************************************** */
4156 /* History Utilities */
4158 /* **************************************************************** */
4160 /* We already have a history library, and that is what we use to control
4161 the history features of readline. However, this is our local interface
4162 to the history mechanism. */
4164 /* While we are editing the history, this is the saved
4165 version of the original line. */
4166 HIST_ENTRY *saved_line_for_history = (HIST_ENTRY *)NULL;
4168 /* Set the history pointer back to the last entry in the history. */
4169 start_using_history ()
4172 if (saved_line_for_history)
4173 free_history_entry (saved_line_for_history);
4175 saved_line_for_history = (HIST_ENTRY *)NULL;
4178 /* Free the contents (and containing structure) of a HIST_ENTRY. */
4179 free_history_entry (entry)
4188 /* Perhaps put back the current line if it has changed. */
4189 maybe_replace_line ()
4191 HIST_ENTRY *temp = current_history ();
4193 /* If the current line has changed, save the changes. */
4194 if (temp && ((UNDO_LIST *)(temp->data) != rl_undo_list))
4196 temp = replace_history_entry (where_history (), the_line, rl_undo_list);
4202 /* Put back the saved_line_for_history if there is one. */
4203 maybe_unsave_line ()
4205 if (saved_line_for_history)
4209 line_len = strlen (saved_line_for_history->line);
4211 if (line_len >= rl_line_buffer_len)
4212 rl_extend_line_buffer (line_len);
4214 strcpy (the_line, saved_line_for_history->line);
4215 rl_undo_list = (UNDO_LIST *)saved_line_for_history->data;
4216 free_history_entry (saved_line_for_history);
4217 saved_line_for_history = (HIST_ENTRY *)NULL;
4218 rl_end = rl_point = strlen (the_line);
4224 /* Save the current line in saved_line_for_history. */
4227 if (!saved_line_for_history)
4229 saved_line_for_history = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
4230 saved_line_for_history->line = savestring (the_line);
4231 saved_line_for_history->data = (char *)rl_undo_list;
4235 /* **************************************************************** */
4237 /* History Commands */
4239 /* **************************************************************** */
4241 /* Meta-< goes to the start of the history. */
4242 rl_beginning_of_history ()
4244 rl_get_previous_history (1 + where_history ());
4247 /* Meta-> goes to the end of the history. (The current line). */
4248 rl_end_of_history ()
4250 maybe_replace_line ();
4252 maybe_unsave_line ();
4255 /* Move down to the next history line. */
4256 rl_get_next_history (count)
4259 HIST_ENTRY *temp = (HIST_ENTRY *)NULL;
4263 rl_get_previous_history (-count);
4270 maybe_replace_line ();
4274 temp = next_history ();
4281 maybe_unsave_line ();
4286 line_len = strlen (temp->line);
4288 if (line_len >= rl_line_buffer_len)
4289 rl_extend_line_buffer (line_len);
4291 strcpy (the_line, temp->line);
4292 rl_undo_list = (UNDO_LIST *)temp->data;
4293 rl_end = rl_point = strlen (the_line);
4294 #if defined (VI_MODE)
4295 if (rl_editing_mode == vi_mode)
4297 #endif /* VI_MODE */
4301 /* Get the previous item out of our interactive history, making it the current
4302 line. If there is no previous history, just ding. */
4303 rl_get_previous_history (count)
4306 HIST_ENTRY *old_temp = (HIST_ENTRY *)NULL;
4307 HIST_ENTRY *temp = (HIST_ENTRY *)NULL;
4311 rl_get_next_history (-count);
4318 /* If we don't have a line saved, then save this one. */
4321 /* If the current line has changed, save the changes. */
4322 maybe_replace_line ();
4326 temp = previous_history ();
4334 /* If there was a large argument, and we moved back to the start of the
4335 history, that is not an error. So use the last value found. */
4336 if (!temp && old_temp)
4345 line_len = strlen (temp->line);
4347 if (line_len >= rl_line_buffer_len)
4348 rl_extend_line_buffer (line_len);
4350 strcpy (the_line, temp->line);
4351 rl_undo_list = (UNDO_LIST *)temp->data;
4352 rl_end = rl_point = line_len;
4354 #if defined (VI_MODE)
4355 if (rl_editing_mode == vi_mode)
4357 #endif /* VI_MODE */
4362 /* **************************************************************** */
4364 /* I-Search and Searching */
4366 /* **************************************************************** */
4368 /* Search backwards through the history looking for a string which is typed
4369 interactively. Start with the current line. */
4370 rl_reverse_search_history (sign, key)
4374 rl_search_history (-sign, key);
4377 /* Search forwards through the history looking for a string which is typed
4378 interactively. Start with the current line. */
4379 rl_forward_search_history (sign, key)
4383 rl_search_history (sign, key);
4386 /* Display the current state of the search in the echo-area.
4387 SEARCH_STRING contains the string that is being searched for,
4388 DIRECTION is zero for forward, or 1 for reverse,
4389 WHERE is the history list number of the current line. If it is
4390 -1, then this line is the starting one. */
4391 rl_display_search (search_string, reverse_p, where)
4392 char *search_string;
4393 int reverse_p, where;
4395 char *message = (char *)NULL;
4398 (char *)alloca (1 + (search_string ? strlen (search_string) : 0) + 30);
4402 #if defined (NOTDEF)
4404 sprintf (message, "[%d]", where + history_base);
4407 strcat (message, "(");
4410 strcat (message, "reverse-");
4412 strcat (message, "i-search)`");
4415 strcat (message, search_string);
4417 strcat (message, "': ");
4418 rl_message (message, 0, 0);
4422 /* Search through the history looking for an interactively typed string.
4423 This is analogous to i-search. We start the search in the current line.
4424 DIRECTION is which direction to search; >= 0 means forward, < 0 means
4426 rl_search_history (direction, invoking_key)
4430 /* The string that the user types in to search for. */
4431 char *search_string = (char *)alloca (128);
4433 /* The current length of SEARCH_STRING. */
4434 int search_string_index;
4436 /* The list of lines to search through. */
4439 /* The length of LINES. */
4442 /* Where we get LINES from. */
4443 HIST_ENTRY **hlist = history_list ();
4446 int orig_point = rl_point;
4447 int orig_line = where_history ();
4448 int last_found_line = orig_line;
4451 /* The line currently being searched. */
4454 /* Offset in that line. */
4457 /* Non-zero if we are doing a reverse search. */
4458 int reverse = (direction < 0);
4460 /* Create an arrary of pointers to the lines that we want to search. */
4461 maybe_replace_line ();
4463 for (i = 0; hlist[i]; i++);
4465 /* Allocate space for this many lines, +1 for the current input line,
4466 and remember those lines. */
4467 lines = (char **)alloca ((1 + (hlen = i)) * sizeof (char *));
4468 for (i = 0; i < hlen; i++)
4469 lines[i] = hlist[i]->line;
4471 if (saved_line_for_history)
4472 lines[i] = saved_line_for_history->line;
4474 /* So I have to type it in this way instead. */
4478 /* Keep that mips alloca happy. */
4479 alloced_line = (char *)alloca (1 + strlen (the_line));
4480 lines[i] = alloced_line;
4481 strcpy (lines[i], &the_line[0]);
4486 /* The line where we start the search. */
4489 /* Initialize search parameters. */
4490 *search_string = '\0';
4491 search_string_index = 0;
4493 /* Normalize DIRECTION into 1 or -1. */
4499 rl_display_search (search_string, reverse, -1);
4508 /* Hack C to Do What I Mean. */
4510 Function *f = (Function *)NULL;
4512 if (keymap[c].type == ISFUNC)
4514 f = keymap[c].function;
4516 if (f == rl_reverse_search_history)
4517 c = reverse ? -1 : -2;
4518 else if (f == rl_forward_search_history)
4519 c = !reverse ? -1 : -2;
4529 /* case invoking_key: */
4533 /* switch directions */
4535 direction = -direction;
4536 reverse = (direction < 0);
4541 strcpy (the_line, lines[orig_line]);
4542 rl_point = orig_point;
4543 rl_end = strlen (the_line);
4544 rl_clear_message ();
4548 if (c < 32 || c > 126)
4550 rl_execute_next (c);
4556 search_string[search_string_index++] = c;
4557 search_string[search_string_index] = '\0';
4562 if (!search_string_index)
4569 if (index != strlen (sline))
4582 (search_string, sline + index, search_string_index)
4590 register int limit =
4591 (strlen (sline) - search_string_index) + 1;
4593 while (index < limit)
4595 if (strncmp (search_string,
4597 search_string_index) == 0)
4606 /* At limit for direction? */
4607 if ((reverse && i < 0) ||
4608 (!reverse && i == hlen))
4613 index = strlen (sline);
4617 /* If the search string is longer than the current
4619 if (search_string_index > strlen (sline))
4622 /* Start actually searching. */
4624 index -= search_string_index;
4628 /* We cannot find the search string. Ding the bell. */
4630 i = last_found_line;
4634 /* We have found the search string. Just display it. But don't
4635 actually move there in the history list until the user accepts
4640 line_len = strlen (lines[i]);
4642 if (line_len >= rl_line_buffer_len)
4643 rl_extend_line_buffer (line_len);
4645 strcpy (the_line, lines[i]);
4648 last_found_line = i;
4650 (search_string, reverse, (i == orig_line) ? -1 : i);
4657 /* The searching is over. The user may have found the string that she
4658 was looking for, or else she may have exited a failing search. If
4659 INDEX is -1, then that shows that the string searched for was not
4660 found. We use this to determine where to place rl_point. */
4662 int now = last_found_line;
4664 /* First put back the original state. */
4665 strcpy (the_line, lines[orig_line]);
4667 if (now < orig_line)
4668 rl_get_previous_history (orig_line - now);
4670 rl_get_next_history (now - orig_line);
4672 /* If the index of the "matched" string is less than zero, then the
4673 final search string was never matched, so put point somewhere
4676 index = strlen (the_line);
4679 rl_clear_message ();
4683 /* Make C be the next command to be executed. */
4687 rl_pending_input = c;
4690 /* **************************************************************** */
4692 /* Killing Mechanism */
4694 /* **************************************************************** */
4696 /* What we assume for a max number of kills. */
4697 #define DEFAULT_MAX_KILLS 10
4699 /* The real variable to look at to find out when to flush kills. */
4700 int rl_max_kills = DEFAULT_MAX_KILLS;
4702 /* Where to store killed text. */
4703 char **rl_kill_ring = (char **)NULL;
4705 /* Where we are in the kill ring. */
4706 int rl_kill_index = 0;
4708 /* How many slots we have in the kill ring. */
4709 int rl_kill_ring_length = 0;
4711 /* How to say that you only want to save a certain amount
4712 of kill material. */
4713 rl_set_retained_kills (num)
4717 /* The way to kill something. This appends or prepends to the last
4718 kill, if the last command was a kill command. if FROM is less
4719 than TO, then the text is appended, otherwise prepended. If the
4720 last command was not a kill command, then a new slot is made for
4722 rl_kill_text (from, to)
4726 char *text = rl_copy (from, to);
4728 /* Is there anything to kill? */
4732 last_command_was_kill++;
4736 /* Delete the copied text from the line. */
4737 rl_delete_text (from, to);
4739 /* First, find the slot to work with. */
4740 if (!last_command_was_kill)
4742 /* Get a new slot. */
4745 /* If we don't have any defined, then make one. */
4746 rl_kill_ring = (char **)
4747 xmalloc (((rl_kill_ring_length = 1) + 1) * sizeof (char *));
4752 /* We have to add a new slot on the end, unless we have
4753 exceeded the max limit for remembering kills. */
4754 slot = rl_kill_ring_length;
4755 if (slot == rl_max_kills)
4758 free (rl_kill_ring[0]);
4759 for (i = 0; i < slot; i++)
4760 rl_kill_ring[i] = rl_kill_ring[i + 1];
4766 xrealloc (rl_kill_ring,
4767 ((slot = (rl_kill_ring_length += 1)) + 1)
4775 slot = rl_kill_ring_length - 1;
4778 /* If the last command was a kill, prepend or append. */
4779 if (last_command_was_kill && rl_editing_mode != vi_mode)
4781 char *old = rl_kill_ring[slot];
4782 char *new = (char *)xmalloc (1 + strlen (old) + strlen (text));
4796 rl_kill_ring[slot] = new;
4800 rl_kill_ring[slot] = text;
4802 rl_kill_index = slot;
4803 last_command_was_kill++;
4806 /* Now REMEMBER! In order to do prepending or appending correctly, kill
4807 commands always make rl_point's original position be the FROM argument,
4808 and rl_point's extent be the TO argument. */
4810 /* **************************************************************** */
4812 /* Killing Commands */
4814 /* **************************************************************** */
4816 /* Delete the word at point, saving the text in the kill ring. */
4817 rl_kill_word (count)
4820 int orig_point = rl_point;
4823 rl_backward_kill_word (-count);
4826 rl_forward_word (count);
4828 if (rl_point != orig_point)
4829 rl_kill_text (orig_point, rl_point);
4831 rl_point = orig_point;
4835 /* Rubout the word before point, placing it on the kill ring. */
4836 rl_backward_kill_word (count)
4839 int orig_point = rl_point;
4842 rl_kill_word (-count);
4845 rl_backward_word (count);
4847 if (rl_point != orig_point)
4848 rl_kill_text (orig_point, rl_point);
4852 /* Kill from here to the end of the line. If DIRECTION is negative, kill
4853 back to the line start instead. */
4854 rl_kill_line (direction)
4857 int orig_point = rl_point;
4860 rl_backward_kill_line (1);
4864 if (orig_point != rl_point)
4865 rl_kill_text (orig_point, rl_point);
4866 rl_point = orig_point;
4870 /* Kill backwards to the start of the line. If DIRECTION is negative, kill
4871 forwards to the line end instead. */
4872 rl_backward_kill_line (direction)
4875 int orig_point = rl_point;
4886 rl_kill_text (orig_point, rl_point);
4891 /* Yank back the last killed text. This ignores arguments. */
4894 if (!rl_kill_ring) rl_abort ();
4895 rl_insert_text (rl_kill_ring[rl_kill_index]);
4898 /* If the last command was yank, or yank_pop, and the text just
4899 before point is identical to the current kill item, then
4900 delete that text from the line, rotate the index down, and
4901 yank back some other text. */
4906 if (((rl_last_func != rl_yank_pop) && (rl_last_func != rl_yank)) ||
4912 l = strlen (rl_kill_ring[rl_kill_index]);
4913 if (((rl_point - l) >= 0) &&
4914 (strncmp (the_line + (rl_point - l),
4915 rl_kill_ring[rl_kill_index], l) == 0))
4917 rl_delete_text ((rl_point - l), rl_point);
4920 if (rl_kill_index < 0)
4921 rl_kill_index = rl_kill_ring_length - 1;
4929 /* Yank the COUNTth argument from the previous history line. */
4930 rl_yank_nth_arg (count, ignore)
4933 register HIST_ENTRY *entry = previous_history ();
4944 arg = history_arg_extract (count, count, entry->line);
4951 rl_begin_undo_group ();
4953 #if defined (VI_MODE)
4954 /* Vi mode always inserts a space befoe yanking the argument, and it
4955 inserts it right *after* rl_point. */
4956 if (rl_editing_mode == vi_mode)
4958 #endif /* VI_MODE */
4960 if (rl_point && the_line[rl_point - 1] != ' ')
4961 rl_insert_text (" ");
4963 rl_insert_text (arg);
4966 rl_end_undo_group ();
4969 /* How to toggle back and forth between editing modes. */
4970 rl_vi_editing_mode ()
4972 #if defined (VI_MODE)
4973 rl_editing_mode = vi_mode;
4974 rl_vi_insertion_mode ();
4975 #endif /* VI_MODE */
4978 rl_emacs_editing_mode ()
4980 rl_editing_mode = emacs_mode;
4981 keymap = emacs_standard_keymap;
4985 /* **************************************************************** */
4989 /* **************************************************************** */
4991 /* Non-zero means that case is not significant in completion. */
4992 int completion_case_fold = 0;
4994 /* Return an array of (char *) which is a list of completions for TEXT.
4995 If there are no completions, return a NULL pointer.
4996 The first entry in the returned array is the substitution for TEXT.
4997 The remaining entries are the possible completions.
4998 The array is terminated with a NULL pointer.
5000 ENTRY_FUNCTION is a function of two args, and returns a (char *).
5001 The first argument is TEXT.
5002 The second is a state argument; it should be zero on the first call, and
5003 non-zero on subsequent calls. It returns a NULL pointer to the caller
5004 when there are no more matches.
5007 completion_matches (text, entry_function)
5009 char *(*entry_function) ();
5011 /* Number of slots in match_list. */
5012 int match_list_size;
5014 /* The list of matches. */
5016 (char **)xmalloc (((match_list_size = 10) + 1) * sizeof (char *));
5018 /* Number of matches actually found. */
5021 /* Temporary string binder. */
5024 match_list[1] = (char *)NULL;
5026 while (string = (*entry_function) (text, matches))
5028 if (matches + 1 == match_list_size)
5029 match_list = (char **)xrealloc
5030 (match_list, ((match_list_size += 10) + 1) * sizeof (char *));
5032 match_list[++matches] = string;
5033 match_list[matches + 1] = (char *)NULL;
5036 /* If there were any matches, then look through them finding out the
5037 lowest common denominator. That then becomes match_list[0]. */
5041 int low = 100000; /* Count of max-matched characters. */
5043 /* If only one match, just use that. */
5046 match_list[0] = match_list[1];
5047 match_list[1] = (char *)NULL;
5051 /* Otherwise, compare each member of the list with
5052 the next, finding out where they stop matching. */
5056 register int c1, c2, si;
5058 if (completion_case_fold)
5061 (c1 = to_lower(match_list[i][si])) &&
5062 (c2 = to_lower(match_list[i + 1][si]));
5064 if (c1 != c2) break;
5069 (c1 = match_list[i][si]) &&
5070 (c2 = match_list[i + 1][si]);
5072 if (c1 != c2) break;
5075 if (low > si) low = si;
5078 match_list[0] = (char *)xmalloc (low + 1);
5079 strncpy (match_list[0], match_list[1], low);
5080 match_list[0][low] = '\0';
5083 else /* There were no matches. */
5086 match_list = (char **)NULL;
5088 return (match_list);
5091 /* Okay, now we write the entry_function for filename completion. In the
5092 general case. Note that completion in the shell is a little different
5093 because of all the pathnames that must be followed when looking up the
5094 completion for a command. */
5096 filename_completion_function (text, state)
5100 static DIR *directory;
5101 static char *filename = (char *)NULL;
5102 static char *dirname = (char *)NULL;
5103 static char *users_dirname = (char *)NULL;
5104 static int filename_len;
5106 dirent *entry = (dirent *)NULL;
5108 /* If we don't have any state, then do some initialization. */
5113 if (dirname) free (dirname);
5114 if (filename) free (filename);
5115 if (users_dirname) free (users_dirname);
5117 filename = savestring (text);
5118 if (!*text) text = ".";
5119 dirname = savestring (text);
5121 temp = rindex (dirname, '/');
5125 strcpy (filename, ++temp);
5129 strcpy (dirname, ".");
5131 /* We aren't done yet. We also support the "~user" syntax. */
5133 /* Save the version of the directory that the user typed. */
5134 users_dirname = savestring (dirname);
5138 temp_dirname = tilde_expand (dirname);
5140 dirname = temp_dirname;
5142 if (rl_symbolic_link_hook)
5143 (*rl_symbolic_link_hook) (&dirname);
5145 directory = opendir (dirname);
5146 filename_len = strlen (filename);
5148 rl_filename_completion_desired = 1;
5151 /* At this point we should entertain the possibility of hacking wildcarded
5152 filenames, like /usr/man/man<WILD>/te<TAB>. If the directory name
5153 contains globbing characters, then build an array of directories to
5154 glob on, and glob on the first one. */
5156 /* Now that we have some state, we can read the directory. */
5158 while (directory && (entry = readdir (directory)))
5160 /* Special case for no filename.
5161 All entries except "." and ".." match. */
5164 if ((strcmp (entry->d_name, ".") != 0) &&
5165 (strcmp (entry->d_name, "..") != 0))
5170 /* Otherwise, if these match upto the length of filename, then
5172 if (entry->d_name[0] == filename[0] && /* Quick test */
5173 (strncmp (filename, entry->d_name, filename_len) == 0))
5184 closedir (directory);
5185 directory = (DIR *)NULL;
5187 return (char *)NULL;
5193 if (dirname && (strcmp (dirname, ".") != 0))
5196 xmalloc (1 + strlen (users_dirname) + strlen (entry->d_name));
5197 strcpy (temp, users_dirname);
5198 strcat (temp, entry->d_name);
5202 temp = (savestring (entry->d_name));
5209 /* **************************************************************** */
5213 /* **************************************************************** */
5215 /* rl_add_defun (char *name, Function *function, int key)
5216 Add NAME to the list of named functions. Make FUNCTION
5217 be the function that gets called.
5218 If KEY is not -1, then bind it. */
5219 rl_add_defun (name, function, key)
5225 rl_bind_key (key, function);
5226 rl_add_funmap_entry (name, function);
5229 /* Bind KEY to FUNCTION. Returns non-zero if KEY is out of range. */
5231 rl_bind_key (key, function)
5238 if (key > 127 && key < 256)
5240 if (keymap[ESC].type == ISKMAP)
5242 Keymap escmap = (Keymap)keymap[ESC].function;
5245 escmap[key].type = ISFUNC;
5246 escmap[key].function = function;
5252 keymap[key].type = ISFUNC;
5253 keymap[key].function = function;
5257 /* Bind KEY to FUNCTION in MAP. Returns non-zero in case of invalid
5260 rl_bind_key_in_map (key, function, map)
5266 Keymap oldmap = keymap;
5269 result = rl_bind_key (key, function);
5274 /* Make KEY do nothing in the currently selected keymap.
5275 Returns non-zero in case of error. */
5280 return (rl_bind_key (key, (Function *)NULL));
5283 /* Make KEY do nothing in MAP.
5284 Returns non-zero in case of error. */
5286 rl_unbind_key_in_map (key, map)
5290 return (rl_bind_key_in_map (key, (Function *)NULL, map));
5293 /* Bind the key sequence represented by the string KEYSEQ to
5294 FUNCTION. This makes new keymaps as necessary. The initial
5295 place to do bindings is in MAP. */
5296 rl_set_key (keyseq, function, map)
5301 rl_generic_bind (ISFUNC, keyseq, function, map);
5304 /* Bind the key sequence represented by the string KEYSEQ to
5305 the string of characters MACRO. This makes new keymaps as
5306 necessary. The initial place to do bindings is in MAP. */
5307 rl_macro_bind (keyseq, macro, map)
5308 char *keyseq, *macro;
5314 macro_keys = (char *)xmalloc ((2 * strlen (macro)) + 1);
5316 if (rl_translate_keyseq (macro, macro_keys, ¯o_keys_len))
5321 rl_generic_bind (ISMACR, keyseq, macro_keys, map);
5324 /* Bind the key sequence represented by the string KEYSEQ to
5325 the arbitrary pointer DATA. TYPE says what kind of data is
5326 pointed to by DATA, right now this can be a function (ISFUNC),
5327 a macro (ISMACR), or a keymap (ISKMAP). This makes new keymaps
5328 as necessary. The initial place to do bindings is in MAP. */
5329 rl_generic_bind (type, keyseq, data, map)
5331 char *keyseq, *data;
5338 /* If no keys to bind to, exit right away. */
5339 if (!keyseq || !*keyseq)
5346 keys = (char *)alloca (1 + (2 * strlen (keyseq)));
5348 /* Translate the ASCII representation of KEYSEQ into an array
5349 of characters. Stuff the characters into ARRAY, and the
5350 length of ARRAY into LENGTH. */
5351 if (rl_translate_keyseq (keyseq, keys, &keys_len))
5354 /* Bind keys, making new keymaps as necessary. */
5355 for (i = 0; i < keys_len; i++)
5357 if (i + 1 < keys_len)
5359 if (map[keys[i]].type != ISKMAP)
5361 if (map[i].type == ISMACR)
5362 free ((char *)map[i].function);
5364 map[keys[i]].type = ISKMAP;
5365 map[keys[i]].function = (Function *)rl_make_bare_keymap ();
5367 map = (Keymap)map[keys[i]].function;
5371 if (map[keys[i]].type == ISMACR)
5372 free ((char *)map[keys[i]].function);
5374 map[keys[i]].function = (Function *)data;
5375 map[keys[i]].type = type;
5380 /* Translate the ASCII representation of SEQ, stuffing the
5381 values into ARRAY, an array of characters. LEN gets the
5382 final length of ARRAY. Return non-zero if there was an
5383 error parsing SEQ. */
5384 rl_translate_keyseq (seq, array, len)
5388 register int i, c, l = 0;
5390 for (i = 0; c = seq[i]; i++)
5399 if (((c == 'C' || c == 'M') && seq[i + 1] == '-') ||
5402 /* Handle special case of backwards define. */
5403 if (strncmp (&seq[i], "C-\\M-", 5) == 0)
5407 array[l++] = CTRL (to_upper (seq[i]));
5422 /* Special hack for C-?... */
5424 array[l++] = RUBOUT;
5426 array[l++] = CTRL (to_upper (seq[i]));
5444 /* Return a pointer to the function that STRING represents.
5445 If STRING doesn't have a matching function, then a NULL pointer
5448 rl_named_function (string)
5453 for (i = 0; funmap[i]; i++)
5454 if (stricmp (funmap[i]->name, string) == 0)
5455 return (funmap[i]->function);
5456 return ((Function *)NULL);
5459 /* The last key bindings file read. */
5460 static char *last_readline_init_file = "~/.inputrc";
5462 /* Re-read the current keybindings file. */
5463 rl_re_read_init_file (count, ignore)
5466 rl_read_init_file ((char *)NULL);
5469 /* Do key bindings from a file. If FILENAME is NULL it defaults
5470 to `~/.inputrc'. If the file existed and could be opened and
5471 read, 0 is returned, otherwise errno is returned. */
5473 rl_read_init_file (filename)
5477 char *buffer, *openname, *line, *end;
5481 /* Default the filename. */
5483 filename = last_readline_init_file;
5485 openname = tilde_expand (filename);
5487 if ((stat (openname, &finfo) < 0) ||
5488 (file = open (openname, O_RDONLY, 0666)) < 0)
5496 last_readline_init_file = filename;
5498 /* Read the file into BUFFER. */
5499 buffer = (char *)xmalloc (finfo.st_size + 1);
5500 i = read (file, buffer, finfo.st_size);
5503 if (i != finfo.st_size)
5506 /* Loop over the lines in the file. Lines that start with `#' are
5507 comments; all other lines are commands for readline initialization. */
5509 end = buffer + finfo.st_size;
5512 /* Find the end of this line. */
5513 for (i = 0; line + i != end && line[i] != '\n'; i++);
5515 /* Mark end of line. */
5518 /* If the line is not a comment, then parse it. */
5520 rl_parse_and_bind (line);
5522 /* Move to the next line. */
5528 /* **************************************************************** */
5530 /* Parser Directives */
5532 /* **************************************************************** */
5536 /* Calling programs set this to have their argv[0]. */
5537 char *rl_readline_name = "other";
5539 /* Stack of previous values of parsing_conditionalized_out. */
5540 static unsigned char *if_stack = (unsigned char *)NULL;
5541 static int if_stack_depth = 0;
5542 static int if_stack_size = 0;
5544 /* Push parsing_conditionalized_out, and set parser state based on ARGS. */
5550 /* Push parser state. */
5551 if (if_stack_depth + 1 >= if_stack_size)
5554 if_stack = (unsigned char *)xmalloc (if_stack_size = 20);
5556 if_stack = (unsigned char *)xrealloc (if_stack, if_stack_size += 20);
5558 if_stack[if_stack_depth++] = parsing_conditionalized_out;
5560 /* If parsing is turned off, then nothing can turn it back on except
5561 for finding the matching endif. In that case, return right now. */
5562 if (parsing_conditionalized_out)
5565 /* Isolate first argument. */
5566 for (i = 0; args[i] && !whitespace (args[i]); i++);
5571 /* Handle "if term=foo" and "if mode=emacs" constructs. If this
5572 isn't term=foo, or mode=emacs, then check to see if the first
5573 word in ARGS is the same as the value stored in rl_readline_name. */
5574 if (rl_terminal_name && strnicmp (args, "term=", 5) == 0)
5578 /* Terminals like "aaa-60" are equivalent to "aaa". */
5579 tname = savestring (rl_terminal_name);
5580 tem = rindex (tname, '-');
5584 if (stricmp (args + 5, tname) == 0)
5585 parsing_conditionalized_out = 0;
5587 parsing_conditionalized_out = 1;
5589 #if defined (VI_MODE)
5590 else if (strnicmp (args, "mode=", 5) == 0)
5594 if (stricmp (args + 5, "emacs") == 0)
5596 else if (stricmp (args + 5, "vi") == 0)
5601 if (mode == rl_editing_mode)
5602 parsing_conditionalized_out = 0;
5604 parsing_conditionalized_out = 1;
5606 #endif /* VI_MODE */
5607 /* Check to see if the first word in ARGS is the same as the
5608 value stored in rl_readline_name. */
5609 else if (stricmp (args, rl_readline_name) == 0)
5610 parsing_conditionalized_out = 0;
5612 parsing_conditionalized_out = 1;
5615 /* Invert the current parser state if there is anything on the stack. */
5621 if (!if_stack_depth)
5623 /* Error message? */
5627 /* Check the previous (n - 1) levels of the stack to make sure that
5628 we haven't previously turned off parsing. */
5629 for (i = 0; i < if_stack_depth - 1; i++)
5630 if (if_stack[i] == 1)
5633 /* Invert the state of parsing if at top level. */
5634 parsing_conditionalized_out = !parsing_conditionalized_out;
5637 /* Terminate a conditional, popping the value of
5638 parsing_conditionalized_out from the stack. */
5643 parsing_conditionalized_out = if_stack[--if_stack_depth];
5646 /* *** What, no error message? *** */
5650 /* Associate textual names with actual functions. */
5654 } parser_directives [] = {
5655 { "if", parser_if },
5656 { "endif", parser_endif },
5657 { "else", parser_else },
5658 { (char *)0x0, (Function *)0x0 }
5661 /* Handle a parser directive. STATEMENT is the line of the directive
5662 without any leading `$'. */
5664 handle_parser_directive (statement)
5668 char *directive, *args;
5670 /* Isolate the actual directive. */
5672 /* Skip whitespace. */
5673 for (i = 0; whitespace (statement[i]); i++);
5675 directive = &statement[i];
5677 for (; statement[i] && !whitespace (statement[i]); i++);
5680 statement[i++] = '\0';
5682 for (; statement[i] && whitespace (statement[i]); i++);
5684 args = &statement[i];
5686 /* Lookup the command, and act on it. */
5687 for (i = 0; parser_directives[i].name; i++)
5688 if (stricmp (directive, parser_directives[i].name) == 0)
5690 (*parser_directives[i].function) (args);
5694 /* *** Should an error message be output? */
5698 /* Ugly but working hack for binding prefix meta. */
5699 #define PREFIX_META_HACK
5701 static int substring_member_of_array ();
5703 /* Read the binding command from STRING and perform it.
5704 A key binding command looks like: Keyname: function-name\0,
5705 a variable binding command looks like: set variable value.
5706 A new-style keybinding looks like "\C-x\C-x": exchange-point-and-mark. */
5707 rl_parse_and_bind (string)
5710 extern char *possible_control_prefixes[], *possible_meta_prefixes[];
5711 char *funname, *kname;
5715 while (string && whitespace (*string))
5718 if (!string || !*string || *string == '#')
5721 /* If this is a parser directive, act on it. */
5724 handle_parser_directive (&string[1]);
5728 /* If we are supposed to be skipping parsing right now, then do it. */
5729 if (parsing_conditionalized_out)
5733 /* If this keyname is a complex key expression surrounded by quotes,
5734 advance to after the matching close quote. */
5737 for (i = 1; c = string[i]; i++)
5739 if (c == '"' && string[i - 1] != '\\')
5744 /* Advance to the colon (:) or whitespace which separates the two objects. */
5745 for (; (c = string[i]) && c != ':' && c != ' ' && c != '\t'; i++ );
5747 /* Mark the end of the command (or keyname). */
5751 /* If this is a command to set a variable, then do that. */
5752 if (stricmp (string, "set") == 0)
5754 char *var = string + i;
5757 /* Make VAR point to start of variable name. */
5758 while (*var && whitespace (*var)) var++;
5760 /* Make value point to start of value string. */
5762 while (*value && !whitespace (*value)) value++;
5765 while (*value && whitespace (*value)) value++;
5767 rl_variable_bind (var, value);
5771 /* Skip any whitespace between keyname and funname. */
5772 for (; string[i] && whitespace (string[i]); i++);
5773 funname = &string[i];
5775 /* Now isolate funname.
5776 For straight function names just look for whitespace, since
5777 that will signify the end of the string. But this could be a
5778 macro definition. In that case, the string is quoted, so skip
5779 to the matching delimiter. */
5780 if (*funname == '\'' || *funname == '"')
5782 int delimiter = string[i++];
5784 for (; c = string[i]; i++)
5786 if (c == delimiter && string[i - 1] != '\\')
5793 /* Advance to the end of the string. */
5794 for (; string[i] && !whitespace (string[i]); i++);
5796 /* No extra whitespace at the end of the string. */
5799 /* If this is a new-style key-binding, then do the binding with
5800 rl_set_key (). Otherwise, let the older code deal with it. */
5803 char *seq = (char *)alloca (1 + strlen (string));
5804 register int j, k = 0;
5806 for (j = 1; string[j]; j++)
5808 if (string[j] == '"' && string[j - 1] != '\\')
5811 seq[k++] = string[j];
5815 /* Binding macro? */
5816 if (*funname == '\'' || *funname == '"')
5818 j = strlen (funname);
5820 if (j && funname[j - 1] == *funname)
5821 funname[j - 1] = '\0';
5823 rl_macro_bind (seq, &funname[1], keymap);
5826 rl_set_key (seq, rl_named_function (funname), keymap);
5831 /* Get the actual character we want to deal with. */
5832 kname = rindex (string, '-');
5838 key = glean_key_from_name (kname);
5840 /* Add in control and meta bits. */
5841 if (substring_member_of_array (string, possible_control_prefixes))
5842 key = CTRL (to_upper (key));
5844 if (substring_member_of_array (string, possible_meta_prefixes))
5847 /* Temporary. Handle old-style keyname with macro-binding. */
5848 if (*funname == '\'' || *funname == '"')
5851 int fl = strlen (funname);
5853 seq[0] = key; seq[1] = '\0';
5854 if (fl && funname[fl - 1] == *funname)
5855 funname[fl - 1] = '\0';
5857 rl_macro_bind (seq, &funname[1], keymap);
5859 #if defined (PREFIX_META_HACK)
5860 /* Ugly, but working hack to keep prefix-meta around. */
5861 else if (stricmp (funname, "prefix-meta") == 0)
5867 rl_generic_bind (ISKMAP, seq, (char *)emacs_meta_keymap, keymap);
5869 #endif /* PREFIX_META_HACK */
5871 rl_bind_key (key, rl_named_function (funname));
5874 rl_variable_bind (name, value)
5877 if (stricmp (name, "editing-mode") == 0)
5879 if (strnicmp (value, "vi", 2) == 0)
5881 #if defined (VI_MODE)
5882 keymap = vi_insertion_keymap;
5883 rl_editing_mode = vi_mode;
5885 #if defined (NOTDEF)
5886 /* What state is the terminal in? I'll tell you:
5887 non-determinate! That means we cannot do any output. */
5890 #endif /* VI_MODE */
5892 else if (strnicmp (value, "emacs", 5) == 0)
5894 keymap = emacs_standard_keymap;
5895 rl_editing_mode = emacs_mode;
5898 else if (stricmp (name, "horizontal-scroll-mode") == 0)
5900 if (!*value || stricmp (value, "On") == 0)
5901 horizontal_scroll_mode = 1;
5903 horizontal_scroll_mode = 0;
5905 else if (stricmp (name, "mark-modified-lines") == 0)
5907 if (!*value || stricmp (value, "On") == 0)
5908 mark_modified_lines = 1;
5910 mark_modified_lines = 0;
5912 else if (stricmp (name, "prefer-visible-bell") == 0)
5914 if (!*value || stricmp (value, "On") == 0)
5915 prefer_visible_bell = 1;
5917 prefer_visible_bell = 0;
5919 else if (stricmp (name, "comment-begin") == 0)
5921 #if defined (VI_MODE)
5922 extern char *rl_vi_comment_begin;
5926 if (rl_vi_comment_begin)
5927 free (rl_vi_comment_begin);
5929 rl_vi_comment_begin = savestring (value);
5931 #endif /* VI_MODE */
5935 /* Return the character which matches NAME.
5936 For example, `Space' returns ' '. */
5943 assoc_list name_key_alist[] = {
5946 { "Escape", '\033' },
5948 { "Newline", '\n' },
5959 glean_key_from_name (name)
5964 for (i = 0; name_key_alist[i].name; i++)
5965 if (stricmp (name, name_key_alist[i].name) == 0)
5966 return (name_key_alist[i].value);
5972 /* **************************************************************** */
5974 /* Key Binding and Function Information */
5976 /* **************************************************************** */
5978 /* Each of the following functions produces information about the
5979 state of keybindings and functions known to Readline. The info
5980 is always printed to rl_outstream, and in such a way that it can
5981 be read back in (i.e., passed to rl_parse_and_bind (). */
5983 /* Print the names of functions known to Readline. */
5985 rl_list_funmap_names (ignore)
5989 char **funmap_names;
5990 extern char **rl_funmap_names ();
5992 funmap_names = rl_funmap_names ();
5997 for (i = 0; funmap_names[i]; i++)
5998 fprintf (rl_outstream, "%s\n", funmap_names[i]);
6000 free (funmap_names);
6003 /* Return a NULL terminated array of strings which represent the key
6004 sequences that are used to invoke FUNCTION in MAP. */
6006 invoking_keyseqs_in_map (function, map)
6012 int result_index, result_size;
6014 result = (char **)NULL;
6015 result_index = result_size = 0;
6017 for (key = 0; key < 128; key++)
6019 switch (map[key].type)
6022 /* Macros match, if, and only if, the pointers are identical.
6023 Thus, they are treated exactly like functions in here. */
6025 /* If the function in the keymap is the one we are looking for,
6026 then add the current KEY to the list of invoking keys. */
6027 if (map[key].function == function)
6029 char *keyname = (char *)xmalloc (5);
6032 sprintf (keyname, "\\C-%c", to_lower (UNCTRL (key)));
6033 else if (key == RUBOUT)
6034 sprintf (keyname, "\\C-?");
6036 sprintf (keyname, "%c", key);
6038 if (result_index + 2 > result_size)
6041 result = (char **) xmalloc
6042 ((result_size = 10) * sizeof (char *));
6044 result = (char **) xrealloc
6045 (result, (result_size += 10) * sizeof (char *));
6048 result[result_index++] = keyname;
6049 result[result_index] = (char *)NULL;
6055 char **seqs = (char **)NULL;
6057 /* Find the list of keyseqs in this map which have FUNCTION as
6058 their target. Add the key sequences found to RESULT. */
6059 if (map[key].function)
6061 invoking_keyseqs_in_map (function, (Keymap)map[key].function);
6067 for (i = 0; seqs[i]; i++)
6069 char *keyname = (char *)xmalloc (6 + strlen (seqs[i]));
6072 sprintf (keyname, "\\e");
6073 else if (CTRL_P (key))
6074 sprintf (keyname, "\\C-%c", to_lower (UNCTRL (key)));
6075 else if (key == RUBOUT)
6076 sprintf (keyname, "\\C-?");
6078 sprintf (keyname, "%c", key);
6080 strcat (keyname, seqs[i]);
6082 if (result_index + 2 > result_size)
6086 xmalloc ((result_size = 10) * sizeof (char *));
6090 (result_size += 10) * sizeof (char *));
6093 result[result_index++] = keyname;
6094 result[result_index] = (char *)NULL;
6104 /* Return a NULL terminated array of strings which represent the key
6105 sequences that can be used to invoke FUNCTION using the current keymap. */
6107 rl_invoking_keyseqs (function)
6110 return (invoking_keyseqs_in_map (function, keymap));
6113 /* Print all of the current functions and their bindings to
6114 rl_outstream. If an explicit argument is given, then print
6115 the output in such a way that it can be read back in. */
6117 rl_dump_functions (count)
6120 void rl_function_dumper ();
6122 rl_function_dumper (rl_explicit_arg);
6127 /* Print all of the functions and their bindings to rl_outstream. If
6128 PRINT_READABLY is non-zero, then print the output in such a way
6129 that it can be read back in. */
6131 rl_function_dumper (print_readably)
6135 char **rl_funmap_names (), **names;
6138 names = rl_funmap_names ();
6140 fprintf (rl_outstream, "\n");
6142 for (i = 0; name = names[i]; i++)
6147 function = rl_named_function (name);
6148 invokers = invoking_keyseqs_in_map (function, keymap);
6153 fprintf (rl_outstream, "# %s (not bound)\n", name);
6158 for (j = 0; invokers[j]; j++)
6160 fprintf (rl_outstream, "\"%s\": %s\n",
6171 fprintf (rl_outstream, "%s is not bound to any keys\n",
6177 fprintf (rl_outstream, "%s can be found on ", name);
6179 for (j = 0; invokers[j] && j < 5; j++)
6181 fprintf (rl_outstream, "\"%s\"%s", invokers[j],
6182 invokers[j + 1] ? ", " : ".\n");
6185 if (j == 5 && invokers[j])
6186 fprintf (rl_outstream, "...\n");
6188 for (j = 0; invokers[j]; j++)
6198 /* **************************************************************** */
6200 /* String Utility Functions */
6202 /* **************************************************************** */
6204 static char *strindex ();
6206 /* Return non-zero if any members of ARRAY are a substring in STRING. */
6208 substring_member_of_array (string, array)
6209 char *string, **array;
6213 if (strindex (string, *array))
6220 /* Whoops, Unix doesn't have strnicmp. */
6222 /* Compare at most COUNT characters from string1 to string2. Case
6225 strnicmp (string1, string2, count)
6226 char *string1, *string2;
6228 register char ch1, ch2;
6234 if (to_upper(ch1) == to_upper(ch2))
6241 /* strcmp (), but caseless. */
6243 stricmp (string1, string2)
6244 char *string1, *string2;
6246 register char ch1, ch2;
6248 while (*string1 && *string2)
6252 if (to_upper(ch1) != to_upper(ch2))
6255 return (*string1 | *string2);
6258 /* Determine if s2 occurs in s1. If so, return a pointer to the
6259 match in s1. The compare is case insensitive. */
6262 register char *s1, *s2;
6264 register int i, l = strlen (s2);
6265 register int len = strlen (s1);
6267 for (i = 0; (len - i) >= l; i++)
6268 if (strnicmp (&s1[i], s2, l) == 0)
6270 return ((char *)NULL);
6274 /* **************************************************************** */
6276 /* USG (System V) Support */
6278 /* **************************************************************** */
6280 /* When compiling and running in the `Posix' environment, Ultrix does
6281 not restart system calls, so this needs to do it. */
6291 result = read (fileno (stream), &c, sizeof (char));
6293 if (result == sizeof (char))
6296 /* If zero characters are returned, then the file that we are
6297 reading from is empty! Return EOF in that case. */
6301 /* If the error that we received was SIGINT, then try again,
6302 this is simply an interrupted system call to read ().
6303 Otherwise, some error ocurred, also signifying EOF. */
6309 #if defined (STATIC_MALLOC)
6311 /* **************************************************************** */
6313 /* xmalloc and xrealloc () */
6315 /* **************************************************************** */
6317 static void memory_error_and_abort ();
6323 char *temp = (char *)malloc (bytes);
6326 memory_error_and_abort ();
6331 xrealloc (pointer, bytes)
6338 temp = (char *)malloc (bytes);
6340 temp = (char *)realloc (pointer, bytes);
6343 memory_error_and_abort ();
6349 memory_error_and_abort ()
6351 fprintf (stderr, "readline: Out of virtual memory!\n");
6354 #endif /* STATIC_MALLOC */
6357 /* **************************************************************** */
6359 /* Testing Readline */
6361 /* **************************************************************** */
6367 HIST_ENTRY **history_list ();
6368 char *temp = (char *)NULL;
6369 char *prompt = "readline% ";
6374 temp = readline (prompt);
6380 /* If there is anything on the line, print it and remember it. */
6383 fprintf (stderr, "%s\r\n", temp);
6387 /* Check for `command' that we handle. */
6388 if (strcmp (temp, "quit") == 0)
6391 if (strcmp (temp, "list") == 0)
6393 HIST_ENTRY **list = history_list ();
6397 for (i = 0; list[i]; i++)
6399 fprintf (stderr, "%d: %s\r\n", i, list[i]->line);
6400 free (list[i]->line);
6414 * compile-command: "gcc -g -traditional -I. -I.. -DTEST -o readline readline.c keymaps.o funmap.o history.o -ltermcap"