home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 198_02 / tcap.c < prev    next >
C/C++ Source or Header  |  1990-01-23  |  7KB  |  377 lines

  1. /*    tcap:    Unix V5, V7 and BS4.2 Termcap video driver
  2.         for MicroEMACS
  3. */
  4.  
  5. #define    termdef    1            /* don't define "term" external */
  6.  
  7. #include <stdio.h>
  8. #include    "estruct.h"
  9. #include        "edef.h"
  10.  
  11. #if TERMCAP
  12.  
  13. #define    MARGIN    8
  14. #define    SCRSIZ    64
  15. #define    NPAUSE    10            /* # times thru update to pause */
  16. #define BEL     0x07
  17. #define ESC     0x1B
  18.  
  19. extern int      ttopen();
  20. extern int      ttgetc();
  21. extern int      ttputc();
  22. extern int    tgetnum();
  23. extern int      ttflush();
  24. extern int      tcapclose();
  25. extern int    tcapkopen();
  26. extern int    tcapkclose();
  27. extern int      tcapmove();
  28. extern int      tcapeol();
  29. extern int      tcapeop();
  30. extern int      tcapbeep();
  31. extern int    tcaprev();
  32. extern int    tcapcres();
  33. extern int      tcapopen();
  34. extern int      tput();
  35. extern char     *tgoto();
  36. #if    COLOR
  37. extern    int    tcapfcol();
  38. extern    int    tcapbcol();
  39.  
  40. int isansi = 0;        /* has support for ansi colors */
  41. int cfcolor = -1;    /* current forground color */
  42. int cbcolor = -1;    /* current background color */
  43. int usedcolor = 0;    /* true if used a color */
  44. #endif
  45.  
  46. #define TCAPSLEN 315
  47. char tcapbuf[TCAPSLEN];
  48. char *UP, PC, *CM, *CE, *CL, *SO, *SE;
  49.  
  50. int oldwidth;
  51. int tcaprv;
  52.  
  53. TERM term = {
  54.     0,    /* these four values are set dynamically at open time */
  55.     0,
  56.     0,
  57.     0,
  58.     MARGIN,
  59.     SCRSIZ,
  60.     NPAUSE,
  61.         tcapopen,
  62.         tcapclose,
  63.         tcapkopen,
  64.         tcapkclose,
  65.         ttgetc,
  66.         ttputc,
  67.         ttflush,
  68.         tcapmove,
  69.         tcapeol,
  70.         tcapeop,
  71.         tcapbeep,
  72.         tcaprev,
  73.         tcapcres
  74. #if    COLOR
  75.     , tcapfcol,
  76.     tcapbcol
  77. #endif
  78. };
  79.  
  80. #define fastputc(c)    TTputc(c)
  81.  
  82. #if     V7 | USG | BSD
  83. #undef fastputc
  84. #define fastputc(c)        fputc((c), stdout)
  85. #endif
  86.  
  87. tcapopen()
  88.  
  89. {
  90.         char *getenv();
  91.         char *t, *p, *tgetstr();
  92.         char tcbuf[1024];
  93.         char *tv_stype;
  94.         char err_str[72];
  95.     char *IS;
  96.     char *temp;
  97.  
  98.     phrow = scrltop = scrlbot = 1000;
  99.     tcaprv = FAILED;
  100.  
  101.         if ((tv_stype = getenv("TERM")) == NULL)
  102.         {
  103.                 puts("Environment variable TERM not defined!");
  104.                 exit(1);
  105.         }
  106.  
  107.         if ((tgetent(tcbuf, tv_stype)) != 1)
  108.         {
  109.                 sprintf(err_str, "Unknown terminal type %s!", tv_stype);
  110.                 puts(err_str);
  111.                 exit(1);
  112.         }
  113.  
  114.  
  115.     if (*tv_stype=='v' && *(tv_stype+1)=='t' && *(tv_stype+2)!='5') {
  116.         isvt100 = TRUE;
  117.         /* if (*(tv_stype+2) == '2') */ vt100keys = 1;
  118.         }
  119. #if    COLOR
  120.     cfcolor = -1;
  121.     cbcolor = -1;
  122.     isansi = isvt100;
  123.     if (*tv_stype=='a' && *(tv_stype+1)=='n' && *(tv_stype+2)=='s')
  124.         isansi = TRUE;
  125. #endif
  126.  
  127.        if ((term.t_nrow=(short)tgetnum("li")-1) == -1){
  128.                puts("termcap entry incomplete (lines)");
  129.                exit(1);
  130.        }
  131.     term.t_mrow =  term.t_nrow;
  132.  
  133.        if ((term.t_ncol=(short)tgetnum("co")) == -1){
  134.                puts("Termcap entry incomplete (columns)");
  135.                exit(1);
  136.        }
  137.     term.t_mcol = term.t_ncol;
  138.     if (isvt100 && term.t_mcol < 132) term.t_mcol = 132;
  139.     oldwidth = term.t_ncol;
  140.  
  141.         p = tcapbuf;
  142.         t = tgetstr("pc", &p);
  143.         if(t)
  144.                 PC = *t;
  145.  
  146.         CL = tgetstr("cl", &p);
  147.         CM = tgetstr("cm", &p);
  148.         CE = tgetstr("ce", &p);
  149.         UP = tgetstr("up", &p);
  150.     SE = tgetstr("se", &p);
  151.     SO = tgetstr("so", &p);
  152.     IS = tgetstr("is", &p);
  153.     if (SO != NULL)
  154.         revexist = TRUE;
  155.  
  156.         if(CL == NULL || CM == NULL || UP == NULL)
  157.         {
  158.                 puts("Incomplete termcap entry\n");
  159.                 exit(1);
  160.         }
  161.  
  162.     if (CE == NULL)        /* will we be able to use clear to EOL? */
  163.         eolexist = FALSE;
  164.         
  165.         if (p >= &tcapbuf[TCAPSLEN])
  166.         {
  167.                 puts("Terminal description too big!\n");
  168.                 exit(1);
  169.         }
  170.         ttopen();
  171.     if (IS != NULL)            /* initialize terminal */
  172.         while(*IS) fastputc(*IS++);
  173.  
  174.     /* if ((temp = getenv("vt100key")) != NULL) vt100keys = (*temp&1); */
  175.  
  176. #if    DECEDT
  177.     /* hack to force vt100's into keypad mode */
  178.     if (isvt100) {
  179.         fastputc('\033'); fastputc('=');
  180.         }
  181. #endif
  182. }
  183.  
  184. tcapclose()
  185. {
  186.     if (isvt100)    {
  187.         ttsetwid(oldwidth);
  188.     }
  189. #if    COLOR
  190.     if (usedcolor) {
  191.         tcapfcol(7);
  192.         tcapbcol(0);
  193.     }
  194. #endif
  195.     ttclose();
  196. }
  197.  
  198. tcapkopen()
  199.  
  200. {
  201.     strcpy(sres, "NORMAL");
  202. }
  203.  
  204. tcapkclose()
  205.  
  206. {
  207. }
  208.  
  209. tcapmove(row, col)
  210. register int row, col;
  211. {
  212.     if (isvt100 && col == 0)
  213.         if (row == phrow+1 && row != scrlbot)
  214.             {fastputc(13); fastputc(10);}
  215.         else if (row == phrow-1 && row+2 != scrltop)
  216.             {fastputc(13); fastputc(27); fastputc('M');}
  217.         else if (row == phrow)
  218.             {fastputc(13);}
  219.         else {
  220.             fastputc(27); fastputc('[');
  221.             if (row >= 9) fastputc('0' + (row+1)/10);
  222.             if (row > 0) fastputc('0' + (row+1) % 10);
  223.             fastputc('H');
  224.         }
  225.         else putpad(tgoto(CM, col, row));
  226.     phrow = row;
  227. }
  228.  
  229. tcapeol()
  230. {
  231. #if    COLOR
  232.     tcapfcol(gfcolor);
  233.     tcapbcol(gbcolor);
  234. #endif
  235.         putpad(CE);
  236. }
  237.  
  238. tcapeop()
  239. {
  240. #if    COLOR
  241.     tcapfcol(gfcolor);
  242.     tcapbcol(gbcolor);
  243. #endif
  244.         putpad(CL);
  245.     phrow = 1000;
  246. }
  247.  
  248. tcaprev(state)        /* change reverse video status */
  249.  
  250. int state;        /* FALSE = normal video, TRUE = reverse video */
  251.  
  252. {
  253. #if    COLOR
  254.     /* int ftmp, btmp; */
  255. #endif
  256.  
  257. #if    COLOR
  258.     /* forground and background already set */
  259.     if (usedcolor) return;
  260. #endif
  261.     if (state == tcaprv)    return;
  262.     tcaprv = state;
  263.     if (state) {
  264.         if (SO != NULL)
  265.             putpad(SO);
  266.     } else    {
  267.         if (SE != NULL)
  268.             putpad(SE);
  269. #if    COLOR
  270.         /*
  271.         ftmp = cfcolor;
  272.         btmp = cbcolor;
  273.         cfcolor = -1;
  274.         cbcolor = -1;
  275.         tcapfcol(ftmp);
  276.         tcapbcol(btmp);
  277.         */
  278. #endif
  279.     }
  280. }
  281.  
  282. tcapcres()    /* change screen resolution */
  283.  
  284. {
  285.     return(TRUE);
  286. }
  287.  
  288. spal(dummy)    /* change palette string */
  289. char *dummy;
  290. {
  291.     /* Does nothing here */
  292. }
  293.  
  294. #if    COLOR
  295. tcapparm(n)
  296. register int n;
  297. {
  298.     register q,r;
  299.  
  300.     q = n/10;
  301.     if (q != 0)    {
  302.         r = q/10;
  303.         if (r != 0)    {fastputc('0' + r%10);}
  304.         fastputc('0' + q%10);
  305.     }
  306.     fastputc('0' + n%10);
  307. }
  308.  
  309. tcapfcol(color)    /* set the current output color */
  310. int color;
  311. {
  312.     if (!usedcolor || color == cfcolor)    return;
  313.     fastputc(ESC);
  314.     fastputc('[');
  315.     tcapparm(color + 30);
  316.     fastputc('m');
  317.     cfcolor = color;
  318. }
  319.  
  320. tcapbcol(color)    /* set the current background color */
  321. int color;
  322. {
  323.     if (!usedcolor || color == cbcolor)    return;
  324.     fastputc(ESC);
  325.     fastputc('[');
  326.     tcapparm(color + 40);
  327.     fastputc('m');
  328.     cbcolor = color;
  329. }
  330. #endif
  331.  
  332. tcapbeep()
  333. {
  334.     fastputc(BEL);
  335. }
  336.  
  337. putpad(str)
  338. char    *str;
  339. {
  340. #if USG && MSC
  341.     int cnt;
  342.     cnt = 0;
  343.     while(*str >= '0' && *str <= '9')
  344.         cnt = cnt*10 + *str++ - '0';
  345.     while(cnt-- > 0) fastputc(PC);
  346.     while(*str != '\0') {putc(*str, stdout); str++;}
  347. #else
  348.     tputs(str, 1, ttputc);
  349. #endif
  350. }
  351.  
  352. putnpad(str, n)
  353. char    *str;
  354. {
  355.     tputs(str, n, ttputc);
  356. }
  357.  
  358.  
  359. #if    FLABEL
  360. fnclabel(f, n)        /* label a function key */
  361.  
  362. int f,n;    /* default flag, numeric argument [unused] */
  363.  
  364. {
  365.     /* on machines with no function keys...don't bother */
  366.     return(TRUE);
  367. }
  368. #endif
  369. #else
  370.  
  371. tcaphello()
  372. {
  373. }
  374.  
  375. #endif
  376.  
  377.