smart update: do it by chunks.
[unix-history] / usr / src / usr.bin / window / wwinit.c
CommitLineData
5528a91e 1#ifndef lint
861cd1ed 2static char *sccsid = "@(#)wwinit.c 3.4 83/08/16";
5528a91e
EW
3#endif
4
5#include "ww.h"
e908bfac 6#include "tt.h"
5528a91e 7
bb05dfb5 8static char *kp = wwkeys;
30647f51 9extern char _sobuf[];
5528a91e
EW
10
11wwinit()
12{
bb05dfb5 13 register i, j;
3e0824a9 14 int kn;
4711df8b 15
30647f51 16 setbuf(stdout, _sobuf);
bb05dfb5 17 wwdtablesize = getdtablesize();
4711df8b
EW
18 if (wwgettty(0, &wwoldtty) < 0)
19 return -1;
c44edccd
EW
20 wwwintty = wwoldtty;
21 wwwintty.ww_sgttyb.sg_flags &= ~XTABS;
4711df8b
EW
22 wwnewtty.ww_sgttyb.sg_flags = wwoldtty.ww_sgttyb.sg_flags;
23 wwnewtty.ww_sgttyb.sg_ispeed = wwoldtty.ww_sgttyb.sg_ispeed;
24 wwnewtty.ww_sgttyb.sg_ospeed = wwoldtty.ww_sgttyb.sg_ospeed;
25 wwnewtty.ww_lmode = wwoldtty.ww_lmode;
26 wwnewtty.ww_pgrp = wwoldtty.ww_pgrp;
27 wwnewtty.ww_ldisc = wwoldtty.ww_ldisc;
28 wwnewtty.ww_sgttyb.sg_flags |= CBREAK;
2b44d852 29 wwnewtty.ww_sgttyb.sg_flags &= ~(ECHO|CRMOD);
4711df8b 30 wwnewtty.ww_lmode |= LLITOUT;
4711df8b 31 if (wwsettty(0, &wwnewtty) < 0)
bb05dfb5
EW
32 goto bad;
33
34 if ((wwterm = getenv("TERM")) == 0)
35 goto bad;
36 if (tgetent(wwtermcap, wwterm) != 1)
37 goto bad;
38 wwbaud = wwbaudmap[wwoldtty.ww_sgttyb.sg_ospeed];
39
40 if (ttinit() < 0)
41 goto bad;
42 wwnrow = tt.tt_nrow;
43 wwncol = tt.tt_ncol;
e908bfac 44 (*tt.tt_init)();
bb05dfb5
EW
45
46 if ((wwsmap = wwalloc(wwnrow, wwncol, sizeof (char))) == 0)
47 goto bad;
48 for (i = 0; i < wwnrow; i++)
49 for (j = 0; j < wwncol; j++)
50 wwsmap[i][j] = WWX_NOBODY;
0896e17e
EW
51 if ((wwfmap = wwalloc(wwnrow, wwncol, sizeof (char))) == 0)
52 goto bad;
53 for (i = 0; i < wwnrow; i++)
54 for (j = 0; j < wwncol; j++)
55 wwfmap[i][j] = 0;
56
bb05dfb5
EW
57 wwos = (union ww_char **)
58 wwalloc(wwnrow, wwncol, sizeof (union ww_char));
59 if (wwos == 0)
60 goto bad;
61 for (i = 0; i < wwnrow; i++)
62 for (j = 0; j < wwncol; j++)
63 wwos[i][j].c_w = ' ';
64 wwns = (union ww_char **)
65 wwalloc(wwnrow, wwncol, sizeof (union ww_char));
66 if (wwns == 0)
67 goto bad;
68 for (i = 0; i < wwnrow; i++)
69 for (j = 0; j < wwncol; j++)
70 wwns[i][j].c_w = ' ';
71
861cd1ed
EW
72 wwtouched = malloc((unsigned) wwnrow);
73 if (wwtouched == 0)
74 goto bad;
75 for (i = 0; i < wwnrow; i++)
76 wwtouched[i] = 0;
77
bb05dfb5
EW
78 wwindex[WWX_NOBODY] = &wwnobody;
79 wwnobody.ww_order = NWW;
3e0824a9 80
3e0824a9
EW
81 addcap("kb");
82 addcap("ku");
83 addcap("kd");
84 addcap("kl");
85 addcap("kr");
86 addcap("kh");
87 if ((kn = tgetnum("kn")) >= 0) {
88 char cap[5];
89 int i;
90
bb05dfb5 91 (void) sprintf(kp, "kn#%d:", kn);
3e0824a9
EW
92 for (; *kp; kp++)
93 ;
94 for (i = 1; i <= kn; i++) {
bb05dfb5 95 (void) sprintf(cap, "k%d", i);
3e0824a9
EW
96 addcap(cap);
97 cap[0] = 'l';
98 addcap(cap);
99 }
100 }
4711df8b 101 return 0;
bb05dfb5 102bad:
861cd1ed
EW
103 /*
104 * Don't bother to free storage. We're supposed
105 * to exit when wwinit fails anyway.
106 */
bb05dfb5
EW
107 (void) wwsettty(0, &wwoldtty);
108 return -1;
5528a91e 109}
3e0824a9 110
bb05dfb5 111static
3e0824a9
EW
112addcap(cap)
113register char *cap;
114{
bb05dfb5
EW
115 char tbuf[512];
116 char *tp = tbuf;
9ad71678 117 register char *str, *p;
3e0824a9
EW
118
119 if ((str = tgetstr(cap, &tp)) != 0) {
120 while (*kp++ = *cap++)
121 ;
122 kp[-1] = '=';
9ad71678
EW
123 while (*str) {
124 for (p = unctrl(*str++); *kp++ = *p++;)
125 ;
126 kp--;
127 }
128 *kp++ = ':';
3e0824a9
EW
129 *kp = 0;
130 }
131}