home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / ckc095.zip / ckcpro.w < prev    next >
Text File  |  1989-08-31  |  16KB  |  457 lines

  1. char *protv = "C-Kermit Protocol Module 4F(034), 19 Jun 89"; /* -*-C-*- */
  2.  
  3. /* C K C P R O  -- C-Kermit Protocol Module, in Wart preprocessor notation. */
  4. /*
  5.  Author: Frank da Cruz (fdc@columbia.edu, FDCCU@CUVMA.BITNET),
  6.  Columbia University Center for Computing Activities.
  7.  First released January 1985.
  8.  Copyright (C) 1985, 1989, Trustees of Columbia University in the City of New 
  9.  York.  Permission is granted to any individual or institution to use, copy, or
  10.  redistribute this software so long as it is not sold for profit, provided this
  11.  copyright notice is retained. 
  12. */
  13. #include "ckcdeb.h"
  14. #include "ckcker.h"
  15. /*
  16.  Note -- This file may also be preprocessed by the Unix Lex program, but 
  17.  you must indent the above #include statements before using Lex, and then
  18.  restore them to the left margin in the resulting C program before compilation.
  19.  Also, the invocation of the "wart()" function below must be replaced by an
  20.  invocation  of the "yylex()" function.  It might also be necessary to remove
  21.  comments in the %%...%% section.
  22. */
  23.  
  24. /* State definitions for Wart (or Lex) */
  25. %states ipkt rfile rattr rdata ssinit ssfile ssattr ssdata sseof sseot
  26. %states serve generic get rgen
  27.  
  28. /* External C-Kermit variable declarations */
  29.   extern char sstate, *versio, *srvtxt, *cmarg, *cmarg2, *rpar();
  30.   extern char data[], filnam[], srvcmd[], ttname[], *srvptr;
  31.   extern int pktnum, timint, nfils, hcflg, xflg, speed, flow, mdmtyp;
  32.   extern int prvpkt, cxseen, czseen, server, local, displa, bctu, bctr, quiet;
  33.   extern int tsecs, parity, backgrd, nakstate, atcapu;
  34.   extern int putsrv(), puttrm(), putfil(), errpkt();
  35.   extern CHAR *rdatap, recpkt[];
  36.   extern char *DIRCMD, *DELCMD, *TYPCMD, *SPACMD, *SPACM2, *WHOCMD;
  37.   extern struct zattr iattr;
  38.  
  39. /* Local variables */
  40.   static char vstate = 0;          /* Saved State   */
  41.   static char vcmd = 0;            /* Saved Command */
  42.   int x;                /* General-purpose integer */
  43.   char *s;                /* General-purpose string pointer */
  44.  
  45. /* Macros - Note, BEGIN is predefined by Wart (and Lex) as "state = ", */
  46. /* BEGIN is NOT a GOTO! */
  47. #define SERVE  tinit(); BEGIN serve
  48. #define RESUME if (server) { SERVE; } else { sleep(2); return; }
  49.  
  50. %%
  51. /* Protocol entry points, one for each start state (sstate) */
  52.  
  53. s { tinit();                            /* Do Send command */
  54.     if (sinit()) BEGIN ssinit;
  55.        else RESUME; }
  56.  
  57. v { tinit(); BEGIN get; }                         /* Receive */
  58. r { tinit(); vstate = get;  vcmd = 0;   sipkt('I'); BEGIN ipkt; } /* Get */
  59. c { tinit(); vstate = rgen; vcmd = 'C'; sipkt('I'); BEGIN ipkt; } /* Host */
  60. g { tinit(); vstate = rgen; vcmd = 'G'; sipkt('I'); BEGIN ipkt; } /* Generic */
  61.  
  62. x { sleep(1); SERVE; }                    /* Be a Server */
  63.  
  64. a { errpkt("User cancelled transaction"); /* "ABEND" -- Tell other side. */
  65.     return(0); }            /* Return from protocol. */
  66.  
  67. /* Dynamic states: <current-states>input-character { action } */
  68.  
  69. <rgen,get,serve>S {            /* Receive Send-Init packet */
  70.     rinit(rdatap);            /* Set parameters */
  71.     bctu = bctr;            /* Switch to agreed-upon block check */
  72.     resetc();                /* Reset counters */
  73.     rtimer();                /* Reset timer */
  74.     BEGIN rfile;            /* Go into receive-file state */
  75. }
  76.  
  77. /* States in which we get replies back from commands sent to a server. */
  78.  
  79. <ipkt>Y {                /* Get ack for I-packet */
  80.     spar(rdatap);            /* Set parameters */
  81.     if (vcmd) {                /* If sending a generic command */
  82.     scmd(vcmd,cmarg);        /* Do that */
  83.     vcmd = 0;            /* and then un-remember it. */
  84.     }
  85.     if (vstate == get) srinit();    /* If sending GET command, do that */
  86.     BEGIN vstate;            /* Switch to desired state */
  87. }
  88.  
  89. <ipkt>E {                /* Ignore Error reply to I packet */
  90.     if (vcmd) {                /* in case other Kermit doesn't */
  91.     scmd(vcmd,cmarg);        /* understand I-packets. */
  92.     vcmd = 0;            /* Otherwise act as above... */
  93.     }
  94.     if (vstate == get) srinit();
  95.     BEGIN vstate;
  96. }
  97.  
  98. <get>Y {        /* Resend of previous I-pkt ACK, same seq number! */
  99.     srinit();
  100. }
  101.  
  102. /* States in which we're being a server */
  103.  
  104. <serve>I {                /* Get I-packet */
  105.     spar(rdatap);            /* Set parameters from it */
  106.     ack1(rpar());            /* Respond with our own parameters */
  107.     pktnum = 0;                /* Reset packet sequence numbers */
  108.     prvpkt = -1;            /* Stay in server command wait */
  109. }
  110.  
  111. <serve>R {                /* Get Receive-Init */
  112.     srvptr = srvcmd;            /* Point to server command buffer */
  113.     decode(rdatap,putsrv);        /* Decode the GET command into it */
  114.     cmarg = srvcmd;
  115.     nfils = -1;                /* Initialize number of files */
  116.     if (sinit())            /* Send Send-Init */
  117.       BEGIN ssinit;            /* If successful, switch state */
  118.     else { SERVE; }            /* Else back to server command wait */
  119. }
  120.  
  121. <serve>G {                /* Generic server command */
  122.     srvptr = srvcmd;            /* Point to command buffer */
  123.     decode(rdatap,putsrv);        /* Decode packet data into it */
  124.     putsrv('\0');            /* Insert a couple nulls */
  125.     putsrv('\0');            /* for termination */
  126.     sstate = srvcmd[0];            /* Set requested start state */
  127.     BEGIN generic;            /* Switch to generic command state */
  128. }
  129.  
  130. <serve>C {                /* Receive Host command */
  131.     srvptr = srvcmd;            /* Point to command buffer */
  132.     decode(rdatap,putsrv);        /* Decode command packet into it */
  133.     putsrv('\0');            /* Null-terminate */
  134.     if (syscmd(srvcmd,""))        /* Try to execute the command */
  135.       BEGIN ssinit;            /* If OK, send back its output */
  136.     else {                /* Otherwise */
  137.     errpkt("Can't do system command"); /* report error */
  138.     SERVE;                /* & go back to server command wait */
  139.     }
  140. }
  141.  
  142. <serve>. {                /* Any other command in this state */
  143.     errpkt("Unimplemented server function"); /* we don't know about */
  144.     SERVE;                /* back to server command wait */
  145. }
  146.  
  147. <generic>C {                /* Got REMOTE CWD command */
  148.     if (!cwd(srvcmd+1)) errpkt("Can't change directory"); /* Try to do it */
  149.     SERVE;                /* Back to server command wait */
  150. }
  151.  
  152. <generic>D {                /* REMOTE DIRECTORY command */
  153.     if (syscmd(DIRCMD,srvcmd+2))    /* If it can be done */
  154.       BEGIN ssinit;            /* send the results back */
  155.     else {                /* otherwise */
  156.     errpkt("Can't list directory");    /* report failure */
  157.     SERVE;                /* & return to server command wait */
  158.     }
  159. }
  160.  
  161. <generic>E {                /* REMOTE DELETE (Erase) command */
  162.     if (syscmd(DELCMD,srvcmd+2))    /* Try to do it */
  163.       BEGIN ssinit;            /* If OK send results back */
  164.     else {                /* otherwise */
  165.     errpkt("Can't remove file");    /* report failure */
  166.     SERVE;                /* & return to server command wait */
  167.     }
  168. }
  169.  
  170. <generic>F {                /* FINISH */
  171.     ack();                /* Acknowledge */
  172.     screen(SCR_TC,0,0l,"");        /* Display */
  173.     return(0);                /* Done */
  174. }
  175.  
  176. <generic>L {                /* BYE (LOGOUT) */
  177.     ack();                /* Acknowledge */
  178.     ttres();                /* Reset the terminal */
  179.     screen(SCR_TC,0,0l,"");        /* Display */
  180.     return(zkself());            /* Try to log self out */
  181. }
  182.  
  183. <generic>H {                /* REMOTE HELP */
  184.     if (sndhlp()) BEGIN ssinit;        /* Try to send it */
  185.     else {                /* If not ok, */
  186.     errpkt("Can't send help");    /* send error message instead */
  187.     SERVE;                /* and return to server command wait */
  188.     }
  189. }
  190.  
  191. <generic>T {                /* REMOTE TYPE */
  192.     if (syscmd(TYPCMD,srvcmd+2))    /* Try */
  193.       BEGIN ssinit;            /* OK */
  194.     else {                /* not OK */
  195.     errpkt("Can't type file");    /* give error message */
  196.     SERVE;                /* wait for next server command */
  197.     }
  198. }
  199.  
  200. <generic>U {                /* REMOTE SPACE */
  201.     x = *(srvcmd+1);            /* Get area to check */
  202.     x = ((x == '\0') || (x == SP));
  203.     x = (x ? syscmd(SPACMD,"") : syscmd(SPACM2,srvcmd+2));
  204.     if (x)                /* If we got the info */
  205.       BEGIN ssinit;            /* send it */
  206.     else {                /* otherwise */
  207.     errpkt("Can't check space");    /* send error message */
  208.     SERVE;                /* and await next server command */
  209.     }
  210. }
  211.  
  212. <generic>W {                /* REMOTE WHO */
  213.     if (syscmd(WHOCMD,srvcmd+2))    /* The now-familiar scenario... */
  214.       BEGIN ssinit;
  215.     else {
  216.     errpkt("Can't do who command");
  217.     SERVE;
  218.     }
  219. }
  220.  
  221. <generic>. {                /* Anything else in this state... */
  222.     errpkt("Unimplemented generic server function"); /* Complain */
  223.     SERVE;                /* and return to server command wait */
  224. }
  225.  
  226. <rgen>Y {                /* Short-Form reply */
  227.     decode(rdatap,puttrm);        /* in ACK Data field */
  228.     RESUME;
  229. }
  230.  
  231. <rgen,rfile>F {                /* File header */
  232.     xflg = 0;                /* Not screen data */
  233.     rcvfil(filnam);            /* Figure out local filename */
  234.     encstr(filnam);            /* Encode it */
  235.     ack1(data);                /* Send it back in ACK */
  236.     initattr(&iattr);            /* Clear file attribute structure */
  237.     nakstate = 1;            /* In this state we can send NAKs */
  238.     BEGIN rattr;            /* Now expect Attribute packets */
  239. }
  240.  
  241. <rgen,rfile>X {                /* X-packet instead of file header */
  242.     xflg = 1;                /* Screen data */
  243.     ack();                /* Acknowledge the X-packet */
  244.     initattr(&iattr);            /* Initialize attribute structure */
  245.     nakstate = 1;            /* Say that we can send NAKs */
  246.     BEGIN rattr;            /* Expect Attribute packets */
  247. }
  248.  
  249. <rattr>A {                /* Attribute packet */
  250.     if (gattr(rdatap,&iattr) == 0)    /* Read into attribute structure */
  251.       ack();                /* If OK, acknowledge */
  252.     else                /* If not */
  253.       ack1("N");            /* refuse to accept the file */
  254. }
  255.  
  256. <rattr>D {                /* First data packet */
  257.     if (xflg)                /* If screen data */
  258.       x = opent();            /* "open" the screen */
  259.     else                /* otherwise */
  260.       x = opena(filnam,&iattr);        /* open the file, with attributes */
  261.     if (x) {                /* If file was opened ok */
  262.     if (decode(rdatap,putfil) < 0) { /* decode first data packet */
  263.         errpkt("Error writing data");
  264.         RESUME;
  265.     }
  266.     ack();                /* acknowledge it */
  267.     BEGIN rdata;            /* and switch to receive-data state */
  268.     } else {                /* otherwise */
  269.     errpkt("Can't open file");    /* send error message */
  270.     RESUME;                /* and quit. */
  271.     }
  272. }
  273.  
  274. <rfile>B {                /* EOT, no more files */
  275.     ack();                /* Acknowledge */
  276.     tsecs = gtimer();            /* Get timing for statistics */
  277.     reot();                /* Do EOT things */
  278.     RESUME;                /* and quit */
  279. }
  280.  
  281. <rdata>D {                /* Data packet */
  282.     if (cxseen)                /* If file interrupt */
  283.       ack1("X");            /* put "X" in ACK */
  284.     else if (czseen)            /* If file-group interrupt */
  285.       ack1("Z");            /* put "Z" in ACK */
  286.     else if (decode(rdatap,putfil) < 0) { /* Normal case, */
  287.     errpkt("Error writing data");    /*   decode data to file */
  288.     RESUME;                /* Send ACK if data written */
  289.     } else ack();            /* to file OK. */
  290. }
  291.  
  292. <rdata,rattr>Z {            /* End Of File (EOF) Packet */
  293.     if (reof(&iattr) < 0) {        /* Close & dispose of the file */
  294.     errpkt("Can't close file");    /* If problem, send error message */
  295.     RESUME;                /* and quit */
  296.     } else {                /* otherwise */
  297.     ack();                /* acknowledge the EOF packet */
  298.     BEGIN rfile;            /* and await another file */
  299.     }
  300. }
  301.  
  302. <ssinit>Y {                /* ACK for Send-Init */
  303.     spar(rdatap);            /* set parameters from it */
  304.     bctu = bctr;            /* switch to agreed-upon block check */
  305.     x = sfile(xflg);            /* Send X or F header packet */
  306.     if (x) {                /* If the packet was sent OK */
  307.     resetc();            /* reset per-transaction counters */
  308.     rtimer();            /* reset timers */
  309.     BEGIN ssfile;            /* and switch to receive-file state */
  310.     } else {                /* otherwise send error msg & quit */
  311.     s = xflg ? "Can't execute command" : "Can't open file";
  312.     errpkt(s);
  313.     RESUME;
  314.     }
  315. }
  316.  
  317. <ssfile>Y {                /* ACK for F packet */
  318.     srvptr = srvcmd;            /* Point to string buffer */
  319.     decode(rdatap,putsrv);        /* Decode data field, if any */
  320.     putsrv('\0');            /* Terminate with null */
  321.     if (*srvcmd)            /* If remote name was recorded */
  322.       tlog(F110," stored as",srvcmd,0); /* Record it in transaction log. */
  323.     if (atcapu) {            /* If attributes are to be used */
  324.     if (sattr(xflg) < 0) {        /* set and send them */
  325.         errpkt("Can't send attributes"); /* if problem, say so */
  326.         RESUME;                 /* and quit */
  327.     } else BEGIN ssattr;        /* if ok, switch to attribute state */
  328.     } else if (sdata() < 0) {        /* No attributes, send data */
  329.     clsif();            /* If not ok, close input file, */
  330.     seof("");            /* send EOF packet */
  331.     BEGIN sseof;            /* and switch to EOF state. */
  332.     } else BEGIN ssdata;        /* All ok, switch to send-data state */
  333. }
  334.  
  335. <ssattr>Y {                /* Got ACK to A packet */
  336.     if (rsattr(rdatap) < 0) {        /* Was the file refused? */
  337.     clsif();            /* yes, close it */
  338.     seof("D");            /* send EOF with "discard" code */
  339.     BEGIN sseof;            /* switch to send-EOF state */
  340.     } else if (sdata() < 0) {        /* File accepted, send data */
  341.     clsif();            /* If problem, close input file */
  342.     seof("");            /* send EOF packet */
  343.     BEGIN sseof;            /* and switch to send-EOF state. */
  344.     } else BEGIN ssdata;        /* All ok, enter send-data state. */
  345. }
  346.  
  347. <ssdata>Y {                /* Got ACK to Data packet */
  348.     if (canned(rdatap)) {        /* If file transfer cancelled */
  349.     clsif();            /* close input file */
  350.     seof("D");            /* send EOF packet with Discard code */
  351.     BEGIN sseof;            /* switch to EOF state */
  352.     } else if (sdata() < 0) {        /* Not cancelled, send next data */
  353.     clsif();            /* If there was a problem close file */
  354.     seof("");            /* Send EOF packet */
  355.     BEGIN sseof;            /* enter send-eof state */
  356.     }
  357. }
  358.  
  359. <sseof>Y {                /* Got ACK to EOF */
  360.     if (gnfile() > 0) {            /* Any more files to send? */
  361.     if (sfile(xflg))        /* Yes, try to send next file header */
  362.       BEGIN ssfile;            /* if ok, enter send-file state */
  363.     else {                /* otherwise */
  364.         errpkt("Can't open file");    /* send error message */
  365.         RESUME;            /* and quit */
  366.     }
  367.     } else {                /* No next file */
  368.     tsecs = gtimer();        /* get statistics timers */
  369.     seot();                /* send EOT packet */
  370.     BEGIN sseot;            /* enter send-eot state */
  371.     }
  372. }
  373.  
  374. <sseot>Y {                /* Got ACK to EOT */
  375.     RESUME;                /* All done, just quit */
  376. }
  377.  
  378. E {                    /* Got Error packet, in any state */
  379.     ermsg(rdatap);            /* Issue message. */
  380.     x = quiet; quiet = 1;        /* Close files silently, */
  381.     clsif(); clsof(1);            /* discarding any output file. */
  382.     tsecs = gtimer();            /* Get timers */
  383.     quiet = x;                /* restore quiet state */
  384.     if (backgrd && !server) fatal("Protocol error");
  385.     RESUME;
  386. }
  387.  
  388. . {                    /* Anything not accounted for above */
  389.     errpkt("Unknown packet type");    /* Give error message */
  390.     RESUME;                /* and quit */
  391. }
  392. %%
  393.  
  394. /*  P R O T O  --  Protocol entry function  */
  395.  
  396. proto() {
  397.  
  398.     extern int sigint();
  399.     int x;
  400.  
  401.     conint(sigint);            /* Enable console interrupts */
  402.  
  403. /* Set up the communication line for file transfer. */
  404.  
  405.     if (local && (speed < 0)) {
  406.     screen(SCR_EM,0,0l,"Sorry, you must 'set speed' first");
  407.     return;
  408.     }
  409.  
  410.     x = -1;
  411.     if (ttopen(ttname,&x,mdmtyp) < 0) {
  412.     debug(F111,"failed: proto ttopen local",ttname,local);
  413.     screen(SCR_EM,0,0l,"Can't open line");
  414.     return;
  415.     }
  416.     if (x > -1) local = x;
  417.     debug(F111,"proto ttopen local",ttname,local);
  418.  
  419.     x = (local) ? speed : -1;
  420.     if (ttpkt(x,flow,parity) < 0) {    /* Put line in packet mode, */
  421.     screen(SCR_EM,0,0l,"Can't condition line");
  422.     return;
  423.     }
  424.     if (sstate == 'x') {        /* If entering server mode, */
  425.     server = 1;            /* set flag, */
  426.     if (!quiet) {
  427.         if (!local)            /* and issue appropriate message. */
  428.             conol(srvtxt);
  429.         else {
  430.             conol("Entering server mode on ");
  431.         conoll(ttname);
  432.         }
  433.     }
  434.     } else server = 0;
  435.     if (sstate == 'v' && !local && !quiet)
  436.       conoll("Escape back to your local Kermit and give a SEND command...");
  437.     if (sstate == 's' && !local && !quiet)
  438.       conoll("Escape back to your local Kermit and give a RECEIVE command...");
  439.     sleep(1);
  440. /*
  441.  The 'wart()' function is generated by the wart program.  It gets a
  442.  character from the input() routine and then based on that character and
  443.  the current state, selects the appropriate action, according to the state
  444.  table above, which is transformed by the wart program into a big case
  445.  statement.  The function is active for one transaction.
  446. */
  447.     wart();                /* Enter the state table switcher. */
  448.     
  449.     if (server) {            /* Back from packet protocol. */
  450.     server = 0;
  451.         if (!quiet)              /* Give appropriate message */
  452.         conoll("C-Kermit server done");
  453.     }
  454.     ttres();
  455.     screen(SCR_TC,0,0l,"");        /* Transaction complete */
  456. }
  457.