home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / old / ckermit4f / ckcfns.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  41KB  |  1,294 lines

  1. char *fnsv = "C-Kermit functions, 4F(058) 14 Jul 89";
  2.  
  3. /*  C K C F N S  --  System-independent Kermit protocol support functions.  */
  4.  
  5. /*  ...Part 1 (others moved to ckcfn2 to make this module small enough) */
  6.  
  7. /*
  8.  Author: Frank da Cruz (fdc@columbia.edu, FDCCU@CUVMA.BITNET),
  9.  Columbia University Center for Computing Activities.
  10.  First released January 1985.
  11.  Copyright (C) 1985, 1989, Trustees of Columbia University in the City of New 
  12.  York.  Permission is granted to any individual or institution to use, copy, or
  13.  redistribute this software so long as it is not sold for profit, provided this
  14.  copyright notice is retained. 
  15. */
  16. /*
  17.  System-dependent primitives defined in:
  18.  
  19.    ck?tio.c -- terminal i/o
  20.    cx?fio.c -- file i/o, directory structure
  21. */
  22. #include "ckcsym.h"            /* Need this for Mac */
  23. #include "ckcker.h"            /* Symbol definitions for Kermit */
  24. #include "ckcdeb.h"            /* Debug formats, typedefs, etc. */
  25.  
  26. #ifndef NULL
  27. #define NULL 0
  28. #endif
  29.  
  30. /* Externals from ckcmai.c */
  31. extern int spsiz, spmax, rpsiz, timint, srvtim, rtimo, npad, ebq, ebqflg,
  32.  rpt, rptq, rptflg, capas, keep;
  33. extern int pktnum, prvpkt, sndtyp, bctr, bctu, fmask,
  34.  size, osize, maxsize, spktl, nfils, stdouf, warn, timef, spsizf;
  35. extern int parity, speed, turn, turnch, 
  36.  delay, displa, pktlog, tralog, seslog, xflg, mypadn;
  37. extern long filcnt, ffc, flci, flco, tlci, tlco, tfc, fsize;
  38. extern int tsecs;
  39. extern int deblog, hcflg, binary, savmod, fncnv, local, server, cxseen, czseen;
  40. extern int nakstate;
  41. extern int rq, rqf, sq, wslots, urpsiz, rln;
  42. extern int atcapr, atcapb, atcapu;
  43. extern int lpcapr, lpcapb, lpcapu;
  44. extern int swcapr, swcapb, swcapu;
  45. extern int bsave, bsavef;
  46. extern int numerrs;
  47. extern CHAR padch, mypadc, eol, seol, ctlq, myctlq, sstate;
  48. extern CHAR filnam[], sndpkt[], recpkt[], data[], srvcmd[], stchr, mystch;
  49. extern char *cmarg, *cmarg2, *hlptxt, **cmlist;
  50. extern CHAR *srvptr;
  51. extern char *rdatap;
  52. long zchki();
  53. char *strcpy();
  54. CHAR *rpar();
  55.  
  56. /* (PWP) external def. of things used in buffered file input and output */
  57. extern CHAR zinbuffer[], zoutbuffer[];
  58. extern CHAR *zinptr, *zoutptr;
  59. extern int zincnt, zoutcnt;
  60.  
  61. /* Variables local to this module */
  62.  
  63. static char *memptr;            /* Pointer for memory strings */
  64.  
  65. static char cmdstr[100];        /* Unix system command string */
  66.  
  67. static int  sndsrc;            /* Flag for where to send from: */
  68.                     /* -1: name in cmdata */
  69.                     /*  0: stdin          */
  70.                     /* >0: list in cmlist */
  71.  
  72. static int n_len;        /* (PWP) packet encode-ahead length (& flag) */
  73.                 /* if < 0, no pre-encoded data */
  74.  
  75. static int  memstr,            /* Flag for input from memory string */
  76.     first;                /* Flag for first char from input */
  77. static CHAR t,                /* Current character */
  78.     next;                /* Next character */
  79. #ifdef datageneral
  80. extern int quiet;
  81. #endif
  82.  
  83. /*  E N C S T R  --  Encode a string from memory. */
  84.  
  85. /*  Call this instead of getpkt() if source is a string, rather than a file. */
  86.  
  87. encstr(s) char* s; {
  88.     int m; char *p;
  89.  
  90.     m = memstr; p = memptr;        /* Save these. */
  91.  
  92.     memptr = s;                /* Point to the string. */
  93.     memstr = 1;                /* Flag memory string as source. */
  94.     first = 1;                /* Initialize character lookahead. */
  95.     getpkt(spsiz-bctu-3);        /* Fill a packet from the string. */
  96.     memstr = m;                /* Restore memory string flag */
  97.     memptr = p;                /* and pointer */
  98.     first = 1;                /* Put this back as we found it. */
  99. }
  100.  
  101. /* E N C O D E - Kermit packet encoding procedure */
  102.  
  103. encode(a) CHAR a; {            /* The current character */
  104.     int a7;                /* Low order 7 bits of character */
  105.     int b8;                /* 8th bit of character */
  106.  
  107.     if (rptflg)    {                   /* Repeat processing? */
  108.         if (a == next && (first == 0)) { /* Got a run... */
  109.         if (++rpt < 94)        /* Below max, just count */
  110.                 return;
  111.         else if (rpt == 94) {    /* Reached max, must dump */
  112.                 data[size++] = rptq;
  113.                 data[size++] = tochar(rpt);
  114.                 rpt = 0;
  115.         }
  116.         } else if (rpt == 1) {        /* Run broken, only 2? */
  117.             rpt = 0;            /* Yes, reset repeat flag & count. */
  118.         encode(a);            /* Do the character twice. */
  119.         if (size <= maxsize) osize = size;
  120.         rpt = 0;
  121.         encode(a);
  122.         return;
  123.     } else if (rpt > 1) {        /* More than two */
  124.             data[size++] = rptq;    /* Insert the repeat prefix */
  125.             data[size++] = tochar(++rpt); /* and count. */
  126.             rpt = 0;            /* Reset repeat counter. */
  127.         }
  128.     }
  129.     a7 = a & 0177;            /* Isolate ASCII part */
  130.     b8 = a & 0200;            /* and 8th (parity) bit. */
  131.  
  132.     if (ebqflg && b8) {            /* Do 8th bit prefix if necessary. */
  133.         data[size++] = ebq;
  134.         a = a7;
  135.     }
  136.     if ((a7 < SP) || (a7==DEL))    {    /* Do control prefix if necessary */
  137.         data[size++] = myctlq;
  138.     a = ctl(a);
  139.     }
  140.     if (a7 == myctlq)            /* Prefix the control prefix */
  141.         data[size++] = myctlq;
  142.  
  143.     if ((rptflg) && (a7 == rptq))    /* If it's the repeat prefix, */
  144.         data[size++] = myctlq;        /* quote it if doing repeat counts. */
  145.  
  146.     if ((ebqflg) && (a7 == ebq))    /* Prefix the 8th bit prefix */
  147.         data[size++] = myctlq;        /* if doing 8th-bit prefixes */
  148.  
  149.     data[size++] = a;            /* Finally, insert the character */
  150.     data[size] = '\0';            /* itself, and mark the end. */
  151. }
  152.  
  153. /*  Output functions passed to 'decode':  */
  154.  
  155. putsrv(c) register char c; {     /*  Put character in server command buffer  */
  156.     *srvptr++ = c;
  157.     *srvptr = '\0';    /* Make sure buffer is null-terminated */
  158.     return(0);
  159. }
  160.  
  161. puttrm(c) register char c; {     /*  Output character to console.  */
  162.     conoc(c);
  163.     return(0);
  164. }
  165.  
  166. putfil(c) register char c; {            /*  Output char to file. */
  167.     if (zchout(ZOFILE, c & fmask) < 0) {
  168.     czseen = 1;               /* If write error... */
  169.     debug(F101,"putfil zchout write error, setting czseen","",1);
  170.     return(-1);
  171.     }
  172.     return(0);
  173. }
  174.  
  175. /* D E C O D E  --  Kermit packet decoding procedure */
  176.  
  177. /* Call with string to be decoded and an output function. */
  178. /* Returns 0 on success, -1 on failure (e.g. disk full).  */
  179.  
  180. decode(buf,fn) register CHAR *buf; register int (*fn)(); {
  181.     register unsigned int a, a7, b8;    /* Low order 7 bits, and the 8th bit */
  182.     int x;
  183.  
  184.     rpt = 0;                /* Initialize repeat count. */
  185.  
  186.     while ((a = *buf++) != '\0') {
  187.     if (rptflg) {            /* Repeat processing? */
  188.         if (a == rptq) {        /* Yes, got a repeat prefix? */
  189.         rpt = xunchar(*buf++);    /* Yes, get the repeat count, */
  190.         a = *buf++;        /* and get the prefixed character. */
  191.         }
  192.     }
  193.     b8 = 0;                /* Check high order "8th" bit */
  194.     if (ebqflg) {            /* 8th-bit prefixing? */
  195.         if (a == ebq) {        /* Yes, got an 8th-bit prefix? */
  196.         b8 = 0200;        /* Yes, remember this, */
  197.         a = *buf++;        /* and get the prefixed character. */
  198.         }
  199.     }
  200.     if (a == ctlq) {        /* If control prefix, */
  201.         a  = *buf++;        /* get its operand. */
  202.         a7 = a & 0177;        /* Only look at low 7 bits. */
  203.         if ((a7 >= 0100 && a7 <= 0137) || a7 == '?') /* Uncontrollify */
  204.         a = ctl(a);            /* if in control range. */
  205.     }
  206.     a |= b8;            /* OR in the 8th bit */
  207.     if (rpt == 0) rpt = 1;        /* If no repeats, then one */
  208. #ifdef NLCHAR
  209.     if (!binary) {            /* If in text mode, */
  210.         if (a == CR) continue;    /* discard carriage returns, */
  211.             if (a == LF) a = NLCHAR;     /* convert LF to system's newline. */
  212.         }
  213. #endif
  214.     if (fn == putfil) { /* (PWP) speedup via buffered output and a macro */
  215.         for (; rpt > 0; rpt--) {    /* Output the char RPT times */
  216.         if ((x = zmchout(a)) < 0) { /* zmchout is a macro */
  217.             debug(F101,"decode zmchout","",x);
  218.             return(-1);
  219.         }
  220.         ffc++;            /* Count the character */
  221.         }
  222.     } else {            /* Not to the file */
  223.         for (; rpt > 0; rpt--) {    /* Output the char RPT times */
  224.         if ((*fn)(a) < 0) return(-1); /* Send to output function. */
  225.         }
  226.     }
  227.     }
  228.     return(0);
  229. }
  230.  
  231. /*  G E T P K T -- Fill a packet data field  */
  232.  
  233. /*
  234.  Gets characters from the current source -- file or memory string.
  235.  Encodes the data into the packet, filling the packet optimally.
  236.  Set first = 1 when calling for the first time on a given input stream
  237.  (string or file).
  238.  
  239.  Uses global variables:
  240.  t     -- current character.
  241.  first -- flag: 1 to start up, 0 for input in progress, -1 for EOF.
  242.  next  -- next character.
  243.  data  -- the packet data buffer.
  244.  size  -- number of characters in the data buffer.
  245.  
  246. Returns the size as value of the function, and also sets global size,
  247. and fills (and null-terminates) the global data array.  Returns 0 upon eof.
  248.  
  249. Rewritten by Paul W. Placeway (PWP) of Ohio State University, March 1989.
  250. Incorporates old getchx() and encode() inline to eliminate function calls,
  251. uses buffered input for much-improved efficiency, and clears up some
  252. confusion with line termination (CRLF vs LF vs CR).
  253. */
  254.  
  255. getpkt(bufmax) int bufmax; {        /* Fill one packet buffer */
  256.     register CHAR rt = t, rnext = next; /* register shadows of the globals */
  257.     register CHAR *dp, *odp, *p1, *p2;    /* pointers... */
  258.     register int x;            /* Loop index. */
  259.     register int a7;            /* Low 7 bits of character */
  260.     static CHAR leftover[6] = { '\0', '\0', '\0', '\0', '\0', '\0' };
  261.  
  262.     if (first == 1) {        /* If first time thru...  */
  263.     first = 0;        /* remember, */
  264.     *leftover = '\0';       /* discard any interrupted leftovers, */
  265.     /* get first character of file into t, watching out for null file */
  266.     if (memstr) {
  267.         if ((rt = *memptr++) == '\0') { /* end of string ==> EOF */
  268.         first = -1;
  269.             size = 0;
  270.         debug(F100,"getpkt: empty string","",0);
  271.         return (0);
  272.         }
  273.     } else {
  274.         if ((x = zminchar()) == -1) { /* End of file */
  275.         first = -1;
  276.         debug(F100,"getpkt: empty file","",0);
  277.             size = 0;
  278.         return (0);
  279.         }
  280.         ffc++;            /* Count a file character */
  281.         rt = x;
  282.     }
  283.     rt &= fmask;            /* bytesize mask */
  284.  
  285.     /* PWP: handling of NLCHAR is done later (in the while loop)... */
  286.  
  287.     } else if ((first == -1) && (*leftover == '\0')) /* EOF from last time? */
  288.         return(size = 0);
  289.  
  290.     /* Do any leftovers */
  291.  
  292.     dp = data;
  293.     for (p1 = leftover; (*dp = *p1) != '\0'; p1++, dp++) /* copy leftovers */
  294.         ;
  295.     *leftover = '\0';
  296.     if (first == -1) 
  297.       return(size = (dp - data));    /* Handle final leftovers */
  298.   
  299.     /* Now fill up the rest of the packet. */
  300.     rpt = 0;                /* Clear out any old repeat count. */
  301.     while (first > -1) {        /* Until EOF... */
  302.     if (memstr) {            /* get next character */
  303.         if ((rnext = *memptr++) == '\0') { /* end of string ==> EOF */
  304.         first = -1;        /* Flag eof for next time. */
  305.         } else {
  306.         rnext &= fmask;        /* Bytesize mask. */
  307.         }
  308.     } else {
  309.         if ((x = zminchar()) == -1) { /* End of file */
  310.         first = -1;        /* Flag eof for next time. */
  311.         } else {
  312.         rnext = x & fmask;    /* Bytesize mask. */
  313.         ffc++;            /* Count it */
  314.         }
  315.     }
  316.  
  317.     /* PWP: handling of NLCHAR is done in a bit...  */
  318.  
  319.     odp = dp;            /* Remember current position. */
  320.  
  321.     /* PWP: the encode() procedure, in-line (for speed) */
  322.     if (rptflg) {            /* Repeat processing? */
  323.         if (
  324. #ifdef NLCHAR
  325.         /*
  326.          * PWP: this is a bit esoteric, so bear with me.
  327.          * If the next char is really CRLF, then we cannot
  328.          * be doing a repeat (unless CR,CR,LF which becomes
  329.          * "~ <n-1> CR CR LF", which is OK but not most efficient).
  330.          * I just plain don't worry about this case.  The actual
  331.          * conversion from NL to CRLF is done after the rptflg if...
  332.          */
  333.         (binary || (rnext != NLCHAR)) &&
  334. #endif /* NLCHAR */
  335.         rt == rnext && (first == 0)) { /* Got a run... */
  336.         if (++rpt < 94) {    /* Below max, just count */
  337.             continue;        /* go back and get another */
  338.         }
  339.         else if (rpt == 94) {    /* Reached max, must dump */
  340.             *dp++ = rptq;
  341.             *dp++ = tochar(rpt);
  342.             rpt = 0;
  343.         }
  344.         } else if (rpt == 1) {    /* Run broken, only 2? */
  345.         /* 
  346.          * PWP: Must encode two characters.  This is handled
  347.          * later, with a bit of blue smoke and mirrors, after
  348.          * the first character is encoded.
  349.          */
  350.         } else if (rpt > 1) {    /* More than two */
  351.         *dp++ = rptq;        /* Insert the repeat prefix */
  352.         *dp++ = tochar(++rpt);    /* and count. */
  353.         rpt = 0;        /* Reset repeat counter. */
  354.         }
  355.     }
  356.  
  357. #ifdef NLCHAR
  358.     /*
  359.      * PWP: even more esoteric NLCHAR handling.  Remember, at
  360.      * this point t may still be the _system_ NLCHAR.  If so,
  361.      * we do it here.
  362.      */
  363.     if (!binary && (rt == NLCHAR)) {
  364.         *dp++ = myctlq;        /* just put in the encoding directly */
  365.         *dp++ = 'M';        /* == ctl(CR) */
  366.         if ((dp-data) <= maxsize) odp = dp;    /* check packet bounds */
  367.         rt = LF;
  368.     }
  369. #endif
  370.  
  371.     /* meta control stuff fixed by fdc */
  372.     a7 = rt & 0177;            /* Low 7 bits of character */
  373.     if (ebqflg && (rt & 0200)) {    /* Do 8th bit prefix if necessary. */
  374.         *dp++ = ebq;
  375.         rt = a7;
  376.     }
  377.     if ((a7 < SP) || (a7 == DEL)) { /* Do control prefix if necessary */
  378.         *dp++ = myctlq;
  379.         rt = ctl(rt);
  380.     }
  381.     if (a7 == myctlq)        /* Prefix the control prefix */
  382.         *dp++ = myctlq;
  383.  
  384.     if ((rptflg) && (a7 == rptq))    /* If it's the repeat prefix, */
  385.         *dp++ = myctlq;        /* quote it if doing repeat counts. */
  386.  
  387.     if ((ebqflg) && (a7 == ebq))    /* Prefix the 8th bit prefix */
  388.         *dp++ = myctlq;        /* if doing 8th-bit prefixes */
  389.  
  390.     *dp++ = rt;            /* Finally, insert the character */
  391.     
  392.     if (rpt == 1) {            /* Exactly two copies? */
  393.         rpt = 0;
  394.         p2 = dp;            /* save current size temporarily */
  395.         for (p1 = odp; p1 < p2; p1++) /* copy the old chars over again */
  396.         *dp++ = *p1;
  397.         if ((p2-data) <= maxsize) odp = p2; /* check packet bounds */
  398.     }
  399.     rt = rnext;            /* Next is now current. */
  400.     if ((dp-data) >= bufmax) {    /* If too big, save some for next. */
  401.         size = (dp-data);
  402.         *dp = '\0';            /* mark (current) the end. */
  403.         if ((dp-data) > bufmax) {    /* if packet is overfull */
  404.         for (p1 = leftover, p2=odp; (*p1 = *p2) != '\0'; p1++,p2++)
  405.             ;
  406.         debug(F111,"getpkt leftover",leftover,size);
  407.         debug(F101," osize","",(odp-data));
  408.         size = (odp-data);    /* Return truncated packet. */
  409.         *odp = '\0';        /* mark real end */
  410.         } else {            /* If the packet is exactly full, */
  411.         debug(F101,"getpkt exact fit","",size);
  412.         }
  413.         t = rt; next = rnext;    /* save for next time */
  414.         return(size);
  415.     }
  416.     }                    /* Otherwise, keep filling. */
  417.     size = (dp-data);
  418.     *dp = '\0';                /* mark (current) the end. */
  419.     debug(F111,"getpkt eof/eot",data,size); /* Fell thru before packet full, */
  420.     return(size);             /* return partially filled last packet. */
  421. }
  422.  
  423. /*  C A N N E D  --  Check if current file transfer cancelled */
  424.  
  425. canned(buf) char *buf; {
  426.     if (*buf == 'X') cxseen = 1;
  427.     if (*buf == 'Z') czseen = 1;
  428.     debug(F101,"canned: cxseen","",cxseen);
  429.     debug(F101," czseen","",czseen);
  430.     return((czseen || cxseen) ? 1 : 0);
  431. }
  432.  
  433. /*  R E S E T C  --  Reset per-transaction character counters */
  434. resetc() {
  435.     flci = flco = 0;
  436.     tfc = tlci = tlco = 0;    /* Total file chars, line chars in & out */
  437. }
  438.  
  439.  
  440. /*  T I N I T  --  Initialize a transaction  */
  441.  
  442. tinit() {
  443.     xflg = 0;                /* Reset x-packet flag */
  444.     rqf = -1;                /* Reset 8th-bit-quote request flag */
  445.     memstr = 0;                /* Reset memory-string flag */
  446.     memptr = NULL;            /*  and pointer */
  447.     n_len = -1;                /* No encoded-ahead data */
  448.     bctu = 1;                /* Reset block check type to 1 */
  449.     ebq = ebqflg = 0;            /* Reset 8th-bit quoting stuff */
  450.     if (savmod) {            /* If global file mode was saved, */
  451.         binary = 1;            /*  restore it, */
  452.     savmod = 0;            /*  unsave it. */
  453.     }
  454.     prvpkt = -1;            /* Reset packet number */
  455.     pktnum = 0;
  456.     numerrs = 0;            /* Transmission error counter */
  457.     cxseen = czseen = 0;        /* Reset interrupt flags */
  458.     *filnam = '\0';            /* Clear file name */
  459.     *sndpkt = '\0';            /* Clear retransmission buffer */
  460.     spktl = 0;                /* And its length */
  461.     nakstate = 0;            /* Say we're not in a NAK'ing state */
  462.     if (server)             /* If acting as server, */
  463.     timint = srvtim;        /* Use server timeout interval. */
  464. }
  465.  
  466.  
  467. /*  R I N I T  --  Respond to S or I packet  */
  468.  
  469. rinit(d) char *d; {
  470.     char *tp;
  471.     ztime(&tp);
  472.     tlog(F110,"Transaction begins",tp,0l); /* Make transaction log entry */
  473.     if (binary)
  474.       tlog(F100,"Global file mode = binary","",0l);
  475.     else
  476.       tlog(F100,"Global file mode = text","",0l);
  477.     filcnt = 0;                /* Init file counter */
  478.     spar(d);
  479.     ack1(rpar());
  480. #ifdef datageneral
  481.     if ((local) && (!quiet))            /* Only do this if local & not quiet */
  482.         consta_mt();                    /* Start the asynch read task */
  483. #endif
  484. }
  485.  
  486. /*  S I N I T  --  Make sure file exists, then send Send-Init packet */
  487.  
  488. sinit() {
  489.     int x; char *tp;
  490.  
  491.     filcnt = 0;
  492.     sndsrc = nfils;            /* Where to look for files to send */
  493.  
  494.     ztime(&tp);
  495.     tlog(F110,"Transaction begins",tp,0l); /* Make transaction log entry */
  496.     debug(F101,"sinit: sndsrc","",sndsrc);
  497.     if (sndsrc < 0) {            /* Must expand from 'send' command */
  498. #ifdef DTILDE
  499.     char *tnam, *tilde_expand();    /* May have to expand tildes */
  500.     tnam = tilde_expand(cmarg);    /* Try to expand tilde. */
  501.     if (*tnam != '\0') cmarg = tnam;
  502. #endif
  503.     nfils = zxpand(cmarg);        /* Look up literal name. */
  504.     if (nfils < 0) {
  505.         if (server)
  506.           errpkt("Too many files");
  507.         else
  508.           screen(SCR_EM,0,0l,"Too many files");
  509.         return(0);
  510.         } else if (nfils == 0) {    /* If none found, */
  511.         char xname[100];        /* convert the name. */
  512.         zrtol(cmarg,xname);
  513.         nfils = zxpand(xname);     /* Look it up again. */
  514.     }
  515.     if (nfils < 1) {        /* If no match, report error. */
  516.         if (server) 
  517.             errpkt("File not found");
  518.         else
  519.         screen(SCR_EM,0,0l,"File not found");
  520.         return(0);
  521.     }
  522.     x = gnfile();            /* Position to first file. */
  523.     if (x < 1) {
  524.         if (!server) 
  525.             screen(SCR_EM,0,0l,"No readable file to send");
  526.             else
  527.             errpkt("No readable file to send");
  528.         return(0);
  529.         } 
  530.     } else if (sndsrc > 0) {        /* Command line arglist -- */
  531.     x = gnfile();            /* Get the first file from it. */
  532.     if (x < 1) return(0);        /* (if any) */
  533.     } else if (sndsrc == 0) {        /* stdin or memory always exist... */
  534.     if ((cmarg2 != NULL) && (*cmarg2)) {
  535.         strcpy(filnam,cmarg2);    /* If F packet, "as" name is used */
  536.         cmarg2 = "";        /* if provided, */
  537.         } else                /* otherwise */
  538.         strcpy(filnam,"stdin");    /* just use this. */
  539.     }
  540.     debug(F101,"sinit: nfils","",nfils);
  541.     debug(F110," filnam",filnam,0);
  542.     debug(F110," cmdstr",cmdstr,0);
  543.     ttflui();                /* Flush input buffer. */
  544.     if (!local && !server) sleep(delay);
  545. #ifdef datageneral
  546.     if ((local) && (!quiet))            /* Only do this if local & not quiet */
  547.         consta_mt();                    /* Start the asynch read task */
  548. #endif
  549.     sipkt('S');                /* Send the Send-Init packet. */
  550.     return(1);
  551. }
  552.  
  553. sipkt(c) char c; {            /* Send S or I packet. */
  554.     CHAR *rp;
  555.     ttflui();                /* Flush pending input. */
  556.     rp = rpar();            /* Get parameters. */
  557.     spack(c,pktnum,strlen(rp),rp);
  558. }
  559.  
  560. /*  R C V F I L -- Receive a file  */
  561.  
  562. /*
  563.   Incoming filename is in data field of F packet.
  564.   This function decodes it into the srvcmd buffer, substituting an
  565.   alternate "as-name", if one was given.
  566.   Finally, it does any requested transformations (like converting to
  567.   lowercase) then if a file of the same name already exists, makes
  568.   a new unique name.
  569. */
  570. rcvfil(n) char *n; {
  571.     char xname[100], *xp;        /* Buffer for constructing name */
  572. #ifdef DTILDE
  573.     char *dirp, *tilde_expand();
  574. #endif
  575.  
  576.     srvptr = srvcmd;            /* Decode file name from packet. */
  577.     decode(rdatap,putsrv);
  578.     if (*srvcmd == '\0')        /* Watch out for null F packet. */
  579.         strcpy(srvcmd,"NONAME");
  580. #ifdef DTILDE
  581.     dirp = tilde_expand(srvcmd);    /* Expand tilde, if any. */
  582.     if (*dirp != '\0') strcpy(srvcmd,dirp);
  583. #endif
  584.     screen(SCR_FN,0,0l,srvcmd);        /* Put it on screen */
  585.     tlog(F110,"Receiving",srvcmd,0l);    /* Transaction log entry */
  586.     if (cmarg2 != NULL) {               /* Check for alternate name */
  587.         if (*cmarg2 != '\0') {
  588.             strcpy(srvcmd,cmarg2);    /* Got one, use it. */
  589.         *cmarg2 = '\0';
  590.         }
  591.     }
  592.     xp = xname;                /* OK to proceed. */
  593.     if (fncnv)                /* If desired, */
  594.         zrtol(srvcmd,xp);        /* convert name to local form */
  595.     else                /* otherwise, */
  596.         strcpy(xname,srvcmd);        /* use it literally */
  597.  
  598.     if (warn) {                /* File collision avoidance? */
  599.     if (zchki(xname) != -1) {    /* Yes, file exists? */
  600.         znewn(xname,&xp);        /* Yes, make new name. */
  601.         strcpy(xname,xp);
  602.         debug(F110," exists, new name ",xname,0);
  603.         }
  604.     }
  605.     debug(F110,"rcvfil: xname",xname,0);
  606.     strcpy(n,xname);            /* Return pointer to actual name. */
  607.     ffc = 0;                /* Init file character counter */
  608.     filcnt++;
  609.     return(1);                /* Always succeeds */
  610. }
  611.  
  612. /*  O P E N A -- Open a file, with attributes.  */
  613. /*
  614.   This function tries to open a new file to put the arriving data in.
  615.   The filename is the one in the srvcmd buffer.  If warning is on and
  616.   a file of that name already exists, then a unique name is chosen.
  617. */
  618. opena(f,zz) char *f; struct zattr *zz; {
  619.     int x;
  620.  
  621.     adebu(f,zz);            /* Write attributes to debug log */
  622.  
  623.     if (bsavef) {            /* If somehow file mode */
  624.     binary = bsave;            /* was saved but not restored, */
  625.     bsavef = 0;            /* restore it. */
  626.     debug(F101,"opena restoring binary","",binary);
  627.     }
  628.     if (x = openo(f)) {            /* Try to open the file. */
  629.     tlog(F110," as",f,0l);        /* OK, open, record name. */
  630.     if (zz->type.val[0] == 'A') {    /* Check attributes */
  631.         bsave = binary;        /* Save global file type */
  632.         bsavef = 1;            /* ( restore it in clsof() ) */
  633.         binary = 0;
  634.         debug(F100,"opena attribute A = text","",binary);
  635.     } else if (zz->type.val[0] == 'B') {
  636.         bsave = binary;        /* Save global file type */
  637.         bsavef = 1;
  638.         binary = 1;
  639.         debug(F100,"opena attribute B = binary","",binary);
  640.     }
  641.     if (binary)            /* Log file mode in transaction log */
  642.       tlog(F100," mode: binary","",0l);
  643.     else
  644.       tlog(F100," mode: text","",0l);
  645.     screen(SCR_AN,0,0l,f);
  646.     intmsg(filcnt);
  647.  
  648. #ifdef datageneral
  649. /* Need to turn on multi-tasking console interrupt task here, since multiple */
  650. /* files may be received. */
  651.         if ((local) && (!quiet))        /* Only do this if local & not quiet */
  652.             consta_mt();                /* Start the asynch read task */
  653. #endif
  654.  
  655.     } else {                /* Did not open file OK. */
  656.         tlog(F110,"Failure to open",f,0l);
  657.     screen(SCR_EM,0,0l,"Can't open file");
  658.     }
  659.     ffc = 0;                /* Init file character counter */
  660.     return(x);                /* Pass on return code from openo */
  661. }
  662.  
  663. /*  R E O F  --  Receive End Of File  */
  664.  
  665. reof(yy) struct zattr *yy; {
  666.     int x;
  667.     char *p;
  668.     char c;
  669.  
  670.     if (cxseen == 0) cxseen = (*rdatap == 'D');    /* Got discard directive? */
  671.     x = clsof(cxseen | czseen);
  672.     if (cxseen || czseen) {
  673.     tlog(F100," *** Discarding","",0l);
  674.     cxseen = 0;
  675.     } else {
  676.     fstats();            /* Close out file statistics */
  677.     if (yy->disp.len != 0) {    /* Handle file disposition */
  678.         p = yy->disp.val;
  679.         c = *p++;
  680.         if (c == 'M') {        /* Mail to user. */
  681.         zmail(p,filnam);    /* Do the system's mail command */
  682.         tlog(F110,"mailed",filnam,0l);
  683.         tlog(F110," to",p,0l);
  684.         zdelet(filnam);        /* Delete the file */
  685.         } else if (c == 'P') {    /* Print the file. */
  686.         zprint(p,filnam);    /* Do the system's print command */
  687.         tlog(F110,"printed",filnam,0l);
  688.         tlog(F110," with options",p,0l);
  689.         zdelet(filnam);        /* Delete the file */
  690.         }
  691.     }
  692.     }
  693.     *filnam = '\0';
  694.     return(x);
  695. }
  696.  
  697.  
  698. /*  R E O T  --  Receive End Of Transaction  */
  699.  
  700. reot() {
  701.     cxseen = czseen = 0;        /* Reset interruption flags */
  702.     tstats();
  703. }
  704.  
  705. /*  S F I L E -- Send File header or teXt header packet  */
  706.  
  707. /*  Call with x nonzero for X packet, zero for F packet  */
  708. /*  Returns 1 on success, 0 on failure                   */
  709.  
  710. sfile(x) int x; {
  711.     char pktnam[100];            /* Local copy of name */
  712.     char *s;
  713.  
  714.     if (x == 0) {            /* F-Packet setup */
  715.  
  716.         if (*cmarg2 != '\0') {        /* If we have a send-as name, */
  717.         strcpy(pktnam,cmarg2);    /* copy it literally, */
  718.         cmarg2 = "";        /* and blank it out for next time. */
  719.         } else {            /* Otherwise use actual file name: */
  720.         if (fncnv) {        /* If converting names, */
  721.             zltor(filnam,pktnam);    /* convert it to common form, */
  722.         } else {            /* otherwise, */
  723.             strcpy(pktnam,filnam);    /* copy it literally. */
  724.             }
  725.         }
  726.         debug(F110,"sfile",filnam,0);    /* Log debugging info */
  727.         debug(F110," pktnam",pktnam,0);
  728.         if (openi(filnam) == 0)     /* Try to open the file */
  729.         return(0);         
  730.         s = pktnam;            /* Name for packet data field */
  731.  
  732.     } else {                /* X-packet setup */
  733.  
  734.         debug(F110,"sxpack",cmdstr,0);    /* Log debugging info */
  735.         s = cmdstr;            /* Name for data field */
  736.     }
  737.  
  738.     encstr(s);                /* Encode the name into data[]. */
  739.     nxtpkt(&pktnum);            /* Increment the packet number */
  740.     spack(x ? 'X' : 'F', pktnum, size, data); /* Send the F or X packet */
  741.  
  742.     if (x == 0) {            /* Display for F packet */
  743.         if (displa) {            /* Screen */
  744.         screen(SCR_FN,'F',(long)pktnum,filnam);
  745.         screen(SCR_AN,0,0l,pktnam);
  746.         screen(SCR_FS,0,(long)fsize,"");
  747.         }
  748.         tlog(F110,"Sending",filnam,0l);    /* Transaction log entry */
  749.         tlog(F110," as",pktnam,0l);
  750.  
  751.     } else {                /* Display for X-packet */
  752.  
  753.         screen(SCR_XD,'X',(long)pktnum,cmdstr);    /* Screen */
  754.         tlog(F110,"Sending from:",cmdstr,0l);    /* Transaction log */
  755.     }
  756.     intmsg(++filcnt);            /* Count file, give interrupt msg */
  757.     first = 1;                /* Init file character lookahead. */
  758.     n_len = -1;            /* (PWP) Init the packet encode-ahead length */
  759.     ffc = 0;                /* Init file character counter. */
  760.     return(1);
  761. }
  762.  
  763. /*  S D A H E A D -- (PWP) Encode the next data packet to send */
  764.  
  765. sdahead() {    
  766.     if (spsiz > MAXPACK)         /* see the logic in ckcfn2.c, spack() */
  767.     n_len = getpkt(spsiz-bctu-5);    /* long packet size */
  768.     else
  769.     n_len = getpkt(spsiz-bctu-2);    /* short packet size */
  770. }
  771.  
  772. /*  S D A T A -- Send a data packet */
  773.  
  774. /*  Return -1 if no data to send, else send packet and return length  */
  775.  
  776. sdata() {
  777.     int len;
  778.     
  779.     if (cxseen || czseen) {        /* If interrupted, done. */
  780.     return(-1);
  781.     }    
  782.     
  783.     if (n_len < 0)            /* (PWP) if we haven't encoded the */
  784.          sdahead();            /*  packet yet, do it now */
  785.  
  786.     len = n_len;
  787.     if (len == 0)             /* Done if no data. */
  788.         return(-1);
  789.  
  790.     nxtpkt(&pktnum);            /* Increment the packet number */
  791.     spack('D',pktnum,len,data);        /* Send the packet */
  792.     
  793. /* comment out next statement if it causes problems... */
  794.     if (len > 0)            /* if we got data last time */
  795.     sdahead();            /* encode more now */
  796.  
  797.     return(len);
  798. }
  799.  
  800.  
  801. /*  S E O F -- Send an End-Of-File packet */
  802.  
  803. /*  Call with a string pointer to character to put in the data field, */
  804. /*  or else a null pointer or "" for no data.  */
  805.  
  806. seof(s) char *s; {
  807.     nxtpkt(&pktnum);            /* Increment the packet number */
  808.     if ((s != NULL) && (*s != '\0')) {
  809.     spack('Z',pktnum,1,s);
  810.     tlog(F100," *** interrupted, sending discard request","",0l);
  811.     } else {
  812.     spack('Z',pktnum,0,"");
  813.     fstats();
  814.     }
  815. }
  816.  
  817.  
  818. /*  S E O T -- Send an End-Of-Transaction packet */
  819.  
  820. seot() {
  821.     nxtpkt(&pktnum);            /* Increment the packet number */
  822.     spack('B',pktnum,0,"");        /* Send the EOT packet */
  823.     cxseen = czseen = 0;        /* Reset interruption flags */
  824.     tstats();                /* Log timing info */
  825. }
  826.  
  827. /*   R P A R -- Fill the data array with my send-init parameters  */
  828.  
  829. CHAR *
  830. rpar() {
  831.     if (rpsiz > MAXPACK)        /* Biggest normal packet I want. */
  832.       data[1] = tochar(MAXPACK);    /* If > 94, use 94, but specify */
  833.     else                /* extended packet length below... */
  834.       data[1] = tochar(rpsiz);        /* else use what the user said. */
  835.     data[2] = tochar(rtimo);        /* When I want to be timed out */
  836.     data[3] = tochar(mypadn);        /* How much padding I need (none) */
  837.     data[4] = ctl(mypadc);        /* Padding character I want */
  838.     data[5] = tochar(eol);        /* End-Of-Line character I want */
  839.     data[6] = '#';            /* Control-Quote character I send */
  840.     switch (rqf) {            /* 8th-bit prefix */
  841.     case -1:
  842.     case  1: if (parity) ebq = sq = '&'; break;
  843.     case  0:
  844.     case  2: break;
  845.     }
  846.     data[7] = sq;
  847.     data[8] = bctr + '0';        /* Block check type */
  848.     if (rptflg)                /* Run length encoding */
  849.         data[9] = rptq;            /* If receiving, agree. */
  850.     else
  851.         data[9] = '~';         
  852.  
  853.     data[10] = tochar((atcapr?atcapb:0)|(lpcapr?lpcapb:0)|(swcapr?swcapb:0));
  854.     data[capas+1] = tochar(swcapr ? wslots : 0); /* Window size */
  855.  
  856.     rpsiz = urpsiz;            /* Long packets ... */
  857.     data[capas+2] = tochar(rpsiz / 95);    /* Long packet size, big part */
  858.     data[capas+3] = tochar(rpsiz % 95);    /* Long packet size, little part */
  859.     data[capas+4] = '\0';        /* Terminate the init string */
  860.     if (deblog) {
  861.     debug(F110,"rpar",data+1,0);
  862.     rdebu(capas+2);    
  863.     }
  864.     return(data+1);            /* Return pointer to string. */
  865. }
  866.  
  867. spar(s) char *s; {            /* Set parameters */
  868.     int x, lpsiz;
  869.  
  870. debug(F110,"entering spar",s,0);
  871.  
  872.     s--;                /* Line up with field numbers. */
  873.  
  874. /* Limit on size of outbound packets */
  875.     x = (rln >= 1) ? xunchar(s[1]) : 80;
  876.     lpsiz = spsiz;            /* Remember what they SET. */
  877.     if (spsizf) {            /* SET-command override? */
  878.     if (x < spsiz) spsiz = x;    /* Ignore LEN unless smaller */
  879.     } else {                /* otherwise */
  880.     spsiz = (x < 10) ? 80 : x;    /* believe them if reasonable */
  881.     }
  882.  
  883. /* Timeout on inbound packets */
  884.     if (!timef) {            /* Only if not SET-cmd override */
  885.     x = (rln >= 2) ? xunchar(s[2]) : 5;
  886.     timint = (x < 0) ? 5 : x;
  887.     }
  888.  
  889. /* Outbound Padding */
  890.     npad = 0; padch = '\0';
  891.     if (rln >= 3) {
  892.     npad = xunchar(s[3]);
  893.     if (rln >= 4) padch = ctl(s[4]); else padch = 0;
  894.     }
  895.  
  896. /* Outbound Packet Terminator */
  897.     seol = (rln >= 5) ? xunchar(s[5]) : '\r';
  898.     if ((seol < 2) || (seol > 31)) seol = '\r';
  899.  
  900. /* Control prefix */
  901.     x = (rln >= 6) ? s[6] : '#';
  902.     myctlq = ((x > 32 && x < 63) || (x > 95 && x < 127)) ? x : '#';
  903.  
  904. /* 8th-bit prefix */
  905.     rq = (rln >= 7) ? s[7] : 0;
  906.     if (rq == 'Y') rqf = 1;
  907.       else if ((rq > 32 && rq < 63) || (rq > 95 && rq < 127)) rqf = 2;
  908.         else rqf = 0;
  909.     switch (rqf) {
  910.     case 0: ebqflg = 0; break;
  911.     case 1: if (parity) { ebqflg = 1; ebq = '&'; } break;
  912.     case 2: if (ebqflg = (ebq == sq || sq == 'Y')) ebq = rq;
  913.     }
  914.  
  915. /* Block check */
  916.     x = 1;
  917.     if (rln >= 8) {
  918.     x = s[8] - '0';
  919.     if ((x < 1) || (x > 3)) x = 1;
  920.     }
  921.     bctr = x;
  922.  
  923. /* Repeat prefix */
  924.     if (rln >= 9) {
  925.     rptq = s[9]; 
  926.     rptflg = ((rptq > 32 && rptq < 63) || (rptq > 95 && rptq < 127));
  927.     } else rptflg = 0;
  928.  
  929. /* Capabilities */
  930.     atcapu = lpcapu = swcapu = 0;
  931.     if (rln >= 10) {
  932.         x = xunchar(s[10]);
  933.         atcapu = (x & atcapb) && atcapr;
  934.     lpcapu = (x & lpcapb) && lpcapr;
  935.     swcapu = (x & swcapb) && swcapb;
  936.     for (capas = 10; (xunchar(s[capas]) & 1) && (rln >= capas); capas++) ;
  937.     }
  938.  
  939. /* Long Packets */
  940.     if (lpcapu) {
  941.         if (rln > capas+2) {
  942.         x = xunchar(s[capas+2]) * 95 + xunchar(s[capas+3]);
  943.         if (spsizf) {        /* If overriding negotiations */
  944.         spsiz = (x < lpsiz) ? x : lpsiz; /* do this, */
  945.         } else {                     /* otherwise */
  946.         spsiz = (x > MAXSP) ? MAXSP : x; /* do this. */
  947.         }
  948.         if (spsiz < 10) spsiz = 80;    /* Be defensive... */
  949.     }
  950.     }
  951.  
  952.     /* (PWP) save current send packet size for optimal packet size calcs */
  953.     spmax = spsiz;
  954.     numerrs = 0;
  955.     
  956. /* Sliding Windows */
  957.     if (swcapu) {
  958.         if (rln > capas+1) {
  959.         x = xunchar(s[capas+1]);
  960.         wslots = x > MAXWS ? MAXWS : x;
  961.     }
  962.     else wslots = 1;
  963.     }
  964.     if (deblog) sdebu(rln);        /* Record parameters in debug log */
  965. }
  966.  
  967. /*  G N F I L E  --  Get the next file name from a file group.  */
  968.  
  969. /*  Returns 1 if there's a next file, 0 otherwise  */
  970.  
  971. gnfile() {
  972.     int x; long y;
  973. #ifdef datageneral
  974.     int dgfiles = 0;             /* Number of files expanded */
  975.     static int dgnfils = -1;        /* Saved nfils value */
  976. #endif
  977.  
  978. /* If file group interruption (C-Z) occured, fail.  */
  979.  
  980.     debug(F101,"gnfile: czseen","",czseen);
  981.  
  982.     if (czseen) {
  983.     tlog(F100,"Transaction cancelled","",0l);
  984.     return(0);
  985.     }
  986.  
  987. /* If input was stdin or memory string, there is no next file.  */
  988.  
  989.     if (sndsrc == 0) return(0);
  990.  
  991. /* If file list comes from command line args, get the next list element. */
  992.  
  993.     y = -1;
  994.     while (y < 0) {            /* Keep trying till we get one... */
  995.  
  996.     if (sndsrc > 0) {
  997.         if (nfils-- > 0) {
  998.  
  999. #ifdef datageneral
  1000. /* The DG does not internally expand the file names when a string of */
  1001. /* filenames is given. So we must in this case. */
  1002.                 if (dgnfils == -1) {   /* This is executed first time only! */
  1003.                     strcpy(filnam,*cmlist++);
  1004.                 dgfiles = zxpand(filnam); /* Expand the string */
  1005.                     debug(F111,"gnfile:cmlist filnam (x-init)",filnam,dgfiles);
  1006.                     dgnfils = nfils + 1;
  1007.                     debug(F111,"gnfile:cmlist filnam (x-init)",filnam,dgnfils);
  1008.              } 
  1009.              x = znext(filnam);
  1010.              if (x > 0) {      /* Get the next file in the expanded list */
  1011.                 debug(F111,"gnfile znext: filnam (exp-x=)",filnam,x);
  1012.                     nfils = dgnfils;     /* The virtual number of files left */
  1013.                  }
  1014.              if (x == 0) {     /* Expand the next command argument */
  1015.                 if (dgnfils == 1) {
  1016.                      dgnfils = -1;   /* The last argument resets things */
  1017.                      *filnam = '\0';
  1018.                      debug(F101,"gnfile cmlist: nfils","",nfils);
  1019.                      return(0);
  1020.                 }
  1021.               
  1022.                     strcpy(filnam,*cmlist++);  /* Finish expanding last arg */
  1023.                 dgfiles = zxpand(filnam); /* Expand the string */
  1024.                     debug(F111,"gnfile: cmlist filnam (exp)",filnam,dgnfils);
  1025.                 x = znext(filnam);
  1026.                 debug(F111,"gnfile znext: filnam (exp)",filnam,x);
  1027.                 nfils = dgnfils--;
  1028.         } 
  1029. #else
  1030.         strcpy(filnam,*cmlist++);
  1031.         debug(F111,"gnfile: cmlist filnam",filnam,nfils);
  1032. #endif
  1033.         } else {
  1034.         *filnam = '\0';
  1035.         debug(F101,"gnfile cmlist: nfils","",nfils);
  1036.         return(0);
  1037.         }
  1038.     }
  1039.  
  1040. /* Otherwise, step to next element of internal wildcard expansion list. */
  1041.  
  1042.     if (sndsrc < 0) {
  1043.         x = znext(filnam);
  1044.         debug(F111,"gnfile znext: filnam",filnam,x);
  1045.         if (x == 0) return(0);
  1046.     }
  1047.  
  1048. /* Get here with a filename. */
  1049.  
  1050.     y = zchki(filnam);        /* Check if file readable */
  1051.     if (y < 0) {
  1052.         debug(F110,"gnfile skipping:",filnam,0);
  1053.         tlog(F111,filnam,"not sent, reason",(long)y);
  1054.         screen(SCR_ST,ST_SKIP,0l,filnam);
  1055.     } else fsize = y;
  1056.     }        
  1057.     return(1);
  1058. }
  1059.  
  1060. /*  O P E N I  --  Open an existing file for input  */
  1061.  
  1062. openi(name) char *name; {
  1063.     int x, filno;
  1064.     if (memstr) return(1);        /* Just return if file is memory. */
  1065.  
  1066.     debug(F110,"openi",name,0);
  1067.     debug(F101," sndsrc","",sndsrc);
  1068.  
  1069.     filno = (sndsrc == 0) ? ZSTDIO : ZIFILE;    /* ... */
  1070.  
  1071.     debug(F101," file number","",filno);
  1072.  
  1073.     if (zopeni(filno,name)) {        /* Otherwise, try to open it. */
  1074.     debug(F110," ok",name,0);
  1075.         return(1);
  1076.     } else {                /* If not found, */
  1077.     char xname[100];        /* convert the name */
  1078.     zrtol(name,xname);        /* to local form and then */
  1079.     x = zopeni(filno,xname);    /* try opening it again. */
  1080.     debug(F101," zopeni","",x);
  1081.     if (x) {
  1082.         debug(F110," ok",xname,0);
  1083.         return(1);            /* It worked. */
  1084.         } else {
  1085.         screen(SCR_EM,0,0l,"Can't open file");  /* It didn't work. */
  1086.         tlog(F110,xname,"could not be opened",0l);
  1087.         debug(F110," openi failed",xname,0);
  1088.         return(0);
  1089.         }
  1090.     }
  1091. }
  1092.  
  1093. /*  O P E N O  --  Open a new file for output.  */
  1094.  
  1095. /*  Returns actual name under which the file was opened in string 'name2'. */
  1096.  
  1097. openo(name) char *name; {
  1098.  
  1099.     if (stdouf)                /* Receiving to stdout? */
  1100.     return(zopeno(ZSTDIO,""));
  1101.  
  1102.     debug(F110,"openo: name",name,0);
  1103.  
  1104.     if (cxseen || czseen) {        /* If interrupted, get out before */
  1105.     debug(F100," open cancelled","",0); /* destroying existing file. */
  1106.     return(1);            /* Pretend to succeed. */
  1107.     }
  1108.     if (zopeno(ZOFILE,name) == 0) {    /* Try to open the file */
  1109.     debug(F110,"openo failed",name,0);
  1110.     tlog(F110,"Failure to open",name,0l);
  1111.     return(0);
  1112.     } else {
  1113.     debug(F110,"openo ok, name",name,0);
  1114.     return(1);
  1115.     }
  1116. }
  1117.  
  1118. /*  O P E N T  --  Open the terminal for output, in place of a file  */
  1119.  
  1120. opent() {
  1121.     ffc = tfc = 0;
  1122.     return(zopeno(ZCTERM,""));
  1123. }
  1124.  
  1125. /*  C L S I F  --  Close the current input file. */
  1126.  
  1127. clsif() {
  1128. #ifdef datageneral
  1129.     if ((local) && (!quiet))    /* Only do this if local & not quiet */
  1130.         if (nfils < 1)          /* More files to send ... leave it on! */
  1131.             connoi_mt();
  1132. #endif
  1133.     if (memstr) {            /* If input was memory string, */
  1134.     memstr = 0;            /* indicate no more. */
  1135.     } else zclose(ZIFILE);        /* else close input file. */
  1136.  
  1137.     if (czseen || cxseen) 
  1138.         screen(SCR_ST,ST_DISC,0l,"");
  1139.     else
  1140.         screen(SCR_ST,ST_OK,0l,"");
  1141.     cxseen = hcflg = 0;            /* Reset flags, */
  1142.     *filnam = '\0';            /* and current file name */
  1143.     n_len = -1;           /* (pwp) reinit packet encode-ahead length */
  1144. }
  1145.  
  1146.  
  1147. /*  C L S O F  --  Close an output file.  */
  1148.  
  1149. /*  Call with disp != 0 if file is to be discarded.  */
  1150. /*  Returns -1 upon failure to close, 0 or greater on success. */
  1151.  
  1152. clsof(disp) int disp; {
  1153.     int x;
  1154.  
  1155.     if (bsavef) {            /* If we saved global file type */
  1156.     debug(F101,"clsof restoring binary","",binary);
  1157.     binary = bsave;            /* restore it */
  1158.     bsavef = 0;            /* only this once. */
  1159.     }
  1160. #ifdef datageneral
  1161.     if ((local) && (!quiet))        /* Only do this if local & not quiet */
  1162.         connoi_mt();
  1163. #endif
  1164.     if ((x = zclose(ZOFILE)) < 0) {    /* Try to close the file */
  1165.     tlog(F100,"Failure to close",filnam,0l);
  1166.     screen(SCR_ST,ST_ERR,0l,"");
  1167.     } else if (disp && (keep == 0)) {    /* Delete it if interrupted, */
  1168.     if (*filnam) zdelet(filnam);    /* and not keeping incomplete files */
  1169.     debug(F100,"Discarded","",0);
  1170.     tlog(F100,"Discarded","",0l);
  1171.     screen(SCR_ST,ST_DISC,0l,"");
  1172.     } else {                /* Nothing wrong, just keep it */
  1173.     debug(F100,"Closed","",0);    /* and give comforting messages. */
  1174.     screen(SCR_ST,ST_OK,0l,"");
  1175.     }
  1176.     return(x);                /* Send back zclose() return code. */
  1177. }
  1178.  
  1179. /*  S N D H L P  --  Routine to send builtin help  */
  1180.  
  1181. sndhlp() {
  1182.     nfils = 0;                /* No files, no lists. */
  1183.     xflg = 1;                /* Flag we must send X packet. */
  1184.     strcpy(cmdstr,"help text");        /* Data for X packet. */
  1185.     first = 1;                /* Init getchx lookahead */
  1186.     memstr = 1;                /* Just set the flag. */
  1187.     memptr = hlptxt;            /* And the pointer. */
  1188.     if (binary) {            /* If file mode is binary, */
  1189.     binary = 0;            /*  turn it back to text for this, */
  1190.     savmod = 1;            /*  remember to restore it later. */
  1191.     }
  1192.     return(sinit());
  1193. }
  1194.  
  1195.  
  1196. /*  C W D  --  Change current working directory  */
  1197.  
  1198. /*
  1199.  String passed has first byte as length of directory name, rest of string
  1200.  is name.  Fails if can't connect, else ACKs (with name) and succeeds. 
  1201. */
  1202.  
  1203. cwd(vdir) char *vdir; {
  1204.     char *cdd, *zgtdir();
  1205.     char *dirp;
  1206. #ifdef DTILDE
  1207.     char *tilde_expand();
  1208. #endif
  1209.  
  1210.     vdir[xunchar(*vdir) + 1] = '\0';    /* Terminate string with a null */
  1211.  
  1212.     dirp = vdir+1;
  1213. #ifdef DTILDE
  1214.     dirp = tilde_expand(vdir+1);    /* Attempt to expand tilde */
  1215.     if (*dirp == '\0') dirp = vdir+1;    /* in directory name. */
  1216. #endif
  1217.     tlog(F110,"Directory requested: ",dirp,01);
  1218.     if (zchdir(dirp)) {
  1219.     cdd = zgtdir();            /* Get new working directory. */
  1220.     tlog(F110,"Changed directory to ",cdd,01);
  1221.     encstr(cdd);
  1222.     ack1(data);
  1223.     tlog(F110,"Changed directory to",cdd,0l);
  1224.     return(1); 
  1225.     } else {
  1226.     tlog(F110,"Failed to change directory to",dirp,0l);
  1227.     return(0);
  1228.     }
  1229. }
  1230.  
  1231.  
  1232. /*  S Y S C M D  --  Do a system command  */
  1233.  
  1234. /*  Command string is formed by concatenating the two arguments.  */
  1235.  
  1236. syscmd(prefix,suffix) char *prefix, *suffix; {
  1237.     char *cp;
  1238.  
  1239.     if (prefix == NULL || *prefix == '\0') return(0);
  1240.  
  1241. #ifdef datageneral
  1242. /* A kludge for now -- the real change needs to be done elsewhere... */
  1243.     {
  1244.     extern char *WHOCMD;
  1245.     if ((strcmp(prefix,WHOCMD) == 0) && (*suffix == 0))
  1246.       strcpy(suffix,"[!pids]");
  1247.     }
  1248. #endif
  1249.     for (cp = cmdstr; *prefix != '\0'; *cp++ = *prefix++) ;
  1250.     while (*cp++ = *suffix++) ;
  1251.  
  1252.     debug(F110,"syscmd",cmdstr,0);
  1253.     if (zopeni(ZSYSFN,cmdstr) > 0) {
  1254.     debug(F100,"syscmd zopeni ok",cmdstr,0);
  1255.     nfils = sndsrc = 0;        /* Flag that input is from stdin */
  1256.     xflg = hcflg = 1;        /* And special flags for pipe */
  1257.     if (binary) {            /* If file mode is binary, */
  1258.         binary = 0;            /*  turn it back to text for this, */
  1259.         savmod = 1;            /*  remember to restore it later. */
  1260.     }
  1261.     return (sinit());        /* Send S packet */
  1262.     } else {
  1263.     debug(F100,"syscmd zopeni failed",cmdstr,0);
  1264.     return(0);
  1265.     }
  1266. }
  1267.  
  1268. /*  A D E B U -- Write attribute packet info to debug log  */
  1269.  
  1270. adebu(f,zz) char *f; struct zattr *zz; {
  1271. #ifdef DEBUG
  1272.     if (deblog == 0) return(0);
  1273.     debug(F110,"Attributes for incoming file ",f,0);
  1274.     debug(F101," length in K","",(int) zz->lengthk);
  1275.     debug(F111," file type",zz->type.val,zz->type.len);
  1276.     debug(F111," creation date",zz->date.val,zz->date.len);
  1277.     debug(F111," creator",zz->creator.val,zz->creator.len);
  1278.     debug(F111," account",zz->account.val,zz->account.len);
  1279.     debug(F111," area",zz->area.val,zz->area.len);
  1280.     debug(F111," password",zz->passwd.val,zz->passwd.len);
  1281.     debug(F101," blksize",(int) zz->blksize,0);
  1282.     debug(F111," access",zz->access.val,zz->access.len);
  1283.     debug(F111," encoding",zz->encoding.val,zz->encoding.len);
  1284.     debug(F111," disposition",zz->disp.val,zz->disp.len);
  1285.     debug(F111," lprotection",zz->lprotect.val,zz->lprotect.len);
  1286.     debug(F111," gprotection",zz->gprotect.val,zz->gprotect.len);
  1287.     debug(F111," systemid",zz->systemid.val,zz->systemid.len);
  1288.     debug(F111," recfm",zz->recfm.val,zz->recfm.len);
  1289.     debug(F111," sysparam",zz->sysparam.val,zz->sysparam.len);
  1290.     debug(F101," length","",(int) zz->length);
  1291. #endif /* DEBUG */
  1292.     return(0);
  1293. }
  1294.