home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff236.lzh / XprZmodem / receive.c < prev    next >
C/C++ Source or Header  |  1989-08-09  |  15KB  |  463 lines

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