1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ TTY (command line) editing interaction.
3 *@ Because we have (had) multiple line-editor implementations, including our
4 *@ own M(ailx) L(ine) E(ditor), change the file layout a bit and place those
5 *@ one after the other below the other externals.
7 * Copyright (c) 2012 - 2017 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
9 * Permission to use, copy, modify, and/or distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 #ifndef HAVE_AMALGAMATION
28 #if defined HAVE_MLE || defined HAVE_TERMCAP
29 # define a_TTY_SIGNALS
32 /* History support macros */
34 # define a_TTY_HISTFILE(S) \
36 char const *__hist_obsolete = ok_vlook(NAIL_HISTFILE);\
37 if(__hist_obsolete != NULL)\
38 n_OBSOLETE(_("please use *history-file* instead of *NAIL_HISTFILE*"));\
39 S = ok_vlook(history_file);\
41 (S) = __hist_obsolete;\
43 S = fexpand(S, FEXP_LOCAL | FEXP_NSHELL);\
46 # define a_TTY_HISTSIZE(V) \
48 char const *__hist_obsolete = ok_vlook(NAIL_HISTSIZE);\
49 char const *__sv = ok_vlook(history_size);\
51 if(__hist_obsolete != NULL)\
52 n_OBSOLETE(_("please use *history-size* instead of *NAIL_HISTSIZE*"));\
54 __sv = __hist_obsolete;\
55 if(__sv == NULL || (__rv = strtol(__sv, NULL, 10)) == 0)\
62 # define a_TTY_CHECK_ADDHIST(S,ISGABBY,NOACT) \
64 if((!(n_psonce & n_PSO_LINE_EDITOR_INIT) && !(n_pstate & n_PS_ROOT)) ||\
65 ok_blook(line_editor_disable) ||\
66 ((ISGABBY) && !ok_blook(history_gabby)) ||\
67 spacechar(*(S)) || *(S) == '\0')\
71 # define C_HISTORY_SHARED \
76 if(ok_blook(line_editor_disable)){\
77 n_err(_("history: *line-editor-disable* is set\n"));\
80 if(!(n_psonce & n_PSO_LINE_EDITOR_INIT)){\
82 assert(n_psonce & n_PSO_LINE_EDITOR_INIT);\
88 if(!asccasecmp(*argv, "show"))\
90 if(!asccasecmp(*argv, "clear"))\
92 if((entry = strtol(*argv, argv, 10)) > 0 && **argv == '\0')\
95 n_err(_("Synopsis: history: %s\n"),\
96 /* Same string as in cmd_tab.h, still hoping...) */\
97 _("<show> (default), <clear> or select <NO> from editor history"));\
101 return (v == NULL ? !STOP : !OKAY); /* xxx 1:bad 0:good -- do some */
102 #endif /* HAVE_HISTORY */
105 static sighandler_type a_tty_oint
, a_tty_oquit
, a_tty_oterm
,
107 a_tty_otstp
, a_tty_ottin
, a_tty_ottou
;
111 static void a_tty_sigs_up(void), a_tty_sigs_down(void);
122 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
123 a_tty_oint
= safe_signal(SIGINT
, &n_tty_signal
);
124 a_tty_oquit
= safe_signal(SIGQUIT
, &n_tty_signal
);
125 a_tty_oterm
= safe_signal(SIGTERM
, &n_tty_signal
);
126 a_tty_ohup
= safe_signal(SIGHUP
, &n_tty_signal
);
127 a_tty_otstp
= safe_signal(SIGTSTP
, &n_tty_signal
);
128 a_tty_ottin
= safe_signal(SIGTTIN
, &n_tty_signal
);
129 a_tty_ottou
= safe_signal(SIGTTOU
, &n_tty_signal
);
130 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
135 a_tty_sigs_down(void){
141 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
142 safe_signal(SIGINT
, a_tty_oint
);
143 safe_signal(SIGQUIT
, a_tty_oquit
);
144 safe_signal(SIGTERM
, a_tty_oterm
);
145 safe_signal(SIGHUP
, a_tty_ohup
);
146 safe_signal(SIGTSTP
, a_tty_otstp
);
147 safe_signal(SIGTTIN
, a_tty_ottin
);
148 safe_signal(SIGTTOU
, a_tty_ottou
);
149 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
152 #endif /* a_TTY_SIGNALS */
154 static sigjmp_buf a_tty__actjmp
; /* TODO someday, we won't need it no more */
156 a_tty__acthdl(int s
) /* TODO someday, we won't need it no more */
158 NYD_X
; /* Signal handler */
159 termios_state_reset();
160 siglongjmp(a_tty__actjmp
, s
);
164 getapproval(char const * volatile prompt
, bool_t noninteract_default
)
166 sighandler_type
volatile oint
, ohup
;
171 if(!(n_psonce
& n_PSO_INTERACTIVE
)){
173 rv
= noninteract_default
;
179 char const *quest
= noninteract_default
180 ? _("[yes]/no? ") : _("[no]/yes? ");
183 prompt
= _("Continue");
184 prompt
= savecatsep(prompt
, ' ', quest
);
187 oint
= safe_signal(SIGINT
, SIG_IGN
);
188 ohup
= safe_signal(SIGHUP
, SIG_IGN
);
189 if ((sig
= sigsetjmp(a_tty__actjmp
, 1)) != 0)
191 safe_signal(SIGINT
, &a_tty__acthdl
);
192 safe_signal(SIGHUP
, &a_tty__acthdl
);
194 if (n_lex_input(n_LEXINPUT_CTX_DEFAULT
| n_LEXINPUT_NL_ESC
, prompt
,
195 &termios_state
.ts_linebuf
, &termios_state
.ts_linesize
, NULL
) >= 0)
196 rv
= (boolify(termios_state
.ts_linebuf
, UIZ_MAX
,
197 noninteract_default
) > 0);
199 termios_state_reset();
201 safe_signal(SIGHUP
, ohup
);
202 safe_signal(SIGINT
, oint
);
212 getuser(char const * volatile query
) /* TODO v15-compat obsolete */
214 sighandler_type
volatile oint
, ohup
;
215 char * volatile user
= NULL
;
222 oint
= safe_signal(SIGINT
, SIG_IGN
);
223 ohup
= safe_signal(SIGHUP
, SIG_IGN
);
224 if ((sig
= sigsetjmp(a_tty__actjmp
, 1)) != 0)
226 safe_signal(SIGINT
, &a_tty__acthdl
);
227 safe_signal(SIGHUP
, &a_tty__acthdl
);
229 if (n_lex_input(n_LEXINPUT_CTX_DEFAULT
| n_LEXINPUT_NL_ESC
, query
,
230 &termios_state
.ts_linebuf
, &termios_state
.ts_linesize
, NULL
) >= 0)
231 user
= termios_state
.ts_linebuf
;
233 termios_state_reset();
235 safe_signal(SIGHUP
, ohup
);
236 safe_signal(SIGINT
, oint
);
244 getpassword(char const *query
)/* TODO v15: use _only_ n_tty_fp! */
246 sighandler_type
volatile oint
, ohup
;
248 char * volatile pass
;
253 if(!(n_psonce
& n_PSO_TTYIN
))
257 query
= _("Password: ");
258 fputs(query
, n_tty_fp
);
261 /* FIXME everywhere: tcsetattr() generates SIGTTOU when we're not in
262 * FIXME foreground pgrp, and can fail with EINTR!! also affects
263 * FIXME termios_state_reset() */
264 tcgetattr(STDIN_FILENO
, &termios_state
.ts_tios
);
265 memcpy(&tios
, &termios_state
.ts_tios
, sizeof tios
);
266 termios_state
.ts_needs_reset
= TRU1
;
267 tios
.c_iflag
&= ~(ISTRIP
);
268 tios
.c_lflag
&= ~(ECHO
| ECHOE
| ECHOK
| ECHONL
);
270 oint
= safe_signal(SIGINT
, SIG_IGN
);
271 ohup
= safe_signal(SIGHUP
, SIG_IGN
);
272 if ((sig
= sigsetjmp(a_tty__actjmp
, 1)) != 0)
274 safe_signal(SIGINT
, &a_tty__acthdl
);
275 safe_signal(SIGHUP
, &a_tty__acthdl
);
277 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &tios
);
278 if (readline_restart(n_stdin
, &termios_state
.ts_linebuf
,
279 &termios_state
.ts_linesize
, 0) >= 0)
280 pass
= termios_state
.ts_linebuf
;
282 termios_state_reset();
283 putc('\n', n_tty_fp
);
285 safe_signal(SIGHUP
, ohup
);
286 safe_signal(SIGINT
, oint
);
293 #endif /* HAVE_SOCKETS */
296 n_tty_create_prompt(struct n_string
*store
, char const *xprompt
,
297 enum n_lexinput_flags lif
){
298 struct n_visual_info_ctx vic
;
304 /* Prompt creation indicates that prompt printing is directly ahead, so take
305 * this opportunity of UI-in-a-known-state and advertise the error ring */
307 if((n_psonce
& (n_PSO_INTERACTIVE
| n_PSO_ERRORS_NOTED
)
308 ) == n_PSO_INTERACTIVE
&& (n_pstate
& n_PS_ERRORS_PROMPT
)){
309 n_psonce
|= n_PSO_ERRORS_NOTED
;
310 fprintf(n_stdout
, _("There are new messages in the error message ring "
312 " The `errors' command manages this message ring\n"),
318 n_string_trunc(store
, 0);
320 if(lif
& n_LEXINPUT_PROMPT_NONE
){
325 if(n_pstate
& n_PS_ERRORS_PROMPT
){
326 n_pstate
&= ~n_PS_ERRORS_PROMPT
;
327 store
= n_string_push_cp(store
, V_(n_error
));
328 store
= n_string_push_c(store
, '#');
329 store
= n_string_push_c(store
, ' ');
333 cp
= (lif
& n_LEXINPUT_PROMPT_EVAL
)
334 ? (lif
& n_LEXINPUT_NL_FOLLOW
? ok_vlook(prompt2
) : ok_vlook(prompt
))
336 if(cp
!= NULL
&& *cp
!= '\0'){
337 enum n_shexp_state shs
;
339 store
= n_string_push_cp(store
, cp
);
340 in
.s
= n_string_cp(store
);
343 store
= n_string_drop_ownership(store
);
345 shs
= n_shexp_parse_token(store
, &in
, n_SHEXP_PARSE_LOG
|
346 n_SHEXP_PARSE_IGNORE_EMPTY
| n_SHEXP_PARSE_QUOTE_AUTO_FIXED
|
347 n_SHEXP_PARSE_QUOTE_AUTO_DSQ
);
348 if((shs
& n_SHEXP_STATE_ERR_MASK
) || !(shs
& n_SHEXP_STATE_STOP
)){
349 store
= n_string_clear(store
);
350 store
= n_string_take_ownership(store
, out
.s
, out
.l
+1, out
.l
);
352 n_err(_("*prompt2?* evaluation failed, actively unsetting it\n"));
353 if(lif
& n_LEXINPUT_NL_FOLLOW
)
364 /* Make all printable TODO not know, we want to pass through ESC/CSI! */
366 in
.s
= n_string_cp(store
);
368 makeprint(&in
, &out
);
369 store
= n_string_assign_buf(store
, out
.s
, out
.l
);
373 /* We need the visual width.. */
374 memset(&vic
, 0, sizeof vic
);
375 vic
.vic_indat
= n_string_cp(store
);
376 vic
.vic_inlen
= store
->s_len
;
377 for(pwidth
= 0; vic
.vic_inlen
> 0;){
378 /* but \[ .. \] is not taken into account */
379 if(vic
.vic_indat
[0] == '\\' && vic
.vic_inlen
> 1 &&
380 vic
.vic_indat
[1] == '['){
383 i
= PTR2SIZE(vic
.vic_indat
- store
->s_dat
);
384 store
= n_string_cut(store
, i
, 2);
385 cp
= &n_string_cp(store
)[i
];
386 i
= store
->s_len
- i
;
389 n_err(_("Open \\[ sequence not closed in *prompt2?*\n"));
392 if(cp
[0] == '\\' && cp
[1] == ']')
395 i
= PTR2SIZE(cp
- store
->s_dat
);
396 store
= n_string_cut(store
, i
, 2);
397 vic
.vic_indat
= &n_string_cp(store
)[i
];
398 vic
.vic_inlen
= store
->s_len
- i
;
399 }else if(!n_visual_info(&vic
, n_VISUAL_INFO_WIDTH_QUERY
|
400 n_VISUAL_INFO_ONE_CHAR
)){
401 n_err(_("Character set error in evaluation of *prompt2?*\n"));
404 pwidth
+= (ui32_t
)vic
.vic_vi_width
;
405 vic
.vic_indat
= vic
.vic_oudat
;
406 vic
.vic_inlen
= vic
.vic_oulen
;
410 /* And there may be colour support, too */
413 struct str
const *psp
, *rsp
;
414 struct n_colour_pen
*ccp
;
416 if((ccp
= n_colour_pen_create(n_COLOUR_ID_MLE_PROMPT
, NULL
)) != NULL
&&
417 (psp
= n_colour_pen_to_str(ccp
)) != NULL
&&
418 (rsp
= n_colour_reset_to_str()) != NULL
){
419 store
= n_string_unshift_buf(store
, psp
->s
, psp
->l
);
420 store
= n_string_push_buf(store
, rsp
->s
, rsp
->l
);
423 #endif /* HAVE_COLOUR */
431 * MLE: the Mailx-Line-Editor, our homebrew editor
432 * (inspired from NetBSDs sh(1) and dash(1)s hetio.c).
434 * Only used in interactive mode.
435 * TODO . This code should be splitted in funs/raw input/bind modules.
436 * TODO . We work with wide characters, but not for buffer takeovers and
437 * TODO cell2save()ings. This should be changed. For the former the buffer
438 * TODO thus needs to be converted to wide first, and then simply be fed in.
439 * TODO . We repaint too much. To overcome this use the same approach that my
440 * TODO terminal library uses, add a true "virtual screen line" that stores
441 * TODO the actually visible content, keep a notion of "first modified slot"
442 * TODO and "last modified slot" (including "unknown" and "any" specials),
443 * TODO update that virtual instead, then synchronize what has truly changed.
444 * TODO I.e., add an indirection layer.
445 * TODO . No BIDI support.
446 * TODO . `bind': we currently use only one lookup tree.
447 * TODO For absolute graceful behaviour in conjunction (with HAVE_TERMCAP) we
448 * TODO need a lower level tree, which possibly combines bytes into "symbolic
449 * TODO wchar_t values", into "keys" that is, as applicable, and an upper
450 * TODO layer which only works on "keys" in order to possibly combine them
451 * TODO into key sequences. We can reuse existent tree code for that.
452 * TODO We need an additional hashmap which maps termcap/terminfo names to
453 * TODO (their byte representations and) a dynamically assigned unique
454 * TODO "symbolic wchar_t value". This implies we may have incompatibilities
455 * TODO when __STDC_ISO_10646__ is not defined. Also we do need takeover-
456 * TODO bytes storage, but it can be a string_creat_auto in the line struct.
457 * TODO Until then we can run into ambiguities; in rare occasions.
460 /* To avoid memory leaks etc. with the current codebase that simply longjmp(3)s
461 * we're forced to use the very same buffer--the one that is passed through to
462 * us from the outside--to store anything we need, i.e., a "struct cell[]", and
463 * convert that on-the-fly back to the plain char* result once we're done.
464 * To simplify our live, use savestr() buffers for all other needed memory */
466 # ifdef HAVE_KEY_BINDINGS
467 /* Default *bind-timeout* key-sequence continuation timeout, in tenths of
468 * a second. Must fit in 8-bit! Update the manual upon change! */
469 # define a_TTY_BIND_TIMEOUT 2
470 # define a_TTY_BIND_TIMEOUT_MAX SI8_MAX
472 n_CTAV(a_TTY_BIND_TIMEOUT_MAX
<= UI8_MAX
);
474 /* We have a chicken-and-egg problem with `bind' and our termcap layer,
475 * because we may not initialize the latter automatically to allow users to
476 * specify *termcap-disable* and let it mean exactly that.
477 * On the other hand users can be expected to use `bind' in resource file(s).
478 * Therefore bindings which involve termcap/terminfo sequences, and which are
479 * defined before n_PSO_STARTED signals usability of termcap/terminfo, will be
480 * (partially) delayed until tty_init() is called.
481 * And we preallocate space for the expansion of the resolved capability */
482 # define a_TTY_BIND_CAPNAME_MAX 15
483 # define a_TTY_BIND_CAPEXP_ROUNDUP 16
485 n_CTAV(n_ISPOW2(a_TTY_BIND_CAPEXP_ROUNDUP
));
486 n_CTA(a_TTY_BIND_CAPEXP_ROUNDUP
<= SI8_MAX
/ 2, "Variable must fit in 6-bit");
487 n_CTA(a_TTY_BIND_CAPEXP_ROUNDUP
>= 8, "Variable too small");
488 # endif /* HAVE_KEY_BINDINGS */
490 /* The maximum size (of a_tty_cell's) in a line */
491 # define a_TTY_LINE_MAX SI32_MAX
493 /* (Some more CTAs around) */
494 n_CTA(a_TTY_LINE_MAX
<= SI32_MAX
,
495 "a_TTY_LINE_MAX larger than SI32_MAX, but the MLE uses 32-bit arithmetic");
497 /* When shall the visual screen be scrolled, in % of usable screen width */
498 # define a_TTY_SCROLL_MARGIN_LEFT 15
499 # define a_TTY_SCROLL_MARGIN_RIGHT 10
501 /* fexpand() flags for expand-on-tab */
502 # define a_TTY_TAB_FEXP_FL (FEXP_FULL | FEXP_SILENT | FEXP_MULTIOK)
504 /* Columns to ripoff: outermost may not be touched, plus position indicator.
505 * Must thus be at least 1, but should be >= 1+4 to dig the position indicator
506 * that we place (if there is sufficient space) */
507 # define a_TTY_WIDTH_RIPOFF 5
509 /* The implementation of the MLE functions always exists, and is based upon
510 * the a_TTY_BIND_FUN_* constants, so most of this enum is always necessary */
511 enum a_tty_bind_flags
{
512 # ifdef HAVE_KEY_BINDINGS
513 a_TTY_BIND_RESOLVE
= 1u<<8, /* Term cap. yet needs to be resolved */
514 a_TTY_BIND_DEFUNCT
= 1u<<9, /* Unicode/term cap. used but not avail. */
515 a_TTY__BIND_MASK
= a_TTY_BIND_RESOLVE
| a_TTY_BIND_DEFUNCT
,
516 /* MLE fun assigned to a one-byte-sequence: this may be used for special
517 * key-sequence bypass processing */
518 a_TTY_BIND_MLE1CNTRL
= 1u<<10,
519 a_TTY_BIND_NOCOMMIT
= 1u<<11, /* Expansion shall be editable */
522 /* MLE internal commands */
523 a_TTY_BIND_FUN_INTERNAL
= 1u<<15,
524 a_TTY__BIND_FUN_SHIFT
= 16u,
525 a_TTY__BIND_FUN_SHIFTMAX
= 24u,
526 a_TTY__BIND_FUN_MASK
= ((1u << a_TTY__BIND_FUN_SHIFTMAX
) - 1) &
527 ~((1u << a_TTY__BIND_FUN_SHIFT
) - 1),
528 # define a_TTY_BIND_FUN_REDUCE(X) \
529 (((ui32_t)(X) & a_TTY__BIND_FUN_MASK) >> a_TTY__BIND_FUN_SHIFT)
530 # define a_TTY_BIND_FUN_EXPAND(X) \
531 (((ui32_t)(X) & (a_TTY__BIND_FUN_MASK >> a_TTY__BIND_FUN_SHIFT)) << \
532 a_TTY__BIND_FUN_SHIFT)
535 a_TTY_BIND_FUN_ ## N = a_TTY_BIND_FUN_EXPAND(I),
538 a_X(GO_BWD
, 1) a_X(GO_FWD
, 2)
539 a_X(GO_WORD_BWD
, 3) a_X(GO_WORD_FWD
, 4)
540 a_X(GO_HOME
, 5) a_X(GO_END
, 6)
541 a_X(DEL_BWD
, 7) a_X(DEL_FWD
, 8)
542 a_X(SNARF_WORD_BWD
, 9) a_X(SNARF_WORD_FWD
, 10)
543 a_X(SNARF_END
, 11) a_X(SNARF_LINE
, 12)
544 a_X(HIST_BWD
, 13) a_X(HIST_FWD
, 14)
545 a_X(HIST_SRCH_BWD
, 15) a_X(HIST_SRCH_FWD
, 16)
547 a_X(QUOTE_RNDTRIP
, 18)
555 a_X(COMMIT
, 25) /* Must be last one! */
558 a_TTY__BIND_LAST
= 1<<25
560 # ifdef HAVE_KEY_BINDINGS
561 n_CTA((ui32_t
)a_TTY_BIND_RESOLVE
>= (ui32_t
)n__LEXINPUT_CTX_MAX1
,
562 "Bit carrier lower boundary must be raised to avoid value sharing");
564 n_CTA(a_TTY_BIND_FUN_EXPAND(a_TTY_BIND_FUN_COMMIT
) <
565 (1 << a_TTY__BIND_FUN_SHIFTMAX
),
566 "Bit carrier range must be expanded to represent necessary bits");
567 n_CTA(a_TTY__BIND_LAST
>= (1u << a_TTY__BIND_FUN_SHIFTMAX
),
568 "Bit carrier upper boundary must be raised to avoid value sharing");
569 n_CTA(UICMP(64, a_TTY__BIND_LAST
, <=, SI32_MAX
),
570 "Flag bits excess storage datatype" /* And we need one bit free */);
572 enum a_tty_fun_status
{
573 a_TTY_FUN_STATUS_OK
, /* Worked, next character */
574 a_TTY_FUN_STATUS_COMMIT
, /* Line done */
575 a_TTY_FUN_STATUS_RESTART
, /* Complete restart, reset multibyte etc. */
576 a_TTY_FUN_STATUS_END
/* End, return EOF */
579 enum a_tty_visual_flags
{
581 a_TTY_VF_MOD_CURSOR
= 1u<<0, /* Cursor moved */
582 a_TTY_VF_MOD_CONTENT
= 1u<<1, /* Content modified */
583 a_TTY_VF_MOD_DIRTY
= 1u<<2, /* Needs complete repaint */
584 a_TTY_VF_MOD_SINGLE
= 1u<<3, /* TODO Drop when indirection as above comes */
585 a_TTY_VF_REFRESH
= a_TTY_VF_MOD_DIRTY
| a_TTY_VF_MOD_CURSOR
|
586 a_TTY_VF_MOD_CONTENT
| a_TTY_VF_MOD_SINGLE
,
587 a_TTY_VF_BELL
= 1u<<8, /* Ring the bell */
588 a_TTY_VF_SYNC
= 1u<<9, /* Flush/Sync I/O channel */
590 a_TTY_VF_ALL_MASK
= a_TTY_VF_REFRESH
| a_TTY_VF_BELL
| a_TTY_VF_SYNC
,
591 a_TTY__VF_LAST
= a_TTY_VF_SYNC
594 # ifdef HAVE_KEY_BINDINGS
595 struct a_tty_bind_ctx
{
596 struct a_tty_bind_ctx
*tbc_next
;
597 char *tbc_seq
; /* quence as given (poss. re-quoted), in .tb__buf */
598 char *tbc_exp
; /* ansion, in .tb__buf */
599 /* The .tbc_seq'uence with any terminal capabilities resolved; in fact an
600 * array of structures, the first entry of which is {si32_t buf_len_iscap;}
601 * where the signed bit indicates whether the buffer is a resolved terminal
602 * capability instead of a (possibly multibyte) character. In .tbc__buf */
608 char tbc__buf
[n_VFIELD_SIZE(0)];
611 struct a_tty_bind_ctx_map
{
612 enum n_lexinput_flags tbcm_ctx
;
613 char const tbcm_name
[12]; /* Name of `bind' context */
615 # endif /* HAVE_KEY_BINDINGS */
617 struct a_tty_bind_builtin_tuple
{
618 bool_t tbbt_iskey
; /* Whether this is a control key; else termcap query */
619 char tbbt_ckey
; /* Control code */
620 ui16_t tbbt_query
; /* enum n_termcap_query (instead) */
621 char tbbt_exp
[12]; /* String or [0]=NUL/[1]=BIND_FUN_REDUCE() */
623 n_CTA(n__TERMCAP_QUERY_MAX1
<= UI16_MAX
,
624 "Enumeration cannot be stored in datatype");
626 # ifdef HAVE_KEY_BINDINGS
627 struct a_tty_bind_parse_ctx
{
628 char const *tbpc_cmd
; /* Command which parses */
629 char const *tbpc_in_seq
; /* In: key sequence */
630 struct str tbpc_exp
; /* In/Out: expansion (or NULL) */
631 struct a_tty_bind_ctx
*tbpc_tbcp
; /* Out: if yet existent */
632 struct a_tty_bind_ctx
*tbpc_ltbcp
; /* Out: the one before .tbpc_tbcp */
633 char *tbpc_seq
; /* Out: normalized sequence */
634 char *tbpc_cnv
; /* Out: sequence when read(2)ing it */
637 ui32_t tbpc_cnv_align_mask
; /* For creating a_tty_bind_ctx.tbc_cnv */
638 ui32_t tbpc_flags
; /* n_lexinput_flags | a_tty_bind_flags */
641 /* Input character tree */
642 struct a_tty_bind_tree
{
643 struct a_tty_bind_tree
*tbt_sibling
; /* s at same level */
644 struct a_tty_bind_tree
*tbt_childs
; /* Sequence continues.. here */
645 struct a_tty_bind_tree
*tbt_parent
;
646 struct a_tty_bind_ctx
*tbt_bind
; /* NULL for intermediates */
647 wchar_t tbt_char
; /* acter this level represents */
648 bool_t tbt_isseq
; /* Belongs to multibyte sequence */
649 bool_t tbt_isseq_trail
; /* ..is trailing byte of it */
652 # endif /* HAVE_KEY_BINDINGS */
656 ui16_t tc_count
; /* ..of bytes */
657 ui8_t tc_width
; /* Visual width; TAB==UI8_MAX! */
658 bool_t tc_novis
; /* Don't display visually as such (control character) */
659 char tc_cbuf
[MB_LEN_MAX
* 2]; /* .. plus reset shift sequence */
663 struct a_tty_line
*tg_line
; /* To be able to access it from signal hdl */
665 struct a_tty_hist
*tg_hist
;
666 struct a_tty_hist
*tg_hist_tail
;
668 size_t tg_hist_size_max
;
670 # ifdef HAVE_KEY_BINDINGS
671 ui32_t tg_bind_cnt
; /* Overall number of bindings */
672 bool_t tg_bind_isdirty
;
673 bool_t tg_bind_isbuild
;
674 # define a_TTY_SHCUT_MAX (3 +1) /* Note: update manual on change! */
675 ui8_t tg_bind__dummy
[2];
676 char tg_bind_shcut_cancel
[n__LEXINPUT_CTX_MAX1
][a_TTY_SHCUT_MAX
];
677 char tg_bind_shcut_prompt_char
[n__LEXINPUT_CTX_MAX1
][a_TTY_SHCUT_MAX
];
678 struct a_tty_bind_ctx
*tg_bind
[n__LEXINPUT_CTX_MAX1
];
679 struct a_tty_bind_tree
*tg_bind_tree
[n__LEXINPUT_CTX_MAX1
][HSHSIZE
];
681 struct termios tg_tios_old
;
682 struct termios tg_tios_new
;
684 # ifdef HAVE_KEY_BINDINGS
685 n_CTA(n__LEXINPUT_CTX_MAX1
== 3 && a_TTY_SHCUT_MAX
== 4 &&
686 n_SIZEOF_FIELD(struct a_tty_global
, tg_bind__dummy
) == 2,
687 "Value results in array sizes that results in bad structure layout");
688 n_CTA(a_TTY_SHCUT_MAX
> 1,
689 "Users need at least one shortcut, plus NUL terminator");
694 struct a_tty_hist
*th_older
;
695 struct a_tty_hist
*th_younger
;
696 ui32_t th_isgabby
: 1;
698 char th_dat
[n_VFIELD_SIZE(sizeof(ui32_t
))];
703 /* Caller pointers */
705 size_t *tl_x_bufsize
;
706 /* Input processing */
707 # ifdef HAVE_KEY_BINDINGS
708 wchar_t tl_bind_takeover
; /* Leftover byte to consume next */
709 ui8_t tl_bind_timeout
; /* In-seq. inter-byte-timer, in 1/10th secs */
710 ui8_t tl__bind_dummy
[3];
711 char (*tl_bind_shcut_cancel
)[a_TTY_SHCUT_MAX
]; /* Special _CANCEL control */
712 char (*tl_bind_shcut_prompt_char
)[a_TTY_SHCUT_MAX
]; /* ..for _PROMPT_CHAR */
713 struct a_tty_bind_tree
*(*tl_bind_tree_hmap
)[HSHSIZE
]; /* Bind lookup tree */
714 struct a_tty_bind_tree
*tl_bind_tree
;
716 char const *tl_reenter_after_cmd
; /* `bind' cmd to exec, then re-readline */
717 /* Line data / content handling */
718 ui32_t tl_count
; /* ..of a_tty_cell's (<= a_TTY_LINE_MAX) */
719 ui32_t tl_cursor
; /* Current a_tty_cell insertion point */
721 char *cbuf
; /* *.tl_x_buf */
722 struct a_tty_cell
*cells
;
724 struct str tl_defc
; /* Current default content */
725 size_t tl_defc_cursor_byte
; /* Desired position of cursor after takeover */
726 struct str tl_savec
; /* Saved default content */
727 struct str tl_pastebuf
; /* Last snarfed data */
729 struct a_tty_hist
*tl_hist
; /* History cursor */
731 ui32_t tl_count_max
; /* ..before buffer needs to grow */
732 /* Visual data representation handling */
733 ui32_t tl_vi_flags
; /* enum a_tty_visual_flags */
734 ui32_t tl_lst_count
; /* .tl_count after last sync */
735 ui32_t tl_lst_cursor
; /* .tl_cursor after last sync */
736 /* TODO Add another indirection layer by adding a tl_phy_line of
737 * TODO a_tty_cell objects, incorporate changes in visual layer,
738 * TODO then check what _really_ has changed, sync those changes only */
739 struct a_tty_cell
const *tl_phy_start
; /* First visible cell, left border */
740 ui32_t tl_phy_cursor
; /* Physical cursor position */
741 bool_t tl_quote_rndtrip
; /* For _kht() expansion */
743 ui32_t tl_prompt_length
; /* Preclassified (TODO needed as a_tty_cell) */
744 ui32_t tl_prompt_width
;
745 char const *tl_prompt
; /* Preformatted prompt (including colours) */
746 /* .tl_pos_buf is a hack */
748 char *tl_pos_buf
; /* mle-position colour-on, [4], reset seq. */
749 char *tl_pos
; /* Address of the [4] */
753 # ifdef HAVE_KEY_BINDINGS
754 /* C99: use [INDEX]={} */
755 n_CTAV(n_LEXINPUT_CTX_BASE
== 0);
756 n_CTAV(n_LEXINPUT_CTX_DEFAULT
== 1);
757 n_CTAV(n_LEXINPUT_CTX_COMPOSE
== 2);
758 static struct a_tty_bind_ctx_map
const
759 a_tty_bind_ctx_maps
[n__LEXINPUT_CTX_MAX1
] = {
760 {n_LEXINPUT_CTX_BASE
, "base"},
761 {n_LEXINPUT_CTX_DEFAULT
, "default"},
762 {n_LEXINPUT_CTX_COMPOSE
, "compose"}
765 /* Special functions which our MLE provides internally.
766 * Update the manual upon change! */
767 static char const a_tty_bind_fun_names
[][24] = {
770 n_FIELD_INITI(a_TTY_BIND_FUN_REDUCE(a_TTY_BIND_FUN_ ## I)) "mle-" N "\0",
773 a_X(GO_BWD
, "go-bwd") a_X(GO_FWD
, "go-fwd")
774 a_X(GO_WORD_BWD
, "go-word-bwd") a_X(GO_WORD_FWD
, "go-word-fwd")
775 a_X(GO_HOME
, "go-home") a_X(GO_END
, "go-end")
776 a_X(DEL_BWD
, "del-bwd") a_X(DEL_FWD
, "del-fwd")
777 a_X(SNARF_WORD_BWD
, "snarf-word-bwd") a_X(SNARF_WORD_FWD
, "snarf-word-fwd")
778 a_X(SNARF_END
, "snarf-end") a_X(SNARF_LINE
, "snarf-line")
779 a_X(HIST_BWD
, "hist-bwd") a_X(HIST_FWD
, "hist-fwd")
780 a_X(HIST_SRCH_BWD
, "hist-srch-bwd") a_X(HIST_SRCH_FWD
, "hist-srch-fwd")
781 a_X(REPAINT
, "repaint")
782 a_X(QUOTE_RNDTRIP
, "quote-rndtrip")
783 a_X(PROMPT_CHAR
, "prompt-char")
784 a_X(COMPLETE
, "complete")
787 a_X(CANCEL
, "cancel")
789 a_X(FULLRESET
, "fullreset")
790 a_X(COMMIT
, "commit")
794 # endif /* HAVE_KEY_BINDINGS */
796 /* The default key bindings (unless disallowed). Update manual upon change!
797 * A logical subset of this table is also used if !HAVE_KEY_BINDINGS (more
798 * expensive than a switch() on control codes directly, but less redundant).
799 * The table for the "base" context */
800 static struct a_tty_bind_builtin_tuple
const a_tty_bind_base_tuples
[] = {
803 {TRU1, K, 0, {'\0', (char)a_TTY_BIND_FUN_REDUCE(a_TTY_BIND_FUN_ ## S),}},
821 a_X('Q', QUOTE_RNDTRIP
)
822 a_X('R', HIST_SRCH_BWD
)
823 a_X('S', HIST_SRCH_FWD
)
826 a_X('V', PROMPT_CHAR
)
827 a_X('W', SNARF_WORD_BWD
)
828 a_X('X', GO_WORD_FWD
)
829 a_X('Y', GO_WORD_BWD
)
836 a_X('_', SNARF_WORD_FWD
)
841 # define a_X(K,S) {TRU1, K, 0, {S}},
843 /* The remains only if we have `bind' functionality available */
844 # ifdef HAVE_KEY_BINDINGS
847 {FAL0, '\0', n_TERMCAP_QUERY_ ## Q,\
848 {'\0', (char)a_TTY_BIND_FUN_REDUCE(a_TTY_BIND_FUN_ ## S),}},
850 a_X(key_backspace
, DEL_BWD
) a_X(key_dc
, DEL_FWD
)
851 a_X(key_eol
, SNARF_END
)
852 a_X(key_home
, GO_HOME
) a_X(key_end
, GO_END
)
853 a_X(key_left
, GO_BWD
) a_X(key_right
, GO_FWD
)
854 a_X(key_sleft
, GO_HOME
) a_X(key_sright
, GO_END
)
855 a_X(key_up
, HIST_BWD
) a_X(key_down
, HIST_FWD
)
856 # endif /* HAVE_KEY_BINDINGS */
859 /* The table for the "default" context */
860 static struct a_tty_bind_builtin_tuple
const a_tty_bind_default_tuples
[] = {
863 {TRU1, K, 0, {'\0', (char)a_TTY_BIND_FUN_REDUCE(a_TTY_BIND_FUN_ ## S),}},
866 # define a_X(K,S) {TRU1, K, 0, {S}},
874 /* The remains only if we have `bind' functionality available */
875 # ifdef HAVE_KEY_BINDINGS
877 # define a_X(Q,S) {FAL0, '\0', n_TERMCAP_QUERY_ ## Q, {S}},
879 a_X(key_shome
, "z0") a_X(key_send
, "z$")
880 a_X(xkey_sup
, "z0") a_X(xkey_sdown
, "z$")
881 a_X(key_ppage
, "z-") a_X(key_npage
, "z+")
882 a_X(xkey_cup
, "dotmove-") a_X(xkey_cdown
, "dotmove+")
883 # endif /* HAVE_KEY_BINDINGS */
887 static struct a_tty_global a_tty
;
889 /* Change from canonical to raw, non-canonical mode, and way back */
890 static void a_tty_term_mode(bool_t raw
);
892 /* Adjust an active raw mode to use / not use a timeout */
893 # ifdef HAVE_KEY_BINDINGS
894 static void a_tty_term_rawmode_timeout(struct a_tty_line
*tlp
, bool_t enable
);
897 /* 0-X (2), UI8_MAX == \t / TAB */
898 static ui8_t
a_tty_wcwidth(wchar_t wc
);
900 /* Memory / cell / word generics */
901 static void a_tty_check_grow(struct a_tty_line
*tlp
, ui32_t no
902 n_MEMORY_DEBUG_ARGS
);
903 static ssize_t
a_tty_cell2dat(struct a_tty_line
*tlp
);
904 static void a_tty_cell2save(struct a_tty_line
*tlp
);
906 /* Save away data bytes of given range (max = non-inclusive) */
907 static void a_tty_copy2paste(struct a_tty_line
*tlp
, struct a_tty_cell
*tcpmin
,
908 struct a_tty_cell
*tcpmax
);
910 /* Ask user for hexadecimal number, interpret as UTF-32 */
911 static wchar_t a_tty_vinuni(struct a_tty_line
*tlp
);
913 /* Visual screen synchronization */
914 static bool_t
a_tty_vi_refresh(struct a_tty_line
*tlp
);
916 static bool_t
a_tty_vi__paint(struct a_tty_line
*tlp
);
918 /* Search for word boundary, starting at tl_cursor, in "dir"ection (<> 0).
919 * Return <0 when moving is impossible (backward direction but in position 0,
920 * forward direction but in outermost column), and relative distance to
921 * tl_cursor otherwise */
922 static si32_t
a_tty_wboundary(struct a_tty_line
*tlp
, si32_t dir
);
924 /* Most function implementations */
925 static void a_tty_khome(struct a_tty_line
*tlp
, bool_t dobell
);
926 static void a_tty_kend(struct a_tty_line
*tlp
);
927 static void a_tty_kbs(struct a_tty_line
*tlp
);
928 static void a_tty_ksnarf(struct a_tty_line
*tlp
, bool_t cplline
, bool_t dobell
);
929 static si32_t
a_tty_kdel(struct a_tty_line
*tlp
);
930 static void a_tty_kleft(struct a_tty_line
*tlp
);
931 static void a_tty_kright(struct a_tty_line
*tlp
);
932 static void a_tty_ksnarfw(struct a_tty_line
*tlp
, bool_t fwd
);
933 static void a_tty_kgow(struct a_tty_line
*tlp
, si32_t dir
);
934 static bool_t
a_tty_kother(struct a_tty_line
*tlp
, wchar_t wc
);
935 static ui32_t
a_tty_kht(struct a_tty_line
*tlp
);
938 /* Return UI32_MAX on "exhaustion" */
939 static ui32_t
a_tty_khist(struct a_tty_line
*tlp
, bool_t fwd
);
940 static ui32_t
a_tty_khist_search(struct a_tty_line
*tlp
, bool_t fwd
);
942 static ui32_t
a_tty__khist_shared(struct a_tty_line
*tlp
,
943 struct a_tty_hist
*thp
);
946 /* Handle a function */
947 static enum a_tty_fun_status
a_tty_fun(struct a_tty_line
*tlp
,
948 enum a_tty_bind_flags tbf
, size_t *len
);
951 static ssize_t
a_tty_readline(struct a_tty_line
*tlp
, size_t len
952 n_MEMORY_DEBUG_ARGS
);
954 # ifdef HAVE_KEY_BINDINGS
955 /* Find context or -1 */
956 static enum n_lexinput_flags
a_tty_bind_ctx_find(char const *name
);
958 /* Create (or replace, if allowed) a binding */
959 static bool_t
a_tty_bind_create(struct a_tty_bind_parse_ctx
*tbpcp
,
962 /* Shared implementation to parse `bind' and `unbind' "key-sequence" and
963 * "expansion" command line arguments into something that we can work with */
964 static bool_t
a_tty_bind_parse(bool_t isbindcmd
,
965 struct a_tty_bind_parse_ctx
*tbpcp
);
967 /* Lazy resolve a termcap(5)/terminfo(5) (or *termcap*!) capability */
968 static void a_tty_bind_resolve(struct a_tty_bind_ctx
*tbcp
);
970 /* Delete an existing binding */
971 static void a_tty_bind_del(struct a_tty_bind_parse_ctx
*tbpcp
);
973 /* Life cycle of all input node trees */
974 static void a_tty_bind_tree_build(void);
975 static void a_tty_bind_tree_teardown(void);
977 static void a_tty__bind_tree_add(ui32_t hmap_idx
,
978 struct a_tty_bind_tree
*store
[HSHSIZE
],
979 struct a_tty_bind_ctx
*tbcp
);
980 static struct a_tty_bind_tree
*a_tty__bind_tree_add_wc(
981 struct a_tty_bind_tree
**treep
, struct a_tty_bind_tree
*parentp
,
982 wchar_t wc
, bool_t isseq
);
983 static void a_tty__bind_tree_free(struct a_tty_bind_tree
*tbtp
);
984 # endif /* HAVE_KEY_BINDINGS */
987 a_tty_term_mode(bool_t raw
){
988 struct termios
*tiosp
;
991 tiosp
= &a_tty
.tg_tios_old
;
995 /* Always requery the attributes, in case we've been moved from background
996 * to foreground or however else in between sessions */
997 /* XXX Always enforce ECHO and ICANON in the OLD attributes - do so as long
998 * XXX as we don't properly deal with TTIN and TTOU etc. */
999 tcgetattr(STDIN_FILENO
, tiosp
); /* TODO v15: use _only_ n_tty_fp! */
1000 tiosp
->c_lflag
|= ECHO
| ICANON
;
1002 memcpy(&a_tty
.tg_tios_new
, tiosp
, sizeof *tiosp
);
1003 tiosp
= &a_tty
.tg_tios_new
;
1004 tiosp
->c_cc
[VMIN
] = 1;
1005 tiosp
->c_cc
[VTIME
] = 0;
1006 /* Enable ^\, ^Q and ^S to be used for key bindings */
1007 tiosp
->c_cc
[VQUIT
] = tiosp
->c_cc
[VSTART
] = tiosp
->c_cc
[VSTOP
] = '\0';
1008 tiosp
->c_iflag
&= ~(ISTRIP
| IGNCR
);
1009 tiosp
->c_lflag
&= ~(ECHO
/*| ECHOE | ECHONL */| ICANON
| IEXTEN
);
1011 tcsetattr(STDIN_FILENO
, TCSADRAIN
, tiosp
);
1015 # ifdef HAVE_KEY_BINDINGS
1017 a_tty_term_rawmode_timeout(struct a_tty_line
*tlp
, bool_t enable
){
1022 a_tty
.tg_tios_new
.c_cc
[VMIN
] = 0;
1023 if((bt
= tlp
->tl_bind_timeout
) == 0)
1024 bt
= a_TTY_BIND_TIMEOUT
;
1025 a_tty
.tg_tios_new
.c_cc
[VTIME
] = bt
;
1027 a_tty
.tg_tios_new
.c_cc
[VMIN
] = 1;
1028 a_tty
.tg_tios_new
.c_cc
[VTIME
] = 0;
1030 tcsetattr(STDIN_FILENO
, TCSANOW
, &a_tty
.tg_tios_new
);
1033 # endif /* HAVE_KEY_BINDINGS */
1036 a_tty_wcwidth(wchar_t wc
){
1040 /* Special case the backslash at first */
1046 # ifdef HAVE_WCWIDTH
1047 rv
= ((i
= wcwidth(wc
)) > 0) ? (ui8_t
)i
: 0;
1049 rv
= iswprint(wc
) ? 1 + (wc
>= 0x1100u
) : 0; /* TODO use S-CText */
1057 a_tty_check_grow(struct a_tty_line
*tlp
, ui32_t no n_MEMORY_DEBUG_ARGS
){
1061 if(n_UNLIKELY((cmax
= tlp
->tl_count
+ no
) > tlp
->tl_count_max
)){
1064 i
= cmax
* sizeof(struct a_tty_cell
) + 2 * sizeof(struct a_tty_cell
);
1065 if(n_LIKELY(i
>= *tlp
->tl_x_bufsize
)){
1066 hold_all_sigs(); /* XXX v15 drop */
1069 *tlp
->tl_x_buf
= (n_realloc
)(*tlp
->tl_x_buf
, i
1070 n_MEMORY_DEBUG_ARGSCALL
);
1071 rele_all_sigs(); /* XXX v15 drop */
1073 tlp
->tl_count_max
= cmax
;
1074 *tlp
->tl_x_bufsize
= i
;
1080 a_tty_cell2dat(struct a_tty_line
*tlp
){
1086 if(n_LIKELY((i
= tlp
->tl_count
) > 0)){
1087 struct a_tty_cell
const *tcap
;
1089 tcap
= tlp
->tl_line
.cells
;
1091 memcpy(tlp
->tl_line
.cbuf
+ len
, tcap
->tc_cbuf
, tcap
->tc_count
);
1092 len
+= tcap
->tc_count
;
1093 }while(++tcap
, --i
> 0);
1096 tlp
->tl_line
.cbuf
[len
] = '\0';
1098 return (ssize_t
)len
;
1102 a_tty_cell2save(struct a_tty_line
*tlp
){
1104 struct a_tty_cell
*tcap
;
1107 tlp
->tl_savec
.s
= NULL
;
1108 tlp
->tl_savec
.l
= 0;
1110 if(n_UNLIKELY(tlp
->tl_count
== 0))
1113 for(tcap
= tlp
->tl_line
.cells
, len
= 0, i
= tlp
->tl_count
; i
> 0;
1115 len
+= tcap
->tc_count
;
1117 tlp
->tl_savec
.s
= salloc((tlp
->tl_savec
.l
= len
) +1);
1119 for(tcap
= tlp
->tl_line
.cells
, len
= 0, i
= tlp
->tl_count
; i
> 0;
1121 memcpy(tlp
->tl_savec
.s
+ len
, tcap
->tc_cbuf
, tcap
->tc_count
);
1122 len
+= tcap
->tc_count
;
1124 tlp
->tl_savec
.s
[len
] = '\0';
1130 a_tty_copy2paste(struct a_tty_line
*tlp
, struct a_tty_cell
*tcpmin
,
1131 struct a_tty_cell
*tcpmax
){
1133 struct a_tty_cell
*tcp
;
1138 for(tcp
= tcpmin
; tcp
< tcpmax
; ++tcp
)
1141 tlp
->tl_pastebuf
.s
= cp
= salloc((tlp
->tl_pastebuf
.l
= l
) +1);
1144 for(tcp
= tcpmin
; tcp
< tcpmax
; cp
+= l
, ++tcp
)
1145 memcpy(cp
, tcp
->tc_cbuf
, l
= tcp
->tc_count
);
1151 a_tty_vinuni(struct a_tty_line
*tlp
){
1152 char buf
[16], *eptr
;
1153 union {size_t i
; long l
;} u
;
1159 if(!n_termcap_cmdx(n_TERMCAP_CMD_cr
) ||
1160 !n_termcap_cmd(n_TERMCAP_CMD_ce
, 0, -1))
1164 struct str
const *cpre
, *csuf
;
1166 struct n_colour_pen
*cpen
;
1168 cpen
= n_colour_pen_create(n_COLOUR_ID_MLE_PROMPT
, NULL
);
1169 if((cpre
= n_colour_pen_to_str(cpen
)) != NULL
)
1170 csuf
= n_colour_reset_to_str();
1176 fprintf(n_tty_fp
, _("%sPlease enter Unicode code point:%s "),
1177 (cpre
!= NULL
? cpre
->s
: n_empty
),
1178 (csuf
!= NULL
? csuf
->s
: n_empty
));
1182 buf
[sizeof(buf
) -1] = '\0';
1184 if(read(STDIN_FILENO
, &buf
[u
.i
], 1) != 1){
1185 if(errno
== EINTR
) /* xxx #if !SA_RESTART ? */
1189 if(buf
[u
.i
] == '\n')
1191 if(!hexchar(buf
[u
.i
])){
1192 char const emsg
[] = "[0-9a-fA-F]";
1194 n_LCTA(sizeof emsg
<= sizeof(buf
), "Preallocated buffer too small");
1195 memcpy(buf
, emsg
, sizeof emsg
);
1199 putc(buf
[u
.i
], n_tty_fp
);
1201 if(++u
.i
== sizeof buf
)
1206 u
.l
= strtol(buf
, &eptr
, 16);
1207 if(u
.l
<= 0 || u
.l
>= 0x10FFFF/* XXX magic; CText */ || *eptr
!= '\0'){
1209 n_err(_("\nInvalid input: %s\n"), buf
);
1215 tlp
->tl_vi_flags
|= a_TTY_VF_MOD_DIRTY
| (wc
== '\0' ? a_TTY_VF_BELL
: 0);
1221 a_tty_vi_refresh(struct a_tty_line
*tlp
){
1225 if(tlp
->tl_vi_flags
& a_TTY_VF_BELL
){
1226 tlp
->tl_vi_flags
|= a_TTY_VF_SYNC
;
1227 if(putc('\a', n_tty_fp
) == EOF
)
1231 if(tlp
->tl_vi_flags
& a_TTY_VF_REFRESH
){
1232 /* kht may want to restore a cursor position after inserting some
1234 if(tlp
->tl_defc_cursor_byte
> 0){
1238 a_tty_khome(tlp
, FAL0
);
1240 i
= tlp
->tl_defc_cursor_byte
;
1241 tlp
->tl_defc_cursor_byte
= 0;
1242 for(j
= 0; tlp
->tl_cursor
< tlp
->tl_count
; ++j
){
1244 if((k
= tlp
->tl_line
.cells
[j
].tc_count
) > i
)
1250 if(!a_tty_vi__paint(tlp
))
1254 if(tlp
->tl_vi_flags
& a_TTY_VF_SYNC
){
1255 tlp
->tl_vi_flags
&= ~a_TTY_VF_SYNC
;
1256 if(fflush(n_tty_fp
))
1262 tlp
->tl_vi_flags
&= ~a_TTY_VF_ALL_MASK
;
1267 clearerr(n_tty_fp
); /* xxx I/O layer rewrite */
1268 n_err(_("Visual refresh failed! Is $TERM set correctly?\n"
1269 " Setting *line-editor-disable* to get us through!\n"));
1270 ok_bset(line_editor_disable
);
1276 a_tty_vi__paint(struct a_tty_line
*tlp
){
1278 a_TRUE_RV
= a_TTY__VF_LAST
<<1, /* Return value bit */
1279 a_HAVE_PROMPT
= a_TTY__VF_LAST
<<2, /* Have a prompt */
1280 a_SHOW_PROMPT
= a_TTY__VF_LAST
<<3, /* Shall print the prompt */
1281 a_MOVE_CURSOR
= a_TTY__VF_LAST
<<4, /* Move visual cursor for user! */
1282 a_LEFT_MIN
= a_TTY__VF_LAST
<<5, /* On left boundary */
1283 a_RIGHT_MAX
= a_TTY__VF_LAST
<<6,
1284 a_HAVE_POSITION
= a_TTY__VF_LAST
<<7, /* Print the position indicator */
1286 /* We carry some flags over invocations (not worth a specific field) */
1287 a_VISIBLE_PROMPT
= a_TTY__VF_LAST
<<8, /* The prompt is on the screen */
1288 a_PERSIST_MASK
= a_VISIBLE_PROMPT
,
1289 a__LAST
= a_PERSIST_MASK
1292 ui32_t f
, w
, phy_wid_base
, phy_wid
, phy_base
, phy_cur
, cnt
,
1293 DBG(lstcur COMMA
) cur
,
1294 vi_left
, /*vi_right,*/ phy_nxtcur
;
1295 struct a_tty_cell
const *tccp
, *tcp_left
, *tcp_right
, *tcxp
;
1297 n_LCTA(UICMP(64, a__LAST
, <, UI32_MAX
), "Flag bits excess storage datatype");
1299 f
= tlp
->tl_vi_flags
;
1300 tlp
->tl_vi_flags
= (f
& ~(a_TTY_VF_REFRESH
| a_PERSIST_MASK
)) |
1303 if((w
= tlp
->tl_prompt_width
) > 0)
1305 f
|= a_HAVE_POSITION
;
1307 /* XXX We don't have a OnTerminalResize event (see main.c) yet, so we need
1308 * XXX to reevaluate our circumstances over and over again */
1309 /* Don't display prompt or position indicator on very small screens */
1310 if((phy_wid_base
= (ui32_t
)n_scrnwidth
) <= a_TTY_WIDTH_RIPOFF
)
1311 f
&= ~(a_HAVE_PROMPT
| a_HAVE_POSITION
);
1313 phy_wid_base
-= a_TTY_WIDTH_RIPOFF
;
1315 /* Disable the prompt if the screen is too small; due to lack of some
1316 * indicator simply add a second ripoff */
1317 if((f
& a_HAVE_PROMPT
) && w
+ a_TTY_WIDTH_RIPOFF
>= phy_wid_base
)
1318 f
&= ~a_HAVE_PROMPT
;
1321 phy_wid
= phy_wid_base
;
1323 phy_cur
= tlp
->tl_phy_cursor
;
1324 cnt
= tlp
->tl_count
;
1325 DBG( lstcur
= tlp
->tl_lst_cursor
; )
1327 /* XXX Assume dirty screen if shrunk */
1328 if(cnt
< tlp
->tl_lst_count
)
1329 f
|= a_TTY_VF_MOD_DIRTY
;
1331 /* TODO Without HAVE_TERMCAP, it would likely be much cheaper to simply
1332 * TODO always "cr + paint + ce + ch", since ce is simulated via spaces.. */
1334 /* Quickshot: if the line is empty, possibly print prompt and out */
1336 /* In that special case dirty anything if it seems better */
1337 if((f
& a_TTY_VF_MOD_CONTENT
) || tlp
->tl_lst_count
> 0)
1338 f
|= a_TTY_VF_MOD_DIRTY
;
1340 if((f
& a_TTY_VF_MOD_DIRTY
) && phy_cur
!= 0){
1341 if(!n_termcap_cmdx(n_TERMCAP_CMD_cr
))
1346 if((f
& (a_TTY_VF_MOD_DIRTY
| a_HAVE_PROMPT
)) ==
1347 (a_TTY_VF_MOD_DIRTY
| a_HAVE_PROMPT
)){
1348 if(fputs(tlp
->tl_prompt
, n_tty_fp
) == EOF
)
1350 phy_cur
= tlp
->tl_prompt_width
+ 1;
1353 /* May need to clear former line content */
1354 if((f
& a_TTY_VF_MOD_DIRTY
) &&
1355 !n_termcap_cmd(n_TERMCAP_CMD_ce
, phy_cur
, -1))
1358 tlp
->tl_phy_start
= tlp
->tl_line
.cells
;
1362 /* Try to get an idea of the visual window */
1364 /* Find the left visual boundary */
1365 phy_wid
= (phy_wid
>> 1) + (phy_wid
>> 2);
1366 if((cur
= tlp
->tl_cursor
) == cnt
)
1369 w
= (tcp_left
= tccp
= tlp
->tl_line
.cells
+ cur
)->tc_width
;
1370 if(w
== UI8_MAX
) /* TODO yet TAB == SPC */
1372 while(tcp_left
> tlp
->tl_line
.cells
){
1373 ui16_t cw
= tcp_left
[-1].tc_width
;
1375 if(cw
== UI8_MAX
) /* TODO yet TAB == SPC */
1377 if(w
+ cw
>= phy_wid
)
1384 /* If the left hand side of our visual viewpoint consumes less than half
1385 * of the screen width, show the prompt */
1386 if(tcp_left
== tlp
->tl_line
.cells
)
1389 if((f
& (a_LEFT_MIN
| a_HAVE_PROMPT
)) == (a_LEFT_MIN
| a_HAVE_PROMPT
) &&
1390 w
+ tlp
->tl_prompt_width
< phy_wid
){
1391 phy_base
= tlp
->tl_prompt_width
;
1395 /* Then search for right boundary. We always leave the rightmost column
1396 * empty because some terminals [cw]ould wrap the line if we write into
1397 * that. XXX terminfo(5)/termcap(5) have the semi_auto_right_margin/sam/YE
1398 * XXX capability to indicate this, but we don't look at that */
1399 phy_wid
= phy_wid_base
- phy_base
;
1400 tcp_right
= tlp
->tl_line
.cells
+ cnt
;
1402 while(&tccp
[1] < tcp_right
){
1403 ui16_t cw
= tccp
[1].tc_width
;
1406 if(cw
== UI8_MAX
) /* TODO yet TAB == SPC */
1414 /*vi_right = w - vi_left;*/
1416 /* If the complete line including prompt fits on the screen, show prompt */
1417 if(--tcp_right
== tccp
){
1420 /* Since we did brute-force walk also for the left boundary we may end up
1421 * in a situation were anything effectively fits on the screen, including
1422 * the prompt that is, but were we don't recognize this since we
1423 * restricted the search to fit in some visual viewpoint. Therefore try
1424 * again to extend the left boundary to overcome that */
1425 if(!(f
& a_LEFT_MIN
)){
1426 struct a_tty_cell
const *tc1p
= tlp
->tl_line
.cells
;
1427 ui32_t vil1
= vi_left
;
1429 assert(!(f
& a_SHOW_PROMPT
));
1430 w
+= tlp
->tl_prompt_width
;
1431 for(tcxp
= tcp_left
;;){
1432 ui32_t i
= tcxp
[-1].tc_width
;
1434 if(i
== UI8_MAX
) /* TODO yet TAB == SPC */
1448 /*w -= tlp->tl_prompt_width;*/
1452 tccp
= tlp
->tl_line
.cells
+ cur
;
1454 if((f
& (a_LEFT_MIN
| a_RIGHT_MAX
| a_HAVE_PROMPT
| a_SHOW_PROMPT
)) ==
1455 (a_LEFT_MIN
| a_RIGHT_MAX
| a_HAVE_PROMPT
) &&
1456 w
+ tlp
->tl_prompt_width
<= phy_wid
){
1457 phy_wid
-= (phy_base
= tlp
->tl_prompt_width
);
1461 /* Try to avoid repainting the complete line - this is possible if the
1462 * cursor "did not leave the screen" and the prompt status hasn't changed.
1463 * I.e., after clamping virtual viewpoint, compare relation to physical */
1464 if((f
& (a_TTY_VF_MOD_SINGLE
/*FIXME*/ |
1465 a_TTY_VF_MOD_CONTENT
/* xxx */ | a_TTY_VF_MOD_DIRTY
)) ||
1466 (tcxp
= tlp
->tl_phy_start
) == NULL
||
1467 tcxp
> tccp
|| tcxp
<= tcp_right
)
1468 f
|= a_TTY_VF_MOD_DIRTY
;
1470 f
|= a_TTY_VF_MOD_DIRTY
;
1472 struct a_tty_cell
const *tcyp
;
1473 si32_t cur_displace
;
1474 ui32_t phy_lmargin
, phy_rmargin
, fx
, phy_displace
;
1476 phy_lmargin
= (fx
= phy_wid
) / 100;
1477 phy_rmargin
= fx
- (phy_lmargin
* a_TTY_SCROLL_MARGIN_RIGHT
);
1478 phy_lmargin
*= a_TTY_SCROLL_MARGIN_LEFT
;
1479 fx
= (f
& (a_SHOW_PROMPT
| a_VISIBLE_PROMPT
));
1481 if(fx
== 0 || fx
== (a_SHOW_PROMPT
| a_VISIBLE_PROMPT
)){
1487 /* We know what we have to paint, start synchronizing */
1489 assert(phy_cur
== tlp
->tl_phy_cursor
);
1490 assert(phy_wid
== phy_wid_base
- phy_base
);
1491 assert(cnt
== tlp
->tl_count
);
1493 assert(lstcur
== tlp
->tl_lst_cursor
);
1494 assert(tccp
== tlp
->tl_line
.cells
+ cur
);
1496 phy_nxtcur
= phy_base
; /* FIXME only if repaint cpl. */
1498 /* Quickshot: is it only cursor movement within the visible screen? */
1499 if((f
& a_TTY_VF_REFRESH
) == a_TTY_VF_MOD_CURSOR
){
1504 /* To be able to apply some quick jump offs, clear line if possible */
1505 if(f
& a_TTY_VF_MOD_DIRTY
){
1506 /* Force complete clearance and cursor reinitialization */
1507 if(!n_termcap_cmdx(n_TERMCAP_CMD_cr
) ||
1508 !n_termcap_cmd(n_TERMCAP_CMD_ce
, 0, -1))
1510 tlp
->tl_phy_start
= tcp_left
;
1514 if((f
& (a_TTY_VF_MOD_DIRTY
| a_SHOW_PROMPT
)) && phy_cur
!= 0){
1515 if(!n_termcap_cmdx(n_TERMCAP_CMD_cr
))
1520 if(f
& a_SHOW_PROMPT
){
1521 assert(phy_base
== tlp
->tl_prompt_width
);
1522 if(fputs(tlp
->tl_prompt
, n_tty_fp
) == EOF
)
1524 phy_cur
= phy_nxtcur
;
1525 f
|= a_VISIBLE_PROMPT
;
1527 f
&= ~a_VISIBLE_PROMPT
;
1529 /* FIXME reposition cursor for paint */
1530 for(w
= phy_nxtcur
; tcp_left
<= tcp_right
; ++tcp_left
){
1533 cw
= tcp_left
->tc_width
;
1535 if(n_LIKELY(!tcp_left
->tc_novis
)){
1536 if(fwrite(tcp_left
->tc_cbuf
, sizeof *tcp_left
->tc_cbuf
,
1537 tcp_left
->tc_count
, n_tty_fp
) != tcp_left
->tc_count
)
1539 }else{ /* XXX Shouldn't be here <-> CText, ui_str.c */
1540 char wbuf
[8]; /* XXX magic */
1542 if(n_psonce
& n_PSO_UNICODE
){
1545 wc
= (ui32_t
)tcp_left
->tc_wc
;
1546 if((wc
& ~0x1Fu
) == 0)
1552 n_utf32_to_utf8(wc
, wbuf
);
1554 wbuf
[0] = '?', wbuf
[1] = '\0';
1556 if(fputs(wbuf
, n_tty_fp
) == EOF
)
1561 if(cw
== UI8_MAX
) /* TODO yet TAB == SPC */
1564 if(tcp_left
== tccp
)
1569 /* Write something position marker alike if it doesn't fit on screen */
1570 if((f
& a_HAVE_POSITION
) &&
1571 ((f
& (a_LEFT_MIN
| a_RIGHT_MAX
)) != (a_LEFT_MIN
| a_RIGHT_MAX
) ||
1572 ((f
& a_HAVE_PROMPT
) && !(f
& a_SHOW_PROMPT
)))){
1574 char *posbuf
= tlp
->tl_pos_buf
, *pos
= tlp
->tl_pos
;
1576 char posbuf
[5], *pos
= posbuf
;
1581 if(phy_cur
!= (w
= phy_wid_base
) &&
1582 !n_termcap_cmd(n_TERMCAP_CMD_ch
, phy_cur
= w
, 0))
1586 if((f
& a_LEFT_MIN
) && (!(f
& a_HAVE_PROMPT
) || (f
& a_SHOW_PROMPT
)))
1587 memcpy(pos
, "^.+", 3);
1588 else if(f
& a_RIGHT_MAX
)
1589 memcpy(pos
, ".+$", 3);
1591 /* Theoretical line length limit a_TTY_LINE_MAX, choose next power of
1592 * ten (10 ** 10) to represent 100 percent, since we don't have a macro
1593 * that generates a constant, and i don't trust the standard "u type
1594 * suffix automatically scales" calculate the large number */
1595 static char const itoa
[] = "0123456789";
1597 ui64_t
const fact100
= (ui64_t
)0x3B9ACA00u
* 10u, fact
= fact100
/ 100;
1598 ui32_t i
= (ui32_t
)(((fact100
/ cnt
) * tlp
->tl_cursor
) / fact
);
1599 n_LCTA(a_TTY_LINE_MAX
<= SI32_MAX
, "a_TTY_LINE_MAX too large");
1602 pos
[0] = ' ', pos
[1] = itoa
[i
];
1604 pos
[1] = itoa
[i
% 10], pos
[0] = itoa
[i
/ 10];
1608 if(fputs(posbuf
, n_tty_fp
) == EOF
)
1613 /* Users are used to see the cursor right of the point of interest, so we
1614 * need some further adjustments unless in special conditions. Be aware
1615 * that we may have adjusted cur at the beginning, too */
1616 if((cur
= tlp
->tl_cursor
) == 0)
1617 phy_nxtcur
= phy_base
;
1618 else if(cur
!= cnt
){
1619 ui16_t cw
= tccp
->tc_width
;
1621 if(cw
== UI8_MAX
) /* TODO yet TAB == SPC */
1627 if(((f
& a_MOVE_CURSOR
) || phy_nxtcur
!= phy_cur
) &&
1628 !n_termcap_cmd(n_TERMCAP_CMD_ch
, phy_cur
= phy_nxtcur
, 0))
1632 tlp
->tl_vi_flags
|= (f
& a_PERSIST_MASK
);
1633 tlp
->tl_lst_count
= tlp
->tl_count
;
1634 tlp
->tl_lst_cursor
= tlp
->tl_cursor
;
1635 tlp
->tl_phy_cursor
= phy_cur
;
1638 return ((f
& a_TRUE_RV
) != 0);
1645 a_tty_wboundary(struct a_tty_line
*tlp
, si32_t dir
){/* TODO shell token-wise */
1647 struct a_tty_cell
*tcap
;
1652 assert(dir
== 1 || dir
== -1);
1655 cnt
= tlp
->tl_count
;
1656 cur
= tlp
->tl_cursor
;
1661 }else if(cur
+ 1 >= cnt
)
1664 --cnt
, --cur
; /* xxx Unsigned wrapping may occur (twice), then */
1666 for(rv
= 0, tcap
= tlp
->tl_line
.cells
, anynon
= FAL0
;;){
1669 wc
= tcap
[cur
+= (ui32_t
)dir
].tc_wc
;
1670 if(iswblank(wc
) || iswpunct(wc
)){
1681 }else if(cur
+ 1 >= cnt
){
1692 a_tty_khome(struct a_tty_line
*tlp
, bool_t dobell
){
1696 if(n_LIKELY(tlp
->tl_cursor
> 0)){
1698 f
= a_TTY_VF_MOD_CURSOR
;
1704 tlp
->tl_vi_flags
|= f
;
1709 a_tty_kend(struct a_tty_line
*tlp
){
1713 if(n_LIKELY(tlp
->tl_cursor
< tlp
->tl_count
)){
1714 tlp
->tl_cursor
= tlp
->tl_count
;
1715 f
= a_TTY_VF_MOD_CURSOR
;
1719 tlp
->tl_vi_flags
|= f
;
1724 a_tty_kbs(struct a_tty_line
*tlp
){
1728 cur
= tlp
->tl_cursor
;
1729 cnt
= tlp
->tl_count
;
1731 if(n_LIKELY(cur
> 0)){
1732 tlp
->tl_cursor
= --cur
;
1733 tlp
->tl_count
= --cnt
;
1735 if((cnt
-= cur
) > 0){
1736 struct a_tty_cell
*tcap
;
1738 tcap
= tlp
->tl_line
.cells
+ cur
;
1739 memmove(tcap
, &tcap
[1], cnt
*= sizeof(*tcap
));
1741 f
= a_TTY_VF_MOD_CURSOR
| a_TTY_VF_MOD_CONTENT
;
1745 tlp
->tl_vi_flags
|= f
;
1750 a_tty_ksnarf(struct a_tty_line
*tlp
, bool_t cplline
, bool_t dobell
){
1757 if(cplline
&& i
> 0){
1758 tlp
->tl_cursor
= i
= 0;
1759 f
= a_TTY_VF_MOD_CURSOR
;
1762 if(n_LIKELY(i
< tlp
->tl_count
)){
1763 struct a_tty_cell
*tcap
;
1765 tcap
= &tlp
->tl_line
.cells
[0];
1766 a_tty_copy2paste(tlp
, &tcap
[i
], &tcap
[tlp
->tl_count
]);
1768 f
= a_TTY_VF_MOD_CONTENT
;
1772 tlp
->tl_vi_flags
|= f
;
1777 a_tty_kdel(struct a_tty_line
*tlp
){
1782 cur
= tlp
->tl_cursor
;
1783 cnt
= tlp
->tl_count
;
1784 i
= (si32_t
)(cnt
- cur
);
1786 if(n_LIKELY(i
> 0)){
1787 tlp
->tl_count
= --cnt
;
1789 if(n_LIKELY(--i
> 0)){
1790 struct a_tty_cell
*tcap
;
1792 tcap
= &tlp
->tl_line
.cells
[cur
];
1793 memmove(tcap
, &tcap
[1], (ui32_t
)i
* sizeof(*tcap
));
1795 f
= a_TTY_VF_MOD_CONTENT
;
1796 }else if(cnt
== 0 && !ok_blook(ignoreeof
)){
1797 putc('^', n_tty_fp
);
1798 putc('D', n_tty_fp
);
1806 tlp
->tl_vi_flags
|= f
;
1812 a_tty_kleft(struct a_tty_line
*tlp
){
1816 if(n_LIKELY(tlp
->tl_cursor
> 0)){
1818 f
= a_TTY_VF_MOD_CURSOR
;
1822 tlp
->tl_vi_flags
|= f
;
1827 a_tty_kright(struct a_tty_line
*tlp
){
1831 if(n_LIKELY((i
= tlp
->tl_cursor
+ 1) <= tlp
->tl_count
)){
1833 i
= a_TTY_VF_MOD_CURSOR
;
1837 tlp
->tl_vi_flags
|= i
;
1842 a_tty_ksnarfw(struct a_tty_line
*tlp
, bool_t fwd
){
1843 struct a_tty_cell
*tcap
;
1848 if(n_UNLIKELY((i
= a_tty_wboundary(tlp
, (fwd
? +1 : -1))) <= 0)){
1849 f
= (i
< 0) ? a_TTY_VF_BELL
: a_TTY_VF_NONE
;
1853 cnt
= tlp
->tl_count
- (ui32_t
)i
;
1854 cur
= tlp
->tl_cursor
;
1857 tcap
= &tlp
->tl_line
.cells
[cur
];
1859 a_tty_copy2paste(tlp
, &tcap
[0], &tcap
[i
]);
1861 if((tlp
->tl_count
= cnt
) != (tlp
->tl_cursor
= cur
)){
1863 memmove(&tcap
[0], &tcap
[i
], cnt
* sizeof(*tcap
)); /* FIXME*/
1866 f
= a_TTY_VF_MOD_CURSOR
| a_TTY_VF_MOD_CONTENT
;
1868 tlp
->tl_vi_flags
|= f
;
1873 a_tty_kgow(struct a_tty_line
*tlp
, si32_t dir
){
1878 if(n_UNLIKELY((i
= a_tty_wboundary(tlp
, dir
)) <= 0))
1879 f
= (i
< 0) ? a_TTY_VF_BELL
: a_TTY_VF_NONE
;
1883 tlp
->tl_cursor
+= (ui32_t
)i
;
1884 f
= a_TTY_VF_MOD_CURSOR
;
1887 tlp
->tl_vi_flags
|= f
;
1892 a_tty_kother(struct a_tty_line
*tlp
, wchar_t wc
){
1893 /* Append if at EOL, insert otherwise;
1894 * since we may move around character-wise, always use a fresh ps */
1896 struct a_tty_cell tc
, *tcap
;
1904 n_LCTA(a_TTY_LINE_MAX
<= SI32_MAX
, "a_TTY_LINE_MAX too large");
1905 if(tlp
->tl_count
+ 1 >= a_TTY_LINE_MAX
){
1906 n_err(_("Stop here, we can't extend line beyond size limit\n"));
1910 /* First init a cell and see whether we'll really handle this wc */
1911 memset(&ps
, 0, sizeof ps
);
1915 l
= wcrtomb(tc
.tc_cbuf
, tc
.tc_wc
= wc
, &ps
);
1916 if(n_UNLIKELY(l
> MB_LEN_MAX
)){
1918 n_err(_("wcrtomb(3) error: too many multibyte character bytes\n"));
1921 tc
.tc_count
= (ui16_t
)l
;
1923 if(n_UNLIKELY((n_psonce
& n_PSO_ENC_MBSTATE
) != 0)){
1924 l
= wcrtomb(&tc
.tc_cbuf
[l
], L
'\0', &ps
);
1925 if(n_LIKELY(l
== 1))
1926 /* Only NUL terminator */;
1927 else if(n_LIKELY(--l
< MB_LEN_MAX
))
1928 tc
.tc_count
+= (ui16_t
)l
;
1934 /* Yes, we will! Place it in the array */
1935 tc
.tc_novis
= (iswprint(wc
) == 0);
1936 tc
.tc_width
= a_tty_wcwidth(wc
);
1937 /* TODO if(tc.tc_novis && tc.tc_width > 0) */
1939 cur
= tlp
->tl_cursor
++;
1940 cnt
= tlp
->tl_count
++ - cur
;
1941 tcap
= &tlp
->tl_line
.cells
[cur
];
1943 memmove(&tcap
[1], tcap
, cnt
* sizeof(*tcap
));
1944 f
= a_TTY_VF_MOD_CONTENT
;
1946 f
= a_TTY_VF_MOD_SINGLE
;
1947 memcpy(tcap
, &tc
, sizeof *tcap
);
1949 f
|= a_TTY_VF_MOD_CURSOR
;
1954 tlp
->tl_vi_flags
|= f
;
1960 a_tty_kht(struct a_tty_line
*tlp
){
1961 ui8_t (*autorecmem
)[n_MEMORY_AUTOREC_TYPE_SIZEOF
], *autorec_persist
;
1963 struct str orig
, bot
, topp
, sub
, exp
, preexp
;
1964 struct n_string shou
, *shoup
;
1965 struct a_tty_cell
*cword
, *ctop
, *cx
;
1966 bool_t wedid
, set_savec
;
1970 /* Get plain line data; if this is the first expansion/xy, update the
1971 * very original content so that ^G gets the origin back */
1972 orig
= tlp
->tl_savec
;
1973 a_tty_cell2save(tlp
);
1974 exp
= tlp
->tl_savec
;
1976 /*tlp->tl_savec = orig;*/
1982 autorec_persist
= n_memory_autorec_current();
1983 n_memory_autorec_push(autorecmem
= n_lofi_alloc(sizeof *autorecmem
));
1985 shoup
= n_string_creat_auto(&shou
);
1988 /* Find the word to be expanded */
1990 cword
= tlp
->tl_line
.cells
;
1991 ctop
= cword
+ tlp
->tl_cursor
;
1992 cx
= cword
+ tlp
->tl_count
;
1994 /* topp: separate data right of cursor */
1996 for(rv
= 0; ctop
< cx
; ++ctop
)
1997 rv
+= ctop
->tc_count
;
1999 topp
.s
= orig
.s
+ orig
.l
- rv
;
2000 ctop
= cword
+ tlp
->tl_cursor
;
2002 topp
.s
= NULL
, topp
.l
= 0;
2004 /* Find the shell token that corresponds to the cursor position */
2010 for(; cword
< ctop
; ++cword
)
2011 max
+= cword
->tc_count
;
2012 cword
= tlp
->tl_line
.cells
;
2020 enum n_shexp_state shs
;
2023 shs
= n_shexp_parse_token(NULL
, &sub
, n_SHEXP_PARSE_DRYRUN
|
2024 n_SHEXP_PARSE_TRIMSPACE
| n_SHEXP_PARSE_IGNORE_EMPTY
|
2025 n_SHEXP_PARSE_QUOTE_AUTO_CLOSE
);
2029 assert(max
>= sub
.l
);
2035 if(shs
& n_SHEXP_STATE_ERR_MASK
){
2036 n_err(_("Invalid completion pattern: %.*s\n"),
2040 n_shexp_parse_token(shoup
, &exp
,
2041 n_SHEXP_PARSE_TRIMSPACE
| n_SHEXP_PARSE_IGNORE_EMPTY
|
2042 n_SHEXP_PARSE_QUOTE_AUTO_CLOSE
);
2046 sub
.s
= n_string_cp(shoup
);
2047 sub
.l
= shoup
->s_len
;
2051 /* Leave room for "implicit asterisk" expansion, as below */
2054 sub
.s
= n_UNCONST("*");
2058 preexp
.s
= n_UNCONST(n_empty
);
2062 /* TODO Super-Heavy-Metal: block all sigs, avoid leaks on jump */
2064 exp
.s
= fexpand(sub
.s
, a_TTY_TAB_FEXP_FL
);
2067 if(exp
.s
== NULL
|| (exp
.l
= strlen(exp
.s
)) == 0){
2068 /* No. But maybe the users' desire was to complete only a part of the
2069 * shell token of interest! TODO This can be improved, we would need to
2070 * TODO have shexp_parse to create a DOM structure of parsed snippets, so
2071 * TODO that we can tell for each snippet which quote is active and
2072 * TODO whether we may cross its boundary and/or apply expansion for it */
2077 for(li
= UIZ_MAX
, i
= sub
.l
; i
-- > 0;){
2080 if((c
= sub
.s
[i
]) == '/' || c
== '+' /* *folder*! */)
2082 /* Do stop once some "magic" characters are seen XXX magic set */
2083 else if(c
== '<' || c
== '>' || c
== '=' || c
== ':')
2097 if(wedid
== TRUM1
&& preexp
.l
> 0)
2098 preexp
.s
= savestrbuf(preexp
.s
, preexp
.l
);
2100 /* May be multi-return! */
2101 if(n_pstate
& n_PS_EXPAND_MULTIRESULT
)
2104 /* xxx That is not really true since the limit counts characters not bytes */
2105 n_LCTA(a_TTY_LINE_MAX
<= SI32_MAX
, "a_TTY_LINE_MAX too large");
2106 if(exp
.l
>= a_TTY_LINE_MAX
- 1 || a_TTY_LINE_MAX
- 1 - exp
.l
< preexp
.l
){
2107 n_err(_("Tabulator expansion would extend beyond line size limit\n"));
2111 /* If the expansion equals the original string, assume the user wants what
2112 * is usually known as tab completion, append `*' and restart */
2113 if(!wedid
&& exp
.l
== sub
.l
&& !memcmp(exp
.s
, sub
.s
, exp
.l
)){
2114 if(sub
.s
[sub
.l
- 1] == '*')
2118 shoup
= n_string_push_c(shoup
, '*');
2119 sub
.s
= n_string_cp(shoup
);
2120 sub
.l
= shoup
->s_len
;
2124 /* If it is a directory, and there is not yet a / appended, then we want the
2125 * user to confirm that he wants to dive in -- with only a HT */
2126 if(wedid
&& exp
.l
== --sub
.l
&& !memcmp(exp
.s
, sub
.s
, exp
.l
) &&
2127 exp
.s
[exp
.l
- 1] != '/'){
2128 if(stat(exp
.s
, &sb
) || !S_ISDIR(sb
.st_mode
))
2130 sub
.s
= salloc(exp
.l
+ 1 +1);
2131 memcpy(sub
.s
, exp
.s
, exp
.l
);
2132 sub
.s
[exp
.l
++] = '/';
2133 sub
.s
[exp
.l
] = '\0';
2138 if(wedid
&& (wedid
= (exp
.s
[exp
.l
- 1] == '*')))
2140 exp
.s
[exp
.l
] = '\0';
2142 exp
.l
= strlen(exp
.s
= n_shexp_quote_cp(exp
.s
, tlp
->tl_quote_rndtrip
));
2143 tlp
->tl_defc_cursor_byte
= bot
.l
+ preexp
.l
+ exp
.l
-1;
2148 orig
.l
= bot
.l
+ preexp
.l
+ exp
.l
+ topp
.l
;
2149 orig
.s
= n_autorec_alloc(autorec_persist
, orig
.l
+ 5 +1);
2150 if((rv
= (ui32_t
)bot
.l
) > 0)
2151 memcpy(orig
.s
, bot
.s
, rv
);
2153 memcpy(&orig
.s
[rv
], preexp
.s
, preexp
.l
);
2156 memcpy(&orig
.s
[rv
], exp
.s
, exp
.l
);
2159 memcpy(&orig
.s
[rv
], topp
.s
, topp
.l
);
2164 tlp
->tl_defc
= orig
;
2165 tlp
->tl_count
= tlp
->tl_cursor
= 0;
2166 f
|= a_TTY_VF_MOD_DIRTY
;
2168 n_memory_autorec_pop(autorecmem
);
2169 n_lofi_free(autorecmem
);
2170 tlp
->tl_vi_flags
|= f
;
2175 struct n_visual_info_ctx vic
;
2180 size_t locolen
, scrwid
, lnlen
, lncnt
, prefixlen
;
2183 if((fp
= Ftmp(NULL
, "tabex", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
){
2184 n_perr(_("tmpfile"), 0);
2188 /* How long is the result string for real? Search the NUL NUL
2189 * terminator. While here, detect the longest entry to perform an
2190 * initial allocation of our accumulator string */
2195 i
= strlen(&exp
.s
[++exp
.l
]);
2196 locolen
= n_MAX(locolen
, i
);
2198 }while(exp
.s
[exp
.l
+ 1] != '\0');
2200 shoup
= n_string_reserve(n_string_trunc(shoup
, 0),
2201 locolen
+ (locolen
>> 1));
2203 /* Iterate (once again) over all results */
2204 scrwid
= (size_t)n_scrnwidth
- ((size_t)n_scrnwidth
>> 3);
2206 n_UNINIT(prefixlen
, 0);
2207 n_UNINIT(lococp
, NULL
);
2209 for(isfirst
= TRU1
; exp
.l
> 0; isfirst
= FAL0
, c1
= c2
){
2211 char const *fullpath
;
2215 sub
.l
= i
= strlen(sub
.s
);
2217 if((exp
.l
-= i
) > 0)
2221 /* Separate dirname and basename */
2226 if((cp
= strrchr(fullpath
, '/')) != NULL
)
2227 prefixlen
= PTR2SIZE(++cp
- fullpath
);
2231 if(prefixlen
> 0 && prefixlen
< sub
.l
){
2236 /* We want case-insensitive sort-order */
2237 memset(&vic
, 0, sizeof vic
);
2238 vic
.vic_indat
= sub
.s
;
2239 vic
.vic_inlen
= sub
.l
;
2240 c2
= n_visual_info(&vic
, n_VISUAL_INFO_ONE_CHAR
) ? vic
.vic_waccu
2242 #ifdef HAVE_C90AMEND1
2248 /* Query longest common prefix along the way */
2253 }else if(locolen
> 0){
2254 for(i
= 0; i
< locolen
; ++i
)
2255 if(lococp
[i
] != sub
.s
[i
]){
2256 i
= field_detect_clip(i
, lococp
, i
);
2262 /* Prepare display */
2264 shoup
= n_shexp_quote(n_string_trunc(shoup
, 0), &input
,
2265 tlp
->tl_quote_rndtrip
);
2266 memset(&vic
, 0, sizeof vic
);
2267 vic
.vic_indat
= shoup
->s_dat
;
2268 vic
.vic_inlen
= shoup
->s_len
;
2269 if(!n_visual_info(&vic
,
2270 n_VISUAL_INFO_SKIP_ERRORS
| n_VISUAL_INFO_WIDTH_QUERY
))
2271 vic
.vic_vi_width
= shoup
->s_len
;
2273 /* Put on screen. Indent follow lines of same sort slot */
2276 scrwid
< lnlen
|| scrwid
- lnlen
<= vic
.vic_vi_width
+ 2){
2283 }else if(lnlen
> 0){
2288 fputs(n_string_cp(shoup
), fp
);
2289 lnlen
+= vic
.vic_vi_width
;
2291 /* Support the known file name tagging
2292 * XXX *line-editor-completion-filetype* or so */
2293 if(!lstat(fullpath
, &sb
)){
2296 if(S_ISDIR(sb
.st_mode
))
2298 else if(S_ISLNK(sb
.st_mode
))
2301 else if(S_ISFIFO(sb
.st_mode
))
2305 else if(S_ISSOCK(sb
.st_mode
))
2309 else if(S_ISCHR(sb
.st_mode
))
2313 else if(S_ISBLK(sb
.st_mode
))
2326 page_or_print(fp
, lncnt
);
2330 n_string_gut(shoup
);
2332 /* A common prefix of 0 means we cannot provide the user any auto
2333 * completed characters */
2337 /* Otherwise we can, so extend the visual line content by the common
2338 * prefix (in a reversible way) */
2339 (exp
.s
= n_UNCONST(lococp
))[locolen
] = '\0';
2341 exp
.l
= (locolen
+= prefixlen
);
2343 /* XXX Indicate that there is multiple choice */
2344 /* XXX f |= a_TTY_VF_BELL; -> *line-editor-completion-bell*? or so */
2350 /* If we've provided a default content, but failed to expand, there is
2351 * nothing we can "revert to": drop that default again */
2353 tlp
->tl_savec
.s
= NULL
;
2354 tlp
->tl_savec
.l
= 0;
2361 # ifdef HAVE_HISTORY
2363 a_tty__khist_shared(struct a_tty_line
*tlp
, struct a_tty_hist
*thp
){
2367 if(n_LIKELY((tlp
->tl_hist
= thp
) != NULL
)){
2368 tlp
->tl_defc
.s
= savestrbuf(thp
->th_dat
, thp
->th_len
);
2369 rv
= tlp
->tl_defc
.l
= thp
->th_len
;
2370 f
= (tlp
->tl_count
> 0) ? a_TTY_VF_MOD_DIRTY
: a_TTY_VF_NONE
;
2371 tlp
->tl_count
= tlp
->tl_cursor
= 0;
2377 tlp
->tl_vi_flags
|= f
;
2383 a_tty_khist(struct a_tty_line
*tlp
, bool_t fwd
){
2384 struct a_tty_hist
*thp
;
2388 /* If we're not in history mode yet, save line content;
2389 * also, disallow forward search, then, and, of course, bail unless we
2390 * do have any history at all */
2391 if((thp
= tlp
->tl_hist
) == NULL
){
2394 if((thp
= a_tty
.tg_hist
) == NULL
)
2396 a_tty_cell2save(tlp
);
2400 thp
= fwd
? thp
->th_younger
: thp
->th_older
;
2402 rv
= a_tty__khist_shared(tlp
, thp
);
2408 a_tty_khist_search(struct a_tty_line
*tlp
, bool_t fwd
){
2409 struct str orig_savec
;
2410 struct a_tty_hist
*thp
;
2416 /* We cannot complete an empty line */
2417 if(n_UNLIKELY(tlp
->tl_count
== 0)){
2418 /* XXX The upcoming hard reset would restore a set savec buffer,
2419 * XXX so forcefully reset that. A cleaner solution would be to
2420 * XXX reset it whenever a restore is no longer desired */
2421 tlp
->tl_savec
.s
= NULL
;
2422 tlp
->tl_savec
.l
= 0;
2426 if((thp
= tlp
->tl_hist
) == NULL
){
2427 if((thp
= a_tty
.tg_hist
) == NULL
)
2429 /* We don't support wraparound, searching forward must always step */
2431 thp
= thp
->th_younger
;
2432 orig_savec
.s
= NULL
;
2433 orig_savec
.l
= 0; /* silence CC */
2434 }else if((thp
= (fwd
? thp
->th_younger
: thp
->th_older
)) == NULL
)
2437 orig_savec
= tlp
->tl_savec
;
2439 if(orig_savec
.s
== NULL
)
2440 a_tty_cell2save(tlp
);
2442 for(; thp
!= NULL
; thp
= (fwd
? thp
->th_younger
: thp
->th_older
))
2443 if(is_prefix(tlp
->tl_savec
.s
, thp
->th_dat
))
2446 if(orig_savec
.s
!= NULL
)
2447 tlp
->tl_savec
= orig_savec
;
2449 rv
= a_tty__khist_shared(tlp
, thp
);
2453 # endif /* HAVE_HISTORY */
2455 static enum a_tty_fun_status
2456 a_tty_fun(struct a_tty_line
*tlp
, enum a_tty_bind_flags tbf
, size_t *len
){
2457 enum a_tty_fun_status rv
;
2460 rv
= a_TTY_FUN_STATUS_OK
;
2462 # define a_X(N) a_TTY_BIND_FUN_REDUCE(a_TTY_BIND_FUN_ ## N)
2463 switch(a_TTY_BIND_FUN_REDUCE(tbf
)){
2465 tlp
->tl_vi_flags
|= a_TTY_VF_BELL
;
2473 case a_X(GO_WORD_BWD
):
2474 a_tty_kgow(tlp
, -1);
2476 case a_X(GO_WORD_FWD
):
2477 a_tty_kgow(tlp
, +1);
2480 a_tty_khome(tlp
, TRU1
);
2489 if(a_tty_kdel(tlp
) < 0)
2490 rv
= a_TTY_FUN_STATUS_END
;
2492 case a_X(SNARF_WORD_BWD
):
2493 a_tty_ksnarfw(tlp
, FAL0
);
2495 case a_X(SNARF_WORD_FWD
):
2496 a_tty_ksnarfw(tlp
, TRU1
);
2498 case a_X(SNARF_END
):
2499 a_tty_ksnarf(tlp
, FAL0
, TRU1
);
2501 case a_X(SNARF_LINE
):
2502 a_tty_ksnarf(tlp
, TRU1
, (tlp
->tl_count
== 0));
2506 # ifdef HAVE_HISTORY
2507 if(tlp
->tl_hist
!= NULL
){
2508 bool_t isfwd
= TRU1
;
2514 # ifdef HAVE_HISTORY
2517 if((*len
= a_tty_khist(tlp
, isfwd
)) != UI32_MAX
){
2518 rv
= a_TTY_FUN_STATUS_RESTART
;
2524 tlp
->tl_vi_flags
|= a_TTY_VF_BELL
;
2527 case a_X(HIST_SRCH_FWD
):{
2528 # ifdef HAVE_HISTORY
2529 bool_t isfwd
= TRU1
;
2534 case a_X(HIST_SRCH_BWD
):
2535 # ifdef HAVE_HISTORY
2538 if((*len
= a_tty_khist_search(tlp
, isfwd
)) != UI32_MAX
){
2539 rv
= a_TTY_FUN_STATUS_RESTART
;
2544 tlp
->tl_vi_flags
|= a_TTY_VF_BELL
;
2549 tlp
->tl_vi_flags
|= a_TTY_VF_MOD_DIRTY
;
2551 case a_X(QUOTE_RNDTRIP
):
2552 tlp
->tl_quote_rndtrip
= !tlp
->tl_quote_rndtrip
;
2554 case a_X(PROMPT_CHAR
):{
2557 if((wc
= a_tty_vinuni(tlp
)) > 0)
2558 a_tty_kother(tlp
, wc
);
2561 if((*len
= a_tty_kht(tlp
)) > 0)
2562 rv
= a_TTY_FUN_STATUS_RESTART
;
2566 if(tlp
->tl_pastebuf
.l
> 0)
2567 *len
= (tlp
->tl_defc
= tlp
->tl_pastebuf
).l
;
2569 tlp
->tl_vi_flags
|= a_TTY_VF_BELL
;
2574 /* Normally this just causes a restart and thus resets the state
2576 if(tlp
->tl_savec
.l
== 0 && tlp
->tl_defc
.l
== 0){
2578 # ifdef HAVE_KEY_BINDINGS
2579 tlp
->tl_bind_takeover
= '\0';
2581 tlp
->tl_vi_flags
|= a_TTY_VF_BELL
;
2582 rv
= a_TTY_FUN_STATUS_RESTART
;
2586 if(tlp
->tl_count
== 0 && tlp
->tl_savec
.l
== 0 && tlp
->tl_defc
.l
== 0){
2587 # ifdef HAVE_KEY_BINDINGS
2588 tlp
->tl_bind_takeover
= '\0';
2590 tlp
->tl_vi_flags
|= a_TTY_VF_MOD_DIRTY
| a_TTY_VF_BELL
;
2593 case a_X(FULLRESET
):
2594 tlp
->tl_savec
.s
= tlp
->tl_defc
.s
= NULL
;
2595 tlp
->tl_savec
.l
= tlp
->tl_defc
.l
= 0;
2596 tlp
->tl_defc_cursor_byte
= 0;
2597 tlp
->tl_vi_flags
|= a_TTY_VF_BELL
;
2600 # ifdef HAVE_KEY_BINDINGS
2601 tlp
->tl_bind_takeover
= '\0';
2603 tlp
->tl_vi_flags
|= a_TTY_VF_MOD_DIRTY
;
2604 tlp
->tl_cursor
= tlp
->tl_count
= 0;
2605 # ifdef HAVE_HISTORY
2606 tlp
->tl_hist
= NULL
;
2608 if((*len
= tlp
->tl_savec
.l
) != 0){
2609 tlp
->tl_defc
= tlp
->tl_savec
;
2610 tlp
->tl_savec
.s
= NULL
;
2611 tlp
->tl_savec
.l
= 0;
2613 *len
= tlp
->tl_defc
.l
;
2614 rv
= a_TTY_FUN_STATUS_RESTART
;
2619 rv
= a_TTY_FUN_STATUS_COMMIT
;
2629 a_tty_readline(struct a_tty_line
*tlp
, size_t len n_MEMORY_DEBUG_ARGS
){
2630 /* We want to save code, yet we may have to incorporate a lines'
2631 * default content and / or default input to switch back to after some
2632 * history movement; let "len > 0" mean "have to display some data
2633 * buffer" -> a_BUFMODE, and only otherwise read(2) it */
2635 char cbuf_base
[MB_LEN_MAX
* 2], *cbuf
, *cbufp
;
2637 struct a_tty_bind_tree
*tbtp
;
2639 enum a_tty_bind_flags tbf
;
2640 enum {a_NONE
, a_WAS_HERE
= 1<<0, a_BUFMODE
= 1<<1, a_MAYBEFUN
= 1<<2,
2641 a_TIMEOUT
= 1<<3, a_TIMEOUT_EXPIRED
= 1<<4,
2642 a_TIMEOUT_MASK
= a_TIMEOUT
| a_TIMEOUT_EXPIRED
,
2643 a_READ_LOOP_MASK
= ~(a_WAS_HERE
| a_MAYBEFUN
| a_TIMEOUT_MASK
)
2648 # ifdef HAVE_KEY_BINDINGS
2649 assert(tlp
->tl_bind_takeover
== '\0');
2652 memset(ps
, 0, sizeof ps
);
2655 tlp
->tl_vi_flags
|= a_TTY_VF_REFRESH
| a_TTY_VF_SYNC
;
2662 /* Ensure we have valid pointers, and room for grow */
2663 a_tty_check_grow(tlp
, ((flags
& a_BUFMODE
) ? (ui32_t
)len
: 1)
2664 n_MEMORY_DEBUG_ARGSCALL
);
2666 /* Handle visual state flags, except in buffer mode */
2667 if(!(flags
& a_BUFMODE
) && (tlp
->tl_vi_flags
& a_TTY_VF_ALL_MASK
))
2668 if(!a_tty_vi_refresh(tlp
)){
2673 /* Ready for messing around.
2674 * Normal read(2)? Else buffer mode: speed this one up */
2675 if(!(flags
& a_BUFMODE
)){
2679 assert(tlp
->tl_defc
.l
> 0 && tlp
->tl_defc
.s
!= NULL
);
2680 assert(tlp
->tl_defc
.l
>= len
);
2682 cbuf
= tlp
->tl_defc
.s
+ (tlp
->tl_defc
.l
- len
);
2686 /* Read in the next complete multibyte character */
2688 # ifdef HAVE_KEY_BINDINGS
2689 struct a_tty_bind_tree
*xtbtp
;
2693 struct a_tty_bind_tree
*tbtp
;
2696 isp_head
= isp
= NULL
;
2699 for(flags
&= a_READ_LOOP_MASK
;;){
2700 # ifdef HAVE_KEY_BINDINGS
2701 if(!(flags
& a_BUFMODE
) && tlp
->tl_bind_takeover
!= '\0'){
2702 wc
= tlp
->tl_bind_takeover
;
2703 tlp
->tl_bind_takeover
= '\0';
2707 if(!(flags
& a_BUFMODE
)){
2708 /* Let me at least once dream of iomon(itor), timer with
2709 * one-shot, enwrapped with key_event and key_sequence_event,
2710 * all driven by an event_loop */
2711 /* TODO v15 Until we have SysV signal handling all through we
2712 * TODO need to temporarily adjust our BSD signal handler with
2713 * TODO a SysV one, here */
2714 n_sighdl_t otstp
, ottin
, ottou
;
2716 otstp
= n_signal(SIGTSTP
, &n_tty_signal
);
2717 ottin
= n_signal(SIGTTIN
, &n_tty_signal
);
2718 ottou
= n_signal(SIGTTOU
, &n_tty_signal
);
2719 # ifdef HAVE_KEY_BINDINGS
2720 flags
&= ~a_TIMEOUT_MASK
;
2721 if(isp
!= NULL
&& (tbtp
= isp
->tbtp
)->tbt_isseq
&&
2722 !tbtp
->tbt_isseq_trail
){
2723 a_tty_term_rawmode_timeout(tlp
, TRU1
);
2728 while((rv
= read(STDIN_FILENO
, cbufp
, 1)) < 1){
2731 if((tlp
->tl_vi_flags
& a_TTY_VF_MOD_DIRTY
) &&
2732 !a_tty_vi_refresh(tlp
))
2739 # ifdef HAVE_KEY_BINDINGS
2740 /* Timeout expiration */
2742 assert(flags
& a_TIMEOUT
);
2743 assert(isp
!= NULL
);
2744 a_tty_term_rawmode_timeout(tlp
, FAL0
);
2746 /* Something "atomic" broke. Maybe the current one can
2747 * also be terminated already, by itself? xxx really? */
2748 if((tbtp
= isp
->tbtp
)->tbt_bind
!= NULL
){
2749 tlp
->tl_bind_takeover
= wc
;
2753 /* Or, maybe there is a second path without a timeout;
2754 * this should be covered by .tbt_isseq_trail, but then
2755 * again a single-layer implementation cannot "know" */
2756 for(xtbtp
= tbtp
; (xtbtp
= xtbtp
->tbt_sibling
) != NULL
;)
2757 if(xtbtp
->tbt_char
== tbtp
->tbt_char
){
2758 assert(!xtbtp
->tbt_isseq
);
2761 /* Lay down on read(2)? */
2766 # endif /* HAVE_KEY_BINDINGS */
2769 # ifdef HAVE_KEY_BINDINGS
2770 if(flags
& a_TIMEOUT
)
2771 a_tty_term_rawmode_timeout(tlp
, FAL0
);
2773 safe_signal(SIGTSTP
, otstp
);
2774 safe_signal(SIGTTIN
, ottin
);
2775 safe_signal(SIGTTOU
, ottou
);
2782 rv
= (ssize_t
)mbrtowc(&wc
, cbuf
, PTR2SIZE(cbufp
- cbuf
), &ps
[0]);
2784 /* Any error during buffer mode can only result in a hard
2785 * reset; Otherwise, if it's a hard error, or if too many
2786 * redundant shift sequences overflow our buffer: perform
2788 if((flags
& a_BUFMODE
) || rv
== -1 ||
2789 sizeof cbuf_base
== PTR2SIZE(cbufp
- cbuf
)){
2790 a_tty_fun(tlp
, a_TTY_BIND_FUN_FULLRESET
, &len
);
2793 /* Otherwise, due to the way we deal with the buffer, we need
2794 * to restore the mbstate_t from before this conversion */
2802 /* Normal read(2)ing is subject to detection of key-bindings */
2803 # ifdef HAVE_KEY_BINDINGS
2804 if(!(flags
& a_BUFMODE
)){
2805 /* Check for special bypass functions before we try to embed
2806 * this character into the tree */
2807 if(n_uasciichar(wc
)){
2811 for(c
= (char)wc
, cp
= &(*tlp
->tl_bind_shcut_prompt_char
)[0];
2814 wc
= a_tty_vinuni(tlp
);
2819 tlp
->tl_vi_flags
|= a_TTY_VF_BELL
;
2823 if(n_uasciichar(wc
))
2824 flags
|= a_MAYBEFUN
;
2826 flags
&= ~a_MAYBEFUN
;
2828 /* Search for this character in the bind tree */
2829 tbtp
= (isp
!= NULL
) ? isp
->tbtp
->tbt_childs
2830 : (*tlp
->tl_bind_tree_hmap
)[wc
% HSHSIZE
];
2831 for(; tbtp
!= NULL
; tbtp
= tbtp
->tbt_sibling
){
2832 if(tbtp
->tbt_char
== wc
){
2835 /* If this one cannot continue we're likely finished! */
2836 if(tbtp
->tbt_childs
== NULL
){
2837 assert(tbtp
->tbt_bind
!= NULL
);
2838 tbf
= tbtp
->tbt_bind
->tbc_flags
;
2842 /* This needs to read more characters */
2843 nisp
= salloc(sizeof *nisp
);
2844 if((nisp
->last
= isp
) == NULL
)
2851 flags
&= ~a_WAS_HERE
;
2858 /* Was there a binding active, but couldn't be continued? */
2860 /* A binding had a timeout, it didn't expire, but we saw
2861 * something non-expected. Something "atomic" broke.
2862 * Maybe there is a second path without a timeout, that
2863 * continues like we've seen it. I.e., it may just have been
2864 * the user, typing too fast. We definitely want to allow
2865 * bindings like \e,d etc. to succeed: users are so used to
2866 * them that a timeout cannot be the mechanism to catch up!
2867 * A single-layer implementation cannot "know" */
2868 if((tbtp
= isp
->tbtp
)->tbt_isseq
&& (isp
->last
== NULL
||
2869 !(xtbtp
= isp
->last
->tbtp
)->tbt_isseq
||
2870 xtbtp
->tbt_isseq_trail
)){
2871 for(xtbtp
= (tbtp
= isp
->tbtp
);
2872 (xtbtp
= xtbtp
->tbt_sibling
) != NULL
;)
2873 if(xtbtp
->tbt_char
== tbtp
->tbt_char
){
2874 assert(!xtbtp
->tbt_isseq
);
2879 tlp
->tl_bind_takeover
= wc
;
2884 /* Check for CANCEL shortcut now */
2885 if(flags
& a_MAYBEFUN
){
2889 for(c
= (char)wc
, cp
= &(*tlp
->tl_bind_shcut_cancel
)[0];
2892 tbf
= a_TTY_BIND_FUN_INTERNAL
|a_TTY_BIND_FUN_CANCEL
;
2897 /* So: maybe the current sequence can be terminated here? */
2898 if((tbtp
= isp
->tbtp
)->tbt_bind
!= NULL
){
2900 tbf
= tbtp
->tbt_bind
->tbc_flags
;
2902 if(tbf
& a_TTY_BIND_FUN_INTERNAL
){
2903 switch(a_tty_fun(tlp
, tbf
, &len
)){
2904 case a_TTY_FUN_STATUS_OK
:
2906 case a_TTY_FUN_STATUS_COMMIT
:
2908 case a_TTY_FUN_STATUS_RESTART
:
2910 case a_TTY_FUN_STATUS_END
:
2914 }else if(tbtp
->tbt_bind
->tbc_flags
& a_TTY_BIND_NOCOMMIT
){
2915 struct a_tty_bind_ctx
*tbcp
;
2917 tbcp
= tbtp
->tbt_bind
;
2918 memcpy(tlp
->tl_defc
.s
= salloc(
2919 (tlp
->tl_defc
.l
= len
= tbcp
->tbc_exp_len
) +1),
2920 tbcp
->tbc_exp
, tbcp
->tbc_exp_len
+1);
2923 tlp
->tl_reenter_after_cmd
= tbtp
->tbt_bind
->tbc_exp
;
2929 /* Otherwise take over all chars "as is" */
2931 for(; isp_head
!= NULL
; isp_head
= isp_head
->next
)
2932 if(a_tty_kother(tlp
, isp_head
->tbtp
->tbt_char
)){
2935 /* And the current one too */
2938 # endif /* HAVE_KEY_BINDINGS */
2940 if((flags
& a_BUFMODE
) && (len
-= (size_t)rv
) == 0){
2941 /* Buffer mode completed */
2942 tlp
->tl_defc
.s
= NULL
;
2944 flags
&= ~a_BUFMODE
;
2949 # ifndef HAVE_KEY_BINDINGS
2950 /* Don't interpret control bytes during buffer mode.
2951 * Otherwise, if it's a control byte check whether it is a MLE
2952 * function. Remarks: initially a complete duplicate to be able to
2953 * switch(), later converted to simply iterate over (an #ifdef'd
2954 * subset of) the MLE base_tuple table in order to have "a SPOF" */
2955 if(cbuf
== cbuf_base
&& n_uasciichar(wc
) && cntrlchar((char)wc
)){
2956 struct a_tty_bind_builtin_tuple
const *tbbtp
, *tbbtp_max
;
2959 c
= (char)wc
^ 0x40;
2960 tbbtp
= a_tty_bind_base_tuples
;
2961 tbbtp_max
= &tbbtp
[n_NELEM(a_tty_bind_base_tuples
)];
2963 for(; tbbtp
< tbbtp_max
; ++tbbtp
){
2964 /* Assert default_tuple table is properly subset'ed */
2965 assert(tbbtp
->tbdt_iskey
);
2966 if(tbbtp
->tbbt_ckey
== c
){
2967 if(tbbtp
->tbbt_exp
[0] == '\0'){
2968 enum a_tty_bind_flags tbf
;
2970 tbf
= a_TTY_BIND_FUN_EXPAND((ui8_t
)tbbtp
->tbbt_exp
[1]);
2971 switch(a_tty_fun(tlp
, tbf
, &len
)){
2972 case a_TTY_FUN_STATUS_OK
:
2974 case a_TTY_FUN_STATUS_COMMIT
:
2976 case a_TTY_FUN_STATUS_RESTART
:
2978 case a_TTY_FUN_STATUS_END
:
2983 tlp
->tl_reenter_after_cmd
= tbbtp
->tbbt_exp
;
2989 &a_tty_bind_base_tuples
[n_NELEM(a_tty_bind_base_tuples
)]){
2990 tbbtp
= a_tty_bind_default_tuples
;
2991 tbbtp_max
= &tbbtp
[n_NELEM(a_tty_bind_default_tuples
)];
2995 # endif /* !HAVE_KEY_BINDINGS */
2997 # ifdef HAVE_KEY_BINDINGS
3000 if(a_tty_kother(tlp
, wc
)){
3001 /* Don't clear the history during buffer mode.. */
3002 # ifdef HAVE_HISTORY
3003 if(!(flags
& a_BUFMODE
) && cbuf
== cbuf_base
)
3004 tlp
->tl_hist
= NULL
;
3010 /* We have a completed input line, convert the struct cell data to its
3011 * plain character equivalent */
3013 rv
= a_tty_cell2dat(tlp
);
3015 putc('\n', n_tty_fp
);
3021 # ifdef HAVE_KEY_BINDINGS
3022 static enum n_lexinput_flags
3023 a_tty_bind_ctx_find(char const *name
){
3024 enum n_lexinput_flags rv
;
3025 struct a_tty_bind_ctx_map
const *tbcmp
;
3028 tbcmp
= a_tty_bind_ctx_maps
;
3029 do if(!asccasecmp(tbcmp
->tbcm_name
, name
)){
3030 rv
= tbcmp
->tbcm_ctx
;
3032 }while(PTRCMP(++tbcmp
, <,
3033 &a_tty_bind_ctx_maps
[n_NELEM(a_tty_bind_ctx_maps
)]));
3035 rv
= (enum n_lexinput_flags
)-1;
3042 a_tty_bind_create(struct a_tty_bind_parse_ctx
*tbpcp
, bool_t replace
){
3043 struct a_tty_bind_ctx
*tbcp
;
3049 if(!a_tty_bind_parse(TRU1
, tbpcp
))
3052 /* Since we use a single buffer for it all, need to replace as such */
3053 if(tbpcp
->tbpc_tbcp
!= NULL
){
3056 a_tty_bind_del(tbpcp
);
3057 }else if(a_tty
.tg_bind_cnt
== UI32_MAX
){
3058 n_err(_("`bind': maximum number of bindings already established\n"));
3065 tbcp
= smalloc(n_VSTRUCT_SIZEOF(struct a_tty_bind_ctx
, tbc__buf
) +
3066 tbpcp
->tbpc_seq_len
+ tbpcp
->tbpc_exp
.l
+
3067 n_MAX(sizeof(si32_t
), sizeof(wc_t
)) + tbpcp
->tbpc_cnv_len
+3);
3068 if(tbpcp
->tbpc_ltbcp
!= NULL
){
3069 tbcp
->tbc_next
= tbpcp
->tbpc_ltbcp
->tbc_next
;
3070 tbpcp
->tbpc_ltbcp
->tbc_next
= tbcp
;
3072 enum n_lexinput_flags lif
= tbpcp
->tbpc_flags
& n__LEXINPUT_CTX_MASK
;
3074 tbcp
->tbc_next
= a_tty
.tg_bind
[lif
];
3075 a_tty
.tg_bind
[lif
] = tbcp
;
3077 memcpy(tbcp
->tbc_seq
= &tbcp
->tbc__buf
[0],
3078 tbpcp
->tbpc_seq
, i
= (tbcp
->tbc_seq_len
= tbpcp
->tbpc_seq_len
) +1);
3079 memcpy(tbcp
->tbc_exp
= &tbcp
->tbc__buf
[i
],
3080 tbpcp
->tbpc_exp
.s
, j
= (tbcp
->tbc_exp_len
= tbpcp
->tbpc_exp
.l
) +1);
3082 i
= (i
+ tbpcp
->tbpc_cnv_align_mask
) & ~tbpcp
->tbpc_cnv_align_mask
;
3083 memcpy(tbcp
->tbc_cnv
= &tbcp
->tbc__buf
[i
],
3084 tbpcp
->tbpc_cnv
, (tbcp
->tbc_cnv_len
= tbpcp
->tbpc_cnv_len
) +1);
3085 tbcp
->tbc_flags
= tbpcp
->tbpc_flags
;
3088 /* Directly resolve any termcap(5) symbol if we are already setup */
3089 if((n_psonce
& n_PSO_STARTED
) &&
3090 (tbcp
->tbc_flags
& (a_TTY_BIND_RESOLVE
| a_TTY_BIND_DEFUNCT
)) ==
3092 a_tty_bind_resolve(tbcp
);
3094 ++a_tty
.tg_bind_cnt
;
3095 /* If this binding is usable invalidate the key input lookup trees */
3096 if(!(tbcp
->tbc_flags
& a_TTY_BIND_DEFUNCT
))
3097 a_tty
.tg_bind_isdirty
= TRU1
;
3105 a_tty_bind_parse(bool_t isbindcmd
, struct a_tty_bind_parse_ctx
*tbpcp
){
3106 enum{a_TRUE_RV
= a_TTY__BIND_LAST
<<1};
3108 struct n_visual_info_ctx vic
;
3109 struct str shin_save
, shin
;
3110 struct n_string shou
, *shoup
;
3117 ui32_t cnv_len
; /* High bit set if a termap to be resolved */
3118 ui32_t calc_cnv_len
; /* Ditto, but aligned etc. */
3119 ui8_t kse__dummy
[4];
3123 n_LCTA(UICMP(64, a_TRUE_RV
, <, UI32_MAX
),
3124 "Flag bits excess storage datatype");
3126 f
= n_LEXINPUT_NONE
;
3127 shoup
= n_string_creat_auto(&shou
);
3130 /* Parse the key-sequence */
3131 for(shin
.s
= n_UNCONST(tbpcp
->tbpc_in_seq
), shin
.l
= UIZ_MAX
;;){
3133 enum n_shexp_state shs
;
3136 shs
= n_shexp_parse_token(shoup
, &shin
,
3137 n_SHEXP_PARSE_TRUNC
| n_SHEXP_PARSE_TRIMSPACE
|
3138 n_SHEXP_PARSE_IGNORE_EMPTY
| n_SHEXP_PARSE_IFS_IS_COMMA
);
3139 if(shs
& n_SHEXP_STATE_ERR_UNICODE
){
3140 f
|= a_TTY_BIND_DEFUNCT
;
3141 if(isbindcmd
&& (n_poption
& n_PO_D_V
))
3142 n_err(_("`%s': \\uNICODE not available in locale: %s\n"),
3143 tbpcp
->tbpc_cmd
, tbpcp
->tbpc_in_seq
);
3145 if((shs
& n_SHEXP_STATE_ERR_MASK
) & ~n_SHEXP_STATE_ERR_UNICODE
){
3146 n_err(_("`%s': failed to parse key-sequence: %s\n"),
3147 tbpcp
->tbpc_cmd
, tbpcp
->tbpc_in_seq
);
3150 if((shs
& (n_SHEXP_STATE_OUTPUT
| n_SHEXP_STATE_STOP
)) ==
3154 ep
= salloc(sizeof *ep
);
3161 if(!(shs
& n_SHEXP_STATE_ERR_UNICODE
)){
3162 i
= strlen(ep
->seq_dat
= n_shexp_quote_cp(n_string_cp(shoup
), TRU1
));
3163 if(i
>= SI32_MAX
- 1)
3165 ep
->seq_len
= (ui32_t
)i
;
3167 /* Otherwise use the original buffer, _we_ can only quote it the wrong
3168 * way (e.g., an initial $'\u3a' becomes '\u3a'), _then_ */
3169 if((i
= shin_save
.l
- shin
.l
) >= SI32_MAX
- 1)
3171 ep
->seq_len
= (ui32_t
)i
;
3172 ep
->seq_dat
= savestrbuf(shin_save
.s
, i
);
3175 memset(&vic
, 0, sizeof vic
);
3176 vic
.vic_inlen
= shoup
->s_len
;
3177 vic
.vic_indat
= shoup
->s_dat
;
3178 if(!n_visual_info(&vic
,
3179 n_VISUAL_INFO_WOUT_CREATE
| n_VISUAL_INFO_WOUT_SALLOC
)){
3180 n_err(_("`%s': key-sequence seems to contain invalid "
3181 "characters: %s: %s\n"),
3182 tbpcp
->tbpc_cmd
, n_string_cp(shoup
), tbpcp
->tbpc_in_seq
);
3183 f
|= a_TTY_BIND_DEFUNCT
;
3185 }else if(vic
.vic_woulen
== 0 ||
3186 vic
.vic_woulen
>= (SI32_MAX
- 2) / sizeof(wc_t
)){
3188 n_err(_("`%s': length of key-sequence unsupported: %s: %s\n"),
3189 tbpcp
->tbpc_cmd
, n_string_cp(shoup
), tbpcp
->tbpc_in_seq
);
3190 f
|= a_TTY_BIND_DEFUNCT
;
3193 ep
->cnv_dat
= vic
.vic_woudat
;
3194 ep
->cnv_len
= (ui32_t
)vic
.vic_woulen
;
3196 /* A termcap(5)/terminfo(5) identifier? */
3197 if(ep
->cnv_len
> 1 && ep
->cnv_dat
[0] == ':'){
3198 i
= --ep
->cnv_len
, ++ep
->cnv_dat
;
3199 # ifndef HAVE_TERMCAP
3200 if(n_poption
& n_PO_D_V
)
3201 n_err(_("`%s': no termcap(5)/terminfo(5) support: %s: %s\n"),
3202 tbpcp
->tbpc_cmd
, ep
->seq_dat
, tbpcp
->tbpc_in_seq
);
3203 f
|= a_TTY_BIND_DEFUNCT
;
3205 if(i
> a_TTY_BIND_CAPNAME_MAX
){
3206 n_err(_("`%s': termcap(5)/terminfo(5) name too long: %s: %s\n"),
3207 tbpcp
->tbpc_cmd
, ep
->seq_dat
, tbpcp
->tbpc_in_seq
);
3208 f
|= a_TTY_BIND_DEFUNCT
;
3211 /* (We store it as char[]) */
3212 if((ui32_t
)ep
->cnv_dat
[--i
] & ~0x7Fu
){
3213 n_err(_("`%s': invalid termcap(5)/terminfo(5) name content: "
3215 tbpcp
->tbpc_cmd
, ep
->seq_dat
, tbpcp
->tbpc_in_seq
);
3216 f
|= a_TTY_BIND_DEFUNCT
;
3219 ep
->cnv_len
|= SI32_MIN
; /* Needs resolve */
3220 f
|= a_TTY_BIND_RESOLVE
;
3223 if(shs
& n_SHEXP_STATE_STOP
)
3229 n_err(_("`%s': effectively empty key-sequence: %s\n"),
3230 tbpcp
->tbpc_cmd
, tbpcp
->tbpc_in_seq
);
3234 if(isbindcmd
) /* (Or always, just "1st time init") */
3235 tbpcp
->tbpc_cnv_align_mask
= n_MAX(sizeof(si32_t
), sizeof(wc_t
)) - 1;
3238 struct a_tty_bind_ctx
*ltbcp
, *tbcp
;
3239 char *cpbase
, *cp
, *cnv
;
3242 /* Unite the parsed sequence(s) into single string representations */
3243 for(sl
= cl
= 0, tail
= head
; tail
!= NULL
; tail
= tail
->next
){
3244 sl
+= tail
->seq_len
+ 1;
3249 /* Preserve room for terminal capabilities to be resolved.
3250 * Above we have ensured the buffer will fit in these calculations */
3251 if((i
= tail
->cnv_len
) & SI32_MIN
){
3253 * struct{si32_t buf_len_iscap; si32_t cap_len; wc_t name[]+NUL;}
3255 * struct{si32_t buf_len_iscap; si32_t cap_len; char buf[]+NUL;} */
3256 n_LCTAV(n_ISPOW2(a_TTY_BIND_CAPEXP_ROUNDUP
));
3257 n_LCTA(a_TTY_BIND_CAPEXP_ROUNDUP
>= sizeof(wc_t
),
3258 "Aligning on this constant doesn't properly align wc_t");
3261 i
+= sizeof(si32_t
);
3262 if(i
< a_TTY_BIND_CAPEXP_ROUNDUP
)
3263 i
= (i
+ (a_TTY_BIND_CAPEXP_ROUNDUP
- 1)) &
3264 ~(a_TTY_BIND_CAPEXP_ROUNDUP
- 1);
3266 /* struct{si32_t buf_len_iscap; wc_t buf[]+NUL;} */
3268 i
+= sizeof(si32_t
) + sizeof(wc_t
); /* (buf_len_iscap, NUL) */
3270 if(tail
->cnv_len
& SI32_MIN
){
3271 tail
->cnv_len
&= SI32_MAX
;
3274 tail
->calc_cnv_len
= (ui32_t
)i
;
3278 tbpcp
->tbpc_seq_len
= sl
;
3279 tbpcp
->tbpc_cnv_len
= cl
;
3283 j
= i
= sl
+ 1; /* Room for comma separator */
3285 i
= (i
+ tbpcp
->tbpc_cnv_align_mask
) & ~tbpcp
->tbpc_cnv_align_mask
;
3289 tbpcp
->tbpc_seq
= cp
= cpbase
= salloc(i
);
3290 tbpcp
->tbpc_cnv
= cnv
= &cpbase
[j
];
3293 for(tail
= head
; tail
!= NULL
; tail
= tail
->next
){
3294 memcpy(cp
, tail
->seq_dat
, tail
->seq_len
);
3295 cp
+= tail
->seq_len
;
3299 char * const save_cnv
= cnv
;
3301 n_UNALIGN(si32_t
*,cnv
)[0] = (si32_t
)(i
= tail
->calc_cnv_len
);
3302 cnv
+= sizeof(si32_t
);
3305 * struct{si32_t buf_len_iscap; si32_t cap_len; wc_t name[];}
3307 * struct{si32_t buf_len_iscap; si32_t cap_len; char buf[];} */
3308 n_UNALIGN(si32_t
*,cnv
)[0] = tail
->cnv_len
;
3309 cnv
+= sizeof(si32_t
);
3311 i
= tail
->cnv_len
* sizeof(wc_t
);
3312 memcpy(cnv
, tail
->cnv_dat
, i
);
3314 *n_UNALIGN(wc_t
*,cnv
) = '\0';
3316 cnv
= save_cnv
+ (tail
->calc_cnv_len
& SI32_MAX
);
3321 /* Search for a yet existing identical mapping */
3322 for(ltbcp
= NULL
, tbcp
= a_tty
.tg_bind
[tbpcp
->tbpc_flags
]; tbcp
!= NULL
;
3323 ltbcp
= tbcp
, tbcp
= tbcp
->tbc_next
)
3324 if(tbcp
->tbc_seq_len
== sl
&& !memcmp(tbcp
->tbc_seq
, cpbase
, sl
)){
3325 tbpcp
->tbpc_tbcp
= tbcp
;
3328 tbpcp
->tbpc_ltbcp
= ltbcp
;
3329 tbpcp
->tbpc_flags
|= (f
& a_TTY__BIND_MASK
);
3332 /* Create single string expansion if so desired */
3336 exp
= tbpcp
->tbpc_exp
.s
;
3338 i
= tbpcp
->tbpc_exp
.l
;
3339 if(i
> 0 && exp
[i
- 1] == '@'){
3341 if(!blankspacechar(exp
[i
- 1]))
3347 exp
[tbpcp
->tbpc_exp
.l
= i
] = '\0';
3348 tbpcp
->tbpc_flags
|= a_TTY_BIND_NOCOMMIT
;
3351 /* Reverse solidus cannot be placed last in expansion to avoid (at the
3352 * time of this writing) possible problems with newline escaping.
3353 * Don't care about (un)even number thereof */
3354 if(i
> 0 && exp
[i
- 1] == '\\'){
3355 n_err(_("`%s': reverse solidus cannot be last in expansion: %s\n"),
3356 tbpcp
->tbpc_cmd
, tbpcp
->tbpc_in_seq
);
3360 /* It may map to an internal MLE command! */
3361 for(i
= 0; i
< n_NELEM(a_tty_bind_fun_names
); ++i
)
3362 if(!asccasecmp(exp
, a_tty_bind_fun_names
[i
])){
3363 tbpcp
->tbpc_flags
|= a_TTY_BIND_FUN_EXPAND(i
) |
3364 a_TTY_BIND_FUN_INTERNAL
|
3365 (head
->next
== NULL
? a_TTY_BIND_MLE1CNTRL
: 0);
3366 if((n_poption
& n_PO_D_V
) &&
3367 (tbpcp
->tbpc_flags
& a_TTY_BIND_NOCOMMIT
))
3368 n_err(_("`%s': MLE commands can't be made editable via @: %s\n"),
3369 tbpcp
->tbpc_cmd
, exp
);
3370 tbpcp
->tbpc_flags
&= ~a_TTY_BIND_NOCOMMIT
;
3375 f
|= a_TRUE_RV
; /* TODO because we only now true and false; DEFUNCT.. */
3377 n_string_gut(shoup
);
3379 return (f
& a_TRUE_RV
) != 0;
3383 a_tty_bind_resolve(struct a_tty_bind_ctx
*tbcp
){
3384 char capname
[a_TTY_BIND_CAPNAME_MAX
+1];
3385 struct n_termcap_value tv
;
3387 bool_t isfirst
; /* TODO For now: first char must be control! */
3391 n_UNINIT(next
, NULL
);
3392 for(cp
= tbcp
->tbc_cnv
, isfirst
= TRU1
, len
= tbcp
->tbc_cnv_len
;
3393 len
> 0; isfirst
= FAL0
, cp
= next
){
3397 i
= n_UNALIGN(si32_t
*,cp
)[0];
3404 /* struct{si32_t buf_len_iscap; si32_t cap_len; wc_t name[];} */
3405 cp
+= sizeof(si32_t
);
3406 i
= n_UNALIGN(si32_t
*,cp
)[0];
3407 cp
+= sizeof(si32_t
);
3408 for(j
= 0; j
< i
; ++j
)
3409 capname
[j
] = n_UNALIGN(wc_t
*,cp
)[j
];
3413 /* Use generic lookup mechanism if not a known query */
3417 tq
= n_termcap_query_for_name(capname
, n_TERMCAP_CAPTYPE_STRING
);
3419 tv
.tv_data
.tvd_string
= capname
;
3420 tq
= n__TERMCAP_QUERY_MAX1
;
3423 if(tq
< 0 || !n_termcap_query(tq
, &tv
)){
3424 if(n_poption
& n_PO_D_V
)
3425 n_err(_("`bind': unknown or unsupported capability: %s: %s\n"),
3426 capname
, tbcp
->tbc_seq
);
3427 tbcp
->tbc_flags
|= a_TTY_BIND_DEFUNCT
;
3432 /* struct{si32_t buf_len_iscap; si32_t cap_len; char buf[]+NUL;} */
3436 i
= strlen(tv
.tv_data
.tvd_string
);
3437 if(/*i > SI32_MAX ||*/ i
>= PTR2SIZE(next
- cp
)){
3438 if(n_poption
& n_PO_D_V
)
3439 n_err(_("`bind': capability expansion too long: %s: %s\n"),
3440 capname
, tbcp
->tbc_seq
);
3441 tbcp
->tbc_flags
|= a_TTY_BIND_DEFUNCT
;
3444 if(n_poption
& n_PO_D_V
)
3445 n_err(_("`bind': empty capability expansion: %s: %s\n"),
3446 capname
, tbcp
->tbc_seq
);
3447 tbcp
->tbc_flags
|= a_TTY_BIND_DEFUNCT
;
3449 }else if(isfirst
&& !cntrlchar(*tv
.tv_data
.tvd_string
)){
3450 if(n_poption
& n_PO_D_V
)
3451 n_err(_("`bind': capability expansion doesn't start with "
3452 "control: %s: %s\n"), capname
, tbcp
->tbc_seq
);
3453 tbcp
->tbc_flags
|= a_TTY_BIND_DEFUNCT
;
3456 n_UNALIGN(si32_t
*,cp
)[-1] = (si32_t
)i
;
3457 memcpy(cp
, tv
.tv_data
.tvd_string
, i
);
3465 a_tty_bind_del(struct a_tty_bind_parse_ctx
*tbpcp
){
3466 struct a_tty_bind_ctx
*ltbcp
, *tbcp
;
3469 tbcp
= tbpcp
->tbpc_tbcp
;
3471 if((ltbcp
= tbpcp
->tbpc_ltbcp
) != NULL
)
3472 ltbcp
->tbc_next
= tbcp
->tbc_next
;
3474 a_tty
.tg_bind
[tbpcp
->tbpc_flags
] = tbcp
->tbc_next
;
3477 --a_tty
.tg_bind_cnt
;
3478 a_tty
.tg_bind_isdirty
= TRU1
;
3483 a_tty_bind_tree_build(void){
3487 for(i
= 0; i
< n__LEXINPUT_CTX_MAX1
; ++i
){
3488 struct a_tty_bind_ctx
*tbcp
;
3489 n_LCTAV(n_LEXINPUT_CTX_BASE
== 0);
3491 /* Somewhat wasteful, but easier to handle: simply clone the entire
3492 * primary key onto the secondary one, then only modify it */
3493 for(tbcp
= a_tty
.tg_bind
[n_LEXINPUT_CTX_BASE
]; tbcp
!= NULL
;
3494 tbcp
= tbcp
->tbc_next
)
3495 if(!(tbcp
->tbc_flags
& a_TTY_BIND_DEFUNCT
))
3496 a_tty__bind_tree_add(n_LEXINPUT_CTX_BASE
, &a_tty
.tg_bind_tree
[i
][0],
3499 if(i
!= n_LEXINPUT_CTX_BASE
)
3500 for(tbcp
= a_tty
.tg_bind
[i
]; tbcp
!= NULL
; tbcp
= tbcp
->tbc_next
)
3501 if(!(tbcp
->tbc_flags
& a_TTY_BIND_DEFUNCT
))
3502 a_tty__bind_tree_add(i
, &a_tty
.tg_bind_tree
[i
][0], tbcp
);
3505 a_tty
.tg_bind_isbuild
= TRU1
;
3510 a_tty_bind_tree_teardown(void){
3514 memset(&a_tty
.tg_bind_shcut_cancel
[0], 0,
3515 sizeof(a_tty
.tg_bind_shcut_cancel
));
3516 memset(&a_tty
.tg_bind_shcut_prompt_char
[0], 0,
3517 sizeof(a_tty
.tg_bind_shcut_prompt_char
));
3519 for(i
= 0; i
< n__LEXINPUT_CTX_MAX1
; ++i
)
3520 for(j
= 0; j
< HSHSIZE
; ++j
)
3521 a_tty__bind_tree_free(a_tty
.tg_bind_tree
[i
][j
]);
3522 memset(&a_tty
.tg_bind_tree
[0], 0, sizeof(a_tty
.tg_bind_tree
));
3524 a_tty
.tg_bind_isdirty
= a_tty
.tg_bind_isbuild
= FAL0
;
3529 a_tty__bind_tree_add(ui32_t hmap_idx
, struct a_tty_bind_tree
*store
[HSHSIZE
],
3530 struct a_tty_bind_ctx
*tbcp
){
3533 struct a_tty_bind_tree
*ntbtp
;
3539 for(cnvdat
= tbcp
->tbc_cnv
, cnvlen
= tbcp
->tbc_cnv_len
; cnvlen
> 0;){
3540 union {wchar_t const *wp
; char const *cp
;} u
;
3543 /* {si32_t buf_len_iscap;} */
3544 entlen
= *n_UNALIGN(si32_t
const*,cnvdat
);
3546 if(entlen
& SI32_MIN
){
3547 /* struct{si32_t buf_len_iscap; si32_t cap_len; char buf[]+NUL;}
3548 * Note that empty capabilities result in DEFUNCT */
3549 for(u
.cp
= (char const*)&n_UNALIGN(si32_t
const*,cnvdat
)[2];
3550 *u
.cp
!= '\0'; ++u
.cp
)
3551 ntbtp
= a_tty__bind_tree_add_wc(store
, ntbtp
, *u
.cp
, TRU1
);
3552 assert(ntbtp
!= NULL
);
3553 ntbtp
->tbt_isseq_trail
= TRU1
;
3556 /* struct{si32_t buf_len_iscap; wc_t buf[]+NUL;} */
3559 u
.wp
= (wchar_t const*)&n_UNALIGN(si32_t
const*,cnvdat
)[1];
3561 /* May be a special shortcut function? */
3562 if(ntbtp
== NULL
&& (tbcp
->tbc_flags
& a_TTY_BIND_MLE1CNTRL
)){
3566 ctx
= tbcp
->tbc_flags
& n__LEXINPUT_CTX_MASK
;
3567 fun
= tbcp
->tbc_flags
& a_TTY__BIND_FUN_MASK
;
3569 if(fun
== a_TTY_BIND_FUN_CANCEL
){
3570 for(cp
= &a_tty
.tg_bind_shcut_cancel
[ctx
][0];
3571 PTRCMP(cp
, <, &a_tty
.tg_bind_shcut_cancel
[ctx
]
3572 [n_NELEM(a_tty
.tg_bind_shcut_cancel
[ctx
]) - 1]); ++cp
)
3577 }else if(fun
== a_TTY_BIND_FUN_PROMPT_CHAR
){
3578 for(cp
= &a_tty
.tg_bind_shcut_prompt_char
[ctx
][0];
3579 PTRCMP(cp
, <, &a_tty
.tg_bind_shcut_prompt_char
[ctx
]
3580 [n_NELEM(a_tty
.tg_bind_shcut_prompt_char
[ctx
]) - 1]);
3589 isseq
= (u
.wp
[1] != '\0');
3590 for(; *u
.wp
!= '\0'; ++u
.wp
)
3591 ntbtp
= a_tty__bind_tree_add_wc(store
, ntbtp
, *u
.wp
, isseq
);
3593 ntbtp
->tbt_isseq_trail
= TRU1
;
3600 /* Should have been rendered defunctional at first instead */
3601 assert(ntbtp
!= NULL
);
3602 ntbtp
->tbt_bind
= tbcp
;
3606 static struct a_tty_bind_tree
*
3607 a_tty__bind_tree_add_wc(struct a_tty_bind_tree
**treep
,
3608 struct a_tty_bind_tree
*parentp
, wchar_t wc
, bool_t isseq
){
3609 struct a_tty_bind_tree
*tbtp
, *xtbtp
;
3612 if(parentp
== NULL
){
3613 treep
+= wc
% HSHSIZE
;
3615 /* Having no parent also means that the tree slot is possibly empty */
3616 for(tbtp
= *treep
; tbtp
!= NULL
;
3617 parentp
= tbtp
, tbtp
= tbtp
->tbt_sibling
){
3618 if(tbtp
->tbt_char
!= wc
)
3620 if(tbtp
->tbt_isseq
== isseq
)
3622 /* isseq MUST be linked before !isseq, so record this "parent"
3623 * sibling, but continue searching for now */
3626 /* Otherwise it is impossible that we'll find what we look for */
3629 while((tbtp
= tbtp
->tbt_sibling
) != NULL
)
3630 assert(tbtp
->tbt_char
!= wc
);
3636 tbtp
= smalloc(sizeof *tbtp
);
3637 memset(tbtp
, 0, sizeof *tbtp
);
3638 tbtp
->tbt_char
= wc
;
3639 tbtp
->tbt_isseq
= isseq
;
3641 if(parentp
== NULL
){
3642 tbtp
->tbt_sibling
= *treep
;
3645 tbtp
->tbt_sibling
= parentp
->tbt_sibling
;
3646 parentp
->tbt_sibling
= tbtp
;
3649 if((tbtp
= *(treep
= &parentp
->tbt_childs
)) != NULL
){
3650 for(;; tbtp
= xtbtp
){
3651 if(tbtp
->tbt_char
== wc
){
3652 if(tbtp
->tbt_isseq
== isseq
)
3654 /* isseq MUST be linked before, so it is impossible that we'll
3655 * find what we look for */
3658 while((tbtp
= tbtp
->tbt_sibling
) != NULL
)
3659 assert(tbtp
->tbt_char
!= wc
);
3666 if((xtbtp
= tbtp
->tbt_sibling
) == NULL
){
3667 treep
= &tbtp
->tbt_sibling
;
3673 xtbtp
= smalloc(sizeof *xtbtp
);
3674 memset(xtbtp
, 0, sizeof *xtbtp
);
3675 xtbtp
->tbt_parent
= parentp
;
3676 xtbtp
->tbt_char
= wc
;
3677 xtbtp
->tbt_isseq
= isseq
;
3687 a_tty__bind_tree_free(struct a_tty_bind_tree
*tbtp
){
3689 while(tbtp
!= NULL
){
3690 struct a_tty_bind_tree
*tmp
;
3692 if((tmp
= tbtp
->tbt_childs
) != NULL
)
3693 a_tty__bind_tree_free(tmp
);
3695 tmp
= tbtp
->tbt_sibling
;
3701 # endif /* HAVE_KEY_BINDINGS */
3707 if(ok_blook(line_editor_disable
))
3710 /* Load the history file */
3711 # ifdef HAVE_HISTORY
3717 size_t lsize
, cnt
, llen
;
3720 a_tty
.tg_hist_size
= 0;
3721 a_tty
.tg_hist_size_max
= (size_t)hs
;
3729 hold_all_sigs(); /* TODO too heavy, yet we may jump even here!? */
3730 f
= fopen(v
, "r"); /* TODO HISTFILE LOAD: use linebuf pool */
3733 (void)n_file_lock(fileno(f
), FLT_READ
, 0,0, UIZ_MAX
);
3735 assert(!(n_pstate
& n_PS_ROOT
));
3736 n_pstate
|= n_PS_ROOT
; /* Allow calling addhist() */
3739 cnt
= (size_t)fsize(f
);
3740 while(fgetline(&lbuf
, &lsize
, &cnt
, &llen
, f
, FAL0
) != NULL
){
3741 if(llen
> 0 && lbuf
[llen
- 1] == '\n')
3742 lbuf
[--llen
] = '\0';
3743 if(llen
== 0 || lbuf
[0] == '#') /* xxx comments? noone! */
3748 isgabby
= (lbuf
[0] == '*');
3749 n_tty_addhist(lbuf
+ isgabby
, isgabby
);
3754 n_pstate
&= ~n_PS_ROOT
;
3758 rele_all_sigs(); /* XXX remove jumps */
3760 # endif /* HAVE_HISTORY */
3762 /* Force immediate resolve for anything which follows */
3763 n_psonce
|= n_PSO_LINE_EDITOR_INIT
;
3765 # ifdef HAVE_KEY_BINDINGS
3766 /* `bind's (and `unbind's) done from within resource files couldn't be
3767 * performed for real since our termcap driver wasn't yet loaded, and we
3768 * can't perform automatic init since the user may have disallowed so */
3770 struct a_tty_bind_ctx
*tbcp
;
3771 enum n_lexinput_flags lif
;
3773 for(lif
= 0; lif
< n__LEXINPUT_CTX_MAX1
; ++lif
)
3774 for(tbcp
= a_tty
.tg_bind
[lif
]; tbcp
!= NULL
; tbcp
= tbcp
->tbc_next
)
3775 if((tbcp
->tbc_flags
& (a_TTY_BIND_RESOLVE
| a_TTY_BIND_DEFUNCT
)) ==
3777 a_tty_bind_resolve(tbcp
);
3780 /* And we want to (try to) install some default key bindings */
3781 if(!ok_blook(line_editor_no_defaults
)){
3783 struct a_tty_bind_parse_ctx tbpc
;
3784 struct a_tty_bind_builtin_tuple
const *tbbtp
, *tbbtp_max
;
3787 buf
[0] = '$', buf
[1] = '\'', buf
[2] = '\\', buf
[3] = 'c',
3788 buf
[5] = '\'', buf
[6] = '\0';
3790 tbbtp
= a_tty_bind_base_tuples
;
3791 tbbtp_max
= &tbbtp
[n_NELEM(a_tty_bind_base_tuples
)];
3792 flags
= n_LEXINPUT_CTX_BASE
;
3794 for(; tbbtp
< tbbtp_max
; ++tbbtp
){
3795 memset(&tbpc
, 0, sizeof tbpc
);
3796 tbpc
.tbpc_cmd
= "bind";
3797 if(tbbtp
->tbbt_iskey
){
3798 buf
[4] = tbbtp
->tbbt_ckey
;
3799 tbpc
.tbpc_in_seq
= buf
;
3801 tbpc
.tbpc_in_seq
= savecatsep(":", '\0',
3802 n_termcap_name_of_query(tbbtp
->tbbt_query
));
3803 tbpc
.tbpc_exp
.s
= n_UNCONST(tbbtp
->tbbt_exp
[0] == '\0'
3804 ? a_tty_bind_fun_names
[(ui8_t
)tbbtp
->tbbt_exp
[1]]
3806 tbpc
.tbpc_exp
.l
= strlen(tbpc
.tbpc_exp
.s
);
3807 tbpc
.tbpc_flags
= flags
;
3808 /* ..but don't want to overwrite any user settings */
3809 a_tty_bind_create(&tbpc
, FAL0
);
3811 if(flags
== n_LEXINPUT_CTX_BASE
){
3812 tbbtp
= a_tty_bind_default_tuples
;
3813 tbbtp_max
= &tbbtp
[n_NELEM(a_tty_bind_default_tuples
)];
3814 flags
= n_LEXINPUT_CTX_DEFAULT
;
3818 # endif /* HAVE_KEY_BINDINGS */
3825 n_tty_destroy(void){
3828 if(!(n_psonce
& n_PSO_LINE_EDITOR_INIT
))
3831 # ifdef HAVE_HISTORY
3835 struct a_tty_hist
*thp
;
3847 dogabby
= ok_blook(history_gabby_persist
);
3849 if((thp
= a_tty
.tg_hist
) != NULL
)
3850 for(; thp
->th_older
!= NULL
; thp
= thp
->th_older
)
3851 if((dogabby
|| !thp
->th_isgabby
) && --hs
== 0)
3854 hold_all_sigs(); /* TODO too heavy, yet we may jump even here!? */
3855 f
= fopen(v
, "w"); /* TODO temporary + rename?! */
3858 (void)n_file_lock(fileno(f
), FLT_WRITE
, 0,0, UIZ_MAX
);
3860 for(; thp
!= NULL
; thp
= thp
->th_younger
){
3861 if(dogabby
|| !thp
->th_isgabby
){
3864 fwrite(thp
->th_dat
, sizeof *thp
->th_dat
, thp
->th_len
, f
);
3870 rele_all_sigs(); /* XXX remove jumps */
3872 # endif /* HAVE_HISTORY */
3874 # if defined HAVE_KEY_BINDINGS && defined HAVE_DEBUG
3875 c_unbind(n_UNCONST("* *"));
3879 memset(&a_tty
, 0, sizeof a_tty
);
3881 DBG( n_psonce
&= ~n_PSO_LINE_EDITOR_INIT
; )
3887 n_tty_signal(int sig
){
3888 sigset_t nset
, oset
;
3889 NYD_X
; /* Signal handler */
3894 /* We don't deal with SIGWINCH, yet get called from main.c.
3895 * Note this case might get called even if !n_PO_LINE_EDITOR_INIT */
3899 a_tty_term_mode(FAL0
);
3900 n_TERMCAP_SUSPEND(TRU1
);
3904 sigaddset(&nset
, sig
);
3905 sigprocmask(SIG_UNBLOCK
, &nset
, &oset
);
3907 /* When we come here we'll continue editing, so reestablish */
3908 sigprocmask(SIG_BLOCK
, &oset
, (sigset_t
*)NULL
);
3911 n_TERMCAP_RESUME(TRU1
);
3912 a_tty_term_mode(TRU1
);
3913 a_tty
.tg_line
->tl_vi_flags
|= a_TTY_VF_MOD_DIRTY
;
3919 (n_tty_readline
)(enum n_lexinput_flags lif
, char const *prompt
,
3920 char **linebuf
, size_t *linesize
, size_t n n_MEMORY_DEBUG_ARGS
){
3921 struct a_tty_line tl
;
3922 struct n_string xprompt
;
3927 ui32_t plen
, pwidth
;
3928 char const *orig_prompt
;
3931 n_UNINIT(pwidth
, 0);
3934 assert(!ok_blook(line_editor_disable
));
3935 if(!(n_psonce
& n_PSO_LINE_EDITOR_INIT
))
3937 assert(n_psonce
& n_PSO_LINE_EDITOR_INIT
);
3939 if(!(lif
& n_LEXINPUT_PROMPT_NONE
))
3940 n_string_creat_auto(&xprompt
);
3941 orig_prompt
= prompt
;
3944 n_colour_env_create(n_COLOUR_CTX_MLE
, FAL0
);
3947 if(!(lif
& n_LEXINPUT_PROMPT_NONE
)){
3948 pwidth
= n_tty_create_prompt(&xprompt
, orig_prompt
, lif
);
3949 plen
= (ui32_t
)xprompt
.s_len
;
3950 prompt
= (pwidth
== 0) ? NULL
: n_string_cp_const(&xprompt
);
3956 struct n_colour_pen
*ccp
;
3957 struct str
const *sp
;
3959 /* .tl_pos_buf is a hack */
3960 posbuf
= pos
= NULL
;
3961 if((ccp
= n_colour_pen_create(n_COLOUR_ID_MLE_POSITION
, NULL
)) != NULL
&&
3962 (sp
= n_colour_pen_to_str(ccp
)) != NULL
){
3964 if((sp
= n_colour_reset_to_str()) != NULL
){
3969 posbuf
= salloc(l1
+ 4 + l2
+1);
3970 memcpy(posbuf
, ccol
, l1
);
3972 memcpy(&pos
[4], sp
->s
, ++l2
);
3976 posbuf
= pos
= salloc(4 +1);
3980 # endif /* HAVE_COLOUR */
3982 memset(&tl
, 0, sizeof tl
);
3984 # ifdef HAVE_KEY_BINDINGS
3986 char const *cp
= ok_vlook(bind_timeout
);
3991 if((ul
= strtoul(cp
, NULL
, 0)) > 0 &&
3992 /* Convert to tenths of a second, unfortunately */
3993 (ul
= (ul
+ 99) / 100) <= a_TTY_BIND_TIMEOUT_MAX
)
3994 tl
.tl_bind_timeout
= (ui8_t
)ul
;
3995 else if(n_poption
& n_PO_D_V
)
3996 n_err(_("Ignoring invalid *bind-timeout*: %s\n"), cp
);
4000 if(a_tty
.tg_bind_isdirty
)
4001 a_tty_bind_tree_teardown();
4002 if(a_tty
.tg_bind_cnt
> 0 && !a_tty
.tg_bind_isbuild
)
4003 a_tty_bind_tree_build();
4004 tl
.tl_bind_tree_hmap
= &a_tty
.tg_bind_tree
[lif
& n__LEXINPUT_CTX_MASK
];
4005 tl
.tl_bind_shcut_cancel
=
4006 &a_tty
.tg_bind_shcut_cancel
[lif
& n__LEXINPUT_CTX_MASK
];
4007 tl
.tl_bind_shcut_prompt_char
=
4008 &a_tty
.tg_bind_shcut_prompt_char
[lif
& n__LEXINPUT_CTX_MASK
];
4009 # endif /* HAVE_KEY_BINDINGS */
4011 if(!(lif
& n_LEXINPUT_PROMPT_NONE
) && pwidth
> 0){
4012 tl
.tl_prompt
= prompt
;
4013 tl
.tl_prompt_length
= plen
;
4014 tl
.tl_prompt_width
= pwidth
;
4017 tl
.tl_pos_buf
= posbuf
;
4021 tl
.tl_line
.cbuf
= *linebuf
;
4023 tl
.tl_defc
.s
= savestrbuf(*linebuf
, n
);
4026 tl
.tl_x_buf
= linebuf
;
4027 tl
.tl_x_bufsize
= linesize
;
4029 a_tty
.tg_line
= &tl
;
4031 n_TERMCAP_RESUME(FAL0
);
4032 a_tty_term_mode(TRU1
);
4033 nn
= a_tty_readline(&tl
, n n_MEMORY_DEBUG_ARGSCALL
);
4034 a_tty_term_mode(FAL0
);
4035 n_TERMCAP_SUSPEND(FAL0
);
4037 a_tty
.tg_line
= NULL
;
4040 n_colour_env_gut(n_tty_fp
);
4043 if(tl
.tl_reenter_after_cmd
!= NULL
){
4044 n_source_command(lif
, tl
.tl_reenter_after_cmd
);
4045 n
= (nn
<= 0) ? 0 : nn
;
4046 if(!ok_blook(line_editor_disable
))
4048 nn
= (n_lex_input
)(lif
, orig_prompt
, linebuf
, linesize
,
4049 (n
== 0 ? n_empty
: savestrbuf(*linebuf
, n
))
4050 n_MEMORY_DEBUG_ARGSCALL
);
4057 n_tty_addhist(char const *s
, bool_t isgabby
){
4058 # ifdef HAVE_HISTORY
4059 /* Super-Heavy-Metal: block all sigs, avoid leaks+ on jump */
4061 struct a_tty_hist
*thp
, *othp
, *ythp
;
4067 # ifdef HAVE_HISTORY
4068 a_TTY_CHECK_ADDHIST(s
, isgabby
, goto j_leave
);
4069 if(a_tty
.tg_hist_size_max
== 0)
4072 l
= (ui32_t
)strlen(s
);
4074 /* Eliminating duplicates is expensive, but simply inacceptable so
4075 * during the load of a potentially large history file! */
4076 if(n_psonce
& n_PSO_LINE_EDITOR_INIT
)
4077 for(thp
= a_tty
.tg_hist
; thp
!= NULL
; thp
= thp
->th_older
)
4078 if(thp
->th_len
== l
&& !strcmp(thp
->th_dat
, s
)){
4079 hold_all_sigs(); /* TODO */
4081 thp
->th_isgabby
= !!isgabby
;
4082 othp
= thp
->th_older
;
4083 ythp
= thp
->th_younger
;
4085 othp
->th_younger
= ythp
;
4087 a_tty
.tg_hist_tail
= ythp
;
4089 ythp
->th_older
= othp
;
4091 a_tty
.tg_hist
= othp
;
4096 ++a_tty
.tg_hist_size
;
4097 if((n_psonce
& n_PSO_LINE_EDITOR_INIT
) &&
4098 a_tty
.tg_hist_size
> a_tty
.tg_hist_size_max
){
4099 --a_tty
.tg_hist_size
;
4100 if((thp
= a_tty
.tg_hist_tail
) != NULL
){
4101 if((a_tty
.tg_hist_tail
= thp
->th_younger
) == NULL
)
4102 a_tty
.tg_hist
= NULL
;
4104 a_tty
.tg_hist_tail
->th_older
= NULL
;
4109 thp
= smalloc(n_VSTRUCT_SIZEOF(struct a_tty_hist
, th_dat
) + l
+1);
4110 thp
->th_isgabby
= !!isgabby
;
4112 memcpy(thp
->th_dat
, s
, l
+1);
4114 if((thp
->th_older
= a_tty
.tg_hist
) != NULL
)
4115 a_tty
.tg_hist
->th_younger
= thp
;
4117 a_tty
.tg_hist_tail
= thp
;
4118 thp
->th_younger
= NULL
;
4119 a_tty
.tg_hist
= thp
;
4127 # ifdef HAVE_HISTORY
4135 struct a_tty_hist
*thp
;
4137 if(a_tty
.tg_hist
== NULL
)
4140 if((fp
= Ftmp(NULL
, "hist", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
){
4141 n_perr(_("tmpfile"), 0);
4146 i
= a_tty
.tg_hist_size
;
4148 for(thp
= a_tty
.tg_hist
; thp
!= NULL
;
4149 --i
, b
+= thp
->th_len
, thp
= thp
->th_older
)
4151 "%c%4" PRIuZ
". %-50.50s (%4" PRIuZ
"+%2" PRIu32
" B)\n",
4152 (thp
->th_isgabby
? '*' : ' '), i
, thp
->th_dat
, b
, thp
->th_len
);
4154 page_or_print(fp
, i
);
4160 struct a_tty_hist
*thp
;
4162 while((thp
= a_tty
.tg_hist
) != NULL
){
4163 a_tty
.tg_hist
= thp
->th_older
;
4166 a_tty
.tg_hist_tail
= NULL
;
4167 a_tty
.tg_hist_size
= 0;
4172 struct a_tty_hist
*thp
;
4174 if(UICMP(z
, entry
, <=, a_tty
.tg_hist_size
)){
4175 entry
= (long)a_tty
.tg_hist_size
- entry
;
4176 for(thp
= a_tty
.tg_hist
;; thp
= thp
->th_older
)
4179 else if(entry
-- != 0)
4182 v
= temporary_arg_v_store
= thp
->th_dat
;
4190 # endif /* HAVE_HISTORY */
4192 # ifdef HAVE_KEY_BINDINGS
4195 n_CMD_ARG_DESC_SUBCLASS_DEF(bind
, 3, a_tty_bind_cad
) { /* TODO cmd_tab.h */
4196 {n_CMD_ARG_DESC_STRING
, 0},
4197 {n_CMD_ARG_DESC_WYSH
| n_CMD_ARG_DESC_OPTION
|
4198 n_CMD_ARG_DESC_HONOUR_STOP
,
4199 n_SHEXP_PARSE_DRYRUN
| n_SHEXP_PARSE_LOG
},
4200 {n_CMD_ARG_DESC_WYSH
| n_CMD_ARG_DESC_OPTION
| n_CMD_ARG_DESC_GREEDY
|
4201 n_CMD_ARG_DESC_HONOUR_STOP
,
4202 n_SHEXP_PARSE_IGNORE_EMPTY
}
4203 } n_CMD_ARG_DESC_SUBCLASS_DEF_END
;
4204 struct n_cmd_arg_ctx cac
;
4205 struct a_tty_bind_ctx
*tbcp
;
4206 enum n_lexinput_flags lif
;
4208 union {char const *cp
; char *p
; char c
;} c
;
4211 cac
.cac_desc
= n_CMD_ARG_DESC_SUBCLASS_CAST(&a_tty_bind_cad
);
4213 cac
.cac_inlen
= UIZ_MAX
;
4214 if(!n_cmd_arg_parse(&cac
)){
4219 c
.cp
= cac
.cac_arg
->ca_arg
.ca_str
.s
;
4223 show
= !asccasecmp(cac
.cac_arg
->ca_next
->ca_arg
.ca_str
.s
, "show");
4226 if((lif
= a_tty_bind_ctx_find(c
.cp
)) == (enum n_lexinput_flags
)-1){
4227 if(!(aster
= n_is_all_or_aster(c
.cp
)) || !show
){
4228 n_err(_("`bind': invalid context: %s\n"), c
.cp
);
4239 if((fp
= Ftmp(NULL
, "bind", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
){
4240 n_perr(_("tmpfile"), 0);
4247 for(tbcp
= a_tty
.tg_bind
[lif
]; tbcp
!= NULL
;
4248 ++lns
, tbcp
= tbcp
->tbc_next
){
4249 /* Print the bytes of resolved terminal capabilities, then */
4250 if((n_poption
& n_PO_D_V
) &&
4251 (tbcp
->tbc_flags
& (a_TTY_BIND_RESOLVE
| a_TTY_BIND_DEFUNCT
)
4252 ) == a_TTY_BIND_RESOLVE
){
4254 union {wchar_t const *wp
; char const *cp
;} u
;
4257 char const *cnvdat
, *bsep
, *cbufp
;
4262 cbuf
[0] = '=', cbuf
[2] = '\0';
4263 for(cnvdat
= tbcp
->tbc_cnv
, cnvlen
= tbcp
->tbc_cnv_len
;
4265 if(cnvdat
!= tbcp
->tbc_cnv
)
4268 /* {si32_t buf_len_iscap;} */
4269 entlen
= *n_UNALIGN(si32_t
const*,cnvdat
);
4270 if(entlen
& SI32_MIN
){
4271 /* struct{si32_t buf_len_iscap; si32_t cap_len;
4272 * char buf[]+NUL;} */
4274 u
.cp
= (char const*)
4275 &n_UNALIGN(si32_t
const*,cnvdat
)[2];
4276 (c
.c
= *u
.cp
) != '\0'; ++u
.cp
){
4277 if(asciichar(c
.c
) && !cntrlchar(c
.c
))
4278 cbuf
[1] = c
.c
, cbufp
= cbuf
;
4281 fprintf(fp
, "%s%02X%s",
4282 bsep
, (ui32_t
)(ui8_t
)c
.c
, cbufp
);
4297 fprintf(fp
, "%sbind %s %s %s%s%s\n",
4298 ((tbcp
->tbc_flags
& a_TTY_BIND_DEFUNCT
)
4299 /* I18N: `bind' sequence not working, either because it is
4300 * I18N: using Unicode and that is not available in the locale,
4301 * I18N: or a termcap(5)/terminfo(5) sequence won't work out */
4302 ? _("# <Defunctional> ") : n_empty
),
4303 a_tty_bind_ctx_maps
[lif
].tbcm_name
, tbcp
->tbc_seq
,
4304 n_shexp_quote_cp(tbcp
->tbc_exp
, TRU1
),
4305 (tbcp
->tbc_flags
& a_TTY_BIND_NOCOMMIT
? "@" : n_empty
),
4306 (!(n_poption
& n_PO_D_VV
) ? n_empty
4307 : (tbcp
->tbc_flags
& a_TTY_BIND_FUN_INTERNAL
4308 ? _(" # MLE internal") : n_empty
))
4311 if(!aster
|| ++lif
>= n__LEXINPUT_CTX_MAX1
)
4314 page_or_print(fp
, lns
);
4318 struct a_tty_bind_parse_ctx tbpc
;
4319 struct n_string store
;
4321 memset(&tbpc
, 0, sizeof tbpc
);
4322 tbpc
.tbpc_cmd
= a_tty_bind_cad
.cad_name
;
4323 tbpc
.tbpc_in_seq
= cac
.cac_arg
->ca_next
->ca_arg
.ca_str
.s
;
4324 tbpc
.tbpc_exp
.s
= n_string_cp(n_cmd_arg_join_greedy(&cac
,
4325 n_string_creat_auto(&store
)));
4326 tbpc
.tbpc_exp
.l
= store
.s_len
;
4327 tbpc
.tbpc_flags
= lif
;
4328 if(!a_tty_bind_create(&tbpc
, TRU1
))
4330 n_string_gut(&store
);
4334 return (v
!= NULL
) ? n_EXIT_OK
: n_EXIT_ERR
;
4339 n_CMD_ARG_DESC_SUBCLASS_DEF(unbind
, 2, a_tty_unbind_cad
) {/* TODO cmd_tab.h*/
4340 {n_CMD_ARG_DESC_STRING
, 0},
4341 {n_CMD_ARG_DESC_WYSH
| n_CMD_ARG_DESC_HONOUR_STOP
,
4342 n_SHEXP_PARSE_DRYRUN
| n_SHEXP_PARSE_LOG
}
4343 } n_CMD_ARG_DESC_SUBCLASS_DEF_END
;
4344 struct a_tty_bind_parse_ctx tbpc
;
4345 struct n_cmd_arg_ctx cac
;
4346 struct a_tty_bind_ctx
*tbcp
;
4347 enum n_lexinput_flags lif
;
4349 union {char const *cp
; char *p
;} c
;
4352 cac
.cac_desc
= n_CMD_ARG_DESC_SUBCLASS_CAST(&a_tty_unbind_cad
);
4354 cac
.cac_inlen
= UIZ_MAX
;
4355 if(!n_cmd_arg_parse(&cac
)){
4360 c
.cp
= cac
.cac_arg
->ca_arg
.ca_str
.s
;
4363 if((lif
= a_tty_bind_ctx_find(c
.cp
)) == (enum n_lexinput_flags
)-1){
4364 if(!(aster
= n_is_all_or_aster(c
.cp
))){
4365 n_err(_("`unbind': invalid context: %s\n"), c
.cp
);
4372 c
.cp
= cac
.cac_arg
->ca_next
->ca_arg
.ca_str
.s
;
4374 if(n_is_all_or_aster(c
.cp
)){
4375 while((tbcp
= a_tty
.tg_bind
[lif
]) != NULL
){
4376 memset(&tbpc
, 0, sizeof tbpc
);
4377 tbpc
.tbpc_tbcp
= tbcp
;
4378 tbpc
.tbpc_flags
= lif
;
4379 a_tty_bind_del(&tbpc
);
4382 memset(&tbpc
, 0, sizeof tbpc
);
4383 tbpc
.tbpc_cmd
= a_tty_unbind_cad
.cad_name
;
4384 tbpc
.tbpc_in_seq
= c
.cp
;
4385 tbpc
.tbpc_flags
= lif
;
4387 if(n_UNLIKELY(!a_tty_bind_parse(FAL0
, &tbpc
)))
4389 else if(n_UNLIKELY((tbcp
= tbpc
.tbpc_tbcp
) == NULL
)){
4390 n_err(_("`unbind': no such `bind'ing: %s %s\n"),
4391 a_tty_bind_ctx_maps
[lif
].tbcm_name
, c
.cp
);
4394 a_tty_bind_del(&tbpc
);
4397 if(aster
&& ++lif
< n__LEXINPUT_CTX_MAX1
)
4401 return (v
!= NULL
) ? n_EXIT_OK
: n_EXIT_ERR
;
4403 # endif /* HAVE_KEY_BINDINGS */
4405 #else /* HAVE_MLE */
4407 * The really-nothing-at-all implementation
4417 n_tty_destroy(void){
4423 n_tty_signal(int sig
){
4424 NYD_X
; /* Signal handler */
4427 # ifdef HAVE_TERMCAP
4430 sigset_t nset
, oset
;
4432 n_TERMCAP_SUSPEND(TRU1
);
4436 sigaddset(&nset
, sig
);
4437 sigprocmask(SIG_UNBLOCK
, &nset
, &oset
);
4439 /* When we come here we'll continue editing, so reestablish */
4440 sigprocmask(SIG_BLOCK
, &oset
, (sigset_t
*)NULL
);
4443 n_TERMCAP_RESUME(TRU1
);
4447 # endif /* HAVE_TERMCAP */
4451 (n_tty_readline
)(enum n_lexinput_flags lif
, char const *prompt
,
4452 char **linebuf
, size_t *linesize
, size_t n n_MEMORY_DEBUG_ARGS
){
4453 struct n_string xprompt
;
4457 if(!(lif
& n_LEXINPUT_PROMPT_NONE
)){
4458 if(n_tty_create_prompt(n_string_creat_auto(&xprompt
), prompt
, lif
) > 0){
4459 fwrite(xprompt
.s_dat
, 1, xprompt
.s_len
, n_tty_fp
);
4464 # ifdef HAVE_TERMCAP
4466 n_TERMCAP_RESUME(FAL0
);
4468 rv
= (readline_restart
)(n_stdin
, linebuf
, linesize
,n n_MEMORY_DEBUG_ARGSCALL
);
4469 # ifdef HAVE_TERMCAP
4470 n_TERMCAP_SUSPEND(FAL0
);
4478 n_tty_addhist(char const *s
, bool_t isgabby
){
4484 #endif /* nothing at all */
4486 #undef a_TTY_SIGNALS