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 | |
44af5659 PB |
1067 | /* True if we have funny auto-line-wrap ("am" and "xn"). */ |
1068 | int term_xn; | |
1069 | ||
5e98bbab PB |
1070 | /* A visible bell, if the terminal can be made to flash the screen. */ |
1071 | char *visible_bell; | |
bd5635a1 | 1072 | |
5e98bbab PB |
1073 | /* Non-zero means that this terminal has a meta key. */ |
1074 | int term_has_meta; | |
bd5635a1 | 1075 | |
5e98bbab PB |
1076 | /* The string to write to turn on the meta key, if this term has one. */ |
1077 | char *term_mm; | |
bd5635a1 | 1078 | |
5e98bbab PB |
1079 | /* The string to write to turn off the meta key, if this term has one. */ |
1080 | char *term_mo; | |
bd5635a1 | 1081 | |
5e98bbab PB |
1082 | /* The key sequences output by the arrow keys, if this terminal has any. */ |
1083 | char *term_ku, *term_kd, *term_kr, *term_kl; | |
bd5635a1 | 1084 | |
5e98bbab PB |
1085 | /* Re-initialize the terminal considering that the TERM/TERMCAP variable |
1086 | has changed. */ | |
1087 | rl_reset_terminal (terminal_name) | |
1088 | char *terminal_name; | |
bd5635a1 | 1089 | { |
5e98bbab PB |
1090 | init_terminal_io (terminal_name); |
1091 | } | |
bd5635a1 | 1092 | |
5e98bbab PB |
1093 | /* Set readline's idea of the screen size. TTY is a file descriptor open |
1094 | to the terminal. If IGNORE_ENV is true, we do not pay attention to the | |
1095 | values of $LINES and $COLUMNS. The tests for TERM_STRING_BUFFER being | |
1096 | non-null serve to check whether or not we have initialized termcap. */ | |
1097 | void | |
1098 | _rl_set_screen_size (tty, ignore_env) | |
1099 | int tty, ignore_env; | |
1100 | { | |
1101 | #if defined (TIOCGWINSZ) | |
1102 | struct winsize window_size; | |
1103 | #endif /* TIOCGWINSZ */ | |
bd5635a1 | 1104 | |
5e98bbab PB |
1105 | #if defined (TIOCGWINSZ) |
1106 | if (ioctl (tty, TIOCGWINSZ, &window_size) == 0) | |
bd5635a1 | 1107 | { |
5e98bbab PB |
1108 | screenwidth = (int) window_size.ws_col; |
1109 | screenheight = (int) window_size.ws_row; | |
bd5635a1 | 1110 | } |
5e98bbab | 1111 | #endif /* TIOCGWINSZ */ |
bd5635a1 | 1112 | |
5e98bbab PB |
1113 | /* Environment variable COLUMNS overrides setting of "co" if IGNORE_ENV |
1114 | is unset. */ | |
1115 | if (screenwidth <= 0) | |
bd5635a1 | 1116 | { |
5e98bbab | 1117 | char *sw; |
bd5635a1 | 1118 | |
5e98bbab PB |
1119 | if (!ignore_env && (sw = getenv ("COLUMNS"))) |
1120 | screenwidth = atoi (sw); | |
bd5635a1 | 1121 | |
5e98bbab PB |
1122 | if (screenwidth <= 0 && term_string_buffer) |
1123 | screenwidth = tgetnum ("co"); | |
870ca253 SG |
1124 | } |
1125 | ||
5e98bbab PB |
1126 | /* Environment variable LINES overrides setting of "li" if IGNORE_ENV |
1127 | is unset. */ | |
1128 | if (screenheight <= 0) | |
bd5635a1 | 1129 | { |
5e98bbab | 1130 | char *sh; |
bd5635a1 | 1131 | |
5e98bbab PB |
1132 | if (!ignore_env && (sh = getenv ("LINES"))) |
1133 | screenheight = atoi (sh); | |
bd5635a1 | 1134 | |
5e98bbab PB |
1135 | if (screenheight <= 0 && term_string_buffer) |
1136 | screenheight = tgetnum ("li"); | |
bd5635a1 | 1137 | } |
bd5635a1 | 1138 | |
5e98bbab PB |
1139 | /* If all else fails, default to 80x24 terminal. */ |
1140 | if (screenwidth <= 0) | |
1141 | screenwidth = 80; | |
bd5635a1 | 1142 | |
5e98bbab PB |
1143 | if (screenheight <= 0) |
1144 | screenheight = 24; | |
bd5635a1 | 1145 | |
5e98bbab PB |
1146 | #if defined (SHELL) |
1147 | /* If we're being compiled as part of bash, set the environment | |
1148 | variables $LINES and $COLUMNS to new values. */ | |
1149 | set_lines_and_columns (screenheight, screenwidth); | |
bd5635a1 | 1150 | #endif |
bd5635a1 | 1151 | |
44af5659 PB |
1152 | /* If we don't have xn (most modern terminals do), |
1153 | don't use the last column. */ | |
1154 | if (!term_xn) | |
1155 | screenwidth--; | |
bd5635a1 RP |
1156 | } |
1157 | ||
1158 | init_terminal_io (terminal_name) | |
1159 | char *terminal_name; | |
1160 | { | |
1267a5c0 | 1161 | #ifdef __GO32__ |
5e98bbab PB |
1162 | screenwidth = ScreenCols (); |
1163 | screenheight = ScreenRows (); | |
1267a5c0 SC |
1164 | term_cr = "\r"; |
1165 | term_im = term_ei = term_ic = term_IC = (char *)NULL; | |
1166 | term_up = term_dc = term_DC = visible_bell = (char *)NULL; | |
5e98bbab PB |
1167 | |
1168 | /* Does the _GO32_ have a meta key? I don't know. */ | |
1169 | term_has_meta = 0; | |
1170 | term_mm = term_mo = (char *)NULL; | |
1171 | ||
1172 | /* It probably has arrow keys, but I don't know what they are. */ | |
1173 | term_ku = term_kd = term_kr = term_kl = (char *)NULL; | |
1174 | ||
1267a5c0 | 1175 | #if defined (HACK_TERMCAP_MOTION) |
5e98bbab | 1176 | term_forward_char = (char *)NULL; |
1267a5c0 | 1177 | #endif |
44af5659 | 1178 | terminal_can_insert = term_xn = 0; |
1267a5c0 | 1179 | return; |
5e98bbab | 1180 | #else /* !__GO32__ */ |
870ca253 | 1181 | char *term, *buffer; |
870ca253 | 1182 | int tty; |
bd5635a1 | 1183 | |
870ca253 | 1184 | term = terminal_name ? terminal_name : getenv ("TERM"); |
bd5635a1 RP |
1185 | |
1186 | if (!term_string_buffer) | |
1187 | term_string_buffer = (char *)xmalloc (2048); | |
1188 | ||
1189 | if (!term_buffer) | |
1190 | term_buffer = (char *)xmalloc (2048); | |
1191 | ||
1192 | buffer = term_string_buffer; | |
1193 | ||
1194 | term_clrpag = term_cr = term_clreol = (char *)NULL; | |
1195 | ||
1196 | if (!term) | |
1197 | term = "dumb"; | |
1198 | ||
da60a50d | 1199 | if (tgetent (term_buffer, term) <= 0) |
bd5635a1 RP |
1200 | { |
1201 | dumb_term = 1; | |
870ca253 SG |
1202 | screenwidth = 79; |
1203 | screenheight = 24; | |
1204 | term_cr = "\r"; | |
1205 | term_im = term_ei = term_ic = term_IC = (char *)NULL; | |
1206 | term_up = term_dc = term_DC = visible_bell = (char *)NULL; | |
5e98bbab | 1207 | term_ku = term_kd = term_kl = term_kr = (char *)NULL; |
870ca253 SG |
1208 | #if defined (HACK_TERMCAP_MOTION) |
1209 | term_forward_char = (char *)NULL; | |
1210 | #endif | |
44af5659 | 1211 | terminal_can_insert = term_xn = 0; |
bd5635a1 RP |
1212 | return; |
1213 | } | |
1214 | ||
870ca253 SG |
1215 | BC = tgetstr ("pc", &buffer); |
1216 | PC = buffer ? *buffer : 0; | |
bd5635a1 RP |
1217 | |
1218 | term_backspace = tgetstr ("le", &buffer); | |
1219 | ||
1220 | term_cr = tgetstr ("cr", &buffer); | |
1221 | term_clreol = tgetstr ("ce", &buffer); | |
1222 | term_clrpag = tgetstr ("cl", &buffer); | |
1223 | ||
1224 | if (!term_cr) | |
1225 | term_cr = "\r"; | |
1226 | ||
870ca253 | 1227 | #if defined (HACK_TERMCAP_MOTION) |
bd5635a1 RP |
1228 | term_forward_char = tgetstr ("nd", &buffer); |
1229 | #endif /* HACK_TERMCAP_MOTION */ | |
1230 | ||
870ca253 SG |
1231 | if (rl_instream) |
1232 | tty = fileno (rl_instream); | |
1233 | else | |
1234 | tty = 0; | |
1235 | ||
1236 | screenwidth = screenheight = 0; | |
bd5635a1 | 1237 | |
44af5659 PB |
1238 | term_xn = tgetflag ("am", &buffer) && tgetflag ("xn", &buffer); |
1239 | ||
5e98bbab | 1240 | _rl_set_screen_size (tty, 0); |
bd5635a1 RP |
1241 | |
1242 | term_im = tgetstr ("im", &buffer); | |
1243 | term_ei = tgetstr ("ei", &buffer); | |
1244 | term_IC = tgetstr ("IC", &buffer); | |
1245 | term_ic = tgetstr ("ic", &buffer); | |
1246 | ||
1247 | /* "An application program can assume that the terminal can do | |
1248 | character insertion if *any one of* the capabilities `IC', | |
1249 | `im', `ic' or `ip' is provided." But we can't do anything if | |
1250 | only `ip' is provided, so... */ | |
1251 | terminal_can_insert = (term_IC || term_im || term_ic); | |
1252 | ||
1253 | term_up = tgetstr ("up", &buffer); | |
1254 | term_dc = tgetstr ("dc", &buffer); | |
1255 | term_DC = tgetstr ("DC", &buffer); | |
870ca253 SG |
1256 | |
1257 | visible_bell = tgetstr ("vb", &buffer); | |
5e98bbab PB |
1258 | |
1259 | /* Check to see if this terminal has a meta key. */ | |
1260 | term_has_meta = (tgetflag ("km") || tgetflag ("MT")); | |
1261 | if (term_has_meta) | |
1262 | { | |
1263 | term_mm = tgetstr ("mm", &buffer); | |
1264 | term_mo = tgetstr ("mo", &buffer); | |
1265 | } | |
1266 | else | |
1267 | { | |
1268 | term_mm = (char *)NULL; | |
1269 | term_mo = (char *)NULL; | |
1270 | } | |
1271 | ||
1272 | /* Attempt to find and bind the arrow keys. Do not override already | |
1273 | bound keys in an overzealous attempt, however. */ | |
1274 | term_ku = tgetstr ("ku", &buffer); | |
1275 | term_kd = tgetstr ("kd", &buffer); | |
1276 | term_kr = tgetstr ("kr", &buffer); | |
1277 | term_kl = tgetstr ("kl", &buffer); | |
1278 | ||
1279 | if (term_ku) | |
1280 | { | |
1281 | Function *func; | |
1282 | ||
1283 | func = rl_function_of_keyseq (term_ku, _rl_keymap, (int *)NULL); | |
1284 | ||
1285 | if (!func || func == rl_do_lowercase_version) | |
1286 | rl_set_key (term_ku, rl_get_previous_history, _rl_keymap); | |
1287 | } | |
1288 | ||
1289 | if (term_kd) | |
1290 | { | |
1291 | Function *func; | |
1292 | ||
1293 | func = rl_function_of_keyseq (term_kd, _rl_keymap, (int *)NULL); | |
1294 | ||
1295 | if (!func || func == rl_do_lowercase_version) | |
1296 | rl_set_key (term_kd, rl_get_next_history, _rl_keymap); | |
1297 | } | |
1298 | ||
1299 | if (term_kr) | |
1300 | { | |
1301 | Function *func; | |
1302 | ||
1303 | func = rl_function_of_keyseq (term_kr, _rl_keymap, (int *)NULL); | |
1304 | ||
1305 | if (!func || func == rl_do_lowercase_version) | |
1306 | rl_set_key (term_kr, rl_forward, _rl_keymap); | |
1307 | } | |
1308 | ||
1309 | if (term_kl) | |
1310 | { | |
1311 | Function *func; | |
1312 | ||
1313 | func = rl_function_of_keyseq (term_kl, _rl_keymap, (int *)NULL); | |
1314 | ||
1315 | if (!func || func == rl_do_lowercase_version) | |
1316 | rl_set_key (term_kl, rl_backward, _rl_keymap); | |
1317 | } | |
1267a5c0 | 1318 | #endif /* !__GO32__ */ |
bd5635a1 RP |
1319 | } |
1320 | ||
1321 | /* A function for the use of tputs () */ | |
5e98bbab PB |
1322 | void |
1323 | _rl_output_character_function (c) | |
bd5635a1 RP |
1324 | int c; |
1325 | { | |
1326 | putc (c, out_stream); | |
1327 | } | |
1328 | ||
1329 | /* Write COUNT characters from STRING to the output stream. */ | |
5e98bbab PB |
1330 | void |
1331 | _rl_output_some_chars (string, count) | |
bd5635a1 RP |
1332 | char *string; |
1333 | int count; | |
1334 | { | |
1335 | fwrite (string, 1, count, out_stream); | |
1336 | } | |
1337 | ||
5e98bbab PB |
1338 | |
1339 | ||
1340 | /* Move the cursor back. */ | |
1341 | backspace (count) | |
bd5635a1 RP |
1342 | int count; |
1343 | { | |
5e98bbab | 1344 | register int i; |
bd5635a1 | 1345 | |
1267a5c0 | 1346 | #ifndef __GO32__ |
bd5635a1 RP |
1347 | if (term_backspace) |
1348 | for (i = 0; i < count; i++) | |
5e98bbab | 1349 | tputs (term_backspace, 1, _rl_output_character_function); |
bd5635a1 | 1350 | else |
1267a5c0 | 1351 | #endif /* !__GO32__ */ |
bd5635a1 RP |
1352 | for (i = 0; i < count; i++) |
1353 | putc ('\b', out_stream); | |
1354 | } | |
1355 | ||
1356 | /* Move to the start of the next line. */ | |
1357 | crlf () | |
1358 | { | |
870ca253 | 1359 | #if defined (NEW_TTY_DRIVER) |
5e98bbab | 1360 | tputs (term_cr, 1, _rl_output_character_function); |
870ca253 | 1361 | #endif /* NEW_TTY_DRIVER */ |
bd5635a1 RP |
1362 | putc ('\n', out_stream); |
1363 | } | |
1364 | ||
bd5635a1 RP |
1365 | \f |
1366 | /* **************************************************************** */ | |
1367 | /* */ | |
1368 | /* Utility Functions */ | |
1369 | /* */ | |
1370 | /* **************************************************************** */ | |
1371 | ||
1372 | /* Return 0 if C is not a member of the class of characters that belong | |
1373 | in words, or 1 if it is. */ | |
1374 | ||
1375 | int allow_pathname_alphabetic_chars = 0; | |
1376 | char *pathname_alphabetic_chars = "/-_=~.#$"; | |
870ca253 | 1377 | |
bd5635a1 RP |
1378 | int |
1379 | alphabetic (c) | |
1380 | int c; | |
1381 | { | |
bd5635a1 RP |
1382 | if (pure_alphabetic (c) || (numeric (c))) |
1383 | return (1); | |
1384 | ||
1385 | if (allow_pathname_alphabetic_chars) | |
5e98bbab | 1386 | return ((int) strchr (pathname_alphabetic_chars, c)); |
bd5635a1 RP |
1387 | else |
1388 | return (0); | |
1389 | } | |
1390 | ||
1391 | /* Return non-zero if C is a numeric character. */ | |
1392 | int | |
1393 | numeric (c) | |
1394 | int c; | |
1395 | { | |
1396 | return (c >= '0' && c <= '9'); | |
1397 | } | |
1398 | ||
1399 | /* Ring the terminal bell. */ | |
1400 | int | |
1401 | ding () | |
1402 | { | |
1403 | if (readline_echoing_p) | |
1404 | { | |
1267a5c0 | 1405 | #ifndef __GO32__ |
5e98bbab PB |
1406 | if (_rl_prefer_visible_bell && visible_bell) |
1407 | tputs (visible_bell, 1, _rl_output_character_function); | |
870ca253 | 1408 | else |
1267a5c0 | 1409 | #endif /* !__GO32__ */ |
870ca253 SG |
1410 | { |
1411 | fprintf (stderr, "\007"); | |
1412 | fflush (stderr); | |
1413 | } | |
bd5635a1 RP |
1414 | } |
1415 | return (-1); | |
1416 | } | |
1417 | ||
1418 | /* How to abort things. */ | |
1419 | rl_abort () | |
1420 | { | |
1421 | ding (); | |
1422 | rl_clear_message (); | |
1423 | rl_init_argument (); | |
1424 | rl_pending_input = 0; | |
1425 | ||
1426 | defining_kbd_macro = 0; | |
1427 | while (executing_macro) | |
1428 | pop_executing_macro (); | |
1429 | ||
870ca253 | 1430 | rl_last_func = (Function *)NULL; |
bd5635a1 RP |
1431 | longjmp (readline_top_level, 1); |
1432 | } | |
1433 | ||
1434 | /* Return a copy of the string between FROM and TO. | |
1435 | FROM is inclusive, TO is not. */ | |
870ca253 | 1436 | char * |
5e98bbab | 1437 | rl_copy_text (from, to) |
bd5635a1 RP |
1438 | int from, to; |
1439 | { | |
1440 | register int length; | |
1441 | char *copy; | |
1442 | ||
1443 | /* Fix it if the caller is confused. */ | |
870ca253 SG |
1444 | if (from > to) |
1445 | { | |
1446 | int t = from; | |
1447 | from = to; | |
1448 | to = t; | |
1449 | } | |
bd5635a1 RP |
1450 | |
1451 | length = to - from; | |
1452 | copy = (char *)xmalloc (1 + length); | |
1453 | strncpy (copy, the_line + from, length); | |
1454 | copy[length] = '\0'; | |
1455 | return (copy); | |
1456 | } | |
1457 | ||
870ca253 SG |
1458 | /* Increase the size of RL_LINE_BUFFER until it has enough space to hold |
1459 | LEN characters. */ | |
1460 | void | |
1461 | rl_extend_line_buffer (len) | |
1462 | int len; | |
1463 | { | |
1464 | while (len >= rl_line_buffer_len) | |
1465 | rl_line_buffer = | |
1466 | (char *)xrealloc | |
1467 | (rl_line_buffer, rl_line_buffer_len += DEFAULT_BUFFER_SIZE); | |
1468 | ||
1469 | the_line = rl_line_buffer; | |
1470 | } | |
1471 | ||
bd5635a1 RP |
1472 | \f |
1473 | /* **************************************************************** */ | |
1474 | /* */ | |
1475 | /* Insert and Delete */ | |
1476 | /* */ | |
1477 | /* **************************************************************** */ | |
1478 | ||
bd5635a1 RP |
1479 | /* Insert a string of text into the line at point. This is the only |
1480 | way that you should do insertion. rl_insert () calls this | |
1481 | function. */ | |
1482 | rl_insert_text (string) | |
1483 | char *string; | |
1484 | { | |
bd5635a1 | 1485 | register int i, l = strlen (string); |
870ca253 SG |
1486 | |
1487 | if (rl_end + l >= rl_line_buffer_len) | |
1488 | rl_extend_line_buffer (rl_end + l); | |
bd5635a1 RP |
1489 | |
1490 | for (i = rl_end; i >= rl_point; i--) | |
1491 | the_line[i + l] = the_line[i]; | |
1492 | strncpy (the_line + rl_point, string, l); | |
1493 | ||
1494 | /* Remember how to undo this if we aren't undoing something. */ | |
1495 | if (!doing_an_undo) | |
1496 | { | |
1497 | /* If possible and desirable, concatenate the undos. */ | |
1498 | if ((strlen (string) == 1) && | |
1499 | rl_undo_list && | |
1500 | (rl_undo_list->what == UNDO_INSERT) && | |
1501 | (rl_undo_list->end == rl_point) && | |
1502 | (rl_undo_list->end - rl_undo_list->start < 20)) | |
1503 | rl_undo_list->end++; | |
1504 | else | |
1505 | rl_add_undo (UNDO_INSERT, rl_point, rl_point + l, (char *)NULL); | |
1506 | } | |
1507 | rl_point += l; | |
1508 | rl_end += l; | |
1509 | the_line[rl_end] = '\0'; | |
1510 | } | |
1511 | ||
1512 | /* Delete the string between FROM and TO. FROM is | |
1513 | inclusive, TO is not. */ | |
1514 | rl_delete_text (from, to) | |
1515 | int from, to; | |
1516 | { | |
bd5635a1 RP |
1517 | register char *text; |
1518 | ||
1519 | /* Fix it if the caller is confused. */ | |
870ca253 SG |
1520 | if (from > to) |
1521 | { | |
1522 | int t = from; | |
1523 | from = to; | |
1524 | to = t; | |
1525 | } | |
5e98bbab | 1526 | text = rl_copy_text (from, to); |
bd5635a1 RP |
1527 | strncpy (the_line + from, the_line + to, rl_end - to); |
1528 | ||
1529 | /* Remember how to undo this delete. */ | |
1530 | if (!doing_an_undo) | |
1531 | rl_add_undo (UNDO_DELETE, from, to, text); | |
1532 | else | |
1533 | free (text); | |
1534 | ||
1535 | rl_end -= (to - from); | |
1536 | the_line[rl_end] = '\0'; | |
1537 | } | |
1538 | ||
1539 | \f | |
1540 | /* **************************************************************** */ | |
1541 | /* */ | |
1542 | /* Readline character functions */ | |
1543 | /* */ | |
1544 | /* **************************************************************** */ | |
1545 | ||
1546 | /* This is not a gap editor, just a stupid line input routine. No hair | |
1547 | is involved in writing any of the functions, and none should be. */ | |
1548 | ||
1549 | /* Note that: | |
1550 | ||
1551 | rl_end is the place in the string that we would place '\0'; | |
1552 | i.e., it is always safe to place '\0' there. | |
1553 | ||
1554 | rl_point is the place in the string where the cursor is. Sometimes | |
1555 | this is the same as rl_end. | |
1556 | ||
1557 | Any command that is called interactively receives two arguments. | |
1558 | The first is a count: the numeric arg pased to this command. | |
1559 | The second is the key which invoked this command. | |
1560 | */ | |
1561 | ||
1562 | \f | |
1563 | /* **************************************************************** */ | |
1564 | /* */ | |
1565 | /* Movement Commands */ | |
1566 | /* */ | |
1567 | /* **************************************************************** */ | |
1568 | ||
1569 | /* Note that if you `optimize' the display for these functions, you cannot | |
1570 | use said functions in other functions which do not do optimizing display. | |
1571 | I.e., you will have to update the data base for rl_redisplay, and you | |
1572 | might as well let rl_redisplay do that job. */ | |
1573 | ||
1574 | /* Move forward COUNT characters. */ | |
1575 | rl_forward (count) | |
1576 | int count; | |
1577 | { | |
1578 | if (count < 0) | |
1579 | rl_backward (-count); | |
1580 | else | |
1581 | while (count) | |
1582 | { | |
870ca253 | 1583 | #if defined (VI_MODE) |
5e98bbab | 1584 | if (rl_point >= (rl_end - (rl_editing_mode == vi_mode))) |
bd5635a1 RP |
1585 | #else |
1586 | if (rl_point == rl_end) | |
870ca253 | 1587 | #endif /* VI_MODE */ |
bd5635a1 RP |
1588 | { |
1589 | ding (); | |
1590 | return; | |
1591 | } | |
1592 | else | |
1593 | rl_point++; | |
1594 | --count; | |
1595 | } | |
1596 | } | |
1597 | ||
1598 | /* Move backward COUNT characters. */ | |
1599 | rl_backward (count) | |
1600 | int count; | |
1601 | { | |
1602 | if (count < 0) | |
1603 | rl_forward (-count); | |
1604 | else | |
1605 | while (count) | |
1606 | { | |
1607 | if (!rl_point) | |
1608 | { | |
1609 | ding (); | |
1610 | return; | |
1611 | } | |
1612 | else | |
1613 | --rl_point; | |
1614 | --count; | |
1615 | } | |
1616 | } | |
1617 | ||
1618 | /* Move to the beginning of the line. */ | |
1619 | rl_beg_of_line () | |
1620 | { | |
1621 | rl_point = 0; | |
1622 | } | |
1623 | ||
1624 | /* Move to the end of the line. */ | |
1625 | rl_end_of_line () | |
1626 | { | |
1627 | rl_point = rl_end; | |
1628 | } | |
1629 | ||
1630 | /* Move forward a word. We do what Emacs does. */ | |
1631 | rl_forward_word (count) | |
1632 | int count; | |
1633 | { | |
1634 | int c; | |
1635 | ||
1636 | if (count < 0) | |
1637 | { | |
1638 | rl_backward_word (-count); | |
1639 | return; | |
1640 | } | |
1641 | ||
1642 | while (count) | |
1643 | { | |
1644 | if (rl_point == rl_end) | |
1645 | return; | |
1646 | ||
1647 | /* If we are not in a word, move forward until we are in one. | |
1648 | Then, move forward until we hit a non-alphabetic character. */ | |
1649 | c = the_line[rl_point]; | |
1650 | if (!alphabetic (c)) | |
1651 | { | |
1652 | while (++rl_point < rl_end) | |
1653 | { | |
1654 | c = the_line[rl_point]; | |
1655 | if (alphabetic (c)) break; | |
1656 | } | |
1657 | } | |
1658 | if (rl_point == rl_end) return; | |
1659 | while (++rl_point < rl_end) | |
1660 | { | |
1661 | c = the_line[rl_point]; | |
1662 | if (!alphabetic (c)) break; | |
1663 | } | |
1664 | --count; | |
1665 | } | |
1666 | } | |
1667 | ||
1668 | /* Move backward a word. We do what Emacs does. */ | |
1669 | rl_backward_word (count) | |
1670 | int count; | |
1671 | { | |
1672 | int c; | |
1673 | ||
1674 | if (count < 0) | |
1675 | { | |
1676 | rl_forward_word (-count); | |
1677 | return; | |
1678 | } | |
1679 | ||
1680 | while (count) | |
1681 | { | |
1682 | if (!rl_point) | |
1683 | return; | |
1684 | ||
1685 | /* Like rl_forward_word (), except that we look at the characters | |
1686 | just before point. */ | |
1687 | ||
1688 | c = the_line[rl_point - 1]; | |
1689 | if (!alphabetic (c)) | |
1690 | { | |
1691 | while (--rl_point) | |
1692 | { | |
1693 | c = the_line[rl_point - 1]; | |
1694 | if (alphabetic (c)) break; | |
1695 | } | |
1696 | } | |
1697 | ||
1698 | while (rl_point) | |
1699 | { | |
1700 | c = the_line[rl_point - 1]; | |
1701 | if (!alphabetic (c)) | |
1702 | break; | |
1703 | else --rl_point; | |
1704 | } | |
1705 | --count; | |
1706 | } | |
1707 | } | |
1708 | ||
1709 | /* Clear the current line. Numeric argument to C-l does this. */ | |
1710 | rl_refresh_line () | |
1711 | { | |
5e98bbab | 1712 | int curr_line = _rl_last_c_pos / screenwidth; |
bd5635a1 | 1713 | |
5e98bbab PB |
1714 | _rl_move_vert (curr_line); |
1715 | _rl_move_cursor_relative (0, the_line); /* XXX is this right */ | |
bd5635a1 | 1716 | |
1267a5c0 SC |
1717 | #ifdef __GO32__ |
1718 | { | |
5e98bbab PB |
1719 | int row, col, width, row_start; |
1720 | ||
1721 | ScreenGetCursor (&row, &col); | |
1722 | width = ScreenCols (); | |
1723 | row_start = ScreenPrimary + (row * width); | |
1724 | memset (row_start + col, 0, (width - col) * 2); | |
1267a5c0 SC |
1725 | } |
1726 | #else /* __GO32__ */ | |
bd5635a1 | 1727 | if (term_clreol) |
5e98bbab | 1728 | tputs (term_clreol, 1, _rl_output_character_function); |
1267a5c0 | 1729 | #endif /* __GO32__/else */ |
bd5635a1 RP |
1730 | |
1731 | rl_forced_update_display (); | |
1732 | rl_display_fixed = 1; | |
1733 | } | |
1734 | ||
1735 | /* C-l typed to a line without quoting clears the screen, and then reprints | |
1736 | the prompt and the current input line. Given a numeric arg, redraw only | |
1737 | the current line. */ | |
1738 | rl_clear_screen () | |
1739 | { | |
bd5635a1 RP |
1740 | if (rl_explicit_arg) |
1741 | { | |
1742 | rl_refresh_line (); | |
1743 | return; | |
1744 | } | |
1745 | ||
1267a5c0 | 1746 | #ifndef __GO32__ |
bd5635a1 | 1747 | if (term_clrpag) |
5e98bbab | 1748 | tputs (term_clrpag, 1, _rl_output_character_function); |
bd5635a1 | 1749 | else |
1267a5c0 | 1750 | #endif /* !__GO32__ */ |
bd5635a1 RP |
1751 | crlf (); |
1752 | ||
1753 | rl_forced_update_display (); | |
1754 | rl_display_fixed = 1; | |
1755 | } | |
1756 | ||
870ca253 SG |
1757 | rl_arrow_keys (count, c) |
1758 | int count, c; | |
1759 | { | |
1760 | int ch; | |
1761 | ||
1762 | ch = rl_read_key (); | |
1763 | ||
1764 | switch (to_upper (ch)) | |
1765 | { | |
1766 | case 'A': | |
1767 | rl_get_previous_history (count); | |
1768 | break; | |
1769 | ||
1770 | case 'B': | |
1771 | rl_get_next_history (count); | |
1772 | break; | |
1773 | ||
1774 | case 'C': | |
1775 | rl_forward (count); | |
1776 | break; | |
1777 | ||
1778 | case 'D': | |
1779 | rl_backward (count); | |
1780 | break; | |
1781 | ||
1782 | default: | |
1783 | ding (); | |
1784 | } | |
1785 | } | |
1786 | ||
bd5635a1 RP |
1787 | \f |
1788 | /* **************************************************************** */ | |
1789 | /* */ | |
1790 | /* Text commands */ | |
1791 | /* */ | |
1792 | /* **************************************************************** */ | |
1793 | ||
1794 | /* Insert the character C at the current location, moving point forward. */ | |
1795 | rl_insert (count, c) | |
1796 | int count, c; | |
1797 | { | |
1798 | register int i; | |
1799 | char *string; | |
1800 | ||
1801 | if (count <= 0) | |
1802 | return; | |
1803 | ||
1804 | /* If we can optimize, then do it. But don't let people crash | |
1805 | readline because of extra large arguments. */ | |
1806 | if (count > 1 && count < 1024) | |
1807 | { | |
5e98bbab | 1808 | string = (char *)alloca (1 + count); |
bd5635a1 RP |
1809 | |
1810 | for (i = 0; i < count; i++) | |
1811 | string[i] = c; | |
1812 | ||
1813 | string[i] = '\0'; | |
1814 | rl_insert_text (string); | |
1815 | return; | |
1816 | } | |
1817 | ||
1818 | if (count > 1024) | |
1819 | { | |
1820 | int decreaser; | |
1821 | ||
5e98bbab | 1822 | string = (char *)alloca (1024 + 1); |
bd5635a1 RP |
1823 | |
1824 | for (i = 0; i < 1024; i++) | |
1825 | string[i] = c; | |
1826 | ||
1827 | while (count) | |
1828 | { | |
1829 | decreaser = (count > 1024 ? 1024 : count); | |
1830 | string[decreaser] = '\0'; | |
1831 | rl_insert_text (string); | |
1832 | count -= decreaser; | |
1833 | } | |
1834 | return; | |
1835 | } | |
1836 | ||
1837 | /* We are inserting a single character. | |
1838 | If there is pending input, then make a string of all of the | |
1839 | pending characters that are bound to rl_insert, and insert | |
1840 | them all. */ | |
1841 | if (any_typein) | |
1842 | { | |
1843 | int key = 0, t; | |
1844 | ||
1845 | i = 0; | |
5e98bbab | 1846 | string = (char *)alloca (ibuffer_len + 1); |
bd5635a1 RP |
1847 | string[i++] = c; |
1848 | ||
1849 | while ((t = rl_get_char (&key)) && | |
5e98bbab PB |
1850 | (_rl_keymap[key].type == ISFUNC && |
1851 | _rl_keymap[key].function == rl_insert)) | |
bd5635a1 RP |
1852 | string[i++] = key; |
1853 | ||
1854 | if (t) | |
1855 | rl_unget_char (key); | |
1856 | ||
1857 | string[i] = '\0'; | |
1858 | rl_insert_text (string); | |
1859 | return; | |
1860 | } | |
1861 | else | |
1862 | { | |
1863 | /* Inserting a single character. */ | |
5e98bbab | 1864 | string = (char *)alloca (2); |
bd5635a1 RP |
1865 | |
1866 | string[1] = '\0'; | |
1867 | string[0] = c; | |
1868 | rl_insert_text (string); | |
1869 | } | |
1870 | } | |
1871 | ||
1872 | /* Insert the next typed character verbatim. */ | |
1873 | rl_quoted_insert (count) | |
1874 | int count; | |
1875 | { | |
5e98bbab PB |
1876 | int c; |
1877 | ||
1878 | c = rl_read_key (); | |
bd5635a1 RP |
1879 | rl_insert (count, c); |
1880 | } | |
1881 | ||
1882 | /* Insert a tab character. */ | |
1883 | rl_tab_insert (count) | |
1884 | int count; | |
1885 | { | |
1886 | rl_insert (count, '\t'); | |
1887 | } | |
1888 | ||
1889 | /* What to do when a NEWLINE is pressed. We accept the whole line. | |
1890 | KEY is the key that invoked this command. I guess it could have | |
1891 | meaning in the future. */ | |
1892 | rl_newline (count, key) | |
1893 | int count, key; | |
1894 | { | |
1895 | ||
1896 | rl_done = 1; | |
1897 | ||
870ca253 | 1898 | #if defined (VI_MODE) |
bd5635a1 | 1899 | { |
5e98bbab PB |
1900 | extern int _rl_vi_doing_insert; |
1901 | if (_rl_vi_doing_insert) | |
bd5635a1 RP |
1902 | { |
1903 | rl_end_undo_group (); | |
5e98bbab | 1904 | _rl_vi_doing_insert = 0; |
bd5635a1 RP |
1905 | } |
1906 | } | |
5e98bbab PB |
1907 | rl_vi_set_last (); |
1908 | ||
bd5635a1 RP |
1909 | #endif /* VI_MODE */ |
1910 | ||
1911 | if (readline_echoing_p) | |
1912 | { | |
5e98bbab PB |
1913 | _rl_move_vert (_rl_vis_botlin); |
1914 | _rl_vis_botlin = 0; | |
bd5635a1 RP |
1915 | crlf (); |
1916 | fflush (out_stream); | |
1917 | rl_display_fixed++; | |
1918 | } | |
1919 | } | |
1920 | ||
1921 | rl_clean_up_for_exit () | |
1922 | { | |
1923 | if (readline_echoing_p) | |
1924 | { | |
5e98bbab PB |
1925 | _rl_move_vert (_rl_vis_botlin); |
1926 | _rl_vis_botlin = 0; | |
bd5635a1 RP |
1927 | fflush (out_stream); |
1928 | rl_restart_output (); | |
1929 | } | |
1930 | } | |
1931 | ||
1932 | /* What to do for some uppercase characters, like meta characters, | |
1933 | and some characters appearing in emacs_ctlx_keymap. This function | |
1934 | is just a stub, you bind keys to it and the code in rl_dispatch () | |
1935 | is special cased. */ | |
1936 | rl_do_lowercase_version (ignore1, ignore2) | |
1937 | int ignore1, ignore2; | |
1938 | { | |
1939 | } | |
1940 | ||
1941 | /* Rubout the character behind point. */ | |
1942 | rl_rubout (count) | |
1943 | int count; | |
1944 | { | |
1945 | if (count < 0) | |
1946 | { | |
1947 | rl_delete (-count); | |
1948 | return; | |
1949 | } | |
1950 | ||
1951 | if (!rl_point) | |
1952 | { | |
1953 | ding (); | |
1954 | return; | |
1955 | } | |
1956 | ||
5e98bbab | 1957 | if (count > 1 || rl_explicit_arg) |
bd5635a1 RP |
1958 | { |
1959 | int orig_point = rl_point; | |
1960 | rl_backward (count); | |
1961 | rl_kill_text (orig_point, rl_point); | |
1962 | } | |
1963 | else | |
1964 | { | |
1965 | int c = the_line[--rl_point]; | |
1966 | rl_delete_text (rl_point, rl_point + 1); | |
1967 | ||
5e98bbab | 1968 | if (rl_point == rl_end && isprint (c) && _rl_last_c_pos) |
bd5635a1 | 1969 | { |
5e98bbab PB |
1970 | int l; |
1971 | l = rl_character_len (c, rl_point); | |
1972 | _rl_erase_at_end_of_line (l); | |
bd5635a1 RP |
1973 | } |
1974 | } | |
1975 | } | |
1976 | ||
1977 | /* Delete the character under the cursor. Given a numeric argument, | |
1978 | kill that many characters instead. */ | |
1979 | rl_delete (count, invoking_key) | |
1980 | int count, invoking_key; | |
1981 | { | |
1982 | if (count < 0) | |
1983 | { | |
1984 | rl_rubout (-count); | |
1985 | return; | |
1986 | } | |
1987 | ||
1988 | if (rl_point == rl_end) | |
1989 | { | |
1990 | ding (); | |
1991 | return; | |
1992 | } | |
1993 | ||
5e98bbab | 1994 | if (count > 1 || rl_explicit_arg) |
bd5635a1 RP |
1995 | { |
1996 | int orig_point = rl_point; | |
1997 | rl_forward (count); | |
1998 | rl_kill_text (orig_point, rl_point); | |
1999 | rl_point = orig_point; | |
2000 | } | |
2001 | else | |
2002 | rl_delete_text (rl_point, rl_point + 1); | |
2003 | } | |
2004 | ||
5e98bbab PB |
2005 | /* Delete all spaces and tabs around point. */ |
2006 | rl_delete_horizontal_space (count, ignore) | |
2007 | int count, ignore; | |
2008 | { | |
2009 | int start = rl_point; | |
2010 | ||
2011 | while (rl_point && whitespace (the_line[rl_point - 1])) | |
2012 | rl_point--; | |
2013 | ||
2014 | start = rl_point; | |
2015 | ||
2016 | while (rl_point < rl_end && whitespace (the_line[rl_point])) | |
2017 | rl_point++; | |
2018 | ||
2019 | if (start == rl_point) | |
2020 | return; | |
2021 | else | |
2022 | { | |
2023 | rl_delete_text (start, rl_point); | |
2024 | rl_point = start; | |
2025 | } | |
2026 | } | |
bd5635a1 RP |
2027 | \f |
2028 | /* **************************************************************** */ | |
2029 | /* */ | |
2030 | /* Kill commands */ | |
2031 | /* */ | |
2032 | /* **************************************************************** */ | |
2033 | ||
2034 | /* The next two functions mimic unix line editing behaviour, except they | |
2035 | save the deleted text on the kill ring. This is safer than not saving | |
2036 | it, and since we have a ring, nobody should get screwed. */ | |
2037 | ||
2038 | /* This does what C-w does in Unix. We can't prevent people from | |
2039 | using behaviour that they expect. */ | |
2040 | rl_unix_word_rubout () | |
2041 | { | |
5e98bbab PB |
2042 | if (!rl_point) |
2043 | ding (); | |
2044 | else | |
2045 | { | |
2046 | int orig_point = rl_point; | |
2047 | ||
2048 | while (rl_point && whitespace (the_line[rl_point - 1])) | |
2049 | rl_point--; | |
2050 | ||
2051 | while (rl_point && !whitespace (the_line[rl_point - 1])) | |
2052 | rl_point--; | |
2053 | ||
2054 | rl_kill_text (rl_point, orig_point); | |
2055 | } | |
bd5635a1 RP |
2056 | } |
2057 | ||
2058 | /* Here is C-u doing what Unix does. You don't *have* to use these | |
2059 | key-bindings. We have a choice of killing the entire line, or | |
2060 | killing from where we are to the start of the line. We choose the | |
2061 | latter, because if you are a Unix weenie, then you haven't backspaced | |
2062 | into the line at all, and if you aren't, then you know what you are | |
2063 | doing. */ | |
2064 | rl_unix_line_discard () | |
2065 | { | |
5e98bbab PB |
2066 | if (!rl_point) |
2067 | ding (); | |
2068 | else | |
2069 | { | |
2070 | rl_kill_text (rl_point, 0); | |
2071 | rl_point = 0; | |
2072 | } | |
bd5635a1 RP |
2073 | } |
2074 | ||
2075 | \f | |
bd5635a1 RP |
2076 | /* **************************************************************** */ |
2077 | /* */ | |
2078 | /* Commands For Typos */ | |
2079 | /* */ | |
2080 | /* **************************************************************** */ | |
2081 | ||
2082 | /* Random and interesting things in here. */ | |
2083 | ||
bd5635a1 RP |
2084 | /* **************************************************************** */ |
2085 | /* */ | |
2086 | /* Changing Case */ | |
2087 | /* */ | |
2088 | /* **************************************************************** */ | |
2089 | ||
2090 | /* The three kinds of things that we know how to do. */ | |
2091 | #define UpCase 1 | |
2092 | #define DownCase 2 | |
2093 | #define CapCase 3 | |
2094 | ||
2095 | /* Uppercase the word at point. */ | |
2096 | rl_upcase_word (count) | |
2097 | int count; | |
2098 | { | |
2099 | rl_change_case (count, UpCase); | |
2100 | } | |
2101 | ||
2102 | /* Lowercase the word at point. */ | |
2103 | rl_downcase_word (count) | |
2104 | int count; | |
2105 | { | |
2106 | rl_change_case (count, DownCase); | |
2107 | } | |
2108 | ||
2109 | /* Upcase the first letter, downcase the rest. */ | |
2110 | rl_capitalize_word (count) | |
2111 | int count; | |
2112 | { | |
2113 | rl_change_case (count, CapCase); | |
2114 | } | |
2115 | ||
2116 | /* The meaty function. | |
2117 | Change the case of COUNT words, performing OP on them. | |
2118 | OP is one of UpCase, DownCase, or CapCase. | |
2119 | If a negative argument is given, leave point where it started, | |
2120 | otherwise, leave it where it moves to. */ | |
2121 | rl_change_case (count, op) | |
2122 | int count, op; | |
2123 | { | |
2124 | register int start = rl_point, end; | |
2125 | int state = 0; | |
2126 | ||
2127 | rl_forward_word (count); | |
2128 | end = rl_point; | |
2129 | ||
2130 | if (count < 0) | |
2131 | { | |
2132 | int temp = start; | |
2133 | start = end; | |
2134 | end = temp; | |
2135 | } | |
2136 | ||
2137 | /* We are going to modify some text, so let's prepare to undo it. */ | |
2138 | rl_modifying (start, end); | |
2139 | ||
2140 | for (; start < end; start++) | |
2141 | { | |
2142 | switch (op) | |
2143 | { | |
2144 | case UpCase: | |
2145 | the_line[start] = to_upper (the_line[start]); | |
2146 | break; | |
2147 | ||
2148 | case DownCase: | |
2149 | the_line[start] = to_lower (the_line[start]); | |
2150 | break; | |
2151 | ||
2152 | case CapCase: | |
2153 | if (state == 0) | |
2154 | { | |
2155 | the_line[start] = to_upper (the_line[start]); | |
2156 | state = 1; | |
2157 | } | |
2158 | else | |
2159 | { | |
2160 | the_line[start] = to_lower (the_line[start]); | |
2161 | } | |
2162 | if (!pure_alphabetic (the_line[start])) | |
2163 | state = 0; | |
2164 | break; | |
2165 | ||
2166 | default: | |
2167 | abort (); | |
2168 | } | |
2169 | } | |
2170 | rl_point = end; | |
2171 | } | |
2172 | ||
2173 | /* **************************************************************** */ | |
2174 | /* */ | |
2175 | /* Transposition */ | |
2176 | /* */ | |
2177 | /* **************************************************************** */ | |
2178 | ||
2179 | /* Transpose the words at point. */ | |
2180 | rl_transpose_words (count) | |
2181 | int count; | |
2182 | { | |
2183 | char *word1, *word2; | |
2184 | int w1_beg, w1_end, w2_beg, w2_end; | |
2185 | int orig_point = rl_point; | |
2186 | ||
2187 | if (!count) return; | |
2188 | ||
2189 | /* Find the two words. */ | |
2190 | rl_forward_word (count); | |
2191 | w2_end = rl_point; | |
2192 | rl_backward_word (1); | |
2193 | w2_beg = rl_point; | |
2194 | rl_backward_word (count); | |
2195 | w1_beg = rl_point; | |
2196 | rl_forward_word (1); | |
2197 | w1_end = rl_point; | |
2198 | ||
2199 | /* Do some check to make sure that there really are two words. */ | |
2200 | if ((w1_beg == w2_beg) || (w2_beg < w1_end)) | |
2201 | { | |
2202 | ding (); | |
2203 | rl_point = orig_point; | |
2204 | return; | |
2205 | } | |
2206 | ||
2207 | /* Get the text of the words. */ | |
5e98bbab PB |
2208 | word1 = rl_copy_text (w1_beg, w1_end); |
2209 | word2 = rl_copy_text (w2_beg, w2_end); | |
bd5635a1 RP |
2210 | |
2211 | /* We are about to do many insertions and deletions. Remember them | |
2212 | as one operation. */ | |
2213 | rl_begin_undo_group (); | |
2214 | ||
2215 | /* Do the stuff at word2 first, so that we don't have to worry | |
2216 | about word1 moving. */ | |
2217 | rl_point = w2_beg; | |
2218 | rl_delete_text (w2_beg, w2_end); | |
2219 | rl_insert_text (word1); | |
2220 | ||
2221 | rl_point = w1_beg; | |
2222 | rl_delete_text (w1_beg, w1_end); | |
2223 | rl_insert_text (word2); | |
2224 | ||
2225 | /* This is exactly correct since the text before this point has not | |
2226 | changed in length. */ | |
2227 | rl_point = w2_end; | |
2228 | ||
2229 | /* I think that does it. */ | |
2230 | rl_end_undo_group (); | |
2231 | free (word1); free (word2); | |
2232 | } | |
2233 | ||
2234 | /* Transpose the characters at point. If point is at the end of the line, | |
2235 | then transpose the characters before point. */ | |
2236 | rl_transpose_chars (count) | |
2237 | int count; | |
2238 | { | |
5e98bbab PB |
2239 | char dummy[2]; |
2240 | ||
bd5635a1 RP |
2241 | if (!count) |
2242 | return; | |
2243 | ||
5e98bbab PB |
2244 | if (!rl_point || rl_end < 2) |
2245 | { | |
2246 | ding (); | |
2247 | return; | |
2248 | } | |
bd5635a1 | 2249 | |
5e98bbab PB |
2250 | rl_begin_undo_group (); |
2251 | ||
2252 | if (rl_point == rl_end) | |
870ca253 | 2253 | { |
5e98bbab PB |
2254 | --rl_point; |
2255 | count = 1; | |
2256 | } | |
2257 | rl_point--; | |
870ca253 | 2258 | |
5e98bbab PB |
2259 | dummy[0] = the_line[rl_point]; |
2260 | dummy[1] = '\0'; | |
870ca253 | 2261 | |
5e98bbab | 2262 | rl_delete_text (rl_point, rl_point + 1); |
870ca253 | 2263 | |
5e98bbab PB |
2264 | rl_point += count; |
2265 | if (rl_point > rl_end) | |
2266 | rl_point = rl_end; | |
2267 | else if (rl_point < 0) | |
2268 | rl_point = 0; | |
2269 | rl_insert_text (dummy); | |
870ca253 | 2270 | |
5e98bbab | 2271 | rl_end_undo_group (); |
bd5635a1 | 2272 | } |
bd5635a1 RP |
2273 | \f |
2274 | /* **************************************************************** */ | |
2275 | /* */ | |
5e98bbab | 2276 | /* Undo, and Undoing */ |
bd5635a1 RP |
2277 | /* */ |
2278 | /* **************************************************************** */ | |
2279 | ||
5e98bbab PB |
2280 | /* Non-zero tells rl_delete_text and rl_insert_text to not add to |
2281 | the undo list. */ | |
2282 | static int doing_an_undo = 0; | |
870ca253 | 2283 | |
5e98bbab PB |
2284 | /* The current undo list for THE_LINE. */ |
2285 | UNDO_LIST *rl_undo_list = (UNDO_LIST *)NULL; | |
bd5635a1 | 2286 | |
5e98bbab PB |
2287 | /* Remember how to undo something. Concatenate some undos if that |
2288 | seems right. */ | |
2289 | rl_add_undo (what, start, end, text) | |
2290 | enum undo_code what; | |
2291 | int start, end; | |
2292 | char *text; | |
bd5635a1 RP |
2293 | { |
2294 | UNDO_LIST *temp = (UNDO_LIST *)xmalloc (sizeof (UNDO_LIST)); | |
2295 | temp->what = what; | |
2296 | temp->start = start; | |
2297 | temp->end = end; | |
2298 | temp->text = text; | |
2299 | temp->next = rl_undo_list; | |
2300 | rl_undo_list = temp; | |
2301 | } | |
2302 | ||
2303 | /* Free the existing undo list. */ | |
2304 | free_undo_list () | |
2305 | { | |
5e98bbab PB |
2306 | while (rl_undo_list) |
2307 | { | |
2308 | UNDO_LIST *release = rl_undo_list; | |
2309 | rl_undo_list = rl_undo_list->next; | |
bd5635a1 | 2310 | |
5e98bbab PB |
2311 | if (release->what == UNDO_DELETE) |
2312 | free (release->text); | |
bd5635a1 | 2313 | |
5e98bbab PB |
2314 | free (release); |
2315 | } | |
2316 | rl_undo_list = (UNDO_LIST *)NULL; | |
bd5635a1 RP |
2317 | } |
2318 | ||
2319 | /* Undo the next thing in the list. Return 0 if there | |
2320 | is nothing to undo, or non-zero if there was. */ | |
2321 | int | |
2322 | rl_do_undo () | |
2323 | { | |
2324 | UNDO_LIST *release; | |
2325 | int waiting_for_begin = 0; | |
2326 | ||
2327 | undo_thing: | |
2328 | if (!rl_undo_list) | |
2329 | return (0); | |
2330 | ||
2331 | doing_an_undo = 1; | |
2332 | ||
2333 | switch (rl_undo_list->what) { | |
2334 | ||
2335 | /* Undoing deletes means inserting some text. */ | |
2336 | case UNDO_DELETE: | |
2337 | rl_point = rl_undo_list->start; | |
2338 | rl_insert_text (rl_undo_list->text); | |
2339 | free (rl_undo_list->text); | |
2340 | break; | |
2341 | ||
2342 | /* Undoing inserts means deleting some text. */ | |
2343 | case UNDO_INSERT: | |
2344 | rl_delete_text (rl_undo_list->start, rl_undo_list->end); | |
2345 | rl_point = rl_undo_list->start; | |
2346 | break; | |
2347 | ||
2348 | /* Undoing an END means undoing everything 'til we get to | |
2349 | a BEGIN. */ | |
2350 | case UNDO_END: | |
2351 | waiting_for_begin++; | |
2352 | break; | |
2353 | ||
2354 | /* Undoing a BEGIN means that we are done with this group. */ | |
2355 | case UNDO_BEGIN: | |
2356 | if (waiting_for_begin) | |
2357 | waiting_for_begin--; | |
2358 | else | |
5e98bbab | 2359 | #if 0 |
bd5635a1 | 2360 | abort (); |
5e98bbab PB |
2361 | #else |
2362 | ding (); | |
2363 | #endif | |
bd5635a1 RP |
2364 | break; |
2365 | } | |
2366 | ||
2367 | doing_an_undo = 0; | |
2368 | ||
2369 | release = rl_undo_list; | |
2370 | rl_undo_list = rl_undo_list->next; | |
2371 | free (release); | |
2372 | ||
2373 | if (waiting_for_begin) | |
2374 | goto undo_thing; | |
2375 | ||
2376 | return (1); | |
2377 | } | |
2378 | ||
2379 | /* Begin a group. Subsequent undos are undone as an atomic operation. */ | |
2380 | rl_begin_undo_group () | |
2381 | { | |
2382 | rl_add_undo (UNDO_BEGIN, 0, 0, 0); | |
2383 | } | |
2384 | ||
2385 | /* End an undo group started with rl_begin_undo_group (). */ | |
2386 | rl_end_undo_group () | |
2387 | { | |
2388 | rl_add_undo (UNDO_END, 0, 0, 0); | |
2389 | } | |
2390 | ||
2391 | /* Save an undo entry for the text from START to END. */ | |
2392 | rl_modifying (start, end) | |
2393 | int start, end; | |
2394 | { | |
2395 | if (start > end) | |
2396 | { | |
2397 | int t = start; | |
2398 | start = end; | |
2399 | end = t; | |
2400 | } | |
2401 | ||
2402 | if (start != end) | |
2403 | { | |
5e98bbab | 2404 | char *temp = rl_copy_text (start, end); |
bd5635a1 RP |
2405 | rl_begin_undo_group (); |
2406 | rl_add_undo (UNDO_DELETE, start, end, temp); | |
2407 | rl_add_undo (UNDO_INSERT, start, end, (char *)NULL); | |
2408 | rl_end_undo_group (); | |
2409 | } | |
2410 | } | |
2411 | ||
2412 | /* Revert the current line to its previous state. */ | |
2413 | rl_revert_line () | |
2414 | { | |
5e98bbab PB |
2415 | if (!rl_undo_list) |
2416 | ding (); | |
2417 | else | |
2418 | { | |
2419 | while (rl_undo_list) | |
2420 | rl_do_undo (); | |
2421 | } | |
bd5635a1 RP |
2422 | } |
2423 | ||
2424 | /* Do some undoing of things that were done. */ | |
2425 | rl_undo_command (count) | |
2426 | { | |
2427 | if (count < 0) return; /* Nothing to do. */ | |
2428 | ||
2429 | while (count) | |
2430 | { | |
2431 | if (rl_do_undo ()) | |
2432 | { | |
2433 | count--; | |
2434 | } | |
2435 | else | |
2436 | { | |
2437 | ding (); | |
2438 | break; | |
2439 | } | |
2440 | } | |
2441 | } | |
2442 | \f | |
2443 | /* **************************************************************** */ | |
2444 | /* */ | |
2445 | /* History Utilities */ | |
2446 | /* */ | |
2447 | /* **************************************************************** */ | |
2448 | ||
2449 | /* We already have a history library, and that is what we use to control | |
2450 | the history features of readline. However, this is our local interface | |
2451 | to the history mechanism. */ | |
2452 | ||
2453 | /* While we are editing the history, this is the saved | |
2454 | version of the original line. */ | |
2455 | HIST_ENTRY *saved_line_for_history = (HIST_ENTRY *)NULL; | |
2456 | ||
2457 | /* Set the history pointer back to the last entry in the history. */ | |
2458 | start_using_history () | |
2459 | { | |
2460 | using_history (); | |
2461 | if (saved_line_for_history) | |
2462 | free_history_entry (saved_line_for_history); | |
2463 | ||
2464 | saved_line_for_history = (HIST_ENTRY *)NULL; | |
2465 | } | |
2466 | ||
2467 | /* Free the contents (and containing structure) of a HIST_ENTRY. */ | |
5e98bbab | 2468 | void |
bd5635a1 RP |
2469 | free_history_entry (entry) |
2470 | HIST_ENTRY *entry; | |
2471 | { | |
2472 | if (!entry) return; | |
2473 | if (entry->line) | |
2474 | free (entry->line); | |
2475 | free (entry); | |
2476 | } | |
2477 | ||
2478 | /* Perhaps put back the current line if it has changed. */ | |
2479 | maybe_replace_line () | |
2480 | { | |
2481 | HIST_ENTRY *temp = current_history (); | |
2482 | ||
2483 | /* If the current line has changed, save the changes. */ | |
870ca253 SG |
2484 | if (temp && ((UNDO_LIST *)(temp->data) != rl_undo_list)) |
2485 | { | |
2486 | temp = replace_history_entry (where_history (), the_line, rl_undo_list); | |
2487 | free (temp->line); | |
2488 | free (temp); | |
2489 | } | |
bd5635a1 RP |
2490 | } |
2491 | ||
2492 | /* Put back the saved_line_for_history if there is one. */ | |
2493 | maybe_unsave_line () | |
2494 | { | |
870ca253 SG |
2495 | if (saved_line_for_history) |
2496 | { | |
2497 | int line_len; | |
2498 | ||
2499 | line_len = strlen (saved_line_for_history->line); | |
2500 | ||
2501 | if (line_len >= rl_line_buffer_len) | |
2502 | rl_extend_line_buffer (line_len); | |
2503 | ||
2504 | strcpy (the_line, saved_line_for_history->line); | |
2505 | rl_undo_list = (UNDO_LIST *)saved_line_for_history->data; | |
2506 | free_history_entry (saved_line_for_history); | |
2507 | saved_line_for_history = (HIST_ENTRY *)NULL; | |
2508 | rl_end = rl_point = strlen (the_line); | |
2509 | } | |
2510 | else | |
bd5635a1 | 2511 | ding (); |
bd5635a1 RP |
2512 | } |
2513 | ||
2514 | /* Save the current line in saved_line_for_history. */ | |
2515 | maybe_save_line () | |
2516 | { | |
870ca253 SG |
2517 | if (!saved_line_for_history) |
2518 | { | |
2519 | saved_line_for_history = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY)); | |
2520 | saved_line_for_history->line = savestring (the_line); | |
2521 | saved_line_for_history->data = (char *)rl_undo_list; | |
2522 | } | |
bd5635a1 | 2523 | } |
bd5635a1 RP |
2524 | \f |
2525 | /* **************************************************************** */ | |
2526 | /* */ | |
2527 | /* History Commands */ | |
2528 | /* */ | |
2529 | /* **************************************************************** */ | |
2530 | ||
2531 | /* Meta-< goes to the start of the history. */ | |
2532 | rl_beginning_of_history () | |
2533 | { | |
2534 | rl_get_previous_history (1 + where_history ()); | |
2535 | } | |
2536 | ||
2537 | /* Meta-> goes to the end of the history. (The current line). */ | |
2538 | rl_end_of_history () | |
2539 | { | |
2540 | maybe_replace_line (); | |
2541 | using_history (); | |
2542 | maybe_unsave_line (); | |
2543 | } | |
2544 | ||
2545 | /* Move down to the next history line. */ | |
2546 | rl_get_next_history (count) | |
2547 | int count; | |
2548 | { | |
2549 | HIST_ENTRY *temp = (HIST_ENTRY *)NULL; | |
2550 | ||
2551 | if (count < 0) | |
2552 | { | |
2553 | rl_get_previous_history (-count); | |
2554 | return; | |
2555 | } | |
2556 | ||
2557 | if (!count) | |
2558 | return; | |
2559 | ||
2560 | maybe_replace_line (); | |
2561 | ||
2562 | while (count) | |
2563 | { | |
2564 | temp = next_history (); | |
2565 | if (!temp) | |
2566 | break; | |
2567 | --count; | |
2568 | } | |
2569 | ||
2570 | if (!temp) | |
2571 | maybe_unsave_line (); | |
2572 | else | |
2573 | { | |
870ca253 SG |
2574 | int line_len; |
2575 | ||
2576 | line_len = strlen (temp->line); | |
2577 | ||
2578 | if (line_len >= rl_line_buffer_len) | |
2579 | rl_extend_line_buffer (line_len); | |
2580 | ||
bd5635a1 RP |
2581 | strcpy (the_line, temp->line); |
2582 | rl_undo_list = (UNDO_LIST *)temp->data; | |
2583 | rl_end = rl_point = strlen (the_line); | |
870ca253 SG |
2584 | #if defined (VI_MODE) |
2585 | if (rl_editing_mode == vi_mode) | |
2586 | rl_point = 0; | |
2587 | #endif /* VI_MODE */ | |
bd5635a1 RP |
2588 | } |
2589 | } | |
2590 | ||
2591 | /* Get the previous item out of our interactive history, making it the current | |
2592 | line. If there is no previous history, just ding. */ | |
2593 | rl_get_previous_history (count) | |
2594 | int count; | |
2595 | { | |
2596 | HIST_ENTRY *old_temp = (HIST_ENTRY *)NULL; | |
2597 | HIST_ENTRY *temp = (HIST_ENTRY *)NULL; | |
2598 | ||
2599 | if (count < 0) | |
2600 | { | |
2601 | rl_get_next_history (-count); | |
2602 | return; | |
2603 | } | |
2604 | ||
2605 | if (!count) | |
2606 | return; | |
2607 | ||
2608 | /* If we don't have a line saved, then save this one. */ | |
2609 | maybe_save_line (); | |
2610 | ||
2611 | /* If the current line has changed, save the changes. */ | |
2612 | maybe_replace_line (); | |
2613 | ||
2614 | while (count) | |
2615 | { | |
2616 | temp = previous_history (); | |
2617 | if (!temp) | |
2618 | break; | |
2619 | else | |
2620 | old_temp = temp; | |
2621 | --count; | |
2622 | } | |
2623 | ||
2624 | /* If there was a large argument, and we moved back to the start of the | |
2625 | history, that is not an error. So use the last value found. */ | |
2626 | if (!temp && old_temp) | |
2627 | temp = old_temp; | |
2628 | ||
2629 | if (!temp) | |
2630 | ding (); | |
2631 | else | |
2632 | { | |
870ca253 SG |
2633 | int line_len; |
2634 | ||
2635 | line_len = strlen (temp->line); | |
2636 | ||
2637 | if (line_len >= rl_line_buffer_len) | |
2638 | rl_extend_line_buffer (line_len); | |
2639 | ||
bd5635a1 RP |
2640 | strcpy (the_line, temp->line); |
2641 | rl_undo_list = (UNDO_LIST *)temp->data; | |
870ca253 SG |
2642 | rl_end = rl_point = line_len; |
2643 | ||
2644 | #if defined (VI_MODE) | |
bd5635a1 RP |
2645 | if (rl_editing_mode == vi_mode) |
2646 | rl_point = 0; | |
2647 | #endif /* VI_MODE */ | |
2648 | } | |
2649 | } | |
2650 | ||
5e98bbab PB |
2651 | /* Make C be the next command to be executed. */ |
2652 | rl_execute_next (c) | |
2653 | int c; | |
2654 | { | |
2655 | rl_pending_input = c; | |
2656 | } | |
2657 | ||
bd5635a1 RP |
2658 | /* **************************************************************** */ |
2659 | /* */ | |
5e98bbab | 2660 | /* The Mark and the Region. */ |
bd5635a1 RP |
2661 | /* */ |
2662 | /* **************************************************************** */ | |
2663 | ||
5e98bbab PB |
2664 | /* Set the mark at POSITION. */ |
2665 | rl_set_mark (position) | |
2666 | int position; | |
bd5635a1 | 2667 | { |
5e98bbab PB |
2668 | if (position > rl_end) |
2669 | return; | |
bd5635a1 | 2670 | |
5e98bbab | 2671 | rl_mark = position; |
bd5635a1 RP |
2672 | } |
2673 | ||
5e98bbab PB |
2674 | /* Exchange the position of mark and point. */ |
2675 | rl_exchange_mark_and_point () | |
bd5635a1 | 2676 | { |
5e98bbab PB |
2677 | if (rl_mark > rl_end) |
2678 | rl_mark = -1; | |
bd5635a1 | 2679 | |
5e98bbab PB |
2680 | if (rl_mark == -1) |
2681 | { | |
2682 | ding (); | |
2683 | return; | |
2684 | } | |
bd5635a1 RP |
2685 | else |
2686 | { | |
5e98bbab | 2687 | int temp = rl_point; |
870ca253 | 2688 | |
5e98bbab PB |
2689 | rl_point = rl_mark; |
2690 | rl_mark = temp; | |
bd5635a1 | 2691 | } |
5e98bbab | 2692 | } |
bd5635a1 | 2693 | |
5e98bbab PB |
2694 | \f |
2695 | /* **************************************************************** */ | |
2696 | /* */ | |
2697 | /* Killing Mechanism */ | |
2698 | /* */ | |
2699 | /* **************************************************************** */ | |
bd5635a1 | 2700 | |
5e98bbab PB |
2701 | /* What we assume for a max number of kills. */ |
2702 | #define DEFAULT_MAX_KILLS 10 | |
bd5635a1 | 2703 | |
5e98bbab PB |
2704 | /* The real variable to look at to find out when to flush kills. */ |
2705 | int rl_max_kills = DEFAULT_MAX_KILLS; | |
bd5635a1 | 2706 | |
5e98bbab PB |
2707 | /* Where to store killed text. */ |
2708 | char **rl_kill_ring = (char **)NULL; | |
870ca253 | 2709 | |
5e98bbab PB |
2710 | /* Where we are in the kill ring. */ |
2711 | int rl_kill_index = 0; | |
bd5635a1 RP |
2712 | |
2713 | /* How many slots we have in the kill ring. */ | |
2714 | int rl_kill_ring_length = 0; | |
2715 | ||
2716 | /* How to say that you only want to save a certain amount | |
2717 | of kill material. */ | |
2718 | rl_set_retained_kills (num) | |
2719 | int num; | |
2720 | {} | |
2721 | ||
2722 | /* The way to kill something. This appends or prepends to the last | |
2723 | kill, if the last command was a kill command. if FROM is less | |
2724 | than TO, then the text is appended, otherwise prepended. If the | |
2725 | last command was not a kill command, then a new slot is made for | |
2726 | this kill. */ | |
2727 | rl_kill_text (from, to) | |
2728 | int from, to; | |
2729 | { | |
2730 | int slot; | |
5e98bbab | 2731 | char *text = rl_copy_text (from, to); |
bd5635a1 RP |
2732 | |
2733 | /* Is there anything to kill? */ | |
870ca253 SG |
2734 | if (from == to) |
2735 | { | |
2736 | free (text); | |
2737 | last_command_was_kill++; | |
2738 | return; | |
2739 | } | |
bd5635a1 RP |
2740 | |
2741 | /* Delete the copied text from the line. */ | |
2742 | rl_delete_text (from, to); | |
2743 | ||
2744 | /* First, find the slot to work with. */ | |
870ca253 SG |
2745 | if (!last_command_was_kill) |
2746 | { | |
2747 | /* Get a new slot. */ | |
2748 | if (!rl_kill_ring) | |
2749 | { | |
2750 | /* If we don't have any defined, then make one. */ | |
2751 | rl_kill_ring = (char **) | |
2752 | xmalloc (((rl_kill_ring_length = 1) + 1) * sizeof (char *)); | |
2753 | slot = 1; | |
2754 | } | |
2755 | else | |
2756 | { | |
2757 | /* We have to add a new slot on the end, unless we have | |
2758 | exceeded the max limit for remembering kills. */ | |
2759 | slot = rl_kill_ring_length; | |
2760 | if (slot == rl_max_kills) | |
2761 | { | |
2762 | register int i; | |
2763 | free (rl_kill_ring[0]); | |
2764 | for (i = 0; i < slot; i++) | |
2765 | rl_kill_ring[i] = rl_kill_ring[i + 1]; | |
2766 | } | |
2767 | else | |
2768 | { | |
2769 | rl_kill_ring = | |
2770 | (char **) | |
2771 | xrealloc (rl_kill_ring, | |
2772 | ((slot = (rl_kill_ring_length += 1)) + 1) | |
2773 | * sizeof (char *)); | |
2774 | } | |
2775 | } | |
2776 | slot--; | |
2777 | } | |
2778 | else | |
2779 | { | |
2780 | slot = rl_kill_ring_length - 1; | |
bd5635a1 | 2781 | } |
bd5635a1 RP |
2782 | |
2783 | /* If the last command was a kill, prepend or append. */ | |
870ca253 SG |
2784 | if (last_command_was_kill && rl_editing_mode != vi_mode) |
2785 | { | |
2786 | char *old = rl_kill_ring[slot]; | |
2787 | char *new = (char *)xmalloc (1 + strlen (old) + strlen (text)); | |
2788 | ||
2789 | if (from < to) | |
2790 | { | |
2791 | strcpy (new, old); | |
2792 | strcat (new, text); | |
2793 | } | |
2794 | else | |
2795 | { | |
2796 | strcpy (new, text); | |
2797 | strcat (new, old); | |
2798 | } | |
2799 | free (old); | |
2800 | free (text); | |
2801 | rl_kill_ring[slot] = new; | |
2802 | } | |
2803 | else | |
2804 | { | |
2805 | rl_kill_ring[slot] = text; | |
2806 | } | |
bd5635a1 RP |
2807 | rl_kill_index = slot; |
2808 | last_command_was_kill++; | |
2809 | } | |
2810 | ||
2811 | /* Now REMEMBER! In order to do prepending or appending correctly, kill | |
2812 | commands always make rl_point's original position be the FROM argument, | |
2813 | and rl_point's extent be the TO argument. */ | |
2814 | ||
bd5635a1 RP |
2815 | /* **************************************************************** */ |
2816 | /* */ | |
2817 | /* Killing Commands */ | |
2818 | /* */ | |
2819 | /* **************************************************************** */ | |
2820 | ||
2821 | /* Delete the word at point, saving the text in the kill ring. */ | |
2822 | rl_kill_word (count) | |
2823 | int count; | |
2824 | { | |
2825 | int orig_point = rl_point; | |
2826 | ||
2827 | if (count < 0) | |
2828 | rl_backward_kill_word (-count); | |
2829 | else | |
2830 | { | |
2831 | rl_forward_word (count); | |
2832 | ||
2833 | if (rl_point != orig_point) | |
2834 | rl_kill_text (orig_point, rl_point); | |
2835 | ||
2836 | rl_point = orig_point; | |
2837 | } | |
2838 | } | |
2839 | ||
2840 | /* Rubout the word before point, placing it on the kill ring. */ | |
2841 | rl_backward_kill_word (count) | |
2842 | int count; | |
2843 | { | |
2844 | int orig_point = rl_point; | |
2845 | ||
2846 | if (count < 0) | |
2847 | rl_kill_word (-count); | |
2848 | else | |
2849 | { | |
2850 | rl_backward_word (count); | |
2851 | ||
2852 | if (rl_point != orig_point) | |
2853 | rl_kill_text (orig_point, rl_point); | |
2854 | } | |
2855 | } | |
2856 | ||
2857 | /* Kill from here to the end of the line. If DIRECTION is negative, kill | |
2858 | back to the line start instead. */ | |
2859 | rl_kill_line (direction) | |
2860 | int direction; | |
2861 | { | |
2862 | int orig_point = rl_point; | |
2863 | ||
2864 | if (direction < 0) | |
2865 | rl_backward_kill_line (1); | |
2866 | else | |
2867 | { | |
2868 | rl_end_of_line (); | |
2869 | if (orig_point != rl_point) | |
2870 | rl_kill_text (orig_point, rl_point); | |
2871 | rl_point = orig_point; | |
2872 | } | |
2873 | } | |
2874 | ||
2875 | /* Kill backwards to the start of the line. If DIRECTION is negative, kill | |
2876 | forwards to the line end instead. */ | |
2877 | rl_backward_kill_line (direction) | |
2878 | int direction; | |
2879 | { | |
2880 | int orig_point = rl_point; | |
2881 | ||
2882 | if (direction < 0) | |
2883 | rl_kill_line (1); | |
2884 | else | |
2885 | { | |
2886 | if (!rl_point) | |
2887 | ding (); | |
2888 | else | |
2889 | { | |
2890 | rl_beg_of_line (); | |
2891 | rl_kill_text (orig_point, rl_point); | |
2892 | } | |
2893 | } | |
2894 | } | |
2895 | ||
2896 | /* Yank back the last killed text. This ignores arguments. */ | |
2897 | rl_yank () | |
2898 | { | |
5e98bbab PB |
2899 | if (!rl_kill_ring) |
2900 | rl_abort (); | |
2901 | ||
2902 | rl_set_mark (rl_point); | |
bd5635a1 RP |
2903 | rl_insert_text (rl_kill_ring[rl_kill_index]); |
2904 | } | |
2905 | ||
2906 | /* If the last command was yank, or yank_pop, and the text just | |
2907 | before point is identical to the current kill item, then | |
2908 | delete that text from the line, rotate the index down, and | |
2909 | yank back some other text. */ | |
2910 | rl_yank_pop () | |
2911 | { | |
2912 | int l; | |
2913 | ||
2914 | if (((rl_last_func != rl_yank_pop) && (rl_last_func != rl_yank)) || | |
2915 | !rl_kill_ring) | |
2916 | { | |
2917 | rl_abort (); | |
2918 | } | |
2919 | ||
2920 | l = strlen (rl_kill_ring[rl_kill_index]); | |
2921 | if (((rl_point - l) >= 0) && | |
2922 | (strncmp (the_line + (rl_point - l), | |
2923 | rl_kill_ring[rl_kill_index], l) == 0)) | |
2924 | { | |
2925 | rl_delete_text ((rl_point - l), rl_point); | |
2926 | rl_point -= l; | |
2927 | rl_kill_index--; | |
2928 | if (rl_kill_index < 0) | |
2929 | rl_kill_index = rl_kill_ring_length - 1; | |
2930 | rl_yank (); | |
2931 | } | |
2932 | else | |
2933 | rl_abort (); | |
2934 | ||
2935 | } | |
2936 | ||
2937 | /* Yank the COUNTth argument from the previous history line. */ | |
2938 | rl_yank_nth_arg (count, ignore) | |
2939 | int count; | |
2940 | { | |
2941 | register HIST_ENTRY *entry = previous_history (); | |
2942 | char *arg; | |
2943 | ||
2944 | if (entry) | |
2945 | next_history (); | |
2946 | else | |
2947 | { | |
2948 | ding (); | |
2949 | return; | |
2950 | } | |
2951 | ||
2952 | arg = history_arg_extract (count, count, entry->line); | |
2953 | if (!arg || !*arg) | |
2954 | { | |
2955 | ding (); | |
2956 | return; | |
2957 | } | |
2958 | ||
2959 | rl_begin_undo_group (); | |
870ca253 SG |
2960 | |
2961 | #if defined (VI_MODE) | |
5e98bbab | 2962 | /* Vi mode always inserts a space before yanking the argument, and it |
870ca253 SG |
2963 | inserts it right *after* rl_point. */ |
2964 | if (rl_editing_mode == vi_mode) | |
2965 | rl_point++; | |
2966 | #endif /* VI_MODE */ | |
2967 | ||
bd5635a1 RP |
2968 | if (rl_point && the_line[rl_point - 1] != ' ') |
2969 | rl_insert_text (" "); | |
870ca253 | 2970 | |
bd5635a1 RP |
2971 | rl_insert_text (arg); |
2972 | free (arg); | |
870ca253 | 2973 | |
bd5635a1 RP |
2974 | rl_end_undo_group (); |
2975 | } | |
2976 | ||
bd5635a1 RP |
2977 | /* How to toggle back and forth between editing modes. */ |
2978 | rl_vi_editing_mode () | |
2979 | { | |
870ca253 | 2980 | #if defined (VI_MODE) |
bd5635a1 RP |
2981 | rl_editing_mode = vi_mode; |
2982 | rl_vi_insertion_mode (); | |
2983 | #endif /* VI_MODE */ | |
2984 | } | |
2985 | ||
2986 | rl_emacs_editing_mode () | |
2987 | { | |
2988 | rl_editing_mode = emacs_mode; | |
5e98bbab | 2989 | _rl_keymap = emacs_standard_keymap; |
bd5635a1 RP |
2990 | } |
2991 | ||
2992 | \f | |
2993 | /* **************************************************************** */ | |
2994 | /* */ | |
5e98bbab | 2995 | /* USG (System V) Support */ |
bd5635a1 RP |
2996 | /* */ |
2997 | /* **************************************************************** */ | |
2998 | ||
bd5635a1 | 2999 | int |
5e98bbab PB |
3000 | rl_getc (stream) |
3001 | FILE *stream; | |
bd5635a1 RP |
3002 | { |
3003 | int result; | |
5e98bbab | 3004 | unsigned char c; |
d3c4fc98 | 3005 | |
5e98bbab PB |
3006 | #ifdef __GO32__ |
3007 | if (isatty (0)) | |
3008 | return (getkey () & 0x7f); | |
3009 | #endif /* __GO32__ */ | |
bd5635a1 | 3010 | |
5e98bbab | 3011 | while (1) |
bd5635a1 | 3012 | { |
5e98bbab | 3013 | result = read (fileno (stream), &c, sizeof (unsigned char)); |
bd5635a1 | 3014 | |
5e98bbab PB |
3015 | if (result == sizeof (unsigned char)) |
3016 | return (c); | |
bd5635a1 | 3017 | |
5e98bbab PB |
3018 | /* If zero characters are returned, then the file that we are |
3019 | reading from is empty! Return EOF in that case. */ | |
3020 | if (result == 0) | |
3021 | return (EOF); | |
bd5635a1 | 3022 | |
5e98bbab PB |
3023 | #if defined (EWOULDBLOCK) |
3024 | if (errno == EWOULDBLOCK) | |
bd5635a1 | 3025 | { |
5e98bbab | 3026 | int flags; |
bd5635a1 | 3027 | |
5e98bbab PB |
3028 | if ((flags = fcntl (fileno (stream), F_GETFL, 0)) < 0) |
3029 | return (EOF); | |
3030 | if (flags & O_NDELAY) | |
3031 | { | |
3032 | flags &= ~O_NDELAY; | |
3033 | fcntl (fileno (stream), F_SETFL, flags); | |
3034 | continue; | |
bd5635a1 | 3035 | } |
5e98bbab | 3036 | continue; |
bd5635a1 | 3037 | } |
5e98bbab | 3038 | #endif /* EWOULDBLOCK */ |
bd5635a1 | 3039 | |
5e98bbab PB |
3040 | #if defined (_POSIX_VERSION) && defined (EAGAIN) && defined (O_NONBLOCK) |
3041 | if (errno == EAGAIN) | |
bd5635a1 | 3042 | { |
5e98bbab | 3043 | int flags; |
bd5635a1 | 3044 | |
5e98bbab PB |
3045 | if ((flags = fcntl (fileno (stream), F_GETFL, 0)) < 0) |
3046 | return (EOF); | |
3047 | if (flags & O_NONBLOCK) | |
bd5635a1 | 3048 | { |
5e98bbab PB |
3049 | flags &= ~O_NONBLOCK; |
3050 | fcntl (fileno (stream), F_SETFL, flags); | |
bd5635a1 RP |
3051 | continue; |
3052 | } | |
3053 | } | |
5e98bbab | 3054 | #endif /* _POSIX_VERSION && EAGAIN && O_NONBLOCK */ |
870ca253 | 3055 | |
1267a5c0 | 3056 | #ifndef __GO32__ |
870ca253 SG |
3057 | /* If the error that we received was SIGINT, then try again, |
3058 | this is simply an interrupted system call to read (). | |
3059 | Otherwise, some error ocurred, also signifying EOF. */ | |
bd5635a1 RP |
3060 | if (errno != EINTR) |
3061 | return (EOF); | |
1267a5c0 | 3062 | #endif /* !__GO32__ */ |
bd5635a1 RP |
3063 | } |
3064 | } | |
bd5635a1 | 3065 | |
870ca253 | 3066 | #if defined (STATIC_MALLOC) |
bd5635a1 RP |
3067 | \f |
3068 | /* **************************************************************** */ | |
3069 | /* */ | |
3070 | /* xmalloc and xrealloc () */ | |
3071 | /* */ | |
3072 | /* **************************************************************** */ | |
3073 | ||
3074 | static void memory_error_and_abort (); | |
3075 | ||
3076 | static char * | |
3077 | xmalloc (bytes) | |
3078 | int bytes; | |
3079 | { | |
3080 | char *temp = (char *)malloc (bytes); | |
3081 | ||
3082 | if (!temp) | |
3083 | memory_error_and_abort (); | |
3084 | return (temp); | |
3085 | } | |
3086 | ||
3087 | static char * | |
3088 | xrealloc (pointer, bytes) | |
3089 | char *pointer; | |
3090 | int bytes; | |
3091 | { | |
870ca253 SG |
3092 | char *temp; |
3093 | ||
3094 | if (!pointer) | |
3095 | temp = (char *)malloc (bytes); | |
3096 | else | |
3097 | temp = (char *)realloc (pointer, bytes); | |
bd5635a1 RP |
3098 | |
3099 | if (!temp) | |
3100 | memory_error_and_abort (); | |
870ca253 | 3101 | |
bd5635a1 RP |
3102 | return (temp); |
3103 | } | |
3104 | ||
3105 | static void | |
3106 | memory_error_and_abort () | |
3107 | { | |
3108 | fprintf (stderr, "readline: Out of virtual memory!\n"); | |
3109 | abort (); | |
3110 | } | |
3111 | #endif /* STATIC_MALLOC */ | |
3112 | ||
3113 | \f | |
3114 | /* **************************************************************** */ | |
3115 | /* */ | |
3116 | /* Testing Readline */ | |
3117 | /* */ | |
3118 | /* **************************************************************** */ | |
3119 | ||
870ca253 | 3120 | #if defined (TEST) |
bd5635a1 RP |
3121 | |
3122 | main () | |
3123 | { | |
3124 | HIST_ENTRY **history_list (); | |
3125 | char *temp = (char *)NULL; | |
3126 | char *prompt = "readline% "; | |
3127 | int done = 0; | |
3128 | ||
3129 | while (!done) | |
3130 | { | |
3131 | temp = readline (prompt); | |
3132 | ||
3133 | /* Test for EOF. */ | |
3134 | if (!temp) | |
3135 | exit (1); | |
3136 | ||
3137 | /* If there is anything on the line, print it and remember it. */ | |
3138 | if (*temp) | |
3139 | { | |
3140 | fprintf (stderr, "%s\r\n", temp); | |
3141 | add_history (temp); | |
3142 | } | |
3143 | ||
3144 | /* Check for `command' that we handle. */ | |
3145 | if (strcmp (temp, "quit") == 0) | |
3146 | done = 1; | |
3147 | ||
870ca253 SG |
3148 | if (strcmp (temp, "list") == 0) |
3149 | { | |
3150 | HIST_ENTRY **list = history_list (); | |
3151 | register int i; | |
3152 | if (list) | |
3153 | { | |
3154 | for (i = 0; list[i]; i++) | |
3155 | { | |
3156 | fprintf (stderr, "%d: %s\r\n", i, list[i]->line); | |
3157 | free (list[i]->line); | |
3158 | } | |
3159 | free (list); | |
3160 | } | |
bd5635a1 | 3161 | } |
bd5635a1 RP |
3162 | free (temp); |
3163 | } | |
3164 | } | |
3165 | ||
3166 | #endif /* TEST */ | |
3167 | ||
3168 | \f | |
3169 | /* | |
3170 | * Local variables: | |
3171 | * compile-command: "gcc -g -traditional -I. -I.. -DTEST -o readline readline.c keymaps.o funmap.o history.o -ltermcap" | |
3172 | * end: | |
3173 | */ |