home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / lib / libcurses / setterm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-23  |  5.4 KB  |  214 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[] = "@(#)setterm.c    5.9 (Berkeley) 8/23/92";
  36. #endif /* not lint */
  37.  
  38. #include <sys/ioctl.h>
  39.  
  40. #include <curses.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <unistd.h>
  44.  
  45. static void zap __P((void));
  46.  
  47. static char    *sflags[] = {
  48.             &AM, &BS, &DA, &EO, &HC, &HZ, &IN, &MI, &MS,
  49.             &NC, &NS, &OS, &UL, &XB, &XN, &XT, &XS, &XX
  50.         };
  51.  
  52. static char    *_PC,
  53.         **sstrs[] = {
  54.             &AL, &BC, &BT, &CD, &CE, &CL, &CM, &CR, &CS,
  55.             &DC, &DL, &DM, &DO, &ED, &EI, &K0, &K1, &K2,
  56.             &K3, &K4, &K5, &K6, &K7, &K8, &K9, &HO, &IC,
  57.             &IM, &IP, &KD, &KE, &KH, &KL, &KR, &KS, &KU,
  58.             &LL, &MA, &ND, &NL, &_PC, &RC, &SC, &SE, &SF,
  59.             &SO, &SR, &TA, &TE, &TI, &UC, &UE, &UP, &US,
  60.             &VB, &VS, &VE, &AL_PARM, &DL_PARM, &UP_PARM,
  61.             &DOWN_PARM, &LEFT_PARM, &RIGHT_PARM,
  62.         };
  63.  
  64. static char    *aoftspace;        /* Address of _tspace for relocation */
  65. static char    tspace[2048];        /* Space for capability strings */
  66.  
  67. static int    destcol, destline;
  68.  
  69. char *ttytype;
  70.  
  71. int
  72. setterm(type)
  73.     register char *type;
  74. {
  75.     static char genbuf[1024];
  76.     static char __ttytype[1024];
  77.     register int unknown;
  78.     struct winsize win;
  79.     char *p;
  80.  
  81. #ifdef DEBUG
  82.     __TRACE("setterm: (\"%s\")\nLINES = %d, COLS = %d\n",
  83.         type, LINES, COLS);
  84. #endif
  85.     if (type[0] == '\0')
  86.         type = "xx";
  87.     unknown = 0;
  88.     if (tgetent(genbuf, type) != 1) {
  89.         unknown++;
  90.         strcpy(genbuf, "xx|dumb:");
  91.     }
  92. #ifdef DEBUG
  93.     __TRACE("setterm: tty = %s\n", type);
  94. #endif
  95.  
  96.     /* Try TIOCGWINSZ, and, if it fails, the termcap entry. */
  97.     if (ioctl(STDERR_FILENO, TIOCGWINSZ, &win) != -1 &&
  98.         win.ws_row != 0 && win.ws_col != 0) {
  99.         LINES = win.ws_row;
  100.         COLS = win.ws_col;
  101.     }  else {
  102.         LINES = tgetnum("li");
  103.         COLS = tgetnum("co");
  104.     }
  105.  
  106.     /* POSIX 1003.2 requires that the environment override. */
  107.     if ((p = getenv("ROWS")) != NULL)
  108.         LINES = strtol(p, NULL, 10);
  109.     if ((p = getenv("COLUMNS")) != NULL)
  110.         COLS = strtol(p, NULL, 10);
  111.  
  112.     /*
  113.      * XXX
  114.      * Historically, curses fails if rows <= 5, cols <= 4.
  115.      */
  116.     if (LINES <= 5 || COLS <= 4)
  117.         return (ERR);
  118.  
  119. #ifdef DEBUG
  120.     __TRACE("setterm: LINES = %d, COLS = %d\n", LINES, COLS);
  121. #endif
  122.     aoftspace = tspace;
  123.     zap();            /* Get terminal description. */
  124.  
  125.     /* Handle funny termcap capabilities. */
  126.     if (CS && SC && RC)
  127.         AL = DL = "";
  128.     if (AL_PARM && AL == NULL)
  129.         AL = "";
  130.     if (DL_PARM && DL == NULL)
  131.         DL = "";
  132.     if (IC) {
  133.         if (IM == NULL)
  134.             IM = "";
  135.         if (EI == NULL)
  136.             EI = "";
  137.     }
  138.     if (!GT)        /* If we can't tab, we can't backtab either. */
  139.         BT = NULL;
  140.  
  141.     if (tgoto(CM, destcol, destline)[0] == 'O') {
  142.         CA = 0;
  143.         CM = 0;
  144.     } else
  145.         CA = 1;
  146.  
  147.     PC = _PC ? _PC[0] : 0;
  148.     aoftspace = tspace;
  149.     ttytype = longname(genbuf, __ttytype);
  150.  
  151.     return (unknown ? ERR : OK);
  152. }
  153.  
  154. /*
  155.  * zap --
  156.  *    Gets all the terminal flags from the termcap database.
  157.  */
  158. static void
  159. zap()
  160. {
  161.     register char *namp, ***sp;
  162.     register char **fp;
  163. #ifdef DEBUG
  164.     register char    *cp;
  165. #endif
  166.  
  167.     namp = "ambsdadbeohchzinmimsncnsosulxbxnxtxsxx";
  168.     fp = sflags;
  169.     do {
  170.         *(*fp++) = tgetflag(namp);
  171. #ifdef DEBUG
  172.         __TRACE("2.2s = %s\n", namp, *fp[-1] ? "TRUE" : "FALSE");
  173. #endif
  174.         namp += 2;
  175.     } while (*namp);
  176.     namp = "albcbtcdceclcmcrcsdcdldmdoedeik0k1k2k3k4k5k6k7k8k9hoicimipkdkekhklkrkskullmandnlpcrcscsesfsosrtatetiucueupusvbvsveALDLUPDOLERI";
  177.     sp = sstrs;
  178.     do {
  179.         *(*sp++) = tgetstr(namp, &aoftspace);
  180. #ifdef DEBUG
  181.         __TRACE("2.2s = %s", namp, *sp[-1] == NULL ? "NULL\n" : "\"");
  182.         if (*sp[-1] != NULL) {
  183.             for (cp = *sp[-1]; *cp; cp++)
  184.                 __TRACE("%s", unctrl(*cp));
  185.             __TRACE("\"\n");
  186.         }
  187. #endif
  188.         namp += 2;
  189.     } while (*namp);
  190.     if (XS)
  191.         SO = SE = NULL;
  192.     else {
  193.         if (tgetnum("sg") > 0)
  194.             SO = NULL;
  195.         if (tgetnum("ug") > 0)
  196.             US = NULL;
  197.         if (!SO && US) {
  198.             SO = US;
  199.             SE = UE;
  200.         }
  201.     }
  202. }
  203.  
  204. /*
  205.  * getcap --
  206.  *    Return a capability from termcap.
  207.  */
  208. char *
  209. getcap(name)
  210.     char *name;
  211. {
  212.     return (tgetstr(name, &aoftspace));
  213. }
  214.