home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / ENTERPRS / CPM / TERMS / ZMP-SRC.LZH / ZMXFER4.C < prev    next >
Text File  |  2000-06-30  |  8KB  |  385 lines

  1. /********************** START OF XFER MODULE 4 ******************************/
  2.  
  3. /* rz.c By Chuck Forsberg modified for cp/m by Hal Maney */
  4.  
  5. #include "zmp.h"
  6. #include "zmodem.h"
  7.  
  8. #ifdef   AZTEC_C
  9. #include "libc.h"
  10. #else
  11. #include <stdio.h>
  12. #endif
  13.  
  14. int Tryzhdrtype;       /* Header type to send corresponding to Last rx close */
  15. char *Rxptr;
  16.  
  17. wcreceive(filename)
  18. char *filename;
  19. {
  20.     char *grabmem(), *alloc();
  21.     static int c;
  22.  
  23.     rlabel();
  24.     QuitFlag = FALSE;
  25.     Zctlesc = 0;
  26.     Baudrate = Baudtable[Current.cbaudindex];
  27.     Tryzhdrtype = ZRINIT;
  28.     Secbuf = alloc(KSIZE + 1);
  29.     if (allocerror(Secbuf))
  30.         return NERROR;
  31.     Cpmbuf = grabmem(&Cpbufsize);
  32.     if (allocerror(Cpmbuf))
  33.         return NERROR;
  34.     Cpindex = 0;                /* just in case */
  35.     Rxptr = Cpmbuf;                /* ditto */
  36.     Rxtimeout = 100;                   /* 10 seconds */
  37.     Errors = 0;
  38.  
  39. #ifdef   DEBUG
  40.     printf("\nbuffer size = %u\n",Cpbufsize);
  41.     wait(5);
  42. #endif
  43.  
  44.     savecurs();
  45.     hidecurs();
  46.     box();
  47.     if (filename == (char *) 0) {            /* batch transfer */
  48.         Crcflag=(Wcsmask==0377);
  49.         if (c=tryz()) {                  /* zmodem transfer */
  50.             report(PROTOCOL,"ZMODEM Receive");
  51.             if (c == ZCOMPL)
  52.                 goto good;
  53.             if (c == NERROR)
  54.                 goto fubar;
  55.             c = rzmfile();
  56.             if (c)
  57.                 goto fubar;
  58.         } 
  59.         else {                            /* ymodem batch transfer */
  60.             report(PROTOCOL,"YMODEM Receive");
  61.             report(BLKCHECK,Crcflag?"CRC":"Checksum");
  62.             for (;;) {
  63.                 if (opabort())
  64.                     goto fubar;
  65.                 if (wcrxpn(Secbuf)== NERROR)
  66.                     goto fubar;
  67.                 if (Secbuf[0]==0)
  68.                     goto good;
  69.                 if (procheader(Secbuf) == NERROR)
  70.                     goto fubar;
  71.                 if (wcrx()==NERROR)
  72.                     goto fubar;
  73.             }
  74.         }
  75.     } 
  76.     else {
  77.         report(PROTOCOL,"XMODEM Receive");
  78.         strcpy(Pathname,filename);
  79.         checkpath(Pathname);
  80.  
  81. #ifdef AZTEC_C
  82.  
  83.         Fd = fopen(Pathname, "wb");
  84.         if (openerror(Fd,Pathname,BUFIOT))
  85. #else
  86.         testexist(Pathname);
  87.         Fd = creat(Pathname, 0);
  88.         if (openerror(Fd,Pathname,UBIOT))
  89. #endif
  90.  
  91.             goto fubar1;
  92.         if (wcrx()==NERROR)                   /* xmodem */
  93.             goto fubar;
  94.     }
  95. good:
  96.     free(Cpmbuf);
  97.     free(Secbuf);
  98.     showcurs();
  99.     restcurs();
  100.     return OK;
  101.  
  102. fubar:
  103.     canit();
  104.  
  105. #ifdef AZTEC_C
  106.     if (Fd)
  107. #else
  108.     if (Fd >= 0)
  109. #endif
  110.  
  111.         unlink(Pathname);    /* File incomplete: erase it */
  112. fubar1:
  113.     free(Cpmbuf);
  114.     free(Secbuf);
  115.     showcurs();
  116.     restcurs();
  117.     return NERROR;
  118. }
  119.  
  120. /*
  121.  * Fetch a pathname from the other end as a C ctyle ASCIZ string.
  122.  * Length is indeterminate as long as less than Blklen
  123.  * A null string represents no more files (YMODEM)
  124.  */
  125.  
  126. wcrxpn(rpn)
  127. char *rpn;    /* receive a pathname */
  128. {
  129.     static int c;
  130.  
  131.     purgeline();
  132.  
  133. et_tu:
  134.     Firstsec=TRUE;  
  135.     Eofseen=FALSE;
  136.     mcharout(Crcflag?WANTCRC:NAK);
  137.     while ((c = wcgetsec(rpn, 100)) != 0) {
  138.         if (QuitFlag)
  139.             return NERROR;
  140.         if (c == WCEOT) {
  141.             mcharout(ACK);
  142.             readline(INTRATIME);
  143.             goto et_tu;
  144.         }
  145.         return NERROR;
  146.     }
  147.     mcharout(ACK);
  148.     return OK;
  149. }
  150.  
  151. /*
  152.  * Adapted from CMODEM13.C, written by
  153.  * Jack M. Wierda and Roderick W. Hart
  154.  */
  155.  
  156. wcrx()
  157. {
  158.     static int sectnum, sectcurr;
  159.     static char sendchar;
  160.     static int cblklen;            /* bytes to dump this block */
  161.     long charsgot;
  162.  
  163.     Firstsec=TRUE;
  164.     sectnum=0; 
  165.     charsgot = 0L;
  166.     Eofseen=FALSE;
  167.     sendchar=Crcflag?WANTCRC:NAK;
  168.     report(BLKCHECK,Crcflag?"CRC":"Checksum");
  169.  
  170.     for (;;) {
  171.         if (opabort())
  172.             return NERROR;
  173.         mcharout(sendchar);                /* send it now, we're ready! */
  174.         sectcurr = wcgetsec(Rxptr,Firstsec||(sectnum&0177)?50:130);
  175.         if (sectcurr==(sectnum+1 &Wcsmask)) {
  176.             charsgot += Blklen;
  177.                sreport(++sectnum,charsgot);
  178.             cblklen = Blklen;
  179.             if (putsec(cblklen,FALSE)==NERROR)
  180.                 return NERROR;
  181.             sendchar=ACK;
  182.         }
  183.         else if (sectcurr==(sectnum&Wcsmask)) {
  184.             zperr("Duplicate Sector",TRUE);
  185.             sendchar=ACK;
  186.         }
  187.         else if (sectcurr==WCEOT) {
  188.             if (closeit())
  189.                 return NERROR;
  190.             mcharout(ACK);
  191.             return OK;
  192.         }
  193.         else if (sectcurr==NERROR)
  194.             return NERROR;
  195.         else {
  196.             zperr( "Sync Error",TRUE);
  197.             return NERROR;
  198.         }
  199.     }
  200. }
  201.  
  202. /*
  203.  * Wcgetsec fetches a Ward Christensen type sector.
  204.  * Returns sector number encountered or NERROR if valid sector not received,
  205.  * or CAN CAN received
  206.  * or WCEOT if eot sector
  207.  * time is timeout for first char, set to 4 seconds thereafter
  208.  ***************** NO ACK IS SENT IF SECTOR IS RECEIVED OK **************
  209.  *    (Caller must do that when he is good and ready to get next sector)
  210.  */
  211.  
  212. wcgetsec(rxbuf, maxtime)
  213. char *rxbuf;
  214. int maxtime;
  215. {
  216.     static int checksum, wcj, firstch;
  217.     static unsigned oldcrc;
  218.     static char *p;
  219.     static int sectcurr;
  220.  
  221.     for (Lastrx=Errors=0; Errors < RETRYMAX; ) {  /* errors incr by zperr */
  222.         if (opabort())
  223.             return NERROR;
  224.         if ((firstch=readline(maxtime))==STX) {
  225.             Blklen=KSIZE; 
  226.             goto get2;
  227.         }
  228.         if (firstch==SOH) {
  229.             Blklen=SECSIZ;
  230. get2:
  231.             sectcurr=readline(INTRATIME);
  232.             if ((sectcurr+(oldcrc=readline(INTRATIME)))==Wcsmask) {
  233.                 oldcrc=checksum=0;
  234.                 for (p=rxbuf,wcj=Blklen; --wcj>=0; ) {
  235.                     if ((firstch=readline(INTRATIME)) < 0)
  236.                         goto bilge;
  237.                     oldcrc=updcrc(firstch, oldcrc);
  238.                     checksum += (*p++ = firstch);
  239.                 }
  240.                 if ((firstch=readline(INTRATIME)) < 0)
  241.                     goto bilge;
  242.                 if (Crcflag) {
  243.                     oldcrc=updcrc(firstch, oldcrc);
  244.                     if ((firstch=readline(INTRATIME)) < 0)
  245.                         goto bilge;
  246.                     oldcrc=updcrc(firstch, oldcrc);
  247.                     if (oldcrc & 0xFFFF)
  248.                         zperr( "CRC Error",TRUE);
  249.                     else {
  250.                         Firstsec=FALSE;
  251.                         return sectcurr;
  252.                     }
  253.                 }
  254.                 else if (((checksum-firstch)&Wcsmask)==0) {
  255.                     Firstsec=FALSE;
  256.                     return sectcurr;
  257.                 }
  258.                 else
  259.                     zperr("Checksum error",TRUE);
  260.             }
  261.             else
  262.                 zperr("Block nr garbled",TRUE);
  263.         }
  264.         /* make sure eot really is eot and not just mixmash */
  265.         else if (firstch==EOT && readline(10)==TIMEOUT)
  266.             return WCEOT;
  267.         else if (firstch==CAN) {
  268.             if (Lastrx==CAN) {
  269.                 zperr( "Sender CANcelled",TRUE);
  270.                 return NERROR;
  271.             } 
  272.             else {
  273.                 Lastrx=CAN;
  274.                 continue;
  275.             }
  276.         }
  277.         else if (firstch==TIMEOUT) {
  278.             if (Firstsec) {
  279.                zperr( "TIMEOUT",TRUE);
  280.                 goto humbug;
  281.          }
  282. bilge:
  283.             zperr( "TIMEOUT",TRUE);
  284.         }
  285.         else
  286.             zperr( "Bad header",TRUE);
  287.  
  288. humbug:
  289.         Lastrx=0;
  290.         while(readline(50) != TIMEOUT)
  291.             if (QuitFlag)
  292.                 return NERROR;
  293.         if (Firstsec) {
  294.             if (Xmodem && (Errors == RETRYMAX/2))
  295.                 Crcflag = !Crcflag;           
  296.             report(BLKCHECK,Crcflag?"CRC":"Checksum");
  297.             mcharout(Crcflag?WANTCRC:NAK);
  298.         } 
  299.         else {
  300.             maxtime=40; 
  301.             mcharout(NAK);
  302.         }
  303.     }
  304.     /* try to stop the bubble machine. */
  305.     canit();
  306.     return NERROR;
  307. }
  308.  
  309. /*
  310.  * Process incoming file information header
  311.  */
  312. procheader(name)
  313. char *name;
  314. {
  315.     long atol();
  316.     static char *p, *ap, c;
  317.     
  318.     /*
  319.      *  Process YMODEM,ZMODEM remote file management requests
  320.      */
  321.  
  322.     clrreports();
  323.     p = name + 1 + strlen(name);
  324.     if (*p) {    /* file coming from Unix or DOS system */
  325.         ap = p;
  326.         while ((c = *p) && (c != ' ')) /* find first space or null */
  327.             ++p;
  328.         if (c)
  329.             *p = '\0';
  330.         /* ap now points to a long integer in ascii */
  331.         report(FILESIZE,ap);
  332.         report(SENDTIME,ttime(atol(ap)));
  333.     }
  334.     strcpy(Pathname, name);
  335.     checkpath(Pathname);
  336.  
  337. #ifdef AZTEC_C
  338.     Fd = fopen(Pathname, "wb");
  339.     if (openerror(Fd,Pathname,BUFIOT))
  340. #else
  341.     testexist(Pathname);
  342.     Fd = creat(Pathname, 0);
  343.     if (openerror(Fd,Pathname,UBIOT))
  344. #endif
  345.  
  346.         return NERROR;
  347.     return OK;
  348. }
  349.  
  350. /*
  351.  * substr(string, token) searches for token in string s
  352.  * returns pointer to token within string if found, NULL otherwise
  353.  */
  354. char *
  355. substr(s, t)
  356. char *s,*t;
  357. {
  358.     static int i;
  359.  
  360.     if ((i=stindex(s,t)) != -1)
  361.         return s+i;
  362.     else
  363.         return NULL;
  364. }
  365.  
  366. /* send cancel string to get the other end to shut up */
  367. canit()
  368. {
  369.     static char canistr[] = {
  370.         24,24,24,24,24,24,24,24,24,24,8,8,8,8,8,8,8,8,8,8,0
  371.     };
  372.  
  373.     mstrout(canistr,FALSE);
  374.     purgeline();
  375. }
  376.  
  377. clrreports()
  378. {
  379.     static int i;
  380.  
  381.     for (i=4; i<13; i++)
  382.         clrline(i);
  383. }
  384. /************************** END OF MODULE 8 *********************************/
  385.