home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d5xx / d534 / term.lha / Term / Source.LZH / ParseCode.c < prev    next >
C/C++ Source or Header  |  1991-07-06  |  7KB  |  316 lines

  1. /* $Revision Header * Header built automatically - do not edit! *************
  2.  *
  3.  *    (C) Copyright 1990 by Olaf 'Olsen' Barthel & MXM
  4.  *
  5.  *    Name .....: ParseCode.c
  6.  *    Created ..: Monday 21-Jan-91 20:12
  7.  *    Revision .: 0
  8.  *
  9.  *    Date            Author          Comment
  10.  *    =========       ========        ====================
  11.  *    21-Jan-91       Olsen           Created this file!
  12.  *
  13.  * $Revision Header ********************************************************/
  14.  
  15. #include "TermGlobal.h"
  16.  
  17.     /* A couple of internally referenced variables. */
  18.  
  19. STATIC BYTE    CharsInBuffer = 0;
  20. STATIC UBYTE    SaveBuffer[30];
  21. STATIC BYTE    ScanStep = 0;
  22.  
  23.     /* External comparison routines. */
  24.  
  25. extern BYTE Num1(WORD Char);
  26. extern BYTE Num2(WORD Char);
  27. extern BYTE Num3(WORD Char);
  28. extern BYTE Num4(WORD Char);
  29.  
  30.     /* This structure describes an ANSI control sequence. */
  31.  
  32. struct ControlCode
  33. {
  34.     UBYTE     FirstChar;
  35.     BYTE    (*Match)(WORD Char);
  36.     UBYTE     LastChar;
  37.  
  38.     BYTE     ExactSize;
  39.     UBYTE *    (*Func)(UBYTE *Buffer);
  40. };
  41.  
  42.     /* At present 55 different control sequences are supported/trapped. */
  43.  
  44. #define NUM_CODES 55
  45.  
  46.     /* This follows the control code information. */
  47.  
  48. struct ControlCode ANSICode[NUM_CODES] =
  49. {
  50.     /* Single Character Sequences. */
  51.  
  52.     'D',    NULL,     0 ,    1,    (APTR)CursorScrollDown,
  53.     'M',    NULL,     0 ,    1,    (APTR)CursorScrollUp,
  54.     'E',    NULL,     0 ,    1,    (APTR)NextLine,
  55.     '7',    NULL,     0 ,    1,    (APTR)SaveCursor,
  56.     '8',    NULL,     0 ,    1,    (APTR)LoadCursor,
  57.     '=',    NULL,     0 ,    1,    (APTR)NumericAppMode,
  58.     '>',    NULL,     0 ,    1,    (APTR)NumericAppMode,
  59.     'N',    NULL,     0 ,    1,    (APTR)Ignore,
  60.     'O',    NULL,     0 ,    1,    (APTR)Ignore,
  61.     'H',    NULL,     0 ,    1,    (APTR)SetTab,
  62.     'Z',    NULL,     0 ,    1,    (APTR)RequestTerminal,
  63.     'c',    NULL,     0 ,    1,    (APTR)Reset,
  64.     '<',    NULL,     0 ,    1,    (APTR)Ignore,
  65.     '~',    NULL,     0 ,    1,    (APTR)Ignore,
  66.     'n',    NULL,     0 ,    1,    (APTR)Ignore,
  67.     '}',    NULL,     0 ,    1,    (APTR)Ignore,
  68.     'o',    NULL,     0 ,    1,    (APTR)Ignore,
  69.     '|',    NULL,     0 ,    1,    (APTR)Ignore,
  70.  
  71.     /* Double Character Sequences. */
  72.  
  73.     '[',    NULL,    's',    2,    (APTR)SaveCursor,
  74.     '[',    NULL,    'u',    2,    (APTR)LoadCursor,
  75.     '(',    NULL,    'A',    2,    (APTR)FontStuff,
  76.     '(',    NULL,    'B',    2,    (APTR)FontStuff,
  77.     '(',    NULL,    '0',    2,    (APTR)FontStuff,
  78.     ')',    NULL,    'A',    2,    (APTR)FontStuff,
  79.     ')',    NULL,    'B',    2,    (APTR)FontStuff,
  80.     ')',    NULL,    '0',    2,    (APTR)FontStuff,
  81.     '#',    NULL,    '3',    2,    (APTR)ScaleFont,
  82.     '#',    NULL,    '4',    2,    (APTR)ScaleFont,
  83.     '#',    NULL,    '5',    2,    (APTR)ScaleFont,
  84.     '#',    NULL,    '6',    2,    (APTR)ScaleFont,
  85.     '#',    NULL,    '8',    2,    (APTR)Ignore,
  86.     ' ',    NULL,    'F',    2,    (APTR)Ignore,
  87.     ' ',    NULL,    'G',    2,    (APTR)Ignore,
  88.  
  89.     /* Multiple Character Sequence. */
  90.  
  91.     '[',    Num3,    'i',    0,    (APTR)Ignore,
  92.     '[',    Num3,    'n',    0,    (APTR)RequestInformation,
  93.     '[',    Num3,    'c',    0,    (APTR)RequestTerminal,
  94.     '[',    Num3,    'h',    0,    (APTR)SetSomething,
  95.     '[',    Num3,    'l',    0,    (APTR)SetSomething,
  96.  
  97.     '[',    Num4,    'h',    0,    (APTR)Ignore,
  98.  
  99.     '[',    Num1,    'A',    0,    (APTR)MoveCursor,
  100.     '[',    Num1,    'B',    0,    (APTR)MoveCursor,
  101.     '[',    Num1,    'C',    0,    (APTR)MoveCursor,
  102.     '[',    Num1,    'D',    0,    (APTR)MoveCursor,
  103.     '[',    Num1,    'K',    0,    (APTR)EraseLine,
  104.     '[',    Num1,    'J',    0,    (APTR)EraseScreen,
  105.     '[',    Num1,    'P',    0,    (APTR)EraseCharacters,
  106.     '[',    Num1,    'L',    0,    (APTR)InsertLine,
  107.     '[',    Num1,    'M',    0,    (APTR)ClearLine,
  108.     '[',    Num1,    'g',    0,    (APTR)SetTabs,
  109.     '[',    Num1,    'q',    0,    (APTR)Ignore,
  110.  
  111.     '[',    Num2,    'H',    0,    (APTR)SetPosition,
  112.     '[',    Num2,    'f',    0,    (APTR)SetPosition,
  113.     '[',    Num2,    'm',    0,    (APTR)SetAttributes,
  114.     '[',    Num2,    'y',    0,    (APTR)Ignore,
  115.     '[',    Num2,    'r',    0,    (APTR)SetRegion,
  116. };
  117.  
  118.     /* DoCancel():
  119.      *
  120.      *    Cancel any currently scanned sequence.
  121.      */
  122.  
  123. VOID
  124. DoCancel()
  125. {
  126.     InSequence = FALSE;
  127.     CharsInBuffer = ScanStep = 0;
  128. }
  129.  
  130.     /* CSIFake():
  131.      *
  132.      *    This routine was added to support 8-bit control
  133.      *    sequences introduced by a CSI character.
  134.      */
  135.  
  136. VOID
  137. CSIFake()
  138. {
  139.         /* Reset scanner */
  140.  
  141.     DoCancel();
  142.  
  143.         /* Perform as if ESC [ had been transmitted. */
  144.  
  145.     InSequence = ParseCode('[');
  146. }
  147.  
  148.     /* ParseCode(UBYTE c):
  149.      *
  150.      *    Input:    A character to be passed through the ANSI code
  151.      *        parser.
  152.      *
  153.      *    Output:    FALSE if input characters did form a valid ANSI
  154.      *        control sequence or if input characters did not
  155.      *        form an ANSI control sequence at all.
  156.      *
  157.      *        TRUE if input characters did possibly introduce
  158.      *        a valid ANSI control sequence.
  159.      */
  160.  
  161. BYTE
  162. ParseCode(UBYTE c)
  163. {
  164.     SHORT i;
  165.  
  166.         /* ScanStep = 0:    This is the first character
  167.          *            to introduce a control sequence.
  168.          */
  169.  
  170.     if(!ScanStep)
  171.     {
  172.             /* Scan all available codes and try to find
  173.              * a match.
  174.              */
  175.  
  176.         for(i = 0 ; i < NUM_CODES ; i++)
  177.         {
  178.                 /* This character may introduce a
  179.                  * control sequence.
  180.                  */
  181.  
  182.             if(ANSICode[i] . FirstChar == c)
  183.             {
  184.                     /* If this is a single
  185.                      * character control sequence
  186.                      * call the approriate function
  187.                      * and exit immediately.
  188.                      */
  189.  
  190.                 if(ANSICode[i] . ExactSize == 1)
  191.                 {
  192.                     SaveBuffer[CharsInBuffer++] = c;
  193.                     SaveBuffer[CharsInBuffer] = 0;
  194.  
  195.                     if(Config . Emulation != EMULATION_ATOMIC || Escape)
  196.                     {
  197.                         UBYTE *SomeString = ANSICode[i] . Func(&SaveBuffer[0]);
  198.  
  199.                         if(SomeString)
  200.                             SerWrite(SomeString,strlen(SomeString));
  201.                     }
  202.  
  203.                     CharsInBuffer = ScanStep = 0;
  204.  
  205.                     return(FALSE);
  206.                 }
  207.  
  208.                     /* The length of this control
  209.                      * sequence is greater than
  210.                      * a single character. Save
  211.                      * the input character and
  212.                      * return.
  213.                      */
  214.  
  215.                 ScanStep = i;
  216.  
  217.                 SaveBuffer[CharsInBuffer++] = c;
  218.  
  219.                 return(TRUE);
  220.             }
  221.         }
  222.  
  223.             /* No control sequence introducing character
  224.              * was found.
  225.              */
  226.  
  227.         /* No Match */
  228.  
  229.         CharsInBuffer = ScanStep = 0;
  230.  
  231.         return(FALSE);
  232.     }
  233.     else
  234.     {
  235.             /* Length of the control sequence
  236.              * overrides the boundary, exit
  237.              * immediately!
  238.              */
  239.  
  240.         if(CharsInBuffer == 21)
  241.         {
  242.             /* No Match */
  243.  
  244.             CharsInBuffer = ScanStep = 0;
  245.  
  246.             return(FALSE);
  247.         }
  248.  
  249.             /* Scan the remaining codes for a match. */
  250.  
  251.         for(i = ScanStep ; i < NUM_CODES ; i++)
  252.         {
  253.                 /* This sequence begins with the
  254.                  * same character the parser was
  255.                  * initialized with, so let's take
  256.                  * a look at it.
  257.                  */
  258.  
  259.             if(ANSICode[i] . FirstChar == SaveBuffer[0])
  260.             {
  261.                     /* This character is supposed to
  262.                      * terminate the sequence. So let's
  263.                      * exit.
  264.                      */
  265.  
  266.                 if(ANSICode[i] . LastChar == c)
  267.                 {
  268.                     SaveBuffer[CharsInBuffer++] = c;
  269.                     SaveBuffer[CharsInBuffer] = 0;
  270.  
  271.                     if(Config . Emulation != EMULATION_ATOMIC || Escape)
  272.                     {
  273.                         UBYTE *SomeString = ANSICode[i] . Func(&SaveBuffer[0]);
  274.  
  275.                         if(SomeString)
  276.                             SerWrite(SomeString,strlen(SomeString));
  277.                     }
  278.  
  279.                     CharsInBuffer = ScanStep = 0;
  280.  
  281.                     return(FALSE);
  282.                 }
  283.                 else
  284.                 {
  285.                     if(ANSICode[i] . Match)
  286.                     {
  287.                             /* This character is part of
  288.                              * a legal sequence. Store it
  289.                              * and return.
  290.                              */
  291.  
  292.                         if(ANSICode[i] . Match(c))
  293.                         {
  294.                             ScanStep = i;
  295.  
  296.                             SaveBuffer[CharsInBuffer++] = c;
  297.  
  298.                             return(TRUE);
  299.                         }
  300.                     }
  301.                 }
  302.             }
  303.         }
  304.  
  305.             /* This character is not part of a valid
  306.              * ANSI control sequence, so exit.
  307.              */
  308.  
  309.         /* No Match */
  310.  
  311.         CharsInBuffer = ScanStep = 0;
  312.  
  313.         return(FALSE);
  314.     }
  315. }
  316.