home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume13 / vn.jan.88 / part03 / term_set.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-01-30  |  4.8 KB  |  225 lines

  1. /*
  2. ** vn news reader.
  3. **
  4. ** term_set.c - terminal control, hides termcap interface
  5. **
  6. ** see copyright disclaimer / history in vn.c source file
  7. */
  8.  
  9. #include <stdio.h>
  10. #include "tty.h"
  11. #include "config.h"
  12. #include "tune.h"
  13. #include "node.h"
  14. #include "page.h"
  15.  
  16. extern int L_allow, C_allow;
  17. extern char *Ku, *Kd, *Kl, *Kr;    
  18.  
  19. static outc (c)
  20. char c;
  21. {
  22.     putchar (c);
  23. }
  24.  
  25. /*
  26.     term_set controls terminal through termcap
  27.     START sets global parameters related to terminal also,
  28.     as well as allocating display buffer which depends on
  29.     terminal lines, and allocating escape strings.  RESTART
  30.     simply re-issues the initialization - used following system
  31.     calls that could have goofed up the terminal state.
  32. */
  33.  
  34. /*
  35. ** Escape strings.
  36. */
  37.  
  38. static char *Cm,*Cl,*So,*Se,*Te,*Bc,*Ce,*Ti,*Ks,*Ke;
  39. #ifdef USEVS
  40. static char *Vs,*Ve;
  41. #endif
  42.  
  43. static int Backspace;        /* backspace works */
  44. static int Overstrike;        /* terminal overstrikes */
  45.  
  46. static t_setup()
  47. {
  48.     char *tgetstr(), *vn_env(), *str_store();
  49.     char *c, tc_buf[2048],optstr[2048];
  50.     char *tvar;
  51.  
  52.     tvar = vn_env("TERM",DEF_TERM);
  53.  
  54.     c = optstr;
  55.     if (tgetent(tc_buf,tvar) != 1)
  56.         printex ("%s - unknown terminal",tvar);
  57.  
  58.     /* get needed capabilities */
  59.     Cm = str_store(tgetstr("cm",&c));
  60.     Cl = str_store(tgetstr("cl",&c));
  61.     So = str_store(tgetstr("so",&c));
  62.     Se = str_store(tgetstr("se",&c));
  63.     Te = str_store(tgetstr("te",&c));
  64.     Ti = str_store(tgetstr("ti",&c));
  65.     Bc = str_store(tgetstr("bc",&c));
  66.     Ce = str_store(tgetstr("ce",&c));
  67.     Kd = str_store(tgetstr("kd",&c));
  68.     Ke = str_store(tgetstr("ke",&c));
  69.     Kl = str_store(tgetstr("kl",&c));
  70.     Kr = str_store(tgetstr("kr",&c));
  71.     Ks = str_store(tgetstr("ks",&c));
  72.     Ku = str_store(tgetstr("ku",&c));
  73. #ifdef USEVS
  74.     Vs = str_store(tgetstr("vs",&c));
  75.     Ve = str_store(tgetstr("ve",&c));
  76. #endif
  77.     Backspace = tgetflag("bs");
  78.     Overstrike = tgetflag("os");
  79.  
  80.     if ( *Cm == '\0' || *Cl == '\0')
  81.     {
  82.         printex ("cursor control and erase capability needed");
  83.     }
  84.  
  85.     /*
  86.     ** Checks for arrow keys which don't issue something beginning
  87.     ** with <ESC>.  This is more paranoid than we need to be, strictly
  88.     ** speaking - we could get away with any string which didn't
  89.     ** conflict with controls used for commands.  However, that would
  90.     ** be a maintenance headache - we will simply reserve <ESC> as the
  91.     ** only char not to be used for commands, and punt on terminals
  92.     ** which don't send reasonable arrow keys.  It would be confusing
  93.     ** to have keys work partially, also.  I know of no terminal with
  94.     ** one arrow key beginning with an escape, and another beginning
  95.     ** with something else, but let's be safe.  This also insists on
  96.     ** definitions for all 4 arrows, which seems reasonable.
  97.     */
  98.  
  99.     if ((*Ku != '\0' && *Ku != '\033') || *Kl != *Ku || *Kr != *Ku || *Kd != *Ku)
  100.     {
  101.         fgprintf("WARNING: arrow keys will not work for this terminal");
  102.         Ku = Kd = Kl = Kr = Kd = Ke = "";
  103.     }
  104.  
  105.     if (Overstrike)
  106.         fgprintf ("WARNING: terminal overstrikes - can't update display without erase\n");
  107.  
  108.     if ((L_allow = tgetnum("li")) < REQLINES)
  109.     {
  110.         if (L_allow < 0)
  111.             printex ("can't determine number of lines on terminal");
  112.         printex ("too few lines for display - %d needed", REQLINES);
  113.     }
  114.  
  115.     /*
  116.     ** C_allow set so as to not use extreme right column.
  117.     ** Avoids "bad wraparound" problems - we're deciding it's best
  118.     ** to ALWAYS assume no automargin, and take care of it ourselves
  119.     */
  120.     if((C_allow = tgetnum("co")) > MAX_C)
  121.         C_allow = MAX_C;
  122.     else
  123.         --C_allow;
  124.     if (C_allow < MIN_C)
  125.     {
  126.         if (C_allow < 0)
  127.             printex("can't determine number of columns on terminal.");
  128.         printex ("too few columns for display - %d needed",MIN_C);
  129.     }
  130.  
  131.     L_allow -= RECBIAS;
  132.     page_alloc();
  133.     tputs(Ti,1,outc);
  134.     tputs(Ks,1,outc);
  135. #ifdef USEVS
  136.     tputs(Vs,1,outc);
  137. #endif
  138. }
  139.  
  140. /* VARARGS */
  141. term_set(cmd,x,y)
  142. int cmd,x,y;
  143. {
  144.     char *tgoto();
  145.     int i;
  146.     switch (cmd)
  147.     {
  148.     case MOVE:
  149.         tputs (tgoto(Cm,x,y),1,outc);
  150.         break;
  151.     case ERASE:
  152.         tputs(Cl,1,outc);
  153.         break;
  154.     case ONREVERSE:
  155.         tputs(So,1,outc);
  156.         break;
  157.     case OFFREVERSE:
  158.         tputs(Se,1,outc);
  159.         break;
  160.     case START:
  161.         t_setup();
  162.         break;
  163.     case RESTART:
  164.         tputs(Ti,1,outc);
  165.         tputs(Ks,1,outc);
  166. #ifdef USEVS
  167.         tputs(Vs,1,outc);
  168. #endif
  169.         break;
  170.     case STOP:
  171.         term_set (MOVE,0,L_allow+RECBIAS-1);
  172.         printf ("\n");
  173.         tputs(Ke,1,outc);
  174.         tputs(Te,1,outc);
  175. #ifdef USEVS
  176.         tputs(Ve,1,outc);
  177. #endif
  178.         break;
  179.     case RUBSEQ:
  180.         if (Overstrike)
  181.         {
  182.             /* space overprint is futile */
  183.             if (Backspace)
  184.                 putchar('\010');
  185.             else
  186.                 tputs(Bc,1,outc);
  187.             break;
  188.         }
  189.         if (Backspace)
  190.             printf("%c %c",'\010','\010');
  191.         else
  192.         {
  193.             tputs(Bc,1,outc);  
  194.             putchar(' ');  
  195.             tputs(Bc,1,outc);
  196.         }
  197.         break;
  198.     case ZAP:
  199.         if (Ce != NULL && *Ce != '\0')
  200.             tputs(Ce,1,outc);
  201.         else
  202.         {
  203.             if (Overstrike)
  204.                 break;        /* punt */
  205.             for (i=x; i < y; ++i)
  206.                 putchar(' ');
  207.             if (Backspace)
  208.             {
  209.                 for (i=x; i < y; ++i)
  210.                     putchar('\010');
  211.             }
  212.             else
  213.             {
  214.                 for (i=x; i < y; ++i)
  215.                     tputs(Bc,1,outc);
  216.             }
  217.         }
  218.         break;
  219.     default:
  220.         printex ("term_set unknown code (%d)",cmd);
  221.         break;
  222.     }
  223.     return (0);
  224. }
  225.