/*
 * KERMIT09 - UniFLEX version header file
 */

#include <stdio.h>
#include <ctype.h>

#define VERSION      "V 1.5"

/* Also modify VERSION in ufkerm.c !! */

#define IDENT        "UniFLEX KERMIT 6809 version "
#define DEFPROMPT    "UF-Kermit>"
#define INITFILE     "kermit.ini"
#define INITFILE1    "/usr/kermit.ini"
#define HELPDIR      "/gen/kermit"
#define PRIVTASK     "/bin/kermit_support"

/* Version of protocol between main task and support task */

#define PROTOCOL_VERSION  2

/* Symbol Definitions */

#define SOH          1                    /* Start of header */
#define BELL         7                    /* ASCII Bell */
#define BS           8                    /* ASCII Backspace */
#define TAB          9                    /* ASCII Tab */
#define LF           10                   /* ASCII Linefeed */
#define CR           13                   /* ASCII Carriage Return */
#define ESC          27                   /* ASCII Escape */
#define SP           32                   /* ASCII space */
#define DEL          127                  /* Delete (rubout) */
#define XON          17                   /* Start output */
#define XOFF         19                   /* Stop output */
#define ESCCHR       '^'                  /* Default escape char for CONNECT */
#define PANIC        '\\' - 64            /* Panic abort exit */
#define TOGDBG       'D' - 64             /* Toggle debug on/off */
#define ABORTX       'X' - 64             /* Ctrl-X to abort transfer */
#define ABORTZ       'Z' - 64             /* Ctrl-Z to abort transfer */
#define COMMENT      '!'                  /* Command line comment character */

#define TRUE         1
#define FALSE        0
#define ERROR        (-1)
#define TYPERROR     (-2)
#define IN           0
#define OUT          1
#define SEND         0
#define RECEIVE      1
#define READ         0
#define WRITE        1
#define BIG_SIZE     2048 + 8             /* Maximum = (95*94+94+8) */
#define SMALL_SIZE   (94+5)

#define TABSIZE      8                    /* Tabstop every eight places */

#define I_MYPACKSTART      SOH            /* Start of packet character */
#define I_MAXPACKSIZ       94             /* Maximum packet size */
#define I_MYPACKSIZE       80             /* Default value if no 'S' packet */
#define I_MAXTRY           10             /* Times to retry a packet */
#define I_MYQUOTE          '#'            /* Quote character I will use */
#define I_MYPAD            0              /* Number of padding char's I need */
#define I_MYPCHAR          0              /* Padding character I need (NULL) */
#define I_MYEOL            '\r'           /* End-Of-Line character I need */
#define I_MYTIME           5              /* My timeout in seconds */
#define I_MYRPTQUOTE       '~'            /* Repeat quote character */
#define I_MYEIGHTQUOTE     '&'            /* Eight_bit quote character */
#define I_BLOCKCHECKTYPE   1              /* Initial checksum type */
#define I_SEND_DELAY       5              /* Delay time before sending */
#define I_DEFLONGSIZE      500            /* Default size for long packets */

#define TIMEOUT      1            /* Internal codes */
#define REMOTE      -1

/*
  UniFLEX kermit error definitions
*/

#define ER_POPNERR      0       /* Communication port open error */
#define ER_NOPARAM      1       /* No parameter specified */
#define ER_NOMEM        2       /* No memory available */
#define ER_NOLINE       3       /* No line to connect to */
#define ER_LOGWRTER     4       /* Log file write error */
#define ER_DIROPN       5       /* Directory open error */
#define ER_NOHELP       6       /* No help available */
#define ER_NOTOPIC      7       /* No such topic */
#define ER_AMBIG        8       /* Ambiguous command */
#define ER_UNKNOWN      9       /* Unknown command */
#define ER_ONENAME      10      /* Only one name allowed */
#define ER_FSPCREQ      11      /* Filespec required */
#define ER_NOFILSEL     12      /* No files selected */
#define ER_ILLREMOT     13      /* Not allowed if remote */
#define ER_AMBIGSET     14      /* Ambiguous 'set' command */
#define ER_UNKNWSET     15      /* Unknown 'set' command */
#define ER_FILEOPEN     16      /* File already open */
#define ER_OPENERR      17      /* File open error */
#define ER_INVDEV       18      /* Invalid device or no permission */
#define ER_NOREMBD      19      /* Can't set baudrate for remote line */
#define ER_NOBAUD       20      /* Unable to set baudrate */
#define ER_INVBAUD      21      /* Invalid baudrate specified */
#define ER_ILLVALUE     22      /* Illegal value specified */
#define ER_CHRINUSE     23      /* Character already in use */
#define ER_INVLENG      24      /* Invalid length specified */
#define ER_VALOUTRN     25      /* Value out of range */
#define ER_EOLDIF       26      /* Eol must be different of start of packet */
#define ER_STRTDIF      27      /* Start of packet must be different of eol */
#define ER_INVCHK       28      /* Invalid block check type */
#define ER_INVCHAR      29      /* Invalid character */
#define ER_AMBIGSHW     30      /* Ambiguous 'show' command */
#define ER_UNKNWSHW     31      /* Unknown 'show' command */
#define ER_GETPWDID     32      /* Get password id error */
#define ER_INVDIR       33      /* Invalid directory specified */
#define ER_SYNERR       34      /* Syntax error */
#define ER_PRIVSERVER   35      /* Privileged support task error */
#define ER_PRIVPROT     36      /* Invalid support task procotol */
#define ER_PERMS        37      /* Error setting file permissions */
#define ER_SPAWN        38      /* Can't spawn 'shell' */

/* Internal error packet error definitions */

#define PER_ABORTED     0       /* Aborted command */
#define PER_UNIMPL      1       /* Unimplemented server function */
#define PER_PACKET      2       /* Packet error */
#define PER_DEVFULL     3       /* Device full */
#define PER_WRITE       4       /* File write error */
#define PER_CREATE      5       /* File create error */
#define PER_OPEN        6       /* File open error */

#define ERR_PARAM       4       /* Errors > ERR_PARAM have parameters */

/* Macro Definitions */

/*
 * tochar: converts a control character to a printable one by adding a space.
 *
 * unchar: undoes tochar.
 *
 * ctl:    converts between control characters and printable characters by
 *         toggling the control bit (ie. ^A becomes A and A becomes ^A).
 */
#define tochar(ch)   ((ch) + ' ')
#define unchar(ch)   ((ch) - ' ')
#define ctl(ch)      ((ch) ^ 64 )

/*
  pread: read from pipe
  pwrite: write to pipe
*/
#define pread(adr,cnt)  read(pd1[READ],(adr),(cnt))
#define pwrite(adr,cnt) write(pd2[WRITE],(adr),(cnt))

/* Global Variables */

/* Structure used by command parser */

typedef struct table_point
{
   char *string;                          /* Command name */
   int funcadr;                           /* Function address */
} TABLE;

int   send_delay,             /* Start sending delay time */
      ttyfd,                  /* I/O port address */
      speed,                  /* line speed */
      tabsleft,               /* Tab counter */
      size,                   /* Size of present data */
      spsiz,                  /* Maximum send packet size */
      maxpacksiz,             /* Maximum packet size */
      nak_sent,               /* nak's sent */
      pack_sent,              /* packets sent */
      nak_rec,                /* nak's received */
      pack_rec,               /* packets received */
      data_rate,              /* effective data rate */
      t_nak_sent,             /* total naks sent */
      t_pack_sent,            /* total packets sent */
      t_nak_rec,              /* total nak's received */
      t_pack_rec,             /* total packets received */
      t_data_rate,            /* total effective data rate */
      pd1[2],                 /* pipe decriptors for support task */
      pd2[2];

long int chr_sent,            /* characters sent */
         dchr_sent,           /* data characters sent */
         chr_rec,             /* characters received */
         dchr_rec,            /* data characters received */
         t_chr_sent,          /* total characters sent */
         t_dchr_sent,         /* total data characters sent */
         t_chr_rec,           /* total characters received */
         t_dchr_rec,          /* total data characters received */
         transmit_chr_count,  /* Character count for data size calculation */
         file_size;           /* Size of current file in bytes */

char  state,                  /* Present state of the automaton */
      padchar,                /* Padding character to send */
      eol,                    /* End-Of-Line character to send */
      escchr,                 /* Connect command escape character */
      quote,                  /* Quote character in incoming data */
      block_check_type,       /* Checksum type */
      eight_quote,            /* eight_bit quote character */
      repeat_quote,           /* repeat quote character */
      timint,                 /* Timeout for foreign host on sends */
      pad,                    /* How much padding to send */
      allowattr,              /* allow or disallow attribute packets */
      attribute,              /* TRUE if attribute packets are used */
      n,                      /* Packet number */
      numtry,                 /* Times this packet retried */
      oldtry,                 /* Times previous packet retried */
      image,                  /* TRUE means 8-bit mode selected with 'set' */
      binfil,                 /* 8-bit mode at runtime with attr packet */
      warning,                /* File conflict warning flag */
      mapping,                /* Filename case mapping flag */
      auto_recover,           /* Auto recovery with extended packet length */
      debug,                  /* indicates debugging output done (0=none) */
      fulldup,                /* TRUE means full duples in connect */
      logfileopen,            /* TRUE means log file open */
      logging,                /* TRUE means logging active */
      dstop,                  /* stop character for remote */
      dstart,                 /* start character for remote */
      filecount,              /* Number of files left to send */
      filenum,                /* current filenumber */
      config,                 /* initialize value */
      numprm,                 /* number of command parameters */
      aborted,                /* TRUE means we're aborted */
      port_open,              /* TRUE means port is open */
      save_file,              /* keep/discard file on abort */
      sflg,                   /* send active flag */
      rflg,                   /* receive active flag */
      cflg,                   /* connect active flag */
      *filnam,                /* Current file name */
      call_baud,              /* Flag if baudrate should be set */
      prvsetdone,             /* privileged task setup done */
      maskflag,               /* mask for RTS and DCD mask option */
      screen,                 /* screen setup can be used */
      nooutput,               /* no output if called with options */
      remote,                 /* Remote line in use */
      bg,                     /* Running in the background */
      **params,               /* parameter list */
      prompt[21],             /* prompt string */
      logfile[50],            /* filename for logfile */
      rec_filnam[128],        /* forced receive filename */
      *recpkt,                /* Receive packet buffer pointer */
      *sndpkt,                /* Transmit packet buffer pointer */
      dbgfname[50],           /* Filename for debug file */
      tty_descr[20],          /* Terminal line in use */
      mypackstart,            /* Start of packet character */
      maxtry,                 /* Times to retry a packet */
      myquote,                /* Quote character I will use */
      mypad,                  /* Number of padding characters I will need */
      mypchar,                /* Padding character I need (NULL) */
      myeol,                  /* End-Of-Line character I need */
      mytime,                 /* Seconds after which I should be timed out */
      myrptquote,             /* Repeat quote character */
      myeightquote,           /* Eight_bit quote character */
      myblock_check_type;     /* My checksum type */

FILE  *fp,                    /* File pointer for current disk file */
      *lfp,                   /* File pointer for log file */
      *dbgfil;                /* File pointer for debug file output */

long  time(),
      get_free(),
      start_time,
      end_time;
