Commit | Line | Data |
---|---|---|
5528a91e | 1 | #ifndef lint |
4222244b | 2 | static char *sccsid = "@(#)wwinit.c 3.16 84/03/23"; |
5528a91e EW |
3 | #endif |
4 | ||
5 | #include "ww.h" | |
e908bfac | 6 | #include "tt.h" |
b1189050 EW |
7 | #include <sys/signal.h> |
8 | #include <fcntl.h> | |
5528a91e | 9 | |
5528a91e EW |
10 | wwinit() |
11 | { | |
bb05dfb5 | 12 | register i, j; |
4222244b EW |
13 | char *kp; |
14 | register char **p, **q; | |
15 | char **env, **termcap; | |
16 | extern char **environ; | |
4711df8b | 17 | |
57fc9db6 | 18 | #ifndef O_4_1A |
bb05dfb5 | 19 | wwdtablesize = getdtablesize(); |
57fc9db6 EW |
20 | #else |
21 | #include <sys/param.h> | |
22 | wwdtablesize = NOFILE; | |
23 | #endif | |
4222244b EW |
24 | wwhead.ww_forw = &wwhead; |
25 | wwhead.ww_back = &wwhead; | |
b1189050 EW |
26 | |
27 | if (signal(SIGIO, wwrint) == BADSIG) | |
28 | return -1; | |
29 | (void) sighold(SIGIO); | |
30 | ||
4711df8b EW |
31 | if (wwgettty(0, &wwoldtty) < 0) |
32 | return -1; | |
c44edccd EW |
33 | wwwintty = wwoldtty; |
34 | wwwintty.ww_sgttyb.sg_flags &= ~XTABS; | |
4222244b EW |
35 | wwnewtty.ww_sgttyb = wwoldtty.ww_sgttyb; |
36 | wwnewtty.ww_sgttyb.sg_erase = -1; | |
37 | wwnewtty.ww_sgttyb.sg_kill = -1; | |
4711df8b | 38 | wwnewtty.ww_sgttyb.sg_flags |= CBREAK; |
2b44d852 | 39 | wwnewtty.ww_sgttyb.sg_flags &= ~(ECHO|CRMOD); |
4222244b EW |
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 | ||
4711df8b | 56 | if (wwsettty(0, &wwnewtty) < 0) |
bb05dfb5 EW |
57 | goto bad; |
58 | ||
03e75950 EW |
59 | if ((wwterm = getenv("TERM")) == 0) { |
60 | wwerrno = WWE_BADTERM; | |
bb05dfb5 | 61 | goto bad; |
03e75950 EW |
62 | } |
63 | if (tgetent(wwtermcap, wwterm) != 1) { | |
64 | wwerrno = WWE_BADTERM; | |
bb05dfb5 | 65 | goto bad; |
03e75950 | 66 | } |
bb05dfb5 EW |
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; | |
7d77e730 | 73 | wwavailmodes = tt.tt_availmodes; |
5e785082 | 74 | wwwrap = tt.tt_wrap; |
e908bfac | 75 | (*tt.tt_init)(); |
bb05dfb5 | 76 | |
73218728 EW |
77 | if (wwavailmodes & WWM_REV) |
78 | wwcursormodes = WWM_REV | wwavailmodes & WWM_BLK; | |
79 | else if (wwavailmodes & WWM_UL) | |
80 | wwcursormodes = WWM_UL; | |
81 | ||
8fa6d94c EW |
82 | if ((wwib = malloc((unsigned) 512)) == 0) |
83 | goto bad; | |
84 | wwibe = wwib + 512; | |
b1189050 | 85 | wwibq = wwibp = wwib; |
8fa6d94c | 86 | |
f2a77fe1 | 87 | if ((wwsmap = wwalloc(0, 0, wwnrow, wwncol, sizeof (char))) == 0) |
bb05dfb5 EW |
88 | goto bad; |
89 | for (i = 0; i < wwnrow; i++) | |
90 | for (j = 0; j < wwncol; j++) | |
91 | wwsmap[i][j] = WWX_NOBODY; | |
0896e17e | 92 | |
bb05dfb5 | 93 | wwos = (union ww_char **) |
f2a77fe1 | 94 | wwalloc(0, 0, wwnrow, wwncol, sizeof (union ww_char)); |
bb05dfb5 EW |
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 **) | |
f2a77fe1 | 101 | wwalloc(0, 0, wwnrow, wwncol, sizeof (union ww_char)); |
bb05dfb5 EW |
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 | ||
861cd1ed | 108 | wwtouched = malloc((unsigned) wwnrow); |
03e75950 EW |
109 | if (wwtouched == 0) { |
110 | wwerrno = WWE_NOMEM; | |
861cd1ed | 111 | goto bad; |
03e75950 | 112 | } |
861cd1ed EW |
113 | for (i = 0; i < wwnrow; i++) |
114 | wwtouched[i] = 0; | |
115 | ||
bb05dfb5 EW |
116 | wwindex[WWX_NOBODY] = &wwnobody; |
117 | wwnobody.ww_order = NWW; | |
3e0824a9 | 118 | |
4222244b EW |
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]; | |
3e0824a9 EW |
128 | int i; |
129 | ||
4222244b | 130 | (void) sprintf(kp, "kn#%d:", j); |
3e0824a9 EW |
131 | for (; *kp; kp++) |
132 | ; | |
4222244b | 133 | for (i = 1; i <= j; i++) { |
bb05dfb5 | 134 | (void) sprintf(cap, "k%d", i); |
4222244b | 135 | addcap(cap, &kp); |
3e0824a9 | 136 | cap[0] = 'l'; |
4222244b | 137 | addcap(cap, &kp); |
3e0824a9 EW |
138 | } |
139 | } | |
4222244b EW |
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 | ||
b1189050 | 156 | (void) sigrelse(SIGIO); |
4711df8b | 157 | return 0; |
bb05dfb5 | 158 | bad: |
861cd1ed EW |
159 | /* |
160 | * Don't bother to free storage. We're supposed | |
161 | * to exit when wwinit fails anyway. | |
162 | */ | |
bb05dfb5 | 163 | (void) wwsettty(0, &wwoldtty); |
b1189050 EW |
164 | (void) signal(SIGIO, SIG_DFL); |
165 | (void) sigrelse(SIGIO); | |
bb05dfb5 | 166 | return -1; |
5528a91e | 167 | } |
3e0824a9 | 168 | |
bb05dfb5 | 169 | static |
4222244b | 170 | addcap(cap, kp) |
3e0824a9 | 171 | register char *cap; |
4222244b | 172 | register char **kp; |
3e0824a9 | 173 | { |
bb05dfb5 EW |
174 | char tbuf[512]; |
175 | char *tp = tbuf; | |
9ad71678 | 176 | register char *str, *p; |
3e0824a9 EW |
177 | |
178 | if ((str = tgetstr(cap, &tp)) != 0) { | |
4222244b | 179 | while (*(*kp)++ = *cap++) |
3e0824a9 | 180 | ; |
4222244b | 181 | (*kp)[-1] = '='; |
9ad71678 | 182 | while (*str) { |
4222244b | 183 | for (p = unctrl(*str++); *(*kp)++ = *p++;) |
9ad71678 | 184 | ; |
4222244b | 185 | (*kp)--; |
9ad71678 | 186 | } |
4222244b EW |
187 | *(*kp)++ = ':'; |
188 | **kp = 0; | |
3e0824a9 EW |
189 | } |
190 | } |