Annotation of src/usr.bin/window/wwinit.c, Revision 1.1
1.1 ! deraadt 1: /* $NetBSD: wwinit.c,v 1.7 1995/09/28 10:35:33 tls Exp $ */
! 2:
! 3: /*
! 4: * Copyright (c) 1983, 1993
! 5: * The Regents of the University of California. All rights reserved.
! 6: *
! 7: * This code is derived from software contributed to Berkeley by
! 8: * Edward Wang at The University of California, Berkeley.
! 9: *
! 10: * Redistribution and use in source and binary forms, with or without
! 11: * modification, are permitted provided that the following conditions
! 12: * are met:
! 13: * 1. Redistributions of source code must retain the above copyright
! 14: * notice, this list of conditions and the following disclaimer.
! 15: * 2. Redistributions in binary form must reproduce the above copyright
! 16: * notice, this list of conditions and the following disclaimer in the
! 17: * documentation and/or other materials provided with the distribution.
! 18: * 3. All advertising materials mentioning features or use of this software
! 19: * must display the following acknowledgement:
! 20: * This product includes software developed by the University of
! 21: * California, Berkeley and its contributors.
! 22: * 4. Neither the name of the University nor the names of its contributors
! 23: * may be used to endorse or promote products derived from this software
! 24: * without specific prior written permission.
! 25: *
! 26: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
! 27: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
! 28: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
! 29: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
! 30: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
! 31: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
! 32: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
! 33: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
! 34: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
! 35: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
! 36: * SUCH DAMAGE.
! 37: */
! 38:
! 39: #ifndef lint
! 40: #if 0
! 41: static char sccsid[] = "@(#)wwinit.c 8.2 (Berkeley) 4/28/95";
! 42: #else
! 43: static char rcsid[] = "$NetBSD: wwinit.c,v 1.7 1995/09/28 10:35:33 tls Exp $";
! 44: #endif
! 45: #endif /* not lint */
! 46:
! 47: #include "ww.h"
! 48: #include "tt.h"
! 49: #include <sys/signal.h>
! 50: #include <fcntl.h>
! 51: #include "char.h"
! 52:
! 53: wwinit()
! 54: {
! 55: register i, j;
! 56: char *kp;
! 57: int s;
! 58:
! 59: wwdtablesize = 3;
! 60: wwhead.ww_forw = &wwhead;
! 61: wwhead.ww_back = &wwhead;
! 62:
! 63: s = sigblock(sigmask(SIGIO) | sigmask(SIGCHLD) | sigmask(SIGALRM) |
! 64: sigmask(SIGHUP) | sigmask(SIGTERM));
! 65: if (signal(SIGIO, wwrint) == BADSIG ||
! 66: signal(SIGCHLD, wwchild) == BADSIG ||
! 67: signal(SIGHUP, wwquit) == BADSIG ||
! 68: signal(SIGTERM, wwquit) == BADSIG ||
! 69: signal(SIGPIPE, SIG_IGN) == BADSIG) {
! 70: wwerrno = WWE_SYS;
! 71: return -1;
! 72: }
! 73:
! 74: if (wwgettty(0, &wwoldtty) < 0)
! 75: return -1;
! 76: wwwintty = wwoldtty;
! 77: #ifdef OLD_TTY
! 78: wwwintty.ww_sgttyb.sg_flags &= ~XTABS;
! 79: wwnewtty.ww_sgttyb = wwoldtty.ww_sgttyb;
! 80: wwnewtty.ww_sgttyb.sg_erase = -1;
! 81: wwnewtty.ww_sgttyb.sg_kill = -1;
! 82: wwnewtty.ww_sgttyb.sg_flags |= CBREAK;
! 83: wwnewtty.ww_sgttyb.sg_flags &= ~(ECHO|CRMOD);
! 84: wwnewtty.ww_tchars.t_intrc = -1;
! 85: wwnewtty.ww_tchars.t_quitc = -1;
! 86: wwnewtty.ww_tchars.t_startc = -1;
! 87: wwnewtty.ww_tchars.t_stopc = -1;
! 88: wwnewtty.ww_tchars.t_eofc = -1;
! 89: wwnewtty.ww_tchars.t_brkc = -1;
! 90: wwnewtty.ww_ltchars.t_suspc = -1;
! 91: wwnewtty.ww_ltchars.t_dsuspc = -1;
! 92: wwnewtty.ww_ltchars.t_rprntc = -1;
! 93: wwnewtty.ww_ltchars.t_flushc = -1;
! 94: wwnewtty.ww_ltchars.t_werasc = -1;
! 95: wwnewtty.ww_ltchars.t_lnextc = -1;
! 96: wwnewtty.ww_lmode = wwoldtty.ww_lmode | LLITOUT;
! 97: wwnewtty.ww_ldisc = wwoldtty.ww_ldisc;
! 98: #else
! 99: #ifndef OXTABS
! 100: #define OXTABS XTABS
! 101: #endif
! 102: #ifndef _POSIX_VDISABLE
! 103: #define _POSIX_VDISABLE -1
! 104: #endif
! 105: wwwintty.ww_termios.c_oflag &= ~OXTABS;
! 106: wwnewtty.ww_termios = wwoldtty.ww_termios;
! 107: wwnewtty.ww_termios.c_iflag &=
! 108: ~(ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXOFF | IMAXBEL);
! 109: wwnewtty.ww_termios.c_oflag = 0;
! 110: wwnewtty.ww_termios.c_cflag &= ~(CSIZE | PARENB);
! 111: wwnewtty.ww_termios.c_cflag |= CS8;
! 112: wwnewtty.ww_termios.c_lflag = 0;
! 113: for (i = 0; i < NCCS; i++)
! 114: wwnewtty.ww_termios.c_cc[i] = _POSIX_VDISABLE;
! 115: wwnewtty.ww_termios.c_cc[VMIN] = 1;
! 116: wwnewtty.ww_termios.c_cc[VTIME] = 0;
! 117: #endif
! 118: wwnewtty.ww_fflags = wwoldtty.ww_fflags | FASYNC;
! 119: if (wwsettty(0, &wwnewtty) < 0)
! 120: goto bad;
! 121:
! 122: if ((wwterm = getenv("TERM")) == 0) {
! 123: wwerrno = WWE_BADTERM;
! 124: goto bad;
! 125: }
! 126: if (tgetent(wwtermcap, wwterm) != 1) {
! 127: wwerrno = WWE_BADTERM;
! 128: goto bad;
! 129: }
! 130: #ifdef OLD_TTY
! 131: wwospeed = wwoldtty.ww_sgttyb.sg_ospeed;
! 132: #else
! 133: wwospeed = cfgetospeed(&wwoldtty.ww_termios);
! 134: wwbaud = wwospeed;
! 135: #endif
! 136: switch (wwospeed) {
! 137: default:
! 138: case B0:
! 139: wwbaud = 0;
! 140: break;
! 141: case B50:
! 142: wwbaud = 50;
! 143: break;
! 144: case B75:
! 145: wwbaud = 75;
! 146: break;
! 147: case B110:
! 148: wwbaud = 110;
! 149: break;
! 150: case B134:
! 151: wwbaud = 134;
! 152: break;
! 153: case B150:
! 154: wwbaud = 150;
! 155: break;
! 156: case B200:
! 157: wwbaud = 200;
! 158: break;
! 159: case B300:
! 160: wwbaud = 300;
! 161: break;
! 162: case B600:
! 163: wwbaud = 600;
! 164: break;
! 165: case B1200:
! 166: wwbaud = 1200;
! 167: break;
! 168: case B1800:
! 169: wwbaud = 1800;
! 170: break;
! 171: case B2400:
! 172: wwbaud = 2400;
! 173: break;
! 174: case B4800:
! 175: wwbaud = 4800;
! 176: break;
! 177: case B9600:
! 178: wwbaud = 9600;
! 179: break;
! 180: #ifdef B19200
! 181: case B19200:
! 182: #else
! 183: case EXTA:
! 184: #endif
! 185: wwbaud = 19200;
! 186: break;
! 187: #ifdef B38400
! 188: case B38400:
! 189: #else
! 190: case EXTB:
! 191: #endif
! 192: wwbaud = 38400;
! 193: break;
! 194: #ifdef B57600
! 195: case B57600:
! 196: wwbaud= 57600;
! 197: break;
! 198: #endif
! 199: #ifdef B115200
! 200: case B115200:
! 201: wwbaud = 115200;
! 202: break;
! 203: #endif
! 204: }
! 205:
! 206: if (xxinit() < 0)
! 207: goto bad;
! 208: wwnrow = tt.tt_nrow;
! 209: wwncol = tt.tt_ncol;
! 210: wwavailmodes = tt.tt_availmodes;
! 211: wwwrap = tt.tt_wrap;
! 212:
! 213: if (wwavailmodes & WWM_REV)
! 214: wwcursormodes = WWM_REV | wwavailmodes & WWM_BLK;
! 215: else if (wwavailmodes & WWM_UL)
! 216: wwcursormodes = WWM_UL;
! 217:
! 218: if ((wwib = malloc((unsigned) 512)) == 0)
! 219: goto bad;
! 220: wwibe = wwib + 512;
! 221: wwibq = wwibp = wwib;
! 222:
! 223: if ((wwsmap = wwalloc(0, 0, wwnrow, wwncol, sizeof (char))) == 0)
! 224: goto bad;
! 225: for (i = 0; i < wwnrow; i++)
! 226: for (j = 0; j < wwncol; j++)
! 227: wwsmap[i][j] = WWX_NOBODY;
! 228:
! 229: wwos = (union ww_char **)
! 230: wwalloc(0, 0, wwnrow, wwncol, sizeof (union ww_char));
! 231: if (wwos == 0)
! 232: goto bad;
! 233: /* wwos is cleared in wwstart1() */
! 234: wwns = (union ww_char **)
! 235: wwalloc(0, 0, wwnrow, wwncol, sizeof (union ww_char));
! 236: if (wwns == 0)
! 237: goto bad;
! 238: for (i = 0; i < wwnrow; i++)
! 239: for (j = 0; j < wwncol; j++)
! 240: wwns[i][j].c_w = ' ';
! 241: if (tt.tt_checkpoint) {
! 242: /* wwcs is also cleared in wwstart1() */
! 243: wwcs = (union ww_char **)
! 244: wwalloc(0, 0, wwnrow, wwncol, sizeof (union ww_char));
! 245: if (wwcs == 0)
! 246: goto bad;
! 247: }
! 248:
! 249: wwtouched = malloc((unsigned) wwnrow);
! 250: if (wwtouched == 0) {
! 251: wwerrno = WWE_NOMEM;
! 252: goto bad;
! 253: }
! 254: for (i = 0; i < wwnrow; i++)
! 255: wwtouched[i] = 0;
! 256:
! 257: wwupd = (struct ww_update *) malloc((unsigned) wwnrow * sizeof *wwupd);
! 258: if (wwupd == 0) {
! 259: wwerrno = WWE_NOMEM;
! 260: goto bad;
! 261: }
! 262:
! 263: wwindex[WWX_NOBODY] = &wwnobody;
! 264: wwnobody.ww_order = NWW;
! 265:
! 266: kp = wwwintermcap;
! 267: if (wwavailmodes & WWM_REV)
! 268: wwaddcap1(WWT_REV, &kp);
! 269: if (wwavailmodes & WWM_BLK)
! 270: wwaddcap1(WWT_BLK, &kp);
! 271: if (wwavailmodes & WWM_UL)
! 272: wwaddcap1(WWT_UL, &kp);
! 273: if (wwavailmodes & WWM_GRP)
! 274: wwaddcap1(WWT_GRP, &kp);
! 275: if (wwavailmodes & WWM_DIM)
! 276: wwaddcap1(WWT_DIM, &kp);
! 277: if (wwavailmodes & WWM_USR)
! 278: wwaddcap1(WWT_USR, &kp);
! 279: if (tt.tt_insline && tt.tt_delline || tt.tt_setscroll)
! 280: wwaddcap1(WWT_ALDL, &kp);
! 281: if (tt.tt_inschar)
! 282: wwaddcap1(WWT_IMEI, &kp);
! 283: if (tt.tt_insspace)
! 284: wwaddcap1(WWT_IC, &kp);
! 285: if (tt.tt_delchar)
! 286: wwaddcap1(WWT_DC, &kp);
! 287: wwaddcap("kb", &kp);
! 288: wwaddcap("ku", &kp);
! 289: wwaddcap("kd", &kp);
! 290: wwaddcap("kl", &kp);
! 291: wwaddcap("kr", &kp);
! 292: wwaddcap("kh", &kp);
! 293: if ((j = tgetnum("kn")) >= 0) {
! 294: char cap[32];
! 295:
! 296: (void) sprintf(kp, "kn#%d:", j);
! 297: for (; *kp; kp++)
! 298: ;
! 299: for (i = 1; i <= j; i++) {
! 300: (void) sprintf(cap, "k%d", i);
! 301: wwaddcap(cap, &kp);
! 302: cap[0] = 'l';
! 303: wwaddcap(cap, &kp);
! 304: }
! 305: }
! 306: /*
! 307: * It's ok to do this here even if setenv() is destructive
! 308: * since tt_init() has already made its own copy of it and
! 309: * wwterm now points to the copy.
! 310: */
! 311: (void) setenv("TERM", WWT_TERM, 1);
! 312: #ifdef TERMINFO
! 313: if (wwterminfoinit() < 0)
! 314: goto bad;
! 315: #endif
! 316:
! 317: if (tt.tt_checkpoint)
! 318: if (signal(SIGALRM, wwalarm) == BADSIG) {
! 319: wwerrno = WWE_SYS;
! 320: goto bad;
! 321: }
! 322: /* catch typeahead before ASYNC was set */
! 323: (void) kill(getpid(), SIGIO);
! 324: wwstart1();
! 325: (void) sigsetmask(s);
! 326: return 0;
! 327: bad:
! 328: /*
! 329: * Don't bother to free storage. We're supposed
! 330: * to exit when wwinit fails anyway.
! 331: */
! 332: (void) wwsettty(0, &wwoldtty);
! 333: (void) signal(SIGIO, SIG_DFL);
! 334: (void) sigsetmask(s);
! 335: return -1;
! 336: }
! 337:
! 338: wwaddcap(cap, kp)
! 339: register char *cap;
! 340: register char **kp;
! 341: {
! 342: char tbuf[512];
! 343: char *tp = tbuf;
! 344: register char *str, *p;
! 345:
! 346: if ((str = tgetstr(cap, &tp)) != 0) {
! 347: while (*(*kp)++ = *cap++)
! 348: ;
! 349: (*kp)[-1] = '=';
! 350: while (*str) {
! 351: for (p = unctrl(*str++); *(*kp)++ = *p++;)
! 352: ;
! 353: (*kp)--;
! 354: }
! 355: *(*kp)++ = ':';
! 356: **kp = 0;
! 357: }
! 358: }
! 359:
! 360: wwaddcap1(cap, kp)
! 361: register char *cap;
! 362: register char **kp;
! 363: {
! 364: while (*(*kp)++ = *cap++)
! 365: ;
! 366: (*kp)--;
! 367: }
! 368:
! 369: wwstart()
! 370: {
! 371: register i;
! 372:
! 373: (void) wwsettty(0, &wwnewtty);
! 374: for (i = 0; i < wwnrow; i++)
! 375: wwtouched[i] = WWU_TOUCHED;
! 376: wwstart1();
! 377: }
! 378:
! 379: wwstart1()
! 380: {
! 381: register i, j;
! 382:
! 383: for (i = 0; i < wwnrow; i++)
! 384: for (j = 0; j < wwncol; j++) {
! 385: wwos[i][j].c_w = ' ';
! 386: if (tt.tt_checkpoint)
! 387: wwcs[i][j].c_w = ' ';
! 388: }
! 389: xxstart();
! 390: if (tt.tt_checkpoint)
! 391: wwdocheckpoint = 1;
! 392: }
! 393:
! 394: /*
! 395: * Reset data structures and terminal from an unknown state.
! 396: * Restoring wwos has been taken care of elsewhere.
! 397: */
! 398: wwreset()
! 399: {
! 400: register i;
! 401:
! 402: xxreset();
! 403: for (i = 0; i < wwnrow; i++)
! 404: wwtouched[i] = WWU_TOUCHED;
! 405: }