Annotation of src/usr.bin/window/wwinit.c, Revision 1.2
1.2 ! deraadt 1: /* $NetBSD: wwinit.c,v 1.10 1995/12/21 11:06:28 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.2 ! deraadt 43: static char rcsid[] = "$NetBSD: wwinit.c,v 1.10 1995/12/21 11:06:28 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:
226: if ((wwsmap = wwalloc(0, 0, wwnrow, wwncol, sizeof (char))) == 0)
227: goto bad;
228: for (i = 0; i < wwnrow; i++)
229: for (j = 0; j < wwncol; j++)
230: wwsmap[i][j] = WWX_NOBODY;
231:
232: wwos = (union ww_char **)
233: wwalloc(0, 0, wwnrow, wwncol, sizeof (union ww_char));
234: if (wwos == 0)
235: goto bad;
236: /* wwos is cleared in wwstart1() */
237: wwns = (union ww_char **)
238: wwalloc(0, 0, wwnrow, wwncol, sizeof (union ww_char));
239: if (wwns == 0)
240: goto bad;
241: for (i = 0; i < wwnrow; i++)
242: for (j = 0; j < wwncol; j++)
243: wwns[i][j].c_w = ' ';
244: if (tt.tt_checkpoint) {
245: /* wwcs is also cleared in wwstart1() */
246: wwcs = (union ww_char **)
247: wwalloc(0, 0, wwnrow, wwncol, sizeof (union ww_char));
248: if (wwcs == 0)
249: goto bad;
250: }
251:
252: wwtouched = malloc((unsigned) wwnrow);
253: if (wwtouched == 0) {
254: wwerrno = WWE_NOMEM;
255: goto bad;
256: }
257: for (i = 0; i < wwnrow; i++)
258: wwtouched[i] = 0;
259:
260: wwupd = (struct ww_update *) malloc((unsigned) wwnrow * sizeof *wwupd);
261: if (wwupd == 0) {
262: wwerrno = WWE_NOMEM;
263: goto bad;
264: }
265:
266: wwindex[WWX_NOBODY] = &wwnobody;
267: wwnobody.ww_order = NWW;
268:
269: kp = wwwintermcap;
270: if (wwavailmodes & WWM_REV)
271: wwaddcap1(WWT_REV, &kp);
272: if (wwavailmodes & WWM_BLK)
273: wwaddcap1(WWT_BLK, &kp);
274: if (wwavailmodes & WWM_UL)
275: wwaddcap1(WWT_UL, &kp);
276: if (wwavailmodes & WWM_GRP)
277: wwaddcap1(WWT_GRP, &kp);
278: if (wwavailmodes & WWM_DIM)
279: wwaddcap1(WWT_DIM, &kp);
280: if (wwavailmodes & WWM_USR)
281: wwaddcap1(WWT_USR, &kp);
282: if (tt.tt_insline && tt.tt_delline || tt.tt_setscroll)
283: wwaddcap1(WWT_ALDL, &kp);
284: if (tt.tt_inschar)
285: wwaddcap1(WWT_IMEI, &kp);
286: if (tt.tt_insspace)
287: wwaddcap1(WWT_IC, &kp);
288: if (tt.tt_delchar)
289: wwaddcap1(WWT_DC, &kp);
290: wwaddcap("kb", &kp);
291: wwaddcap("ku", &kp);
292: wwaddcap("kd", &kp);
293: wwaddcap("kl", &kp);
294: wwaddcap("kr", &kp);
295: wwaddcap("kh", &kp);
296: if ((j = tgetnum("kn")) >= 0) {
297: char cap[32];
298:
299: (void) sprintf(kp, "kn#%d:", j);
300: for (; *kp; kp++)
301: ;
302: for (i = 1; i <= j; i++) {
303: (void) sprintf(cap, "k%d", i);
304: wwaddcap(cap, &kp);
305: cap[0] = 'l';
306: wwaddcap(cap, &kp);
307: }
308: }
309: /*
310: * It's ok to do this here even if setenv() is destructive
311: * since tt_init() has already made its own copy of it and
312: * wwterm now points to the copy.
313: */
314: (void) setenv("TERM", WWT_TERM, 1);
315: #ifdef TERMINFO
316: if (wwterminfoinit() < 0)
317: goto bad;
318: #endif
319:
320: if (tt.tt_checkpoint)
321: if (signal(SIGALRM, wwalarm) == BADSIG) {
322: wwerrno = WWE_SYS;
323: goto bad;
324: }
325: wwstart1();
1.2 ! deraadt 326:
! 327: sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
1.1 deraadt 328: return 0;
1.2 ! deraadt 329:
1.1 deraadt 330: bad:
331: /*
332: * Don't bother to free storage. We're supposed
333: * to exit when wwinit fails anyway.
334: */
335: (void) wwsettty(0, &wwoldtty);
1.2 ! deraadt 336:
! 337: sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
1.1 deraadt 338: return -1;
339: }
340:
341: wwaddcap(cap, kp)
342: register char *cap;
343: register char **kp;
344: {
345: char tbuf[512];
346: char *tp = tbuf;
347: register char *str, *p;
348:
349: if ((str = tgetstr(cap, &tp)) != 0) {
350: while (*(*kp)++ = *cap++)
351: ;
352: (*kp)[-1] = '=';
353: while (*str) {
354: for (p = unctrl(*str++); *(*kp)++ = *p++;)
355: ;
356: (*kp)--;
357: }
358: *(*kp)++ = ':';
359: **kp = 0;
360: }
361: }
362:
363: wwaddcap1(cap, kp)
364: register char *cap;
365: register char **kp;
366: {
367: while (*(*kp)++ = *cap++)
368: ;
369: (*kp)--;
370: }
371:
372: wwstart()
373: {
374: register i;
375:
376: (void) wwsettty(0, &wwnewtty);
377: for (i = 0; i < wwnrow; i++)
378: wwtouched[i] = WWU_TOUCHED;
379: wwstart1();
380: }
381:
382: wwstart1()
383: {
384: register i, j;
385:
386: for (i = 0; i < wwnrow; i++)
387: for (j = 0; j < wwncol; j++) {
388: wwos[i][j].c_w = ' ';
389: if (tt.tt_checkpoint)
390: wwcs[i][j].c_w = ' ';
391: }
392: xxstart();
393: if (tt.tt_checkpoint)
394: wwdocheckpoint = 1;
395: }
396:
397: /*
398: * Reset data structures and terminal from an unknown state.
399: * Restoring wwos has been taken care of elsewhere.
400: */
401: wwreset()
402: {
403: register i;
404:
405: xxreset();
406: for (i = 0; i < wwnrow; i++)
407: wwtouched[i] = WWU_TOUCHED;
408: }