home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 300.lha / xprzmodem.library_v2.0 / receive.c < prev    next >
Encoding:
C/C++ Source or Header  |  1980-12-01  |  14.9 KB  |  477 lines

  1. /*  Receive.c:  File reception routines for xprzmodem.library;
  2.     Version 2.0, 28 October 1989, by Rick Huebner.
  3.     Based closely on Chuck Forsberg's rz.c example ZModem code,
  4.     but too pervasively modified to even think of detailing the changes.
  5.     Released to the Public Domain; do as you like with this code.  */
  6.  
  7.  
  8. #include <proto/all.h>
  9. #include <exec/types.h>
  10. #include <ctype.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include "xproto.h"
  15. #include "zmodem.h"
  16. #include "xprzmodem.h"
  17.  
  18. #ifdef DEBUGLOG
  19. extern void *DebugLog;
  20. #endif
  21.  
  22.  
  23.  
  24. /* Main file reception routine; called by comm program */
  25. long __saveds XProtocolReceive(struct XPR_IO *io) {
  26.   struct SetupVars *sv;
  27.   struct Vars *v;
  28.   UBYTE err = FALSE;
  29.  
  30.   /* Perform common setup and initializations */
  31.   if (!(v = setup(io))) return XPRS_FAILURE;
  32.   v->Tryzhdrtype = ZRINIT;
  33.   v->Rxtimeout = 100;
  34.  
  35.   sv = v->io.xpr_data;
  36.   if (sv->bufpos) {
  37.     v->Modemchar = v->Modembuf;
  38.     if (sv->buflen > sizeof(v->Modembuf)) sv->buflen = sizeof(v->Modembuf);
  39.     memcpy(v->Modembuf,sv->bufpos,sv->buflen);
  40.     v->Modemcount = sv->buflen;
  41.   }
  42.  
  43.   /* Transfer the files */  
  44.   if (rcvbatch(v) == ERROR) {
  45.     upderr(v,"Download cancelled or timed out");
  46.     err = TRUE;
  47.   } else updmsg(v,"Done.");
  48.  
  49.   /* Clean up and return */
  50.   if (v->io.xpr_setserial) (*v->io.xpr_setserial)(v->Oldstatus);
  51.   FreeMem(v->Filebuf,v->Filebufmax);
  52.   FreeMem(v,(long)sizeof(struct Vars));
  53.  
  54. #ifdef DEBUGLOG
  55.   if (DebugLog) {
  56.     (*v->io.xpr_fclose)(DebugLog);
  57.     DebugLog = NULL;
  58.   }
  59. #endif
  60.  
  61.   return (err) ? XPRS_FAILURE : XPRS_SUCCESS;
  62. }
  63.  
  64.  
  65.  
  66. /* Start the batch transfer */
  67. short rcvbatch(struct Vars *v) {
  68.   switch (tryz(v)) {
  69.     case ZCOMPL:
  70.       return OK;
  71.     case ZFILE:
  72.       if (rzfiles(v) == OK) return OK;
  73.   }
  74.   canit(v);
  75.   return ERROR;
  76. }
  77.  
  78.  
  79.  
  80. /* Negotiate with sender to start a file transfer */
  81. short tryz(struct Vars *v) {
  82.   short n, errors = 0;
  83.  
  84.   for (n=10; --n>=0; ) {
  85.     /* Set max frame length and capability flags */
  86.     stohdr(v,(long)v->Tframlen);
  87.     v->Txhdr[ZF0] = CANFDX | CANOVIO;
  88.     zshhdr(v,v->Tryzhdrtype);
  89. again:
  90.     switch (zgethdr(v)) {
  91.       case ZFILE:    /* File name and info packet */
  92.         v->Zconv  = v->Rxhdr[ZF0];  /* Suggested text mode; ZCNL = text, ZCBIN = binary, 0 = don't know */
  93.         v->Zmanag = v->Rxhdr[ZF1];  /* Suggested file management mode */
  94.         v->Ztrans = v->Rxhdr[ZF2];  /* Suggested file transport mode */
  95.         v->Tryzhdrtype = ZRINIT;
  96.         if (zrdata(v,v->Pktbuf,KSIZE) == GOTCRCW) return ZFILE;
  97.         zshhdr(v,ZNAK);   /* Packet mangled, ask for retry */
  98.         goto again;
  99.       case ZSINIT:   /* Special attention-grabbing string to use to interrupt sender */
  100.         if (zrdata(v,v->Attn,ZATTNLEN) == GOTCRCW) zshhdr(v,ZACK);
  101.         else zshhdr(v,ZNAK);
  102.         goto again;
  103.       case ZFREECNT: /* Sender wants to know how much room we've got */
  104.         stohdr(v,getfree());
  105.         zshhdr(v,ZACK);
  106.         goto again;
  107.       case ZCOMMAND: /* Sender wants us to do remote commands, but we don't do requests */
  108.         if (zrdata(v,v->Pktbuf,KSIZE) == GOTCRCW) {
  109.           sprintf(v->Msgbuf,"Ignoring command: %s",v->Pktbuf);
  110.           upderr(v,v->Msgbuf); /* Ignore and report all uploaded commands */
  111.           stohdr(v,0L);        /* whilst telling sender they worked; */
  112.           do zshhdr(v,ZCOMPL); /* paranoia can be good for you... */
  113.           while (++errors < 10 && zgethdr(v) != ZFIN);
  114.           ackbibi(v);
  115.           return ZCOMPL;
  116.         } else zshhdr(v,ZNAK);
  117.         goto again;
  118.       case ZCOMPL:
  119.         goto again;
  120.       case ZFIN:    /* Sender has ended batch */
  121.         ackbibi(v);
  122.         return ZCOMPL;
  123.       case ZCAN:
  124.       case RCDO:
  125.         return ERROR;
  126.     }
  127.   }
  128.   return ERROR;
  129. }
  130.  
  131.  
  132.  
  133. /* Receive a batch of files */
  134. short rzfiles(struct Vars *v) {
  135.   struct SetupVars *sv;
  136.   short c;
  137.  
  138.   /* Keep receiving files until end of batch or error */
  139.   while (TRUE) {
  140.     switch (c = rzfile(v)) {
  141.       case ZEOF:
  142.       case ZSKIP:
  143.         switch (tryz(v)) {
  144.           case ZCOMPL:
  145.             return OK;
  146.           default:
  147.             return ERROR;
  148.           case ZFILE:
  149.             break;
  150.         }
  151.         break;
  152.       default:
  153.         bfclose(v);
  154.         sv = v->io.xpr_data;
  155.         if (*sv->option_k == 'N' && v->io.xpr_extension >= 2 && v->io.xpr_unlink) {
  156.           updmsg(v,"Deleting partially received file");
  157.           (*v->io.xpr_unlink)(v->Filename);
  158.         } else updmsg(v,"Keeping partially received file");
  159.         return c;
  160.     }
  161.   }
  162. }
  163.  
  164.  
  165.  
  166. /* Receive one file; file name packet already read into Pktbuf by tryz() */
  167. short rzfile(struct Vars *v) {
  168.   short c, n;
  169.  
  170.   /* Process file name packet; either open file and prepare to receive,
  171.      or tell us to skip this one. */
  172.   if (procheader(v) == ERROR) return v->Tryzhdrtype = ZSKIP;
  173.  
  174.   n = 10;
  175.   v->Rxbytes = v->Strtpos;
  176.   v->Eofseen = FALSE;
  177.  
  178.   /* Receive ZDATA frames until finished */
  179.   while (TRUE) {
  180.     stohdr(v,v->Rxbytes);      /* Tell sender where to start frame */
  181.     zshhdr(v,ZRPOS);
  182. nxthdr:
  183.     switch (c = zgethdr(v)) {  /* Wait for frame header */
  184.       default:
  185. #ifdef DEBUGLOG
  186.         sprintf(v->Msgbuf,"rzfile: zgethdr returned %d\n",c);
  187.         dlog(v,v->Msgbuf);
  188. #endif
  189.         return ERROR;
  190.       case ZNAK:
  191.       case TIMEOUT:
  192.         if (--n < 0) return ERROR;
  193. #ifdef DEBUGLOG
  194.         dlog(v,"rzfile: zgethdr NAK/Timeout\n");
  195. #endif
  196.         v->xpru.xpru_updatemask = XPRU_ERRORMSG | XPRU_TIMEOUTS;
  197.         sprintf(strchr(v->Msgbuf,'\0'),"@ %ld; %d retries left",v->Rxbytes,n);
  198.         v->xpru.xpru_errormsg = (char *)v->Msgbuf;
  199.         ++v->xpru.xpru_timeouts;
  200.         (*v->io.xpr_update)(&v->xpru);
  201.         continue;
  202.       case ZFILE:     /* Sender didn't see our ZRPOS yet; try again */
  203.         zrdata(v,v->Pktbuf,KSIZE);  /* Read and discard redundant filename packet */
  204.         continue;
  205.       case ZEOF:      /* End of file data */
  206.         if (v->Rxpos != v->Rxbytes) {  /* We aren't in sync; go back */
  207.           sprintf(v->Msgbuf,"Bad EOF; here=%ld, there=%ld",v->Rxbytes,v->Rxpos);
  208.           upderr(v,v->Msgbuf);
  209.           continue;
  210.         }
  211.         bfclose(v);  /* All done; close file */
  212. #ifdef DEBUGLOG
  213.         dlog(v,"rzfile: EOF\n");
  214. #endif
  215.         updmsg(v,"EOF received; checking for next file");
  216.         return c;
  217.       case ERROR:     /* Too much garbage while waiting for frame header */
  218.         if ( --n < 0) return ERROR;
  219. #ifdef DEBUGLOG
  220.         dlog(v,"rzfile: zgethdr garbage overflow\n");
  221. #endif
  222.         v->xpru.xpru_updatemask = XPRU_ERRORMSG | XPRU_ERRORS;
  223.         sprintf(strchr(v->Msgbuf,'\0'),"@ %ld; %d retries left",v->Rxbytes,n);
  224.         v->xpru.xpru_errormsg = (char *)v->Msgbuf;
  225.         ++v->xpru.xpru_errors;
  226.         (*v->io.xpr_update)(&v->xpru);
  227.         zmputs(v,v->Attn);
  228.         continue;
  229.       case ZDATA:     /* More file data packets forthcoming */
  230.         if (v->Rxpos != v->Rxbytes) {   /* We aren't in sync; go back */
  231.           if ( --n < 0) return ERROR;
  232.           v->xpru.xpru_updatemask = XPRU_ERRORMSG | XPRU_ERRORS;
  233.           sprintf(v->Msgbuf,"Data at bad position; here=%ld, there=%ld",v->Rxbytes,v->Rxpos);
  234.           v->xpru.xpru_errormsg = (char *)v->Msgbuf;
  235.           ++v->xpru.xpru_errors;
  236.           (*v->io.xpr_update)(&v->xpru);
  237.           zmputs(v,v->Attn);
  238.           continue;
  239.         }
  240.         /* Receive file data packet(s) */
  241. moredata:
  242.         /* Give comm program its timeslice if it needs one */
  243.         if (v->io.xpr_chkmisc) (*v->io.xpr_chkmisc)();
  244.         /* Check for abort from comm program */
  245.         if (v->io.xpr_chkabort && (*v->io.xpr_chkabort)()) goto aborted;
  246.         switch (c = zrdata(v,v->Pktbuf,KSIZE)) {
  247.           case ZCAN:
  248.           case RCDO:
  249. aborted:
  250. #ifdef DEBUGLOG
  251.             dlog(v,"rzfile: zrdata returned CAN\n");
  252. #endif
  253.             upderr(v,"Transfer cancelled");
  254.             return ERROR;
  255.           case ERROR:     /* CRC error or packet too long */
  256.             if ( --n < 0) return ERROR;
  257. #ifdef DEBUGLOG
  258.             dlog(v,"rzfile: zrdata returned error\n");
  259. #endif
  260.             v->xpru.xpru_updatemask = XPRU_ERRORMSG | XPRU_ERRORS;
  261.             sprintf(strchr(v->Msgbuf,'\0'),"@ %ld; %d retries left",v->Rxbytes,n);
  262.             v->xpru.xpru_errormsg = (char *)v->Msgbuf;
  263.             ++v->xpru.xpru_errors;
  264.             (*v->io.xpr_update)(&v->xpru);
  265. #ifdef DEBUGLOG
  266.             dlog(v,v->Msgbuf);
  267.             dlog(v,"\n");
  268. #endif
  269.             zmputs(v,v->Attn);
  270.             continue;
  271.           case TIMEOUT:
  272.             if ( --n < 0) return ERROR;
  273. #ifdef DEBUGLOG
  274.             dlog(v,"rzfile: zrdata returned timeout\n");
  275. #endif
  276.             v->xpru.xpru_updatemask = XPRU_ERRORMSG | XPRU_TIMEOUTS;
  277.             sprintf(strchr(v->Msgbuf,'\0'),"@ %ld; %d retries left",v->Rxbytes,n);
  278.             v->xpru.xpru_errormsg = (char *)v->Msgbuf;
  279.             ++v->xpru.xpru_timeouts;
  280.             (*v->io.xpr_update)(&v->xpru);
  281. #ifdef DEBUGLOG
  282.             dlog(v,v->Msgbuf);
  283.             dlog(v,"\n");
  284. #endif
  285.             continue;
  286.           case GOTCRCW:   /* Sender says it's waiting for an ACK */
  287.             n = 10;
  288.             if (putsec(v) == ERROR) return ERROR;
  289.             stohdr(v,v->Rxbytes);
  290.             zshhdr(v,ZACK);
  291.             goto nxthdr;
  292.           case GOTCRCQ:   /* Sender says it's not waiting, but ACK anyway (rarely used) */
  293.             n = 10;
  294.             if (putsec(v) == ERROR) return ERROR;
  295.             stohdr(v,v->Rxbytes);
  296.             zshhdr(v,ZACK);
  297.             goto moredata;
  298.           case GOTCRCG:   /* Sender says keep receiving, there's more coming */
  299.             n = 10;
  300.             if (putsec(v) == ERROR) return ERROR;
  301.             goto moredata;
  302.           case GOTCRCE:   /* Sender says this is the last packet */
  303.             n = 10;
  304.             if (putsec(v) == ERROR) return ERROR;
  305.             goto nxthdr;
  306.         }
  307.     }
  308.   }
  309. }
  310.  
  311.  
  312.  
  313. /* Process file name & info packet; either open file and prepare to receive,
  314.    or return ERROR if we should skip this one for some reason */
  315. short procheader(struct Vars *v) {
  316.   struct SetupVars *sv;
  317.   UBYTE *p, *openmode;
  318.  
  319.   openmode = "w";
  320.   v->Strtpos = 0;
  321.  
  322.   /* Extract expected filesize from file info packet, if given */
  323.   v->Fsize = -1;
  324.   p = strchr(v->Pktbuf,'\0') + 1;
  325.   if (*p) v->Fsize = atol(p);
  326.   /* Make sure we have room for file; skip it if not.
  327.      Commented out for now, since getfree() isn't implemented yet.
  328.   if (v->Fsize > getfree()) {
  329.     sprintf(v->Msgbuf,"Insufficient disk space; need %ld bytes, have %ld",v->Fsize,getfree());
  330.     upderr(v,v->Msgbuf);
  331.     v->Noroom = TRUE;
  332.     return ERROR;
  333.   } */
  334.  
  335.   /* If option RY set, use full received file path */
  336.   sv = v->io.xpr_data;
  337.   if (*sv->option_r == 'Y') strcpy(v->Filename,v->Pktbuf);
  338.   else {
  339.     /* else use the default directory path specified in the setup options */
  340.     strcpy(v->Filename,sv->option_p);
  341.     p = v->Filename + strlen(v->Filename) - 1;
  342.     if (p >= v->Filename && *p != '/' && *p != ':') *++p = '/';
  343.     *++p = '\0';
  344.     /* Append the filename from the file info packet; ignore anything before
  345.        last /, \, or : in filename (received directory path is ignored) */
  346.     p = strchr(v->Pktbuf,'\0'); /* start at end and scan back to start of name */
  347.     while (p >= v->Pktbuf && *p != '/' && *p != '\\' && *p != ':') --p;
  348.     strcat(v->Filename,++p);
  349.   }
  350.  
  351.   /* Display name of file being received for user */
  352.   v->xpru.xpru_updatemask = XPRU_FILENAME;
  353.   v->xpru.xpru_filename = (char *)v->Filename;
  354.   (*v->io.xpr_update)(&v->xpru);
  355.  
  356.   /* If a file with this name already exists, handle in accordance with O option */
  357.   if (exist(v)) {
  358.     switch (*sv->option_o) {
  359.       case 'N': /* Don't overwrite; change name to prevent collision */
  360.         while (exist(v)) strcat(v->Filename,".dup");
  361.         /* Update filename display to show new name */
  362.         (*v->io.xpr_update)(&v->xpru);
  363.         break;
  364.       case 'R': /* Resume transfer from current end of file */
  365.         openmode = "a";
  366.         v->Strtpos = (*v->io.xpr_finfo)(v->Filename,1L);
  367.         break;
  368.       case 'S': /* Skip it */
  369.         upderr(v,"File already exists; skipping");
  370.         return ERROR;
  371.       /* Else 'Y', go ahead and overwrite it (openmode = w) */
  372.     }
  373.   }
  374.  
  375.   /* Set text/binary mode according to options before opening file */
  376.   set_textmode(v);
  377.  
  378.   /* Figure out file translation mode to use; either binary (verbatim
  379.      transfer) or ASCII (perform end-of-line conversions).  If user has
  380.      specified a mode (TY or TN), that's what we use.  If user says use
  381.      sender's suggestion (T?), set mode according to Zconv flag.  If neither
  382.      side specifies, default to binary mode. */
  383.   v->Thisbinary =  v->Rxbinary || !v->Rxascii;
  384.   if (!v->Rxbinary && v->Zconv == ZCNL) v->Thisbinary = FALSE;
  385.   if (!v->Rxascii && v->Zconv == ZCBIN) v->Thisbinary = TRUE;
  386.  
  387.   /* Open the file (finally) */
  388.   if (!(v->File = bfopen(v,openmode))) {
  389.     ++v->Errcnt;
  390.     upderr(v,"Can't open file; skipping");
  391.     return ERROR;
  392.   }
  393.   v->Starttime = GetSysTime(NULL);
  394.  
  395.   /* Initialize comm program transfer status display */
  396.   v->xpru.xpru_updatemask = XPRU_PROTOCOL | XPRU_FILESIZE | XPRU_MSG | XPRU_BLOCKS |
  397.                             XPRU_ERRORS | XPRU_TIMEOUTS | XPRU_BLOCKCHECK | XPRU_BYTES |
  398.                             XPRU_EXPECTTIME | XPRU_ELAPSEDTIME| XPRU_DATARATE;
  399.   v->xpru.xpru_protocol = "ZModem";
  400.   v->xpru.xpru_filesize = v->Fsize;
  401.   v->xpru.xpru_msg = (v->Thisbinary) ? "Receiving binary file..." : "Receiving text file...";
  402.   v->xpru.xpru_blocks = v->xpru.xpru_errors = v->xpru.xpru_timeouts = 0;
  403.   v->xpru.xpru_blockcheck = "CRC-16";
  404.   v->xpru.xpru_bytes = v->Strtpos;
  405.   update_rate(v);
  406.   (*v->io.xpr_update)(&v->xpru);
  407.  
  408.   return OK;
  409. }
  410.  
  411.  
  412.  
  413. /* Writes the received file data to the output file.
  414.    If in ASCII mode, stops writing at first ^Z, and converts all
  415.    \r\n pairs or solo \r's to \n's. */
  416. short putsec(struct Vars *v) {
  417.   static char nl = '\n';
  418.   UBYTE *p;
  419.   short n;
  420.  
  421.   /* If in binary mode, write it out verbatim */
  422.   if (v->Thisbinary) {
  423.     if (bfwrite(v,v->Pktbuf,(long)v->Rxcount) != v->Rxcount) goto diskfull;
  424.   /* If in text mode, perform end-of-line cleanup */
  425.   } else {
  426.     if (v->Eofseen) return OK;
  427.     for (p=v->Pktbuf, n=v->Rxcount; --n >= 0; ++p) {
  428.       if (*p == CPMEOF) {
  429.         v->Eofseen = TRUE;
  430.         return OK;
  431.       } else if (*p != '\n' && v->Lastsent == '\r') {
  432.         if (bfwrite(v,&nl,1L) != 1) goto diskfull;
  433.       }  
  434.       if (*p != '\r' && bfwrite(v,p,1L) != 1) goto diskfull;
  435.       v->Lastsent = *p;
  436.     }
  437.   }
  438.  
  439.   /* Update comm program status display */
  440.   v->xpru.xpru_updatemask = XPRU_BLOCKS | XPRU_BLOCKSIZE | XPRU_BYTES |
  441.                             XPRU_EXPECTTIME | XPRU_ELAPSEDTIME | XPRU_DATARATE;
  442.   ++v->xpru.xpru_blocks;
  443.   v->xpru.xpru_blocksize = v->Rxcount;
  444.   v->xpru.xpru_bytes = v->Rxbytes += v->Rxcount;
  445.   update_rate(v);
  446.   (*v->io.xpr_update)(&v->xpru);
  447.  
  448.   return OK;
  449.  
  450. diskfull:
  451.   upderr(v,"Error writing file; disk full?");
  452.   v->Noroom = TRUE;
  453.   return ERROR;
  454. }
  455.  
  456.  
  457.  
  458. /* End of batch transmission; disengage cleanly from sender */
  459. void ackbibi(struct Vars *v) {
  460.   short n;
  461.  
  462. #ifdef DEBUGLOG
  463.   dlog(v,"ackbibi:\n");
  464. #endif
  465.   stohdr(v,0L);
  466.   for (n=4; --n;) {
  467.     zshhdr(v,ZFIN);
  468.     switch (readock(v,100)) {
  469.       case 'O':
  470.         readock(v,1);    /* Discard 2nd 'O' */
  471.       case TIMEOUT:
  472.       case RCDO:
  473.         return;
  474.     }
  475.   }
  476. }
  477.