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