only clean out gs when last window goes
[nvi.git] / cl / cl_bsd.c
blob130f1d618306259400c016aa1ce7cb0fad41a97f
1 /*-
2 * Copyright (c) 1995, 1996
3 * Keith Bostic. All rights reserved.
5 * See the LICENSE file for redistribution information.
6 */
8 #include "config.h"
10 #ifndef lint
11 static const char sccsid[] = "$Id: cl_bsd.c,v 8.30 2000/05/07 19:49:39 skimo Exp $ (Berkeley) $Date: 2000/05/07 19:49:39 $";
12 #endif /* not lint */
14 #include <sys/types.h>
15 #include <sys/queue.h>
16 #include <sys/time.h>
18 #include <bitstring.h>
19 #include <ctype.h>
20 #include <curses.h>
21 #include <signal.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <termios.h>
26 #include <unistd.h>
28 #include "../common/common.h"
29 #include "../vi/vi.h"
30 #include "cl.h"
32 static char *ke; /* Keypad on. */
33 static char *ks; /* Keypad off. */
34 static char *vb; /* Visible bell string. */
37 * HP's support the entire System V curses package except for the tigetstr
38 * and tigetnum functions. Ultrix supports the BSD curses package except
39 * for the idlok function. Cthulu only knows why. Break things up into a
40 * minimal set of functions.
43 #ifndef HAVE_CURSES_WADDNSTR
45 * waddnstr --
47 * PUBLIC: #ifndef HAVE_CURSES_WADDNSTR
48 * PUBLIC: int waddnstr __P((WINDOW*, char *, int));
49 * PUBLIC: #endif
51 int
52 waddnstr(w, s, n)
53 WINDOW *w;
54 char *s;
55 int n;
57 int ch;
59 while (n-- && (ch = *s++))
60 waddch(w, ch);
61 return (OK);
63 #endif
65 #ifndef HAVE_CURSES_BEEP
67 * beep --
69 * PUBLIC: #ifndef HAVE_CURSES_BEEP
70 * PUBLIC: void beep __P((void));
71 * PUBLIC: #endif
73 void
74 beep()
76 (void)write(1, "\007", 1); /* '\a' */
78 #endif /* !HAVE_CURSES_BEEP */
80 #ifndef HAVE_CURSES_FLASH
82 * flash --
83 * Flash the screen.
85 * PUBLIC: #ifndef HAVE_CURSES_FLASH
86 * PUBLIC: void flash __P((void));
87 * PUBLIC: #endif
89 void
90 flash()
92 if (vb != NULL) {
93 (void)tputs(vb, 1, cl_putchar);
94 (void)fflush(stdout);
95 } else
96 beep();
98 #endif /* !HAVE_CURSES_FLASH */
100 #ifndef HAVE_CURSES_IDLOK
102 * idlok --
103 * Turn on/off hardware line insert/delete.
105 * PUBLIC: #ifndef HAVE_CURSES_IDLOK
106 * PUBLIC: void idlok __P((WINDOW *, int));
107 * PUBLIC: #endif
109 void
110 idlok(win, bf)
111 WINDOW *win;
112 int bf;
114 return;
116 #endif /* !HAVE_CURSES_IDLOK */
118 #ifndef HAVE_CURSES_KEYPAD
120 * keypad --
121 * Put the keypad/cursor arrows into or out of application mode.
123 * PUBLIC: #ifndef HAVE_CURSES_KEYPAD
124 * PUBLIC: int keypad __P((void *, int));
125 * PUBLIC: #endif
128 keypad(a, on)
129 void *a;
130 int on;
132 char *p;
134 if ((p = tigetstr(on ? "smkx" : "rmkx")) != (char *)-1) {
135 (void)tputs(p, 0, cl_putchar);
136 (void)fflush(stdout);
138 return (0);
140 #endif /* !HAVE_CURSES_KEYPAD */
142 #ifndef HAVE_CURSES_NEWTERM
144 * newterm --
145 * Create a new curses screen.
147 * PUBLIC: #ifndef HAVE_CURSES_NEWTERM
148 * PUBLIC: void *newterm __P((const char *, FILE *, FILE *));
149 * PUBLIC: #endif
151 void *
152 newterm(a, b, c)
153 const char *a;
154 FILE *b, *c;
156 return (initscr());
158 #endif /* !HAVE_CURSES_NEWTERM */
160 #ifndef HAVE_CURSES_SETUPTERM
162 * setupterm --
163 * Set up terminal.
165 * PUBLIC: #ifndef HAVE_CURSES_SETUPTERM
166 * PUBLIC: void setupterm __P((char *, int, int *));
167 * PUBLIC: #endif
169 void
170 setupterm(ttype, fno, errp)
171 char *ttype;
172 int fno, *errp;
174 static char buf[2048];
175 char *p;
177 if ((*errp = tgetent(buf, ttype)) > 0) {
178 if (ke != NULL)
179 free(ke);
180 ke = ((p = tigetstr("rmkx")) == (char *)-1) ?
181 NULL : strdup(p);
182 if (ks != NULL)
183 free(ks);
184 ks = ((p = tigetstr("smkx")) == (char *)-1) ?
185 NULL : strdup(p);
186 if (vb != NULL)
187 free(vb);
188 vb = ((p = tigetstr("flash")) == (char *)-1) ?
189 NULL : strdup(p);
192 #endif /* !HAVE_CURSES_SETUPTERM */
194 #ifndef HAVE_CURSES_TIGETSTR
195 /* Terminfo-to-termcap translation table. */
196 typedef struct _tl {
197 char *terminfo; /* Terminfo name. */
198 char *termcap; /* Termcap name. */
199 } TL;
200 static const TL list[] = {
201 "cols", "co", /* Terminal columns. */
202 "cup", "cm", /* Cursor up. */
203 "cuu1", "up", /* Cursor up. */
204 "el", "ce", /* Clear to end-of-line. */
205 "flash", "vb", /* Visible bell. */
206 "kcub1", "kl", /* Cursor left. */
207 "kcud1", "kd", /* Cursor down. */
208 "kcuf1", "kr", /* Cursor right. */
209 "kcuu1", "ku", /* Cursor up. */
210 "kdch1", "kD", /* Delete character. */
211 "kdl1", "kL", /* Delete line. */
212 "ked", "kS", /* Delete to end of screen. */
213 "kel", "kE", /* Delete to eol. */
214 "khome", "kh", /* Go to sol. */
215 "kich1", "kI", /* Insert at cursor. */
216 "kil1", "kA", /* Insert line. */
217 "kind", "kF", /* Scroll down. */
218 "kll", "kH", /* Go to eol. */
219 "knp", "kN", /* Page down. */
220 "kpp", "kP", /* Page up. */
221 "kri", "kR", /* Scroll up. */
222 "lines", "li", /* Terminal lines. */
223 "rmcup", "te", /* Terminal end string. */
224 "rmkx", "ke", /* Exit "keypad-transmit" mode. */
225 "rmso", "se", /* Standout end. */
226 "smcup", "ti", /* Terminal initialization string. */
227 "smkx", "ks", /* Enter "keypad-transmit" mode. */
228 "smso", "so", /* Standout begin. */
231 #ifdef _AIX
233 * AIX's implementation for function keys greater than 10 is different and
234 * only goes as far as 36.
236 static const char codes[] = {
237 /* 0-10 */ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ';',
238 /* 11-20 */ '<', '>', '!', '@', '#', '$', '%', '^', '&', '*',
239 /* 21-30 */ '(', ')', '-', '_', '+', ',', ':', '?', '[', ']',
240 /* 31-36 */ '{', '}', '|', '~', '/', '='
243 #else
246 * !!!
247 * Historically, the 4BSD termcap code didn't support functions keys greater
248 * than 9. This was silently enforced -- asking for key k12 would return the
249 * value for k1. We try and get around this by using the tables specified in
250 * the terminfo(TI_ENV) man page from the 3rd Edition SVID. This assumes the
251 * implementors of any System V compatibility code or an extended termcap used
252 * those codes.
254 static const char codes[] = {
255 /* 0-10 */ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ';',
256 /* 11-19 */ '1', '2', '3', '4', '5', '6', '7', '8', '9',
257 /* 20-63 */ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
258 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
259 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
260 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
262 #endif /* _AIX */
265 * lcmp --
266 * list comparison routine for bsearch.
268 static int
269 lcmp(a, b)
270 const void *a, *b;
272 return (strcmp(a, ((TL *)b)->terminfo));
276 * tigetstr --
278 * Vendors put the prototype for tigetstr into random include files, including
279 * <term.h>, which we can't include because it makes other systems unhappy.
280 * Try and work around the problem, since we only care about the return value.
282 * PUBLIC: #ifdef HAVE_CURSES_TIGETSTR
283 * PUBLIC: char *tigetstr();
284 * PUBLIC: #else
285 * PUBLIC: char *tigetstr __P((char *));
286 * PUBLIC: #endif
288 char *
289 tigetstr(name)
290 char *name;
292 static char sbuf[256];
293 TL *tlp;
294 int n;
295 char *p, keyname[3];
297 if ((tlp = bsearch(name,
298 list, sizeof(list) / sizeof(TL), sizeof(TL), lcmp)) == NULL) {
299 #ifdef _AIX
300 if (name[0] == 'k' &&
301 name[1] == 'f' && (n = atoi(name + 2)) <= 36) {
302 keyname[0] = 'k';
303 keyname[1] = codes[n];
304 keyname[2] = '\0';
305 #else
306 if (name[0] == 'k' &&
307 name[1] == 'f' && (n = atoi(name + 2)) <= 63) {
308 keyname[0] = n <= 10 ? 'k' : 'F';
309 keyname[1] = codes[n];
310 keyname[2] = '\0';
311 #endif
312 name = keyname;
314 } else
315 name = tlp->termcap;
317 p = sbuf;
318 #ifdef _AIX
319 return ((p = tgetstr(name, &p)) == NULL ? (char *)-1 : strcpy(sbuf, p));
320 #else
321 return (tgetstr(name, &p) == NULL ? (char *)-1 : sbuf);
322 #endif
326 * tigetnum --
328 * PUBLIC: #ifndef HAVE_CURSES_TIGETSTR
329 * PUBLIC: int tigetnum __P((char *));
330 * PUBLIC: #endif
333 tigetnum(name)
334 char *name;
336 TL *tlp;
337 int val;
339 if ((tlp = bsearch(name,
340 list, sizeof(list) / sizeof(TL), sizeof(TL), lcmp)) != NULL) {
341 name = tlp->termcap;
344 return ((val = tgetnum(name)) == -1 ? -2 : val);
346 #endif /* !HAVE_CURSES_TIGETSTR */