/*->c.mzmodem */

/* collection of common routines and variables from zmodem progs */


#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#include <time.h>


#include "h.os"
#include "h.wimp"
#include "h.bbc"


#include "h.def"

#include "h.wos"
#include "h.timex"

#include "h.zmfix"


#include "h.mym"
#include "h.serial"

#include "h.ftpglue"


#include "h.zmodem"
#include "h.zmextra"




/*****************************************************************************/

int  AutoResume=0;
int  zmautodload;


unsigned Txwindow;      /* Control the size of the transmitted window */
unsigned Txwspac;       /* Spacing between zcrcq requests */
unsigned Txwcnt;        /* Counter used to space ack requests */
long Lrxpos;            /* Receiver's last reported offset */


int Filesleft;
long Totalleft;


FILE *in;

char * txbuf;


int Ascii=0;            /* Add CR's for brain damaged programs */
int blklen=128;         /* length of transmitted records */
/* int Optiong; */      /* Let it rip no wait for sector ACK's */
int Eofseen;            /* EOF seen on input set by zfilbuf */
int BEofseen;           /* EOF seen on input set by fooseek */
int Lfseen=0;


unsigned Rxbuflen = 16384;      /* Receiver's max buffer length */
int Tframlen = 0;       /* Override for tx frame length */
int blkopt=0;           /* Override value for zmodem blklen */
int Rxflags = 0;
long bytcnt;
int  Wantfcs32=TRUE;    /* want to send 32 bit FCS */
char Lzconv;            /* Local ZMODEM file conversion request */
char Lzmanag;           /* Local ZMODEM file management request */
int  Lskipnocor;
char Lztrans;

char zconv;             /* ZMODEM file conversion request */
char zmanag;            /* ZMODEM file management request */
char ztrans;            /* ZMODEM file transport request */


long Lastsync;          /* Last offset to which we got a ZRPOS */
int Beenhereb4;         /* How many times we've been ZRPOS'd same place */



FILE *fout;

int Lastrx;
int Firstsec;
int Eofseen;            /* indicates cpm eof (~Z) has been received */
int errors;


long Bytesleft;         /* number of bytes of incoming file left */
long Modtime;           /* Unix style mod time for incoming file */
int  Filemode;          /* Unix style mode for incoming file */



int Rxbinary=FALSE;     /* receive all files in bin mode */
int Rxascii=FALSE;      /* receive files in ascii (translate) mode */
int Thisbinary;         /* current file is to be received in bin mode */
int Blklen;             /* record length of received packets */


char * secbuf;


time_t timep[2];
char   Lzmanag;           /* Local file management request */
char   zconv;             /* ZMODEM file conversion request */
char   zmanag;            /* ZMODEM file management request */
char   ztrans;            /* ZMODEM file transport request */
int    Zctlesc;           /* Encode control characters */
int    Zrwindow = 1400;   /* RX window size (controls garbage count) */




int tryzhdrtype=ZRINIT; /* Header type to send corresponding to Last rx close */



/*****************************************************************************/

/*
 * Routine to calculate the free bytes on the current file system
 *  ~0 means many free bytes (unknown)
 */

long getfree(void)
{
 return(~0L);    /* many free bytes ... */
}

/* Local screen character display function */

void bttyout(int c)
{
 /* display character on terminal screen */
}




#ifdef ZMDEBUG


static FILE * zdbf=NULL;


void zdeb(char * format, ...)
{
 va_list args;
 char v[256];

 va_start(args, format);

 if(!zdbf)
 {
  zdbf=fopen("xx","wb");
  if(!zdbf) return;
 }

 vsprintf(v, format, args);

 fprintf(zdbf,"%s\n",v);

 va_end(args);
}


#endif





/*****************************************************************************/

/* attempt to rx files using zmodem */

void zmodemrx(void)
{
 char buff[1030];

 secbuf=buff;

 wcreceive();
}


/* attempt to tx files using zmodem */

void zmodemtx(void)
{
 int  bn;
 int  ret;
 char buff[1030];
 int  i;

 txbuf=buff;

 Txfcs32=Wantfcs32;

 ret=!ERROR;

 stohdr(0);
 zshhdr(ZRQINIT,Txhdr);

 for(i=0;i<txdocuments;i++)
 {
  bn=findviewsn(txsn);


  if((ret=wcs(bn))==ERROR)
  {
   schedaddattempt();
   break;
  }

  schedsentdoc();
  writetolog("{LOG11} %s {LOG12}\n",leaf(txdocname));
  txnexttarget();
 }

 sendsaybibi();
}

