home *** CD-ROM | disk | FTP | other *** search
/ Computer Club Elmshorn Atari PD / CCE_PD.iso / pc / 0600 / CCE_0636.ZIP / CCE_0636 / CURSES / CRSSRC12.ZOO / src / cr_tty.c < prev    next >
C/C++ Source or Header  |  1991-09-27  |  5KB  |  216 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 are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Berkeley.  The name of the
  11.  * University may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  */
  17.  
  18. #ifndef lint
  19. static char sccsid[] = "@(#)cr_tty.c    5.4 (Berkeley) 6/30/88";
  20. #endif /* not lint */
  21.  
  22. /*
  23.  * Terminal initialization routines.
  24.  *
  25.  */
  26.  
  27. # include    "curses.ext"
  28. # include    <string.h>
  29. # include    <termcap.h>
  30.  
  31. static bool    *sflags[] = {
  32.             &AM, &BS, &DA, &DB, &EO, &HC, &HZ, &IN, &MI,
  33.             &MS, &NC, &NS, &OS, &UL, &XB, &XN, &XT, &XS,
  34.             &XX
  35.         };
  36.  
  37. static char    *_PC,
  38.         **sstrs[] = {
  39.             &AL, &BC, &BT, &CD, &CE, &CL, &CM, &CR, &CS,
  40.             &DC, &DL, &DM, &DO, &ED, &EI, &K0, &K1, &K2,
  41.             &K3, &K4, &K5, &K6, &K7, &K8, &K9, &HO, &IC,
  42.             &IM, &IP, &KD, &KE, &KH, &KL, &KR, &KS, &KU,
  43.             &LL, &MA, &ND, &NL, &_PC, &RC, &SC, &SE, &SF,
  44.             &SO, &SR, &TA, &TE, &TI, &UC, &UE, &UP, &US,
  45.             &VB, &VS, &VE, &AL_PARM, &DL_PARM, &UP_PARM,
  46.             &DOWN_PARM, &LEFT_PARM, &RIGHT_PARM,
  47.             };
  48.  
  49. char        _tspace[2048];        /* Space for capability strings */
  50.  
  51. static char    *aoftspace;        /* Address of _tspace for relocation */
  52.  
  53. static int    destcol, destline;
  54.  
  55. /*
  56.  *    This routine does terminal type initialization routines, and
  57.  * calculation of flags at entry.  It is almost entirely stolen from
  58.  * Bill Joy's ex version 2.6.
  59.  */
  60. short    ospeed = -1;
  61.  
  62. void gettmode() {
  63.  
  64.     if (gtty(_tty_ch, &_tty) < 0)
  65.         return;
  66.     savetty();
  67.     if (stty(_tty_ch, &_tty) < 0)
  68.         _tty.sg_flags = _res_flg;
  69.     ospeed = _tty.sg_ospeed;
  70.     _res_flg = _tty.sg_flags;
  71.     UPPERCASE = (_tty.sg_flags & LCASE) != 0;
  72.     GT = ((_tty.sg_flags & XTABS) == 0);
  73.     NONL = ((_tty.sg_flags & CRMOD) == 0);
  74.     _tty.sg_flags &= ~XTABS;
  75.     stty(_tty_ch, &_tty);
  76. # ifdef DEBUG
  77.     fprintf(outf, "GETTMODE: UPPERCASE = %s\n", UPPERCASE ? "TRUE":"FALSE");
  78.     fprintf(outf, "GETTMODE: GT = %s\n", GT ? "TRUE" : "FALSE");
  79.     fprintf(outf, "GETTMODE: NONL = %s\n", NONL ? "TRUE" : "FALSE");
  80.     fprintf(outf, "GETTMODE: ospeed = %d\n", ospeed);
  81. # endif
  82. }
  83.  
  84. int setterm(type)
  85. reg char    *type; {
  86.  
  87.     reg int        unknown;
  88.     static char    genbuf[1024];
  89. # ifdef TIOCGWINSZ
  90.     struct winsize win;
  91. # endif
  92.  
  93. # ifdef DEBUG
  94.     fprintf(outf, "SETTERM(\"%s\")\n", type);
  95.     fprintf(outf, "SETTERM: LINES = %d, COLS = %d\n", LINES, COLS);
  96. # endif
  97.     if (type[0] == '\0')
  98.         type = "xx";
  99.     unknown = FALSE;
  100.     if (tgetent(genbuf, type) != 1) {
  101.         unknown++;
  102.         strcpy(genbuf, "xx|dumb:");
  103.     }
  104. # ifdef DEBUG
  105.     fprintf(outf, "SETTERM: tty = %s\n", type);
  106. # endif
  107. # ifdef TIOCGWINSZ
  108.     if (ioctl(_tty_ch, TIOCGWINSZ, &win) >= 0) {
  109.         if (LINES == 0)
  110.             LINES = win.ws_row;
  111.         if (COLS == 0)
  112.             COLS = win.ws_col;
  113.     }
  114. # endif
  115.  
  116.     if (LINES == 0)
  117.         LINES = tgetnum("li");
  118.     if (LINES <= 5)
  119.         LINES = 24;
  120.  
  121.     if (COLS == 0)
  122.         COLS = tgetnum("co");
  123.     if (COLS <= 4)
  124.         COLS = 80;
  125.  
  126. # ifdef DEBUG
  127.     fprintf(outf, "SETTERM: LINES = %d, COLS = %d\n", LINES, COLS);
  128. # endif
  129.     aoftspace = _tspace;
  130.     zap();            /* get terminal description        */
  131.  
  132.     /*
  133.      * Handle funny termcap capabilities
  134.      */
  135.     if (CS && SC && RC) AL=DL="";
  136.     if (AL_PARM && AL==NULL) AL="";
  137.     if (DL_PARM && DL==NULL) DL="";
  138.     if (IC && IM==NULL) IM="";
  139.     if (IC && EI==NULL) EI="";
  140.     if (!GT) BT=NULL;    /* If we can't tab, we can't backtab either */
  141.  
  142.     if (tgoto(CM, destcol, destline)[0] == 'O')
  143.         CA = FALSE, CM = 0;
  144.     else
  145.         CA = TRUE;
  146.  
  147.     PC = _PC ? _PC[0] : FALSE;
  148.     aoftspace = _tspace;
  149.     strncpy(ttytype, longname(genbuf, type), sizeof(ttytype) - 1);
  150.     ttytype[sizeof(ttytype) - 1] = '\0';
  151.     if (unknown)
  152.         return ERR;
  153.     return OK;
  154. }
  155.  
  156. /*
  157.  *    This routine gets all the terminal flags from the termcap database
  158.  */
  159.  
  160. void zap()
  161. {
  162.     register char    *namp;
  163.     register bool    **fp;
  164.     register char    ***sp;
  165. #ifdef    DEBUG
  166.     register char    *cp;
  167. #endif
  168.  
  169.     namp = "ambsdadbeohchzinmimsncnsosulxbxnxtxsxx";
  170.     fp = sflags;
  171.     do {
  172.         *(*fp++) = tgetflag(namp);
  173. #ifdef DEBUG
  174.         fprintf(outf, "%2.2s = %s\n", namp, *fp[-1] ? "TRUE" : "FALSE");
  175. #endif
  176.         namp += 2;
  177.     } while (*namp);
  178.     namp = "albcbtcdceclcmcrcsdcdldmdoedeik0k1k2k3k4k5k6k7k8k9hoicimipkdkekhklkrkskullmandnlpcrcscsesfsosrtatetiucueupusvbvsveALDLUPDOLERI";
  179.     sp = sstrs;
  180.     do {
  181.         *(*sp++) = tgetstr(namp, &aoftspace);
  182. #ifdef DEBUG
  183.         fprintf(outf, "%2.2s = %s", namp, *sp[-1] == NULL ? "NULL\n" : "\"");
  184.         if (*sp[-1] != NULL) {
  185.             for (cp = *sp[-1]; *cp; cp++)
  186.                 fprintf(outf, "%s", unctrl(*cp));
  187.             fprintf(outf, "\"\n");
  188.         }
  189. #endif
  190.         namp += 2;
  191.     } while (*namp);
  192.     if (XS)
  193.         SO = SE = NULL;
  194.     else {
  195.         if (tgetnum("sg") > 0)
  196.             SO = NULL;
  197.         if (tgetnum("ug") > 0)
  198.             US = NULL;
  199.         if (!SO && US) {
  200.             SO = US;
  201.             SE = UE;
  202.         }
  203.     }
  204. }
  205.  
  206. /*
  207.  * return a capability from termcap
  208.  */
  209. char *
  210. getcap(name)
  211. char *name;
  212. {
  213.  
  214.     return tgetstr(name, &aoftspace);
  215. }
  216.