(BWDIC!) Drop readline(3) support..
[s-mailx.git] / tty.c
blob2de792fea30fc71776a71b2cbac012e0d9301c53
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.
21 #undef n_FILE
22 #define n_FILE tty
24 #ifndef HAVE_AMALGAMATION
25 # include "nail.h"
26 #endif
28 #if defined HAVE_MLE || defined HAVE_TERMCAP
29 # define a_TTY_SIGNALS
30 #endif
32 /* Shared history support macros */
33 #ifdef HAVE_HISTORY
34 # define a_TTY_HISTFILE(S) \
35 do{\
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);\
40 if((S) == NULL)\
41 (S) = __hist_obsolete;\
42 if((S) != NULL)\
43 S = fexpand(S, FEXP_LOCAL | FEXP_NSHELL);\
44 }while(0)
46 # define a_TTY_HISTSIZE(V) \
47 do{\
48 char const *__hist_obsolete = ok_vlook(NAIL_HISTSIZE);\
49 char const *__sv = ok_vlook(history_size);\
50 long __rv;\
51 if(__hist_obsolete != NULL)\
52 OBSOLETE(_("please use *history-size* instead of *NAIL_HISTSIZE*"));\
53 if(__sv == NULL)\
54 __sv = __hist_obsolete;\
55 if(__sv == NULL || (__rv = strtol(__sv, NULL, 10)) == 0)\
56 (V) = HIST_SIZE;\
57 else if(__rv < 0)\
58 __rv = 0;\
59 else\
60 (V) = __rv;\
61 }while(0)
63 # define a_TTY_CHECK_ADDHIST(S,NOACT) \
64 do{\
65 switch(*(S)){\
66 case '\0':\
67 case ' ':\
68 case '\t':\
69 NOACT;\
70 default:\
71 break;\
73 }while(0)
75 # define C_HISTORY_SHARED \
76 char **argv = v;\
77 long entry;\
78 NYD_ENTER;\
80 if(!(pstate & PS_LINE_EDITOR_INIT))\
81 goto jleave;\
82 if(*argv == NULL)\
83 goto jlist;\
84 if(argv[1] != NULL)\
85 goto jerr;\
86 if(!asccasecmp(*argv, "show"))\
87 goto jlist;\
88 if(!asccasecmp(*argv, "clear"))\
89 goto jclear;\
90 if((entry = strtol(*argv, argv, 10)) > 0 && **argv == '\0')\
91 goto jentry;\
92 jerr:\
93 n_err(_("Synopsis: history: %s\n" \
94 "<show> (default), <clear> or select <NO> from editor history"));\
95 v = NULL;\
96 jleave:\
97 NYD_LEAVE;\
98 return (v == NULL ? !STOP : !OKAY); /* xxx 1:bad 0:good -- do some */
99 #endif /* HAVE_HISTORY */
101 #ifdef a_TTY_SIGNALS
102 static sighandler_type a_tty_oint, a_tty_oquit, a_tty_oterm,
103 a_tty_ohup,
104 a_tty_otstp, a_tty_ottin, a_tty_ottou;
105 #endif
107 #ifdef a_TTY_SIGNALS
108 static void a_tty_sigs_up(void), a_tty_sigs_down(void);
109 #endif
111 #ifdef a_TTY_SIGNALS
112 static void
113 a_tty_sigs_up(void){
114 sigset_t nset, oset;
115 NYD2_ENTER;
117 sigfillset(&nset);
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);
128 NYD2_LEAVE;
131 static void
132 a_tty_sigs_down(void){
133 sigset_t nset, oset;
134 NYD2_ENTER;
136 sigfillset(&nset);
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);
147 NYD2_LEAVE;
149 #endif /* a_TTY_SIGNALS */
151 static sigjmp_buf a_tty__actjmp; /* TODO someday, we won't need it no more */
152 static void
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);
160 FL bool_t
161 getapproval(char const * volatile prompt, bool_t noninteract_default)
163 sighandler_type volatile oint, ohup;
164 bool_t volatile rv;
165 int volatile sig;
166 NYD_ENTER;
168 if (!(options & OPT_INTERACTIVE)) {
169 sig = 0;
170 rv = noninteract_default;
171 goto jleave;
173 rv = FAL0;
175 /* C99 */{
176 char const *quest = noninteract_default
177 ? _("[yes]/no? ") : _("[no]/yes? ");
179 if (prompt == NULL)
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)
187 goto jrestore;
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);
195 jrestore:
196 termios_state_reset();
198 safe_signal(SIGHUP, ohup);
199 safe_signal(SIGINT, oint);
200 jleave:
201 NYD_LEAVE;
202 if (sig != 0)
203 n_raise(sig);
204 return rv;
207 #ifdef HAVE_SOCKETS
208 FL char *
209 getuser(char const * volatile query) /* TODO v15-compat obsolete */
211 sighandler_type volatile oint, ohup;
212 char * volatile user = NULL;
213 int volatile sig;
214 NYD_ENTER;
216 if (query == NULL)
217 query = _("User: ");
219 oint = safe_signal(SIGINT, SIG_IGN);
220 ohup = safe_signal(SIGHUP, SIG_IGN);
221 if ((sig = sigsetjmp(a_tty__actjmp, 1)) != 0)
222 goto jrestore;
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;
229 jrestore:
230 termios_state_reset();
232 safe_signal(SIGHUP, ohup);
233 safe_signal(SIGINT, oint);
234 NYD_LEAVE;
235 if (sig != 0)
236 n_raise(sig);
237 return user;
240 FL char *
241 getpassword(char const *query)
243 sighandler_type volatile oint, ohup;
244 struct termios tios;
245 char * volatile pass = NULL;
246 int volatile sig;
247 NYD_ENTER;
249 if (query == NULL)
250 query = _("Password: ");
251 fputs(query, stdout);
252 fflush(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)
268 goto jrestore;
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;
278 jrestore:
279 termios_state_reset();
280 if (options & OPT_TTYIN)
281 putc('\n', stdout);
283 safe_signal(SIGHUP, ohup);
284 safe_signal(SIGINT, oint);
285 NYD_LEAVE;
286 if (sig != 0)
287 n_raise(sig);
288 return pass;
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.
322 #ifdef HAVE_MLE
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 */
383 # endif
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)
396 # undef a_X
397 # define a_X(N,I)\
398 a_TTY_BIND_FUN_ ## N = a_TTY_BIND_FUN_EXPAND(I),
400 a_X(BELL, 0)
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)
409 a_X(REPAINT, 17)
410 a_X(QUOTE_RNDTRIP, 18)
411 a_X(PROMPT_CHAR, 19)
412 a_X(COMPLETE, 20)
413 a_X(PASTE, 21)
415 a_X(CANCEL, 22)
416 a_X(RESET, 23)
417 a_X(FULLRESET, 24)
418 a_X(COMMIT, 25) /* Must be last one! */
419 # undef a_X
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");
426 # endif
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{
443 a_TTY_VF_NONE,
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 */
466 char *tbc_cnv;
467 ui32_t tbc_seq_len;
468 ui32_t tbc_exp_len;
469 ui32_t tbc_cnv_len;
470 ui32_t tbc_flags;
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 */
498 ui32_t tbpc_seq_len;
499 ui32_t tbpc_cnv_len;
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 */
513 ui8_t tbt__dummy[2];
515 # endif /* HAVE_KEY_BINDINGS */
517 struct a_tty_cell{
518 wchar_t tc_wc;
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 */
525 struct a_tty_global{
526 struct a_tty_line *tg_line; /* To be able to access it from signal hdl */
527 # ifdef HAVE_HISTORY
528 struct a_tty_hist *tg_hist;
529 struct a_tty_hist *tg_hist_tail;
530 size_t tg_hist_size;
531 size_t tg_hist_size_max;
532 # endif
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];
541 # endif
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");
548 # ifdef HAVE_HISTORY
549 struct a_tty_hist{
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;
554 # endif
555 ui32_t th_len : 31;
556 # ifndef HAVE_BYTE_ORDER_LITTLE
557 ui32_t th_isgabby : 1;
558 # endif
559 char th_dat[VFIELD_SIZE(sizeof(ui32_t))];
561 # endif
563 struct a_tty_line{
564 /* Caller pointers */
565 char **tl_x_buf;
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;
576 # endif
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 */
581 union{
582 char *cbuf; /* *.tl_x_buf */
583 struct a_tty_cell *cells;
584 } tl_line;
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 */
589 # ifdef HAVE_HISTORY
590 struct a_tty_hist *tl_hist; /* History cursor */
591 # endif
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 */
603 ui8_t tl__dummy2[3];
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 */
608 # ifdef HAVE_COLOUR
609 char *tl_pos_buf; /* mle-position colour-on, [4], reset seq. */
610 char *tl_pos; /* Address of the [4] */
611 # endif
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] = {
627 # undef a_X
628 # define a_X(I,N) \
629 n_FIELD_INITI(a_TTY_BIND_FUN_REDUCE(a_TTY_BIND_FUN_ ## I)) "mle-" N "\0",
631 a_X(BELL, "bell")
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")
644 a_X(PASTE, "paste")
646 a_X(CANCEL, "cancel")
647 a_X(RESET, "reset")
648 a_X(FULLRESET, "fullreset")
649 a_X(COMMIT, "commit")
651 # undef a_X
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[] = {
659 # undef a_X
660 # define a_X(K,S) \
661 {TRU1, K, 0, {'\0', (char)a_TTY_BIND_FUN_REDUCE(a_TTY_BIND_FUN_ ## S),}},
663 a_X('A', GO_HOME)
664 a_X('B', GO_BWD)
665 /* C: SIGINT */
666 a_X('D', DEL_FWD)
667 a_X('E', GO_END)
668 a_X('F', GO_FWD)
669 a_X('G', RESET)
670 a_X('H', DEL_BWD)
671 a_X('I', COMPLETE)
672 a_X('J', COMMIT)
673 a_X('K', SNARF_END)
674 a_X('L', REPAINT)
675 /* M: same as J */
676 a_X('N', HIST_FWD)
677 /* O: below */
678 a_X('P', HIST_BWD)
679 a_X('Q', QUOTE_RNDTRIP)
680 a_X('R', HIST_SRCH_BWD)
681 a_X('S', HIST_SRCH_FWD)
682 a_X('T', PASTE)
683 a_X('U', SNARF_LINE)
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)
688 /* Z: SIGTSTP */
690 a_X('[', CANCEL)
691 /* \: below */
692 /* ]: below */
693 /* ^: below */
694 a_X('_', SNARF_WORD_FWD)
696 a_X('?', DEL_BWD)
698 # undef a_X
699 # define a_X(K,S) {TRU1, K, 0, {S}},
701 a_X('O', "dt")
702 a_X('\\', "z+")
703 a_X(']', "z$")
704 a_X('^', "z0")
706 # ifdef HAVE_KEY_BINDINGS
707 # undef a_X
708 # define a_X(Q,S) \
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)
719 # undef a_X
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 */
728 # undef a_X
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);
739 # endif
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
746 SMALLOC_DEBUG_ARGS);
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);
780 # ifdef HAVE_HISTORY
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);
785 # endif
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);
791 /* Readline core */
792 static ssize_t a_tty_readline(struct a_tty_line *tlp, size_t len
793 SMALLOC_DEBUG_ARGS);
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,
801 bool_t replace);
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 */
827 static void
828 a_tty_term_mode(bool_t raw){
829 struct termios *tiosp;
830 NYD2_ENTER;
832 tiosp = &a_tty.tg_tios_old;
833 if(!raw)
834 goto jleave;
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);
851 jleave:
852 tcsetattr(STDIN_FILENO, TCSADRAIN, tiosp);
853 NYD2_LEAVE;
856 # ifdef HAVE_KEY_BINDINGS
857 static void
858 a_tty_term_rawmode_timeout(struct a_tty_line *tlp, bool_t enable){
859 NYD2_ENTER;
860 if(enable){
861 ui8_t bt;
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;
867 }else{
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);
872 NYD2_LEAVE;
874 # endif /* HAVE_KEY_BINDINGS */
876 static ui8_t
877 a_tty_wcwidth(wchar_t wc){
878 ui8_t rv;
879 NYD2_ENTER;
881 /* Special case the backslash at first */
882 if(wc == '\t')
883 rv = UI8_MAX;
884 else{
885 int i;
887 # ifdef HAVE_WCWIDTH
888 rv = ((i = wcwidth(wc)) > 0) ? (ui8_t)i : 0;
889 # else
890 rv = iswprint(wc) ? 1 + (wc >= 0x1100u) : 0; /* TODO use S-CText */
891 # endif
893 NYD2_LEAVE;
894 return rv;
897 static void
898 a_tty_check_grow(struct a_tty_line *tlp, ui32_t no SMALLOC_DEBUG_ARGS){
899 ui32_t cmax;
900 NYD2_ENTER;
902 if(UNLIKELY((cmax = tlp->tl_count + no) > tlp->tl_count_max)){
903 size_t i;
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 */
908 i <<= 1;
909 tlp->tl_line.cbuf =
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;
916 NYD2_LEAVE;
919 static ssize_t
920 a_tty_cell2dat(struct a_tty_line *tlp){
921 size_t len, i;
922 NYD2_ENTER;
924 len = 0;
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';
937 NYD2_LEAVE;
938 return (ssize_t)len;
941 static void
942 a_tty_cell2save(struct a_tty_line *tlp){
943 size_t len, i;
944 struct a_tty_cell *tcap;
945 NYD2_ENTER;
947 tlp->tl_savec.s = NULL;
948 tlp->tl_savec.l = 0;
950 if(UNLIKELY(tlp->tl_count == 0))
951 goto jleave;
953 for(tcap = tlp->tl_line.cells, len = 0, i = tlp->tl_count; i > 0;
954 ++tcap, --i)
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;
960 ++tcap, --i){
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';
965 jleave:
966 NYD2_LEAVE;
969 static void
970 a_tty_copy2paste(struct a_tty_line *tlp, struct a_tty_cell *tcpmin,
971 struct a_tty_cell *tcpmax){
972 char *cp;
973 struct a_tty_cell *tcp;
974 size_t l;
975 NYD2_ENTER;
977 l = 0;
978 for(tcp = tcpmin; tcp < tcpmax; ++tcp)
979 l += tcp->tc_count;
981 tlp->tl_pastebuf.s = cp = salloc((tlp->tl_pastebuf.l = l) +1);
983 l = 0;
984 for(tcp = tcpmin; tcp < tcpmax; cp += l, ++tcp)
985 memcpy(cp, tcp->tc_cbuf, l = tcp->tc_count);
986 *cp = '\0';
987 NYD2_LEAVE;
990 static wchar_t
991 a_tty_vinuni(struct a_tty_line *tlp){
992 char buf[16], *eptr;
993 union {size_t i; long l;} u;
994 wchar_t wc;
995 NYD2_ENTER;
997 wc = '\0';
999 if(!n_termcap_cmdx(n_TERMCAP_CMD_cr) ||
1000 !n_termcap_cmd(n_TERMCAP_CMD_ce, 0, -1))
1001 goto jleave;
1003 /* C99 */{
1004 struct str const *cpre, *csuf;
1005 #ifdef HAVE_COLOUR
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();
1011 else
1012 csuf = NULL;
1013 #else
1014 cpre = csuf = NULL;
1015 #endif
1016 printf(_("%sPlease enter Unicode code point:%s "),
1017 (cpre != NULL ? cpre->s : ""), (csuf != NULL ? csuf->s : ""));
1019 fflush(stdout);
1021 buf[sizeof(buf) -1] = '\0';
1022 for(u.i = 0;;){
1023 if(read(STDIN_FILENO, &buf[u.i], 1) != 1){
1024 if(errno == EINTR) /* xxx #if !SA_RESTART ? */
1025 continue;
1026 goto jleave;
1028 if(buf[u.i] == '\n')
1029 break;
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);
1035 goto jerr;
1038 putc(buf[u.i], stdout);
1039 fflush(stdout);
1040 if(++u.i == sizeof buf)
1041 goto jerr;
1043 buf[u.i] = '\0';
1045 u.l = strtol(buf, &eptr, 16);
1046 if(u.l <= 0 || u.l >= 0x10FFFF/* XXX magic; CText */ || *eptr != '\0'){
1047 jerr:
1048 n_err(_("\nInvalid input: %s\n"), buf);
1049 goto jleave;
1052 wc = (wchar_t)u.l;
1053 jleave:
1054 tlp->tl_vi_flags |= a_TTY_VF_MOD_DIRTY | (wc == '\0' ? a_TTY_VF_BELL : 0);
1055 NYD2_LEAVE;
1056 return wc;
1059 static bool_t
1060 a_tty_vi_refresh(struct a_tty_line *tlp){
1061 bool_t rv;
1062 NYD2_ENTER;
1064 if(tlp->tl_vi_flags & a_TTY_VF_BELL){
1065 tlp->tl_vi_flags |= a_TTY_VF_SYNC;
1066 if(putchar('\a') == EOF)
1067 goto jerr;
1070 if(tlp->tl_vi_flags & a_TTY_VF_REFRESH){
1071 /* kht may want to restore a cursor position after inserting some
1072 * data somewhere */
1073 if(tlp->tl_defc_cursor_byte > 0){
1074 size_t i, j;
1075 ssize_t k;
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){
1082 a_tty_kright(tlp);
1083 if((k = tlp->tl_line.cells[j].tc_count) > i)
1084 break;
1085 i -= k;
1089 if(!a_tty_vi__paint(tlp))
1090 goto jerr;
1093 if(tlp->tl_vi_flags & a_TTY_VF_SYNC){
1094 tlp->tl_vi_flags &= ~a_TTY_VF_SYNC;
1095 if(fflush(stdout))
1096 goto jerr;
1099 rv = TRU1;
1100 jleave:
1101 tlp->tl_vi_flags &= ~a_TTY_VF_ALL_MASK;
1102 NYD2_LEAVE;
1103 return rv;
1105 jerr:
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);
1110 rv = FAL0;
1111 goto jleave;
1114 static bool_t
1115 a_tty_vi__paint(struct a_tty_line *tlp){
1116 enum{
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;
1134 NYD2_ENTER;
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)) |
1139 a_TTY_VF_SYNC;
1140 f |= a_TRUE_RV;
1141 if((w = tlp->tl_prompt_length) > 0)
1142 f |= a_HAVE_PROMPT;
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);
1150 else{
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;
1160 phy_base = 0;
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 */
1173 if(cnt == 0){
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))
1180 goto jerr;
1181 phy_cur = 0;
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)
1187 goto jerr;
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))
1194 goto jerr;
1196 tlp->tl_phy_start = tlp->tl_line.cells;
1197 goto jleave;
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)
1205 --cur;
1207 w = (tcp_left = tccp = tlp->tl_line.cells + cur)->tc_width;
1208 if(w == UI8_MAX) /* TODO yet TAB == SPC */
1209 w = 1;
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 */
1214 cw = 1;
1215 if(w + cw >= phy_wid)
1216 break;
1217 w += cw;
1218 --tcp_left;
1220 vi_left = w;
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)
1225 f |= a_LEFT_MIN;
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;
1230 f |= a_SHOW_PROMPT;
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;
1242 ui32_t i;
1244 if(cw == UI8_MAX) /* TODO yet TAB == SPC */
1245 cw = 1;
1246 i = w + cw;
1247 if(i > phy_wid)
1248 break;
1249 w = i;
1250 ++tccp;
1252 vi_right = w - vi_left;
1254 /* If the complete line including prompt fits on the screen, show prompt */
1255 if(--tcp_right == tccp){
1256 f |= a_RIGHT_MAX;
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 */
1273 i = 1;
1274 vil1 += i;
1275 i += w;
1276 if(i > phy_wid)
1277 break;
1278 w = i;
1279 if(--tcxp == tc1p){
1280 tcp_left = tc1p;
1281 vi_left = vil1;
1282 f |= a_LEFT_MIN;
1283 break;
1286 /*w -= tlp->tl_prompt_width;*/
1289 tcp_right = tccp;
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);
1296 f |= a_SHOW_PROMPT;
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;
1307 else{
1308 f |= a_TTY_VF_MOD_DIRTY;
1309 #if 0
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)){
1321 #endif
1323 goto jpaint;
1325 /* We know what we have to paint, start synchronizing */
1326 jpaint:
1327 assert(phy_cur == tlp->tl_phy_cursor);
1328 assert(phy_wid == phy_wid_base - phy_base);
1329 assert(cnt == tlp->tl_count);
1330 assert(cnt > 0);
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){
1338 f |= a_MOVE_CURSOR;
1339 goto jcursor;
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))
1347 goto jerr;
1348 tlp->tl_phy_start = tcp_left;
1349 phy_cur = 0;
1352 if((f & (a_TTY_VF_MOD_DIRTY | a_SHOW_PROMPT)) && phy_cur != 0){
1353 if(!n_termcap_cmdx(n_TERMCAP_CMD_cr))
1354 goto jerr;
1355 phy_cur = 0;
1358 if(f & a_SHOW_PROMPT){
1359 assert(phy_base == tlp->tl_prompt_width);
1360 if(fputs(tlp->tl_prompt, stdout) == EOF)
1361 goto jerr;
1362 phy_cur = phy_nxtcur;
1363 f |= a_VISIBLE_PROMPT;
1364 }else
1365 f &= ~a_VISIBLE_PROMPT;
1367 /* FIXME reposition cursor for paint */
1368 for(w = phy_nxtcur; tcp_left <= tcp_right; ++tcp_left){
1369 ui16_t cw;
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)
1376 goto jerr;
1377 }else{ /* XXX Shouldn't be here <-> CText, ui_str.c */
1378 char wbuf[8]; /* XXX magic */
1380 if(options & OPT_UNICODE){
1381 ui32_t wc;
1383 wc = (ui32_t)tcp_left->tc_wc;
1384 if((wc & ~0x1Fu) == 0)
1385 wc |= 0x2400;
1386 else if(wc == 0x7F)
1387 wc = 0x2421;
1388 else
1389 wc = 0x2426;
1390 n_utf32_to_utf8(wc, wbuf);
1391 }else
1392 wbuf[0] = '?', wbuf[1] = '\0';
1394 if(fputs(wbuf, stdout) == EOF)
1395 goto jerr;
1396 cw = 1;
1399 if(cw == UI8_MAX) /* TODO yet TAB == SPC */
1400 cw = 1;
1401 w += cw;
1402 if(tcp_left == tccp)
1403 phy_nxtcur = w;
1404 phy_cur += cw;
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)))){
1411 # ifdef HAVE_COLOUR
1412 char *posbuf = tlp->tl_pos_buf, *pos = tlp->tl_pos;
1413 # else
1414 char posbuf[5], *pos = posbuf;
1416 pos[4] = '\0';
1417 # endif
1419 if(phy_cur != (w = phy_wid_base) &&
1420 !n_termcap_cmd(n_TERMCAP_CMD_ch, phy_cur = w, 0))
1421 goto jerr;
1423 *pos++ = '|';
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);
1428 else{
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");
1439 if(i < 10)
1440 pos[0] = ' ', pos[1] = itoa[i];
1441 else
1442 pos[1] = itoa[i % 10], pos[0] = itoa[i / 10];
1443 pos[2] = '%';
1446 if(fputs(posbuf, stdout) == EOF)
1447 goto jerr;
1448 phy_cur += 4;
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 */
1460 cw = 1;
1461 phy_nxtcur -= cw;
1464 jcursor:
1465 if(((f & a_MOVE_CURSOR) || phy_nxtcur != phy_cur) &&
1466 !n_termcap_cmd(n_TERMCAP_CMD_ch, phy_cur = phy_nxtcur, 0))
1467 goto jerr;
1469 jleave:
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;
1475 NYD2_LEAVE;
1476 return ((f & a_TRUE_RV) != 0);
1477 jerr:
1478 f &= ~a_TRUE_RV;
1479 goto jleave;
1482 static si32_t
1483 a_tty_wboundary(struct a_tty_line *tlp, si32_t dir){/* TODO shell token-wise */
1484 bool_t anynon;
1485 struct a_tty_cell *tcap;
1486 ui32_t cur, cnt;
1487 si32_t rv;
1488 NYD2_ENTER;
1490 assert(dir == 1 || dir == -1);
1492 rv = -1;
1493 cnt = tlp->tl_count;
1494 cur = tlp->tl_cursor;
1496 if(dir < 0){
1497 if(cur == 0)
1498 goto jleave;
1499 }else if(cur + 1 >= cnt)
1500 goto jleave;
1501 else
1502 --cnt, --cur; /* xxx Unsigned wrapping may occur (twice), then */
1504 for(rv = 0, tcap = tlp->tl_line.cells, anynon = FAL0;;){
1505 wchar_t wc;
1507 wc = tcap[cur += (ui32_t)dir].tc_wc;
1508 if(iswblank(wc) || iswpunct(wc)){
1509 if(anynon)
1510 break;
1511 }else
1512 anynon = TRU1;
1514 ++rv;
1516 if(dir < 0){
1517 if(cur == 0)
1518 break;
1519 }else if(cur + 1 >= cnt){
1520 ++rv;
1521 break;
1524 jleave:
1525 NYD2_LEAVE;
1526 return rv;
1529 static void
1530 a_tty_khome(struct a_tty_line *tlp, bool_t dobell){
1531 ui32_t f;
1532 NYD2_ENTER;
1534 if(LIKELY(tlp->tl_cursor > 0)){
1535 tlp->tl_cursor = 0;
1536 f = a_TTY_VF_MOD_CURSOR;
1537 }else if(dobell)
1538 f = a_TTY_VF_BELL;
1539 else
1540 f = a_TTY_VF_NONE;
1542 tlp->tl_vi_flags |= f;
1543 NYD2_LEAVE;
1546 static void
1547 a_tty_kend(struct a_tty_line *tlp){
1548 ui32_t f;
1549 NYD2_ENTER;
1551 if(LIKELY(tlp->tl_cursor < tlp->tl_count)){
1552 tlp->tl_cursor = tlp->tl_count;
1553 f = a_TTY_VF_MOD_CURSOR;
1554 }else
1555 f = a_TTY_VF_BELL;
1557 tlp->tl_vi_flags |= f;
1558 NYD2_LEAVE;
1561 static void
1562 a_tty_kbs(struct a_tty_line *tlp){
1563 ui32_t f, cur, cnt;
1564 NYD2_ENTER;
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;
1580 }else
1581 f = a_TTY_VF_BELL;
1583 tlp->tl_vi_flags |= f;
1584 NYD2_LEAVE;
1587 static void
1588 a_tty_ksnarf(struct a_tty_line *tlp, bool_t cplline, bool_t dobell){
1589 ui32_t i, f;
1590 NYD2_ENTER;
1592 f = a_TTY_VF_NONE;
1593 i = tlp->tl_cursor;
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]);
1605 tlp->tl_count = i;
1606 f = a_TTY_VF_MOD_CONTENT;
1607 }else if(dobell)
1608 f |= a_TTY_VF_BELL;
1610 tlp->tl_vi_flags |= f;
1611 NYD2_LEAVE;
1614 static si32_t
1615 a_tty_kdel(struct a_tty_line *tlp){
1616 ui32_t cur, cnt, f;
1617 si32_t i;
1618 NYD2_ENTER;
1620 cur = tlp->tl_cursor;
1621 cnt = tlp->tl_count;
1622 i = (si32_t)(cnt - cur);
1624 if(LIKELY(i > 0)){
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)){
1635 putchar('^');
1636 putchar('D');
1637 i = -1;
1638 f = a_TTY_VF_NONE;
1639 }else{
1640 i = 0;
1641 f = a_TTY_VF_BELL;
1644 tlp->tl_vi_flags |= f;
1645 NYD2_LEAVE;
1646 return i;
1649 static void
1650 a_tty_kleft(struct a_tty_line *tlp){
1651 ui32_t f;
1652 NYD2_ENTER;
1654 if(LIKELY(tlp->tl_cursor > 0)){
1655 --tlp->tl_cursor;
1656 f = a_TTY_VF_MOD_CURSOR;
1657 }else
1658 f = a_TTY_VF_BELL;
1660 tlp->tl_vi_flags |= f;
1661 NYD2_LEAVE;
1664 static void
1665 a_tty_kright(struct a_tty_line *tlp){
1666 ui32_t i;
1667 NYD2_ENTER;
1669 if(LIKELY((i = tlp->tl_cursor + 1) <= tlp->tl_count)){
1670 tlp->tl_cursor = i;
1671 i = a_TTY_VF_MOD_CURSOR;
1672 }else
1673 i = a_TTY_VF_BELL;
1675 tlp->tl_vi_flags |= i;
1676 NYD2_LEAVE;
1679 static void
1680 a_tty_ksnarfw(struct a_tty_line *tlp, bool_t fwd){
1681 struct a_tty_cell *tcap;
1682 ui32_t cnt, cur, f;
1683 si32_t i;
1684 NYD2_ENTER;
1686 if(UNLIKELY((i = a_tty_wboundary(tlp, (fwd ? +1 : -1))) <= 0)){
1687 f = (i < 0) ? a_TTY_VF_BELL : a_TTY_VF_NONE;
1688 goto jleave;
1691 cnt = tlp->tl_count - (ui32_t)i;
1692 cur = tlp->tl_cursor;
1693 if(!fwd)
1694 cur -= (ui32_t)i;
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)){
1700 cnt -= cur;
1701 memmove(&tcap[0], &tcap[i], cnt * sizeof(*tcap)); /* FIXME*/
1704 f = a_TTY_VF_MOD_CURSOR | a_TTY_VF_MOD_CONTENT;
1705 jleave:
1706 tlp->tl_vi_flags |= f;
1707 NYD2_LEAVE;
1710 static void
1711 a_tty_kgow(struct a_tty_line *tlp, si32_t dir){
1712 ui32_t f;
1713 si32_t i;
1714 NYD2_ENTER;
1716 if(UNLIKELY((i = a_tty_wboundary(tlp, dir)) <= 0))
1717 f = (i < 0) ? a_TTY_VF_BELL : a_TTY_VF_NONE;
1718 else{
1719 if(dir < 0)
1720 i = -i;
1721 tlp->tl_cursor += (ui32_t)i;
1722 f = a_TTY_VF_MOD_CURSOR;
1725 tlp->tl_vi_flags |= f;
1726 NYD2_LEAVE;
1729 static bool_t
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 */
1733 mbstate_t ps;
1734 struct a_tty_cell tc, *tcap;
1735 ui32_t f, cur, cnt;
1736 bool_t rv;
1737 NYD2_ENTER;
1739 rv = FAL0;
1740 f = a_TTY_VF_NONE;
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"));
1745 goto jleave;
1748 /* First init a cell and see whether we'll really handle this wc */
1749 memset(&ps, 0, sizeof ps);
1750 /* C99 */{
1751 size_t l;
1753 l = wcrtomb(tc.tc_cbuf, tc.tc_wc = wc, &ps);
1754 if(UNLIKELY(l > MB_LEN_MAX)){
1755 jemb:
1756 n_err(_("wcrtomb(3) error: too many multibyte character bytes\n"));
1757 goto jleave;
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);
1763 if(LIKELY(l == 1))
1764 /* Only NUL terminator */;
1765 else if(LIKELY(--l < MB_LEN_MAX))
1766 tc.tc_count += (ui16_t)l;
1767 else
1768 goto jemb;
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];
1780 if(cnt >= 1){
1781 memmove(&tcap[1], tcap, cnt * sizeof(*tcap));
1782 f = a_TTY_VF_MOD_CONTENT;
1783 }else
1784 f = a_TTY_VF_MOD_SINGLE;
1785 memcpy(tcap, &tc, sizeof *tcap);
1787 f |= a_TTY_VF_MOD_CURSOR;
1788 rv = TRU1;
1789 jleave:
1790 if(!rv)
1791 f |= a_TTY_VF_BELL;
1792 tlp->tl_vi_flags |= f;
1793 NYD2_LEAVE;
1794 return rv;
1797 static ui32_t
1798 a_tty_kht(struct a_tty_line *tlp){
1799 struct stat sb;
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;
1804 ui32_t rv, f;
1805 NYD2_ENTER;
1807 f = a_TTY_VF_NONE;
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;
1815 if(orig.s != NULL){
1816 /*tlp->tl_savec = orig;*/
1817 set_savec = FAL0;
1818 }else
1819 set_savec = TRU1;
1820 orig = exp;
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 */
1829 if(cx > ctop){
1830 for(rv = 0; ctop < cx; ++ctop)
1831 rv += ctop->tc_count;
1832 topp.l = rv;
1833 topp.s = orig.s + orig.l - rv;
1834 ctop = cword + tlp->tl_cursor;
1835 }else
1836 topp.s = NULL, topp.l = 0;
1838 /* Find the shell token that corresponds to the cursor position */
1839 /* C99 */{
1840 size_t max;
1842 max = 0;
1843 if(ctop > cword){
1844 for(; cword < ctop; ++cword)
1845 max += cword->tc_count;
1846 cword = tlp->tl_line.cells;
1848 bot = sub = orig;
1849 bot.l = 0;
1850 sub.l = max;
1852 if(max > 0){
1853 for(;;){
1854 enum n_shexp_state shs;
1856 exp = sub;
1857 shs = n_shell_parse_token(NULL, &sub, n_SHEXP_PARSE_DRYRUN |
1858 n_SHEXP_PARSE_TRIMSPACE | n_SHEXP_PARSE_IGNORE_EMPTY);
1859 if(sub.l != 0){
1860 size_t x;
1862 assert(max >= sub.l);
1863 x = max - sub.l;
1864 bot.l += x;
1865 max -= x;
1866 continue;
1868 if(shs & n_SHEXP_STATE_ERR_MASK){
1869 n_err(_("Invalid completion pattern: %.*s\n"),
1870 (int)exp.l, exp.s);
1871 goto jnope;
1873 n_shell_parse_token(shoup, &exp,
1874 n_SHEXP_PARSE_TRIMSPACE | n_SHEXP_PARSE_IGNORE_EMPTY);
1875 break;
1878 sub.s = n_string_cp(shoup);
1879 sub.l = shoup->s_len;
1883 /* Leave room for "implicit asterisk" expansion, as below */
1884 if(sub.l == 0){
1885 wedid = TRU1;
1886 sub.s = UNCONST("*");
1887 sub.l = 1;
1890 wedid = FAL0;
1891 jredo:
1892 /* TODO Super-Heavy-Metal: block all sigs, avoid leaks on jump */
1893 hold_all_sigs();
1894 exp.s = fexpand(sub.s, a_TTY_TAB_FEXP_FL);
1895 rele_all_sigs();
1897 if(exp.s == NULL || (exp.l = strlen(exp.s)) == 0)
1898 goto jnope;
1900 /* May be multi-return! */
1901 if(pstate & PS_EXPAND_MULTIRESULT)
1902 goto jmulti;
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"));
1908 goto jnope;
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] == '*')
1915 goto jnope;
1917 wedid = TRU1;
1918 sub.s[sub.l++] = '*';
1919 sub.s[sub.l] = '\0';
1920 goto jredo;
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))
1927 goto jnope;
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';
1932 exp.s = sub.s;
1933 wedid = FAL0;
1934 goto jset;
1935 }else{
1936 if(wedid && (wedid = (exp.s[exp.l - 1] == '*')))
1937 --exp.l;
1938 exp.s[exp.l] = '\0';
1939 jset:
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;
1942 if(wedid)
1943 goto jnope;
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);
1951 rv += exp.l;
1952 if(topp.l > 0){
1953 memcpy(orig.s + rv, topp.s, topp.l);
1954 rv += topp.l;
1956 orig.s[rv] = '\0';
1958 tlp->tl_defc = orig;
1959 tlp->tl_count = tlp->tl_cursor = 0;
1960 f |= a_TTY_VF_MOD_DIRTY;
1961 jleave:
1962 n_string_gut(shoup);
1963 tlp->tl_vi_flags |= f;
1964 NYD2_LEAVE;
1965 return rv;
1967 jmulti:{
1968 struct n_visual_info_ctx vic;
1969 struct str input;
1970 wc_t c2, c1;
1971 bool_t isfirst;
1972 char const *lococp;
1973 size_t locolen, scrwid, lnlen, lncnt, prefixlen;
1974 FILE *fp;
1976 if((fp = Ftmp(NULL, "tabex", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL){
1977 n_perr(_("tmpfile"), 0);
1978 fp = stdout;
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 */
1984 locolen = 0;
1986 size_t i;
1988 i = strlen(&exp.s[++exp.l]);
1989 locolen = MAX(locolen, i);
1990 exp.l += 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);
1998 lnlen = lncnt = 0;
1999 UNINIT(prefixlen, 0);
2000 UNINIT(lococp, NULL);
2001 UNINIT(c1, '\0');
2002 for(isfirst = TRU1; exp.l > 0; isfirst = FAL0, c1 = c2){
2003 size_t i;
2004 char const *fullpath;
2006 /* Next result */
2007 sub = exp;
2008 sub.l = i = strlen(sub.s);
2009 assert(exp.l >= i);
2010 if((exp.l -= i) > 0)
2011 --exp.l;
2012 exp.s += ++i;
2014 /* Separate dirname and basename */
2015 fullpath = sub.s;
2016 if(isfirst){
2017 char const *cp;
2019 if((cp = strrchr(fullpath, '/')) != NULL)
2020 prefixlen = PTR2SIZE(++cp - fullpath);
2021 else
2022 prefixlen = 0;
2024 if(prefixlen > 0 && prefixlen < sub.l){
2025 sub.l -= prefixlen;
2026 sub.s += prefixlen;
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
2034 : (ui8_t)*sub.s;
2035 #ifdef HAVE_C90AMEND1
2036 c2 = towlower(c2);
2037 #else
2038 c2 = lowerconv(c2);
2039 #endif
2041 /* Query longest common prefix along the way */
2042 if(isfirst){
2043 c1 = c2;
2044 lococp = sub.s;
2045 locolen = sub.l;
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);
2050 locolen = i;
2051 break;
2055 /* Prepare display */
2056 input = sub;
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 */
2067 c1 = (c1 != c2);
2068 if(isfirst || c1 ||
2069 scrwid < lnlen || scrwid - lnlen <= vic.vic_vi_width + 2){
2070 putc('\n', fp);
2071 if(scrwid < lnlen)
2072 ++lncnt;
2073 ++lncnt, lnlen = 0;
2074 if(!isfirst && !c1)
2075 goto jsep;
2076 }else if(lnlen > 0){
2077 jsep:
2078 fputs(" ", fp);
2079 lnlen += 2;
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)){
2087 char c = '\0';
2089 if(S_ISDIR(sb.st_mode))
2090 c = '/';
2091 else if(S_ISLNK(sb.st_mode))
2092 c = '@';
2093 # ifdef S_ISFIFO
2094 else if(S_ISFIFO(sb.st_mode))
2095 c = '|';
2096 # endif
2097 # ifdef S_ISSOCK
2098 else if(S_ISSOCK(sb.st_mode))
2099 c = '=';
2100 # endif
2101 # ifdef S_ISCHR
2102 else if(S_ISCHR(sb.st_mode))
2103 c = '%';
2104 # endif
2105 # ifdef S_ISBLK
2106 else if(S_ISBLK(sb.st_mode))
2107 c = '#';
2108 # endif
2110 if(c != '\0'){
2111 putc(c, fp);
2112 ++lnlen;
2116 putc('\n', fp);
2117 ++lncnt;
2119 page_or_print(fp, lncnt);
2120 if(fp != stdout)
2121 Fclose(fp);
2123 n_string_gut(shoup);
2125 /* A common prefix of 0 means we cannot provide the user any auto
2126 * completed characters */
2127 if(locolen == 0)
2128 goto jnope;
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';
2133 exp.s -= prefixlen;
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 */
2138 wedid = FAL0;
2139 goto jset;
2142 jnope:
2143 /* If we've provided a default content, but failed to expand, there is
2144 * nothing we can "revert to": drop that default again */
2145 if(set_savec){
2146 tlp->tl_savec.s = NULL;
2147 tlp->tl_savec.l = 0;
2149 f = a_TTY_VF_NONE;
2150 rv = 0;
2151 goto jleave;
2154 # ifdef HAVE_HISTORY
2155 static ui32_t
2156 a_tty__khist_shared(struct a_tty_line *tlp, struct a_tty_hist *thp){
2157 ui32_t f, rv;
2158 NYD2_ENTER;
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;
2165 }else{
2166 f = a_TTY_VF_BELL;
2167 rv = 0;
2170 tlp->tl_vi_flags |= f;
2171 NYD2_LEAVE;
2172 return rv;
2175 static ui32_t
2176 a_tty_khist(struct a_tty_line *tlp, bool_t fwd){
2177 struct a_tty_hist *thp;
2178 ui32_t rv;
2179 NYD2_ENTER;
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){
2185 if(fwd)
2186 goto jleave;
2187 if((thp = a_tty.tg_hist) == NULL)
2188 goto jleave;
2189 a_tty_cell2save(tlp);
2190 goto jleave;
2193 thp = fwd ? thp->th_younger : thp->th_older;
2194 jleave:
2195 rv = a_tty__khist_shared(tlp, thp);
2196 NYD2_LEAVE;
2197 return rv;
2200 static ui32_t
2201 a_tty_khist_search(struct a_tty_line *tlp, bool_t fwd){
2202 struct str orig_savec;
2203 struct a_tty_hist *thp;
2204 ui32_t rv;
2205 NYD2_ENTER;
2207 thp = NULL;
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;
2216 goto jleave;
2219 if((thp = tlp->tl_hist) == NULL){
2220 if((thp = a_tty.tg_hist) == NULL)
2221 goto jleave;
2222 /* We don't support wraparound, searching forward must always step */
2223 if(fwd)
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)
2228 goto jleave;
2229 else
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))
2237 break;
2239 if(orig_savec.s != NULL)
2240 tlp->tl_savec = orig_savec;
2241 jleave:
2242 rv = a_tty__khist_shared(tlp, thp);
2243 NYD2_LEAVE;
2244 return rv;
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;
2251 NYD2_ENTER;
2253 rv = a_TTY_FUN_STATUS_OK;
2254 # undef a_X
2255 # define a_X(N) a_TTY_BIND_FUN_REDUCE(a_TTY_BIND_FUN_ ## N)
2256 switch(a_TTY_BIND_FUN_REDUCE(tbf)){
2257 case a_X(BELL):
2258 tlp->tl_vi_flags |= a_TTY_VF_BELL;
2259 break;
2260 case a_X(GO_BWD):
2261 a_tty_kleft(tlp);
2262 break;
2263 case a_X(GO_FWD):
2264 a_tty_kright(tlp);
2265 break;
2266 case a_X(GO_WORD_BWD):
2267 a_tty_kgow(tlp, -1);
2268 break;
2269 case a_X(GO_WORD_FWD):
2270 a_tty_kgow(tlp, +1);
2271 break;
2272 case a_X(GO_HOME):
2273 a_tty_khome(tlp, TRU1);
2274 break;
2275 case a_X(GO_END):
2276 a_tty_kend(tlp);
2277 break;
2278 case a_X(DEL_BWD):
2279 a_tty_kbs(tlp);
2280 break;
2281 case a_X(DEL_FWD):
2282 if(a_tty_kdel(tlp) < 0)
2283 rv = a_TTY_FUN_STATUS_END;
2284 break;
2285 case a_X(SNARF_WORD_BWD):
2286 a_tty_ksnarfw(tlp, FAL0);
2287 break;
2288 case a_X(SNARF_WORD_FWD):
2289 a_tty_ksnarfw(tlp, TRU1);
2290 break;
2291 case a_X(SNARF_END):
2292 a_tty_ksnarf(tlp, FAL0, TRU1);
2293 break;
2294 case a_X(SNARF_LINE):
2295 a_tty_ksnarf(tlp, TRU1, (tlp->tl_count == 0));
2296 break;
2298 case a_X(HIST_FWD):
2299 # ifdef HAVE_HISTORY
2300 if(tlp->tl_hist != NULL){
2301 bool_t isfwd = TRU1;
2303 if(0){
2304 # endif
2305 /* FALLTHRU */
2306 case a_X(HIST_BWD):
2307 # ifdef HAVE_HISTORY
2308 isfwd = FAL0;
2310 if((*len = a_tty_khist(tlp, isfwd)) > 0){
2311 rv = a_TTY_FUN_STATUS_RESTART;
2312 break;
2314 goto jreset;
2315 # endif
2317 tlp->tl_vi_flags |= a_TTY_VF_BELL;
2318 break;
2320 case a_X(HIST_SRCH_FWD):{
2321 # ifdef HAVE_HISTORY
2322 bool_t isfwd = TRU1;
2324 if(0){
2325 # endif
2326 /* FALLTHRU */
2327 case a_X(HIST_SRCH_BWD):
2328 # ifdef HAVE_HISTORY
2329 isfwd = FAL0;
2331 if((*len = a_tty_khist_search(tlp, isfwd)) > 0){
2332 rv = a_TTY_FUN_STATUS_RESTART;
2333 break;
2335 goto jreset;
2336 # else
2337 tlp->tl_vi_flags |= a_TTY_VF_BELL;
2338 # endif
2339 } break;
2341 case a_X(REPAINT):
2342 tlp->tl_vi_flags |= a_TTY_VF_MOD_DIRTY;
2343 break;
2344 case a_X(QUOTE_RNDTRIP):
2345 tlp->tl_quote_rndtrip = !tlp->tl_quote_rndtrip;
2346 break;
2347 case a_X(PROMPT_CHAR):{
2348 wchar_t wc;
2350 if((wc = a_tty_vinuni(tlp)) > 0)
2351 a_tty_kother(tlp, wc);
2352 } break;
2353 case a_X(COMPLETE):
2354 if((*len = a_tty_kht(tlp)) > 0)
2355 rv = a_TTY_FUN_STATUS_RESTART;
2356 break;
2358 case a_X(PASTE):
2359 if(tlp->tl_pastebuf.l > 0)
2360 *len = (tlp->tl_defc = tlp->tl_pastebuf).l;
2361 else
2362 tlp->tl_vi_flags |= a_TTY_VF_BELL;
2363 break;
2366 case a_X(CANCEL):
2367 /* Normally this just causes a restart and thus resets the state
2368 * machine */
2369 if(tlp->tl_savec.l == 0 && tlp->tl_defc.l == 0){
2371 # ifdef HAVE_KEY_BINDINGS
2372 tlp->tl_bind_takeover = '\0';
2373 # endif
2374 tlp->tl_vi_flags |= a_TTY_VF_BELL;
2375 rv = a_TTY_FUN_STATUS_RESTART;
2376 break;
2378 case a_X(RESET):
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';
2382 # endif
2383 tlp->tl_vi_flags |= a_TTY_VF_MOD_DIRTY | a_TTY_VF_BELL;
2384 break;
2385 }else if(0){
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;
2392 jreset:
2393 # ifdef HAVE_KEY_BINDINGS
2394 tlp->tl_bind_takeover = '\0';
2395 # endif
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;
2400 # endif
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;
2405 }else
2406 *len = tlp->tl_defc.l;
2407 rv = a_TTY_FUN_STATUS_RESTART;
2408 break;
2410 default:
2411 case a_X(COMMIT):
2412 rv = a_TTY_FUN_STATUS_COMMIT;
2413 break;
2415 # undef a_X
2417 NYD2_LEAVE;
2418 return rv;
2421 static ssize_t
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 */
2427 mbstate_t ps[2];
2428 char cbuf_base[MB_LEN_MAX * 2], *cbuf, *cbufp;
2429 ssize_t rv;
2430 struct a_tty_bind_tree *tbtp;
2431 wchar_t wc;
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)
2437 } flags;
2438 NYD_ENTER;
2440 UNINIT(rv, 0);
2441 # ifdef HAVE_KEY_BINDINGS
2442 assert(tlp->tl_bind_takeover == '\0');
2443 # endif
2444 jrestart:
2445 memset(ps, 0, sizeof ps);
2446 flags = a_NONE;
2447 tbf = 0;
2448 tlp->tl_vi_flags |= a_TTY_VF_REFRESH | a_TTY_VF_SYNC;
2450 jinput_loop:
2451 for(;;){
2452 if(len != 0)
2453 flags |= a_BUFMODE;
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)){
2462 rv = -1;
2463 goto jleave;
2466 /* Ready for messing around.
2467 * Normal read(2)? Else buffer mode: speed this one up */
2468 if(!(flags & a_BUFMODE)){
2469 cbufp =
2470 cbuf = cbuf_base;
2471 }else{
2472 assert(tlp->tl_defc.l > 0 && tlp->tl_defc.s != NULL);
2473 assert(tlp->tl_defc.l >= len);
2474 cbufp =
2475 cbuf = tlp->tl_defc.s + (tlp->tl_defc.l - len);
2476 cbufp += len;
2479 /* Read in the next complete multibyte character */
2480 /* C99 */{
2481 # ifdef HAVE_KEY_BINDINGS
2482 struct a_tty_bind_tree *xtbtp;
2483 struct inseq{
2484 struct inseq *last;
2485 struct inseq *next;
2486 struct a_tty_bind_tree *tbtp;
2487 } *isp_head, *isp;
2489 isp_head = isp = NULL;
2490 # endif
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';
2497 }else
2498 # endif
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);
2517 flags |= a_TIMEOUT;
2519 # endif
2521 while((rv = read(STDIN_FILENO, cbufp, 1)) < 1){
2522 if(rv == -1){
2523 if(errno == EINTR){
2524 if((tlp->tl_vi_flags & a_TTY_VF_MOD_DIRTY) &&
2525 !a_tty_vi_refresh(tlp))
2526 break;
2527 continue;
2529 break;
2532 # ifdef HAVE_KEY_BINDINGS
2533 /* Timeout expiration */
2534 if(rv == 0){
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;
2543 goto jhave_bind;
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);
2552 break;
2554 /* Lay down on read(2)? */
2555 if(xtbtp != NULL)
2556 continue;
2557 goto jtake_over;
2559 # endif /* HAVE_KEY_BINDINGS */
2562 # ifdef HAVE_KEY_BINDINGS
2563 if(flags & a_TIMEOUT)
2564 a_tty_term_rawmode_timeout(tlp, FAL0);
2565 # endif
2566 safe_signal(SIGTSTP, otstp);
2567 safe_signal(SIGTTIN, ottin);
2568 safe_signal(SIGTTOU, ottou);
2569 if(rv < 0)
2570 goto jleave;
2572 ++cbufp;
2575 rv = (ssize_t)mbrtowc(&wc, cbuf, PTR2SIZE(cbufp - cbuf), &ps[0]);
2576 if(rv <= 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
2580 * hard reset */
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);
2584 goto jrestart;
2586 /* Otherwise, due to the way we deal with the buffer, we need
2587 * to restore the mbstate_t from before this conversion */
2588 ps[0] = ps[1];
2589 continue;
2591 cbufp = cbuf;
2592 ps[1] = ps[0];
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)){
2601 char c;
2602 char const *cp;
2604 for(c = (char)wc, cp = &(*tlp->tl_bind_shcut_prompt_char)[0];
2605 *cp != '\0'; ++cp){
2606 if(c == *cp){
2607 wc = a_tty_vinuni(tlp);
2608 break;
2611 if(wc == '\0'){
2612 tlp->tl_vi_flags |= a_TTY_VF_BELL;
2613 goto jinput_loop;
2616 if(n_uasciichar(wc))
2617 flags |= a_MAYBEFUN;
2618 else
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){
2626 struct inseq *nisp;
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;
2632 goto jmle_fun;
2635 /* This needs to read more characters */
2636 nisp = salloc(sizeof *nisp);
2637 if((nisp->last = isp) == NULL)
2638 isp_head = nisp;
2639 else
2640 isp->next = nisp;
2641 nisp->next = NULL;
2642 nisp->tbtp = tbtp;
2643 isp = nisp;
2644 flags &= ~a_WAS_HERE;
2645 break;
2648 if(tbtp != NULL)
2649 continue;
2651 /* Was there a binding active, but couldn't be continued? */
2652 if(isp != NULL){
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);
2668 break;
2670 if(xtbtp != NULL){
2671 isp->tbtp = xtbtp;
2672 tlp->tl_bind_takeover = wc;
2673 continue;
2677 /* Check for CANCEL shortcut now */
2678 if(flags & a_MAYBEFUN){
2679 char c;
2680 char const *cp;
2682 for(c = (char)wc, cp = &(*tlp->tl_bind_shcut_cancel)[0];
2683 *cp != '\0'; ++cp)
2684 if(c == *cp){
2685 tbf = a_TTY_BIND_FUN_INTERNAL |a_TTY_BIND_FUN_CANCEL;
2686 goto jmle_fun;
2690 /* So: maybe the current sequence can be terminated here? */
2691 if((tbtp = isp->tbtp)->tbt_bind != NULL){
2692 jhave_bind:
2693 tbf = tbtp->tbt_bind->tbc_flags;
2694 jmle_fun:
2695 if(tbf & a_TTY_BIND_FUN_INTERNAL){
2696 switch(a_tty_fun(tlp, tbf, &len)){
2697 case a_TTY_FUN_STATUS_OK:
2698 goto jinput_loop;
2699 case a_TTY_FUN_STATUS_COMMIT:
2700 goto jdone;
2701 case a_TTY_FUN_STATUS_RESTART:
2702 goto jrestart;
2703 case a_TTY_FUN_STATUS_END:
2704 goto jleave;
2706 assert(0);
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);
2714 goto jrestart;
2715 }else{
2716 tlp->tl_reenter_after_cmd = tbtp->tbt_bind->tbc_exp;
2717 goto jdone;
2722 /* Otherwise take over all chars "as is" */
2723 jtake_over:
2724 for(; isp_head != NULL; isp_head = isp_head->next)
2725 if(a_tty_kother(tlp, isp_head->tbtp->tbt_char)){
2726 /* FIXME */
2728 /* And the current one too */
2729 goto jkother;
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;
2736 tlp->tl_defc.l = 0;
2737 flags &= ~a_BUFMODE;
2739 break;
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;
2750 char c;
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)]);
2755 ++tbdtp){
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:
2765 goto jinput_loop;
2766 case a_TTY_FUN_STATUS_COMMIT:
2767 goto jdone;
2768 case a_TTY_FUN_STATUS_RESTART:
2769 goto jrestart;
2770 case a_TTY_FUN_STATUS_END:
2771 goto jleave;
2773 assert(0);
2774 }else{
2775 tlp->tl_reenter_after_cmd = tbdtp->tbdt_exp;
2776 goto jdone;
2781 # endif /* !HAVE_KEY_BINDINGS */
2783 # ifdef HAVE_KEY_BINDINGS
2784 jkother:
2785 # endif
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;
2791 # endif
2796 /* We have a completed input line, convert the struct cell data to its
2797 * plain character equivalent */
2798 jdone:
2799 rv = a_tty_cell2dat(tlp);
2800 jleave:
2801 putchar('\n');
2802 fflush(stdout);
2803 NYD_LEAVE;
2804 return rv;
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;
2812 NYD2_ENTER;
2814 tbcmp = a_tty_bind_ctx_maps;
2815 do if(!asccasecmp(tbcmp->tbcm_name, name)){
2816 rv = tbcmp->tbcm_ctx;
2817 goto jleave;
2818 }while(PTRCMP(++tbcmp, <, &a_tty_bind_ctx_maps[NELEM(a_tty_bind_ctx_maps)]));
2820 rv = (enum n_lexinput_flags)-1;
2821 jleave:
2822 NYD2_LEAVE;
2823 return rv;
2826 static bool_t
2827 a_tty_bind_create(struct a_tty_bind_parse_ctx *tbpcp, bool_t replace){
2828 struct a_tty_bind_ctx *tbcp;
2829 bool_t rv;
2830 NYD2_ENTER;
2832 rv = FAL0;
2834 if(!a_tty_bind_parse(TRU1, tbpcp))
2835 goto jleave;
2837 /* Since we use a single buffer for it all, need to replace as such */
2838 if(tbpcp->tbpc_tbcp != NULL){
2839 if(!replace)
2840 goto jleave;
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"));
2844 goto jleave;
2847 /* C99 */{
2848 size_t i, j;
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;
2856 }else{
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)) ==
2874 a_TTY_BIND_RESOLVE)
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;
2881 rv = TRU1;
2882 jleave:
2883 NYD2_LEAVE;
2884 return rv;
2887 static bool_t
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;
2894 size_t i;
2895 struct kse{
2896 struct kse *next;
2897 char *seq_dat;
2898 wc_t *cnv_dat;
2899 ui32_t seq_len;
2900 ui32_t cnv_len; /* High bit set if a termap to be resolved */
2901 ui32_t calc_cnv_len; /* Ditto, but aligned etc. */
2902 } *head, *tail;
2903 ui32_t f;
2904 NYD2_ENTER;
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);
2910 head = tail = NULL;
2912 /* Parse the key-sequence */
2913 for(shin.s = UNCONST(tbpcp->tbpc_in_seq), shin.l = UIZ_MAX;;){
2914 struct kse *ep;
2915 enum n_shexp_state shs;
2917 shin_save = shin;
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);
2930 goto jleave;
2932 if((shs & (n_SHEXP_STATE_OUTPUT | n_SHEXP_STATE_STOP)) ==
2933 n_SHEXP_STATE_STOP)
2934 break;
2936 ep = salloc(sizeof *ep);
2937 if(head == NULL)
2938 head = ep;
2939 else
2940 tail->next = ep;
2941 tail = ep;
2942 ep->next = NULL;
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)
2946 goto jelen;
2947 ep->seq_len = (ui32_t)i;
2948 }else{
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)
2952 goto jelen;
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;
2966 goto jleave;
2967 }else if(vic.vic_woulen == 0 ||
2968 vic.vic_woulen >= (SI32_MAX - 2) / sizeof(wc_t)){
2969 jelen:
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;
2973 goto jleave;
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;
2986 # endif
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;
2992 while(i > 0)
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: "
2996 "%s: %s\n"),
2997 tbpcp->tbpc_cmd, ep->seq_dat, tbpcp->tbpc_in_seq);
2998 f |= a_TTY_BIND_DEFUNCT;
2999 break;
3001 ep->cnv_len |= SI32_MIN; /* Needs resolve */
3002 f |= a_TTY_BIND_RESOLVE;
3005 if(shs & n_SHEXP_STATE_STOP)
3006 break;
3009 if(head == NULL){
3010 jeempty:
3011 n_err(_("`%s': effectively empty key-sequence: %s\n"),
3012 tbpcp->tbpc_cmd, tbpcp->tbpc_in_seq);
3013 goto jleave;
3016 /* C99 */{
3017 struct a_tty_bind_ctx *ltbcp, *tbcp;
3018 char *cpbase, *cp, *cnv;
3019 size_t sl, cl;
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;
3025 if(!isbindcmd)
3026 continue;
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){
3031 /* For now
3032 * struct{si32_t buf_len_iscap; si32_t cap_len; wc_t name[]+NUL;}
3033 * later
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");
3038 i &= SI32_MAX;
3039 i *= sizeof(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);
3044 }else
3045 /* struct{si32_t buf_len_iscap; wc_t buf[]+NUL;} */
3046 i *= sizeof(wc_t);
3047 i += sizeof(si32_t) + sizeof(wc_t); /* (buf_len_iscap, NUL) */
3048 cl += i;
3049 if(tail->cnv_len & SI32_MIN){
3050 tail->cnv_len &= SI32_MAX;
3051 i |= SI32_MIN;
3053 tail->calc_cnv_len = (ui32_t)i;
3055 --sl;
3057 tbpcp->tbpc_seq_len = sl;
3058 tbpcp->tbpc_cnv_len = cl;
3059 /* C99 */{
3060 size_t j;
3062 j = i = sl + 1; /* Room for comma separator */
3063 if(isbindcmd){
3064 size_t const al = MAX(sizeof(si32_t), sizeof(wc_t)) - 1;
3066 i = (i + al) & ~al;
3067 j = i;
3068 i += cl;
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;
3077 *cp++ = ',';
3079 if(isbindcmd){
3080 char * const save_cnv = cnv;
3082 ((si32_t*)cnv)[0] = (si32_t)(i = tail->calc_cnv_len);
3083 cnv += sizeof(si32_t);
3084 if(i & SI32_MIN){
3085 /* For now
3086 * struct{si32_t buf_len_iscap; si32_t cap_len; wc_t name[];}
3087 * later
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);
3094 cnv += i;
3095 *(wc_t*)cnv = '\0';
3097 cnv = save_cnv + (tail->calc_cnv_len & SI32_MAX);
3100 *--cp = '\0';
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;
3107 break;
3109 tbpcp->tbpc_ltbcp = ltbcp;
3110 tbpcp->tbpc_flags |= (f & a_TTY__BIND_MASK);
3113 /* Create single string expansion if so desired */
3114 if(isbindcmd){
3115 char *exp;
3117 exp = tbpcp->tbpc_exp.s;
3119 i = tbpcp->tbpc_exp.l;
3120 if(i > 0 && exp[i - 1] == '@'){
3121 while(--i > 0){
3122 if(!blankspacechar(exp[i - 1]))
3123 break;
3125 if(i == 0)
3126 goto jeempty;
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;
3142 break;
3146 f |= a_TRUE_RV; /* TODO because we only now true and false; DEFUNCT.. */
3147 jleave:
3148 n_string_gut(shoup);
3149 NYD2_LEAVE;
3150 return (f & a_TRUE_RV) != 0;
3153 static void
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;
3157 size_t len;
3158 bool_t isfirst; /* TODO For now: first char must be control! */
3159 char *cp, *next;
3160 NYD2_ENTER;
3162 UNINIT(next, NULL);
3163 for(cp = tbcp->tbc_cnv, isfirst = TRU1, len = tbcp->tbc_cnv_len;
3164 len > 0; isfirst = FAL0, cp = next){
3165 /* C99 */{
3166 si32_t i, j;
3168 i = ((si32_t*)cp)[0];
3169 j = i & SI32_MAX;
3170 next = &cp[j];
3171 len -= j;
3172 if(i == j)
3173 continue;
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];
3181 capname[j] = '\0';
3184 /* Use generic lookup mechanism if not a known query */
3185 /* C99 */{
3186 si32_t tq;
3188 tq = n_termcap_query_for_name(capname, n_TERMCAP_CAPTYPE_STRING);
3189 if(tq == -1){
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;
3199 break;
3203 /* struct{si32_t buf_len_iscap; si32_t cap_len; char buf[]+NUL;} */
3204 /* C99 */{
3205 size_t i;
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;
3213 break;
3214 }else if(i == 0){
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;
3219 break;
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;
3225 break;
3227 ((si32_t*)cp)[-1] = (si32_t)i;
3228 memcpy(cp, tv.tv_data.tvd_string, i);
3229 cp[i] = '\0';
3232 NYD2_LEAVE;
3235 static void
3236 a_tty_bind_del(struct a_tty_bind_parse_ctx *tbpcp){
3237 struct a_tty_bind_ctx *ltbcp, *tbcp;
3238 NYD2_ENTER;
3240 tbcp = tbpcp->tbpc_tbcp;
3242 if((ltbcp = tbpcp->tbpc_ltbcp) != NULL)
3243 ltbcp->tbc_next = tbcp->tbc_next;
3244 else
3245 a_tty.tg_bind[tbpcp->tbpc_flags] = tbcp->tbc_next;
3246 free(tbcp);
3248 --a_tty.tg_bind_cnt;
3249 a_tty.tg_bind_isdirty = TRU1;
3250 NYD2_LEAVE;
3253 static void
3254 a_tty_bind_tree_build(void){
3255 size_t i;
3256 NYD2_ENTER;
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],
3268 tbcp);
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;
3277 NYD2_LEAVE;
3280 static void
3281 a_tty_bind_tree_teardown(void){
3282 size_t i, j;
3283 NYD2_ENTER;
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;
3296 NYD2_LEAVE;
3299 static void
3300 a_tty__bind_tree_add(ui32_t hmap_idx, struct a_tty_bind_tree *store[HSHSIZE],
3301 struct a_tty_bind_ctx *tbcp){
3302 ui32_t cnvlen;
3303 char const *cnvdat;
3304 struct a_tty_bind_tree *ntbtp;
3305 NYD2_ENTER;
3306 UNUSED(hmap_idx);
3308 ntbtp = NULL;
3310 for(cnvdat = tbcp->tbc_cnv, cnvlen = tbcp->tbc_cnv_len; cnvlen > 0;){
3311 union {wchar_t const *wp; char const *cp;} u;
3312 si32_t entlen;
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;
3324 entlen &= SI32_MAX;
3325 }else{
3326 /* struct{si32_t buf_len_iscap; wc_t buf[]+NUL;} */
3327 bool_t isseq;
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)){
3333 char *cp;
3334 ui32_t ctx, fun;
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)
3343 if(*cp == '\0'){
3344 *cp = (char)*u.wp;
3345 break;
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]);
3351 ++cp)
3352 if(*cp == '\0'){
3353 *cp = (char)*u.wp;
3354 break;
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);
3362 if(isseq)
3363 ntbtp->tbt_isseq_trail = TRU1;
3366 cnvlen -= entlen;
3367 cnvdat += entlen;
3370 /* Should have been rendered defunctional at first instead */
3371 assert(ntbtp != NULL);
3372 ntbtp->tbt_bind = tbcp;
3373 NYD2_LEAVE;
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;
3380 NYD2_ENTER;
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)
3389 continue;
3390 if(tbtp->tbt_isseq == isseq)
3391 goto jleave;
3392 /* isseq MUST be linked before !isseq, so record this "parent"
3393 * sibling, but continue searching for now */
3394 if(!isseq)
3395 parentp = tbtp;
3396 /* Otherwise it is impossible that we'll find what we look for */
3397 else{
3398 #ifdef HAVE_DEBUG
3399 while((tbtp = tbtp->tbt_sibling) != NULL)
3400 assert(tbtp->tbt_char != wc);
3401 #endif
3402 break;
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;
3413 *treep = tbtp;
3414 }else{
3415 tbtp->tbt_sibling = parentp->tbt_sibling;
3416 parentp->tbt_sibling = tbtp;
3418 }else{
3419 if((tbtp = *(treep = &parentp->tbt_childs)) != NULL){
3420 for(;; tbtp = xtbtp){
3421 if(tbtp->tbt_char == wc){
3422 if(tbtp->tbt_isseq == isseq)
3423 goto jleave;
3424 /* isseq MUST be linked before, so it is impossible that we'll
3425 * find what we look for */
3426 if(isseq){
3427 #ifdef HAVE_DEBUG
3428 while((tbtp = tbtp->tbt_sibling) != NULL)
3429 assert(tbtp->tbt_char != wc);
3430 #endif
3431 tbtp = NULL;
3432 break;
3436 if((xtbtp = tbtp->tbt_sibling) == NULL){
3437 treep = &tbtp->tbt_sibling;
3438 break;
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;
3448 tbtp = xtbtp;
3449 *treep = tbtp;
3451 jleave:
3452 NYD2_LEAVE;
3453 return tbtp;
3456 static void
3457 a_tty__bind_tree_free(struct a_tty_bind_tree *tbtp){
3458 NYD2_ENTER;
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;
3466 free(tbtp);
3467 tbtp = tmp;
3469 NYD2_LEAVE;
3471 # endif /* HAVE_KEY_BINDINGS */
3473 FL void
3474 n_tty_init(void){
3475 NYD_ENTER;
3477 /* Load the history file */
3478 # ifdef HAVE_HISTORY
3479 do/* for break */{
3480 long hs;
3481 char const *v;
3482 char *lbuf;
3483 FILE *f;
3484 size_t lsize, cnt, llen;
3486 a_TTY_HISTSIZE(hs);
3487 a_tty.tg_hist_size = 0;
3488 a_tty.tg_hist_size_max = (size_t)hs;
3489 if(hs == 0)
3490 break;
3492 a_TTY_HISTFILE(v);
3493 if(v == NULL)
3494 break;
3496 hold_all_sigs(); /* TODO too heavy, yet we may jump even here!? */
3497 f = fopen(v, "r"); /* TODO HISTFILE LOAD: use linebuf pool */
3498 if(f == NULL)
3499 goto jhist_done;
3500 (void)n_file_lock(fileno(f), FLT_READ, 0,0, UIZ_MAX);
3502 lbuf = NULL;
3503 lsize = 0;
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! */
3509 continue;
3510 else{
3511 bool_t isgabby;
3513 isgabby = (lbuf[0] == '*');
3514 n_tty_addhist(lbuf + isgabby, isgabby);
3517 if(lbuf != NULL)
3518 free(lbuf);
3520 fclose(f);
3521 jhist_done:
3522 rele_all_sigs(); /* XXX remove jumps */
3523 }while(0);
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 */
3533 /* C99 */{
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)) ==
3540 a_TTY_BIND_RESOLVE)
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)){
3546 char buf[8];
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;
3553 PTRCMP(tbdtp, <,
3554 &a_tty_bind_default_tuples[NELEM(a_tty_bind_default_tuples)]);
3555 ++tbdtp){
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;
3561 }else
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]]
3566 : tbdtp->tbdt_exp);
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 */
3575 NYD_LEAVE;
3578 FL void
3579 n_tty_destroy(void){
3580 NYD_ENTER;
3582 # ifdef HAVE_HISTORY
3583 do/* for break */{
3584 long hs;
3585 char const *v;
3586 struct a_tty_hist *thp;
3587 bool_t dogabby;
3588 FILE *f;
3590 a_TTY_HISTSIZE(hs);
3591 if(hs == 0)
3592 break;
3594 a_TTY_HISTFILE(v);
3595 if(v == NULL)
3596 break;
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)
3603 break;
3605 hold_all_sigs(); /* TODO too heavy, yet we may jump even here!? */
3606 f = fopen(v, "w"); /* TODO temporary + rename?! */
3607 if(f == NULL)
3608 goto jhist_done;
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){
3613 if(thp->th_isgabby)
3614 putc('*', f);
3615 fwrite(thp->th_dat, sizeof *thp->th_dat, thp->th_len, f);
3616 putc('\n', f);
3619 fclose(f);
3620 jhist_done:
3621 rele_all_sigs(); /* XXX remove jumps */
3622 }while(0);
3623 # endif /* HAVE_HISTORY */
3625 # if defined HAVE_KEY_BINDINGS && defined HAVE_DEBUG
3626 c_unbind(UNCONST("* *"));
3627 # endif
3629 #ifdef HAVE_DEBUG
3630 memset(&a_tty, 0, sizeof a_tty);
3631 #endif
3632 DBG( pstate &= ~PS_LINE_EDITOR_INIT; )
3633 NYD_LEAVE;
3636 FL void
3637 n_tty_signal(int sig){
3638 sigset_t nset, oset;
3639 NYD_X; /* Signal handler */
3641 switch(sig){
3642 case SIGWINCH:
3643 /* We don't deal with SIGWINCH, yet get called from main.c */
3644 break;
3645 default:
3646 a_tty_term_mode(FAL0);
3647 n_TERMCAP_SUSPEND(TRU1);
3648 a_tty_sigs_down();
3650 sigemptyset(&nset);
3651 sigaddset(&nset, sig);
3652 sigprocmask(SIG_UNBLOCK, &nset, &oset);
3653 n_raise(sig);
3654 /* When we come here we'll continue editing, so reestablish */
3655 sigprocmask(SIG_BLOCK, &oset, (sigset_t*)NULL);
3657 a_tty_sigs_up();
3658 n_TERMCAP_RESUME(TRU1);
3659 a_tty_term_mode(TRU1);
3660 a_tty.tg_line->tl_vi_flags |= a_TTY_VF_MOD_DIRTY;
3661 break;
3665 FL int
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;
3669 # ifdef HAVE_COLOUR
3670 char *posbuf, *pos;
3671 # endif
3672 ui32_t plen, pwidth;
3673 ssize_t nn;
3674 char const *orig_prompt;
3675 NYD_ENTER;
3677 assert(pstate & PS_LINE_EDITOR_INIT);
3679 orig_prompt = prompt;
3680 jredo:
3681 /* TODO because of jumping away we cannot use srelax()ation: would be good */
3682 prompt = orig_prompt;
3684 # ifdef HAVE_COLOUR
3685 n_colour_env_create(n_COLOUR_CTX_MLE, FAL0);
3686 # endif
3688 /* Classify prompt */
3689 UNINIT(plen, 0);
3690 UNINIT(pwidth, 0);
3691 if(prompt != NULL){
3692 size_t i = strlen(prompt);
3694 if(i == 0 || i >= UI32_MAX)
3695 prompt = NULL;
3696 else{
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;
3706 vic.vic_inlen = i;
3707 if(n_visual_info(&vic, n_VISUAL_INFO_WIDTH_QUERY)){
3708 pwidth = (ui32_t)vic.vic_vi_width;
3709 plen = (ui32_t)i;
3710 }else{
3711 n_err(_("Character set error in evaluation of prompt\n"));
3712 prompt = NULL;
3717 # ifdef HAVE_COLOUR
3718 /* C99 */{
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);
3736 prompt = nprompt;
3737 plen = nplen;
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);
3752 pos = &posbuf[l1];
3753 memcpy(&pos[4], sp->s, ++l2);
3756 if(posbuf == NULL){
3757 posbuf = pos = salloc(4 +1);
3758 pos[4] = '\0';
3761 # endif /* HAVE_COLOUR */
3763 memset(&tl, 0, sizeof tl);
3765 # ifdef HAVE_KEY_BINDINGS
3766 /* C99 */{
3767 char const *cp = ok_vlook(bind_timeout);
3769 if(cp != NULL){
3770 ul_i ul;
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;
3797 # ifdef HAVE_COLOUR
3798 tl.tl_pos_buf = posbuf;
3799 tl.tl_pos = pos;
3800 # endif
3802 tl.tl_line.cbuf = *linebuf;
3803 if(n != 0){
3804 tl.tl_defc.s = savestrbuf(*linebuf, n);
3805 tl.tl_defc.l = n;
3807 tl.tl_x_buf = linebuf;
3808 tl.tl_x_bufsize = linesize;
3810 a_tty.tg_line = &tl;
3811 a_tty_sigs_up();
3812 a_tty_term_mode(TRU1);
3813 nn = a_tty_readline(&tl, n SMALLOC_DEBUG_ARGSCALL);
3814 a_tty_term_mode(FAL0);
3815 a_tty_sigs_down();
3816 a_tty.tg_line = NULL;
3818 # ifdef HAVE_COLOUR
3819 n_colour_env_gut(stdout);
3820 # endif
3822 if(tl.tl_reenter_after_cmd != NULL){
3823 n = (nn <= 0) ? 0 : nn;
3824 n_source_command(tl.tl_reenter_after_cmd);
3825 goto jredo;
3827 NYD_LEAVE;
3828 return (int)nn;
3831 FL void
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 */
3835 ui32_t l;
3836 struct a_tty_hist *thp, *othp, *ythp;
3837 # endif
3838 NYD_ENTER;
3839 UNUSED(s);
3840 UNUSED(isgabby);
3842 # ifdef HAVE_HISTORY
3843 if(isgabby && !ok_blook(history_gabby))
3844 goto j_leave;
3846 if(a_tty.tg_hist_size_max == 0)
3847 goto j_leave;
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 */
3858 if(thp->th_isgabby)
3859 thp->th_isgabby = !!isgabby;
3860 othp = thp->th_older;
3861 ythp = thp->th_younger;
3862 if(othp != NULL)
3863 othp->th_younger = ythp;
3864 else
3865 a_tty.tg_hist_tail = ythp;
3866 if(ythp != NULL)
3867 ythp->th_older = othp;
3868 else
3869 a_tty.tg_hist = othp;
3870 goto jleave;
3872 hold_all_sigs();
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;
3881 else
3882 a_tty.tg_hist_tail->th_older = NULL;
3883 free(thp);
3887 thp = smalloc((sizeof(struct a_tty_hist) -
3888 VFIELD_SIZEOF(struct a_tty_hist, th_dat)) + l +1);
3889 thp->th_isgabby = !!isgabby;
3890 thp->th_len = l;
3891 memcpy(thp->th_dat, s, l +1);
3892 jleave:
3893 if((thp->th_older = a_tty.tg_hist) != NULL)
3894 a_tty.tg_hist->th_younger = thp;
3895 else
3896 a_tty.tg_hist_tail = thp;
3897 thp->th_younger = NULL;
3898 a_tty.tg_hist = thp;
3900 rele_all_sigs();
3901 j_leave:
3902 # endif
3903 NYD_LEAVE;
3906 # ifdef HAVE_HISTORY
3907 FL int
3908 c_history(void *v){
3909 C_HISTORY_SHARED;
3911 jlist:{
3912 FILE *fp;
3913 size_t i, b;
3914 struct a_tty_hist *thp;
3916 if(a_tty.tg_hist == NULL)
3917 goto jleave;
3919 if((fp = Ftmp(NULL, "hist", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL){
3920 n_perr(_("tmpfile"), 0);
3921 v = NULL;
3922 goto jleave;
3925 i = a_tty.tg_hist_size;
3926 b = 0;
3927 for(thp = a_tty.tg_hist; thp != NULL;
3928 --i, b += thp->th_len, thp = thp->th_older)
3929 fprintf(fp,
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);
3934 Fclose(fp);
3936 goto jleave;
3938 jclear:{
3939 struct a_tty_hist *thp;
3941 while((thp = a_tty.tg_hist) != NULL){
3942 a_tty.tg_hist = thp->th_older;
3943 free(thp);
3945 a_tty.tg_hist_tail = NULL;
3946 a_tty.tg_hist_size = 0;
3948 goto jleave;
3950 jentry:{
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)
3956 if(thp == NULL)
3957 break;
3958 else if(entry-- != 0)
3959 continue;
3960 else{
3961 v = temporary_arg_v_store = thp->th_dat;
3962 goto jleave;
3965 v = NULL;
3967 goto jleave;
3969 # endif /* HAVE_HISTORY */
3971 # ifdef HAVE_KEY_BINDINGS
3972 FL int
3973 c_bind(void *v){
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;
3986 bool_t aster, show;
3987 union {char const *cp; char *p; char c;} c;
3988 NYD_ENTER;
3990 cac.cac_desc = n_CMD_ARG_DESC_SUBCLASS_CAST(&a_tty_bind_cad);
3991 cac.cac_indat = v;
3992 cac.cac_inlen = UIZ_MAX;
3993 if(!n_cmd_arg_parse(&cac)){
3994 v = NULL;
3995 goto jleave;
3998 c.cp = cac.cac_arg->ca_arg.ca_str.s;
3999 if(cac.cac_no == 1)
4000 show = TRU1;
4001 else
4002 show = !asccasecmp(cac.cac_arg->ca_next->ca_arg.ca_str.s, "show");
4003 aster = FAL0;
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);
4008 v = NULL;
4009 goto jleave;
4011 lif = 0;
4014 if(show){
4015 ui32_t lns;
4016 FILE *fp;
4018 if((fp = Ftmp(NULL, "bind", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL){
4019 n_perr(_("tmpfile"), 0);
4020 v = NULL;
4021 goto jleave;
4024 lns = 0;
4025 for(;;){
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){
4032 char cbuf[8];
4033 union {wchar_t const *wp; char const *cp;} u;
4034 si32_t entlen;
4035 ui32_t cnvlen;
4036 char const *cnvdat, *bsep, *cbufp;
4038 putc('#', fp);
4039 putc(' ', fp);
4041 cbuf[0] = '=', cbuf[2] = '\0';
4042 for(cnvdat = tbcp->tbc_cnv, cnvlen = tbcp->tbc_cnv_len;
4043 cnvlen > 0;){
4044 if(cnvdat != tbcp->tbc_cnv)
4045 putc(',', fp);
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;} */
4052 for(bsep = "",
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;
4057 else
4058 cbufp = "";
4059 fprintf(fp, "%s%02X%s",
4060 bsep, (ui32_t)(ui8_t)c.c, cbufp);
4061 bsep = " ";
4063 entlen &= SI32_MAX;
4064 }else
4065 putc('-', fp);
4067 cnvlen -= entlen;
4068 cnvdat += entlen;
4071 fputs("\n ", fp);
4072 ++lns;
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)
4090 break;
4092 page_or_print(fp, lns);
4094 Fclose(fp);
4095 }else{
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))
4107 v = NULL;
4108 n_string_gut(&store);
4110 jleave:
4111 NYD_LEAVE;
4112 return (v != NULL) ? EXIT_OK : EXIT_ERR;
4115 FL int
4116 c_unbind(void *v){
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;
4126 bool_t aster;
4127 union {char const *cp; char *p;} c;
4128 NYD_ENTER;
4130 cac.cac_desc = n_CMD_ARG_DESC_SUBCLASS_CAST(&a_tty_unbind_cad);
4131 cac.cac_indat = v;
4132 cac.cac_inlen = UIZ_MAX;
4133 if(!n_cmd_arg_parse(&cac)){
4134 v = NULL;
4135 goto jleave;
4138 c.cp = cac.cac_arg->ca_arg.ca_str.s;
4139 aster = FAL0;
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);
4144 v = NULL;
4145 goto jleave;
4147 lif = 0;
4150 c.cp = cac.cac_arg->ca_next->ca_arg.ca_str.s;
4151 jredo:
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);
4159 }else{
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)))
4166 v = NULL;
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);
4170 v = NULL;
4171 }else
4172 a_tty_bind_del(&tbpc);
4175 if(aster && ++lif < n__LEXINPUT_CTX_MAX)
4176 goto jredo;
4177 jleave:
4178 NYD_LEAVE;
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
4189 FL void
4190 n_tty_init(void){
4191 NYD_ENTER;
4192 NYD_LEAVE;
4195 FL void
4196 n_tty_destroy(void){
4197 NYD_ENTER;
4198 NYD_LEAVE;
4201 FL void
4202 n_tty_signal(int sig){
4203 NYD_X; /* Signal handler */
4204 UNUSED(sig);
4206 # ifdef HAVE_TERMCAP
4207 switch(sig){
4208 default:{
4209 sigset_t nset, oset;
4211 n_TERMCAP_SUSPEND(TRU1);
4212 a_tty_sigs_down();
4214 sigemptyset(&nset);
4215 sigaddset(&nset, sig);
4216 sigprocmask(SIG_UNBLOCK, &nset, &oset);
4217 n_raise(sig);
4218 /* When we come here we'll continue editing, so reestablish */
4219 sigprocmask(SIG_BLOCK, &oset, (sigset_t*)NULL);
4221 a_tty_sigs_up();
4222 n_TERMCAP_RESUME(TRU1);
4223 break;
4226 # endif /* HAVE_TERMCAP */
4229 FL int
4230 (n_tty_readline)(char const *prompt, char **linebuf, size_t *linesize, size_t n
4231 SMALLOC_DEBUG_ARGS){
4232 int rv;
4233 NYD_ENTER;
4235 if(prompt != NULL){
4236 if(*prompt != '\0')
4237 fputs(prompt, stdout);
4238 fflush(stdout);
4240 # ifdef HAVE_TERMCAP
4241 a_tty_sigs_up();
4242 # endif
4243 rv = (readline_restart)(stdin, linebuf, linesize,n SMALLOC_DEBUG_ARGSCALL);
4244 # ifdef HAVE_TERMCAP
4245 a_tty_sigs_down();
4246 # endif
4247 NYD_LEAVE;
4248 return rv;
4251 FL void
4252 n_tty_addhist(char const *s, bool_t isgabby){
4253 NYD_ENTER;
4254 UNUSED(s);
4255 UNUSED(isgabby);
4256 NYD_LEAVE;
4258 #endif /* nothing at all */
4260 #undef a_TTY_SIGNALS
4261 /* s-it-mode */