new copyright notice
[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 *
5 * Redistribution and use in source and binary forms are permitted
5e8b0e60
KB
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
60de5df9
EW
16 */
17
46e9ea25 18#ifndef lint
07578449 19static char sccsid[] = "@(#)wwinit.c 3.36 (Berkeley) %G%";
46e9ea25
KB
20#endif /* not lint */
21
5528a91e 22#include "ww.h"
e908bfac 23#include "tt.h"
b1189050
EW
24#include <sys/signal.h>
25#include <fcntl.h>
0e64e422 26#include "char.h"
5528a91e 27
5528a91e
EW
28wwinit()
29{
bb05dfb5 30 register i, j;
4222244b 31 char *kp;
7c38ad4d 32 int s;
4711df8b 33
bb05dfb5 34 wwdtablesize = getdtablesize();
4222244b
EW
35 wwhead.ww_forw = &wwhead;
36 wwhead.ww_back = &wwhead;
b1189050 37
7c38ad4d 38 s = sigblock(sigmask(SIGIO));
ab8b3b31
EW
39 if (signal(SIGIO, wwrint) == BADSIG ||
40 signal(SIGCHLD, wwchild) == BADSIG ||
41 signal(SIGPIPE, SIG_IGN) == BADSIG) {
42 wwerrno = WWE_SYS;
b1189050 43 return -1;
ab8b3b31 44 }
b1189050 45
4711df8b
EW
46 if (wwgettty(0, &wwoldtty) < 0)
47 return -1;
c44edccd
EW
48 wwwintty = wwoldtty;
49 wwwintty.ww_sgttyb.sg_flags &= ~XTABS;
4222244b
EW
50 wwnewtty.ww_sgttyb = wwoldtty.ww_sgttyb;
51 wwnewtty.ww_sgttyb.sg_erase = -1;
52 wwnewtty.ww_sgttyb.sg_kill = -1;
4711df8b 53 wwnewtty.ww_sgttyb.sg_flags |= CBREAK;
2b44d852 54 wwnewtty.ww_sgttyb.sg_flags &= ~(ECHO|CRMOD);
4222244b
EW
55 wwnewtty.ww_tchars.t_intrc = -1;
56 wwnewtty.ww_tchars.t_quitc = -1;
57 wwnewtty.ww_tchars.t_startc = -1;
58 wwnewtty.ww_tchars.t_stopc = -1;
59 wwnewtty.ww_tchars.t_eofc = -1;
60 wwnewtty.ww_tchars.t_brkc = -1;
61 wwnewtty.ww_ltchars.t_suspc = -1;
62 wwnewtty.ww_ltchars.t_dsuspc = -1;
63 wwnewtty.ww_ltchars.t_rprntc = -1;
64 wwnewtty.ww_ltchars.t_flushc = -1;
65 wwnewtty.ww_ltchars.t_werasc = -1;
66 wwnewtty.ww_ltchars.t_lnextc = -1;
67 wwnewtty.ww_lmode = wwoldtty.ww_lmode | LLITOUT;
68 wwnewtty.ww_ldisc = wwoldtty.ww_ldisc;
69 wwnewtty.ww_fflags = wwoldtty.ww_fflags | FASYNC;
4cbd8755 70 if (wwsettty(0, &wwnewtty, &wwoldtty) < 0)
bb05dfb5
EW
71 goto bad;
72
03e75950
EW
73 if ((wwterm = getenv("TERM")) == 0) {
74 wwerrno = WWE_BADTERM;
bb05dfb5 75 goto bad;
03e75950
EW
76 }
77 if (tgetent(wwtermcap, wwterm) != 1) {
78 wwerrno = WWE_BADTERM;
bb05dfb5 79 goto bad;
03e75950 80 }
bb05dfb5
EW
81 wwbaud = wwbaudmap[wwoldtty.ww_sgttyb.sg_ospeed];
82
ab8b3b31 83 if (xxinit() < 0)
bb05dfb5
EW
84 goto bad;
85 wwnrow = tt.tt_nrow;
86 wwncol = tt.tt_ncol;
7d77e730 87 wwavailmodes = tt.tt_availmodes;
5e785082 88 wwwrap = tt.tt_wrap;
bb05dfb5 89
73218728
EW
90 if (wwavailmodes & WWM_REV)
91 wwcursormodes = WWM_REV | wwavailmodes & WWM_BLK;
92 else if (wwavailmodes & WWM_UL)
93 wwcursormodes = WWM_UL;
94
8fa6d94c
EW
95 if ((wwib = malloc((unsigned) 512)) == 0)
96 goto bad;
97 wwibe = wwib + 512;
b1189050 98 wwibq = wwibp = wwib;
8fa6d94c 99
f2a77fe1 100 if ((wwsmap = wwalloc(0, 0, wwnrow, wwncol, sizeof (char))) == 0)
bb05dfb5
EW
101 goto bad;
102 for (i = 0; i < wwnrow; i++)
103 for (j = 0; j < wwncol; j++)
104 wwsmap[i][j] = WWX_NOBODY;
0896e17e 105
bb05dfb5 106 wwos = (union ww_char **)
f2a77fe1 107 wwalloc(0, 0, wwnrow, wwncol, sizeof (union ww_char));
bb05dfb5
EW
108 if (wwos == 0)
109 goto bad;
110 for (i = 0; i < wwnrow; i++)
111 for (j = 0; j < wwncol; j++)
112 wwos[i][j].c_w = ' ';
113 wwns = (union ww_char **)
f2a77fe1 114 wwalloc(0, 0, wwnrow, wwncol, sizeof (union ww_char));
bb05dfb5
EW
115 if (wwns == 0)
116 goto bad;
117 for (i = 0; i < wwnrow; i++)
118 for (j = 0; j < wwncol; j++)
119 wwns[i][j].c_w = ' ';
120
861cd1ed 121 wwtouched = malloc((unsigned) wwnrow);
03e75950
EW
122 if (wwtouched == 0) {
123 wwerrno = WWE_NOMEM;
861cd1ed 124 goto bad;
03e75950 125 }
861cd1ed
EW
126 for (i = 0; i < wwnrow; i++)
127 wwtouched[i] = 0;
128
6a1ef78a
EW
129 wwupd = (struct ww_update *) malloc((unsigned) wwnrow * sizeof *wwupd);
130 if (wwupd == 0) {
131 wwerrno = WWE_NOMEM;
132 goto bad;
133 }
134
bb05dfb5
EW
135 wwindex[WWX_NOBODY] = &wwnobody;
136 wwnobody.ww_order = NWW;
3e0824a9 137
cf28fdac
EW
138 kp = wwwintermcap;
139 if (wwavailmodes & WWM_REV)
140 wwaddcap1(WWT_REV, &kp);
141 if (wwavailmodes & WWM_BLK)
142 wwaddcap1(WWT_BLK, &kp);
143 if (wwavailmodes & WWM_UL)
144 wwaddcap1(WWT_UL, &kp);
145 if (wwavailmodes & WWM_GRP)
146 wwaddcap1(WWT_GRP, &kp);
147 if (wwavailmodes & WWM_DIM)
148 wwaddcap1(WWT_DIM, &kp);
149 if (wwavailmodes & WWM_USR)
150 wwaddcap1(WWT_USR, &kp);
151 if (tt.tt_insline && tt.tt_delline || tt.tt_setscroll)
152 wwaddcap1(WWT_ALDL, &kp);
07578449 153 if (tt.tt_inschar)
cf28fdac 154 wwaddcap1(WWT_IMEI, &kp);
07578449
EW
155 if (tt.tt_insspace)
156 wwaddcap1(WWT_IC, &kp);
cf28fdac
EW
157 if (tt.tt_delchar)
158 wwaddcap1(WWT_DC, &kp);
3f32a92a
EW
159 wwaddcap("kb", &kp);
160 wwaddcap("ku", &kp);
161 wwaddcap("kd", &kp);
162 wwaddcap("kl", &kp);
163 wwaddcap("kr", &kp);
164 wwaddcap("kh", &kp);
4222244b
EW
165 if ((j = tgetnum("kn")) >= 0) {
166 char cap[32];
3e0824a9 167
4222244b 168 (void) sprintf(kp, "kn#%d:", j);
3e0824a9
EW
169 for (; *kp; kp++)
170 ;
4222244b 171 for (i = 1; i <= j; i++) {
bb05dfb5 172 (void) sprintf(cap, "k%d", i);
3f32a92a 173 wwaddcap(cap, &kp);
3e0824a9 174 cap[0] = 'l';
3f32a92a 175 wwaddcap(cap, &kp);
3e0824a9
EW
176 }
177 }
cd9c5720
EW
178 /*
179 * It's ok to do this here even if setenv() is destructive
180 * since tt_init() has already made its own copy of it and
181 * wwterm now points to the copy.
182 */
91b689b9 183 (void) setenv("TERM", WWT_TERM, 1);
4222244b 184
7c38ad4d 185 (void) sigsetmask(s);
ab8b3b31
EW
186 /* catch typeahead before ASYNC was set */
187 (void) kill(getpid(), SIGIO);
188 xxstart();
4711df8b 189 return 0;
bb05dfb5 190bad:
861cd1ed
EW
191 /*
192 * Don't bother to free storage. We're supposed
193 * to exit when wwinit fails anyway.
194 */
4cbd8755 195 (void) wwsettty(0, &wwoldtty, &wwnewtty);
b1189050 196 (void) signal(SIGIO, SIG_DFL);
7c38ad4d 197 (void) sigsetmask(s);
bb05dfb5 198 return -1;
5528a91e 199}
3e0824a9 200
3f32a92a 201wwaddcap(cap, kp)
cf28fdac
EW
202 register char *cap;
203 register char **kp;
3e0824a9 204{
bb05dfb5
EW
205 char tbuf[512];
206 char *tp = tbuf;
9ad71678 207 register char *str, *p;
3e0824a9
EW
208
209 if ((str = tgetstr(cap, &tp)) != 0) {
4222244b 210 while (*(*kp)++ = *cap++)
3e0824a9 211 ;
4222244b 212 (*kp)[-1] = '=';
9ad71678 213 while (*str) {
4222244b 214 for (p = unctrl(*str++); *(*kp)++ = *p++;)
9ad71678 215 ;
4222244b 216 (*kp)--;
9ad71678 217 }
4222244b
EW
218 *(*kp)++ = ':';
219 **kp = 0;
3e0824a9
EW
220 }
221}
cf28fdac
EW
222
223wwaddcap1(cap, kp)
224 register char *cap;
225 register char **kp;
226{
227 while (*(*kp)++ = *cap++)
228 ;
229 (*kp)--;
230}