home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / memacs / ue311c.arc / TCAP.C < prev    next >
C/C++ Source or Header  |  1990-08-16  |  15KB  |  584 lines

  1. /*    tcap:    Unix V5, SUN OS, SCO XENIX, V7 and BS4.2 Termcap video driver
  2.         for MicroEMACS 3.10
  3.  
  4.          12-10-88 - Modifications made by Guy Turcotte to accomodate
  5.                     SunOS V4.0 and Xenix V2.2.1 :
  6.  
  7.                   SunOS mods:
  8.                   
  9.                   o p_seq field of TBIND struct augmented to 10 chars
  10.                     to take into account longer definitions for keys
  11.                     (some Sun's keys definitions need at least 7 chars...)
  12.                     as such, the code in get1key has been modified to take
  13.                     care of the longer p_seq string.
  14.  
  15.                   o tcapopen modified to take care of the tgetstr problem
  16.                     (returns NULL on undefined keys instead of a valid
  17.                     string pointer...)
  18.  
  19.                   o The timout algorithm of get1key has been modified to
  20.                     take care of the following select() function problem:
  21.                     if some chars are already in the terminal buffer before
  22.                     select is called and no others char appears on the terminal,
  23.                     it will timeout anyway... (maybe a feature of SunOs V4.0)
  24.  
  25.                   Xenix mods:
  26.  
  27.                   o The first two points indicated above are applicable for
  28.                     the Xenix OS
  29.  
  30.                   o With my current knowledge, I can't find a clean solution
  31.                     to the timeout problem of the get1key function
  32.                     under Xenix. I modified the code to get rid of the BSD code 
  33.                     (via the #if directive) and use the Xenix nap() and rdchk()
  34.                     functions to 
  35.                     make a 1/30 second wait. Seems to work as long as there is
  36.                     not to much of activity from other processes on the system.
  37.                     (The link command of the makefile must be modified to
  38.                     link with the x library... you must add the option -lx)
  39.  
  40.                   o The input.c file has been modified to not include the
  41.                     get1key function defined there in the case of USG. The
  42.                     #if directive preceeding the get1key definition has been
  43.                     modified from:
  44.  
  45.                      #if (V7 == 0) && (BSD == 0)
  46.  
  47.                     to:
  48.  
  49.                      #if (V7 == 0) && (BSD == 0) && (USG == 0)
  50.                      
  51.                   o The following lines define the new termcap entry for
  52.                     the ansi kind of terminal: it permits the use of functions
  53.                     keys F1 .. F10 and keys HOME,END,PgUp,PgDn on the IBM PC
  54.                     keyboard (the last 3 lines of the definition have been
  55.                     added):
  56.  
  57.  li|ansi|Ansi standard crt:\
  58.      :al=\E[L:am:bs:cd=\E[J:ce=\E[K:cl=\E[2J\E[H:cm=\E[%i%d;%dH:co#80:\
  59.      :dc=\E[P:dl=\E[M:do=\E[B:bt=\E[Z:ei=:ho=\E[H:ic=\E[@:im=:li#25:\
  60.      :nd=\E[C:pt:so=\E[7m:se=\E[m:us=\E[4m:ue=\E[m:up=\E[A:\
  61.      :kb=^h:ku=\E[A:kd=\E[B:kl=\E[D:kr=\E[C:eo:sf=\E[S:sr=\E[T:\
  62.      :GS=\E[12m:GE=\E[10m:GV=\63:GH=D:\
  63.      :GC=E:GL=\64:GR=C:RT=^J:G1=?:G2=Z:G3=@:G4=Y:GU=A:GD=B:\
  64.      :CW=\E[M:NU=\E[N:RF=\E[O:RC=\E[P:\
  65.      :WL=\E[S:WR=\E[T:CL=\E[U:CR=\E[V:\
  66.      :HM=\E[H:EN=\E[F:PU=\E[I:PD=\E[G:\
  67.      :k1=\E[M:k2=\E[N:k3=\E[O:k4=\E[P:k5=\E[Q:\
  68.      :k6=\E[R:k7=\E[S:k8=\E[T:k9=\E[U:k0=\E[V:\
  69.      :kh=\E[H:kH=\E[F:kA=\E[L:kN=\E[G:kP=\E[I:
  70.                     
  71. */
  72.  
  73. #define termdef 1            /* don't define "term" external */
  74.  
  75. #include <stdio.h>
  76. #include    "estruct.h"
  77. #include    "eproto.h"
  78. #include    "edef.h"
  79. #include    "elang.h"
  80.  
  81. #if TERMCAP
  82.  
  83. #if    USG | HPUX | SMOS
  84. #include    <time.h>
  85. #endif
  86. #if    BSD | V7
  87. #include    <sys/types.h>
  88. #include    <sys/time.h>
  89. #endif
  90.  
  91. #define MARGIN    8
  92. #define SCRSIZ    64
  93. #define NPAUSE    10            /* # times thru update to pause */
  94. #define BEL    0x07
  95. #define ESC    0x1B
  96.  
  97. /*    Termcap Sequence definitions    */
  98.  
  99. typedef struct TBIND {
  100.     char p_name[4]; /* sequence name */
  101.     short p_code;    /* resulting keycode of sequence */
  102.     char p_seq[10];    /* terminal escape sequence */
  103. } TBIND;
  104.  
  105. TBIND ttable[] = {
  106.     "bt",    SHFT | CTRL | 'i',    "",    /* backtab */
  107.     "k1",    SPEC | '1',        "",    /* function key 1 */
  108.     "k2",    SPEC | '2',        "",    /* function key 2 */
  109.     "k3",    SPEC | '3',        "",    /* function key 3 */
  110.     "k4",    SPEC | '4',        "",    /* function key 4 */
  111.     "k5",    SPEC | '5',        "",    /* function key 5 */
  112.     "k6",    SPEC | '6',        "",    /* function key 6 */
  113.     "k7",    SPEC | '7',        "",    /* function key 7 */
  114.     "k8",    SPEC | '8',        "",    /* function key 8 */
  115.     "k9",    SPEC | '9',        "",    /* function key 9 */
  116.     "k0",    SPEC | '0',        "",    /* function key 10 */
  117.     "kA",    CTRL | 'O',        "",    /* insert line */
  118.     "kb",    CTRL | 'H',        "",    /* backspace */
  119.     "kC",    CTRL | 'L',        "",    /* clear screen */
  120.     "kD",    SPEC | 'D',        "",    /* delete character */
  121.     "kd",    SPEC | 'N',        "",    /* down cursor */
  122.     "kE",    CTRL | 'K',        "",    /* clear to end of line */
  123.     "kF",    CTRL | 'V',        "",    /* scroll down */
  124.     "kH",    SPEC | '>',        "",    /* home down [END?] key */
  125.     "kh",    SPEC | '<',        "",    /* home */
  126.     "kI",    SPEC | 'C',        "",    /* insert character */
  127.     "kL",    CTRL | 'K',        "",    /* delete line */
  128.     "kl",    SPEC | 'B',        "",    /* left cursor */
  129.     "kN",    SPEC | 'V',        "",    /* next page */
  130.     "kP",    SPEC | 'Z',        "",    /* previous page */
  131.     "kR",    CTRL | 'Z',        "",    /* scroll down */
  132.     "kr",    SPEC | 'F',        "",    /* right cursor */
  133.     "ku",    SPEC | 'P',        "",    /* up cursor */
  134. #if    SMOS
  135.     "ka",    SPEC | 'J',        "",    /* function key 11*/
  136.     "F1",    SPEC | 'K',        "",    /* function key 12*/
  137.     "F2",    SPEC | 'L',        "",    /* function key 13*/
  138.     "F3",    SPEC | 'M',        "",    /* function key 14*/
  139.     "F4",    SPEC | 'N',        "",    /* function key 15*/
  140.     "F5",    SPEC | 'O',        "",    /* function key 16*/
  141.     "F6",    SHFT | SPEC | '1',    "",    /* S-function key 1 */
  142.     "F7",    SHFT | SPEC | '2',    "",    /* S-function key 2 */
  143.     "F8",    SHFT | SPEC | '3',    "",    /* S-function key 3 */
  144.     "F9",    SHFT | SPEC | '4',    "",    /* S-function key 4 */
  145.     "FA",    SHFT | SPEC | '5',    "",    /* S-function key 5 */
  146.     "FB",    SHFT | SPEC | '6',    "",    /* S-function key 6 */
  147.     "FC",    SHFT | SPEC | '7',    "",    /* S-function key 7 */
  148.     "FD",    SHFT | SPEC | '8',    "",    /* S-function key 8 */
  149.     "FE",    SHFT | SPEC | '9',    "",    /* S-function key 9 */
  150.     "FF",    SHFT | SPEC | '0',    "",    /* S-function key 10*/
  151.     "FG",    SHFT | SPEC | 'J',    "",    /* S-function key 11*/
  152.     "FH",    SHFT | SPEC | 'K',    "",    /* S-function key 12*/
  153.     "FI",    SHFT | SPEC | 'L',    "",    /* S-function key 13*/
  154.     "FJ",    SHFT | SPEC | 'M',    "",    /* S-function key 14*/
  155.     "FK",    SHFT | SPEC | 'N',    "",    /* S-function key 15*/
  156.     "FL",    SHFT | SPEC | 'O',    "",    /* S-function key 16*/
  157. #endif
  158. };
  159.  
  160. #define    NTBINDS    sizeof(ttable)/sizeof(TBIND)
  161.  
  162. extern int    ttopen();
  163. extern int    ttgetc();
  164. extern int    ttputc();
  165. extern int    tgetnum();
  166. extern int    ttflush();
  167. extern int    ttclose();
  168. extern int    tcapkopen();
  169. extern int    tcapkclose();
  170. extern int    tcapgetc();
  171. extern int    tcapmove();
  172. extern int    tcapeeol();
  173. extern int    tcapeeop();
  174. extern int    tcapbeep();
  175. extern int    tcaprev();
  176. extern int    tcapcres();
  177. extern int    tcapopen();
  178. extern int    tcapclose();
  179. extern int    tput();
  180. extern char    *tgoto();
  181. #if    COLOR
  182. extern    int    tcapfcol();
  183. extern    int    tcapbcol();
  184. #endif
  185.  
  186. #define TCAPSLEN 1024
  187. char tcapbuf[TCAPSLEN];
  188. char *UP, PC, *CM, *CE, *CL, *SO, *SE, *IS, *KS, *KE;
  189.  
  190. TERM term = {
  191.     0, 0, 0, 0,    /* these four values are set dynamically at open time */
  192.     MARGIN,
  193.     SCRSIZ,
  194.     NPAUSE,
  195.     tcapopen,
  196.     tcapclose,
  197.     tcapkopen,
  198.     tcapkclose,
  199.     tcapgetc,
  200.     ttputc,
  201.     ttflush,
  202.     tcapmove,
  203.     tcapeeol,
  204.     tcapeeop,
  205.     tcapbeep,
  206.     tcaprev,
  207.     tcapcres
  208. #if    COLOR
  209.     , tcapfcol,
  210.     tcapbcol
  211. #endif
  212. };
  213.  
  214. /*    input buffers and pointers    */
  215.  
  216. #define    IBUFSIZE    64    /* this must be a power of 2 */
  217.  
  218. unsigned char in_buf[IBUFSIZE];    /* input character buffer */
  219. int in_next = 0;        /* pos to retrieve next input character */
  220. int in_last = 0;        /* pos to place most recent input character */
  221.  
  222. in_init()    /* initialize the input buffer */
  223.  
  224. {
  225.     in_next = in_last = 0;
  226. }
  227.  
  228. in_check()    /* is the input buffer non-empty? */
  229.  
  230. {
  231.     if (in_next == in_last)
  232.         return(FALSE);
  233.     else
  234.         return(TRUE);
  235. }
  236.  
  237. in_put(event)
  238.  
  239. int event;    /* event to enter into the input buffer */
  240.  
  241. {
  242.     in_buf[in_last++] = event;
  243.     in_last &= (IBUFSIZE - 1);
  244. }
  245.  
  246. int in_get()    /* get an event from the input buffer */
  247.  
  248. {
  249.     register int event;    /* event to return */
  250.  
  251.     event = in_buf[in_next++];
  252.     in_next &= (IBUFSIZE - 1);
  253.     return(event);
  254. }
  255.  
  256. /*    Open the terminal
  257.     put it in RA mode
  258.     learn about the screen size
  259.     read TERMCAP strings for function keys
  260. */
  261.  
  262. tcapopen()
  263.  
  264. {
  265.     register int index;        /* general index */
  266.     char *t, *p;
  267.     char tcbuf[1024];
  268.     char *tv_stype;
  269.     char err_str[72];
  270.     char *getenv();
  271.     char *tgetstr();
  272.  
  273.     if ((tv_stype = getenv("TERM")) == NULL) {
  274.         puts(TEXT182);
  275. /*             "Environment variable TERM not defined!" */
  276.         meexit(1);
  277.     }
  278.  
  279.     if ((tgetent(tcbuf, tv_stype)) != 1) {
  280.         sprintf(err_str, TEXT183, tv_stype);
  281. /*                 "Unknown terminal type %s!" */
  282.         puts(err_str);
  283.         meexit(1);
  284.     }
  285.  
  286.  
  287.     if ((term.t_nrow=(short)tgetnum("li")-1) == -1) {
  288.            puts(TEXT184);
  289. /*            "termcap entry incomplete (lines)" */
  290.            meexit(1);
  291.     }
  292.     term.t_mrow =  term.t_nrow;
  293.  
  294.     if ((term.t_ncol=(short)tgetnum("co")) == -1){
  295.         puts(TEXT185);
  296. /*            "Termcap entry incomplete (columns)" */
  297.         meexit(1);
  298.     }
  299.     term.t_mcol = term.t_ncol;
  300.  
  301.     p = tcapbuf;
  302.     t = tgetstr("pc", &p);
  303.     if (t)
  304.         PC = *t;
  305.  
  306.     CL = tgetstr("cl", &p);
  307.     CM = tgetstr("cm", &p);
  308.     CE = tgetstr("ce", &p);
  309.     UP = tgetstr("up", &p);
  310.     SE = tgetstr("se", &p);
  311.     SO = tgetstr("so", &p);
  312.     if (SO != NULL)
  313.         revexist = TRUE;
  314.  
  315.     if (CL == NULL || CM == NULL || UP == NULL)
  316.     {
  317.         puts(TEXT186);
  318. /*             "Incomplete termcap entry\n" */
  319.         meexit(1);
  320.     }
  321.  
  322.     if (CE == NULL)     /* will we be able to use clear to EOL? */
  323.         eolexist = FALSE;
  324.          
  325.     IS = tgetstr("is", &p); /* extract init string */
  326.     KS = tgetstr("ks", &p); /* extract keypad transmit string */
  327.     KE = tgetstr("ke", &p); /* extract keypad transmit end string */
  328.             
  329.     /* read definitions of various function keys into ttable */
  330.     for (index = 0; index < NTBINDS; index++) {
  331.         strcpy(ttable[index].p_seq,
  332.             fixnull(tgetstr(ttable[index].p_name, &p)));
  333.     }
  334.  
  335.     /* tell unix we are goint to use the terminal */
  336.     ttopen();
  337.  
  338.     /* make sure we don't over run the buffer (TOO LATE I THINK) */
  339.     if (p >= &tcapbuf[TCAPSLEN]) {
  340.         puts(TEXT187);
  341. /*             "Terminal description too big!\n" */
  342.         meexit(1);
  343.     }
  344.  
  345.     /* send init strings if defined */
  346.     if (IS != NULL)
  347.         putpad(IS);
  348.  
  349.     if (KS != NULL)
  350.         putpad(KS);
  351.  
  352.     /* initialize the input buffer */
  353.     in_init();
  354. }
  355.  
  356. tcapclose()
  357. {
  358.     /* send end-of-keypad-transmit string if defined */
  359.     if (KE != NULL)
  360.         putpad(KE);
  361.     ttclose();
  362. }
  363.  
  364. tcapkopen()
  365.  
  366. {
  367.     strcpy(sres, "NORMAL");
  368. }
  369.  
  370. tcapkclose()
  371.  
  372. {
  373. }
  374.  
  375. unsigned int extcode(c)
  376.  
  377. unsigned int c;
  378.  
  379. {
  380.     return(c);
  381. }
  382.  
  383. /*    TCAPGETC:    Get on character.  Resolve and setup all the
  384.             appropriate keystroke escapes as defined in
  385.             the comments at the beginning of input.c
  386. */
  387.  
  388. int tcapgetc()
  389.  
  390. {
  391.     int c;        /* current extended keystroke */
  392.  
  393.     /* if there are already keys waiting.... send them */
  394.     if (in_check())
  395.         return(in_get());
  396.  
  397.     /* otherwise... get the char for now */
  398.     c = get1key();
  399.  
  400.     /* unfold the control bit back into the character */
  401.     if (CTRL & c)
  402.         c = (c & ~ CTRL) - '@';
  403.  
  404.     /* fold the event type into the input stream as an escape seq */
  405.     if ((c & ~255) != 0) {
  406.         in_put(0);        /* keyboard escape prefix */
  407.         in_put(c >> 8);        /* event type */
  408.         in_put(c & 255);    /* event code */
  409.         return(tcapgetc());
  410.     }
  411.  
  412.     return(c);
  413. }
  414.  
  415. /*    GET1KEY:    Get one keystroke. The only prefixs legal here
  416.             are the SPEC and CTRL prefixes.
  417.  
  418.     Note:
  419.  
  420.         Escape sequences that are generated by terminal function
  421.         and cursor keys could be confused with the user typing
  422.         the default META prefix followed by other chars... ie
  423.  
  424.         UPARROW  =  <ESC>A   on some terminals...
  425.         apropos  =  M-A
  426.  
  427.         The difference is determined by measuring the time between
  428.         the input of the first and second character... if an <ESC>
  429.         is types, and is not followed by another char in 1/30 of
  430.         a second (think 300 baud) then it is a user input, otherwise
  431.         it was generated by an escape sequence and should be SPECed.
  432. */
  433.  
  434. int PASCAL NEAR get1key()
  435.  
  436. {
  437.     register int c;
  438.     register int index;    /* index into termcap binding table */
  439.     char *sp;
  440. #if    BSD | V7 | HPUX
  441.     int fdset;
  442.     struct timeval timeout;
  443. #endif
  444.     char cseq[10];        /* current sequence being parsed */
  445.  
  446.     c = ttgetc();
  447.  
  448.     /* if it is not an escape character */
  449.     if (c != 27)
  450.             return(c);
  451.  
  452.     /* process a possible escape sequence */
  453.     /* set up to check the keyboard for input */
  454. #if    BSD | V7 | HPUX
  455.     fdset = 1;
  456.     timeout.tv_sec = 0;
  457.     timeout.tv_usec = 35000L;
  458.  
  459.     /* check to see if things are pending soon */
  460.     if (kbdmode != PLAY &&
  461.         select(1, &fdset, (int *)NULL, (int *)NULL, &timeout) == 0)
  462.         return(CTRL | '[');
  463. #endif
  464.  
  465. #if XENIX | SUNOS
  466.     if ((kbdmode != PLAY) && (rdchk(0) <= 0)) {
  467.         nap(35L);
  468.         if (rdchk(0) <= 0)
  469.             return(CTRL | '[');
  470.     }
  471. #endif
  472.  
  473. #if    USG | SMOS
  474.     /* we don't know how to do this check for a pending char within
  475.        1/30th of a second machine independantly in the general System V
  476.        case.... so we don't */
  477.     if (kbdmode != PLAY)
  478.         return(CTRL | '[');
  479. #endif
  480.  
  481.     /* a key is pending within 1/30 of a sec... its an escape sequence */
  482.     cseq[0] = 27;
  483.     sp = &cseq[1];
  484.     while (sp < &cseq[6]) {
  485.         c = ttgetc();
  486.         *sp++ = c;
  487.         *sp = 0;
  488.         for (index = 0; index < NTBINDS; index++) {
  489.             if (strcmp(cseq, ttable[index].p_seq) == 0)
  490.                 return(ttable[index].p_code);
  491.         }
  492.     }
  493.     return(SPEC | 0);
  494. }
  495.  
  496. tcapmove(row, col)
  497. register int row, col;
  498. {
  499.     putpad(tgoto(CM, col, row));
  500. }
  501.  
  502. tcapeeol()
  503. {
  504.     putpad(CE);
  505. }
  506.  
  507. tcapeeop()
  508. {
  509.     putpad(CL);
  510. }
  511.  
  512. tcaprev(state)        /* change reverse video status */
  513.  
  514. int state;        /* FALSE = normal video, TRUE = reverse video */
  515.  
  516. {
  517. /*    static int revstate = FALSE;*/
  518.  
  519.     if (state) {
  520.         if (SO != NULL)
  521.             putpad(SO);
  522.     } else
  523.         if (SE != NULL)
  524.             putpad(SE);
  525. }
  526.  
  527. tcapcres()    /* change screen resolution */
  528.  
  529. {
  530.     return(TRUE);
  531. }
  532.  
  533. spal(dummy)    /* change palette string */
  534.  
  535. {
  536.     /*    Does nothing here    */
  537. }
  538.  
  539. #if    COLOR
  540. tcapfcol()    /* no colors here, ignore this */
  541. {
  542. }
  543.  
  544. tcapbcol()    /* no colors here, ignore this */
  545. {
  546. }
  547. #endif
  548.  
  549. tcapbeep()
  550. {
  551.     ttputc(BEL);
  552. }
  553.  
  554. putpad(str)
  555. char    *str;
  556. {
  557.     tputs(str, 1, ttputc);
  558. }
  559.  
  560. putnpad(str, n)
  561. char    *str;
  562. {
  563.     tputs(str, n, ttputc);
  564. }
  565.  
  566.  
  567. #if    FLABEL
  568. fnclabel(f, n)        /* label a function key */
  569.  
  570. int f,n;    /* default flag, numeric argument [unused] */
  571.  
  572. {
  573.     /* on machines with no function keys...don't bother */
  574.     return(TRUE);
  575. }
  576. #endif
  577. #else
  578.  
  579. hello()
  580. {
  581. }
  582.  
  583. #endif
  584.