home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 307.lha / Getty_UUCico_v1.00_Beta / uuserdump.c < prev    next >
Encoding:
C/C++ Source or Header  |  1980-12-05  |  3.7 KB  |  146 lines

  1.  
  2. /*
  3.  *  UUSERDUMP.C
  4.  *
  5.  *  UUSERDUMP filename
  6.  *
  7.  *  KEY THINGS ABOUT UUSER: USED AS STDIN/STDOUT
  8.  *
  9.  *    Currently UUSER: works only with single sessions started
  10.  *    by Getty (this has to do with how UUSER: deals with carrier
  11.  *    detect).  THIS WILL BE FIXED.
  12.  *
  13.  *    (1) Use Level 1 I/O if you can (read/write)
  14.  *        read() returns 0 on timeout, -1 on carrier lost
  15.  *        otherwise, read() returns the immediate number of
  16.  *        data bytes ready up to the amount you requested.
  17.  *        (i.e. if you read(0,buf,256) you can get anywhere from
  18.  *        -1, 0, 1 to 256 back).
  19.  8
  20.  *        write() returns the number you wrote or -1 (lost carrier)
  21.  *
  22.  *        To 'poll' data ready you can read(0, NULL, 0) .. reading
  23.  *        0 bytes returns 0 (data ready) or -1 (data not ready).
  24.  *        NOTE: 0 (data ready) will be returned if carrier is lost
  25.  *        when you read 0 bytes... this is so your program thinks
  26.  *        data is ready and when it does a real read it finds that
  27.  *        carrier was lost!
  28.  *
  29.  *    (2) If you want to use Level 2 I/O (stdio) I suggest you use
  30.  *        it for writing only.  If you really want to use it for
  31.  *        reading remember that the timeout will cause an EOF
  32.  *        condition which must be cleared (clrerr()).  And you
  33.  *        must call ferror() to determine whether an EOF is an
  34.  *        EOF (timeout()) or an actual error (lost carrier).
  35.  */
  36.  
  37. #include <proto/all.h>
  38. #include <stdio.h>
  39.  
  40. void
  41. CXBRK()             /*  on BREAK    */
  42. {
  43.     exit(1);
  44. }
  45.  
  46. char buf[256];
  47. short i;
  48.  
  49. void
  50. main(ac, av)
  51. char *av[];
  52. {
  53.     short i;
  54.     char c;
  55.     short n;
  56.     FILE *fi = fopen(av[1], "r");
  57.  
  58.     /*
  59.      *    set output to line buffering (stdio), else it will use
  60.      *    file buffering!  Which is bad if we want to catch ^S/^C
  61.      */
  62.  
  63.     setvbuf(stdout, NULL, _IOLBF, 0);
  64.  
  65.  
  66.     if (ac == 1)
  67.     exit(1);
  68.  
  69.     if (fi == NULL)
  70.     printf("Info file %s not available\r\n", av[1]);
  71.  
  72.     printf("Dumping info file (suggest capture), hit Enter when ready\n");
  73.     fflush(stdout);
  74.  
  75.     /*
  76.      *    Getty uses the R1000 option, so there is a one second timeout
  77.      *    on reads when no data is present.  If data is present, UUSER:
  78.      *    returns the number of bytes immediately available regardless of
  79.      *    how many you requested.
  80.      */
  81.  
  82.     for (i = 0; i < 60; ++i) {
  83.     n = read(0, &c, 1);
  84.     if (n < 0)              /*  lost carrier */
  85.         exit(1);
  86.                 /*  success    */
  87.     if (n > 0 && (c == 10 || c == 13))
  88.         goto skip;
  89.     /* timeout */
  90.     }
  91.     exit(1);
  92. skip:
  93.     while (fgets(buf, 256, fi)) {
  94.     if (CheckControl())
  95.         break;
  96.     fputs(buf, stdout);
  97.     }
  98.     fclose(fi);
  99.  
  100.     printf("End of dump, any key to disconnect\n");
  101.     fflush(stdout);
  102.     for (i = 0; i < 60; ++i) {
  103.     n = read(0, &c, 1);
  104.     if (n == 0)     /*  1 second timeout    */
  105.         continue;
  106.     break;        /*  any key or lost cd    */
  107.     }
  108. }
  109.  
  110. /*
  111.  *  Here we use a UUSER: trick ... requesting 0 bytes in a read()
  112.  *  incurs NO timeout.    0 is returned if data is ready, -1 if data
  113.  *  is not ready.
  114.  *
  115.  *  Another way to do it is to dump a seconds worth of data, then
  116.  *  call read() (which blocks for up to 1 second if no data is
  117.  *  ready, but the user doesn't see that because we've already queued
  118.  *  a second's worth of data).  The first method is better because
  119.  *  there is finer granularity (that is, the one used below)
  120.  */
  121.  
  122. CheckControl()
  123. {
  124.     char c = 0;
  125.     short n;
  126.  
  127.     if (read(0, NULL, 0) == 0) {    /*  returns 0=data ready, -1=not rdy */
  128.     read(0, &c, 1);
  129.     if (c == ('s'&0x1F)) {      /*  ^S           */
  130.         for (;;) {
  131.         n = read(0, &c, 1);
  132.         if (n < 0)          /*  lost carrier */
  133.             exit(1);
  134.         if (n == 0)         /*  timeout      */
  135.             continue;
  136.         break;            /*    got a key    */
  137.         }
  138.         return(0);
  139.     }
  140.     if (c == ('c'&0x1F))        /*  ^C           */
  141.         return(1);
  142.     }
  143.     return(0);
  144. }
  145.  
  146.