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. */
1971 /* On Solaris2, sys/types.h brings in sys/reg.h,
1972 which screws up the Termcap variable PC, used below. */
1979 /* Some strings to control terminal actions. These are output by tputs (). */
1980 char *term_goto, *term_clreol, *term_cr, *term_clrpag, *term_backspace;
1982 int screenwidth, screenheight;
1984 /* Non-zero if we determine that the terminal can do character insertion. */
1985 int terminal_can_insert = 0;
1987 /* How to insert characters. */
1988 char *term_im, *term_ei, *term_ic, *term_ip, *term_IC;
1990 /* How to delete characters. */
1991 char *term_dc, *term_DC;
1993 #if defined (HACK_TERMCAP_MOTION)
1994 char *term_forward_char;
1995 #endif /* HACK_TERMCAP_MOTION */
1997 /* How to go up a line. */
2000 /* A visible bell, if the terminal can be made to flash the screen. */
2003 /* Re-initialize the terminal considering that the TERM/TERMCAP variable
2005 rl_reset_terminal (terminal_name)
2006 char *terminal_name;
2008 init_terminal_io (terminal_name);
2011 init_terminal_io (terminal_name)
2012 char *terminal_name;
2015 screenwidth = ScreenCols();
2016 screenheight = ScreenRows();
2018 term_im = term_ei = term_ic = term_IC = (char *)NULL;
2019 term_up = term_dc = term_DC = visible_bell = (char *)NULL;
2020 #if defined (HACK_TERMCAP_MOTION)
2021 term_forward_char = (char *)NULL;
2023 terminal_can_insert = 0;
2026 extern char *tgetstr ();
2027 char *term, *buffer;
2028 #if defined (TIOCGWINSZ)
2029 struct winsize window_size;
2033 term = terminal_name ? terminal_name : getenv ("TERM");
2035 if (!term_string_buffer)
2036 term_string_buffer = (char *)xmalloc (2048);
2039 term_buffer = (char *)xmalloc (2048);
2041 buffer = term_string_buffer;
2043 term_clrpag = term_cr = term_clreol = (char *)NULL;
2048 if (tgetent (term_buffer, term) < 0)
2054 term_im = term_ei = term_ic = term_IC = (char *)NULL;
2055 term_up = term_dc = term_DC = visible_bell = (char *)NULL;
2056 #if defined (HACK_TERMCAP_MOTION)
2057 term_forward_char = (char *)NULL;
2059 terminal_can_insert = 0;
2063 BC = tgetstr ("pc", &buffer);
2064 PC = buffer ? *buffer : 0;
2066 term_backspace = tgetstr ("le", &buffer);
2068 term_cr = tgetstr ("cr", &buffer);
2069 term_clreol = tgetstr ("ce", &buffer);
2070 term_clrpag = tgetstr ("cl", &buffer);
2075 #if defined (HACK_TERMCAP_MOTION)
2076 term_forward_char = tgetstr ("nd", &buffer);
2077 #endif /* HACK_TERMCAP_MOTION */
2080 tty = fileno (rl_instream);
2084 screenwidth = screenheight = 0;
2085 #if defined (TIOCGWINSZ)
2086 if (ioctl (tty, TIOCGWINSZ, &window_size) == 0)
2088 screenwidth = (int) window_size.ws_col;
2089 screenheight = (int) window_size.ws_row;
2093 if (screenwidth <= 0 || screenheight <= 0)
2095 screenwidth = tgetnum ("co");
2096 screenheight = tgetnum ("li");
2101 if (screenwidth <= 0)
2104 if (screenheight <= 0)
2107 term_im = tgetstr ("im", &buffer);
2108 term_ei = tgetstr ("ei", &buffer);
2109 term_IC = tgetstr ("IC", &buffer);
2110 term_ic = tgetstr ("ic", &buffer);
2112 /* "An application program can assume that the terminal can do
2113 character insertion if *any one of* the capabilities `IC',
2114 `im', `ic' or `ip' is provided." But we can't do anything if
2115 only `ip' is provided, so... */
2116 terminal_can_insert = (term_IC || term_im || term_ic);
2118 term_up = tgetstr ("up", &buffer);
2119 term_dc = tgetstr ("dc", &buffer);
2120 term_DC = tgetstr ("DC", &buffer);
2122 visible_bell = tgetstr ("vb", &buffer);
2123 #endif /* !__GO32__ */
2126 /* A function for the use of tputs () */
2128 output_character_function (c)
2131 putc (c, out_stream);
2134 /* Write COUNT characters from STRING to the output stream. */
2136 output_some_chars (string, count)
2140 fwrite (string, 1, count, out_stream);
2143 /* Delete COUNT characters from the display line. */
2145 delete_chars (count)
2150 ScreenGetCursor(&r, &c);
2152 memcpy(ScreenPrimary+r*w+c, ScreenPrimary+r*w+c+count, w-c-count);
2153 memset(ScreenPrimary+r*w+w-count, 0, count*2);
2154 #else /* __GO32__ */
2155 if (count > screenwidth)
2158 if (term_DC && *term_DC)
2160 char *tgoto (), *buffer;
2161 buffer = tgoto (term_DC, 0, count);
2162 tputs (buffer, 1, output_character_function);
2166 if (term_dc && *term_dc)
2168 tputs (term_dc, 1, output_character_function);
2170 #endif /* __GO32__ */
2173 /* Insert COUNT characters from STRING to the output stream. */
2175 insert_some_chars (string, count)
2181 ScreenGetCursor(&r, &c);
2183 memcpy(ScreenPrimary+r*w+c+count, ScreenPrimary+r*w+c, w-c-count);
2184 /* Print the text. */
2185 output_some_chars (string, count);
2186 #else /* __GO32__ */
2187 /* If IC is defined, then we do not have to "enter" insert mode. */
2190 char *tgoto (), *buffer;
2191 buffer = tgoto (term_IC, 0, count);
2192 tputs (buffer, 1, output_character_function);
2193 output_some_chars (string, count);
2199 /* If we have to turn on insert-mode, then do so. */
2200 if (term_im && *term_im)
2201 tputs (term_im, 1, output_character_function);
2203 /* If there is a special command for inserting characters, then
2204 use that first to open up the space. */
2205 if (term_ic && *term_ic)
2207 for (i = count; i--; )
2208 tputs (term_ic, 1, output_character_function);
2211 /* Print the text. */
2212 output_some_chars (string, count);
2214 /* If there is a string to turn off insert mode, we had best use
2216 if (term_ei && *term_ei)
2217 tputs (term_ei, 1, output_character_function);
2219 #endif /* __GO32__ */
2222 /* Move the cursor back. */
2230 for (i = 0; i < count; i++)
2231 tputs (term_backspace, 1, output_character_function);
2233 #endif /* !__GO32__ */
2234 for (i = 0; i < count; i++)
2235 putc ('\b', out_stream);
2238 /* Move to the start of the next line. */
2241 #if defined (NEW_TTY_DRIVER)
2242 tputs (term_cr, 1, output_character_function);
2243 #endif /* NEW_TTY_DRIVER */
2244 putc ('\n', out_stream);
2247 /* Clear to the end of the line. COUNT is the minimum
2248 number of character spaces to clear, */
2249 clear_to_eol (count)
2255 tputs (term_clreol, 1, output_character_function);
2258 #endif /* !__GO32__ */
2262 /* Do one more character space. */
2265 for (i = 0; i < count; i++)
2266 putc (' ', out_stream);
2273 /* **************************************************************** */
2275 /* Saving and Restoring the TTY */
2277 /* **************************************************************** */
2279 /* Non-zero means that the terminal is in a prepped state. */
2280 static int terminal_prepped = 0;
2282 #if defined (NEW_TTY_DRIVER)
2284 /* Standard flags, including ECHO. */
2285 static int original_tty_flags = 0;
2287 /* Local mode flags, like LPASS8. */
2288 static int local_mode_flags = 0;
2290 /* Terminal characters. This has C-s and C-q in it. */
2291 static struct tchars original_tchars;
2293 /* Local special characters. This has the interrupt characters in it. */
2294 #if defined (TIOCGLTC)
2295 static struct ltchars original_ltchars;
2298 /* We use this to get and set the tty_flags. */
2299 static struct sgttyb the_ttybuff;
2301 /* Put the terminal in CBREAK mode so that we can detect key presses. */
2306 int tty = fileno (rl_instream);
2307 #if defined (HAVE_BSD_SIGNALS)
2309 #endif /* HAVE_BSD_SIGNALS */
2311 if (terminal_prepped)
2314 oldmask = sigblock (sigmask (SIGINT));
2316 /* We always get the latest tty values. Maybe stty changed them. */
2317 ioctl (tty, TIOCGETP, &the_ttybuff);
2318 original_tty_flags = the_ttybuff.sg_flags;
2320 readline_echoing_p = (original_tty_flags & ECHO);
2322 #if defined (TIOCLGET)
2323 ioctl (tty, TIOCLGET, &local_mode_flags);
2327 # define ANYP (EVENP | ODDP)
2330 /* If this terminal doesn't care how the 8th bit is used,
2331 then we can use it for the meta-key. We check by seeing
2332 if BOTH odd and even parity are allowed. */
2333 if (the_ttybuff.sg_flags & ANYP)
2336 the_ttybuff.sg_flags |= PASS8;
2339 /* Hack on local mode flags if we can. */
2340 #if defined (TIOCLGET) && defined (LPASS8)
2343 flags = local_mode_flags | LPASS8;
2344 ioctl (tty, TIOCLSET, &flags);
2346 #endif /* TIOCLGET && LPASS8 */
2349 #if defined (TIOCGETC)
2353 ioctl (tty, TIOCGETC, &original_tchars);
2354 temp = original_tchars;
2356 #if defined (USE_XON_XOFF)
2357 /* Get rid of C-s and C-q.
2358 We remember the value of startc (C-q) so that if the terminal is in
2359 xoff state, the user can xon it by pressing that character. */
2360 xon_char = temp.t_startc;
2364 /* If there is an XON character, bind it to restart the output. */
2366 rl_bind_key (xon_char, rl_restart_output);
2367 #endif /* USE_XON_XOFF */
2369 /* If there is an EOF char, bind eof_char to it. */
2370 if (temp.t_eofc != -1)
2371 eof_char = temp.t_eofc;
2373 #if defined (NO_KILL_INTR)
2374 /* Get rid of C-\ and C-c. */
2375 temp.t_intrc = temp.t_quitc = -1;
2376 #endif /* NO_KILL_INTR */
2378 ioctl (tty, TIOCSETC, &temp);
2380 #endif /* TIOCGETC */
2382 #if defined (TIOCGLTC)
2384 struct ltchars temp;
2386 ioctl (tty, TIOCGLTC, &original_ltchars);
2387 temp = original_ltchars;
2389 /* Make the interrupt keys go away. Just enough to make people
2391 temp.t_dsuspc = -1; /* C-y */
2392 temp.t_lnextc = -1; /* C-v */
2394 ioctl (tty, TIOCSLTC, &temp);
2396 #endif /* TIOCGLTC */
2398 the_ttybuff.sg_flags &= ~(ECHO | CRMOD);
2399 the_ttybuff.sg_flags |= CBREAK;
2400 ioctl (tty, TIOCSETN, &the_ttybuff);
2402 terminal_prepped = 1;
2404 #if defined (HAVE_BSD_SIGNALS)
2405 sigsetmask (oldmask);
2407 #endif /* !__GO32__ */
2410 /* Restore the terminal to its original state. */
2412 rl_deprep_terminal ()
2415 int tty = fileno (rl_instream);
2416 #if defined (HAVE_BSD_SIGNALS)
2420 if (!terminal_prepped)
2423 oldmask = sigblock (sigmask (SIGINT));
2425 the_ttybuff.sg_flags = original_tty_flags;
2426 ioctl (tty, TIOCSETN, &the_ttybuff);
2427 readline_echoing_p = 1;
2429 #if defined (TIOCLGET)
2430 ioctl (tty, TIOCLSET, &local_mode_flags);
2433 #if defined (TIOCSLTC)
2434 ioctl (tty, TIOCSLTC, &original_ltchars);
2437 #if defined (TIOCSETC)
2438 ioctl (tty, TIOCSETC, &original_tchars);
2440 terminal_prepped = 0;
2442 #if defined (HAVE_BSD_SIGNALS)
2443 sigsetmask (oldmask);
2445 #endif /* !__GO32 */
2448 #else /* !defined (NEW_TTY_DRIVER) */
2454 #if !defined (VTIME)
2459 #if defined (TERMIOS_TTY_DRIVER)
2460 static struct termios otio;
2462 static struct termio otio;
2463 #endif /* !TERMIOS_TTY_DRIVER */
2464 #endif /* __GO32__ */
2470 int tty = fileno (rl_instream);
2471 #if defined (TERMIOS_TTY_DRIVER)
2475 #endif /* !TERMIOS_TTY_DRIVER */
2477 #if defined (HAVE_POSIX_SIGNALS)
2480 # if defined (HAVE_BSD_SIGNALS)
2482 # endif /* HAVE_BSD_SIGNALS */
2483 #endif /* !HAVE_POSIX_SIGNALS */
2485 if (terminal_prepped)
2488 /* Try to keep this function from being INTerrupted. We can do it
2489 on POSIX and systems with BSD-like signal handling. */
2490 #if defined (HAVE_POSIX_SIGNALS)
2492 sigaddset (&set, SIGINT);
2493 sigprocmask (SIG_BLOCK, &set, &oset);
2494 #else /* !HAVE_POSIX_SIGNALS */
2495 # if defined (HAVE_BSD_SIGNALS)
2496 oldmask = sigblock (sigmask (SIGINT));
2497 # endif /* HAVE_BSD_SIGNALS */
2498 #endif /* !HAVE_POSIX_SIGNALS */
2500 #if defined (TERMIOS_TTY_DRIVER)
2501 tcgetattr (tty, &tio);
2503 ioctl (tty, TCGETA, &tio);
2504 #endif /* !TERMIOS_TTY_DRIVER */
2508 readline_echoing_p = (tio.c_lflag & ECHO);
2510 tio.c_lflag &= ~(ICANON|ECHO);
2512 if (otio.c_cc[VEOF] != _POSIX_VDISABLE)
2513 eof_char = otio.c_cc[VEOF];
2515 #if defined (USE_XON_XOFF)
2517 tio.c_iflag &= ~(IXON|IXOFF|IXANY);
2519 /* `strict' Posix systems do not define IXANY. */
2520 tio.c_iflag &= ~(IXON|IXOFF);
2522 #endif /* USE_XON_XOFF */
2524 /* Only turn this off if we are using all 8 bits. */
2526 tio.c_iflag &= ~(ISTRIP | INPCK);
2528 /* Make sure we differentiate between CR and NL on input. */
2529 tio.c_iflag &= ~(ICRNL | INLCR);
2531 #if !defined (HANDLE_SIGNALS)
2532 tio.c_lflag &= ~ISIG;
2534 tio.c_lflag |= ISIG;
2538 tio.c_cc[VTIME] = 0;
2540 /* Turn off characters that we need on Posix systems with job control,
2541 just to be sure. This includes ^Y and ^V. This should not really
2543 #if defined (TERMIOS_TTY_DRIVER) && defined (_POSIX_JOB_CONTROL)
2545 #if defined (VLNEXT)
2546 tio.c_cc[VLNEXT] = _POSIX_VDISABLE;
2549 #if defined (VDSUSP)
2550 tio.c_cc[VDSUSP] = _POSIX_VDISABLE;
2553 #endif /* POSIX && JOB_CONTROL */
2555 #if defined (TERMIOS_TTY_DRIVER)
2556 tcsetattr (tty, TCSADRAIN, &tio);
2557 tcflow (tty, TCOON); /* Simulate a ^Q. */
2559 ioctl (tty, TCSETAW, &tio);
2560 ioctl (tty, TCXONC, 1); /* Simulate a ^Q. */
2561 #endif /* !TERMIOS_TTY_DRIVER */
2563 terminal_prepped = 1;
2565 #if defined (HAVE_POSIX_SIGNALS)
2566 sigprocmask (SIG_SETMASK, &oset, (sigset_t *)NULL);
2568 # if defined (HAVE_BSD_SIGNALS)
2569 sigsetmask (oldmask);
2570 # endif /* HAVE_BSD_SIGNALS */
2571 #endif /* !HAVE_POSIX_SIGNALS */
2572 #endif /* !__GO32__ */
2576 rl_deprep_terminal ()
2579 int tty = fileno (rl_instream);
2581 /* Try to keep this function from being INTerrupted. We can do it
2582 on POSIX and systems with BSD-like signal handling. */
2583 #if defined (HAVE_POSIX_SIGNALS)
2585 #else /* !HAVE_POSIX_SIGNALS */
2586 # if defined (HAVE_BSD_SIGNALS)
2588 # endif /* HAVE_BSD_SIGNALS */
2589 #endif /* !HAVE_POSIX_SIGNALS */
2591 if (!terminal_prepped)
2594 #if defined (HAVE_POSIX_SIGNALS)
2596 sigaddset (&set, SIGINT);
2597 sigprocmask (SIG_BLOCK, &set, &oset);
2598 #else /* !HAVE_POSIX_SIGNALS */
2599 # if defined (HAVE_BSD_SIGNALS)
2600 oldmask = sigblock (sigmask (SIGINT));
2601 # endif /* HAVE_BSD_SIGNALS */
2602 #endif /* !HAVE_POSIX_SIGNALS */
2604 #if defined (TERMIOS_TTY_DRIVER)
2605 tcsetattr (tty, TCSADRAIN, &otio);
2606 tcflow (tty, TCOON); /* Simulate a ^Q. */
2607 #else /* TERMIOS_TTY_DRIVER */
2608 ioctl (tty, TCSETAW, &otio);
2609 ioctl (tty, TCXONC, 1); /* Simulate a ^Q. */
2610 #endif /* !TERMIOS_TTY_DRIVER */
2612 terminal_prepped = 0;
2614 #if defined (HAVE_POSIX_SIGNALS)
2615 sigprocmask (SIG_SETMASK, &oset, (sigset_t *)NULL);
2616 #else /* !HAVE_POSIX_SIGNALS */
2617 # if defined (HAVE_BSD_SIGNALS)
2618 sigsetmask (oldmask);
2619 # endif /* HAVE_BSD_SIGNALS */
2620 #endif /* !HAVE_POSIX_SIGNALS */
2621 #endif /* !__GO32__ */
2623 #endif /* NEW_TTY_DRIVER */
2626 /* **************************************************************** */
2628 /* Utility Functions */
2630 /* **************************************************************** */
2632 /* Return 0 if C is not a member of the class of characters that belong
2633 in words, or 1 if it is. */
2635 int allow_pathname_alphabetic_chars = 0;
2636 char *pathname_alphabetic_chars = "/-_=~.#$";
2642 if (pure_alphabetic (c) || (numeric (c)))
2645 if (allow_pathname_alphabetic_chars)
2646 return ((int)rindex (pathname_alphabetic_chars, c));
2651 /* Return non-zero if C is a numeric character. */
2656 return (c >= '0' && c <= '9');
2659 /* Ring the terminal bell. */
2663 if (readline_echoing_p)
2666 if (prefer_visible_bell && visible_bell)
2667 tputs (visible_bell, 1, output_character_function);
2669 #endif /* !__GO32__ */
2671 fprintf (stderr, "\007");
2678 /* How to abort things. */
2682 rl_clear_message ();
2683 rl_init_argument ();
2684 rl_pending_input = 0;
2686 defining_kbd_macro = 0;
2687 while (executing_macro)
2688 pop_executing_macro ();
2690 rl_last_func = (Function *)NULL;
2691 longjmp (readline_top_level, 1);
2694 /* Return a copy of the string between FROM and TO.
2695 FROM is inclusive, TO is not. */
2696 #if defined (sun) /* Yes, that's right, some crufty function in sunview is
2697 called rl_copy (). */
2704 register int length;
2707 /* Fix it if the caller is confused. */
2716 copy = (char *)xmalloc (1 + length);
2717 strncpy (copy, the_line + from, length);
2718 copy[length] = '\0';
2722 /* Increase the size of RL_LINE_BUFFER until it has enough space to hold
2725 rl_extend_line_buffer (len)
2728 while (len >= rl_line_buffer_len)
2731 (rl_line_buffer, rl_line_buffer_len += DEFAULT_BUFFER_SIZE);
2733 the_line = rl_line_buffer;
2737 /* **************************************************************** */
2739 /* Insert and Delete */
2741 /* **************************************************************** */
2743 /* Insert a string of text into the line at point. This is the only
2744 way that you should do insertion. rl_insert () calls this
2746 rl_insert_text (string)
2749 extern int doing_an_undo;
2750 register int i, l = strlen (string);
2752 if (rl_end + l >= rl_line_buffer_len)
2753 rl_extend_line_buffer (rl_end + l);
2755 for (i = rl_end; i >= rl_point; i--)
2756 the_line[i + l] = the_line[i];
2757 strncpy (the_line + rl_point, string, l);
2759 /* Remember how to undo this if we aren't undoing something. */
2762 /* If possible and desirable, concatenate the undos. */
2763 if ((strlen (string) == 1) &&
2765 (rl_undo_list->what == UNDO_INSERT) &&
2766 (rl_undo_list->end == rl_point) &&
2767 (rl_undo_list->end - rl_undo_list->start < 20))
2768 rl_undo_list->end++;
2770 rl_add_undo (UNDO_INSERT, rl_point, rl_point + l, (char *)NULL);
2774 the_line[rl_end] = '\0';
2777 /* Delete the string between FROM and TO. FROM is
2778 inclusive, TO is not. */
2779 rl_delete_text (from, to)
2782 extern int doing_an_undo;
2783 register char *text;
2785 /* Fix it if the caller is confused. */
2792 text = rl_copy (from, to);
2793 strncpy (the_line + from, the_line + to, rl_end - to);
2795 /* Remember how to undo this delete. */
2797 rl_add_undo (UNDO_DELETE, from, to, text);
2801 rl_end -= (to - from);
2802 the_line[rl_end] = '\0';
2806 /* **************************************************************** */
2808 /* Readline character functions */
2810 /* **************************************************************** */
2812 /* This is not a gap editor, just a stupid line input routine. No hair
2813 is involved in writing any of the functions, and none should be. */
2817 rl_end is the place in the string that we would place '\0';
2818 i.e., it is always safe to place '\0' there.
2820 rl_point is the place in the string where the cursor is. Sometimes
2821 this is the same as rl_end.
2823 Any command that is called interactively receives two arguments.
2824 The first is a count: the numeric arg pased to this command.
2825 The second is the key which invoked this command.
2829 /* **************************************************************** */
2831 /* Movement Commands */
2833 /* **************************************************************** */
2835 /* Note that if you `optimize' the display for these functions, you cannot
2836 use said functions in other functions which do not do optimizing display.
2837 I.e., you will have to update the data base for rl_redisplay, and you
2838 might as well let rl_redisplay do that job. */
2840 /* Move forward COUNT characters. */
2845 rl_backward (-count);
2849 #if defined (VI_MODE)
2850 if (rl_point == (rl_end - (rl_editing_mode == vi_mode)))
2852 if (rl_point == rl_end)
2853 #endif /* VI_MODE */
2864 /* Move backward COUNT characters. */
2869 rl_forward (-count);
2884 /* Move to the beginning of the line. */
2890 /* Move to the end of the line. */
2896 /* Move forward a word. We do what Emacs does. */
2897 rl_forward_word (count)
2904 rl_backward_word (-count);
2910 if (rl_point == rl_end)
2913 /* If we are not in a word, move forward until we are in one.
2914 Then, move forward until we hit a non-alphabetic character. */
2915 c = the_line[rl_point];
2916 if (!alphabetic (c))
2918 while (++rl_point < rl_end)
2920 c = the_line[rl_point];
2921 if (alphabetic (c)) break;
2924 if (rl_point == rl_end) return;
2925 while (++rl_point < rl_end)
2927 c = the_line[rl_point];
2928 if (!alphabetic (c)) break;
2934 /* Move backward a word. We do what Emacs does. */
2935 rl_backward_word (count)
2942 rl_forward_word (-count);
2951 /* Like rl_forward_word (), except that we look at the characters
2952 just before point. */
2954 c = the_line[rl_point - 1];
2955 if (!alphabetic (c))
2959 c = the_line[rl_point - 1];
2960 if (alphabetic (c)) break;
2966 c = the_line[rl_point - 1];
2967 if (!alphabetic (c))
2975 /* Clear the current line. Numeric argument to C-l does this. */
2978 int curr_line = last_c_pos / screenwidth;
2979 extern char *term_clreol;
2981 move_vert(curr_line);
2982 move_cursor_relative (0, the_line); /* XXX is this right */
2987 ScreenGetCursor(&r, &c);
2989 memset(ScreenPrimary+r*w+c, 0, (w-c)*2);
2991 #else /* __GO32__ */
2993 tputs (term_clreol, 1, output_character_function);
2994 #endif /* __GO32__/else */
2996 rl_forced_update_display ();
2997 rl_display_fixed = 1;
3000 /* C-l typed to a line without quoting clears the screen, and then reprints
3001 the prompt and the current input line. Given a numeric arg, redraw only
3002 the current line. */
3005 extern char *term_clrpag;
3007 if (rl_explicit_arg)
3015 tputs (term_clrpag, 1, output_character_function);
3017 #endif /* !__GO32__ */
3020 rl_forced_update_display ();
3021 rl_display_fixed = 1;
3024 rl_arrow_keys (count, c)
3029 ch = rl_read_key ();
3031 switch (to_upper (ch))
3034 rl_get_previous_history (count);
3038 rl_get_next_history (count);
3046 rl_backward (count);
3055 /* **************************************************************** */
3059 /* **************************************************************** */
3061 /* Insert the character C at the current location, moving point forward. */
3062 rl_insert (count, c)
3071 /* If we can optimize, then do it. But don't let people crash
3072 readline because of extra large arguments. */
3073 if (count > 1 && count < 1024)
3075 string = (char *)alloca (1 + count);
3077 for (i = 0; i < count; i++)
3081 rl_insert_text (string);
3089 string = (char *)alloca (1024 + 1);
3091 for (i = 0; i < 1024; i++)
3096 decreaser = (count > 1024 ? 1024 : count);
3097 string[decreaser] = '\0';
3098 rl_insert_text (string);
3104 /* We are inserting a single character.
3105 If there is pending input, then make a string of all of the
3106 pending characters that are bound to rl_insert, and insert
3113 string = (char *)alloca (ibuffer_len + 1);
3116 while ((t = rl_get_char (&key)) &&
3117 (keymap[key].type == ISFUNC &&
3118 keymap[key].function == rl_insert))
3122 rl_unget_char (key);
3125 rl_insert_text (string);
3130 /* Inserting a single character. */
3131 string = (char *)alloca (2);
3135 rl_insert_text (string);
3139 /* Insert the next typed character verbatim. */
3140 rl_quoted_insert (count)
3143 int c = rl_read_key ();
3144 rl_insert (count, c);
3147 /* Insert a tab character. */
3148 rl_tab_insert (count)
3151 rl_insert (count, '\t');
3154 /* What to do when a NEWLINE is pressed. We accept the whole line.
3155 KEY is the key that invoked this command. I guess it could have
3156 meaning in the future. */
3157 rl_newline (count, key)
3163 #if defined (VI_MODE)
3165 extern int vi_doing_insert;
3166 if (vi_doing_insert)
3168 rl_end_undo_group ();
3169 vi_doing_insert = 0;
3172 #endif /* VI_MODE */
3174 if (readline_echoing_p)
3176 move_vert (vis_botlin);
3179 fflush (out_stream);
3184 rl_clean_up_for_exit ()
3186 if (readline_echoing_p)
3188 move_vert (vis_botlin);
3190 fflush (out_stream);
3191 rl_restart_output ();
3195 /* What to do for some uppercase characters, like meta characters,
3196 and some characters appearing in emacs_ctlx_keymap. This function
3197 is just a stub, you bind keys to it and the code in rl_dispatch ()
3198 is special cased. */
3199 rl_do_lowercase_version (ignore1, ignore2)
3200 int ignore1, ignore2;
3204 /* Rubout the character behind point. */
3222 int orig_point = rl_point;
3223 rl_backward (count);
3224 rl_kill_text (orig_point, rl_point);
3228 int c = the_line[--rl_point];
3229 rl_delete_text (rl_point, rl_point + 1);
3231 if (rl_point == rl_end && alphabetic (c) && last_c_pos)
3234 putc (' ', out_stream);
3237 visible_line[last_c_pos] = '\0';
3243 /* Delete the character under the cursor. Given a numeric argument,
3244 kill that many characters instead. */
3245 rl_delete (count, invoking_key)
3246 int count, invoking_key;
3254 if (rl_point == rl_end)
3262 int orig_point = rl_point;
3264 rl_kill_text (orig_point, rl_point);
3265 rl_point = orig_point;
3268 rl_delete_text (rl_point, rl_point + 1);
3272 /* **************************************************************** */
3276 /* **************************************************************** */
3278 /* The next two functions mimic unix line editing behaviour, except they
3279 save the deleted text on the kill ring. This is safer than not saving
3280 it, and since we have a ring, nobody should get screwed. */
3282 /* This does what C-w does in Unix. We can't prevent people from
3283 using behaviour that they expect. */
3284 rl_unix_word_rubout ()
3286 if (!rl_point) ding ();
3288 int orig_point = rl_point;
3289 while (rl_point && whitespace (the_line[rl_point - 1]))
3291 while (rl_point && !whitespace (the_line[rl_point - 1]))
3293 rl_kill_text (rl_point, orig_point);
3297 /* Here is C-u doing what Unix does. You don't *have* to use these
3298 key-bindings. We have a choice of killing the entire line, or
3299 killing from where we are to the start of the line. We choose the
3300 latter, because if you are a Unix weenie, then you haven't backspaced
3301 into the line at all, and if you aren't, then you know what you are
3303 rl_unix_line_discard ()
3305 if (!rl_point) ding ();
3307 rl_kill_text (rl_point, 0);
3314 /* **************************************************************** */
3316 /* Commands For Typos */
3318 /* **************************************************************** */
3320 /* Random and interesting things in here. */
3322 /* **************************************************************** */
3326 /* **************************************************************** */
3328 /* The three kinds of things that we know how to do. */
3333 /* Uppercase the word at point. */
3334 rl_upcase_word (count)
3337 rl_change_case (count, UpCase);
3340 /* Lowercase the word at point. */
3341 rl_downcase_word (count)
3344 rl_change_case (count, DownCase);
3347 /* Upcase the first letter, downcase the rest. */
3348 rl_capitalize_word (count)
3351 rl_change_case (count, CapCase);
3354 /* The meaty function.
3355 Change the case of COUNT words, performing OP on them.
3356 OP is one of UpCase, DownCase, or CapCase.
3357 If a negative argument is given, leave point where it started,
3358 otherwise, leave it where it moves to. */
3359 rl_change_case (count, op)
3362 register int start = rl_point, end;
3365 rl_forward_word (count);
3375 /* We are going to modify some text, so let's prepare to undo it. */
3376 rl_modifying (start, end);
3378 for (; start < end; start++)
3383 the_line[start] = to_upper (the_line[start]);
3387 the_line[start] = to_lower (the_line[start]);
3393 the_line[start] = to_upper (the_line[start]);
3398 the_line[start] = to_lower (the_line[start]);
3400 if (!pure_alphabetic (the_line[start]))
3411 /* **************************************************************** */
3415 /* **************************************************************** */
3417 /* Transpose the words at point. */
3418 rl_transpose_words (count)
3421 char *word1, *word2;
3422 int w1_beg, w1_end, w2_beg, w2_end;
3423 int orig_point = rl_point;
3427 /* Find the two words. */
3428 rl_forward_word (count);
3430 rl_backward_word (1);
3432 rl_backward_word (count);
3434 rl_forward_word (1);
3437 /* Do some check to make sure that there really are two words. */
3438 if ((w1_beg == w2_beg) || (w2_beg < w1_end))
3441 rl_point = orig_point;
3445 /* Get the text of the words. */
3446 word1 = rl_copy (w1_beg, w1_end);
3447 word2 = rl_copy (w2_beg, w2_end);
3449 /* We are about to do many insertions and deletions. Remember them
3450 as one operation. */
3451 rl_begin_undo_group ();
3453 /* Do the stuff at word2 first, so that we don't have to worry
3454 about word1 moving. */
3456 rl_delete_text (w2_beg, w2_end);
3457 rl_insert_text (word1);
3460 rl_delete_text (w1_beg, w1_end);
3461 rl_insert_text (word2);
3463 /* This is exactly correct since the text before this point has not
3464 changed in length. */
3467 /* I think that does it. */
3468 rl_end_undo_group ();
3469 free (word1); free (word2);
3472 /* Transpose the characters at point. If point is at the end of the line,
3473 then transpose the characters before point. */
3474 rl_transpose_chars (count)
3480 if (!rl_point || rl_end < 2) {
3487 if (rl_point == rl_end)
3489 int t = the_line[rl_point - 1];
3491 the_line[rl_point - 1] = the_line[rl_point - 2];
3492 the_line[rl_point - 2] = t;
3496 int t = the_line[rl_point];
3498 the_line[rl_point] = the_line[rl_point - 1];
3499 the_line[rl_point - 1] = t;
3501 if (count < 0 && rl_point)
3515 /* **************************************************************** */
3517 /* Bogus Flow Control */
3519 /* **************************************************************** */
3521 rl_restart_output (count, key)
3524 int fildes = fileno (rl_outstream);
3525 #if defined (TIOCSTART)
3526 #if defined (apollo)
3527 ioctl (&fildes, TIOCSTART, 0);
3529 ioctl (fildes, TIOCSTART, 0);
3533 # if defined (TERMIOS_TTY_DRIVER)
3534 tcflow (fildes, TCOON);
3536 # if defined (TCXONC)
3537 ioctl (fildes, TCXONC, TCOON);
3538 # endif /* TCXONC */
3539 # endif /* !TERMIOS_TTY_DRIVER */
3540 #endif /* TIOCSTART */
3543 rl_stop_output (count, key)
3546 int fildes = fileno (rl_instream);
3548 #if defined (TIOCSTOP)
3549 # if defined (apollo)
3550 ioctl (&fildes, TIOCSTOP, 0);
3552 ioctl (fildes, TIOCSTOP, 0);
3553 # endif /* apollo */
3555 # if defined (TERMIOS_TTY_DRIVER)
3556 tcflow (fildes, TCOOFF);
3558 # if defined (TCXONC)
3559 ioctl (fildes, TCXONC, TCOON);
3560 # endif /* TCXONC */
3561 # endif /* !TERMIOS_TTY_DRIVER */
3562 #endif /* TIOCSTOP */
3565 /* **************************************************************** */
3567 /* Completion matching, from readline's point of view. */
3569 /* **************************************************************** */
3571 /* Pointer to the generator function for completion_matches ().
3572 NULL means to use filename_entry_function (), the default filename
3574 Function *rl_completion_entry_function = (Function *)NULL;
3576 /* Pointer to alternative function to create matches.
3577 Function is called with TEXT, START, and END.
3578 START and END are indices in RL_LINE_BUFFER saying what the boundaries
3580 If this function exists and returns NULL then call the value of
3581 rl_completion_entry_function to try to match, otherwise use the
3582 array of strings returned. */
3583 Function *rl_attempted_completion_function = (Function *)NULL;
3585 /* Local variable states what happened during the last completion attempt. */
3586 static int completion_changed_buffer = 0;
3588 /* Complete the word at or before point. You have supplied the function
3589 that does the initial simple matching selection algorithm (see
3590 completion_matches ()). The default is to do filename completion. */
3592 rl_complete (ignore, invoking_key)
3593 int ignore, invoking_key;
3595 if (rl_last_func == rl_complete && !completion_changed_buffer)
3596 rl_complete_internal ('?');
3598 rl_complete_internal (TAB);
3601 /* List the possible completions. See description of rl_complete (). */
3602 rl_possible_completions ()
3604 rl_complete_internal ('?');
3607 /* The user must press "y" or "n". Non-zero return means "y" pressed. */
3613 if (c == 'y' || c == 'Y') return (1);
3614 if (c == 'n' || c == 'N') return (0);
3615 if (c == ABORT_CHAR) rl_abort ();
3619 /* Up to this many items will be displayed in response to a
3620 possible-completions call. After that, we ask the user if
3621 she is sure she wants to see them all. */
3622 int rl_completion_query_items = 100;
3624 /* The basic list of characters that signal a break between words for the
3625 completer routine. The contents of this variable is what breaks words
3626 in the shell, i.e. " \t\n\"\\'`@$><=" */
3627 char *rl_basic_word_break_characters = " \t\n\"\\'`@$><=;|&{(";
3629 /* The list of characters that signal a break between words for
3630 rl_complete_internal. The default list is the contents of
3631 rl_basic_word_break_characters. */
3632 char *rl_completer_word_break_characters = (char *)NULL;
3634 /* The list of characters which are used to quote a substring of the command
3635 line. Command completion occurs on the entire substring, and within the
3636 substring rl_completer_word_break_characters are treated as any other
3637 character, unless they also appear within this list. */
3638 char *rl_completer_quote_characters = (char *)NULL;
3640 /* List of characters that are word break characters, but should be left
3641 in TEXT when it is passed to the completion function. The shell uses
3642 this to help determine what kind of completing to do. */
3643 char *rl_special_prefixes = (char *)NULL;
3645 /* If non-zero, then disallow duplicates in the matches. */
3646 int rl_ignore_completion_duplicates = 1;
3648 /* Non-zero means that the results of the matches are to be treated
3649 as filenames. This is ALWAYS zero on entry, and can only be changed
3650 within a completion entry finder function. */
3651 int rl_filename_completion_desired = 0;
3653 /* This function, if defined, is called by the completer when real
3654 filename completion is done, after all the matching names have been
3655 generated. It is passed a (char**) known as matches in the code below.
3656 It consists of a NULL-terminated array of pointers to potential
3657 matching strings. The 1st element (matches[0]) is the maximal
3658 substring that is common to all matches. This function can re-arrange
3659 the list of matches as required, but all elements of the array must be
3660 free()'d if they are deleted. The main intent of this function is
3661 to implement FIGNORE a la SunOS csh. */
3662 Function *rl_ignore_some_completions_function = (Function *)NULL;
3664 /* Complete the word at or before point.
3665 WHAT_TO_DO says what to do with the completion.
3666 `?' means list the possible completions.
3667 TAB means do standard completion.
3668 `*' means insert all of the possible completions. */
3669 rl_complete_internal (what_to_do)
3672 char *filename_completion_function ();
3673 char **completion_matches (), **matches;
3675 int start, scan, end, delimiter = 0;
3676 char *text, *saved_line_buffer;
3677 char quote_char = '\0';
3681 saved_line_buffer = savestring (the_line);
3683 saved_line_buffer = (char *)NULL;
3685 if (rl_completion_entry_function)
3686 our_func = rl_completion_entry_function;
3688 our_func = (int (*)())filename_completion_function;
3690 /* Only the completion entry function can change this. */
3691 rl_filename_completion_desired = 0;
3693 /* We now look backwards for the start of a filename/variable word. */
3698 if (rl_completer_quote_characters)
3700 /* We have a list of characters which can be used in pairs to quote
3701 substrings for completion. Try to find the start of an unclosed
3703 FIXME: Doesn't yet handle '\' escapes to hid embedded quotes */
3704 for (scan = 0; scan < end; scan++)
3706 if (quote_char != '\0')
3708 /* Ignore everything until the matching close quote char */
3709 if (the_line[scan] == quote_char)
3711 /* Found matching close quote. Abandon this substring. */
3716 else if (rindex (rl_completer_quote_characters, the_line[scan]))
3718 /* Found start of a quoted substring. */
3719 quote_char = the_line[scan];
3720 rl_point = scan + 1;
3724 if (rl_point == end)
3726 /* We didn't find an unclosed quoted substring upon which to do
3727 completion, so use the word break characters to find the
3728 substring on which to do completion. */
3729 while (--rl_point &&
3730 !rindex (rl_completer_word_break_characters,
3731 the_line[rl_point])) {;}
3734 /* If we are at a word break, then advance past it. */
3735 if (rindex (rl_completer_word_break_characters, the_line[rl_point]))
3737 /* If the character that caused the word break was a quoting
3738 character, then remember it as the delimiter. */
3739 if (rindex ("\"'", the_line[rl_point]) && (end - rl_point) > 1)
3740 delimiter = the_line[rl_point];
3742 /* If the character isn't needed to determine something special
3743 about what kind of completion to perform, then advance past it. */
3745 if (!rl_special_prefixes ||
3746 !rindex (rl_special_prefixes, the_line[rl_point]))
3753 text = rl_copy (start, end);
3755 /* If the user wants to TRY to complete, but then wants to give
3756 up and use the default completion function, they set the
3757 variable rl_attempted_completion_function. */
3758 if (rl_attempted_completion_function)
3761 (char **)(*rl_attempted_completion_function) (text, start, end);
3765 our_func = (Function *)NULL;
3766 goto after_usual_completion;
3770 matches = completion_matches (text, our_func);
3772 after_usual_completion:
3783 /* It seems to me that in all the cases we handle we would like
3784 to ignore duplicate possibilities. Scan for the text to
3785 insert being identical to the other completions. */
3786 if (rl_ignore_completion_duplicates)
3788 char *lowest_common;
3791 /* Sort the items. */
3792 /* It is safe to sort this array, because the lowest common
3793 denominator found in matches[0] will remain in place. */
3794 for (i = 0; matches[i]; i++);
3795 qsort (matches, i, sizeof (char *), compare_strings);
3797 /* Remember the lowest common denominator for it may be unique. */
3798 lowest_common = savestring (matches[0]);
3800 for (i = 0; matches[i + 1]; i++)
3802 if (strcmp (matches[i], matches[i + 1]) == 0)
3805 matches[i] = (char *)-1;
3811 /* We have marked all the dead slots with (char *)-1.
3812 Copy all the non-dead entries into a new array. */
3815 (char **)malloc ((3 + newlen) * sizeof (char *));
3817 for (i = 1, j = 1; matches[i]; i++)
3819 if (matches[i] != (char *)-1)
3820 temp_array[j++] = matches[i];
3823 temp_array[j] = (char *)NULL;
3825 if (matches[0] != (char *)-1)
3830 matches = temp_array;
3833 /* Place the lowest common denominator back in [0]. */
3834 matches[0] = lowest_common;
3836 /* If there is one string left, and it is identical to the
3837 lowest common denominator, then the LCD is the string to
3839 if (j == 2 && strcmp (matches[0], matches[1]) == 0)
3842 matches[1] = (char *)NULL;
3849 /* If we are matching filenames, then here is our chance to
3850 do clever processing by re-examining the list. Call the
3851 ignore function with the array as a parameter. It can
3852 munge the array, deleting matches as it desires. */
3853 if (rl_ignore_some_completions_function &&
3854 our_func == (int (*)())filename_completion_function)
3855 (void)(*rl_ignore_some_completions_function)(matches);
3857 /* If we are doing completions on quoted substrings, and any matches
3858 contain any of the completer word break characters, then auto-
3859 matically prepend the substring with a quote character (just
3860 pick the first one from the list of such) if it does not already
3861 begin with a quote string. FIXME: Need to remove any such
3862 automatically inserted quote character when it no longer is
3863 necessary, such as if we change the string we are completing on
3864 and the new set of matches don't require a quoted substring? */
3866 replacement = matches[0];
3867 if (matches[0] != NULL
3868 && rl_completer_quote_characters != NULL
3869 && (quote_char == '\0'))
3871 for (i = 1; matches[i] != NULL; i++)
3873 if (strpbrk (matches[i], rl_completer_word_break_characters))
3875 /* Found an embedded word break character in a potential
3876 match, so need to prepend a quote character if we are
3877 replacing the completion string. */
3878 replacement = alloca (strlen (matches[0]) + 2);
3879 quote_char = *rl_completer_quote_characters;
3880 *replacement = quote_char;
3881 strcpy (replacement + 1, matches[0]);
3888 rl_delete_text (start, rl_point);
3890 rl_insert_text (replacement);
3893 /* If there are more matches, ring the bell to indicate.
3894 If this was the only match, and we are hacking files,
3895 check the file to see if it was a directory. If so,
3896 add a '/' to the name. If not, and we are at the end
3897 of the line, then add a space. */
3900 ding (); /* There are other matches remaining. */
3904 char temp_string[16];
3909 temp_string[temp_index++] = quote_char;
3911 temp_string[temp_index++] = delimiter ? delimiter : ' ';
3912 temp_string[temp_index++] = '\0';
3914 if (rl_filename_completion_desired)
3917 char *filename = tilde_expand (matches[0]);
3919 if ((stat (filename, &finfo) == 0) &&
3920 S_ISDIR (finfo.st_mode))
3922 if (the_line[rl_point] != '/')
3923 rl_insert_text ("/");
3927 if (rl_point == rl_end)
3928 rl_insert_text (temp_string);
3934 if (rl_point == rl_end)
3935 rl_insert_text (temp_string);
3944 rl_delete_text (start, rl_point);
3946 rl_begin_undo_group ();
3951 rl_insert_text (matches[i++]);
3952 rl_insert_text (" ");
3957 rl_insert_text (matches[0]);
3958 rl_insert_text (" ");
3960 rl_end_undo_group ();
3966 int len, count, limit, max = 0;
3969 /* Handle simple case first. What if there is only one answer? */
3974 if (rl_filename_completion_desired)
3975 temp = rindex (matches[0], '/');
3977 temp = (char *)NULL;
3985 fprintf (out_stream, "%s", temp);
3990 /* There is more than one answer. Find out how many there are,
3991 and find out what the maximum printed length of a single entry
3993 for (i = 1; matches[i]; i++)
3995 char *temp = (char *)NULL;
3997 /* If we are hacking filenames, then only count the characters
3998 after the last slash in the pathname. */
3999 if (rl_filename_completion_desired)
4000 temp = rindex (matches[i], '/');
4002 temp = (char *)NULL;
4009 if (strlen (temp) > max)
4010 max = strlen (temp);
4015 /* If there are many items, then ask the user if she
4016 really wants to see them all. */
4017 if (len >= rl_completion_query_items)
4020 fprintf (out_stream,
4021 "There are %d possibilities. Do you really", len);
4023 fprintf (out_stream, "wish to see them all? (y or n)");
4024 fflush (out_stream);
4031 /* How many items of MAX length can we fit in the screen window? */
4033 limit = screenwidth / max;
4034 if (limit != 1 && (limit * max == screenwidth))
4037 /* Avoid a possible floating exception. If max > screenwidth,
4038 limit will be 0 and a divide-by-zero fault will result. */
4042 /* How many iterations of the printing loop? */
4043 count = (len + (limit - 1)) / limit;
4045 /* Watch out for special case. If LEN is less than LIMIT, then
4046 just do the inner printing loop. */
4047 if (len < limit) count = 1;
4049 /* Sort the items if they are not already sorted. */
4050 if (!rl_ignore_completion_duplicates)
4051 qsort (matches, len, sizeof (char *), compare_strings);
4053 /* Print the sorted items, up-and-down alphabetically, like
4057 for (i = 1; i < count + 1; i++)
4059 for (j = 0, l = i; j < limit; j++)
4061 if (l > len || !matches[l])
4067 char *temp = (char *)NULL;
4069 if (rl_filename_completion_desired)
4070 temp = rindex (matches[l], '/');
4072 temp = (char *)NULL;
4079 fprintf (out_stream, "%s", temp);
4080 for (k = 0; k < max - strlen (temp); k++)
4081 putc (' ', out_stream);
4097 for (i = 0; matches[i]; i++)
4102 /* Check to see if the line has changed through all of this manipulation. */
4103 if (saved_line_buffer)
4105 if (strcmp (the_line, saved_line_buffer) != 0)
4106 completion_changed_buffer = 1;
4108 completion_changed_buffer = 0;
4110 free (saved_line_buffer);
4114 /* Stupid comparison routine for qsort () ing strings. */
4116 compare_strings (s1, s2)
4119 return (strcmp (*s1, *s2));
4122 /* A completion function for usernames.
4123 TEXT contains a partial username preceded by a random
4124 character (usually `~'). */
4126 username_completion_function (text, state)
4131 return (char *)NULL;
4132 #else /* !__GO32__ */
4133 static char *username = (char *)NULL;
4134 static struct passwd *entry;
4135 static int namelen, first_char, first_char_loc;
4144 if (first_char == '~')
4149 username = savestring (&text[first_char_loc]);
4150 namelen = strlen (username);
4154 while (entry = getpwent ())
4156 if (strncmp (username, entry->pw_name, namelen) == 0)
4163 return ((char *)NULL);
4167 char *value = (char *)xmalloc (2 + strlen (entry->pw_name));
4171 strcpy (value + first_char_loc, entry->pw_name);
4173 if (first_char == '~')
4174 rl_filename_completion_desired = 1;
4178 #endif /* !__GO32__ */
4181 /* **************************************************************** */
4183 /* Undo, and Undoing */
4185 /* **************************************************************** */
4187 /* Non-zero tells rl_delete_text and rl_insert_text to not add to
4189 int doing_an_undo = 0;
4191 /* The current undo list for THE_LINE. */
4192 UNDO_LIST *rl_undo_list = (UNDO_LIST *)NULL;
4194 /* Remember how to undo something. Concatenate some undos if that
4196 rl_add_undo (what, start, end, text)
4197 enum undo_code what;
4201 UNDO_LIST *temp = (UNDO_LIST *)xmalloc (sizeof (UNDO_LIST));
4203 temp->start = start;
4206 temp->next = rl_undo_list;
4207 rl_undo_list = temp;
4210 /* Free the existing undo list. */
4213 while (rl_undo_list) {
4214 UNDO_LIST *release = rl_undo_list;
4215 rl_undo_list = rl_undo_list->next;
4217 if (release->what == UNDO_DELETE)
4218 free (release->text);
4224 /* Undo the next thing in the list. Return 0 if there
4225 is nothing to undo, or non-zero if there was. */
4230 int waiting_for_begin = 0;
4238 switch (rl_undo_list->what) {
4240 /* Undoing deletes means inserting some text. */
4242 rl_point = rl_undo_list->start;
4243 rl_insert_text (rl_undo_list->text);
4244 free (rl_undo_list->text);
4247 /* Undoing inserts means deleting some text. */
4249 rl_delete_text (rl_undo_list->start, rl_undo_list->end);
4250 rl_point = rl_undo_list->start;
4253 /* Undoing an END means undoing everything 'til we get to
4256 waiting_for_begin++;
4259 /* Undoing a BEGIN means that we are done with this group. */
4261 if (waiting_for_begin)
4262 waiting_for_begin--;
4270 release = rl_undo_list;
4271 rl_undo_list = rl_undo_list->next;
4274 if (waiting_for_begin)
4280 /* Begin a group. Subsequent undos are undone as an atomic operation. */
4281 rl_begin_undo_group ()
4283 rl_add_undo (UNDO_BEGIN, 0, 0, 0);
4286 /* End an undo group started with rl_begin_undo_group (). */
4287 rl_end_undo_group ()
4289 rl_add_undo (UNDO_END, 0, 0, 0);
4292 /* Save an undo entry for the text from START to END. */
4293 rl_modifying (start, end)
4305 char *temp = rl_copy (start, end);
4306 rl_begin_undo_group ();
4307 rl_add_undo (UNDO_DELETE, start, end, temp);
4308 rl_add_undo (UNDO_INSERT, start, end, (char *)NULL);
4309 rl_end_undo_group ();
4313 /* Revert the current line to its previous state. */
4316 if (!rl_undo_list) ding ();
4318 while (rl_undo_list)
4323 /* Do some undoing of things that were done. */
4324 rl_undo_command (count)
4326 if (count < 0) return; /* Nothing to do. */
4342 /* **************************************************************** */
4344 /* History Utilities */
4346 /* **************************************************************** */
4348 /* We already have a history library, and that is what we use to control
4349 the history features of readline. However, this is our local interface
4350 to the history mechanism. */
4352 /* While we are editing the history, this is the saved
4353 version of the original line. */
4354 HIST_ENTRY *saved_line_for_history = (HIST_ENTRY *)NULL;
4356 /* Set the history pointer back to the last entry in the history. */
4357 start_using_history ()
4360 if (saved_line_for_history)
4361 free_history_entry (saved_line_for_history);
4363 saved_line_for_history = (HIST_ENTRY *)NULL;
4366 /* Free the contents (and containing structure) of a HIST_ENTRY. */
4367 free_history_entry (entry)
4376 /* Perhaps put back the current line if it has changed. */
4377 maybe_replace_line ()
4379 HIST_ENTRY *temp = current_history ();
4381 /* If the current line has changed, save the changes. */
4382 if (temp && ((UNDO_LIST *)(temp->data) != rl_undo_list))
4384 temp = replace_history_entry (where_history (), the_line, rl_undo_list);
4390 /* Put back the saved_line_for_history if there is one. */
4391 maybe_unsave_line ()
4393 if (saved_line_for_history)
4397 line_len = strlen (saved_line_for_history->line);
4399 if (line_len >= rl_line_buffer_len)
4400 rl_extend_line_buffer (line_len);
4402 strcpy (the_line, saved_line_for_history->line);
4403 rl_undo_list = (UNDO_LIST *)saved_line_for_history->data;
4404 free_history_entry (saved_line_for_history);
4405 saved_line_for_history = (HIST_ENTRY *)NULL;
4406 rl_end = rl_point = strlen (the_line);
4412 /* Save the current line in saved_line_for_history. */
4415 if (!saved_line_for_history)
4417 saved_line_for_history = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
4418 saved_line_for_history->line = savestring (the_line);
4419 saved_line_for_history->data = (char *)rl_undo_list;
4423 /* **************************************************************** */
4425 /* History Commands */
4427 /* **************************************************************** */
4429 /* Meta-< goes to the start of the history. */
4430 rl_beginning_of_history ()
4432 rl_get_previous_history (1 + where_history ());
4435 /* Meta-> goes to the end of the history. (The current line). */
4436 rl_end_of_history ()
4438 maybe_replace_line ();
4440 maybe_unsave_line ();
4443 /* Move down to the next history line. */
4444 rl_get_next_history (count)
4447 HIST_ENTRY *temp = (HIST_ENTRY *)NULL;
4451 rl_get_previous_history (-count);
4458 maybe_replace_line ();
4462 temp = next_history ();
4469 maybe_unsave_line ();
4474 line_len = strlen (temp->line);
4476 if (line_len >= rl_line_buffer_len)
4477 rl_extend_line_buffer (line_len);
4479 strcpy (the_line, temp->line);
4480 rl_undo_list = (UNDO_LIST *)temp->data;
4481 rl_end = rl_point = strlen (the_line);
4482 #if defined (VI_MODE)
4483 if (rl_editing_mode == vi_mode)
4485 #endif /* VI_MODE */
4489 /* Get the previous item out of our interactive history, making it the current
4490 line. If there is no previous history, just ding. */
4491 rl_get_previous_history (count)
4494 HIST_ENTRY *old_temp = (HIST_ENTRY *)NULL;
4495 HIST_ENTRY *temp = (HIST_ENTRY *)NULL;
4499 rl_get_next_history (-count);
4506 /* If we don't have a line saved, then save this one. */
4509 /* If the current line has changed, save the changes. */
4510 maybe_replace_line ();
4514 temp = previous_history ();
4522 /* If there was a large argument, and we moved back to the start of the
4523 history, that is not an error. So use the last value found. */
4524 if (!temp && old_temp)
4533 line_len = strlen (temp->line);
4535 if (line_len >= rl_line_buffer_len)
4536 rl_extend_line_buffer (line_len);
4538 strcpy (the_line, temp->line);
4539 rl_undo_list = (UNDO_LIST *)temp->data;
4540 rl_end = rl_point = line_len;
4542 #if defined (VI_MODE)
4543 if (rl_editing_mode == vi_mode)
4545 #endif /* VI_MODE */
4550 /* **************************************************************** */
4552 /* I-Search and Searching */
4554 /* **************************************************************** */
4556 /* Search backwards through the history looking for a string which is typed
4557 interactively. Start with the current line. */
4558 rl_reverse_search_history (sign, key)
4562 rl_search_history (-sign, key);
4565 /* Search forwards through the history looking for a string which is typed
4566 interactively. Start with the current line. */
4567 rl_forward_search_history (sign, key)
4571 rl_search_history (sign, key);
4574 /* Display the current state of the search in the echo-area.
4575 SEARCH_STRING contains the string that is being searched for,
4576 DIRECTION is zero for forward, or 1 for reverse,
4577 WHERE is the history list number of the current line. If it is
4578 -1, then this line is the starting one. */
4579 rl_display_search (search_string, reverse_p, where)
4580 char *search_string;
4581 int reverse_p, where;
4583 char *message = (char *)NULL;
4586 (char *)alloca (1 + (search_string ? strlen (search_string) : 0) + 30);
4590 #if defined (NOTDEF)
4592 sprintf (message, "[%d]", where + history_base);
4595 strcat (message, "(");
4598 strcat (message, "reverse-");
4600 strcat (message, "i-search)`");
4603 strcat (message, search_string);
4605 strcat (message, "': ");
4606 rl_message (message, 0, 0);
4610 /* Search through the history looking for an interactively typed string.
4611 This is analogous to i-search. We start the search in the current line.
4612 DIRECTION is which direction to search; >= 0 means forward, < 0 means
4614 rl_search_history (direction, invoking_key)
4618 /* The string that the user types in to search for. */
4619 char *search_string = (char *)alloca (128);
4621 /* The current length of SEARCH_STRING. */
4622 int search_string_index;
4624 /* The list of lines to search through. */
4627 /* The length of LINES. */
4630 /* Where we get LINES from. */
4631 HIST_ENTRY **hlist = history_list ();
4634 int orig_point = rl_point;
4635 int orig_line = where_history ();
4636 int last_found_line = orig_line;
4639 /* The line currently being searched. */
4642 /* Offset in that line. */
4645 /* Non-zero if we are doing a reverse search. */
4646 int reverse = (direction < 0);
4648 /* Create an arrary of pointers to the lines that we want to search. */
4649 maybe_replace_line ();
4651 for (i = 0; hlist[i]; i++);
4653 /* Allocate space for this many lines, +1 for the current input line,
4654 and remember those lines. */
4655 lines = (char **)alloca ((1 + (hlen = i)) * sizeof (char *));
4656 for (i = 0; i < hlen; i++)
4657 lines[i] = hlist[i]->line;
4659 if (saved_line_for_history)
4660 lines[i] = saved_line_for_history->line;
4662 /* So I have to type it in this way instead. */
4666 /* Keep that mips alloca happy. */
4667 alloced_line = (char *)alloca (1 + strlen (the_line));
4668 lines[i] = alloced_line;
4669 strcpy (lines[i], &the_line[0]);
4674 /* The line where we start the search. */
4677 /* Initialize search parameters. */
4678 *search_string = '\0';
4679 search_string_index = 0;
4681 /* Normalize DIRECTION into 1 or -1. */
4687 rl_display_search (search_string, reverse, -1);
4696 /* Hack C to Do What I Mean. */
4698 Function *f = (Function *)NULL;
4700 if (keymap[c].type == ISFUNC)
4702 f = keymap[c].function;
4704 if (f == rl_reverse_search_history)
4705 c = reverse ? -1 : -2;
4706 else if (f == rl_forward_search_history)
4707 c = !reverse ? -1 : -2;
4717 /* case invoking_key: */
4721 /* switch directions */
4723 direction = -direction;
4724 reverse = (direction < 0);
4729 strcpy (the_line, lines[orig_line]);
4730 rl_point = orig_point;
4731 rl_end = strlen (the_line);
4732 rl_clear_message ();
4736 if (c < 32 || c > 126)
4738 rl_execute_next (c);
4744 search_string[search_string_index++] = c;
4745 search_string[search_string_index] = '\0';
4750 if (!search_string_index)
4757 if (index != strlen (sline))
4770 (search_string, sline + index, search_string_index)
4778 register int limit =
4779 (strlen (sline) - search_string_index) + 1;
4781 while (index < limit)
4783 if (strncmp (search_string,
4785 search_string_index) == 0)
4794 /* At limit for direction? */
4795 if ((reverse && i < 0) ||
4796 (!reverse && i == hlen))
4801 index = strlen (sline);
4805 /* If the search string is longer than the current
4807 if (search_string_index > strlen (sline))
4810 /* Start actually searching. */
4812 index -= search_string_index;
4816 /* We cannot find the search string. Ding the bell. */
4818 i = last_found_line;
4822 /* We have found the search string. Just display it. But don't
4823 actually move there in the history list until the user accepts
4828 line_len = strlen (lines[i]);
4830 if (line_len >= rl_line_buffer_len)
4831 rl_extend_line_buffer (line_len);
4833 strcpy (the_line, lines[i]);
4836 last_found_line = i;
4838 (search_string, reverse, (i == orig_line) ? -1 : i);
4845 /* The searching is over. The user may have found the string that she
4846 was looking for, or else she may have exited a failing search. If
4847 INDEX is -1, then that shows that the string searched for was not
4848 found. We use this to determine where to place rl_point. */
4850 int now = last_found_line;
4852 /* First put back the original state. */
4853 strcpy (the_line, lines[orig_line]);
4855 if (now < orig_line)
4856 rl_get_previous_history (orig_line - now);
4858 rl_get_next_history (now - orig_line);
4860 /* If the index of the "matched" string is less than zero, then the
4861 final search string was never matched, so put point somewhere
4864 index = strlen (the_line);
4867 rl_clear_message ();
4871 /* Make C be the next command to be executed. */
4875 rl_pending_input = c;
4878 /* **************************************************************** */
4880 /* Killing Mechanism */
4882 /* **************************************************************** */
4884 /* What we assume for a max number of kills. */
4885 #define DEFAULT_MAX_KILLS 10
4887 /* The real variable to look at to find out when to flush kills. */
4888 int rl_max_kills = DEFAULT_MAX_KILLS;
4890 /* Where to store killed text. */
4891 char **rl_kill_ring = (char **)NULL;
4893 /* Where we are in the kill ring. */
4894 int rl_kill_index = 0;
4896 /* How many slots we have in the kill ring. */
4897 int rl_kill_ring_length = 0;
4899 /* How to say that you only want to save a certain amount
4900 of kill material. */
4901 rl_set_retained_kills (num)
4905 /* The way to kill something. This appends or prepends to the last
4906 kill, if the last command was a kill command. if FROM is less
4907 than TO, then the text is appended, otherwise prepended. If the
4908 last command was not a kill command, then a new slot is made for
4910 rl_kill_text (from, to)
4914 char *text = rl_copy (from, to);
4916 /* Is there anything to kill? */
4920 last_command_was_kill++;
4924 /* Delete the copied text from the line. */
4925 rl_delete_text (from, to);
4927 /* First, find the slot to work with. */
4928 if (!last_command_was_kill)
4930 /* Get a new slot. */
4933 /* If we don't have any defined, then make one. */
4934 rl_kill_ring = (char **)
4935 xmalloc (((rl_kill_ring_length = 1) + 1) * sizeof (char *));
4940 /* We have to add a new slot on the end, unless we have
4941 exceeded the max limit for remembering kills. */
4942 slot = rl_kill_ring_length;
4943 if (slot == rl_max_kills)
4946 free (rl_kill_ring[0]);
4947 for (i = 0; i < slot; i++)
4948 rl_kill_ring[i] = rl_kill_ring[i + 1];
4954 xrealloc (rl_kill_ring,
4955 ((slot = (rl_kill_ring_length += 1)) + 1)
4963 slot = rl_kill_ring_length - 1;
4966 /* If the last command was a kill, prepend or append. */
4967 if (last_command_was_kill && rl_editing_mode != vi_mode)
4969 char *old = rl_kill_ring[slot];
4970 char *new = (char *)xmalloc (1 + strlen (old) + strlen (text));
4984 rl_kill_ring[slot] = new;
4988 rl_kill_ring[slot] = text;
4990 rl_kill_index = slot;
4991 last_command_was_kill++;
4994 /* Now REMEMBER! In order to do prepending or appending correctly, kill
4995 commands always make rl_point's original position be the FROM argument,
4996 and rl_point's extent be the TO argument. */
4998 /* **************************************************************** */
5000 /* Killing Commands */
5002 /* **************************************************************** */
5004 /* Delete the word at point, saving the text in the kill ring. */
5005 rl_kill_word (count)
5008 int orig_point = rl_point;
5011 rl_backward_kill_word (-count);
5014 rl_forward_word (count);
5016 if (rl_point != orig_point)
5017 rl_kill_text (orig_point, rl_point);
5019 rl_point = orig_point;
5023 /* Rubout the word before point, placing it on the kill ring. */
5024 rl_backward_kill_word (count)
5027 int orig_point = rl_point;
5030 rl_kill_word (-count);
5033 rl_backward_word (count);
5035 if (rl_point != orig_point)
5036 rl_kill_text (orig_point, rl_point);
5040 /* Kill from here to the end of the line. If DIRECTION is negative, kill
5041 back to the line start instead. */
5042 rl_kill_line (direction)
5045 int orig_point = rl_point;
5048 rl_backward_kill_line (1);
5052 if (orig_point != rl_point)
5053 rl_kill_text (orig_point, rl_point);
5054 rl_point = orig_point;
5058 /* Kill backwards to the start of the line. If DIRECTION is negative, kill
5059 forwards to the line end instead. */
5060 rl_backward_kill_line (direction)
5063 int orig_point = rl_point;
5074 rl_kill_text (orig_point, rl_point);
5079 /* Yank back the last killed text. This ignores arguments. */
5082 if (!rl_kill_ring) rl_abort ();
5083 rl_insert_text (rl_kill_ring[rl_kill_index]);
5086 /* If the last command was yank, or yank_pop, and the text just
5087 before point is identical to the current kill item, then
5088 delete that text from the line, rotate the index down, and
5089 yank back some other text. */
5094 if (((rl_last_func != rl_yank_pop) && (rl_last_func != rl_yank)) ||
5100 l = strlen (rl_kill_ring[rl_kill_index]);
5101 if (((rl_point - l) >= 0) &&
5102 (strncmp (the_line + (rl_point - l),
5103 rl_kill_ring[rl_kill_index], l) == 0))
5105 rl_delete_text ((rl_point - l), rl_point);
5108 if (rl_kill_index < 0)
5109 rl_kill_index = rl_kill_ring_length - 1;
5117 /* Yank the COUNTth argument from the previous history line. */
5118 rl_yank_nth_arg (count, ignore)
5121 register HIST_ENTRY *entry = previous_history ();
5132 arg = history_arg_extract (count, count, entry->line);
5139 rl_begin_undo_group ();
5141 #if defined (VI_MODE)
5142 /* Vi mode always inserts a space befoe yanking the argument, and it
5143 inserts it right *after* rl_point. */
5144 if (rl_editing_mode == vi_mode)
5146 #endif /* VI_MODE */
5148 if (rl_point && the_line[rl_point - 1] != ' ')
5149 rl_insert_text (" ");
5151 rl_insert_text (arg);
5154 rl_end_undo_group ();
5157 /* How to toggle back and forth between editing modes. */
5158 rl_vi_editing_mode ()
5160 #if defined (VI_MODE)
5161 rl_editing_mode = vi_mode;
5162 rl_vi_insertion_mode ();
5163 #endif /* VI_MODE */
5166 rl_emacs_editing_mode ()
5168 rl_editing_mode = emacs_mode;
5169 keymap = emacs_standard_keymap;
5173 /* **************************************************************** */
5177 /* **************************************************************** */
5179 /* Non-zero means that case is not significant in completion. */
5180 int completion_case_fold = 0;
5182 /* Return an array of (char *) which is a list of completions for TEXT.
5183 If there are no completions, return a NULL pointer.
5184 The first entry in the returned array is the substitution for TEXT.
5185 The remaining entries are the possible completions.
5186 The array is terminated with a NULL pointer.
5188 ENTRY_FUNCTION is a function of two args, and returns a (char *).
5189 The first argument is TEXT.
5190 The second is a state argument; it should be zero on the first call, and
5191 non-zero on subsequent calls. It returns a NULL pointer to the caller
5192 when there are no more matches.
5195 completion_matches (text, entry_function)
5197 char *(*entry_function) ();
5199 /* Number of slots in match_list. */
5200 int match_list_size;
5202 /* The list of matches. */
5204 (char **)xmalloc (((match_list_size = 10) + 1) * sizeof (char *));
5206 /* Number of matches actually found. */
5209 /* Temporary string binder. */
5212 match_list[1] = (char *)NULL;
5214 while (string = (*entry_function) (text, matches))
5216 if (matches + 1 == match_list_size)
5217 match_list = (char **)xrealloc
5218 (match_list, ((match_list_size += 10) + 1) * sizeof (char *));
5220 match_list[++matches] = string;
5221 match_list[matches + 1] = (char *)NULL;
5224 /* If there were any matches, then look through them finding out the
5225 lowest common denominator. That then becomes match_list[0]. */
5229 int low = 100000; /* Count of max-matched characters. */
5231 /* If only one match, just use that. */
5234 match_list[0] = match_list[1];
5235 match_list[1] = (char *)NULL;
5239 /* Otherwise, compare each member of the list with
5240 the next, finding out where they stop matching. */
5244 register int c1, c2, si;
5246 if (completion_case_fold)
5249 (c1 = to_lower(match_list[i][si])) &&
5250 (c2 = to_lower(match_list[i + 1][si]));
5252 if (c1 != c2) break;
5257 (c1 = match_list[i][si]) &&
5258 (c2 = match_list[i + 1][si]);
5260 if (c1 != c2) break;
5263 if (low > si) low = si;
5266 match_list[0] = (char *)xmalloc (low + 1);
5267 strncpy (match_list[0], match_list[1], low);
5268 match_list[0][low] = '\0';
5271 else /* There were no matches. */
5274 match_list = (char **)NULL;
5276 return (match_list);
5279 /* Okay, now we write the entry_function for filename completion. In the
5280 general case. Note that completion in the shell is a little different
5281 because of all the pathnames that must be followed when looking up the
5282 completion for a command. */
5284 filename_completion_function (text, state)
5288 static DIR *directory;
5289 static char *filename = (char *)NULL;
5290 static char *dirname = (char *)NULL;
5291 static char *users_dirname = (char *)NULL;
5292 static int filename_len;
5294 dirent *entry = (dirent *)NULL;
5296 /* If we don't have any state, then do some initialization. */
5301 if (dirname) free (dirname);
5302 if (filename) free (filename);
5303 if (users_dirname) free (users_dirname);
5305 filename = savestring (text);
5306 if (!*text) text = ".";
5307 dirname = savestring (text);
5309 temp = rindex (dirname, '/');
5313 strcpy (filename, ++temp);
5317 strcpy (dirname, ".");
5319 /* We aren't done yet. We also support the "~user" syntax. */
5321 /* Save the version of the directory that the user typed. */
5322 users_dirname = savestring (dirname);
5326 temp_dirname = tilde_expand (dirname);
5328 dirname = temp_dirname;
5330 if (rl_symbolic_link_hook)
5331 (*rl_symbolic_link_hook) (&dirname);
5333 directory = opendir (dirname);
5334 filename_len = strlen (filename);
5336 rl_filename_completion_desired = 1;
5339 /* At this point we should entertain the possibility of hacking wildcarded
5340 filenames, like /usr/man/man<WILD>/te<TAB>. If the directory name
5341 contains globbing characters, then build an array of directories to
5342 glob on, and glob on the first one. */
5344 /* Now that we have some state, we can read the directory. */
5346 while (directory && (entry = readdir (directory)))
5348 /* Special case for no filename.
5349 All entries except "." and ".." match. */
5352 if ((strcmp (entry->d_name, ".") != 0) &&
5353 (strcmp (entry->d_name, "..") != 0))
5358 /* Otherwise, if these match upto the length of filename, then
5360 if (entry->d_name[0] == filename[0] && /* Quick test */
5361 (strncmp (filename, entry->d_name, filename_len) == 0))
5372 closedir (directory);
5373 directory = (DIR *)NULL;
5375 return (char *)NULL;
5381 if (dirname && (strcmp (dirname, ".") != 0))
5384 xmalloc (1 + strlen (users_dirname) + strlen (entry->d_name));
5385 strcpy (temp, users_dirname);
5386 strcat (temp, entry->d_name);
5390 temp = (savestring (entry->d_name));
5397 /* **************************************************************** */
5401 /* **************************************************************** */
5403 /* rl_add_defun (char *name, Function *function, int key)
5404 Add NAME to the list of named functions. Make FUNCTION
5405 be the function that gets called.
5406 If KEY is not -1, then bind it. */
5407 rl_add_defun (name, function, key)
5413 rl_bind_key (key, function);
5414 rl_add_funmap_entry (name, function);
5417 /* Bind KEY to FUNCTION. Returns non-zero if KEY is out of range. */
5419 rl_bind_key (key, function)
5426 if (key > 127 && key < 256)
5428 if (keymap[ESC].type == ISKMAP)
5430 Keymap escmap = (Keymap)keymap[ESC].function;
5433 escmap[key].type = ISFUNC;
5434 escmap[key].function = function;
5440 keymap[key].type = ISFUNC;
5441 keymap[key].function = function;
5445 /* Bind KEY to FUNCTION in MAP. Returns non-zero in case of invalid
5448 rl_bind_key_in_map (key, function, map)
5454 Keymap oldmap = keymap;
5457 result = rl_bind_key (key, function);
5462 /* Make KEY do nothing in the currently selected keymap.
5463 Returns non-zero in case of error. */
5468 return (rl_bind_key (key, (Function *)NULL));
5471 /* Make KEY do nothing in MAP.
5472 Returns non-zero in case of error. */
5474 rl_unbind_key_in_map (key, map)
5478 return (rl_bind_key_in_map (key, (Function *)NULL, map));
5481 /* Bind the key sequence represented by the string KEYSEQ to
5482 FUNCTION. This makes new keymaps as necessary. The initial
5483 place to do bindings is in MAP. */
5484 rl_set_key (keyseq, function, map)
5489 rl_generic_bind (ISFUNC, keyseq, function, map);
5492 /* Bind the key sequence represented by the string KEYSEQ to
5493 the string of characters MACRO. This makes new keymaps as
5494 necessary. The initial place to do bindings is in MAP. */
5495 rl_macro_bind (keyseq, macro, map)
5496 char *keyseq, *macro;
5502 macro_keys = (char *)xmalloc ((2 * strlen (macro)) + 1);
5504 if (rl_translate_keyseq (macro, macro_keys, ¯o_keys_len))
5509 rl_generic_bind (ISMACR, keyseq, macro_keys, map);
5512 /* Bind the key sequence represented by the string KEYSEQ to
5513 the arbitrary pointer DATA. TYPE says what kind of data is
5514 pointed to by DATA, right now this can be a function (ISFUNC),
5515 a macro (ISMACR), or a keymap (ISKMAP). This makes new keymaps
5516 as necessary. The initial place to do bindings is in MAP. */
5517 rl_generic_bind (type, keyseq, data, map)
5519 char *keyseq, *data;
5526 /* If no keys to bind to, exit right away. */
5527 if (!keyseq || !*keyseq)
5534 keys = (char *)alloca (1 + (2 * strlen (keyseq)));
5536 /* Translate the ASCII representation of KEYSEQ into an array
5537 of characters. Stuff the characters into ARRAY, and the
5538 length of ARRAY into LENGTH. */
5539 if (rl_translate_keyseq (keyseq, keys, &keys_len))
5542 /* Bind keys, making new keymaps as necessary. */
5543 for (i = 0; i < keys_len; i++)
5545 if (i + 1 < keys_len)
5547 if (map[keys[i]].type != ISKMAP)
5549 if (map[i].type == ISMACR)
5550 free ((char *)map[i].function);
5552 map[keys[i]].type = ISKMAP;
5553 map[keys[i]].function = (Function *)rl_make_bare_keymap ();
5555 map = (Keymap)map[keys[i]].function;
5559 if (map[keys[i]].type == ISMACR)
5560 free ((char *)map[keys[i]].function);
5562 map[keys[i]].function = (Function *)data;
5563 map[keys[i]].type = type;
5568 /* Translate the ASCII representation of SEQ, stuffing the
5569 values into ARRAY, an array of characters. LEN gets the
5570 final length of ARRAY. Return non-zero if there was an
5571 error parsing SEQ. */
5572 rl_translate_keyseq (seq, array, len)
5576 register int i, c, l = 0;
5578 for (i = 0; c = seq[i]; i++)
5587 if (((c == 'C' || c == 'M') && seq[i + 1] == '-') ||
5590 /* Handle special case of backwards define. */
5591 if (strncmp (&seq[i], "C-\\M-", 5) == 0)
5595 array[l++] = CTRL (to_upper (seq[i]));
5610 /* Special hack for C-?... */
5612 array[l++] = RUBOUT;
5614 array[l++] = CTRL (to_upper (seq[i]));
5632 /* Return a pointer to the function that STRING represents.
5633 If STRING doesn't have a matching function, then a NULL pointer
5636 rl_named_function (string)
5641 for (i = 0; funmap[i]; i++)
5642 if (stricmp (funmap[i]->name, string) == 0)
5643 return (funmap[i]->function);
5644 return ((Function *)NULL);
5647 /* The last key bindings file read. */
5649 /* Don't know what to do, but this is a guess */
5650 static char *last_readline_init_file = "/INPUTRC";
5652 static char *last_readline_init_file = "~/inputrc";
5655 /* Re-read the current keybindings file. */
5656 rl_re_read_init_file (count, ignore)
5659 rl_read_init_file ((char *)NULL);
5662 /* Do key bindings from a file. If FILENAME is NULL it defaults
5663 to `~/.inputrc'. If the file existed and could be opened and
5664 read, 0 is returned, otherwise errno is returned. */
5666 rl_read_init_file (filename)
5670 char *buffer, *openname, *line, *end;
5674 /* Default the filename. */
5676 filename = last_readline_init_file;
5678 openname = tilde_expand (filename);
5680 if (!openname || *openname == '\000')
5683 if ((stat (openname, &finfo) < 0) ||
5684 (file = open (openname, O_RDONLY, 0666)) < 0)
5692 last_readline_init_file = filename;
5694 /* Read the file into BUFFER. */
5695 buffer = (char *)xmalloc (finfo.st_size + 1);
5696 i = read (file, buffer, finfo.st_size);
5699 if (i != finfo.st_size)
5702 /* Loop over the lines in the file. Lines that start with `#' are
5703 comments; all other lines are commands for readline initialization. */
5705 end = buffer + finfo.st_size;
5708 /* Find the end of this line. */
5709 for (i = 0; line + i != end && line[i] != '\n'; i++);
5711 /* Mark end of line. */
5714 /* If the line is not a comment, then parse it. */
5716 rl_parse_and_bind (line);
5718 /* Move to the next line. */
5724 /* **************************************************************** */
5726 /* Parser Directives */
5728 /* **************************************************************** */
5732 /* Calling programs set this to have their argv[0]. */
5733 char *rl_readline_name = "other";
5735 /* Stack of previous values of parsing_conditionalized_out. */
5736 static unsigned char *if_stack = (unsigned char *)NULL;
5737 static int if_stack_depth = 0;
5738 static int if_stack_size = 0;
5740 /* Push parsing_conditionalized_out, and set parser state based on ARGS. */
5746 /* Push parser state. */
5747 if (if_stack_depth + 1 >= if_stack_size)
5750 if_stack = (unsigned char *)xmalloc (if_stack_size = 20);
5752 if_stack = (unsigned char *)xrealloc (if_stack, if_stack_size += 20);
5754 if_stack[if_stack_depth++] = parsing_conditionalized_out;
5756 /* If parsing is turned off, then nothing can turn it back on except
5757 for finding the matching endif. In that case, return right now. */
5758 if (parsing_conditionalized_out)
5761 /* Isolate first argument. */
5762 for (i = 0; args[i] && !whitespace (args[i]); i++);
5767 /* Handle "if term=foo" and "if mode=emacs" constructs. If this
5768 isn't term=foo, or mode=emacs, then check to see if the first
5769 word in ARGS is the same as the value stored in rl_readline_name. */
5770 if (rl_terminal_name && strnicmp (args, "term=", 5) == 0)
5774 /* Terminals like "aaa-60" are equivalent to "aaa". */
5775 tname = savestring (rl_terminal_name);
5776 tem = rindex (tname, '-');
5780 if (stricmp (args + 5, tname) == 0)
5781 parsing_conditionalized_out = 0;
5783 parsing_conditionalized_out = 1;
5785 #if defined (VI_MODE)
5786 else if (strnicmp (args, "mode=", 5) == 0)
5790 if (stricmp (args + 5, "emacs") == 0)
5792 else if (stricmp (args + 5, "vi") == 0)
5797 if (mode == rl_editing_mode)
5798 parsing_conditionalized_out = 0;
5800 parsing_conditionalized_out = 1;
5802 #endif /* VI_MODE */
5803 /* Check to see if the first word in ARGS is the same as the
5804 value stored in rl_readline_name. */
5805 else if (stricmp (args, rl_readline_name) == 0)
5806 parsing_conditionalized_out = 0;
5808 parsing_conditionalized_out = 1;
5811 /* Invert the current parser state if there is anything on the stack. */
5817 if (!if_stack_depth)
5819 /* Error message? */
5823 /* Check the previous (n - 1) levels of the stack to make sure that
5824 we haven't previously turned off parsing. */
5825 for (i = 0; i < if_stack_depth - 1; i++)
5826 if (if_stack[i] == 1)
5829 /* Invert the state of parsing if at top level. */
5830 parsing_conditionalized_out = !parsing_conditionalized_out;
5833 /* Terminate a conditional, popping the value of
5834 parsing_conditionalized_out from the stack. */
5839 parsing_conditionalized_out = if_stack[--if_stack_depth];
5842 /* *** What, no error message? *** */
5846 /* Associate textual names with actual functions. */
5850 } parser_directives [] = {
5851 { "if", parser_if },
5852 { "endif", parser_endif },
5853 { "else", parser_else },
5854 { (char *)0x0, (Function *)0x0 }
5857 /* Handle a parser directive. STATEMENT is the line of the directive
5858 without any leading `$'. */
5860 handle_parser_directive (statement)
5864 char *directive, *args;
5866 /* Isolate the actual directive. */
5868 /* Skip whitespace. */
5869 for (i = 0; whitespace (statement[i]); i++);
5871 directive = &statement[i];
5873 for (; statement[i] && !whitespace (statement[i]); i++);
5876 statement[i++] = '\0';
5878 for (; statement[i] && whitespace (statement[i]); i++);
5880 args = &statement[i];
5882 /* Lookup the command, and act on it. */
5883 for (i = 0; parser_directives[i].name; i++)
5884 if (stricmp (directive, parser_directives[i].name) == 0)
5886 (*parser_directives[i].function) (args);
5890 /* *** Should an error message be output? */
5894 /* Ugly but working hack for binding prefix meta. */
5895 #define PREFIX_META_HACK
5897 static int substring_member_of_array ();
5899 /* Read the binding command from STRING and perform it.
5900 A key binding command looks like: Keyname: function-name\0,
5901 a variable binding command looks like: set variable value.
5902 A new-style keybinding looks like "\C-x\C-x": exchange-point-and-mark. */
5903 rl_parse_and_bind (string)
5906 extern char *possible_control_prefixes[], *possible_meta_prefixes[];
5907 char *funname, *kname;
5911 while (string && whitespace (*string))
5914 if (!string || !*string || *string == '#')
5917 /* If this is a parser directive, act on it. */
5920 handle_parser_directive (&string[1]);
5924 /* If we are supposed to be skipping parsing right now, then do it. */
5925 if (parsing_conditionalized_out)
5929 /* If this keyname is a complex key expression surrounded by quotes,
5930 advance to after the matching close quote. */
5933 for (i = 1; c = string[i]; i++)
5935 if (c == '"' && string[i - 1] != '\\')
5940 /* Advance to the colon (:) or whitespace which separates the two objects. */
5941 for (; (c = string[i]) && c != ':' && c != ' ' && c != '\t'; i++ );
5943 /* Mark the end of the command (or keyname). */
5947 /* If this is a command to set a variable, then do that. */
5948 if (stricmp (string, "set") == 0)
5950 char *var = string + i;
5953 /* Make VAR point to start of variable name. */
5954 while (*var && whitespace (*var)) var++;
5956 /* Make value point to start of value string. */
5958 while (*value && !whitespace (*value)) value++;
5961 while (*value && whitespace (*value)) value++;
5963 rl_variable_bind (var, value);
5967 /* Skip any whitespace between keyname and funname. */
5968 for (; string[i] && whitespace (string[i]); i++);
5969 funname = &string[i];
5971 /* Now isolate funname.
5972 For straight function names just look for whitespace, since
5973 that will signify the end of the string. But this could be a
5974 macro definition. In that case, the string is quoted, so skip
5975 to the matching delimiter. */
5976 if (*funname == '\'' || *funname == '"')
5978 int delimiter = string[i++];
5980 for (; c = string[i]; i++)
5982 if (c == delimiter && string[i - 1] != '\\')
5989 /* Advance to the end of the string. */
5990 for (; string[i] && !whitespace (string[i]); i++);
5992 /* No extra whitespace at the end of the string. */
5995 /* If this is a new-style key-binding, then do the binding with
5996 rl_set_key (). Otherwise, let the older code deal with it. */
5999 char *seq = (char *)alloca (1 + strlen (string));
6000 register int j, k = 0;
6002 for (j = 1; string[j]; j++)
6004 if (string[j] == '"' && string[j - 1] != '\\')
6007 seq[k++] = string[j];
6011 /* Binding macro? */
6012 if (*funname == '\'' || *funname == '"')
6014 j = strlen (funname);
6016 if (j && funname[j - 1] == *funname)
6017 funname[j - 1] = '\0';
6019 rl_macro_bind (seq, &funname[1], keymap);
6022 rl_set_key (seq, rl_named_function (funname), keymap);
6027 /* Get the actual character we want to deal with. */
6028 kname = rindex (string, '-');
6034 key = glean_key_from_name (kname);
6036 /* Add in control and meta bits. */
6037 if (substring_member_of_array (string, possible_control_prefixes))
6038 key = CTRL (to_upper (key));
6040 if (substring_member_of_array (string, possible_meta_prefixes))
6043 /* Temporary. Handle old-style keyname with macro-binding. */
6044 if (*funname == '\'' || *funname == '"')
6047 int fl = strlen (funname);
6049 seq[0] = key; seq[1] = '\0';
6050 if (fl && funname[fl - 1] == *funname)
6051 funname[fl - 1] = '\0';
6053 rl_macro_bind (seq, &funname[1], keymap);
6055 #if defined (PREFIX_META_HACK)
6056 /* Ugly, but working hack to keep prefix-meta around. */
6057 else if (stricmp (funname, "prefix-meta") == 0)
6063 rl_generic_bind (ISKMAP, seq, (char *)emacs_meta_keymap, keymap);
6065 #endif /* PREFIX_META_HACK */
6067 rl_bind_key (key, rl_named_function (funname));
6070 rl_variable_bind (name, value)
6073 if (stricmp (name, "editing-mode") == 0)
6075 if (strnicmp (value, "vi", 2) == 0)
6077 #if defined (VI_MODE)
6078 keymap = vi_insertion_keymap;
6079 rl_editing_mode = vi_mode;
6081 #if defined (NOTDEF)
6082 /* What state is the terminal in? I'll tell you:
6083 non-determinate! That means we cannot do any output. */
6086 #endif /* VI_MODE */
6088 else if (strnicmp (value, "emacs", 5) == 0)
6090 keymap = emacs_standard_keymap;
6091 rl_editing_mode = emacs_mode;
6094 else if (stricmp (name, "horizontal-scroll-mode") == 0)
6096 if (!*value || stricmp (value, "On") == 0)
6097 horizontal_scroll_mode = 1;
6099 horizontal_scroll_mode = 0;
6101 else if (stricmp (name, "mark-modified-lines") == 0)
6103 if (!*value || stricmp (value, "On") == 0)
6104 mark_modified_lines = 1;
6106 mark_modified_lines = 0;
6108 else if (stricmp (name, "prefer-visible-bell") == 0)
6110 if (!*value || stricmp (value, "On") == 0)
6111 prefer_visible_bell = 1;
6113 prefer_visible_bell = 0;
6115 else if (stricmp (name, "comment-begin") == 0)
6117 #if defined (VI_MODE)
6118 extern char *rl_vi_comment_begin;
6122 if (rl_vi_comment_begin)
6123 free (rl_vi_comment_begin);
6125 rl_vi_comment_begin = savestring (value);
6127 #endif /* VI_MODE */
6131 /* Return the character which matches NAME.
6132 For example, `Space' returns ' '. */
6139 assoc_list name_key_alist[] = {
6142 { "Escape", '\033' },
6144 { "Newline", '\n' },
6155 glean_key_from_name (name)
6160 for (i = 0; name_key_alist[i].name; i++)
6161 if (stricmp (name, name_key_alist[i].name) == 0)
6162 return (name_key_alist[i].value);
6168 /* **************************************************************** */
6170 /* Key Binding and Function Information */
6172 /* **************************************************************** */
6174 /* Each of the following functions produces information about the
6175 state of keybindings and functions known to Readline. The info
6176 is always printed to rl_outstream, and in such a way that it can
6177 be read back in (i.e., passed to rl_parse_and_bind (). */
6179 /* Print the names of functions known to Readline. */
6181 rl_list_funmap_names (ignore)
6185 char **funmap_names;
6186 extern char **rl_funmap_names ();
6188 funmap_names = rl_funmap_names ();
6193 for (i = 0; funmap_names[i]; i++)
6194 fprintf (rl_outstream, "%s\n", funmap_names[i]);
6196 free (funmap_names);
6199 /* Return a NULL terminated array of strings which represent the key
6200 sequences that are used to invoke FUNCTION in MAP. */
6202 invoking_keyseqs_in_map (function, map)
6208 int result_index, result_size;
6210 result = (char **)NULL;
6211 result_index = result_size = 0;
6213 for (key = 0; key < 128; key++)
6215 switch (map[key].type)
6218 /* Macros match, if, and only if, the pointers are identical.
6219 Thus, they are treated exactly like functions in here. */
6221 /* If the function in the keymap is the one we are looking for,
6222 then add the current KEY to the list of invoking keys. */
6223 if (map[key].function == function)
6225 char *keyname = (char *)xmalloc (5);
6228 sprintf (keyname, "\\C-%c", to_lower (UNCTRL (key)));
6229 else if (key == RUBOUT)
6230 sprintf (keyname, "\\C-?");
6232 sprintf (keyname, "%c", key);
6234 if (result_index + 2 > result_size)
6237 result = (char **) xmalloc
6238 ((result_size = 10) * sizeof (char *));
6240 result = (char **) xrealloc
6241 (result, (result_size += 10) * sizeof (char *));
6244 result[result_index++] = keyname;
6245 result[result_index] = (char *)NULL;
6251 char **seqs = (char **)NULL;
6253 /* Find the list of keyseqs in this map which have FUNCTION as
6254 their target. Add the key sequences found to RESULT. */
6255 if (map[key].function)
6257 invoking_keyseqs_in_map (function, (Keymap)map[key].function);
6263 for (i = 0; seqs[i]; i++)
6265 char *keyname = (char *)xmalloc (6 + strlen (seqs[i]));
6268 sprintf (keyname, "\\e");
6269 else if (CTRL_P (key))
6270 sprintf (keyname, "\\C-%c", to_lower (UNCTRL (key)));
6271 else if (key == RUBOUT)
6272 sprintf (keyname, "\\C-?");
6274 sprintf (keyname, "%c", key);
6276 strcat (keyname, seqs[i]);
6278 if (result_index + 2 > result_size)
6282 xmalloc ((result_size = 10) * sizeof (char *));
6286 (result_size += 10) * sizeof (char *));
6289 result[result_index++] = keyname;
6290 result[result_index] = (char *)NULL;
6300 /* Return a NULL terminated array of strings which represent the key
6301 sequences that can be used to invoke FUNCTION using the current keymap. */
6303 rl_invoking_keyseqs (function)
6306 return (invoking_keyseqs_in_map (function, keymap));
6309 /* Print all of the current functions and their bindings to
6310 rl_outstream. If an explicit argument is given, then print
6311 the output in such a way that it can be read back in. */
6313 rl_dump_functions (count)
6316 void rl_function_dumper ();
6318 rl_function_dumper (rl_explicit_arg);
6323 /* Print all of the functions and their bindings to rl_outstream. If
6324 PRINT_READABLY is non-zero, then print the output in such a way
6325 that it can be read back in. */
6327 rl_function_dumper (print_readably)
6331 char **rl_funmap_names (), **names;
6334 names = rl_funmap_names ();
6336 fprintf (rl_outstream, "\n");
6338 for (i = 0; name = names[i]; i++)
6343 function = rl_named_function (name);
6344 invokers = invoking_keyseqs_in_map (function, keymap);
6349 fprintf (rl_outstream, "# %s (not bound)\n", name);
6354 for (j = 0; invokers[j]; j++)
6356 fprintf (rl_outstream, "\"%s\": %s\n",
6367 fprintf (rl_outstream, "%s is not bound to any keys\n",
6373 fprintf (rl_outstream, "%s can be found on ", name);
6375 for (j = 0; invokers[j] && j < 5; j++)
6377 fprintf (rl_outstream, "\"%s\"%s", invokers[j],
6378 invokers[j + 1] ? ", " : ".\n");
6381 if (j == 5 && invokers[j])
6382 fprintf (rl_outstream, "...\n");
6384 for (j = 0; invokers[j]; j++)
6394 /* **************************************************************** */
6396 /* String Utility Functions */
6398 /* **************************************************************** */
6400 static char *strindex ();
6402 /* Return pointer to first occurance in STRING1 of any character from STRING2,
6403 or NULL if no occurance found. */
6405 strpbrk (string1, string2)
6406 char *string1, *string2;
6408 register char *scan;
6410 for (; *string1 != '\0'; string1++)
6412 for (scan = string2; *scan != '\0'; scan++)
6414 if (*string1 == *scan)
6423 /* Return non-zero if any members of ARRAY are a substring in STRING. */
6425 substring_member_of_array (string, array)
6426 char *string, **array;
6430 if (strindex (string, *array))
6437 /* Whoops, Unix doesn't have strnicmp. */
6439 /* Compare at most COUNT characters from string1 to string2. Case
6442 strnicmp (string1, string2, count)
6443 char *string1, *string2;
6445 register char ch1, ch2;
6451 if (to_upper(ch1) == to_upper(ch2))
6458 /* strcmp (), but caseless. */
6460 stricmp (string1, string2)
6461 char *string1, *string2;
6463 register char ch1, ch2;
6465 while (*string1 && *string2)
6469 if (to_upper(ch1) != to_upper(ch2))
6472 return (*string1 | *string2);
6475 /* Determine if s2 occurs in s1. If so, return a pointer to the
6476 match in s1. The compare is case insensitive. */
6479 register char *s1, *s2;
6481 register int i, l = strlen (s2);
6482 register int len = strlen (s1);
6484 for (i = 0; (len - i) >= l; i++)
6485 if (strnicmp (&s1[i], s2, l) == 0)
6487 return ((char *)NULL);
6491 /* **************************************************************** */
6493 /* USG (System V) Support */
6495 /* **************************************************************** */
6497 /* When compiling and running in the `Posix' environment, Ultrix does
6498 not restart system calls, so this needs to do it. */
6509 #endif /* __GO32__ */
6513 result = read (fileno (stream), &c, sizeof (char));
6515 if (result == sizeof (char))
6518 /* If zero characters are returned, then the file that we are
6519 reading from is empty! Return EOF in that case. */
6524 /* If the error that we received was SIGINT, then try again,
6525 this is simply an interrupted system call to read ().
6526 Otherwise, some error ocurred, also signifying EOF. */
6529 #endif /* !__GO32__ */
6533 #if defined (STATIC_MALLOC)
6535 /* **************************************************************** */
6537 /* xmalloc and xrealloc () */
6539 /* **************************************************************** */
6541 static void memory_error_and_abort ();
6547 char *temp = (char *)malloc (bytes);
6550 memory_error_and_abort ();
6555 xrealloc (pointer, bytes)
6562 temp = (char *)malloc (bytes);
6564 temp = (char *)realloc (pointer, bytes);
6567 memory_error_and_abort ();
6573 memory_error_and_abort ()
6575 fprintf (stderr, "readline: Out of virtual memory!\n");
6578 #endif /* STATIC_MALLOC */
6581 /* **************************************************************** */
6583 /* Testing Readline */
6585 /* **************************************************************** */
6591 HIST_ENTRY **history_list ();
6592 char *temp = (char *)NULL;
6593 char *prompt = "readline% ";
6598 temp = readline (prompt);
6604 /* If there is anything on the line, print it and remember it. */
6607 fprintf (stderr, "%s\r\n", temp);
6611 /* Check for `command' that we handle. */
6612 if (strcmp (temp, "quit") == 0)
6615 if (strcmp (temp, "list") == 0)
6617 HIST_ENTRY **list = history_list ();
6621 for (i = 0; list[i]; i++)
6623 fprintf (stderr, "%d: %s\r\n", i, list[i]->line);
6624 free (list[i]->line);
6638 * compile-command: "gcc -g -traditional -I. -I.. -DTEST -o readline readline.c keymaps.o funmap.o history.o -ltermcap"