Release 2.33.1
[external/binutils.git] / readline / misc.c
1 /* misc.c -- miscellaneous bindable readline functions. */
2
3 /* Copyright (C) 1987-2017 Free Software Foundation, Inc.
4
5    This file is part of the GNU Readline Library (Readline), a library
6    for reading lines of text with interactive input and history editing.      
7
8    Readline is free software: you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation, either version 3 of the License, or
11    (at your option) any later version.
12
13    Readline is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with Readline.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #define READLINE_LIBRARY
23
24 #if defined (HAVE_CONFIG_H)
25 #  include <config.h>
26 #endif
27
28 #if defined (HAVE_UNISTD_H)
29 #  include <unistd.h>
30 #endif /* HAVE_UNISTD_H */
31
32 #if defined (HAVE_STDLIB_H)
33 #  include <stdlib.h>
34 #else
35 #  include "ansi_stdlib.h"
36 #endif /* HAVE_STDLIB_H */
37
38 #if defined (HAVE_LOCALE_H)
39 #  include <locale.h>
40 #endif
41
42 #include <stdio.h>
43
44 /* System-specific feature definitions and include files. */
45 #include "rldefs.h"
46 #include "rlmbutil.h"
47
48 /* Some standard library routines. */
49 #include "readline.h"
50 #include "history.h"
51
52 #include "rlprivate.h"
53 #include "rlshell.h"
54 #include "xmalloc.h"
55
56 static int rl_digit_loop PARAMS((void));
57 static void _rl_history_set_point PARAMS((void));
58
59 /* Forward declarations used in this file */
60 void _rl_free_history_entry PARAMS((HIST_ENTRY *));
61
62 /* If non-zero, rl_get_previous_history and rl_get_next_history attempt
63    to preserve the value of rl_point from line to line. */
64 int _rl_history_preserve_point = 0;
65
66 _rl_arg_cxt _rl_argcxt;
67
68 /* Saved target point for when _rl_history_preserve_point is set.  Special
69    value of -1 means that point is at the end of the line. */
70 int _rl_history_saved_point = -1;
71
72 /* **************************************************************** */
73 /*                                                                  */
74 /*                      Numeric Arguments                           */
75 /*                                                                  */
76 /* **************************************************************** */
77
78 int
79 _rl_arg_overflow (void)
80 {
81   if (rl_numeric_arg > 1000000)
82     {
83       _rl_argcxt = 0;
84       rl_explicit_arg = rl_numeric_arg = 0;
85       rl_ding ();
86       rl_restore_prompt ();
87       rl_clear_message ();
88       RL_UNSETSTATE(RL_STATE_NUMERICARG);
89       return 1;
90     }
91   return 0;
92 }
93
94 void
95 _rl_arg_init (void)
96 {
97   rl_save_prompt ();
98   _rl_argcxt = 0;
99   RL_SETSTATE(RL_STATE_NUMERICARG);
100 }
101
102 int
103 _rl_arg_getchar (void)
104 {
105   int c;
106
107   rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg);
108   RL_SETSTATE(RL_STATE_MOREINPUT);
109   c = rl_read_key ();
110   RL_UNSETSTATE(RL_STATE_MOREINPUT);
111
112   return c;
113 }
114
115 /* Process C as part of the current numeric argument.  Return -1 if the
116    argument should be aborted, 0 if we should not read any more chars, and
117    1 if we should continue to read chars. */
118 int
119 _rl_arg_dispatch (_rl_arg_cxt cxt, int c)
120 {
121   int key, r;
122
123   key = c;
124
125   /* If we see a key bound to `universal-argument' after seeing digits,
126       it ends the argument but is otherwise ignored. */
127   if (c >= 0 && _rl_keymap[c].type == ISFUNC && _rl_keymap[c].function == rl_universal_argument)
128     {
129       if ((cxt & NUM_SAWDIGITS) == 0)
130         {
131           rl_numeric_arg *= 4;
132           return 1;
133         }
134       else if (RL_ISSTATE (RL_STATE_CALLBACK))
135         {
136           _rl_argcxt |= NUM_READONE;
137           return 0;     /* XXX */
138         }
139       else
140         {
141           RL_SETSTATE(RL_STATE_MOREINPUT);
142           key = rl_read_key ();
143           RL_UNSETSTATE(RL_STATE_MOREINPUT);
144           rl_restore_prompt ();
145           rl_clear_message ();
146           RL_UNSETSTATE(RL_STATE_NUMERICARG);
147           if (key < 0)
148             return -1;
149           return (_rl_dispatch (key, _rl_keymap));
150         }
151     }
152
153   c = UNMETA (c);
154
155   if (_rl_digit_p (c))
156     {
157       r = _rl_digit_value (c);          
158       rl_numeric_arg = rl_explicit_arg ? (rl_numeric_arg * 10) +  r : r;
159       rl_explicit_arg = 1;
160       _rl_argcxt |= NUM_SAWDIGITS;
161     }
162   else if (c == '-' && rl_explicit_arg == 0)
163     {
164       rl_numeric_arg = 1;
165       _rl_argcxt |= NUM_SAWMINUS;
166       rl_arg_sign = -1;
167     }
168   else
169     {
170       /* Make M-- command equivalent to M--1 command. */
171       if ((_rl_argcxt & NUM_SAWMINUS) && rl_numeric_arg == 1 && rl_explicit_arg == 0)
172         rl_explicit_arg = 1;
173       rl_restore_prompt ();
174       rl_clear_message ();
175       RL_UNSETSTATE(RL_STATE_NUMERICARG);
176
177       r = _rl_dispatch (key, _rl_keymap);
178       if (RL_ISSTATE (RL_STATE_CALLBACK))
179         {
180           /* At worst, this will cause an extra redisplay.  Otherwise,
181              we have to wait until the next character comes in. */
182           if (rl_done == 0)
183             (*rl_redisplay_function) ();
184           r = 0;
185         }
186       return r;
187     }
188
189   return 1;
190 }
191
192 /* Handle C-u style numeric args, as well as M--, and M-digits. */
193 static int
194 rl_digit_loop (void)
195 {
196   int c, r;
197
198   while (1)
199     {
200       if (_rl_arg_overflow ())
201         return 1;
202
203       c = _rl_arg_getchar ();
204
205       if (c < 0)
206         {
207           _rl_abort_internal ();
208           return -1;
209         }
210
211       r = _rl_arg_dispatch (_rl_argcxt, c);
212       if (r <= 0 || (RL_ISSTATE (RL_STATE_NUMERICARG) == 0))
213         break;
214     }
215
216   return r;
217 }
218
219 /* Create a default argument. */
220 void
221 _rl_reset_argument (void)
222 {
223   rl_numeric_arg = rl_arg_sign = 1;
224   rl_explicit_arg = 0;
225   _rl_argcxt = 0;
226 }
227
228 /* Start a numeric argument with initial value KEY */
229 int
230 rl_digit_argument (int ignore, int key)
231 {
232   _rl_arg_init ();
233   if (RL_ISSTATE (RL_STATE_CALLBACK))
234     {
235       _rl_arg_dispatch (_rl_argcxt, key);
236       rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg);
237       return 0;
238     }
239   else
240     {
241       rl_execute_next (key);
242       return (rl_digit_loop ());
243     }
244 }
245
246 /* C-u, universal argument.  Multiply the current argument by 4.
247    Read a key.  If the key has nothing to do with arguments, then
248    dispatch on it.  If the key is the abort character then abort. */
249 int
250 rl_universal_argument (int count, int key)
251 {
252   _rl_arg_init ();
253   rl_numeric_arg *= 4;
254
255   return (RL_ISSTATE (RL_STATE_CALLBACK) ? 0 : rl_digit_loop ());
256 }
257
258 int
259 _rl_arg_callback (_rl_arg_cxt cxt)
260 {
261   int c, r;
262
263   c = _rl_arg_getchar ();
264   if (c < 0)
265     return (1);         /* EOF */
266
267   if (_rl_argcxt & NUM_READONE)
268     {
269       _rl_argcxt &= ~NUM_READONE;
270       rl_restore_prompt ();
271       rl_clear_message ();
272       RL_UNSETSTATE(RL_STATE_NUMERICARG);
273       rl_execute_next (c);
274       return 0;
275     }
276
277   r = _rl_arg_dispatch (cxt, c);
278   if (r > 0)
279     rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg);
280   return (r != 1);
281 }
282
283 /* What to do when you abort reading an argument. */
284 int
285 rl_discard_argument (void)
286 {
287   rl_ding ();
288   rl_clear_message ();
289   _rl_reset_argument ();
290
291   return 0;
292 }
293
294 /* **************************************************************** */
295 /*                                                                  */
296 /*                      History Utilities                           */
297 /*                                                                  */
298 /* **************************************************************** */
299
300 /* We already have a history library, and that is what we use to control
301    the history features of readline.  This is our local interface to
302    the history mechanism. */
303
304 /* While we are editing the history, this is the saved
305    version of the original line. */
306 HIST_ENTRY *_rl_saved_line_for_history = (HIST_ENTRY *)NULL;
307
308 /* Set the history pointer back to the last entry in the history. */
309 void
310 _rl_start_using_history (void)
311 {
312   using_history ();
313   if (_rl_saved_line_for_history)
314     _rl_free_history_entry (_rl_saved_line_for_history);
315
316   _rl_saved_line_for_history = (HIST_ENTRY *)NULL;
317 }
318
319 /* Free the contents (and containing structure) of a HIST_ENTRY. */
320 void
321 _rl_free_history_entry (HIST_ENTRY *entry)
322 {
323   if (entry == 0)
324     return;
325
326   FREE (entry->line);
327   FREE (entry->timestamp);
328
329   xfree (entry);
330 }
331
332 /* Perhaps put back the current line if it has changed. */
333 int
334 rl_maybe_replace_line (void)
335 {
336   HIST_ENTRY *temp;
337
338   temp = current_history ();
339   /* If the current line has changed, save the changes. */
340   if (temp && ((UNDO_LIST *)(temp->data) != rl_undo_list))
341     {
342       temp = replace_history_entry (where_history (), rl_line_buffer, (histdata_t)rl_undo_list);
343       xfree (temp->line);
344       FREE (temp->timestamp);
345       xfree (temp);
346     }
347   return 0;
348 }
349
350 /* Restore the _rl_saved_line_for_history if there is one. */
351 int
352 rl_maybe_unsave_line (void)
353 {
354   if (_rl_saved_line_for_history)
355     {
356       /* Can't call with `1' because rl_undo_list might point to an undo
357          list from a history entry, as in rl_replace_from_history() below. */
358       rl_replace_line (_rl_saved_line_for_history->line, 0);
359       rl_undo_list = (UNDO_LIST *)_rl_saved_line_for_history->data;
360       _rl_free_history_entry (_rl_saved_line_for_history);
361       _rl_saved_line_for_history = (HIST_ENTRY *)NULL;
362       rl_point = rl_end;        /* rl_replace_line sets rl_end */
363     }
364   else
365     rl_ding ();
366   return 0;
367 }
368
369 /* Save the current line in _rl_saved_line_for_history. */
370 int
371 rl_maybe_save_line (void)
372 {
373   if (_rl_saved_line_for_history == 0)
374     {
375       _rl_saved_line_for_history = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
376       _rl_saved_line_for_history->line = savestring (rl_line_buffer);
377       _rl_saved_line_for_history->timestamp = (char *)NULL;
378       _rl_saved_line_for_history->data = (char *)rl_undo_list;
379     }
380
381   return 0;
382 }
383
384 int
385 _rl_free_saved_history_line (void)
386 {
387   if (_rl_saved_line_for_history)
388     {
389       _rl_free_history_entry (_rl_saved_line_for_history);
390       _rl_saved_line_for_history = (HIST_ENTRY *)NULL;
391     }
392   return 0;
393 }
394
395 static void
396 _rl_history_set_point (void)
397 {
398   rl_point = (_rl_history_preserve_point && _rl_history_saved_point != -1)
399                 ? _rl_history_saved_point
400                 : rl_end;
401   if (rl_point > rl_end)
402     rl_point = rl_end;
403
404 #if defined (VI_MODE)
405   if (rl_editing_mode == vi_mode && _rl_keymap != vi_insertion_keymap)
406     rl_point = 0;
407 #endif /* VI_MODE */
408
409   if (rl_editing_mode == emacs_mode)
410     rl_mark = (rl_point == rl_end ? 0 : rl_end);
411 }
412
413 void
414 rl_replace_from_history (HIST_ENTRY *entry, int flags)
415 {
416   /* Can't call with `1' because rl_undo_list might point to an undo list
417      from a history entry, just like we're setting up here. */
418   rl_replace_line (entry->line, 0);
419   rl_undo_list = (UNDO_LIST *)entry->data;
420   rl_point = rl_end;
421   rl_mark = 0;
422
423 #if defined (VI_MODE)
424   if (rl_editing_mode == vi_mode)
425     {
426       rl_point = 0;
427       rl_mark = rl_end;
428     }
429 #endif
430 }
431
432 /* Process and free undo lists attached to each history entry prior to the
433    current entry, inclusive, reverting each line to its saved state.  This 
434    is destructive, and state about the current line is lost.  This is not
435    intended to be called while actively editing, and the current line is
436    not assumed to have been added to the history list. */
437 void
438 _rl_revert_all_lines (void)
439 {
440   int hpos;
441   HIST_ENTRY *entry;
442   UNDO_LIST *ul, *saved_undo_list;
443   char *lbuf;
444
445   lbuf = savestring (rl_line_buffer);
446   saved_undo_list = rl_undo_list;
447   hpos = where_history ();
448
449   entry = (hpos == history_length) ? previous_history () : current_history ();
450   while (entry)
451     {
452       if (ul = (UNDO_LIST *)entry->data)
453         {
454           if (ul == saved_undo_list)
455             saved_undo_list = 0;
456           /* Set up rl_line_buffer and other variables from history entry */
457           rl_replace_from_history (entry, 0);   /* entry->line is now current */
458           entry->data = 0;                      /* entry->data is now current undo list */
459           /* Undo all changes to this history entry */
460           while (rl_undo_list)
461             rl_do_undo ();
462           /* And copy the reverted line back to the history entry, preserving
463              the timestamp. */
464           FREE (entry->line);
465           entry->line = savestring (rl_line_buffer);
466         }
467       entry = previous_history ();
468     }
469
470   /* Restore history state */
471   rl_undo_list = saved_undo_list;       /* may have been set to null */
472   history_set_pos (hpos);
473   
474   /* reset the line buffer */
475   rl_replace_line (lbuf, 0);
476   _rl_set_the_line ();
477
478   /* and clean up */
479   xfree (lbuf);
480 }  
481
482 /* Free the history list, including private readline data and take care
483    of pointer aliases to history data.  Resets rl_undo_list if it points
484    to an UNDO_LIST * saved as some history entry's data member.  This
485    should not be called while editing is active. */
486 void
487 rl_clear_history (void)
488 {
489   HIST_ENTRY **hlist, *hent;
490   register int i;
491   UNDO_LIST *ul, *saved_undo_list;
492
493   saved_undo_list = rl_undo_list;
494   hlist = history_list ();              /* direct pointer, not copy */
495
496   for (i = 0; i < history_length; i++)
497     {
498       hent = hlist[i];
499       if (ul = (UNDO_LIST *)hent->data)
500         {
501           if (ul == saved_undo_list)
502             saved_undo_list = 0;
503           _rl_free_undo_list (ul);
504           hent->data = 0;
505         }
506       _rl_free_history_entry (hent);
507     }
508
509   history_offset = history_length = 0;
510   rl_undo_list = saved_undo_list;       /* should be NULL */
511 }
512
513 /* **************************************************************** */
514 /*                                                                  */
515 /*                      History Commands                            */
516 /*                                                                  */
517 /* **************************************************************** */
518
519 /* Meta-< goes to the start of the history. */
520 int
521 rl_beginning_of_history (int count, int key)
522 {
523   return (rl_get_previous_history (1 + where_history (), key));
524 }
525
526 /* Meta-> goes to the end of the history.  (The current line). */
527 int
528 rl_end_of_history (int count, int key)
529 {
530   rl_maybe_replace_line ();
531   using_history ();
532   rl_maybe_unsave_line ();
533   return 0;
534 }
535
536 /* Move down to the next history line. */
537 int
538 rl_get_next_history (int count, int key)
539 {
540   HIST_ENTRY *temp;
541
542   if (count < 0)
543     return (rl_get_previous_history (-count, key));
544
545   if (count == 0)
546     return 0;
547
548   rl_maybe_replace_line ();
549
550   /* either not saved by rl_newline or at end of line, so set appropriately. */
551   if (_rl_history_saved_point == -1 && (rl_point || rl_end))
552     _rl_history_saved_point = (rl_point == rl_end) ? -1 : rl_point;
553
554   temp = (HIST_ENTRY *)NULL;
555   while (count)
556     {
557       temp = next_history ();
558       if (!temp)
559         break;
560       --count;
561     }
562
563   if (temp == 0)
564     rl_maybe_unsave_line ();
565   else
566     {
567       rl_replace_from_history (temp, 0);
568       _rl_history_set_point ();
569     }
570   return 0;
571 }
572
573 /* Get the previous item out of our interactive history, making it the current
574    line.  If there is no previous history, just ding. */
575 int
576 rl_get_previous_history (int count, int key)
577 {
578   HIST_ENTRY *old_temp, *temp;
579
580   if (count < 0)
581     return (rl_get_next_history (-count, key));
582
583   if (count == 0 || history_list () == 0)
584     return 0;
585
586   /* either not saved by rl_newline or at end of line, so set appropriately. */
587   if (_rl_history_saved_point == -1 && (rl_point || rl_end))
588     _rl_history_saved_point = (rl_point == rl_end) ? -1 : rl_point;
589
590   /* If we don't have a line saved, then save this one. */
591   rl_maybe_save_line ();
592
593   /* If the current line has changed, save the changes. */
594   rl_maybe_replace_line ();
595
596   temp = old_temp = (HIST_ENTRY *)NULL;
597   while (count)
598     {
599       temp = previous_history ();
600       if (temp == 0)
601         break;
602
603       old_temp = temp;
604       --count;
605     }
606
607   /* If there was a large argument, and we moved back to the start of the
608      history, that is not an error.  So use the last value found. */
609   if (!temp && old_temp)
610     temp = old_temp;
611
612   if (temp == 0)
613     {
614       rl_maybe_unsave_line ();
615       rl_ding ();
616     }
617   else
618     {
619       rl_replace_from_history (temp, 0);
620       _rl_history_set_point ();
621     }
622
623   return 0;
624 }
625
626 /* **************************************************************** */
627 /*                                                                  */
628 /*                          Editing Modes                           */
629 /*                                                                  */
630 /* **************************************************************** */
631 /* How to toggle back and forth between editing modes. */
632 int
633 rl_vi_editing_mode (int count, int key)
634 {
635 #if defined (VI_MODE)
636   _rl_set_insert_mode (RL_IM_INSERT, 1);        /* vi mode ignores insert mode */
637   rl_editing_mode = vi_mode;
638   rl_vi_insert_mode (1, key);
639 #endif /* VI_MODE */
640
641   return 0;
642 }
643
644 int
645 rl_emacs_editing_mode (int count, int key)
646 {
647   rl_editing_mode = emacs_mode;
648   _rl_set_insert_mode (RL_IM_INSERT, 1); /* emacs mode default is insert mode */
649   _rl_keymap = emacs_standard_keymap;
650
651   if (_rl_show_mode_in_prompt)
652     _rl_reset_prompt ();
653
654   return 0;
655 }
656
657 /* Function for the rest of the library to use to set insert/overwrite mode. */
658 void
659 _rl_set_insert_mode (int im, int force)
660 {
661 #ifdef CURSOR_MODE
662   _rl_set_cursor (im, force);
663 #endif
664
665   rl_insert_mode = im;
666 }
667
668 /* Toggle overwrite mode.  A positive explicit argument selects overwrite
669    mode.  A negative or zero explicit argument selects insert mode. */
670 int
671 rl_overwrite_mode (int count, int key)
672 {
673   if (rl_explicit_arg == 0)
674     _rl_set_insert_mode (rl_insert_mode ^ 1, 0);
675   else if (count > 0)
676     _rl_set_insert_mode (RL_IM_OVERWRITE, 0);
677   else
678     _rl_set_insert_mode (RL_IM_INSERT, 0);
679
680   return 0;
681 }