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 */
51 #undef HAVE_BSD_SIGNALS
54 /* Some USG machines have BSD signal handling (sigblock, sigsetmask, etc.) */
55 #if defined (USG) && !defined (hpux)
56 #undef HAVE_BSD_SIGNALS
59 /* System V machines use termio. */
60 #if !defined (_POSIX_VERSION)
61 # if defined (USG) || defined (hpux) || defined (Xenix) || defined (sgi) || defined (DGUX)
62 # undef NEW_TTY_DRIVER
63 # define TERMIO_TTY_DRIVER
68 # endif /* USG || hpux || Xenix || sgi || DUGX */
69 #endif /* !_POSIX_VERSION */
71 /* Posix systems use termios and the Posix signal functions. */
72 #if defined (_POSIX_VERSION)
73 # if !defined (TERMIOS_MISSING)
74 # undef NEW_TTY_DRIVER
75 # define TERMIOS_TTY_DRIVER
77 # endif /* !TERMIOS_MISSING */
78 # define HAVE_POSIX_SIGNALS
79 # if !defined (O_NDELAY)
80 # define O_NDELAY O_NONBLOCK /* Posix-style non-blocking i/o */
81 # endif /* O_NDELAY */
82 #endif /* _POSIX_VERSION */
84 /* Other (BSD) machines use sgtty. */
85 #if defined (NEW_TTY_DRIVER)
89 /* Define _POSIX_VDISABLE if we are not using the `new' tty driver and
90 it is not already defined. It is used both to determine if a
91 special character is disabled and to disable certain special
92 characters. Posix systems should set to 0, USG systems to -1. */
93 #if !defined (NEW_TTY_DRIVER) && !defined (_POSIX_VDISABLE)
94 # if defined (_POSIX_VERSION)
95 # define _POSIX_VDISABLE 0
96 # else /* !_POSIX_VERSION */
97 # define _POSIX_VDISABLE -1
98 # endif /* !_POSIX_VERSION */
99 #endif /* !NEW_TTY_DRIVER && !_POSIX_VDISABLE */
105 #include <sys/stat.h>
107 /* Posix macro to check file in statbuf for directory-ness. */
108 #if defined (S_IFDIR) && !defined (S_ISDIR)
109 #define S_ISDIR(m) (((m)&S_IFMT) == S_IFDIR)
113 /* These next are for filename completion. Perhaps this belongs
114 in a different place. */
116 #endif /* __MSDOS__ */
118 #if defined (USG) && !defined (isc386) && !defined (sgi)
119 struct passwd *getpwuid (), *getpwent ();
122 /* #define HACK_TERMCAP_MOTION */
124 /* Some standard library routines. */
125 #include "readline.h"
129 #define digit(c) ((c) >= '0' && (c) <= '9')
133 #define isletter(c) (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z'))
137 #define digit_value(c) ((c) - '0')
141 #define member(c, s) ((c) ? index ((s), (c)) : 0)
145 #define isident(c) ((isletter(c) || digit(c) || c == '_'))
149 #define exchange(x, y) {int temp = x; x = y; y = temp;}
152 #if !defined (rindex)
153 extern char *rindex ();
157 extern char *index ();
160 extern char *getenv ();
161 extern char *tilde_expand ();
163 static update_line ();
164 static void output_character_function ();
165 static delete_chars ();
166 static insert_some_chars ();
168 #if defined (VOID_SIGHANDLER)
169 # define sighandler void
171 # define sighandler int
172 #endif /* VOID_SIGHANDLER */
174 /* This typedef is equivalant to the one for Function; it allows us
175 to say SigHandler *foo = signal (SIGKILL, SIG_IGN); */
176 typedef sighandler SigHandler ();
178 /* If on, then readline handles signals in a way that doesn't screw. */
179 #define HANDLE_SIGNALS
183 #undef HANDLE_SIGNALS
187 /* **************************************************************** */
189 /* Line editing input utility */
191 /* **************************************************************** */
193 /* A pointer to the keymap that is currently in use.
194 By default, it is the standard emacs keymap. */
195 Keymap keymap = emacs_standard_keymap;
201 /* The current style of editing. */
202 int rl_editing_mode = emacs_mode;
204 /* Non-zero if the previous command was a kill command. */
205 static int last_command_was_kill = 0;
207 /* The current value of the numeric argument specified by the user. */
208 int rl_numeric_arg = 1;
210 /* Non-zero if an argument was typed. */
211 int rl_explicit_arg = 0;
213 /* Temporary value used while generating the argument. */
216 /* Non-zero means we have been called at least once before. */
217 static int rl_initialized = 0;
219 /* If non-zero, this program is running in an EMACS buffer. */
220 static char *running_in_emacs = (char *)NULL;
222 /* The current offset in the current input line. */
225 /* Mark in the current input line. */
228 /* Length of the current input line. */
231 /* Make this non-zero to return the current input_line. */
234 /* The last function executed by readline. */
235 Function *rl_last_func = (Function *)NULL;
237 /* Top level environment for readline_internal (). */
238 static jmp_buf readline_top_level;
240 /* The streams we interact with. */
241 static FILE *in_stream, *out_stream;
243 /* The names of the streams that we do input and output to. */
244 FILE *rl_instream, *rl_outstream;
246 /* Non-zero means echo characters as they are read. */
247 int readline_echoing_p = 1;
249 /* Current prompt. */
252 /* The number of characters read in order to type this complete command. */
253 int rl_key_sequence_length = 0;
255 /* If non-zero, then this is the address of a function to call just
256 before readline_internal () prints the first prompt. */
257 Function *rl_startup_hook = (Function *)NULL;
259 /* If non-zero, then this is the address of a function to call when
260 completing on a directory name. The function is called with
261 the address of a string (the current directory name) as an arg. */
262 Function *rl_symbolic_link_hook = (Function *)NULL;
264 /* What we use internally. You should always refer to RL_LINE_BUFFER. */
265 static char *the_line;
267 /* The character that can generate an EOF. Really read from
268 the terminal driver... just defaulted here. */
269 static int eof_char = CTRL ('D');
271 /* Non-zero makes this the next keystroke to read. */
272 int rl_pending_input = 0;
274 /* Pointer to a useful terminal name. */
275 char *rl_terminal_name = (char *)NULL;
277 /* Line buffer and maintenence. */
278 char *rl_line_buffer = (char *)NULL;
279 int rl_line_buffer_len = 0;
280 #define DEFAULT_BUFFER_SIZE 256
283 /* **************************************************************** */
285 /* `Forward' declarations */
287 /* **************************************************************** */
289 /* Non-zero means do not parse any lines other than comments and
290 parser directives. */
291 static unsigned char parsing_conditionalized_out = 0;
293 /* Caseless strcmp (). */
294 static int stricmp (), strnicmp ();
295 static char *strpbrk ();
297 /* Non-zero means to save keys that we dispatch on in a kbd macro. */
298 static int defining_kbd_macro = 0;
301 /* **************************************************************** */
303 /* Top Level Functions */
305 /* **************************************************************** */
307 static void rl_prep_terminal (), rl_deprep_terminal ();
309 /* Read a line of input. Prompt with PROMPT. A NULL PROMPT means
310 none. A return value of NULL means that EOF was encountered. */
315 char *readline_internal ();
320 /* If we are at EOF return a NULL string. */
321 if (rl_pending_input == EOF)
323 rl_pending_input = 0;
324 return ((char *)NULL);
330 #if defined (HANDLE_SIGNALS)
334 value = readline_internal ();
335 rl_deprep_terminal ();
337 #if defined (HANDLE_SIGNALS)
344 /* Read a line of input from the global rl_instream, doing output on
345 the global rl_outstream.
346 If rl_prompt is non-null, then that is our prompt. */
350 int lastc, c, eof_found;
352 in_stream = rl_instream;
353 out_stream = rl_outstream;
359 (*rl_startup_hook) ();
361 if (!readline_echoing_p)
365 fprintf (out_stream, "%s", rl_prompt);
373 #if defined (VI_MODE)
374 if (rl_editing_mode == vi_mode)
375 rl_vi_insertion_mode ();
381 int lk = last_command_was_kill;
382 int code = setjmp (readline_top_level);
387 if (!rl_pending_input)
389 /* Then initialize the argument and number of keys read. */
391 rl_key_sequence_length = 0;
396 /* EOF typed to a non-blank line is a <NL>. */
397 if (c == EOF && rl_end)
400 /* The character eof_char typed to blank line, and not as the
401 previous character is interpreted as EOF. */
402 if (((c == eof_char && lastc != c) || c == EOF) && !rl_end)
409 rl_dispatch (c, keymap);
411 /* If there was no change in last_command_was_kill, then no kill
412 has taken place. Note that if input is pending we are reading
413 a prefix command, so nothing has changed yet. */
414 if (!rl_pending_input)
416 if (lk == last_command_was_kill)
417 last_command_was_kill = 0;
420 #if defined (VI_MODE)
421 /* In vi mode, when you exit insert mode, the cursor moves back
422 over the previous character. We explicitly check for that here. */
423 if (rl_editing_mode == vi_mode && keymap == vi_movement_keymap)
431 /* Restore the original of this history line, iff the line that we
432 are editing was originally in the history, AND the line has changed. */
434 HIST_ENTRY *entry = current_history ();
436 if (entry && rl_undo_list)
438 char *temp = savestring (the_line);
440 entry = replace_history_entry (where_history (), the_line,
442 free_history_entry (entry);
444 strcpy (the_line, temp);
449 /* At any rate, it is highly likely that this line has an undo list. Get
457 return (savestring (the_line));
461 /* **************************************************************** */
463 /* Signal Handling */
465 /* **************************************************************** */
467 #if defined (SIGWINCH)
468 static SigHandler *old_sigwinch = (SigHandler *)NULL;
471 rl_handle_sigwinch (sig)
476 term = rl_terminal_name;
478 if (readline_echoing_p)
481 term = getenv ("TERM");
484 rl_reset_terminal (term);
487 rl_forced_update_display ();
492 old_sigwinch != (SigHandler *)SIG_IGN &&
493 old_sigwinch != (SigHandler *)SIG_DFL)
494 (*old_sigwinch) (sig);
495 #if !defined (VOID_SIGHANDLER)
497 #endif /* VOID_SIGHANDLER */
499 #endif /* SIGWINCH */
501 #if defined (HANDLE_SIGNALS)
502 /* Interrupt handling. */
504 *old_int = (SigHandler *)NULL,
505 *old_tstp = (SigHandler *)NULL,
506 *old_ttou = (SigHandler *)NULL,
507 *old_ttin = (SigHandler *)NULL,
508 *old_cont = (SigHandler *)NULL,
509 *old_alrm = (SigHandler *)NULL;
511 /* Handle an interrupt character. */
513 rl_signal_handler (sig)
516 #if !defined (HAVE_BSD_SIGNALS)
517 /* Since the signal will not be blocked while we are in the signal
518 handler, ignore it until rl_clear_signals resets the catcher. */
520 signal (sig, SIG_IGN);
521 #endif /* !HAVE_BSD_SIGNALS */
530 #if defined (SIGTSTP)
536 rl_clean_up_for_exit ();
537 rl_deprep_terminal ();
539 rl_pending_input = 0;
541 kill (getpid (), sig);
543 #if defined (HAVE_POSIX_SIGNALS)
548 sigprocmask (SIG_SETMASK, &set, (sigset_t *)NULL);
551 #if defined (HAVE_BSD_SIGNALS)
553 #endif /* HAVE_BSD_SIGNALS */
554 #endif /* HAVE_POSIX_SIGNALS */
560 #if !defined (VOID_SIGHANDLER)
562 #endif /* !VOID_SIGHANDLER */
567 old_int = (SigHandler *)signal (SIGINT, rl_signal_handler);
568 if (old_int == (SigHandler *)SIG_IGN)
569 signal (SIGINT, SIG_IGN);
571 old_alrm = (SigHandler *)signal (SIGALRM, rl_signal_handler);
572 if (old_alrm == (SigHandler *)SIG_IGN)
573 signal (SIGALRM, SIG_IGN);
575 #if defined (SIGTSTP)
576 old_tstp = (SigHandler *)signal (SIGTSTP, rl_signal_handler);
577 if (old_tstp == (SigHandler *)SIG_IGN)
578 signal (SIGTSTP, SIG_IGN);
580 #if defined (SIGTTOU)
581 old_ttou = (SigHandler *)signal (SIGTTOU, rl_signal_handler);
582 old_ttin = (SigHandler *)signal (SIGTTIN, rl_signal_handler);
584 if (old_tstp == (SigHandler *)SIG_IGN)
586 signal (SIGTTOU, SIG_IGN);
587 signal (SIGTTIN, SIG_IGN);
591 #if defined (SIGWINCH)
592 old_sigwinch = (SigHandler *)signal (SIGWINCH, rl_handle_sigwinch);
598 signal (SIGINT, old_int);
599 signal (SIGALRM, old_alrm);
601 #if defined (SIGTSTP)
602 signal (SIGTSTP, old_tstp);
605 #if defined (SIGTTOU)
606 signal (SIGTTOU, old_ttou);
607 signal (SIGTTIN, old_ttin);
610 #if defined (SIGWINCH)
611 signal (SIGWINCH, old_sigwinch);
614 #endif /* HANDLE_SIGNALS */
617 /* **************************************************************** */
619 /* Character Input Buffering */
621 /* **************************************************************** */
623 #if defined (USE_XON_XOFF)
624 /* If the terminal was in xoff state when we got to it, then xon_char
625 contains the character that is supposed to start it again. */
626 static int xon_char, xoff_state;
627 #endif /* USE_XON_XOFF */
629 static int pop_index = 0, push_index = 0, ibuffer_len = 511;
630 static unsigned char ibuffer[512];
632 /* Non-null means it is a pointer to a function to run while waiting for
634 Function *rl_event_hook = (Function *)NULL;
636 #define any_typein (push_index != pop_index)
638 /* Add KEY to the buffer of characters to be read. */
645 rl_pending_input = EOF;
647 ibuffer[push_index++] = key;
648 if (push_index >= ibuffer_len)
652 /* Return the amount of space available in the
653 buffer for stuffing characters. */
657 if (pop_index > push_index)
658 return (pop_index - push_index);
660 return (ibuffer_len - (push_index - pop_index));
663 /* Get a key from the buffer of characters to be read.
664 Return the key in KEY.
665 Result is KEY if there was a key, or 0 if there wasn't. */
670 if (push_index == pop_index)
673 *key = ibuffer[pop_index++];
675 if (pop_index >= ibuffer_len)
681 /* Stuff KEY into the *front* of the input buffer.
682 Returns non-zero if successful, zero if there is
683 no space left in the buffer. */
688 if (ibuffer_space ())
692 pop_index = ibuffer_len - 1;
693 ibuffer[pop_index] = key;
699 /* If a character is available to be read, then read it
700 and stuff it into IBUFFER. Otherwise, just return. */
712 if (kbhit() && ibuffer_space())
713 rl_stuff_char(getkey());
715 int tty = fileno (in_stream);
716 register int tem, result = -1;
720 #if defined (FIONREAD)
721 result = ioctl (tty, FIONREAD, &chars_avail);
728 flags = fcntl (tty, F_GETFL, 0);
730 fcntl (tty, F_SETFL, (flags | O_NDELAY));
731 chars_avail = read (tty, &input, 1);
733 fcntl (tty, F_SETFL, flags);
734 if (chars_avail == -1 && errno == EAGAIN)
738 /* If there's nothing available, don't waste time trying to read
740 if (chars_avail == 0)
743 tem = ibuffer_space ();
745 if (chars_avail > tem)
748 /* One cannot read all of the available input. I can only read a single
749 character at a time, or else programs which require input can be
750 thwarted. If the buffer is larger than one character, I lose.
752 if (tem < ibuffer_len)
757 while (chars_avail--)
758 rl_stuff_char (rl_getc (in_stream));
763 rl_stuff_char (input);
765 #endif /* def __GO32__/else */
768 static int next_macro_key ();
769 /* Read a key, including pending input. */
775 rl_key_sequence_length++;
777 if (rl_pending_input)
779 c = rl_pending_input;
780 rl_pending_input = 0;
784 /* If input is coming from a macro, then use that. */
785 if (c = next_macro_key ())
788 /* If the user has an event function, then call it periodically. */
791 while (rl_event_hook && !rl_get_char (&c))
799 if (!rl_get_char (&c))
800 c = rl_getc (in_stream);
807 /* I'm beginning to hate the declaration rules for various compilers. */
808 static void add_macro_char (), with_macro_input ();
810 /* Do the command associated with KEY in MAP.
811 If the associated command is really a keymap, then read
812 another key, and dispatch into that map. */
813 rl_dispatch (key, map)
818 if (defining_kbd_macro)
819 add_macro_char (key);
821 if (key > 127 && key < 256)
823 if (map[ESC].type == ISKMAP)
825 map = (Keymap)map[ESC].function;
827 rl_dispatch (key, map);
834 switch (map[key].type)
838 Function *func = map[key].function;
840 if (func != (Function *)NULL)
842 /* Special case rl_do_lowercase_version (). */
843 if (func == rl_do_lowercase_version)
845 rl_dispatch (to_lower (key), map);
849 (*map[key].function)(rl_numeric_arg * rl_arg_sign, key);
851 /* If we have input pending, then the last command was a prefix
852 command. Don't change the state of rl_last_func. Otherwise,
853 remember the last command executed in this variable. */
854 if (!rl_pending_input)
855 rl_last_func = map[key].function;
866 if (map[key].function != (Function *)NULL)
870 rl_key_sequence_length++;
871 newkey = rl_read_key ();
872 rl_dispatch (newkey, (Keymap)map[key].function);
882 if (map[key].function != (Function *)NULL)
886 macro = savestring ((char *)map[key].function);
887 with_macro_input (macro);
895 /* **************************************************************** */
897 /* Hacking Keyboard Macros */
899 /* **************************************************************** */
901 /* The currently executing macro string. If this is non-zero,
902 then it is a malloc ()'ed string where input is coming from. */
903 static char *executing_macro = (char *)NULL;
905 /* The offset in the above string to the next character to be read. */
906 static int executing_macro_index = 0;
908 /* The current macro string being built. Characters get stuffed
909 in here by add_macro_char (). */
910 static char *current_macro = (char *)NULL;
912 /* The size of the buffer allocated to current_macro. */
913 static int current_macro_size = 0;
915 /* The index at which characters are being added to current_macro. */
916 static int current_macro_index = 0;
918 /* A structure used to save nested macro strings.
919 It is a linked list of string/index for each saved macro. */
921 struct saved_macro *next;
926 /* The list of saved macros. */
927 struct saved_macro *macro_list = (struct saved_macro *)NULL;
929 /* Forward declarations of static functions. Thank you C. */
930 static void push_executing_macro (), pop_executing_macro ();
932 /* This one has to be declared earlier in the file. */
933 /* static void add_macro_char (); */
935 /* Set up to read subsequent input from STRING.
936 STRING is free ()'ed when we are done with it. */
938 with_macro_input (string)
941 push_executing_macro ();
942 executing_macro = string;
943 executing_macro_index = 0;
946 /* Return the next character available from a macro, or 0 if
947 there are no macro characters. */
951 if (!executing_macro)
954 if (!executing_macro[executing_macro_index])
956 pop_executing_macro ();
957 return (next_macro_key ());
960 return (executing_macro[executing_macro_index++]);
963 /* Save the currently executing macro on a stack of saved macros. */
965 push_executing_macro ()
967 struct saved_macro *saver;
969 saver = (struct saved_macro *)xmalloc (sizeof (struct saved_macro));
970 saver->next = macro_list;
971 saver->index = executing_macro_index;
972 saver->string = executing_macro;
977 /* Discard the current macro, replacing it with the one
978 on the top of the stack of saved macros. */
980 pop_executing_macro ()
983 free (executing_macro);
985 executing_macro = (char *)NULL;
986 executing_macro_index = 0;
990 struct saved_macro *disposer = macro_list;
991 executing_macro = macro_list->string;
992 executing_macro_index = macro_list->index;
993 macro_list = macro_list->next;
998 /* Add a character to the macro being built. */
1003 if (current_macro_index + 1 >= current_macro_size)
1006 current_macro = (char *)xmalloc (current_macro_size = 25);
1009 (char *)xrealloc (current_macro, current_macro_size += 25);
1012 current_macro[current_macro_index++] = c;
1013 current_macro[current_macro_index] = '\0';
1016 /* Begin defining a keyboard macro.
1017 Keystrokes are recorded as they are executed.
1018 End the definition with rl_end_kbd_macro ().
1019 If a numeric argument was explicitly typed, then append this
1020 definition to the end of the existing macro, and start by
1021 re-executing the existing macro. */
1022 rl_start_kbd_macro (ignore1, ignore2)
1023 int ignore1, ignore2;
1025 if (defining_kbd_macro)
1028 if (rl_explicit_arg)
1031 with_macro_input (savestring (current_macro));
1034 current_macro_index = 0;
1036 defining_kbd_macro = 1;
1039 /* Stop defining a keyboard macro.
1040 A numeric argument says to execute the macro right now,
1041 that many times, counting the definition as the first time. */
1042 rl_end_kbd_macro (count, ignore)
1045 if (!defining_kbd_macro)
1048 current_macro_index -= (rl_key_sequence_length - 1);
1049 current_macro[current_macro_index] = '\0';
1051 defining_kbd_macro = 0;
1053 rl_call_last_kbd_macro (--count, 0);
1056 /* Execute the most recently defined keyboard macro.
1057 COUNT says how many times to execute it. */
1058 rl_call_last_kbd_macro (count, ignore)
1065 with_macro_input (savestring (current_macro));
1069 /* **************************************************************** */
1071 /* Initializations */
1073 /* **************************************************************** */
1075 /* Initliaze readline (and terminal if not already). */
1078 extern char *rl_display_prompt;
1080 /* If we have never been called before, initialize the
1081 terminal and data structures. */
1082 if (!rl_initialized)
1084 readline_initialize_everything ();
1088 /* Initalize the current line information. */
1089 rl_point = rl_end = 0;
1090 the_line = rl_line_buffer;
1093 /* We aren't done yet. We haven't even gotten started yet! */
1096 /* Tell the history routines what is going on. */
1097 start_using_history ();
1099 /* Make the display buffer match the state of the line. */
1101 extern char *rl_display_prompt;
1102 extern int forced_display;
1106 rl_display_prompt = rl_prompt ? rl_prompt : "";
1110 /* No such function typed yet. */
1111 rl_last_func = (Function *)NULL;
1113 /* Parsing of key-bindings begins in an enabled state. */
1114 parsing_conditionalized_out = 0;
1117 /* Initialize the entire state of the world. */
1118 readline_initialize_everything ()
1120 /* Find out if we are running in Emacs. */
1121 running_in_emacs = getenv ("EMACS");
1123 /* Set up input and output if they aren't already. */
1125 rl_instream = stdin;
1127 rl_outstream = stdout;
1129 /* Allocate data structures. */
1130 if (!rl_line_buffer)
1132 (char *)xmalloc (rl_line_buffer_len = DEFAULT_BUFFER_SIZE);
1134 /* Initialize the terminal interface. */
1135 init_terminal_io ((char *)NULL);
1137 /* Bind tty characters to readline functions. */
1138 readline_default_bindings ();
1140 /* Initialize the function names. */
1141 rl_initialize_funmap ();
1143 /* Read in the init file. */
1144 rl_read_init_file ((char *)NULL);
1146 /* If the completion parser's default word break characters haven't
1147 been set yet, then do so now. */
1149 extern char *rl_completer_word_break_characters;
1150 extern char *rl_basic_word_break_characters;
1152 if (rl_completer_word_break_characters == (char *)NULL)
1153 rl_completer_word_break_characters = rl_basic_word_break_characters;
1157 /* If this system allows us to look at the values of the regular
1158 input editing characters, then bind them to their readline
1159 equivalents, iff the characters are not bound to keymaps. */
1160 readline_default_bindings ()
1164 #if defined (NEW_TTY_DRIVER)
1165 struct sgttyb ttybuff;
1166 int tty = fileno (rl_instream);
1168 if (ioctl (tty, TIOCGETP, &ttybuff) != -1)
1172 erase = ttybuff.sg_erase;
1173 kill = ttybuff.sg_kill;
1175 if (erase != -1 && keymap[erase].type == ISFUNC)
1176 keymap[erase].function = rl_rubout;
1178 if (kill != -1 && keymap[kill].type == ISFUNC)
1179 keymap[kill].function = rl_unix_line_discard;
1182 #if defined (TIOCGLTC)
1186 if (ioctl (tty, TIOCGLTC, <) != -1)
1190 erase = lt.t_werasc;
1191 nextc = lt.t_lnextc;
1193 if (erase != -1 && keymap[erase].type == ISFUNC)
1194 keymap[erase].function = rl_unix_word_rubout;
1196 if (nextc != -1 && keymap[nextc].type == ISFUNC)
1197 keymap[nextc].function = rl_quoted_insert;
1200 #endif /* TIOCGLTC */
1201 #else /* not NEW_TTY_DRIVER */
1203 #if defined (TERMIOS_TTY_DRIVER)
1204 struct termios ttybuff;
1206 struct termio ttybuff;
1207 #endif /* TERMIOS_TTY_DRIVER */
1208 int tty = fileno (rl_instream);
1210 #if defined (TERMIOS_TTY_DRIVER)
1211 if (tcgetattr (tty, &ttybuff) != -1)
1213 if (ioctl (tty, TCGETA, &ttybuff) != -1)
1214 #endif /* !TERMIOS_TTY_DRIVER */
1218 erase = ttybuff.c_cc[VERASE];
1219 kill = ttybuff.c_cc[VKILL];
1221 if (erase != _POSIX_VDISABLE &&
1222 keymap[(unsigned char)erase].type == ISFUNC)
1223 keymap[(unsigned char)erase].function = rl_rubout;
1225 if (kill != _POSIX_VDISABLE &&
1226 keymap[(unsigned char)kill].type == ISFUNC)
1227 keymap[(unsigned char)kill].function = rl_unix_line_discard;
1229 #if defined (VLNEXT) && defined (TERMIOS_TTY_DRIVER)
1233 nextc = ttybuff.c_cc[VLNEXT];
1235 if (nextc != _POSIX_VDISABLE &&
1236 keymap[(unsigned char)nextc].type == ISFUNC)
1237 keymap[(unsigned char)nextc].function = rl_quoted_insert;
1239 #endif /* VLNEXT && TERMIOS_TTY_DRIVER */
1241 #if defined (VWERASE)
1245 werase = ttybuff.c_cc[VWERASE];
1247 if (werase != _POSIX_VDISABLE &&
1248 keymap[(unsigned char)werase].type == ISFUNC)
1249 keymap[(unsigned char)werase].function = rl_unix_word_rubout;
1251 #endif /* VWERASE */
1253 #endif /* !NEW_TTY_DRIVER */
1254 #endif /* def __GO32__ */
1258 /* **************************************************************** */
1260 /* Numeric Arguments */
1262 /* **************************************************************** */
1264 /* Handle C-u style numeric args, as well as M--, and M-digits. */
1266 /* Add the current digit to the argument in progress. */
1267 rl_digit_argument (ignore, key)
1270 rl_pending_input = key;
1274 /* What to do when you abort reading an argument. */
1275 rl_discard_argument ()
1278 rl_clear_message ();
1279 rl_init_argument ();
1282 /* Create a default argument. */
1285 rl_numeric_arg = rl_arg_sign = 1;
1286 rl_explicit_arg = 0;
1289 /* C-u, universal argument. Multiply the current argument by 4.
1290 Read a key. If the key has nothing to do with arguments, then
1291 dispatch on it. If the key is the abort character then abort. */
1292 rl_universal_argument ()
1294 rl_numeric_arg *= 4;
1303 rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg);
1304 key = c = rl_read_key ();
1306 if (keymap[c].type == ISFUNC &&
1307 keymap[c].function == rl_universal_argument)
1309 rl_numeric_arg *= 4;
1315 if (rl_explicit_arg)
1316 rl_numeric_arg = (rl_numeric_arg * 10) + (c - '0');
1318 rl_numeric_arg = (c - '0');
1319 rl_explicit_arg = 1;
1323 if (c == '-' && !rl_explicit_arg)
1330 rl_clear_message ();
1331 rl_dispatch (key, keymap);
1339 /* **************************************************************** */
1343 /* **************************************************************** */
1345 /* This is the stuff that is hard for me. I never seem to write good
1346 display routines in C. Let's see how I do this time. */
1348 /* (PWP) Well... Good for a simple line updater, but totally ignores
1349 the problems of input lines longer than the screen width.
1351 update_line and the code that calls it makes a multiple line,
1352 automatically wrapping line update. Carefull attention needs
1353 to be paid to the vertical position variables.
1355 handling of terminals with autowrap on (incl. DEC braindamage)
1356 could be improved a bit. Right now I just cheat and decrement
1357 screenwidth by one. */
1359 /* Keep two buffers; one which reflects the current contents of the
1360 screen, and the other to draw what we think the new contents should
1361 be. Then compare the buffers, and make whatever changes to the
1362 screen itself that we should. Finally, make the buffer that we
1363 just drew into be the one which reflects the current contents of the
1364 screen, and place the cursor where it belongs.
1366 Commands that want to can fix the display themselves, and then let
1367 this function know that the display has been fixed by setting the
1368 RL_DISPLAY_FIXED variable. This is good for efficiency. */
1370 /* Termcap variables: */
1371 extern char *term_up, *term_dc, *term_cr;
1372 extern int screenheight, screenwidth, terminal_can_insert;
1374 /* What YOU turn on when you have handled all redisplay yourself. */
1375 int rl_display_fixed = 0;
1377 /* The visible cursor position. If you print some text, adjust this. */
1381 /* The last left edge of text that was displayed. This is used when
1382 doing horizontal scrolling. It shifts in thirds of a screenwidth. */
1383 static int last_lmargin = 0;
1385 /* The line display buffers. One is the line currently displayed on
1386 the screen. The other is the line about to be displayed. */
1387 static char *visible_line = (char *)NULL;
1388 static char *invisible_line = (char *)NULL;
1390 /* Number of lines currently on screen minus 1. */
1393 /* A buffer for `modeline' messages. */
1396 /* Non-zero forces the redisplay even if we thought it was unnecessary. */
1397 int forced_display = 0;
1399 /* The stuff that gets printed out before the actual text of the line.
1400 This is usually pointing to rl_prompt. */
1401 char *rl_display_prompt = (char *)NULL;
1403 /* Default and initial buffer size. Can grow. */
1404 static int line_size = 1024;
1406 /* Non-zero means to always use horizontal scrolling in line display. */
1407 static int horizontal_scroll_mode = 0;
1409 /* Non-zero means to display an asterisk at the starts of history lines
1410 which have been modified. */
1411 static int mark_modified_lines = 0;
1413 /* Non-zero means to use a visible bell if one is available rather than
1414 simply ringing the terminal bell. */
1415 static int prefer_visible_bell = 0;
1417 /* I really disagree with this, but my boss (among others) insists that we
1418 support compilers that don't work. I don't think we are gaining by doing
1419 so; what is the advantage in producing better code if we can't use it? */
1420 /* The following two declarations belong inside the
1421 function block, not here. */
1422 static void move_cursor_relative ();
1423 static void output_some_chars ();
1424 static void output_character_function ();
1425 static int compare_strings ();
1427 /* Basic redisplay algorithm. */
1430 register int in, out, c, linenum;
1431 register char *line = invisible_line;
1432 char *prompt_this_line;
1434 int inv_botlin = 0; /* Number of lines in newly drawn buffer. */
1436 extern int readline_echoing_p;
1438 if (!readline_echoing_p)
1441 if (!rl_display_prompt)
1442 rl_display_prompt = "";
1444 if (!invisible_line)
1446 visible_line = (char *)xmalloc (line_size);
1447 invisible_line = (char *)xmalloc (line_size);
1448 line = invisible_line;
1449 for (in = 0; in < line_size; in++)
1451 visible_line[in] = 0;
1452 invisible_line[in] = 1;
1457 /* Draw the line into the buffer. */
1460 /* Mark the line as modified or not. We only do this for history
1463 if (mark_modified_lines && current_history () && rl_undo_list)
1469 /* If someone thought that the redisplay was handled, but the currently
1470 visible line has a different modification state than the one about
1471 to become visible, then correct the callers misconception. */
1472 if (visible_line[0] != invisible_line[0])
1473 rl_display_fixed = 0;
1475 prompt_this_line = rindex (rl_display_prompt, '\n');
1476 if (!prompt_this_line)
1477 prompt_this_line = rl_display_prompt;
1482 output_some_chars (rl_display_prompt,
1483 prompt_this_line - rl_display_prompt);
1486 strncpy (line + out, prompt_this_line, strlen (prompt_this_line));
1487 out += strlen (prompt_this_line);
1490 for (in = 0; in < rl_end; in++)
1492 c = (unsigned char)the_line[in];
1494 if (out + 1 >= line_size)
1497 visible_line = (char *)xrealloc (visible_line, line_size);
1498 invisible_line = (char *)xrealloc (invisible_line, line_size);
1499 line = invisible_line;
1509 line[out++] = c - 128;
1511 #define DISPLAY_TABS
1512 #if defined (DISPLAY_TABS)
1515 register int newout = (out | (int)7) + 1;
1516 while (out < newout)
1524 line[out++] = c + 64;
1539 /* PWP: now is when things get a bit hairy. The visible and invisible
1540 line buffers are really multiple lines, which would wrap every
1541 (screenwidth - 1) characters. Go through each in turn, finding
1542 the changed region and updating it. The line order is top to bottom. */
1544 /* If we can move the cursor up and down, then use multiple lines,
1545 otherwise, let long lines display in a single terminal line, and
1546 horizontally scroll it. */
1548 if (!horizontal_scroll_mode && term_up && *term_up)
1550 int total_screen_chars = (screenwidth * screenheight);
1552 if (!rl_display_fixed || forced_display)
1556 /* If we have more than a screenful of material to display, then
1557 only display a screenful. We should display the last screen,
1558 not the first. I'll fix this in a minute. */
1559 if (out >= total_screen_chars)
1560 out = total_screen_chars - 1;
1562 /* Number of screen lines to display. */
1563 inv_botlin = out / screenwidth;
1565 /* For each line in the buffer, do the updating display. */
1566 for (linenum = 0; linenum <= inv_botlin; linenum++)
1567 update_line (linenum > vis_botlin ? ""
1568 : &visible_line[linenum * screenwidth],
1569 &invisible_line[linenum * screenwidth],
1572 /* We may have deleted some lines. If so, clear the left over
1573 blank ones at the bottom out. */
1574 if (vis_botlin > inv_botlin)
1577 for (; linenum <= vis_botlin; linenum++)
1579 tt = &visible_line[linenum * screenwidth];
1580 move_vert (linenum);
1581 move_cursor_relative (0, tt);
1582 clear_to_eol ((linenum == vis_botlin)?
1583 strlen (tt) : screenwidth);
1586 vis_botlin = inv_botlin;
1588 /* Move the cursor where it should be. */
1589 move_vert (c_pos / screenwidth);
1590 move_cursor_relative (c_pos % screenwidth,
1591 &invisible_line[(c_pos / screenwidth) * screenwidth]);
1594 else /* Do horizontal scrolling. */
1598 /* Always at top line. */
1601 /* If the display position of the cursor would be off the edge
1602 of the screen, start the display of this line at an offset that
1603 leaves the cursor on the screen. */
1604 if (c_pos - last_lmargin > screenwidth - 2)
1605 lmargin = (c_pos / (screenwidth / 3) - 2) * (screenwidth / 3);
1606 else if (c_pos - last_lmargin < 1)
1607 lmargin = ((c_pos - 1) / (screenwidth / 3)) * (screenwidth / 3);
1609 lmargin = last_lmargin;
1611 /* If the first character on the screen isn't the first character
1612 in the display line, indicate this with a special character. */
1614 line[lmargin] = '<';
1616 if (lmargin + screenwidth < out)
1617 line[lmargin + screenwidth - 1] = '>';
1619 if (!rl_display_fixed || forced_display || lmargin != last_lmargin)
1622 update_line (&visible_line[last_lmargin],
1623 &invisible_line[lmargin], 0);
1625 move_cursor_relative (c_pos - lmargin, &invisible_line[lmargin]);
1626 last_lmargin = lmargin;
1629 fflush (out_stream);
1631 /* Swap visible and non-visible lines. */
1633 char *temp = visible_line;
1634 visible_line = invisible_line;
1635 invisible_line = temp;
1636 rl_display_fixed = 0;
1640 /* PWP: update_line() is based on finding the middle difference of each
1641 line on the screen; vis:
1643 /old first difference
1644 /beginning of line | /old last same /old EOL
1646 old: eddie> Oh, my little gruntle-buggy is to me, as lurgid as
1647 new: eddie> Oh, my little buggy says to me, as lurgid as
1649 \beginning of line | \new last same \new end of line
1650 \new first difference
1652 All are character pointers for the sake of speed. Special cases for
1653 no differences, as well as for end of line additions must be handeled.
1655 Could be made even smarter, but this works well enough */
1657 update_line (old, new, current_line)
1658 register char *old, *new;
1661 register char *ofd, *ols, *oe, *nfd, *nls, *ne;
1662 int lendiff, wsatend;
1664 /* Find first difference. */
1665 for (ofd = old, nfd = new;
1666 (ofd - old < screenwidth) && *ofd && (*ofd == *nfd);
1670 /* Move to the end of the screen line. */
1671 for (oe = ofd; ((oe - old) < screenwidth) && *oe; oe++);
1672 for (ne = nfd; ((ne - new) < screenwidth) && *ne; ne++);
1674 /* If no difference, continue to next line. */
1675 if (ofd == oe && nfd == ne)
1678 wsatend = 1; /* flag for trailing whitespace */
1679 ols = oe - 1; /* find last same */
1681 while ((*ols == *nls) && (ols > ofd) && (nls > nfd))
1694 else if (*ols != *nls)
1696 if (*ols) /* don't step past the NUL */
1702 move_vert (current_line);
1703 move_cursor_relative (ofd - old, old);
1705 /* if (len (new) > len (old)) */
1706 lendiff = (nls - nfd) - (ols - ofd);
1708 /* Insert (diff(len(old),len(new)) ch */
1711 if (terminal_can_insert)
1713 extern char *term_IC;
1715 /* Sometimes it is cheaper to print the characters rather than
1716 use the terminal's capabilities. */
1717 if ((2 * (ne - nfd)) < lendiff && !term_IC)
1719 output_some_chars (nfd, (ne - nfd));
1720 last_c_pos += (ne - nfd);
1726 insert_some_chars (nfd, lendiff);
1727 last_c_pos += lendiff;
1731 /* At the end of a line the characters do not have to
1732 be "inserted". They can just be placed on the screen. */
1733 output_some_chars (nfd, lendiff);
1734 last_c_pos += lendiff;
1736 /* Copy (new) chars to screen from first diff to last match. */
1737 if (((nls - nfd) - lendiff) > 0)
1739 output_some_chars (&nfd[lendiff], ((nls - nfd) - lendiff));
1740 last_c_pos += ((nls - nfd) - lendiff);
1745 { /* cannot insert chars, write to EOL */
1746 output_some_chars (nfd, (ne - nfd));
1747 last_c_pos += (ne - nfd);
1750 else /* Delete characters from line. */
1752 /* If possible and inexpensive to use terminal deletion, then do so. */
1753 if (term_dc && (2 * (ne - nfd)) >= (-lendiff))
1756 delete_chars (-lendiff); /* delete (diff) characters */
1758 /* Copy (new) chars to screen from first diff to last match */
1759 if ((nls - nfd) > 0)
1761 output_some_chars (nfd, (nls - nfd));
1762 last_c_pos += (nls - nfd);
1765 /* Otherwise, print over the existing material. */
1768 output_some_chars (nfd, (ne - nfd));
1769 last_c_pos += (ne - nfd);
1770 clear_to_eol ((oe - old) - (ne - new));
1775 /* (PWP) tell the update routines that we have moved onto a
1776 new (empty) line. */
1780 visible_line[0] = '\0';
1782 last_c_pos = last_v_pos = 0;
1783 vis_botlin = last_lmargin = 0;
1786 /* Actually update the display, period. */
1787 rl_forced_update_display ()
1791 register char *temp = visible_line;
1793 while (*temp) *temp++ = '\0';
1800 /* Move the cursor from last_c_pos to NEW, which are buffer indices.
1801 DATA is the contents of the screen line of interest; i.e., where
1802 the movement is being done. */
1804 move_cursor_relative (new, data)
1810 /* It may be faster to output a CR, and then move forwards instead
1811 of moving backwards. */
1812 if (new + 1 < last_c_pos - new)
1815 putc('\r', out_stream);
1817 tputs (term_cr, 1, output_character_function);
1822 if (last_c_pos == new) return;
1824 if (last_c_pos < new)
1826 /* Move the cursor forward. We do it by printing the command
1827 to move the cursor forward if there is one, else print that
1828 portion of the output buffer again. Which is cheaper? */
1830 /* The above comment is left here for posterity. It is faster
1831 to print one character (non-control) than to print a control
1832 sequence telling the terminal to move forward one character.
1833 That kind of control is for people who don't know what the
1834 data is underneath the cursor. */
1835 #if defined (HACK_TERMCAP_MOTION)
1836 extern char *term_forward_char;
1838 if (term_forward_char)
1839 for (i = last_c_pos; i < new; i++)
1840 tputs (term_forward_char, 1, output_character_function);
1842 for (i = last_c_pos; i < new; i++)
1843 putc (data[i], out_stream);
1845 for (i = last_c_pos; i < new; i++)
1846 putc (data[i], out_stream);
1847 #endif /* HACK_TERMCAP_MOTION */
1850 backspace (last_c_pos - new);
1854 /* PWP: move the cursor up or down. */
1858 void output_character_function ();
1859 register int delta, i;
1861 if (last_v_pos == to) return;
1863 if (to > screenheight)
1869 ScreenGetCursor(&cur_r, &cur_c);
1870 ScreenSetCursor(cur_r+to-last_v_pos, cur_c);
1872 #else /* __GO32__ */
1873 if ((delta = to - last_v_pos) > 0)
1875 for (i = 0; i < delta; i++)
1876 putc ('\n', out_stream);
1877 tputs (term_cr, 1, output_character_function);
1882 if (term_up && *term_up)
1883 for (i = 0; i < -delta; i++)
1884 tputs (term_up, 1, output_character_function);
1886 #endif /* __GO32__ */
1887 last_v_pos = to; /* now to is here */
1890 /* Physically print C on out_stream. This is for functions which know
1891 how to optimize the display. */
1897 fprintf (out_stream, "M-");
1901 #if defined (DISPLAY_TABS)
1902 if (c < 32 && c != '\t')
1911 putc (c, out_stream);
1912 fflush (out_stream);
1915 #if defined (DISPLAY_TABS)
1917 rl_character_len (c, pos)
1918 register int c, pos;
1920 if (c < ' ' || c > 126)
1923 return (((pos | (int)7) + 1) - pos);
1932 rl_character_len (c)
1935 if (c < ' ' || c > 126)
1940 #endif /* DISPLAY_TAB */
1942 /* How to print things in the "echo-area". The prompt is treated as a
1944 rl_message (string, arg1, arg2)
1947 sprintf (msg_buf, string, arg1, arg2);
1948 rl_display_prompt = msg_buf;
1952 /* How to clear things from the "echo-area". */
1955 rl_display_prompt = rl_prompt;
1959 /* **************************************************************** */
1961 /* Terminal and Termcap */
1963 /* **************************************************************** */
1965 static char *term_buffer = (char *)NULL;
1966 static char *term_string_buffer = (char *)NULL;
1968 /* Non-zero means this terminal can't really do anything. */
1974 /* Some strings to control terminal actions. These are output by tputs (). */
1975 char *term_goto, *term_clreol, *term_cr, *term_clrpag, *term_backspace;
1977 int screenwidth, screenheight;
1979 /* Non-zero if we determine that the terminal can do character insertion. */
1980 int terminal_can_insert = 0;
1982 /* How to insert characters. */
1983 char *term_im, *term_ei, *term_ic, *term_ip, *term_IC;
1985 /* How to delete characters. */
1986 char *term_dc, *term_DC;
1988 #if defined (HACK_TERMCAP_MOTION)
1989 char *term_forward_char;
1990 #endif /* HACK_TERMCAP_MOTION */
1992 /* How to go up a line. */
1995 /* A visible bell, if the terminal can be made to flash the screen. */
1998 /* Re-initialize the terminal considering that the TERM/TERMCAP variable
2000 rl_reset_terminal (terminal_name)
2001 char *terminal_name;
2003 init_terminal_io (terminal_name);
2006 init_terminal_io (terminal_name)
2007 char *terminal_name;
2010 screenwidth = ScreenCols();
2011 screenheight = ScreenRows();
2013 term_im = term_ei = term_ic = term_IC = (char *)NULL;
2014 term_up = term_dc = term_DC = visible_bell = (char *)NULL;
2015 #if defined (HACK_TERMCAP_MOTION)
2016 term_forward_char = (char *)NULL;
2018 terminal_can_insert = 0;
2021 extern char *tgetstr ();
2022 char *term, *buffer;
2023 #if defined (TIOCGWINSZ)
2024 struct winsize window_size;
2028 term = terminal_name ? terminal_name : getenv ("TERM");
2030 if (!term_string_buffer)
2031 term_string_buffer = (char *)xmalloc (2048);
2034 term_buffer = (char *)xmalloc (2048);
2036 buffer = term_string_buffer;
2038 term_clrpag = term_cr = term_clreol = (char *)NULL;
2043 if (tgetent (term_buffer, term) < 0)
2049 term_im = term_ei = term_ic = term_IC = (char *)NULL;
2050 term_up = term_dc = term_DC = visible_bell = (char *)NULL;
2051 #if defined (HACK_TERMCAP_MOTION)
2052 term_forward_char = (char *)NULL;
2054 terminal_can_insert = 0;
2058 BC = tgetstr ("pc", &buffer);
2059 PC = buffer ? *buffer : 0;
2061 term_backspace = tgetstr ("le", &buffer);
2063 term_cr = tgetstr ("cr", &buffer);
2064 term_clreol = tgetstr ("ce", &buffer);
2065 term_clrpag = tgetstr ("cl", &buffer);
2070 #if defined (HACK_TERMCAP_MOTION)
2071 term_forward_char = tgetstr ("nd", &buffer);
2072 #endif /* HACK_TERMCAP_MOTION */
2075 tty = fileno (rl_instream);
2079 screenwidth = screenheight = 0;
2080 #if defined (TIOCGWINSZ)
2081 if (ioctl (tty, TIOCGWINSZ, &window_size) == 0)
2083 screenwidth = (int) window_size.ws_col;
2084 screenheight = (int) window_size.ws_row;
2088 if (screenwidth <= 0 || screenheight <= 0)
2090 screenwidth = tgetnum ("co");
2091 screenheight = tgetnum ("li");
2096 if (screenwidth <= 0)
2099 if (screenheight <= 0)
2102 term_im = tgetstr ("im", &buffer);
2103 term_ei = tgetstr ("ei", &buffer);
2104 term_IC = tgetstr ("IC", &buffer);
2105 term_ic = tgetstr ("ic", &buffer);
2107 /* "An application program can assume that the terminal can do
2108 character insertion if *any one of* the capabilities `IC',
2109 `im', `ic' or `ip' is provided." But we can't do anything if
2110 only `ip' is provided, so... */
2111 terminal_can_insert = (term_IC || term_im || term_ic);
2113 term_up = tgetstr ("up", &buffer);
2114 term_dc = tgetstr ("dc", &buffer);
2115 term_DC = tgetstr ("DC", &buffer);
2117 visible_bell = tgetstr ("vb", &buffer);
2118 #endif /* !__GO32__ */
2121 /* A function for the use of tputs () */
2123 output_character_function (c)
2126 putc (c, out_stream);
2129 /* Write COUNT characters from STRING to the output stream. */
2131 output_some_chars (string, count)
2135 fwrite (string, 1, count, out_stream);
2138 /* Delete COUNT characters from the display line. */
2140 delete_chars (count)
2145 ScreenGetCursor(&r, &c);
2147 memcpy(ScreenPrimary+r*w+c, ScreenPrimary+r*w+c+count, w-c-count);
2148 memset(ScreenPrimary+r*w+w-count, 0, count*2);
2149 #else /* __GO32__ */
2150 if (count > screenwidth)
2153 if (term_DC && *term_DC)
2155 char *tgoto (), *buffer;
2156 buffer = tgoto (term_DC, 0, count);
2157 tputs (buffer, 1, output_character_function);
2161 if (term_dc && *term_dc)
2163 tputs (term_dc, 1, output_character_function);
2165 #endif /* __GO32__ */
2168 /* Insert COUNT characters from STRING to the output stream. */
2170 insert_some_chars (string, count)
2176 ScreenGetCursor(&r, &c);
2178 memcpy(ScreenPrimary+r*w+c+count, ScreenPrimary+r*w+c, w-c-count);
2179 /* Print the text. */
2180 output_some_chars (string, count);
2181 #else /* __GO32__ */
2182 /* If IC is defined, then we do not have to "enter" insert mode. */
2185 char *tgoto (), *buffer;
2186 buffer = tgoto (term_IC, 0, count);
2187 tputs (buffer, 1, output_character_function);
2188 output_some_chars (string, count);
2194 /* If we have to turn on insert-mode, then do so. */
2195 if (term_im && *term_im)
2196 tputs (term_im, 1, output_character_function);
2198 /* If there is a special command for inserting characters, then
2199 use that first to open up the space. */
2200 if (term_ic && *term_ic)
2202 for (i = count; i--; )
2203 tputs (term_ic, 1, output_character_function);
2206 /* Print the text. */
2207 output_some_chars (string, count);
2209 /* If there is a string to turn off insert mode, we had best use
2211 if (term_ei && *term_ei)
2212 tputs (term_ei, 1, output_character_function);
2214 #endif /* __GO32__ */
2217 /* Move the cursor back. */
2225 for (i = 0; i < count; i++)
2226 tputs (term_backspace, 1, output_character_function);
2228 #endif /* !__GO32__ */
2229 for (i = 0; i < count; i++)
2230 putc ('\b', out_stream);
2233 /* Move to the start of the next line. */
2236 #if defined (NEW_TTY_DRIVER)
2237 tputs (term_cr, 1, output_character_function);
2238 #endif /* NEW_TTY_DRIVER */
2239 putc ('\n', out_stream);
2242 /* Clear to the end of the line. COUNT is the minimum
2243 number of character spaces to clear, */
2244 clear_to_eol (count)
2250 tputs (term_clreol, 1, output_character_function);
2253 #endif /* !__GO32__ */
2257 /* Do one more character space. */
2260 for (i = 0; i < count; i++)
2261 putc (' ', out_stream);
2268 /* **************************************************************** */
2270 /* Saving and Restoring the TTY */
2272 /* **************************************************************** */
2274 /* Non-zero means that the terminal is in a prepped state. */
2275 static int terminal_prepped = 0;
2277 #if defined (NEW_TTY_DRIVER)
2279 /* Standard flags, including ECHO. */
2280 static int original_tty_flags = 0;
2282 /* Local mode flags, like LPASS8. */
2283 static int local_mode_flags = 0;
2285 /* Terminal characters. This has C-s and C-q in it. */
2286 static struct tchars original_tchars;
2288 /* Local special characters. This has the interrupt characters in it. */
2289 #if defined (TIOCGLTC)
2290 static struct ltchars original_ltchars;
2293 /* We use this to get and set the tty_flags. */
2294 static struct sgttyb the_ttybuff;
2296 /* Put the terminal in CBREAK mode so that we can detect key presses. */
2301 int tty = fileno (rl_instream);
2302 #if defined (HAVE_BSD_SIGNALS)
2304 #endif /* HAVE_BSD_SIGNALS */
2306 if (terminal_prepped)
2309 oldmask = sigblock (sigmask (SIGINT));
2311 /* We always get the latest tty values. Maybe stty changed them. */
2312 ioctl (tty, TIOCGETP, &the_ttybuff);
2313 original_tty_flags = the_ttybuff.sg_flags;
2315 readline_echoing_p = (original_tty_flags & ECHO);
2317 #if defined (TIOCLGET)
2318 ioctl (tty, TIOCLGET, &local_mode_flags);
2322 # define ANYP (EVENP | ODDP)
2325 /* If this terminal doesn't care how the 8th bit is used,
2326 then we can use it for the meta-key. We check by seeing
2327 if BOTH odd and even parity are allowed. */
2328 if (the_ttybuff.sg_flags & ANYP)
2331 the_ttybuff.sg_flags |= PASS8;
2334 /* Hack on local mode flags if we can. */
2335 #if defined (TIOCLGET) && defined (LPASS8)
2338 flags = local_mode_flags | LPASS8;
2339 ioctl (tty, TIOCLSET, &flags);
2341 #endif /* TIOCLGET && LPASS8 */
2344 #if defined (TIOCGETC)
2348 ioctl (tty, TIOCGETC, &original_tchars);
2349 temp = original_tchars;
2351 #if defined (USE_XON_XOFF)
2352 /* Get rid of C-s and C-q.
2353 We remember the value of startc (C-q) so that if the terminal is in
2354 xoff state, the user can xon it by pressing that character. */
2355 xon_char = temp.t_startc;
2359 /* If there is an XON character, bind it to restart the output. */
2361 rl_bind_key (xon_char, rl_restart_output);
2362 #endif /* USE_XON_XOFF */
2364 /* If there is an EOF char, bind eof_char to it. */
2365 if (temp.t_eofc != -1)
2366 eof_char = temp.t_eofc;
2368 #if defined (NO_KILL_INTR)
2369 /* Get rid of C-\ and C-c. */
2370 temp.t_intrc = temp.t_quitc = -1;
2371 #endif /* NO_KILL_INTR */
2373 ioctl (tty, TIOCSETC, &temp);
2375 #endif /* TIOCGETC */
2377 #if defined (TIOCGLTC)
2379 struct ltchars temp;
2381 ioctl (tty, TIOCGLTC, &original_ltchars);
2382 temp = original_ltchars;
2384 /* Make the interrupt keys go away. Just enough to make people
2386 temp.t_dsuspc = -1; /* C-y */
2387 temp.t_lnextc = -1; /* C-v */
2389 ioctl (tty, TIOCSLTC, &temp);
2391 #endif /* TIOCGLTC */
2393 the_ttybuff.sg_flags &= ~(ECHO | CRMOD);
2394 the_ttybuff.sg_flags |= CBREAK;
2395 ioctl (tty, TIOCSETN, &the_ttybuff);
2397 terminal_prepped = 1;
2399 #if defined (HAVE_BSD_SIGNALS)
2400 sigsetmask (oldmask);
2402 #endif /* !__GO32__ */
2405 /* Restore the terminal to its original state. */
2407 rl_deprep_terminal ()
2410 int tty = fileno (rl_instream);
2411 #if defined (HAVE_BSD_SIGNALS)
2415 if (!terminal_prepped)
2418 oldmask = sigblock (sigmask (SIGINT));
2420 the_ttybuff.sg_flags = original_tty_flags;
2421 ioctl (tty, TIOCSETN, &the_ttybuff);
2422 readline_echoing_p = 1;
2424 #if defined (TIOCLGET)
2425 ioctl (tty, TIOCLSET, &local_mode_flags);
2428 #if defined (TIOCSLTC)
2429 ioctl (tty, TIOCSLTC, &original_ltchars);
2432 #if defined (TIOCSETC)
2433 ioctl (tty, TIOCSETC, &original_tchars);
2435 terminal_prepped = 0;
2437 #if defined (HAVE_BSD_SIGNALS)
2438 sigsetmask (oldmask);
2440 #endif /* !__GO32 */
2443 #else /* !defined (NEW_TTY_DRIVER) */
2449 #if !defined (VTIME)
2454 #if defined (TERMIOS_TTY_DRIVER)
2455 static struct termios otio;
2457 static struct termio otio;
2458 #endif /* !TERMIOS_TTY_DRIVER */
2459 #endif /* __GO32__ */
2465 int tty = fileno (rl_instream);
2466 #if defined (TERMIOS_TTY_DRIVER)
2470 #endif /* !TERMIOS_TTY_DRIVER */
2472 #if defined (HAVE_POSIX_SIGNALS)
2475 # if defined (HAVE_BSD_SIGNALS)
2477 # endif /* HAVE_BSD_SIGNALS */
2478 #endif /* !HAVE_POSIX_SIGNALS */
2480 if (terminal_prepped)
2483 /* Try to keep this function from being INTerrupted. We can do it
2484 on POSIX and systems with BSD-like signal handling. */
2485 #if defined (HAVE_POSIX_SIGNALS)
2487 sigaddset (&set, SIGINT);
2488 sigprocmask (SIG_BLOCK, &set, &oset);
2489 #else /* !HAVE_POSIX_SIGNALS */
2490 # if defined (HAVE_BSD_SIGNALS)
2491 oldmask = sigblock (sigmask (SIGINT));
2492 # endif /* HAVE_BSD_SIGNALS */
2493 #endif /* !HAVE_POSIX_SIGNALS */
2495 #if defined (TERMIOS_TTY_DRIVER)
2496 tcgetattr (tty, &tio);
2498 ioctl (tty, TCGETA, &tio);
2499 #endif /* !TERMIOS_TTY_DRIVER */
2503 readline_echoing_p = (tio.c_lflag & ECHO);
2505 tio.c_lflag &= ~(ICANON|ECHO);
2507 if (otio.c_cc[VEOF] != _POSIX_VDISABLE)
2508 eof_char = otio.c_cc[VEOF];
2510 #if defined (USE_XON_XOFF)
2512 tio.c_iflag &= ~(IXON|IXOFF|IXANY);
2514 /* `strict' Posix systems do not define IXANY. */
2515 tio.c_iflag &= ~(IXON|IXOFF);
2517 #endif /* USE_XON_XOFF */
2519 /* Only turn this off if we are using all 8 bits. */
2521 tio.c_iflag &= ~(ISTRIP | INPCK);
2523 /* Make sure we differentiate between CR and NL on input. */
2524 tio.c_iflag &= ~(ICRNL | INLCR);
2526 #if !defined (HANDLE_SIGNALS)
2527 tio.c_lflag &= ~ISIG;
2529 tio.c_lflag |= ISIG;
2533 tio.c_cc[VTIME] = 0;
2535 /* Turn off characters that we need on Posix systems with job control,
2536 just to be sure. This includes ^Y and ^V. This should not really
2538 #if defined (TERMIOS_TTY_DRIVER) && defined (_POSIX_JOB_CONTROL)
2540 #if defined (VLNEXT)
2541 tio.c_cc[VLNEXT] = _POSIX_VDISABLE;
2544 #if defined (VDSUSP)
2545 tio.c_cc[VDSUSP] = _POSIX_VDISABLE;
2548 #endif /* POSIX && JOB_CONTROL */
2550 #if defined (TERMIOS_TTY_DRIVER)
2551 tcsetattr (tty, TCSADRAIN, &tio);
2552 tcflow (tty, TCOON); /* Simulate a ^Q. */
2554 ioctl (tty, TCSETAW, &tio);
2555 ioctl (tty, TCXONC, 1); /* Simulate a ^Q. */
2556 #endif /* !TERMIOS_TTY_DRIVER */
2558 terminal_prepped = 1;
2560 #if defined (HAVE_POSIX_SIGNALS)
2561 sigprocmask (SIG_SETMASK, &oset, (sigset_t *)NULL);
2563 # if defined (HAVE_BSD_SIGNALS)
2564 sigsetmask (oldmask);
2565 # endif /* HAVE_BSD_SIGNALS */
2566 #endif /* !HAVE_POSIX_SIGNALS */
2567 #endif /* !__GO32__ */
2571 rl_deprep_terminal ()
2574 int tty = fileno (rl_instream);
2576 /* Try to keep this function from being INTerrupted. We can do it
2577 on POSIX and systems with BSD-like signal handling. */
2578 #if defined (HAVE_POSIX_SIGNALS)
2580 #else /* !HAVE_POSIX_SIGNALS */
2581 # if defined (HAVE_BSD_SIGNALS)
2583 # endif /* HAVE_BSD_SIGNALS */
2584 #endif /* !HAVE_POSIX_SIGNALS */
2586 if (!terminal_prepped)
2589 #if defined (HAVE_POSIX_SIGNALS)
2591 sigaddset (&set, SIGINT);
2592 sigprocmask (SIG_BLOCK, &set, &oset);
2593 #else /* !HAVE_POSIX_SIGNALS */
2594 # if defined (HAVE_BSD_SIGNALS)
2595 oldmask = sigblock (sigmask (SIGINT));
2596 # endif /* HAVE_BSD_SIGNALS */
2597 #endif /* !HAVE_POSIX_SIGNALS */
2599 #if defined (TERMIOS_TTY_DRIVER)
2600 tcsetattr (tty, TCSADRAIN, &otio);
2601 tcflow (tty, TCOON); /* Simulate a ^Q. */
2602 #else /* TERMIOS_TTY_DRIVER */
2603 ioctl (tty, TCSETAW, &otio);
2604 ioctl (tty, TCXONC, 1); /* Simulate a ^Q. */
2605 #endif /* !TERMIOS_TTY_DRIVER */
2607 terminal_prepped = 0;
2609 #if defined (HAVE_POSIX_SIGNALS)
2610 sigprocmask (SIG_SETMASK, &oset, (sigset_t *)NULL);
2611 #else /* !HAVE_POSIX_SIGNALS */
2612 # if defined (HAVE_BSD_SIGNALS)
2613 sigsetmask (oldmask);
2614 # endif /* HAVE_BSD_SIGNALS */
2615 #endif /* !HAVE_POSIX_SIGNALS */
2616 #endif /* !__GO32__ */
2618 #endif /* NEW_TTY_DRIVER */
2621 /* **************************************************************** */
2623 /* Utility Functions */
2625 /* **************************************************************** */
2627 /* Return 0 if C is not a member of the class of characters that belong
2628 in words, or 1 if it is. */
2630 int allow_pathname_alphabetic_chars = 0;
2631 char *pathname_alphabetic_chars = "/-_=~.#$";
2637 if (pure_alphabetic (c) || (numeric (c)))
2640 if (allow_pathname_alphabetic_chars)
2641 return ((int)rindex (pathname_alphabetic_chars, c));
2646 /* Return non-zero if C is a numeric character. */
2651 return (c >= '0' && c <= '9');
2654 /* Ring the terminal bell. */
2658 if (readline_echoing_p)
2661 if (prefer_visible_bell && visible_bell)
2662 tputs (visible_bell, 1, output_character_function);
2664 #endif /* !__GO32__ */
2666 fprintf (stderr, "\007");
2673 /* How to abort things. */
2677 rl_clear_message ();
2678 rl_init_argument ();
2679 rl_pending_input = 0;
2681 defining_kbd_macro = 0;
2682 while (executing_macro)
2683 pop_executing_macro ();
2685 rl_last_func = (Function *)NULL;
2686 longjmp (readline_top_level, 1);
2689 /* Return a copy of the string between FROM and TO.
2690 FROM is inclusive, TO is not. */
2691 #if defined (sun) /* Yes, that's right, some crufty function in sunview is
2692 called rl_copy (). */
2699 register int length;
2702 /* Fix it if the caller is confused. */
2711 copy = (char *)xmalloc (1 + length);
2712 strncpy (copy, the_line + from, length);
2713 copy[length] = '\0';
2717 /* Increase the size of RL_LINE_BUFFER until it has enough space to hold
2720 rl_extend_line_buffer (len)
2723 while (len >= rl_line_buffer_len)
2726 (rl_line_buffer, rl_line_buffer_len += DEFAULT_BUFFER_SIZE);
2728 the_line = rl_line_buffer;
2732 /* **************************************************************** */
2734 /* Insert and Delete */
2736 /* **************************************************************** */
2738 /* Insert a string of text into the line at point. This is the only
2739 way that you should do insertion. rl_insert () calls this
2741 rl_insert_text (string)
2744 extern int doing_an_undo;
2745 register int i, l = strlen (string);
2747 if (rl_end + l >= rl_line_buffer_len)
2748 rl_extend_line_buffer (rl_end + l);
2750 for (i = rl_end; i >= rl_point; i--)
2751 the_line[i + l] = the_line[i];
2752 strncpy (the_line + rl_point, string, l);
2754 /* Remember how to undo this if we aren't undoing something. */
2757 /* If possible and desirable, concatenate the undos. */
2758 if ((strlen (string) == 1) &&
2760 (rl_undo_list->what == UNDO_INSERT) &&
2761 (rl_undo_list->end == rl_point) &&
2762 (rl_undo_list->end - rl_undo_list->start < 20))
2763 rl_undo_list->end++;
2765 rl_add_undo (UNDO_INSERT, rl_point, rl_point + l, (char *)NULL);
2769 the_line[rl_end] = '\0';
2772 /* Delete the string between FROM and TO. FROM is
2773 inclusive, TO is not. */
2774 rl_delete_text (from, to)
2777 extern int doing_an_undo;
2778 register char *text;
2780 /* Fix it if the caller is confused. */
2787 text = rl_copy (from, to);
2788 strncpy (the_line + from, the_line + to, rl_end - to);
2790 /* Remember how to undo this delete. */
2792 rl_add_undo (UNDO_DELETE, from, to, text);
2796 rl_end -= (to - from);
2797 the_line[rl_end] = '\0';
2801 /* **************************************************************** */
2803 /* Readline character functions */
2805 /* **************************************************************** */
2807 /* This is not a gap editor, just a stupid line input routine. No hair
2808 is involved in writing any of the functions, and none should be. */
2812 rl_end is the place in the string that we would place '\0';
2813 i.e., it is always safe to place '\0' there.
2815 rl_point is the place in the string where the cursor is. Sometimes
2816 this is the same as rl_end.
2818 Any command that is called interactively receives two arguments.
2819 The first is a count: the numeric arg pased to this command.
2820 The second is the key which invoked this command.
2824 /* **************************************************************** */
2826 /* Movement Commands */
2828 /* **************************************************************** */
2830 /* Note that if you `optimize' the display for these functions, you cannot
2831 use said functions in other functions which do not do optimizing display.
2832 I.e., you will have to update the data base for rl_redisplay, and you
2833 might as well let rl_redisplay do that job. */
2835 /* Move forward COUNT characters. */
2840 rl_backward (-count);
2844 #if defined (VI_MODE)
2845 if (rl_point == (rl_end - (rl_editing_mode == vi_mode)))
2847 if (rl_point == rl_end)
2848 #endif /* VI_MODE */
2859 /* Move backward COUNT characters. */
2864 rl_forward (-count);
2879 /* Move to the beginning of the line. */
2885 /* Move to the end of the line. */
2891 /* Move forward a word. We do what Emacs does. */
2892 rl_forward_word (count)
2899 rl_backward_word (-count);
2905 if (rl_point == rl_end)
2908 /* If we are not in a word, move forward until we are in one.
2909 Then, move forward until we hit a non-alphabetic character. */
2910 c = the_line[rl_point];
2911 if (!alphabetic (c))
2913 while (++rl_point < rl_end)
2915 c = the_line[rl_point];
2916 if (alphabetic (c)) break;
2919 if (rl_point == rl_end) return;
2920 while (++rl_point < rl_end)
2922 c = the_line[rl_point];
2923 if (!alphabetic (c)) break;
2929 /* Move backward a word. We do what Emacs does. */
2930 rl_backward_word (count)
2937 rl_forward_word (-count);
2946 /* Like rl_forward_word (), except that we look at the characters
2947 just before point. */
2949 c = the_line[rl_point - 1];
2950 if (!alphabetic (c))
2954 c = the_line[rl_point - 1];
2955 if (alphabetic (c)) break;
2961 c = the_line[rl_point - 1];
2962 if (!alphabetic (c))
2970 /* Clear the current line. Numeric argument to C-l does this. */
2973 int curr_line = last_c_pos / screenwidth;
2974 extern char *term_clreol;
2976 move_vert(curr_line);
2977 move_cursor_relative (0, the_line); /* XXX is this right */
2982 ScreenGetCursor(&r, &c);
2984 memset(ScreenPrimary+r*w+c, 0, (w-c)*2);
2986 #else /* __GO32__ */
2988 tputs (term_clreol, 1, output_character_function);
2989 #endif /* __GO32__/else */
2991 rl_forced_update_display ();
2992 rl_display_fixed = 1;
2995 /* C-l typed to a line without quoting clears the screen, and then reprints
2996 the prompt and the current input line. Given a numeric arg, redraw only
2997 the current line. */
3000 extern char *term_clrpag;
3002 if (rl_explicit_arg)
3010 tputs (term_clrpag, 1, output_character_function);
3012 #endif /* !__GO32__ */
3015 rl_forced_update_display ();
3016 rl_display_fixed = 1;
3019 rl_arrow_keys (count, c)
3024 ch = rl_read_key ();
3026 switch (to_upper (ch))
3029 rl_get_previous_history (count);
3033 rl_get_next_history (count);
3041 rl_backward (count);
3050 /* **************************************************************** */
3054 /* **************************************************************** */
3056 /* Insert the character C at the current location, moving point forward. */
3057 rl_insert (count, c)
3066 /* If we can optimize, then do it. But don't let people crash
3067 readline because of extra large arguments. */
3068 if (count > 1 && count < 1024)
3070 string = (char *)alloca (1 + count);
3072 for (i = 0; i < count; i++)
3076 rl_insert_text (string);
3084 string = (char *)alloca (1024 + 1);
3086 for (i = 0; i < 1024; i++)
3091 decreaser = (count > 1024 ? 1024 : count);
3092 string[decreaser] = '\0';
3093 rl_insert_text (string);
3099 /* We are inserting a single character.
3100 If there is pending input, then make a string of all of the
3101 pending characters that are bound to rl_insert, and insert
3108 string = (char *)alloca (ibuffer_len + 1);
3111 while ((t = rl_get_char (&key)) &&
3112 (keymap[key].type == ISFUNC &&
3113 keymap[key].function == rl_insert))
3117 rl_unget_char (key);
3120 rl_insert_text (string);
3125 /* Inserting a single character. */
3126 string = (char *)alloca (2);
3130 rl_insert_text (string);
3134 /* Insert the next typed character verbatim. */
3135 rl_quoted_insert (count)
3138 int c = rl_read_key ();
3139 rl_insert (count, c);
3142 /* Insert a tab character. */
3143 rl_tab_insert (count)
3146 rl_insert (count, '\t');
3149 /* What to do when a NEWLINE is pressed. We accept the whole line.
3150 KEY is the key that invoked this command. I guess it could have
3151 meaning in the future. */
3152 rl_newline (count, key)
3158 #if defined (VI_MODE)
3160 extern int vi_doing_insert;
3161 if (vi_doing_insert)
3163 rl_end_undo_group ();
3164 vi_doing_insert = 0;
3167 #endif /* VI_MODE */
3169 if (readline_echoing_p)
3171 move_vert (vis_botlin);
3174 fflush (out_stream);
3179 rl_clean_up_for_exit ()
3181 if (readline_echoing_p)
3183 move_vert (vis_botlin);
3185 fflush (out_stream);
3186 rl_restart_output ();
3190 /* What to do for some uppercase characters, like meta characters,
3191 and some characters appearing in emacs_ctlx_keymap. This function
3192 is just a stub, you bind keys to it and the code in rl_dispatch ()
3193 is special cased. */
3194 rl_do_lowercase_version (ignore1, ignore2)
3195 int ignore1, ignore2;
3199 /* Rubout the character behind point. */
3217 int orig_point = rl_point;
3218 rl_backward (count);
3219 rl_kill_text (orig_point, rl_point);
3223 int c = the_line[--rl_point];
3224 rl_delete_text (rl_point, rl_point + 1);
3226 if (rl_point == rl_end && alphabetic (c) && last_c_pos)
3229 putc (' ', out_stream);
3232 visible_line[last_c_pos] = '\0';
3238 /* Delete the character under the cursor. Given a numeric argument,
3239 kill that many characters instead. */
3240 rl_delete (count, invoking_key)
3241 int count, invoking_key;
3249 if (rl_point == rl_end)
3257 int orig_point = rl_point;
3259 rl_kill_text (orig_point, rl_point);
3260 rl_point = orig_point;
3263 rl_delete_text (rl_point, rl_point + 1);
3267 /* **************************************************************** */
3271 /* **************************************************************** */
3273 /* The next two functions mimic unix line editing behaviour, except they
3274 save the deleted text on the kill ring. This is safer than not saving
3275 it, and since we have a ring, nobody should get screwed. */
3277 /* This does what C-w does in Unix. We can't prevent people from
3278 using behaviour that they expect. */
3279 rl_unix_word_rubout ()
3281 if (!rl_point) ding ();
3283 int orig_point = rl_point;
3284 while (rl_point && whitespace (the_line[rl_point - 1]))
3286 while (rl_point && !whitespace (the_line[rl_point - 1]))
3288 rl_kill_text (rl_point, orig_point);
3292 /* Here is C-u doing what Unix does. You don't *have* to use these
3293 key-bindings. We have a choice of killing the entire line, or
3294 killing from where we are to the start of the line. We choose the
3295 latter, because if you are a Unix weenie, then you haven't backspaced
3296 into the line at all, and if you aren't, then you know what you are
3298 rl_unix_line_discard ()
3300 if (!rl_point) ding ();
3302 rl_kill_text (rl_point, 0);
3309 /* **************************************************************** */
3311 /* Commands For Typos */
3313 /* **************************************************************** */
3315 /* Random and interesting things in here. */
3317 /* **************************************************************** */
3321 /* **************************************************************** */
3323 /* The three kinds of things that we know how to do. */
3328 /* Uppercase the word at point. */
3329 rl_upcase_word (count)
3332 rl_change_case (count, UpCase);
3335 /* Lowercase the word at point. */
3336 rl_downcase_word (count)
3339 rl_change_case (count, DownCase);
3342 /* Upcase the first letter, downcase the rest. */
3343 rl_capitalize_word (count)
3346 rl_change_case (count, CapCase);
3349 /* The meaty function.
3350 Change the case of COUNT words, performing OP on them.
3351 OP is one of UpCase, DownCase, or CapCase.
3352 If a negative argument is given, leave point where it started,
3353 otherwise, leave it where it moves to. */
3354 rl_change_case (count, op)
3357 register int start = rl_point, end;
3360 rl_forward_word (count);
3370 /* We are going to modify some text, so let's prepare to undo it. */
3371 rl_modifying (start, end);
3373 for (; start < end; start++)
3378 the_line[start] = to_upper (the_line[start]);
3382 the_line[start] = to_lower (the_line[start]);
3388 the_line[start] = to_upper (the_line[start]);
3393 the_line[start] = to_lower (the_line[start]);
3395 if (!pure_alphabetic (the_line[start]))
3406 /* **************************************************************** */
3410 /* **************************************************************** */
3412 /* Transpose the words at point. */
3413 rl_transpose_words (count)
3416 char *word1, *word2;
3417 int w1_beg, w1_end, w2_beg, w2_end;
3418 int orig_point = rl_point;
3422 /* Find the two words. */
3423 rl_forward_word (count);
3425 rl_backward_word (1);
3427 rl_backward_word (count);
3429 rl_forward_word (1);
3432 /* Do some check to make sure that there really are two words. */
3433 if ((w1_beg == w2_beg) || (w2_beg < w1_end))
3436 rl_point = orig_point;
3440 /* Get the text of the words. */
3441 word1 = rl_copy (w1_beg, w1_end);
3442 word2 = rl_copy (w2_beg, w2_end);
3444 /* We are about to do many insertions and deletions. Remember them
3445 as one operation. */
3446 rl_begin_undo_group ();
3448 /* Do the stuff at word2 first, so that we don't have to worry
3449 about word1 moving. */
3451 rl_delete_text (w2_beg, w2_end);
3452 rl_insert_text (word1);
3455 rl_delete_text (w1_beg, w1_end);
3456 rl_insert_text (word2);
3458 /* This is exactly correct since the text before this point has not
3459 changed in length. */
3462 /* I think that does it. */
3463 rl_end_undo_group ();
3464 free (word1); free (word2);
3467 /* Transpose the characters at point. If point is at the end of the line,
3468 then transpose the characters before point. */
3469 rl_transpose_chars (count)
3475 if (!rl_point || rl_end < 2) {
3482 if (rl_point == rl_end)
3484 int t = the_line[rl_point - 1];
3486 the_line[rl_point - 1] = the_line[rl_point - 2];
3487 the_line[rl_point - 2] = t;
3491 int t = the_line[rl_point];
3493 the_line[rl_point] = the_line[rl_point - 1];
3494 the_line[rl_point - 1] = t;
3496 if (count < 0 && rl_point)
3510 /* **************************************************************** */
3512 /* Bogus Flow Control */
3514 /* **************************************************************** */
3516 rl_restart_output (count, key)
3519 int fildes = fileno (rl_outstream);
3520 #if defined (TIOCSTART)
3521 #if defined (apollo)
3522 ioctl (&fildes, TIOCSTART, 0);
3524 ioctl (fildes, TIOCSTART, 0);
3528 # if defined (TERMIOS_TTY_DRIVER)
3529 tcflow (fildes, TCOON);
3531 # if defined (TCXONC)
3532 ioctl (fildes, TCXONC, TCOON);
3533 # endif /* TCXONC */
3534 # endif /* !TERMIOS_TTY_DRIVER */
3535 #endif /* TIOCSTART */
3538 rl_stop_output (count, key)
3541 int fildes = fileno (rl_instream);
3543 #if defined (TIOCSTOP)
3544 # if defined (apollo)
3545 ioctl (&fildes, TIOCSTOP, 0);
3547 ioctl (fildes, TIOCSTOP, 0);
3548 # endif /* apollo */
3550 # if defined (TERMIOS_TTY_DRIVER)
3551 tcflow (fildes, TCOOFF);
3553 # if defined (TCXONC)
3554 ioctl (fildes, TCXONC, TCOON);
3555 # endif /* TCXONC */
3556 # endif /* !TERMIOS_TTY_DRIVER */
3557 #endif /* TIOCSTOP */
3560 /* **************************************************************** */
3562 /* Completion matching, from readline's point of view. */
3564 /* **************************************************************** */
3566 /* Pointer to the generator function for completion_matches ().
3567 NULL means to use filename_entry_function (), the default filename
3569 Function *rl_completion_entry_function = (Function *)NULL;
3571 /* Pointer to alternative function to create matches.
3572 Function is called with TEXT, START, and END.
3573 START and END are indices in RL_LINE_BUFFER saying what the boundaries
3575 If this function exists and returns NULL then call the value of
3576 rl_completion_entry_function to try to match, otherwise use the
3577 array of strings returned. */
3578 Function *rl_attempted_completion_function = (Function *)NULL;
3580 /* Local variable states what happened during the last completion attempt. */
3581 static int completion_changed_buffer = 0;
3583 /* Complete the word at or before point. You have supplied the function
3584 that does the initial simple matching selection algorithm (see
3585 completion_matches ()). The default is to do filename completion. */
3587 rl_complete (ignore, invoking_key)
3588 int ignore, invoking_key;
3590 if (rl_last_func == rl_complete && !completion_changed_buffer)
3591 rl_complete_internal ('?');
3593 rl_complete_internal (TAB);
3596 /* List the possible completions. See description of rl_complete (). */
3597 rl_possible_completions ()
3599 rl_complete_internal ('?');
3602 /* The user must press "y" or "n". Non-zero return means "y" pressed. */
3608 if (c == 'y' || c == 'Y') return (1);
3609 if (c == 'n' || c == 'N') return (0);
3610 if (c == ABORT_CHAR) rl_abort ();
3614 /* Up to this many items will be displayed in response to a
3615 possible-completions call. After that, we ask the user if
3616 she is sure she wants to see them all. */
3617 int rl_completion_query_items = 100;
3619 /* The basic list of characters that signal a break between words for the
3620 completer routine. The contents of this variable is what breaks words
3621 in the shell, i.e. " \t\n\"\\'`@$><=" */
3622 char *rl_basic_word_break_characters = " \t\n\"\\'`@$><=;|&{(";
3624 /* The list of characters that signal a break between words for
3625 rl_complete_internal. The default list is the contents of
3626 rl_basic_word_break_characters. */
3627 char *rl_completer_word_break_characters = (char *)NULL;
3629 /* The list of characters which are used to quote a substring of the command
3630 line. Command completion occurs on the entire substring, and within the
3631 substring rl_completer_word_break_characters are treated as any other
3632 character, unless they also appear within this list. */
3633 char *rl_completer_quote_characters = (char *)NULL;
3635 /* List of characters that are word break characters, but should be left
3636 in TEXT when it is passed to the completion function. The shell uses
3637 this to help determine what kind of completing to do. */
3638 char *rl_special_prefixes = (char *)NULL;
3640 /* If non-zero, then disallow duplicates in the matches. */
3641 int rl_ignore_completion_duplicates = 1;
3643 /* Non-zero means that the results of the matches are to be treated
3644 as filenames. This is ALWAYS zero on entry, and can only be changed
3645 within a completion entry finder function. */
3646 int rl_filename_completion_desired = 0;
3648 /* This function, if defined, is called by the completer when real
3649 filename completion is done, after all the matching names have been
3650 generated. It is passed a (char**) known as matches in the code below.
3651 It consists of a NULL-terminated array of pointers to potential
3652 matching strings. The 1st element (matches[0]) is the maximal
3653 substring that is common to all matches. This function can re-arrange
3654 the list of matches as required, but all elements of the array must be
3655 free()'d if they are deleted. The main intent of this function is
3656 to implement FIGNORE a la SunOS csh. */
3657 Function *rl_ignore_some_completions_function = (Function *)NULL;
3659 /* Complete the word at or before point.
3660 WHAT_TO_DO says what to do with the completion.
3661 `?' means list the possible completions.
3662 TAB means do standard completion.
3663 `*' means insert all of the possible completions. */
3664 rl_complete_internal (what_to_do)
3667 char *filename_completion_function ();
3668 char **completion_matches (), **matches;
3670 int start, scan, end, delimiter = 0;
3671 char *text, *saved_line_buffer;
3672 char quote_char = '\0';
3676 saved_line_buffer = savestring (the_line);
3678 saved_line_buffer = (char *)NULL;
3680 if (rl_completion_entry_function)
3681 our_func = rl_completion_entry_function;
3683 our_func = (int (*)())filename_completion_function;
3685 /* Only the completion entry function can change this. */
3686 rl_filename_completion_desired = 0;
3688 /* We now look backwards for the start of a filename/variable word. */
3693 if (rl_completer_quote_characters)
3695 /* We have a list of characters which can be used in pairs to quote
3696 substrings for completion. Try to find the start of an unclosed
3698 FIXME: Doesn't yet handle '\' escapes to hid embedded quotes */
3699 for (scan = 0; scan < end; scan++)
3701 if (quote_char != '\0')
3703 /* Ignore everything until the matching close quote char */
3704 if (the_line[scan] == quote_char)
3706 /* Found matching close quote. Abandon this substring. */
3711 else if (rindex (rl_completer_quote_characters, the_line[scan]))
3713 /* Found start of a quoted substring. */
3714 quote_char = the_line[scan];
3715 rl_point = scan + 1;
3719 if (rl_point == end)
3721 /* We didn't find an unclosed quoted substring upon which to do
3722 completion, so use the word break characters to find the
3723 substring on which to do completion. */
3724 while (--rl_point &&
3725 !rindex (rl_completer_word_break_characters,
3726 the_line[rl_point])) {;}
3729 /* If we are at a word break, then advance past it. */
3730 if (rindex (rl_completer_word_break_characters, the_line[rl_point]))
3732 /* If the character that caused the word break was a quoting
3733 character, then remember it as the delimiter. */
3734 if (rindex ("\"'", the_line[rl_point]) && (end - rl_point) > 1)
3735 delimiter = the_line[rl_point];
3737 /* If the character isn't needed to determine something special
3738 about what kind of completion to perform, then advance past it. */
3740 if (!rl_special_prefixes ||
3741 !rindex (rl_special_prefixes, the_line[rl_point]))
3748 text = rl_copy (start, end);
3750 /* If the user wants to TRY to complete, but then wants to give
3751 up and use the default completion function, they set the
3752 variable rl_attempted_completion_function. */
3753 if (rl_attempted_completion_function)
3756 (char **)(*rl_attempted_completion_function) (text, start, end);
3760 our_func = (Function *)NULL;
3761 goto after_usual_completion;
3765 matches = completion_matches (text, our_func);
3767 after_usual_completion:
3778 /* It seems to me that in all the cases we handle we would like
3779 to ignore duplicate possibilities. Scan for the text to
3780 insert being identical to the other completions. */
3781 if (rl_ignore_completion_duplicates)
3783 char *lowest_common;
3786 /* Sort the items. */
3787 /* It is safe to sort this array, because the lowest common
3788 denominator found in matches[0] will remain in place. */
3789 for (i = 0; matches[i]; i++);
3790 qsort (matches, i, sizeof (char *), compare_strings);
3792 /* Remember the lowest common denominator for it may be unique. */
3793 lowest_common = savestring (matches[0]);
3795 for (i = 0; matches[i + 1]; i++)
3797 if (strcmp (matches[i], matches[i + 1]) == 0)
3800 matches[i] = (char *)-1;
3806 /* We have marked all the dead slots with (char *)-1.
3807 Copy all the non-dead entries into a new array. */
3810 (char **)malloc ((3 + newlen) * sizeof (char *));
3812 for (i = 1, j = 1; matches[i]; i++)
3814 if (matches[i] != (char *)-1)
3815 temp_array[j++] = matches[i];
3818 temp_array[j] = (char *)NULL;
3820 if (matches[0] != (char *)-1)
3825 matches = temp_array;
3828 /* Place the lowest common denominator back in [0]. */
3829 matches[0] = lowest_common;
3831 /* If there is one string left, and it is identical to the
3832 lowest common denominator, then the LCD is the string to
3834 if (j == 2 && strcmp (matches[0], matches[1]) == 0)
3837 matches[1] = (char *)NULL;
3844 /* If we are matching filenames, then here is our chance to
3845 do clever processing by re-examining the list. Call the
3846 ignore function with the array as a parameter. It can
3847 munge the array, deleting matches as it desires. */
3848 if (rl_ignore_some_completions_function &&
3849 our_func == (int (*)())filename_completion_function)
3850 (void)(*rl_ignore_some_completions_function)(matches);
3852 /* If we are doing completions on quoted substrings, and any matches
3853 contain any of the completer word break characters, then auto-
3854 matically prepend the substring with a quote character (just
3855 pick the first one from the list of such) if it does not already
3856 begin with a quote string. FIXME: Need to remove any such
3857 automatically inserted quote character when it no longer is
3858 necessary, such as if we change the string we are completing on
3859 and the new set of matches don't require a quoted substring? */
3861 replacement = matches[0];
3862 if (matches[0] != NULL
3863 && rl_completer_quote_characters != NULL
3864 && (quote_char == '\0'))
3866 for (i = 1; matches[i] != NULL; i++)
3868 if (strpbrk (matches[i], rl_completer_word_break_characters))
3870 /* Found an embedded word break character in a potential
3871 match, so need to prepend a quote character if we are
3872 replacing the completion string. */
3873 replacement = alloca (strlen (matches[0]) + 2);
3874 quote_char = *rl_completer_quote_characters;
3875 *replacement = quote_char;
3876 strcpy (replacement + 1, matches[0]);
3883 rl_delete_text (start, rl_point);
3885 rl_insert_text (replacement);
3888 /* If there are more matches, ring the bell to indicate.
3889 If this was the only match, and we are hacking files,
3890 check the file to see if it was a directory. If so,
3891 add a '/' to the name. If not, and we are at the end
3892 of the line, then add a space. */
3895 ding (); /* There are other matches remaining. */
3899 char temp_string[16];
3904 temp_string[temp_index++] = quote_char;
3906 temp_string[temp_index++] = delimiter ? delimiter : ' ';
3907 temp_string[temp_index++] = '\0';
3909 if (rl_filename_completion_desired)
3912 char *filename = tilde_expand (matches[0]);
3914 if ((stat (filename, &finfo) == 0) &&
3915 S_ISDIR (finfo.st_mode))
3917 if (the_line[rl_point] != '/')
3918 rl_insert_text ("/");
3922 if (rl_point == rl_end)
3923 rl_insert_text (temp_string);
3929 if (rl_point == rl_end)
3930 rl_insert_text (temp_string);
3939 rl_delete_text (start, rl_point);
3941 rl_begin_undo_group ();
3946 rl_insert_text (matches[i++]);
3947 rl_insert_text (" ");
3952 rl_insert_text (matches[0]);
3953 rl_insert_text (" ");
3955 rl_end_undo_group ();
3961 int len, count, limit, max = 0;
3964 /* Handle simple case first. What if there is only one answer? */
3969 if (rl_filename_completion_desired)
3970 temp = rindex (matches[0], '/');
3972 temp = (char *)NULL;
3980 fprintf (out_stream, "%s", temp);
3985 /* There is more than one answer. Find out how many there are,
3986 and find out what the maximum printed length of a single entry
3988 for (i = 1; matches[i]; i++)
3990 char *temp = (char *)NULL;
3992 /* If we are hacking filenames, then only count the characters
3993 after the last slash in the pathname. */
3994 if (rl_filename_completion_desired)
3995 temp = rindex (matches[i], '/');
3997 temp = (char *)NULL;
4004 if (strlen (temp) > max)
4005 max = strlen (temp);
4010 /* If there are many items, then ask the user if she
4011 really wants to see them all. */
4012 if (len >= rl_completion_query_items)
4015 fprintf (out_stream,
4016 "There are %d possibilities. Do you really", len);
4018 fprintf (out_stream, "wish to see them all? (y or n)");
4019 fflush (out_stream);
4026 /* How many items of MAX length can we fit in the screen window? */
4028 limit = screenwidth / max;
4029 if (limit != 1 && (limit * max == screenwidth))
4032 /* Avoid a possible floating exception. If max > screenwidth,
4033 limit will be 0 and a divide-by-zero fault will result. */
4037 /* How many iterations of the printing loop? */
4038 count = (len + (limit - 1)) / limit;
4040 /* Watch out for special case. If LEN is less than LIMIT, then
4041 just do the inner printing loop. */
4042 if (len < limit) count = 1;
4044 /* Sort the items if they are not already sorted. */
4045 if (!rl_ignore_completion_duplicates)
4046 qsort (matches, len, sizeof (char *), compare_strings);
4048 /* Print the sorted items, up-and-down alphabetically, like
4052 for (i = 1; i < count + 1; i++)
4054 for (j = 0, l = i; j < limit; j++)
4056 if (l > len || !matches[l])
4062 char *temp = (char *)NULL;
4064 if (rl_filename_completion_desired)
4065 temp = rindex (matches[l], '/');
4067 temp = (char *)NULL;
4074 fprintf (out_stream, "%s", temp);
4075 for (k = 0; k < max - strlen (temp); k++)
4076 putc (' ', out_stream);
4092 for (i = 0; matches[i]; i++)
4097 /* Check to see if the line has changed through all of this manipulation. */
4098 if (saved_line_buffer)
4100 if (strcmp (the_line, saved_line_buffer) != 0)
4101 completion_changed_buffer = 1;
4103 completion_changed_buffer = 0;
4105 free (saved_line_buffer);
4109 /* Stupid comparison routine for qsort () ing strings. */
4111 compare_strings (s1, s2)
4114 return (strcmp (*s1, *s2));
4117 /* A completion function for usernames.
4118 TEXT contains a partial username preceded by a random
4119 character (usually `~'). */
4121 username_completion_function (text, state)
4126 return (char *)NULL;
4127 #else /* !__GO32__ */
4128 static char *username = (char *)NULL;
4129 static struct passwd *entry;
4130 static int namelen, first_char, first_char_loc;
4139 if (first_char == '~')
4144 username = savestring (&text[first_char_loc]);
4145 namelen = strlen (username);
4149 while (entry = getpwent ())
4151 if (strncmp (username, entry->pw_name, namelen) == 0)
4158 return ((char *)NULL);
4162 char *value = (char *)xmalloc (2 + strlen (entry->pw_name));
4166 strcpy (value + first_char_loc, entry->pw_name);
4168 if (first_char == '~')
4169 rl_filename_completion_desired = 1;
4173 #endif /* !__GO32__ */
4176 /* **************************************************************** */
4178 /* Undo, and Undoing */
4180 /* **************************************************************** */
4182 /* Non-zero tells rl_delete_text and rl_insert_text to not add to
4184 int doing_an_undo = 0;
4186 /* The current undo list for THE_LINE. */
4187 UNDO_LIST *rl_undo_list = (UNDO_LIST *)NULL;
4189 /* Remember how to undo something. Concatenate some undos if that
4191 rl_add_undo (what, start, end, text)
4192 enum undo_code what;
4196 UNDO_LIST *temp = (UNDO_LIST *)xmalloc (sizeof (UNDO_LIST));
4198 temp->start = start;
4201 temp->next = rl_undo_list;
4202 rl_undo_list = temp;
4205 /* Free the existing undo list. */
4208 while (rl_undo_list) {
4209 UNDO_LIST *release = rl_undo_list;
4210 rl_undo_list = rl_undo_list->next;
4212 if (release->what == UNDO_DELETE)
4213 free (release->text);
4219 /* Undo the next thing in the list. Return 0 if there
4220 is nothing to undo, or non-zero if there was. */
4225 int waiting_for_begin = 0;
4233 switch (rl_undo_list->what) {
4235 /* Undoing deletes means inserting some text. */
4237 rl_point = rl_undo_list->start;
4238 rl_insert_text (rl_undo_list->text);
4239 free (rl_undo_list->text);
4242 /* Undoing inserts means deleting some text. */
4244 rl_delete_text (rl_undo_list->start, rl_undo_list->end);
4245 rl_point = rl_undo_list->start;
4248 /* Undoing an END means undoing everything 'til we get to
4251 waiting_for_begin++;
4254 /* Undoing a BEGIN means that we are done with this group. */
4256 if (waiting_for_begin)
4257 waiting_for_begin--;
4265 release = rl_undo_list;
4266 rl_undo_list = rl_undo_list->next;
4269 if (waiting_for_begin)
4275 /* Begin a group. Subsequent undos are undone as an atomic operation. */
4276 rl_begin_undo_group ()
4278 rl_add_undo (UNDO_BEGIN, 0, 0, 0);
4281 /* End an undo group started with rl_begin_undo_group (). */
4282 rl_end_undo_group ()
4284 rl_add_undo (UNDO_END, 0, 0, 0);
4287 /* Save an undo entry for the text from START to END. */
4288 rl_modifying (start, end)
4300 char *temp = rl_copy (start, end);
4301 rl_begin_undo_group ();
4302 rl_add_undo (UNDO_DELETE, start, end, temp);
4303 rl_add_undo (UNDO_INSERT, start, end, (char *)NULL);
4304 rl_end_undo_group ();
4308 /* Revert the current line to its previous state. */
4311 if (!rl_undo_list) ding ();
4313 while (rl_undo_list)
4318 /* Do some undoing of things that were done. */
4319 rl_undo_command (count)
4321 if (count < 0) return; /* Nothing to do. */
4337 /* **************************************************************** */
4339 /* History Utilities */
4341 /* **************************************************************** */
4343 /* We already have a history library, and that is what we use to control
4344 the history features of readline. However, this is our local interface
4345 to the history mechanism. */
4347 /* While we are editing the history, this is the saved
4348 version of the original line. */
4349 HIST_ENTRY *saved_line_for_history = (HIST_ENTRY *)NULL;
4351 /* Set the history pointer back to the last entry in the history. */
4352 start_using_history ()
4355 if (saved_line_for_history)
4356 free_history_entry (saved_line_for_history);
4358 saved_line_for_history = (HIST_ENTRY *)NULL;
4361 /* Free the contents (and containing structure) of a HIST_ENTRY. */
4362 free_history_entry (entry)
4371 /* Perhaps put back the current line if it has changed. */
4372 maybe_replace_line ()
4374 HIST_ENTRY *temp = current_history ();
4376 /* If the current line has changed, save the changes. */
4377 if (temp && ((UNDO_LIST *)(temp->data) != rl_undo_list))
4379 temp = replace_history_entry (where_history (), the_line, rl_undo_list);
4385 /* Put back the saved_line_for_history if there is one. */
4386 maybe_unsave_line ()
4388 if (saved_line_for_history)
4392 line_len = strlen (saved_line_for_history->line);
4394 if (line_len >= rl_line_buffer_len)
4395 rl_extend_line_buffer (line_len);
4397 strcpy (the_line, saved_line_for_history->line);
4398 rl_undo_list = (UNDO_LIST *)saved_line_for_history->data;
4399 free_history_entry (saved_line_for_history);
4400 saved_line_for_history = (HIST_ENTRY *)NULL;
4401 rl_end = rl_point = strlen (the_line);
4407 /* Save the current line in saved_line_for_history. */
4410 if (!saved_line_for_history)
4412 saved_line_for_history = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
4413 saved_line_for_history->line = savestring (the_line);
4414 saved_line_for_history->data = (char *)rl_undo_list;
4418 /* **************************************************************** */
4420 /* History Commands */
4422 /* **************************************************************** */
4424 /* Meta-< goes to the start of the history. */
4425 rl_beginning_of_history ()
4427 rl_get_previous_history (1 + where_history ());
4430 /* Meta-> goes to the end of the history. (The current line). */
4431 rl_end_of_history ()
4433 maybe_replace_line ();
4435 maybe_unsave_line ();
4438 /* Move down to the next history line. */
4439 rl_get_next_history (count)
4442 HIST_ENTRY *temp = (HIST_ENTRY *)NULL;
4446 rl_get_previous_history (-count);
4453 maybe_replace_line ();
4457 temp = next_history ();
4464 maybe_unsave_line ();
4469 line_len = strlen (temp->line);
4471 if (line_len >= rl_line_buffer_len)
4472 rl_extend_line_buffer (line_len);
4474 strcpy (the_line, temp->line);
4475 rl_undo_list = (UNDO_LIST *)temp->data;
4476 rl_end = rl_point = strlen (the_line);
4477 #if defined (VI_MODE)
4478 if (rl_editing_mode == vi_mode)
4480 #endif /* VI_MODE */
4484 /* Get the previous item out of our interactive history, making it the current
4485 line. If there is no previous history, just ding. */
4486 rl_get_previous_history (count)
4489 HIST_ENTRY *old_temp = (HIST_ENTRY *)NULL;
4490 HIST_ENTRY *temp = (HIST_ENTRY *)NULL;
4494 rl_get_next_history (-count);
4501 /* If we don't have a line saved, then save this one. */
4504 /* If the current line has changed, save the changes. */
4505 maybe_replace_line ();
4509 temp = previous_history ();
4517 /* If there was a large argument, and we moved back to the start of the
4518 history, that is not an error. So use the last value found. */
4519 if (!temp && old_temp)
4528 line_len = strlen (temp->line);
4530 if (line_len >= rl_line_buffer_len)
4531 rl_extend_line_buffer (line_len);
4533 strcpy (the_line, temp->line);
4534 rl_undo_list = (UNDO_LIST *)temp->data;
4535 rl_end = rl_point = line_len;
4537 #if defined (VI_MODE)
4538 if (rl_editing_mode == vi_mode)
4540 #endif /* VI_MODE */
4545 /* **************************************************************** */
4547 /* I-Search and Searching */
4549 /* **************************************************************** */
4551 /* Search backwards through the history looking for a string which is typed
4552 interactively. Start with the current line. */
4553 rl_reverse_search_history (sign, key)
4557 rl_search_history (-sign, key);
4560 /* Search forwards through the history looking for a string which is typed
4561 interactively. Start with the current line. */
4562 rl_forward_search_history (sign, key)
4566 rl_search_history (sign, key);
4569 /* Display the current state of the search in the echo-area.
4570 SEARCH_STRING contains the string that is being searched for,
4571 DIRECTION is zero for forward, or 1 for reverse,
4572 WHERE is the history list number of the current line. If it is
4573 -1, then this line is the starting one. */
4574 rl_display_search (search_string, reverse_p, where)
4575 char *search_string;
4576 int reverse_p, where;
4578 char *message = (char *)NULL;
4581 (char *)alloca (1 + (search_string ? strlen (search_string) : 0) + 30);
4585 #if defined (NOTDEF)
4587 sprintf (message, "[%d]", where + history_base);
4590 strcat (message, "(");
4593 strcat (message, "reverse-");
4595 strcat (message, "i-search)`");
4598 strcat (message, search_string);
4600 strcat (message, "': ");
4601 rl_message (message, 0, 0);
4605 /* Search through the history looking for an interactively typed string.
4606 This is analogous to i-search. We start the search in the current line.
4607 DIRECTION is which direction to search; >= 0 means forward, < 0 means
4609 rl_search_history (direction, invoking_key)
4613 /* The string that the user types in to search for. */
4614 char *search_string = (char *)alloca (128);
4616 /* The current length of SEARCH_STRING. */
4617 int search_string_index;
4619 /* The list of lines to search through. */
4622 /* The length of LINES. */
4625 /* Where we get LINES from. */
4626 HIST_ENTRY **hlist = history_list ();
4629 int orig_point = rl_point;
4630 int orig_line = where_history ();
4631 int last_found_line = orig_line;
4634 /* The line currently being searched. */
4637 /* Offset in that line. */
4640 /* Non-zero if we are doing a reverse search. */
4641 int reverse = (direction < 0);
4643 /* Create an arrary of pointers to the lines that we want to search. */
4644 maybe_replace_line ();
4646 for (i = 0; hlist[i]; i++);
4648 /* Allocate space for this many lines, +1 for the current input line,
4649 and remember those lines. */
4650 lines = (char **)alloca ((1 + (hlen = i)) * sizeof (char *));
4651 for (i = 0; i < hlen; i++)
4652 lines[i] = hlist[i]->line;
4654 if (saved_line_for_history)
4655 lines[i] = saved_line_for_history->line;
4657 /* So I have to type it in this way instead. */
4661 /* Keep that mips alloca happy. */
4662 alloced_line = (char *)alloca (1 + strlen (the_line));
4663 lines[i] = alloced_line;
4664 strcpy (lines[i], &the_line[0]);
4669 /* The line where we start the search. */
4672 /* Initialize search parameters. */
4673 *search_string = '\0';
4674 search_string_index = 0;
4676 /* Normalize DIRECTION into 1 or -1. */
4682 rl_display_search (search_string, reverse, -1);
4691 /* Hack C to Do What I Mean. */
4693 Function *f = (Function *)NULL;
4695 if (keymap[c].type == ISFUNC)
4697 f = keymap[c].function;
4699 if (f == rl_reverse_search_history)
4700 c = reverse ? -1 : -2;
4701 else if (f == rl_forward_search_history)
4702 c = !reverse ? -1 : -2;
4712 /* case invoking_key: */
4716 /* switch directions */
4718 direction = -direction;
4719 reverse = (direction < 0);
4724 strcpy (the_line, lines[orig_line]);
4725 rl_point = orig_point;
4726 rl_end = strlen (the_line);
4727 rl_clear_message ();
4731 if (c < 32 || c > 126)
4733 rl_execute_next (c);
4739 search_string[search_string_index++] = c;
4740 search_string[search_string_index] = '\0';
4745 if (!search_string_index)
4752 if (index != strlen (sline))
4765 (search_string, sline + index, search_string_index)
4773 register int limit =
4774 (strlen (sline) - search_string_index) + 1;
4776 while (index < limit)
4778 if (strncmp (search_string,
4780 search_string_index) == 0)
4789 /* At limit for direction? */
4790 if ((reverse && i < 0) ||
4791 (!reverse && i == hlen))
4796 index = strlen (sline);
4800 /* If the search string is longer than the current
4802 if (search_string_index > strlen (sline))
4805 /* Start actually searching. */
4807 index -= search_string_index;
4811 /* We cannot find the search string. Ding the bell. */
4813 i = last_found_line;
4817 /* We have found the search string. Just display it. But don't
4818 actually move there in the history list until the user accepts
4823 line_len = strlen (lines[i]);
4825 if (line_len >= rl_line_buffer_len)
4826 rl_extend_line_buffer (line_len);
4828 strcpy (the_line, lines[i]);
4831 last_found_line = i;
4833 (search_string, reverse, (i == orig_line) ? -1 : i);
4840 /* The searching is over. The user may have found the string that she
4841 was looking for, or else she may have exited a failing search. If
4842 INDEX is -1, then that shows that the string searched for was not
4843 found. We use this to determine where to place rl_point. */
4845 int now = last_found_line;
4847 /* First put back the original state. */
4848 strcpy (the_line, lines[orig_line]);
4850 if (now < orig_line)
4851 rl_get_previous_history (orig_line - now);
4853 rl_get_next_history (now - orig_line);
4855 /* If the index of the "matched" string is less than zero, then the
4856 final search string was never matched, so put point somewhere
4859 index = strlen (the_line);
4862 rl_clear_message ();
4866 /* Make C be the next command to be executed. */
4870 rl_pending_input = c;
4873 /* **************************************************************** */
4875 /* Killing Mechanism */
4877 /* **************************************************************** */
4879 /* What we assume for a max number of kills. */
4880 #define DEFAULT_MAX_KILLS 10
4882 /* The real variable to look at to find out when to flush kills. */
4883 int rl_max_kills = DEFAULT_MAX_KILLS;
4885 /* Where to store killed text. */
4886 char **rl_kill_ring = (char **)NULL;
4888 /* Where we are in the kill ring. */
4889 int rl_kill_index = 0;
4891 /* How many slots we have in the kill ring. */
4892 int rl_kill_ring_length = 0;
4894 /* How to say that you only want to save a certain amount
4895 of kill material. */
4896 rl_set_retained_kills (num)
4900 /* The way to kill something. This appends or prepends to the last
4901 kill, if the last command was a kill command. if FROM is less
4902 than TO, then the text is appended, otherwise prepended. If the
4903 last command was not a kill command, then a new slot is made for
4905 rl_kill_text (from, to)
4909 char *text = rl_copy (from, to);
4911 /* Is there anything to kill? */
4915 last_command_was_kill++;
4919 /* Delete the copied text from the line. */
4920 rl_delete_text (from, to);
4922 /* First, find the slot to work with. */
4923 if (!last_command_was_kill)
4925 /* Get a new slot. */
4928 /* If we don't have any defined, then make one. */
4929 rl_kill_ring = (char **)
4930 xmalloc (((rl_kill_ring_length = 1) + 1) * sizeof (char *));
4935 /* We have to add a new slot on the end, unless we have
4936 exceeded the max limit for remembering kills. */
4937 slot = rl_kill_ring_length;
4938 if (slot == rl_max_kills)
4941 free (rl_kill_ring[0]);
4942 for (i = 0; i < slot; i++)
4943 rl_kill_ring[i] = rl_kill_ring[i + 1];
4949 xrealloc (rl_kill_ring,
4950 ((slot = (rl_kill_ring_length += 1)) + 1)
4958 slot = rl_kill_ring_length - 1;
4961 /* If the last command was a kill, prepend or append. */
4962 if (last_command_was_kill && rl_editing_mode != vi_mode)
4964 char *old = rl_kill_ring[slot];
4965 char *new = (char *)xmalloc (1 + strlen (old) + strlen (text));
4979 rl_kill_ring[slot] = new;
4983 rl_kill_ring[slot] = text;
4985 rl_kill_index = slot;
4986 last_command_was_kill++;
4989 /* Now REMEMBER! In order to do prepending or appending correctly, kill
4990 commands always make rl_point's original position be the FROM argument,
4991 and rl_point's extent be the TO argument. */
4993 /* **************************************************************** */
4995 /* Killing Commands */
4997 /* **************************************************************** */
4999 /* Delete the word at point, saving the text in the kill ring. */
5000 rl_kill_word (count)
5003 int orig_point = rl_point;
5006 rl_backward_kill_word (-count);
5009 rl_forward_word (count);
5011 if (rl_point != orig_point)
5012 rl_kill_text (orig_point, rl_point);
5014 rl_point = orig_point;
5018 /* Rubout the word before point, placing it on the kill ring. */
5019 rl_backward_kill_word (count)
5022 int orig_point = rl_point;
5025 rl_kill_word (-count);
5028 rl_backward_word (count);
5030 if (rl_point != orig_point)
5031 rl_kill_text (orig_point, rl_point);
5035 /* Kill from here to the end of the line. If DIRECTION is negative, kill
5036 back to the line start instead. */
5037 rl_kill_line (direction)
5040 int orig_point = rl_point;
5043 rl_backward_kill_line (1);
5047 if (orig_point != rl_point)
5048 rl_kill_text (orig_point, rl_point);
5049 rl_point = orig_point;
5053 /* Kill backwards to the start of the line. If DIRECTION is negative, kill
5054 forwards to the line end instead. */
5055 rl_backward_kill_line (direction)
5058 int orig_point = rl_point;
5069 rl_kill_text (orig_point, rl_point);
5074 /* Yank back the last killed text. This ignores arguments. */
5077 if (!rl_kill_ring) rl_abort ();
5078 rl_insert_text (rl_kill_ring[rl_kill_index]);
5081 /* If the last command was yank, or yank_pop, and the text just
5082 before point is identical to the current kill item, then
5083 delete that text from the line, rotate the index down, and
5084 yank back some other text. */
5089 if (((rl_last_func != rl_yank_pop) && (rl_last_func != rl_yank)) ||
5095 l = strlen (rl_kill_ring[rl_kill_index]);
5096 if (((rl_point - l) >= 0) &&
5097 (strncmp (the_line + (rl_point - l),
5098 rl_kill_ring[rl_kill_index], l) == 0))
5100 rl_delete_text ((rl_point - l), rl_point);
5103 if (rl_kill_index < 0)
5104 rl_kill_index = rl_kill_ring_length - 1;
5112 /* Yank the COUNTth argument from the previous history line. */
5113 rl_yank_nth_arg (count, ignore)
5116 register HIST_ENTRY *entry = previous_history ();
5127 arg = history_arg_extract (count, count, entry->line);
5134 rl_begin_undo_group ();
5136 #if defined (VI_MODE)
5137 /* Vi mode always inserts a space befoe yanking the argument, and it
5138 inserts it right *after* rl_point. */
5139 if (rl_editing_mode == vi_mode)
5141 #endif /* VI_MODE */
5143 if (rl_point && the_line[rl_point - 1] != ' ')
5144 rl_insert_text (" ");
5146 rl_insert_text (arg);
5149 rl_end_undo_group ();
5152 /* How to toggle back and forth between editing modes. */
5153 rl_vi_editing_mode ()
5155 #if defined (VI_MODE)
5156 rl_editing_mode = vi_mode;
5157 rl_vi_insertion_mode ();
5158 #endif /* VI_MODE */
5161 rl_emacs_editing_mode ()
5163 rl_editing_mode = emacs_mode;
5164 keymap = emacs_standard_keymap;
5168 /* **************************************************************** */
5172 /* **************************************************************** */
5174 /* Non-zero means that case is not significant in completion. */
5175 int completion_case_fold = 0;
5177 /* Return an array of (char *) which is a list of completions for TEXT.
5178 If there are no completions, return a NULL pointer.
5179 The first entry in the returned array is the substitution for TEXT.
5180 The remaining entries are the possible completions.
5181 The array is terminated with a NULL pointer.
5183 ENTRY_FUNCTION is a function of two args, and returns a (char *).
5184 The first argument is TEXT.
5185 The second is a state argument; it should be zero on the first call, and
5186 non-zero on subsequent calls. It returns a NULL pointer to the caller
5187 when there are no more matches.
5190 completion_matches (text, entry_function)
5192 char *(*entry_function) ();
5194 /* Number of slots in match_list. */
5195 int match_list_size;
5197 /* The list of matches. */
5199 (char **)xmalloc (((match_list_size = 10) + 1) * sizeof (char *));
5201 /* Number of matches actually found. */
5204 /* Temporary string binder. */
5207 match_list[1] = (char *)NULL;
5209 while (string = (*entry_function) (text, matches))
5211 if (matches + 1 == match_list_size)
5212 match_list = (char **)xrealloc
5213 (match_list, ((match_list_size += 10) + 1) * sizeof (char *));
5215 match_list[++matches] = string;
5216 match_list[matches + 1] = (char *)NULL;
5219 /* If there were any matches, then look through them finding out the
5220 lowest common denominator. That then becomes match_list[0]. */
5224 int low = 100000; /* Count of max-matched characters. */
5226 /* If only one match, just use that. */
5229 match_list[0] = match_list[1];
5230 match_list[1] = (char *)NULL;
5234 /* Otherwise, compare each member of the list with
5235 the next, finding out where they stop matching. */
5239 register int c1, c2, si;
5241 if (completion_case_fold)
5244 (c1 = to_lower(match_list[i][si])) &&
5245 (c2 = to_lower(match_list[i + 1][si]));
5247 if (c1 != c2) break;
5252 (c1 = match_list[i][si]) &&
5253 (c2 = match_list[i + 1][si]);
5255 if (c1 != c2) break;
5258 if (low > si) low = si;
5261 match_list[0] = (char *)xmalloc (low + 1);
5262 strncpy (match_list[0], match_list[1], low);
5263 match_list[0][low] = '\0';
5266 else /* There were no matches. */
5269 match_list = (char **)NULL;
5271 return (match_list);
5274 /* Okay, now we write the entry_function for filename completion. In the
5275 general case. Note that completion in the shell is a little different
5276 because of all the pathnames that must be followed when looking up the
5277 completion for a command. */
5279 filename_completion_function (text, state)
5283 static DIR *directory;
5284 static char *filename = (char *)NULL;
5285 static char *dirname = (char *)NULL;
5286 static char *users_dirname = (char *)NULL;
5287 static int filename_len;
5289 dirent *entry = (dirent *)NULL;
5291 /* If we don't have any state, then do some initialization. */
5296 if (dirname) free (dirname);
5297 if (filename) free (filename);
5298 if (users_dirname) free (users_dirname);
5300 filename = savestring (text);
5301 if (!*text) text = ".";
5302 dirname = savestring (text);
5304 temp = rindex (dirname, '/');
5308 strcpy (filename, ++temp);
5312 strcpy (dirname, ".");
5314 /* We aren't done yet. We also support the "~user" syntax. */
5316 /* Save the version of the directory that the user typed. */
5317 users_dirname = savestring (dirname);
5321 temp_dirname = tilde_expand (dirname);
5323 dirname = temp_dirname;
5325 if (rl_symbolic_link_hook)
5326 (*rl_symbolic_link_hook) (&dirname);
5328 directory = opendir (dirname);
5329 filename_len = strlen (filename);
5331 rl_filename_completion_desired = 1;
5334 /* At this point we should entertain the possibility of hacking wildcarded
5335 filenames, like /usr/man/man<WILD>/te<TAB>. If the directory name
5336 contains globbing characters, then build an array of directories to
5337 glob on, and glob on the first one. */
5339 /* Now that we have some state, we can read the directory. */
5341 while (directory && (entry = readdir (directory)))
5343 /* Special case for no filename.
5344 All entries except "." and ".." match. */
5347 if ((strcmp (entry->d_name, ".") != 0) &&
5348 (strcmp (entry->d_name, "..") != 0))
5353 /* Otherwise, if these match upto the length of filename, then
5355 if (entry->d_name[0] == filename[0] && /* Quick test */
5356 (strncmp (filename, entry->d_name, filename_len) == 0))
5367 closedir (directory);
5368 directory = (DIR *)NULL;
5370 return (char *)NULL;
5376 if (dirname && (strcmp (dirname, ".") != 0))
5379 xmalloc (1 + strlen (users_dirname) + strlen (entry->d_name));
5380 strcpy (temp, users_dirname);
5381 strcat (temp, entry->d_name);
5385 temp = (savestring (entry->d_name));
5392 /* **************************************************************** */
5396 /* **************************************************************** */
5398 /* rl_add_defun (char *name, Function *function, int key)
5399 Add NAME to the list of named functions. Make FUNCTION
5400 be the function that gets called.
5401 If KEY is not -1, then bind it. */
5402 rl_add_defun (name, function, key)
5408 rl_bind_key (key, function);
5409 rl_add_funmap_entry (name, function);
5412 /* Bind KEY to FUNCTION. Returns non-zero if KEY is out of range. */
5414 rl_bind_key (key, function)
5421 if (key > 127 && key < 256)
5423 if (keymap[ESC].type == ISKMAP)
5425 Keymap escmap = (Keymap)keymap[ESC].function;
5428 escmap[key].type = ISFUNC;
5429 escmap[key].function = function;
5435 keymap[key].type = ISFUNC;
5436 keymap[key].function = function;
5440 /* Bind KEY to FUNCTION in MAP. Returns non-zero in case of invalid
5443 rl_bind_key_in_map (key, function, map)
5449 Keymap oldmap = keymap;
5452 result = rl_bind_key (key, function);
5457 /* Make KEY do nothing in the currently selected keymap.
5458 Returns non-zero in case of error. */
5463 return (rl_bind_key (key, (Function *)NULL));
5466 /* Make KEY do nothing in MAP.
5467 Returns non-zero in case of error. */
5469 rl_unbind_key_in_map (key, map)
5473 return (rl_bind_key_in_map (key, (Function *)NULL, map));
5476 /* Bind the key sequence represented by the string KEYSEQ to
5477 FUNCTION. This makes new keymaps as necessary. The initial
5478 place to do bindings is in MAP. */
5479 rl_set_key (keyseq, function, map)
5484 rl_generic_bind (ISFUNC, keyseq, function, map);
5487 /* Bind the key sequence represented by the string KEYSEQ to
5488 the string of characters MACRO. This makes new keymaps as
5489 necessary. The initial place to do bindings is in MAP. */
5490 rl_macro_bind (keyseq, macro, map)
5491 char *keyseq, *macro;
5497 macro_keys = (char *)xmalloc ((2 * strlen (macro)) + 1);
5499 if (rl_translate_keyseq (macro, macro_keys, ¯o_keys_len))
5504 rl_generic_bind (ISMACR, keyseq, macro_keys, map);
5507 /* Bind the key sequence represented by the string KEYSEQ to
5508 the arbitrary pointer DATA. TYPE says what kind of data is
5509 pointed to by DATA, right now this can be a function (ISFUNC),
5510 a macro (ISMACR), or a keymap (ISKMAP). This makes new keymaps
5511 as necessary. The initial place to do bindings is in MAP. */
5512 rl_generic_bind (type, keyseq, data, map)
5514 char *keyseq, *data;
5521 /* If no keys to bind to, exit right away. */
5522 if (!keyseq || !*keyseq)
5529 keys = (char *)alloca (1 + (2 * strlen (keyseq)));
5531 /* Translate the ASCII representation of KEYSEQ into an array
5532 of characters. Stuff the characters into ARRAY, and the
5533 length of ARRAY into LENGTH. */
5534 if (rl_translate_keyseq (keyseq, keys, &keys_len))
5537 /* Bind keys, making new keymaps as necessary. */
5538 for (i = 0; i < keys_len; i++)
5540 if (i + 1 < keys_len)
5542 if (map[keys[i]].type != ISKMAP)
5544 if (map[i].type == ISMACR)
5545 free ((char *)map[i].function);
5547 map[keys[i]].type = ISKMAP;
5548 map[keys[i]].function = (Function *)rl_make_bare_keymap ();
5550 map = (Keymap)map[keys[i]].function;
5554 if (map[keys[i]].type == ISMACR)
5555 free ((char *)map[keys[i]].function);
5557 map[keys[i]].function = (Function *)data;
5558 map[keys[i]].type = type;
5563 /* Translate the ASCII representation of SEQ, stuffing the
5564 values into ARRAY, an array of characters. LEN gets the
5565 final length of ARRAY. Return non-zero if there was an
5566 error parsing SEQ. */
5567 rl_translate_keyseq (seq, array, len)
5571 register int i, c, l = 0;
5573 for (i = 0; c = seq[i]; i++)
5582 if (((c == 'C' || c == 'M') && seq[i + 1] == '-') ||
5585 /* Handle special case of backwards define. */
5586 if (strncmp (&seq[i], "C-\\M-", 5) == 0)
5590 array[l++] = CTRL (to_upper (seq[i]));
5605 /* Special hack for C-?... */
5607 array[l++] = RUBOUT;
5609 array[l++] = CTRL (to_upper (seq[i]));
5627 /* Return a pointer to the function that STRING represents.
5628 If STRING doesn't have a matching function, then a NULL pointer
5631 rl_named_function (string)
5636 for (i = 0; funmap[i]; i++)
5637 if (stricmp (funmap[i]->name, string) == 0)
5638 return (funmap[i]->function);
5639 return ((Function *)NULL);
5642 /* The last key bindings file read. */
5644 /* Don't know what to do, but this is a guess */
5645 static char *last_readline_init_file = "/INPUTRC";
5647 static char *last_readline_init_file = "~/inputrc";
5650 /* Re-read the current keybindings file. */
5651 rl_re_read_init_file (count, ignore)
5654 rl_read_init_file ((char *)NULL);
5657 /* Do key bindings from a file. If FILENAME is NULL it defaults
5658 to `~/.inputrc'. If the file existed and could be opened and
5659 read, 0 is returned, otherwise errno is returned. */
5661 rl_read_init_file (filename)
5665 char *buffer, *openname, *line, *end;
5669 /* Default the filename. */
5671 filename = last_readline_init_file;
5673 openname = tilde_expand (filename);
5675 if (!openname || *openname == '\000')
5678 if ((stat (openname, &finfo) < 0) ||
5679 (file = open (openname, O_RDONLY, 0666)) < 0)
5687 last_readline_init_file = filename;
5689 /* Read the file into BUFFER. */
5690 buffer = (char *)xmalloc (finfo.st_size + 1);
5691 i = read (file, buffer, finfo.st_size);
5694 if (i != finfo.st_size)
5697 /* Loop over the lines in the file. Lines that start with `#' are
5698 comments; all other lines are commands for readline initialization. */
5700 end = buffer + finfo.st_size;
5703 /* Find the end of this line. */
5704 for (i = 0; line + i != end && line[i] != '\n'; i++);
5706 /* Mark end of line. */
5709 /* If the line is not a comment, then parse it. */
5711 rl_parse_and_bind (line);
5713 /* Move to the next line. */
5719 /* **************************************************************** */
5721 /* Parser Directives */
5723 /* **************************************************************** */
5727 /* Calling programs set this to have their argv[0]. */
5728 char *rl_readline_name = "other";
5730 /* Stack of previous values of parsing_conditionalized_out. */
5731 static unsigned char *if_stack = (unsigned char *)NULL;
5732 static int if_stack_depth = 0;
5733 static int if_stack_size = 0;
5735 /* Push parsing_conditionalized_out, and set parser state based on ARGS. */
5741 /* Push parser state. */
5742 if (if_stack_depth + 1 >= if_stack_size)
5745 if_stack = (unsigned char *)xmalloc (if_stack_size = 20);
5747 if_stack = (unsigned char *)xrealloc (if_stack, if_stack_size += 20);
5749 if_stack[if_stack_depth++] = parsing_conditionalized_out;
5751 /* If parsing is turned off, then nothing can turn it back on except
5752 for finding the matching endif. In that case, return right now. */
5753 if (parsing_conditionalized_out)
5756 /* Isolate first argument. */
5757 for (i = 0; args[i] && !whitespace (args[i]); i++);
5762 /* Handle "if term=foo" and "if mode=emacs" constructs. If this
5763 isn't term=foo, or mode=emacs, then check to see if the first
5764 word in ARGS is the same as the value stored in rl_readline_name. */
5765 if (rl_terminal_name && strnicmp (args, "term=", 5) == 0)
5769 /* Terminals like "aaa-60" are equivalent to "aaa". */
5770 tname = savestring (rl_terminal_name);
5771 tem = rindex (tname, '-');
5775 if (stricmp (args + 5, tname) == 0)
5776 parsing_conditionalized_out = 0;
5778 parsing_conditionalized_out = 1;
5780 #if defined (VI_MODE)
5781 else if (strnicmp (args, "mode=", 5) == 0)
5785 if (stricmp (args + 5, "emacs") == 0)
5787 else if (stricmp (args + 5, "vi") == 0)
5792 if (mode == rl_editing_mode)
5793 parsing_conditionalized_out = 0;
5795 parsing_conditionalized_out = 1;
5797 #endif /* VI_MODE */
5798 /* Check to see if the first word in ARGS is the same as the
5799 value stored in rl_readline_name. */
5800 else if (stricmp (args, rl_readline_name) == 0)
5801 parsing_conditionalized_out = 0;
5803 parsing_conditionalized_out = 1;
5806 /* Invert the current parser state if there is anything on the stack. */
5812 if (!if_stack_depth)
5814 /* Error message? */
5818 /* Check the previous (n - 1) levels of the stack to make sure that
5819 we haven't previously turned off parsing. */
5820 for (i = 0; i < if_stack_depth - 1; i++)
5821 if (if_stack[i] == 1)
5824 /* Invert the state of parsing if at top level. */
5825 parsing_conditionalized_out = !parsing_conditionalized_out;
5828 /* Terminate a conditional, popping the value of
5829 parsing_conditionalized_out from the stack. */
5834 parsing_conditionalized_out = if_stack[--if_stack_depth];
5837 /* *** What, no error message? *** */
5841 /* Associate textual names with actual functions. */
5845 } parser_directives [] = {
5846 { "if", parser_if },
5847 { "endif", parser_endif },
5848 { "else", parser_else },
5849 { (char *)0x0, (Function *)0x0 }
5852 /* Handle a parser directive. STATEMENT is the line of the directive
5853 without any leading `$'. */
5855 handle_parser_directive (statement)
5859 char *directive, *args;
5861 /* Isolate the actual directive. */
5863 /* Skip whitespace. */
5864 for (i = 0; whitespace (statement[i]); i++);
5866 directive = &statement[i];
5868 for (; statement[i] && !whitespace (statement[i]); i++);
5871 statement[i++] = '\0';
5873 for (; statement[i] && whitespace (statement[i]); i++);
5875 args = &statement[i];
5877 /* Lookup the command, and act on it. */
5878 for (i = 0; parser_directives[i].name; i++)
5879 if (stricmp (directive, parser_directives[i].name) == 0)
5881 (*parser_directives[i].function) (args);
5885 /* *** Should an error message be output? */
5889 /* Ugly but working hack for binding prefix meta. */
5890 #define PREFIX_META_HACK
5892 static int substring_member_of_array ();
5894 /* Read the binding command from STRING and perform it.
5895 A key binding command looks like: Keyname: function-name\0,
5896 a variable binding command looks like: set variable value.
5897 A new-style keybinding looks like "\C-x\C-x": exchange-point-and-mark. */
5898 rl_parse_and_bind (string)
5901 extern char *possible_control_prefixes[], *possible_meta_prefixes[];
5902 char *funname, *kname;
5906 while (string && whitespace (*string))
5909 if (!string || !*string || *string == '#')
5912 /* If this is a parser directive, act on it. */
5915 handle_parser_directive (&string[1]);
5919 /* If we are supposed to be skipping parsing right now, then do it. */
5920 if (parsing_conditionalized_out)
5924 /* If this keyname is a complex key expression surrounded by quotes,
5925 advance to after the matching close quote. */
5928 for (i = 1; c = string[i]; i++)
5930 if (c == '"' && string[i - 1] != '\\')
5935 /* Advance to the colon (:) or whitespace which separates the two objects. */
5936 for (; (c = string[i]) && c != ':' && c != ' ' && c != '\t'; i++ );
5938 /* Mark the end of the command (or keyname). */
5942 /* If this is a command to set a variable, then do that. */
5943 if (stricmp (string, "set") == 0)
5945 char *var = string + i;
5948 /* Make VAR point to start of variable name. */
5949 while (*var && whitespace (*var)) var++;
5951 /* Make value point to start of value string. */
5953 while (*value && !whitespace (*value)) value++;
5956 while (*value && whitespace (*value)) value++;
5958 rl_variable_bind (var, value);
5962 /* Skip any whitespace between keyname and funname. */
5963 for (; string[i] && whitespace (string[i]); i++);
5964 funname = &string[i];
5966 /* Now isolate funname.
5967 For straight function names just look for whitespace, since
5968 that will signify the end of the string. But this could be a
5969 macro definition. In that case, the string is quoted, so skip
5970 to the matching delimiter. */
5971 if (*funname == '\'' || *funname == '"')
5973 int delimiter = string[i++];
5975 for (; c = string[i]; i++)
5977 if (c == delimiter && string[i - 1] != '\\')
5984 /* Advance to the end of the string. */
5985 for (; string[i] && !whitespace (string[i]); i++);
5987 /* No extra whitespace at the end of the string. */
5990 /* If this is a new-style key-binding, then do the binding with
5991 rl_set_key (). Otherwise, let the older code deal with it. */
5994 char *seq = (char *)alloca (1 + strlen (string));
5995 register int j, k = 0;
5997 for (j = 1; string[j]; j++)
5999 if (string[j] == '"' && string[j - 1] != '\\')
6002 seq[k++] = string[j];
6006 /* Binding macro? */
6007 if (*funname == '\'' || *funname == '"')
6009 j = strlen (funname);
6011 if (j && funname[j - 1] == *funname)
6012 funname[j - 1] = '\0';
6014 rl_macro_bind (seq, &funname[1], keymap);
6017 rl_set_key (seq, rl_named_function (funname), keymap);
6022 /* Get the actual character we want to deal with. */
6023 kname = rindex (string, '-');
6029 key = glean_key_from_name (kname);
6031 /* Add in control and meta bits. */
6032 if (substring_member_of_array (string, possible_control_prefixes))
6033 key = CTRL (to_upper (key));
6035 if (substring_member_of_array (string, possible_meta_prefixes))
6038 /* Temporary. Handle old-style keyname with macro-binding. */
6039 if (*funname == '\'' || *funname == '"')
6042 int fl = strlen (funname);
6044 seq[0] = key; seq[1] = '\0';
6045 if (fl && funname[fl - 1] == *funname)
6046 funname[fl - 1] = '\0';
6048 rl_macro_bind (seq, &funname[1], keymap);
6050 #if defined (PREFIX_META_HACK)
6051 /* Ugly, but working hack to keep prefix-meta around. */
6052 else if (stricmp (funname, "prefix-meta") == 0)
6058 rl_generic_bind (ISKMAP, seq, (char *)emacs_meta_keymap, keymap);
6060 #endif /* PREFIX_META_HACK */
6062 rl_bind_key (key, rl_named_function (funname));
6065 rl_variable_bind (name, value)
6068 if (stricmp (name, "editing-mode") == 0)
6070 if (strnicmp (value, "vi", 2) == 0)
6072 #if defined (VI_MODE)
6073 keymap = vi_insertion_keymap;
6074 rl_editing_mode = vi_mode;
6076 #if defined (NOTDEF)
6077 /* What state is the terminal in? I'll tell you:
6078 non-determinate! That means we cannot do any output. */
6081 #endif /* VI_MODE */
6083 else if (strnicmp (value, "emacs", 5) == 0)
6085 keymap = emacs_standard_keymap;
6086 rl_editing_mode = emacs_mode;
6089 else if (stricmp (name, "horizontal-scroll-mode") == 0)
6091 if (!*value || stricmp (value, "On") == 0)
6092 horizontal_scroll_mode = 1;
6094 horizontal_scroll_mode = 0;
6096 else if (stricmp (name, "mark-modified-lines") == 0)
6098 if (!*value || stricmp (value, "On") == 0)
6099 mark_modified_lines = 1;
6101 mark_modified_lines = 0;
6103 else if (stricmp (name, "prefer-visible-bell") == 0)
6105 if (!*value || stricmp (value, "On") == 0)
6106 prefer_visible_bell = 1;
6108 prefer_visible_bell = 0;
6110 else if (stricmp (name, "comment-begin") == 0)
6112 #if defined (VI_MODE)
6113 extern char *rl_vi_comment_begin;
6117 if (rl_vi_comment_begin)
6118 free (rl_vi_comment_begin);
6120 rl_vi_comment_begin = savestring (value);
6122 #endif /* VI_MODE */
6126 /* Return the character which matches NAME.
6127 For example, `Space' returns ' '. */
6134 assoc_list name_key_alist[] = {
6137 { "Escape", '\033' },
6139 { "Newline", '\n' },
6150 glean_key_from_name (name)
6155 for (i = 0; name_key_alist[i].name; i++)
6156 if (stricmp (name, name_key_alist[i].name) == 0)
6157 return (name_key_alist[i].value);
6163 /* **************************************************************** */
6165 /* Key Binding and Function Information */
6167 /* **************************************************************** */
6169 /* Each of the following functions produces information about the
6170 state of keybindings and functions known to Readline. The info
6171 is always printed to rl_outstream, and in such a way that it can
6172 be read back in (i.e., passed to rl_parse_and_bind (). */
6174 /* Print the names of functions known to Readline. */
6176 rl_list_funmap_names (ignore)
6180 char **funmap_names;
6181 extern char **rl_funmap_names ();
6183 funmap_names = rl_funmap_names ();
6188 for (i = 0; funmap_names[i]; i++)
6189 fprintf (rl_outstream, "%s\n", funmap_names[i]);
6191 free (funmap_names);
6194 /* Return a NULL terminated array of strings which represent the key
6195 sequences that are used to invoke FUNCTION in MAP. */
6197 invoking_keyseqs_in_map (function, map)
6203 int result_index, result_size;
6205 result = (char **)NULL;
6206 result_index = result_size = 0;
6208 for (key = 0; key < 128; key++)
6210 switch (map[key].type)
6213 /* Macros match, if, and only if, the pointers are identical.
6214 Thus, they are treated exactly like functions in here. */
6216 /* If the function in the keymap is the one we are looking for,
6217 then add the current KEY to the list of invoking keys. */
6218 if (map[key].function == function)
6220 char *keyname = (char *)xmalloc (5);
6223 sprintf (keyname, "\\C-%c", to_lower (UNCTRL (key)));
6224 else if (key == RUBOUT)
6225 sprintf (keyname, "\\C-?");
6227 sprintf (keyname, "%c", key);
6229 if (result_index + 2 > result_size)
6232 result = (char **) xmalloc
6233 ((result_size = 10) * sizeof (char *));
6235 result = (char **) xrealloc
6236 (result, (result_size += 10) * sizeof (char *));
6239 result[result_index++] = keyname;
6240 result[result_index] = (char *)NULL;
6246 char **seqs = (char **)NULL;
6248 /* Find the list of keyseqs in this map which have FUNCTION as
6249 their target. Add the key sequences found to RESULT. */
6250 if (map[key].function)
6252 invoking_keyseqs_in_map (function, (Keymap)map[key].function);
6258 for (i = 0; seqs[i]; i++)
6260 char *keyname = (char *)xmalloc (6 + strlen (seqs[i]));
6263 sprintf (keyname, "\\e");
6264 else if (CTRL_P (key))
6265 sprintf (keyname, "\\C-%c", to_lower (UNCTRL (key)));
6266 else if (key == RUBOUT)
6267 sprintf (keyname, "\\C-?");
6269 sprintf (keyname, "%c", key);
6271 strcat (keyname, seqs[i]);
6273 if (result_index + 2 > result_size)
6277 xmalloc ((result_size = 10) * sizeof (char *));
6281 (result_size += 10) * sizeof (char *));
6284 result[result_index++] = keyname;
6285 result[result_index] = (char *)NULL;
6295 /* Return a NULL terminated array of strings which represent the key
6296 sequences that can be used to invoke FUNCTION using the current keymap. */
6298 rl_invoking_keyseqs (function)
6301 return (invoking_keyseqs_in_map (function, keymap));
6304 /* Print all of the current functions and their bindings to
6305 rl_outstream. If an explicit argument is given, then print
6306 the output in such a way that it can be read back in. */
6308 rl_dump_functions (count)
6311 void rl_function_dumper ();
6313 rl_function_dumper (rl_explicit_arg);
6318 /* Print all of the functions and their bindings to rl_outstream. If
6319 PRINT_READABLY is non-zero, then print the output in such a way
6320 that it can be read back in. */
6322 rl_function_dumper (print_readably)
6326 char **rl_funmap_names (), **names;
6329 names = rl_funmap_names ();
6331 fprintf (rl_outstream, "\n");
6333 for (i = 0; name = names[i]; i++)
6338 function = rl_named_function (name);
6339 invokers = invoking_keyseqs_in_map (function, keymap);
6344 fprintf (rl_outstream, "# %s (not bound)\n", name);
6349 for (j = 0; invokers[j]; j++)
6351 fprintf (rl_outstream, "\"%s\": %s\n",
6362 fprintf (rl_outstream, "%s is not bound to any keys\n",
6368 fprintf (rl_outstream, "%s can be found on ", name);
6370 for (j = 0; invokers[j] && j < 5; j++)
6372 fprintf (rl_outstream, "\"%s\"%s", invokers[j],
6373 invokers[j + 1] ? ", " : ".\n");
6376 if (j == 5 && invokers[j])
6377 fprintf (rl_outstream, "...\n");
6379 for (j = 0; invokers[j]; j++)
6389 /* **************************************************************** */
6391 /* String Utility Functions */
6393 /* **************************************************************** */
6395 static char *strindex ();
6397 /* Return pointer to first occurance in STRING1 of any character from STRING2,
6398 or NULL if no occurance found. */
6400 strpbrk (string1, string2)
6401 char *string1, *string2;
6403 register char *scan;
6405 for (; *string1 != '\0'; string1++)
6407 for (scan = string2; *scan != '\0'; scan++)
6409 if (*string1 == *scan)
6418 /* Return non-zero if any members of ARRAY are a substring in STRING. */
6420 substring_member_of_array (string, array)
6421 char *string, **array;
6425 if (strindex (string, *array))
6432 /* Whoops, Unix doesn't have strnicmp. */
6434 /* Compare at most COUNT characters from string1 to string2. Case
6437 strnicmp (string1, string2, count)
6438 char *string1, *string2;
6440 register char ch1, ch2;
6446 if (to_upper(ch1) == to_upper(ch2))
6453 /* strcmp (), but caseless. */
6455 stricmp (string1, string2)
6456 char *string1, *string2;
6458 register char ch1, ch2;
6460 while (*string1 && *string2)
6464 if (to_upper(ch1) != to_upper(ch2))
6467 return (*string1 | *string2);
6470 /* Determine if s2 occurs in s1. If so, return a pointer to the
6471 match in s1. The compare is case insensitive. */
6474 register char *s1, *s2;
6476 register int i, l = strlen (s2);
6477 register int len = strlen (s1);
6479 for (i = 0; (len - i) >= l; i++)
6480 if (strnicmp (&s1[i], s2, l) == 0)
6482 return ((char *)NULL);
6486 /* **************************************************************** */
6488 /* USG (System V) Support */
6490 /* **************************************************************** */
6492 /* When compiling and running in the `Posix' environment, Ultrix does
6493 not restart system calls, so this needs to do it. */
6504 #endif /* __GO32__ */
6508 result = read (fileno (stream), &c, sizeof (char));
6510 if (result == sizeof (char))
6513 /* If zero characters are returned, then the file that we are
6514 reading from is empty! Return EOF in that case. */
6519 /* If the error that we received was SIGINT, then try again,
6520 this is simply an interrupted system call to read ().
6521 Otherwise, some error ocurred, also signifying EOF. */
6524 #endif /* !__GO32__ */
6528 #if defined (STATIC_MALLOC)
6530 /* **************************************************************** */
6532 /* xmalloc and xrealloc () */
6534 /* **************************************************************** */
6536 static void memory_error_and_abort ();
6542 char *temp = (char *)malloc (bytes);
6545 memory_error_and_abort ();
6550 xrealloc (pointer, bytes)
6557 temp = (char *)malloc (bytes);
6559 temp = (char *)realloc (pointer, bytes);
6562 memory_error_and_abort ();
6568 memory_error_and_abort ()
6570 fprintf (stderr, "readline: Out of virtual memory!\n");
6573 #endif /* STATIC_MALLOC */
6576 /* **************************************************************** */
6578 /* Testing Readline */
6580 /* **************************************************************** */
6586 HIST_ENTRY **history_list ();
6587 char *temp = (char *)NULL;
6588 char *prompt = "readline% ";
6593 temp = readline (prompt);
6599 /* If there is anything on the line, print it and remember it. */
6602 fprintf (stderr, "%s\r\n", temp);
6606 /* Check for `command' that we handle. */
6607 if (strcmp (temp, "quit") == 0)
6610 if (strcmp (temp, "list") == 0)
6612 HIST_ENTRY **list = history_list ();
6616 for (i = 0; list[i]; i++)
6618 fprintf (stderr, "%d: %s\r\n", i, list[i]->line);
6619 free (list[i]->line);
6633 * compile-command: "gcc -g -traditional -I. -I.. -DTEST -o readline readline.c keymaps.o funmap.o history.o -ltermcap"