Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / nvi / cl / cl_bsd.c
blob647e3c304275b748940dd932885e10d4adc094fb
1 /* $NetBSD: cl_bsd.c,v 1.2 2008/05/20 17:52:10 aymeric Exp $ */
3 /*-
4 * Copyright (c) 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
8 */
10 #include "config.h"
12 #ifndef lint
13 static const char sccsid[] = "Id: cl_bsd.c,v 8.32 2000/12/01 13:56:17 skimo Exp (Berkeley) Date: 2000/12/01 13:56:17";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/time.h>
20 #include <bitstring.h>
21 #include <ctype.h>
22 #include <signal.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <termios.h>
27 #include <unistd.h>
29 #include "../common/common.h"
30 #include "../vi/vi.h"
31 #include "cl.h"
33 static char *ke; /* Keypad on. */
34 static char *ks; /* Keypad off. */
35 static char *vb; /* Visible bell string. */
38 * HP's support the entire System V curses package except for the tigetstr
39 * and tigetnum functions. Ultrix supports the BSD curses package except
40 * for the idlok function. Cthulu only knows why. Break things up into a
41 * minimal set of functions.
44 #ifndef HAVE_CURSES_WADDNSTR
46 * waddnstr --
48 * PUBLIC: #ifndef HAVE_CURSES_WADDNSTR
49 * PUBLIC: int waddnstr __P((WINDOW*, char *, int));
50 * PUBLIC: #endif
52 int
53 waddnstr(w, s, n)
54 WINDOW *w;
55 char *s;
56 int n;
58 int ch;
60 while (n-- && (ch = *s++))
61 waddch(w, ch);
62 return (OK);
64 #endif
66 #ifndef HAVE_CURSES_BEEP
68 * beep --
70 * PUBLIC: #ifndef HAVE_CURSES_BEEP
71 * PUBLIC: void beep __P((void));
72 * PUBLIC: #endif
74 void
75 beep()
77 (void)write(1, "\007", 1); /* '\a' */
79 #endif /* !HAVE_CURSES_BEEP */
81 #ifndef HAVE_CURSES_FLASH
83 * flash --
84 * Flash the screen.
86 * PUBLIC: #ifndef HAVE_CURSES_FLASH
87 * PUBLIC: void flash __P((void));
88 * PUBLIC: #endif
90 void
91 flash()
93 if (vb != NULL) {
94 (void)tputs(vb, 1, cl_putchar);
95 (void)fflush(stdout);
96 } else
97 beep();
99 #endif /* !HAVE_CURSES_FLASH */
101 #ifndef HAVE_CURSES_IDLOK
103 * idlok --
104 * Turn on/off hardware line insert/delete.
106 * PUBLIC: #ifndef HAVE_CURSES_IDLOK
107 * PUBLIC: void idlok __P((WINDOW *, int));
108 * PUBLIC: #endif
110 void
111 idlok(win, bf)
112 WINDOW *win;
113 int bf;
115 return;
117 #endif /* !HAVE_CURSES_IDLOK */
119 #ifndef HAVE_CURSES_KEYPAD
121 * keypad --
122 * Put the keypad/cursor arrows into or out of application mode.
124 * PUBLIC: #ifndef HAVE_CURSES_KEYPAD
125 * PUBLIC: int keypad __P((void *, int));
126 * PUBLIC: #endif
129 keypad(a, on)
130 void *a;
131 int on;
133 char *p;
135 if ((p = tigetstr(on ? "smkx" : "rmkx")) != (char *)-1) {
136 (void)tputs(p, 0, cl_putchar);
137 (void)fflush(stdout);
139 return (0);
141 #endif /* !HAVE_CURSES_KEYPAD */
143 #ifndef HAVE_CURSES_NEWTERM
145 * newterm --
146 * Create a new curses screen.
148 * PUBLIC: #ifndef HAVE_CURSES_NEWTERM
149 * PUBLIC: void *newterm __P((const char *, FILE *, FILE *));
150 * PUBLIC: #endif
152 void *
153 newterm(a, b, c)
154 const char *a;
155 FILE *b, *c;
157 return (initscr());
159 #endif /* !HAVE_CURSES_NEWTERM */
161 #ifndef HAVE_CURSES_SETUPTERM
163 * setupterm --
164 * Set up terminal.
166 * PUBLIC: #ifndef HAVE_CURSES_SETUPTERM
167 * PUBLIC: void setupterm __P((char *, int, int *));
168 * PUBLIC: #endif
170 void
171 setupterm(ttype, fno, errp)
172 char *ttype;
173 int fno, *errp;
175 static char buf[2048];
176 char *p;
178 if ((*errp = tgetent(buf, ttype)) > 0) {
179 if (ke != NULL)
180 free(ke);
181 ke = ((p = tigetstr("rmkx")) == (char *)-1) ?
182 NULL : strdup(p);
183 if (ks != NULL)
184 free(ks);
185 ks = ((p = tigetstr("smkx")) == (char *)-1) ?
186 NULL : strdup(p);
187 if (vb != NULL)
188 free(vb);
189 vb = ((p = tigetstr("flash")) == (char *)-1) ?
190 NULL : strdup(p);
193 #endif /* !HAVE_CURSES_SETUPTERM */
195 #ifndef HAVE_CURSES_TIGETSTR
196 /* Terminfo-to-termcap translation table. */
197 typedef struct _tl {
198 const char *terminfo; /* Terminfo name. */
199 const char *termcap; /* Termcap name. */
200 } TL;
201 static const TL list[] = {
202 { "cols", "co", }, /* Terminal columns. */
203 { "cup", "cm", }, /* Cursor up. */
204 { "cuu1", "up", }, /* Cursor up. */
205 { "el", "ce", }, /* Clear to end-of-line. */
206 { "flash", "vb", }, /* Visible bell. */
207 { "kcub1", "kl", }, /* Cursor left. */
208 { "kcud1", "kd", }, /* Cursor down. */
209 { "kcuf1", "kr", }, /* Cursor right. */
210 { "kcuu1", "ku", }, /* Cursor up. */
211 { "kdch1", "kD", }, /* Delete character. */
212 { "kdl1", "kL", }, /* Delete line. */
213 { "ked", "kS", }, /* Delete to end of screen. */
214 { "kel", "kE", }, /* Delete to eol. */
215 { "kend", "@7", }, /* Go to eol. */
216 { "khome", "kh", }, /* Go to sol. */
217 { "kich1", "kI", }, /* Insert at cursor. */
218 { "kil1", "kA", }, /* Insert line. */
219 { "kind", "kF", }, /* Scroll down. */
220 { "kll", "kH", }, /* Go to eol. */
221 { "knp", "kN", }, /* Page down. */
222 { "kpp", "kP", }, /* Page up. */
223 { "kri", "kR", }, /* Scroll up. */
224 { "lines", "li", }, /* Terminal lines. */
225 { "rmcup", "te", }, /* Terminal end string. */
226 { "rmkx", "ke", }, /* Exit "keypad-transmit" mode. */
227 { "rmso", "se", }, /* Standout end. */
228 { "smcup", "ti", }, /* Terminal initialization string. */
229 { "smkx", "ks", }, /* Enter "keypad-transmit" mode. */
230 { "smso", "so", }, /* Standout begin. */
233 #ifdef _AIX
235 * AIX's implementation for function keys greater than 10 is different and
236 * only goes as far as 36.
238 static const char codes[] = {
239 /* 0-10 */ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ';',
240 /* 11-20 */ '<', '>', '!', '@', '#', '$', '%', '^', '&', '*',
241 /* 21-30 */ '(', ')', '-', '_', '+', ',', ':', '?', '[', ']',
242 /* 31-36 */ '{', '}', '|', '~', '/', '='
245 #else
248 * !!!
249 * Historically, the 4BSD termcap code didn't support functions keys greater
250 * than 9. This was silently enforced -- asking for key k12 would return the
251 * value for k1. We try and get around this by using the tables specified in
252 * the terminfo(TI_ENV) man page from the 3rd Edition SVID. This assumes the
253 * implementors of any System V compatibility code or an extended termcap used
254 * those codes.
256 static const char codes[] = {
257 /* 0-10 */ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ';',
258 /* 11-19 */ '1', '2', '3', '4', '5', '6', '7', '8', '9',
259 /* 20-63 */ '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',
261 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
262 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
264 #endif /* _AIX */
267 * lcmp --
268 * list comparison routine for bsearch.
270 static int
271 lcmp(const void *a, const void *b)
273 return (strcmp(a, ((const TL *)b)->terminfo));
277 * tigetstr --
279 * Vendors put the prototype for tigetstr into random include files, including
280 * <term.h>, which we can't include because it makes other systems unhappy.
281 * Try and work around the problem, since we only care about the return value.
283 * PUBLIC: #ifdef HAVE_CURSES_TIGETSTR
284 * PUBLIC: char *tigetstr();
285 * PUBLIC: #else
286 * PUBLIC: char *tigetstr __P((char *));
287 * PUBLIC: #endif
289 char *
290 tigetstr(name)
291 const char *name;
293 static char sbuf[256];
294 TL *tlp;
295 int n;
296 char *p, mykeyname[3];
298 if ((tlp = bsearch(name,
299 list, sizeof(list) / sizeof(TL), sizeof(TL), lcmp)) == NULL) {
300 #ifdef _AIX
301 if (name[0] == 'k' &&
302 name[1] == 'f' && (n = atoi(name + 2)) <= 36) {
303 mykeyname[0] = 'k';
304 mykeyname[1] = codes[n];
305 mykeyname[2] = '\0';
306 #else
307 if (name[0] == 'k' &&
308 name[1] == 'f' && (n = atoi(name + 2)) <= 63) {
309 mykeyname[0] = n <= 10 ? 'k' : 'F';
310 mykeyname[1] = codes[n];
311 mykeyname[2] = '\0';
312 #endif
313 name = mykeyname;
315 } else
316 name = tlp->termcap;
318 p = sbuf;
319 #ifdef _AIX
320 return ((p = tgetstr(name, &p)) == NULL ? (char *)-1 : strcpy(sbuf, p));
321 #else
322 return (tgetstr(name, &p) == NULL ? (char *)-1 : sbuf);
323 #endif
327 * tigetnum --
329 * PUBLIC: #ifndef HAVE_CURSES_TIGETSTR
330 * PUBLIC: int tigetnum __P((char *));
331 * PUBLIC: #endif
334 tigetnum(name)
335 const char *name;
337 TL *tlp;
338 int val;
340 if ((tlp = bsearch(name,
341 list, sizeof(list) / sizeof(TL), sizeof(TL), lcmp)) != NULL) {
342 name = tlp->termcap;
345 return ((val = tgetnum(name)) == -1 ? -2 : val);
347 #endif /* !HAVE_CURSES_TIGETSTR */