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 = stdin, *rl_outstream = stdout;
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 ();
296 /* Non-zero means to save keys that we dispatch on in a kbd macro. */
297 static int defining_kbd_macro = 0;
300 /* **************************************************************** */
302 /* Top Level Functions */
304 /* **************************************************************** */
306 static void rl_prep_terminal (), rl_deprep_terminal ();
308 /* Read a line of input. Prompt with PROMPT. A NULL PROMPT means
309 none. A return value of NULL means that EOF was encountered. */
314 char *readline_internal ();
319 /* If we are at EOF return a NULL string. */
320 if (rl_pending_input == EOF)
322 rl_pending_input = 0;
323 return ((char *)NULL);
329 #if defined (HANDLE_SIGNALS)
333 value = readline_internal ();
334 rl_deprep_terminal ();
336 #if defined (HANDLE_SIGNALS)
343 /* Read a line of input from the global rl_instream, doing output on
344 the global rl_outstream.
345 If rl_prompt is non-null, then that is our prompt. */
349 int lastc, c, eof_found;
351 in_stream = rl_instream;
352 out_stream = rl_outstream;
358 (*rl_startup_hook) ();
360 if (!readline_echoing_p)
364 fprintf (out_stream, "%s", rl_prompt);
372 #if defined (VI_MODE)
373 if (rl_editing_mode == vi_mode)
374 rl_vi_insertion_mode ();
380 int lk = last_command_was_kill;
381 int code = setjmp (readline_top_level);
386 if (!rl_pending_input)
388 /* Then initialize the argument and number of keys read. */
390 rl_key_sequence_length = 0;
395 /* EOF typed to a non-blank line is a <NL>. */
396 if (c == EOF && rl_end)
399 /* The character eof_char typed to blank line, and not as the
400 previous character is interpreted as EOF. */
401 if (((c == eof_char && lastc != c) || c == EOF) && !rl_end)
408 rl_dispatch (c, keymap);
410 /* If there was no change in last_command_was_kill, then no kill
411 has taken place. Note that if input is pending we are reading
412 a prefix command, so nothing has changed yet. */
413 if (!rl_pending_input)
415 if (lk == last_command_was_kill)
416 last_command_was_kill = 0;
419 #if defined (VI_MODE)
420 /* In vi mode, when you exit insert mode, the cursor moves back
421 over the previous character. We explicitly check for that here. */
422 if (rl_editing_mode == vi_mode && keymap == vi_movement_keymap)
430 /* Restore the original of this history line, iff the line that we
431 are editing was originally in the history, AND the line has changed. */
433 HIST_ENTRY *entry = current_history ();
435 if (entry && rl_undo_list)
437 char *temp = savestring (the_line);
439 entry = replace_history_entry (where_history (), the_line,
441 free_history_entry (entry);
443 strcpy (the_line, temp);
448 /* At any rate, it is highly likely that this line has an undo list. Get
456 return (savestring (the_line));
460 /* **************************************************************** */
462 /* Signal Handling */
464 /* **************************************************************** */
466 #if defined (SIGWINCH)
467 static SigHandler *old_sigwinch = (SigHandler *)NULL;
470 rl_handle_sigwinch (sig)
475 term = rl_terminal_name;
477 if (readline_echoing_p)
480 term = getenv ("TERM");
483 rl_reset_terminal (term);
486 rl_forced_update_display ();
491 old_sigwinch != (SigHandler *)SIG_IGN &&
492 old_sigwinch != (SigHandler *)SIG_DFL)
493 (*old_sigwinch) (sig);
494 #if !defined (VOID_SIGHANDLER)
496 #endif /* VOID_SIGHANDLER */
498 #endif /* SIGWINCH */
500 #if defined (HANDLE_SIGNALS)
501 /* Interrupt handling. */
503 *old_int = (SigHandler *)NULL,
504 *old_tstp = (SigHandler *)NULL,
505 *old_ttou = (SigHandler *)NULL,
506 *old_ttin = (SigHandler *)NULL,
507 *old_cont = (SigHandler *)NULL,
508 *old_alrm = (SigHandler *)NULL;
510 /* Handle an interrupt character. */
512 rl_signal_handler (sig)
515 #if !defined (HAVE_BSD_SIGNALS)
516 /* Since the signal will not be blocked while we are in the signal
517 handler, ignore it until rl_clear_signals resets the catcher. */
519 signal (sig, SIG_IGN);
520 #endif /* !HAVE_BSD_SIGNALS */
529 #if defined (SIGTSTP)
535 rl_clean_up_for_exit ();
536 rl_deprep_terminal ();
538 rl_pending_input = 0;
540 kill (getpid (), sig);
542 #if defined (HAVE_POSIX_SIGNALS)
547 sigprocmask (SIG_SETMASK, &set, (sigset_t *)NULL);
550 #if defined (HAVE_BSD_SIGNALS)
552 #endif /* HAVE_BSD_SIGNALS */
553 #endif /* HAVE_POSIX_SIGNALS */
559 #if !defined (VOID_SIGHANDLER)
561 #endif /* !VOID_SIGHANDLER */
566 old_int = (SigHandler *)signal (SIGINT, rl_signal_handler);
567 if (old_int == (SigHandler *)SIG_IGN)
568 signal (SIGINT, SIG_IGN);
570 old_alrm = (SigHandler *)signal (SIGALRM, rl_signal_handler);
571 if (old_alrm == (SigHandler *)SIG_IGN)
572 signal (SIGALRM, SIG_IGN);
574 #if defined (SIGTSTP)
575 old_tstp = (SigHandler *)signal (SIGTSTP, rl_signal_handler);
576 if (old_tstp == (SigHandler *)SIG_IGN)
577 signal (SIGTSTP, SIG_IGN);
579 #if defined (SIGTTOU)
580 old_ttou = (SigHandler *)signal (SIGTTOU, rl_signal_handler);
581 old_ttin = (SigHandler *)signal (SIGTTIN, rl_signal_handler);
583 if (old_tstp == (SigHandler *)SIG_IGN)
585 signal (SIGTTOU, SIG_IGN);
586 signal (SIGTTIN, SIG_IGN);
590 #if defined (SIGWINCH)
591 old_sigwinch = (SigHandler *)signal (SIGWINCH, rl_handle_sigwinch);
597 signal (SIGINT, old_int);
598 signal (SIGALRM, old_alrm);
600 #if defined (SIGTSTP)
601 signal (SIGTSTP, old_tstp);
604 #if defined (SIGTTOU)
605 signal (SIGTTOU, old_ttou);
606 signal (SIGTTIN, old_ttin);
609 #if defined (SIGWINCH)
610 signal (SIGWINCH, old_sigwinch);
613 #endif /* HANDLE_SIGNALS */
616 /* **************************************************************** */
618 /* Character Input Buffering */
620 /* **************************************************************** */
622 #if defined (USE_XON_XOFF)
623 /* If the terminal was in xoff state when we got to it, then xon_char
624 contains the character that is supposed to start it again. */
625 static int xon_char, xoff_state;
626 #endif /* USE_XON_XOFF */
628 static int pop_index = 0, push_index = 0, ibuffer_len = 511;
629 static unsigned char ibuffer[512];
631 /* Non-null means it is a pointer to a function to run while waiting for
633 Function *rl_event_hook = (Function *)NULL;
635 #define any_typein (push_index != pop_index)
637 /* Add KEY to the buffer of characters to be read. */
644 rl_pending_input = EOF;
646 ibuffer[push_index++] = key;
647 if (push_index >= ibuffer_len)
651 /* Return the amount of space available in the
652 buffer for stuffing characters. */
656 if (pop_index > push_index)
657 return (pop_index - push_index);
659 return (ibuffer_len - (push_index - pop_index));
662 /* Get a key from the buffer of characters to be read.
663 Return the key in KEY.
664 Result is KEY if there was a key, or 0 if there wasn't. */
669 if (push_index == pop_index)
672 *key = ibuffer[pop_index++];
674 if (pop_index >= ibuffer_len)
680 /* Stuff KEY into the *front* of the input buffer.
681 Returns non-zero if successful, zero if there is
682 no space left in the buffer. */
687 if (ibuffer_space ())
691 pop_index = ibuffer_len - 1;
692 ibuffer[pop_index] = key;
698 /* If a character is available to be read, then read it
699 and stuff it into IBUFFER. Otherwise, just return. */
711 if (kbhit() && ibuffer_space())
712 rl_stuff_char(getkey());
714 int tty = fileno (in_stream);
715 register int tem, result = -1;
719 #if defined (FIONREAD)
720 result = ioctl (tty, FIONREAD, &chars_avail);
727 flags = fcntl (tty, F_GETFL, 0);
729 fcntl (tty, F_SETFL, (flags | O_NDELAY));
730 chars_avail = read (tty, &input, 1);
732 fcntl (tty, F_SETFL, flags);
733 if (chars_avail == -1 && errno == EAGAIN)
737 /* If there's nothing available, don't waste time trying to read
739 if (chars_avail == 0)
742 tem = ibuffer_space ();
744 if (chars_avail > tem)
747 /* One cannot read all of the available input. I can only read a single
748 character at a time, or else programs which require input can be
749 thwarted. If the buffer is larger than one character, I lose.
751 if (tem < ibuffer_len)
756 while (chars_avail--)
757 rl_stuff_char (rl_getc (in_stream));
762 rl_stuff_char (input);
764 #endif /* def __GO32__/else */
767 static int next_macro_key ();
768 /* Read a key, including pending input. */
774 rl_key_sequence_length++;
776 if (rl_pending_input)
778 c = rl_pending_input;
779 rl_pending_input = 0;
783 /* If input is coming from a macro, then use that. */
784 if (c = next_macro_key ())
787 /* If the user has an event function, then call it periodically. */
790 while (rl_event_hook && !rl_get_char (&c))
798 if (!rl_get_char (&c))
799 c = rl_getc (in_stream);
806 /* I'm beginning to hate the declaration rules for various compilers. */
807 static void add_macro_char (), with_macro_input ();
809 /* Do the command associated with KEY in MAP.
810 If the associated command is really a keymap, then read
811 another key, and dispatch into that map. */
812 rl_dispatch (key, map)
817 if (defining_kbd_macro)
818 add_macro_char (key);
820 if (key > 127 && key < 256)
822 if (map[ESC].type == ISKMAP)
824 map = (Keymap)map[ESC].function;
826 rl_dispatch (key, map);
833 switch (map[key].type)
837 Function *func = map[key].function;
839 if (func != (Function *)NULL)
841 /* Special case rl_do_lowercase_version (). */
842 if (func == rl_do_lowercase_version)
844 rl_dispatch (to_lower (key), map);
848 (*map[key].function)(rl_numeric_arg * rl_arg_sign, key);
850 /* If we have input pending, then the last command was a prefix
851 command. Don't change the state of rl_last_func. Otherwise,
852 remember the last command executed in this variable. */
853 if (!rl_pending_input)
854 rl_last_func = map[key].function;
865 if (map[key].function != (Function *)NULL)
869 rl_key_sequence_length++;
870 newkey = rl_read_key ();
871 rl_dispatch (newkey, (Keymap)map[key].function);
881 if (map[key].function != (Function *)NULL)
885 macro = savestring ((char *)map[key].function);
886 with_macro_input (macro);
894 /* **************************************************************** */
896 /* Hacking Keyboard Macros */
898 /* **************************************************************** */
900 /* The currently executing macro string. If this is non-zero,
901 then it is a malloc ()'ed string where input is coming from. */
902 static char *executing_macro = (char *)NULL;
904 /* The offset in the above string to the next character to be read. */
905 static int executing_macro_index = 0;
907 /* The current macro string being built. Characters get stuffed
908 in here by add_macro_char (). */
909 static char *current_macro = (char *)NULL;
911 /* The size of the buffer allocated to current_macro. */
912 static int current_macro_size = 0;
914 /* The index at which characters are being added to current_macro. */
915 static int current_macro_index = 0;
917 /* A structure used to save nested macro strings.
918 It is a linked list of string/index for each saved macro. */
920 struct saved_macro *next;
925 /* The list of saved macros. */
926 struct saved_macro *macro_list = (struct saved_macro *)NULL;
928 /* Forward declarations of static functions. Thank you C. */
929 static void push_executing_macro (), pop_executing_macro ();
931 /* This one has to be declared earlier in the file. */
932 /* static void add_macro_char (); */
934 /* Set up to read subsequent input from STRING.
935 STRING is free ()'ed when we are done with it. */
937 with_macro_input (string)
940 push_executing_macro ();
941 executing_macro = string;
942 executing_macro_index = 0;
945 /* Return the next character available from a macro, or 0 if
946 there are no macro characters. */
950 if (!executing_macro)
953 if (!executing_macro[executing_macro_index])
955 pop_executing_macro ();
956 return (next_macro_key ());
959 return (executing_macro[executing_macro_index++]);
962 /* Save the currently executing macro on a stack of saved macros. */
964 push_executing_macro ()
966 struct saved_macro *saver;
968 saver = (struct saved_macro *)xmalloc (sizeof (struct saved_macro));
969 saver->next = macro_list;
970 saver->index = executing_macro_index;
971 saver->string = executing_macro;
976 /* Discard the current macro, replacing it with the one
977 on the top of the stack of saved macros. */
979 pop_executing_macro ()
982 free (executing_macro);
984 executing_macro = (char *)NULL;
985 executing_macro_index = 0;
989 struct saved_macro *disposer = macro_list;
990 executing_macro = macro_list->string;
991 executing_macro_index = macro_list->index;
992 macro_list = macro_list->next;
997 /* Add a character to the macro being built. */
1002 if (current_macro_index + 1 >= current_macro_size)
1005 current_macro = (char *)xmalloc (current_macro_size = 25);
1008 (char *)xrealloc (current_macro, current_macro_size += 25);
1011 current_macro[current_macro_index++] = c;
1012 current_macro[current_macro_index] = '\0';
1015 /* Begin defining a keyboard macro.
1016 Keystrokes are recorded as they are executed.
1017 End the definition with rl_end_kbd_macro ().
1018 If a numeric argument was explicitly typed, then append this
1019 definition to the end of the existing macro, and start by
1020 re-executing the existing macro. */
1021 rl_start_kbd_macro (ignore1, ignore2)
1022 int ignore1, ignore2;
1024 if (defining_kbd_macro)
1027 if (rl_explicit_arg)
1030 with_macro_input (savestring (current_macro));
1033 current_macro_index = 0;
1035 defining_kbd_macro = 1;
1038 /* Stop defining a keyboard macro.
1039 A numeric argument says to execute the macro right now,
1040 that many times, counting the definition as the first time. */
1041 rl_end_kbd_macro (count, ignore)
1044 if (!defining_kbd_macro)
1047 current_macro_index -= (rl_key_sequence_length - 1);
1048 current_macro[current_macro_index] = '\0';
1050 defining_kbd_macro = 0;
1052 rl_call_last_kbd_macro (--count, 0);
1055 /* Execute the most recently defined keyboard macro.
1056 COUNT says how many times to execute it. */
1057 rl_call_last_kbd_macro (count, ignore)
1064 with_macro_input (savestring (current_macro));
1068 /* **************************************************************** */
1070 /* Initializations */
1072 /* **************************************************************** */
1074 /* Initliaze readline (and terminal if not already). */
1077 extern char *rl_display_prompt;
1079 /* If we have never been called before, initialize the
1080 terminal and data structures. */
1081 if (!rl_initialized)
1083 readline_initialize_everything ();
1087 /* Initalize the current line information. */
1088 rl_point = rl_end = 0;
1089 the_line = rl_line_buffer;
1092 /* We aren't done yet. We haven't even gotten started yet! */
1095 /* Tell the history routines what is going on. */
1096 start_using_history ();
1098 /* Make the display buffer match the state of the line. */
1100 extern char *rl_display_prompt;
1101 extern int forced_display;
1105 rl_display_prompt = rl_prompt ? rl_prompt : "";
1109 /* No such function typed yet. */
1110 rl_last_func = (Function *)NULL;
1112 /* Parsing of key-bindings begins in an enabled state. */
1113 parsing_conditionalized_out = 0;
1116 /* Initialize the entire state of the world. */
1117 readline_initialize_everything ()
1119 /* Find out if we are running in Emacs. */
1120 running_in_emacs = getenv ("EMACS");
1122 /* Allocate data structures. */
1123 if (!rl_line_buffer)
1125 (char *)xmalloc (rl_line_buffer_len = DEFAULT_BUFFER_SIZE);
1127 /* Initialize the terminal interface. */
1128 init_terminal_io ((char *)NULL);
1130 /* Bind tty characters to readline functions. */
1131 readline_default_bindings ();
1133 /* Initialize the function names. */
1134 rl_initialize_funmap ();
1136 /* Read in the init file. */
1137 rl_read_init_file ((char *)NULL);
1139 /* If the completion parser's default word break characters haven't
1140 been set yet, then do so now. */
1142 extern char *rl_completer_word_break_characters;
1143 extern char *rl_basic_word_break_characters;
1145 if (rl_completer_word_break_characters == (char *)NULL)
1146 rl_completer_word_break_characters = rl_basic_word_break_characters;
1150 /* If this system allows us to look at the values of the regular
1151 input editing characters, then bind them to their readline
1152 equivalents, iff the characters are not bound to keymaps. */
1153 readline_default_bindings ()
1157 #if defined (NEW_TTY_DRIVER)
1158 struct sgttyb ttybuff;
1159 int tty = fileno (rl_instream);
1161 if (ioctl (tty, TIOCGETP, &ttybuff) != -1)
1165 erase = ttybuff.sg_erase;
1166 kill = ttybuff.sg_kill;
1168 if (erase != -1 && keymap[erase].type == ISFUNC)
1169 keymap[erase].function = rl_rubout;
1171 if (kill != -1 && keymap[kill].type == ISFUNC)
1172 keymap[kill].function = rl_unix_line_discard;
1175 #if defined (TIOCGLTC)
1179 if (ioctl (tty, TIOCGLTC, <) != -1)
1183 erase = lt.t_werasc;
1184 nextc = lt.t_lnextc;
1186 if (erase != -1 && keymap[erase].type == ISFUNC)
1187 keymap[erase].function = rl_unix_word_rubout;
1189 if (nextc != -1 && keymap[nextc].type == ISFUNC)
1190 keymap[nextc].function = rl_quoted_insert;
1193 #endif /* TIOCGLTC */
1194 #else /* not NEW_TTY_DRIVER */
1196 #if defined (TERMIOS_TTY_DRIVER)
1197 struct termios ttybuff;
1199 struct termio ttybuff;
1200 #endif /* TERMIOS_TTY_DRIVER */
1201 int tty = fileno (rl_instream);
1203 #if defined (TERMIOS_TTY_DRIVER)
1204 if (tcgetattr (tty, &ttybuff) != -1)
1206 if (ioctl (tty, TCGETA, &ttybuff) != -1)
1207 #endif /* !TERMIOS_TTY_DRIVER */
1211 erase = ttybuff.c_cc[VERASE];
1212 kill = ttybuff.c_cc[VKILL];
1214 if (erase != _POSIX_VDISABLE &&
1215 keymap[(unsigned char)erase].type == ISFUNC)
1216 keymap[(unsigned char)erase].function = rl_rubout;
1218 if (kill != _POSIX_VDISABLE &&
1219 keymap[(unsigned char)kill].type == ISFUNC)
1220 keymap[(unsigned char)kill].function = rl_unix_line_discard;
1222 #if defined (VLNEXT) && defined (TERMIOS_TTY_DRIVER)
1226 nextc = ttybuff.c_cc[VLNEXT];
1228 if (nextc != _POSIX_VDISABLE &&
1229 keymap[(unsigned char)nextc].type == ISFUNC)
1230 keymap[(unsigned char)nextc].function = rl_quoted_insert;
1232 #endif /* VLNEXT && TERMIOS_TTY_DRIVER */
1234 #if defined (VWERASE)
1238 werase = ttybuff.c_cc[VWERASE];
1240 if (werase != _POSIX_VDISABLE &&
1241 keymap[(unsigned char)werase].type == ISFUNC)
1242 keymap[(unsigned char)werase].function = rl_unix_word_rubout;
1244 #endif /* VWERASE */
1246 #endif /* !NEW_TTY_DRIVER */
1247 #endif /* def __GO32__ */
1251 /* **************************************************************** */
1253 /* Numeric Arguments */
1255 /* **************************************************************** */
1257 /* Handle C-u style numeric args, as well as M--, and M-digits. */
1259 /* Add the current digit to the argument in progress. */
1260 rl_digit_argument (ignore, key)
1263 rl_pending_input = key;
1267 /* What to do when you abort reading an argument. */
1268 rl_discard_argument ()
1271 rl_clear_message ();
1272 rl_init_argument ();
1275 /* Create a default argument. */
1278 rl_numeric_arg = rl_arg_sign = 1;
1279 rl_explicit_arg = 0;
1282 /* C-u, universal argument. Multiply the current argument by 4.
1283 Read a key. If the key has nothing to do with arguments, then
1284 dispatch on it. If the key is the abort character then abort. */
1285 rl_universal_argument ()
1287 rl_numeric_arg *= 4;
1296 rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg);
1297 key = c = rl_read_key ();
1299 if (keymap[c].type == ISFUNC &&
1300 keymap[c].function == rl_universal_argument)
1302 rl_numeric_arg *= 4;
1308 if (rl_explicit_arg)
1309 rl_numeric_arg = (rl_numeric_arg * 10) + (c - '0');
1311 rl_numeric_arg = (c - '0');
1312 rl_explicit_arg = 1;
1316 if (c == '-' && !rl_explicit_arg)
1323 rl_clear_message ();
1324 rl_dispatch (key, keymap);
1332 /* **************************************************************** */
1336 /* **************************************************************** */
1338 /* This is the stuff that is hard for me. I never seem to write good
1339 display routines in C. Let's see how I do this time. */
1341 /* (PWP) Well... Good for a simple line updater, but totally ignores
1342 the problems of input lines longer than the screen width.
1344 update_line and the code that calls it makes a multiple line,
1345 automatically wrapping line update. Carefull attention needs
1346 to be paid to the vertical position variables.
1348 handling of terminals with autowrap on (incl. DEC braindamage)
1349 could be improved a bit. Right now I just cheat and decrement
1350 screenwidth by one. */
1352 /* Keep two buffers; one which reflects the current contents of the
1353 screen, and the other to draw what we think the new contents should
1354 be. Then compare the buffers, and make whatever changes to the
1355 screen itself that we should. Finally, make the buffer that we
1356 just drew into be the one which reflects the current contents of the
1357 screen, and place the cursor where it belongs.
1359 Commands that want to can fix the display themselves, and then let
1360 this function know that the display has been fixed by setting the
1361 RL_DISPLAY_FIXED variable. This is good for efficiency. */
1363 /* Termcap variables: */
1364 extern char *term_up, *term_dc, *term_cr;
1365 extern int screenheight, screenwidth, terminal_can_insert;
1367 /* What YOU turn on when you have handled all redisplay yourself. */
1368 int rl_display_fixed = 0;
1370 /* The visible cursor position. If you print some text, adjust this. */
1374 /* The last left edge of text that was displayed. This is used when
1375 doing horizontal scrolling. It shifts in thirds of a screenwidth. */
1376 static int last_lmargin = 0;
1378 /* The line display buffers. One is the line currently displayed on
1379 the screen. The other is the line about to be displayed. */
1380 static char *visible_line = (char *)NULL;
1381 static char *invisible_line = (char *)NULL;
1383 /* Number of lines currently on screen minus 1. */
1386 /* A buffer for `modeline' messages. */
1389 /* Non-zero forces the redisplay even if we thought it was unnecessary. */
1390 int forced_display = 0;
1392 /* The stuff that gets printed out before the actual text of the line.
1393 This is usually pointing to rl_prompt. */
1394 char *rl_display_prompt = (char *)NULL;
1396 /* Default and initial buffer size. Can grow. */
1397 static int line_size = 1024;
1399 /* Non-zero means to always use horizontal scrolling in line display. */
1400 static int horizontal_scroll_mode = 0;
1402 /* Non-zero means to display an asterisk at the starts of history lines
1403 which have been modified. */
1404 static int mark_modified_lines = 0;
1406 /* Non-zero means to use a visible bell if one is available rather than
1407 simply ringing the terminal bell. */
1408 static int prefer_visible_bell = 0;
1410 /* I really disagree with this, but my boss (among others) insists that we
1411 support compilers that don't work. I don't think we are gaining by doing
1412 so; what is the advantage in producing better code if we can't use it? */
1413 /* The following two declarations belong inside the
1414 function block, not here. */
1415 static void move_cursor_relative ();
1416 static void output_some_chars ();
1417 static void output_character_function ();
1418 static int compare_strings ();
1420 /* Basic redisplay algorithm. */
1423 register int in, out, c, linenum;
1424 register char *line = invisible_line;
1425 char *prompt_this_line;
1427 int inv_botlin = 0; /* Number of lines in newly drawn buffer. */
1429 extern int readline_echoing_p;
1431 if (!readline_echoing_p)
1434 if (!rl_display_prompt)
1435 rl_display_prompt = "";
1437 if (!invisible_line)
1439 visible_line = (char *)xmalloc (line_size);
1440 invisible_line = (char *)xmalloc (line_size);
1441 line = invisible_line;
1442 for (in = 0; in < line_size; in++)
1444 visible_line[in] = 0;
1445 invisible_line[in] = 1;
1450 /* Draw the line into the buffer. */
1453 /* Mark the line as modified or not. We only do this for history
1456 if (mark_modified_lines && current_history () && rl_undo_list)
1462 /* If someone thought that the redisplay was handled, but the currently
1463 visible line has a different modification state than the one about
1464 to become visible, then correct the callers misconception. */
1465 if (visible_line[0] != invisible_line[0])
1466 rl_display_fixed = 0;
1468 prompt_this_line = rindex (rl_display_prompt, '\n');
1469 if (!prompt_this_line)
1470 prompt_this_line = rl_display_prompt;
1475 output_some_chars (rl_display_prompt,
1476 prompt_this_line - rl_display_prompt);
1479 strncpy (line + out, prompt_this_line, strlen (prompt_this_line));
1480 out += strlen (prompt_this_line);
1483 for (in = 0; in < rl_end; in++)
1485 c = (unsigned char)the_line[in];
1487 if (out + 1 >= line_size)
1490 visible_line = (char *)xrealloc (visible_line, line_size);
1491 invisible_line = (char *)xrealloc (invisible_line, line_size);
1492 line = invisible_line;
1502 line[out++] = c - 128;
1504 #define DISPLAY_TABS
1505 #if defined (DISPLAY_TABS)
1508 register int newout = (out | (int)7) + 1;
1509 while (out < newout)
1517 line[out++] = c + 64;
1532 /* PWP: now is when things get a bit hairy. The visible and invisible
1533 line buffers are really multiple lines, which would wrap every
1534 (screenwidth - 1) characters. Go through each in turn, finding
1535 the changed region and updating it. The line order is top to bottom. */
1537 /* If we can move the cursor up and down, then use multiple lines,
1538 otherwise, let long lines display in a single terminal line, and
1539 horizontally scroll it. */
1541 if (!horizontal_scroll_mode && term_up && *term_up)
1543 int total_screen_chars = (screenwidth * screenheight);
1545 if (!rl_display_fixed || forced_display)
1549 /* If we have more than a screenful of material to display, then
1550 only display a screenful. We should display the last screen,
1551 not the first. I'll fix this in a minute. */
1552 if (out >= total_screen_chars)
1553 out = total_screen_chars - 1;
1555 /* Number of screen lines to display. */
1556 inv_botlin = out / screenwidth;
1558 /* For each line in the buffer, do the updating display. */
1559 for (linenum = 0; linenum <= inv_botlin; linenum++)
1560 update_line (linenum > vis_botlin ? ""
1561 : &visible_line[linenum * screenwidth],
1562 &invisible_line[linenum * screenwidth],
1565 /* We may have deleted some lines. If so, clear the left over
1566 blank ones at the bottom out. */
1567 if (vis_botlin > inv_botlin)
1570 for (; linenum <= vis_botlin; linenum++)
1572 tt = &visible_line[linenum * screenwidth];
1573 move_vert (linenum);
1574 move_cursor_relative (0, tt);
1575 clear_to_eol ((linenum == vis_botlin)?
1576 strlen (tt) : screenwidth);
1579 vis_botlin = inv_botlin;
1581 /* Move the cursor where it should be. */
1582 move_vert (c_pos / screenwidth);
1583 move_cursor_relative (c_pos % screenwidth,
1584 &invisible_line[(c_pos / screenwidth) * screenwidth]);
1587 else /* Do horizontal scrolling. */
1591 /* Always at top line. */
1594 /* If the display position of the cursor would be off the edge
1595 of the screen, start the display of this line at an offset that
1596 leaves the cursor on the screen. */
1597 if (c_pos - last_lmargin > screenwidth - 2)
1598 lmargin = (c_pos / (screenwidth / 3) - 2) * (screenwidth / 3);
1599 else if (c_pos - last_lmargin < 1)
1600 lmargin = ((c_pos - 1) / (screenwidth / 3)) * (screenwidth / 3);
1602 lmargin = last_lmargin;
1604 /* If the first character on the screen isn't the first character
1605 in the display line, indicate this with a special character. */
1607 line[lmargin] = '<';
1609 if (lmargin + screenwidth < out)
1610 line[lmargin + screenwidth - 1] = '>';
1612 if (!rl_display_fixed || forced_display || lmargin != last_lmargin)
1615 update_line (&visible_line[last_lmargin],
1616 &invisible_line[lmargin], 0);
1618 move_cursor_relative (c_pos - lmargin, &invisible_line[lmargin]);
1619 last_lmargin = lmargin;
1622 fflush (out_stream);
1624 /* Swap visible and non-visible lines. */
1626 char *temp = visible_line;
1627 visible_line = invisible_line;
1628 invisible_line = temp;
1629 rl_display_fixed = 0;
1633 /* PWP: update_line() is based on finding the middle difference of each
1634 line on the screen; vis:
1636 /old first difference
1637 /beginning of line | /old last same /old EOL
1639 old: eddie> Oh, my little gruntle-buggy is to me, as lurgid as
1640 new: eddie> Oh, my little buggy says to me, as lurgid as
1642 \beginning of line | \new last same \new end of line
1643 \new first difference
1645 All are character pointers for the sake of speed. Special cases for
1646 no differences, as well as for end of line additions must be handeled.
1648 Could be made even smarter, but this works well enough */
1650 update_line (old, new, current_line)
1651 register char *old, *new;
1654 register char *ofd, *ols, *oe, *nfd, *nls, *ne;
1655 int lendiff, wsatend;
1657 /* Find first difference. */
1658 for (ofd = old, nfd = new;
1659 (ofd - old < screenwidth) && *ofd && (*ofd == *nfd);
1663 /* Move to the end of the screen line. */
1664 for (oe = ofd; ((oe - old) < screenwidth) && *oe; oe++);
1665 for (ne = nfd; ((ne - new) < screenwidth) && *ne; ne++);
1667 /* If no difference, continue to next line. */
1668 if (ofd == oe && nfd == ne)
1671 wsatend = 1; /* flag for trailing whitespace */
1672 ols = oe - 1; /* find last same */
1674 while ((*ols == *nls) && (ols > ofd) && (nls > nfd))
1687 else if (*ols != *nls)
1689 if (*ols) /* don't step past the NUL */
1695 move_vert (current_line);
1696 move_cursor_relative (ofd - old, old);
1698 /* if (len (new) > len (old)) */
1699 lendiff = (nls - nfd) - (ols - ofd);
1701 /* Insert (diff(len(old),len(new)) ch */
1704 if (terminal_can_insert)
1706 extern char *term_IC;
1708 /* Sometimes it is cheaper to print the characters rather than
1709 use the terminal's capabilities. */
1710 if ((2 * (ne - nfd)) < lendiff && !term_IC)
1712 output_some_chars (nfd, (ne - nfd));
1713 last_c_pos += (ne - nfd);
1719 insert_some_chars (nfd, lendiff);
1720 last_c_pos += lendiff;
1724 /* At the end of a line the characters do not have to
1725 be "inserted". They can just be placed on the screen. */
1726 output_some_chars (nfd, lendiff);
1727 last_c_pos += lendiff;
1729 /* Copy (new) chars to screen from first diff to last match. */
1730 if (((nls - nfd) - lendiff) > 0)
1732 output_some_chars (&nfd[lendiff], ((nls - nfd) - lendiff));
1733 last_c_pos += ((nls - nfd) - lendiff);
1738 { /* cannot insert chars, write to EOL */
1739 output_some_chars (nfd, (ne - nfd));
1740 last_c_pos += (ne - nfd);
1743 else /* Delete characters from line. */
1745 /* If possible and inexpensive to use terminal deletion, then do so. */
1746 if (term_dc && (2 * (ne - nfd)) >= (-lendiff))
1749 delete_chars (-lendiff); /* delete (diff) characters */
1751 /* Copy (new) chars to screen from first diff to last match */
1752 if ((nls - nfd) > 0)
1754 output_some_chars (nfd, (nls - nfd));
1755 last_c_pos += (nls - nfd);
1758 /* Otherwise, print over the existing material. */
1761 output_some_chars (nfd, (ne - nfd));
1762 last_c_pos += (ne - nfd);
1763 clear_to_eol ((oe - old) - (ne - new));
1768 /* (PWP) tell the update routines that we have moved onto a
1769 new (empty) line. */
1773 visible_line[0] = '\0';
1775 last_c_pos = last_v_pos = 0;
1776 vis_botlin = last_lmargin = 0;
1779 /* Actually update the display, period. */
1780 rl_forced_update_display ()
1784 register char *temp = visible_line;
1786 while (*temp) *temp++ = '\0';
1793 /* Move the cursor from last_c_pos to NEW, which are buffer indices.
1794 DATA is the contents of the screen line of interest; i.e., where
1795 the movement is being done. */
1797 move_cursor_relative (new, data)
1803 /* It may be faster to output a CR, and then move forwards instead
1804 of moving backwards. */
1805 if (new + 1 < last_c_pos - new)
1808 putc('\r', out_stream);
1810 tputs (term_cr, 1, output_character_function);
1815 if (last_c_pos == new) return;
1817 if (last_c_pos < new)
1819 /* Move the cursor forward. We do it by printing the command
1820 to move the cursor forward if there is one, else print that
1821 portion of the output buffer again. Which is cheaper? */
1823 /* The above comment is left here for posterity. It is faster
1824 to print one character (non-control) than to print a control
1825 sequence telling the terminal to move forward one character.
1826 That kind of control is for people who don't know what the
1827 data is underneath the cursor. */
1828 #if defined (HACK_TERMCAP_MOTION)
1829 extern char *term_forward_char;
1831 if (term_forward_char)
1832 for (i = last_c_pos; i < new; i++)
1833 tputs (term_forward_char, 1, output_character_function);
1835 for (i = last_c_pos; i < new; i++)
1836 putc (data[i], out_stream);
1838 for (i = last_c_pos; i < new; i++)
1839 putc (data[i], out_stream);
1840 #endif /* HACK_TERMCAP_MOTION */
1843 backspace (last_c_pos - new);
1847 /* PWP: move the cursor up or down. */
1851 void output_character_function ();
1852 register int delta, i;
1854 if (last_v_pos == to) return;
1856 if (to > screenheight)
1862 ScreenGetCursor(&cur_r, &cur_c);
1863 ScreenSetCursor(cur_r+to-last_v_pos, cur_c);
1865 #else /* __GO32__ */
1866 if ((delta = to - last_v_pos) > 0)
1868 for (i = 0; i < delta; i++)
1869 putc ('\n', out_stream);
1870 tputs (term_cr, 1, output_character_function);
1875 if (term_up && *term_up)
1876 for (i = 0; i < -delta; i++)
1877 tputs (term_up, 1, output_character_function);
1879 #endif /* __GO32__ */
1880 last_v_pos = to; /* now to is here */
1883 /* Physically print C on out_stream. This is for functions which know
1884 how to optimize the display. */
1890 fprintf (out_stream, "M-");
1894 #if defined (DISPLAY_TABS)
1895 if (c < 32 && c != '\t')
1904 putc (c, out_stream);
1905 fflush (out_stream);
1908 #if defined (DISPLAY_TABS)
1910 rl_character_len (c, pos)
1911 register int c, pos;
1913 if (c < ' ' || c > 126)
1916 return (((pos | (int)7) + 1) - pos);
1925 rl_character_len (c)
1928 if (c < ' ' || c > 126)
1933 #endif /* DISPLAY_TAB */
1935 /* How to print things in the "echo-area". The prompt is treated as a
1937 rl_message (string, arg1, arg2)
1940 sprintf (msg_buf, string, arg1, arg2);
1941 rl_display_prompt = msg_buf;
1945 /* How to clear things from the "echo-area". */
1948 rl_display_prompt = rl_prompt;
1952 /* **************************************************************** */
1954 /* Terminal and Termcap */
1956 /* **************************************************************** */
1958 static char *term_buffer = (char *)NULL;
1959 static char *term_string_buffer = (char *)NULL;
1961 /* Non-zero means this terminal can't really do anything. */
1967 /* Some strings to control terminal actions. These are output by tputs (). */
1968 char *term_goto, *term_clreol, *term_cr, *term_clrpag, *term_backspace;
1970 int screenwidth, screenheight;
1972 /* Non-zero if we determine that the terminal can do character insertion. */
1973 int terminal_can_insert = 0;
1975 /* How to insert characters. */
1976 char *term_im, *term_ei, *term_ic, *term_ip, *term_IC;
1978 /* How to delete characters. */
1979 char *term_dc, *term_DC;
1981 #if defined (HACK_TERMCAP_MOTION)
1982 char *term_forward_char;
1983 #endif /* HACK_TERMCAP_MOTION */
1985 /* How to go up a line. */
1988 /* A visible bell, if the terminal can be made to flash the screen. */
1991 /* Re-initialize the terminal considering that the TERM/TERMCAP variable
1993 rl_reset_terminal (terminal_name)
1994 char *terminal_name;
1996 init_terminal_io (terminal_name);
1999 init_terminal_io (terminal_name)
2000 char *terminal_name;
2003 screenwidth = ScreenCols();
2004 screenheight = ScreenRows();
2006 term_im = term_ei = term_ic = term_IC = (char *)NULL;
2007 term_up = term_dc = term_DC = visible_bell = (char *)NULL;
2008 #if defined (HACK_TERMCAP_MOTION)
2009 term_forward_char = (char *)NULL;
2011 terminal_can_insert = 0;
2014 extern char *tgetstr ();
2015 char *term, *buffer;
2016 #if defined (TIOCGWINSZ)
2017 struct winsize window_size;
2021 term = terminal_name ? terminal_name : getenv ("TERM");
2023 if (!term_string_buffer)
2024 term_string_buffer = (char *)xmalloc (2048);
2027 term_buffer = (char *)xmalloc (2048);
2029 buffer = term_string_buffer;
2031 term_clrpag = term_cr = term_clreol = (char *)NULL;
2036 if (tgetent (term_buffer, term) < 0)
2042 term_im = term_ei = term_ic = term_IC = (char *)NULL;
2043 term_up = term_dc = term_DC = visible_bell = (char *)NULL;
2044 #if defined (HACK_TERMCAP_MOTION)
2045 term_forward_char = (char *)NULL;
2047 terminal_can_insert = 0;
2051 BC = tgetstr ("pc", &buffer);
2052 PC = buffer ? *buffer : 0;
2054 term_backspace = tgetstr ("le", &buffer);
2056 term_cr = tgetstr ("cr", &buffer);
2057 term_clreol = tgetstr ("ce", &buffer);
2058 term_clrpag = tgetstr ("cl", &buffer);
2063 #if defined (HACK_TERMCAP_MOTION)
2064 term_forward_char = tgetstr ("nd", &buffer);
2065 #endif /* HACK_TERMCAP_MOTION */
2068 tty = fileno (rl_instream);
2072 screenwidth = screenheight = 0;
2073 #if defined (TIOCGWINSZ)
2074 if (ioctl (tty, TIOCGWINSZ, &window_size) == 0)
2076 screenwidth = (int) window_size.ws_col;
2077 screenheight = (int) window_size.ws_row;
2081 if (screenwidth <= 0 || screenheight <= 0)
2083 screenwidth = tgetnum ("co");
2084 screenheight = tgetnum ("li");
2089 if (screenwidth <= 0)
2092 if (screenheight <= 0)
2095 term_im = tgetstr ("im", &buffer);
2096 term_ei = tgetstr ("ei", &buffer);
2097 term_IC = tgetstr ("IC", &buffer);
2098 term_ic = tgetstr ("ic", &buffer);
2100 /* "An application program can assume that the terminal can do
2101 character insertion if *any one of* the capabilities `IC',
2102 `im', `ic' or `ip' is provided." But we can't do anything if
2103 only `ip' is provided, so... */
2104 terminal_can_insert = (term_IC || term_im || term_ic);
2106 term_up = tgetstr ("up", &buffer);
2107 term_dc = tgetstr ("dc", &buffer);
2108 term_DC = tgetstr ("DC", &buffer);
2110 visible_bell = tgetstr ("vb", &buffer);
2111 #endif /* !__GO32__ */
2114 /* A function for the use of tputs () */
2116 output_character_function (c)
2119 putc (c, out_stream);
2122 /* Write COUNT characters from STRING to the output stream. */
2124 output_some_chars (string, count)
2128 fwrite (string, 1, count, out_stream);
2131 /* Delete COUNT characters from the display line. */
2133 delete_chars (count)
2138 ScreenGetCursor(&r, &c);
2140 memcpy(ScreenPrimary+r*w+c, ScreenPrimary+r*w+c+count, w-c-count);
2141 memset(ScreenPrimary+r*w+w-count, 0, count*2);
2142 #else /* __GO32__ */
2143 if (count > screenwidth)
2146 if (term_DC && *term_DC)
2148 char *tgoto (), *buffer;
2149 buffer = tgoto (term_DC, 0, count);
2150 tputs (buffer, 1, output_character_function);
2154 if (term_dc && *term_dc)
2156 tputs (term_dc, 1, output_character_function);
2158 #endif /* __GO32__ */
2161 /* Insert COUNT characters from STRING to the output stream. */
2163 insert_some_chars (string, count)
2169 ScreenGetCursor(&r, &c);
2171 memcpy(ScreenPrimary+r*w+c+count, ScreenPrimary+r*w+c, w-c-count);
2172 /* Print the text. */
2173 output_some_chars (string, count);
2174 #else /* __GO32__ */
2175 /* If IC is defined, then we do not have to "enter" insert mode. */
2178 char *tgoto (), *buffer;
2179 buffer = tgoto (term_IC, 0, count);
2180 tputs (buffer, 1, output_character_function);
2181 output_some_chars (string, count);
2187 /* If we have to turn on insert-mode, then do so. */
2188 if (term_im && *term_im)
2189 tputs (term_im, 1, output_character_function);
2191 /* If there is a special command for inserting characters, then
2192 use that first to open up the space. */
2193 if (term_ic && *term_ic)
2195 for (i = count; i--; )
2196 tputs (term_ic, 1, output_character_function);
2199 /* Print the text. */
2200 output_some_chars (string, count);
2202 /* If there is a string to turn off insert mode, we had best use
2204 if (term_ei && *term_ei)
2205 tputs (term_ei, 1, output_character_function);
2207 #endif /* __GO32__ */
2210 /* Move the cursor back. */
2218 for (i = 0; i < count; i++)
2219 tputs (term_backspace, 1, output_character_function);
2221 #endif /* !__GO32__ */
2222 for (i = 0; i < count; i++)
2223 putc ('\b', out_stream);
2226 /* Move to the start of the next line. */
2229 #if defined (NEW_TTY_DRIVER)
2230 tputs (term_cr, 1, output_character_function);
2231 #endif /* NEW_TTY_DRIVER */
2232 putc ('\n', out_stream);
2235 /* Clear to the end of the line. COUNT is the minimum
2236 number of character spaces to clear, */
2237 clear_to_eol (count)
2243 tputs (term_clreol, 1, output_character_function);
2246 #endif /* !__GO32__ */
2250 /* Do one more character space. */
2253 for (i = 0; i < count; i++)
2254 putc (' ', out_stream);
2261 /* **************************************************************** */
2263 /* Saving and Restoring the TTY */
2265 /* **************************************************************** */
2267 /* Non-zero means that the terminal is in a prepped state. */
2268 static int terminal_prepped = 0;
2270 #if defined (NEW_TTY_DRIVER)
2272 /* Standard flags, including ECHO. */
2273 static int original_tty_flags = 0;
2275 /* Local mode flags, like LPASS8. */
2276 static int local_mode_flags = 0;
2278 /* Terminal characters. This has C-s and C-q in it. */
2279 static struct tchars original_tchars;
2281 /* Local special characters. This has the interrupt characters in it. */
2282 #if defined (TIOCGLTC)
2283 static struct ltchars original_ltchars;
2286 /* We use this to get and set the tty_flags. */
2287 static struct sgttyb the_ttybuff;
2289 /* Put the terminal in CBREAK mode so that we can detect key presses. */
2294 int tty = fileno (rl_instream);
2295 #if defined (HAVE_BSD_SIGNALS)
2297 #endif /* HAVE_BSD_SIGNALS */
2299 if (terminal_prepped)
2302 oldmask = sigblock (sigmask (SIGINT));
2304 /* We always get the latest tty values. Maybe stty changed them. */
2305 ioctl (tty, TIOCGETP, &the_ttybuff);
2306 original_tty_flags = the_ttybuff.sg_flags;
2308 readline_echoing_p = (original_tty_flags & ECHO);
2310 #if defined (TIOCLGET)
2311 ioctl (tty, TIOCLGET, &local_mode_flags);
2315 # define ANYP (EVENP | ODDP)
2318 /* If this terminal doesn't care how the 8th bit is used,
2319 then we can use it for the meta-key. We check by seeing
2320 if BOTH odd and even parity are allowed. */
2321 if (the_ttybuff.sg_flags & ANYP)
2324 the_ttybuff.sg_flags |= PASS8;
2327 /* Hack on local mode flags if we can. */
2328 #if defined (TIOCLGET) && defined (LPASS8)
2331 flags = local_mode_flags | LPASS8;
2332 ioctl (tty, TIOCLSET, &flags);
2334 #endif /* TIOCLGET && LPASS8 */
2337 #if defined (TIOCGETC)
2341 ioctl (tty, TIOCGETC, &original_tchars);
2342 temp = original_tchars;
2344 #if defined (USE_XON_XOFF)
2345 /* Get rid of C-s and C-q.
2346 We remember the value of startc (C-q) so that if the terminal is in
2347 xoff state, the user can xon it by pressing that character. */
2348 xon_char = temp.t_startc;
2352 /* If there is an XON character, bind it to restart the output. */
2354 rl_bind_key (xon_char, rl_restart_output);
2355 #endif /* USE_XON_XOFF */
2357 /* If there is an EOF char, bind eof_char to it. */
2358 if (temp.t_eofc != -1)
2359 eof_char = temp.t_eofc;
2361 #if defined (NO_KILL_INTR)
2362 /* Get rid of C-\ and C-c. */
2363 temp.t_intrc = temp.t_quitc = -1;
2364 #endif /* NO_KILL_INTR */
2366 ioctl (tty, TIOCSETC, &temp);
2368 #endif /* TIOCGETC */
2370 #if defined (TIOCGLTC)
2372 struct ltchars temp;
2374 ioctl (tty, TIOCGLTC, &original_ltchars);
2375 temp = original_ltchars;
2377 /* Make the interrupt keys go away. Just enough to make people
2379 temp.t_dsuspc = -1; /* C-y */
2380 temp.t_lnextc = -1; /* C-v */
2382 ioctl (tty, TIOCSLTC, &temp);
2384 #endif /* TIOCGLTC */
2386 the_ttybuff.sg_flags &= ~(ECHO | CRMOD);
2387 the_ttybuff.sg_flags |= CBREAK;
2388 ioctl (tty, TIOCSETN, &the_ttybuff);
2390 terminal_prepped = 1;
2392 #if defined (HAVE_BSD_SIGNALS)
2393 sigsetmask (oldmask);
2395 #endif /* !__GO32__ */
2398 /* Restore the terminal to its original state. */
2400 rl_deprep_terminal ()
2403 int tty = fileno (rl_instream);
2404 #if defined (HAVE_BSD_SIGNALS)
2408 if (!terminal_prepped)
2411 oldmask = sigblock (sigmask (SIGINT));
2413 the_ttybuff.sg_flags = original_tty_flags;
2414 ioctl (tty, TIOCSETN, &the_ttybuff);
2415 readline_echoing_p = 1;
2417 #if defined (TIOCLGET)
2418 ioctl (tty, TIOCLSET, &local_mode_flags);
2421 #if defined (TIOCSLTC)
2422 ioctl (tty, TIOCSLTC, &original_ltchars);
2425 #if defined (TIOCSETC)
2426 ioctl (tty, TIOCSETC, &original_tchars);
2428 terminal_prepped = 0;
2430 #if defined (HAVE_BSD_SIGNALS)
2431 sigsetmask (oldmask);
2433 #endif /* !__GO32 */
2436 #else /* !defined (NEW_TTY_DRIVER) */
2442 #if !defined (VTIME)
2447 #if defined (TERMIOS_TTY_DRIVER)
2448 static struct termios otio;
2450 static struct termio otio;
2451 #endif /* !TERMIOS_TTY_DRIVER */
2452 #endif /* __GO32__ */
2458 int tty = fileno (rl_instream);
2459 #if defined (TERMIOS_TTY_DRIVER)
2463 #endif /* !TERMIOS_TTY_DRIVER */
2465 #if defined (HAVE_POSIX_SIGNALS)
2468 # if defined (HAVE_BSD_SIGNALS)
2470 # endif /* HAVE_BSD_SIGNALS */
2471 #endif /* !HAVE_POSIX_SIGNALS */
2473 if (terminal_prepped)
2476 /* Try to keep this function from being INTerrupted. We can do it
2477 on POSIX and systems with BSD-like signal handling. */
2478 #if defined (HAVE_POSIX_SIGNALS)
2480 sigaddset (&set, SIGINT);
2481 sigprocmask (SIG_BLOCK, &set, &oset);
2482 #else /* !HAVE_POSIX_SIGNALS */
2483 # if defined (HAVE_BSD_SIGNALS)
2484 oldmask = sigblock (sigmask (SIGINT));
2485 # endif /* HAVE_BSD_SIGNALS */
2486 #endif /* !HAVE_POSIX_SIGNALS */
2488 #if defined (TERMIOS_TTY_DRIVER)
2489 tcgetattr (tty, &tio);
2491 ioctl (tty, TCGETA, &tio);
2492 #endif /* !TERMIOS_TTY_DRIVER */
2496 readline_echoing_p = (tio.c_lflag & ECHO);
2498 tio.c_lflag &= ~(ICANON|ECHO);
2500 if (otio.c_cc[VEOF] != _POSIX_VDISABLE)
2501 eof_char = otio.c_cc[VEOF];
2503 #if defined (USE_XON_XOFF)
2505 tio.c_iflag &= ~(IXON|IXOFF|IXANY);
2507 /* `strict' Posix systems do not define IXANY. */
2508 tio.c_iflag &= ~(IXON|IXOFF);
2510 #endif /* USE_XON_XOFF */
2512 /* Only turn this off if we are using all 8 bits. */
2514 tio.c_iflag &= ~(ISTRIP | INPCK);
2516 /* Make sure we differentiate between CR and NL on input. */
2517 tio.c_iflag &= ~(ICRNL | INLCR);
2519 #if !defined (HANDLE_SIGNALS)
2520 tio.c_lflag &= ~ISIG;
2522 tio.c_lflag |= ISIG;
2526 tio.c_cc[VTIME] = 0;
2528 /* Turn off characters that we need on Posix systems with job control,
2529 just to be sure. This includes ^Y and ^V. This should not really
2531 #if defined (TERMIOS_TTY_DRIVER) && defined (_POSIX_JOB_CONTROL)
2533 #if defined (VLNEXT)
2534 tio.c_cc[VLNEXT] = _POSIX_VDISABLE;
2537 #if defined (VDSUSP)
2538 tio.c_cc[VDSUSP] = _POSIX_VDISABLE;
2541 #endif /* POSIX && JOB_CONTROL */
2543 #if defined (TERMIOS_TTY_DRIVER)
2544 tcsetattr (tty, TCSADRAIN, &tio);
2545 tcflow (tty, TCOON); /* Simulate a ^Q. */
2547 ioctl (tty, TCSETAW, &tio);
2548 ioctl (tty, TCXONC, 1); /* Simulate a ^Q. */
2549 #endif /* !TERMIOS_TTY_DRIVER */
2551 terminal_prepped = 1;
2553 #if defined (HAVE_POSIX_SIGNALS)
2554 sigprocmask (SIG_SETMASK, &oset, (sigset_t *)NULL);
2556 # if defined (HAVE_BSD_SIGNALS)
2557 sigsetmask (oldmask);
2558 # endif /* HAVE_BSD_SIGNALS */
2559 #endif /* !HAVE_POSIX_SIGNALS */
2560 #endif /* !__GO32__ */
2564 rl_deprep_terminal ()
2567 int tty = fileno (rl_instream);
2569 /* Try to keep this function from being INTerrupted. We can do it
2570 on POSIX and systems with BSD-like signal handling. */
2571 #if defined (HAVE_POSIX_SIGNALS)
2573 #else /* !HAVE_POSIX_SIGNALS */
2574 # if defined (HAVE_BSD_SIGNALS)
2576 # endif /* HAVE_BSD_SIGNALS */
2577 #endif /* !HAVE_POSIX_SIGNALS */
2579 if (!terminal_prepped)
2582 #if defined (HAVE_POSIX_SIGNALS)
2584 sigaddset (&set, SIGINT);
2585 sigprocmask (SIG_BLOCK, &set, &oset);
2586 #else /* !HAVE_POSIX_SIGNALS */
2587 # if defined (HAVE_BSD_SIGNALS)
2588 oldmask = sigblock (sigmask (SIGINT));
2589 # endif /* HAVE_BSD_SIGNALS */
2590 #endif /* !HAVE_POSIX_SIGNALS */
2592 #if defined (TERMIOS_TTY_DRIVER)
2593 tcsetattr (tty, TCSADRAIN, &otio);
2594 tcflow (tty, TCOON); /* Simulate a ^Q. */
2595 #else /* TERMIOS_TTY_DRIVER */
2596 ioctl (tty, TCSETAW, &otio);
2597 ioctl (tty, TCXONC, 1); /* Simulate a ^Q. */
2598 #endif /* !TERMIOS_TTY_DRIVER */
2600 terminal_prepped = 0;
2602 #if defined (HAVE_POSIX_SIGNALS)
2603 sigprocmask (SIG_SETMASK, &oset, (sigset_t *)NULL);
2604 #else /* !HAVE_POSIX_SIGNALS */
2605 # if defined (HAVE_BSD_SIGNALS)
2606 sigsetmask (oldmask);
2607 # endif /* HAVE_BSD_SIGNALS */
2608 #endif /* !HAVE_POSIX_SIGNALS */
2609 #endif /* !__GO32__ */
2611 #endif /* NEW_TTY_DRIVER */
2614 /* **************************************************************** */
2616 /* Utility Functions */
2618 /* **************************************************************** */
2620 /* Return 0 if C is not a member of the class of characters that belong
2621 in words, or 1 if it is. */
2623 int allow_pathname_alphabetic_chars = 0;
2624 char *pathname_alphabetic_chars = "/-_=~.#$";
2630 if (pure_alphabetic (c) || (numeric (c)))
2633 if (allow_pathname_alphabetic_chars)
2634 return ((int)rindex (pathname_alphabetic_chars, c));
2639 /* Return non-zero if C is a numeric character. */
2644 return (c >= '0' && c <= '9');
2647 /* Ring the terminal bell. */
2651 if (readline_echoing_p)
2654 if (prefer_visible_bell && visible_bell)
2655 tputs (visible_bell, 1, output_character_function);
2657 #endif /* !__GO32__ */
2659 fprintf (stderr, "\007");
2666 /* How to abort things. */
2670 rl_clear_message ();
2671 rl_init_argument ();
2672 rl_pending_input = 0;
2674 defining_kbd_macro = 0;
2675 while (executing_macro)
2676 pop_executing_macro ();
2678 rl_last_func = (Function *)NULL;
2679 longjmp (readline_top_level, 1);
2682 /* Return a copy of the string between FROM and TO.
2683 FROM is inclusive, TO is not. */
2684 #if defined (sun) /* Yes, that's right, some crufty function in sunview is
2685 called rl_copy (). */
2692 register int length;
2695 /* Fix it if the caller is confused. */
2704 copy = (char *)xmalloc (1 + length);
2705 strncpy (copy, the_line + from, length);
2706 copy[length] = '\0';
2710 /* Increase the size of RL_LINE_BUFFER until it has enough space to hold
2713 rl_extend_line_buffer (len)
2716 while (len >= rl_line_buffer_len)
2719 (rl_line_buffer, rl_line_buffer_len += DEFAULT_BUFFER_SIZE);
2721 the_line = rl_line_buffer;
2725 /* **************************************************************** */
2727 /* Insert and Delete */
2729 /* **************************************************************** */
2731 /* Insert a string of text into the line at point. This is the only
2732 way that you should do insertion. rl_insert () calls this
2734 rl_insert_text (string)
2737 extern int doing_an_undo;
2738 register int i, l = strlen (string);
2740 if (rl_end + l >= rl_line_buffer_len)
2741 rl_extend_line_buffer (rl_end + l);
2743 for (i = rl_end; i >= rl_point; i--)
2744 the_line[i + l] = the_line[i];
2745 strncpy (the_line + rl_point, string, l);
2747 /* Remember how to undo this if we aren't undoing something. */
2750 /* If possible and desirable, concatenate the undos. */
2751 if ((strlen (string) == 1) &&
2753 (rl_undo_list->what == UNDO_INSERT) &&
2754 (rl_undo_list->end == rl_point) &&
2755 (rl_undo_list->end - rl_undo_list->start < 20))
2756 rl_undo_list->end++;
2758 rl_add_undo (UNDO_INSERT, rl_point, rl_point + l, (char *)NULL);
2762 the_line[rl_end] = '\0';
2765 /* Delete the string between FROM and TO. FROM is
2766 inclusive, TO is not. */
2767 rl_delete_text (from, to)
2770 extern int doing_an_undo;
2771 register char *text;
2773 /* Fix it if the caller is confused. */
2780 text = rl_copy (from, to);
2781 strncpy (the_line + from, the_line + to, rl_end - to);
2783 /* Remember how to undo this delete. */
2785 rl_add_undo (UNDO_DELETE, from, to, text);
2789 rl_end -= (to - from);
2790 the_line[rl_end] = '\0';
2794 /* **************************************************************** */
2796 /* Readline character functions */
2798 /* **************************************************************** */
2800 /* This is not a gap editor, just a stupid line input routine. No hair
2801 is involved in writing any of the functions, and none should be. */
2805 rl_end is the place in the string that we would place '\0';
2806 i.e., it is always safe to place '\0' there.
2808 rl_point is the place in the string where the cursor is. Sometimes
2809 this is the same as rl_end.
2811 Any command that is called interactively receives two arguments.
2812 The first is a count: the numeric arg pased to this command.
2813 The second is the key which invoked this command.
2817 /* **************************************************************** */
2819 /* Movement Commands */
2821 /* **************************************************************** */
2823 /* Note that if you `optimize' the display for these functions, you cannot
2824 use said functions in other functions which do not do optimizing display.
2825 I.e., you will have to update the data base for rl_redisplay, and you
2826 might as well let rl_redisplay do that job. */
2828 /* Move forward COUNT characters. */
2833 rl_backward (-count);
2837 #if defined (VI_MODE)
2838 if (rl_point == (rl_end - (rl_editing_mode == vi_mode)))
2840 if (rl_point == rl_end)
2841 #endif /* VI_MODE */
2852 /* Move backward COUNT characters. */
2857 rl_forward (-count);
2872 /* Move to the beginning of the line. */
2878 /* Move to the end of the line. */
2884 /* Move forward a word. We do what Emacs does. */
2885 rl_forward_word (count)
2892 rl_backward_word (-count);
2898 if (rl_point == rl_end)
2901 /* If we are not in a word, move forward until we are in one.
2902 Then, move forward until we hit a non-alphabetic character. */
2903 c = the_line[rl_point];
2904 if (!alphabetic (c))
2906 while (++rl_point < rl_end)
2908 c = the_line[rl_point];
2909 if (alphabetic (c)) break;
2912 if (rl_point == rl_end) return;
2913 while (++rl_point < rl_end)
2915 c = the_line[rl_point];
2916 if (!alphabetic (c)) break;
2922 /* Move backward a word. We do what Emacs does. */
2923 rl_backward_word (count)
2930 rl_forward_word (-count);
2939 /* Like rl_forward_word (), except that we look at the characters
2940 just before point. */
2942 c = the_line[rl_point - 1];
2943 if (!alphabetic (c))
2947 c = the_line[rl_point - 1];
2948 if (alphabetic (c)) break;
2954 c = the_line[rl_point - 1];
2955 if (!alphabetic (c))
2963 /* Clear the current line. Numeric argument to C-l does this. */
2966 int curr_line = last_c_pos / screenwidth;
2967 extern char *term_clreol;
2969 move_vert(curr_line);
2970 move_cursor_relative (0, the_line); /* XXX is this right */
2975 ScreenGetCursor(&r, &c);
2977 memset(ScreenPrimary+r*w+c, 0, (w-c)*2);
2979 #else /* __GO32__ */
2981 tputs (term_clreol, 1, output_character_function);
2982 #endif /* __GO32__/else */
2984 rl_forced_update_display ();
2985 rl_display_fixed = 1;
2988 /* C-l typed to a line without quoting clears the screen, and then reprints
2989 the prompt and the current input line. Given a numeric arg, redraw only
2990 the current line. */
2993 extern char *term_clrpag;
2995 if (rl_explicit_arg)
3003 tputs (term_clrpag, 1, output_character_function);
3005 #endif /* !__GO32__ */
3008 rl_forced_update_display ();
3009 rl_display_fixed = 1;
3012 rl_arrow_keys (count, c)
3017 ch = rl_read_key ();
3019 switch (to_upper (ch))
3022 rl_get_previous_history (count);
3026 rl_get_next_history (count);
3034 rl_backward (count);
3043 /* **************************************************************** */
3047 /* **************************************************************** */
3049 /* Insert the character C at the current location, moving point forward. */
3050 rl_insert (count, c)
3059 /* If we can optimize, then do it. But don't let people crash
3060 readline because of extra large arguments. */
3061 if (count > 1 && count < 1024)
3063 string = (char *)alloca (1 + count);
3065 for (i = 0; i < count; i++)
3069 rl_insert_text (string);
3077 string = (char *)alloca (1024 + 1);
3079 for (i = 0; i < 1024; i++)
3084 decreaser = (count > 1024 ? 1024 : count);
3085 string[decreaser] = '\0';
3086 rl_insert_text (string);
3092 /* We are inserting a single character.
3093 If there is pending input, then make a string of all of the
3094 pending characters that are bound to rl_insert, and insert
3101 string = (char *)alloca (ibuffer_len + 1);
3104 while ((t = rl_get_char (&key)) &&
3105 (keymap[key].type == ISFUNC &&
3106 keymap[key].function == rl_insert))
3110 rl_unget_char (key);
3113 rl_insert_text (string);
3118 /* Inserting a single character. */
3119 string = (char *)alloca (2);
3123 rl_insert_text (string);
3127 /* Insert the next typed character verbatim. */
3128 rl_quoted_insert (count)
3131 int c = rl_read_key ();
3132 rl_insert (count, c);
3135 /* Insert a tab character. */
3136 rl_tab_insert (count)
3139 rl_insert (count, '\t');
3142 /* What to do when a NEWLINE is pressed. We accept the whole line.
3143 KEY is the key that invoked this command. I guess it could have
3144 meaning in the future. */
3145 rl_newline (count, key)
3151 #if defined (VI_MODE)
3153 extern int vi_doing_insert;
3154 if (vi_doing_insert)
3156 rl_end_undo_group ();
3157 vi_doing_insert = 0;
3160 #endif /* VI_MODE */
3162 if (readline_echoing_p)
3164 move_vert (vis_botlin);
3167 fflush (out_stream);
3172 rl_clean_up_for_exit ()
3174 if (readline_echoing_p)
3176 move_vert (vis_botlin);
3178 fflush (out_stream);
3179 rl_restart_output ();
3183 /* What to do for some uppercase characters, like meta characters,
3184 and some characters appearing in emacs_ctlx_keymap. This function
3185 is just a stub, you bind keys to it and the code in rl_dispatch ()
3186 is special cased. */
3187 rl_do_lowercase_version (ignore1, ignore2)
3188 int ignore1, ignore2;
3192 /* Rubout the character behind point. */
3210 int orig_point = rl_point;
3211 rl_backward (count);
3212 rl_kill_text (orig_point, rl_point);
3216 int c = the_line[--rl_point];
3217 rl_delete_text (rl_point, rl_point + 1);
3219 if (rl_point == rl_end && alphabetic (c) && last_c_pos)
3222 putc (' ', out_stream);
3225 visible_line[last_c_pos] = '\0';
3231 /* Delete the character under the cursor. Given a numeric argument,
3232 kill that many characters instead. */
3233 rl_delete (count, invoking_key)
3234 int count, invoking_key;
3242 if (rl_point == rl_end)
3250 int orig_point = rl_point;
3252 rl_kill_text (orig_point, rl_point);
3253 rl_point = orig_point;
3256 rl_delete_text (rl_point, rl_point + 1);
3260 /* **************************************************************** */
3264 /* **************************************************************** */
3266 /* The next two functions mimic unix line editing behaviour, except they
3267 save the deleted text on the kill ring. This is safer than not saving
3268 it, and since we have a ring, nobody should get screwed. */
3270 /* This does what C-w does in Unix. We can't prevent people from
3271 using behaviour that they expect. */
3272 rl_unix_word_rubout ()
3274 if (!rl_point) ding ();
3276 int orig_point = rl_point;
3277 while (rl_point && whitespace (the_line[rl_point - 1]))
3279 while (rl_point && !whitespace (the_line[rl_point - 1]))
3281 rl_kill_text (rl_point, orig_point);
3285 /* Here is C-u doing what Unix does. You don't *have* to use these
3286 key-bindings. We have a choice of killing the entire line, or
3287 killing from where we are to the start of the line. We choose the
3288 latter, because if you are a Unix weenie, then you haven't backspaced
3289 into the line at all, and if you aren't, then you know what you are
3291 rl_unix_line_discard ()
3293 if (!rl_point) ding ();
3295 rl_kill_text (rl_point, 0);
3302 /* **************************************************************** */
3304 /* Commands For Typos */
3306 /* **************************************************************** */
3308 /* Random and interesting things in here. */
3310 /* **************************************************************** */
3314 /* **************************************************************** */
3316 /* The three kinds of things that we know how to do. */
3321 /* Uppercase the word at point. */
3322 rl_upcase_word (count)
3325 rl_change_case (count, UpCase);
3328 /* Lowercase the word at point. */
3329 rl_downcase_word (count)
3332 rl_change_case (count, DownCase);
3335 /* Upcase the first letter, downcase the rest. */
3336 rl_capitalize_word (count)
3339 rl_change_case (count, CapCase);
3342 /* The meaty function.
3343 Change the case of COUNT words, performing OP on them.
3344 OP is one of UpCase, DownCase, or CapCase.
3345 If a negative argument is given, leave point where it started,
3346 otherwise, leave it where it moves to. */
3347 rl_change_case (count, op)
3350 register int start = rl_point, end;
3353 rl_forward_word (count);
3363 /* We are going to modify some text, so let's prepare to undo it. */
3364 rl_modifying (start, end);
3366 for (; start < end; start++)
3371 the_line[start] = to_upper (the_line[start]);
3375 the_line[start] = to_lower (the_line[start]);
3381 the_line[start] = to_upper (the_line[start]);
3386 the_line[start] = to_lower (the_line[start]);
3388 if (!pure_alphabetic (the_line[start]))
3399 /* **************************************************************** */
3403 /* **************************************************************** */
3405 /* Transpose the words at point. */
3406 rl_transpose_words (count)
3409 char *word1, *word2;
3410 int w1_beg, w1_end, w2_beg, w2_end;
3411 int orig_point = rl_point;
3415 /* Find the two words. */
3416 rl_forward_word (count);
3418 rl_backward_word (1);
3420 rl_backward_word (count);
3422 rl_forward_word (1);
3425 /* Do some check to make sure that there really are two words. */
3426 if ((w1_beg == w2_beg) || (w2_beg < w1_end))
3429 rl_point = orig_point;
3433 /* Get the text of the words. */
3434 word1 = rl_copy (w1_beg, w1_end);
3435 word2 = rl_copy (w2_beg, w2_end);
3437 /* We are about to do many insertions and deletions. Remember them
3438 as one operation. */
3439 rl_begin_undo_group ();
3441 /* Do the stuff at word2 first, so that we don't have to worry
3442 about word1 moving. */
3444 rl_delete_text (w2_beg, w2_end);
3445 rl_insert_text (word1);
3448 rl_delete_text (w1_beg, w1_end);
3449 rl_insert_text (word2);
3451 /* This is exactly correct since the text before this point has not
3452 changed in length. */
3455 /* I think that does it. */
3456 rl_end_undo_group ();
3457 free (word1); free (word2);
3460 /* Transpose the characters at point. If point is at the end of the line,
3461 then transpose the characters before point. */
3462 rl_transpose_chars (count)
3468 if (!rl_point || rl_end < 2) {
3475 if (rl_point == rl_end)
3477 int t = the_line[rl_point - 1];
3479 the_line[rl_point - 1] = the_line[rl_point - 2];
3480 the_line[rl_point - 2] = t;
3484 int t = the_line[rl_point];
3486 the_line[rl_point] = the_line[rl_point - 1];
3487 the_line[rl_point - 1] = t;
3489 if (count < 0 && rl_point)
3503 /* **************************************************************** */
3505 /* Bogus Flow Control */
3507 /* **************************************************************** */
3509 rl_restart_output (count, key)
3512 int fildes = fileno (rl_outstream);
3513 #if defined (TIOCSTART)
3514 #if defined (apollo)
3515 ioctl (&fildes, TIOCSTART, 0);
3517 ioctl (fildes, TIOCSTART, 0);
3521 # if defined (TERMIOS_TTY_DRIVER)
3522 tcflow (fildes, TCOON);
3524 # if defined (TCXONC)
3525 ioctl (fildes, TCXONC, TCOON);
3526 # endif /* TCXONC */
3527 # endif /* !TERMIOS_TTY_DRIVER */
3528 #endif /* TIOCSTART */
3531 rl_stop_output (count, key)
3534 int fildes = fileno (rl_instream);
3536 #if defined (TIOCSTOP)
3537 # if defined (apollo)
3538 ioctl (&fildes, TIOCSTOP, 0);
3540 ioctl (fildes, TIOCSTOP, 0);
3541 # endif /* apollo */
3543 # if defined (TERMIOS_TTY_DRIVER)
3544 tcflow (fildes, TCOOFF);
3546 # if defined (TCXONC)
3547 ioctl (fildes, TCXONC, TCOON);
3548 # endif /* TCXONC */
3549 # endif /* !TERMIOS_TTY_DRIVER */
3550 #endif /* TIOCSTOP */
3553 /* **************************************************************** */
3555 /* Completion matching, from readline's point of view. */
3557 /* **************************************************************** */
3559 /* Pointer to the generator function for completion_matches ().
3560 NULL means to use filename_entry_function (), the default filename
3562 Function *rl_completion_entry_function = (Function *)NULL;
3564 /* Pointer to alternative function to create matches.
3565 Function is called with TEXT, START, and END.
3566 START and END are indices in RL_LINE_BUFFER saying what the boundaries
3568 If this function exists and returns NULL then call the value of
3569 rl_completion_entry_function to try to match, otherwise use the
3570 array of strings returned. */
3571 Function *rl_attempted_completion_function = (Function *)NULL;
3573 /* Local variable states what happened during the last completion attempt. */
3574 static int completion_changed_buffer = 0;
3576 /* Complete the word at or before point. You have supplied the function
3577 that does the initial simple matching selection algorithm (see
3578 completion_matches ()). The default is to do filename completion. */
3580 rl_complete (ignore, invoking_key)
3581 int ignore, invoking_key;
3583 if (rl_last_func == rl_complete && !completion_changed_buffer)
3584 rl_complete_internal ('?');
3586 rl_complete_internal (TAB);
3589 /* List the possible completions. See description of rl_complete (). */
3590 rl_possible_completions ()
3592 rl_complete_internal ('?');
3595 /* The user must press "y" or "n". Non-zero return means "y" pressed. */
3601 if (c == 'y' || c == 'Y') return (1);
3602 if (c == 'n' || c == 'N') return (0);
3603 if (c == ABORT_CHAR) rl_abort ();
3607 /* Up to this many items will be displayed in response to a
3608 possible-completions call. After that, we ask the user if
3609 she is sure she wants to see them all. */
3610 int rl_completion_query_items = 100;
3612 /* The basic list of characters that signal a break between words for the
3613 completer routine. The contents of this variable is what breaks words
3614 in the shell, i.e. " \t\n\"\\'`@$><=" */
3615 char *rl_basic_word_break_characters = " \t\n\"\\'`@$><=;|&{(";
3617 /* The list of characters that signal a break between words for
3618 rl_complete_internal. The default list is the contents of
3619 rl_basic_word_break_characters. */
3620 char *rl_completer_word_break_characters = (char *)NULL;
3622 /* List of characters that are word break characters, but should be left
3623 in TEXT when it is passed to the completion function. The shell uses
3624 this to help determine what kind of completing to do. */
3625 char *rl_special_prefixes = (char *)NULL;
3627 /* If non-zero, then disallow duplicates in the matches. */
3628 int rl_ignore_completion_duplicates = 1;
3630 /* Non-zero means that the results of the matches are to be treated
3631 as filenames. This is ALWAYS zero on entry, and can only be changed
3632 within a completion entry finder function. */
3633 int rl_filename_completion_desired = 0;
3635 /* This function, if defined, is called by the completer when real
3636 filename completion is done, after all the matching names have been
3637 generated. It is passed a (char**) known as matches in the code below.
3638 It consists of a NULL-terminated array of pointers to potential
3639 matching strings. The 1st element (matches[0]) is the maximal
3640 substring that is common to all matches. This function can re-arrange
3641 the list of matches as required, but all elements of the array must be
3642 free()'d if they are deleted. The main intent of this function is
3643 to implement FIGNORE a la SunOS csh. */
3644 Function *rl_ignore_some_completions_function = (Function *)NULL;
3646 /* Complete the word at or before point.
3647 WHAT_TO_DO says what to do with the completion.
3648 `?' means list the possible completions.
3649 TAB means do standard completion.
3650 `*' means insert all of the possible completions. */
3651 rl_complete_internal (what_to_do)
3654 char *filename_completion_function ();
3655 char **completion_matches (), **matches;
3657 int start, end, delimiter = 0;
3658 char *text, *saved_line_buffer;
3661 saved_line_buffer = savestring (the_line);
3663 saved_line_buffer = (char *)NULL;
3665 if (rl_completion_entry_function)
3666 our_func = rl_completion_entry_function;
3668 our_func = (int (*)())filename_completion_function;
3670 /* Only the completion entry function can change this. */
3671 rl_filename_completion_desired = 0;
3673 /* We now look backwards for the start of a filename/variable word. */
3678 while (--rl_point &&
3679 !rindex (rl_completer_word_break_characters, the_line[rl_point]));
3681 /* If we are at a word break, then advance past it. */
3682 if (rindex (rl_completer_word_break_characters, the_line[rl_point]))
3684 /* If the character that caused the word break was a quoting
3685 character, then remember it as the delimiter. */
3686 if (rindex ("\"'", the_line[rl_point]) && (end - rl_point) > 1)
3687 delimiter = the_line[rl_point];
3689 /* If the character isn't needed to determine something special
3690 about what kind of completion to perform, then advance past it. */
3692 if (!rl_special_prefixes ||
3693 !rindex (rl_special_prefixes, the_line[rl_point]))
3700 text = rl_copy (start, end);
3702 /* If the user wants to TRY to complete, but then wants to give
3703 up and use the default completion function, they set the
3704 variable rl_attempted_completion_function. */
3705 if (rl_attempted_completion_function)
3708 (char **)(*rl_attempted_completion_function) (text, start, end);
3712 our_func = (Function *)NULL;
3713 goto after_usual_completion;
3717 matches = completion_matches (text, our_func);
3719 after_usual_completion:
3730 /* It seems to me that in all the cases we handle we would like
3731 to ignore duplicate possiblilities. Scan for the text to
3732 insert being identical to the other completions. */
3733 if (rl_ignore_completion_duplicates)
3735 char *lowest_common;
3738 /* Sort the items. */
3739 /* It is safe to sort this array, because the lowest common
3740 denominator found in matches[0] will remain in place. */
3741 for (i = 0; matches[i]; i++);
3742 qsort (matches, i, sizeof (char *), compare_strings);
3744 /* Remember the lowest common denominator for it may be unique. */
3745 lowest_common = savestring (matches[0]);
3747 for (i = 0; matches[i + 1]; i++)
3749 if (strcmp (matches[i], matches[i + 1]) == 0)
3752 matches[i] = (char *)-1;
3758 /* We have marked all the dead slots with (char *)-1.
3759 Copy all the non-dead entries into a new array. */
3762 (char **)malloc ((3 + newlen) * sizeof (char *));
3764 for (i = 1, j = 1; matches[i]; i++)
3766 if (matches[i] != (char *)-1)
3767 temp_array[j++] = matches[i];
3770 temp_array[j] = (char *)NULL;
3772 if (matches[0] != (char *)-1)
3777 matches = temp_array;
3780 /* Place the lowest common denominator back in [0]. */
3781 matches[0] = lowest_common;
3783 /* If there is one string left, and it is identical to the
3784 lowest common denominator, then the LCD is the string to
3786 if (j == 2 && strcmp (matches[0], matches[1]) == 0)
3789 matches[1] = (char *)NULL;
3796 /* If we are matching filenames, then here is our chance to
3797 do clever processing by re-examining the list. Call the
3798 ignore function with the array as a parameter. It can
3799 munge the array, deleting matches as it desires. */
3800 if (rl_ignore_some_completions_function &&
3801 our_func == (int (*)())filename_completion_function)
3802 (void)(*rl_ignore_some_completions_function)(matches);
3806 rl_delete_text (start, rl_point);
3808 rl_insert_text (matches[0]);
3811 /* If there are more matches, ring the bell to indicate.
3812 If this was the only match, and we are hacking files,
3813 check the file to see if it was a directory. If so,
3814 add a '/' to the name. If not, and we are at the end
3815 of the line, then add a space. */
3818 ding (); /* There are other matches remaining. */
3822 char temp_string[2];
3824 temp_string[0] = delimiter ? delimiter : ' ';
3825 temp_string[1] = '\0';
3827 if (rl_filename_completion_desired)
3830 char *filename = tilde_expand (matches[0]);
3832 if ((stat (filename, &finfo) == 0) &&
3833 S_ISDIR (finfo.st_mode))
3835 if (the_line[rl_point] != '/')
3836 rl_insert_text ("/");
3840 if (rl_point == rl_end)
3841 rl_insert_text (temp_string);
3847 if (rl_point == rl_end)
3848 rl_insert_text (temp_string);
3857 rl_delete_text (start, rl_point);
3859 rl_begin_undo_group ();
3864 rl_insert_text (matches[i++]);
3865 rl_insert_text (" ");
3870 rl_insert_text (matches[0]);
3871 rl_insert_text (" ");
3873 rl_end_undo_group ();
3879 int len, count, limit, max = 0;
3882 /* Handle simple case first. What if there is only one answer? */
3887 if (rl_filename_completion_desired)
3888 temp = rindex (matches[0], '/');
3890 temp = (char *)NULL;
3898 fprintf (out_stream, "%s", temp);
3903 /* There is more than one answer. Find out how many there are,
3904 and find out what the maximum printed length of a single entry
3906 for (i = 1; matches[i]; i++)
3908 char *temp = (char *)NULL;
3910 /* If we are hacking filenames, then only count the characters
3911 after the last slash in the pathname. */
3912 if (rl_filename_completion_desired)
3913 temp = rindex (matches[i], '/');
3915 temp = (char *)NULL;
3922 if (strlen (temp) > max)
3923 max = strlen (temp);
3928 /* If there are many items, then ask the user if she
3929 really wants to see them all. */
3930 if (len >= rl_completion_query_items)
3933 fprintf (out_stream,
3934 "There are %d possibilities. Do you really", len);
3936 fprintf (out_stream, "wish to see them all? (y or n)");
3937 fflush (out_stream);
3944 /* How many items of MAX length can we fit in the screen window? */
3946 limit = screenwidth / max;
3947 if (limit != 1 && (limit * max == screenwidth))
3950 /* Avoid a possible floating exception. If max > screenwidth,
3951 limit will be 0 and a divide-by-zero fault will result. */
3955 /* How many iterations of the printing loop? */
3956 count = (len + (limit - 1)) / limit;
3958 /* Watch out for special case. If LEN is less than LIMIT, then
3959 just do the inner printing loop. */
3960 if (len < limit) count = 1;
3962 /* Sort the items if they are not already sorted. */
3963 if (!rl_ignore_completion_duplicates)
3964 qsort (matches, len, sizeof (char *), compare_strings);
3966 /* Print the sorted items, up-and-down alphabetically, like
3970 for (i = 1; i < count + 1; i++)
3972 for (j = 0, l = i; j < limit; j++)
3974 if (l > len || !matches[l])
3980 char *temp = (char *)NULL;
3982 if (rl_filename_completion_desired)
3983 temp = rindex (matches[l], '/');
3985 temp = (char *)NULL;
3992 fprintf (out_stream, "%s", temp);
3993 for (k = 0; k < max - strlen (temp); k++)
3994 putc (' ', out_stream);
4010 for (i = 0; matches[i]; i++)
4015 /* Check to see if the line has changed through all of this manipulation. */
4016 if (saved_line_buffer)
4018 if (strcmp (the_line, saved_line_buffer) != 0)
4019 completion_changed_buffer = 1;
4021 completion_changed_buffer = 0;
4023 free (saved_line_buffer);
4027 /* Stupid comparison routine for qsort () ing strings. */
4029 compare_strings (s1, s2)
4032 return (strcmp (*s1, *s2));
4035 /* A completion function for usernames.
4036 TEXT contains a partial username preceded by a random
4037 character (usually `~'). */
4039 username_completion_function (text, state)
4044 return (char *)NULL;
4045 #else /* !__GO32__ */
4046 static char *username = (char *)NULL;
4047 static struct passwd *entry;
4048 static int namelen, first_char, first_char_loc;
4057 if (first_char == '~')
4062 username = savestring (&text[first_char_loc]);
4063 namelen = strlen (username);
4067 while (entry = getpwent ())
4069 if (strncmp (username, entry->pw_name, namelen) == 0)
4076 return ((char *)NULL);
4080 char *value = (char *)xmalloc (2 + strlen (entry->pw_name));
4084 strcpy (value + first_char_loc, entry->pw_name);
4086 if (first_char == '~')
4087 rl_filename_completion_desired = 1;
4091 #endif /* !__GO32__ */
4094 /* **************************************************************** */
4096 /* Undo, and Undoing */
4098 /* **************************************************************** */
4100 /* Non-zero tells rl_delete_text and rl_insert_text to not add to
4102 int doing_an_undo = 0;
4104 /* The current undo list for THE_LINE. */
4105 UNDO_LIST *rl_undo_list = (UNDO_LIST *)NULL;
4107 /* Remember how to undo something. Concatenate some undos if that
4109 rl_add_undo (what, start, end, text)
4110 enum undo_code what;
4114 UNDO_LIST *temp = (UNDO_LIST *)xmalloc (sizeof (UNDO_LIST));
4116 temp->start = start;
4119 temp->next = rl_undo_list;
4120 rl_undo_list = temp;
4123 /* Free the existing undo list. */
4126 while (rl_undo_list) {
4127 UNDO_LIST *release = rl_undo_list;
4128 rl_undo_list = rl_undo_list->next;
4130 if (release->what == UNDO_DELETE)
4131 free (release->text);
4137 /* Undo the next thing in the list. Return 0 if there
4138 is nothing to undo, or non-zero if there was. */
4143 int waiting_for_begin = 0;
4151 switch (rl_undo_list->what) {
4153 /* Undoing deletes means inserting some text. */
4155 rl_point = rl_undo_list->start;
4156 rl_insert_text (rl_undo_list->text);
4157 free (rl_undo_list->text);
4160 /* Undoing inserts means deleting some text. */
4162 rl_delete_text (rl_undo_list->start, rl_undo_list->end);
4163 rl_point = rl_undo_list->start;
4166 /* Undoing an END means undoing everything 'til we get to
4169 waiting_for_begin++;
4172 /* Undoing a BEGIN means that we are done with this group. */
4174 if (waiting_for_begin)
4175 waiting_for_begin--;
4183 release = rl_undo_list;
4184 rl_undo_list = rl_undo_list->next;
4187 if (waiting_for_begin)
4193 /* Begin a group. Subsequent undos are undone as an atomic operation. */
4194 rl_begin_undo_group ()
4196 rl_add_undo (UNDO_BEGIN, 0, 0, 0);
4199 /* End an undo group started with rl_begin_undo_group (). */
4200 rl_end_undo_group ()
4202 rl_add_undo (UNDO_END, 0, 0, 0);
4205 /* Save an undo entry for the text from START to END. */
4206 rl_modifying (start, end)
4218 char *temp = rl_copy (start, end);
4219 rl_begin_undo_group ();
4220 rl_add_undo (UNDO_DELETE, start, end, temp);
4221 rl_add_undo (UNDO_INSERT, start, end, (char *)NULL);
4222 rl_end_undo_group ();
4226 /* Revert the current line to its previous state. */
4229 if (!rl_undo_list) ding ();
4231 while (rl_undo_list)
4236 /* Do some undoing of things that were done. */
4237 rl_undo_command (count)
4239 if (count < 0) return; /* Nothing to do. */
4255 /* **************************************************************** */
4257 /* History Utilities */
4259 /* **************************************************************** */
4261 /* We already have a history library, and that is what we use to control
4262 the history features of readline. However, this is our local interface
4263 to the history mechanism. */
4265 /* While we are editing the history, this is the saved
4266 version of the original line. */
4267 HIST_ENTRY *saved_line_for_history = (HIST_ENTRY *)NULL;
4269 /* Set the history pointer back to the last entry in the history. */
4270 start_using_history ()
4273 if (saved_line_for_history)
4274 free_history_entry (saved_line_for_history);
4276 saved_line_for_history = (HIST_ENTRY *)NULL;
4279 /* Free the contents (and containing structure) of a HIST_ENTRY. */
4280 free_history_entry (entry)
4289 /* Perhaps put back the current line if it has changed. */
4290 maybe_replace_line ()
4292 HIST_ENTRY *temp = current_history ();
4294 /* If the current line has changed, save the changes. */
4295 if (temp && ((UNDO_LIST *)(temp->data) != rl_undo_list))
4297 temp = replace_history_entry (where_history (), the_line, rl_undo_list);
4303 /* Put back the saved_line_for_history if there is one. */
4304 maybe_unsave_line ()
4306 if (saved_line_for_history)
4310 line_len = strlen (saved_line_for_history->line);
4312 if (line_len >= rl_line_buffer_len)
4313 rl_extend_line_buffer (line_len);
4315 strcpy (the_line, saved_line_for_history->line);
4316 rl_undo_list = (UNDO_LIST *)saved_line_for_history->data;
4317 free_history_entry (saved_line_for_history);
4318 saved_line_for_history = (HIST_ENTRY *)NULL;
4319 rl_end = rl_point = strlen (the_line);
4325 /* Save the current line in saved_line_for_history. */
4328 if (!saved_line_for_history)
4330 saved_line_for_history = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
4331 saved_line_for_history->line = savestring (the_line);
4332 saved_line_for_history->data = (char *)rl_undo_list;
4336 /* **************************************************************** */
4338 /* History Commands */
4340 /* **************************************************************** */
4342 /* Meta-< goes to the start of the history. */
4343 rl_beginning_of_history ()
4345 rl_get_previous_history (1 + where_history ());
4348 /* Meta-> goes to the end of the history. (The current line). */
4349 rl_end_of_history ()
4351 maybe_replace_line ();
4353 maybe_unsave_line ();
4356 /* Move down to the next history line. */
4357 rl_get_next_history (count)
4360 HIST_ENTRY *temp = (HIST_ENTRY *)NULL;
4364 rl_get_previous_history (-count);
4371 maybe_replace_line ();
4375 temp = next_history ();
4382 maybe_unsave_line ();
4387 line_len = strlen (temp->line);
4389 if (line_len >= rl_line_buffer_len)
4390 rl_extend_line_buffer (line_len);
4392 strcpy (the_line, temp->line);
4393 rl_undo_list = (UNDO_LIST *)temp->data;
4394 rl_end = rl_point = strlen (the_line);
4395 #if defined (VI_MODE)
4396 if (rl_editing_mode == vi_mode)
4398 #endif /* VI_MODE */
4402 /* Get the previous item out of our interactive history, making it the current
4403 line. If there is no previous history, just ding. */
4404 rl_get_previous_history (count)
4407 HIST_ENTRY *old_temp = (HIST_ENTRY *)NULL;
4408 HIST_ENTRY *temp = (HIST_ENTRY *)NULL;
4412 rl_get_next_history (-count);
4419 /* If we don't have a line saved, then save this one. */
4422 /* If the current line has changed, save the changes. */
4423 maybe_replace_line ();
4427 temp = previous_history ();
4435 /* If there was a large argument, and we moved back to the start of the
4436 history, that is not an error. So use the last value found. */
4437 if (!temp && old_temp)
4446 line_len = strlen (temp->line);
4448 if (line_len >= rl_line_buffer_len)
4449 rl_extend_line_buffer (line_len);
4451 strcpy (the_line, temp->line);
4452 rl_undo_list = (UNDO_LIST *)temp->data;
4453 rl_end = rl_point = line_len;
4455 #if defined (VI_MODE)
4456 if (rl_editing_mode == vi_mode)
4458 #endif /* VI_MODE */
4463 /* **************************************************************** */
4465 /* I-Search and Searching */
4467 /* **************************************************************** */
4469 /* Search backwards through the history looking for a string which is typed
4470 interactively. Start with the current line. */
4471 rl_reverse_search_history (sign, key)
4475 rl_search_history (-sign, key);
4478 /* Search forwards through the history looking for a string which is typed
4479 interactively. Start with the current line. */
4480 rl_forward_search_history (sign, key)
4484 rl_search_history (sign, key);
4487 /* Display the current state of the search in the echo-area.
4488 SEARCH_STRING contains the string that is being searched for,
4489 DIRECTION is zero for forward, or 1 for reverse,
4490 WHERE is the history list number of the current line. If it is
4491 -1, then this line is the starting one. */
4492 rl_display_search (search_string, reverse_p, where)
4493 char *search_string;
4494 int reverse_p, where;
4496 char *message = (char *)NULL;
4499 (char *)alloca (1 + (search_string ? strlen (search_string) : 0) + 30);
4503 #if defined (NOTDEF)
4505 sprintf (message, "[%d]", where + history_base);
4508 strcat (message, "(");
4511 strcat (message, "reverse-");
4513 strcat (message, "i-search)`");
4516 strcat (message, search_string);
4518 strcat (message, "': ");
4519 rl_message (message, 0, 0);
4523 /* Search through the history looking for an interactively typed string.
4524 This is analogous to i-search. We start the search in the current line.
4525 DIRECTION is which direction to search; >= 0 means forward, < 0 means
4527 rl_search_history (direction, invoking_key)
4531 /* The string that the user types in to search for. */
4532 char *search_string = (char *)alloca (128);
4534 /* The current length of SEARCH_STRING. */
4535 int search_string_index;
4537 /* The list of lines to search through. */
4540 /* The length of LINES. */
4543 /* Where we get LINES from. */
4544 HIST_ENTRY **hlist = history_list ();
4547 int orig_point = rl_point;
4548 int orig_line = where_history ();
4549 int last_found_line = orig_line;
4552 /* The line currently being searched. */
4555 /* Offset in that line. */
4558 /* Non-zero if we are doing a reverse search. */
4559 int reverse = (direction < 0);
4561 /* Create an arrary of pointers to the lines that we want to search. */
4562 maybe_replace_line ();
4564 for (i = 0; hlist[i]; i++);
4566 /* Allocate space for this many lines, +1 for the current input line,
4567 and remember those lines. */
4568 lines = (char **)alloca ((1 + (hlen = i)) * sizeof (char *));
4569 for (i = 0; i < hlen; i++)
4570 lines[i] = hlist[i]->line;
4572 if (saved_line_for_history)
4573 lines[i] = saved_line_for_history->line;
4575 /* So I have to type it in this way instead. */
4579 /* Keep that mips alloca happy. */
4580 alloced_line = (char *)alloca (1 + strlen (the_line));
4581 lines[i] = alloced_line;
4582 strcpy (lines[i], &the_line[0]);
4587 /* The line where we start the search. */
4590 /* Initialize search parameters. */
4591 *search_string = '\0';
4592 search_string_index = 0;
4594 /* Normalize DIRECTION into 1 or -1. */
4600 rl_display_search (search_string, reverse, -1);
4609 /* Hack C to Do What I Mean. */
4611 Function *f = (Function *)NULL;
4613 if (keymap[c].type == ISFUNC)
4615 f = keymap[c].function;
4617 if (f == rl_reverse_search_history)
4618 c = reverse ? -1 : -2;
4619 else if (f == rl_forward_search_history)
4620 c = !reverse ? -1 : -2;
4630 /* case invoking_key: */
4634 /* switch directions */
4636 direction = -direction;
4637 reverse = (direction < 0);
4642 strcpy (the_line, lines[orig_line]);
4643 rl_point = orig_point;
4644 rl_end = strlen (the_line);
4645 rl_clear_message ();
4649 if (c < 32 || c > 126)
4651 rl_execute_next (c);
4657 search_string[search_string_index++] = c;
4658 search_string[search_string_index] = '\0';
4663 if (!search_string_index)
4670 if (index != strlen (sline))
4683 (search_string, sline + index, search_string_index)
4691 register int limit =
4692 (strlen (sline) - search_string_index) + 1;
4694 while (index < limit)
4696 if (strncmp (search_string,
4698 search_string_index) == 0)
4707 /* At limit for direction? */
4708 if ((reverse && i < 0) ||
4709 (!reverse && i == hlen))
4714 index = strlen (sline);
4718 /* If the search string is longer than the current
4720 if (search_string_index > strlen (sline))
4723 /* Start actually searching. */
4725 index -= search_string_index;
4729 /* We cannot find the search string. Ding the bell. */
4731 i = last_found_line;
4735 /* We have found the search string. Just display it. But don't
4736 actually move there in the history list until the user accepts
4741 line_len = strlen (lines[i]);
4743 if (line_len >= rl_line_buffer_len)
4744 rl_extend_line_buffer (line_len);
4746 strcpy (the_line, lines[i]);
4749 last_found_line = i;
4751 (search_string, reverse, (i == orig_line) ? -1 : i);
4758 /* The searching is over. The user may have found the string that she
4759 was looking for, or else she may have exited a failing search. If
4760 INDEX is -1, then that shows that the string searched for was not
4761 found. We use this to determine where to place rl_point. */
4763 int now = last_found_line;
4765 /* First put back the original state. */
4766 strcpy (the_line, lines[orig_line]);
4768 if (now < orig_line)
4769 rl_get_previous_history (orig_line - now);
4771 rl_get_next_history (now - orig_line);
4773 /* If the index of the "matched" string is less than zero, then the
4774 final search string was never matched, so put point somewhere
4777 index = strlen (the_line);
4780 rl_clear_message ();
4784 /* Make C be the next command to be executed. */
4788 rl_pending_input = c;
4791 /* **************************************************************** */
4793 /* Killing Mechanism */
4795 /* **************************************************************** */
4797 /* What we assume for a max number of kills. */
4798 #define DEFAULT_MAX_KILLS 10
4800 /* The real variable to look at to find out when to flush kills. */
4801 int rl_max_kills = DEFAULT_MAX_KILLS;
4803 /* Where to store killed text. */
4804 char **rl_kill_ring = (char **)NULL;
4806 /* Where we are in the kill ring. */
4807 int rl_kill_index = 0;
4809 /* How many slots we have in the kill ring. */
4810 int rl_kill_ring_length = 0;
4812 /* How to say that you only want to save a certain amount
4813 of kill material. */
4814 rl_set_retained_kills (num)
4818 /* The way to kill something. This appends or prepends to the last
4819 kill, if the last command was a kill command. if FROM is less
4820 than TO, then the text is appended, otherwise prepended. If the
4821 last command was not a kill command, then a new slot is made for
4823 rl_kill_text (from, to)
4827 char *text = rl_copy (from, to);
4829 /* Is there anything to kill? */
4833 last_command_was_kill++;
4837 /* Delete the copied text from the line. */
4838 rl_delete_text (from, to);
4840 /* First, find the slot to work with. */
4841 if (!last_command_was_kill)
4843 /* Get a new slot. */
4846 /* If we don't have any defined, then make one. */
4847 rl_kill_ring = (char **)
4848 xmalloc (((rl_kill_ring_length = 1) + 1) * sizeof (char *));
4853 /* We have to add a new slot on the end, unless we have
4854 exceeded the max limit for remembering kills. */
4855 slot = rl_kill_ring_length;
4856 if (slot == rl_max_kills)
4859 free (rl_kill_ring[0]);
4860 for (i = 0; i < slot; i++)
4861 rl_kill_ring[i] = rl_kill_ring[i + 1];
4867 xrealloc (rl_kill_ring,
4868 ((slot = (rl_kill_ring_length += 1)) + 1)
4876 slot = rl_kill_ring_length - 1;
4879 /* If the last command was a kill, prepend or append. */
4880 if (last_command_was_kill && rl_editing_mode != vi_mode)
4882 char *old = rl_kill_ring[slot];
4883 char *new = (char *)xmalloc (1 + strlen (old) + strlen (text));
4897 rl_kill_ring[slot] = new;
4901 rl_kill_ring[slot] = text;
4903 rl_kill_index = slot;
4904 last_command_was_kill++;
4907 /* Now REMEMBER! In order to do prepending or appending correctly, kill
4908 commands always make rl_point's original position be the FROM argument,
4909 and rl_point's extent be the TO argument. */
4911 /* **************************************************************** */
4913 /* Killing Commands */
4915 /* **************************************************************** */
4917 /* Delete the word at point, saving the text in the kill ring. */
4918 rl_kill_word (count)
4921 int orig_point = rl_point;
4924 rl_backward_kill_word (-count);
4927 rl_forward_word (count);
4929 if (rl_point != orig_point)
4930 rl_kill_text (orig_point, rl_point);
4932 rl_point = orig_point;
4936 /* Rubout the word before point, placing it on the kill ring. */
4937 rl_backward_kill_word (count)
4940 int orig_point = rl_point;
4943 rl_kill_word (-count);
4946 rl_backward_word (count);
4948 if (rl_point != orig_point)
4949 rl_kill_text (orig_point, rl_point);
4953 /* Kill from here to the end of the line. If DIRECTION is negative, kill
4954 back to the line start instead. */
4955 rl_kill_line (direction)
4958 int orig_point = rl_point;
4961 rl_backward_kill_line (1);
4965 if (orig_point != rl_point)
4966 rl_kill_text (orig_point, rl_point);
4967 rl_point = orig_point;
4971 /* Kill backwards to the start of the line. If DIRECTION is negative, kill
4972 forwards to the line end instead. */
4973 rl_backward_kill_line (direction)
4976 int orig_point = rl_point;
4987 rl_kill_text (orig_point, rl_point);
4992 /* Yank back the last killed text. This ignores arguments. */
4995 if (!rl_kill_ring) rl_abort ();
4996 rl_insert_text (rl_kill_ring[rl_kill_index]);
4999 /* If the last command was yank, or yank_pop, and the text just
5000 before point is identical to the current kill item, then
5001 delete that text from the line, rotate the index down, and
5002 yank back some other text. */
5007 if (((rl_last_func != rl_yank_pop) && (rl_last_func != rl_yank)) ||
5013 l = strlen (rl_kill_ring[rl_kill_index]);
5014 if (((rl_point - l) >= 0) &&
5015 (strncmp (the_line + (rl_point - l),
5016 rl_kill_ring[rl_kill_index], l) == 0))
5018 rl_delete_text ((rl_point - l), rl_point);
5021 if (rl_kill_index < 0)
5022 rl_kill_index = rl_kill_ring_length - 1;
5030 /* Yank the COUNTth argument from the previous history line. */
5031 rl_yank_nth_arg (count, ignore)
5034 register HIST_ENTRY *entry = previous_history ();
5045 arg = history_arg_extract (count, count, entry->line);
5052 rl_begin_undo_group ();
5054 #if defined (VI_MODE)
5055 /* Vi mode always inserts a space befoe yanking the argument, and it
5056 inserts it right *after* rl_point. */
5057 if (rl_editing_mode == vi_mode)
5059 #endif /* VI_MODE */
5061 if (rl_point && the_line[rl_point - 1] != ' ')
5062 rl_insert_text (" ");
5064 rl_insert_text (arg);
5067 rl_end_undo_group ();
5070 /* How to toggle back and forth between editing modes. */
5071 rl_vi_editing_mode ()
5073 #if defined (VI_MODE)
5074 rl_editing_mode = vi_mode;
5075 rl_vi_insertion_mode ();
5076 #endif /* VI_MODE */
5079 rl_emacs_editing_mode ()
5081 rl_editing_mode = emacs_mode;
5082 keymap = emacs_standard_keymap;
5086 /* **************************************************************** */
5090 /* **************************************************************** */
5092 /* Non-zero means that case is not significant in completion. */
5093 int completion_case_fold = 0;
5095 /* Return an array of (char *) which is a list of completions for TEXT.
5096 If there are no completions, return a NULL pointer.
5097 The first entry in the returned array is the substitution for TEXT.
5098 The remaining entries are the possible completions.
5099 The array is terminated with a NULL pointer.
5101 ENTRY_FUNCTION is a function of two args, and returns a (char *).
5102 The first argument is TEXT.
5103 The second is a state argument; it should be zero on the first call, and
5104 non-zero on subsequent calls. It returns a NULL pointer to the caller
5105 when there are no more matches.
5108 completion_matches (text, entry_function)
5110 char *(*entry_function) ();
5112 /* Number of slots in match_list. */
5113 int match_list_size;
5115 /* The list of matches. */
5117 (char **)xmalloc (((match_list_size = 10) + 1) * sizeof (char *));
5119 /* Number of matches actually found. */
5122 /* Temporary string binder. */
5125 match_list[1] = (char *)NULL;
5127 while (string = (*entry_function) (text, matches))
5129 if (matches + 1 == match_list_size)
5130 match_list = (char **)xrealloc
5131 (match_list, ((match_list_size += 10) + 1) * sizeof (char *));
5133 match_list[++matches] = string;
5134 match_list[matches + 1] = (char *)NULL;
5137 /* If there were any matches, then look through them finding out the
5138 lowest common denominator. That then becomes match_list[0]. */
5142 int low = 100000; /* Count of max-matched characters. */
5144 /* If only one match, just use that. */
5147 match_list[0] = match_list[1];
5148 match_list[1] = (char *)NULL;
5152 /* Otherwise, compare each member of the list with
5153 the next, finding out where they stop matching. */
5157 register int c1, c2, si;
5159 if (completion_case_fold)
5162 (c1 = to_lower(match_list[i][si])) &&
5163 (c2 = to_lower(match_list[i + 1][si]));
5165 if (c1 != c2) break;
5170 (c1 = match_list[i][si]) &&
5171 (c2 = match_list[i + 1][si]);
5173 if (c1 != c2) break;
5176 if (low > si) low = si;
5179 match_list[0] = (char *)xmalloc (low + 1);
5180 strncpy (match_list[0], match_list[1], low);
5181 match_list[0][low] = '\0';
5184 else /* There were no matches. */
5187 match_list = (char **)NULL;
5189 return (match_list);
5192 /* Okay, now we write the entry_function for filename completion. In the
5193 general case. Note that completion in the shell is a little different
5194 because of all the pathnames that must be followed when looking up the
5195 completion for a command. */
5197 filename_completion_function (text, state)
5201 static DIR *directory;
5202 static char *filename = (char *)NULL;
5203 static char *dirname = (char *)NULL;
5204 static char *users_dirname = (char *)NULL;
5205 static int filename_len;
5207 dirent *entry = (dirent *)NULL;
5209 /* If we don't have any state, then do some initialization. */
5214 if (dirname) free (dirname);
5215 if (filename) free (filename);
5216 if (users_dirname) free (users_dirname);
5218 filename = savestring (text);
5219 if (!*text) text = ".";
5220 dirname = savestring (text);
5222 temp = rindex (dirname, '/');
5226 strcpy (filename, ++temp);
5230 strcpy (dirname, ".");
5232 /* We aren't done yet. We also support the "~user" syntax. */
5234 /* Save the version of the directory that the user typed. */
5235 users_dirname = savestring (dirname);
5239 temp_dirname = tilde_expand (dirname);
5241 dirname = temp_dirname;
5243 if (rl_symbolic_link_hook)
5244 (*rl_symbolic_link_hook) (&dirname);
5246 directory = opendir (dirname);
5247 filename_len = strlen (filename);
5249 rl_filename_completion_desired = 1;
5252 /* At this point we should entertain the possibility of hacking wildcarded
5253 filenames, like /usr/man/man<WILD>/te<TAB>. If the directory name
5254 contains globbing characters, then build an array of directories to
5255 glob on, and glob on the first one. */
5257 /* Now that we have some state, we can read the directory. */
5259 while (directory && (entry = readdir (directory)))
5261 /* Special case for no filename.
5262 All entries except "." and ".." match. */
5265 if ((strcmp (entry->d_name, ".") != 0) &&
5266 (strcmp (entry->d_name, "..") != 0))
5271 /* Otherwise, if these match upto the length of filename, then
5273 if (entry->d_name[0] == filename[0] && /* Quick test */
5274 (strncmp (filename, entry->d_name, filename_len) == 0))
5285 closedir (directory);
5286 directory = (DIR *)NULL;
5288 return (char *)NULL;
5294 if (dirname && (strcmp (dirname, ".") != 0))
5297 xmalloc (1 + strlen (users_dirname) + strlen (entry->d_name));
5298 strcpy (temp, users_dirname);
5299 strcat (temp, entry->d_name);
5303 temp = (savestring (entry->d_name));
5310 /* **************************************************************** */
5314 /* **************************************************************** */
5316 /* rl_add_defun (char *name, Function *function, int key)
5317 Add NAME to the list of named functions. Make FUNCTION
5318 be the function that gets called.
5319 If KEY is not -1, then bind it. */
5320 rl_add_defun (name, function, key)
5326 rl_bind_key (key, function);
5327 rl_add_funmap_entry (name, function);
5330 /* Bind KEY to FUNCTION. Returns non-zero if KEY is out of range. */
5332 rl_bind_key (key, function)
5339 if (key > 127 && key < 256)
5341 if (keymap[ESC].type == ISKMAP)
5343 Keymap escmap = (Keymap)keymap[ESC].function;
5346 escmap[key].type = ISFUNC;
5347 escmap[key].function = function;
5353 keymap[key].type = ISFUNC;
5354 keymap[key].function = function;
5358 /* Bind KEY to FUNCTION in MAP. Returns non-zero in case of invalid
5361 rl_bind_key_in_map (key, function, map)
5367 Keymap oldmap = keymap;
5370 result = rl_bind_key (key, function);
5375 /* Make KEY do nothing in the currently selected keymap.
5376 Returns non-zero in case of error. */
5381 return (rl_bind_key (key, (Function *)NULL));
5384 /* Make KEY do nothing in MAP.
5385 Returns non-zero in case of error. */
5387 rl_unbind_key_in_map (key, map)
5391 return (rl_bind_key_in_map (key, (Function *)NULL, map));
5394 /* Bind the key sequence represented by the string KEYSEQ to
5395 FUNCTION. This makes new keymaps as necessary. The initial
5396 place to do bindings is in MAP. */
5397 rl_set_key (keyseq, function, map)
5402 rl_generic_bind (ISFUNC, keyseq, function, map);
5405 /* Bind the key sequence represented by the string KEYSEQ to
5406 the string of characters MACRO. This makes new keymaps as
5407 necessary. The initial place to do bindings is in MAP. */
5408 rl_macro_bind (keyseq, macro, map)
5409 char *keyseq, *macro;
5415 macro_keys = (char *)xmalloc ((2 * strlen (macro)) + 1);
5417 if (rl_translate_keyseq (macro, macro_keys, ¯o_keys_len))
5422 rl_generic_bind (ISMACR, keyseq, macro_keys, map);
5425 /* Bind the key sequence represented by the string KEYSEQ to
5426 the arbitrary pointer DATA. TYPE says what kind of data is
5427 pointed to by DATA, right now this can be a function (ISFUNC),
5428 a macro (ISMACR), or a keymap (ISKMAP). This makes new keymaps
5429 as necessary. The initial place to do bindings is in MAP. */
5430 rl_generic_bind (type, keyseq, data, map)
5432 char *keyseq, *data;
5439 /* If no keys to bind to, exit right away. */
5440 if (!keyseq || !*keyseq)
5447 keys = (char *)alloca (1 + (2 * strlen (keyseq)));
5449 /* Translate the ASCII representation of KEYSEQ into an array
5450 of characters. Stuff the characters into ARRAY, and the
5451 length of ARRAY into LENGTH. */
5452 if (rl_translate_keyseq (keyseq, keys, &keys_len))
5455 /* Bind keys, making new keymaps as necessary. */
5456 for (i = 0; i < keys_len; i++)
5458 if (i + 1 < keys_len)
5460 if (map[keys[i]].type != ISKMAP)
5462 if (map[i].type == ISMACR)
5463 free ((char *)map[i].function);
5465 map[keys[i]].type = ISKMAP;
5466 map[keys[i]].function = (Function *)rl_make_bare_keymap ();
5468 map = (Keymap)map[keys[i]].function;
5472 if (map[keys[i]].type == ISMACR)
5473 free ((char *)map[keys[i]].function);
5475 map[keys[i]].function = (Function *)data;
5476 map[keys[i]].type = type;
5481 /* Translate the ASCII representation of SEQ, stuffing the
5482 values into ARRAY, an array of characters. LEN gets the
5483 final length of ARRAY. Return non-zero if there was an
5484 error parsing SEQ. */
5485 rl_translate_keyseq (seq, array, len)
5489 register int i, c, l = 0;
5491 for (i = 0; c = seq[i]; i++)
5500 if (((c == 'C' || c == 'M') && seq[i + 1] == '-') ||
5503 /* Handle special case of backwards define. */
5504 if (strncmp (&seq[i], "C-\\M-", 5) == 0)
5508 array[l++] = CTRL (to_upper (seq[i]));
5523 /* Special hack for C-?... */
5525 array[l++] = RUBOUT;
5527 array[l++] = CTRL (to_upper (seq[i]));
5545 /* Return a pointer to the function that STRING represents.
5546 If STRING doesn't have a matching function, then a NULL pointer
5549 rl_named_function (string)
5554 for (i = 0; funmap[i]; i++)
5555 if (stricmp (funmap[i]->name, string) == 0)
5556 return (funmap[i]->function);
5557 return ((Function *)NULL);
5560 /* The last key bindings file read. */
5562 static char *last_readline_init_file = "~/inputrc";
5566 /* Re-read the current keybindings file. */
5567 rl_re_read_init_file (count, ignore)
5570 rl_read_init_file ((char *)NULL);
5573 /* Do key bindings from a file. If FILENAME is NULL it defaults
5574 to `~/.inputrc'. If the file existed and could be opened and
5575 read, 0 is returned, otherwise errno is returned. */
5577 rl_read_init_file (filename)
5581 char *buffer, *openname, *line, *end;
5585 /* Default the filename. */
5587 filename = last_readline_init_file;
5589 openname = tilde_expand (filename);
5591 if (!openname || *openname == '\000')
5594 if ((stat (openname, &finfo) < 0) ||
5595 (file = open (openname, O_RDONLY, 0666)) < 0)
5603 last_readline_init_file = filename;
5605 /* Read the file into BUFFER. */
5606 buffer = (char *)xmalloc (finfo.st_size + 1);
5607 i = read (file, buffer, finfo.st_size);
5610 if (i != finfo.st_size)
5613 /* Loop over the lines in the file. Lines that start with `#' are
5614 comments; all other lines are commands for readline initialization. */
5616 end = buffer + finfo.st_size;
5619 /* Find the end of this line. */
5620 for (i = 0; line + i != end && line[i] != '\n'; i++);
5622 /* Mark end of line. */
5625 /* If the line is not a comment, then parse it. */
5627 rl_parse_and_bind (line);
5629 /* Move to the next line. */
5635 /* **************************************************************** */
5637 /* Parser Directives */
5639 /* **************************************************************** */
5643 /* Calling programs set this to have their argv[0]. */
5644 char *rl_readline_name = "other";
5646 /* Stack of previous values of parsing_conditionalized_out. */
5647 static unsigned char *if_stack = (unsigned char *)NULL;
5648 static int if_stack_depth = 0;
5649 static int if_stack_size = 0;
5651 /* Push parsing_conditionalized_out, and set parser state based on ARGS. */
5657 /* Push parser state. */
5658 if (if_stack_depth + 1 >= if_stack_size)
5661 if_stack = (unsigned char *)xmalloc (if_stack_size = 20);
5663 if_stack = (unsigned char *)xrealloc (if_stack, if_stack_size += 20);
5665 if_stack[if_stack_depth++] = parsing_conditionalized_out;
5667 /* If parsing is turned off, then nothing can turn it back on except
5668 for finding the matching endif. In that case, return right now. */
5669 if (parsing_conditionalized_out)
5672 /* Isolate first argument. */
5673 for (i = 0; args[i] && !whitespace (args[i]); i++);
5678 /* Handle "if term=foo" and "if mode=emacs" constructs. If this
5679 isn't term=foo, or mode=emacs, then check to see if the first
5680 word in ARGS is the same as the value stored in rl_readline_name. */
5681 if (rl_terminal_name && strnicmp (args, "term=", 5) == 0)
5685 /* Terminals like "aaa-60" are equivalent to "aaa". */
5686 tname = savestring (rl_terminal_name);
5687 tem = rindex (tname, '-');
5691 if (stricmp (args + 5, tname) == 0)
5692 parsing_conditionalized_out = 0;
5694 parsing_conditionalized_out = 1;
5696 #if defined (VI_MODE)
5697 else if (strnicmp (args, "mode=", 5) == 0)
5701 if (stricmp (args + 5, "emacs") == 0)
5703 else if (stricmp (args + 5, "vi") == 0)
5708 if (mode == rl_editing_mode)
5709 parsing_conditionalized_out = 0;
5711 parsing_conditionalized_out = 1;
5713 #endif /* VI_MODE */
5714 /* Check to see if the first word in ARGS is the same as the
5715 value stored in rl_readline_name. */
5716 else if (stricmp (args, rl_readline_name) == 0)
5717 parsing_conditionalized_out = 0;
5719 parsing_conditionalized_out = 1;
5722 /* Invert the current parser state if there is anything on the stack. */
5728 if (!if_stack_depth)
5730 /* Error message? */
5734 /* Check the previous (n - 1) levels of the stack to make sure that
5735 we haven't previously turned off parsing. */
5736 for (i = 0; i < if_stack_depth - 1; i++)
5737 if (if_stack[i] == 1)
5740 /* Invert the state of parsing if at top level. */
5741 parsing_conditionalized_out = !parsing_conditionalized_out;
5744 /* Terminate a conditional, popping the value of
5745 parsing_conditionalized_out from the stack. */
5750 parsing_conditionalized_out = if_stack[--if_stack_depth];
5753 /* *** What, no error message? *** */
5757 /* Associate textual names with actual functions. */
5761 } parser_directives [] = {
5762 { "if", parser_if },
5763 { "endif", parser_endif },
5764 { "else", parser_else },
5765 { (char *)0x0, (Function *)0x0 }
5768 /* Handle a parser directive. STATEMENT is the line of the directive
5769 without any leading `$'. */
5771 handle_parser_directive (statement)
5775 char *directive, *args;
5777 /* Isolate the actual directive. */
5779 /* Skip whitespace. */
5780 for (i = 0; whitespace (statement[i]); i++);
5782 directive = &statement[i];
5784 for (; statement[i] && !whitespace (statement[i]); i++);
5787 statement[i++] = '\0';
5789 for (; statement[i] && whitespace (statement[i]); i++);
5791 args = &statement[i];
5793 /* Lookup the command, and act on it. */
5794 for (i = 0; parser_directives[i].name; i++)
5795 if (stricmp (directive, parser_directives[i].name) == 0)
5797 (*parser_directives[i].function) (args);
5801 /* *** Should an error message be output? */
5805 /* Ugly but working hack for binding prefix meta. */
5806 #define PREFIX_META_HACK
5808 static int substring_member_of_array ();
5810 /* Read the binding command from STRING and perform it.
5811 A key binding command looks like: Keyname: function-name\0,
5812 a variable binding command looks like: set variable value.
5813 A new-style keybinding looks like "\C-x\C-x": exchange-point-and-mark. */
5814 rl_parse_and_bind (string)
5817 extern char *possible_control_prefixes[], *possible_meta_prefixes[];
5818 char *funname, *kname;
5822 while (string && whitespace (*string))
5825 if (!string || !*string || *string == '#')
5828 /* If this is a parser directive, act on it. */
5831 handle_parser_directive (&string[1]);
5835 /* If we are supposed to be skipping parsing right now, then do it. */
5836 if (parsing_conditionalized_out)
5840 /* If this keyname is a complex key expression surrounded by quotes,
5841 advance to after the matching close quote. */
5844 for (i = 1; c = string[i]; i++)
5846 if (c == '"' && string[i - 1] != '\\')
5851 /* Advance to the colon (:) or whitespace which separates the two objects. */
5852 for (; (c = string[i]) && c != ':' && c != ' ' && c != '\t'; i++ );
5854 /* Mark the end of the command (or keyname). */
5858 /* If this is a command to set a variable, then do that. */
5859 if (stricmp (string, "set") == 0)
5861 char *var = string + i;
5864 /* Make VAR point to start of variable name. */
5865 while (*var && whitespace (*var)) var++;
5867 /* Make value point to start of value string. */
5869 while (*value && !whitespace (*value)) value++;
5872 while (*value && whitespace (*value)) value++;
5874 rl_variable_bind (var, value);
5878 /* Skip any whitespace between keyname and funname. */
5879 for (; string[i] && whitespace (string[i]); i++);
5880 funname = &string[i];
5882 /* Now isolate funname.
5883 For straight function names just look for whitespace, since
5884 that will signify the end of the string. But this could be a
5885 macro definition. In that case, the string is quoted, so skip
5886 to the matching delimiter. */
5887 if (*funname == '\'' || *funname == '"')
5889 int delimiter = string[i++];
5891 for (; c = string[i]; i++)
5893 if (c == delimiter && string[i - 1] != '\\')
5900 /* Advance to the end of the string. */
5901 for (; string[i] && !whitespace (string[i]); i++);
5903 /* No extra whitespace at the end of the string. */
5906 /* If this is a new-style key-binding, then do the binding with
5907 rl_set_key (). Otherwise, let the older code deal with it. */
5910 char *seq = (char *)alloca (1 + strlen (string));
5911 register int j, k = 0;
5913 for (j = 1; string[j]; j++)
5915 if (string[j] == '"' && string[j - 1] != '\\')
5918 seq[k++] = string[j];
5922 /* Binding macro? */
5923 if (*funname == '\'' || *funname == '"')
5925 j = strlen (funname);
5927 if (j && funname[j - 1] == *funname)
5928 funname[j - 1] = '\0';
5930 rl_macro_bind (seq, &funname[1], keymap);
5933 rl_set_key (seq, rl_named_function (funname), keymap);
5938 /* Get the actual character we want to deal with. */
5939 kname = rindex (string, '-');
5945 key = glean_key_from_name (kname);
5947 /* Add in control and meta bits. */
5948 if (substring_member_of_array (string, possible_control_prefixes))
5949 key = CTRL (to_upper (key));
5951 if (substring_member_of_array (string, possible_meta_prefixes))
5954 /* Temporary. Handle old-style keyname with macro-binding. */
5955 if (*funname == '\'' || *funname == '"')
5958 int fl = strlen (funname);
5960 seq[0] = key; seq[1] = '\0';
5961 if (fl && funname[fl - 1] == *funname)
5962 funname[fl - 1] = '\0';
5964 rl_macro_bind (seq, &funname[1], keymap);
5966 #if defined (PREFIX_META_HACK)
5967 /* Ugly, but working hack to keep prefix-meta around. */
5968 else if (stricmp (funname, "prefix-meta") == 0)
5974 rl_generic_bind (ISKMAP, seq, (char *)emacs_meta_keymap, keymap);
5976 #endif /* PREFIX_META_HACK */
5978 rl_bind_key (key, rl_named_function (funname));
5981 rl_variable_bind (name, value)
5984 if (stricmp (name, "editing-mode") == 0)
5986 if (strnicmp (value, "vi", 2) == 0)
5988 #if defined (VI_MODE)
5989 keymap = vi_insertion_keymap;
5990 rl_editing_mode = vi_mode;
5992 #if defined (NOTDEF)
5993 /* What state is the terminal in? I'll tell you:
5994 non-determinate! That means we cannot do any output. */
5997 #endif /* VI_MODE */
5999 else if (strnicmp (value, "emacs", 5) == 0)
6001 keymap = emacs_standard_keymap;
6002 rl_editing_mode = emacs_mode;
6005 else if (stricmp (name, "horizontal-scroll-mode") == 0)
6007 if (!*value || stricmp (value, "On") == 0)
6008 horizontal_scroll_mode = 1;
6010 horizontal_scroll_mode = 0;
6012 else if (stricmp (name, "mark-modified-lines") == 0)
6014 if (!*value || stricmp (value, "On") == 0)
6015 mark_modified_lines = 1;
6017 mark_modified_lines = 0;
6019 else if (stricmp (name, "prefer-visible-bell") == 0)
6021 if (!*value || stricmp (value, "On") == 0)
6022 prefer_visible_bell = 1;
6024 prefer_visible_bell = 0;
6026 else if (stricmp (name, "comment-begin") == 0)
6028 #if defined (VI_MODE)
6029 extern char *rl_vi_comment_begin;
6033 if (rl_vi_comment_begin)
6034 free (rl_vi_comment_begin);
6036 rl_vi_comment_begin = savestring (value);
6038 #endif /* VI_MODE */
6042 /* Return the character which matches NAME.
6043 For example, `Space' returns ' '. */
6050 assoc_list name_key_alist[] = {
6053 { "Escape", '\033' },
6055 { "Newline", '\n' },
6066 glean_key_from_name (name)
6071 for (i = 0; name_key_alist[i].name; i++)
6072 if (stricmp (name, name_key_alist[i].name) == 0)
6073 return (name_key_alist[i].value);
6079 /* **************************************************************** */
6081 /* Key Binding and Function Information */
6083 /* **************************************************************** */
6085 /* Each of the following functions produces information about the
6086 state of keybindings and functions known to Readline. The info
6087 is always printed to rl_outstream, and in such a way that it can
6088 be read back in (i.e., passed to rl_parse_and_bind (). */
6090 /* Print the names of functions known to Readline. */
6092 rl_list_funmap_names (ignore)
6096 char **funmap_names;
6097 extern char **rl_funmap_names ();
6099 funmap_names = rl_funmap_names ();
6104 for (i = 0; funmap_names[i]; i++)
6105 fprintf (rl_outstream, "%s\n", funmap_names[i]);
6107 free (funmap_names);
6110 /* Return a NULL terminated array of strings which represent the key
6111 sequences that are used to invoke FUNCTION in MAP. */
6113 invoking_keyseqs_in_map (function, map)
6119 int result_index, result_size;
6121 result = (char **)NULL;
6122 result_index = result_size = 0;
6124 for (key = 0; key < 128; key++)
6126 switch (map[key].type)
6129 /* Macros match, if, and only if, the pointers are identical.
6130 Thus, they are treated exactly like functions in here. */
6132 /* If the function in the keymap is the one we are looking for,
6133 then add the current KEY to the list of invoking keys. */
6134 if (map[key].function == function)
6136 char *keyname = (char *)xmalloc (5);
6139 sprintf (keyname, "\\C-%c", to_lower (UNCTRL (key)));
6140 else if (key == RUBOUT)
6141 sprintf (keyname, "\\C-?");
6143 sprintf (keyname, "%c", key);
6145 if (result_index + 2 > result_size)
6148 result = (char **) xmalloc
6149 ((result_size = 10) * sizeof (char *));
6151 result = (char **) xrealloc
6152 (result, (result_size += 10) * sizeof (char *));
6155 result[result_index++] = keyname;
6156 result[result_index] = (char *)NULL;
6162 char **seqs = (char **)NULL;
6164 /* Find the list of keyseqs in this map which have FUNCTION as
6165 their target. Add the key sequences found to RESULT. */
6166 if (map[key].function)
6168 invoking_keyseqs_in_map (function, (Keymap)map[key].function);
6174 for (i = 0; seqs[i]; i++)
6176 char *keyname = (char *)xmalloc (6 + strlen (seqs[i]));
6179 sprintf (keyname, "\\e");
6180 else if (CTRL_P (key))
6181 sprintf (keyname, "\\C-%c", to_lower (UNCTRL (key)));
6182 else if (key == RUBOUT)
6183 sprintf (keyname, "\\C-?");
6185 sprintf (keyname, "%c", key);
6187 strcat (keyname, seqs[i]);
6189 if (result_index + 2 > result_size)
6193 xmalloc ((result_size = 10) * sizeof (char *));
6197 (result_size += 10) * sizeof (char *));
6200 result[result_index++] = keyname;
6201 result[result_index] = (char *)NULL;
6211 /* Return a NULL terminated array of strings which represent the key
6212 sequences that can be used to invoke FUNCTION using the current keymap. */
6214 rl_invoking_keyseqs (function)
6217 return (invoking_keyseqs_in_map (function, keymap));
6220 /* Print all of the current functions and their bindings to
6221 rl_outstream. If an explicit argument is given, then print
6222 the output in such a way that it can be read back in. */
6224 rl_dump_functions (count)
6227 void rl_function_dumper ();
6229 rl_function_dumper (rl_explicit_arg);
6234 /* Print all of the functions and their bindings to rl_outstream. If
6235 PRINT_READABLY is non-zero, then print the output in such a way
6236 that it can be read back in. */
6238 rl_function_dumper (print_readably)
6242 char **rl_funmap_names (), **names;
6245 names = rl_funmap_names ();
6247 fprintf (rl_outstream, "\n");
6249 for (i = 0; name = names[i]; i++)
6254 function = rl_named_function (name);
6255 invokers = invoking_keyseqs_in_map (function, keymap);
6260 fprintf (rl_outstream, "# %s (not bound)\n", name);
6265 for (j = 0; invokers[j]; j++)
6267 fprintf (rl_outstream, "\"%s\": %s\n",
6278 fprintf (rl_outstream, "%s is not bound to any keys\n",
6284 fprintf (rl_outstream, "%s can be found on ", name);
6286 for (j = 0; invokers[j] && j < 5; j++)
6288 fprintf (rl_outstream, "\"%s\"%s", invokers[j],
6289 invokers[j + 1] ? ", " : ".\n");
6292 if (j == 5 && invokers[j])
6293 fprintf (rl_outstream, "...\n");
6295 for (j = 0; invokers[j]; j++)
6305 /* **************************************************************** */
6307 /* String Utility Functions */
6309 /* **************************************************************** */
6311 static char *strindex ();
6313 /* Return non-zero if any members of ARRAY are a substring in STRING. */
6315 substring_member_of_array (string, array)
6316 char *string, **array;
6320 if (strindex (string, *array))
6327 /* Whoops, Unix doesn't have strnicmp. */
6329 /* Compare at most COUNT characters from string1 to string2. Case
6332 strnicmp (string1, string2, count)
6333 char *string1, *string2;
6335 register char ch1, ch2;
6341 if (to_upper(ch1) == to_upper(ch2))
6348 /* strcmp (), but caseless. */
6350 stricmp (string1, string2)
6351 char *string1, *string2;
6353 register char ch1, ch2;
6355 while (*string1 && *string2)
6359 if (to_upper(ch1) != to_upper(ch2))
6362 return (*string1 | *string2);
6365 /* Determine if s2 occurs in s1. If so, return a pointer to the
6366 match in s1. The compare is case insensitive. */
6369 register char *s1, *s2;
6371 register int i, l = strlen (s2);
6372 register int len = strlen (s1);
6374 for (i = 0; (len - i) >= l; i++)
6375 if (strnicmp (&s1[i], s2, l) == 0)
6377 return ((char *)NULL);
6381 /* **************************************************************** */
6383 /* USG (System V) Support */
6385 /* **************************************************************** */
6387 /* When compiling and running in the `Posix' environment, Ultrix does
6388 not restart system calls, so this needs to do it. */
6399 #endif /* __GO32__ */
6403 result = read (fileno (stream), &c, sizeof (char));
6405 if (result == sizeof (char))
6408 /* If zero characters are returned, then the file that we are
6409 reading from is empty! Return EOF in that case. */
6414 /* If the error that we received was SIGINT, then try again,
6415 this is simply an interrupted system call to read ().
6416 Otherwise, some error ocurred, also signifying EOF. */
6419 #endif /* !__GO32__ */
6423 #if defined (STATIC_MALLOC)
6425 /* **************************************************************** */
6427 /* xmalloc and xrealloc () */
6429 /* **************************************************************** */
6431 static void memory_error_and_abort ();
6437 char *temp = (char *)malloc (bytes);
6440 memory_error_and_abort ();
6445 xrealloc (pointer, bytes)
6452 temp = (char *)malloc (bytes);
6454 temp = (char *)realloc (pointer, bytes);
6457 memory_error_and_abort ();
6463 memory_error_and_abort ()
6465 fprintf (stderr, "readline: Out of virtual memory!\n");
6468 #endif /* STATIC_MALLOC */
6471 /* **************************************************************** */
6473 /* Testing Readline */
6475 /* **************************************************************** */
6481 HIST_ENTRY **history_list ();
6482 char *temp = (char *)NULL;
6483 char *prompt = "readline% ";
6488 temp = readline (prompt);
6494 /* If there is anything on the line, print it and remember it. */
6497 fprintf (stderr, "%s\r\n", temp);
6501 /* Check for `command' that we handle. */
6502 if (strcmp (temp, "quit") == 0)
6505 if (strcmp (temp, "list") == 0)
6507 HIST_ENTRY **list = history_list ();
6511 for (i = 0; list[i]; i++)
6513 fprintf (stderr, "%d: %s\r\n", i, list[i]->line);
6514 free (list[i]->line);
6528 * compile-command: "gcc -g -traditional -I. -I.. -DTEST -o readline readline.c keymaps.o funmap.o history.o -ltermcap"