home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / uniflex.zip / ufscrn.c < prev    next >
C/C++ Source or Header  |  1993-08-23  |  5KB  |  177 lines

  1. #include "ufk.h"
  2. #include "uftcap.h"
  3.  
  4. #define TERMCAP_FILE   "/etc/termcap"
  5.  
  6. #define ESCAPE    255  /* cursor control escape */
  7. #define XY        1    /* bit for row or column, on->column */
  8. #define BIAS      2    /* bit for bias */
  9. #define SUB       4    /* subtract from maximum */
  10. #define BCD       8    /* bit for BCD */
  11. #define ASCII     16   /* bit to send position in ASCII */
  12. #define NIL       0    /* Null pointer */
  13. #define DIRSIZE   32   /* Terminal Directory Size */
  14. #define NUMCAP    62   /* Number of capabilities */
  15. #define DATABYTES 4    /* Number of bytes before pointers */
  16.  
  17. typedef int BOOLEAN;
  18.  
  19. struct ttycap cap;              /* Terminal capabilities */
  20.  
  21. int nrows;                      /* Number of rows on screen */
  22. int ncols;                      /* Number of columns on screen */
  23. int p_l_row;                    /* Physical last row */
  24. int p_l_column;                 /* Physical last column */
  25. int last_row;                   /* Last row number (0 origin) */
  26. int last_column;                /* Last column number (0 origin) */
  27. int delay;                      /* Screen settle time in seconds */
  28. int cu_size;                    /* Size of cursor up string */
  29. int cd_size;                    /* Size of cursor down string */
  30. int cl_size;                    /* Size of cursor left string */
  31. int cr_size;                    /* Size of cursor right string */
  32. int hm_size;                    /* Size of home up string */
  33. int row;                        /* Current row number (0 origin) */
  34. int column;                     /* Current column number (0 origin) */
  35.  
  36. clreol(x,y)
  37. int x;
  38. int y;
  39. {
  40.    if (screen && !remote && !nooutput)
  41.    {
  42.       if (x >= 0)
  43.          posit(x,y);
  44.       fputs(cap.c_blank,stdout);
  45.    }
  46. }
  47.  
  48. clear_screen()     /* Clear screen */
  49. {
  50.    if (screen && !remote && !nooutput)
  51.    {
  52.       fputs(cap.c_clear,stdout);
  53.       if (delay != 0)
  54.          sleep(delay);
  55.       row = column = 0;
  56.    }
  57. }
  58.  
  59. curs_on()
  60. {
  61.    if ((cap.c_curon != NIL) && !nooutput)
  62.       fputs(cap.c_curon, stdout);
  63. }
  64.  
  65. curs_off()
  66. {
  67.    if ((cap.c_curoff != NIL) && !nooutput)
  68.       fputs(cap.c_curoff, stdout);
  69. }
  70.  
  71. background()
  72. {
  73.    if ((cap.c_backg != NIL) && !nooutput)
  74.       fputs(cap.c_backg, stdout);
  75. }
  76.  
  77. foreground()
  78. {
  79.    if ((cap.c_foreg != NIL) && !nooutput)
  80.       fputs(cap.c_foreg, stdout);
  81. }
  82.  
  83. posit(x,y)       /* Move cursor to specified position */
  84. int    x;  /* column */
  85. int    y;  /* row */
  86. {
  87.    int     v;
  88.    char    *c_ptr;
  89.    char    ch;
  90.  
  91.    if (((c_ptr = cap.c_pos) != NIL) && screen && !remote && !nooutput)
  92.    {                                /* can position cursor */
  93.       if (x > last_column)
  94.          x = last_column;           /* truncate x */
  95.       if (y > last_row)
  96.          y = last_row;              /* truncate y */
  97.       while ((v = (*c_ptr++) & 0xff) != NULL)
  98.       {
  99.          if (v!=ESCAPE)
  100.             putchar(v);
  101.          else if((v = (*c_ptr++) & 0xff) != NULL)
  102.          {
  103.             ch = ((v & XY) == 0) ?
  104.                ((v & SUB) == 0 ? y : p_l_row - y) :
  105.                ((v & SUB) == 0 ? x : p_l_column - x);
  106.             if ((v & BIAS) != 0)
  107.                ch += (unsigned)*c_ptr++;
  108.             if ((v & BCD) != 0)
  109.             {
  110.                ch = (unsigned)ch / 10 << 4 | (unsigned)ch % 10;
  111.                putchar(ch);
  112.             }
  113.             else if ((v & ASCII) != 0)
  114.             {
  115.                putchar((unsigned)ch / 10 + '0');
  116.                putchar((unsigned)ch % 10 + '0');
  117.             }
  118.             else putchar(ch);
  119.          }
  120.       }
  121.       column = x;
  122.       row = y;
  123.    }
  124. }
  125.  
  126. BOOLEAN terminit()                    /* Initialize terminal */
  127. {
  128.    if (!termcap(&cap))
  129.       return FALSE;
  130.    if (cap.c_pos == NIL)
  131.       return FALSE;                   /* Cursor positioning required */
  132.    nrows = (int)cap.c_rows;
  133.    ncols = (int)cap.c_cols;
  134.    p_l_row = last_row = nrows - 1;
  135.    p_l_column = last_column = ncols - 1;
  136.    delay = (int)cap.c_wait;
  137.    cu_size = strlen(cap.c_up);
  138.    cd_size = strlen(cap.c_down);
  139.    cl_size = strlen(cap.c_left);
  140.    cr_size = strlen(cap.c_right);
  141.    hm_size = strlen(cap.c_home);
  142.    if (cap.c_init != NIL)
  143.       fputs(cap.c_init,stdout);
  144.    clear_screen();
  145.    return TRUE;
  146. }
  147.  
  148. BOOLEAN termcap(cp)              /* Get terminal capabilities */
  149. struct ttycap *cp;
  150. {
  151.    char **cap_ptr;
  152.    int  i;
  153.    int  fd;
  154.    int  direc[DIRSIZE];
  155.    long lseek();
  156.    int open(), read();
  157.  
  158.    if ((fd = open(TERMCAP_FILE,0)) == ERROR)
  159.       return FALSE;
  160.    if ((i = read(fd, (char *)direc, sizeof(int) * DIRSIZE)) == -1
  161.          || i != sizeof(int) * DIRSIZE
  162.          || (i = direc[ttyslot()]) == 0
  163.          || lseek(fd, (long)i, 0) == -1L
  164.          || (i = read(fd, (char *)cp, sizeof(struct ttycap))) == -1
  165.          || i != sizeof(struct ttycap))
  166.    {
  167.       close(fd);
  168.       return FALSE;
  169.    }
  170.    close(fd);
  171.    cap_ptr = (char **)((char *)cp + DATABYTES);
  172.    for (i = NUMCAP; i--; cap_ptr++)
  173.       if (*cap_ptr != NIL)
  174.          *cap_ptr += (unsigned)cp;
  175.    return TRUE;
  176. }
  177.