nail.1: tweak text of -a
[s-mailx.git] / termcap.c
blob6bfaf9859ca2e392aa4de7d865d67467db2cae0b
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[n_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 == n_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 n_VFIELD_SIZEOF(struct a_termcap_ext_ent, tee_name) +
228 kl +1);
229 teep->tee_next = a_termcap_g->tg_ext_ents;
230 a_termcap_g->tg_ext_ents = teep;
231 memcpy(teep->tee_name, ccp, kl);
232 teep->tee_name[kl] = '\0';
234 tep = &teep->tee_super;
235 tep->te_flags = n_TERMCAP_CAPTYPE_STRING | a_TERMCAP_F_QUERY;
236 tep->te_off = (ui16_t)a_termcap_g->tg_dat.s_len;
237 if(!a_termcap__strexp(&a_termcap_g->tg_dat, v))
238 tep->te_flags |= a_TERMCAP_F_DISABLED;
239 goto jlearned;
240 }else
241 #endif /* HAVE_KEY_BINDINGS */
242 if(options & OPT_D_V)
243 n_err(_("*termcap*: unknown capability: %s\n"), ccp);
244 continue;
246 i = (size_t)tci;
248 tcp = &a_termcap_control[i];
249 if((tcp->tc_flags & a_TERMCAP_F_TYPE_MASK) != f){
250 n_err(_("*termcap*: entry type mismatch: %s\n"), ccp);
251 break;
253 tep = &a_termcap_g->tg_ents[i];
254 tep->te_flags = tcp->tc_flags;
255 tep->te_off = (ui16_t)a_termcap_g->tg_dat.s_len;
258 if((f & a_TERMCAP_F_TYPE_MASK) == n_TERMCAP_CAPTYPE_BOOL)
260 else if(*v == '\0')
261 tep->te_flags |= a_TERMCAP_F_DISABLED;
262 else if((f & a_TERMCAP_F_TYPE_MASK) == n_TERMCAP_CAPTYPE_NUMERIC){
263 char *eptr;
264 long l = strtol(v, &eptr, 10);
266 if(*eptr != '\0' || l < 0 || UICMP(32, l, >=, UI16_MAX))
267 goto jeinvent;
268 tep->te_off = (ui16_t)l;
269 }else if(!a_termcap__strexp(&a_termcap_g->tg_dat, v))
270 tep->te_flags |= a_TERMCAP_F_DISABLED;
271 #ifdef HAVE_KEY_BINDINGS
272 jlearned:
273 #endif
274 if(options & OPT_D_VV)
275 n_err(_("*termcap*: learned %.*s: %s\n"), (int)kl, ccp,
276 (tep->te_flags & a_TERMCAP_F_DISABLED ? "<disabled>"
277 : (f & a_TERMCAP_F_TYPE_MASK) == n_TERMCAP_CAPTYPE_BOOL ? "true"
278 : v));
280 DBG( if(options & OPT_D_V) n_err("*termcap* parsed: buffer used=%lu\n",
281 (ul_i)a_termcap_g->tg_dat.s_len) );
283 /* Catch some inter-dependencies the user may have triggered */
284 #ifdef HAVE_TERMCAP
285 if(a_termcap_g->tg_ents[n_TERMCAP_CMD_te].te_flags & a_TERMCAP_F_DISABLED)
286 a_termcap_g->tg_ents[n_TERMCAP_CMD_ti].te_flags = a_TERMCAP_F_DISABLED;
287 else if(a_termcap_g->tg_ents[n_TERMCAP_CMD_ti].te_flags &
288 a_TERMCAP_F_DISABLED)
289 a_termcap_g->tg_ents[n_TERMCAP_CMD_te].te_flags = a_TERMCAP_F_DISABLED;
290 #endif
292 j_leave:
293 NYD2_LEAVE;
296 static bool_t
297 a_termcap__strexp(struct n_string *store, char const *ibuf){ /* XXX ASCII */
298 char c;
299 char const *oibuf;
300 size_t olen;
301 NYD2_ENTER;
303 olen = store->s_len;
305 for(oibuf = ibuf; (c = *ibuf) != '\0';){
306 if(c == '\\'){
307 if((c = ibuf[1]) == '\0')
308 goto jebsseq;
310 if(c == 'E'){
311 c = '\033';
312 ibuf += 2;
313 goto jpush;
316 if(octalchar(c)){
317 char c2, c3;
319 if((c2 = ibuf[2]) == '\0' || !octalchar(c2) ||
320 (c3 = ibuf[3]) == '\0' || !octalchar(c3)){
321 n_err(_("*termcap*: invalid octal sequence: %s\n"), oibuf);
322 goto jerr;
324 c -= '0', c2 -= '0', c3 -= '0';
325 c <<= 3, c |= c2;
326 if((ui8_t)c > 0x1F){
327 n_err(_("*termcap*: octal number too large: %s\n"), oibuf);
328 goto jerr;
330 c <<= 3, c |= c3;
331 ibuf += 4;
332 goto jpush;
334 jebsseq:
335 n_err(_("*termcap*: invalid reverse solidus \\ sequence: %s\n"),oibuf);
336 goto jerr;
337 }else if(c == '^'){
338 if((c = ibuf[1]) == '\0'){
339 n_err(_("*termcap*: incomplete ^CNTRL sequence: %s\n"), oibuf);
340 goto jerr;
342 c = upperconv(c) ^ 0x40;
343 if((ui8_t)c > 0x1F && c != 0x7F){ /* ASCII C0: 0..1F, 7F */
344 n_err(_("*termcap*: invalid ^CNTRL sequence: %s\n"), oibuf);
345 goto jerr;
347 ibuf += 2;
348 }else
349 ++ibuf;
351 jpush:
352 store = n_string_push_c(store, c);
355 c = (store->s_len != olen) ? '\1' : '\0';
356 jleave:
357 n_string_push_c(store, '\0');
358 NYD2_LEAVE;
359 return (c != '\0');
360 jerr:
361 store = n_string_trunc(store, olen);
362 c = '\0';
363 goto jleave;
366 static void
367 a_termcap_init_altern(void){
368 /* We silently ignore user _F_DISABLED requests for those entries for which
369 * we have fallback entries, and which we need to ensure proper functioning.
370 * I.e., this allows users to explicitly disable some termcap(5) capability
371 * and enforce usage of the builtin fallback */
372 /* xxx Use table-based approach for fallback strategies */
373 #define a_OK(CMD) a_OOK(&a_termcap_g->tg_ents[CMD])
374 #define a_OOK(TEP) \
375 ((TEP)->te_flags != 0 && !((TEP)->te_flags & a_TERMCAP_F_NOENT))
376 #define a_SET(TEP,CMD,ALT) \
377 (TEP)->te_flags = a_termcap_control[CMD].tc_flags |\
378 ((ALT) ? a_TERMCAP_F_ALTERN : 0)
380 struct a_termcap_ent *tep;
381 NYD2_ENTER;
382 n_UNUSED(tep);
384 /* For simplicity in the rest of this file null flags of disabled commands,
385 * as we won't check and try to lazy query any command */
386 /* C99 */{
387 size_t i;
389 for(i = n__TERMCAP_CMD_MAX;;){
390 if(i-- == 0)
391 break;
392 if((tep = &a_termcap_g->tg_ents[i])->te_flags & a_TERMCAP_F_DISABLED)
393 tep->te_flags = 0;
397 #ifdef HAVE_TERMCAP
398 /* cl == ho+cd */
399 tep = &a_termcap_g->tg_ents[n_TERMCAP_CMD_cl];
400 if(!a_OOK(tep)){
401 if(a_OK(n_TERMCAP_CMD_cd) && a_OK(n_TERMCAP_CMD_ho))
402 a_SET(tep, n_TERMCAP_CMD_cl, TRU1);
404 #endif
406 #ifdef HAVE_MLE
407 /* ce == ch + [:SPC:] (start column specified by argument) */
408 tep = &a_termcap_g->tg_ents[n_TERMCAP_CMD_ce];
409 if(!a_OOK(tep))
410 a_SET(tep, n_TERMCAP_CMD_ce, TRU1);
412 /* ch == cr[\r] + nd[:\033C:] */
413 tep = &a_termcap_g->tg_ents[n_TERMCAP_CMD_ch];
414 if(!a_OOK(tep))
415 a_SET(tep, n_TERMCAP_CMD_ch, TRU1);
417 /* cr == \r */
418 tep = &a_termcap_g->tg_ents[n_TERMCAP_CMD_cr];
419 if(!a_OOK(tep)){
420 a_SET(tep, n_TERMCAP_CMD_cr, FAL0);
421 tep->te_off = (ui16_t)a_termcap_g->tg_dat.s_len;
422 n_string_push_c(n_string_push_c(&a_termcap_g->tg_dat, '\r'), '\0');
425 /* le == \b */
426 tep = &a_termcap_g->tg_ents[n_TERMCAP_CMD_le];
427 if(!a_OOK(tep)){
428 a_SET(tep, n_TERMCAP_CMD_le, FAL0);
429 tep->te_off = (ui16_t)a_termcap_g->tg_dat.s_len;
430 n_string_push_c(n_string_push_c(&a_termcap_g->tg_dat, '\b'), '\0');
433 /* nd == \033[C (we may not fail, anyway, so use xterm sequence default) */
434 tep = &a_termcap_g->tg_ents[n_TERMCAP_CMD_nd];
435 if(!a_OOK(tep)){
436 a_SET(tep, n_TERMCAP_CMD_nd, FAL0);
437 tep->te_off = (ui16_t)a_termcap_g->tg_dat.s_len;
438 n_string_push_buf(&a_termcap_g->tg_dat, "\033[C", sizeof("\033[C"));
440 #endif /* HAVE_MLE */
442 NYD2_LEAVE;
443 #undef a_OK
444 #undef a_OOK
445 #undef a_SET
448 #ifdef HAVE_TERMCAP
449 # ifdef HAVE_TERMINFO
450 static bool_t
451 a_termcap_load(char const *term){
452 bool_t rv;
453 int err;
454 NYD2_ENTER;
456 if(!(rv = (setupterm(term, STDOUT_FILENO, &err) == OK)))
457 n_err(_("Unknown ${TERM}inal, using only *termcap*: %s\n"), term);
458 NYD2_LEAVE;
459 return rv;
462 static bool_t
463 a_termcap_ent_query(struct a_termcap_ent *tep,
464 char const *cname, ui16_t cflags){
465 bool_t rv;
466 NYD2_ENTER;
467 assert(!(pstate & PS_TERMCAP_DISABLE));
469 if(n_UNLIKELY(*cname == '\0'))
470 rv = FAL0;
471 else switch((tep->te_flags = cflags) & a_TERMCAP_F_TYPE_MASK){
472 case n_TERMCAP_CAPTYPE_BOOL:
473 tep->te_off = (tigetflag(cname) > 0);
474 rv = TRU1;
475 break;
476 case n_TERMCAP_CAPTYPE_NUMERIC:{
477 int r = tigetnum(cname);
479 if((rv = (r >= 0)))
480 tep->te_off = (ui16_t)n_MIN(UI16_MAX, r);
481 else
482 tep->te_flags |= a_TERMCAP_F_NOENT;
483 } break;
484 default:
485 case n_TERMCAP_CAPTYPE_STRING:{
486 char *cp;
488 cp = tigetstr(cname);
489 if((rv = (cp != NULL && cp != (char*)-1))){
490 tep->te_off = (ui16_t)a_termcap_g->tg_dat.s_len;
491 n_string_push_buf(&a_termcap_g->tg_dat, cp, strlen(cp) +1);
492 }else
493 tep->te_flags |= a_TERMCAP_F_NOENT;
494 } break;
496 NYD2_LEAVE;
497 return rv;
500 SINLINE bool_t
501 a_termcap_ent_query_tcp(struct a_termcap_ent *tep,
502 struct a_termcap_control const *tcp){
503 assert(!(pstate & PS_TERMCAP_DISABLE));
504 return a_termcap_ent_query(tep, &a_termcap_namedat[tcp->tc_off] + 2,
505 tcp->tc_flags);
508 # else /* HAVE_TERMINFO */
509 static bool_t
510 a_termcap_load(char const *term){
511 bool_t rv;
512 NYD2_ENTER;
514 /* ncurses may return -1 */
515 # ifndef HAVE_TGETENT_NULL_BUF
516 # define a_BUF &a_termcap_g->tg_lib_buf[0]
517 # else
518 # define a_BUF NULL
519 # endif
520 if(!(rv = tgetent(a_BUF, term) > 0))
521 n_err(_("Unknown ${TERM}inal, using only *termcap*: %s\n"), term);
522 # undef a_BUF
523 NYD2_LEAVE;
524 return rv;
527 static bool_t
528 a_termcap_ent_query(struct a_termcap_ent *tep,
529 char const *cname, ui16_t cflags){
530 bool_t rv;
531 NYD2_ENTER;
532 assert(!(pstate & PS_TERMCAP_DISABLE));
534 if(n_UNLIKELY(*cname == '\0'))
535 rv = FAL0;
536 else switch((tep->te_flags = cflags) & a_TERMCAP_F_TYPE_MASK){
537 case n_TERMCAP_CAPTYPE_BOOL:
538 tep->te_off = (tgetflag(cname) > 0);
539 rv = TRU1;
540 break;
541 case n_TERMCAP_CAPTYPE_NUMERIC:{
542 int r = tgetnum(cname);
544 if((rv = (r >= 0)))
545 tep->te_off = (ui16_t)n_MIN(UI16_MAX, r);
546 else
547 tep->te_flags |= a_TERMCAP_F_NOENT;
548 } break;
549 default:
550 case n_TERMCAP_CAPTYPE_STRING:{
551 # ifndef HAVE_TGETENT_NULL_BUF
552 char buf_base[a_TERMCAP_ENTRYSIZE_MAX], *buf = &buf_base[0];
553 # define a_BUF &buf
554 # else
555 # define a_BUF NULL
556 # endif
557 char *cp;
559 if((rv = ((cp = tgetstr(cname, a_BUF)) != NULL))){
560 tep->te_off = (ui16_t)a_termcap_g->tg_dat.s_len;
561 n_string_push_buf(&a_termcap_g->tg_dat, cp, strlen(cp) +1);
562 # undef a_BUF
563 }else
564 tep->te_flags |= a_TERMCAP_F_NOENT;
565 } break;
567 NYD2_LEAVE;
568 return rv;
571 SINLINE bool_t
572 a_termcap_ent_query_tcp(struct a_termcap_ent *tep,
573 struct a_termcap_control const *tcp){
574 assert(!(pstate & PS_TERMCAP_DISABLE));
575 return a_termcap_ent_query(tep, &a_termcap_namedat[tcp->tc_off],
576 tcp->tc_flags);
578 # endif /* !HAVE_TERMINFO */
580 static int
581 a_termcap_putc(int c){
582 return putchar(c);
584 #endif /* HAVE_TERMCAP */
586 static si32_t
587 a_termcap_enum_for_name(char const *name, size_t nlen, si32_t min, si32_t max){
588 struct a_termcap_control const *tcp;
589 char const *cnam;
590 si32_t rv;
591 NYD2_ENTER;
593 /* Prefer terminfo(5) names */
594 for(rv = max;;){
595 if(rv-- == min){
596 rv = -1;
597 break;
600 tcp = &a_termcap_control[(ui32_t)rv];
601 cnam = &a_termcap_namedat[tcp->tc_off];
602 if(cnam[2] != '\0'){
603 char const *xcp = cnam + 2;
605 if(nlen == strlen(xcp) && !memcmp(xcp, name, nlen))
606 break;
608 if(nlen == 2 && cnam[0] == name[0] && cnam[1] == name[1])
609 break;
611 NYD2_LEAVE;
612 return rv;
615 FL void
616 n_termcap_init(void){
617 struct str termvar;
618 char const *ccp;
619 NYD_ENTER;
621 assert((options & (OPT_INTERACTIVE | OPT_QUICKRUN_MASK)) == OPT_INTERACTIVE);
623 a_termcap_g = smalloc(sizeof *a_termcap_g);
624 a_termcap_g->tg_ext_ents = NULL;
625 memset(&a_termcap_g->tg_ents[0], 0, sizeof(a_termcap_g->tg_ents));
626 if((ccp = ok_vlook(termcap)) != NULL)
627 termvar.l = strlen(termvar.s = n_UNCONST(ccp));
628 else
629 /*termvar.s = NULL,*/ termvar.l = 0;
630 n_string_reserve(n_string_creat(&a_termcap_g->tg_dat),
631 ((termvar.l + (256 - 64)) & ~127));
633 if(termvar.l > 0)
634 a_termcap_init_var(&termvar);
636 if(ok_blook(termcap_disable))
637 pstate |= PS_TERMCAP_DISABLE;
638 #ifdef HAVE_TERMCAP
639 else if((ccp = ok_vlook(TERM)) == NULL){
640 n_err(_("Environment variable $TERM is not set, using only *termcap*\n"));
641 pstate |= PS_TERMCAP_DISABLE;
642 }else if(!a_termcap_load(ccp))
643 pstate |= PS_TERMCAP_DISABLE;
644 else{
645 /* Query termcap(5) for each command slot that is not yet set */
646 struct a_termcap_ent *tep;
647 size_t i;
649 for(i = n__TERMCAP_CMD_MAX;;){
650 if(i-- == 0)
651 break;
652 if((tep = &a_termcap_g->tg_ents[i])->te_flags == 0)
653 a_termcap_ent_query_tcp(tep, &a_termcap_control[i]);
656 #endif
658 a_termcap_init_altern();
660 #ifdef HAVE_TERMCAP
661 if(a_termcap_g->tg_ents[n_TERMCAP_CMD_te].te_flags != 0)
662 pstate |= PS_TERMCAP_CA_MODE;
663 #endif
664 n_TERMCAP_RESUME(TRU1);
665 NYD_LEAVE;
668 FL void
669 n_termcap_destroy(void){
670 NYD_ENTER;
671 assert((options & (OPT_INTERACTIVE | OPT_QUICKRUN_MASK)) == OPT_INTERACTIVE);
673 n_TERMCAP_SUSPEND(TRU1);
675 #ifdef HAVE_DEBUG
676 /* C99 */{
677 struct a_termcap_ext_ent *tmp;
679 while((tmp = a_termcap_g->tg_ext_ents) != NULL){
680 a_termcap_g->tg_ext_ents = tmp->tee_next;
681 free(tmp);
684 n_string_gut(&a_termcap_g->tg_dat);
685 free(a_termcap_g);
686 a_termcap_g = NULL;
687 #endif
688 NYD_LEAVE;
691 #ifdef HAVE_TERMCAP
692 FL void
693 n_termcap_resume(bool_t complete){
694 NYD_ENTER;
695 if(!(pstate & PS_TERMCAP_DISABLE) &&
696 (options & (OPT_INTERACTIVE | OPT_QUICKRUN_MASK)) == OPT_INTERACTIVE){
697 if(complete && (pstate & PS_TERMCAP_CA_MODE))
698 n_termcap_cmdx(n_TERMCAP_CMD_ti);
699 n_termcap_cmdx(n_TERMCAP_CMD_ks);
700 fflush(stdout);
702 NYD_LEAVE;
705 FL void
706 n_termcap_suspend(bool_t complete){
707 NYD_ENTER;
708 if(!(pstate & PS_TERMCAP_DISABLE) &&
709 (options & (OPT_INTERACTIVE | OPT_QUICKRUN_MASK)) == OPT_INTERACTIVE){
710 n_termcap_cmdx(n_TERMCAP_CMD_ke);
711 if(complete && (pstate & PS_TERMCAP_CA_MODE))
712 n_termcap_cmdx(n_TERMCAP_CMD_te);
713 fflush(stdout);
715 NYD_LEAVE;
717 #endif /* HAVE_TERMCAP */
719 FL ssize_t
720 n_termcap_cmd(enum n_termcap_cmd cmd, ssize_t a1, ssize_t a2){
721 /* Commands are not lazy queried */
722 struct a_termcap_ent const *tep;
723 enum a_termcap_flags flags;
724 ssize_t rv;
725 NYD2_ENTER;
726 n_UNUSED(a1);
727 n_UNUSED(a2);
729 rv = FAL0;
730 if((options & (OPT_INTERACTIVE | OPT_QUICKRUN_MASK)) != OPT_INTERACTIVE)
731 goto jleave;
732 assert(a_termcap_g != NULL);
734 flags = cmd & ~n__TERMCAP_CMD_MASK;
735 cmd &= n__TERMCAP_CMD_MASK;
736 tep = a_termcap_g->tg_ents;
738 if((flags & n_TERMCAP_CMD_FLAG_CA_MODE) && !(pstate & PS_TERMCAP_CA_MODE))
739 rv = TRU1;
740 else if((tep += cmd)->te_flags == 0 || (tep->te_flags & a_TERMCAP_F_NOENT))
741 rv = TRUM1;
742 else if(!(tep->te_flags & a_TERMCAP_F_ALTERN)){
743 char const *cp = a_termcap_g->tg_dat.s_dat + tep->te_off;
745 assert((tep->te_flags & a_TERMCAP_F_TYPE_MASK) ==
746 n_TERMCAP_CAPTYPE_STRING);
748 #ifdef HAVE_TERMCAP
749 if(tep->te_flags & (a_TERMCAP_F_ARG_IDX1 | a_TERMCAP_F_ARG_IDX2)){
750 if(pstate & PS_TERMCAP_DISABLE){
751 if(options & OPT_D_V){
752 char const *cnam = &a_termcap_namedat[
753 a_termcap_control[cmd].tc_off];
755 if(cnam[2] != '\0')
756 cnam += 2;
757 n_err(_("*termcap-disable*d (/$TERM not set/unknown): "
758 "can't perform CAP: %s\n"), cnam);
760 goto jleave;
763 /* Follow Thomas Dickey's advise on pre-va_arg prototypes, add 0s */
764 # ifdef HAVE_TERMINFO
765 if((cp = tparm(cp, a1, a2, 0,0,0,0,0,0,0)) == NULL)
766 goto jleave;
767 # else
768 /* curs_termcap.3:
769 * The \fBtgoto\fP function swaps the order of parameters.
770 * It does this also for calls requiring only a single parameter.
771 * In that case, the first parameter is merely a placeholder. */
772 if(!(tep->te_flags & a_TERMCAP_F_ARG_IDX2)){
773 a2 = a1;
774 a1 = (ui32_t)-1;
776 if((cp = tgoto(cp, (int)a1, (int)a2)) == NULL)
777 goto jleave;
778 # endif
780 #endif
782 for(;;){
783 #ifdef HAVE_TERMCAP
784 if(!(pstate & PS_TERMCAP_DISABLE)){
785 if(tputs(cp, 1, &a_termcap_putc) != OK)
786 break;
787 }else
788 #endif
789 if(fputs(cp, stdout) == EOF)
790 break;
791 if(!(tep->te_flags & a_TERMCAP_F_ARG_CNT) || --a1 <= 0){
792 rv = TRU1;
793 break;
796 goto jflush;
797 }else{
798 switch(cmd){
799 default:
800 rv = TRUM1;
801 break;
803 #ifdef HAVE_TERMCAP
804 case n_TERMCAP_CMD_cl: /* cl = ho + cd */
805 rv = n_termcap_cmdx(n_TERMCAP_CMD_ho);
806 if(rv > 0)
807 rv = n_termcap_cmdx(n_TERMCAP_CMD_cd | flags);
808 break;
809 #endif
811 #ifdef HAVE_MLE
812 case n_TERMCAP_CMD_ce: /* ce == ch + [:SPC:] */
813 if(a1 > 0)
814 --a1;
815 if((rv = n_termcap_cmd(n_TERMCAP_CMD_ch, a1, 0)) > 0){
816 for(a2 = scrnwidth - a1 - 1; a2 > 0; --a2)
817 if(putchar(' ') == EOF){
818 rv = FAL0;
819 break;
821 if(rv && n_termcap_cmd(n_TERMCAP_CMD_ch, a1, -1) != TRU1)
822 rv = FAL0;
824 break;
825 case n_TERMCAP_CMD_ch: /* ch == cr + nd */
826 rv = n_termcap_cmdx(n_TERMCAP_CMD_cr);
827 if(rv > 0 && a1 > 0){
828 rv = n_termcap_cmd(n_TERMCAP_CMD_nd, a1, -1);
830 break;
831 #endif /* HAVE_MLE */
834 jflush:
835 if(flags & n_TERMCAP_CMD_FLAG_FLUSH)
836 fflush(stdout);
837 if(ferror(stdout))
838 rv = FAL0;
841 jleave:
842 NYD2_LEAVE;
843 return rv;
846 FL bool_t
847 n_termcap_query(enum n_termcap_query query, struct n_termcap_value *tvp){
848 /* Queries are lazy queried upon request */
849 struct a_termcap_ent const *tep;
850 bool_t rv;
851 NYD2_ENTER;
853 assert(tvp != NULL);
854 rv = FAL0;
856 if((options & (OPT_INTERACTIVE | OPT_QUICKRUN_MASK)) != OPT_INTERACTIVE)
857 goto jleave;
858 assert(a_termcap_g != NULL);
860 /* Is it a builtin query? */
861 if(query != n__TERMCAP_QUERY_MAX){
862 tep = &a_termcap_g->tg_ents[n__TERMCAP_CMD_MAX + query];
864 if(tep->te_flags == 0
865 #ifdef HAVE_TERMCAP
866 && ((pstate & PS_TERMCAP_DISABLE) ||
867 !a_termcap_ent_query_tcp(n_UNCONST(tep),
868 &a_termcap_control[n__TERMCAP_CMD_MAX + query]))
869 #endif
871 goto jleave;
872 }else{
873 #ifdef HAVE_TERMCAP
874 size_t nlen;
875 #endif
876 struct a_termcap_ext_ent *teep;
877 char const *ndat = tvp->tv_data.tvd_string;
879 for(teep = a_termcap_g->tg_ext_ents; teep != NULL; teep = teep->tee_next)
880 if(!strcmp(teep->tee_name, ndat)){
881 tep = &teep->tee_super;
882 goto jextok;
885 #ifdef HAVE_TERMCAP
886 if(pstate & PS_TERMCAP_DISABLE)
887 #endif
888 goto jleave;
889 #ifdef HAVE_TERMCAP
890 nlen = strlen(ndat) +1;
891 teep = smalloc(sizeof(*teep) -
892 n_VFIELD_SIZEOF(struct a_termcap_ext_ent, tee_name) + nlen);
893 tep = &teep->tee_super;
894 teep->tee_next = a_termcap_g->tg_ext_ents;
895 a_termcap_g->tg_ext_ents = teep;
896 memcpy(teep->tee_name, ndat, nlen);
898 if(!a_termcap_ent_query(n_UNCONST(tep), ndat,
899 n_TERMCAP_CAPTYPE_STRING | a_TERMCAP_F_QUERY))
900 goto jleave;
901 #endif
902 jextok:;
905 if(tep->te_flags & a_TERMCAP_F_NOENT)
906 goto jleave;
908 rv = (tep->te_flags & a_TERMCAP_F_ALTERN) ? TRUM1 : TRU1;
910 switch((tvp->tv_captype = tep->te_flags & a_TERMCAP_F_TYPE_MASK)){
911 case n_TERMCAP_CAPTYPE_BOOL:
912 tvp->tv_data.tvd_bool = (bool_t)tep->te_off;
913 break;
914 case n_TERMCAP_CAPTYPE_NUMERIC:
915 tvp->tv_data.tvd_numeric = (ui32_t)tep->te_off;
916 break;
917 default:
918 case n_TERMCAP_CAPTYPE_STRING:
919 tvp->tv_data.tvd_string = a_termcap_g->tg_dat.s_dat + tep->te_off;
920 break;
922 jleave:
923 NYD2_LEAVE;
924 return rv;
927 #ifdef HAVE_KEY_BINDINGS
928 FL si32_t
929 n_termcap_query_for_name(char const *name, enum n_termcap_captype type){
930 si32_t rv;
931 NYD2_ENTER;
933 if((rv = a_termcap_query_for_name(name, strlen(name))) >= 0){
934 struct a_termcap_control const *tcp = &a_termcap_control[(ui32_t)rv];
936 if(type != n_TERMCAP_CAPTYPE_NONE &&
937 (tcp->tc_flags & a_TERMCAP_F_TYPE_MASK) != type)
938 rv = -2;
939 else
940 rv -= n__TERMCAP_CMD_MAX;
942 NYD2_LEAVE;
943 return rv;
946 FL char const *
947 n_termcap_name_of_query(enum n_termcap_query query){
948 char const *rv;
949 NYD2_ENTER;
951 rv = &a_termcap_namedat[
952 a_termcap_control[n__TERMCAP_CMD_MAX + query].tc_off + 2];
953 NYD2_LEAVE;
954 return rv;
956 #endif /* HAVE_KEY_BINDINGS */
957 #endif /* n_HAVE_TCAP */
959 /* s-it-mode */