home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / UE311C.ZIP / TCAP.C < prev    next >
C/C++ Source or Header  |  1991-10-04  |  15KB  |  585 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.     0, 0,
  193.     MARGIN,
  194.     SCRSIZ,
  195.     NPAUSE,
  196.     tcapopen,
  197.     tcapclose,
  198.     tcapkopen,
  199.     tcapkclose,
  200.     tcapgetc,
  201.     ttputc,
  202.     ttflush,
  203.     tcapmove,
  204.     tcapeeol,
  205.     tcapeeop,
  206.     tcapbeep,
  207.     tcaprev,
  208.     tcapcres
  209. #if    COLOR
  210.     , tcapfcol,
  211.     tcapbcol
  212. #endif
  213. };
  214.  
  215. /*    input buffers and pointers    */
  216.  
  217. #define    IBUFSIZE    64    /* this must be a power of 2 */
  218.  
  219. unsigned char in_buf[IBUFSIZE];    /* input character buffer */
  220. int in_next = 0;        /* pos to retrieve next input character */
  221. int in_last = 0;        /* pos to place most recent input character */
  222.  
  223. in_init()    /* initialize the input buffer */
  224.  
  225. {
  226.     in_next = in_last = 0;
  227. }
  228.  
  229. in_check()    /* is the input buffer non-empty? */
  230.  
  231. {
  232.     if (in_next == in_last)
  233.         return(FALSE);
  234.     else
  235.         return(TRUE);
  236. }
  237.  
  238. in_put(event)
  239.  
  240. int event;    /* event to enter into the input buffer */
  241.  
  242. {
  243.     in_buf[in_last++] = event;
  244.     in_last &= (IBUFSIZE - 1);
  245. }
  246.  
  247. int in_get()    /* get an event from the input buffer */
  248.  
  249. {
  250.     register int event;    /* event to return */
  251.  
  252.     event = in_buf[in_next++];
  253.     in_next &= (IBUFSIZE - 1);
  254.     return(event);
  255. }
  256.  
  257. /*    Open the terminal
  258.     put it in RA mode
  259.     learn about the screen size
  260.     read TERMCAP strings for function keys
  261. */
  262.  
  263. tcapopen()
  264.  
  265. {
  266.     register int index;        /* general index */
  267.     char *t, *p;
  268.     char tcbuf[1024];
  269.     char *tv_stype;
  270.     char err_str[72];
  271.     char *getenv();
  272.     char *tgetstr();
  273.  
  274.     if ((tv_stype = getenv("TERM")) == NULL) {
  275.         puts(TEXT182);
  276. /*             "Environment variable TERM not defined!" */
  277.         meexit(1);
  278.     }
  279.  
  280.     if ((tgetent(tcbuf, tv_stype)) != 1) {
  281.         sprintf(err_str, TEXT183, tv_stype);
  282. /*                 "Unknown terminal type %s!" */
  283.         puts(err_str);
  284.         meexit(1);
  285.     }
  286.  
  287.  
  288.     if ((term.t_nrow=(short)tgetnum("li")-1) == -1) {
  289.            puts(TEXT184);
  290. /*            "termcap entry incomplete (lines)" */
  291.            meexit(1);
  292.     }
  293.     term.t_mrow =  term.t_nrow;
  294.  
  295.     if ((term.t_ncol=(short)tgetnum("co")) == -1){
  296.         puts(TEXT185);
  297. /*            "Termcap entry incomplete (columns)" */
  298.         meexit(1);
  299.     }
  300.     term.t_mcol = term.t_ncol;
  301.  
  302.     p = tcapbuf;
  303.     t = tgetstr("pc", &p);
  304.     if (t)
  305.         PC = *t;
  306.  
  307.     CL = tgetstr("cl", &p);
  308.     CM = tgetstr("cm", &p);
  309.     CE = tgetstr("ce", &p);
  310.     UP = tgetstr("up", &p);
  311.     SE = tgetstr("se", &p);
  312.     SO = tgetstr("so", &p);
  313.     if (SO != NULL)
  314.         revexist = TRUE;
  315.  
  316.     if (CL == NULL || CM == NULL || UP == NULL)
  317.     {
  318.         puts(TEXT186);
  319. /*             "Incomplete termcap entry\n" */
  320.         meexit(1);
  321.     }
  322.  
  323.     if (CE == NULL)     /* will we be able to use clear to EOL? */
  324.         eolexist = FALSE;
  325.          
  326.     IS = tgetstr("is", &p); /* extract init string */
  327.     KS = tgetstr("ks", &p); /* extract keypad transmit string */
  328.     KE = tgetstr("ke", &p); /* extract keypad transmit end string */
  329.             
  330.     /* read definitions of various function keys into ttable */
  331.     for (index = 0; index < NTBINDS; index++) {
  332.         strcpy(ttable[index].p_seq,
  333.             fixnull(tgetstr(ttable[index].p_name, &p)));
  334.     }
  335.  
  336.     /* tell unix we are goint to use the terminal */
  337.     ttopen();
  338.  
  339.     /* make sure we don't over run the buffer (TOO LATE I THINK) */
  340.     if (p >= &tcapbuf[TCAPSLEN]) {
  341.         puts(TEXT187);
  342. /*             "Terminal description too big!\n" */
  343.         meexit(1);
  344.     }
  345.  
  346.     /* send init strings if defined */
  347.     if (IS != NULL)
  348.         putpad(IS);
  349.  
  350.     if (KS != NULL)
  351.         putpad(KS);
  352.  
  353.     /* initialize the input buffer */
  354.     in_init();
  355. }
  356.  
  357. tcapclose()
  358. {
  359.     /* send end-of-keypad-transmit string if defined */
  360.     if (KE != NULL)
  361.         putpad(KE);
  362.     ttclose();
  363. }
  364.  
  365. tcapkopen()
  366.  
  367. {
  368.     strcpy(sres, "NORMAL");
  369. }
  370.  
  371. tcapkclose()
  372.  
  373. {
  374. }
  375.  
  376. unsigned int extcode(c)
  377.  
  378. unsigned int c;
  379.  
  380. {
  381.     return(c);
  382. }
  383.  
  384. /*    TCAPGETC:    Get on character.  Resolve and setup all the
  385.             appropriate keystroke escapes as defined in
  386.             the comments at the beginning of input.c
  387. */
  388.  
  389. int tcapgetc()
  390.  
  391. {
  392.     int c;        /* current extended keystroke */
  393.  
  394.     /* if there are already keys waiting.... send them */
  395.     if (in_check())
  396.         return(in_get());
  397.  
  398.     /* otherwise... get the char for now */
  399.     c = get1key();
  400.  
  401.     /* unfold the control bit back into the character */
  402.     if (CTRL & c)
  403.         c = (c & ~ CTRL) - '@';
  404.  
  405.     /* fold the event type into the input stream as an escape seq */
  406.     if ((c & ~255) != 0) {
  407.         in_put(0);        /* keyboard escape prefix */
  408.         in_put(c >> 8);        /* event type */
  409.         in_put(c & 255);    /* event code */
  410.         return(tcapgetc());
  411.     }
  412.  
  413.     return(c);
  414. }
  415.  
  416. /*    GET1KEY:    Get one keystroke. The only prefixs legal here
  417.             are the SPEC and CTRL prefixes.
  418.  
  419.     Note:
  420.  
  421.         Escape sequences that are generated by terminal function
  422.         and cursor keys could be confused with the user typing
  423.         the default META prefix followed by other chars... ie
  424.  
  425.         UPARROW  =  <ESC>A   on some terminals...
  426.         apropos  =  M-A
  427.  
  428.         The difference is determined by measuring the time between
  429.         the input of the first and second character... if an <ESC>
  430.         is types, and is not followed by another char in 1/30 of
  431.         a second (think 300 baud) then it is a user input, otherwise
  432.         it was generated by an escape sequence and should be SPECed.
  433. */
  434.  
  435. int PASCAL NEAR get1key()
  436.  
  437. {
  438.     register int c;
  439.     register int index;    /* index into termcap binding table */
  440.     char *sp;
  441. #if    BSD | V7 | HPUX
  442.     int fdset;
  443.     struct timeval timeout;
  444. #endif
  445.     char cseq[10];        /* current sequence being parsed */
  446.  
  447.     c = ttgetc();
  448.  
  449.     /* if it is not an escape character */
  450.     if (c != 27)
  451.             return(c);
  452.  
  453.     /* process a possible escape sequence */
  454.     /* set up to check the keyboard for input */
  455. #if    BSD | V7 | HPUX
  456.     fdset = 1;
  457.     timeout.tv_sec = 0;
  458.     timeout.tv_usec = 35000L;
  459.  
  460.     /* check to see if things are pending soon */
  461.     if (kbdmode != PLAY &&
  462.         select(1, &fdset, (int *)NULL, (int *)NULL, &timeout) == 0)
  463.         return(CTRL | '[');
  464. #endif
  465.  
  466. #if XENIX | SUNOS
  467.     if ((kbdmode != PLAY) && (rdchk(0) <= 0)) {
  468.         nap(35L);
  469.         if (rdchk(0) <= 0)
  470.             return(CTRL | '[');
  471.     }
  472. #endif
  473.  
  474. #if    USG | SMOS
  475.     /* we don't know how to do this check for a pending char within
  476.        1/30th of a second machine independantly in the general System V
  477.        case.... so we don't */
  478.     if (kbdmode != PLAY)
  479.         return(CTRL | '[');
  480. #endif
  481.  
  482.     /* a key is pending within 1/30 of a sec... its an escape sequence */
  483.     cseq[0] = 27;
  484.     sp = &cseq[1];
  485.     while (sp < &cseq[6]) {
  486.         c = ttgetc();
  487.         *sp++ = c;
  488.         *sp = 0;
  489.         for (index = 0; index < NTBINDS; index++) {
  490.             if (strcmp(cseq, ttable[index].p_seq) == 0)
  491.                 return(ttable[index].p_code);
  492.         }
  493.     }
  494.     return(SPEC | 0);
  495. }
  496.  
  497. tcapmove(row, col)
  498. register int row, col;
  499. {
  500.     putpad(tgoto(CM, col, row));
  501. }
  502.  
  503. tcapeeol()
  504. {
  505.     putpad(CE);
  506. }
  507.  
  508. tcapeeop()
  509. {
  510.     putpad(CL);
  511. }
  512.  
  513. tcaprev(state)        /* change reverse video status */
  514.  
  515. int state;        /* FALSE = normal video, TRUE = reverse video */
  516.  
  517. {
  518. /*    static int revstate = FALSE;*/
  519.  
  520.     if (state) {
  521.         if (SO != NULL)
  522.             putpad(SO);
  523.     } else
  524.         if (SE != NULL)
  525.             putpad(SE);
  526. }
  527.  
  528. tcapcres()    /* change screen resolution */
  529.  
  530. {
  531.     return(TRUE);
  532. }
  533.  
  534. spal(dummy)    /* change palette string */
  535.  
  536. {
  537.     /*    Does nothing here    */
  538. }
  539.  
  540. #if    COLOR
  541. tcapfcol()    /* no colors here, ignore this */
  542. {
  543. }
  544.  
  545. tcapbcol()    /* no colors here, ignore this */
  546. {
  547. }
  548. #endif
  549.  
  550. tcapbeep()
  551. {
  552.     ttputc(BEL);
  553. }
  554.  
  555. putpad(str)
  556. char    *str;
  557. {
  558.     tputs(str, 1, ttputc);
  559. }
  560.  
  561. putnpad(str, n)
  562. char    *str;
  563. {
  564.     tputs(str, n, ttputc);
  565. }
  566.  
  567.  
  568. #if    FLABEL
  569. fnclabel(f, n)        /* label a function key */
  570.  
  571. int f,n;    /* default flag, numeric argument [unused] */
  572.  
  573. {
  574.     /* on machines with no function keys...don't bother */
  575.     return(TRUE);
  576. }
  577. #endif
  578. #else
  579.  
  580. hello()
  581. {
  582. }
  583.  
  584. #endif
  585.