delay set modes and insert.
[unix-history] / usr / src / usr.bin / window / wwinit.c
CommitLineData
5528a91e 1#ifndef lint
2357b64e 2static char *sccsid = "@(#)wwinit.c 3.8 83/08/19";
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;
7d77e730 44 wwavailmodes = tt.tt_availmodes;
5e785082 45 wwwrap = tt.tt_wrap;
e908bfac 46 (*tt.tt_init)();
bb05dfb5 47
73218728
EW
48 if (wwavailmodes & WWM_REV)
49 wwcursormodes = WWM_REV | wwavailmodes & WWM_BLK;
50 else if (wwavailmodes & WWM_UL)
51 wwcursormodes = WWM_UL;
52
bb05dfb5
EW
53 if ((wwsmap = wwalloc(wwnrow, wwncol, sizeof (char))) == 0)
54 goto bad;
55 for (i = 0; i < wwnrow; i++)
56 for (j = 0; j < wwncol; j++)
57 wwsmap[i][j] = WWX_NOBODY;
0896e17e 58
bb05dfb5
EW
59 wwos = (union ww_char **)
60 wwalloc(wwnrow, wwncol, sizeof (union ww_char));
61 if (wwos == 0)
62 goto bad;
63 for (i = 0; i < wwnrow; i++)
64 for (j = 0; j < wwncol; j++)
65 wwos[i][j].c_w = ' ';
66 wwns = (union ww_char **)
67 wwalloc(wwnrow, wwncol, sizeof (union ww_char));
68 if (wwns == 0)
69 goto bad;
70 for (i = 0; i < wwnrow; i++)
71 for (j = 0; j < wwncol; j++)
72 wwns[i][j].c_w = ' ';
73
861cd1ed
EW
74 wwtouched = malloc((unsigned) wwnrow);
75 if (wwtouched == 0)
76 goto bad;
77 for (i = 0; i < wwnrow; i++)
78 wwtouched[i] = 0;
79
bb05dfb5
EW
80 wwindex[WWX_NOBODY] = &wwnobody;
81 wwnobody.ww_order = NWW;
3e0824a9 82
3e0824a9
EW
83 addcap("kb");
84 addcap("ku");
85 addcap("kd");
86 addcap("kl");
87 addcap("kr");
88 addcap("kh");
89 if ((kn = tgetnum("kn")) >= 0) {
90 char cap[5];
91 int i;
92
bb05dfb5 93 (void) sprintf(kp, "kn#%d:", kn);
3e0824a9
EW
94 for (; *kp; kp++)
95 ;
96 for (i = 1; i <= kn; i++) {
bb05dfb5 97 (void) sprintf(cap, "k%d", i);
3e0824a9
EW
98 addcap(cap);
99 cap[0] = 'l';
100 addcap(cap);
101 }
102 }
4711df8b 103 return 0;
bb05dfb5 104bad:
861cd1ed
EW
105 /*
106 * Don't bother to free storage. We're supposed
107 * to exit when wwinit fails anyway.
108 */
bb05dfb5
EW
109 (void) wwsettty(0, &wwoldtty);
110 return -1;
5528a91e 111}
3e0824a9 112
bb05dfb5 113static
3e0824a9
EW
114addcap(cap)
115register char *cap;
116{
bb05dfb5
EW
117 char tbuf[512];
118 char *tp = tbuf;
9ad71678 119 register char *str, *p;
3e0824a9
EW
120
121 if ((str = tgetstr(cap, &tp)) != 0) {
122 while (*kp++ = *cap++)
123 ;
124 kp[-1] = '=';
9ad71678
EW
125 while (*str) {
126 for (p = unctrl(*str++); *kp++ = *p++;)
127 ;
128 kp--;
129 }
130 *kp++ = ':';
3e0824a9
EW
131 *kp = 0;
132 }
133}