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

  1. /*  Utils.c: Miscellaneous support routines for xprzmodem.library;
  2.     Version 2.0, 28 October 1989, by Rick Huebner.
  3.     Released to the Public Domain; do as you like with this code.  */
  4.  
  5.  
  6. #include <proto/all.h>
  7. #include <exec/types.h>
  8. #include <exec/memory.h>
  9. #include <ctype.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include "xproto.h"
  14. #include "zmodem.h"
  15. #include "xprzmodem.h"
  16.  
  17. /* Transfer options to use if XProtocolSetup not called */
  18. struct SetupVars Default_Config = {
  19.   NULL, NULL, 0,
  20.   { "C" }, { "N" }, { "16" }, { "0" }, { "N" },
  21.   { "N" }, { "Y" }, { "N" }, { "Y" }, { "" }
  22. };
  23.  
  24. #ifdef DEBUGLOG
  25. UBYTE DebugName[] = "Log:ZDebug.log";
  26. void *DebugLog = NULL;
  27. #endif
  28.  
  29.  
  30. /* Called by comm program to set transfer options */
  31. long __saveds XProtocolSetup(struct XPR_IO *io) {
  32.   struct SetupVars *sv, tempsv;
  33.   struct xpr_option *option_ptrs[11], *optr, xo_hdr, xo_t, xo_o, xo_b, xo_f, xo_s, xo_r, xo_a, xo_d, xo_k, xo_p;
  34.   UBYTE buf[256], *p;
  35.   long i, len;
  36.  
  37.   /* Allocate memory for transfer options string */
  38.   if (!(sv = io->xpr_data)) {
  39.     io->xpr_data = AllocMem((long)sizeof(struct SetupVars),MEMF_CLEAR);
  40.     if (!(sv = io->xpr_data)) {
  41.       ioerr(io,"Not enough memory");
  42.       return XPRS_FAILURE;
  43.     }
  44.     /* Start out with default options; merge user changes into defaults */
  45.     *sv = Default_Config;
  46.   }
  47.  
  48.   /* If options string passed by comm prog, use it; else prompt user */
  49.   if (io->xpr_filename) strcpy(buf,io->xpr_filename);
  50.   else {
  51.     /* If xpr_options() implemented by comm program, use it */
  52.     if (io->xpr_extension >= 1 && io->xpr_options) {
  53.       /* Let user edit temp copy of options so we can ignore invalid entries */
  54.       /* Have to init all this crud the hard way 'cause it's got to be on the
  55.          stack in order to maintain reentrancy */
  56.       tempsv = *sv;
  57.       xo_hdr.xpro_description = "ZModem options:";
  58.       xo_hdr.xpro_type = XPRO_HEADER;
  59.       xo_hdr.xpro_value = NULL;
  60.       xo_hdr.xpro_length = 0;
  61.       option_ptrs[0] = &xo_hdr;
  62.       xo_t.xpro_description = "Text mode (Y,N,?,C):";
  63.       xo_t.xpro_type = XPRO_STRING;
  64.       xo_t.xpro_value = tempsv.option_t;
  65.       xo_t.xpro_length = sizeof(tempsv.option_t);
  66.       option_ptrs[1] = &xo_t;
  67.       xo_o.xpro_description = "Overwrite mode (Y,N,R,S):";
  68.       xo_o.xpro_type = XPRO_STRING;
  69.       xo_o.xpro_value = tempsv.option_o;
  70.       xo_o.xpro_length = sizeof(tempsv.option_o);
  71.       option_ptrs[2] = &xo_o;
  72.       xo_b.xpro_description = "I/O buffer size (KB):";
  73.       xo_b.xpro_type = XPRO_LONG;
  74.       xo_b.xpro_value = tempsv.option_b;
  75.       xo_b.xpro_length = sizeof(tempsv.option_b);
  76.       option_ptrs[3] = &xo_b;
  77.       xo_f.xpro_description = "Frame size (bytes):";
  78.       xo_f.xpro_type = XPRO_LONG;
  79.       xo_f.xpro_value = tempsv.option_f;
  80.       xo_f.xpro_length = sizeof(tempsv.option_f);
  81.       option_ptrs[4] = &xo_f;
  82.       xo_a.xpro_description = "Auto-activate receiver:";
  83.       xo_a.xpro_type = XPRO_BOOLEAN;
  84.       xo_a.xpro_value = tempsv.option_a;
  85.       xo_a.xpro_length = sizeof(tempsv.option_a);
  86.       option_ptrs[5] = &xo_a;
  87.       xo_d.xpro_description = "Delete after sending:";
  88.       xo_d.xpro_type = XPRO_BOOLEAN;
  89.       xo_d.xpro_value = tempsv.option_d;
  90.       xo_d.xpro_length = sizeof(tempsv.option_d);
  91.       option_ptrs[6] = &xo_d;
  92.       xo_k.xpro_description = "Keep partial files:";
  93.       xo_k.xpro_type = XPRO_BOOLEAN;
  94.       xo_k.xpro_value = tempsv.option_k;
  95.       xo_k.xpro_length = sizeof(tempsv.option_k);
  96.       option_ptrs[7] = &xo_k;
  97.       xo_s.xpro_description = "Send full path:";
  98.       xo_s.xpro_type = XPRO_BOOLEAN;
  99.       xo_s.xpro_value = tempsv.option_s;
  100.       xo_s.xpro_length = sizeof(tempsv.option_s);
  101.       option_ptrs[8] = &xo_s;
  102.       xo_r.xpro_description = "Use received path:";
  103.       xo_r.xpro_type = XPRO_BOOLEAN;
  104.       xo_r.xpro_value = tempsv.option_r;
  105.       xo_r.xpro_length = sizeof(tempsv.option_r);
  106.       option_ptrs[9] = &xo_r;
  107.       xo_p.xpro_description = "Default receive path:";
  108.       xo_p.xpro_type = XPRO_STRING;
  109.       xo_p.xpro_value = tempsv.option_p;
  110.       xo_p.xpro_length = sizeof(tempsv.option_p);
  111.       option_ptrs[10] = &xo_p;
  112.       /* Convert Y/N used elsewhere into "yes"/"no" required by spec */
  113.       for (i=5; i<=9; ++i) {
  114.         optr = option_ptrs[i];
  115.         strcpy(optr->xpro_value,(*optr->xpro_value == 'Y') ? "yes" : "no");
  116.       }
  117.       (*io->xpr_options)(11L,option_ptrs);
  118.       /* Convert "yes"/"no" or "on"/"off" into Y/N */
  119.       for (i=5; i<=9; ++i) {
  120.         optr = option_ptrs[i];
  121.         strcpy(optr->xpro_value,(!stricmp(optr->xpro_value,"yes") || !stricmp(optr->xpro_value,"on")) ? "Y" : "N");
  122.       }
  123.       /* Convert xpr_options() results into parseable options string */
  124.       sprintf(buf,"T%s,O%s,B%s,F%s,A%s,D%s,K%s,S%s,R%s,P%s",tempsv.option_t,tempsv.option_o,tempsv.option_b,tempsv.option_f,
  125.         tempsv.option_a,tempsv.option_d,tempsv.option_k,tempsv.option_s,tempsv.option_r,tempsv.option_p);
  126.     /* If xpr_options() not provided, try xpr_gets() instead */
  127.     } else {
  128.       /* Start buffer with current settings so user can see/edit them in place */
  129.       sprintf(buf,"T%s,O%s,B%s,F%s,A%s,D%s,K%s,S%s,R%s,P%s",sv->option_t,sv->option_o,sv->option_b,sv->option_f,
  130.         sv->option_a,sv->option_d,sv->option_k,sv->option_s,sv->option_r,sv->option_p);
  131.       if (io->xpr_gets) (*io->xpr_gets)("ZModem options:",buf);
  132.     }
  133.   }
  134.   /* Upshift options string for easier parsing */
  135.   strupr(buf);
  136.  
  137.   /* Merge new T(ext) option into current settings if given */
  138.   /* "TY" = Force Text mode on,
  139.      "TN" = Force Text mode off,
  140.      "T?" = Use other end's text mode suggestion (default to binary)
  141.      "TC" = Ask Comm program for file type */
  142.   if (p = find_option(buf,'T')) {
  143.     if (*p == 'Y' || *p == 'N' || *p == '?' || *p == 'C') *sv->option_t = *p;
  144.     else ioerr(io,"Invalid T flag ignored; should be Y, N, ?, or C");
  145.   }
  146.  
  147.   /* Merge new O(verwrite) option into current settings if given */
  148.   /* "OY" = Yes, delete old file and replace with new one,
  149.      "ON" = No, prevent overwrite by appending ".dup" to avoid name collision,
  150.      "OR" = Resume transfer at end of existing file,
  151.      "OS" = Skip file if it already exists; go on to next */
  152.   if (p = find_option(buf,'O')) {
  153.     if (*p == 'R' && !io->xpr_finfo) ioerr(io,"Can't Resume; xpr_finfo() not supported");
  154.     else if (*p == 'Y' || *p == 'N' || *p == 'R' || *p == 'S') *sv->option_o = *p;
  155.     else ioerr(io,"Invalid O flag ignored; should be Y, N, R, or S");
  156.   }
  157.  
  158.   /* Merge new B(uffer) setting into current settings if given */
  159.   /* Size of file I/O buffer in kilobytes */
  160.   if (p = find_option(buf,'B')) {
  161.     len = atol(p);
  162.     if (len < 1) len = 1;
  163.     sprintf(sv->option_b,"%ld",len);
  164.   }
  165.  
  166.   /* Merge new F(ramelength) setting into other settings if given */
  167.   /* Number of bytes we're willing to send or receive between ACKs.
  168.      0 = unlimited; nonstop streaming data */
  169.   if (p = find_option(buf,'F')) {
  170.     len = atol(p);
  171.     if (len < 0) len = 0;
  172.     if (len > 0 && len < MINBLOCK) len = MINBLOCK;
  173.     sprintf(sv->option_f,"%ld",len);
  174.   }
  175.  
  176.   /* Merge new A(uto-activate) setting into other settings if given */
  177.   /* "AY" = Automatically call XProtocolReceive() if ZRQINIT string received
  178.      "AN" = Don't look for ZRQINIT; user will explicitly activate receive */
  179.   if (p = find_option(buf,'A')) {
  180.     if (*p == 'Y' || *p == 'N') *sv->option_a = *p;
  181.     else ioerr(io,"Invalid A flag ignored; should be Y or N");
  182.   }
  183.  
  184.   /* Merge new D(elete after sending) setting into other options */
  185.   /* "DY" = Delete files after successfully sending them
  186.      "DN" = Don't delete files after sending */
  187.   if (p = find_option(buf,'D')) {
  188.     if (*p == 'Y' && (io->xpr_extension < 2 || !io->xpr_unlink))
  189.       ioerr(io,"Can't use DY; xpr_unlink() not supported");
  190.     else if (*p == 'Y' || *p == 'N') *sv->option_d = *p;
  191.     else ioerr(io,"Invalid D flag ignored; should be Y or N");
  192.   }
  193.  
  194.   /* Merge new K(eep partial files) setting into other options */
  195.   /* "KY" = Keep partially-received file fragments to allow later resumption
  196.      "KN" = Delete partially-received file fragments */
  197.   if (p = find_option(buf,'K')) {
  198.     if (*p == 'N' && (io->xpr_extension < 2 || !io->xpr_unlink))
  199.       ioerr(io,"Can't use KN; xpr_unlink() not supported");
  200.     else if (*p == 'Y' || *p == 'N') *sv->option_k = *p;
  201.     else ioerr(io,"Invalid K flag ignored; should be Y or N");
  202.   }
  203.  
  204.   /* Merge new S(end full path) setting into other options */
  205.   /* "SY" = Send full filename including directory path to receiver
  206.      "SN" = Send only simple filename portion, not including directory path */
  207.   if (p = find_option(buf,'S')) {
  208.     if (*p == 'Y' || *p == 'N') *sv->option_s = *p;
  209.     else ioerr(io,"Invalid S flag ignored; should be Y or N");
  210.   }
  211.  
  212.   /* Merge new R(eceive path) setting into other options */
  213.   /* "RY" = Use full filename exactly as received; don't use P option path
  214.      "RN" = Ignore received directory path if any; use path from P option */
  215.   if (p = find_option(buf,'R')) {
  216.     if (*p == 'Y' || *p == 'N') *sv->option_r = *p;
  217.     else ioerr(io,"Invalid R flag ignored; should be Y or N");
  218.   }
  219.  
  220.   /* Merge new P(ath) setting into other options */
  221.   /* "Pdir" = Receive files into directory "dir" if RN selected
  222.      "dir" can by any valid existing directory, with or without trailing "/" */
  223.   if (p = find_option(buf,'P')) {
  224.     strcpy(sv->option_p,p);
  225.     p = sv->option_p + strcspn(sv->option_p," ,\t\r\n");
  226.     *p = '\0';
  227.   }
  228.  
  229.   return (*sv->option_a == 'Y') ? XPRS_SUCCESS|XPRS_NORECREQ|XPRS_HOSTMON : XPRS_SUCCESS|XPRS_NORECREQ;
  230. }
  231.  
  232.  
  233. /* Called by comm program to give us a chance to clean up before program ends */
  234. long __saveds XProtocolCleanup(struct XPR_IO *io) {
  235.   /* Release option memory, if any */
  236.   if (io->xpr_data) {
  237.     FreeMem(io->xpr_data,(long)sizeof(struct SetupVars));
  238.     io->xpr_data = NULL;
  239.   }
  240.  
  241.   return XPRS_SUCCESS;
  242. }
  243.  
  244.  
  245.  
  246. /* Called by comm program upon our request (XPRS_HOSTMON) to let us monitor
  247.    the incoming data stream for our receiver auto-activation string (ZRQINIT packet).
  248.    We only ask for this to be called if option AY is set. */
  249. long __saveds XProtocolHostMon(struct XPR_IO *io,UBYTE *serbuff,long actual,long maxsize) {
  250.   static UBYTE startrcv[] = { ZPAD, ZDLE, ZHEX, "00" };
  251.   struct SetupVars *sv;
  252.  
  253.   if (!(sv = io->xpr_data)) return actual;  /* XProtocolSetup() never called?! */
  254.   if (!sv->matchptr) sv->matchptr = startrcv;
  255.  
  256.   /* Scan through serbuff to see if we can match all bytes in the start string in sequence */
  257.   for (sv->bufpos=serbuff; sv->bufpos < serbuff+actual; ++sv->bufpos) {
  258.     if (*sv->bufpos == *sv->matchptr) {  /* if data matches current position in match string */
  259.       ++sv->matchptr;          /* increment match position */
  260.       if (!*sv->matchptr) {    /* if at end of match string, it all matched */
  261.         sv->matchptr = startrcv;
  262.         sv->buflen = actual - (sv->bufpos - serbuff);
  263.         XProtocolReceive(io);
  264.         actual = 0;            /* serbuff contents probably trashed by Receive() */
  265.         break;
  266.       }
  267.     } else if (sv->matchptr > startrcv) { /* if mismatch, reset to start of match string */
  268.       sv->matchptr = startrcv;
  269.       if (*sv->bufpos == *sv->matchptr) ++sv->matchptr;
  270.     }
  271.   }
  272.  
  273.   sv->bufpos = NULL;
  274.   return actual;
  275. }
  276.  
  277.  
  278. /* Called by comm program to let us monitor user's inputs; we never ask for
  279.    this to be called, but it's better to recover gracefully than guru the machine */
  280. long __saveds XProtocolUserMon(struct XPR_IO *io,UBYTE *serbuff,long actual,long maxsize) {
  281.   return actual;
  282. }
  283.  
  284.  
  285.  
  286. /* Perform setup and initializations common to both Send and Receive routines */
  287. struct Vars *setup(struct XPR_IO *io) {
  288.   static long bauds[] = { 110,300,1200,2400,4800,9600,19200,31250,38400,57600,76800,115200 };
  289.   struct SetupVars *sv;
  290.   struct Vars *v;
  291.   long origbuf, newstatus;
  292. #ifdef DEBUGLOG
  293.   long i, *lng;
  294. #endif
  295.  
  296.   /* Make sure comm program supports the required call-back functions */
  297.   if (!io->xpr_update) return NULL;
  298.   if (!io->xpr_fopen || !io->xpr_fclose || !io->xpr_fread || !io->xpr_fwrite ||
  299.       !io->xpr_fseek || !io->xpr_sread || !io->xpr_swrite) {
  300.     ioerr(io,"Comm prog missing required function(s); see docs");
  301.     return NULL;
  302.   }
  303.  
  304.   /* Hook in default transfer options if XProtocolSetup wasn't called */
  305.   if (!(sv = io->xpr_data)) {
  306.     io->xpr_data = AllocMem((long)sizeof(struct SetupVars),MEMF_CLEAR);
  307.     if (!(sv = io->xpr_data)) {
  308.       ioerr(io,"Not enough memory");
  309.       return NULL;
  310.     }
  311.     *sv = Default_Config;
  312.   }
  313.  
  314.   /* Allocate memory for our unshared variables, to provide reentrancy */  
  315.   if (!(v = AllocMem((long)sizeof(struct Vars),MEMF_CLEAR))) {
  316. nomem:
  317.     ioerr(io,"Not enough memory");
  318.     return NULL;
  319.   }
  320.  
  321.   /* Allocate memory for our file I/O buffer; if we can't get as much as
  322.      requested, keep asking for less until we hit minimum before giving up */
  323.   v->Filebufmax = origbuf = atol(sv->option_b) * 1024;
  324.   while (!(v->Filebuf = AllocMem(v->Filebufmax,0L))) {
  325.     if (v->Filebufmax > 1024) v->Filebufmax -= 1024;
  326.     else {
  327.       FreeMem(v,(long)sizeof(struct Vars));
  328.       goto nomem;
  329.     }
  330.   }
  331.  
  332.   /* If framelength was intended to match buffer size, stay in sync */
  333.   v->Tframlen = atol(sv->option_f);
  334.   if (v->Tframlen && v->Tframlen == origbuf) v->Tframlen = v->Filebufmax;
  335.   
  336.   /* Copy caller's io struct into our Vars for easier passing */
  337.   v->io = *io;
  338.  
  339. #ifdef DEBUGLOG
  340.   if (!DebugLog) DebugLog = (*v->io.xpr_fopen)(DebugName,"w");
  341.   dlog(v,"XPR_IO struct:\n");
  342.   for (i=0,lng=(long *)io; i < sizeof(struct XPR_IO)/4; ++i) {
  343.     sprintf(v->Msgbuf,"  %08lx\n",*lng++);
  344.     dlog(v,v->Msgbuf);
  345.   }
  346. #endif
  347.  
  348.   /* Get baud rate; set serial port mode if necessary (and possible) */  
  349.   if (v->io.xpr_setserial) {
  350.     v->Oldstatus = (*v->io.xpr_setserial)(-1L);
  351.     /* ZModem requires 8 data bits, no parity (full transparency), 
  352.        leave other settings alone */
  353.     newstatus = v->Oldstatus & 0xFFFFE0BC;
  354.     /* newstatus |= on_flags; Here's where we'd turn bits on if we needed to */
  355.     if (newstatus != v->Oldstatus) (*v->io.xpr_setserial)(newstatus);
  356.     v->Baud = bauds[(newstatus>>16) & 0xFF];
  357. #ifdef DEBUGLOG
  358.     sprintf(v->Msgbuf,"Old serial status = %lx, new = %lx, baud = %ld\n",v->Oldstatus,newstatus,v->Baud);
  359.     dlog(v,v->Msgbuf);
  360. #endif
  361.   /* If no xpr_setserial(), muddle along with most likely guess */
  362.   } else v->Baud = 2400;
  363.  
  364.   return v;
  365. }
  366.  
  367.  
  368.  
  369. /* Set text/binary mode flags in accordance with T option setting */
  370. void set_textmode(struct Vars *v) {
  371.   struct SetupVars *sv;
  372.   long i;
  373.  
  374.   sv = v->io.xpr_data;
  375.   switch(*sv->option_t) {
  376.     case 'Y':  /* Force text mode on receive; suggest text mode on send */
  377. TY:   v->Rxascii = TRUE;
  378.       v->Rxbinary = FALSE;
  379.       v->Lzconv = ZCNL;
  380.       break;
  381.     case 'N':  /* Force binary mode on receive; suggest binary mode on send */
  382. TN:   v->Rxascii = FALSE;
  383.       v->Rxbinary = TRUE;
  384.       v->Lzconv = ZCBIN;
  385.       break;
  386.     case 'C':  /* Ask comm program for proper mode for this file */
  387.       if (v->io.xpr_finfo) {
  388.         i = (*v->io.xpr_finfo)(v->Filename,2L);
  389.         if (i == 1) goto TN;  /* Comm program says use binary mode */
  390.         if (i == 2) goto TY;  /* Comm program says use text mode */
  391.       }
  392.       /* xpr_finfo() not provided (or failed); default to T? */
  393.     case '?':
  394.       v->Rxascii = v->Rxbinary = FALSE;
  395.       v->Lzconv = 0;
  396.       break;
  397.   }
  398. }
  399.  
  400.  
  401.  
  402. /* Search for specified option setting in string */
  403. UBYTE *find_option(UBYTE *buf,UBYTE option) {
  404.   while (*buf) {
  405.     buf += strspn(buf," ,\t\r\n");
  406.     if (*buf == option) return ++buf;
  407.     buf += strcspn(buf," ,\t\r\n");
  408.   }
  409.  
  410.   return NULL;
  411. }
  412.  
  413.  
  414.  
  415. /* send cancel string to get the other end to shut up */
  416. void canit(struct Vars *v) {
  417.   static char canistr[] = { 24,24,24,24,24,24,24,24,24,24,8,8,8,8,8,8,8,8,8,8,0 };
  418.  
  419.   zmputs(v,canistr);
  420. }
  421.  
  422.  
  423.  
  424. /* Send a string to the modem, with processing for \336 (sleep 1 sec)
  425.    and \335 (break signal, ignored since XPR spec doesn't support it) */
  426. void zmputs(struct Vars *v,UBYTE *s) {
  427.   UBYTE c;
  428.  
  429.   while (*s) {
  430.     switch (c = *s++) {
  431.       case '\336':
  432.         TimeOut(50L);
  433.       case '\335':
  434.         break;
  435.       default:
  436.         sendline(v,c);
  437.     }
  438.   }
  439. }
  440.  
  441.  
  442.  
  443. /* Write one character to the modem */
  444. void xsendline(struct Vars *v,UBYTE c) {
  445.   (*v->io.xpr_swrite)(&c,1L);
  446. }
  447.  
  448.  
  449. /* Get a byte from the modem;
  450.    return TIMEOUT if no read within timeout tenths of a second,
  451.    return RCDO if carrier lost or other fatal error (sread returns -1).
  452.    Added in some buffering so we wouldn't hammer the system with single-byte
  453.    serial port reads.  Also, the buffering makes char_avail() a lot easier to implement. */
  454. short readock(struct Vars *v,short tenths) {
  455.   /* If there's data waiting in our buffer, return next byte */
  456.   if (v->Modemcount) {
  457. gotdata:
  458.     --v->Modemcount;
  459.     return (short)(*v->Modemchar++);
  460.   }
  461.  
  462.   /* Our buffer is empty; see if there's anything waiting in system buffer */
  463.   v->Modemcount = (*v->io.xpr_sread)(v->Modembuf,(long)sizeof(v->Modembuf),0L);
  464.   if (v->Modemcount < 0) {  /* Carrier dropped or other fatal error; abort */
  465.     v->Modemcount = 0;
  466.     return RCDO;
  467.   } else if (!v->Modemcount) {   /* Nothing in system buffer; try waiting */
  468.     v->Modemcount = (*v->io.xpr_sread)(v->Modembuf,1L,tenths*100000L);
  469.     if (v->Modemcount < 0) {
  470.       v->Modemcount = 0;
  471.       return RCDO;
  472.     } else if (!v->Modemcount) return TIMEOUT;  /* Nothing received in time */
  473.   }
  474.   v->Modemchar = v->Modembuf;  /* Reset buffer pointer to start of data */
  475.   goto gotdata;
  476. }
  477.  
  478.  
  479.  
  480. /* Check if there's anything available to read from the modem */
  481. char char_avail(struct Vars *v) {
  482.   if (v->Modemcount) return TRUE;
  483.  
  484.   /* No data in our buffer; check system's input buffer */  
  485.   v->Modemcount = (*v->io.xpr_sread)(v->Modembuf,(long)sizeof(v->Modembuf),0L);
  486.   if (v->Modemcount < 1) {  /* Nothing in system buffer either */
  487.     v->Modemcount = 0;
  488.     return FALSE;
  489.   } else {                  /* System buffer had something waiting for us */
  490.     v->Modemchar = v->Modembuf;
  491.     return TRUE;
  492.   }
  493. }
  494.  
  495.  
  496.  
  497. /* Update the elapsed time, expected total time, and effective data
  498.    transfer rate values for status display */
  499. void update_rate(struct Vars *v) {
  500.   long sent, elapsed, expect;
  501.   short hr, min;
  502.  
  503.   /* Compute effective data rate so far in characters per second */
  504.   sent = v->xpru.xpru_bytes - v->Strtpos; /* Actual number of chars transferred */
  505.   elapsed = GetSysTime(NULL) - v->Starttime;    /* Time it took to send them */
  506.   if (elapsed < 1) elapsed = 1;
  507.   /* If we haven't transferred anything yet (just starting), make reasonable
  508.      guess (95% throughput); otherwise, compute actual effective transfer rate */
  509.   v->xpru.xpru_datarate = (sent) ? sent / elapsed : v->Baud * 95L / 1000;
  510.  
  511.   /* Compute expected total transfer time based on data rate so far */
  512.   if (v->xpru.xpru_filesize < 0) expect = 0; /* Don't know filesize; display time=0 */
  513.   else expect = (v->xpru.xpru_filesize - v->Strtpos) / v->xpru.xpru_datarate;
  514.   hr = expect / (60*60);   /* How many whole hours */
  515.   expect -= hr * (60*60);  /* Remainder not counting hours */
  516.   min = expect / 60;       /* How many whole minutes */
  517.   expect -= min * 60;      /* Remaining seconds */
  518.   sprintf(v->Msgbuf,"%02ld:%02ld:%02ld",(long)hr,(long)min,expect);
  519.   v->xpru.xpru_expecttime = (char *)v->Msgbuf;
  520.  
  521.   /* Compute elapsed time for this transfer so far */
  522.   hr = elapsed / (60*60);
  523.   elapsed -= hr * (60*60);
  524.   min = elapsed / 60;
  525.   elapsed -= min * 60;
  526.   sprintf(v->Msgbuf+20,"%02ld:%02ld:%02ld",(long)hr,(long)min,elapsed);
  527.   v->xpru.xpru_elapsedtime = (char *)v->Msgbuf+20;
  528. }
  529.  
  530.  
  531.  
  532. /* Buffered file I/O fopen() interface routine */
  533. void *bfopen(struct Vars *v,UBYTE *mode) {
  534.   /* Initialize file-handling variables */
  535.   v->Filebufpos = v->Filebuflen = v->Filebufcnt = 0;
  536.   v->Fileflush = FALSE;
  537.   v->Filebufptr = v->Filebuf;
  538.   /* Open the file */
  539. #ifdef DEBUGLOG
  540.   sprintf(v->Msgbuf,"bfopen: %s %s\n",v->Filename,mode);
  541.   dlog(v,v->Msgbuf);
  542. #endif
  543.   return (*v->io.xpr_fopen)(v->Filename,mode);
  544. }
  545.  
  546.  
  547.  
  548. /* Buffered file I/O fclose() interface routine */
  549. void bfclose(struct Vars *v) {
  550.   /* If bfwrite() left data lingering in buffer, flush it out before closing */
  551.   if (v->Fileflush) (*v->io.xpr_fwrite)(v->Filebuf,1L,v->Filebufcnt,v->File);
  552.   /* Close the file */
  553.   (*v->io.xpr_fclose)(v->File);
  554.   v->File = NULL;
  555. }
  556.  
  557.  
  558.  
  559. /* Buffered file I/O fseek() interface routine */
  560. void bfseek(struct Vars *v,long pos) {
  561.   long offset;
  562.  
  563.   /* If new file position is within currently buffered section, reset pointers */
  564.   if (pos >= v->Filebufpos && pos < v->Filebufpos + v->Filebuflen) {
  565.     offset = pos - v->Filebufpos;
  566.     v->Filebufptr = v->Filebuf + offset;
  567.     v->Filebufcnt = v->Filebuflen - offset;
  568.   /* Otherwise, fseek() file and discard buffer contents to force new read */
  569.   } else {
  570.     (*v->io.xpr_fseek)(v->File,pos,0L);
  571.     v->Filebuflen = v->Filebufcnt = 0;
  572.     v->Filebufpos = pos;
  573.   }
  574. }
  575.  
  576.  
  577.  
  578. /* Buffered file I/O fread() interface routine */
  579. long bfread(struct Vars *v,UBYTE *buf,long length) {
  580.   long count, total = 0;
  581.  
  582.   /* Keep going until entire request completed */
  583.   while (length > 0) {
  584.     /* Copy as much of the request as possible from the buffer */
  585.     count = (length <= v->Filebufcnt) ? length : v->Filebufcnt;
  586.     CopyMem(v->Filebufptr,buf,count);
  587. #ifdef DEBUGLOG
  588.     sprintf(v->Msgbuf,"bfread got %ld bytes from buffer\n",count);
  589.     dlog(v,v->Msgbuf);
  590. #endif
  591.     buf += count;
  592.     total += count;
  593.     length -= count;
  594.     v->Filebufptr += count;
  595.     v->Filebufcnt -= count;
  596.  
  597.     /* If we've emptied the buffer, read next buffer's worth */
  598.     if (!v->Filebufcnt) {
  599.       v->Filebufpos += v->Filebuflen;
  600.       v->Filebufptr = v->Filebuf;
  601.       v->Filebufcnt = v->Filebuflen = (*v->io.xpr_fread)(v->Filebuf,1L,v->Filebufmax,v->File);
  602. #ifdef DEBUGLOG
  603.       sprintf(v->Msgbuf,"bfread read %ld bytes\n",v->Filebufcnt);
  604.       dlog(v,v->Msgbuf);
  605. #endif
  606.       /* If we hit the EOF, return with however much we read so far */
  607.       if (!v->Filebufcnt) break;
  608.     }
  609.   }
  610.  
  611.   return total;
  612. }
  613.  
  614.  
  615.  
  616. /* Buffered file I/O fwrite() interface routine */
  617. long bfwrite(struct Vars *v,UBYTE *buf,long length) {
  618.   long count, total = 0;
  619.  
  620.   /* Keep going until entire request completed */
  621.   while (length > 0) {
  622.     /* Copy as much as will fit into the buffer */
  623.     count = v->Filebufmax - v->Filebufcnt;
  624.     if (length < count) count = length;
  625.     CopyMem(buf,v->Filebufptr,count);
  626. #ifdef DEBUGLOG
  627.     sprintf(v->Msgbuf,"bfwrite buffered %ld bytes\n",count);
  628.     dlog(v,v->Msgbuf);
  629. #endif
  630.     buf += count;
  631.     total += count;
  632.     length -= count;
  633.     v->Filebufptr += count;
  634.     v->Filebufcnt += count;
  635.     v->Fileflush = TRUE;
  636.  
  637.     /* If we've filled the buffer, write it out */
  638.     if (v->Filebufcnt == v->Filebufmax) {
  639.       count = (*v->io.xpr_fwrite)(v->Filebuf,1L,v->Filebufcnt,v->File);
  640. #ifdef DEBUGLOG
  641.       sprintf(v->Msgbuf,"bfwrite wrote %ld bytes\n",count);
  642.       dlog(v,v->Msgbuf);
  643. #endif
  644.       if (count < v->Filebufcnt) return -1;
  645.       v->Filebufptr = v->Filebuf;
  646.       v->Filebufcnt = 0;
  647.       v->Fileflush = FALSE;
  648.     }
  649.   }
  650.  
  651.   return total;
  652. }
  653.  
  654.     
  655.  
  656. /* Have the comm program display an error message for us, using a
  657.    temporary XPR_UPDATE structure; used to display errors before Vars 
  658.    gets allocated */
  659. void ioerr(struct XPR_IO *io,char *msg) {
  660.   struct XPR_UPDATE xpru;
  661.  
  662.   if (io->xpr_update) {
  663.     xpru.xpru_updatemask = XPRU_ERRORMSG;
  664.     xpru.xpru_errormsg = msg;
  665.     (*io->xpr_update)(&xpru);
  666.   }
  667. }
  668.  
  669.  
  670.  
  671. /* Have the comm program display an error message for us, using the
  672.    normal XPR_IO structure allocated in Vars */
  673. void upderr(struct Vars *v,char *msg) {
  674.   v->xpru.xpru_updatemask = XPRU_ERRORMSG;
  675.   v->xpru.xpru_errormsg = msg;
  676.   (*v->io.xpr_update)(&v->xpru);
  677. #ifdef DEBUGLOG
  678.   dlog(v,msg);
  679.   dlog(v,"\n");
  680. #endif
  681. }
  682.  
  683.  
  684.  
  685. /* Have the comm program display a normal message for us */
  686. void updmsg(struct Vars *v,char *msg) {
  687.   v->xpru.xpru_updatemask = XPRU_MSG;
  688.   v->xpru.xpru_msg = msg;
  689.   (*v->io.xpr_update)(&v->xpru);
  690. #ifdef DEBUGLOG
  691.   dlog(v,msg);
  692.   dlog(v,"\n");
  693. #endif
  694. }
  695.  
  696.  
  697.  
  698. /* Figure out how many bytes are free on the drive we're uploading to.
  699.    Stubbed out for now; not supported by XPR spec. */
  700. long getfree(void) {
  701.   return 0x7FFFFFFF;
  702. }
  703.  
  704.  
  705.  
  706. /* Check whether file already exists; used to detect potential overwrites */
  707. char exist(struct Vars *v) {
  708.   void *file;
  709.  
  710.   file = (*v->io.xpr_fopen)(v->Filename,"r");
  711.   if (file) {
  712.     (*v->io.xpr_fclose)(file);
  713.     return TRUE;
  714.   } else return FALSE;
  715. }
  716.  
  717.  
  718.  
  719. #ifdef DEBUGLOG
  720. /* Write a message to the debug log */
  721. void dlog(struct Vars *v,UBYTE *s) {
  722.   /* Open the debug log if it isn't already open */
  723.   if (!DebugLog) DebugLog = (*v->io.xpr_fopen)(DebugName,"a");
  724.   (*v->io.xpr_fwrite)(s,1L,(long)strlen(s),DebugLog);
  725.   /* Close file to flush output buffer; comment these two lines out if
  726.      you aren't crashing your system and don't mind waiting until the
  727.      transfer finishes to look at your log file.
  728.   (*v->io.xpr_fclose)(DebugLog);
  729.   DebugLog = NULL; */
  730. }
  731. #endif
  732.