(Pseudo) Fix history (non gabbiness) by stripping PS_ARGLIST_MASK
[s-mailx.git] / termcap.c
blobd3cdc75faf0719f4f759fda46a82c52c670c81f7
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Terminal capability interaction.
3 *@ For encapsulation purposes provide a basic foundation even without
4 *@ HAVE_TERMCAP, but with nail.h:n_HAVE_TCAP.
5 *@ HOWTO add a new non-dynamic command or query:
6 *@ - add an entry to nail.h:enum n_termcap_{cmd,query}
7 *@ - run mk-tcap-map.pl
8 *@ - update the *termcap* member documentation on changes!
9 *@ Bug: in case of clashes of two-letter names terminfo(5) wins.
11 * Copyright (c) 2016 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
13 * Permission to use, copy, modify, and/or distribute this software for any
14 * purpose with or without fee is hereby granted, provided that the above
15 * copyright notice and this permission notice appear in all copies.
17 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
18 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
20 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
23 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 #undef n_FILE
26 #define n_FILE termcap
28 #ifndef HAVE_AMALGAMATION
29 # include "nail.h"
30 #endif
32 EMPTY_FILE()
33 #ifdef n_HAVE_TCAP
34 /* If available, curses.h must be included before term.h! */
35 #ifdef HAVE_TERMCAP
36 # ifdef HAVE_TERMCAP_CURSES
37 # include <curses.h>
38 # endif
39 # include <term.h>
40 #endif
43 * xxx We are not really compatible with very old and strange terminals since
44 * we don't care at all for circumstances indicated by terminal flags: if we
45 * find a capability we use it and assume it works. E.g., if "Co" indicates
46 * colours we simply use ISO 6429 also for font attributes etc. That is,
47 * we don't use the ncurses/terminfo interface with all its internal logic.
48 * TODO After I/O layer rewrite, "output to STDIN_FILENO".
51 /* Unless HAVE_TERMINFO or HAVE_TGETENT_NULL_BUF are defined we will use this
52 * to space the buffer we pass through to tgetent(3).
53 * Since for (such) elder non-emulated terminals really weird things will
54 * happen if an entry would require more than 1024 bytes, don't really mind.
55 * Use a ui16_t for storage */
56 #define a_TERMCAP_ENTRYSIZE_MAX ((2668 + 64) & ~64) /* As of ncurses 6.0 */
58 n_CTA(a_TERMCAP_ENTRYSIZE_MAX < UI16_MAX,
59 "Chosen buffer size exceeds datatype capability");
61 /* For simplicity we store commands and queries in single continuous control
62 * and entry structure arrays: to index queries one has to add
63 * n__TERMCAP_CMD_MAX first! And don't confound with ENTRYSIZE_MAX! */
64 enum{
65 a_TERMCAP_ENT_MAX = n__TERMCAP_CMD_MAX + n__TERMCAP_QUERY_MAX
68 enum a_termcap_flags{
69 a_TERMCAP_F_NONE,
70 /* enum n_termcap_captype values stored here.
71 * Note that presence of a type in an a_termcap_ent signals initialization */
72 a_TERMCAP_F_TYPE_MASK = (1<<4) - 1,
74 a_TERMCAP_F_QUERY = 1<<4, /* A query rather than a command */
75 a_TERMCAP_F_DISABLED = 1<<5, /* User explicitly disabled command/query */
76 a_TERMCAP_F_ALTERN = 1<<6, /* Not available, but has alternative */
77 a_TERMCAP_F_NOENT = 1<<7, /* Not available */
79 /* _cmd() argument interpretion (_T_STR) */
80 a_TERMCAP_F_ARG_IDX1 = 1<<11, /* Argument 1 used, and is an index */
81 a_TERMCAP_F_ARG_IDX2 = 1<<12,
82 a_TERMCAP_F_ARG_CNT = 1<<13, /* .., and is a count */
84 a_TERMCAP_F__LAST = a_TERMCAP_F_ARG_CNT
86 n_CTA((ui32_t)n__TERMCAP_CAPTYPE_MAX <= (ui32_t)a_TERMCAP_F_TYPE_MASK,
87 "enum n_termcap_captype exceeds bit range of a_termcap_flags");
89 struct a_termcap_control{
90 ui16_t tc_flags;
91 /* Offset base into a_termcap_namedat[], which stores the two-letter
92 * termcap(5) name directly followed by a NUL terminated terminfo(5) name.
93 * A termcap(5) name may consist of two NULs meaning ENOENT,
94 * a terminfo(5) name may be empty for the same purpose */
95 ui16_t tc_off;
97 n_CTA(a_TERMCAP_F__LAST <= UI16_MAX,
98 "a_termcap_flags exceed storage datatype in a_termcap_control");
100 struct a_termcap_ent{
101 ui16_t te_flags;
102 ui16_t te_off; /* in a_termcap_g->tg_dat / value for T_BOOL and T_NUM */
104 n_CTA(a_TERMCAP_F__LAST <= UI16_MAX,
105 "a_termcap_flags exceed storage datatype in a_termcap_ent");
107 /* Structure for extended queries, which don't have an entry constant in
108 * n_termcap_query (to allow free query/binding of keycodes) */
109 struct a_termcap_ext_ent{
110 struct a_termcap_ent tee_super;
111 ui8_t tee__dummy[4];
112 struct a_termcap_ext_ent *tee_next;
113 /* Resolvable termcap(5)/terminfo(5) name as given by user; the actual data
114 * is stored just like for normal queries */
115 char tee_name[VFIELD_SIZE(0)];
118 struct a_termcap_g{
119 struct a_termcap_ext_ent *tg_ext_ents; /* List of extended queries */
120 struct a_termcap_ent tg_ents[a_TERMCAP_ENT_MAX];
121 struct n_string tg_dat; /* Storage for resolved caps */
122 # if !defined HAVE_TGETENT_NULL_BUF && !defined HAVE_TERMINFO
123 char tg_lib_buf[a_TERMCAP_ENTRYSIZE_MAX];
124 # endif
127 /* Include the constant mk-tcap-map.pl output */
128 #include "tcaps.h"
129 n_CTA(sizeof a_termcap_namedat <= UI16_MAX,
130 "Termcap command and query name data exceed storage datatype");
131 n_CTA(a_TERMCAP_ENT_MAX == NELEM(a_termcap_control),
132 "Control array doesn't match command/query array to be controlled");
134 static struct a_termcap_g *a_termcap_g;
136 /* Query *termcap*, parse it and incorporate into a_termcap_g */
137 static void a_termcap_init_var(struct str const *termvar);
139 /* Expand ^CNTRL, \[Ee] and \OCT. False for parse error and empty results */
140 static bool_t a_termcap__strexp(struct n_string *store, char const *ibuf);
142 /* Initialize any _ent for which we have _F_ALTERN and which isn't yet set */
143 static void a_termcap_init_altern(void);
145 #ifdef HAVE_TERMCAP
146 /* Setup the library we use to work with term */
147 static bool_t a_termcap_load(char const *term);
149 /* Query the capability tcp and fill in tep (upon success) */
150 static bool_t a_termcap_ent_query(struct a_termcap_ent *tep,
151 char const *cname, ui16_t cflags);
152 SINLINE bool_t a_termcap_ent_query_tcp(struct a_termcap_ent *tep,
153 struct a_termcap_control const *tcp);
155 /* Output PTF for both, termcap(5) and terminfo(5) */
156 static int a_termcap_putc(int c);
157 #endif
159 /* Get n_termcap_cmd or n_termcap_query constant belonging to (nlen bytes of)
160 * name, or -1 if not found. min and max have to be used to cramp the result */
161 static si32_t a_termcap_enum_for_name(char const *name, size_t nlen,
162 si32_t min, si32_t max);
163 #define a_termcap_cmd_for_name(NB,NL) \
164 a_termcap_enum_for_name(NB, NL, 0, n__TERMCAP_CMD_MAX)
165 #define a_termcap_query_for_name(NB,NL) \
166 a_termcap_enum_for_name(NB, NL, n__TERMCAP_CMD_MAX, a_TERMCAP_ENT_MAX)
168 static void
169 a_termcap_init_var(struct str const *termvar){
170 char *cbp_base, *cbp;
171 size_t i;
172 char const *ccp;
173 NYD2_ENTER;
175 if(termvar->l >= UI16_MAX){
176 n_err(_("*termcap*: length excesses internal limit, skipping\n"));
177 goto j_leave;
180 assert(termvar->s[termvar->l] == '\0');
181 i = termvar->l +1;
182 cbp_base = salloc(i);
183 memcpy(cbp = cbp_base, termvar->s, i);
185 for(; (ccp = n_strsep(&cbp, ',', TRU1)) != NULL;){
186 struct a_termcap_ent *tep;
187 size_t kl;
188 char const *v;
189 ui16_t f;
191 /* Separate key/value, if any */
192 if(/* no empties ccp[0] == '\0' ||*/ ccp[1] == '\0'){
193 jeinvent:
194 n_err(_("*termcap*: invalid entry: %s\n"), ccp);
195 continue;
197 for(kl = 2, v = &ccp[2];; ++kl, ++v){
198 char c = *v;
200 if(c == '\0'){
201 f = n_TERMCAP_CAPTYPE_BOOL;
202 break;
203 }else if(c == '#'){
204 f = n_TERMCAP_CAPTYPE_NUMERIC;
205 ++v;
206 break;
207 }else if(c == '='){
208 f = n_TERMCAP_CAPTYPE_STRING;
209 ++v;
210 break;
214 /* Do we know about this one? */
215 /* C99 */{
216 struct a_termcap_control const *tcp;
217 si32_t tci;
219 tci = a_termcap_enum_for_name(ccp, kl, 0, a_TERMCAP_ENT_MAX);
220 if(tci < 0){
221 /* For key binding purposes, save any given string */
222 #ifdef HAVE_KEY_BINDINGS
223 if((f & a_TERMCAP_F_TYPE_MASK) == n_TERMCAP_CAPTYPE_STRING){
224 struct a_termcap_ext_ent *teep;
226 teep = smalloc(sizeof(*teep) -
227 VFIELD_SIZEOF(struct a_termcap_ext_ent, tee_name) + kl +1);
228 teep->tee_next = a_termcap_g->tg_ext_ents;
229 a_termcap_g->tg_ext_ents = teep;
230 memcpy(teep->tee_name, ccp, kl);
231 teep->tee_name[kl] = '\0';
233 tep = &teep->tee_super;
234 tep->te_flags = n_TERMCAP_CAPTYPE_STRING | a_TERMCAP_F_QUERY;
235 tep->te_off = (ui16_t)a_termcap_g->tg_dat.s_len;
236 if(!a_termcap__strexp(&a_termcap_g->tg_dat, v))
237 tep->te_flags |= a_TERMCAP_F_DISABLED;
238 goto jlearned;
239 }else
240 #endif /* HAVE_KEY_BINDINGS */
241 if(options & OPT_D_V)
242 n_err(_("*termcap*: unknown capability: %s\n"), ccp);
243 continue;
245 i = (size_t)tci;
247 tcp = &a_termcap_control[i];
248 if((tcp->tc_flags & a_TERMCAP_F_TYPE_MASK) != f){
249 n_err(_("*termcap*: entry type mismatch: %s\n"), ccp);
250 break;
252 tep = &a_termcap_g->tg_ents[i];
253 tep->te_flags = tcp->tc_flags;
254 tep->te_off = (ui16_t)a_termcap_g->tg_dat.s_len;
257 if((f & a_TERMCAP_F_TYPE_MASK) == n_TERMCAP_CAPTYPE_BOOL)
259 else if(*v == '\0')
260 tep->te_flags |= a_TERMCAP_F_DISABLED;
261 else if((f & a_TERMCAP_F_TYPE_MASK) == n_TERMCAP_CAPTYPE_NUMERIC){
262 char *eptr;
263 long l = strtol(v, &eptr, 10);
265 if(*eptr != '\0' || l < 0 || UICMP(32, l, >=, UI16_MAX))
266 goto jeinvent;
267 tep->te_off = (ui16_t)l;
268 }else if(!a_termcap__strexp(&a_termcap_g->tg_dat, v))
269 tep->te_flags |= a_TERMCAP_F_DISABLED;
270 #ifdef HAVE_KEY_BINDINGS
271 jlearned:
272 #endif
273 if(options & OPT_D_VV)
274 n_err(_("*termcap*: learned %.*s: %s\n"), (int)kl, ccp,
275 (tep->te_flags & a_TERMCAP_F_DISABLED ? "<disabled>"
276 : (f & a_TERMCAP_F_TYPE_MASK) == n_TERMCAP_CAPTYPE_BOOL ? "true"
277 : v));
279 DBG( if(options & OPT_D_V) n_err("*termcap* parsed: buffer used=%lu\n",
280 (ul_i)a_termcap_g->tg_dat.s_len) );
282 /* Catch some inter-dependencies the user may have triggered */
283 #ifdef HAVE_TERMCAP
284 if(a_termcap_g->tg_ents[n_TERMCAP_CMD_te].te_flags & a_TERMCAP_F_DISABLED)
285 a_termcap_g->tg_ents[n_TERMCAP_CMD_ti].te_flags = a_TERMCAP_F_DISABLED;
286 else if(a_termcap_g->tg_ents[n_TERMCAP_CMD_ti].te_flags &
287 a_TERMCAP_F_DISABLED)
288 a_termcap_g->tg_ents[n_TERMCAP_CMD_te].te_flags = a_TERMCAP_F_DISABLED;
289 #endif
291 j_leave:
292 NYD2_LEAVE;
295 static bool_t
296 a_termcap__strexp(struct n_string *store, char const *ibuf){ /* XXX ASCII */
297 char c;
298 char const *oibuf;
299 size_t olen;
300 NYD2_ENTER;
302 olen = store->s_len;
304 for(oibuf = ibuf; (c = *ibuf) != '\0';){
305 if(c == '\\'){
306 if((c = ibuf[1]) == '\0')
307 goto jebsseq;
309 if(c == 'E'){
310 c = '\033';
311 ibuf += 2;
312 goto jpush;
315 if(octalchar(c)){
316 char c2, c3;
318 if((c2 = ibuf[2]) == '\0' || !octalchar(c2) ||
319 (c3 = ibuf[3]) == '\0' || !octalchar(c3)){
320 n_err(_("*termcap*: invalid octal sequence: %s\n"), oibuf);
321 goto jerr;
323 c -= '0', c2 -= '0', c3 -= '0';
324 c <<= 3, c |= c2;
325 if((ui8_t)c > 0x1F){
326 n_err(_("*termcap*: octal number too large: %s\n"), oibuf);
327 goto jerr;
329 c <<= 3, c |= c3;
330 ibuf += 4;
331 goto jpush;
333 jebsseq:
334 n_err(_("*termcap*: invalid reverse solidus \\ sequence: %s\n"),oibuf);
335 goto jerr;
336 }else if(c == '^'){
337 if((c = ibuf[1]) == '\0'){
338 n_err(_("*termcap*: incomplete ^CNTRL sequence: %s\n"), oibuf);
339 goto jerr;
341 c = upperconv(c) ^ 0x40;
342 if((ui8_t)c > 0x1F && c != 0x7F){ /* ASCII C0: 0..1F, 7F */
343 n_err(_("*termcap*: invalid ^CNTRL sequence: %s\n"), oibuf);
344 goto jerr;
346 ibuf += 2;
347 }else
348 ++ibuf;
350 jpush:
351 store = n_string_push_c(store, c);
354 c = (store->s_len != olen) ? '\1' : '\0';
355 jleave:
356 n_string_push_c(store, '\0');
357 NYD2_LEAVE;
358 return (c != '\0');
359 jerr:
360 store = n_string_trunc(store, olen);
361 c = '\0';
362 goto jleave;
365 static void
366 a_termcap_init_altern(void){
367 /* We silently ignore user _F_DISABLED requests for those entries for which
368 * we have fallback entries, and which we need to ensure proper functioning.
369 * I.e., this allows users to explicitly disable some termcap(5) capability
370 * and enforce usage of the builtin fallback */
371 /* xxx Use table-based approach for fallback strategies */
372 #define a_OK(CMD) a_OOK(&a_termcap_g->tg_ents[CMD])
373 #define a_OOK(TEP) \
374 ((TEP)->te_flags != 0 && !((TEP)->te_flags & a_TERMCAP_F_NOENT))
375 #define a_SET(TEP,CMD,ALT) \
376 (TEP)->te_flags = a_termcap_control[CMD].tc_flags |\
377 ((ALT) ? a_TERMCAP_F_ALTERN : 0)
379 struct a_termcap_ent *tep;
380 NYD2_ENTER;
381 UNUSED(tep);
383 /* For simplicity in the rest of this file null flags of disabled commands,
384 * as we won't check and try to lazy query any command */
385 /* C99 */{
386 size_t i;
388 for(i = n__TERMCAP_CMD_MAX;;){
389 if(i-- == 0)
390 break;
391 if((tep = &a_termcap_g->tg_ents[i])->te_flags & a_TERMCAP_F_DISABLED)
392 tep->te_flags = 0;
396 #ifdef HAVE_TERMCAP
397 /* cl == ho+cd */
398 tep = &a_termcap_g->tg_ents[n_TERMCAP_CMD_cl];
399 if(!a_OOK(tep)){
400 if(a_OK(n_TERMCAP_CMD_cd) && a_OK(n_TERMCAP_CMD_ho))
401 a_SET(tep, n_TERMCAP_CMD_cl, TRU1);
403 #endif
405 #ifdef HAVE_MLE
406 /* ce == ch + [:SPC:] (start column specified by argument) */
407 tep = &a_termcap_g->tg_ents[n_TERMCAP_CMD_ce];
408 if(!a_OOK(tep))
409 a_SET(tep, n_TERMCAP_CMD_ce, TRU1);
411 /* ch == cr[\r] + nd[:\033C:] */
412 tep = &a_termcap_g->tg_ents[n_TERMCAP_CMD_ch];
413 if(!a_OOK(tep))
414 a_SET(tep, n_TERMCAP_CMD_ch, TRU1);
416 /* cr == \r */
417 tep = &a_termcap_g->tg_ents[n_TERMCAP_CMD_cr];
418 if(!a_OOK(tep)){
419 a_SET(tep, n_TERMCAP_CMD_cr, FAL0);
420 tep->te_off = (ui16_t)a_termcap_g->tg_dat.s_len;
421 n_string_push_c(n_string_push_c(&a_termcap_g->tg_dat, '\r'), '\0');
424 /* le == \b */
425 tep = &a_termcap_g->tg_ents[n_TERMCAP_CMD_le];
426 if(!a_OOK(tep)){
427 a_SET(tep, n_TERMCAP_CMD_le, FAL0);
428 tep->te_off = (ui16_t)a_termcap_g->tg_dat.s_len;
429 n_string_push_c(n_string_push_c(&a_termcap_g->tg_dat, '\b'), '\0');
432 /* nd == \033[C (we may not fail, anyway, so use xterm sequence default) */
433 tep = &a_termcap_g->tg_ents[n_TERMCAP_CMD_nd];
434 if(!a_OOK(tep)){
435 a_SET(tep, n_TERMCAP_CMD_nd, FAL0);
436 tep->te_off = (ui16_t)a_termcap_g->tg_dat.s_len;
437 n_string_push_buf(&a_termcap_g->tg_dat, "\033[C", sizeof("\033[C"));
439 #endif /* HAVE_MLE */
441 NYD2_LEAVE;
442 #undef a_OK
443 #undef a_OOK
444 #undef a_SET
447 #ifdef HAVE_TERMCAP
448 # ifdef HAVE_TERMINFO
449 static bool_t
450 a_termcap_load(char const *term){
451 bool_t rv;
452 int err;
453 NYD2_ENTER;
455 if(!(rv = (setupterm(term, STDOUT_FILENO, &err) == OK)))
456 n_err(_("Unknown ${TERM}inal, using only *termcap*: %s\n"), term);
457 NYD2_LEAVE;
458 return rv;
461 static bool_t
462 a_termcap_ent_query(struct a_termcap_ent *tep,
463 char const *cname, ui16_t cflags){
464 bool_t rv;
465 NYD2_ENTER;
466 assert(!(pstate & PS_TERMCAP_DISABLE));
468 if(UNLIKELY(*cname == '\0'))
469 rv = FAL0;
470 else switch((tep->te_flags = cflags) & a_TERMCAP_F_TYPE_MASK){
471 case n_TERMCAP_CAPTYPE_BOOL:
472 tep->te_off = (tigetflag(cname) > 0);
473 rv = TRU1;
474 break;
475 case n_TERMCAP_CAPTYPE_NUMERIC:{
476 int r = tigetnum(cname);
478 if((rv = (r >= 0)))
479 tep->te_off = (ui16_t)MIN(UI16_MAX, r);
480 else
481 tep->te_flags |= a_TERMCAP_F_NOENT;
482 } break;
483 default:
484 case n_TERMCAP_CAPTYPE_STRING:{
485 char *cp;
487 cp = tigetstr(cname);
488 if((rv = (cp != NULL && cp != (char*)-1))){
489 tep->te_off = (ui16_t)a_termcap_g->tg_dat.s_len;
490 n_string_push_buf(&a_termcap_g->tg_dat, cp, strlen(cp) +1);
491 }else
492 tep->te_flags |= a_TERMCAP_F_NOENT;
493 } break;
495 NYD2_LEAVE;
496 return rv;
499 SINLINE bool_t
500 a_termcap_ent_query_tcp(struct a_termcap_ent *tep,
501 struct a_termcap_control const *tcp){
502 assert(!(pstate & PS_TERMCAP_DISABLE));
503 return a_termcap_ent_query(tep, &a_termcap_namedat[tcp->tc_off] + 2,
504 tcp->tc_flags);
507 # else /* HAVE_TERMINFO */
508 static bool_t
509 a_termcap_load(char const *term){
510 bool_t rv;
511 NYD2_ENTER;
513 /* ncurses may return -1 */
514 # ifndef HAVE_TGETENT_NULL_BUF
515 # define a_BUF &a_termcap_g->tg_lib_buf[0]
516 # else
517 # define a_BUF NULL
518 # endif
519 if(!(rv = tgetent(a_BUF, term) > 0))
520 n_err(_("Unknown ${TERM}inal, using only *termcap*: %s\n"), term);
521 # undef a_BUF
522 NYD2_LEAVE;
523 return rv;
526 static bool_t
527 a_termcap_ent_query(struct a_termcap_ent *tep,
528 char const *cname, ui16_t cflags){
529 bool_t rv;
530 NYD2_ENTER;
531 assert(!(pstate & PS_TERMCAP_DISABLE));
533 if(UNLIKELY(*cname == '\0'))
534 rv = FAL0;
535 else switch((tep->te_flags = cflags) & a_TERMCAP_F_TYPE_MASK){
536 case n_TERMCAP_CAPTYPE_BOOL:
537 tep->te_off = (tgetflag(cname) > 0);
538 rv = TRU1;
539 break;
540 case n_TERMCAP_CAPTYPE_NUMERIC:{
541 int r = tgetnum(cname);
543 if((rv = (r >= 0)))
544 tep->te_off = (ui16_t)MIN(UI16_MAX, r);
545 else
546 tep->te_flags |= a_TERMCAP_F_NOENT;
547 } break;
548 default:
549 case n_TERMCAP_CAPTYPE_STRING:{
550 # ifndef HAVE_TGETENT_NULL_BUF
551 char buf_base[a_TERMCAP_ENTRYSIZE_MAX], *buf = &buf_base[0];
552 # define a_BUF &buf
553 # else
554 # define a_BUF NULL
555 # endif
556 char *cp;
558 if((rv = ((cp = tgetstr(cname, a_BUF)) != NULL))){
559 tep->te_off = (ui16_t)a_termcap_g->tg_dat.s_len;
560 n_string_push_buf(&a_termcap_g->tg_dat, cp, strlen(cp) +1);
561 # undef a_BUF
562 }else
563 tep->te_flags |= a_TERMCAP_F_NOENT;
564 } break;
566 NYD2_LEAVE;
567 return rv;
570 SINLINE bool_t
571 a_termcap_ent_query_tcp(struct a_termcap_ent *tep,
572 struct a_termcap_control const *tcp){
573 assert(!(pstate & PS_TERMCAP_DISABLE));
574 return a_termcap_ent_query(tep, &a_termcap_namedat[tcp->tc_off],
575 tcp->tc_flags);
577 # endif /* !HAVE_TERMINFO */
579 static int
580 a_termcap_putc(int c){
581 return putchar(c);
583 #endif /* HAVE_TERMCAP */
585 static si32_t
586 a_termcap_enum_for_name(char const *name, size_t nlen, si32_t min, si32_t max){
587 struct a_termcap_control const *tcp;
588 char const *cnam;
589 si32_t rv;
590 NYD2_ENTER;
592 /* Prefer terminfo(5) names */
593 for(rv = max;;){
594 if(rv-- == min){
595 rv = -1;
596 break;
599 tcp = &a_termcap_control[(ui32_t)rv];
600 cnam = &a_termcap_namedat[tcp->tc_off];
601 if(cnam[2] != '\0'){
602 char const *xcp = cnam + 2;
604 if(nlen == strlen(xcp) && !memcmp(xcp, name, nlen))
605 break;
607 if(nlen == 2 && cnam[0] == name[0] && cnam[1] == name[1])
608 break;
610 NYD2_LEAVE;
611 return rv;
614 FL void
615 n_termcap_init(void){
616 struct str termvar;
617 char const *ccp;
618 NYD_ENTER;
620 assert((options & (OPT_INTERACTIVE | OPT_QUICKRUN_MASK)) == OPT_INTERACTIVE);
622 a_termcap_g = smalloc(sizeof *a_termcap_g);
623 a_termcap_g->tg_ext_ents = NULL;
624 memset(&a_termcap_g->tg_ents[0], 0, sizeof(a_termcap_g->tg_ents));
625 if((ccp = ok_vlook(termcap)) != NULL)
626 termvar.l = strlen(termvar.s = UNCONST(ccp));
627 else
628 /*termvar.s = NULL,*/ termvar.l = 0;
629 n_string_reserve(n_string_creat(&a_termcap_g->tg_dat),
630 ((termvar.l + (256 - 64)) & ~127));
632 if(termvar.l > 0)
633 a_termcap_init_var(&termvar);
635 if(ok_blook(termcap_disable))
636 pstate |= PS_TERMCAP_DISABLE;
637 #ifdef HAVE_TERMCAP
638 else if((ccp = ok_vlook(TERM)) == NULL){
639 n_err(_("Environment variable $TERM is not set, using only *termcap*\n"));
640 pstate |= PS_TERMCAP_DISABLE;
641 }else if(!a_termcap_load(ccp))
642 pstate |= PS_TERMCAP_DISABLE;
643 else{
644 /* Query termcap(5) for each command slot that is not yet set */
645 struct a_termcap_ent *tep;
646 size_t i;
648 for(i = n__TERMCAP_CMD_MAX;;){
649 if(i-- == 0)
650 break;
651 if((tep = &a_termcap_g->tg_ents[i])->te_flags == 0)
652 a_termcap_ent_query_tcp(tep, &a_termcap_control[i]);
655 #endif
657 a_termcap_init_altern();
659 #ifdef HAVE_TERMCAP
660 if(a_termcap_g->tg_ents[n_TERMCAP_CMD_te].te_flags != 0)
661 pstate |= PS_TERMCAP_CA_MODE;
662 #endif
663 n_TERMCAP_RESUME(TRU1);
664 NYD_LEAVE;
667 FL void
668 n_termcap_destroy(void){
669 NYD_ENTER;
670 assert((options & (OPT_INTERACTIVE | OPT_QUICKRUN_MASK)) == OPT_INTERACTIVE);
672 n_TERMCAP_SUSPEND(TRU1);
674 #ifdef HAVE_DEBUG
675 /* C99 */{
676 struct a_termcap_ext_ent *tmp;
678 while((tmp = a_termcap_g->tg_ext_ents) != NULL){
679 a_termcap_g->tg_ext_ents = tmp->tee_next;
680 free(tmp);
683 n_string_gut(&a_termcap_g->tg_dat);
684 free(a_termcap_g);
685 a_termcap_g = NULL;
686 #endif
687 NYD_LEAVE;
690 #ifdef HAVE_TERMCAP
691 FL void
692 n_termcap_resume(bool_t complete){
693 NYD_ENTER;
694 if(!(pstate & PS_TERMCAP_DISABLE) &&
695 (options & (OPT_INTERACTIVE | OPT_QUICKRUN_MASK)) == OPT_INTERACTIVE){
696 if(complete && (pstate & PS_TERMCAP_CA_MODE))
697 n_termcap_cmdx(n_TERMCAP_CMD_ti);
698 n_termcap_cmdx(n_TERMCAP_CMD_ks);
699 fflush(stdout);
701 NYD_LEAVE;
704 FL void
705 n_termcap_suspend(bool_t complete){
706 NYD_ENTER;
707 if(!(pstate & PS_TERMCAP_DISABLE) &&
708 (options & (OPT_INTERACTIVE | OPT_QUICKRUN_MASK)) == OPT_INTERACTIVE){
709 if(complete && (pstate & PS_TERMCAP_CA_MODE))
710 n_termcap_cmdx(n_TERMCAP_CMD_ke);
711 n_termcap_cmdx(n_TERMCAP_CMD_te);
712 fflush(stdout);
714 NYD_LEAVE;
716 #endif /* HAVE_TERMCAP */
718 FL ssize_t
719 n_termcap_cmd(enum n_termcap_cmd cmd, ssize_t a1, ssize_t a2){
720 /* Commands are not lazy queried */
721 struct a_termcap_ent const *tep;
722 enum a_termcap_flags flags;
723 ssize_t rv;
724 NYD2_ENTER;
725 UNUSED(a1);
726 UNUSED(a2);
728 rv = FAL0;
729 if((options & (OPT_INTERACTIVE | OPT_QUICKRUN_MASK)) != OPT_INTERACTIVE)
730 goto jleave;
731 assert(a_termcap_g != NULL);
733 flags = cmd & ~n__TERMCAP_CMD_MASK;
734 cmd &= n__TERMCAP_CMD_MASK;
735 tep = a_termcap_g->tg_ents;
737 if((flags & n_TERMCAP_CMD_FLAG_CA_MODE) && !(pstate & PS_TERMCAP_CA_MODE))
738 rv = TRU1;
739 else if((tep += cmd)->te_flags == 0 || (tep->te_flags & a_TERMCAP_F_NOENT))
740 rv = TRUM1;
741 else if(!(tep->te_flags & a_TERMCAP_F_ALTERN)){
742 char const *cp = a_termcap_g->tg_dat.s_dat + tep->te_off;
744 assert((tep->te_flags & a_TERMCAP_F_TYPE_MASK) ==
745 n_TERMCAP_CAPTYPE_STRING);
747 #ifdef HAVE_TERMCAP
748 if(tep->te_flags & (a_TERMCAP_F_ARG_IDX1 | a_TERMCAP_F_ARG_IDX2)){
749 if(pstate & PS_TERMCAP_DISABLE){
750 if(options & OPT_D_V){
751 char const *cnam = &a_termcap_namedat[
752 a_termcap_control[cmd].tc_off];
754 if(cnam[2] != '\0')
755 cnam += 2;
756 n_err(_("*termcap-disable*d (/$TERM not set/unknown): "
757 "can't perform CAP: %s\n"), cnam);
759 goto jleave;
762 /* Follow Thomas Dickey's advise on pre-va_arg prototypes, add 0s */
763 # ifdef HAVE_TERMINFO
764 if((cp = tparm(cp, a1, a2, 0,0,0,0,0,0,0)) == NULL)
765 goto jleave;
766 # else
767 /* curs_termcap.3:
768 * The \fBtgoto\fP function swaps the order of parameters.
769 * It does this also for calls requiring only a single parameter.
770 * In that case, the first parameter is merely a placeholder. */
771 if(!(tep->te_flags & a_TERMCAP_F_ARG_IDX2)){
772 a2 = a1;
773 a1 = (ui32_t)-1;
775 if((cp = tgoto(cp, (int)a1, (int)a2)) == NULL)
776 goto jleave;
777 # endif
779 #endif
781 for(;;){
782 #ifdef HAVE_TERMCAP
783 if(!(pstate & PS_TERMCAP_DISABLE)){
784 if(tputs(cp, 1, &a_termcap_putc) != OK)
785 break;
786 }else
787 #endif
788 if(fputs(cp, stdout) == EOF)
789 break;
790 if(!(tep->te_flags & a_TERMCAP_F_ARG_CNT) || --a1 <= 0){
791 rv = TRU1;
792 break;
795 goto jflush;
796 }else{
797 switch(cmd){
798 default:
799 rv = TRUM1;
800 break;
802 #ifdef HAVE_TERMCAP
803 case n_TERMCAP_CMD_cl: /* cl = ho + cd */
804 rv = n_termcap_cmdx(n_TERMCAP_CMD_ho);
805 if(rv > 0)
806 rv = n_termcap_cmdx(n_TERMCAP_CMD_cd | flags);
807 break;
808 #endif
810 #ifdef HAVE_MLE
811 case n_TERMCAP_CMD_ce: /* ce == ch + [:SPC:] */
812 if(a1 > 0)
813 --a1;
814 if((rv = n_termcap_cmd(n_TERMCAP_CMD_ch, a1, 0)) > 0){
815 for(a2 = scrnwidth - a1 - 1; a2 > 0; --a2)
816 if(putchar(' ') == EOF){
817 rv = FAL0;
818 break;
820 if(rv && n_termcap_cmd(n_TERMCAP_CMD_ch, a1, -1) != TRU1)
821 rv = FAL0;
823 break;
824 case n_TERMCAP_CMD_ch: /* ch == cr + nd */
825 rv = n_termcap_cmdx(n_TERMCAP_CMD_cr);
826 if(rv > 0 && a1 > 0){
827 rv = n_termcap_cmd(n_TERMCAP_CMD_nd, a1, -1);
829 break;
830 #endif /* HAVE_MLE */
833 jflush:
834 if(flags & n_TERMCAP_CMD_FLAG_FLUSH)
835 fflush(stdout);
836 if(ferror(stdout))
837 rv = FAL0;
840 jleave:
841 NYD2_LEAVE;
842 return rv;
845 FL bool_t
846 n_termcap_query(enum n_termcap_query query, struct n_termcap_value *tvp){
847 /* Queries are lazy queried upon request */
848 struct a_termcap_ent const *tep;
849 bool_t rv;
850 NYD2_ENTER;
852 assert(tvp != NULL);
853 rv = FAL0;
855 if((options & (OPT_INTERACTIVE | OPT_QUICKRUN_MASK)) != OPT_INTERACTIVE)
856 goto jleave;
857 assert(a_termcap_g != NULL);
859 /* Is it a builtin query? */
860 if(query != n__TERMCAP_QUERY_MAX){
861 tep = &a_termcap_g->tg_ents[n__TERMCAP_CMD_MAX + query];
863 if(tep->te_flags == 0
864 #ifdef HAVE_TERMCAP
865 && ((pstate & PS_TERMCAP_DISABLE) ||
866 !a_termcap_ent_query_tcp(UNCONST(tep),
867 &a_termcap_control[n__TERMCAP_CMD_MAX + query]))
868 #endif
870 goto jleave;
871 }else{
872 #ifdef HAVE_TERMCAP
873 size_t nlen;
874 #endif
875 struct a_termcap_ext_ent *teep;
876 char const *ndat = tvp->tv_data.tvd_string;
878 for(teep = a_termcap_g->tg_ext_ents; teep != NULL; teep = teep->tee_next)
879 if(!strcmp(teep->tee_name, ndat)){
880 tep = &teep->tee_super;
881 goto jextok;
884 #ifdef HAVE_TERMCAP
885 if(pstate & PS_TERMCAP_DISABLE)
886 #endif
887 goto jleave;
888 #ifdef HAVE_TERMCAP
889 nlen = strlen(ndat) +1;
890 teep = smalloc(sizeof(*teep) -
891 VFIELD_SIZEOF(struct a_termcap_ext_ent, tee_name) + nlen);
892 tep = &teep->tee_super;
893 teep->tee_next = a_termcap_g->tg_ext_ents;
894 a_termcap_g->tg_ext_ents = teep;
895 memcpy(teep->tee_name, ndat, nlen);
897 if(!a_termcap_ent_query(UNCONST(tep), ndat,
898 n_TERMCAP_CAPTYPE_STRING | a_TERMCAP_F_QUERY))
899 goto jleave;
900 #endif
901 jextok:;
904 if(tep->te_flags & a_TERMCAP_F_NOENT)
905 goto jleave;
907 rv = (tep->te_flags & a_TERMCAP_F_ALTERN) ? TRUM1 : TRU1;
909 switch((tvp->tv_captype = tep->te_flags & a_TERMCAP_F_TYPE_MASK)){
910 case n_TERMCAP_CAPTYPE_BOOL:
911 tvp->tv_data.tvd_bool = (bool_t)tep->te_off;
912 break;
913 case n_TERMCAP_CAPTYPE_NUMERIC:
914 tvp->tv_data.tvd_numeric = (ui32_t)tep->te_off;
915 break;
916 default:
917 case n_TERMCAP_CAPTYPE_STRING:
918 tvp->tv_data.tvd_string = a_termcap_g->tg_dat.s_dat + tep->te_off;
919 break;
921 jleave:
922 NYD2_LEAVE;
923 return rv;
926 #ifdef HAVE_KEY_BINDINGS
927 FL si32_t
928 n_termcap_query_for_name(char const *name, enum n_termcap_captype type){
929 si32_t rv;
930 NYD2_ENTER;
932 if((rv = a_termcap_query_for_name(name, strlen(name))) >= 0){
933 struct a_termcap_control const *tcp = &a_termcap_control[(ui32_t)rv];
935 if(type != n_TERMCAP_CAPTYPE_NONE &&
936 (tcp->tc_flags & a_TERMCAP_F_TYPE_MASK) != type)
937 rv = -2;
938 else
939 rv -= n__TERMCAP_CMD_MAX;
941 NYD2_LEAVE;
942 return rv;
945 FL char const *
946 n_termcap_name_of_query(enum n_termcap_query query){
947 char const *rv;
948 NYD2_ENTER;
950 rv = &a_termcap_namedat[
951 a_termcap_control[n__TERMCAP_CMD_MAX + query].tc_off + 2];
952 NYD2_LEAVE;
953 return rv;
955 #endif /* HAVE_KEY_BINDINGS */
956 #endif /* n_HAVE_TCAP */
958 /* s-it-mode */