1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ TTY (command line) editing interaction.
3 *@ Because we have multiple line-editor implementations, including our own
4 *@ 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 - 2015 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
9 * Permission to use, copy, modify, and/or distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 #ifndef HAVE_AMALGAMATION
28 #if defined HAVE_MLE || defined HAVE_TERMCAP
29 # define a_TTY_SIGNALS
32 /* Shared history support macros */
34 # define a_TTY_HISTFILE(S) \
36 char const *__hist_obsolete = ok_vlook(NAIL_HISTFILE);\
37 if(__hist_obsolete != NULL)\
38 OBSOLETE(_("please use *history-file* instead of *NAIL_HISTFILE*"));\
39 S = ok_vlook(history_file);\
41 (S) = __hist_obsolete;\
43 S = fexpand(S, FEXP_LOCAL | FEXP_NSHELL);\
46 # define a_TTY_HISTSIZE(V) \
48 char const *__hist_obsolete = ok_vlook(NAIL_HISTSIZE);\
49 char const *__sv = ok_vlook(history_size);\
51 if(__hist_obsolete != NULL)\
52 OBSOLETE(_("please use *history-size* instead of *NAIL_HISTSIZE*"));\
54 __sv = __hist_obsolete;\
55 if(__sv == NULL || (__rv = strtol(__sv, NULL, 10)) == 0)\
63 # define a_TTY_CHECK_ADDHIST(S,NOACT) \
75 # define C_HISTORY_SHARED \
80 if(!(pstate & PS_LINE_EDITOR_INIT))\
86 if(!asccasecmp(*argv, "show"))\
88 if(!asccasecmp(*argv, "clear"))\
90 if((entry = strtol(*argv, argv, 10)) > 0 && **argv == '\0')\
93 n_err(_("Synopsis: history: %s\n" \
94 "<show> (default), <clear> or select <NO> from editor history"));\
98 return (v == NULL ? !STOP : !OKAY); /* xxx 1:bad 0:good -- do some */
99 #endif /* HAVE_HISTORY */
102 static sighandler_type a_tty_oint
, a_tty_oquit
, a_tty_oterm
,
104 a_tty_otstp
, a_tty_ottin
, a_tty_ottou
;
108 static void a_tty_sigs_up(void), a_tty_sigs_down(void);
119 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
120 a_tty_oint
= safe_signal(SIGINT
, &n_tty_signal
);
121 a_tty_oquit
= safe_signal(SIGQUIT
, &n_tty_signal
);
122 a_tty_oterm
= safe_signal(SIGTERM
, &n_tty_signal
);
123 a_tty_ohup
= safe_signal(SIGHUP
, &n_tty_signal
);
124 a_tty_otstp
= safe_signal(SIGTSTP
, &n_tty_signal
);
125 a_tty_ottin
= safe_signal(SIGTTIN
, &n_tty_signal
);
126 a_tty_ottou
= safe_signal(SIGTTOU
, &n_tty_signal
);
127 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
132 a_tty_sigs_down(void){
138 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
139 safe_signal(SIGINT
, a_tty_oint
);
140 safe_signal(SIGQUIT
, a_tty_oquit
);
141 safe_signal(SIGTERM
, a_tty_oterm
);
142 safe_signal(SIGHUP
, a_tty_ohup
);
143 safe_signal(SIGTSTP
, a_tty_otstp
);
144 safe_signal(SIGTTIN
, a_tty_ottin
);
145 safe_signal(SIGTTOU
, a_tty_ottou
);
146 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
149 #endif /* a_TTY_SIGNALS */
151 static sigjmp_buf a_tty__actjmp
; /* TODO someday, we won't need it no more */
153 a_tty__acthdl(int s
) /* TODO someday, we won't need it no more */
155 NYD_X
; /* Signal handler */
156 termios_state_reset();
157 siglongjmp(a_tty__actjmp
, s
);
161 getapproval(char const * volatile prompt
, bool_t noninteract_default
)
163 sighandler_type
volatile oint
, ohup
;
168 if (!(options
& OPT_INTERACTIVE
)) {
170 rv
= noninteract_default
;
176 char const *quest
= noninteract_default
177 ? _("[yes]/no? ") : _("[no]/yes? ");
180 prompt
= _("Continue");
181 prompt
= savecatsep(prompt
, ' ', quest
);
184 oint
= safe_signal(SIGINT
, SIG_IGN
);
185 ohup
= safe_signal(SIGHUP
, SIG_IGN
);
186 if ((sig
= sigsetjmp(a_tty__actjmp
, 1)) != 0)
188 safe_signal(SIGINT
, &a_tty__acthdl
);
189 safe_signal(SIGHUP
, &a_tty__acthdl
);
191 if (n_lex_input(prompt
, TRU1
, &termios_state
.ts_linebuf
,
192 &termios_state
.ts_linesize
, NULL
) >= 0)
193 rv
= (boolify(termios_state
.ts_linebuf
, UIZ_MAX
,
194 noninteract_default
) > 0);
196 termios_state_reset();
198 safe_signal(SIGHUP
, ohup
);
199 safe_signal(SIGINT
, oint
);
209 getuser(char const * volatile query
) /* TODO v15-compat obsolete */
211 sighandler_type
volatile oint
, ohup
;
212 char * volatile user
= NULL
;
219 oint
= safe_signal(SIGINT
, SIG_IGN
);
220 ohup
= safe_signal(SIGHUP
, SIG_IGN
);
221 if ((sig
= sigsetjmp(a_tty__actjmp
, 1)) != 0)
223 safe_signal(SIGINT
, &a_tty__acthdl
);
224 safe_signal(SIGHUP
, &a_tty__acthdl
);
226 if (n_lex_input(query
, TRU1
, &termios_state
.ts_linebuf
,
227 &termios_state
.ts_linesize
, NULL
) >= 0)
228 user
= termios_state
.ts_linebuf
;
230 termios_state_reset();
232 safe_signal(SIGHUP
, ohup
);
233 safe_signal(SIGINT
, oint
);
241 getpassword(char const *query
)
243 sighandler_type
volatile oint
, ohup
;
245 char * volatile pass
= NULL
;
250 query
= _("Password: ");
251 fputs(query
, stdout
);
254 /* FIXME everywhere: tcsetattr() generates SIGTTOU when we're not in
255 * FIXME foreground pgrp, and can fail with EINTR!! also affects
256 * FIXME termios_state_reset() */
257 if (options
& OPT_TTYIN
) {
258 tcgetattr(STDIN_FILENO
, &termios_state
.ts_tios
);
259 memcpy(&tios
, &termios_state
.ts_tios
, sizeof tios
);
260 termios_state
.ts_needs_reset
= TRU1
;
261 tios
.c_iflag
&= ~(ISTRIP
);
262 tios
.c_lflag
&= ~(ECHO
| ECHOE
| ECHOK
| ECHONL
);
265 oint
= safe_signal(SIGINT
, SIG_IGN
);
266 ohup
= safe_signal(SIGHUP
, SIG_IGN
);
267 if ((sig
= sigsetjmp(a_tty__actjmp
, 1)) != 0)
269 safe_signal(SIGINT
, &a_tty__acthdl
);
270 safe_signal(SIGHUP
, &a_tty__acthdl
);
272 if (options
& OPT_TTYIN
)
273 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, &tios
);
275 if (readline_restart(stdin
, &termios_state
.ts_linebuf
,
276 &termios_state
.ts_linesize
, 0) >= 0)
277 pass
= termios_state
.ts_linebuf
;
279 termios_state_reset();
280 if (options
& OPT_TTYIN
)
283 safe_signal(SIGHUP
, ohup
);
284 safe_signal(SIGINT
, oint
);
290 #endif /* HAVE_SOCKETS */
293 * MLE: the Mailx-Line-Editor, our homebrew editor
294 * (inspired from NetBSDs sh(1) and dash(1)s hetio.c).
296 * Only used in interactive mode, simply use STDIN_FILENO as point of interest.
297 * TODO . This code should be splitted in funs/raw input/bind modules.
298 * TODO . After I/O layer rewrite, also "output to STDIN_FILENO".
299 * TODO . We work with wide characters, but not for buffer takeovers and
300 * TODO cell2save()ings. This should be changed. For the former the buffer
301 * TODO thus needs to be converted to wide first, and then simply be fed in.
302 * TODO . We repaint too much. To overcome this use the same approach that my
303 * TODO terminal library uses, add a true "virtual screen line" that stores
304 * TODO the actually visible content, keep a notion of "first modified slot"
305 * TODO and "last modified slot" (including "unknown" and "any" specials),
306 * TODO update that virtual instead, then synchronize what has truly changed.
307 * TODO I.e., add an indirection layer.
308 * TODO . No BIDI support.
309 * TODO . `bind': we currently use only one lookup tree.
310 * TODO For absolute graceful behaviour in conjunction (with HAVE_TERMCAP) we
311 * TODO need a lower level tree, which possibly combines bytes into "symbolic
312 * TODO wchar_t values", into "keys" that is, as applicable, and an upper
313 * TODO layer which only works on "keys" in order to possibly combine them
314 * TODO into key sequences. We can reuse existent tree code for that.
315 * TODO We need an additional hashmap which maps termcap/terminfo names to
316 * TODO (their byte representations and) a dynamically assigned unique
317 * TODO "symbolic wchar_t value". This implies we may have incompatibilities
318 * TODO when __STDC_ISO_10646__ is not defined. Also we do need takeover-
319 * TODO bytes storage, but it can be a string_creat_auto in the line struct.
320 * TODO Until then we can run into ambiguities; in rare occasions.
323 /* To avoid memory leaks etc. with the current codebase that simply longjmp(3)s
324 * we're forced to use the very same buffer--the one that is passed through to
325 * us from the outside--to store anything we need, i.e., a "struct cell[]", and
326 * convert that on-the-fly back to the plain char* result once we're done.
327 * To simplify our live, use savestr() buffers for all other needed memory */
329 # ifdef HAVE_KEY_BINDINGS
330 /* Default *bind-timeout* key-sequence continuation timeout, in tenths of
331 * a second. Must fit in 8-bit! Update the manual upon change! */
332 # define a_TTY_BIND_TIMEOUT 2
333 # define a_TTY_BIND_TIMEOUT_MAX SI8_MAX
335 n_CTAV(a_TTY_BIND_TIMEOUT_MAX
<= UI8_MAX
);
337 /* We have a chicken-and-egg problem with `bind' and our termcap layer,
338 * because we may not initialize the latter automatically to allow users to
339 * specify *termcap-disable* and let it mean exactly that.
340 * On the other hand users can be expected to use `bind' in resource file(s).
341 * Therefore bindings which involve termcap/terminfo sequences, and which are
342 * defined before PS_STARTED signals usability of termcap/terminfo, will be
343 * (partially) delayed until tty_init() is called.
344 * And we preallocate space for the expansion of the resolved capability */
345 # define a_TTY_BIND_CAPNAME_MAX 15
346 # define a_TTY_BIND_CAPEXP_ROUNDUP 16
348 n_CTAV(ISPOW2(a_TTY_BIND_CAPEXP_ROUNDUP
));
349 n_CTA(a_TTY_BIND_CAPEXP_ROUNDUP
<= SI8_MAX
/ 2, "Variable must fit in 6-bit");
350 n_CTA(a_TTY_BIND_CAPEXP_ROUNDUP
>= 8, "Variable too small");
351 # endif /* HAVE_KEY_BINDINGS */
353 /* The maximum size (of a_tty_cell's) in a line */
354 # define a_TTY_LINE_MAX SI32_MAX
356 /* (Some more CTAs around) */
357 n_CTA(a_TTY_LINE_MAX
<= SI32_MAX
,
358 "a_TTY_LINE_MAX larger than SI32_MAX, but the MLE uses 32-bit arithmetic");
360 /* When shall the visual screen be scrolled, in % of usable screen width */
361 # define a_TTY_SCROLL_MARGIN_LEFT 15
362 # define a_TTY_SCROLL_MARGIN_RIGHT 10
364 /* fexpand() flags for expand-on-tab */
365 # define a_TTY_TAB_FEXP_FL (FEXP_FULL | FEXP_SILENT | FEXP_MULTIOK)
367 /* Columns to ripoff: outermost may not be touched, plus position indicator.
368 * Must thus be at least 1, but should be >= 1+4 to dig the position indicator
369 * that we place (if there is sufficient space) */
370 # define a_TTY_WIDTH_RIPOFF 5
372 /* The implementation of the MLE functions always exists, and is based upon
373 * the a_TTY_BIND_FUN_* constants, so most of this enum is always necessary */
374 enum a_tty_bind_flags
{
375 # ifdef HAVE_KEY_BINDINGS
376 a_TTY_BIND_RESOLVE
= 1<<8, /* Term cap. yet needs to be resolved */
377 a_TTY_BIND_DEFUNCT
= 1<<9, /* Unicode/term cap. used but not avail. */
378 a_TTY__BIND_MASK
= a_TTY_BIND_RESOLVE
| a_TTY_BIND_DEFUNCT
,
379 /* MLE fun assigned to a one-byte-sequence: this may be used for special
380 * key-sequence bypass processing */
381 a_TTY_BIND_MLE1CNTRL
= 1<<10,
382 a_TTY_BIND_NOCOMMIT
= 1<<11, /* Expansion shall be editable */
385 /* MLE internal commands */
386 a_TTY_BIND_FUN_INTERNAL
= 1<<15,
387 a_TTY__BIND_FUN_SHIFT
= 16,
388 a_TTY__BIND_FUN_SHIFTMAX
= 24,
389 a_TTY__BIND_FUN_MASK
= ((1 << a_TTY__BIND_FUN_SHIFTMAX
) - 1) &
390 ~((1 << a_TTY__BIND_FUN_SHIFT
) - 1),
391 # define a_TTY_BIND_FUN_REDUCE(X) \
392 (((ui32_t)(X) & a_TTY__BIND_FUN_MASK) >> a_TTY__BIND_FUN_SHIFT)
393 # define a_TTY_BIND_FUN_EXPAND(X) \
394 (((ui32_t)(X) & (a_TTY__BIND_FUN_MASK >> a_TTY__BIND_FUN_SHIFT)) << \
395 a_TTY__BIND_FUN_SHIFT)
398 a_TTY_BIND_FUN_ ## N = a_TTY_BIND_FUN_EXPAND(I),
401 a_X(GO_BWD
, 1) a_X(GO_FWD
, 2)
402 a_X(GO_WORD_BWD
, 3) a_X(GO_WORD_FWD
, 4)
403 a_X(GO_HOME
, 5) a_X(GO_END
, 6)
404 a_X(DEL_BWD
, 7) a_X(DEL_FWD
, 8)
405 a_X(SNARF_WORD_BWD
, 9) a_X(SNARF_WORD_FWD
, 10)
406 a_X(SNARF_END
, 11) a_X(SNARF_LINE
, 12)
407 a_X(HIST_BWD
, 13) a_X(HIST_FWD
, 14)
408 a_X(HIST_SRCH_BWD
, 15) a_X(HIST_SRCH_FWD
, 16)
410 a_X(QUOTE_RNDTRIP
, 18)
418 a_X(COMMIT
, 25) /* Must be last one! */
421 a_TTY__BIND_LAST
= 1<<25
423 # ifdef HAVE_KEY_BINDINGS
424 n_CTA((ui32_t
)a_TTY_BIND_RESOLVE
> (ui32_t
)n__LEXINPUT_CTX_MAX
,
425 "Bit carrier lower boundary must be raised to avoid value sharing");
427 n_CTA(a_TTY_BIND_FUN_EXPAND(a_TTY_BIND_FUN_COMMIT
) <
428 (1 << a_TTY__BIND_FUN_SHIFTMAX
),
429 "Bit carrier range must be expanded to represent necessary bits");
430 n_CTA(a_TTY__BIND_LAST
>= (1 << a_TTY__BIND_FUN_SHIFTMAX
),
431 "Bit carrier upper boundary must be raised to avoid value sharing");
432 n_CTA(UICMP(64, a_TTY__BIND_LAST
, <=, SI32_MAX
),
433 "Flag bits excess storage datatype" /* And we need one bit free */);
435 enum a_tty_fun_status
{
436 a_TTY_FUN_STATUS_OK
, /* Worked, next character */
437 a_TTY_FUN_STATUS_COMMIT
, /* Line done */
438 a_TTY_FUN_STATUS_RESTART
, /* Complete restart, reset multibyte etc. */
439 a_TTY_FUN_STATUS_END
/* End, return EOF */
442 enum a_tty_visual_flags
{
444 a_TTY_VF_MOD_CURSOR
= 1<<0, /* Cursor moved */
445 a_TTY_VF_MOD_CONTENT
= 1<<1, /* Content modified */
446 a_TTY_VF_MOD_DIRTY
= 1<<2, /* Needs complete repaint */
447 a_TTY_VF_MOD_SINGLE
= 1<<3, /* TODO Drop when indirection as above comes */
448 a_TTY_VF_REFRESH
= a_TTY_VF_MOD_DIRTY
| a_TTY_VF_MOD_CURSOR
|
449 a_TTY_VF_MOD_CONTENT
| a_TTY_VF_MOD_SINGLE
,
450 a_TTY_VF_BELL
= 1<<8, /* Ring the bell */
451 a_TTY_VF_SYNC
= 1<<9, /* Flush/Sync I/O channel */
453 a_TTY_VF_ALL_MASK
= a_TTY_VF_REFRESH
| a_TTY_VF_BELL
| a_TTY_VF_SYNC
,
454 a_TTY__VF_LAST
= a_TTY_VF_SYNC
457 # ifdef HAVE_KEY_BINDINGS
458 struct a_tty_bind_ctx
{
459 struct a_tty_bind_ctx
*tbc_next
;
460 char *tbc_seq
; /* quence as given (poss. re-quoted), in .tb__buf */
461 char *tbc_exp
; /* ansion, in .tb__buf */
462 /* The .tbc_seq'uence with any terminal capabilities resolved; in fact an
463 * array of structures, the first entry of which is {si32_t buf_len_iscap;}
464 * where the signed bit indicates whether the buffer is a resolved terminal
465 * capability instead of a (possibly multibyte) character. In .tbc__buf */
471 char tbc__buf
[VFIELD_SIZE(0)];
474 struct a_tty_bind_ctx_map
{
475 enum n_lexinput_flags tbcm_ctx
;
476 char const tbcm_name
[12]; /* Name of `bind' context */
478 # endif /* HAVE_KEY_BINDINGS */
480 struct a_tty_bind_default_tuple
{
481 bool_t tbdt_iskey
; /* Whether this is a control key; else termcap query */
482 char tbdt_ckey
; /* Control code */
483 ui16_t tbdt_query
; /* enum n_termcap_query (instead) */
484 char tbdt_exp
[12]; /* String or [0]=NUL/[1]=BIND_FUN_REDUCE() */
486 n_CTA(n__TERMCAP_QUERY_MAX
<= UI16_MAX
,
487 "Enumeration cannot be stored in datatype");
489 # ifdef HAVE_KEY_BINDINGS
490 struct a_tty_bind_parse_ctx
{
491 char const *tbpc_cmd
; /* Command which parses */
492 char const *tbpc_in_seq
; /* In: key sequence */
493 struct str tbpc_exp
; /* In/Out: expansion (or NULL) */
494 struct a_tty_bind_ctx
*tbpc_tbcp
; /* Out: if yet existent */
495 struct a_tty_bind_ctx
*tbpc_ltbcp
; /* Out: the one before .tbpc_tbcp */
496 char *tbpc_seq
; /* Out: normalized sequence */
497 char *tbpc_cnv
; /* Out: sequence when read(2)ing it */
500 ui32_t tbpc_flags
; /* n_lexinput_flags | a_tty_bind_flags */
501 ui8_t tbpc__dummy
[4];
504 /* Input character tree */
505 struct a_tty_bind_tree
{
506 struct a_tty_bind_tree
*tbt_sibling
; /* s at same level */
507 struct a_tty_bind_tree
*tbt_childs
; /* Sequence continues.. here */
508 struct a_tty_bind_tree
*tbt_parent
;
509 struct a_tty_bind_ctx
*tbt_bind
; /* NULL for intermediates */
510 wchar_t tbt_char
; /* acter this level represents */
511 bool_t tbt_isseq
; /* Belongs to multibyte sequence */
512 bool_t tbt_isseq_trail
; /* ..is trailing byte of it */
515 # endif /* HAVE_KEY_BINDINGS */
519 ui16_t tc_count
; /* ..of bytes */
520 ui8_t tc_width
; /* Visual width; TAB==UI8_MAX! */
521 bool_t tc_novis
; /* Don't display visually as such (control character) */
522 char tc_cbuf
[MB_LEN_MAX
* 2]; /* .. plus reset shift sequence */
526 struct a_tty_line
*tg_line
; /* To be able to access it from signal hdl */
528 struct a_tty_hist
*tg_hist
;
529 struct a_tty_hist
*tg_hist_tail
;
531 size_t tg_hist_size_max
;
533 # ifdef HAVE_KEY_BINDINGS
534 ui32_t tg_bind_cnt
; /* Overall number of bindings */
535 bool_t tg_bind_isdirty
;
536 bool_t tg_bind_isbuild
;
537 char tg_bind_shcut_cancel
[n__LEXINPUT_CTX_MAX
][5];
538 char tg_bind_shcut_prompt_char
[n__LEXINPUT_CTX_MAX
][5];
539 struct a_tty_bind_ctx
*tg_bind
[n__LEXINPUT_CTX_MAX
];
540 struct a_tty_bind_tree
*tg_bind_tree
[n__LEXINPUT_CTX_MAX
][HSHSIZE
];
542 struct termios tg_tios_old
;
543 struct termios tg_tios_new
;
545 n_CTA(n__LEXINPUT_CTX_MAX
== 2,
546 "Value results in array sizes that results in bad structure layout");
550 struct a_tty_hist
*th_older
;
551 struct a_tty_hist
*th_younger
;
552 # ifdef HAVE_BYTE_ORDER_LITTLE
553 ui32_t th_isgabby
: 1;
556 # ifndef HAVE_BYTE_ORDER_LITTLE
557 ui32_t th_isgabby
: 1;
559 char th_dat
[VFIELD_SIZE(sizeof(ui32_t
))];
564 /* Caller pointers */
566 size_t *tl_x_bufsize
;
567 /* Input processing */
568 # ifdef HAVE_KEY_BINDINGS
569 wchar_t tl_bind_takeover
; /* Leftover byte to consume next */
570 ui8_t tl_bind_timeout
; /* In-seq. inter-byte-timer, in 1/10th secs */
571 ui8_t tl__bind_dummy
[3];
572 char (*tl_bind_shcut_cancel
)[5]; /* Special _CANCEL shortcut control */
573 char (*tl_bind_shcut_prompt_char
)[5]; /* ..for _PROMPT_CHAR */
574 struct a_tty_bind_tree
*(*tl_bind_tree_hmap
)[HSHSIZE
]; /* Bind lookup tree */
575 struct a_tty_bind_tree
*tl_bind_tree
;
577 char const *tl_reenter_after_cmd
; /* `bind' cmd to exec, then re-readline */
578 /* Line data / content handling */
579 ui32_t tl_count
; /* ..of a_tty_cell's (<= a_TTY_LINE_MAX) */
580 ui32_t tl_cursor
; /* Current a_tty_cell insertion point */
582 char *cbuf
; /* *.tl_x_buf */
583 struct a_tty_cell
*cells
;
585 struct str tl_defc
; /* Current default content */
586 size_t tl_defc_cursor_byte
; /* Desired position of cursor after takeover */
587 struct str tl_savec
; /* Saved default content */
588 struct str tl_pastebuf
; /* Last snarfed data */
590 struct a_tty_hist
*tl_hist
; /* History cursor */
592 ui32_t tl_count_max
; /* ..before buffer needs to grow */
593 /* Visual data representation handling */
594 ui32_t tl_vi_flags
; /* enum a_tty_visual_flags */
595 ui32_t tl_lst_count
; /* .tl_count after last sync */
596 ui32_t tl_lst_cursor
; /* .tl_cursor after last sync */
597 /* TODO Add another indirection layer by adding a tl_phy_line of
598 * TODO a_tty_cell objects, incorporate changes in visual layer,
599 * TODO then check what _really_ has changed, sync those changes only */
600 struct a_tty_cell
const *tl_phy_start
; /* First visible cell, left border */
601 ui32_t tl_phy_cursor
; /* Physical cursor position */
602 bool_t tl_quote_rndtrip
; /* For _kht() expansion */
604 ui32_t tl_prompt_length
; /* Preclassified (TODO needed as a_tty_cell) */
605 ui32_t tl_prompt_width
;
606 char const *tl_prompt
; /* Preformatted prompt (including colours) */
607 /* .tl_pos_buf is a hack */
609 char *tl_pos_buf
; /* mle-position colour-on, [4], reset seq. */
610 char *tl_pos
; /* Address of the [4] */
614 # ifdef HAVE_KEY_BINDINGS
615 /* C99: use [INDEX]={} */
616 n_CTAV(n_LEXINPUT_CTX_BASE
== 0);
617 n_CTAV(n_LEXINPUT_CTX_COMPOSE
== 1);
618 static struct a_tty_bind_ctx_map
const
619 a_tty_bind_ctx_maps
[n__LEXINPUT_CTX_MAX
] = {
620 {n_LEXINPUT_CTX_BASE
, "base"},
621 {n_LEXINPUT_CTX_COMPOSE
, "compose"}
624 /* Special functions which our MLE provides internally.
625 * Update the manual upon change! */
626 static char const a_tty_bind_fun_names
[][24] = {
629 n_FIELD_INITI(a_TTY_BIND_FUN_REDUCE(a_TTY_BIND_FUN_ ## I)) "mle-" N "\0",
632 a_X(GO_BWD
, "go-bwd") a_X(GO_FWD
, "go-fwd")
633 a_X(GO_WORD_BWD
, "go-word-bwd") a_X(GO_WORD_FWD
, "go-word-fwd")
634 a_X(GO_HOME
, "go-home") a_X(GO_END
, "go-end")
635 a_X(DEL_BWD
, "del-bwd") a_X(DEL_FWD
, "del-fwd")
636 a_X(SNARF_WORD_BWD
, "snarf-word-bwd") a_X(SNARF_WORD_FWD
, "snarf-word-fwd")
637 a_X(SNARF_END
, "snarf-end") a_X(SNARF_LINE
, "snarf-line")
638 a_X(HIST_BWD
, "hist-bwd") a_X(HIST_FWD
, "hist-fwd")
639 a_X(HIST_SRCH_BWD
, "hist-srch-bwd") a_X(HIST_SRCH_FWD
, "hist-srch-fwd")
640 a_X(REPAINT
, "repaint")
641 a_X(QUOTE_RNDTRIP
, "quote-rndtrip")
642 a_X(PROMPT_CHAR
, "prompt-char")
643 a_X(COMPLETE
, "complete")
646 a_X(CANCEL
, "cancel")
648 a_X(FULLRESET
, "fullreset")
649 a_X(COMMIT
, "commit")
653 # endif /* HAVE_KEY_BINDINGS */
655 /* The default key bindings (unless disallowed). Update manual upon change!
656 * A logical subset of this table is also used if !HAVE_KEY_BINDINGS (more
657 * expensive than a switch() on control codes directly, but less redundant) */
658 static struct a_tty_bind_default_tuple
const a_tty_bind_default_tuples
[] = {
661 {TRU1, K, 0, {'\0', (char)a_TTY_BIND_FUN_REDUCE(a_TTY_BIND_FUN_ ## S),}},
679 a_X('Q', QUOTE_RNDTRIP
)
680 a_X('R', HIST_SRCH_BWD
)
681 a_X('S', HIST_SRCH_FWD
)
684 a_X('V', PROMPT_CHAR
)
685 a_X('W', SNARF_WORD_BWD
)
686 a_X('X', GO_WORD_FWD
)
687 a_X('Y', GO_WORD_BWD
)
694 a_X('_', SNARF_WORD_FWD
)
699 # define a_X(K,S) {TRU1, K, 0, {S}},
706 # ifdef HAVE_KEY_BINDINGS
709 {FAL0, '\0', n_TERMCAP_QUERY_ ## Q,\
710 {'\0', (char)a_TTY_BIND_FUN_REDUCE(a_TTY_BIND_FUN_ ## S),}},
712 a_X(key_backspace
, DEL_BWD
) a_X(key_dc
, DEL_FWD
)
713 a_X(key_eol
, SNARF_END
)
714 a_X(key_home
, GO_HOME
) a_X(key_end
, GO_END
)
715 a_X(key_left
, GO_BWD
) a_X(key_right
, GO_FWD
)
716 a_X(key_sleft
, GO_HOME
) a_X(key_sright
, GO_END
)
717 a_X(key_up
, HIST_BWD
) a_X(key_down
, HIST_FWD
)
720 # define a_X(Q,S) {FAL0, '\0', n_TERMCAP_QUERY_ ## Q, {S}},
722 a_X(key_shome
, "z0") a_X(key_send
, "z$")
723 a_X(xkey_sup
, "z0") a_X(xkey_sdown
, "z$")
724 a_X(key_ppage
, "z-") a_X(key_npage
, "z+")
725 a_X(xkey_cup
, "dotmove-") a_X(xkey_cdown
, "dotmove+")
727 # endif /* HAVE_KEY_BINDINGS */
731 static struct a_tty_global a_tty
;
733 /* Change from canonical to raw, non-canonical mode, and way back */
734 static void a_tty_term_mode(bool_t raw
);
736 /* Adjust an active raw mode to use / not use a timeout */
737 # ifdef HAVE_KEY_BINDINGS
738 static void a_tty_term_rawmode_timeout(struct a_tty_line
*tlp
, bool_t enable
);
741 /* 0-X (2), UI8_MAX == \t / TAB */
742 static ui8_t
a_tty_wcwidth(wchar_t wc
);
744 /* Memory / cell / word generics */
745 static void a_tty_check_grow(struct a_tty_line
*tlp
, ui32_t no
747 static ssize_t
a_tty_cell2dat(struct a_tty_line
*tlp
);
748 static void a_tty_cell2save(struct a_tty_line
*tlp
);
750 /* Save away data bytes of given range (max = non-inclusive) */
751 static void a_tty_copy2paste(struct a_tty_line
*tlp
, struct a_tty_cell
*tcpmin
,
752 struct a_tty_cell
*tcpmax
);
754 /* Ask user for hexadecimal number, interpret as UTF-32 */
755 static wchar_t a_tty_vinuni(struct a_tty_line
*tlp
);
757 /* Visual screen synchronization */
758 static bool_t
a_tty_vi_refresh(struct a_tty_line
*tlp
);
760 static bool_t
a_tty_vi__paint(struct a_tty_line
*tlp
);
762 /* Search for word boundary, starting at tl_cursor, in "dir"ection (<> 0).
763 * Return <0 when moving is impossible (backward direction but in position 0,
764 * forward direction but in outermost column), and relative distance to
765 * tl_cursor otherwise */
766 static si32_t
a_tty_wboundary(struct a_tty_line
*tlp
, si32_t dir
);
768 /* Most function implementations */
769 static void a_tty_khome(struct a_tty_line
*tlp
, bool_t dobell
);
770 static void a_tty_kend(struct a_tty_line
*tlp
);
771 static void a_tty_kbs(struct a_tty_line
*tlp
);
772 static void a_tty_ksnarf(struct a_tty_line
*tlp
, bool_t cplline
, bool_t dobell
);
773 static si32_t
a_tty_kdel(struct a_tty_line
*tlp
);
774 static void a_tty_kleft(struct a_tty_line
*tlp
);
775 static void a_tty_kright(struct a_tty_line
*tlp
);
776 static void a_tty_ksnarfw(struct a_tty_line
*tlp
, bool_t fwd
);
777 static void a_tty_kgow(struct a_tty_line
*tlp
, si32_t dir
);
778 static bool_t
a_tty_kother(struct a_tty_line
*tlp
, wchar_t wc
);
779 static ui32_t
a_tty_kht(struct a_tty_line
*tlp
);
781 static ui32_t
a_tty__khist_shared(struct a_tty_line
*tlp
,
782 struct a_tty_hist
*thp
);
783 static ui32_t
a_tty_khist(struct a_tty_line
*tlp
, bool_t fwd
);
784 static ui32_t
a_tty_khist_search(struct a_tty_line
*tlp
, bool_t fwd
);
787 /* Handle a function */
788 static enum a_tty_fun_status
a_tty_fun(struct a_tty_line
*tlp
,
789 enum a_tty_bind_flags tbf
, size_t *len
);
792 static ssize_t
a_tty_readline(struct a_tty_line
*tlp
, size_t len
795 # ifdef HAVE_KEY_BINDINGS
796 /* Find context or -1 */
797 static enum n_lexinput_flags
a_tty_bind_ctx_find(char const *name
);
799 /* Create (or replace, if allowed) a binding */
800 static bool_t
a_tty_bind_create(struct a_tty_bind_parse_ctx
*tbpcp
,
803 /* Shared implementation to parse `bind' and `unbind' "key-sequence" and
804 * "expansion" command line arguments into something that we can work with */
805 static bool_t
a_tty_bind_parse(bool_t isbindcmd
,
806 struct a_tty_bind_parse_ctx
*tbpcp
);
808 /* Lazy resolve a termcap(5)/terminfo(5) (or *termcap*!) capability */
809 static void a_tty_bind_resolve(struct a_tty_bind_ctx
*tbcp
);
811 /* Delete an existing binding */
812 static void a_tty_bind_del(struct a_tty_bind_parse_ctx
*tbpcp
);
814 /* Life cycle of all input node trees */
815 static void a_tty_bind_tree_build(void);
816 static void a_tty_bind_tree_teardown(void);
818 static void a_tty__bind_tree_add(ui32_t hmap_idx
,
819 struct a_tty_bind_tree
*store
[HSHSIZE
],
820 struct a_tty_bind_ctx
*tbcp
);
821 static struct a_tty_bind_tree
*a_tty__bind_tree_add_wc(
822 struct a_tty_bind_tree
**treep
, struct a_tty_bind_tree
*parentp
,
823 wchar_t wc
, bool_t isseq
);
824 static void a_tty__bind_tree_free(struct a_tty_bind_tree
*tbtp
);
825 # endif /* HAVE_KEY_BINDINGS */
828 a_tty_term_mode(bool_t raw
){
829 struct termios
*tiosp
;
832 tiosp
= &a_tty
.tg_tios_old
;
836 /* Always requery the attributes, in case we've been moved from background
837 * to foreground or however else in between sessions */
838 /* XXX Always enforce ECHO and ICANON in the OLD attributes - do so as long
839 * XXX as we don't properly deal with TTIN and TTOU etc. */
840 tcgetattr(STDIN_FILENO
, tiosp
);
841 tiosp
->c_lflag
|= ECHO
| ICANON
;
843 memcpy(&a_tty
.tg_tios_new
, tiosp
, sizeof *tiosp
);
844 tiosp
= &a_tty
.tg_tios_new
;
845 tiosp
->c_cc
[VMIN
] = 1;
846 tiosp
->c_cc
[VTIME
] = 0;
847 /* Enable ^\, ^Q and ^S to be used for key bindings */
848 tiosp
->c_cc
[VQUIT
] = tiosp
->c_cc
[VSTART
] = tiosp
->c_cc
[VSTOP
] = '\0';
849 tiosp
->c_iflag
&= ~(ISTRIP
| IGNCR
);
850 tiosp
->c_lflag
&= ~(ECHO
/*| ECHOE | ECHONL */| ICANON
| IEXTEN
);
852 tcsetattr(STDIN_FILENO
, TCSADRAIN
, tiosp
);
856 # ifdef HAVE_KEY_BINDINGS
858 a_tty_term_rawmode_timeout(struct a_tty_line
*tlp
, bool_t enable
){
863 a_tty
.tg_tios_new
.c_cc
[VMIN
] = 0;
864 if((bt
= tlp
->tl_bind_timeout
) == 0)
865 bt
= a_TTY_BIND_TIMEOUT
;
866 a_tty
.tg_tios_new
.c_cc
[VTIME
] = bt
;
868 a_tty
.tg_tios_new
.c_cc
[VMIN
] = 1;
869 a_tty
.tg_tios_new
.c_cc
[VTIME
] = 0;
871 tcsetattr(STDIN_FILENO
, TCSANOW
, &a_tty
.tg_tios_new
);
874 # endif /* HAVE_KEY_BINDINGS */
877 a_tty_wcwidth(wchar_t wc
){
881 /* Special case the backslash at first */
888 rv
= ((i
= wcwidth(wc
)) > 0) ? (ui8_t
)i
: 0;
890 rv
= iswprint(wc
) ? 1 + (wc
>= 0x1100u
) : 0; /* TODO use S-CText */
898 a_tty_check_grow(struct a_tty_line
*tlp
, ui32_t no SMALLOC_DEBUG_ARGS
){
902 if(UNLIKELY((cmax
= tlp
->tl_count
+ no
) > tlp
->tl_count_max
)){
905 i
= cmax
* sizeof(struct a_tty_cell
) + 2 * sizeof(struct a_tty_cell
);
906 if(LIKELY(i
>= *tlp
->tl_x_bufsize
)){
907 hold_all_sigs(); /* XXX v15 drop */
910 *tlp
->tl_x_buf
= (srealloc
)(*tlp
->tl_x_buf
, i SMALLOC_DEBUG_ARGSCALL
);
911 rele_all_sigs(); /* XXX v15 drop */
913 tlp
->tl_count_max
= cmax
;
914 *tlp
->tl_x_bufsize
= i
;
920 a_tty_cell2dat(struct a_tty_line
*tlp
){
926 if(LIKELY((i
= tlp
->tl_count
) > 0)){
927 struct a_tty_cell
const *tcap
;
929 tcap
= tlp
->tl_line
.cells
;
931 memcpy(tlp
->tl_line
.cbuf
+ len
, tcap
->tc_cbuf
, tcap
->tc_count
);
932 len
+= tcap
->tc_count
;
933 }while(++tcap
, --i
> 0);
936 tlp
->tl_line
.cbuf
[len
] = '\0';
942 a_tty_cell2save(struct a_tty_line
*tlp
){
944 struct a_tty_cell
*tcap
;
947 tlp
->tl_savec
.s
= NULL
;
950 if(UNLIKELY(tlp
->tl_count
== 0))
953 for(tcap
= tlp
->tl_line
.cells
, len
= 0, i
= tlp
->tl_count
; i
> 0;
955 len
+= tcap
->tc_count
;
957 tlp
->tl_savec
.s
= salloc((tlp
->tl_savec
.l
= len
) +1);
959 for(tcap
= tlp
->tl_line
.cells
, len
= 0, i
= tlp
->tl_count
; i
> 0;
961 memcpy(tlp
->tl_savec
.s
+ len
, tcap
->tc_cbuf
, tcap
->tc_count
);
962 len
+= tcap
->tc_count
;
964 tlp
->tl_savec
.s
[len
] = '\0';
970 a_tty_copy2paste(struct a_tty_line
*tlp
, struct a_tty_cell
*tcpmin
,
971 struct a_tty_cell
*tcpmax
){
973 struct a_tty_cell
*tcp
;
978 for(tcp
= tcpmin
; tcp
< tcpmax
; ++tcp
)
981 tlp
->tl_pastebuf
.s
= cp
= salloc((tlp
->tl_pastebuf
.l
= l
) +1);
984 for(tcp
= tcpmin
; tcp
< tcpmax
; cp
+= l
, ++tcp
)
985 memcpy(cp
, tcp
->tc_cbuf
, l
= tcp
->tc_count
);
991 a_tty_vinuni(struct a_tty_line
*tlp
){
993 union {size_t i
; long l
;} u
;
999 if(!n_termcap_cmdx(n_TERMCAP_CMD_cr
) ||
1000 !n_termcap_cmd(n_TERMCAP_CMD_ce
, 0, -1))
1004 struct str
const *cpre
, *csuf
;
1006 struct n_colour_pen
*cpen
;
1008 cpen
= n_colour_pen_create(n_COLOUR_ID_MLE_PROMPT
, NULL
);
1009 if((cpre
= n_colour_pen_to_str(cpen
)) != NULL
)
1010 csuf
= n_colour_reset_to_str();
1016 printf(_("%sPlease enter Unicode code point:%s "),
1017 (cpre
!= NULL
? cpre
->s
: ""), (csuf
!= NULL
? csuf
->s
: ""));
1021 buf
[sizeof(buf
) -1] = '\0';
1023 if(read(STDIN_FILENO
, &buf
[u
.i
], 1) != 1){
1024 if(errno
== EINTR
) /* xxx #if !SA_RESTART ? */
1028 if(buf
[u
.i
] == '\n')
1030 if(!hexchar(buf
[u
.i
])){
1031 char const emsg
[] = "[0-9a-fA-F]";
1033 LCTA(sizeof emsg
<= sizeof(buf
));
1034 memcpy(buf
, emsg
, sizeof emsg
);
1038 putc(buf
[u
.i
], stdout
);
1040 if(++u
.i
== sizeof buf
)
1045 u
.l
= strtol(buf
, &eptr
, 16);
1046 if(u
.l
<= 0 || u
.l
>= 0x10FFFF/* XXX magic; CText */ || *eptr
!= '\0'){
1048 n_err(_("\nInvalid input: %s\n"), buf
);
1054 tlp
->tl_vi_flags
|= a_TTY_VF_MOD_DIRTY
| (wc
== '\0' ? a_TTY_VF_BELL
: 0);
1060 a_tty_vi_refresh(struct a_tty_line
*tlp
){
1064 if(tlp
->tl_vi_flags
& a_TTY_VF_BELL
){
1065 tlp
->tl_vi_flags
|= a_TTY_VF_SYNC
;
1066 if(putchar('\a') == EOF
)
1070 if(tlp
->tl_vi_flags
& a_TTY_VF_REFRESH
){
1071 /* kht may want to restore a cursor position after inserting some
1073 if(tlp
->tl_defc_cursor_byte
> 0){
1077 a_tty_khome(tlp
, FAL0
);
1079 i
= tlp
->tl_defc_cursor_byte
;
1080 tlp
->tl_defc_cursor_byte
= 0;
1081 for(j
= 0; tlp
->tl_cursor
< tlp
->tl_count
; ++j
){
1083 if((k
= tlp
->tl_line
.cells
[j
].tc_count
) > i
)
1089 if(!a_tty_vi__paint(tlp
))
1093 if(tlp
->tl_vi_flags
& a_TTY_VF_SYNC
){
1094 tlp
->tl_vi_flags
&= ~a_TTY_VF_SYNC
;
1101 tlp
->tl_vi_flags
&= ~a_TTY_VF_ALL_MASK
;
1106 clearerr(stdout
); /* xxx I/O layer rewrite */
1107 n_err(_("Visual refresh failed! Is $TERM set correctly?\n"
1108 " Setting *line-editor-disable* to get us through!\n"));
1109 ok_bset(line_editor_disable
, TRU1
);
1115 a_tty_vi__paint(struct a_tty_line
*tlp
){
1117 a_TRUE_RV
= a_TTY__VF_LAST
<<1, /* Return value bit */
1118 a_HAVE_PROMPT
= a_TTY__VF_LAST
<<2, /* Have a prompt */
1119 a_SHOW_PROMPT
= a_TTY__VF_LAST
<<3, /* Shall print the prompt */
1120 a_MOVE_CURSOR
= a_TTY__VF_LAST
<<4, /* Move visual cursor for user! */
1121 a_LEFT_MIN
= a_TTY__VF_LAST
<<5, /* On left boundary */
1122 a_RIGHT_MAX
= a_TTY__VF_LAST
<<6,
1123 a_HAVE_POSITION
= a_TTY__VF_LAST
<<7, /* Print the position indicator */
1125 /* We carry some flags over invocations (not worth a specific field) */
1126 a_VISIBLE_PROMPT
= a_TTY__VF_LAST
<<8, /* The prompt is on the screen */
1127 a_PERSIST_MASK
= a_VISIBLE_PROMPT
,
1128 a__LAST
= a_PERSIST_MASK
1131 ui32_t f
, w
, phy_wid_base
, phy_wid
, phy_base
, phy_cur
, cnt
, lstcur
, cur
,
1132 vi_left
, vi_right
, phy_nxtcur
;
1133 struct a_tty_cell
const *tccp
, *tcp_left
, *tcp_right
, *tcxp
;
1135 n_LCTA(UICMP(64, a__LAST
, <, UI32_MAX
), "Flag bits excess storage datatype");
1137 f
= tlp
->tl_vi_flags
;
1138 tlp
->tl_vi_flags
= (f
& ~(a_TTY_VF_REFRESH
| a_PERSIST_MASK
)) |
1141 if((w
= tlp
->tl_prompt_length
) > 0)
1143 f
|= a_HAVE_POSITION
;
1145 /* XXX We don't have a OnTerminalResize event (see main.c) yet, so we need
1146 * XXX to reevaluate our circumstances over and over again */
1147 /* Don't display prompt or position indicator on very small screens */
1148 if((phy_wid_base
= (ui32_t
)scrnwidth
) <= a_TTY_WIDTH_RIPOFF
)
1149 f
&= ~(a_HAVE_PROMPT
| a_HAVE_POSITION
);
1151 phy_wid_base
-= a_TTY_WIDTH_RIPOFF
;
1153 /* Disable the prompt if the screen is too small; due to lack of some
1154 * indicator simply add a second ripoff */
1155 if((f
& a_HAVE_PROMPT
) && w
+ a_TTY_WIDTH_RIPOFF
>= phy_wid_base
)
1156 f
&= ~a_HAVE_PROMPT
;
1159 phy_wid
= phy_wid_base
;
1161 phy_cur
= tlp
->tl_phy_cursor
;
1162 cnt
= tlp
->tl_count
;
1163 lstcur
= tlp
->tl_lst_cursor
;
1165 /* XXX Assume dirty screen if shrunk */
1166 if(cnt
< tlp
->tl_lst_count
)
1167 f
|= a_TTY_VF_MOD_DIRTY
;
1169 /* TODO Without HAVE_TERMCAP, it would likely be much cheaper to simply
1170 * TODO always "cr + paint + ce + ch", since ce is simulated via spaces.. */
1172 /* Quickshot: if the line is empty, possibly print prompt and out */
1174 /* In that special case dirty anything if it seems better */
1175 if((f
& a_TTY_VF_MOD_CONTENT
) || tlp
->tl_lst_count
> 0)
1176 f
|= a_TTY_VF_MOD_DIRTY
;
1178 if((f
& a_TTY_VF_MOD_DIRTY
) && phy_cur
!= 0){
1179 if(!n_termcap_cmdx(n_TERMCAP_CMD_cr
))
1184 if((f
& (a_TTY_VF_MOD_DIRTY
| a_HAVE_PROMPT
)) ==
1185 (a_TTY_VF_MOD_DIRTY
| a_HAVE_PROMPT
)){
1186 if(fputs(tlp
->tl_prompt
, stdout
) == EOF
)
1188 phy_cur
= tlp
->tl_prompt_width
+ 1;
1191 /* May need to clear former line content */
1192 if((f
& a_TTY_VF_MOD_DIRTY
) &&
1193 !n_termcap_cmd(n_TERMCAP_CMD_ce
, phy_cur
, -1))
1196 tlp
->tl_phy_start
= tlp
->tl_line
.cells
;
1200 /* Try to get an idea of the visual window */
1202 /* Find the left visual boundary */
1203 phy_wid
= (phy_wid
>> 1) + (phy_wid
>> 2);
1204 if((cur
= tlp
->tl_cursor
) == cnt
)
1207 w
= (tcp_left
= tccp
= tlp
->tl_line
.cells
+ cur
)->tc_width
;
1208 if(w
== UI8_MAX
) /* TODO yet TAB == SPC */
1210 while(tcp_left
> tlp
->tl_line
.cells
){
1211 ui16_t cw
= tcp_left
[-1].tc_width
;
1213 if(cw
== UI8_MAX
) /* TODO yet TAB == SPC */
1215 if(w
+ cw
>= phy_wid
)
1222 /* If the left hand side of our visual viewpoint consumes less than half
1223 * of the screen width, show the prompt */
1224 if(tcp_left
== tlp
->tl_line
.cells
)
1227 if((f
& (a_LEFT_MIN
| a_HAVE_PROMPT
)) == (a_LEFT_MIN
| a_HAVE_PROMPT
) &&
1228 w
+ tlp
->tl_prompt_width
< phy_wid
){
1229 phy_base
= tlp
->tl_prompt_width
;
1233 /* Then search for right boundary. We always leave the rightmost column
1234 * empty because some terminals [cw]ould wrap the line if we write into
1235 * that. XXX terminfo(5)/termcap(5) have the semi_auto_right_margin/sam/YE
1236 * XXX capability to indicate this, but we don't look at that */
1237 phy_wid
= phy_wid_base
- phy_base
;
1238 tcp_right
= tlp
->tl_line
.cells
+ cnt
;
1240 while(&tccp
[1] < tcp_right
){
1241 ui16_t cw
= tccp
[1].tc_width
;
1244 if(cw
== UI8_MAX
) /* TODO yet TAB == SPC */
1252 vi_right
= w
- vi_left
;
1254 /* If the complete line including prompt fits on the screen, show prompt */
1255 if(--tcp_right
== tccp
){
1258 /* Since we did brute-force walk also for the left boundary we may end up
1259 * in a situation were anything effectively fits on the screen, including
1260 * the prompt that is, but were we don't recognize this since we
1261 * restricted the search to fit in some visual viewpoint. Therefore try
1262 * again to extend the left boundary to overcome that */
1263 if(!(f
& a_LEFT_MIN
)){
1264 struct a_tty_cell
const *tc1p
= tlp
->tl_line
.cells
;
1265 ui32_t vil1
= vi_left
;
1267 assert(!(f
& a_SHOW_PROMPT
));
1268 w
+= tlp
->tl_prompt_width
;
1269 for(tcxp
= tcp_left
;;){
1270 ui32_t i
= tcxp
[-1].tc_width
;
1272 if(i
== UI8_MAX
) /* TODO yet TAB == SPC */
1286 /*w -= tlp->tl_prompt_width;*/
1290 tccp
= tlp
->tl_line
.cells
+ cur
;
1292 if((f
& (a_LEFT_MIN
| a_RIGHT_MAX
| a_HAVE_PROMPT
| a_SHOW_PROMPT
)) ==
1293 (a_LEFT_MIN
| a_RIGHT_MAX
| a_HAVE_PROMPT
) &&
1294 w
+ tlp
->tl_prompt_width
<= phy_wid
){
1295 phy_wid
-= (phy_base
= tlp
->tl_prompt_width
);
1299 /* Try to avoid repainting the complete line - this is possible if the
1300 * cursor "did not leave the screen" and the prompt status hasn't changed.
1301 * I.e., after clamping virtual viewpoint, compare relation to physical */
1302 if((f
& (a_TTY_VF_MOD_SINGLE
/*FIXME*/ |
1303 a_TTY_VF_MOD_CONTENT
/* xxx */ | a_TTY_VF_MOD_DIRTY
)) ||
1304 (tcxp
= tlp
->tl_phy_start
) == NULL
||
1305 tcxp
> tccp
|| tcxp
<= tcp_right
)
1306 f
|= a_TTY_VF_MOD_DIRTY
;
1308 f
|= a_TTY_VF_MOD_DIRTY
;
1310 struct a_tty_cell
const *tcyp
;
1311 si32_t cur_displace
;
1312 ui32_t phy_lmargin
, phy_rmargin
, fx
, phy_displace
;
1314 phy_lmargin
= (fx
= phy_wid
) / 100;
1315 phy_rmargin
= fx
- (phy_lmargin
* a_TTY_SCROLL_MARGIN_RIGHT
);
1316 phy_lmargin
*= a_TTY_SCROLL_MARGIN_LEFT
;
1317 fx
= (f
& (a_SHOW_PROMPT
| a_VISIBLE_PROMPT
));
1319 if(fx
== 0 || fx
== (a_SHOW_PROMPT
| a_VISIBLE_PROMPT
)){
1325 /* We know what we have to paint, start synchronizing */
1327 assert(phy_cur
== tlp
->tl_phy_cursor
);
1328 assert(phy_wid
== phy_wid_base
- phy_base
);
1329 assert(cnt
== tlp
->tl_count
);
1331 assert(lstcur
== tlp
->tl_lst_cursor
);
1332 assert(tccp
== tlp
->tl_line
.cells
+ cur
);
1334 phy_nxtcur
= phy_base
; /* FIXME only if repaint cpl. */
1336 /* Quickshot: is it only cursor movement within the visible screen? */
1337 if((f
& a_TTY_VF_REFRESH
) == a_TTY_VF_MOD_CURSOR
){
1342 /* To be able to apply some quick jump offs, clear line if possible */
1343 if(f
& a_TTY_VF_MOD_DIRTY
){
1344 /* Force complete clearance and cursor reinitialization */
1345 if(!n_termcap_cmdx(n_TERMCAP_CMD_cr
) ||
1346 !n_termcap_cmd(n_TERMCAP_CMD_ce
, 0, -1))
1348 tlp
->tl_phy_start
= tcp_left
;
1352 if((f
& (a_TTY_VF_MOD_DIRTY
| a_SHOW_PROMPT
)) && phy_cur
!= 0){
1353 if(!n_termcap_cmdx(n_TERMCAP_CMD_cr
))
1358 if(f
& a_SHOW_PROMPT
){
1359 assert(phy_base
== tlp
->tl_prompt_width
);
1360 if(fputs(tlp
->tl_prompt
, stdout
) == EOF
)
1362 phy_cur
= phy_nxtcur
;
1363 f
|= a_VISIBLE_PROMPT
;
1365 f
&= ~a_VISIBLE_PROMPT
;
1367 /* FIXME reposition cursor for paint */
1368 for(w
= phy_nxtcur
; tcp_left
<= tcp_right
; ++tcp_left
){
1371 cw
= tcp_left
->tc_width
;
1373 if(LIKELY(!tcp_left
->tc_novis
)){
1374 if(fwrite(tcp_left
->tc_cbuf
, sizeof *tcp_left
->tc_cbuf
,
1375 tcp_left
->tc_count
, stdout
) != tcp_left
->tc_count
)
1377 }else{ /* XXX Shouldn't be here <-> CText, ui_str.c */
1378 char wbuf
[8]; /* XXX magic */
1380 if(options
& OPT_UNICODE
){
1383 wc
= (ui32_t
)tcp_left
->tc_wc
;
1384 if((wc
& ~0x1Fu
) == 0)
1390 n_utf32_to_utf8(wc
, wbuf
);
1392 wbuf
[0] = '?', wbuf
[1] = '\0';
1394 if(fputs(wbuf
, stdout
) == EOF
)
1399 if(cw
== UI8_MAX
) /* TODO yet TAB == SPC */
1402 if(tcp_left
== tccp
)
1407 /* Write something position marker alike if it doesn't fit on screen */
1408 if((f
& a_HAVE_POSITION
) &&
1409 ((f
& (a_LEFT_MIN
| a_RIGHT_MAX
)) != (a_LEFT_MIN
| a_RIGHT_MAX
) ||
1410 ((f
& a_HAVE_PROMPT
) && !(f
& a_SHOW_PROMPT
)))){
1412 char *posbuf
= tlp
->tl_pos_buf
, *pos
= tlp
->tl_pos
;
1414 char posbuf
[5], *pos
= posbuf
;
1419 if(phy_cur
!= (w
= phy_wid_base
) &&
1420 !n_termcap_cmd(n_TERMCAP_CMD_ch
, phy_cur
= w
, 0))
1424 if((f
& a_LEFT_MIN
) && (!(f
& a_HAVE_PROMPT
) || (f
& a_SHOW_PROMPT
)))
1425 memcpy(pos
, "^.+", 3);
1426 else if(f
& a_RIGHT_MAX
)
1427 memcpy(pos
, ".+$", 3);
1429 /* Theoretical line length limit a_TTY_LINE_MAX, choose next power of
1430 * ten (10 ** 10) to represent 100 percent, since we don't have a macro
1431 * that generates a constant, and i don't trust the standard "u type
1432 * suffix automatically scales" calculate the large number */
1433 static char const itoa
[] = "0123456789";
1435 ui64_t
const fact100
= (ui64_t
)0x3B9ACA00u
* 10u, fact
= fact100
/ 100;
1436 ui32_t i
= (ui32_t
)(((fact100
/ cnt
) * tlp
->tl_cursor
) / fact
);
1437 n_LCTA(a_TTY_LINE_MAX
<= SI32_MAX
, "a_TTY_LINE_MAX too large");
1440 pos
[0] = ' ', pos
[1] = itoa
[i
];
1442 pos
[1] = itoa
[i
% 10], pos
[0] = itoa
[i
/ 10];
1446 if(fputs(posbuf
, stdout
) == EOF
)
1451 /* Users are used to see the cursor right of the point of interest, so we
1452 * need some further adjustments unless in special conditions. Be aware
1453 * that we may have adjusted cur at the beginning, too */
1454 if((cur
= tlp
->tl_cursor
) == 0)
1455 phy_nxtcur
= phy_base
;
1456 else if(cur
!= cnt
){
1457 ui16_t cw
= tccp
->tc_width
;
1459 if(cw
== UI8_MAX
) /* TODO yet TAB == SPC */
1465 if(((f
& a_MOVE_CURSOR
) || phy_nxtcur
!= phy_cur
) &&
1466 !n_termcap_cmd(n_TERMCAP_CMD_ch
, phy_cur
= phy_nxtcur
, 0))
1470 tlp
->tl_vi_flags
|= (f
& a_PERSIST_MASK
);
1471 tlp
->tl_lst_count
= tlp
->tl_count
;
1472 tlp
->tl_lst_cursor
= tlp
->tl_cursor
;
1473 tlp
->tl_phy_cursor
= phy_cur
;
1476 return ((f
& a_TRUE_RV
) != 0);
1483 a_tty_wboundary(struct a_tty_line
*tlp
, si32_t dir
){/* TODO shell token-wise */
1485 struct a_tty_cell
*tcap
;
1490 assert(dir
== 1 || dir
== -1);
1493 cnt
= tlp
->tl_count
;
1494 cur
= tlp
->tl_cursor
;
1499 }else if(cur
+ 1 >= cnt
)
1502 --cnt
, --cur
; /* xxx Unsigned wrapping may occur (twice), then */
1504 for(rv
= 0, tcap
= tlp
->tl_line
.cells
, anynon
= FAL0
;;){
1507 wc
= tcap
[cur
+= (ui32_t
)dir
].tc_wc
;
1508 if(iswblank(wc
) || iswpunct(wc
)){
1519 }else if(cur
+ 1 >= cnt
){
1530 a_tty_khome(struct a_tty_line
*tlp
, bool_t dobell
){
1534 if(LIKELY(tlp
->tl_cursor
> 0)){
1536 f
= a_TTY_VF_MOD_CURSOR
;
1542 tlp
->tl_vi_flags
|= f
;
1547 a_tty_kend(struct a_tty_line
*tlp
){
1551 if(LIKELY(tlp
->tl_cursor
< tlp
->tl_count
)){
1552 tlp
->tl_cursor
= tlp
->tl_count
;
1553 f
= a_TTY_VF_MOD_CURSOR
;
1557 tlp
->tl_vi_flags
|= f
;
1562 a_tty_kbs(struct a_tty_line
*tlp
){
1566 cur
= tlp
->tl_cursor
;
1567 cnt
= tlp
->tl_count
;
1569 if(LIKELY(cur
> 0)){
1570 tlp
->tl_cursor
= --cur
;
1571 tlp
->tl_count
= --cnt
;
1573 if((cnt
-= cur
) > 0){
1574 struct a_tty_cell
*tcap
;
1576 tcap
= tlp
->tl_line
.cells
+ cur
;
1577 memmove(tcap
, &tcap
[1], cnt
*= sizeof(*tcap
));
1579 f
= a_TTY_VF_MOD_CURSOR
| a_TTY_VF_MOD_CONTENT
;
1583 tlp
->tl_vi_flags
|= f
;
1588 a_tty_ksnarf(struct a_tty_line
*tlp
, bool_t cplline
, bool_t dobell
){
1595 if(cplline
&& i
> 0){
1596 tlp
->tl_cursor
= i
= 0;
1597 f
= a_TTY_VF_MOD_CURSOR
;
1600 if(LIKELY(i
< tlp
->tl_count
)){
1601 struct a_tty_cell
*tcap
;
1603 tcap
= &tlp
->tl_line
.cells
[0];
1604 a_tty_copy2paste(tlp
, &tcap
[i
], &tcap
[tlp
->tl_count
]);
1606 f
= a_TTY_VF_MOD_CONTENT
;
1610 tlp
->tl_vi_flags
|= f
;
1615 a_tty_kdel(struct a_tty_line
*tlp
){
1620 cur
= tlp
->tl_cursor
;
1621 cnt
= tlp
->tl_count
;
1622 i
= (si32_t
)(cnt
- cur
);
1625 tlp
->tl_count
= --cnt
;
1627 if(LIKELY(--i
> 0)){
1628 struct a_tty_cell
*tcap
;
1630 tcap
= &tlp
->tl_line
.cells
[cur
];
1631 memmove(tcap
, &tcap
[1], (ui32_t
)i
* sizeof(*tcap
));
1633 f
= a_TTY_VF_MOD_CONTENT
;
1634 }else if(cnt
== 0 && !ok_blook(ignoreeof
)){
1644 tlp
->tl_vi_flags
|= f
;
1650 a_tty_kleft(struct a_tty_line
*tlp
){
1654 if(LIKELY(tlp
->tl_cursor
> 0)){
1656 f
= a_TTY_VF_MOD_CURSOR
;
1660 tlp
->tl_vi_flags
|= f
;
1665 a_tty_kright(struct a_tty_line
*tlp
){
1669 if(LIKELY((i
= tlp
->tl_cursor
+ 1) <= tlp
->tl_count
)){
1671 i
= a_TTY_VF_MOD_CURSOR
;
1675 tlp
->tl_vi_flags
|= i
;
1680 a_tty_ksnarfw(struct a_tty_line
*tlp
, bool_t fwd
){
1681 struct a_tty_cell
*tcap
;
1686 if(UNLIKELY((i
= a_tty_wboundary(tlp
, (fwd
? +1 : -1))) <= 0)){
1687 f
= (i
< 0) ? a_TTY_VF_BELL
: a_TTY_VF_NONE
;
1691 cnt
= tlp
->tl_count
- (ui32_t
)i
;
1692 cur
= tlp
->tl_cursor
;
1695 tcap
= &tlp
->tl_line
.cells
[cur
];
1697 a_tty_copy2paste(tlp
, &tcap
[0], &tcap
[i
]);
1699 if((tlp
->tl_count
= cnt
) != (tlp
->tl_cursor
= cur
)){
1701 memmove(&tcap
[0], &tcap
[i
], cnt
* sizeof(*tcap
)); /* FIXME*/
1704 f
= a_TTY_VF_MOD_CURSOR
| a_TTY_VF_MOD_CONTENT
;
1706 tlp
->tl_vi_flags
|= f
;
1711 a_tty_kgow(struct a_tty_line
*tlp
, si32_t dir
){
1716 if(UNLIKELY((i
= a_tty_wboundary(tlp
, dir
)) <= 0))
1717 f
= (i
< 0) ? a_TTY_VF_BELL
: a_TTY_VF_NONE
;
1721 tlp
->tl_cursor
+= (ui32_t
)i
;
1722 f
= a_TTY_VF_MOD_CURSOR
;
1725 tlp
->tl_vi_flags
|= f
;
1730 a_tty_kother(struct a_tty_line
*tlp
, wchar_t wc
){
1731 /* Append if at EOL, insert otherwise;
1732 * since we may move around character-wise, always use a fresh ps */
1734 struct a_tty_cell tc
, *tcap
;
1742 n_LCTA(a_TTY_LINE_MAX
<= SI32_MAX
, "a_TTY_LINE_MAX too large");
1743 if(tlp
->tl_count
+ 1 >= a_TTY_LINE_MAX
){
1744 n_err(_("Stop here, we can't extend line beyond size limit\n"));
1748 /* First init a cell and see whether we'll really handle this wc */
1749 memset(&ps
, 0, sizeof ps
);
1753 l
= wcrtomb(tc
.tc_cbuf
, tc
.tc_wc
= wc
, &ps
);
1754 if(UNLIKELY(l
> MB_LEN_MAX
)){
1756 n_err(_("wcrtomb(3) error: too many multibyte character bytes\n"));
1759 tc
.tc_count
= (ui16_t
)l
;
1761 if(UNLIKELY((options
& OPT_ENC_MBSTATE
) != 0)){
1762 l
= wcrtomb(&tc
.tc_cbuf
[l
], L
'\0', &ps
);
1764 /* Only NUL terminator */;
1765 else if(LIKELY(--l
< MB_LEN_MAX
))
1766 tc
.tc_count
+= (ui16_t
)l
;
1772 /* Yes, we will! Place it in the array */
1773 tc
.tc_novis
= (iswprint(wc
) == 0);
1774 tc
.tc_width
= a_tty_wcwidth(wc
);
1775 /* TODO if(tc.tc_novis && tc.tc_width > 0) */
1777 cur
= tlp
->tl_cursor
++;
1778 cnt
= tlp
->tl_count
++ - cur
;
1779 tcap
= &tlp
->tl_line
.cells
[cur
];
1781 memmove(&tcap
[1], tcap
, cnt
* sizeof(*tcap
));
1782 f
= a_TTY_VF_MOD_CONTENT
;
1784 f
= a_TTY_VF_MOD_SINGLE
;
1785 memcpy(tcap
, &tc
, sizeof *tcap
);
1787 f
|= a_TTY_VF_MOD_CURSOR
;
1792 tlp
->tl_vi_flags
|= f
;
1798 a_tty_kht(struct a_tty_line
*tlp
){
1800 struct str orig
, bot
, topp
, sub
, exp
;
1801 struct n_string shou
, *shoup
;
1802 struct a_tty_cell
*cword
, *ctop
, *cx
;
1803 bool_t wedid
, set_savec
;
1808 shoup
= n_string_creat_auto(&shou
);
1810 /* Get plain line data; if this is the first expansion/xy, update the
1811 * very original content so that ^G gets the origin back */
1812 orig
= tlp
->tl_savec
;
1813 a_tty_cell2save(tlp
);
1814 exp
= tlp
->tl_savec
;
1816 /*tlp->tl_savec = orig;*/
1822 /* Find the word to be expanded */
1824 cword
= tlp
->tl_line
.cells
;
1825 ctop
= cword
+ tlp
->tl_cursor
;
1826 cx
= cword
+ tlp
->tl_count
;
1828 /* topp: separate data right of cursor */
1830 for(rv
= 0; ctop
< cx
; ++ctop
)
1831 rv
+= ctop
->tc_count
;
1833 topp
.s
= orig
.s
+ orig
.l
- rv
;
1834 ctop
= cword
+ tlp
->tl_cursor
;
1836 topp
.s
= NULL
, topp
.l
= 0;
1838 /* Find the shell token that corresponds to the cursor position */
1844 for(; cword
< ctop
; ++cword
)
1845 max
+= cword
->tc_count
;
1846 cword
= tlp
->tl_line
.cells
;
1854 enum n_shexp_state shs
;
1857 shs
= n_shell_parse_token(NULL
, &sub
, n_SHEXP_PARSE_DRYRUN
|
1858 n_SHEXP_PARSE_TRIMSPACE
| n_SHEXP_PARSE_IGNORE_EMPTY
);
1862 assert(max
>= sub
.l
);
1868 if(shs
& n_SHEXP_STATE_ERR_MASK
){
1869 n_err(_("Invalid completion pattern: %.*s\n"),
1873 n_shell_parse_token(shoup
, &exp
,
1874 n_SHEXP_PARSE_TRIMSPACE
| n_SHEXP_PARSE_IGNORE_EMPTY
);
1878 sub
.s
= n_string_cp(shoup
);
1879 sub
.l
= shoup
->s_len
;
1883 /* Leave room for "implicit asterisk" expansion, as below */
1886 sub
.s
= UNCONST("*");
1892 /* TODO Super-Heavy-Metal: block all sigs, avoid leaks on jump */
1894 exp
.s
= fexpand(sub
.s
, a_TTY_TAB_FEXP_FL
);
1897 if(exp
.s
== NULL
|| (exp
.l
= strlen(exp
.s
)) == 0)
1900 /* May be multi-return! */
1901 if(pstate
& PS_EXPAND_MULTIRESULT
)
1904 /* xxx That is not really true since the limit counts characters not bytes */
1905 n_LCTA(a_TTY_LINE_MAX
<= SI32_MAX
, "a_TTY_LINE_MAX too large");
1906 if(exp
.l
+ 1 >= a_TTY_LINE_MAX
){
1907 n_err(_("Tabulator expansion would extend beyond line size limit\n"));
1911 /* If the expansion equals the original string, assume the user wants what
1912 * is usually known as tab completion, append `*' and restart */
1913 if(!wedid
&& exp
.l
== sub
.l
&& !memcmp(exp
.s
, sub
.s
, exp
.l
)){
1914 if(sub
.s
[sub
.l
- 1] == '*')
1918 sub
.s
[sub
.l
++] = '*';
1919 sub
.s
[sub
.l
] = '\0';
1922 /* If it is a directory, and there is not yet a / appended, then we want the
1923 * user to confirm that he wants to dive in -- with only a HT */
1924 else if(wedid
&& exp
.l
== --sub
.l
&& !memcmp(exp
.s
, sub
.s
, exp
.l
) &&
1925 exp
.s
[exp
.l
- 1] != '/'){
1926 if(stat(exp
.s
, &sb
) || !S_ISDIR(sb
.st_mode
))
1928 sub
.s
= salloc(exp
.l
+ 1 +1);
1929 memcpy(sub
.s
, exp
.s
, exp
.l
);
1930 sub
.s
[exp
.l
++] = '/';
1931 sub
.s
[exp
.l
] = '\0';
1936 if(wedid
&& (wedid
= (exp
.s
[exp
.l
- 1] == '*')))
1938 exp
.s
[exp
.l
] = '\0';
1940 exp
.l
= strlen(exp
.s
= n_shell_quote_cp(exp
.s
, tlp
->tl_quote_rndtrip
));
1941 tlp
->tl_defc_cursor_byte
= bot
.l
+ exp
.l
-1;
1946 orig
.l
= bot
.l
+ exp
.l
+ topp
.l
;
1947 orig
.s
= salloc(orig
.l
+ 5 +1);
1948 if((rv
= (ui32_t
)bot
.l
) > 0)
1949 memcpy(orig
.s
, bot
.s
, rv
);
1950 memcpy(orig
.s
+ rv
, exp
.s
, exp
.l
);
1953 memcpy(orig
.s
+ rv
, topp
.s
, topp
.l
);
1958 tlp
->tl_defc
= orig
;
1959 tlp
->tl_count
= tlp
->tl_cursor
= 0;
1960 f
|= a_TTY_VF_MOD_DIRTY
;
1962 n_string_gut(shoup
);
1963 tlp
->tl_vi_flags
|= f
;
1968 struct n_visual_info_ctx vic
;
1973 size_t locolen
, scrwid
, lnlen
, lncnt
, prefixlen
;
1976 if((fp
= Ftmp(NULL
, "tabex", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
){
1977 n_perr(_("tmpfile"), 0);
1981 /* How long is the result string for real? Search the NUL NUL
1982 * terminator. While here, detect the longest entry to perform an
1983 * initial allocation of our accumulator string */
1988 i
= strlen(&exp
.s
[++exp
.l
]);
1989 locolen
= MAX(locolen
, i
);
1991 }while(exp
.s
[exp
.l
+ 1] != '\0');
1993 shoup
= n_string_reserve(n_string_trunc(shoup
, 0),
1994 locolen
+ (locolen
>> 1));
1996 /* Iterate (once again) over all results */
1997 scrwid
= (size_t)scrnwidth
- ((size_t)scrnwidth
>> 3);
1999 UNINIT(prefixlen
, 0);
2000 UNINIT(lococp
, NULL
);
2002 for(isfirst
= TRU1
; exp
.l
> 0; isfirst
= FAL0
, c1
= c2
){
2004 char const *fullpath
;
2008 sub
.l
= i
= strlen(sub
.s
);
2010 if((exp
.l
-= i
) > 0)
2014 /* Separate dirname and basename */
2019 if((cp
= strrchr(fullpath
, '/')) != NULL
)
2020 prefixlen
= PTR2SIZE(++cp
- fullpath
);
2024 if(prefixlen
> 0 && prefixlen
< sub
.l
){
2029 /* We want case-insensitive sort-order */
2030 memset(&vic
, 0, sizeof vic
);
2031 vic
.vic_indat
= sub
.s
;
2032 vic
.vic_inlen
= sub
.l
;
2033 c2
= n_visual_info(&vic
, n_VISUAL_INFO_ONE_CHAR
) ? vic
.vic_waccu
2035 #ifdef HAVE_C90AMEND1
2041 /* Query longest common prefix along the way */
2046 }else if(locolen
> 0){
2047 for(i
= 0; i
< locolen
; ++i
)
2048 if(lococp
[i
] != sub
.s
[i
]){
2049 i
= field_detect_clip(i
, lococp
, i
);
2055 /* Prepare display */
2057 shoup
= n_shell_quote(n_string_trunc(shoup
, 0), &input
,
2058 tlp
->tl_quote_rndtrip
);
2059 memset(&vic
, 0, sizeof vic
);
2060 vic
.vic_indat
= shoup
->s_dat
;
2061 vic
.vic_inlen
= shoup
->s_len
;
2062 if(!n_visual_info(&vic
,
2063 n_VISUAL_INFO_SKIP_ERRORS
| n_VISUAL_INFO_WIDTH_QUERY
))
2064 vic
.vic_vi_width
= shoup
->s_len
;
2066 /* Put on screen. Indent follow lines of same sort slot */
2069 scrwid
< lnlen
|| scrwid
- lnlen
<= vic
.vic_vi_width
+ 2){
2076 }else if(lnlen
> 0){
2081 fputs(n_string_cp(shoup
), fp
);
2082 lnlen
+= vic
.vic_vi_width
;
2084 /* Support the known file name tagging
2085 * XXX *line-editor-completion-filetype* or so */
2086 if(!lstat(fullpath
, &sb
)){
2089 if(S_ISDIR(sb
.st_mode
))
2091 else if(S_ISLNK(sb
.st_mode
))
2094 else if(S_ISFIFO(sb
.st_mode
))
2098 else if(S_ISSOCK(sb
.st_mode
))
2102 else if(S_ISCHR(sb
.st_mode
))
2106 else if(S_ISBLK(sb
.st_mode
))
2119 page_or_print(fp
, lncnt
);
2123 n_string_gut(shoup
);
2125 /* A common prefix of 0 means we cannot provide the user any auto
2126 * completed characters */
2130 /* Otherwise we can, so extend the visual line content by the common
2131 * prefix (in a reversible way) */
2132 (exp
.s
= UNCONST(lococp
))[locolen
] = '\0';
2134 exp
.l
= (locolen
+= prefixlen
);
2136 /* XXX Indicate that there is multiple choice */
2137 /* XXX f |= a_TTY_VF_BELL; -> *line-editor-completion-bell*? or so */
2143 /* If we've provided a default content, but failed to expand, there is
2144 * nothing we can "revert to": drop that default again */
2146 tlp
->tl_savec
.s
= NULL
;
2147 tlp
->tl_savec
.l
= 0;
2154 # ifdef HAVE_HISTORY
2156 a_tty__khist_shared(struct a_tty_line
*tlp
, struct a_tty_hist
*thp
){
2160 if(LIKELY((tlp
->tl_hist
= thp
) != NULL
)){
2161 tlp
->tl_defc
.s
= savestrbuf(thp
->th_dat
, thp
->th_len
);
2162 rv
= tlp
->tl_defc
.l
= thp
->th_len
;
2163 f
= (tlp
->tl_count
> 0) ? a_TTY_VF_MOD_DIRTY
: a_TTY_VF_NONE
;
2164 tlp
->tl_count
= tlp
->tl_cursor
= 0;
2170 tlp
->tl_vi_flags
|= f
;
2176 a_tty_khist(struct a_tty_line
*tlp
, bool_t fwd
){
2177 struct a_tty_hist
*thp
;
2181 /* If we're not in history mode yet, save line content;
2182 * also, disallow forward search, then, and, of course, bail unless we
2183 * do have any history at all */
2184 if((thp
= tlp
->tl_hist
) == NULL
){
2187 if((thp
= a_tty
.tg_hist
) == NULL
)
2189 a_tty_cell2save(tlp
);
2193 thp
= fwd
? thp
->th_younger
: thp
->th_older
;
2195 rv
= a_tty__khist_shared(tlp
, thp
);
2201 a_tty_khist_search(struct a_tty_line
*tlp
, bool_t fwd
){
2202 struct str orig_savec
;
2203 struct a_tty_hist
*thp
;
2209 /* We cannot complete an empty line */
2210 if(UNLIKELY(tlp
->tl_count
== 0)){
2211 /* XXX The upcoming hard reset would restore a set savec buffer,
2212 * XXX so forcefully reset that. A cleaner solution would be to
2213 * XXX reset it whenever a restore is no longer desired */
2214 tlp
->tl_savec
.s
= NULL
;
2215 tlp
->tl_savec
.l
= 0;
2219 if((thp
= tlp
->tl_hist
) == NULL
){
2220 if((thp
= a_tty
.tg_hist
) == NULL
)
2222 /* We don't support wraparound, searching forward must always step */
2224 thp
= thp
->th_younger
;
2225 orig_savec
.s
= NULL
;
2226 orig_savec
.l
= 0; /* silence CC */
2227 }else if((thp
= (fwd
? thp
->th_younger
: thp
->th_older
)) == NULL
)
2230 orig_savec
= tlp
->tl_savec
;
2232 if(orig_savec
.s
== NULL
)
2233 a_tty_cell2save(tlp
);
2235 for(; thp
!= NULL
; thp
= (fwd
? thp
->th_younger
: thp
->th_older
))
2236 if(is_prefix(tlp
->tl_savec
.s
, thp
->th_dat
))
2239 if(orig_savec
.s
!= NULL
)
2240 tlp
->tl_savec
= orig_savec
;
2242 rv
= a_tty__khist_shared(tlp
, thp
);
2246 # endif /* HAVE_HISTORY */
2248 static enum a_tty_fun_status
2249 a_tty_fun(struct a_tty_line
*tlp
, enum a_tty_bind_flags tbf
, size_t *len
){
2250 enum a_tty_fun_status rv
;
2253 rv
= a_TTY_FUN_STATUS_OK
;
2255 # define a_X(N) a_TTY_BIND_FUN_REDUCE(a_TTY_BIND_FUN_ ## N)
2256 switch(a_TTY_BIND_FUN_REDUCE(tbf
)){
2258 tlp
->tl_vi_flags
|= a_TTY_VF_BELL
;
2266 case a_X(GO_WORD_BWD
):
2267 a_tty_kgow(tlp
, -1);
2269 case a_X(GO_WORD_FWD
):
2270 a_tty_kgow(tlp
, +1);
2273 a_tty_khome(tlp
, TRU1
);
2282 if(a_tty_kdel(tlp
) < 0)
2283 rv
= a_TTY_FUN_STATUS_END
;
2285 case a_X(SNARF_WORD_BWD
):
2286 a_tty_ksnarfw(tlp
, FAL0
);
2288 case a_X(SNARF_WORD_FWD
):
2289 a_tty_ksnarfw(tlp
, TRU1
);
2291 case a_X(SNARF_END
):
2292 a_tty_ksnarf(tlp
, FAL0
, TRU1
);
2294 case a_X(SNARF_LINE
):
2295 a_tty_ksnarf(tlp
, TRU1
, (tlp
->tl_count
== 0));
2299 # ifdef HAVE_HISTORY
2300 if(tlp
->tl_hist
!= NULL
){
2301 bool_t isfwd
= TRU1
;
2307 # ifdef HAVE_HISTORY
2310 if((*len
= a_tty_khist(tlp
, isfwd
)) > 0){
2311 rv
= a_TTY_FUN_STATUS_RESTART
;
2317 tlp
->tl_vi_flags
|= a_TTY_VF_BELL
;
2320 case a_X(HIST_SRCH_FWD
):{
2321 # ifdef HAVE_HISTORY
2322 bool_t isfwd
= TRU1
;
2327 case a_X(HIST_SRCH_BWD
):
2328 # ifdef HAVE_HISTORY
2331 if((*len
= a_tty_khist_search(tlp
, isfwd
)) > 0){
2332 rv
= a_TTY_FUN_STATUS_RESTART
;
2337 tlp
->tl_vi_flags
|= a_TTY_VF_BELL
;
2342 tlp
->tl_vi_flags
|= a_TTY_VF_MOD_DIRTY
;
2344 case a_X(QUOTE_RNDTRIP
):
2345 tlp
->tl_quote_rndtrip
= !tlp
->tl_quote_rndtrip
;
2347 case a_X(PROMPT_CHAR
):{
2350 if((wc
= a_tty_vinuni(tlp
)) > 0)
2351 a_tty_kother(tlp
, wc
);
2354 if((*len
= a_tty_kht(tlp
)) > 0)
2355 rv
= a_TTY_FUN_STATUS_RESTART
;
2359 if(tlp
->tl_pastebuf
.l
> 0)
2360 *len
= (tlp
->tl_defc
= tlp
->tl_pastebuf
).l
;
2362 tlp
->tl_vi_flags
|= a_TTY_VF_BELL
;
2367 /* Normally this just causes a restart and thus resets the state
2369 if(tlp
->tl_savec
.l
== 0 && tlp
->tl_defc
.l
== 0){
2371 # ifdef HAVE_KEY_BINDINGS
2372 tlp
->tl_bind_takeover
= '\0';
2374 tlp
->tl_vi_flags
|= a_TTY_VF_BELL
;
2375 rv
= a_TTY_FUN_STATUS_RESTART
;
2379 if(tlp
->tl_count
== 0 && tlp
->tl_savec
.l
== 0 && tlp
->tl_defc
.l
== 0){
2380 # ifdef HAVE_KEY_BINDINGS
2381 tlp
->tl_bind_takeover
= '\0';
2383 tlp
->tl_vi_flags
|= a_TTY_VF_MOD_DIRTY
| a_TTY_VF_BELL
;
2386 case a_X(FULLRESET
):
2387 tlp
->tl_savec
.s
= tlp
->tl_defc
.s
= NULL
;
2388 tlp
->tl_savec
.l
= tlp
->tl_defc
.l
= 0;
2389 tlp
->tl_defc_cursor_byte
= 0;
2390 tlp
->tl_vi_flags
|= a_TTY_VF_BELL
;
2393 # ifdef HAVE_KEY_BINDINGS
2394 tlp
->tl_bind_takeover
= '\0';
2396 tlp
->tl_vi_flags
|= a_TTY_VF_MOD_DIRTY
;
2397 tlp
->tl_cursor
= tlp
->tl_count
= 0;
2398 # ifdef HAVE_HISTORY
2399 tlp
->tl_hist
= NULL
;
2401 if((*len
= tlp
->tl_savec
.l
) != 0){
2402 tlp
->tl_defc
= tlp
->tl_savec
;
2403 tlp
->tl_savec
.s
= NULL
;
2404 tlp
->tl_savec
.l
= 0;
2406 *len
= tlp
->tl_defc
.l
;
2407 rv
= a_TTY_FUN_STATUS_RESTART
;
2412 rv
= a_TTY_FUN_STATUS_COMMIT
;
2422 a_tty_readline(struct a_tty_line
*tlp
, size_t len SMALLOC_DEBUG_ARGS
){
2423 /* We want to save code, yet we may have to incorporate a lines'
2424 * default content and / or default input to switch back to after some
2425 * history movement; let "len > 0" mean "have to display some data
2426 * buffer" -> a_BUFMODE, and only otherwise read(2) it */
2428 char cbuf_base
[MB_LEN_MAX
* 2], *cbuf
, *cbufp
;
2430 struct a_tty_bind_tree
*tbtp
;
2432 enum a_tty_bind_flags tbf
;
2433 enum {a_NONE
, a_WAS_HERE
= 1<<0, a_BUFMODE
= 1<<1, a_MAYBEFUN
= 1<<2,
2434 a_TIMEOUT
= 1<<3, a_TIMEOUT_EXPIRED
= 1<<4,
2435 a_TIMEOUT_MASK
= a_TIMEOUT
| a_TIMEOUT_EXPIRED
,
2436 a_READ_LOOP_MASK
= ~(a_WAS_HERE
| a_MAYBEFUN
| a_TIMEOUT_MASK
)
2441 # ifdef HAVE_KEY_BINDINGS
2442 assert(tlp
->tl_bind_takeover
== '\0');
2445 memset(ps
, 0, sizeof ps
);
2448 tlp
->tl_vi_flags
|= a_TTY_VF_REFRESH
| a_TTY_VF_SYNC
;
2455 /* Ensure we have valid pointers, and room for grow */
2456 a_tty_check_grow(tlp
, ((flags
& a_BUFMODE
) ? (ui32_t
)len
: 1)
2457 SMALLOC_DEBUG_ARGSCALL
);
2459 /* Handle visual state flags, except in buffer mode */
2460 if(!(flags
& a_BUFMODE
) && (tlp
->tl_vi_flags
& a_TTY_VF_ALL_MASK
))
2461 if(!a_tty_vi_refresh(tlp
)){
2466 /* Ready for messing around.
2467 * Normal read(2)? Else buffer mode: speed this one up */
2468 if(!(flags
& a_BUFMODE
)){
2472 assert(tlp
->tl_defc
.l
> 0 && tlp
->tl_defc
.s
!= NULL
);
2473 assert(tlp
->tl_defc
.l
>= len
);
2475 cbuf
= tlp
->tl_defc
.s
+ (tlp
->tl_defc
.l
- len
);
2479 /* Read in the next complete multibyte character */
2481 # ifdef HAVE_KEY_BINDINGS
2482 struct a_tty_bind_tree
*xtbtp
;
2486 struct a_tty_bind_tree
*tbtp
;
2489 isp_head
= isp
= NULL
;
2492 for(flags
&= a_READ_LOOP_MASK
;;){
2493 # ifdef HAVE_KEY_BINDINGS
2494 if(!(flags
& a_BUFMODE
) && tlp
->tl_bind_takeover
!= '\0'){
2495 wc
= tlp
->tl_bind_takeover
;
2496 tlp
->tl_bind_takeover
= '\0';
2500 if(!(flags
& a_BUFMODE
)){
2501 /* Let me at least once dream of iomon(itor), timer with
2502 * one-shot, enwrapped with key_event and key_sequence_event,
2503 * all driven by an event_loop */
2504 /* TODO v15 Until we have SysV signal handling all through we
2505 * TODO need to temporarily adjust our BSD signal handler with
2506 * TODO a SysV one, here */
2507 n_sighdl_t otstp
, ottin
, ottou
;
2509 otstp
= n_signal(SIGTSTP
, &n_tty_signal
);
2510 ottin
= n_signal(SIGTTIN
, &n_tty_signal
);
2511 ottou
= n_signal(SIGTTOU
, &n_tty_signal
);
2512 # ifdef HAVE_KEY_BINDINGS
2513 flags
&= ~a_TIMEOUT_MASK
;
2514 if(isp
!= NULL
&& (tbtp
= isp
->tbtp
)->tbt_isseq
&&
2515 !tbtp
->tbt_isseq_trail
){
2516 a_tty_term_rawmode_timeout(tlp
, TRU1
);
2521 while((rv
= read(STDIN_FILENO
, cbufp
, 1)) < 1){
2524 if((tlp
->tl_vi_flags
& a_TTY_VF_MOD_DIRTY
) &&
2525 !a_tty_vi_refresh(tlp
))
2532 # ifdef HAVE_KEY_BINDINGS
2533 /* Timeout expiration */
2535 assert(flags
& a_TIMEOUT
);
2536 assert(isp
!= NULL
);
2537 a_tty_term_rawmode_timeout(tlp
, FAL0
);
2539 /* Something "atomic" broke. Maybe the current one can
2540 * also be terminated already, by itself? xxx really? */
2541 if((tbtp
= isp
->tbtp
)->tbt_bind
!= NULL
){
2542 tlp
->tl_bind_takeover
= wc
;
2546 /* Or, maybe there is a second path without a timeout;
2547 * this should be covered by .tbt_isseq_trail, but then
2548 * again a single-layer implementation cannot "know" */
2549 for(xtbtp
= tbtp
; (xtbtp
= xtbtp
->tbt_sibling
) != NULL
;)
2550 if(xtbtp
->tbt_char
== tbtp
->tbt_char
){
2551 assert(!xtbtp
->tbt_isseq
);
2554 /* Lay down on read(2)? */
2559 # endif /* HAVE_KEY_BINDINGS */
2562 # ifdef HAVE_KEY_BINDINGS
2563 if(flags
& a_TIMEOUT
)
2564 a_tty_term_rawmode_timeout(tlp
, FAL0
);
2566 safe_signal(SIGTSTP
, otstp
);
2567 safe_signal(SIGTTIN
, ottin
);
2568 safe_signal(SIGTTOU
, ottou
);
2575 rv
= (ssize_t
)mbrtowc(&wc
, cbuf
, PTR2SIZE(cbufp
- cbuf
), &ps
[0]);
2577 /* Any error during buffer mode can only result in a hard
2578 * reset; Otherwise, if it's a hard error, or if too many
2579 * redundant shift sequences overflow our buffer: perform
2581 if((flags
& a_BUFMODE
) || rv
== -1 ||
2582 sizeof cbuf_base
== PTR2SIZE(cbufp
- cbuf
)){
2583 a_tty_fun(tlp
, a_TTY_BIND_FUN_FULLRESET
, &len
);
2586 /* Otherwise, due to the way we deal with the buffer, we need
2587 * to restore the mbstate_t from before this conversion */
2595 /* Normal read(2)ing is subject to detection of key-bindings */
2596 # ifdef HAVE_KEY_BINDINGS
2597 if(!(flags
& a_BUFMODE
)){
2598 /* Check for special bypass functions before we try to embed
2599 * this character into the tree */
2600 if(n_uasciichar(wc
)){
2604 for(c
= (char)wc
, cp
= &(*tlp
->tl_bind_shcut_prompt_char
)[0];
2607 wc
= a_tty_vinuni(tlp
);
2612 tlp
->tl_vi_flags
|= a_TTY_VF_BELL
;
2616 if(n_uasciichar(wc
))
2617 flags
|= a_MAYBEFUN
;
2619 flags
&= ~a_MAYBEFUN
;
2621 /* Search for this character in the bind tree */
2622 tbtp
= (isp
!= NULL
) ? isp
->tbtp
->tbt_childs
2623 : (*tlp
->tl_bind_tree_hmap
)[wc
% HSHSIZE
];
2624 for(; tbtp
!= NULL
; tbtp
= tbtp
->tbt_sibling
){
2625 if(tbtp
->tbt_char
== wc
){
2628 /* If this one cannot continue we're likely finished! */
2629 if(tbtp
->tbt_childs
== NULL
){
2630 assert(tbtp
->tbt_bind
!= NULL
);
2631 tbf
= tbtp
->tbt_bind
->tbc_flags
;
2635 /* This needs to read more characters */
2636 nisp
= salloc(sizeof *nisp
);
2637 if((nisp
->last
= isp
) == NULL
)
2644 flags
&= ~a_WAS_HERE
;
2651 /* Was there a binding active, but couldn't be continued? */
2653 /* A binding had a timeout, it didn't expire, but we saw
2654 * something non-expected. Something "atomic" broke.
2655 * Maybe there is a second path without a timeout, that
2656 * continues like we've seen it. I.e., it may just have been
2657 * the user, typing two fast. We definitely want to allow
2658 * bindings like \e,d etc. to succeed: users are so used to
2659 * them that a timeout cannot be the mechanism to catch up!
2660 * A single-layer implementation cannot "know" */
2661 if((tbtp
= isp
->tbtp
)->tbt_isseq
&& (isp
->last
== NULL
||
2662 !(xtbtp
= isp
->last
->tbtp
)->tbt_isseq
||
2663 xtbtp
->tbt_isseq_trail
)){
2664 for(xtbtp
= (tbtp
= isp
->tbtp
);
2665 (xtbtp
= xtbtp
->tbt_sibling
) != NULL
;)
2666 if(xtbtp
->tbt_char
== tbtp
->tbt_char
){
2667 assert(!xtbtp
->tbt_isseq
);
2672 tlp
->tl_bind_takeover
= wc
;
2677 /* Check for CANCEL shortcut now */
2678 if(flags
& a_MAYBEFUN
){
2682 for(c
= (char)wc
, cp
= &(*tlp
->tl_bind_shcut_cancel
)[0];
2685 tbf
= a_TTY_BIND_FUN_INTERNAL
|a_TTY_BIND_FUN_CANCEL
;
2690 /* So: maybe the current sequence can be terminated here? */
2691 if((tbtp
= isp
->tbtp
)->tbt_bind
!= NULL
){
2693 tbf
= tbtp
->tbt_bind
->tbc_flags
;
2695 if(tbf
& a_TTY_BIND_FUN_INTERNAL
){
2696 switch(a_tty_fun(tlp
, tbf
, &len
)){
2697 case a_TTY_FUN_STATUS_OK
:
2699 case a_TTY_FUN_STATUS_COMMIT
:
2701 case a_TTY_FUN_STATUS_RESTART
:
2703 case a_TTY_FUN_STATUS_END
:
2707 }else if(tbtp
->tbt_bind
->tbc_flags
& a_TTY_BIND_NOCOMMIT
){
2708 struct a_tty_bind_ctx
*tbcp
;
2710 tbcp
= tbtp
->tbt_bind
;
2711 memcpy(tlp
->tl_defc
.s
= salloc(
2712 (tlp
->tl_defc
.l
= len
= tbcp
->tbc_exp_len
) +1),
2713 tbcp
->tbc_exp
, tbcp
->tbc_exp_len
+1);
2716 tlp
->tl_reenter_after_cmd
= tbtp
->tbt_bind
->tbc_exp
;
2722 /* Otherwise take over all chars "as is" */
2724 for(; isp_head
!= NULL
; isp_head
= isp_head
->next
)
2725 if(a_tty_kother(tlp
, isp_head
->tbtp
->tbt_char
)){
2728 /* And the current one too */
2731 # endif /* HAVE_KEY_BINDINGS */
2733 if((flags
& a_BUFMODE
) && (len
-= (size_t)rv
) == 0){
2734 /* Buffer mode completed */
2735 tlp
->tl_defc
.s
= NULL
;
2737 flags
&= ~a_BUFMODE
;
2742 # ifndef HAVE_KEY_BINDINGS
2743 /* Don't interpret control bytes during buffer mode.
2744 * Otherwise, if it's a control byte check whether it is a MLE
2745 * function. Remarks: initially a complete duplicate to be able to
2746 * switch(), later converted to simply iterate over (an #ifdef'd
2747 * subset of) the MLE default_tuple table in order to have "a SPOF" */
2748 if(cbuf
== cbuf_base
&& n_uasciichar(wc
) && cntrlchar((char)wc
)){
2749 struct a_tty_bind_default_tuple
const *tbdtp
;
2752 for(c
= (char)wc
^ 0x40, tbdtp
= a_tty_bind_default_tuples
;
2753 PTRCMP(tbdtp
, <, &a_tty_bind_default_tuples
[
2754 NELEM(a_tty_bind_default_tuples
)]);
2756 /* Assert default_tuple table is properly subset'ed */
2757 assert(tbdtp
->tbdt_iskey
);
2758 if(tbdtp
->tbdt_ckey
== c
){
2759 if(tbdtp
->tbdt_exp
[0] == '\0'){
2760 enum a_tty_bind_flags tbf
;
2762 tbf
= a_TTY_BIND_FUN_EXPAND((ui8_t
)tbdtp
->tbdt_exp
[1]);
2763 switch(a_tty_fun(tlp
, tbf
, &len
)){
2764 case a_TTY_FUN_STATUS_OK
:
2766 case a_TTY_FUN_STATUS_COMMIT
:
2768 case a_TTY_FUN_STATUS_RESTART
:
2770 case a_TTY_FUN_STATUS_END
:
2775 tlp
->tl_reenter_after_cmd
= tbdtp
->tbdt_exp
;
2781 # endif /* !HAVE_KEY_BINDINGS */
2783 # ifdef HAVE_KEY_BINDINGS
2786 if(a_tty_kother(tlp
, wc
)){
2787 /* Don't clear the history during buffer mode.. */
2788 # ifdef HAVE_HISTORY
2789 if(!(flags
& a_BUFMODE
) && cbuf
== cbuf_base
)
2790 tlp
->tl_hist
= NULL
;
2796 /* We have a completed input line, convert the struct cell data to its
2797 * plain character equivalent */
2799 rv
= a_tty_cell2dat(tlp
);
2807 # ifdef HAVE_KEY_BINDINGS
2808 static enum n_lexinput_flags
2809 a_tty_bind_ctx_find(char const *name
){
2810 enum n_lexinput_flags rv
;
2811 struct a_tty_bind_ctx_map
const *tbcmp
;
2814 tbcmp
= a_tty_bind_ctx_maps
;
2815 do if(!asccasecmp(tbcmp
->tbcm_name
, name
)){
2816 rv
= tbcmp
->tbcm_ctx
;
2818 }while(PTRCMP(++tbcmp
, <, &a_tty_bind_ctx_maps
[NELEM(a_tty_bind_ctx_maps
)]));
2820 rv
= (enum n_lexinput_flags
)-1;
2827 a_tty_bind_create(struct a_tty_bind_parse_ctx
*tbpcp
, bool_t replace
){
2828 struct a_tty_bind_ctx
*tbcp
;
2834 if(!a_tty_bind_parse(TRU1
, tbpcp
))
2837 /* Since we use a single buffer for it all, need to replace as such */
2838 if(tbpcp
->tbpc_tbcp
!= NULL
){
2841 a_tty_bind_del(tbpcp
);
2842 }else if(a_tty
.tg_bind_cnt
== UI32_MAX
){
2843 n_err(_("`bind': maximum number of bindings already established\n"));
2850 tbcp
= smalloc(sizeof(*tbcp
) -
2851 VFIELD_SIZEOF(struct a_tty_bind_ctx
, tbc__buf
) +
2852 tbpcp
->tbpc_seq_len
+ tbpcp
->tbpc_exp
.l
+ tbpcp
->tbpc_cnv_len
+3);
2853 if(tbpcp
->tbpc_ltbcp
!= NULL
){
2854 tbcp
->tbc_next
= tbpcp
->tbpc_ltbcp
->tbc_next
;
2855 tbpcp
->tbpc_ltbcp
->tbc_next
= tbcp
;
2857 enum n_lexinput_flags lif
= tbpcp
->tbpc_flags
& n__LEXINPUT_CTX_MASK
;
2859 tbcp
->tbc_next
= a_tty
.tg_bind
[lif
];
2860 a_tty
.tg_bind
[lif
] = tbcp
;
2862 memcpy(tbcp
->tbc_seq
= &tbcp
->tbc__buf
[0],
2863 tbpcp
->tbpc_seq
, i
= (tbcp
->tbc_seq_len
= tbpcp
->tbpc_seq_len
) +1);
2864 memcpy(tbcp
->tbc_exp
= &tbcp
->tbc__buf
[i
],
2865 tbpcp
->tbpc_exp
.s
, j
= (tbcp
->tbc_exp_len
= tbpcp
->tbpc_exp
.l
) +1);
2866 memcpy(tbcp
->tbc_cnv
= &tbcp
->tbc__buf
[i
+= j
],
2867 tbpcp
->tbpc_cnv
, (tbcp
->tbc_cnv_len
= tbpcp
->tbpc_cnv_len
) +1);
2868 tbcp
->tbc_flags
= tbpcp
->tbpc_flags
;
2871 /* Directly resolve any termcap(5) symbol if we are already setup */
2872 if((pstate
& PS_STARTED
) &&
2873 (tbcp
->tbc_flags
& (a_TTY_BIND_RESOLVE
| a_TTY_BIND_DEFUNCT
)) ==
2875 a_tty_bind_resolve(tbcp
);
2877 ++a_tty
.tg_bind_cnt
;
2878 /* If this binding is usable invalidate the key input lookup trees */
2879 if(!(tbcp
->tbc_flags
& a_TTY_BIND_DEFUNCT
))
2880 a_tty
.tg_bind_isdirty
= TRU1
;
2888 a_tty_bind_parse(bool_t isbindcmd
, struct a_tty_bind_parse_ctx
*tbpcp
){
2889 enum{a_TRUE_RV
= a_TTY__BIND_LAST
<<1};
2891 struct n_visual_info_ctx vic
;
2892 struct str shin_save
, shin
;
2893 struct n_string shou
, *shoup
;
2900 ui32_t cnv_len
; /* High bit set if a termap to be resolved */
2901 ui32_t calc_cnv_len
; /* Ditto, but aligned etc. */
2905 n_LCTA(UICMP(64, a_TRUE_RV
, <, UI32_MAX
),
2906 "Flag bits excess storage datatype");
2908 f
= n_LEXINPUT_NONE
;
2909 shoup
= n_string_creat_auto(&shou
);
2912 /* Parse the key-sequence */
2913 for(shin
.s
= UNCONST(tbpcp
->tbpc_in_seq
), shin
.l
= UIZ_MAX
;;){
2915 enum n_shexp_state shs
;
2918 shs
= n_shell_parse_token(shoup
, &shin
,
2919 n_SHEXP_PARSE_TRUNC
| n_SHEXP_PARSE_TRIMSPACE
|
2920 n_SHEXP_PARSE_IGNORE_EMPTY
| n_SHEXP_PARSE_IFS_IS_COMMA
);
2921 if(shs
& n_SHEXP_STATE_ERR_UNICODE
){
2922 f
|= a_TTY_BIND_DEFUNCT
;
2923 if(isbindcmd
&& (options
& OPT_D_V
))
2924 n_err(_("`%s': \\uNICODE not available in locale: %s\n"),
2925 tbpcp
->tbpc_cmd
, tbpcp
->tbpc_in_seq
);
2927 if((shs
& n_SHEXP_STATE_ERR_MASK
) & ~n_SHEXP_STATE_ERR_UNICODE
){
2928 n_err(_("`%s': failed to parse key-sequence: %s\n"),
2929 tbpcp
->tbpc_cmd
, tbpcp
->tbpc_in_seq
);
2932 if((shs
& (n_SHEXP_STATE_OUTPUT
| n_SHEXP_STATE_STOP
)) ==
2936 ep
= salloc(sizeof *ep
);
2943 if(!(shs
& n_SHEXP_STATE_ERR_UNICODE
)){
2944 i
= strlen(ep
->seq_dat
= n_shell_quote_cp(n_string_cp(shoup
), TRU1
));
2945 if(i
>= SI32_MAX
- 1)
2947 ep
->seq_len
= (ui32_t
)i
;
2949 /* Otherwise use the original buffer, _we_ can only quote it the wrong
2950 * way (e.g., an initial $'\u3a' becomes '\u3a'), _then_ */
2951 if((i
= shin_save
.l
- shin
.l
) >= SI32_MAX
- 1)
2953 ep
->seq_len
= (ui32_t
)i
;
2954 ep
->seq_dat
= savestrbuf(shin_save
.s
, i
);
2957 memset(&vic
, 0, sizeof vic
);
2958 vic
.vic_inlen
= shoup
->s_len
;
2959 vic
.vic_indat
= shoup
->s_dat
;
2960 if(!n_visual_info(&vic
,
2961 n_VISUAL_INFO_WOUT_CREATE
| n_VISUAL_INFO_WOUT_SALLOC
)){
2962 n_err(_("`%s': key-sequence seems to contain invalid "
2963 "characters: %s: %s\n"),
2964 tbpcp
->tbpc_cmd
, n_string_cp(shoup
), tbpcp
->tbpc_in_seq
);
2965 f
|= a_TTY_BIND_DEFUNCT
;
2967 }else if(vic
.vic_woulen
== 0 ||
2968 vic
.vic_woulen
>= (SI32_MAX
- 2) / sizeof(wc_t
)){
2970 n_err(_("`%s': length of key-sequence unsupported: %s: %s\n"),
2971 tbpcp
->tbpc_cmd
, n_string_cp(shoup
), tbpcp
->tbpc_in_seq
);
2972 f
|= a_TTY_BIND_DEFUNCT
;
2975 ep
->cnv_dat
= vic
.vic_woudat
;
2976 ep
->cnv_len
= (ui32_t
)vic
.vic_woulen
;
2978 /* A termcap(5)/terminfo(5) identifier? */
2979 if(ep
->cnv_len
> 1 && ep
->cnv_dat
[0] == ':'){
2980 i
= --ep
->cnv_len
, ++ep
->cnv_dat
;
2981 # ifndef HAVE_TERMCAP
2982 if(options
& OPT_D_V
)
2983 n_err(_("`%s': no termcap(5)/terminfo(5) support: %s: %s\n"),
2984 tbpcp
->tbpc_cmd
, ep
->seq_dat
, tbpcp
->tbpc_in_seq
);
2985 f
|= a_TTY_BIND_DEFUNCT
;
2987 if(i
> a_TTY_BIND_CAPNAME_MAX
){
2988 n_err(_("`%s': termcap(5)/terminfo(5) name too long: %s: %s\n"),
2989 tbpcp
->tbpc_cmd
, ep
->seq_dat
, tbpcp
->tbpc_in_seq
);
2990 f
|= a_TTY_BIND_DEFUNCT
;
2993 /* (We store it as char[]) */
2994 if((ui32_t
)ep
->cnv_dat
[--i
] & ~0x7Fu
){
2995 n_err(_("`%s': invalid termcap(5)/terminfo(5) name content: "
2997 tbpcp
->tbpc_cmd
, ep
->seq_dat
, tbpcp
->tbpc_in_seq
);
2998 f
|= a_TTY_BIND_DEFUNCT
;
3001 ep
->cnv_len
|= SI32_MIN
; /* Needs resolve */
3002 f
|= a_TTY_BIND_RESOLVE
;
3005 if(shs
& n_SHEXP_STATE_STOP
)
3011 n_err(_("`%s': effectively empty key-sequence: %s\n"),
3012 tbpcp
->tbpc_cmd
, tbpcp
->tbpc_in_seq
);
3017 struct a_tty_bind_ctx
*ltbcp
, *tbcp
;
3018 char *cpbase
, *cp
, *cnv
;
3021 /* Unite the parsed sequence(s) into single string representations */
3022 for(sl
= cl
= 0, tail
= head
; tail
!= NULL
; tail
= tail
->next
){
3023 sl
+= tail
->seq_len
+ 1;
3028 /* Preserve room for terminal capabilities to be resolved.
3029 * Above we have ensured the buffer will fit in these calculations */
3030 if((i
= tail
->cnv_len
) & SI32_MIN
){
3032 * struct{si32_t buf_len_iscap; si32_t cap_len; wc_t name[]+NUL;}
3034 * struct{si32_t buf_len_iscap; si32_t cap_len; char buf[]+NUL;} */
3035 n_LCTAV(ISPOW2(a_TTY_BIND_CAPEXP_ROUNDUP
));
3036 n_LCTA(a_TTY_BIND_CAPEXP_ROUNDUP
>= sizeof(wc_t
),
3037 "Aligning on this constant doesn't properly align wc_t");
3040 i
+= sizeof(si32_t
);
3041 if(i
< a_TTY_BIND_CAPEXP_ROUNDUP
)
3042 i
= (i
+ (a_TTY_BIND_CAPEXP_ROUNDUP
- 1)) &
3043 ~(a_TTY_BIND_CAPEXP_ROUNDUP
- 1);
3045 /* struct{si32_t buf_len_iscap; wc_t buf[]+NUL;} */
3047 i
+= sizeof(si32_t
) + sizeof(wc_t
); /* (buf_len_iscap, NUL) */
3049 if(tail
->cnv_len
& SI32_MIN
){
3050 tail
->cnv_len
&= SI32_MAX
;
3053 tail
->calc_cnv_len
= (ui32_t
)i
;
3057 tbpcp
->tbpc_seq_len
= sl
;
3058 tbpcp
->tbpc_cnv_len
= cl
;
3062 j
= i
= sl
+ 1; /* Room for comma separator */
3064 size_t const al
= MAX(sizeof(si32_t
), sizeof(wc_t
)) - 1;
3070 tbpcp
->tbpc_seq
= cp
= cpbase
= salloc(i
);
3071 tbpcp
->tbpc_cnv
= cnv
= &cpbase
[j
];
3074 for(tail
= head
; tail
!= NULL
; tail
= tail
->next
){
3075 memcpy(cp
, tail
->seq_dat
, tail
->seq_len
);
3076 cp
+= tail
->seq_len
;
3080 char * const save_cnv
= cnv
;
3082 ((si32_t
*)cnv
)[0] = (si32_t
)(i
= tail
->calc_cnv_len
);
3083 cnv
+= sizeof(si32_t
);
3086 * struct{si32_t buf_len_iscap; si32_t cap_len; wc_t name[];}
3088 * struct{si32_t buf_len_iscap; si32_t cap_len; char buf[];} */
3089 ((si32_t
*)cnv
)[0] = tail
->cnv_len
;
3090 cnv
+= sizeof(si32_t
);
3092 i
= tail
->cnv_len
* sizeof(wc_t
);
3093 memcpy(cnv
, tail
->cnv_dat
, i
);
3097 cnv
= save_cnv
+ (tail
->calc_cnv_len
& SI32_MAX
);
3102 /* Search for a yet existing identical mapping */
3103 for(ltbcp
= NULL
, tbcp
= a_tty
.tg_bind
[tbpcp
->tbpc_flags
]; tbcp
!= NULL
;
3104 ltbcp
= tbcp
, tbcp
= tbcp
->tbc_next
)
3105 if(tbcp
->tbc_seq_len
== sl
&& !memcmp(tbcp
->tbc_seq
, cpbase
, sl
)){
3106 tbpcp
->tbpc_tbcp
= tbcp
;
3109 tbpcp
->tbpc_ltbcp
= ltbcp
;
3110 tbpcp
->tbpc_flags
|= (f
& a_TTY__BIND_MASK
);
3113 /* Create single string expansion if so desired */
3117 exp
= tbpcp
->tbpc_exp
.s
;
3119 i
= tbpcp
->tbpc_exp
.l
;
3120 if(i
> 0 && exp
[i
- 1] == '@'){
3122 if(!blankspacechar(exp
[i
- 1]))
3128 exp
[tbpcp
->tbpc_exp
.l
= i
] = '\0';
3129 tbpcp
->tbpc_flags
|= a_TTY_BIND_NOCOMMIT
;
3132 /* It may map to an internal MLE command! */
3133 for(i
= 0; i
< NELEM(a_tty_bind_fun_names
); ++i
)
3134 if(!asccasecmp(exp
, a_tty_bind_fun_names
[i
])){
3135 tbpcp
->tbpc_flags
|= a_TTY_BIND_FUN_EXPAND(i
) |
3136 a_TTY_BIND_FUN_INTERNAL
|
3137 (head
->next
== NULL
? a_TTY_BIND_MLE1CNTRL
: 0);
3138 if((options
& OPT_D_V
) && (tbpcp
->tbpc_flags
& a_TTY_BIND_NOCOMMIT
))
3139 n_err(_("`%s': MLE commands can't be made editable via @: %s\n"),
3140 tbpcp
->tbpc_cmd
, exp
);
3141 tbpcp
->tbpc_flags
&= ~a_TTY_BIND_NOCOMMIT
;
3146 f
|= a_TRUE_RV
; /* TODO because we only now true and false; DEFUNCT.. */
3148 n_string_gut(shoup
);
3150 return (f
& a_TRUE_RV
) != 0;
3154 a_tty_bind_resolve(struct a_tty_bind_ctx
*tbcp
){
3155 char capname
[a_TTY_BIND_CAPNAME_MAX
+1];
3156 struct n_termcap_value tv
;
3158 bool_t isfirst
; /* TODO For now: first char must be control! */
3163 for(cp
= tbcp
->tbc_cnv
, isfirst
= TRU1
, len
= tbcp
->tbc_cnv_len
;
3164 len
> 0; isfirst
= FAL0
, cp
= next
){
3168 i
= ((si32_t
*)cp
)[0];
3175 /* struct{si32_t buf_len_iscap; si32_t cap_len; wc_t name[];} */
3176 cp
+= sizeof(si32_t
);
3177 i
= ((si32_t
*)cp
)[0];
3178 cp
+= sizeof(si32_t
);
3179 for(j
= 0; j
< i
; ++j
)
3180 capname
[j
] = ((wc_t
*)cp
)[j
];
3184 /* Use generic lookup mechanism if not a known query */
3188 tq
= n_termcap_query_for_name(capname
, n_TERMCAP_CAPTYPE_STRING
);
3190 tv
.tv_data
.tvd_string
= capname
;
3191 tq
= n__TERMCAP_QUERY_MAX
;
3194 if(tq
< 0 || !n_termcap_query(tq
, &tv
)){
3195 if(options
& OPT_D_V
)
3196 n_err(_("`bind': unknown or unsupported capability: %s: %s\n"),
3197 capname
, tbcp
->tbc_seq
);
3198 tbcp
->tbc_flags
|= a_TTY_BIND_DEFUNCT
;
3203 /* struct{si32_t buf_len_iscap; si32_t cap_len; char buf[]+NUL;} */
3207 i
= strlen(tv
.tv_data
.tvd_string
);
3208 if(/*i > SI32_MAX ||*/ i
>= PTR2SIZE(next
- cp
)){
3209 if(options
& OPT_D_V
)
3210 n_err(_("`bind': capability expansion too long: %s: %s\n"),
3211 capname
, tbcp
->tbc_seq
);
3212 tbcp
->tbc_flags
|= a_TTY_BIND_DEFUNCT
;
3215 if(options
& OPT_D_V
)
3216 n_err(_("`bind': empty capability expansion: %s: %s\n"),
3217 capname
, tbcp
->tbc_seq
);
3218 tbcp
->tbc_flags
|= a_TTY_BIND_DEFUNCT
;
3220 }else if(isfirst
&& !cntrlchar(*tv
.tv_data
.tvd_string
)){
3221 if(options
& OPT_D_V
)
3222 n_err(_("`bind': capability expansion doesn't start with "
3223 "control: %s: %s\n"), capname
, tbcp
->tbc_seq
);
3224 tbcp
->tbc_flags
|= a_TTY_BIND_DEFUNCT
;
3227 ((si32_t
*)cp
)[-1] = (si32_t
)i
;
3228 memcpy(cp
, tv
.tv_data
.tvd_string
, i
);
3236 a_tty_bind_del(struct a_tty_bind_parse_ctx
*tbpcp
){
3237 struct a_tty_bind_ctx
*ltbcp
, *tbcp
;
3240 tbcp
= tbpcp
->tbpc_tbcp
;
3242 if((ltbcp
= tbpcp
->tbpc_ltbcp
) != NULL
)
3243 ltbcp
->tbc_next
= tbcp
->tbc_next
;
3245 a_tty
.tg_bind
[tbpcp
->tbpc_flags
] = tbcp
->tbc_next
;
3248 --a_tty
.tg_bind_cnt
;
3249 a_tty
.tg_bind_isdirty
= TRU1
;
3254 a_tty_bind_tree_build(void){
3258 for(i
= 0; i
< n__LEXINPUT_CTX_MAX
; ++i
){
3259 struct a_tty_bind_ctx
*tbcp
;
3260 n_LCTAV(n_LEXINPUT_CTX_BASE
== 0);
3262 /* Somewhat wasteful, but easier to handle: simply clone the entire
3263 * primary key onto the secondary one, then only modify it */
3264 for(tbcp
= a_tty
.tg_bind
[n_LEXINPUT_CTX_BASE
]; tbcp
!= NULL
;
3265 tbcp
= tbcp
->tbc_next
)
3266 if(!(tbcp
->tbc_flags
& a_TTY_BIND_DEFUNCT
))
3267 a_tty__bind_tree_add(n_LEXINPUT_CTX_BASE
, &a_tty
.tg_bind_tree
[i
][0],
3270 if(i
!= n_LEXINPUT_CTX_BASE
)
3271 for(tbcp
= a_tty
.tg_bind
[i
]; tbcp
!= NULL
; tbcp
= tbcp
->tbc_next
)
3272 if(!(tbcp
->tbc_flags
& a_TTY_BIND_DEFUNCT
))
3273 a_tty__bind_tree_add(i
, &a_tty
.tg_bind_tree
[i
][0], tbcp
);
3276 a_tty
.tg_bind_isbuild
= TRU1
;
3281 a_tty_bind_tree_teardown(void){
3285 memset(&a_tty
.tg_bind_shcut_cancel
[0], 0,
3286 sizeof(a_tty
.tg_bind_shcut_cancel
));
3287 memset(&a_tty
.tg_bind_shcut_prompt_char
[0], 0,
3288 sizeof(a_tty
.tg_bind_shcut_prompt_char
));
3290 for(i
= 0; i
< n__LEXINPUT_CTX_MAX
; ++i
)
3291 for(j
= 0; j
< HSHSIZE
; ++j
)
3292 a_tty__bind_tree_free(a_tty
.tg_bind_tree
[i
][j
]);
3293 memset(&a_tty
.tg_bind_tree
[0], 0, sizeof(a_tty
.tg_bind_tree
));
3295 a_tty
.tg_bind_isdirty
= a_tty
.tg_bind_isbuild
= FAL0
;
3300 a_tty__bind_tree_add(ui32_t hmap_idx
, struct a_tty_bind_tree
*store
[HSHSIZE
],
3301 struct a_tty_bind_ctx
*tbcp
){
3304 struct a_tty_bind_tree
*ntbtp
;
3310 for(cnvdat
= tbcp
->tbc_cnv
, cnvlen
= tbcp
->tbc_cnv_len
; cnvlen
> 0;){
3311 union {wchar_t const *wp
; char const *cp
;} u
;
3314 /* {si32_t buf_len_iscap;} */
3315 entlen
= *(si32_t
const*)cnvdat
;
3317 if(entlen
& SI32_MIN
){
3318 /* struct{si32_t buf_len_iscap; si32_t cap_len; char buf[]+NUL;}
3319 * Note that empty capabilities result in DEFUNCT */
3320 for(u
.cp
= (char const*)&((si32_t
const*)cnvdat
)[2];
3321 *u
.cp
!= '\0'; ++u
.cp
)
3322 ntbtp
= a_tty__bind_tree_add_wc(store
, ntbtp
, *u
.cp
, TRU1
);
3323 ntbtp
->tbt_isseq_trail
= TRU1
;
3326 /* struct{si32_t buf_len_iscap; wc_t buf[]+NUL;} */
3329 u
.wp
= (wchar_t const*)&((si32_t
const*)cnvdat
)[1];
3331 /* May be a special shortcut function? */
3332 if(ntbtp
== NULL
&& (tbcp
->tbc_flags
& a_TTY_BIND_MLE1CNTRL
)){
3336 ctx
= tbcp
->tbc_flags
& n__LEXINPUT_CTX_MAX
;
3337 fun
= tbcp
->tbc_flags
& a_TTY__BIND_FUN_MASK
;
3339 if(fun
== a_TTY_BIND_FUN_CANCEL
){
3340 for(cp
= &a_tty
.tg_bind_shcut_cancel
[ctx
][0];
3341 PTRCMP(cp
, <, &a_tty
.tg_bind_shcut_cancel
[ctx
]
3342 [NELEM(a_tty
.tg_bind_shcut_cancel
[ctx
]) - 1]); ++cp
)
3347 }else if(fun
== a_TTY_BIND_FUN_PROMPT_CHAR
){
3348 for(cp
= &a_tty
.tg_bind_shcut_prompt_char
[ctx
][0];
3349 PTRCMP(cp
, <, &a_tty
.tg_bind_shcut_prompt_char
[ctx
]
3350 [NELEM(a_tty
.tg_bind_shcut_prompt_char
[ctx
]) - 1]);
3359 isseq
= (u
.wp
[1] != '\0');
3360 for(; *u
.wp
!= '\0'; ++u
.wp
)
3361 ntbtp
= a_tty__bind_tree_add_wc(store
, ntbtp
, *u
.wp
, isseq
);
3363 ntbtp
->tbt_isseq_trail
= TRU1
;
3370 /* Should have been rendered defunctional at first instead */
3371 assert(ntbtp
!= NULL
);
3372 ntbtp
->tbt_bind
= tbcp
;
3376 static struct a_tty_bind_tree
*
3377 a_tty__bind_tree_add_wc(struct a_tty_bind_tree
**treep
,
3378 struct a_tty_bind_tree
*parentp
, wchar_t wc
, bool_t isseq
){
3379 struct a_tty_bind_tree
*tbtp
, *xtbtp
;
3382 if(parentp
== NULL
){
3383 treep
+= wc
% HSHSIZE
;
3385 /* Having no parent also means that the tree slot is possibly empty */
3386 for(tbtp
= *treep
; tbtp
!= NULL
;
3387 parentp
= tbtp
, tbtp
= tbtp
->tbt_sibling
){
3388 if(tbtp
->tbt_char
!= wc
)
3390 if(tbtp
->tbt_isseq
== isseq
)
3392 /* isseq MUST be linked before !isseq, so record this "parent"
3393 * sibling, but continue searching for now */
3396 /* Otherwise it is impossible that we'll find what we look for */
3399 while((tbtp
= tbtp
->tbt_sibling
) != NULL
)
3400 assert(tbtp
->tbt_char
!= wc
);
3406 tbtp
= smalloc(sizeof *tbtp
);
3407 memset(tbtp
, 0, sizeof *tbtp
);
3408 tbtp
->tbt_char
= wc
;
3409 tbtp
->tbt_isseq
= isseq
;
3411 if(parentp
== NULL
){
3412 tbtp
->tbt_sibling
= *treep
;
3415 tbtp
->tbt_sibling
= parentp
->tbt_sibling
;
3416 parentp
->tbt_sibling
= tbtp
;
3419 if((tbtp
= *(treep
= &parentp
->tbt_childs
)) != NULL
){
3420 for(;; tbtp
= xtbtp
){
3421 if(tbtp
->tbt_char
== wc
){
3422 if(tbtp
->tbt_isseq
== isseq
)
3424 /* isseq MUST be linked before, so it is impossible that we'll
3425 * find what we look for */
3428 while((tbtp
= tbtp
->tbt_sibling
) != NULL
)
3429 assert(tbtp
->tbt_char
!= wc
);
3436 if((xtbtp
= tbtp
->tbt_sibling
) == NULL
){
3437 treep
= &tbtp
->tbt_sibling
;
3443 xtbtp
= smalloc(sizeof *xtbtp
);
3444 memset(xtbtp
, 0, sizeof *xtbtp
);
3445 xtbtp
->tbt_parent
= parentp
;
3446 xtbtp
->tbt_char
= wc
;
3447 xtbtp
->tbt_isseq
= isseq
;
3457 a_tty__bind_tree_free(struct a_tty_bind_tree
*tbtp
){
3459 while(tbtp
!= NULL
){
3460 struct a_tty_bind_tree
*tmp
;
3462 if((tmp
= tbtp
->tbt_childs
) != NULL
)
3463 a_tty__bind_tree_free(tmp
);
3465 tmp
= tbtp
->tbt_sibling
;
3471 # endif /* HAVE_KEY_BINDINGS */
3477 /* Load the history file */
3478 # ifdef HAVE_HISTORY
3484 size_t lsize
, cnt
, llen
;
3487 a_tty
.tg_hist_size
= 0;
3488 a_tty
.tg_hist_size_max
= (size_t)hs
;
3496 hold_all_sigs(); /* TODO too heavy, yet we may jump even here!? */
3497 f
= fopen(v
, "r"); /* TODO HISTFILE LOAD: use linebuf pool */
3500 (void)n_file_lock(fileno(f
), FLT_READ
, 0,0, UIZ_MAX
);
3504 cnt
= (size_t)fsize(f
);
3505 while(fgetline(&lbuf
, &lsize
, &cnt
, &llen
, f
, FAL0
) != NULL
){
3506 if(llen
> 0 && lbuf
[llen
- 1] == '\n')
3507 lbuf
[--llen
] = '\0';
3508 if(llen
== 0 || lbuf
[0] == '#') /* xxx comments? noone! */
3513 isgabby
= (lbuf
[0] == '*');
3514 n_tty_addhist(lbuf
+ isgabby
, isgabby
);
3522 rele_all_sigs(); /* XXX remove jumps */
3524 # endif /* HAVE_HISTORY */
3526 /* Force immediate resolve for anything which follows */
3527 pstate
|= PS_LINE_EDITOR_INIT
;
3529 # ifdef HAVE_KEY_BINDINGS
3530 /* `bind's (and `unbind's) done from within resource files couldn't be
3531 * performed for real since our termcap driver wasn't yet loaded, and we
3532 * can't perform automatic init since the user may have disallowed so */
3534 struct a_tty_bind_ctx
*tbcp
;
3535 enum n_lexinput_flags lif
;
3537 for(lif
= 0; lif
< n__LEXINPUT_CTX_MAX
; ++lif
)
3538 for(tbcp
= a_tty
.tg_bind
[lif
]; tbcp
!= NULL
; tbcp
= tbcp
->tbc_next
)
3539 if((tbcp
->tbc_flags
& (a_TTY_BIND_RESOLVE
| a_TTY_BIND_DEFUNCT
)) ==
3541 a_tty_bind_resolve(tbcp
);
3544 /* And we want to (try to) install some default key bindings */
3545 if(!ok_blook(line_editor_no_defaults
)){
3547 struct a_tty_bind_parse_ctx tbpc
;
3548 struct a_tty_bind_default_tuple
const *tbdtp
;
3550 buf
[0] = '$', buf
[1] = '\'', buf
[2] = '\\', buf
[3] = 'c',
3551 buf
[5] = '\'', buf
[6] = '\0';
3552 for(tbdtp
= a_tty_bind_default_tuples
;
3554 &a_tty_bind_default_tuples
[NELEM(a_tty_bind_default_tuples
)]);
3556 memset(&tbpc
, 0, sizeof tbpc
);
3557 tbpc
.tbpc_cmd
= "bind";
3558 if(tbdtp
->tbdt_iskey
){
3559 buf
[4] = tbdtp
->tbdt_ckey
;
3560 tbpc
.tbpc_in_seq
= buf
;
3562 tbpc
.tbpc_in_seq
= savecatsep(":", '\0',
3563 n_termcap_name_of_query(tbdtp
->tbdt_query
));
3564 tbpc
.tbpc_exp
.s
= UNCONST(tbdtp
->tbdt_exp
[0] == '\0'
3565 ? a_tty_bind_fun_names
[(ui8_t
)tbdtp
->tbdt_exp
[1]]
3567 tbpc
.tbpc_exp
.l
= strlen(tbpc
.tbpc_exp
.s
);
3568 tbpc
.tbpc_flags
= n_LEXINPUT_CTX_BASE
;
3569 /* ..but don't want to overwrite any user settings */
3570 a_tty_bind_create(&tbpc
, FAL0
);
3573 # endif /* HAVE_KEY_BINDINGS */
3579 n_tty_destroy(void){
3582 # ifdef HAVE_HISTORY
3586 struct a_tty_hist
*thp
;
3598 dogabby
= ok_blook(history_gabby_persist
);
3600 if((thp
= a_tty
.tg_hist
) != NULL
)
3601 for(; thp
->th_older
!= NULL
; thp
= thp
->th_older
)
3602 if((dogabby
|| !thp
->th_isgabby
) && --hs
== 0)
3605 hold_all_sigs(); /* TODO too heavy, yet we may jump even here!? */
3606 f
= fopen(v
, "w"); /* TODO temporary + rename?! */
3609 (void)n_file_lock(fileno(f
), FLT_WRITE
, 0,0, UIZ_MAX
);
3611 for(; thp
!= NULL
; thp
= thp
->th_younger
){
3612 if(dogabby
|| !thp
->th_isgabby
){
3615 fwrite(thp
->th_dat
, sizeof *thp
->th_dat
, thp
->th_len
, f
);
3621 rele_all_sigs(); /* XXX remove jumps */
3623 # endif /* HAVE_HISTORY */
3625 # if defined HAVE_KEY_BINDINGS && defined HAVE_DEBUG
3626 c_unbind(UNCONST("* *"));
3630 memset(&a_tty
, 0, sizeof a_tty
);
3632 DBG( pstate
&= ~PS_LINE_EDITOR_INIT
; )
3637 n_tty_signal(int sig
){
3638 sigset_t nset
, oset
;
3639 NYD_X
; /* Signal handler */
3643 /* We don't deal with SIGWINCH, yet get called from main.c */
3646 a_tty_term_mode(FAL0
);
3647 n_TERMCAP_SUSPEND(TRU1
);
3651 sigaddset(&nset
, sig
);
3652 sigprocmask(SIG_UNBLOCK
, &nset
, &oset
);
3654 /* When we come here we'll continue editing, so reestablish */
3655 sigprocmask(SIG_BLOCK
, &oset
, (sigset_t
*)NULL
);
3658 n_TERMCAP_RESUME(TRU1
);
3659 a_tty_term_mode(TRU1
);
3660 a_tty
.tg_line
->tl_vi_flags
|= a_TTY_VF_MOD_DIRTY
;
3666 (n_tty_readline
)(char const *prompt
, char **linebuf
, size_t *linesize
, size_t n
3667 SMALLOC_DEBUG_ARGS
){
3668 struct a_tty_line tl
;
3672 ui32_t plen
, pwidth
;
3674 char const *orig_prompt
;
3677 assert(pstate
& PS_LINE_EDITOR_INIT
);
3679 orig_prompt
= prompt
;
3681 /* TODO because of jumping away we cannot use srelax()ation: would be good */
3682 prompt
= orig_prompt
;
3685 n_colour_env_create(n_COLOUR_CTX_MLE
, FAL0
);
3688 /* Classify prompt */
3692 size_t i
= strlen(prompt
);
3694 if(i
== 0 || i
>= UI32_MAX
)
3697 /* TODO *prompt* is in multibyte and not in a_tty_cell, therefore
3698 * TODO we cannot handle it in parts, it's all or nothing.
3699 * TODO Later (S-CText, SysV signals) the prompt should be some global
3700 * TODO carrier thing, fully evaluated and passed around as UI-enabled
3701 * TODO string, then we can print it character by character */
3702 struct n_visual_info_ctx vic
;
3704 memset(&vic
, 0, sizeof vic
);
3705 vic
.vic_indat
= prompt
;
3707 if(n_visual_info(&vic
, n_VISUAL_INFO_WIDTH_QUERY
)){
3708 pwidth
= (ui32_t
)vic
.vic_vi_width
;
3711 n_err(_("Character set error in evaluation of prompt\n"));
3719 struct n_colour_pen
*ccp
;
3720 struct str
const *sp
;
3722 if(prompt
!= NULL
&&
3723 (ccp
= n_colour_pen_create(n_COLOUR_ID_MLE_PROMPT
, NULL
)) != NULL
&&
3724 (sp
= n_colour_pen_to_str(ccp
)) != NULL
){
3725 char const *ccol
= sp
->s
;
3727 if((sp
= n_colour_reset_to_str()) != NULL
){
3728 size_t l1
= strlen(ccol
), l2
= strlen(sp
->s
);
3729 ui32_t nplen
= (ui32_t
)(l1
+ plen
+ l2
);
3730 char *nprompt
= salloc(nplen
+1);
3732 memcpy(nprompt
, ccol
, l1
);
3733 memcpy(&nprompt
[l1
], prompt
, plen
);
3734 memcpy(&nprompt
[l1
+= plen
], sp
->s
, ++l2
);
3741 /* .tl_pos_buf is a hack */
3742 posbuf
= pos
= NULL
;
3743 if((ccp
= n_colour_pen_create(n_COLOUR_ID_MLE_POSITION
, NULL
)) != NULL
&&
3744 (sp
= n_colour_pen_to_str(ccp
)) != NULL
){
3745 char const *ccol
= sp
->s
;
3747 if((sp
= n_colour_reset_to_str()) != NULL
){
3748 size_t l1
= strlen(ccol
), l2
= strlen(sp
->s
);
3750 posbuf
= salloc(l1
+ 4 + l2
+1);
3751 memcpy(posbuf
, ccol
, l1
);
3753 memcpy(&pos
[4], sp
->s
, ++l2
);
3757 posbuf
= pos
= salloc(4 +1);
3761 # endif /* HAVE_COLOUR */
3763 memset(&tl
, 0, sizeof tl
);
3765 # ifdef HAVE_KEY_BINDINGS
3767 char const *cp
= ok_vlook(bind_timeout
);
3772 if((ul
= strtoul(cp
, NULL
, 0)) > 0 &&
3773 /* Convert to tenths of a second, unfortunately */
3774 (ul
= (ul
+ 99) / 100) <= a_TTY_BIND_TIMEOUT_MAX
)
3775 tl
.tl_bind_timeout
= (ui8_t
)ul
;
3776 else if(options
& OPT_D_V
)
3777 n_err(_("Ignoring invalid *bind-timeout*: %s\n"), cp
);
3781 /* TODO honour other bind contexts, like "compose" */
3782 if(a_tty
.tg_bind_isdirty
)
3783 a_tty_bind_tree_teardown();
3784 if(a_tty
.tg_bind_cnt
> 0 && !a_tty
.tg_bind_isbuild
)
3785 a_tty_bind_tree_build();
3786 tl
.tl_bind_tree_hmap
= &a_tty
.tg_bind_tree
[n_LEXINPUT_CTX_BASE
];
3787 tl
.tl_bind_shcut_cancel
=
3788 &a_tty
.tg_bind_shcut_cancel
[n_LEXINPUT_CTX_BASE
];
3789 tl
.tl_bind_shcut_prompt_char
=
3790 &a_tty
.tg_bind_shcut_prompt_char
[n_LEXINPUT_CTX_BASE
];
3791 # endif /* HAVE_KEY_BINDINGS */
3793 if((tl
.tl_prompt
= prompt
) != NULL
){ /* XXX not re-evaluated */
3794 tl
.tl_prompt_length
= plen
;
3795 tl
.tl_prompt_width
= pwidth
;
3798 tl
.tl_pos_buf
= posbuf
;
3802 tl
.tl_line
.cbuf
= *linebuf
;
3804 tl
.tl_defc
.s
= savestrbuf(*linebuf
, n
);
3807 tl
.tl_x_buf
= linebuf
;
3808 tl
.tl_x_bufsize
= linesize
;
3810 a_tty
.tg_line
= &tl
;
3812 a_tty_term_mode(TRU1
);
3813 nn
= a_tty_readline(&tl
, n SMALLOC_DEBUG_ARGSCALL
);
3814 a_tty_term_mode(FAL0
);
3816 a_tty
.tg_line
= NULL
;
3819 n_colour_env_gut(stdout
);
3822 if(tl
.tl_reenter_after_cmd
!= NULL
){
3823 n
= (nn
<= 0) ? 0 : nn
;
3824 n_source_command(tl
.tl_reenter_after_cmd
);
3832 n_tty_addhist(char const *s
, bool_t isgabby
){
3833 # ifdef HAVE_HISTORY
3834 /* Super-Heavy-Metal: block all sigs, avoid leaks+ on jump */
3836 struct a_tty_hist
*thp
, *othp
, *ythp
;
3842 # ifdef HAVE_HISTORY
3843 if(isgabby
&& !ok_blook(history_gabby
))
3846 if(a_tty
.tg_hist_size_max
== 0)
3848 a_TTY_CHECK_ADDHIST(s
, goto j_leave
);
3850 l
= (ui32_t
)strlen(s
);
3852 /* Eliminating duplicates is expensive, but simply inacceptable so
3853 * during the load of a potentially large history file! */
3854 if(pstate
& PS_LINE_EDITOR_INIT
)
3855 for(thp
= a_tty
.tg_hist
; thp
!= NULL
; thp
= thp
->th_older
)
3856 if(thp
->th_len
== l
&& !strcmp(thp
->th_dat
, s
)){
3857 hold_all_sigs(); /* TODO */
3859 thp
->th_isgabby
= !!isgabby
;
3860 othp
= thp
->th_older
;
3861 ythp
= thp
->th_younger
;
3863 othp
->th_younger
= ythp
;
3865 a_tty
.tg_hist_tail
= ythp
;
3867 ythp
->th_older
= othp
;
3869 a_tty
.tg_hist
= othp
;
3874 ++a_tty
.tg_hist_size
;
3875 if((pstate
& PS_LINE_EDITOR_INIT
) &&
3876 a_tty
.tg_hist_size
> a_tty
.tg_hist_size_max
){
3877 --a_tty
.tg_hist_size
;
3878 if((thp
= a_tty
.tg_hist_tail
) != NULL
){
3879 if((a_tty
.tg_hist_tail
= thp
->th_younger
) == NULL
)
3880 a_tty
.tg_hist
= NULL
;
3882 a_tty
.tg_hist_tail
->th_older
= NULL
;
3887 thp
= smalloc((sizeof(struct a_tty_hist
) -
3888 VFIELD_SIZEOF(struct a_tty_hist
, th_dat
)) + l
+1);
3889 thp
->th_isgabby
= !!isgabby
;
3891 memcpy(thp
->th_dat
, s
, l
+1);
3893 if((thp
->th_older
= a_tty
.tg_hist
) != NULL
)
3894 a_tty
.tg_hist
->th_younger
= thp
;
3896 a_tty
.tg_hist_tail
= thp
;
3897 thp
->th_younger
= NULL
;
3898 a_tty
.tg_hist
= thp
;
3906 # ifdef HAVE_HISTORY
3914 struct a_tty_hist
*thp
;
3916 if(a_tty
.tg_hist
== NULL
)
3919 if((fp
= Ftmp(NULL
, "hist", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
){
3920 n_perr(_("tmpfile"), 0);
3925 i
= a_tty
.tg_hist_size
;
3927 for(thp
= a_tty
.tg_hist
; thp
!= NULL
;
3928 --i
, b
+= thp
->th_len
, thp
= thp
->th_older
)
3930 "%c%4" PRIuZ
". %-50.50s (%4" PRIuZ
"+%2" PRIu32
" B)\n",
3931 (thp
->th_isgabby
? '*' : ' '), i
, thp
->th_dat
, b
, thp
->th_len
);
3933 page_or_print(fp
, i
);
3939 struct a_tty_hist
*thp
;
3941 while((thp
= a_tty
.tg_hist
) != NULL
){
3942 a_tty
.tg_hist
= thp
->th_older
;
3945 a_tty
.tg_hist_tail
= NULL
;
3946 a_tty
.tg_hist_size
= 0;
3951 struct a_tty_hist
*thp
;
3953 if(UICMP(z
, entry
, <=, a_tty
.tg_hist_size
)){
3954 entry
= (long)a_tty
.tg_hist_size
- entry
;
3955 for(thp
= a_tty
.tg_hist
;; thp
= thp
->th_older
)
3958 else if(entry
-- != 0)
3961 v
= temporary_arg_v_store
= thp
->th_dat
;
3969 # endif /* HAVE_HISTORY */
3971 # ifdef HAVE_KEY_BINDINGS
3974 n_CMD_ARG_DESC_SUBCLASS_DEF(bind
, 3, a_tty_bind_cad
) { /* TODO cmd_tab.h */
3975 {n_CMD_ARG_DESC_STRING
, 0},
3976 {n_CMD_ARG_DESC_WYSH
| n_CMD_ARG_DESC_OPTION
|
3977 n_CMD_ARG_DESC_HONOUR_STOP
,
3978 n_SHEXP_PARSE_DRYRUN
| n_SHEXP_PARSE_LOG
},
3979 {n_CMD_ARG_DESC_WYSH
| n_CMD_ARG_DESC_OPTION
| n_CMD_ARG_DESC_GREEDY
|
3980 n_CMD_ARG_DESC_HONOUR_STOP
,
3981 n_SHEXP_PARSE_IGNORE_EMPTY
}
3982 } n_CMD_ARG_DESC_SUBCLASS_DEF_END
;
3983 struct n_cmd_arg_ctx cac
;
3984 struct a_tty_bind_ctx
*tbcp
;
3985 enum n_lexinput_flags lif
;
3987 union {char const *cp
; char *p
; char c
;} c
;
3990 cac
.cac_desc
= n_CMD_ARG_DESC_SUBCLASS_CAST(&a_tty_bind_cad
);
3992 cac
.cac_inlen
= UIZ_MAX
;
3993 if(!n_cmd_arg_parse(&cac
)){
3998 c
.cp
= cac
.cac_arg
->ca_arg
.ca_str
.s
;
4002 show
= !asccasecmp(cac
.cac_arg
->ca_next
->ca_arg
.ca_str
.s
, "show");
4005 if((lif
= a_tty_bind_ctx_find(c
.cp
)) == (enum n_lexinput_flags
)-1){
4006 if(!(aster
= n_is_all_or_aster(c
.cp
)) || !show
){
4007 n_err(_("`bind': invalid context: %s\n"), c
.cp
);
4018 if((fp
= Ftmp(NULL
, "bind", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
){
4019 n_perr(_("tmpfile"), 0);
4026 for(tbcp
= a_tty
.tg_bind
[lif
]; tbcp
!= NULL
;
4027 ++lns
, tbcp
= tbcp
->tbc_next
){
4028 /* Print the bytes of resolved terminal capabilities, then */
4029 if((options
& OPT_D_V
) &&
4030 (tbcp
->tbc_flags
& (a_TTY_BIND_RESOLVE
| a_TTY_BIND_DEFUNCT
)
4031 ) == a_TTY_BIND_RESOLVE
){
4033 union {wchar_t const *wp
; char const *cp
;} u
;
4036 char const *cnvdat
, *bsep
, *cbufp
;
4041 cbuf
[0] = '=', cbuf
[2] = '\0';
4042 for(cnvdat
= tbcp
->tbc_cnv
, cnvlen
= tbcp
->tbc_cnv_len
;
4044 if(cnvdat
!= tbcp
->tbc_cnv
)
4047 /* {si32_t buf_len_iscap;} */
4048 entlen
= *(si32_t
const*)cnvdat
;
4049 if(entlen
& SI32_MIN
){
4050 /* struct{si32_t buf_len_iscap; si32_t cap_len;
4051 * char buf[]+NUL;} */
4053 u
.cp
= (char const*)&((si32_t
const*)cnvdat
)[2];
4054 (c
.c
= *u
.cp
) != '\0'; ++u
.cp
){
4055 if(asciichar(c
.c
) && !cntrlchar(c
.c
))
4056 cbuf
[1] = c
.c
, cbufp
= cbuf
;
4059 fprintf(fp
, "%s%02X%s",
4060 bsep
, (ui32_t
)(ui8_t
)c
.c
, cbufp
);
4075 fprintf(fp
, "%sbind %s %s %s%s%s\n",
4076 ((tbcp
->tbc_flags
& a_TTY_BIND_DEFUNCT
)
4077 /* I18N: `bind' sequence not working, either because it is
4078 * I18N: using Unicode and that is not available in the locale,
4079 * I18N: or a termcap(5)/terminfo(5) sequence won't work out */
4080 ? _("# <Defunctional> ") : ""),
4081 a_tty_bind_ctx_maps
[lif
].tbcm_name
, tbcp
->tbc_seq
,
4082 n_shell_quote_cp(tbcp
->tbc_exp
, TRU1
),
4083 (tbcp
->tbc_flags
& a_TTY_BIND_NOCOMMIT
? "@" : ""),
4084 (!(options
& OPT_D_VV
) ? ""
4085 : (tbcp
->tbc_flags
& a_TTY_BIND_FUN_INTERNAL
4086 ? _(" # MLE internal") : ""))
4089 if(!aster
|| ++lif
>= n__LEXINPUT_CTX_MAX
)
4092 page_or_print(fp
, lns
);
4096 struct a_tty_bind_parse_ctx tbpc
;
4097 struct n_string store
;
4099 memset(&tbpc
, 0, sizeof tbpc
);
4100 tbpc
.tbpc_cmd
= a_tty_bind_cad
.cad_name
;
4101 tbpc
.tbpc_in_seq
= cac
.cac_arg
->ca_next
->ca_arg
.ca_str
.s
;
4102 tbpc
.tbpc_exp
.s
= n_string_cp(n_cmd_arg_join_greedy(&cac
,
4103 n_string_creat_auto(&store
)));
4104 tbpc
.tbpc_exp
.l
= store
.s_len
;
4105 tbpc
.tbpc_flags
= lif
;
4106 if(!a_tty_bind_create(&tbpc
, TRU1
))
4108 n_string_gut(&store
);
4112 return (v
!= NULL
) ? EXIT_OK
: EXIT_ERR
;
4117 n_CMD_ARG_DESC_SUBCLASS_DEF(unbind
, 2, a_tty_unbind_cad
) {/* TODO cmd_tab.h*/
4118 {n_CMD_ARG_DESC_STRING
, 0},
4119 {n_CMD_ARG_DESC_WYSH
| n_CMD_ARG_DESC_HONOUR_STOP
,
4120 n_SHEXP_PARSE_DRYRUN
| n_SHEXP_PARSE_LOG
}
4121 } n_CMD_ARG_DESC_SUBCLASS_DEF_END
;
4122 struct a_tty_bind_parse_ctx tbpc
;
4123 struct n_cmd_arg_ctx cac
;
4124 struct a_tty_bind_ctx
*tbcp
;
4125 enum n_lexinput_flags lif
;
4127 union {char const *cp
; char *p
;} c
;
4130 cac
.cac_desc
= n_CMD_ARG_DESC_SUBCLASS_CAST(&a_tty_unbind_cad
);
4132 cac
.cac_inlen
= UIZ_MAX
;
4133 if(!n_cmd_arg_parse(&cac
)){
4138 c
.cp
= cac
.cac_arg
->ca_arg
.ca_str
.s
;
4141 if((lif
= a_tty_bind_ctx_find(c
.cp
)) == (enum n_lexinput_flags
)-1){
4142 if(!(aster
= n_is_all_or_aster(c
.cp
))){
4143 n_err(_("`unbind': invalid context: %s\n"), c
.cp
);
4150 c
.cp
= cac
.cac_arg
->ca_next
->ca_arg
.ca_str
.s
;
4152 if(n_is_all_or_aster(c
.cp
)){
4153 while((tbcp
= a_tty
.tg_bind
[lif
]) != NULL
){
4154 memset(&tbpc
, 0, sizeof tbpc
);
4155 tbpc
.tbpc_tbcp
= tbcp
;
4156 tbpc
.tbpc_flags
= lif
;
4157 a_tty_bind_del(&tbpc
);
4160 memset(&tbpc
, 0, sizeof tbpc
);
4161 tbpc
.tbpc_cmd
= a_tty_unbind_cad
.cad_name
;
4162 tbpc
.tbpc_in_seq
= c
.cp
;
4163 tbpc
.tbpc_flags
= lif
;
4165 if(UNLIKELY(!a_tty_bind_parse(FAL0
, &tbpc
)))
4167 else if(UNLIKELY((tbcp
= tbpc
.tbpc_tbcp
) == NULL
)){
4168 n_err(_("`unbind': no such `bind'ing: %s %s\n"),
4169 a_tty_bind_ctx_maps
[lif
].tbcm_name
, c
.cp
);
4172 a_tty_bind_del(&tbpc
);
4175 if(aster
&& ++lif
< n__LEXINPUT_CTX_MAX
)
4179 return (v
!= NULL
) ? EXIT_OK
: EXIT_ERR
;
4181 # endif /* HAVE_KEY_BINDINGS */
4182 #endif /* HAVE_MLE */
4185 * The really-nothing-at-all implementation
4187 #if !defined HAVE_MLE
4196 n_tty_destroy(void){
4202 n_tty_signal(int sig
){
4203 NYD_X
; /* Signal handler */
4206 # ifdef HAVE_TERMCAP
4209 sigset_t nset
, oset
;
4211 n_TERMCAP_SUSPEND(TRU1
);
4215 sigaddset(&nset
, sig
);
4216 sigprocmask(SIG_UNBLOCK
, &nset
, &oset
);
4218 /* When we come here we'll continue editing, so reestablish */
4219 sigprocmask(SIG_BLOCK
, &oset
, (sigset_t
*)NULL
);
4222 n_TERMCAP_RESUME(TRU1
);
4226 # endif /* HAVE_TERMCAP */
4230 (n_tty_readline
)(char const *prompt
, char **linebuf
, size_t *linesize
, size_t n
4231 SMALLOC_DEBUG_ARGS
){
4237 fputs(prompt
, stdout
);
4240 # ifdef HAVE_TERMCAP
4243 rv
= (readline_restart
)(stdin
, linebuf
, linesize
,n SMALLOC_DEBUG_ARGSCALL
);
4244 # ifdef HAVE_TERMCAP
4252 n_tty_addhist(char const *s
, bool_t isgabby
){
4258 #endif /* nothing at all */
4260 #undef a_TTY_SIGNALS