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