Bump S-nail v14.8.14.ar, 2016-10-20
[s-mailx.git] / termcap.c
blob1ae382839d5473420704e6829d9b73fb942a445f
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.
18 #undef n_FILE
19 #define n_FILE termcap
21 #ifndef HAVE_AMALGAMATION
22 # include "nail.h"
23 #endif
25 EMPTY_FILE()
26 #ifdef HAVE_TERMCAP
27 /* If available, curses.h must be included before term.h! */
28 #ifdef HAVE_TERMCAP_CURSES
29 # include <curses.h>
30 #endif
32 #include <term.h>
34 static char *_termcap_buffer, *_termcap_ti, *_termcap_te;
36 static int _termcap_putc(int c);
38 static int
39 _termcap_putc(int c)
41 return putchar(c);
44 FL void
45 termcap_init(void)
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;
53 NYD_ENTER;
55 /* We don't do nothing unless stdout is a terminal TODO */
56 if (!(options & OPT_TTYOUT))
57 goto jleave;
59 if (!ok_blook(term_ca_mode))
60 goto jleave;
61 if ((cp = env_vlook("TERM", FAL0)) == NULL)
62 goto jleave;
64 if (!tgetent(buf, cp))
65 goto jleave;
66 cpb = cmdbuf;
68 cpti = cpb;
69 if ((cp = tgetstr(UNCONST("ti"), &cpb)) == NULL)
70 goto jleave;
71 cpte = cpb;
72 if ((cp = tgetstr(UNCONST("te"), &cpb)) == NULL)
73 goto jleave;
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);
82 fflush(stdout);
83 jleave:
84 NYD_LEAVE;
87 FL void
88 termcap_destroy(void)
90 NYD_ENTER;
92 if (_termcap_buffer == NULL)
93 goto jleave;
95 tputs(_termcap_te, 1, &_termcap_putc);
97 free(_termcap_buffer);
98 jleave:
99 NYD_LEAVE;
102 #endif /* HAVE_TERMCAP */
104 /* s-it-mode */