home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / pub / sinclairqla / qlkswi.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  25KB  |  663 lines

  1. /*
  2.     SWITCH_C - QL-Kermit protocol state switcher
  3.  
  4.     Based on ckcpro.w, (C) Columbia University
  5. */
  6.  
  7.  
  8. /* Include files */
  9.  
  10. #include "ram1_ker_h"                            /* Kermit definitions */
  11.  
  12. #include "flp1_fcntl_h"                          /* File opening modes */
  13.  
  14.  
  15. /* External variables */
  16.  
  17. extern int size;                                 /* Current packet size */
  18. extern int n;                                    /* Current packet number */
  19. extern int numtry;                               /* Retry count */
  20. extern int oldtry;                               /* Saved retry count */
  21. extern int debug;                                /* Debugging level */
  22. extern int retry;                                /* Retry limit */
  23.  
  24. extern long filein;                              /* File bytes received */
  25. extern long fileout;                             /* File bytes sent */
  26. extern long totin;                               /* Batch bytes received */
  27. extern long totout;                              /* Batch bytes sent */
  28.  
  29. extern bool toscr;                               /* Flag for screen output */
  30.  
  31. extern char sndpkt[];                            /* Send packet data */
  32. extern char recpkt[];                            /* Received packet data */
  33. extern char *newfilnam;                          /* Name of file created */
  34. extern char *destdev;                            /* Default destination device */
  35. extern char *sourdev;                            /* Default source device */
  36. extern char *errdata;                            /* Data for E packet */
  37.  
  38. extern char stype;                               /* Server command */
  39. extern char type;                                /* Current packet type */
  40.  
  41. extern char *cmarg,*cmarg2;                      /* Command arguments */
  42.  
  43. extern int fp;                                   /* Transfer file */
  44.  
  45. extern int _oserr;                               /* QDOS error code */
  46. extern int oserr;                                /* Copy of above */
  47.  
  48. extern bool warn8;                               /* Lost 8th bit warning given */
  49. extern bool cxseen;                              /* File interrupt flag */
  50. extern bool czseen;                              /* Batch interrupt flag */
  51.  
  52. extern int state;                                /* Current state */
  53.  
  54.  
  55. /* External functions */
  56.  
  57. extern char *stname();                           /* Printable state name */
  58.  
  59.  
  60. /* Local variables */
  61.  
  62. int num;                                         /* Packet number */
  63. int len;                                         /* Packet length */
  64. int pt;                                          /* Packet type */
  65.  
  66. static char ackd;                                /* Y packet data */
  67.  
  68.  
  69. /* Switcher macros */
  70.  
  71. #define CHKTRY     if (numtry++>retry) RERR
  72. #define SWREC      switch (pt = rpack(&len,&num,recpkt))
  73.  
  74. #define NNXT       n = (n+1)%64
  75.  
  76. #define NEXT(s)    return(s)
  77. #define STAY       return(state)
  78.  
  79. #define GOT(c)     case 'c':
  80. #define OTHER      default:
  81. #define NONE       case BAD:
  82. #define INTRPT     case INTE:
  83.  
  84. #define DONE       return(COMP)
  85. #define STOP       return(ABORT)
  86. #define QERR       return(qdoserr())
  87. #define PERR       return(proterr())
  88. #define IERR       return(interrupt())
  89. #define RERR       return(rtlim())
  90.  
  91.  
  92. /* SWITCHER - The state table switcher for all the Kermit actions.  It loops
  93.    until either it finishes, or an error is encountered.  The routines
  94.    called by switcher are responsible for changing the state, although the
  95.    choice of which new state may also be influenced by the current
  96.    Kermit command.  The entry state is set by the user's command.
  97. */
  98.  
  99. bool switcher(start)
  100. int start;
  101. {
  102.     newfilnam[0] = '\0';                         /* Set up file name */
  103.     n = 0;                                       /* Initialize packet number */
  104.     numtry = 0;                                  /* No retries yet */
  105.     tinit();                                     /* Set up default Send-Init values */
  106.  
  107.     state = start;                               /* Set up starting state */
  108.  
  109.     flushinput();                                /* Start each transaction clean */
  110.  
  111.     forever                                      /* Do this as long as necessary */
  112.     {
  113.          if (debfull) debugst();                 /* Log state and packet number */
  114.          if (state==ABORT) closef();             /* Clean up mess */
  115.  
  116.          switch (state)
  117.          {
  118.  
  119. case ABORT:   return(FALSE);                     /* Abort */
  120. case COMP:    return(TRUE);                      /* Complete */
  121. case R_INIT:  state = rinit();  endcase;         /* Receive-Init */
  122. case R_FILE:  state = rfile();  endcase;         /* Receive-File */
  123. case R_DATA:  state = rdata();  endcase;         /* Receive-Data */
  124. case S_INIT:  state = sinit();  endcase;         /* Send-Init */
  125. case S_FILE:  state = sfile();  endcase;         /* Send-File */
  126. case S_DATA:  state = sdata();  endcase;         /* Send-Data */
  127. case S_EOF:   state = seof();   endcase;         /* Send-End-of-File */
  128. case S_BRK:   state = sbreak(); endcase;         /* Send-Break */
  129. case S_COMD:  state = scomd();  endcase;         /* Send-Command */
  130. case G_INIT:  state = ginit();  endcase;         /* Get-Init */
  131. case K_ERR:   state = serr();   endcase;         /* Send-Error */
  132.  
  133. default:      error("Bad protocol state - %d",state);
  134.               return(FALSE);                     /* Unknown state, fail */
  135.          }
  136.     }
  137. }
  138.  
  139.  
  140. /* SERR - Send an error packet to remote machine */
  141.  
  142. int serr()
  143. {
  144.     if (*errdata=='\0')                          /* If no error message, */
  145.          strcpy(errdata,"No description");       /* put in a default one */
  146.     spack('E',n,strlen(errdata),errdata);        /* Send the error packet */
  147.     STOP;                                        /* and end transaction */
  148. }
  149.  
  150.  
  151. /* SCOMD - Send generic or system command to server */
  152.  
  153. int scomd()
  154. {
  155.     int l;
  156.  
  157.     CHKTRY;                                      /* Check retries */
  158.     printf("Sending command\n");
  159.  
  160.     len = stclen(cmarg);                         /* Get length of command name */
  161.     spack(stype,0,len,cmarg);                    /* Send command packet */
  162.     SWREC                                        /* What was the reply? */
  163.     {
  164.  
  165. GOT(Y)   if (stype=='G') DONE;                   /* Back to parser */
  166.          STAY;                                   /* Try again otherwise */
  167.  
  168. GOT(S)   rpar(recpkt);
  169.          l = spar(sndpkt);
  170.          spack('Y',n++,l,sndpkt);                /* Get ready to receive  */
  171.          toscr = TRUE;                           /* onto screen */
  172.          NEXT(R_DATA);                           /* any command output */
  173.  
  174. GOT(E)   prerrpkt(recpkt);                       /* Error, print message */
  175.          STOP;
  176.  
  177. GOT(N)   STAY;                                   /* NAK from server, keep trying */
  178.  
  179. GOT(B)   if (num!=n) STOP;                       /* Need right packet number here */
  180.          ack();                                  /* Say OK */
  181.          STAY;                                   /* and keep trying */
  182.  
  183. NONE     nak();                                  /* Appease server to avoid error */
  184.          STAY;                                   /* and try again */
  185.  
  186. INTRPT   IERR;                                   /* CTRL-E interrupt */
  187.  
  188. OTHER    PERR;                                   /* Some other packet type */
  189.  
  190.     }
  191. }
  192.  
  193.  
  194. /* GINIT - Ask server for file */
  195.  
  196. int ginit()
  197. {
  198.     int l;
  199.  
  200.     CHKTRY;                                      /* Check retries */
  201.     printf("Asking server for %s\n",cmarg);
  202.  
  203.     len = stclen(cmarg);
  204.     spack('R',0,len,cmarg);                      /* Send an R packet */
  205.     SWREC                                        /* What was the reply? */
  206.     {
  207.  
  208. GOT(S)   rpar(recpkt);                           /* Get the other side's init data */
  209.          l = spar(sndpkt);                       /* Fill up packet with mine */
  210.          spack('Y',n,l,sndpkt);                  /* and ACK with my parameters  */
  211.          NNXT;
  212.          oldtry = numtry;                        /* Save old try count */
  213.          numtry = 0;                             /* Start a new counter */
  214.          NEXT(R_FILE);                           /* and enter Get-File state */
  215.  
  216. GOT(E)   closef();                               /* Clean up transfer file */
  217.          discard(newfilnam);                     /* and delete it */
  218.          prerrpkt(recpkt);                       /* Print error message */
  219.          STOP;
  220.  
  221. GOT(N)   STAY;                                   /* NAK from server, keep trying */
  222.  
  223. GOT(B)   if (num!=n) STOP;                       /* Need right packet number here */
  224.          ack();                                  /* Say OK */
  225.          STAY;                                   /* and keep trying */
  226.  
  227. NONE     nak();                                  /* Appease server to avoid error */
  228.          STAY;                                   /* and try again */
  229.  
  230. INTRPT   IERR;                                   /* CTRL-E interrupt */
  231.  
  232. OTHER    PERR;                                   /* Some other packet type */
  233.  
  234.     }
  235. }
  236.  
  237.  
  238. /* RINIT - Receive Initialization */
  239.  
  240. int rinit()
  241. {
  242.     int l;
  243.  
  244.     CHKTRY;                                      /* Check retries */
  245.     printf("Exchanging parameters\n");
  246.  
  247.     SWREC                                        /* Get a packet */
  248.     {
  249.  
  250. GOT(S)   rpar(recpkt);                           /* Get the other side's init data */
  251.          l = spar(sndpkt);                       /* Fill up packet with mine */
  252.          spack('Y',n,l,sndpkt);                  /* and ACK with my parameters */
  253.          oldtry = numtry;                        /* Save old try count */
  254.          numtry = 0;                             /* Start a new counter */
  255.          NNXT;
  256.          NEXT(R_FILE);                           /* and enter Receive-File state */
  257.  
  258. GOT(E)   closef();                               /* Clean up file */
  259.          discard(newfilnam);                     /* and delete it */
  260.          prerrpkt(recpkt);                       /* Print error message */
  261.          STOP;
  262.  
  263. NONE     nak();                                  /* Return a NAK */
  264.          STAY;                                   /* and keep trying */
  265.  
  266. INTRPT   IERR;                                   /* CTRL-E interrupt */
  267.  
  268. OTHER    PERR;                                   /* Some other packet type */
  269.  
  270.     }
  271. }
  272.  
  273.  
  274. /* RFILE - Receive File Header */
  275.  
  276. int rfile()
  277. {
  278.     int l;
  279.     char file[30];
  280.  
  281.     CHKTRY;                                      /* Check retries */
  282.  
  283.     SWREC                                        /* Get a packet */
  284.     {
  285.  
  286. GOT(S)   if (oldtry++>retry) RERR;               /* Check retries */
  287.          if (num==((n==0) ? 63 : n-1))           /* Previous packet, mod 64? */
  288.          {                                       /* Yes, ACK it again with  */
  289.               l = spar(sndpkt);                  /* our Send-Init parameters */
  290.               spack('Y',num,l,sndpkt);
  291.               numtry = 0;                        /* Reset try counter */
  292.               STAY;
  293.          }
  294.          else STOP;                              /* Not previous packet */
  295.  
  296. GOT(Z)   if (oldtry++>retry) RERR;               /* Check retries */
  297.          if (num==((n==0) ? 63 : n-1))           /* Previous packet, mod 64? */
  298.          {                                       /* Yes, ACK it again */
  299.               ackn(num);
  300.               numtry = 0;
  301.               STAY;
  302.          }
  303.          else STOP;                              /* Not previous packet */
  304.  
  305. GOT(F)   if (num!=n) STOP;                       /* Must have right packet number */
  306.  
  307.          if (strlen(cmarg2)==0) rtol(file,recpkt);
  308.          else strcpy(file,cmarg2);               /* Make new file name */
  309.  
  310.          if ((fp = devopen(file,destdev,O_WRONLY|O_CREAT|O_TRUNC))<0)
  311.          {
  312.               error("Cannot create %s for receiving",file);
  313.               QERR;
  314.          }
  315.  
  316.          printf("Receiving %s as %s\n",recpkt,newfilnam);
  317.          ack();                                  /* Acknowledge the file header */
  318.          oldtry = numtry;                        /* Reset try counters */
  319.          numtry = 0;
  320.          toscr = FALSE;                          /* Data into a file */
  321.          filein = 0;                             /* Clear byte counter */
  322.          NNXT;
  323.          NEXT(R_DATA);                           /* Switch to Data state */
  324.  
  325. GOT(X)   if (num!=n) STOP;                       /* Screen data is coming */
  326.          toscr = TRUE;                           /* Set data destination */
  327.          bufemp(recpkt,len);                     /* Write the data to screen */
  328.          ack();                                  /* Acknowlege it */
  329.          oldtry = numtry;                        /* Reset the try counters */
  330.          numtry = 0;
  331.          NNXT;
  332.          NEXT(R_DATA);                           /* and continue to receive data */
  333.  
  334. GOT(B)   if (num!=n) STOP;                       /* Need right packet number here */
  335.          ack();                                  /* Say OK */
  336.          printf("Received EOT, total %ld bytes\n",totin);
  337.          n = 0;                                  /* Reset packet counter */
  338.          DONE;                                   /* and finish transaction */
  339.  
  340. GOT(E)   closef();                               /* Clean up file */
  341.          discard(newfilnam);                     /* and delete it */
  342.          prerrpkt(recpkt);                       /* Print error message */
  343.          STOP;
  344.  
  345. NONE     nak();                                  /* Return a NAK */
  346.          STAY;                                   /* and keep trying */
  347.  
  348. INTRPT   IERR;                                   /* CTRL-E interrupt */
  349.  
  350. OTHER    PERR;                                   /* Some other packet type */
  351.  
  352.     }
  353. }
  354.  
  355.  
  356. /* RDATA - Receive Data */
  357.  
  358. int rdata()
  359. {
  360.     CHKTRY;                                      /* Check retries */
  361.  
  362.     SWREC                                        /* Get a packet */
  363.     {
  364.  
  365. GOT(D)   if (num!=n)                             /* Right packet number? */
  366.          {
  367.               if (oldtry++>retry) RERR;          /* Check retries */
  368.               if (num==((n==0) ? 63 : n-1))      /* Check packet number */
  369.               {                                  /* Previous packet again? */
  370.                    ackn(num);                    /* Yes, re-ACK it */
  371.                    numtry = 0;                   /* Reset try counter */
  372.                    STAY;                         /* Don't write out data! */
  373.               }
  374.               else STOP;                         /* Not previous packet */
  375.          }
  376.  
  377.          if (!bufemp(recpkt,len))                /* Write the data to file or screen */
  378.          {
  379.               error("Write failure");
  380.               QERR;
  381.          }
  382.  
  383.          if (cxseen) ack1("X");                  /* Check interrupt flags */
  384.          else if (czseen) ack1("Z");             /* and send an */
  385.          else ack();                             /* appropriate acknowlegement */
  386.  
  387.          oldtry = numtry;                        /* Reset the try counters */
  388.          numtry = 0;
  389.          NNXT;
  390.          STAY;
  391.  
  392. GOT(F)   if (oldtry++>retry) RERR;               /* Check retries */
  393.          if (num==((n==0) ? 63 : n-1))           /* Check packet number */
  394.          {
  395.               ackn(num);                         /* ACK previous one again */
  396.               numtry = 0;                        /* Reset try counter */
  397.               STAY;
  398.          }
  399.          else STOP;                              /* Not previous packet */
  400.  
  401. GOT(Z)   if (num!=n) STOP;                       /* Must have right packet number */
  402.          ack();                                  /* OK, ACK it */
  403.          printf("Received EOF, %ld bytes\n",filein);
  404.          totin += filein;
  405.          if (!toscr)
  406.          {
  407.               closef();                          /* Close the file */
  408.               if (*recpkt=='D')                  /* Check for 'discard file' */
  409.               {
  410.                    discard(newfilnam);
  411.                    printf("File discarded by %s interrupt\n",(cxseen || czseen) ? "local" : "remote");
  412.               }
  413.          }
  414.          toscr = FALSE;                          /* Reset disposition flag */
  415.          NNXT;
  416.          NEXT(R_FILE);                           /* and go back to Receive-File state */
  417.  
  418. GOT(E)   closef();                               /* Tidy up file */
  419.          discard(newfilnam);                     /* and delete it */
  420.          prerrpkt(recpkt);                       /* Print error message */
  421.          STOP;
  422.  
  423. NONE     nak();                                  /* Return a NAK */
  424.          STAY;                                   /* and keep trying */
  425.  
  426. INTRPT   IERR;                                   /* CTRL-E interrupt */
  427.  
  428. OTHER    PERR;                                   /* Some other packet type */
  429.  
  430.     }
  431. }
  432.  
  433.  
  434. /* SINIT - send this side's parameters and get other side's back */
  435.  
  436. int sinit()
  437. {
  438.     int l;
  439.  
  440.     CHKTRY;                                      /* Check retries */
  441.     printf("Exchanging parameters\n");
  442.  
  443.     l = spar(sndpkt);                            /* Fill up init info packet */
  444.     spack('S',n,l,sndpkt);                       /* Send an S packet */
  445.     SWREC                                        /* What was the reply? */
  446.     {
  447.  
  448. GOT(N)   STAY;                                   /* NAK, try it again */
  449.  
  450. GOT(Y)   if (n!=num) STAY;                       /* If wrong ACK, try again */
  451.          rpar(recpkt);                           /* Get other side's init info */
  452.          numtry = 0;                             /* Reset try counter */
  453.          NNXT;
  454.          NEXT(S_FILE);                           /* and switch state to Send-File */
  455.  
  456. GOT(E)   prerrpkt(recpkt);                       /* Print error message */
  457.          STOP;
  458.  
  459. NONE     STAY;                                   /* Receive failure, try again */
  460.  
  461. INTRPT   IERR;                                   /* CTRL-E interrupt */
  462.  
  463. OTHER    PERR;                                   /* Some other packet type */
  464.  
  465.     }
  466. }
  467.  
  468.  
  469. /* SFILE - Send File Header */
  470.  
  471. int sfile()
  472. {
  473.     char file[30];                               /* Name file is sent under */
  474.  
  475.     CHKTRY;                                      /* Check retries */
  476.     closef();                                    /* Ensure last file closed */
  477.  
  478.     if ((fp = devopen(cmarg,sourdev,O_RDONLY))<0)
  479.     {                                            /* Open transfer file */
  480.          error("Cannot open %s for sending",cmarg);
  481.          QERR;
  482.     }
  483.  
  484.     if (strlen(cmarg2)==0) ltor(file,newfilnam); /* No remote name, use local one */
  485.     else strcpy(file,cmarg2);                    /* Remote name given, use that */
  486.  
  487.     len = strlen(file);                          /* Get length of new filename */
  488.     printf("Sending %s as %s\n",newfilnam,file);
  489.  
  490.     warn8 = FALSE;                               /* Clear 8-bit warning flag, */
  491.     ackd = ' ';                                  /* remote interrupt flag */
  492.     cxseen = FALSE;                              /* and file interrupt flag */
  493.     fileout = 0;                                 /* Reset character count */
  494.  
  495.     spack('F',n,len,file);                       /* Send an F packet */
  496.  
  497.     SWREC                                        /* What was the reply? */
  498.     {
  499.  
  500. GOT(N)   num = (--num<0 ? 63 : num);             /* NAK, just stay in this state */
  501.          if (n!=num) STAY;                       /* unless it's NAK for next packet, */
  502.                                                  /* which is just like an ACK for */
  503.                                                  /* this packet so fall through */
  504.  
  505. GOT(Y)   if (n!=num) STAY;                       /* If wrong ACK, stay here */
  506.          numtry = 0;                             /* Reset try counter */
  507.          NNXT;
  508.          size = bufill(sndpkt);                  /* Get first data from file */
  509.          NEXT(S_DATA);                           /* and switch to sending state */
  510.  
  511. GOT(E)   prerrpkt(recpkt);                       /* Print error message */
  512.          STOP;
  513.  
  514. NONE     STAY;                                   /* Receive failure, try again */
  515.  
  516. INTRPT   IERR;                                   /* CTRL-E interrupt */
  517.  
  518. OTHER    PERR;                                   /* Some other packet type */
  519.  
  520.     }
  521. }
  522.  
  523.  
  524. /* SDATA - Send File Data */
  525.  
  526. int sdata()
  527. {
  528.     char r;                                      /* Temporary */
  529.  
  530.     CHKTRY;                                      /* Check retries */
  531.  
  532.     if (cxseen || czseen) NEXT(S_EOF);           /* Interrupt from this end? */
  533.  
  534.     spack('D',n,size,sndpkt);                    /* Send a D packet */
  535.     SWREC                                        /* What was the reply? */
  536.     {
  537.  
  538. GOT(N)   num = (--num<0 ? 63 : num);             /* NAK, just stay in this state */
  539.          if (n!=num) STAY;                       /* unless it's NAK for next packet */
  540.                                                  /* which is just like an ACK for */
  541.                                                  /* this packet so fall through */
  542.  
  543. GOT(Y)   if (n!=num) STAY;                       /* If wrong ACK, try again */
  544.          numtry = 0;                             /* Reset try counter */
  545.          NNXT;
  546.  
  547.          if ((r = recpkt[0])=='X' || r=='Z')     /* Interrupt from remote end? */
  548.          {
  549.               ackd = r;                          /* Yes, save interrupt type */
  550.               NEXT(S_EOF);                       /* and go to send Z/D */
  551.          }
  552.  
  553.          size = bufill(sndpkt);                  /* Get data from file */
  554.          if (size==EOF) NEXT(S_EOF);             /* If EOF set state to that */
  555.          else STAY;                              /* Got data, stay in send state */
  556.  
  557. GOT(E)   prerrpkt(recpkt);                       /* Print error message */
  558.          STOP;
  559.  
  560. NONE     STAY;                                   /* Receive failure, try again */
  561.  
  562. INTRPT   IERR;                                   /* CTRL-E interrupt */
  563.  
  564. OTHER    PERR;                                   /* Some other packet type */
  565.  
  566.     }
  567. }
  568.  
  569.  
  570. /* SEOF - Send End-Of-File */
  571.  
  572. int seof()
  573. {
  574.     CHKTRY;                                      /* Check retries */
  575.  
  576.     if (cxseen || czseen || ackd=='X' || ackd=='Z')
  577.          printf("Sending discard (%s interrupt)\n",(cxseen || czseen) ? "local" : "remote");
  578.     else printf("Sending EOF, %ld bytes\n",fileout);
  579.     totout += fileout;
  580.  
  581.     if (debon) printf("Closing input file\n");
  582.     closef();                                    /* Close the input file */
  583.  
  584.     if (cxseen || czseen || ackd=='X' || ackd=='Z') spack('Z',n,1,"D");
  585.     else spack('Z',n,0,0);                       /* Send appropriate EOF */
  586.  
  587.     SWREC                                        /* What was the reply? */
  588.     {
  589.  
  590. GOT(N)   num = (--num<0 ? 63 : num);             /* NAK, just stay in this state */
  591.          if (n!=num) STAY;                       /* unless it's NAK for next packet, */
  592.                                                  /* which is just like an ACK for */
  593.                                                  /* this packet so fall through */
  594.  
  595. GOT(Y)   if (n!=num) STAY;                       /* If wrong ACK, try again */
  596.          numtry = 0;                             /* Reset try counter */
  597.          NNXT;
  598.          NEXT(S_BRK);                            /* then finish transaction */
  599.  
  600. GOT(E)   prerrpkt(recpkt);                       /* Print error message */
  601.          STOP;
  602.  
  603. NONE     STAY;                                   /* Receive failure, try again */
  604.  
  605. INTRPT   IERR;                                   /* CTRL-E interrupt */
  606.  
  607. OTHER    PERR;                                   /* Some other packet type */
  608.  
  609.     }
  610. }
  611.  
  612.  
  613. /* SBREAK - Send Break (end of transaction) */
  614.  
  615. int sbreak()
  616. {
  617.     CHKTRY;                                      /* Check retries */
  618.     printf("Sending EOT, total %ld bytes\n",totout);
  619.  
  620.     spack('B',n,0,0);                            /* Send a B packet */
  621.     SWREC                                        /* What was the reply? */
  622.     {
  623.  
  624. GOT(N)   num = (--num<0 ? 63 : num);             /* NAK, just stay in this state */
  625.          if (n!=num) STAY;                       /* unless it's NAK for next packet, */
  626.                                                  /* which is just like an ACK for */
  627.                                                  /* this packet so fall through */
  628.  
  629. GOT(Y)   if (n!=num) STAY;                       /* If wrong ACK, try again */
  630.          numtry = 0;                             /* Reset try counter */
  631.          NNXT;
  632.          DONE;                                   /* Transaction complete */
  633.  
  634. GOT(E)   prerrpkt(recpkt);                       /* Print error message */
  635.          STOP;
  636.  
  637. NONE     STAY;                                   /* Receive failure, try again */
  638.  
  639. INTRPT   IERR;                                   /* CTRL-E interrupt */
  640.  
  641. OTHER    PERR;                                   /* Some other packet type */
  642.  
  643.     }
  644. }
  645.  
  646.  
  647. /* DEBUGST - Log the state & expected packet number */
  648.  
  649. debugst()
  650. {
  651.     printf("State %s, packet %d\n",stname(state),n);
  652. }
  653.  
  654.  
  655. /* PROTERR - An unexpected packet type received */
  656.  
  657. int proterr()
  658. {
  659.     printf("PROTOCOL ERROR: %c packet in state %s\n",pt,stname(state));
  660.     strcpy(errdata,"Protocol error");
  661.     return(K_ERR);
  662. }
  663.