home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / modem / cvt100.zip / VTTIO.C < prev   
Text File  |  1988-08-03  |  18KB  |  653 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4.  
  5. #define NORMAL        0x7         /* Normal video attribute */
  6. #define BOLD          0x8         /* Bold video attribute */
  7. #define UNDERLINED    0xA         /* Underlined video attribute */
  8. #define REVERSE       0x70        /* Reverse video attribute */
  9.  
  10. /****************************************************************************/
  11. /* Function prototypes                                                      */
  12.  
  13. void VTInit( void );
  14. void ConOut( unsigned char );
  15. static void atnrm( unsigned char );
  16. static void (*ttstate)() = atnrm;
  17. static void atescf( unsigned char );
  18. static void AnsiParse( unsigned char );
  19. static void ExtParse( unsigned char );
  20. static void AnsiModeSet( char, int );
  21. static void ExtModeSet( char, int );
  22. static void SetChar0( unsigned char );
  23. static void SetChar1( unsigned char );
  24. static void SetDouble( unsigned char );
  25. static void TransmitId( void );
  26. static void VTBell( void );
  27.  
  28. /****************************************************************************/
  29. /* Global variables                                                         */
  30.  
  31. unsigned originmode;              /* Origin mode, relative or absolute */
  32. unsigned insertmode;              /* Insert mode, off or on */
  33. unsigned autowrap;                /* Automatic wrap mode, off or on */
  34. unsigned newline;                 /* Newline mode, off or on,  GLOBAL data!*/
  35. unsigned cursorvisible;           /* Cursor visibility, on or hidden */
  36. unsigned reversebackground;       /* Reverse background attribute, on or off*/
  37. unsigned screenwid;               /* Screen column width */
  38. unsigned char log;                /* Flag to indicate char logging is on */
  39.  
  40. /****************************************************************************/
  41. /* External variables                                                       */
  42.  
  43.  
  44.  
  45. /****************************************************************************/
  46. /* Local static data                                                        */
  47.  
  48. char term_id_str[]= "[?1;2c";     /* VT100 id string */
  49. #define lansarg 10                /* Max number of ANSI arguments */
  50. static int nansarg = 0;           /* Index for ANSI argument list */
  51. static int ansargs[lansarg];      /* Room for 10 ANSI arguments */
  52. static unsigned char lastc;       /* Saved last character */
  53.  
  54.  
  55. /*****************************************************************************/
  56. /*****************************************************************************/
  57.  
  58. /*  V T I N I T  --   */
  59.  
  60. void VTinit() {
  61.  
  62.     if (GetVTSetup() == 0) {      /* If there are no saved values for */
  63.         screenwid = 80;           /* VT emulation parameters then provid'em */
  64.         newline = 0;
  65.         autowrap = 0;
  66.         insertmode = 0;
  67.         cursorvisible = 1;
  68.         reversebackground = 0;
  69.         log = 0;
  70.     }
  71.  
  72.  
  73.     Setvattr( NORMAL );
  74.     ttstate = atnrm;              /* initial output state is normal */
  75.     SetScroll(0,0);
  76.     ClearScreen();
  77.     SetCharSet(0, 'B');
  78.     SetCharSet(1, 'B');
  79.     MapCharSet(0);
  80.     ClearAllTabs();
  81.     InitTabs();
  82.     SetScreenWidth(screenwid);
  83.     SetCursorVisibility(cursorvisible);
  84.     SetBackGround(reversebackground);
  85.     SetCurs(1,1);
  86.     SaveCursor();
  87.     lastc = '\0';
  88. }
  89.  
  90.  
  91.  
  92. /*  C O N O U T  --  Put a character to the terminal screen */
  93.  
  94. void ConOut(unsigned char c) {
  95.  
  96.    (*ttstate) (c);
  97.    if (log)
  98.        writelog(c);
  99.  
  100.    lastc = c;
  101.  
  102. }
  103.  
  104. /*  A T N R M  --  local routine to process an arbitrary character */
  105.  
  106. static void atnrm(c) unsigned char c; {
  107.  
  108.    if ( c >= 32 && c < 0x80 )
  109.       chrwrite(c);
  110.  
  111.    else switch (c) {
  112.  
  113.       case 27:                    /* Escape */
  114.           ttstate = atescf;       /* next state parser is esc follower */
  115.           break;
  116.  
  117.       case '\r':                  /* Carriage return */
  118.           SetCurs(1,0);
  119.           break;
  120.  
  121.       case '\n':                  /* Line feed */
  122.           if (newline)
  123.               SetCurs(1,0);
  124.  
  125.       case 11:                    /* Vertical tab */
  126.       case 12:                    /* Form feed */
  127.           ScrollUp();
  128.           break;
  129.  
  130.       case '\a':                  /* Ring terminal bell */
  131.           VTBell();
  132.           break;
  133.  
  134.       case 8:                     /* back space */
  135.           SetRelCurs( -1, 0 );
  136.           break;
  137.  
  138.       case '\t':                  /* Horizontal tab */
  139.           DoTab();
  140.           break;
  141.  
  142.  
  143.       case 14:                    /* Map G1 to current */
  144.           MapCharSet(1);
  145.           break;
  146.  
  147.       case 15:                    /* Map G0 to current */
  148.           MapCharSet(0);
  149.           break;
  150.  
  151.       case 5:                     /* transmit answer back */
  152.           break;
  153.  
  154.       default:
  155.  
  156.       }
  157. }
  158.  
  159.  
  160.  
  161.  
  162. /*  A T E S C F  --  escape sequence follower */
  163.  
  164. static void atescf(c) unsigned char c; {
  165.    register char *termid = term_id_str;
  166.  
  167.    switch (c) {
  168.  
  169.         case  '[':                /* Parse ansi args */
  170.            memset(ansargs,0,sizeof(ansargs));
  171.            nansarg = 0;
  172.            ttstate = AnsiParse;
  173.            break;
  174.  
  175.         case  'D':                /* Index */
  176.            ScrollUp();
  177.            ttstate = atnrm;
  178.            break;
  179.  
  180.         case  'E':                /* Carriage return/line feed combination */
  181.            SetCurs(1,0);
  182.            ScrollUp();
  183.            ttstate = atnrm;
  184.            break;
  185.  
  186.         case  'M':                /* Reverse Index */
  187.            ScrollDown();
  188.            ttstate = atnrm;
  189.            break;
  190.  
  191.         case  'H':                /* Set a tab stop */
  192.            SetTabStop();
  193.            ttstate = atnrm;
  194.            break;
  195.  
  196.         case  '7':                /* Save cursor description */
  197.            SaveCursor();
  198.            ttstate = atnrm;
  199.            break;
  200.  
  201.         case  '8':                /* Restore cursor description */
  202.            RestoreCursor();
  203.            ttstate = atnrm;
  204.            break;
  205.  
  206.         case  '=':                /* Enable application keypad */
  207.            SetKeyPad(1);
  208.            ttstate = atnrm;
  209.            break;
  210.  
  211.         case  '>':                /* Enable numeric keypad */
  212.            SetKeyPad(0);
  213.            ttstate = atnrm;
  214.            break;
  215.  
  216.         case  'c':                /* Reset terminal to power on values */
  217.            VTInit();
  218.            ttstate = atnrm;
  219.            break;
  220.  
  221.         case  '(':                /* Select character set G0 */
  222.            ttstate = SetChar0;
  223.            break;
  224.  
  225.         case  ')':                /* Select character set G1 */
  226.            ttstate = SetChar1;
  227.            break;
  228.  
  229.         case  '#':                /* Set double high/wide characters */
  230.            ttstate = SetDouble;
  231.            break;
  232.  
  233.         case 24:
  234.         case 26:                  /* Cancel escape sequence */
  235.            ttstate = atnrm;
  236.            break;
  237.  
  238.  
  239.         case  'Z':                /* Transmit the terminal ID */
  240.            TransmitId();
  241.            ttstate = atnrm;
  242.            break;
  243.  
  244.         case  '\\':
  245.            ttstate = atnrm;
  246.            break;
  247.  
  248.         case  '<':
  249.            ttstate = atnrm;
  250.            break;
  251.  
  252.         case  'P':
  253.            ttstate = atnrm;
  254.            break;
  255.  
  256.         case  '*':
  257.            ttstate = atnrm;
  258.            break;
  259.  
  260.         default:                  /* unrecognized so ignore */
  261.            ttstate = atnrm;
  262.            break;
  263.         }
  264.  
  265. }
  266.  
  267.  
  268. /*  A N S I P A R S E  --  parse ansi arguments */
  269.  
  270. static void AnsiParse(c) unsigned char c; {
  271.    register int i;
  272.    register int j;
  273.  
  274.    c &= 0x7F;
  275.  
  276.    switch(c) {
  277.  
  278.         case '0':
  279.         case '1':
  280.         case '2':
  281.         case '3':
  282.         case '4':
  283.         case '5':
  284.         case '6':
  285.         case '7':
  286.         case '8':
  287.         case '9':
  288.            ansargs[nansarg] = (ansargs[nansarg] * 10) + ( c - '0');
  289.            break;
  290.  
  291.         case ';':                 /* Argument separator */
  292.            if ( ++nansarg > lansarg)
  293.               ttstate = atnrm;
  294.            break;
  295.  
  296.         case 'h':                 /* Set ANSI mode */
  297.            for( i = 0, ++nansarg; i < nansarg && i <= lansarg; i++)
  298.               AnsiModeSet(ansargs[i],1);
  299.            ttstate = atnrm;
  300.            break;
  301.  
  302.         case 'l':                 /* Reset ANSI mode */
  303.            for( i = 0, ++nansarg; i < nansarg && i <= lansarg; i++)
  304.               AnsiModeSet(ansargs[i],0);
  305.            ttstate = atnrm;
  306.            break;
  307.  
  308.  
  309.         case 'H':                 /* Address cursor to line and column */
  310.         case 'f':
  311.            i = ansargs[0];
  312.            j = ansargs[1];
  313.            if ( i == 0)
  314.               i = 1;
  315.            if ( j == 0)
  316.               j = 1;
  317.            SetCurs(j,i);
  318.  
  319.            ttstate = atnrm;
  320.            break;
  321.  
  322.         case 'J':                 /* Erase screen */
  323.            if (ansargs[0] == 0){      /* from cursur to end of the screen */
  324.                ClearEOS();
  325.                }
  326.            else if (ansargs[0] == 1){ /* from home position to cursur */
  327.                ClearBOS();
  328.                }
  329.            else if (ansargs[0] == 2){ /* whole screen */
  330.                ClearScreen();
  331.                }
  332.            ttstate = atnrm;
  333.            break;
  334.  
  335.  
  336.         case 'K':                 /* Erase Line */
  337.            if (ansargs[0] == 0)       /* from cursur to end of the line */
  338.                ClearEOL();
  339.            else if (ansargs[0] == 1)  /* start of line to cursur */
  340.                ClearBOL();
  341.            else if (ansargs[0] == 2){ /* whole line */
  342.                ClearBOL();
  343.                ClearEOL();
  344.                }
  345.            ttstate = atnrm;
  346.            break;
  347.  
  348.  
  349.         case 'm':                 /* Select screen attribute */
  350.            ttstate = atnrm;
  351.            if ( ++nansarg <= lansarg) {
  352.               for ( i = 0; i < nansarg; i++) {
  353.                  switch (ansargs[i]) {
  354.                     case 0:
  355.                     case 22:
  356.                        if (i == 0)
  357.                           SetVattr( NORMAL );
  358.                        else
  359.                           AddVattr(NORMAL);
  360.                        break;
  361.                     case 1:
  362.                        if (i == 0)
  363.                           SetVattr( BOLD);
  364.                        else
  365.                           AddVattr(BOLD);
  366.                        break;
  367.                     case 2:
  368.                        SubVattr( BOLD );
  369.                        break;
  370.                     case 4:
  371.                        if (i == 0)
  372.                           SetVattr( UNDERLINED );
  373.                        else
  374.                           AddVattr( UNDERLINED );
  375.                        break;
  376.                     case 5:
  377.                        if (i == 0)
  378.                           SetVattr( BLINK );
  379.                        else
  380.                           AddVattr( BLINK );
  381.                        break;
  382.                     case 7:
  383.                        if (i == 0)
  384.                           SetVattr( REVERSE );
  385.                        else
  386.                           AddVattr( REVERSE );
  387.                        break;
  388.                     case 24:
  389.                        SubVattr( UNDERLINED );
  390.                        break;
  391.                     case 25:
  392.                        SubVattr( BLINK );
  393.                        break;
  394.                     case 27:
  395.                        SubVattr( REVERSE );
  396.                        break;
  397.                     default:
  398.                        break;
  399.                  }
  400.               }
  401.            }
  402.            break;
  403.  
  404.        case '?':                  /* Extended mode set/reset */
  405.            if (lastc == '[')
  406.               ttstate = ExtParse;
  407.            else
  408.               ttstate = atnrm;
  409.            break;
  410.  
  411.         case 'r':                 /* Define scrolling region */
  412.            SetScroll(ansargs[0],ansargs[1]);
  413.            ttstate = atnrm;
  414.            break;
  415.  
  416.         case 'A':                 /* Move cursor up */
  417.            if (ansargs[0] == 0)
  418.               SetRelCurs(0,-1);
  419.            else
  420.               SetRelCurs(0,-ansargs[0]);
  421.  
  422.            ttstate = atnrm;
  423.            break;
  424.  
  425.         case 'B':                 /* Move cursor down */
  426.            if (ansargs[0] == 0)
  427.               SetRelCurs(0,1);
  428.            else
  429.               SetRelCurs(0,ansargs[0]);
  430.            ttstate = atnrm;
  431.            break;
  432.  
  433.         case 'C':                 /* Move cursor right */
  434.            if (ansargs[0] == 0)
  435.               SetRelCurs(1,0);
  436.            else
  437.               SetRelCurs(ansargs[0],0);
  438.            ttstate = atnrm;
  439.            break;
  440.  
  441.         case 'D':                 /* Move cursor left */
  442.            if (ansargs[0] == 0)
  443.               SetRelCurs(-1,0);
  444.            else
  445.               SetRelCurs(-ansargs[0],0);
  446.            ttstate = atnrm;
  447.            break;
  448.  
  449.         case 'g':                 /* Tab stop set/reset */
  450.            if (ansargs[0] == 0)
  451.                ClearTabStop();
  452.            else if (ansargs[0] == 3)
  453.                ClearAllTabs();
  454.            ttstate = atnrm;
  455.            break;
  456.  
  457.  
  458.         case 'c':                 /* Transmit the terminal ID */
  459.            TransmitId();
  460.            break;
  461.  
  462.         case 24:
  463.         case 26:                  /* Cancel escape sequence */
  464.            ttstate = atnrm;
  465.            break;
  466.  
  467.  
  468.         default:                  /* unrecognized so ignore */
  469.            ttstate = atnrm;
  470.            break;
  471.    }
  472. }
  473.  
  474.  
  475. /* E X T P A R S E  -- Parse extended mode Set/Reset */
  476.  
  477. static void ExtParse(unsigned char c) {
  478.     register int i;
  479.  
  480.     switch (c) {
  481.  
  482.         case '0':
  483.         case '1':
  484.         case '2':
  485.         case '3':
  486.         case '4':
  487.         case '5':
  488.         case '6':
  489.         case '7':
  490.         case '8':
  491.         case '9':
  492.            ansargs[nansarg] = (ansargs[nansarg] * 10) + ( c - '0');
  493.            break;
  494.  
  495.         case ';':                 /* Argument separator */
  496.            if ( ++nansarg > lansarg)
  497.               ttstate = atnrm;
  498.            break;
  499.  
  500.         case 'h':
  501.            for( i = 0, ++nansarg; i < nansarg && i <= lansarg; i++)
  502.               ExtModeSet(ansargs[i],1);
  503.            ttstate = atnrm;
  504.            break;
  505.  
  506.         case 'l':
  507.            for( i = 0, ++nansarg; i < nansarg && i <= lansarg; i++)
  508.               ExtModeSet(ansargs[i],0);
  509.            ttstate = atnrm;
  510.            break;
  511.  
  512.         case 24:
  513.         case 26:                  /* Cancel escape sequence */
  514.            ttstate = atnrm;
  515.            break;
  516.  
  517.         default:
  518.            ttstate = atnrm;
  519.            break;
  520.     }
  521. }
  522.  
  523.  
  524. /* A N S I M O D E S E T  -- Set/Reset ANSI mode   ,  ESC [ P1,,, Pn h/l */
  525.  
  526. static void AnsiModeSet( char c, register int mode) {
  527.  
  528.     switch (c ) {
  529.         case 2:                   /* Lock/unlock keyboard */
  530.             break;
  531.  
  532.  
  533.         case 4:                   /* Insert/Replace setting */
  534.             insertmode = mode;
  535.             break;
  536.  
  537.         case 12:                  /* Echo */
  538.             break;
  539.  
  540.         case 20:                  /* New Line mode */
  541.             newline = mode;
  542.             break;
  543.  
  544.         default:
  545.            break;
  546.     }
  547. }
  548.  
  549.  
  550. /* E X T M O D E S E T  --  Set/Reset extended mode after ESC [ ? */
  551.  
  552. static void ExtModeSet(char c, int mode) {
  553.  
  554.     switch (c) {
  555.         case 1:
  556.             SetCursorKey(mode);   /* set cursor key mode */
  557.             break;
  558.  
  559.         case 3:                   /* Set the screen width */
  560.             if (mode)
  561.                SetScreenWidth(132);
  562.             else
  563.                SetScreenWidth(80);
  564.             break;
  565.  
  566.         case 4:                   /* Jump/smooth scroll (not supported) */
  567.             break;
  568.  
  569.         case 5:                   /* Set the background attribute */
  570.             SetBackGround(mode);
  571.             break;
  572.  
  573.  
  574.         case 6:                   /* Set the scrolling origin */
  575.             originmode = mode;
  576.             break;
  577.  
  578.         case 7:                   /* set the autowrap mode */
  579.             autowrap = mode;
  580.             break;
  581.  
  582.         case 8:                   /* Auto repeat on/off */
  583.             break;
  584.  
  585.         case 18:                  /* Page print terminator */
  586.             break;
  587.  
  588.         case 19:                  /* Print region */
  589.             break;
  590.  
  591.         case 25:                  /* Display/Hide cursor */
  592.             SetCursorVisibility(mode);
  593.             break;
  594.  
  595.         default:
  596.             break;
  597.     }
  598. }
  599.  
  600.  
  601.  
  602. /* A T C H R S E T 0 -- Set the current character set for G0 */
  603.  
  604. void SetChar0( unsigned char c) {
  605.   SetCharSet( 0, c );
  606.   ttstate = atnrm;
  607. }
  608.  
  609. /* A T C H R S E T 1 -- Set the current character set for G1 */
  610.  
  611. void SetChar1( unsigned char c) {
  612.   SetCharSet( 1, c );
  613.   ttstate = atnrm;
  614. }
  615.  
  616.  
  617. /* S E T D O U B L E -- Set the current line to double high and/or wide */
  618.  
  619. void SetDouble( unsigned char c) {
  620.  
  621.   switch (c) {
  622.      case '5':                    /* Single width */
  623.      case '6':                    /* Double width */
  624.      case '3':                    /* Double height/width */
  625.      case '4':                    /* Bottom half of double height/width */
  626.      default:
  627.   }
  628.   ttstate = atnrm;
  629.  
  630. }
  631.  
  632. /* T R A N S M I T I D -- Transmit the terminal id to the host */
  633.  
  634. static void TransmitId() {
  635.    register char *termid = term_id_str;
  636.  
  637.    ttoc(27);
  638.    while(*termid != '\0')         /* Do until end of id string */
  639.       ttoc(*termid++);               /* Transmit each character of string */
  640. }
  641.  
  642.  
  643. /*  V T B E L L  --  Do a VT100 style bell */
  644.  
  645. static void VTBell() {
  646.  
  647.    sound(880);
  648.    delay(125);
  649.    nosound();
  650.  
  651. }
  652.  
  653.