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