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 - 2018 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
8 * SPDX-License-Identifier: ISC
10 * Permission to use, copy, modify, and/or distribute this software for any
11 * purpose with or without fee is hereby granted, provided that the above
12 * copyright notice and this permission notice appear in all copies.
14 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 #ifndef HAVE_AMALGAMATION
29 #if defined HAVE_MLE || defined HAVE_TERMCAP
30 # define a_TTY_SIGNALS
34 static sighandler_type a_tty_oint
, a_tty_oquit
, a_tty_oterm
,
36 a_tty_otstp
, a_tty_ottin
, a_tty_ottou
;
41 static void a_tty_sigs_up(void), a_tty_sigs_down(void);
43 /* Is editor specific code */
44 static void a_tty_signal(int sig
);
55 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
56 a_tty_oint
= safe_signal(SIGINT
, &a_tty_signal
);
57 a_tty_oquit
= safe_signal(SIGQUIT
, &a_tty_signal
);
58 a_tty_oterm
= safe_signal(SIGTERM
, &a_tty_signal
);
59 a_tty_ohup
= safe_signal(SIGHUP
, &a_tty_signal
);
60 a_tty_otstp
= safe_signal(SIGTSTP
, &a_tty_signal
);
61 a_tty_ottin
= safe_signal(SIGTTIN
, &a_tty_signal
);
62 a_tty_ottou
= safe_signal(SIGTTOU
, &a_tty_signal
);
63 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
68 a_tty_sigs_down(void){
74 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
75 safe_signal(SIGINT
, a_tty_oint
);
76 safe_signal(SIGQUIT
, a_tty_oquit
);
77 safe_signal(SIGTERM
, a_tty_oterm
);
78 safe_signal(SIGHUP
, a_tty_ohup
);
79 safe_signal(SIGTSTP
, a_tty_otstp
);
80 safe_signal(SIGTTIN
, a_tty_ottin
);
81 safe_signal(SIGTTOU
, a_tty_ottou
);
82 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
85 #endif /* a_TTY_SIGNALS */
87 static sigjmp_buf a_tty__actjmp
; /* TODO someday, we won't need it no more */
89 a_tty__acthdl(int s
) /* TODO someday, we won't need it no more */
91 NYD_X
; /* Signal handler */
92 siglongjmp(a_tty__actjmp
, s
);
96 getapproval(char const * volatile prompt
, bool_t noninteract_default
)
98 sighandler_type
volatile oint
, ohup
;
103 if(!(n_psonce
& n_PSO_INTERACTIVE
) || (n_pstate
& n_PS_ROBOT
)){
105 rv
= noninteract_default
;
111 char const *quest
= noninteract_default
112 ? _("[yes]/no? ") : _("[no]/yes? ");
115 prompt
= _("Continue");
116 prompt
= savecatsep(prompt
, ' ', quest
);
119 oint
= safe_signal(SIGINT
, SIG_IGN
);
120 ohup
= safe_signal(SIGHUP
, SIG_IGN
);
121 if ((sig
= sigsetjmp(a_tty__actjmp
, 1)) != 0)
123 safe_signal(SIGINT
, &a_tty__acthdl
);
124 safe_signal(SIGHUP
, &a_tty__acthdl
);
126 while(n_go_input(n_GO_INPUT_CTX_DEFAULT
| n_GO_INPUT_NL_ESC
, prompt
,
127 &termios_state
.ts_linebuf
, &termios_state
.ts_linesize
, NULL
,NULL
131 x
= n_boolify(termios_state
.ts_linebuf
, UIZ_MAX
, noninteract_default
);
138 safe_signal(SIGHUP
, ohup
);
139 safe_signal(SIGINT
, oint
);
149 getuser(char const * volatile query
) /* TODO v15-compat obsolete */
151 sighandler_type
volatile oint
, ohup
;
152 char * volatile user
= NULL
;
159 oint
= safe_signal(SIGINT
, SIG_IGN
);
160 ohup
= safe_signal(SIGHUP
, SIG_IGN
);
161 if ((sig
= sigsetjmp(a_tty__actjmp
, 1)) != 0)
163 safe_signal(SIGINT
, &a_tty__acthdl
);
164 safe_signal(SIGHUP
, &a_tty__acthdl
);
166 if (n_go_input(n_GO_INPUT_CTX_DEFAULT
| n_GO_INPUT_NL_ESC
, query
,
167 &termios_state
.ts_linebuf
, &termios_state
.ts_linesize
, NULL
,NULL
) >= 0)
168 user
= termios_state
.ts_linebuf
;
171 safe_signal(SIGHUP
, ohup
);
172 safe_signal(SIGINT
, oint
);
181 getpassword(char const *query
)/* TODO v15: use _only_ n_tty_fp! */
183 sighandler_type
volatile oint
, ohup
;
185 char * volatile pass
;
190 if(!(n_psonce
& n_PSO_TTYIN
))
194 query
= _("Password: ");
195 fputs(query
, n_tty_fp
);
198 /* FIXME everywhere: tcsetattr() generates SIGTTOU when we're not in
199 * FIXME foreground pgrp, and can fail with EINTR!! also affects
200 * FIXME termios_state_reset() */
201 tcgetattr(STDIN_FILENO
, &termios_state
.ts_tios
);
202 memcpy(&tios
, &termios_state
.ts_tios
, sizeof tios
);
203 termios_state
.ts_needs_reset
= TRU1
;
204 tios
.c_iflag
&= ~(ISTRIP
);
205 tios
.c_lflag
&= ~(ECHO
| ECHOE
| ECHOK
| ECHONL
);
207 oint
= safe_signal(SIGINT
, SIG_IGN
);
208 ohup
= safe_signal(SIGHUP
, SIG_IGN
);
209 if ((sig
= sigsetjmp(a_tty__actjmp
, 1)) != 0)
211 safe_signal(SIGINT
, &a_tty__acthdl
);
212 safe_signal(SIGHUP
, &a_tty__acthdl
);
214 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &tios
);
215 if (readline_restart(n_stdin
, &termios_state
.ts_linebuf
,
216 &termios_state
.ts_linesize
, 0) >= 0)
217 pass
= termios_state
.ts_linebuf
;
219 termios_state_reset();
220 putc('\n', n_tty_fp
);
222 safe_signal(SIGHUP
, ohup
);
223 safe_signal(SIGINT
, oint
);
230 #endif /* HAVE_SOCKETS */
233 n_tty_create_prompt(struct n_string
*store
, char const *xprompt
,
234 enum n_go_input_flags gif
){
235 struct n_visual_info_ctx vic
;
241 /* Prompt creation indicates that prompt printing is directly ahead, so take
242 * this opportunity of UI-in-a-known-state and advertise the error ring */
244 if((n_psonce
& (n_PSO_INTERACTIVE
| n_PSO_ERRORS_NOTED
)
245 ) == n_PSO_INTERACTIVE
&& (n_pstate
& n_PS_ERRORS_PROMPT
)){
246 n_psonce
|= n_PSO_ERRORS_NOTED
;
247 fprintf(n_stdout
, _("There are new messages in the error message ring "
249 " The `errors' command manages this message ring\n"),
255 n_string_trunc(store
, 0);
257 if(gif
& n_GO_INPUT_PROMPT_NONE
){
262 if(n_pstate
& n_PS_ERRORS_PROMPT
){
263 n_pstate
&= ~n_PS_ERRORS_PROMPT
;
264 store
= n_string_push_cp(store
, V_(n_error
));
265 store
= n_string_push_c(store
, '#');
266 store
= n_string_push_c(store
, ' ');
270 cp
= (gif
& n_GO_INPUT_PROMPT_EVAL
)
271 ? (gif
& n_GO_INPUT_NL_FOLLOW
? ok_vlook(prompt2
) : ok_vlook(prompt
))
273 if(cp
!= NULL
&& *cp
!= '\0'){
274 enum n_shexp_state shs
;
276 store
= n_string_push_cp(store
, cp
);
277 in
.s
= n_string_cp(store
);
280 store
= n_string_drop_ownership(store
);
282 shs
= n_shexp_parse_token((n_SHEXP_PARSE_LOG
|
283 n_SHEXP_PARSE_IGNORE_EMPTY
| n_SHEXP_PARSE_QUOTE_AUTO_FIXED
|
284 n_SHEXP_PARSE_QUOTE_AUTO_DSQ
), store
, &in
, NULL
);
285 if((shs
& n_SHEXP_STATE_ERR_MASK
) || !(shs
& n_SHEXP_STATE_STOP
)){
286 store
= n_string_clear(store
);
287 store
= n_string_take_ownership(store
, out
.s
, out
.l
+1, out
.l
);
289 n_err(_("*prompt2?* evaluation failed, actively unsetting it\n"));
290 if(gif
& n_GO_INPUT_NL_FOLLOW
)
301 /* Make all printable TODO not know, we want to pass through ESC/CSI! */
303 in
.s
= n_string_cp(store
);
305 makeprint(&in
, &out
);
306 store
= n_string_assign_buf(store
, out
.s
, out
.l
);
310 /* We need the visual width.. */
311 memset(&vic
, 0, sizeof vic
);
312 vic
.vic_indat
= n_string_cp(store
);
313 vic
.vic_inlen
= store
->s_len
;
314 for(pwidth
= 0; vic
.vic_inlen
> 0;){
315 /* but \[ .. \] is not taken into account */
316 if(vic
.vic_indat
[0] == '\\' && vic
.vic_inlen
> 1 &&
317 vic
.vic_indat
[1] == '['){
320 i
= PTR2SIZE(vic
.vic_indat
- store
->s_dat
);
321 store
= n_string_cut(store
, i
, 2);
322 cp
= &n_string_cp(store
)[i
];
323 i
= store
->s_len
- i
;
326 n_err(_("Open \\[ sequence not closed in *prompt2?*\n"));
329 if(cp
[0] == '\\' && cp
[1] == ']')
332 i
= PTR2SIZE(cp
- store
->s_dat
);
333 store
= n_string_cut(store
, i
, 2);
334 vic
.vic_indat
= &n_string_cp(store
)[i
];
335 vic
.vic_inlen
= store
->s_len
- i
;
336 }else if(!n_visual_info(&vic
, n_VISUAL_INFO_WIDTH_QUERY
|
337 n_VISUAL_INFO_ONE_CHAR
)){
338 n_err(_("Character set error in evaluation of *prompt2?*\n"));
341 pwidth
+= (ui32_t
)vic
.vic_vi_width
;
342 vic
.vic_indat
= vic
.vic_oudat
;
343 vic
.vic_inlen
= vic
.vic_oulen
;
347 /* And there may be colour support, too */
349 if(n_COLOUR_IS_ACTIVE()){
350 struct str
const *psp
, *rsp
;
351 struct n_colour_pen
*ccp
;
353 if((ccp
= n_colour_pen_create(n_COLOUR_ID_MLE_PROMPT
, NULL
)) != NULL
&&
354 (psp
= n_colour_pen_to_str(ccp
)) != NULL
&&
355 (rsp
= n_colour_reset_to_str()) != NULL
){
356 store
= n_string_unshift_buf(store
, psp
->s
, psp
->l
);
357 /*store =*/ n_string_push_buf(store
, rsp
->s
, rsp
->l
);
360 #endif /* HAVE_COLOUR */
368 * MLE: the Mailx-Line-Editor, our homebrew editor
369 * (inspired from NetBSDs sh(1) and dash(1)s hetio.c).
371 * Only used in interactive mode.
372 * TODO . This code should be splitted in funs/raw input/bind modules.
373 * TODO . We work with wide characters, but not for buffer takeovers and
374 * TODO cell2save()ings. This should be changed. For the former the buffer
375 * TODO thus needs to be converted to wide first, and then simply be fed in.
376 * TODO . We repaint too much. To overcome this use the same approach that my
377 * TODO terminal library uses, add a true "virtual screen line" that stores
378 * TODO the actually visible content, keep a notion of "first modified slot"
379 * TODO and "last modified slot" (including "unknown" and "any" specials),
380 * TODO update that virtual instead, then synchronize what has truly changed.
381 * TODO I.e., add an indirection layer.
382 * TODO . No BIDI support.
383 * TODO . `bind': we currently use only one lookup tree.
384 * TODO For absolute graceful behaviour (in conjunction with HAVE_TERMCAP) we
385 * TODO need a lower level tree, which possibly combines bytes into "symbolic
386 * TODO wchar_t values", into "keys" that is, as applicable, and an upper
387 * TODO layer which only works on "keys" in order to possibly combine them
388 * TODO into key sequences. We can reuse existent tree code for that.
389 * TODO We need an additional hashmap which maps termcap/terminfo names to
390 * TODO (their byte representations and) a dynamically assigned unique
391 * TODO "symbolic wchar_t value". This implies we may have incompatibilities
392 * TODO when __STDC_ISO_10646__ is not defined. Also we do need takeover-
393 * TODO bytes storage, but it can be a string_creat_auto in the line struct.
394 * TODO Until then we can run into ambiguities; in rare occasions.
397 /* To avoid memory leaks etc. with the current codebase that simply longjmp(3)s
398 * we're forced to use the very same buffer--the one that is passed through to
399 * us from the outside--to store anything we need, i.e., a "struct cell[]", and
400 * convert that on-the-fly back to the plain char* result once we're done.
401 * To simplify our live, use savestr() buffers for all other needed memory */
403 # ifdef HAVE_KEY_BINDINGS
404 /* Default *bind-timeout* key-sequence continuation timeout, in tenths of
405 * a second. Must fit in 8-bit! Update the manual upon change! */
406 # define a_TTY_BIND_TIMEOUT 2
407 # define a_TTY_BIND_TIMEOUT_MAX SI8_MAX
409 n_CTAV(a_TTY_BIND_TIMEOUT_MAX
<= UI8_MAX
);
411 /* We have a chicken-and-egg problem with `bind' and our termcap layer,
412 * because we may not initialize the latter automatically to allow users to
413 * specify *termcap-disable* and let it mean exactly that.
414 * On the other hand users can be expected to use `bind' in resources.
415 * Therefore bindings which involve termcap/terminfo sequences, and which
416 * are defined before n_PSO_STARTED signals usability of termcap/terminfo,
417 * will be (partially) delayed until tty_init() is called.
418 * And we preallocate space for the expansion of the resolved capability */
419 # define a_TTY_BIND_CAPNAME_MAX 15
420 # define a_TTY_BIND_CAPEXP_ROUNDUP 16
422 n_CTAV(n_ISPOW2(a_TTY_BIND_CAPEXP_ROUNDUP
));
423 n_CTA(a_TTY_BIND_CAPEXP_ROUNDUP
<= SI8_MAX
/ 2, "Variable must fit in 6-bit");
424 n_CTA(a_TTY_BIND_CAPEXP_ROUNDUP
>= 8, "Variable too small");
425 # endif /* HAVE_KEY_BINDINGS */
428 /* The first line of the history file is used as a marker after >v14.9.6 */
429 # define a_TTY_HIST_MARKER "@s-mailx history v2"
432 /* The maximum size (of a_tty_cell's) in a line */
433 # define a_TTY_LINE_MAX SI32_MAX
435 /* (Some more CTAs around) */
436 n_CTA(a_TTY_LINE_MAX
<= SI32_MAX
,
437 "a_TTY_LINE_MAX larger than SI32_MAX, but the MLE uses 32-bit arithmetic");
439 /* When shall the visual screen be scrolled, in % of usable screen width */
440 # define a_TTY_SCROLL_MARGIN_LEFT 15
441 # define a_TTY_SCROLL_MARGIN_RIGHT 10
443 /* fexpand() flags for expand-on-tab */
444 # define a_TTY_TAB_FEXP_FL \
445 (FEXP_NOPROTO | FEXP_FULL | FEXP_SILENT | FEXP_MULTIOK)
447 /* Columns to ripoff: outermost may not be touched, plus position indicator.
448 * Must thus be at least 1, but should be >= 1+4 to dig the position indicator
449 * that we place (if there is sufficient space) */
450 # define a_TTY_WIDTH_RIPOFF 5
452 /* The implementation of the MLE functions always exists, and is based upon
453 * the a_TTY_BIND_FUN_* constants, so most of this enum is always necessary */
454 enum a_tty_bind_flags
{
455 # ifdef HAVE_KEY_BINDINGS
456 a_TTY_BIND_RESOLVE
= 1u<<8, /* Term cap. yet needs to be resolved */
457 a_TTY_BIND_DEFUNCT
= 1u<<9, /* Unicode/term cap. used but not avail. */
458 a_TTY__BIND_MASK
= a_TTY_BIND_RESOLVE
| a_TTY_BIND_DEFUNCT
,
459 /* MLE fun assigned to a one-byte-sequence: this may be used for special
460 * key-sequence bypass processing */
461 a_TTY_BIND_MLE1CNTRL
= 1u<<10,
462 a_TTY_BIND_NOCOMMIT
= 1u<<11, /* Expansion shall be editable */
465 /* MLE internal commands XXX Can these be values not bits? */
466 a_TTY_BIND_FUN_INTERNAL
= 1u<<15,
467 a_TTY__BIND_FUN_SHIFT
= 16u,
468 a_TTY__BIND_FUN_SHIFTMAX
= 24u,
469 a_TTY__BIND_FUN_MASK
= ((1u << a_TTY__BIND_FUN_SHIFTMAX
) - 1) &
470 ~((1u << a_TTY__BIND_FUN_SHIFT
) - 1),
471 # define a_TTY_BIND_FUN_REDUCE(X) \
472 (((ui32_t)(X) & a_TTY__BIND_FUN_MASK) >> a_TTY__BIND_FUN_SHIFT)
473 # define a_TTY_BIND_FUN_EXPAND(X) \
474 (((ui32_t)(X) & (a_TTY__BIND_FUN_MASK >> a_TTY__BIND_FUN_SHIFT)) << \
475 a_TTY__BIND_FUN_SHIFT)
478 a_TTY_BIND_FUN_ ## N = a_TTY_BIND_FUN_EXPAND(I),
481 a_X(GO_BWD
, 1) a_X(GO_FWD
, 2)
482 a_X(GO_WORD_BWD
, 3) a_X(GO_WORD_FWD
, 4)
483 a_X(GO_SCREEN_BWD
, 5) a_X(GO_SCREEN_FWD
, 6)
484 a_X(GO_HOME
, 7) a_X(GO_END
, 8)
485 a_X(DEL_BWD
, 9) a_X(DEL_FWD
, 10)
486 a_X(SNARF_WORD_BWD
, 11) a_X(SNARF_WORD_FWD
, 12)
487 a_X(SNARF_END
, 13) a_X(SNARF_LINE
, 14)
488 a_X(HIST_BWD
, 15) a_X(HIST_FWD
, 16)
489 a_X(HIST_SRCH_BWD
, 17) a_X(HIST_SRCH_FWD
, 18)
491 a_X(QUOTE_RNDTRIP
, 20)
495 a_X(CLEAR_SCREEN
, 24)
500 a_X(COMMIT
, 28) /* Must be last one! */
503 a_TTY__BIND_LAST
= 1<<28
505 # ifdef HAVE_KEY_BINDINGS
506 n_CTA((ui32_t
)a_TTY_BIND_RESOLVE
>= (ui32_t
)n__GO_INPUT_CTX_MAX1
,
507 "Bit carrier lower boundary must be raised to avoid value sharing");
509 n_CTA(a_TTY_BIND_FUN_EXPAND(a_TTY_BIND_FUN_COMMIT
) <
510 (1 << a_TTY__BIND_FUN_SHIFTMAX
),
511 "Bit carrier range must be expanded to represent necessary bits");
512 n_CTA(a_TTY__BIND_LAST
>= (1u << a_TTY__BIND_FUN_SHIFTMAX
),
513 "Bit carrier upper boundary must be raised to avoid value sharing");
514 n_CTA(UICMP(64, a_TTY__BIND_LAST
, <=, SI32_MAX
),
515 "Flag bits excess storage datatype" /* And we need one bit free */);
517 enum a_tty_fun_status
{
518 a_TTY_FUN_STATUS_OK
, /* Worked, next character */
519 a_TTY_FUN_STATUS_COMMIT
, /* Line done */
520 a_TTY_FUN_STATUS_RESTART
, /* Complete restart, reset multibyte etc. */
521 a_TTY_FUN_STATUS_END
/* End, return EOF */
525 enum a_tty_hist_flags
{
526 a_TTY_HIST_CTX_DEFAULT
= n_GO_INPUT_CTX_DEFAULT
,
527 a_TTY_HIST_CTX_COMPOSE
= n_GO_INPUT_CTX_COMPOSE
,
528 a_TTY_HIST_CTX_MASK
= n__GO_INPUT_CTX_MASK
,
529 /* Cannot use enum n_go_input_flags for the rest, need to stay in 8-bit */
530 a_TTY_HIST_GABBY
= 1u<<7,
531 a_TTY_HIST__MAX
= a_TTY_HIST_GABBY
533 n_CTA(a_TTY_HIST_CTX_MASK
< a_TTY_HIST_GABBY
, "Enumeration value overlap");
536 enum a_tty_visual_flags
{
538 a_TTY_VF_MOD_CURSOR
= 1u<<0, /* Cursor moved */
539 a_TTY_VF_MOD_CONTENT
= 1u<<1, /* Content modified */
540 a_TTY_VF_MOD_DIRTY
= 1u<<2, /* Needs complete repaint */
541 a_TTY_VF_MOD_SINGLE
= 1u<<3, /* TODO Drop when indirection as above comes */
542 a_TTY_VF_REFRESH
= a_TTY_VF_MOD_DIRTY
| a_TTY_VF_MOD_CURSOR
|
543 a_TTY_VF_MOD_CONTENT
| a_TTY_VF_MOD_SINGLE
,
544 a_TTY_VF_BELL
= 1u<<8, /* Ring the bell */
545 a_TTY_VF_SYNC
= 1u<<9, /* Flush/Sync I/O channel */
547 a_TTY_VF_ALL_MASK
= a_TTY_VF_REFRESH
| a_TTY_VF_BELL
| a_TTY_VF_SYNC
,
548 a_TTY__VF_LAST
= a_TTY_VF_SYNC
551 # ifdef HAVE_KEY_BINDINGS
552 struct a_tty_bind_ctx
{
553 struct a_tty_bind_ctx
*tbc_next
;
554 char *tbc_seq
; /* quence as given (poss. re-quoted), in .tb__buf */
555 char *tbc_exp
; /* ansion, in .tb__buf */
556 /* The .tbc_seq'uence with any terminal capabilities resolved; in fact an
557 * array of structures, the first entry of which is {si32_t buf_len_iscap;}
558 * where the signed bit indicates whether the buffer is a resolved terminal
559 * capability instead of a (possibly multibyte) character. In .tbc__buf */
565 char tbc__buf
[n_VFIELD_SIZE(0)];
568 struct a_tty_bind_ctx_map
{
569 enum n_go_input_flags tbcm_ctx
;
570 char const tbcm_name
[12]; /* Name of `bind' context */
572 # endif /* HAVE_KEY_BINDINGS */
574 struct a_tty_bind_builtin_tuple
{
575 bool_t tbbt_iskey
; /* Whether this is a control key; else termcap query */
576 char tbbt_ckey
; /* Control code */
577 ui16_t tbbt_query
; /* enum n_termcap_query (instead) */
578 char tbbt_exp
[12]; /* String or [0]=NUL/[1]=BIND_FUN_REDUCE() */
580 n_CTA(n__TERMCAP_QUERY_MAX1
<= UI16_MAX
,
581 "Enumeration cannot be stored in datatype");
583 # ifdef HAVE_KEY_BINDINGS
584 struct a_tty_bind_parse_ctx
{
585 char const *tbpc_cmd
; /* Command which parses */
586 char const *tbpc_in_seq
; /* In: key sequence */
587 struct str tbpc_exp
; /* In/Out: expansion (or NULL) */
588 struct a_tty_bind_ctx
*tbpc_tbcp
; /* Out: if yet existent */
589 struct a_tty_bind_ctx
*tbpc_ltbcp
; /* Out: the one before .tbpc_tbcp */
590 char *tbpc_seq
; /* Out: normalized sequence */
591 char *tbpc_cnv
; /* Out: sequence when read(2)ing it */
594 ui32_t tbpc_cnv_align_mask
; /* For creating a_tty_bind_ctx.tbc_cnv */
595 ui32_t tbpc_flags
; /* n_go_input_flags | a_tty_bind_flags */
598 /* Input character tree */
599 struct a_tty_bind_tree
{
600 struct a_tty_bind_tree
*tbt_sibling
; /* s at same level */
601 struct a_tty_bind_tree
*tbt_childs
; /* Sequence continues.. here */
602 struct a_tty_bind_tree
*tbt_parent
;
603 struct a_tty_bind_ctx
*tbt_bind
; /* NULL for intermediates */
604 wchar_t tbt_char
; /* acter this level represents */
605 bool_t tbt_isseq
; /* Belongs to multibyte sequence */
606 bool_t tbt_isseq_trail
; /* ..is trailing byte of it */
609 # endif /* HAVE_KEY_BINDINGS */
613 ui16_t tc_count
; /* ..of bytes */
614 ui8_t tc_width
; /* Visual width; TAB==UI8_MAX! */
615 bool_t tc_novis
; /* Don't display visually as such (control character) */
616 char tc_cbuf
[MB_LEN_MAX
* 2]; /* .. plus reset shift sequence */
620 struct a_tty_line
*tg_line
; /* To be able to access it from signal hdl */
622 struct a_tty_hist
*tg_hist
;
623 struct a_tty_hist
*tg_hist_tail
;
625 size_t tg_hist_size_max
;
627 # ifdef HAVE_KEY_BINDINGS
628 ui32_t tg_bind_cnt
; /* Overall number of bindings */
629 bool_t tg_bind_isdirty
;
630 bool_t tg_bind_isbuild
;
631 # define a_TTY_SHCUT_MAX (3 +1) /* Note: update manual on change! */
632 ui8_t tg_bind__dummy
[2];
633 char tg_bind_shcut_cancel
[n__GO_INPUT_CTX_MAX1
][a_TTY_SHCUT_MAX
];
634 char tg_bind_shcut_prompt_char
[n__GO_INPUT_CTX_MAX1
][a_TTY_SHCUT_MAX
];
635 struct a_tty_bind_ctx
*tg_bind
[n__GO_INPUT_CTX_MAX1
];
636 struct a_tty_bind_tree
*tg_bind_tree
[n__GO_INPUT_CTX_MAX1
][HSHSIZE
];
638 struct termios tg_tios_old
;
639 struct termios tg_tios_new
;
641 # ifdef HAVE_KEY_BINDINGS
642 n_CTA(n__GO_INPUT_CTX_MAX1
== 3 && a_TTY_SHCUT_MAX
== 4 &&
643 n_SIZEOF_FIELD(struct a_tty_global
, tg_bind__dummy
) == 2,
644 "Value results in array sizes that results in bad structure layout");
645 n_CTA(a_TTY_SHCUT_MAX
> 1,
646 "Users need at least one shortcut, plus NUL terminator");
651 struct a_tty_hist
*th_older
;
652 struct a_tty_hist
*th_younger
;
654 ui8_t th_flags
; /* enum a_tty_hist_flags */
655 char th_dat
[n_VFIELD_SIZE(3)];
657 n_CTA(UI8_MAX
>= a_TTY_HIST__MAX
, "Value exceeds datatype storage");
661 /* Caller pointers */
663 size_t *tl_x_bufsize
;
664 /* Input processing */
665 # ifdef HAVE_KEY_BINDINGS
666 wchar_t tl_bind_takeover
; /* Leftover byte to consume next */
667 ui8_t tl_bind_timeout
; /* In-seq. inter-byte-timer, in 1/10th secs */
668 ui8_t tl__bind_dummy
[3];
669 char (*tl_bind_shcut_cancel
)[a_TTY_SHCUT_MAX
]; /* Special _CANCEL control */
670 char (*tl_bind_shcut_prompt_char
)[a_TTY_SHCUT_MAX
]; /* ..for _PROMPT_CHAR */
671 struct a_tty_bind_tree
*(*tl_bind_tree_hmap
)[HSHSIZE
]; /* Bind lookup tree */
672 struct a_tty_bind_tree
*tl_bind_tree
;
674 /* Line data / content handling */
675 ui32_t tl_count
; /* ..of a_tty_cell's (<= a_TTY_LINE_MAX) */
676 ui32_t tl_cursor
; /* Current a_tty_cell insertion point */
678 char *cbuf
; /* *.tl_x_buf */
679 struct a_tty_cell
*cells
;
681 struct str tl_defc
; /* Current default content */
682 size_t tl_defc_cursor_byte
; /* Desired position of cursor after takeover */
683 struct str tl_savec
; /* Saved default content */
684 struct str tl_pastebuf
; /* Last snarfed data */
686 struct a_tty_hist
*tl_hist
; /* History cursor */
688 ui32_t tl_count_max
; /* ..before buffer needs to grow */
689 /* Visual data representation handling */
690 ui32_t tl_vi_flags
; /* enum a_tty_visual_flags */
691 ui32_t tl_lst_count
; /* .tl_count after last sync */
692 ui32_t tl_lst_cursor
; /* .tl_cursor after last sync */
693 /* TODO Add another indirection layer by adding a tl_phy_line of
694 * TODO a_tty_cell objects, incorporate changes in visual layer,
695 * TODO then check what _really_ has changed, sync those changes only */
696 struct a_tty_cell
const *tl_phy_start
; /* First visible cell, left border */
697 ui32_t tl_phy_cursor
; /* Physical cursor position */
698 bool_t tl_quote_rndtrip
; /* For _kht() expansion */
699 ui8_t tl__dummy2
[3 + 4];
700 ui32_t tl_goinflags
; /* enum n_go_input_flags */
701 ui32_t tl_prompt_length
; /* Preclassified (TODO needed as a_tty_cell) */
702 ui32_t tl_prompt_width
;
703 char const *tl_prompt
; /* Preformatted prompt (including colours) */
704 /* .tl_pos_buf is a hack */
706 char *tl_pos_buf
; /* mle-position colour-on, [4], reset seq. */
707 char *tl_pos
; /* Address of the [4] */
711 # ifdef HAVE_KEY_BINDINGS
712 /* C99: use [INDEX]={} */
713 n_CTAV(n_GO_INPUT_CTX_BASE
== 0);
714 n_CTAV(n_GO_INPUT_CTX_DEFAULT
== 1);
715 n_CTAV(n_GO_INPUT_CTX_COMPOSE
== 2);
716 static struct a_tty_bind_ctx_map
const
717 a_tty_bind_ctx_maps
[n__GO_INPUT_CTX_MAX1
] = {
718 {n_GO_INPUT_CTX_BASE
, "base"},
719 {n_GO_INPUT_CTX_DEFAULT
, "default"},
720 {n_GO_INPUT_CTX_COMPOSE
, "compose"}
723 /* Special functions which our MLE provides internally.
724 * Update the manual upon change! */
725 static char const a_tty_bind_fun_names
[][24] = {
728 n_FIELD_INITI(a_TTY_BIND_FUN_REDUCE(a_TTY_BIND_FUN_ ## I)) "mle-" N "\0",
731 a_X(GO_BWD
, "go-bwd") a_X(GO_FWD
, "go-fwd")
732 a_X(GO_WORD_BWD
, "go-word-bwd") a_X(GO_WORD_FWD
, "go-word-fwd")
733 a_X(GO_SCREEN_BWD
, "go-screen-bwd") a_X(GO_SCREEN_FWD
, "go-screen-fwd")
734 a_X(GO_HOME
, "go-home") a_X(GO_END
, "go-end")
735 a_X(DEL_BWD
, "del-bwd") a_X(DEL_FWD
, "del-fwd")
736 a_X(SNARF_WORD_BWD
, "snarf-word-bwd") a_X(SNARF_WORD_FWD
, "snarf-word-fwd")
737 a_X(SNARF_END
, "snarf-end") a_X(SNARF_LINE
, "snarf-line")
738 a_X(HIST_BWD
, "hist-bwd") a_X(HIST_FWD
, "hist-fwd")
739 a_X(HIST_SRCH_BWD
, "hist-srch-bwd") a_X(HIST_SRCH_FWD
, "hist-srch-fwd")
740 a_X(REPAINT
, "repaint")
741 a_X(QUOTE_RNDTRIP
, "quote-rndtrip")
742 a_X(PROMPT_CHAR
, "prompt-char")
743 a_X(COMPLETE
, "complete")
745 a_X(CLEAR_SCREEN
, "clear-screen")
747 a_X(CANCEL
, "cancel")
749 a_X(FULLRESET
, "fullreset")
750 a_X(COMMIT
, "commit")
754 # endif /* HAVE_KEY_BINDINGS */
756 /* The default key bindings (unless disallowed). Update manual upon change!
757 * A logical subset of this table is also used if !HAVE_KEY_BINDINGS (more
758 * expensive than a switch() on control codes directly, but less redundant).
759 * The table for the "base" context */
760 static struct a_tty_bind_builtin_tuple
const a_tty_bind_base_tuples
[] = {
763 {TRU1, K, 0, {'\0', (char)a_TTY_BIND_FUN_REDUCE(a_TTY_BIND_FUN_ ## S),}},
781 a_X('Q', QUOTE_RNDTRIP
)
782 a_X('R', HIST_SRCH_BWD
)
783 a_X('S', HIST_SRCH_FWD
)
786 a_X('V', PROMPT_CHAR
)
787 a_X('W', SNARF_WORD_BWD
)
788 a_X('X', GO_WORD_FWD
)
789 a_X('Y', GO_WORD_BWD
)
796 a_X('_', SNARF_WORD_FWD
)
801 # define a_X(K,S) {TRU1, K, 0, {S}},
803 /* The remains only if we have `bind' functionality available */
804 # ifdef HAVE_KEY_BINDINGS
807 {FAL0, '\0', n_TERMCAP_QUERY_ ## Q,\
808 {'\0', (char)a_TTY_BIND_FUN_REDUCE(a_TTY_BIND_FUN_ ## S),}},
810 a_X(key_backspace
, DEL_BWD
) a_X(key_dc
, DEL_FWD
)
811 a_X(key_eol
, SNARF_END
)
812 a_X(key_home
, GO_HOME
) a_X(key_end
, GO_END
)
813 a_X(key_left
, GO_BWD
) a_X(key_right
, GO_FWD
)
814 a_X(xkey_aleft
, GO_WORD_BWD
) a_X(xkey_aright
, GO_WORD_FWD
)
815 a_X(xkey_cleft
, GO_SCREEN_BWD
) a_X(xkey_cright
, GO_SCREEN_FWD
)
816 a_X(key_sleft
, GO_HOME
) a_X(key_sright
, GO_END
)
817 a_X(key_up
, HIST_BWD
) a_X(key_down
, HIST_FWD
)
818 # endif /* HAVE_KEY_BINDINGS */
821 /* The table for the "default" context */
822 static struct a_tty_bind_builtin_tuple
const a_tty_bind_default_tuples
[] = {
825 {TRU1, K, 0, {'\0', (char)a_TTY_BIND_FUN_REDUCE(a_TTY_BIND_FUN_ ## S),}},
828 # define a_X(K,S) {TRU1, K, 0, {S}},
836 /* The remains only if we have `bind' functionality available */
837 # ifdef HAVE_KEY_BINDINGS
839 # define a_X(Q,S) {FAL0, '\0', n_TERMCAP_QUERY_ ## Q, {S}},
841 a_X(key_shome
, "z0") a_X(key_send
, "z$")
842 a_X(xkey_sup
, "z0") a_X(xkey_sdown
, "z$")
843 a_X(key_ppage
, "z-") a_X(key_npage
, "z+")
844 a_X(xkey_cup
, "dotmove-") a_X(xkey_cdown
, "dotmove+")
845 # endif /* HAVE_KEY_BINDINGS */
849 static struct a_tty_global a_tty
;
851 /* Change from canonical to raw, non-canonical mode, and way back */
852 static void a_tty_term_mode(bool_t raw
);
855 /* Load and save the history file, respectively */
856 static bool_t
a_tty_hist_load(void);
857 static bool_t
a_tty_hist_save(void);
859 /* Initialize .tg_hist_size_max and return desired history file, or NULL */
860 static char const *a_tty_hist__query_config(void);
862 /* (Definetely) Add an entry TODO yet assumes hold_all_sigs() is held! */
863 static void a_tty_hist_add(char const *s
, enum n_go_input_flags gif
);
866 /* Adjust an active raw mode to use / not use a timeout */
867 # ifdef HAVE_KEY_BINDINGS
868 static void a_tty_term_rawmode_timeout(struct a_tty_line
*tlp
, bool_t enable
);
871 /* 0-X (2), UI8_MAX == \t / HT */
872 static ui8_t
a_tty_wcwidth(wchar_t wc
);
874 /* Memory / cell / word generics */
875 static void a_tty_check_grow(struct a_tty_line
*tlp
, ui32_t no
876 n_MEMORY_DEBUG_ARGS
);
877 static ssize_t
a_tty_cell2dat(struct a_tty_line
*tlp
);
878 static void a_tty_cell2save(struct a_tty_line
*tlp
);
880 /* Save away data bytes of given range (max = non-inclusive) */
881 static void a_tty_copy2paste(struct a_tty_line
*tlp
, struct a_tty_cell
*tcpmin
,
882 struct a_tty_cell
*tcpmax
);
884 /* Ask user for hexadecimal number, interpret as UTF-32 */
885 static wchar_t a_tty_vinuni(struct a_tty_line
*tlp
);
887 /* Visual screen synchronization */
888 static bool_t
a_tty_vi_refresh(struct a_tty_line
*tlp
);
890 static bool_t
a_tty_vi__paint(struct a_tty_line
*tlp
);
892 /* Search for word boundary, starting at tl_cursor, in "dir"ection (<> 0).
893 * Return <0 when moving is impossible (backward direction but in position 0,
894 * forward direction but in outermost column), and relative distance to
895 * tl_cursor otherwise */
896 static si32_t
a_tty_wboundary(struct a_tty_line
*tlp
, si32_t dir
);
898 /* Most function implementations */
899 static void a_tty_khome(struct a_tty_line
*tlp
, bool_t dobell
);
900 static void a_tty_kend(struct a_tty_line
*tlp
);
901 static void a_tty_kbs(struct a_tty_line
*tlp
);
902 static void a_tty_ksnarf(struct a_tty_line
*tlp
, bool_t cplline
, bool_t dobell
);
903 static si32_t
a_tty_kdel(struct a_tty_line
*tlp
);
904 static void a_tty_kleft(struct a_tty_line
*tlp
);
905 static void a_tty_kright(struct a_tty_line
*tlp
);
906 static void a_tty_ksnarfw(struct a_tty_line
*tlp
, bool_t fwd
);
907 static void a_tty_kgow(struct a_tty_line
*tlp
, si32_t dir
);
908 static void a_tty_kgoscr(struct a_tty_line
*tlp
, si32_t dir
);
909 static bool_t
a_tty_kother(struct a_tty_line
*tlp
, wchar_t wc
);
910 static ui32_t
a_tty_kht(struct a_tty_line
*tlp
);
913 /* Return UI32_MAX on "exhaustion" */
914 static ui32_t
a_tty_khist(struct a_tty_line
*tlp
, bool_t fwd
);
915 static ui32_t
a_tty_khist_search(struct a_tty_line
*tlp
, bool_t fwd
);
917 static ui32_t
a_tty__khist_shared(struct a_tty_line
*tlp
,
918 struct a_tty_hist
*thp
);
921 /* Handle a function */
922 static enum a_tty_fun_status
a_tty_fun(struct a_tty_line
*tlp
,
923 enum a_tty_bind_flags tbf
, size_t *len
);
926 static ssize_t
a_tty_readline(struct a_tty_line
*tlp
, size_t len
,
927 bool_t
*histok_or_null n_MEMORY_DEBUG_ARGS
);
929 # ifdef HAVE_KEY_BINDINGS
930 /* Find context or -1 */
931 static enum n_go_input_flags
a_tty_bind_ctx_find(char const *name
);
933 /* Create (or replace, if allowed) a binding */
934 static bool_t
a_tty_bind_create(struct a_tty_bind_parse_ctx
*tbpcp
,
937 /* Shared implementation to parse `bind' and `unbind' "key-sequence" and
938 * "expansion" command line arguments into something that we can work with */
939 static bool_t
a_tty_bind_parse(bool_t isbindcmd
,
940 struct a_tty_bind_parse_ctx
*tbpcp
);
942 /* Lazy resolve a termcap(5)/terminfo(5) (or *termcap*!) capability */
943 static void a_tty_bind_resolve(struct a_tty_bind_ctx
*tbcp
);
945 /* Delete an existing binding */
946 static void a_tty_bind_del(struct a_tty_bind_parse_ctx
*tbpcp
);
948 /* Life cycle of all input node trees */
949 static void a_tty_bind_tree_build(void);
950 static void a_tty_bind_tree_teardown(void);
952 static void a_tty__bind_tree_add(ui32_t hmap_idx
,
953 struct a_tty_bind_tree
*store
[HSHSIZE
],
954 struct a_tty_bind_ctx
*tbcp
);
955 static struct a_tty_bind_tree
*a_tty__bind_tree_add_wc(
956 struct a_tty_bind_tree
**treep
, struct a_tty_bind_tree
*parentp
,
957 wchar_t wc
, bool_t isseq
);
958 static void a_tty__bind_tree_free(struct a_tty_bind_tree
*tbtp
);
959 # endif /* HAVE_KEY_BINDINGS */
962 a_tty_signal(int sig
){
963 /* Prototype at top */
965 NYD_X
; /* Signal handler */
967 n_COLOUR( n_colour_env_gut(); ) /* TODO NO SIMPLE SUSPENSION POSSIBLE.. */
968 a_tty_term_mode(FAL0
);
969 n_TERMCAP_SUSPEND(TRU1
);
973 sigaddset(&nset
, sig
);
974 sigprocmask(SIG_UNBLOCK
, &nset
, &oset
);
976 /* When we come here we'll continue editing, so reestablish */
977 sigprocmask(SIG_BLOCK
, &oset
, (sigset_t
*)NULL
);
979 /* TODO THEREFORE NEED TO _GUT() .. _CREATE() ENTIRE ENVS!! */
980 n_COLOUR( n_colour_env_create(n_COLOUR_CTX_MLE
, n_tty_fp
, FAL0
); )
982 n_TERMCAP_RESUME(TRU1
);
983 a_tty_term_mode(TRU1
);
984 a_tty
.tg_line
->tl_vi_flags
|= a_TTY_VF_MOD_DIRTY
;
988 a_tty_term_mode(bool_t raw
){
989 struct termios
*tiosp
;
992 tiosp
= &a_tty
.tg_tios_old
;
996 /* Always requery the attributes, in case we've been moved from background
997 * to foreground or however else in between sessions */
998 /* XXX Always enforce ECHO and ICANON in the OLD attributes - do so as long
999 * XXX as we don't properly deal with TTIN and TTOU etc. */
1000 tcgetattr(STDIN_FILENO
, tiosp
); /* TODO v15: use _only_ n_tty_fp! */
1001 tiosp
->c_lflag
|= ECHO
| ICANON
;
1003 memcpy(&a_tty
.tg_tios_new
, tiosp
, sizeof *tiosp
);
1004 tiosp
= &a_tty
.tg_tios_new
;
1005 tiosp
->c_cc
[VMIN
] = 1;
1006 tiosp
->c_cc
[VTIME
] = 0;
1007 /* Enable ^\, ^Q and ^S to be used for key bindings */
1008 tiosp
->c_cc
[VQUIT
] = tiosp
->c_cc
[VSTART
] = tiosp
->c_cc
[VSTOP
] = '\0';
1009 tiosp
->c_iflag
&= ~(ISTRIP
| IGNCR
);
1010 tiosp
->c_lflag
&= ~(ECHO
/*| ECHOE | ECHONL */| ICANON
| IEXTEN
);
1012 tcsetattr(STDIN_FILENO
, TCSADRAIN
, tiosp
);
1016 # ifdef HAVE_HISTORY
1018 a_tty_hist_load(void){
1020 size_t lsize
, cnt
, llen
;
1029 if((v
= a_tty_hist__query_config()) == NULL
||
1030 a_tty
.tg_hist_size_max
== 0)
1033 hold_all_sigs(); /* TODO too heavy, yet we may jump even here!? */
1034 f
= fopen(v
, "r"); /* TODO HISTFILE LOAD: use linebuf pool */
1039 n_err(_("Cannot read *history-file*=%s: %s\n"),
1040 n_shexp_quote_cp(v
, FAL0
), n_err_to_doc(e
));
1044 (void)n_file_lock(fileno(f
), FLT_READ
, 0,0, UIZ_MAX
);
1046 /* Clear old history */
1048 struct a_tty_hist
*thp
;
1050 while((thp
= a_tty
.tg_hist
) != NULL
){
1051 a_tty
.tg_hist
= thp
->th_older
;
1054 a_tty
.tg_hist_tail
= NULL
;
1055 a_tty
.tg_hist_size
= 0;
1060 cnt
= (size_t)fsize(f
);
1063 while(fgetline(&lbuf
, &lsize
, &cnt
, &llen
, f
, FAL0
) != NULL
){
1065 /* Hand-edited history files may have this, probably */
1066 while(llen
> 0 && spacechar(cp
[0])){
1070 if(llen
> 0 && cp
[llen
- 1] == '\n')
1073 /* Skip empty and comment lines */
1074 if(llen
== 0 || cp
[0] == '#')
1077 if(n_UNLIKELY(version
== 0) &&
1078 (version
= strcmp(cp
, a_TTY_HIST_MARKER
) ? 1 : 2) != 1)
1082 enum n_go_input_flags gif
;
1086 /* XXX n_err(_("Skipped invalid *history-file* entry: %s\n"),
1087 * XXX n_shexp_quote_cp(cp));*/
1093 gif
= n_GO_INPUT_CTX_DEFAULT
; /* == a_TTY_HIST_CTX_DEFAULT */
1096 gif
= n_GO_INPUT_CTX_COMPOSE
; /* == a_TTY_HIST_CTX_COMPOSE */
1101 gif
|= n_GO_INPUT_HIST_GABBY
;
1106 gif
= n_GO_INPUT_CTX_DEFAULT
;
1109 gif
|= n_GO_INPUT_HIST_GABBY
;
1113 a_tty_hist_add(cp
, gif
);
1122 rele_all_sigs(); /* XXX remove jumps */
1129 a_tty_hist_save(void){
1131 struct a_tty_hist
*thp
;
1139 if((v
= a_tty_hist__query_config()) == NULL
||
1140 a_tty
.tg_hist_size_max
== 0)
1143 dogabby
= ok_blook(history_gabby_persist
);
1145 if((thp
= a_tty
.tg_hist
) != NULL
)
1146 for(i
= a_tty
.tg_hist_size_max
; thp
->th_older
!= NULL
;
1147 thp
= thp
->th_older
)
1148 if((dogabby
|| !(thp
->th_flags
& a_TTY_HIST_GABBY
)) && --i
== 0)
1151 hold_all_sigs(); /* TODO too heavy, yet we may jump even here!? */
1152 if((f
= fopen(v
, "w")) == NULL
){ /* TODO temporary + rename?! */
1156 n_err(_("Cannot write *history-file*=%s: %s\n"),
1157 n_shexp_quote_cp(v
, FAL0
), n_err_to_doc(e
));
1161 (void)n_file_lock(fileno(f
), FLT_WRITE
, 0,0, UIZ_MAX
);
1163 if(fwrite(a_TTY_HIST_MARKER
"\n", sizeof *a_TTY_HIST_MARKER
,
1164 sizeof(a_TTY_HIST_MARKER
"\n") -1, f
) !=
1165 sizeof(a_TTY_HIST_MARKER
"\n") -1)
1167 else for(; thp
!= NULL
; thp
= thp
->th_younger
){
1168 if(dogabby
|| !(thp
->th_flags
& a_TTY_HIST_GABBY
)){
1171 switch(thp
->th_flags
& a_TTY_HIST_CTX_MASK
){
1173 case a_TTY_HIST_CTX_DEFAULT
:
1176 case a_TTY_HIST_CTX_COMPOSE
:
1180 if(putc(c
, f
) == EOF
)
1183 if((thp
->th_flags
& a_TTY_HIST_GABBY
) && putc('*', f
) == EOF
)
1186 if(putc(' ', f
) == EOF
||
1187 fwrite(thp
->th_dat
, sizeof *thp
->th_dat
, thp
->th_len
, f
) !=
1188 sizeof(*thp
->th_dat
) * thp
->th_len
||
1189 putc('\n', f
) == EOF
){
1191 n_err(_("I/O error while writing *history-file* %s\n"),
1192 n_shexp_quote_cp(v
, FAL0
));
1201 rele_all_sigs(); /* XXX remove jumps */
1208 a_tty_hist__query_config(void){
1209 char const *rv
, *cp
;
1212 if((cp
= ok_vlook(NAIL_HISTSIZE
)) != NULL
)
1213 n_OBSOLETE(_("please use *history-size* instead of *NAIL_HISTSIZE*"));
1214 if((rv
= ok_vlook(history_size
)) == NULL
)
1217 a_tty
.tg_hist_size_max
= UIZ_MAX
;
1219 (void)n_idec_uiz_cp(&a_tty
.tg_hist_size_max
, rv
, 10, NULL
);
1221 if((cp
= ok_vlook(NAIL_HISTFILE
)) != NULL
)
1222 n_OBSOLETE(_("please use *history-file* instead of *NAIL_HISTFILE*"));
1223 if((rv
= ok_vlook(history_file
)) == NULL
)
1226 rv
= fexpand(rv
, FEXP_LOCAL
| FEXP_NSHELL
);
1232 a_tty_hist_add(char const *s
, enum n_go_input_flags gif
){
1234 struct a_tty_hist
*thp
, *othp
, *ythp
;
1237 l
= (ui32_t
)strlen(s
); /* xxx simply do not store if >= SI32_MAX */
1239 /* Eliminating duplicates is expensive, but simply inacceptable so
1240 * during the load of a potentially large history file! */
1241 if(n_psonce
& n_PSO_LINE_EDITOR_INIT
)
1242 for(thp
= a_tty
.tg_hist
; thp
!= NULL
; thp
= thp
->th_older
)
1243 if(thp
->th_len
== l
&& !strcmp(thp
->th_dat
, s
)){
1244 thp
->th_flags
= (gif
& a_TTY_HIST_CTX_MASK
) |
1245 (gif
& n_GO_INPUT_HIST_GABBY
? a_TTY_HIST_GABBY
: 0);
1246 othp
= thp
->th_older
;
1247 ythp
= thp
->th_younger
;
1249 othp
->th_younger
= ythp
;
1251 a_tty
.tg_hist_tail
= ythp
;
1253 ythp
->th_older
= othp
;
1255 a_tty
.tg_hist
= othp
;
1259 if(n_LIKELY(a_tty
.tg_hist_size
<= a_tty
.tg_hist_size_max
))
1260 ++a_tty
.tg_hist_size
;
1262 --a_tty
.tg_hist_size
;
1263 if((thp
= a_tty
.tg_hist_tail
) != NULL
){
1264 if((a_tty
.tg_hist_tail
= thp
->th_younger
) == NULL
)
1265 a_tty
.tg_hist
= NULL
;
1267 a_tty
.tg_hist_tail
->th_older
= NULL
;
1272 thp
= n_alloc(n_VSTRUCT_SIZEOF(struct a_tty_hist
, th_dat
) + l
+1);
1274 thp
->th_flags
= (gif
& a_TTY_HIST_CTX_MASK
) |
1275 (gif
& n_GO_INPUT_HIST_GABBY
? a_TTY_HIST_GABBY
: 0);
1276 memcpy(thp
->th_dat
, s
, l
+1);
1278 if((thp
->th_older
= a_tty
.tg_hist
) != NULL
)
1279 a_tty
.tg_hist
->th_younger
= thp
;
1281 a_tty
.tg_hist_tail
= thp
;
1282 thp
->th_younger
= NULL
;
1283 a_tty
.tg_hist
= thp
;
1286 # endif /* HAVE_HISTORY */
1288 # ifdef HAVE_KEY_BINDINGS
1290 a_tty_term_rawmode_timeout(struct a_tty_line
*tlp
, bool_t enable
){
1295 a_tty
.tg_tios_new
.c_cc
[VMIN
] = 0;
1296 if((bt
= tlp
->tl_bind_timeout
) == 0)
1297 bt
= a_TTY_BIND_TIMEOUT
;
1298 a_tty
.tg_tios_new
.c_cc
[VTIME
] = bt
;
1300 a_tty
.tg_tios_new
.c_cc
[VMIN
] = 1;
1301 a_tty
.tg_tios_new
.c_cc
[VTIME
] = 0;
1303 tcsetattr(STDIN_FILENO
, TCSANOW
, &a_tty
.tg_tios_new
);
1306 # endif /* HAVE_KEY_BINDINGS */
1309 a_tty_wcwidth(wchar_t wc
){
1313 /* Special case the reverse solidus at first */
1319 # ifdef HAVE_WCWIDTH
1320 rv
= ((i
= wcwidth(wc
)) > 0) ? (ui8_t
)i
: 0;
1322 rv
= iswprint(wc
) ? 1 + (wc
>= 0x1100u
) : 0; /* TODO use S-CText */
1330 a_tty_check_grow(struct a_tty_line
*tlp
, ui32_t no n_MEMORY_DEBUG_ARGS
){
1334 if(n_UNLIKELY((cmax
= tlp
->tl_count
+ no
) > tlp
->tl_count_max
)){
1337 i
= cmax
* sizeof(struct a_tty_cell
) + 2 * sizeof(struct a_tty_cell
);
1338 if(n_LIKELY(i
>= *tlp
->tl_x_bufsize
)){
1339 hold_all_sigs(); /* XXX v15 drop */
1342 *tlp
->tl_x_buf
= (n_realloc
)(*tlp
->tl_x_buf
, i
1343 n_MEMORY_DEBUG_ARGSCALL
);
1344 rele_all_sigs(); /* XXX v15 drop */
1346 tlp
->tl_count_max
= cmax
;
1347 *tlp
->tl_x_bufsize
= i
;
1353 a_tty_cell2dat(struct a_tty_line
*tlp
){
1359 if(n_LIKELY((i
= tlp
->tl_count
) > 0)){
1360 struct a_tty_cell
const *tcap
;
1362 tcap
= tlp
->tl_line
.cells
;
1364 memcpy(tlp
->tl_line
.cbuf
+ len
, tcap
->tc_cbuf
, tcap
->tc_count
);
1365 len
+= tcap
->tc_count
;
1366 }while(++tcap
, --i
> 0);
1369 tlp
->tl_line
.cbuf
[len
] = '\0';
1371 return (ssize_t
)len
;
1375 a_tty_cell2save(struct a_tty_line
*tlp
){
1377 struct a_tty_cell
*tcap
;
1380 tlp
->tl_savec
.s
= NULL
;
1381 tlp
->tl_savec
.l
= 0;
1383 if(n_UNLIKELY(tlp
->tl_count
== 0))
1386 for(tcap
= tlp
->tl_line
.cells
, len
= 0, i
= tlp
->tl_count
; i
> 0;
1388 len
+= tcap
->tc_count
;
1390 tlp
->tl_savec
.s
= n_autorec_alloc((tlp
->tl_savec
.l
= len
) +1);
1392 for(tcap
= tlp
->tl_line
.cells
, len
= 0, i
= tlp
->tl_count
; i
> 0;
1394 memcpy(tlp
->tl_savec
.s
+ len
, tcap
->tc_cbuf
, tcap
->tc_count
);
1395 len
+= tcap
->tc_count
;
1397 tlp
->tl_savec
.s
[len
] = '\0';
1403 a_tty_copy2paste(struct a_tty_line
*tlp
, struct a_tty_cell
*tcpmin
,
1404 struct a_tty_cell
*tcpmax
){
1406 struct a_tty_cell
*tcp
;
1411 for(tcp
= tcpmin
; tcp
< tcpmax
; ++tcp
)
1414 tlp
->tl_pastebuf
.s
= cp
= n_autorec_alloc((tlp
->tl_pastebuf
.l
= l
) +1);
1416 for(tcp
= tcpmin
; tcp
< tcpmax
; cp
+= l
, ++tcp
)
1417 memcpy(cp
, tcp
->tc_cbuf
, l
= tcp
->tc_count
);
1423 a_tty_vinuni(struct a_tty_line
*tlp
){
1431 if(!n_termcap_cmdx(n_TERMCAP_CMD_cr
) ||
1432 !n_termcap_cmd(n_TERMCAP_CMD_ce
, 0, -1))
1436 struct str
const *cpre
, *csuf
;
1440 if(n_COLOUR_IS_ACTIVE()){
1441 struct n_colour_pen
*cpen
;
1443 cpen
= n_colour_pen_create(n_COLOUR_ID_MLE_PROMPT
, NULL
);
1444 if((cpre
= n_colour_pen_to_str(cpen
)) != NULL
)
1445 csuf
= n_colour_reset_to_str();
1448 fprintf(n_tty_fp
, _("%sPlease enter Unicode code point:%s "),
1449 (cpre
!= NULL
? cpre
->s
: n_empty
),
1450 (csuf
!= NULL
? csuf
->s
: n_empty
));
1454 buf
[sizeof(buf
) -1] = '\0';
1456 if(read(STDIN_FILENO
, &buf
[i
], 1) != 1){
1457 if(n_err_no
== n_ERR_INTR
) /* xxx #if !SA_RESTART ? */
1463 if(!hexchar(buf
[i
])){
1464 char const emsg
[] = "[0-9a-fA-F]";
1466 n_LCTA(sizeof emsg
<= sizeof(buf
), "Preallocated buffer too small");
1467 memcpy(buf
, emsg
, sizeof emsg
);
1471 putc(buf
[i
], n_tty_fp
);
1473 if(++i
== sizeof buf
)
1478 if((n_idec_uiz_cp(&i
, buf
, 16, NULL
1479 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
1480 ) != n_IDEC_STATE_CONSUMED
|| i
> 0x10FFFF/* XXX magic; CText */){
1482 n_err(_("\nInvalid input: %s\n"), buf
);
1488 tlp
->tl_vi_flags
|= a_TTY_VF_MOD_DIRTY
| (wc
== '\0' ? a_TTY_VF_BELL
: 0);
1494 a_tty_vi_refresh(struct a_tty_line
*tlp
){
1498 if(tlp
->tl_vi_flags
& a_TTY_VF_BELL
){
1499 tlp
->tl_vi_flags
|= a_TTY_VF_SYNC
;
1500 if(putc('\a', n_tty_fp
) == EOF
)
1504 if(tlp
->tl_vi_flags
& a_TTY_VF_REFRESH
){
1505 /* kht may want to restore a cursor position after inserting some
1507 if(tlp
->tl_defc_cursor_byte
> 0){
1511 a_tty_khome(tlp
, FAL0
);
1513 i
= tlp
->tl_defc_cursor_byte
;
1514 tlp
->tl_defc_cursor_byte
= 0;
1515 for(j
= 0; tlp
->tl_cursor
< tlp
->tl_count
; ++j
){
1517 if((k
= tlp
->tl_line
.cells
[j
].tc_count
) > i
)
1523 if(!a_tty_vi__paint(tlp
))
1527 if(tlp
->tl_vi_flags
& a_TTY_VF_SYNC
){
1528 tlp
->tl_vi_flags
&= ~a_TTY_VF_SYNC
;
1529 if(fflush(n_tty_fp
))
1535 tlp
->tl_vi_flags
&= ~a_TTY_VF_ALL_MASK
;
1540 clearerr(n_tty_fp
); /* xxx I/O layer rewrite */
1541 n_err(_("Visual refresh failed! Is $TERM set correctly?\n"
1542 " Setting *line-editor-disable* to get us through!\n"));
1543 ok_bset(line_editor_disable
);
1549 a_tty_vi__paint(struct a_tty_line
*tlp
){
1551 a_TRUE_RV
= a_TTY__VF_LAST
<<1, /* Return value bit */
1552 a_HAVE_PROMPT
= a_TTY__VF_LAST
<<2, /* Have a prompt */
1553 a_SHOW_PROMPT
= a_TTY__VF_LAST
<<3, /* Shall print the prompt */
1554 a_MOVE_CURSOR
= a_TTY__VF_LAST
<<4, /* Move visual cursor for user! */
1555 a_LEFT_MIN
= a_TTY__VF_LAST
<<5, /* On left boundary */
1556 a_RIGHT_MAX
= a_TTY__VF_LAST
<<6,
1557 a_HAVE_POSITION
= a_TTY__VF_LAST
<<7, /* Print the position indicator */
1559 /* We carry some flags over invocations (not worth a specific field) */
1560 a_VISIBLE_PROMPT
= a_TTY__VF_LAST
<<8, /* The prompt is on the screen */
1561 a_PERSIST_MASK
= a_VISIBLE_PROMPT
,
1562 a__LAST
= a_PERSIST_MASK
1565 ui32_t f
, w
, phy_wid_base
, phy_wid
, phy_base
, phy_cur
, cnt
,
1566 DBG(lstcur COMMA
) cur
,
1567 vi_left
, /*vi_right,*/ phy_nxtcur
;
1568 struct a_tty_cell
const *tccp
, *tcp_left
, *tcp_right
, *tcxp
;
1570 n_LCTA(UICMP(64, a__LAST
, <, UI32_MAX
), "Flag bits excess storage datatype");
1572 f
= tlp
->tl_vi_flags
;
1573 tlp
->tl_vi_flags
= (f
& ~(a_TTY_VF_REFRESH
| a_PERSIST_MASK
)) |
1576 if((w
= tlp
->tl_prompt_width
) > 0)
1578 f
|= a_HAVE_POSITION
;
1580 /* XXX We don't have a OnTerminalResize event (see main.c) yet, so we need
1581 * XXX to reevaluate our circumstances over and over again */
1582 /* Don't display prompt or position indicator on very small screens */
1583 if((phy_wid_base
= (ui32_t
)n_scrnwidth
) <= a_TTY_WIDTH_RIPOFF
)
1584 f
&= ~(a_HAVE_PROMPT
| a_HAVE_POSITION
);
1586 phy_wid_base
-= a_TTY_WIDTH_RIPOFF
;
1588 /* Disable the prompt if the screen is too small; due to lack of some
1589 * indicator simply add a second ripoff */
1590 if((f
& a_HAVE_PROMPT
) && w
+ a_TTY_WIDTH_RIPOFF
>= phy_wid_base
)
1591 f
&= ~a_HAVE_PROMPT
;
1594 phy_wid
= phy_wid_base
;
1596 phy_cur
= tlp
->tl_phy_cursor
;
1597 cnt
= tlp
->tl_count
;
1598 DBG( lstcur
= tlp
->tl_lst_cursor
; )
1600 /* XXX Assume dirty screen if shrunk */
1601 if(cnt
< tlp
->tl_lst_count
)
1602 f
|= a_TTY_VF_MOD_DIRTY
;
1604 /* TODO Without HAVE_TERMCAP, it would likely be much cheaper to simply
1605 * TODO always "cr + paint + ce + ch", since ce is simulated via spaces.. */
1607 /* Quickshot: if the line is empty, possibly print prompt and out */
1609 /* In that special case dirty anything if it seems better */
1610 if((f
& a_TTY_VF_MOD_CONTENT
) || tlp
->tl_lst_count
> 0)
1611 f
|= a_TTY_VF_MOD_DIRTY
;
1613 if((f
& a_TTY_VF_MOD_DIRTY
) && phy_cur
!= 0){
1614 if(!n_termcap_cmdx(n_TERMCAP_CMD_cr
))
1619 if((f
& (a_TTY_VF_MOD_DIRTY
| a_HAVE_PROMPT
)) ==
1620 (a_TTY_VF_MOD_DIRTY
| a_HAVE_PROMPT
)){
1621 if(fputs(tlp
->tl_prompt
, n_tty_fp
) == EOF
)
1623 phy_cur
= tlp
->tl_prompt_width
+ 1;
1626 /* May need to clear former line content */
1627 if((f
& a_TTY_VF_MOD_DIRTY
) &&
1628 !n_termcap_cmd(n_TERMCAP_CMD_ce
, phy_cur
, -1))
1631 tlp
->tl_phy_start
= tlp
->tl_line
.cells
;
1635 /* Try to get an idea of the visual window */
1637 /* Find the left visual boundary */
1638 phy_wid
= (phy_wid
>> 1) + (phy_wid
>> 2);
1639 if((cur
= tlp
->tl_cursor
) == cnt
)
1642 w
= (tcp_left
= tccp
= tlp
->tl_line
.cells
+ cur
)->tc_width
;
1643 if(w
== UI8_MAX
) /* TODO yet HT == SPACE */
1645 while(tcp_left
> tlp
->tl_line
.cells
){
1646 ui16_t cw
= tcp_left
[-1].tc_width
;
1648 if(cw
== UI8_MAX
) /* TODO yet HT == SPACE */
1650 if(w
+ cw
>= phy_wid
)
1657 /* If the left hand side of our visual viewpoint consumes less than half
1658 * of the screen width, show the prompt */
1659 if(tcp_left
== tlp
->tl_line
.cells
)
1662 if((f
& (a_LEFT_MIN
| a_HAVE_PROMPT
)) == (a_LEFT_MIN
| a_HAVE_PROMPT
) &&
1663 w
+ tlp
->tl_prompt_width
< phy_wid
){
1664 phy_base
= tlp
->tl_prompt_width
;
1668 /* Then search for right boundary. We always leave the rightmost column
1669 * empty because some terminals [cw]ould wrap the line if we write into
1670 * that. XXX terminfo(5)/termcap(5) have the semi_auto_right_margin/sam/YE
1671 * XXX capability to indicate this, but we don't look at that */
1672 phy_wid
= phy_wid_base
- phy_base
;
1673 tcp_right
= tlp
->tl_line
.cells
+ cnt
;
1675 while(&tccp
[1] < tcp_right
){
1676 ui16_t cw
= tccp
[1].tc_width
;
1679 if(cw
== UI8_MAX
) /* TODO yet HT == SPACE */
1687 /*vi_right = w - vi_left;*/
1689 /* If the complete line including prompt fits on the screen, show prompt */
1690 if(--tcp_right
== tccp
){
1693 /* Since we did brute-force walk also for the left boundary we may end up
1694 * in a situation were anything effectively fits on the screen, including
1695 * the prompt that is, but were we don't recognize this since we
1696 * restricted the search to fit in some visual viewpoint. Therefore try
1697 * again to extend the left boundary to overcome that */
1698 if(!(f
& a_LEFT_MIN
)){
1699 struct a_tty_cell
const *tc1p
= tlp
->tl_line
.cells
;
1700 ui32_t vil1
= vi_left
;
1702 assert(!(f
& a_SHOW_PROMPT
));
1703 w
+= tlp
->tl_prompt_width
;
1704 for(tcxp
= tcp_left
;;){
1705 ui32_t i
= tcxp
[-1].tc_width
;
1707 if(i
== UI8_MAX
) /* TODO yet HT == SPACE */
1721 /*w -= tlp->tl_prompt_width;*/
1725 tccp
= tlp
->tl_line
.cells
+ cur
;
1727 if((f
& (a_LEFT_MIN
| a_RIGHT_MAX
| a_HAVE_PROMPT
| a_SHOW_PROMPT
)) ==
1728 (a_LEFT_MIN
| a_RIGHT_MAX
| a_HAVE_PROMPT
) &&
1729 w
+ tlp
->tl_prompt_width
<= phy_wid
){
1730 phy_wid
-= (phy_base
= tlp
->tl_prompt_width
);
1734 /* Try to avoid repainting the complete line - this is possible if the
1735 * cursor "did not leave the screen" and the prompt status hasn't changed.
1736 * I.e., after clamping virtual viewpoint, compare relation to physical */
1737 if((f
& (a_TTY_VF_MOD_SINGLE
/*FIXME*/ |
1738 a_TTY_VF_MOD_CONTENT
/* xxx */ | a_TTY_VF_MOD_DIRTY
)) ||
1739 (tcxp
= tlp
->tl_phy_start
) == NULL
||
1740 tcxp
> tccp
|| tcxp
<= tcp_right
)
1741 f
|= a_TTY_VF_MOD_DIRTY
;
1743 f
|= a_TTY_VF_MOD_DIRTY
;
1745 struct a_tty_cell
const *tcyp
;
1746 si32_t cur_displace
;
1747 ui32_t phy_lmargin
, phy_rmargin
, fx
, phy_displace
;
1749 phy_lmargin
= (fx
= phy_wid
) / 100;
1750 phy_rmargin
= fx
- (phy_lmargin
* a_TTY_SCROLL_MARGIN_RIGHT
);
1751 phy_lmargin
*= a_TTY_SCROLL_MARGIN_LEFT
;
1752 fx
= (f
& (a_SHOW_PROMPT
| a_VISIBLE_PROMPT
));
1754 if(fx
== 0 || fx
== (a_SHOW_PROMPT
| a_VISIBLE_PROMPT
)){
1760 /* We know what we have to paint, start synchronizing */
1762 assert(phy_cur
== tlp
->tl_phy_cursor
);
1763 assert(phy_wid
== phy_wid_base
- phy_base
);
1764 assert(cnt
== tlp
->tl_count
);
1766 assert(lstcur
== tlp
->tl_lst_cursor
);
1767 assert(tccp
== tlp
->tl_line
.cells
+ cur
);
1769 phy_nxtcur
= phy_base
; /* FIXME only if repaint cpl. */
1771 /* Quickshot: is it only cursor movement within the visible screen? */
1772 if((f
& a_TTY_VF_REFRESH
) == a_TTY_VF_MOD_CURSOR
){
1777 /* To be able to apply some quick jump offs, clear line if possible */
1778 if(f
& a_TTY_VF_MOD_DIRTY
){
1779 /* Force complete clearance and cursor reinitialization */
1780 if(!n_termcap_cmdx(n_TERMCAP_CMD_cr
) ||
1781 !n_termcap_cmd(n_TERMCAP_CMD_ce
, 0, -1))
1783 tlp
->tl_phy_start
= tcp_left
;
1787 if((f
& (a_TTY_VF_MOD_DIRTY
| a_SHOW_PROMPT
)) && phy_cur
!= 0){
1788 if(!n_termcap_cmdx(n_TERMCAP_CMD_cr
))
1793 if(f
& a_SHOW_PROMPT
){
1794 assert(phy_base
== tlp
->tl_prompt_width
);
1795 if(fputs(tlp
->tl_prompt
, n_tty_fp
) == EOF
)
1797 phy_cur
= phy_nxtcur
;
1798 f
|= a_VISIBLE_PROMPT
;
1800 f
&= ~a_VISIBLE_PROMPT
;
1802 /* FIXME reposition cursor for paint */
1803 for(w
= phy_nxtcur
; tcp_left
<= tcp_right
; ++tcp_left
){
1806 cw
= tcp_left
->tc_width
;
1808 if(n_LIKELY(!tcp_left
->tc_novis
)){
1809 if(fwrite(tcp_left
->tc_cbuf
, sizeof *tcp_left
->tc_cbuf
,
1810 tcp_left
->tc_count
, n_tty_fp
) != tcp_left
->tc_count
)
1812 }else{ /* XXX Shouldn't be here <-> CText, ui_str.c */
1813 char wbuf
[8]; /* XXX magic */
1815 if(n_psonce
& n_PSO_UNICODE
){
1818 wc
= (ui32_t
)tcp_left
->tc_wc
;
1819 if((wc
& ~0x1Fu
) == 0)
1825 n_utf32_to_utf8(wc
, wbuf
);
1827 wbuf
[0] = '?', wbuf
[1] = '\0';
1829 if(fputs(wbuf
, n_tty_fp
) == EOF
)
1834 if(cw
== UI8_MAX
) /* TODO yet HT == SPACE */
1837 if(tcp_left
== tccp
)
1842 /* Write something position marker alike if it does not fit on screen */
1843 if((f
& a_HAVE_POSITION
) &&
1844 ((f
& (a_LEFT_MIN
| a_RIGHT_MAX
)) != (a_LEFT_MIN
| a_RIGHT_MAX
) ||
1845 ((f
& a_HAVE_PROMPT
) && !(f
& a_SHOW_PROMPT
)))){
1847 char *posbuf
= tlp
->tl_pos_buf
, *pos
= tlp
->tl_pos
;
1849 char posbuf
[5], *pos
= posbuf
;
1854 if(phy_cur
!= (w
= phy_wid_base
) &&
1855 !n_termcap_cmd(n_TERMCAP_CMD_ch
, phy_cur
= w
, 0))
1859 if((f
& a_LEFT_MIN
) && (!(f
& a_HAVE_PROMPT
) || (f
& a_SHOW_PROMPT
)))
1860 memcpy(pos
, "^.+", 3);
1861 else if(f
& a_RIGHT_MAX
)
1862 memcpy(pos
, ".+$", 3);
1864 /* Theoretical line length limit a_TTY_LINE_MAX, choose next power of
1865 * ten (10 ** 10) to represent 100 percent, since we don't have a macro
1866 * that generates a constant, and i don't trust the standard "u type
1867 * suffix automatically scales" calculate the large number */
1868 static char const itoa
[] = "0123456789";
1870 ui64_t
const fact100
= (ui64_t
)0x3B9ACA00u
* 10u, fact
= fact100
/ 100;
1871 ui32_t i
= (ui32_t
)(((fact100
/ cnt
) * tlp
->tl_cursor
) / fact
);
1872 n_LCTA(a_TTY_LINE_MAX
<= SI32_MAX
, "a_TTY_LINE_MAX too large");
1875 pos
[0] = ' ', pos
[1] = itoa
[i
];
1877 pos
[1] = itoa
[i
% 10], pos
[0] = itoa
[i
/ 10];
1881 if(fputs(posbuf
, n_tty_fp
) == EOF
)
1886 /* Users are used to see the cursor right of the point of interest, so we
1887 * need some further adjustments unless in special conditions. Be aware
1888 * that we may have adjusted cur at the beginning, too */
1889 if((cur
= tlp
->tl_cursor
) == 0)
1890 phy_nxtcur
= phy_base
;
1891 else if(cur
!= cnt
){
1892 ui16_t cw
= tccp
->tc_width
;
1894 if(cw
== UI8_MAX
) /* TODO yet HT == SPACE */
1900 if(((f
& a_MOVE_CURSOR
) || phy_nxtcur
!= phy_cur
) &&
1901 !n_termcap_cmd(n_TERMCAP_CMD_ch
, phy_cur
= phy_nxtcur
, 0))
1905 tlp
->tl_vi_flags
|= (f
& a_PERSIST_MASK
);
1906 tlp
->tl_lst_count
= tlp
->tl_count
;
1907 tlp
->tl_lst_cursor
= tlp
->tl_cursor
;
1908 tlp
->tl_phy_cursor
= phy_cur
;
1911 return ((f
& a_TRUE_RV
) != 0);
1918 a_tty_wboundary(struct a_tty_line
*tlp
, si32_t dir
){/* TODO shell token-wise */
1920 struct a_tty_cell
*tcap
;
1925 assert(dir
== 1 || dir
== -1);
1928 cnt
= tlp
->tl_count
;
1929 cur
= tlp
->tl_cursor
;
1934 }else if(cur
+ 1 >= cnt
)
1937 --cnt
, --cur
; /* xxx Unsigned wrapping may occur (twice), then */
1939 for(rv
= 0, tcap
= tlp
->tl_line
.cells
, anynon
= FAL0
;;){
1942 wc
= tcap
[cur
+= (ui32_t
)dir
].tc_wc
;
1943 if(/*TODO not everywhere iswblank(wc)*/ wc
== L
' ' || wc
== L
'\t' ||
1955 }else if(cur
+ 1 >= cnt
){
1966 a_tty_khome(struct a_tty_line
*tlp
, bool_t dobell
){
1970 if(n_LIKELY(tlp
->tl_cursor
> 0)){
1972 f
= a_TTY_VF_MOD_CURSOR
;
1978 tlp
->tl_vi_flags
|= f
;
1983 a_tty_kend(struct a_tty_line
*tlp
){
1987 if(n_LIKELY(tlp
->tl_cursor
< tlp
->tl_count
)){
1988 tlp
->tl_cursor
= tlp
->tl_count
;
1989 f
= a_TTY_VF_MOD_CURSOR
;
1993 tlp
->tl_vi_flags
|= f
;
1998 a_tty_kbs(struct a_tty_line
*tlp
){
2002 cur
= tlp
->tl_cursor
;
2003 cnt
= tlp
->tl_count
;
2005 if(n_LIKELY(cur
> 0)){
2006 tlp
->tl_cursor
= --cur
;
2007 tlp
->tl_count
= --cnt
;
2009 if((cnt
-= cur
) > 0){
2010 struct a_tty_cell
*tcap
;
2012 tcap
= tlp
->tl_line
.cells
+ cur
;
2013 memmove(tcap
, &tcap
[1], cnt
*= sizeof(*tcap
));
2015 f
= a_TTY_VF_MOD_CURSOR
| a_TTY_VF_MOD_CONTENT
;
2019 tlp
->tl_vi_flags
|= f
;
2024 a_tty_ksnarf(struct a_tty_line
*tlp
, bool_t cplline
, bool_t dobell
){
2031 if(cplline
&& i
> 0){
2032 tlp
->tl_cursor
= i
= 0;
2033 f
= a_TTY_VF_MOD_CURSOR
;
2036 if(n_LIKELY(i
< tlp
->tl_count
)){
2037 struct a_tty_cell
*tcap
;
2039 tcap
= &tlp
->tl_line
.cells
[0];
2040 a_tty_copy2paste(tlp
, &tcap
[i
], &tcap
[tlp
->tl_count
]);
2042 f
= a_TTY_VF_MOD_CONTENT
;
2046 tlp
->tl_vi_flags
|= f
;
2051 a_tty_kdel(struct a_tty_line
*tlp
){
2056 cur
= tlp
->tl_cursor
;
2057 cnt
= tlp
->tl_count
;
2058 i
= (si32_t
)(cnt
- cur
);
2060 if(n_LIKELY(i
> 0)){
2061 tlp
->tl_count
= --cnt
;
2063 if(n_LIKELY(--i
> 0)){
2064 struct a_tty_cell
*tcap
;
2066 tcap
= &tlp
->tl_line
.cells
[cur
];
2067 memmove(tcap
, &tcap
[1], (ui32_t
)i
* sizeof(*tcap
));
2069 f
= a_TTY_VF_MOD_CONTENT
;
2070 }else if(cnt
== 0 && !ok_blook(ignoreeof
)){
2071 putc('^', n_tty_fp
);
2072 putc('D', n_tty_fp
);
2080 tlp
->tl_vi_flags
|= f
;
2086 a_tty_kleft(struct a_tty_line
*tlp
){
2090 if(n_LIKELY(tlp
->tl_cursor
> 0)){
2092 f
= a_TTY_VF_MOD_CURSOR
;
2096 tlp
->tl_vi_flags
|= f
;
2101 a_tty_kright(struct a_tty_line
*tlp
){
2105 if(n_LIKELY((i
= tlp
->tl_cursor
+ 1) <= tlp
->tl_count
)){
2107 i
= a_TTY_VF_MOD_CURSOR
;
2111 tlp
->tl_vi_flags
|= i
;
2116 a_tty_ksnarfw(struct a_tty_line
*tlp
, bool_t fwd
){
2117 struct a_tty_cell
*tcap
;
2122 if(n_UNLIKELY((i
= a_tty_wboundary(tlp
, (fwd
? +1 : -1))) <= 0)){
2123 f
= (i
< 0) ? a_TTY_VF_BELL
: a_TTY_VF_NONE
;
2127 cnt
= tlp
->tl_count
- (ui32_t
)i
;
2128 cur
= tlp
->tl_cursor
;
2131 tcap
= &tlp
->tl_line
.cells
[cur
];
2133 a_tty_copy2paste(tlp
, &tcap
[0], &tcap
[i
]);
2135 if((tlp
->tl_count
= cnt
) != (tlp
->tl_cursor
= cur
)){
2137 memmove(&tcap
[0], &tcap
[i
], cnt
* sizeof(*tcap
)); /* FIXME*/
2140 f
= a_TTY_VF_MOD_CURSOR
| a_TTY_VF_MOD_CONTENT
;
2142 tlp
->tl_vi_flags
|= f
;
2147 a_tty_kgow(struct a_tty_line
*tlp
, si32_t dir
){
2152 if(n_UNLIKELY((i
= a_tty_wboundary(tlp
, dir
)) <= 0))
2153 f
= (i
< 0) ? a_TTY_VF_BELL
: a_TTY_VF_NONE
;
2157 tlp
->tl_cursor
+= (ui32_t
)i
;
2158 f
= a_TTY_VF_MOD_CURSOR
;
2161 tlp
->tl_vi_flags
|= f
;
2166 a_tty_kgoscr(struct a_tty_line
*tlp
, si32_t dir
){
2167 ui32_t sw
, i
, cur
, f
, cnt
;
2170 sw
= (ui32_t
)n_scrnwidth
- 2;
2171 if(sw
> (i
= tlp
->tl_prompt_width
))
2173 cur
= tlp
->tl_cursor
;
2177 for(cnt
= tlp
->tl_count
; cur
< cnt
&& sw
> 0; ++cur
){
2178 i
= tlp
->tl_line
.cells
[cur
].tc_width
;
2183 while(cur
> 0 && sw
> 0){
2184 i
= tlp
->tl_line
.cells
[--cur
].tc_width
;
2189 if(cur
!= tlp
->tl_cursor
){
2190 tlp
->tl_cursor
= cur
;
2191 f
= a_TTY_VF_MOD_CURSOR
;
2194 tlp
->tl_vi_flags
|= f
;
2199 a_tty_kother(struct a_tty_line
*tlp
, wchar_t wc
){
2200 /* Append if at EOL, insert otherwise;
2201 * since we may move around character-wise, always use a fresh ps */
2203 struct a_tty_cell tc
, *tcap
;
2211 n_LCTA(a_TTY_LINE_MAX
<= SI32_MAX
, "a_TTY_LINE_MAX too large");
2212 if(tlp
->tl_count
+ 1 >= a_TTY_LINE_MAX
){
2213 n_err(_("Stop here, we can't extend line beyond size limit\n"));
2217 /* First init a cell and see whether we'll really handle this wc */
2218 memset(&ps
, 0, sizeof ps
);
2222 l
= wcrtomb(tc
.tc_cbuf
, tc
.tc_wc
= wc
, &ps
);
2223 if(n_UNLIKELY(l
> MB_LEN_MAX
)){
2225 n_err(_("wcrtomb(3) error: too many multibyte character bytes\n"));
2228 tc
.tc_count
= (ui16_t
)l
;
2230 if(n_UNLIKELY((n_psonce
& n_PSO_ENC_MBSTATE
) != 0)){
2231 l
= wcrtomb(&tc
.tc_cbuf
[l
], L
'\0', &ps
);
2232 if(n_LIKELY(l
== 1))
2233 /* Only NUL terminator */;
2234 else if(n_LIKELY(--l
< MB_LEN_MAX
))
2235 tc
.tc_count
+= (ui16_t
)l
;
2241 /* Yes, we will! Place it in the array */
2242 tc
.tc_novis
= (iswprint(wc
) == 0);
2243 tc
.tc_width
= a_tty_wcwidth(wc
);
2244 /* TODO if(tc.tc_novis && tc.tc_width > 0) */
2246 cur
= tlp
->tl_cursor
++;
2247 cnt
= tlp
->tl_count
++ - cur
;
2248 tcap
= &tlp
->tl_line
.cells
[cur
];
2250 memmove(&tcap
[1], tcap
, cnt
* sizeof(*tcap
));
2251 f
= a_TTY_VF_MOD_CONTENT
;
2253 f
= a_TTY_VF_MOD_SINGLE
;
2254 memcpy(tcap
, &tc
, sizeof *tcap
);
2256 f
|= a_TTY_VF_MOD_CURSOR
;
2261 tlp
->tl_vi_flags
|= f
;
2267 a_tty_kht(struct a_tty_line
*tlp
){
2268 ui8_t (*mempool
)[n_MEMORY_POOL_TYPE_SIZEOF
], *mempool_persist
;
2270 struct str orig
, bot
, topp
, sub
, exp
, preexp
;
2271 struct n_string shou
, *shoup
;
2272 struct a_tty_cell
*ctop
, *cx
;
2273 bool_t wedid
, set_savec
;
2277 /* Get plain line data; if this is the first expansion/xy, update the
2278 * very original content so that ^G gets the origin back */
2279 orig
= tlp
->tl_savec
;
2280 a_tty_cell2save(tlp
);
2281 exp
= tlp
->tl_savec
;
2283 /*tlp->tl_savec = orig;*/
2289 mempool_persist
= n_memory_pool_top();
2290 n_memory_pool_push(mempool
= n_lofi_alloc(sizeof *mempool
), TRU1
);
2292 shoup
= n_string_creat_auto(&shou
);
2297 struct a_tty_cell
*cword
;
2299 /* Find the word to be expanded */
2300 cword
= tlp
->tl_line
.cells
;
2301 ctop
= &cword
[tlp
->tl_cursor
];
2302 cx
= &cword
[tlp
->tl_count
];
2304 /* topp: separate data right of cursor */
2306 for(rv
= 0; ctop
< cx
; ++ctop
)
2307 rv
+= ctop
->tc_count
;
2309 topp
.s
= orig
.s
+ orig
.l
- rv
;
2310 ctop
= cword
+ tlp
->tl_cursor
;
2312 topp
.s
= NULL
, topp
.l
= 0;
2314 /* Find the shell token that corresponds to the cursor position */
2317 for(; cword
< ctop
; ++cword
)
2318 max
+= cword
->tc_count
;
2326 enum n_shexp_state shs
;
2329 shs
= n_shexp_parse_token((n_SHEXP_PARSE_DRYRUN
|
2330 n_SHEXP_PARSE_TRIM_SPACE
| n_SHEXP_PARSE_IGNORE_EMPTY
|
2331 n_SHEXP_PARSE_QUOTE_AUTO_CLOSE
), NULL
, &sub
, NULL
);
2335 assert(max
>= sub
.l
);
2341 if(shs
& n_SHEXP_STATE_ERR_MASK
){
2342 n_err(_("Invalid completion pattern: %.*s\n"),
2348 /* All WS? Trailing WS that has been "jumped over"? */
2349 if(exp
.l
== 0 || (shs
& n_SHEXP_STATE_WS_TRAIL
))
2352 n_shexp_parse_token((n_SHEXP_PARSE_TRIM_SPACE
|
2353 n_SHEXP_PARSE_IGNORE_EMPTY
| n_SHEXP_PARSE_QUOTE_AUTO_CLOSE
),
2358 sub
.s
= n_string_cp(shoup
);
2359 sub
.l
= shoup
->s_len
;
2363 /* Leave room for "implicit asterisk" expansion, as below */
2365 sub
.s
= n_UNCONST(n_star
);
2366 sub
.l
= sizeof(n_star
) -1;
2369 preexp
.s
= n_UNCONST(n_empty
);
2370 preexp
.l
= sizeof(n_empty
) -1;
2373 /* TODO Super-Heavy-Metal: block all sigs, avoid leaks on jump */
2375 exp
.s
= fexpand(sub
.s
, a_TTY_TAB_FEXP_FL
);
2378 if(exp
.s
== NULL
|| (exp
.l
= strlen(exp
.s
)) == 0){
2381 /* No. But maybe the users' desire was to complete only a part of the
2382 * shell token of interest! TODO This can be improved, we would need to
2383 * TODO have shexp_parse to create a DOM structure of parsed snippets, so
2384 * TODO that we can tell for each snippet which quote is active and
2385 * TODO whether we may cross its boundary and/or apply expansion for it!
2386 * TODO Only like that we would be able to properly requote user input
2387 * TODO like "'['a-z]<TAB>" to e.g. "\[a-z]" for glob purposes! */
2392 for(li
= UIZ_MAX
, i
= sub
.l
; i
-- > 0;){
2395 if((c
= sub
.s
[i
]) == '/' || c
== '+' /* *folder*! */)
2397 /* Do stop once some "magic" characters are seen XXX magic set */
2398 else if(c
== '<' || c
== '>' || c
== '=' || c
== ':')
2410 /* A different case is that the user input includes for example character
2411 * classes: here fexpand() will go over glob, and that will not find any
2412 * match, thus returning NULL; try to wildcard expand this pattern! */
2414 if(sub
.s
[sub
.l
- 1] != '*'){
2416 shoup
= n_string_push_c(shoup
, '*');
2417 sub
.s
= n_string_cp(shoup
);
2418 sub
.l
= shoup
->s_len
;
2424 if(wedid
== TRUM1
&& preexp
.l
> 0)
2425 preexp
.s
= savestrbuf(preexp
.s
, preexp
.l
);
2427 /* May be multi-return! */
2428 if(n_pstate
& n_PS_EXPAND_MULTIRESULT
)
2431 /* xxx That is not really true since the limit counts characters not bytes */
2432 n_LCTA(a_TTY_LINE_MAX
<= SI32_MAX
, "a_TTY_LINE_MAX too large");
2433 if(exp
.l
>= a_TTY_LINE_MAX
- 1 || a_TTY_LINE_MAX
- 1 - exp
.l
< preexp
.l
){
2434 n_err(_("Tabulator expansion would extend beyond line size limit\n"));
2439 /* If the expansion equals the original string, assume the user wants what
2440 * is usually known as tab completion, append `*' and restart */
2441 if(!wedid
&& exp
.l
== sub
.l
&& !memcmp(exp
.s
, sub
.s
, exp
.l
))
2444 if(exp
.s
[exp
.l
- 1] != '/'){
2445 if(!stat(exp
.s
, &sb
) && S_ISDIR(sb
.st_mode
)){
2446 shoup
= n_string_assign_buf(shoup
, exp
.s
, exp
.l
);
2447 shoup
= n_string_push_c(shoup
, '/');
2448 exp
.s
= n_string_cp(shoup
);
2452 exp
.s
[exp
.l
] = '\0';
2455 exp
.l
= strlen(exp
.s
= n_shexp_quote_cp(exp
.s
, tlp
->tl_quote_rndtrip
));
2456 tlp
->tl_defc_cursor_byte
= bot
.l
+ preexp
.l
+ exp
.l
-1;
2458 orig
.l
= bot
.l
+ preexp
.l
+ exp
.l
+ topp
.l
;
2459 orig
.s
= n_autorec_alloc_from_pool(mempool_persist
, orig
.l
+ 5 +1);
2460 if((rv
= (ui32_t
)bot
.l
) > 0)
2461 memcpy(orig
.s
, bot
.s
, rv
);
2463 memcpy(&orig
.s
[rv
], preexp
.s
, preexp
.l
);
2466 memcpy(&orig
.s
[rv
], exp
.s
, exp
.l
);
2469 memcpy(&orig
.s
[rv
], topp
.s
, topp
.l
);
2474 tlp
->tl_defc
= orig
;
2475 tlp
->tl_count
= tlp
->tl_cursor
= 0;
2476 f
|= a_TTY_VF_MOD_DIRTY
;
2478 n_memory_pool_pop(mempool
, TRU1
);
2479 n_lofi_free(mempool
);
2480 tlp
->tl_vi_flags
|= f
;
2485 struct n_visual_info_ctx vic
;
2490 size_t locolen
, scrwid
, lnlen
, lncnt
, prefixlen
;
2493 if((fp
= Ftmp(NULL
, "tabex", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
){
2494 n_perr(_("tmpfile"), 0);
2498 /* How long is the result string for real? Search the NUL NUL
2499 * terminator. While here, detect the longest entry to perform an
2500 * initial allocation of our accumulator string */
2505 i
= strlen(&exp
.s
[++exp
.l
]);
2506 locolen
= n_MAX(locolen
, i
);
2508 }while(exp
.s
[exp
.l
+ 1] != '\0');
2510 shoup
= n_string_reserve(n_string_trunc(shoup
, 0),
2511 locolen
+ (locolen
>> 1));
2513 /* Iterate (once again) over all results */
2514 scrwid
= n_SCRNWIDTH_FOR_LISTS
;
2516 n_UNINIT(prefixlen
, 0);
2517 n_UNINIT(lococp
, NULL
);
2519 for(isfirst
= TRU1
; exp
.l
> 0; isfirst
= FAL0
, c1
= c2
){
2521 char const *fullpath
;
2525 sub
.l
= i
= strlen(sub
.s
);
2527 if((exp
.l
-= i
) > 0)
2531 /* Separate dirname and basename */
2536 if((cp
= strrchr(fullpath
, '/')) != NULL
)
2537 prefixlen
= PTR2SIZE(++cp
- fullpath
);
2541 if(prefixlen
> 0 && prefixlen
< sub
.l
){
2546 /* We want case-insensitive sort-order */
2547 memset(&vic
, 0, sizeof vic
);
2548 vic
.vic_indat
= sub
.s
;
2549 vic
.vic_inlen
= sub
.l
;
2550 c2
= n_visual_info(&vic
, n_VISUAL_INFO_ONE_CHAR
) ? vic
.vic_waccu
2552 #ifdef HAVE_C90AMEND1
2558 /* Query longest common prefix along the way */
2563 }else if(locolen
> 0){
2564 for(i
= 0; i
< locolen
; ++i
)
2565 if(lococp
[i
] != sub
.s
[i
]){
2566 i
= field_detect_clip(i
, lococp
, i
);
2572 /* Prepare display */
2574 shoup
= n_shexp_quote(n_string_trunc(shoup
, 0), &input
,
2575 tlp
->tl_quote_rndtrip
);
2576 memset(&vic
, 0, sizeof vic
);
2577 vic
.vic_indat
= shoup
->s_dat
;
2578 vic
.vic_inlen
= shoup
->s_len
;
2579 if(!n_visual_info(&vic
,
2580 n_VISUAL_INFO_SKIP_ERRORS
| n_VISUAL_INFO_WIDTH_QUERY
))
2581 vic
.vic_vi_width
= shoup
->s_len
;
2583 /* Put on screen. Indent follow lines of same sort slot.
2584 * Leave enough room for filename tagging */
2585 if((c1
= (c1
!= c2
))){
2586 #ifdef HAVE_C90AMEND1
2587 c1
= (iswalnum(c2
) != 0);
2589 c1
= (alnumchar(c2
) != 0);
2593 scrwid
< lnlen
|| scrwid
- lnlen
<= vic
.vic_vi_width
+ 2){
2600 }else if(lnlen
> 0){
2605 fputs(n_string_cp(shoup
), fp
);
2606 lnlen
+= vic
.vic_vi_width
;
2608 /* Support the known filename tagging
2609 * XXX *line-editor-completion-filetype* or so */
2610 if(!lstat(fullpath
, &sb
)){
2613 if(S_ISDIR(sb
.st_mode
))
2615 else if(S_ISLNK(sb
.st_mode
))
2618 else if(S_ISFIFO(sb
.st_mode
))
2622 else if(S_ISSOCK(sb
.st_mode
))
2626 else if(S_ISCHR(sb
.st_mode
))
2630 else if(S_ISBLK(sb
.st_mode
))
2643 page_or_print(fp
, lncnt
);
2647 n_string_gut(shoup
);
2649 /* A common prefix of 0 means we cannot provide the user any auto
2650 * completed characters for the multiple possible results.
2651 * Otherwise we can, so extend the visual line content by the common
2652 * prefix (in a reversible way) */
2653 f
|= a_TTY_VF_BELL
; /* XXX -> *line-editor-completion-bell*? or so */
2655 (exp
.s
= n_UNCONST(lococp
))[locolen
] = '\0';
2657 exp
.l
= (locolen
+= prefixlen
);
2663 /* If we've provided a default content, but failed to expand, there is
2664 * nothing we can "revert to": drop that default again */
2666 tlp
->tl_savec
.s
= NULL
;
2667 tlp
->tl_savec
.l
= 0;
2669 f
&= a_TTY_VF_BELL
; /* XXX -> *line-editor-completion-bell*? or so */
2674 # ifdef HAVE_HISTORY
2676 a_tty__khist_shared(struct a_tty_line
*tlp
, struct a_tty_hist
*thp
){
2680 if(n_LIKELY((tlp
->tl_hist
= thp
) != NULL
)){
2685 if(tlp
->tl_goinflags
& n_GO_INPUT_CTX_COMPOSE
){
2687 if(!(thp
->th_flags
& a_TTY_HIST_CTX_COMPOSE
))
2690 tlp
->tl_defc
.s
= cp
= n_autorec_alloc(i
+1);
2691 if(tlp
->tl_goinflags
& n_GO_INPUT_CTX_COMPOSE
){
2692 if((*cp
= ok_vlook(escape
)[0]) == '\0')
2695 if(!(thp
->th_flags
& a_TTY_HIST_CTX_COMPOSE
))
2698 memcpy(cp
, thp
->th_dat
, thp
->th_len
+1);
2699 rv
= tlp
->tl_defc
.l
= i
;
2701 f
= (tlp
->tl_count
> 0) ? a_TTY_VF_MOD_DIRTY
: a_TTY_VF_NONE
;
2702 tlp
->tl_count
= tlp
->tl_cursor
= 0;
2708 tlp
->tl_vi_flags
|= f
;
2714 a_tty_khist(struct a_tty_line
*tlp
, bool_t fwd
){
2715 struct a_tty_hist
*thp
;
2719 /* If we're not in history mode yet, save line content */
2720 if((thp
= tlp
->tl_hist
) == NULL
){
2721 a_tty_cell2save(tlp
);
2722 if((thp
= a_tty
.tg_hist
) == NULL
)
2725 while(thp
->th_older
!= NULL
)
2726 thp
= thp
->th_older
;
2730 while((thp
= fwd
? thp
->th_younger
: thp
->th_older
) != NULL
){
2731 /* Applicable to input context? Compose mode swallows anything */
2732 if((tlp
->tl_goinflags
& n__GO_INPUT_CTX_MASK
) == n_GO_INPUT_CTX_COMPOSE
)
2734 if((thp
->th_flags
& a_TTY_HIST_CTX_MASK
) != a_TTY_HIST_CTX_COMPOSE
)
2738 rv
= a_tty__khist_shared(tlp
, thp
);
2744 a_tty_khist_search(struct a_tty_line
*tlp
, bool_t fwd
){
2745 struct str orig_savec
;
2747 struct a_tty_hist
*thp
;
2752 /* We cannot complete an empty line */
2753 if(n_UNLIKELY(tlp
->tl_count
== 0)){
2754 /* XXX The upcoming hard reset would restore a set savec buffer,
2755 * XXX so forcefully reset that. A cleaner solution would be to
2756 * XXX reset it whenever a restore is no longer desired */
2757 tlp
->tl_savec
.s
= NULL
;
2758 tlp
->tl_savec
.l
= 0;
2762 /* xxx It is a bit of a hack, but let's just hard-code the knowledge that
2763 * xxx in compose mode the first character is *escape* and must be skipped
2764 * xxx when searching the history. Alternatively we would need to have
2765 * xxx context-specific history search hooks which would do the search,
2766 * xxx which is overkill for our sole special case compose mode */
2767 if((tlp
->tl_goinflags
& n__GO_INPUT_CTX_MASK
) == n_GO_INPUT_CTX_COMPOSE
)
2772 if((thp
= tlp
->tl_hist
) == NULL
){
2773 a_tty_cell2save(tlp
);
2774 if((thp
= a_tty
.tg_hist
) == NULL
) /* TODO Should end "doing nothing"! */
2777 while(thp
->th_older
!= NULL
)
2778 thp
= thp
->th_older
;
2779 orig_savec
.s
= NULL
;
2780 orig_savec
.l
= 0; /* silence CC */
2782 while((thp
= fwd
? thp
->th_younger
: thp
->th_older
) != NULL
){
2783 /* Applicable to input context? Compose mode swallows anything */
2786 if((thp
->th_flags
& a_TTY_HIST_CTX_MASK
) != a_TTY_HIST_CTX_COMPOSE
)
2792 orig_savec
= tlp
->tl_savec
;
2795 if(xoff
>= tlp
->tl_savec
.l
){
2800 if(orig_savec
.s
== NULL
)
2801 a_tty_cell2save(tlp
);
2803 for(; thp
!= NULL
; thp
= fwd
? thp
->th_younger
: thp
->th_older
){
2804 /* Applicable to input context? Compose mode swallows anything */
2805 if(xoff
!= 1 && (thp
->th_flags
& a_TTY_HIST_CTX_MASK
) ==
2806 a_TTY_HIST_CTX_COMPOSE
)
2808 if(is_prefix(&tlp
->tl_savec
.s
[xoff
], thp
->th_dat
))
2812 if(orig_savec
.s
!= NULL
)
2813 tlp
->tl_savec
= orig_savec
;
2815 rv
= a_tty__khist_shared(tlp
, thp
);
2819 # endif /* HAVE_HISTORY */
2821 static enum a_tty_fun_status
2822 a_tty_fun(struct a_tty_line
*tlp
, enum a_tty_bind_flags tbf
, size_t *len
){
2823 enum a_tty_fun_status rv
;
2826 rv
= a_TTY_FUN_STATUS_OK
;
2828 # define a_X(N) a_TTY_BIND_FUN_REDUCE(a_TTY_BIND_FUN_ ## N)
2829 switch(a_TTY_BIND_FUN_REDUCE(tbf
)){
2831 tlp
->tl_vi_flags
|= a_TTY_VF_BELL
;
2839 case a_X(GO_WORD_BWD
):
2840 a_tty_kgow(tlp
, -1);
2842 case a_X(GO_WORD_FWD
):
2843 a_tty_kgow(tlp
, +1);
2845 case a_X(GO_SCREEN_BWD
):
2846 a_tty_kgoscr(tlp
, -1);
2848 case a_X(GO_SCREEN_FWD
):
2849 a_tty_kgoscr(tlp
, +1);
2852 a_tty_khome(tlp
, TRU1
);
2861 if(a_tty_kdel(tlp
) < 0)
2862 rv
= a_TTY_FUN_STATUS_END
;
2864 case a_X(SNARF_WORD_BWD
):
2865 a_tty_ksnarfw(tlp
, FAL0
);
2867 case a_X(SNARF_WORD_FWD
):
2868 a_tty_ksnarfw(tlp
, TRU1
);
2870 case a_X(SNARF_END
):
2871 a_tty_ksnarf(tlp
, FAL0
, TRU1
);
2873 case a_X(SNARF_LINE
):
2874 a_tty_ksnarf(tlp
, TRU1
, (tlp
->tl_count
== 0));
2877 case a_X(HIST_FWD
):{
2878 # ifdef HAVE_HISTORY
2879 bool_t isfwd
= TRU1
;
2885 # ifdef HAVE_HISTORY
2888 if((*len
= a_tty_khist(tlp
, isfwd
)) != UI32_MAX
){
2889 rv
= a_TTY_FUN_STATUS_RESTART
;
2895 tlp
->tl_vi_flags
|= a_TTY_VF_BELL
;
2898 case a_X(HIST_SRCH_FWD
):{
2899 # ifdef HAVE_HISTORY
2900 bool_t isfwd
= TRU1
;
2905 case a_X(HIST_SRCH_BWD
):
2906 # ifdef HAVE_HISTORY
2909 if((*len
= a_tty_khist_search(tlp
, isfwd
)) != UI32_MAX
){
2910 rv
= a_TTY_FUN_STATUS_RESTART
;
2915 tlp
->tl_vi_flags
|= a_TTY_VF_BELL
;
2920 tlp
->tl_vi_flags
|= a_TTY_VF_MOD_DIRTY
;
2922 case a_X(QUOTE_RNDTRIP
):
2923 tlp
->tl_quote_rndtrip
= !tlp
->tl_quote_rndtrip
;
2925 case a_X(PROMPT_CHAR
):{
2928 if((wc
= a_tty_vinuni(tlp
)) > 0)
2929 a_tty_kother(tlp
, wc
);
2932 if((*len
= a_tty_kht(tlp
)) > 0)
2933 rv
= a_TTY_FUN_STATUS_RESTART
;
2937 if(tlp
->tl_pastebuf
.l
> 0)
2938 *len
= (tlp
->tl_defc
= tlp
->tl_pastebuf
).l
;
2940 tlp
->tl_vi_flags
|= a_TTY_VF_BELL
;
2943 case a_X(CLEAR_SCREEN
):
2944 tlp
->tl_vi_flags
|= (n_termcap_cmdx(n_TERMCAP_CMD_cl
) == TRU1
)
2945 ? a_TTY_VF_MOD_DIRTY
: a_TTY_VF_BELL
;
2949 /* Normally this just causes a restart and thus resets the state
2951 if(tlp
->tl_savec
.l
== 0 && tlp
->tl_defc
.l
== 0){
2953 # ifdef HAVE_KEY_BINDINGS
2954 tlp
->tl_bind_takeover
= '\0';
2956 tlp
->tl_vi_flags
|= a_TTY_VF_BELL
;
2957 rv
= a_TTY_FUN_STATUS_RESTART
;
2961 if(tlp
->tl_count
== 0 && tlp
->tl_savec
.l
== 0 && tlp
->tl_defc
.l
== 0){
2962 # ifdef HAVE_KEY_BINDINGS
2963 tlp
->tl_bind_takeover
= '\0';
2965 tlp
->tl_vi_flags
|= a_TTY_VF_MOD_DIRTY
| a_TTY_VF_BELL
;
2968 case a_X(FULLRESET
):
2969 tlp
->tl_savec
.s
= tlp
->tl_defc
.s
= NULL
;
2970 tlp
->tl_savec
.l
= tlp
->tl_defc
.l
= 0;
2971 tlp
->tl_defc_cursor_byte
= 0;
2972 tlp
->tl_vi_flags
|= a_TTY_VF_BELL
;
2975 # ifdef HAVE_KEY_BINDINGS
2976 tlp
->tl_bind_takeover
= '\0';
2978 tlp
->tl_vi_flags
|= a_TTY_VF_MOD_DIRTY
;
2979 tlp
->tl_cursor
= tlp
->tl_count
= 0;
2980 # ifdef HAVE_HISTORY
2981 tlp
->tl_hist
= NULL
;
2983 if((*len
= tlp
->tl_savec
.l
) != 0){
2984 tlp
->tl_defc
= tlp
->tl_savec
;
2985 tlp
->tl_savec
.s
= NULL
;
2986 tlp
->tl_savec
.l
= 0;
2988 *len
= tlp
->tl_defc
.l
;
2989 rv
= a_TTY_FUN_STATUS_RESTART
;
2994 rv
= a_TTY_FUN_STATUS_COMMIT
;
3004 a_tty_readline(struct a_tty_line
*tlp
, size_t len
, bool_t
*histok_or_null
3005 n_MEMORY_DEBUG_ARGS
){
3006 /* We want to save code, yet we may have to incorporate a lines'
3007 * default content and / or default input to switch back to after some
3008 * history movement; let "len > 0" mean "have to display some data
3009 * buffer" -> a_BUFMODE, and only otherwise read(2) it */
3011 char cbuf_base
[MB_LEN_MAX
* 2], *cbuf
, *cbufp
;
3013 struct a_tty_bind_tree
*tbtp
;
3015 enum a_tty_bind_flags tbf
;
3016 enum {a_NONE
, a_WAS_HERE
= 1<<0, a_BUFMODE
= 1<<1, a_MAYBEFUN
= 1<<2,
3017 a_TIMEOUT
= 1<<3, a_TIMEOUT_EXPIRED
= 1<<4,
3018 a_TIMEOUT_MASK
= a_TIMEOUT
| a_TIMEOUT_EXPIRED
,
3019 a_READ_LOOP_MASK
= ~(a_WAS_HERE
| a_MAYBEFUN
| a_TIMEOUT_MASK
)
3024 # ifdef HAVE_KEY_BINDINGS
3025 assert(tlp
->tl_bind_takeover
== '\0');
3028 memset(ps
, 0, sizeof ps
);
3030 tlp
->tl_vi_flags
|= a_TTY_VF_REFRESH
| a_TTY_VF_SYNC
;
3037 /* Ensure we have valid pointers, and room for grow */
3038 a_tty_check_grow(tlp
, ((flags
& a_BUFMODE
) ? (ui32_t
)len
: 1)
3039 n_MEMORY_DEBUG_ARGSCALL
);
3041 /* Handle visual state flags, except in buffer mode */
3042 if(!(flags
& a_BUFMODE
) && (tlp
->tl_vi_flags
& a_TTY_VF_ALL_MASK
))
3043 if(!a_tty_vi_refresh(tlp
)){
3048 /* Ready for messing around.
3049 * Normal read(2)? Else buffer mode: speed this one up */
3050 if(!(flags
& a_BUFMODE
)){
3054 assert(tlp
->tl_defc
.l
> 0 && tlp
->tl_defc
.s
!= NULL
);
3055 assert(tlp
->tl_defc
.l
>= len
);
3057 cbuf
= tlp
->tl_defc
.s
+ (tlp
->tl_defc
.l
- len
);
3061 /* Read in the next complete multibyte character */
3063 # ifdef HAVE_KEY_BINDINGS
3064 struct a_tty_bind_tree
*xtbtp
;
3068 struct a_tty_bind_tree
*tbtp
;
3071 isp_head
= isp
= NULL
;
3074 for(flags
&= a_READ_LOOP_MASK
;;){
3075 # ifdef HAVE_KEY_BINDINGS
3076 if(!(flags
& a_BUFMODE
) && tlp
->tl_bind_takeover
!= '\0'){
3077 wc
= tlp
->tl_bind_takeover
;
3078 tlp
->tl_bind_takeover
= '\0';
3082 if(!(flags
& a_BUFMODE
)){
3083 /* Let me at least once dream of iomon(itor), timer with
3084 * one-shot, enwrapped with key_event and key_sequence_event,
3085 * all driven by an event_loop */
3086 /* TODO v15 Until we have SysV signal handling all through we
3087 * TODO need to temporarily adjust our BSD signal handler with
3088 * TODO a SysV one, here */
3089 n_sighdl_t otstp
, ottin
, ottou
;
3091 otstp
= n_signal(SIGTSTP
, &a_tty_signal
);
3092 ottin
= n_signal(SIGTTIN
, &a_tty_signal
);
3093 ottou
= n_signal(SIGTTOU
, &a_tty_signal
);
3094 # ifdef HAVE_KEY_BINDINGS
3095 flags
&= ~a_TIMEOUT_MASK
;
3096 if(isp
!= NULL
&& (tbtp
= isp
->tbtp
)->tbt_isseq
&&
3097 !tbtp
->tbt_isseq_trail
){
3098 a_tty_term_rawmode_timeout(tlp
, TRU1
);
3103 while((rv
= read(STDIN_FILENO
, cbufp
, 1)) < 1){
3105 if(n_err_no
== n_ERR_INTR
){
3106 if((tlp
->tl_vi_flags
& a_TTY_VF_MOD_DIRTY
) &&
3107 !a_tty_vi_refresh(tlp
))
3114 # ifdef HAVE_KEY_BINDINGS
3115 /* Timeout expiration */
3117 assert(flags
& a_TIMEOUT
);
3118 assert(isp
!= NULL
);
3119 a_tty_term_rawmode_timeout(tlp
, FAL0
);
3121 /* Something "atomic" broke. Maybe the current one can
3122 * also be terminated already, by itself? xxx really? */
3123 if((tbtp
= isp
->tbtp
)->tbt_bind
!= NULL
){
3124 tlp
->tl_bind_takeover
= wc
;
3128 /* Or, maybe there is a second path without a timeout;
3129 * this should be covered by .tbt_isseq_trail, but then
3130 * again a single-layer implementation cannot "know" */
3131 for(xtbtp
= tbtp
; (xtbtp
= xtbtp
->tbt_sibling
) != NULL
;)
3132 if(xtbtp
->tbt_char
== tbtp
->tbt_char
){
3133 assert(!xtbtp
->tbt_isseq
);
3136 /* Lay down on read(2)? */
3141 # endif /* HAVE_KEY_BINDINGS */
3144 # ifdef HAVE_KEY_BINDINGS
3145 if(flags
& a_TIMEOUT
)
3146 a_tty_term_rawmode_timeout(tlp
, FAL0
);
3148 safe_signal(SIGTSTP
, otstp
);
3149 safe_signal(SIGTTIN
, ottin
);
3150 safe_signal(SIGTTOU
, ottou
);
3154 /* As a special case, simulate EOF via EOT (which can happen
3155 * via type-ahead as when typing "yes\n^@" during sleep of
3156 * $ sleep 5; mail -s byjove $LOGNAME */
3158 assert((n_psonce
& n_PSO_INTERACTIVE
) &&
3159 !(n_pstate
& n_PS_ROBOT
));
3165 rv
= (ssize_t
)mbrtowc(&wc
, cbuf
, PTR2SIZE(cbufp
- cbuf
), &ps
[0]);
3167 /* Any error during buffer mode can only result in a hard
3168 * reset; Otherwise, if it's a hard error, or if too many
3169 * redundant shift sequences overflow our buffer: perform
3171 if((flags
& a_BUFMODE
) || rv
== -1 ||
3172 sizeof cbuf_base
== PTR2SIZE(cbufp
- cbuf
)){
3173 a_tty_fun(tlp
, a_TTY_BIND_FUN_FULLRESET
, &len
);
3176 /* Otherwise, due to the way we deal with the buffer, we need
3177 * to restore the mbstate_t from before this conversion */
3185 /* Normal read(2)ing is subject to detection of key-bindings */
3186 # ifdef HAVE_KEY_BINDINGS
3187 if(!(flags
& a_BUFMODE
)){
3188 /* Check for special bypass functions before we try to embed
3189 * this character into the tree */
3190 if(n_uasciichar(wc
)){
3194 for(c
= (char)wc
, cp
= &(*tlp
->tl_bind_shcut_prompt_char
)[0];
3197 wc
= a_tty_vinuni(tlp
);
3202 tlp
->tl_vi_flags
|= a_TTY_VF_BELL
;
3206 if(n_uasciichar(wc
))
3207 flags
|= a_MAYBEFUN
;
3209 flags
&= ~a_MAYBEFUN
;
3211 /* Search for this character in the bind tree */
3212 tbtp
= (isp
!= NULL
) ? isp
->tbtp
->tbt_childs
3213 : (*tlp
->tl_bind_tree_hmap
)[wc
% HSHSIZE
];
3214 for(; tbtp
!= NULL
; tbtp
= tbtp
->tbt_sibling
){
3215 if(tbtp
->tbt_char
== wc
){
3218 /* If this one cannot continue we're likely finished! */
3219 if(tbtp
->tbt_childs
== NULL
){
3220 assert(tbtp
->tbt_bind
!= NULL
);
3221 tbf
= tbtp
->tbt_bind
->tbc_flags
;
3225 /* This needs to read more characters */
3226 nisp
= n_autorec_alloc(sizeof *nisp
);
3227 if((nisp
->last
= isp
) == NULL
)
3234 flags
&= ~a_WAS_HERE
;
3241 /* Was there a binding active, but couldn't be continued? */
3243 /* A binding had a timeout, it didn't expire, but we saw
3244 * something non-expected. Something "atomic" broke.
3245 * Maybe there is a second path without a timeout, that
3246 * continues like we've seen it. I.e., it may just have been
3247 * the user, typing too fast. We definitely want to allow
3248 * bindings like \e,d etc. to succeed: users are so used to
3249 * them that a timeout cannot be the mechanism to catch up!
3250 * A single-layer implementation cannot "know" */
3251 if((tbtp
= isp
->tbtp
)->tbt_isseq
&& (isp
->last
== NULL
||
3252 !(xtbtp
= isp
->last
->tbtp
)->tbt_isseq
||
3253 xtbtp
->tbt_isseq_trail
)){
3254 for(xtbtp
= (tbtp
= isp
->tbtp
);
3255 (xtbtp
= xtbtp
->tbt_sibling
) != NULL
;)
3256 if(xtbtp
->tbt_char
== tbtp
->tbt_char
){
3257 assert(!xtbtp
->tbt_isseq
);
3262 tlp
->tl_bind_takeover
= wc
;
3267 /* Check for CANCEL shortcut now */
3268 if(flags
& a_MAYBEFUN
){
3272 for(c
= (char)wc
, cp
= &(*tlp
->tl_bind_shcut_cancel
)[0];
3275 tbf
= a_TTY_BIND_FUN_INTERNAL
|a_TTY_BIND_FUN_CANCEL
;
3280 /* So: maybe the current sequence can be terminated here? */
3281 if((tbtp
= isp
->tbtp
)->tbt_bind
!= NULL
){
3283 tbf
= tbtp
->tbt_bind
->tbc_flags
;
3285 if(tbf
& a_TTY_BIND_FUN_INTERNAL
){
3286 switch(a_tty_fun(tlp
, tbf
, &len
)){
3287 case a_TTY_FUN_STATUS_OK
:
3289 case a_TTY_FUN_STATUS_COMMIT
:
3291 case a_TTY_FUN_STATUS_RESTART
:
3293 case a_TTY_FUN_STATUS_END
:
3298 }else if(tbtp
->tbt_bind
->tbc_flags
& a_TTY_BIND_NOCOMMIT
){
3299 struct a_tty_bind_ctx
*tbcp
;
3301 tbcp
= tbtp
->tbt_bind
;
3302 memcpy(tlp
->tl_defc
.s
= n_autorec_alloc(
3303 (tlp
->tl_defc
.l
= len
= tbcp
->tbc_exp_len
) +1),
3304 tbcp
->tbc_exp
, tbcp
->tbc_exp_len
+1);
3307 cbufp
= tbtp
->tbt_bind
->tbc_exp
;
3313 /* Otherwise take over all chars "as is" */
3315 for(; isp_head
!= NULL
; isp_head
= isp_head
->next
)
3316 if(a_tty_kother(tlp
, isp_head
->tbtp
->tbt_char
)){
3319 /* And the current one too */
3322 # endif /* HAVE_KEY_BINDINGS */
3324 if((flags
& a_BUFMODE
) && (len
-= (size_t)rv
) == 0){
3325 /* Buffer mode completed */
3326 tlp
->tl_defc
.s
= NULL
;
3328 flags
&= ~a_BUFMODE
;
3333 # ifndef HAVE_KEY_BINDINGS
3334 /* Don't interpret control bytes during buffer mode.
3335 * Otherwise, if it's a control byte check whether it is a MLE
3336 * function. Remarks: initially a complete duplicate to be able to
3337 * switch(), later converted to simply iterate over (an #ifdef'd
3338 * subset of) the MLE base_tuple table in order to have "a SPOF" */
3339 if(cbuf
== cbuf_base
&& n_uasciichar(wc
) && cntrlchar((char)wc
)){
3340 struct a_tty_bind_builtin_tuple
const *tbbtp
, *tbbtp_max
;
3343 c
= (char)wc
^ 0x40;
3344 tbbtp
= a_tty_bind_base_tuples
;
3345 tbbtp_max
= &tbbtp
[n_NELEM(a_tty_bind_base_tuples
)];
3347 for(; tbbtp
< tbbtp_max
; ++tbbtp
){
3348 /* Assert default_tuple table is properly subset'ed */
3349 assert(tbbtp
->tbdt_iskey
);
3350 if(tbbtp
->tbbt_ckey
== c
){
3351 if(tbbtp
->tbbt_exp
[0] == '\0'){
3352 tbf
= a_TTY_BIND_FUN_EXPAND((ui8_t
)tbbtp
->tbbt_exp
[1]);
3353 switch(a_tty_fun(tlp
, tbf
, &len
)){
3354 case a_TTY_FUN_STATUS_OK
:
3356 case a_TTY_FUN_STATUS_COMMIT
:
3358 case a_TTY_FUN_STATUS_RESTART
:
3360 case a_TTY_FUN_STATUS_END
:
3366 cbufp
= n_UNCONST(tbbtp
->tbbt_exp
);
3372 &a_tty_bind_base_tuples
[n_NELEM(a_tty_bind_base_tuples
)]){
3373 tbbtp
= a_tty_bind_default_tuples
;
3374 tbbtp_max
= &tbbtp
[n_NELEM(a_tty_bind_default_tuples
)];
3378 # endif /* !HAVE_KEY_BINDINGS */
3380 # ifdef HAVE_KEY_BINDINGS
3383 if(a_tty_kother(tlp
, wc
)){
3384 /* Don't clear the history during buffer mode.. */
3385 # ifdef HAVE_HISTORY
3386 if(!(flags
& a_BUFMODE
) && cbuf
== cbuf_base
)
3387 tlp
->tl_hist
= NULL
;
3393 /* We have a completed input line, convert the struct cell data to its
3394 * plain character equivalent */
3396 rv
= a_tty_cell2dat(tlp
);
3398 putc('\n', n_tty_fp
);
3406 hold_all_sigs(); /* XXX v15 drop */
3407 i
= a_tty_cell2dat(tlp
);
3408 n_go_input_inject(n_GO_INPUT_INJECT_NONE
, tlp
->tl_line
.cbuf
, i
);
3409 i
= strlen(cbufp
) +1;
3410 if(i
>= *tlp
->tl_x_bufsize
){
3411 *tlp
->tl_x_buf
= (n_realloc
)(*tlp
->tl_x_buf
, i n_MEMORY_DEBUG_ARGSCALL
);
3412 *tlp
->tl_x_bufsize
= i
;
3414 memcpy(*tlp
->tl_x_buf
, cbufp
, i
);
3415 rele_all_sigs(); /* XXX v15 drop */
3416 if(histok_or_null
!= NULL
)
3417 *histok_or_null
= FAL0
;
3423 # ifdef HAVE_KEY_BINDINGS
3424 static enum n_go_input_flags
3425 a_tty_bind_ctx_find(char const *name
){
3426 enum n_go_input_flags rv
;
3427 struct a_tty_bind_ctx_map
const *tbcmp
;
3430 tbcmp
= a_tty_bind_ctx_maps
;
3431 do if(!asccasecmp(tbcmp
->tbcm_name
, name
)){
3432 rv
= tbcmp
->tbcm_ctx
;
3434 }while(PTRCMP(++tbcmp
, <,
3435 &a_tty_bind_ctx_maps
[n_NELEM(a_tty_bind_ctx_maps
)]));
3437 rv
= (enum n_go_input_flags
)-1;
3444 a_tty_bind_create(struct a_tty_bind_parse_ctx
*tbpcp
, bool_t replace
){
3445 struct a_tty_bind_ctx
*tbcp
;
3451 if(!a_tty_bind_parse(TRU1
, tbpcp
))
3454 /* Since we use a single buffer for it all, need to replace as such */
3455 if(tbpcp
->tbpc_tbcp
!= NULL
){
3458 a_tty_bind_del(tbpcp
);
3459 }else if(a_tty
.tg_bind_cnt
== UI32_MAX
){
3460 n_err(_("`bind': maximum number of bindings already established\n"));
3467 tbcp
= n_alloc(n_VSTRUCT_SIZEOF(struct a_tty_bind_ctx
, tbc__buf
) +
3468 tbpcp
->tbpc_seq_len
+ tbpcp
->tbpc_exp
.l
+
3469 n_MAX(sizeof(si32_t
), sizeof(wc_t
)) + tbpcp
->tbpc_cnv_len
+3);
3470 if(tbpcp
->tbpc_ltbcp
!= NULL
){
3471 tbcp
->tbc_next
= tbpcp
->tbpc_ltbcp
->tbc_next
;
3472 tbpcp
->tbpc_ltbcp
->tbc_next
= tbcp
;
3474 enum n_go_input_flags gif
;
3476 gif
= tbpcp
->tbpc_flags
& n__GO_INPUT_CTX_MASK
;
3477 tbcp
->tbc_next
= a_tty
.tg_bind
[gif
];
3478 a_tty
.tg_bind
[gif
] = tbcp
;
3480 memcpy(tbcp
->tbc_seq
= &tbcp
->tbc__buf
[0],
3481 tbpcp
->tbpc_seq
, i
= (tbcp
->tbc_seq_len
= tbpcp
->tbpc_seq_len
) +1);
3482 memcpy(tbcp
->tbc_exp
= &tbcp
->tbc__buf
[i
],
3483 tbpcp
->tbpc_exp
.s
, j
= (tbcp
->tbc_exp_len
= tbpcp
->tbpc_exp
.l
) +1);
3485 i
= (i
+ tbpcp
->tbpc_cnv_align_mask
) & ~tbpcp
->tbpc_cnv_align_mask
;
3486 memcpy(tbcp
->tbc_cnv
= &tbcp
->tbc__buf
[i
],
3487 tbpcp
->tbpc_cnv
, (tbcp
->tbc_cnv_len
= tbpcp
->tbpc_cnv_len
) +1);
3488 tbcp
->tbc_flags
= tbpcp
->tbpc_flags
;
3491 /* Directly resolve any termcap(5) symbol if we are already setup */
3492 if((n_psonce
& n_PSO_STARTED
) &&
3493 (tbcp
->tbc_flags
& (a_TTY_BIND_RESOLVE
| a_TTY_BIND_DEFUNCT
)) ==
3495 a_tty_bind_resolve(tbcp
);
3497 ++a_tty
.tg_bind_cnt
;
3498 /* If this binding is usable invalidate the key input lookup trees */
3499 if(!(tbcp
->tbc_flags
& a_TTY_BIND_DEFUNCT
))
3500 a_tty
.tg_bind_isdirty
= TRU1
;
3508 a_tty_bind_parse(bool_t isbindcmd
, struct a_tty_bind_parse_ctx
*tbpcp
){
3509 enum{a_TRUE_RV
= a_TTY__BIND_LAST
<<1};
3511 struct n_visual_info_ctx vic
;
3512 struct str shin_save
, shin
;
3513 struct n_string shou
, *shoup
;
3520 ui32_t cnv_len
; /* High bit set if a termap to be resolved */
3521 ui32_t calc_cnv_len
; /* Ditto, but aligned etc. */
3522 ui8_t kse__dummy
[4];
3526 n_LCTA(UICMP(64, a_TRUE_RV
, <, UI32_MAX
),
3527 "Flag bits excess storage datatype");
3529 f
= n_GO_INPUT_NONE
;
3530 shoup
= n_string_creat_auto(&shou
);
3533 /* Parse the key-sequence */
3534 for(shin
.s
= n_UNCONST(tbpcp
->tbpc_in_seq
), shin
.l
= UIZ_MAX
;;){
3536 enum n_shexp_state shs
;
3539 shs
= n_shexp_parse_token((n_SHEXP_PARSE_TRUNC
|
3540 n_SHEXP_PARSE_TRIM_SPACE
| n_SHEXP_PARSE_IGNORE_EMPTY
|
3541 n_SHEXP_PARSE_IFS_IS_COMMA
), shoup
, &shin
, NULL
);
3542 if(shs
& n_SHEXP_STATE_ERR_UNICODE
){
3543 f
|= a_TTY_BIND_DEFUNCT
;
3544 if(isbindcmd
&& (n_poption
& n_PO_D_V
))
3545 n_err(_("`%s': \\uNICODE not available in locale: %s\n"),
3546 tbpcp
->tbpc_cmd
, tbpcp
->tbpc_in_seq
);
3548 if((shs
& n_SHEXP_STATE_ERR_MASK
) & ~n_SHEXP_STATE_ERR_UNICODE
){
3549 n_err(_("`%s': failed to parse key-sequence: %s\n"),
3550 tbpcp
->tbpc_cmd
, tbpcp
->tbpc_in_seq
);
3553 if((shs
& (n_SHEXP_STATE_OUTPUT
| n_SHEXP_STATE_STOP
)) ==
3557 ep
= n_autorec_alloc(sizeof *ep
);
3564 if(!(shs
& n_SHEXP_STATE_ERR_UNICODE
)){
3565 i
= strlen(ep
->seq_dat
= n_shexp_quote_cp(n_string_cp(shoup
), TRU1
));
3566 if(i
>= SI32_MAX
- 1)
3568 ep
->seq_len
= (ui32_t
)i
;
3570 /* Otherwise use the original buffer, _we_ can only quote it the wrong
3571 * way (e.g., an initial $'\u3a' becomes '\u3a'), _then_ */
3572 if((i
= shin_save
.l
- shin
.l
) >= SI32_MAX
- 1)
3574 ep
->seq_len
= (ui32_t
)i
;
3575 ep
->seq_dat
= savestrbuf(shin_save
.s
, i
);
3578 memset(&vic
, 0, sizeof vic
);
3579 vic
.vic_inlen
= shoup
->s_len
;
3580 vic
.vic_indat
= shoup
->s_dat
;
3581 if(!n_visual_info(&vic
,
3582 n_VISUAL_INFO_WOUT_CREATE
| n_VISUAL_INFO_WOUT_SALLOC
)){
3583 n_err(_("`%s': key-sequence seems to contain invalid "
3584 "characters: %s: %s\n"),
3585 tbpcp
->tbpc_cmd
, n_string_cp(shoup
), tbpcp
->tbpc_in_seq
);
3586 f
|= a_TTY_BIND_DEFUNCT
;
3588 }else if(vic
.vic_woulen
== 0 ||
3589 vic
.vic_woulen
>= (SI32_MAX
- 2) / sizeof(wc_t
)){
3591 n_err(_("`%s': length of key-sequence unsupported: %s: %s\n"),
3592 tbpcp
->tbpc_cmd
, n_string_cp(shoup
), tbpcp
->tbpc_in_seq
);
3593 f
|= a_TTY_BIND_DEFUNCT
;
3596 ep
->cnv_dat
= vic
.vic_woudat
;
3597 ep
->cnv_len
= (ui32_t
)vic
.vic_woulen
;
3599 /* A termcap(5)/terminfo(5) identifier? */
3600 if(ep
->cnv_len
> 1 && ep
->cnv_dat
[0] == ':'){
3601 i
= --ep
->cnv_len
, ++ep
->cnv_dat
;
3602 # if 0 /* ndef HAVE_TERMCAP xxx User can, via *termcap*! */
3603 if(n_poption
& n_PO_D_V
)
3604 n_err(_("`%s': no termcap(5)/terminfo(5) support: %s: %s\n"),
3605 tbpcp
->tbpc_cmd
, ep
->seq_dat
, tbpcp
->tbpc_in_seq
);
3606 f
|= a_TTY_BIND_DEFUNCT
;
3608 if(i
> a_TTY_BIND_CAPNAME_MAX
){
3609 n_err(_("`%s': termcap(5)/terminfo(5) name too long: %s: %s\n"),
3610 tbpcp
->tbpc_cmd
, ep
->seq_dat
, tbpcp
->tbpc_in_seq
);
3611 f
|= a_TTY_BIND_DEFUNCT
;
3614 /* (We store it as char[]) */
3615 if((ui32_t
)ep
->cnv_dat
[--i
] & ~0x7Fu
){
3616 n_err(_("`%s': invalid termcap(5)/terminfo(5) name content: "
3618 tbpcp
->tbpc_cmd
, ep
->seq_dat
, tbpcp
->tbpc_in_seq
);
3619 f
|= a_TTY_BIND_DEFUNCT
;
3622 ep
->cnv_len
|= SI32_MIN
; /* Needs resolve */
3623 f
|= a_TTY_BIND_RESOLVE
;
3626 if(shs
& n_SHEXP_STATE_STOP
)
3632 n_err(_("`%s': effectively empty key-sequence: %s\n"),
3633 tbpcp
->tbpc_cmd
, tbpcp
->tbpc_in_seq
);
3637 if(isbindcmd
) /* (Or always, just "1st time init") */
3638 tbpcp
->tbpc_cnv_align_mask
= n_MAX(sizeof(si32_t
), sizeof(wc_t
)) - 1;
3641 struct a_tty_bind_ctx
*ltbcp
, *tbcp
;
3642 char *cpbase
, *cp
, *cnv
;
3645 /* Unite the parsed sequence(s) into single string representations */
3646 for(sl
= cl
= 0, tail
= head
; tail
!= NULL
; tail
= tail
->next
){
3647 sl
+= tail
->seq_len
+ 1;
3652 /* Preserve room for terminal capabilities to be resolved.
3653 * Above we have ensured the buffer will fit in these calculations */
3654 if((i
= tail
->cnv_len
) & SI32_MIN
){
3656 * struct{si32_t buf_len_iscap; si32_t cap_len; wc_t name[]+NUL;}
3658 * struct{si32_t buf_len_iscap; si32_t cap_len; char buf[]+NUL;} */
3659 n_LCTAV(n_ISPOW2(a_TTY_BIND_CAPEXP_ROUNDUP
));
3660 n_LCTA(a_TTY_BIND_CAPEXP_ROUNDUP
>= sizeof(wc_t
),
3661 "Aligning on this constant does not properly align wc_t");
3664 i
+= sizeof(si32_t
);
3665 if(i
< a_TTY_BIND_CAPEXP_ROUNDUP
)
3666 i
= (i
+ (a_TTY_BIND_CAPEXP_ROUNDUP
- 1)) &
3667 ~(a_TTY_BIND_CAPEXP_ROUNDUP
- 1);
3669 /* struct{si32_t buf_len_iscap; wc_t buf[]+NUL;} */
3671 i
+= sizeof(si32_t
) + sizeof(wc_t
); /* (buf_len_iscap, NUL) */
3673 if(tail
->cnv_len
& SI32_MIN
){
3674 tail
->cnv_len
&= SI32_MAX
;
3677 tail
->calc_cnv_len
= (ui32_t
)i
;
3681 tbpcp
->tbpc_seq_len
= sl
;
3682 tbpcp
->tbpc_cnv_len
= cl
;
3686 j
= i
= sl
+ 1; /* Room for comma separator */
3688 i
= (i
+ tbpcp
->tbpc_cnv_align_mask
) & ~tbpcp
->tbpc_cnv_align_mask
;
3692 tbpcp
->tbpc_seq
= cp
= cpbase
= n_autorec_alloc(i
);
3693 tbpcp
->tbpc_cnv
= cnv
= &cpbase
[j
];
3696 for(tail
= head
; tail
!= NULL
; tail
= tail
->next
){
3697 memcpy(cp
, tail
->seq_dat
, tail
->seq_len
);
3698 cp
+= tail
->seq_len
;
3702 char * const save_cnv
= cnv
;
3704 n_UNALIGN(si32_t
*,cnv
)[0] = (si32_t
)(i
= tail
->calc_cnv_len
);
3705 cnv
+= sizeof(si32_t
);
3708 * struct{si32_t buf_len_iscap; si32_t cap_len; wc_t name[];}
3710 * struct{si32_t buf_len_iscap; si32_t cap_len; char buf[];} */
3711 n_UNALIGN(si32_t
*,cnv
)[0] = tail
->cnv_len
;
3712 cnv
+= sizeof(si32_t
);
3714 i
= tail
->cnv_len
* sizeof(wc_t
);
3715 memcpy(cnv
, tail
->cnv_dat
, i
);
3717 *n_UNALIGN(wc_t
*,cnv
) = '\0';
3719 cnv
= save_cnv
+ (tail
->calc_cnv_len
& SI32_MAX
);
3724 /* Search for a yet existing identical mapping */
3726 enum n_go_input_flags gif
;
3728 gif
= tbpcp
->tbpc_flags
& n__GO_INPUT_CTX_MASK
;
3730 for(ltbcp
= NULL
, tbcp
= a_tty
.tg_bind
[gif
]; tbcp
!= NULL
;
3731 ltbcp
= tbcp
, tbcp
= tbcp
->tbc_next
)
3732 if(tbcp
->tbc_seq_len
== sl
&& !memcmp(tbcp
->tbc_seq
, cpbase
, sl
)){
3733 tbpcp
->tbpc_tbcp
= tbcp
;
3737 tbpcp
->tbpc_ltbcp
= ltbcp
;
3738 tbpcp
->tbpc_flags
|= (f
& a_TTY__BIND_MASK
);
3741 /* Create single string expansion if so desired */
3745 exp
= tbpcp
->tbpc_exp
.s
;
3747 i
= tbpcp
->tbpc_exp
.l
;
3748 if(i
> 0 && exp
[i
- 1] == '@'){
3750 if(!blankspacechar(exp
[i
- 1]))
3756 exp
[tbpcp
->tbpc_exp
.l
= i
] = '\0';
3757 tbpcp
->tbpc_flags
|= a_TTY_BIND_NOCOMMIT
;
3760 /* Reverse solidus cannot be placed last in expansion to avoid (at the
3761 * time of this writing) possible problems with newline escaping.
3762 * Don't care about (un)even number thereof */
3763 if(i
> 0 && exp
[i
- 1] == '\\'){
3764 n_err(_("`%s': reverse solidus cannot be last in expansion: %s\n"),
3765 tbpcp
->tbpc_cmd
, tbpcp
->tbpc_in_seq
);
3769 /* It may map to an internal MLE command! */
3770 for(i
= 0; i
< n_NELEM(a_tty_bind_fun_names
); ++i
)
3771 if(!asccasecmp(exp
, a_tty_bind_fun_names
[i
])){
3772 tbpcp
->tbpc_flags
|= a_TTY_BIND_FUN_EXPAND(i
) |
3773 a_TTY_BIND_FUN_INTERNAL
|
3774 (head
->next
== NULL
? a_TTY_BIND_MLE1CNTRL
: 0);
3775 if((n_poption
& n_PO_D_V
) &&
3776 (tbpcp
->tbpc_flags
& a_TTY_BIND_NOCOMMIT
))
3777 n_err(_("`%s': MLE commands can't be made editable via @: %s\n"),
3778 tbpcp
->tbpc_cmd
, exp
);
3779 tbpcp
->tbpc_flags
&= ~a_TTY_BIND_NOCOMMIT
;
3784 f
|= a_TRUE_RV
; /* TODO because we only now true and false; DEFUNCT.. */
3786 n_string_gut(shoup
);
3788 return (f
& a_TRUE_RV
) != 0;
3792 a_tty_bind_resolve(struct a_tty_bind_ctx
*tbcp
){
3793 char capname
[a_TTY_BIND_CAPNAME_MAX
+1];
3794 struct n_termcap_value tv
;
3796 bool_t isfirst
; /* TODO For now: first char must be control! */
3800 n_UNINIT(next
, NULL
);
3801 for(cp
= tbcp
->tbc_cnv
, isfirst
= TRU1
, len
= tbcp
->tbc_cnv_len
;
3802 len
> 0; isfirst
= FAL0
, cp
= next
){
3806 i
= n_UNALIGN(si32_t
*,cp
)[0];
3813 /* struct{si32_t buf_len_iscap; si32_t cap_len; wc_t name[];} */
3814 cp
+= sizeof(si32_t
);
3815 i
= n_UNALIGN(si32_t
*,cp
)[0];
3816 cp
+= sizeof(si32_t
);
3817 for(j
= 0; j
< i
; ++j
)
3818 capname
[j
] = n_UNALIGN(wc_t
*,cp
)[j
];
3822 /* Use generic lookup mechanism if not a known query */
3826 tq
= n_termcap_query_for_name(capname
, n_TERMCAP_CAPTYPE_STRING
);
3828 tv
.tv_data
.tvd_string
= capname
;
3829 tq
= n__TERMCAP_QUERY_MAX1
;
3832 if(tq
< 0 || !n_termcap_query(tq
, &tv
)){
3833 if(n_poption
& n_PO_D_V
)
3834 n_err(_("`bind': unknown or unsupported capability: %s: %s\n"),
3835 capname
, tbcp
->tbc_seq
);
3836 tbcp
->tbc_flags
|= a_TTY_BIND_DEFUNCT
;
3841 /* struct{si32_t buf_len_iscap; si32_t cap_len; char buf[]+NUL;} */
3845 i
= strlen(tv
.tv_data
.tvd_string
);
3846 if(/*i > SI32_MAX ||*/ i
>= PTR2SIZE(next
- cp
)){
3847 if(n_poption
& n_PO_D_V
)
3848 n_err(_("`bind': capability expansion too long: %s: %s\n"),
3849 capname
, tbcp
->tbc_seq
);
3850 tbcp
->tbc_flags
|= a_TTY_BIND_DEFUNCT
;
3853 if(n_poption
& n_PO_D_V
)
3854 n_err(_("`bind': empty capability expansion: %s: %s\n"),
3855 capname
, tbcp
->tbc_seq
);
3856 tbcp
->tbc_flags
|= a_TTY_BIND_DEFUNCT
;
3858 }else if(isfirst
&& !cntrlchar(*tv
.tv_data
.tvd_string
)){
3859 if(n_poption
& n_PO_D_V
)
3860 n_err(_("`bind': capability expansion does not start with "
3861 "control: %s: %s\n"), capname
, tbcp
->tbc_seq
);
3862 tbcp
->tbc_flags
|= a_TTY_BIND_DEFUNCT
;
3865 n_UNALIGN(si32_t
*,cp
)[-1] = (si32_t
)i
;
3866 memcpy(cp
, tv
.tv_data
.tvd_string
, i
);
3874 a_tty_bind_del(struct a_tty_bind_parse_ctx
*tbpcp
){
3875 struct a_tty_bind_ctx
*ltbcp
, *tbcp
;
3878 tbcp
= tbpcp
->tbpc_tbcp
;
3880 if((ltbcp
= tbpcp
->tbpc_ltbcp
) != NULL
)
3881 ltbcp
->tbc_next
= tbcp
->tbc_next
;
3883 a_tty
.tg_bind
[tbpcp
->tbpc_flags
& n__GO_INPUT_CTX_MASK
] = tbcp
->tbc_next
;
3886 --a_tty
.tg_bind_cnt
;
3887 a_tty
.tg_bind_isdirty
= TRU1
;
3892 a_tty_bind_tree_build(void){
3896 for(i
= 0; i
< n__GO_INPUT_CTX_MAX1
; ++i
){
3897 struct a_tty_bind_ctx
*tbcp
;
3898 n_LCTAV(n_GO_INPUT_CTX_BASE
== 0);
3900 /* Somewhat wasteful, but easier to handle: simply clone the entire
3901 * primary key onto the secondary one, then only modify it */
3902 for(tbcp
= a_tty
.tg_bind
[n_GO_INPUT_CTX_BASE
]; tbcp
!= NULL
;
3903 tbcp
= tbcp
->tbc_next
)
3904 if(!(tbcp
->tbc_flags
& a_TTY_BIND_DEFUNCT
))
3905 a_tty__bind_tree_add(n_GO_INPUT_CTX_BASE
, &a_tty
.tg_bind_tree
[i
][0],
3908 if(i
!= n_GO_INPUT_CTX_BASE
)
3909 for(tbcp
= a_tty
.tg_bind
[i
]; tbcp
!= NULL
; tbcp
= tbcp
->tbc_next
)
3910 if(!(tbcp
->tbc_flags
& a_TTY_BIND_DEFUNCT
))
3911 a_tty__bind_tree_add(i
, &a_tty
.tg_bind_tree
[i
][0], tbcp
);
3914 a_tty
.tg_bind_isbuild
= TRU1
;
3919 a_tty_bind_tree_teardown(void){
3923 memset(&a_tty
.tg_bind_shcut_cancel
[0], 0,
3924 sizeof(a_tty
.tg_bind_shcut_cancel
));
3925 memset(&a_tty
.tg_bind_shcut_prompt_char
[0], 0,
3926 sizeof(a_tty
.tg_bind_shcut_prompt_char
));
3928 for(i
= 0; i
< n__GO_INPUT_CTX_MAX1
; ++i
)
3929 for(j
= 0; j
< HSHSIZE
; ++j
)
3930 a_tty__bind_tree_free(a_tty
.tg_bind_tree
[i
][j
]);
3931 memset(&a_tty
.tg_bind_tree
[0], 0, sizeof(a_tty
.tg_bind_tree
));
3933 a_tty
.tg_bind_isdirty
= a_tty
.tg_bind_isbuild
= FAL0
;
3938 a_tty__bind_tree_add(ui32_t hmap_idx
, struct a_tty_bind_tree
*store
[HSHSIZE
],
3939 struct a_tty_bind_ctx
*tbcp
){
3942 struct a_tty_bind_tree
*ntbtp
;
3948 for(cnvdat
= tbcp
->tbc_cnv
, cnvlen
= tbcp
->tbc_cnv_len
; cnvlen
> 0;){
3949 union {wchar_t const *wp
; char const *cp
;} u
;
3952 /* {si32_t buf_len_iscap;} */
3953 entlen
= *n_UNALIGN(si32_t
const*,cnvdat
);
3955 if(entlen
& SI32_MIN
){
3956 /* struct{si32_t buf_len_iscap; si32_t cap_len; char buf[]+NUL;}
3957 * Note that empty capabilities result in DEFUNCT */
3958 for(u
.cp
= (char const*)&n_UNALIGN(si32_t
const*,cnvdat
)[2];
3959 *u
.cp
!= '\0'; ++u
.cp
)
3960 ntbtp
= a_tty__bind_tree_add_wc(store
, ntbtp
, *u
.cp
, TRU1
);
3961 assert(ntbtp
!= NULL
);
3962 ntbtp
->tbt_isseq_trail
= TRU1
;
3965 /* struct{si32_t buf_len_iscap; wc_t buf[]+NUL;} */
3968 u
.wp
= (wchar_t const*)&n_UNALIGN(si32_t
const*,cnvdat
)[1];
3970 /* May be a special shortcut function? */
3971 if(ntbtp
== NULL
&& (tbcp
->tbc_flags
& a_TTY_BIND_MLE1CNTRL
)){
3975 ctx
= tbcp
->tbc_flags
& n__GO_INPUT_CTX_MASK
;
3976 fun
= tbcp
->tbc_flags
& a_TTY__BIND_FUN_MASK
;
3978 if(fun
== a_TTY_BIND_FUN_CANCEL
){
3979 for(cp
= &a_tty
.tg_bind_shcut_cancel
[ctx
][0];
3980 PTRCMP(cp
, <, &a_tty
.tg_bind_shcut_cancel
[ctx
]
3981 [n_NELEM(a_tty
.tg_bind_shcut_cancel
[ctx
]) - 1]); ++cp
)
3986 }else if(fun
== a_TTY_BIND_FUN_PROMPT_CHAR
){
3987 for(cp
= &a_tty
.tg_bind_shcut_prompt_char
[ctx
][0];
3988 PTRCMP(cp
, <, &a_tty
.tg_bind_shcut_prompt_char
[ctx
]
3989 [n_NELEM(a_tty
.tg_bind_shcut_prompt_char
[ctx
]) - 1]);
3998 isseq
= (u
.wp
[1] != '\0');
3999 for(; *u
.wp
!= '\0'; ++u
.wp
)
4000 ntbtp
= a_tty__bind_tree_add_wc(store
, ntbtp
, *u
.wp
, isseq
);
4002 assert(ntbtp
!= NULL
);
4003 ntbtp
->tbt_isseq_trail
= TRU1
;
4011 /* Should have been rendered defunctional at first instead */
4012 assert(ntbtp
!= NULL
);
4013 ntbtp
->tbt_bind
= tbcp
;
4017 static struct a_tty_bind_tree
*
4018 a_tty__bind_tree_add_wc(struct a_tty_bind_tree
**treep
,
4019 struct a_tty_bind_tree
*parentp
, wchar_t wc
, bool_t isseq
){
4020 struct a_tty_bind_tree
*tbtp
, *xtbtp
;
4023 if(parentp
== NULL
){
4024 treep
+= wc
% HSHSIZE
;
4026 /* Having no parent also means that the tree slot is possibly empty */
4027 for(tbtp
= *treep
; tbtp
!= NULL
;
4028 parentp
= tbtp
, tbtp
= tbtp
->tbt_sibling
){
4029 if(tbtp
->tbt_char
!= wc
)
4031 if(tbtp
->tbt_isseq
== isseq
)
4033 /* isseq MUST be linked before !isseq, so record this "parent"
4034 * sibling, but continue searching for now.
4035 * Otherwise it is impossible that we'll find what we look for */
4038 while((tbtp
= tbtp
->tbt_sibling
) != NULL
)
4039 assert(tbtp
->tbt_char
!= wc
);
4045 tbtp
= n_alloc(sizeof *tbtp
);
4046 memset(tbtp
, 0, sizeof *tbtp
);
4047 tbtp
->tbt_char
= wc
;
4048 tbtp
->tbt_isseq
= isseq
;
4050 if(parentp
== NULL
){
4051 tbtp
->tbt_sibling
= *treep
;
4054 tbtp
->tbt_sibling
= parentp
->tbt_sibling
;
4055 parentp
->tbt_sibling
= tbtp
;
4058 if((tbtp
= *(treep
= &parentp
->tbt_childs
)) != NULL
){
4059 for(;; tbtp
= xtbtp
){
4060 if(tbtp
->tbt_char
== wc
){
4061 if(tbtp
->tbt_isseq
== isseq
)
4063 /* isseq MUST be linked before, so it is impossible that we'll
4064 * find what we look for */
4067 while((tbtp
= tbtp
->tbt_sibling
) != NULL
)
4068 assert(tbtp
->tbt_char
!= wc
);
4075 if((xtbtp
= tbtp
->tbt_sibling
) == NULL
){
4076 treep
= &tbtp
->tbt_sibling
;
4082 xtbtp
= n_alloc(sizeof *xtbtp
);
4083 memset(xtbtp
, 0, sizeof *xtbtp
);
4084 xtbtp
->tbt_parent
= parentp
;
4085 xtbtp
->tbt_char
= wc
;
4086 xtbtp
->tbt_isseq
= isseq
;
4096 a_tty__bind_tree_free(struct a_tty_bind_tree
*tbtp
){
4098 while(tbtp
!= NULL
){
4099 struct a_tty_bind_tree
*tmp
;
4101 if((tmp
= tbtp
->tbt_childs
) != NULL
)
4102 a_tty__bind_tree_free(tmp
);
4104 tmp
= tbtp
->tbt_sibling
;
4110 # endif /* HAVE_KEY_BINDINGS */
4116 if(ok_blook(line_editor_disable
))
4119 /* Load the history file */
4120 # ifdef HAVE_HISTORY
4124 /* Force immediate resolve for anything which follows */
4125 n_psonce
|= n_PSO_LINE_EDITOR_INIT
;
4127 # ifdef HAVE_KEY_BINDINGS
4128 /* `bind's (and `unbind's) done from within resource files couldn't be
4129 * performed for real since our termcap driver wasn't yet loaded, and we
4130 * can't perform automatic init since the user may have disallowed so */
4131 /* C99 */{ /* TODO outsource into own file */
4132 struct a_tty_bind_ctx
*tbcp
;
4133 enum n_go_input_flags gif
;
4135 for(gif
= 0; gif
< n__GO_INPUT_CTX_MAX1
; ++gif
)
4136 for(tbcp
= a_tty
.tg_bind
[gif
]; tbcp
!= NULL
; tbcp
= tbcp
->tbc_next
)
4137 if((tbcp
->tbc_flags
& (a_TTY_BIND_RESOLVE
| a_TTY_BIND_DEFUNCT
)) ==
4139 a_tty_bind_resolve(tbcp
);
4142 /* And we want to (try to) install some default key bindings */
4143 if(!ok_blook(line_editor_no_defaults
)){
4145 struct a_tty_bind_parse_ctx tbpc
;
4146 struct a_tty_bind_builtin_tuple
const *tbbtp
, *tbbtp_max
;
4149 buf
[0] = '$', buf
[1] = '\'', buf
[2] = '\\', buf
[3] = 'c',
4150 buf
[5] = '\'', buf
[6] = '\0';
4152 tbbtp
= a_tty_bind_base_tuples
;
4153 tbbtp_max
= &tbbtp
[n_NELEM(a_tty_bind_base_tuples
)];
4154 flags
= n_GO_INPUT_CTX_BASE
;
4156 for(; tbbtp
< tbbtp_max
; ++tbbtp
){
4157 memset(&tbpc
, 0, sizeof tbpc
);
4158 tbpc
.tbpc_cmd
= "bind";
4159 if(tbbtp
->tbbt_iskey
){
4160 buf
[4] = tbbtp
->tbbt_ckey
;
4161 tbpc
.tbpc_in_seq
= buf
;
4163 tbpc
.tbpc_in_seq
= savecatsep(":", '\0',
4164 n_termcap_name_of_query(tbbtp
->tbbt_query
));
4165 tbpc
.tbpc_exp
.s
= n_UNCONST(tbbtp
->tbbt_exp
[0] == '\0'
4166 ? a_tty_bind_fun_names
[(ui8_t
)tbbtp
->tbbt_exp
[1]]
4168 tbpc
.tbpc_exp
.l
= strlen(tbpc
.tbpc_exp
.s
);
4169 tbpc
.tbpc_flags
= flags
;
4170 /* ..but don't want to overwrite any user settings */
4171 a_tty_bind_create(&tbpc
, FAL0
);
4173 if(flags
== n_GO_INPUT_CTX_BASE
){
4174 tbbtp
= a_tty_bind_default_tuples
;
4175 tbbtp_max
= &tbbtp
[n_NELEM(a_tty_bind_default_tuples
)];
4176 flags
= n_GO_INPUT_CTX_DEFAULT
;
4180 # endif /* HAVE_KEY_BINDINGS */
4187 n_tty_destroy(bool_t xit_fastpath
){
4190 if(!(n_psonce
& n_PSO_LINE_EDITOR_INIT
))
4193 /* Write the history file */
4194 # ifdef HAVE_HISTORY
4199 # if defined HAVE_KEY_BINDINGS && defined HAVE_DEBUG
4201 n_go_command(n_GO_INPUT_NONE
, "unbind * *");
4205 memset(&a_tty
, 0, sizeof a_tty
);
4207 n_psonce
&= ~n_PSO_LINE_EDITOR_INIT
;
4214 (n_tty_readline
)(enum n_go_input_flags gif
, char const *prompt
,
4215 char **linebuf
, size_t *linesize
, size_t n
, bool_t
*histok_or_null
4216 n_MEMORY_DEBUG_ARGS
){
4217 struct a_tty_line tl
;
4218 struct n_string xprompt
;
4225 assert(!ok_blook(line_editor_disable
));
4226 if(!(n_psonce
& n_PSO_LINE_EDITOR_INIT
))
4228 assert(n_psonce
& n_PSO_LINE_EDITOR_INIT
);
4231 n_colour_env_create(n_COLOUR_CTX_MLE
, n_tty_fp
, FAL0
);
4233 /* .tl_pos_buf is a hack */
4234 posbuf
= pos
= NULL
;
4236 if(n_COLOUR_IS_ACTIVE()){
4238 struct n_colour_pen
*ccp
;
4239 struct str
const *sp
;
4241 if((ccp
= n_colour_pen_create(n_COLOUR_ID_MLE_POSITION
, NULL
)) != NULL
&&
4242 (sp
= n_colour_pen_to_str(ccp
)) != NULL
){
4244 if((sp
= n_colour_reset_to_str()) != NULL
){
4249 posbuf
= n_autorec_alloc(l1
+ 4 + l2
+1);
4250 memcpy(posbuf
, ccol
, l1
);
4252 memcpy(&pos
[4], sp
->s
, ++l2
);
4258 posbuf
= pos
= n_autorec_alloc(4 +1);
4261 # endif /* HAVE_COLOUR */
4263 memset(&tl
, 0, sizeof tl
);
4264 tl
.tl_goinflags
= gif
;
4266 # ifdef HAVE_KEY_BINDINGS
4270 if((cp
= ok_vlook(bind_timeout
)) != NULL
){
4273 n_idec_ui64_cp(&uib
, cp
, 0, NULL
);
4276 /* Convert to tenths of a second, unfortunately */
4277 (uib
= (uib
+ 99) / 100) <= a_TTY_BIND_TIMEOUT_MAX
)
4278 tl
.tl_bind_timeout
= (ui8_t
)uib
;
4279 else if(n_poption
& n_PO_D_V
)
4280 n_err(_("Ignoring invalid *bind-timeout*: %s\n"), cp
);
4284 if(a_tty
.tg_bind_isdirty
)
4285 a_tty_bind_tree_teardown();
4286 if(a_tty
.tg_bind_cnt
> 0 && !a_tty
.tg_bind_isbuild
)
4287 a_tty_bind_tree_build();
4288 tl
.tl_bind_tree_hmap
= &a_tty
.tg_bind_tree
[gif
& n__GO_INPUT_CTX_MASK
];
4289 tl
.tl_bind_shcut_cancel
=
4290 &a_tty
.tg_bind_shcut_cancel
[gif
& n__GO_INPUT_CTX_MASK
];
4291 tl
.tl_bind_shcut_prompt_char
=
4292 &a_tty
.tg_bind_shcut_prompt_char
[gif
& n__GO_INPUT_CTX_MASK
];
4293 # endif /* HAVE_KEY_BINDINGS */
4296 tl
.tl_pos_buf
= posbuf
;
4300 if(!(gif
& n_GO_INPUT_PROMPT_NONE
)){
4301 n_string_creat_auto(&xprompt
);
4303 if((tl
.tl_prompt_width
= n_tty_create_prompt(&xprompt
, prompt
, gif
)
4305 tl
.tl_prompt
= n_string_cp_const(&xprompt
);
4306 tl
.tl_prompt_length
= (ui32_t
)xprompt
.s_len
;
4310 tl
.tl_line
.cbuf
= *linebuf
;
4312 tl
.tl_defc
.s
= savestrbuf(*linebuf
, n
);
4315 tl
.tl_x_buf
= linebuf
;
4316 tl
.tl_x_bufsize
= linesize
;
4318 a_tty
.tg_line
= &tl
;
4320 n_TERMCAP_RESUME(FAL0
);
4321 a_tty_term_mode(TRU1
);
4322 nn
= a_tty_readline(&tl
, n
, histok_or_null n_MEMORY_DEBUG_ARGSCALL
);
4323 n_COLOUR( n_colour_env_gut(); )
4324 a_tty_term_mode(FAL0
);
4325 n_TERMCAP_SUSPEND(FAL0
);
4327 a_tty
.tg_line
= NULL
;
4334 n_tty_addhist(char const *s
, enum n_go_input_flags gif
){
4339 # ifdef HAVE_HISTORY
4340 if(*s
!= '\0' && (n_psonce
& n_PSO_LINE_EDITOR_INIT
) &&
4341 a_tty
.tg_hist_size_max
> 0 &&
4342 (!(gif
& n_GO_INPUT_HIST_GABBY
) || ok_blook(history_gabby
)) &&
4343 !ok_blook(line_editor_disable
)){
4345 a_tty_hist_add(s
, gif
);
4352 # ifdef HAVE_HISTORY
4356 struct a_tty_hist
*thp
;
4360 if(ok_blook(line_editor_disable
)){
4361 n_err(_("history: *line-editor-disable* is set\n"));
4365 if(!(n_psonce
& n_PSO_LINE_EDITOR_INIT
)){
4367 assert(n_psonce
& n_PSO_LINE_EDITOR_INIT
);
4370 if(*(argv
= v
) == NULL
)
4374 if(!asccasecmp(*argv
, "show"))
4376 if(!asccasecmp(*argv
, "clear"))
4379 if(!asccasecmp(*argv
, "load")){
4380 if(!a_tty_hist_load())
4384 if(!asccasecmp(*argv
, "save")){
4385 if(!a_tty_hist_save())
4390 if((n_idec_siz_cp(&entry
, *argv
, 10, NULL
4391 ) & (n_IDEC_STATE_EMASK
| n_IDEC_STATE_CONSUMED
)
4392 ) == n_IDEC_STATE_CONSUMED
)
4395 n_err(_("Synopsis: history: %s\n"),
4396 /* Same string as in cmd-tab.h, still hoping...) */
4397 _("<show (default)|load|save|clear> or select history <NO>"));
4401 return (v
== NULL
? !STOP
: !OKAY
); /* xxx 1:bad 0:good -- do some */
4407 if(a_tty
.tg_hist
== NULL
)
4410 if((fp
= Ftmp(NULL
, "hist", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
){
4411 n_perr(_("tmpfile"), 0);
4416 no
= a_tty
.tg_hist_size
;
4419 for(thp
= a_tty
.tg_hist
; thp
!= NULL
;
4420 --no
, ++l
, thp
= thp
->th_older
){
4425 switch(thp
->th_flags
& a_TTY_HIST_CTX_MASK
){
4427 case a_TTY_HIST_CTX_DEFAULT
:
4430 case a_TTY_HIST_CTX_COMPOSE
:
4434 c2
= (thp
->th_flags
& a_TTY_HIST_GABBY
) ? '*' : ' ';
4436 if(n_poption
& n_PO_D_V
)
4437 fprintf(fp
, "# Length +%" PRIu32
", total %" PRIuZ
"\n",
4439 fprintf(fp
, "%c%c%4" PRIuZ
"\t%s\n", c1
, c2
, no
, thp
->th_dat
);
4442 page_or_print(fp
, l
);
4448 while((thp
= a_tty
.tg_hist
) != NULL
){
4449 a_tty
.tg_hist
= thp
->th_older
;
4452 a_tty
.tg_hist_tail
= NULL
;
4453 a_tty
.tg_hist_size
= 0;
4459 ep
= (entry
< 0) ? -entry
: entry
;
4461 if(ep
!= 0 && UICMP(z
, ep
, <=, a_tty
.tg_hist_size
)){
4465 ep
= (siz_t
)a_tty
.tg_hist_size
- ep
;
4466 for(thp
= a_tty
.tg_hist
;; thp
= thp
->th_older
){
4467 assert(thp
!= NULL
);
4469 n_go_input_inject((n_GO_INPUT_INJECT_COMMIT
|
4470 n_GO_INPUT_INJECT_HISTORY
), v
= thp
->th_dat
, thp
->th_len
);
4475 n_err(_("`history': no such entry: %" PRIdZ
"\n"), entry
);
4481 # endif /* HAVE_HISTORY */
4483 # ifdef HAVE_KEY_BINDINGS
4486 struct a_tty_bind_ctx
*tbcp
;
4487 enum n_go_input_flags gif
;
4489 union {char const *cp
; char *p
; char c
;} c
;
4490 struct n_cmd_arg_ctx
*cacp
;
4495 c
.cp
= cacp
->cac_arg
->ca_arg
.ca_str
.s
;
4496 if(cacp
->cac_no
== 1)
4499 show
= !asccasecmp(cacp
->cac_arg
->ca_next
->ca_arg
.ca_str
.s
, "show");
4502 if((gif
= a_tty_bind_ctx_find(c
.cp
)) == (enum n_go_input_flags
)-1){
4503 if(!(aster
= n_is_all_or_aster(c
.cp
)) || !show
){
4504 n_err(_("`bind': invalid context: %s\n"), c
.cp
);
4515 if((fp
= Ftmp(NULL
, "bind", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
){
4516 n_perr(_("tmpfile"), 0);
4523 for(tbcp
= a_tty
.tg_bind
[gif
]; tbcp
!= NULL
;
4524 ++lns
, tbcp
= tbcp
->tbc_next
){
4525 /* Print the bytes of resolved terminal capabilities, then */
4526 if((n_poption
& n_PO_D_V
) &&
4527 (tbcp
->tbc_flags
& (a_TTY_BIND_RESOLVE
| a_TTY_BIND_DEFUNCT
)
4528 ) == a_TTY_BIND_RESOLVE
){
4530 union {wchar_t const *wp
; char const *cp
;} u
;
4533 char const *cnvdat
, *bsep
, *cbufp
;
4538 cbuf
[0] = '=', cbuf
[2] = '\0';
4539 for(cnvdat
= tbcp
->tbc_cnv
, cnvlen
= tbcp
->tbc_cnv_len
;
4541 if(cnvdat
!= tbcp
->tbc_cnv
)
4544 /* {si32_t buf_len_iscap;} */
4545 entlen
= *n_UNALIGN(si32_t
const*,cnvdat
);
4546 if(entlen
& SI32_MIN
){
4547 /* struct{si32_t buf_len_iscap; si32_t cap_len;
4548 * char buf[]+NUL;} */
4550 u
.cp
= (char const*)
4551 &n_UNALIGN(si32_t
const*,cnvdat
)[2];
4552 (c
.c
= *u
.cp
) != '\0'; ++u
.cp
){
4553 if(asciichar(c
.c
) && !cntrlchar(c
.c
))
4554 cbuf
[1] = c
.c
, cbufp
= cbuf
;
4557 fprintf(fp
, "%s\\x%02X%s",
4558 bsep
, (ui32_t
)(ui8_t
)c
.c
, cbufp
);
4573 fprintf(fp
, "%sbind %s %s %s%s%s\n",
4574 ((tbcp
->tbc_flags
& a_TTY_BIND_DEFUNCT
)
4575 /* I18N: `bind' sequence not working, either because it is
4576 * I18N: using Unicode and that is not available in the locale,
4577 * I18N: or a termcap(5)/terminfo(5) sequence won't work out */
4578 ? _("# <Defunctional> ") : n_empty
),
4579 a_tty_bind_ctx_maps
[gif
].tbcm_name
, tbcp
->tbc_seq
,
4580 n_shexp_quote_cp(tbcp
->tbc_exp
, TRU1
),
4581 (tbcp
->tbc_flags
& a_TTY_BIND_NOCOMMIT
? n_at
: n_empty
),
4582 (!(n_poption
& n_PO_D_VV
) ? n_empty
4583 : (tbcp
->tbc_flags
& a_TTY_BIND_FUN_INTERNAL
4584 ? _(" # MLE internal") : n_empty
))
4587 if(!aster
|| ++gif
>= n__GO_INPUT_CTX_MAX1
)
4590 page_or_print(fp
, lns
);
4594 struct a_tty_bind_parse_ctx tbpc
;
4595 struct n_cmd_arg
*cap
;
4597 memset(&tbpc
, 0, sizeof tbpc
);
4598 tbpc
.tbpc_cmd
= cacp
->cac_desc
->cad_name
;
4599 tbpc
.tbpc_in_seq
= (cap
= cacp
->cac_arg
->ca_next
)->ca_arg
.ca_str
.s
;
4600 if((cap
= cap
->ca_next
) != NULL
){
4601 tbpc
.tbpc_exp
.s
= cap
->ca_arg
.ca_str
.s
;
4602 tbpc
.tbpc_exp
.l
= cap
->ca_arg
.ca_str
.l
;
4604 tbpc
.tbpc_flags
= gif
;
4605 if(!a_tty_bind_create(&tbpc
, TRU1
))
4610 return (v
!= NULL
) ? n_EXIT_OK
: n_EXIT_ERR
;
4615 struct a_tty_bind_parse_ctx tbpc
;
4616 struct a_tty_bind_ctx
*tbcp
;
4617 enum n_go_input_flags gif
;
4619 union {char const *cp
; char *p
;} c
;
4620 struct n_cmd_arg_ctx
*cacp
;
4624 c
.cp
= cacp
->cac_arg
->ca_arg
.ca_str
.s
;
4627 if((gif
= a_tty_bind_ctx_find(c
.cp
)) == (enum n_go_input_flags
)-1){
4628 if(!(aster
= n_is_all_or_aster(c
.cp
))){
4629 n_err(_("`unbind': invalid context: %s\n"), c
.cp
);
4636 c
.cp
= cacp
->cac_arg
->ca_next
->ca_arg
.ca_str
.s
;
4638 if(n_is_all_or_aster(c
.cp
)){
4639 while((tbcp
= a_tty
.tg_bind
[gif
]) != NULL
){
4640 memset(&tbpc
, 0, sizeof tbpc
);
4641 tbpc
.tbpc_tbcp
= tbcp
;
4642 tbpc
.tbpc_flags
= gif
;
4643 a_tty_bind_del(&tbpc
);
4646 memset(&tbpc
, 0, sizeof tbpc
);
4647 tbpc
.tbpc_cmd
= cacp
->cac_desc
->cad_name
;
4648 tbpc
.tbpc_in_seq
= c
.cp
;
4649 tbpc
.tbpc_flags
= gif
;
4651 if(n_UNLIKELY(!a_tty_bind_parse(FAL0
, &tbpc
)))
4653 else if(n_UNLIKELY((tbcp
= tbpc
.tbpc_tbcp
) == NULL
)){
4654 n_err(_("`unbind': no such `bind'ing: %s %s\n"),
4655 a_tty_bind_ctx_maps
[gif
].tbcm_name
, c
.cp
);
4658 a_tty_bind_del(&tbpc
);
4661 if(aster
&& ++gif
< n__GO_INPUT_CTX_MAX1
)
4665 return (v
!= NULL
) ? n_EXIT_OK
: n_EXIT_ERR
;
4667 # endif /* HAVE_KEY_BINDINGS */
4669 #else /* HAVE_MLE */
4671 * The really-nothing-at-all implementation
4674 # ifdef a_TTY_SIGNALS
4676 a_tty_signal(int sig
){
4677 /* Prototype at top */
4678 # ifdef HAVE_TERMCAP
4679 sigset_t nset
, oset
;
4681 NYD_X
; /* Signal handler */
4684 # ifdef HAVE_TERMCAP
4685 n_TERMCAP_SUSPEND(TRU1
);
4689 sigaddset(&nset
, sig
);
4690 sigprocmask(SIG_UNBLOCK
, &nset
, &oset
);
4692 /* When we come here we'll continue editing, so reestablish */
4693 sigprocmask(SIG_BLOCK
, &oset
, (sigset_t
*)NULL
);
4696 n_TERMCAP_RESUME(TRU1
);
4697 # endif /* HAVE_TERMCAP */
4699 # endif /* a_TTY_SIGNALS */
4709 n_tty_destroy(bool_t xit_fastpath
){
4711 n_UNUSED(xit_fastpath
);
4717 (n_tty_readline
)(enum n_go_input_flags gif
, char const *prompt
,
4718 char **linebuf
, size_t *linesize
, size_t n
, bool_t
*histok_or_null
4719 n_MEMORY_DEBUG_ARGS
){
4720 struct n_string xprompt
;
4723 n_UNUSED(histok_or_null
);
4725 if(!(gif
& n_GO_INPUT_PROMPT_NONE
)){
4726 if(n_tty_create_prompt(n_string_creat_auto(&xprompt
), prompt
, gif
) > 0){
4727 fwrite(xprompt
.s_dat
, 1, xprompt
.s_len
, n_tty_fp
);
4732 # ifdef HAVE_TERMCAP
4734 n_TERMCAP_RESUME(FAL0
);
4736 rv
= (readline_restart
)(n_stdin
, linebuf
, linesize
, n
4737 n_MEMORY_DEBUG_ARGSCALL
);
4738 # ifdef HAVE_TERMCAP
4739 n_TERMCAP_SUSPEND(FAL0
);
4747 n_tty_addhist(char const *s
, enum n_go_input_flags gif
){
4753 #endif /* nothing at all */
4755 #undef a_TTY_SIGNALS