Fixed trivial compile warnings.
[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 #ifdef notdef_lyndon_posix
38
39 /*
40         XXX No current termcap should need this.  If your compile barfs,
41         email details to lyndon@orthanc.ca.  This code will vanish soon ...
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 #endif /* notdef_lyndon_posix */
58
59 static long speedcode;
60
61 static int initLI = 0;
62 static int initCO = 0;
63
64 static int HC = 0;       /* are we on a hardcopy terminal?        */
65 static int LI = 40;      /* number of lines                       */
66 static int CO = 80;      /* number of colums                      */
67 static char *CL = NULL;  /* termcap string to clear screen        */
68 static char *SE = NULL;  /* termcap string to end standout mode   */
69 static char *SO = NULL;  /* termcap string to begin standout mode */
70
71 static char termcap[TXTSIZ];
72
73
74 static void
75 read_termcap(void)
76 {
77     char *bp, *cp;
78     char *term;
79
80 #ifndef TGETENT_ACCEPTS_NULL
81     char termbuf[TXTSIZ];
82 #endif
83
84     struct termios tio;
85     static int inited = 0;
86
87     if (inited++)
88         return;
89
90     if (!(term = getenv ("TERM")))
91         return;
92
93 /*
94  * If possible, we let tgetent allocate its own termcap buffer
95  */
96 #ifdef TGETENT_ACCEPTS_NULL
97     if (tgetent (NULL, term) != TGETENT_SUCCESS)
98         return;
99 #else
100     if (tgetent (termbuf, term) != TGETENT_SUCCESS)
101         return;
102 #endif
103
104     speedcode = cfgetospeed(&tio);
105
106     HC = tgetflag ("hc");
107
108     if (!initCO && (CO = tgetnum ("co")) <= 0)
109         CO = 80;
110     if (!initLI && (LI = tgetnum ("li")) <= 0)
111         LI = 24;
112
113     cp = termcap;
114     CL = tgetstr ("cl", &cp);
115     if ((bp = tgetstr ("pc", &cp)))
116         PC = *bp;
117     if (tgetnum ("sg") <= 0) {
118         SE = tgetstr ("se", &cp);
119         SO = tgetstr ("so", &cp);
120     }
121 }
122
123
124 int
125 sc_width (void)
126 {
127 #ifdef TIOCGWINSZ
128     struct winsize win;
129     int width;
130
131     if (ioctl (fileno (stderr), TIOCGWINSZ, &win) != NOTOK
132             && (width = win.ws_col) > 0) {
133         CO = width;
134         initCO++;
135     } else
136 #endif /* TIOCGWINSZ */
137         read_termcap();
138
139     return CO;
140 }
141
142
143 int
144 sc_length (void)
145 {
146 #ifdef TIOCGWINSZ
147     struct winsize win;
148
149     if (ioctl (fileno (stderr), TIOCGWINSZ, &win) != NOTOK
150             && (LI = win.ws_row) > 0)
151         initLI++;
152     else
153 #endif /* TIOCGWINSZ */
154         read_termcap();
155
156     return LI;
157 }
158
159
160 static int
161 outc (int c)
162 {
163     return putchar(c);
164 }
165
166
167 void
168 clear_screen (void)
169 {
170     read_termcap ();
171
172     if (CL && speedcode)
173         tputs (CL, LI, outc);
174     else {
175         printf ("\f");
176         if (speedcode)
177             printf ("\200");
178     }
179
180     fflush (stdout);
181 }
182
183
184 /*
185  * print in standout mode
186  */
187 int
188 SOprintf (char *fmt, ...)
189 {
190     va_list ap;
191
192     read_termcap ();
193     if (!(SO && SE))
194         return NOTOK;
195
196     tputs (SO, 1, outc);
197
198     va_start(ap, fmt);
199     vprintf (fmt, ap);
200     va_end(ap);
201
202     tputs (SE, 1, outc);
203
204     return OK;
205 }
206
207 /*
208  * Is this a hardcopy terminal?
209  */
210
211 int
212 sc_hardcopy(void)
213 {
214     read_termcap();
215     return HC;
216 }
217