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 / RZMP-SRC.LZH / RZMP5.C < prev    next >
Text File  |  2000-06-30  |  13KB  |  661 lines

  1. /********************** START OF RZMP MODULE 5 ******************************/
  2.  
  3. /* rz.c By Chuck Forsberg modified for cp/m by Hal Maney */
  4. /* Modified further rjm to remove xmodem/ymodem stuff for rzmp */
  5.  
  6. #define RZMP
  7. #include "zmp.h"
  8. #include "zmodem.h"
  9.  
  10. #ifdef   AZTEC_C
  11. #include "libc.h"
  12. #else
  13. #include <stdio.h>
  14. #endif
  15.  
  16. int Tryzhdrtype;       /* Header type to send corresponding to Last rx close */
  17. char *Rxptr;
  18.  
  19. wcreceive(filename)
  20. char *filename;
  21. {
  22.     char *grabmem(), *alloc();
  23.     static int c;
  24.  
  25.     rlabel();
  26.     QuitFlag = FALSE;
  27.     Zctlesc = 0;
  28.     Baudrate = Baudtable[Current.cbaudindex];
  29.     Tryzhdrtype = ZRINIT;
  30.     Secbuf = alloc(KSIZE + 1);
  31.     if (allocerror(Secbuf))
  32.         return NERROR;
  33.     Cpmbuf = grabmem(&Cpbufsize);
  34.     if (allocerror(Cpmbuf))
  35.         return NERROR;
  36.     Cpindex = 0;                /* just in case */
  37.     Rxptr = Cpmbuf;            /* ditto */
  38.     Rxtimeout = 100;                   /* 10 seconds */
  39.     Errors = 0;
  40.  
  41. #ifdef   DEBUG
  42.     printf("\nbuffer size = %u\n",Cpbufsize);
  43.     wait(5);
  44. #endif
  45.  
  46.     savecurs();
  47.     hidecurs();
  48.     box();
  49.     if (filename == (char *) 0) {            /* batch transfer */
  50.         Crcflag=(Wcsmask==0377);
  51.         if (c=tryz()) {                  /* zmodem transfer */
  52.             report(PROTOCOL,"ZMODEM Receive");
  53.             if (c == ZCOMPL)
  54.                 goto good;
  55.             if (c == NERROR)
  56.                 goto fubar;
  57.             c = rzmfile();
  58.             if (c)
  59.                 goto fubar;
  60.         } 
  61.         else 
  62.             goto fubar;    /* We only sell zmodem here */
  63.  
  64.     }
  65. good:
  66.      free(Cpmbuf);
  67.      free(Secbuf);
  68.      showcurs();
  69.      restcurs();
  70.      return OK;
  71.  
  72. fubar:
  73.     canit();
  74.  
  75. #ifdef AZTEC_C
  76.     if (Fd)
  77. #else
  78.     if (Fd >= 0)
  79. #endif
  80.  
  81.     unlink(Pathname);    /* File incomplete: erase it */
  82.     free(Cpmbuf);
  83.     free(Secbuf);
  84.     showcurs();
  85.     restcurs();
  86.     return NERROR;
  87. }
  88.  
  89.  
  90. /*
  91.  * Process incoming file information header
  92.  */
  93. procheader(name)
  94. char *name;
  95. {
  96.    long atol();
  97.     static char *p, *ap, c;
  98.     
  99.     /*
  100.      *  Process YMODEM,ZMODEM remote file management requests
  101.      */
  102.  
  103.     clrreports();
  104.     p = name + 1 + strlen(name);
  105.     if (*p) {    /* file coming from Unix or DOS system */
  106.         ap = p;
  107.         while ((c = *p) && (c != ' ')) /* find first space or null */
  108.             ++p;
  109.         if (c)
  110.             *p = '\0';
  111.             /* ap now points to a long integer in ascii */
  112.         report(FILESIZE,ap);
  113.         report(SENDTIME,ttime(atol(ap)));
  114.     }
  115.     checkpath(name);
  116.     strcpy(Pathname, name);
  117.     if (Private)
  118.         addu(Pathname,Uprivdrive,Uprivuser);
  119.  
  120. #ifdef AZTEC_C
  121.     Fd = fopen(Pathname, "wb");
  122.     if (openerror(Fd,Pathname,BUFIOT))
  123. #else
  124.     if (testexist(Pathname) == NERROR)
  125.         return NERROR;    /* not wheel and file's already there */
  126.     Fd = creat(Pathname, 0);
  127.     if (openerror(Fd,Pathname,UBIOT))
  128. #endif
  129.  
  130.         return NERROR;
  131.     return OK;
  132. }
  133.  
  134. /*
  135.  * substr(string, token) searches for token in string s
  136.  * returns pointer to token within string if found, NULL otherwise
  137.  */
  138. char *
  139. substr(s, t)
  140. char *s,*t;
  141. {
  142.     static int i;
  143.  
  144.     if ((i=stindex(s,t)) != -1)
  145.         return s+i;
  146.     else
  147.         return NULL;
  148. }
  149.  
  150. /* send cancel string to get the other end to shut up */
  151. canit()
  152. {
  153.     static char canistr[] = {
  154.         24,24,24,24,24,24,24,24,24,24,8,8,8,8,8,8,8,8,8,8,0
  155.     };
  156.  
  157.     mstrout(canistr,FALSE);
  158.     purgeline();
  159. }
  160.  
  161. clrreports()
  162. {
  163.     static int i;
  164.  
  165.     for (i=4; i<13; i++)
  166.         clrline(i);
  167. }
  168.  
  169. zperr(string,incrflag)
  170. char *string;
  171. int incrflag;
  172. {
  173.     clrline(MESSAGE);
  174.     report(MESSAGE,string);
  175.     if (incrflag)
  176.         dreport(ERRORS,++Errors);
  177. }
  178.  
  179. dreport(row,value)
  180. int row, value;
  181. {
  182.     static char buf[7];
  183.  
  184.     report(row,itoa(value,buf));
  185. }
  186.  
  187. lreport(row,value)
  188. int row;
  189. long value;
  190. {
  191.     static char buf[20];
  192.  
  193.     report(row,ltoa(value,buf));
  194. }
  195.  
  196. sreport(sct,bytes)
  197. int sct;
  198. long bytes;
  199. {  
  200.     dreport(BLOCKS,sct);
  201.     lreport(KBYTES,bytes);
  202. }
  203.  
  204. clrline(line)
  205. int line;
  206. {
  207.     report(line,"                ");
  208. }
  209.  
  210. /*
  211.  * Initialize for Zmodem receive attempt, try to activate Zmodem sender
  212.  *  Handles ZSINIT frame
  213.  *  Return ZFILE if Zmodem filename received, -1 on error,
  214.  *   ZCOMPL if transaction finished,  else 0
  215.  */
  216. tryz()
  217. {
  218.     static int c, n, *ip;
  219.     static int cmdzack1flg;
  220.  
  221.     if (Nozmodem)        /* ymodem has been forced */
  222.         return 0;
  223.  
  224.     for (n=Zmodem?15:5; --n>=0; ) {
  225.         if (opabort())
  226.             return NERROR;
  227.         /* Set buffer length (0) and capability flags */
  228.         stohdr(0L);
  229.         Txhdr[ZF0] = (Wantfcs32 ? CANFC32 : 0) | CANFDX;
  230.         if (Zctlesc)
  231.             Txhdr[ZF0] |= TESCCTL;
  232.         ip = (int *)&Txhdr[ZP0];
  233.         *ip = Cpbufsize;
  234.         zshhdr(Tryzhdrtype, Txhdr);
  235.         if (Tryzhdrtype == ZSKIP)    /* Don't skip too far */
  236.             Tryzhdrtype = ZRINIT;    /* CAF 8-21-87 */
  237. again:
  238.         switch (zgethdr(Rxhdr, 0)) {
  239.             case ZRQINIT:
  240.                 continue;
  241.             case ZEOF:
  242.                 continue;
  243.             case TIMEOUT:
  244.                 continue;
  245.             case ZFILE:
  246.                 Zconv = Rxhdr[ZF0];
  247.                 Zmanag = Rxhdr[ZF1];
  248.                 Ztrans = Rxhdr[ZF2];
  249.                 Tryzhdrtype = ZRINIT;
  250.                 c = zrdata(Secbuf, KSIZE);
  251.                 if (c == GOTCRCW)
  252.                     return ZFILE;
  253.                 zshhdr(ZNAK, Txhdr);
  254.                 goto again;
  255.             case ZSINIT:
  256.                 Zctlesc = TESCCTL & Rxhdr[ZF0];
  257.                 if (zrdata(Attn, ZATTNLEN) == GOTCRCW) {
  258.                     zshhdr(ZACK, Txhdr);
  259.                     goto again;
  260.                 }
  261.                 zshhdr(ZNAK, Txhdr);
  262.                 goto again;
  263.             case ZFREECNT:
  264.                 stohdr(0L);
  265.                 zshhdr(ZACK, Txhdr);
  266.                 goto again;
  267.             case ZCOMMAND:
  268.                 cmdzack1flg = Rxhdr[ZF0];
  269.                 if (zrdata(Secbuf, KSIZE) == GOTCRCW) {
  270.                        stohdr(0L);
  271.                     purgeline();    /* dump impatient questions */
  272.                     do {
  273.                         zshhdr(ZCOMPL, Txhdr);
  274.                         zperr("Waiting for ZFIN",FALSE);
  275.                         if (opabort())
  276.                             return NERROR;
  277.                     }
  278.                         while (++Errors<20 && zgethdr(Rxhdr,1) != ZFIN);
  279.                     ackbibi();
  280.                     return ZCOMPL;
  281.                 }
  282.                 zshhdr(ZNAK, Txhdr); 
  283.                 goto again;
  284.             case ZCOMPL:
  285.                 goto again;
  286.             default:
  287.                 continue;
  288.             case ZFIN:
  289.                 ackbibi(); 
  290.                 return ZCOMPL;
  291.             case ZCAN:
  292.                 return NERROR;
  293.         }
  294.     }
  295.     return 0;
  296. }
  297.  
  298. /*
  299.  * Receive 1 or more files with ZMODEM protocol
  300.  */
  301.  
  302. rzmfile()
  303. {
  304.     static int c;
  305.  
  306.     for (;;) {
  307.         if (opabort())
  308.             return NERROR;
  309.         switch (c = rzfile()) {
  310.         case ZEOF:
  311.         case ZSKIP:
  312.             switch (tryz()) {
  313.             case ZCOMPL:
  314.                 return OK;
  315.             default:
  316.                 return NERROR;
  317.             case ZFILE:
  318.                 break;
  319.             }
  320.             continue;
  321.         default:
  322.             return c;
  323.         case NERROR:
  324.             return NERROR;
  325.         }
  326.     }
  327. }
  328.  
  329. /*
  330.  * Receive a file with ZMODEM protocol
  331.  *  Assumes file name frame is in Secbuf
  332.  */
  333. rzfile()
  334. {
  335.     static int c, n;
  336.     static unsigned bufleft;
  337.     static long rxbytes;
  338.  
  339.     Eofseen=FALSE;
  340.     if (procheader(Secbuf) == NERROR) {
  341.         return (Tryzhdrtype = ZSKIP);
  342.     }
  343.  
  344.     n = 20; 
  345.     rxbytes = 0L;
  346.     Firstsec = TRUE;
  347.  
  348.     for (;;) {
  349.         if (opabort())
  350.             return NERROR;
  351.         stohdr(rxbytes);
  352.         zshhdr(ZRPOS, Txhdr);
  353. nxthdr:
  354.         if (opabort())
  355.             return NERROR;
  356.         switch (c = zgethdr(Rxhdr, 0)) {
  357.  
  358.             default:
  359.                 return NERROR;
  360.  
  361.             case ZNAK:
  362.             case TIMEOUT:
  363.                 if ( --n < 0)
  364.                     return NERROR;
  365.  
  366.             case ZFILE:
  367.                 zrdata(Secbuf, KSIZE);
  368.                 continue;
  369.  
  370.             case ZEOF:
  371.                 if (rclhdr(Rxhdr) != rxbytes) {
  372.                 /*
  373.                  * Ignore eof if it's at wrong place - force
  374.                  *  a timeout because the eof might have gone
  375.                  *  out before we sent our zrpos.
  376.                  */
  377.                     Errors = 0;  
  378.                     goto nxthdr;
  379.                 }
  380.                 if (closeit()) {
  381.                     Tryzhdrtype = ZFERR;
  382.                     return NERROR;
  383.                 }
  384.                      lreport(KBYTES,rxbytes);
  385.                 crcrept(Crc32);
  386.                 (*Uploads)++;    /* bump upload count */
  387.                 if (!(*Wheel)) {    /* if not wheel, */
  388.                     if (!Private) /* Privates have du: */
  389.                         addu(Pathname,Upldrive,
  390.                                 Upluser);
  391.                     savelog(Pathname); /* write to disk */
  392.                 }
  393.                 return c;
  394.  
  395.             case NERROR:    /* Too much garbage in header search error */
  396.                 if ( --n < 0)
  397.                     return NERROR;
  398.                 zmputs(Attn);
  399.                 continue;
  400.  
  401.             case ZDATA:
  402.                 if (rclhdr(Rxhdr) != rxbytes) {
  403.                     if ( --n < 0)
  404.                         return NERROR;
  405.                     zmputs(Attn);  
  406.                     continue;
  407.                 }
  408. moredata:
  409.                 if (opabort())
  410.                     return NERROR;
  411.                 bufleft = Cpbufsize - Cpindex;
  412.                 c = zrdata(Rxptr, 
  413.                     (bufleft > KSIZE) ? KSIZE : bufleft);
  414.                 switch (c) {
  415.  
  416.                     case ZCAN:
  417.                         return NERROR;
  418.  
  419.                     case NERROR:    /* CRC error */
  420.                         statrep(rxbytes);
  421.                         if ( --n < 0)
  422.                             return NERROR;
  423.                         zmputs(Attn);
  424.                         continue;
  425.  
  426.                     case TIMEOUT:
  427.                         statrep(rxbytes);
  428.                         if ( --n < 0)
  429.                             return NERROR;
  430.                         continue;
  431.  
  432.                     case GOTCRCW:
  433.                         n = 20;
  434.                             /* Write to disk! */
  435.                         if (putsec(Rxcount,TRUE)
  436.                             == NERROR)
  437.                                 return NERROR;
  438.                         rxbytes += Rxcount;
  439.                         stohdr(rxbytes);
  440.                         statrep(rxbytes);
  441.                         zshhdr(ZACK, Txhdr);
  442.                         xmchout(XON);
  443.                         goto nxthdr;
  444.  
  445.                     case GOTCRCQ:
  446.                         n = 20;
  447.                             /* Write to disk! */
  448.                         if (putsec(Rxcount,TRUE)
  449.                             == NERROR)
  450.                                 return NERROR;
  451.                         rxbytes += Rxcount;
  452.                         stohdr(rxbytes);
  453.                         zshhdr(ZACK, Txhdr);
  454.                         goto moredata;
  455.  
  456.                     case GOTCRCG:
  457.                         n = 20;
  458.                         if (putsec(Rxcount,FALSE)
  459.                             == NERROR)
  460.                                 return NERROR;
  461.                         rxbytes += Rxcount;
  462.                         goto moredata;
  463.  
  464.                     case GOTCRCE:
  465.                         n = 20;
  466.                         if (putsec(Rxcount,FALSE)
  467.                             == NERROR)
  468.                                 return NERROR;
  469.                         rxbytes += Rxcount;
  470.                         goto nxthdr;
  471.                 }
  472.         }
  473.     }
  474. }
  475.  
  476. /* Status report: don't do unless after error or ZCRCW since characters */
  477. /*    will be lost unless rx has interrupt-driven I/O            */
  478. statrep(rxbytes)
  479. long rxbytes;
  480. {
  481.     lreport(KBYTES,rxbytes);
  482.     crcrept(Crc32);
  483. }
  484.  
  485. /* Report CRC mode in use, but only if first sector */
  486. crcrept(flag)
  487. short flag;
  488. {
  489.     if (Firstsec)
  490.         report(BLKCHECK, flag ? "CRC-32" : "CRC-16");
  491.     Firstsec = FALSE;        /* clear the flag */
  492. }
  493.  
  494. /* Add a block to the main buffer pointer and write to disk if full */
  495.         /* or if flag set */
  496. putsec(count,flag)
  497. int count, flag;
  498. {
  499.     short status;
  500.     unsigned size;
  501.  
  502.     status = 0;
  503.     Rxptr += count;
  504.     Cpindex += count;
  505.     if ((Cpindex >= Cpbufsize) || flag)
  506.     {
  507.         size = (Cpindex > Cpbufsize) ? Cpbufsize : Cpindex;
  508.  
  509. #ifdef AZTEC_C
  510.         status = fwrite(Cpmbuf,1,size,Fd);
  511.         if (status <= 0)
  512. #else
  513.         status = write(Fd,Cpmbuf,size);
  514.         if (status != size)
  515. #endif
  516.  
  517.         {
  518.             zperr("Disk write error",TRUE);
  519.             status = NERROR;
  520.         }
  521.         Cpindex = 0;
  522.         Rxptr = Cpmbuf;
  523.     }
  524.     return status;
  525. }
  526.  
  527. /*
  528.  * Send a string to the modem, processing for \336 (sleep 1 sec)
  529.  *   and \335 (break signal)
  530.  */
  531. zmputs(s)
  532. char *s;
  533. {
  534.     static int c;
  535.  
  536.     while (*s) {
  537.       if (opabort())
  538.          return NERROR;
  539.         switch (c = *s++) {
  540.         case '\336':
  541.             wait(1); 
  542.             continue;
  543.         case '\335':
  544.             sendbrk(); 
  545.             continue;
  546.         default:
  547.             xmchout(c);
  548.         }
  549.     }
  550. }
  551.  
  552. /* Test if file exists, rename to .BAK if so */
  553. testexist(filename)
  554. char *filename;
  555. {
  556.     int fd, status;
  557.     char *p, newfile[20], *index();
  558.  
  559.     status = OK;
  560.     if ((fd = open(filename,0)) != UBIOT) {
  561.         if (*Wheel) {         /* file with that name is there */
  562.             close(fd);    /* only wheels get .bak */
  563.             strcpy(newfile,filename);
  564.             if (p = index(newfile,'.'))
  565.                 *p = '\0';    /* stop at dot */
  566.             strcat(newfile,".bak");
  567.             unlink(newfile);    /* remove any .bak already there */
  568.             rename(filename,newfile);
  569.         }
  570.         else
  571.             status = NERROR;
  572.     }
  573.     return status;
  574. }
  575.  
  576. /*
  577.  * Close the receive dataset, return OK or NERROR
  578.  */
  579. closeit()
  580. {  
  581.     static int status;
  582.     int length;
  583.  
  584.     status = OK;
  585.     if (Cpindex) {
  586.         length = 128*roundup(Cpindex,128);
  587.  
  588. #ifdef AZTEC_C
  589.         status = fwrite(Cpmbuf,length,1,Fd) ? OK : NERROR;
  590. #else
  591.         status = ((write(Fd,Cpmbuf,length) == length) ? OK : NERROR);
  592. #endif
  593.  
  594.         Cpindex = 0;
  595.         Rxptr = Cpmbuf;
  596.     }
  597.     if (status == NERROR)
  598.         zperr("Disk write error",TRUE);
  599.  
  600. #ifdef AZTEC_C
  601.     if (fclose(Fd)==NERROR) {
  602.         Fd = 0;
  603. #else
  604.     if (close(Fd)==NERROR) {
  605.         Fd = -1;
  606. #endif
  607.  
  608.         zperr("File close error",TRUE);
  609.         return NERROR;
  610.     }
  611.     return status;
  612. }
  613.  
  614. /*
  615.  * Ack a ZFIN packet, let byegones be byegones
  616.  */
  617.  
  618. ackbibi()
  619. {
  620.     static int n;
  621.  
  622.     stohdr(0L);
  623.     for (n=3; --n>=0; ) {
  624.         purgeline();
  625.         zshhdr(ZFIN, Txhdr);
  626.         switch (readline(100)) {
  627.         case 'O':
  628.             readline(INTRATIME);    /* Discard 2nd 'O' */
  629.             return;
  630.         case RCDO:
  631.             return;
  632.         case TIMEOUT:
  633.         default:
  634.             break;
  635.         }
  636.     }
  637. }
  638.    
  639. long
  640. atol(string)
  641. char *string;
  642. {
  643.    static long value, lv;
  644.    static char *p;
  645.    
  646.    value = 0L;
  647.    p = string + strlen(string);     /* end of string */
  648.    while (!isdigit(*p))
  649.       p--;
  650.    for (lv = 1L; isdigit(*p) && p >= string; lv *= 10)
  651.       value += ((*p--) - '0') * lv;
  652.    return value;
  653. }
  654.  
  655. rlabel() /*print receive mode labels on the 25th line*/
  656. {
  657.    putlabel("RECEIVE FILE Mode:  Press ESC to Abort...");
  658. }
  659.  
  660. /************************** END OF MODULE 5 *********************************/
  661.