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