Imported from ../bash-1.14.7.tar.gz.
[platform/upstream/bash.git] / lib / readline / readline.c
1 /* readline.c -- a general facility for reading lines of input
2    with emacs style editing and completion. */
3
4 /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
5
6    This file is part of the GNU Readline Library, a library for
7    reading lines of text with interactive input and history editing.
8
9    The GNU Readline Library is free software; you can redistribute it
10    and/or modify it under the terms of the GNU General Public License
11    as published by the Free Software Foundation; either version 1, or
12    (at your option) any later version.
13
14    The GNU Readline Library is distributed in the hope that it will be
15    useful, but WITHOUT ANY WARRANTY; without even the implied warranty
16    of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    The GNU General Public License is often shipped with GNU software, and
20    is generally kept in a file called COPYING or LICENSE.  If you do not
21    have a copy of the license, write to the Free Software Foundation,
22    675 Mass Ave, Cambridge, MA 02139, USA. */
23 #define READLINE_LIBRARY
24
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include <fcntl.h>
28 #if !defined (NO_SYS_FILE)
29 #  include <sys/file.h>
30 #endif /* !NO_SYS_FILE */
31 #include <signal.h>
32
33 #if defined (HAVE_UNISTD_H)
34 #  include <unistd.h>
35 #endif /* HAVE_UNISTD_H */
36
37 #if defined (HAVE_STDLIB_H)
38 #  include <stdlib.h>
39 #else
40 #  include "ansi_stdlib.h"
41 #endif /* HAVE_STDLIB_H */
42
43 #include <errno.h>
44 /* Not all systems declare ERRNO in errno.h... and some systems #define it! */
45 #if !defined (errno)
46 extern int errno;
47 #endif /* !errno */
48
49 #include <setjmp.h>
50
51 #include "posixstat.h"
52
53 /* System-specific feature definitions and include files. */
54 #include "rldefs.h"
55
56 #if defined (GWINSZ_IN_SYS_IOCTL) || (defined (VSTATUS) && !defined (SunOS4))
57 #  include <sys/ioctl.h>
58 #endif /* GWINSZ_IN_SYS_IOCTL || VSTATUS */
59
60 /* Some standard library routines. */
61 #include "readline.h"
62 #include "history.h"
63
64 /* NOTE: Functions and variables prefixed with `_rl_' are
65    pseudo-global: they are global so they can be shared
66    between files in the readline library, but are not intended
67    to be visible to readline callers. */
68
69 /* Functions imported from other files in the library. */
70 extern char *tgetstr ();
71 extern void rl_prep_terminal (), rl_deprep_terminal ();
72
73 extern void _rl_bind_if_unbound ();
74
75 /* External redisplay functions and variables from display.c */
76 extern void _rl_move_vert ();
77 extern void _rl_update_final ();
78
79 extern void _rl_erase_at_end_of_line ();
80 extern void _rl_move_cursor_relative ();
81
82 extern int _rl_vis_botlin;
83 extern int _rl_last_c_pos;
84 extern int _rl_horizontal_scroll_mode;
85 extern int rl_display_fixed;
86 extern char *rl_display_prompt;
87
88 /* Variables imported from complete.c. */
89 extern char *rl_completer_word_break_characters;
90 extern char *rl_basic_word_break_characters;
91 extern int rl_completion_query_items;
92 extern int rl_complete_with_tilde_expansion;
93
94 #if defined (VI_MODE)
95 extern void _rl_vi_set_last ();
96 extern void _rl_vi_reset_last ();
97 extern void _rl_vi_done_inserting ();
98 #endif /* VI_MODE */
99
100 /* Forward declarations used in this file. */
101 void _rl_free_history_entry ();
102
103 int _rl_dispatch ();
104 void _rl_set_screen_size ();
105 int _rl_output_character_function ();
106
107 static char *readline_internal ();
108 static void readline_initialize_everything ();
109 static int init_terminal_io ();
110 static void start_using_history ();
111 static void bind_arrow_keys ();
112
113 #if !defined (__GO32__)
114 static void readline_default_bindings ();
115 #endif /* !__GO32__ */
116
117 #if defined (__GO32__)
118 #  include <sys/pc.h>
119 #  undef HANDLE_SIGNALS
120 #endif /* __GO32__ */
121
122 #if defined (STATIC_MALLOC)
123 static char *xmalloc (), *xrealloc ();
124 #else
125 extern char *xmalloc (), *xrealloc ();
126 #endif /* STATIC_MALLOC */
127
128 \f
129 /* **************************************************************** */
130 /*                                                                  */
131 /*                      Line editing input utility                  */
132 /*                                                                  */
133 /* **************************************************************** */
134
135 static char *LibraryVersion = "2.0";
136
137 /* A pointer to the keymap that is currently in use.
138    By default, it is the standard emacs keymap. */
139 Keymap _rl_keymap = emacs_standard_keymap;
140
141 /* The current style of editing. */
142 int rl_editing_mode = emacs_mode;
143
144 /* Non-zero if the previous command was a kill command. */
145 static int last_command_was_kill = 0;
146
147 /* The current value of the numeric argument specified by the user. */
148 int rl_numeric_arg = 1;
149
150 /* Non-zero if an argument was typed. */
151 int rl_explicit_arg = 0;
152
153 /* Temporary value used while generating the argument. */
154 int rl_arg_sign = 1;
155
156 /* Non-zero means we have been called at least once before. */
157 static int rl_initialized = 0;
158
159 /* If non-zero, this program is running in an EMACS buffer. */
160 static int running_in_emacs = 0;
161
162 /* The current offset in the current input line. */
163 int rl_point;
164
165 /* Mark in the current input line. */
166 int rl_mark;
167
168 /* Length of the current input line. */
169 int rl_end;
170
171 /* Make this non-zero to return the current input_line. */
172 int rl_done;
173
174 /* The last function executed by readline. */
175 Function *rl_last_func = (Function *)NULL;
176
177 /* Top level environment for readline_internal (). */
178 static jmp_buf readline_top_level;
179
180 /* The streams we interact with. */
181 static FILE *in_stream, *out_stream;
182
183 /* The names of the streams that we do input and output to. */
184 FILE *rl_instream = (FILE *)NULL;
185 FILE *rl_outstream = (FILE *)NULL;
186
187 /* Non-zero means echo characters as they are read. */
188 int readline_echoing_p = 1;
189
190 /* Current prompt. */
191 char *rl_prompt;
192 int rl_visible_prompt_length = 0;
193
194 /* The number of characters read in order to type this complete command. */
195 int rl_key_sequence_length = 0;
196
197 /* If non-zero, then this is the address of a function to call just
198    before readline_internal () prints the first prompt. */
199 Function *rl_startup_hook = (Function *)NULL;
200
201 /* What we use internally.  You should always refer to RL_LINE_BUFFER. */
202 static char *the_line;
203
204 /* The character that can generate an EOF.  Really read from
205    the terminal driver... just defaulted here. */
206 int _rl_eof_char = CTRL ('D');
207
208 /* Non-zero makes this the next keystroke to read. */
209 int rl_pending_input = 0;
210
211 /* Pointer to a useful terminal name. */
212 char *rl_terminal_name = (char *)NULL;
213
214 /* Non-zero means to always use horizontal scrolling in line display. */
215 int _rl_horizontal_scroll_mode = 0;
216
217 /* Non-zero means to display an asterisk at the starts of history lines
218    which have been modified. */
219 int _rl_mark_modified_lines = 0;  
220
221 /* The style of `bell' notification preferred.  This can be set to NO_BELL,
222    AUDIBLE_BELL, or VISIBLE_BELL. */
223 int _rl_bell_preference = AUDIBLE_BELL;
224      
225 /* Line buffer and maintenence. */
226 char *rl_line_buffer = (char *)NULL;
227 int rl_line_buffer_len = 0;
228 #define DEFAULT_BUFFER_SIZE 256
229
230 /* Forward declarations used by the display and termcap code. */
231 int term_xn;
232 int screenwidth, screenheight, screenchars;
233
234 \f
235 /* **************************************************************** */
236 /*                                                                  */
237 /*                      `Forward' declarations                      */
238 /*                                                                  */
239 /* **************************************************************** */
240
241 /* Non-zero means do not parse any lines other than comments and
242    parser directives. */
243 unsigned char _rl_parsing_conditionalized_out = 0;
244
245 /* Non-zero means to save keys that we dispatch on in a kbd macro. */
246 static int defining_kbd_macro = 0;
247
248 /* Non-zero means to convert characters with the meta bit set to
249    escape-prefixed characters so we can indirect through
250    emacs_meta_keymap or vi_escape_keymap. */
251 int _rl_convert_meta_chars_to_ascii = 1;
252
253 /* Non-zero means to output characters with the meta bit set directly
254    rather than as a meta-prefixed escape sequence. */
255 int _rl_output_meta_chars = 0;
256
257 /* Non-zero tells rl_delete_text and rl_insert_text to not add to
258    the undo list. */
259 static int doing_an_undo = 0;
260 \f
261 /* **************************************************************** */
262 /*                                                                  */
263 /*                      Top Level Functions                         */
264 /*                                                                  */
265 /* **************************************************************** */
266
267 /* Non-zero means treat 0200 bit in terminal input as Meta bit. */
268 int _rl_meta_flag = 0;  /* Forward declaration */
269
270 /* Read a line of input.  Prompt with PROMPT.  A NULL PROMPT means
271    none.  A return value of NULL means that EOF was encountered. */
272 char *
273 readline (prompt)
274      char *prompt;
275 {
276   char *value;
277
278   rl_prompt = prompt;
279
280   /* If we are at EOF return a NULL string. */
281   if (rl_pending_input == EOF)
282     {
283       rl_pending_input = 0;
284       return ((char *)NULL);
285     }
286
287   rl_visible_prompt_length = rl_expand_prompt (rl_prompt);
288
289   rl_initialize ();
290   rl_prep_terminal (_rl_meta_flag);
291
292 #if defined (HANDLE_SIGNALS)
293   rl_set_signals ();
294 #endif
295
296   value = readline_internal ();
297   rl_deprep_terminal ();
298
299 #if defined (HANDLE_SIGNALS)
300   rl_clear_signals ();
301 #endif
302
303   return (value);
304 }
305
306 /* Read a line of input from the global rl_instream, doing output on
307    the global rl_outstream.
308    If rl_prompt is non-null, then that is our prompt. */
309 static char *
310 readline_internal ()
311 {
312   int lastc, c, eof_found;
313
314   in_stream  = rl_instream;
315   out_stream = rl_outstream;
316
317   lastc = -1;
318   eof_found = 0;
319
320   if (rl_startup_hook)
321     (*rl_startup_hook) ();
322
323   if (!readline_echoing_p)
324     {
325       if (rl_prompt)
326         {
327           fprintf (out_stream, "%s", rl_prompt);
328           fflush (out_stream);
329         }
330     }
331   else
332     {
333       rl_on_new_line ();
334       rl_redisplay ();
335 #if defined (VI_MODE)
336       if (rl_editing_mode == vi_mode)
337         rl_vi_insertion_mode ();
338 #endif /* VI_MODE */
339     }
340
341   while (!rl_done)
342     {
343       int lk = last_command_was_kill;
344       int code;
345
346       code = setjmp (readline_top_level);
347
348       if (code)
349         rl_redisplay ();
350
351       if (!rl_pending_input)
352         {
353           /* Then initialize the argument and number of keys read. */
354           rl_init_argument ();
355           rl_key_sequence_length = 0;
356         }
357
358       c = rl_read_key ();
359
360       /* EOF typed to a non-blank line is a <NL>. */
361       if (c == EOF && rl_end)
362         c = NEWLINE;
363
364       /* The character _rl_eof_char typed to blank line, and not as the
365          previous character is interpreted as EOF. */
366       if (((c == _rl_eof_char && lastc != c) || c == EOF) && !rl_end)
367         {
368           eof_found = 1;
369           break;
370         }
371
372       lastc = c;
373       _rl_dispatch (c, _rl_keymap);
374
375       /* If there was no change in last_command_was_kill, then no kill
376          has taken place.  Note that if input is pending we are reading
377          a prefix command, so nothing has changed yet. */
378       if (!rl_pending_input)
379         {
380           if (lk == last_command_was_kill)
381             last_command_was_kill = 0;
382         }
383
384 #if defined (VI_MODE)
385       /* In vi mode, when you exit insert mode, the cursor moves back
386          over the previous character.  We explicitly check for that here. */
387       if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap)
388         rl_vi_check ();
389 #endif /* VI_MODE */
390
391       if (!rl_done)
392         rl_redisplay ();
393     }
394
395   /* Restore the original of this history line, iff the line that we
396      are editing was originally in the history, AND the line has changed. */
397   {
398     HIST_ENTRY *entry = current_history ();
399
400     if (entry && rl_undo_list)
401       {
402         char *temp = savestring (the_line);
403         rl_revert_line ();
404         entry = replace_history_entry (where_history (), the_line,
405                                        (HIST_ENTRY *)NULL);
406         _rl_free_history_entry (entry);
407
408         strcpy (the_line, temp);
409         free (temp);
410       }
411   }
412
413   /* At any rate, it is highly likely that this line has an undo list.  Get
414      rid of it now. */
415   if (rl_undo_list)
416     free_undo_list ();
417
418   if (eof_found)
419     return (char *)NULL;
420   else
421     return (savestring (the_line));
422 }
423 \f
424 /* **************************************************************** */
425 /*                                                                  */
426 /*                      Character Input Buffering                   */
427 /*                                                                  */
428 /* **************************************************************** */
429
430 static int pop_index = 0, push_index = 0, ibuffer_len = 511;
431 static unsigned char ibuffer[512];
432
433 /* Non-null means it is a pointer to a function to run while waiting for
434    character input. */
435 Function *rl_event_hook = (Function *)NULL;
436
437 #define any_typein (push_index != pop_index)
438
439 /* Add KEY to the buffer of characters to be read. */
440 rl_stuff_char (key)
441      int key;
442 {
443   if (key == EOF)
444     {
445       key = NEWLINE;
446       rl_pending_input = EOF;
447     }
448   ibuffer[push_index++] = key;
449   if (push_index >= ibuffer_len)
450     push_index = 0;
451   return push_index;
452 }
453
454 /* Return the amount of space available in the
455    buffer for stuffing characters. */
456 int
457 ibuffer_space ()
458 {
459   if (pop_index > push_index)
460     return (pop_index - push_index);
461   else
462     return (ibuffer_len - (push_index - pop_index));
463 }
464
465 /* Get a key from the buffer of characters to be read.
466    Return the key in KEY.
467    Result is KEY if there was a key, or 0 if there wasn't. */
468 int
469 rl_get_char (key)
470      int *key;
471 {
472   if (push_index == pop_index)
473     return (0);
474
475   *key = ibuffer[pop_index++];
476
477   if (pop_index >= ibuffer_len)
478     pop_index = 0;
479
480   return (1);
481 }
482
483 /* Stuff KEY into the *front* of the input buffer.
484    Returns non-zero if successful, zero if there is
485    no space left in the buffer. */
486 int
487 rl_unget_char (key)
488      int key;
489 {
490   if (ibuffer_space ())
491     {
492       pop_index--;
493       if (pop_index < 0)
494         pop_index = ibuffer_len - 1;
495       ibuffer[pop_index] = key;
496       return (1);
497     }
498   return (0);
499 }
500
501 /* If a character is available to be read, then read it
502    and stuff it into IBUFFER.  Otherwise, just return. */
503 void
504 rl_gather_tyi ()
505 {
506 #if defined (__GO32__)
507   char input;
508
509   if (isatty (0))
510     {
511       int i = rl_getc ();
512
513       if (i != EOF)
514         rl_stuff_char (i);
515     }
516   else if (kbhit () && ibuffer_space ())
517     rl_stuff_char (getkey ());
518 #else /* !__GO32__ */
519
520   int tty = fileno (in_stream);
521   register int tem, result = -1;
522   int chars_avail;
523   char input;
524
525 #if defined (FIONREAD)
526   result = ioctl (tty, FIONREAD, &chars_avail);
527 #endif
528
529 #if defined (O_NDELAY)
530   if (result == -1)
531     {
532       int flags;
533
534       flags = fcntl (tty, F_GETFL, 0);
535
536       fcntl (tty, F_SETFL, (flags | O_NDELAY));
537       chars_avail = read (tty, &input, 1);
538
539       fcntl (tty, F_SETFL, flags);
540       if (chars_avail == -1 && errno == EAGAIN)
541         return;
542     }
543 #endif /* O_NDELAY */
544
545   /* If there's nothing available, don't waste time trying to read
546      something. */
547   if (chars_avail == 0)
548     return;
549
550   tem = ibuffer_space ();
551
552   if (chars_avail > tem)
553     chars_avail = tem;
554
555   /* One cannot read all of the available input.  I can only read a single
556      character at a time, or else programs which require input can be
557      thwarted.  If the buffer is larger than one character, I lose.
558      Damn! */
559   if (tem < ibuffer_len)
560     chars_avail = 0;
561
562   if (result != -1)
563     {
564       while (chars_avail--)
565         rl_stuff_char (rl_getc (in_stream));
566     }
567   else
568     {
569       if (chars_avail)
570         rl_stuff_char (input);
571     }
572 #endif /* !__GO32__ */
573 }
574
575 static int next_macro_key ();
576 /* Read a key, including pending input. */
577 int
578 rl_read_key ()
579 {
580   int c;
581
582   rl_key_sequence_length++;
583
584   if (rl_pending_input)
585     {
586       c = rl_pending_input;
587       rl_pending_input = 0;
588     }
589   else
590     {
591       /* If input is coming from a macro, then use that. */
592       if (c = next_macro_key ())
593         return (c);
594
595       /* If the user has an event function, then call it periodically. */
596       if (rl_event_hook)
597         {
598           while (rl_event_hook && !rl_get_char (&c))
599             {
600               (*rl_event_hook) ();
601               rl_gather_tyi ();
602             }
603         }
604       else
605         {
606           if (!rl_get_char (&c))
607             c = rl_getc (in_stream);
608         }
609     }
610
611   return (c);
612 }
613
614 /* Found later in this file. */
615 static void add_macro_char (), with_macro_input ();
616
617 /* Do the command associated with KEY in MAP.
618    If the associated command is really a keymap, then read
619    another key, and dispatch into that map. */
620 int
621 _rl_dispatch (key, map)
622      register int key;
623      Keymap map;
624 {
625   int r = 0;
626
627   if (META_CHAR (key) && _rl_convert_meta_chars_to_ascii)
628     {
629       if (map[ESC].type == ISKMAP)
630         {
631           if (defining_kbd_macro)
632             add_macro_char (ESC);
633           map = FUNCTION_TO_KEYMAP (map, ESC);
634           key = UNMETA (key);
635           rl_key_sequence_length += 2;
636           return (_rl_dispatch (key, map));
637         }
638       else
639         ding ();
640       return 0;
641     }
642
643   if (defining_kbd_macro)
644     add_macro_char (key);
645
646   switch (map[key].type)
647     {
648     case ISFUNC:
649       {
650         Function *func = map[key].function;
651
652         if (func != (Function *)NULL)
653           {
654             /* Special case rl_do_lowercase_version (). */
655             if (func == rl_do_lowercase_version)
656               return (_rl_dispatch (to_lower (key), map));
657
658             r = (*map[key].function)(rl_numeric_arg * rl_arg_sign, key);
659
660             /* If we have input pending, then the last command was a prefix
661                command.  Don't change the state of rl_last_func.  Otherwise,
662                remember the last command executed in this variable. */
663             if (!rl_pending_input)
664               rl_last_func = map[key].function;
665           }
666         else
667           {
668             rl_abort ();
669             return -1;
670           }
671       }
672       break;
673
674     case ISKMAP:
675       if (map[key].function != (Function *)NULL)
676         {
677           int newkey;
678
679           rl_key_sequence_length++;
680           newkey = rl_read_key ();
681           r = _rl_dispatch (newkey, FUNCTION_TO_KEYMAP (map, key));
682         }
683       else
684         {
685           rl_abort ();
686           return -1;
687         }
688       break;
689
690     case ISMACR:
691       if (map[key].function != (Function *)NULL)
692         {
693           char *macro;
694
695           macro = savestring ((char *)map[key].function);
696           with_macro_input (macro);
697           return 0;
698         }
699       break;
700     }
701 #if defined (VI_MODE)
702   if (rl_editing_mode == vi_mode && _rl_keymap == vi_movement_keymap &&
703       rl_vi_textmod_command (key))
704     _rl_vi_set_last (key, rl_numeric_arg, rl_arg_sign);
705 #endif
706   return (r);
707 }
708
709 \f
710 /* **************************************************************** */
711 /*                                                                  */
712 /*                      Hacking Keyboard Macros                     */
713 /*                                                                  */
714 /* **************************************************************** */
715
716 /* The currently executing macro string.  If this is non-zero,
717    then it is a malloc ()'ed string where input is coming from. */
718 static char *executing_macro = (char *)NULL;
719
720 /* The offset in the above string to the next character to be read. */
721 static int executing_macro_index = 0;
722
723 /* The current macro string being built.  Characters get stuffed
724    in here by add_macro_char (). */
725 static char *current_macro = (char *)NULL;
726
727 /* The size of the buffer allocated to current_macro. */
728 static int current_macro_size = 0;
729
730 /* The index at which characters are being added to current_macro. */
731 static int current_macro_index = 0;
732
733 /* A structure used to save nested macro strings.
734    It is a linked list of string/index for each saved macro. */
735 struct saved_macro {
736   struct saved_macro *next;
737   char *string;
738   int sindex;
739 };
740
741 /* The list of saved macros. */
742 struct saved_macro *macro_list = (struct saved_macro *)NULL;
743
744 /* Forward declarations of static functions.  Thank you C. */
745 static void push_executing_macro (), pop_executing_macro ();
746
747 /* This one has to be declared earlier in the file. */
748 /* static void add_macro_char (); */
749
750 /* Set up to read subsequent input from STRING.
751    STRING is free ()'ed when we are done with it. */
752 static void
753 with_macro_input (string)
754      char *string;
755 {
756   push_executing_macro ();
757   executing_macro = string;
758   executing_macro_index = 0;
759 }
760
761 /* Return the next character available from a macro, or 0 if
762    there are no macro characters. */
763 static int
764 next_macro_key ()
765 {
766   if (!executing_macro)
767     return (0);
768
769   if (!executing_macro[executing_macro_index])
770     {
771       pop_executing_macro ();
772       return (next_macro_key ());
773     }
774
775   return (executing_macro[executing_macro_index++]);
776 }
777
778 /* Save the currently executing macro on a stack of saved macros. */
779 static void
780 push_executing_macro ()
781 {
782   struct saved_macro *saver;
783
784   saver = (struct saved_macro *)xmalloc (sizeof (struct saved_macro));
785   saver->next = macro_list;
786   saver->sindex = executing_macro_index;
787   saver->string = executing_macro;
788
789   macro_list = saver;
790 }
791
792 /* Discard the current macro, replacing it with the one
793    on the top of the stack of saved macros. */
794 static void
795 pop_executing_macro ()
796 {
797   if (executing_macro)
798     free (executing_macro);
799
800   executing_macro = (char *)NULL;
801   executing_macro_index = 0;
802
803   if (macro_list)
804     {
805       struct saved_macro *disposer = macro_list;
806       executing_macro = macro_list->string;
807       executing_macro_index = macro_list->sindex;
808       macro_list = macro_list->next;
809       free (disposer);
810     }
811 }
812
813 /* Add a character to the macro being built. */
814 static void
815 add_macro_char (c)
816      int c;
817 {
818   if (current_macro_index + 1 >= current_macro_size)
819     {
820       if (!current_macro)
821         current_macro = xmalloc (current_macro_size = 25);
822       else
823         current_macro = xrealloc (current_macro, current_macro_size += 25);
824     }
825
826   current_macro[current_macro_index++] = c;
827   current_macro[current_macro_index] = '\0';
828 }
829
830 /* Begin defining a keyboard macro.
831    Keystrokes are recorded as they are executed.
832    End the definition with rl_end_kbd_macro ().
833    If a numeric argument was explicitly typed, then append this
834    definition to the end of the existing macro, and start by
835    re-executing the existing macro. */
836 rl_start_kbd_macro (ignore1, ignore2)
837      int ignore1, ignore2;
838 {
839   if (defining_kbd_macro)
840     {
841       rl_abort ();
842       return -1;
843     }
844
845   if (rl_explicit_arg)
846     {
847       if (current_macro)
848         with_macro_input (savestring (current_macro));
849     }
850   else
851     current_macro_index = 0;
852
853   defining_kbd_macro = 1;
854   return 0;
855 }
856
857 /* Stop defining a keyboard macro.
858    A numeric argument says to execute the macro right now,
859    that many times, counting the definition as the first time. */
860 rl_end_kbd_macro (count, ignore)
861      int count, ignore;
862 {
863   if (!defining_kbd_macro)
864     {
865       rl_abort ();
866       return -1;
867     }
868
869   current_macro_index -= (rl_key_sequence_length - 1);
870   current_macro[current_macro_index] = '\0';
871
872   defining_kbd_macro = 0;
873
874   return (rl_call_last_kbd_macro (--count, 0));
875 }
876
877 /* Execute the most recently defined keyboard macro.
878    COUNT says how many times to execute it. */
879 rl_call_last_kbd_macro (count, ignore)
880      int count, ignore;
881 {
882   if (!current_macro)
883     rl_abort ();
884
885   if (defining_kbd_macro)
886     {
887       ding ();          /* no recursive macros */
888       current_macro[--current_macro_index] = '\0';      /* erase this char */
889       return 0;
890     }
891
892   while (count--)
893     with_macro_input (savestring (current_macro));
894   return 0;
895 }
896
897 void
898 _rl_kill_kbd_macro ()
899 {
900   if (current_macro)
901     {
902       free (current_macro);
903       current_macro = (char *) NULL;
904     }
905   current_macro_size = current_macro_index = 0;
906
907   if (executing_macro)
908     {
909       free (executing_macro);
910       executing_macro = (char *) NULL;
911     }
912   executing_macro_index = 0;
913
914   defining_kbd_macro = 0;
915 }
916 \f
917 /* **************************************************************** */
918 /*                                                                  */
919 /*                      Initializations                             */
920 /*                                                                  */
921 /* **************************************************************** */
922
923 /* Initliaze readline (and terminal if not already). */
924 rl_initialize ()
925 {
926   /* If we have never been called before, initialize the
927      terminal and data structures. */
928   if (!rl_initialized)
929     {
930       readline_initialize_everything ();
931       rl_initialized++;
932     }
933
934   /* Initalize the current line information. */
935   rl_point = rl_end = 0;
936   the_line = rl_line_buffer;
937   the_line[0] = 0;
938
939   /* We aren't done yet.  We haven't even gotten started yet! */
940   rl_done = 0;
941
942   /* Tell the history routines what is going on. */
943   start_using_history ();
944
945   /* Make the display buffer match the state of the line. */
946   rl_reset_line_state ();
947
948   /* No such function typed yet. */
949   rl_last_func = (Function *)NULL;
950
951   /* Parsing of key-bindings begins in an enabled state. */
952   _rl_parsing_conditionalized_out = 0;
953
954   return 0;
955 }
956
957 /* Initialize the entire state of the world. */
958 static void
959 readline_initialize_everything ()
960 {
961   char *t;
962
963   /* Find out if we are running in Emacs. */
964   running_in_emacs = getenv ("EMACS") != (char *)0;
965
966   /* Set up input and output if they are not already set up. */
967   if (!rl_instream)
968     rl_instream = stdin;
969
970   if (!rl_outstream)
971     rl_outstream = stdout;
972
973   /* Bind in_stream and out_stream immediately.  These values may change,
974      but they may also be used before readline_internal () is called. */
975   in_stream = rl_instream;
976   out_stream = rl_outstream;
977
978   /* Allocate data structures. */
979   if (!rl_line_buffer)
980     rl_line_buffer = xmalloc (rl_line_buffer_len = DEFAULT_BUFFER_SIZE);
981
982   /* Initialize the terminal interface. */
983   init_terminal_io ((char *)NULL);
984
985 #if !defined (__GO32__)
986   /* Bind tty characters to readline functions. */
987   readline_default_bindings ();
988 #endif /* !__GO32__ */
989
990   /* Initialize the function names. */
991   rl_initialize_funmap ();
992
993   /* Check for LC_CTYPE and use its value to decide the defaults for
994      8-bit character input and output. */
995   t = getenv ("LC_CTYPE");
996   if (t && (strcmp (t, "iso-8859-1") == 0 || strcmp (t, "iso_8859_1") == 0 ||
997             strcmp (t, "ISO-8859-1") == 0))
998     {
999       _rl_meta_flag = 1;
1000       _rl_convert_meta_chars_to_ascii = 0;
1001       _rl_output_meta_chars = 1;
1002     }
1003       
1004   /* Read in the init file. */
1005   rl_read_init_file ((char *)NULL);
1006
1007   /* XXX */
1008   if (_rl_horizontal_scroll_mode && term_xn)
1009     {
1010       screenwidth--;
1011       screenchars -= screenheight;
1012     }
1013
1014   /* Override the effect of any `set keymap' assignments in the
1015      inputrc file. */
1016   rl_set_keymap_from_edit_mode ();
1017
1018   /* Try to bind a common arrow key prefix, if not already bound. */
1019   bind_arrow_keys ();
1020
1021   /* If the completion parser's default word break characters haven't
1022      been set yet, then do so now. */
1023   if (rl_completer_word_break_characters == (char *)NULL)
1024     rl_completer_word_break_characters = rl_basic_word_break_characters;
1025 }
1026
1027 /* If this system allows us to look at the values of the regular
1028    input editing characters, then bind them to their readline
1029    equivalents, iff the characters are not bound to keymaps. */
1030 static void
1031 readline_default_bindings ()
1032 {
1033   rltty_set_default_bindings (_rl_keymap);
1034 }
1035
1036 static void
1037 bind_arrow_keys_internal ()
1038 {
1039   Function *f;
1040
1041   f = rl_function_of_keyseq ("\033[A", _rl_keymap, (int *)NULL);
1042   if (!f || f == rl_do_lowercase_version)
1043     {
1044       _rl_bind_if_unbound ("\033[A", rl_get_previous_history);
1045       _rl_bind_if_unbound ("\033[B", rl_get_next_history);
1046       _rl_bind_if_unbound ("\033[C", rl_forward);
1047       _rl_bind_if_unbound ("\033[D", rl_backward);
1048     }
1049
1050   f = rl_function_of_keyseq ("\033OA", _rl_keymap, (int *)NULL);
1051   if (!f || f == rl_do_lowercase_version)
1052     {
1053       _rl_bind_if_unbound ("\033OA", rl_get_previous_history);
1054       _rl_bind_if_unbound ("\033OB", rl_get_next_history);
1055       _rl_bind_if_unbound ("\033OC", rl_forward);
1056       _rl_bind_if_unbound ("\033OD", rl_backward);
1057     }
1058 }
1059
1060 /* Try and bind the common arrow key prefix after giving termcap and
1061    the inputrc file a chance to bind them and create `real' keymaps
1062    for the arrow key prefix. */
1063 static void
1064 bind_arrow_keys ()
1065 {
1066   Keymap xkeymap;
1067
1068   xkeymap = _rl_keymap;
1069
1070   _rl_keymap = emacs_standard_keymap;
1071   bind_arrow_keys_internal ();
1072
1073 #if defined (VI_MODE)
1074   _rl_keymap = vi_movement_keymap;
1075   bind_arrow_keys_internal ();
1076 #endif
1077
1078   _rl_keymap = xkeymap;
1079 }
1080
1081 \f
1082 /* **************************************************************** */
1083 /*                                                                  */
1084 /*                      Numeric Arguments                           */
1085 /*                                                                  */
1086 /* **************************************************************** */
1087
1088 /* Handle C-u style numeric args, as well as M--, and M-digits. */
1089 static int
1090 rl_digit_loop ()
1091 {
1092   int key, c;
1093
1094   while (1)
1095     {
1096       rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg);
1097       key = c = rl_read_key ();
1098
1099       if (_rl_keymap[c].type == ISFUNC &&
1100           _rl_keymap[c].function == rl_universal_argument)
1101         {
1102           rl_numeric_arg *= 4;
1103           continue;
1104         }
1105       c = UNMETA (c);
1106       if (digit_p (c))
1107         {
1108           if (rl_explicit_arg)
1109             rl_numeric_arg = (rl_numeric_arg * 10) + (c - '0');
1110           else
1111             rl_numeric_arg = (c - '0');
1112           rl_explicit_arg = 1;
1113         }
1114       else
1115         {
1116           if (c == '-' && !rl_explicit_arg)
1117             {
1118               rl_numeric_arg = 1;
1119               rl_arg_sign = -1;
1120             }
1121           else
1122             {
1123               rl_clear_message ();
1124               return (_rl_dispatch (key, _rl_keymap));
1125             }
1126         }
1127     }
1128   return 0;
1129 }
1130
1131 /* Add the current digit to the argument in progress. */
1132 rl_digit_argument (ignore, key)
1133      int ignore, key;
1134 {
1135   rl_pending_input = key;
1136   return (rl_digit_loop ());
1137 }
1138
1139 /* What to do when you abort reading an argument. */
1140 rl_discard_argument ()
1141 {
1142   ding ();
1143   rl_clear_message ();
1144   rl_init_argument ();
1145   return 0;
1146 }
1147
1148 /* Create a default argument. */
1149 rl_init_argument ()
1150 {
1151   rl_numeric_arg = rl_arg_sign = 1;
1152   rl_explicit_arg = 0;
1153   return 0;
1154 }
1155
1156 /* C-u, universal argument.  Multiply the current argument by 4.
1157    Read a key.  If the key has nothing to do with arguments, then
1158    dispatch on it.  If the key is the abort character then abort. */
1159 rl_universal_argument ()
1160 {
1161   rl_numeric_arg *= 4;
1162   return (rl_digit_loop ());
1163 }
1164 \f
1165 /* **************************************************************** */
1166 /*                                                                  */
1167 /*                      Terminal and Termcap                        */
1168 /*                                                                  */
1169 /* **************************************************************** */
1170
1171 static char *term_buffer = (char *)NULL;
1172 static char *term_string_buffer = (char *)NULL;
1173
1174 static int tcap_initialized = 0;
1175
1176 /* Non-zero means this terminal can't really do anything. */
1177 int dumb_term = 0;
1178 /* On Solaris2, sys/types.h #includes sys/reg.h, which #defines PC.
1179    Unfortunately, PC is a global variable used by the termcap library. */
1180 #undef PC
1181
1182 #if !defined (__linux__)
1183 /* If this causes problems, add back the `extern'. */
1184 /*extern*/ char PC, *BC, *UP;
1185 #endif /* __linux__ */
1186
1187 /* Some strings to control terminal actions.  These are output by tputs (). */
1188 char *term_goto, *term_clreol, *term_cr, *term_clrpag, *term_backspace;
1189 char *term_pc;
1190
1191 /* Non-zero if we determine that the terminal can do character insertion. */
1192 int terminal_can_insert = 0;
1193
1194 /* How to insert characters. */
1195 char *term_im, *term_ei, *term_ic, *term_ip, *term_IC;
1196
1197 /* How to delete characters. */
1198 char *term_dc, *term_DC;
1199
1200 #if defined (HACK_TERMCAP_MOTION)
1201 char *term_forward_char;
1202 #endif  /* HACK_TERMCAP_MOTION */
1203
1204 /* How to go up a line. */
1205 char *term_up;
1206
1207 /* A visible bell, if the terminal can be made to flash the screen. */
1208 char *visible_bell;
1209
1210 /* Non-zero means that this terminal has a meta key. */
1211 int term_has_meta;
1212
1213 /* The string to write to turn on the meta key, if this term has one. */
1214 char *term_mm;
1215
1216 /* The string to write to turn off the meta key, if this term has one. */
1217 char *term_mo;
1218
1219 /* The key sequences output by the arrow keys, if this terminal has any. */
1220 char *term_ku, *term_kd, *term_kr, *term_kl;
1221
1222 /* How to initialize and reset the arrow keys, if this terminal has any. */
1223 char *term_ks, *term_ke;
1224
1225 /* Re-initialize the terminal considering that the TERM/TERMCAP variable
1226    has changed. */
1227 rl_reset_terminal (terminal_name)
1228      char *terminal_name;
1229 {
1230   init_terminal_io (terminal_name);
1231   return 0;
1232 }
1233
1234 /* Set readline's idea of the screen size.  TTY is a file descriptor open
1235    to the terminal.  If IGNORE_ENV is true, we do not pay attention to the
1236    values of $LINES and $COLUMNS.  The tests for TERM_STRING_BUFFER being
1237    non-null serve to check whether or not we have initialized termcap. */
1238 void
1239 _rl_set_screen_size (tty, ignore_env)
1240      int tty, ignore_env;
1241 {
1242 #if defined (TIOCGWINSZ)
1243   struct winsize window_size;
1244 #endif /* TIOCGWINSZ */
1245
1246 #if defined (TIOCGWINSZ)
1247   if (ioctl (tty, TIOCGWINSZ, &window_size) == 0)
1248     {
1249       screenwidth = (int) window_size.ws_col;
1250       screenheight = (int) window_size.ws_row;
1251     }
1252 #endif /* TIOCGWINSZ */
1253
1254   /* Environment variable COLUMNS overrides setting of "co" if IGNORE_ENV
1255      is unset. */
1256   if (screenwidth <= 0)
1257     {
1258       char *sw;
1259
1260       if (!ignore_env && (sw = getenv ("COLUMNS")))
1261         screenwidth = atoi (sw);
1262
1263       if (screenwidth <= 0 && term_string_buffer)
1264         screenwidth = tgetnum ("co");
1265     }
1266
1267   /* Environment variable LINES overrides setting of "li" if IGNORE_ENV
1268      is unset. */
1269   if (screenheight <= 0)
1270     {
1271       char *sh;
1272
1273       if (!ignore_env && (sh = getenv ("LINES")))
1274         screenheight = atoi (sh);
1275
1276       if (screenheight <= 0 && term_string_buffer)
1277         screenheight = tgetnum ("li");
1278     }
1279
1280   /* If all else fails, default to 80x24 terminal. */
1281   if (screenwidth <= 1)
1282     screenwidth = 80;
1283
1284   if (screenheight <= 0)
1285     screenheight = 24;
1286
1287 #if defined (SHELL)
1288   /* If we're being compiled as part of bash, set the environment
1289      variables $LINES and $COLUMNS to new values. */
1290   set_lines_and_columns (screenheight, screenwidth);
1291 #endif
1292
1293   if (!term_xn)
1294     screenwidth--;
1295
1296   screenchars = screenwidth * screenheight;
1297 }
1298
1299 struct _tc_string {
1300      char *tc_var;
1301      char **tc_value;
1302 };
1303
1304 /* This should be kept sorted, just in case we decide to change the
1305    search algorithm to something smarter. */
1306 static struct _tc_string tc_strings[] =
1307 {
1308   "DC", &term_DC,
1309   "IC", &term_IC,
1310   "ce", &term_clreol,
1311   "cl", &term_clrpag,
1312   "cr", &term_cr,
1313   "dc", &term_dc,
1314   "ei", &term_ei,
1315   "ic", &term_ic,
1316   "im", &term_im,
1317   "kd", &term_kd,
1318   "kl", &term_kl,
1319   "kr", &term_kr,
1320   "ku", &term_ku,
1321   "ks", &term_ks,
1322   "ke", &term_ke,
1323   "le", &term_backspace,
1324   "mm", &term_mm,
1325   "mo", &term_mo,
1326 #if defined (HACK_TERMCAP_MOTION)
1327   "nd", &term_forward_char,
1328 #endif
1329   "pc", &term_pc,
1330   "up", &term_up,
1331   "vb", &visible_bell,
1332 };
1333
1334 #define NUM_TC_STRINGS (sizeof (tc_strings) / sizeof (struct _tc_string))
1335
1336 /* Read the desired terminal capability strings into BP.  The capabilities
1337    are described in the TC_STRINGS table. */
1338 static void
1339 get_term_capabilities (bp)
1340      char **bp;
1341 {
1342   register int i;
1343
1344   for (i = 0; i < NUM_TC_STRINGS; i++)
1345     *(tc_strings[i].tc_value) = tgetstr (tc_strings[i].tc_var, bp);
1346   tcap_initialized = 1;
1347 }
1348
1349 static int
1350 init_terminal_io (terminal_name)
1351      char *terminal_name;
1352 {
1353 #if defined (__GO32__)
1354   screenwidth = ScreenCols ();
1355   screenheight = ScreenRows ();
1356   screenchars = screenwidth * screenheight;
1357   term_cr = "\r";
1358   term_im = term_ei = term_ic = term_IC = (char *)NULL;
1359   term_up = term_dc = term_DC = visible_bell = (char *)NULL;
1360
1361   /* Does the __GO32__ have a meta key?  I don't know. */
1362   term_has_meta = 0;
1363   term_mm = term_mo = (char *)NULL;
1364
1365   /* It probably has arrow keys, but I don't know what they are. */
1366   term_ku = term_kd = term_kr = term_kl = (char *)NULL;
1367
1368 #if defined (HACK_TERMCAP_MOTION)
1369   term_forward_char = (char *)NULL;
1370 #endif /* HACK_TERMCAP_MOTION */
1371   terminal_can_insert = term_xn = 0;
1372   return;
1373 #else /* !__GO32__ */
1374
1375   char *term, *buffer;
1376   int tty;
1377   Keymap xkeymap;
1378
1379   term = terminal_name ? terminal_name : getenv ("TERM");
1380
1381   if (!term_string_buffer)
1382     term_string_buffer = xmalloc (2048);
1383
1384   if (!term_buffer)
1385     term_buffer = xmalloc (2048);
1386
1387   buffer = term_string_buffer;
1388
1389   term_clrpag = term_cr = term_clreol = (char *)NULL;
1390
1391   if (!term)
1392     term = "dumb";
1393
1394   if (tgetent (term_buffer, term) <= 0)
1395     {
1396       dumb_term = 1;
1397       screenwidth = 79;
1398       screenheight = 24;
1399       screenchars = 79 * 24;
1400       term_cr = "\r";
1401       term_im = term_ei = term_ic = term_IC = (char *)NULL;
1402       term_up = term_dc = term_DC = visible_bell = (char *)NULL;
1403       term_ku = term_kd = term_kl = term_kr = (char *)NULL;
1404 #if defined (HACK_TERMCAP_MOTION)
1405       term_forward_char = (char *)NULL;
1406 #endif
1407       terminal_can_insert = 0;
1408       return 0;
1409     }
1410
1411   get_term_capabilities (&buffer);
1412
1413   /* Set up the variables that the termcap library expects the application
1414      to provide. */
1415   PC = term_pc ? *term_pc : 0;
1416   BC = term_backspace;
1417   UP = term_up;
1418
1419   if (!term_cr)
1420     term_cr =  "\r";
1421
1422   if (rl_instream)
1423     tty = fileno (rl_instream);
1424   else
1425     tty = 0;
1426
1427   screenwidth = screenheight = 0;
1428
1429   term_xn = tgetflag ("am") && tgetflag ("xn");
1430
1431   _rl_set_screen_size (tty, 0);
1432
1433   /* "An application program can assume that the terminal can do
1434       character insertion if *any one of* the capabilities `IC',
1435       `im', `ic' or `ip' is provided."  But we can't do anything if
1436       only `ip' is provided, so... */
1437   terminal_can_insert = (term_IC || term_im || term_ic);
1438
1439   /* Check to see if this terminal has a meta key and clear the capability
1440      variables if there is none. */
1441   term_has_meta = (tgetflag ("km") || tgetflag ("MT"));
1442   if (!term_has_meta)
1443     {
1444       term_mm = (char *)NULL;
1445       term_mo = (char *)NULL;
1446     }
1447
1448   /* Attempt to find and bind the arrow keys.  Do not override already
1449      bound keys in an overzealous attempt, however. */
1450   xkeymap = _rl_keymap;
1451
1452   _rl_keymap = emacs_standard_keymap;
1453   _rl_bind_if_unbound (term_ku, rl_get_previous_history);
1454   _rl_bind_if_unbound (term_kd, rl_get_next_history);
1455   _rl_bind_if_unbound (term_kr, rl_forward);
1456   _rl_bind_if_unbound (term_kl, rl_backward);
1457
1458 #if defined (VI_MODE)
1459   _rl_keymap = vi_movement_keymap;
1460   _rl_bind_if_unbound (term_ku, rl_get_previous_history);
1461   _rl_bind_if_unbound (term_kd, rl_get_next_history);
1462   _rl_bind_if_unbound (term_kr, rl_forward);
1463   _rl_bind_if_unbound (term_kl, rl_backward);
1464 #endif /* VI_MODE */
1465
1466   _rl_keymap = xkeymap;
1467
1468 #endif /* !__GO32__ */
1469   return 0;
1470 }
1471
1472 char *
1473 rl_get_termcap (cap)
1474      char *cap;
1475 {
1476   register int i;
1477
1478   if (tcap_initialized == 0)
1479     return ((char *)NULL);
1480   for (i = 0; i < NUM_TC_STRINGS; i++)
1481     {
1482       if (tc_strings[i].tc_var[0] == cap[0] && strcmp (tc_strings[i].tc_var, cap) == 0)
1483         return *(tc_strings[i].tc_value);
1484     }
1485   return ((char *)NULL);
1486 }
1487
1488 /* A function for the use of tputs () */
1489 int
1490 _rl_output_character_function (c)
1491      int c;
1492 {
1493   return putc (c, out_stream);
1494 }
1495
1496 /* Write COUNT characters from STRING to the output stream. */
1497 void
1498 _rl_output_some_chars (string, count)
1499      char *string;
1500      int count;
1501 {
1502   fwrite (string, 1, count, out_stream);
1503 }
1504
1505 /* Move the cursor back. */
1506 backspace (count)
1507      int count;
1508 {
1509   register int i;
1510
1511 #if !defined (__GO32__)
1512   if (term_backspace)
1513     for (i = 0; i < count; i++)
1514       tputs (term_backspace, 1, _rl_output_character_function);
1515   else
1516 #endif /* !__GO32__ */
1517     for (i = 0; i < count; i++)
1518       putc ('\b', out_stream);
1519   return 0;
1520 }
1521
1522 /* Move to the start of the next line. */
1523 crlf ()
1524 {
1525 #if defined (NEW_TTY_DRIVER)
1526   tputs (term_cr, 1, _rl_output_character_function);
1527 #endif /* NEW_TTY_DRIVER */
1528   putc ('\n', out_stream);
1529   return 0;
1530 }
1531
1532 rl_tty_status (count, key)
1533      int count, key;
1534 {
1535 #if defined (TIOCSTAT)
1536   ioctl (1, TIOCSTAT, (char *)0);
1537   rl_refresh_line ();
1538 #else
1539   ding ();
1540 #endif
1541   return 0;
1542 }
1543
1544 \f
1545 /* **************************************************************** */
1546 /*                                                                  */
1547 /*                      Utility Functions                           */
1548 /*                                                                  */
1549 /* **************************************************************** */
1550
1551 /* Return 0 if C is not a member of the class of characters that belong
1552    in words, or 1 if it is. */
1553
1554 int allow_pathname_alphabetic_chars = 0;
1555 char *pathname_alphabetic_chars = "/-_=~.#$";
1556
1557 int
1558 alphabetic (c)
1559      int c;
1560 {
1561   if (pure_alphabetic (c) || (digit_p (c)))
1562     return (1);
1563
1564   if (allow_pathname_alphabetic_chars)
1565     return (strchr (pathname_alphabetic_chars, c) != NULL);
1566   else
1567     return (0);
1568 }
1569
1570 /* Ring the terminal bell. */
1571 int
1572 ding ()
1573 {
1574   if (readline_echoing_p)
1575     {
1576 #if !defined (__GO32__)
1577       switch (_rl_bell_preference)
1578         {
1579         case NO_BELL:
1580         default:
1581           break;
1582         case VISIBLE_BELL:
1583           if (visible_bell)
1584             {
1585               tputs (visible_bell, 1, _rl_output_character_function);
1586               break;
1587             }
1588           /* FALLTHROUGH */
1589         case AUDIBLE_BELL:
1590           fprintf (stderr, "\007");
1591           fflush (stderr);
1592           break;
1593         }
1594 #else /* __GO32__ */
1595       fprintf (stderr, "\007");
1596       fflush (stderr);
1597 #endif /* __GO32__ */
1598       return (0);
1599     }
1600   return (-1);
1601 }
1602
1603 /* How to abort things. */
1604 rl_abort (count, key)
1605      int count, key;
1606 {
1607   ding ();
1608   rl_clear_message ();
1609   rl_init_argument ();
1610   rl_pending_input = 0;
1611
1612   defining_kbd_macro = 0;
1613   while (executing_macro)
1614     pop_executing_macro ();
1615
1616   rl_last_func = (Function *)NULL;
1617   longjmp (readline_top_level, 1);
1618 }
1619
1620 /* Return a copy of the string between FROM and TO.
1621    FROM is inclusive, TO is not. */
1622 char *
1623 rl_copy_text (from, to)
1624      int from, to;
1625 {
1626   register int length;
1627   char *copy;
1628
1629   /* Fix it if the caller is confused. */
1630   if (from > to)
1631     {
1632       int t = from;
1633       from = to;
1634       to = t;
1635     }
1636
1637   length = to - from;
1638   copy = xmalloc (1 + length);
1639   strncpy (copy, the_line + from, length);
1640   copy[length] = '\0';
1641   return (copy);
1642 }
1643
1644 /* Increase the size of RL_LINE_BUFFER until it has enough space to hold
1645    LEN characters. */
1646 void
1647 rl_extend_line_buffer (len)
1648      int len;
1649 {
1650   while (len >= rl_line_buffer_len)
1651     {
1652       rl_line_buffer_len += DEFAULT_BUFFER_SIZE;
1653       rl_line_buffer = xrealloc (rl_line_buffer, rl_line_buffer_len);
1654     }
1655
1656   the_line = rl_line_buffer;
1657 }
1658
1659 \f
1660 /* **************************************************************** */
1661 /*                                                                  */
1662 /*                      Insert and Delete                           */
1663 /*                                                                  */
1664 /* **************************************************************** */
1665
1666 /* Insert a string of text into the line at point.  This is the only
1667    way that you should do insertion.  rl_insert () calls this
1668    function. */
1669 rl_insert_text (string)
1670      char *string;
1671 {
1672   register int i, l = strlen (string);
1673
1674   if (rl_end + l >= rl_line_buffer_len)
1675     rl_extend_line_buffer (rl_end + l);
1676
1677   for (i = rl_end; i >= rl_point; i--)
1678     the_line[i + l] = the_line[i];
1679   strncpy (the_line + rl_point, string, l);
1680
1681   /* Remember how to undo this if we aren't undoing something. */
1682   if (!doing_an_undo)
1683     {
1684       /* If possible and desirable, concatenate the undos. */
1685       if ((l == 1) &&
1686           rl_undo_list &&
1687           (rl_undo_list->what == UNDO_INSERT) &&
1688           (rl_undo_list->end == rl_point) &&
1689           (rl_undo_list->end - rl_undo_list->start < 20))
1690         rl_undo_list->end++;
1691       else
1692         rl_add_undo (UNDO_INSERT, rl_point, rl_point + l, (char *)NULL);
1693     }
1694   rl_point += l;
1695   rl_end += l;
1696   the_line[rl_end] = '\0';
1697   return l;
1698 }
1699
1700 /* Delete the string between FROM and TO.  FROM is
1701    inclusive, TO is not. */
1702 rl_delete_text (from, to)
1703      int from, to;
1704 {
1705   register char *text;
1706   register int diff, i;
1707
1708   /* Fix it if the caller is confused. */
1709   if (from > to)
1710     {
1711       int t = from;
1712       from = to;
1713       to = t;
1714     }
1715
1716   if (to > rl_end)
1717     to = rl_end;
1718
1719   text = rl_copy_text (from, to);
1720
1721   /* Some versions of strncpy() can't handle overlapping arguments. */
1722   diff = to - from;
1723   for (i = from; i < rl_end - diff; i++)
1724     the_line[i] = the_line[i + diff];
1725
1726   /* Remember how to undo this delete. */
1727   if (!doing_an_undo)
1728     rl_add_undo (UNDO_DELETE, from, to, text);
1729   else
1730     free (text);
1731
1732   rl_end -= diff;
1733   the_line[rl_end] = '\0';
1734   return (diff);
1735 }
1736
1737 \f
1738 /* **************************************************************** */
1739 /*                                                                  */
1740 /*                      Readline character functions                */
1741 /*                                                                  */
1742 /* **************************************************************** */
1743
1744 /* This is not a gap editor, just a stupid line input routine.  No hair
1745    is involved in writing any of the functions, and none should be. */
1746
1747 /* Note that:
1748
1749    rl_end is the place in the string that we would place '\0';
1750    i.e., it is always safe to place '\0' there.
1751
1752    rl_point is the place in the string where the cursor is.  Sometimes
1753    this is the same as rl_end.
1754
1755    Any command that is called interactively receives two arguments.
1756    The first is a count: the numeric arg pased to this command.
1757    The second is the key which invoked this command.
1758 */
1759
1760 \f
1761 /* **************************************************************** */
1762 /*                                                                  */
1763 /*                      Movement Commands                           */
1764 /*                                                                  */
1765 /* **************************************************************** */
1766
1767 /* Note that if you `optimize' the display for these functions, you cannot
1768    use said functions in other functions which do not do optimizing display.
1769    I.e., you will have to update the data base for rl_redisplay, and you
1770    might as well let rl_redisplay do that job. */
1771
1772 /* Move forward COUNT characters. */
1773 rl_forward (count, key)
1774      int count, key;
1775 {
1776   if (count < 0)
1777     rl_backward (-count);
1778   else if (count > 0)
1779     {
1780       int end = rl_point + count;
1781 #if defined (VI_MODE)
1782       int lend = rl_end - (rl_editing_mode == vi_mode);
1783 #else
1784       int lend = rl_end;
1785 #endif
1786
1787       if (end > lend)
1788         {
1789           rl_point = lend;
1790           ding ();
1791         }
1792       else
1793         rl_point = end;
1794     }
1795   return 0;
1796 }
1797
1798 /* Move backward COUNT characters. */
1799 rl_backward (count, key)
1800      int count, key;
1801 {
1802   if (count < 0)
1803     rl_forward (-count);
1804   else if (count > 0)
1805     {
1806       if (rl_point < count)
1807         {
1808           rl_point = 0;
1809           ding ();
1810         }
1811       else
1812         rl_point -= count;
1813     }
1814   return 0;
1815 }
1816
1817 /* Move to the beginning of the line. */
1818 rl_beg_of_line (count, key)
1819      int count, key;
1820 {
1821   rl_point = 0;
1822   return 0;
1823 }
1824
1825 /* Move to the end of the line. */
1826 rl_end_of_line (count, key)
1827      int count, key;
1828 {
1829   rl_point = rl_end;
1830   return 0;
1831 }
1832
1833 /* Move forward a word.  We do what Emacs does. */
1834 rl_forward_word (count, key)
1835      int count, key;
1836 {
1837   int c;
1838
1839   if (count < 0)
1840     {
1841       rl_backward_word (-count);
1842       return 0;
1843     }
1844
1845   while (count)
1846     {
1847       if (rl_point == rl_end)
1848         return 0;
1849
1850       /* If we are not in a word, move forward until we are in one.
1851          Then, move forward until we hit a non-alphabetic character. */
1852       c = the_line[rl_point];
1853       if (!alphabetic (c))
1854         {
1855           while (++rl_point < rl_end)
1856             {
1857               c = the_line[rl_point];
1858               if (alphabetic (c))
1859                 break;
1860             }
1861         }
1862       if (rl_point == rl_end)
1863         return 0;
1864       while (++rl_point < rl_end)
1865         {
1866           c = the_line[rl_point];
1867           if (!alphabetic (c))
1868             break;
1869         }
1870       --count;
1871     }
1872   return 0;
1873 }
1874
1875 /* Move backward a word.  We do what Emacs does. */
1876 rl_backward_word (count, key)
1877      int count, key;
1878 {
1879   int c;
1880
1881   if (count < 0)
1882     {
1883       rl_forward_word (-count);
1884       return 0;
1885     }
1886
1887   while (count)
1888     {
1889       if (!rl_point)
1890         return 0;
1891
1892       /* Like rl_forward_word (), except that we look at the characters
1893          just before point. */
1894
1895       c = the_line[rl_point - 1];
1896       if (!alphabetic (c))
1897         {
1898           while (--rl_point)
1899             {
1900               c = the_line[rl_point - 1];
1901               if (alphabetic (c))
1902                 break;
1903             }
1904         }
1905
1906       while (rl_point)
1907         {
1908           c = the_line[rl_point - 1];
1909           if (!alphabetic (c))
1910             break;
1911           else
1912             --rl_point;
1913         }
1914       --count;
1915     }
1916   return 0;
1917 }
1918
1919 /* Clear the current line.  Numeric argument to C-l does this. */
1920 rl_refresh_line ()
1921 {
1922   int curr_line, nleft;
1923
1924   /* Find out whether or not there might be invisible characters in the
1925      editing buffer. */
1926   if (rl_display_prompt == rl_prompt)
1927     nleft = _rl_last_c_pos - screenwidth - rl_visible_prompt_length;
1928   else
1929     nleft = _rl_last_c_pos - screenwidth;
1930
1931   if (nleft > 0)
1932     curr_line = 1 + nleft / screenwidth;
1933   else
1934     curr_line = 0;
1935
1936   _rl_move_vert (curr_line);
1937   _rl_move_cursor_relative (0, the_line);   /* XXX is this right */
1938
1939 #if defined (__GO32__)
1940   {
1941     int row, col, width, row_start;
1942
1943     ScreenGetCursor (&row, &col);
1944     width = ScreenCols ();
1945     row_start = ScreenPrimary + (row * width);
1946     memset (row_start + col, 0, (width - col) * 2);
1947   }
1948 #else /* !__GO32__ */
1949   if (term_clreol)
1950     tputs (term_clreol, 1, _rl_output_character_function);
1951 #endif /* !__GO32__ */
1952
1953   rl_forced_update_display ();
1954   rl_display_fixed = 1;
1955
1956   return 0;
1957 }
1958
1959 /* C-l typed to a line without quoting clears the screen, and then reprints
1960    the prompt and the current input line.  Given a numeric arg, redraw only
1961    the current line. */
1962 rl_clear_screen (count, key)
1963      int count, key;
1964 {
1965   if (rl_explicit_arg)
1966     {
1967       rl_refresh_line ();
1968       return 0;
1969     }
1970
1971 #if !defined (__GO32__)
1972   if (term_clrpag)
1973     tputs (term_clrpag, 1, _rl_output_character_function);
1974   else
1975 #endif /* !__GO32__ */
1976     crlf ();
1977
1978   rl_forced_update_display ();
1979   rl_display_fixed = 1;
1980
1981   return 0;
1982 }
1983
1984 rl_arrow_keys (count, c)
1985      int count, c;
1986 {
1987   int ch;
1988
1989   ch = rl_read_key ();
1990
1991   switch (to_upper (ch))
1992     {
1993     case 'A':
1994       rl_get_previous_history (count);
1995       break;
1996
1997     case 'B':
1998       rl_get_next_history (count);
1999       break;
2000
2001     case 'C':
2002       rl_forward (count);
2003       break;
2004
2005     case 'D':
2006       rl_backward (count);
2007       break;
2008
2009     default:
2010       ding ();
2011     }
2012   return 0;
2013 }
2014
2015 \f
2016 /* **************************************************************** */
2017 /*                                                                  */
2018 /*                      Text commands                               */
2019 /*                                                                  */
2020 /* **************************************************************** */
2021
2022 /* Insert the character C at the current location, moving point forward. */
2023 rl_insert (count, c)
2024      int count, c;
2025 {
2026   register int i;
2027   char *string;
2028
2029   if (count <= 0)
2030     return 0;
2031
2032   /* If we can optimize, then do it.  But don't let people crash
2033      readline because of extra large arguments. */
2034   if (count > 1 && count < 1024)
2035     {
2036       string = xmalloc (1 + count);
2037
2038       for (i = 0; i < count; i++)
2039         string[i] = c;
2040
2041       string[i] = '\0';
2042       rl_insert_text (string);
2043       free (string);
2044
2045       return 0;
2046     }
2047
2048   if (count > 1024)
2049     {
2050       int decreaser;
2051       char str[1024+1];
2052
2053       for (i = 0; i < 1024; i++)
2054         str[i] = c;
2055
2056       while (count)
2057         {
2058           decreaser = (count > 1024 ? 1024 : count);
2059           str[decreaser] = '\0';
2060           rl_insert_text (str);
2061           count -= decreaser;
2062         }
2063
2064       return 0;
2065     }
2066
2067   /* We are inserting a single character.
2068      If there is pending input, then make a string of all of the
2069      pending characters that are bound to rl_insert, and insert
2070      them all. */
2071   if (any_typein)
2072     {
2073       int key = 0, t;
2074
2075       i = 0;
2076       string = xmalloc (ibuffer_len + 1);
2077       string[i++] = c;
2078
2079       while ((t = rl_get_char (&key)) &&
2080              (_rl_keymap[key].type == ISFUNC &&
2081               _rl_keymap[key].function == rl_insert))
2082         string[i++] = key;
2083
2084       if (t)
2085         rl_unget_char (key);
2086
2087       string[i] = '\0';
2088       rl_insert_text (string);
2089       free (string);
2090     }
2091   else
2092     {
2093       /* Inserting a single character. */
2094       char str[2];
2095
2096       str[1] = '\0';
2097       str[0] = c;
2098       rl_insert_text (str);
2099     }
2100   return 0;
2101 }
2102
2103 /* Insert the next typed character verbatim. */
2104 rl_quoted_insert (count, key)
2105      int count, key;
2106 {
2107   int c;
2108
2109   c = rl_read_key ();
2110   return (rl_insert (count, c));  
2111 }
2112
2113 /* Insert a tab character. */
2114 rl_tab_insert (count, key)
2115      int count, key;
2116 {
2117   return (rl_insert (count, '\t'));
2118 }
2119
2120 /* What to do when a NEWLINE is pressed.  We accept the whole line.
2121    KEY is the key that invoked this command.  I guess it could have
2122    meaning in the future. */
2123 rl_newline (count, key)
2124      int count, key;
2125 {
2126   rl_done = 1;
2127
2128 #if defined (VI_MODE)
2129   _rl_vi_done_inserting ();
2130   _rl_vi_reset_last ();
2131
2132 #endif /* VI_MODE */
2133
2134   if (readline_echoing_p)
2135     _rl_update_final ();
2136   return 0;
2137 }
2138
2139 rl_clean_up_for_exit ()
2140 {
2141   if (readline_echoing_p)
2142     {
2143       _rl_move_vert (_rl_vis_botlin);
2144       _rl_vis_botlin = 0;
2145       fflush (out_stream);
2146       rl_restart_output ();
2147     }
2148   return 0;
2149 }
2150
2151 /* What to do for some uppercase characters, like meta characters,
2152    and some characters appearing in emacs_ctlx_keymap.  This function
2153    is just a stub, you bind keys to it and the code in _rl_dispatch ()
2154    is special cased. */
2155 rl_do_lowercase_version (ignore1, ignore2)
2156      int ignore1, ignore2;
2157 {
2158   return 0;
2159 }
2160
2161 /* Rubout the character behind point. */
2162 rl_rubout (count, key)
2163      int count, key;
2164 {
2165   if (count < 0)
2166     {
2167       rl_delete (-count);
2168       return 0;
2169     }
2170
2171   if (!rl_point)
2172     {
2173       ding ();
2174       return -1;
2175     }
2176
2177   if (count > 1 || rl_explicit_arg)
2178     {
2179       int orig_point = rl_point;
2180       rl_backward (count);
2181       rl_kill_text (orig_point, rl_point);
2182     }
2183   else
2184     {
2185       int c = the_line[--rl_point];
2186       rl_delete_text (rl_point, rl_point + 1);
2187
2188       if (rl_point == rl_end && isprint (c) && _rl_last_c_pos)
2189         {
2190           int l;
2191           l = rl_character_len (c, rl_point);
2192           _rl_erase_at_end_of_line (l);
2193         }
2194     }
2195   return 0;
2196 }
2197
2198 /* Delete the character under the cursor.  Given a numeric argument,
2199    kill that many characters instead. */
2200 rl_delete (count, invoking_key)
2201      int count, invoking_key;
2202 {
2203   if (count < 0)
2204     {
2205       return (rl_rubout (-count));
2206     }
2207
2208   if (rl_point == rl_end)
2209     {
2210       ding ();
2211       return -1;
2212     }
2213
2214   if (count > 1 || rl_explicit_arg)
2215     {
2216       int orig_point = rl_point;
2217       rl_forward (count);
2218       rl_kill_text (orig_point, rl_point);
2219       rl_point = orig_point;
2220       return 0;
2221     }
2222   else
2223     return (rl_delete_text (rl_point, rl_point + 1));
2224   
2225 }
2226
2227 /* Delete all spaces and tabs around point. */
2228 rl_delete_horizontal_space (count, ignore)
2229      int count, ignore;
2230 {
2231   int start = rl_point;
2232
2233   while (rl_point && whitespace (the_line[rl_point - 1]))
2234     rl_point--;
2235
2236   start = rl_point;
2237
2238   while (rl_point < rl_end && whitespace (the_line[rl_point]))
2239     rl_point++;
2240
2241   if (start != rl_point)
2242     {
2243       rl_delete_text (start, rl_point);
2244       rl_point = start;
2245     }
2246   return 0;
2247 }
2248
2249 \f
2250 /* **************************************************************** */
2251 /*                                                                  */
2252 /*                      Kill commands                               */
2253 /*                                                                  */
2254 /* **************************************************************** */
2255
2256 /* The next two functions mimic unix line editing behaviour, except they
2257    save the deleted text on the kill ring.  This is safer than not saving
2258    it, and since we have a ring, nobody should get screwed. */
2259
2260 /* This does what C-w does in Unix.  We can't prevent people from
2261    using behaviour that they expect. */
2262 rl_unix_word_rubout (count, key)
2263      int count, key;
2264 {
2265   if (!rl_point)
2266     ding ();
2267   else
2268     {
2269       int orig_point = rl_point;
2270       if (count <= 0)
2271         count = 1;
2272
2273       while (count--)
2274         {
2275           while (rl_point && whitespace (the_line[rl_point - 1]))
2276             rl_point--;
2277
2278           while (rl_point && !whitespace (the_line[rl_point - 1]))
2279             rl_point--;
2280         }
2281
2282       rl_kill_text (orig_point, rl_point);
2283     }
2284   return 0;
2285 }
2286
2287 /* Here is C-u doing what Unix does.  You don't *have* to use these
2288    key-bindings.  We have a choice of killing the entire line, or
2289    killing from where we are to the start of the line.  We choose the
2290    latter, because if you are a Unix weenie, then you haven't backspaced
2291    into the line at all, and if you aren't, then you know what you are
2292    doing. */
2293 rl_unix_line_discard (count, key)
2294      int count, key;
2295 {
2296   if (!rl_point)
2297     ding ();
2298   else
2299     {
2300       rl_kill_text (rl_point, 0);
2301       rl_point = 0;
2302     }
2303   return 0;
2304 }
2305
2306 \f
2307 /* **************************************************************** */
2308 /*                                                                  */
2309 /*                      Commands For Typos                          */
2310 /*                                                                  */
2311 /* **************************************************************** */
2312
2313 /* Random and interesting things in here.  */
2314
2315 /* **************************************************************** */
2316 /*                                                                  */
2317 /*                      Changing Case                               */
2318 /*                                                                  */
2319 /* **************************************************************** */
2320
2321 /* The three kinds of things that we know how to do. */
2322 #define UpCase 1
2323 #define DownCase 2
2324 #define CapCase 3
2325
2326 static int rl_change_case ();
2327
2328 /* Uppercase the word at point. */
2329 rl_upcase_word (count, key)
2330      int count, key;
2331 {
2332   return (rl_change_case (count, UpCase));
2333 }
2334
2335 /* Lowercase the word at point. */
2336 rl_downcase_word (count, key)
2337      int count, key;
2338 {
2339   return (rl_change_case (count, DownCase));
2340 }
2341
2342 /* Upcase the first letter, downcase the rest. */
2343 rl_capitalize_word (count, key)
2344      int count, key;
2345 {
2346  return (rl_change_case (count, CapCase));
2347 }
2348
2349 /* The meaty function.
2350    Change the case of COUNT words, performing OP on them.
2351    OP is one of UpCase, DownCase, or CapCase.
2352    If a negative argument is given, leave point where it started,
2353    otherwise, leave it where it moves to. */
2354 static int
2355 rl_change_case (count, op)
2356      int count, op;
2357 {
2358   register int start = rl_point, end;
2359   int state = 0;
2360
2361   rl_forward_word (count);
2362   end = rl_point;
2363
2364   if (count < 0)
2365     {
2366       int temp = start;
2367       start = end;
2368       end = temp;
2369     }
2370
2371   /* We are going to modify some text, so let's prepare to undo it. */
2372   rl_modifying (start, end);
2373
2374   for (; start < end; start++)
2375     {
2376       switch (op)
2377         {
2378         case UpCase:
2379           the_line[start] = to_upper (the_line[start]);
2380           break;
2381
2382         case DownCase:
2383           the_line[start] = to_lower (the_line[start]);
2384           break;
2385
2386         case CapCase:
2387           if (state == 0)
2388             {
2389               the_line[start] = to_upper (the_line[start]);
2390               state = 1;
2391             }
2392           else
2393             {
2394               the_line[start] = to_lower (the_line[start]);
2395             }
2396           if (!pure_alphabetic (the_line[start]))
2397             state = 0;
2398           break;
2399
2400         default:
2401           ding ();
2402           return -1;
2403         }
2404     }
2405   rl_point = end;
2406   return 0;
2407 }
2408
2409 /* **************************************************************** */
2410 /*                                                                  */
2411 /*                      Transposition                               */
2412 /*                                                                  */
2413 /* **************************************************************** */
2414
2415 /* Transpose the words at point. */
2416 rl_transpose_words (count, key)
2417      int count, key;
2418 {
2419   char *word1, *word2;
2420   int w1_beg, w1_end, w2_beg, w2_end;
2421   int orig_point = rl_point;
2422
2423   if (!count)
2424     return 0;
2425
2426   /* Find the two words. */
2427   rl_forward_word (count);
2428   w2_end = rl_point;
2429   rl_backward_word (1);
2430   w2_beg = rl_point;
2431   rl_backward_word (count);
2432   w1_beg = rl_point;
2433   rl_forward_word (1);
2434   w1_end = rl_point;
2435
2436   /* Do some check to make sure that there really are two words. */
2437   if ((w1_beg == w2_beg) || (w2_beg < w1_end))
2438     {
2439       ding ();
2440       rl_point = orig_point;
2441       return -1;
2442     }
2443
2444   /* Get the text of the words. */
2445   word1 = rl_copy_text (w1_beg, w1_end);
2446   word2 = rl_copy_text (w2_beg, w2_end);
2447
2448   /* We are about to do many insertions and deletions.  Remember them
2449      as one operation. */
2450   rl_begin_undo_group ();
2451
2452   /* Do the stuff at word2 first, so that we don't have to worry
2453      about word1 moving. */
2454   rl_point = w2_beg;
2455   rl_delete_text (w2_beg, w2_end);
2456   rl_insert_text (word1);
2457
2458   rl_point = w1_beg;
2459   rl_delete_text (w1_beg, w1_end);
2460   rl_insert_text (word2);
2461
2462   /* This is exactly correct since the text before this point has not
2463      changed in length. */
2464   rl_point = w2_end;
2465
2466   /* I think that does it. */
2467   rl_end_undo_group ();
2468   free (word1);
2469   free (word2);
2470
2471   return 0;
2472 }
2473
2474 /* Transpose the characters at point.  If point is at the end of the line,
2475    then transpose the characters before point. */
2476 rl_transpose_chars (count, key)
2477      int count, key;
2478 {
2479   char dummy[2];
2480
2481   if (!count)
2482     return 0;
2483
2484   if (!rl_point || rl_end < 2)
2485     {
2486       ding ();
2487       return -1;
2488     }
2489
2490   rl_begin_undo_group ();
2491
2492   if (rl_point == rl_end)
2493     {
2494       --rl_point;
2495       count = 1;
2496     }
2497   rl_point--;
2498
2499   dummy[0] = the_line[rl_point];
2500   dummy[1] = '\0';
2501
2502   rl_delete_text (rl_point, rl_point + 1);
2503
2504   rl_point += count;
2505   if (rl_point > rl_end)
2506     rl_point = rl_end;
2507   else if (rl_point < 0)
2508     rl_point = 0;
2509   rl_insert_text (dummy);
2510
2511   rl_end_undo_group ();
2512   return 0;
2513 }
2514 \f
2515 /* **************************************************************** */
2516 /*                                                                  */
2517 /*                      Undo, and Undoing                           */
2518 /*                                                                  */
2519 /* **************************************************************** */
2520
2521 /* The current undo list for THE_LINE. */
2522 UNDO_LIST *rl_undo_list = (UNDO_LIST *)NULL;
2523
2524 /* Remember how to undo something.  Concatenate some undos if that
2525    seems right. */
2526 void
2527 rl_add_undo (what, start, end, text)
2528      enum undo_code what;
2529      int start, end;
2530      char *text;
2531 {
2532   UNDO_LIST *temp = (UNDO_LIST *)xmalloc (sizeof (UNDO_LIST));
2533   temp->what = what;
2534   temp->start = start;
2535   temp->end = end;
2536   temp->text = text;
2537   temp->next = rl_undo_list;
2538   rl_undo_list = temp;
2539 }
2540
2541 /* Free the existing undo list. */
2542 void
2543 free_undo_list ()
2544 {
2545   while (rl_undo_list)
2546     {
2547       UNDO_LIST *release = rl_undo_list;
2548       rl_undo_list = rl_undo_list->next;
2549
2550       if (release->what == UNDO_DELETE)
2551         free (release->text);
2552
2553       free (release);
2554     }
2555   rl_undo_list = (UNDO_LIST *)NULL;
2556 }
2557
2558 /* Undo the next thing in the list.  Return 0 if there
2559    is nothing to undo, or non-zero if there was. */
2560 int
2561 rl_do_undo ()
2562 {
2563   UNDO_LIST *release;
2564   int waiting_for_begin = 0;
2565
2566 undo_thing:
2567   if (!rl_undo_list)
2568     return (0);
2569
2570   doing_an_undo = 1;
2571
2572   switch (rl_undo_list->what) {
2573
2574     /* Undoing deletes means inserting some text. */
2575   case UNDO_DELETE:
2576     rl_point = rl_undo_list->start;
2577     rl_insert_text (rl_undo_list->text);
2578     free (rl_undo_list->text);
2579     break;
2580
2581     /* Undoing inserts means deleting some text. */
2582   case UNDO_INSERT:
2583     rl_delete_text (rl_undo_list->start, rl_undo_list->end);
2584     rl_point = rl_undo_list->start;
2585     break;
2586
2587     /* Undoing an END means undoing everything 'til we get to
2588        a BEGIN. */
2589   case UNDO_END:
2590     waiting_for_begin++;
2591     break;
2592
2593     /* Undoing a BEGIN means that we are done with this group. */
2594   case UNDO_BEGIN:
2595     if (waiting_for_begin)
2596       waiting_for_begin--;
2597     else
2598       ding ();
2599     break;
2600   }
2601
2602   doing_an_undo = 0;
2603
2604   release = rl_undo_list;
2605   rl_undo_list = rl_undo_list->next;
2606   free (release);
2607
2608   if (waiting_for_begin)
2609     goto undo_thing;
2610
2611   return (1);
2612 }
2613
2614 /* Begin a group.  Subsequent undos are undone as an atomic operation. */
2615 int
2616 rl_begin_undo_group ()
2617 {
2618   rl_add_undo (UNDO_BEGIN, 0, 0, 0);
2619   return 0;
2620 }
2621
2622 /* End an undo group started with rl_begin_undo_group (). */
2623 int
2624 rl_end_undo_group ()
2625 {
2626   rl_add_undo (UNDO_END, 0, 0, 0);
2627   return 0;
2628 }
2629
2630 /* Save an undo entry for the text from START to END. */
2631 rl_modifying (start, end)
2632      int start, end;
2633 {
2634   if (start > end)
2635     {
2636       int t = start;
2637       start = end;
2638       end = t;
2639     }
2640
2641   if (start != end)
2642     {
2643       char *temp = rl_copy_text (start, end);
2644       rl_begin_undo_group ();
2645       rl_add_undo (UNDO_DELETE, start, end, temp);
2646       rl_add_undo (UNDO_INSERT, start, end, (char *)NULL);
2647       rl_end_undo_group ();
2648     }
2649   return 0;
2650 }
2651
2652 /* Revert the current line to its previous state. */
2653 int
2654 rl_revert_line (count, key)
2655      int count, key;
2656 {
2657   if (!rl_undo_list)
2658     ding ();
2659   else
2660     {
2661       while (rl_undo_list)
2662         rl_do_undo ();
2663     }
2664   return 0;
2665 }
2666
2667 /* Do some undoing of things that were done. */
2668 int
2669 rl_undo_command (count, key)
2670      int count, key;
2671 {
2672   if (count < 0)
2673     return 0;   /* Nothing to do. */
2674
2675   while (count)
2676     {
2677       if (rl_do_undo ())
2678         count--;
2679       else
2680         {
2681           ding ();
2682           break;
2683         }
2684     }
2685   return 0;
2686 }
2687 \f
2688 /* **************************************************************** */
2689 /*                                                                  */
2690 /*                      History Utilities                           */
2691 /*                                                                  */
2692 /* **************************************************************** */
2693
2694 /* We already have a history library, and that is what we use to control
2695    the history features of readline.  However, this is our local interface
2696    to the history mechanism. */
2697
2698 /* While we are editing the history, this is the saved
2699    version of the original line. */
2700 HIST_ENTRY *saved_line_for_history = (HIST_ENTRY *)NULL;
2701
2702 /* Set the history pointer back to the last entry in the history. */
2703 static void
2704 start_using_history ()
2705 {
2706   using_history ();
2707   if (saved_line_for_history)
2708     _rl_free_history_entry (saved_line_for_history);
2709
2710   saved_line_for_history = (HIST_ENTRY *)NULL;
2711 }
2712
2713 /* Free the contents (and containing structure) of a HIST_ENTRY. */
2714 void
2715 _rl_free_history_entry (entry)
2716      HIST_ENTRY *entry;
2717 {
2718   if (!entry)
2719     return;
2720   if (entry->line)
2721     free (entry->line);
2722   free (entry);
2723 }
2724
2725 /* Perhaps put back the current line if it has changed. */
2726 maybe_replace_line ()
2727 {
2728   HIST_ENTRY *temp = current_history ();
2729
2730   /* If the current line has changed, save the changes. */
2731   if (temp && ((UNDO_LIST *)(temp->data) != rl_undo_list))
2732     {
2733       temp = replace_history_entry (where_history (), the_line, rl_undo_list);
2734       free (temp->line);
2735       free (temp);
2736     }
2737   return 0;
2738 }
2739
2740 /* Put back the saved_line_for_history if there is one. */
2741 maybe_unsave_line ()
2742 {
2743   if (saved_line_for_history)
2744     {
2745       int line_len;
2746
2747       line_len = strlen (saved_line_for_history->line);
2748
2749       if (line_len >= rl_line_buffer_len)
2750         rl_extend_line_buffer (line_len);
2751
2752       strcpy (the_line, saved_line_for_history->line);
2753       rl_undo_list = (UNDO_LIST *)saved_line_for_history->data;
2754       _rl_free_history_entry (saved_line_for_history);
2755       saved_line_for_history = (HIST_ENTRY *)NULL;
2756       rl_end = rl_point = strlen (the_line);
2757     }
2758   else
2759     ding ();
2760   return 0;
2761 }
2762
2763 /* Save the current line in saved_line_for_history. */
2764 maybe_save_line ()
2765 {
2766   if (!saved_line_for_history)
2767     {
2768       saved_line_for_history = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
2769       saved_line_for_history->line = savestring (the_line);
2770       saved_line_for_history->data = (char *)rl_undo_list;
2771     }
2772   return 0;
2773 }
2774 \f
2775 /* **************************************************************** */
2776 /*                                                                  */
2777 /*                      History Commands                            */
2778 /*                                                                  */
2779 /* **************************************************************** */
2780
2781 /* Meta-< goes to the start of the history. */
2782 rl_beginning_of_history (count, key)
2783      int count, key;
2784 {
2785   return (rl_get_previous_history (1 + where_history ()));
2786 }
2787
2788 /* Meta-> goes to the end of the history.  (The current line). */
2789 rl_end_of_history (count, key)
2790      int count, key;
2791 {
2792   maybe_replace_line ();
2793   using_history ();
2794   maybe_unsave_line ();
2795   return 0;
2796 }
2797
2798 /* Move down to the next history line. */
2799 rl_get_next_history (count, key)
2800      int count, key;
2801 {
2802   HIST_ENTRY *temp = (HIST_ENTRY *)NULL;
2803
2804   if (count < 0)
2805     return (rl_get_previous_history (-count));
2806
2807   if (!count)
2808     return 0;
2809
2810   maybe_replace_line ();
2811
2812   while (count)
2813     {
2814       temp = next_history ();
2815       if (!temp)
2816         break;
2817       --count;
2818     }
2819
2820   if (!temp)
2821     maybe_unsave_line ();
2822   else
2823     {
2824       int line_len;
2825
2826       line_len = strlen (temp->line);
2827
2828       if (line_len >= rl_line_buffer_len)
2829         rl_extend_line_buffer (line_len);
2830
2831       strcpy (the_line, temp->line);
2832       rl_undo_list = (UNDO_LIST *)temp->data;
2833       rl_end = rl_point = strlen (the_line);
2834 #if defined (VI_MODE)
2835       if (rl_editing_mode == vi_mode)
2836         rl_point = 0;
2837 #endif /* VI_MODE */
2838     }
2839   return 0;
2840 }
2841
2842 /* Get the previous item out of our interactive history, making it the current
2843    line.  If there is no previous history, just ding. */
2844 rl_get_previous_history (count, key)
2845      int count, key;
2846 {
2847   HIST_ENTRY *old_temp = (HIST_ENTRY *)NULL;
2848   HIST_ENTRY *temp = (HIST_ENTRY *)NULL;
2849
2850   if (count < 0)
2851     return (rl_get_next_history (-count));
2852
2853   if (!count)
2854     return 0;
2855
2856   /* If we don't have a line saved, then save this one. */
2857   maybe_save_line ();
2858
2859   /* If the current line has changed, save the changes. */
2860   maybe_replace_line ();
2861
2862   while (count)
2863     {
2864       temp = previous_history ();
2865       if (!temp)
2866         break;
2867       else
2868         old_temp = temp;
2869       --count;
2870     }
2871
2872   /* If there was a large argument, and we moved back to the start of the
2873      history, that is not an error.  So use the last value found. */
2874   if (!temp && old_temp)
2875     temp = old_temp;
2876
2877   if (!temp)
2878     ding ();
2879   else
2880     {
2881       int line_len;
2882
2883       line_len = strlen (temp->line);
2884
2885       if (line_len >= rl_line_buffer_len)
2886         rl_extend_line_buffer (line_len);
2887
2888       strcpy (the_line, temp->line);
2889       rl_undo_list = (UNDO_LIST *)temp->data;
2890       rl_end = rl_point = line_len;
2891
2892 #if defined (VI_MODE)
2893       if (rl_editing_mode == vi_mode)
2894         rl_point = 0;
2895 #endif /* VI_MODE */
2896     }
2897   return 0;
2898 }
2899
2900 /* Make C be the next command to be executed. */
2901 rl_execute_next (c)
2902      int c;
2903 {
2904   rl_pending_input = c;
2905   return 0;
2906 }
2907
2908 /* **************************************************************** */
2909 /*                                                                  */
2910 /*                 The Mark and the Region.                         */
2911 /*                                                                  */
2912 /* **************************************************************** */
2913
2914 /* Set the mark at POSITION. */
2915 rl_set_mark (position)
2916      int position;
2917 {
2918   if (position > rl_end)
2919     return -1;
2920
2921   rl_mark = position;
2922   return 0;
2923 }
2924
2925 /* Exchange the position of mark and point. */
2926 rl_exchange_mark_and_point (count, key)
2927      int count, key;
2928 {
2929   if (rl_mark > rl_end)
2930     rl_mark = -1;
2931
2932   if (rl_mark == -1)
2933     {
2934       ding ();
2935       return -1;
2936     }
2937   else
2938     {
2939       int temp = rl_point;
2940
2941       rl_point = rl_mark;
2942       rl_mark = temp;
2943     }
2944   return 0;
2945 }
2946
2947 \f
2948 /* **************************************************************** */
2949 /*                                                                  */
2950 /*                      Killing Mechanism                           */
2951 /*                                                                  */
2952 /* **************************************************************** */
2953
2954 /* What we assume for a max number of kills. */
2955 #define DEFAULT_MAX_KILLS 10
2956
2957 /* The real variable to look at to find out when to flush kills. */
2958 int rl_max_kills =  DEFAULT_MAX_KILLS;
2959
2960 /* Where to store killed text. */
2961 char **rl_kill_ring = (char **)NULL;
2962
2963 /* Where we are in the kill ring. */
2964 int rl_kill_index = 0;
2965
2966 /* How many slots we have in the kill ring. */
2967 int rl_kill_ring_length = 0;
2968
2969 /* How to say that you only want to save a certain amount
2970    of kill material. */
2971 rl_set_retained_kills (num)
2972      int num;
2973 {
2974   return 0;
2975 }
2976
2977 /* The way to kill something.  This appends or prepends to the last
2978    kill, if the last command was a kill command.  if FROM is less
2979    than TO, then the text is appended, otherwise prepended.  If the
2980    last command was not a kill command, then a new slot is made for
2981    this kill. */
2982 rl_kill_text (from, to)
2983      int from, to;
2984 {
2985   int slot;
2986   char *text;
2987
2988   /* Is there anything to kill? */
2989   if (from == to)
2990     {
2991       last_command_was_kill++;
2992       return 0;
2993     }
2994
2995   text = rl_copy_text (from, to);
2996
2997   /* Delete the copied text from the line. */
2998   rl_delete_text (from, to);
2999
3000   /* First, find the slot to work with. */
3001   if (!last_command_was_kill)
3002     {
3003       /* Get a new slot.  */
3004       if (!rl_kill_ring)
3005         {
3006           /* If we don't have any defined, then make one. */
3007           rl_kill_ring = (char **)
3008             xmalloc (((rl_kill_ring_length = 1) + 1) * sizeof (char *));
3009           rl_kill_ring[slot = 0] = (char *)NULL;
3010         }
3011       else
3012         {
3013           /* We have to add a new slot on the end, unless we have
3014              exceeded the max limit for remembering kills. */
3015           slot = rl_kill_ring_length;
3016           if (slot == rl_max_kills)
3017             {
3018               register int i;
3019               free (rl_kill_ring[0]);
3020               for (i = 0; i < slot; i++)
3021                 rl_kill_ring[i] = rl_kill_ring[i + 1];
3022             }
3023           else
3024             {
3025               slot = rl_kill_ring_length += 1;
3026               rl_kill_ring = (char **)xrealloc (rl_kill_ring, slot * sizeof (char *));
3027             }
3028           rl_kill_ring[--slot] = (char *)NULL;
3029         }
3030     }
3031   else
3032     slot = rl_kill_ring_length - 1;
3033
3034   /* If the last command was a kill, prepend or append. */
3035   if (last_command_was_kill && rl_editing_mode != vi_mode)
3036     {
3037       char *old = rl_kill_ring[slot];
3038       char *new = xmalloc (1 + strlen (old) + strlen (text));
3039
3040       if (from < to)
3041         {
3042           strcpy (new, old);
3043           strcat (new, text);
3044         }
3045       else
3046         {
3047           strcpy (new, text);
3048           strcat (new, old);
3049         }
3050       free (old);
3051       free (text);
3052       rl_kill_ring[slot] = new;
3053     }
3054   else
3055     {
3056       rl_kill_ring[slot] = text;
3057     }
3058   rl_kill_index = slot;
3059   last_command_was_kill++;
3060   return 0;
3061 }
3062
3063 /* Now REMEMBER!  In order to do prepending or appending correctly, kill
3064    commands always make rl_point's original position be the FROM argument,
3065    and rl_point's extent be the TO argument. */
3066
3067 /* **************************************************************** */
3068 /*                                                                  */
3069 /*                      Killing Commands                            */
3070 /*                                                                  */
3071 /* **************************************************************** */
3072
3073 /* Delete the word at point, saving the text in the kill ring. */
3074 rl_kill_word (count, key)
3075      int count, key;
3076 {
3077   int orig_point = rl_point;
3078
3079   if (count < 0)
3080     return (rl_backward_kill_word (-count));
3081   else
3082     {
3083       rl_forward_word (count);
3084
3085       if (rl_point != orig_point)
3086         rl_kill_text (orig_point, rl_point);
3087
3088       rl_point = orig_point;
3089     }
3090   return 0;
3091 }
3092
3093 /* Rubout the word before point, placing it on the kill ring. */
3094 rl_backward_kill_word (count, ignore)
3095      int count, ignore;
3096 {
3097   int orig_point = rl_point;
3098
3099   if (count < 0)
3100     return (rl_kill_word (-count));
3101   else
3102     {
3103       rl_backward_word (count);
3104
3105       if (rl_point != orig_point)
3106         rl_kill_text (orig_point, rl_point);
3107     }
3108   return 0;
3109 }
3110
3111 /* Kill from here to the end of the line.  If DIRECTION is negative, kill
3112    back to the line start instead. */
3113 rl_kill_line (direction, ignore)
3114      int direction, ignore;
3115 {
3116   int orig_point = rl_point;
3117
3118   if (direction < 0)
3119     return (rl_backward_kill_line (1));
3120   else
3121     {
3122       rl_end_of_line (1, ignore);
3123       if (orig_point != rl_point)
3124         rl_kill_text (orig_point, rl_point);
3125       rl_point = orig_point;
3126     }
3127   return 0;
3128 }
3129
3130 /* Kill backwards to the start of the line.  If DIRECTION is negative, kill
3131    forwards to the line end instead. */
3132 rl_backward_kill_line (direction, ignore)
3133      int direction, ignore;
3134 {
3135   int orig_point = rl_point;
3136
3137   if (direction < 0)
3138     return (rl_kill_line (1));
3139   else
3140     {
3141       if (!rl_point)
3142         ding ();
3143       else
3144         {
3145           rl_beg_of_line (1, ignore);
3146           rl_kill_text (orig_point, rl_point);
3147         }
3148     }
3149   return 0;
3150 }
3151
3152 /* Kill the whole line, no matter where point is. */
3153 rl_kill_full_line (count, ignore)
3154      int count, ignore;
3155 {
3156   rl_begin_undo_group ();
3157   rl_point = 0;
3158   rl_kill_text (rl_point, rl_end);
3159   rl_end_undo_group ();
3160   return 0;
3161 }
3162
3163 /* Yank back the last killed text.  This ignores arguments. */
3164 rl_yank (count, ignore)
3165      int count, ignore;
3166 {
3167   if (!rl_kill_ring)
3168     {
3169       rl_abort (count, ignore);
3170       return -1;
3171     }
3172
3173   rl_set_mark (rl_point);
3174   rl_insert_text (rl_kill_ring[rl_kill_index]);
3175   return 0;
3176 }
3177
3178 /* If the last command was yank, or yank_pop, and the text just
3179    before point is identical to the current kill item, then
3180    delete that text from the line, rotate the index down, and
3181    yank back some other text. */
3182 rl_yank_pop (count, key)
3183      int count, key;
3184 {
3185   int l;
3186
3187   if (((rl_last_func != rl_yank_pop) && (rl_last_func != rl_yank)) ||
3188       !rl_kill_ring)
3189     {
3190       rl_abort (1, key);
3191       return -1;
3192     }
3193
3194   l = strlen (rl_kill_ring[rl_kill_index]);
3195   if (((rl_point - l) >= 0) &&
3196       (strncmp (the_line + (rl_point - l),
3197                 rl_kill_ring[rl_kill_index], l) == 0))
3198     {
3199       rl_delete_text ((rl_point - l), rl_point);
3200       rl_point -= l;
3201       rl_kill_index--;
3202       if (rl_kill_index < 0)
3203         rl_kill_index = rl_kill_ring_length - 1;
3204       rl_yank (1, 0);
3205       return 0;
3206     }
3207   else
3208     {
3209       rl_abort (1, key);
3210       return -1;
3211     }
3212 }
3213
3214 /* Yank the COUNTth argument from the previous history line. */
3215 rl_yank_nth_arg (count, ignore)
3216      int count, ignore;
3217 {
3218   register HIST_ENTRY *entry = previous_history ();
3219   char *arg;
3220
3221   if (entry)
3222     next_history ();
3223   else
3224     {
3225       ding ();
3226       return -1;
3227     }
3228
3229   arg = history_arg_extract (count, count, entry->line);
3230   if (!arg || !*arg)
3231     {
3232       ding ();
3233       return -1;
3234     }
3235
3236   rl_begin_undo_group ();
3237
3238 #if defined (VI_MODE)
3239   /* Vi mode always inserts a space before yanking the argument, and it
3240      inserts it right *after* rl_point. */
3241   if (rl_editing_mode == vi_mode)
3242     {
3243       rl_vi_append_mode ();
3244       rl_insert_text (" ");
3245     }
3246 #endif /* VI_MODE */
3247
3248   rl_insert_text (arg);
3249   free (arg);
3250
3251   rl_end_undo_group ();
3252   return 0;
3253 }
3254
3255 /* Yank the last argument from the previous history line.  This `knows'
3256    how rl_yank_nth_arg treats a count of `$'.  With an argument, this
3257    behaves the same as rl_yank_nth_arg. */
3258 int
3259 rl_yank_last_arg (count, key)
3260      int count, key;
3261 {
3262   if (rl_explicit_arg)
3263     return (rl_yank_nth_arg (count, key));
3264   else
3265     return (rl_yank_nth_arg ('$', key));
3266 }
3267
3268 /* How to toggle back and forth between editing modes. */
3269 rl_vi_editing_mode (count, key)
3270      int count, key;
3271 {
3272 #if defined (VI_MODE)
3273   rl_editing_mode = vi_mode;
3274   rl_vi_insertion_mode ();
3275   return 0;
3276 #endif /* VI_MODE */
3277 }
3278
3279 rl_emacs_editing_mode (count, key)
3280      int count, key;
3281 {
3282   rl_editing_mode = emacs_mode;
3283   _rl_keymap = emacs_standard_keymap;
3284   return 0;
3285 }
3286
3287 \f
3288 /* **************************************************************** */
3289 /*                                                                  */
3290 /*                      USG (System V) Support                      */
3291 /*                                                                  */
3292 /* **************************************************************** */
3293
3294 int
3295 rl_getc (stream)
3296      FILE *stream;
3297 {
3298   int result;
3299   unsigned char c;
3300
3301 #if defined (__GO32__)
3302   if (isatty (0))
3303     return (getkey () & 0x7F);
3304 #endif /* __GO32__ */
3305
3306   while (1)
3307     {
3308       result = read (fileno (stream), &c, sizeof (unsigned char));
3309
3310       if (result == sizeof (unsigned char))
3311         return (c);
3312
3313       /* If zero characters are returned, then the file that we are
3314          reading from is empty!  Return EOF in that case. */
3315       if (result == 0)
3316         return (EOF);
3317
3318 #if defined (EWOULDBLOCK)
3319       if (errno == EWOULDBLOCK)
3320         {
3321           int flags;
3322
3323           if ((flags = fcntl (fileno (stream), F_GETFL, 0)) < 0)
3324             return (EOF);
3325           if (flags & O_NDELAY)
3326             {
3327               flags &= ~O_NDELAY;
3328               fcntl (fileno (stream), F_SETFL, flags);
3329               continue;
3330             }
3331           continue;
3332         }
3333 #endif /* EWOULDBLOCK */
3334
3335 #if defined (_POSIX_VERSION) && defined (EAGAIN) && defined (O_NONBLOCK)
3336       if (errno == EAGAIN)
3337         {
3338           int flags;
3339
3340           if ((flags = fcntl (fileno (stream), F_GETFL, 0)) < 0)
3341             return (EOF);
3342           if (flags & O_NONBLOCK)
3343             {
3344               flags &= ~O_NONBLOCK;
3345               fcntl (fileno (stream), F_SETFL, flags);
3346               continue;
3347             }
3348         }
3349 #endif /* _POSIX_VERSION && EAGAIN && O_NONBLOCK */
3350
3351 #if !defined (__GO32__)
3352       /* If the error that we received was SIGINT, then try again,
3353          this is simply an interrupted system call to read ().
3354          Otherwise, some error ocurred, also signifying EOF. */
3355       if (errno != EINTR)
3356         return (EOF);
3357 #endif /* !__GO32__ */
3358     }
3359 }
3360
3361 #if !defined (SHELL)
3362 #ifdef savestring
3363 #undef savestring
3364 #endif
3365 /* Backwards compatibilty, now that savestring has been removed from
3366    all `public' readline header files. */
3367 char *
3368 savestring (s)
3369      char *s;
3370 {
3371   return ((char *)strcpy (xmalloc (1 + (int)strlen (s)), (s)));
3372 }
3373 #endif
3374
3375 /* Function equivalents for the macros defined in chartypes.h. */
3376 #undef uppercase_p
3377 int
3378 uppercase_p (c)
3379      int c;
3380 {
3381   return (isupper (c));
3382 }
3383
3384 #undef lowercase_p
3385 int
3386 lowercase_p (c)
3387      int c;
3388 {
3389   return (islower (c));
3390 }
3391
3392 #undef pure_alphabetic
3393 int
3394 pure_alphabetic (c)
3395      int c;
3396 {
3397   return (isupper (c) || islower (c));
3398 }
3399
3400 #undef digit_p
3401 int
3402 digit_p (c)
3403      int c;
3404 {
3405   return (isdigit (c));
3406 }
3407
3408 #undef to_lower
3409 int
3410 to_lower (c)
3411      int c;
3412 {
3413   return (isupper (c) ? tolower (c) : c);
3414 }
3415
3416 #undef to_upper
3417 int
3418 to_upper (c)
3419      int c;
3420 {
3421   return (islower (c) ? toupper (c) : c);
3422 }
3423
3424 #undef digit_value
3425 int
3426 digit_value (c)
3427      int c;
3428 {
3429   return (isdigit (c) ? c - '0' : c);
3430 }
3431
3432 #if defined (STATIC_MALLOC)
3433 \f
3434 /* **************************************************************** */
3435 /*                                                                  */
3436 /*                      xmalloc and xrealloc ()                     */
3437 /*                                                                  */
3438 /* **************************************************************** */
3439
3440 static void memory_error_and_abort ();
3441
3442 static char *
3443 xmalloc (bytes)
3444      int bytes;
3445 {
3446   char *temp = (char *)malloc (bytes);
3447
3448   if (!temp)
3449     memory_error_and_abort ();
3450   return (temp);
3451 }
3452
3453 static char *
3454 xrealloc (pointer, bytes)
3455      char *pointer;
3456      int bytes;
3457 {
3458   char *temp;
3459
3460   if (!pointer)
3461     temp = (char *)malloc (bytes);
3462   else
3463     temp = (char *)realloc (pointer, bytes);
3464
3465   if (!temp)
3466     memory_error_and_abort ();
3467
3468   return (temp);
3469 }
3470
3471 static void
3472 memory_error_and_abort ()
3473 {
3474   fprintf (stderr, "readline: Out of virtual memory!\n");
3475   abort ();
3476 }
3477 #endif /* STATIC_MALLOC */
3478
3479 \f
3480 /* **************************************************************** */
3481 /*                                                                  */
3482 /*                      Testing Readline                            */
3483 /*                                                                  */
3484 /* **************************************************************** */
3485
3486 #if defined (TEST)
3487
3488 main ()
3489 {
3490   HIST_ENTRY **history_list ();
3491   char *temp = (char *)NULL;
3492   char *prompt = "readline% ";
3493   int done = 0;
3494
3495   while (!done)
3496     {
3497       temp = readline (prompt);
3498
3499       /* Test for EOF. */
3500       if (!temp)
3501         exit (1);
3502
3503       /* If there is anything on the line, print it and remember it. */
3504       if (*temp)
3505         {
3506           fprintf (stderr, "%s\r\n", temp);
3507           add_history (temp);
3508         }
3509
3510       /* Check for `command' that we handle. */
3511       if (strcmp (temp, "quit") == 0)
3512         done = 1;
3513
3514       if (strcmp (temp, "list") == 0)
3515         {
3516           HIST_ENTRY **list = history_list ();
3517           register int i;
3518           if (list)
3519             {
3520               for (i = 0; list[i]; i++)
3521                 {
3522                   fprintf (stderr, "%d: %s\r\n", i, list[i]->line);
3523                   free (list[i]->line);
3524                 }
3525               free (list);
3526             }
3527         }
3528       free (temp);
3529     }
3530 }
3531
3532 #endif /* TEST */
3533
3534 \f
3535 /*
3536  * Local variables:
3537  * compile-command: "gcc -g -traditional -I. -I.. -DTEST -o readline readline.c keymaps.o funmap.o history.o -ltermcap"
3538  * end:
3539  */