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