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