1 /* $NetBSD: wwinit.c,v 1.18 2003/08/13 15:21:07 itojun Exp $ */
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Edward Wang at The University of California, Berkeley.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
38 static char sccsid
[] = "@(#)wwinit.c 8.2 (Berkeley) 4/28/95";
40 __RCSID("$NetBSD: wwinit.c,v 1.18 2003/08/13 15:21:07 itojun Exp $");
59 sigset_t nsigset
, osigset
;
62 wwhead
.ww_forw
= &wwhead
;
63 wwhead
.ww_back
= &wwhead
;
65 sigemptyset(&nsigset
);
66 sigaddset(&nsigset
, SIGCHLD
);
67 sigaddset(&nsigset
, SIGALRM
);
68 sigaddset(&nsigset
, SIGHUP
);
69 sigaddset(&nsigset
, SIGTERM
);
70 sigprocmask(SIG_BLOCK
, &nsigset
, &osigset
);
72 if (signal(SIGCHLD
, wwchild
) == SIG_ERR
||
73 signal(SIGHUP
, wwquit
) == SIG_ERR
||
74 signal(SIGTERM
, wwquit
) == SIG_ERR
||
75 signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
) {
80 if (wwgettty(0, &wwoldtty
) < 0)
84 wwwintty
.ww_sgttyb
.sg_flags
&= ~XTABS
;
85 wwnewtty
.ww_sgttyb
= wwoldtty
.ww_sgttyb
;
86 wwnewtty
.ww_sgttyb
.sg_erase
= -1;
87 wwnewtty
.ww_sgttyb
.sg_kill
= -1;
88 wwnewtty
.ww_sgttyb
.sg_flags
|= CBREAK
;
89 wwnewtty
.ww_sgttyb
.sg_flags
&= ~(ECHO
|CRMOD
);
90 wwnewtty
.ww_tchars
.t_intrc
= -1;
91 wwnewtty
.ww_tchars
.t_quitc
= -1;
92 wwnewtty
.ww_tchars
.t_startc
= -1;
93 wwnewtty
.ww_tchars
.t_stopc
= -1;
94 wwnewtty
.ww_tchars
.t_eofc
= -1;
95 wwnewtty
.ww_tchars
.t_brkc
= -1;
96 wwnewtty
.ww_ltchars
.t_suspc
= -1;
97 wwnewtty
.ww_ltchars
.t_dsuspc
= -1;
98 wwnewtty
.ww_ltchars
.t_rprntc
= -1;
99 wwnewtty
.ww_ltchars
.t_flushc
= -1;
100 wwnewtty
.ww_ltchars
.t_werasc
= -1;
101 wwnewtty
.ww_ltchars
.t_lnextc
= -1;
102 wwnewtty
.ww_lmode
= wwoldtty
.ww_lmode
| LLITOUT
;
103 wwnewtty
.ww_ldisc
= wwoldtty
.ww_ldisc
;
108 #ifndef _POSIX_VDISABLE
109 #define _POSIX_VDISABLE -1
111 wwwintty
.ww_termios
.c_oflag
&= ~OXTABS
;
112 wwwintty
.ww_termios
.c_cflag
&= ~CLOCAL
;
113 wwwintty
.ww_termios
.c_cflag
|= HUPCL
;
114 wwnewtty
.ww_termios
= wwoldtty
.ww_termios
;
115 wwnewtty
.ww_termios
.c_iflag
&=
116 ~(ISTRIP
| INLCR
| IGNCR
| ICRNL
| IXON
| IXOFF
| IMAXBEL
);
117 wwnewtty
.ww_termios
.c_oflag
= 0;
118 wwnewtty
.ww_termios
.c_cflag
&= ~(CSIZE
| PARENB
);
119 wwnewtty
.ww_termios
.c_cflag
|= CS8
;
120 wwnewtty
.ww_termios
.c_lflag
= 0;
121 for (i
= 0; i
< NCCS
; i
++)
122 wwnewtty
.ww_termios
.c_cc
[i
] = _POSIX_VDISABLE
;
123 wwnewtty
.ww_termios
.c_cc
[VMIN
] = 1;
124 wwnewtty
.ww_termios
.c_cc
[VTIME
] = 0;
126 if (wwsettty(0, &wwnewtty
) < 0)
129 if ((wwterm
= getenv("TERM")) == 0) {
130 wwerrno
= WWE_BADTERM
;
133 if (tgetent(wwtermcap
, wwterm
) != 1) {
134 wwerrno
= WWE_BADTERM
;
138 wwospeed
= wwoldtty
.ww_sgttyb
.sg_ospeed
;
140 wwospeed
= cfgetospeed(&wwoldtty
.ww_termios
);
217 wwavailmodes
= tt
.tt_availmodes
;
220 if (wwavailmodes
& WWM_REV
)
221 wwcursormodes
= WWM_REV
| (wwavailmodes
& WWM_BLK
);
222 else if (wwavailmodes
& WWM_UL
)
223 wwcursormodes
= WWM_UL
;
225 if ((wwib
= malloc((unsigned) 512)) == 0)
228 wwibq
= wwibp
= wwib
;
230 wwsmap
= (unsigned char **)
231 wwalloc(0, 0, wwnrow
, wwncol
, sizeof (unsigned char));
234 for (i
= 0; i
< wwnrow
; i
++)
235 for (j
= 0; j
< wwncol
; j
++)
236 wwsmap
[i
][j
] = WWX_NOBODY
;
238 wwos
= (union ww_char
**)
239 wwalloc(0, 0, wwnrow
, wwncol
, sizeof (union ww_char
));
242 /* wwos is cleared in wwstart1() */
243 wwns
= (union ww_char
**)
244 wwalloc(0, 0, wwnrow
, wwncol
, sizeof (union ww_char
));
247 for (i
= 0; i
< wwnrow
; i
++)
248 for (j
= 0; j
< wwncol
; j
++)
249 wwns
[i
][j
].c_w
= ' ';
250 if (tt
.tt_checkpoint
) {
251 /* wwcs is also cleared in wwstart1() */
252 wwcs
= (union ww_char
**)
253 wwalloc(0, 0, wwnrow
, wwncol
, sizeof (union ww_char
));
258 wwtouched
= malloc((unsigned) wwnrow
);
259 if (wwtouched
== 0) {
263 for (i
= 0; i
< wwnrow
; i
++)
266 wwupd
= (struct ww_update
*) malloc((unsigned) wwnrow
* sizeof *wwupd
);
272 wwindex
[WWX_NOBODY
] = &wwnobody
;
273 wwnobody
.ww_order
= NWW
;
276 if (wwavailmodes
& WWM_REV
)
277 wwaddcap1(WWT_REV
, &kp
);
278 if (wwavailmodes
& WWM_BLK
)
279 wwaddcap1(WWT_BLK
, &kp
);
280 if (wwavailmodes
& WWM_UL
)
281 wwaddcap1(WWT_UL
, &kp
);
282 if (wwavailmodes
& WWM_GRP
)
283 wwaddcap1(WWT_GRP
, &kp
);
284 if (wwavailmodes
& WWM_DIM
)
285 wwaddcap1(WWT_DIM
, &kp
);
286 if (wwavailmodes
& WWM_USR
)
287 wwaddcap1(WWT_USR
, &kp
);
288 if ((tt
.tt_insline
&& tt
.tt_delline
) || tt
.tt_setscroll
)
289 wwaddcap1(WWT_ALDL
, &kp
);
291 wwaddcap1(WWT_IMEI
, &kp
);
293 wwaddcap1(WWT_IC
, &kp
);
295 wwaddcap1(WWT_DC
, &kp
);
302 if ((j
= tgetnum("kn")) >= 0) {
305 (void) sprintf(kp
, "kn#%d:", j
);
308 for (i
= 1; i
<= j
; i
++) {
309 (void) sprintf(cap
, "k%d", i
);
316 * It's ok to do this here even if setenv() is destructive
317 * since tt_init() has already made its own copy of it and
318 * wwterm now points to the copy.
320 (void) setenv("TERM", WWT_TERM
, 1);
322 if (wwterminfoinit() < 0)
326 if (tt
.tt_checkpoint
)
327 if (signal(SIGALRM
, wwalarm
) == SIG_ERR
) {
333 sigprocmask(SIG_SETMASK
, &osigset
, (sigset_t
*)0);
338 * Don't bother to free storage. We're supposed
339 * to exit when wwinit fails anyway.
341 (void) wwsettty(0, &wwoldtty
);
343 sigprocmask(SIG_SETMASK
, &osigset
, (sigset_t
*)0);
348 wwaddcap(const char *cap
, char **kp
)
355 if ((str
= tgetstr(cap
, &tp
)) != 0) {
356 while ((*(*kp
)++ = *cap
++))
360 for (p
= unctrl(*str
++); (*(*kp
)++ = *p
++);)
370 wwaddcap1(const char *cap
, char **kp
)
372 while ((*(*kp
)++ = *cap
++))
382 (void) wwsettty(0, &wwnewtty
);
383 for (i
= 0; i
< wwnrow
; i
++)
384 wwtouched
[i
] = WWU_TOUCHED
;
393 for (i
= 0; i
< wwnrow
; i
++)
394 for (j
= 0; j
< wwncol
; j
++) {
395 wwos
[i
][j
].c_w
= ' ';
396 if (tt
.tt_checkpoint
)
397 wwcs
[i
][j
].c_w
= ' ';
400 if (tt
.tt_checkpoint
)
405 * Reset data structures and terminal from an unknown state.
406 * Restoring wwos has been taken care of elsewhere.
414 for (i
= 0; i
< wwnrow
; i
++)
415 wwtouched
[i
] = WWU_TOUCHED
;