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