home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume7 / rvi / part3 / rv_sync.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-04-09  |  2.1 KB  |  109 lines

  1. #include "rv.h"
  2.  
  3. /*
  4.  * Synchronize communication by sending a command
  5.  * with a recognizable response.
  6.  */
  7.  
  8. void
  9. xmit_sync()
  10. /*
  11.  * Transmit sync
  12.  *
  13.  * A sync consists of transmitting two f commands followed
  14.  * by an illegal command, and waiting to receive the file name echoed
  15.  * twice followed by a question mark.
  16.  */
  17. {
  18.     if (fputs("f\nf\nzzz\n", file.fi_fpout) == NULL)
  19.         panic("Error while writing to ed\n\n");
  20. }
  21.  
  22. boolean
  23. recv_sync(disp_flag)
  24. /*
  25.  * Wait for sync to come back
  26.  *
  27.  * Returns TRUE if no errors received prior to sync, else
  28.  *     displays errors if disp_flag is TRUE.
  29.  */
  30. boolean disp_flag;
  31. {
  32.     register char buf[512];
  33.     register INT i, namematch = 0, errcount = 0;
  34.     INT old_alarm;
  35.     extern alarmring();
  36.  
  37.     old_alarm = alarm(RV_TIMEOUT);
  38.  
  39.     fflush(file.fi_fpout);
  40.     for (;;) {
  41.         if (fgets(buf, 511, file.fi_fpin) == NULL)
  42.             panic("Error while reading from ed\n\n");
  43.         /*
  44.          * Strip trailing \n
  45.          */
  46.         if ((i = strlen(buf)) > 0 && buf[i-1] == '\n')
  47.             buf[--i] = '\0';
  48.         if (buf[0] == '?') {
  49.             if (namematch > 1)
  50.                 goto found;
  51.             /*
  52.              * Display error
  53.              */
  54.             ++errcount;
  55.             if (file.fi_sysv) {
  56.                 if (buf[1] != ' ' ||
  57.                         file.fi_cray == FALSE)
  58.                     fgets(buf, 511, file.fi_fpin);
  59.                 if (disp_flag)
  60.                     botprint(TRUE, "%s", buf);
  61.             }
  62.             else
  63.                 if (disp_flag)
  64.                     botprint(TRUE, "Failed ");
  65.             namematch = 0;
  66.             continue;
  67.         }
  68.         /*
  69.          * Look for file name echoed twice
  70.          */
  71.         if (strcmp(buf, file.fi_name) == 0) 
  72.             ++namematch;
  73.         else
  74.             namematch = 0;
  75.         if (namematch >= 2) {
  76.             (void) fgets(buf, 511, file.fi_fpin);
  77.             if ((i = strlen(buf)) > 0 && buf[i-1] == '\n')
  78.                 buf[--i] = '\0';
  79.             /*
  80.              * Look for question mark
  81.              */
  82.             if (buf[0] == '?') {
  83.         found:
  84.                 if (file.fi_sysv &&
  85.                     (buf[1] != ' ' || file.fi_cray == FALSE))
  86.                     /*
  87.                      * Skip error msg
  88.                      */
  89.                     fgets(buf, 511, file.fi_fpin);
  90.                 break;
  91.             }
  92.             else {
  93.                 if (disp_flag > 1)
  94.                     botprint(FALSE, "%s", file.fi_name);
  95.                 if (strcmp(buf, file.fi_name)) {
  96.                     if (disp_flag > 1)
  97.                         botprint(FALSE, "%s", file.fi_name);
  98.                     namematch = 0;
  99.                 }
  100.             }
  101.         }
  102.         if (disp_flag > 1 && namematch == 0)
  103.             botprint(FALSE, "%s", buf);
  104.     }
  105.     alarm(old_alarm);
  106.  
  107.     return (errcount == 0);
  108. }
  109.