home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume14 / shellforms / part02 / term.c < prev   
Encoding:
C/C++ Source or Header  |  1988-05-09  |  6.4 KB  |  243 lines

  1. /* Last update: 01/13/88  11:16 AM  (Edition: 9) */
  2. #include    <stdio.h>
  3. #include    <ctype.h>
  4. #include    <strings.h>
  5. #include    "form.h"
  6.  
  7. #define    YES    (1)
  8. #define    NO    (0)
  9. #define    EOS    '\0'
  10. #define    when        break; case
  11. #define    otherwise    break;default
  12.  
  13. static    char    *Erase = "\010 \010";    /* erase string */
  14.  
  15. struct    tcap    {
  16.     char        *tc_id;        /* key for capability    */
  17.     char        *tc_str;    /* ptr to Tc_str area    */
  18.     unsigned char    tc_delay;    /* # of msec to delay    */
  19.     unsigned char    tc_len;        /* length of tc_str    */
  20.     };
  21.  
  22. static    char    Termcap [1024];
  23. static    char    Tstr [1024];        /* buffer for real escape sequence */
  24. static    char    *Tsp = Tstr;        /* pointer to be used by tgetstr */
  25. static    int    Tcap_count = 0;        /* # of entries extracted */
  26.  
  27. /*-------- You may want to modify the following (also term.h) ----------*/
  28. #include    "term.h"
  29.  
  30. static    struct    tcap Tcap [] = {
  31.         { "bc",    0, NULL, 0 },    /* cursor backspace        */
  32.         { "cm",    0, NULL, 0 },    /* cursor motion        */
  33.         { "cl", 0, NULL, 0 },    /* clear entire screen        */
  34.         { "cd", 0, NULL, 0 },    /* clear to end of display    */
  35.         { "ce", 0, NULL, 0 },    /* clear to end of line        */
  36.         { "ho", 0, NULL, 0 },    /* home cursor            */
  37.         { "ks", 0, NULL, 0 },    /* start keypad xmit mode    */
  38.         { "ke", 0, NULL, 0 },    /* end keypad xmit mode        */
  39.         { "ku", 0, NULL, 0 },    /* (input) cursor upper        */
  40.         { "kd", 0, NULL, 0 },    /* (input) cursor down        */
  41.         { "kl", 0, NULL, 0 },    /* (input) cursor left        */
  42.         { "kr", 0, NULL, 0 },    /* (input) cursor right        */
  43.         { "md", 0, NULL, 0 },    /* mode dim (or highlite)    */
  44.         { "me", 0, NULL, 0 },    /* mode end (return to normal)    */
  45.         { "rc", 0, NULL, 0 },    /* restore cursor        */
  46.         { "sc", 0, NULL, 0 },    /* save cursor            */
  47.         { "so", 0, NULL, 0 },    /* start reverse video mode    */
  48.         { "se", 0, NULL, 0 },    /* end                */
  49.         { "us", 0, NULL, 0 },    /* start underline mode     */
  50.         { "ue", 0, NULL, 0 },    /* end                */
  51.         { NULL, 0, NULL, 0 }
  52.     };
  53.  
  54. char    *getenv ();
  55. char    *tgetstr ();
  56. char    *tgoto ();
  57.  
  58. /*-------------------------------------------------------------05/10/86-+
  59. |                                    |
  60. |          tcap_init : initialize termcap data structure        |
  61. |                                    |
  62. +----------------------------------------------------------------------*/
  63. tcap_init ()
  64.     {
  65.     struct    tcap    *p;
  66.     char        *tp;
  67.     unsigned int    delay;
  68.     int        status;
  69.     char        *termtype = getenv ("TERM");
  70.  
  71.     if ((status = tgetent (Termcap, termtype)) != 1) {
  72.         if (status == 0) {
  73.             fprintf (stderr, "No entry for %s in termcap\r\n",
  74.                  termtype);
  75.             }
  76.         else    fprintf (stderr, "Can not open termcap file\r\n");
  77.         exit (1);
  78.         }
  79.  
  80.     for (p= &Tcap[0]; p->tc_id != EOS; p++) {
  81.         tp = tgetstr (p-> tc_id, &Tsp);
  82.         if (tp == NULL) {     /* no such capability */
  83.             if (p == &Tcap[BC]) tp = "\010";
  84.             else tp = "";
  85.             }
  86.         delay = 0;
  87.         while (isdigit (*tp)) {
  88.             delay = delay*10 + (*tp++) - '0';
  89.             }
  90.         p->tc_str = tp;
  91.         p->tc_delay = delay;
  92.         p->tc_len = strlen (tp);
  93.         Tcap_count++;
  94.         }
  95.     }
  96.  
  97. /*----------------------------------------------------------------------+
  98. |                                    |
  99. |        screen : common screen operation routine        |
  100. |                                    |
  101. +----------------------------------------------------------------------*/
  102. screen (code)
  103. int    code;        /* operation code */
  104.     {
  105.     int        n = BAD;    /* init to not valid entry */
  106.     struct    tcap    *te;
  107.  
  108.     if (Tcap_count == 0) tcap_init ();
  109.     switch (code) {
  110.         when SCR_DEL:        put_string (Erase, 0);
  111.         when SCR_BACKSPACE:    n = BC;
  112.         when SCR_ERASE:        n = CL;
  113.         when SCR_HOME:        n = HO;
  114.         when SCR_EEOL:        n = CE;
  115.         when SCR_SAVE:        n = SC;
  116.         when SCR_KEYXMIT:    n = KS;
  117.         when SCR_NOKEYXMIT:    n = KE;
  118.         when SCR_RESTORE:    n = RC;
  119.         when SCR_REVERSE:    n = SO;
  120.         when SCR_NORMAL:    n = SE;
  121.         otherwise:    ;    /* ignore it */
  122.         }
  123.     if (n != BAD) {
  124.         te = &Tcap[n];
  125.         delay (te-> tc_delay);
  126.         put_string (te-> tc_str, (unsigned)te-> tc_len);
  127.         }
  128.     }
  129.  
  130. /*----------------------------------------------------------------------+
  131. |                                    |
  132. |       poscur : position cursor on line, column on screen        |
  133. |                                    |
  134. +----------------------------------------------------------------------*/
  135. poscur (line, column, s)
  136. unsigned char    line;
  137. unsigned char    column;
  138. char        *s;        /* option string to output at line, column */
  139.     {
  140.     char    *p;
  141.  
  142.     if (Tcap_count == 0) tcap_init ();
  143.     p = tgoto (Tcap[CM].tc_str, column-1, line-1);
  144.     delay (Tcap[CM].tc_delay);
  145.     put_string (p, 0);
  146.     if (s != NULL) put_string (s, 0);
  147.     }
  148.  
  149. /*-------------------------------------------------------------05/10/86-+
  150. |                                    |
  151. |      delay : delay output for n msec (actually write NULL)        |
  152. |                                    |
  153. +----------------------------------------------------------------------*/
  154. static
  155. delay (n)
  156. unsigned char    n;            /* # of msec to delay */
  157.     {
  158.     static    char    c = EOS;
  159.     register int    i;
  160.  
  161.     if (n == 0) return;
  162.  
  163.     for (i=0; i<n; i++) {
  164.         write (fileno (stdout), &c, 1);
  165.         }
  166.     }
  167.  
  168. /*-------------------------------------------------------------05/11/86-+
  169. |                                    |
  170. |          Routines to get mode-specific strings            |
  171. |                                    |
  172. +----------------------------------------------------------------------*/
  173. get_undline (sbuf, ebuf)
  174. char    *sbuf;                /* enter mode buffer */
  175. char    *ebuf;                /* exit mode buffer */
  176.     {
  177.     if (Tcap_count == 0) tcap_init ();
  178.     strcpy (sbuf, Tcap[US].tc_str);
  179.     strcpy (ebuf, Tcap[UE].tc_str);
  180.     }
  181.  
  182. get_hilite (sbuf, ebuf)
  183. char    *sbuf;                /* enter mode buffer */
  184. char    *ebuf;                /* exit mode buffer */
  185.     {
  186.     if (Tcap_count == 0) tcap_init ();
  187.     strcpy (sbuf, Tcap[MD].tc_str);
  188.     strcpy (ebuf, Tcap[ME].tc_str);
  189.     }
  190.  
  191. get_rvideo (sbuf, ebuf)
  192. char    *sbuf;                /* enter mode buffer */
  193. char    *ebuf;                /* exit mode buffer */
  194.     {
  195.     if (Tcap_count == 0) tcap_init ();
  196.     strcpy (sbuf, Tcap[SO].tc_str);
  197.     strcpy (ebuf, Tcap[SE].tc_str);
  198.     }
  199.  
  200. /*-------------------------------------------------------------07/05/87-+
  201. |                                    |
  202. |        getkey : get user entered key (handle cursor key)        |
  203. |                                    |
  204. +----------------------------------------------------------------------*/
  205. getkey ()
  206.     {
  207.     char        buf[20];    /* temporary hold the input stream */
  208.     char        c;
  209.     register int    i = 0;
  210.     register int    ci = 0;        /* current matched index */
  211.     int        idx;
  212.     char        match;        /* flag, YES/NO */
  213.     char        kmatch [4];    /* record how may char matched in
  214.                        each key (KU, KD, KL, KR) */
  215.     extern char    get_char();
  216.  
  217.     for (i=0; i<4; i++) kmatch[i] = 0;
  218. loop:
  219.     c = get_char () & 0x7f;            /* make it ASCII */
  220.     for (match=NO, i=0; i<4; i++) {
  221.         if (kmatch[i] < ci) continue;
  222.         switch (i) {
  223.             when 0: idx = KU;
  224.             when 1: idx = KD;
  225.             when 2: idx = KL;
  226.             when 3: idx = KR;
  227.             }
  228.         if (c == Tcap[idx].tc_str[ci]) {
  229.             kmatch[i]++;
  230.             if (Tcap[idx].tc_len == ci+1) {
  231.                 ci = 0;
  232.                 return (0x80 | idx);
  233.                 }
  234.             match = YES;
  235.             }
  236.         }
  237.     buf[ci++] = c;        /* save input char in temp buffer */
  238.     if (match == YES) goto loop;
  239.  
  240.     pushback (&buf[1], ci-1);
  241.     return (buf[0]);
  242.     }
  243.