.Xr botch fix
[unix-history] / usr / src / usr.bin / window / wwinit.c
CommitLineData
60de5df9 1/*
46e9ea25
KB
2 * Copyright (c) 1983 Regents of the University of California.
3 * All rights reserved.
4 *
3dd3a9e5
KB
5 * This code is derived from software contributed to Berkeley by
6 * Edward Wang at The University of California, Berkeley.
7 *
87f529ec 8 * %sccs.include.redist.c%
60de5df9
EW
9 */
10
46e9ea25 11#ifndef lint
3dd3a9e5 12static char sccsid[] = "@(#)wwinit.c 3.39 (Berkeley) %G%";
46e9ea25
KB
13#endif /* not lint */
14
5528a91e 15#include "ww.h"
e908bfac 16#include "tt.h"
b1189050
EW
17#include <sys/signal.h>
18#include <fcntl.h>
0e64e422 19#include "char.h"
5528a91e 20
5528a91e
EW
21wwinit()
22{
bb05dfb5 23 register i, j;
4222244b 24 char *kp;
7c38ad4d 25 int s;
4711df8b 26
bb05dfb5 27 wwdtablesize = getdtablesize();
4222244b
EW
28 wwhead.ww_forw = &wwhead;
29 wwhead.ww_back = &wwhead;
b1189050 30
7c38ad4d 31 s = sigblock(sigmask(SIGIO));
ab8b3b31
EW
32 if (signal(SIGIO, wwrint) == BADSIG ||
33 signal(SIGCHLD, wwchild) == BADSIG ||
34 signal(SIGPIPE, SIG_IGN) == BADSIG) {
35 wwerrno = WWE_SYS;
b1189050 36 return -1;
ab8b3b31 37 }
b1189050 38
4711df8b
EW
39 if (wwgettty(0, &wwoldtty) < 0)
40 return -1;
c44edccd 41 wwwintty = wwoldtty;
bbbb7b14 42#ifndef POSIX_TTY
c44edccd 43 wwwintty.ww_sgttyb.sg_flags &= ~XTABS;
4222244b
EW
44 wwnewtty.ww_sgttyb = wwoldtty.ww_sgttyb;
45 wwnewtty.ww_sgttyb.sg_erase = -1;
46 wwnewtty.ww_sgttyb.sg_kill = -1;
4711df8b 47 wwnewtty.ww_sgttyb.sg_flags |= CBREAK;
2b44d852 48 wwnewtty.ww_sgttyb.sg_flags &= ~(ECHO|CRMOD);
4222244b
EW
49 wwnewtty.ww_tchars.t_intrc = -1;
50 wwnewtty.ww_tchars.t_quitc = -1;
51 wwnewtty.ww_tchars.t_startc = -1;
52 wwnewtty.ww_tchars.t_stopc = -1;
53 wwnewtty.ww_tchars.t_eofc = -1;
54 wwnewtty.ww_tchars.t_brkc = -1;
55 wwnewtty.ww_ltchars.t_suspc = -1;
56 wwnewtty.ww_ltchars.t_dsuspc = -1;
57 wwnewtty.ww_ltchars.t_rprntc = -1;
58 wwnewtty.ww_ltchars.t_flushc = -1;
59 wwnewtty.ww_ltchars.t_werasc = -1;
60 wwnewtty.ww_ltchars.t_lnextc = -1;
61 wwnewtty.ww_lmode = wwoldtty.ww_lmode | LLITOUT;
62 wwnewtty.ww_ldisc = wwoldtty.ww_ldisc;
bbbb7b14
EW
63#else
64 wwwintty.ww_termios.c_oflag &= ~OXTABS;
65 wwnewtty.ww_termios = wwoldtty.ww_termios;
66 wwnewtty.ww_termios.c_iflag &=
67 ~(ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXOFF | IMAXBEL);
68 wwnewtty.ww_termios.c_iflag |= INPCK;
69 wwnewtty.ww_termios.c_oflag = 0;
70 wwnewtty.ww_termios.c_cflag &= ~(CSIZE | PARENB);
71 wwnewtty.ww_termios.c_cflag |= CS8;
72 wwnewtty.ww_termios.c_lflag = 0;
73 for (i = 0; i < NCC; i++)
74 wwnewtty.ww_termios.c_cc[i] = _POSIX_VDISABLE;
75#endif
4222244b 76 wwnewtty.ww_fflags = wwoldtty.ww_fflags | FASYNC;
4cbd8755 77 if (wwsettty(0, &wwnewtty, &wwoldtty) < 0)
bb05dfb5
EW
78 goto bad;
79
03e75950
EW
80 if ((wwterm = getenv("TERM")) == 0) {
81 wwerrno = WWE_BADTERM;
bb05dfb5 82 goto bad;
03e75950
EW
83 }
84 if (tgetent(wwtermcap, wwterm) != 1) {
85 wwerrno = WWE_BADTERM;
bb05dfb5 86 goto bad;
03e75950 87 }
bbbb7b14 88#ifndef POSIX_TTY
bb05dfb5 89 wwbaud = wwbaudmap[wwoldtty.ww_sgttyb.sg_ospeed];
bbbb7b14
EW
90#else
91#ifdef CBAUD
92 wwbaud = wwbaudmap[wwoldtty.ww_termios.c_cflag & CBAUD];
93#else
94 wwbaud = wwoldtty.ww_termios.c_ospeed;
95#endif
96#endif
bb05dfb5 97
ab8b3b31 98 if (xxinit() < 0)
bb05dfb5
EW
99 goto bad;
100 wwnrow = tt.tt_nrow;
101 wwncol = tt.tt_ncol;
7d77e730 102 wwavailmodes = tt.tt_availmodes;
5e785082 103 wwwrap = tt.tt_wrap;
bb05dfb5 104
73218728
EW
105 if (wwavailmodes & WWM_REV)
106 wwcursormodes = WWM_REV | wwavailmodes & WWM_BLK;
107 else if (wwavailmodes & WWM_UL)
108 wwcursormodes = WWM_UL;
109
8fa6d94c
EW
110 if ((wwib = malloc((unsigned) 512)) == 0)
111 goto bad;
112 wwibe = wwib + 512;
b1189050 113 wwibq = wwibp = wwib;
8fa6d94c 114
f2a77fe1 115 if ((wwsmap = wwalloc(0, 0, wwnrow, wwncol, sizeof (char))) == 0)
bb05dfb5
EW
116 goto bad;
117 for (i = 0; i < wwnrow; i++)
118 for (j = 0; j < wwncol; j++)
119 wwsmap[i][j] = WWX_NOBODY;
0896e17e 120
bb05dfb5 121 wwos = (union ww_char **)
f2a77fe1 122 wwalloc(0, 0, wwnrow, wwncol, sizeof (union ww_char));
bb05dfb5
EW
123 if (wwos == 0)
124 goto bad;
125 for (i = 0; i < wwnrow; i++)
126 for (j = 0; j < wwncol; j++)
127 wwos[i][j].c_w = ' ';
128 wwns = (union ww_char **)
f2a77fe1 129 wwalloc(0, 0, wwnrow, wwncol, sizeof (union ww_char));
bb05dfb5
EW
130 if (wwns == 0)
131 goto bad;
132 for (i = 0; i < wwnrow; i++)
133 for (j = 0; j < wwncol; j++)
134 wwns[i][j].c_w = ' ';
135
861cd1ed 136 wwtouched = malloc((unsigned) wwnrow);
03e75950
EW
137 if (wwtouched == 0) {
138 wwerrno = WWE_NOMEM;
861cd1ed 139 goto bad;
03e75950 140 }
861cd1ed
EW
141 for (i = 0; i < wwnrow; i++)
142 wwtouched[i] = 0;
143
6a1ef78a
EW
144 wwupd = (struct ww_update *) malloc((unsigned) wwnrow * sizeof *wwupd);
145 if (wwupd == 0) {
146 wwerrno = WWE_NOMEM;
147 goto bad;
148 }
149
bb05dfb5
EW
150 wwindex[WWX_NOBODY] = &wwnobody;
151 wwnobody.ww_order = NWW;
3e0824a9 152
cf28fdac
EW
153 kp = wwwintermcap;
154 if (wwavailmodes & WWM_REV)
155 wwaddcap1(WWT_REV, &kp);
156 if (wwavailmodes & WWM_BLK)
157 wwaddcap1(WWT_BLK, &kp);
158 if (wwavailmodes & WWM_UL)
159 wwaddcap1(WWT_UL, &kp);
160 if (wwavailmodes & WWM_GRP)
161 wwaddcap1(WWT_GRP, &kp);
162 if (wwavailmodes & WWM_DIM)
163 wwaddcap1(WWT_DIM, &kp);
164 if (wwavailmodes & WWM_USR)
165 wwaddcap1(WWT_USR, &kp);
166 if (tt.tt_insline && tt.tt_delline || tt.tt_setscroll)
167 wwaddcap1(WWT_ALDL, &kp);
07578449 168 if (tt.tt_inschar)
cf28fdac 169 wwaddcap1(WWT_IMEI, &kp);
07578449
EW
170 if (tt.tt_insspace)
171 wwaddcap1(WWT_IC, &kp);
cf28fdac
EW
172 if (tt.tt_delchar)
173 wwaddcap1(WWT_DC, &kp);
3f32a92a
EW
174 wwaddcap("kb", &kp);
175 wwaddcap("ku", &kp);
176 wwaddcap("kd", &kp);
177 wwaddcap("kl", &kp);
178 wwaddcap("kr", &kp);
179 wwaddcap("kh", &kp);
4222244b
EW
180 if ((j = tgetnum("kn")) >= 0) {
181 char cap[32];
3e0824a9 182
4222244b 183 (void) sprintf(kp, "kn#%d:", j);
3e0824a9
EW
184 for (; *kp; kp++)
185 ;
4222244b 186 for (i = 1; i <= j; i++) {
bb05dfb5 187 (void) sprintf(cap, "k%d", i);
3f32a92a 188 wwaddcap(cap, &kp);
3e0824a9 189 cap[0] = 'l';
3f32a92a 190 wwaddcap(cap, &kp);
3e0824a9
EW
191 }
192 }
cd9c5720
EW
193 /*
194 * It's ok to do this here even if setenv() is destructive
195 * since tt_init() has already made its own copy of it and
196 * wwterm now points to the copy.
197 */
91b689b9 198 (void) setenv("TERM", WWT_TERM, 1);
4222244b 199
7c38ad4d 200 (void) sigsetmask(s);
ab8b3b31
EW
201 /* catch typeahead before ASYNC was set */
202 (void) kill(getpid(), SIGIO);
203 xxstart();
4711df8b 204 return 0;
bb05dfb5 205bad:
861cd1ed
EW
206 /*
207 * Don't bother to free storage. We're supposed
208 * to exit when wwinit fails anyway.
209 */
4cbd8755 210 (void) wwsettty(0, &wwoldtty, &wwnewtty);
b1189050 211 (void) signal(SIGIO, SIG_DFL);
7c38ad4d 212 (void) sigsetmask(s);
bb05dfb5 213 return -1;
5528a91e 214}
3e0824a9 215
3f32a92a 216wwaddcap(cap, kp)
cf28fdac
EW
217 register char *cap;
218 register char **kp;
3e0824a9 219{
bb05dfb5
EW
220 char tbuf[512];
221 char *tp = tbuf;
9ad71678 222 register char *str, *p;
3e0824a9
EW
223
224 if ((str = tgetstr(cap, &tp)) != 0) {
4222244b 225 while (*(*kp)++ = *cap++)
3e0824a9 226 ;
4222244b 227 (*kp)[-1] = '=';
9ad71678 228 while (*str) {
4222244b 229 for (p = unctrl(*str++); *(*kp)++ = *p++;)
9ad71678 230 ;
4222244b 231 (*kp)--;
9ad71678 232 }
4222244b
EW
233 *(*kp)++ = ':';
234 **kp = 0;
3e0824a9
EW
235 }
236}
cf28fdac
EW
237
238wwaddcap1(cap, kp)
239 register char *cap;
240 register char **kp;
241{
242 while (*(*kp)++ = *cap++)
243 ;
244 (*kp)--;
245}