1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Terminal capability interaction. TODO very rudimentary yet
4 * Copyright (c) 2015 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #define n_FILE termcap
21 #ifndef HAVE_AMALGAMATION
27 /* If available, curses.h must be included before term.h! */
28 #ifdef HAVE_TERMCAP_CURSES
34 static char *_termcap_buffer
, *_termcap_ti
, *_termcap_te
;
36 static int _termcap_putc(int c
);
47 /* For newer ncurses based termcap emulation buf will remain unused, for
48 * elder non-emulated ones really weird things will happen if an entry
49 * would require more than 1024 bytes, so don't mind.
50 * Things are more unserious with cmdbuf, but a single termcap command
51 * should really not excess that limit */
52 char buf
[1024 + 512], cmdbuf
[2048], *cpb
, *cpti
, *cpte
, *cp
;
55 /* We don't do nothing unless stdout is a terminal TODO */
56 if (!(options
& OPT_TTYOUT
))
59 if (!ok_blook(term_ca_mode
))
61 if ((cp
= env_vlook("TERM", FAL0
)) == NULL
)
64 if (!tgetent(buf
, cp
))
69 if ((cp
= tgetstr(UNCONST("ti"), &cpb
)) == NULL
)
72 if ((cp
= tgetstr(UNCONST("te"), &cpb
)) == NULL
)
75 _termcap_buffer
= smalloc(PTR2SIZE(cpb
- cmdbuf
));
76 memcpy(_termcap_buffer
, cmdbuf
, PTR2SIZE(cpb
- cmdbuf
));
78 _termcap_ti
= _termcap_buffer
+ PTR2SIZE(cpti
- cmdbuf
);
79 _termcap_te
= _termcap_ti
+ PTR2SIZE(cpte
- cpti
);
81 tputs(_termcap_ti
, 1, &_termcap_putc
);
92 if (_termcap_buffer
== NULL
)
95 tputs(_termcap_te
, 1, &_termcap_putc
);
97 free(_termcap_buffer
);
102 #endif /* HAVE_TERMCAP */