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