libtommath: Fix possible integer overflow CVE-2023-36328
[heimdal.git] / lib / libedit / src / vi.c
blob0c37bfb9b2ec8ec0703e5db62f6413156c1883d8
1 /* $NetBSD: vi.c,v 1.62 2016/05/09 21:46:56 christos Exp $ */
3 /*-
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Christos Zoulas of Cornell University.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
35 #include "config.h"
36 #if !defined(lint) && !defined(SCCSID)
37 #if 0
38 static char sccsid[] = "@(#)vi.c 8.1 (Berkeley) 6/4/93";
39 #else
40 __RCSID("$NetBSD: vi.c,v 1.62 2016/05/09 21:46:56 christos Exp $");
41 #endif
42 #endif /* not lint && not SCCSID */
45 * vi.c: Vi mode commands.
47 #include <sys/wait.h>
48 #include <ctype.h>
49 #include <limits.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
54 #include "el.h"
55 #include "common.h"
56 #include "emacs.h"
57 #include "fcns.h"
58 #include "vi.h"
60 static el_action_t cv_action(EditLine *, wint_t);
61 static el_action_t cv_paste(EditLine *, wint_t);
63 /* cv_action():
64 * Handle vi actions.
66 static el_action_t
67 cv_action(EditLine *el, wint_t c)
70 if (el->el_chared.c_vcmd.action != NOP) {
71 /* 'cc', 'dd' and (possibly) friends */
72 if (c != (wint_t)el->el_chared.c_vcmd.action)
73 return CC_ERROR;
75 if (!(c & YANK))
76 cv_undo(el);
77 cv_yank(el, el->el_line.buffer,
78 (int)(el->el_line.lastchar - el->el_line.buffer));
79 el->el_chared.c_vcmd.action = NOP;
80 el->el_chared.c_vcmd.pos = 0;
81 if (!(c & YANK)) {
82 el->el_line.lastchar = el->el_line.buffer;
83 el->el_line.cursor = el->el_line.buffer;
85 if (c & INSERT)
86 el->el_map.current = el->el_map.key;
88 return CC_REFRESH;
90 el->el_chared.c_vcmd.pos = el->el_line.cursor;
91 el->el_chared.c_vcmd.action = c;
92 return CC_ARGHACK;
95 /* cv_paste():
96 * Paste previous deletion before or after the cursor
98 static el_action_t
99 cv_paste(EditLine *el, wint_t c)
101 c_kill_t *k = &el->el_chared.c_kill;
102 size_t len = (size_t)(k->last - k->buf);
104 if (k->buf == NULL || len == 0)
105 return CC_ERROR;
106 #ifdef DEBUG_PASTE
107 (void) fprintf(el->el_errfile, "Paste: \"%.*ls\"\n", (int)len,
108 k->buf);
109 #endif
111 cv_undo(el);
113 if (!c && el->el_line.cursor < el->el_line.lastchar)
114 el->el_line.cursor++;
116 c_insert(el, (int)len);
117 if (el->el_line.cursor + len > el->el_line.lastchar)
118 return CC_ERROR;
119 (void) memcpy(el->el_line.cursor, k->buf, len *
120 sizeof(*el->el_line.cursor));
122 return CC_REFRESH;
126 /* vi_paste_next():
127 * Vi paste previous deletion to the right of the cursor
128 * [p]
130 libedit_private el_action_t
131 /*ARGSUSED*/
132 vi_paste_next(EditLine *el, wint_t c __attribute__((__unused__)))
135 return cv_paste(el, 0);
139 /* vi_paste_prev():
140 * Vi paste previous deletion to the left of the cursor
141 * [P]
143 libedit_private el_action_t
144 /*ARGSUSED*/
145 vi_paste_prev(EditLine *el, wint_t c __attribute__((__unused__)))
148 return cv_paste(el, 1);
152 /* vi_prev_big_word():
153 * Vi move to the previous space delimited word
154 * [B]
156 libedit_private el_action_t
157 /*ARGSUSED*/
158 vi_prev_big_word(EditLine *el, wint_t c __attribute__((__unused__)))
161 if (el->el_line.cursor == el->el_line.buffer)
162 return CC_ERROR;
164 el->el_line.cursor = cv_prev_word(el->el_line.cursor,
165 el->el_line.buffer,
166 el->el_state.argument,
167 cv__isWord);
169 if (el->el_chared.c_vcmd.action != NOP) {
170 cv_delfini(el);
171 return CC_REFRESH;
173 return CC_CURSOR;
177 /* vi_prev_word():
178 * Vi move to the previous word
179 * [b]
181 libedit_private el_action_t
182 /*ARGSUSED*/
183 vi_prev_word(EditLine *el, wint_t c __attribute__((__unused__)))
186 if (el->el_line.cursor == el->el_line.buffer)
187 return CC_ERROR;
189 el->el_line.cursor = cv_prev_word(el->el_line.cursor,
190 el->el_line.buffer,
191 el->el_state.argument,
192 cv__isword);
194 if (el->el_chared.c_vcmd.action != NOP) {
195 cv_delfini(el);
196 return CC_REFRESH;
198 return CC_CURSOR;
202 /* vi_next_big_word():
203 * Vi move to the next space delimited word
204 * [W]
206 libedit_private el_action_t
207 /*ARGSUSED*/
208 vi_next_big_word(EditLine *el, wint_t c __attribute__((__unused__)))
211 if (el->el_line.cursor >= el->el_line.lastchar - 1)
212 return CC_ERROR;
214 el->el_line.cursor = cv_next_word(el, el->el_line.cursor,
215 el->el_line.lastchar, el->el_state.argument, cv__isWord);
217 if (el->el_map.type == MAP_VI)
218 if (el->el_chared.c_vcmd.action != NOP) {
219 cv_delfini(el);
220 return CC_REFRESH;
222 return CC_CURSOR;
226 /* vi_next_word():
227 * Vi move to the next word
228 * [w]
230 libedit_private el_action_t
231 /*ARGSUSED*/
232 vi_next_word(EditLine *el, wint_t c __attribute__((__unused__)))
235 if (el->el_line.cursor >= el->el_line.lastchar - 1)
236 return CC_ERROR;
238 el->el_line.cursor = cv_next_word(el, el->el_line.cursor,
239 el->el_line.lastchar, el->el_state.argument, cv__isword);
241 if (el->el_map.type == MAP_VI)
242 if (el->el_chared.c_vcmd.action != NOP) {
243 cv_delfini(el);
244 return CC_REFRESH;
246 return CC_CURSOR;
250 /* vi_change_case():
251 * Vi change case of character under the cursor and advance one character
252 * [~]
254 libedit_private el_action_t
255 vi_change_case(EditLine *el, wint_t c)
257 int i;
259 if (el->el_line.cursor >= el->el_line.lastchar)
260 return CC_ERROR;
261 cv_undo(el);
262 for (i = 0; i < el->el_state.argument; i++) {
264 c = *el->el_line.cursor;
265 if (iswupper(c))
266 *el->el_line.cursor = towlower(c);
267 else if (iswlower(c))
268 *el->el_line.cursor = towupper(c);
270 if (++el->el_line.cursor >= el->el_line.lastchar) {
271 el->el_line.cursor--;
272 re_fastaddc(el);
273 break;
275 re_fastaddc(el);
277 return CC_NORM;
281 /* vi_change_meta():
282 * Vi change prefix command
283 * [c]
285 libedit_private el_action_t
286 /*ARGSUSED*/
287 vi_change_meta(EditLine *el, wint_t c __attribute__((__unused__)))
291 * Delete with insert == change: first we delete and then we leave in
292 * insert mode.
294 return cv_action(el, DELETE | INSERT);
298 /* vi_insert_at_bol():
299 * Vi enter insert mode at the beginning of line
300 * [I]
302 libedit_private el_action_t
303 /*ARGSUSED*/
304 vi_insert_at_bol(EditLine *el, wint_t c __attribute__((__unused__)))
307 el->el_line.cursor = el->el_line.buffer;
308 cv_undo(el);
309 el->el_map.current = el->el_map.key;
310 return CC_CURSOR;
314 /* vi_replace_char():
315 * Vi replace character under the cursor with the next character typed
316 * [r]
318 libedit_private el_action_t
319 /*ARGSUSED*/
320 vi_replace_char(EditLine *el, wint_t c __attribute__((__unused__)))
323 if (el->el_line.cursor >= el->el_line.lastchar)
324 return CC_ERROR;
326 el->el_map.current = el->el_map.key;
327 el->el_state.inputmode = MODE_REPLACE_1;
328 cv_undo(el);
329 return CC_ARGHACK;
333 /* vi_replace_mode():
334 * Vi enter replace mode
335 * [R]
337 libedit_private el_action_t
338 /*ARGSUSED*/
339 vi_replace_mode(EditLine *el, wint_t c __attribute__((__unused__)))
342 el->el_map.current = el->el_map.key;
343 el->el_state.inputmode = MODE_REPLACE;
344 cv_undo(el);
345 return CC_NORM;
349 /* vi_substitute_char():
350 * Vi replace character under the cursor and enter insert mode
351 * [s]
353 libedit_private el_action_t
354 /*ARGSUSED*/
355 vi_substitute_char(EditLine *el, wint_t c __attribute__((__unused__)))
358 c_delafter(el, el->el_state.argument);
359 el->el_map.current = el->el_map.key;
360 return CC_REFRESH;
364 /* vi_substitute_line():
365 * Vi substitute entire line
366 * [S]
368 libedit_private el_action_t
369 /*ARGSUSED*/
370 vi_substitute_line(EditLine *el, wint_t c __attribute__((__unused__)))
373 cv_undo(el);
374 cv_yank(el, el->el_line.buffer,
375 (int)(el->el_line.lastchar - el->el_line.buffer));
376 (void) em_kill_line(el, 0);
377 el->el_map.current = el->el_map.key;
378 return CC_REFRESH;
382 /* vi_change_to_eol():
383 * Vi change to end of line
384 * [C]
386 libedit_private el_action_t
387 /*ARGSUSED*/
388 vi_change_to_eol(EditLine *el, wint_t c __attribute__((__unused__)))
391 cv_undo(el);
392 cv_yank(el, el->el_line.cursor,
393 (int)(el->el_line.lastchar - el->el_line.cursor));
394 (void) ed_kill_line(el, 0);
395 el->el_map.current = el->el_map.key;
396 return CC_REFRESH;
400 /* vi_insert():
401 * Vi enter insert mode
402 * [i]
404 libedit_private el_action_t
405 /*ARGSUSED*/
406 vi_insert(EditLine *el, wint_t c __attribute__((__unused__)))
409 el->el_map.current = el->el_map.key;
410 cv_undo(el);
411 return CC_NORM;
415 /* vi_add():
416 * Vi enter insert mode after the cursor
417 * [a]
419 libedit_private el_action_t
420 /*ARGSUSED*/
421 vi_add(EditLine *el, wint_t c __attribute__((__unused__)))
423 int ret;
425 el->el_map.current = el->el_map.key;
426 if (el->el_line.cursor < el->el_line.lastchar) {
427 el->el_line.cursor++;
428 if (el->el_line.cursor > el->el_line.lastchar)
429 el->el_line.cursor = el->el_line.lastchar;
430 ret = CC_CURSOR;
431 } else
432 ret = CC_NORM;
434 cv_undo(el);
436 return (el_action_t)ret;
440 /* vi_add_at_eol():
441 * Vi enter insert mode at end of line
442 * [A]
444 libedit_private el_action_t
445 /*ARGSUSED*/
446 vi_add_at_eol(EditLine *el, wint_t c __attribute__((__unused__)))
449 el->el_map.current = el->el_map.key;
450 el->el_line.cursor = el->el_line.lastchar;
451 cv_undo(el);
452 return CC_CURSOR;
456 /* vi_delete_meta():
457 * Vi delete prefix command
458 * [d]
460 libedit_private el_action_t
461 /*ARGSUSED*/
462 vi_delete_meta(EditLine *el, wint_t c __attribute__((__unused__)))
465 return cv_action(el, DELETE);
469 /* vi_end_big_word():
470 * Vi move to the end of the current space delimited word
471 * [E]
473 libedit_private el_action_t
474 /*ARGSUSED*/
475 vi_end_big_word(EditLine *el, wint_t c __attribute__((__unused__)))
478 if (el->el_line.cursor == el->el_line.lastchar)
479 return CC_ERROR;
481 el->el_line.cursor = cv__endword(el->el_line.cursor,
482 el->el_line.lastchar, el->el_state.argument, cv__isWord);
484 if (el->el_chared.c_vcmd.action != NOP) {
485 el->el_line.cursor++;
486 cv_delfini(el);
487 return CC_REFRESH;
489 return CC_CURSOR;
493 /* vi_end_word():
494 * Vi move to the end of the current word
495 * [e]
497 libedit_private el_action_t
498 /*ARGSUSED*/
499 vi_end_word(EditLine *el, wint_t c __attribute__((__unused__)))
502 if (el->el_line.cursor == el->el_line.lastchar)
503 return CC_ERROR;
505 el->el_line.cursor = cv__endword(el->el_line.cursor,
506 el->el_line.lastchar, el->el_state.argument, cv__isword);
508 if (el->el_chared.c_vcmd.action != NOP) {
509 el->el_line.cursor++;
510 cv_delfini(el);
511 return CC_REFRESH;
513 return CC_CURSOR;
517 /* vi_undo():
518 * Vi undo last change
519 * [u]
521 libedit_private el_action_t
522 /*ARGSUSED*/
523 vi_undo(EditLine *el, wint_t c __attribute__((__unused__)))
525 c_undo_t un = el->el_chared.c_undo;
527 if (un.len == -1)
528 return CC_ERROR;
530 /* switch line buffer and undo buffer */
531 el->el_chared.c_undo.buf = el->el_line.buffer;
532 el->el_chared.c_undo.len = el->el_line.lastchar - el->el_line.buffer;
533 el->el_chared.c_undo.cursor =
534 (int)(el->el_line.cursor - el->el_line.buffer);
535 el->el_line.limit = un.buf + (el->el_line.limit - el->el_line.buffer);
536 el->el_line.buffer = un.buf;
537 el->el_line.cursor = un.buf + un.cursor;
538 el->el_line.lastchar = un.buf + un.len;
540 return CC_REFRESH;
544 /* vi_command_mode():
545 * Vi enter command mode (use alternative key bindings)
546 * [<ESC>]
548 libedit_private el_action_t
549 /*ARGSUSED*/
550 vi_command_mode(EditLine *el, wint_t c __attribute__((__unused__)))
553 /* [Esc] cancels pending action */
554 el->el_chared.c_vcmd.action = NOP;
555 el->el_chared.c_vcmd.pos = 0;
557 el->el_state.doingarg = 0;
559 el->el_state.inputmode = MODE_INSERT;
560 el->el_map.current = el->el_map.alt;
561 #ifdef VI_MOVE
562 if (el->el_line.cursor > el->el_line.buffer)
563 el->el_line.cursor--;
564 #endif
565 return CC_CURSOR;
569 /* vi_zero():
570 * Vi move to the beginning of line
571 * [0]
573 libedit_private el_action_t
574 vi_zero(EditLine *el, wint_t c)
577 if (el->el_state.doingarg)
578 return ed_argument_digit(el, c);
580 el->el_line.cursor = el->el_line.buffer;
581 if (el->el_chared.c_vcmd.action != NOP) {
582 cv_delfini(el);
583 return CC_REFRESH;
585 return CC_CURSOR;
589 /* vi_delete_prev_char():
590 * Vi move to previous character (backspace)
591 * [^H] in insert mode only
593 libedit_private el_action_t
594 /*ARGSUSED*/
595 vi_delete_prev_char(EditLine *el, wint_t c __attribute__((__unused__)))
598 if (el->el_line.cursor <= el->el_line.buffer)
599 return CC_ERROR;
601 c_delbefore1(el);
602 el->el_line.cursor--;
603 return CC_REFRESH;
607 /* vi_list_or_eof():
608 * Vi list choices for completion or indicate end of file if empty line
609 * [^D]
611 libedit_private el_action_t
612 /*ARGSUSED*/
613 vi_list_or_eof(EditLine *el, wint_t c)
616 if (el->el_line.cursor == el->el_line.lastchar) {
617 if (el->el_line.cursor == el->el_line.buffer) {
618 terminal_writec(el, c); /* then do a EOF */
619 return CC_EOF;
620 } else {
622 * Here we could list completions, but it is an
623 * error right now
625 terminal_beep(el);
626 return CC_ERROR;
628 } else {
629 #ifdef notyet
630 re_goto_bottom(el);
631 *el->el_line.lastchar = '\0'; /* just in case */
632 return CC_LIST_CHOICES;
633 #else
635 * Just complain for now.
637 terminal_beep(el);
638 return CC_ERROR;
639 #endif
644 /* vi_kill_line_prev():
645 * Vi cut from beginning of line to cursor
646 * [^U]
648 libedit_private el_action_t
649 /*ARGSUSED*/
650 vi_kill_line_prev(EditLine *el, wint_t c __attribute__((__unused__)))
652 wchar_t *kp, *cp;
654 cp = el->el_line.buffer;
655 kp = el->el_chared.c_kill.buf;
656 while (cp < el->el_line.cursor)
657 *kp++ = *cp++; /* copy it */
658 el->el_chared.c_kill.last = kp;
659 c_delbefore(el, (int)(el->el_line.cursor - el->el_line.buffer));
660 el->el_line.cursor = el->el_line.buffer; /* zap! */
661 return CC_REFRESH;
665 /* vi_search_prev():
666 * Vi search history previous
667 * [?]
669 libedit_private el_action_t
670 /*ARGSUSED*/
671 vi_search_prev(EditLine *el, wint_t c __attribute__((__unused__)))
674 return cv_search(el, ED_SEARCH_PREV_HISTORY);
678 /* vi_search_next():
679 * Vi search history next
680 * [/]
682 libedit_private el_action_t
683 /*ARGSUSED*/
684 vi_search_next(EditLine *el, wint_t c __attribute__((__unused__)))
687 return cv_search(el, ED_SEARCH_NEXT_HISTORY);
691 /* vi_repeat_search_next():
692 * Vi repeat current search in the same search direction
693 * [n]
695 libedit_private el_action_t
696 /*ARGSUSED*/
697 vi_repeat_search_next(EditLine *el, wint_t c __attribute__((__unused__)))
700 if (el->el_search.patlen == 0)
701 return CC_ERROR;
702 else
703 return cv_repeat_srch(el, el->el_search.patdir);
707 /* vi_repeat_search_prev():
708 * Vi repeat current search in the opposite search direction
709 * [N]
711 /*ARGSUSED*/
712 libedit_private el_action_t
713 vi_repeat_search_prev(EditLine *el, wint_t c __attribute__((__unused__)))
716 if (el->el_search.patlen == 0)
717 return CC_ERROR;
718 else
719 return (cv_repeat_srch(el,
720 el->el_search.patdir == ED_SEARCH_PREV_HISTORY ?
721 ED_SEARCH_NEXT_HISTORY : ED_SEARCH_PREV_HISTORY));
725 /* vi_next_char():
726 * Vi move to the character specified next
727 * [f]
729 libedit_private el_action_t
730 /*ARGSUSED*/
731 vi_next_char(EditLine *el, wint_t c __attribute__((__unused__)))
733 return cv_csearch(el, CHAR_FWD, -1, el->el_state.argument, 0);
737 /* vi_prev_char():
738 * Vi move to the character specified previous
739 * [F]
741 libedit_private el_action_t
742 /*ARGSUSED*/
743 vi_prev_char(EditLine *el, wint_t c __attribute__((__unused__)))
745 return cv_csearch(el, CHAR_BACK, -1, el->el_state.argument, 0);
749 /* vi_to_next_char():
750 * Vi move up to the character specified next
751 * [t]
753 libedit_private el_action_t
754 /*ARGSUSED*/
755 vi_to_next_char(EditLine *el, wint_t c __attribute__((__unused__)))
757 return cv_csearch(el, CHAR_FWD, -1, el->el_state.argument, 1);
761 /* vi_to_prev_char():
762 * Vi move up to the character specified previous
763 * [T]
765 libedit_private el_action_t
766 /*ARGSUSED*/
767 vi_to_prev_char(EditLine *el, wint_t c __attribute__((__unused__)))
769 return cv_csearch(el, CHAR_BACK, -1, el->el_state.argument, 1);
773 /* vi_repeat_next_char():
774 * Vi repeat current character search in the same search direction
775 * [;]
777 libedit_private el_action_t
778 /*ARGSUSED*/
779 vi_repeat_next_char(EditLine *el, wint_t c __attribute__((__unused__)))
782 return cv_csearch(el, el->el_search.chadir, el->el_search.chacha,
783 el->el_state.argument, el->el_search.chatflg);
787 /* vi_repeat_prev_char():
788 * Vi repeat current character search in the opposite search direction
789 * [,]
791 libedit_private el_action_t
792 /*ARGSUSED*/
793 vi_repeat_prev_char(EditLine *el, wint_t c __attribute__((__unused__)))
795 el_action_t r;
796 int dir = el->el_search.chadir;
798 r = cv_csearch(el, -dir, el->el_search.chacha,
799 el->el_state.argument, el->el_search.chatflg);
800 el->el_search.chadir = dir;
801 return r;
805 /* vi_match():
806 * Vi go to matching () {} or []
807 * [%]
809 libedit_private el_action_t
810 /*ARGSUSED*/
811 vi_match(EditLine *el, wint_t c __attribute__((__unused__)))
813 const wchar_t match_chars[] = L"()[]{}";
814 wchar_t *cp;
815 size_t delta, i, count;
816 wchar_t o_ch, c_ch;
818 *el->el_line.lastchar = '\0'; /* just in case */
820 i = wcscspn(el->el_line.cursor, match_chars);
821 o_ch = el->el_line.cursor[i];
822 if (o_ch == 0)
823 return CC_ERROR;
824 delta = (size_t)(wcschr(match_chars, o_ch) - match_chars);
825 c_ch = match_chars[delta ^ 1];
826 count = 1;
827 delta = 1 - (delta & 1) * 2;
829 for (cp = &el->el_line.cursor[i]; count; ) {
830 cp += delta;
831 if (cp < el->el_line.buffer || cp >= el->el_line.lastchar)
832 return CC_ERROR;
833 if (*cp == o_ch)
834 count++;
835 else if (*cp == c_ch)
836 count--;
839 el->el_line.cursor = cp;
841 if (el->el_chared.c_vcmd.action != NOP) {
842 /* NB posix says char under cursor should NOT be deleted
843 for -ve delta - this is different to netbsd vi. */
844 if (delta > 0)
845 el->el_line.cursor++;
846 cv_delfini(el);
847 return CC_REFRESH;
849 return CC_CURSOR;
852 /* vi_undo_line():
853 * Vi undo all changes to line
854 * [U]
856 libedit_private el_action_t
857 /*ARGSUSED*/
858 vi_undo_line(EditLine *el, wint_t c __attribute__((__unused__)))
861 cv_undo(el);
862 return hist_get(el);
865 /* vi_to_column():
866 * Vi go to specified column
867 * [|]
868 * NB netbsd vi goes to screen column 'n', posix says nth character
870 libedit_private el_action_t
871 /*ARGSUSED*/
872 vi_to_column(EditLine *el, wint_t c __attribute__((__unused__)))
875 el->el_line.cursor = el->el_line.buffer;
876 el->el_state.argument--;
877 return ed_next_char(el, 0);
880 /* vi_yank_end():
881 * Vi yank to end of line
882 * [Y]
884 libedit_private el_action_t
885 /*ARGSUSED*/
886 vi_yank_end(EditLine *el, wint_t c __attribute__((__unused__)))
889 cv_yank(el, el->el_line.cursor,
890 (int)(el->el_line.lastchar - el->el_line.cursor));
891 return CC_REFRESH;
894 /* vi_yank():
895 * Vi yank
896 * [y]
898 libedit_private el_action_t
899 /*ARGSUSED*/
900 vi_yank(EditLine *el, wint_t c __attribute__((__unused__)))
903 return cv_action(el, YANK);
906 /* vi_comment_out():
907 * Vi comment out current command
908 * [#]
910 libedit_private el_action_t
911 /*ARGSUSED*/
912 vi_comment_out(EditLine *el, wint_t c __attribute__((__unused__)))
915 el->el_line.cursor = el->el_line.buffer;
916 c_insert(el, 1);
917 *el->el_line.cursor = '#';
918 re_refresh(el);
919 return ed_newline(el, 0);
922 /* vi_alias():
923 * Vi include shell alias
924 * [@]
925 * NB: posix implies that we should enter insert mode, however
926 * this is against historical precedent...
928 libedit_private el_action_t
929 /*ARGSUSED*/
930 vi_alias(EditLine *el, wint_t c __attribute__((__unused__)))
932 char alias_name[3];
933 const char *alias_text;
935 if (el->el_chared.c_aliasfun == NULL)
936 return CC_ERROR;
938 alias_name[0] = '_';
939 alias_name[2] = 0;
940 if (el_getc(el, &alias_name[1]) != 1)
941 return CC_ERROR;
943 alias_text = (*el->el_chared.c_aliasfun)(el->el_chared.c_aliasarg,
944 alias_name);
945 if (alias_text != NULL)
946 el_wpush(el, ct_decode_string(alias_text, &el->el_scratch));
947 return CC_NORM;
950 /* vi_to_history_line():
951 * Vi go to specified history file line.
952 * [G]
954 libedit_private el_action_t
955 /*ARGSUSED*/
956 vi_to_history_line(EditLine *el, wint_t c __attribute__((__unused__)))
958 int sv_event_no = el->el_history.eventno;
959 el_action_t rval;
962 if (el->el_history.eventno == 0) {
963 (void) wcsncpy(el->el_history.buf, el->el_line.buffer,
964 EL_BUFSIZ);
965 el->el_history.last = el->el_history.buf +
966 (el->el_line.lastchar - el->el_line.buffer);
969 /* Lack of a 'count' means oldest, not 1 */
970 if (!el->el_state.doingarg) {
971 el->el_history.eventno = 0x7fffffff;
972 hist_get(el);
973 } else {
974 /* This is brain dead, all the rest of this code counts
975 * upwards going into the past. Here we need count in the
976 * other direction (to match the output of fc -l).
977 * I could change the world, but this seems to suffice.
979 el->el_history.eventno = 1;
980 if (hist_get(el) == CC_ERROR)
981 return CC_ERROR;
982 el->el_history.eventno = 1 + el->el_history.ev.num
983 - el->el_state.argument;
984 if (el->el_history.eventno < 0) {
985 el->el_history.eventno = sv_event_no;
986 return CC_ERROR;
989 rval = hist_get(el);
990 if (rval == CC_ERROR)
991 el->el_history.eventno = sv_event_no;
992 return rval;
995 /* vi_histedit():
996 * Vi edit history line with vi
997 * [v]
999 libedit_private el_action_t
1000 /*ARGSUSED*/
1001 vi_histedit(EditLine *el, wint_t c __attribute__((__unused__)))
1003 int fd;
1004 pid_t pid;
1005 ssize_t st;
1006 int status;
1007 char tempfile[] = "/tmp/histedit.XXXXXXXXXX";
1008 char *cp = NULL;
1009 size_t len;
1010 wchar_t *line = NULL;
1012 if (el->el_state.doingarg) {
1013 if (vi_to_history_line(el, 0) == CC_ERROR)
1014 return CC_ERROR;
1017 fd = mkstemp(tempfile);
1018 if (fd < 0)
1019 return CC_ERROR;
1020 len = (size_t)(el->el_line.lastchar - el->el_line.buffer);
1021 #define TMP_BUFSIZ (EL_BUFSIZ * MB_LEN_MAX)
1022 cp = el_malloc(TMP_BUFSIZ * sizeof(*cp));
1023 if (cp == NULL)
1024 goto error;
1025 line = el_malloc(len * sizeof(*line) + 1);
1026 if (line == NULL)
1027 goto error;
1028 wcsncpy(line, el->el_line.buffer, len);
1029 line[len] = '\0';
1030 wcstombs(cp, line, TMP_BUFSIZ - 1);
1031 cp[TMP_BUFSIZ - 1] = '\0';
1032 len = strlen(cp);
1033 write(fd, cp, len);
1034 write(fd, "\n", (size_t)1);
1035 pid = fork();
1036 switch (pid) {
1037 case -1:
1038 goto error;
1039 case 0:
1040 close(fd);
1041 execlp("vi", "vi", tempfile, (char *)NULL);
1042 exit(0);
1043 /*NOTREACHED*/
1044 default:
1045 while (waitpid(pid, &status, 0) != pid)
1046 continue;
1047 lseek(fd, (off_t)0, SEEK_SET);
1048 st = read(fd, cp, TMP_BUFSIZ - 1);
1049 if (st > 0) {
1050 cp[st] = '\0';
1051 len = (size_t)(el->el_line.limit - el->el_line.buffer);
1052 len = mbstowcs(el->el_line.buffer, cp, len);
1053 if (len > 0 && el->el_line.buffer[len - 1] == '\n')
1054 --len;
1056 else
1057 len = 0;
1058 el->el_line.cursor = el->el_line.buffer;
1059 el->el_line.lastchar = el->el_line.buffer + len;
1060 el_free(cp);
1061 el_free(line);
1062 break;
1065 close(fd);
1066 unlink(tempfile);
1067 /* return CC_REFRESH; */
1068 return ed_newline(el, 0);
1069 error:
1070 el_free(line);
1071 el_free(cp);
1072 close(fd);
1073 unlink(tempfile);
1074 return CC_ERROR;
1077 /* vi_history_word():
1078 * Vi append word from previous input line
1079 * [_]
1080 * Who knows where this one came from!
1081 * '_' in vi means 'entire current line', so 'cc' is a synonym for 'c_'
1083 libedit_private el_action_t
1084 /*ARGSUSED*/
1085 vi_history_word(EditLine *el, wint_t c __attribute__((__unused__)))
1087 const wchar_t *wp = HIST_FIRST(el);
1088 const wchar_t *wep, *wsp;
1089 int len;
1090 wchar_t *cp;
1091 const wchar_t *lim;
1093 if (wp == NULL)
1094 return CC_ERROR;
1096 wep = wsp = NULL;
1097 do {
1098 while (iswspace(*wp))
1099 wp++;
1100 if (*wp == 0)
1101 break;
1102 wsp = wp;
1103 while (*wp && !iswspace(*wp))
1104 wp++;
1105 wep = wp;
1106 } while ((!el->el_state.doingarg || --el->el_state.argument > 0)
1107 && *wp != 0);
1109 if (wsp == NULL || (el->el_state.doingarg && el->el_state.argument != 0))
1110 return CC_ERROR;
1112 cv_undo(el);
1113 len = (int)(wep - wsp);
1114 if (el->el_line.cursor < el->el_line.lastchar)
1115 el->el_line.cursor++;
1116 c_insert(el, len + 1);
1117 cp = el->el_line.cursor;
1118 lim = el->el_line.limit;
1119 if (cp < lim)
1120 *cp++ = ' ';
1121 while (wsp < wep && cp < lim)
1122 *cp++ = *wsp++;
1123 el->el_line.cursor = cp;
1125 el->el_map.current = el->el_map.key;
1126 return CC_REFRESH;
1129 /* vi_redo():
1130 * Vi redo last non-motion command
1131 * [.]
1133 libedit_private el_action_t
1134 /*ARGSUSED*/
1135 vi_redo(EditLine *el, wint_t c __attribute__((__unused__)))
1137 c_redo_t *r = &el->el_chared.c_redo;
1139 if (!el->el_state.doingarg && r->count) {
1140 el->el_state.doingarg = 1;
1141 el->el_state.argument = r->count;
1144 el->el_chared.c_vcmd.pos = el->el_line.cursor;
1145 el->el_chared.c_vcmd.action = r->action;
1146 if (r->pos != r->buf) {
1147 if (r->pos + 1 > r->lim)
1148 /* sanity */
1149 r->pos = r->lim - 1;
1150 r->pos[0] = 0;
1151 el_wpush(el, r->buf);
1154 el->el_state.thiscmd = r->cmd;
1155 el->el_state.thisch = r->ch;
1156 return (*el->el_map.func[r->cmd])(el, r->ch);