home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / gnu / src / baseline / jove-4.14.6.lha / jove-4.14.6 / term.c < prev    next >
C/C++ Source or Header  |  1992-01-10  |  7KB  |  377 lines

  1. /***************************************************************************
  2.  * This program is Copyright (C) 1986, 1987, 1988 by Jonathan Payne.  JOVE *
  3.  * is provided to you without charge, and with no warranty.  You may give  *
  4.  * away copies of JOVE, including sources, provided that this notice is    *
  5.  * included in all the files.                                              *
  6.  ***************************************************************************/
  7.  
  8. #include "jove.h"
  9. #include "fp.h"
  10. #include "disp.h"
  11. #include "ctype.h"
  12. #include <errno.h>
  13.  
  14. #ifndef    MAC    /* most of the file... */
  15.  
  16. # ifdef    STDARGS
  17. #  include <stdarg.h>
  18. # else
  19. #  include <varargs.h>
  20. # endif
  21.  
  22. #ifndef    MSDOS
  23. # ifdef    SYSV
  24. #   include <termio.h>
  25. # else
  26. #   include <sgtty.h>
  27. # endif    /* SYSV */
  28. #endif    /* MSDOS */
  29.  
  30. #ifdef    IPROCS
  31. # include <signal.h>
  32. #endif
  33.  
  34. #include "termcap.h"
  35.  
  36. /* Termcap definitions */
  37.  
  38. #ifndef    IBMPC
  39. char    *CS,
  40.     *SO,
  41.     *SE,
  42.     *CM,
  43.     *CL,
  44.     *CE,
  45.     *HO,
  46.     *AL,
  47.     *DL,
  48.     *VS,
  49.     *VE,
  50.     *KS,
  51.     *KE,
  52.     *TI,
  53.     *TE,
  54.     *IC,
  55.     *DC,
  56.     *IM,
  57.     *EI,
  58.     *LL,
  59.     *M_IC,    /* Insert char with arg */
  60.     *M_DC,    /* Delete char with arg */
  61.     *M_AL,    /* Insert line with arg */
  62.     *M_DL,    /* Delete line with arg */
  63.     *SF,    /* Scroll forward */
  64.     *SR,
  65.     *M_SF,    /* Scroll forward with arg */
  66.     *M_SR,    /* Scroll back with arg */
  67.     *SP,    /* Send Cursor Position */
  68.     *VB,
  69.     *BL,
  70.     *IP,    /* insert pad after character inserted */
  71.     *lPC,
  72.     *NL,
  73.     *DO;
  74. #endif
  75.  
  76. int    LI,
  77.     ILI,    /* Internal lines, i.e., 23 of LI is 24. */
  78.     CO,
  79.  
  80.     SG,    /* number of magic cookies left by SO and SE */
  81.  
  82.     UPlen,
  83.     HOlen,
  84.     LLlen;
  85.  
  86. bool
  87.     XS,    /* whether standout is braindamaged */
  88.     Hazeltine,    /* Hazeltine tilde kludge */
  89.     MI,
  90.     UL,
  91.     NP,
  92.     TABS;
  93.  
  94. #ifdef    NEVER
  95.     /*
  96.      * Are you sure about this one Jon?  On the SYSV system I tried this
  97.      * on I got a multiple definition of PC because it was already
  98.      * defined in -ltermcap.  Similarly for BC and UP ...
  99.      */
  100. # ifdef    SYSVR2 /* release 2, at least */
  101. char    PC;
  102. # endif    /* SYSVR2 */
  103. #endif
  104.  
  105. #ifndef    IBMPC
  106. private char    tspace[256];
  107.  
  108. /* The ordering of ts and meas must agree !! */
  109. private const char    ts[] =
  110.      "vsvealdlspcssosecmclcehoupbcicimdceillsfsrvbksketiteALDLICDCpcipblnldoSFSR";
  111.  
  112. private char    **const meas[] = {
  113.     &VS, &VE, &AL, &DL, &SP, &CS, &SO, &SE,
  114.     &CM, &CL, &CE, &HO, &UP, &BC, &IC, &IM,
  115.     &DC, &EI, &LL, &SF, &SR, &VB, &KS, &KE,
  116.     &TI, &TE, &M_AL, &M_DL, &M_IC, &M_DC,
  117.     &lPC, &IP, &BL, &NL, &DO, &M_SF, &M_SR, NULL
  118. };
  119.  
  120. private void
  121. TermError()
  122. {
  123.     flushscreen();
  124.     _exit(1);
  125. }
  126.  
  127. void
  128. getTERM()
  129. {
  130.     char    termbuf[13],
  131.         *termname = NULL,
  132.         *termp = tspace,
  133.         tbuff[2048];    /* Good grief! */
  134.     const char    *tsp = ts;
  135.     int    i;
  136.  
  137.     termname = getenv("TERM");
  138.     if ((termname == NULL) || (*termname == '\0') ||
  139.         (strcmp(termname, "dumb") == 0) ||
  140.         (strcmp(termname, "unknown") == 0) ||
  141.         (strcmp(termname, "network") == 0)) {
  142.         putstr("Enter terminal type (e.g, vt100): ");
  143.         flushscreen();
  144.         termbuf[read(0, (UnivPtr) termbuf, sizeof(termbuf)) - 1] = '\0';
  145.         if (termbuf[0] == '\0')
  146.             TermError();
  147.  
  148.         termname = termbuf;
  149.     }
  150.  
  151.     if (tgetent(tbuff, termname) < 1) {
  152.         writef("[\"%s\" unknown terminal type?]", termname);
  153.         TermError();
  154.     }
  155.     if ((CO = tgetnum("co")) == -1) {
  156. wimperr:
  157.         writef("You can't run JOVE on a %s terminal.\n", termname);
  158.         TermError();
  159.         /*NOTREACHED*/
  160.     }
  161.  
  162.     else if (CO > MAXCOLS)
  163.         CO = MAXCOLS;
  164.  
  165.     if ((LI = tgetnum("li")) == -1)
  166.         goto wimperr;
  167.  
  168.     if ((SG = tgetnum("sg")) == -1)
  169.         SG = 0;            /* Used for mode line only */
  170.  
  171.     XS = tgetflag("xs")==TRUE;    /* Used for mode line only */
  172.     Hazeltine = tgetflag("hz")==TRUE;    /* Hazeltine tilde kludge */
  173.     NP = tgetflag("NP")==TRUE;    /* no pad char flag */
  174.  
  175.     for (i = 0; meas[i]; i++) {
  176.         static char    nm[3] = "xx";
  177.  
  178.         nm[0] = *tsp++;
  179.         nm[1] = *tsp++;
  180.         *(meas[i]) = (char *) tgetstr(nm, &termp);
  181.         if (termp > tspace + sizeof(tspace))
  182.             goto wimperr;
  183.     }
  184.     if (lPC)
  185.         PC = *lPC;
  186.     if (XS)
  187.         SO = SE = NULL;
  188.  
  189.     if (CS && !SR)
  190.         CS = SR = SF = M_SF = M_SR = NULL;
  191.  
  192.     if (CS && !SF)
  193.         SF = "\n";
  194.  
  195.     if (IM && (*IM == '\0'))
  196.         IM = NULL;
  197.     else
  198.         MI = tgetflag("mi")==TRUE;
  199.  
  200.     UL = tgetflag("ul")==TRUE;
  201.  
  202.     if (NL == NULL)
  203.         NL = "\n";
  204.     else {            /* strip stupid padding information */
  205.         while (jisdigit(*NL))
  206.             NL += 1;
  207.         if (*NL == '*')
  208.             NL += 1;
  209.     }
  210.     if (!DO)
  211.         DO = NL;
  212.  
  213.     if (BL == NULL)
  214.         BL = "\007";
  215.  
  216.     if (tgetflag("km") == TRUE)        /* has meta-key */
  217.         MetaKey = ON;
  218.  
  219. #ifdef    ID_CHAR
  220.     disp_opt_init();
  221. #endif
  222.     if ((CanScroll = ((AL && DL) || CS)) != 0)
  223.         IDline_setup(termname);
  224. }
  225.  
  226. #else    /* IBMPC */
  227.  
  228. void
  229. InitCM()
  230. {
  231. }
  232.  
  233. bool EGA;
  234.  
  235. void
  236. getTERM()
  237. {
  238.     char    *getenv(), *tgetstr() ;
  239.     char    *termname;
  240.     void    init_43(), init_term();
  241.     unsigned char lpp(), chpl();
  242.  
  243.     if (getenv("EGA") != NULL || (!stricmp(getenv("TERM"), "EGA"))) {
  244.        termname = "ega";
  245.        init_43();
  246.        EGA = YES;
  247.     } else {
  248.        termname = "ibmpc";
  249.        init_term();
  250.        EGA = NO;
  251.     }
  252.  
  253.     CO = chpl();
  254.     LI = lpp();
  255.  
  256.     SG = 0;            /* Used for mode line only */
  257.     XS = NO;            /* Used for mode line only */
  258.  
  259.     CanScroll = YES;
  260. }
  261.  
  262. #endif    /* IBMPC */
  263.  
  264. #else    /* MAC */
  265. int    LI,
  266.     ILI,    /* Internal lines, i.e., 23 of LI is 24. */
  267.     CO,
  268.     SG;
  269.  
  270. bool    TABS;
  271.  
  272. void getTERM()
  273. {
  274.     SG = 0;
  275.     CanScroll = YES;
  276. }
  277.  
  278. #endif    /* MAC */
  279.  
  280. /* put a string with padding */
  281.  
  282. #ifndef    IBMPC
  283. private void
  284. tputc(c)
  285. int    c;
  286. {
  287.     jputchar(c);
  288. }
  289. #endif    /* IBMPC */
  290.  
  291. #ifndef    MAC
  292. void
  293. putpad(str, lines)
  294. char    *str;
  295. int    lines;
  296. {
  297. #ifndef    IBMPC
  298.     if (str)
  299.         tputs(str, lines, tputc);
  300. #else    /* IBMPC */
  301.     write_emif(str);
  302. #endif    /* IBMPC */
  303. }
  304.  
  305. void
  306. putargpad(str, arg, lines)
  307. char    *str;
  308. int    arg,
  309.     lines;
  310. {
  311. #ifndef    IBMPC
  312.     if (str) {
  313.         tputs(
  314. #ifdef    TERMINFO
  315.             tparm(str, arg),
  316. #else    /* TERMINFO */
  317.             tgoto(str, 0, arg),    /* fudge */
  318. #endif    /* TERMINFO */
  319.             lines, tputc);
  320.     }
  321. #else    /* IBMPC */
  322.     /* This code is only a guess: I don't know if any M_* termcap
  323.      * attributes are defined for the PC.  If they are not used,
  324.      * this routine is not called.  Perhaps this routine should
  325.      * simply abort.
  326.      */
  327.     if (str) {
  328.         char    buf[16];    /* hope that this is long enough */
  329.  
  330.         swritef(buf, sizeof(buf), str, arg);    /* hope only %d appears in str */
  331.         write_em(buf);
  332.     }
  333. #endif    /* IBMPC */
  334. }
  335.  
  336. #endif    /* MAC */
  337.  
  338. /* Determine the number of characters to buffer at each baud rate.  The
  339.    lower the number, the quicker the response when new input arrives.  Of
  340.    course the lower the number, the more prone the program is to stop in
  341.    output.  Decide what matters most to you. This sets BufSize to the right
  342.    number or chars, and initializes `stdout'.  */
  343.  
  344. void
  345. settout(ttbuf)
  346. char    *ttbuf;
  347. {
  348. #ifdef    UNIX
  349.     static const int speeds[] = {
  350.         1,    /* 0    */
  351.         1,    /* 50    */
  352.         1,    /* 75    */
  353.         1,    /* 110    */
  354.         1,    /* 134    */
  355.         1,    /* 150    */
  356.         1,    /* 200    */
  357.         2,    /* 300    */
  358.         4,    /* 600    */
  359.         8,    /* 1200 */
  360.         16,    /* 1800    */
  361.         32,    /* 2400    */
  362.         128,    /* 4800    */
  363.         256,    /* 9600    */
  364.         512,    /* EXTA    */
  365.         1024    /* EXT    */
  366.     };
  367.  
  368.     int    speed_chars = speeds[ospeed];
  369. #else
  370.     int    speed_chars = 256;
  371. #endif
  372.  
  373.     flushscreen();        /* flush the one character buffer */
  374.     BufSize = min(MAXTTYBUF, speed_chars * max(LI / 24, 1));
  375.     stdout = fd_open("/dev/tty", F_WRITE|F_LOCKED, 1, ttbuf, BufSize);
  376. }
  377.