1 /* readline.c -- a general facility for reading lines of input
2 with emacs style editing and completion. */
3
4 /* Copyright (C) 1987,1989 Free Software Foundation, Inc.
5
6 This file contains the Readline Library (the Library), a set of
7 routines for providing Emacs style line input to programs that ask
8 for it.
9
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)
13 any later version.
14
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.
19
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. */
24
25 /* Remove these declarations when we have a complete libgnu.a. */
26 #define STATIC_MALLOC
27 #ifndef STATIC_MALLOC
28 extern char *xmalloc (), *xrealloc ();
29 #else
30 static char *xmalloc (), *xrealloc ();
31 #endif
32
33 #include <stdio.h>
34 #include <sys/types.h>
35 #include <fcntl.h>
36 #include <sys/file.h>
37 #include <signal.h>
38
39 #ifdef __GNUC__
40 #define alloca __builtin_alloca
41 #else
42 #if defined (sparc) && defined (sun)
43 #include <alloca.h>
44 #endif
45 #endif
46
47 #if defined (HAVE_UNISTD_H)
48 #include <unistd.h>
49 #endif
50
51 #define NEW_TTY_DRIVER
52 #define HAVE_BSD_SIGNALS
53
54 /* Some USG machines have BSD signal handling (sigblock, sigsetmask, etc.) */
55 #if defined (USG) && !defined (hpux)
56 #undef HAVE_BSD_SIGNALS
57 #endif
58
59 /* System V machines use termio. */
60 #if !defined (_POSIX_VERSION)
61 #if defined (USG) || defined (hpux) || defined (Xenix) || defined (sgi) || defined (DGUX)
62 #undef NEW_TTY_DRIVER
63 #include <termio.h>
64
65 #if !defined (TCOON)
66 #define TCOON 1
67 #endif
68
69 #endif /* USG | hpux | Xenix sgi | DUGX */
70 #endif /* !_POSIX_VERSION */
71
72 /* Posix systems use termios. */
73 #if defined (_POSIX_VERSION)
74 #undef NEW_TTY_DRIVER
75 #include <termios.h>
76 #if !defined (O_NDELAY)
77 #define O_NDELAY O_NONBLOCK /* Posix-style non-blocking i/o */
78 #endif /* O_NDELAY */
79 #endif
80
81 /* Other (BSD) machines use sgtty. */
82 #if defined (NEW_TTY_DRIVER)
83 #include <sgtty.h>
84 #endif
85
86 #include <errno.h>
87 extern int errno;
88
89 #include <setjmp.h>
90 #include <sys/stat.h>
91
92 /* These next are for filename completion. Perhaps this belongs
93 in a different place. */
94 #include <pwd.h>
95 #if defined (USG)
96 struct passwd *getpwuid (), *getpwent ();
97 #endif
98
99 /* #define HACK_TERMCAP_MOTION */
100
101 #if !defined (USG)
102 #include <sys/dir.h>
103 #else /* USG */
104 #if defined (Xenix)
105 #include <sys/ndir.h>
106 #else
107 #ifdef hpux
108 #include <ndir.h>
109 #else
110 #include <dirent.h>
111 #define direct dirent
112 #define d_namlen d_reclen
113 #endif /* hpux */
114 #endif /* xenix */
115 #endif /* USG */
116
117 #if defined (USG) && defined (TIOCGWINSZ)
118 #include <sys/stream.h>
119 # if defined (USGr4) || defined (USGr3)
120 # include <sys/ptem.h>
121 # endif /* USGr4 */
122 #endif /* USG && TIOCGWINSZ */
123
124 /* Some standard library routines. */
125 #include "readline.h"
126 #include "history.h"
127
128 #ifndef digit
129 #define digit(c) ((c) >= '0' && (c) <= '9')
130 #endif
131
132 #ifndef isletter
133 #define isletter(c) (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z'))
134 #endif
135
136 #ifndef digit_value
137 #define digit_value(c) ((c) - '0')
138 #endif
139
140 #ifndef member
141 char *index ();
142 #define member(c, s) ((c) ? index ((s), (c)) : 0)
143 #endif
144
145 #ifndef isident
146 #define isident(c) ((isletter(c) || digit(c) || c == '_'))
147 #endif
148
149 #ifndef exchange
150 #define exchange(x, y) {int temp = x; x = y; y = temp;}
151 #endif
152
153 static update_line ();
154 static void output_character_function ();
155 static delete_chars ();
156 static insert_some_chars ();
157
158 #ifdef VOID_SIGHANDLER
159 #define sighandler void
160 #else
161 #define sighandler int
162 #endif
163
164 /* This typedef is equivalant to the one for Function; it allows us
165 to say SigHandler *foo = signal (SIGKILL, SIG_IGN); */
166 typedef sighandler SigHandler ();
167
168 /* If on, then readline handles signals in a way that doesn't screw. */
169 #define HANDLE_SIGNALS
170
171
172 /* **************************************************************** */
173 /* */
174 /* Line editing input utility */
175 /* */
176 /* **************************************************************** */
177
178 /* A pointer to the keymap that is currently in use.
179 By default, it is the standard emacs keymap. */
180 Keymap keymap = emacs_standard_keymap;
181
182 #define vi_mode 0
183 #define emacs_mode 1
184
185 /* The current style of editing. */
186 int rl_editing_mode = emacs_mode;
187
188 /* Non-zero if the previous command was a kill command. */
189 static int last_command_was_kill = 0;
190
191 /* The current value of the numeric argument specified by the user. */
192 int rl_numeric_arg = 1;
193
194 /* Non-zero if an argument was typed. */
195 int rl_explicit_arg = 0;
196
197 /* Temporary value used while generating the argument. */
198 static int arg_sign = 1;
199
200 /* Non-zero means we have been called at least once before. */
201 static int rl_initialized = 0;
202
203 /* If non-zero, this program is running in an EMACS buffer. */
204 static char *running_in_emacs = (char *)NULL;
205
206 /* The current offset in the current input line. */
207 int rl_point;
208
209 /* Mark in the current input line. */
210 int rl_mark;
211
212 /* Length of the current input line. */
213 int rl_end;
214
215 /* Make this non-zero to return the current input_line. */
216 int rl_done;
217
218 /* The last function executed by readline. */
219 Function *rl_last_func = (Function *)NULL;
220
221 /* Top level environment for readline_internal (). */
222 static jmp_buf readline_top_level;
223
224 /* The streams we interact with. */
225 static FILE *in_stream, *out_stream;
226
227 /* The names of the streams that we do input and output to. */
228 FILE *rl_instream = stdin, *rl_outstream = stdout;
229
230 /* Non-zero means echo characters as they are read. */
231 int readline_echoing_p = 1;
232
233 /* Current prompt. */
234 char *rl_prompt;
235
236 /* The number of characters read in order to type this complete command. */
237 int rl_key_sequence_length = 0;
238
239 /* If non-zero, then this is the address of a function to call just
240 before readline_internal () prints the first prompt. */
241 Function *rl_startup_hook = (Function *)NULL;
242
243 /* If non-zero, then this is the address of a function to call when
244 completing on a directory name. The function is called with
245 the address of a string (the current directory name) as an arg. */
246 Function *rl_symbolic_link_hook = (Function *)NULL;
247
248 /* What we use internally. You should always refer to RL_LINE_BUFFER. */
249 static char *the_line;
250
251 /* The character that can generate an EOF. Really read from
252 the terminal driver... just defaulted here. */
253 static int eof_char = CTRL ('D');
254
255 /* Non-zero makes this the next keystroke to read. */
256 int rl_pending_input = 0;
257
258 /* Pointer to a useful terminal name. */
259 char *rl_terminal_name = (char *)NULL;
260
261 /* Line buffer and maintenence. */
262 char *rl_line_buffer = (char *)NULL;
263 static int rl_line_buffer_len = 0;
264 #define DEFAULT_BUFFER_SIZE 256
265
266
267 /* **************************************************************** */
268 /* */
269 /* `Forward' declarations */
270 /* */
271 /* **************************************************************** */
272
273 /* Non-zero means do not parse any lines other than comments and
274 parser directives. */
275 static unsigned char parsing_conditionalized_out = 0;
276
277 /* Caseless strcmp (). */
278 static int stricmp (), strnicmp ();
279
280 /* Non-zero means to save keys that we dispatch on in a kbd macro. */
281 static int defining_kbd_macro = 0;
282
283
284 /* **************************************************************** */
285 /* */
286 /* Top Level Functions */
287 /* */
288 /* **************************************************************** */
289
290 /* Read a line of input. Prompt with PROMPT. A NULL PROMPT means
291 none. A return value of NULL means that EOF was encountered. */
292 char *
readline(prompt)293 readline (prompt)
294 char *prompt;
295 {
296 static rl_prep_terminal (), rl_deprep_terminal ();
297 char *readline_internal ();
298 char *value;
299
300 rl_prompt = prompt;
301
302 /* If we are at EOF return a NULL string. */
303 if (rl_pending_input == EOF)
304 {
305 rl_pending_input = 0;
306 return ((char *)NULL);
307 }
308
309 rl_initialize ();
310 rl_prep_terminal ();
311
312 #ifdef HANDLE_SIGNALS
313 rl_set_signals ();
314 #endif
315
316 value = readline_internal ();
317 rl_deprep_terminal ();
318
319 #ifdef HANDLE_SIGNALS
320 rl_clear_signals ();
321 #endif
322
323 return (value);
324 }
325
326 /* Read a line of input from the global rl_instream, doing output on
327 the global rl_outstream.
328 If rl_prompt is non-null, then that is our prompt. */
329 char *
readline_internal()330 readline_internal ()
331 {
332 int lastc, c, eof_found;
333
334 in_stream = rl_instream; out_stream = rl_outstream;
335 lastc = eof_found = 0;
336
337 if (rl_startup_hook)
338 (*rl_startup_hook) ();
339
340 if (!readline_echoing_p)
341 {
342 if (rl_prompt)
343 {
344 fprintf (out_stream, "%s", rl_prompt);
345 fflush (out_stream);
346 }
347 }
348 else
349 {
350 rl_on_new_line ();
351 rl_redisplay ();
352 #ifdef VI_MODE
353 if (rl_editing_mode == vi_mode)
354 rl_vi_insertion_mode ();
355 #endif /* VI_MODE */
356 }
357
358 while (!rl_done)
359 {
360 int lk = last_command_was_kill;
361 int code = setjmp (readline_top_level);
362
363 if (code)
364 rl_redisplay ();
365
366 if (!rl_pending_input)
367 {
368 /* Then initialize the argument and number of keys read. */
369 rl_init_argument ();
370 rl_key_sequence_length = 0;
371 }
372
373 c = rl_read_key ();
374
375 /* EOF typed to a non-blank line is a <NL>. */
376 if (c == EOF && rl_end)
377 c = NEWLINE;
378
379 /* The character eof_char typed to blank line, and not as the
380 previous character is interpreted as EOF. */
381 if (((c == eof_char && lastc != c) || c == EOF) && !rl_end)
382 {
383 eof_found = 1;
384 break;
385 }
386
387 lastc = c;
388 rl_dispatch (c, keymap);
389
390 /* If there was no change in last_command_was_kill, then no kill
391 has taken place. Note that if input is pending we are reading
392 a prefix command, so nothing has changed yet. */
393 if (!rl_pending_input)
394 {
395 if (lk == last_command_was_kill)
396 last_command_was_kill = 0;
397 }
398
399 #ifdef VI_MODE
400 /* In vi mode, when you exit insert mode, the cursor moves back
401 over the previous character. We explicitly check for that here. */
402 if (rl_editing_mode == vi_mode && keymap == vi_movement_keymap)
403 rl_vi_check ();
404 #endif
405
406 if (!rl_done)
407 rl_redisplay ();
408 }
409
410 /* Restore the original of this history line, iff the line that we
411 are editing was originally in the history, AND the line has changed. */
412 {
413 HIST_ENTRY *entry = current_history ();
414
415 if (entry && rl_undo_list)
416 {
417 char *temp = savestring (the_line);
418 rl_revert_line ();
419 entry = replace_history_entry (where_history (), the_line,
420 (HIST_ENTRY *)NULL);
421 free_history_entry (entry);
422
423 strcpy (the_line, temp);
424 free (temp);
425 }
426 }
427
428 /* At any rate, it is highly likely that this line has an undo list. Get
429 rid of it now. */
430 if (rl_undo_list)
431 free_undo_list ();
432
433 if (eof_found)
434 return (char *)NULL;
435 else
436 return (savestring (the_line));
437 }
438
439
440 /* **************************************************************** */
441 /* */
442 /* Signal Handling */
443 /* */
444 /* **************************************************************** */
445
446 #ifdef SIGWINCH
447 static SigHandler *old_sigwinch = (SigHandler *)NULL;
448
449 static sighandler
rl_handle_sigwinch(sig,code,scp)450 rl_handle_sigwinch (sig, code, scp)
451 int sig, code;
452 struct sigcontext *scp;
453 {
454 char *term = rl_terminal_name, *getenv ();
455
456 if (readline_echoing_p)
457 {
458 if (!term)
459 term = getenv ("TERM");
460 if (!term)
461 term = "dumb";
462 rl_reset_terminal (term);
463 #ifdef NEVER
464 crlf ();
465 rl_forced_update_display ();
466 #endif
467 }
468
469 if (old_sigwinch &&
470 old_sigwinch != (SigHandler *)SIG_IGN &&
471 old_sigwinch != (SigHandler *)SIG_DFL)
472 (*old_sigwinch)(sig, code, scp);
473 }
474 #endif /* SIGWINCH */
475
476 #ifdef HANDLE_SIGNALS
477 /* Interrupt handling. */
478 static SigHandler *old_int = (SigHandler *)NULL,
479 *old_tstp = (SigHandler *)NULL,
480 *old_ttou = (SigHandler *)NULL,
481 *old_ttin = (SigHandler *)NULL,
482 *old_cont = (SigHandler *)NULL;
483
484 /* Handle an interrupt character. */
485 static sighandler
rl_signal_handler(sig,code,scp)486 rl_signal_handler (sig, code, scp)
487 int sig, code;
488 struct sigcontext *scp;
489 {
490 static rl_prep_terminal (), rl_deprep_terminal ();
491
492 #if !defined (HAVE_BSD_SIGNALS) || defined (hpux)
493 /* Since the signal will not be blocked while we are in the signal
494 handler, ignore it until rl_clear_signals resets the catcher. */
495 if (sig == SIGINT)
496 signal (sig, SIG_IGN);
497 #endif /* !HAVE_BSD_SIGNALS || hpux */
498
499 switch (sig)
500 {
501 case SIGINT:
502 free_undo_list ();
503 rl_clear_message ();
504 rl_init_argument ();
505
506 #ifdef SIGTSTP
507 case SIGTSTP:
508 case SIGTTOU:
509 case SIGTTIN:
510 #endif
511
512 rl_clean_up_for_exit ();
513 rl_deprep_terminal ();
514 rl_clear_signals ();
515 rl_pending_input = 0;
516
517 kill (getpid (), sig);
518
519 #if defined (_POSIX_VERSION)
520 {
521 sigset_t set;
522
523 sigemptyset (&set);
524 sigprocmask (SIG_SETMASK, &set, (sigset_t *)NULL);
525 }
526 #else
527 #if defined (HAVE_BSD_SIGNALS)
528 sigsetmask (0);
529 #endif /* HAVE_BSD_SIGNALS */
530 #endif /* _POSIX_VERSION */
531
532 rl_prep_terminal ();
533 rl_set_signals ();
534 }
535 }
536
rl_set_signals()537 rl_set_signals ()
538 {
539 old_int = (SigHandler *)signal (SIGINT, rl_signal_handler);
540 if (old_int == (SigHandler *)SIG_IGN)
541 signal (SIGINT, SIG_IGN);
542
543 #ifdef SIGTSTP
544 old_tstp = (SigHandler *)signal (SIGTSTP, rl_signal_handler);
545 if (old_tstp == (SigHandler *)SIG_IGN)
546 signal (SIGTSTP, SIG_IGN);
547 #endif
548 #ifdef SIGTTOU
549 old_ttou = (SigHandler *)signal (SIGTTOU, rl_signal_handler);
550 old_ttin = (SigHandler *)signal (SIGTTIN, rl_signal_handler);
551
552 if (old_tstp == (SigHandler *)SIG_IGN)
553 {
554 signal (SIGTTOU, SIG_IGN);
555 signal (SIGTTIN, SIG_IGN);
556 }
557 #endif
558
559 #ifdef SIGWINCH
560 old_sigwinch = (SigHandler *)signal (SIGWINCH, rl_handle_sigwinch);
561 #endif
562 }
563
rl_clear_signals()564 rl_clear_signals ()
565 {
566 signal (SIGINT, old_int);
567
568 #ifdef SIGTSTP
569 signal (SIGTSTP, old_tstp);
570 #endif
571
572 #ifdef SIGTTOU
573 signal (SIGTTOU, old_ttou);
574 signal (SIGTTIN, old_ttin);
575 #endif
576
577 #ifdef SIGWINCH
578 signal (SIGWINCH, old_sigwinch);
579 #endif
580 }
581 #endif /* HANDLE_SIGNALS */
582
583
584 /* **************************************************************** */
585 /* */
586 /* Character Input Buffering */
587 /* */
588 /* **************************************************************** */
589
590 /* If the terminal was in xoff state when we got to it, then xon_char
591 contains the character that is supposed to start it again. */
592 static int xon_char, xoff_state;
593 static int pop_index = 0, push_index = 0, ibuffer_len = 511;
594 static unsigned char ibuffer[512];
595
596 /* Non-null means it is a pointer to a function to run while waiting for
597 character input. */
598 Function *rl_event_hook = (Function *)NULL;
599
600 #define any_typein (push_index != pop_index)
601
602 /* Add KEY to the buffer of characters to be read. */
rl_stuff_char(key)603 rl_stuff_char (key)
604 int key;
605 {
606 if (key == EOF)
607 {
608 key = NEWLINE;
609 rl_pending_input = EOF;
610 }
611 ibuffer[push_index++] = key;
612 if (push_index >= ibuffer_len)
613 push_index = 0;
614 }
615
616 /* Return the amount of space available in the
617 buffer for stuffing characters. */
618 int
ibuffer_space()619 ibuffer_space ()
620 {
621 if (pop_index > push_index)
622 return (pop_index - push_index);
623 else
624 return (ibuffer_len - (push_index - pop_index));
625 }
626
627 /* Get a key from the buffer of characters to be read.
628 Return the key in KEY.
629 Result is KEY if there was a key, or 0 if there wasn't. */
630 int
rl_get_char(key)631 rl_get_char (key)
632 int *key;
633 {
634 if (push_index == pop_index)
635 return (0);
636
637 *key = ibuffer[pop_index++];
638
639 if (pop_index >= ibuffer_len)
640 pop_index = 0;
641
642 return (1);
643 }
644
645 /* Stuff KEY into the *front* of the input buffer.
646 Returns non-zero if successful, zero if there is
647 no space left in the buffer. */
648 int
rl_unget_char(key)649 rl_unget_char (key)
650 int key;
651 {
652 if (ibuffer_space ())
653 {
654 pop_index--;
655 if (pop_index < 0)
656 pop_index = ibuffer_len - 1;
657 ibuffer[pop_index] = key;
658 return (1);
659 }
660 return (0);
661 }
662
663 /* If a character is available to be read, then read it
664 and stuff it into IBUFFER. Otherwise, just return. */
rl_gather_tyi()665 rl_gather_tyi ()
666 {
667 int tty = fileno (in_stream);
668 register int tem, result = -1;
669 long chars_avail;
670 char input;
671
672 #ifdef FIONREAD
673 result = ioctl (tty, FIONREAD, &chars_avail);
674 #endif
675
676 if (result == -1)
677 {
678 fcntl (tty, F_SETFL, O_NDELAY);
679 chars_avail = read (tty, &input, 1);
680 fcntl (tty, F_SETFL, 0);
681 if (chars_avail == -1 && errno == EAGAIN)
682 return;
683 }
684
685 /* If there's nothing available, don't waste time trying to read
686 something. */
687 if (chars_avail == 0)
688 return;
689
690 tem = ibuffer_space ();
691
692 if (chars_avail > tem)
693 chars_avail = tem;
694
695 /* One cannot read all of the available input. I can only read a single
696 character at a time, or else programs which require input can be
697 thwarted. If the buffer is larger than one character, I lose.
698 Damn! */
699 if (tem < ibuffer_len)
700 chars_avail = 0;
701
702 if (result != -1)
703 {
704 while (chars_avail--)
705 rl_stuff_char (rl_getc (in_stream));
706 }
707 else
708 {
709 if (chars_avail)
710 rl_stuff_char (input);
711 }
712 }
713
714 /* Read a key, including pending input. */
715 int
rl_read_key()716 rl_read_key ()
717 {
718 int c;
719
720 rl_key_sequence_length++;
721
722 if (rl_pending_input)
723 {
724 c = rl_pending_input;
725 rl_pending_input = 0;
726 }
727 else
728 {
729 static int next_macro_key ();
730
731 /* If input is coming from a macro, then use that. */
732 if (c = next_macro_key ())
733 return (c);
734
735 /* If the user has an event function, then call it periodically. */
736 if (rl_event_hook)
737 {
738 while (rl_event_hook && !rl_get_char (&c))
739 {
740 (*rl_event_hook) ();
741 rl_gather_tyi ();
742 }
743 }
744 else
745 {
746 if (!rl_get_char (&c))
747 c = rl_getc (in_stream);
748 }
749 }
750
751 #ifdef NEVER /* This breaks supdup to 4.0.3c machines. */
752 #ifdef TIOCSTART
753 /* Ugh. But I can't think of a better way. */
754 if (xoff_state && c == xon_char)
755 {
756 ioctl (fileno (in_stream), TIOCSTART, 0);
757 xoff_state = 0;
758 return (rl_read_key ());
759 }
760 #endif /* TIOCSTART */
761 #endif
762
763 return (c);
764 }
765
766 /* I'm beginning to hate the declaration rules for various compilers. */
767 static void add_macro_char ();
768
769 /* Do the command associated with KEY in MAP.
770 If the associated command is really a keymap, then read
771 another key, and dispatch into that map. */
rl_dispatch(key,map)772 rl_dispatch (key, map)
773 register int key;
774 Keymap map;
775 {
776
777 if (defining_kbd_macro)
778 add_macro_char (key);
779
780 if (key > 127 && key < 256)
781 {
782 if (map[ESC].type == ISKMAP)
783 {
784 map = (Keymap)map[ESC].function;
785 key -= 128;
786 rl_dispatch (key, map);
787 }
788 else
789 ding ();
790 return;
791 }
792
793 switch (map[key].type)
794 {
795 case ISFUNC:
796 {
797 Function *func = map[key].function;
798
799 if (func != (Function *)NULL)
800 {
801 /* Special case rl_do_lowercase_version (). */
802 if (func == rl_do_lowercase_version)
803 {
804 rl_dispatch (to_lower (key), map);
805 return;
806 }
807
808 (*map[key].function)(rl_numeric_arg * arg_sign, key);
809
810 /* If we have input pending, then the last command was a prefix
811 command. Don't change the state of rl_last_func. Otherwise,
812 remember the last command executed in this variable. */
813 if (!rl_pending_input)
814 rl_last_func = map[key].function;
815 }
816 else
817 {
818 rl_abort ();
819 return;
820 }
821 }
822 break;
823
824 case ISKMAP:
825 if (map[key].function != (Function *)NULL)
826 {
827 int newkey;
828
829 rl_key_sequence_length++;
830 newkey = rl_read_key ();
831 rl_dispatch (newkey, (Keymap)map[key].function);
832 }
833 else
834 {
835 rl_abort ();
836 return;
837 }
838 break;
839
840 case ISMACR:
841 if (map[key].function != (Function *)NULL)
842 {
843 static with_macro_input ();
844 char *macro = savestring ((char *)map[key].function);
845
846 with_macro_input (macro);
847 return;
848 }
849 break;
850 }
851 }
852
853
854 /* **************************************************************** */
855 /* */
856 /* Hacking Keyboard Macros */
857 /* */
858 /* **************************************************************** */
859
860 /* The currently executing macro string. If this is non-zero,
861 then it is a malloc ()'ed string where input is coming from. */
862 static char *executing_macro = (char *)NULL;
863
864 /* The offset in the above string to the next character to be read. */
865 static int executing_macro_index = 0;
866
867 /* The current macro string being built. Characters get stuffed
868 in here by add_macro_char (). */
869 static char *current_macro = (char *)NULL;
870
871 /* The size of the buffer allocated to current_macro. */
872 static int current_macro_size = 0;
873
874 /* The index at which characters are being added to current_macro. */
875 static int current_macro_index = 0;
876
877 /* A structure used to save nested macro strings.
878 It is a linked list of string/index for each saved macro. */
879 struct saved_macro {
880 struct saved_macro *next;
881 char *string;
882 int index;
883 };
884
885 /* The list of saved macros. */
886 struct saved_macro *macro_list = (struct saved_macro *)NULL;
887
888 /* Forward declarations of static functions. Thank you C. */
889 static void push_executing_macro (), pop_executing_macro ();
890
891 /* This one has to be declared earlier in the file. */
892 /* static void add_macro_char (); */
893
894 /* Set up to read subsequent input from STRING.
895 STRING is free ()'ed when we are done with it. */
896 static
with_macro_input(string)897 with_macro_input (string)
898 char *string;
899 {
900 push_executing_macro ();
901 executing_macro = string;
902 executing_macro_index = 0;
903 }
904
905 /* Return the next character available from a macro, or 0 if
906 there are no macro characters. */
907 static int
next_macro_key()908 next_macro_key ()
909 {
910 if (!executing_macro)
911 return (0);
912
913 if (!executing_macro[executing_macro_index])
914 {
915 pop_executing_macro ();
916 return (next_macro_key ());
917 }
918
919 return (executing_macro[executing_macro_index++]);
920 }
921
922 /* Save the currently executing macro on a stack of saved macros. */
923 static void
push_executing_macro()924 push_executing_macro ()
925 {
926 struct saved_macro *saver;
927
928 saver = (struct saved_macro *)xmalloc (sizeof (struct saved_macro));
929 saver->next = macro_list;
930 saver->index = executing_macro_index;
931 saver->string = executing_macro;
932
933 macro_list = saver;
934 }
935
936 /* Discard the current macro, replacing it with the one
937 on the top of the stack of saved macros. */
938 static void
pop_executing_macro()939 pop_executing_macro ()
940 {
941 if (executing_macro)
942 free (executing_macro);
943
944 executing_macro = (char *)NULL;
945 executing_macro_index = 0;
946
947 if (macro_list)
948 {
949 struct saved_macro *disposer = macro_list;
950 executing_macro = macro_list->string;
951 executing_macro_index = macro_list->index;
952 macro_list = macro_list->next;
953 free (disposer);
954 }
955 }
956
957 /* Add a character to the macro being built. */
958 static void
add_macro_char(c)959 add_macro_char (c)
960 int c;
961 {
962 if (current_macro_index + 1 >= current_macro_size)
963 {
964 if (!current_macro)
965 current_macro = (char *)xmalloc (current_macro_size = 25);
966 else
967 current_macro =
968 (char *)xrealloc (current_macro, current_macro_size += 25);
969 }
970
971 current_macro[current_macro_index++] = c;
972 current_macro[current_macro_index] = '\0';
973 }
974
975 /* Begin defining a keyboard macro.
976 Keystrokes are recorded as they are executed.
977 End the definition with rl_end_kbd_macro ().
978 If a numeric argument was explicitly typed, then append this
979 definition to the end of the existing macro, and start by
980 re-executing the existing macro. */
rl_start_kbd_macro(ignore1,ignore2)981 rl_start_kbd_macro (ignore1, ignore2)
982 int ignore1, ignore2;
983 {
984 if (defining_kbd_macro)
985 rl_abort ();
986
987 if (rl_explicit_arg)
988 {
989 if (current_macro)
990 with_macro_input (savestring (current_macro));
991 }
992 else
993 current_macro_index = 0;
994
995 defining_kbd_macro = 1;
996 }
997
998 /* Stop defining a keyboard macro.
999 A numeric argument says to execute the macro right now,
1000 that many times, counting the definition as the first time. */
rl_end_kbd_macro(count,ignore)1001 rl_end_kbd_macro (count, ignore)
1002 int count, ignore;
1003 {
1004 if (!defining_kbd_macro)
1005 rl_abort ();
1006
1007 current_macro_index -= (rl_key_sequence_length - 1);
1008 current_macro[current_macro_index] = '\0';
1009
1010 defining_kbd_macro = 0;
1011
1012 rl_call_last_kbd_macro (--count, 0);
1013 }
1014
1015 /* Execute the most recently defined keyboard macro.
1016 COUNT says how many times to execute it. */
rl_call_last_kbd_macro(count,ignore)1017 rl_call_last_kbd_macro (count, ignore)
1018 int count, ignore;
1019 {
1020 if (!current_macro)
1021 rl_abort ();
1022
1023 while (count--)
1024 with_macro_input (savestring (current_macro));
1025 }
1026
1027
1028 /* **************************************************************** */
1029 /* */
1030 /* Initializations */
1031 /* */
1032 /* **************************************************************** */
1033
1034 /* Initliaze readline (and terminal if not already). */
rl_initialize()1035 rl_initialize ()
1036 {
1037 extern char *rl_display_prompt;
1038
1039 /* If we have never been called before, initialize the
1040 terminal and data structures. */
1041 if (!rl_initialized)
1042 {
1043 readline_initialize_everything ();
1044 rl_initialized++;
1045 }
1046
1047 /* Initalize the current line information. */
1048 rl_point = rl_end = 0;
1049 the_line = rl_line_buffer;
1050 the_line[0] = 0;
1051
1052 /* We aren't done yet. We haven't even gotten started yet! */
1053 rl_done = 0;
1054
1055 /* Tell the history routines what is going on. */
1056 start_using_history ();
1057
1058 /* Make the display buffer match the state of the line. */
1059 {
1060 extern char *rl_display_prompt;
1061 extern int forced_display;
1062
1063 rl_on_new_line ();
1064
1065 rl_display_prompt = rl_prompt ? rl_prompt : "";
1066 forced_display = 1;
1067 }
1068
1069 /* No such function typed yet. */
1070 rl_last_func = (Function *)NULL;
1071
1072 /* Parsing of key-bindings begins in an enabled state. */
1073 parsing_conditionalized_out = 0;
1074 }
1075
1076 /* Initialize the entire state of the world. */
readline_initialize_everything()1077 readline_initialize_everything ()
1078 {
1079 /* Find out if we are running in Emacs. */
1080 running_in_emacs = (char *)getenv ("EMACS");
1081
1082 /* Allocate data structures. */
1083 if (!rl_line_buffer)
1084 rl_line_buffer =
1085 (char *)xmalloc (rl_line_buffer_len = DEFAULT_BUFFER_SIZE);
1086
1087 /* Initialize the terminal interface. */
1088 init_terminal_io ((char *)NULL);
1089
1090 /* Bind tty characters to readline functions. */
1091 readline_default_bindings ();
1092
1093 /* Initialize the function names. */
1094 rl_initialize_funmap ();
1095
1096 /* Read in the init file. */
1097 rl_read_init_file ((char *)NULL);
1098
1099 /* If the completion parser's default word break characters haven't
1100 been set yet, then do so now. */
1101 {
1102 extern char *rl_completer_word_break_characters;
1103 extern char *rl_basic_word_break_characters;
1104
1105 if (rl_completer_word_break_characters == (char *)NULL)
1106 rl_completer_word_break_characters = rl_basic_word_break_characters;
1107 }
1108 }
1109
1110 /* If this system allows us to look at the values of the regular
1111 input editing characters, then bind them to their readline
1112 equivalents. */
readline_default_bindings()1113 readline_default_bindings ()
1114 {
1115
1116 #ifdef NEW_TTY_DRIVER
1117 struct sgttyb ttybuff;
1118 int tty = fileno (rl_instream);
1119
1120 if (ioctl (tty, TIOCGETP, &ttybuff) != -1)
1121 {
1122 int erase = ttybuff.sg_erase, kill = ttybuff.sg_kill;
1123
1124 if (erase != -1 && keymap[erase].type == ISFUNC)
1125 keymap[erase].function = rl_rubout;
1126
1127 if (kill != -1 && keymap[kill].type == ISFUNC)
1128 keymap[kill].function = rl_unix_line_discard;
1129 }
1130
1131 #ifdef TIOCGLTC
1132 {
1133 struct ltchars lt;
1134
1135 if (ioctl (tty, TIOCGLTC, <) != -1)
1136 {
1137 int erase = lt.t_werasc, nextc = lt.t_lnextc;
1138
1139 if (erase != -1 && keymap[erase].type == ISFUNC)
1140 keymap[erase].function = rl_unix_word_rubout;
1141
1142 if (nextc != -1 && keymap[nextc].type == ISFUNC)
1143 keymap[nextc].function = rl_quoted_insert;
1144 }
1145 }
1146 #endif /* TIOCGLTC */
1147 #else /* not NEW_TTY_DRIVER */
1148
1149 #if defined (_POSIX_VERSION)
1150 struct termios ttybuff;
1151 #else
1152 struct termio ttybuff;
1153 #endif /* POSIX */
1154 int tty = fileno (rl_instream);
1155
1156 #if defined (_POSIX_VERSION)
1157 if (tcgetattr (tty, &ttybuff) != -1)
1158 #else
1159 if (ioctl (tty, TCGETA, &ttybuff) != -1)
1160 #endif /* POSIX */
1161 {
1162 int erase = ttybuff.c_cc[VERASE];
1163 int kill = ttybuff.c_cc[VKILL];
1164
1165 if (erase != -1 && keymap[(unsigned char)erase].type == ISFUNC)
1166 keymap[(unsigned char)erase].function = rl_rubout;
1167
1168 if (kill != -1 && keymap[(unsigned char)kill].type == ISFUNC)
1169 keymap[(unsigned char)kill].function = rl_unix_line_discard;
1170 }
1171 #endif /* NEW_TTY_DRIVER */
1172 }
1173
1174
1175 /* **************************************************************** */
1176 /* */
1177 /* Numeric Arguments */
1178 /* */
1179 /* **************************************************************** */
1180
1181 /* Handle C-u style numeric args, as well as M--, and M-digits. */
1182
1183 /* Add the current digit to the argument in progress. */
rl_digit_argument(ignore,key)1184 rl_digit_argument (ignore, key)
1185 int ignore, key;
1186 {
1187 rl_pending_input = key;
1188 rl_digit_loop ();
1189 }
1190
1191 /* What to do when you abort reading an argument. */
rl_discard_argument()1192 rl_discard_argument ()
1193 {
1194 ding ();
1195 rl_clear_message ();
1196 rl_init_argument ();
1197 }
1198
1199 /* Create a default argument. */
rl_init_argument()1200 rl_init_argument ()
1201 {
1202 rl_numeric_arg = arg_sign = 1;
1203 rl_explicit_arg = 0;
1204 }
1205
1206 /* C-u, universal argument. Multiply the current argument by 4.
1207 Read a key. If the key has nothing to do with arguments, then
1208 dispatch on it. If the key is the abort character then abort. */
rl_universal_argument()1209 rl_universal_argument ()
1210 {
1211 rl_numeric_arg *= 4;
1212 rl_digit_loop ();
1213 }
1214
rl_digit_loop()1215 rl_digit_loop ()
1216 {
1217 int key, c;
1218 while (1)
1219 {
1220 rl_message ("(arg: %d) ", arg_sign * rl_numeric_arg);
1221 key = c = rl_read_key ();
1222
1223 if (keymap[c].type == ISFUNC &&
1224 keymap[c].function == rl_universal_argument)
1225 {
1226 rl_numeric_arg *= 4;
1227 continue;
1228 }
1229 c = UNMETA (c);
1230 if (numeric (c))
1231 {
1232 if (rl_explicit_arg)
1233 rl_numeric_arg = (rl_numeric_arg * 10) + (c - '0');
1234 else
1235 rl_numeric_arg = (c - '0');
1236 rl_explicit_arg = 1;
1237 }
1238 else
1239 {
1240 if (c == '-' && !rl_explicit_arg)
1241 {
1242 rl_numeric_arg = 1;
1243 arg_sign = -1;
1244 }
1245 else
1246 {
1247 rl_clear_message ();
1248 rl_dispatch (key, keymap);
1249 return;
1250 }
1251 }
1252 }
1253 }
1254
1255
1256 /* **************************************************************** */
1257 /* */
1258 /* Display stuff */
1259 /* */
1260 /* **************************************************************** */
1261
1262 /* This is the stuff that is hard for me. I never seem to write good
1263 display routines in C. Let's see how I do this time. */
1264
1265 /* (PWP) Well... Good for a simple line updater, but totally ignores
1266 the problems of input lines longer than the screen width.
1267
1268 update_line and the code that calls it makes a multiple line,
1269 automatically wrapping line update. Carefull attention needs
1270 to be paid to the vertical position variables.
1271
1272 handling of terminals with autowrap on (incl. DEC braindamage)
1273 could be improved a bit. Right now I just cheat and decrement
1274 screenwidth by one. */
1275
1276 /* Keep two buffers; one which reflects the current contents of the
1277 screen, and the other to draw what we think the new contents should
1278 be. Then compare the buffers, and make whatever changes to the
1279 screen itself that we should. Finally, make the buffer that we
1280 just drew into be the one which reflects the current contents of the
1281 screen, and place the cursor where it belongs.
1282
1283 Commands that want to can fix the display themselves, and then let
1284 this function know that the display has been fixed by setting the
1285 RL_DISPLAY_FIXED variable. This is good for efficiency. */
1286
1287 /* Termcap variables: */
1288 extern char *term_up, *term_dc, *term_cr;
1289 extern int screenheight, screenwidth, terminal_can_insert;
1290
1291 /* What YOU turn on when you have handled all redisplay yourself. */
1292 int rl_display_fixed = 0;
1293
1294 /* The visible cursor position. If you print some text, adjust this. */
1295 int last_c_pos = 0;
1296 int last_v_pos = 0;
1297
1298 /* The last left edge of text that was displayed. This is used when
1299 doing horizontal scrolling. It shifts in thirds of a screenwidth. */
1300 static int last_lmargin = 0;
1301
1302 /* The line display buffers. One is the line currently displayed on
1303 the screen. The other is the line about to be displayed. */
1304 static char *visible_line = (char *)NULL;
1305 static char *invisible_line = (char *)NULL;
1306
1307 /* Number of lines currently on screen minus 1. */
1308 int vis_botlin = 0;
1309
1310 /* A buffer for `modeline' messages. */
1311 char msg_buf[128];
1312
1313 /* Non-zero forces the redisplay even if we thought it was unnecessary. */
1314 int forced_display = 0;
1315
1316 /* The stuff that gets printed out before the actual text of the line.
1317 This is usually pointing to rl_prompt. */
1318 char *rl_display_prompt = (char *)NULL;
1319
1320 /* Default and initial buffer size. Can grow. */
1321 static int line_size = 1024;
1322
1323 /* Non-zero means to always use horizontal scrolling in line display. */
1324 static int horizontal_scroll_mode = 0;
1325
1326 /* Non-zero means to display an asterisk at the starts of history lines
1327 which have been modified. */
1328 static int mark_modified_lines = 0;
1329
1330 /* Non-zero means to use a visible bell if one is available rather than
1331 simply ringing the terminal bell. */
1332 static int prefer_visible_bell = 0;
1333
1334 /* I really disagree with this, but my boss (among others) insists that we
1335 support compilers that don't work. I don't think we are gaining by doing
1336 so; what is the advantage in producing better code if we can't use it? */
1337 /* The following two declarations belong inside the
1338 function block, not here. */
1339 static void move_cursor_relative ();
1340 static void output_some_chars ();
1341 static void output_character_function ();
1342 static int compare_strings ();
1343
1344 /* Basic redisplay algorithm. */
rl_redisplay()1345 rl_redisplay ()
1346 {
1347 register int in, out, c, linenum;
1348 register char *line = invisible_line;
1349 char *rindex (), *prompt_this_line;
1350 int c_pos = 0;
1351 int inv_botlin = 0; /* Number of lines in newly drawn buffer. */
1352
1353 extern int readline_echoing_p;
1354
1355 if (!readline_echoing_p)
1356 return;
1357
1358 if (!rl_display_prompt)
1359 rl_display_prompt = "";
1360
1361 if (!invisible_line)
1362 {
1363 visible_line = (char *)xmalloc (line_size);
1364 invisible_line = (char *)xmalloc (line_size);
1365 line = invisible_line;
1366 for (in = 0; in < line_size; in++)
1367 {
1368 visible_line[in] = 0;
1369 invisible_line[in] = 1;
1370 }
1371 rl_on_new_line ();
1372 }
1373
1374 /* Draw the line into the buffer. */
1375 c_pos = -1;
1376
1377 /* Mark the line as modified or not. We only do this for history
1378 lines. */
1379 out = 0;
1380 if (mark_modified_lines && current_history () && rl_undo_list)
1381 {
1382 line[out++] = '*';
1383 line[out] = '\0';
1384 }
1385
1386 /* If someone thought that the redisplay was handled, but the currently
1387 visible line has a different modification state than the one about
1388 to become visible, then correct the callers misconception. */
1389 if (visible_line[0] != invisible_line[0])
1390 rl_display_fixed = 0;
1391
1392 prompt_this_line = rindex (rl_display_prompt, '\n');
1393 if (!prompt_this_line)
1394 prompt_this_line = rl_display_prompt;
1395 else
1396 {
1397 prompt_this_line++;
1398 if (forced_display)
1399 output_some_chars (rl_display_prompt,
1400 prompt_this_line - rl_display_prompt);
1401 }
1402
1403 strncpy (line + out, prompt_this_line, strlen (prompt_this_line));
1404 out += strlen (prompt_this_line);
1405 line[out] = '\0';
1406
1407 for (in = 0; in < rl_end; in++)
1408 {
1409 c = the_line[in];
1410
1411 if (out + 1 >= line_size)
1412 {
1413 line_size *= 2;
1414 visible_line = (char *)xrealloc (visible_line, line_size);
1415 invisible_line = (char *)xrealloc (invisible_line, line_size);
1416 line = invisible_line;
1417 }
1418
1419 if (in == rl_point)
1420 c_pos = out;
1421
1422 if (c > 127)
1423 {
1424 line[out++] = 'M';
1425 line[out++] = '-';
1426 line[out++] = c - 128;
1427 }
1428 #define DISPLAY_TABS
1429 #ifdef DISPLAY_TABS
1430 else if (c == '\t')
1431 {
1432 register int newout = (out | (int)7) + 1;
1433 while (out < newout)
1434 line[out++] = ' ';
1435 }
1436 #endif
1437 else if (c < 32)
1438 {
1439 line[out++] = 'C';
1440 line[out++] = '-';
1441 line[out++] = c + 64;
1442 }
1443 else
1444 line[out++] = c;
1445 }
1446 line[out] = '\0';
1447 if (c_pos < 0)
1448 c_pos = out;
1449
1450 /* PWP: now is when things get a bit hairy. The visible and invisible
1451 line buffers are really multiple lines, which would wrap every
1452 (screenwidth - 1) characters. Go through each in turn, finding
1453 the changed region and updating it. The line order is top to bottom. */
1454
1455 /* If we can move the cursor up and down, then use multiple lines,
1456 otherwise, let long lines display in a single terminal line, and
1457 horizontally scroll it. */
1458
1459 if (!horizontal_scroll_mode && term_up && *term_up)
1460 {
1461 int total_screen_chars = (screenwidth * screenheight);
1462
1463 if (!rl_display_fixed || forced_display)
1464 {
1465 forced_display = 0;
1466
1467 /* If we have more than a screenful of material to display, then
1468 only display a screenful. We should display the last screen,
1469 not the first. I'll fix this in a minute. */
1470 if (out >= total_screen_chars)
1471 out = total_screen_chars - 1;
1472
1473 /* Number of screen lines to display. */
1474 inv_botlin = out / screenwidth;
1475
1476 /* For each line in the buffer, do the updating display. */
1477 for (linenum = 0; linenum <= inv_botlin; linenum++)
1478 update_line (linenum > vis_botlin ? ""
1479 : &visible_line[linenum * screenwidth],
1480 &invisible_line[linenum * screenwidth],
1481 linenum);
1482
1483 /* We may have deleted some lines. If so, clear the left over
1484 blank ones at the bottom out. */
1485 if (vis_botlin > inv_botlin)
1486 {
1487 char *tt;
1488 for (; linenum <= vis_botlin; linenum++)
1489 {
1490 tt = &visible_line[linenum * screenwidth];
1491 move_vert (linenum);
1492 move_cursor_relative (0, tt);
1493 clear_to_eol ((linenum == vis_botlin)?
1494 strlen (tt) : screenwidth);
1495 }
1496 }
1497 vis_botlin = inv_botlin;
1498
1499 /* Move the cursor where it should be. */
1500 move_vert (c_pos / screenwidth);
1501 move_cursor_relative (c_pos % screenwidth,
1502 &invisible_line[(c_pos / screenwidth) * screenwidth]);
1503 }
1504 }
1505 else /* Do horizontal scrolling. */
1506 {
1507 int lmargin;
1508
1509 /* Always at top line. */
1510 last_v_pos = 0;
1511
1512 /* If the display position of the cursor would be off the edge
1513 of the screen, start the display of this line at an offset that
1514 leaves the cursor on the screen. */
1515 if (c_pos - last_lmargin > screenwidth - 2)
1516 lmargin = (c_pos / (screenwidth / 3) - 2) * (screenwidth / 3);
1517 else if (c_pos - last_lmargin < 1)
1518 lmargin = ((c_pos - 1) / (screenwidth / 3)) * (screenwidth / 3);
1519 else
1520 lmargin = last_lmargin;
1521
1522 /* If the first character on the screen isn't the first character
1523 in the display line, indicate this with a special character. */
1524 if (lmargin > 0)
1525 line[lmargin] = '<';
1526
1527 if (lmargin + screenwidth < out)
1528 line[lmargin + screenwidth - 1] = '>';
1529
1530 if (!rl_display_fixed || forced_display || lmargin != last_lmargin)
1531 {
1532 forced_display = 0;
1533 update_line (&visible_line[last_lmargin],
1534 &invisible_line[lmargin], 0);
1535
1536 move_cursor_relative (c_pos - lmargin, &invisible_line[lmargin]);
1537 last_lmargin = lmargin;
1538 }
1539 }
1540 fflush (out_stream);
1541
1542 /* Swap visible and non-visible lines. */
1543 {
1544 char *temp = visible_line;
1545 visible_line = invisible_line;
1546 invisible_line = temp;
1547 rl_display_fixed = 0;
1548 }
1549 }
1550
1551 /* PWP: update_line() is based on finding the middle difference of each
1552 line on the screen; vis:
1553
1554 /old first difference
1555 /beginning of line | /old last same /old EOL
1556 v v v v
1557 old: eddie> Oh, my little gruntle-buggy is to me, as lurgid as
1558 new: eddie> Oh, my little buggy says to me, as lurgid as
1559 ^ ^ ^ ^
1560 \beginning of line | \new last same \new end of line
1561 \new first difference
1562
1563 All are character pointers for the sake of speed. Special cases for
1564 no differences, as well as for end of line additions must be handeled.
1565
1566 Could be made even smarter, but this works well enough */
1567 static
update_line(old,new,current_line)1568 update_line (old, new, current_line)
1569 register char *old, *new;
1570 int current_line;
1571 {
1572 register char *ofd, *ols, *oe, *nfd, *nls, *ne;
1573 int lendiff, wsatend;
1574
1575 /* Find first difference. */
1576 for (ofd = old, nfd = new;
1577 (ofd - old < screenwidth) && *ofd && (*ofd == *nfd);
1578 ofd++, nfd++)
1579 ;
1580
1581 /* Move to the end of the screen line. */
1582 for (oe = ofd; ((oe - old) < screenwidth) && *oe; oe++);
1583 for (ne = nfd; ((ne - new) < screenwidth) && *ne; ne++);
1584
1585 /* If no difference, continue to next line. */
1586 if (ofd == oe && nfd == ne)
1587 return;
1588
1589 wsatend = 1; /* flag for trailing whitespace */
1590 ols = oe - 1; /* find last same */
1591 nls = ne - 1;
1592 while ((*ols == *nls) && (ols > ofd) && (nls > nfd))
1593 {
1594 if (*ols != ' ')
1595 wsatend = 0;
1596 ols--;
1597 nls--;
1598 }
1599
1600 if (wsatend)
1601 {
1602 ols = oe;
1603 nls = ne;
1604 }
1605 else if (*ols != *nls)
1606 {
1607 if (*ols) /* don't step past the NUL */
1608 ols++;
1609 if (*nls)
1610 nls++;
1611 }
1612
1613 move_vert (current_line);
1614 move_cursor_relative (ofd - old, old);
1615
1616 /* if (len (new) > len (old)) */
1617 lendiff = (nls - nfd) - (ols - ofd);
1618
1619 /* Insert (diff(len(old),len(new)) ch */
1620 if (lendiff > 0)
1621 {
1622 if (terminal_can_insert)
1623 {
1624 extern char *term_IC;
1625
1626 /* Sometimes it is cheaper to print the characters rather than
1627 use the terminal's capabilities. */
1628 if ((2 * (ne - nfd)) < lendiff && !term_IC)
1629 {
1630 output_some_chars (nfd, (ne - nfd));
1631 last_c_pos += (ne - nfd);
1632 }
1633 else
1634 {
1635 if (*ols)
1636 {
1637 insert_some_chars (nfd, lendiff);
1638 last_c_pos += lendiff;
1639 }
1640 else
1641 {
1642 /* At the end of a line the characters do not have to
1643 be "inserted". They can just be placed on the screen. */
1644 output_some_chars (nfd, lendiff);
1645 last_c_pos += lendiff;
1646 }
1647 /* Copy (new) chars to screen from first diff to last match. */
1648 if (((nls - nfd) - lendiff) > 0)
1649 {
1650 output_some_chars (&nfd[lendiff], ((nls - nfd) - lendiff));
1651 last_c_pos += ((nls - nfd) - lendiff);
1652 }
1653 }
1654 }
1655 else
1656 { /* cannot insert chars, write to EOL */
1657 output_some_chars (nfd, (ne - nfd));
1658 last_c_pos += (ne - nfd);
1659 }
1660 }
1661 else /* Delete characters from line. */
1662 {
1663 /* If possible and inexpensive to use terminal deletion, then do so. */
1664 if (term_dc && (2 * (ne - nfd)) >= (-lendiff))
1665 {
1666 if (lendiff)
1667 delete_chars (-lendiff); /* delete (diff) characters */
1668
1669 /* Copy (new) chars to screen from first diff to last match */
1670 if ((nls - nfd) > 0)
1671 {
1672 output_some_chars (nfd, (nls - nfd));
1673 last_c_pos += (nls - nfd);
1674 }
1675 }
1676 /* Otherwise, print over the existing material. */
1677 else
1678 {
1679 output_some_chars (nfd, (ne - nfd));
1680 last_c_pos += (ne - nfd);
1681 clear_to_eol ((oe - old) - (ne - new));
1682 }
1683 }
1684 }
1685
1686 /* (PWP) tell the update routines that we have moved onto a
1687 new (empty) line. */
rl_on_new_line()1688 rl_on_new_line ()
1689 {
1690 if (visible_line)
1691 visible_line[0] = '\0';
1692
1693 last_c_pos = last_v_pos = 0;
1694 vis_botlin = last_lmargin = 0;
1695 }
1696
1697 /* Actually update the display, period. */
rl_forced_update_display()1698 rl_forced_update_display ()
1699 {
1700 if (visible_line)
1701 {
1702 register char *temp = visible_line;
1703
1704 while (*temp) *temp++ = '\0';
1705 }
1706 rl_on_new_line ();
1707 forced_display++;
1708 rl_redisplay ();
1709 }
1710
1711 /* Move the cursor from last_c_pos to NEW, which are buffer indices.
1712 DATA is the contents of the screen line of interest; i.e., where
1713 the movement is being done. */
1714 static void
move_cursor_relative(new,data)1715 move_cursor_relative (new, data)
1716 int new;
1717 char *data;
1718 {
1719 register int i;
1720
1721 /* It may be faster to output a CR, and then move forwards instead
1722 of moving backwards. */
1723 if (new + 1 < last_c_pos - new)
1724 {
1725 tputs (term_cr, 1, output_character_function);
1726 last_c_pos = 0;
1727 }
1728
1729 if (last_c_pos == new) return;
1730
1731 if (last_c_pos < new)
1732 {
1733 /* Move the cursor forward. We do it by printing the command
1734 to move the cursor forward if there is one, else print that
1735 portion of the output buffer again. Which is cheaper? */
1736
1737 /* The above comment is left here for posterity. It is faster
1738 to print one character (non-control) than to print a control
1739 sequence telling the terminal to move forward one character.
1740 That kind of control is for people who don't know what the
1741 data is underneath the cursor. */
1742 #ifdef HACK_TERMCAP_MOTION
1743 extern char *term_forward_char;
1744
1745 if (term_forward_char)
1746 for (i = last_c_pos; i < new; i++)
1747 tputs (term_forward_char, 1, output_character_function);
1748 else
1749 for (i = last_c_pos; i < new; i++)
1750 putc (data[i], out_stream);
1751 #else
1752 for (i = last_c_pos; i < new; i++)
1753 putc (data[i], out_stream);
1754 #endif /* HACK_TERMCAP_MOTION */
1755 }
1756 else
1757 backspace (last_c_pos - new);
1758 last_c_pos = new;
1759 }
1760
1761 /* PWP: move the cursor up or down. */
move_vert(to)1762 move_vert (to)
1763 int to;
1764 {
1765 void output_character_function ();
1766 register int delta, i;
1767
1768 if (last_v_pos == to) return;
1769
1770 if (to > screenheight)
1771 return;
1772
1773 if ((delta = to - last_v_pos) > 0)
1774 {
1775 for (i = 0; i < delta; i++)
1776 putc ('\n', out_stream);
1777 tputs (term_cr, 1, output_character_function);
1778 last_c_pos = 0; /* because crlf() will do \r\n */
1779 }
1780 else
1781 { /* delta < 0 */
1782 if (term_up && *term_up)
1783 for (i = 0; i < -delta; i++)
1784 tputs (term_up, 1, output_character_function);
1785 }
1786 last_v_pos = to; /* now to is here */
1787 }
1788
1789 /* Physically print C on out_stream. This is for functions which know
1790 how to optimize the display. */
rl_show_char(c)1791 rl_show_char (c)
1792 int c;
1793 {
1794 if (c > 127)
1795 {
1796 fprintf (out_stream, "M-");
1797 c -= 128;
1798 }
1799
1800 #ifdef DISPLAY_TABS
1801 if (c < 32 && c != '\t')
1802 #else
1803 if (c < 32)
1804 #endif
1805 {
1806
1807 c += 64;
1808 }
1809
1810 putc (c, out_stream);
1811 fflush (out_stream);
1812 }
1813
1814 #ifdef DISPLAY_TABS
1815 int
rl_character_len(c,pos)1816 rl_character_len (c, pos)
1817 register int c, pos;
1818 {
1819 if (c < ' ' || c > 126)
1820 {
1821 if (c == '\t')
1822 return (((pos | (int)7) + 1) - pos);
1823 else
1824 return (3);
1825 }
1826 else
1827 return (1);
1828 }
1829 #else
1830 int
rl_character_len(c)1831 rl_character_len (c)
1832 int c;
1833 {
1834 if (c < ' ' || c > 126)
1835 return (3);
1836 else
1837 return (1);
1838 }
1839 #endif /* DISPLAY_TAB */
1840
1841 /* How to print things in the "echo-area". The prompt is treated as a
1842 mini-modeline. */
rl_message(string,arg1,arg2)1843 rl_message (string, arg1, arg2)
1844 char *string;
1845 {
1846 sprintf (msg_buf, string, arg1, arg2);
1847 rl_display_prompt = msg_buf;
1848 rl_redisplay ();
1849 }
1850
1851 /* How to clear things from the "echo-area". */
rl_clear_message()1852 rl_clear_message ()
1853 {
1854 rl_display_prompt = rl_prompt;
1855 rl_redisplay ();
1856 }
1857
1858 /* **************************************************************** */
1859 /* */
1860 /* Terminal and Termcap */
1861 /* */
1862 /* **************************************************************** */
1863
1864 static char *term_buffer = (char *)NULL;
1865 static char *term_string_buffer = (char *)NULL;
1866
1867 /* Non-zero means this terminal can't really do anything. */
1868 int dumb_term = 0;
1869
1870 char PC;
1871 char *BC, *UP;
1872
1873 /* Some strings to control terminal actions. These are output by tputs (). */
1874 char *term_goto, *term_clreol, *term_cr, *term_clrpag, *term_backspace;
1875
1876 int screenwidth, screenheight;
1877
1878 /* Non-zero if we determine that the terminal can do character insertion. */
1879 int terminal_can_insert = 0;
1880
1881 /* How to insert characters. */
1882 char *term_im, *term_ei, *term_ic, *term_ip, *term_IC;
1883
1884 /* How to delete characters. */
1885 char *term_dc, *term_DC;
1886
1887 #ifdef HACK_TERMCAP_MOTION
1888 char *term_forward_char;
1889 #endif /* HACK_TERMCAP_MOTION */
1890
1891 /* How to go up a line. */
1892 char *term_up;
1893
1894 /* A visible bell, if the terminal can be made to flash the screen. */
1895 char *visible_bell;
1896
1897 /* Re-initialize the terminal considering that the TERM/TERMCAP variable
1898 has changed. */
rl_reset_terminal(terminal_name)1899 rl_reset_terminal (terminal_name)
1900 char *terminal_name;
1901 {
1902 init_terminal_io (terminal_name);
1903 }
1904
init_terminal_io(terminal_name)1905 init_terminal_io (terminal_name)
1906 char *terminal_name;
1907 {
1908 char *term = (terminal_name? terminal_name : (char *)getenv ("TERM"));
1909 char *tgetstr (), *buffer;
1910 #ifdef TIOCGWINSZ
1911 struct winsize window_size;
1912 #endif
1913 int tty;
1914
1915 if (!term_string_buffer)
1916 term_string_buffer = (char *)xmalloc (2048);
1917
1918 if (!term_buffer)
1919 term_buffer = (char *)xmalloc (2048);
1920
1921 buffer = term_string_buffer;
1922
1923 term_clrpag = term_cr = term_clreol = (char *)NULL;
1924
1925 if (!term)
1926 term = "dumb";
1927
1928 if (tgetent (term_buffer, term) < 0)
1929 {
1930 dumb_term = 1;
1931 return;
1932 }
1933
1934 BC = tgetstr ("pc", &buffer);
1935 PC = buffer ? *buffer : 0;
1936
1937 term_backspace = tgetstr ("le", &buffer);
1938
1939 term_cr = tgetstr ("cr", &buffer);
1940 term_clreol = tgetstr ("ce", &buffer);
1941 term_clrpag = tgetstr ("cl", &buffer);
1942
1943 if (!term_cr)
1944 term_cr = "\r";
1945
1946 #ifdef HACK_TERMCAP_MOTION
1947 term_forward_char = tgetstr ("nd", &buffer);
1948 #endif /* HACK_TERMCAP_MOTION */
1949
1950 if (rl_instream)
1951 tty = fileno (rl_instream);
1952 else
1953 tty = 0;
1954
1955 screenwidth = screenheight = 0;
1956 #ifdef TIOCGWINSZ
1957 if (ioctl (tty, TIOCGWINSZ, &window_size) == 0)
1958 {
1959 screenwidth = (int) window_size.ws_col;
1960 screenheight = (int) window_size.ws_row;
1961 }
1962 #endif
1963
1964 if (screenwidth <= 0 || screenheight <= 0)
1965 {
1966 screenwidth = tgetnum ("co");
1967 screenheight = tgetnum ("li");
1968 }
1969
1970 screenwidth--;
1971
1972 if (screenwidth <= 0)
1973 screenwidth = 79;
1974
1975 if (screenheight <= 0)
1976 screenheight = 24;
1977
1978 term_im = tgetstr ("im", &buffer);
1979 term_ei = tgetstr ("ei", &buffer);
1980 term_IC = tgetstr ("IC", &buffer);
1981 term_ic = tgetstr ("ic", &buffer);
1982
1983 /* "An application program can assume that the terminal can do
1984 character insertion if *any one of* the capabilities `IC',
1985 `im', `ic' or `ip' is provided." But we can't do anything if
1986 only `ip' is provided, so... */
1987 terminal_can_insert = (term_IC || term_im || term_ic);
1988
1989 term_up = tgetstr ("up", &buffer);
1990 term_dc = tgetstr ("dc", &buffer);
1991 term_DC = tgetstr ("DC", &buffer);
1992
1993 visible_bell = tgetstr ("vb", &buffer);
1994 }
1995
1996 /* A function for the use of tputs () */
1997 static void
output_character_function(c)1998 output_character_function (c)
1999 int c;
2000 {
2001 putc (c, out_stream);
2002 }
2003
2004 /* Write COUNT characters from STRING to the output stream. */
2005 static void
output_some_chars(string,count)2006 output_some_chars (string, count)
2007 char *string;
2008 int count;
2009 {
2010 fwrite (string, 1, count, out_stream);
2011 }
2012
2013 /* Delete COUNT characters from the display line. */
2014 static
delete_chars(count)2015 delete_chars (count)
2016 int count;
2017 {
2018 if (count > screenwidth)
2019 return;
2020
2021 if (term_DC && *term_DC)
2022 {
2023 char *tgoto (), *buffer;
2024 buffer = tgoto (term_DC, 0, count);
2025 tputs (buffer, 1, output_character_function);
2026 }
2027 else
2028 {
2029 if (term_dc && *term_dc)
2030 while (count--)
2031 tputs (term_dc, 1, output_character_function);
2032 }
2033 }
2034
2035 /* Insert COUNT characters from STRING to the output stream. */
2036 static
insert_some_chars(string,count)2037 insert_some_chars (string, count)
2038 char *string;
2039 int count;
2040 {
2041 /* If IC is defined, then we do not have to "enter" insert mode. */
2042 if (term_IC)
2043 {
2044 char *tgoto (), *buffer;
2045 buffer = tgoto (term_IC, 0, count);
2046 tputs (buffer, 1, output_character_function);
2047 output_some_chars (string, count);
2048 }
2049 else
2050 {
2051 register int i;
2052
2053 /* If we have to turn on insert-mode, then do so. */
2054 if (term_im && *term_im)
2055 tputs (term_im, 1, output_character_function);
2056
2057 /* If there is a special command for inserting characters, then
2058 use that first to open up the space. */
2059 if (term_ic && *term_ic)
2060 {
2061 for (i = count; i--; )
2062 tputs (term_ic, 1, output_character_function);
2063 }
2064
2065 /* Print the text. */
2066 output_some_chars (string, count);
2067
2068 /* If there is a string to turn off insert mode, we had best use
2069 it now. */
2070 if (term_ei && *term_ei)
2071 tputs (term_ei, 1, output_character_function);
2072 }
2073 }
2074
2075 /* Move the cursor back. */
backspace(count)2076 backspace (count)
2077 int count;
2078 {
2079 register int i;
2080
2081 if (term_backspace)
2082 for (i = 0; i < count; i++)
2083 tputs (term_backspace, 1, output_character_function);
2084 else
2085 for (i = 0; i < count; i++)
2086 putc ('\b', out_stream);
2087 }
2088
2089 /* Move to the start of the next line. */
crlf()2090 crlf ()
2091 {
2092 tputs (term_cr, 1, output_character_function);
2093 putc ('\n', out_stream);
2094 }
2095
2096 /* Clear to the end of the line. COUNT is the minimum
2097 number of character spaces to clear, */
clear_to_eol(count)2098 clear_to_eol (count)
2099 int count;
2100 {
2101 if (term_clreol)
2102 {
2103 tputs (term_clreol, 1, output_character_function);
2104 }
2105 else
2106 {
2107 register int i;
2108
2109 /* Do one more character space. */
2110 count++;
2111
2112 for (i = 0; i < count; i++)
2113 putc (' ', out_stream);
2114
2115 backspace (count);
2116 }
2117 }
2118
2119
2120 /* **************************************************************** */
2121 /* */
2122 /* Saving and Restoring the TTY */
2123 /* */
2124 /* **************************************************************** */
2125
2126 /* Non-zero means that the terminal is in a prepped state. */
2127 static int terminal_prepped = 0;
2128
2129 #ifdef NEW_TTY_DRIVER
2130
2131 /* Standard flags, including ECHO. */
2132 static int original_tty_flags = 0;
2133
2134 /* Local mode flags, like LPASS8. */
2135 static int local_mode_flags = 0;
2136
2137 /* Terminal characters. This has C-s and C-q in it. */
2138 static struct tchars original_tchars;
2139
2140 /* Local special characters. This has the interrupt characters in it. */
2141 static struct ltchars original_ltchars;
2142
2143 /* We use this to get and set the tty_flags. */
2144 static struct sgttyb the_ttybuff;
2145
2146 /* Put the terminal in CBREAK mode so that we can detect key presses. */
2147 static
rl_prep_terminal()2148 rl_prep_terminal ()
2149 {
2150 int tty = fileno (rl_instream);
2151 int oldmask = sigblock (sigmask (SIGINT));
2152
2153 if (!terminal_prepped)
2154 {
2155 /* We always get the latest tty values. Maybe stty changed them. */
2156 ioctl (tty, TIOCGETP, &the_ttybuff);
2157 original_tty_flags = the_ttybuff.sg_flags;
2158
2159 readline_echoing_p = (original_tty_flags & ECHO);
2160
2161 #if defined (TIOCLGET)
2162 ioctl (tty, TIOCLGET, &local_mode_flags);
2163 #endif
2164
2165 #if !defined (ANYP)
2166 #define ANYP (EVENP | ODDP)
2167 #endif
2168 /* If this terminal doesn't care how the 8th bit is used,
2169 then we can use it for the meta-key.
2170 We check by seeing if BOTH odd and even parity are allowed. */
2171 if (the_ttybuff.sg_flags & ANYP)
2172 {
2173 #if defined (PASS8)
2174 the_ttybuff.sg_flags |= PASS8;
2175 #endif
2176
2177 /* Hack on local mode flags if we can. */
2178 #if defined (TIOCLGET) && defined (LPASS8)
2179 {
2180 int flags;
2181 flags = local_mode_flags | LPASS8;
2182 ioctl (tty, TIOCLSET, &flags);
2183 }
2184 #endif /* TIOCLGET && LPASS8 */
2185 }
2186 #ifdef TIOCGETC
2187 {
2188 struct tchars temp;
2189
2190 ioctl (tty, TIOCGETC, &original_tchars);
2191 temp = original_tchars;
2192
2193 /* Get rid of C-s and C-q.
2194 We remember the value of startc (C-q) so that if the terminal is in
2195 xoff state, the user can xon it by pressing that character. */
2196 xon_char = temp.t_startc;
2197 temp.t_stopc = -1;
2198 temp.t_startc = -1;
2199
2200 /* If there is an XON character, bind it to restart the output. */
2201 if (xon_char != -1)
2202 rl_bind_key (xon_char, rl_restart_output);
2203
2204 /* If there is an EOF char, bind eof_char to it. */
2205 if (temp.t_eofc != -1)
2206 eof_char = temp.t_eofc;
2207
2208 #if defined (NOTDEF)
2209 /* Get rid of C-\ and C-c. */
2210 temp.t_intrc = temp.t_quitc = -1;
2211 #endif /* NOTDEF */
2212
2213 ioctl (tty, TIOCSETC, &temp);
2214 }
2215 #endif /* TIOCGETC */
2216
2217 #ifdef TIOCGLTC
2218 {
2219 struct ltchars temp;
2220
2221 ioctl (tty, TIOCGLTC, &original_ltchars);
2222 temp = original_ltchars;
2223
2224 /* Make the interrupt keys go away. Just enough to make people
2225 happy. */
2226 temp.t_dsuspc = -1; /* C-y */
2227 temp.t_lnextc = -1; /* C-v */
2228
2229 ioctl (tty, TIOCSLTC, &temp);
2230 }
2231 #endif /* TIOCGLTC */
2232
2233 the_ttybuff.sg_flags &= (~ECHO|CRMOD);
2234 the_ttybuff.sg_flags |= CBREAK;
2235 ioctl (tty, TIOCSETN, &the_ttybuff);
2236
2237 terminal_prepped = 1;
2238 }
2239 sigsetmask (oldmask);
2240 }
2241
2242 /* Restore the terminal to its original state. */
2243 static
rl_deprep_terminal()2244 rl_deprep_terminal ()
2245 {
2246 int tty = fileno (rl_instream);
2247 int oldmask = sigblock (sigmask (SIGINT));
2248
2249 if (terminal_prepped)
2250 {
2251 the_ttybuff.sg_flags = original_tty_flags;
2252 ioctl (tty, TIOCSETN, &the_ttybuff);
2253 readline_echoing_p = 1;
2254
2255 #if defined (TIOCLGET)
2256 ioctl (tty, TIOCLSET, &local_mode_flags);
2257 #endif
2258
2259 #ifdef TIOCSLTC
2260 ioctl (tty, TIOCSLTC, &original_ltchars);
2261 #endif
2262
2263 #ifdef TIOCSETC
2264 ioctl (tty, TIOCSETC, &original_tchars);
2265 #endif
2266 terminal_prepped = 0;
2267 }
2268 sigsetmask (oldmask);
2269 }
2270
2271 #else /* !defined (NEW_TTY_DRIVER) */
2272
2273 #if !defined (VMIN)
2274 #define VMIN VEOF
2275 #endif
2276
2277 #if !defined (VTIME)
2278 #define VTIME VEOL
2279 #endif
2280
2281 #if defined (_POSIX_VERSION)
2282 static struct termios otio;
2283 #else
2284 static struct termio otio;
2285 #endif
2286
2287 static
rl_prep_terminal()2288 rl_prep_terminal ()
2289 {
2290 int tty = fileno (rl_instream);
2291 #if defined (_POSIX_VERSION)
2292 struct termios tio;
2293 #else
2294 struct termio tio;
2295 #endif
2296
2297 /* If we are on a Posix system, block the delivery of SIGINT for a while. */
2298 #if defined (_POSIX_VERSION)
2299 sigset_t set, oset;
2300
2301 sigemptyset (&set);
2302 sigaddset (&set, SIGINT);
2303 sigprocmask (SIG_BLOCK, &set, &oset);
2304 #else
2305 # if defined (HAVE_BSD_SIGNALS)
2306 int oldmask = sigblock (sigmask (SIGINT));
2307 # endif /* HAVE_BSD_SIGNALS */
2308 #endif /* POSIX */
2309
2310 #if defined (_POSIX_VERSION)
2311 tcgetattr (tty, &tio);
2312 #else
2313 ioctl (tty, TCGETA, &tio);
2314 #endif /* POSIX */
2315
2316 otio = tio;
2317
2318 readline_echoing_p = (tio.c_lflag & ECHO);
2319
2320 tio.c_lflag &= ~(ICANON|ECHO);
2321
2322 #if defined (IXANY)
2323 tio.c_iflag &= ~(IXON|IXOFF|IXANY);
2324 #else
2325 /* `strict' Posix systems do not define IXANY. */
2326 tio.c_iflag &= ~(IXON|IXOFF);
2327 #endif /* IXANY */
2328
2329 /* Only turn this off if we are using all 8 bits. */
2330 /* |ISTRIP|INPCK */
2331
2332 #if !defined (HANDLE_SIGNALS)
2333 tio.c_lflag &= ~ISIG;
2334 #else
2335 tio.c_lflag |= ISIG; /* shouldn't be needed, but... */
2336 #endif
2337
2338 tio.c_cc[VMIN] = 1;
2339 tio.c_cc[VTIME] = 0;
2340
2341 /* Turn off characters that we need on Posix systems with job control,
2342 just to be sure. This includes ^Y and ^V. This should not really
2343 be necessary. */
2344 #if defined (_POSIX_VERSION) && defined (_POSIX_JOB_CONTROL)
2345
2346 #if !defined (_POSIX_VDISABLE)
2347 #define _POSIX_VDISABLE 0
2348 #endif /* POSIX_VDISABLE */
2349
2350 #if defined (VLNEXT)
2351 tio.c_cc[VLNEXT] = _POSIX_VDISABLE;
2352 #endif
2353
2354 #if defined (VDSUSP)
2355 tio.c_cc[VDSUSP] = _POSIX_VDISABLE;
2356 #endif
2357
2358 #endif /* POSIX && JOB_CONTROL */
2359
2360 #if defined (_POSIX_VERSION)
2361 tcsetattr (tty, TCSADRAIN, &tio);
2362 tcflow (tty, TCOON); /* Simulate a ^Q. */
2363 #else
2364 ioctl (tty, TCSETAW, &tio);
2365 ioctl (tty, TCXONC, 1); /* Simulate a ^Q. */
2366 #endif /* POSIX */
2367
2368 #if defined (_POSIX_VERSION)
2369 sigprocmask (SIG_SETMASK, &oset, (sigset_t *)NULL);
2370 #else
2371 # if defined (HAVE_BSD_SIGNALS)
2372 sigsetmask (oldmask);
2373 # endif /* HAVE_BSD_SIGNALS */
2374 #endif /* POSIX */
2375 }
2376
2377 static
rl_deprep_terminal()2378 rl_deprep_terminal ()
2379 {
2380 int tty = fileno (rl_instream);
2381
2382 /* If we are on a Posix system, block the delivery of SIGINT for a while. */
2383 #if defined (_POSIX_VERSION)
2384 sigset_t set, oset;
2385
2386 sigemptyset (&set);
2387 sigaddset (&set, SIGINT);
2388 sigprocmask (SIG_BLOCK, &set, &oset);
2389 #else
2390 # if defined (HAVE_BSD_SIGNALS)
2391 int oldmask = sigblock (sigmask (SIGINT));
2392 # endif /* HAVE_BSD_SIGNALS */
2393 #endif /* POSIX */
2394
2395 #if defined (_POSIX_VERSION)
2396 tcsetattr (tty, TCSADRAIN, &otio);
2397 tcflow (tty, TCOON); /* Simulate a ^Q. */
2398 #else
2399 ioctl (tty, TCSETAW, &otio);
2400 ioctl (tty, TCXONC, 1); /* Simulate a ^Q. */
2401 #endif /* POSIX */
2402
2403 #if defined (_POSIX_VERSION)
2404 sigprocmask (SIG_SETMASK, &oset, (sigset_t *)NULL);
2405 #else
2406 # if defined (HAVE_BSD_SIGNALS)
2407 sigsetmask (oldmask);
2408 # endif /* HAVE_BSD_SIGNALS */
2409 #endif /* POSIX */
2410 }
2411 #endif /* NEW_TTY_DRIVER */
2412
2413
2414 /* **************************************************************** */
2415 /* */
2416 /* Utility Functions */
2417 /* */
2418 /* **************************************************************** */
2419
2420 /* Return 0 if C is not a member of the class of characters that belong
2421 in words, or 1 if it is. */
2422
2423 int allow_pathname_alphabetic_chars = 0;
2424 char *pathname_alphabetic_chars = "/-_=~.#$";
2425
2426 int
alphabetic(c)2427 alphabetic (c)
2428 int c;
2429 {
2430 char *rindex ();
2431 if (pure_alphabetic (c) || (numeric (c)))
2432 return (1);
2433
2434 if (allow_pathname_alphabetic_chars)
2435 return ((int)rindex (pathname_alphabetic_chars, c));
2436 else
2437 return (0);
2438 }
2439
2440 /* Return non-zero if C is a numeric character. */
2441 int
numeric(c)2442 numeric (c)
2443 int c;
2444 {
2445 return (c >= '0' && c <= '9');
2446 }
2447
2448 /* Ring the terminal bell. */
2449 int
ding()2450 ding ()
2451 {
2452 if (readline_echoing_p)
2453 {
2454 if (prefer_visible_bell && visible_bell)
2455 tputs (visible_bell, 1, output_character_function);
2456 else
2457 {
2458 fprintf (stderr, "\007");
2459 fflush (stderr);
2460 }
2461 }
2462 return (-1);
2463 }
2464
2465 /* How to abort things. */
rl_abort()2466 rl_abort ()
2467 {
2468 ding ();
2469 rl_clear_message ();
2470 rl_init_argument ();
2471 rl_pending_input = 0;
2472
2473 defining_kbd_macro = 0;
2474 while (executing_macro)
2475 pop_executing_macro ();
2476
2477 rl_last_func = (Function *)NULL;
2478 longjmp (readline_top_level, 1);
2479 }
2480
2481 /* Return a copy of the string between FROM and TO.
2482 FROM is inclusive, TO is not. */
2483 #if defined (sun) /* Yes, that's right, some crufty function in sunview is
2484 called rl_copy (). */
2485 static
2486 #endif
2487 char *
rl_copy(from,to)2488 rl_copy (from, to)
2489 int from, to;
2490 {
2491 register int length;
2492 char *copy;
2493
2494 /* Fix it if the caller is confused. */
2495 if (from > to) {
2496 int t = from;
2497 from = to;
2498 to = t;
2499 }
2500
2501 length = to - from;
2502 copy = (char *)xmalloc (1 + length);
2503 strncpy (copy, the_line + from, length);
2504 copy[length] = '\0';
2505 return (copy);
2506 }
2507
2508
2509 /* **************************************************************** */
2510 /* */
2511 /* Insert and Delete */
2512 /* */
2513 /* **************************************************************** */
2514
2515 /* Insert a string of text into the line at point. This is the only
2516 way that you should do insertion. rl_insert () calls this
2517 function. */
rl_insert_text(string)2518 rl_insert_text (string)
2519 char *string;
2520 {
2521 extern int doing_an_undo;
2522 register int i, l = strlen (string);
2523 while (rl_end + l >= rl_line_buffer_len)
2524 {
2525 rl_line_buffer =
2526 (char *)xrealloc (rl_line_buffer,
2527 rl_line_buffer_len += DEFAULT_BUFFER_SIZE);
2528 the_line = rl_line_buffer;
2529 }
2530
2531 for (i = rl_end; i >= rl_point; i--)
2532 the_line[i + l] = the_line[i];
2533 strncpy (the_line + rl_point, string, l);
2534
2535 /* Remember how to undo this if we aren't undoing something. */
2536 if (!doing_an_undo)
2537 {
2538 /* If possible and desirable, concatenate the undos. */
2539 if ((strlen (string) == 1) &&
2540 rl_undo_list &&
2541 (rl_undo_list->what == UNDO_INSERT) &&
2542 (rl_undo_list->end == rl_point) &&
2543 (rl_undo_list->end - rl_undo_list->start < 20))
2544 rl_undo_list->end++;
2545 else
2546 rl_add_undo (UNDO_INSERT, rl_point, rl_point + l, (char *)NULL);
2547 }
2548 rl_point += l;
2549 rl_end += l;
2550 the_line[rl_end] = '\0';
2551 }
2552
2553 /* Delete the string between FROM and TO. FROM is
2554 inclusive, TO is not. */
rl_delete_text(from,to)2555 rl_delete_text (from, to)
2556 int from, to;
2557 {
2558 extern int doing_an_undo;
2559 register char *text;
2560
2561 /* Fix it if the caller is confused. */
2562 if (from > to)
2563 {
2564 int t = from;
2565 from = to;
2566 to = t;
2567 }
2568 text = rl_copy (from, to);
2569 strncpy (the_line + from, the_line + to, rl_end - to);
2570
2571 /* Remember how to undo this delete. */
2572 if (!doing_an_undo)
2573 rl_add_undo (UNDO_DELETE, from, to, text);
2574 else
2575 free (text);
2576
2577 rl_end -= (to - from);
2578 the_line[rl_end] = '\0';
2579 }
2580
2581
2582 /* **************************************************************** */
2583 /* */
2584 /* Readline character functions */
2585 /* */
2586 /* **************************************************************** */
2587
2588 /* This is not a gap editor, just a stupid line input routine. No hair
2589 is involved in writing any of the functions, and none should be. */
2590
2591 /* Note that:
2592
2593 rl_end is the place in the string that we would place '\0';
2594 i.e., it is always safe to place '\0' there.
2595
2596 rl_point is the place in the string where the cursor is. Sometimes
2597 this is the same as rl_end.
2598
2599 Any command that is called interactively receives two arguments.
2600 The first is a count: the numeric arg pased to this command.
2601 The second is the key which invoked this command.
2602 */
2603
2604
2605 /* **************************************************************** */
2606 /* */
2607 /* Movement Commands */
2608 /* */
2609 /* **************************************************************** */
2610
2611 /* Note that if you `optimize' the display for these functions, you cannot
2612 use said functions in other functions which do not do optimizing display.
2613 I.e., you will have to update the data base for rl_redisplay, and you
2614 might as well let rl_redisplay do that job. */
2615
2616 /* Move forward COUNT characters. */
rl_forward(count)2617 rl_forward (count)
2618 int count;
2619 {
2620 if (count < 0)
2621 rl_backward (-count);
2622 else
2623 while (count)
2624 {
2625 #ifdef VI_MODE
2626 if (rl_point == (rl_end - (rl_editing_mode == vi_mode)))
2627 #else
2628 if (rl_point == rl_end)
2629 #endif
2630 {
2631 ding ();
2632 return;
2633 }
2634 else
2635 rl_point++;
2636 --count;
2637 }
2638 }
2639
2640 /* Move backward COUNT characters. */
rl_backward(count)2641 rl_backward (count)
2642 int count;
2643 {
2644 if (count < 0)
2645 rl_forward (-count);
2646 else
2647 while (count)
2648 {
2649 if (!rl_point)
2650 {
2651 ding ();
2652 return;
2653 }
2654 else
2655 --rl_point;
2656 --count;
2657 }
2658 }
2659
2660 /* Move to the beginning of the line. */
rl_beg_of_line()2661 rl_beg_of_line ()
2662 {
2663 rl_point = 0;
2664 }
2665
2666 /* Move to the end of the line. */
rl_end_of_line()2667 rl_end_of_line ()
2668 {
2669 rl_point = rl_end;
2670 }
2671
2672 /* Move forward a word. We do what Emacs does. */
rl_forward_word(count)2673 rl_forward_word (count)
2674 int count;
2675 {
2676 int c;
2677
2678 if (count < 0)
2679 {
2680 rl_backward_word (-count);
2681 return;
2682 }
2683
2684 while (count)
2685 {
2686 if (rl_point == rl_end)
2687 return;
2688
2689 /* If we are not in a word, move forward until we are in one.
2690 Then, move forward until we hit a non-alphabetic character. */
2691 c = the_line[rl_point];
2692 if (!alphabetic (c))
2693 {
2694 while (++rl_point < rl_end)
2695 {
2696 c = the_line[rl_point];
2697 if (alphabetic (c)) break;
2698 }
2699 }
2700 if (rl_point == rl_end) return;
2701 while (++rl_point < rl_end)
2702 {
2703 c = the_line[rl_point];
2704 if (!alphabetic (c)) break;
2705 }
2706 --count;
2707 }
2708 }
2709
2710 /* Move backward a word. We do what Emacs does. */
rl_backward_word(count)2711 rl_backward_word (count)
2712 int count;
2713 {
2714 int c;
2715
2716 if (count < 0)
2717 {
2718 rl_forward_word (-count);
2719 return;
2720 }
2721
2722 while (count)
2723 {
2724 if (!rl_point)
2725 return;
2726
2727 /* Like rl_forward_word (), except that we look at the characters
2728 just before point. */
2729
2730 c = the_line[rl_point - 1];
2731 if (!alphabetic (c))
2732 {
2733 while (--rl_point)
2734 {
2735 c = the_line[rl_point - 1];
2736 if (alphabetic (c)) break;
2737 }
2738 }
2739
2740 while (rl_point)
2741 {
2742 c = the_line[rl_point - 1];
2743 if (!alphabetic (c))
2744 break;
2745 else --rl_point;
2746 }
2747 --count;
2748 }
2749 }
2750
2751 /* Clear the current line. Numeric argument to C-l does this. */
rl_refresh_line()2752 rl_refresh_line ()
2753 {
2754 int curr_line = last_c_pos / screenwidth;
2755 extern char *term_clreol;
2756
2757 move_vert(curr_line);
2758 move_cursor_relative (0, the_line); /* XXX is this right */
2759
2760 if (term_clreol)
2761 tputs (term_clreol, 1, output_character_function);
2762
2763 rl_forced_update_display ();
2764 rl_display_fixed = 1;
2765 }
2766
2767 /* C-l typed to a line without quoting clears the screen, and then reprints
2768 the prompt and the current input line. Given a numeric arg, redraw only
2769 the current line. */
rl_clear_screen()2770 rl_clear_screen ()
2771 {
2772 extern char *term_clrpag;
2773
2774 if (rl_explicit_arg)
2775 {
2776 rl_refresh_line ();
2777 return;
2778 }
2779
2780 if (term_clrpag)
2781 tputs (term_clrpag, 1, output_character_function);
2782 else
2783 crlf ();
2784
2785 rl_forced_update_display ();
2786 rl_display_fixed = 1;
2787 }
2788
rl_arrow_keys(count,c)2789 rl_arrow_keys(count,c)
2790 int count,c;
2791 {
2792 int ch = rl_read_key();
2793
2794 switch(ch) {
2795 case 'a':
2796 case 'A':
2797 rl_get_previous_history(count);
2798 return;
2799 case 'b':
2800 case 'B':
2801 rl_get_next_history(count);
2802 return;
2803 case 'c':
2804 case 'C':
2805 rl_forward(count);
2806 return;
2807 case 'd':
2808 case 'D':
2809 rl_backward(count);
2810 return;
2811 default:
2812 ding();
2813 return;
2814 }
2815 }
2816
2817
2818 /* **************************************************************** */
2819 /* */
2820 /* Text commands */
2821 /* */
2822 /* **************************************************************** */
2823
2824 /* Insert the character C at the current location, moving point forward. */
rl_insert(count,c)2825 rl_insert (count, c)
2826 int count, c;
2827 {
2828 register int i;
2829 char *string;
2830
2831 if (count <= 0)
2832 return;
2833
2834 /* If we can optimize, then do it. But don't let people crash
2835 readline because of extra large arguments. */
2836 if (count > 1 && count < 1024)
2837 {
2838 string = (char *)alloca (1 + count);
2839
2840 for (i = 0; i < count; i++)
2841 string[i] = c;
2842
2843 string[i] = '\0';
2844 rl_insert_text (string);
2845 return;
2846 }
2847
2848 if (count > 1024)
2849 {
2850 int decreaser;
2851
2852 string = (char *)alloca (1024 + 1);
2853
2854 for (i = 0; i < 1024; i++)
2855 string[i] = c;
2856
2857 while (count)
2858 {
2859 decreaser = (count > 1024 ? 1024 : count);
2860 string[decreaser] = '\0';
2861 rl_insert_text (string);
2862 count -= decreaser;
2863 }
2864 return;
2865 }
2866
2867 /* We are inserting a single character.
2868 If there is pending input, then make a string of all of the
2869 pending characters that are bound to rl_insert, and insert
2870 them all. */
2871 if (any_typein)
2872 {
2873 int key = 0, t;
2874
2875 i = 0;
2876 string = (char *)alloca (ibuffer_len + 1);
2877 string[i++] = c;
2878
2879 while ((t = rl_get_char (&key)) &&
2880 (keymap[key].type == ISFUNC &&
2881 keymap[key].function == rl_insert))
2882 string[i++] = key;
2883
2884 if (t)
2885 rl_unget_char (key);
2886
2887 string[i] = '\0';
2888 rl_insert_text (string);
2889 return;
2890 }
2891 else
2892 {
2893 /* Inserting a single character. */
2894 string = (char *)alloca (2);
2895
2896 string[1] = '\0';
2897 string[0] = c;
2898 rl_insert_text (string);
2899 }
2900 }
2901
2902 /* Insert the next typed character verbatim. */
rl_quoted_insert(count)2903 rl_quoted_insert (count)
2904 int count;
2905 {
2906 int c = rl_read_key ();
2907 rl_insert (count, c);
2908 }
2909
2910 /* Insert a tab character. */
rl_tab_insert(count)2911 rl_tab_insert (count)
2912 int count;
2913 {
2914 rl_insert (count, '\t');
2915 }
2916
2917 /* What to do when a NEWLINE is pressed. We accept the whole line.
2918 KEY is the key that invoked this command. I guess it could have
2919 meaning in the future. */
rl_newline(count,key)2920 rl_newline (count, key)
2921 int count, key;
2922 {
2923
2924 rl_done = 1;
2925
2926 #ifdef VI_MODE
2927 {
2928 extern int vi_doing_insert;
2929 if (vi_doing_insert)
2930 {
2931 rl_end_undo_group ();
2932 vi_doing_insert = 0;
2933 }
2934 }
2935 #endif /* VI_MODE */
2936
2937 if (readline_echoing_p)
2938 {
2939 move_vert (vis_botlin);
2940 vis_botlin = 0;
2941 crlf ();
2942 fflush (out_stream);
2943 rl_display_fixed++;
2944 }
2945 }
2946
rl_clean_up_for_exit()2947 rl_clean_up_for_exit ()
2948 {
2949 if (readline_echoing_p)
2950 {
2951 move_vert (vis_botlin);
2952 vis_botlin = 0;
2953 fflush (out_stream);
2954 rl_restart_output ();
2955 }
2956 }
2957
2958 /* What to do for some uppercase characters, like meta characters,
2959 and some characters appearing in emacs_ctlx_keymap. This function
2960 is just a stub, you bind keys to it and the code in rl_dispatch ()
2961 is special cased. */
rl_do_lowercase_version(ignore1,ignore2)2962 rl_do_lowercase_version (ignore1, ignore2)
2963 int ignore1, ignore2;
2964 {
2965 }
2966
2967 /* Rubout the character behind point. */
rl_rubout(count)2968 rl_rubout (count)
2969 int count;
2970 {
2971 if (count < 0)
2972 {
2973 rl_delete (-count);
2974 return;
2975 }
2976
2977 if (!rl_point)
2978 {
2979 ding ();
2980 return;
2981 }
2982
2983 if (count > 1)
2984 {
2985 int orig_point = rl_point;
2986 rl_backward (count);
2987 rl_kill_text (orig_point, rl_point);
2988 }
2989 else
2990 {
2991 int c = the_line[--rl_point];
2992 rl_delete_text (rl_point, rl_point + 1);
2993
2994 if (rl_point == rl_end && alphabetic (c) && last_c_pos)
2995 {
2996 backspace (1);
2997 putc (' ', out_stream);
2998 backspace (1);
2999 last_c_pos--;
3000 visible_line[last_c_pos] = '\0';
3001 rl_display_fixed++;
3002 }
3003 }
3004 }
3005
3006 /* Delete the character under the cursor. Given a numeric argument,
3007 kill that many characters instead. */
rl_delete(count,invoking_key)3008 rl_delete (count, invoking_key)
3009 int count, invoking_key;
3010 {
3011 if (count < 0)
3012 {
3013 rl_rubout (-count);
3014 return;
3015 }
3016
3017 if (rl_point == rl_end)
3018 {
3019 ding ();
3020 return;
3021 }
3022
3023 if (count > 1)
3024 {
3025 int orig_point = rl_point;
3026 rl_forward (count);
3027 rl_kill_text (orig_point, rl_point);
3028 rl_point = orig_point;
3029 }
3030 else
3031 rl_delete_text (rl_point, rl_point + 1);
3032 }
3033
3034
3035 /* **************************************************************** */
3036 /* */
3037 /* Kill commands */
3038 /* */
3039 /* **************************************************************** */
3040
3041 /* The next two functions mimic unix line editing behaviour, except they
3042 save the deleted text on the kill ring. This is safer than not saving
3043 it, and since we have a ring, nobody should get screwed. */
3044
3045 /* This does what C-w does in Unix. We can't prevent people from
3046 using behaviour that they expect. */
rl_unix_word_rubout()3047 rl_unix_word_rubout ()
3048 {
3049 if (!rl_point) ding ();
3050 else {
3051 int orig_point = rl_point;
3052 while (rl_point && whitespace (the_line[rl_point - 1]))
3053 rl_point--;
3054 while (rl_point && !whitespace (the_line[rl_point - 1]))
3055 rl_point--;
3056 rl_kill_text (rl_point, orig_point);
3057 }
3058 }
3059
3060 /* Here is C-u doing what Unix does. You don't *have* to use these
3061 key-bindings. We have a choice of killing the entire line, or
3062 killing from where we are to the start of the line. We choose the
3063 latter, because if you are a Unix weenie, then you haven't backspaced
3064 into the line at all, and if you aren't, then you know what you are
3065 doing. */
rl_unix_line_discard()3066 rl_unix_line_discard ()
3067 {
3068 if (!rl_point) ding ();
3069 else {
3070 rl_kill_text (rl_point, 0);
3071 rl_point = 0;
3072 }
3073 }
3074
3075
3076
3077 /* **************************************************************** */
3078 /* */
3079 /* Commands For Typos */
3080 /* */
3081 /* **************************************************************** */
3082
3083 /* Random and interesting things in here. */
3084
3085 /* **************************************************************** */
3086 /* */
3087 /* Changing Case */
3088 /* */
3089 /* **************************************************************** */
3090
3091 /* The three kinds of things that we know how to do. */
3092 #define UpCase 1
3093 #define DownCase 2
3094 #define CapCase 3
3095
3096 /* Uppercase the word at point. */
rl_upcase_word(count)3097 rl_upcase_word (count)
3098 int count;
3099 {
3100 rl_change_case (count, UpCase);
3101 }
3102
3103 /* Lowercase the word at point. */
rl_downcase_word(count)3104 rl_downcase_word (count)
3105 int count;
3106 {
3107 rl_change_case (count, DownCase);
3108 }
3109
3110 /* Upcase the first letter, downcase the rest. */
rl_capitalize_word(count)3111 rl_capitalize_word (count)
3112 int count;
3113 {
3114 rl_change_case (count, CapCase);
3115 }
3116
3117 /* The meaty function.
3118 Change the case of COUNT words, performing OP on them.
3119 OP is one of UpCase, DownCase, or CapCase.
3120 If a negative argument is given, leave point where it started,
3121 otherwise, leave it where it moves to. */
rl_change_case(count,op)3122 rl_change_case (count, op)
3123 int count, op;
3124 {
3125 register int start = rl_point, end;
3126 int state = 0;
3127
3128 rl_forward_word (count);
3129 end = rl_point;
3130
3131 if (count < 0)
3132 {
3133 int temp = start;
3134 start = end;
3135 end = temp;
3136 }
3137
3138 /* We are going to modify some text, so let's prepare to undo it. */
3139 rl_modifying (start, end);
3140
3141 for (; start < end; start++)
3142 {
3143 switch (op)
3144 {
3145 case UpCase:
3146 the_line[start] = to_upper (the_line[start]);
3147 break;
3148
3149 case DownCase:
3150 the_line[start] = to_lower (the_line[start]);
3151 break;
3152
3153 case CapCase:
3154 if (state == 0)
3155 {
3156 the_line[start] = to_upper (the_line[start]);
3157 state = 1;
3158 }
3159 else
3160 {
3161 the_line[start] = to_lower (the_line[start]);
3162 }
3163 if (!pure_alphabetic (the_line[start]))
3164 state = 0;
3165 break;
3166
3167 default:
3168 abort ();
3169 }
3170 }
3171 rl_point = end;
3172 }
3173
3174 /* **************************************************************** */
3175 /* */
3176 /* Transposition */
3177 /* */
3178 /* **************************************************************** */
3179
3180 /* Transpose the words at point. */
rl_transpose_words(count)3181 rl_transpose_words (count)
3182 int count;
3183 {
3184 char *word1, *word2;
3185 int w1_beg, w1_end, w2_beg, w2_end;
3186 int orig_point = rl_point;
3187
3188 if (!count) return;
3189
3190 /* Find the two words. */
3191 rl_forward_word (count);
3192 w2_end = rl_point;
3193 rl_backward_word (1);
3194 w2_beg = rl_point;
3195 rl_backward_word (count);
3196 w1_beg = rl_point;
3197 rl_forward_word (1);
3198 w1_end = rl_point;
3199
3200 /* Do some check to make sure that there really are two words. */
3201 if ((w1_beg == w2_beg) || (w2_beg < w1_end))
3202 {
3203 ding ();
3204 rl_point = orig_point;
3205 return;
3206 }
3207
3208 /* Get the text of the words. */
3209 word1 = rl_copy (w1_beg, w1_end);
3210 word2 = rl_copy (w2_beg, w2_end);
3211
3212 /* We are about to do many insertions and deletions. Remember them
3213 as one operation. */
3214 rl_begin_undo_group ();
3215
3216 /* Do the stuff at word2 first, so that we don't have to worry
3217 about word1 moving. */
3218 rl_point = w2_beg;
3219 rl_delete_text (w2_beg, w2_end);
3220 rl_insert_text (word1);
3221
3222 rl_point = w1_beg;
3223 rl_delete_text (w1_beg, w1_end);
3224 rl_insert_text (word2);
3225
3226 /* This is exactly correct since the text before this point has not
3227 changed in length. */
3228 rl_point = w2_end;
3229
3230 /* I think that does it. */
3231 rl_end_undo_group ();
3232 free (word1); free (word2);
3233 }
3234
3235 /* Transpose the characters at point. If point is at the end of the line,
3236 then transpose the characters before point. */
rl_transpose_chars(count)3237 rl_transpose_chars (count)
3238 int count;
3239 {
3240 if (!count)
3241 return;
3242
3243 if (!rl_point || rl_end < 2) {
3244 ding ();
3245 return;
3246 }
3247
3248 while (count) {
3249 if (rl_point == rl_end) {
3250 int t = the_line[rl_point - 1];
3251 the_line[rl_point - 1] = the_line[rl_point - 2];
3252 the_line[rl_point - 2] = t;
3253 } else {
3254 int t = the_line[rl_point];
3255 the_line[rl_point] = the_line[rl_point - 1];
3256 the_line[rl_point - 1] = t;
3257 if (count < 0 && rl_point)
3258 rl_point--;
3259 else
3260 rl_point++;
3261 }
3262 if (count < 0)
3263 count++;
3264 else
3265 count--;
3266 }
3267 }
3268
3269
3270 /* **************************************************************** */
3271 /* */
3272 /* Bogus Flow Control */
3273 /* */
3274 /* **************************************************************** */
3275
rl_restart_output(count,key)3276 rl_restart_output (count, key)
3277 int count, key;
3278 {
3279 int fildes = fileno (stdin);
3280 #if defined (TIOCSTART)
3281 #if defined (apollo)
3282 ioctl (&fildes, TIOCSTART, 0);
3283 #else
3284 ioctl (fildes, TIOCSTART, 0);
3285 #endif /* apollo */
3286
3287 #else
3288 # if defined (TCXONC)
3289 ioctl (fildes, TCXONC, TCOON);
3290 # endif /* TCXONC */
3291 #endif /* TIOCSTART */
3292 }
3293
3294 /* **************************************************************** */
3295 /* */
3296 /* Completion matching, from readline's point of view. */
3297 /* */
3298 /* **************************************************************** */
3299
3300 /* Pointer to the generator function for completion_matches ().
3301 NULL means to use filename_entry_function (), the default filename
3302 completer. */
3303 Function *rl_completion_entry_function = (Function *)NULL;
3304
3305 /* Pointer to alternative function to create matches.
3306 Function is called with TEXT, START, and END.
3307 START and END are indices in RL_LINE_BUFFER saying what the boundaries
3308 of TEXT are.
3309 If this function exists and returns NULL then call the value of
3310 rl_completion_entry_function to try to match, otherwise use the
3311 array of strings returned. */
3312 Function *rl_attempted_completion_function = (Function *)NULL;
3313
3314 /* Complete the word at or before point. You have supplied the function
3315 that does the initial simple matching selection algorithm (see
3316 completion_matches ()). The default is to do filename completion. */
rl_complete(ignore,invoking_key)3317 rl_complete (ignore, invoking_key)
3318 int ignore, invoking_key;
3319 {
3320 if (rl_last_func == rl_complete)
3321 rl_complete_internal ('?');
3322 else
3323 rl_complete_internal (TAB);
3324 }
3325
3326 /* List the possible completions. See description of rl_complete (). */
rl_possible_completions()3327 rl_possible_completions ()
3328 {
3329 rl_complete_internal ('?');
3330 }
3331
3332 /* The user must press "y" or "n". Non-zero return means "y" pressed. */
get_y_or_n()3333 get_y_or_n ()
3334 {
3335 int c;
3336 loop:
3337 c = rl_read_key ();
3338 if (c == 'y' || c == 'Y') return (1);
3339 if (c == 'n' || c == 'N') return (0);
3340 if (c == ABORT_CHAR) rl_abort ();
3341 ding (); goto loop;
3342 }
3343
3344 /* Up to this many items will be displayed in response to a
3345 possible-completions call. After that, we ask the user if
3346 she is sure she wants to see them all. */
3347 int rl_completion_query_items = 100;
3348
3349 /* The basic list of characters that signal a break between words for the
3350 completer routine. The contents of this variable is what breaks words
3351 in the shell, i.e. " \t\n\"\\'`@$><=" */
3352 char *rl_basic_word_break_characters = " \t\n\"\\'`@$><=";
3353
3354 /* The list of characters that signal a break between words for
3355 rl_complete_internal. The default list is the contents of
3356 rl_basic_word_break_characters. */
3357 char *rl_completer_word_break_characters = (char *)NULL;
3358
3359 /* List of characters that are word break characters, but should be left
3360 in TEXT when it is passed to the completion function. The shell uses
3361 this to help determine what kind of completing to do. */
3362 char *rl_special_prefixes = (char *)NULL;
3363
3364 /* If non-zero, then disallow duplicates in the matches. */
3365 int rl_ignore_completion_duplicates = 1;
3366
3367 /* Non-zero means that the results of the matches are to be treated
3368 as filenames. This is ALWAYS zero on entry, and can only be changed
3369 within a completion entry finder function. */
3370 int rl_filename_completion_desired = 0;
3371
3372 /* This function, if defined, is called by the completer when real
3373 filename completion is done, after all the matching names have been
3374 generated. It is passed a (char**) known as matches in the code below.
3375 It consists of a NULL-terminated array of pointers to potential
3376 matching strings. The 1st element (matches[0]) is the maximal
3377 substring that is common to all matches. This function can re-arrange
3378 the list of matches as required, but all elements of the array must be
3379 free()'d if they are deleted. The main intent of this function is
3380 to implement FIGNORE a la SunOS csh. */
3381 Function *rl_ignore_some_completions_function = (Function *)NULL;
3382
3383 /* Complete the word at or before point.
3384 WHAT_TO_DO says what to do with the completion.
3385 `?' means list the possible completions.
3386 TAB means do standard completion.
3387 `*' means insert all of the possible completions. */
rl_complete_internal(what_to_do)3388 rl_complete_internal (what_to_do)
3389 int what_to_do;
3390 {
3391 char *filename_completion_function ();
3392 char **completion_matches (), **matches;
3393 Function *our_func;
3394 int start, end, delimiter = 0;
3395 char *text;
3396
3397 if (rl_completion_entry_function)
3398 our_func = rl_completion_entry_function;
3399 else
3400 our_func = (int (*)())filename_completion_function;
3401
3402 /* Only the completion entry function can change this. */
3403 rl_filename_completion_desired = 0;
3404
3405 /* We now look backwards for the start of a filename/variable word. */
3406 end = rl_point;
3407
3408 if (rl_point)
3409 {
3410 while (--rl_point &&
3411 !rindex (rl_completer_word_break_characters, the_line[rl_point]));
3412
3413 /* If we are at a word break, then advance past it. */
3414 if (rindex (rl_completer_word_break_characters, (the_line[rl_point])))
3415 {
3416 /* If the character that caused the word break was a quoting
3417 character, then remember it as the delimiter. */
3418 if (rindex ("\"'", the_line[rl_point]) && (end - rl_point) > 1)
3419 delimiter = the_line[rl_point];
3420
3421 /* If the character isn't needed to determine something special
3422 about what kind of completion to perform, then advance past it. */
3423
3424 if (!rl_special_prefixes ||
3425 !rindex (rl_special_prefixes, the_line[rl_point]))
3426 rl_point++;
3427 }
3428 }
3429
3430 start = rl_point;
3431 rl_point = end;
3432 text = rl_copy (start, end);
3433
3434 /* If the user wants to TRY to complete, but then wants to give
3435 up and use the default completion function, they set the
3436 variable rl_attempted_completion_function. */
3437 if (rl_attempted_completion_function)
3438 {
3439 matches =
3440 (char **)(*rl_attempted_completion_function) (text, start, end);
3441
3442 if (matches)
3443 {
3444 our_func = (Function *)NULL;
3445 goto after_usual_completion;
3446 }
3447 }
3448
3449 matches = completion_matches (text, our_func);
3450
3451 after_usual_completion:
3452 free (text);
3453
3454 if (!matches)
3455 ding ();
3456 else
3457 {
3458 register int i;
3459
3460 some_matches:
3461
3462 /* It seems to me that in all the cases we handle we would like
3463 to ignore duplicate possiblilities. Scan for the text to
3464 insert being identical to the other completions. */
3465 if (rl_ignore_completion_duplicates)
3466 {
3467 char *lowest_common;
3468 int j, newlen = 0;
3469
3470 /* Sort the items. */
3471 /* It is safe to sort this array, because the lowest common
3472 denominator found in matches[0] will remain in place. */
3473 for (i = 0; matches[i]; i++);
3474 qsort (matches, i, sizeof (char *), compare_strings);
3475
3476 /* Remember the lowest common denominator for it may be unique. */
3477 lowest_common = savestring (matches[0]);
3478
3479 for (i = 0; matches[i + 1]; i++)
3480 {
3481 if (strcmp (matches[i], matches[i + 1]) == 0)
3482 {
3483 free (matches[i]);
3484 matches[i] = (char *)-1;
3485 }
3486 else
3487 newlen++;
3488 }
3489
3490 /* We have marked all the dead slots with (char *)-1.
3491 Copy all the non-dead entries into a new array. */
3492 {
3493 char **temp_array =
3494 (char **)malloc ((3 + newlen) * sizeof (char *));
3495
3496 for (i = 1, j = 1; matches[i]; i++)
3497 {
3498 if (matches[i] != (char *)-1)
3499 temp_array[j++] = matches[i];
3500 }
3501
3502 temp_array[j] = (char *)NULL;
3503
3504 if (matches[0] != (char *)-1)
3505 free (matches[0]);
3506
3507 free (matches);
3508
3509 matches = temp_array;
3510 }
3511
3512 /* Place the lowest common denominator back in [0]. */
3513 matches[0] = lowest_common;
3514
3515 /* If there is one string left, and it is identical to the
3516 lowest common denominator, then the LCD is the string to
3517 insert. */
3518 if (j == 2 && strcmp (matches[0], matches[1]) == 0)
3519 {
3520 free (matches[1]);
3521 matches[1] = (char *)NULL;
3522 }
3523 }
3524
3525 switch (what_to_do)
3526 {
3527 case TAB:
3528 /* If we are matching filenames, then here is our chance to
3529 do clever processing by re-examining the list. Call the
3530 ignore function with the array as a parameter. It can
3531 munge the array, deleting matches as it desires */
3532 if (rl_ignore_some_completions_function &&
3533 our_func == (int (*)())filename_completion_function)
3534 (void)(*rl_ignore_some_completions_function)(matches);
3535
3536 if (matches[0])
3537 {
3538 rl_delete_text (start, rl_point);
3539 rl_point = start;
3540 rl_insert_text (matches[0]);
3541 }
3542
3543 /* If there are more matches, ring the bell to indicate.
3544 If this was the only match, and we are hacking files,
3545 check the file to see if it was a directory. If so,
3546 add a '/' to the name. If not, and we are at the end
3547 of the line, then add a space. */
3548 if (matches[1])
3549 {
3550 ding (); /* There are other matches remaining. */
3551 }
3552 else
3553 {
3554 char temp_string[2];
3555
3556 temp_string[0] = delimiter ? delimiter : ' ';
3557 temp_string[1] = '\0';
3558
3559 if (rl_filename_completion_desired)
3560 {
3561 struct stat finfo;
3562 char *tilde_expand ();
3563 char *filename = tilde_expand (matches[0]);
3564
3565 if ((stat (filename, &finfo) == 0) &&
3566 ((finfo.st_mode & S_IFMT) == S_IFDIR))
3567 {
3568 if (the_line[rl_point] != '/')
3569 rl_insert_text ("/");
3570 }
3571 else
3572 {
3573 if (rl_point == rl_end)
3574 rl_insert_text (temp_string);
3575 }
3576 free (filename);
3577 }
3578 else
3579 {
3580 if (rl_point == rl_end)
3581 rl_insert_text (temp_string);
3582 }
3583 }
3584 break;
3585
3586 case '*':
3587 {
3588 int i = 1;
3589
3590 rl_delete_text (start, rl_point);
3591 rl_point = start;
3592 rl_begin_undo_group ();
3593 if (matches[1])
3594 {
3595 while (matches[i])
3596 {
3597 rl_insert_text (matches[i++]);
3598 rl_insert_text (" ");
3599 }
3600 }
3601 else
3602 {
3603 rl_insert_text (matches[0]);
3604 rl_insert_text (" ");
3605 }
3606 rl_end_undo_group ();
3607 }
3608 break;
3609
3610 case '?':
3611 {
3612 int len, count, limit, max = 0;
3613 int j, k, l;
3614
3615 /* Handle simple case first. What if there is only one answer? */
3616 if (!matches[1])
3617 {
3618 char *rindex (), *temp;
3619
3620 if (rl_filename_completion_desired)
3621 temp = rindex (matches[0], '/');
3622 else
3623 temp = (char *)NULL;
3624
3625 if (!temp)
3626 temp = matches[0];
3627 else
3628 temp++;
3629
3630 crlf ();
3631 fprintf (out_stream, "%s", temp);
3632 crlf ();
3633 goto restart;
3634 }
3635
3636 /* There is more than one answer. Find out how many there are,
3637 and find out what the maximum printed length of a single entry
3638 is. */
3639 for (i = 1; matches[i]; i++)
3640 {
3641 char *rindex (), *temp = (char *)NULL;
3642
3643 /* If we are hacking filenames, then only count the characters
3644 after the last slash in the pathname. */
3645 if (rl_filename_completion_desired)
3646 temp = rindex (matches[i], '/');
3647 else
3648 temp = (char *)NULL;
3649
3650 if (!temp)
3651 temp = matches[i];
3652 else
3653 temp++;
3654
3655 if (strlen (temp) > max)
3656 max = strlen (temp);
3657 }
3658
3659 len = i;
3660
3661 /* If there are many items, then ask the user if she
3662 really wants to see them all. */
3663 if (len >= rl_completion_query_items)
3664 {
3665 crlf ();
3666 fprintf (out_stream,
3667 "There are %d possibilities. Do you really", len);
3668 crlf ();
3669 fprintf (out_stream, "wish to see them all? (y or n)");
3670 fflush (out_stream);
3671 if (!get_y_or_n ())
3672 {
3673 crlf ();
3674 goto restart;
3675 }
3676 }
3677 /* How many items of MAX length can we fit in the screen window? */
3678 max += 2;
3679 limit = screenwidth / max;
3680 if (limit != 1 && (limit * max == screenwidth))
3681 limit--;
3682
3683 /* Avoid a possible floating exception. If max > screenwidth,
3684 limit will be 0 and a divide-by-zero fault will result. */
3685 if (limit == 0)
3686 limit = 1;
3687
3688 /* How many iterations of the printing loop? */
3689 count = (len + (limit - 1)) / limit;
3690
3691 /* Watch out for special case. If LEN is less than LIMIT, then
3692 just do the inner printing loop. */
3693 if (len < limit) count = 1;
3694
3695 /* Sort the items if they are not already sorted. */
3696 if (!rl_ignore_completion_duplicates)
3697 qsort (matches, len, sizeof (char *), compare_strings);
3698
3699 /* Print the sorted items, up-and-down alphabetically, like
3700 ls might. */
3701 crlf ();
3702
3703 for (i = 1; i < count + 1; i++)
3704 {
3705 for (j = 0, l = i; j < limit; j++)
3706 {
3707 if (l > len || !matches[l])
3708 {
3709 break;
3710 }
3711 else
3712 {
3713 char *rindex (), *temp = (char *)NULL;
3714
3715 if (rl_filename_completion_desired)
3716 temp = rindex (matches[l], '/');
3717 else
3718 temp = (char *)NULL;
3719
3720 if (!temp)
3721 temp = matches[l];
3722 else
3723 temp++;
3724
3725 fprintf (out_stream, "%s", temp);
3726 for (k = 0; k < max - strlen (temp); k++)
3727 putc (' ', out_stream);
3728 }
3729 l += count;
3730 }
3731 crlf ();
3732 }
3733 restart:
3734
3735 rl_on_new_line ();
3736 }
3737 break;
3738
3739 default:
3740 abort ();
3741 }
3742
3743 for (i = 0; matches[i]; i++)
3744 free (matches[i]);
3745 free (matches);
3746 }
3747 }
3748
3749 /* Stupid comparison routine for qsort () ing strings. */
3750 static int
compare_strings(s1,s2)3751 compare_strings (s1, s2)
3752 char **s1, **s2;
3753 {
3754 return (strcmp (*s1, *s2));
3755 }
3756
3757 /* A completion function for usernames.
3758 TEXT contains a partial username preceded by a random
3759 character (usually `~'). */
3760 char *
username_completion_function(text,state)3761 username_completion_function (text, state)
3762 int state;
3763 char *text;
3764 {
3765 static char *username = (char *)NULL;
3766 static struct passwd *entry;
3767 static int namelen, first_char, first_char_loc;
3768
3769 if (!state)
3770 {
3771 if (username)
3772 free (username);
3773
3774 first_char = *text;
3775
3776 if (first_char == '~')
3777 first_char_loc = 1;
3778 else
3779 first_char_loc = 0;
3780
3781 username = savestring (&text[first_char_loc]);
3782 namelen = strlen (username);
3783 setpwent ();
3784 }
3785
3786 while (entry = getpwent ())
3787 {
3788 if (strncmp (username, entry->pw_name, namelen) == 0)
3789 break;
3790 }
3791
3792 if (!entry)
3793 {
3794 endpwent ();
3795 return ((char *)NULL);
3796 }
3797 else
3798 {
3799 char *value = (char *)xmalloc (2 + strlen (entry->pw_name));
3800
3801 *value = *text;
3802
3803 strcpy (value + first_char_loc, entry->pw_name);
3804
3805 if (first_char == '~')
3806 rl_filename_completion_desired = 1;
3807
3808 return (value);
3809 }
3810 }
3811
3812 /* If non-null, this contains the address of a function to call if the
3813 standard meaning for expanding a tilde fails. The function is called
3814 with the text (sans tilde, as in "foo"), and returns a malloc()'ed string
3815 which is the expansion, or a NULL pointer if there is no expansion. */
3816 Function *rl_tilde_expander = (Function *)NULL;
3817
3818 /* Expand FILENAME if it begins with a tilde. This always returns
3819 a new string. */
3820 char *
tilde_expand(filename)3821 tilde_expand (filename)
3822 char *filename;
3823 {
3824 char *dirname = filename ? savestring (filename) : (char *)NULL;
3825
3826 if (dirname && *dirname == '~')
3827 {
3828 char *temp_name;
3829 if (!dirname[1] || dirname[1] == '/')
3830 {
3831 /* Prepend $HOME to the rest of the string. */
3832 char *temp_home = (char *)getenv ("HOME");
3833
3834 temp_name = (char *)alloca (1 + strlen (&dirname[1])
3835 + (temp_home? strlen (temp_home) : 0));
3836 temp_name[0] = '\0';
3837 if (temp_home)
3838 strcpy (temp_name, temp_home);
3839 strcat (temp_name, &dirname[1]);
3840 free (dirname);
3841 dirname = savestring (temp_name);
3842 }
3843 else
3844 {
3845 struct passwd *getpwnam (), *user_entry;
3846 char *username = (char *)alloca (257);
3847 int i, c;
3848
3849 for (i = 1; c = dirname[i]; i++)
3850 {
3851 if (c == '/') break;
3852 else username[i - 1] = c;
3853 }
3854 username[i - 1] = '\0';
3855
3856 if (!(user_entry = getpwnam (username)))
3857 {
3858 /* If the calling program has a special syntax for
3859 expanding tildes, and we couldn't find a standard
3860 expansion, then let them try. */
3861 if (rl_tilde_expander)
3862 {
3863 char *expansion;
3864
3865 expansion = (char *)(*rl_tilde_expander) (username);
3866
3867 if (expansion)
3868 {
3869 temp_name = (char *)alloca (1 + strlen (expansion)
3870 + strlen (&dirname[i]));
3871 strcpy (temp_name, expansion);
3872 strcat (temp_name, &dirname[i]);
3873 free (expansion);
3874 goto return_name;
3875 }
3876 }
3877 /*
3878 * We shouldn't report errors.
3879 */
3880 }
3881 else
3882 {
3883 temp_name = (char *)alloca (1 + strlen (user_entry->pw_dir)
3884 + strlen (&dirname[i]));
3885 strcpy (temp_name, user_entry->pw_dir);
3886 strcat (temp_name, &dirname[i]);
3887 return_name:
3888 free (dirname);
3889 dirname = savestring (temp_name);
3890 }
3891 endpwent ();
3892 }
3893 }
3894 return (dirname);
3895 }
3896
3897
3898 /* **************************************************************** */
3899 /* */
3900 /* Undo, and Undoing */
3901 /* */
3902 /* **************************************************************** */
3903
3904 /* Non-zero tells rl_delete_text and rl_insert_text to not add to
3905 the undo list. */
3906 int doing_an_undo = 0;
3907
3908 /* The current undo list for THE_LINE. */
3909 UNDO_LIST *rl_undo_list = (UNDO_LIST *)NULL;
3910
3911 /* Remember how to undo something. Concatenate some undos if that
3912 seems right. */
3913 rl_add_undo (what, start, end, text)
3914 enum undo_code what;
3915 int start, end;
3916 char *text;
3917 {
3918 UNDO_LIST *temp = (UNDO_LIST *)xmalloc (sizeof (UNDO_LIST));
3919 temp->what = what;
3920 temp->start = start;
3921 temp->end = end;
3922 temp->text = text;
3923 temp->next = rl_undo_list;
3924 rl_undo_list = temp;
3925 }
3926
3927 /* Free the existing undo list. */
free_undo_list()3928 free_undo_list ()
3929 {
3930 while (rl_undo_list) {
3931 UNDO_LIST *release = rl_undo_list;
3932 rl_undo_list = rl_undo_list->next;
3933
3934 if (release->what == UNDO_DELETE)
3935 free (release->text);
3936
3937 free (release);
3938 }
3939 }
3940
3941 /* Undo the next thing in the list. Return 0 if there
3942 is nothing to undo, or non-zero if there was. */
3943 int
rl_do_undo()3944 rl_do_undo ()
3945 {
3946 UNDO_LIST *release;
3947 int waiting_for_begin = 0;
3948
3949 undo_thing:
3950 if (!rl_undo_list)
3951 return (0);
3952
3953 doing_an_undo = 1;
3954
3955 switch (rl_undo_list->what) {
3956
3957 /* Undoing deletes means inserting some text. */
3958 case UNDO_DELETE:
3959 rl_point = rl_undo_list->start;
3960 rl_insert_text (rl_undo_list->text);
3961 free (rl_undo_list->text);
3962 break;
3963
3964 /* Undoing inserts means deleting some text. */
3965 case UNDO_INSERT:
3966 rl_delete_text (rl_undo_list->start, rl_undo_list->end);
3967 rl_point = rl_undo_list->start;
3968 break;
3969
3970 /* Undoing an END means undoing everything 'til we get to
3971 a BEGIN. */
3972 case UNDO_END:
3973 waiting_for_begin++;
3974 break;
3975
3976 /* Undoing a BEGIN means that we are done with this group. */
3977 case UNDO_BEGIN:
3978 if (waiting_for_begin)
3979 waiting_for_begin--;
3980 else
3981 abort ();
3982 break;
3983 }
3984
3985 doing_an_undo = 0;
3986
3987 release = rl_undo_list;
3988 rl_undo_list = rl_undo_list->next;
3989 free (release);
3990
3991 if (waiting_for_begin)
3992 goto undo_thing;
3993
3994 return (1);
3995 }
3996
3997 /* Begin a group. Subsequent undos are undone as an atomic operation. */
rl_begin_undo_group()3998 rl_begin_undo_group ()
3999 {
4000 rl_add_undo (UNDO_BEGIN, 0, 0, 0);
4001 }
4002
4003 /* End an undo group started with rl_begin_undo_group (). */
rl_end_undo_group()4004 rl_end_undo_group ()
4005 {
4006 rl_add_undo (UNDO_END, 0, 0, 0);
4007 }
4008
4009 /* Save an undo entry for the text from START to END. */
rl_modifying(start,end)4010 rl_modifying (start, end)
4011 int start, end;
4012 {
4013 if (start > end)
4014 {
4015 int t = start;
4016 start = end;
4017 end = t;
4018 }
4019
4020 if (start != end)
4021 {
4022 char *temp = rl_copy (start, end);
4023 rl_begin_undo_group ();
4024 rl_add_undo (UNDO_DELETE, start, end, temp);
4025 rl_add_undo (UNDO_INSERT, start, end, (char *)NULL);
4026 rl_end_undo_group ();
4027 }
4028 }
4029
4030 /* Revert the current line to its previous state. */
rl_revert_line()4031 rl_revert_line ()
4032 {
4033 if (!rl_undo_list) ding ();
4034 else {
4035 while (rl_undo_list)
4036 rl_do_undo ();
4037 }
4038 }
4039
4040 /* Do some undoing of things that were done. */
rl_undo_command(count)4041 rl_undo_command (count)
4042 {
4043 if (count < 0) return; /* Nothing to do. */
4044
4045 while (count)
4046 {
4047 if (rl_do_undo ())
4048 {
4049 count--;
4050 }
4051 else
4052 {
4053 ding ();
4054 break;
4055 }
4056 }
4057 }
4058
4059 /* **************************************************************** */
4060 /* */
4061 /* History Utilities */
4062 /* */
4063 /* **************************************************************** */
4064
4065 /* We already have a history library, and that is what we use to control
4066 the history features of readline. However, this is our local interface
4067 to the history mechanism. */
4068
4069 /* While we are editing the history, this is the saved
4070 version of the original line. */
4071 HIST_ENTRY *saved_line_for_history = (HIST_ENTRY *)NULL;
4072
4073 /* Set the history pointer back to the last entry in the history. */
start_using_history()4074 start_using_history ()
4075 {
4076 using_history ();
4077 if (saved_line_for_history)
4078 free_history_entry (saved_line_for_history);
4079
4080 saved_line_for_history = (HIST_ENTRY *)NULL;
4081 }
4082
4083 /* Free the contents (and containing structure) of a HIST_ENTRY. */
free_history_entry(entry)4084 free_history_entry (entry)
4085 HIST_ENTRY *entry;
4086 {
4087 if (!entry) return;
4088 if (entry->line)
4089 free (entry->line);
4090 free (entry);
4091 }
4092
4093 /* Perhaps put back the current line if it has changed. */
maybe_replace_line()4094 maybe_replace_line ()
4095 {
4096 HIST_ENTRY *temp = current_history ();
4097
4098 /* If the current line has changed, save the changes. */
4099 if (temp && ((UNDO_LIST *)(temp->data) != rl_undo_list)) {
4100 temp = replace_history_entry (where_history (), the_line, rl_undo_list);
4101 free (temp->line);
4102 free (temp);
4103 }
4104 }
4105
4106 /* Put back the saved_line_for_history if there is one. */
maybe_unsave_line()4107 maybe_unsave_line ()
4108 {
4109 if (saved_line_for_history) {
4110 strcpy (the_line, saved_line_for_history->line);
4111 rl_undo_list = (UNDO_LIST *)saved_line_for_history->data;
4112 free_history_entry (saved_line_for_history);
4113 saved_line_for_history = (HIST_ENTRY *)NULL;
4114 rl_end = rl_point = strlen (the_line);
4115 } else {
4116 ding ();
4117 }
4118 }
4119
4120 /* Save the current line in saved_line_for_history. */
maybe_save_line()4121 maybe_save_line ()
4122 {
4123 if (!saved_line_for_history) {
4124 saved_line_for_history = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
4125 saved_line_for_history->line = savestring (the_line);
4126 saved_line_for_history->data = (char *)rl_undo_list;
4127 }
4128 }
4129
4130
4131 /* **************************************************************** */
4132 /* */
4133 /* History Commands */
4134 /* */
4135 /* **************************************************************** */
4136
4137 /* Meta-< goes to the start of the history. */
rl_beginning_of_history()4138 rl_beginning_of_history ()
4139 {
4140 rl_get_previous_history (1 + where_history ());
4141 }
4142
4143 /* Meta-> goes to the end of the history. (The current line). */
rl_end_of_history()4144 rl_end_of_history ()
4145 {
4146 maybe_replace_line ();
4147 using_history ();
4148 maybe_unsave_line ();
4149 }
4150
4151 /* Move down to the next history line. */
rl_get_next_history(count)4152 rl_get_next_history (count)
4153 int count;
4154 {
4155 HIST_ENTRY *temp = (HIST_ENTRY *)NULL;
4156
4157 if (count < 0)
4158 {
4159 rl_get_previous_history (-count);
4160 return;
4161 }
4162
4163 if (!count)
4164 return;
4165
4166 maybe_replace_line ();
4167
4168 while (count)
4169 {
4170 temp = next_history ();
4171 if (!temp)
4172 break;
4173 --count;
4174 }
4175
4176 if (!temp)
4177 maybe_unsave_line ();
4178 else
4179 {
4180 strcpy (the_line, temp->line);
4181 rl_undo_list = (UNDO_LIST *)temp->data;
4182 rl_end = rl_point = strlen (the_line);
4183 }
4184 }
4185
4186 /* Get the previous item out of our interactive history, making it the current
4187 line. If there is no previous history, just ding. */
rl_get_previous_history(count)4188 rl_get_previous_history (count)
4189 int count;
4190 {
4191 HIST_ENTRY *old_temp = (HIST_ENTRY *)NULL;
4192 HIST_ENTRY *temp = (HIST_ENTRY *)NULL;
4193
4194 if (count < 0)
4195 {
4196 rl_get_next_history (-count);
4197 return;
4198 }
4199
4200 if (!count)
4201 return;
4202
4203 /* If we don't have a line saved, then save this one. */
4204 maybe_save_line ();
4205
4206 /* If the current line has changed, save the changes. */
4207 maybe_replace_line ();
4208
4209 while (count)
4210 {
4211 temp = previous_history ();
4212 if (!temp)
4213 break;
4214 else
4215 old_temp = temp;
4216 --count;
4217 }
4218
4219 /* If there was a large argument, and we moved back to the start of the
4220 history, that is not an error. So use the last value found. */
4221 if (!temp && old_temp)
4222 temp = old_temp;
4223
4224 if (!temp)
4225 ding ();
4226 else
4227 {
4228 strcpy (the_line, temp->line);
4229 rl_undo_list = (UNDO_LIST *)temp->data;
4230 rl_end = rl_point = strlen (the_line);
4231 #ifdef VI_MODE
4232 if (rl_editing_mode == vi_mode)
4233 rl_point = 0;
4234 #endif /* VI_MODE */
4235 }
4236 }
4237
4238 /* There is a command in the K*rn shell which yanks into this line, the last
4239 word of the previous line. Here it is. We left it on M-. */
rl_yank_previous_last_arg(ignore)4240 rl_yank_previous_last_arg (ignore)
4241 int ignore;
4242 {
4243 }
4244
4245
4246 /* **************************************************************** */
4247 /* */
4248 /* I-Search and Searching */
4249 /* */
4250 /* **************************************************************** */
4251
4252 /* Search backwards through the history looking for a string which is typed
4253 interactively. Start with the current line. */
rl_reverse_search_history(sign,key)4254 rl_reverse_search_history (sign, key)
4255 int sign;
4256 int key;
4257 {
4258 rl_search_history (-sign, key);
4259 }
4260
4261 /* Search forwards through the history looking for a string which is typed
4262 interactively. Start with the current line. */
rl_forward_search_history(sign,key)4263 rl_forward_search_history (sign, key)
4264 int sign;
4265 int key;
4266 {
4267 rl_search_history (sign, key);
4268 }
4269
4270 /* Display the current state of the search in the echo-area.
4271 SEARCH_STRING contains the string that is being searched for,
4272 DIRECTION is zero for forward, or 1 for reverse,
4273 WHERE is the history list number of the current line. If it is
4274 -1, then this line is the starting one. */
rl_display_search(search_string,reverse_p,where)4275 rl_display_search (search_string, reverse_p, where)
4276 char *search_string;
4277 int reverse_p, where;
4278 {
4279 char *message = (char *)NULL;
4280
4281 message =
4282 (char *)alloca (1 + (search_string ? strlen (search_string) : 0) + 30);
4283
4284 *message = '\0';
4285
4286 #ifdef NEVER
4287 if (where != -1)
4288 sprintf (message, "[%d]", where + history_base);
4289 #endif
4290
4291 strcat (message, "(");
4292
4293 if (reverse_p)
4294 strcat (message, "reverse-");
4295
4296 strcat (message, "i-search)`");
4297
4298 if (search_string)
4299 strcat (message, search_string);
4300
4301 strcat (message, "': ");
4302 rl_message (message, 0, 0);
4303 rl_redisplay ();
4304 }
4305
4306 /* Search through the history looking for an interactively typed string.
4307 This is analogous to i-search. We start the search in the current line.
4308 DIRECTION is which direction to search; > 0 means forward, < 0 means
4309 backwards. */
rl_search_history(direction,invoking_key)4310 rl_search_history (direction, invoking_key)
4311 int direction;
4312 int invoking_key;
4313 {
4314 /* The string that the user types in to search for. */
4315 char *search_string = (char *)alloca (128);
4316
4317 /* The current length of SEARCH_STRING. */
4318 int search_string_index;
4319
4320 /* The list of lines to search through. */
4321 char **lines;
4322
4323 /* The length of LINES. */
4324 int hlen;
4325
4326 /* Where we get LINES from. */
4327 HIST_ENTRY **hlist = history_list ();
4328
4329 register int i = 0;
4330 int orig_point = rl_point;
4331 int orig_line = where_history ();
4332 int last_found_line = orig_line;
4333 int c, done = 0;
4334
4335 /* The line currently being searched. */
4336 char *sline;
4337
4338 /* Offset in that line. */
4339 int index;
4340
4341 /* Non-zero if we are doing a reverse search. */
4342 int reverse = (direction < 0);
4343
4344 /* Create an arrary of pointers to the lines that we want to search. */
4345
4346 maybe_replace_line ();
4347 if (hlist)
4348 for (i = 0; hlist[i]; i++);
4349
4350 /* Allocate space for this many lines, +1 for the current input line,
4351 and remember those lines. */
4352 lines = (char **)alloca ((1 + (hlen = i)) * sizeof (char *));
4353 for (i = 0; i < hlen; i++)
4354 lines[i] = hlist[i]->line;
4355
4356 if (saved_line_for_history)
4357 lines[i] = saved_line_for_history->line;
4358 else
4359 {
4360 /* So I have to type it in this way instead. */
4361 lines[i] = (char *)alloca (1 + strlen (the_line));
4362 strcpy (lines[i], &the_line[0]);
4363 }
4364
4365 hlen++;
4366
4367 /* The line where we start the search. */
4368 i = orig_line;
4369
4370 /* Initialize search parameters. */
4371 *search_string = '\0';
4372 search_string_index = 0;
4373
4374 rl_display_search (search_string, reverse, -1);
4375
4376 sline = the_line;
4377 index = rl_point;
4378
4379 while (!done)
4380 {
4381 c = rl_read_key ();
4382
4383 /* Hack C to Do What I Mean. */
4384 {
4385 Function *f = (Function *)NULL;
4386
4387 if (keymap[c].type == ISFUNC)
4388 f = keymap[c].function;
4389
4390 if (f == rl_reverse_search_history)
4391 c = reverse ? -1 : -2;
4392 else if (f == rl_forward_search_history)
4393 c = !reverse ? -1 : -2;
4394 }
4395
4396 switch (c)
4397 {
4398 case ESC:
4399 done = 1;
4400 continue;
4401
4402 /* case invoking_key: */
4403 case -1:
4404 goto search_again;
4405
4406 /* switch directions */
4407 case -2:
4408 direction = -direction;
4409 reverse = (direction < 0);
4410
4411 goto do_search;
4412
4413 case CTRL ('G'):
4414 strcpy (the_line, lines[orig_line]);
4415 rl_point = orig_point;
4416 rl_end = strlen (the_line);
4417 rl_clear_message ();
4418 return;
4419
4420 default:
4421 if (c < 32 || c > 126)
4422 {
4423 rl_execute_next (c);
4424 done = 1;
4425 continue;
4426 }
4427 else
4428 {
4429 search_string[search_string_index++] = c;
4430 search_string[search_string_index] = '\0';
4431 goto do_search;
4432
4433 search_again:
4434
4435 if (!search_string_index)
4436 continue;
4437 else
4438 {
4439 if (reverse)
4440 --index;
4441 else
4442 if (index != strlen (sline))
4443 ++index;
4444 else
4445 ding ();
4446 }
4447 do_search:
4448
4449 while (1)
4450 {
4451 if (reverse)
4452 {
4453 while (index >= 0)
4454 if (strncmp
4455 (search_string,
4456 sline + index,
4457 search_string_index) == 0)
4458 goto string_found;
4459 else
4460 index--;
4461 }
4462 else
4463 {
4464 register int limit =
4465 (strlen (sline) - search_string_index) + 1;
4466
4467 while (index < limit)
4468 {
4469 if (strncmp (search_string,
4470 sline + index,
4471 search_string_index) == 0)
4472 goto string_found;
4473 index++;
4474 }
4475 }
4476
4477 next_line:
4478 i += direction;
4479
4480 /* At limit for direction? */
4481 if ((reverse && i < 0) ||
4482 (!reverse && i == hlen))
4483 goto search_failed;
4484
4485 sline = lines[i];
4486 if (reverse)
4487 index = strlen (sline);
4488 else
4489 index = 0;
4490
4491 /* If the search string is longer than the current
4492 line, no match. */
4493 if (search_string_index > strlen (sline))
4494 goto next_line;
4495
4496 /* Start actually searching. */
4497 if (reverse)
4498 index -= search_string_index;
4499 }
4500
4501 search_failed:
4502 /* We cannot find the search string. Ding the bell. */
4503 ding ();
4504 i = last_found_line;
4505 break;
4506
4507 string_found:
4508 /* We have found the search string. Just display it. But don't
4509 actually move there in the history list until the user accepts
4510 the location. */
4511 strcpy (the_line, lines[i]);
4512 rl_point = index;
4513 rl_end = strlen (the_line);
4514 last_found_line = i;
4515 rl_display_search (search_string, reverse,
4516 (i == orig_line) ? -1 : i);
4517 }
4518 }
4519 continue;
4520 }
4521
4522 /* The searching is over. The user may have found the string that she
4523 was looking for, or else she may have exited a failing search. If
4524 INDEX is -1, then that shows that the string searched for was not
4525 found. We use this to determine where to place rl_point. */
4526 {
4527 int now = last_found_line;
4528
4529 /* First put back the original state. */
4530 strcpy (the_line, lines[orig_line]);
4531
4532 if (now < orig_line)
4533 rl_get_previous_history (orig_line - now);
4534 else
4535 rl_get_next_history (now - orig_line);
4536
4537 /* If the index of the "matched" string is less than zero, then the
4538 final search string was never matched, so put point somewhere
4539 reasonable. */
4540 if (index < 0)
4541 index = strlen (the_line);
4542
4543 rl_point = index;
4544 rl_clear_message ();
4545 }
4546 }
4547
4548 /* Make C be the next command to be executed. */
rl_execute_next(c)4549 rl_execute_next (c)
4550 int c;
4551 {
4552 rl_pending_input = c;
4553 }
4554
4555 /* **************************************************************** */
4556 /* */
4557 /* Killing Mechanism */
4558 /* */
4559 /* **************************************************************** */
4560
4561 /* What we assume for a max number of kills. */
4562 #define DEFAULT_MAX_KILLS 10
4563
4564 /* The real variable to look at to find out when to flush kills. */
4565 int rl_max_kills = DEFAULT_MAX_KILLS;
4566
4567 /* Where to store killed text. */
4568 char **rl_kill_ring = (char **)NULL;
4569
4570 /* Where we are in the kill ring. */
4571 int rl_kill_index = 0;
4572
4573 /* How many slots we have in the kill ring. */
4574 int rl_kill_ring_length = 0;
4575
4576 /* How to say that you only want to save a certain amount
4577 of kill material. */
rl_set_retained_kills(num)4578 rl_set_retained_kills (num)
4579 int num;
4580 {}
4581
4582 /* The way to kill something. This appends or prepends to the last
4583 kill, if the last command was a kill command. if FROM is less
4584 than TO, then the text is appended, otherwise prepended. If the
4585 last command was not a kill command, then a new slot is made for
4586 this kill. */
rl_kill_text(from,to)4587 rl_kill_text (from, to)
4588 int from, to;
4589 {
4590 int slot;
4591 char *text = rl_copy (from, to);
4592
4593 /* Is there anything to kill? */
4594 if (from == to)
4595 {
4596 free (text);
4597 last_command_was_kill++;
4598 return;
4599 }
4600
4601 /* Delete the copied text from the line. */
4602 rl_delete_text (from, to);
4603
4604 /* First, find the slot to work with. */
4605 if (!last_command_was_kill)
4606 {
4607 /* Get a new slot. */
4608 if (!rl_kill_ring)
4609 {
4610 /* If we don't have any defined, then make one. */
4611 rl_kill_ring = (char **)
4612 xmalloc (((rl_kill_ring_length = 1) + 1) * sizeof (char *));
4613 slot = 1;
4614 }
4615 else
4616 {
4617 /* We have to add a new slot on the end, unless we have
4618 exceeded the max limit for remembering kills. */
4619 slot = rl_kill_ring_length;
4620 if (slot == rl_max_kills)
4621 {
4622 register int i;
4623 free (rl_kill_ring[0]);
4624 for (i = 0; i < slot; i++)
4625 rl_kill_ring[i] = rl_kill_ring[i + 1];
4626 }
4627 else
4628 {
4629 rl_kill_ring =
4630 (char **)
4631 xrealloc (rl_kill_ring,
4632 ((slot = (rl_kill_ring_length += 1)) + 1)
4633 * sizeof (char *));
4634 }
4635 }
4636 slot--;
4637 }
4638 else
4639 {
4640 slot = rl_kill_ring_length - 1;
4641 }
4642
4643 /* If the last command was a kill, prepend or append. */
4644 if (last_command_was_kill && rl_editing_mode != vi_mode)
4645 {
4646 char *old = rl_kill_ring[slot];
4647 char *new = (char *)xmalloc (1 + strlen (old) + strlen (text));
4648
4649 if (from < to)
4650 {
4651 strcpy (new, old);
4652 strcat (new, text);
4653 }
4654 else
4655 {
4656 strcpy (new, text);
4657 strcat (new, old);
4658 }
4659 free (old);
4660 free (text);
4661 rl_kill_ring[slot] = new;
4662 }
4663 else
4664 {
4665 rl_kill_ring[slot] = text;
4666 }
4667 rl_kill_index = slot;
4668 last_command_was_kill++;
4669 }
4670
4671 /* Now REMEMBER! In order to do prepending or appending correctly, kill
4672 commands always make rl_point's original position be the FROM argument,
4673 and rl_point's extent be the TO argument. */
4674
4675 /* **************************************************************** */
4676 /* */
4677 /* Killing Commands */
4678 /* */
4679 /* **************************************************************** */
4680
4681 /* Delete the word at point, saving the text in the kill ring. */
rl_kill_word(count)4682 rl_kill_word (count)
4683 int count;
4684 {
4685 int orig_point = rl_point;
4686
4687 if (count < 0)
4688 rl_backward_kill_word (-count);
4689 else
4690 {
4691 rl_forward_word (count);
4692
4693 if (rl_point != orig_point)
4694 rl_kill_text (orig_point, rl_point);
4695
4696 rl_point = orig_point;
4697 }
4698 }
4699
4700 /* Rubout the word before point, placing it on the kill ring. */
rl_backward_kill_word(count)4701 rl_backward_kill_word (count)
4702 int count;
4703 {
4704 int orig_point = rl_point;
4705
4706 if (count < 0)
4707 rl_kill_word (-count);
4708 else
4709 {
4710 rl_backward_word (count);
4711
4712 if (rl_point != orig_point)
4713 rl_kill_text (orig_point, rl_point);
4714 }
4715 }
4716
4717 /* Kill from here to the end of the line. If DIRECTION is negative, kill
4718 back to the line start instead. */
rl_kill_line(direction)4719 rl_kill_line (direction)
4720 int direction;
4721 {
4722 int orig_point = rl_point;
4723
4724 if (direction < 0)
4725 rl_backward_kill_line (1);
4726 else
4727 {
4728 rl_end_of_line ();
4729 if (orig_point != rl_point)
4730 rl_kill_text (orig_point, rl_point);
4731 rl_point = orig_point;
4732 }
4733 }
4734
4735 /* Kill backwards to the start of the line. If DIRECTION is negative, kill
4736 forwards to the line end instead. */
rl_backward_kill_line(direction)4737 rl_backward_kill_line (direction)
4738 int direction;
4739 {
4740 int orig_point = rl_point;
4741
4742 if (direction < 0)
4743 rl_kill_line (1);
4744 else
4745 {
4746 if (!rl_point)
4747 ding ();
4748 else
4749 {
4750 rl_beg_of_line ();
4751 rl_kill_text (orig_point, rl_point);
4752 }
4753 }
4754 }
4755
4756 /* Yank back the last killed text. This ignores arguments. */
rl_yank()4757 rl_yank ()
4758 {
4759 if (!rl_kill_ring) rl_abort ();
4760 rl_insert_text (rl_kill_ring[rl_kill_index]);
4761 }
4762
4763 /* If the last command was yank, or yank_pop, and the text just
4764 before point is identical to the current kill item, then
4765 delete that text from the line, rotate the index down, and
4766 yank back some other text. */
rl_yank_pop()4767 rl_yank_pop ()
4768 {
4769 int l;
4770
4771 if (((rl_last_func != rl_yank_pop) && (rl_last_func != rl_yank)) ||
4772 !rl_kill_ring)
4773 {
4774 rl_abort ();
4775 }
4776
4777 l = strlen (rl_kill_ring[rl_kill_index]);
4778 if (((rl_point - l) >= 0) &&
4779 (strncmp (the_line + (rl_point - l),
4780 rl_kill_ring[rl_kill_index], l) == 0))
4781 {
4782 rl_delete_text ((rl_point - l), rl_point);
4783 rl_point -= l;
4784 rl_kill_index--;
4785 if (rl_kill_index < 0)
4786 rl_kill_index = rl_kill_ring_length - 1;
4787 rl_yank ();
4788 }
4789 else
4790 rl_abort ();
4791
4792 }
4793
4794 /* Yank the COUNTth argument from the previous history line. */
rl_yank_nth_arg(count,ignore)4795 rl_yank_nth_arg (count, ignore)
4796 int count;
4797 {
4798 register HIST_ENTRY *entry = previous_history ();
4799 char *arg;
4800
4801 if (entry)
4802 next_history ();
4803 else
4804 {
4805 ding ();
4806 return;
4807 }
4808
4809 arg = history_arg_extract (count, count, entry->line);
4810 if (!arg || !*arg)
4811 {
4812 ding ();
4813 return;
4814 }
4815
4816 rl_begin_undo_group ();
4817 if (rl_point && the_line[rl_point - 1] != ' ')
4818 rl_insert_text (" ");
4819 rl_insert_text (arg);
4820 free (arg);
4821 rl_end_undo_group ();
4822 }
4823
4824 /* Vi Mode. */
4825 #ifdef VI_MODE
4826 #include "vi_mode.c"
4827 #endif /* VI_MODE */
4828
4829 /* How to toggle back and forth between editing modes. */
rl_vi_editing_mode()4830 rl_vi_editing_mode ()
4831 {
4832 #ifdef VI_MODE
4833 rl_editing_mode = vi_mode;
4834 rl_vi_insertion_mode ();
4835 #endif /* VI_MODE */
4836 }
4837
rl_emacs_editing_mode()4838 rl_emacs_editing_mode ()
4839 {
4840 rl_editing_mode = emacs_mode;
4841 keymap = emacs_standard_keymap;
4842 }
4843
4844
4845 /* **************************************************************** */
4846 /* */
4847 /* Completion */
4848 /* */
4849 /* **************************************************************** */
4850
4851 /* Non-zero means that case is not significant in completion. */
4852 int completion_case_fold = 0;
4853
4854 /* Return an array of (char *) which is a list of completions for TEXT.
4855 If there are no completions, return a NULL pointer.
4856 The first entry in the returned array is the substitution for TEXT.
4857 The remaining entries are the possible completions.
4858 The array is terminated with a NULL pointer.
4859
4860 ENTRY_FUNCTION is a function of two args, and returns a (char *).
4861 The first argument is TEXT.
4862 The second is a state argument; it should be zero on the first call, and
4863 non-zero on subsequent calls. It returns a NULL pointer to the caller
4864 when there are no more matches.
4865 */
4866 char **
completion_matches(text,entry_function)4867 completion_matches (text, entry_function)
4868 char *text;
4869 char *(*entry_function) ();
4870 {
4871 /* Number of slots in match_list. */
4872 int match_list_size;
4873
4874 /* The list of matches. */
4875 char **match_list =
4876 (char **)xmalloc (((match_list_size = 10) + 1) * sizeof (char *));
4877
4878 /* Number of matches actually found. */
4879 int matches = 0;
4880
4881 /* Temporary string binder. */
4882 char *string;
4883
4884 match_list[1] = (char *)NULL;
4885
4886 while (string = (*entry_function) (text, matches))
4887 {
4888 if (matches + 1 == match_list_size)
4889 match_list =
4890 (char **)xrealloc (match_list,
4891 ((match_list_size += 10) + 1) * sizeof (char *));
4892
4893 match_list[++matches] = string;
4894 match_list[matches + 1] = (char *)NULL;
4895 }
4896
4897 /* If there were any matches, then look through them finding out the
4898 lowest common denominator. That then becomes match_list[0]. */
4899 if (matches)
4900 {
4901 register int i = 1;
4902 int low = 100000; /* Count of max-matched characters. */
4903
4904 /* If only one match, just use that. */
4905 if (matches == 1)
4906 {
4907 match_list[0] = match_list[1];
4908 match_list[1] = (char *)NULL;
4909 }
4910 else
4911 {
4912 /* Otherwise, compare each member of the list with
4913 the next, finding out where they stop matching. */
4914
4915 while (i < matches)
4916 {
4917 register int c1, c2, si;
4918
4919 if (completion_case_fold)
4920 {
4921 for (si = 0;
4922 (c1 = to_lower(match_list[i][si])) &&
4923 (c2 = to_lower(match_list[i + 1][si]));
4924 si++)
4925 if (c1 != c2) break;
4926 }
4927 else
4928 {
4929 for (si = 0;
4930 (c1 = match_list[i][si]) &&
4931 (c2 = match_list[i + 1][si]);
4932 si++)
4933 if (c1 != c2) break;
4934 }
4935
4936 if (low > si) low = si;
4937 i++;
4938 }
4939 match_list[0] = (char *)xmalloc (low + 1);
4940 strncpy (match_list[0], match_list[1], low);
4941 match_list[0][low] = '\0';
4942 }
4943 }
4944 else /* There were no matches. */
4945 {
4946 free (match_list);
4947 match_list = (char **)NULL;
4948 }
4949 return (match_list);
4950 }
4951
4952 /* Okay, now we write the entry_function for filename completion. In the
4953 general case. Note that completion in the shell is a little different
4954 because of all the pathnames that must be followed when looking up the
4955 completion for a command. */
4956 char *
filename_completion_function(text,state)4957 filename_completion_function (text, state)
4958 int state;
4959 char *text;
4960 {
4961 static DIR *directory;
4962 static char *filename = (char *)NULL;
4963 static char *dirname = (char *)NULL;
4964 static char *users_dirname = (char *)NULL;
4965 static int filename_len;
4966
4967 struct direct *entry = (struct direct *)NULL;
4968
4969 /* If we don't have any state, then do some initialization. */
4970 if (!state)
4971 {
4972 char *rindex (), *temp;
4973
4974 if (dirname) free (dirname);
4975 if (filename) free (filename);
4976 if (users_dirname) free (users_dirname);
4977
4978 filename = savestring (text);
4979 if (!*text) text = ".";
4980 dirname = savestring (text);
4981
4982 temp = rindex (dirname, '/');
4983
4984 if (temp)
4985 {
4986 strcpy (filename, ++temp);
4987 *temp = '\0';
4988 }
4989 else
4990 strcpy (dirname, ".");
4991
4992 /* We aren't done yet. We also support the "~user" syntax. */
4993
4994 /* Save the version of the directory that the user typed. */
4995 users_dirname = savestring (dirname);
4996 {
4997 char *tilde_expand (), *temp_dirname = tilde_expand (dirname);
4998 free (dirname);
4999 dirname = temp_dirname;
5000
5001 if (rl_symbolic_link_hook)
5002 (*rl_symbolic_link_hook) (&dirname);
5003 }
5004 directory = opendir (dirname);
5005 filename_len = strlen (filename);
5006
5007 rl_filename_completion_desired = 1;
5008 }
5009
5010 /* At this point we should entertain the possibility of hacking wildcarded
5011 filenames, like /usr/man*\/te<TAB>. If the directory name contains
5012 globbing characters, then build an array of directories to glob on, and
5013 glob on the first one. */
5014
5015 /* Now that we have some state, we can read the directory. */
5016
5017 while (directory && (entry = readdir (directory)))
5018 {
5019 /* Special case for no filename.
5020 All entries except "." and ".." match. */
5021 if (!filename_len)
5022 {
5023 if ((strcmp (entry->d_name, ".") != 0) &&
5024 (strcmp (entry->d_name, "..") != 0))
5025 break;
5026 }
5027 else
5028 {
5029 /* Otherwise, if these match upto the length of filename, then
5030 it is a match. */
5031 if ((entry->d_namlen >= filename_len) &&
5032 (strncmp (filename, entry->d_name, filename_len) == 0))
5033 {
5034 break;
5035 }
5036 }
5037 }
5038
5039 if (!entry)
5040 {
5041 if (directory)
5042 {
5043 closedir (directory);
5044 directory = (DIR *)NULL;
5045 }
5046 return (char *)NULL;
5047 }
5048 else
5049 {
5050 char *temp;
5051
5052 if (dirname && (strcmp (dirname, ".") != 0))
5053 {
5054 temp = (char *)xmalloc (1 + strlen (users_dirname)
5055 + entry->d_namlen);
5056 strcpy (temp, users_dirname);
5057 strcat (temp, entry->d_name);
5058 }
5059 else
5060 {
5061 temp = (savestring (entry->d_name));
5062 }
5063 return (temp);
5064 }
5065 }
5066
5067
5068 /* **************************************************************** */
5069 /* */
5070 /* Binding keys */
5071 /* */
5072 /* **************************************************************** */
5073
5074 /* rl_add_defun (char *name, Function *function, int key)
5075 Add NAME to the list of named functions. Make FUNCTION
5076 be the function that gets called.
5077 If KEY is not -1, then bind it. */
rl_add_defun(name,function,key)5078 rl_add_defun (name, function, key)
5079 char *name;
5080 Function *function;
5081 int key;
5082 {
5083 if (key != -1)
5084 rl_bind_key (key, function);
5085 rl_add_funmap_entry (name, function);
5086 }
5087
5088 /* Bind KEY to FUNCTION. Returns non-zero if KEY is out of range. */
5089 int
rl_bind_key(key,function)5090 rl_bind_key (key, function)
5091 int key;
5092 Function *function;
5093 {
5094 if (key < 0)
5095 return (key);
5096
5097 if (key > 127 && key < 256)
5098 {
5099 if (keymap[ESC].type == ISKMAP)
5100 {
5101 Keymap escmap = (Keymap)keymap[ESC].function;
5102
5103 key -= 128;
5104 escmap[key].type = ISFUNC;
5105 escmap[key].function = function;
5106 return (0);
5107 }
5108 return (key);
5109 }
5110
5111 keymap[key].type = ISFUNC;
5112 keymap[key].function = function;
5113 return (0);
5114 }
5115
5116 /* Bind KEY to FUNCTION in MAP. Returns non-zero in case of invalid
5117 KEY. */
5118 int
rl_bind_key_in_map(key,function,map)5119 rl_bind_key_in_map (key, function, map)
5120 int key;
5121 Function *function;
5122 Keymap map;
5123 {
5124 int result;
5125 Keymap oldmap = keymap;
5126
5127 keymap = map;
5128 result = rl_bind_key (key, function);
5129 keymap = oldmap;
5130 return (result);
5131 }
5132
5133 /* Make KEY do nothing in the currently selected keymap.
5134 Returns non-zero in case of error. */
5135 int
rl_unbind_key(key)5136 rl_unbind_key (key)
5137 int key;
5138 {
5139 return (rl_bind_key (key, (Function *)NULL));
5140 }
5141
5142 /* Make KEY do nothing in MAP.
5143 Returns non-zero in case of error. */
5144 int
rl_unbind_key_in_map(key,map)5145 rl_unbind_key_in_map (key, map)
5146 int key;
5147 Keymap map;
5148 {
5149 return (rl_bind_key_in_map (key, (Function *)NULL, map));
5150 }
5151
5152 /* Bind the key sequence represented by the string KEYSEQ to
5153 FUNCTION. This makes new keymaps as necessary. The initial
5154 place to do bindings is in MAP. */
rl_set_key(keyseq,function,map)5155 rl_set_key (keyseq, function, map)
5156 char *keyseq;
5157 Function *function;
5158 Keymap map;
5159 {
5160 rl_generic_bind (ISFUNC, keyseq, function, map);
5161 }
5162
5163 /* Bind the key sequence represented by the string KEYSEQ to
5164 the string of characters MACRO. This makes new keymaps as
5165 necessary. The initial place to do bindings is in MAP. */
rl_macro_bind(keyseq,macro,map)5166 rl_macro_bind (keyseq, macro, map)
5167 char *keyseq, *macro;
5168 Keymap map;
5169 {
5170 char *macro_keys = (char *)xmalloc (2 * (strlen (macro)));
5171 int macro_keys_len;
5172
5173 if (rl_translate_keyseq (macro, macro_keys, ¯o_keys_len))
5174 {
5175 free (macro_keys);
5176 return;
5177 }
5178 rl_generic_bind (ISMACR, keyseq, macro_keys, map);
5179 }
5180
5181 /* Bind the key sequence represented by the string KEYSEQ to
5182 the arbitrary pointer DATA. TYPE says what kind of data is
5183 pointed to by DATA, right now this can be a function (ISFUNC),
5184 a macro (ISMACR), or a keymap (ISKMAP). This makes new keymaps
5185 as necessary. The initial place to do bindings is in MAP. */
rl_generic_bind(type,keyseq,data,map)5186 rl_generic_bind (type, keyseq, data, map)
5187 int type;
5188 char *keyseq, *data;
5189 Keymap map;
5190 {
5191 char *keys;
5192 int keys_len;
5193 register int i;
5194
5195 /* If no keys to bind to, exit right away. */
5196 if (!keyseq || !*keyseq)
5197 {
5198 if (type == ISMACR)
5199 free (data);
5200 return;
5201 }
5202
5203 keys = (char *)alloca (1 + (2 * strlen (keyseq)));
5204
5205 /* Translate the ASCII representation of KEYSEQ into an array
5206 of characters. Stuff the characters into ARRAY, and the
5207 length of ARRAY into LENGTH. */
5208 if (rl_translate_keyseq (keyseq, keys, &keys_len))
5209 return;
5210
5211 /* Bind keys, making new keymaps as necessary. */
5212 for (i = 0; i < keys_len; i++)
5213 {
5214 if (i + 1 < keys_len)
5215 {
5216 if (map[keys[i]].type != ISKMAP)
5217 {
5218 if (map[i].type == ISMACR)
5219 free ((char *)map[i].function);
5220
5221 map[keys[i]].type = ISKMAP;
5222 map[keys[i]].function = (Function *)rl_make_bare_keymap ();
5223 }
5224 map = (Keymap)map[keys[i]].function;
5225 }
5226 else
5227 {
5228 if (map[keys[i]].type == ISMACR)
5229 free ((char *)map[keys[i]].function);
5230
5231 map[keys[i]].function = (Function *)data;
5232 map[keys[i]].type = type;
5233 }
5234 }
5235 }
5236
5237 /* Translate the ASCII representation of SEQ, stuffing the
5238 values into ARRAY, an array of characters. LEN gets the
5239 final length of ARRAY. Return non-zero if there was an
5240 error parsing SEQ. */
rl_translate_keyseq(seq,array,len)5241 rl_translate_keyseq (seq, array, len)
5242 char *seq, *array;
5243 int *len;
5244 {
5245 register int i, c, l = 0;
5246
5247 for (i = 0; c = seq[i]; i++)
5248 {
5249 if (c == '\\')
5250 {
5251 c = seq[++i];
5252
5253 if (!c)
5254 break;
5255
5256 if (((c == 'C' || c == 'M') && seq[i + 1] == '-') ||
5257 (c == 'e'))
5258 {
5259 /* Handle special case of backwards define. */
5260 if (strncmp (&seq[i], "C-\\M-", 5) == 0)
5261 {
5262 array[l++] = ESC;
5263 i += 5;
5264 array[l++] = CTRL (to_upper (seq[i]));
5265 if (!seq[i])
5266 i--;
5267 continue;
5268 }
5269
5270 switch (c)
5271 {
5272 case 'M':
5273 i++;
5274 array[l++] = ESC;
5275 break;
5276
5277 case 'C':
5278 i += 2;
5279 array[l++] = CTRL (to_upper (seq[i]));
5280 break;
5281
5282 case 'e':
5283 array[l++] = ESC;
5284 }
5285
5286 continue;
5287 }
5288 }
5289 array[l++] = c;
5290 }
5291
5292 *len = l;
5293 array[l] = '\0';
5294 return (0);
5295 }
5296
5297 /* Return a pointer to the function that STRING represents.
5298 If STRING doesn't have a matching function, then a NULL pointer
5299 is returned. */
5300 Function *
rl_named_function(string)5301 rl_named_function (string)
5302 char *string;
5303 {
5304 register int i;
5305
5306 for (i = 0; funmap[i]; i++)
5307 if (stricmp (funmap[i]->name, string) == 0)
5308 return (funmap[i]->function);
5309 return ((Function *)NULL);
5310 }
5311
5312 /* The last key bindings file read. */
5313 static char *last_readline_init_file = "~/.inputrc";
5314
5315 /* Re-read the current keybindings file. */
rl_re_read_init_file(count,ignore)5316 rl_re_read_init_file (count, ignore)
5317 int count, ignore;
5318 {
5319 rl_read_init_file ((char *)NULL);
5320 }
5321
5322 /* Do key bindings from a file. If FILENAME is NULL it defaults
5323 to `~/.inputrc'. If the file existed and could be opened and
5324 read, 0 is returned, otherwise errno is returned. */
5325 int
rl_read_init_file(filename)5326 rl_read_init_file (filename)
5327 char *filename;
5328 {
5329 register int i;
5330 char *buffer, *openname, *line, *end;
5331 struct stat finfo;
5332 int file;
5333
5334 /* Default the filename. */
5335 if (!filename)
5336 filename = last_readline_init_file;
5337
5338 openname = tilde_expand (filename);
5339
5340 if ((stat (openname, &finfo) < 0) ||
5341 (file = open (openname, O_RDONLY, 0666)) < 0)
5342 {
5343 free (openname);
5344 return (errno);
5345 }
5346 else
5347 free (openname);
5348
5349 last_readline_init_file = filename;
5350
5351 /* Read the file into BUFFER. */
5352 buffer = (char *)xmalloc (finfo.st_size + 1);
5353 i = read (file, buffer, finfo.st_size);
5354 close (file);
5355
5356 if (i != finfo.st_size)
5357 return (errno);
5358
5359 /* Loop over the lines in the file. Lines that start with `#' are
5360 comments; all other lines are commands for readline initialization. */
5361 line = buffer;
5362 end = buffer + finfo.st_size;
5363 while (line < end)
5364 {
5365 /* Find the end of this line. */
5366 for (i = 0; line + i != end && line[i] != '\n'; i++);
5367
5368 /* Mark end of line. */
5369 line[i] = '\0';
5370
5371 /* If the line is not a comment, then parse it. */
5372 if (*line != '#')
5373 rl_parse_and_bind (line);
5374
5375 /* Move to the next line. */
5376 line += i + 1;
5377 }
5378 return (0);
5379 }
5380
5381 /* **************************************************************** */
5382 /* */
5383 /* Parser Directives */
5384 /* */
5385 /* **************************************************************** */
5386
5387 /* Conditionals. */
5388
5389 /* Calling programs set this to have their argv[0]. */
5390 char *rl_readline_name = "other";
5391
5392 /* Stack of previous values of parsing_conditionalized_out. */
5393 static unsigned char *if_stack = (unsigned char *)NULL;
5394 static int if_stack_depth = 0;
5395 static int if_stack_size = 0;
5396
5397 /* Push parsing_conditionalized_out, and set parser state based on ARGS. */
parser_if(args)5398 parser_if (args)
5399 char *args;
5400 {
5401 register int i;
5402
5403 /* Push parser state. */
5404 if (if_stack_depth + 1 >= if_stack_size)
5405 {
5406 if (!if_stack)
5407 if_stack = (unsigned char *)xmalloc (if_stack_size = 20);
5408 else
5409 if_stack = (unsigned char *)xrealloc (if_stack, if_stack_size += 20);
5410 }
5411 if_stack[if_stack_depth++] = parsing_conditionalized_out;
5412
5413 /* We only check to see if the first word in ARGS is the same as the
5414 value stored in rl_readline_name. */
5415
5416 /* Isolate first argument. */
5417 for (i = 0; args[i] && !whitespace (args[i]); i++);
5418
5419 if (args[i])
5420 args[i++] = '\0';
5421
5422 /* Handle "if term=foo" construct. If this isn't term=foo, then
5423 check to see if the first word in ARGS is the same as the
5424 value stored in rl_readline_name. */
5425 if (rl_terminal_name && strnicmp (args, "term=", 5) == 0)
5426 {
5427 char *rindex (), *tem, *tname;
5428
5429 /* Terminals like "aaa-60" are equivalent to "aaa". */
5430 tname = savestring (rl_terminal_name);
5431 tem = rindex (tname, '-');
5432 if (tem)
5433 *tem = '\0';
5434
5435 if (stricmp (args + 5, tname) == 0)
5436 parsing_conditionalized_out = 1;
5437 else
5438 parsing_conditionalized_out = 0;
5439 }
5440 else if (stricmp (args, rl_readline_name) == 0)
5441 parsing_conditionalized_out = 0;
5442 else
5443 parsing_conditionalized_out = 1;
5444 }
5445
5446 /* Invert the current parser state if there is anything on the stack. */
parser_else(args)5447 parser_else (args)
5448 char *args;
5449 {
5450 if (if_stack_depth)
5451 parsing_conditionalized_out = !parsing_conditionalized_out;
5452 else
5453 {
5454 /* *** What, no error message? *** */
5455 }
5456 }
5457
5458 /* Terminate a conditional, popping the value of
5459 parsing_conditionalized_out from the stack. */
parser_endif(args)5460 parser_endif (args)
5461 char *args;
5462 {
5463 if (if_stack_depth)
5464 parsing_conditionalized_out = if_stack[--if_stack_depth];
5465 else
5466 {
5467 /* *** What, no error message? *** */
5468 }
5469 }
5470
5471 /* Associate textual names with actual functions. */
5472 static struct {
5473 char *name;
5474 Function *function;
5475 } parser_directives [] = {
5476 { "if", parser_if },
5477 { "endif", parser_endif },
5478 { "else", parser_else },
5479 { (char *)0x0, (Function *)0x0 }
5480 };
5481
5482 /* Handle a parser directive. STATEMENT is the line of the directive
5483 without any leading `$'. */
5484 static int
handle_parser_directive(statement)5485 handle_parser_directive (statement)
5486 char *statement;
5487 {
5488 register int i;
5489 char *directive, *args;
5490
5491 /* Isolate the actual directive. */
5492
5493 /* Skip whitespace. */
5494 for (i = 0; whitespace (statement[i]); i++);
5495
5496 directive = &statement[i];
5497
5498 for (; statement[i] && !whitespace (statement[i]); i++);
5499
5500 if (statement[i])
5501 statement[i++] = '\0';
5502
5503 for (; statement[i] && whitespace (statement[i]); i++);
5504
5505 args = &statement[i];
5506
5507 /* Lookup the command, and act on it. */
5508 for (i = 0; parser_directives[i].name; i++)
5509 if (stricmp (directive, parser_directives[i].name) == 0)
5510 {
5511 (*parser_directives[i].function) (args);
5512 return (0);
5513 }
5514
5515 /* *** Should an error message be output? */
5516 return (1);
5517 }
5518
5519 /* Read the binding command from STRING and perform it.
5520 A key binding command looks like: Keyname: function-name\0,
5521 a variable binding command looks like: set variable value.
5522 A new-style keybinding looks like "\C-x\C-x": exchange-point-and-mark. */
rl_parse_and_bind(string)5523 rl_parse_and_bind (string)
5524 char *string;
5525 {
5526 extern char *possible_control_prefixes[], *possible_meta_prefixes[];
5527 char *rindex (), *funname, *kname;
5528 static int substring_member_of_array ();
5529 register int c;
5530 int key, i;
5531
5532 if (!string || !*string || *string == '#')
5533 return;
5534
5535 /* If this is a parser directive, act on it. */
5536 if (*string == '$')
5537 {
5538 handle_parser_directive (&string[1]);
5539 return;
5540 }
5541
5542 /* If we are supposed to be skipping parsing right now, then do it. */
5543 if (parsing_conditionalized_out)
5544 return;
5545
5546 i = 0;
5547 /* If this keyname is a complex key expression surrounded by quotes,
5548 advance to after the matching close quote. */
5549 if (*string == '"')
5550 {
5551 for (i = 1; c = string[i]; i++)
5552 {
5553 if (c == '"' && string[i - 1] != '\\')
5554 break;
5555 }
5556 }
5557
5558 /* Advance to the colon (:) or whitespace which separates the two objects. */
5559 for (; (c = string[i]) && c != ':' && c != ' ' && c != '\t'; i++ );
5560
5561 /* Mark the end of the command (or keyname). */
5562 if (string[i])
5563 string[i++] = '\0';
5564
5565 /* If this is a command to set a variable, then do that. */
5566 if (stricmp (string, "set") == 0)
5567 {
5568 char *var = string + i;
5569 char *value;
5570
5571 /* Make VAR point to start of variable name. */
5572 while (*var && whitespace (*var)) var++;
5573
5574 /* Make value point to start of value string. */
5575 value = var;
5576 while (*value && !whitespace (*value)) value++;
5577 if (*value)
5578 *value++ = '\0';
5579 while (*value && whitespace (*value)) value++;
5580
5581 rl_variable_bind (var, value);
5582 return;
5583 }
5584
5585 /* Skip any whitespace between keyname and funname. */
5586 for (; string[i] && whitespace (string[i]); i++);
5587 funname = &string[i];
5588
5589 /* Now isolate funname.
5590 For straight function names just look for whitespace, since
5591 that will signify the end of the string. But this could be a
5592 macro definition. In that case, the string is quoted, so skip
5593 to the matching delimiter. */
5594 if (*funname == '\'' || *funname == '"')
5595 {
5596 int delimiter = string[i++];
5597
5598 for (; c = string[i]; i++)
5599 {
5600 if (c == delimiter && string[i - 1] != '\\')
5601 break;
5602 }
5603 if (c)
5604 i++;
5605 }
5606
5607 /* Advance to the end of the string. */
5608 for (; string[i] && !whitespace (string[i]); i++);
5609
5610 /* No extra whitespace at the end of the string. */
5611 string[i] = '\0';
5612
5613 /* If this is a new-style key-binding, then do the binding with
5614 rl_set_key (). Otherwise, let the older code deal with it. */
5615 if (*string == '"')
5616 {
5617 char *seq = (char *)alloca (1 + strlen (string));
5618 register int j, k = 0;
5619
5620 for (j = 1; string[j]; j++)
5621 {
5622 if (string[j] == '"' && string[j - 1] != '\\')
5623 break;
5624
5625 seq[k++] = string[j];
5626 }
5627 seq[k] = '\0';
5628
5629 /* Binding macro? */
5630 if (*funname == '\'' || *funname == '"')
5631 {
5632 j = strlen (funname);
5633
5634 if (j && funname[j - 1] == *funname)
5635 funname[j - 1] = '\0';
5636
5637 rl_macro_bind (seq, &funname[1], keymap);
5638 }
5639 else
5640 rl_set_key (seq, rl_named_function (funname), keymap);
5641
5642 return;
5643 }
5644
5645 /* Get the actual character we want to deal with. */
5646 kname = rindex (string, '-');
5647 if (!kname)
5648 kname = string;
5649 else
5650 kname++;
5651
5652 key = glean_key_from_name (kname);
5653
5654 /* Add in control and meta bits. */
5655 if (substring_member_of_array (string, possible_control_prefixes))
5656 key = CTRL (to_upper (key));
5657
5658 if (substring_member_of_array (string, possible_meta_prefixes))
5659 key = META (key);
5660
5661 /* Temporary. Handle old-style keyname with macro-binding. */
5662 if (*funname == '\'' || *funname == '"')
5663 {
5664 char seq[2];
5665 int fl = strlen (funname);
5666
5667 seq[0] = key; seq[1] = '\0';
5668 if (fl && funname[fl - 1] == *funname)
5669 funname[fl - 1] = '\0';
5670
5671 rl_macro_bind (seq, &funname[1], keymap);
5672 }
5673 else
5674 rl_bind_key (key, rl_named_function (funname));
5675 }
5676
rl_variable_bind(name,value)5677 rl_variable_bind (name, value)
5678 char *name, *value;
5679 {
5680 if (stricmp (name, "editing-mode") == 0)
5681 {
5682 if (strnicmp (value, "vi", 2) == 0)
5683 {
5684 #ifdef VI_MODE
5685 keymap = vi_insertion_keymap;
5686 rl_editing_mode = vi_mode;
5687 #endif /* VI_MODE */
5688 }
5689 else if (strnicmp (value, "emacs", 5) == 0)
5690 {
5691 keymap = emacs_standard_keymap;
5692 rl_editing_mode = emacs_mode;
5693 }
5694 }
5695 else if (stricmp (name, "horizontal-scroll-mode") == 0)
5696 {
5697 if (!*value || stricmp (value, "On") == 0)
5698 horizontal_scroll_mode = 1;
5699 else
5700 horizontal_scroll_mode = 0;
5701 }
5702 else if (stricmp (name, "mark-modified-lines") == 0)
5703 {
5704 if (!*value || stricmp (value, "On") == 0)
5705 mark_modified_lines = 1;
5706 else
5707 mark_modified_lines = 0;
5708 }
5709 else if (stricmp (name, "prefer-visible-bell") == 0)
5710 {
5711 if (!*value || stricmp (value, "On") == 0)
5712 prefer_visible_bell = 1;
5713 else
5714 prefer_visible_bell = 0;
5715 }
5716 }
5717
5718 /* Return the character which matches NAME.
5719 For example, `Space' returns ' '. */
5720
5721 typedef struct {
5722 char *name;
5723 int value;
5724 } assoc_list;
5725
5726 assoc_list name_key_alist[] = {
5727 { "Space", ' ' },
5728 { "SPC", ' ' },
5729 { "Rubout", 0x7f },
5730 { "DEL", 0x7f },
5731 { "Tab", 0x09 },
5732 { "Newline", '\n' },
5733 { "Return", '\r' },
5734 { "RET", '\r' },
5735 { "LFD", '\n' },
5736 { "Escape", '\033' },
5737 { "ESC", '\033' },
5738
5739 { (char *)0x0, 0 }
5740 };
5741
5742 int
glean_key_from_name(name)5743 glean_key_from_name (name)
5744 char *name;
5745 {
5746 register int i;
5747
5748 for (i = 0; name_key_alist[i].name; i++)
5749 if (stricmp (name, name_key_alist[i].name) == 0)
5750 return (name_key_alist[i].value);
5751
5752 return (*name);
5753 }
5754
5755
5756 /* **************************************************************** */
5757 /* */
5758 /* String Utility Functions */
5759 /* */
5760 /* **************************************************************** */
5761
5762 /* Return non-zero if any members of ARRAY are a substring in STRING. */
5763 static int
substring_member_of_array(string,array)5764 substring_member_of_array (string, array)
5765 char *string, **array;
5766 {
5767 static char *strindex ();
5768
5769 while (*array)
5770 {
5771 if (strindex (string, *array))
5772 return (1);
5773 array++;
5774 }
5775 return (0);
5776 }
5777
5778 /* Whoops, Unix doesn't have strnicmp. */
5779
5780 /* Compare at most COUNT characters from string1 to string2. Case
5781 doesn't matter. */
5782 static int
strnicmp(string1,string2,count)5783 strnicmp (string1, string2, count)
5784 char *string1, *string2;
5785 {
5786 register char ch1, ch2;
5787
5788 while (count)
5789 {
5790 ch1 = *string1++;
5791 ch2 = *string2++;
5792 if (to_upper(ch1) == to_upper(ch2))
5793 count--;
5794 else break;
5795 }
5796 return (count);
5797 }
5798
5799 /* strcmp (), but caseless. */
5800 static int
stricmp(string1,string2)5801 stricmp (string1, string2)
5802 char *string1, *string2;
5803 {
5804 register char ch1, ch2;
5805
5806 while (*string1 && *string2)
5807 {
5808 ch1 = *string1++;
5809 ch2 = *string2++;
5810 if (to_upper(ch1) != to_upper(ch2))
5811 return (1);
5812 }
5813 return (*string1 | *string2);
5814 }
5815
5816 /* Determine if s2 occurs in s1. If so, return a pointer to the
5817 match in s1. The compare is case insensitive. */
5818 static char *
strindex(s1,s2)5819 strindex (s1, s2)
5820 register char *s1, *s2;
5821 {
5822 register int i, l = strlen (s2);
5823 register int len = strlen (s1);
5824
5825 for (i = 0; (len - i) >= l; i++)
5826 if (strnicmp (&s1[i], s2, l) == 0)
5827 return (s1 + i);
5828 return ((char *)NULL);
5829 }
5830
5831
5832 /* **************************************************************** */
5833 /* */
5834 /* USG (System V) Support */
5835 /* */
5836 /* **************************************************************** */
5837
5838 /* When compiling and running in the `Posix' environment, Ultrix does
5839 not restart system calls, so this needs to do it. */
5840 int
rl_getc(stream)5841 rl_getc (stream)
5842 FILE *stream;
5843 {
5844 int result;
5845 unsigned char c;
5846
5847 while (1)
5848 {
5849 result = read (fileno (stream), &c, sizeof (char));
5850
5851 if (result == sizeof (char))
5852 return (c);
5853
5854 if (errno != EINTR)
5855 return (EOF);
5856 }
5857 }
5858
5859 #ifdef STATIC_MALLOC
5860
5861 /* **************************************************************** */
5862 /* */
5863 /* xmalloc and xrealloc () */
5864 /* */
5865 /* **************************************************************** */
5866
5867 static void memory_error_and_abort ();
5868
5869 static char *
xmalloc(bytes)5870 xmalloc (bytes)
5871 int bytes;
5872 {
5873 char *temp = (char *)malloc (bytes);
5874
5875 if (!temp)
5876 memory_error_and_abort ();
5877 return (temp);
5878 }
5879
5880 static char *
xrealloc(pointer,bytes)5881 xrealloc (pointer, bytes)
5882 char *pointer;
5883 int bytes;
5884 {
5885 char *temp = (char *)realloc (pointer, bytes);
5886
5887 if (!temp)
5888 memory_error_and_abort ();
5889 return (temp);
5890 }
5891
5892 static void
memory_error_and_abort()5893 memory_error_and_abort ()
5894 {
5895 fprintf (stderr, "readline: Out of virtual memory!\n");
5896 abort ();
5897 }
5898 #endif /* STATIC_MALLOC */
5899
5900
5901 /* **************************************************************** */
5902 /* */
5903 /* Testing Readline */
5904 /* */
5905 /* **************************************************************** */
5906
5907 #if defined (TEST)
5908
main()5909 main ()
5910 {
5911 HIST_ENTRY **history_list ();
5912 char *temp = (char *)NULL;
5913 char *prompt = "readline% ";
5914 int done = 0;
5915
5916 while (!done)
5917 {
5918 temp = readline (prompt);
5919
5920 /* Test for EOF. */
5921 if (!temp)
5922 exit (1);
5923
5924 /* If there is anything on the line, print it and remember it. */
5925 if (*temp)
5926 {
5927 fprintf (stderr, "%s\r\n", temp);
5928 add_history (temp);
5929 }
5930
5931 /* Check for `command' that we handle. */
5932 if (strcmp (temp, "quit") == 0)
5933 done = 1;
5934
5935 if (strcmp (temp, "list") == 0) {
5936 HIST_ENTRY **list = history_list ();
5937 register int i;
5938 if (list) {
5939 for (i = 0; list[i]; i++) {
5940 fprintf (stderr, "%d: %s\r\n", i, list[i]->line);
5941 free (list[i]->line);
5942 }
5943 free (list);
5944 }
5945 }
5946 free (temp);
5947 }
5948 }
5949
5950 #endif /* TEST */
5951
5952
5953 /*
5954 * Local variables:
5955 * compile-command: "gcc -g -traditional -I. -I.. -DTEST -o readline readline.c keymaps.o funmap.o history.o -ltermcap"
5956 * end:
5957 */
5958
5959
5960 /*** Prompting Input Editor added by Mark J Daly, mjdaly@athena.mit.edu ****/
5961
gnu_edit_this(prompt_str,start_str)5962 char *gnu_edit_this(prompt_str,start_str)
5963 char *prompt_str, *start_str;
5964 {
5965 static rl_prep_terminal (), rl_deprep_terminal ();
5966 char *readline_internal ();
5967 char *newstr;
5968
5969 rl_initialize();
5970 rl_prep_terminal();
5971 strcpy(rl_line_buffer,start_str);
5972 rl_end= strlen(start_str);
5973 strcpy(rl_prompt,prompt_str);
5974 newstr= readline_internal(); /* maybe null */
5975 rl_deprep_terminal();
5976
5977 return(newstr);
5978 }
5979
5980