home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0020 - 0029 / ibm0020-0029 / ibm0028.tar / ibm0028 / GRLF-C-1.ZIP / GCOMM / _ASCIIR.C next >
Encoding:
C/C++ Source or Header  |  1990-08-14  |  5.9 KB  |  201 lines

  1. /*
  2.  * _ASCIIR.C
  3.  *
  4.  * Contains:    _AsciiReceive()
  5.  *              _AsciiReceiveReadData()
  6.  *              _AsciiReceiveWaitForRxData()
  7.  *
  8.  * The Greenleaf Comm Library
  9.  *
  10.  * Copyright (C) 1989-90 Greenleaf Software Inc.  All Rights Reserved.
  11.  *
  12.  */
  13.  
  14. #include <stdio.h>
  15. #include "gf.h"
  16. #include "asiports.h"
  17. #include "xfer.h"
  18. #include "_xfer.h"
  19.  
  20. /*
  21.  * Local prototypes:
  22.  */
  23.  
  24. static int GF_CONV _AsciiReceiveReadData( XFER *ascii );
  25. static int GF_CONV _AsciiReceiveWaitForRxData( XFER *ascii );
  26.  
  27. /*
  28.  * int _AsciiReceive( XFER *ascii )
  29.  *
  30.  * ARGUMENTS
  31.  *
  32.  *  XFER *ascii:                       This is a pointer to an XFER block that
  33.  *                                     keeps track of how things are set up
  34.  *                                     for the file transfer.
  35.  *
  36.  * DESCRIPTION
  37.  *
  38.  * This routine is the private driver for performing an ascii file receive.
  39.  * The logic for the routine is fairly simple. After getting set up,
  40.  * the routine reads in, echoes, and stores characters as long
  41.  * as they are coming in.  While characters are not coming in, the timeout
  42.  * timer runs.  The only ways out of this private driver are with a fatal
  43.  * error, a keyboard abort, or a timeout.
  44.  * Be sure to note that there is no built-in handshaking going on here.
  45.  * If XON/XOFF handshaking is turned on for the port in question, it will
  46.  * work fine during the transfer.
  47.  *
  48.  * SIDE EFFECTS
  49.  *
  50.  * File may have been transfered.
  51.  *
  52.  * RETURNS
  53.  *
  54.  * The routine returns one of the defined XFER_RETURN codes defined
  55.  * in XFER.H.
  56.  *
  57.  * AUTHOR
  58.  *  Mark Nelson          28-Aug-1989  20:57:40.92
  59.  *
  60.  * MODIFICATIONS
  61.  *
  62.  */
  63. int GF_CONV _AsciiReceive( XFER *ascii )
  64. {
  65.  
  66.     ascii->sending = FALSE;
  67.     if ( !_XferInitialize( ascii ) )   /* The initialize routine sets up the */
  68.         return( FALSE );               /* the buffers and other odds and ends*/
  69.  
  70.     if ( !_XferOpenFile( ascii ) )     /* This routine opens up the file that*/
  71.         return( FALSE );               /* will be received.                  */
  72. /*
  73.  * This is the big loop where the file gets loaded.  In this case since
  74.  * the loop is a forever loop the only way out is a return statement.
  75.  * And you only get one of those when there is an error or a user abort.
  76.  */
  77.     for (;;) {
  78.         if ( !_AsciiReceiveReadData( ascii ) )
  79.             return( FALSE );
  80.         if ( !_AsciiReceiveWaitForRxData( ascii ) )
  81.             return( FALSE );
  82.     }
  83. }
  84.  
  85. /*
  86.  * static int _AsciiReceiveReadData( XFER *ascii )
  87.  *
  88.  * ARGUMENTS
  89.  *
  90.  *  XFER *ascii:                       This is a pointer to an XFER block that
  91.  *                                     keeps track of how things are set up
  92.  *                                     for the file transfer.
  93.  *
  94.  * DESCRIPTION
  95.  *
  96.  * This routine is a private support routine used by the Ascii Receive
  97.  * program. All it does is read in characters from the com port and store
  98.  * them. Once in a while it checks for keyboard abort codes.  It exits when
  99.  * the rx buffer is empty.  It can also exit when something goes wrong
  100.  * or the user aborts the whole deal.
  101.  *
  102.  * SIDE EFFECTS
  103.  *
  104.  *
  105.  * RETURNS
  106.  *
  107.  * The routine returns TRUE or FALSE.  TRUE means keep processing.  FALSE
  108.  * means time to quit.
  109.  *
  110.  * AUTHOR
  111.  *  Mark Nelson          29-Sep-1989  20:57:40.92
  112.  *
  113.  * MODIFICATIONS
  114.  *
  115.  */
  116.  
  117. static int GF_CONV _AsciiReceiveReadData( XFER *ascii )
  118. {
  119.     int c;
  120.  
  121.     while ( !isrxempty( ascii->port ) ) {
  122.         c=asigetc( ascii->port );
  123.         if ( c < ASSUCCESS ) {
  124.             ascii->return_status = XFER_RETURN_CANT_GET_CHAR;
  125.             _XferCleanup( ascii );
  126.             return( FALSE );
  127.         }
  128.         if ( ascii->x.ascii.received_character_printer != NULL )
  129.             ascii->x.ascii.received_character_printer( c );
  130.         ascii->byte_count++;
  131. /*
  132.  * If characters are coming in like crazy, I still need to check for the
  133.  * abort key once in a while.  This code here makes sure that I check for
  134.  * an abort key at least once every 256 characters.
  135.  */
  136.         if ( (ascii->byte_count & 0xff) == 0 )
  137.             if ( _XferAbortKeyPressed( ascii ) )
  138.                 return( FALSE );
  139. /*
  140.  * Here is where I store the character.  I do the stripping of line feeds
  141.  * here if the user asked for it.
  142.  */
  143.         if ( c != '\n' || !ascii->x.ascii.strip_lf )
  144.             if ( putc( c, ascii->file ) != c ) {
  145.                 ascii->return_status = XFER_RETURN_FILE_ERROR;
  146.                 _XferCleanup( ascii );
  147.                 return( FALSE );
  148.             }
  149.     }
  150.     return( TRUE );
  151. }
  152.  
  153. /*
  154.  * static int _AsciiReceiveWaitForRxData( XFER *ascii )
  155.  *
  156.  * ARGUMENTS
  157.  *
  158.  *  XFER *ascii:                       This is a pointer to an XFER block that
  159.  *                                     keeps track of how things are set up
  160.  *                                     for the file transfer.
  161.  *
  162.  * DESCRIPTION
  163.  *
  164.  * This routine is a private receiver used by the Ascii Receive program.
  165.  * Its job is wait for there to be stuff in the rx buffer.  When there is
  166.  * stuff ready to receive it returns.  It also can timeout waiting for
  167.  * data, and it can abort for the usual reasons.
  168.  *
  169.  * SIDE EFFECTS
  170.  *
  171.  *
  172.  * RETURNS
  173.  *
  174.  * The routine returns TRUE or FALSE.  TRUE means keep processing.  FALSE
  175.  * means time to quit.
  176.  *
  177.  * AUTHOR
  178.  *  Mark Nelson          29-Sep-1989  20:57:40.92
  179.  *
  180.  * MODIFICATIONS
  181.  *
  182.  */
  183. static int GF_CONV _AsciiReceiveWaitForRxData( XFER *ascii )
  184. {
  185.     int timeout_timer;
  186.  
  187.     timeout_timer = ASCII_RECEIVE_TIMEOUT*TICKS_PER_SECOND;
  188.     while ( isrxempty( ascii->port ) ) {
  189.         if ( _XferAbortKeyPressed( ascii ) )
  190.             return( FALSE );
  191.         timer( 1 );
  192.         timeout_timer--;
  193.         if ( isrxempty( ascii->port ) && timeout_timer <= 0 ) {
  194.             ascii->return_status = XFER_RETURN_ASCII_TIMEOUT;
  195.             _XferCleanup( ascii );
  196.             return( FALSE );
  197.         }
  198.     }
  199.     return( TRUE );
  200. }
  201.