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