#include <stdio.h>
#include <dos.h>
#include <bios.h>
#include <cxldef.h>
#include <cxlvid.h>

#define PORT  0   /*  Port Assignements 0 = Com 1 etc... */
#define PORT1 1
#define PORT2 2
#define PORT3 3

unsigned int filesize();

void sport();
void send_file();
void rec_file();
void get_file_name();
void send_file_name();
void port_init();
void wait();

/************************************/
error_clear()
{
 delay (1500);
 gotoxy (18,23);
 printf ("                                          ");
}

/************************************/
main()
{
 hidecur();
 clrscr();
 printf ("                    Lights of American Data Control Systems\n\n");
 printf ("                               Local Area Network\n\n\n\n\n");
 printf ("                             Revision 1.00 Beta Test\n");
 printf ("                                 July, 17, 1991\n\n\n");
 printf ("                             Press ANY KEY to abort!\n");
 gotoxy (1,18);
 printf ("旼컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컫컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴커");
 gotoxy (1,19);
 printf ("� Date:                                � Time:                                 �");
 gotoxy (1,20);
 printf ("쳐컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컵컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴캑");
 gotoxy (1,21);
 printf ("� Sending:                             � Receiving:                            �");
 gotoxy (1,22);
 printf ("쳐컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컨컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴캑");
 gotoxy (1,23);
 printf ("� Error Messages:                                                              �");
 gotoxy (1,24);
 printf ("읕컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴컴켸");

 port_init(PORT1);
 do
 {
  gotoxy (9,19);
  printf ("%s",sysdate(0));
  gotoxy (48,19);
  printf ("%s",systime(0));
  gotoxy (12,21);
  printf ("                           ");
  gotoxy (53,21);
  printf ("                       ");
  if (check_stat(PORT1) & 256)
  {
   switch (rport(PORT1))
   {
    case 's': send_file(PORT1); break;
    case 'r': rec_file(PORT1); break;
   }
  }
 }
 while (!kbhit());
 clrscr();
}
/*************************/
void send_file(port)
int port;
{
 FILE *fp;
 char ch,fname[14];
 union
 {
  char c[2];
  unsigned int count;
 } cnt;
 sport(port,'.');

 get_file_name(fname,PORT1);
 if (!(fp=fopen(fname,"rb")))
 {
  gotoxy (19,23);
  printf ("Cannot open input file!");
  exit (1);
  error_clear();
 }
 if (rport(port) != '.')
 {
  gotoxy (19,23);
  printf ("Remote file failure\n");
  exit (1);
  error_clear();
 }
 gotoxy (12,21);
 printf ("%s\n",fname);
 cnt.count = filesize(fp);
 sport (port,cnt.c[0]);
 wait (port);

 sport (port,cnt.c[1]);
 do
 {
  ch = getc(fp);
  if (ferror(fp))
  {
   gotoxy (19,23);
   printf ("Error reading input file");
   error_clear();
   break;
  }
  if (!feof(fp))
  {
   wait (port);
   sport(port,ch);
  }
 }
 while (!feof(fp));
 wait (port);
 fclose(fp);
}

/********************************/
void rec_file(port)
int port;
{
 FILE *fp;
 char ch, fname[14];
 union
 {
  char c[2];
  unsigned int count;
 } cnt;
 sport (port,'.');

 get_file_name (fname,PORT1);

 gotoxy (53,21);
 printf ("%s\n",fname);
 /*remove (fname); */
 if (!(fp=fopen(fname,"wb")))
 {
  gotoxy (19,23);
  printf ("Cannot open output file\n");
  exit (1);
  error_clear();
 }
 sport (port,'.');
 cnt.c[0] = rport (port);
 sport (port,'.');
 cnt.c[1] = rport (port);
 sport (port,'.');

 for (; cnt.count; cnt.count --)
 {
  ch = rport (port);
  putc (ch,fp);
  if (ferror(fp))
  {
   gotoxy (19,23);
   printf ("Error writing file.\n");
   exit (1);
   error_clear();
  }
  sport (port,'.');
 }
 fclose(fp);
}

/***********************************/
unsigned int filesize(fp)
FILE *fp;
{
 unsigned long int i;
 i = 0;
 do
 {
  getc(fp);
  i++;
 }
 while (!feof(fp));
 rewind (fp);
 return i-1;
}

/***********************************/
void send_file_name (f,port)
char *f;
int port;
{
 do
 {
  sport (port,'?');
 }
 while (!kbhit() && !(check_stat(port) & 256));
 if (kbhit())
 {
  getch();
  exit (1);
 }
 wait (port);
 while (*f)
 {
  sport (port, *f++);
  wait (port);
 }
 sport (port,0);
}

/********************************/
void get_file_name (f,port)
char *f;
int port;
{
 while (rport(port) != '?') printf (".");
 sport (port,'.');
 while ((*f=rport (port)))
 {
  if (*f!='?')
  {
   f++;
   sport (port, '.');
  }
 }
 sport (port,'.');
}

/********************************/
void wait (port)
int port;
{
 if (rport (port) !='.')
 {
  gotoxy (19,23);
  printf ("Communication Error\n");
  exit (1);
  error_clear();
 }
}

/*********************************/
void sport (port,c)
int port;
char c;
{
 union REGS r;
 r.x.dx = port;
 r.h.al = c;
 r.h.ah = 1;
 int86 (0x14,&r,&r);
 if (r.h.ah & 128)
 {
  gotoxy (19,23);
  printf ("Send error detected in serial port");
  exit (1);
  error_clear();
 }
}

/*************************************/
rport (port)
int port;
{
 union REGS r;
 while (!(check_stat (port) & 256))
 if (kbhit())
 {
  getch();
  exit (1);
 }
 r.x.dx = port;
 r.h.ah = 2;
 int86 (0x14,&r,&r);
 if (r.h.ah & 128)
 {
  gotoxy (19,23);
  printf ("Read error detected in serial port.\n");
  error_clear();
 }
 return (r.h.al);
}

/***********************************/
check_stat (port)
int port;
{
 union REGS r;
 r.x.dx = port;
 r.h.ah = 3;
 int86 (0x14,&r,&r);
 return r.x.ax;
}

/**************************************/
void port_init (port)
int port;
{
 union REGS r;
 r.x.dx = port;
 r.h.al = 0;
 r.h.al = 251;
 int86 (0x14,&r,&r);
}