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.
26 #define n_FILE termcap
28 #ifndef HAVE_AMALGAMATION
34 /* If available, curses.h must be included before term.h! */
36 # ifdef HAVE_TERMCAP_CURSES
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! */
65 a_TERMCAP_ENT_MAX
= n__TERMCAP_CMD_MAX
+ n__TERMCAP_QUERY_MAX
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
{
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 */
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
{
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
;
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)];
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
];
127 /* Include the constant mk-tcap-map.pl output */
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);
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
);
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)
169 a_termcap_init_var(struct str
const *termvar
){
170 char *cbp_base
, *cbp
;
175 if(termvar
->l
>= UI16_MAX
){
176 n_err(_("*termcap*: length excesses internal limit, skipping\n"));
180 assert(termvar
->s
[termvar
->l
] == '\0');
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
;
191 /* Separate key/value, if any */
192 if(/* no empties ccp[0] == '\0' ||*/ ccp
[1] == '\0'){
194 n_err(_("*termcap*: invalid entry: %s\n"), ccp
);
197 for(kl
= 2, v
= &ccp
[2];; ++kl
, ++v
){
201 f
= n_TERMCAP_CAPTYPE_BOOL
;
204 f
= n_TERMCAP_CAPTYPE_NUMERIC
;
208 f
= n_TERMCAP_CAPTYPE_STRING
;
214 /* Do we know about this one? */
216 struct a_termcap_control
const *tcp
;
219 tci
= a_termcap_enum_for_name(ccp
, kl
, 0, a_TERMCAP_ENT_MAX
);
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
) +
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
;
241 #endif /* HAVE_KEY_BINDINGS */
242 if(options
& OPT_D_V
)
243 n_err(_("*termcap*: unknown capability: %s\n"), ccp
);
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
);
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
)
261 tep
->te_flags
|= a_TERMCAP_F_DISABLED
;
262 else if((f
& a_TERMCAP_F_TYPE_MASK
) == n_TERMCAP_CAPTYPE_NUMERIC
){
264 long l
= strtol(v
, &eptr
, 10);
266 if(*eptr
!= '\0' || l
< 0 || UICMP(32, l
, >=, UI16_MAX
))
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
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"
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 */
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
;
297 a_termcap__strexp(struct n_string
*store
, char const *ibuf
){ /* XXX ASCII */
305 for(oibuf
= ibuf
; (c
= *ibuf
) != '\0';){
307 if((c
= ibuf
[1]) == '\0')
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
);
324 c
-= '0', c2
-= '0', c3
-= '0';
327 n_err(_("*termcap*: octal number too large: %s\n"), oibuf
);
335 n_err(_("*termcap*: invalid reverse solidus \\ sequence: %s\n"),oibuf
);
338 if((c
= ibuf
[1]) == '\0'){
339 n_err(_("*termcap*: incomplete ^CNTRL sequence: %s\n"), oibuf
);
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
);
352 store
= n_string_push_c(store
, c
);
355 c
= (store
->s_len
!= olen
) ? '\1' : '\0';
357 n_string_push_c(store
, '\0');
361 store
= n_string_trunc(store
, olen
);
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])
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
;
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 */
389 for(i
= n__TERMCAP_CMD_MAX
;;){
392 if((tep
= &a_termcap_g
->tg_ents
[i
])->te_flags
& a_TERMCAP_F_DISABLED
)
399 tep
= &a_termcap_g
->tg_ents
[n_TERMCAP_CMD_cl
];
401 if(a_OK(n_TERMCAP_CMD_cd
) && a_OK(n_TERMCAP_CMD_ho
))
402 a_SET(tep
, n_TERMCAP_CMD_cl
, TRU1
);
407 /* ce == ch + [:SPC:] (start column specified by argument) */
408 tep
= &a_termcap_g
->tg_ents
[n_TERMCAP_CMD_ce
];
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
];
415 a_SET(tep
, n_TERMCAP_CMD_ch
, TRU1
);
418 tep
= &a_termcap_g
->tg_ents
[n_TERMCAP_CMD_cr
];
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');
426 tep
= &a_termcap_g
->tg_ents
[n_TERMCAP_CMD_le
];
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
];
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 */
449 # ifdef HAVE_TERMINFO
451 a_termcap_load(char const *term
){
456 if(!(rv
= (setupterm(term
, STDOUT_FILENO
, &err
) == OK
)))
457 n_err(_("Unknown ${TERM}inal, using only *termcap*: %s\n"), term
);
463 a_termcap_ent_query(struct a_termcap_ent
*tep
,
464 char const *cname
, ui16_t cflags
){
467 assert(!(pstate
& PS_TERMCAP_DISABLE
));
469 if(n_UNLIKELY(*cname
== '\0'))
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);
476 case n_TERMCAP_CAPTYPE_NUMERIC
:{
477 int r
= tigetnum(cname
);
480 tep
->te_off
= (ui16_t
)n_MIN(UI16_MAX
, r
);
482 tep
->te_flags
|= a_TERMCAP_F_NOENT
;
485 case n_TERMCAP_CAPTYPE_STRING
:{
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);
493 tep
->te_flags
|= a_TERMCAP_F_NOENT
;
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,
508 # else /* HAVE_TERMINFO */
510 a_termcap_load(char const *term
){
514 /* ncurses may return -1 */
515 # ifndef HAVE_TGETENT_NULL_BUF
516 # define a_BUF &a_termcap_g->tg_lib_buf[0]
520 if(!(rv
= tgetent(a_BUF
, term
) > 0))
521 n_err(_("Unknown ${TERM}inal, using only *termcap*: %s\n"), term
);
528 a_termcap_ent_query(struct a_termcap_ent
*tep
,
529 char const *cname
, ui16_t cflags
){
532 assert(!(pstate
& PS_TERMCAP_DISABLE
));
534 if(n_UNLIKELY(*cname
== '\0'))
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);
541 case n_TERMCAP_CAPTYPE_NUMERIC
:{
542 int r
= tgetnum(cname
);
545 tep
->te_off
= (ui16_t
)n_MIN(UI16_MAX
, r
);
547 tep
->te_flags
|= a_TERMCAP_F_NOENT
;
550 case n_TERMCAP_CAPTYPE_STRING
:{
551 # ifndef HAVE_TGETENT_NULL_BUF
552 char buf_base
[a_TERMCAP_ENTRYSIZE_MAX
], *buf
= &buf_base
[0];
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);
564 tep
->te_flags
|= a_TERMCAP_F_NOENT
;
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
],
578 # endif /* !HAVE_TERMINFO */
581 a_termcap_putc(int c
){
584 #endif /* HAVE_TERMCAP */
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
;
593 /* Prefer terminfo(5) names */
600 tcp
= &a_termcap_control
[(ui32_t
)rv
];
601 cnam
= &a_termcap_namedat
[tcp
->tc_off
];
603 char const *xcp
= cnam
+ 2;
605 if(nlen
== strlen(xcp
) && !memcmp(xcp
, name
, nlen
))
608 if(nlen
== 2 && cnam
[0] == name
[0] && cnam
[1] == name
[1])
616 n_termcap_init(void){
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
));
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));
634 a_termcap_init_var(&termvar
);
636 if(ok_blook(termcap_disable
))
637 pstate
|= PS_TERMCAP_DISABLE
;
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
;
645 /* Query termcap(5) for each command slot that is not yet set */
646 struct a_termcap_ent
*tep
;
649 for(i
= n__TERMCAP_CMD_MAX
;;){
652 if((tep
= &a_termcap_g
->tg_ents
[i
])->te_flags
== 0)
653 a_termcap_ent_query_tcp(tep
, &a_termcap_control
[i
]);
658 a_termcap_init_altern();
661 if(a_termcap_g
->tg_ents
[n_TERMCAP_CMD_te
].te_flags
!= 0)
662 pstate
|= PS_TERMCAP_CA_MODE
;
664 n_TERMCAP_RESUME(TRU1
);
669 n_termcap_destroy(void){
671 assert((options
& (OPT_INTERACTIVE
| OPT_QUICKRUN_MASK
)) == OPT_INTERACTIVE
);
673 n_TERMCAP_SUSPEND(TRU1
);
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
;
684 n_string_gut(&a_termcap_g
->tg_dat
);
693 n_termcap_resume(bool_t complete
){
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
);
706 n_termcap_suspend(bool_t complete
){
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
);
717 #endif /* HAVE_TERMCAP */
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
;
730 if((options
& (OPT_INTERACTIVE
| OPT_QUICKRUN_MASK
)) != OPT_INTERACTIVE
)
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
))
740 else if((tep
+= cmd
)->te_flags
== 0 || (tep
->te_flags
& a_TERMCAP_F_NOENT
))
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
);
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
];
757 n_err(_("*termcap-disable*d (/$TERM not set/unknown): "
758 "can't perform CAP: %s\n"), cnam
);
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
)
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
)){
776 if((cp
= tgoto(cp
, (int)a1
, (int)a2
)) == NULL
)
784 if(!(pstate
& PS_TERMCAP_DISABLE
)){
785 if(tputs(cp
, 1, &a_termcap_putc
) != OK
)
789 if(fputs(cp
, stdout
) == EOF
)
791 if(!(tep
->te_flags
& a_TERMCAP_F_ARG_CNT
) || --a1
<= 0){
804 case n_TERMCAP_CMD_cl
: /* cl = ho + cd */
805 rv
= n_termcap_cmdx(n_TERMCAP_CMD_ho
);
807 rv
= n_termcap_cmdx(n_TERMCAP_CMD_cd
| flags
);
812 case n_TERMCAP_CMD_ce
: /* ce == ch + [:SPC:] */
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
){
821 if(rv
&& n_termcap_cmd(n_TERMCAP_CMD_ch
, a1
, -1) != TRU1
)
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);
831 #endif /* HAVE_MLE */
835 if(flags
& n_TERMCAP_CMD_FLAG_FLUSH
)
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
;
856 if((options
& (OPT_INTERACTIVE
| OPT_QUICKRUN_MASK
)) != OPT_INTERACTIVE
)
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
866 && ((pstate
& PS_TERMCAP_DISABLE
) ||
867 !a_termcap_ent_query_tcp(n_UNCONST(tep
),
868 &a_termcap_control
[n__TERMCAP_CMD_MAX
+ query
]))
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
;
886 if(pstate
& PS_TERMCAP_DISABLE
)
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
))
905 if(tep
->te_flags
& a_TERMCAP_F_NOENT
)
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
;
914 case n_TERMCAP_CAPTYPE_NUMERIC
:
915 tvp
->tv_data
.tvd_numeric
= (ui32_t
)tep
->te_off
;
918 case n_TERMCAP_CAPTYPE_STRING
:
919 tvp
->tv_data
.tvd_string
= a_termcap_g
->tg_dat
.s_dat
+ tep
->te_off
;
927 #ifdef HAVE_KEY_BINDINGS
929 n_termcap_query_for_name(char const *name
, enum n_termcap_captype type
){
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
)
940 rv
-= n__TERMCAP_CMD_MAX
;
947 n_termcap_name_of_query(enum n_termcap_query query
){
951 rv
= &a_termcap_namedat
[
952 a_termcap_control
[n__TERMCAP_CMD_MAX
+ query
].tc_off
+ 2];
956 #endif /* HAVE_KEY_BINDINGS */
957 #endif /* n_HAVE_TCAP */