1 /* 2 * Copyright (c) 1983 Regents of the University of California. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Edward Wang at The University of California, Berkeley. 7 * 8 * %sccs.include.redist.c% 9 */ 10 11 #ifndef lint 12 static char sccsid[] = "@(#)wwinit.c 3.39 (Berkeley) 06/06/90"; 13 #endif /* not lint */ 14 15 #include "ww.h" 16 #include "tt.h" 17 #include <sys/signal.h> 18 #include <fcntl.h> 19 #include "char.h" 20 21 wwinit() 22 { 23 register i, j; 24 char *kp; 25 int s; 26 27 wwdtablesize = getdtablesize(); 28 wwhead.ww_forw = &wwhead; 29 wwhead.ww_back = &wwhead; 30 31 s = sigblock(sigmask(SIGIO)); 32 if (signal(SIGIO, wwrint) == BADSIG || 33 signal(SIGCHLD, wwchild) == BADSIG || 34 signal(SIGPIPE, SIG_IGN) == BADSIG) { 35 wwerrno = WWE_SYS; 36 return -1; 37 } 38 39 if (wwgettty(0, &wwoldtty) < 0) 40 return -1; 41 wwwintty = wwoldtty; 42 #ifndef POSIX_TTY 43 wwwintty.ww_sgttyb.sg_flags &= ~XTABS; 44 wwnewtty.ww_sgttyb = wwoldtty.ww_sgttyb; 45 wwnewtty.ww_sgttyb.sg_erase = -1; 46 wwnewtty.ww_sgttyb.sg_kill = -1; 47 wwnewtty.ww_sgttyb.sg_flags |= CBREAK; 48 wwnewtty.ww_sgttyb.sg_flags &= ~(ECHO|CRMOD); 49 wwnewtty.ww_tchars.t_intrc = -1; 50 wwnewtty.ww_tchars.t_quitc = -1; 51 wwnewtty.ww_tchars.t_startc = -1; 52 wwnewtty.ww_tchars.t_stopc = -1; 53 wwnewtty.ww_tchars.t_eofc = -1; 54 wwnewtty.ww_tchars.t_brkc = -1; 55 wwnewtty.ww_ltchars.t_suspc = -1; 56 wwnewtty.ww_ltchars.t_dsuspc = -1; 57 wwnewtty.ww_ltchars.t_rprntc = -1; 58 wwnewtty.ww_ltchars.t_flushc = -1; 59 wwnewtty.ww_ltchars.t_werasc = -1; 60 wwnewtty.ww_ltchars.t_lnextc = -1; 61 wwnewtty.ww_lmode = wwoldtty.ww_lmode | LLITOUT; 62 wwnewtty.ww_ldisc = wwoldtty.ww_ldisc; 63 #else 64 wwwintty.ww_termios.c_oflag &= ~OXTABS; 65 wwnewtty.ww_termios = wwoldtty.ww_termios; 66 wwnewtty.ww_termios.c_iflag &= 67 ~(ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXOFF | IMAXBEL); 68 wwnewtty.ww_termios.c_iflag |= INPCK; 69 wwnewtty.ww_termios.c_oflag = 0; 70 wwnewtty.ww_termios.c_cflag &= ~(CSIZE | PARENB); 71 wwnewtty.ww_termios.c_cflag |= CS8; 72 wwnewtty.ww_termios.c_lflag = 0; 73 for (i = 0; i < NCC; i++) 74 wwnewtty.ww_termios.c_cc[i] = _POSIX_VDISABLE; 75 #endif 76 wwnewtty.ww_fflags = wwoldtty.ww_fflags | FASYNC; 77 if (wwsettty(0, &wwnewtty, &wwoldtty) < 0) 78 goto bad; 79 80 if ((wwterm = getenv("TERM")) == 0) { 81 wwerrno = WWE_BADTERM; 82 goto bad; 83 } 84 if (tgetent(wwtermcap, wwterm) != 1) { 85 wwerrno = WWE_BADTERM; 86 goto bad; 87 } 88 #ifndef POSIX_TTY 89 wwbaud = wwbaudmap[wwoldtty.ww_sgttyb.sg_ospeed]; 90 #else 91 #ifdef CBAUD 92 wwbaud = wwbaudmap[wwoldtty.ww_termios.c_cflag & CBAUD]; 93 #else 94 wwbaud = wwoldtty.ww_termios.c_ospeed; 95 #endif 96 #endif 97 98 if (xxinit() < 0) 99 goto bad; 100 wwnrow = tt.tt_nrow; 101 wwncol = tt.tt_ncol; 102 wwavailmodes = tt.tt_availmodes; 103 wwwrap = tt.tt_wrap; 104 105 if (wwavailmodes & WWM_REV) 106 wwcursormodes = WWM_REV | wwavailmodes & WWM_BLK; 107 else if (wwavailmodes & WWM_UL) 108 wwcursormodes = WWM_UL; 109 110 if ((wwib = malloc((unsigned) 512)) == 0) 111 goto bad; 112 wwibe = wwib + 512; 113 wwibq = wwibp = wwib; 114 115 if ((wwsmap = wwalloc(0, 0, wwnrow, wwncol, sizeof (char))) == 0) 116 goto bad; 117 for (i = 0; i < wwnrow; i++) 118 for (j = 0; j < wwncol; j++) 119 wwsmap[i][j] = WWX_NOBODY; 120 121 wwos = (union ww_char **) 122 wwalloc(0, 0, wwnrow, wwncol, sizeof (union ww_char)); 123 if (wwos == 0) 124 goto bad; 125 for (i = 0; i < wwnrow; i++) 126 for (j = 0; j < wwncol; j++) 127 wwos[i][j].c_w = ' '; 128 wwns = (union ww_char **) 129 wwalloc(0, 0, wwnrow, wwncol, sizeof (union ww_char)); 130 if (wwns == 0) 131 goto bad; 132 for (i = 0; i < wwnrow; i++) 133 for (j = 0; j < wwncol; j++) 134 wwns[i][j].c_w = ' '; 135 136 wwtouched = malloc((unsigned) wwnrow); 137 if (wwtouched == 0) { 138 wwerrno = WWE_NOMEM; 139 goto bad; 140 } 141 for (i = 0; i < wwnrow; i++) 142 wwtouched[i] = 0; 143 144 wwupd = (struct ww_update *) malloc((unsigned) wwnrow * sizeof *wwupd); 145 if (wwupd == 0) { 146 wwerrno = WWE_NOMEM; 147 goto bad; 148 } 149 150 wwindex[WWX_NOBODY] = &wwnobody; 151 wwnobody.ww_order = NWW; 152 153 kp = wwwintermcap; 154 if (wwavailmodes & WWM_REV) 155 wwaddcap1(WWT_REV, &kp); 156 if (wwavailmodes & WWM_BLK) 157 wwaddcap1(WWT_BLK, &kp); 158 if (wwavailmodes & WWM_UL) 159 wwaddcap1(WWT_UL, &kp); 160 if (wwavailmodes & WWM_GRP) 161 wwaddcap1(WWT_GRP, &kp); 162 if (wwavailmodes & WWM_DIM) 163 wwaddcap1(WWT_DIM, &kp); 164 if (wwavailmodes & WWM_USR) 165 wwaddcap1(WWT_USR, &kp); 166 if (tt.tt_insline && tt.tt_delline || tt.tt_setscroll) 167 wwaddcap1(WWT_ALDL, &kp); 168 if (tt.tt_inschar) 169 wwaddcap1(WWT_IMEI, &kp); 170 if (tt.tt_insspace) 171 wwaddcap1(WWT_IC, &kp); 172 if (tt.tt_delchar) 173 wwaddcap1(WWT_DC, &kp); 174 wwaddcap("kb", &kp); 175 wwaddcap("ku", &kp); 176 wwaddcap("kd", &kp); 177 wwaddcap("kl", &kp); 178 wwaddcap("kr", &kp); 179 wwaddcap("kh", &kp); 180 if ((j = tgetnum("kn")) >= 0) { 181 char cap[32]; 182 183 (void) sprintf(kp, "kn#%d:", j); 184 for (; *kp; kp++) 185 ; 186 for (i = 1; i <= j; i++) { 187 (void) sprintf(cap, "k%d", i); 188 wwaddcap(cap, &kp); 189 cap[0] = 'l'; 190 wwaddcap(cap, &kp); 191 } 192 } 193 /* 194 * It's ok to do this here even if setenv() is destructive 195 * since tt_init() has already made its own copy of it and 196 * wwterm now points to the copy. 197 */ 198 (void) setenv("TERM", WWT_TERM, 1); 199 200 (void) sigsetmask(s); 201 /* catch typeahead before ASYNC was set */ 202 (void) kill(getpid(), SIGIO); 203 xxstart(); 204 return 0; 205 bad: 206 /* 207 * Don't bother to free storage. We're supposed 208 * to exit when wwinit fails anyway. 209 */ 210 (void) wwsettty(0, &wwoldtty, &wwnewtty); 211 (void) signal(SIGIO, SIG_DFL); 212 (void) sigsetmask(s); 213 return -1; 214 } 215 216 wwaddcap(cap, kp) 217 register char *cap; 218 register char **kp; 219 { 220 char tbuf[512]; 221 char *tp = tbuf; 222 register char *str, *p; 223 224 if ((str = tgetstr(cap, &tp)) != 0) { 225 while (*(*kp)++ = *cap++) 226 ; 227 (*kp)[-1] = '='; 228 while (*str) { 229 for (p = unctrl(*str++); *(*kp)++ = *p++;) 230 ; 231 (*kp)--; 232 } 233 *(*kp)++ = ':'; 234 **kp = 0; 235 } 236 } 237 238 wwaddcap1(cap, kp) 239 register char *cap; 240 register char **kp; 241 { 242 while (*(*kp)++ = *cap++) 243 ; 244 (*kp)--; 245 } 246