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

  1. /*
  2.  * The routines in this file provide support for ANSI style terminals
  3.  * over a serial line. The serial I/O services are provided by routines in
  4.  * termio.c. It compiles into nothing if not an ANSI device.
  5.  *
  6.  * 12-Dec-1989    KAM
  7.  * If we're on VMS, leave the keypad turned on if the terminal is set to
  8.  * application keypad mode.
  9.  */
  10.  
  11. #define    termdef    1            /* don't define term external */
  12.  
  13. #include        <stdio.h>
  14. #include    "estruct.h"
  15. #include    "eproto.h"
  16. #include        "edef.h"
  17. #include    "elang.h"
  18.  
  19. #if     ANSI
  20.  
  21. #if    PROTO
  22. int PASCAL NEAR fnclabel(int f, int n);
  23. int PASCAL NEAR readparam( int *v);
  24. void PASCAL NEAR dobbnmouse(void);
  25. void PASCAL NEAR docsi( int oh);
  26. void PASCAL NEAR ttputs(char *string);
  27. #else
  28. int PASCAL NEAR fnclabel();
  29. int PASCAL NEAR readparam();
  30. void PASCAL NEAR dobbnmouse();
  31. void PASCAL NEAR docsi();
  32. void PASCAL NEAR ttputs();
  33. #endif
  34.  
  35. #if VMS
  36. #include ttdef
  37. #include tt2def
  38.  
  39. /*
  40.     This structure, along with ttdef.h, is good for manipulating
  41.     terminal characteristics.
  42. */
  43. typedef struct
  44. {/* Terminal characteristics buffer */
  45.     unsigned char class, type;
  46.     unsigned short width;
  47.     unsigned tt1 : 24;
  48.     unsigned char page;
  49.     unsigned long tt2;
  50. } TTCHAR;
  51. extern NOSHARE TTCHAR orgchar;            /* Original characteristics */
  52. #endif
  53.  
  54. #define NROW    25                      /* Screen size.                 */
  55. #define NCOL    80                      /* Edit if you want to.         */
  56. #define    NPAUSE    100            /* # times thru update to pause */
  57. #define    MARGIN    8            /* size of minimim margin and    */
  58. #define    SCRSIZ    64            /* scroll size for extended lines */
  59. #define BEL     0x07                    /* BEL character.               */
  60. #define ESC     0x1B                    /* ESC character.               */
  61.  
  62. /* Forward references.          */
  63. extern int PASCAL NEAR ansimove();
  64. extern int PASCAL NEAR ansieeol();
  65. extern int PASCAL NEAR ansieeop();
  66. extern int PASCAL NEAR ansibeep();
  67. extern int PASCAL NEAR ansiopen();
  68. extern int PASCAL NEAR ansirev();
  69. extern int PASCAL NEAR ansiclose();
  70. extern int PASCAL NEAR ansikopen();
  71. extern int PASCAL NEAR ansikclose();
  72. extern int PASCAL NEAR ansicres();
  73. extern int PASCAL NEAR ansiparm();
  74. extern int PASCAL NEAR ansigetc();
  75.  
  76. #if    COLOR
  77. extern int PASCAL NEAR ansifcol();
  78. extern int PASCAL NEAR ansibcol();
  79.  
  80. static int cfcolor = -1;    /* current forground color */
  81. static int cbcolor = -1;    /* current background color */
  82.  
  83. #if    AMIGA
  84. /* Apparently the AMIGA does not follow the ANSI standards as
  85.  * regards to colors....maybe because of the default pallette
  86.  * settings?  Color translation table needed.
  87.  */
  88.  
  89. int coltran[16] = {2,  3,  5,  7, 0,  4,  6,  1,
  90.            8, 12, 10, 14, 9, 13, 11, 15};
  91. #endif
  92. #endif
  93.  
  94. /*
  95.  * Standard terminal interface dispatch table. Most of the fields point into
  96.  * "termio" code.
  97.  */
  98. NOSHARE TERM term    = {
  99.     NROW-1,
  100.         NROW-1,
  101.         NCOL,
  102.         NCOL,
  103.     MARGIN,
  104.     SCRSIZ,
  105.     NPAUSE,
  106.         ansiopen,
  107.         ansiclose,
  108.     ansikopen,
  109.     ansikclose,
  110.         ansigetc,
  111.         ttputc,
  112.         ttflush,
  113.         ansimove,
  114.         ansieeol,
  115.         ansieeop,
  116.         ansibeep,
  117.     ansirev,
  118.     ansicres
  119. #if    COLOR
  120.     , ansifcol,
  121.     ansibcol
  122. #endif
  123. };
  124.  
  125. #if    COLOR
  126. PASCAL NEAR ansifcol(color)        /* set the current output color */
  127.  
  128. int color;    /* color to set */
  129.  
  130. {
  131.     if (color == cfcolor)
  132.         return;
  133.     ttputc(ESC);
  134.     ttputc('[');
  135. #if    AMIGA
  136.     ansiparm(coltran[color]+30);
  137. #else
  138.     ansiparm(color+30);
  139. #endif
  140.     ttputc('m');
  141.     cfcolor = color;
  142. }
  143.  
  144. PASCAL NEAR ansibcol(color)        /* set the current background color */
  145.  
  146. int color;    /* color to set */
  147.  
  148. {
  149.     if (color == cbcolor)
  150.         return;
  151.     ttputc(ESC);
  152.     ttputc('[');
  153. #if    AMIGA
  154.     ansiparm(coltran[color]+40);
  155. #else
  156.     ansiparm(color+40);
  157. #endif
  158.     ttputc('m');
  159.         cbcolor = color;
  160. }
  161. #endif
  162.  
  163. PASCAL NEAR ansimove(row, col)
  164. {
  165.         ttputc(ESC);
  166.         ttputc('[');
  167.         ansiparm(row+1);
  168.         ttputc(';');
  169.         ansiparm(col+1);
  170.         ttputc('H');
  171. }
  172.  
  173. PASCAL NEAR ansieeol()
  174. {
  175.         ttputc(ESC);
  176.         ttputc('[');
  177.         ttputc('K');
  178. }
  179.  
  180. PASCAL NEAR ansieeop()
  181. {
  182. #if    COLOR
  183.     ansifcol(gfcolor);
  184.     ansibcol(gbcolor);
  185. #endif
  186.         ttputc(ESC);
  187.         ttputc('[');
  188.         ttputc('J');
  189. }
  190.  
  191. PASCAL NEAR ansirev(state)        /* change reverse video state */
  192.  
  193. int state;    /* TRUE = reverse, FALSE = normal */
  194.  
  195. {
  196. #if    COLOR
  197.     int ftmp, btmp;        /* temporaries for colors */
  198. #endif
  199.  
  200.     ttputc(ESC);
  201.     ttputc('[');
  202.     ttputc(state ? '7': '0');
  203.     ttputc('m');
  204. #if    COLOR
  205.     if (state == FALSE) {
  206.         ftmp = cfcolor;
  207.         btmp = cbcolor;
  208.         cfcolor = -1;
  209.         cbcolor = -1;
  210.         ansifcol(ftmp);
  211.         ansibcol(btmp);
  212.     }
  213. #endif
  214. }
  215.  
  216. PASCAL NEAR ansicres()    /* change screen resolution */
  217.  
  218. {
  219.     return(TRUE);
  220. }
  221.  
  222. #if    PROTO
  223. PASCAL NEAR spal(char *dummy)        /* change pallette settings */
  224. #else
  225. PASCAL NEAR spal(dummy)        /* change pallette settings */
  226.  
  227. char *dummy;
  228. #endif
  229.  
  230. {
  231.     /* none for now */
  232. }
  233.  
  234. PASCAL NEAR ansibeep()
  235. {
  236.         ttputc(BEL);
  237.         ttflush();
  238. }
  239.  
  240. PASCAL NEAR ansiparm(n)
  241. register int    n;
  242. {
  243.         register int q,r;
  244.  
  245.         q = n/10;
  246.         if (q != 0) {
  247.         r = q/10;
  248.         if (r != 0) {
  249.             ttputc((r%10)+'0');
  250.         }
  251.         ttputc((q%10) + '0');
  252.         }
  253.         ttputc((n%10) + '0');
  254. }
  255.  
  256. PASCAL NEAR ansiopen()
  257. {
  258. #if     V7 | USG | HPUX | BSD | SUN | XENIX
  259.         register char *cp;
  260.         char *getenv();
  261.  
  262.         if ((cp = getenv("TERM")) == NULL) {
  263.                 puts(TEXT4);
  264. /*                   "Shell variable TERM not defined!" */
  265.                 meexit(1);
  266.         }
  267.         if (strcmp(cp, "vt100") != 0 && strcmp(cp,"vt200") != 0 &&
  268.                     strcmp(cp,"vt300") != 0) {
  269.                 puts(TEXT5);
  270. /*                   "Terminal type not 'vt100'!" */
  271.                 meexit(1);
  272.         }
  273. #endif
  274. #if    MOUSE & ( V7 | USG | HPUX | BSD | SUN | XENIX | VMS)
  275. /*
  276.     If this is an ansi terminal of at least DEC level
  277.     2 capability, some terminals of this level, such as the "Whack"
  278.     emulator, the VWS terminal emulator, and some versions of XTERM,
  279.     support access to the workstation mouse via escape sequences.  In
  280.     addition, any terminal that conforms to level 2 will, at very least,
  281.     IGNORE the escape sequences for the mouse.
  282. */
  283.     {
  284.     char *s;
  285.  
  286.     s = getenv( "MICROEMACS$MOUSE_ENABLE");
  287.     if( !s) s = "\033[1)u\033[1;3'{\033[1;2'z";
  288.     ttputs( s);
  289.     }
  290. #endif
  291.     strcpy(sres, "NORMAL");
  292.     revexist = TRUE;
  293.         ttopen();
  294.  
  295. #if    KEYPAD
  296.     ttputc(ESC);
  297.     ttputc('=');
  298. #endif
  299. }
  300.  
  301. PASCAL NEAR ansiclose()
  302.  
  303. {
  304. #if    COLOR
  305.     ansifcol(7);
  306.     ansibcol(0);
  307. #endif
  308. #if    MOUSE & ( V7 | USG | HPUX | BSD | SUN | XENIX | VMS)
  309.     {
  310.         char *s;
  311.  
  312.         s = getenv( "MICROEMACS$MOUSE_DISABLE");
  313.  
  314.         if( !s)        /* Regular DEC workstation */
  315.             s = "\033[0'{\033[0;0'z";
  316.         ttputs( s);
  317.     }
  318. #endif
  319. #if    KEYPAD
  320. #if     VMS
  321.     if ((orgchar.tt2 & TT2$M_APP_KEYPAD)==0)
  322. #endif
  323.     {
  324.         ttputc(ESC);
  325.         ttputc('>');
  326.     }
  327. #endif
  328.     ttclose();
  329. }
  330.  
  331. PASCAL NEAR ansikopen()    /* open the keyboard (a noop here) */
  332.  
  333. {
  334. }
  335.  
  336. PASCAL NEAR ansikclose()    /* close the keyboard (a noop here) */
  337.  
  338. {
  339. }
  340.  
  341. #if     V7 | USG | HPUX | BSD | SUN | XENIX | VMS
  342. /***
  343.  *  ttputs  -  Send a string to ttputc
  344.  *
  345.  *  Nothing returned
  346.  ***/
  347. void PASCAL NEAR ttputs(string)
  348. char * string;                /* String to write        */
  349. {
  350.     if (string)
  351.         while (*string != '\0')
  352.             ttputc(*string++);
  353. }
  354.  
  355. /*
  356.     On the "real" ansi terminals, used on "mainframe" type
  357.     terminal/CPU connections of the above operating systems, we do
  358.     conversion from VT100/VT200 style function keys into the Emacs
  359.     standard key sequence form.
  360. */
  361. static unsigned char inbuffer[ 10];
  362. static int inpos=0;
  363. static int inlen=0;
  364.  
  365. NOSHARE int mouserow, mousecol;
  366.  
  367. int PASCAL NEAR readparam( v)    /* Read an ansi parameter */
  368. int *v;    /* Place to put parameter value */
  369. {
  370.     int ch;
  371.  
  372.     *v = 0;
  373.     for(;;)
  374.     { /* Read in a number */
  375.        ch = ttgetc();
  376.     if( ch>='0' && ch<='9') *v = 10 * *v + (ch - '0');
  377.     else return( ch);
  378.     }
  379. }
  380.  
  381. /*
  382.  * Handle the CSI (<esc>[) and SS3 (<esc>O) sequences.
  383.  * Static arrays are set up to translate the ANSI escape sequence
  384.  * into the MicroEMACS keycode.
  385.  *
  386.  * The 'x' in the arrays keypad[] and dec_fnkey[] are merely placeholders.
  387.  */
  388. void PASCAL NEAR docsi( oh)
  389. int oh;
  390. {
  391.     static char crsr[4] = {'P', 'N', 'F', 'B'};
  392.     static char keypad[14] = {',', '-', '.', 'x', '0', '1', '2', '3',
  393.                   '4', '5', '6', '7', '8', '9'};
  394.  
  395.     static char dec_fnkey[32] = {'x', 'S', 'C', 'D', '@', 'Z', 'V', 'x',
  396.                      'x', '1', '2', '3', '4', '5', '6', '7',
  397.                  '8', '9', '0', 'x', '1', '2', '3', '4',
  398.                  'x', '5', '6', 'x', '7', '8', '9', '0',
  399.                 };
  400.  
  401.     unsigned int params[ 5];
  402.     int i;
  403.     unsigned int ch;
  404.  
  405.     params[ 0] = params[ 1] = params[ 2] = params[ 3] = params[ 4] = 0;
  406.     for( i=0;;)
  407.     {
  408.     ch = readparam( ¶ms[ i]);
  409.     if( ch >= '@')
  410.     { /* This ends the sequence, check for the ones we care about */
  411.         mousecol = params[ 0];
  412.         mouserow = params[ 1];
  413.         if( ch == 'R' && oh != 'O')
  414.         {    /* Cursor pos report */
  415.         inbuffer[ inlen++] = 0x0;
  416.         inbuffer[ inlen++] = MOUS>>8;
  417.         inbuffer[ inlen++] = mouserow;
  418.         inbuffer[ inlen++] = mousecol;
  419.         inbuffer[ inlen++] = '1';
  420.         }
  421.         else if( ch == '~')
  422.         {/* LK201 function key */
  423.         inbuffer[ inlen++] = 0x0;
  424.         if (params[0] > 8) params[0] -= 3;
  425.         if (params[0] > 18)
  426.             inbuffer[ inlen++] = (SHFT|SPEC)>>8;
  427.         else
  428.             inbuffer[ inlen++] = SPEC>>8;
  429.         inbuffer[ inlen++] = dec_fnkey[ params[ 0]];
  430.         }
  431.         else if( ch == 'w' && oh != 'O')
  432.         { /* mouse report */
  433.         mousecol = params[ 3]-1;
  434.         mouserow = params[ 2]-1;
  435.         inbuffer[ inlen++] = 0x0;
  436.         inbuffer[ inlen++] = MOUS>>8;
  437.         inbuffer[ inlen++] = mousecol;
  438.         inbuffer[ inlen++] = mouserow;
  439.         inbuffer[ inlen++] = ('a'-2)+params[ 0];
  440.         }
  441.         else if( ch == 'd' && oh != 'O')
  442.         { /* Pixette mouse report */
  443.         mousecol = params[ 0]-1;
  444.         mouserow = params[ 1]-1;
  445.         inbuffer[ inlen++] = 0x0;
  446.         inbuffer[ inlen++] = MOUS>>8;
  447.         inbuffer[ inlen++] = mousecol;
  448.         inbuffer[ inlen++] = mouserow;
  449.         inbuffer[ inlen++] = ('a'-2)+params[ 2];
  450.         }
  451.         else /* Ordinary keypad or arrow key */
  452.         {
  453.         inbuffer[ inlen++] = 0x0;
  454.         if( ch <= 'D' && ch >= 'A') /* Arrow keys.*/
  455.         {
  456.             inbuffer[ inlen++] = (SPEC)>>8;
  457.             inbuffer[ inlen++] = crsr[ ch - 'A'];
  458.         }
  459.         else if (ch <= 'S' && ch >= 'P')    /* PF keys.*/
  460.         {
  461.             inbuffer[ inlen++] = (SPEC|CTRL)>>8;
  462.             inbuffer[ inlen++] = ch - ('P' - '1');
  463.         }
  464.         else
  465.         {
  466.             inbuffer[ inlen++] = (ALTD)>>8;
  467.             if (ch == 'M')
  468.             inbuffer[ inlen++] = 'E';
  469.             else
  470.             inbuffer[ inlen++] = keypad[ ch - 'l'];
  471.         }
  472.         }
  473.         return;
  474.     }
  475.     if( i<5) i++;
  476.     }
  477. }
  478.  
  479. void PASCAL NEAR dobbnmouse()
  480. {
  481.     int params[ 5];
  482.     int i, ch;
  483.     static char prev = 0;
  484.     int event, flags;
  485.  
  486.     params[ 0] = 0;
  487.     params[ 1] = 0;
  488.     params[ 2] = 0;
  489.     params[ 3] = 0;
  490.     params[ 4] = 0;
  491.     for( i=0;;)
  492.     {
  493.         /* Is the sequence finished?
  494.          * check for the ones we care about.
  495.          */
  496.         if( (ch = readparam( ¶ms[ i])) >= '@')
  497.         {
  498.             mousecol = (params[ 1]+4)/9;
  499.             mouserow = (1015-params[ 2])/16;
  500.             flags = params[ 3] & 7;
  501.             event = flags ^ prev;
  502.             prev = flags;
  503.             flags = ((flags & event) == 0);
  504.             event = flags + (6 - (event & 6));
  505.             if( ch == 'c')
  506.             {    /* Cursor pos report */
  507.                 inbuffer[ inlen++] = 0x0;
  508.                 inbuffer[ inlen++] = MOUS>>8;
  509.                 inbuffer[ inlen++] = mousecol;
  510.                 inbuffer[ inlen++] = mouserow;
  511.                 inbuffer[ inlen++] = ('a'-2)+event;
  512.             }
  513.             return;
  514.         }
  515.     if( i<5) i++;
  516.     }
  517. }
  518.  
  519. /*
  520.  *    Read a keystroke from the terminal.  Interpret escape sequences
  521.  *    that come from function keys, mouse reports, and cursor location
  522.  *    reports, and return them using Emacs's coding of these events.
  523.  */
  524. PASCAL NEAR ansigetc()
  525. {
  526.     int ch;
  527.  
  528.     for(;;)
  529.     {/* Until we get a character to return */
  530.     if( inpos < inlen)
  531.     { /* Working out a multi-byte input sequence */
  532.         return( inbuffer[ inpos++]);
  533.     }
  534.     inpos = 0;
  535.     inlen = 0;
  536.     ch = ttgetc();
  537.     if( ch == 27)
  538.     { /* ESC, see if sequence follows */
  539. /*
  540.     If the "terminator" is ESC, and if we are currently reading a
  541.     string where the terminator is ESC, then return the ESC and do
  542.     not allow function keys or mouse to operate properly.  This
  543.     makes VT100 users much happier.
  544. */
  545.         ch = ttgetc_shortwait();
  546.         if( ch < 0) return( 27);    /* Wasn't a function key */
  547.         if( ch == '[') docsi( ch);
  548.         else if( ch == ':') dobbnmouse();
  549.         else if( ch == 'O') docsi( ch);
  550.         else
  551.         { /* This isn't an escape sequence, return it unmodified */
  552.         inbuffer[ inlen++] = ch;
  553.         return( 27);
  554.         }
  555.     }
  556.     else if( ch == 27+128) docsi( ch);
  557.     else return( ch);
  558.     }
  559. }
  560. #else
  561. PASCAL NEAR  ansigetc() {return( ttgetc());}
  562. #endif
  563.  
  564. #if    FLABEL
  565. int PASCAL NEAR fnclabel(f, n)        /* label a function key */
  566.  
  567. int f,n;    /* default flag, numeric argument [unused] */
  568.  
  569. {
  570.     /* on machines with no function keys...don't bother */
  571.     return(TRUE);
  572. }
  573. #endif
  574. #else
  575. ansihello()
  576. {
  577. }
  578. #endif
  579.