
#pragma once

#include "other.h"

#include <String.h>
#include <stdio.h>
#include <stdarg.h>

#include <CursesW.h>

enum TerminalType
{
  _TTy,
  _VT100,
  _IBM3270
};


class VT100emulator
{
 protected:

  int VTArgn =0;
  int VTArg[10];

  enum VT100_code {
         c_NotCode = -30,
         c_InCode      ,
         c_UnkOp       ,
         c_ED          ,
         c_EL          ,
         c_CUP         ,
         c_DECSET      ,
         c_DECRST      ,
         c_DECPAM      ,
         c_SGR };

 private:
  // helper functions for filter_escapeCodes()
  bool         matched (int);
  bool         matched (char*);
  VT100_code   Was (VT100_code);
  VT100_code   WasUnk ();
  VT100_code   IsMore();

  bool      in_code =0;         // have seen ESC, and are inside code
  bool      in_code_num =0;     // parsing numeric argument

  const int CODEMaxSize = 10;
  int       code[CODEMaxSize];  // Record of parse.  See .cc file for rep.
  int       codeidx =0;

  void      encountered_unknown_code(); // Called when... Used for debugging.

  VT100_code  last =c_UnkOp;    // Used by send_last_code.

  char      buffer[2048];
  int       length = 0;
 public:
  static CursesWindow *userWindow;

  VT100_code    filter_escapeCodes(int);
  void          send_last_code();

  int IncrementalWrite(char* buf, int nchars);

  virtual int write (char* buf) { return write(buf,strlen(buf)); };
  virtual int write (String& s) { return write(s,s.length()); };
  virtual int   write            (char*buf, int nchars);
  virtual void  flush();
};


//--------------

#include "hardcopy.h"

//--------------

//
// VT100 customized to BARTON.
//  Makes BARTON 24 rather than 25 lines high, and filters out boldface.
//  Slides lines 2-25 upward.
//  (Line 2 is normally blank, being intended for system error messages.)
// 
class VT100_from_VT100barton : public VT100emulator
{
  bool  on_line_25;
 public:
  virtual int   write            (char*buf, int nchars);  
};
typedef VT100_from_VT100barton VT100_barton;

// Not used.
class VT100_filter : public VT100emulator
{
  const int   maxpop   =40;
  VT100_code passcodes[maxpop];
  int         passcodepop;

  enum { echo, parsed_echo, filter } mode;

 public:
                VT100_filter    () { mode = echo; };
  void          set_filter_mode (int m);
  void          set_pass_codes  (VT100_code *, int);
  virtual int   write           (char*buf,int nchars);
};
