faster and better frames
[unix-history] / usr / src / usr.bin / window / wwinit.c
CommitLineData
5528a91e 1#ifndef lint
0896e17e 2static char *sccsid = "@(#)wwinit.c 3.2 83/08/12";
5528a91e
EW
3#endif
4
5#include "ww.h"
6
bb05dfb5 7static char *kp = wwkeys;
30647f51 8extern char _sobuf[];
5528a91e
EW
9
10wwinit()
11{
bb05dfb5 12 register i, j;
3e0824a9 13 int kn;
4711df8b 14
30647f51 15 setbuf(stdout, _sobuf);
bb05dfb5 16 wwdtablesize = getdtablesize();
4711df8b
EW
17 if (wwgettty(0, &wwoldtty) < 0)
18 return -1;
c44edccd
EW
19 wwwintty = wwoldtty;
20 wwwintty.ww_sgttyb.sg_flags &= ~XTABS;
4711df8b
EW
21 wwnewtty.ww_sgttyb.sg_flags = wwoldtty.ww_sgttyb.sg_flags;
22 wwnewtty.ww_sgttyb.sg_ispeed = wwoldtty.ww_sgttyb.sg_ispeed;
23 wwnewtty.ww_sgttyb.sg_ospeed = wwoldtty.ww_sgttyb.sg_ospeed;
24 wwnewtty.ww_lmode = wwoldtty.ww_lmode;
25 wwnewtty.ww_pgrp = wwoldtty.ww_pgrp;
26 wwnewtty.ww_ldisc = wwoldtty.ww_ldisc;
27 wwnewtty.ww_sgttyb.sg_flags |= CBREAK;
2b44d852 28 wwnewtty.ww_sgttyb.sg_flags &= ~(ECHO|CRMOD);
4711df8b 29 wwnewtty.ww_lmode |= LLITOUT;
4711df8b 30 if (wwsettty(0, &wwnewtty) < 0)
bb05dfb5
EW
31 goto bad;
32
33 if ((wwterm = getenv("TERM")) == 0)
34 goto bad;
35 if (tgetent(wwtermcap, wwterm) != 1)
36 goto bad;
37 wwbaud = wwbaudmap[wwoldtty.ww_sgttyb.sg_ospeed];
38
39 if (ttinit() < 0)
40 goto bad;
41 wwnrow = tt.tt_nrow;
42 wwncol = tt.tt_ncol;
43 (*tt.tt_reset)();
44 (*tt.tt_clreos)();
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
72 wwindex[WWX_NOBODY] = &wwnobody;
73 wwnobody.ww_order = NWW;
3e0824a9 74
3e0824a9
EW
75 addcap("kb");
76 addcap("ku");
77 addcap("kd");
78 addcap("kl");
79 addcap("kr");
80 addcap("kh");
81 if ((kn = tgetnum("kn")) >= 0) {
82 char cap[5];
83 int i;
84
bb05dfb5 85 (void) sprintf(kp, "kn#%d:", kn);
3e0824a9
EW
86 for (; *kp; kp++)
87 ;
88 for (i = 1; i <= kn; i++) {
bb05dfb5 89 (void) sprintf(cap, "k%d", i);
3e0824a9
EW
90 addcap(cap);
91 cap[0] = 'l';
92 addcap(cap);
93 }
94 }
4711df8b 95 return 0;
bb05dfb5
EW
96bad:
97 (void) wwsettty(0, &wwoldtty);
98 return -1;
5528a91e 99}
3e0824a9 100
bb05dfb5 101static
3e0824a9
EW
102addcap(cap)
103register char *cap;
104{
bb05dfb5
EW
105 char tbuf[512];
106 char *tp = tbuf;
9ad71678 107 register char *str, *p;
3e0824a9
EW
108
109 if ((str = tgetstr(cap, &tp)) != 0) {
110 while (*kp++ = *cap++)
111 ;
112 kp[-1] = '=';
9ad71678
EW
113 while (*str) {
114 for (p = unctrl(*str++); *kp++ = *p++;)
115 ;
116 kp--;
117 }
118 *kp++ = ':';
3e0824a9
EW
119 *kp = 0;
120 }
121}