1 /* @(#)wwinit.c 8.2 (Berkeley) 4/28/95 */
2 /* $NetBSD: wwinit.c,v 1.19 2009/04/14 08:50:06 lukem Exp $ */
5 * Copyright (c) 1983, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Edward Wang at The University of California, Berkeley.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 sigset_t nsigset, osigset;
54 wwhead.ww_forw = &wwhead;
55 wwhead.ww_back = &wwhead;
57 sigemptyset(&nsigset);
58 sigaddset(&nsigset, SIGCHLD);
59 sigaddset(&nsigset, SIGALRM);
60 sigaddset(&nsigset, SIGHUP);
61 sigaddset(&nsigset, SIGTERM);
62 sigprocmask(SIG_BLOCK, &nsigset, &osigset);
64 if (signal(SIGCHLD, wwchild) == SIG_ERR ||
65 signal(SIGHUP, wwquit) == SIG_ERR ||
66 signal(SIGTERM, wwquit) == SIG_ERR ||
67 signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
72 if (wwgettty(0, &wwoldtty) < 0)
76 wwwintty.ww_sgttyb.sg_flags &= ~XTABS;
77 wwnewtty.ww_sgttyb = wwoldtty.ww_sgttyb;
78 wwnewtty.ww_sgttyb.sg_erase = -1;
79 wwnewtty.ww_sgttyb.sg_kill = -1;
80 wwnewtty.ww_sgttyb.sg_flags |= CBREAK;
81 wwnewtty.ww_sgttyb.sg_flags &= ~(ECHO|CRMOD);
82 wwnewtty.ww_tchars.t_intrc = -1;
83 wwnewtty.ww_tchars.t_quitc = -1;
84 wwnewtty.ww_tchars.t_startc = -1;
85 wwnewtty.ww_tchars.t_stopc = -1;
86 wwnewtty.ww_tchars.t_eofc = -1;
87 wwnewtty.ww_tchars.t_brkc = -1;
88 wwnewtty.ww_ltchars.t_suspc = -1;
89 wwnewtty.ww_ltchars.t_dsuspc = -1;
90 wwnewtty.ww_ltchars.t_rprntc = -1;
91 wwnewtty.ww_ltchars.t_flushc = -1;
92 wwnewtty.ww_ltchars.t_werasc = -1;
93 wwnewtty.ww_ltchars.t_lnextc = -1;
94 wwnewtty.ww_lmode = wwoldtty.ww_lmode | LLITOUT;
95 wwnewtty.ww_ldisc = wwoldtty.ww_ldisc;
100 #ifndef _POSIX_VDISABLE
101 #define _POSIX_VDISABLE -1
103 wwwintty.ww_termios.c_oflag &= ~OXTABS;
104 wwwintty.ww_termios.c_cflag &= ~CLOCAL;
105 wwwintty.ww_termios.c_cflag |= HUPCL;
106 wwnewtty.ww_termios = wwoldtty.ww_termios;
107 wwnewtty.ww_termios.c_iflag &=
108 ~(ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXOFF | IMAXBEL);
109 wwnewtty.ww_termios.c_oflag = 0;
110 wwnewtty.ww_termios.c_cflag &= ~(CSIZE | PARENB);
111 wwnewtty.ww_termios.c_cflag |= CS8;
112 wwnewtty.ww_termios.c_lflag = 0;
113 for (i = 0; i < NCCS; i++)
114 wwnewtty.ww_termios.c_cc[i] = _POSIX_VDISABLE;
115 wwnewtty.ww_termios.c_cc[VMIN] = 1;
116 wwnewtty.ww_termios.c_cc[VTIME] = 0;
118 if (wwsettty(0, &wwnewtty) < 0)
121 if ((wwterm = getenv("TERM")) == 0) {
122 wwerrno = WWE_BADTERM;
125 if (tgetent(wwtermcap, wwterm) != 1) {
126 wwerrno = WWE_BADTERM;
130 wwospeed = wwoldtty.ww_sgttyb.sg_ospeed;
132 wwospeed = cfgetospeed(&wwoldtty.ww_termios);
209 wwavailmodes = tt.tt_availmodes;
212 if (wwavailmodes & WWM_REV)
213 wwcursormodes = WWM_REV | (wwavailmodes & WWM_BLK);
214 else if (wwavailmodes & WWM_UL)
215 wwcursormodes = WWM_UL;
217 if ((wwib = malloc((unsigned) 512)) == 0)
220 wwibq = wwibp = wwib;
222 wwsmap = (unsigned char **)
223 wwalloc(0, 0, wwnrow, wwncol, sizeof (unsigned char));
226 for (i = 0; i < wwnrow; i++)
227 for (j = 0; j < wwncol; j++)
228 wwsmap[i][j] = WWX_NOBODY;
230 wwos = (union ww_char **)
231 wwalloc(0, 0, wwnrow, wwncol, sizeof (union ww_char));
234 /* wwos is cleared in wwstart1() */
235 wwns = (union ww_char **)
236 wwalloc(0, 0, wwnrow, wwncol, sizeof (union ww_char));
239 for (i = 0; i < wwnrow; i++)
240 for (j = 0; j < wwncol; j++)
241 wwns[i][j].c_w = ' ';
242 if (tt.tt_checkpoint) {
243 /* wwcs is also cleared in wwstart1() */
244 wwcs = (union ww_char **)
245 wwalloc(0, 0, wwnrow, wwncol, sizeof (union ww_char));
250 wwtouched = malloc((unsigned) wwnrow);
251 if (wwtouched == 0) {
255 for (i = 0; i < wwnrow; i++)
258 wwupd = (struct ww_update *) malloc((unsigned) wwnrow * sizeof *wwupd);
264 wwindex[WWX_NOBODY] = &wwnobody;
265 wwnobody.ww_order = NWW;
268 if (wwavailmodes & WWM_REV)
269 wwaddcap1(WWT_REV, &kp);
270 if (wwavailmodes & WWM_BLK)
271 wwaddcap1(WWT_BLK, &kp);
272 if (wwavailmodes & WWM_UL)
273 wwaddcap1(WWT_UL, &kp);
274 if (wwavailmodes & WWM_GRP)
275 wwaddcap1(WWT_GRP, &kp);
276 if (wwavailmodes & WWM_DIM)
277 wwaddcap1(WWT_DIM, &kp);
278 if (wwavailmodes & WWM_USR)
279 wwaddcap1(WWT_USR, &kp);
280 if ((tt.tt_insline && tt.tt_delline) || tt.tt_setscroll)
281 wwaddcap1(WWT_ALDL, &kp);
283 wwaddcap1(WWT_IMEI, &kp);
285 wwaddcap1(WWT_IC, &kp);
287 wwaddcap1(WWT_DC, &kp);
294 if ((j = tgetnum("kn")) >= 0) {
297 (void) sprintf(kp, "kn#%d:", j);
300 for (i = 1; i <= j; i++) {
301 (void) sprintf(cap, "k%d", i);
308 * It's ok to do this here even if setenv() is destructive
309 * since tt_init() has already made its own copy of it and
310 * wwterm now points to the copy.
312 (void) setenv("TERM", WWT_TERM, 1);
314 if (wwterminfoinit() < 0)
318 if (tt.tt_checkpoint)
319 if (signal(SIGALRM, wwalarm) == SIG_ERR) {
325 sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
330 * Don't bother to free storage. We're supposed
331 * to exit when wwinit fails anyway.
333 (void) wwsettty(0, &wwoldtty);
335 sigprocmask(SIG_SETMASK, &osigset, (sigset_t *)0);
340 wwaddcap(const char *cap, char **kp)
347 if ((str = tgetstr(cap, &tp)) != NULL) {
348 while ((*(*kp)++ = *cap++))
352 for (p = unctrl(*str++); (*(*kp)++ = *p++);)
362 wwaddcap1(const char *cap, char **kp)
364 while ((*(*kp)++ = *cap++))
374 (void) wwsettty(0, &wwnewtty);
375 for (i = 0; i < wwnrow; i++)
376 wwtouched[i] = WWU_TOUCHED;
385 for (i = 0; i < wwnrow; i++)
386 for (j = 0; j < wwncol; j++) {
387 wwos[i][j].c_w = ' ';
388 if (tt.tt_checkpoint)
389 wwcs[i][j].c_w = ' ';
392 if (tt.tt_checkpoint)
397 * Reset data structures and terminal from an unknown state.
398 * Restoring wwos has been taken care of elsewhere.
406 for (i = 0; i < wwnrow; i++)
407 wwtouched[i] = WWU_TOUCHED;