home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / curses-old / cr_tty.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-01  |  6.9 KB  |  275 lines

  1. /*
  2.  * Copyright (c) 1981 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. static char sccsid[] = "@(#)cr_tty.c    5.8 (Berkeley) 6/1/90";
  36. #endif /* not lint */
  37.  
  38. void zap ();
  39.  
  40. /*
  41.  * Terminal initialization routines.
  42.  *
  43.  */
  44.  
  45. # include    "curses.ext"
  46.  
  47. static bool    *sflags[] = {
  48.             &AM, &BS, &DA, &DB, &EO, &HC, &HZ, &IN, &MI,
  49.             &MS, &NC, &NS, &OS, &UL, &XB, &XN, &XT, &XS,
  50.             &XX
  51.         };
  52.  
  53. static char    *_PC,
  54.         **sstrs[] = {
  55.             &AL, &BC, &BT, &CD, &CE, &CL, &CM, &CR, &CS,
  56.             &DC, &DL, &DM, &DO, &ED, &EI, &K0, &K1, &K2,
  57.             &K3, &K4, &K5, &K6, &K7, &K8, &K9, &HO, &IC,
  58.             &IM, &IP, &KD, &KE, &KH, &KL, &KR, &KS, &KU,
  59.             &LL, &MA, &ND, &NL, &_PC, &RC, &SC, &SE, &SF,
  60.             &SO, &SR, &TA, &TE, &TI, &UC, &UE, &UP, &US,
  61.             &VB, &VS, &VE, &AL_PARM, &DL_PARM, &UP_PARM,
  62.             &DOWN_PARM, &LEFT_PARM, &RIGHT_PARM,
  63.         };
  64.  
  65. extern char    *tgoto();
  66.  
  67. char        _tspace[2048];        /* Space for capability strings */
  68.  
  69. static char    *aoftspace;        /* Address of _tspace for relocation */
  70.  
  71. static int    destcol, destline;
  72.  
  73. /*
  74.  *    This routine does terminal type initialization routines, and
  75.  * calculation of flags at entry.  It is almost entirely stolen from
  76.  * Bill Joy's ex version 2.6.
  77.  */
  78. #ifndef linux
  79. #ifdef POSIX
  80. speed_t    ospeed = -1;
  81. #else
  82. short    ospeed = -1;
  83. #endif
  84. #endif
  85.  
  86. gettmode() {
  87.  
  88. #ifdef POSIX
  89.     if (tcgetattr(_tty_ch, &_tty) < 0)
  90.         return;
  91.     savetty();
  92.     if (tcsetattr(_tty_ch, 0, &_tty) < 0) {
  93.         _tty.c_lflag = _res_lflg;
  94.         _tty.c_iflag = _res_iflg;
  95.     }
  96.     ospeed = cfgetospeed(&_tty);
  97.     _res_iflg = _tty.c_iflag;
  98.     _res_lflg = _tty.c_lflag;
  99. #if defined(IUCLC) && defined(OLCUC) && defined(XCASE)
  100.     UPPERCASE = ((_tty.c_iflag & IUCLC) ||
  101.              (_tty.c_oflag & OLCUC) ||
  102.              (_tty.c_lflag & XCASE));
  103. #else
  104.     UPPERCASE = 0;
  105. #endif
  106.     GT = ((_tty.c_oflag & XTABS) == 0);
  107.     NONL = ((_tty.c_iflag & ICRNL) == 0);
  108.     _tty.c_oflag &= ~XTABS;
  109.     tcsetattr(_tty_ch, 0, &_tty);
  110. #else
  111.     if (ioctl(_tty_ch, TIOCGETP, &_tty) < 0)
  112.         return;
  113.     savetty();
  114.     if (ioctl(_tty_ch, TIOCSETP, &_tty) < 0)
  115.         _tty.sg_flags = _res_flg;
  116.     ospeed = _tty.sg_ospeed;
  117.     _res_flg = _tty.sg_flags;
  118.     UPPERCASE = (_tty.sg_flags & LCASE) != 0;
  119.     GT = ((_tty.sg_flags & XTABS) == 0);
  120.     NONL = ((_tty.sg_flags & CRMOD) == 0);
  121.     _tty.sg_flags &= ~XTABS;
  122.     ioctl(_tty_ch, TIOCSETP, &_tty);
  123. #endif
  124. # ifdef DEBUG
  125.     fprintf(outf, "GETTMODE: UPPERCASE = %s\n", UPPERCASE ? "TRUE":"FALSE");
  126.     fprintf(outf, "GETTMODE: GT = %s\n", GT ? "TRUE" : "FALSE");
  127.     fprintf(outf, "GETTMODE: NONL = %s\n", NONL ? "TRUE" : "FALSE");
  128.     fprintf(outf, "GETTMODE: ospeed = %d\n", ospeed);
  129. # endif
  130. }
  131.  
  132. setterm(type)
  133. register const char    *type; {
  134.  
  135.     register int        unknown;
  136.     static char    genbuf[1024];
  137. # ifdef TIOCGWINSZ
  138.     struct winsize win;
  139. # endif
  140.  
  141. # ifdef DEBUG
  142.     fprintf(outf, "SETTERM(\"%s\")\n", type);
  143.     fprintf(outf, "SETTERM: LINES = %d, COLS = %d\n", LINES, COLS);
  144. # endif
  145.     if (type[0] == '\0')
  146.         type = "xx";
  147.     unknown = FALSE;
  148.     if (tgetent(genbuf, type) != 1) {
  149.         unknown++;
  150.         strcpy(genbuf, "xx|dumb:");
  151.     }
  152. # ifdef DEBUG
  153.     fprintf(outf, "SETTERM: tty = %s\n", type);
  154. # endif
  155. # ifdef TIOCGWINSZ
  156.     if (ioctl(_tty_ch, TIOCGWINSZ, &win) >= 0) {
  157.         if (LINES == 0)
  158.             LINES = win.ws_row;
  159.         if (COLS == 0)
  160.             COLS = win.ws_col;
  161.     }
  162. # endif
  163.  
  164.     if (LINES == 0)
  165.         LINES = tgetnum("li");
  166.     if (LINES <= 5)
  167.         LINES = 24;
  168.  
  169.     if (COLS == 0)
  170.         COLS = tgetnum("co");
  171.     if (COLS <= 4)
  172.         COLS = 80;
  173.  
  174. # ifdef DEBUG
  175.     fprintf(outf, "SETTERM: LINES = %d, COLS = %d\n", LINES, COLS);
  176. # endif
  177.     aoftspace = _tspace;
  178.     zap();            /* get terminal description        */
  179.  
  180.     /*
  181.      * Handle funny termcap capabilities
  182.      */
  183.     if (CS && SC && RC) AL=DL="";
  184.     if (AL_PARM && AL==NULL) AL="";
  185.     if (DL_PARM && DL==NULL) DL="";
  186.     if (IC && IM==NULL) IM="";
  187.     if (IC && EI==NULL) EI="";
  188.     if (!GT) BT=NULL;    /* If we can't tab, we can't backtab either */
  189.  
  190.     if (tgoto(CM, destcol, destline)[0] == 'O')
  191.         CA = FALSE, CM = 0;
  192.     else
  193.         CA = TRUE;
  194.  
  195.     PC = _PC ? _PC[0] : FALSE;
  196.     aoftspace = _tspace;
  197.     {
  198.         /* xtype should be the same size as genbuf for longname(). */
  199.         static char xtype[1024];
  200.  
  201.         (void)strcpy(xtype,type);
  202.         strncpy(ttytype, longname(genbuf, xtype), sizeof(ttytype) - 1);
  203.     }
  204.     ttytype[sizeof(ttytype) - 1] = '\0';
  205.     if (unknown)
  206.         return ERR;
  207.     return OK;
  208. }
  209.  
  210. /*
  211.  *    This routine gets all the terminal flags from the termcap database
  212.  */
  213.  
  214. void
  215. zap()
  216. {
  217.     register char    *namp;
  218.     register bool    **fp;
  219.     register char    ***sp;
  220. #ifdef    DEBUG
  221.     register char    *cp;
  222. #endif
  223. #ifndef linux
  224.     extern char    *tgetstr();
  225. #endif
  226.  
  227.     namp = "ambsdadbeohchzinmimsncnsosulxbxnxtxsxx";
  228.     fp = sflags;
  229.     do {
  230.         *(*fp++) = tgetflag(namp);
  231. #ifdef DEBUG
  232.         fprintf(outf, "%2.2s = %s\n", namp, *fp[-1] ? "TRUE" : "FALSE");
  233. #endif
  234.         namp += 2;
  235.     } while (*namp);
  236.     namp = "albcbtcdceclcmcrcsdcdldmdoedeik0k1k2k3k4k5k6k7k8k9hoicimipkdkekhklkrkskullmandnlpcrcscsesfsosrtatetiucueupusvbvsveALDLUPDOLERI";
  237.     sp = sstrs;
  238.     do {
  239.         *(*sp++) = tgetstr(namp, &aoftspace);
  240. #ifdef DEBUG
  241.         fprintf(outf, "%2.2s = %s", namp, *sp[-1] == NULL ? "NULL\n" : "\"");
  242.         if (*sp[-1] != NULL) {
  243.             for (cp = *sp[-1]; *cp; cp++)
  244.                 fprintf(outf, "%s", unctrl(*cp));
  245.             fprintf(outf, "\"\n");
  246.         }
  247. #endif
  248.         namp += 2;
  249.     } while (*namp);
  250.     if (XS)
  251.         SO = SE = NULL;
  252.     else {
  253.         if (tgetnum("sg") > 0)
  254.             SO = NULL;
  255.         if (tgetnum("ug") > 0)
  256.             US = NULL;
  257.         if (!SO && US) {
  258.             SO = US;
  259.             SE = UE;
  260.         }
  261.     }
  262. }
  263.  
  264. /*
  265.  * return a capability from termcap
  266.  */
  267. char *
  268. getcap(name)
  269. char *name;
  270. {
  271.     char *tgetstr();
  272.  
  273.     return tgetstr(name, &aoftspace);
  274. }
  275.