home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d8xx / d832 / term.lha / Term / term-3.1-Source.lha / termEmulation.c < prev    next >
C/C++ Source or Header  |  1993-02-18  |  43KB  |  2,477 lines

  1. /*
  2. **    termEmulation.c
  3. **
  4. **    Terminal emulation (parsing and processing) routines
  5. **
  6. **    Copyright © 1990-1993 by Olaf `Olsen' Barthel & MXM
  7. **        All Rights Reserved
  8. */
  9.  
  10. #include "termGlobal.h"
  11.  
  12.     /* One of the emulation callback routines. */
  13.  
  14. typedef VOID (* __regargs EPTR)(STRPTR Buffer);
  15.  
  16.     /* This structure describes an ANSI control sequence. */
  17.  
  18. struct ControlCode
  19. {
  20.     UBYTE    FirstChar;
  21.     STRPTR    Match;
  22.     UBYTE    LastChar;
  23.  
  24.     BYTE    ExactSize;
  25.     EPTR    Func;
  26. };
  27.  
  28.     /* How many characters we will keep in the scan buffer. */
  29.  
  30. #define MAX_SCAN_SIZE    256
  31.  
  32.     /* Flag indicating whether the cursor has already been
  33.      * erased or not.
  34.      */
  35.  
  36. STATIC BYTE CursorEnabled = FALSE;
  37.  
  38.     /* Global string buffer and backup style. */
  39.  
  40. STATIC UBYTE GlobalBuffer[40],StyleType = FS_NORMAL;
  41.  
  42.     /* Cursor backup data. */
  43.  
  44. STATIC struct CursorData CursorBackup;
  45.  
  46.     /* A couple of internally referenced variables. */
  47.  
  48. STATIC BYTE    CharsInBuffer    = 0,
  49.         ScanStep    = 0;
  50. STATIC UBYTE    SaveBuffer[MAX_SCAN_SIZE + 1];
  51.  
  52.     /* Character access tables. */
  53.  
  54. STATIC UBYTE Table0[256] =
  55. {
  56.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  57.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  58.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  59.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  60.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  61.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  62.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  63.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  64.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  65.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  66.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  67.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  68.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  69.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  70.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  71.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  72. };
  73.  
  74. STATIC UBYTE Table1[256] =
  75. {
  76.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  77.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  78.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  79.     1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,
  80.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  81.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  82.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  83.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  84.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  85.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  86.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  87.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  88.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  89.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  90.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  91.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  92. };
  93.  
  94. STATIC UBYTE Table2[256] =
  95. {
  96.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  97.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  98.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  99.     1,1,1,1,1,1,1,1,1,1,0,1,0,0,0,0,
  100.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  101.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  102.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  103.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  104.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  105.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  106.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  107.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  108.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  109.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  110.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  111.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  112. };
  113.  
  114. STATIC UBYTE Table3[256] =
  115. {
  116.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  117.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  118.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  119.     1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,1,
  120.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  121.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  122.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  123.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  124.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  125.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  126.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  127.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  128.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  129.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  130.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  131.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  132. };
  133.  
  134. STATIC UBYTE Table4[256] =
  135. {
  136.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  137.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  138.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  139.     1,1,1,1,1,1,1,1,1,1,0,0,0,1,0,0,
  140.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  141.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  142.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  143.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  144.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  145.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  146.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  147.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  148.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  149.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  150.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  151.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  152. };
  153.  
  154.     /* This follows the control code information. */
  155.  
  156. struct ControlCode ANSICode[] =
  157. {
  158.     /* Single Character Sequences. */
  159.  
  160.     'D',    Table0,     0 ,    1,    (EPTR)CursorScrollDown,
  161.     'M',    Table0,     0 ,    1,    (EPTR)CursorScrollUp,
  162.     'E',    Table0,     0 ,    1,    (EPTR)NextLine,
  163.     '7',    Table0,     0 ,    1,    (EPTR)SaveCursor,
  164.     '8',    Table0,     0 ,    1,    (EPTR)LoadCursor,
  165.     '=',    Table0,     0 ,    1,    (EPTR)NumericAppMode,
  166.     '>',    Table0,     0 ,    1,    (EPTR)NumericAppMode,
  167.     'N',    Table0,     0 ,    1,    (EPTR)Ignore,
  168.     'O',    Table0,     0 ,    1,    (EPTR)Ignore,
  169.     'H',    Table0,     0 ,    1,    (EPTR)SetTab,
  170.     'Z',    Table0,     0 ,    1,    (EPTR)RequestTerminal,
  171.     'c',    Table0,     0 ,    1,    (EPTR)Reset,
  172.     '<',    Table0,     0 ,    1,    (EPTR)Ignore,
  173.     '~',    Table0,     0 ,    1,    (EPTR)Ignore,
  174.     'n',    Table0,     0 ,    1,    (EPTR)Ignore,
  175.     '}',    Table0,     0 ,    1,    (EPTR)Ignore,
  176.     'o',    Table0,     0 ,    1,    (EPTR)Ignore,
  177.     '|',    Table0,     0 ,    1,    (EPTR)Ignore,
  178.  
  179.     /* Double Character Sequences. */
  180.  
  181.     '[',    Table0,    's',    2,    (EPTR)SaveCursor,
  182.     '[',    Table0,    'u',    2,    (EPTR)LoadCursor,
  183.     '(',    Table0,    'A',    2,    (EPTR)FontStuff,
  184.     '(',    Table0,    'B',    2,    (EPTR)FontStuff,
  185.     '(',    Table0,    '0',    2,    (EPTR)FontStuff,
  186.     ')',    Table0,    'A',    2,    (EPTR)FontStuff,
  187.     ')',    Table0,    'B',    2,    (EPTR)FontStuff,
  188.     ')',    Table0,    '0',    2,    (EPTR)FontStuff,
  189.     '#',    Table0,    '3',    2,    (EPTR)ScaleFont,
  190.     '#',    Table0,    '4',    2,    (EPTR)ScaleFont,
  191.     '#',    Table0,    '5',    2,    (EPTR)ScaleFont,
  192.     '#',    Table0,    '6',    2,    (EPTR)ScaleFont,
  193.     '#',    Table0,    '8',    2,    (EPTR)AlignmentTest,
  194.     ' ',    Table0,    'F',    2,    (EPTR)Ignore,
  195.     ' ',    Table0,    'G',    2,    (EPTR)Ignore,
  196.  
  197.     /* Multiple Character Sequence. */
  198.  
  199.     '[',    Table3,    'i',    0,    (EPTR)PrinterController,
  200.  
  201.     '[',    Table3,    'n',    0,    (EPTR)RequestInformation,
  202.     '[',    Table3,    'c',    0,    (EPTR)RequestTerminal,
  203.     '[',    Table3,    'h',    0,    (EPTR)SetSomething,
  204.     '[',    Table3,    'l',    0,    (EPTR)SetSomething,
  205.  
  206.     '[',    Table4,    'h',    0,    (EPTR)Ignore,
  207.  
  208.     '[',    Table1,    'A',    0,    (EPTR)MoveCursor,
  209.     '[',    Table1,    'B',    0,    (EPTR)MoveCursor,
  210.     '[',    Table1,    'C',    0,    (EPTR)MoveCursor,
  211.     '[',    Table1,    'D',    0,    (EPTR)MoveCursor,
  212.     '[',    Table1,    'K',    0,    (EPTR)EraseLine,
  213.     '[',    Table1,    'J',    0,    (EPTR)EraseScreen,
  214.     '[',    Table1,    'P',    0,    (EPTR)EraseCharacters,
  215.     '[',    Table1,    '@',    0,    (EPTR)InsertCharacters,
  216.     '[',    Table1,    'L',    0,    (EPTR)InsertLine,
  217.     '[',    Table1,    'M',    0,    (EPTR)ClearLine,
  218.     '[',    Table1,    'g',    0,    (EPTR)SetTabs,
  219.     '[',    Table1,    'q',    0,    (EPTR)Ignore,
  220.  
  221.     '[',    Table2,    'H',    0,    (EPTR)SetAbsolutePosition,
  222.     '[',    Table2,    'f',    0,    (EPTR)SetAbsolutePosition,
  223.     '[',    Table2,    'm',    0,    (EPTR)SetAttributes,
  224.     '[',    Table2,    'y',    0,    (EPTR)Ignore,
  225.     '[',    Table2,    'r',    0,    (EPTR)SetRegion,
  226.  
  227.     '[',    Table1,    'S',    0,    (EPTR)ScrollUp,
  228.     '[',    Table1,    'T',    0,    (EPTR)ScrollDown
  229. };
  230.  
  231. STATIC WORD NumCodes = sizeof(ANSICode) / sizeof(struct ControlCode);
  232.  
  233.     /* DoCancel():
  234.      *
  235.      *    Cancel any currently scanned sequence.
  236.      */
  237.  
  238. BYTE __regargs
  239. DoCancel()
  240. {
  241.     InSequence    = FALSE;
  242.     CharsInBuffer    = ScanStep = 0;
  243.  
  244.     return(FALSE);
  245. }
  246.  
  247.     /* CSIFake():
  248.      *
  249.      *    This routine was added to support 8-bit control
  250.      *    sequences introduced by a CSI character.
  251.      */
  252.  
  253. VOID
  254. CSIFake()
  255. {
  256.         /* Reset scanner */
  257.  
  258.     DoCancel();
  259.  
  260.         /* Perform as if ESC [ had been transmitted. */
  261.  
  262.     InSequence = ParseCode('[');
  263. }
  264.  
  265.     /* ParseCode(UBYTE c):
  266.      *
  267.      *    Input:    A character to be passed through the ANSI code
  268.      *        parser.
  269.      *
  270.      *    Output:    FALSE if input characters did form a valid ANSI
  271.      *        control sequence or if input characters did not
  272.      *        form an ANSI control sequence at all.
  273.      *
  274.      *        TRUE if input characters did possibly introduce
  275.      *        a valid ANSI control sequence.
  276.      */
  277.  
  278. BYTE __regargs
  279. ParseCode(UBYTE c)
  280. {
  281.         /* ScanStep = 0:    This is the first character
  282.          *            to introduce a control sequence.
  283.          */
  284.  
  285.     if(!ScanStep)
  286.     {
  287.         register WORD i;
  288.  
  289.             /* Scan all available codes and try to find
  290.              * a match.
  291.              */
  292.  
  293.         for(i = 0 ; i < NumCodes ; i++)
  294.         {
  295.                 /* This character may introduce a
  296.                  * control sequence.
  297.                  */
  298.  
  299.             if(ANSICode[i] . FirstChar == c)
  300.             {
  301.                     /* If this is a single
  302.                      * character control sequence
  303.                      * call the approriate function
  304.                      * and exit immediately.
  305.                      */
  306.  
  307.                 if(ANSICode[i] . ExactSize == 1)
  308.                 {
  309.                     if(Config -> TerminalConfig -> EmulationMode != EMULATION_ATOMIC)
  310.                     {
  311.                         SaveBuffer[CharsInBuffer++] = c;
  312.                         SaveBuffer[CharsInBuffer  ] = 0;
  313.  
  314.                         (*ANSICode[i] . Func)(SaveBuffer);
  315.                     }
  316.  
  317.                     CharsInBuffer = ScanStep = 0;
  318.  
  319.                     return(FALSE);
  320.                 }
  321.                 else
  322.                 {
  323.                         /* The length of this control
  324.                          * sequence is greater than
  325.                          * a single character. Save
  326.                          * the input character and
  327.                          * return.
  328.                          */
  329.  
  330.                     ScanStep = i;
  331.  
  332.                     SaveBuffer[CharsInBuffer++] = c;
  333.  
  334.                     return(TRUE);
  335.                 }
  336.             }
  337.         }
  338.     }
  339.     else
  340.     {
  341.         if(CharsInBuffer < MAX_SCAN_SIZE)
  342.         {
  343.             register WORD i;
  344.  
  345.                 /* Scan the remaining codes for a match. */
  346.  
  347.             for(i = ScanStep ; i < NumCodes ; i++)
  348.             {
  349.                     /* This sequence begins with the
  350.                      * same character the parser was
  351.                      * initialized with, so let's take
  352.                      * a look at it.
  353.                      */
  354.  
  355.                 if(ANSICode[i] . FirstChar == SaveBuffer[0])
  356.                 {
  357.                         /* This character is supposed to
  358.                          * terminate the sequence, so exit.
  359.                          */
  360.  
  361.                     if(ANSICode[i] . LastChar == c)
  362.                     {
  363.                         if(Config -> TerminalConfig -> EmulationMode != EMULATION_ATOMIC)
  364.                         {
  365.                             SaveBuffer[CharsInBuffer++] = c;
  366.                             SaveBuffer[CharsInBuffer  ] = 0;
  367.  
  368.                             (*ANSICode[i] . Func)(SaveBuffer);
  369.                         }
  370.  
  371.                         CharsInBuffer = ScanStep = 0;
  372.  
  373.                         return(FALSE);
  374.                     }
  375.                     else
  376.                     {
  377.                             /* If this character is part of
  378.                              * a legal sequence store it
  379.                              * and return.
  380.                              */
  381.  
  382.                         if(ANSICode[i] . Match[c])
  383.                         {
  384.                             ScanStep = i;
  385.  
  386.                             SaveBuffer[CharsInBuffer++] = c;
  387.  
  388.                             return(TRUE);
  389.                         }
  390.                     }
  391.                 }
  392.             }
  393.         }
  394.     }
  395.  
  396.         /* Return failure. */
  397.  
  398.     CharsInBuffer = ScanStep = 0;
  399.  
  400.     return(FALSE);
  401. }
  402.  
  403.     /* GetFontWidth():
  404.      *
  405.      *    Get the font width of the current line.
  406.      */
  407.  
  408. STATIC WORD
  409. GetFontWidth(VOID)
  410. {
  411.     if(RasterAttr[CursorY] == SCALE_ATTR_NORMAL)
  412.     {
  413.         if(Config -> EmulationConfig -> FontScale == SCALE_HALF)
  414.             return((WORD)(TextFontWidth / 2));
  415.         else
  416.             return(TextFontWidth);
  417.     }
  418.     else
  419.     {
  420.         if(Config -> EmulationConfig -> FontScale == SCALE_HALF)
  421.             return(TextFontWidth);
  422.         else
  423.             return((WORD)(TextFontWidth * 2));
  424.     }
  425. }
  426.  
  427.     /* ColourValue(UWORD Colour):
  428.      *
  429.      *    Calculate the value of a given colour (brightness).
  430.      */
  431.  
  432. STATIC WORD __inline
  433. ColourValue(UWORD Colour)
  434. {
  435.     BYTE    Red,Green,Blue;
  436.     WORD    Sum;
  437.  
  438.     Red    =  Colour >> 8;
  439.     Green    = (Colour >> 4) & 0xF;
  440.     Blue    =  Colour       & 0xF;
  441.  
  442.     Sum = (Red + Green + Blue) / 3;
  443.  
  444.     return(Sum);
  445. }
  446.  
  447.     /* ScrollRegion(WORD Direction):
  448.      *
  449.      *    Scroll the current scroll region up or down.
  450.      */
  451.  
  452. STATIC VOID __inline
  453. ScrollRegion(WORD Direction)
  454. {
  455.     WORD RegionTop,RegionBottom,RegionLines;
  456.     LONG Dir,MinY,MaxY;
  457.  
  458.     if(Direction < 0)
  459.         Dir = -Direction;
  460.     else
  461.         Dir = Direction;
  462.  
  463.     if(RegionSet)
  464.     {
  465.         MinY         = MUL_Y(Top);
  466.         MaxY        = MUL_Y(Bottom + 1) - 1;
  467.  
  468.         RegionTop    = Top;
  469.         RegionBottom    = Bottom + 1;
  470.         RegionLines    = Bottom - Top + 1;
  471.     }
  472.     else
  473.     {
  474.         MinY        = 0;
  475.         MaxY         = MUL_Y(LastLine + 1) - 1;
  476.  
  477.         RegionTop    = 0;
  478.         RegionBottom    = LastLine + 1;
  479.         RegionLines    = LastLine + 1;
  480.     }
  481.  
  482.     BackupRender();
  483.  
  484.     RasterScrollRegion(Direction,RegionTop,RegionBottom,RegionLines);
  485.  
  486.     if(Config -> EmulationConfig -> ScrollMode == SCROLL_JUMP || (TextFontHeight & 1))
  487.     {
  488.         if(Dir > RegionLines)
  489.             ScrollLineRectFill(RPort,0,MinY,LastPixel,MaxY);
  490.         else
  491.         {
  492.             if(Direction > 0)
  493.                 ScrollLineRaster(RPort,0,MUL_Y(Direction),0,MinY,LastPixel,MaxY,FALSE);
  494.             else
  495.                 ScrollLineRaster(RPort,0,-MUL_Y(-Direction),0,MinY,LastPixel,MaxY,FALSE);
  496.         }
  497.     }
  498.     else
  499.     {
  500.         if(Direction > 0)
  501.             ScrollLineRaster(RPort,0,MUL_Y(Direction),0,MinY,LastPixel,MaxY,TRUE);
  502.         else
  503.             ScrollLineRaster(RPort,0,-MUL_Y(-Direction),0,MinY,LastPixel,MaxY,TRUE);
  504.     }
  505.  
  506.     BackupRender();
  507. }
  508.  
  509.     /* LastChar(STRPTR Buffer):
  510.      *
  511.      *    Return the last character in a string.
  512.      */
  513.  
  514. STATIC UBYTE __inline
  515. LastChar(STRPTR Buffer)
  516. {
  517.     WORD Offset = 0;
  518.  
  519.     while(Buffer[Offset])
  520.         Offset++;
  521.  
  522.     return(Buffer[Offset - 1]);
  523. }
  524.  
  525.     /* ReadValue(STRPTR Buffer,BYTE *Value):
  526.      *
  527.      *    Parse a buffer for numbers and return a pointer
  528.      *    to the next buffer element to contain additional
  529.      *    information.
  530.      */
  531.  
  532. STATIC STRPTR __inline
  533. ReadValue(STRPTR Buffer,WORD *Value)
  534. {
  535.     while((*Buffer < '0' || *Buffer > '9') && (*Buffer != ';') && *Buffer)
  536.         Buffer++;
  537.  
  538.     if(*Buffer)
  539.     {
  540.         *Value = 0;
  541.  
  542.         while(*Buffer >= '0' && *Buffer <= '9')
  543.             *Value = (*Value * 10) + (*Buffer++ - '0');
  544.     }
  545.     else
  546.         *Value = -1;
  547.  
  548.     if(*Buffer == ';' || *Buffer == ' ')
  549.         return(&Buffer[1]);
  550.     else
  551.         return(NULL);
  552. }
  553.  
  554.     /* ClipBlitCursor(UBYTE DoClip,UBYTE DoMove):
  555.      *
  556.      *    Change the appearance of the cursor.
  557.      */
  558.  
  559. VOID __regargs
  560. ClipBlitCursor(UBYTE DoClip,UBYTE DoMove)
  561. {
  562.     if(DoClip || DoMove)
  563.     {
  564.         STATIC WORD LastCursorX = -1,LastCursorY = -1;
  565.         STATIC LONG DestX,DestY,XSize,X,Y,Column;
  566.  
  567.         if(CursorY != LastCursorY || CursorX != LastCursorX)
  568.         {
  569.             if(CursorY != LastCursorY)
  570.             {
  571.                 if(CursorY > LastLine)
  572.                     Y = LastLine;
  573.                 else
  574.                 {
  575.                     if(CursorY < 0)
  576.                         Y = 0;
  577.                     else
  578.                         Y = CursorY;
  579.                 }
  580.  
  581.                 if(RasterAttr[Y] == SCALE_ATTR_NORMAL)
  582.                     Column = LastColumn;
  583.                 else
  584.                     Column = ((LastColumn + 1) / 2) - 1;
  585.  
  586.                 DestY = MUL_Y(Y);
  587.  
  588.                 LastCursorY = CursorY;
  589.             }
  590.  
  591.             LastCursorX = CursorX;
  592.  
  593.             if(CursorX > Column)
  594.                 X = Column;
  595.             else
  596.             {
  597.                 if(CursorX < 0)
  598.                     X = 0;
  599.                 else
  600.                     X = CursorX;
  601.             }
  602.  
  603.             if(Config -> EmulationConfig -> FontScale == SCALE_NORMAL)
  604.             {
  605.                 if(RasterAttr[Y] == SCALE_ATTR_NORMAL)
  606.                 {
  607.                     DestX = MUL_X(X);
  608.                     XSize = TextFontWidth;
  609.                 }
  610.                 else
  611.                 {
  612.                     DestX = MUL_X(X) * 2;
  613.                     XSize = TextFontWidth * 2;
  614.  
  615.                     if(X > ((LastColumn + 1) / 2) - 1)
  616.                         X = ((LastColumn + 1) / 2) - 1;
  617.                 }
  618.             }
  619.             else
  620.             {
  621.                 if(RasterAttr[CursorY] == SCALE_ATTR_NORMAL)
  622.                 {
  623.                     DestX = MUL_X(X) / 2;
  624.                     XSize = TextFontWidth / 2;
  625.                 }
  626.                 else
  627.                 {
  628.                     DestX = MUL_X(X);
  629.                     XSize = TextFontWidth;
  630.  
  631.                     if(X > ((LastColumn + 1) / 2) - 1)
  632.                         X = ((LastColumn + 1) / 2) - 1;
  633.                 }
  634.             }
  635.         }
  636.  
  637.         if(DoMove)
  638.             Move(RPort,WindowLeft + DestX,WindowTop + DestY + TextFontBase);
  639.  
  640.         if(DoClip)
  641.         {
  642.             if(UseMasking)
  643.             {
  644.                 UBYTE Mask = RPort -> Mask;
  645.  
  646.                 SetWrMsk(RPort,DepthMask);
  647.  
  648.                 ClipBlit(RPort,0,0,RPort,WindowLeft + DestX,WindowTop + DestY,XSize,TextFontHeight,0x50);
  649.  
  650.                 SetWrMsk(RPort,Mask);
  651.             }
  652.             else
  653.                 ClipBlit(RPort,0,0,RPort,WindowLeft + DestX,WindowTop + DestY,XSize,TextFontHeight,0x50);
  654.         }
  655.     }
  656. }
  657.  
  658.     /* ClearCursor():
  659.      *
  660.      *    Clear the cursor image.
  661.      */
  662.  
  663. VOID
  664. ClearCursor()
  665. {
  666.     if(CursorEnabled)
  667.     {
  668.         ClipBlitCursor(TRUE,FALSE);
  669.  
  670.         CursorEnabled = FALSE;
  671.     }
  672. }
  673.  
  674.     /* DrawCursor():
  675.      *
  676.      *    Explicitely (re-)draw the cursor image.
  677.      */
  678.  
  679. VOID
  680. DrawCursor()
  681. {
  682.     if(!CursorEnabled)
  683.     {
  684.         ClipBlitCursor(TRUE,FALSE);
  685.  
  686.         CursorEnabled = TRUE;
  687.     }
  688. }
  689.  
  690.     /* BackupRender():
  691.      *
  692.      *    Save current draw modes, pen and position or restore
  693.      *    the data.
  694.      */
  695.  
  696. VOID
  697. BackupRender()
  698. {
  699.     STATIC BYTE    Called = FALSE;
  700.     STATIC UBYTE    DrMd,
  701.             FgPen,
  702.             BgPen;
  703.     STATIC UWORD    OldX,OldY;
  704.     STATIC UBYTE    Style;
  705.  
  706.     if(!Called)
  707.     {
  708.         DrMd    = ReadDrMd(RPort);
  709.         FgPen    = ReadAPen(RPort);
  710.         BgPen    = ReadBPen(RPort);
  711.  
  712.         OldX    = RPort -> cp_x - WindowLeft;
  713.         OldY    = RPort -> cp_y - WindowTop;
  714.  
  715.         Style    = StyleType;
  716.  
  717.         Called    = TRUE;
  718.     }
  719.     else
  720.     {
  721.         if(ReadDrMd(RPort) != DrMd)
  722.             SetDrMd(RPort,DrMd);
  723.  
  724.         if(ReadAPen(RPort) != FgPen)
  725.             SetAPen(RPort,FgPen);
  726.  
  727.         if(ReadBPen(RPort) != BgPen)
  728.             SetBPen(RPort,BgPen);
  729.  
  730.         Move(RPort,OldX + WindowLeft,OldY + WindowTop);
  731.  
  732.         if(Style != StyleType)
  733.         {
  734.             SetSoftStyle(RPort,Style,0xFF);
  735.  
  736.             StyleType = Style;
  737.         }
  738.  
  739.         Called = FALSE;
  740.     }
  741. }
  742.  
  743.     /* ShiftChar(LONG Size):
  744.      *
  745.      *    Simulate character insertion at the current cursor
  746.      *    position by shifting the whole line Size times eight pixels
  747.      *    to the right.
  748.      */
  749.  
  750. VOID __regargs
  751. ShiftChar(LONG Size)
  752. {
  753.     LONG DeltaX,MinX,MinY;
  754.  
  755.     MinY = MUL_Y(CursorY);
  756.  
  757.     if(Config -> EmulationConfig -> FontScale == SCALE_NORMAL)
  758.     {
  759.         if(RasterAttr[CursorY] == SCALE_ATTR_NORMAL)
  760.         {
  761.             DeltaX    = MUL_X(Size);
  762.             MinX    = MUL_X(CursorX);
  763.         }
  764.         else
  765.         {
  766.             DeltaX    = MUL_X(Size) * 2;
  767.             MinX    = MUL_X(CursorX) * 2;
  768.         }
  769.     }
  770.     else
  771.     {
  772.         if(RasterAttr[CursorY] == SCALE_ATTR_NORMAL)
  773.         {
  774.             DeltaX    = MUL_X(Size) / 2;
  775.             MinX    = MUL_X(CursorX) / 2;
  776.         }
  777.         else
  778.         {
  779.             DeltaX    = MUL_X(Size);
  780.             MinX    = MUL_X(CursorX);
  781.         }
  782.     }
  783.  
  784.     if(MinX < WindowWidth)
  785.     {
  786.         BackupRender();
  787.  
  788.         ScrollLineRaster(RPort,-DeltaX,0,MinX,MinY,LastPixel,MinY + TextFontHeight - 1,FALSE);
  789.  
  790.         BackupRender();
  791.     }
  792. }
  793.  
  794.     /* Ignore():
  795.      *
  796.      *    Do nothing, return immediately.
  797.      */
  798.  
  799. VOID
  800. Ignore()
  801. {
  802. }
  803.  
  804.     /* ScrollDown(STRPTR Buffer):
  805.      *
  806.      *    Scroll the current region down.
  807.      */
  808.  
  809. VOID __regargs
  810. ScrollDown(STRPTR Buffer)
  811. {
  812.     WORD Value;
  813.  
  814.     ReadValue(Buffer,&Value);
  815.  
  816.     if(Value < 1)
  817.         Value = 1;
  818.  
  819.     ScrollRegion(-Value);
  820. }
  821.  
  822.     /* ScrollUp(STRPTR Buffer):
  823.      *
  824.      *    Scroll the current region up.
  825.      */
  826.  
  827. VOID __regargs
  828. ScrollUp(STRPTR Buffer)
  829. {
  830.     WORD Value;
  831.  
  832.     ReadValue(Buffer,&Value);
  833.  
  834.     if(Value < 1)
  835.         Value = 1;
  836.  
  837.     ScrollRegion(Value);
  838. }
  839.  
  840.     /* CursorScrollDown():
  841.      *
  842.      *    Move cursor down and scroll region if necessary.
  843.      */
  844.  
  845. VOID
  846. CursorScrollDown()
  847. {
  848.     DownLine();
  849.  
  850.     ClipBlitCursor(FALSE,TRUE);
  851. }
  852.  
  853. VOID
  854. DownLine()
  855. {
  856.     UBYTE InRegion = TRUE;
  857.     WORD  Hit      = LastLine;
  858.  
  859.     if(RegionSet)
  860.     {
  861.         if(CursorY <= Bottom)
  862.             Hit = Bottom;
  863.         else
  864.             InRegion = FALSE;
  865.     }
  866.  
  867.     if(CursorY == Hit)
  868.     {
  869.         if(InRegion)
  870.             ScrollRegion(1);
  871.     }
  872.     else
  873.     {
  874.         CursorY++;
  875.  
  876.         if(CursorY > LastLine)
  877.             CursorY = LastLine;
  878.  
  879.         ConFontScaleUpdate();
  880.     }
  881. }
  882.  
  883.     /* CursorScrollUp():
  884.      *
  885.      *    Move cursor up and scroll region if necessary.
  886.      */
  887.  
  888. VOID
  889. CursorScrollUp()
  890. {
  891.     BYTE InRegion    = TRUE;
  892.     WORD Hit    = 0;
  893.  
  894.     if(RegionSet)
  895.     {
  896.         if(CursorY >= Top)
  897.             Hit = Top;
  898.         else
  899.             InRegion = FALSE;
  900.     }
  901.  
  902.     if(CursorY == Hit)
  903.     {
  904.         if(InRegion)
  905.             ScrollRegion(-1);
  906.     }
  907.     else
  908.     {
  909.         if(--CursorY < 0)
  910.             CursorY = 0;
  911.  
  912.         ConFontScaleUpdate();
  913.     }
  914.  
  915.     ClipBlitCursor(FALSE,TRUE);
  916. }
  917.  
  918.     /* NextLine():
  919.      *
  920.      *    Do something like CR+LF.
  921.      */
  922.  
  923. VOID
  924. NextLine()
  925. {
  926.     CursorX = 0;
  927.  
  928.     DownLine();
  929.  
  930.     ClipBlitCursor(FALSE,TRUE);
  931. }
  932.  
  933.     /* SaveCursor():
  934.      *
  935.      *    Save cursor position and rendering attributes.
  936.      */
  937.  
  938. VOID
  939. SaveCursor()
  940. {
  941.     CursorBackup . Charset        = Charset;
  942.     CursorBackup . Attributes    = Attributes;
  943.     CursorBackup . CursorX        = CursorX;
  944.     CursorBackup . CursorY        = CursorY;
  945.     CursorBackup . Style        = StyleType;
  946.     CursorBackup . FgPen        = GetPenIndex(ReadAPen(RPort));
  947.     CursorBackup . BgPen        = GetPenIndex(ReadBPen(RPort));
  948.     CursorBackup . CurrentFont    = CurrentFont;
  949.     CursorBackup . CharMode[0]    = CharMode[0];
  950.     CursorBackup . CharMode[1]    = CharMode[1];
  951. }
  952.  
  953.     /* FontStuff(STRPTR Buffer):
  954.      *
  955.      *    Set the drawing font (standard characters/line).
  956.      */
  957.  
  958. VOID __regargs
  959. FontStuff(STRPTR Buffer)
  960. {
  961.     BYTE Changed = FALSE;
  962.  
  963.     if(Buffer[0] == '(')
  964.     {
  965.         switch(LastChar(Buffer))
  966.         {
  967.             case 'A':
  968.             case 'B':
  969.  
  970.                 if(CharMode[0] != TABLE_ASCII && !Charset)
  971.                     Changed = TRUE;
  972.  
  973.                 CharMode[0] = TABLE_ASCII;
  974.  
  975.                 break;
  976.  
  977.             case '0':
  978.  
  979.                 if(CharMode[0] != TABLE_GFX && !Charset)
  980.                     Changed = TRUE;
  981.  
  982.                 CharMode[0] = TABLE_GFX;
  983.  
  984.                 break;
  985.         }
  986.     }
  987.  
  988.     if(Buffer[0] == ')')
  989.     {
  990.         switch(LastChar(Buffer))
  991.         {
  992.             case 'A':
  993.             case 'B':
  994.  
  995.                 if(CharMode[1] != TABLE_ASCII && Charset == 1)
  996.                     Changed = TRUE;
  997.  
  998.                 CharMode[1] = TABLE_ASCII;
  999.  
  1000.                 break;
  1001.  
  1002.             case '0':
  1003.  
  1004.                 if(CharMode[1] != TABLE_GFX && Charset == 1)
  1005.                     Changed = TRUE;
  1006.  
  1007.                 CharMode[1] = TABLE_GFX;
  1008.  
  1009.                 break;
  1010.         }
  1011.     }
  1012.  
  1013.     if(Changed)
  1014.     {
  1015.         BackupRender();
  1016.  
  1017.         if(Charset)
  1018.             DoShiftIn();
  1019.         else
  1020.             DoShiftOut();
  1021.  
  1022.         BackupRender();
  1023.     }
  1024. }
  1025.  
  1026.     /* LoadCursor():
  1027.      *
  1028.      *    Load cursor position and rendering attributes.
  1029.      */
  1030.  
  1031. VOID
  1032. LoadCursor()
  1033. {
  1034.     Charset        = CursorBackup . Charset;
  1035.  
  1036.     CharMode[0]    = CursorBackup . CharMode[0];
  1037.     CharMode[1]    = CursorBackup . CharMode[1];
  1038.  
  1039.     if(CurrentFont != CursorBackup . CurrentFont)
  1040.     {
  1041.         CurrentFont = CursorBackup . CurrentFont;
  1042.  
  1043.         SetFont(RPort,CurrentFont);
  1044.  
  1045.         ConOutputUpdate();
  1046.     }
  1047.  
  1048.     if(StyleType != CursorBackup . Style)
  1049.     {
  1050.         SetSoftStyle(RPort,CursorBackup . Style,0xFF);
  1051.  
  1052.         StyleType = CursorBackup . Style;
  1053.     }
  1054.  
  1055.     if(ReadAPen(RPort) != MappedPens[0][CursorBackup . FgPen])
  1056.         SetAPen(RPort,MappedPens[0][CursorBackup . FgPen]);
  1057.  
  1058.     if(ReadBPen(RPort) != MappedPens[0][CursorBackup . BgPen])
  1059.         SetBPen(RPort,MappedPens[0][CursorBackup . BgPen]);
  1060.  
  1061.     Attributes    = CursorBackup . Attributes;
  1062.     CursorX        = CursorBackup . CursorX;
  1063.     CursorY        = CursorBackup . CursorY;
  1064.  
  1065.     ConFontScaleUpdate();
  1066.  
  1067.     ClipBlitCursor(FALSE,TRUE);
  1068. }
  1069.  
  1070.     /* ScaleFont(STRPTR Buffer):
  1071.      *
  1072.      *    Select a new font scale.
  1073.      */
  1074.  
  1075. VOID __regargs
  1076. ScaleFont(STRPTR Buffer)
  1077. {
  1078.     WORD NewScale,Scale;
  1079.  
  1080.     Scale = RasterAttr[CursorY];
  1081.  
  1082.     NewScale = Scale;
  1083.  
  1084.     switch(LastChar(Buffer))
  1085.     {
  1086.         case '3':
  1087.  
  1088.             NewScale = SCALE_ATTR_TOP2X;
  1089.  
  1090.             break;
  1091.  
  1092.         case '4':
  1093.  
  1094.             NewScale = SCALE_ATTR_BOT2X;
  1095.  
  1096.             break;
  1097.  
  1098.         case '5':
  1099.  
  1100.             NewScale = SCALE_NORMAL;
  1101.  
  1102.             break;
  1103.  
  1104.         case '6':
  1105.  
  1106.             NewScale = SCALE_ATTR_2X;
  1107.  
  1108.             break;
  1109.     }
  1110.  
  1111.     if(Scale != NewScale)
  1112.     {
  1113.         UBYTE    *RasterPtr    = &Raster[CursorY * RasterWidth];
  1114.         WORD     RightMargin    = LastColumn + 1,
  1115.              CursorXSave    = CursorX;
  1116.  
  1117.         if(NewScale != SCALE_ATTR_NORMAL)
  1118.             RightMargin /= 2;
  1119.  
  1120.         RasterAttr[CursorY] = NewScale;
  1121.  
  1122.         ConFontScaleUpdate();
  1123.  
  1124.         if(((Config -> EmulationConfig -> FontScale == SCALE_NORMAL) && (NewScale == SCALE_ATTR_NORMAL)) || ((Config -> EmulationConfig -> FontScale == SCALE_HALF) && (NewScale == SCALE_ATTR_2X)))
  1125.         {
  1126.             Move(RPort,WindowLeft,WindowTop + MUL_Y(CursorY) + TextFontBase);
  1127.             Text(RPort,RasterPtr,RightMargin);
  1128.         }
  1129.         else
  1130.         {
  1131.             CursorX = 0;
  1132.  
  1133.             PrintScaled(RasterPtr,RightMargin,NewScale);
  1134.         }
  1135.  
  1136.         if(CursorXSave >= RightMargin)
  1137.             CursorX = RightMargin - 1;
  1138.         else
  1139.             CursorX = CursorXSave;
  1140.     }
  1141.  
  1142.     ClipBlitCursor(FALSE,TRUE);
  1143. }
  1144.  
  1145.     /* AlignmentTest():
  1146.      *
  1147.      *    Perform screen alignment test, fill the screen with `E's.
  1148.      */
  1149.  
  1150. VOID
  1151. AlignmentTest()
  1152. {
  1153.     STRPTR Buffer;
  1154.  
  1155.     if(Buffer = AllocVec(LastColumn + 1,MEMF_ANY))
  1156.     {
  1157.         WORD i;
  1158.  
  1159.         memset(Buffer,'E',LastColumn + 1);
  1160.  
  1161.         EraseScreen("2");
  1162.  
  1163.         if(Config -> EmulationConfig -> FontScale == SCALE_HALF)
  1164.         {
  1165.             for(i = 0 ; i <= LastLine ; i++)
  1166.             {
  1167.                 CursorX = 0;
  1168.                 CursorY = i;
  1169.  
  1170.                 RasterAttr[i] = SCALE_ATTR_NORMAL;
  1171.  
  1172.                 RasterPutString(Buffer,LastColumn + 1);
  1173.                 ScrollLinePutString(LastColumn + 1);
  1174.  
  1175.                 Move(RPort,WindowLeft,WindowTop + MUL_Y(i) + TextFontBase);
  1176.                 PrintScaled(Buffer,LastColumn + 1,SCALE_ATTR_NORMAL);
  1177.             }
  1178.         }
  1179.         else
  1180.         {
  1181.             for(i = 0 ; i <= LastLine ; i++)
  1182.             {
  1183.                 CursorX = 0;
  1184.                 CursorY = i;
  1185.  
  1186.                 RasterAttr[i] = SCALE_ATTR_NORMAL;
  1187.  
  1188.                 RasterPutString(Buffer,LastColumn + 1);
  1189.                 ScrollLinePutString(LastColumn + 1);
  1190.  
  1191.                 Move(RPort,WindowLeft,WindowTop + MUL_Y(i) + TextFontBase);
  1192.                 Text(RPort,Buffer,LastColumn + 1);
  1193.             }
  1194.         }
  1195.  
  1196.         CursorX = CursorY = 0;
  1197.  
  1198.         ClipBlitCursor(FALSE,TRUE);
  1199.  
  1200.         FreeVec(Buffer);
  1201.  
  1202.         ConFontScaleUpdate();
  1203.     }
  1204. }
  1205.  
  1206.     /* SetTab():
  1207.      *
  1208.      *    Set a tabulator stop at the current position.
  1209.      */
  1210.  
  1211. VOID
  1212. SetTab()
  1213. {
  1214.     if(CursorX < TabStopMax)
  1215.         TabStops[CursorX] = TRUE;
  1216. }
  1217.  
  1218.     /* RequestTerminal(STRPTR Buffer):
  1219.      *
  1220.      *    Return the current terminal position.
  1221.      */
  1222.  
  1223. VOID __regargs
  1224. RequestTerminal(STRPTR Buffer)
  1225. {
  1226.     switch(Buffer[0])
  1227.     {
  1228.             /* Make ourselves known as a VT200
  1229.              * terminal.
  1230.              */
  1231.  
  1232.         case '[':
  1233.  
  1234.             if(Buffer[1] != '>')
  1235.                 SerWrite("\033[?62;1;2;6;7;8;9c",-1);
  1236.             else
  1237.                 SerWrite("\033[>1;10;0c",-1);
  1238.  
  1239.             break;
  1240.  
  1241.             /* This is an old status request type,
  1242.              * we will return the standard `I am a
  1243.              * VT101' sequence.
  1244.              */
  1245.  
  1246.         case 'Z':
  1247.  
  1248.             SerWrite("\033[?1;0c",-1);
  1249.             break;
  1250.     }
  1251. }
  1252.  
  1253.     /* SoftReset():
  1254.      *
  1255.      *    Plain and simple: reset the text rendering colours, style and the
  1256.      *    font being used. This works similar to the Reset() call which
  1257.      *    also clears the screen.
  1258.      */
  1259.  
  1260. VOID
  1261. SoftReset()
  1262. {
  1263.         /* Are we running on an external emulation? */
  1264.  
  1265.     if(XEmulatorBase && Config -> TerminalConfig -> EmulationMode == EMULATION_EXTERNAL)
  1266.     {
  1267.         XEmulatorResetTextStyles(XEM_IO);
  1268.         XEmulatorResetCharset(XEM_IO);
  1269.     }
  1270.     else
  1271.     {
  1272.             /* Reset the text rendering styles. */
  1273.  
  1274.         SetAttributes("0m");
  1275.  
  1276.         Config -> EmulationConfig -> FontScale = SCALE_NORMAL;
  1277.  
  1278.         ConFontScaleUpdate();
  1279.  
  1280.             /* Reset the text rendering colours. */
  1281.  
  1282.         FgPen = GetPenIndex(SafeTextPen);
  1283.         BgPen = 0;
  1284.  
  1285.         if(ReadAPen(RPort) != MappedPens[0][FgPen])
  1286.             SetAPen(RPort,MappedPens[0][FgPen]);
  1287.  
  1288.         if(ReadBPen(RPort) != MappedPens[0][BgPen])
  1289.             SetBPen(RPort,MappedPens[0][BgPen]);
  1290.  
  1291.         SetWrMsk(RPort,DepthMask);
  1292.  
  1293.         CurrentFont = TextFont;
  1294.  
  1295.         SetFont(RPort,CurrentFont);
  1296.  
  1297.         ConOutputUpdate();
  1298.     }
  1299. }
  1300.  
  1301.     /* Reset():
  1302.      *
  1303.      *    Reset terminal to initial state.
  1304.      */
  1305.  
  1306. VOID
  1307. Reset()
  1308. {
  1309.     LONG    MaxColumns,MaxLines,
  1310.         Columns,Lines,
  1311.         i;
  1312.  
  1313.     CursorEnabled = FALSE;
  1314.  
  1315.         /* Determine window inner dimensions and top/left edge offsets. */
  1316.  
  1317.     WindowLeft    = Window -> BorderLeft;
  1318.     WindowTop    = Window -> BorderTop;
  1319.  
  1320.     WindowWidth    = Window -> Width - (Window -> BorderLeft + Window -> BorderRight);
  1321.     WindowHeight    = Window -> Height - (Window -> BorderTop + Window -> BorderBottom);
  1322.  
  1323.     MaxColumns    = WindowWidth / TextFontWidth;
  1324.     MaxLines    = WindowHeight / TextFontHeight;
  1325.  
  1326.         /* Set up the new screen width. */
  1327.  
  1328.     if(Config -> TerminalConfig -> NumColumns < 20)
  1329.         Columns = MaxColumns;
  1330.     else
  1331.         Columns = Config -> TerminalConfig -> NumColumns;
  1332.  
  1333.         /* Set up the new screen height. */
  1334.  
  1335.     if(Config -> TerminalConfig -> NumLines < 20)
  1336.         Lines = MaxLines;
  1337.     else
  1338.         Lines = Config -> TerminalConfig -> NumLines;
  1339.  
  1340.         /* More columns than we will be able to display? */
  1341.  
  1342.     if(Columns > MaxColumns)
  1343.         Columns = MaxColumns;
  1344.  
  1345.         /* More lines than we will be able to display? */
  1346.  
  1347.     if(Lines > MaxLines)
  1348.         Lines = MaxLines;
  1349.  
  1350.         /* Set up the central data. */
  1351.  
  1352.     LastColumn    = Columns - 1;
  1353.     LastLine    = Lines - 1;
  1354.     LastPixel    = MUL_X(Columns) - 1;
  1355.  
  1356.     memset(TabStops,FALSE,TabStopMax);
  1357.  
  1358.     for(i = 8 ; i < TabStopMax ; i += 8)
  1359.         TabStops[i] = TRUE;
  1360.  
  1361.     CharMode[0] = TABLE_ASCII;
  1362.     CharMode[1] = TABLE_GFX;
  1363.  
  1364.     Charset = 0;
  1365.  
  1366.     SetAPen(RPort,MappedPens[0][0]);
  1367.  
  1368.     SetWrMsk(RPort,DepthMask);
  1369.  
  1370.     RectFill(RPort,WindowLeft,WindowTop,WindowLeft + WindowWidth - 1,WindowTop + WindowHeight - 1);
  1371.  
  1372.     ScrollLineEraseScreen(2);
  1373.  
  1374.     RasterEraseScreen(2);
  1375.  
  1376.     FgPen = GetPenIndex(SafeTextPen);
  1377.     BgPen = 0;
  1378.  
  1379.     if(ReadAPen(RPort) != MappedPens[0][FgPen])
  1380.         SetAPen(RPort,MappedPens[0][FgPen]);
  1381.  
  1382.     if(ReadBPen(RPort) != MappedPens[0][BgPen])
  1383.         SetBPen(RPort,MappedPens[0][BgPen]);
  1384.  
  1385.     if(StyleType != FS_NORMAL)
  1386.     {
  1387.         SetSoftStyle(RPort,FS_NORMAL,0xFF);
  1388.  
  1389.         StyleType = FS_NORMAL;
  1390.     }
  1391.  
  1392.     CurrentFont = TextFont;
  1393.  
  1394.     SetFont(RPort,CurrentFont);
  1395.  
  1396.     ConOutputUpdate();
  1397.  
  1398.     UseRegion = FALSE;
  1399.     RegionSet = FALSE;
  1400.  
  1401.     Config -> EmulationConfig -> CursorMode        = KEYMODE_STANDARD;
  1402.     Config -> EmulationConfig -> NumericMode    = KEYMODE_STANDARD;
  1403.  
  1404.     Config -> EmulationConfig -> NewLineMode    = FALSE;
  1405.     Config -> EmulationConfig -> InsertMode        = FALSE;
  1406.  
  1407.     Config -> EmulationConfig -> LineWrap        = TRUE;
  1408.     Config -> EmulationConfig -> CursorWrap        = FALSE;
  1409.  
  1410.     Config -> EmulationConfig -> FontScale        = SCALE_NORMAL;
  1411.     Config -> EmulationConfig -> ScrollMode        = SCROLL_JUMP;
  1412.  
  1413.     Attributes    = 0;
  1414.     Top        = 0;
  1415.     Bottom        = LastLine;
  1416.     CursorX        = 0;
  1417.     CursorY        = 0;
  1418.  
  1419.     CursorBackup . Charset        = Charset;
  1420.     CursorBackup . Attributes    = Attributes;
  1421.     CursorBackup . CursorX        = CursorX;
  1422.     CursorBackup . CursorY        = CursorY;
  1423.     CursorBackup . Style        = StyleType;
  1424.     CursorBackup . FgPen        = FgPen;
  1425.     CursorBackup . BgPen        = BgPen;
  1426.     CursorBackup . CurrentFont    = CurrentFont;
  1427.     CursorBackup . CharMode[0]    = CharMode[0];
  1428.     CursorBackup . CharMode[1]    = CharMode[1];
  1429.  
  1430.     ConFontScaleUpdate();
  1431.  
  1432.     ClipBlitCursor(FALSE,TRUE);
  1433. }
  1434.  
  1435.     /* PrinterController(STRPTR Buffer):
  1436.      *
  1437.      *    Controls various screen dump and capture functions.
  1438.      */
  1439.  
  1440. VOID __regargs
  1441. PrinterController(STRPTR Buffer)
  1442. {
  1443.     if(Config -> EmulationConfig -> PrinterEnabled)
  1444.     {
  1445.         switch(Buffer[1])
  1446.         {
  1447.             case 'i':
  1448.             case '0':
  1449.  
  1450.                 if(RegionSet)
  1451.                     PrintRegion(Top,Bottom + 1);
  1452.                 else
  1453.                     PrintRegion(0,LastLine + 1);
  1454.  
  1455.                 break;
  1456.  
  1457.             case '5':
  1458.  
  1459.                 OpenPrinterCapture(TRUE);
  1460.                 break;
  1461.  
  1462.             case '4':
  1463.  
  1464.                 ClosePrinterCapture(FALSE);
  1465.                 break;
  1466.         }
  1467.     }
  1468. }
  1469.  
  1470.     /* RequestInformation(STRPTR Buffer):
  1471.      *
  1472.      *    Request miscellaneous information (state & cursor position).
  1473.      */
  1474.  
  1475. VOID __regargs
  1476. RequestInformation(STRPTR Buffer)
  1477. {
  1478.     WORD Value;
  1479.  
  1480.     ReadValue(Buffer,&Value);
  1481.  
  1482.     switch(Value)
  1483.     {
  1484.             /* Terminal status report, return code
  1485.              * for `no malfunction'.
  1486.              */
  1487.  
  1488.         case 5:
  1489.  
  1490.             SerWrite("\033[0n",-1);
  1491.             break;
  1492.  
  1493.             /* The origin is placed at 0/0 and the first
  1494.              * cursor position is 1/1. We'll have to add
  1495.              * 1 to our internal positions since our
  1496.              * universe has been shifted one field to the
  1497.              * left top corner.
  1498.              */
  1499.  
  1500.         case 6:
  1501.  
  1502.             SPrintf(GlobalBuffer,"\033[%ld;%ldR",CursorY + 1,CursorX + 1);
  1503.  
  1504.             SerWrite(GlobalBuffer,-1);
  1505.  
  1506.             break;
  1507.  
  1508.             /* A VT200 command: request printer status.
  1509.              * We will return `the printer is ready' in
  1510.              * case the printer control commands are
  1511.              * enabled, else return `no printer connected'.
  1512.              */
  1513.  
  1514.         case 15:
  1515.  
  1516.             if(Config -> EmulationConfig -> PrinterEnabled)
  1517.                 SerWrite("\033[?10n",-1);
  1518.             else
  1519.                 SerWrite("\033[?11n",-1);
  1520.  
  1521.             break;
  1522.  
  1523.             /* VT200 command: request user defined
  1524.              * key status. We will return `user
  1525.              * defined keys are locked'.
  1526.              */
  1527.  
  1528.         case 25:
  1529.  
  1530.             SerWrite("\033[?21n",-1);
  1531.             break;
  1532.  
  1533.             /* Another VT200 command: request
  1534.              * keyboard language. We will return
  1535.              * `keyboard language unknown'.
  1536.              */
  1537.  
  1538.         case 26:
  1539.  
  1540.             SerWrite("\033[?27;0n",-1);
  1541.             break;
  1542.     }
  1543. }
  1544.  
  1545.     /* SetSomething(STRPTR Buffer):
  1546.      *
  1547.      *    Set a terminal option.
  1548.      */
  1549.  
  1550. VOID __regargs
  1551. SetSomething(STRPTR Buffer)
  1552. {
  1553.     switch(Buffer[1])
  1554.     {
  1555.         case '?':
  1556.  
  1557.             switch(Buffer[2])
  1558.             {
  1559.                     /* Set cursor keys applications mode. */
  1560.  
  1561.                 case '1':
  1562.  
  1563.                     if(Buffer[3] == 'h')
  1564.                         Config -> EmulationConfig -> CursorMode = KEYMODE_APPLICATION;
  1565.                     else
  1566.                         Config -> EmulationConfig -> CursorMode = KEYMODE_STANDARD;
  1567.  
  1568.                     break;
  1569.  
  1570.                     /* Set line length (132 or 80). */
  1571.  
  1572.                 case '3':
  1573.  
  1574.                     if(Buffer[3] == 'h')
  1575.                     {
  1576.                         if(Config -> EmulationConfig -> FontScale != SCALE_HALF)
  1577.                         {
  1578.                             PrivateConfig = Config;
  1579.  
  1580.                             Config -> EmulationConfig -> FontScale = SCALE_HALF;
  1581.  
  1582.                             ScreenSizeStuff();
  1583.                         }
  1584.                     }
  1585.                     else
  1586.                     {
  1587.                         if(Config -> EmulationConfig -> FontScale != SCALE_NORMAL)
  1588.                         {
  1589.                             PrivateConfig = Config;
  1590.  
  1591.                             Config -> EmulationConfig -> FontScale = SCALE_NORMAL;
  1592.  
  1593.                             ScreenSizeStuff();
  1594.                         }
  1595.                     }
  1596.  
  1597.                     CursorX = CursorY = 0;
  1598.  
  1599.                     ClipBlitCursor(FALSE,TRUE);
  1600.  
  1601.                     EraseScreen("2");
  1602.  
  1603.                     break;
  1604.  
  1605.                     /* Set scroll mode (jump or smooth). */
  1606.  
  1607.                 case '4':
  1608.  
  1609.                     if(Buffer[3] == 'h')
  1610.                         Config -> EmulationConfig -> ScrollMode = SCROLL_SMOOTH;
  1611.                     else
  1612.                         Config -> EmulationConfig -> ScrollMode = SCROLL_JUMP;
  1613.  
  1614.                     break;
  1615.  
  1616.                     /* Turn region on or off. */
  1617.  
  1618.                 case '6':
  1619.  
  1620.                     if(Buffer[3] == 'h')
  1621.                         UseRegion = TRUE;
  1622.                     else
  1623.                         UseRegion = FALSE;
  1624.  
  1625.                     ResetCursor();
  1626.  
  1627.                     break;
  1628.  
  1629.                     /* Turn character wrapping on or off. */
  1630.  
  1631.                 case '7':
  1632.  
  1633.                     if(Buffer[3] == 'h')
  1634.                         Config -> EmulationConfig -> LineWrap = TRUE;
  1635.                     else
  1636.                         Config -> EmulationConfig -> LineWrap = FALSE;
  1637.  
  1638.                     break;
  1639.  
  1640.                     /* Set interlaced mode. */
  1641.  
  1642.                 case '9':
  1643.  
  1644.                     if(Buffer[3] == 'h')
  1645.                     {
  1646.                         if(!(Config -> ScreenConfig -> DisplayMode & LACE))
  1647.                         {
  1648.                             if(!ModeNotAvailable(Config -> ScreenConfig -> DisplayMode | LACE))
  1649.                             {
  1650.                                 SaveConfig(Config,PrivateConfig);
  1651.  
  1652.                                 Config -> ScreenConfig -> DisplayMode |= LACE;
  1653.  
  1654.                                 ResetDisplay = TRUE;
  1655.                             }
  1656.                         }
  1657.                     }
  1658.                     else
  1659.                     {
  1660.                         if(Config -> ScreenConfig -> DisplayMode & LACE)
  1661.                         {
  1662.                             if(!ModeNotAvailable(Config -> ScreenConfig -> DisplayMode & ~LACE))
  1663.                             {
  1664.                                 SaveConfig(Config,PrivateConfig);
  1665.  
  1666.                                 Config -> ScreenConfig -> DisplayMode &= ~LACE;
  1667.  
  1668.                                 ResetDisplay = TRUE;
  1669.                             }
  1670.                         }
  1671.                     }
  1672.  
  1673.                     break;
  1674.             }
  1675.  
  1676.             break;
  1677.  
  1678.         case '2':
  1679.  
  1680.                 /* Set newline mode. */
  1681.  
  1682.             if(Buffer[2] == '0')
  1683.             {
  1684.                 if(Buffer[3] == 'h')
  1685.                     Config -> EmulationConfig -> NewLineMode = TRUE;
  1686.                 else
  1687.                     Config -> EmulationConfig -> NewLineMode = FALSE;
  1688.             }
  1689.  
  1690.             break;
  1691.  
  1692.         case '4':
  1693.  
  1694.                 /* Set insert mode. */
  1695.  
  1696.             if(Buffer[2] == 'h')
  1697.                 Config -> EmulationConfig -> InsertMode = TRUE;
  1698.             else
  1699.                 Config -> EmulationConfig -> InsertMode = FALSE;
  1700.  
  1701.             break;
  1702.  
  1703.         case '1':
  1704.  
  1705.                 /* Print region or screen. */
  1706.  
  1707.             if(Buffer[2] == '9')
  1708.             {
  1709.                 if(Buffer[3] == 'l')
  1710.                 {
  1711.                     if(RegionSet)
  1712.                         PrintRegion(Top,Bottom + 1);
  1713.                     else
  1714.                         PrintRegion(0,LastLine + 1);
  1715.                 }
  1716.  
  1717.                 if(Buffer[3] == 'h')
  1718.                     PrintRegion(0,LastLine + 1);
  1719.             }
  1720.  
  1721.             break;
  1722.  
  1723.         default:
  1724.  
  1725.             break;
  1726.     }
  1727. }
  1728.  
  1729.     /* NumericAppMode(STRPTR Buffer):
  1730.      *
  1731.      *    Set the numeric pad applications mode.
  1732.      */
  1733.  
  1734. VOID __regargs
  1735. NumericAppMode(STRPTR Buffer)
  1736. {
  1737.     if(*Buffer == '=')
  1738.         Config -> EmulationConfig -> NumericMode = TRUE;
  1739.     else
  1740.     {
  1741.         if(*Buffer == '>')
  1742.             Config -> EmulationConfig -> NumericMode = FALSE;
  1743.     }
  1744. }
  1745.  
  1746.     /* MoveCursor(STRPTR Buffer):
  1747.      *
  1748.      *    Move the cursor in some direction and stop at
  1749.      *    top/bottom/margin if necessary.
  1750.      */
  1751.  
  1752. VOID __regargs
  1753. MoveCursor(STRPTR Buffer)
  1754. {
  1755.     WORD Value,Hit,LastCharPosition;
  1756.     BYTE InRegion = TRUE;
  1757.  
  1758.     ReadValue(Buffer,&Value);
  1759.  
  1760.     if(Value < 1)
  1761.         Value = 1;
  1762.  
  1763.     switch(LastChar(Buffer))
  1764.     {
  1765.             /* Move cursor Up value lines */
  1766.  
  1767.         case 'A':
  1768.  
  1769. ScrollUp:        Hit = 0;
  1770.  
  1771.             if(RegionSet)
  1772.             {
  1773.                 if(CursorY >= Top)
  1774.                     Hit = Top;
  1775.                 else
  1776.                     InRegion = FALSE;
  1777.             }
  1778.  
  1779.             CursorY -= Value;
  1780.  
  1781.             if(CursorY < Hit)
  1782.             {
  1783.                 Value = CursorY - Hit;
  1784.  
  1785.                 CursorY = Hit;
  1786.  
  1787.                 if(Config -> EmulationConfig -> CursorWrap && InRegion)
  1788.                     ScrollRegion(Value);
  1789.             }
  1790.  
  1791.             ConFontScaleUpdate();
  1792.  
  1793.             break;
  1794.  
  1795.             /* Move cursor Down value lines */
  1796.  
  1797.         case 'B':
  1798.  
  1799. ScrollDown:        Hit = LastLine;
  1800.  
  1801.             if(RegionSet)
  1802.             {
  1803.                 if(CursorY <= Bottom)
  1804.                     Hit = Bottom;
  1805.                 else
  1806.                     InRegion = FALSE;
  1807.             }
  1808.  
  1809.             CursorY += Value;
  1810.  
  1811.             if(CursorY > Hit)
  1812.             {
  1813.                 Value = CursorY - Hit;
  1814.  
  1815.                 CursorY = Hit;
  1816.  
  1817.                 if(Config -> EmulationConfig -> CursorWrap && InRegion)
  1818.                     ScrollRegion(Value);
  1819.             }
  1820.  
  1821.             ConFontScaleUpdate();
  1822.  
  1823.             break;
  1824.  
  1825.             /* Move cursor Right value columns */
  1826.  
  1827.         case 'C':
  1828.  
  1829.             CursorX += Value;
  1830.  
  1831.             if(CursorX > LastColumn)
  1832.             {
  1833.                 if(Config -> EmulationConfig -> CursorWrap)
  1834.                 {
  1835.                     Value     = CursorX / (LastColumn + 1);
  1836.  
  1837.                     CursorX    -= Value * (LastColumn + 1);
  1838.  
  1839.                     goto ScrollDown;
  1840.                 }
  1841.                 else
  1842.                     CursorX = LastColumn;
  1843.             }
  1844.  
  1845.             break;
  1846.  
  1847.             /* Move cursor Left value columns */
  1848.  
  1849.         case 'D':
  1850.  
  1851.             CursorX -= Value;
  1852.  
  1853.             if(CursorX < 0)
  1854.             {
  1855.                 if(Config -> EmulationConfig -> CursorWrap)
  1856.                 {
  1857.                     Value     = CursorX / (LastColumn + 1);
  1858.                     CursorX    -= Value * (LastColumn + 1);
  1859.                     Value     = -Value;
  1860.  
  1861.                     goto ScrollDown;
  1862.                 }
  1863.                 else
  1864.                     CursorX = 0;
  1865.             }
  1866.  
  1867.             break;
  1868.     }
  1869.  
  1870.     if(RasterAttr[CursorY] == SCALE_ATTR_NORMAL)
  1871.         LastCharPosition = LastColumn;
  1872.     else
  1873.         LastCharPosition = ((LastColumn + 1) / 2) - 1;
  1874.  
  1875.     if(CursorX > LastCharPosition)
  1876.         CursorX = LastCharPosition;
  1877.  
  1878.     ClipBlitCursor(FALSE,TRUE);
  1879. }
  1880.  
  1881.     /* EraseLine(STRPTR Buffer):
  1882.      *
  1883.      *    Erase a line on the display.
  1884.      */
  1885.  
  1886. VOID __regargs
  1887. EraseLine(STRPTR Buffer)
  1888. {
  1889.     WORD Value,Width = GetFontWidth();
  1890.  
  1891.     ReadValue(Buffer,&Value);
  1892.  
  1893.     BackupRender();
  1894.  
  1895.     SetAPen(RPort,MappedPens[0][0]);
  1896.  
  1897.     switch(Value)
  1898.     {
  1899.         case 1:
  1900.  
  1901.             ScrollLineRectFill(RPort,0,MUL_Y(CursorY),((CursorX + 1) * Width) - 1,MUL_Y(CursorY + 1) - 1);
  1902.             break;
  1903.  
  1904.         case 2:
  1905.  
  1906.             ScrollLineRectFill(RPort,0,MUL_Y(CursorY),LastPixel,MUL_Y(CursorY + 1) - 1);
  1907.             break;
  1908.  
  1909.         default:
  1910.  
  1911.             ScrollLineRectFill(RPort,CursorX * Width,MUL_Y(CursorY),LastPixel,MUL_Y(CursorY + 1) - 1);
  1912.             break;
  1913.     }
  1914.  
  1915.     ScrollLineEraseLine(Value);
  1916.  
  1917.     RasterEraseLine(Value);
  1918.  
  1919.     BackupRender();
  1920. }
  1921.  
  1922.     /* EraseScreen(STRPTR Buffer):
  1923.      *
  1924.      *    Erase parts of the screen.
  1925.      */
  1926.  
  1927. VOID __regargs
  1928. EraseScreen(STRPTR Buffer)
  1929. {
  1930.     WORD Value,Width = GetFontWidth();
  1931.  
  1932.     ReadValue(Buffer,&Value);
  1933.  
  1934.     BackupRender();
  1935.  
  1936.     SetAPen(RPort,MappedPens[0][0]);
  1937.  
  1938.     switch(Value)
  1939.     {
  1940.         case 1:
  1941.  
  1942.             if(CursorY)
  1943.                 ScrollLineRectFill(RPort,0,0,LastPixel,MUL_Y(CursorY) - 1);
  1944.  
  1945.             ScrollLineRectFill(RPort,0,MUL_Y(TextFontHeight),((CursorX + 1) * Width) - 1,MUL_Y(CursorY + 1) - 1);
  1946.  
  1947.             break;
  1948.  
  1949.         case 2:
  1950.  
  1951.             ScrollLineRectFill(RPort,0,0,LastPixel,MUL_Y(LastLine + 1) - 1);
  1952.             break;
  1953.  
  1954.         default:
  1955.  
  1956.             ScrollLineRectFill(RPort,CursorX * Width,MUL_Y(CursorY),LastPixel,MUL_Y(CursorY + 1) - 1);
  1957.  
  1958.             if(CursorY != LastLine)
  1959.                 ScrollLineRectFill(RPort,0,MUL_Y(CursorY + 1),LastPixel,MUL_Y(LastLine + 1) - 1);
  1960.  
  1961.             break;
  1962.     }
  1963.  
  1964.     ScrollLineEraseScreen(Value);
  1965.  
  1966.     RasterEraseScreen(Value);
  1967.  
  1968.     BackupRender();
  1969. }
  1970.  
  1971.     /* EraseCharacters(STRPTR Buffer):
  1972.      *
  1973.      *    Erase a number of characters.
  1974.      */
  1975.  
  1976. VOID __regargs
  1977. EraseCharacters(STRPTR Buffer)
  1978. {
  1979.     WORD Value,Width = GetFontWidth();
  1980.  
  1981.     ReadValue(Buffer,&Value);
  1982.  
  1983.     BackupRender();
  1984.  
  1985.     if(Value < 1)
  1986.         Value = 1;
  1987.  
  1988.     RasterEraseCharacters(Value);
  1989.  
  1990.     ScrollLineEraseCharacters(Value);
  1991.  
  1992.     ScrollLineRaster(RPort,Value * Width,0,CursorX * Width,MUL_Y(CursorY),LastPixel,MUL_Y(CursorY + 1) - 1,FALSE);
  1993.  
  1994.     BackupRender();
  1995. }
  1996.  
  1997.     /* InsertCharacters(STRPTR Buffer):
  1998.      *
  1999.      *    Insert a number of characters.
  2000.      */
  2001.  
  2002. VOID __regargs
  2003. InsertCharacters(STRPTR Buffer)
  2004. {
  2005.     WORD Value,Width = GetFontWidth();
  2006.  
  2007.     ReadValue(Buffer,&Value);
  2008.  
  2009.     BackupRender();
  2010.  
  2011.     if(Value < 1)
  2012.         Value = 1;
  2013.  
  2014.     RasterShiftChar(Value);
  2015.  
  2016.     ScrollLineShiftChar(Value);
  2017.  
  2018.     ScrollLineRaster(RPort,-Value * Width,0,CursorX * Width,MUL_Y(CursorY),LastPixel,MUL_Y(CursorY + 1) - 1,FALSE);
  2019.  
  2020.     BackupRender();
  2021. }
  2022.  
  2023.     /* InsertLine(STRPTR Buffer):
  2024.      *
  2025.      *    Insert a number of lines and scroll the rest of the
  2026.      *    display down.
  2027.      */
  2028.  
  2029. VOID __regargs
  2030. InsertLine(STRPTR Buffer)
  2031. {
  2032.     WORD Value,RegionBottom,RegionTop,TheTop = CursorY;
  2033.  
  2034.     ReadValue(Buffer,&Value);
  2035.  
  2036.     BackupRender();
  2037.  
  2038.     SetAPen(RPort,MappedPens[0][0]);
  2039.  
  2040.     if(Value < 1)
  2041.         Value = 1;
  2042.  
  2043.     if(RegionSet)
  2044.     {
  2045.         RegionTop    = Top;
  2046.         RegionBottom    = Bottom + 1;
  2047.     }
  2048.     else
  2049.     {
  2050.         RegionTop    = 0;
  2051.         RegionBottom    = LastLine + 1;
  2052.     }
  2053.  
  2054.     if(TheTop < RegionTop)
  2055.         TheTop = RegionTop;
  2056.  
  2057.     RasterInsertLine(Value,TheTop);
  2058.  
  2059.     ScrollLineRaster(RPort,0,-MUL_Y(Value),0,MUL_Y(TheTop),LastPixel,MUL_Y(RegionBottom) - 1,FALSE);
  2060.  
  2061.     BackupRender();
  2062. }
  2063.  
  2064.     /* ClearLine(STRPTR Buffer):
  2065.      *
  2066.      *    Clear a number of lines and scroll up the ones below it.
  2067.      */
  2068.  
  2069. VOID __regargs
  2070. ClearLine(STRPTR Buffer)
  2071. {
  2072.     WORD Value,RegionBottom,RegionTop,TheTop = CursorY;
  2073.  
  2074.     ReadValue(Buffer,&Value);
  2075.  
  2076.     BackupRender();
  2077.  
  2078.     SetAPen(RPort,MappedPens[0][0]);
  2079.  
  2080.     if(Value < 1)
  2081.         Value = 1;
  2082.  
  2083.     if(RegionSet)
  2084.     {
  2085.         RegionTop    = Top;
  2086.         RegionBottom    = Bottom + 1;
  2087.     }
  2088.     else
  2089.     {
  2090.         RegionTop    = 0;
  2091.         RegionBottom    = LastLine + 1;
  2092.     }
  2093.  
  2094.     if(TheTop < RegionTop)
  2095.         TheTop = RegionTop;
  2096.  
  2097.     RasterClearLine(Value,TheTop);
  2098.  
  2099.     ScrollLineRaster(RPort,0,MUL_Y(Value),0,MUL_Y(TheTop),LastPixel,MUL_Y(RegionBottom) - 1,FALSE);
  2100.  
  2101.     BackupRender();
  2102. }
  2103.  
  2104.     /* SetTabs(STRPTR Buffer):
  2105.      *
  2106.      *    Set the current tab stops.
  2107.      */
  2108.  
  2109. VOID __regargs
  2110. SetTabs(STRPTR Buffer)
  2111. {
  2112.     WORD Value;
  2113.  
  2114.     ReadValue(Buffer,&Value);
  2115.  
  2116.     if(Value < 1)
  2117.         Value = 0;
  2118.  
  2119.     switch(Value)
  2120.     {
  2121.         case 0:
  2122.  
  2123.             if(CursorX < TabStopMax)
  2124.                 TabStops[CursorX] = FALSE;
  2125.  
  2126.             break;
  2127.  
  2128.         case 3:
  2129.  
  2130.             memset(TabStops,FALSE,TabStopMax);
  2131.  
  2132.             break;
  2133.  
  2134.         default:
  2135.  
  2136.             break;
  2137.     }
  2138. }
  2139.  
  2140.     /* SetAbsolutePosition(STRPTR Buffer):
  2141.      *
  2142.      *    Move the cursor to a given location on the display,
  2143.      *    this routine ignores the current scroll region
  2144.      *    settings.
  2145.      */
  2146.  
  2147. VOID __regargs
  2148. SetAbsolutePosition(STRPTR Buffer)
  2149. {
  2150.     WORD Value;
  2151.  
  2152.     Buffer = ReadValue(Buffer,&Value);
  2153.  
  2154.     if(UseRegion && RegionSet)
  2155.         CursorY = Top;
  2156.     else
  2157.         CursorY = 0;
  2158.  
  2159.     CursorX = 0;
  2160.  
  2161.     if(Value != -1)
  2162.     {
  2163.             /* Our raster origin is 0/0 instead of 1/1. */
  2164.  
  2165.         if(Value)
  2166.             Value--;
  2167.  
  2168.         if(UseRegion && RegionSet)
  2169.             CursorY = Top + Value;
  2170.         else
  2171.             CursorY = Value;
  2172.  
  2173.         if(Buffer)
  2174.         {
  2175.             ReadValue(Buffer,&Value);
  2176.  
  2177.             if(Value > 0)
  2178.                 CursorX = Value - 1;
  2179.             else
  2180.                 CursorX = 0;
  2181.         }
  2182.  
  2183.             /* Truncate illegal positions. */
  2184.  
  2185.         if(CursorX > LastColumn)
  2186.             CursorX = LastColumn;
  2187.  
  2188.         if(CursorY > LastLine)
  2189.             CursorY = LastLine;
  2190.     }
  2191.  
  2192.     ConFontScaleUpdate();
  2193.  
  2194.     ClipBlitCursor(FALSE,TRUE);
  2195. }
  2196.  
  2197.     /* SetAttributes(STRPTR Buffer):
  2198.      *
  2199.      *    Set the current display rendering attributes.
  2200.      */
  2201.  
  2202. VOID __regargs
  2203. SetAttributes(STRPTR Buffer)
  2204. {
  2205.     LONG TextFlags = FS_NORMAL;
  2206.     WORD Value;
  2207.  
  2208.     do
  2209.     {
  2210.         Buffer = ReadValue(Buffer,&Value);
  2211.  
  2212.         if(Value == -1)
  2213.             Value = 0;
  2214.  
  2215.         switch(Value)
  2216.         {
  2217.             case 0:
  2218.  
  2219.                 FgPen = GetPenIndex(SafeTextPen);
  2220.                 BgPen = 0;
  2221.  
  2222.                 Attributes = 0;
  2223.  
  2224.                 break;
  2225.  
  2226.             case 1:
  2227.  
  2228.                 Attributes |= ATTR_HIGHLIGHT;
  2229.  
  2230.                 break;
  2231.  
  2232.             case 4:
  2233.  
  2234.                 Attributes |= ATTR_UNDERLINE;
  2235.  
  2236.                 break;
  2237.  
  2238.             case 5:
  2239.  
  2240.                 Attributes |= ATTR_BLINK;
  2241.  
  2242.                 break;
  2243.  
  2244.             case 7:
  2245.  
  2246.                 if(!(Attributes & ATTR_INVERSE))
  2247.                 {
  2248.                     BYTE Help;
  2249.  
  2250.                     Help    = FgPen;
  2251.                     FgPen    = BgPen;
  2252.                     BgPen    = Help;
  2253.                 }
  2254.  
  2255.                 Attributes |= ATTR_INVERSE;
  2256.  
  2257.                 break;
  2258.  
  2259.             default:
  2260.  
  2261.                 if(Value >= 30)
  2262.                 {
  2263.                     if(Value <= 37)
  2264.                     {
  2265.                         if(Attributes & ATTR_INVERSE)
  2266.                             BgPen = Value - 30;
  2267.                         else
  2268.                             FgPen = Value - 30;
  2269.                     }
  2270.                     else
  2271.                     {
  2272.                         if(Value >= 40 && Value <= 47)
  2273.                         {
  2274.                             if(Attributes & ATTR_INVERSE)
  2275.                                 FgPen = Value - 40;
  2276.                             else
  2277.                                 BgPen = Value - 40;
  2278.                         }
  2279.                     }
  2280.                 }
  2281.  
  2282.                 break;
  2283.         }
  2284.     }
  2285.     while(Buffer);
  2286.  
  2287.         /* Make sure that the text rendered will be
  2288.          * visible by wrapping the colour if
  2289.          * necessary.
  2290.          */
  2291.  
  2292.     if(FgPen > DepthMask && !(FgPen & DepthMask))
  2293.         FgPen = GetPenIndex(SafeTextPen);
  2294.  
  2295.     if(BgPen > DepthMask && !(BgPen & DepthMask))
  2296.         BgPen = GetPenIndex(SafeTextPen);
  2297.  
  2298.     if(Attributes & ATTR_UNDERLINE)
  2299.         TextFlags |= FSF_UNDERLINED;
  2300.  
  2301.     if(Attributes & ATTR_HIGHLIGHT)
  2302.     {
  2303.         if(Config -> ScreenConfig -> ColourMode == COLOUR_SIXTEEN)
  2304.         {
  2305.             if(Attributes & ATTR_INVERSE)
  2306.                 BgPen |= 8;
  2307.             else
  2308.                 FgPen |= 8;
  2309.         }
  2310.         else
  2311.             TextFlags |= FSF_BOLD;
  2312.     }
  2313.  
  2314.     if(Attributes & ATTR_BLINK)
  2315.     {
  2316.         if(Config -> TerminalConfig -> EmulationMode == EMULATION_ANSIVT100)
  2317.         {
  2318.             switch(Config -> ScreenConfig -> ColourMode)
  2319.             {
  2320.                 case COLOUR_AMIGA:
  2321.  
  2322.                     if(Attributes & ATTR_INVERSE)
  2323.                         BgPen = 3;
  2324.                     else
  2325.                         FgPen = 3;
  2326.  
  2327.                     break;
  2328.  
  2329.                 case COLOUR_EIGHT:
  2330.  
  2331.                     if(Attributes & ATTR_INVERSE)
  2332.                         BgPen |= 8;
  2333.                     else
  2334.                         FgPen |= 8;
  2335.  
  2336.                     break;
  2337.  
  2338.                 case COLOUR_MONO:
  2339.  
  2340.                     if(Attributes & ATTR_INVERSE)
  2341.                         BgPen = GetPenIndex(SafeTextPen);
  2342.                     else
  2343.                         FgPen = GetPenIndex(SafeTextPen);
  2344.  
  2345.                     break;
  2346.             }
  2347.         }
  2348.     }
  2349.  
  2350.     if(TextFlags != StyleType)
  2351.     {
  2352.         SetSoftStyle(RPort,TextFlags,0xFF);
  2353.  
  2354.         StyleType = TextFlags;
  2355.     }
  2356.  
  2357.     if(Config -> ScreenConfig -> ColourMode == COLOUR_MONO)
  2358.     {
  2359.         if(ColourValue(MappedPens[0][FgPen]) < ColourValue(MappedPens[0][BgPen]))
  2360.         {
  2361.             if(Attributes & ATTR_INVERSE)
  2362.             {
  2363.                 FgPen = GetPenIndex(SafeTextPen);
  2364.                 BgPen = 0;
  2365.             }
  2366.             else
  2367.             {
  2368.                 FgPen = 0;
  2369.                 BgPen = GetPenIndex(SafeTextPen);
  2370.             }
  2371.         }
  2372.         else
  2373.         {
  2374.             if(Attributes & ATTR_INVERSE)
  2375.             {
  2376.                 FgPen = 0;
  2377.                 BgPen = GetPenIndex(SafeTextPen);
  2378.             }
  2379.             else
  2380.             {
  2381.                 FgPen = GetPenIndex(SafeTextPen);
  2382.                 BgPen = 0;
  2383.             }
  2384.         }
  2385.     }
  2386.  
  2387.     if(MappedPens[0][FgPen] != ReadAPen(RPort))
  2388.         SetAPen(RPort,MappedPens[0][FgPen]);
  2389.  
  2390.     if(MappedPens[0][BgPen] != ReadBPen(RPort))
  2391.         SetBPen(RPort,MappedPens[0][BgPen]);
  2392.  
  2393.     ClipBlitCursor(FALSE,TRUE);
  2394. }
  2395.  
  2396.     /* SetRegion(STRPTR Buffer):
  2397.      *
  2398.      *    Set the current scroll region top and bottom.
  2399.      */
  2400.  
  2401. VOID __regargs
  2402. SetRegion(STRPTR Buffer)
  2403. {
  2404.     WORD NewTop,Value,NewBottom = LastLine;
  2405.  
  2406.     Buffer = ReadValue(Buffer,&Value);
  2407.  
  2408.     if(!Value)
  2409.         Value = 1;
  2410.  
  2411.     if(Value > 0)
  2412.     {
  2413.         if(Buffer)
  2414.         {
  2415.             NewTop = Value - 1;
  2416.  
  2417.             ReadValue(Buffer,&Value);
  2418.  
  2419.             if(Value > 0)
  2420.                 NewBottom = Value - 1;
  2421.  
  2422.             if(NewBottom > LastLine)
  2423.                 NewBottom = LastLine;
  2424.  
  2425.             if(NewTop > LastLine)
  2426.                 NewTop = LastLine;
  2427.         }
  2428.         else
  2429.         {
  2430.             NewTop        = 0;
  2431.             NewBottom    = LastLine;
  2432.         }
  2433.     }
  2434.     else
  2435.     {
  2436.         NewTop        = 0;
  2437.         NewBottom    = LastLine;
  2438.     }
  2439.  
  2440.     if(NewTop < NewBottom)
  2441.     {
  2442.         if(NewTop != 0 || NewBottom != LastLine)
  2443.         {
  2444.             Top    = NewTop;
  2445.             Bottom    = NewBottom;
  2446.  
  2447.             RegionSet = TRUE;
  2448.         }
  2449.         else
  2450.             UseRegion = RegionSet = FALSE;
  2451.  
  2452.         ResetCursor();
  2453.     }
  2454.     else
  2455.         RegionSet = FALSE;
  2456. }
  2457.  
  2458.     /* ResetCursor():
  2459.      *
  2460.      *    Reset cursor to top of screen.
  2461.      */
  2462.  
  2463. VOID
  2464. ResetCursor()
  2465. {
  2466.     CursorX    = 0;
  2467.  
  2468.     if(UseRegion && RegionSet)
  2469.         CursorY = Top;
  2470.     else
  2471.         CursorY    = 0;
  2472.  
  2473.     ConFontScaleUpdate();
  2474.  
  2475.     ClipBlitCursor(FALSE,TRUE);
  2476. }
  2477.