home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / k95source / ckcpro.w < prev    next >
Text File  |  2020-01-01  |  105KB  |  3,601 lines

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