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