home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / software / unix / saoimage / sao1_07.tar / ctrlmbox.c < prev    next >
C/C++ Source or Header  |  1990-05-02  |  5KB  |  185 lines

  1. #ifndef lint
  2. static char SccsId[] = "%W%  %G%";
  3. #endif
  4.  
  5. /* Module:    ctrlmbox.c (ControlMailbox)
  6.  * Purpose:    Handle a VMS mailbox for IO
  7.  * Subroutine:    open_mailbox()        returns: int
  8.  * Subroutine:    close_mailbox()        returns: void
  9.  * Subroutine:    flush_mailbox()        returns: void
  10.  * Copyright:    1989, 1990 Smithsonian Astrophysical Observatory
  11.  *        You may do anything you like with this file except remove
  12.  *        this copyright.  The Smithsonian Astrophysical Observatory
  13.  *        makes no representations about the suitability of this
  14.  *        software for any purpose.  It is provided "as is" without
  15.  *        express or implied warranty.
  16.  * Modified:    {0} Jay Travisano (STScI)    initial version   10 Nov 1989
  17.  *        {1} MVH simplified for new connect module    10 March 1990
  18.  *        {n} <who> -- <does what> -- <when>
  19.  */
  20.  
  21. #ifdef VMS
  22.     /*
  23.      * VMS -- Use VMS/IRAF Imtool driver (ZFIOVI)
  24.      */
  25.  
  26. #include <stdio.h>        /* define stderr, FILE, NULL, etc */
  27. #include <sys/file.h>        /* define open */
  28. #include <X11/Xlib.h>        /* X window stuff */
  29. #include "hfiles/control.h"    /* declare control structure types */
  30.  
  31. #define READ_ONLY  1        /* IRAF SPP definitions */
  32. #define READ_WRITE 2
  33. #define WRITE_ONLY 3
  34.  
  35. /*
  36.  * Subroutine:    open_mailbox
  37.  * Purpose:    Open a VMS mailbox I/O
  38.  * Returns:    Channel number on success else -1.
  39.  */
  40. int open_mailbox ( device_name, direction, flush_flag )
  41.      char *device_name;        /* i: name of mailbox device */
  42.      int direction;        /* i: IOP_Write, IOP_Read, IOP_ReadWrite */
  43.      int flush_flag;        /* i: 1=flush, 0=don't flush */
  44. {
  45.   int ipc;        /* o: channel of new pipe connection */
  46.   int mode;
  47.  
  48.   if( direction == IOP_Write )
  49.     mode = WRITE_ONLY;
  50.   else if( direction == IOP_Read )
  51.     mode = READ_ONLY;
  52.   else if( direction == IOP_ReadWrite )
  53.     mode = READ_WRITE;
  54.   else
  55.     return( -1 );
  56.   zopnvi(device_name, &mode, &ipc);
  57.   if( ipc == -1 ) {
  58.     (void)fprintf(stderr,"Warning: cannot open %s\n", device_name);
  59.     return( -1 );
  60.   }
  61.  
  62. #ifdef VMS_FLUSH_INPUT 
  63.   /* Don't think we want to do this.  Mailboxes are pretty well-behaved,
  64.    * and we should be able to pick up pending input if we are (re)started
  65.    * after IRAF has sent data.
  66.    */
  67.   if( flush_flag && (mode != WRITE_ONLY) )
  68.     /* if reading, flush old input, if any */
  69.     flush_mailbox(ipc, 0, device_name);
  70. #endif
  71.  
  72.   return( ipc );
  73. }
  74.  
  75. /*
  76.  * Subroutine:    close_mailbox
  77.  * Purpose:    Close a mailbox connection
  78.  * Returns:    0 on success else -1.
  79.  */
  80. int close_mailbox ( ipc, device_name )
  81.      int ipc;            /* i: mailbox channel number */
  82.      char *device_name;        /* i: name of mailbox device */
  83. {
  84.   int chan, status;
  85.  
  86.   chan = ipc;
  87.   zclsvi(&chan, &status);
  88.   if( status == -1 ) {
  89.     (void)fprintf(stderr,"Warning: cannot close mailbox %s\n", device_name);
  90.     return( -1 );
  91.   } else {
  92.     return( 0 );
  93.   }
  94. }
  95.  
  96. /*
  97.  * Subroutine:    flush_mailbox
  98.  * Purpose:    Suck all bytes out of a pipe open for reading
  99.  */
  100. void flush_mailbox ( ipc, device_name )
  101.      int  ipc;            /* i: mailbox channel number */
  102.      char *device_name;        /* [i]: name of mailbox device */
  103. {
  104.   int bytes, total;
  105.   char buf[8192];
  106.   int read_mailbox();
  107.  
  108.   total = 0;
  109.   while( ZPending(ipc) ) {
  110.     bytes = read_mailbox(ipc, buf, sizeof(buf), 0, device_name, "junk");
  111.     total += bytes;
  112.   }
  113. #ifdef DEBUG
  114.   if( total > 0 )
  115.     (void)fprintf(stderr, "Flushed %d bytes from pipe\n", total);
  116. #endif
  117. }
  118.  
  119. /*
  120.  * Subroutine:    read_mailbox
  121.  * Purpose:    Read VMS mailbox
  122.  * Returns:    Byte count, 0 on EOF, -1 on error
  123.  */
  124. int read_mailbox ( ipc, buf, bytes, report_error, device_name, detail )
  125.      int  ipc;            /* i: mailbox channel number */
  126.      char *buf;            /* i: address of buffer to receive data */
  127.      int  bytes;        /* i: maximum number of bytes to read */
  128.      int  report_error;        /* i: report under-count read */
  129.      char *device_name;        /* [i]: name of mailbox device */
  130.      char *detail;        /* [i]: details of operation */
  131. {
  132.   int    chan = ipc;
  133.   int    maxbytes = bytes;
  134.   int    offset = 0;
  135.   int    status;
  136.  
  137.   zardvi(&chan, buf, &maxbytes, &offset);
  138.   zawtvi(&chan, &status);
  139.  
  140.   if( report_error ) {
  141.     if( status <= 0 ) {
  142.       (void)fprintf(stderr, "Error in reading");
  143.       if( detail != NULL )
  144.     (void)fprintf(stderr, " %s", detail);
  145.       if( device_name != NULL )
  146.     (void)fprintf(stderr, " from %s", device_name);
  147.       (void)fprintf(stderr, "\n");
  148.       (void)fflush(stderr);
  149.     } else {
  150.       if (status < maxbytes) {
  151.     (void)fprintf(stderr, "Expected %d bytes, read %d\n", 
  152.               maxbytes, status);
  153.     (void)fflush(stderr);
  154.       }
  155.     }
  156.   }
  157.   return( status );
  158. }
  159.  
  160. /*
  161.  * Subroutine:    write_mailbox
  162.  * Purpose:    Write to VMS mailbox
  163.  * Returns:    Byte count, 0 on EOF, -1 on error
  164.  */
  165. int write_mailbox ( ipc, buf, bytes, device_name )
  166.      int  ipc;            /* i: mailbox channel number */
  167.      char *buf;            /* i: address of buffer to write */
  168.      int  bytes;        /* i: number of bytes to write */
  169.      char *device_name;        /* i: name of mailbox device */
  170. {
  171.   int   chan = ipc;
  172.   int    nbytes = bytes;
  173.   int    offset = 0;
  174.   int    status;
  175.  
  176.   zawrvi(&chan, buf, &nbytes, &offset);
  177.   zawtvi(&chan, &status);
  178.  
  179.   if( status < bytes )
  180.     (void)fprintf(stderr, "Write error on %s; wrote %d of %d bytes\n",
  181.           device_name, status, bytes);
  182.   return( status );
  183. }
  184. #endif
  185.