Annotation of src/usr.bin/window/wwinit.c, Revision 1.3
1.3 ! niklas 1: /* $NetBSD: wwinit.c,v 1.11 1996/02/08 21:49:07 mycroft Exp $ */
1.1 deraadt 2:
3: /*
4: * Copyright (c) 1983, 1993
5: * The Regents of the University of California. All rights reserved.
6: *
7: * This code is derived from software contributed to Berkeley by
8: * Edward Wang at The University of California, Berkeley.
9: *
10: * Redistribution and use in source and binary forms, with or without
11: * modification, are permitted provided that the following conditions
12: * are met:
13: * 1. Redistributions of source code must retain the above copyright
14: * notice, this list of conditions and the following disclaimer.
15: * 2. Redistributions in binary form must reproduce the above copyright
16: * notice, this list of conditions and the following disclaimer in the
17: * documentation and/or other materials provided with the distribution.
18: * 3. All advertising materials mentioning features or use of this software
19: * must display the following acknowledgement:
20: * This product includes software developed by the University of
21: * California, Berkeley and its contributors.
22: * 4. Neither the name of the University nor the names of its contributors
23: * may be used to endorse or promote products derived from this software
24: * without specific prior written permission.
25: *
26: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36: * SUCH DAMAGE.
37: */
38:
39: #ifndef lint
40: #if 0
41: static char sccsid[] = "@(#)wwinit.c 8.2 (Berkeley) 4/28/95";
42: #else
1.3 ! niklas 43: static char rcsid[] = "$NetBSD: wwinit.c,v 1.11 1996/02/08 21:49:07 mycroft Exp $";
1.1 deraadt 44: #endif
45: #endif /* not lint */
46:
47: #include "ww.h"
48: #include "tt.h"
49: #include <sys/signal.h>
50: #include <fcntl.h>
51: #include "char.h"
52:
53: wwinit()
54: {
55: register i, j;
56: char *kp;
1.2 deraadt 57: sigset_t sigset, osigset;
1.1 deraadt 58:
59: wwdtablesize = 3;
60: wwhead.ww_forw = &wwhead;
61: wwhead.ww_back = &wwhead;
62:
1.2 deraadt 63: sigemptyset(&sigset);
64: sigaddset(&sigset, SIGCHLD);
65: sigaddset(&sigset, SIGALRM);
66: sigaddset(&sigset, SIGHUP);
67: sigaddset(&sigset, SIGTERM);
68: sigprocmask(SIG_BLOCK, &sigset, &osigset);
69:
70: if (signal(SIGCHLD, wwchild) == BADSIG ||
1.1 deraadt 71: signal(SIGHUP, wwquit) == BADSIG ||
72: signal(SIGTERM, wwquit) == BADSIG ||
73: signal(SIGPIPE, SIG_IGN) == BADSIG) {
74: wwerrno = WWE_SYS;
75: return -1;
76: }
77:
78: if (wwgettty(0, &wwoldtty) < 0)
79: return -1;
80: wwwintty = wwoldtty;
81: #ifdef OLD_TTY
82: wwwintty.ww_sgttyb.sg_flags &= ~XTABS;
83: wwnewtty.ww_sgttyb = wwoldtty.ww_sgttyb;
84: wwnewtty.ww_sgttyb.sg_erase = -1;
85: wwnewtty.ww_sgttyb.sg_kill = -1;
86: wwnewtty.ww_sgttyb.sg_flags |= CBREAK;
87: wwnewtty.ww_sgttyb.sg_flags &= ~(ECHO|CRMOD);
88: wwnewtty.ww_tchars.t_intrc = -1;
89: wwnewtty.ww_tchars.t_quitc = -1;
90: wwnewtty.ww_tchars.t_startc = -1;
91: wwnewtty.ww_tchars.t_stopc = -1;
92: wwnewtty.ww_tchars.t_eofc = -1;
93: wwnewtty.ww_tchars.t_brkc = -1;
94: wwnewtty.ww_ltchars.t_suspc = -1;
95: wwnewtty.ww_ltchars.t_dsuspc = -1;
96: wwnewtty.ww_ltchars.t_rprntc = -1;
97: wwnewtty.ww_ltchars.t_flushc = -1;
98: wwnewtty.ww_ltchars.t_werasc = -1;
99: wwnewtty.ww_ltchars.t_lnextc = -1;
100: wwnewtty.ww_lmode = wwoldtty.ww_lmode | LLITOUT;
101: wwnewtty.ww_ldisc = wwoldtty.ww_ldisc;
102: #else
103: #ifndef OXTABS
104: #define OXTABS XTABS
105: #endif
106: #ifndef _POSIX_VDISABLE
107: #define _POSIX_VDISABLE -1
108: #endif
109: wwwintty.ww_termios.c_oflag &= ~OXTABS;
110: wwnewtty.ww_termios = wwoldtty.ww_termios;
111: wwnewtty.ww_termios.c_iflag &=
112: ~(ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXOFF | IMAXBEL);
113: wwnewtty.ww_termios.c_oflag = 0;
114: wwnewtty.ww_termios.c_cflag &= ~(CSIZE | PARENB);
115: wwnewtty.ww_termios.c_cflag |= CS8;
116: wwnewtty.ww_termios.c_lflag = 0;
117: for (i = 0; i < NCCS; i++)
118: wwnewtty.ww_termios.c_cc[i] = _POSIX_VDISABLE;
119: wwnewtty.ww_termios.c_cc[VMIN] = 1;
120: wwnewtty.ww_termios.c_cc[VTIME] = 0;
121: #endif
122: if (wwsettty(0, &wwnewtty) < 0)
123: goto bad;
124:
125: if ((wwterm = getenv("TERM")) == 0) {
126: wwerrno = WWE_BADTERM;
127: goto bad;
128: }
129: if (tgetent(wwtermcap, wwterm) != 1) {
130: wwerrno = WWE_BADTERM;
131: goto bad;
132: }
133: #ifdef OLD_TTY
134: wwospeed = wwoldtty.ww_sgttyb.sg_ospeed;
135: #else
136: wwospeed = cfgetospeed(&wwoldtty.ww_termios);
137: wwbaud = wwospeed;
138: #endif
139: switch (wwospeed) {
140: default:
141: case B0:
142: wwbaud = 0;
143: break;
144: case B50:
145: wwbaud = 50;
146: break;
147: case B75:
148: wwbaud = 75;
149: break;
150: case B110:
151: wwbaud = 110;
152: break;
153: case B134:
154: wwbaud = 134;
155: break;
156: case B150:
157: wwbaud = 150;
158: break;
159: case B200:
160: wwbaud = 200;
161: break;
162: case B300:
163: wwbaud = 300;
164: break;
165: case B600:
166: wwbaud = 600;
167: break;
168: case B1200:
169: wwbaud = 1200;
170: break;
171: case B1800:
172: wwbaud = 1800;
173: break;
174: case B2400:
175: wwbaud = 2400;
176: break;
177: case B4800:
178: wwbaud = 4800;
179: break;
180: case B9600:
181: wwbaud = 9600;
182: break;
183: #ifdef B19200
184: case B19200:
185: #else
186: case EXTA:
187: #endif
188: wwbaud = 19200;
189: break;
190: #ifdef B38400
191: case B38400:
192: #else
193: case EXTB:
194: #endif
195: wwbaud = 38400;
196: break;
197: #ifdef B57600
198: case B57600:
199: wwbaud= 57600;
200: break;
201: #endif
202: #ifdef B115200
203: case B115200:
204: wwbaud = 115200;
205: break;
206: #endif
207: }
208:
209: if (xxinit() < 0)
210: goto bad;
211: wwnrow = tt.tt_nrow;
212: wwncol = tt.tt_ncol;
213: wwavailmodes = tt.tt_availmodes;
214: wwwrap = tt.tt_wrap;
215:
216: if (wwavailmodes & WWM_REV)
217: wwcursormodes = WWM_REV | wwavailmodes & WWM_BLK;
218: else if (wwavailmodes & WWM_UL)
219: wwcursormodes = WWM_UL;
220:
221: if ((wwib = malloc((unsigned) 512)) == 0)
222: goto bad;
223: wwibe = wwib + 512;
224: wwibq = wwibp = wwib;
225:
1.3 ! niklas 226: wwsmap = (unsigned char **)
! 227: wwalloc(0, 0, wwnrow, wwncol, sizeof (unsigned char));
! 228: if (wwsmap == 0)
1.1 deraadt 229: goto bad;
230: for (i = 0; i < wwnrow; i++)
231: for (j = 0; j < wwncol; j++)
232: wwsmap[i][j] = WWX_NOBODY;
233:
234: wwos = (union ww_char **)
235: wwalloc(0, 0, wwnrow, wwncol, sizeof (union ww_char));
236: if (wwos == 0)
237: goto bad;
238: /* wwos is cleared in wwstart1() */
239: wwns = (union ww_char **)
240: wwalloc(0, 0, wwnrow, wwncol, sizeof (union ww_char));
241: if (wwns == 0)
242: goto bad;
243: for (i = 0; i < wwnrow; i++)
244: for (j = 0; j < wwncol; j++)
245: wwns[i][j].c_w = ' ';
246: if (tt.tt_checkpoint) {
247: /* wwcs is also cleared in wwstart1() */
248: wwcs = (union ww_char **)
249: wwalloc(0, 0, wwnrow, wwncol, sizeof (union ww_char));
250: if (wwcs == 0)
251: goto bad;
252: }
253:
254: wwtouched = malloc((unsigned) wwnrow);
255: if (wwtouched == 0) {
256: wwerrno = WWE_NOMEM;
257: goto bad;
258: }
259: for (i = 0; i < wwnrow; i++)
260: wwtouched[i] = 0;
261:
262: wwupd = (struct ww_update *) malloc((unsigned) wwnrow * sizeof *wwupd);
263: if (wwupd == 0) {
264: wwerrno = WWE_NOMEM;
265: goto bad;
266: }
267:
268: wwindex[WWX_NOBODY] = &wwnobody;
269: wwnobody.ww_order = NWW;
270:
271: kp = wwwintermcap;
272: if (wwavailmodes & WWM_REV)
273: wwaddcap1(WWT_REV, &kp);
274: if (wwavailmodes & WWM_BLK)
275: wwaddcap1(WWT_BLK, &kp);
276: if (wwavailmodes & WWM_UL)
277: wwaddcap1(WWT_UL, &kp);
278: if (wwavailmodes & WWM_GRP)
279: wwaddcap1(WWT_GRP, &kp);
280: if (wwavailmodes & WWM_DIM)
281: wwaddcap1(WWT_DIM, &kp);
282: if (wwavailmodes & WWM_USR)
283: wwaddcap1(WWT_USR, &kp);
284: if (tt.tt_insline && tt.tt_delline || tt.tt_setscroll)
285: wwaddcap1(WWT_ALDL, &kp);
286: if (tt.tt_inschar)
287: wwaddcap1(WWT_IMEI, &kp);
288: if (tt.tt_insspace)
289: wwaddcap1(WWT_IC, &kp);
290: if (tt.tt_delchar)
291: wwaddcap1(WWT_DC, &kp);
292: wwaddcap("kb", &kp);
293: wwaddcap("ku", &kp);
294: wwaddcap("kd", &kp);
295: wwaddcap("kl", &kp);
296: wwaddcap("kr", &kp);
297: wwaddcap("kh", &kp);
298: if ((j = tgetnum("kn")) >= 0) {
299: char cap[32];
300:
301: (void) sprintf(kp, "kn#%d:", j);
302: for (; *kp; kp++)
303: ;
304: for (i = 1; i <= j; i++) {
305: (void) sprintf(cap, "k%d", i);
306: wwaddcap(cap, &kp);
307: cap[0] = 'l';
308: wwaddcap(cap, &kp);
309: }
310: }
311: /*
312: * It's ok to do this here even if setenv() is destructive
313: * since tt_init() has already made its own copy of it and
314: * wwterm now points to the copy.
315: */
316: (void) setenv("TERM", WWT_TERM, 1);
317: #ifdef TERMINFO
318: if (wwterminfoinit() < 0)
319: goto bad;
320: #endif
321:
322: if (tt.tt_checkpoint)
323: if (signal(SIGALRM, wwalarm) == BADSIG) {
324: wwerrno = WWE_SYS;
325: goto bad;
326: }
327: wwstart1();
1.2 deraadt 328:
329: sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
1.1 deraadt 330: return 0;
1.2 deraadt 331:
1.1 deraadt 332: bad:
333: /*
334: * Don't bother to free storage. We're supposed
335: * to exit when wwinit fails anyway.
336: */
337: (void) wwsettty(0, &wwoldtty);
1.2 deraadt 338:
339: sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
1.1 deraadt 340: return -1;
341: }
342:
343: wwaddcap(cap, kp)
344: register char *cap;
345: register char **kp;
346: {
347: char tbuf[512];
348: char *tp = tbuf;
349: register char *str, *p;
350:
351: if ((str = tgetstr(cap, &tp)) != 0) {
352: while (*(*kp)++ = *cap++)
353: ;
354: (*kp)[-1] = '=';
355: while (*str) {
356: for (p = unctrl(*str++); *(*kp)++ = *p++;)
357: ;
358: (*kp)--;
359: }
360: *(*kp)++ = ':';
361: **kp = 0;
362: }
363: }
364:
365: wwaddcap1(cap, kp)
366: register char *cap;
367: register char **kp;
368: {
369: while (*(*kp)++ = *cap++)
370: ;
371: (*kp)--;
372: }
373:
374: wwstart()
375: {
376: register i;
377:
378: (void) wwsettty(0, &wwnewtty);
379: for (i = 0; i < wwnrow; i++)
380: wwtouched[i] = WWU_TOUCHED;
381: wwstart1();
382: }
383:
384: wwstart1()
385: {
386: register i, j;
387:
388: for (i = 0; i < wwnrow; i++)
389: for (j = 0; j < wwncol; j++) {
390: wwos[i][j].c_w = ' ';
391: if (tt.tt_checkpoint)
392: wwcs[i][j].c_w = ' ';
393: }
394: xxstart();
395: if (tt.tt_checkpoint)
396: wwdocheckpoint = 1;
397: }
398:
399: /*
400: * Reset data structures and terminal from an unknown state.
401: * Restoring wwos has been taken care of elsewhere.
402: */
403: wwreset()
404: {
405: register i;
406:
407: xxreset();
408: for (i = 0; i < wwnrow; i++)
409: wwtouched[i] = WWU_TOUCHED;
410: }