1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2014 Steffen "Daode" Nurpmeso <sdaoden@users.sf.net>.
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * Copyright (c) 2013 - 2014 Steffen "Daode" Nurpmeso <sdaoden@users.sf.net>.
43 * Permission to use, copy, modify, and/or distribute this software for any
44 * purpose with or without fee is hereby granted, provided that the above
45 * copyright notice and this permission notice appear in all copies.
47 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
48 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
49 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
50 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
51 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
52 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
53 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
56 #ifndef HAVE_AMALGAMATION
61 # include <readline/readline.h>
63 # include <readline/history.h>
65 #elif defined HAVE_EDITLINE
66 # include <histedit.h>
69 /* Shared history support macros */
71 # define _CL_HISTFILE(S) \
73 S = ok_vlook(NAIL_HISTFILE);\
75 S = fexpand(S, FEXP_LOCAL);\
78 # define _CL_HISTSIZE(V) \
80 char const *__sv = ok_vlook(NAIL_HISTSIZE);\
82 if (__sv == NULL || *__sv == '\0' || (__rv = strtol(__sv, NULL, 10)) == 0)\
90 # define _CL_CHECK_ADDHIST(S,NOACT) \
101 # define C_HISTORY_SHARED \
107 if (argv[1] != NULL)\
109 if (asccasecmp(*argv, "show") == 0)\
111 if (asccasecmp(*argv, "clear") == 0)\
113 if ((entry = strtol(*argv, argv, 10)) > 0 && **argv == '\0')\
116 fprintf(stderr, "Synopsis: history: %s\n", tr(431,\
117 "<show> (default), <clear> or select <NO> from editor history"));\
120 return (v == NULL ? !STOP : !OKAY); /* xxx 1:bad 0:good -- do some */
121 #endif /* HAVE_HISTORY */
123 /* fexpand() flags for expand-on-tab */
124 #define _CL_TAB_FEXP_FL (FEXP_FULL | FEXP_SILENT | FEXP_MULTIOK)
127 * Because we have multiple identical implementations, change file layout a bit
128 * and place the implementations one after the other below the other externals
132 getapproval(char const *prompt
, bool_t noninteract_default
)
136 if (!(options
& OPT_INTERACTIVE
)) {
137 rv
= noninteract_default
;
142 prompt
= tr(264, "Continue (y/n)? ");
145 if (readline_input(prompt
, FAL0
, &termios_state
.ts_linebuf
,
146 &termios_state
.ts_linesize
, NULL
) >= 0)
147 switch (termios_state
.ts_linebuf
[0]) {
155 termios_state_reset();
161 yorn(char const *msg
)
165 rv
= getapproval(msg
, TRU1
);
170 getuser(char const *query
)
175 query
= tr(509, "User: ");
177 if (readline_input(query
, FAL0
, &termios_state
.ts_linebuf
,
178 &termios_state
.ts_linesize
, NULL
) >= 0)
179 user
= termios_state
.ts_linebuf
;
180 termios_state_reset();
185 getpassword(char const *query
) /* FIXME encaps ttystate signal safe */
191 query
= tr(510, "Password: ");
192 fputs(query
, stdout
);
196 * FIXME everywhere: tcsetattr() generates SIGTTOU when we're not in
197 * foreground pgrp, and can fail with EINTR!!
199 if (options
& OPT_TTYIN
) {
200 tcgetattr(STDIN_FILENO
, &termios_state
.ts_tios
);
201 memcpy(&tios
, &termios_state
.ts_tios
, sizeof tios
);
202 termios_state
.ts_needs_reset
= TRU1
;
203 tios
.c_iflag
&= ~(ISTRIP
);
204 tios
.c_lflag
&= ~(ECHO
| ECHOE
| ECHOK
| ECHONL
);
205 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &tios
);
208 if (readline_restart(stdin
, &termios_state
.ts_linebuf
,
209 &termios_state
.ts_linesize
, 0) >= 0)
210 pass
= termios_state
.ts_linebuf
;
211 termios_state_reset();
213 if (options
& OPT_TTYIN
)
219 getcredentials(char **user
, char **pass
)
222 char *u
= *user
, *p
= *pass
;
225 if ((u
= getuser(NULL
)) == NULL
)
233 if ((p
= getpassword(NULL
)) == NULL
)
245 static sighandler_type _rl_shup
;
246 static char * _rl_buf
; /* pre_input() hook: initial line */
247 static int _rl_buflen
; /* content, and its length */
249 static int _rl_pre_input(void);
254 /* Handle leftover data from \ escaped former line */
255 rl_extend_line_buffer(_rl_buflen
+ 10);
256 memcpy(rl_line_buffer
, _rl_buf
, _rl_buflen
+ 1);
257 rl_point
= rl_end
= _rl_buflen
;
258 rl_pre_input_hook
= (rl_hook_func_t
*)NULL
;
271 rl_readline_name
= UNCONST(uagent
);
275 stifle_history((int)hs
);
277 rl_read_init_file(NULL
);
279 /* Because rl_read_init_file() may have introduced yet a different
280 * history size limit, simply load and incorporate the history, leave
281 * it up to readline(3) to do the rest */
313 /* readline(3) doesn't catch it :( */
314 rl_free_line_state();
315 rl_cleanup_after_signal();
316 safe_signal(SIGHUP
, _rl_shup
);
318 sigaddset(&nset
, sig
);
319 sigprocmask(SIG_UNBLOCK
, &nset
, &oset
);
321 /* XXX When we come here we'll continue editing, so reestablish
322 * XXX cannot happen */
323 sigprocmask(SIG_BLOCK
, &oset
, (sigset_t
*)NULL
);
324 _rl_shup
= safe_signal(SIGHUP
, &tty_signal
);
325 rl_reset_after_signal();
333 (tty_readline
)(char const *prompt
, char **linebuf
, size_t *linesize
, size_t n
342 rl_pre_input_hook
= &_rl_pre_input
;
345 _rl_shup
= safe_signal(SIGHUP
, &tty_signal
);
346 line
= readline(prompt
!= NULL
? prompt
: "");
347 safe_signal(SIGHUP
, _rl_shup
);
355 if (n
>= *linesize
) {
356 *linesize
= LINESIZE
+ n
+ 1;
357 *linebuf
= (srealloc
)(*linebuf
, *linesize SMALLOC_DEBUG_ARGSCALL
);
359 memcpy(*linebuf
, line
, n
);
361 (*linebuf
)[n
] = '\0';
368 tty_addhist(char const *s
)
371 _CL_CHECK_ADDHIST(s
, goto jleave
);
372 hold_all_sigs(); /* XXX too heavy */
373 add_history(s
); /* XXX yet we jump away! */
374 rele_all_sigs(); /* XXX remove jumps */
393 if ((fp
= Ftemp(&cp
, "Ra", "w+", 0600, 1)) == NULL
) {
401 hs
= history_get_history_state();
403 for (i
= (ul_it
)hs
->length
, hl
= hs
->entries
+ i
, b
= 0; i
> 0; --i
) {
404 size_t sl
= strlen(cp
= (*--hl
)->line
);
405 fprintf(fp
, "%4lu. %-50.50s (%4lu+%lu bytes)\n", i
, cp
, b
, sl
);
409 page_or_print(fp
, (size_t)hs
->length
);
419 HISTORY_STATE
*hs
= history_get_history_state();
421 if (UICMP(z
, entry
, <=, hs
->length
))
422 v
= temporary_arg_v_store
= hs
->entries
[entry
- 1]->line
;
428 # endif /* HAVE_HISTORY */
429 #endif /* HAVE_READLINE */
436 static EditLine
* _el_el
; /* editline(3) handle */
437 static char const * _el_prompt
; /* Current prompt */
439 static History
* _el_hcom
; /* History handle for commline */
442 static char const * _el_getprompt(void);
461 _el_hcom
= history_init();
462 history(_el_hcom
, &he
, H_SETSIZE
, (int)hs
);
463 /* We unroll our own one history(_el_hcom, &he, H_SETUNIQUE, 1);*/
466 _el_el
= el_init(uagent
, stdin
, stdout
, stderr
);
467 el_set(_el_el
, EL_SIGNAL
, 1);
468 el_set(_el_el
, EL_TERMINAL
, NULL
);
469 /* Need to set HIST before EDITOR, otherwise it won't work automatic */
471 el_set(_el_el
, EL_HIST
, &history
, _el_hcom
);
473 el_set(_el_el
, EL_EDITOR
, "emacs");
474 # ifdef EL_PROMPT_ESC
475 el_set(_el_el
, EL_PROMPT_ESC
, &_el_getprompt
, '\1');
477 el_set(_el_el
, EL_PROMPT
, &_el_getprompt
);
480 el_set(_el_el
, EL_ADDFN
, "tab_complete",
481 "editline(3) internal completion function", &_el_file_cpl
);
482 el_set(_el_el
, EL_BIND
, "^I", "tab_complete", NULL
);
485 el_set(_el_el
, EL_BIND
, "^R", "ed-search-prev-history", NULL
);
487 el_source(_el_el
, NULL
); /* Source ~/.editrc */
489 /* Because el_source() may have introduced yet a different history size
490 * limit, simply load and incorporate the history, leave it up to
491 * editline(3) to do the rest */
495 history(_el_hcom
, &he
, H_LOAD
, v
);
512 history(_el_hcom
, &he
, H_SAVE
, v
);
513 history_end(_el_hcom
);
532 (tty_readline
)(char const *prompt
, char **linebuf
, size_t *linesize
, size_t n
538 _el_prompt
= (prompt
!= NULL
) ? prompt
: "";
540 el_push(_el_el
, *linebuf
);
541 line
= el_gets(_el_el
, &nn
);
549 if (n
> 0 && line
[n
- 1] == '\n')
552 if (n
>= *linesize
) {
553 *linesize
= LINESIZE
+ n
+ 1;
554 *linebuf
= (srealloc
)(*linebuf
, *linesize SMALLOC_DEBUG_ARGSCALL
);
556 memcpy(*linebuf
, line
, n
);
557 (*linebuf
)[n
] = '\0';
563 tty_addhist(char const *s
)
566 /* Enlarge meaning of unique .. to something that rocks;
567 * xxx unfortunately this is expensive to do with editline(3)
568 * xxx maybe it would be better to hook the ptfs instead? */
572 _CL_CHECK_ADDHIST(s
, goto jleave
);
574 hold_all_sigs(); /* XXX too heavy, yet we jump away! */
575 for (i
= history(_el_hcom
, &he
, H_FIRST
); i
>= 0;
576 i
= history(_el_hcom
, &he
, H_NEXT
))
577 if (strcmp(he
.str
, s
) == 0) {
578 history(_el_hcom
, &he
, H_DEL
, he
.num
);
581 history(_el_hcom
, &he
, H_ENTER
, s
);
582 rele_all_sigs(); /* XXX remove jumps */
601 if ((fp
= Ftemp(&cp
, "Ra", "w+", 0600, 1)) == NULL
) {
609 i
= (size_t)((history(_el_hcom
, &he
, H_GETSIZE
) >= 0) ? he
.num
: 0);
611 for (x
= history(_el_hcom
, &he
, H_FIRST
); x
>= 0;
612 x
= history(_el_hcom
, &he
, H_NEXT
)) {
613 size_t sl
= strlen(he
.str
);
614 fprintf(fp
, "%4lu. %-50.50s (%4lu+%lu bytes)\n",
615 (ul_it
)i
, he
.str
, (ul_it
)b
, (ul_it
)sl
);
620 page_or_print(fp
, i
);
627 history(_el_hcom
, &he
, H_CLEAR
);
636 i
= (size_t)((history(_el_hcom
, &he
, H_GETSIZE
) >= 0) ? he
.num
: 0);
637 if (UICMP(z
, entry
, <=, i
)) {
638 entry
= (long)i
- entry
;
639 for (x
= history(_el_hcom
, &he
, H_FIRST
); x
>= 0;
640 x
= history(_el_hcom
, &he
, H_NEXT
))
642 v
= temporary_arg_v_store
= UNCONST(he
.str
);
650 # endif /* HAVE_HISTORY */
651 #endif /* HAVE_EDITLINE */
654 * NCL: our homebrew version (inspired from NetBSD sh(1) / dash(1)s hetio.c).
656 * Only used in interactive mode, simply use STDIN_FILENO as point of interest.
657 * We do not handle character widths because the terminal must deal with that
658 * anyway on the one hand, and also wcwidth(3) doesn't support zero-width
659 * characters by definition on the other. We're addicted.
661 * To avoid memory leaks etc. with the current codebase that simply longjmp(3)s
662 * we're forced to use the very same buffer--the one that is passed through to
663 * us from the outside--to store anything we need, i.e., a `struct cell[]', and
664 * convert that on-the-fly back to the plain char* result once we're done.
665 * To simplify our live, use savestr() buffers for all other needed memory
669 * TODO NCL: don't use that stupid .sint=-1 stuff, but simply block all signals
670 * TODO NCL: during handler de-/installation handling.
675 # define MAX_INPUT 255 /* (_POSIX_MAX_INPUT = 255 as of Issue 7 TC1) */
678 /* Since we simply fputs(3) the prompt, assume each character requires two
679 * visual cells -- and we need to restrict the maximum prompt size because
680 * of MAX_INPUT and our desire to have room for some error message left */
681 # define _PROMPT_VLEN(P) (strlen(P) * 2)
682 # define _PROMPT_MAX ((MAX_INPUT / 2) + (MAX_INPUT / 4))
685 sighandler_type shdl
; /* Try avoid races by setting */
686 sl_it sint
; /* .sint=-1 when inactive */
688 CTA(sizeof(sl_it
) >= sizeof(sighandler_type
));
698 char cbuf
[MB_LEN_MAX
* 2]; /* .. plus reset shift sequence */
702 size_t cursor
; /* Current cursor position */
703 size_t topins
; /* Outermost cursor col set */
705 char * cbuf
; /* *x_buf */
708 struct str defc
; /* Current default content */
709 struct str savec
; /* Saved default content */
711 struct hist
* hist
; /* History cursor */
714 char const * nd
; /* Cursor right */
715 char ** x_buf
; /* Caller pointers */
722 struct hist
* younger
;
724 char dat
[VFIELD_SIZE(sizeof(size_t))];
728 static union xsighdl _ncl_oint
;
729 static union xsighdl _ncl_oquit
;
730 static union xsighdl _ncl_oterm
;
731 static union xsighdl _ncl_ohup
;
732 static union xsighdl _ncl_otstp
;
733 static union xsighdl _ncl_ottin
;
734 static union xsighdl _ncl_ottou
;
735 static struct xtios _ncl_tios
;
737 static struct hist
* _ncl_hist
;
738 static struct hist
* _ncl_hist_tail
;
739 static size_t _ncl_hist_size
;
740 static size_t _ncl_hist_size_max
;
741 static bool_t _ncl_hist_load
;
744 static void _ncl_sigs_up(void);
745 static void _ncl_sigs_down(void);
747 static void _ncl_term_mode(bool_t raw
);
749 static void _ncl_check_grow(struct line
*l
, size_t no SMALLOC_DEBUG_ARGS
);
750 static void _ncl_bs_eof_dvup(struct cell
*cap
, size_t i
);
751 static ssize_t
_ncl_wboundary(struct line
*l
, ssize_t dir
);
752 static ssize_t
_ncl_cell2dat(struct line
*l
);
753 # if defined HAVE_HISTORY || defined HAVE_TABEXPAND
754 static void _ncl_cell2save(struct line
*l
);
757 static void _ncl_khome(struct line
*l
, bool_t dobell
);
758 static void _ncl_kend(struct line
*l
);
759 static void _ncl_kbs(struct line
*l
);
760 static void _ncl_kkill(struct line
*l
, bool_t dobell
);
761 static ssize_t
_ncl_keof(struct line
*l
);
762 static void _ncl_kleft(struct line
*l
);
763 static void _ncl_kright(struct line
*l
);
764 static void _ncl_krefresh(struct line
*l
);
765 static void _ncl_kbwddelw(struct line
*l
);
766 static void _ncl_kgow(struct line
*l
, ssize_t dir
);
767 static void _ncl_kother(struct line
*l
, wchar_t wc
);
769 static size_t __ncl_khist_shared(struct line
*l
, struct hist
*hp
);
770 static size_t _ncl_khist(struct line
*l
, bool_t backwd
);
771 static size_t _ncl_krhist(struct line
*l
);
773 # ifdef HAVE_TABEXPAND
774 static size_t _ncl_kht(struct line
*l
);
776 static ssize_t
_ncl_readline(char const *prompt
, char **buf
, size_t *bufsize
,
777 size_t len SMALLOC_DEBUG_ARGS
);
782 if (_ncl_oint
.sint
== -1)
783 _ncl_oint
.shdl
= safe_signal(SIGINT
, &tty_signal
);
784 if (_ncl_oquit
.sint
== -1)
785 _ncl_oquit
.shdl
= safe_signal(SIGQUIT
, &tty_signal
);
786 if (_ncl_oterm
.sint
== -1)
787 _ncl_oterm
.shdl
= safe_signal(SIGTERM
, &tty_signal
);
788 if (_ncl_ohup
.sint
== -1)
789 _ncl_ohup
.shdl
= safe_signal(SIGHUP
, &tty_signal
);
790 if (_ncl_otstp
.sint
== -1)
791 _ncl_otstp
.shdl
= safe_signal(SIGTSTP
, &tty_signal
);
792 if (_ncl_ottin
.sint
== -1)
793 _ncl_ottin
.shdl
= safe_signal(SIGTTIN
, &tty_signal
);
794 if (_ncl_ottou
.sint
== -1)
795 _ncl_ottou
.shdl
= safe_signal(SIGTTOU
, &tty_signal
);
801 /* aaah.. atomic cas would be nice (but isn't it all redundant?) */
804 if (_ncl_ottou
.sint
!= -1) {
805 st
= _ncl_ottou
.shdl
, _ncl_ottou
.sint
= -1;
806 safe_signal(SIGTTOU
, st
);
808 if (_ncl_ottin
.sint
!= -1) {
809 st
= _ncl_ottin
.shdl
, _ncl_ottin
.sint
= -1;
810 safe_signal(SIGTTIN
, st
);
812 if (_ncl_otstp
.sint
!= -1) {
813 st
= _ncl_otstp
.shdl
, _ncl_otstp
.sint
= -1;
814 safe_signal(SIGTSTP
, st
);
816 if (_ncl_ohup
.sint
!= -1) {
817 st
= _ncl_ohup
.shdl
, _ncl_ohup
.sint
= -1;
818 safe_signal(SIGHUP
, st
);
820 if (_ncl_oterm
.sint
!= -1) {
821 st
= _ncl_oterm
.shdl
, _ncl_oterm
.sint
= -1;
822 safe_signal(SIGTERM
, st
);
824 if (_ncl_oquit
.sint
!= -1) {
825 st
= _ncl_oquit
.shdl
, _ncl_oquit
.sint
= -1;
826 safe_signal(SIGQUIT
, st
);
828 if (_ncl_oint
.sint
!= -1) {
829 st
= _ncl_oint
.shdl
, _ncl_oint
.sint
= -1;
830 safe_signal(SIGINT
, st
);
835 _ncl_term_mode(bool_t raw
)
837 struct termios
*tiosp
= &_ncl_tios
.told
;
842 /* Always requery the attributes, in case we've been moved from background
843 * to foreground or however else in between sessions */
844 tcgetattr(STDIN_FILENO
, tiosp
);
845 memcpy(&_ncl_tios
.tnew
, tiosp
, sizeof *tiosp
);
846 tiosp
= &_ncl_tios
.tnew
;
847 tiosp
->c_cc
[VMIN
] = 1;
848 tiosp
->c_cc
[VTIME
] = 0;
849 tiosp
->c_iflag
&= ~(ISTRIP
);
850 tiosp
->c_lflag
&= ~(ECHO
/*| ECHOE | ECHONL */| ICANON
| IEXTEN
);
852 tcsetattr(STDIN_FILENO
, TCSADRAIN
, tiosp
);
856 _ncl_check_grow(struct line
*l
, size_t no SMALLOC_DEBUG_ARGS
)
858 size_t i
= (l
->topins
+ no
) * sizeof(struct cell
) + 2 * sizeof(struct cell
);
860 if (i
> *l
->x_bufsize
) {
864 *l
->x_buf
= (srealloc
)(*l
->x_buf
, i SMALLOC_DEBUG_ARGSCALL
);
869 _ncl_bs_eof_dvup(struct cell
*cap
, size_t i
)
874 memmove(cap
, cap
+ 1, i
* sizeof(*cap
));
876 /* And.. the (rest of the) visual update */
877 for (j
= 0; j
< i
; ++j
)
878 fwrite(cap
[j
].cbuf
, sizeof *cap
->cbuf
, cap
[j
].count
, stdout
);
879 fputs(" \b", stdout
);
880 for (j
= 0; j
< i
; ++j
)
885 _ncl_wboundary(struct line
*l
, ssize_t dir
)
887 size_t c
= l
->cursor
, t
= l
->topins
, i
;
898 --t
, --c
; /* Unsigned wrapping may occur (twice), then */
900 for (i
= 0, cap
= l
->line
.cells
, anynon
= FAL0
;;) {
901 wchar_t wc
= cap
[c
+ dir
].wc
;
902 if (iswblank(wc
) || iswpunct(wc
)) {
920 _ncl_cell2dat(struct line
*l
)
925 for (i
= 0; i
< l
->topins
; ++i
) {
926 struct cell
*cap
= l
->line
.cells
+ i
;
927 memcpy(l
->line
.cbuf
+ len
, cap
->cbuf
, cap
->count
);
930 l
->line
.cbuf
[len
] = '\0';
934 # if defined HAVE_HISTORY || defined HAVE_TABEXPAND
936 _ncl_cell2save(struct line
*l
)
941 l
->savec
.s
= NULL
, l
->savec
.l
= 0;
945 for (cap
= l
->line
.cells
, len
= i
= 0; i
< l
->topins
; ++cap
, ++i
)
949 l
->savec
.s
= salloc(len
+ 1);
951 for (cap
= l
->line
.cells
, len
= i
= 0; i
< l
->topins
; ++cap
, ++i
) {
952 memcpy(l
->savec
.s
+ len
, cap
->cbuf
, cap
->count
);
955 l
->savec
.s
[len
] = '\0';
962 _ncl_khome(struct line
*l
, bool_t dobell
)
964 size_t c
= l
->cursor
;
975 _ncl_kend(struct line
*l
)
977 ssize_t i
= (ssize_t
)(l
->topins
- l
->cursor
);
980 l
->cursor
= l
->topins
;
982 fputs(l
->nd
, stdout
);
988 _ncl_kbs(struct line
*l
)
990 ssize_t c
= l
->cursor
, t
= l
->topins
;
997 _ncl_bs_eof_dvup(l
->line
.cells
+ c
, t
);
1003 _ncl_kkill(struct line
*l
, bool_t dobell
)
1005 size_t j
, c
= l
->cursor
, i
= (size_t)(l
->topins
- c
);
1009 for (j
= i
; j
!= 0; --j
)
1011 for (j
= i
; j
!= 0; --j
)
1018 _ncl_keof(struct line
*l
)
1020 size_t c
= l
->cursor
, t
= l
->topins
;
1021 ssize_t i
= (ssize_t
)(t
- c
);
1025 _ncl_bs_eof_dvup(l
->line
.cells
+ c
, --i
);
1026 } else if (t
== 0 && !ok_blook(ignoreeof
)) {
1027 fputs("^D", stdout
);
1038 _ncl_kleft(struct line
*l
)
1040 if (l
->cursor
> 0) {
1048 _ncl_kright(struct line
*l
)
1050 if (l
->cursor
< l
->topins
) {
1052 fputs(l
->nd
, stdout
);
1058 _ncl_krefresh(struct line
*l
)
1064 if (l
->prompt
!= NULL
&& *l
->prompt
!= '\0')
1065 fputs(l
->prompt
, stdout
);
1066 for (cap
= l
->line
.cells
, i
= l
->topins
; i
> 0; ++cap
, --i
)
1067 fwrite(cap
->cbuf
, sizeof *cap
->cbuf
, cap
->count
, stdout
);
1068 for (i
= l
->topins
- l
->cursor
; i
> 0; --i
)
1073 _ncl_kbwddelw(struct line
*l
)
1076 size_t c
= l
->cursor
, t
, j
;
1079 i
= _ncl_wboundary(l
, -1);
1090 cap
= l
->line
.cells
+ c
;
1092 if (t
!= l
->cursor
) {
1094 memmove(cap
, cap
+ i
, j
* sizeof(*cap
));
1097 for (j
= i
; j
> 0; --j
)
1099 for (j
= l
->topins
- c
; j
> 0; ++cap
, --j
)
1100 fwrite(cap
[0].cbuf
, sizeof *cap
->cbuf
, cap
[0].count
, stdout
);
1101 for (j
= i
; j
> 0; --j
)
1103 for (j
= t
- c
; j
> 0; --j
)
1110 _ncl_kgow(struct line
*l
, ssize_t dir
)
1112 ssize_t i
= _ncl_wboundary(l
, dir
);
1126 fputs(l
->nd
, stdout
);
1133 _ncl_kother(struct line
*l
, wchar_t wc
)
1135 /* Append if at EOL, insert otherwise;
1136 * since we may move around character-wise, always use a fresh ps */
1138 struct cell cell
, *cap
;
1141 /* First init a cell and see wether we'll really handle this wc */
1143 memset(&ps
, 0, sizeof ps
);
1144 i
= wcrtomb(cell
.cbuf
, wc
, &ps
);
1147 cell
.count
= (ui_it
)i
;
1148 if (enc_has_state
) {
1149 i
= wcrtomb(cell
.cbuf
+ i
, L
'\0', &ps
);
1152 else if (--i
< MB_LEN_MAX
)
1153 cell
.count
+= (ui_it
)i
;
1158 /* Yes, we will! Place it in the array */
1160 i
= l
->topins
++ - c
;
1161 cap
= l
->line
.cells
+ c
;
1163 memmove(cap
+ 1, cap
, i
* sizeof(cell
));
1164 memcpy(cap
, &cell
, sizeof cell
);
1166 /* And update visual */
1169 fwrite(cap
->cbuf
, sizeof *cap
->cbuf
, cap
->count
, stdout
);
1170 while ((++cap
, i
-- != 0));
1177 # ifdef HAVE_HISTORY
1179 __ncl_khist_shared(struct line
*l
, struct hist
*hp
)
1183 if ((l
->hist
= hp
) != NULL
) {
1184 l
->defc
.s
= savestrbuf(hp
->dat
, hp
->len
);
1186 l
->defc
.l
= hp
->len
;
1187 if (l
->topins
> 0) {
1188 _ncl_khome(l
, FAL0
);
1189 _ncl_kkill(l
, FAL0
);
1199 _ncl_khist(struct line
*l
, bool_t backwd
)
1203 /* If we're not in history mode yet, save line content;
1204 * also, disallow forward search, then, and, of course, bail unless we
1205 * do have any history at all */
1206 if ((hp
= l
->hist
) == NULL
) {
1209 if ((hp
= _ncl_hist
) == NULL
)
1215 hp
= backwd
? hp
->older
: hp
->younger
;
1217 return __ncl_khist_shared(l
, hp
);
1221 _ncl_krhist(struct line
*l
)
1223 struct str orig_savec
;
1224 struct hist
*hp
= NULL
;
1226 /* We cannot complete an empty line */
1227 if (l
->topins
== 0) {
1228 /* XXX The upcoming hard reset would restore a set savec buffer,
1229 * XXX so forcefully reset that. A cleaner solution would be to
1230 * XXX reset it whenever a restore is no longer desired */
1231 l
->savec
.s
= NULL
, l
->savec
.l
= 0;
1234 if ((hp
= l
->hist
) == NULL
) {
1235 if ((hp
= _ncl_hist
) == NULL
)
1237 orig_savec
.s
= NULL
;
1238 orig_savec
.l
= 0; /* silence CC */
1239 } else if ((hp
= hp
->older
) == NULL
)
1242 orig_savec
= l
->savec
;
1244 if (orig_savec
.s
== NULL
)
1246 for (; hp
!= NULL
; hp
= hp
->older
)
1247 if (is_prefix(l
->savec
.s
, hp
->dat
))
1249 if (orig_savec
.s
!= NULL
)
1250 l
->savec
= orig_savec
;
1252 return __ncl_khist_shared(l
, hp
);
1256 # ifdef HAVE_TABEXPAND
1258 _ncl_kht(struct line
*l
)
1260 struct str orig
, bot
, topp
, sub
, exp
;
1261 struct cell
*cword
, *ctop
, *cx
;
1262 bool_t set_savec
= FAL0
;
1265 /* We cannot expand an empty line */
1269 /* Get plain line data; if this is the first expansion/xy, update the
1270 * very original content so that ^G gets the origin back */
1280 cword
= l
->line
.cells
;
1281 ctop
= cword
+ l
->cursor
;
1283 /* topp: separate data right of cursor */
1284 if ((cx
= cword
+ l
->topins
) != ctop
) {
1285 for (rv
= 0; cx
> ctop
; --cx
)
1288 topp
.s
= orig
.s
+ orig
.l
- rv
;
1290 topp
.s
= NULL
, topp
.l
= 0;
1292 /* bot, sub: we cannot expand the entire data left of cursor, but only
1293 * the last "word", so separate them */
1294 while (cx
> cword
&& !iswspace(cx
[-1].wc
))
1296 for (rv
= 0; cword
< cx
; ++cword
)
1305 /* Leave room for "implicit asterisk" expansion, as below */
1307 sub
.s
= UNCONST("*");
1310 exp
.s
= salloc(sub
.l
+ 1 +1);
1311 memcpy(exp
.s
, sub
.s
, sub
.l
);
1312 exp
.s
[sub
.l
] = '\0';
1316 /* TODO there is a TODO note upon fexpand() with multi-return;
1317 * TODO if that will change, the if() below can be simplified */
1318 /* Super-Heavy-Metal: block all sigs, avoid leaks on jump */
1321 exp
.s
= fexpand(sub
.s
, _CL_TAB_FEXP_FL
);
1324 if (exp
.s
== NULL
|| (exp
.l
= strlen(exp
.s
)) == 0)
1326 /* If the expansion equals the original string, assume the user wants what
1327 * is usually known as tab completion, append `*' and restart */
1328 if (exp
.l
== sub
.l
&& !strcmp(exp
.s
, sub
.s
)) {
1329 if (sub
.s
[sub
.l
- 1] == '*')
1331 sub
.s
[sub
.l
++] = '*';
1332 sub
.s
[sub
.l
] = '\0';
1336 /* Cramp expansion length to MAX_INPUT, or 255 if not defined.
1337 * Take care to take *prompt* into account, since we don't know
1338 * anything about it's visual length (fputs(3) is used), simply
1339 * assume each character requires two columns */
1340 /* TODO the problem is that we loose control otherwise; in the best
1341 * TODO case the user can control via ^A and ^K etc., but be safe;
1342 * TODO we cannot simply adjust fexpand() because we don't know how
1343 * TODO that is implemented... The real solution would be to check
1344 * TODO wether we fit on a line, and start a pager if not.
1345 * TODO However, that should be part of a real tab-COMPLETION, then,
1346 * TODO i.e., don't EXPAND, but SHOW COMPLETIONS, page-wise if needed.
1347 * TODO And: MAX_INPUT is dynamic: pathconf(2), _SC_MAX_INPUT */
1348 rv
= (l
->prompt
!= NULL
) ? _PROMPT_VLEN(l
->prompt
) : 0;
1349 if (rv
+ bot
.l
+ exp
.l
+ topp
.l
>= MAX_INPUT
) {
1350 char const e1
[] = "[maximum line size exceeded]";
1351 exp
.s
= UNCONST(e1
);
1352 exp
.l
= sizeof(e1
) - 1;
1354 if (rv
+ bot
.l
+ exp
.l
>= MAX_INPUT
)
1356 if (rv
+ exp
.l
>= MAX_INPUT
) {
1357 char const e2
[] = "[ERR]";
1358 exp
.s
= UNCONST(e2
);
1359 exp
.l
= sizeof(e2
) - 1;
1363 orig
.l
= bot
.l
+ exp
.l
+ topp
.l
;
1364 orig
.s
= salloc(orig
.l
+ 1 + 5);
1365 if ((rv
= bot
.l
) > 0)
1366 memcpy(orig
.s
, bot
.s
, rv
);
1367 memcpy(orig
.s
+ rv
, exp
.s
, exp
.l
);
1370 memcpy(orig
.s
+ rv
, topp
.s
, topp
.l
);
1376 _ncl_khome(l
, FAL0
);
1377 _ncl_kkill(l
, FAL0
);
1381 /* If we've provided a default content, but failed to expand, there is
1382 * nothing we can "revert to": drop that default again */
1384 l
->savec
.s
= NULL
, l
->savec
.l
= 0;
1388 # endif /* HAVE_TABEXPAND */
1391 _ncl_readline(char const *prompt
, char **buf
, size_t *bufsize
, size_t len
1394 /* We want to save code, yet we may have to incorporate a lines'
1395 * default content and / or default input to switch back to after some
1396 * history movement; let "len > 0" mean "have to display some data
1397 * buffer", and only otherwise read(2) it */
1400 char cbuf_base
[MB_LEN_MAX
* 2], *cbuf
, *cbufp
;
1403 ui32_t maybe_cursor
;
1405 memset(&l
, 0, sizeof l
);
1408 l
.defc
.s
= savestrbuf(*buf
, len
);
1411 if ((l
.prompt
= prompt
) != NULL
&& _PROMPT_VLEN(prompt
) > _PROMPT_MAX
)
1412 l
.prompt
= prompt
= "?ERR?";
1413 /* TODO *l.nd=='\0' only because we have no value-cache -> see acmava.c */
1414 if ((l
.nd
= ok_vlook(line_editor_cursor_right
)) == NULL
|| *l
.nd
== '\0')
1415 l
.nd
= "\033[C"; /* XXX no "magic" constant */
1417 l
.x_bufsize
= bufsize
;
1419 if (prompt
!= NULL
&& *prompt
!= '\0') {
1420 fputs(prompt
, stdout
);
1424 memset(ps
, 0, sizeof ps
);
1426 /* TODO: NCL: we should output the reset sequence when we jrestart:
1427 * TODO: NCL: if we are using a stateful encoding? !
1428 * TODO: NCL: in short: this is not yet well understood */
1430 _ncl_check_grow(&l
, len SMALLOC_DEBUG_ARGSCALL
);
1432 /* Normal read(2)? Else buffer-takeover: speed this one up */
1437 assert(l
.defc
.l
> 0 && l
.defc
.s
!= NULL
);
1439 cbuf
= l
.defc
.s
+ (l
.defc
.l
- len
);
1443 /* Read in the next complete multibyte character */
1446 if ((rv
= read(STDIN_FILENO
, cbufp
, 1)) < 1) {
1447 if (errno
== EINTR
) /* xxx #if !SA_RESTART ? */
1454 /* Ach! the ISO C multibyte handling!
1455 * Encodings with locking shift states cannot really be helped, since
1456 * it is impossible to only query the shift state, as opposed to the
1457 * entire shift state + character pair (via ISO C functions) */
1458 rv
= (ssize_t
)mbrtowc(&wc
, cbuf
, (size_t)(cbufp
- cbuf
), ps
+ 0);
1460 /* Any error during take-over can only result in a hard reset;
1461 * Otherwise, if it's a hard error, or if too many redundant shift
1462 * sequences overflow our buffer, also perform a hard reset */
1463 if (len
!= 0 || rv
== -1 ||
1464 sizeof cbuf_base
== (size_t)(cbufp
- cbuf
)) {
1465 l
.savec
.s
= l
.defc
.s
= NULL
,
1466 l
.savec
.l
= l
.defc
.l
= len
= 0;
1471 /* Otherwise, due to the way we deal with the buffer, we need to
1472 * restore the mbstate_t from before this conversion */
1477 if (len
!= 0 && (len
-= (size_t)rv
) == 0)
1478 l
.defc
.s
= NULL
, l
.defc
.l
= 0;
1483 /* Don't interpret control bytes during buffer take-over */
1484 if (cbuf
!= cbuf_base
)
1487 case 'A' ^ 0x40: /* cursor home */
1488 _ncl_khome(&l
, TRU1
);
1491 case 'B' ^ 0x40: /* backward character */
1494 /* 'C': interrupt (CTRL-C) */
1495 case 'D' ^ 0x40: /* delete char forward if any, else EOF */
1496 if ((rv
= _ncl_keof(&l
)) < 0)
1499 case 'E' ^ 0x40: /* end of line */
1503 case 'F' ^ 0x40: /* forward character */
1507 case 'H' ^ 0x40: /* backspace */
1511 case 'I' ^ 0x40: /* horizontal tab */
1512 # ifdef HAVE_TABEXPAND
1513 if ((len
= _ncl_kht(&l
)) > 0)
1517 case 'J' ^ 0x40: /* NL (\n) */
1519 case 'G' ^ 0x40: /* full reset */
1522 case 'U' ^ 0x40: /* ^U: ^A + ^K */
1523 _ncl_khome(&l
, FAL0
);
1525 case 'K' ^ 0x40: /* kill from cursor to end of line */
1526 _ncl_kkill(&l
, (wc
== ('K' ^ 0x40) || l
.topins
== 0));
1527 /* (Handle full reset?) */
1528 if (wc
== ('G' ^ 0x40)) {
1529 # ifdef HAVE_HISTORY
1532 if ((len
= l
.savec
.l
) != 0) {
1534 l
.savec
.s
= NULL
, l
.savec
.l
= 0;
1540 case 'L' ^ 0x40: /* repaint line */
1545 case 'N' ^ 0x40: /* history next */
1546 # ifdef HAVE_HISTORY
1549 if ((len
= _ncl_khist(&l
, FAL0
)) > 0)
1558 case 'P' ^ 0x40: /* history previous */
1559 # ifdef HAVE_HISTORY
1560 if ((len
= _ncl_khist(&l
, TRU1
)) > 0)
1568 case 'R' ^ 0x40: /* reverse history search */
1569 # ifdef HAVE_HISTORY
1570 if ((len
= _ncl_krhist(&l
)) > 0)
1579 /*case 'V' ^ 0x40: TODO*/ /* forward delete "word" */
1580 case 'W' ^ 0x40: /* backward delete "word" */
1583 case 'X' ^ 0x40: /* move cursor forward "word" */
1586 case 'Y' ^ 0x40: /* move cursor backward "word" */
1589 /* 'Z': suspend (CTRL-Z) */
1591 if (maybe_cursor
++ != 0)
1595 /* XXX Handle usual ^[[[ABCD] cursor keys -- UGLY, "MAGIC", INFLEX */
1596 if (maybe_cursor
> 0) {
1597 if (++maybe_cursor
== 2) {
1604 case L
'A': goto j_p
;
1605 case L
'B': goto j_n
;
1606 case L
'C': goto j_f
;
1607 case L
'D': goto j_b
;
1609 _ncl_kother(&l
, L
'[');
1614 _ncl_kother(&l
, wc
);
1615 /* Don't clear the history during takeover..
1616 * ..and also avoid fflush()ing unless we've
1617 * worked the entire buffer */
1620 # ifdef HAVE_HISTORY
1621 if (cbuf
== cbuf_base
)
1633 /* We have a completed input line, convert the struct cell data to its
1634 * plain character equivalent */
1638 len
= _ncl_cell2dat(&l
);
1647 # ifdef HAVE_HISTORY
1651 size_t lsize
, cnt
, llen
;
1654 _ncl_oint
.sint
= _ncl_oquit
.sint
= _ncl_oterm
.sint
=
1655 _ncl_ohup
.sint
= _ncl_otstp
.sint
= _ncl_ottin
.sint
=
1656 _ncl_ottou
.sint
= -1;
1658 # ifdef HAVE_HISTORY
1661 _ncl_hist_size_max
= hs
;
1669 hold_all_sigs(); /* XXX too heavy, yet we may jump even here!? */
1670 f
= fopen(v
, "r"); /* TODO HISTFILE LOAD: use linebuf pool */
1677 while (fgetline(&lbuf
, &lsize
, &cnt
, &llen
, f
, FAL0
) != NULL
) {
1678 if (llen
> 0 && lbuf
[llen
- 1] == '\n')
1679 lbuf
[--llen
] = '\0';
1680 if (llen
== 0 || lbuf
[0] == '#') /* xxx comments? noone! */
1682 _ncl_hist_load
= TRU1
;
1684 _ncl_hist_load
= FAL0
;
1691 rele_all_sigs(); /* XXX remove jumps */
1693 # endif /* HAVE_HISTORY */
1700 # ifdef HAVE_HISTORY
1714 if ((hp
= _ncl_hist
) != NULL
)
1715 while (hp
->older
!= NULL
&& hs
-- != 0)
1718 hold_all_sigs(); /* TODO too heavy, yet we may jump even here!? */
1719 f
= fopen(v
, "w"); /* TODO temporary + rename?! */
1722 if (fchmod(fileno(f
), S_IRUSR
| S_IWUSR
) != 0)
1725 for (; hp
!= NULL
; hp
= hp
->younger
) {
1726 fwrite(hp
->dat
, sizeof *hp
->dat
, hp
->len
, f
);
1732 rele_all_sigs(); /* XXX remove jumps */
1734 # endif /* HAVE_HISTORY */
1741 sigset_t nset
, oset
;
1745 /* We don't deal with SIGWINCH, yet get called from main.c */
1748 _ncl_term_mode(FAL0
);
1751 sigaddset(&nset
, sig
);
1752 sigprocmask(SIG_UNBLOCK
, &nset
, &oset
);
1754 /* When we come here we'll continue editing, so reestablish */
1755 sigprocmask(SIG_BLOCK
, &oset
, (sigset_t
*)NULL
);
1757 _ncl_term_mode(TRU1
);
1763 (tty_readline
)(char const *prompt
, char **linebuf
, size_t *linesize
, size_t n
1768 /* Of course we have races here, but they cannot be avoided on POSIX
1769 * (except by even *more* actions) */
1771 _ncl_term_mode(TRU1
);
1772 nn
= _ncl_readline(prompt
, linebuf
, linesize
, n SMALLOC_DEBUG_ARGSCALL
);
1773 _ncl_term_mode(FAL0
);
1780 tty_addhist(char const *s
)
1782 # ifdef HAVE_HISTORY
1783 /* Super-Heavy-Metal: block all sigs, avoid leaks+ on jump */
1784 size_t l
= strlen(s
);
1785 struct hist
*h
, *o
, *y
;
1787 if (_ncl_hist_size_max
== 0)
1789 _CL_CHECK_ADDHIST(s
, goto j_leave
);
1791 /* Eliminating duplicates is expensive, but simply inacceptable so
1792 * during the load of a potentially large history file! */
1793 if (!_ncl_hist_load
)
1794 for (h
= _ncl_hist
; h
!= NULL
; h
= h
->older
)
1795 if (h
->len
== l
&& !strcmp(h
->dat
, s
)) {
1796 hold_all_sigs(); /* TODO */
1812 if (!_ncl_hist_load
&& _ncl_hist_size
> _ncl_hist_size_max
) {
1814 if ((h
= _ncl_hist_tail
) != NULL
) {
1815 if ((_ncl_hist_tail
= h
->younger
) == NULL
)
1818 _ncl_hist_tail
->older
= NULL
;
1823 h
= smalloc((sizeof(struct hist
) - VFIELD_SIZEOF(struct hist
, dat
)) + l
+1);
1825 memcpy(h
->dat
, s
, l
+1);
1827 if ((h
->older
= _ncl_hist
) != NULL
)
1828 _ncl_hist
->younger
= h
;
1840 # ifdef HAVE_HISTORY
1852 if (_ncl_hist
== NULL
)
1855 if ((fp
= Ftemp(&cp
, "Ra", "w+", 0600, 1)) == NULL
) {
1865 for (h
= _ncl_hist
; h
!= NULL
; --i
, b
+= h
->len
, h
= h
->older
)
1866 fprintf(fp
, "%4lu. %-50.50s (%4lu+%lu bytes)\n",
1867 (ul_it
)i
, h
->dat
, (ul_it
)b
, (ul_it
)h
->len
);
1869 page_or_print(fp
, i
);
1876 while ((h
= _ncl_hist
) != NULL
) {
1877 _ncl_hist
= h
->older
;
1880 _ncl_hist_tail
= NULL
;
1886 struct hist
*h
= _ncl_hist
;
1887 if (UICMP(z
, entry
, <=, _ncl_hist_size
)) {
1888 entry
= (long)_ncl_hist_size
- entry
;
1889 for (h
= _ncl_hist
;; h
= h
->older
)
1892 else if (entry
-- != 0)
1895 v
= temporary_arg_v_store
= h
->dat
;
1903 # endif /* HAVE_HISTORY */
1904 #endif /* HAVE_NCL */
1907 * The really-nothing-at-all implementation
1910 #if !defined HAVE_READLINE && !defined HAVE_EDITLINE && !defined HAVE_NCL
1926 (tty_readline
)(char const *prompt
, char **linebuf
, size_t *linesize
, size_t n
1930 * TODO The nothing-at-all tty layer even forces re-entering all the
1931 * TODO original data when re-editing a field
1933 bool_t doffl
= FAL0
;
1935 if (prompt
!= NULL
&& *prompt
!= '\0') {
1936 fputs(prompt
, stdout
);
1940 fprintf(stdout
, tr(511, "{former content: %.*s} "), (int)n
, *linebuf
);
1946 return (readline_restart
)(stdin
, linebuf
, linesize
,n SMALLOC_DEBUG_ARGSCALL
);
1950 tty_addhist(char const *s
)
1954 #endif /* nothing at all */
1956 /* vim:set fenc=utf-8:s-it-mode */