1 /* readline.c -- a general facility for reading lines of input
2 with emacs style editing and completion. */
4 /* Copyright (C) 1987,1989 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 1, or (at your option)
15 The Library is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 The GNU General Public License is often shipped with GNU software, and
21 is generally kept in a file called COPYING or LICENSE. If you do not
22 have a copy of the license, write to the Free Software Foundation,
23 675 Mass Ave, Cambridge, MA 02139, USA. */
25 /* Remove these declarations when we have a complete libgnu.a. */
28 extern char *xmalloc (), *xrealloc ();
30 static char *xmalloc (), *xrealloc ();
34 #include <sys/types.h>
40 #define alloca __builtin_alloca
42 #if defined (sparc) && defined (sun)
47 #define NEW_TTY_DRIVER
48 #if defined (SYSV) || defined (hpux) || defined (Xenix)
60 /* These next are for filename completion. Perhaps this belongs
61 in a different place. */
66 struct passwd *getpwuid (), *getpwent ();
69 #define HACK_TERMCAP_MOTION
82 #define d_namlen d_reclen
87 /* Some standard library routines. */
92 #define digit(c) ((c) >= '0' && (c) <= '9')
96 #define isletter(c) (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z'))
100 #define digit_value(c) ((c) - '0')
105 #define member(c, s) ((c) ? index ((s), (c)) : 0)
109 #define isident(c) ((isletter(c) || digit(c) || c == '_'))
113 #define exchange(x, y) {int temp = x; x = y; y = temp;}
116 static update_line ();
117 static void output_character_function ();
118 static delete_chars ();
119 static delete_chars ();
120 static insert_some_chars ();
122 #ifdef VOID_SIGHANDLER
123 #define sighandler void
125 #define sighandler int
128 /* This typedef is equivalant to the one for Function; it allows us
129 to say SigHandler *foo = signal (SIGKILL, SIG_IGN); */
130 typedef sighandler SigHandler ();
132 /* If on, then readline handles signals in a way that doesn't screw. */
133 #define HANDLE_SIGNALS
136 /* **************************************************************** */
138 /* Line editing input utility */
140 /* **************************************************************** */
142 /* A pointer to the keymap that is currently in use.
143 By default, it is the standard emacs keymap. */
144 Keymap keymap = emacs_standard_keymap;
149 /* The current style of editing. */
150 int rl_editing_mode = emacs_mode;
152 /* Non-zero if the previous command was a kill command. */
153 static int last_command_was_kill = 0;
155 /* The current value of the numeric argument specified by the user. */
156 int rl_numeric_arg = 1;
158 /* Non-zero if an argument was typed. */
159 int rl_explicit_arg = 0;
161 /* Temporary value used while generating the argument. */
162 static int arg_sign = 1;
164 /* Non-zero means we have been called at least once before. */
165 static int rl_initialized = 0;
167 /* If non-zero, this program is running in an EMACS buffer. */
168 static char *running_in_emacs = (char *)NULL;
170 /* The current offset in the current input line. */
173 /* Mark in the current input line. */
176 /* Length of the current input line. */
179 /* Make this non-zero to return the current input_line. */
182 /* The last function executed by readline. */
183 Function *rl_last_func = (Function *)NULL;
185 /* Top level environment for readline_internal (). */
186 static jmp_buf readline_top_level;
188 /* The streams we interact with. */
189 static FILE *in_stream, *out_stream;
191 /* The names of the streams that we do input and output to. */
192 FILE *rl_instream = stdin, *rl_outstream = stdout;
194 /* Non-zero means echo characters as they are read. */
195 int readline_echoing_p = 1;
197 /* Current prompt. */
200 /* The number of characters read in order to type this complete command. */
201 int rl_key_sequence_length = 0;
203 /* If non-zero, then this is the address of a function to call just
204 before readline_internal () prints the first prompt. */
205 Function *rl_startup_hook = (Function *)NULL;
207 /* If non-zero, then this is the address of a function to call when
208 completing on a directory name. The function is called with
209 the address of a string (the current directory name) as an arg. */
210 Function *rl_symbolic_link_hook = (Function *)NULL;
212 /* What we use internally. You should always refer to RL_LINE_BUFFER. */
213 static char *the_line;
215 /* The character that can generate an EOF. Really read from
216 the terminal driver... just defaulted here. */
217 static int eof_char = CTRL ('D');
219 /* Non-zero makes this the next keystroke to read. */
220 int rl_pending_input = 0;
222 /* Pointer to a useful terminal name. */
223 char *rl_terminal_name = (char *)NULL;
225 /* Line buffer and maintenence. */
226 char *rl_line_buffer = (char *)NULL;
227 static int rl_line_buffer_len = 0;
228 #define DEFAULT_BUFFER_SIZE 256
231 /* **************************************************************** */
233 /* `Forward' declarations */
235 /* **************************************************************** */
237 /* Non-zero means do not parse any lines other than comments and
238 parser directives. */
239 static unsigned char parsing_conditionalized_out = 0;
241 /* Caseless strcmp (). */
242 static int stricmp (), strnicmp ();
244 /* Non-zero means to save keys that we dispatch on in a kbd macro. */
245 static int defining_kbd_macro = 0;
248 /* **************************************************************** */
250 /* Top Level Functions */
252 /* **************************************************************** */
254 /* Read a line of input. Prompt with PROMPT. A NULL PROMPT means
255 none. A return value of NULL means that EOF was encountered. */
260 static rl_prep_terminal (), rl_deprep_terminal ();
261 char *readline_internal ();
266 /* If we are at EOF return a NULL string. */
267 if (rl_pending_input == EOF)
269 rl_pending_input = 0;
270 return ((char *)NULL);
276 #ifdef HANDLE_SIGNALS
280 value = readline_internal ();
281 rl_deprep_terminal ();
283 #ifdef HANDLE_SIGNALS
290 /* Read a line of input from the global rl_instream, doing output on
291 the global rl_outstream.
292 If rl_prompt is non-null, then that is our prompt. */
296 int lastc, c, eof_found;
298 in_stream = rl_instream; out_stream = rl_outstream;
299 lastc = eof_found = 0;
302 (*rl_startup_hook) ();
304 if (!readline_echoing_p)
308 fprintf (out_stream, "%s", rl_prompt);
317 if (rl_editing_mode == vi_mode)
318 rl_vi_insertion_mode ();
324 int lk = last_command_was_kill;
325 int code = setjmp (readline_top_level);
330 if (!rl_pending_input)
332 /* Then initialize the argument and number of keys read. */
334 rl_key_sequence_length = 0;
339 /* EOF typed to a non-blank line is a <NL>. */
340 if (c == EOF && rl_end)
343 /* The character eof_char typed to blank line, and not as the
344 previous character is interpreted as EOF. */
345 if (((c == eof_char && lastc != c) || c == EOF) && !rl_end)
352 rl_dispatch (c, keymap);
354 /* If there was no change in last_command_was_kill, then no kill
355 has taken place. Note that if input is pending we are reading
356 a prefix command, so nothing has changed yet. */
357 if (!rl_pending_input)
359 if (lk == last_command_was_kill)
360 last_command_was_kill = 0;
364 /* In vi mode, when you exit insert mode, the cursor moves back
365 over the previous character. We explicitly check for that here. */
366 if (rl_editing_mode == vi_mode && keymap == vi_movement_keymap)
374 /* Restore the original of this history line, iff the line that we
375 are editing was originally in the history, AND the line has changed. */
377 HIST_ENTRY *entry = current_history ();
379 if (entry && rl_undo_list)
381 char *temp = savestring (the_line);
383 entry = replace_history_entry (where_history (), the_line,
385 free_history_entry (entry);
387 strcpy (the_line, temp);
392 /* At any rate, it is highly likely that this line has an undo list. Get
400 return (savestring (the_line));
404 /* **************************************************************** */
406 /* Signal Handling */
408 /* **************************************************************** */
411 static SigHandler *old_sigwinch = (SigHandler *)NULL;
414 rl_handle_sigwinch (sig, code, scp)
416 struct sigcontext *scp;
418 char *term = rl_terminal_name, *getenv ();
420 if (readline_echoing_p)
423 term = getenv ("TERM");
426 rl_reset_terminal (term);
429 rl_forced_update_display ();
434 old_sigwinch != (SigHandler *)SIG_IGN &&
435 old_sigwinch != (SigHandler *)SIG_DFL)
436 (*old_sigwinch)(sig, code, scp);
438 #endif /* SIGWINCH */
440 #ifdef HANDLE_SIGNALS
441 /* Interrupt handling. */
442 static SigHandler *old_int = (SigHandler *)NULL,
443 *old_tstp = (SigHandler *)NULL,
444 *old_ttou = (SigHandler *)NULL,
445 *old_ttin = (SigHandler *)NULL,
446 *old_cont = (SigHandler *)NULL;
448 /* Handle an interrupt character. */
450 rl_signal_handler (sig, code, scp)
452 struct sigcontext *scp;
454 static rl_prep_terminal (), rl_deprep_terminal ();
469 rl_clean_up_for_exit ();
470 rl_deprep_terminal ();
472 rl_pending_input = 0;
474 kill (getpid (), sig);
484 old_int = (SigHandler *)signal (SIGINT, rl_signal_handler);
485 if (old_int == (SigHandler *)SIG_IGN)
486 signal (SIGINT, SIG_IGN);
489 old_tstp = (SigHandler *)signal (SIGTSTP, rl_signal_handler);
490 if (old_tstp == (SigHandler *)SIG_IGN)
491 signal (SIGTSTP, SIG_IGN);
494 old_ttou = (SigHandler *)signal (SIGTTOU, rl_signal_handler);
495 old_ttin = (SigHandler *)signal (SIGTTIN, rl_signal_handler);
497 if (old_tstp == (SigHandler *)SIG_IGN)
499 signal (SIGTTOU, SIG_IGN);
500 signal (SIGTTIN, SIG_IGN);
505 old_sigwinch = (SigHandler *)signal (SIGWINCH, rl_handle_sigwinch);
511 signal (SIGINT, old_int);
514 signal (SIGTSTP, old_tstp);
518 signal (SIGTTOU, old_ttou);
519 signal (SIGTTIN, old_ttin);
523 signal (SIGWINCH, old_sigwinch);
526 #endif /* HANDLE_SIGNALS */
529 /* **************************************************************** */
531 /* Character Input Buffering */
533 /* **************************************************************** */
535 /* If the terminal was in xoff state when we got to it, then xon_char
536 contains the character that is supposed to start it again. */
537 static int xon_char, xoff_state;
538 static int pop_index = 0, push_index = 0, ibuffer_len = 511;
539 static unsigned char ibuffer[512];
541 /* Non-null means it is a pointer to a function to run while waiting for
543 Function *rl_event_hook = (Function *)NULL;
545 #define any_typein (push_index != pop_index)
547 /* Add KEY to the buffer of characters to be read. */
554 rl_pending_input = EOF;
556 ibuffer[push_index++] = key;
557 if (push_index >= ibuffer_len)
561 /* Return the amount of space available in the
562 buffer for stuffing characters. */
566 if (pop_index > push_index)
567 return (pop_index - push_index);
569 return (ibuffer_len - (push_index - pop_index));
572 /* Get a key from the buffer of characters to be read.
573 Return the key in KEY.
574 Result is KEY if there was a key, or 0 if there wasn't. */
579 if (push_index == pop_index)
582 *key = ibuffer[pop_index++];
584 if (pop_index >= ibuffer_len)
590 /* Stuff KEY into the *front* of the input buffer.
591 Returns non-zero if successful, zero if there is
592 no space left in the buffer. */
597 if (ibuffer_space ())
601 pop_index = ibuffer_len - 1;
602 ibuffer[pop_index] = key;
608 /* If a character is available to be read, then read it
609 and stuff it into IBUFFER. Otherwise, just return. */
612 int tty = fileno (in_stream);
613 register int tem, result = -1;
618 result = ioctl (tty, FIONREAD, &chars_avail);
623 fcntl (tty, F_SETFL, O_NDELAY);
624 chars_avail = read (tty, &input, 1);
625 fcntl (tty, F_SETFL, 0);
626 if (chars_avail == -1 && errno == EAGAIN)
630 tem = ibuffer_space ();
632 if (chars_avail > tem)
635 /* One cannot read all of the available input. I can only read a single
636 character at a time, or else programs which require input can be
637 thwarted. If the buffer is larger than one character, I lose.
639 if (tem < ibuffer_len)
644 while (chars_avail--)
645 rl_stuff_char (rl_getc (in_stream));
650 rl_stuff_char (input);
654 /* Read a key, including pending input. */
660 rl_key_sequence_length++;
662 if (rl_pending_input)
664 c = rl_pending_input;
665 rl_pending_input = 0;
669 static int next_macro_key ();
671 /* If input is coming from a macro, then use that. */
672 if (c = next_macro_key ())
675 /* If the user has an event function, then call it periodically. */
678 while (rl_event_hook && !rl_get_char (&c))
686 if (!rl_get_char (&c))
687 c = rl_getc (in_stream);
691 #ifdef NEVER /* This breaks supdup to 4.0.3c machines. */
693 /* Ugh. But I can't think of a better way. */
694 if (xoff_state && c == xon_char)
696 ioctl (fileno (in_stream), TIOCSTART, 0);
698 return (rl_read_key ());
700 #endif /* TIOCSTART */
706 /* I'm beginning to hate the declaration rules for various compilers. */
707 static void add_macro_char ();
709 /* Do the command associated with KEY in MAP.
710 If the associated command is really a keymap, then read
711 another key, and dispatch into that map. */
712 rl_dispatch (key, map)
717 if (defining_kbd_macro)
718 add_macro_char (key);
720 if (key > 127 && key < 256)
722 if (map[ESC].type == ISKMAP)
724 map = (Keymap)map[ESC].function;
726 rl_dispatch (key, map);
733 switch (map[key].type)
737 Function *func = map[key].function;
739 if (func != (Function *)NULL)
741 /* Special case rl_do_lowercase_version (). */
742 if (func == rl_do_lowercase_version)
744 rl_dispatch (to_lower (key), map);
748 (*map[key].function)(rl_numeric_arg * arg_sign, key);
759 if (map[key].function != (Function *)NULL)
763 rl_key_sequence_length++;
764 newkey = rl_read_key ();
765 rl_dispatch (newkey, (Keymap)map[key].function);
775 if (map[key].function != (Function *)NULL)
777 static with_macro_input ();
778 char *macro = savestring ((char *)map[key].function);
780 with_macro_input (macro);
786 /* If we have input pending, then the last command was a prefix
787 command. Don't change the state of rl_last_func. */
788 if (!rl_pending_input)
789 rl_last_func = map[key].function;
793 /* **************************************************************** */
795 /* Hacking Keyboard Macros */
797 /* **************************************************************** */
799 /* The currently executing macro string. If this is non-zero,
800 then it is a malloc ()'ed string where input is coming from. */
801 static char *executing_macro = (char *)NULL;
803 /* The offset in the above string to the next character to be read. */
804 static int executing_macro_index = 0;
806 /* The current macro string being built. Characters get stuffed
807 in here by add_macro_char (). */
808 static char *current_macro = (char *)NULL;
810 /* The size of the buffer allocated to current_macro. */
811 static int current_macro_size = 0;
813 /* The index at which characters are being added to current_macro. */
814 static int current_macro_index = 0;
816 /* A structure used to save nested macro strings.
817 It is a linked list of string/index for each saved macro. */
819 struct saved_macro *next;
824 /* The list of saved macros. */
825 struct saved_macro *macro_list = (struct saved_macro *)NULL;
827 /* Forward declarations of static functions. Thank you C. */
828 static void push_executing_macro (), pop_executing_macro ();
830 /* This one has to be declared earlier in the file. */
831 /* static void add_macro_char (); */
833 /* Set up to read subsequent input from STRING.
834 STRING is free ()'ed when we are done with it. */
836 with_macro_input (string)
839 push_executing_macro ();
840 executing_macro = string;
841 executing_macro_index = 0;
844 /* Return the next character available from a macro, or 0 if
845 there are no macro characters. */
849 if (!executing_macro)
852 if (!executing_macro[executing_macro_index])
854 pop_executing_macro ();
855 return (next_macro_key ());
858 return (executing_macro[executing_macro_index++]);
861 /* Save the currently executing macro on a stack of saved macros. */
863 push_executing_macro ()
865 struct saved_macro *saver;
867 saver = (struct saved_macro *)xmalloc (sizeof (struct saved_macro));
868 saver->next = macro_list;
869 saver->index = executing_macro_index;
870 saver->string = executing_macro;
875 /* Discard the current macro, replacing it with the one
876 on the top of the stack of saved macros. */
878 pop_executing_macro ()
881 free (executing_macro);
883 executing_macro = (char *)NULL;
884 executing_macro_index = 0;
888 struct saved_macro *disposer = macro_list;
889 executing_macro = macro_list->string;
890 executing_macro_index = macro_list->index;
891 macro_list = macro_list->next;
896 /* Add a character to the macro being built. */
901 if (current_macro_index + 1 >= current_macro_size)
904 current_macro = (char *)xmalloc (current_macro_size = 25);
907 (char *)xrealloc (current_macro, current_macro_size += 25);
910 current_macro[current_macro_index++] = c;
911 current_macro[current_macro_index] = '\0';
914 /* Begin defining a keyboard macro.
915 Keystrokes are recorded as they are executed.
916 End the definition with rl_end_kbd_macro ().
917 If a numeric argument was explicitly typed, then append this
918 definition to the end of the existing macro, and start by
919 re-executing the existing macro. */
920 rl_start_kbd_macro (ignore1, ignore2)
921 int ignore1, ignore2;
923 if (defining_kbd_macro)
929 with_macro_input (savestring (current_macro));
932 current_macro_index = 0;
934 defining_kbd_macro = 1;
937 /* Stop defining a keyboard macro.
938 A numeric argument says to execute the macro right now,
939 that many times, counting the definition as the first time. */
940 rl_end_kbd_macro (count, ignore)
943 if (!defining_kbd_macro)
946 current_macro_index -= (rl_key_sequence_length - 1);
947 current_macro[current_macro_index] = '\0';
949 defining_kbd_macro = 0;
951 rl_call_last_kbd_macro (--count, 0);
954 /* Execute the most recently defined keyboard macro.
955 COUNT says how many times to execute it. */
956 rl_call_last_kbd_macro (count, ignore)
963 with_macro_input (savestring (current_macro));
967 /* **************************************************************** */
969 /* Initializations */
971 /* **************************************************************** */
973 /* Initliaze readline (and terminal if not already). */
976 extern char *rl_display_prompt;
978 /* If we have never been called before, initialize the
979 terminal and data structures. */
982 readline_initialize_everything ();
986 /* Initalize the current line information. */
987 rl_point = rl_end = 0;
988 the_line = rl_line_buffer;
991 /* We aren't done yet. We haven't even gotten started yet! */
994 /* Tell the history routines what is going on. */
995 start_using_history ();
997 /* Make the display buffer match the state of the line. */
999 extern char *rl_display_prompt;
1000 extern int forced_display;
1004 rl_display_prompt = rl_prompt ? rl_prompt : "";
1008 /* No such function typed yet. */
1009 rl_last_func = (Function *)NULL;
1011 /* Parsing of key-bindings begins in an enabled state. */
1012 parsing_conditionalized_out = 0;
1015 /* Initialize the entire state of the world. */
1016 readline_initialize_everything ()
1018 /* Find out if we are running in Emacs. */
1019 running_in_emacs = (char *)getenv ("EMACS");
1021 /* Allocate data structures. */
1022 if (!rl_line_buffer)
1024 (char *)xmalloc (rl_line_buffer_len = DEFAULT_BUFFER_SIZE);
1026 /* Initialize the terminal interface. */
1027 init_terminal_io ((char *)NULL);
1029 /* Bind tty characters to readline functions. */
1030 readline_default_bindings ();
1032 /* Initialize the function names. */
1033 rl_initialize_funmap ();
1035 /* Read in the init file. */
1036 rl_read_init_file ((char *)NULL);
1038 /* If the completion parser's default word break characters haven't
1039 been set yet, then do so now. */
1041 extern char *rl_completer_word_break_characters;
1042 extern char *rl_basic_word_break_characters;
1044 if (rl_completer_word_break_characters == (char *)NULL)
1045 rl_completer_word_break_characters = rl_basic_word_break_characters;
1049 /* If this system allows us to look at the values of the regular
1050 input editing characters, then bind them to their readline
1052 readline_default_bindings ()
1055 #ifdef NEW_TTY_DRIVER
1056 struct sgttyb ttybuff;
1057 int tty = fileno (rl_instream);
1059 if (ioctl (tty, TIOCGETP, &ttybuff) != -1)
1061 int erase = ttybuff.sg_erase, kill = ttybuff.sg_kill;
1063 if (erase != -1 && keymap[erase].type == ISFUNC)
1064 keymap[erase].function = rl_rubout;
1066 if (kill != -1 && keymap[kill].type == ISFUNC)
1067 keymap[kill].function = rl_unix_line_discard;
1074 if (ioctl (tty, TIOCGLTC, <) != -1)
1076 int erase = lt.t_werasc, nextc = lt.t_lnextc;
1078 if (erase != -1 && keymap[erase].type == ISFUNC)
1079 keymap[erase].function = rl_unix_word_rubout;
1081 if (nextc != -1 && keymap[nextc].type == ISFUNC)
1082 keymap[nextc].function = rl_quoted_insert;
1085 #endif /* TIOCGLTC */
1086 #else /* not NEW_TTY_DRIVER */
1087 struct termio ttybuff;
1088 int tty = fileno (rl_instream);
1090 if (ioctl (tty, TCGETA, &ttybuff) != -1)
1092 int erase = ttybuff.c_cc[VERASE];
1093 int kill = ttybuff.c_cc[VKILL];
1095 if (erase != -1 && keymap[(unsigned char)erase].type == ISFUNC)
1096 keymap[(unsigned char)erase].function = rl_rubout;
1098 if (kill != -1 && keymap[(unsigned char)kill].type == ISFUNC)
1099 keymap[(unsigned char)kill].function = rl_unix_line_discard;
1101 #endif /* NEW_TTY_DRIVER */
1105 /* **************************************************************** */
1107 /* Numeric Arguments */
1109 /* **************************************************************** */
1111 /* Handle C-u style numeric args, as well as M--, and M-digits. */
1113 /* Add the current digit to the argument in progress. */
1114 rl_digit_argument (ignore, key)
1117 rl_pending_input = key;
1121 /* What to do when you abort reading an argument. */
1122 rl_discard_argument ()
1125 rl_clear_message ();
1126 rl_init_argument ();
1129 /* Create a default argument. */
1132 rl_numeric_arg = arg_sign = 1;
1133 rl_explicit_arg = 0;
1136 /* C-u, universal argument. Multiply the current argument by 4.
1137 Read a key. If the key has nothing to do with arguments, then
1138 dispatch on it. If the key is the abort character then abort. */
1139 rl_universal_argument ()
1141 rl_numeric_arg *= 4;
1150 rl_message ("(arg: %d) ", arg_sign * rl_numeric_arg);
1151 key = c = rl_read_key ();
1153 if (keymap[c].type == ISFUNC &&
1154 keymap[c].function == rl_universal_argument)
1156 rl_numeric_arg *= 4;
1162 if (rl_explicit_arg)
1163 rl_numeric_arg = (rl_numeric_arg * 10) + (c - '0');
1165 rl_numeric_arg = (c - '0');
1166 rl_explicit_arg = 1;
1170 if (c == '-' && !rl_explicit_arg)
1177 rl_clear_message ();
1178 rl_dispatch (key, keymap);
1186 /* **************************************************************** */
1190 /* **************************************************************** */
1192 /* This is the stuff that is hard for me. I never seem to write good
1193 display routines in C. Let's see how I do this time. */
1195 /* (PWP) Well... Good for a simple line updater, but totally ignores
1196 the problems of input lines longer than the screen width.
1198 update_line and the code that calls it makes a multiple line,
1199 automatically wrapping line update. Carefull attention needs
1200 to be paid to the vertical position variables.
1202 handling of terminals with autowrap on (incl. DEC braindamage)
1203 could be improved a bit. Right now I just cheat and decrement
1204 screenwidth by one. */
1206 /* Keep two buffers; one which reflects the current contents of the
1207 screen, and the other to draw what we think the new contents should
1208 be. Then compare the buffers, and make whatever changes to the
1209 screen itself that we should. Finally, make the buffer that we
1210 just drew into be the one which reflects the current contents of the
1211 screen, and place the cursor where it belongs.
1213 Commands that want to can fix the display themselves, and then let
1214 this function know that the display has been fixed by setting the
1215 RL_DISPLAY_FIXED variable. This is good for efficiency. */
1217 /* Termcap variables: */
1218 extern char *term_up, *term_dc, *term_cr;
1219 extern int screenheight, screenwidth, terminal_can_insert;
1221 /* What YOU turn on when you have handled all redisplay yourself. */
1222 int rl_display_fixed = 0;
1224 /* The visible cursor position. If you print some text, adjust this. */
1228 /* The last left edge of text that was displayed. This is used when
1229 doing horizontal scrolling. It shifts in thirds of a screenwidth. */
1230 static int last_lmargin = 0;
1232 /* The line display buffers. One is the line currently displayed on
1233 the screen. The other is the line about to be displayed. */
1234 static char *visible_line = (char *)NULL;
1235 static char *invisible_line = (char *)NULL;
1237 /* Number of lines currently on screen minus 1. */
1240 /* A buffer for `modeline' messages. */
1243 /* Non-zero forces the redisplay even if we thought it was unnecessary. */
1244 int forced_display = 0;
1246 /* The stuff that gets printed out before the actual text of the line.
1247 This is usually pointing to rl_prompt. */
1248 char *rl_display_prompt = (char *)NULL;
1250 /* Default and initial buffer size. Can grow. */
1251 static int line_size = 1024;
1253 /* Non-zero means to always use horizontal scrolling in line display. */
1254 static int horizontal_scroll_mode = 0;
1256 /* Non-zero means to display an asterisk at the starts of history lines
1257 which have been modified. */
1258 static int mark_modified_lines = 0;
1260 /* I really disagree with this, but my boss (among others) insists that we
1261 support compilers that don't work. I don't think we are gaining by doing
1262 so; what is the advantage in producing better code if we can't use it? */
1263 /* The following two declarations belong inside the
1264 function block, not here. */
1265 static void move_cursor_relative ();
1266 static void output_some_chars ();
1267 static void output_character_function ();
1268 static int compare_strings ();
1270 /* Basic redisplay algorithm. */
1273 register int in, out, c, linenum;
1274 register char *line = invisible_line;
1276 int inv_botlin = 0; /* Number of lines in newly drawn buffer. */
1278 extern int readline_echoing_p;
1280 if (!readline_echoing_p)
1283 if (!rl_display_prompt)
1284 rl_display_prompt = "";
1286 if (!invisible_line)
1288 visible_line = (char *)xmalloc (line_size);
1289 invisible_line = (char *)xmalloc (line_size);
1290 line = invisible_line;
1291 for (in = 0; in < line_size; in++)
1293 visible_line[in] = 0;
1294 invisible_line[in] = 1;
1299 /* Draw the line into the buffer. */
1302 /* Mark the line as modified or not. We only do this for history
1305 if (mark_modified_lines && current_history () && rl_undo_list)
1311 /* If someone thought that the redisplay was handled, but the currently
1312 visible line has a different modification state than the one about
1313 to become visible, then correct the callers misconception. */
1314 if (visible_line[0] != invisible_line[0])
1315 rl_display_fixed = 0;
1317 strncpy (line + out, rl_display_prompt, strlen (rl_display_prompt));
1318 out += strlen (rl_display_prompt);
1321 for (in = 0; in < rl_end; in++)
1325 if (out + 1 >= line_size)
1328 visible_line = (char *)xrealloc (visible_line, line_size);
1329 invisible_line = (char *)xrealloc (invisible_line, line_size);
1330 line = invisible_line;
1340 line[out++] = c - 128;
1342 #define DISPLAY_TABS
1346 register int newout = (out | (int)7) + 1;
1347 while (out < newout)
1355 line[out++] = c + 64;
1364 /* PWP: now is when things get a bit hairy. The visible and invisible
1365 line buffers are really multiple lines, which would wrap every
1366 (screenwidth - 1) characters. Go through each in turn, finding
1367 the changed region and updating it. The line order is top to bottom. */
1369 /* If we can move the cursor up and down, then use multiple lines,
1370 otherwise, let long lines display in a single terminal line, and
1371 horizontally scroll it. */
1373 if (!horizontal_scroll_mode && term_up && *term_up)
1375 int total_screen_chars = (screenwidth * screenheight);
1377 if (!rl_display_fixed || forced_display)
1381 /* If we have more than a screenful of material to display, then
1382 only display a screenful. We should display the last screen,
1383 not the first. I'll fix this in a minute. */
1384 if (out >= total_screen_chars)
1385 out = total_screen_chars - 1;
1387 /* Number of screen lines to display. */
1388 inv_botlin = out / screenwidth;
1390 /* For each line in the buffer, do the updating display. */
1391 for (linenum = 0; linenum <= inv_botlin; linenum++)
1392 update_line (linenum > vis_botlin ? ""
1393 : &visible_line[linenum * screenwidth],
1394 &invisible_line[linenum * screenwidth],
1397 /* We may have deleted some lines. If so, clear the left over
1398 blank ones at the bottom out. */
1399 if (vis_botlin > inv_botlin)
1402 for (; linenum <= vis_botlin; linenum++)
1404 tt = &visible_line[linenum * screenwidth];
1405 move_vert (linenum);
1406 move_cursor_relative (0, tt);
1407 clear_to_eol ((linenum == vis_botlin)?
1408 strlen (tt) : screenwidth);
1411 vis_botlin = inv_botlin;
1413 /* Move the cursor where it should be. */
1414 move_vert (c_pos / screenwidth);
1415 move_cursor_relative (c_pos % screenwidth,
1416 &invisible_line[(c_pos / screenwidth) * screenwidth]);
1419 else /* Do horizontal scrolling. */
1423 /* Always at top line. */
1426 /* If the display position of the cursor would be off the edge
1427 of the screen, start the display of this line at an offset that
1428 leaves the cursor on the screen. */
1429 if (c_pos - last_lmargin > screenwidth - 2)
1430 lmargin = (c_pos / (screenwidth / 3) - 2) * (screenwidth / 3);
1431 else if (c_pos - last_lmargin < 1)
1432 lmargin = ((c_pos - 1) / (screenwidth / 3)) * (screenwidth / 3);
1434 lmargin = last_lmargin;
1436 /* If the first character on the screen isn't the first character
1437 in the display line, indicate this with a special character. */
1439 line[lmargin] = '<';
1441 if (lmargin + screenwidth < out)
1442 line[lmargin + screenwidth - 1] = '>';
1444 if (!rl_display_fixed || forced_display || lmargin != last_lmargin)
1447 update_line (&visible_line[last_lmargin],
1448 &invisible_line[lmargin], 0);
1450 move_cursor_relative (c_pos - lmargin, &invisible_line[lmargin]);
1451 last_lmargin = lmargin;
1454 fflush (out_stream);
1456 /* Swap visible and non-visible lines. */
1458 char *temp = visible_line;
1459 visible_line = invisible_line;
1460 invisible_line = temp;
1461 rl_display_fixed = 0;
1465 /* PWP: update_line() is based on finding the middle difference of each
1466 line on the screen; vis:
1468 /old first difference
1469 /beginning of line | /old last same /old EOL
1471 old: eddie> Oh, my little gruntle-buggy is to me, as lurgid as
1472 new: eddie> Oh, my little buggy says to me, as lurgid as
1474 \beginning of line | \new last same \new end of line
1475 \new first difference
1477 All are character pointers for the sake of speed. Special cases for
1478 no differences, as well as for end of line additions must be handeled.
1480 Could be made even smarter, but this works well enough */
1482 update_line (old, new, current_line)
1483 register char *old, *new;
1486 register char *ofd, *ols, *oe, *nfd, *nls, *ne;
1487 int lendiff, wsatend;
1489 /* Find first difference. */
1490 for (ofd = old, nfd = new;
1491 (ofd - old < screenwidth) && *ofd && (*ofd == *nfd);
1495 /* Move to the end of the screen line. */
1496 for (oe = ofd; ((oe - old) < screenwidth) && *oe; oe++);
1497 for (ne = nfd; ((ne - new) < screenwidth) && *ne; ne++);
1499 /* If no difference, continue to next line. */
1500 if (ofd == oe && nfd == ne)
1503 wsatend = 1; /* flag for trailing whitespace */
1504 ols = oe - 1; /* find last same */
1506 while ((*ols == *nls) && (ols > ofd) && (nls > nfd))
1519 else if (*ols != *nls)
1521 if (*ols) /* don't step past the NUL */
1527 move_vert (current_line);
1528 move_cursor_relative (ofd - old, old);
1530 /* if (len (new) > len (old)) */
1531 lendiff = (nls - nfd) - (ols - ofd);
1533 /* Insert (diff(len(old),len(new)) ch */
1536 if (terminal_can_insert)
1538 extern char *term_IC;
1540 /* Sometimes it is cheaper to print the characters rather than
1541 use the terminal's capabilities. */
1542 if ((2 * (ne - nfd)) < lendiff && !term_IC)
1544 output_some_chars (nfd, (ne - nfd));
1545 last_c_pos += (ne - nfd);
1551 insert_some_chars (nfd, lendiff);
1552 last_c_pos += lendiff;
1556 /* At the end of a line the characters do not have to
1557 be "inserted". They can just be placed on the screen. */
1558 output_some_chars (nfd, lendiff);
1559 last_c_pos += lendiff;
1561 /* Copy (new) chars to screen from first diff to last match. */
1562 if (((nls - nfd) - lendiff) > 0)
1564 output_some_chars (&nfd[lendiff], ((nls - nfd) - lendiff));
1565 last_c_pos += ((nls - nfd) - lendiff);
1570 { /* cannot insert chars, write to EOL */
1571 output_some_chars (nfd, (ne - nfd));
1572 last_c_pos += (ne - nfd);
1575 else /* Delete characters from line. */
1577 /* If possible and inexpensive to use terminal deletion, then do so. */
1578 if (term_dc && (2 * (ne - nfd)) >= (-lendiff))
1581 delete_chars (-lendiff); /* delete (diff) characters */
1583 /* Copy (new) chars to screen from first diff to last match */
1584 if ((nls - nfd) > 0)
1586 output_some_chars (nfd, (nls - nfd));
1587 last_c_pos += (nls - nfd);
1590 /* Otherwise, print over the existing material. */
1593 output_some_chars (nfd, (ne - nfd));
1594 last_c_pos += (ne - nfd);
1595 clear_to_eol ((oe - old) - (ne - new));
1600 /* (PWP) tell the update routines that we have moved onto a
1601 new (empty) line. */
1605 visible_line[0] = '\0';
1607 last_c_pos = last_v_pos = 0;
1608 vis_botlin = last_lmargin = 0;
1611 /* Actually update the display, period. */
1612 rl_forced_update_display ()
1616 register char *temp = visible_line;
1618 while (*temp) *temp++ = '\0';
1625 /* Move the cursor from last_c_pos to NEW, which are buffer indices.
1626 DATA is the contents of the screen line of interest; i.e., where
1627 the movement is being done. */
1629 move_cursor_relative (new, data)
1635 /* It may be faster to output a CR, and then move forwards instead
1636 of moving backwards. */
1637 if (new + 1 < last_c_pos - new)
1639 tputs (term_cr, 1, output_character_function);
1643 if (last_c_pos == new) return;
1645 if (last_c_pos < new)
1647 /* Move the cursor forward. We do it by printing the command
1648 to move the cursor forward if there is one, else print that
1649 portion of the output buffer again. Which is cheaper? */
1651 /* The above comment is left here for posterity. It is faster
1652 to print one character (non-control) than to print a control
1653 sequence telling the terminal to move forward one character.
1654 That kind of control is for people who don't know what the
1655 data is underneath the cursor. */
1656 #ifdef HACK_TERMCAP_MOTION
1657 extern char *term_forward_char;
1659 if (term_forward_char)
1660 for (i = last_c_pos; i < new; i++)
1661 tputs (term_forward_char, 1, output_character_function);
1663 for (i = last_c_pos; i < new; i++)
1664 putc (data[i], out_stream);
1666 for (i = last_c_pos; i < new; i++)
1667 putc (data[i], out_stream);
1668 #endif /* HACK_TERMCAP_MOTION */
1671 backspace (last_c_pos - new);
1675 /* PWP: move the cursor up or down. */
1679 void output_character_function ();
1680 register int delta, i;
1682 if (last_v_pos == to) return;
1684 if (to > screenheight)
1687 if ((delta = to - last_v_pos) > 0)
1689 for (i = 0; i < delta; i++)
1690 putc ('\n', out_stream);
1691 tputs (term_cr, 1, output_character_function);
1692 last_c_pos = 0; /* because crlf() will do \r\n */
1696 if (term_up && *term_up)
1697 for (i = 0; i < -delta; i++)
1698 tputs (term_up, 1, output_character_function);
1700 last_v_pos = to; /* now to is here */
1703 /* Physically print C on out_stream. This is for functions which know
1704 how to optimize the display. */
1710 fprintf (out_stream, "M-");
1715 if (c < 32 && c != '\t')
1724 putc (c, out_stream);
1725 fflush (out_stream);
1730 rl_character_len (c, pos)
1731 register int c, pos;
1733 if (c < ' ' || c > 126)
1736 return (((pos | (int)7) + 1) - pos);
1745 rl_character_len (c)
1748 if (c < ' ' || c > 126)
1753 #endif /* DISPLAY_TAB */
1755 /* How to print things in the "echo-area". The prompt is treated as a
1757 rl_message (string, arg1, arg2)
1760 sprintf (msg_buf, string, arg1, arg2);
1761 rl_display_prompt = msg_buf;
1765 /* How to clear things from the "echo-area". */
1768 rl_display_prompt = rl_prompt;
1772 /* **************************************************************** */
1774 /* Terminal and Termcap */
1776 /* **************************************************************** */
1778 static char *term_buffer = (char *)NULL;
1779 static char *term_string_buffer = (char *)NULL;
1781 /* Non-zero means this terminal can't really do anything. */
1787 /* Some strings to control terminal actions. These are output by tputs (). */
1788 char *term_goto, *term_clreol, *term_cr, *term_clrpag, *term_backspace;
1790 int screenwidth, screenheight;
1792 /* Non-zero if we determine that the terminal can do character insertion. */
1793 int terminal_can_insert = 0;
1795 /* How to insert characters. */
1796 char *term_im, *term_ei, *term_ic, *term_ip, *term_IC;
1798 /* How to delete characters. */
1799 char *term_dc, *term_DC;
1801 #ifdef HACK_TERMCAP_MOTION
1802 char *term_forward_char;
1803 #endif /* HACK_TERMCAP_MOTION */
1805 /* How to go up a line. */
1808 /* Re-initialize the terminal considering that the TERM/TERMCAP variable
1810 rl_reset_terminal (terminal_name)
1811 char *terminal_name;
1813 init_terminal_io (terminal_name);
1816 init_terminal_io (terminal_name)
1817 char *terminal_name;
1819 char *term = (terminal_name? terminal_name : (char *)getenv ("TERM"));
1820 char *tgetstr (), *buffer;
1823 if (!term_string_buffer)
1824 term_string_buffer = (char *)xmalloc (2048);
1827 term_buffer = (char *)xmalloc (2048);
1829 buffer = term_string_buffer;
1831 term_clrpag = term_cr = term_clreol = (char *)NULL;
1836 if (tgetent (term_buffer, term) < 0)
1842 BC = tgetstr ("pc", &buffer);
1843 PC = buffer ? *buffer : 0;
1845 term_backspace = tgetstr ("le", &buffer);
1847 term_cr = tgetstr ("cr", &buffer);
1848 term_clreol = tgetstr ("ce", &buffer);
1849 term_clrpag = tgetstr ("cl", &buffer);
1854 #ifdef HACK_TERMCAP_MOTION
1855 term_forward_char = tgetstr ("nd", &buffer);
1856 #endif /* HACK_TERMCAP_MOTION */
1858 screenwidth = tgetnum ("co");
1859 if (screenwidth <= 0)
1861 screenwidth--; /* PWP: avoid autowrap bugs */
1863 screenheight = tgetnum ("li");
1864 if (screenheight <= 0)
1867 term_im = tgetstr ("im", &buffer);
1868 term_ei = tgetstr ("ei", &buffer);
1869 term_IC = tgetstr ("IC", &buffer);
1870 term_ic = tgetstr ("ic", &buffer);
1872 /* "An application program can assume that the terminal can do
1873 character insertion if *any one of* the capabilities `IC',
1874 `im', `ic' or `ip' is provided." But we can't do anything if
1875 only `ip' is provided, so... */
1876 terminal_can_insert = (term_IC || term_im || term_ic);
1878 term_up = tgetstr ("up", &buffer);
1879 term_dc = tgetstr ("dc", &buffer);
1880 term_DC = tgetstr ("DC", &buffer);
1883 /* A function for the use of tputs () */
1885 output_character_function (c)
1888 putc (c, out_stream);
1891 /* Write COUNT characters from STRING to the output stream. */
1893 output_some_chars (string, count)
1897 fwrite (string, 1, count, out_stream);
1901 /* Delete COUNT characters from the display line. */
1903 delete_chars (count)
1906 if (count > screenwidth)
1909 if (term_DC && *term_DC)
1911 char *tgoto (), *buffer;
1912 buffer = tgoto (term_DC, 0, count);
1913 tputs (buffer, 1, output_character_function);
1917 if (term_dc && *term_dc)
1919 tputs (term_dc, 1, output_character_function);
1923 /* Insert COUNT character from STRING to the output stream. */
1925 insert_some_chars (string, count)
1929 /* If IC is defined, then we do not have to "enter" insert mode. */
1932 char *tgoto (), *buffer;
1933 buffer = tgoto (term_IC, 0, count);
1934 tputs (buffer, 1, output_character_function);
1935 output_some_chars (string, count);
1941 /* If we have to turn on insert-mode, then do so. */
1942 if (term_im && *term_im)
1943 tputs (term_im, 1, output_character_function);
1945 /* If there is a special command for inserting characters, then
1946 use that first to open up the space. */
1947 if (term_ic && *term_ic)
1949 for (i = count; i--; )
1950 tputs (term_ic, 1, output_character_function);
1953 /* Print the text. */
1954 output_some_chars (string, count);
1956 /* If there is a string to turn off insert mode, we had best use
1958 if (term_ei && *term_ei)
1959 tputs (term_ei, 1, output_character_function);
1963 /* Move the cursor back. */
1970 for (i = 0; i < count; i++)
1971 tputs (term_backspace, 1, output_character_function);
1973 for (i = 0; i < count; i++)
1974 putc ('\b', out_stream);
1977 /* Move to the start of the next line. */
1980 tputs (term_cr, 1, output_character_function);
1981 putc ('\n', out_stream);
1984 /* Clear to the end of the line. COUNT is the minimum
1985 number of character spaces to clear, */
1986 clear_to_eol (count)
1991 tputs (term_clreol, 1, output_character_function);
1997 /* Do one more character space. */
2000 for (i = 0; i < count; i++)
2001 putc (' ', out_stream);
2008 /* **************************************************************** */
2010 /* Saving and Restoring the TTY */
2012 /* **************************************************************** */
2014 /* Non-zero means that the terminal is in a prepped state. */
2015 static int terminal_prepped = 0;
2017 #ifdef NEW_TTY_DRIVER
2019 /* Standard flags, including ECHO. */
2020 static int original_tty_flags = 0;
2022 /* Local mode flags, like LPASS8. */
2023 static int local_mode_flags = 0;
2025 /* Terminal characters. This has C-s and C-q in it. */
2026 static struct tchars original_tchars;
2028 /* Local special characters. This has the interrupt characters in it. */
2029 static struct ltchars original_ltchars;
2031 /* We use this to get and set the tty_flags. */
2032 static struct sgttyb the_ttybuff;
2034 /* Put the terminal in CBREAK mode so that we can detect key presses. */
2038 int tty = fileno (rl_instream);
2039 int oldmask = sigblock (sigmask (SIGINT));
2041 if (!terminal_prepped)
2043 /* We always get the latest tty values. Maybe stty changed them. */
2044 ioctl (tty, TIOCGETP, &the_ttybuff);
2045 original_tty_flags = the_ttybuff.sg_flags;
2047 readline_echoing_p = (original_tty_flags & ECHO);
2050 #if defined (TIOCLGET)
2051 ioctl (tty, TIOCLGET, &local_mode_flags);
2054 /* If this terminal doesn't care how the 8th bit is used,
2055 then we can use it for the meta-key.
2056 We check by seeing if BOTH odd and even parity are allowed. */
2057 if ((the_ttybuff.sg_flags & ODDP) && (the_ttybuff.sg_flags & EVENP))
2060 the_ttybuff.sg_flags |= PASS8;
2062 /* Hack on local mode flags if we can. */
2063 #if defined (TIOCLGET) && defined (LPASS8)
2066 flags = local_mode_flags | LPASS8;
2067 ioctl (tty, TIOCLSET, &flags);
2076 ioctl (tty, TIOCGETC, &original_tchars);
2077 bcopy (&original_tchars, &temp, sizeof (struct tchars));
2079 /* Get rid of C-s and C-q.
2080 We remember the value of startc (C-q) so that if the terminal is in
2081 xoff state, the user can xon it by pressing that character. */
2082 xon_char = temp.t_startc;
2086 /* If there is an XON character, bind it to restart the output. */
2088 rl_bind_key (xon_char, rl_restart_output);
2090 /* If there is an EOF char, bind eof_char to it. */
2091 if (temp.t_eofc != -1)
2092 eof_char = temp.t_eofc;
2095 /* Get rid of C-\ and C-c. */
2096 temp.t_intrc = temp.t_quitc = -1;
2099 ioctl (tty, TIOCSETC, &temp);
2101 #endif /* TIOCGETC */
2105 struct ltchars temp;
2107 ioctl (tty, TIOCGLTC, &original_ltchars);
2108 bcopy (&original_ltchars, &temp, sizeof (struct ltchars));
2110 /* Make the interrupt keys go away. Just enough to make people happy. */
2111 temp.t_dsuspc = -1; /* C-y */
2112 temp.t_lnextc = -1; /* C-v */
2114 ioctl (tty, TIOCSLTC, &temp);
2116 #endif /* TIOCGLTC */
2118 the_ttybuff.sg_flags &= ~ECHO;
2119 the_ttybuff.sg_flags |= CBREAK;
2120 ioctl (tty, TIOCSETN, &the_ttybuff);
2122 terminal_prepped = 1;
2124 sigsetmask (oldmask);
2127 /* Restore the terminal to its original state. */
2129 rl_deprep_terminal ()
2131 int tty = fileno (rl_instream);
2132 int oldmask = sigblock (sigmask (SIGINT));
2134 if (terminal_prepped)
2136 the_ttybuff.sg_flags = original_tty_flags;
2137 ioctl (tty, TIOCSETN, &the_ttybuff);
2138 readline_echoing_p = 1;
2140 #if defined (TIOCLGET)
2141 ioctl (tty, TIOCLSET, &local_mode_flags);
2145 ioctl (tty, TIOCSLTC, &original_ltchars);
2149 ioctl (tty, TIOCSETC, &original_tchars);
2151 terminal_prepped = 0;
2154 sigsetmask (oldmask);
2157 #else /* !defined (NEW_TTY_DRIVER) */
2163 #if !defined (VTIME)
2167 static struct termio otio;
2172 int tty = fileno (rl_instream);
2175 ioctl (tty, TCGETA, &tio);
2176 ioctl (tty, TCGETA, &otio);
2178 readline_echoing_p = (tio.c_lflag & ECHO);
2180 tio.c_lflag &= ~(ICANON|ECHO);
2181 tio.c_iflag &= ~(IXON|IXOFF|IXANY|ISTRIP|INPCK);
2183 #if !defined (HANDLE_SIGNALS)
2184 tio.c_lflag &= ~ISIG;
2188 tio.c_cc[VTIME] = 0;
2189 ioctl (tty, TCSETAW, &tio);
2190 ioctl (tty, TCXONC, 1); /* Simulate a ^Q. */
2194 rl_deprep_terminal ()
2196 int tty = fileno (rl_instream);
2197 ioctl (tty, TCSETAW, &otio);
2198 ioctl (tty, TCXONC, 1); /* Simulate a ^Q. */
2200 #endif /* NEW_TTY_DRIVER */
2203 /* **************************************************************** */
2205 /* Utility Functions */
2207 /* **************************************************************** */
2209 /* Return 0 if C is not a member of the class of characters that belong
2210 in words, or 1 if it is. */
2212 int allow_pathname_alphabetic_chars = 0;
2213 char *pathname_alphabetic_chars = "/-_=~.#$";
2220 if (pure_alphabetic (c) || (numeric (c)))
2223 if (allow_pathname_alphabetic_chars)
2224 return ((int)rindex (pathname_alphabetic_chars, c));
2229 /* Return non-zero if C is a numeric character. */
2234 return (c >= '0' && c <= '9');
2237 /* Ring the terminal bell. */
2241 if (readline_echoing_p)
2243 fprintf (stderr, "\007");
2249 /* How to abort things. */
2253 rl_clear_message ();
2254 rl_init_argument ();
2255 rl_pending_input = 0;
2257 defining_kbd_macro = 0;
2258 while (executing_macro)
2259 pop_executing_macro ();
2261 longjmp (readline_top_level, 1);
2264 /* Return a copy of the string between FROM and TO.
2265 FROM is inclusive, TO is not. */
2270 register int length;
2273 /* Fix it if the caller is confused. */
2281 copy = (char *)xmalloc (1 + length);
2282 strncpy (copy, the_line + from, length);
2283 copy[length] = '\0';
2288 /* **************************************************************** */
2290 /* Insert and Delete */
2292 /* **************************************************************** */
2295 /* Insert a string of text into the line at point. This is the only
2296 way that you should do insertion. rl_insert () calls this
2298 rl_insert_text (string)
2301 extern int doing_an_undo;
2302 register int i, l = strlen (string);
2303 while (rl_end + l >= rl_line_buffer_len)
2306 (char *)xrealloc (rl_line_buffer,
2307 rl_line_buffer_len += DEFAULT_BUFFER_SIZE);
2308 the_line = rl_line_buffer;
2311 for (i = rl_end; i >= rl_point; i--)
2312 the_line[i + l] = the_line[i];
2313 strncpy (the_line + rl_point, string, l);
2315 /* Remember how to undo this if we aren't undoing something. */
2318 /* If possible and desirable, concatenate the undos. */
2319 if ((strlen (string) == 1) &&
2321 (rl_undo_list->what == UNDO_INSERT) &&
2322 (rl_undo_list->end == rl_point) &&
2323 (rl_undo_list->end - rl_undo_list->start < 20))
2324 rl_undo_list->end++;
2326 rl_add_undo (UNDO_INSERT, rl_point, rl_point + l, (char *)NULL);
2330 the_line[rl_end] = '\0';
2333 /* Delete the string between FROM and TO. FROM is
2334 inclusive, TO is not. */
2335 rl_delete_text (from, to)
2338 extern int doing_an_undo;
2339 register char *text;
2341 /* Fix it if the caller is confused. */
2347 text = rl_copy (from, to);
2348 strncpy (the_line + from, the_line + to, rl_end - to);
2350 /* Remember how to undo this delete. */
2352 rl_add_undo (UNDO_DELETE, from, to, text);
2356 rl_end -= (to - from);
2357 the_line[rl_end] = '\0';
2361 /* **************************************************************** */
2363 /* Readline character functions */
2365 /* **************************************************************** */
2367 /* This is not a gap editor, just a stupid line input routine. No hair
2368 is involved in writing any of the functions, and none should be. */
2372 rl_end is the place in the string that we would place '\0';
2373 i.e., it is always safe to place '\0' there.
2375 rl_point is the place in the string where the cursor is. Sometimes
2376 this is the same as rl_end.
2378 Any command that is called interactively receives two arguments.
2379 The first is a count: the numeric arg pased to this command.
2380 The second is the key which invoked this command.
2384 /* **************************************************************** */
2386 /* Movement Commands */
2388 /* **************************************************************** */
2390 /* Note that if you `optimize' the display for these functions, you cannot
2391 use said functions in other functions which do not do optimizing display.
2392 I.e., you will have to update the data base for rl_redisplay, and you
2393 might as well let rl_redisplay do that job. */
2395 /* Move forward COUNT characters. */
2400 rl_backward (-count);
2405 if (rl_point == (rl_end - (rl_editing_mode == vi_mode)))
2407 if (rl_point == rl_end)
2419 /* Move backward COUNT characters. */
2424 rl_forward (-count);
2439 /* Move to the beginning of the line. */
2445 /* Move to the end of the line. */
2451 /* Move forward a word. We do what Emacs does. */
2452 rl_forward_word (count)
2459 rl_backward_word (-count);
2465 if (rl_point == rl_end)
2468 /* If we are not in a word, move forward until we are in one.
2469 Then, move forward until we hit a non-alphabetic character. */
2470 c = the_line[rl_point];
2471 if (!alphabetic (c))
2473 while (++rl_point < rl_end)
2475 c = the_line[rl_point];
2476 if (alphabetic (c)) break;
2479 if (rl_point == rl_end) return;
2480 while (++rl_point < rl_end)
2482 c = the_line[rl_point];
2483 if (!alphabetic (c)) break;
2489 /* Move backward a word. We do what Emacs does. */
2490 rl_backward_word (count)
2497 rl_forward_word (-count);
2506 /* Like rl_forward_word (), except that we look at the characters
2507 just before point. */
2509 c = the_line[rl_point - 1];
2510 if (!alphabetic (c))
2514 c = the_line[rl_point - 1];
2515 if (alphabetic (c)) break;
2521 c = the_line[rl_point - 1];
2522 if (!alphabetic (c))
2530 /* Clear the current line. Numeric argument to C-l does this. */
2533 int curr_line = last_c_pos / screenwidth;
2534 extern char *term_clreol;
2536 move_vert(curr_line);
2537 move_cursor_relative (0, the_line); /* XXX is this right */
2540 tputs (term_clreol, 1, output_character_function);
2542 rl_forced_update_display ();
2543 rl_display_fixed = 1;
2546 /* C-l typed to a line without quoting clears the screen, and then reprints
2547 the prompt and the current input line. Given a numeric arg, redraw only
2548 the current line. */
2551 extern char *term_clrpag;
2553 if (rl_explicit_arg)
2560 tputs (term_clrpag, 1, output_character_function);
2564 rl_forced_update_display ();
2565 rl_display_fixed = 1;
2569 /* **************************************************************** */
2573 /* **************************************************************** */
2575 /* Insert the character C at the current location, moving point forward. */
2576 rl_insert (count, c)
2585 /* If we can optimize, then do it. But don't let people crash
2586 readline because of extra large arguments. */
2587 if (count > 1 && count < 1024)
2589 string = (char *)alloca (1 + count);
2591 for (i = 0; i < count; i++)
2595 rl_insert_text (string);
2603 string = (char *)alloca (1024 + 1);
2605 for (i = 0; i < 1024; i++)
2610 decreaser = (count > 1024 ? 1024 : count);
2611 string[decreaser] = '\0';
2612 rl_insert_text (string);
2618 /* We are inserting a single character.
2619 If there is pending input, then make a string of all of the
2620 pending characters that are bound to rl_insert, and insert
2627 string = (char *)alloca (ibuffer_len + 1);
2630 while ((t = rl_get_char (&key)) &&
2631 (keymap[key].type == ISFUNC &&
2632 keymap[key].function == rl_insert))
2636 rl_unget_char (key);
2639 rl_insert_text (string);
2644 /* Inserting a single character. */
2645 string = (char *)alloca (2);
2649 rl_insert_text (string);
2653 /* Insert the next typed character verbatim. */
2654 rl_quoted_insert (count)
2657 int c = rl_read_key ();
2658 rl_insert (count, c);
2661 /* Insert a tab character. */
2662 rl_tab_insert (count)
2665 rl_insert (count, '\t');
2668 /* What to do when a NEWLINE is pressed. We accept the whole line.
2669 KEY is the key that invoked this command. I guess it could have
2670 meaning in the future. */
2671 rl_newline (count, key)
2679 extern int vi_doing_insert;
2680 if (vi_doing_insert)
2682 rl_end_undo_group ();
2683 vi_doing_insert = 0;
2686 #endif /* VI_MODE */
2688 if (readline_echoing_p)
2690 move_vert (vis_botlin);
2693 fflush (out_stream);
2698 rl_clean_up_for_exit ()
2700 if (readline_echoing_p)
2702 move_vert (vis_botlin);
2704 fflush (out_stream);
2705 rl_restart_output ();
2709 /* What to do for some uppercase characters, like meta characters,
2710 and some characters appearing in emacs_ctlx_keymap. This function
2711 is just a stub, you bind keys to it and the code in rl_dispatch ()
2712 is special cased. */
2713 rl_do_lowercase_version (ignore1, ignore2)
2714 int ignore1, ignore2;
2718 /* Rubout the character behind point. */
2736 int orig_point = rl_point;
2737 rl_backward (count);
2738 rl_kill_text (orig_point, rl_point);
2742 int c = the_line[--rl_point];
2743 rl_delete_text (rl_point, rl_point + 1);
2745 if (rl_point == rl_end && alphabetic (c) && last_c_pos)
2748 putc (' ', out_stream);
2751 visible_line[last_c_pos] = '\0';
2757 /* Delete the character under the cursor. Given a numeric argument,
2758 kill that many characters instead. */
2759 rl_delete (count, invoking_key)
2760 int count, invoking_key;
2768 if (rl_point == rl_end)
2776 int orig_point = rl_point;
2778 rl_kill_text (orig_point, rl_point);
2779 rl_point = orig_point;
2782 rl_delete_text (rl_point, rl_point + 1);
2786 /* **************************************************************** */
2790 /* **************************************************************** */
2792 /* The next two functions mimic unix line editing behaviour, except they
2793 save the deleted text on the kill ring. This is safer than not saving
2794 it, and since we have a ring, nobody should get screwed. */
2796 /* This does what C-w does in Unix. We can't prevent people from
2797 using behaviour that they expect. */
2798 rl_unix_word_rubout ()
2800 if (!rl_point) ding ();
2802 int orig_point = rl_point;
2803 while (rl_point && whitespace (the_line[rl_point - 1]))
2805 while (rl_point && !whitespace (the_line[rl_point - 1]))
2807 rl_kill_text (rl_point, orig_point);
2811 /* Here is C-u doing what Unix does. You don't *have* to use these
2812 key-bindings. We have a choice of killing the entire line, or
2813 killing from where we are to the start of the line. We choose the
2814 latter, because if you are a Unix weenie, then you haven't backspaced
2815 into the line at all, and if you aren't, then you know what you are
2817 rl_unix_line_discard ()
2819 if (!rl_point) ding ();
2821 rl_kill_text (rl_point, 0);
2828 /* **************************************************************** */
2830 /* Commands For Typos */
2832 /* **************************************************************** */
2834 /* Random and interesting things in here. */
2837 /* **************************************************************** */
2841 /* **************************************************************** */
2843 /* The three kinds of things that we know how to do. */
2848 /* Uppercase the word at point. */
2849 rl_upcase_word (count)
2852 rl_change_case (count, UpCase);
2855 /* Lowercase the word at point. */
2856 rl_downcase_word (count)
2859 rl_change_case (count, DownCase);
2862 /* Upcase the first letter, downcase the rest. */
2863 rl_capitalize_word (count)
2866 rl_change_case (count, CapCase);
2869 /* The meaty function.
2870 Change the case of COUNT words, performing OP on them.
2871 OP is one of UpCase, DownCase, or CapCase.
2872 If a negative argument is given, leave point where it started,
2873 otherwise, leave it where it moves to. */
2874 rl_change_case (count, op)
2877 register int start = rl_point, end;
2880 rl_forward_word (count);
2890 /* We are going to modify some text, so let's prepare to undo it. */
2891 rl_modifying (start, end);
2893 for (; start < end; start++)
2898 the_line[start] = to_upper (the_line[start]);
2902 the_line[start] = to_lower (the_line[start]);
2908 the_line[start] = to_upper (the_line[start]);
2913 the_line[start] = to_lower (the_line[start]);
2915 if (!pure_alphabetic (the_line[start]))
2926 /* **************************************************************** */
2930 /* **************************************************************** */
2932 /* Transpose the words at point. */
2933 rl_transpose_words (count)
2936 char *word1, *word2;
2937 int w1_beg, w1_end, w2_beg, w2_end;
2938 int orig_point = rl_point;
2942 /* Find the two words. */
2943 rl_forward_word (count);
2945 rl_backward_word (1);
2947 rl_backward_word (count);
2949 rl_forward_word (1);
2952 /* Do some check to make sure that there really are two words. */
2953 if ((w1_beg == w2_beg) || (w2_beg < w1_end))
2956 rl_point = orig_point;
2960 /* Get the text of the words. */
2961 word1 = rl_copy (w1_beg, w1_end);
2962 word2 = rl_copy (w2_beg, w2_end);
2964 /* We are about to do many insertions and deletions. Remember them
2965 as one operation. */
2966 rl_begin_undo_group ();
2968 /* Do the stuff at word2 first, so that we don't have to worry
2969 about word1 moving. */
2971 rl_delete_text (w2_beg, w2_end);
2972 rl_insert_text (word1);
2975 rl_delete_text (w1_beg, w1_end);
2976 rl_insert_text (word2);
2978 /* This is exactly correct since the text before this point has not
2979 changed in length. */
2982 /* I think that does it. */
2983 rl_end_undo_group ();
2984 free (word1); free (word2);
2987 /* Transpose the characters at point. If point is at the end of the line,
2988 then transpose the characters before point. */
2989 rl_transpose_chars (count)
2995 if (!rl_point || rl_end < 2) {
3001 if (rl_point == rl_end) {
3002 int t = the_line[rl_point - 1];
3003 the_line[rl_point - 1] = the_line[rl_point - 2];
3004 the_line[rl_point - 2] = t;
3006 int t = the_line[rl_point];
3007 the_line[rl_point] = the_line[rl_point - 1];
3008 the_line[rl_point - 1] = t;
3009 if (count < 0 && rl_point)
3022 /* **************************************************************** */
3024 /* Bogus Flow Control */
3026 /* **************************************************************** */
3028 rl_restart_output (count, key)
3031 int fildes = fileno (stdin);
3033 ioctl (fildes, TIOCSTART, 0);
3034 #endif /* TIOCSTART */
3037 /* **************************************************************** */
3039 /* Completion matching, from readline's point of view. */
3041 /* **************************************************************** */
3043 /* Pointer to the generator function for completion_matches ().
3044 NULL means to use filename_entry_function (), the default filename
3046 Function *rl_completion_entry_function = (Function *)NULL;
3048 /* Pointer to alternative function to create matches.
3049 Function is called with TEXT, START, and END.
3050 START and END are indices in RL_LINE_BUFFER saying what the boundaries
3052 If this function exists and returns NULL then call the value of
3053 rl_completion_entry_function to try to match, otherwise use the
3054 array of strings returned. */
3055 Function *rl_attempted_completion_function = (Function *)NULL;
3057 /* Complete the word at or before point. You have supplied the function
3058 that does the initial simple matching selection algorithm (see
3059 completion_matches ()). The default is to do filename completion. */
3060 rl_complete (ignore, invoking_key)
3061 int ignore, invoking_key;
3063 rl_complete_internal (TAB);
3066 /* List the possible completions. See description of rl_complete (). */
3067 rl_possible_completions ()
3069 rl_complete_internal ('?');
3072 /* The user must press "y" or "n". Non-zero return means "y" pressed. */
3078 if (c == 'y' || c == 'Y') return (1);
3079 if (c == 'n' || c == 'N') return (0);
3080 if (c == ABORT_CHAR) rl_abort ();
3084 /* Up to this many items will be displayed in response to a
3085 possible-completions call. After that, we ask the user if
3086 she is sure she wants to see them all. */
3087 int rl_completion_query_items = 100;
3089 /* The basic list of characters that signal a break between words for the
3090 completer routine. The contents of this variable is what breaks words
3091 in the shell, i.e. " \t\n\"\\'`@$><=" */
3092 char *rl_basic_word_break_characters = " \t\n\"\\'`@$><=";
3094 /* The list of characters that signal a break between words for
3095 rl_complete_internal. The default list is the contents of
3096 rl_basic_word_break_characters. */
3097 char *rl_completer_word_break_characters = (char *)NULL;
3099 /* List of characters that are word break characters, but should be left
3100 in TEXT when it is passed to the completion function. The shell uses
3101 this to help determine what kind of completing to do. */
3102 char *rl_special_prefixes = (char *)NULL;
3104 /* If non-zero, then disallow duplicates in the matches. */
3105 int rl_ignore_completion_duplicates = 1;
3107 /* Non-zero means that the results of the matches are to be treated
3108 as filenames. This is ALWAYS zero on entry, and can only be changed
3109 within a completion entry finder function. */
3110 int rl_filename_completion_desired = 0;
3112 /* Complete the word at or before point.
3113 WHAT_TO_DO says what to do with the completion.
3114 `?' means list the possible completions.
3115 TAB means do standard completion.
3116 `*' means insert all of the possible completions. */
3117 rl_complete_internal (what_to_do)
3120 char *filename_completion_function ();
3121 char **completion_matches (), **matches;
3123 int start, end, delimiter = 0;
3126 if (rl_completion_entry_function)
3127 our_func = rl_completion_entry_function;
3129 our_func = (int (*)())filename_completion_function;
3131 /* Only the completion entry function can change this. */
3132 rl_filename_completion_desired = 0;
3134 /* We now look backwards for the start of a filename/variable word. */
3138 while (--rl_point &&
3139 !rindex (rl_completer_word_break_characters, the_line[rl_point]));
3141 /* If we are at a word break, then advance past it. */
3142 if (rindex (rl_completer_word_break_characters, (the_line[rl_point])))
3144 /* If the character that caused the word break was a quoting
3145 character, then remember it as the delimiter. */
3146 if (rindex ("\"'", the_line[rl_point]) && (end - rl_point) > 1)
3147 delimiter = the_line[rl_point];
3149 /* If the character isn't needed to determine something special
3150 about what kind of completion to perform, then advance past it. */
3152 if (!rl_special_prefixes ||
3153 !rindex (rl_special_prefixes, the_line[rl_point]))
3160 text = rl_copy (start, end);
3162 /* If the user wants to TRY to complete, but then wants to give
3163 up and use the default completion function, they set the
3164 variable rl_attempted_completion_function. */
3165 if (rl_attempted_completion_function)
3168 (char **)(*rl_attempted_completion_function) (text, start, end);
3171 goto after_usual_completion;
3174 matches = completion_matches (text, our_func, start, end);
3176 after_usual_completion:
3187 /* It seems to me that in all the cases we handle we would like
3188 to ignore duplicate possiblilities. Scan for the text to
3189 insert being identical to the other completions. */
3190 if (rl_ignore_completion_duplicates)
3192 char *lowest_common;
3195 /* Sort the items. */
3196 /* It is safe to sort this array, because the lowest common
3197 denominator found in matches[0] will remain in place. */
3198 for (i = 0; matches[i]; i++);
3199 qsort (matches, i, sizeof (char *), compare_strings);
3201 /* Remember the lowest common denimator for it may be unique. */
3202 lowest_common = savestring (matches[0]);
3204 for (i = 0; matches[i + 1]; i++)
3206 if (strcmp (matches[i], matches[i + 1]) == 0)
3209 matches[i] = (char *)-1;
3215 /* We have marked all the dead slots with (char *)-1.
3216 Copy all the non-dead entries into a new array. */
3219 (char **)malloc ((3 + newlen) * sizeof (char *));
3221 for (i = 1, j = 1; matches[i]; i++)
3222 if (matches[i] != (char *)-1)
3223 temp_array[j++] = matches[i];
3224 temp_array[j] = (char *)NULL;
3226 if (matches[0] != (char *)-1)
3230 matches = temp_array;
3233 /* Place the lowest common denominator back in [0]. */
3234 matches[0] = lowest_common;
3236 /* If there is one string left, and it is identical to the
3237 lowest common denominator, then the LCD is the string to
3239 if (j == 2 && strcmp (matches[0], matches[1]) == 0)
3242 matches[1] = (char *)NULL;
3251 rl_delete_text (start, rl_point);
3253 rl_insert_text (matches[0]);
3256 /* If there are more matches, ring the bell to indicate.
3257 If this was the only match, and we are hacking files,
3258 check the file to see if it was a directory. If so,
3259 add a '/' to the name. If not, and we are at the end
3260 of the line, then add a space. */
3263 ding (); /* There are other matches remaining. */
3267 char temp_string[2];
3269 temp_string[0] = delimiter ? delimiter : ' ';
3270 temp_string[1] = '\0';
3272 if (rl_filename_completion_desired)
3275 char *tilde_expand ();
3276 char *filename = tilde_expand (matches[0]);
3278 if ((stat (filename, &finfo) == 0) &&
3279 ((finfo.st_mode & S_IFMT) == S_IFDIR))
3281 if (the_line[rl_point] != '/')
3282 rl_insert_text ("/");
3286 if (rl_point == rl_end)
3287 rl_insert_text (temp_string);
3293 if (rl_point == rl_end)
3294 rl_insert_text (temp_string);
3303 rl_delete_text (start, rl_point);
3305 rl_begin_undo_group ();
3310 rl_insert_text (matches[i++]);
3311 rl_insert_text (" ");
3316 rl_insert_text (matches[0]);
3317 rl_insert_text (" ");
3319 rl_end_undo_group ();
3326 int len, count, limit, max = 0;
3329 /* Handle simple case first. What if there is only one answer? */
3332 char *rindex (), *temp;
3334 if (rl_filename_completion_desired)
3335 temp = rindex (matches[0], '/');
3337 temp = (char *)NULL;
3345 fprintf (out_stream, "%s", temp);
3350 /* There is more than one answer. Find out how many there are,
3351 and find out what the maximum printed length of a single entry
3353 for (i = 1; matches[i]; i++)
3355 char *rindex (), *temp = (char *)NULL;
3357 /* If we are hacking filenames, then only count the characters
3358 after the last slash in the pathname. */
3359 if (rl_filename_completion_desired)
3360 temp = rindex (matches[i], '/');
3362 temp = (char *)NULL;
3369 if (strlen (temp) > max)
3370 max = strlen (temp);
3375 /* If there are many items, then ask the user if she
3376 really wants to see them all. */
3377 if (len >= rl_completion_query_items)
3380 fprintf (out_stream,
3381 "There are %d possibilities. Do you really", len);
3383 fprintf (out_stream, "wish to see them all? (y or n)");
3384 fflush (out_stream);
3391 /* How many items of MAX length can we fit in the screen window? */
3393 limit = screenwidth / max;
3394 if (limit != 1 && (limit * max == screenwidth))
3397 /* How many iterations of the printing loop? */
3398 count = (len + (limit - 1)) / limit;
3400 /* Watch out for special case. If LEN is less than LIMIT, then
3401 just do the inner printing loop. */
3402 if (len < limit) count = 1;
3404 /* Sort the items if they are not already sorted. */
3405 if (!rl_ignore_completion_duplicates)
3406 qsort (matches, len, sizeof (char *), compare_strings);
3408 /* Print the sorted items, up-and-down alphabetically, like
3412 for (i = 1; i < count + 1; i++)
3414 for (j = 0, l = i; j < limit; j++)
3416 if (l > len || !matches[l])
3422 char *rindex (), *temp = (char *)NULL;
3424 if (rl_filename_completion_desired)
3425 temp = rindex (matches[l], '/');
3427 temp = (char *)NULL;
3434 fprintf (out_stream, "%s", temp);
3435 for (k = 0; k < max - strlen (temp); k++)
3436 putc (' ', out_stream);
3452 for (i = 0; matches[i]; i++)
3458 /* Stupid comparison routine for qsort () ing strings. */
3460 compare_strings (s1, s2)
3463 return (strcmp (*s1, *s2));
3466 /* A completion function for usernames.
3467 TEXT contains a partial username preceded by a random
3468 character (usually `~'). */
3470 username_completion_function (text, state)
3474 static char *username = (char *)NULL;
3475 static struct passwd *entry;
3482 username = savestring (&text[1]);
3483 namelen = strlen (username);
3487 while (entry = getpwent ())
3489 if (strncmp (username, entry->pw_name, namelen) == 0)
3496 return ((char *)NULL);
3500 char *value = (char *)xmalloc (2 + strlen (entry->pw_name));
3502 strcpy (value + 1, entry->pw_name);
3503 rl_filename_completion_desired = 1;
3508 /* If non-null, this contains the address of a function to call if the
3509 standard meaning for expanding a tilde fails. The function is called
3510 with the text (sans tilde, as in "foo"), and returns a malloc()'ed string
3511 which is the expansion, or a NULL pointer if there is no expansion. */
3512 Function *rl_tilde_expander = (Function *)NULL;
3514 /* Expand FILENAME if it begins with a tilde. This always returns
3517 tilde_expand (filename)
3520 char *dirname = filename ? savestring (filename) : (char *)NULL;
3522 if (dirname && *dirname == '~')
3525 if (!dirname[1] || dirname[1] == '/')
3527 /* Prepend $HOME to the rest of the string. */
3528 char *temp_home = (char *)getenv ("HOME");
3530 temp_name = (char *)alloca (1 + strlen (&dirname[1])
3531 + (temp_home? strlen (temp_home) : 0));
3532 temp_name[0] = '\0';
3534 strcpy (temp_name, temp_home);
3535 strcat (temp_name, &dirname[1]);
3537 dirname = savestring (temp_name);
3541 struct passwd *getpwnam (), *user_entry;
3542 char *username = (char *)alloca (257);
3545 for (i = 1; c = dirname[i]; i++)
3547 if (c == '/') break;
3548 else username[i - 1] = c;
3550 username[i - 1] = '\0';
3552 if (!(user_entry = getpwnam (username)))
3554 /* If the calling program has a special syntax for
3555 expanding tildes, and we couldn't find a standard
3556 expansion, then let them try. */
3557 if (rl_tilde_expander)
3561 expansion = (char *)(*rl_tilde_expander) (username);
3565 temp_name = (char *)alloca (1 + strlen (expansion)
3566 + strlen (&dirname[i]));
3567 strcpy (temp_name, expansion);
3568 strcat (temp_name, &dirname[i]);
3574 * We shouldn't report errors.
3579 temp_name = (char *)alloca (1 + strlen (user_entry->pw_dir)
3580 + strlen (&dirname[i]));
3581 strcpy (temp_name, user_entry->pw_dir);
3582 strcat (temp_name, &dirname[i]);
3585 dirname = savestring (temp_name);
3593 /* **************************************************************** */
3595 /* Undo, and Undoing */
3597 /* **************************************************************** */
3599 /* Non-zero tells rl_delete_text and rl_insert_text to not add to
3601 int doing_an_undo = 0;
3603 /* The current undo list for THE_LINE. */
3604 UNDO_LIST *rl_undo_list = (UNDO_LIST *)NULL;
3606 /* Remember how to undo something. Concatenate some undos if that
3608 rl_add_undo (what, start, end, text)
3609 enum undo_code what;
3613 UNDO_LIST *temp = (UNDO_LIST *)xmalloc (sizeof (UNDO_LIST));
3615 temp->start = start;
3618 temp->next = rl_undo_list;
3619 rl_undo_list = temp;
3622 /* Free the existing undo list. */
3625 while (rl_undo_list) {
3626 UNDO_LIST *release = rl_undo_list;
3627 rl_undo_list = rl_undo_list->next;
3629 if (release->what == UNDO_DELETE)
3630 free (release->text);
3636 /* Undo the next thing in the list. Return 0 if there
3637 is nothing to undo, or non-zero if there was. */
3642 int waiting_for_begin = 0;
3650 switch (rl_undo_list->what) {
3652 /* Undoing deletes means inserting some text. */
3654 rl_point = rl_undo_list->start;
3655 rl_insert_text (rl_undo_list->text);
3656 free (rl_undo_list->text);
3659 /* Undoing inserts means deleting some text. */
3661 rl_delete_text (rl_undo_list->start, rl_undo_list->end);
3662 rl_point = rl_undo_list->start;
3665 /* Undoing an END means undoing everything 'til we get to
3668 waiting_for_begin++;
3671 /* Undoing a BEGIN means that we are done with this group. */
3673 if (waiting_for_begin)
3674 waiting_for_begin--;
3682 release = rl_undo_list;
3683 rl_undo_list = rl_undo_list->next;
3686 if (waiting_for_begin)
3692 /* Begin a group. Subsequent undos are undone as an atomic operation. */
3693 rl_begin_undo_group ()
3695 rl_add_undo (UNDO_BEGIN, 0, 0, 0);
3698 /* End an undo group started with rl_begin_undo_group (). */
3699 rl_end_undo_group ()
3701 rl_add_undo (UNDO_END, 0, 0, 0);
3704 /* Save an undo entry for the text from START to END. */
3705 rl_modifying (start, end)
3717 char *temp = rl_copy (start, end);
3718 rl_begin_undo_group ();
3719 rl_add_undo (UNDO_DELETE, start, end, temp);
3720 rl_add_undo (UNDO_INSERT, start, end, (char *)NULL);
3721 rl_end_undo_group ();
3725 /* Revert the current line to its previous state. */
3728 if (!rl_undo_list) ding ();
3730 while (rl_undo_list)
3735 /* Do some undoing of things that were done. */
3736 rl_undo_command (count)
3738 if (count < 0) return; /* Nothing to do. */
3754 /* **************************************************************** */
3756 /* History Utilities */
3758 /* **************************************************************** */
3760 /* We already have a history library, and that is what we use to control
3761 the history features of readline. However, this is our local interface
3762 to the history mechanism. */
3764 /* While we are editing the history, this is the saved
3765 version of the original line. */
3766 HIST_ENTRY *saved_line_for_history = (HIST_ENTRY *)NULL;
3768 /* Set the history pointer back to the last entry in the history. */
3769 start_using_history ()
3772 if (saved_line_for_history)
3773 free_history_entry (saved_line_for_history);
3775 saved_line_for_history = (HIST_ENTRY *)NULL;
3778 /* Free the contents (and containing structure) of a HIST_ENTRY. */
3779 free_history_entry (entry)
3788 /* Perhaps put back the current line if it has changed. */
3789 maybe_replace_line ()
3791 HIST_ENTRY *temp = current_history ();
3793 /* If the current line has changed, save the changes. */
3794 if (temp && ((UNDO_LIST *)(temp->data) != rl_undo_list)) {
3795 temp = replace_history_entry (where_history (), the_line, rl_undo_list);
3801 /* Put back the saved_line_for_history if there is one. */
3802 maybe_unsave_line ()
3804 if (saved_line_for_history) {
3805 strcpy (the_line, saved_line_for_history->line);
3806 rl_undo_list = (UNDO_LIST *)saved_line_for_history->data;
3807 free_history_entry (saved_line_for_history);
3808 saved_line_for_history = (HIST_ENTRY *)NULL;
3809 rl_end = rl_point = strlen (the_line);
3815 /* Save the current line in saved_line_for_history. */
3818 if (!saved_line_for_history) {
3819 saved_line_for_history = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
3820 saved_line_for_history->line = savestring (the_line);
3821 saved_line_for_history->data = (char *)rl_undo_list;
3827 /* **************************************************************** */
3829 /* History Commands */
3831 /* **************************************************************** */
3833 /* Meta-< goes to the start of the history. */
3834 rl_beginning_of_history ()
3836 rl_get_previous_history (1 + where_history ());
3839 /* Meta-> goes to the end of the history. (The current line). */
3840 rl_end_of_history ()
3842 maybe_replace_line ();
3844 maybe_unsave_line ();
3847 /* Move down to the next history line. */
3848 rl_get_next_history (count)
3851 HIST_ENTRY *temp = (HIST_ENTRY *)NULL;
3855 rl_get_previous_history (-count);
3862 maybe_replace_line ();
3866 temp = next_history ();
3873 maybe_unsave_line ();
3876 strcpy (the_line, temp->line);
3877 rl_undo_list = (UNDO_LIST *)temp->data;
3878 rl_end = rl_point = strlen (the_line);
3882 /* Get the previous item out of our interactive history, making it the current
3883 line. If there is no previous history, just ding. */
3884 rl_get_previous_history (count)
3887 HIST_ENTRY *old_temp = (HIST_ENTRY *)NULL;
3888 HIST_ENTRY *temp = (HIST_ENTRY *)NULL;
3892 rl_get_next_history (-count);
3899 /* If we don't have a line saved, then save this one. */
3902 /* If the current line has changed, save the changes. */
3903 maybe_replace_line ();
3907 temp = previous_history ();
3915 /* If there was a large argument, and we moved back to the start of the
3916 history, that is not an error. So use the last value found. */
3917 if (!temp && old_temp)
3924 strcpy (the_line, temp->line);
3925 rl_undo_list = (UNDO_LIST *)temp->data;
3926 rl_end = rl_point = strlen (the_line);
3928 if (rl_editing_mode == vi_mode)
3930 #endif /* VI_MODE */
3934 /* There is a command in ksh which yanks into this line, the last word
3935 of the previous line. Here it is. We left it on M-. */
3936 rl_yank_previous_last_arg (ignore)
3943 /* **************************************************************** */
3945 /* I-Search and Searching */
3947 /* **************************************************************** */
3949 /* Search backwards through the history looking for a string which is typed
3950 interactively. Start with the current line. */
3951 rl_reverse_search_history (sign, key)
3955 rl_search_history (-sign, key);
3958 /* Search forwards through the history looking for a string which is typed
3959 interactively. Start with the current line. */
3960 rl_forward_search_history (sign, key)
3964 rl_search_history (sign, key);
3967 /* Display the current state of the search in the echo-area.
3968 SEARCH_STRING contains the string that is being searched for,
3969 DIRECTION is zero for forward, or 1 for reverse,
3970 WHERE is the history list number of the current line. If it is
3971 -1, then this line is the starting one. */
3972 rl_display_search (search_string, reverse_p, where)
3973 char *search_string;
3974 int reverse_p, where;
3976 char *message = (char *)NULL;
3979 (char *)alloca (1 + (search_string ? strlen (search_string) : 0) + 30);
3985 sprintf (message, "[%d]", where + history_base);
3988 strcat (message, "(");
3991 strcat (message, "reverse-");
3993 strcat (message, "i-search)`");
3996 strcat (message, search_string);
3998 strcat (message, "': ");
3999 rl_message (message, 0, 0);
4003 /* Search through the history looking for an interactively typed string.
4004 This is analogous to i-search. We start the search in the current line.
4005 DIRECTION is which direction to search; > 0 means forward, < 0 means
4007 rl_search_history (direction, invoking_key)
4011 /* The string that the user types in to search for. */
4012 char *search_string = (char *)alloca (128);
4014 /* The current length of SEARCH_STRING. */
4015 int search_string_index;
4017 /* The list of lines to search through. */
4020 /* The length of LINES. */
4023 /* Where we get LINES from. */
4024 HIST_ENTRY **hlist = history_list ();
4026 int orig_point = rl_point;
4027 int orig_line = where_history ();
4028 int last_found_line = orig_line;
4033 /* The line currently being searched. */
4036 /* Offset in that line. */
4039 /* Non-zero if we are doing a reverse search. */
4040 int reverse = (direction < 0);
4042 /* Create an arrary of pointers to the lines that we want to search. */
4044 maybe_replace_line ();
4046 for (i = 0; hlist[i]; i++);
4048 /* Allocate space for this many lines, +1 for the current input line,
4049 and remember those lines. */
4050 lines = (char **)alloca ((1 + (hlen = i)) * sizeof (char *));
4051 for (i = 0; i < hlen; i++)
4052 lines[i] = hlist[i]->line;
4054 if (saved_line_for_history)
4055 lines[i] = saved_line_for_history->line;
4058 /* So I have to type it in this way instead. */
4059 lines[i] = (char *)alloca (1 + strlen (the_line));
4060 strcpy (lines[i], &the_line[0]);
4065 /* The line where we start the search. */
4068 /* Initialize search parameters. */
4069 *search_string = '\0';
4070 search_string_index = 0;
4072 rl_display_search (search_string, reverse, -1);
4081 /* Hack C to Do What I Mean. */
4083 Function *f = (Function *)NULL;
4085 if (keymap[c].type == ISFUNC)
4086 f = keymap[c].function;
4088 if (f == rl_reverse_search_history)
4089 c = reverse ? -1 : -2;
4090 else if (f == rl_forward_search_history)
4091 c = !reverse ? -1 : -2;
4100 /* case invoking_key: */
4104 /* switch directions */
4106 direction = -direction;
4107 reverse = (direction < 0);
4112 strcpy (the_line, lines[orig_line]);
4113 rl_point = orig_point;
4114 rl_end = strlen (the_line);
4115 rl_clear_message ();
4119 if (c < 32 || c > 126)
4121 rl_execute_next (c);
4127 search_string[search_string_index++] = c;
4128 search_string[search_string_index] = '\0';
4133 if (!search_string_index)
4140 if (index != strlen (sline))
4155 search_string_index) == 0)
4162 register int limit =
4163 (strlen (sline) - search_string_index) + 1;
4165 while (index < limit)
4167 if (strncmp (search_string,
4169 search_string_index) == 0)
4178 /* At limit for direction? */
4179 if ((reverse && i < 0) ||
4180 (!reverse && i == hlen))
4185 index = strlen (sline);
4189 /* If the search string is longer than the current
4191 if (search_string_index > strlen (sline))
4194 /* Start actually searching. */
4196 index -= search_string_index;
4200 /* We cannot find the search string. Ding the bell. */
4202 i = last_found_line;
4206 /* We have found the search string. Just display it. But don't
4207 actually move there in the history list until the user accepts
4209 strcpy (the_line, lines[i]);
4211 rl_end = strlen (the_line);
4212 last_found_line = i;
4213 rl_display_search (search_string, reverse,
4214 (i == orig_line) ? -1 : i);
4219 /* The user has won. They found the string that they wanted. Now all
4220 we have to do is place them there. */
4222 int now = last_found_line;
4224 /* First put back the original state. */
4225 strcpy (the_line, lines[orig_line]);
4227 if (now < orig_line)
4228 rl_get_previous_history (orig_line - now);
4230 rl_get_next_history (now - orig_line);
4233 rl_clear_message ();
4237 /* Make C be the next command to be executed. */
4241 rl_pending_input = c;
4244 /* **************************************************************** */
4246 /* Killing Mechanism */
4248 /* **************************************************************** */
4250 /* What we assume for a max number of kills. */
4251 #define DEFAULT_MAX_KILLS 10
4253 /* The real variable to look at to find out when to flush kills. */
4254 int rl_max_kills = DEFAULT_MAX_KILLS;
4256 /* Where to store killed text. */
4257 char **rl_kill_ring = (char **)NULL;
4259 /* Where we are in the kill ring. */
4260 int rl_kill_index = 0;
4262 /* How many slots we have in the kill ring. */
4263 int rl_kill_ring_length = 0;
4265 /* How to say that you only want to save a certain amount
4266 of kill material. */
4267 rl_set_retained_kills (num)
4271 /* The way to kill something. This appends or prepends to the last
4272 kill, if the last command was a kill command. if FROM is less
4273 than TO, then the text is appended, otherwise prepended. If the
4274 last command was not a kill command, then a new slot is made for
4276 rl_kill_text (from, to)
4280 char *text = rl_copy (from, to);
4282 /* Is there anything to kill? */
4285 last_command_was_kill++;
4289 /* Delete the copied text from the line. */
4290 rl_delete_text (from, to);
4292 /* First, find the slot to work with. */
4293 if (!last_command_was_kill) {
4295 /* Get a new slot. */
4296 if (!rl_kill_ring) {
4298 /* If we don't have any defined, then make one. */
4300 (char **)xmalloc (((rl_kill_ring_length = 1) + 1) * sizeof (char *));
4305 /* We have to add a new slot on the end, unless we have exceeded
4306 the max limit for remembering kills. */
4307 slot = rl_kill_ring_length;
4308 if (slot == rl_max_kills) {
4310 free (rl_kill_ring[0]);
4311 for (i = 0; i < slot; i++)
4312 rl_kill_ring[i] = rl_kill_ring[i + 1];
4315 (char **)xrealloc (rl_kill_ring,
4316 ((slot = (rl_kill_ring_length += 1)) + 1)
4322 slot = rl_kill_ring_length - 1;
4325 /* If the last command was a kill, prepend or append. */
4326 if (last_command_was_kill) {
4327 char *old = rl_kill_ring[slot];
4328 char *new = (char *)xmalloc (1 + strlen (old) + strlen (text));
4339 rl_kill_ring[slot] = new;
4341 rl_kill_ring[slot] = text;
4343 rl_kill_index = slot;
4344 last_command_was_kill++;
4347 /* Now REMEMBER! In order to do prepending or appending correctly, kill
4348 commands always make rl_point's original position be the FROM argument,
4349 and rl_point's extent be the TO argument. */
4352 /* **************************************************************** */
4354 /* Killing Commands */
4356 /* **************************************************************** */
4358 /* Delete the word at point, saving the text in the kill ring. */
4359 rl_kill_word (count)
4362 int orig_point = rl_point;
4365 rl_backward_kill_word (-count);
4368 rl_forward_word (count);
4370 if (rl_point != orig_point)
4371 rl_kill_text (orig_point, rl_point);
4373 rl_point = orig_point;
4377 /* Rubout the word before point, placing it on the kill ring. */
4378 rl_backward_kill_word (count)
4381 int orig_point = rl_point;
4384 rl_kill_word (-count);
4387 rl_backward_word (count);
4389 if (rl_point != orig_point)
4390 rl_kill_text (orig_point, rl_point);
4394 /* Kill from here to the end of the line. If DIRECTION is negative, kill
4395 back to the line start instead. */
4396 rl_kill_line (direction)
4399 int orig_point = rl_point;
4402 rl_backward_kill_line (1);
4406 if (orig_point != rl_point)
4407 rl_kill_text (orig_point, rl_point);
4408 rl_point = orig_point;
4412 /* Kill backwards to the start of the line. If DIRECTION is negative, kill
4413 forwards to the line end instead. */
4414 rl_backward_kill_line (direction)
4417 int orig_point = rl_point;
4428 rl_kill_text (orig_point, rl_point);
4433 /* Yank back the last killed text. This ignores arguments. */
4436 if (!rl_kill_ring) rl_abort ();
4437 rl_insert_text (rl_kill_ring[rl_kill_index]);
4440 /* If the last command was yank, or yank_pop, and the text just
4441 before point is identical to the current kill item, then
4442 delete that text from the line, rotate the index down, and
4443 yank back some other text. */
4448 if (((rl_last_func != rl_yank_pop) && (rl_last_func != rl_yank)) ||
4454 l = strlen (rl_kill_ring[rl_kill_index]);
4455 if (((rl_point - l) >= 0) &&
4456 (strncmp (the_line + (rl_point - l),
4457 rl_kill_ring[rl_kill_index], l) == 0))
4459 rl_delete_text ((rl_point - l), rl_point);
4462 if (rl_kill_index < 0)
4463 rl_kill_index = rl_kill_ring_length - 1;
4471 /* Yank the COUNTth argument from the previous history line. */
4472 rl_yank_nth_arg (count, ignore)
4475 register HIST_ENTRY *entry = previous_history ();
4486 arg = history_arg_extract (count, count, entry->line);
4493 rl_begin_undo_group ();
4494 if (rl_point && the_line[rl_point - 1] != ' ')
4495 rl_insert_text (" ");
4496 rl_insert_text (arg);
4498 rl_end_undo_group ();
4503 #include "vi_mode.c"
4504 #endif /* VI_MODE */
4506 /* How to toggle back and forth between editing modes. */
4507 rl_vi_editing_mode ()
4510 rl_editing_mode = vi_mode;
4511 rl_vi_insertion_mode ();
4512 #endif /* VI_MODE */
4515 rl_emacs_editing_mode ()
4517 rl_editing_mode = emacs_mode;
4518 keymap = emacs_standard_keymap;
4522 /* **************************************************************** */
4526 /* **************************************************************** */
4528 /* Non-zero means that case is not significant in completion. */
4529 int completion_case_fold = 0;
4531 /* Return an array of (char *) which is a list of completions for TEXT.
4532 If there are no completions, return a NULL pointer.
4533 The first entry in the returned array is the substitution for TEXT.
4534 The remaining entries are the possible completions.
4535 The array is terminated with a NULL pointer.
4537 ENTRY_FUNCTION is a function of two args, and returns a (char *).
4538 The first argument is TEXT.
4539 The second is a state argument; it should be zero on the first call, and
4540 non-zero on subsequent calls. It returns a NULL pointer to the caller
4541 when there are no more matches.
4544 completion_matches (text, entry_function)
4546 char *(*entry_function) ();
4548 /* Number of slots in match_list. */
4549 int match_list_size;
4551 /* The list of matches. */
4553 (char **)xmalloc (((match_list_size = 10) + 1) * sizeof (char *));
4555 /* Number of matches actually found. */
4558 /* Temporary string binder. */
4561 match_list[1] = (char *)NULL;
4563 while (string = (*entry_function) (text, matches))
4565 if (matches + 1 == match_list_size)
4567 (char **)xrealloc (match_list,
4568 ((match_list_size += 10) + 1) * sizeof (char *));
4570 match_list[++matches] = string;
4571 match_list[matches + 1] = (char *)NULL;
4574 /* If there were any matches, then look through them finding out the
4575 lowest common denominator. That then becomes match_list[0]. */
4579 int low = 100000; /* Count of max-matched characters. */
4581 /* If only one match, just use that. */
4584 match_list[0] = match_list[1];
4585 match_list[1] = (char *)NULL;
4589 /* Otherwise, compare each member of the list with
4590 the next, finding out where they stop matching. */
4594 register int c1, c2, si;
4596 if (completion_case_fold)
4599 (c1 = to_lower(match_list[i][si])) &&
4600 (c2 = to_lower(match_list[i + 1][si]));
4602 if (c1 != c2) break;
4607 (c1 = match_list[i][si]) &&
4608 (c2 = match_list[i + 1][si]);
4610 if (c1 != c2) break;
4613 if (low > si) low = si;
4616 match_list[0] = (char *)xmalloc (low + 1);
4617 strncpy (match_list[0], match_list[1], low);
4618 match_list[0][low] = '\0';
4621 else /* There were no matches. */
4624 match_list = (char **)NULL;
4626 return (match_list);
4629 /* Okay, now we write the entry_function for filename completion. In the
4630 general case. Note that completion in the shell is a little different
4631 because of all the pathnames that must be followed when looking up the
4632 completion for a command. */
4634 filename_completion_function (text, state)
4638 static DIR *directory;
4639 static char *filename = (char *)NULL;
4640 static char *dirname = (char *)NULL;
4641 static char *users_dirname = (char *)NULL;
4642 static int filename_len;
4644 struct direct *entry = (struct direct *)NULL;
4646 /* If we don't have any state, then do some initialization. */
4649 char *rindex (), *temp;
4651 if (dirname) free (dirname);
4652 if (filename) free (filename);
4653 if (users_dirname) free (users_dirname);
4655 filename = savestring (text);
4656 if (!*text) text = ".";
4657 dirname = savestring (text);
4659 temp = rindex (dirname, '/');
4663 strcpy (filename, ++temp);
4667 strcpy (dirname, ".");
4669 /* We aren't done yet. We also support the "~user" syntax. */
4671 /* Save the version of the directory that the user typed. */
4672 users_dirname = savestring (dirname);
4674 char *tilde_expand (), *temp_dirname = tilde_expand (dirname);
4676 dirname = temp_dirname;
4678 if (rl_symbolic_link_hook)
4679 (*rl_symbolic_link_hook) (&dirname);
4681 directory = opendir (dirname);
4682 filename_len = strlen (filename);
4684 rl_filename_completion_desired = 1;
4687 /* At this point we should entertain the possibility of hacking wildcarded
4688 filenames, like /usr/man*\/te<TAB>. If the directory name contains
4689 globbing characters, then build an array of directories to glob on, and
4690 glob on the first one. */
4692 /* Now that we have some state, we can read the directory. */
4694 while (directory && (entry = readdir (directory)))
4696 /* Special case for no filename.
4697 All entries except "." and ".." match. */
4700 if ((strcmp (entry->d_name, ".") != 0) &&
4701 (strcmp (entry->d_name, "..") != 0))
4706 /* Otherwise, if these match upto the length of filename, then
4709 if ((strlen (entry->d_name) >= filename_len) &&
4710 (strncmp (filename, entry->d_name, filename_len) == 0))
4712 if ((entry->d_namlen >= filename_len) &&
4713 (strncmp (filename, entry->d_name, filename_len) == 0))
4714 #endif /* TMB_SYSV */
4725 closedir (directory);
4726 directory = (DIR *)NULL;
4728 return (char *)NULL;
4734 if (dirname && (strcmp (dirname, ".") != 0))
4737 temp = (char *)xmalloc (1 + strlen (users_dirname)
4738 + strlen (entry->d_name));
4740 temp = (char *)xmalloc (1 + strlen (users_dirname)
4742 #endif /* TMB_SYSV */
4743 strcpy (temp, users_dirname);
4744 strcat (temp, entry->d_name);
4748 temp = (savestring (entry->d_name));
4755 /* **************************************************************** */
4759 /* **************************************************************** */
4761 /* rl_add_defun (char *name, Function *function, int key)
4762 Add NAME to the list of named functions. Make FUNCTION
4763 be the function that gets called.
4764 If KEY is not -1, then bind it. */
4765 rl_add_defun (name, function, key)
4771 rl_bind_key (key, function);
4772 rl_add_funmap_entry (name, function);
4775 /* Bind KEY to FUNCTION. Returns non-zero if KEY is out of range. */
4777 rl_bind_key (key, function)
4784 if (key > 127 && key < 256)
4786 if (keymap[ESC].type == ISKMAP)
4788 Keymap escmap = (Keymap)keymap[ESC].function;
4791 escmap[key].type = ISFUNC;
4792 escmap[key].function = function;
4798 keymap[key].type = ISFUNC;
4799 keymap[key].function = function;
4803 /* Bind KEY to FUNCTION in MAP. Returns non-zero in case of invalid
4806 rl_bind_key_in_map (key, function, map)
4812 Keymap oldmap = keymap;
4815 result = rl_bind_key (key, function);
4820 /* Make KEY do nothing in the currently selected keymap.
4821 Returns non-zero in case of error. */
4826 return (rl_bind_key (key, (Function *)NULL));
4829 /* Make KEY do nothing in MAP.
4830 Returns non-zero in case of error. */
4832 rl_unbind_key_in_map (key, map)
4836 return (rl_bind_key_in_map (key, (Function *)NULL, map));
4839 /* Bind the key sequence represented by the string KEYSEQ to
4840 FUNCTION. This makes new keymaps as necessary. The initial
4841 place to do bindings is in MAP. */
4842 rl_set_key (keyseq, function, map)
4847 rl_generic_bind (ISFUNC, keyseq, function, map);
4850 /* Bind the key sequence represented by the string KEYSEQ to
4851 the string of characters MACRO. This makes new keymaps as
4852 necessary. The initial place to do bindings is in MAP. */
4853 rl_macro_bind (keyseq, macro, map)
4854 char *keyseq, *macro;
4857 char *macro_keys = (char *)xmalloc (2 * (strlen (macro)));
4860 if (rl_translate_keyseq (macro, macro_keys, ¯o_keys_len))
4865 rl_generic_bind (ISMACR, keyseq, macro_keys, map);
4868 /* Bind the key sequence represented by the string KEYSEQ to
4869 the arbitrary pointer DATA. TYPE says what kind of data is
4870 pointed to by DATA, right now this can be a function (ISFUNC),
4871 a macro (ISMACR), or a keymap (ISKMAP). This makes new keymaps
4872 as necessary. The initial place to do bindings is in MAP. */
4873 rl_generic_bind (type, keyseq, data, map)
4875 char *keyseq, *data;
4882 /* If no keys to bind to, exit right away. */
4883 if (!keyseq || !*keyseq)
4890 keys = (char *)alloca (1 + (2 * strlen (keyseq)));
4892 /* Translate the ASCII representation of KEYSEQ into an array
4893 of characters. Stuff the characters into ARRAY, and the
4894 length of ARRAY into LENGTH. */
4895 if (rl_translate_keyseq (keyseq, keys, &keys_len))
4898 /* Bind keys, making new keymaps as necessary. */
4899 for (i = 0; i < keys_len; i++)
4901 if (i + 1 < keys_len)
4903 if (map[keys[i]].type != ISKMAP)
4905 if (map[i].type == ISMACR)
4906 free ((char *)map[i].function);
4908 map[keys[i]].type = ISKMAP;
4909 map[keys[i]].function = (Function *)rl_make_bare_keymap ();
4911 map = (Keymap)map[keys[i]].function;
4915 if (map[keys[i]].type == ISMACR)
4916 free ((char *)map[keys[i]].function);
4918 map[keys[i]].function = (Function *)data;
4919 map[keys[i]].type = type;
4924 /* Translate the ASCII representation of SEQ, stuffing the
4925 values into ARRAY, an array of characters. LEN gets the
4926 final length of ARRAY. Return non-zero if there was an
4927 error parsing SEQ. */
4928 rl_translate_keyseq (seq, array, len)
4932 register int i, c, l = 0;
4934 for (i = 0; c = seq[i]; i++)
4943 if (((c == 'C' || c == 'M') && seq[i + 1] == '-') ||
4946 /* Handle special case of backwards define. */
4947 if (strncmp (&seq[i], "C-\\M-", 5) == 0)
4951 array[l++] = CTRL (to_upper (seq[i]));
4966 array[l++] = CTRL (to_upper (seq[i]));
4984 /* Return a pointer to the function that STRING represents.
4985 If STRING doesn't have a matching function, then a NULL pointer
4988 rl_named_function (string)
4993 for (i = 0; funmap[i]; i++)
4994 if (stricmp (funmap[i]->name, string) == 0)
4995 return (funmap[i]->function);
4996 return ((Function *)NULL);
4999 /* The last key bindings file read. */
5000 static char *last_readline_init_file = "~/.inputrc";
5002 /* Re-read the current keybindings file. */
5003 rl_re_read_init_file (count, ignore)
5006 rl_read_init_file (last_readline_init_file);
5009 /* Do key bindings from a file. If FILENAME is NULL it defaults
5010 to `~/.inputrc'. If the file existed and could be opened and
5011 read, 0 is returned, otherwise errno is returned. */
5013 rl_read_init_file (filename)
5017 int line_size, line_index;
5018 char *line = (char *)xmalloc (line_size = 100);
5024 /* Default the filename. */
5026 filename = "~/.inputrc";
5028 openname = tilde_expand (filename);
5030 /* Open the file. */
5031 file = fopen (openname, "r");
5037 last_readline_init_file = filename;
5039 /* Loop reading lines from the file. Lines that start with `#' are
5040 comments, all other lines are commands for readline initialization. */
5041 while ((c = rl_getc (file)) != EOF)
5043 /* If comment, flush to EOL. */
5046 while ((c = rl_getc (file)) != EOF && c != '\n');
5052 /* Otherwise, this is the start of a line. Read the
5053 line from the file. */
5055 while (c != EOF && c != '\n')
5057 line[line_index++] = c;
5058 if (line_index == line_size)
5059 line = (char *)xrealloc (line, line_size += 100);
5062 line[line_index] = '\0';
5064 /* Parse the line. */
5065 rl_parse_and_bind (line);
5071 /* Close up the file and exit. */
5077 /* **************************************************************** */
5079 /* Parser Directives */
5081 /* **************************************************************** */
5085 /* Calling programs set this to have their argv[0]. */
5086 char *rl_readline_name = "other";
5088 /* Stack of previous values of parsing_conditionalized_out. */
5089 static unsigned char *if_stack = (unsigned char *)NULL;
5090 static int if_stack_depth = 0;
5091 static int if_stack_size = 0;
5093 /* Push parsing_conditionalized_out, and set parser state based on ARGS. */
5099 /* Push parser state. */
5100 if (if_stack_depth + 1 >= if_stack_size)
5103 if_stack = (unsigned char *)xmalloc (if_stack_size = 20);
5105 if_stack = (unsigned char *)xrealloc (if_stack, if_stack_size += 20);
5107 if_stack[if_stack_depth++] = parsing_conditionalized_out;
5109 /* We only check to see if the first word in ARGS is the same as the
5110 value stored in rl_readline_name. */
5112 /* Isolate first argument. */
5113 for (i = 0; args[i] && !whitespace (args[i]); i++);
5118 if (stricmp (args, rl_readline_name) == 0)
5119 parsing_conditionalized_out = 0;
5121 parsing_conditionalized_out = 1;
5124 /* Invert the current parser state if there is anything on the stack. */
5129 parsing_conditionalized_out = !parsing_conditionalized_out;
5132 /* *** What, no error message? *** */
5136 /* Terminate a conditional, popping the value of
5137 parsing_conditionalized_out from the stack. */
5142 parsing_conditionalized_out = if_stack[--if_stack_depth];
5145 /* *** What, no error message? *** */
5149 /* Associate textual names with actual functions. */
5153 } parser_directives [] = {
5154 { "if", parser_if },
5155 { "endif", parser_endif },
5156 { "else", parser_else },
5157 { (char *)0x0, (Function *)0x0 }
5160 /* Handle a parser directive. STATEMENT is the line of the directive
5161 without any leading `$'. */
5163 handle_parser_directive (statement)
5167 char *directive, *args;
5169 /* Isolate the actual directive. */
5171 /* Skip whitespace. */
5172 for (i = 0; whitespace (statement[i]); i++);
5174 directive = &statement[i];
5176 for (; statement[i] && !whitespace (statement[i]); i++);
5179 statement[i++] = '\0';
5181 for (; statement[i] && whitespace (statement[i]); i++);
5183 args = &statement[i];
5185 /* Lookup the command, and act on it. */
5186 for (i = 0; parser_directives[i].name; i++)
5187 if (stricmp (directive, parser_directives[i].name) == 0)
5189 (*parser_directives[i].function) (args);
5193 /* *** Should an error message be output? */
5197 /* Read the binding command from STRING and perform it.
5198 A key binding command looks like: Keyname: function-name\0,
5199 a variable binding command looks like: set variable value.
5200 A new-style keybinding looks like "\C-x\C-x": exchange-point-and-mark. */
5201 rl_parse_and_bind (string)
5204 extern char *possible_control_prefixes[], *possible_meta_prefixes[];
5205 char *rindex (), *funname, *kname;
5206 static int substring_member_of_array ();
5210 if (!string || !*string || *string == '#')
5213 /* If this is a parser directive, act on it. */
5216 handle_parser_directive (&string[1]);
5220 /* If we are supposed to be skipping parsing right now, then do it. */
5221 if (parsing_conditionalized_out)
5225 /* If this keyname is a complex key expression surrounded by quotes,
5226 advance to after the matching close quote. */
5229 for (i = 1; c = string[i]; i++)
5231 if (c == '"' && string[i - 1] != '\\')
5236 /* Advance to the colon (:) or whitespace which separates the two objects. */
5237 for (; (c = string[i]) && c != ':' && c != ' ' && c != '\t'; i++ );
5239 /* Mark the end of the command (or keyname). */
5243 /* If this is a command to set a variable, then do that. */
5244 if (stricmp (string, "set") == 0)
5246 char *var = string + i;
5249 /* Make VAR point to start of variable name. */
5250 while (*var && whitespace (*var)) var++;
5252 /* Make value point to start of value string. */
5254 while (*value && !whitespace (*value)) value++;
5257 while (*value && whitespace (*value)) value++;
5259 rl_variable_bind (var, value);
5263 /* Skip any whitespace between keyname and funname. */
5264 for (; string[i] && whitespace (string[i]); i++);
5265 funname = &string[i];
5267 /* Now isolate funname.
5268 For straight function names just look for whitespace, since
5269 that will signify the end of the string. But this could be a
5270 macro definition. In that case, the string is quoted, so skip
5271 to the matching delimiter. */
5272 if (*funname == '\'' || *funname == '"')
5274 int delimiter = string[i++];
5276 for (; c = string[i]; i++)
5278 if (c == delimiter && string[i - 1] != '\\')
5285 /* Advance to the end of the string. */
5286 for (; string[i] && !whitespace (string[i]); i++);
5288 /* No extra whitespace at the end of the string. */
5291 /* If this is a new-style key-binding, then do the binding with
5292 rl_set_key (). Otherwise, let the older code deal with it. */
5295 char *seq = (char *)alloca (1 + strlen (string));
5296 register int j, k = 0;
5298 for (j = 1; string[j]; j++)
5300 if (string[j] == '"' && string[j - 1] != '\\')
5303 seq[k++] = string[j];
5307 /* Binding macro? */
5308 if (*funname == '\'' || *funname == '"')
5310 j = strlen (funname);
5312 if (j && funname[j - 1] == *funname)
5313 funname[j - 1] = '\0';
5315 rl_macro_bind (seq, &funname[1], keymap);
5318 rl_set_key (seq, rl_named_function (funname), keymap);
5323 /* Get the actual character we want to deal with. */
5324 kname = rindex (string, '-');
5330 key = glean_key_from_name (kname);
5332 /* Add in control and meta bits. */
5333 if (substring_member_of_array (string, possible_control_prefixes))
5334 key = CTRL (to_upper (key));
5336 if (substring_member_of_array (string, possible_meta_prefixes))
5339 /* Temporary. Handle old-style keyname with macro-binding. */
5340 if (*funname == '\'' || *funname == '"')
5343 int fl = strlen (funname);
5345 seq[0] = key; seq[1] = '\0';
5346 if (fl && funname[fl - 1] == *funname)
5347 funname[fl - 1] = '\0';
5349 rl_macro_bind (seq, &funname[1], keymap);
5352 rl_bind_key (key, rl_named_function (funname));
5355 rl_variable_bind (name, value)
5358 if (stricmp (name, "editing-mode") == 0)
5360 if (strnicmp (value, "vi", 2) == 0)
5363 keymap = vi_insertion_keymap;
5364 rl_editing_mode = vi_mode;
5365 #endif /* VI_MODE */
5367 else if (strnicmp (value, "emacs", 5) == 0)
5369 keymap = emacs_standard_keymap;
5370 rl_editing_mode = emacs_mode;
5373 else if (stricmp (name, "horizontal-scroll-mode") == 0)
5375 if (!*value || stricmp (value, "On") == 0)
5376 horizontal_scroll_mode = 1;
5378 horizontal_scroll_mode = 0;
5380 else if (stricmp (name, "mark-modified-lines") == 0)
5382 if (!*value || stricmp (value, "On") == 0)
5383 mark_modified_lines = 1;
5385 mark_modified_lines = 0;
5389 /* Return the character which matches NAME.
5390 For example, `Space' returns ' '. */
5397 assoc_list name_key_alist[] = {
5403 { "Newline", '\n' },
5407 { "Escape", '\033' },
5414 glean_key_from_name (name)
5419 for (i = 0; name_key_alist[i].name; i++)
5420 if (stricmp (name, name_key_alist[i].name) == 0)
5421 return (name_key_alist[i].value);
5427 /* **************************************************************** */
5429 /* String Utility Functions */
5431 /* **************************************************************** */
5433 /* Return non-zero if any members of ARRAY are a substring in STRING. */
5435 substring_member_of_array (string, array)
5436 char *string, **array;
5438 static char *strindex ();
5442 if (strindex (string, *array))
5449 /* Whoops, Unix doesn't have strnicmp. */
5451 /* Compare at most COUNT characters from string1 to string2. Case
5454 strnicmp (string1, string2, count)
5455 char *string1, *string2;
5457 register char ch1, ch2;
5463 if (to_upper(ch1) == to_upper(ch2))
5470 /* strcmp (), but caseless. */
5472 stricmp (string1, string2)
5473 char *string1, *string2;
5475 register char ch1, ch2;
5477 while (*string1 && *string2)
5481 if (to_upper(ch1) != to_upper(ch2))
5484 return (*string1 | *string2);
5487 /* Determine if s2 occurs in s1. If so, return a pointer to the
5488 match in s1. The compare is case insensitive. */
5491 register char *s1, *s2;
5493 register int i, l = strlen (s2);
5494 register int len = strlen (s1);
5496 for (i = 0; (len - i) >= l; i++)
5497 if (strnicmp (&s1[i], s2, l) == 0)
5499 return ((char *)NULL);
5503 /* **************************************************************** */
5507 /* **************************************************************** */
5509 /* Since system V reads input differently than we do, I have to
5510 make a special version of getc for that. */
5515 #include <sys/errno.h>
5526 result = read (fileno (stream), &c, sizeof (char));
5527 if (result == sizeof (char))
5539 return (getc (stream));
5543 #ifdef STATIC_MALLOC
5545 /* **************************************************************** */
5547 /* xmalloc and xrealloc () */
5549 /* **************************************************************** */
5551 static void memory_error_and_abort ();
5557 char *temp = (char *)malloc (bytes);
5560 memory_error_and_abort ();
5565 xrealloc (pointer, bytes)
5569 char *temp = (char *)realloc (pointer, bytes);
5572 memory_error_and_abort ();
5577 memory_error_and_abort ()
5579 fprintf (stderr, "readline: Out of virtual memory!\n");
5582 #endif /* STATIC_MALLOC */
5585 /* **************************************************************** */
5587 /* Testing Readline */
5589 /* **************************************************************** */
5595 HIST_ENTRY **history_list ();
5596 char *temp = (char *)NULL;
5597 char *prompt = "readline% ";
5602 temp = readline (prompt);
5608 /* If there is anything on the line, print it and remember it. */
5611 fprintf (stderr, "%s\r\n", temp);
5615 /* Check for `command' that we handle. */
5616 if (strcmp (temp, "quit") == 0)
5619 if (strcmp (temp, "list") == 0) {
5620 HIST_ENTRY **list = history_list ();
5623 for (i = 0; list[i]; i++) {
5624 fprintf (stderr, "%d: %s\r\n", i, list[i]->line);
5625 free (list[i]->line);
5639 * compile-command: "gcc -g -traditional -I. -I.. -DTEST -o readline readline.c keymaps.o funmap.o history.o -ltermcap"