/* File:     QLtoZ88_c
   Author:   Timothy Swenson

   Send files to the Z88 from the QL

   This program will send any file to a Z88.
   It has an option to translate all Line Feeds (LF)
   (QL End-Of-Line markers) to Carriage Return (CR)
   (Z88 End-Of-Line makers).

   At the end of each file it will send an End Of File
   escape command.  It will the send an End of Batch
   escape sequence. Since Z88 files store the filename
   with in the file (when transfered out of Import-
   Export) the Batch Recieve can be used to send a
   file without explicitly telling the Z88 the
   filename.

*/

#define  TRUE    1
#define  FALSE   0
/* #define  CR      13  defined in STDIO_H */
/* #define  LF      10  defined in STDIO_H */
#define  ESC     27

#include <stdio_h>

main() {
   char  c, file[30];

   int   i, convert, baud_rate, fd1, fd2;

   printf("      QL To Z88 \n");
   printf("        By Tim Swenson\n\n");
   printf("Enter Input File Name : \n");
   gets(file);

   fd1 = fopen(file,"r");
   if (fd1 == NULL)  {
      printf("Did not open file: %s",file);
      abort(1);
   }

   printf("\nConvert LF to CR? (Y/N)");
   getchar(c);
   convert = FALSE;    /* default */
   if ( c == 'Y' || c == 'y' )
       convert = TRUE;

   printf("\nSelect Baud Rate: \n");
   printf("     1 -   300\n");
   printf("     2 -  1200\n");
   printf("     3 -  2400\n");
   printf("     4 -  9600\n");
   printf("Default of 1200\n");
   getchar(c);
   baud_rate = 1200;    /* default */
   if ( c == '1' )  baud_rate = 300;
   if ( c == '2' )  baud_rate = 1200;
   if ( c == '3' )  baud_rate = 2400;
   if ( c == '4' )  baud_rate = 9600;

   fd2 = fopen("ser2ir","w");
   if (fd2 == NULL) {
      printf("Could not open Serial 2\n");
      abort(1);
   }
   baud(baud_rate);

   while (( c = getc(fd1)) != EOF) {
          if ( convert == TRUE && c == LF )
             putc(CR,fd2);
          else
             putc(c,fd2);

          /* Pause in sending */
          for ( i = 1; i < 500; i++)
                 ;
   }

/* Send ESC E to signal End of File */
/*    ( Z88 Protocal ) */
   putc(ESC,fd2);
   putc('E',fd2);

/* add another pause */
   for ( i = 1; i < 500; i++ )
           ;

/* Send ESC Z to signal End of Batch */
   putc(ESC,fd2);
   putc('Z',fd2);

   fclose(fd1);
   fclose(fd2);
}

