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)
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
79 #define d_namlen d_reclen
83 /* Some standard library routines. */
88 #define digit(c) ((c) >= '0' && (c) <= '9')
92 #define isletter(c) (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z'))
96 #define digit_value(c) ((c) - '0')
101 #define member(c, s) ((c) ? index ((s), (c)) : 0)
105 #define isident(c) ((isletter(c) || digit(c) || c == '_'))
109 #define exchange(x, y) {int temp = x; x = y; y = temp;}
112 static update_line ();
113 static void output_character_function ();
114 static delete_chars ();
115 static start_insert ();
116 static end_insert ();
118 #ifdef VOID_SIGHANDLER
119 #define sighandler void
121 #define sighandler int
124 /* This typedef is equivalant to the one for Function; it allows us
125 to say SigHandler *foo = signal (SIGKILL, SIG_IGN); */
126 typedef sighandler
SigHandler ();
129 static sighandler
rl_handle_sigwinch ();
130 static SigHandler
*old_sigwinch
= (SigHandler
*)NULL
;
133 /* If on, then readline handles signals in a way that doesn't screw. */
134 #define HANDLE_SIGNALS
137 #ifdef HANDLE_SIGNALS
138 #undef HANDLE_SIGNALS
142 /* Stupid comparison routine for qsort () ing strings. */
144 compare_strings (s1
, s2
)
147 return (strcmp (*s1
, *s2
));
151 /* **************************************************************** */
153 /* Line editing input utility */
155 /* **************************************************************** */
157 /* A pointer to the keymap that is currently in use.
158 By default, it is the standard emacs keymap. */
159 Keymap keymap
= emacs_standard_keymap
;
164 /* The current style of editing. */
165 int rl_editing_mode
= emacs_mode
;
167 /* Non-zero if the previous command was a kill command. */
168 static int last_command_was_kill
= 0;
170 /* The current value of the numeric argument specified by the user. */
171 int rl_numeric_arg
= 1;
173 /* Non-zero if an argument was typed. */
174 int rl_explicit_arg
= 0;
176 /* Temporary value used while generating the argument. */
177 static int arg_sign
= 1;
179 /* Non-zero means we have been called at least once before. */
180 static int rl_initialized
= 0;
182 /* If non-zero, this program is running in an EMACS buffer. */
183 static char *running_in_emacs
= (char *)NULL
;
185 /* The current offset in the current input line. */
188 /* Mark in the current input line. */
191 /* Length of the current input line. */
194 /* Make this non-zero to return the current input_line. */
197 /* The last function executed by readline. */
198 Function
*rl_last_func
= (Function
*)NULL
;
200 /* Top level environment for readline_internal (). */
201 static jmp_buf readline_top_level
;
203 /* The streams we interact with. */
204 static FILE *in_stream
, *out_stream
;
206 /* The names of the streams that we do input and output to. */
207 FILE *rl_instream
= stdin
, *rl_outstream
= stdout
;
209 /* Non-zero means echo characters as they are read. */
210 int readline_echoing_p
= 1;
212 /* Current prompt. */
215 /* The number of characters read in order to type this complete command. */
216 int rl_key_sequence_length
= 0;
218 /* If non-zero, then this is the address of a function to call just
219 before readline_internal () prints the first prompt. */
220 Function
*rl_startup_hook
= (Function
*)NULL
;
222 /* What we use internally. You should always refer to RL_LINE_BUFFER. */
223 static char *the_line
;
225 /* The character that can generate an EOF. Really read from
226 the terminal driver... just defaulted here. */
227 static int eof_char
= CTRL ('D');
229 /* Non-zero makes this the next keystroke to read. */
230 int rl_pending_input
= 0;
232 /* Pointer to a useful terminal name. */
233 char *rl_terminal_name
= (char *)NULL
;
235 /* Line buffer and maintenence. */
236 char *rl_line_buffer
= (char *)NULL
;
237 static int rl_line_buffer_len
= 0;
238 #define DEFAULT_BUFFER_SIZE 256
241 /* **************************************************************** */
243 /* Top Level Functions */
245 /* **************************************************************** */
247 /* Read a line of input. Prompt with PROMPT. A NULL PROMPT means
248 none. A return value of NULL means that EOF was encountered. */
253 static rl_prep_terminal (), rl_deprep_terminal ();
254 char *readline_internal ();
259 /* If we are at EOF return a NULL string. */
260 if (rl_pending_input
== EOF
)
262 rl_pending_input
= 0;
263 return ((char *)NULL
);
270 old_sigwinch
= (SigHandler
*)signal (SIGWINCH
, rl_handle_sigwinch
);
273 #ifdef HANDLE_SIGNALS
277 value
= readline_internal ();
278 rl_deprep_terminal ();
281 signal (SIGWINCH
, old_sigwinch
);
284 #ifdef HANDLE_SIGNALS
291 /* Read a line of input from the global rl_instream, doing output on
292 the global rl_outstream.
293 If rl_prompt is non-null, then that is our prompt. */
297 int lastc
, c
, eof_found
;
299 in_stream
= rl_instream
; out_stream
= rl_outstream
;
300 lastc
= eof_found
= 0;
303 (*rl_startup_hook
) ();
305 if (!readline_echoing_p
)
308 fprintf (out_stream
, "%s", rl_prompt
);
315 if (rl_editing_mode
== vi_mode
)
316 rl_vi_insertion_mode ();
322 int lk
= last_command_was_kill
;
323 int code
= setjmp (readline_top_level
);
328 if (!rl_pending_input
)
330 /* Then initialize the argument and number of keys read. */
332 rl_key_sequence_length
= 0;
337 /* EOF typed to a non-blank line is a <NL>. */
338 if (c
== EOF
&& rl_end
)
341 /* The character eof_char typed to blank line, and not as the
342 previous character is interpreted as EOF. */
343 if (((c
== eof_char
&& lastc
!= c
) || c
== EOF
) && !rl_end
)
350 rl_dispatch (c
, keymap
);
352 /* If there was no change in last_command_was_kill, then no kill
353 has taken place. Note that if input is pending we are reading
354 a prefix command, so nothing has changed yet. */
355 if (!rl_pending_input
)
357 if (lk
== last_command_was_kill
)
358 last_command_was_kill
= 0;
362 /* In vi mode, when you exit insert mode, the cursor moves back
363 over the previous character. We explicitly check for that here. */
364 if (rl_editing_mode
== vi_mode
&& keymap
== vi_movement_keymap
)
372 /* Restore the original of this history line, iff the line that we
373 are editing was originally in the history, AND the line has changed. */
375 HIST_ENTRY
*entry
= current_history ();
377 if (entry
&& rl_undo_list
)
379 char *temp
= savestring (the_line
);
381 entry
= replace_history_entry (where_history (), the_line
,
383 free_history_entry (entry
);
385 strcpy (the_line
, temp
);
390 /* At any rate, it is highly likely that this line has an undo list. Get
398 return (savestring (the_line
));
402 /* Variables for keyboard macros. */
404 /* The currently executing macro string. If this is non-zero,
405 then it is a malloc ()'ed string where input is coming from. */
406 static char *executing_macro
= (char *)NULL
;
408 /* The offset in the above string to the next character to be read. */
409 static int executing_macro_index
= 0;
411 /* Non-zero means to save keys that we dispatch on in a kbd macro. */
412 static int defining_kbd_macro
= 0;
414 /* The current macro string being built. Characters get stuffed
415 in here by add_macro_char (). */
416 static char *current_macro
= (char *)NULL
;
418 /* The size of the buffer allocated to current_macro. */
419 static int current_macro_size
= 0;
421 /* The index at which characters are being added to current_macro. */
422 static int current_macro_index
= 0;
424 /* A structure used to save nested macro strings.
425 It is a linked list of string/index for each saved macro. */
427 struct saved_macro
*next
;
432 /* The list of saved macros. */
433 struct saved_macro
*macro_list
= (struct saved_macro
*)NULL
;
436 /* **************************************************************** */
438 /* Signal Handling */
440 /* **************************************************************** */
444 rl_handle_sigwinch (sig
, code
, scp
)
446 struct sigcontext
*scp
;
448 char *term
= rl_terminal_name
, *getenv ();
450 if (readline_echoing_p
)
453 term
= getenv ("TERM");
456 rl_reset_terminal (term
);
459 rl_forced_update_display ();
464 old_sigwinch
!= (SigHandler
*)SIG_IGN
&&
465 old_sigwinch
!= (SigHandler
*)SIG_DFL
)
466 (*old_sigwinch
)(sig
, code
, scp
);
468 #endif /* SIGWINCH */
470 #ifdef HANDLE_SIGNALS
471 /* Interrupt handling. */
472 static SigHandler
*old_int
= (SigHandler
*)NULL
,
473 *old_tstp
= (SigHandler
*)NULL
,
474 *old_ttou
= (SigHandler
*)NULL
,
475 *old_ttin
= (SigHandler
*)NULL
,
476 *old_cont
= (SigHandler
*)NULL
;
478 /* Handle an interrupt character. */
480 rl_signal_handler (sig
, code
, scp
)
482 struct sigcontext
*scp
;
484 static rl_prep_terminal (), rl_deprep_terminal ();
493 signal (SIGWINCH
, old_sigwinch
);
502 rl_clean_up_for_exit ();
503 rl_deprep_terminal ();
505 rl_pending_input
= 0;
507 kill (getpid (), sig
);
517 old_int
= (SigHandler
*)signal (SIGINT
, rl_signal_handler
);
519 if (old_int
== (SigHandler
*)SIG_IGN
)
520 signal (SIGINT
, SIG_IGN
);
523 old_tstp
= (SigHandler
*)signal (SIGTSTP
, rl_signal_handler
);
524 if (old_tstp
== (SigHandler
*)SIG_IGN
)
525 signal (SIGTSTP
, SIG_IGN
);
528 old_ttou
= (SigHandler
*)signal (SIGTTOU
, rl_signal_handler
);
529 old_ttin
= (SigHandler
*)signal (SIGTTIN
, rl_signal_handler
);
535 signal (SIGINT
, old_int
);
538 signal (SIGTSTP
, old_tstp
);
541 signal (SIGTTOU
, old_ttou
);
542 signal (SIGTTIN
, old_ttin
);
545 #endif /* HANDLE_SIGNALS */
549 /* **************************************************************** */
551 /* Character Input Buffering */
553 /* **************************************************************** */
555 /* If the terminal was in xoff state when we got to it, then xon_char
556 contains the character that is supposed to start it again. */
557 static int xon_char
, xoff_state
;
558 static int pop_index
= 0, push_index
= 0, ibuffer_len
= 511;
559 static unsigned char ibuffer
[512];
561 /* Non-null means it is a pointer to a function to run while waiting for
563 Function
*rl_event_hook
= (Function
*)NULL
;
565 #define any_typein (push_index != pop_index)
567 /* Add KEY to the buffer of characters to be read. */
574 rl_pending_input
= EOF
;
576 ibuffer
[push_index
++] = key
;
577 if (push_index
>= ibuffer_len
)
581 /* Return the amount of space available in the
582 buffer for stuffing characters. */
586 if (pop_index
> push_index
)
587 return (pop_index
- push_index
);
589 return (ibuffer_len
- (push_index
- pop_index
));
592 /* Get a key from the buffer of characters to be read.
593 Return the key in KEY.
594 Result is KEY if there was a key, or 0 if there wasn't. */
599 if (push_index
== pop_index
)
602 *key
= ibuffer
[pop_index
++];
604 if (pop_index
>= ibuffer_len
)
610 /* Stuff KEY into the *front* of the input buffer.
611 Returns non-zero if successful, zero if there is
612 no space left in the buffer. */
617 if (ibuffer_space ())
621 pop_index
= ibuffer_len
- 1;
622 ibuffer
[pop_index
] = key
;
628 /* If a character is available to be read, then read it
629 and stuff it into IBUFFER. Otherwise, just return. */
632 int tty
= fileno (in_stream
);
633 register int tem
, result
= -1;
638 result
= ioctl (tty
, FIONREAD
, &chars_avail
);
643 fcntl (tty
, F_SETFL
, O_NDELAY
);
644 chars_avail
= read (tty
, &input
, 1);
645 fcntl (tty
, F_SETFL
, 0);
646 if (chars_avail
== -1 && errno
== EAGAIN
)
650 tem
= ibuffer_space ();
652 if (chars_avail
> tem
)
655 /* One cannot read all of the available input. I can only read a single
656 character at a time, or else programs which require input can be
657 thwarted. If the buffer is larger than one character, I lose.
659 if (tem
< ibuffer_len
)
664 while (chars_avail
--)
665 rl_stuff_char (rl_getc (in_stream
));
670 rl_stuff_char (input
);
674 /* Read a key, including pending input. */
680 rl_key_sequence_length
++;
682 if (rl_pending_input
)
684 c
= rl_pending_input
;
685 rl_pending_input
= 0;
689 static int next_macro_key ();
691 /* If input is coming from a macro, then use that. */
692 if (c
= next_macro_key ())
695 /* If the user has an event function, then call it periodically. */
698 while (rl_event_hook
&& !rl_get_char (&c
))
706 if (!rl_get_char (&c
))
707 c
= rl_getc (in_stream
);
711 /* Ugh. But I can't think of a better way. */
712 if (xoff_state
&& c
== xon_char
)
714 ioctl (fileno (in_stream
), TIOCSTART
, 0);
716 return rl_read_key ();
718 #endif /* TIOCSTART */
722 /* Do the command associated with KEY in MAP.
723 If the associated command is really a keymap, then read
724 another key, and dispatch into that map. */
725 rl_dispatch (key
, map
)
729 if (defining_kbd_macro
)
731 static add_macro_char ();
733 add_macro_char (key
);
736 if (key
> 127 && key
< 256)
738 if (map
[ESC
].type
== ISKMAP
)
740 map
= (Keymap
)map
[ESC
].function
;
742 rl_dispatch (key
, map
);
749 switch (map
[key
].type
)
753 Function
*func
= map
[key
].function
;
755 if (func
!= (Function
*)NULL
)
757 /* Special case rl_do_lowercase_version (). */
758 if (func
== rl_do_lowercase_version
)
760 rl_dispatch (to_lower (key
), map
);
764 (*map
[key
].function
)(rl_numeric_arg
* arg_sign
, key
);
775 if (map
[key
].function
!= (Function
*)NULL
)
779 rl_key_sequence_length
++;
780 newkey
= rl_read_key ();
781 rl_dispatch (newkey
, (Keymap
)map
[key
].function
);
791 if (map
[key
].function
!= (Function
*)NULL
)
793 static with_macro_input ();
794 char *macro
= savestring ((char *)map
[key
].function
);
796 with_macro_input (macro
);
802 /* If we have input pending, then the last command was a prefix
803 command. Don't change the state of rl_last_func. */
804 if (!rl_pending_input
)
805 rl_last_func
= map
[key
].function
;
809 /* **************************************************************** */
811 /* Hacking Keyboard Macros */
813 /* **************************************************************** */
815 /* Set up to read subsequent input from STRING.
816 STRING is free ()'ed when we are done with it. */
818 with_macro_input (string
)
821 static push_executing_macro ();
823 push_executing_macro ();
824 executing_macro
= string
;
825 executing_macro_index
= 0;
828 /* Return the next character available from a macro, or 0 if
829 there are no macro characters. */
833 if (!executing_macro
)
836 if (!executing_macro
[executing_macro_index
])
838 static pop_executing_macro ();
840 pop_executing_macro ();
841 return (next_macro_key ());
844 return (executing_macro
[executing_macro_index
++]);
847 /* Save the currently executing macro on a stack of saved macros. */
849 push_executing_macro ()
851 struct saved_macro
*saver
;
853 saver
= (struct saved_macro
*)xmalloc (sizeof (struct saved_macro
));
854 saver
->next
= macro_list
;
855 saver
->index
= executing_macro_index
;
856 saver
->string
= executing_macro
;
861 /* Discard the current macro, replacing it with the one
862 on the top of the stack of saved macros. */
864 pop_executing_macro ()
867 free (executing_macro
);
869 executing_macro
= (char *)NULL
;
870 executing_macro_index
= 0;
874 struct saved_macro
*disposer
= macro_list
;
875 executing_macro
= macro_list
->string
;
876 executing_macro_index
= macro_list
->index
;
877 macro_list
= macro_list
->next
;
882 /* Add a character to the macro being built. */
887 if (current_macro_index
+ 1 >= current_macro_size
)
890 current_macro
= (char *)xmalloc (current_macro_size
= 25);
893 (char *)xrealloc (current_macro
, current_macro_size
+= 25);
896 current_macro
[current_macro_index
++] = c
;
897 current_macro
[current_macro_index
] = '\0';
900 /* Begin defining a keyboard macro.
901 Keystrokes are recorded as they are executed.
902 End the definition with rl_end_kbd_macro ().
903 If a numeric argument was explicitly typed, then append this
904 definition to the end of the existing macro, and start by
905 re-executing the existing macro. */
906 rl_start_kbd_macro (ignore1
, ignore2
)
907 int ignore1
, ignore2
;
909 if (defining_kbd_macro
)
915 with_macro_input (savestring (current_macro
));
918 current_macro_index
= 0;
920 defining_kbd_macro
= 1;
923 /* Stop defining a keyboard macro.
924 A numeric argument says to execute the macro right now,
925 that many times, counting the definition as the first time. */
926 rl_end_kbd_macro (count
, ignore
)
929 if (!defining_kbd_macro
)
932 current_macro_index
-= (rl_key_sequence_length
- 1);
933 current_macro
[current_macro_index
] = '\0';
935 defining_kbd_macro
= 0;
937 rl_call_last_kbd_macro (--count
, 0);
940 /* Execute the most recently defined keyboard macro.
941 COUNT says how many times to execute it. */
942 rl_call_last_kbd_macro (count
, ignore
)
949 with_macro_input (savestring (current_macro
));
953 /* Non-zero means do not parse any lines other than comments and
954 parser directives. */
955 static unsigned char parsing_conditionalized_out
= 0;
957 /* **************************************************************** */
959 /* Initializations */
961 /* **************************************************************** */
963 /* Initliaze readline (and terminal if not already). */
966 extern char *rl_display_prompt
;
968 /* If we have never been called before, initialize the
969 terminal and data structures. */
972 readline_initialize_everything ();
976 /* Initalize the current line information. */
977 rl_point
= rl_end
= 0;
978 the_line
= rl_line_buffer
;
981 /* We aren't done yet. We haven't even gotten started yet! */
984 /* Tell the history routines what is going on. */
985 start_using_history ();
987 /* Make the display buffer match the state of the line. */
989 extern char *rl_display_prompt
;
990 extern int forced_display
;
994 rl_display_prompt
= rl_prompt
? rl_prompt
: "";
998 /* No such function typed yet. */
999 rl_last_func
= (Function
*)NULL
;
1001 /* Parsing of key-bindings begins in an enabled state. */
1003 parsing_conditionalized_out
= 0;
1007 /* Initialize the entire state of the world. */
1008 readline_initialize_everything ()
1010 /* Find out if we are running in Emacs. */
1011 running_in_emacs
= (char *)getenv ("EMACS");
1013 /* Allocate data structures. */
1014 if (!rl_line_buffer
)
1016 (char *)xmalloc (rl_line_buffer_len
= DEFAULT_BUFFER_SIZE
);
1018 /* Initialize the terminal interface. */
1019 init_terminal_io ((char *)NULL
);
1021 /* Bind tty characters to readline functions. */
1022 readline_default_bindings ();
1024 /* Initialize the function names. */
1025 rl_initialize_funmap ();
1027 /* Read in the init file. */
1028 rl_read_init_file ((char *)NULL
);
1030 /* If the completion parser's default word break characters haven't
1031 been set yet, then do so now. */
1033 extern char *rl_completer_word_break_characters
;
1034 extern char *rl_basic_word_break_characters
;
1036 if (rl_completer_word_break_characters
== (char *)NULL
)
1037 rl_completer_word_break_characters
= rl_basic_word_break_characters
;
1041 /* If this system allows us to look at the values of the regular
1042 input editing characters, then bind them to their readline
1044 readline_default_bindings ()
1047 struct sgttyb ttybuff
;
1048 int tty
= fileno (rl_instream
);
1050 if (ioctl (tty
, TIOCGETP
, &ttybuff
) != -1)
1052 int erase
= ttybuff
.sg_erase
, kill
= ttybuff
.sg_kill
;
1054 if (erase
!= -1 && keymap
[erase
].type
== ISFUNC
)
1055 keymap
[erase
].function
= rl_rubout
;
1057 if (kill
!= -1 && keymap
[kill
].type
== ISFUNC
)
1058 keymap
[kill
].function
= rl_unix_line_discard
;
1065 if (ioctl (tty
, TIOCGLTC
, <
) != -1)
1067 int erase
= lt
.t_werasc
, nextc
= lt
.t_lnextc
;
1069 if (erase
!= -1 && keymap
[erase
].type
== ISFUNC
)
1070 keymap
[erase
].function
= rl_unix_word_rubout
;
1072 if (nextc
!= -1 && keymap
[nextc
].type
== ISFUNC
)
1073 keymap
[nextc
].function
= rl_quoted_insert
;
1076 #endif /* TIOCGLTC */
1077 #endif /* TIOCGETP */
1081 /* **************************************************************** */
1083 /* Numeric Arguments */
1085 /* **************************************************************** */
1087 /* Handle C-u style numeric args, as well as M--, and M-digits. */
1089 /* Add the current digit to the argument in progress. */
1090 rl_digit_argument (ignore
, key
)
1093 rl_pending_input
= key
;
1097 /* What to do when you abort reading an argument. */
1098 rl_discard_argument ()
1101 rl_clear_message ();
1102 rl_init_argument ();
1105 /* Create a default argument. */
1108 rl_numeric_arg
= arg_sign
= 1;
1109 rl_explicit_arg
= 0;
1112 /* C-u, universal argument. Multiply the current argument by 4.
1113 Read a key. If the key has nothing to do with arguments, then
1114 dispatch on it. If the key is the abort character then abort. */
1115 rl_universal_argument ()
1117 rl_numeric_arg
*= 4;
1126 rl_message ("(arg: %d) ", arg_sign
* rl_numeric_arg
);
1127 key
= c
= rl_read_key ();
1129 if (keymap
[c
].type
== ISFUNC
&&
1130 keymap
[c
].function
== rl_universal_argument
)
1132 rl_numeric_arg
*= 4;
1138 if (rl_explicit_arg
)
1139 rl_numeric_arg
= (rl_numeric_arg
* 10) + (c
- '0');
1141 rl_numeric_arg
= (c
- '0');
1142 rl_explicit_arg
= 1;
1146 if (c
== '-' && !rl_explicit_arg
)
1153 rl_clear_message ();
1154 rl_dispatch (key
, keymap
);
1162 /* **************************************************************** */
1166 /* **************************************************************** */
1168 /* This is the stuff that is hard for me. I never seem to write good
1169 display routines in C. Let's see how I do this time. */
1171 /* (PWP) Well... Good for a simple line updater, but totally ignores
1172 the problems of input lines longer than the screen width.
1174 update_line and the code that calls it makes a multiple line,
1175 automatically wrapping line update. Carefull attention needs
1176 to be paid to the vertical position variables.
1178 handling of terminals with autowrap on (incl. DEC braindamage)
1179 could be improved a bit. Right now I just cheat and decrement
1180 screenwidth by one. */
1182 /* Keep two buffers; one which reflects the current contents of the
1183 screen, and the other to draw what we think the new contents should
1184 be. Then compare the buffers, and make whatever changes to the
1185 screen itself that we should. Finally, make the buffer that we
1186 just drew into be the one which reflects the current contents of the
1187 screen, and place the cursor where it belongs.
1189 Commands that want to can fix the display themselves, and then let
1190 this function know that the display has been fixed by setting the
1191 RL_DISPLAY_FIXED variable. This is good for efficiency. */
1193 /* Termcap variables: */
1194 extern char *term_up
, *term_dc
, *term_cr
;
1195 extern int screenheight
, screenwidth
, terminal_can_insert
;
1197 /* What YOU turn on when you have handled all redisplay yourself. */
1198 int rl_display_fixed
= 0;
1200 /* The visible cursor position. If you print some text, adjust this. */
1204 /* The last left edge of text that was displayed. This is used when
1205 doing horizontal scrolling. It shifts in thirds of a screenwidth. */
1206 static int last_lmargin
= 0;
1208 /* The line display buffers. One is the line currently displayed on
1209 the screen. The other is the line about to be displayed. */
1210 static char *visible_line
= (char *)NULL
;
1211 static char *invisible_line
= (char *)NULL
;
1213 /* Number of lines currently on screen minus 1. */
1216 /* A buffer for `modeline' messages. */
1219 /* Non-zero forces the redisplay even if we thought it was unnecessary. */
1220 int forced_display
= 0;
1222 /* The stuff that gets printed out before the actual text of the line.
1223 This is usually pointing to rl_prompt. */
1224 char *rl_display_prompt
= (char *)NULL
;
1226 /* Default and initial buffer size. Can grow. */
1227 static int line_size
= 1024;
1229 /* Non-zero means to always use horizontal scrolling in line display. */
1230 int horizontal_scroll_mode
= 0;
1232 /* I really disagree with this, but my boss (among others) insists that we
1233 support compilers that don't work. I don't think we are gaining by doing
1234 so; what is the advantage in producing better code if we can't use it? */
1235 /* The following two declarations belong inside the
1236 function block, not here. */
1237 static void move_cursor_relative ();
1238 static void output_some_chars ();
1240 /* Basic redisplay algorithm. */
1243 register int in
, out
, c
, linenum
;
1244 register char *line
= invisible_line
;
1246 int inv_botlin
= 0; /* Number of lines in newly drawn buffer. */
1248 extern int readline_echoing_p
;
1250 if (!readline_echoing_p
)
1253 if (!rl_display_prompt
)
1254 rl_display_prompt
= "";
1256 if (!invisible_line
)
1258 visible_line
= (char *)xmalloc (line_size
);
1259 invisible_line
= (char *)xmalloc (line_size
);
1260 line
= invisible_line
;
1261 for (in
= 0; in
< line_size
; in
++)
1263 visible_line
[in
] = 0;
1264 invisible_line
[in
] = 1;
1269 /* Draw the line into the buffer. */
1272 /* Mark the line as modified or not. We only do this for history
1275 if (current_history () && rl_undo_list
)
1281 /* If someone thought that the redisplay was handled, but the currently
1282 visible line has a different modification state than the one about
1283 to become visible, then correct the callers misconception. */
1284 if (visible_line
[0] != invisible_line
[0])
1285 rl_display_fixed
= 0;
1287 strncpy (line
+ out
, rl_display_prompt
, strlen (rl_display_prompt
));
1288 out
+= strlen (rl_display_prompt
);
1291 for (in
= 0; in
< rl_end
; in
++)
1295 if (out
+ 1 >= line_size
)
1298 visible_line
= (char *)xrealloc (visible_line
, line_size
);
1299 invisible_line
= (char *)xrealloc (invisible_line
, line_size
);
1300 line
= invisible_line
;
1310 line
[out
++] = c
- 128;
1312 #define DISPLAY_TABS
1316 register int newout
= (out
| (int)7) + 1;
1317 while (out
< newout
)
1325 line
[out
++] = c
+ 64;
1334 /* PWP: now is when things get a bit hairy. The visible and invisible
1335 line buffers are really multiple lines, which would wrap every
1336 (screenwidth - 1) characters. Go through each in turn, finding
1337 the changed region and updating it. The line order is top to bottom. */
1339 /* If we can move the cursor up and down, then use multiple lines,
1340 otherwise, let long lines display in a single terminal line, and
1341 horizontally scroll it. */
1343 if (!horizontal_scroll_mode
&& term_up
&& *term_up
)
1345 int total_screen_chars
= (screenwidth
* screenheight
);
1347 if (!rl_display_fixed
|| forced_display
)
1351 /* If we have more than a screenful of material to display, then
1352 only display a screenful. We should display the last screen,
1353 not the first. I'll fix this in a minute. */
1354 if (out
>= total_screen_chars
)
1355 out
= total_screen_chars
- 1;
1357 /* Number of screen lines to display. */
1358 inv_botlin
= out
/ screenwidth
;
1360 /* For each line in the buffer, do the updating display. */
1361 for (linenum
= 0; linenum
<= inv_botlin
; linenum
++)
1362 update_line (linenum
> vis_botlin
? ""
1363 : &visible_line
[linenum
* screenwidth
],
1364 &invisible_line
[linenum
* screenwidth
],
1367 /* We may have deleted some lines. If so, clear the left over
1368 blank ones at the bottom out. */
1369 if (vis_botlin
> inv_botlin
)
1372 for (; linenum
<= vis_botlin
; linenum
++)
1374 tt
= &visible_line
[linenum
* screenwidth
];
1375 move_vert (linenum
);
1376 move_cursor_relative (0, tt
);
1377 clear_to_eol ((linenum
== vis_botlin
)?
1378 strlen (tt
) : screenwidth
);
1381 vis_botlin
= inv_botlin
;
1383 /* Move the cursor where it should be. */
1384 move_vert (c_pos
/ screenwidth
);
1385 move_cursor_relative (c_pos
% screenwidth
,
1386 &invisible_line
[(c_pos
/ screenwidth
) * screenwidth
]);
1389 else /* Do horizontal scrolling. */
1393 /* Always at top line. */
1396 /* If the display position of the cursor would be off the edge
1397 of the screen, start the display of this line at an offset that
1398 leaves the cursor on the screen. */
1399 if (c_pos
- last_lmargin
> screenwidth
- 2)
1400 lmargin
= (c_pos
/ (screenwidth
/ 3) - 2) * (screenwidth
/ 3);
1401 else if (c_pos
- last_lmargin
< 1)
1402 lmargin
= ((c_pos
- 1) / (screenwidth
/ 3)) * (screenwidth
/ 3);
1404 lmargin
= last_lmargin
;
1406 /* If the first character on the screen isn't the first character
1407 in the display line, indicate this with a special character. */
1409 line
[lmargin
] = '<';
1411 if (lmargin
+ screenwidth
< out
)
1412 line
[lmargin
+ screenwidth
- 1] = '>';
1414 if (!rl_display_fixed
|| forced_display
|| lmargin
!= last_lmargin
)
1417 update_line (&visible_line
[last_lmargin
],
1418 &invisible_line
[lmargin
], 0);
1420 move_cursor_relative (c_pos
- lmargin
, &invisible_line
[lmargin
]);
1421 last_lmargin
= lmargin
;
1424 fflush (out_stream
);
1426 /* Swap visible and non-visible lines. */
1428 char *temp
= visible_line
;
1429 visible_line
= invisible_line
;
1430 invisible_line
= temp
;
1431 rl_display_fixed
= 0;
1435 /* PWP: update_line() is based on finding the middle difference of each
1436 line on the screen; vis:
1438 /old first difference
1439 /beginning of line | /old last same /old EOL
1441 old: eddie> Oh, my little gruntle-buggy is to me, as lurgid as
1442 new: eddie> Oh, my little buggy says to me, as lurgid as
1444 \beginning of line | \new last same \new end of line
1445 \new first difference
1447 All are character pointers for the sake of speed. Special cases for
1448 no differences, as well as for end of line additions must be handeled.
1450 Could be made even smarter, but this works well enough */
1452 update_line (old
, new, current_line
)
1453 register char *old
, *new;
1456 register char *ofd
, *ols
, *oe
, *nfd
, *nls
, *ne
;
1457 int lendiff
, wsatend
;
1459 /* Find first difference. */
1460 for (ofd
= old
, nfd
= new;
1461 (ofd
- old
< screenwidth
) && *ofd
&& (*ofd
== *nfd
);
1465 /* Move to the end of the screen line. */
1466 for (oe
= ofd
; ((oe
- old
) < screenwidth
) && *oe
; oe
++);
1467 for (ne
= nfd
; ((ne
- new) < screenwidth
) && *ne
; ne
++);
1469 /* If no difference, continue to next line. */
1470 if (ofd
== oe
&& nfd
== ne
)
1473 wsatend
= 1; /* flag for trailing whitespace */
1474 ols
= oe
- 1; /* find last same */
1476 while ((*ols
== *nls
) && (ols
> ofd
) && (nls
> nfd
))
1489 else if (*ols
!= *nls
)
1491 if (*ols
) /* don't step past the NUL */
1497 move_vert (current_line
);
1498 move_cursor_relative (ofd
- old
, old
);
1500 /* if (len (new) > len (old)) */
1501 lendiff
= (nls
- nfd
) - (ols
- ofd
);
1503 /* Insert (diff(len(old),len(new)) ch */
1506 if (terminal_can_insert
)
1508 extern char *term_IC
;
1510 /* Sometimes it is cheaper to print the characters rather than
1511 use the terminal's capabilities. */
1512 if ((2 * (ne
- nfd
)) < lendiff
&& (!term_IC
|| !*term_IC
))
1514 output_some_chars (nfd
, (ne
- nfd
));
1515 last_c_pos
+= (ne
- nfd
);
1521 start_insert (lendiff
);
1522 output_some_chars (nfd
, lendiff
);
1523 last_c_pos
+= lendiff
;
1528 /* At the end of a line the characters do not have to
1529 be "inserted". They can just be placed on the screen. */
1530 output_some_chars (nfd
, lendiff
);
1531 last_c_pos
+= lendiff
;
1533 /* Copy (new) chars to screen from first diff to last match. */
1534 if (((nls
- nfd
) - lendiff
) > 0)
1536 output_some_chars (&nfd
[lendiff
], ((nls
- nfd
) - lendiff
));
1537 last_c_pos
+= ((nls
- nfd
) - lendiff
);
1542 { /* cannot insert chars, write to EOL */
1543 output_some_chars (nfd
, (ne
- nfd
));
1544 last_c_pos
+= (ne
- nfd
);
1547 else /* Delete characters from line. */
1549 /* If possible and inexpensive to use terminal deletion, then do so. */
1550 if (term_dc
&& (2 * (ne
- nfd
)) >= (-lendiff
))
1553 delete_chars (-lendiff
); /* delete (diff) characters */
1555 /* Copy (new) chars to screen from first diff to last match */
1556 if ((nls
- nfd
) > 0)
1558 output_some_chars (nfd
, (nls
- nfd
));
1559 last_c_pos
+= (nls
- nfd
);
1562 /* Otherwise, print over the existing material. */
1565 output_some_chars (nfd
, (ne
- nfd
));
1566 last_c_pos
+= (ne
- nfd
);
1567 clear_to_eol ((oe
- old
) - (ne
- new));
1572 /* (PWP) tell the update routines that we have moved onto a
1573 new (empty) line. */
1577 visible_line
[0] = '\0';
1579 last_c_pos
= last_v_pos
= 0;
1580 vis_botlin
= last_lmargin
= 0;
1583 /* Actually update the display, period. */
1584 rl_forced_update_display ()
1588 register char *temp
= visible_line
;
1590 while (*temp
) *temp
++ = '\0';
1597 /* Move the cursor from last_c_pos to NEW, which are buffer indices.
1598 DATA is the contents of the screen line of interest; i.e., where
1599 the movement is being done. */
1601 move_cursor_relative (new, data
)
1606 static void output_character_function ();
1608 /* It may be faster to output a CR, and then move forwards instead
1609 of moving backwards. */
1610 if (new + 1 < last_c_pos
- new)
1612 tputs (term_cr
, 1, output_character_function
);
1616 if (last_c_pos
== new) return;
1618 if (last_c_pos
< new)
1620 /* Move the cursor forward. We do it by printing the command
1621 to move the cursor forward if there is one, else print that
1622 portion of the output buffer again. Which is cheaper? */
1624 /* The above comment is left here for posterity. It is faster
1625 to print one character (non-control) than to print a control
1626 sequence telling the terminal to move forward one character.
1627 That kind of control is for people who don't know what the
1628 data is underneath the cursor. */
1629 #ifdef HACK_TERMCAP_MOTION
1630 extern char *term_forward_char
;
1632 if (term_forward_char
)
1633 for (i
= last_c_pos
; i
< new; i
++)
1634 tputs (term_forward_char
, 1, output_character_function
);
1636 for (i
= last_c_pos
; i
< new; i
++)
1637 putc (data
[i
], out_stream
);
1639 for (i
= last_c_pos
; i
< new; i
++)
1640 putc (data
[i
], out_stream
);
1641 #endif /* HACK_TERMCAP_MOTION */
1644 backspace (last_c_pos
- new);
1648 /* PWP: move the cursor up or down. */
1652 void output_character_function ();
1653 register int delta
, i
;
1655 if (last_v_pos
== to
) return;
1657 if (to
> screenheight
)
1660 if ((delta
= to
- last_v_pos
) > 0)
1662 for (i
= 0; i
< delta
; i
++)
1663 putc ('\n', out_stream
);
1664 tputs (term_cr
, 1, output_character_function
);
1665 last_c_pos
= 0; /* because crlf() will do \r\n */
1669 if (term_up
&& *term_up
)
1670 for (i
= 0; i
< -delta
; i
++)
1671 tputs (term_up
, 1, output_character_function
);
1673 last_v_pos
= to
; /* now to is here */
1676 /* Physically print C on out_stream. This is for functions which know
1677 how to optimize the display. */
1683 fprintf (out_stream
, "M-");
1688 if (c
< 32 && c
!= '\t')
1697 putc (c
, out_stream
);
1698 fflush (out_stream
);
1703 rl_character_len (c
, pos
)
1704 register int c
, pos
;
1706 if (c
< ' ' || c
> 126)
1709 return (((pos
| (int)7) + 1) - pos
);
1718 rl_character_len (c
)
1721 if (c
< ' ' || c
> 126)
1726 #endif /* DISPLAY_TAB */
1728 /* How to print things in the "echo-area". The prompt is treated as a
1730 rl_message (string
, arg1
, arg2
)
1733 sprintf (msg_buf
, string
, arg1
, arg2
);
1734 rl_display_prompt
= msg_buf
;
1738 /* How to clear things from the "echo-area". */
1741 rl_display_prompt
= rl_prompt
;
1745 /* **************************************************************** */
1747 /* Terminal and Termcap */
1749 /* **************************************************************** */
1751 static char *term_buffer
= (char *)NULL
;
1752 static char *term_string_buffer
= (char *)NULL
;
1754 /* Non-zero means this terminal can't really do anything. */
1760 /* Some strings to control terminal actions. These are output by tputs (). */
1761 char *term_goto
, *term_clreol
, *term_cr
, *term_clrpag
, *term_backspace
;
1763 int screenwidth
, screenheight
;
1765 /* Non-zero if we determine that the terminal can do character insertion. */
1766 int terminal_can_insert
= 0;
1768 /* How to insert characters. */
1769 char *term_im
, *term_ei
, *term_ic
, *term_ip
, *term_IC
;
1771 /* How to delete characters. */
1772 char *term_dc
, *term_DC
;
1774 #ifdef HACK_TERMCAP_MOTION
1775 char *term_forward_char
;
1776 #endif /* HACK_TERMCAP_MOTION */
1778 /* How to go up a line. */
1781 /* Re-initialize the terminal considering that the TERM/TERMCAP variable
1783 rl_reset_terminal (terminal_name
)
1784 char *terminal_name
;
1786 init_terminal_io (terminal_name
);
1789 init_terminal_io (terminal_name
)
1790 char *terminal_name
;
1792 char *term
= (terminal_name
? terminal_name
: (char *)getenv ("TERM"));
1793 char *tgetstr (), *buffer
;
1796 if (!term_string_buffer
)
1797 term_string_buffer
= (char *)xmalloc (2048);
1800 term_buffer
= (char *)xmalloc (2048);
1802 buffer
= term_string_buffer
;
1804 term_clrpag
= term_cr
= term_clreol
= (char *)NULL
;
1809 if (tgetent (term_buffer
, term
) < 0)
1815 BC
= tgetstr ("pc", &buffer
);
1816 PC
= buffer
? *buffer
: 0;
1818 term_backspace
= tgetstr ("le", &buffer
);
1820 term_cr
= tgetstr ("cr", &buffer
);
1821 term_clreol
= tgetstr ("ce", &buffer
);
1822 term_clrpag
= tgetstr ("cl", &buffer
);
1827 #ifdef HACK_TERMCAP_MOTION
1828 term_forward_char
= tgetstr ("nd", &buffer
);
1829 #endif /* HACK_TERMCAP_MOTION */
1831 screenwidth
= tgetnum ("co");
1832 if (screenwidth
<= 0)
1834 screenwidth
--; /* PWP: avoid autowrap bugs */
1836 screenheight
= tgetnum ("li");
1837 if (screenheight
<= 0)
1840 term_im
= tgetstr ("im", &buffer
);
1841 term_ei
= tgetstr ("ei", &buffer
);
1842 term_IC
= tgetstr ("IC", &buffer
);
1843 term_ic
= tgetstr ("ic", &buffer
);
1844 term_ip
= tgetstr ("ip", &buffer
);
1845 term_IC
= tgetstr ("IC", &buffer
);
1847 /* "An application program can assume that the terminal can do
1848 character insertion if *any one of* the capabilities `IC',
1849 `im', `ic' or `ip' is provided." */
1850 terminal_can_insert
= (term_IC
|| term_im
|| term_ic
|| term_ip
);
1852 term_up
= tgetstr ("up", &buffer
);
1853 term_dc
= tgetstr ("dc", &buffer
);
1854 term_DC
= tgetstr ("DC", &buffer
);
1857 /* A function for the use of tputs () */
1859 output_character_function (c
)
1862 putc (c
, out_stream
);
1865 /* Write COUNT characters from STRING to the output stream. */
1867 output_some_chars (string
, count
)
1871 fwrite (string
, 1, count
, out_stream
);
1875 /* Delete COUNT characters from the display line. */
1877 delete_chars (count
)
1880 if (count
> screenwidth
)
1883 if (term_DC
&& *term_DC
)
1885 char *tgoto (), *buffer
;
1886 buffer
= tgoto (term_DC
, 0, count
);
1887 tputs (buffer
, 1, output_character_function
);
1891 if (term_dc
&& *term_dc
)
1893 tputs (term_dc
, 1, output_character_function
);
1897 /* Prepare to insert by inserting COUNT blank spaces. */
1899 start_insert (count
)
1902 if (term_im
&& *term_im
)
1903 tputs (term_im
, 1, output_character_function
);
1905 if (term_IC
&& *term_IC
&&
1906 (count
> 1 || !term_ic
|| !*term_ic
))
1908 char *tgoto (), *buffer
;
1909 buffer
= tgoto (term_IC
, 0, count
);
1910 tputs (buffer
, 1, output_character_function
);
1914 if (term_ic
&& *term_ic
)
1916 tputs (term_ic
, 1, output_character_function
);
1920 /* We are finished doing our insertion. Send ending string. */
1924 if (term_ei
&& *term_ei
)
1925 tputs (term_ei
, 1, output_character_function
);
1928 /* Move the cursor back. */
1935 for (i
= 0; i
< count
; i
++)
1936 tputs (term_backspace
, 1, output_character_function
);
1938 for (i
= 0; i
< count
; i
++)
1939 putc ('\b', out_stream
);
1942 /* Move to the start of the next line. */
1945 tputs (term_cr
, 1, output_character_function
);
1946 putc ('\n', out_stream
);
1949 /* Clear to the end of the line. COUNT is the minimum
1950 number of character spaces to clear, */
1951 clear_to_eol (count
)
1955 tputs (term_clreol
, 1, output_character_function
);
1958 /* Do one more character space. */
1960 for (i
= 0; i
< count
; i
++)
1961 putc (' ', out_stream
);
1967 /* **************************************************************** */
1969 /* Saving and Restoring the TTY */
1971 /* **************************************************************** */
1973 #ifdef NEW_TTY_DRIVER
1975 /* Standard flags, including ECHO. */
1976 static int original_tty_flags
= 0;
1978 /* Local mode flags, like LPASS8. */
1979 static int local_mode_flags
= 0;
1981 /* Terminal characters. This has C-s and C-q in it. */
1982 static struct tchars original_tchars
;
1984 /* Local special characters. This has the interrupt characters in it. */
1985 static struct ltchars original_ltchars
;
1987 /* We use this to get and set the tty_flags. */
1988 static struct sgttyb the_ttybuff
;
1990 /* Put the terminal in CBREAK mode so that we can detect key presses. */
1994 int tty
= fileno (rl_instream
);
1996 /* We always get the latest tty values. Maybe stty changed them. */
1998 ioctl (tty
, TIOCGETP
, &the_ttybuff
);
1999 original_tty_flags
= the_ttybuff
.sg_flags
;
2001 readline_echoing_p
= (original_tty_flags
& ECHO
);
2003 /* If this terminal doesn't care how the 8th bit is used,
2004 then we can use it for the meta-key.
2005 We check by seeing if BOTH odd and even parity are allowed. */
2006 if ((the_ttybuff
.sg_flags
& (ODDP
| EVENP
)) == (ODDP
| EVENP
))
2009 the_ttybuff
.sg_flags
|= PASS8
;
2012 #if defined (TIOCLGET) && defined (LPASS8)
2015 ioctl (tty
, TIOCLGET
, &flags
);
2016 local_mode_flags
= flags
;
2018 ioctl (tty
, TIOCLSET
, &flags
);
2027 ioctl (tty
, TIOCGETC
, &original_tchars
);
2028 bcopy (&original_tchars
, &temp
, sizeof (struct tchars
));
2030 /* Get rid of C-s and C-q.
2031 We remember the value of startc (C-q) so that if the terminal is in
2032 xoff state, the user can xon it by pressing that character. */
2033 xon_char
= temp
.t_startc
;
2037 /* If there is an XON character, bind it to restart the output. */
2039 rl_bind_key (xon_char
, rl_restart_output
);
2041 /* If there is an EOF char, bind eof_char to it. */
2042 if (temp
.t_eofc
!= -1)
2043 eof_char
= temp
.t_eofc
;
2046 /* Get rid of C-\ and C-c. */
2047 temp
.t_intrc
= temp
.t_quitc
= -1;
2050 ioctl (tty
, TIOCSETC
, &temp
);
2052 #endif /* TIOCGETC */
2056 struct ltchars temp
;
2058 ioctl (tty
, TIOCGLTC
, &original_ltchars
);
2059 bcopy (&original_ltchars
, &temp
, sizeof (struct ltchars
));
2061 /* Make the interrupt keys go away. Just enough to make people happy. */
2062 temp
.t_dsuspc
= -1; /* C-y */
2063 temp
.t_lnextc
= -1; /* C-v */
2065 ioctl (tty
, TIOCSLTC
, &temp
);
2067 #endif /* TIOCGLTC */
2069 the_ttybuff
.sg_flags
&= ~ECHO
;
2070 the_ttybuff
.sg_flags
|= CBREAK
;
2071 ioctl (tty
, TIOCSETN
, &the_ttybuff
);
2074 /* Restore the terminal to its original state. */
2076 rl_deprep_terminal ()
2078 int tty
= fileno (rl_instream
);
2080 #if defined (TIOCLGET) && defined (LPASS8)
2081 if ((the_ttybuff
.sg_flags
& (ODDP
| EVENP
)) == (ODDP
| EVENP
))
2082 ioctl (tty
, TIOCLSET
, &local_mode_flags
);
2086 ioctl (tty
, TIOCSLTC
, &original_ltchars
);
2090 ioctl (tty
, TIOCSETC
, &original_tchars
);
2093 the_ttybuff
.sg_flags
= original_tty_flags
;
2094 ioctl (tty
, TIOCSETN
, &the_ttybuff
);
2095 readline_echoing_p
= 1;
2098 #else /* !defined (NEW_TTY_DRIVER) */
2099 static struct termio otio
;
2104 int tty
= fileno (rl_instream
);
2107 ioctl (tty
, TCGETA
, &tio
);
2108 ioctl (tty
, TCGETA
, &otio
);
2110 readline_echoing_p
= (tio
.c_lflag
& ECHO
);
2112 tio
.c_lflag
&= ~(ICANON
|ECHO
);
2113 tio
.c_iflag
&= ~(IXON
|ISTRIP
|INPCK
);
2115 #ifndef HANDLE_SIGNALS
2116 tio
.c_lflag
&= ~ISIG
;
2119 tio
.c_cc
[VEOF
] = 1; /* really: MIN */
2120 tio
.c_cc
[VEOL
] = 0; /* really: TIME */
2121 ioctl (tty
, TCSETAW
,&tio
);
2125 rl_deprep_terminal ()
2127 int tty
= fileno (rl_instream
);
2128 ioctl (tty
, TCSETAW
, &otio
);
2130 #endif /* NEW_TTY_DRIVER */
2133 /* **************************************************************** */
2135 /* Utility Functions */
2137 /* **************************************************************** */
2139 /* Return 0 if C is not a member of the class of characters that belong
2140 in words, or 1 if it is. */
2142 int allow_pathname_alphabetic_chars
= 0;
2143 char *pathname_alphabetic_chars
= "/-_=~.#$";
2150 if (pure_alphabetic (c
) || (numeric (c
)))
2153 if (allow_pathname_alphabetic_chars
)
2154 return ((int)rindex (pathname_alphabetic_chars
, c
));
2159 /* Return non-zero if C is a numeric character. */
2164 return (c
>= '0' && c
<= '9');
2167 /* Ring the terminal bell. */
2171 if (readline_echoing_p
)
2173 fprintf (stderr
, "\007");
2179 /* How to abort things. */
2183 rl_clear_message ();
2184 rl_init_argument ();
2185 rl_pending_input
= 0;
2187 defining_kbd_macro
= 0;
2188 while (executing_macro
)
2189 pop_executing_macro ();
2191 longjmp (readline_top_level
, 1);
2194 /* Return a copy of the string between FROM and TO.
2195 FROM is inclusive, TO is not. */
2200 register int length
;
2203 /* Fix it if the caller is confused. */
2211 copy
= (char *)xmalloc (1 + length
);
2212 strncpy (copy
, the_line
+ from
, length
);
2213 copy
[length
] = '\0';
2218 /* **************************************************************** */
2220 /* Insert and Delete */
2222 /* **************************************************************** */
2225 /* Insert a string of text into the line at point. This is the only
2226 way that you should do insertion. rl_insert () calls this
2228 rl_insert_text (string
)
2231 extern int doing_an_undo
;
2232 register int i
, l
= strlen (string
);
2233 while (rl_end
+ l
>= rl_line_buffer_len
)
2236 (char *)xrealloc (rl_line_buffer
,
2237 rl_line_buffer_len
+= DEFAULT_BUFFER_SIZE
);
2238 the_line
= rl_line_buffer
;
2241 for (i
= rl_end
; i
>= rl_point
; i
--)
2242 the_line
[i
+ l
] = the_line
[i
];
2243 strncpy (the_line
+ rl_point
, string
, l
);
2245 /* Remember how to undo this if we aren't undoing something. */
2248 /* If possible and desirable, concatenate the undos. */
2249 if ((strlen (string
) == 1) &&
2251 (rl_undo_list
->what
== UNDO_INSERT
) &&
2252 (rl_undo_list
->end
== rl_point
) &&
2253 (rl_undo_list
->end
- rl_undo_list
->start
< 20))
2254 rl_undo_list
->end
++;
2256 rl_add_undo (UNDO_INSERT
, rl_point
, rl_point
+ l
, (char *)NULL
);
2260 the_line
[rl_end
] = '\0';
2263 /* Delete the string between FROM and TO. FROM is
2264 inclusive, TO is not. */
2265 rl_delete_text (from
, to
)
2268 extern int doing_an_undo
;
2269 register char *text
;
2271 /* Fix it if the caller is confused. */
2277 text
= rl_copy (from
, to
);
2278 strncpy (the_line
+ from
, the_line
+ to
, rl_end
- to
);
2280 /* Remember how to undo this delete. */
2282 rl_add_undo (UNDO_DELETE
, from
, to
, text
);
2286 rl_end
-= (to
- from
);
2287 the_line
[rl_end
] = '\0';
2291 /* **************************************************************** */
2293 /* Readline character functions */
2295 /* **************************************************************** */
2297 /* This is not a gap editor, just a stupid line input routine. No hair
2298 is involved in writing any of the functions, and none should be. */
2302 rl_end is the place in the string that we would place '\0';
2303 i.e., it is always safe to place '\0' there.
2305 rl_point is the place in the string where the cursor is. Sometimes
2306 this is the same as rl_end.
2308 Any command that is called interactively receives two arguments.
2309 The first is a count: the numeric arg pased to this command.
2310 The second is the key which invoked this command.
2314 /* **************************************************************** */
2316 /* Movement Commands */
2318 /* **************************************************************** */
2320 /* Note that if you `optimize' the display for these functions, you cannot
2321 use said functions in other functions which do not do optimizing display.
2322 I.e., you will have to update the data base for rl_redisplay, and you
2323 might as well let rl_redisplay do that job. */
2325 /* Move forward COUNT characters. */
2330 rl_backward (-count
);
2335 if (rl_point
== (rl_end
- (rl_editing_mode
== vi_mode
)))
2337 if (rl_point
== rl_end
)
2349 /* Move backward COUNT characters. */
2354 rl_forward (-count
);
2369 /* Move to the beginning of the line. */
2375 /* Move to the end of the line. */
2381 /* Move forward a word. We do what Emacs does. */
2382 rl_forward_word (count
)
2389 rl_backward_word (-count
);
2395 if (rl_point
== rl_end
)
2398 /* If we are not in a word, move forward until we are in one.
2399 Then, move forward until we hit a non-alphabetic character. */
2400 c
= the_line
[rl_point
];
2401 if (!alphabetic (c
))
2403 while (++rl_point
< rl_end
)
2405 c
= the_line
[rl_point
];
2406 if (alphabetic (c
)) break;
2409 if (rl_point
== rl_end
) return;
2410 while (++rl_point
< rl_end
)
2412 c
= the_line
[rl_point
];
2413 if (!alphabetic (c
)) break;
2419 /* Move backward a word. We do what Emacs does. */
2420 rl_backward_word (count
)
2427 rl_forward_word (-count
);
2436 /* Like rl_forward_word (), except that we look at the characters
2437 just before point. */
2439 c
= the_line
[rl_point
- 1];
2440 if (!alphabetic (c
))
2444 c
= the_line
[rl_point
- 1];
2445 if (alphabetic (c
)) break;
2451 c
= the_line
[rl_point
- 1];
2452 if (!alphabetic (c
))
2460 /* Clear the current line. Numeric argument to C-l does this. */
2463 int curr_line
= last_c_pos
/ screenwidth
;
2465 move_vert(curr_line
);
2466 move_cursor_relative (0, the_line
); /* XXX is this right */
2467 rl_forced_update_display ();
2468 rl_display_fixed
= 1;
2471 /* C-l typed to a line without quoting clears the screen, and then reprints
2472 the prompt and the current input line. Given a numeric arg, redraw only
2473 the current line. */
2476 extern char *term_clrpag
;
2477 static void output_character_function ();
2479 if (rl_explicit_arg
)
2486 tputs (term_clrpag
, 1, output_character_function
);
2490 rl_forced_update_display ();
2491 rl_display_fixed
= 1;
2495 /* **************************************************************** */
2499 /* **************************************************************** */
2501 /* Insert the character C at the current location, moving point forward. */
2502 rl_insert (count
, c
)
2511 /* If we can optimize, then do it. But don't let people crash
2512 readline because of extra large arguments. */
2513 if (count
> 1 && count
< 1024)
2515 string
= (char *)alloca (1 + count
);
2517 for (i
= 0; i
< count
; i
++)
2521 rl_insert_text (string
);
2529 string
= (char *)alloca (1024 + 1);
2531 for (i
= 0; i
< 1024; i
++)
2536 descreaser
= (count
> 1024 ? 1024 : count
);
2537 string
[descreaser
] = '\0';
2538 rl_insert_text (string
);
2539 count
-= descreaser
;
2544 /* We are inserting a single character.
2545 If there is pending input, then make a string of all of the
2546 pending characters that are bound to rl_insert, and insert
2550 int slen
, key
= 0, t
;
2553 string
= (char *)alloca (ibuffer_len
+ 1);
2556 while ((t
= rl_get_char (&key
)) &&
2557 (keymap
[key
].type
== ISFUNC
&&
2558 keymap
[key
].function
== rl_insert
))
2562 rl_unget_char (key
);
2565 rl_insert_text (string
);
2570 /* Inserting a single character. */
2571 string
= (char *)alloca (2);
2575 rl_insert_text (string
);
2579 /* Insert the next typed character verbatim. */
2580 rl_quoted_insert (count
)
2583 int c
= rl_read_key (in_stream
);
2584 rl_insert (count
, c
);
2587 /* Insert a tab character. */
2588 rl_tab_insert (count
)
2591 rl_insert (count
, '\t');
2595 /* Non-zero means enter insertion mode. */
2596 static vi_doing_insert
= 0;
2599 /* What to do when a NEWLINE is pressed. We accept the whole line.
2600 KEY is the key that invoked this command. I guess it could have
2601 meaning in the future. */
2602 rl_newline (count
, key
)
2610 if (vi_doing_insert
)
2612 rl_end_undo_group ();
2613 vi_doing_insert
= 0;
2616 #endif /* VI_MODE */
2618 if (readline_echoing_p
)
2620 move_vert (vis_botlin
);
2623 fflush (out_stream
);
2628 rl_clean_up_for_exit ()
2630 if (readline_echoing_p
)
2632 move_vert (vis_botlin
);
2634 fflush (out_stream
);
2635 rl_restart_output ();
2639 /* What to do for some uppercase characters, like meta characters,
2640 and some characters appearing in emacs_ctlx_keymap. This function
2641 is just a stub, you bind keys to it and the code in rl_dispatch ()
2642 is special cased. */
2643 rl_do_lowercase_version (ignore1
, ignore2
)
2644 int ignore1
, ignore2
;
2648 /* Rubout the character behind point. */
2666 int orig_point
= rl_point
;
2667 rl_backward (count
);
2668 rl_kill_text (orig_point
, rl_point
);
2672 int c
= the_line
[--rl_point
];
2673 rl_delete_text (rl_point
, rl_point
+ 1);
2675 if (rl_point
== rl_end
&& alphabetic (c
) && last_c_pos
)
2678 putc (' ', out_stream
);
2686 /* Delete the character under the cursor. Given a numeric argument,
2687 kill that many characters instead. */
2688 rl_delete (count
, invoking_key
)
2697 if (rl_point
== rl_end
)
2705 int orig_point
= rl_point
;
2707 rl_kill_text (orig_point
, rl_point
);
2708 rl_point
= orig_point
;
2711 rl_delete_text (rl_point
, rl_point
+ 1);
2715 /* **************************************************************** */
2719 /* **************************************************************** */
2721 /* The next two functions mimic unix line editing behaviour, except they
2722 save the deleted text on the kill ring. This is safer than not saving
2723 it, and since we have a ring, nobody should get screwed. */
2725 /* This does what C-w does in Unix. We can't prevent people from
2726 using behaviour that they expect. */
2727 rl_unix_word_rubout ()
2729 if (!rl_point
) ding ();
2731 int orig_point
= rl_point
;
2732 while (rl_point
&& whitespace (the_line
[rl_point
- 1]))
2734 while (rl_point
&& !whitespace (the_line
[rl_point
- 1]))
2736 rl_kill_text (rl_point
, orig_point
);
2740 /* Here is C-u doing what Unix does. You don't *have* to use these
2741 key-bindings. We have a choice of killing the entire line, or
2742 killing from where we are to the start of the line. We choose the
2743 latter, because if you are a Unix weenie, then you haven't backspaced
2744 into the line at all, and if you aren't, then you know what you are
2746 rl_unix_line_discard ()
2748 if (!rl_point
) ding ();
2750 rl_kill_text (rl_point
, 0);
2757 /* **************************************************************** */
2759 /* Commands For Typos */
2761 /* **************************************************************** */
2763 /* Random and interesting things in here. */
2766 /* **************************************************************** */
2770 /* **************************************************************** */
2772 /* The three kinds of things that we know how to do. */
2777 /* Uppercase the word at point. */
2778 rl_upcase_word (count
)
2781 rl_change_case (count
, UpCase
);
2784 /* Lowercase the word at point. */
2785 rl_downcase_word (count
)
2788 rl_change_case (count
, DownCase
);
2791 /* Upcase the first letter, downcase the rest. */
2792 rl_capitalize_word (count
)
2795 rl_change_case (count
, CapCase
);
2798 /* The meaty function.
2799 Change the case of COUNT words, performing OP on them.
2800 OP is one of UpCase, DownCase, or CapCase.
2801 If a negative argument is given, leave point where it started,
2802 otherwise, leave it where it moves to. */
2803 rl_change_case (count
, op
)
2806 register int start
= rl_point
, end
;
2809 rl_forward_word (count
);
2819 /* We are going to modify some text, so let's prepare to undo it. */
2820 rl_modifying (start
, end
);
2822 for (; start
< end
; start
++)
2827 the_line
[start
] = to_upper (the_line
[start
]);
2831 the_line
[start
] = to_lower (the_line
[start
]);
2837 the_line
[start
] = to_upper (the_line
[start
]);
2842 the_line
[start
] = to_lower (the_line
[start
]);
2844 if (!pure_alphabetic (the_line
[start
]))
2855 /* **************************************************************** */
2859 /* **************************************************************** */
2861 /* Transpose the words at point. */
2862 rl_transpose_words (count
)
2865 char *word1
, *word2
;
2866 int w1_beg
, w1_end
, w2_beg
, w2_end
;
2867 int orig_point
= rl_point
;
2871 /* Find the two words. */
2872 rl_forward_word (count
);
2874 rl_backward_word (1);
2876 rl_backward_word (count
);
2878 rl_forward_word (1);
2881 /* Do some check to make sure that there really are two words. */
2882 if ((w1_beg
== w2_beg
) || (w2_beg
< w1_end
))
2885 rl_point
= orig_point
;
2889 /* Get the text of the words. */
2890 word1
= rl_copy (w1_beg
, w1_end
);
2891 word2
= rl_copy (w2_beg
, w2_end
);
2893 /* We are about to do many insertions and deletions. Remember them
2894 as one operation. */
2895 rl_begin_undo_group ();
2897 /* Do the stuff at word2 first, so that we don't have to worry
2898 about word1 moving. */
2900 rl_delete_text (w2_beg
, w2_end
);
2901 rl_insert_text (word1
);
2904 rl_delete_text (w1_beg
, w1_end
);
2905 rl_insert_text (word2
);
2907 /* This is exactly correct since the text before this point has not
2908 changed in length. */
2911 /* I think that does it. */
2912 rl_end_undo_group ();
2913 free (word1
); free (word2
);
2916 /* Transpose the characters at point. If point is at the end of the line,
2917 then transpose the characters before point. */
2918 rl_transpose_chars (count
)
2924 if (!rl_point
|| rl_end
< 2) {
2930 if (rl_point
== rl_end
) {
2931 int t
= the_line
[rl_point
- 1];
2932 the_line
[rl_point
- 1] = the_line
[rl_point
- 2];
2933 the_line
[rl_point
- 2] = t
;
2935 int t
= the_line
[rl_point
];
2936 the_line
[rl_point
] = the_line
[rl_point
- 1];
2937 the_line
[rl_point
- 1] = t
;
2938 if (count
< 0 && rl_point
)
2951 /* **************************************************************** */
2953 /* Bogus Flow Control */
2955 /* **************************************************************** */
2957 rl_restart_output (count
, key
)
2960 int fildes
= fileno (stdin
);
2962 ioctl (fildes
, TIOCSTART
, 0);
2963 #endif /* TIOCSTART */
2966 /* **************************************************************** */
2968 /* Completion matching, from readline's point of view. */
2970 /* **************************************************************** */
2972 /* Pointer to the generator function for completion_matches ().
2973 NULL means to use filename_entry_function (), the default filename
2975 Function
*rl_completion_entry_function
= (Function
*)NULL
;
2977 /* Pointer to alternative function to create matches.
2978 Function is called with TEXT, START, and END.
2979 START and END are indices in RL_LINE_BUFFER saying what the boundaries
2981 If this function exists and returns NULL then call the value of
2982 rl_completion_entry_function to try to match, otherwise use the
2983 array of strings returned. */
2984 Function
*rl_attempted_completion_function
= (Function
*)NULL
;
2986 /* Complete the word at or before point. You have supplied the function
2987 that does the initial simple matching selection algorithm (see
2988 completion_matches ()). The default is to do filename completion. */
2989 rl_complete (ignore
, invoking_key
)
2990 int ignore
, invoking_key
;
2992 rl_complete_internal (TAB
);
2993 if (running_in_emacs
)
2994 printf ("%s", the_line
);
2997 /* List the possible completions. See description of rl_complete (). */
2998 rl_possible_completions ()
3000 rl_complete_internal ('?');
3003 /* The user must press "y" or "n". Non-zero return means "y" pressed. */
3008 c
= rl_read_key (in_stream
);
3009 if (c
== 'y' || c
== 'Y') return (1);
3010 if (c
== 'n' || c
== 'N') return (0);
3011 if (c
== ABORT_CHAR
) rl_abort ();
3015 /* Up to this many items will be displayed in response to a
3016 possible-completions call. After that, we ask the user if
3017 she is sure she wants to see them all. */
3018 int rl_completion_query_items
= 100;
3020 /* The basic list of characters that signal a break between words for the
3021 completer routine. The contents of this variable is what breaks words
3022 in the shell, i.e. " \t\n\"\\'`@$><=" */
3023 char *rl_basic_word_break_characters
= " \t\n\"\\'`@$><=";
3025 /* The list of characters that signal a break between words for
3026 rl_complete_internal. The default list is the contents of
3027 rl_basic_word_break_characters. */
3028 char *rl_completer_word_break_characters
= (char *)NULL
;
3030 /* List of characters that are word break characters, but should be left
3031 in TEXT when it is passed to the completion function. The shell uses
3032 this to help determine what kind of completing to do. */
3033 char *rl_special_prefixes
= (char *)NULL
;
3035 /* If non-zero, then disallow duplicates in the matches. */
3036 int rl_ignore_completion_duplicates
= 1;
3038 /* Non-zero means that the results of the matches are to be treated
3039 as filenames. This is ALWAYS zero on entry, and can only be changed
3040 within a completion entry finder function. */
3041 int rl_filename_completion_desired
= 0;
3043 /* Complete the word at or before point.
3044 WHAT_TO_DO says what to do with the completion.
3045 `?' means list the possible completions.
3046 TAB means do standard completion.
3047 `*' means insert all of the possible completions. */
3048 rl_complete_internal (what_to_do
)
3051 char *filename_completion_function ();
3052 char **completion_matches (), **matches
;
3054 int start
, end
, delimiter
= 0;
3057 if (rl_completion_entry_function
)
3058 our_func
= rl_completion_entry_function
;
3060 our_func
= (int (*)())filename_completion_function
;
3062 /* Only the completion entry function can change this. */
3063 rl_filename_completion_desired
= 0;
3065 /* We now look backwards for the start of a filename/variable word. */
3069 while (--rl_point
&&
3070 !rindex (rl_completer_word_break_characters
, the_line
[rl_point
]));
3072 /* If we are at a word break, then advance past it. */
3073 if (rindex (rl_completer_word_break_characters
, (the_line
[rl_point
])))
3075 /* If the character that caused the word break was a quoting
3076 character, then remember it as the delimiter. */
3077 if (rindex ("\"'", the_line
[rl_point
]) && (end
- rl_point
) > 1)
3078 delimiter
= the_line
[rl_point
];
3080 /* If the character isn't needed to determine something special
3081 about what kind of completion to perform, then advance past it. */
3083 if (!rl_special_prefixes
||
3084 !rindex (rl_special_prefixes
, the_line
[rl_point
]))
3091 text
= rl_copy (start
, end
);
3093 /* If the user wants to TRY to complete, but then wants to give
3094 up and use the default completion function, they set the
3095 variable rl_attempted_completion_function. */
3096 if (rl_attempted_completion_function
)
3099 (char **)(*rl_attempted_completion_function
) (text
, start
, end
);
3102 goto after_usual_completion
;
3105 matches
= completion_matches (text
, our_func
, start
, end
);
3107 after_usual_completion
:
3118 /* It seems to me that in all the cases we handle we would like
3119 to ignore duplicate possiblilities. Scan for the text to
3120 insert being identical to the other completions. */
3121 if (rl_ignore_completion_duplicates
)
3123 char *lowest_common
;
3126 /* Sort the items. */
3127 /* It is safe to sort this array, because the lowest common
3128 denominator found in matches[0] will remain in place. */
3129 for (i
= 0; matches
[i
]; i
++);
3130 qsort (matches
, i
, sizeof (char *), compare_strings
);
3132 /* Remember the lowest common denimator for it may be unique. */
3133 lowest_common
= savestring (matches
[0]);
3135 for (i
= 0; matches
[i
+ 1]; i
++)
3137 if (strcmp (matches
[i
], matches
[i
+ 1]) == 0)
3140 matches
[i
] = (char *)-1;
3146 /* We have marked all the dead slots with (char *)-1.
3147 Copy all the non-dead entries into a new array. */
3150 (char **)malloc ((3 + newlen
) * sizeof (char *));
3152 for (i
= 1, j
= 1; matches
[i
]; i
++)
3153 if (matches
[i
] != (char *)-1)
3154 temp_array
[j
++] = matches
[i
];
3155 temp_array
[j
] = (char *)NULL
;
3157 if (matches
[0] != (char *)-1)
3161 matches
= temp_array
;
3164 /* Place the lowest common denominator back in [0]. */
3165 matches
[0] = lowest_common
;
3167 /* If there is one string left, and it is identical to the
3168 lowest common denominator, then the LCD is the string to
3170 if (j
== 2 && strcmp (matches
[0], matches
[1]) == 0)
3173 matches
[1] = (char *)NULL
;
3180 rl_delete_text (start
, rl_point
);
3182 rl_insert_text (matches
[0]);
3184 /* If there are more matches, ring the bell to indicate.
3185 If this was the only match, and we are hacking files,
3186 check the file to see if it was a directory. If so,
3187 add a '/' to the name. If not, and we are at the end
3188 of the line, then add a space. */
3191 ding (); /* There are other matches remaining. */
3195 char temp_string
[2];
3197 temp_string
[0] = delimiter
? delimiter
: ' ';
3198 temp_string
[1] = '\0';
3200 if (rl_filename_completion_desired
)
3203 char *tilde_expand ();
3204 char *filename
= tilde_expand (matches
[0]);
3206 if ((stat (filename
, &finfo
) == 0) &&
3207 ((finfo
.st_mode
& S_IFMT
) == S_IFDIR
))
3209 if (the_line
[rl_point
] != '/')
3210 rl_insert_text ("/");
3214 if (rl_point
== rl_end
)
3215 rl_insert_text (temp_string
);
3221 if (rl_point
== rl_end
)
3222 rl_insert_text (temp_string
);
3231 rl_delete_text (start
, rl_point
);
3233 rl_begin_undo_group ();
3238 rl_insert_text (matches
[i
++]);
3239 rl_insert_text (" ");
3244 rl_insert_text (matches
[0]);
3245 rl_insert_text (" ");
3247 rl_end_undo_group ();
3254 int len
, count
, limit
, max
= 0;
3257 /* Handle simple case first. What if there is only one answer? */
3260 char *rindex (), *temp
;
3262 if (rl_filename_completion_desired
)
3263 temp
= rindex (matches
[0], '/');
3265 temp
= (char *)NULL
;
3273 fprintf (out_stream
, "%s", temp
);
3278 /* There is more than one answer. Find out how many there are,
3279 and find out what the maximum printed length of a single entry
3281 for (i
= 1; matches
[i
]; i
++)
3283 char *rindex (), *temp
= (char *)NULL
;
3285 /* If we are hacking filenames, then only count the characters
3286 after the last slash in the pathname. */
3287 if (rl_filename_completion_desired
)
3288 temp
= rindex (matches
[i
], '/');
3290 temp
= (char *)NULL
;
3297 if (strlen (temp
) > max
)
3298 max
= strlen (temp
);
3303 /* If there are many items, then ask the user if she
3304 really wants to see them all. */
3305 if (len
>= rl_completion_query_items
)
3308 fprintf (out_stream
,
3309 "There are %d possibilities. Do you really", len
);
3311 fprintf (out_stream
, "wish to see them all? (y or n)");
3312 fflush (out_stream
);
3319 /* How many items of MAX length can we fit in the screen window? */
3321 limit
= screenwidth
/ max
;
3322 if (limit
!= 1 && (limit
* max
== screenwidth
))
3325 /* How many iterations of the printing loop? */
3326 count
= (len
+ (limit
- 1)) / limit
;
3328 /* Watch out for special case. If LEN is less than LIMIT, then
3329 just do the inner printing loop. */
3330 if (len
< limit
) count
= 1;
3332 /* Sort the items if they are not already sorted. */
3333 if (!rl_ignore_completion_duplicates
)
3335 qsort (matches
, len
, sizeof (char *), compare_strings
);
3338 /* Print the sorted items, up-and-down alphabetically, like
3342 for (i
= 1; i
< count
+ 1; i
++)
3344 for (j
= 0, l
= i
; j
< limit
; j
++)
3346 if (l
> len
|| !matches
[l
])
3352 char *rindex (), *temp
= (char *)NULL
;
3354 if (rl_filename_completion_desired
)
3355 temp
= rindex (matches
[l
], '/');
3357 temp
= (char *)NULL
;
3364 fprintf (out_stream
, "%s", temp
);
3365 for (k
= 0; k
< max
- strlen (temp
); k
++)
3366 putc (' ', out_stream
);
3382 for (i
= 0; matches
[i
]; i
++)
3388 /* A completion function for usernames.
3389 TEXT contains a partial username preceded by a random
3390 character (usually `~'). */
3392 username_completion_function (text
, state
)
3396 static char *username
= (char *)NULL
;
3397 static struct passwd
*entry
;
3404 username
= savestring (&text
[1]);
3405 namelen
= strlen (username
);
3409 while (entry
= getpwent ())
3411 if (strncmp (username
, entry
->pw_name
, namelen
) == 0)
3418 return ((char *)NULL
);
3422 char *value
= (char *)xmalloc (2 + strlen (entry
->pw_name
));
3424 strcpy (value
+ 1, entry
->pw_name
);
3425 rl_filename_completion_desired
= 1;
3430 /* If non-null, this contains the address of a function to call if the
3431 standard meaning for expanding a tilde fails. The function is called
3432 with the text (sans tilde, as in "foo"), and returns a malloc()'ed string
3433 which is the expansion, or a NULL pointer if there is no expansion. */
3434 Function
*rl_tilde_expander
= (Function
*)NULL
;
3436 /* Expand FILENAME if it begins with a tilde. This always returns
3439 tilde_expand (filename
)
3442 char *dirname
= filename
? savestring (filename
) : (char *)NULL
;
3444 if (dirname
&& *dirname
== '~')
3447 if (!dirname
[1] || dirname
[1] == '/')
3449 /* Prepend $HOME to the rest of the string. */
3450 char *temp_home
= (char *)getenv ("HOME");
3452 temp_name
= (char *)alloca (1 + strlen (&dirname
[1])
3453 + (temp_home
? strlen (temp_home
) : 0));
3454 temp_name
[0] = '\0';
3456 strcpy (temp_name
, temp_home
);
3457 strcat (temp_name
, &dirname
[1]);
3459 dirname
= savestring (temp_name
);
3463 struct passwd
*getpwnam (), *user_entry
;
3464 char *username
= (char *)alloca (257);
3467 for (i
= 1; c
= dirname
[i
]; i
++)
3469 if (c
== '/') break;
3470 else username
[i
- 1] = c
;
3472 username
[i
- 1] = '\0';
3474 if (!(user_entry
= getpwnam (username
)))
3476 /* If the calling program has a special syntax for
3477 expanding tildes, and we couldn't find a standard
3478 expansion, then let them try. */
3479 if (rl_tilde_expander
)
3483 expansion
= (char *)(*rl_tilde_expander
) (username
);
3487 temp_name
= (char *)alloca (1 + strlen (expansion
)
3488 + strlen (&dirname
[i
]));
3489 strcpy (temp_name
, expansion
);
3490 strcat (temp_name
, &dirname
[i
]);
3496 * We shouldn't report errors.
3501 temp_name
= (char *)alloca (1 + strlen (user_entry
->pw_dir
)
3502 + strlen (&dirname
[i
]));
3503 strcpy (temp_name
, user_entry
->pw_dir
);
3504 strcat (temp_name
, &dirname
[i
]);
3507 dirname
= savestring (temp_name
);
3515 /* **************************************************************** */
3517 /* Undo, and Undoing */
3519 /* **************************************************************** */
3521 /* Non-zero tells rl_delete_text and rl_insert_text to not add to
3523 int doing_an_undo
= 0;
3525 /* The current undo list for THE_LINE. */
3526 UNDO_LIST
*rl_undo_list
= (UNDO_LIST
*)NULL
;
3528 /* Remember how to undo something. Concatenate some undos if that
3530 rl_add_undo (what
, start
, end
, text
)
3531 enum undo_code what
;
3535 UNDO_LIST
*temp
= (UNDO_LIST
*)xmalloc (sizeof (UNDO_LIST
));
3537 temp
->start
= start
;
3540 temp
->next
= rl_undo_list
;
3541 rl_undo_list
= temp
;
3544 /* Free the existing undo list. */
3547 while (rl_undo_list
) {
3548 UNDO_LIST
*release
= rl_undo_list
;
3549 rl_undo_list
= rl_undo_list
->next
;
3551 if (release
->what
== UNDO_DELETE
)
3552 free (release
->text
);
3558 /* Undo the next thing in the list. Return 0 if there
3559 is nothing to undo, or non-zero if there was. */
3564 int waiting_for_begin
= 0;
3572 switch (rl_undo_list
->what
) {
3574 /* Undoing deletes means inserting some text. */
3576 rl_point
= rl_undo_list
->start
;
3577 rl_insert_text (rl_undo_list
->text
);
3578 free (rl_undo_list
->text
);
3581 /* Undoing inserts means deleting some text. */
3583 rl_delete_text (rl_undo_list
->start
, rl_undo_list
->end
);
3584 rl_point
= rl_undo_list
->start
;
3587 /* Undoing an END means undoing everything 'til we get to
3590 waiting_for_begin
++;
3593 /* Undoing a BEGIN means that we are done with this group. */
3595 if (waiting_for_begin
)
3596 waiting_for_begin
--;
3604 release
= rl_undo_list
;
3605 rl_undo_list
= rl_undo_list
->next
;
3608 if (waiting_for_begin
)
3614 /* Begin a group. Subsequent undos are undone as an atomic operation. */
3615 rl_begin_undo_group ()
3617 rl_add_undo (UNDO_BEGIN
, 0, 0, 0);
3620 /* End an undo group started with rl_begin_undo_group (). */
3621 rl_end_undo_group ()
3623 rl_add_undo (UNDO_END
, 0, 0, 0);
3626 /* Save an undo entry for the text from START to END. */
3627 rl_modifying (start
, end
)
3639 char *temp
= rl_copy (start
, end
);
3640 rl_begin_undo_group ();
3641 rl_add_undo (UNDO_DELETE
, start
, end
, temp
);
3642 rl_add_undo (UNDO_INSERT
, start
, end
, (char *)NULL
);
3643 rl_end_undo_group ();
3647 /* Revert the current line to its previous state. */
3650 if (!rl_undo_list
) ding ();
3652 while (rl_undo_list
)
3657 /* Do some undoing of things that were done. */
3658 rl_undo_command (count
)
3660 if (count
< 0) return; /* Nothing to do. */
3676 /* **************************************************************** */
3678 /* History Utilities */
3680 /* **************************************************************** */
3682 /* We already have a history library, and that is what we use to control
3683 the history features of readline. However, this is our local interface
3684 to the history mechanism. */
3686 /* While we are editing the history, this is the saved
3687 version of the original line. */
3688 HIST_ENTRY
*saved_line_for_history
= (HIST_ENTRY
*)NULL
;
3690 /* Set the history pointer back to the last entry in the history. */
3691 start_using_history ()
3694 if (saved_line_for_history
)
3695 free_history_entry (saved_line_for_history
);
3697 saved_line_for_history
= (HIST_ENTRY
*)NULL
;
3700 /* Free the contents (and containing structure) of a HIST_ENTRY. */
3701 free_history_entry (entry
)
3710 /* Perhaps put back the current line if it has changed. */
3711 maybe_replace_line ()
3713 HIST_ENTRY
*temp
= current_history ();
3715 /* If the current line has changed, save the changes. */
3716 if (temp
&& ((UNDO_LIST
*)(temp
->data
) != rl_undo_list
)) {
3717 temp
= replace_history_entry (where_history (), the_line
, rl_undo_list
);
3723 /* Put back the saved_line_for_history if there is one. */
3724 maybe_unsave_line ()
3726 if (saved_line_for_history
) {
3727 strcpy (the_line
, saved_line_for_history
->line
);
3728 rl_undo_list
= (UNDO_LIST
*)saved_line_for_history
->data
;
3729 free_history_entry (saved_line_for_history
);
3730 saved_line_for_history
= (HIST_ENTRY
*)NULL
;
3731 rl_end
= rl_point
= strlen (the_line
);
3737 /* Save the current line in saved_line_for_history. */
3740 if (!saved_line_for_history
) {
3741 saved_line_for_history
= (HIST_ENTRY
*)xmalloc (sizeof (HIST_ENTRY
));
3742 saved_line_for_history
->line
= savestring (the_line
);
3743 saved_line_for_history
->data
= (char *)rl_undo_list
;
3749 /* **************************************************************** */
3751 /* History Commands */
3753 /* **************************************************************** */
3755 /* Meta-< goes to the start of the history. */
3756 rl_beginning_of_history ()
3758 rl_get_previous_history (1 + where_history ());
3761 /* Meta-> goes to the end of the history. (The current line). */
3762 rl_end_of_history ()
3764 maybe_replace_line ();
3766 maybe_unsave_line ();
3769 /* Move down to the next history line. */
3770 rl_get_next_history (count
)
3773 HIST_ENTRY
*temp
= (HIST_ENTRY
*)NULL
;
3777 rl_get_previous_history (-count
);
3784 maybe_replace_line ();
3788 temp
= next_history ();
3795 maybe_unsave_line ();
3798 strcpy (the_line
, temp
->line
);
3799 rl_undo_list
= (UNDO_LIST
*)temp
->data
;
3800 rl_end
= rl_point
= strlen (the_line
);
3804 /* Get the previous item out of our interactive history, making it the current
3805 line. If there is no previous history, just ding. */
3806 rl_get_previous_history (count
)
3809 HIST_ENTRY
*old_temp
= (HIST_ENTRY
*)NULL
;
3810 HIST_ENTRY
*temp
= (HIST_ENTRY
*)NULL
;
3814 rl_get_next_history (-count
);
3821 /* If we don't have a line saved, then save this one. */
3824 /* If the current line has changed, save the changes. */
3825 maybe_replace_line ();
3829 temp
= previous_history ();
3837 /* If there was a large argument, and we moved back to the start of the
3838 history, that is not an error. So use the last value found. */
3839 if (!temp
&& old_temp
)
3846 strcpy (the_line
, temp
->line
);
3847 rl_undo_list
= (UNDO_LIST
*)temp
->data
;
3848 rl_end
= rl_point
= strlen (the_line
);
3850 if (rl_editing_mode
== vi_mode
)
3852 #endif /* VI_MODE */
3856 /* There is a command in ksh which yanks into this line, the last word
3857 of the previous line. Here it is. We left it on M-. */
3858 rl_yank_previous_last_arg (ignore
)
3865 /* **************************************************************** */
3867 /* I-Search and Searching */
3869 /* **************************************************************** */
3871 /* Search backwards through the history looking for a string which is typed
3872 interactively. Start with the current line. */
3873 rl_reverse_search_history (sign
, key
)
3877 rl_search_history (-sign
, key
);
3880 /* Search forwards through the history looking for a string which is typed
3881 interactively. Start with the current line. */
3882 rl_forward_search_history (sign
, key
)
3886 rl_search_history (sign
, key
);
3889 /* Display the current state of the search in the echo-area.
3890 SEARCH_STRING contains the string that is being searched for,
3891 DIRECTION is zero for forward, or 1 for reverse,
3892 WHERE is the history list number of the current line. If it is
3893 -1, then this line is the starting one. */
3894 rl_display_search (search_string
, reverse_p
, where
)
3895 char *search_string
;
3896 int reverse_p
, where
;
3898 char *message
= (char *)NULL
;
3901 (char *)alloca (1 + (search_string
? strlen (search_string
) : 0) + 30);
3907 sprintf (message
, "[%d]", where
+ history_base
);
3910 strcat (message
, "(");
3913 strcat (message
, "reverse-");
3915 strcat (message
, "i-search)`");
3918 strcat (message
, search_string
);
3920 strcat (message
, "': ");
3921 rl_message (message
, 0, 0);
3925 /* Search through the history looking for an interactively typed string.
3926 This is analogous to i-search. We start the search in the current line.
3927 DIRECTION is which direction to search; > 0 means forward, < 0 means
3929 rl_search_history (direction
, invoking_key
)
3933 /* The string that the user types in to search for. */
3934 char *search_string
= (char *)alloca (128);
3936 /* The current length of SEARCH_STRING. */
3937 int search_string_index
;
3939 /* The list of lines to search through. */
3942 /* The length of LINES. */
3945 /* Where we get LINES from. */
3946 HIST_ENTRY
**hlist
= history_list ();
3948 int orig_point
= rl_point
;
3949 int orig_line
= where_history ();
3950 int last_found_line
= orig_line
;
3955 /* The line currently being searched. */
3958 /* Offset in that line. */
3961 /* Non-zero if we are doing a reverse search. */
3962 int reverse
= (direction
< 0);
3964 /* Create an arrary of pointers to the lines that we want to search. */
3966 maybe_replace_line ();
3968 for (i
= 0; hlist
[i
]; i
++);
3970 /* Allocate space for this many lines, +1 for the current input line,
3971 and remember those lines. */
3972 lines
= (char **)alloca ((1 + (hlen
= i
)) * sizeof (char *));
3973 for (i
= 0; i
< hlen
; i
++)
3974 lines
[i
] = hlist
[i
]->line
;
3976 if (saved_line_for_history
)
3977 lines
[i
] = saved_line_for_history
->line
;
3980 /* So I have to type it in this way instead. */
3981 lines
[i
] = (char *)alloca (1 + strlen (the_line
));
3982 strcpy (lines
[i
], &the_line
[0]);
3987 /* The line where we start the search. */
3990 /* Initialize search parameters. */
3991 *search_string
= '\0';
3992 search_string_index
= 0;
3994 rl_display_search (search_string
, reverse
, -1);
4001 c
= rl_read_key (in_stream
);
4003 /* Hack C to Do What I Mean. */
4005 Function
*f
= (Function
*)NULL
;
4007 if (keymap
[c
].type
== ISFUNC
)
4008 f
= keymap
[c
].function
;
4010 if (f
== rl_reverse_search_history
)
4011 c
= reverse
? -1 : -2;
4012 else if (f
== rl_forward_search_history
)
4013 c
= !reverse
? -1 : -2;
4022 /* case invoking_key: */
4026 /* switch directions */
4028 direction
= -direction
;
4029 reverse
= (direction
< 0);
4034 strcpy (the_line
, lines
[orig_line
]);
4035 rl_point
= orig_point
;
4036 rl_end
= strlen (the_line
);
4037 rl_clear_message ();
4041 if (c
< 32 || c
> 126)
4043 rl_execute_next (c
);
4049 search_string
[search_string_index
++] = c
;
4050 search_string
[search_string_index
] = '\0';
4055 if (!search_string_index
)
4062 if (index
!= strlen (sline
))
4077 search_string_index
) == 0)
4084 register int limit
=
4085 (strlen (sline
) - search_string_index
) + 1;
4087 while (index
< limit
)
4089 if (strncmp (search_string
,
4091 search_string_index
) == 0)
4100 /* At limit for direction? */
4101 if ((reverse
&& i
< 0) ||
4102 (!reverse
&& i
== hlen
))
4107 index
= strlen (sline
);
4111 /* If the search string is longer than the current
4113 if (search_string_index
> strlen (sline
))
4116 /* Start actually searching. */
4118 index
-= search_string_index
;
4122 /* We cannot find the search string. Ding the bell. */
4124 i
= last_found_line
;
4128 /* We have found the search string. Just display it. But don't
4129 actually move there in the history list until the user accepts
4131 strcpy (the_line
, lines
[i
]);
4133 rl_end
= strlen (the_line
);
4134 last_found_line
= i
;
4135 rl_display_search (search_string
, reverse
,
4136 (i
== orig_line
) ? -1 : i
);
4141 /* The user has won. They found the string that they wanted. Now all
4142 we have to do is place them there. */
4144 int now
= last_found_line
;
4146 /* First put back the original state. */
4147 strcpy (the_line
, lines
[orig_line
]);
4149 if (now
< orig_line
)
4150 rl_get_previous_history (orig_line
- now
);
4152 rl_get_next_history (now
- orig_line
);
4155 rl_clear_message ();
4159 /* Make C be the next command to be executed. */
4163 rl_pending_input
= c
;
4166 /* **************************************************************** */
4168 /* Killing Mechanism */
4170 /* **************************************************************** */
4172 /* What we assume for a max number of kills. */
4173 #define DEFAULT_MAX_KILLS 10
4175 /* The real variable to look at to find out when to flush kills. */
4176 int rl_max_kills
= DEFAULT_MAX_KILLS
;
4178 /* Where to store killed text. */
4179 char **rl_kill_ring
= (char **)NULL
;
4181 /* Where we are in the kill ring. */
4182 int rl_kill_index
= 0;
4184 /* How many slots we have in the kill ring. */
4185 int rl_kill_ring_length
= 0;
4187 /* How to say that you only want to save a certain amount
4188 of kill material. */
4189 rl_set_retained_kills (num
)
4193 /* The way to kill something. This appends or prepends to the last
4194 kill, if the last command was a kill command. if FROM is less
4195 than TO, then the text is appended, otherwise prepended. If the
4196 last command was not a kill command, then a new slot is made for
4198 rl_kill_text (from
, to
)
4202 char *text
= rl_copy (from
, to
);
4204 /* Is there anything to kill? */
4207 last_command_was_kill
++;
4211 /* Delete the copied text from the line. */
4212 rl_delete_text (from
, to
);
4214 /* First, find the slot to work with. */
4215 if (!last_command_was_kill
) {
4217 /* Get a new slot. */
4218 if (!rl_kill_ring
) {
4220 /* If we don't have any defined, then make one. */
4222 (char **)xmalloc (((rl_kill_ring_length
= 1) + 1) * sizeof (char *));
4227 /* We have to add a new slot on the end, unless we have exceeded
4228 the max limit for remembering kills. */
4229 slot
= rl_kill_ring_length
;
4230 if (slot
== rl_max_kills
) {
4232 free (rl_kill_ring
[0]);
4233 for (i
= 0; i
< slot
; i
++)
4234 rl_kill_ring
[i
] = rl_kill_ring
[i
+ 1];
4237 (char **)xrealloc (rl_kill_ring
,
4238 ((slot
= (rl_kill_ring_length
+= 1)) + 1)
4244 slot
= rl_kill_ring_length
- 1;
4247 /* If the last command was a kill, prepend or append. */
4248 if (last_command_was_kill
) {
4249 char *old
= rl_kill_ring
[slot
];
4250 char *new = (char *)xmalloc (1 + strlen (old
) + strlen (text
));
4261 rl_kill_ring
[slot
] = new;
4263 rl_kill_ring
[slot
] = text
;
4265 rl_kill_index
= slot
;
4266 last_command_was_kill
++;
4269 /* Now REMEMBER! In order to do prepending or appending correctly, kill
4270 commands always make rl_point's original position be the FROM argument,
4271 and rl_point's extent be the TO argument. */
4274 /* **************************************************************** */
4276 /* Killing Commands */
4278 /* **************************************************************** */
4280 /* Delete the word at point, saving the text in the kill ring. */
4281 rl_kill_word (count
)
4284 int orig_point
= rl_point
;
4287 rl_backward_kill_word (-count
);
4290 rl_forward_word (count
);
4292 if (rl_point
!= orig_point
)
4293 rl_kill_text (orig_point
, rl_point
);
4295 rl_point
= orig_point
;
4299 /* Rubout the word before point, placing it on the kill ring. */
4300 rl_backward_kill_word (count
)
4303 int orig_point
= rl_point
;
4306 rl_kill_word (-count
);
4309 rl_backward_word (count
);
4311 if (rl_point
!= orig_point
)
4312 rl_kill_text (orig_point
, rl_point
);
4316 /* Kill from here to the end of the line. If DIRECTION is negative, kill
4317 back to the line start instead. */
4318 rl_kill_line (direction
)
4321 int orig_point
= rl_point
;
4324 rl_backward_kill_line (1);
4328 if (orig_point
!= rl_point
)
4329 rl_kill_text (orig_point
, rl_point
);
4330 rl_point
= orig_point
;
4334 /* Kill backwards to the start of the line. If DIRECTION is negative, kill
4335 forwards to the line end instead. */
4336 rl_backward_kill_line (direction
)
4339 int orig_point
= rl_point
;
4350 rl_kill_text (orig_point
, rl_point
);
4355 /* Yank back the last killed text. This ignores arguments. */
4358 if (!rl_kill_ring
) rl_abort ();
4359 rl_insert_text (rl_kill_ring
[rl_kill_index
]);
4362 /* If the last command was yank, or yank_pop, and the text just
4363 before point is identical to the current kill item, then
4364 delete that text from the line, rotate the index down, and
4365 yank back some other text. */
4370 if (((rl_last_func
!= rl_yank_pop
) && (rl_last_func
!= rl_yank
)) ||
4376 l
= strlen (rl_kill_ring
[rl_kill_index
]);
4377 if (((rl_point
- l
) >= 0) &&
4378 (strncmp (the_line
+ (rl_point
- l
),
4379 rl_kill_ring
[rl_kill_index
], l
) == 0))
4381 rl_delete_text ((rl_point
- l
), rl_point
);
4384 if (rl_kill_index
< 0)
4385 rl_kill_index
= rl_kill_ring_length
- 1;
4393 /* Yank the COUNTth argument from the previous history line. */
4394 rl_yank_nth_arg (count
, ignore
)
4397 register HIST_ENTRY
*entry
= previous_history ();
4408 arg
= history_arg_extract (count
, count
, entry
->line
);
4415 rl_begin_undo_group ();
4416 if (rl_point
&& the_line
[rl_point
- 1] != ' ')
4417 rl_insert_text (" ");
4418 rl_insert_text (arg
);
4420 rl_end_undo_group ();
4425 #include "vi_mode.c"
4426 #endif /* VI_MODE */
4428 /* How to toggle back and forth between editing modes. */
4429 rl_vi_editing_mode ()
4432 rl_editing_mode
= vi_mode
;
4433 rl_vi_insertion_mode ();
4434 #endif /* VI_MODE */
4437 rl_emacs_editing_mode ()
4439 rl_editing_mode
= emacs_mode
;
4440 keymap
= emacs_standard_keymap
;
4444 /* **************************************************************** */
4448 /* **************************************************************** */
4450 /* Non-zero means that case is not significant in completion. */
4451 int completion_case_fold
= 0;
4453 /* Return an array of (char *) which is a list of completions for TEXT.
4454 If there are no completions, return a NULL pointer.
4455 The first entry in the returned array is the substitution for TEXT.
4456 The remaining entries are the possible completions.
4457 The array is terminated with a NULL pointer.
4459 ENTRY_FUNCTION is a function of two args, and returns a (char *).
4460 The first argument is TEXT.
4461 The second is a state argument; it should be zero on the first call, and
4462 non-zero on subsequent calls. It returns a NULL pointer to the caller
4463 when there are no more matches.
4466 completion_matches (text
, entry_function
)
4468 char *(*entry_function
) ();
4470 /* Number of slots in match_list. */
4471 int match_list_size
;
4473 /* The list of matches. */
4475 (char **)xmalloc (((match_list_size
= 10) + 1) * sizeof (char *));
4477 /* Number of matches actually found. */
4480 /* Temporary string binder. */
4483 match_list
[1] = (char *)NULL
;
4485 while (string
= (*entry_function
) (text
, matches
))
4487 if (matches
+ 1 == match_list_size
)
4489 (char **)xrealloc (match_list
,
4490 ((match_list_size
+= 10) + 1) * sizeof (char *));
4492 match_list
[++matches
] = string
;
4493 match_list
[matches
+ 1] = (char *)NULL
;
4496 /* If there were any matches, then look through them finding out the
4497 lowest common denominator. That then becomes match_list[0]. */
4501 int low
= 100000; /* Count of max-matched characters. */
4503 /* If only one match, just use that. */
4506 match_list
[0] = match_list
[1];
4507 match_list
[1] = (char *)NULL
;
4511 /* Otherwise, compare each member of the list with
4512 the next, finding out where they stop matching. */
4516 register int c1
, c2
, si
;
4518 if (completion_case_fold
)
4521 (c1
= to_lower(match_list
[i
][si
])) &&
4522 (c2
= to_lower(match_list
[i
+ 1][si
]));
4524 if (c1
!= c2
) break;
4529 (c1
= match_list
[i
][si
]) &&
4530 (c2
= match_list
[i
+ 1][si
]);
4532 if (c1
!= c2
) break;
4535 if (low
> si
) low
= si
;
4538 match_list
[0] = (char *)xmalloc (low
+ 1);
4539 strncpy (match_list
[0], match_list
[1], low
);
4540 match_list
[0][low
] = '\0';
4543 else /* There were no matches. */
4546 match_list
= (char **)NULL
;
4548 return (match_list
);
4551 /* Okay, now we write the entry_function for filename completion. In the
4552 general case. Note that completion in the shell is a little different
4553 because of all the pathnames that must be followed when looking up the
4554 completion for a command. */
4556 filename_completion_function (text
, state
)
4560 static DIR *directory
;
4561 static char *filename
= (char *)NULL
;
4562 static char *dirname
= (char *)NULL
;
4563 static char *users_dirname
= (char *)NULL
;
4564 static int filename_len
;
4566 struct direct
*entry
= (struct direct
*)NULL
;
4568 /* If we don't have any state, then do some initialization. */
4571 char *rindex (), *temp
;
4573 if (dirname
) free (dirname
);
4574 if (filename
) free (filename
);
4575 if (users_dirname
) free (users_dirname
);
4577 filename
= savestring (text
);
4578 if (!*text
) text
= ".";
4579 dirname
= savestring (text
);
4581 temp
= rindex (dirname
, '/');
4585 strcpy (filename
, ++temp
);
4589 strcpy (dirname
, ".");
4591 /* We aren't done yet. We also support the "~user" syntax. */
4593 /* Save the version of the directory that the user typed. */
4594 users_dirname
= savestring (dirname
);
4596 char *tilde_expand (), *temp_dirname
= tilde_expand (dirname
);
4598 dirname
= temp_dirname
;
4601 extern int follow_symbolic_links
;
4602 char *make_absolute ();
4604 if (follow_symbolic_links
&& (strcmp (dirname
, ".") != 0))
4606 temp_dirname
= make_absolute (dirname
, get_working_directory (""));
4611 dirname
= temp_dirname
;
4617 directory
= opendir (dirname
);
4618 filename_len
= strlen (filename
);
4620 rl_filename_completion_desired
= 1;
4623 /* At this point we should entertain the possibility of hacking wildcarded
4624 filenames, like /usr/man*\/te<TAB>. If the directory name contains
4625 globbing characters, then build an array of directories to glob on, and
4626 glob on the first one. */
4628 /* Now that we have some state, we can read the directory. */
4630 while (directory
&& (entry
= readdir (directory
)))
4632 /* Special case for no filename.
4633 All entries except "." and ".." match. */
4636 if ((strcmp (entry
->d_name
, ".") != 0) &&
4637 (strcmp (entry
->d_name
, "..") != 0))
4642 /* Otherwise, if these match upto the length of filename, then
4645 if ((strlen (entry
->d_name
) >= filename_len
) &&
4646 (strncmp (filename
, entry
->d_name
, filename_len
) == 0))
4648 if ((entry
->d_namlen
>= filename_len
) &&
4649 (strncmp (filename
, entry
->d_name
, filename_len
) == 0))
4650 #endif /* TMB_SYSV */
4661 closedir (directory
);
4662 directory
= (DIR *)NULL
;
4664 return (char *)NULL
;
4670 if (dirname
&& (strcmp (dirname
, ".") != 0))
4673 temp
= (char *)xmalloc (1 + strlen (users_dirname
)
4674 + strlen (entry
->d_name
));
4676 temp
= (char *)xmalloc (1 + strlen (users_dirname
)
4678 #endif /* TMB_SYSV */
4679 strcpy (temp
, users_dirname
);
4680 strcat (temp
, entry
->d_name
);
4684 temp
= (savestring (entry
->d_name
));
4691 /* **************************************************************** */
4695 /* **************************************************************** */
4697 /* rl_add_defun (char *name, Function *function, int key)
4698 Add NAME to the list of named functions. Make FUNCTION
4699 be the function that gets called.
4700 If KEY is not -1, then bind it. */
4701 rl_add_defun (name
, function
, key
)
4707 rl_bind_key (key
, function
);
4708 rl_add_funmap_entry (name
, function
);
4711 /* Bind KEY to FUNCTION. Returns non-zero if KEY is out of range. */
4713 rl_bind_key (key
, function
)
4720 if (key
> 127 && key
< 256)
4722 if (keymap
[ESC
].type
== ISKMAP
)
4724 Keymap escmap
= (Keymap
)keymap
[ESC
].function
;
4727 escmap
[key
].type
= ISFUNC
;
4728 escmap
[key
].function
= function
;
4734 keymap
[key
].type
= ISFUNC
;
4735 keymap
[key
].function
= function
;
4739 /* Bind KEY to FUNCTION in MAP. Returns non-zero in case of invalid
4742 rl_bind_key_in_map (key
, function
, map
)
4748 Keymap oldmap
= keymap
;
4751 result
= rl_bind_key (key
, function
);
4756 /* Make KEY do nothing in the currently selected keymap.
4757 Returns non-zero in case of error. */
4762 return (rl_bind_key (key
, (Function
*)NULL
));
4765 /* Make KEY do nothing in MAP.
4766 Returns non-zero in case of error. */
4768 rl_unbind_key_in_map (key
, map
)
4772 return (rl_bind_key_in_map (key
, (Function
*)NULL
, map
));
4775 /* Bind the key sequence represented by the string KEYSEQ to
4776 FUNCTION. This makes new keymaps as necessary. The initial
4777 place to do bindings is in MAP. */
4778 rl_set_key (keyseq
, function
, map
)
4783 rl_generic_bind (ISFUNC
, keyseq
, function
, map
);
4786 /* Bind the key sequence represented by the string KEYSEQ to
4787 the string of characters MACRO. This makes new keymaps as
4788 necessary. The initial place to do bindings is in MAP. */
4789 rl_macro_bind (keyseq
, macro
, map
)
4790 char *keyseq
, *macro
;
4793 char *macro_keys
= (char *)xmalloc (2 * (strlen (macro
)));
4796 if (rl_translate_keyseq (macro
, macro_keys
, ¯o_keys_len
))
4801 rl_generic_bind (ISMACR
, keyseq
, macro_keys
, map
);
4804 /* Bind the key sequence represented by the string KEYSEQ to
4805 the arbitrary pointer DATA. TYPE says what kind of data is
4806 pointed to by DATA, right now this can be a function (ISFUNC),
4807 a macro (ISMACR), or a keymap (ISKMAP). This makes new keymaps
4808 as necessary. The initial place to do bindings is in MAP. */
4809 rl_generic_bind (type
, keyseq
, data
, map
)
4811 char *keyseq
, *data
;
4818 /* If no keys to bind to, exit right away. */
4819 if (!keyseq
|| !*keyseq
)
4826 keys
= (char *)alloca (1 + (2 * strlen (keyseq
)));
4828 /* Translate the ASCII representation of KEYSEQ into an array
4829 of characters. Stuff the characters into ARRAY, and the
4830 length of ARRAY into LENGTH. */
4831 if (rl_translate_keyseq (keyseq
, keys
, &keys_len
))
4834 /* Bind keys, making new keymaps as necessary. */
4835 for (i
= 0; i
< keys_len
; i
++)
4837 if (i
+ 1 < keys_len
)
4839 if (map
[keys
[i
]].type
!= ISKMAP
)
4841 if (map
[i
].type
== ISMACR
)
4842 free ((char *)map
[i
].function
);
4844 map
[keys
[i
]].type
= ISKMAP
;
4845 map
[keys
[i
]].function
= (Function
*)rl_make_bare_keymap ();
4847 map
= (Keymap
)map
[keys
[i
]].function
;
4851 if (map
[keys
[i
]].type
== ISMACR
)
4852 free ((char *)map
[keys
[i
]].function
);
4854 map
[keys
[i
]].function
= (Function
*)data
;
4855 map
[keys
[i
]].type
= type
;
4860 /* Translate the ASCII representation of SEQ, stuffing the
4861 values into ARRAY, an array of characters. LEN gets the
4862 final length of ARRAY. Return non-zero if there was an
4863 error parsing SEQ. */
4864 rl_translate_keyseq (seq
, array
, len
)
4868 register int i
, c
, l
= 0;
4870 for (i
= 0; c
= seq
[i
]; i
++)
4879 if (((c
== 'C' || c
== 'M') && seq
[i
+ 1] == '-') ||
4882 /* Handle special case of backwards define. */
4883 if (strncmp (&seq
[i
], "C-\\M-", 5) == 0)
4887 array
[l
++] = CTRL (to_upper (seq
[i
]));
4902 array
[l
++] = CTRL (to_upper (seq
[i
]));
4920 /* Return a pointer to the function that STRING represents.
4921 If STRING doesn't have a matching function, then a NULL pointer
4924 rl_named_function (string
)
4928 static int stricmp ();
4930 for (i
= 0; funmap
[i
]; i
++)
4931 if (stricmp (funmap
[i
]->name
, string
) == 0)
4932 return (funmap
[i
]->function
);
4933 return ((Function
*)NULL
);
4936 /* The last key bindings file read. */
4937 static char *last_readline_init_file
= "~/.inputrc";
4939 /* Re-read the current keybindings file. */
4940 rl_re_read_init_file (count
, ignore
)
4943 rl_read_init_file (last_readline_init_file
);
4946 /* Do key bindings from a file. If FILENAME is NULL it defaults
4947 to `~/.inputrc'. If the file existed and could be opened and
4948 read, 0 is returned, otherwise errno is returned. */
4950 rl_read_init_file (filename
)
4954 int line_size
, line_index
;
4955 char *line
= (char *)xmalloc (line_size
= 100);
4961 /* Default the filename. */
4963 filename
= "~/.inputrc";
4965 openname
= tilde_expand (filename
);
4967 /* Open the file. */
4968 file
= fopen (openname
, "r");
4974 last_readline_init_file
= filename
;
4976 /* Loop reading lines from the file. Lines that start with `#' are
4977 comments, all other lines are commands for readline initialization. */
4978 while ((c
= rl_getc (file
)) != EOF
)
4980 /* If comment, flush to EOL. */
4983 while ((c
= rl_getc (file
)) != EOF
&& c
!= '\n');
4989 /* Otherwise, this is the start of a line. Read the
4990 line from the file. */
4992 while (c
!= EOF
&& c
!= '\n')
4994 line
[line_index
++] = c
;
4995 if (line_index
== line_size
)
4996 line
= (char *)xrealloc (line
, line_size
+= 100);
4999 line
[line_index
] = '\0';
5001 /* Parse the line. */
5002 rl_parse_and_bind (line
);
5008 /* Close up the file and exit. */
5014 /* **************************************************************** */
5016 /* Parser Directives */
5018 /* **************************************************************** */
5022 /* Calling programs set this to have their argv[0]. */
5023 char *rl_readline_name
= "other";
5025 /* Stack of previous values of parsing_conditionalized_out. */
5026 static unsigned char *if_stack
= (unsigned char *)NULL
;
5027 static int if_stack_depth
= 0;
5028 static int if_stack_size
= 0;
5030 /* Push parsing_conditionalized_out, and set parser state based on ARGS. */
5035 static int stricmp ();
5037 /* Push parser state. */
5038 if (if_stack_depth
+ 1 >= if_stack_size
)
5041 if_stack
= (unsigned char *)xmalloc (if_stack_size
= 20);
5043 if_stack
= (unsigned char *)xrealloc (if_stack
, if_stack_size
+= 20);
5045 if_stack
[if_stack_depth
++] = parsing_conditionalized_out
;
5047 /* We only check to see if the first word in ARGS is the same as the
5048 value stored in rl_readline_name. */
5050 /* Isolate first argument. */
5051 for (i
= 0; args
[i
] && !whitespace (args
[i
]); i
++);
5056 if (stricmp (args
, rl_readline_name
) == 0)
5057 parsing_conditionalized_out
= 0;
5059 parsing_conditionalized_out
= 1;
5062 /* Invert the current parser state if there is anything on the stack. */
5067 parsing_conditionalized_out
= !parsing_conditionalized_out
;
5070 /* *** What, no error message? *** */
5074 /* Terminate a conditional, popping the value of
5075 parsing_conditionalized_out from the stack. */
5080 parsing_conditionalized_out
= if_stack
[--if_stack_depth
];
5083 /* *** What, no error message? *** */
5087 /* Associate textual names with actual functions. */
5091 } parser_directives
[] = {
5092 { "if", parser_if
},
5093 { "endif", parser_endif
},
5094 { "else", parser_else
},
5095 { (char *)0x0, (Function
*)0x0 }
5098 /* Handle a parser directive. STATEMENT is the line of the directive
5099 without any leading `$'. */
5101 handle_parser_directive (statement
)
5105 char *directive
, *args
;
5106 static int stricmp ();
5108 /* Isolate the actual directive. */
5110 /* Skip whitespace. */
5111 for (i
= 0; whitespace (statement
[i
]); i
++);
5113 directive
= &statement
[i
];
5115 for (; statement
[i
] && !whitespace (statement
[i
]); i
++);
5118 statement
[i
++] = '\0';
5120 for (; statement
[i
] && whitespace (statement
[i
]); i
++);
5122 args
= &statement
[i
];
5124 /* Lookup the command, and act on it. */
5125 for (i
= 0; parser_directives
[i
].name
; i
++)
5126 if (stricmp (directive
, parser_directives
[i
].name
) == 0)
5128 (*parser_directives
[i
].function
) (args
);
5132 /* *** Should an error message be output? */
5136 /* Read the binding command from STRING and perform it.
5137 A key binding command looks like: Keyname: function-name\0,
5138 a variable binding command looks like: set variable value.
5139 A new-style keybinding looks like "\C-x\C-x": exchange-point-and-mark. */
5140 rl_parse_and_bind (string
)
5143 extern char *possible_control_prefixes
[], *possible_meta_prefixes
[];
5144 char *rindex (), *funname
, *kname
;
5145 static int substring_member_of_array (), stricmp ();
5149 if (!string
|| !*string
|| *string
== '#')
5152 /* If this is a parser directive, act on it. */
5155 handle_parser_directive (&string
[1]);
5159 /* If we are supposed to be skipping parsing right now, then do it. */
5160 if (parsing_conditionalized_out
)
5164 /* If this keyname is a complex key expression surrounded by quotes,
5165 advance to after the matching close quote. */
5168 for (i
= 1; c
= string
[i
]; i
++)
5170 if (c
== '"' && string
[i
- 1] != '\\')
5175 /* Advance to the colon (:) or whitespace which separates the two objects. */
5176 for (; (c
= string
[i
]) && c
!= ':' && c
!= ' ' && c
!= '\t'; i
++ );
5178 /* Mark the end of the command (or keyname). */
5182 /* If this is a command to set a variable, then do that. */
5183 if (stricmp (string
, "set") == 0)
5185 char *var
= string
+ i
;
5188 /* Make VAR point to start of variable name. */
5189 while (*var
&& whitespace (*var
)) var
++;
5191 /* Make value point to start of value string. */
5193 while (*value
&& !whitespace (*value
)) value
++;
5196 while (*value
&& whitespace (*value
)) value
++;
5198 rl_variable_bind (var
, value
);
5202 /* Skip any whitespace between keyname and funname. */
5203 for (; string
[i
] && whitespace (string
[i
]); i
++);
5204 funname
= &string
[i
];
5206 /* Now isolate funname.
5207 For straight function names just look for whitespace, since
5208 that will signify the end of the string. But this could be a
5209 macro definition. In that case, the string is quoted, so skip
5210 to the matching delimiter. */
5211 if (*funname
== '\'' || *funname
== '"')
5213 int delimiter
= string
[i
++];
5215 for (; c
= string
[i
]; i
++)
5217 if (c
== delimiter
&& string
[i
- 1] != '\\')
5224 /* Advance to the end of the string. */
5225 for (; string
[i
] && !whitespace (string
[i
]); i
++);
5227 /* No extra whitespace at the end of the string. */
5230 /* If this is a new-style key-binding, then do the binding with
5231 rl_set_key (). Otherwise, let the older code deal with it. */
5234 char *seq
= (char *)alloca (1 + strlen (string
));
5235 register int j
, k
= 0;
5237 for (j
= 1; string
[j
]; j
++)
5239 if (string
[j
] == '"' && string
[j
- 1] != '\\')
5242 seq
[k
++] = string
[j
];
5246 /* Binding macro? */
5247 if (*funname
== '\'' || *funname
== '"')
5249 j
= strlen (funname
);
5251 if (j
&& funname
[j
- 1] == *funname
)
5252 funname
[j
- 1] = '\0';
5254 rl_macro_bind (seq
, &funname
[1], keymap
);
5257 rl_set_key (seq
, rl_named_function (funname
), keymap
);
5262 /* Get the actual character we want to deal with. */
5263 kname
= rindex (string
, '-');
5269 key
= glean_key_from_name (kname
);
5271 /* Add in control and meta bits. */
5272 if (substring_member_of_array (string
, possible_control_prefixes
))
5273 key
= CTRL (to_upper (key
));
5275 if (substring_member_of_array (string
, possible_meta_prefixes
))
5278 /* Temporary. Handle old-style keyname with macro-binding. */
5279 if (*funname
== '\'' || *funname
== '"')
5282 int fl
= strlen (funname
);
5284 seq
[0] = key
; seq
[1] = '\0';
5285 if (fl
&& funname
[fl
- 1] == *funname
)
5286 funname
[fl
- 1] = '\0';
5288 rl_macro_bind (seq
, &funname
[1], keymap
);
5291 rl_bind_key (key
, rl_named_function (funname
));
5294 rl_variable_bind (name
, value
)
5297 static int strnicmp (), stricmp ();
5299 if (stricmp (name
, "editing-mode") == 0)
5301 if (strnicmp (value
, "vi", 2) == 0)
5304 keymap
= vi_insertion_keymap
;
5305 rl_editing_mode
= vi_mode
;
5306 #endif /* VI_MODE */
5308 else if (strnicmp (value
, "emacs", 5) == 0)
5310 keymap
= emacs_standard_keymap
;
5311 rl_editing_mode
= emacs_mode
;
5314 else if (stricmp (name
, "horizontal-scroll-mode") == 0)
5316 if (!*value
|| stricmp (value
, "On") == 0)
5317 horizontal_scroll_mode
= 1;
5319 horizontal_scroll_mode
= 0;
5323 /* Return the character which matches NAME.
5324 For example, `Space' returns ' '. */
5331 assoc_list name_key_alist
[] = {
5337 { "Newline", '\n' },
5341 { "Escape", '\033' },
5348 glean_key_from_name (name
)
5352 static int stricmp ();
5354 for (i
= 0; name_key_alist
[i
].name
; i
++)
5355 if (stricmp (name
, name_key_alist
[i
].name
) == 0)
5356 return (name_key_alist
[i
].value
);
5362 /* **************************************************************** */
5364 /* String Utility Functions */
5366 /* **************************************************************** */
5368 /* Return non-zero if any members of ARRAY are a substring in STRING. */
5370 substring_member_of_array (string
, array
)
5371 char *string
, **array
;
5373 static char *strindex ();
5377 if (strindex (string
, *array
))
5384 /* Whoops, Unix doesn't have strnicmp. */
5386 /* Compare at most COUNT characters from string1 to string2. Case
5389 strnicmp (string1
, string2
, count
)
5390 char *string1
, *string2
;
5392 register char ch1
, ch2
;
5397 if (to_upper(ch1
) == to_upper(ch2
))
5404 /* strcmp (), but caseless. */
5406 stricmp (string1
, string2
)
5407 char *string1
, *string2
;
5409 register char ch1
, ch2
;
5411 while (*string1
&& *string2
) {
5414 if (to_upper(ch1
) != to_upper(ch2
))
5417 return (*string1
| *string2
);
5420 /* Determine if s2 occurs in s1. If so, return a pointer to the
5421 match in s1. The compare is case insensitive. */
5424 register char *s1
, *s2
;
5426 register int i
, l
= strlen (s2
);
5427 register int len
= strlen (s1
);
5429 for (i
= 0; (len
- i
) >= l
; i
++)
5430 if (strnicmp (&s1
[i
], s2
, l
) == 0)
5432 return ((char *)NULL
);
5436 /* **************************************************************** */
5440 /* **************************************************************** */
5442 /* Since system V reads input differently than we do, I have to
5443 make a special version of getc for that. */
5448 #include <sys/errno.h>
5459 result
= read (fileno (stream
), &c
, sizeof (char));
5460 if (result
== sizeof (char))
5472 return (getc (stream
));
5476 #ifdef STATIC_MALLOC
5478 /* **************************************************************** */
5480 /* xmalloc and xrealloc () */
5482 /* **************************************************************** */
5488 static memory_error_and_abort ();
5489 char *temp
= (char *)malloc (bytes
);
5492 memory_error_and_abort ();
5497 xrealloc (pointer
, bytes
)
5501 static memory_error_and_abort ();
5502 char *temp
= (char *)realloc (pointer
, bytes
);
5505 memory_error_and_abort ();
5510 memory_error_and_abort ()
5512 fprintf (stderr
, "readline: Out of virtual memory!\n");
5515 #endif /* STATIC_MALLOC */
5518 /* **************************************************************** */
5520 /* Testing Readline */
5522 /* **************************************************************** */
5528 HIST_ENTRY
**history_list ();
5529 char *temp
= (char *)NULL
;
5530 char *prompt
= "readline% ";
5535 temp
= readline (prompt
);
5541 /* If there is anything on the line, print it and remember it. */
5544 fprintf (stderr
, "%s\r\n", temp
);
5548 /* Check for `command' that we handle. */
5549 if (strcmp (temp
, "quit") == 0)
5552 if (strcmp (temp
, "list") == 0) {
5553 HIST_ENTRY
**list
= history_list ();
5556 for (i
= 0; list
[i
]; i
++) {
5557 fprintf (stderr
, "%d: %s\r\n", i
, list
[i
]->line
);
5558 free (list
[i
]->line
);
5572 * compile-command: "gcc -g -traditional -I. -I.. -DTEST -o readline readline.c keymaps.o funmap.o history.o -ltermcap"