context: public partial class ackterm
  {
      private partial class TermWindow : System.Windows.Forms.Form
      {
          private void CommandRouter (object Sender, ParserEventArgs e)
          {
  
              //prntSome.printSome("CommandRouter");
              switch (e.Action)
              {
                  case Actions.Print:
                      this.PrintChar (e.CurChar);
                      //System.Console.Write ("{0}", e.CurChar);
                      break;
  
                  case Actions.Execute:
                      this.ExecuteChar (e.CurChar);
                      break;
  
                  case Actions.Dispatch:
                      break;
  
                  default:
                      break;
              }
  
              System.Int32 Param = 0;
  
              System.Int32 Inc = 1; // increment
  
              //rdbsb.AppendLine("cmdrotr: " + e.CurSequence + " params: "+e.CurParams.Elements.Count);
  
              //for(int rr=0; rr < e.CurParams.Elements.Count; rr++)
              //    rdbsb.AppendLine("cmdrotr params: " + e.CurParams.Elements[rr]);
  
              switch (e.CurSequence)
              {
                  case "":
                      break;
  
                  case "\x1b" + "7": //DECSC Save Cursor position and attributes
                      this.SavedCarets.Add (new uc_CaretAttribs (
                                               this.Caret.Pos,
                                               this.G0.Set, 
                                               this.G1.Set, 
                                               this.G2.Set, 
                                               this.G3.Set, 
                                               this.CharAttribs));
                                                
                      break;
  
                  case "\x1b" + "8": //DECRC Restore Cursor position and attributes
                      this.Caret.Pos   = this.SavedCarets[this.SavedCarets.Count - 1].Pos;
                      this.CharAttribs = this.SavedCarets[this.SavedCarets.Count - 1].Attribs;
  
                      this.G0.Set = this.SavedCarets[this.SavedCarets.Count - 1].G0Set;
                      this.G1.Set = this.SavedCarets[this.SavedCarets.Count - 1].G1Set;
                      this.G2.Set = this.SavedCarets[this.SavedCarets.Count - 1].G2Set;
                      this.G3.Set = this.SavedCarets[this.SavedCarets.Count - 1].G3Set;
  
                      this.SavedCarets.RemoveAt (this.SavedCarets.Count - 1);
  
                      break;
  
                  case "\x1b~": //LS1R Locking Shift G1 -> GR
                      this.CharAttribs.GR = G1;
                      break;
  
                  case "\x1bn": //LS2 Locking Shift G2 -> GL
                      this.CharAttribs.GL = G2;
                      break;
  
                  case "\x1b}": //LS2R Locking Shift G2 -> GR
                      this.CharAttribs.GR = G2;
                      break;
  
                  case "\x1bo": //LS3 Locking Shift G3 -> GL
                      this.CharAttribs.GL = G3;
                      break;
  
                  case "\x1b|": //LS3R Locking Shift G3 -> GR
                      this.CharAttribs.GR = G3;
                      break;
  
                  case "\x1b#8": //DECALN
                      e.CurParams.Elements.Add ("1");
                      e.CurParams.Elements.Add (this.Rows.ToString ());
                      this.SetScrollRegion (e.CurParams);
  
                      // put E's on the entire screen
                      for (int y = 0; y < this.Rows; y++)
                      {
                          this.CaretToAbs (y, 0);
  
                          for (int x = 0; x < this.Columns; x++)
                          {
                             this.PrintChar ('E');
                          }
                      }
                      break; 
  
                  case "\x1b=": // Keypad to Application mode
                      this.Modes.Flags = this.Modes.Flags | uc_Mode.KeypadAppln;
                      break;
  
                  case "\x1b>": // Keypad to Numeric mode
                      this.Modes.Flags = this.Modes.Flags ^ uc_Mode.KeypadAppln;
                      break;
  
                  case "\x1b[B": // CUD
  
                      if (e.CurParams.Count () > 0) 
                      {
                          Inc = System.Convert.ToInt32 (e.CurParams.Elements[0]); 
                      }
  
                      if (Inc == 0) Inc = 1;
  
                      this.CaretToAbs (this.Caret.Pos.Y + Inc, this.Caret.Pos.X);
                      break;
  
                  case "\x1b[A": // CUU
  
                      if (e.CurParams.Count () > 0) 
                      {
                          Inc = System.Convert.ToInt32 (e.CurParams.Elements[0]); 
                      }
  
                      if (Inc == 0) Inc = 1;
  
                      this.CaretToAbs (this.Caret.Pos.Y - Inc, this.Caret.Pos.X);
                      break;
  
                  case "\x1b[C": // CUF
  
                      if (e.CurParams.Count () > 0) 
                      {
                          Inc = System.Convert.ToInt32 (e.CurParams.Elements[0]); 
                      }
  
                      if (Inc == 0) Inc = 1;
  
                      this.CaretToAbs (this.Caret.Pos.Y, this.Caret.Pos.X + Inc);
                      break;
  
                  case "\x1b[D": // CUB
  
                      if (e.CurParams.Count () > 0) 
                      {
                          Inc = System.Convert.ToInt32 (e.CurParams.Elements[0]); 
                      }
  
                      if (Inc == 0) Inc = 1;
  
                      this.CaretToAbs (this.Caret.Pos.Y, this.Caret.Pos.X - Inc);
                      break;
  
                  case "\x1b[H": // CUP
  
                      System.Int32 X = 0;
                      System.Int32 Y = 0;
  
                      if (e.CurParams.Count() > 0)
                      {
                          Y = System.Convert.ToInt32(e.CurParams.Elements[0]) - 1;
                      }
  
                      if (e.CurParams.Count() > 1)
                      {
                          X = System.Convert.ToInt32(e.CurParams.Elements[1]) - 1;
                      }
  
                      this.CaretToRel(Y, X);
                      break;
                  case "\x1b[f": // HVP
  
                      System.Int32 Xf = 0;
                      System.Int32 Yf = 0;                    
  
                      if (e.CurParams.Count () > 0) 
                      {
                          Yf = System.Convert.ToInt32 (e.CurParams.Elements[0]) - 1; 
                      }
  
                      if (e.CurParams.Count () > 1)
                      {
                          Xf = System.Convert.ToInt32 (e.CurParams.Elements[1]) - 1;
                      }
  
                      this.CaretToRel (Yf, Xf);
                      break;
  
                  case "\x1b[J":
  
                      if (e.CurParams.Count () > 0) 
                      {
                          Param = System.Convert.ToInt32 (e.CurParams.Elements[0]); 
                      }
  
                      this.ClearDown (Param);
                      break;
  
                  case "\x1b[K":
  
                      if (e.CurParams.Count () > 0) 
                      {
                          Param = System.Convert.ToInt32 (e.CurParams.Elements[0]); 
                      }
  
                      this.ClearRight (Param);
                      break;
  
                  case "\x1b[L": // INSERT LINE
                      this.InsertLine (e.CurParams);
                      break;
  
                  case "\x1b[M": // DELETE LINE
                      this.DeleteLine (e.CurParams);
                      break;
  
                  case "\x1bN": // SS2 Single Shift (G2 -> GL)
                      this.CharAttribs.GS = this.G2;
                      break;
  
                  case "\x1bO": // SS3 Single Shift (G3 -> GL)
                      this.CharAttribs.GS = this.G3;
                      //System.Console.WriteLine ("SS3: GS = {0}", this.CharAttribs.GS);
                      break;
  
                  case "\x1b[m":
                      this.SetCharAttribs (e.CurParams);
                      break;
  
                  case "\x1b[?h":
                      this.SetqmhMode (e.CurParams);
                      break;
  
                  case "\x1b[?l":
                      this.SetqmlMode (e.CurParams);
                      break;
  
                  case "\x1b[c": // DA Device Attributes
  //                    this.DispatchMessage (this, "\x1b[?64;1;2;6;7;8;9c");
                      this.DispatchMessage (this, "\x1b[?6c");
                      //prntSome.printSome(" DA Device Attributes", "montdis", countmontdisrdb);
                      //countmontdisrdb++;
                      break;
  
                  case "\x1b[g":
                      this.ClearTabs (e.CurParams);
                      break;
  
                  case "\x1b[h":
                      this.SethMode (e.CurParams);
                      break;
  
                  case "\x1b[l":
                      this.SetlMode (e.CurParams);
                      break;
  
                  case "\x1b[r": // DECSTBM Set Top and Bottom Margins
                      this.SetScrollRegion (e.CurParams);
                      break;
  
                  case "\x1b[t": // DECSLPP Set Lines Per Page
  
                      if (e.CurParams.Count () > 0) 
                      {
                          Param = System.Convert.ToInt32 (e.CurParams.Elements[0]); 
                      }
  
                      if (Param > 0) this.SetSize (Param, this.Columns);
  
                      break;
  
                  case "\x1b" + "D": // IND
  
                      if (e.CurParams.Count () > 0) 
                      {
                          Param = System.Convert.ToInt32 (e.CurParams.Elements[0]); 
                      }
  
                      this.Index (Param);
                      break;
  
                  case "\x1b" + "E": // NEL
                      this.LineFeed ();
                      this.CarriageReturn ();
                      break;
  
                  case "\x1bH": // HTS
                      this.TabSet ();
                      break;
  
                  case "\x1bM": // RI
                      if (e.CurParams.Count () > 0) 
                      {
                          Param = System.Convert.ToInt32 (e.CurParams.Elements[0]); 
                      }
  
                      this.ReverseIndex (Param);
                      break;
  
                  default:
                      //System.Console.Write ("unsupported VT sequence {0} happened\n", e.CurSequence);
                      break;
                  
              }
  
              if (e.CurSequence.StartsWith ("\x1b("))
              {
                  this.SelectCharSet (ref this.G0.Set, e.CurSequence.Substring (2));
              }
              else if (e.CurSequence.StartsWith ("\x1b-") ||
                       e.CurSequence.StartsWith ("\x1b)"))
              {
                  this.SelectCharSet (ref this.G1.Set, e.CurSequence.Substring (2));
              }
              else if (e.CurSequence.StartsWith ("\x1b.") ||
                       e.CurSequence.StartsWith ("\x1b*"))
              {
                  this.SelectCharSet (ref this.G2.Set, e.CurSequence.Substring (2));
              }
              else if (e.CurSequence.StartsWith ("\x1b/") ||
                       e.CurSequence.StartsWith ("\x1b+"))
              {
                  this.SelectCharSet (ref this.G3.Set, e.CurSequence.Substring (2));
              }
          }
  
          private void SelectCharSet (ref uc_Chars.Sets CurTarget, System.String CurString)
          {
              //prntSome.printSome("SelectCharSet");
              switch (CurString)
              {
                  case "B":
                      CurTarget = uc_Chars.Sets.ASCII;
                      break;
  
                  case "%5":
                      CurTarget = uc_Chars.Sets.DECS;
                      break;
  
                  case "0":
                      CurTarget = uc_Chars.Sets.DECSG;
                      break;
  
                  case ">":
                      CurTarget = uc_Chars.Sets.DECTECH;
                      break;
  
                  case "<":
                      CurTarget = uc_Chars.Sets.DECSG;
                      break;
  
                  case "A":
                      if ((this.Modes.Flags & uc_Mode.National) == 0)
                      {
                          CurTarget = uc_Chars.Sets.ISOLatin1S;
                      }
                      else
                      {
                          CurTarget = uc_Chars.Sets.NRCUK;
                      }
                      break;
  
                  case "4":
  //                    CurTarget = uc_Chars.Sets.NRCDutch;
                      break;
  
                  case "5":
                      CurTarget = uc_Chars.Sets.NRCFinnish;
                      break;
  
                  case "R":
                      CurTarget = uc_Chars.Sets.NRCFrench;
                      break;
  
                  case "9":
                      CurTarget = uc_Chars.Sets.NRCFrenchCanadian;
                      break;
  
                  case "K":
                      CurTarget = uc_Chars.Sets.NRCGerman;
                      break;
  
                  case "Y":
                      CurTarget = uc_Chars.Sets.NRCItalian;
                      break;
  
                  case "6":
                      CurTarget = uc_Chars.Sets.NRCNorDanish;
                      break;
  
                  case "'":
                      CurTarget = uc_Chars.Sets.NRCNorDanish;
                      break;
  
                  case "%6":
                      CurTarget = uc_Chars.Sets.NRCPortuguese;
                      break;
  
                  case "Z":
                      CurTarget = uc_Chars.Sets.NRCSpanish;
                      break;
  
                  case "7":
                      CurTarget = uc_Chars.Sets.NRCSwedish;
                      break;
  
                  case "=":
                      CurTarget = uc_Chars.Sets.NRCSwiss;
                      break;
  
                  default:
                      break;
              }
          }
  
          private void SetqmhMode (uc_Params CurParams) // set mode for ESC?h command
          {
              //prntSome.printSome("SetqmhMode");
              System.Int32 OptInt = 0;
  
              foreach (System.String CurOption in CurParams.Elements)
              {
                  try 
                  {
                      OptInt = System.Convert.ToInt32 (CurOption);
                  }
                  catch (System.Exception CurException)
                  {
                      System.Console.WriteLine (CurException.Message);
                  }
  
                  switch (OptInt) 
                  {
                      case 1: // set cursor keys to application mode
                          this.Modes.Flags = this.Modes.Flags | uc_Mode.CursorAppln;
                          break;
  
                      case 2: // lock the keyboard
                          this.Modes.Flags = this.Modes.Flags | uc_Mode.Locked;
                          break;
  
                      case 3: // set terminal to 132 column mode
                          this.SetSize (this.Rows, 132);
                          break;
  
                      case 5: // Light Background Mode
                          this.Modes.Flags = this.Modes.Flags | uc_Mode.LightBackground;
                          this.RefreshEvent ();
                          break;
  
                      case 6: // Origin Mode Relative
                          this.Modes.Flags = this.Modes.Flags | uc_Mode.OriginRelative;
                          this.CaretToRel (0, 0);
                          break;
  
                      case 7: // Autowrap On
                          this.Modes.Flags = this.Modes.Flags | uc_Mode.AutoWrap;
                          break;
  
                      case 8: // AutoRepeat On
                          this.Modes.Flags = this.Modes.Flags | uc_Mode.Repeat;
                          break;
  
                      case 42: // DECNRCM Multinational Charset
                          this.Modes.Flags = this.Modes.Flags | uc_Mode.National;
                          break;
  
                      case 66: // Numeric Keypad Application Mode On
                          this.Modes.Flags = this.Modes.Flags | uc_Mode.KeypadAppln;
                          break;
  
                      default:
                          
                          break;
                  } 
              }
          }
  
          private void SetqmlMode (uc_Params CurParams) // set mode for ESC?l command
          {
              
              System.Int32 OptInt = 0;
  
              foreach (System.String CurOption in CurParams.Elements)
              {
                  try 
                  {
                      OptInt = System.Convert.ToInt32 (CurOption);
                  }
                  catch (System.Exception CurException)
                  {
                      System.Console.WriteLine (CurException.Message);
                  }
                  
  
                  switch (OptInt) 
                  {
                      case 1: // set cursor keys to normal cursor mode
                          this.Modes.Flags = this.Modes.Flags & ~uc_Mode.CursorAppln;
                          break;
  
                      case 2: // unlock the keyboard
                          this.Modes.Flags = this.Modes.Flags & ~uc_Mode.Locked;
                          break;
  
                      case 3: // set terminal to 80 column mode
                          this.SetSize (this.Rows, 80);
                          break;
  
                      case 5: // Dark Background Mode
                          this.Modes.Flags = this.Modes.Flags & ~uc_Mode.LightBackground;
                          this.RefreshEvent ();
                          break;
  
                      case 6: // Origin Mode Absolute
                          this.Modes.Flags = this.Modes.Flags & ~uc_Mode.OriginRelative;
                          this.CaretToAbs (0, 0);
                          break;
  
                      case 7: // Autowrap Off
                          this.Modes.Flags = this.Modes.Flags & ~uc_Mode.AutoWrap;
                          break;
  
                      case 8: // AutoRepeat Off
                          this.Modes.Flags = this.Modes.Flags & ~uc_Mode.Repeat;
                          break;
  
                      case 42: // DECNRCM National Charset
                          this.Modes.Flags = this.Modes.Flags & ~uc_Mode.National;
                          break;
  
                      case 66: // Numeric Keypad Application Mode On
                          this.Modes.Flags = this.Modes.Flags & ~uc_Mode.KeypadAppln;
                          break;
  
                      default:
                          break;
                  } 
              }
          }
  
          private void SethMode (uc_Params CurParams) // set mode for ESC?h command
          {
              //prntSome.printSome("SethMode");
              System.Int32 OptInt = 0;
  
              foreach (System.String CurOption in CurParams.Elements)
              {
                  try 
                  {
                      OptInt = System.Convert.ToInt32 (CurOption);
                  }
                  catch (System.Exception CurException)
                  {
                      System.Console.WriteLine (CurException.Message);
                  }
  
                  switch (OptInt) 
                  {
                      case 1: // set local echo off
                          this.Modes.Flags = this.Modes.Flags | uc_Mode.LocalEchoOff;
                          break;
  
                      default:
                          break;
                  } 
              }
          }
  
          private void SetlMode (uc_Params CurParams) // set mode for ESC?l command
          {
              //prntSome.printSome("SetlMode");
              System.Int32 OptInt = 0;
  
              foreach (System.String CurOption in CurParams.Elements)
              {
                  try 
                  {
                      OptInt = System.Convert.ToInt32 (CurOption);
                  }
                  catch (System.Exception CurException)
                  {
                      System.Console.WriteLine (CurException.Message);
                  }
  
                  switch (OptInt) 
                  {
                      case 1: // set LocalEcho on
                          this.Modes.Flags = this.Modes.Flags & ~uc_Mode.LocalEchoOff;
                          break;
  
                      default:
                          break;
                  } 
              }
          }
  
          private void SetScrollRegion (uc_Params CurParams)
          {
              //prntSome.printSome("SetScrollRegion");
  
gt:             if (CurParams.Count () > 0) 