compile with -R
[unix-history] / usr / src / usr.bin / window / wwinit.c
... / ...
CommitLineData
1#ifndef lint
2static char *sccsid = "@(#)wwinit.c 3.16 84/03/23";
3#endif
4
5#include "ww.h"
6#include "tt.h"
7#include <sys/signal.h>
8#include <fcntl.h>
9
10wwinit()
11{
12 register i, j;
13 char *kp;
14 register char **p, **q;
15 char **env, **termcap;
16 extern char **environ;
17
18#ifndef O_4_1A
19 wwdtablesize = getdtablesize();
20#else
21#include <sys/param.h>
22 wwdtablesize = NOFILE;
23#endif
24 wwhead.ww_forw = &wwhead;
25 wwhead.ww_back = &wwhead;
26
27 if (signal(SIGIO, wwrint) == BADSIG)
28 return -1;
29 (void) sighold(SIGIO);
30
31 if (wwgettty(0, &wwoldtty) < 0)
32 return -1;
33 wwwintty = wwoldtty;
34 wwwintty.ww_sgttyb.sg_flags &= ~XTABS;
35 wwnewtty.ww_sgttyb = wwoldtty.ww_sgttyb;
36 wwnewtty.ww_sgttyb.sg_erase = -1;
37 wwnewtty.ww_sgttyb.sg_kill = -1;
38 wwnewtty.ww_sgttyb.sg_flags |= CBREAK;
39 wwnewtty.ww_sgttyb.sg_flags &= ~(ECHO|CRMOD);
40 wwnewtty.ww_tchars.t_intrc = -1;
41 wwnewtty.ww_tchars.t_quitc = -1;
42 wwnewtty.ww_tchars.t_startc = -1;
43 wwnewtty.ww_tchars.t_stopc = -1;
44 wwnewtty.ww_tchars.t_eofc = -1;
45 wwnewtty.ww_tchars.t_brkc = -1;
46 wwnewtty.ww_ltchars.t_suspc = -1;
47 wwnewtty.ww_ltchars.t_dsuspc = -1;
48 wwnewtty.ww_ltchars.t_rprntc = -1;
49 wwnewtty.ww_ltchars.t_flushc = -1;
50 wwnewtty.ww_ltchars.t_werasc = -1;
51 wwnewtty.ww_ltchars.t_lnextc = -1;
52 wwnewtty.ww_lmode = wwoldtty.ww_lmode | LLITOUT;
53 wwnewtty.ww_ldisc = wwoldtty.ww_ldisc;
54 wwnewtty.ww_fflags = wwoldtty.ww_fflags | FASYNC;
55
56 if (wwsettty(0, &wwnewtty) < 0)
57 goto bad;
58
59 if ((wwterm = getenv("TERM")) == 0) {
60 wwerrno = WWE_BADTERM;
61 goto bad;
62 }
63 if (tgetent(wwtermcap, wwterm) != 1) {
64 wwerrno = WWE_BADTERM;
65 goto bad;
66 }
67 wwbaud = wwbaudmap[wwoldtty.ww_sgttyb.sg_ospeed];
68
69 if (ttinit() < 0)
70 goto bad;
71 wwnrow = tt.tt_nrow;
72 wwncol = tt.tt_ncol;
73 wwavailmodes = tt.tt_availmodes;
74 wwwrap = tt.tt_wrap;
75 (*tt.tt_init)();
76
77 if (wwavailmodes & WWM_REV)
78 wwcursormodes = WWM_REV | wwavailmodes & WWM_BLK;
79 else if (wwavailmodes & WWM_UL)
80 wwcursormodes = WWM_UL;
81
82 if ((wwib = malloc((unsigned) 512)) == 0)
83 goto bad;
84 wwibe = wwib + 512;
85 wwibq = wwibp = wwib;
86
87 if ((wwsmap = wwalloc(0, 0, wwnrow, wwncol, sizeof (char))) == 0)
88 goto bad;
89 for (i = 0; i < wwnrow; i++)
90 for (j = 0; j < wwncol; j++)
91 wwsmap[i][j] = WWX_NOBODY;
92
93 wwos = (union ww_char **)
94 wwalloc(0, 0, wwnrow, wwncol, sizeof (union ww_char));
95 if (wwos == 0)
96 goto bad;
97 for (i = 0; i < wwnrow; i++)
98 for (j = 0; j < wwncol; j++)
99 wwos[i][j].c_w = ' ';
100 wwns = (union ww_char **)
101 wwalloc(0, 0, wwnrow, wwncol, sizeof (union ww_char));
102 if (wwns == 0)
103 goto bad;
104 for (i = 0; i < wwnrow; i++)
105 for (j = 0; j < wwncol; j++)
106 wwns[i][j].c_w = ' ';
107
108 wwtouched = malloc((unsigned) wwnrow);
109 if (wwtouched == 0) {
110 wwerrno = WWE_NOMEM;
111 goto bad;
112 }
113 for (i = 0; i < wwnrow; i++)
114 wwtouched[i] = 0;
115
116 wwindex[WWX_NOBODY] = &wwnobody;
117 wwnobody.ww_order = NWW;
118
119 kp = wwkeys;
120 addcap("kb", &kp);
121 addcap("ku", &kp);
122 addcap("kd", &kp);
123 addcap("kl", &kp);
124 addcap("kr", &kp);
125 addcap("kh", &kp);
126 if ((j = tgetnum("kn")) >= 0) {
127 char cap[32];
128 int i;
129
130 (void) sprintf(kp, "kn#%d:", j);
131 for (; *kp; kp++)
132 ;
133 for (i = 1; i <= j; i++) {
134 (void) sprintf(cap, "k%d", i);
135 addcap(cap, &kp);
136 cap[0] = 'l';
137 addcap(cap, &kp);
138 }
139 }
140 for (i = 0, p = environ; *p++; i++)
141 ;
142 if ((env = (char **)malloc((unsigned)(i + 3) * sizeof (char *))) == 0)
143 goto bad;
144 for (p = environ, q = env; *p; p++, q++) {
145 if (strncmp(*p, "TERM=", 5) == 0)
146 *q = WWT_TERM;
147 else if (strncmp(*p, "TERMCAP=", 8) == 0)
148 termcap = q;
149 else
150 *q = *p;
151 }
152 *(termcap ? termcap : q++) = wwwintermcap;
153 *q = 0;
154 environ = env;
155
156 (void) sigrelse(SIGIO);
157 return 0;
158bad:
159 /*
160 * Don't bother to free storage. We're supposed
161 * to exit when wwinit fails anyway.
162 */
163 (void) wwsettty(0, &wwoldtty);
164 (void) signal(SIGIO, SIG_DFL);
165 (void) sigrelse(SIGIO);
166 return -1;
167}
168
169static
170addcap(cap, kp)
171register char *cap;
172register char **kp;
173{
174 char tbuf[512];
175 char *tp = tbuf;
176 register char *str, *p;
177
178 if ((str = tgetstr(cap, &tp)) != 0) {
179 while (*(*kp)++ = *cap++)
180 ;
181 (*kp)[-1] = '=';
182 while (*str) {
183 for (p = unctrl(*str++); *(*kp)++ = *p++;)
184 ;
185 (*kp)--;
186 }
187 *(*kp)++ = ':';
188 **kp = 0;
189 }
190}