Collapse termio/termios/sgtty terminal interface code down to
[mmh] / uip / termsbr.c
1
2 /*
3  * termsbr.c -- termcap support
4  *
5  * This code is Copyright (c) 2002, by the authors of nmh.  See the
6  * COPYRIGHT file in the root directory of the nmh distribution for
7  * complete copyright information.
8  */
9
10 #include <h/mh.h>
11
12 #include <termios.h>
13
14 #ifdef HAVE_TERMCAP_H
15 # include <termcap.h>
16 #endif
17
18 /* <sys/ioctl.h> is need anyway for ioctl()
19 #ifdef GWINSZ_IN_SYS_IOCTL
20 */
21 # include <sys/ioctl.h>
22 /*
23 #endif
24 */
25
26 #ifdef WINSIZE_IN_PTEM
27 # include <sys/stream.h>
28 # include <sys/ptem.h>
29 #endif
30
31 #if BUFSIZ<2048
32 # define TXTSIZ 2048
33 #else
34 # define TXTSIZ BUFSIZ
35 #endif
36
37 /*
38  * These variables are sometimes defined in,
39  * and needed by the termcap library.
40  */
41 #ifdef HAVE_OSPEED
42 # ifdef MUST_DEFINE_OSPEED
43 extern short ospeed;
44 extern char PC;
45 # endif
46 #else
47 short ospeed;
48 char PC;
49 #endif
50
51 static long speedcode;
52
53 static int initLI = 0;
54 static int initCO = 0;
55
56 static int HC = 0;       /* are we on a hardcopy terminal?        */
57 static int LI = 40;      /* number of lines                       */
58 static int CO = 80;      /* number of colums                      */
59 static char *CL = NULL;  /* termcap string to clear screen        */
60 static char *SE = NULL;  /* termcap string to end standout mode   */
61 static char *SO = NULL;  /* termcap string to begin standout mode */
62
63 static char termcap[TXTSIZ];
64
65
66 static void
67 read_termcap(void)
68 {
69     char *bp, *cp;
70     char *term;
71
72 #ifndef TGETENT_ACCEPTS_NULL
73     char termbuf[TXTSIZ];
74 #endif
75
76     struct termios tio;
77     static int inited = 0;
78
79     if (inited++)
80         return;
81
82     if (!(term = getenv ("TERM")))
83         return;
84
85 /*
86  * If possible, we let tgetent allocate its own termcap buffer
87  */
88 #ifdef TGETENT_ACCEPTS_NULL
89     if (tgetent (NULL, term) != TGETENT_SUCCESS)
90         return;
91 #else
92     if (tgetent (termbuf, term) != TGETENT_SUCCESS)
93         return;
94 #endif
95
96     speedcode = cfgetospeed(&tio);
97
98     HC = tgetflag ("hc");
99
100     if (!initCO && (CO = tgetnum ("co")) <= 0)
101         CO = 80;
102     if (!initLI && (LI = tgetnum ("li")) <= 0)
103         LI = 24;
104
105     cp = termcap;
106     CL = tgetstr ("cl", &cp);
107     if ((bp = tgetstr ("pc", &cp)))
108         PC = *bp;
109     if (tgetnum ("sg") <= 0) {
110         SE = tgetstr ("se", &cp);
111         SO = tgetstr ("so", &cp);
112     }
113 }
114
115
116 int
117 sc_width (void)
118 {
119 #ifdef TIOCGWINSZ
120     struct winsize win;
121     int width;
122
123     if (ioctl (fileno (stderr), TIOCGWINSZ, &win) != NOTOK
124             && (width = win.ws_col) > 0) {
125         CO = width;
126         initCO++;
127     } else
128 #endif /* TIOCGWINSZ */
129         read_termcap();
130
131     return CO;
132 }
133
134
135 int
136 sc_length (void)
137 {
138 #ifdef TIOCGWINSZ
139     struct winsize win;
140
141     if (ioctl (fileno (stderr), TIOCGWINSZ, &win) != NOTOK
142             && (LI = win.ws_row) > 0)
143         initLI++;
144     else
145 #endif /* TIOCGWINSZ */
146         read_termcap();
147
148     return LI;
149 }
150
151
152 static int
153 outc (int c)
154 {
155     return putchar(c);
156 }
157
158
159 void
160 clear_screen (void)
161 {
162     read_termcap ();
163
164     if (CL && speedcode)
165         tputs (CL, LI, outc);
166     else {
167         printf ("\f");
168         if (speedcode)
169             printf ("\200");
170     }
171
172     fflush (stdout);
173 }
174
175
176 /*
177  * print in standout mode
178  */
179 int
180 SOprintf (char *fmt, ...)
181 {
182     va_list ap;
183
184     read_termcap ();
185     if (!(SO && SE))
186         return NOTOK;
187
188     tputs (SO, 1, outc);
189
190     va_start(ap, fmt);
191     vprintf (fmt, ap);
192     va_end(ap);
193
194     tputs (SE, 1, outc);
195
196     return OK;
197 }
198
199 /*
200  * Is this a hardcopy terminal?
201  */
202
203 int
204 sc_hardcopy(void)
205 {
206     read_termcap();
207     return HC;
208 }
209