home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / dirs / xprzmodem_459.lzh / XprZmodem / send.c < prev    next >
C/C++ Source or Header  |  1991-02-18  |  14KB  |  508 lines

  1. /*  Send.c: File transmission routines for xprzmodem.library;
  2.     Version 2.10, 12 February 1991, by Rick Huebner.
  3.     Based closely on Chuck Forsberg's sz.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 <string.h>
  13. #include "xproto.h"
  14. #include "zmodem.h"
  15. #include "xprzmodem.h"
  16.  
  17. #ifdef DEBUGLOG
  18. extern void *DebugLog;
  19. #endif
  20.  
  21.  
  22.  
  23. /* Main file transmission routine; called by comm program */
  24. long  __saveds XProtocolSend(struct XPR_IO *io) {
  25.   struct Vars *v;
  26.   short err;
  27.  
  28.   /* Perform common setup and initializations */
  29.   if (!(v = setup(io)))
  30.     return XPRS_FAILURE;
  31.   v->Rxtimeout = 600;
  32.  
  33.   /* Transfer the files */  
  34.   zmputs(v,"rz\r");
  35.   stohdr(v,0L);
  36.   zshhdr(v,ZRQINIT);
  37.   sendbuf(v);
  38.   if (getzrxinit(v) == ERROR)
  39.     upderr(v,"Upload cancelled or timed out");
  40.   else
  41.     sendbatch(v);
  42.  
  43.   /* Clean up and return */
  44.   if (err = v->Errcnt)
  45.     upderr(v,"One or more files skipped due to errors");
  46.   else
  47.     updmsg(v,"Done.");
  48.   if (v->io.xpr_setserial && v->Oldstatus != -1)
  49.     xpr_setserial(&v->io,v->Oldstatus);
  50.   FreeMem(v->Filebuf,v->Filebufmax);
  51.   FreeMem(v,(long)sizeof(struct Vars));
  52.   
  53. #ifdef DEBUGLOG
  54.   if (DebugLog) {
  55.     xpr_fclose(&v->io,DebugLog);
  56.     DebugLog = NULL;
  57.   }
  58. #endif
  59.  
  60.   return (err) ? XPRS_FAILURE : XPRS_SUCCESS;
  61. }
  62.  
  63.  
  64.  
  65. /* Negotiate with receiver to start a file transfer */
  66. short getzrxinit(struct Vars *v) {
  67.   short n;
  68.  
  69.   for (n=v->ErrorLimit; --n>=0; ) {
  70.     /* Check for abort from comm program */
  71.     if (v->io.xpr_chkabort && xpr_chkabort(&v->io))
  72.       return ERROR;
  73.     switch (zgethdr(v)) {
  74.       case ZCHALLENGE:        /* Echo receiver's challenge number */
  75.         stohdr(v,v->Rxpos);
  76.         zshhdr(v,ZACK);
  77.         sendbuf(v);
  78.         continue;
  79.       case ZCOMMAND:          /* They didn't see our ZRQINIT; try again */
  80.         stohdr(v,0L);
  81.         zshhdr(v,ZRQINIT);
  82.         sendbuf(v);
  83.         continue;
  84.       case ZRINIT:            /* Receiver ready; get transfer parameters */
  85.         v->Rxbuflen = ((USHORT)v->Rxhdr[ZP1]<<8) | v->Rxhdr[ZP0];
  86. #ifdef DEBUGLOG
  87.         sprintf(v->Msgbuf,"Rxbuflen=%ld Tframlen=%ld\n",(long)v->Rxbuflen,(long)v->Tframlen);
  88.         dlog(v,v->Msgbuf);
  89. #endif
  90.         /* Use shortest of the two side's max frame lengths */
  91.         if (v->Tframlen && (!v->Rxbuflen || v->Tframlen < v->Rxbuflen))
  92.           v->Rxbuflen = v->Tframlen;
  93. #ifdef DEBUGLOG
  94.         sprintf(v->Msgbuf,"Rxbuflen=%ld\n",(long)v->Rxbuflen);
  95.         dlog(v,v->Msgbuf);
  96. #endif
  97.         return OK;
  98.       case ZCAN:
  99.       case RCDO:
  100.       case TIMEOUT:
  101.         upderr(v,v->Msgbuf);
  102.         return ERROR;
  103.       case ZRQINIT:
  104.         if (v->Rxhdr[ZF0] == ZCOMMAND) continue;
  105.         /* fallthrough... */
  106.       default:
  107.         zshhdr(v,ZNAK);
  108.         sendbuf(v);
  109.         continue;
  110.     }
  111.   }
  112.   return ERROR;
  113. }
  114.  
  115.  
  116.  
  117. /* Send a batch of files */
  118. void sendbatch(struct Vars *v) {
  119.   UBYTE single, done = FALSE;
  120.   long fstate;
  121.  
  122.   /* If template routines not provided, must be single filename */
  123.   if (!v->io.xpr_ffirst || !v->io.xpr_fnext) {
  124.     single = TRUE;
  125.     strcpy(v->Filename,v->io.xpr_filename);
  126.   /* Else use the template routines to get the first filename */
  127.   } else {
  128.     single = FALSE;
  129.     fstate = xpr_ffirst(&v->io,v->Filename,v->io.xpr_filename);
  130.     if (!fstate) {
  131.       upderr(v,"No files match template");
  132.       return;
  133.     }
  134.   }
  135.  
  136.   /* If using templates, keep getting names & sending until done */
  137.   while (!done) {
  138.     if (sendone(v) == ERROR) return;
  139.     if (single) break;
  140.     fstate = xpr_fnext(&v->io,fstate,v->Filename,v->io.xpr_filename);
  141.     done = !fstate;
  142.   }
  143.  
  144.   /* End batch and return; if we never got started, just cancel receiver */
  145.   if (v->Filcnt) saybibi(v);
  146.   else canit(v);
  147. }
  148.  
  149.  
  150.  
  151. /* Send the file named in v->Filename */
  152. short sendone(struct Vars *v) {
  153.   struct SetupVars *sv;
  154.  
  155. #ifdef DEBUGLOG
  156.   sprintf(v->Msgbuf,"*** Sending %s\n",v->Filename);
  157.   dlog(v,v->Msgbuf);
  158. #endif
  159.  
  160.   /* Display name of file being sent for user */
  161.   v->xpru.xpru_updatemask = XPRU_FILENAME;
  162.   v->xpru.xpru_filename = v->Filename;
  163.   xpr_update(&v->io,&v->xpru);
  164.  
  165.   /* Set text/binary mode according to options before opening file */
  166.   set_textmode(v);
  167.  
  168.   /* Open the file, if possible */
  169.   if (!(v->File = bfopen(v,"r"))) {
  170.     ++v->Errcnt;
  171.     upderr(v,"Can't open file; skipping");
  172.     return OK;      /* pass over it, there may be others */
  173.   }
  174.   ++v->Filcnt;
  175.   GetSysTime(&v->Starttime);
  176.  
  177.   /* Kick off the file transfer */
  178.   sv = (void *)v->io.xpr_data;
  179.   switch (sendname(v)) {
  180.     case ERROR:
  181.       ++v->Errcnt;
  182.       return ERROR;
  183.     case OK:
  184.       bfclose(v);
  185.       /* File sent; if option DY, delete file after sending */
  186.       if (*sv->option_d == 'Y' && v->io.xpr_extension >= 2 && v->io.xpr_unlink) {
  187.         updmsg(v,"Deleting file after send");
  188.         xpr_unlink(&v->io,v->Filename);
  189.       }
  190.       break;
  191.   }
  192.   return OK;
  193. }
  194.  
  195.  
  196.  
  197. /* Build file info block consisting of file name, length, time, and mode */
  198. short sendname(struct Vars *v) {
  199.   struct SetupVars *sv;
  200.   UBYTE *p, *q, buff[32];
  201.  
  202.   /* Initialize comm program transfer status display */
  203.   v->Fsize =  (v->io.xpr_finfo) ? xpr_finfo(&v->io,v->Filename,1L) : -1;
  204.   v->xpru.xpru_updatemask = XPRU_PROTOCOL | XPRU_FILESIZE | XPRU_MSG | XPRU_BLOCKS |
  205.                             XPRU_ERRORS | XPRU_TIMEOUTS | XPRU_BLOCKCHECK | XPRU_BYTES |
  206.                             XPRU_EXPECTTIME | XPRU_ELAPSEDTIME | XPRU_DATARATE;
  207.   v->xpru.xpru_protocol = "ZModem";
  208.   v->xpru.xpru_filesize = v->Fsize;
  209.   v->xpru.xpru_msg = (v->Lzconv == ZCNL) ? "Sending text file..." :
  210.     ( (v->Lzconv == ZCBIN) ? "Sending binary file..." : "Sending file...");
  211.   v->xpru.xpru_blocks = v->xpru.xpru_errors = v->xpru.xpru_timeouts = 0;
  212.   v->xpru.xpru_blockcheck = "CRC-16";
  213.   v->xpru.xpru_bytes = v->Strtpos = 0;
  214.   update_rate(v);
  215.   xpr_update(&v->io,&v->xpru);
  216.  
  217.   sv = (void *)v->io.xpr_data;
  218.   if (*sv->option_s == 'Y') {
  219.     /* If "SY" option selected, send full path */
  220.     strcpy(v->Pktbuf,v->Filename);
  221.     p = v->Pktbuf + strlen(v->Pktbuf) + 1;
  222.   } else {
  223.     /* else extract outgoing file name without directory path */
  224.     for (p=v->Filename, q=v->Pktbuf ; *p; ++p, ++q)
  225.       if ((*q = *p) == '/' || *q == ':') q = v->Pktbuf - 1;
  226.     *q = '\0';
  227.     p = ++q;
  228.   }
  229.  
  230.   /* Zero out remainder of file info packet */
  231.   memset(p,0,sizeof(v->Pktbuf) - (p - v->Pktbuf));
  232.  
  233.   /* Store file size, timestamp, and mode in info packet */
  234.   /* XPR spec doesn't provide a way to get the file timestamp or file mode,
  235.      so we'll just fake it with the current time and a dummy 0. */
  236.   stcl_o(buff,GetSysTime(NULL)+UnixTimeOffset);
  237.   /* amiga.lib sprintf() can't do %lo format, so we do it the hard way */
  238.   /* Yes, octal; ZModem was originally done on Unix, and they like octal there */
  239.   sprintf(p,"%ld %s 0",(v->Fsize < 0) ? 0L : v->Fsize,buff);
  240.  
  241.   /* Send filename packet */
  242.   return zsendfile(v,(short)(p - v->Pktbuf + strlen(p) + 1));
  243. }
  244.  
  245.  
  246.  
  247. /* Send the filename packet and see if receiver will accept file */
  248. short zsendfile(struct Vars *v,short blen) {
  249.   short c;
  250.  
  251.   while (TRUE) {
  252.     v->Txhdr[ZF0] = v->Lzconv; /* Text or Binary mode; from config string */
  253.     v->Txhdr[ZF1] = LZMANAG;   /* Default file management mode */
  254.     v->Txhdr[ZF2] = LZTRANS;   /* Default file transport mode */
  255.     v->Txhdr[ZF3] = 0;
  256.     zsbhdr(v,ZFILE);
  257.     zsdata(v,blen,ZCRCW);
  258.     sendbuf(v);
  259. again:
  260.     /* Check for abort from comm program */
  261.     if (v->io.xpr_chkabort && xpr_chkabort(&v->io)) {
  262.       bfclose(v);
  263.       return ERROR;
  264.     }
  265.     switch (c = zgethdr(v)) {
  266.       case ZRINIT:
  267.         goto again;
  268.       case ZCAN:
  269.       case ZCRC:
  270.       case RCDO:
  271.       case TIMEOUT:
  272.       case ZABORT:
  273.       case ZFIN:
  274.         upderr(v,v->Msgbuf);
  275.         return ERROR;
  276.       case ZSKIP:             /* Receiver doesn't want this one */
  277.         upderr(v,"SKIP command received");
  278.         bfclose(v);
  279.         return c;
  280.       case ZRPOS:             /* Receiver wants it; this is starting position */
  281.         bfseek(v,v->Rxpos);
  282.         v->Strtpos = v->Txpos = v->Rxpos;
  283.         if (v->io.xpr_sflush) xpr_sflush(&v->io);
  284.         v->Modemcount = 0;
  285.         return zsendfdata(v);
  286.     }
  287.   }
  288. }
  289.  
  290.  
  291.  
  292. /* Send the file data */
  293. short zsendfdata(struct Vars *v) {
  294.   short c, e, blklen, goodbytes = 0;
  295.   USHORT framelen, maxblklen, goodneeded = 512;
  296.  
  297.   /* Figure out max data packet size to send */
  298.   maxblklen = KSIZE;
  299.   if (v->Rxbuflen && maxblklen > v->Rxbuflen)
  300.     maxblklen = v->Rxbuflen;
  301.   blklen = (v->Baud < 1200) ? 256 : KSIZE;
  302.   if (blklen > maxblklen)
  303.     blklen = maxblklen;
  304. #ifdef DEBUGLOG
  305.   sprintf(v->Msgbuf,"Rxbuflen=%ld blklen=%ld\n",(long)v->Rxbuflen,(long)blklen);
  306.   dlog(v,v->Msgbuf);
  307. #endif
  308.  
  309.   /* If an interruption happened, handle it; else keep sending data */
  310. somemore:
  311.   while (char_avail(v)) {
  312.     /* Check for another incoming packet while discarding line noise */
  313.     switch (readock(v,1)) {
  314.       case CAN:
  315.       case RCDO:
  316.       case ZPAD:
  317.         break;
  318.       default:
  319.         continue;
  320.     }
  321. waitack:
  322. #ifdef DEBUGLOG
  323.     dlog(v,"--- At waitack\n");
  324. #endif
  325.     switch (c = getinsync(v)) {
  326.       default:
  327.         upderr(v,"Transfer cancelled");
  328.         bfclose(v);
  329.         return ERROR;
  330.       case ZSKIP:  /* Receiver changed its mind and wants to skip the file */
  331.         return c;
  332.       case ZACK:   /* ACK at end of frame; resume sending data */
  333.         break;
  334.       case ZRPOS:  /* An error; resend data from last good point */
  335.         blklen >>= 2;
  336.         if (blklen < MINBLOCK) blklen = MINBLOCK;
  337.         if (goodneeded < MAXGOODNEEDED) goodneeded <<= 1;
  338.         v->xpru.xpru_updatemask = XPRU_ERRORS;
  339.         ++v->xpru.xpru_errors;
  340.         xpr_update(&v->io,&v->xpru);
  341.         break;
  342.       case ZRINIT:
  343.         updmsg(v,"Done.");
  344.         return OK;
  345.     }
  346.   }
  347.  
  348.   /* Transmit ZDATA frame header */
  349.   framelen = v->Rxbuflen;
  350.   stohdr(v,v->Txpos);
  351.   zsbhdr(v,ZDATA);
  352.  
  353.   /* Keep sending data packets until finished or interrupted */
  354.   do {
  355.     /* Read next chunk of file data */
  356.     c = bfread(v,v->Pktbuf,(long)blklen);
  357.  
  358.     /* Figure out how to handle this data packet */
  359.     if (c < blklen)
  360.       e = ZCRCE;  /* If end of file, this is last data packet */
  361.     else if (v->Rxbuflen && (framelen -= c) <= 0)
  362.       e = ZCRCW;  /* If end of frame, ask for ACK */
  363.     else
  364.       e = ZCRCG;  /* Else tell receiver to expect more data packets */
  365.  
  366.     zsdata(v,c,e);  /* Send the packet */
  367.     sendbuf(v);
  368.  
  369.     /* Update comm program status display */
  370.     v->xpru.xpru_updatemask = XPRU_BLOCKS | XPRU_BLOCKSIZE | XPRU_BYTES |
  371.                               XPRU_EXPECTTIME | XPRU_ELAPSEDTIME | XPRU_DATARATE;
  372.     ++v->xpru.xpru_blocks;
  373.     v->xpru.xpru_blocksize = c;
  374.     v->xpru.xpru_bytes = v->Txpos += c;
  375.     update_rate(v);
  376.     xpr_update(&v->io,&v->xpru);
  377.  
  378.     /* If we've been sending smaller than normal packets, see if it's
  379.        time to bump the packet size up a notch yet */
  380.     if (blklen < maxblklen && (goodbytes += c) >= goodneeded) {
  381.       blklen <<= 1;
  382.       if (blklen > maxblklen) blklen = maxblklen;
  383.       goodbytes = 0;
  384. #ifdef DEBUGLOG
  385.       sprintf(v->Msgbuf,"Bumping packet size to %ld at %ld\n",(long)blklen,v->Txpos);
  386.       dlog(v,v->Msgbuf);
  387. #endif
  388.     }
  389.  
  390.     /* Give comm program its timeslice if it needs one */
  391.     if (v->io.xpr_chkmisc) xpr_chkmisc(&v->io);
  392.     /* Check for abort from comm program */
  393.     if (v->io.xpr_chkabort && xpr_chkabort(&v->io)) goto aborted;
  394.     /* If this was last packet in frame, go wait for ACK from receiver */
  395.     if (e == ZCRCW) goto waitack;
  396.  
  397.     /* Check if receiver trying to interrupt us; look for incoming packet
  398.        while discarding line noise */
  399.     while (char_avail(v)) {
  400.       switch (readock(v,1)) {
  401.         case CAN:
  402.         case RCDO:
  403.         case ZPAD:
  404.           /* Interruption detected; stop sending and process complaint */
  405. #ifdef DEBUGLOG
  406.           dlog(v,"--- Interrupted send\n");
  407. #endif
  408.           zsdata(v,0,ZCRCE);
  409.           sendbuf(v);
  410.           goto waitack;
  411.       }
  412.     }
  413.   } while (e == ZCRCG);  /* If no interruption, keep sending data packets */
  414.  
  415.   /* Done sending file data; send EOF and wait for receiver to acknowledge */
  416.   while (TRUE) {
  417.     updmsg(v,"Sending EOF");
  418.     stohdr(v,v->Txpos);
  419.     zsbhdr(v,ZEOF);
  420.     sendbuf(v);
  421.     switch (c = getinsync(v)) {
  422.       case ZACK:
  423.         continue;
  424.       case ZRPOS:
  425.         goto somemore;
  426.       case ZRINIT:
  427.         updmsg(v,"EOF acknowledged");
  428.         ++v->Starttime.tv_secs;
  429.         update_rate(v);
  430.         v->xpru.xpru_updatemask = XPRU_EXPECTTIME | XPRU_ELAPSEDTIME | XPRU_DATARATE;
  431.         xpr_update(&v->io,&v->xpru);
  432.         return OK;
  433.       case ZSKIP:
  434.         return c;
  435.       default:
  436. aborted: upderr(v,"Transfer cancelled");
  437.         bfclose(v);
  438.         return ERROR;
  439.     }
  440.   }
  441. }
  442.  
  443.  
  444.  
  445. /* Respond to receiver's complaint, get back in sync with receiver */
  446. short getinsync(struct Vars *v) {
  447.   short c;
  448.  
  449.   while (TRUE) {
  450. #ifdef DEBUGLOG
  451.     dlog(v,"--- At getinsync\n");
  452. #endif
  453.     c = zgethdr(v);
  454.     if (v->io.xpr_sflush)
  455.       xpr_sflush(&v->io);
  456.     v->Modemcount = 0;
  457.     switch (c) {
  458.       case ZCAN:
  459.       case ZABORT:
  460.       case ZFIN:
  461.       case RCDO:
  462.       case TIMEOUT:
  463.         upderr(v,v->Msgbuf);
  464.         return ERROR;
  465.       case ZRPOS:
  466.         bfseek(v,v->Rxpos);
  467.         v->Txpos = v->Rxpos;
  468.         sprintf(v->Msgbuf,"Resending from %ld",v->Txpos);
  469.         upderr(v,v->Msgbuf);
  470.         return c;
  471.       case ZSKIP:
  472.         upderr(v,"SKIP command received");
  473.         /* fallthrough... */
  474.       case ZRINIT:
  475.         bfclose(v);
  476.         /* fallthrough... */
  477.       case ZACK:
  478.         return c;
  479.       default:
  480.         zsbhdr(v,ZNAK);
  481.         sendbuf(v);
  482.         continue;
  483.     }
  484.   }
  485. }
  486.  
  487.  
  488.  
  489. /* End of batch transmission; disengage cleanly from receiver */
  490. void saybibi(struct Vars *v) {
  491.   while (TRUE) {
  492.     stohdr(v,0L);
  493.     zsbhdr(v,ZFIN);
  494.     sendbuf(v);
  495.     switch (zgethdr(v)) {
  496.       case ZFIN:
  497.         sendline(v,'O');
  498.         sendline(v,'O');
  499.         sendbuf(v);
  500.         /* fallthrough... */
  501.       case ZCAN:
  502.       case RCDO:
  503.       case TIMEOUT:
  504.         return;
  505.     }
  506.   }
  507. }
  508.