home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / cku200.tar / ckcpro.w < prev    next >
Text File  |  2001-11-14  |  102KB  |  3,502 lines

  1. char *protv =                                                     /* -*-C-*- */
  2. "C-Kermit Protocol Module 8.0.156, 10 Nov 2001";
  3.  
  4. int kactive = 0;            /* Kermit protocol is active */
  5.  
  6. #define PKTZEROHACK
  7.  
  8. /* C K C P R O  -- C-Kermit Protocol Module, in Wart preprocessor notation. */
  9. /*
  10.   Author: Frank da Cruz <fdc@columbia.edu>,
  11.   Columbia University Academic Information Systems, New York City.
  12.  
  13.   Copyright (C) 1985, 2001,
  14.     Trustees of Columbia University in the City of New York.
  15.     All rights reserved.  See the C-Kermit COPYING.TXT file or the
  16.     copyright text in the ckcmai.c module for disclaimer and permissions.
  17. */
  18. #ifndef NOXFER
  19. #include "ckcsym.h"
  20. #include "ckcdeb.h"
  21. #include "ckcasc.h"
  22. #include "ckcker.h"
  23. #ifdef OS2
  24. #ifndef NT
  25. #define INCL_NOPM
  26. #define INCL_VIO            /* Needed for ckocon.h */
  27. #include <os2.h>
  28. #undef COMMENT
  29. #endif /* NT */
  30. #include "ckocon.h"
  31. #endif /* OS2 */
  32.  
  33. /*
  34.  Note -- This file may also be preprocessed by the UNIX Lex program, but
  35.  you must indent the above #include statements before using Lex, and then
  36.  restore them to the left margin in the resulting C program before compilation.
  37.  Also, the invocation of the "wart()" function below must be replaced by an
  38.  invocation  of the "yylex()" function.  It might also be necessary to remove
  39.  comments in the (%)(%)...(%)(%) section.
  40. */
  41.  
  42. /* State definitions for Wart (or Lex) */
  43. %states ipkt rfile rattr rdpkt ssinit ssfile ssattr ssdata sseof sseot
  44. %states serve generic get rgen ssopkt ropkt
  45.  
  46. _PROTOTYP(static VOID xxproto,(void));
  47. _PROTOTYP(int wart,(void));
  48. _PROTOTYP(static int sgetinit,(int,int));
  49. _PROTOTYP(int sndspace,(int));
  50.  
  51. /* External C-Kermit variable declarations */
  52.   extern char *versio, *srvtxt, *cmarg, *cmarg2, **cmlist, *rf_err;
  53.   extern char *cdmsgfile[];
  54.   extern char * snd_move, * snd_rename, * srimsg;
  55.   extern char filnam[], ofilnam[], fspec[], ttname[], ofn1[];
  56.   extern CHAR sstate, *srvptr, *data;
  57.   extern int timint, rtimo, nfils, hcflg, xflg, flow, mdmtyp, network;
  58.   extern int oopts, omode, oname, opath, nopush, isguest, xcmdsrc;
  59.   extern int rejection, moving, fncact, bye_active, urserver, fatalio;
  60.   extern int protocol, prefixing, filcnt, carrier, fnspath, interrupted;
  61.   extern int recursive, inserver, nzxopts, idletmo, srvidl, xfrint;
  62.   extern struct ck_p ptab[];
  63.   extern int remfile, rempipe, xferstat, filestatus, wearealike, fackpath;
  64.   extern int patterns, filepeek, gnferror;
  65.   extern char * remdest;
  66.  
  67. #ifdef PKTZEROHACK
  68. #define PKTZEROLEN 32
  69. static char ipktack[PKTZEROLEN];
  70. static int ipktlen = 0;
  71. #endif /* PKTZEROHACK */
  72.  
  73. static int s_timint = -1;        /* For saving timeout value */
  74. static int havefs = 0;
  75. #ifdef CK_LOGIN
  76. static int logtries = 0;
  77. #endif /* CK_LOGIN */
  78.  
  79. static int cancel = 0;
  80. int fackbug = 0;
  81.  
  82. #ifdef STREAMING
  83. extern int streaming, streamok;
  84.  
  85. static VOID
  86. streamon() {
  87.     if (streamok) {
  88.     debug(F100,"streamon","",0);
  89.     streaming = 1;
  90.     timint = 0;            /* No timeouts while streaming. */
  91.     }
  92. }
  93.  
  94. #ifdef COMMENT                /* (not used) */
  95. static VOID
  96. streamoff() {
  97.     if (streaming) {
  98.     debug(F100,"streamoff","",0);
  99.     streaming = 0;
  100.     timint = s_timint;        /* Restore timeout */
  101.     }
  102. }
  103. #endif /* COMMENT */
  104. #else /* STREAMING */
  105. #define streamon()
  106. #define streamoff()
  107. #endif /* STREAMING */
  108.  
  109. #ifndef NOSPL
  110. _PROTOTYP( int addmac, (char *, char *) );
  111. _PROTOTYP( int zzstring, (char *, char **, int *) );
  112. #endif /* NOSPL */
  113. #ifndef NOICP
  114. _PROTOTYP( int cmdsrc, (void) );
  115. #endif /* NOICP */
  116.  
  117. #ifndef NOSERVER
  118.   extern char * x_user, * x_passwd, * x_acct;
  119.   extern int x_login, x_logged;
  120. #endif /* NOSERVER */
  121.  
  122. #ifdef TNCODE
  123.   extern int ttnproto;            /* Network protocol */
  124. #endif /* TNCODE */
  125.  
  126. #ifdef CK_SPEED
  127.   extern short ctlp[];            /* Control-character prefix table */
  128. #endif /* CK_SPEED */
  129.  
  130. #include "ckcnet.h"
  131. #ifdef TNCODE
  132.   extern int tn_b_nlm, tn_b_xfer, tn_nlm;
  133. #ifdef CK_ENCRYPTION
  134.   extern int tn_no_encrypt_xfer;
  135. #endif /* CK_ENCRYPTION */
  136. #endif /* TNCODE */
  137.  
  138. #ifdef TCPSOCKET
  139. #ifndef NOLISTEN
  140.   extern int tcpsrfd;
  141. #endif /* NOLISTEN */
  142. #endif /* TCPSOCKET */
  143.  
  144.   extern int cxseen, czseen, server, srvdis, local, displa, bctu, bctr, bctl;
  145.   extern int quiet, tsecs, parity, backgrd, nakstate, atcapu, wslotn, winlo;
  146.   extern int wslots, success, xitsta, rprintf, discard, cdtimo, keep, fdispla;
  147.   extern int timef, stdinf, rscapu, sendmode, epktflg, epktrcvd, epktsent;
  148.   extern int binary, fncnv;
  149.   extern long speed, ffc, crc16, calibrate, dest;
  150. #ifdef COMMENT
  151.   extern char *TYPCMD, *DIRCMD, *DIRCM2;
  152. #endif /* COMMENT */
  153. #ifndef OS2
  154.   extern char *SPACMD, *SPACM2, *WHOCMD;
  155. #endif /* OS2 */
  156.   extern CHAR *rdatap;
  157.   extern struct zattr iattr;
  158.  
  159. #ifdef VMS
  160.   extern int batch;
  161. #endif /* VMS */
  162.  
  163. #ifdef GFTIMER
  164.   extern CKFLOAT fptsecs;
  165. #endif /* GFTIMER */
  166.  
  167.   extern CHAR *srvcmd;
  168.   extern CHAR *epktmsg;
  169.  
  170. #ifdef CK_TMPDIR
  171. extern int f_tmpdir;            /* Directory changed temporarily */
  172. extern char savdir[];            /* For saving current directory */
  173. extern char * dldir;
  174. #endif /* CK_TMPDIR */
  175.  
  176.   extern int query;            /* Query-active flag */
  177. #ifndef NOSPL
  178.   extern int cmdlvl;
  179.   char querybuf[QBUFL+1] = { NUL, NUL }; /* QUERY response buffer */
  180.   char *qbufp = querybuf;        /* Pointer to it */
  181.   int qbufn = 0;            /* Length of data in it */
  182. #else
  183.   extern int tlevel;
  184. #endif /* NOSPL */
  185.  
  186. #ifndef NOICP
  187.   extern int escape;
  188. #endif /* NOICP */
  189. /*
  190.   If the following flag is nonzero when the protocol module is entered,
  191.   then server mode persists for exactly one transaction, rather than
  192.   looping until BYE or FINISH is received.
  193. */
  194. extern int justone;
  195.  
  196. static int r_save = -1;
  197. static int p_save = -1;
  198.  
  199. static VOID
  200. rdebug() {
  201.     if (server)
  202.       debug(F111,"RESUME","server=1",justone);
  203.     else
  204.       debug(F111,"RESUME","server=0",justone);
  205. }
  206.  
  207. /* Flags for the ENABLE and DISABLE commands */
  208. extern int
  209.   en_cpy, en_cwd, en_del, en_dir, en_fin, en_get, en_bye, en_mai, en_pri,
  210.   en_hos, en_ren, en_sen, en_spa, en_set, en_typ, en_who, en_ret, en_xit,
  211.   en_mkd, en_rmd;
  212. #ifndef NOSPL
  213. extern int en_asg, en_que;
  214. #endif /* NOSPL */
  215. extern int what, lastxfer;
  216.  
  217. /* Global variables declared here */
  218.  
  219.   int whatru = 0;            /* What are you. */
  220.   int whatru2 = 0;            /* What are you, cont'd. */
  221.  
  222. /* Local variables */
  223.  
  224.   static char vstate = 0;          /* Saved State   */
  225.   static char vcmd = 0;            /* Saved Command */
  226.   static int reget = 0;            /* Flag for executing REGET */
  227.   static int retrieve = 0;        /* Flag for executing RETRIEVE */
  228.   static int opkt = 0;            /* Send Extended GET packet */
  229.  
  230.   static int x;                /* General-purpose integer */
  231.   static char *s;            /* General-purpose string pointer */
  232.  
  233. /* Macros - Note, BEGIN is predefined by Wart (and Lex) as "state = ", */
  234. /* BEGIN is NOT a GOTO! */
  235. #define TINIT if (tinit(1) < 0) return(-9)
  236. #define SERVE { TINIT; resetc(); nakstate=1; what=W_NOTHING; cmarg2=""; \
  237. sendmode=SM_SEND; havefs=0; recursive=r_save; fnspath=p_save; BEGIN serve; }
  238. #define RESUME { rdebug(); if (!server) { return(0); } else \
  239. if (justone) { justone=0; return(0); } else { SERVE; } }
  240.  
  241. #ifdef GFTIMER
  242. #define QUIT x=quiet; quiet=1; clsif(); clsof(1); tsecs=gtimer(); \
  243.  fptsecs=gftimer(); quiet=x; return(success)
  244. #else
  245. #define QUIT x=quiet; quiet=1; clsif(); clsof(1); tsecs=gtimer(); quiet=x; \
  246.  return(success)
  247. #endif /* GFTIMER */
  248.  
  249. /*
  250.   By late 1999, the big switch() statement generated from the following state
  251.   table began choking even gcc, so here we extract the code from the larger
  252.   states into static routines to reduce the size of the cases and the
  253.   switch() overall.  The routines follow the state table; the prototypes are
  254.   here.  Each of these routines simply contains the text from the
  255.   corresponding case, but with return(-1) added in appropriate places; see
  256.   instructions after the state table switcher.
  257. */
  258. static int rc;                /* Return code for these routines */
  259. static int rcv_s_pkt();            /* Received an S packet */
  260. static int rcv_firstdata();        /* Received first Data packet */
  261. static int rcv_shortreply();        /* Short reply to a REMOTE command  */
  262. static int srv_query();            /* Server answers an query */
  263. static int srv_copy();            /* Server executes REMOTE COPY */
  264. static int srv_rename();        /* Server executes REMOTE RENAME */
  265. static int srv_login();            /* Server executes REMOTE LOGIN */
  266. static int srv_timeout();        /* Server times out */
  267.  
  268. %%
  269.  
  270. /*
  271.   Protocol entry points, one for each start state (sstate).
  272.   The lowercase letters are internal "inputs" from the user interface.
  273.   NOTE: The start state letters that appear on the left margin immediately
  274.   below can NOT be used as packet types OR as G-packet subcodes.
  275. */
  276.  
  277. s { TINIT;                /* Send file(s) */
  278.     if (sinit() > 0) BEGIN ssinit;
  279.        else RESUME; }
  280.  
  281. v { TINIT; nakstate = 1; BEGIN get; }    /* Receive file(s) */
  282.  
  283. r {                    /* Client sends a GET command */
  284.     TINIT;
  285.     vstate = get;
  286.     reget = 0;
  287.     retrieve = 0;
  288.     opkt = 0;
  289.     vcmd = 0;
  290. #ifdef PKTZEROHACK
  291.     ipktack[0] = NUL;
  292. #endif /* PKTZEROHACK */
  293.     if (sipkt('I') >= 0)
  294.       BEGIN ipkt;
  295.     else
  296.       RESUME;
  297. }
  298.  
  299. h {                    /* Client sends a RETRIEVE command */
  300.     TINIT;
  301.     vstate = get;
  302.     reget = 0;
  303.     retrieve = 1;
  304.     opkt = 0;
  305.     vcmd = 0;
  306.     if (sipkt('I') >= 0)
  307.       BEGIN ipkt;
  308.     else
  309.       RESUME;
  310. }
  311. j {                    /* Client sends a REGET command */
  312.     TINIT;
  313.     vstate = get;
  314.     reget = 1;
  315.     retrieve = 0;
  316.     opkt = 0;
  317.     vcmd = 0;
  318.     if (sipkt('I') >= 0)
  319.       BEGIN ipkt;
  320.     else
  321.       RESUME;
  322. }
  323. o {                    /* Client sends Extended GET Packet */
  324.     TINIT;
  325.     vstate = get;
  326.     reget = oopts & GOPT_RES;
  327.     retrieve = oopts & GOPT_DEL;
  328.     opkt = 1;
  329.     vcmd = 0;
  330.     if (sipkt('I') >= 0)
  331.       BEGIN ipkt;
  332.     else
  333.       RESUME;
  334. }
  335. c {                    /* Client sends a Host command */
  336.     TINIT;
  337.     vstate = rgen;
  338.     vcmd = 'C';
  339.     if (sipkt('I') >= 0)
  340.       BEGIN ipkt;
  341.     else
  342.       RESUME;
  343. }
  344. k { TINIT;                /* Client sends a Kermit command */
  345.     vstate = rgen;
  346.     vcmd = 'K';
  347.     if (sipkt('I') >= 0)
  348.       BEGIN ipkt;
  349.     else
  350.       RESUME;
  351. }
  352. g {                    /* Client sends a REMOTE command */
  353.     TINIT;
  354.     vstate = rgen;
  355.     vcmd = 'G';
  356.     if (sipkt('I') >= 0)
  357.       BEGIN ipkt;
  358.     else
  359.       RESUME;
  360. }
  361. x {                    /* Enter server mode */
  362.     int x;
  363.     x = justone;
  364.     if (!ENABLED(en_del)) {        /* If DELETE is disabled */
  365.     if (fncact == XYFX_B ||        /* undo any file collision action */
  366.         fncact == XYFX_U ||        /* that could result in deletion or */
  367.         fncact == XYFX_A ||        /* modification of existing files. */
  368.         fncact == XYFX_X) {
  369. #ifndef NOICP
  370.         extern int g_fncact;
  371.         g_fncact = fncact;        /* Save current setting */
  372. #endif /* NOICP */
  373.         fncact = XYFX_R;        /* Change to RENAME */
  374.         debug(F101,"server DELETE disabled so fncact RENAME","",fncact);
  375.     }
  376.     }
  377.     SERVE;                /* tinit() clears justone... */
  378.     justone = x;
  379. #ifdef IKSDB
  380.     if (ikdbopen) slotstate(what, "SERVER", "", "");
  381. #endif /* IKSDB */
  382. }
  383.  
  384. a {
  385.     int b1 = 0, b2 = 0;
  386.     if (!data) TINIT;            /* "ABEND" -- Tell other side. */
  387. #ifndef pdp11
  388.     if (epktflg) {            /* If because of E-PACKET command */
  389.     b1 = bctl; b2 = bctu;        /* Save block check type */
  390.     bctl = bctu = 1;        /* set it to 1 */
  391.     }
  392. #endif /* pdp11 */
  393.     errpkt((CHAR *)"User cancelled");    /* Send the packet */
  394. #ifndef pdp11
  395.     if (epktflg) {            /* Restore the block check */
  396.     epktflg = 0;
  397.     bctl = b1; bctu = b2;
  398.     }
  399. #endif /* pdp11 */
  400.     success = 0;
  401.     return(0);                /* Return from protocol. */
  402. }
  403.  
  404. /*
  405.   Dynamic states: <current-states>input-character { action }
  406.   nakstate != 0 means we're in a receiving state, in which we send ACKs & NAKs.
  407. */
  408.  
  409. <rgen,get,serve,ropkt>S {        /* Receive Send-Init packet. */
  410.     rc = rcv_s_pkt();
  411.     cancel = 0;                /* Reset cancellation counter */
  412.     debug(F101,"rcv_s_pkt","",rc);
  413.     if (rc > -1) return(rc);        /* (see below) */
  414. }
  415.  
  416. /* States in which we get replies back from commands sent to a server. */
  417. /* Complicated because direction of protocol changes, packet number    */
  418. /* stays at zero through I-G-S sequence, and complicated even more by  */
  419. /* sliding windows buffer allocation. */
  420.  
  421. <ipkt>Y {                /* Get ack for I-packet */
  422.     int x = 0;
  423. #ifdef PKTZEROHACK
  424.     ckstrncpy(ipktack,(char *)rdatap,PKTZEROLEN); /* Save a copy of the ACK */
  425.     ipktlen = strlen(ipktack);
  426. #endif /* PKTZEROHACK */
  427.     spar(rdatap);            /* Set parameters */
  428.     cancel = 0;
  429.     winlo = 0;                /* Set window-low back to zero */
  430.     debug(F101,"<ipkt>Y winlo","",winlo);
  431.     urserver = 1;            /* So I know I'm talking to a server */
  432.     if (vcmd) {                /* If sending a generic command */
  433.     if (tinit(0) < 0) return(-9);    /* Initialize many things */
  434.     x = scmd(vcmd,(CHAR *)cmarg);    /* Do that */
  435.     if (x >= 0) x = 0;        /* (because of O-Packet) */
  436.     debug(F101,"proto G packet scmd","",x);
  437.     vcmd = 0;            /* and then un-remember it. */
  438.     } else if (vstate == get) {
  439.     debug(F101,"REGET sstate","",sstate);
  440.     x = srinit(reget, retrieve, opkt); /* GET or REGET, etc */
  441.     }
  442.     if (x < 0) {            /* If command was too long */
  443.     if (!srimsg)
  444.       srimsg = "Error sending string";
  445.     errpkt((CHAR *)srimsg);        /* cancel both sides. */
  446.     success = 0;
  447.     RESUME;
  448.     } else if (x > 0) {            /* Need to send more O-Packets */
  449.     BEGIN ssopkt;
  450.     } else {
  451.     rtimer();            /* Reset the elapsed seconds timer. */
  452. #ifdef GFTIMER
  453.     rftimer();
  454. #endif /* GFTIMER */
  455.     winlo = 0;            /* Window back to 0, again. */
  456.     debug(F101,"<ipkt>Y vstate","",vstate);
  457.     nakstate = 1;            /* Can send NAKs from here. */
  458.     BEGIN vstate;            /* Switch to desired state */
  459.     }
  460. }
  461.  
  462. <ssopkt>Y {                /* Got ACK to O-Packet */
  463.     debug(F100,"CPCPRO <ssopkt>Y","",0);
  464.     x = sopkt();
  465.     debug(F101,"CPCPRO <ssopkt>Y x","",x);
  466.     if (x < 0) {            /* If error */
  467.     errpkt((CHAR *)srimsg);        /* cancel both sides. */
  468.     success = 0;
  469.     RESUME;
  470.     } else if (x == 0) {        /* This was the last O-Packet */
  471.     rtimer();            /* Reset the elapsed seconds timer. */
  472. #ifdef GFTIMER
  473.     rftimer();
  474. #endif /* GFTIMER */
  475.     winlo = 0;            /* Window back to 0, again. */
  476.     debug(F101,"<ssopkt>Y winlo","",winlo);
  477.     nakstate = 1;            /* Can send NAKs from here. */
  478.     BEGIN vstate;            /* Switch to desired state */
  479.     }
  480.     debug(F101,"CPCPRO <ssopkt>Y not changing state","",x);
  481. }
  482.  
  483. <ipkt>E {                /* Ignore Error reply to I packet */
  484.     int x = 0;
  485.     winlo = 0;                /* Set window-low back to zero */
  486.     debug(F101,"<ipkt>E winlo","",winlo);
  487.     if (vcmd) {                /* In case other Kermit doesn't */
  488.     if (tinit(0) < 0) return(-9);
  489.     x = scmd(vcmd,(CHAR *)cmarg);    /* understand I-packets. */
  490.     if (x >= 0) x = 0;        /* (because of O-Packet) */
  491.     vcmd = 0;            /* Otherwise act as above... */
  492.     } else if (vstate == get) x = srinit(reget, retrieve, opkt);
  493.     if (x < 0) {            /* If command was too long */
  494.     errpkt((CHAR *)srimsg);        /* cancel both sides. */
  495.     success = 0;
  496.     RESUME;
  497.     } else if (x > 0) {            /* Need to send more O-Packets */
  498.     BEGIN ssopkt;
  499.     } else {
  500.     freerpkt(winlo);        /* Discard the Error packet. */
  501.     debug(F101,"<ipkt>E winlo","",winlo);
  502.     winlo = 0;            /* Back to packet 0 again. */
  503.     nakstate = 1;            /* Can send NAKs from here. */
  504.     BEGIN vstate;
  505.     }
  506. }
  507.  
  508. <get>Y {        /* Resend of previous I-pkt ACK, same seq number */
  509.     freerpkt(0);            /* Free the ACK's receive buffer */
  510.     resend(0);                /* Send the GET packet again. */
  511. }
  512.  
  513. /* States in which we're being a server */
  514.  
  515. <serve,get>I {                /* Get I-packet */
  516. #ifndef NOSERVER
  517.     spar(rdatap);            /* Set parameters from it */
  518.     ack1(rpar());            /* Respond with our own parameters */
  519. #ifdef COMMENT
  520.     pktinit();                /* Reinitialize packet numbers */
  521. #else
  522. #ifdef COMMENT
  523.     /* This can't be right - it undoes the stuff we just negotiated */
  524.     x = justone;
  525.     tinit(1);                /* Reinitialize EVERYTHING */
  526.     justone = x;            /* But this... */
  527. #else
  528.     tinit(0);                /* Initialize most things */
  529. #endif /* COMMENT */
  530. #endif /* COMMENT */
  531. #endif /* NOSERVER */
  532.     cancel = 0;                /* Reset cancellation counter */
  533. }
  534.  
  535. <serve>R {                /* GET */
  536. #ifndef NOSERVER
  537.     if (x_login && !x_logged) {
  538.     errpkt((CHAR *)"Login required");
  539.     SERVE;
  540.     } else if (sgetinit(0,0) < 0) {
  541.     RESUME;
  542.     } else {
  543. #ifdef CKSYSLOG
  544.     if (ckxsyslog >= SYSLG_PR && ckxlogging)
  545.       cksyslog(SYSLG_PR, 1, "server", "GET", (char *)srvcmd);
  546. #endif /* CKSYSLOG */
  547.     BEGIN ssinit;
  548.     }
  549. #endif /* NOSERVER */
  550. }
  551.  
  552. <serve>H {                /* GET /DELETE (RETRIEVE) */
  553. #ifndef NOSERVER
  554.     if (x_login && !x_logged) {
  555.     errpkt((CHAR *)"Login required");
  556.     RESUME;
  557.     } else if (!ENABLED(en_del)) {
  558.     errpkt((CHAR *)"Deleting files is disabled");
  559.     RESUME;
  560.     } else if (sgetinit(0,0) < 0) {
  561.     RESUME;
  562.     } else {
  563.     moving = 1;
  564. #ifdef CKSYSLOG
  565.     if (ckxsyslog >= SYSLG_PR && ckxlogging)
  566.       cksyslog(SYSLG_PR, 1, "server", "GET /DELETE", (char *)srvcmd);
  567. #endif /* CKSYSLOG */
  568.     BEGIN ssinit;
  569.     }
  570. #endif /* NOSERVER */
  571. }
  572.  
  573. <serve>V {                /* GET /RECURSIVE */
  574. #ifndef NOSERVER
  575.     recursive = 1;            /* Set these before sgetinit() */
  576.     if (fnspath == PATH_OFF)
  577.       fnspath = PATH_REL;        /* Don't worry, they will be */
  578.     if (x_login && !x_logged) {        /* reset next time through. */
  579.     errpkt((CHAR *)"Login required");
  580.     RESUME;
  581.     } else if (sgetinit(0,0) < 0) {
  582.     RESUME;
  583.     } else {
  584. #ifdef CKSYSLOG
  585.     if (ckxsyslog >= SYSLG_PR && ckxlogging)
  586.       cksyslog(SYSLG_PR, 1, "server", "GET /RECURSIVE", (char *)srvcmd);
  587. #endif /* CKSYSLOG */
  588.     BEGIN ssinit;
  589.     }
  590. #endif /* NOSERVER */
  591. }
  592.  
  593. <serve>W {                /* GET /RECURSIVE /DELETE */
  594. #ifndef NOSERVER
  595.     recursive = 1;            /* Set these before sgetinit() */
  596.     if (fnspath == PATH_OFF)
  597.       fnspath = PATH_REL;        /* Don't worry, they will be */
  598.     moving = 1;                /* reset next time through. */
  599.     if (x_login && !x_logged) {
  600.     errpkt((CHAR *)"Login required");
  601.     RESUME;
  602.     } else if (!ENABLED(en_del)) {
  603.     errpkt((CHAR *)"Deleting files is disabled");
  604.     RESUME;
  605.     } else if (sgetinit(0,0) < 0) {
  606.     RESUME;
  607.     } else {
  608. #ifdef CKSYSLOG
  609.     if (ckxsyslog >= SYSLG_PR && ckxlogging)
  610.       cksyslog(SYSLG_PR,1,"server",
  611.            "GET /RECURSIVE /DELETE",(char *)srvcmd);
  612. #endif /* CKSYSLOG */
  613.     BEGIN ssinit;
  614.     }
  615. #endif /* NOSERVER */
  616. }
  617.  
  618. <serve>J {                /* GET /RECOVER (REGET) */
  619. #ifndef NOSERVER
  620.     if (x_login && !x_logged) {
  621.     errpkt((CHAR *)"Login required");
  622.     SERVE;
  623.     } else if (sgetinit(1,0) < 0) {
  624.     RESUME;
  625.     } else {
  626. #ifdef CKSYSLOG
  627.     if (ckxsyslog >= SYSLG_PR && ckxlogging)
  628.       cksyslog(SYSLG_PR, 1, "server", "GET /RECOVER", (char *)srvcmd);
  629. #endif /* CKSYSLOG */
  630.     BEGIN ssinit;
  631.     }
  632. #endif /* NOSERVER */
  633. }
  634.  
  635. <serve>O {                /* Extended GET */
  636. #ifndef NOSERVER
  637.     if (x_login && !x_logged) {        /* (any combination of options) */
  638.     errpkt((CHAR *)"Login required");
  639.     SERVE;
  640.     } else if ((x = sgetinit(0,1)) < 0) {
  641.     debug(F101,"CKCPRO <serve>O sgetinit fail","",x);
  642.     RESUME;
  643.     } else if (x == 0) {
  644.     debug(F101,"CKCPRO <serve>O sgetinit done","",x);
  645. #ifdef CKSYSLOG
  646.     if (ckxsyslog >= SYSLG_PR && ckxlogging)
  647.       cksyslog(SYSLG_PR, 1, "server", "EXTENDED GET", (char *)srvcmd);
  648. #endif /* CKSYSLOG */
  649.     BEGIN ssinit;
  650.     } else {                /* Otherwise stay in this state */
  651.     debug(F101,"CKCPRO <serve>O sgetinit TBC","",x);
  652.     ack();
  653.     BEGIN ropkt;
  654.     }
  655. #endif /* NOSERVER */
  656. }
  657.  
  658. <ropkt>O {
  659. #ifndef NOSERVER
  660.     if (x_login && !x_logged) {        /* (any combination of options) */
  661.     errpkt((CHAR *)"Login required");
  662.     SERVE;
  663.     } else if ((x = sgetinit(0,1)) < 0) {
  664.     debug(F101,"CKCPRO <ropkt>O sgetinit fail","",x);
  665.     RESUME;
  666.     } else if (x == 0) {
  667.     debug(F101,"CKCPRO <ropkt>O sgetinit done","",x);
  668. #ifdef CKSYSLOG
  669.     if (ckxsyslog >= SYSLG_PR && ckxlogging)
  670.       cksyslog(SYSLG_PR, 1, "server", "EXTENDED GET", (char *)srvcmd);
  671. #endif /* CKSYSLOG */
  672.     BEGIN ssinit;
  673.     } else {                /* Otherwise stay in this state */
  674.     debug(F101,"CKCPRO <ropkt>O sgetinit TBC","",x);
  675.     ack();
  676.     }
  677. #endif /* NOSERVER */
  678. }
  679.  
  680. <serve>G {                /* Generic server command */
  681. #ifndef NOSERVER
  682.     srvptr = srvcmd;            /* Point to command buffer */
  683.     decode(rdatap,putsrv,0);        /* Decode packet data into it */
  684.     putsrv(NUL);            /* Insert a couple nulls */
  685.     putsrv(NUL);            /* for termination */
  686.     if (srvcmd[0]) {
  687.     sstate = srvcmd[0];        /* Set requested start state */
  688.     if (x_login && !x_logged &&    /* Login required? */
  689.         /* Login, Logout, and Help are allowed when not logged in */
  690.         sstate != 'I' && sstate != 'L' && sstate != 'H') {
  691.         errpkt((CHAR *)"Login required");
  692.         SERVE;
  693.     } else {
  694.         nakstate = 0;        /* Now I'm the sender. */
  695.         what = W_REMO;        /* Doing a REMOTE command. */
  696. #ifdef STREAMING
  697.         if (!streaming)
  698. #endif /* STREAMING */
  699.           if (timint < 1)
  700.         timint = chktimo(rtimo,timef); /* Switch to per-packet timer */
  701.         binary = XYFT_T;        /* Switch to text mode */
  702.         BEGIN generic;        /* Switch to generic command state */
  703.     }
  704.     } else {
  705.     errpkt((CHAR *)"Badly formed server command"); /* report error */
  706.     RESUME;            /* & go back to server command wait */
  707.     }
  708. #endif /* NOSERVER */
  709. }
  710.  
  711. <serve>C {                /* Receive Host command */
  712. #ifndef NOSERVER
  713.     if (x_login && !x_logged) {
  714.     errpkt((CHAR *)"Login required");
  715.     SERVE;
  716.     } else if (!ENABLED(en_hos)) {
  717.     errpkt((CHAR *)"REMOTE HOST disabled");
  718.     RESUME;
  719.     } else if (nopush) {
  720.     errpkt((CHAR *)"HOST commands not available");
  721.     RESUME;
  722.     } else {
  723.     srvptr = srvcmd;        /* Point to command buffer */
  724.     decode(rdatap,putsrv,0);    /* Decode command packet into it */
  725.     putsrv(NUL);            /* Null-terminate */
  726.     nakstate = 0;            /* Now sending, not receiving */
  727.     binary = XYFT_T;        /* Switch to text mode */
  728.     if (syscmd((char *)srvcmd,"")) { /* Try to execute the command */
  729.         what = W_REMO;        /* Doing a REMOTE command. */
  730. #ifdef STREAMING
  731.         if (!streaming)
  732. #endif /* STREAMING */
  733.           if (timint < 1)
  734.         timint = chktimo(rtimo,timef); /* Switch to per-packet timer */
  735. #ifdef CKSYSLOG
  736.         if (ckxsyslog >= SYSLG_PR && ckxlogging)
  737.           cksyslog(SYSLG_PR, 1, "server", "REMOTE HOST", (char *)srvcmd);
  738. #endif /* CKSYSLOG */
  739.         BEGIN ssinit;        /* If OK, send back its output */
  740.     } else {            /* Otherwise */
  741.         errpkt((CHAR *)"Can't do system command"); /* report error */
  742.         RESUME;            /* & go back to server command wait */
  743.     }
  744.     }
  745. #endif /* NOSERVER */
  746. }
  747.  
  748. <serve>q {                /* Interrupted or connection lost */
  749.     rc = srv_timeout();
  750.     debug(F101,"srv_timeout","",rc);
  751.     if (rc > -1) return(rc);        /* (see below) */
  752. }
  753.  
  754. <serve>N {                /* Server got a NAK in command-wait */
  755. #ifndef NOSERVER
  756.     errpkt((CHAR *)"Did you say RECEIVE instead of GET?");
  757.     RESUME;
  758. #endif /* NOSERVER */
  759. }
  760.  
  761. <serve>. {                /* Any other command in this state */
  762. #ifndef NOSERVER
  763.     if (c != ('E' - SP) && c != ('Y' - SP)) /* except E and Y packets. */
  764.       errpkt((CHAR *)"Unimplemented server function");
  765.     /* If we answer an E with an E, we get an infinite loop. */
  766.     /* A Y (ACK) can show up here if we sent back a short-form reply to */
  767.     /* a G packet and it was echoed.  ACKs can be safely ignored here. */
  768.     RESUME;                /* Go back to server command wait. */
  769. #endif /* NOSERVER */
  770. }
  771.  
  772. <generic>I {                /* Login/Out */
  773.     rc = srv_login();
  774.     debug(F101,"<generic>I srv_login","",rc);
  775.     if (rc > -1) return(rc);        /* (see below) */
  776. }
  777.  
  778. <generic>C {                /* Got REMOTE CD command */
  779. #ifndef NOSERVER
  780. #ifdef CKSYSLOG
  781.     if (ckxsyslog >= SYSLG_PR && ckxlogging)
  782.       cksyslog(SYSLG_PR, 1, "server", "REMOTE CD", (char *)srvcmd);
  783. #endif /* CKSYSLOG */
  784.     if (!ENABLED(en_cwd)) {
  785.     errpkt((CHAR *)"REMOTE CD disabled");
  786.     RESUME;
  787.     } else {
  788.     char * p = NULL;
  789.     x = cwd((char *)(srvcmd+1));    /* Try to change directory */
  790. #ifdef IKSDB
  791.     if (ikdbopen) slotstate(what,"REMOTE CD", (char *)(srvcmd+2), "");
  792. #endif /* IKSDB */
  793.     if (!x) {            /* Failed */
  794.         errpkt((CHAR *)"Can't change directory");
  795.         RESUME;            /* Back to server command wait */
  796.     } else if (x == 2) {        /* User wants message */
  797.         if (!ENABLED(en_typ)) {    /* Messages (REMOTE TYPE) disabled? */
  798.         errpkt((CHAR *)"REMOTE TYPE disabled");
  799.         RESUME;
  800.         } else {            /* TYPE is enabled */
  801.         int i;
  802.         for (i = 0; i < 8; i++) {
  803.             if (zchki(cdmsgfile[i]) > -1) {
  804.             break;
  805.             }
  806.         }
  807.         binary = XYFT_T;    /* Use text mode for this. */
  808.         if (i < 8 && sndtype(cdmsgfile[i])) { /* Have readme file? */
  809.             BEGIN ssinit;    /* OK */
  810.         } else {        /* not OK */
  811.             p = zgtdir();
  812.             if (!p) p = "";
  813.             success = (*p) ? 1 : 0;
  814.             ack1((CHAR *)p);    /* ACK with new directory name */
  815.             success = 1;
  816.             RESUME;        /* wait for next server command */
  817.         }
  818.         }
  819.     } else {            /* User doesn't want message */
  820.         p =zgtdir();
  821.         if (!p) p = "";
  822.         success = (*p) ? 1 : 0;
  823.         ack1((CHAR *)p);
  824.         success = 1;
  825.         RESUME;            /* Wait for next server command */
  826.     }
  827.     }
  828. #endif /* NOSERVER */
  829. }
  830.  
  831. <generic>A {                /* Got REMOTE PWD command */
  832. #ifndef NOSERVER
  833. #ifdef CKSYSLOG
  834.     if (ckxsyslog >= SYSLG_PR && ckxlogging)
  835.       cksyslog(SYSLG_PR, 1, "server", "REMOTE PWD", NULL);
  836. #endif /* CKSYSLOG */
  837.     if (!ENABLED(en_cwd)) {
  838.     errpkt((CHAR *)"REMOTE CD disabled");
  839.     RESUME;
  840.     } else {
  841.     if (encstr((CHAR *)zgtdir()) > -1) { /* Encode current directory */
  842.         ack1(data);            /* If it fits, send it back in ACK */
  843.         success = 1;
  844.     } else {            /* Failed */
  845.         ack();            /* Send empty ACK */
  846.         success = 0;        /* and indicate failure locally */
  847.     }
  848.     RESUME;                /* Back to server command wait */
  849.     }
  850. #endif /* NOSERVER */
  851. }
  852.  
  853. <generic>D {                /* REMOTE DIRECTORY command */
  854. #ifndef NOSERVER
  855.     char *n2;
  856. #ifdef CKSYSLOG
  857.     if (ckxsyslog >= SYSLG_PR && ckxlogging)
  858.       cksyslog(SYSLG_PR, 1, "server", "REMOTE DIRECTORY", (char *)srvcmd);
  859. #endif /* CKSYSLOG */
  860.     if (!ENABLED(en_dir)) {        /* If DIR is disabled, */
  861.     errpkt((CHAR *)"REMOTE DIRECTORY disabled"); /* refuse. */
  862.     RESUME;
  863.     } else {                /* DIR is enabled. */
  864. #ifdef IKSDB
  865.     if (ikdbopen) slotstate(what,"REMOTE DIR", (char *)(srvcmd+2), "");
  866. #endif /* IKSDB */
  867.     if (!ENABLED(en_cwd)) {        /* But CWD is disabled */
  868.         zstrip((char *)(srvcmd+2),&n2); /* and they included a pathname, */
  869.         if (strcmp((char *)(srvcmd+2),n2)) { /* so refuse. */
  870.         errpkt((CHAR *)"Access denied");
  871.         RESUME;            /* Remember, this is not a goto! */
  872.         }
  873.     }
  874.     if (state == generic) {            /* It's OK to go ahead. */
  875. #ifdef COMMENT
  876.         n2 = (*(srvcmd+2)) ? DIRCMD : DIRCM2;
  877.         if (syscmd(n2,(char *)(srvcmd+2)))  /* If it can be done */
  878. #else
  879.         int x;
  880.         if ((x = snddir((char*)(srvcmd+2))) > 0)
  881. #endif /* COMMENT */
  882.         {
  883.         BEGIN ssinit;        /* send the results back; */
  884.         } else {            /* otherwise */
  885.         if (x < 0)
  886.           errpkt((CHAR *)"No files match");
  887.         else
  888.           errpkt((CHAR *)"Can't list directory");
  889.         RESUME;            /* return to server command wait */
  890.         }
  891.     }
  892.     }
  893. #endif /* NOSERVER */
  894. }
  895.  
  896. <generic>E {                /* REMOTE DELETE (Erase) */
  897. #ifndef NOSERVER
  898.     char *n2;
  899. #ifdef CKSYSLOG
  900.     if (ckxsyslog >= SYSLG_PR && ckxlogging)
  901.       cksyslog(SYSLG_PR, 1, "server", "REMOTE DELETE", (char *)srvcmd);
  902. #endif /* CKSYSLOG */
  903.     if (!ENABLED(en_del)) {
  904.     errpkt((CHAR *)"REMOTE DELETE disabled");
  905.     RESUME;
  906.     } else {                /* DELETE is enabled */
  907. #ifdef IKSDB
  908.     if (ikdbopen) slotstate(what,"REMOTE DELETE", (char *)(srvcmd+2), "");
  909. #endif /* IKSDB */
  910.     if (!ENABLED(en_cwd)) {        /* but CWD is disabled */
  911.         zstrip((char *)(srvcmd+2),&n2); /* and they included a pathname, */
  912.         if (strcmp((char *)(srvcmd+2),n2)) { /* so refuse. */
  913.         errpkt((CHAR *)"Access denied");
  914.         RESUME;            /* Remember, this is not a goto! */
  915.         }
  916.     } else if (isdir((char *)(srvcmd+2))) { /* A directory name? */
  917.         errpkt((CHAR *)"It's a directory");
  918.         RESUME;
  919.     }
  920.     if (state == generic) {        /* It's OK to go ahead. */
  921.         int x;
  922.         if ((x = snddel((char*)(srvcmd+2))) > 0) {
  923.         BEGIN ssinit;        /* If OK send results back */
  924.         } else {            /* otherwise */
  925.         if (x < 0)
  926.           errpkt((CHAR *)"File not found"); /* report failure */
  927.         else
  928.           errpkt((CHAR *)"DELETE failed");
  929.         RESUME;            /* & return to server command wait */
  930.         }
  931.     }
  932.     }
  933. #endif /* NOSERVER */
  934. }
  935.  
  936. <generic>F {                /* FINISH */
  937. #ifndef NOSERVER
  938. #ifdef CKSYSLOG
  939.     if (ckxsyslog >= SYSLG_PR && ckxlogging)
  940.       cksyslog(SYSLG_PR, 1, "server", "FINISH", NULL);
  941. #endif /* CKSYSLOG */
  942. #ifdef IKSDB
  943.     if (ikdbopen) slotstate(what,"SERVER FINISH", "", "");
  944. #endif /* IKSDB */
  945.     if (!ENABLED(en_fin)) {
  946.     errpkt((CHAR *)"FINISH disabled");
  947.     RESUME;
  948.     } else {
  949.     ack();                /* Acknowledge */
  950.     xxscreen(SCR_TC,0,0L,"");    /* Display */
  951.     success = 1;
  952.     return(0);            /* Done */
  953.     }
  954. #endif /* NOSERVER */
  955. }
  956.  
  957. <generic>X {                /* EXIT */
  958. #ifndef NOSERVER
  959. #ifdef CKSYSLOG
  960.     if (ckxsyslog >= SYSLG_PR && ckxlogging)
  961.       cksyslog(SYSLG_PR, 1, "server", "REMOTE EXIT", NULL);
  962. #endif /* CKSYSLOG */
  963. #ifdef IKSDB
  964.     if (ikdbopen) slotstate(what,"REMOTE EXIT", "", "");
  965. #endif /* IKSDB */
  966.     if (!ENABLED(en_xit)) {
  967.     errpkt((CHAR *)"EXIT disabled");
  968.     RESUME;
  969.     } else {
  970.     ack();                /* Acknowledge */
  971.     xxscreen(SCR_TC,0,0L,"");    /* Display */
  972.     doexit(GOOD_EXIT,xitsta);
  973.     }
  974. #endif /* NOSERVER */
  975. }
  976.  
  977. <generic>L {                /* BYE (Logout) */
  978. #ifndef NOSERVER
  979. #ifdef CKSYSLOG
  980.     if (ckxsyslog >= SYSLG_PR && ckxlogging)
  981.       cksyslog(SYSLG_PR, 1, "server", "BYE", NULL);
  982. #endif /* CKSYSLOG */
  983. #ifdef IKSDB
  984.     if (ikdbopen) slotstate(what,"SERVER BYE", "", "");
  985. #endif /* IKSDB */
  986.     if (!ENABLED(en_bye)) {
  987.     errpkt((CHAR *)"BYE disabled");
  988.     RESUME;
  989.     } else {
  990.     ack();                /* Acknowledge */
  991.     success = 1;
  992.     msleep(750);            /* Give the ACK time to get out */
  993.     if (local)
  994.       ttres();            /* Reset the terminal */
  995.     xxscreen(SCR_TC,0,0L,"");    /* Display */
  996.     doclean(1);            /* Clean up files, etc */
  997. #ifdef DEBUG
  998.     debug(F100,"C-Kermit BYE - Loggin out...","",0);
  999.     zclose(ZDFILE);
  1000. #endif /* DEBUG */
  1001. #ifdef IKSD
  1002. #ifdef CK_LOGIN
  1003.     if (inserver)
  1004.       ckxlogout();
  1005.     else
  1006. #endif /* CK_LOGIN */
  1007. #endif /* IKSD */
  1008. #ifdef TCPSOCKET
  1009. #ifndef NOLISTEN
  1010.       if (network && tcpsrfd > 0 && !inserver)
  1011.         doexit(GOOD_EXIT,xitsta);
  1012.     else
  1013. #endif /* NOLISTEN */
  1014. #endif /* TCPSOCKET */
  1015.       return(zkself());        /* Try to log self out */
  1016.     }
  1017. #endif /* NOSERVER */
  1018. }
  1019.  
  1020. <generic>H {                /* REMOTE HELP */
  1021. #ifdef CKSYSLOG
  1022.     if (ckxsyslog >= SYSLG_PR && ckxlogging)
  1023.       cksyslog(SYSLG_PR, 1, "server", "REMOTE HELP", NULL);
  1024. #endif /* CKSYSLOG */
  1025. #ifdef IKSDB
  1026.     if (ikdbopen) slotstate(what,"REMOTE HELP", "", "");
  1027. #endif /* IKSDB */
  1028. #ifndef NOSERVER
  1029.     if (sndhlp(NULL)) {
  1030.     BEGIN ssinit;            /* try to send it */
  1031.     } else {                /* If not ok, */
  1032.     errpkt((CHAR *)"Can't send help"); /* send error message instead */
  1033.     RESUME;                /* and return to server command wait */
  1034.     }
  1035. #endif /* NOSERVER */
  1036. }
  1037.  
  1038. <generic>R {                            /* REMOTE RENAME */
  1039.     rc = srv_rename();
  1040.     debug(F101,"srv_rename","",rc);
  1041.     if (rc > -1) return(rc);        /* (see below) */
  1042. }
  1043.  
  1044. <generic>K {                            /* REMOTE COPY */
  1045.     rc = srv_copy();
  1046.     debug(F101,"srv_copy","",rc);
  1047.     if (rc > -1) return(rc);        /* (see below) */
  1048. }
  1049.  
  1050. <generic>S {                /* REMOTE SET */
  1051. #ifdef CKSYSLOG
  1052.     if (ckxsyslog >= SYSLG_PR && ckxlogging)
  1053.       cksyslog(SYSLG_PR, 1, "server", "REMOTE SET", (char *)srvcmd);
  1054. #endif /* CKSYSLOG */
  1055. #ifndef NOSERVER
  1056. #ifdef IKSDB
  1057.     if (ikdbopen) slotstate(what,"REMOTE SET", (char *)(srvcmd+1), "");
  1058. #endif /* IKSDB */
  1059.     if (!ENABLED(en_set)) {
  1060.     errpkt((CHAR *)"REMOTE SET disabled");
  1061.     RESUME;
  1062.     } else {
  1063.     if (remset((char *)(srvcmd+1))) { /* Try to do what they ask */
  1064.         success = 1;
  1065.         ack();            /* If OK, then acknowledge */
  1066.     } else                /* Otherwise */
  1067.       errpkt((CHAR *)"Unknown REMOTE SET parameter"); /* give error msg */
  1068.     RESUME;                /* Return to server command wait */
  1069.     }
  1070. #endif /* NOSERVER */
  1071. }
  1072.  
  1073. <generic>T {                /* REMOTE TYPE */
  1074. #ifndef NOSERVER
  1075.     char *n2;
  1076. #ifdef CKSYSLOG
  1077.     if (ckxsyslog >= SYSLG_PR && ckxlogging)
  1078.       cksyslog(SYSLG_PR, 1, "server", "REMOTE TYPE", (char *)srvcmd);
  1079. #endif /* CKSYSLOG */
  1080.     if (!ENABLED(en_typ)) {
  1081.     errpkt((CHAR *)"REMOTE TYPE disabled");
  1082.     RESUME;
  1083.     } else {
  1084. #ifdef IKSDB
  1085.     if (ikdbopen) slotstate(what,"REMOTE TYPE", (char *)(srvcmd+2), "");
  1086. #endif /* IKSDB */
  1087.     if (!ENABLED(en_cwd)) {        /* If CWD disabled */
  1088.         zstrip((char *)(srvcmd+2),&n2); /* and they included a pathname, */
  1089.         if (strcmp((char *)(srvcmd+2),n2)) { /* refuse. */
  1090.         errpkt((CHAR *)"Access denied");
  1091.         RESUME;            /* Remember, this is not a goto! */
  1092.         }
  1093.     }
  1094.     if (state == generic) {        /* It's OK to go ahead. */
  1095.         binary = XYFT_T;        /* Use text mode for this. */
  1096.         if (            /* (RESUME didn't change state) */
  1097. #ifdef COMMENT
  1098.           syscmd(TYPCMD,(char *)(srvcmd+2))    /* Old way */
  1099. #else
  1100.           sndtype((char *)(srvcmd+2)) /* New way */
  1101. #endif /* COMMENT */
  1102.         )
  1103.           BEGIN ssinit;        /* OK */
  1104.         else {            /* not OK */
  1105.         errpkt((CHAR *)"Can't type file"); /* give error message */
  1106.         RESUME;            /* wait for next server command */
  1107.         }
  1108.     }
  1109.     }
  1110. #endif /* NOSERVER */
  1111. }
  1112.  
  1113. <generic>m {                /* REMOTE MKDIR */
  1114. #ifndef NOSERVER
  1115. #ifdef CK_MKDIR
  1116. #ifdef CKSYSLOG
  1117.     if (ckxsyslog >= SYSLG_PR && ckxlogging)
  1118.       cksyslog(SYSLG_PR, 1, "server", "REMOTE MKDIR", (char *)srvcmd);
  1119. #endif /* CKSYSLOG */
  1120. #ifdef IKSDB
  1121.     if (ikdbopen) slotstate(what,"REMOTE MKDIR", (char *)(srvcmd+2), "");
  1122. #endif /* IKSDB */
  1123.     if (!ENABLED(en_mkd)) {
  1124.     errpkt((CHAR *)"REMOTE MKDIR disabled");
  1125.     RESUME;
  1126.     } else if (!ENABLED(en_cwd)) {    /* If CWD disabled */
  1127.     errpkt((CHAR *)"Directory access restricted");
  1128.     RESUME;                /* Remember, this is not a goto! */
  1129.     }
  1130.     if (state == generic) {        /* OK to go ahead. */
  1131.     char *p = NULL;
  1132.     x = ckmkdir(0,(char *)(srvcmd+2),&p,0,1); /* Make the directory */
  1133.     if (!p) p = "";
  1134.     if (x > -1) {
  1135.         encstr((CHAR *)p);        /* OK - encode the name */
  1136.         ack1(data);            /* Send short-form response */
  1137.         success = 1;
  1138.         RESUME;
  1139.     } else {            /* not OK */
  1140.         if (!*p) p = "Directory creation failure";
  1141.         errpkt((CHAR *)p);        /* give error message */
  1142.         RESUME;            /* Wait for next server command */
  1143.     }
  1144.     }
  1145. #else
  1146.     errpkt((CHAR *)"REMOTE MKDIR not available");
  1147.     RESUME;
  1148. #endif /* CK_MKDIR */
  1149. #endif /* NOSERVER */
  1150. }
  1151.  
  1152. <generic>d {                /* REMOTE RMDIR */
  1153. #ifndef NOSERVER
  1154. #ifdef CK_MKDIR
  1155. #ifdef CKSYSLOG
  1156.     if (ckxsyslog >= SYSLG_PR && ckxlogging)
  1157.       cksyslog(SYSLG_PR, 1, "server", "REMOTE RMDIR", (char *)srvcmd);
  1158. #endif /* CKSYSLOG */
  1159. #ifdef IKSDB
  1160.     if (ikdbopen) slotstate(what,"REMOTE RMDIR", (char *)(srvcmd+2), "");
  1161. #endif /* IKSDB */
  1162.     if (!ENABLED(en_rmd)) {
  1163.     errpkt((CHAR *)"REMOTE RMDIR disabled");
  1164.     RESUME;
  1165.     } else if (!ENABLED(en_cwd)) {    /* If CWD disabled */
  1166.     errpkt((CHAR *)"Directory access restricted");
  1167.     RESUME;                /* Remember, this is not a goto! */
  1168.     }
  1169.     if (state == generic) {        /* OK to go ahead. */
  1170.     char *p = NULL;
  1171.     x = ckmkdir(1,(char *)(srvcmd+2),&p,0,1);
  1172.     if (!p) p = "";
  1173.     if (x > -1) {
  1174.         encstr((CHAR *)p);        /* OK - encode the name */
  1175.         ack1(data);            /* Send short-form response */
  1176.         success = 1;
  1177.         RESUME;
  1178.     } else {            /* not OK */
  1179.         if (!*p) p = "Directory removal failure";
  1180.         errpkt((CHAR *)p);        /* give error message */
  1181.         RESUME;            /* Wait for next server command */
  1182.     }
  1183.     }
  1184. #else
  1185.     errpkt((CHAR *)"REMOTE RMDIR not available");
  1186.     RESUME;
  1187. #endif /* CK_MKDIR */
  1188. #endif /* NOSERVER */
  1189. }
  1190.  
  1191. <generic>U {                /* REMOTE SPACE */
  1192. #ifndef NOSERVER
  1193. #ifdef CKSYSLOG
  1194.     if (ckxsyslog >= SYSLG_PR && ckxlogging)
  1195.       cksyslog(SYSLG_PR, 1, "server", "REMOTE SPACE", (char *)srvcmd);
  1196. #endif /* CKSYSLOG */
  1197.     if (!ENABLED(en_spa)) {
  1198.     errpkt((CHAR *)"REMOTE SPACE disabled");
  1199.     RESUME;
  1200.     } else {
  1201.     x = srvcmd[1];            /* Get area to check */
  1202.     x = ((x == NUL) || (x == SP)
  1203. #ifdef OS2
  1204.          || (x == '!') || (srvcmd[3] == ':')
  1205. #endif /* OS2 */
  1206.          );
  1207. #ifdef IKSDB
  1208.     if (ikdbopen) slotstate(what,
  1209.                   "REMOTE SPACE",
  1210.                   (x ? "" : (char *)srvcmd),
  1211.                   ""
  1212.                   );
  1213. #endif /* IKSDB */
  1214.     if (!x && !ENABLED(en_cwd)) {    /* CWD disabled */
  1215.         errpkt((CHAR *)"Access denied"); /* and non-default area given, */
  1216.         RESUME;            /* refuse. */
  1217.     } else {
  1218. #ifdef OS2
  1219. _PROTOTYP(int sndspace,(int));
  1220.         if (sndspace(x ? toupper(srvcmd[2]) : 0)) {
  1221.         BEGIN ssinit;        /* send the report. */
  1222.         } else {            /* If not ok, */
  1223.         errpkt((CHAR *)"Can't send space"); /* send error message */
  1224.         RESUME;            /* and return to server command wait */
  1225.         }
  1226. #else
  1227.             if (nopush)
  1228.               x = 0;
  1229.             else
  1230.               x = (x ? syscmd(SPACMD,"") : syscmd(SPACM2,(char *)(srvcmd+2)));
  1231.         if (x) {            /* If we got the info */
  1232.         BEGIN ssinit;        /* send it */
  1233.         } else {            /* otherwise */
  1234.         errpkt((CHAR *)"Can't check space"); /* send error message */
  1235.         RESUME;            /* and await next server command */
  1236.         }
  1237. #endif /* OS2 */
  1238.     }
  1239.     }
  1240. #endif /* NOSERVER */
  1241. }
  1242.  
  1243. <generic>W {                /* REMOTE WHO */
  1244. #ifndef NOSERVER
  1245. #ifdef CKSYSLOG
  1246.     if (ckxsyslog >= SYSLG_PR && ckxlogging)
  1247.       cksyslog(SYSLG_PR, 1, "server", "REMOTE WHO", (char *)srvcmd);
  1248. #endif /* CKSYSLOG */
  1249. #ifdef IKSDB
  1250.     if (ikdbopen) slotstate(what,"REMOTE WHO", (char *)(srvcmd+2), "");
  1251. #endif /* IKSDB */
  1252.     if (!ENABLED(en_who)) {
  1253.     errpkt((CHAR *)"REMOTE WHO disabled");
  1254.     RESUME;
  1255.     } else {
  1256. #ifdef OS2
  1257. _PROTOTYP(int sndwho,(char *));
  1258.         if (sndwho((char *)(srvcmd+2))) {
  1259.         BEGIN ssinit;        /* try to send it */
  1260.         } else {            /* If not ok, */
  1261.         errpkt((CHAR *)"Can't do who command"); /* send error msg */
  1262.         RESUME;            /* and return to server command wait */
  1263.         }
  1264. #else
  1265.     if (syscmd(WHOCMD,(char *)(srvcmd+2))) {
  1266.         BEGIN ssinit;
  1267.     } else {
  1268.         errpkt((CHAR *)"Can't do who command");
  1269.         RESUME;
  1270.     }
  1271. #endif /* OS2 */
  1272.     }
  1273. #endif /* NOSERVER */
  1274. }
  1275.  
  1276. <generic>V {                /* Variable query or set */
  1277.     rc = srv_query();
  1278.     debug(F101,"srv_query","",rc);
  1279.     if (rc > -1) return(rc);
  1280. }
  1281.  
  1282. <generic>q {                /* Interrupted or connection lost */
  1283. #ifndef NOSERVER
  1284.     if (fatalio) {            /* Connection lost */
  1285. #ifdef CKSYSLOG
  1286.     if (ckxsyslog >= SYSLG_PR && ckxlogging)
  1287.       cksyslog(SYSLG_PR, 1, "server", "Interrupted", NULL);
  1288. #endif /* CKSYSLOG */
  1289.     success = 0;
  1290.     xitsta |= (what & W_KERMIT);
  1291.     QUIT;
  1292.     } else if (interrupted) {
  1293.     if (!ENABLED(en_fin)) {        /* Ctrl-C typed */
  1294.         errpkt((CHAR *)"QUIT disabled");
  1295.         RESUME;
  1296.     } else {
  1297. #ifdef CKSYSLOG
  1298.         if (ckxsyslog >= SYSLG_PR && ckxlogging)
  1299.           cksyslog(SYSLG_PR, 1, "server", "Interrupted", NULL);
  1300. #endif /* CKSYSLOG */
  1301.         success = 0;
  1302.         xitsta |= (what & W_KERMIT);
  1303.         QUIT;
  1304.     }
  1305.     } else {                /* Shouldn't happen */
  1306.     debug(F100,"SERVER (generic) GOT UNEXPECTED 'q'","",0);
  1307.     QUIT;
  1308.     }
  1309. #endif /* NOSERVER */
  1310. }
  1311.  
  1312. <generic>. {                /* Anything else in this state... */
  1313. #ifndef NOSERVER
  1314.     errpkt((CHAR *)"Unimplemented REMOTE command"); /* Complain */
  1315.     RESUME;                /* and return to server command wait */
  1316. #endif /* NOSERVER */
  1317. }
  1318.  
  1319. <rgen>q {                /* Sent BYE and connection broken */
  1320.     if (bye_active && ttchk() < 0) {
  1321.     msleep(500);
  1322.     bye_active = 0;
  1323.     ttclos(0);            /* Close our end of the connection */
  1324.     clsof(0);
  1325.     return(success = 1);
  1326.     } else {                /* Other generic command */
  1327.     return(success = 0);        /* or connection not broken */
  1328.     }
  1329. }
  1330.  
  1331. <rgen>Y {                /* Short-Form reply */
  1332.     rc = rcv_shortreply();
  1333.     debug(F101,"<rgen>Y rcv_shortreply","",rc);
  1334.     if (rc > -1) return(rc);
  1335. }
  1336.  
  1337. <rgen,rfile>F {                /* File header */
  1338.     /* char *n2; */
  1339.     extern int rsn;
  1340.     debug(F101,"<rfile>F winlo 1","",winlo);
  1341.     xflg = 0;                /* Not screen data */
  1342.     if (!czseen)
  1343.       cancel = 0;            /* Reset cancellation counter */
  1344. #ifdef CALIBRATE
  1345.     if (dest == DEST_N)
  1346.       calibrate = 1;
  1347. #endif /* CALIBRATE */
  1348.     if (!rcvfil(filnam)) {        /* Figure out local filename */
  1349.     errpkt((CHAR *)rf_err);        /* Trouble */
  1350.     RESUME;
  1351.     } else {                /* Real file, OK to receive */
  1352.     char * fnp;
  1353.     debug(F111,"<rfile>F winlo 2",fspec,winlo);
  1354.     if (filcnt == 1)        /* rcvfil set this to 1 for 1st file */
  1355.       crc16 = 0L;            /* Clear file CRC */
  1356.     fnp = fspec;            /* This is the full path */
  1357.     if (server && !ENABLED(en_cwd) || /* if DISABLE CD */
  1358.         !fackpath              /* or F-ACK-PATH OFF */
  1359.         ) {
  1360.         zstrip(fspec,&fnp);        /* don't send back full path */
  1361.     }
  1362.     encstr((CHAR *)fnp);
  1363.     if (fackbug)
  1364.       ack();
  1365.     else
  1366.       ack1(data);            /* Send it back in ACK */
  1367.     initattr(&iattr);        /* Clear file attribute structure */
  1368.     streamon();
  1369.     if (window(wslotn) < 0) {    /* Allocate negotiated window slots */
  1370.         errpkt((CHAR *)"Can't open window");
  1371.         RESUME;
  1372.     }
  1373. #ifdef IKSDB
  1374.     if (ikdbopen) slotstate(what,
  1375.                   server ? "SERVER" : "",
  1376.                   "RECEIVE",
  1377.                   fspec
  1378.                   );
  1379. #endif /* IKSDB */
  1380.     BEGIN rattr;            /* Now expect Attribute packets */
  1381.     }
  1382. }
  1383.  
  1384. <rgen,rfile>X {                /* X-packet instead of file header */
  1385.     xflg = 1;                /* Screen data */
  1386.     if (!czseen)
  1387.       cancel = 0;            /* Reset cancellation counter */
  1388.     ack();                /* Acknowledge the X-packet */
  1389.     initattr(&iattr);            /* Initialize attribute structure */
  1390.     streamon();
  1391.     if (window(wslotn) < 0) {        /* allocate negotiated window slots */
  1392.     errpkt((CHAR *)"Can't open window");
  1393.     RESUME;
  1394.     }
  1395. #ifndef NOSPL
  1396.     if (query) {            /* If this is the response to */
  1397.     qbufp = querybuf;        /* a query that we sent, initialize */
  1398.     qbufn = 0;            /* the response buffer */
  1399.     querybuf[0] = NUL;
  1400.     }
  1401. #endif /* NOSPL */
  1402.     what = W_REMO;            /* we're doing a REMOTE command */
  1403. #ifdef IKSDB
  1404.     if (ikdbopen) slotstate(what,
  1405.               server ? "SERVER" : "",
  1406.               "RECEIVE",
  1407.               fspec
  1408.               );
  1409. #endif /* IKSDB */
  1410.     BEGIN rattr;            /* Expect Attribute packets */
  1411. }
  1412.  
  1413. <rattr>A {                /* Attribute packet */
  1414.     if (gattr(rdatap,&iattr) == 0) {    /* Read into attribute structure */
  1415. #ifdef CK_RESEND
  1416.     ack1((CHAR *)iattr.reply.val);    /* Reply with data */
  1417. #else
  1418.     ack();                /* If OK, acknowledge */
  1419. #endif /* CK_RESEND */
  1420.     } else {                /* Otherwise */
  1421.     extern long fsize;
  1422.     char *r;
  1423.     r = getreason(iattr.reply.val);
  1424.     ack1((CHAR *)iattr.reply.val);    /* refuse to accept the file */
  1425.     xxscreen(SCR_ST,ST_REFU,0L,r);    /* reason */
  1426. #ifdef TLOG
  1427.     if (tralog && !tlogfmt)
  1428.       doxlog(what,filnam,fsize,binary,1,r);
  1429. #endif /* TLOG */
  1430.     }
  1431. }
  1432.  
  1433. <rattr>D {                /* First data packet */
  1434.     debug(F100,"<rattr> D firstdata","",0);
  1435.     rc = rcv_firstdata();
  1436.     debug(F101,"rcv_firstdata rc","",rc);
  1437.     if (rc > -1) return(rc);        /* (see below) */
  1438. }
  1439.  
  1440. <rfile>B {                /* EOT, no more files */
  1441.     ack();                /* Acknowledge the B packet */
  1442.     reot();                /* Do EOT things */
  1443. #ifdef CK_TMPDIR
  1444. /* If we were cd'd temporarily to another device or directory ... */
  1445.     if (f_tmpdir) {
  1446.     int x;
  1447.     x = zchdir((char *) savdir);    /* ... restore previous directory */
  1448.     f_tmpdir = 0;            /* and remember we did it. */
  1449.     debug(F111,"ckcpro.w B tmpdir restoring",savdir,x);
  1450.     }
  1451. #endif /* CK_TMPDIR */
  1452.     RESUME;                /* and quit */
  1453. }
  1454.  
  1455. <rdpkt>D {                /* Got Data packet */
  1456.     debug(F101,"<rdpkt>D cxseen","",cxseen);
  1457.     debug(F101,"<rdpkt>D czseen","",czseen);
  1458.     if (cxseen || czseen || discard) {    /* If file or group interruption */
  1459.     CHAR * msg;
  1460.     msg = czseen ? (CHAR *)"Z" : (CHAR *)"X";
  1461. #ifdef STREAMING
  1462.     if (streaming) {        /* Need to cancel */
  1463.         debug(F111,"<rdpkt>D streaming cancel",msg,cancel);
  1464.         if (cancel++ == 0) {    /* Only do this once */
  1465.         ack1(msg);        /* Put "X" or "Z" in ACK */
  1466.         } else if (czseen) {
  1467.         errpkt((CHAR *)"User canceled");
  1468.         RESUME;
  1469.         } else {
  1470.         fastack();
  1471.         }
  1472.     } else
  1473. #endif /* STREAMING */
  1474.       ack1(msg);
  1475.     } else {                /* No interruption */
  1476.     int rc, qf;
  1477. #ifndef NOSPL
  1478.     qf = query;
  1479. #else
  1480.     qf = 0;
  1481. #endif /* NOSPL */
  1482. #ifdef CKTUNING
  1483.     rc = (binary && !parity) ?
  1484.       bdecode(rdatap,putfil):
  1485.         decode(rdatap, qf ? puttrm : putfil, 1);
  1486. #else
  1487.     rc = decode(rdatap, qf ? puttrm : putfil, 1);
  1488. #endif /* CKTUNING */
  1489.     if (rc < 0) {
  1490.         discard = (keep == 0 || (keep == SET_AUTO && binary != XYFT_T));
  1491.         errpkt((CHAR *)"Error writing data"); /* If failure, */
  1492.         RESUME;
  1493.     } else                /* Data written OK, send ACK */
  1494. #ifdef STREAMING
  1495.       if (streaming)
  1496.         fastack();
  1497.     else
  1498. #endif /* STREAMING */
  1499.       ack();
  1500.     }
  1501. }
  1502.  
  1503. <rattr>Z {                /* EOF immediately after A-Packet. */
  1504.     rf_err = "Can't create file";
  1505.     timint = s_timint;
  1506.     if (discard) {            /* Discarding a real file... */
  1507.     x = 1;
  1508.     } else if (xflg) {            /* If screen data */
  1509.     if (remfile) {            /* redirected to file */
  1510.         if (rempipe)        /* or pipe */
  1511.           x = openc(ZOFILE,remdest); /* Pipe: start command */
  1512.         else
  1513.           x = opena(remdest,&iattr); /* File: open with attributes */
  1514.     } else {            /* otherwise */
  1515.         x = opent(&iattr);        /* "open" the screen */
  1516.     }
  1517. #ifdef CALIBRATE
  1518.     } else if (calibrate) {        /* If calibration run */
  1519.     x = ckopenx(&iattr);        /* do this */
  1520. #endif /* CALIBRATE */
  1521.     } else {                /* otherwise */
  1522.     x = opena(filnam,&iattr);    /* open the file, with attributes */
  1523.     if (x == -17) {            /* REGET skipped because same size */
  1524.         discard = 1;
  1525.         rejection = 1;
  1526.     }
  1527.     }
  1528.     if (!x || reof(filnam, &iattr) < 0) { /* Close output file */
  1529.     errpkt((CHAR *) rf_err);    /* If problem, send error msg */
  1530.     RESUME;                /* and quit */
  1531.     } else {                /* otherwise */
  1532.     if (x == -17)
  1533.       xxscreen(SCR_ST,ST_SKIP,SKP_RES,"");
  1534.     ack();                /* acknowledge the EOF packet */
  1535.     BEGIN rfile;            /* and await another file */
  1536.     }
  1537. }
  1538.  
  1539. <rdpkt>q {                  /* Ctrl-C or connection loss. */
  1540.     timint = s_timint;
  1541.     window(1);                /* Set window size back to 1... */
  1542.     cxseen = 1;
  1543.     x = clsof(1);            /* Close file */
  1544.     return(success = 0);        /* Failed */
  1545. }
  1546.  
  1547. <rdpkt>Z {                /* End Of File (EOF) Packet */
  1548. /*  wslots = 1;    */            /* (don't set) Window size back to 1 */
  1549. #ifndef COHERENT /* Coherent compiler blows up on this switch() statement. */
  1550.     x = reof(filnam, &iattr);        /* Handle the EOF packet */
  1551.     switch (x) {            /* reof() sets the success flag */
  1552.       case -5:                /* Handle problems */
  1553.     errpkt((CHAR *)"RENAME failed"); /* Fatal */
  1554.     RESUME;
  1555.     break;
  1556.       case -4:
  1557.     errpkt((CHAR *)"MOVE failed");    /* Fatal */
  1558.     RESUME;
  1559.     break;
  1560.       case -3:                /* If problem, send error msg */
  1561.     errpkt((CHAR *)"Can't print file"); /* Fatal */
  1562.     RESUME;
  1563.     break;
  1564.       case -2:
  1565.     errpkt((CHAR *)"Can't mail file"); /* Fatal */
  1566.     RESUME;
  1567.     break;
  1568.       case 2:                /* Not fatal */
  1569.       case 3:
  1570.     xxscreen(SCR_EM,0,0L,"Receiver can't delete temp file");
  1571.     RESUME;
  1572.     break;
  1573.       default:
  1574.     if (x < 0) {            /* Fatal */
  1575.         errpkt((CHAR *)"Can't close file");
  1576.         RESUME;
  1577.     } else {            /* Success */
  1578. #ifndef NOSPL
  1579.         if (query)            /* Query reponses generally */
  1580.           conoll("");        /* don't have line terminators */
  1581. #endif /* NOSPL */
  1582.         if (czseen) {        /* Batch canceled? */
  1583.         if (cancel++ == 0) {    /* If we haven't tried this yet */
  1584.             ack1((CHAR *)"Z");    /* Try it once */
  1585.         } else {        /* Otherwise */
  1586.             errpkt((CHAR *)"User canceled"); /* quite with Error */
  1587.             RESUME;
  1588.         }
  1589.         } else
  1590.           ack();            /* Acknowledge the EOF packet */
  1591.         BEGIN rfile;        /* and await another file */
  1592.     }
  1593.     }
  1594. #else
  1595.     if (reof(filnam, &iattr) < 0) {    /* Close the file */
  1596.     errpkt((CHAR *)"Error at end of file");
  1597.     RESUME;
  1598.     } else {                /* reof() sets success flag */
  1599.     ack();
  1600.     BEGIN rfile;
  1601.     }
  1602. #endif /* COHERENT */
  1603. }
  1604.  
  1605. <ssinit>Y {                /* ACK for Send-Init */
  1606.     spar(rdatap);            /* set parameters from it */
  1607.     cancel = 0;
  1608.     bctu = bctr;            /* switch to agreed-upon block check */
  1609.     bctl = (bctu == 4) ? 2 : bctu;    /* Set block-check length */
  1610. #ifdef CK_RESEND
  1611.     if ((sendmode == SM_RESEND) && (!atcapu || !rscapu)) { /* RESEND */
  1612.     errpkt((CHAR *) "RESEND capabilities not negotiated");
  1613.     RESUME;
  1614.     } else {
  1615. #endif /* CK_RESEND */
  1616.     what = W_SEND;            /* Remember we're sending */
  1617.     lastxfer = W_SEND;
  1618.     x = sfile(xflg);        /* Send X or F header packet */
  1619.     cancel = 0;            /* Reset cancellation counter */
  1620.     if (x) {            /* If the packet was sent OK */
  1621.         if (!xflg && filcnt == 1)    /* and it's a real file */
  1622.           crc16 = 0L;        /* Clear the file CRC */
  1623.         resetc();            /* reset per-transaction counters */
  1624.         rtimer();            /* reset timers */
  1625. #ifdef GFTIMER
  1626.         rftimer();
  1627. #endif /* GFTIMER */
  1628.         streamon();            /* turn on streaming */
  1629. #ifdef IKSDB
  1630.         if (ikdbopen) slotstate(what,
  1631.                   (server ? "SERVER" : ""),
  1632.                   "SEND",
  1633.                   filnam
  1634.                   );
  1635. #endif /* IKSDB */
  1636.         BEGIN ssfile;        /* and switch to receive-file state */
  1637.     } else {            /* otherwise send error msg & quit */
  1638.         s = xflg ? "Can't execute command" : (char *)epktmsg;
  1639.         if (!*s) s = "Can't open file";
  1640.         errpkt((CHAR *)s);
  1641.         RESUME;
  1642.     }
  1643. #ifdef CK_RESEND
  1644.     }
  1645. #endif /* CK_RESEND */
  1646. }
  1647.  
  1648. /*
  1649.   These states are necessary to handle the case where we get a server command
  1650.   packet (R, G, or C) reply with an S packet, but the client retransmits the
  1651.   command packet.  The input() function doesn't catch this because the packet
  1652.   number is still zero.
  1653. */
  1654. <ssinit>R {                /* R packet was retransmitted. */
  1655.     xsinit();                /* Resend packet 0 */
  1656. }
  1657.  
  1658. <ssinit>G {                /* Same deal if G packet comes again */
  1659.     xsinit();
  1660. }
  1661.  
  1662. /* should probably add cases for O, W, V, H, J, ... */
  1663.  
  1664. <ssinit>C {                /* Same deal if C packet comes again */
  1665.     xsinit();
  1666. }
  1667.  
  1668. <ssfile>Y {                /* ACK for F or X packet */
  1669.     srvptr = srvcmd;            /* Point to string buffer */
  1670.     decode(rdatap,putsrv,0);        /* Decode data field, if any */
  1671.     putsrv(NUL);            /* Terminate with null */
  1672.     ffc = 0L;                /* Reset file byte counter */
  1673.     debug(F101,"<ssfile>Y cxseen","",cxseen);
  1674.     if (*srvcmd) {            /* If remote name was recorded */
  1675.         if (sendmode != SM_RESEND) {
  1676.         extern char * srfspec;
  1677.         if (fdispla == XYFD_C || fdispla == XYFD_S)
  1678.           xxscreen(SCR_AN,0,0L,(char *)srvcmd);
  1679.         tlog(F110," remote name:",(char *) srvcmd,0L);
  1680.         makestr(&srfspec,(char *)srvcmd);
  1681.         }
  1682.     }
  1683.     if (cxseen||czseen) {        /* Interrupted? */
  1684.     debug(F101,"<ssfile>Y canceling","",0);
  1685.     x = clsif();            /* Close input file */
  1686.     sxeof(1);            /* Send EOF(D) */
  1687.     BEGIN sseof;            /* and switch to EOF state. */
  1688.     } else if (atcapu) {        /* If attributes are to be used */
  1689.     if (sattr(xflg | stdinf, 1) < 0) { /* send them */
  1690.         errpkt((CHAR *)"Can't send attributes"); /* if problem, say so */
  1691.         RESUME;            /* and quit */
  1692.     } else BEGIN ssattr;        /* if ok, switch to attribute state */
  1693.     } else {                /* Attributes not negotiated */
  1694.     if (window(wslotn) < 0) {    /* Open window */
  1695.         errpkt((CHAR *)"Can't open window");
  1696.         RESUME;
  1697.     } else if ((x = sdata()) == -2) { /* Send first data packet data */
  1698.         window(1);            /* Connection lost, reset window */
  1699.         x = clsif();        /* Close input file */
  1700.         return(success = 0);    /* Return failure */
  1701.     } else if (x == -9) {        /* User interrupted */
  1702.         errpkt((CHAR *)"User cancelled"); /* Send Error packet */
  1703.         window(1);            /* Set window size back to 1... */
  1704.         timint = s_timint;        /* Restore timeout */
  1705.         return(success = 0);    /* Failed */
  1706.     } else if (x < 0) {        /* EOF (empty file) or interrupted */
  1707.         window(1);            /* put window size back to 1, */
  1708.         debug(F101,"<ssfile>Y cxseen","",cxseen);
  1709.         x = clsif();        /* If not ok, close input file, */
  1710.         if (x < 0)            /* treating failure as interruption */
  1711.           cxseen = 1;        /* Send EOF packet */
  1712.         seof(cxseen||czseen);
  1713.         BEGIN sseof;        /* and switch to EOF state. */
  1714.     } else {            /* First data sent OK */
  1715.         BEGIN ssdata;        /* All ok, switch to send-data state */
  1716.     }
  1717.     }
  1718. }
  1719.  
  1720. <ssattr>Y {                /* Got ACK to A packet */
  1721.     ffc = 0L;                /* Reset file byte counter */
  1722.     debug(F101,"<ssattr>Y cxseen","",cxseen);
  1723.     if (cxseen||czseen) {        /* Interrupted? */
  1724.     debug(F101,"<sattr>Y canceling","",0);
  1725.     x = clsif();            /* Close input file */
  1726.     sxeof(1);            /* Send EOF(D) */
  1727.     BEGIN sseof;            /* and switch to EOF state. */
  1728.     } else if (rsattr(rdatap) < 0) {    /* Was the file refused? */
  1729.     discard = 1;            /* Set the discard flag */
  1730.     clsif();            /* Close the file */
  1731.     sxeof(1);            /* send EOF with "discard" code */
  1732.     BEGIN sseof;            /* switch to send-EOF state */
  1733.     } else if ((x = sattr(xflg | stdinf, 0)) < 0) { /* Send more? */
  1734.     errpkt((CHAR *)"Can't send attributes"); /* Trouble... */
  1735.     RESUME;
  1736.     } else if (x == 0) {        /* No more to send so now the data */
  1737.     if (window(wslotn) < 0) {    /* Allocate negotiated window slots */
  1738.         errpkt((CHAR *)"Can't open window");
  1739.         RESUME;
  1740.     }
  1741.     if ((x = sdata()) == -2) {    /* File accepted, send first data */
  1742.         window(1);            /* Connection broken */
  1743.         x = clsif();        /* Close file */
  1744.         return(success = 0);    /* Return failure */
  1745.     } else if (x == -9) {        /* User interrupted */
  1746.         errpkt((CHAR *)"User cancelled"); /* Send Error packet */
  1747.         window(1);            /* Set window size back to 1... */
  1748.         timint = s_timint;        /* Restore timeout */
  1749.         return(success = 0);    /* Failed */
  1750.     } else if (x < 0) {        /* If data was not sent */
  1751.         window(1);            /* put window size back to 1, */
  1752.         debug(F101,"<ssattr>Y cxseen","",cxseen);
  1753.         if (clsif() < 0)        /* Close input file */
  1754.           cxseen = 1;        /* Send EOF packet */
  1755.         seof(cxseen||czseen);
  1756.         BEGIN sseof;        /* and switch to EOF state. */
  1757.     } else {
  1758.         BEGIN ssdata;        /* All ok, switch to send-data state */
  1759.     }
  1760.     }
  1761. }
  1762.  
  1763. <ssdata>q {                  /* Ctrl-C or connection loss. */
  1764.     window(1);                /* Set window size back to 1... */
  1765.     cxseen = 1;                /* To indicate interruption */
  1766.     x = clsif();            /* Close file */
  1767.     return(success = 0);        /* Failed */
  1768. }
  1769.  
  1770. <ssdata>Y {                /* Got ACK to Data packet */
  1771.     canned(rdatap);            /* Check if file transfer cancelled */
  1772.     debug(F111,"<ssdata>Y cxseen",rdatap,cxseen);
  1773.     debug(F111,"<ssdata>Y czseen",rdatap,czseen);
  1774.     if ((x = sdata()) == -2) {        /* Try to send next data */
  1775.     window(1);            /* Connection lost, reset window */
  1776.     x = clsif();            /* Close file */
  1777.     return(success = 0);        /* Failed */
  1778.     } else if (x == -9) {        /* User interrupted */
  1779.     errpkt((CHAR *)"User cancelled"); /* Send Error packet */
  1780.     window(1);            /* Set window size back to 1... */
  1781.     timint = s_timint;        /* Restore original timeout */
  1782.     return(success = 0);        /* Failed */
  1783.     } else if (x < 0) {            /* EOF - finished sending data */
  1784.     debug(F101,"<ssdata>Y cxseen","",cxseen);
  1785.     window(1);            /* Set window size back to 1... */
  1786.     if (clsif() < 0)        /* Close input file */
  1787.       cxseen = 1;            /* Send EOF packet */
  1788.     debug(F101,"<ssdata>Y CALLING SEOF()","",cxseen);
  1789.     seof(cxseen||czseen);
  1790.     BEGIN sseof;            /* and enter send-eof state */
  1791.     }
  1792.     /* NOTE: If x == 0 it means we're draining: see sdata()! */
  1793. }
  1794.  
  1795. <sseof>Y {                /* Got ACK to EOF */
  1796.     int g;
  1797.     canned(rdatap);            /* Check if file transfer cancelled */
  1798.     debug(F111,"<sseof>Y cxseen",rdatap,cxseen);
  1799.     debug(F111,"<sseof>Y czseen",rdatap,czseen);
  1800.     success = (cxseen == 0 && czseen == 0); /* Transfer status... */
  1801.     debug(F101,"<sseof>Y success","",success);
  1802.     if (success && rejection > 0)        /* If rejected, succeed if */
  1803.       if (rejection != '#' &&            /* reason was date */
  1804.       rejection != 1 && rejection != '?') /* or name; */
  1805.     success = 0;                /* fail otherwise. */
  1806.     cxseen = 0;                /* This goes back to zero. */
  1807.     if (success) {            /* Only if transfer succeeded... */
  1808.     xxscreen(SCR_ST,ST_OK,0L,"");
  1809.     if (moving) {            /* If MOVE'ing */
  1810.         x = zdelet(filnam);        /* Try to delete the source file */
  1811. #ifdef TLOG
  1812.         if (tralog) {
  1813.         if (x > -1) {
  1814.             tlog(F110," deleted",filnam,0);
  1815.         } else {
  1816.             tlog(F110," delete failed:",ck_errstr(),0);
  1817.         }
  1818.         }
  1819. #endif /* TLOG */
  1820.     } else if (snd_move) {        /* Or move it */
  1821.         int x;
  1822.         x = zrename(filnam,snd_move);
  1823. #ifdef TLOG
  1824.         if (tralog) {
  1825.         if (x > -1) {
  1826.             tlog(F110," moved to ",snd_move,0);
  1827.         } else {
  1828.             tlog(F110," move failed:",ck_errstr(),0);
  1829.         }
  1830.         }
  1831. #endif /* TLOG */
  1832.     } else if (snd_rename) {    /* Or rename it */
  1833.         char *s = snd_rename;    /* Renaming string */
  1834. #ifndef NOSPL
  1835.         int y;            /* Pass it thru the evaluator */
  1836.         extern int cmd_quoting;    /* for \v(filename) */
  1837.         if (cmd_quoting) {        /* But only if cmd_quoting is on */
  1838.         y = MAXRP;
  1839.         s = (char *)srvcmd;
  1840.         zzstring(snd_rename,&s,&y);
  1841.         s = (char *)srvcmd;
  1842.         }
  1843. #endif /* NOSPL */
  1844.         if (s) if (*s) {
  1845.         int x;
  1846.         x = zrename(filnam,s);
  1847. #ifdef TLOG
  1848.         if (tralog) {
  1849.         if (x > -1) {
  1850.             tlog(F110," renamed to",s,0);
  1851.         } else {
  1852.             tlog(F110," rename failed:",ck_errstr(),0);
  1853.         }
  1854.         }
  1855. #endif /* TLOG */
  1856. #ifdef COMMENT
  1857.         *s = NUL;
  1858. #endif /* COMMENT */
  1859.         }
  1860.     }
  1861.     }
  1862.     if (czseen) {            /* Check group interruption flag */
  1863.     g = 0;                /* No more files if interrupted */
  1864.     } else {                /* Otherwise... */
  1865. #ifdef COMMENT
  1866.     /* This code makes any open error fatal to a file-group transfer. */
  1867.     g = gnfile();
  1868.     debug(F111,"<sseof>Y gnfile",filnam,g);
  1869.     if (g > 0) {            /* Any more files to send? */
  1870.         if (sfile(xflg))        /* Yes, try to send next file header */
  1871.           BEGIN ssfile;        /* if ok, enter send-file state */
  1872.         else {            /* otherwise */
  1873.         s = xflg ? "Can't execute command" : (char *)epktmsg;
  1874.         if (!*s) s = "Can't open file";
  1875.         errpkt((CHAR *)s);    /* send error message */
  1876.         RESUME;            /* and quit */
  1877.         }
  1878.     } else {            /* No next file */
  1879.         tsecs = gtimer();        /* get statistics timers */
  1880. #ifdef GFTIMER
  1881.         fptsecs = gftimer();
  1882. #endif /* GFTIMER */
  1883.         seot();            /* send EOT packet */
  1884.         BEGIN sseot;        /* enter send-eot state */
  1885.     }
  1886. #else  /* COMMENT */
  1887.     while (1) {            /* Keep trying... */
  1888.         g = gnfile();        /* Get next file */
  1889.         debug(F111,"<sseof>Y gnfile",filnam,g);
  1890.         if (g == 0 && gnferror == 0) /* No more, stop trying */
  1891.           break;
  1892.         if (g > 0) {        /* Have one */
  1893.         if (sfile(xflg)) {    /* Try to open and send F packet */
  1894.             BEGIN ssfile;    /* If OK, enter send-file state */
  1895.             break;        /* and break out of loop. */
  1896.         }
  1897.         } /* Otherwise keep trying to get one we can send... */
  1898.     }
  1899.     }
  1900.     if (g == 0) {
  1901.     debug(F101,"<sseof>Y no more files","",czseen);
  1902.     tsecs = gtimer();        /* Get statistics timers */
  1903. #ifdef GFTIMER
  1904.     fptsecs = gftimer();
  1905. #endif /* GFTIMER */
  1906.     seot();                /* Send EOT packet */
  1907.     BEGIN sseot;            /* Enter send-eot state */
  1908.     }
  1909. #endif /* COMMENT */
  1910. }
  1911.  
  1912. <sseot>Y {                /* Got ACK to EOT */
  1913.     debug(F101,"sseot justone","",justone);
  1914.     RESUME;                /* All done, just quit */
  1915. }
  1916.  
  1917. E {                    /* Got Error packet, in any state */
  1918.     char *s = "";
  1919.     window(1);                /* Close window */
  1920.     timint = s_timint;            /* Restore original timeout */
  1921.     if (*epktmsg)            /* Message from Error packet */
  1922.       s = (char *)epktmsg;
  1923.     if (!*s) {                /* If not there then maybe here */
  1924.     s = (char *)rdatap;
  1925.     ckstrncpy((char *)epktmsg,(char *)rdatap,PKTMSGLEN);
  1926.     }
  1927.     if (!*s)                /* Hopefully we'll never see this. */
  1928.       s = "Unknown error";
  1929.     success = 0;            /* For IF SUCCESS/FAIL. */
  1930.     debug(F101,"ckcpro.w justone at E pkt","",justone);
  1931.  
  1932.     success = 0;            /* Transfer failed */
  1933.     xferstat = success;            /* Remember transfer status */
  1934.     if (!epktsent) {
  1935.     x = quiet; quiet = 1;        /* Close files silently, */
  1936.     epktrcvd = 1;            /* Prevent messages from clsof() */
  1937.     clsif();
  1938.     clsof(1);             /* discarding any output file. */
  1939.     ermsg(s);            /* Issue the message (calls screen). */
  1940.     quiet = x;            /* Restore quiet state */
  1941.     }
  1942.     tstats();                /* Get stats */
  1943. /*
  1944.   If we are executing commands from a command file or macro, let the command
  1945.   file or macro decide whether to exit, based on SET { TAKE, MACRO } ERROR.
  1946. */
  1947.     if (
  1948. #ifndef NOICP
  1949.     !xcmdsrc &&
  1950. #endif /* NOICP */
  1951.     backgrd && !server)
  1952.       fatal("Protocol error");
  1953.     xitsta |= (what & W_KERMIT);    /* Save this for doexit(). */
  1954. #ifdef CK_TMPDIR
  1955. /* If we were cd'd temporarily to another device or directory ... */
  1956.     if (f_tmpdir) {
  1957.     int x;
  1958.     x = zchdir((char *) savdir);    /* ... restore previous directory */
  1959.     f_tmpdir = 0;            /* and remember we did it. */
  1960.     debug(F111,"ckcpro.w E tmpdir restored",savdir,x);
  1961.     }
  1962. #endif /* CK_TMPDIR */
  1963. #ifdef IKSDB
  1964.     if (ikdbopen) slotstate(what,"ERROR", (char *)epktmsg, "");
  1965. #endif /* IKSDB */
  1966.     RESUME;
  1967. }
  1968.  
  1969. q { success = 0; QUIT; }        /* Ctrl-C or connection loss. */
  1970.  
  1971. . {                    /* Anything not accounted for above */
  1972.     errpkt((CHAR *)"Unexpected packet type"); /* Give error message */
  1973.     window(1);
  1974.     xitsta |= (what & W_KERMIT);    /* Save this for doexit(). */
  1975.     RESUME;                /* and quit */
  1976. }
  1977.  
  1978. %%
  1979.  
  1980. /*
  1981.   From here down to proto() are routines that were moved out of the state
  1982.   table switcher because the resulting switch() had become too large.
  1983.   To move the contents of a state-table case to a routine:
  1984.     1. Add a prototype to the list above the state table switcher.
  1985.     2. Make a routine with an appropriate name, returning int.
  1986.     3. Move the code into it.
  1987.     4. Put a call to the new routine in the former spot:
  1988.          rc = name_of_routine();
  1989.          if (rc > -1) return(rc);
  1990.     5. Add "return(-1);" after every RESUME, SERVE, or BEGIN macro and
  1991.        at the end if the code is open-ended.
  1992. */
  1993. static int
  1994. rcv_firstdata() {
  1995.     extern int dispos;
  1996.     debug(F101,"rcv_firstdata","",dispos);
  1997.  
  1998.     if (discard) {            /* if we're discarding the file */
  1999.     ack1((CHAR *)"X");        /* just ack the data like this. */
  2000.     cancel++;            /* and count it */
  2001.     BEGIN rdpkt;            /* and wait for more data packets. */
  2002.     return(-1);
  2003.     } else {                /* Not discarding. */
  2004.     rf_err = "Can't open file";
  2005.     if (xflg) {            /* If screen data */
  2006.         if (remfile) {        /* redirected to file */
  2007.         if (rempipe)        /* or pipe */
  2008.           x = openc(ZOFILE,remdest); /* Pipe: start command */
  2009.         else
  2010.           x = opena(remdest,&iattr); /* File: open with attributes */
  2011.         } else {            /* otherwise */
  2012.         x = opent(&iattr);    /* "open" the screen */
  2013.         }
  2014.     } else {            /* otherwise */
  2015. #ifdef CALIBRATE
  2016.         if (calibrate) {        /* If calibration run */
  2017.         x = ckopenx(&iattr);    /* open nothing */
  2018. #ifdef STREAMING
  2019.         if (streaming)        /* Streaming */
  2020.           fastack();        /* ACK without ACKing. */
  2021.         else
  2022. #endif /* STREAMING */
  2023.           ack();        /* Send real ACK */
  2024.         BEGIN rdpkt;        /* Proceed to next state */
  2025.         return(-1);
  2026.         } else
  2027. #endif /* CALIBRATE */
  2028. #ifdef UNIX
  2029. /*
  2030.   In UNIX we can pipe the file data into the mail program, which is to be
  2031.   preferred to writing it out to a temp file and then mailing it afterwards.
  2032.   This depends rather heavily on all UNIXes having a mail command that
  2033.   accepts '-s "subject"' on the command line.  MAILCMD (e.g. mail, Mail, mailx)
  2034.   is defined in ckufio.c.
  2035. */
  2036.         if (dispos == 'M') {    /* Mail... */
  2037.         char *s;
  2038.         char * tmp = NULL;
  2039.         int n = 0;
  2040.         extern char *MAILCMD;
  2041.         s = iattr.disp.val + 1;
  2042.         n = (int)strlen(MAILCMD) +    /* Mail command */
  2043.           (int)strlen(s) +          /* address */
  2044.           (int)strlen(ofilnam) + 32;  /* subject */
  2045.         if (tmp = (char *)malloc(n)) {
  2046.             ckmakxmsg(tmp,n,
  2047.                   MAILCMD," -s \"",ofilnam,"\" ",s,
  2048.                   NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  2049.             debug(F111,"rcv_firsdata mail",tmp,(int)strlen(tmp));
  2050.             x = openc(ZOFILE,(char *)tmp);
  2051.             free(tmp);
  2052.         } else
  2053.           x = 0;
  2054.         } else if (dispos == 'P') { /* Ditto for print */
  2055.         char * tmp = NULL;
  2056.         int n;
  2057.         extern char *PRINTCMD;
  2058.         n = (int)strlen(PRINTCMD) + (int)strlen(iattr.disp.val+1) + 4;
  2059.         if (tmp = (char *)malloc(n)) {
  2060.             sprintf(tmp,    /* safe (prechecked) */
  2061.                 "%s %s", PRINTCMD, iattr.disp.val + 1);
  2062.             x = openc(ZOFILE,(char *)tmp);
  2063.             free(tmp);
  2064.         } else
  2065.           x = 0;
  2066.         } else
  2067. #endif /* UNIX */
  2068.           x = opena(filnam,&iattr);    /* open the file, with attributes */
  2069.     }
  2070.     if (x) {            /* If file was opened ok */
  2071.         int rc, qf;
  2072. #ifndef NOSPL
  2073.         qf = query;
  2074. #else
  2075.         qf = 0;
  2076. #endif /* NOSPL */
  2077.  
  2078. #ifdef CKTUNING
  2079.         rc = (binary && !parity) ?
  2080.           bdecode(rdatap,putfil):
  2081.            decode(rdatap, qf ? puttrm : putfil, 1);
  2082. #else
  2083.         rc = decode(rdatap, qf ? puttrm : putfil, 1);
  2084. #endif /* CKTUNING */
  2085.         if (rc < 0) {
  2086.         errpkt((CHAR *)"Error writing data");
  2087.         RESUME;
  2088.         return(-1);
  2089.         }
  2090. #ifdef STREAMING
  2091.         if (streaming)        /* Streaming was negotiated */
  2092.           fastack();        /* ACK without ACKing. */
  2093.         else
  2094. #endif /* STREAMING */
  2095.           ack();            /* acknowledge it */
  2096.         BEGIN rdpkt;        /* and switch to receive-data state */
  2097.         return(-1);
  2098.     } else {            /* otherwise */
  2099.         errpkt((CHAR *) rf_err);    /* send error packet */
  2100.             RESUME;            /* and quit. */
  2101.         return(-1);
  2102.     }
  2103.     }
  2104. }
  2105.  
  2106. static int
  2107. rcv_shortreply() {
  2108. #ifdef PKTZEROHACK
  2109.     success = 0;
  2110.     debug(F111,"rcv_shortreply",rdatap,ipktlen);
  2111.     if (ipktack[0] && !strncmp(ipktack,(char *)rdatap,ipktlen)) {
  2112.     /* No it's the ACK to the I packet again */
  2113.     x = scmd(vcmd,(CHAR *)cmarg);    /* So send the REMOTE command again */
  2114.     /* Maybe this should be resend() */
  2115.     debug(F110,"IPKTZEROHACK",ipktack,x);
  2116.     if (x < 0) {
  2117.         errpkt((CHAR *)srimsg);
  2118.         RESUME;
  2119.         return(-1);
  2120.     }
  2121.     } else {
  2122.     ipktack[0] = NUL;
  2123. #endif /* PKTZEROHACK */
  2124.     urserver = 1;
  2125. #ifndef NOSERVER
  2126. #ifndef NOSPL
  2127.     if (query) {            /* If to query, */
  2128.         qbufp = querybuf;        /*  initialize query response buffer */
  2129.         qbufn = 0;
  2130.         querybuf[0] = NUL;
  2131.     }
  2132. #endif /* NOSPL */
  2133.     x = 1;
  2134.     if (remfile) {            /* Response redirected to file */
  2135.         rf_err = "Can't open file";
  2136.         if (rempipe)        /* or pipe */
  2137.           x =
  2138. #ifndef NOPUSH
  2139.         zxcmd(ZOFILE,remdest)    /* Pipe: Start command */
  2140. #else
  2141.         0
  2142. #endif /* NOPUSH */
  2143.         ;
  2144.         else
  2145.           x = opena(remdest,&iattr); /* File: Open with attributes */
  2146.         debug(F111,"rcv_shortreply remfile",remdest,x);
  2147.     } else {
  2148.         x = opent(&iattr);        /* "open" the screen */
  2149.     }
  2150.     if (x) {            /* If file was opened ok */
  2151.         if (decode(rdatap,
  2152. #ifndef NOSPL
  2153.                (query || !remfile) ? puttrm :
  2154. #else
  2155.                !remfile ? puttrm :
  2156. #endif /* NOSPL */
  2157.                zputfil, 1) < 0) { /* Note: zputfil, not putfil. */
  2158.         errpkt((CHAR *)"Error writing data");
  2159.         RESUME;
  2160.         return(-1);
  2161.         } else {
  2162.         if (rdatap)        /* If we had data */
  2163.           if (*rdatap)        /* add a line terminator */
  2164.             if (remfile) {    /* to file */
  2165.             zsoutl(ZOFILE,"");
  2166.             } else {        /* or to screen. */
  2167. #ifndef NOICP
  2168.             if (!query || !xcmdsrc)
  2169. #endif /* NOICP */
  2170.               conoll("");
  2171.             }
  2172.         if (bye_active && network) { /* I sent BYE or REMOTE LOGOUT */
  2173.             msleep(500);    /* command and got the ACK... */
  2174.             bye_active = 0;
  2175.             ttclos(0);
  2176.         }
  2177.         clsof(0);
  2178.         if (!epktsent && !epktrcvd) /* If no error packet... */
  2179.           success = 1;        /* success. */
  2180.         RESUME;
  2181.         return(-1);
  2182.         }
  2183.     } else {            /* File not opened OK */
  2184.         errpkt((CHAR *) rf_err);    /* send error message */
  2185.         RESUME;            /* and quit. */
  2186.         return(-1);
  2187.     }
  2188. #endif /* NOSERVER */
  2189. #ifdef PKTZEROHACK
  2190.     }
  2191. #endif /* PKTZEROHACK */
  2192.     debug(F101,"rcv_shortreply fallthru","",success);
  2193.     return(-1);
  2194. }
  2195.  
  2196.  
  2197. static int
  2198. srv_query() {
  2199. #ifndef NOSERVER
  2200. #ifndef NOSPL
  2201.     char c;
  2202. #ifdef CKSYSLOG
  2203.     if (ckxsyslog >= SYSLG_PR && ckxlogging)
  2204.       cksyslog(SYSLG_PR, 1, "server", "REMOTE QUERY", (char *)srvcmd);
  2205. #endif /* CKSYSLOG */
  2206. #ifdef IKSDB
  2207.     if (ikdbopen) slotstate(what,"REMOTE QUERY", (char *)(srvcmd+2), "");
  2208. #endif /* IKSDB */
  2209.     c = *(srvcmd+2);            /* Q = Query, S = Set */
  2210.     if (c == 'Q') {            /* Query */
  2211.     if (!ENABLED(en_que)) { /* Security */
  2212.         errpkt((CHAR *)"REMOTE QUERY disabled");
  2213.         RESUME;
  2214.         return(-1);
  2215.     } else {            /* Query allowed */
  2216.         int n; char *p, *q;
  2217.         qbufp = querybuf;        /* Wipe out old stuff */
  2218.         qbufn = 0;
  2219.         querybuf[0] = NUL;
  2220.         p = (char *) srvcmd + 3;    /* Pointer for making wrapper */
  2221.         n = strlen((char *)srvcmd);    /* Position of end */
  2222.         c = *(srvcmd+4);        /* Which type of variable */
  2223.  
  2224.         if (*(srvcmd+6) == CMDQ) {    /* Starts with command quote? */
  2225.         p = (char *) srvcmd + 6; /* Take it literally */
  2226.         if (*p == CMDQ) p++;
  2227.         } else {            /* They played by the rules */
  2228.         if (c == 'K') {        /* Kermit variable */
  2229.             int k;
  2230.             k = (int) strlen(p);
  2231.             if (k > 0 && p[k-1] == ')') {
  2232.             p = (char *)(srvcmd + 4);
  2233.             *(srvcmd+4) = CMDQ;
  2234.             *(srvcmd+5) = 'f'; /* Function, so make it \f...() */
  2235.             } else {
  2236.             *(srvcmd+3) = CMDQ; /* Stuff wrapping into buffer */
  2237.             *(srvcmd+4) = 'v';  /* Variable, so make it \v(...) */
  2238.             *(srvcmd+5) = '(';  /* around variable name */
  2239.             *(srvcmd+n) = ')';
  2240.             *(srvcmd+n+1) = NUL;
  2241.             }
  2242.         } else {
  2243.             *(srvcmd+3) = CMDQ; /* Stuff wrapping into buffer */
  2244.             *(srvcmd+4) = 'v'; /*  Variable, so make it \v(...) */
  2245.             *(srvcmd+5) = '(';    /* around variable name */
  2246.             *(srvcmd+n) = ')';
  2247.             *(srvcmd+n+1) = NUL;
  2248.             if (c == 'S') {    /* System variable */
  2249.             *(srvcmd+4) = '$'; /*  so it's \$(...) */
  2250.             } else if (c == 'G') { /* Non-\ Global variable */
  2251.             *(srvcmd+4) = 'm'; /*  so wrap it in \m(...) */
  2252.             }
  2253.         }
  2254.         }                /* Now evaluate it */
  2255.         n = QBUFL;            /* Max length */
  2256.         q = querybuf;        /* Where to put it */
  2257.         if (zzstring(p,&q,&n) < 0) {
  2258.         errpkt((n > 0) ? (CHAR *)"Can't get value"
  2259.                        : (CHAR *)"Value too long"
  2260.                );
  2261.         RESUME;
  2262.         return(-1);
  2263.         } else {
  2264.         if (encstr((CHAR *)querybuf) > -1) { /* Encode it */
  2265.             ack1(data);        /* If it fits, send it back in ACK */
  2266.             success = 1;
  2267.             RESUME;
  2268.             return(-1);
  2269.         } else if (sndstring(querybuf)) { /* Long form response */
  2270.             BEGIN ssinit;
  2271.             return(-1);
  2272.         } else {        /* sndhlp() fails */
  2273.             errpkt((CHAR *)"Can't send value");
  2274.             RESUME;
  2275.             return(-1);
  2276.         }
  2277.         }
  2278.     }
  2279.     } else if (c == 'S') {        /* Set (assign) */
  2280.     if (!ENABLED(en_asg)) {        /* Security */
  2281.         errpkt((CHAR *)"REMOTE ASSIGN disabled");
  2282.         RESUME;
  2283.         return(-1);
  2284.     } else {            /* OK */
  2285.         int n;
  2286.         n = xunchar(*(srvcmd+3));    /* Length of name */
  2287.         n = 3 + n + 1;        /* Position of length of value */
  2288.         *(srvcmd+n) = NUL;        /* Don't need it */
  2289.         if (addmac((char *)(srvcmd+4),(char *)(srvcmd+n+1)) < 0)
  2290.           errpkt((CHAR *)"REMOTE ASSIGN failed");
  2291.         else {
  2292.         ack();
  2293.         success = 1;
  2294.         }
  2295.         RESUME;
  2296.         return(-1);
  2297.     }
  2298.     } else {
  2299.     errpkt((CHAR *)"Badly formed server command");
  2300.     RESUME;
  2301.     return(-1);
  2302.     }
  2303. #else
  2304.     errpkt((CHAR *)"Variable query/set not available");
  2305.     RESUME;
  2306.     return(-1);
  2307. #endif /* NOSPL */
  2308. #endif /* NOSERVER */
  2309. }
  2310.  
  2311. static int
  2312. srv_copy() {
  2313. #ifndef NOSERVER
  2314. #ifdef CKSYSLOG
  2315.     if (ckxsyslog >= SYSLG_PR && ckxlogging)
  2316.       cksyslog(SYSLG_PR, 1, "server", "REMOTE COPY", (char *)srvcmd);
  2317. #endif /* CKSYSLOG */
  2318. #ifdef ZCOPY
  2319.     if (!ENABLED(en_cpy)) {
  2320.     errpkt((CHAR *)"REMOTE COPY disabled");
  2321.     RESUME;
  2322.     return(-1);
  2323.     } else {
  2324.     char *str1, *str2, f1[256], f2[256];
  2325.     int  len1, len2;
  2326.         len1 = xunchar(srvcmd[1]);    /* Separate the parameters */
  2327.         len2 = xunchar(srvcmd[2+len1]);
  2328.         strncpy(f1,(char *)(srvcmd+2),len1);
  2329.         f1[len1] = NUL;
  2330.         strncpy(f2,(char *)(srvcmd+3+len1),len2);
  2331.         f2[len2] = NUL;
  2332. #ifdef IKSDB
  2333.     if (ikdbopen) slotstate(what,"REMOTE COPY", f1, f2);
  2334. #endif /* IKSDB */
  2335.     if (!ENABLED(en_cwd)) {        /* If CWD is disabled */
  2336.         zstrip(f1,&str1);        /* and they included a pathname, */
  2337.             zstrip(f2,&str2);
  2338.         if (strcmp(f1,str1) || strcmp(f2,str2)) { /* Refuse. */
  2339.         errpkt((CHAR *)"Access denied");
  2340.         RESUME;            /* Remember, this is not a goto! */
  2341.         return(-1);
  2342.         }
  2343.     }
  2344.     if (state == generic) {        /* It's OK to go ahead. */
  2345.             if (zcopy(f1,f2)) {        /* Try */
  2346.         errpkt((CHAR *)"Can't copy file"); /* give error message */
  2347.         } else {
  2348.         success = 1;
  2349.         ack();
  2350.         }
  2351.             RESUME;            /* wait for next server command */
  2352.         return(-1);
  2353.     }
  2354.     }
  2355.     return(-1);
  2356. #else /* no ZCOPY */
  2357.     errpkt((CHAR *)"REMOTE COPY not available"); /* give error message */
  2358.     RESUME;                /* wait for next server command */
  2359.     return(-1);
  2360. #endif /* ZCOPY */
  2361. #endif /* NOSERVER */
  2362. }
  2363.  
  2364. static int
  2365. srv_rename() {
  2366. #ifndef NOSERVER
  2367. #ifdef CKSYSLOG
  2368.     if (ckxsyslog >= SYSLG_PR && ckxlogging)
  2369.       cksyslog(SYSLG_PR, 1, "server", "REMOTE RENAME", (char *)srvcmd);
  2370. #endif /* CKSYSLOG */
  2371. #ifdef ZRENAME
  2372.     if (!ENABLED(en_ren)) {
  2373.     errpkt((CHAR *)"REMOTE RENAME disabled");
  2374.     RESUME;
  2375.     return(-1);
  2376.     } else {                /* RENAME is enabled */
  2377.     char *str1, *str2, f1[256], f2[256];
  2378.     int len1, len2;
  2379.     len1 = xunchar(srvcmd[1]);    /* Separate the parameters */
  2380.     len2 = xunchar(srvcmd[2+len1]);
  2381.     strncpy(f1,(char *)(srvcmd+2),len1);
  2382.     f1[len1] = NUL;
  2383.     strncpy(f2,(char *)(srvcmd+3+len1),len2);
  2384.     f2[len2] = NUL;
  2385.     len2 = xunchar(srvcmd[2+len1]);
  2386.     strncpy(f1,(char *)(srvcmd+2),len1);
  2387.     f1[len1] = NUL;
  2388.     strncpy(f2,(char *)(srvcmd+3+len1),len2);
  2389.     f2[len2] = NUL;
  2390. #ifdef IKSDB
  2391.     if (ikdbopen) slotstate(what,"REMOTE RENAME", f1, f2);
  2392. #endif /* IKSDB */
  2393.     if (!ENABLED(en_cwd)) {        /* If CWD is disabled */
  2394.         zstrip(f1,&str1);        /* and they included a pathname, */
  2395.         zstrip(f2,&str2);
  2396.         if ( strcmp(f1,str1) || strcmp(f2,str2) ) { /* refuse. */
  2397.         errpkt((CHAR *)"Access denied");
  2398.         RESUME;            /* Remember, this is not a goto! */
  2399.         return(-1);
  2400.         }
  2401.     }
  2402.     if (state == generic) {        /* It's OK to go ahead. */
  2403.         if (zrename(f1,f2)) {    /* Try */
  2404.         errpkt((CHAR *)"Can't rename file"); /* Give error msg */
  2405.         } else {
  2406.         success = 1;
  2407.         ack();
  2408.         }
  2409.         RESUME;            /* Wait for next server command */
  2410.         return(-1);
  2411.     }
  2412.     }
  2413.     return(-1);
  2414. #else /* no ZRENAME */
  2415.     /* Give error message */
  2416.     errpkt((CHAR *)"REMOTE RENAME not available");
  2417.     RESUME;                /* Wait for next server command */
  2418.     return(-1);
  2419. #endif /* ZRENAME */
  2420. #endif /* NOSERVER */
  2421. }
  2422.  
  2423. static int
  2424. srv_login() {
  2425. #ifndef NOSERVER
  2426.     char f1[LOGINLEN+1], f2[LOGINLEN+1], f3[LOGINLEN+1];
  2427.     CHAR *p;
  2428.     int len, i;
  2429.  
  2430.     debug(F101,"REMOTE LOGIN x_login","",x_login);
  2431.     debug(F101,"REMOTE LOGIN x_logged","",x_logged);
  2432.  
  2433.     f1[0] = NUL; f2[0] = NUL; f3[0] = NUL;
  2434.     len = 0;
  2435.     if (srvcmd[1])            /* First length field */
  2436.       len = xunchar(srvcmd[1]);        /* Separate the parameters */
  2437.  
  2438.     if (x_login) {            /* Login required */
  2439.     if (x_logged) {            /* And already logged in */
  2440.         if (len > 0) {        /* Logging in again */
  2441.         errpkt((CHAR *)"Already logged in.");
  2442.         } else {            /* Logging out */
  2443.         debug(F101,"REMOTE LOGOUT","",x_logged);
  2444. #ifdef CKSYSLOG
  2445.         if (ckxsyslog >= SYSLG_PR && ckxlogging)
  2446.           cksyslog(SYSLG_PR, 1, "server", "REMOTE LOGOUT", NULL);
  2447. #endif /* CKSYSLOG */
  2448. #ifdef IKSDB
  2449.         if (ikdbopen) slotstate(what,"REMOTE LOGOUT", "", "");
  2450. #endif /* IKSDB */
  2451.         tlog(F110,"Logged out",x_user,0);
  2452.         ack1((CHAR *)"Logged out");
  2453.         success = 1;
  2454.         msleep(500);
  2455. #ifdef CK_LOGIN
  2456.         x_logged = 0;
  2457. #ifdef IKSD
  2458.         if (inserver)
  2459.           ckxlogout();
  2460. #endif /* IKSD */
  2461. #endif /* CK_LOGIN */
  2462.         }
  2463.     } else {            /* Not logged in yet */
  2464.         debug(F101,"REMOTE LOGIN len","",len);
  2465.         if (len > 0) {        /* Have username */
  2466. #ifdef CKSYSLOG
  2467.         if (ckxsyslog >= SYSLG_PR && ckxlogging)
  2468.           cksyslog(SYSLG_PR, 1, "server", "REMOTE LOGIN", NULL);
  2469. #endif /* CKSYSLOG */
  2470.         if (len > LOGINLEN) {
  2471.             errpkt((CHAR *)"Username too long");
  2472.         }
  2473.         p = srvcmd + 2;        /* Point to it */
  2474.         for (i = 0; i < len; i++) /* Copy it */
  2475.           f1[i] = p[i];
  2476.         f1[len] = NUL;        /* Terminate it */
  2477.         p += len;        /* Point to next length field */
  2478.         if (*p) {        /* If we have one */
  2479.             len = xunchar(*p++); /* decode it */
  2480.             if (len > 0 && len <= LOGINLEN) {
  2481.             for (i = 0; i < len; i++) /* Same deal for password */
  2482.               f2[i] = p[i];
  2483.             f2[len] = NUL;
  2484.             p += len;    /* And account */
  2485.             if (*p) {
  2486.                 len = xunchar(*p++);
  2487.                 if (len > 0 && len <= LOGINLEN) {
  2488.                 for (i = 0; i < len; i++)
  2489.                   f3[i] = p[i];    /* Set but never used */
  2490.                 f3[len] = NUL; /* (because account not used) */
  2491.                 }
  2492.             }
  2493.             }
  2494.         }
  2495.         debug(F101,"REMOTE LOGIN 1","",x_logged);
  2496. #ifdef IKSD
  2497. #ifdef CK_LOGIN
  2498.         if (inserver) {        /* Log in to system for real */
  2499.             x_logged = ckxlogin((CHAR *)f1,(CHAR *)f2,NULL,0);
  2500.             debug(F101,"REMOTE LOGIN 2","",x_logged);
  2501.             if (x_logged) {    /* Count attempts */
  2502.             logtries = 0;
  2503.             justone = 1;
  2504.             } else {
  2505.             logtries++;
  2506.             sleep(logtries);
  2507.             }
  2508.         } else
  2509. #endif /* CK_LOGIN */
  2510. #endif /* IKSD */
  2511.           if (x_user && x_passwd) { /* User and password must match */
  2512.               if (!strcmp(x_user,f1)) /* SET SERVER LOGIN */
  2513.             if (!strcmp(x_passwd,f2))
  2514.               x_logged = 1;
  2515.               debug(F101,"REMOTE LOGIN 3","",x_logged);
  2516.           } else if (x_user) {    /* Only username given, no password */
  2517.               if (!strcmp(x_user,f1)) /* so only username must match */
  2518.             x_logged = 1;
  2519.               debug(F101,"REMOTE LOGIN 4","",x_logged);
  2520.           }
  2521. #ifdef CK_LOGIN 
  2522.                 else {
  2523.             x_logged = ckxlogin((CHAR *)f1,(CHAR *)f2,NULL,0);
  2524.             debug(F101,"REMOTE LOGIN 5","",x_logged);
  2525.                 }
  2526. #endif /* CK_LOGIN */
  2527.         if (x_logged) {        /* Logged in? */
  2528.             tlog(F110,"Logged in", x_user, 0);
  2529.             if (isguest)
  2530.               ack1((CHAR *)"Logged in as guest - restrictions apply");
  2531.             else
  2532.               ack1((CHAR *)"Logged in");
  2533.             success = 1;
  2534.         } else {
  2535.             tlog(F110,"Login failed", f1, 0);
  2536.             errpkt((CHAR *)"Access denied.");
  2537. #ifdef IKSD
  2538. #ifdef CK_LOGIN
  2539.             if (inserver && logtries > 2)
  2540.               ckxlogout();
  2541. #endif /* CK_LOGIN */
  2542. #endif /* IKSD */
  2543.         }
  2544.         } else {            /* LOGOUT */
  2545.         errpkt((CHAR *)"Logout ignored");
  2546.         }
  2547.     }
  2548.     } else {                /* Login not required */
  2549.     if (len > 0)
  2550.       errpkt((CHAR *)"Login ignored.");
  2551.     else
  2552.       errpkt((CHAR *)"Logout ignored.");
  2553.     }
  2554. #endif /* NOSERVER */
  2555.     RESUME;
  2556.     return(-1);
  2557. }
  2558.  
  2559. static int
  2560. srv_timeout() {
  2561.     /* K95 does this its own way */
  2562.     if (idletmo) {
  2563. #ifdef IKSD
  2564.         if (inserver) {
  2565.            printf("\r\nIKSD IDLE TIMEOUT: %d sec\r\n", srvidl);
  2566.            doexit(GOOD_EXIT,xitsta);
  2567.         }
  2568. #endif /* IKSD */
  2569.     idletmo = 0;
  2570.     printf("\r\nSERVER IDLE TIMEOUT: %d sec\r\n", srvidl);
  2571.     xitsta |= (what & W_KERMIT);
  2572.     QUIT;
  2573.     }
  2574. #ifndef NOSERVER
  2575.     else if (fatalio) {            /* Connection lost */
  2576. #ifdef CKSYSLOG
  2577.       if (ckxsyslog >= SYSLG_PR && ckxlogging)
  2578.         cksyslog(SYSLG_PR, 1, "server", "Connection lost", NULL);
  2579. #endif /* CKSYSLOG */
  2580. #ifdef IKSDB
  2581.       if (ikdbopen) slotstate(what,"SERVER DISCONNECT",(char *)srvcmd, "");
  2582. #endif /* IKSDB */
  2583.     xitsta |= what;
  2584.     QUIT;
  2585.     } else if (interrupted) {        /* Interrupted by hand */
  2586.     if (!ENABLED(en_fin)) {
  2587.         errpkt((CHAR *)"QUIT disabled");
  2588.         RESUME;
  2589.         return(-1);
  2590.     } else {
  2591.         if (what == W_SEND || what == W_RECV || what == W_REMO) {
  2592.         success = 0;
  2593. #ifdef CKSYSLOG
  2594.         if (ckxsyslog >= SYSLG_PR && ckxlogging)
  2595.           cksyslog(SYSLG_PR, 1, "server", "Interrupted", NULL);
  2596. #endif /* CKSYSLOG */
  2597.         } else if (what == W_NOTHING && filcnt == 0) {
  2598.         success = 1;
  2599.         } /* Otherwise leave success alone */
  2600.         xitsta |= (what & W_KERMIT);
  2601.         QUIT;
  2602.     }
  2603.     } else {                /* Shouldn't happen */
  2604.     debug(F100,"SERVER (top) GOT UNEXPECTED 'q'","",0);
  2605.     QUIT;
  2606.     }
  2607. #endif /* NOSERVER */
  2608. }
  2609.  
  2610. static int
  2611. rcv_s_pkt() {
  2612. #ifndef NOSERVER
  2613.     if (state == rgen)
  2614.       urserver = 1;
  2615.     if (/* state == serve && */ x_login && !x_logged) {
  2616.     errpkt((CHAR *)"Login required");
  2617.     SERVE;
  2618.     } else
  2619. #endif /* NOSERVER */
  2620.       if (state == serve && !ENABLED(en_sen)) { /* Not in server mode */
  2621.     errpkt((CHAR *)"SEND disabled"); /* when SEND is disabled. */
  2622.     RESUME;
  2623.     return(-1);
  2624.     } else {                /* OK to go ahead. */
  2625. #ifdef CK_TMPDIR
  2626.     if (dldir && !f_tmpdir) {    /* If they have a download directory */
  2627.         debug(F110,"receive download dir",dldir,0);
  2628.         if (s = zgtdir()) {        /* Get current directory */
  2629.         debug(F110,"receive current dir",s,0);
  2630.         if (zchdir(dldir)) {    /* Change to download directory */
  2631.             debug(F100,"receive zchdir ok","",0);
  2632.             ckstrncpy(savdir,s,TMPDIRLEN);
  2633.             f_tmpdir = 1;    /* Remember that we did this */
  2634.         } else
  2635.           debug(F100,"receive zchdir failed","",0);
  2636.         }
  2637.     }
  2638. #endif /* CK_TMPDIR */
  2639.     nakstate = 1;            /* Can send NAKs from here. */
  2640.     rinit(rdatap);            /* Set parameters */
  2641.     bctu = bctr;            /* Switch to agreed-upon block check */
  2642.     bctl = (bctu == 4) ? 2 : bctu;    /* Set block-check length */
  2643.     what = W_RECV;            /* Remember we're receiving */
  2644.     lastxfer = W_RECV;
  2645.     resetc();            /* Reset counters */
  2646.     rtimer();            /* Reset timer */
  2647. #ifdef GFTIMER
  2648.     rftimer();
  2649. #endif /* GFTIMER */
  2650.     streamon();
  2651.     BEGIN rfile;            /* Go into receive-file state */
  2652.     }
  2653.     return(-1);
  2654. }
  2655.  
  2656. /* END OF ROUTINES MOVED OUT OF STATE MACHINE */
  2657.  
  2658.  
  2659. /*  P R O T O  --  Protocol entry function  */
  2660.  
  2661. static int is_tn = 0;            /* It's a Telnet connection */
  2662.  
  2663. #ifdef CK_SPEED
  2664. int f_ctlp = 0;                /* Control-character prefix table */
  2665. #ifdef COMMENT
  2666. short s_ctlp[256];
  2667. #endif /* COMMENT */
  2668. #endif /* CK_SPEED */
  2669.  
  2670. /*
  2671.   This is simply a wrapper for the real protocol function just below,
  2672.   that saves any items that might be changed automatically by protocol
  2673.   negotiations and then restores them upon exit from protocol mode.
  2674. */
  2675. VOID
  2676. proto() {
  2677.     extern int b_save, f_save, c_save, ss_save, slostart, reliable, urclear;
  2678. #ifndef NOCSETS
  2679.     extern int fcharset, fcs_save, tcharset, tcs_save;
  2680. #endif /* NOCSETS */
  2681.  
  2682. #ifdef PIPESEND
  2683.     extern int pipesend;
  2684. #endif /* PIPESEND */
  2685. #ifndef NOLOCAL
  2686. #ifdef OS2
  2687.     extern int cursorena[], cursor_save, term_io;
  2688.     extern BYTE vmode;
  2689.     extern int display_demo;
  2690.     int term_io_save;
  2691. #endif /* OS2 */
  2692. #endif /* NOLOCAL */
  2693. #ifdef TNCODE
  2694.     int _u_bin=0, _me_bin = 0;
  2695. #ifdef IKS_OPTION
  2696.     int /* _u_start=0, */ _me_start = 0;
  2697. #endif /* IKS_OPTION */
  2698. #endif /* TNCODE */
  2699. #ifdef PATTERNS
  2700.     int pa_save;
  2701.     int i;
  2702. #endif /* PATTERNS */
  2703.     int scan_save;
  2704.  
  2705. #ifdef PATTERNS
  2706.     pa_save = patterns;
  2707. #endif /* PATTERNS */
  2708.     scan_save = filepeek;
  2709.  
  2710. #ifdef CK_LOGIN
  2711.     if (isguest) {            /* If user is anonymous */
  2712.     en_pri = 0;            /* disable printing */
  2713.     en_mai = 0;            /* and disable email */
  2714.     en_del = 0;            /* and file deletion */
  2715.     }
  2716. #endif /* CK_LOGIN */
  2717.  
  2718. #ifndef NOLOCAL
  2719. #ifdef OS2
  2720.     cursor_save = cursorena[vmode];
  2721.     cursorena[vmode] = 0;
  2722.     term_io_save = term_io;
  2723.     term_io = 0;
  2724. #endif /* OS2 */
  2725. #endif /* NOLOCAL */
  2726.     b_save = binary;            /* SET FILE TYPE */
  2727.     f_save = fncnv;            /* SET FILE NAMES */
  2728.     c_save = bctr;
  2729.     p_save = fnspath;
  2730.     r_save = recursive;
  2731.     s_timint = timint;
  2732.     ss_save = slostart;
  2733. #ifndef NOCSETS
  2734.     fcs_save = fcharset;
  2735.     tcs_save = tcharset;
  2736. #endif /* NOCSETS */
  2737.  
  2738. #ifdef COMMENT
  2739. /* Don't do this because then user can never find out what happened. */
  2740. #ifdef CK_SPEED
  2741.     for (i = 0; i < 256; i++)
  2742.       s_ctlp[i] = ctlp[i];
  2743.     f_ctlp = 1;
  2744. #endif /* CK_SPEED */
  2745. #endif /* COMMENT */
  2746.     if (reliable == SET_ON)
  2747.       slostart = 0;
  2748.     is_tn = (!local && sstelnet)
  2749. #ifdef TNCODE
  2750.       || (local && network && ttnproto == NP_TELNET)
  2751. #endif /* TNCODE */
  2752.     ;
  2753. #ifdef TNCODE
  2754.     if (is_tn) {
  2755.         if (tn_b_xfer && !(sstelnet || inserver)) {
  2756.         /* Save the current state of Telnet Binary */
  2757.         _u_bin = TELOPT_U(TELOPT_BINARY);
  2758.         _me_bin = TELOPT_ME(TELOPT_BINARY);
  2759.  
  2760.         /* If either direction is not Binary attempt to negotiate it */
  2761.         if (!_u_bin && TELOPT_U_MODE(TELOPT_BINARY) != TN_NG_RF) {
  2762.         tn_sopt(DO,TELOPT_BINARY);
  2763.         TELOPT_UNANSWERED_DO(TELOPT_BINARY) = 1;
  2764.         }
  2765.         if (!_me_bin && TELOPT_ME_MODE(TELOPT_BINARY) != TN_NG_RF) {
  2766.         tn_sopt(WILL,TELOPT_BINARY);
  2767.         TELOPT_UNANSWERED_WILL(TELOPT_BINARY) = 1;
  2768.         }
  2769.         if (!(_me_bin && _u_bin))
  2770.           tn_wait("proto set binary mode");
  2771.         }
  2772. #ifdef IKS_OPTION
  2773. #ifdef CK_XYZ
  2774.         if (protocol != PROTO_K) {    /* Non-Kermit protocol selected */
  2775.             if (TELOPT_U(TELOPT_KERMIT) &&
  2776.                 TELOPT_SB(TELOPT_KERMIT).kermit.u_start) {
  2777.                 iks_wait(KERMIT_REQ_STOP,0); /* Stop the other Server */
  2778.         /* _u_start = 1; */
  2779.             }
  2780.             if (TELOPT_ME(TELOPT_KERMIT) &&
  2781.                 TELOPT_SB(TELOPT_KERMIT).kermit.me_start) {
  2782.                 tn_siks(KERMIT_STOP);    /* I'm not servering */
  2783.          TELOPT_SB(TELOPT_KERMIT).kermit.me_start = 0;
  2784.         _me_start = 1;
  2785.             }
  2786.         } else
  2787. #endif /* CK_XYZ */
  2788.         if (sstate == 'x' || sstate == 'v') { /* Responding to a request */
  2789.             if (!inserver && TELOPT_U(TELOPT_KERMIT) &&
  2790.                 TELOPT_SB(TELOPT_KERMIT).kermit.u_start) {
  2791.                 iks_wait(KERMIT_REQ_STOP,0); /* Stop the other Server */
  2792.         /* _u_start = 1; */
  2793.             }
  2794.             if (TELOPT_ME(TELOPT_KERMIT) &&
  2795.                 !TELOPT_SB(TELOPT_KERMIT).kermit.me_start) {
  2796.                 tn_siks(KERMIT_START);    /* Send Kermit-Server Start */
  2797.          TELOPT_SB(TELOPT_KERMIT).kermit.me_start = 1;
  2798.             }
  2799.         } else {            /* Initiating a request */
  2800.             if (TELOPT_ME(TELOPT_KERMIT) &&
  2801.                 TELOPT_SB(TELOPT_KERMIT).kermit.me_start) {
  2802.                 tn_siks(KERMIT_STOP);    /* I'm not servering */
  2803.          TELOPT_SB(TELOPT_KERMIT).kermit.me_start = 0;
  2804.         _me_start = 1;
  2805.             }
  2806.             if (TELOPT_U(TELOPT_KERMIT) &&
  2807.             !TELOPT_SB(TELOPT_KERMIT).kermit.u_start) {
  2808.         /* Send Req-Server-Start */
  2809.                 if (!iks_wait(KERMIT_REQ_START,0)) {
  2810.                     if (sstate != 's') {
  2811.             success = 0;    /* Other Kermit refused to serve */
  2812.             if (local)
  2813.               printf("A Kermit Server is not available\r\n");
  2814.             debug(F110,"proto()",
  2815.                              "A Kermit Server is not available",0);
  2816.             tlog(F110,"IKS client/server failure",
  2817.                              "A Kermit Server is not available",0);
  2818.             goto xxprotox;
  2819.                     }
  2820.         }
  2821.             }
  2822.         }
  2823. #endif /* IKS_OPTION */
  2824. #ifdef CK_ENCRYPTION
  2825.         if (tn_no_encrypt_xfer && !(sstelnet || inserver)) {
  2826.             ck_tn_enc_stop();
  2827.         }
  2828. #endif /* CK_ENCRYPTION */
  2829.     }
  2830. #endif /* TNCODE */
  2831.  
  2832.     if (!xfrint) connoi();
  2833.     xxproto();                /* Call the real protocol function */
  2834.  
  2835. #ifdef IKS_OPTION
  2836.   xxprotox:
  2837. #endif /* IKS_OPTION */
  2838.     xferstat = success;            /* Remember transfer status */
  2839.     kactive = 0;
  2840.  
  2841. #ifdef TNCODE
  2842. #ifdef CK_ENCRYPTION
  2843.         if (tn_no_encrypt_xfer && !(sstelnet || inserver)) {
  2844.             ck_tn_enc_start();
  2845.         }
  2846. #endif /* CK_ENCRYPTION */
  2847. #ifdef IKS_OPTION
  2848.     if (TELOPT_ME(TELOPT_KERMIT) &&
  2849.         TELOPT_SB(TELOPT_KERMIT).kermit.me_start && !_me_start) {
  2850.         tn_siks(KERMIT_STOP);        /* Server is stopped */
  2851.      TELOPT_SB(TELOPT_KERMIT).kermit.me_start = 0;
  2852.     }
  2853. #endif /* IKS_OPTION */
  2854.     if (is_tn && tn_b_xfer && !(sstelnet || inserver)) {
  2855.         /* if we negotiated Binary mode try to reset it */
  2856.         if (!_u_bin) {
  2857.             /* Check to see if the state changed during the transfer */
  2858.         if (TELOPT_U(TELOPT_BINARY)) {
  2859.         tn_sopt(DONT,TELOPT_BINARY);
  2860.         TELOPT_UNANSWERED_DONT(TELOPT_BINARY) = 1;
  2861.         } else
  2862.           _u_bin = 1;        /* So we don't call tn_wait() */
  2863.         }
  2864.         if (!_me_bin) {
  2865.             /* Check to see if the state changed during the transfer */
  2866.         if (TELOPT_ME(TELOPT_BINARY)) {
  2867.         tn_sopt(WONT,TELOPT_BINARY);
  2868.         TELOPT_UNANSWERED_WONT(TELOPT_BINARY) = 1;
  2869.         } else
  2870.           _me_bin = 1;        /* So we don't call tn_wait() */
  2871.     }
  2872.     if (!(_me_bin && _u_bin))
  2873.       tn_wait("proto reset binary mode");
  2874.     }
  2875. #endif /* TNCODE */
  2876.  
  2877. #ifdef PATTERNS
  2878.     patterns = pa_save;
  2879. #endif /* PATTERNS */
  2880.     filepeek = scan_save;
  2881.  
  2882. #ifdef STREAMING
  2883.     streaming = 0;
  2884.     /* streamok = 0; */
  2885. #endif /* STREAMING */
  2886. #ifdef COMMENT
  2887. #ifdef CK_SPEED
  2888.     for (i = 0; i < 256; i++)
  2889.       ctlp[i] = s_ctlp[i];
  2890.     f_ctlp = 0;
  2891. #endif /* CK_SPEED */
  2892. #endif /* COMMENT */
  2893.     urclear = 0;
  2894.     if (!success) {
  2895.     xitsta |= (what & W_KERMIT);
  2896.     tlog(F110," failed:",(char *)epktmsg,0);
  2897.     }
  2898.     debug(F111,"proto xferstat",epktmsg,xferstat);
  2899.     slostart = ss_save;
  2900.     if (s_timint > -1) {        /* Because of REMOTE SET */
  2901.     timint = s_timint;
  2902.     s_timint = -1;
  2903.     }
  2904.     recursive = r_save;
  2905.     fnspath = p_save;
  2906.     if (c_save > -1) {            /* Because of REMOTE SET */
  2907.     bctr = c_save;
  2908.     c_save = -1;
  2909.     }
  2910.     fncnv   = f_save;
  2911.     binary  = b_save;
  2912. #ifdef PIPESEND
  2913.     pipesend = 0;                /* Next time might not be pipesend */
  2914. #endif /* PIPESEND */
  2915. #ifndef NOLOCAL
  2916. #ifdef OS2
  2917.     cursorena[vmode] = cursor_save;
  2918.     term_io = term_io_save;
  2919.     display_demo = 1;
  2920. #endif /* OS2 */
  2921. #endif /* NOLOCAL */
  2922. }
  2923.  
  2924. static VOID
  2925. xxproto() {
  2926.     int x;
  2927.     long lx;
  2928. #ifdef CK_XYZ
  2929. #ifdef XYZ_INTERNAL
  2930. _PROTOTYP( int pxyz, (int) );
  2931. #endif /* XYZ_INTERNAL */
  2932. #endif /* CK_XYZ */
  2933.  
  2934.     char xss[2];            /* String representation of sstate */
  2935.     xss[0] = sstate;
  2936.     xss[1] = NUL;
  2937.     s_timint = timint;
  2938.  
  2939.     debug(F101,"xxproto entry justone","",justone);
  2940.     success = 0;
  2941.  
  2942.     retrieve = 0;            /* Reset these ... */
  2943.     reget = 0;
  2944.     opkt = 0;
  2945.  
  2946.     if (local && ttchk() < 0) {        /* Giving BYE or FIN */
  2947.     if (bye_active) {        /* but there is no connection */
  2948.         ttclos(0);
  2949.         success = 1;
  2950.         return;
  2951.     }
  2952.     /* Ditto for any REMOTE command */
  2953.     if (sstate == 'g' && cmarg ) {
  2954.         if (*cmarg == 'L' || *cmarg == 'F' || *cmarg == 'X')
  2955.           success = 1;
  2956.         else
  2957.           printf("?No connection\r\n");
  2958.         return;
  2959.     }
  2960.     }
  2961.  
  2962. /* Set up the communication line for file transfer. */
  2963. /* NOTE: All of the xxscreen() calls prior to the wart() invocation */
  2964. /* could just as easily be printf's or, for that matter, hints. */
  2965.  
  2966.     if (local && (speed < 0L) && (network == 0)) {
  2967.     xxscreen(SCR_EM,0,0L,"Sorry, you must 'set speed' first");
  2968.     return;
  2969.     }
  2970.     x = -1;
  2971.     if (ttopen(ttname,&x,mdmtyp,cdtimo) < 0) {
  2972.     debug(F111,"failed: proto ttopen local",ttname,local);
  2973.     xxscreen(SCR_EM,0,0L,"Can't open line");
  2974.     return;
  2975.     }
  2976.     if (x > -1) local = x;
  2977.     debug(F111,"proto ttopen local",ttname,local);
  2978.  
  2979.     lx = (local && !network) ? speed : -1;
  2980. #ifdef NETCONN
  2981. #ifdef CK_SPEED
  2982.     if (is_tn) {
  2983.     ctlp[(unsigned)255] = ctlp[CR] = 1;
  2984.     if (parity == 'e' || parity == 'm') ctlp[127] = 1;
  2985.     if (flow == FLO_XONX) {        /* Also watch out for Xon/Xoff */
  2986.         ctlp[17] = ctlp[19] = 1;
  2987.         ctlp[17+128] = ctlp[19+128] = 1;
  2988.     }
  2989.     }
  2990. #endif /* CK_SPEED */
  2991. #endif /* NETCONN */
  2992.     if (ttpkt(lx,flow,parity) < 0) {    /* Put line in packet mode, */
  2993.     xxscreen(SCR_EM,0,0L,"Can't condition line");
  2994.     return;
  2995.     }
  2996.     if (local && !network && carrier != CAR_OFF) {
  2997.     int x;                /* Serial connection */
  2998.     x = ttgmdm();            /* with carrier checking */
  2999.     if (x > -1) {
  3000.         if (!(x & BM_DCD)) {
  3001.         debug(F101,"proto ttgmdm","",0);
  3002.         xxscreen(SCR_EM,0,0L,"Carrier required but not detected");
  3003.         return;
  3004.         }
  3005.     }
  3006.     }
  3007.     /* Send remote side's "receive" or "server" startup string, if any */
  3008.     if (local && ckindex((char *)xss,"srgcjhk",0,0,1)) {
  3009.     char *s = NULL;
  3010.         if (
  3011. #ifdef IKS_OPTION
  3012.         /* Don't send auto-blah string if we know other side is serving */
  3013.         !TELOPT_U(TELOPT_KERMIT) ||
  3014.         !TELOPT_SB(TELOPT_KERMIT).kermit.u_start
  3015. #else
  3016.         1
  3017. #endif /* IKS_OPTION */
  3018.         ) {
  3019.         if (sstate == 's') {    /* Sending file(s) */
  3020.         s = binary ? ptab[protocol].h_b_init : ptab[protocol].h_t_init;
  3021.         } else if (protocol == PROTO_K) { /* Command for server */
  3022.         s = ptab[protocol].h_x_init;
  3023.         }
  3024.     }
  3025. #ifdef CK_SPEED
  3026. #ifndef UNPREFIXZERO
  3027.     if (protocol == PROTO_K)    /* Because of C-strings... */
  3028.       ctlp[0] = 1;
  3029. #endif /* UNPREFIXZERO */
  3030. #endif /* CK_SPEED */
  3031.     if (s) if (*s) {        /* If we have a command to send... */
  3032.         char tmpbuf[356];
  3033.         int tmpbufsiz = 356;
  3034.         int stuff = -1, stuff2 = -1, len = 0;
  3035.         extern int tnlm;
  3036.         if (sstate == 's') {    /* Sending file(s) */
  3037. #ifdef CK_XYZ
  3038.         if (protocol == PROTO_X) {
  3039.             char * s2;
  3040.             s2 = cmarg2[0] ? cmarg2 : cmarg;
  3041.             if ((int)strlen(s) + (int)strlen(s2) + 4 < 356)
  3042.               sprintf(tmpbuf, s, s2);
  3043.             else
  3044.               tmpbuf[0] = NUL;
  3045.         } else {
  3046. #endif /* CK_XYZ */
  3047.             ckmakmsg(tmpbuf, 356, s, NULL, NULL, NULL);
  3048. #ifdef CK_XYZ
  3049.         }
  3050. #endif /* CK_XYZ */
  3051.         } else {            /* Command for server */
  3052.         ckstrncpy(tmpbuf,s,356);
  3053.         }
  3054.         ckstrncat(tmpbuf, "\015",sizeof(tmpbuf));
  3055.         if (tnlm)            /* TERMINAL NEWLINE ON */
  3056.           stuff = LF;        /* Stuff LF */
  3057. #ifdef TNCODE
  3058.         /* TELNET NEWLINE MODE */
  3059.         if (is_tn) {
  3060.         switch (TELOPT_ME(TELOPT_BINARY) ? tn_b_nlm : tn_nlm) {
  3061.           case TNL_CR:
  3062.             break;
  3063.           case TNL_CRNUL:
  3064.             break;
  3065.           case TNL_CRLF:
  3066.             stuff2 = stuff;
  3067.             stuff = LF;
  3068.             break;
  3069.         }
  3070.         }
  3071. #endif /* TNCODE */
  3072.  
  3073. #ifdef NETCONN
  3074. #ifdef TCPSOCKET
  3075. #ifdef RLOGCODE
  3076.         if (network && ttnproto == NP_RLOGIN) {
  3077.         switch (tn_b_nlm) { /* Always BINARY */
  3078.           case TNL_CR:
  3079.             break;
  3080.           case TNL_CRNUL:
  3081.             stuff2 = stuff;
  3082.             stuff  = NUL;
  3083.             break;
  3084.           case TNL_CRLF:
  3085.             stuff2 = stuff;
  3086.             stuff = LF;
  3087.             break;
  3088.         }
  3089.         }
  3090. #endif /* RLOGCODE */
  3091. #endif /* TCPSOCKET */
  3092. #endif /* NETCONN */
  3093.  
  3094.         len = strlen(tmpbuf);
  3095.         if (stuff >= 0 && len < tmpbufsiz - 1) {
  3096.         tmpbuf[len++] = stuff;
  3097.         if (stuff2 >= 0 && len < tmpbufsiz - 1)
  3098.           tmpbuf[len++] = stuff2;
  3099.         tmpbuf[len] = NUL;
  3100.         }
  3101.         ttol((CHAR *)tmpbuf,len);
  3102.         if (protocol == PROTO_K)    /* Give remote Kermit time to start */
  3103.           msleep(400);
  3104.     }
  3105.     }
  3106.  
  3107. #ifdef CK_XYZ
  3108.     if (protocol != PROTO_K) {        /* Non-Kermit protocol selected */
  3109.     char tmpbuf[356];
  3110.     int tmpbufsiz = 356;
  3111.     char * s = "";
  3112.  
  3113. #ifdef CK_TMPDIR
  3114.     if (sstate == 'v') {        /* If receiving and... */
  3115.         if (dldir && !f_tmpdir) {    /* if they have a download directory */
  3116.         if (s = zgtdir()) {    /* Get current directory */
  3117.             if (zchdir(dldir)) { /* Change to download directory */
  3118.             ckstrncpy(savdir,s,TMPDIRLEN);
  3119.             f_tmpdir = 1;    /* Remember that we did this */
  3120.             }
  3121.         }
  3122.         }
  3123.     }
  3124. #endif /* CK_TMPDIR */
  3125.  
  3126. #ifdef XYZ_INTERNAL            /* Internal */
  3127.     success = !pxyz(sstate);
  3128. #else
  3129. #ifdef CK_REDIR                /* External */
  3130.     switch (sstate) {
  3131.       case 's':            /* 'Tis better to SEND... */
  3132.         s = binary ? ptab[protocol].p_b_scmd : ptab[protocol].p_t_scmd;
  3133.         break;
  3134.       case 'v':            /* ... than RECEIVE */
  3135.         s = binary ? ptab[protocol].p_b_rcmd : ptab[protocol].p_t_rcmd;
  3136.         break;
  3137.     }
  3138.     if (!s) s = "";
  3139.     if (*s) {
  3140.         if (sstate == 's') {
  3141.         if ((int)strlen(s) + (int)strlen(fspec) < tmpbufsiz) {
  3142.             sprintf(tmpbuf,s,fspec); /* safe (prechecked) */
  3143.             tlog(F110,"Sending",fspec,0L);
  3144.         }
  3145.         } else {
  3146.         if ((int)strlen(s) + (int)strlen(cmarg2) < tmpbufsiz) {
  3147.             sprintf(tmpbuf,s,cmarg2); /* safe (prechecked) */
  3148.             tlog(F110,"Receiving",cmarg2,0L);
  3149.         }
  3150.         }
  3151.         tlog(F110," via external protocol:",tmpbuf,0);
  3152.         debug(F110,"ckcpro ttruncmd",tmpbuf,0);
  3153.         success = ttruncmd(tmpbuf);
  3154.         tlog(F110," status:",success ? "OK" : "FAILED", 0);
  3155.     } else {
  3156.         printf("?Sorry, no external protocol defined for %s\r\n",
  3157.            ptab[protocol].p_name
  3158.            );
  3159.     }
  3160. #else
  3161.     printf(
  3162. "Sorry, only Kermit protocol is supported in this version of Kermit\n"
  3163.            );
  3164. #endif /* CK_REDIR */
  3165. #endif /* XYZ_INTERNAL */
  3166.     return;
  3167.     }
  3168. #endif /* CK_XYZ */
  3169.  
  3170. #ifdef NTSIGX
  3171.     conraw();
  3172.     connoi();
  3173. #else
  3174.     if (!local)
  3175.       connoi();                /* No console interrupts if remote */
  3176. #endif /* NTSIG */
  3177.  
  3178.     kactive = 1;
  3179.     if (sstate == 'x') {        /* If entering server mode, */
  3180.     server = 1;            /* set flag, */
  3181.     debug(F101,"server backgrd","",backgrd);
  3182.     debug(F101,"server quiet","",quiet);
  3183.     debug(F100,"SHOULD NOT SEE THIS IF IN BACKGROUND!","",0);
  3184.     if (!local) {            /* and issue appropriate message. */
  3185.         if (!quiet && !backgrd
  3186. #ifdef IKS_OPTION
  3187.                 && !TELOPT_ME(TELOPT_KERMIT) /* User was told by negotiation */
  3188. #endif /* IKS_OPTION */
  3189.         ) {
  3190.         conoll(srvtxt);
  3191.         conoll("KERMIT READY TO SERVE...");
  3192.         }
  3193.     } else {
  3194.         conol("Entering server mode on ");
  3195.         conoll(ttname);
  3196.         conoll("Type Ctrl-C to quit.");
  3197.         if (srvdis) intmsg(-1L);
  3198. #ifdef TCPSOCKET
  3199. #ifndef NOLISTEN
  3200.         if (network && tcpsrfd > 0)
  3201.           ttol((CHAR *)"KERMIT READY TO SERVE...\015\012",26);
  3202. #endif /* NOLISTEN */
  3203. #endif /* TCPSOCKET */
  3204.     }
  3205.     } else
  3206.       server = 0;
  3207. #ifdef VMS
  3208.     if (!quiet && !backgrd)    /* So message doesn't overwrite prompt */
  3209.       conoll("");
  3210.     if (local) conres();       /* So Ctrl-C will work */
  3211. #endif /* VMS */
  3212. /*
  3213.   If in remote mode, not shushed, not in background, and at top command level,
  3214.   issue a helpful message telling what to do...
  3215. */
  3216.     if (!local && !quiet && !backgrd) {
  3217.     if (sstate == 'v') {
  3218.         conoll("Return to your local Kermit and give a SEND command.");
  3219.         conoll("");
  3220.         conoll("KERMIT READY TO RECEIVE...");
  3221.     } else if (sstate == 's') {
  3222.         conoll("Return to your local Kermit and give a RECEIVE command.");
  3223.         conoll("");
  3224.         conoll("KERMIT READY TO SEND...");
  3225.     } else if ( sstate == 'g' || sstate == 'r' || sstate == 'h' ||
  3226.             sstate == 'j' || sstate == 'c' ) {
  3227.         conoll("Return to your local Kermit and give a SERVER command.");
  3228.         conoll("");
  3229.         conoll((sstate == 'r' || sstate == 'j' || sstate == 'h') ?
  3230.            "KERMIT READY TO GET..." :
  3231.            "KERMIT READY TO SEND SERVER COMMAND...");
  3232.     }
  3233.     }
  3234. #ifdef COMMENT
  3235.     if (!local) sleep(1);
  3236. #endif /* COMMENT */
  3237. /*
  3238.   The 'wart()' function is generated by the wart program.  It gets a
  3239.   character from the input() routine and then based on that character and
  3240.   the current state, selects the appropriate action, according to the state
  3241.   table above, which is transformed by the wart program into a big case
  3242.   statement.  The function is active for one transaction.
  3243. */
  3244.     rtimer();                /* Reset elapsed-time timer */
  3245. #ifdef GFTIMER
  3246.     rftimer();
  3247. #endif /* GFTIMER */
  3248.     resetc();                /* & other per-transaction counters. */
  3249.  
  3250.     debug(F101,"proto calling wart, justone","",justone);
  3251.  
  3252.     wart();                /* Enter the state table switcher. */
  3253. /*
  3254.   Note: the following is necessary in case we have just done a remote-mode
  3255.   file transfer, in which case the controlling terminal modes have been
  3256.   changed by ttpkt().  In particular, special characters like Ctrl-C and
  3257.   Ctrl-\ might have been turned off (see ttpkt).  So this call to ttres() is
  3258.   essential.  IMPORTANT: restore interrupt handlers first, otherwise any
  3259.   terminal interrupts that occur before this is done in the normal place
  3260.   later will cause a crash.
  3261. */
  3262. #ifdef OS2
  3263.     ttres();                /* Reset the communication device */
  3264. #else
  3265.     if (!local) {
  3266.     setint();            /* Arm interrupt handlers FIRST */
  3267.     msleep(500);
  3268.     ttres();            /* Then restore terminal. */
  3269.     }
  3270. #endif /* OS2 */
  3271.     xxscreen(SCR_TC,0,0L,"");        /* Transaction complete */
  3272.     x = quiet;
  3273.     quiet=1;
  3274.     clsif();                /* Failsafe in case we missed */
  3275.     clsof(1);                /* a case in the state machine. */
  3276.     quiet = x;
  3277.  
  3278.     if (server) {            /* Back from packet protocol. */
  3279.         if (!quiet && !backgrd
  3280. #ifdef IKSD
  3281.         && !inserver
  3282. #endif /* IKSD */
  3283.         ) {                /* Give appropriate message */
  3284.         conoll("");
  3285.         conoll("C-Kermit server done");
  3286.         }
  3287.         server = 0;            /* Not a server any more */
  3288.     }
  3289. }
  3290.  
  3291. /*  S G E T I N I T  --  Handle incoming GET-Class packets  */
  3292.  
  3293. /*
  3294.   Returns:
  3295.    -1: On error
  3296.     0: GET packet processed OK - ready to Send.
  3297.     1: Extended GET processed OK - wait for another.
  3298. */
  3299. static int
  3300. sgetinit(reget,xget) int reget, xget; {    /* Server end of GET command */
  3301.     char * fs = NULL;            /* Pointer to filespec */
  3302.     int i, n, done = 0;
  3303. #ifdef PIPESEND
  3304.     extern int usepipes, pipesend;
  3305. #endif /* PIPESEND */
  3306.  
  3307.     if (!ENABLED(en_get)) {        /* Only if not disabled!  */
  3308.     errpkt((CHAR *)"GET disabled");
  3309.     return(-1);
  3310.     }
  3311.  
  3312.     /* OK to proceed */
  3313.  
  3314.     filcnt = 0;
  3315.  
  3316. #ifdef WHATAMI
  3317.     /* If they are alike this was already done in whoarewe() */
  3318.     debug(F101,"sgetinit whatru","",whatru);
  3319.     if (whatru & WMI_FLAG) {        /* Did we get WHATAMI info? */
  3320.     debug(F101,"sgetinit binary (1)","",binary);
  3321. #ifdef VMS
  3322.     if (binary != XYFT_I && binary != XYFT_L)
  3323. #else
  3324. #ifdef OS2
  3325.       if (binary != XYFT_L)
  3326. #endif /* OS2 */
  3327. #endif /* VMS */
  3328.         binary = (whatru & WMI_FMODE) ? /* Yes, set file type */
  3329.           XYFT_B : XYFT_T;    /* automatically */
  3330.     debug(F101,"sgetinit binary (2)","",binary);
  3331.     if (!wearealike)
  3332.       fncnv = (whatru & WMI_FNAME) ? 1 : 0; /* And name conversion */
  3333.     }
  3334. #endif /* WHATAMI */
  3335.  
  3336.     fs = (char *)srvcmd;
  3337.     srvptr = srvcmd;            /* Point to server command buffer */
  3338.     decode(rdatap,putsrv,0);        /* Decode the GET command into it */
  3339.     /* Accept multiple filespecs */
  3340.     cmarg2 = "";            /* Don't use cmarg2 */
  3341.     cmarg = "";                /* Don't use cmarg */
  3342.  
  3343.     done = 1;                /* Only 1 packet needed... */
  3344.     if (xget) {                /* Special decoding for Extended GET */
  3345.     char L, next, c;        /* PLV items */
  3346.     int len, val;            /* More PLV items */
  3347.     char * p = (char *)srvcmd;    /* String to decode */
  3348.  
  3349.     done = 0;            /* Maybe more packets needed */
  3350.     fs = NULL;            /* We don't know the filespec yet */
  3351.     c = *p++;            /* Get first parameter */
  3352.  
  3353.     while (c) {            /* For all parameters... */
  3354.         debug(F000,"sgetinit c","",c);
  3355.         L = *p++;            /* Get length */
  3356.         if (L >= SP)        /* Decode length */
  3357.           len = xunchar(L);
  3358.         else if (c == '@') {    /* Allow missing EOP length field */
  3359.         len = 0;
  3360.         } else {
  3361.         len = (xunchar(*p++) * 95);
  3362.         len += xunchar(*p++);
  3363.         }
  3364.         debug(F101,"sgetinit len","",len);
  3365.         next = *(p+len);        /* Get next parameter */
  3366.         *(p+len) = NUL;        /* Zero it out to terminal value */
  3367.         debug(F110,"sgetinit p",p,0);
  3368.         switch (c) {        /* Do the parameter */
  3369.           case 'O':            /* GET Options */
  3370.         val = atoi(p);        /* Convert to int */
  3371.         debug(F101,"sgetinit O val","",val);
  3372.         if (val & GOPT_DEL) moving = 1;
  3373.         if (val & GOPT_RES) reget = 1;
  3374.         if (val & GOPT_REC) {
  3375.             recursive = 1;
  3376.             if (fnspath == PATH_OFF)
  3377.               fnspath = PATH_REL;
  3378.         }
  3379.         break;
  3380.           case 'M':            /* Transfer Mode */
  3381.         val = atoi(p);
  3382.         debug(F101,"sgetinit M val","",val);
  3383.         if (val < 1)
  3384.           break;
  3385.         patterns = 0;        /* Takes precedence over patterns */
  3386.         filepeek = 0;        /* and FILE SCAN */
  3387.         if (val == GMOD_TXT) binary = XYFT_T; /* Text */
  3388.         if (val == GMOD_BIN) binary = XYFT_B; /* Binary */
  3389.         if (val == GMOD_LBL) binary = XYFT_L; /* Labeled */
  3390.         break;
  3391.           case 'F':            /* Filename */
  3392.         fs = p;
  3393.         debug(F110,"sgetinit filename",fs,0);
  3394.         break;
  3395.           case '@':            /* End Of Parameters */
  3396.         done = 1;
  3397.         debug(F100,"sgetinit EOP","",0);
  3398.         break;
  3399.           default:
  3400.         errpkt((CHAR *)"Unknown GET Parameter");
  3401.         debug(F100,"sgetinit unknown parameter","",0);
  3402.         return(-1);
  3403.         }
  3404.         p += (len + 1);
  3405.         c = next;
  3406.     }
  3407.     }
  3408.     if (!fs) fs = "";            /* A filename is required */
  3409.     if (*fs) {
  3410.     havefs = 1;
  3411.     n = 0;                /* Check for quoted name */
  3412.     if ((n = strlen(fs)) > 1) {
  3413.         /* Note: this does not allow for multiple quoted names */
  3414.         if ((fs[0] == '{' && fs[n-1] == '}') ||
  3415.         (fs[0] == '"' && fs[n-1] == '"')) {
  3416.         fs[n-1] = '\0';
  3417.         fs++;
  3418.         debug(F111,"sgetinit unquoted filename",fs,n);
  3419.         } else
  3420.           n = 0;            /* This means no quoting */
  3421.     }
  3422.  
  3423. #ifdef PIPESEND
  3424.     debug(F111,"sgetinit",fs,usepipes);
  3425.     if (usepipes && ENABLED(en_hos) && *fs == '!') {
  3426.         cmarg = fs + 1;        /* Point past the bang */
  3427.         *fs = NUL;
  3428.         nfils = -1;
  3429.         pipesend = 1;
  3430.         debug(F111,"sgetinit pipesend",cmarg,pipesend);
  3431.     }
  3432.     if (!pipesend) {        /* If it's not a pipe */
  3433. #endif /* PIPESEND */
  3434.         if (n == 0) {        /* If the name was not quoted */
  3435. #ifndef NOMSEND
  3436.         nfils = fnparse(fs);    /* Allow it to be a list of names */
  3437.         debug(F111,"sgetinit A",fs,nfils);
  3438. #ifdef COMMENT
  3439. /* This doesn't work if a GET-PATH is set. */
  3440.         if (nfils == 1 && !iswild(fs)) { /* Single file */
  3441.             char * m;
  3442.             if ((x = zchki(fs)) < 0) { /* Check if it's sendable */
  3443.             switch (x) {
  3444.               case -1: m = "File not found"; break;
  3445.               case -2: m = "Not a regular file"; break;
  3446.               case -3: m = "Read access denied"; break;
  3447.             }
  3448.             errpkt((CHAR *)m);
  3449.             return(-1);
  3450.             }
  3451.         }
  3452. #endif /* COMMENT */
  3453.         } else {            /* If it was quoted */
  3454. #endif /* NOMSEND */
  3455.         nzxopts = 0;
  3456. #ifdef UNIXOROSK
  3457.         if (matchdot)  nzxopts |= ZX_MATCHDOT;
  3458. #endif /* UNIXOROSK */
  3459.         if (recursive) nzxopts |= ZX_RECURSE;
  3460.         /* Treat as a single filespec */
  3461.         nfils = 0 - nzxpand(fs,nzxopts);
  3462.         debug(F111,"sgetinit B",fs,nfils);
  3463.         cmarg = fs;
  3464.         }
  3465. #ifdef PIPESEND
  3466.     }
  3467. #endif /* PIPESEND */
  3468.     }
  3469.     if (!done) {            /* Need more O packets... */
  3470.     debug(F100,"sgetinit O-Packet TBC","",0); /* To Be Continued */
  3471.     return(1);
  3472.     }
  3473.     debug(F100,"sgetinit O-Packet done - havefs","",havefs);
  3474.     if (!havefs) {            /* Done - make sure we have filename */
  3475.     errpkt((CHAR *)"GET without filename");
  3476.     return(-1);
  3477.     }
  3478.     freerpkt(winlo);
  3479.     winlo = 0;                /* Back to packet 0 again. */
  3480.     debug(F101,"sgetinit winlo","",winlo);
  3481.     nakstate = 0;            /* Now I'm the sender! */
  3482.     if (reget) sendmode = SM_RESEND;
  3483.     if (sinit() > 0) {            /* Send Send-Init */
  3484. #ifdef STREAMING
  3485.     if (!streaming)
  3486. #endif /* STREAMING */
  3487.       timint = chktimo(rtimo,timef); /* Switch to per-packet timer */
  3488.     return(0);            /* If successful, switch state */
  3489.     } else return(-1);            /* Else back to server command wait */
  3490. }
  3491.  
  3492. #else  /* NOXFER */
  3493.  
  3494. #include "ckcdeb.h"
  3495.  
  3496. VOID
  3497. proto() {
  3498.     extern int success;
  3499.     success = 0;
  3500. }
  3501. #endif /* NOXFER */
  3502.