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 - 2017 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.
50 /* Unless HAVE_TERMINFO or HAVE_TGETENT_NULL_BUF are defined we will use this
51 * to space the buffer we pass through to tgetent(3).
52 * Since for (such) elder non-emulated terminals really weird things will
53 * happen if an entry would require more than 1024 bytes, don't really mind.
54 * Use a ui16_t for storage */
55 #define a_TERMCAP_ENTRYSIZE_MAX ((2668 + 64) & ~64) /* As of ncurses 6.0 */
57 n_CTA(a_TERMCAP_ENTRYSIZE_MAX
< UI16_MAX
,
58 "Chosen buffer size exceeds datatype capability");
60 /* For simplicity we store commands and queries in single continuous control
61 * and entry structure arrays: to index queries one has to add
62 * n__TERMCAP_CMD_MAX1 first! And don't confound with ENTRYSIZE_MAX! */
64 a_TERMCAP_ENT_MAX1
= n__TERMCAP_CMD_MAX1
+ n__TERMCAP_QUERY_MAX1
69 /* enum n_termcap_captype values stored here.
70 * Note that presence of a type in an a_termcap_ent signals initialization */
71 a_TERMCAP_F_TYPE_MASK
= (1<<4) - 1,
73 a_TERMCAP_F_QUERY
= 1<<4, /* A query rather than a command */
74 a_TERMCAP_F_DISABLED
= 1<<5, /* User explicitly disabled command/query */
75 a_TERMCAP_F_ALTERN
= 1<<6, /* Not available, but has alternative */
76 a_TERMCAP_F_NOENT
= 1<<7, /* Not available */
78 /* _cmd() argument interpretion (_T_STR) */
79 a_TERMCAP_F_ARG_IDX1
= 1<<11, /* Argument 1 used, and is an index */
80 a_TERMCAP_F_ARG_IDX2
= 1<<12,
81 a_TERMCAP_F_ARG_CNT
= 1<<13, /* .., and is a count */
83 a_TERMCAP_F__LAST
= a_TERMCAP_F_ARG_CNT
85 n_CTA((ui32_t
)n__TERMCAP_CAPTYPE_MAX1
<= (ui32_t
)a_TERMCAP_F_TYPE_MASK
,
86 "enum n_termcap_captype exceeds bit range of a_termcap_flags");
88 struct a_termcap_control
{
90 /* Offset base into a_termcap_namedat[], which stores the two-letter
91 * termcap(5) name directly followed by a NUL terminated terminfo(5) name.
92 * A termcap(5) name may consist of two NULs meaning ENOENT,
93 * a terminfo(5) name may be empty for the same purpose */
96 n_CTA(a_TERMCAP_F__LAST
<= UI16_MAX
,
97 "a_termcap_flags exceed storage datatype in a_termcap_control");
101 ui16_t te_off
; /* in a_termcap_g->tg_dat / value for T_BOOL and T_NUM */
103 n_CTA(a_TERMCAP_F__LAST
<= UI16_MAX
,
104 "a_termcap_flags exceed storage datatype in a_termcap_ent");
106 /* Structure for extended queries, which don't have an entry constant in
107 * n_termcap_query (to allow free query/binding of keycodes) */
108 struct a_termcap_ext_ent
{
109 struct a_termcap_ent tee_super
;
111 struct a_termcap_ext_ent
*tee_next
;
112 /* Resolvable termcap(5)/terminfo(5) name as given by user; the actual data
113 * is stored just like for normal queries */
114 char tee_name
[n_VFIELD_SIZE(0)];
118 struct a_termcap_ext_ent
*tg_ext_ents
; /* List of extended queries */
119 struct a_termcap_ent tg_ents
[a_TERMCAP_ENT_MAX1
];
120 struct n_string tg_dat
; /* Storage for resolved caps */
121 # if !defined HAVE_TGETENT_NULL_BUF && !defined HAVE_TERMINFO
122 char tg_lib_buf
[a_TERMCAP_ENTRYSIZE_MAX
];
126 /* Include the constant mk-tcap-map.pl output */
128 n_CTA(sizeof a_termcap_namedat
<= UI16_MAX
,
129 "Termcap command and query name data exceed storage datatype");
130 n_CTA(a_TERMCAP_ENT_MAX1
== n_NELEM(a_termcap_control
),
131 "Control array doesn't match command/query array to be controlled");
133 static struct a_termcap_g
*a_termcap_g
;
135 /* Query *termcap*, parse it and incorporate into a_termcap_g */
136 static void a_termcap_init_var(struct str
const *termvar
);
138 /* Expand ^CNTRL, \[Ee] and \OCT. False for parse error and empty results */
139 static bool_t
a_termcap__strexp(struct n_string
*store
, char const *ibuf
);
141 /* Initialize any _ent for which we have _F_ALTERN and which isn't yet set */
142 static void a_termcap_init_altern(void);
145 /* Setup the library we use to work with term */
146 static bool_t
a_termcap_load(char const *term
);
148 /* Query the capability tcp and fill in tep (upon success) */
149 static bool_t
a_termcap_ent_query(struct a_termcap_ent
*tep
,
150 char const *cname
, ui16_t cflags
);
151 SINLINE bool_t
a_termcap_ent_query_tcp(struct a_termcap_ent
*tep
,
152 struct a_termcap_control
const *tcp
);
154 /* Output PTF for both, termcap(5) and terminfo(5) */
155 static int a_termcap_putc(int c
);
158 /* Get n_termcap_cmd or n_termcap_query constant belonging to (nlen bytes of)
159 * name, or -1 if not found. min and max have to be used to cramp the result */
160 static si32_t
a_termcap_enum_for_name(char const *name
, size_t nlen
,
161 si32_t min
, si32_t max
);
162 #define a_termcap_cmd_for_name(NB,NL) \
163 a_termcap_enum_for_name(NB, NL, 0, n__TERMCAP_CMD_MAX1)
164 #define a_termcap_query_for_name(NB,NL) \
165 a_termcap_enum_for_name(NB, NL, n__TERMCAP_CMD_MAX1, a_TERMCAP_ENT_MAX1)
168 a_termcap_init_var(struct str
const *termvar
){
169 char *cbp_base
, *cbp
;
174 if(termvar
->l
>= UI16_MAX
){
175 n_err(_("*termcap*: length excesses internal limit, skipping\n"));
179 assert(termvar
->s
[termvar
->l
] == '\0');
181 cbp_base
= salloc(i
);
182 memcpy(cbp
= cbp_base
, termvar
->s
, i
);
184 for(; (ccp
= n_strsep(&cbp
, ',', TRU1
)) != NULL
;){
185 struct a_termcap_ent
*tep
;
190 /* Separate key/value, if any */
191 if(/* no empties ccp[0] == '\0' ||*/ ccp
[1] == '\0'){
193 n_err(_("*termcap*: invalid entry: %s\n"), ccp
);
196 for(kl
= 2, v
= &ccp
[2];; ++kl
, ++v
){
200 f
= n_TERMCAP_CAPTYPE_BOOL
;
203 f
= n_TERMCAP_CAPTYPE_NUMERIC
;
207 f
= n_TERMCAP_CAPTYPE_STRING
;
213 /* Do we know about this one? */
215 struct a_termcap_control
const *tcp
;
218 tci
= a_termcap_enum_for_name(ccp
, kl
, 0, a_TERMCAP_ENT_MAX1
);
220 /* For key binding purposes, save any given string */
221 #ifdef HAVE_KEY_BINDINGS
222 if((f
& a_TERMCAP_F_TYPE_MASK
) == n_TERMCAP_CAPTYPE_STRING
){
223 struct a_termcap_ext_ent
*teep
;
225 teep
= smalloc(n_VSTRUCT_SIZEOF(struct a_termcap_ext_ent
,
227 teep
->tee_next
= a_termcap_g
->tg_ext_ents
;
228 a_termcap_g
->tg_ext_ents
= teep
;
229 memcpy(teep
->tee_name
, ccp
, kl
);
230 teep
->tee_name
[kl
] = '\0';
232 tep
= &teep
->tee_super
;
233 tep
->te_flags
= n_TERMCAP_CAPTYPE_STRING
| a_TERMCAP_F_QUERY
;
234 tep
->te_off
= (ui16_t
)a_termcap_g
->tg_dat
.s_len
;
235 if(!a_termcap__strexp(&a_termcap_g
->tg_dat
, v
))
236 tep
->te_flags
|= a_TERMCAP_F_DISABLED
;
239 #endif /* HAVE_KEY_BINDINGS */
240 if(options
& OPT_D_V
)
241 n_err(_("*termcap*: unknown capability: %s\n"), ccp
);
246 tcp
= &a_termcap_control
[i
];
247 if((tcp
->tc_flags
& a_TERMCAP_F_TYPE_MASK
) != f
){
248 n_err(_("*termcap*: entry type mismatch: %s\n"), ccp
);
251 tep
= &a_termcap_g
->tg_ents
[i
];
252 tep
->te_flags
= tcp
->tc_flags
;
253 tep
->te_off
= (ui16_t
)a_termcap_g
->tg_dat
.s_len
;
256 if((f
& a_TERMCAP_F_TYPE_MASK
) == n_TERMCAP_CAPTYPE_BOOL
)
259 tep
->te_flags
|= a_TERMCAP_F_DISABLED
;
260 else if((f
& a_TERMCAP_F_TYPE_MASK
) == n_TERMCAP_CAPTYPE_NUMERIC
){
262 long l
= strtol(v
, &eptr
, 10);
264 if(*eptr
!= '\0' || l
< 0 || UICMP(32, l
, >=, UI16_MAX
))
266 tep
->te_off
= (ui16_t
)l
;
267 }else if(!a_termcap__strexp(&a_termcap_g
->tg_dat
, v
))
268 tep
->te_flags
|= a_TERMCAP_F_DISABLED
;
269 #ifdef HAVE_KEY_BINDINGS
272 if(options
& OPT_D_VV
)
273 n_err(_("*termcap*: learned %.*s: %s\n"), (int)kl
, ccp
,
274 (tep
->te_flags
& a_TERMCAP_F_DISABLED
? "<disabled>"
275 : (f
& a_TERMCAP_F_TYPE_MASK
) == n_TERMCAP_CAPTYPE_BOOL
? "true"
278 DBG( if(options
& OPT_D_V
) n_err("*termcap* parsed: buffer used=%lu\n",
279 (ul_i
)a_termcap_g
->tg_dat
.s_len
) );
281 /* Catch some inter-dependencies the user may have triggered */
283 if(a_termcap_g
->tg_ents
[n_TERMCAP_CMD_te
].te_flags
& a_TERMCAP_F_DISABLED
)
284 a_termcap_g
->tg_ents
[n_TERMCAP_CMD_ti
].te_flags
= a_TERMCAP_F_DISABLED
;
285 else if(a_termcap_g
->tg_ents
[n_TERMCAP_CMD_ti
].te_flags
&
286 a_TERMCAP_F_DISABLED
)
287 a_termcap_g
->tg_ents
[n_TERMCAP_CMD_te
].te_flags
= a_TERMCAP_F_DISABLED
;
295 a_termcap__strexp(struct n_string
*store
, char const *ibuf
){ /* XXX ASCII */
303 for(oibuf
= ibuf
; (c
= *ibuf
) != '\0';){
305 if((c
= ibuf
[1]) == '\0')
317 if((c2
= ibuf
[2]) == '\0' || !octalchar(c2
) ||
318 (c3
= ibuf
[3]) == '\0' || !octalchar(c3
)){
319 n_err(_("*termcap*: invalid octal sequence: %s\n"), oibuf
);
322 c
-= '0', c2
-= '0', c3
-= '0';
325 n_err(_("*termcap*: octal number too large: %s\n"), oibuf
);
333 n_err(_("*termcap*: invalid reverse solidus \\ sequence: %s\n"),oibuf
);
336 if((c
= ibuf
[1]) == '\0'){
337 n_err(_("*termcap*: incomplete ^CNTRL sequence: %s\n"), oibuf
);
340 c
= upperconv(c
) ^ 0x40;
341 if((ui8_t
)c
> 0x1F && c
!= 0x7F){ /* ASCII C0: 0..1F, 7F */
342 n_err(_("*termcap*: invalid ^CNTRL sequence: %s\n"), oibuf
);
350 store
= n_string_push_c(store
, c
);
353 c
= (store
->s_len
!= olen
) ? '\1' : '\0';
355 n_string_push_c(store
, '\0');
359 store
= n_string_trunc(store
, olen
);
365 a_termcap_init_altern(void){
366 /* We silently ignore user _F_DISABLED requests for those entries for which
367 * we have fallback entries, and which we need to ensure proper functioning.
368 * I.e., this allows users to explicitly disable some termcap(5) capability
369 * and enforce usage of the builtin fallback */
370 /* xxx Use table-based approach for fallback strategies */
371 #define a_OK(CMD) a_OOK(&a_termcap_g->tg_ents[CMD])
373 ((TEP)->te_flags != 0 && !((TEP)->te_flags & a_TERMCAP_F_NOENT))
374 #define a_SET(TEP,CMD,ALT) \
375 (TEP)->te_flags = a_termcap_control[CMD].tc_flags |\
376 ((ALT) ? a_TERMCAP_F_ALTERN : 0)
378 struct a_termcap_ent
*tep
;
382 /* For simplicity in the rest of this file null flags of disabled commands,
383 * as we won't check and try to lazy query any command */
387 for(i
= n__TERMCAP_CMD_MAX1
;;){
390 if((tep
= &a_termcap_g
->tg_ents
[i
])->te_flags
& a_TERMCAP_F_DISABLED
)
397 tep
= &a_termcap_g
->tg_ents
[n_TERMCAP_CMD_cl
];
399 if(a_OK(n_TERMCAP_CMD_cd
) && a_OK(n_TERMCAP_CMD_ho
))
400 a_SET(tep
, n_TERMCAP_CMD_cl
, TRU1
);
405 /* ce == ch + [:SPC:] (start column specified by argument) */
406 tep
= &a_termcap_g
->tg_ents
[n_TERMCAP_CMD_ce
];
408 a_SET(tep
, n_TERMCAP_CMD_ce
, TRU1
);
410 /* ch == cr[\r] + nd[:\033C:] */
411 tep
= &a_termcap_g
->tg_ents
[n_TERMCAP_CMD_ch
];
413 a_SET(tep
, n_TERMCAP_CMD_ch
, TRU1
);
416 tep
= &a_termcap_g
->tg_ents
[n_TERMCAP_CMD_cr
];
418 a_SET(tep
, n_TERMCAP_CMD_cr
, FAL0
);
419 tep
->te_off
= (ui16_t
)a_termcap_g
->tg_dat
.s_len
;
420 n_string_push_c(n_string_push_c(&a_termcap_g
->tg_dat
, '\r'), '\0');
424 tep
= &a_termcap_g
->tg_ents
[n_TERMCAP_CMD_le
];
426 a_SET(tep
, n_TERMCAP_CMD_le
, FAL0
);
427 tep
->te_off
= (ui16_t
)a_termcap_g
->tg_dat
.s_len
;
428 n_string_push_c(n_string_push_c(&a_termcap_g
->tg_dat
, '\b'), '\0');
431 /* nd == \033[C (we may not fail, anyway, so use xterm sequence default) */
432 tep
= &a_termcap_g
->tg_ents
[n_TERMCAP_CMD_nd
];
434 a_SET(tep
, n_TERMCAP_CMD_nd
, FAL0
);
435 tep
->te_off
= (ui16_t
)a_termcap_g
->tg_dat
.s_len
;
436 n_string_push_buf(&a_termcap_g
->tg_dat
, "\033[C", sizeof("\033[C"));
438 #endif /* HAVE_MLE */
447 # ifdef HAVE_TERMINFO
449 a_termcap_load(char const *term
){
454 if(!(rv
= (setupterm(term
, fileno(n_tty_fp
), &err
) == OK
)))
455 n_err(_("Unknown ${TERM}inal, using only *termcap*: %s\n"), term
);
461 a_termcap_ent_query(struct a_termcap_ent
*tep
,
462 char const *cname
, ui16_t cflags
){
465 assert(!(pstate
& PS_TERMCAP_DISABLE
));
467 if(n_UNLIKELY(*cname
== '\0'))
469 else switch((tep
->te_flags
= cflags
) & a_TERMCAP_F_TYPE_MASK
){
470 case n_TERMCAP_CAPTYPE_BOOL
:
471 tep
->te_off
= (tigetflag(cname
) > 0);
474 case n_TERMCAP_CAPTYPE_NUMERIC
:{
475 int r
= tigetnum(cname
);
478 tep
->te_off
= (ui16_t
)n_MIN(UI16_MAX
, r
);
480 tep
->te_flags
|= a_TERMCAP_F_NOENT
;
483 case n_TERMCAP_CAPTYPE_STRING
:{
486 cp
= tigetstr(cname
);
487 if((rv
= (cp
!= NULL
&& cp
!= (char*)-1))){
488 tep
->te_off
= (ui16_t
)a_termcap_g
->tg_dat
.s_len
;
489 n_string_push_buf(&a_termcap_g
->tg_dat
, cp
, strlen(cp
) +1);
491 tep
->te_flags
|= a_TERMCAP_F_NOENT
;
499 a_termcap_ent_query_tcp(struct a_termcap_ent
*tep
,
500 struct a_termcap_control
const *tcp
){
501 assert(!(pstate
& PS_TERMCAP_DISABLE
));
502 return a_termcap_ent_query(tep
, &a_termcap_namedat
[tcp
->tc_off
] + 2,
506 # else /* HAVE_TERMINFO */
508 a_termcap_load(char const *term
){
512 /* ncurses may return -1 */
513 # ifndef HAVE_TGETENT_NULL_BUF
514 # define a_BUF &a_termcap_g->tg_lib_buf[0]
518 if(!(rv
= tgetent(a_BUF
, term
) > 0))
519 n_err(_("Unknown ${TERM}inal, using only *termcap*: %s\n"), term
);
526 a_termcap_ent_query(struct a_termcap_ent
*tep
,
527 char const *cname
, ui16_t cflags
){
530 assert(!(pstate
& PS_TERMCAP_DISABLE
));
532 if(n_UNLIKELY(*cname
== '\0'))
534 else switch((tep
->te_flags
= cflags
) & a_TERMCAP_F_TYPE_MASK
){
535 case n_TERMCAP_CAPTYPE_BOOL
:
536 tep
->te_off
= (tgetflag(cname
) > 0);
539 case n_TERMCAP_CAPTYPE_NUMERIC
:{
540 int r
= tgetnum(cname
);
543 tep
->te_off
= (ui16_t
)n_MIN(UI16_MAX
, r
);
545 tep
->te_flags
|= a_TERMCAP_F_NOENT
;
548 case n_TERMCAP_CAPTYPE_STRING
:{
549 # ifndef HAVE_TGETENT_NULL_BUF
550 char buf_base
[a_TERMCAP_ENTRYSIZE_MAX
], *buf
= &buf_base
[0];
557 if((rv
= ((cp
= tgetstr(cname
, a_BUF
)) != NULL
))){
558 tep
->te_off
= (ui16_t
)a_termcap_g
->tg_dat
.s_len
;
559 n_string_push_buf(&a_termcap_g
->tg_dat
, cp
, strlen(cp
) +1);
562 tep
->te_flags
|= a_TERMCAP_F_NOENT
;
570 a_termcap_ent_query_tcp(struct a_termcap_ent
*tep
,
571 struct a_termcap_control
const *tcp
){
572 assert(!(pstate
& PS_TERMCAP_DISABLE
));
573 return a_termcap_ent_query(tep
, &a_termcap_namedat
[tcp
->tc_off
],
576 # endif /* !HAVE_TERMINFO */
579 a_termcap_putc(int c
){
580 return putc(c
, n_tty_fp
);
582 #endif /* HAVE_TERMCAP */
585 a_termcap_enum_for_name(char const *name
, size_t nlen
, si32_t min
, si32_t max
){
586 struct a_termcap_control
const *tcp
;
591 /* Prefer terminfo(5) names */
598 tcp
= &a_termcap_control
[(ui32_t
)rv
];
599 cnam
= &a_termcap_namedat
[tcp
->tc_off
];
601 char const *xcp
= cnam
+ 2;
603 if(nlen
== strlen(xcp
) && !memcmp(xcp
, name
, nlen
))
606 if(nlen
== 2 && cnam
[0] == name
[0] && cnam
[1] == name
[1])
614 n_termcap_init(void){
619 assert((options
& (OPT_INTERACTIVE
| OPT_QUICKRUN_MASK
)) == OPT_INTERACTIVE
);
621 a_termcap_g
= smalloc(sizeof *a_termcap_g
);
622 a_termcap_g
->tg_ext_ents
= NULL
;
623 memset(&a_termcap_g
->tg_ents
[0], 0, sizeof(a_termcap_g
->tg_ents
));
624 if((ccp
= ok_vlook(termcap
)) != NULL
)
625 termvar
.l
= strlen(termvar
.s
= n_UNCONST(ccp
));
627 /*termvar.s = NULL,*/ termvar
.l
= 0;
628 n_string_reserve(n_string_creat(&a_termcap_g
->tg_dat
),
629 ((termvar
.l
+ (256 - 64)) & ~127));
632 a_termcap_init_var(&termvar
);
634 if(ok_blook(termcap_disable
))
635 pstate
|= PS_TERMCAP_DISABLE
;
637 else if((ccp
= ok_vlook(TERM
)) == NULL
){
638 n_err(_("Environment variable $TERM is not set, using only *termcap*\n"));
639 pstate
|= PS_TERMCAP_DISABLE
;
640 }else if(!a_termcap_load(ccp
))
641 pstate
|= PS_TERMCAP_DISABLE
;
643 /* Query termcap(5) for each command slot that is not yet set */
644 struct a_termcap_ent
*tep
;
647 for(i
= n__TERMCAP_CMD_MAX1
;;){
650 if((tep
= &a_termcap_g
->tg_ents
[i
])->te_flags
== 0)
651 a_termcap_ent_query_tcp(tep
, &a_termcap_control
[i
]);
656 a_termcap_init_altern();
659 if(a_termcap_g
->tg_ents
[n_TERMCAP_CMD_te
].te_flags
!= 0)
660 pstate
|= PS_TERMCAP_CA_MODE
;
662 n_TERMCAP_RESUME(TRU1
);
667 n_termcap_destroy(void){
669 assert((options
& (OPT_INTERACTIVE
| OPT_QUICKRUN_MASK
)) == OPT_INTERACTIVE
);
671 n_TERMCAP_SUSPEND(TRU1
);
675 struct a_termcap_ext_ent
*tmp
;
677 while((tmp
= a_termcap_g
->tg_ext_ents
) != NULL
){
678 a_termcap_g
->tg_ext_ents
= tmp
->tee_next
;
682 n_string_gut(&a_termcap_g
->tg_dat
);
691 n_termcap_resume(bool_t complete
){
693 if(!(pstate
& PS_TERMCAP_DISABLE
) &&
694 (options
& (OPT_INTERACTIVE
| OPT_QUICKRUN_MASK
)) == OPT_INTERACTIVE
){
695 if(complete
&& (pstate
& PS_TERMCAP_CA_MODE
))
696 n_termcap_cmdx(n_TERMCAP_CMD_ti
);
697 n_termcap_cmdx(n_TERMCAP_CMD_ks
);
704 n_termcap_suspend(bool_t complete
){
706 if(!(pstate
& PS_TERMCAP_DISABLE
) &&
707 (options
& (OPT_INTERACTIVE
| OPT_QUICKRUN_MASK
)) == OPT_INTERACTIVE
){
708 n_termcap_cmdx(n_TERMCAP_CMD_ke
);
709 if(complete
&& (pstate
& PS_TERMCAP_CA_MODE
))
710 n_termcap_cmdx(n_TERMCAP_CMD_te
);
715 #endif /* HAVE_TERMCAP */
718 n_termcap_cmd(enum n_termcap_cmd cmd
, ssize_t a1
, ssize_t a2
){
719 /* Commands are not lazy queried */
720 struct a_termcap_ent
const *tep
;
721 enum a_termcap_flags flags
;
728 if((options
& (OPT_INTERACTIVE
| OPT_QUICKRUN_MASK
)) != OPT_INTERACTIVE
)
730 assert(a_termcap_g
!= NULL
);
732 flags
= cmd
& ~n__TERMCAP_CMD_MASK
;
733 cmd
&= n__TERMCAP_CMD_MASK
;
734 tep
= a_termcap_g
->tg_ents
;
736 if((flags
& n_TERMCAP_CMD_FLAG_CA_MODE
) && !(pstate
& PS_TERMCAP_CA_MODE
))
738 else if((tep
+= cmd
)->te_flags
== 0 || (tep
->te_flags
& a_TERMCAP_F_NOENT
))
740 else if(!(tep
->te_flags
& a_TERMCAP_F_ALTERN
)){
741 char const *cp
= a_termcap_g
->tg_dat
.s_dat
+ tep
->te_off
;
743 assert((tep
->te_flags
& a_TERMCAP_F_TYPE_MASK
) ==
744 n_TERMCAP_CAPTYPE_STRING
);
747 if(tep
->te_flags
& (a_TERMCAP_F_ARG_IDX1
| a_TERMCAP_F_ARG_IDX2
)){
748 if(pstate
& PS_TERMCAP_DISABLE
){
749 if(options
& OPT_D_V
){
750 char const *cnam
= &a_termcap_namedat
[
751 a_termcap_control
[cmd
].tc_off
];
755 n_err(_("*termcap-disable*d (/$TERM not set/unknown): "
756 "can't perform CAP: %s\n"), cnam
);
761 /* Follow Thomas Dickey's advise on pre-va_arg prototypes, add 0s */
762 # ifdef HAVE_TERMINFO
763 if((cp
= tparm(cp
, a1
, a2
, 0,0,0,0,0,0,0)) == NULL
)
767 * The \fBtgoto\fP function swaps the order of parameters.
768 * It does this also for calls requiring only a single parameter.
769 * In that case, the first parameter is merely a placeholder. */
770 if(!(tep
->te_flags
& a_TERMCAP_F_ARG_IDX2
)){
774 if((cp
= tgoto(cp
, (int)a1
, (int)a2
)) == NULL
)
782 if(!(pstate
& PS_TERMCAP_DISABLE
)){
783 if(tputs(cp
, 1, &a_termcap_putc
) != OK
)
787 if(fputs(cp
, n_tty_fp
) == EOF
)
789 if(!(tep
->te_flags
& a_TERMCAP_F_ARG_CNT
) || --a1
<= 0){
802 case n_TERMCAP_CMD_cl
: /* cl = ho + cd */
803 rv
= n_termcap_cmdx(n_TERMCAP_CMD_ho
);
805 rv
= n_termcap_cmdx(n_TERMCAP_CMD_cd
| flags
);
810 case n_TERMCAP_CMD_ce
: /* ce == ch + [:SPC:] */
813 if((rv
= n_termcap_cmd(n_TERMCAP_CMD_ch
, a1
, 0)) > 0){
814 for(a2
= scrnwidth
- a1
- 1; a2
> 0; --a2
)
815 if(putc(' ', n_tty_fp
) == EOF
){
819 if(rv
&& n_termcap_cmd(n_TERMCAP_CMD_ch
, a1
, -1) != TRU1
)
823 case n_TERMCAP_CMD_ch
: /* ch == cr + nd */
824 rv
= n_termcap_cmdx(n_TERMCAP_CMD_cr
);
825 if(rv
> 0 && a1
> 0){
826 rv
= n_termcap_cmd(n_TERMCAP_CMD_nd
, a1
, -1);
829 #endif /* HAVE_MLE */
833 if(flags
& n_TERMCAP_CMD_FLAG_FLUSH
)
845 n_termcap_query(enum n_termcap_query query
, struct n_termcap_value
*tvp
){
846 /* Queries are lazy queried upon request */
847 struct a_termcap_ent
const *tep
;
854 if((options
& (OPT_INTERACTIVE
| OPT_QUICKRUN_MASK
)) != OPT_INTERACTIVE
)
856 assert(a_termcap_g
!= NULL
);
858 /* Is it a builtin query? */
859 if(query
!= n__TERMCAP_QUERY_MAX1
){
860 tep
= &a_termcap_g
->tg_ents
[n__TERMCAP_CMD_MAX1
+ query
];
862 if(tep
->te_flags
== 0
864 && ((pstate
& PS_TERMCAP_DISABLE
) ||
865 !a_termcap_ent_query_tcp(n_UNCONST(tep
),
866 &a_termcap_control
[n__TERMCAP_CMD_MAX1
+ query
]))
874 struct a_termcap_ext_ent
*teep
;
875 char const *ndat
= tvp
->tv_data
.tvd_string
;
877 for(teep
= a_termcap_g
->tg_ext_ents
; teep
!= NULL
; teep
= teep
->tee_next
)
878 if(!strcmp(teep
->tee_name
, ndat
)){
879 tep
= &teep
->tee_super
;
884 if(pstate
& PS_TERMCAP_DISABLE
)
888 nlen
= strlen(ndat
) +1;
889 teep
= smalloc(n_VSTRUCT_SIZEOF(struct a_termcap_ext_ent
, tee_name
) +
891 tep
= &teep
->tee_super
;
892 teep
->tee_next
= a_termcap_g
->tg_ext_ents
;
893 a_termcap_g
->tg_ext_ents
= teep
;
894 memcpy(teep
->tee_name
, ndat
, nlen
);
896 if(!a_termcap_ent_query(n_UNCONST(tep
), ndat
,
897 n_TERMCAP_CAPTYPE_STRING
| a_TERMCAP_F_QUERY
))
903 if(tep
->te_flags
& a_TERMCAP_F_NOENT
)
906 rv
= (tep
->te_flags
& a_TERMCAP_F_ALTERN
) ? TRUM1
: TRU1
;
908 switch((tvp
->tv_captype
= tep
->te_flags
& a_TERMCAP_F_TYPE_MASK
)){
909 case n_TERMCAP_CAPTYPE_BOOL
:
910 tvp
->tv_data
.tvd_bool
= (bool_t
)tep
->te_off
;
912 case n_TERMCAP_CAPTYPE_NUMERIC
:
913 tvp
->tv_data
.tvd_numeric
= (ui32_t
)tep
->te_off
;
916 case n_TERMCAP_CAPTYPE_STRING
:
917 tvp
->tv_data
.tvd_string
= a_termcap_g
->tg_dat
.s_dat
+ tep
->te_off
;
925 #ifdef HAVE_KEY_BINDINGS
927 n_termcap_query_for_name(char const *name
, enum n_termcap_captype type
){
931 if((rv
= a_termcap_query_for_name(name
, strlen(name
))) >= 0){
932 struct a_termcap_control
const *tcp
= &a_termcap_control
[(ui32_t
)rv
];
934 if(type
!= n_TERMCAP_CAPTYPE_NONE
&&
935 (tcp
->tc_flags
& a_TERMCAP_F_TYPE_MASK
) != type
)
938 rv
-= n__TERMCAP_CMD_MAX1
;
945 n_termcap_name_of_query(enum n_termcap_query query
){
949 rv
= &a_termcap_namedat
[
950 a_termcap_control
[n__TERMCAP_CMD_MAX1
+ query
].tc_off
+ 2];
954 #endif /* HAVE_KEY_BINDINGS */
955 #endif /* n_HAVE_TCAP */