home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / window / wwinit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-18  |  8.0 KB  |  333 lines

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