home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / VILE327.ZIP / VILE327.TAR / vile3.27 / tcap.c < prev    next >
C/C++ Source or Header  |  1992-12-14  |  8KB  |  440 lines

  1. /*    tcap:    Unix V5, V7 and BS4.2 Termcap video driver
  2.  *        for MicroEMACS
  3.  *
  4.  * $Log: tcap.c,v $
  5.  * Revision 1.16  1992/12/04  09:18:31  foxharp
  6.  * allow the open routine to be called again, to emit TI, KS
  7.  *
  8.  * Revision 1.15  1992/05/19  08:55:44  foxharp
  9.  * more prototype and shadowed decl fixups
  10.  *
  11.  * Revision 1.14  1992/05/16  12:00:31  pgf
  12.  * prototypes/ansi/void-int stuff/microsoftC
  13.  *
  14.  * Revision 1.13  1992/04/10  18:47:25  pgf
  15.  * change abs to absol to get rid of name conflicts
  16.  *
  17.  * Revision 1.12  1992/03/24  08:46:02  pgf
  18.  * fixed support for AL,DL -- I hope it's really safe to use tgoto as
  19.  * a generic parm capability expander.  I
  20.  *
  21.  * Revision 1.11  1992/03/24  07:46:34  pgf
  22.  * added support for DL and AL capabilities, which are multi-line insert
  23.  * and delete -- much better in an xterm
  24.  *
  25.  * Revision 1.10  1992/01/22  20:27:47  pgf
  26.  * added TI, TE, KS, KE support, per user suggestion (sorry, forgot who)
  27.  *
  28.  * Revision 1.9  1991/11/01  14:38:00  pgf
  29.  * saber cleanup
  30.  *
  31.  * Revision 1.8  1991/10/23  12:05:37  pgf
  32.  * NULL initializations should have been 0 instead
  33.  *
  34.  * Revision 1.7  1991/09/10  01:19:35  pgf
  35.  * re-tabbed, and moved ESC and BEL to estruct.h
  36.  *
  37.  * Revision 1.6  1991/08/07  12:35:07  pgf
  38.  * added RCS log messages
  39.  *
  40.  * revision 1.5
  41.  * date: 1991/08/06 15:26:22;
  42.  * sprintf changes
  43.  * 
  44.  * revision 1.4
  45.  * date: 1991/06/19 01:32:21;
  46.  * change name of howmany 'cuz of HP/UX conflict
  47.  * sheesh
  48.  * 
  49.  * revision 1.3
  50.  * date: 1991/05/31 11:25:14;
  51.  * moved PRETTIER_SCROLL to esturct.h
  52.  * 
  53.  * revision 1.2
  54.  * date: 1990/10/01 10:37:44;
  55.  * un-#ifdef spal()
  56.  * 
  57.  * revision 1.1
  58.  * date: 1990/09/21 10:26:09;
  59.  * initial vile RCS revision
  60. */
  61.  
  62. #define termdef 1            /* don't define "term" external */
  63.  
  64. #include <stdio.h>
  65. #include    "estruct.h"
  66. #include    "edef.h"
  67. #include <signal.h>
  68.  
  69. #if TERMCAP
  70.  
  71. #define MARGIN    8
  72. #define SCRSIZ    64
  73. #define NPAUSE    10            /* # times thru update to pause */
  74.  
  75.  
  76. #define TCAPSLEN 315
  77. char tcapbuf[TCAPSLEN];
  78. char *UP, PC, *CM, *CE, *CL, *SO, *SE;
  79. char *TI, *TE, *KS, *KE;
  80.  
  81. #if    SCROLLCODE
  82. char *CS, *dl, *al, *DL, *AL, *SF, *SR;
  83. #endif
  84.  
  85. extern char *tgoto P((char *, int, int));
  86. extern int tgetent P((char *, char *));
  87. extern int tgetnum P((char * ));
  88. extern int tputs P((char *, int, void(*_f)(int) ));
  89.  
  90. TERM term = {
  91.     0,    /* these four values are set dynamically at open time */
  92.     0,
  93.     0,
  94.     0,
  95.     MARGIN,
  96.     SCRSIZ,
  97.     NPAUSE,
  98.     tcapopen,
  99.     tcapclose,
  100.     tcapkopen,
  101.     tcapkclose,
  102.     ttgetc,
  103.     ttputc,
  104.     ttflush,
  105.     tcapmove,
  106.     tcapeeol,
  107.     tcapeeop,
  108.     tcapbeep,
  109.     tcaprev,
  110.     tcapcres
  111. #if    COLOR
  112.     , tcapfcol,
  113.     tcapbcol
  114. #endif
  115. #if    SCROLLCODE
  116.     , NULL        /* set dynamically at open time */
  117. #endif
  118. };
  119.  
  120. void
  121. tcapopen()
  122. {
  123.     char *getenv();
  124.     char *t, *p, *tgetstr();
  125.     char tcbuf[1024];
  126.     char *tv_stype;
  127.     char err_str[72];
  128.     static int already_open = 0;
  129.  
  130.     if (already_open) 
  131.     {
  132.         if (TI)
  133.             putnpad(TI, strlen(TI));
  134.         if (KS)
  135.             putpad(KS);
  136.         return;
  137.     }
  138.  
  139.     if ((tv_stype = getenv("TERM")) == NULL)
  140.     {
  141.         puts("Environment variable TERM not defined!");
  142.         exit(1);
  143.     }
  144.  
  145.     if ((tgetent(tcbuf, tv_stype)) != 1)
  146.     {
  147.         lsprintf(err_str, "Unknown terminal type %s!", tv_stype);
  148.         puts(err_str);
  149.         exit(1);
  150.     }
  151.  
  152.     /* Get screen size from system, or else from termcap.  */
  153.     getscreensize(&term.t_ncol, &term.t_nrow);
  154.  
  155.     if ((term.t_nrow <= 0) && (term.t_nrow=(short)tgetnum("li")) == -1) {
  156.         puts("termcap entry incomplete (lines)");
  157.         exit(1);
  158.     }
  159.     term.t_nrow -= 1;
  160.  
  161.  
  162.     if ((term.t_ncol <= 0) &&(term.t_ncol=(short)tgetnum("co")) == -1){
  163.         puts("Termcap entry incomplete (columns)");
  164.         exit(1);
  165.     }
  166.  
  167. #ifdef SIGWINCH
  168.     term.t_mrow =  200;
  169.     term.t_mcol = 200;
  170. #else
  171.     term.t_mrow =  term.t_nrow;
  172.     term.t_mcol =  term.t_ncol;
  173. #endif
  174.     p = tcapbuf;
  175.     t = tgetstr("pc", &p);
  176.     if(t)
  177.         PC = *t;
  178.  
  179.     CL = tgetstr("cl", &p);
  180.     CM = tgetstr("cm", &p);
  181.     CE = tgetstr("ce", &p);
  182.     UP = tgetstr("up", &p);
  183.     SE = tgetstr("se", &p);
  184.     SO = tgetstr("so", &p);
  185.     TI = tgetstr("ti", &p);
  186.     TE = tgetstr("te", &p);
  187.     KS = tgetstr("ks", &p);
  188.     KE = tgetstr("ke", &p);
  189.     if (SO != NULL)
  190.         revexist = TRUE;
  191.  
  192.     if(CL == NULL || CM == NULL || UP == NULL)
  193.     {
  194.         puts("Incomplete termcap entry\n");
  195.         exit(1);
  196.     }
  197.  
  198.     if (CE == NULL)     /* will we be able to use clear to EOL? */
  199.         eolexist = FALSE;
  200. #if SCROLLCODE
  201.     CS = tgetstr("cs", &p);
  202.     SF = tgetstr("sf", &p);
  203.     SR = tgetstr("sr", &p);
  204.     dl = tgetstr("dl", &p);
  205.     al = tgetstr("al", &p);
  206.     DL = tgetstr("DL", &p);
  207.     AL = tgetstr("AL", &p);
  208.         
  209.     if (CS && SR) {
  210.         if (SF == NULL) /* assume '\n' scrolls forward */
  211.             SF = "\n";
  212.         term.t_scroll = tcapscroll_reg;
  213.     } else if ((DL && AL) || (dl && al)) {
  214.         term.t_scroll = tcapscroll_delins;
  215.     } else {
  216.         term.t_scroll = NULL;
  217.     }
  218. #endif
  219.             
  220.     if (p >= &tcapbuf[TCAPSLEN])
  221.     {
  222.         puts("Terminal description too big!\n");
  223.         exit(1);
  224.     }
  225.     ttopen();
  226.     if (TI)
  227.         putnpad(TI, strlen(TI));
  228.     if (KS)
  229.         putpad(KS);
  230.     already_open = TRUE;
  231. }
  232.  
  233. void
  234. tcapclose()
  235. {
  236.     if (TE)
  237.         putnpad(TE, strlen(TE));
  238.     if (KE)
  239.         putpad(KE);
  240. }
  241.  
  242. void
  243. tcapkopen()
  244. {
  245.     strcpy(sres, "NORMAL");
  246. }
  247.  
  248. void
  249. tcapkclose()
  250. {
  251. }
  252.  
  253. void
  254. tcapmove(row, col)
  255. register int row, col;
  256. {
  257.     putpad(tgoto(CM, col, row));
  258. }
  259.  
  260. void
  261. tcapeeol()
  262. {
  263.     putpad(CE);
  264. }
  265.  
  266. void
  267. tcapeeop()
  268. {
  269.     putpad(CL);
  270. }
  271.  
  272. void
  273. tcaprev(state)        /* change reverse video status */
  274. int state;        /* FALSE = normal video, TRUE = reverse video */
  275. {
  276.     static int revstate = -1;
  277.     if (state == revstate)
  278.         return;
  279.     revstate = state;
  280.     if (state) {
  281.         if (SO != NULL)
  282.             putpad(SO);
  283.     } else {
  284.         if (SE != NULL)
  285.             putpad(SE);
  286.     }
  287. }
  288.  
  289. int
  290. tcapcres()    /* change screen resolution */
  291. {
  292.     return(TRUE);
  293. }
  294.  
  295. #if SCROLLCODE
  296.  
  297. /* move howmany lines starting at from to to */
  298. void
  299. tcapscroll_reg(from,to,n)
  300. int from, to, n;
  301. {
  302.     int i;
  303.     if (to == from) return;
  304.     if (to < from) {
  305.         tcapscrollregion(to, from + n - 1);
  306.         tcapmove(from + n - 1,0);
  307.         for (i = from - to; i > 0; i--)
  308.             putpad(SF);
  309.     } else { /* from < to */
  310.         tcapscrollregion(from, to + n - 1);
  311.         tcapmove(from,0);
  312.         for (i = to - from; i > 0; i--)
  313.             putpad(SR);
  314.     }
  315.     tcapscrollregion(0, term.t_nrow);
  316. }
  317.  
  318. /* 
  319. PRETTIER_SCROLL is prettier but slower -- it scrolls 
  320.         a line at a time instead of all at once.
  321. */
  322.  
  323. /* move howmany lines starting at from to to */
  324. void
  325. tcapscroll_delins(from,to,n)
  326. int from, to, n;
  327. {
  328.     int i;
  329.     if (to == from) return;
  330.     if (DL && AL) {
  331.         if (to < from) {
  332.             tcapmove(to,0);
  333.             putpad(tgoto(DL,0,from-to));
  334.             tcapmove(to+n,0);
  335.             putpad(tgoto(AL,0,from-to));
  336.         } else {
  337.             tcapmove(from+n,0);
  338.             putpad(tgoto(DL,0,to-from));
  339.             tcapmove(from,0);
  340.             putpad(tgoto(AL,0,to-from));
  341.         }
  342.     } else { /* must be dl and al */
  343. #if PRETTIER_SCROLL
  344.         if (absol(from-to) > 1) {
  345.             tcapscroll_delins(from, (from<to) ? to-1:to+1, n);
  346.             if (from < to)
  347.                 from = to-1;
  348.             else
  349.                 from = to+1;    
  350.         }
  351. #endif
  352.         if (to < from) {
  353.             tcapmove(to,0);
  354.             for (i = from - to; i > 0; i--)
  355.                 putpad(dl);
  356.             tcapmove(to+n,0);
  357.             for (i = from - to; i > 0; i--)
  358.                 putpad(al);
  359.         } else {
  360.             tcapmove(from+n,0);
  361.             for (i = to - from; i > 0; i--)
  362.                 putpad(dl);
  363.             tcapmove(from,0);
  364.             for (i = to - from; i > 0; i--)
  365.                 putpad(al);
  366.         }
  367.     }
  368. }
  369.  
  370. /* cs is set up just like cm, so we use tgoto... */
  371. void
  372. tcapscrollregion(top,bot)
  373. int top,bot;
  374. {
  375.     putpad(tgoto(CS, bot, top));
  376. }
  377.  
  378. #endif
  379.  
  380. /* ARGSUSED */
  381. void
  382. spal(dummy)    /* change palette string */
  383. char *dummy;
  384. {
  385.     /*    Does nothing here    */
  386. }
  387.  
  388. #if    COLOR
  389. void
  390. tcapfcol()    /* no colors here, ignore this */
  391. {
  392. }
  393.  
  394. void
  395. tcapbcol()    /* no colors here, ignore this */
  396. {
  397. }
  398. #endif
  399.  
  400. void
  401. tcapbeep()
  402. {
  403.     ttputc(BEL);
  404. }
  405.  
  406. void
  407. putpad(str)
  408. char    *str;
  409. {
  410.     tputs(str, 1, ttputc);
  411. }
  412.  
  413. void
  414. putnpad(str, n)
  415. char    *str;
  416. int n;
  417. {
  418.     tputs(str, n, ttputc);
  419. }
  420.  
  421.  
  422. #if    FLABEL
  423. /* ARGSUSED */
  424. int
  425. fnclabel(f, n)        /* label a function key */
  426. int f,n;    /* default flag, numeric argument [unused] */
  427. {
  428.     /* on machines with no function keys...don't bother */
  429.     return(TRUE);
  430. }
  431. #endif
  432. #else
  433.  
  434. void
  435. hello()
  436. {
  437. }
  438.  
  439. #endif
  440.