HTML filter: support ‌
[s-mailx.git] / tty.c
blob865aa72228d50869d991bad534c0c30c17254d18
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ TTY (command line) editing interaction.
3 *@ Because we have (had) multiple line-editor implementations, including our
4 *@ own M(ailx) L(ine) E(ditor), change the file layout a bit and place those
5 *@ one after the other below the other externals.
7 * Copyright (c) 2012 - 2018 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
8 * SPDX-License-Identifier: ISC
10 * Permission to use, copy, modify, and/or distribute this software for any
11 * purpose with or without fee is hereby granted, provided that the above
12 * copyright notice and this permission notice appear in all copies.
14 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #undef n_FILE
23 #define n_FILE tty
25 #ifndef HAVE_AMALGAMATION
26 # include "nail.h"
27 #endif
29 #if defined HAVE_MLE || defined HAVE_TERMCAP
30 # define a_TTY_SIGNALS
31 #endif
33 #ifdef a_TTY_SIGNALS
34 static sighandler_type a_tty_oint, a_tty_oquit, a_tty_oterm,
35 a_tty_ohup,
36 a_tty_otstp, a_tty_ottin, a_tty_ottou;
37 #endif
39 #ifdef a_TTY_SIGNALS
40 /**/
41 static void a_tty_sigs_up(void), a_tty_sigs_down(void);
43 /* Is editor specific code */
44 static void a_tty_signal(int sig);
45 #endif
47 #ifdef a_TTY_SIGNALS
48 static void
49 a_tty_sigs_up(void){
50 sigset_t nset, oset;
51 NYD2_ENTER;
53 sigfillset(&nset);
55 sigprocmask(SIG_BLOCK, &nset, &oset);
56 a_tty_oint = safe_signal(SIGINT, &a_tty_signal);
57 a_tty_oquit = safe_signal(SIGQUIT, &a_tty_signal);
58 a_tty_oterm = safe_signal(SIGTERM, &a_tty_signal);
59 a_tty_ohup = safe_signal(SIGHUP, &a_tty_signal);
60 a_tty_otstp = safe_signal(SIGTSTP, &a_tty_signal);
61 a_tty_ottin = safe_signal(SIGTTIN, &a_tty_signal);
62 a_tty_ottou = safe_signal(SIGTTOU, &a_tty_signal);
63 sigprocmask(SIG_SETMASK, &oset, NULL);
64 NYD2_LEAVE;
67 static void
68 a_tty_sigs_down(void){
69 sigset_t nset, oset;
70 NYD2_ENTER;
72 sigfillset(&nset);
74 sigprocmask(SIG_BLOCK, &nset, &oset);
75 safe_signal(SIGINT, a_tty_oint);
76 safe_signal(SIGQUIT, a_tty_oquit);
77 safe_signal(SIGTERM, a_tty_oterm);
78 safe_signal(SIGHUP, a_tty_ohup);
79 safe_signal(SIGTSTP, a_tty_otstp);
80 safe_signal(SIGTTIN, a_tty_ottin);
81 safe_signal(SIGTTOU, a_tty_ottou);
82 sigprocmask(SIG_SETMASK, &oset, NULL);
83 NYD2_LEAVE;
85 #endif /* a_TTY_SIGNALS */
87 static sigjmp_buf a_tty__actjmp; /* TODO someday, we won't need it no more */
88 static void
89 a_tty__acthdl(int s) /* TODO someday, we won't need it no more */
91 NYD_X; /* Signal handler */
92 siglongjmp(a_tty__actjmp, s);
95 FL bool_t
96 getapproval(char const * volatile prompt, bool_t noninteract_default)
98 sighandler_type volatile oint, ohup;
99 bool_t volatile rv;
100 int volatile sig;
101 NYD_ENTER;
103 if(!(n_psonce & n_PSO_INTERACTIVE) || (n_pstate & n_PS_ROBOT)){
104 sig = 0;
105 rv = noninteract_default;
106 goto jleave;
108 rv = FAL0;
110 /* C99 */{
111 char const *quest = noninteract_default
112 ? _("[yes]/no? ") : _("[no]/yes? ");
114 if (prompt == NULL)
115 prompt = _("Continue");
116 prompt = savecatsep(prompt, ' ', quest);
119 oint = safe_signal(SIGINT, SIG_IGN);
120 ohup = safe_signal(SIGHUP, SIG_IGN);
121 if ((sig = sigsetjmp(a_tty__actjmp, 1)) != 0)
122 goto jrestore;
123 safe_signal(SIGINT, &a_tty__acthdl);
124 safe_signal(SIGHUP, &a_tty__acthdl);
126 while(n_go_input(n_GO_INPUT_CTX_DEFAULT | n_GO_INPUT_NL_ESC, prompt,
127 &termios_state.ts_linebuf, &termios_state.ts_linesize, NULL,NULL
128 ) >= 0){
129 bool_t x;
131 x = n_boolify(termios_state.ts_linebuf, UIZ_MAX, noninteract_default);
132 if(x >= FAL0){
133 rv = x;
134 break;
137 jrestore:
138 safe_signal(SIGHUP, ohup);
139 safe_signal(SIGINT, oint);
140 jleave:
141 NYD_LEAVE;
142 if (sig != 0)
143 n_raise(sig);
144 return rv;
147 #ifdef HAVE_SOCKETS
148 FL char *
149 getuser(char const * volatile query) /* TODO v15-compat obsolete */
151 sighandler_type volatile oint, ohup;
152 char * volatile user = NULL;
153 int volatile sig;
154 NYD_ENTER;
156 if (query == NULL)
157 query = _("User: ");
159 oint = safe_signal(SIGINT, SIG_IGN);
160 ohup = safe_signal(SIGHUP, SIG_IGN);
161 if ((sig = sigsetjmp(a_tty__actjmp, 1)) != 0)
162 goto jrestore;
163 safe_signal(SIGINT, &a_tty__acthdl);
164 safe_signal(SIGHUP, &a_tty__acthdl);
166 if (n_go_input(n_GO_INPUT_CTX_DEFAULT | n_GO_INPUT_NL_ESC, query,
167 &termios_state.ts_linebuf, &termios_state.ts_linesize, NULL,NULL) >= 0)
168 user = termios_state.ts_linebuf;
170 jrestore:
171 safe_signal(SIGHUP, ohup);
172 safe_signal(SIGINT, oint);
174 NYD_LEAVE;
175 if (sig != 0)
176 n_raise(sig);
177 return user;
180 FL char *
181 getpassword(char const *query)/* TODO v15: use _only_ n_tty_fp! */
183 sighandler_type volatile oint, ohup;
184 struct termios tios;
185 char * volatile pass;
186 int volatile sig;
187 NYD_ENTER;
189 pass = NULL;
190 if(!(n_psonce & n_PSO_TTYIN))
191 goto j_leave;
193 if (query == NULL)
194 query = _("Password: ");
195 fputs(query, n_tty_fp);
196 fflush(n_tty_fp);
198 /* FIXME everywhere: tcsetattr() generates SIGTTOU when we're not in
199 * FIXME foreground pgrp, and can fail with EINTR!! also affects
200 * FIXME termios_state_reset() */
201 tcgetattr(STDIN_FILENO, &termios_state.ts_tios);
202 memcpy(&tios, &termios_state.ts_tios, sizeof tios);
203 termios_state.ts_needs_reset = TRU1;
204 tios.c_iflag &= ~(ISTRIP);
205 tios.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
207 oint = safe_signal(SIGINT, SIG_IGN);
208 ohup = safe_signal(SIGHUP, SIG_IGN);
209 if ((sig = sigsetjmp(a_tty__actjmp, 1)) != 0)
210 goto jrestore;
211 safe_signal(SIGINT, &a_tty__acthdl);
212 safe_signal(SIGHUP, &a_tty__acthdl);
214 tcsetattr(STDIN_FILENO, TCSAFLUSH, &tios);
215 if (readline_restart(n_stdin, &termios_state.ts_linebuf,
216 &termios_state.ts_linesize, 0) >= 0)
217 pass = termios_state.ts_linebuf;
218 jrestore:
219 termios_state_reset();
220 putc('\n', n_tty_fp);
222 safe_signal(SIGHUP, ohup);
223 safe_signal(SIGINT, oint);
224 NYD_LEAVE;
225 if (sig != 0)
226 n_raise(sig);
227 j_leave:
228 return pass;
230 #endif /* HAVE_SOCKETS */
232 FL ui32_t
233 n_tty_create_prompt(struct n_string *store, char const *xprompt,
234 enum n_go_input_flags gif){
235 struct n_visual_info_ctx vic;
236 struct str in, out;
237 ui32_t pwidth;
238 char const *cp;
239 NYD2_ENTER;
241 /* Prompt creation indicates that prompt printing is directly ahead, so take
242 * this opportunity of UI-in-a-known-state and advertise the error ring */
243 #ifdef HAVE_ERRORS
244 if((n_psonce & (n_PSO_INTERACTIVE | n_PSO_ERRORS_NOTED)
245 ) == n_PSO_INTERACTIVE && (n_pstate & n_PS_ERRORS_PROMPT)){
246 n_psonce |= n_PSO_ERRORS_NOTED;
247 fprintf(n_stdout, _("There are new messages in the error message ring "
248 "(denoted by %s)\n"
249 " The `errors' command manages this message ring\n"),
250 V_(n_error));
252 #endif
254 jredo:
255 n_string_trunc(store, 0);
257 if(gif & n_GO_INPUT_PROMPT_NONE){
258 pwidth = 0;
259 goto jleave;
261 #ifdef HAVE_ERRORS
262 if(n_pstate & n_PS_ERRORS_PROMPT){
263 n_pstate &= ~n_PS_ERRORS_PROMPT;
264 store = n_string_push_cp(store, V_(n_error));
265 store = n_string_push_c(store, '#');
266 store = n_string_push_c(store, ' ');
268 #endif
270 cp = (gif & n_GO_INPUT_PROMPT_EVAL)
271 ? (gif & n_GO_INPUT_NL_FOLLOW ? ok_vlook(prompt2) : ok_vlook(prompt))
272 : xprompt;
273 if(cp != NULL && *cp != '\0'){
274 enum n_shexp_state shs;
276 store = n_string_push_cp(store, cp);
277 in.s = n_string_cp(store);
278 in.l = store->s_len;
279 out = in;
280 store = n_string_drop_ownership(store);
282 shs = n_shexp_parse_token((n_SHEXP_PARSE_LOG |
283 n_SHEXP_PARSE_IGNORE_EMPTY | n_SHEXP_PARSE_QUOTE_AUTO_FIXED |
284 n_SHEXP_PARSE_QUOTE_AUTO_DSQ), store, &in, NULL);
285 if((shs & n_SHEXP_STATE_ERR_MASK) || !(shs & n_SHEXP_STATE_STOP)){
286 store = n_string_clear(store);
287 store = n_string_take_ownership(store, out.s, out.l +1, out.l);
288 jeeval:
289 n_err(_("*prompt2?* evaluation failed, actively unsetting it\n"));
290 if(gif & n_GO_INPUT_NL_FOLLOW)
291 ok_vclear(prompt2);
292 else
293 ok_vclear(prompt);
294 goto jredo;
297 if(!store->s_auto)
298 n_free(out.s);
301 /* Make all printable TODO not know, we want to pass through ESC/CSI! */
302 #if 0
303 in.s = n_string_cp(store);
304 in.l = store->s_len;
305 makeprint(&in, &out);
306 store = n_string_assign_buf(store, out.s, out.l);
307 n_free(out.s);
308 #endif
310 /* We need the visual width.. */
311 memset(&vic, 0, sizeof vic);
312 vic.vic_indat = n_string_cp(store);
313 vic.vic_inlen = store->s_len;
314 for(pwidth = 0; vic.vic_inlen > 0;){
315 /* but \[ .. \] is not taken into account */
316 if(vic.vic_indat[0] == '\\' && vic.vic_inlen > 1 &&
317 vic.vic_indat[1] == '['){
318 size_t i;
320 i = PTR2SIZE(vic.vic_indat - store->s_dat);
321 store = n_string_cut(store, i, 2);
322 cp = &n_string_cp(store)[i];
323 i = store->s_len - i;
324 for(;; ++cp, --i){
325 if(i < 2){
326 n_err(_("Open \\[ sequence not closed in *prompt2?*\n"));
327 goto jeeval;
329 if(cp[0] == '\\' && cp[1] == ']')
330 break;
332 i = PTR2SIZE(cp - store->s_dat);
333 store = n_string_cut(store, i, 2);
334 vic.vic_indat = &n_string_cp(store)[i];
335 vic.vic_inlen = store->s_len - i;
336 }else if(!n_visual_info(&vic, n_VISUAL_INFO_WIDTH_QUERY |
337 n_VISUAL_INFO_ONE_CHAR)){
338 n_err(_("Character set error in evaluation of *prompt2?*\n"));
339 goto jeeval;
340 }else{
341 pwidth += (ui32_t)vic.vic_vi_width;
342 vic.vic_indat = vic.vic_oudat;
343 vic.vic_inlen = vic.vic_oulen;
347 /* And there may be colour support, too */
348 #ifdef HAVE_COLOUR
349 if(n_COLOUR_IS_ACTIVE()){
350 struct str const *psp, *rsp;
351 struct n_colour_pen *ccp;
353 if((ccp = n_colour_pen_create(n_COLOUR_ID_MLE_PROMPT, NULL)) != NULL &&
354 (psp = n_colour_pen_to_str(ccp)) != NULL &&
355 (rsp = n_colour_reset_to_str()) != NULL){
356 store = n_string_unshift_buf(store, psp->s, psp->l);
357 /*store =*/ n_string_push_buf(store, rsp->s, rsp->l);
360 #endif /* HAVE_COLOUR */
362 jleave:
363 NYD2_LEAVE;
364 return pwidth;
368 * MLE: the Mailx-Line-Editor, our homebrew editor
369 * (inspired from NetBSDs sh(1) and dash(1)s hetio.c).
371 * Only used in interactive mode.
372 * TODO . This code should be splitted in funs/raw input/bind modules.
373 * TODO . We work with wide characters, but not for buffer takeovers and
374 * TODO cell2save()ings. This should be changed. For the former the buffer
375 * TODO thus needs to be converted to wide first, and then simply be fed in.
376 * TODO . We repaint too much. To overcome this use the same approach that my
377 * TODO terminal library uses, add a true "virtual screen line" that stores
378 * TODO the actually visible content, keep a notion of "first modified slot"
379 * TODO and "last modified slot" (including "unknown" and "any" specials),
380 * TODO update that virtual instead, then synchronize what has truly changed.
381 * TODO I.e., add an indirection layer.
382 * TODO . No BIDI support.
383 * TODO . `bind': we currently use only one lookup tree.
384 * TODO For absolute graceful behaviour (in conjunction with HAVE_TERMCAP) we
385 * TODO need a lower level tree, which possibly combines bytes into "symbolic
386 * TODO wchar_t values", into "keys" that is, as applicable, and an upper
387 * TODO layer which only works on "keys" in order to possibly combine them
388 * TODO into key sequences. We can reuse existent tree code for that.
389 * TODO We need an additional hashmap which maps termcap/terminfo names to
390 * TODO (their byte representations and) a dynamically assigned unique
391 * TODO "symbolic wchar_t value". This implies we may have incompatibilities
392 * TODO when __STDC_ISO_10646__ is not defined. Also we do need takeover-
393 * TODO bytes storage, but it can be a string_creat_auto in the line struct.
394 * TODO Until then we can run into ambiguities; in rare occasions.
396 #ifdef HAVE_MLE
397 /* To avoid memory leaks etc. with the current codebase that simply longjmp(3)s
398 * we're forced to use the very same buffer--the one that is passed through to
399 * us from the outside--to store anything we need, i.e., a "struct cell[]", and
400 * convert that on-the-fly back to the plain char* result once we're done.
401 * To simplify our live, use savestr() buffers for all other needed memory */
403 # ifdef HAVE_KEY_BINDINGS
404 /* Default *bind-timeout* key-sequence continuation timeout, in tenths of
405 * a second. Must fit in 8-bit! Update the manual upon change! */
406 # define a_TTY_BIND_TIMEOUT 2
407 # define a_TTY_BIND_TIMEOUT_MAX SI8_MAX
409 n_CTAV(a_TTY_BIND_TIMEOUT_MAX <= UI8_MAX);
411 /* We have a chicken-and-egg problem with `bind' and our termcap layer,
412 * because we may not initialize the latter automatically to allow users to
413 * specify *termcap-disable* and let it mean exactly that.
414 * On the other hand users can be expected to use `bind' in resources.
415 * Therefore bindings which involve termcap/terminfo sequences, and which
416 * are defined before n_PSO_STARTED signals usability of termcap/terminfo,
417 * will be (partially) delayed until tty_init() is called.
418 * And we preallocate space for the expansion of the resolved capability */
419 # define a_TTY_BIND_CAPNAME_MAX 15
420 # define a_TTY_BIND_CAPEXP_ROUNDUP 16
422 n_CTAV(n_ISPOW2(a_TTY_BIND_CAPEXP_ROUNDUP));
423 n_CTA(a_TTY_BIND_CAPEXP_ROUNDUP <= SI8_MAX / 2, "Variable must fit in 6-bit");
424 n_CTA(a_TTY_BIND_CAPEXP_ROUNDUP >= 8, "Variable too small");
425 # endif /* HAVE_KEY_BINDINGS */
427 # ifdef HAVE_HISTORY
428 /* The first line of the history file is used as a marker after >v14.9.6 */
429 # define a_TTY_HIST_MARKER "@s-mailx history v2"
430 # endif
432 /* The maximum size (of a_tty_cell's) in a line */
433 # define a_TTY_LINE_MAX SI32_MAX
435 /* (Some more CTAs around) */
436 n_CTA(a_TTY_LINE_MAX <= SI32_MAX,
437 "a_TTY_LINE_MAX larger than SI32_MAX, but the MLE uses 32-bit arithmetic");
439 /* When shall the visual screen be scrolled, in % of usable screen width */
440 # define a_TTY_SCROLL_MARGIN_LEFT 15
441 # define a_TTY_SCROLL_MARGIN_RIGHT 10
443 /* fexpand() flags for expand-on-tab */
444 # define a_TTY_TAB_FEXP_FL \
445 (FEXP_NOPROTO | FEXP_FULL | FEXP_SILENT | FEXP_MULTIOK)
447 /* Columns to ripoff: outermost may not be touched, plus position indicator.
448 * Must thus be at least 1, but should be >= 1+4 to dig the position indicator
449 * that we place (if there is sufficient space) */
450 # define a_TTY_WIDTH_RIPOFF 5
452 /* The implementation of the MLE functions always exists, and is based upon
453 * the a_TTY_BIND_FUN_* constants, so most of this enum is always necessary */
454 enum a_tty_bind_flags{
455 # ifdef HAVE_KEY_BINDINGS
456 a_TTY_BIND_RESOLVE = 1u<<8, /* Term cap. yet needs to be resolved */
457 a_TTY_BIND_DEFUNCT = 1u<<9, /* Unicode/term cap. used but not avail. */
458 a_TTY__BIND_MASK = a_TTY_BIND_RESOLVE | a_TTY_BIND_DEFUNCT,
459 /* MLE fun assigned to a one-byte-sequence: this may be used for special
460 * key-sequence bypass processing */
461 a_TTY_BIND_MLE1CNTRL = 1u<<10,
462 a_TTY_BIND_NOCOMMIT = 1u<<11, /* Expansion shall be editable */
463 # endif
465 /* MLE internal commands XXX Can these be values not bits? */
466 a_TTY_BIND_FUN_INTERNAL = 1u<<15,
467 a_TTY__BIND_FUN_SHIFT = 16u,
468 a_TTY__BIND_FUN_SHIFTMAX = 24u,
469 a_TTY__BIND_FUN_MASK = ((1u << a_TTY__BIND_FUN_SHIFTMAX) - 1) &
470 ~((1u << a_TTY__BIND_FUN_SHIFT) - 1),
471 # define a_TTY_BIND_FUN_REDUCE(X) \
472 (((ui32_t)(X) & a_TTY__BIND_FUN_MASK) >> a_TTY__BIND_FUN_SHIFT)
473 # define a_TTY_BIND_FUN_EXPAND(X) \
474 (((ui32_t)(X) & (a_TTY__BIND_FUN_MASK >> a_TTY__BIND_FUN_SHIFT)) << \
475 a_TTY__BIND_FUN_SHIFT)
476 # undef a_X
477 # define a_X(N,I)\
478 a_TTY_BIND_FUN_ ## N = a_TTY_BIND_FUN_EXPAND(I),
480 a_X(BELL, 0)
481 a_X(GO_BWD, 1) a_X(GO_FWD, 2)
482 a_X(GO_WORD_BWD, 3) a_X(GO_WORD_FWD, 4)
483 a_X(GO_SCREEN_BWD, 5) a_X(GO_SCREEN_FWD, 6)
484 a_X(GO_HOME, 7) a_X(GO_END, 8)
485 a_X(DEL_BWD, 9) a_X(DEL_FWD, 10)
486 a_X(SNARF_WORD_BWD, 11) a_X(SNARF_WORD_FWD, 12)
487 a_X(SNARF_END, 13) a_X(SNARF_LINE, 14)
488 a_X(HIST_BWD, 15) a_X(HIST_FWD, 16)
489 a_X(HIST_SRCH_BWD, 17) a_X(HIST_SRCH_FWD, 18)
490 a_X(REPAINT, 19)
491 a_X(QUOTE_RNDTRIP, 20)
492 a_X(PROMPT_CHAR, 21)
493 a_X(COMPLETE, 22)
494 a_X(PASTE, 23)
495 a_X(CLEAR_SCREEN, 24)
497 a_X(CANCEL, 25)
498 a_X(RESET, 26)
499 a_X(FULLRESET, 27)
500 a_X(COMMIT, 28) /* Must be last one! */
501 # undef a_X
503 a_TTY__BIND_LAST = 1<<28
505 # ifdef HAVE_KEY_BINDINGS
506 n_CTA((ui32_t)a_TTY_BIND_RESOLVE >= (ui32_t)n__GO_INPUT_CTX_MAX1,
507 "Bit carrier lower boundary must be raised to avoid value sharing");
508 # endif
509 n_CTA(a_TTY_BIND_FUN_EXPAND(a_TTY_BIND_FUN_COMMIT) <
510 (1 << a_TTY__BIND_FUN_SHIFTMAX),
511 "Bit carrier range must be expanded to represent necessary bits");
512 n_CTA(a_TTY__BIND_LAST >= (1u << a_TTY__BIND_FUN_SHIFTMAX),
513 "Bit carrier upper boundary must be raised to avoid value sharing");
514 n_CTA(UICMP(64, a_TTY__BIND_LAST, <=, SI32_MAX),
515 "Flag bits excess storage datatype" /* And we need one bit free */);
517 enum a_tty_fun_status{
518 a_TTY_FUN_STATUS_OK, /* Worked, next character */
519 a_TTY_FUN_STATUS_COMMIT, /* Line done */
520 a_TTY_FUN_STATUS_RESTART, /* Complete restart, reset multibyte etc. */
521 a_TTY_FUN_STATUS_END /* End, return EOF */
524 # ifdef HAVE_HISTORY
525 enum a_tty_hist_flags{
526 a_TTY_HIST_CTX_DEFAULT = n_GO_INPUT_CTX_DEFAULT,
527 a_TTY_HIST_CTX_COMPOSE = n_GO_INPUT_CTX_COMPOSE,
528 a_TTY_HIST_CTX_MASK = n__GO_INPUT_CTX_MASK,
529 /* Cannot use enum n_go_input_flags for the rest, need to stay in 8-bit */
530 a_TTY_HIST_GABBY = 1u<<7,
531 a_TTY_HIST__MAX = a_TTY_HIST_GABBY
533 n_CTA(a_TTY_HIST_CTX_MASK < a_TTY_HIST_GABBY, "Enumeration value overlap");
534 # endif
536 enum a_tty_visual_flags{
537 a_TTY_VF_NONE,
538 a_TTY_VF_MOD_CURSOR = 1u<<0, /* Cursor moved */
539 a_TTY_VF_MOD_CONTENT = 1u<<1, /* Content modified */
540 a_TTY_VF_MOD_DIRTY = 1u<<2, /* Needs complete repaint */
541 a_TTY_VF_MOD_SINGLE = 1u<<3, /* TODO Drop when indirection as above comes */
542 a_TTY_VF_REFRESH = a_TTY_VF_MOD_DIRTY | a_TTY_VF_MOD_CURSOR |
543 a_TTY_VF_MOD_CONTENT | a_TTY_VF_MOD_SINGLE,
544 a_TTY_VF_BELL = 1u<<8, /* Ring the bell */
545 a_TTY_VF_SYNC = 1u<<9, /* Flush/Sync I/O channel */
547 a_TTY_VF_ALL_MASK = a_TTY_VF_REFRESH | a_TTY_VF_BELL | a_TTY_VF_SYNC,
548 a_TTY__VF_LAST = a_TTY_VF_SYNC
551 # ifdef HAVE_KEY_BINDINGS
552 struct a_tty_bind_ctx{
553 struct a_tty_bind_ctx *tbc_next;
554 char *tbc_seq; /* quence as given (poss. re-quoted), in .tb__buf */
555 char *tbc_exp; /* ansion, in .tb__buf */
556 /* The .tbc_seq'uence with any terminal capabilities resolved; in fact an
557 * array of structures, the first entry of which is {si32_t buf_len_iscap;}
558 * where the signed bit indicates whether the buffer is a resolved terminal
559 * capability instead of a (possibly multibyte) character. In .tbc__buf */
560 char *tbc_cnv;
561 ui32_t tbc_seq_len;
562 ui32_t tbc_exp_len;
563 ui32_t tbc_cnv_len;
564 ui32_t tbc_flags;
565 char tbc__buf[n_VFIELD_SIZE(0)];
568 struct a_tty_bind_ctx_map{
569 enum n_go_input_flags tbcm_ctx;
570 char const tbcm_name[12]; /* Name of `bind' context */
572 # endif /* HAVE_KEY_BINDINGS */
574 struct a_tty_bind_builtin_tuple{
575 bool_t tbbt_iskey; /* Whether this is a control key; else termcap query */
576 char tbbt_ckey; /* Control code */
577 ui16_t tbbt_query; /* enum n_termcap_query (instead) */
578 char tbbt_exp[12]; /* String or [0]=NUL/[1]=BIND_FUN_REDUCE() */
580 n_CTA(n__TERMCAP_QUERY_MAX1 <= UI16_MAX,
581 "Enumeration cannot be stored in datatype");
583 # ifdef HAVE_KEY_BINDINGS
584 struct a_tty_bind_parse_ctx{
585 char const *tbpc_cmd; /* Command which parses */
586 char const *tbpc_in_seq; /* In: key sequence */
587 struct str tbpc_exp; /* In/Out: expansion (or NULL) */
588 struct a_tty_bind_ctx *tbpc_tbcp; /* Out: if yet existent */
589 struct a_tty_bind_ctx *tbpc_ltbcp; /* Out: the one before .tbpc_tbcp */
590 char *tbpc_seq; /* Out: normalized sequence */
591 char *tbpc_cnv; /* Out: sequence when read(2)ing it */
592 ui32_t tbpc_seq_len;
593 ui32_t tbpc_cnv_len;
594 ui32_t tbpc_cnv_align_mask; /* For creating a_tty_bind_ctx.tbc_cnv */
595 ui32_t tbpc_flags; /* n_go_input_flags | a_tty_bind_flags */
598 /* Input character tree */
599 struct a_tty_bind_tree{
600 struct a_tty_bind_tree *tbt_sibling; /* s at same level */
601 struct a_tty_bind_tree *tbt_childs; /* Sequence continues.. here */
602 struct a_tty_bind_tree *tbt_parent;
603 struct a_tty_bind_ctx *tbt_bind; /* NULL for intermediates */
604 wchar_t tbt_char; /* acter this level represents */
605 bool_t tbt_isseq; /* Belongs to multibyte sequence */
606 bool_t tbt_isseq_trail; /* ..is trailing byte of it */
607 ui8_t tbt__dummy[2];
609 # endif /* HAVE_KEY_BINDINGS */
611 struct a_tty_cell{
612 wchar_t tc_wc;
613 ui16_t tc_count; /* ..of bytes */
614 ui8_t tc_width; /* Visual width; TAB==UI8_MAX! */
615 bool_t tc_novis; /* Don't display visually as such (control character) */
616 char tc_cbuf[MB_LEN_MAX * 2]; /* .. plus reset shift sequence */
619 struct a_tty_global{
620 struct a_tty_line *tg_line; /* To be able to access it from signal hdl */
621 # ifdef HAVE_HISTORY
622 struct a_tty_hist *tg_hist;
623 struct a_tty_hist *tg_hist_tail;
624 size_t tg_hist_size;
625 size_t tg_hist_size_max;
626 # endif
627 # ifdef HAVE_KEY_BINDINGS
628 ui32_t tg_bind_cnt; /* Overall number of bindings */
629 bool_t tg_bind_isdirty;
630 bool_t tg_bind_isbuild;
631 # define a_TTY_SHCUT_MAX (3 +1) /* Note: update manual on change! */
632 ui8_t tg_bind__dummy[2];
633 char tg_bind_shcut_cancel[n__GO_INPUT_CTX_MAX1][a_TTY_SHCUT_MAX];
634 char tg_bind_shcut_prompt_char[n__GO_INPUT_CTX_MAX1][a_TTY_SHCUT_MAX];
635 struct a_tty_bind_ctx *tg_bind[n__GO_INPUT_CTX_MAX1];
636 struct a_tty_bind_tree *tg_bind_tree[n__GO_INPUT_CTX_MAX1][HSHSIZE];
637 # endif
638 struct termios tg_tios_old;
639 struct termios tg_tios_new;
641 # ifdef HAVE_KEY_BINDINGS
642 n_CTA(n__GO_INPUT_CTX_MAX1 == 3 && a_TTY_SHCUT_MAX == 4 &&
643 n_SIZEOF_FIELD(struct a_tty_global, tg_bind__dummy) == 2,
644 "Value results in array sizes that results in bad structure layout");
645 n_CTA(a_TTY_SHCUT_MAX > 1,
646 "Users need at least one shortcut, plus NUL terminator");
647 # endif
649 # ifdef HAVE_HISTORY
650 struct a_tty_hist{
651 struct a_tty_hist *th_older;
652 struct a_tty_hist *th_younger;
653 ui32_t th_len;
654 ui8_t th_flags; /* enum a_tty_hist_flags */
655 char th_dat[n_VFIELD_SIZE(3)];
657 n_CTA(UI8_MAX >= a_TTY_HIST__MAX, "Value exceeds datatype storage");
658 # endif
660 struct a_tty_line{
661 /* Caller pointers */
662 char **tl_x_buf;
663 size_t *tl_x_bufsize;
664 /* Input processing */
665 # ifdef HAVE_KEY_BINDINGS
666 wchar_t tl_bind_takeover; /* Leftover byte to consume next */
667 ui8_t tl_bind_timeout; /* In-seq. inter-byte-timer, in 1/10th secs */
668 ui8_t tl__bind_dummy[3];
669 char (*tl_bind_shcut_cancel)[a_TTY_SHCUT_MAX]; /* Special _CANCEL control */
670 char (*tl_bind_shcut_prompt_char)[a_TTY_SHCUT_MAX]; /* ..for _PROMPT_CHAR */
671 struct a_tty_bind_tree *(*tl_bind_tree_hmap)[HSHSIZE]; /* Bind lookup tree */
672 struct a_tty_bind_tree *tl_bind_tree;
673 # endif
674 /* Line data / content handling */
675 ui32_t tl_count; /* ..of a_tty_cell's (<= a_TTY_LINE_MAX) */
676 ui32_t tl_cursor; /* Current a_tty_cell insertion point */
677 union{
678 char *cbuf; /* *.tl_x_buf */
679 struct a_tty_cell *cells;
680 } tl_line;
681 struct str tl_defc; /* Current default content */
682 size_t tl_defc_cursor_byte; /* Desired position of cursor after takeover */
683 struct str tl_savec; /* Saved default content */
684 struct str tl_pastebuf; /* Last snarfed data */
685 # ifdef HAVE_HISTORY
686 struct a_tty_hist *tl_hist; /* History cursor */
687 # endif
688 ui32_t tl_count_max; /* ..before buffer needs to grow */
689 /* Visual data representation handling */
690 ui32_t tl_vi_flags; /* enum a_tty_visual_flags */
691 ui32_t tl_lst_count; /* .tl_count after last sync */
692 ui32_t tl_lst_cursor; /* .tl_cursor after last sync */
693 /* TODO Add another indirection layer by adding a tl_phy_line of
694 * TODO a_tty_cell objects, incorporate changes in visual layer,
695 * TODO then check what _really_ has changed, sync those changes only */
696 struct a_tty_cell const *tl_phy_start; /* First visible cell, left border */
697 ui32_t tl_phy_cursor; /* Physical cursor position */
698 bool_t tl_quote_rndtrip; /* For _kht() expansion */
699 ui8_t tl__dummy2[3 + 4];
700 ui32_t tl_goinflags; /* enum n_go_input_flags */
701 ui32_t tl_prompt_length; /* Preclassified (TODO needed as a_tty_cell) */
702 ui32_t tl_prompt_width;
703 char const *tl_prompt; /* Preformatted prompt (including colours) */
704 /* .tl_pos_buf is a hack */
705 # ifdef HAVE_COLOUR
706 char *tl_pos_buf; /* mle-position colour-on, [4], reset seq. */
707 char *tl_pos; /* Address of the [4] */
708 # endif
711 # ifdef HAVE_KEY_BINDINGS
712 /* C99: use [INDEX]={} */
713 n_CTAV(n_GO_INPUT_CTX_BASE == 0);
714 n_CTAV(n_GO_INPUT_CTX_DEFAULT == 1);
715 n_CTAV(n_GO_INPUT_CTX_COMPOSE == 2);
716 static struct a_tty_bind_ctx_map const
717 a_tty_bind_ctx_maps[n__GO_INPUT_CTX_MAX1] = {
718 {n_GO_INPUT_CTX_BASE, "base"},
719 {n_GO_INPUT_CTX_DEFAULT, "default"},
720 {n_GO_INPUT_CTX_COMPOSE, "compose"}
723 /* Special functions which our MLE provides internally.
724 * Update the manual upon change! */
725 static char const a_tty_bind_fun_names[][24] = {
726 # undef a_X
727 # define a_X(I,N) \
728 n_FIELD_INITI(a_TTY_BIND_FUN_REDUCE(a_TTY_BIND_FUN_ ## I)) "mle-" N "\0",
730 a_X(BELL, "bell")
731 a_X(GO_BWD, "go-bwd") a_X(GO_FWD, "go-fwd")
732 a_X(GO_WORD_BWD, "go-word-bwd") a_X(GO_WORD_FWD, "go-word-fwd")
733 a_X(GO_SCREEN_BWD, "go-screen-bwd") a_X(GO_SCREEN_FWD, "go-screen-fwd")
734 a_X(GO_HOME, "go-home") a_X(GO_END, "go-end")
735 a_X(DEL_BWD, "del-bwd") a_X(DEL_FWD, "del-fwd")
736 a_X(SNARF_WORD_BWD, "snarf-word-bwd") a_X(SNARF_WORD_FWD, "snarf-word-fwd")
737 a_X(SNARF_END, "snarf-end") a_X(SNARF_LINE, "snarf-line")
738 a_X(HIST_BWD, "hist-bwd") a_X(HIST_FWD, "hist-fwd")
739 a_X(HIST_SRCH_BWD, "hist-srch-bwd") a_X(HIST_SRCH_FWD, "hist-srch-fwd")
740 a_X(REPAINT, "repaint")
741 a_X(QUOTE_RNDTRIP, "quote-rndtrip")
742 a_X(PROMPT_CHAR, "prompt-char")
743 a_X(COMPLETE, "complete")
744 a_X(PASTE, "paste")
745 a_X(CLEAR_SCREEN, "clear-screen")
747 a_X(CANCEL, "cancel")
748 a_X(RESET, "reset")
749 a_X(FULLRESET, "fullreset")
750 a_X(COMMIT, "commit")
752 # undef a_X
754 # endif /* HAVE_KEY_BINDINGS */
756 /* The default key bindings (unless disallowed). Update manual upon change!
757 * A logical subset of this table is also used if !HAVE_KEY_BINDINGS (more
758 * expensive than a switch() on control codes directly, but less redundant).
759 * The table for the "base" context */
760 static struct a_tty_bind_builtin_tuple const a_tty_bind_base_tuples[] = {
761 # undef a_X
762 # define a_X(K,S) \
763 {TRU1, K, 0, {'\0', (char)a_TTY_BIND_FUN_REDUCE(a_TTY_BIND_FUN_ ## S),}},
765 a_X('A', GO_HOME)
766 a_X('B', GO_BWD)
767 /* C: SIGINT */
768 a_X('D', DEL_FWD)
769 a_X('E', GO_END)
770 a_X('F', GO_FWD)
771 a_X('G', RESET)
772 a_X('H', DEL_BWD)
773 a_X('I', COMPLETE)
774 a_X('J', COMMIT)
775 a_X('K', SNARF_END)
776 a_X('L', REPAINT)
777 /* M: same as J */
778 a_X('N', HIST_FWD)
779 /* O: below */
780 a_X('P', HIST_BWD)
781 a_X('Q', QUOTE_RNDTRIP)
782 a_X('R', HIST_SRCH_BWD)
783 a_X('S', HIST_SRCH_FWD)
784 a_X('T', PASTE)
785 a_X('U', SNARF_LINE)
786 a_X('V', PROMPT_CHAR)
787 a_X('W', SNARF_WORD_BWD)
788 a_X('X', GO_WORD_FWD)
789 a_X('Y', GO_WORD_BWD)
790 /* Z: SIGTSTP */
792 a_X('[', CANCEL)
793 /* \: below */
794 /* ]: below */
795 /* ^: below */
796 a_X('_', SNARF_WORD_FWD)
798 a_X('?', DEL_BWD)
800 # undef a_X
801 # define a_X(K,S) {TRU1, K, 0, {S}},
803 /* The remains only if we have `bind' functionality available */
804 # ifdef HAVE_KEY_BINDINGS
805 # undef a_X
806 # define a_X(Q,S) \
807 {FAL0, '\0', n_TERMCAP_QUERY_ ## Q,\
808 {'\0', (char)a_TTY_BIND_FUN_REDUCE(a_TTY_BIND_FUN_ ## S),}},
810 a_X(key_backspace, DEL_BWD) a_X(key_dc, DEL_FWD)
811 a_X(key_eol, SNARF_END)
812 a_X(key_home, GO_HOME) a_X(key_end, GO_END)
813 a_X(key_left, GO_BWD) a_X(key_right, GO_FWD)
814 a_X(xkey_aleft, GO_WORD_BWD) a_X(xkey_aright, GO_WORD_FWD)
815 a_X(xkey_cleft, GO_SCREEN_BWD) a_X(xkey_cright, GO_SCREEN_FWD)
816 a_X(key_sleft, GO_HOME) a_X(key_sright, GO_END)
817 a_X(key_up, HIST_BWD) a_X(key_down, HIST_FWD)
818 # endif /* HAVE_KEY_BINDINGS */
821 /* The table for the "default" context */
822 static struct a_tty_bind_builtin_tuple const a_tty_bind_default_tuples[] = {
823 # undef a_X
824 # define a_X(K,S) \
825 {TRU1, K, 0, {'\0', (char)a_TTY_BIND_FUN_REDUCE(a_TTY_BIND_FUN_ ## S),}},
827 # undef a_X
828 # define a_X(K,S) {TRU1, K, 0, {S}},
830 a_X('O', "dt")
832 a_X('\\', "z+")
833 a_X(']', "z$")
834 a_X('^', "z0")
836 /* The remains only if we have `bind' functionality available */
837 # ifdef HAVE_KEY_BINDINGS
838 # undef a_X
839 # define a_X(Q,S) {FAL0, '\0', n_TERMCAP_QUERY_ ## Q, {S}},
841 a_X(key_shome, "z0") a_X(key_send, "z$")
842 a_X(xkey_sup, "z0") a_X(xkey_sdown, "z$")
843 a_X(key_ppage, "z-") a_X(key_npage, "z+")
844 a_X(xkey_cup, "dotmove-") a_X(xkey_cdown, "dotmove+")
845 # endif /* HAVE_KEY_BINDINGS */
847 # undef a_X
849 static struct a_tty_global a_tty;
851 /* Change from canonical to raw, non-canonical mode, and way back */
852 static void a_tty_term_mode(bool_t raw);
854 # ifdef HAVE_HISTORY
855 /* Load and save the history file, respectively */
856 static bool_t a_tty_hist_load(void);
857 static bool_t a_tty_hist_save(void);
859 /* Initialize .tg_hist_size_max and return desired history file, or NULL */
860 static char const *a_tty_hist__query_config(void);
862 /* (Definetely) Add an entry TODO yet assumes hold_all_sigs() is held! */
863 static void a_tty_hist_add(char const *s, enum n_go_input_flags gif);
864 # endif
866 /* Adjust an active raw mode to use / not use a timeout */
867 # ifdef HAVE_KEY_BINDINGS
868 static void a_tty_term_rawmode_timeout(struct a_tty_line *tlp, bool_t enable);
869 # endif
871 /* 0-X (2), UI8_MAX == \t / HT */
872 static ui8_t a_tty_wcwidth(wchar_t wc);
874 /* Memory / cell / word generics */
875 static void a_tty_check_grow(struct a_tty_line *tlp, ui32_t no
876 n_MEMORY_DEBUG_ARGS);
877 static ssize_t a_tty_cell2dat(struct a_tty_line *tlp);
878 static void a_tty_cell2save(struct a_tty_line *tlp);
880 /* Save away data bytes of given range (max = non-inclusive) */
881 static void a_tty_copy2paste(struct a_tty_line *tlp, struct a_tty_cell *tcpmin,
882 struct a_tty_cell *tcpmax);
884 /* Ask user for hexadecimal number, interpret as UTF-32 */
885 static wchar_t a_tty_vinuni(struct a_tty_line *tlp);
887 /* Visual screen synchronization */
888 static bool_t a_tty_vi_refresh(struct a_tty_line *tlp);
890 static bool_t a_tty_vi__paint(struct a_tty_line *tlp);
892 /* Search for word boundary, starting at tl_cursor, in "dir"ection (<> 0).
893 * Return <0 when moving is impossible (backward direction but in position 0,
894 * forward direction but in outermost column), and relative distance to
895 * tl_cursor otherwise */
896 static si32_t a_tty_wboundary(struct a_tty_line *tlp, si32_t dir);
898 /* Most function implementations */
899 static void a_tty_khome(struct a_tty_line *tlp, bool_t dobell);
900 static void a_tty_kend(struct a_tty_line *tlp);
901 static void a_tty_kbs(struct a_tty_line *tlp);
902 static void a_tty_ksnarf(struct a_tty_line *tlp, bool_t cplline, bool_t dobell);
903 static si32_t a_tty_kdel(struct a_tty_line *tlp);
904 static void a_tty_kleft(struct a_tty_line *tlp);
905 static void a_tty_kright(struct a_tty_line *tlp);
906 static void a_tty_ksnarfw(struct a_tty_line *tlp, bool_t fwd);
907 static void a_tty_kgow(struct a_tty_line *tlp, si32_t dir);
908 static void a_tty_kgoscr(struct a_tty_line *tlp, si32_t dir);
909 static bool_t a_tty_kother(struct a_tty_line *tlp, wchar_t wc);
910 static ui32_t a_tty_kht(struct a_tty_line *tlp);
912 # ifdef HAVE_HISTORY
913 /* Return UI32_MAX on "exhaustion" */
914 static ui32_t a_tty_khist(struct a_tty_line *tlp, bool_t fwd);
915 static ui32_t a_tty_khist_search(struct a_tty_line *tlp, bool_t fwd);
917 static ui32_t a_tty__khist_shared(struct a_tty_line *tlp,
918 struct a_tty_hist *thp);
919 # endif
921 /* Handle a function */
922 static enum a_tty_fun_status a_tty_fun(struct a_tty_line *tlp,
923 enum a_tty_bind_flags tbf, size_t *len);
925 /* Readline core */
926 static ssize_t a_tty_readline(struct a_tty_line *tlp, size_t len,
927 bool_t *histok_or_null n_MEMORY_DEBUG_ARGS);
929 # ifdef HAVE_KEY_BINDINGS
930 /* Find context or -1 */
931 static enum n_go_input_flags a_tty_bind_ctx_find(char const *name);
933 /* Create (or replace, if allowed) a binding */
934 static bool_t a_tty_bind_create(struct a_tty_bind_parse_ctx *tbpcp,
935 bool_t replace);
937 /* Shared implementation to parse `bind' and `unbind' "key-sequence" and
938 * "expansion" command line arguments into something that we can work with */
939 static bool_t a_tty_bind_parse(bool_t isbindcmd,
940 struct a_tty_bind_parse_ctx *tbpcp);
942 /* Lazy resolve a termcap(5)/terminfo(5) (or *termcap*!) capability */
943 static void a_tty_bind_resolve(struct a_tty_bind_ctx *tbcp);
945 /* Delete an existing binding */
946 static void a_tty_bind_del(struct a_tty_bind_parse_ctx *tbpcp);
948 /* Life cycle of all input node trees */
949 static void a_tty_bind_tree_build(void);
950 static void a_tty_bind_tree_teardown(void);
952 static void a_tty__bind_tree_add(ui32_t hmap_idx,
953 struct a_tty_bind_tree *store[HSHSIZE],
954 struct a_tty_bind_ctx *tbcp);
955 static struct a_tty_bind_tree *a_tty__bind_tree_add_wc(
956 struct a_tty_bind_tree **treep, struct a_tty_bind_tree *parentp,
957 wchar_t wc, bool_t isseq);
958 static void a_tty__bind_tree_free(struct a_tty_bind_tree *tbtp);
959 # endif /* HAVE_KEY_BINDINGS */
961 static void
962 a_tty_signal(int sig){
963 /* Prototype at top */
964 sigset_t nset, oset;
965 NYD_X; /* Signal handler */
967 n_COLOUR( n_colour_env_gut(); ) /* TODO NO SIMPLE SUSPENSION POSSIBLE.. */
968 a_tty_term_mode(FAL0);
969 n_TERMCAP_SUSPEND(TRU1);
970 a_tty_sigs_down();
972 sigemptyset(&nset);
973 sigaddset(&nset, sig);
974 sigprocmask(SIG_UNBLOCK, &nset, &oset);
975 n_raise(sig);
976 /* When we come here we'll continue editing, so reestablish */
977 sigprocmask(SIG_BLOCK, &oset, (sigset_t*)NULL);
979 /* TODO THEREFORE NEED TO _GUT() .. _CREATE() ENTIRE ENVS!! */
980 n_COLOUR( n_colour_env_create(n_COLOUR_CTX_MLE, n_tty_fp, FAL0); )
981 a_tty_sigs_up();
982 n_TERMCAP_RESUME(TRU1);
983 a_tty_term_mode(TRU1);
984 a_tty.tg_line->tl_vi_flags |= a_TTY_VF_MOD_DIRTY;
987 static void
988 a_tty_term_mode(bool_t raw){
989 struct termios *tiosp;
990 NYD2_ENTER;
992 tiosp = &a_tty.tg_tios_old;
993 if(!raw)
994 goto jleave;
996 /* Always requery the attributes, in case we've been moved from background
997 * to foreground or however else in between sessions */
998 /* XXX Always enforce ECHO and ICANON in the OLD attributes - do so as long
999 * XXX as we don't properly deal with TTIN and TTOU etc. */
1000 tcgetattr(STDIN_FILENO, tiosp); /* TODO v15: use _only_ n_tty_fp! */
1001 tiosp->c_lflag |= ECHO | ICANON;
1003 memcpy(&a_tty.tg_tios_new, tiosp, sizeof *tiosp);
1004 tiosp = &a_tty.tg_tios_new;
1005 tiosp->c_cc[VMIN] = 1;
1006 tiosp->c_cc[VTIME] = 0;
1007 /* Enable ^\, ^Q and ^S to be used for key bindings */
1008 tiosp->c_cc[VQUIT] = tiosp->c_cc[VSTART] = tiosp->c_cc[VSTOP] = '\0';
1009 tiosp->c_iflag &= ~(ISTRIP | IGNCR);
1010 tiosp->c_lflag &= ~(ECHO /*| ECHOE | ECHONL */| ICANON | IEXTEN);
1011 jleave:
1012 tcsetattr(STDIN_FILENO, TCSADRAIN, tiosp);
1013 NYD2_LEAVE;
1016 # ifdef HAVE_HISTORY
1017 static bool_t
1018 a_tty_hist_load(void){
1019 ui8_t version;
1020 size_t lsize, cnt, llen;
1021 char *lbuf, *cp;
1022 FILE *f;
1023 char const *v;
1024 bool_t rv;
1025 NYD_ENTER;
1027 rv = TRU1;
1029 if((v = a_tty_hist__query_config()) == NULL ||
1030 a_tty.tg_hist_size_max == 0)
1031 goto jleave;
1033 hold_all_sigs(); /* TODO too heavy, yet we may jump even here!? */
1034 f = fopen(v, "r"); /* TODO HISTFILE LOAD: use linebuf pool */
1035 if(f == NULL){
1036 int e;
1038 e = errno;
1039 n_err(_("Cannot read *history-file*=%s: %s\n"),
1040 n_shexp_quote_cp(v, FAL0), n_err_to_doc(e));
1041 rv = FAL0;
1042 goto jrele;
1044 (void)n_file_lock(fileno(f), FLT_READ, 0,0, UIZ_MAX);
1046 /* Clear old history */
1047 /* C99 */{
1048 struct a_tty_hist *thp;
1050 while((thp = a_tty.tg_hist) != NULL){
1051 a_tty.tg_hist = thp->th_older;
1052 n_free(thp);
1054 a_tty.tg_hist_tail = NULL;
1055 a_tty.tg_hist_size = 0;
1058 lbuf = NULL;
1059 lsize = 0;
1060 cnt = (size_t)fsize(f);
1061 version = 0;
1063 while(fgetline(&lbuf, &lsize, &cnt, &llen, f, FAL0) != NULL){
1064 cp = lbuf;
1065 /* Hand-edited history files may have this, probably */
1066 while(llen > 0 && spacechar(cp[0])){
1067 ++cp;
1068 --llen;
1070 if(llen > 0 && cp[llen - 1] == '\n')
1071 cp[--llen] = '\0';
1073 /* Skip empty and comment lines */
1074 if(llen == 0 || cp[0] == '#')
1075 continue;
1077 if(n_UNLIKELY(version == 0) &&
1078 (version = strcmp(cp, a_TTY_HIST_MARKER) ? 1 : 2) != 1)
1079 continue;
1081 /* C99 */{
1082 enum n_go_input_flags gif;
1084 if(version == 2){
1085 if(llen <= 2){
1086 /* XXX n_err(_("Skipped invalid *history-file* entry: %s\n"),
1087 * XXX n_shexp_quote_cp(cp));*/
1088 continue;
1090 switch(*cp++){
1091 default:
1092 case 'd':
1093 gif = n_GO_INPUT_CTX_DEFAULT; /* == a_TTY_HIST_CTX_DEFAULT */
1094 break;
1095 case 'c':
1096 gif = n_GO_INPUT_CTX_COMPOSE; /* == a_TTY_HIST_CTX_COMPOSE */
1097 break;
1100 if(*cp++ == '*')
1101 gif |= n_GO_INPUT_HIST_GABBY;
1103 while(*cp == ' ')
1104 ++cp;
1105 }else{
1106 gif = n_GO_INPUT_CTX_DEFAULT;
1107 if(cp[0] == '*'){
1108 ++cp;
1109 gif |= n_GO_INPUT_HIST_GABBY;
1113 a_tty_hist_add(cp, gif);
1117 if(lbuf != NULL)
1118 n_free(lbuf);
1120 fclose(f);
1121 jrele:
1122 rele_all_sigs(); /* XXX remove jumps */
1123 jleave:
1124 NYD_LEAVE;
1125 return rv;
1128 static bool_t
1129 a_tty_hist_save(void){
1130 size_t i;
1131 struct a_tty_hist *thp;
1132 FILE *f;
1133 char const *v;
1134 bool_t rv, dogabby;
1135 NYD_ENTER;
1137 rv = TRU1;
1139 if((v = a_tty_hist__query_config()) == NULL ||
1140 a_tty.tg_hist_size_max == 0)
1141 goto jleave;
1143 dogabby = ok_blook(history_gabby_persist);
1145 if((thp = a_tty.tg_hist) != NULL)
1146 for(i = a_tty.tg_hist_size_max; thp->th_older != NULL;
1147 thp = thp->th_older)
1148 if((dogabby || !(thp->th_flags & a_TTY_HIST_GABBY)) && --i == 0)
1149 break;
1151 hold_all_sigs(); /* TODO too heavy, yet we may jump even here!? */
1152 if((f = fopen(v, "w")) == NULL){ /* TODO temporary + rename?! */
1153 int e;
1155 e = errno;
1156 n_err(_("Cannot write *history-file*=%s: %s\n"),
1157 n_shexp_quote_cp(v, FAL0), n_err_to_doc(e));
1158 rv = FAL0;
1159 goto jrele;
1161 (void)n_file_lock(fileno(f), FLT_WRITE, 0,0, UIZ_MAX);
1163 if(fwrite(a_TTY_HIST_MARKER "\n", sizeof *a_TTY_HIST_MARKER,
1164 sizeof(a_TTY_HIST_MARKER "\n") -1, f) !=
1165 sizeof(a_TTY_HIST_MARKER "\n") -1)
1166 goto jioerr;
1167 else for(; thp != NULL; thp = thp->th_younger){
1168 if(dogabby || !(thp->th_flags & a_TTY_HIST_GABBY)){
1169 char c;
1171 switch(thp->th_flags & a_TTY_HIST_CTX_MASK){
1172 default:
1173 case a_TTY_HIST_CTX_DEFAULT:
1174 c = 'd';
1175 break;
1176 case a_TTY_HIST_CTX_COMPOSE:
1177 c = 'c';
1178 break;
1180 if(putc(c, f) == EOF)
1181 goto jioerr;
1183 if((thp->th_flags & a_TTY_HIST_GABBY) && putc('*', f) == EOF)
1184 goto jioerr;
1186 if(putc(' ', f) == EOF ||
1187 fwrite(thp->th_dat, sizeof *thp->th_dat, thp->th_len, f) !=
1188 sizeof(*thp->th_dat) * thp->th_len ||
1189 putc('\n', f) == EOF){
1190 jioerr:
1191 n_err(_("I/O error while writing *history-file* %s\n"),
1192 n_shexp_quote_cp(v, FAL0));
1193 rv = FAL0;
1194 break;
1199 fclose(f);
1200 jrele:
1201 rele_all_sigs(); /* XXX remove jumps */
1202 jleave:
1203 NYD_LEAVE;
1204 return rv;
1207 static char const *
1208 a_tty_hist__query_config(void){
1209 char const *rv, *cp;
1210 NYD2_ENTER;
1212 if((cp = ok_vlook(NAIL_HISTSIZE)) != NULL)
1213 n_OBSOLETE(_("please use *history-size* instead of *NAIL_HISTSIZE*"));
1214 if((rv = ok_vlook(history_size)) == NULL)
1215 rv = cp;
1216 if(rv == NULL)
1217 a_tty.tg_hist_size_max = UIZ_MAX;
1218 else
1219 (void)n_idec_uiz_cp(&a_tty.tg_hist_size_max, rv, 10, NULL);
1221 if((cp = ok_vlook(NAIL_HISTFILE)) != NULL)
1222 n_OBSOLETE(_("please use *history-file* instead of *NAIL_HISTFILE*"));
1223 if((rv = ok_vlook(history_file)) == NULL)
1224 rv = cp;
1225 if(rv != NULL)
1226 rv = fexpand(rv, FEXP_LOCAL | FEXP_NSHELL);
1227 NYD2_LEAVE;
1228 return rv;
1231 static void
1232 a_tty_hist_add(char const *s, enum n_go_input_flags gif){
1233 ui32_t l;
1234 struct a_tty_hist *thp, *othp, *ythp;
1235 NYD2_ENTER;
1237 l = (ui32_t)strlen(s); /* xxx simply do not store if >= SI32_MAX */
1239 /* Eliminating duplicates is expensive, but simply inacceptable so
1240 * during the load of a potentially large history file! */
1241 if(n_psonce & n_PSO_LINE_EDITOR_INIT)
1242 for(thp = a_tty.tg_hist; thp != NULL; thp = thp->th_older)
1243 if(thp->th_len == l && !strcmp(thp->th_dat, s)){
1244 thp->th_flags = (gif & a_TTY_HIST_CTX_MASK) |
1245 (gif & n_GO_INPUT_HIST_GABBY ? a_TTY_HIST_GABBY : 0);
1246 othp = thp->th_older;
1247 ythp = thp->th_younger;
1248 if(othp != NULL)
1249 othp->th_younger = ythp;
1250 else
1251 a_tty.tg_hist_tail = ythp;
1252 if(ythp != NULL)
1253 ythp->th_older = othp;
1254 else
1255 a_tty.tg_hist = othp;
1256 goto jleave;
1259 if(n_LIKELY(a_tty.tg_hist_size <= a_tty.tg_hist_size_max))
1260 ++a_tty.tg_hist_size;
1261 else{
1262 --a_tty.tg_hist_size;
1263 if((thp = a_tty.tg_hist_tail) != NULL){
1264 if((a_tty.tg_hist_tail = thp->th_younger) == NULL)
1265 a_tty.tg_hist = NULL;
1266 else
1267 a_tty.tg_hist_tail->th_older = NULL;
1268 n_free(thp);
1272 thp = n_alloc(n_VSTRUCT_SIZEOF(struct a_tty_hist, th_dat) + l +1);
1273 thp->th_len = l;
1274 thp->th_flags = (gif & a_TTY_HIST_CTX_MASK) |
1275 (gif & n_GO_INPUT_HIST_GABBY ? a_TTY_HIST_GABBY : 0);
1276 memcpy(thp->th_dat, s, l +1);
1277 jleave:
1278 if((thp->th_older = a_tty.tg_hist) != NULL)
1279 a_tty.tg_hist->th_younger = thp;
1280 else
1281 a_tty.tg_hist_tail = thp;
1282 thp->th_younger = NULL;
1283 a_tty.tg_hist = thp;
1284 NYD2_LEAVE;
1286 # endif /* HAVE_HISTORY */
1288 # ifdef HAVE_KEY_BINDINGS
1289 static void
1290 a_tty_term_rawmode_timeout(struct a_tty_line *tlp, bool_t enable){
1291 NYD2_ENTER;
1292 if(enable){
1293 ui8_t bt;
1295 a_tty.tg_tios_new.c_cc[VMIN] = 0;
1296 if((bt = tlp->tl_bind_timeout) == 0)
1297 bt = a_TTY_BIND_TIMEOUT;
1298 a_tty.tg_tios_new.c_cc[VTIME] = bt;
1299 }else{
1300 a_tty.tg_tios_new.c_cc[VMIN] = 1;
1301 a_tty.tg_tios_new.c_cc[VTIME] = 0;
1303 tcsetattr(STDIN_FILENO, TCSANOW, &a_tty.tg_tios_new);
1304 NYD2_LEAVE;
1306 # endif /* HAVE_KEY_BINDINGS */
1308 static ui8_t
1309 a_tty_wcwidth(wchar_t wc){
1310 ui8_t rv;
1311 NYD2_ENTER;
1313 /* Special case the reverse solidus at first */
1314 if(wc == '\t')
1315 rv = UI8_MAX;
1316 else{
1317 int i;
1319 # ifdef HAVE_WCWIDTH
1320 rv = ((i = wcwidth(wc)) > 0) ? (ui8_t)i : 0;
1321 # else
1322 rv = iswprint(wc) ? 1 + (wc >= 0x1100u) : 0; /* TODO use S-CText */
1323 # endif
1325 NYD2_LEAVE;
1326 return rv;
1329 static void
1330 a_tty_check_grow(struct a_tty_line *tlp, ui32_t no n_MEMORY_DEBUG_ARGS){
1331 ui32_t cmax;
1332 NYD2_ENTER;
1334 if(n_UNLIKELY((cmax = tlp->tl_count + no) > tlp->tl_count_max)){
1335 size_t i;
1337 i = cmax * sizeof(struct a_tty_cell) + 2 * sizeof(struct a_tty_cell);
1338 if(n_LIKELY(i >= *tlp->tl_x_bufsize)){
1339 hold_all_sigs(); /* XXX v15 drop */
1340 i <<= 1;
1341 tlp->tl_line.cbuf =
1342 *tlp->tl_x_buf = (n_realloc)(*tlp->tl_x_buf, i
1343 n_MEMORY_DEBUG_ARGSCALL);
1344 rele_all_sigs(); /* XXX v15 drop */
1346 tlp->tl_count_max = cmax;
1347 *tlp->tl_x_bufsize = i;
1349 NYD2_LEAVE;
1352 static ssize_t
1353 a_tty_cell2dat(struct a_tty_line *tlp){
1354 size_t len, i;
1355 NYD2_ENTER;
1357 len = 0;
1359 if(n_LIKELY((i = tlp->tl_count) > 0)){
1360 struct a_tty_cell const *tcap;
1362 tcap = tlp->tl_line.cells;
1364 memcpy(tlp->tl_line.cbuf + len, tcap->tc_cbuf, tcap->tc_count);
1365 len += tcap->tc_count;
1366 }while(++tcap, --i > 0);
1369 tlp->tl_line.cbuf[len] = '\0';
1370 NYD2_LEAVE;
1371 return (ssize_t)len;
1374 static void
1375 a_tty_cell2save(struct a_tty_line *tlp){
1376 size_t len, i;
1377 struct a_tty_cell *tcap;
1378 NYD2_ENTER;
1380 tlp->tl_savec.s = NULL;
1381 tlp->tl_savec.l = 0;
1383 if(n_UNLIKELY(tlp->tl_count == 0))
1384 goto jleave;
1386 for(tcap = tlp->tl_line.cells, len = 0, i = tlp->tl_count; i > 0;
1387 ++tcap, --i)
1388 len += tcap->tc_count;
1390 tlp->tl_savec.s = n_autorec_alloc((tlp->tl_savec.l = len) +1);
1392 for(tcap = tlp->tl_line.cells, len = 0, i = tlp->tl_count; i > 0;
1393 ++tcap, --i){
1394 memcpy(tlp->tl_savec.s + len, tcap->tc_cbuf, tcap->tc_count);
1395 len += tcap->tc_count;
1397 tlp->tl_savec.s[len] = '\0';
1398 jleave:
1399 NYD2_LEAVE;
1402 static void
1403 a_tty_copy2paste(struct a_tty_line *tlp, struct a_tty_cell *tcpmin,
1404 struct a_tty_cell *tcpmax){
1405 char *cp;
1406 struct a_tty_cell *tcp;
1407 size_t l;
1408 NYD2_ENTER;
1410 l = 0;
1411 for(tcp = tcpmin; tcp < tcpmax; ++tcp)
1412 l += tcp->tc_count;
1414 tlp->tl_pastebuf.s = cp = n_autorec_alloc((tlp->tl_pastebuf.l = l) +1);
1416 for(tcp = tcpmin; tcp < tcpmax; cp += l, ++tcp)
1417 memcpy(cp, tcp->tc_cbuf, l = tcp->tc_count);
1418 *cp = '\0';
1419 NYD2_LEAVE;
1422 static wchar_t
1423 a_tty_vinuni(struct a_tty_line *tlp){
1424 char buf[16];
1425 uiz_t i;
1426 wchar_t wc;
1427 NYD2_ENTER;
1429 wc = '\0';
1431 if(!n_termcap_cmdx(n_TERMCAP_CMD_cr) ||
1432 !n_termcap_cmd(n_TERMCAP_CMD_ce, 0, -1))
1433 goto jleave;
1435 /* C99 */{
1436 struct str const *cpre, *csuf;
1438 cpre = csuf = NULL;
1439 #ifdef HAVE_COLOUR
1440 if(n_COLOUR_IS_ACTIVE()){
1441 struct n_colour_pen *cpen;
1443 cpen = n_colour_pen_create(n_COLOUR_ID_MLE_PROMPT, NULL);
1444 if((cpre = n_colour_pen_to_str(cpen)) != NULL)
1445 csuf = n_colour_reset_to_str();
1447 #endif
1448 fprintf(n_tty_fp, _("%sPlease enter Unicode code point:%s "),
1449 (cpre != NULL ? cpre->s : n_empty),
1450 (csuf != NULL ? csuf->s : n_empty));
1452 fflush(n_tty_fp);
1454 buf[sizeof(buf) -1] = '\0';
1455 for(i = 0;;){
1456 if(read(STDIN_FILENO, &buf[i], 1) != 1){
1457 if(n_err_no == n_ERR_INTR) /* xxx #if !SA_RESTART ? */
1458 continue;
1459 goto jleave;
1461 if(buf[i] == '\n')
1462 break;
1463 if(!hexchar(buf[i])){
1464 char const emsg[] = "[0-9a-fA-F]";
1466 n_LCTA(sizeof emsg <= sizeof(buf), "Preallocated buffer too small");
1467 memcpy(buf, emsg, sizeof emsg);
1468 goto jerr;
1471 putc(buf[i], n_tty_fp);
1472 fflush(n_tty_fp);
1473 if(++i == sizeof buf)
1474 goto jerr;
1476 buf[i] = '\0';
1478 if((n_idec_uiz_cp(&i, buf, 16, NULL
1479 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
1480 ) != n_IDEC_STATE_CONSUMED || i > 0x10FFFF/* XXX magic; CText */){
1481 jerr:
1482 n_err(_("\nInvalid input: %s\n"), buf);
1483 goto jleave;
1486 wc = (wchar_t)i;
1487 jleave:
1488 tlp->tl_vi_flags |= a_TTY_VF_MOD_DIRTY | (wc == '\0' ? a_TTY_VF_BELL : 0);
1489 NYD2_LEAVE;
1490 return wc;
1493 static bool_t
1494 a_tty_vi_refresh(struct a_tty_line *tlp){
1495 bool_t rv;
1496 NYD2_ENTER;
1498 if(tlp->tl_vi_flags & a_TTY_VF_BELL){
1499 tlp->tl_vi_flags |= a_TTY_VF_SYNC;
1500 if(putc('\a', n_tty_fp) == EOF)
1501 goto jerr;
1504 if(tlp->tl_vi_flags & a_TTY_VF_REFRESH){
1505 /* kht may want to restore a cursor position after inserting some
1506 * data somewhere */
1507 if(tlp->tl_defc_cursor_byte > 0){
1508 size_t i, j;
1509 ssize_t k;
1511 a_tty_khome(tlp, FAL0);
1513 i = tlp->tl_defc_cursor_byte;
1514 tlp->tl_defc_cursor_byte = 0;
1515 for(j = 0; tlp->tl_cursor < tlp->tl_count; ++j){
1516 a_tty_kright(tlp);
1517 if((k = tlp->tl_line.cells[j].tc_count) > i)
1518 break;
1519 i -= k;
1523 if(!a_tty_vi__paint(tlp))
1524 goto jerr;
1527 if(tlp->tl_vi_flags & a_TTY_VF_SYNC){
1528 tlp->tl_vi_flags &= ~a_TTY_VF_SYNC;
1529 if(fflush(n_tty_fp))
1530 goto jerr;
1533 rv = TRU1;
1534 jleave:
1535 tlp->tl_vi_flags &= ~a_TTY_VF_ALL_MASK;
1536 NYD2_LEAVE;
1537 return rv;
1539 jerr:
1540 clearerr(n_tty_fp); /* xxx I/O layer rewrite */
1541 n_err(_("Visual refresh failed! Is $TERM set correctly?\n"
1542 " Setting *line-editor-disable* to get us through!\n"));
1543 ok_bset(line_editor_disable);
1544 rv = FAL0;
1545 goto jleave;
1548 static bool_t
1549 a_tty_vi__paint(struct a_tty_line *tlp){
1550 enum{
1551 a_TRUE_RV = a_TTY__VF_LAST<<1, /* Return value bit */
1552 a_HAVE_PROMPT = a_TTY__VF_LAST<<2, /* Have a prompt */
1553 a_SHOW_PROMPT = a_TTY__VF_LAST<<3, /* Shall print the prompt */
1554 a_MOVE_CURSOR = a_TTY__VF_LAST<<4, /* Move visual cursor for user! */
1555 a_LEFT_MIN = a_TTY__VF_LAST<<5, /* On left boundary */
1556 a_RIGHT_MAX = a_TTY__VF_LAST<<6,
1557 a_HAVE_POSITION = a_TTY__VF_LAST<<7, /* Print the position indicator */
1559 /* We carry some flags over invocations (not worth a specific field) */
1560 a_VISIBLE_PROMPT = a_TTY__VF_LAST<<8, /* The prompt is on the screen */
1561 a_PERSIST_MASK = a_VISIBLE_PROMPT,
1562 a__LAST = a_PERSIST_MASK
1565 ui32_t f, w, phy_wid_base, phy_wid, phy_base, phy_cur, cnt,
1566 DBG(lstcur COMMA) cur,
1567 vi_left, /*vi_right,*/ phy_nxtcur;
1568 struct a_tty_cell const *tccp, *tcp_left, *tcp_right, *tcxp;
1569 NYD2_ENTER;
1570 n_LCTA(UICMP(64, a__LAST, <, UI32_MAX), "Flag bits excess storage datatype");
1572 f = tlp->tl_vi_flags;
1573 tlp->tl_vi_flags = (f & ~(a_TTY_VF_REFRESH | a_PERSIST_MASK)) |
1574 a_TTY_VF_SYNC;
1575 f |= a_TRUE_RV;
1576 if((w = tlp->tl_prompt_width) > 0)
1577 f |= a_HAVE_PROMPT;
1578 f |= a_HAVE_POSITION;
1580 /* XXX We don't have a OnTerminalResize event (see main.c) yet, so we need
1581 * XXX to reevaluate our circumstances over and over again */
1582 /* Don't display prompt or position indicator on very small screens */
1583 if((phy_wid_base = (ui32_t)n_scrnwidth) <= a_TTY_WIDTH_RIPOFF)
1584 f &= ~(a_HAVE_PROMPT | a_HAVE_POSITION);
1585 else{
1586 phy_wid_base -= a_TTY_WIDTH_RIPOFF;
1588 /* Disable the prompt if the screen is too small; due to lack of some
1589 * indicator simply add a second ripoff */
1590 if((f & a_HAVE_PROMPT) && w + a_TTY_WIDTH_RIPOFF >= phy_wid_base)
1591 f &= ~a_HAVE_PROMPT;
1594 phy_wid = phy_wid_base;
1595 phy_base = 0;
1596 phy_cur = tlp->tl_phy_cursor;
1597 cnt = tlp->tl_count;
1598 DBG( lstcur = tlp->tl_lst_cursor; )
1600 /* XXX Assume dirty screen if shrunk */
1601 if(cnt < tlp->tl_lst_count)
1602 f |= a_TTY_VF_MOD_DIRTY;
1604 /* TODO Without HAVE_TERMCAP, it would likely be much cheaper to simply
1605 * TODO always "cr + paint + ce + ch", since ce is simulated via spaces.. */
1607 /* Quickshot: if the line is empty, possibly print prompt and out */
1608 if(cnt == 0){
1609 /* In that special case dirty anything if it seems better */
1610 if((f & a_TTY_VF_MOD_CONTENT) || tlp->tl_lst_count > 0)
1611 f |= a_TTY_VF_MOD_DIRTY;
1613 if((f & a_TTY_VF_MOD_DIRTY) && phy_cur != 0){
1614 if(!n_termcap_cmdx(n_TERMCAP_CMD_cr))
1615 goto jerr;
1616 phy_cur = 0;
1619 if((f & (a_TTY_VF_MOD_DIRTY | a_HAVE_PROMPT)) ==
1620 (a_TTY_VF_MOD_DIRTY | a_HAVE_PROMPT)){
1621 if(fputs(tlp->tl_prompt, n_tty_fp) == EOF)
1622 goto jerr;
1623 phy_cur = tlp->tl_prompt_width + 1;
1626 /* May need to clear former line content */
1627 if((f & a_TTY_VF_MOD_DIRTY) &&
1628 !n_termcap_cmd(n_TERMCAP_CMD_ce, phy_cur, -1))
1629 goto jerr;
1631 tlp->tl_phy_start = tlp->tl_line.cells;
1632 goto jleave;
1635 /* Try to get an idea of the visual window */
1637 /* Find the left visual boundary */
1638 phy_wid = (phy_wid >> 1) + (phy_wid >> 2);
1639 if((cur = tlp->tl_cursor) == cnt)
1640 --cur;
1642 w = (tcp_left = tccp = tlp->tl_line.cells + cur)->tc_width;
1643 if(w == UI8_MAX) /* TODO yet HT == SPACE */
1644 w = 1;
1645 while(tcp_left > tlp->tl_line.cells){
1646 ui16_t cw = tcp_left[-1].tc_width;
1648 if(cw == UI8_MAX) /* TODO yet HT == SPACE */
1649 cw = 1;
1650 if(w + cw >= phy_wid)
1651 break;
1652 w += cw;
1653 --tcp_left;
1655 vi_left = w;
1657 /* If the left hand side of our visual viewpoint consumes less than half
1658 * of the screen width, show the prompt */
1659 if(tcp_left == tlp->tl_line.cells)
1660 f |= a_LEFT_MIN;
1662 if((f & (a_LEFT_MIN | a_HAVE_PROMPT)) == (a_LEFT_MIN | a_HAVE_PROMPT) &&
1663 w + tlp->tl_prompt_width < phy_wid){
1664 phy_base = tlp->tl_prompt_width;
1665 f |= a_SHOW_PROMPT;
1668 /* Then search for right boundary. We always leave the rightmost column
1669 * empty because some terminals [cw]ould wrap the line if we write into
1670 * that. XXX terminfo(5)/termcap(5) have the semi_auto_right_margin/sam/YE
1671 * XXX capability to indicate this, but we don't look at that */
1672 phy_wid = phy_wid_base - phy_base;
1673 tcp_right = tlp->tl_line.cells + cnt;
1675 while(&tccp[1] < tcp_right){
1676 ui16_t cw = tccp[1].tc_width;
1677 ui32_t i;
1679 if(cw == UI8_MAX) /* TODO yet HT == SPACE */
1680 cw = 1;
1681 i = w + cw;
1682 if(i > phy_wid)
1683 break;
1684 w = i;
1685 ++tccp;
1687 /*vi_right = w - vi_left;*/
1689 /* If the complete line including prompt fits on the screen, show prompt */
1690 if(--tcp_right == tccp){
1691 f |= a_RIGHT_MAX;
1693 /* Since we did brute-force walk also for the left boundary we may end up
1694 * in a situation were anything effectively fits on the screen, including
1695 * the prompt that is, but were we don't recognize this since we
1696 * restricted the search to fit in some visual viewpoint. Therefore try
1697 * again to extend the left boundary to overcome that */
1698 if(!(f & a_LEFT_MIN)){
1699 struct a_tty_cell const *tc1p = tlp->tl_line.cells;
1700 ui32_t vil1 = vi_left;
1702 assert(!(f & a_SHOW_PROMPT));
1703 w += tlp->tl_prompt_width;
1704 for(tcxp = tcp_left;;){
1705 ui32_t i = tcxp[-1].tc_width;
1707 if(i == UI8_MAX) /* TODO yet HT == SPACE */
1708 i = 1;
1709 vil1 += i;
1710 i += w;
1711 if(i > phy_wid)
1712 break;
1713 w = i;
1714 if(--tcxp == tc1p){
1715 tcp_left = tc1p;
1716 /*vi_left = vil1;*/
1717 f |= a_LEFT_MIN;
1718 break;
1721 /*w -= tlp->tl_prompt_width;*/
1724 tcp_right = tccp;
1725 tccp = tlp->tl_line.cells + cur;
1727 if((f & (a_LEFT_MIN | a_RIGHT_MAX | a_HAVE_PROMPT | a_SHOW_PROMPT)) ==
1728 (a_LEFT_MIN | a_RIGHT_MAX | a_HAVE_PROMPT) &&
1729 w + tlp->tl_prompt_width <= phy_wid){
1730 phy_wid -= (phy_base = tlp->tl_prompt_width);
1731 f |= a_SHOW_PROMPT;
1734 /* Try to avoid repainting the complete line - this is possible if the
1735 * cursor "did not leave the screen" and the prompt status hasn't changed.
1736 * I.e., after clamping virtual viewpoint, compare relation to physical */
1737 if((f & (a_TTY_VF_MOD_SINGLE/*FIXME*/ |
1738 a_TTY_VF_MOD_CONTENT/* xxx */ | a_TTY_VF_MOD_DIRTY)) ||
1739 (tcxp = tlp->tl_phy_start) == NULL ||
1740 tcxp > tccp || tcxp <= tcp_right)
1741 f |= a_TTY_VF_MOD_DIRTY;
1742 else{
1743 f |= a_TTY_VF_MOD_DIRTY;
1744 #if 0
1745 struct a_tty_cell const *tcyp;
1746 si32_t cur_displace;
1747 ui32_t phy_lmargin, phy_rmargin, fx, phy_displace;
1749 phy_lmargin = (fx = phy_wid) / 100;
1750 phy_rmargin = fx - (phy_lmargin * a_TTY_SCROLL_MARGIN_RIGHT);
1751 phy_lmargin *= a_TTY_SCROLL_MARGIN_LEFT;
1752 fx = (f & (a_SHOW_PROMPT | a_VISIBLE_PROMPT));
1754 if(fx == 0 || fx == (a_SHOW_PROMPT | a_VISIBLE_PROMPT)){
1756 #endif
1758 goto jpaint;
1760 /* We know what we have to paint, start synchronizing */
1761 jpaint:
1762 assert(phy_cur == tlp->tl_phy_cursor);
1763 assert(phy_wid == phy_wid_base - phy_base);
1764 assert(cnt == tlp->tl_count);
1765 assert(cnt > 0);
1766 assert(lstcur == tlp->tl_lst_cursor);
1767 assert(tccp == tlp->tl_line.cells + cur);
1769 phy_nxtcur = phy_base; /* FIXME only if repaint cpl. */
1771 /* Quickshot: is it only cursor movement within the visible screen? */
1772 if((f & a_TTY_VF_REFRESH) == a_TTY_VF_MOD_CURSOR){
1773 f |= a_MOVE_CURSOR;
1774 goto jcursor;
1777 /* To be able to apply some quick jump offs, clear line if possible */
1778 if(f & a_TTY_VF_MOD_DIRTY){
1779 /* Force complete clearance and cursor reinitialization */
1780 if(!n_termcap_cmdx(n_TERMCAP_CMD_cr) ||
1781 !n_termcap_cmd(n_TERMCAP_CMD_ce, 0, -1))
1782 goto jerr;
1783 tlp->tl_phy_start = tcp_left;
1784 phy_cur = 0;
1787 if((f & (a_TTY_VF_MOD_DIRTY | a_SHOW_PROMPT)) && phy_cur != 0){
1788 if(!n_termcap_cmdx(n_TERMCAP_CMD_cr))
1789 goto jerr;
1790 phy_cur = 0;
1793 if(f & a_SHOW_PROMPT){
1794 assert(phy_base == tlp->tl_prompt_width);
1795 if(fputs(tlp->tl_prompt, n_tty_fp) == EOF)
1796 goto jerr;
1797 phy_cur = phy_nxtcur;
1798 f |= a_VISIBLE_PROMPT;
1799 }else
1800 f &= ~a_VISIBLE_PROMPT;
1802 /* FIXME reposition cursor for paint */
1803 for(w = phy_nxtcur; tcp_left <= tcp_right; ++tcp_left){
1804 ui16_t cw;
1806 cw = tcp_left->tc_width;
1808 if(n_LIKELY(!tcp_left->tc_novis)){
1809 if(fwrite(tcp_left->tc_cbuf, sizeof *tcp_left->tc_cbuf,
1810 tcp_left->tc_count, n_tty_fp) != tcp_left->tc_count)
1811 goto jerr;
1812 }else{ /* XXX Shouldn't be here <-> CText, ui_str.c */
1813 char wbuf[8]; /* XXX magic */
1815 if(n_psonce & n_PSO_UNICODE){
1816 ui32_t wc;
1818 wc = (ui32_t)tcp_left->tc_wc;
1819 if((wc & ~0x1Fu) == 0)
1820 wc |= 0x2400;
1821 else if(wc == 0x7F)
1822 wc = 0x2421;
1823 else
1824 wc = 0x2426;
1825 n_utf32_to_utf8(wc, wbuf);
1826 }else
1827 wbuf[0] = '?', wbuf[1] = '\0';
1829 if(fputs(wbuf, n_tty_fp) == EOF)
1830 goto jerr;
1831 cw = 1;
1834 if(cw == UI8_MAX) /* TODO yet HT == SPACE */
1835 cw = 1;
1836 w += cw;
1837 if(tcp_left == tccp)
1838 phy_nxtcur = w;
1839 phy_cur += cw;
1842 /* Write something position marker alike if it does not fit on screen */
1843 if((f & a_HAVE_POSITION) &&
1844 ((f & (a_LEFT_MIN | a_RIGHT_MAX)) != (a_LEFT_MIN | a_RIGHT_MAX) ||
1845 ((f & a_HAVE_PROMPT) && !(f & a_SHOW_PROMPT)))){
1846 # ifdef HAVE_COLOUR
1847 char *posbuf = tlp->tl_pos_buf, *pos = tlp->tl_pos;
1848 # else
1849 char posbuf[5], *pos = posbuf;
1851 pos[4] = '\0';
1852 # endif
1854 if(phy_cur != (w = phy_wid_base) &&
1855 !n_termcap_cmd(n_TERMCAP_CMD_ch, phy_cur = w, 0))
1856 goto jerr;
1858 *pos++ = '|';
1859 if((f & a_LEFT_MIN) && (!(f & a_HAVE_PROMPT) || (f & a_SHOW_PROMPT)))
1860 memcpy(pos, "^.+", 3);
1861 else if(f & a_RIGHT_MAX)
1862 memcpy(pos, ".+$", 3);
1863 else{
1864 /* Theoretical line length limit a_TTY_LINE_MAX, choose next power of
1865 * ten (10 ** 10) to represent 100 percent, since we don't have a macro
1866 * that generates a constant, and i don't trust the standard "u type
1867 * suffix automatically scales" calculate the large number */
1868 static char const itoa[] = "0123456789";
1870 ui64_t const fact100 = (ui64_t)0x3B9ACA00u * 10u, fact = fact100 / 100;
1871 ui32_t i = (ui32_t)(((fact100 / cnt) * tlp->tl_cursor) / fact);
1872 n_LCTA(a_TTY_LINE_MAX <= SI32_MAX, "a_TTY_LINE_MAX too large");
1874 if(i < 10)
1875 pos[0] = ' ', pos[1] = itoa[i];
1876 else
1877 pos[1] = itoa[i % 10], pos[0] = itoa[i / 10];
1878 pos[2] = '%';
1881 if(fputs(posbuf, n_tty_fp) == EOF)
1882 goto jerr;
1883 phy_cur += 4;
1886 /* Users are used to see the cursor right of the point of interest, so we
1887 * need some further adjustments unless in special conditions. Be aware
1888 * that we may have adjusted cur at the beginning, too */
1889 if((cur = tlp->tl_cursor) == 0)
1890 phy_nxtcur = phy_base;
1891 else if(cur != cnt){
1892 ui16_t cw = tccp->tc_width;
1894 if(cw == UI8_MAX) /* TODO yet HT == SPACE */
1895 cw = 1;
1896 phy_nxtcur -= cw;
1899 jcursor:
1900 if(((f & a_MOVE_CURSOR) || phy_nxtcur != phy_cur) &&
1901 !n_termcap_cmd(n_TERMCAP_CMD_ch, phy_cur = phy_nxtcur, 0))
1902 goto jerr;
1904 jleave:
1905 tlp->tl_vi_flags |= (f & a_PERSIST_MASK);
1906 tlp->tl_lst_count = tlp->tl_count;
1907 tlp->tl_lst_cursor = tlp->tl_cursor;
1908 tlp->tl_phy_cursor = phy_cur;
1910 NYD2_LEAVE;
1911 return ((f & a_TRUE_RV) != 0);
1912 jerr:
1913 f &= ~a_TRUE_RV;
1914 goto jleave;
1917 static si32_t
1918 a_tty_wboundary(struct a_tty_line *tlp, si32_t dir){/* TODO shell token-wise */
1919 bool_t anynon;
1920 struct a_tty_cell *tcap;
1921 ui32_t cur, cnt;
1922 si32_t rv;
1923 NYD2_ENTER;
1925 assert(dir == 1 || dir == -1);
1927 rv = -1;
1928 cnt = tlp->tl_count;
1929 cur = tlp->tl_cursor;
1931 if(dir < 0){
1932 if(cur == 0)
1933 goto jleave;
1934 }else if(cur + 1 >= cnt)
1935 goto jleave;
1936 else
1937 --cnt, --cur; /* xxx Unsigned wrapping may occur (twice), then */
1939 for(rv = 0, tcap = tlp->tl_line.cells, anynon = FAL0;;){
1940 wchar_t wc;
1942 wc = tcap[cur += (ui32_t)dir].tc_wc;
1943 if(/*TODO not everywhere iswblank(wc)*/ wc == L' ' || wc == L'\t' ||
1944 iswpunct(wc)){
1945 if(anynon)
1946 break;
1947 }else
1948 anynon = TRU1;
1950 ++rv;
1952 if(dir < 0){
1953 if(cur == 0)
1954 break;
1955 }else if(cur + 1 >= cnt){
1956 ++rv;
1957 break;
1960 jleave:
1961 NYD2_LEAVE;
1962 return rv;
1965 static void
1966 a_tty_khome(struct a_tty_line *tlp, bool_t dobell){
1967 ui32_t f;
1968 NYD2_ENTER;
1970 if(n_LIKELY(tlp->tl_cursor > 0)){
1971 tlp->tl_cursor = 0;
1972 f = a_TTY_VF_MOD_CURSOR;
1973 }else if(dobell)
1974 f = a_TTY_VF_BELL;
1975 else
1976 f = a_TTY_VF_NONE;
1978 tlp->tl_vi_flags |= f;
1979 NYD2_LEAVE;
1982 static void
1983 a_tty_kend(struct a_tty_line *tlp){
1984 ui32_t f;
1985 NYD2_ENTER;
1987 if(n_LIKELY(tlp->tl_cursor < tlp->tl_count)){
1988 tlp->tl_cursor = tlp->tl_count;
1989 f = a_TTY_VF_MOD_CURSOR;
1990 }else
1991 f = a_TTY_VF_BELL;
1993 tlp->tl_vi_flags |= f;
1994 NYD2_LEAVE;
1997 static void
1998 a_tty_kbs(struct a_tty_line *tlp){
1999 ui32_t f, cur, cnt;
2000 NYD2_ENTER;
2002 cur = tlp->tl_cursor;
2003 cnt = tlp->tl_count;
2005 if(n_LIKELY(cur > 0)){
2006 tlp->tl_cursor = --cur;
2007 tlp->tl_count = --cnt;
2009 if((cnt -= cur) > 0){
2010 struct a_tty_cell *tcap;
2012 tcap = tlp->tl_line.cells + cur;
2013 memmove(tcap, &tcap[1], cnt *= sizeof(*tcap));
2015 f = a_TTY_VF_MOD_CURSOR | a_TTY_VF_MOD_CONTENT;
2016 }else
2017 f = a_TTY_VF_BELL;
2019 tlp->tl_vi_flags |= f;
2020 NYD2_LEAVE;
2023 static void
2024 a_tty_ksnarf(struct a_tty_line *tlp, bool_t cplline, bool_t dobell){
2025 ui32_t i, f;
2026 NYD2_ENTER;
2028 f = a_TTY_VF_NONE;
2029 i = tlp->tl_cursor;
2031 if(cplline && i > 0){
2032 tlp->tl_cursor = i = 0;
2033 f = a_TTY_VF_MOD_CURSOR;
2036 if(n_LIKELY(i < tlp->tl_count)){
2037 struct a_tty_cell *tcap;
2039 tcap = &tlp->tl_line.cells[0];
2040 a_tty_copy2paste(tlp, &tcap[i], &tcap[tlp->tl_count]);
2041 tlp->tl_count = i;
2042 f = a_TTY_VF_MOD_CONTENT;
2043 }else if(dobell)
2044 f |= a_TTY_VF_BELL;
2046 tlp->tl_vi_flags |= f;
2047 NYD2_LEAVE;
2050 static si32_t
2051 a_tty_kdel(struct a_tty_line *tlp){
2052 ui32_t cur, cnt, f;
2053 si32_t i;
2054 NYD2_ENTER;
2056 cur = tlp->tl_cursor;
2057 cnt = tlp->tl_count;
2058 i = (si32_t)(cnt - cur);
2060 if(n_LIKELY(i > 0)){
2061 tlp->tl_count = --cnt;
2063 if(n_LIKELY(--i > 0)){
2064 struct a_tty_cell *tcap;
2066 tcap = &tlp->tl_line.cells[cur];
2067 memmove(tcap, &tcap[1], (ui32_t)i * sizeof(*tcap));
2069 f = a_TTY_VF_MOD_CONTENT;
2070 }else if(cnt == 0 && !ok_blook(ignoreeof)){
2071 putc('^', n_tty_fp);
2072 putc('D', n_tty_fp);
2073 i = -1;
2074 f = a_TTY_VF_NONE;
2075 }else{
2076 i = 0;
2077 f = a_TTY_VF_BELL;
2080 tlp->tl_vi_flags |= f;
2081 NYD2_LEAVE;
2082 return i;
2085 static void
2086 a_tty_kleft(struct a_tty_line *tlp){
2087 ui32_t f;
2088 NYD2_ENTER;
2090 if(n_LIKELY(tlp->tl_cursor > 0)){
2091 --tlp->tl_cursor;
2092 f = a_TTY_VF_MOD_CURSOR;
2093 }else
2094 f = a_TTY_VF_BELL;
2096 tlp->tl_vi_flags |= f;
2097 NYD2_LEAVE;
2100 static void
2101 a_tty_kright(struct a_tty_line *tlp){
2102 ui32_t i;
2103 NYD2_ENTER;
2105 if(n_LIKELY((i = tlp->tl_cursor + 1) <= tlp->tl_count)){
2106 tlp->tl_cursor = i;
2107 i = a_TTY_VF_MOD_CURSOR;
2108 }else
2109 i = a_TTY_VF_BELL;
2111 tlp->tl_vi_flags |= i;
2112 NYD2_LEAVE;
2115 static void
2116 a_tty_ksnarfw(struct a_tty_line *tlp, bool_t fwd){
2117 struct a_tty_cell *tcap;
2118 ui32_t cnt, cur, f;
2119 si32_t i;
2120 NYD2_ENTER;
2122 if(n_UNLIKELY((i = a_tty_wboundary(tlp, (fwd ? +1 : -1))) <= 0)){
2123 f = (i < 0) ? a_TTY_VF_BELL : a_TTY_VF_NONE;
2124 goto jleave;
2127 cnt = tlp->tl_count - (ui32_t)i;
2128 cur = tlp->tl_cursor;
2129 if(!fwd)
2130 cur -= (ui32_t)i;
2131 tcap = &tlp->tl_line.cells[cur];
2133 a_tty_copy2paste(tlp, &tcap[0], &tcap[i]);
2135 if((tlp->tl_count = cnt) != (tlp->tl_cursor = cur)){
2136 cnt -= cur;
2137 memmove(&tcap[0], &tcap[i], cnt * sizeof(*tcap)); /* FIXME*/
2140 f = a_TTY_VF_MOD_CURSOR | a_TTY_VF_MOD_CONTENT;
2141 jleave:
2142 tlp->tl_vi_flags |= f;
2143 NYD2_LEAVE;
2146 static void
2147 a_tty_kgow(struct a_tty_line *tlp, si32_t dir){
2148 ui32_t f;
2149 si32_t i;
2150 NYD2_ENTER;
2152 if(n_UNLIKELY((i = a_tty_wboundary(tlp, dir)) <= 0))
2153 f = (i < 0) ? a_TTY_VF_BELL : a_TTY_VF_NONE;
2154 else{
2155 if(dir < 0)
2156 i = -i;
2157 tlp->tl_cursor += (ui32_t)i;
2158 f = a_TTY_VF_MOD_CURSOR;
2161 tlp->tl_vi_flags |= f;
2162 NYD2_LEAVE;
2165 static void
2166 a_tty_kgoscr(struct a_tty_line *tlp, si32_t dir){
2167 ui32_t sw, i, cur, f, cnt;
2168 NYD2_ENTER;
2170 sw = (ui32_t)n_scrnwidth - 2;
2171 if(sw > (i = tlp->tl_prompt_width))
2172 sw -= i;
2173 cur = tlp->tl_cursor;
2174 f = a_TTY_VF_BELL;
2176 if(dir > 0){
2177 for(cnt = tlp->tl_count; cur < cnt && sw > 0; ++cur){
2178 i = tlp->tl_line.cells[cur].tc_width;
2179 i = n_MIN(sw, i);
2180 sw -= i;
2182 }else{
2183 while(cur > 0 && sw > 0){
2184 i = tlp->tl_line.cells[--cur].tc_width;
2185 i = n_MIN(sw, i);
2186 sw -= i;
2189 if(cur != tlp->tl_cursor){
2190 tlp->tl_cursor = cur;
2191 f = a_TTY_VF_MOD_CURSOR;
2194 tlp->tl_vi_flags |= f;
2195 NYD2_LEAVE;
2198 static bool_t
2199 a_tty_kother(struct a_tty_line *tlp, wchar_t wc){
2200 /* Append if at EOL, insert otherwise;
2201 * since we may move around character-wise, always use a fresh ps */
2202 mbstate_t ps;
2203 struct a_tty_cell tc, *tcap;
2204 ui32_t f, cur, cnt;
2205 bool_t rv;
2206 NYD2_ENTER;
2208 rv = FAL0;
2209 f = a_TTY_VF_NONE;
2211 n_LCTA(a_TTY_LINE_MAX <= SI32_MAX, "a_TTY_LINE_MAX too large");
2212 if(tlp->tl_count + 1 >= a_TTY_LINE_MAX){
2213 n_err(_("Stop here, we can't extend line beyond size limit\n"));
2214 goto jleave;
2217 /* First init a cell and see whether we'll really handle this wc */
2218 memset(&ps, 0, sizeof ps);
2219 /* C99 */{
2220 size_t l;
2222 l = wcrtomb(tc.tc_cbuf, tc.tc_wc = wc, &ps);
2223 if(n_UNLIKELY(l > MB_LEN_MAX)){
2224 jemb:
2225 n_err(_("wcrtomb(3) error: too many multibyte character bytes\n"));
2226 goto jleave;
2228 tc.tc_count = (ui16_t)l;
2230 if(n_UNLIKELY((n_psonce & n_PSO_ENC_MBSTATE) != 0)){
2231 l = wcrtomb(&tc.tc_cbuf[l], L'\0', &ps);
2232 if(n_LIKELY(l == 1))
2233 /* Only NUL terminator */;
2234 else if(n_LIKELY(--l < MB_LEN_MAX))
2235 tc.tc_count += (ui16_t)l;
2236 else
2237 goto jemb;
2241 /* Yes, we will! Place it in the array */
2242 tc.tc_novis = (iswprint(wc) == 0);
2243 tc.tc_width = a_tty_wcwidth(wc);
2244 /* TODO if(tc.tc_novis && tc.tc_width > 0) */
2246 cur = tlp->tl_cursor++;
2247 cnt = tlp->tl_count++ - cur;
2248 tcap = &tlp->tl_line.cells[cur];
2249 if(cnt >= 1){
2250 memmove(&tcap[1], tcap, cnt * sizeof(*tcap));
2251 f = a_TTY_VF_MOD_CONTENT;
2252 }else
2253 f = a_TTY_VF_MOD_SINGLE;
2254 memcpy(tcap, &tc, sizeof *tcap);
2256 f |= a_TTY_VF_MOD_CURSOR;
2257 rv = TRU1;
2258 jleave:
2259 if(!rv)
2260 f |= a_TTY_VF_BELL;
2261 tlp->tl_vi_flags |= f;
2262 NYD2_LEAVE;
2263 return rv;
2266 static ui32_t
2267 a_tty_kht(struct a_tty_line *tlp){
2268 ui8_t (*mempool)[n_MEMORY_POOL_TYPE_SIZEOF], *mempool_persist;
2269 struct stat sb;
2270 struct str orig, bot, topp, sub, exp, preexp;
2271 struct n_string shou, *shoup;
2272 struct a_tty_cell *ctop, *cx;
2273 bool_t wedid, set_savec;
2274 ui32_t rv, f;
2275 NYD2_ENTER;
2277 /* Get plain line data; if this is the first expansion/xy, update the
2278 * very original content so that ^G gets the origin back */
2279 orig = tlp->tl_savec;
2280 a_tty_cell2save(tlp);
2281 exp = tlp->tl_savec;
2282 if(orig.s != NULL){
2283 /*tlp->tl_savec = orig;*/
2284 set_savec = FAL0;
2285 }else
2286 set_savec = TRU1;
2287 orig = exp;
2289 mempool_persist = n_memory_pool_top();
2290 n_memory_pool_push(mempool = n_lofi_alloc(sizeof *mempool), TRU1);
2292 shoup = n_string_creat_auto(&shou);
2293 f = a_TTY_VF_NONE;
2295 /* C99 */{
2296 size_t max;
2297 struct a_tty_cell *cword;
2299 /* Find the word to be expanded */
2300 cword = tlp->tl_line.cells;
2301 ctop = &cword[tlp->tl_cursor];
2302 cx = &cword[tlp->tl_count];
2304 /* topp: separate data right of cursor */
2305 if(cx > ctop){
2306 for(rv = 0; ctop < cx; ++ctop)
2307 rv += ctop->tc_count;
2308 topp.l = rv;
2309 topp.s = orig.s + orig.l - rv;
2310 ctop = cword + tlp->tl_cursor;
2311 }else
2312 topp.s = NULL, topp.l = 0;
2314 /* Find the shell token that corresponds to the cursor position */
2315 max = 0;
2316 if(ctop > cword){
2317 for(; cword < ctop; ++cword)
2318 max += cword->tc_count;
2320 bot = sub = orig;
2321 bot.l = 0;
2322 sub.l = max;
2324 if(max > 0){
2325 for(;;){
2326 enum n_shexp_state shs;
2328 exp = sub;
2329 shs = n_shexp_parse_token((n_SHEXP_PARSE_DRYRUN |
2330 n_SHEXP_PARSE_TRIM_SPACE | n_SHEXP_PARSE_IGNORE_EMPTY |
2331 n_SHEXP_PARSE_QUOTE_AUTO_CLOSE), NULL, &sub, NULL);
2332 if(sub.l != 0){
2333 size_t x;
2335 assert(max >= sub.l);
2336 x = max - sub.l;
2337 bot.l += x;
2338 max -= x;
2339 continue;
2341 if(shs & n_SHEXP_STATE_ERR_MASK){
2342 n_err(_("Invalid completion pattern: %.*s\n"),
2343 (int)exp.l, exp.s);
2344 f |= a_TTY_VF_BELL;
2345 goto jnope;
2348 /* All WS? Trailing WS that has been "jumped over"? */
2349 if(exp.l == 0 || (shs & n_SHEXP_STATE_WS_TRAIL))
2350 break;
2352 n_shexp_parse_token((n_SHEXP_PARSE_TRIM_SPACE |
2353 n_SHEXP_PARSE_IGNORE_EMPTY | n_SHEXP_PARSE_QUOTE_AUTO_CLOSE),
2354 shoup, &exp, NULL);
2355 break;
2358 sub.s = n_string_cp(shoup);
2359 sub.l = shoup->s_len;
2363 /* Leave room for "implicit asterisk" expansion, as below */
2364 if(sub.l == 0){
2365 sub.s = n_UNCONST(n_star);
2366 sub.l = sizeof(n_star) -1;
2369 preexp.s = n_UNCONST(n_empty);
2370 preexp.l = sizeof(n_empty) -1;
2371 wedid = FAL0;
2372 jredo:
2373 /* TODO Super-Heavy-Metal: block all sigs, avoid leaks on jump */
2374 hold_all_sigs();
2375 exp.s = fexpand(sub.s, a_TTY_TAB_FEXP_FL);
2376 rele_all_sigs();
2378 if(exp.s == NULL || (exp.l = strlen(exp.s)) == 0){
2379 if(wedid < FAL0)
2380 goto jnope;
2381 /* No. But maybe the users' desire was to complete only a part of the
2382 * shell token of interest! TODO This can be improved, we would need to
2383 * TODO have shexp_parse to create a DOM structure of parsed snippets, so
2384 * TODO that we can tell for each snippet which quote is active and
2385 * TODO whether we may cross its boundary and/or apply expansion for it!
2386 * TODO Only like that we would be able to properly requote user input
2387 * TODO like "'['a-z]<TAB>" to e.g. "\[a-z]" for glob purposes! */
2388 if(wedid == TRU1){
2389 size_t i, li;
2391 wedid = TRUM1;
2392 for(li = UIZ_MAX, i = sub.l; i-- > 0;){
2393 char c;
2395 if((c = sub.s[i]) == '/' || c == '+' /* *folder*! */)
2396 li = i;
2397 /* Do stop once some "magic" characters are seen XXX magic set */
2398 else if(c == '<' || c == '>' || c == '=' || c == ':')
2399 break;
2401 if(li != UIZ_MAX){
2402 preexp = sub;
2403 preexp.l = li;
2404 sub.l -= li;
2405 sub.s += li;
2406 goto jredo;
2410 /* A different case is that the user input includes for example character
2411 * classes: here fexpand() will go over glob, and that will not find any
2412 * match, thus returning NULL; try to wildcard expand this pattern! */
2413 jaster_check:
2414 if(sub.s[sub.l - 1] != '*'){
2415 wedid = TRU1;
2416 shoup = n_string_push_c(shoup, '*');
2417 sub.s = n_string_cp(shoup);
2418 sub.l = shoup->s_len;
2419 goto jredo;
2421 goto jnope;
2424 if(wedid == TRUM1 && preexp.l > 0)
2425 preexp.s = savestrbuf(preexp.s, preexp.l);
2427 /* May be multi-return! */
2428 if(n_pstate & n_PS_EXPAND_MULTIRESULT)
2429 goto jmulti;
2431 /* xxx That is not really true since the limit counts characters not bytes */
2432 n_LCTA(a_TTY_LINE_MAX <= SI32_MAX, "a_TTY_LINE_MAX too large");
2433 if(exp.l >= a_TTY_LINE_MAX - 1 || a_TTY_LINE_MAX - 1 - exp.l < preexp.l){
2434 n_err(_("Tabulator expansion would extend beyond line size limit\n"));
2435 f |= a_TTY_VF_BELL;
2436 goto jnope;
2439 /* If the expansion equals the original string, assume the user wants what
2440 * is usually known as tab completion, append `*' and restart */
2441 if(!wedid && exp.l == sub.l && !memcmp(exp.s, sub.s, exp.l))
2442 goto jaster_check;
2444 if(exp.s[exp.l - 1] != '/'){
2445 if(!stat(exp.s, &sb) && S_ISDIR(sb.st_mode)){
2446 shoup = n_string_assign_buf(shoup, exp.s, exp.l);
2447 shoup = n_string_push_c(shoup, '/');
2448 exp.s = n_string_cp(shoup);
2449 goto jset;
2452 exp.s[exp.l] = '\0';
2454 jset:
2455 exp.l = strlen(exp.s = n_shexp_quote_cp(exp.s, tlp->tl_quote_rndtrip));
2456 tlp->tl_defc_cursor_byte = bot.l + preexp.l + exp.l -1;
2458 orig.l = bot.l + preexp.l + exp.l + topp.l;
2459 orig.s = n_autorec_alloc_from_pool(mempool_persist, orig.l + 5 +1);
2460 if((rv = (ui32_t)bot.l) > 0)
2461 memcpy(orig.s, bot.s, rv);
2462 if(preexp.l > 0){
2463 memcpy(&orig.s[rv], preexp.s, preexp.l);
2464 rv += preexp.l;
2466 memcpy(&orig.s[rv], exp.s, exp.l);
2467 rv += exp.l;
2468 if(topp.l > 0){
2469 memcpy(&orig.s[rv], topp.s, topp.l);
2470 rv += topp.l;
2472 orig.s[rv] = '\0';
2474 tlp->tl_defc = orig;
2475 tlp->tl_count = tlp->tl_cursor = 0;
2476 f |= a_TTY_VF_MOD_DIRTY;
2477 jleave:
2478 n_memory_pool_pop(mempool, TRU1);
2479 n_lofi_free(mempool);
2480 tlp->tl_vi_flags |= f;
2481 NYD2_LEAVE;
2482 return rv;
2484 jmulti:{
2485 struct n_visual_info_ctx vic;
2486 struct str input;
2487 wc_t c2, c1;
2488 bool_t isfirst;
2489 char const *lococp;
2490 size_t locolen, scrwid, lnlen, lncnt, prefixlen;
2491 FILE *fp;
2493 if((fp = Ftmp(NULL, "tabex", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL){
2494 n_perr(_("tmpfile"), 0);
2495 fp = n_tty_fp;
2498 /* How long is the result string for real? Search the NUL NUL
2499 * terminator. While here, detect the longest entry to perform an
2500 * initial allocation of our accumulator string */
2501 locolen = preexp.l;
2503 size_t i;
2505 i = strlen(&exp.s[++exp.l]);
2506 locolen = n_MAX(locolen, i);
2507 exp.l += i;
2508 }while(exp.s[exp.l + 1] != '\0');
2510 shoup = n_string_reserve(n_string_trunc(shoup, 0),
2511 locolen + (locolen >> 1));
2513 /* Iterate (once again) over all results */
2514 scrwid = n_SCRNWIDTH_FOR_LISTS;
2515 lnlen = lncnt = 0;
2516 n_UNINIT(prefixlen, 0);
2517 n_UNINIT(lococp, NULL);
2518 n_UNINIT(c1, '\0');
2519 for(isfirst = TRU1; exp.l > 0; isfirst = FAL0, c1 = c2){
2520 size_t i;
2521 char const *fullpath;
2523 /* Next result */
2524 sub = exp;
2525 sub.l = i = strlen(sub.s);
2526 assert(exp.l >= i);
2527 if((exp.l -= i) > 0)
2528 --exp.l;
2529 exp.s += ++i;
2531 /* Separate dirname and basename */
2532 fullpath = sub.s;
2533 if(isfirst){
2534 char const *cp;
2536 if((cp = strrchr(fullpath, '/')) != NULL)
2537 prefixlen = PTR2SIZE(++cp - fullpath);
2538 else
2539 prefixlen = 0;
2541 if(prefixlen > 0 && prefixlen < sub.l){
2542 sub.l -= prefixlen;
2543 sub.s += prefixlen;
2546 /* We want case-insensitive sort-order */
2547 memset(&vic, 0, sizeof vic);
2548 vic.vic_indat = sub.s;
2549 vic.vic_inlen = sub.l;
2550 c2 = n_visual_info(&vic, n_VISUAL_INFO_ONE_CHAR) ? vic.vic_waccu
2551 : (ui8_t)*sub.s;
2552 #ifdef HAVE_C90AMEND1
2553 c2 = towlower(c2);
2554 #else
2555 c2 = lowerconv(c2);
2556 #endif
2558 /* Query longest common prefix along the way */
2559 if(isfirst){
2560 c1 = c2;
2561 lococp = sub.s;
2562 locolen = sub.l;
2563 }else if(locolen > 0){
2564 for(i = 0; i < locolen; ++i)
2565 if(lococp[i] != sub.s[i]){
2566 i = field_detect_clip(i, lococp, i);
2567 locolen = i;
2568 break;
2572 /* Prepare display */
2573 input = sub;
2574 shoup = n_shexp_quote(n_string_trunc(shoup, 0), &input,
2575 tlp->tl_quote_rndtrip);
2576 memset(&vic, 0, sizeof vic);
2577 vic.vic_indat = shoup->s_dat;
2578 vic.vic_inlen = shoup->s_len;
2579 if(!n_visual_info(&vic,
2580 n_VISUAL_INFO_SKIP_ERRORS | n_VISUAL_INFO_WIDTH_QUERY))
2581 vic.vic_vi_width = shoup->s_len;
2583 /* Put on screen. Indent follow lines of same sort slot.
2584 * Leave enough room for filename tagging */
2585 if((c1 = (c1 != c2))){
2586 #ifdef HAVE_C90AMEND1
2587 c1 = (iswalnum(c2) != 0);
2588 #else
2589 c1 = (alnumchar(c2) != 0);
2590 #endif
2592 if(isfirst || c1 ||
2593 scrwid < lnlen || scrwid - lnlen <= vic.vic_vi_width + 2){
2594 putc('\n', fp);
2595 if(scrwid < lnlen)
2596 ++lncnt;
2597 ++lncnt, lnlen = 0;
2598 if(!isfirst && !c1)
2599 goto jsep;
2600 }else if(lnlen > 0){
2601 jsep:
2602 fputs(" ", fp);
2603 lnlen += 2;
2605 fputs(n_string_cp(shoup), fp);
2606 lnlen += vic.vic_vi_width;
2608 /* Support the known filename tagging
2609 * XXX *line-editor-completion-filetype* or so */
2610 if(!lstat(fullpath, &sb)){
2611 char c = '\0';
2613 if(S_ISDIR(sb.st_mode))
2614 c = '/';
2615 else if(S_ISLNK(sb.st_mode))
2616 c = '@';
2617 # ifdef S_ISFIFO
2618 else if(S_ISFIFO(sb.st_mode))
2619 c = '|';
2620 # endif
2621 # ifdef S_ISSOCK
2622 else if(S_ISSOCK(sb.st_mode))
2623 c = '=';
2624 # endif
2625 # ifdef S_ISCHR
2626 else if(S_ISCHR(sb.st_mode))
2627 c = '%';
2628 # endif
2629 # ifdef S_ISBLK
2630 else if(S_ISBLK(sb.st_mode))
2631 c = '#';
2632 # endif
2634 if(c != '\0'){
2635 putc(c, fp);
2636 ++lnlen;
2640 putc('\n', fp);
2641 ++lncnt;
2643 page_or_print(fp, lncnt);
2644 if(fp != n_tty_fp)
2645 Fclose(fp);
2647 n_string_gut(shoup);
2649 /* A common prefix of 0 means we cannot provide the user any auto
2650 * completed characters for the multiple possible results.
2651 * Otherwise we can, so extend the visual line content by the common
2652 * prefix (in a reversible way) */
2653 f |= a_TTY_VF_BELL; /* XXX -> *line-editor-completion-bell*? or so */
2654 if(locolen > 0){
2655 (exp.s = n_UNCONST(lococp))[locolen] = '\0';
2656 exp.s -= prefixlen;
2657 exp.l = (locolen += prefixlen);
2658 goto jset;
2662 jnope:
2663 /* If we've provided a default content, but failed to expand, there is
2664 * nothing we can "revert to": drop that default again */
2665 if(set_savec){
2666 tlp->tl_savec.s = NULL;
2667 tlp->tl_savec.l = 0;
2669 f &= a_TTY_VF_BELL; /* XXX -> *line-editor-completion-bell*? or so */
2670 rv = 0;
2671 goto jleave;
2674 # ifdef HAVE_HISTORY
2675 static ui32_t
2676 a_tty__khist_shared(struct a_tty_line *tlp, struct a_tty_hist *thp){
2677 ui32_t f, rv;
2678 NYD2_ENTER;
2680 if(n_LIKELY((tlp->tl_hist = thp) != NULL)){
2681 char *cp;
2682 size_t i;
2684 i = thp->th_len;
2685 if(tlp->tl_goinflags & n_GO_INPUT_CTX_COMPOSE){
2686 ++i;
2687 if(!(thp->th_flags & a_TTY_HIST_CTX_COMPOSE))
2688 ++i;
2690 tlp->tl_defc.s = cp = n_autorec_alloc(i +1);
2691 if(tlp->tl_goinflags & n_GO_INPUT_CTX_COMPOSE){
2692 if((*cp = ok_vlook(escape)[0]) == '\0')
2693 *cp = n_ESCAPE[0];
2694 ++cp;
2695 if(!(thp->th_flags & a_TTY_HIST_CTX_COMPOSE))
2696 *cp++ = ':';
2698 memcpy(cp, thp->th_dat, thp->th_len +1);
2699 rv = tlp->tl_defc.l = i;
2701 f = (tlp->tl_count > 0) ? a_TTY_VF_MOD_DIRTY : a_TTY_VF_NONE;
2702 tlp->tl_count = tlp->tl_cursor = 0;
2703 }else{
2704 f = a_TTY_VF_BELL;
2705 rv = UI32_MAX;
2708 tlp->tl_vi_flags |= f;
2709 NYD2_LEAVE;
2710 return rv;
2713 static ui32_t
2714 a_tty_khist(struct a_tty_line *tlp, bool_t fwd){
2715 struct a_tty_hist *thp;
2716 ui32_t rv;
2717 NYD2_ENTER;
2719 /* If we're not in history mode yet, save line content */
2720 if((thp = tlp->tl_hist) == NULL){
2721 a_tty_cell2save(tlp);
2722 if((thp = a_tty.tg_hist) == NULL)
2723 goto jleave;
2724 if(fwd)
2725 while(thp->th_older != NULL)
2726 thp = thp->th_older;
2727 goto jleave;
2730 while((thp = fwd ? thp->th_younger : thp->th_older) != NULL){
2731 /* Applicable to input context? Compose mode swallows anything */
2732 if((tlp->tl_goinflags & n__GO_INPUT_CTX_MASK) == n_GO_INPUT_CTX_COMPOSE)
2733 break;
2734 if((thp->th_flags & a_TTY_HIST_CTX_MASK) != a_TTY_HIST_CTX_COMPOSE)
2735 break;
2737 jleave:
2738 rv = a_tty__khist_shared(tlp, thp);
2739 NYD2_LEAVE;
2740 return rv;
2743 static ui32_t
2744 a_tty_khist_search(struct a_tty_line *tlp, bool_t fwd){
2745 struct str orig_savec;
2746 ui32_t xoff, rv;
2747 struct a_tty_hist *thp;
2748 NYD2_ENTER;
2750 thp = NULL;
2752 /* We cannot complete an empty line */
2753 if(n_UNLIKELY(tlp->tl_count == 0)){
2754 /* XXX The upcoming hard reset would restore a set savec buffer,
2755 * XXX so forcefully reset that. A cleaner solution would be to
2756 * XXX reset it whenever a restore is no longer desired */
2757 tlp->tl_savec.s = NULL;
2758 tlp->tl_savec.l = 0;
2759 goto jleave;
2762 /* xxx It is a bit of a hack, but let's just hard-code the knowledge that
2763 * xxx in compose mode the first character is *escape* and must be skipped
2764 * xxx when searching the history. Alternatively we would need to have
2765 * xxx context-specific history search hooks which would do the search,
2766 * xxx which is overkill for our sole special case compose mode */
2767 if((tlp->tl_goinflags & n__GO_INPUT_CTX_MASK) == n_GO_INPUT_CTX_COMPOSE)
2768 xoff = 1;
2769 else
2770 xoff = 0;
2772 if((thp = tlp->tl_hist) == NULL){
2773 a_tty_cell2save(tlp);
2774 if((thp = a_tty.tg_hist) == NULL) /* TODO Should end "doing nothing"! */
2775 goto jleave;
2776 if(fwd)
2777 while(thp->th_older != NULL)
2778 thp = thp->th_older;
2779 orig_savec.s = NULL;
2780 orig_savec.l = 0; /* silence CC */
2781 }else{
2782 while((thp = fwd ? thp->th_younger : thp->th_older) != NULL){
2783 /* Applicable to input context? Compose mode swallows anything */
2784 if(xoff != 0)
2785 break;
2786 if((thp->th_flags & a_TTY_HIST_CTX_MASK) != a_TTY_HIST_CTX_COMPOSE)
2787 break;
2789 if(thp == NULL)
2790 goto jleave;
2792 orig_savec = tlp->tl_savec;
2795 if(xoff >= tlp->tl_savec.l){
2796 thp = NULL;
2797 goto jleave;
2800 if(orig_savec.s == NULL)
2801 a_tty_cell2save(tlp);
2803 for(; thp != NULL; thp = fwd ? thp->th_younger : thp->th_older){
2804 /* Applicable to input context? Compose mode swallows anything */
2805 if(xoff != 1 && (thp->th_flags & a_TTY_HIST_CTX_MASK) ==
2806 a_TTY_HIST_CTX_COMPOSE)
2807 continue;
2808 if(is_prefix(&tlp->tl_savec.s[xoff], thp->th_dat))
2809 break;
2812 if(orig_savec.s != NULL)
2813 tlp->tl_savec = orig_savec;
2814 jleave:
2815 rv = a_tty__khist_shared(tlp, thp);
2816 NYD2_LEAVE;
2817 return rv;
2819 # endif /* HAVE_HISTORY */
2821 static enum a_tty_fun_status
2822 a_tty_fun(struct a_tty_line *tlp, enum a_tty_bind_flags tbf, size_t *len){
2823 enum a_tty_fun_status rv;
2824 NYD2_ENTER;
2826 rv = a_TTY_FUN_STATUS_OK;
2827 # undef a_X
2828 # define a_X(N) a_TTY_BIND_FUN_REDUCE(a_TTY_BIND_FUN_ ## N)
2829 switch(a_TTY_BIND_FUN_REDUCE(tbf)){
2830 case a_X(BELL):
2831 tlp->tl_vi_flags |= a_TTY_VF_BELL;
2832 break;
2833 case a_X(GO_BWD):
2834 a_tty_kleft(tlp);
2835 break;
2836 case a_X(GO_FWD):
2837 a_tty_kright(tlp);
2838 break;
2839 case a_X(GO_WORD_BWD):
2840 a_tty_kgow(tlp, -1);
2841 break;
2842 case a_X(GO_WORD_FWD):
2843 a_tty_kgow(tlp, +1);
2844 break;
2845 case a_X(GO_SCREEN_BWD):
2846 a_tty_kgoscr(tlp, -1);
2847 break;
2848 case a_X(GO_SCREEN_FWD):
2849 a_tty_kgoscr(tlp, +1);
2850 break;
2851 case a_X(GO_HOME):
2852 a_tty_khome(tlp, TRU1);
2853 break;
2854 case a_X(GO_END):
2855 a_tty_kend(tlp);
2856 break;
2857 case a_X(DEL_BWD):
2858 a_tty_kbs(tlp);
2859 break;
2860 case a_X(DEL_FWD):
2861 if(a_tty_kdel(tlp) < 0)
2862 rv = a_TTY_FUN_STATUS_END;
2863 break;
2864 case a_X(SNARF_WORD_BWD):
2865 a_tty_ksnarfw(tlp, FAL0);
2866 break;
2867 case a_X(SNARF_WORD_FWD):
2868 a_tty_ksnarfw(tlp, TRU1);
2869 break;
2870 case a_X(SNARF_END):
2871 a_tty_ksnarf(tlp, FAL0, TRU1);
2872 break;
2873 case a_X(SNARF_LINE):
2874 a_tty_ksnarf(tlp, TRU1, (tlp->tl_count == 0));
2875 break;
2877 case a_X(HIST_FWD):{
2878 # ifdef HAVE_HISTORY
2879 bool_t isfwd = TRU1;
2881 if(0){
2882 # endif
2883 /* FALLTHRU */
2884 case a_X(HIST_BWD):
2885 # ifdef HAVE_HISTORY
2886 isfwd = FAL0;
2888 if((*len = a_tty_khist(tlp, isfwd)) != UI32_MAX){
2889 rv = a_TTY_FUN_STATUS_RESTART;
2890 break;
2892 goto jreset;
2893 # endif
2895 tlp->tl_vi_flags |= a_TTY_VF_BELL;
2896 break;
2898 case a_X(HIST_SRCH_FWD):{
2899 # ifdef HAVE_HISTORY
2900 bool_t isfwd = TRU1;
2902 if(0){
2903 # endif
2904 /* FALLTHRU */
2905 case a_X(HIST_SRCH_BWD):
2906 # ifdef HAVE_HISTORY
2907 isfwd = FAL0;
2909 if((*len = a_tty_khist_search(tlp, isfwd)) != UI32_MAX){
2910 rv = a_TTY_FUN_STATUS_RESTART;
2911 break;
2913 goto jreset;
2914 # else
2915 tlp->tl_vi_flags |= a_TTY_VF_BELL;
2916 # endif
2917 }break;
2919 case a_X(REPAINT):
2920 tlp->tl_vi_flags |= a_TTY_VF_MOD_DIRTY;
2921 break;
2922 case a_X(QUOTE_RNDTRIP):
2923 tlp->tl_quote_rndtrip = !tlp->tl_quote_rndtrip;
2924 break;
2925 case a_X(PROMPT_CHAR):{
2926 wchar_t wc;
2928 if((wc = a_tty_vinuni(tlp)) > 0)
2929 a_tty_kother(tlp, wc);
2930 }break;
2931 case a_X(COMPLETE):
2932 if((*len = a_tty_kht(tlp)) > 0)
2933 rv = a_TTY_FUN_STATUS_RESTART;
2934 break;
2936 case a_X(PASTE):
2937 if(tlp->tl_pastebuf.l > 0)
2938 *len = (tlp->tl_defc = tlp->tl_pastebuf).l;
2939 else
2940 tlp->tl_vi_flags |= a_TTY_VF_BELL;
2941 break;
2943 case a_X(CLEAR_SCREEN):
2944 tlp->tl_vi_flags |= (n_termcap_cmdx(n_TERMCAP_CMD_cl) == TRU1)
2945 ? a_TTY_VF_MOD_DIRTY : a_TTY_VF_BELL;
2946 break;
2948 case a_X(CANCEL):
2949 /* Normally this just causes a restart and thus resets the state
2950 * machine */
2951 if(tlp->tl_savec.l == 0 && tlp->tl_defc.l == 0){
2953 # ifdef HAVE_KEY_BINDINGS
2954 tlp->tl_bind_takeover = '\0';
2955 # endif
2956 tlp->tl_vi_flags |= a_TTY_VF_BELL;
2957 rv = a_TTY_FUN_STATUS_RESTART;
2958 break;
2960 case a_X(RESET):
2961 if(tlp->tl_count == 0 && tlp->tl_savec.l == 0 && tlp->tl_defc.l == 0){
2962 # ifdef HAVE_KEY_BINDINGS
2963 tlp->tl_bind_takeover = '\0';
2964 # endif
2965 tlp->tl_vi_flags |= a_TTY_VF_MOD_DIRTY | a_TTY_VF_BELL;
2966 break;
2967 }else if(0){
2968 case a_X(FULLRESET):
2969 tlp->tl_savec.s = tlp->tl_defc.s = NULL;
2970 tlp->tl_savec.l = tlp->tl_defc.l = 0;
2971 tlp->tl_defc_cursor_byte = 0;
2972 tlp->tl_vi_flags |= a_TTY_VF_BELL;
2974 jreset:
2975 # ifdef HAVE_KEY_BINDINGS
2976 tlp->tl_bind_takeover = '\0';
2977 # endif
2978 tlp->tl_vi_flags |= a_TTY_VF_MOD_DIRTY;
2979 tlp->tl_cursor = tlp->tl_count = 0;
2980 # ifdef HAVE_HISTORY
2981 tlp->tl_hist = NULL;
2982 # endif
2983 if((*len = tlp->tl_savec.l) != 0){
2984 tlp->tl_defc = tlp->tl_savec;
2985 tlp->tl_savec.s = NULL;
2986 tlp->tl_savec.l = 0;
2987 }else
2988 *len = tlp->tl_defc.l;
2989 rv = a_TTY_FUN_STATUS_RESTART;
2990 break;
2992 default:
2993 case a_X(COMMIT):
2994 rv = a_TTY_FUN_STATUS_COMMIT;
2995 break;
2997 # undef a_X
2999 NYD2_LEAVE;
3000 return rv;
3003 static ssize_t
3004 a_tty_readline(struct a_tty_line *tlp, size_t len, bool_t *histok_or_null
3005 n_MEMORY_DEBUG_ARGS){
3006 /* We want to save code, yet we may have to incorporate a lines'
3007 * default content and / or default input to switch back to after some
3008 * history movement; let "len > 0" mean "have to display some data
3009 * buffer" -> a_BUFMODE, and only otherwise read(2) it */
3010 mbstate_t ps[2];
3011 char cbuf_base[MB_LEN_MAX * 2], *cbuf, *cbufp;
3012 ssize_t rv;
3013 struct a_tty_bind_tree *tbtp;
3014 wchar_t wc;
3015 enum a_tty_bind_flags tbf;
3016 enum {a_NONE, a_WAS_HERE = 1<<0, a_BUFMODE = 1<<1, a_MAYBEFUN = 1<<2,
3017 a_TIMEOUT = 1<<3, a_TIMEOUT_EXPIRED = 1<<4,
3018 a_TIMEOUT_MASK = a_TIMEOUT | a_TIMEOUT_EXPIRED,
3019 a_READ_LOOP_MASK = ~(a_WAS_HERE | a_MAYBEFUN | a_TIMEOUT_MASK)
3020 } flags;
3021 NYD_ENTER;
3023 n_UNINIT(rv, 0);
3024 # ifdef HAVE_KEY_BINDINGS
3025 assert(tlp->tl_bind_takeover == '\0');
3026 # endif
3027 jrestart:
3028 memset(ps, 0, sizeof ps);
3029 flags = a_NONE;
3030 tlp->tl_vi_flags |= a_TTY_VF_REFRESH | a_TTY_VF_SYNC;
3032 jinput_loop:
3033 for(;;){
3034 if(len != 0)
3035 flags |= a_BUFMODE;
3037 /* Ensure we have valid pointers, and room for grow */
3038 a_tty_check_grow(tlp, ((flags & a_BUFMODE) ? (ui32_t)len : 1)
3039 n_MEMORY_DEBUG_ARGSCALL);
3041 /* Handle visual state flags, except in buffer mode */
3042 if(!(flags & a_BUFMODE) && (tlp->tl_vi_flags & a_TTY_VF_ALL_MASK))
3043 if(!a_tty_vi_refresh(tlp)){
3044 rv = -1;
3045 goto jleave;
3048 /* Ready for messing around.
3049 * Normal read(2)? Else buffer mode: speed this one up */
3050 if(!(flags & a_BUFMODE)){
3051 cbufp =
3052 cbuf = cbuf_base;
3053 }else{
3054 assert(tlp->tl_defc.l > 0 && tlp->tl_defc.s != NULL);
3055 assert(tlp->tl_defc.l >= len);
3056 cbufp =
3057 cbuf = tlp->tl_defc.s + (tlp->tl_defc.l - len);
3058 cbufp += len;
3061 /* Read in the next complete multibyte character */
3062 /* C99 */{
3063 # ifdef HAVE_KEY_BINDINGS
3064 struct a_tty_bind_tree *xtbtp;
3065 struct inseq{
3066 struct inseq *last;
3067 struct inseq *next;
3068 struct a_tty_bind_tree *tbtp;
3069 } *isp_head, *isp;
3071 isp_head = isp = NULL;
3072 # endif
3074 for(flags &= a_READ_LOOP_MASK;;){
3075 # ifdef HAVE_KEY_BINDINGS
3076 if(!(flags & a_BUFMODE) && tlp->tl_bind_takeover != '\0'){
3077 wc = tlp->tl_bind_takeover;
3078 tlp->tl_bind_takeover = '\0';
3079 }else
3080 # endif
3082 if(!(flags & a_BUFMODE)){
3083 /* Let me at least once dream of iomon(itor), timer with
3084 * one-shot, enwrapped with key_event and key_sequence_event,
3085 * all driven by an event_loop */
3086 /* TODO v15 Until we have SysV signal handling all through we
3087 * TODO need to temporarily adjust our BSD signal handler with
3088 * TODO a SysV one, here */
3089 n_sighdl_t otstp, ottin, ottou;
3091 otstp = n_signal(SIGTSTP, &a_tty_signal);
3092 ottin = n_signal(SIGTTIN, &a_tty_signal);
3093 ottou = n_signal(SIGTTOU, &a_tty_signal);
3094 # ifdef HAVE_KEY_BINDINGS
3095 flags &= ~a_TIMEOUT_MASK;
3096 if(isp != NULL && (tbtp = isp->tbtp)->tbt_isseq &&
3097 !tbtp->tbt_isseq_trail){
3098 a_tty_term_rawmode_timeout(tlp, TRU1);
3099 flags |= a_TIMEOUT;
3101 # endif
3103 while((rv = read(STDIN_FILENO, cbufp, 1)) < 1){
3104 if(rv == -1){
3105 if(n_err_no == n_ERR_INTR){
3106 if((tlp->tl_vi_flags & a_TTY_VF_MOD_DIRTY) &&
3107 !a_tty_vi_refresh(tlp))
3108 break;
3109 continue;
3111 break;
3114 # ifdef HAVE_KEY_BINDINGS
3115 /* Timeout expiration */
3116 if(rv == 0){
3117 assert(flags & a_TIMEOUT);
3118 assert(isp != NULL);
3119 a_tty_term_rawmode_timeout(tlp, FAL0);
3121 /* Something "atomic" broke. Maybe the current one can
3122 * also be terminated already, by itself? xxx really? */
3123 if((tbtp = isp->tbtp)->tbt_bind != NULL){
3124 tlp->tl_bind_takeover = wc;
3125 goto jhave_bind;
3128 /* Or, maybe there is a second path without a timeout;
3129 * this should be covered by .tbt_isseq_trail, but then
3130 * again a single-layer implementation cannot "know" */
3131 for(xtbtp = tbtp; (xtbtp = xtbtp->tbt_sibling) != NULL;)
3132 if(xtbtp->tbt_char == tbtp->tbt_char){
3133 assert(!xtbtp->tbt_isseq);
3134 break;
3136 /* Lay down on read(2)? */
3137 if(xtbtp != NULL)
3138 continue;
3139 goto jtake_over;
3141 # endif /* HAVE_KEY_BINDINGS */
3144 # ifdef HAVE_KEY_BINDINGS
3145 if(flags & a_TIMEOUT)
3146 a_tty_term_rawmode_timeout(tlp, FAL0);
3147 # endif
3148 safe_signal(SIGTSTP, otstp);
3149 safe_signal(SIGTTIN, ottin);
3150 safe_signal(SIGTTOU, ottou);
3151 if(rv < 0)
3152 goto jleave;
3154 /* As a special case, simulate EOF via EOT (which can happen
3155 * via type-ahead as when typing "yes\n^@" during sleep of
3156 * $ sleep 5; mail -s byjove $LOGNAME */
3157 if(*cbufp == '\0'){
3158 assert((n_psonce & n_PSO_INTERACTIVE) &&
3159 !(n_pstate & n_PS_ROBOT));
3160 *cbuf = '\x04';
3162 ++cbufp;
3165 rv = (ssize_t)mbrtowc(&wc, cbuf, PTR2SIZE(cbufp - cbuf), &ps[0]);
3166 if(rv <= 0){
3167 /* Any error during buffer mode can only result in a hard
3168 * reset; Otherwise, if it's a hard error, or if too many
3169 * redundant shift sequences overflow our buffer: perform
3170 * hard reset */
3171 if((flags & a_BUFMODE) || rv == -1 ||
3172 sizeof cbuf_base == PTR2SIZE(cbufp - cbuf)){
3173 a_tty_fun(tlp, a_TTY_BIND_FUN_FULLRESET, &len);
3174 goto jrestart;
3176 /* Otherwise, due to the way we deal with the buffer, we need
3177 * to restore the mbstate_t from before this conversion */
3178 ps[0] = ps[1];
3179 continue;
3181 cbufp = cbuf;
3182 ps[1] = ps[0];
3185 /* Normal read(2)ing is subject to detection of key-bindings */
3186 # ifdef HAVE_KEY_BINDINGS
3187 if(!(flags & a_BUFMODE)){
3188 /* Check for special bypass functions before we try to embed
3189 * this character into the tree */
3190 if(n_uasciichar(wc)){
3191 char c;
3192 char const *cp;
3194 for(c = (char)wc, cp = &(*tlp->tl_bind_shcut_prompt_char)[0];
3195 *cp != '\0'; ++cp){
3196 if(c == *cp){
3197 wc = a_tty_vinuni(tlp);
3198 break;
3201 if(wc == '\0'){
3202 tlp->tl_vi_flags |= a_TTY_VF_BELL;
3203 goto jinput_loop;
3206 if(n_uasciichar(wc))
3207 flags |= a_MAYBEFUN;
3208 else
3209 flags &= ~a_MAYBEFUN;
3211 /* Search for this character in the bind tree */
3212 tbtp = (isp != NULL) ? isp->tbtp->tbt_childs
3213 : (*tlp->tl_bind_tree_hmap)[wc % HSHSIZE];
3214 for(; tbtp != NULL; tbtp = tbtp->tbt_sibling){
3215 if(tbtp->tbt_char == wc){
3216 struct inseq *nisp;
3218 /* If this one cannot continue we're likely finished! */
3219 if(tbtp->tbt_childs == NULL){
3220 assert(tbtp->tbt_bind != NULL);
3221 tbf = tbtp->tbt_bind->tbc_flags;
3222 goto jmle_fun;
3225 /* This needs to read more characters */
3226 nisp = n_autorec_alloc(sizeof *nisp);
3227 if((nisp->last = isp) == NULL)
3228 isp_head = nisp;
3229 else
3230 isp->next = nisp;
3231 nisp->next = NULL;
3232 nisp->tbtp = tbtp;
3233 isp = nisp;
3234 flags &= ~a_WAS_HERE;
3235 break;
3238 if(tbtp != NULL)
3239 continue;
3241 /* Was there a binding active, but couldn't be continued? */
3242 if(isp != NULL){
3243 /* A binding had a timeout, it didn't expire, but we saw
3244 * something non-expected. Something "atomic" broke.
3245 * Maybe there is a second path without a timeout, that
3246 * continues like we've seen it. I.e., it may just have been
3247 * the user, typing too fast. We definitely want to allow
3248 * bindings like \e,d etc. to succeed: users are so used to
3249 * them that a timeout cannot be the mechanism to catch up!
3250 * A single-layer implementation cannot "know" */
3251 if((tbtp = isp->tbtp)->tbt_isseq && (isp->last == NULL ||
3252 !(xtbtp = isp->last->tbtp)->tbt_isseq ||
3253 xtbtp->tbt_isseq_trail)){
3254 for(xtbtp = (tbtp = isp->tbtp);
3255 (xtbtp = xtbtp->tbt_sibling) != NULL;)
3256 if(xtbtp->tbt_char == tbtp->tbt_char){
3257 assert(!xtbtp->tbt_isseq);
3258 break;
3260 if(xtbtp != NULL){
3261 isp->tbtp = xtbtp;
3262 tlp->tl_bind_takeover = wc;
3263 continue;
3267 /* Check for CANCEL shortcut now */
3268 if(flags & a_MAYBEFUN){
3269 char c;
3270 char const *cp;
3272 for(c = (char)wc, cp = &(*tlp->tl_bind_shcut_cancel)[0];
3273 *cp != '\0'; ++cp)
3274 if(c == *cp){
3275 tbf = a_TTY_BIND_FUN_INTERNAL |a_TTY_BIND_FUN_CANCEL;
3276 goto jmle_fun;
3280 /* So: maybe the current sequence can be terminated here? */
3281 if((tbtp = isp->tbtp)->tbt_bind != NULL){
3282 jhave_bind:
3283 tbf = tbtp->tbt_bind->tbc_flags;
3284 jmle_fun:
3285 if(tbf & a_TTY_BIND_FUN_INTERNAL){
3286 switch(a_tty_fun(tlp, tbf, &len)){
3287 case a_TTY_FUN_STATUS_OK:
3288 goto jinput_loop;
3289 case a_TTY_FUN_STATUS_COMMIT:
3290 goto jdone;
3291 case a_TTY_FUN_STATUS_RESTART:
3292 goto jrestart;
3293 case a_TTY_FUN_STATUS_END:
3294 rv = -1;
3295 goto jleave;
3297 assert(0);
3298 }else if(tbtp->tbt_bind->tbc_flags & a_TTY_BIND_NOCOMMIT){
3299 struct a_tty_bind_ctx *tbcp;
3301 tbcp = tbtp->tbt_bind;
3302 memcpy(tlp->tl_defc.s = n_autorec_alloc(
3303 (tlp->tl_defc.l = len = tbcp->tbc_exp_len) +1),
3304 tbcp->tbc_exp, tbcp->tbc_exp_len +1);
3305 goto jrestart;
3306 }else{
3307 cbufp = tbtp->tbt_bind->tbc_exp;
3308 goto jinject_input;
3313 /* Otherwise take over all chars "as is" */
3314 jtake_over:
3315 for(; isp_head != NULL; isp_head = isp_head->next)
3316 if(a_tty_kother(tlp, isp_head->tbtp->tbt_char)){
3317 /* FIXME */
3319 /* And the current one too */
3320 goto jkother;
3322 # endif /* HAVE_KEY_BINDINGS */
3324 if((flags & a_BUFMODE) && (len -= (size_t)rv) == 0){
3325 /* Buffer mode completed */
3326 tlp->tl_defc.s = NULL;
3327 tlp->tl_defc.l = 0;
3328 flags &= ~a_BUFMODE;
3330 break;
3333 # ifndef HAVE_KEY_BINDINGS
3334 /* Don't interpret control bytes during buffer mode.
3335 * Otherwise, if it's a control byte check whether it is a MLE
3336 * function. Remarks: initially a complete duplicate to be able to
3337 * switch(), later converted to simply iterate over (an #ifdef'd
3338 * subset of) the MLE base_tuple table in order to have "a SPOF" */
3339 if(cbuf == cbuf_base && n_uasciichar(wc) && cntrlchar((char)wc)){
3340 struct a_tty_bind_builtin_tuple const *tbbtp, *tbbtp_max;
3341 char c;
3343 c = (char)wc ^ 0x40;
3344 tbbtp = a_tty_bind_base_tuples;
3345 tbbtp_max = &tbbtp[n_NELEM(a_tty_bind_base_tuples)];
3346 jbuiltin_redo:
3347 for(; tbbtp < tbbtp_max; ++tbbtp){
3348 /* Assert default_tuple table is properly subset'ed */
3349 assert(tbbtp->tbdt_iskey);
3350 if(tbbtp->tbbt_ckey == c){
3351 if(tbbtp->tbbt_exp[0] == '\0'){
3352 tbf = a_TTY_BIND_FUN_EXPAND((ui8_t)tbbtp->tbbt_exp[1]);
3353 switch(a_tty_fun(tlp, tbf, &len)){
3354 case a_TTY_FUN_STATUS_OK:
3355 goto jinput_loop;
3356 case a_TTY_FUN_STATUS_COMMIT:
3357 goto jdone;
3358 case a_TTY_FUN_STATUS_RESTART:
3359 goto jrestart;
3360 case a_TTY_FUN_STATUS_END:
3361 rv = -1;
3362 goto jleave;
3364 assert(0);
3365 }else{
3366 cbufp = n_UNCONST(tbbtp->tbbt_exp);
3367 goto jinject_input;
3371 if(tbbtp ==
3372 &a_tty_bind_base_tuples[n_NELEM(a_tty_bind_base_tuples)]){
3373 tbbtp = a_tty_bind_default_tuples;
3374 tbbtp_max = &tbbtp[n_NELEM(a_tty_bind_default_tuples)];
3375 goto jbuiltin_redo;
3378 # endif /* !HAVE_KEY_BINDINGS */
3380 # ifdef HAVE_KEY_BINDINGS
3381 jkother:
3382 # endif
3383 if(a_tty_kother(tlp, wc)){
3384 /* Don't clear the history during buffer mode.. */
3385 # ifdef HAVE_HISTORY
3386 if(!(flags & a_BUFMODE) && cbuf == cbuf_base)
3387 tlp->tl_hist = NULL;
3388 # endif
3393 /* We have a completed input line, convert the struct cell data to its
3394 * plain character equivalent */
3395 jdone:
3396 rv = a_tty_cell2dat(tlp);
3397 jleave:
3398 putc('\n', n_tty_fp);
3399 fflush(n_tty_fp);
3400 NYD_LEAVE;
3401 return rv;
3403 jinject_input:{
3404 size_t i;
3406 hold_all_sigs(); /* XXX v15 drop */
3407 i = a_tty_cell2dat(tlp);
3408 n_go_input_inject(n_GO_INPUT_INJECT_NONE, tlp->tl_line.cbuf, i);
3409 i = strlen(cbufp) +1;
3410 if(i >= *tlp->tl_x_bufsize){
3411 *tlp->tl_x_buf = (n_realloc)(*tlp->tl_x_buf, i n_MEMORY_DEBUG_ARGSCALL);
3412 *tlp->tl_x_bufsize = i;
3414 memcpy(*tlp->tl_x_buf, cbufp, i);
3415 rele_all_sigs(); /* XXX v15 drop */
3416 if(histok_or_null != NULL)
3417 *histok_or_null = FAL0;
3418 rv = (ssize_t)--i;
3420 goto jleave;
3423 # ifdef HAVE_KEY_BINDINGS
3424 static enum n_go_input_flags
3425 a_tty_bind_ctx_find(char const *name){
3426 enum n_go_input_flags rv;
3427 struct a_tty_bind_ctx_map const *tbcmp;
3428 NYD2_ENTER;
3430 tbcmp = a_tty_bind_ctx_maps;
3431 do if(!asccasecmp(tbcmp->tbcm_name, name)){
3432 rv = tbcmp->tbcm_ctx;
3433 goto jleave;
3434 }while(PTRCMP(++tbcmp, <,
3435 &a_tty_bind_ctx_maps[n_NELEM(a_tty_bind_ctx_maps)]));
3437 rv = (enum n_go_input_flags)-1;
3438 jleave:
3439 NYD2_LEAVE;
3440 return rv;
3443 static bool_t
3444 a_tty_bind_create(struct a_tty_bind_parse_ctx *tbpcp, bool_t replace){
3445 struct a_tty_bind_ctx *tbcp;
3446 bool_t rv;
3447 NYD2_ENTER;
3449 rv = FAL0;
3451 if(!a_tty_bind_parse(TRU1, tbpcp))
3452 goto jleave;
3454 /* Since we use a single buffer for it all, need to replace as such */
3455 if(tbpcp->tbpc_tbcp != NULL){
3456 if(!replace)
3457 goto jleave;
3458 a_tty_bind_del(tbpcp);
3459 }else if(a_tty.tg_bind_cnt == UI32_MAX){
3460 n_err(_("`bind': maximum number of bindings already established\n"));
3461 goto jleave;
3464 /* C99 */{
3465 size_t i, j;
3467 tbcp = n_alloc(n_VSTRUCT_SIZEOF(struct a_tty_bind_ctx, tbc__buf) +
3468 tbpcp->tbpc_seq_len + tbpcp->tbpc_exp.l +
3469 n_MAX(sizeof(si32_t), sizeof(wc_t)) + tbpcp->tbpc_cnv_len +3);
3470 if(tbpcp->tbpc_ltbcp != NULL){
3471 tbcp->tbc_next = tbpcp->tbpc_ltbcp->tbc_next;
3472 tbpcp->tbpc_ltbcp->tbc_next = tbcp;
3473 }else{
3474 enum n_go_input_flags gif;
3476 gif = tbpcp->tbpc_flags & n__GO_INPUT_CTX_MASK;
3477 tbcp->tbc_next = a_tty.tg_bind[gif];
3478 a_tty.tg_bind[gif] = tbcp;
3480 memcpy(tbcp->tbc_seq = &tbcp->tbc__buf[0],
3481 tbpcp->tbpc_seq, i = (tbcp->tbc_seq_len = tbpcp->tbpc_seq_len) +1);
3482 memcpy(tbcp->tbc_exp = &tbcp->tbc__buf[i],
3483 tbpcp->tbpc_exp.s, j = (tbcp->tbc_exp_len = tbpcp->tbpc_exp.l) +1);
3484 i += j;
3485 i = (i + tbpcp->tbpc_cnv_align_mask) & ~tbpcp->tbpc_cnv_align_mask;
3486 memcpy(tbcp->tbc_cnv = &tbcp->tbc__buf[i],
3487 tbpcp->tbpc_cnv, (tbcp->tbc_cnv_len = tbpcp->tbpc_cnv_len) +1);
3488 tbcp->tbc_flags = tbpcp->tbpc_flags;
3491 /* Directly resolve any termcap(5) symbol if we are already setup */
3492 if((n_psonce & n_PSO_STARTED) &&
3493 (tbcp->tbc_flags & (a_TTY_BIND_RESOLVE | a_TTY_BIND_DEFUNCT)) ==
3494 a_TTY_BIND_RESOLVE)
3495 a_tty_bind_resolve(tbcp);
3497 ++a_tty.tg_bind_cnt;
3498 /* If this binding is usable invalidate the key input lookup trees */
3499 if(!(tbcp->tbc_flags & a_TTY_BIND_DEFUNCT))
3500 a_tty.tg_bind_isdirty = TRU1;
3501 rv = TRU1;
3502 jleave:
3503 NYD2_LEAVE;
3504 return rv;
3507 static bool_t
3508 a_tty_bind_parse(bool_t isbindcmd, struct a_tty_bind_parse_ctx *tbpcp){
3509 enum{a_TRUE_RV = a_TTY__BIND_LAST<<1};
3511 struct n_visual_info_ctx vic;
3512 struct str shin_save, shin;
3513 struct n_string shou, *shoup;
3514 size_t i;
3515 struct kse{
3516 struct kse *next;
3517 char *seq_dat;
3518 wc_t *cnv_dat;
3519 ui32_t seq_len;
3520 ui32_t cnv_len; /* High bit set if a termap to be resolved */
3521 ui32_t calc_cnv_len; /* Ditto, but aligned etc. */
3522 ui8_t kse__dummy[4];
3523 } *head, *tail;
3524 ui32_t f;
3525 NYD2_ENTER;
3526 n_LCTA(UICMP(64, a_TRUE_RV, <, UI32_MAX),
3527 "Flag bits excess storage datatype");
3529 f = n_GO_INPUT_NONE;
3530 shoup = n_string_creat_auto(&shou);
3531 head = tail = NULL;
3533 /* Parse the key-sequence */
3534 for(shin.s = n_UNCONST(tbpcp->tbpc_in_seq), shin.l = UIZ_MAX;;){
3535 struct kse *ep;
3536 enum n_shexp_state shs;
3538 shin_save = shin;
3539 shs = n_shexp_parse_token((n_SHEXP_PARSE_TRUNC |
3540 n_SHEXP_PARSE_TRIM_SPACE | n_SHEXP_PARSE_IGNORE_EMPTY |
3541 n_SHEXP_PARSE_IFS_IS_COMMA), shoup, &shin, NULL);
3542 if(shs & n_SHEXP_STATE_ERR_UNICODE){
3543 f |= a_TTY_BIND_DEFUNCT;
3544 if(isbindcmd && (n_poption & n_PO_D_V))
3545 n_err(_("`%s': \\uNICODE not available in locale: %s\n"),
3546 tbpcp->tbpc_cmd, tbpcp->tbpc_in_seq);
3548 if((shs & n_SHEXP_STATE_ERR_MASK) & ~n_SHEXP_STATE_ERR_UNICODE){
3549 n_err(_("`%s': failed to parse key-sequence: %s\n"),
3550 tbpcp->tbpc_cmd, tbpcp->tbpc_in_seq);
3551 goto jleave;
3553 if((shs & (n_SHEXP_STATE_OUTPUT | n_SHEXP_STATE_STOP)) ==
3554 n_SHEXP_STATE_STOP)
3555 break;
3557 ep = n_autorec_alloc(sizeof *ep);
3558 if(head == NULL)
3559 head = ep;
3560 else
3561 tail->next = ep;
3562 tail = ep;
3563 ep->next = NULL;
3564 if(!(shs & n_SHEXP_STATE_ERR_UNICODE)){
3565 i = strlen(ep->seq_dat = n_shexp_quote_cp(n_string_cp(shoup), TRU1));
3566 if(i >= SI32_MAX - 1)
3567 goto jelen;
3568 ep->seq_len = (ui32_t)i;
3569 }else{
3570 /* Otherwise use the original buffer, _we_ can only quote it the wrong
3571 * way (e.g., an initial $'\u3a' becomes '\u3a'), _then_ */
3572 if((i = shin_save.l - shin.l) >= SI32_MAX - 1)
3573 goto jelen;
3574 ep->seq_len = (ui32_t)i;
3575 ep->seq_dat = savestrbuf(shin_save.s, i);
3578 memset(&vic, 0, sizeof vic);
3579 vic.vic_inlen = shoup->s_len;
3580 vic.vic_indat = shoup->s_dat;
3581 if(!n_visual_info(&vic,
3582 n_VISUAL_INFO_WOUT_CREATE | n_VISUAL_INFO_WOUT_SALLOC)){
3583 n_err(_("`%s': key-sequence seems to contain invalid "
3584 "characters: %s: %s\n"),
3585 tbpcp->tbpc_cmd, n_string_cp(shoup), tbpcp->tbpc_in_seq);
3586 f |= a_TTY_BIND_DEFUNCT;
3587 goto jleave;
3588 }else if(vic.vic_woulen == 0 ||
3589 vic.vic_woulen >= (SI32_MAX - 2) / sizeof(wc_t)){
3590 jelen:
3591 n_err(_("`%s': length of key-sequence unsupported: %s: %s\n"),
3592 tbpcp->tbpc_cmd, n_string_cp(shoup), tbpcp->tbpc_in_seq);
3593 f |= a_TTY_BIND_DEFUNCT;
3594 goto jleave;
3596 ep->cnv_dat = vic.vic_woudat;
3597 ep->cnv_len = (ui32_t)vic.vic_woulen;
3599 /* A termcap(5)/terminfo(5) identifier? */
3600 if(ep->cnv_len > 1 && ep->cnv_dat[0] == ':'){
3601 i = --ep->cnv_len, ++ep->cnv_dat;
3602 # if 0 /* ndef HAVE_TERMCAP xxx User can, via *termcap*! */
3603 if(n_poption & n_PO_D_V)
3604 n_err(_("`%s': no termcap(5)/terminfo(5) support: %s: %s\n"),
3605 tbpcp->tbpc_cmd, ep->seq_dat, tbpcp->tbpc_in_seq);
3606 f |= a_TTY_BIND_DEFUNCT;
3607 # endif
3608 if(i > a_TTY_BIND_CAPNAME_MAX){
3609 n_err(_("`%s': termcap(5)/terminfo(5) name too long: %s: %s\n"),
3610 tbpcp->tbpc_cmd, ep->seq_dat, tbpcp->tbpc_in_seq);
3611 f |= a_TTY_BIND_DEFUNCT;
3613 while(i > 0)
3614 /* (We store it as char[]) */
3615 if((ui32_t)ep->cnv_dat[--i] & ~0x7Fu){
3616 n_err(_("`%s': invalid termcap(5)/terminfo(5) name content: "
3617 "%s: %s\n"),
3618 tbpcp->tbpc_cmd, ep->seq_dat, tbpcp->tbpc_in_seq);
3619 f |= a_TTY_BIND_DEFUNCT;
3620 break;
3622 ep->cnv_len |= SI32_MIN; /* Needs resolve */
3623 f |= a_TTY_BIND_RESOLVE;
3626 if(shs & n_SHEXP_STATE_STOP)
3627 break;
3630 if(head == NULL){
3631 jeempty:
3632 n_err(_("`%s': effectively empty key-sequence: %s\n"),
3633 tbpcp->tbpc_cmd, tbpcp->tbpc_in_seq);
3634 goto jleave;
3637 if(isbindcmd) /* (Or always, just "1st time init") */
3638 tbpcp->tbpc_cnv_align_mask = n_MAX(sizeof(si32_t), sizeof(wc_t)) - 1;
3640 /* C99 */{
3641 struct a_tty_bind_ctx *ltbcp, *tbcp;
3642 char *cpbase, *cp, *cnv;
3643 size_t sl, cl;
3645 /* Unite the parsed sequence(s) into single string representations */
3646 for(sl = cl = 0, tail = head; tail != NULL; tail = tail->next){
3647 sl += tail->seq_len + 1;
3649 if(!isbindcmd)
3650 continue;
3652 /* Preserve room for terminal capabilities to be resolved.
3653 * Above we have ensured the buffer will fit in these calculations */
3654 if((i = tail->cnv_len) & SI32_MIN){
3655 /* For now
3656 * struct{si32_t buf_len_iscap; si32_t cap_len; wc_t name[]+NUL;}
3657 * later
3658 * struct{si32_t buf_len_iscap; si32_t cap_len; char buf[]+NUL;} */
3659 n_LCTAV(n_ISPOW2(a_TTY_BIND_CAPEXP_ROUNDUP));
3660 n_LCTA(a_TTY_BIND_CAPEXP_ROUNDUP >= sizeof(wc_t),
3661 "Aligning on this constant does not properly align wc_t");
3662 i &= SI32_MAX;
3663 i *= sizeof(wc_t);
3664 i += sizeof(si32_t);
3665 if(i < a_TTY_BIND_CAPEXP_ROUNDUP)
3666 i = (i + (a_TTY_BIND_CAPEXP_ROUNDUP - 1)) &
3667 ~(a_TTY_BIND_CAPEXP_ROUNDUP - 1);
3668 }else
3669 /* struct{si32_t buf_len_iscap; wc_t buf[]+NUL;} */
3670 i *= sizeof(wc_t);
3671 i += sizeof(si32_t) + sizeof(wc_t); /* (buf_len_iscap, NUL) */
3672 cl += i;
3673 if(tail->cnv_len & SI32_MIN){
3674 tail->cnv_len &= SI32_MAX;
3675 i |= SI32_MIN;
3677 tail->calc_cnv_len = (ui32_t)i;
3679 --sl;
3681 tbpcp->tbpc_seq_len = sl;
3682 tbpcp->tbpc_cnv_len = cl;
3683 /* C99 */{
3684 size_t j;
3686 j = i = sl + 1; /* Room for comma separator */
3687 if(isbindcmd){
3688 i = (i + tbpcp->tbpc_cnv_align_mask) & ~tbpcp->tbpc_cnv_align_mask;
3689 j = i;
3690 i += cl;
3692 tbpcp->tbpc_seq = cp = cpbase = n_autorec_alloc(i);
3693 tbpcp->tbpc_cnv = cnv = &cpbase[j];
3696 for(tail = head; tail != NULL; tail = tail->next){
3697 memcpy(cp, tail->seq_dat, tail->seq_len);
3698 cp += tail->seq_len;
3699 *cp++ = ',';
3701 if(isbindcmd){
3702 char * const save_cnv = cnv;
3704 n_UNALIGN(si32_t*,cnv)[0] = (si32_t)(i = tail->calc_cnv_len);
3705 cnv += sizeof(si32_t);
3706 if(i & SI32_MIN){
3707 /* For now
3708 * struct{si32_t buf_len_iscap; si32_t cap_len; wc_t name[];}
3709 * later
3710 * struct{si32_t buf_len_iscap; si32_t cap_len; char buf[];} */
3711 n_UNALIGN(si32_t*,cnv)[0] = tail->cnv_len;
3712 cnv += sizeof(si32_t);
3714 i = tail->cnv_len * sizeof(wc_t);
3715 memcpy(cnv, tail->cnv_dat, i);
3716 cnv += i;
3717 *n_UNALIGN(wc_t*,cnv) = '\0';
3719 cnv = save_cnv + (tail->calc_cnv_len & SI32_MAX);
3722 *--cp = '\0';
3724 /* Search for a yet existing identical mapping */
3725 /* C99 */{
3726 enum n_go_input_flags gif;
3728 gif = tbpcp->tbpc_flags & n__GO_INPUT_CTX_MASK;
3730 for(ltbcp = NULL, tbcp = a_tty.tg_bind[gif]; tbcp != NULL;
3731 ltbcp = tbcp, tbcp = tbcp->tbc_next)
3732 if(tbcp->tbc_seq_len == sl && !memcmp(tbcp->tbc_seq, cpbase, sl)){
3733 tbpcp->tbpc_tbcp = tbcp;
3734 break;
3737 tbpcp->tbpc_ltbcp = ltbcp;
3738 tbpcp->tbpc_flags |= (f & a_TTY__BIND_MASK);
3741 /* Create single string expansion if so desired */
3742 if(isbindcmd){
3743 char *exp;
3745 exp = tbpcp->tbpc_exp.s;
3747 i = tbpcp->tbpc_exp.l;
3748 if(i > 0 && exp[i - 1] == '@'){
3749 while(--i > 0){
3750 if(!blankspacechar(exp[i - 1]))
3751 break;
3753 if(i == 0)
3754 goto jeempty;
3756 exp[tbpcp->tbpc_exp.l = i] = '\0';
3757 tbpcp->tbpc_flags |= a_TTY_BIND_NOCOMMIT;
3760 /* Reverse solidus cannot be placed last in expansion to avoid (at the
3761 * time of this writing) possible problems with newline escaping.
3762 * Don't care about (un)even number thereof */
3763 if(i > 0 && exp[i - 1] == '\\'){
3764 n_err(_("`%s': reverse solidus cannot be last in expansion: %s\n"),
3765 tbpcp->tbpc_cmd, tbpcp->tbpc_in_seq);
3766 goto jleave;
3769 /* It may map to an internal MLE command! */
3770 for(i = 0; i < n_NELEM(a_tty_bind_fun_names); ++i)
3771 if(!asccasecmp(exp, a_tty_bind_fun_names[i])){
3772 tbpcp->tbpc_flags |= a_TTY_BIND_FUN_EXPAND(i) |
3773 a_TTY_BIND_FUN_INTERNAL |
3774 (head->next == NULL ? a_TTY_BIND_MLE1CNTRL : 0);
3775 if((n_poption & n_PO_D_V) &&
3776 (tbpcp->tbpc_flags & a_TTY_BIND_NOCOMMIT))
3777 n_err(_("`%s': MLE commands can't be made editable via @: %s\n"),
3778 tbpcp->tbpc_cmd, exp);
3779 tbpcp->tbpc_flags &= ~a_TTY_BIND_NOCOMMIT;
3780 break;
3784 f |= a_TRUE_RV; /* TODO because we only now true and false; DEFUNCT.. */
3785 jleave:
3786 n_string_gut(shoup);
3787 NYD2_LEAVE;
3788 return (f & a_TRUE_RV) != 0;
3791 static void
3792 a_tty_bind_resolve(struct a_tty_bind_ctx *tbcp){
3793 char capname[a_TTY_BIND_CAPNAME_MAX +1];
3794 struct n_termcap_value tv;
3795 size_t len;
3796 bool_t isfirst; /* TODO For now: first char must be control! */
3797 char *cp, *next;
3798 NYD2_ENTER;
3800 n_UNINIT(next, NULL);
3801 for(cp = tbcp->tbc_cnv, isfirst = TRU1, len = tbcp->tbc_cnv_len;
3802 len > 0; isfirst = FAL0, cp = next){
3803 /* C99 */{
3804 si32_t i, j;
3806 i = n_UNALIGN(si32_t*,cp)[0];
3807 j = i & SI32_MAX;
3808 next = &cp[j];
3809 len -= j;
3810 if(i == j)
3811 continue;
3813 /* struct{si32_t buf_len_iscap; si32_t cap_len; wc_t name[];} */
3814 cp += sizeof(si32_t);
3815 i = n_UNALIGN(si32_t*,cp)[0];
3816 cp += sizeof(si32_t);
3817 for(j = 0; j < i; ++j)
3818 capname[j] = n_UNALIGN(wc_t*,cp)[j];
3819 capname[j] = '\0';
3822 /* Use generic lookup mechanism if not a known query */
3823 /* C99 */{
3824 si32_t tq;
3826 tq = n_termcap_query_for_name(capname, n_TERMCAP_CAPTYPE_STRING);
3827 if(tq == -1){
3828 tv.tv_data.tvd_string = capname;
3829 tq = n__TERMCAP_QUERY_MAX1;
3832 if(tq < 0 || !n_termcap_query(tq, &tv)){
3833 if(n_poption & n_PO_D_V)
3834 n_err(_("`bind': unknown or unsupported capability: %s: %s\n"),
3835 capname, tbcp->tbc_seq);
3836 tbcp->tbc_flags |= a_TTY_BIND_DEFUNCT;
3837 break;
3841 /* struct{si32_t buf_len_iscap; si32_t cap_len; char buf[]+NUL;} */
3842 /* C99 */{
3843 size_t i;
3845 i = strlen(tv.tv_data.tvd_string);
3846 if(/*i > SI32_MAX ||*/ i >= PTR2SIZE(next - cp)){
3847 if(n_poption & n_PO_D_V)
3848 n_err(_("`bind': capability expansion too long: %s: %s\n"),
3849 capname, tbcp->tbc_seq);
3850 tbcp->tbc_flags |= a_TTY_BIND_DEFUNCT;
3851 break;
3852 }else if(i == 0){
3853 if(n_poption & n_PO_D_V)
3854 n_err(_("`bind': empty capability expansion: %s: %s\n"),
3855 capname, tbcp->tbc_seq);
3856 tbcp->tbc_flags |= a_TTY_BIND_DEFUNCT;
3857 break;
3858 }else if(isfirst && !cntrlchar(*tv.tv_data.tvd_string)){
3859 if(n_poption & n_PO_D_V)
3860 n_err(_("`bind': capability expansion does not start with "
3861 "control: %s: %s\n"), capname, tbcp->tbc_seq);
3862 tbcp->tbc_flags |= a_TTY_BIND_DEFUNCT;
3863 break;
3865 n_UNALIGN(si32_t*,cp)[-1] = (si32_t)i;
3866 memcpy(cp, tv.tv_data.tvd_string, i);
3867 cp[i] = '\0';
3870 NYD2_LEAVE;
3873 static void
3874 a_tty_bind_del(struct a_tty_bind_parse_ctx *tbpcp){
3875 struct a_tty_bind_ctx *ltbcp, *tbcp;
3876 NYD2_ENTER;
3878 tbcp = tbpcp->tbpc_tbcp;
3880 if((ltbcp = tbpcp->tbpc_ltbcp) != NULL)
3881 ltbcp->tbc_next = tbcp->tbc_next;
3882 else
3883 a_tty.tg_bind[tbpcp->tbpc_flags & n__GO_INPUT_CTX_MASK] = tbcp->tbc_next;
3884 n_free(tbcp);
3886 --a_tty.tg_bind_cnt;
3887 a_tty.tg_bind_isdirty = TRU1;
3888 NYD2_LEAVE;
3891 static void
3892 a_tty_bind_tree_build(void){
3893 size_t i;
3894 NYD2_ENTER;
3896 for(i = 0; i < n__GO_INPUT_CTX_MAX1; ++i){
3897 struct a_tty_bind_ctx *tbcp;
3898 n_LCTAV(n_GO_INPUT_CTX_BASE == 0);
3900 /* Somewhat wasteful, but easier to handle: simply clone the entire
3901 * primary key onto the secondary one, then only modify it */
3902 for(tbcp = a_tty.tg_bind[n_GO_INPUT_CTX_BASE]; tbcp != NULL;
3903 tbcp = tbcp->tbc_next)
3904 if(!(tbcp->tbc_flags & a_TTY_BIND_DEFUNCT))
3905 a_tty__bind_tree_add(n_GO_INPUT_CTX_BASE, &a_tty.tg_bind_tree[i][0],
3906 tbcp);
3908 if(i != n_GO_INPUT_CTX_BASE)
3909 for(tbcp = a_tty.tg_bind[i]; tbcp != NULL; tbcp = tbcp->tbc_next)
3910 if(!(tbcp->tbc_flags & a_TTY_BIND_DEFUNCT))
3911 a_tty__bind_tree_add(i, &a_tty.tg_bind_tree[i][0], tbcp);
3914 a_tty.tg_bind_isbuild = TRU1;
3915 NYD2_LEAVE;
3918 static void
3919 a_tty_bind_tree_teardown(void){
3920 size_t i, j;
3921 NYD2_ENTER;
3923 memset(&a_tty.tg_bind_shcut_cancel[0], 0,
3924 sizeof(a_tty.tg_bind_shcut_cancel));
3925 memset(&a_tty.tg_bind_shcut_prompt_char[0], 0,
3926 sizeof(a_tty.tg_bind_shcut_prompt_char));
3928 for(i = 0; i < n__GO_INPUT_CTX_MAX1; ++i)
3929 for(j = 0; j < HSHSIZE; ++j)
3930 a_tty__bind_tree_free(a_tty.tg_bind_tree[i][j]);
3931 memset(&a_tty.tg_bind_tree[0], 0, sizeof(a_tty.tg_bind_tree));
3933 a_tty.tg_bind_isdirty = a_tty.tg_bind_isbuild = FAL0;
3934 NYD2_LEAVE;
3937 static void
3938 a_tty__bind_tree_add(ui32_t hmap_idx, struct a_tty_bind_tree *store[HSHSIZE],
3939 struct a_tty_bind_ctx *tbcp){
3940 ui32_t cnvlen;
3941 char const *cnvdat;
3942 struct a_tty_bind_tree *ntbtp;
3943 NYD2_ENTER;
3944 n_UNUSED(hmap_idx);
3946 ntbtp = NULL;
3948 for(cnvdat = tbcp->tbc_cnv, cnvlen = tbcp->tbc_cnv_len; cnvlen > 0;){
3949 union {wchar_t const *wp; char const *cp;} u;
3950 si32_t entlen;
3952 /* {si32_t buf_len_iscap;} */
3953 entlen = *n_UNALIGN(si32_t const*,cnvdat);
3955 if(entlen & SI32_MIN){
3956 /* struct{si32_t buf_len_iscap; si32_t cap_len; char buf[]+NUL;}
3957 * Note that empty capabilities result in DEFUNCT */
3958 for(u.cp = (char const*)&n_UNALIGN(si32_t const*,cnvdat)[2];
3959 *u.cp != '\0'; ++u.cp)
3960 ntbtp = a_tty__bind_tree_add_wc(store, ntbtp, *u.cp, TRU1);
3961 assert(ntbtp != NULL);
3962 ntbtp->tbt_isseq_trail = TRU1;
3963 entlen &= SI32_MAX;
3964 }else{
3965 /* struct{si32_t buf_len_iscap; wc_t buf[]+NUL;} */
3966 bool_t isseq;
3968 u.wp = (wchar_t const*)&n_UNALIGN(si32_t const*,cnvdat)[1];
3970 /* May be a special shortcut function? */
3971 if(ntbtp == NULL && (tbcp->tbc_flags & a_TTY_BIND_MLE1CNTRL)){
3972 char *cp;
3973 ui32_t ctx, fun;
3975 ctx = tbcp->tbc_flags & n__GO_INPUT_CTX_MASK;
3976 fun = tbcp->tbc_flags & a_TTY__BIND_FUN_MASK;
3978 if(fun == a_TTY_BIND_FUN_CANCEL){
3979 for(cp = &a_tty.tg_bind_shcut_cancel[ctx][0];
3980 PTRCMP(cp, <, &a_tty.tg_bind_shcut_cancel[ctx]
3981 [n_NELEM(a_tty.tg_bind_shcut_cancel[ctx]) - 1]); ++cp)
3982 if(*cp == '\0'){
3983 *cp = (char)*u.wp;
3984 break;
3986 }else if(fun == a_TTY_BIND_FUN_PROMPT_CHAR){
3987 for(cp = &a_tty.tg_bind_shcut_prompt_char[ctx][0];
3988 PTRCMP(cp, <, &a_tty.tg_bind_shcut_prompt_char[ctx]
3989 [n_NELEM(a_tty.tg_bind_shcut_prompt_char[ctx]) - 1]);
3990 ++cp)
3991 if(*cp == '\0'){
3992 *cp = (char)*u.wp;
3993 break;
3998 isseq = (u.wp[1] != '\0');
3999 for(; *u.wp != '\0'; ++u.wp)
4000 ntbtp = a_tty__bind_tree_add_wc(store, ntbtp, *u.wp, isseq);
4001 if(isseq){
4002 assert(ntbtp != NULL);
4003 ntbtp->tbt_isseq_trail = TRU1;
4007 cnvlen -= entlen;
4008 cnvdat += entlen;
4011 /* Should have been rendered defunctional at first instead */
4012 assert(ntbtp != NULL);
4013 ntbtp->tbt_bind = tbcp;
4014 NYD2_LEAVE;
4017 static struct a_tty_bind_tree *
4018 a_tty__bind_tree_add_wc(struct a_tty_bind_tree **treep,
4019 struct a_tty_bind_tree *parentp, wchar_t wc, bool_t isseq){
4020 struct a_tty_bind_tree *tbtp, *xtbtp;
4021 NYD2_ENTER;
4023 if(parentp == NULL){
4024 treep += wc % HSHSIZE;
4026 /* Having no parent also means that the tree slot is possibly empty */
4027 for(tbtp = *treep; tbtp != NULL;
4028 parentp = tbtp, tbtp = tbtp->tbt_sibling){
4029 if(tbtp->tbt_char != wc)
4030 continue;
4031 if(tbtp->tbt_isseq == isseq)
4032 goto jleave;
4033 /* isseq MUST be linked before !isseq, so record this "parent"
4034 * sibling, but continue searching for now.
4035 * Otherwise it is impossible that we'll find what we look for */
4036 if(isseq){
4037 #ifdef HAVE_DEBUG
4038 while((tbtp = tbtp->tbt_sibling) != NULL)
4039 assert(tbtp->tbt_char != wc);
4040 #endif
4041 break;
4045 tbtp = n_alloc(sizeof *tbtp);
4046 memset(tbtp, 0, sizeof *tbtp);
4047 tbtp->tbt_char = wc;
4048 tbtp->tbt_isseq = isseq;
4050 if(parentp == NULL){
4051 tbtp->tbt_sibling = *treep;
4052 *treep = tbtp;
4053 }else{
4054 tbtp->tbt_sibling = parentp->tbt_sibling;
4055 parentp->tbt_sibling = tbtp;
4057 }else{
4058 if((tbtp = *(treep = &parentp->tbt_childs)) != NULL){
4059 for(;; tbtp = xtbtp){
4060 if(tbtp->tbt_char == wc){
4061 if(tbtp->tbt_isseq == isseq)
4062 goto jleave;
4063 /* isseq MUST be linked before, so it is impossible that we'll
4064 * find what we look for */
4065 if(isseq){
4066 #ifdef HAVE_DEBUG
4067 while((tbtp = tbtp->tbt_sibling) != NULL)
4068 assert(tbtp->tbt_char != wc);
4069 #endif
4070 tbtp = NULL;
4071 break;
4075 if((xtbtp = tbtp->tbt_sibling) == NULL){
4076 treep = &tbtp->tbt_sibling;
4077 break;
4082 xtbtp = n_alloc(sizeof *xtbtp);
4083 memset(xtbtp, 0, sizeof *xtbtp);
4084 xtbtp->tbt_parent = parentp;
4085 xtbtp->tbt_char = wc;
4086 xtbtp->tbt_isseq = isseq;
4087 tbtp = xtbtp;
4088 *treep = tbtp;
4090 jleave:
4091 NYD2_LEAVE;
4092 return tbtp;
4095 static void
4096 a_tty__bind_tree_free(struct a_tty_bind_tree *tbtp){
4097 NYD2_ENTER;
4098 while(tbtp != NULL){
4099 struct a_tty_bind_tree *tmp;
4101 if((tmp = tbtp->tbt_childs) != NULL)
4102 a_tty__bind_tree_free(tmp);
4104 tmp = tbtp->tbt_sibling;
4105 n_free(tbtp);
4106 tbtp = tmp;
4108 NYD2_LEAVE;
4110 # endif /* HAVE_KEY_BINDINGS */
4112 FL void
4113 n_tty_init(void){
4114 NYD_ENTER;
4116 if(ok_blook(line_editor_disable))
4117 goto jleave;
4119 /* Load the history file */
4120 # ifdef HAVE_HISTORY
4121 a_tty_hist_load();
4122 # endif
4124 /* Force immediate resolve for anything which follows */
4125 n_psonce |= n_PSO_LINE_EDITOR_INIT;
4127 # ifdef HAVE_KEY_BINDINGS
4128 /* `bind's (and `unbind's) done from within resource files couldn't be
4129 * performed for real since our termcap driver wasn't yet loaded, and we
4130 * can't perform automatic init since the user may have disallowed so */
4131 /* C99 */{ /* TODO outsource into own file */
4132 struct a_tty_bind_ctx *tbcp;
4133 enum n_go_input_flags gif;
4135 for(gif = 0; gif < n__GO_INPUT_CTX_MAX1; ++gif)
4136 for(tbcp = a_tty.tg_bind[gif]; tbcp != NULL; tbcp = tbcp->tbc_next)
4137 if((tbcp->tbc_flags & (a_TTY_BIND_RESOLVE | a_TTY_BIND_DEFUNCT)) ==
4138 a_TTY_BIND_RESOLVE)
4139 a_tty_bind_resolve(tbcp);
4142 /* And we want to (try to) install some default key bindings */
4143 if(!ok_blook(line_editor_no_defaults)){
4144 char buf[8];
4145 struct a_tty_bind_parse_ctx tbpc;
4146 struct a_tty_bind_builtin_tuple const *tbbtp, *tbbtp_max;
4147 ui32_t flags;
4149 buf[0] = '$', buf[1] = '\'', buf[2] = '\\', buf[3] = 'c',
4150 buf[5] = '\'', buf[6] = '\0';
4152 tbbtp = a_tty_bind_base_tuples;
4153 tbbtp_max = &tbbtp[n_NELEM(a_tty_bind_base_tuples)];
4154 flags = n_GO_INPUT_CTX_BASE;
4155 jbuiltin_redo:
4156 for(; tbbtp < tbbtp_max; ++tbbtp){
4157 memset(&tbpc, 0, sizeof tbpc);
4158 tbpc.tbpc_cmd = "bind";
4159 if(tbbtp->tbbt_iskey){
4160 buf[4] = tbbtp->tbbt_ckey;
4161 tbpc.tbpc_in_seq = buf;
4162 }else
4163 tbpc.tbpc_in_seq = savecatsep(":", '\0',
4164 n_termcap_name_of_query(tbbtp->tbbt_query));
4165 tbpc.tbpc_exp.s = n_UNCONST(tbbtp->tbbt_exp[0] == '\0'
4166 ? a_tty_bind_fun_names[(ui8_t)tbbtp->tbbt_exp[1]]
4167 : tbbtp->tbbt_exp);
4168 tbpc.tbpc_exp.l = strlen(tbpc.tbpc_exp.s);
4169 tbpc.tbpc_flags = flags;
4170 /* ..but don't want to overwrite any user settings */
4171 a_tty_bind_create(&tbpc, FAL0);
4173 if(flags == n_GO_INPUT_CTX_BASE){
4174 tbbtp = a_tty_bind_default_tuples;
4175 tbbtp_max = &tbbtp[n_NELEM(a_tty_bind_default_tuples)];
4176 flags = n_GO_INPUT_CTX_DEFAULT;
4177 goto jbuiltin_redo;
4180 # endif /* HAVE_KEY_BINDINGS */
4182 jleave:
4183 NYD_LEAVE;
4186 FL void
4187 n_tty_destroy(bool_t xit_fastpath){
4188 NYD_ENTER;
4190 if(!(n_psonce & n_PSO_LINE_EDITOR_INIT))
4191 goto jleave;
4193 /* Write the history file */
4194 # ifdef HAVE_HISTORY
4195 if(!xit_fastpath)
4196 a_tty_hist_save();
4197 # endif
4199 # if defined HAVE_KEY_BINDINGS && defined HAVE_DEBUG
4200 if(!xit_fastpath)
4201 n_go_command(n_GO_INPUT_NONE, "unbind * *");
4202 # endif
4204 # ifdef HAVE_DEBUG
4205 memset(&a_tty, 0, sizeof a_tty);
4207 n_psonce &= ~n_PSO_LINE_EDITOR_INIT;
4208 # endif
4209 jleave:
4210 NYD_LEAVE;
4213 FL int
4214 (n_tty_readline)(enum n_go_input_flags gif, char const *prompt,
4215 char **linebuf, size_t *linesize, size_t n, bool_t *histok_or_null
4216 n_MEMORY_DEBUG_ARGS){
4217 struct a_tty_line tl;
4218 struct n_string xprompt;
4219 # ifdef HAVE_COLOUR
4220 char *posbuf, *pos;
4221 # endif
4222 ssize_t nn;
4223 NYD_ENTER;
4225 assert(!ok_blook(line_editor_disable));
4226 if(!(n_psonce & n_PSO_LINE_EDITOR_INIT))
4227 n_tty_init();
4228 assert(n_psonce & n_PSO_LINE_EDITOR_INIT);
4230 # ifdef HAVE_COLOUR
4231 n_colour_env_create(n_COLOUR_CTX_MLE, n_tty_fp, FAL0);
4233 /* .tl_pos_buf is a hack */
4234 posbuf = pos = NULL;
4236 if(n_COLOUR_IS_ACTIVE()){
4237 char const *ccol;
4238 struct n_colour_pen *ccp;
4239 struct str const *sp;
4241 if((ccp = n_colour_pen_create(n_COLOUR_ID_MLE_POSITION, NULL)) != NULL &&
4242 (sp = n_colour_pen_to_str(ccp)) != NULL){
4243 ccol = sp->s;
4244 if((sp = n_colour_reset_to_str()) != NULL){
4245 size_t l1, l2;
4247 l1 = strlen(ccol);
4248 l2 = strlen(sp->s);
4249 posbuf = n_autorec_alloc(l1 + 4 + l2 +1);
4250 memcpy(posbuf, ccol, l1);
4251 pos = &posbuf[l1];
4252 memcpy(&pos[4], sp->s, ++l2);
4257 if(posbuf == NULL){
4258 posbuf = pos = n_autorec_alloc(4 +1);
4259 pos[4] = '\0';
4261 # endif /* HAVE_COLOUR */
4263 memset(&tl, 0, sizeof tl);
4264 tl.tl_goinflags = gif;
4266 # ifdef HAVE_KEY_BINDINGS
4267 /* C99 */{
4268 char const *cp;
4270 if((cp = ok_vlook(bind_timeout)) != NULL){
4271 ui64_t uib;
4273 n_idec_ui64_cp(&uib, cp, 0, NULL);
4275 if(uib > 0 &&
4276 /* Convert to tenths of a second, unfortunately */
4277 (uib = (uib + 99) / 100) <= a_TTY_BIND_TIMEOUT_MAX)
4278 tl.tl_bind_timeout = (ui8_t)uib;
4279 else if(n_poption & n_PO_D_V)
4280 n_err(_("Ignoring invalid *bind-timeout*: %s\n"), cp);
4284 if(a_tty.tg_bind_isdirty)
4285 a_tty_bind_tree_teardown();
4286 if(a_tty.tg_bind_cnt > 0 && !a_tty.tg_bind_isbuild)
4287 a_tty_bind_tree_build();
4288 tl.tl_bind_tree_hmap = &a_tty.tg_bind_tree[gif & n__GO_INPUT_CTX_MASK];
4289 tl.tl_bind_shcut_cancel =
4290 &a_tty.tg_bind_shcut_cancel[gif & n__GO_INPUT_CTX_MASK];
4291 tl.tl_bind_shcut_prompt_char =
4292 &a_tty.tg_bind_shcut_prompt_char[gif & n__GO_INPUT_CTX_MASK];
4293 # endif /* HAVE_KEY_BINDINGS */
4295 # ifdef HAVE_COLOUR
4296 tl.tl_pos_buf = posbuf;
4297 tl.tl_pos = pos;
4298 # endif
4300 if(!(gif & n_GO_INPUT_PROMPT_NONE)){
4301 n_string_creat_auto(&xprompt);
4303 if((tl.tl_prompt_width = n_tty_create_prompt(&xprompt, prompt, gif)
4304 ) > 0){
4305 tl.tl_prompt = n_string_cp_const(&xprompt);
4306 tl.tl_prompt_length = (ui32_t)xprompt.s_len;
4310 tl.tl_line.cbuf = *linebuf;
4311 if(n != 0){
4312 tl.tl_defc.s = savestrbuf(*linebuf, n);
4313 tl.tl_defc.l = n;
4315 tl.tl_x_buf = linebuf;
4316 tl.tl_x_bufsize = linesize;
4318 a_tty.tg_line = &tl;
4319 a_tty_sigs_up();
4320 n_TERMCAP_RESUME(FAL0);
4321 a_tty_term_mode(TRU1);
4322 nn = a_tty_readline(&tl, n, histok_or_null n_MEMORY_DEBUG_ARGSCALL);
4323 n_COLOUR( n_colour_env_gut(); )
4324 a_tty_term_mode(FAL0);
4325 n_TERMCAP_SUSPEND(FAL0);
4326 a_tty_sigs_down();
4327 a_tty.tg_line = NULL;
4329 NYD_LEAVE;
4330 return (int)nn;
4333 FL void
4334 n_tty_addhist(char const *s, enum n_go_input_flags gif){
4335 NYD_ENTER;
4336 n_UNUSED(s);
4337 n_UNUSED(gif);
4339 # ifdef HAVE_HISTORY
4340 if(*s != '\0' && (n_psonce & n_PSO_LINE_EDITOR_INIT) &&
4341 a_tty.tg_hist_size_max > 0 &&
4342 (!(gif & n_GO_INPUT_HIST_GABBY) || ok_blook(history_gabby)) &&
4343 !ok_blook(line_editor_disable)){
4344 hold_all_sigs();
4345 a_tty_hist_add(s, gif);
4346 rele_all_sigs();
4348 # endif
4349 NYD_LEAVE;
4352 # ifdef HAVE_HISTORY
4353 FL int
4354 c_history(void *v){
4355 siz_t entry;
4356 struct a_tty_hist *thp;
4357 char **argv;
4358 NYD_ENTER;
4360 if(ok_blook(line_editor_disable)){
4361 n_err(_("history: *line-editor-disable* is set\n"));
4362 goto jerr;
4365 if(!(n_psonce & n_PSO_LINE_EDITOR_INIT)){
4366 n_tty_init();
4367 assert(n_psonce & n_PSO_LINE_EDITOR_INIT);
4370 if(*(argv = v) == NULL)
4371 goto jlist;
4372 if(argv[1] != NULL)
4373 goto jerr;
4374 if(!asccasecmp(*argv, "show"))
4375 goto jlist;
4376 if(!asccasecmp(*argv, "clear"))
4377 goto jclear;
4379 if(!asccasecmp(*argv, "load")){
4380 if(!a_tty_hist_load())
4381 v = NULL;
4382 goto jleave;
4384 if(!asccasecmp(*argv, "save")){
4385 if(!a_tty_hist_save())
4386 v = NULL;
4387 goto jleave;
4390 if((n_idec_siz_cp(&entry, *argv, 10, NULL
4391 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
4392 ) == n_IDEC_STATE_CONSUMED)
4393 goto jentry;
4394 jerr:
4395 n_err(_("Synopsis: history: %s\n"),
4396 /* Same string as in cmd-tab.h, still hoping...) */
4397 _("<show (default)|load|save|clear> or select history <NO>"));
4398 v = NULL;
4399 jleave:
4400 NYD_LEAVE;
4401 return (v == NULL ? !STOP : !OKAY); /* xxx 1:bad 0:good -- do some */
4403 jlist:{
4404 size_t no, l, b;
4405 FILE *fp;
4407 if(a_tty.tg_hist == NULL)
4408 goto jleave;
4410 if((fp = Ftmp(NULL, "hist", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL){
4411 n_perr(_("tmpfile"), 0);
4412 v = NULL;
4413 goto jleave;
4416 no = a_tty.tg_hist_size;
4417 l = b = 0;
4419 for(thp = a_tty.tg_hist; thp != NULL;
4420 --no, ++l, thp = thp->th_older){
4421 char c1, c2;
4423 b += thp->th_len;
4425 switch(thp->th_flags & a_TTY_HIST_CTX_MASK){
4426 default:
4427 case a_TTY_HIST_CTX_DEFAULT:
4428 c1 = 'd';
4429 break;
4430 case a_TTY_HIST_CTX_COMPOSE:
4431 c1 = 'c';
4432 break;
4434 c2 = (thp->th_flags & a_TTY_HIST_GABBY) ? '*' : ' ';
4436 if(n_poption & n_PO_D_V)
4437 fprintf(fp, "# Length +%" PRIu32 ", total %" PRIuZ "\n",
4438 thp->th_len, b);
4439 fprintf(fp, "%c%c%4" PRIuZ "\t%s\n", c1, c2, no, thp->th_dat);
4442 page_or_print(fp, l);
4443 Fclose(fp);
4445 goto jleave;
4447 jclear:
4448 while((thp = a_tty.tg_hist) != NULL){
4449 a_tty.tg_hist = thp->th_older;
4450 n_free(thp);
4452 a_tty.tg_hist_tail = NULL;
4453 a_tty.tg_hist_size = 0;
4454 goto jleave;
4456 jentry:{
4457 siz_t ep;
4459 ep = (entry < 0) ? -entry : entry;
4461 if(ep != 0 && UICMP(z, ep, <=, a_tty.tg_hist_size)){
4462 if(ep != entry)
4463 --ep;
4464 else
4465 ep = (siz_t)a_tty.tg_hist_size - ep;
4466 for(thp = a_tty.tg_hist;; thp = thp->th_older){
4467 assert(thp != NULL);
4468 if(ep-- == 0){
4469 n_go_input_inject((n_GO_INPUT_INJECT_COMMIT |
4470 n_GO_INPUT_INJECT_HISTORY), v = thp->th_dat, thp->th_len);
4471 break;
4474 }else{
4475 n_err(_("`history': no such entry: %" PRIdZ "\n"), entry);
4476 v = NULL;
4479 goto jleave;
4481 # endif /* HAVE_HISTORY */
4483 # ifdef HAVE_KEY_BINDINGS
4484 FL int
4485 c_bind(void *v){
4486 struct a_tty_bind_ctx *tbcp;
4487 enum n_go_input_flags gif;
4488 bool_t aster, show;
4489 union {char const *cp; char *p; char c;} c;
4490 struct n_cmd_arg_ctx *cacp;
4491 NYD_ENTER;
4493 cacp = v;
4495 c.cp = cacp->cac_arg->ca_arg.ca_str.s;
4496 if(cacp->cac_no == 1)
4497 show = TRU1;
4498 else
4499 show = !asccasecmp(cacp->cac_arg->ca_next->ca_arg.ca_str.s, "show");
4500 aster = FAL0;
4502 if((gif = a_tty_bind_ctx_find(c.cp)) == (enum n_go_input_flags)-1){
4503 if(!(aster = n_is_all_or_aster(c.cp)) || !show){
4504 n_err(_("`bind': invalid context: %s\n"), c.cp);
4505 v = NULL;
4506 goto jleave;
4508 gif = 0;
4511 if(show){
4512 ui32_t lns;
4513 FILE *fp;
4515 if((fp = Ftmp(NULL, "bind", OF_RDWR | OF_UNLINK | OF_REGISTER)) == NULL){
4516 n_perr(_("tmpfile"), 0);
4517 v = NULL;
4518 goto jleave;
4521 lns = 0;
4522 for(;;){
4523 for(tbcp = a_tty.tg_bind[gif]; tbcp != NULL;
4524 ++lns, tbcp = tbcp->tbc_next){
4525 /* Print the bytes of resolved terminal capabilities, then */
4526 if((n_poption & n_PO_D_V) &&
4527 (tbcp->tbc_flags & (a_TTY_BIND_RESOLVE | a_TTY_BIND_DEFUNCT)
4528 ) == a_TTY_BIND_RESOLVE){
4529 char cbuf[8];
4530 union {wchar_t const *wp; char const *cp;} u;
4531 si32_t entlen;
4532 ui32_t cnvlen;
4533 char const *cnvdat, *bsep, *cbufp;
4535 putc('#', fp);
4536 putc(' ', fp);
4538 cbuf[0] = '=', cbuf[2] = '\0';
4539 for(cnvdat = tbcp->tbc_cnv, cnvlen = tbcp->tbc_cnv_len;
4540 cnvlen > 0;){
4541 if(cnvdat != tbcp->tbc_cnv)
4542 putc(',', fp);
4544 /* {si32_t buf_len_iscap;} */
4545 entlen = *n_UNALIGN(si32_t const*,cnvdat);
4546 if(entlen & SI32_MIN){
4547 /* struct{si32_t buf_len_iscap; si32_t cap_len;
4548 * char buf[]+NUL;} */
4549 for(bsep = n_empty,
4550 u.cp = (char const*)
4551 &n_UNALIGN(si32_t const*,cnvdat)[2];
4552 (c.c = *u.cp) != '\0'; ++u.cp){
4553 if(asciichar(c.c) && !cntrlchar(c.c))
4554 cbuf[1] = c.c, cbufp = cbuf;
4555 else
4556 cbufp = n_empty;
4557 fprintf(fp, "%s\\x%02X%s",
4558 bsep, (ui32_t)(ui8_t)c.c, cbufp);
4559 bsep = " ";
4561 entlen &= SI32_MAX;
4562 }else
4563 putc('-', fp);
4565 cnvlen -= entlen;
4566 cnvdat += entlen;
4569 fputs("\n ", fp);
4570 ++lns;
4573 fprintf(fp, "%sbind %s %s %s%s%s\n",
4574 ((tbcp->tbc_flags & a_TTY_BIND_DEFUNCT)
4575 /* I18N: `bind' sequence not working, either because it is
4576 * I18N: using Unicode and that is not available in the locale,
4577 * I18N: or a termcap(5)/terminfo(5) sequence won't work out */
4578 ? _("# <Defunctional> ") : n_empty),
4579 a_tty_bind_ctx_maps[gif].tbcm_name, tbcp->tbc_seq,
4580 n_shexp_quote_cp(tbcp->tbc_exp, TRU1),
4581 (tbcp->tbc_flags & a_TTY_BIND_NOCOMMIT ? n_at : n_empty),
4582 (!(n_poption & n_PO_D_VV) ? n_empty
4583 : (tbcp->tbc_flags & a_TTY_BIND_FUN_INTERNAL
4584 ? _(" # MLE internal") : n_empty))
4587 if(!aster || ++gif >= n__GO_INPUT_CTX_MAX1)
4588 break;
4590 page_or_print(fp, lns);
4592 Fclose(fp);
4593 }else{
4594 struct a_tty_bind_parse_ctx tbpc;
4595 struct n_cmd_arg *cap;
4597 memset(&tbpc, 0, sizeof tbpc);
4598 tbpc.tbpc_cmd = cacp->cac_desc->cad_name;
4599 tbpc.tbpc_in_seq = (cap = cacp->cac_arg->ca_next)->ca_arg.ca_str.s;
4600 if((cap = cap->ca_next) != NULL){
4601 tbpc.tbpc_exp.s = cap->ca_arg.ca_str.s;
4602 tbpc.tbpc_exp.l = cap->ca_arg.ca_str.l;
4604 tbpc.tbpc_flags = gif;
4605 if(!a_tty_bind_create(&tbpc, TRU1))
4606 v = NULL;
4608 jleave:
4609 NYD_LEAVE;
4610 return (v != NULL) ? n_EXIT_OK : n_EXIT_ERR;
4613 FL int
4614 c_unbind(void *v){
4615 struct a_tty_bind_parse_ctx tbpc;
4616 struct a_tty_bind_ctx *tbcp;
4617 enum n_go_input_flags gif;
4618 bool_t aster;
4619 union {char const *cp; char *p;} c;
4620 struct n_cmd_arg_ctx *cacp;
4621 NYD_ENTER;
4623 cacp = v;
4624 c.cp = cacp->cac_arg->ca_arg.ca_str.s;
4625 aster = FAL0;
4627 if((gif = a_tty_bind_ctx_find(c.cp)) == (enum n_go_input_flags)-1){
4628 if(!(aster = n_is_all_or_aster(c.cp))){
4629 n_err(_("`unbind': invalid context: %s\n"), c.cp);
4630 v = NULL;
4631 goto jleave;
4633 gif = 0;
4636 c.cp = cacp->cac_arg->ca_next->ca_arg.ca_str.s;
4637 jredo:
4638 if(n_is_all_or_aster(c.cp)){
4639 while((tbcp = a_tty.tg_bind[gif]) != NULL){
4640 memset(&tbpc, 0, sizeof tbpc);
4641 tbpc.tbpc_tbcp = tbcp;
4642 tbpc.tbpc_flags = gif;
4643 a_tty_bind_del(&tbpc);
4645 }else{
4646 memset(&tbpc, 0, sizeof tbpc);
4647 tbpc.tbpc_cmd = cacp->cac_desc->cad_name;
4648 tbpc.tbpc_in_seq = c.cp;
4649 tbpc.tbpc_flags = gif;
4651 if(n_UNLIKELY(!a_tty_bind_parse(FAL0, &tbpc)))
4652 v = NULL;
4653 else if(n_UNLIKELY((tbcp = tbpc.tbpc_tbcp) == NULL)){
4654 n_err(_("`unbind': no such `bind'ing: %s %s\n"),
4655 a_tty_bind_ctx_maps[gif].tbcm_name, c.cp);
4656 v = NULL;
4657 }else
4658 a_tty_bind_del(&tbpc);
4661 if(aster && ++gif < n__GO_INPUT_CTX_MAX1)
4662 goto jredo;
4663 jleave:
4664 NYD_LEAVE;
4665 return (v != NULL) ? n_EXIT_OK : n_EXIT_ERR;
4667 # endif /* HAVE_KEY_BINDINGS */
4669 #else /* HAVE_MLE */
4671 * The really-nothing-at-all implementation
4674 # ifdef a_TTY_SIGNALS
4675 static void
4676 a_tty_signal(int sig){
4677 /* Prototype at top */
4678 # ifdef HAVE_TERMCAP
4679 sigset_t nset, oset;
4680 # endif
4681 NYD_X; /* Signal handler */
4682 n_UNUSED(sig);
4684 # ifdef HAVE_TERMCAP
4685 n_TERMCAP_SUSPEND(TRU1);
4686 a_tty_sigs_down();
4688 sigemptyset(&nset);
4689 sigaddset(&nset, sig);
4690 sigprocmask(SIG_UNBLOCK, &nset, &oset);
4691 n_raise(sig);
4692 /* When we come here we'll continue editing, so reestablish */
4693 sigprocmask(SIG_BLOCK, &oset, (sigset_t*)NULL);
4695 a_tty_sigs_up();
4696 n_TERMCAP_RESUME(TRU1);
4697 # endif /* HAVE_TERMCAP */
4699 # endif /* a_TTY_SIGNALS */
4701 # if 0
4702 FL void
4703 n_tty_init(void){
4704 NYD_ENTER;
4705 NYD_LEAVE;
4708 FL void
4709 n_tty_destroy(bool_t xit_fastpath){
4710 NYD_ENTER;
4711 n_UNUSED(xit_fastpath);
4712 NYD_LEAVE;
4714 # endif /* 0 */
4716 FL int
4717 (n_tty_readline)(enum n_go_input_flags gif, char const *prompt,
4718 char **linebuf, size_t *linesize, size_t n, bool_t *histok_or_null
4719 n_MEMORY_DEBUG_ARGS){
4720 struct n_string xprompt;
4721 int rv;
4722 NYD_ENTER;
4723 n_UNUSED(histok_or_null);
4725 if(!(gif & n_GO_INPUT_PROMPT_NONE)){
4726 if(n_tty_create_prompt(n_string_creat_auto(&xprompt), prompt, gif) > 0){
4727 fwrite(xprompt.s_dat, 1, xprompt.s_len, n_tty_fp);
4728 fflush(n_tty_fp);
4732 # ifdef HAVE_TERMCAP
4733 a_tty_sigs_up();
4734 n_TERMCAP_RESUME(FAL0);
4735 # endif
4736 rv = (readline_restart)(n_stdin, linebuf, linesize, n
4737 n_MEMORY_DEBUG_ARGSCALL);
4738 # ifdef HAVE_TERMCAP
4739 n_TERMCAP_SUSPEND(FAL0);
4740 a_tty_sigs_down();
4741 # endif
4742 NYD_LEAVE;
4743 return rv;
4746 FL void
4747 n_tty_addhist(char const *s, enum n_go_input_flags gif){
4748 NYD_ENTER;
4749 n_UNUSED(s);
4750 n_UNUSED(gif);
4751 NYD_LEAVE;
4753 #endif /* nothing at all */
4755 #undef a_TTY_SIGNALS
4756 /* s-it-mode */