home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume4 / xmodem / to_vax.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  3.6 KB  |  177 lines

  1. #
  2. # include <file.h>
  3. # include <curses.h>
  4. # include <signal.h>
  5.  
  6. # define    ST_BLK    33
  7. # define    READY    34
  8. # define    OKAY    35
  9. # define    NOT_OK    36
  10. # define    CANCEL    37
  11. # define    CR    13
  12. # define    ctoi(c)    (c - '0' > 9 ? c - '0' - 7 : c - '0')
  13.  
  14. char filename[64];
  15. int old;
  16. main(argc,argv)
  17. int argc;
  18. char *argv[]; {
  19.  
  20.     int fd;
  21.     register i;
  22.     register char *ptr, *op;
  23.     char buf[512], seq[4];
  24.     char *index();
  25.     int xsum, lim, old_seq, pid;
  26.  
  27.     if (!((argc == 2) || (argc == 3))) {
  28.         printf("ILLEGAL NUMBER OF ARGUMENTS.\n");
  29.         printf("SYNOPSIS: to_vax [-t] filename\n");
  30.         exit(-1);
  31.     }
  32.  
  33.     if (argc == 3) {
  34.         if (strcmp(argv[1],"-t")) {
  35.             printf("bad argument `%s'\n",argv[1]);
  36.             printf("SYNOPSIS: to_vax [-t] filename\n");
  37.             exit(-1);
  38.         }
  39.     }
  40.  
  41.     strcpy(filename,argv[argc-1]);
  42.     if ((fd = open(filename,O_WRONLY|O_CREAT|O_TRUNC,0644)) < 0) {
  43.         perror(filename);
  44.         exit(-1);
  45.     }
  46.  
  47.     initscr();
  48.     crmode();        /* set terminal to unbuffered I/O */
  49.  
  50.     printf("\nPress Alt-F7 to transmit file to VAX.\n");
  51.  
  52.  
  53. /*
  54. **    Ignore interrupts from the user.
  55. **    If the user could delete this program the terminal
  56. **    would be left in a undiserable state of mind.
  57. */
  58.     sigsetmask( -1 );    /* ignore all signals */
  59.  
  60.     if ((pid = fork()) == 0) {
  61.         sleep(100);
  62.         RESET(fd);
  63.         kill(0, SIGKILL);     /* Can't wait forever for a READY reply */
  64.     }
  65.  
  66.     ptr = buf;
  67.     while (1) {              /* wait for ready character */
  68.         read(0,ptr,1);
  69.         if (*ptr == READY) break;
  70.     }
  71.     kill(pid, SIGKILL);
  72.     wait(0);
  73.     read(0,ptr,1);            /* get the CR from the READY */
  74.     WCHAR(OKAY); WCHAR(CR);
  75.  
  76.     for(;;) {
  77. input:
  78.         if ((pid = fork()) == 0) {
  79.             i = 0;
  80.             while ( i++ < 10 ) {
  81.                 sleep(10);
  82.                 WCHAR(NOT_OK); WCHAR(CR);
  83.             }
  84.             sleep(10);
  85.             RESET(fd);
  86.             kill(0, SIGKILL);      /* kill all processes */
  87.         }
  88.         READ(buf);            /* read data block */
  89.         kill( pid, SIGKILL );          /* kill child */
  90.         wait(0);
  91.  
  92.         /* check for 90 character block */
  93.         ptr = index(buf,ST_BLK);
  94.         if (strlen(ptr) != 90) {
  95.             WCHAR(NOT_OK); WCHAR(CR);
  96.             goto input;
  97.         }
  98.  
  99.         xsum = 0;
  100.         while ( ptr < &buf[86] )  /* compute check sum */
  101.             xsum = (xsum ^ *ptr++) & 0177;
  102.         if ( xsum != atoi(ptr) ) {     /* check sum not ok */
  103.             WCHAR(NOT_OK); WCHAR(CR);
  104.             goto input;
  105.         }
  106.  
  107.         ptr = index(buf,ST_BLK) + 1;
  108.         strncpy(seq,ptr,3);
  109.         switch( (lim = atoi(seq)) - old_seq ) {
  110.             case 0:            /* reply garbled, resend reply */
  111.                 WCHAR(OKAY); WSTR(seq); WCHAR(OKAY); WCHAR(CR);
  112.                 goto input;
  113.             case 1: case -999:      /* sequence number ok */
  114.                 old_seq = lim;
  115.                 break;
  116.             default:             /* fatal loss of synchronization */
  117.                 WCHAR(CANCEL); WCHAR(CANCEL); WCHAR(CANCEL); WCHAR(CR);
  118.                 RESET(fd);
  119.                 exit(-1);
  120.         }
  121.         
  122.         ptr += 3;
  123.         if (!(strncmp(ptr,"XX",2) && strncmp(ptr,"EE",2))) break;
  124.  
  125.         if (strncmp(ptr,"HH",2)) {      /* decode data block */
  126.             lim = ctoi(*ptr) * 10 + ctoi(*(ptr+1));
  127.             op = buf;
  128.             for ( i = 2; i < lim + 2; i += 2 )
  129.                 *op++ = ctoi( *(ptr+i) ) * 16 + ctoi( *(ptr+i+1) );
  130.             write(fd, buf, lim/2);     /* write data block to file */
  131.         }
  132.  
  133.         WCHAR(OKAY); WSTR(seq); WCHAR(OKAY); WCHAR(CR);
  134.     } /* end while */
  135.  
  136.     WCHAR(OKAY); WSTR(seq); WCHAR(OKAY); WCHAR(CR);
  137.     nocrmode();
  138.     endwin();
  139.     close(fd);
  140.     if (argc == 3) {
  141.         printf("Deleting carriage returns\n");
  142.         DELCR(filename);
  143.     }
  144. } /* end main */
  145.  
  146. WCHAR(c)
  147. char c; {   /* write a character to standard output */
  148.  
  149.     write(1, &c, 1);
  150. }
  151.  
  152. WSTR(s)
  153. char *s; {   /* write a string to standard output */
  154.     
  155.     write(1, s, strlen(s));
  156. }
  157.  
  158. READ(s) 
  159. char *s; {
  160.     register char *ptr;
  161.  
  162.     ptr = s;
  163.     while ( read(0, ptr, 1) > 0 ) {       /* read characters until CR */
  164.         if (*ptr++ == CR) break;
  165.     }
  166.     *ptr = '\0';
  167. }
  168.  
  169. RESET(fd) 
  170. int fd; {            /* put terminal back into start up state */
  171.  
  172.     nocrmode();
  173.     endwin();
  174.     close(fd);
  175.     unlink(filename);
  176. }
  177.