recv: do; 

/*    This is an ISIS utility ptogram for use with a Remote Computer. */

/*    This utility will run on an ISIS cluster board which is connected
      to a remote computer rather than a dumb terminal.
*/


$nolist include(:f3:common.lit)
$nolist include(:f3:isis.ext)

Declare buffer(128)    byte;
Declare (actual, status, aftn) word;
Declare (i, J, checksum, character) byte;

/*    Read the remainder of the command line */
call read(1, .buffer, 128, .actual, .status);

/*    Is the requested ISIS file available */
j = 0;
do i = 0 to 2;     /* Skip "RECV <FILENAME> FROM" in the command word.
                   /* Need to get the file on the NDS-II system.
                   /* Don't need to check for syntax - it is already done
                   /* by the program on the remote computer.
                   */
    do while buffer(j) <> ' '; 
        j = j + 1;
    end;
    do while buffer(j) = ' ';
        j = j + 1;
    end;
end;
call open(.aftn, .buffer(J), 1, 0, .status);
if status <> 0 then do;
    call write(0,.(cr,lf,'  NDS-II file does not exist',cr,lf), 32, .status);
    call exit;
end;

/*    File is OK */
/*    Get the first buffer of information */
call read(aftn, .buffer, 128, .actual, .status);

do while actual <> 0;
/*    and send it */
    if actual <> 128 then do i = actual to 128;
        buffer(i-1) = ' ';
    end;
    call co(STX);
    checksum = 0;
    do i = 0 to 127;
        call co(buffer(i));
        checksum = checksum + buffer(i);
    end;
    call co(checksum);
    call co(ETX);

/*    Did the Remote Computer receive this OK */
    character = ci and 7FH;

    if character = EOT then call exit;        /* Remote error */
    if character = ACK then call read(aftn, .buffer, 128, .actual, .status);

/*    otherwise assume a transmission error and resend */
end;

/*    Arrive here when the complete file has been sent */
call close(aftn, .status);
call co(ETX);
call exit;

end recv;