home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / wps / com / zocdev / tap / tap2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-21  |  5.9 KB  |  200 lines

  1. /***********************************************************************
  2. *                                                                      *
  3. *   A tap listener is an independent applications that  opens a queue  *
  4. *   to receive information about file operations occuring in file      *
  5. *   transfers (e.g. via ZMODEM).                                       *
  6. *   These informations can be used for add-in applications like        *
  7. *   auto-unzippers or online viewers.                                  *
  8. *                                                                      *
  9. *   ------------------------------------------------------------------ *
  10. *                                                                      *
  11. *   This listener just waits for download opens (write) and dumps      *
  12. *   all bytes to stdout. The main loop is (unlike TAP.C) not driven    *
  13. *   driven by queue events, but it is driven by requests for data      *
  14. *   (very much like reading from a file).                              *
  15. *                                                                      *
  16. *   For a detailed description of the events take a closer look to     *
  17. *   TAP.C                                                              *
  18. *                                                                      *
  19. *   ------------------------------------------------------------------ *
  20. *                                                                      *
  21. *   Manufacturing: ICC -Ss TAP2.C                                      *
  22. *                                                                      *
  23. *   ------------------------------------------------------------------ *
  24. *                                                                      *
  25. *   No copyright by Markus Schmidt, 1993                               *
  26. *                                                                      *
  27. ***********************************************************************/
  28.  
  29. #define INCL_DOSFILEMGR
  30. #define INCL_DOSQUEUES
  31. #define INCL_DOSMEMMGR
  32. #define INCL_NOCOMMON
  33.  
  34. #include <os2.h>
  35. #include <stdio.h>
  36.  
  37. #include "tap.h"
  38.  
  39.  
  40. // define events for QueueManager function
  41. enum _QUEUE_OPS {
  42.     WAITFOR,
  43.     GETNEXTBYTE
  44. };
  45.  
  46.  
  47. // function prototypes
  48. static void ReadQueue(HQUEUE hq, VOID **ppdata, ULONG *plength, int *px);
  49. static int QueueManager(HQUEUE hq, int op, int op2);
  50.  
  51.  
  52. /***********************************************************************
  53. *   Open a queue, wait for an open and then request bytes until EOF    *
  54. *                                                                      *
  55. *                                                                      *
  56. ***********************************************************************/
  57. int main()
  58. {
  59.     int error;
  60.     int i;
  61.     char szQueName[]= TAP_QUEUE_NAME;
  62.     HQUEUE hq= 0;
  63.  
  64.  
  65.     // Find a free name and open a data queue to receive info
  66.     for (i= 0; i<TAP_MAX && hq==0; i++) {
  67.         DosCreateQueue(&hq, QUE_FIFO, szQueName);
  68.         szQueName[strlen(szQueName)-1]++;
  69.     }
  70.  
  71.     // queue open?
  72.     if (hq) {
  73.         for (;;) {
  74.             printf("\nWait for open ...\n");
  75.             error= QueueManager(hq, WAITFOR, TAP_EVENT_OPENFORWRITE);
  76.             if (!error) {
  77.                 int byte;
  78.  
  79.                 do {
  80.                     byte= QueueManager(hq, GETNEXTBYTE, 0);
  81.                     printf("%02X  ", byte);
  82.                 } while (byte!=EOF);
  83.             }
  84.         }
  85.     }
  86.  
  87.     // Destroy queue
  88.     DosCloseQueue(hq);
  89.  
  90.     // terminate
  91.     return (0);
  92. }
  93.  
  94.  
  95.  
  96. /***********************************************************************
  97. *   Wait for a specific tap event or return next byte/EOF              *
  98. *                                                                      *
  99. *                                                                      *
  100. ***********************************************************************/
  101. int
  102. QueueManager(HQUEUE hq, int op, int op2)
  103. {
  104.     static unsigned char *data= NULL;
  105.     static int datalength= 0, dataindex=0;
  106.     ULONG length;
  107.     int code, rc=0;
  108.         
  109.         
  110.     // wait for something
  111.     if (op==WAITFOR) {
  112.         // discard old data if waiting for something
  113.         datalength= 0;
  114.         dataindex= 0;
  115.         if (data) {
  116.             DosFreeMem(data);
  117.             data=NULL;
  118.         }
  119.  
  120.         // wait for it
  121.         do {
  122.             ReadQueue(hq, (VOID**)&data, &length, &code);
  123.             if (code!=op2 && data!=NULL) {
  124.                 DosFreeMem(data);
  125.                 data= NULL;
  126.             }
  127.         } while (code!=op2);
  128.     }
  129.     else 
  130.     // need input
  131.     if (op==GETNEXTBYTE) {
  132.         // if no data there, wait for a significant event
  133.         if (data==NULL || dataindex>=datalength) {
  134.             // maybe a somehow forgotten block
  135.             if (data!=NULL) {
  136.                 DosFreeMem(data);
  137.                 data= NULL;
  138.                 datalength= 0;
  139.                 dataindex= 0;
  140.             }
  141.  
  142.             // wait for data or close
  143.             for (;;) {
  144.  
  145.                 ReadQueue(hq, (VOID**)&data, &length, &code);
  146.  
  147.                 if (code==TAP_EVENT_CLOSE ||
  148.                     code==TAP_EVENT_SEEK) {    // take a seek for CLOSE
  149.                     break;
  150.                 }
  151.                 else 
  152.                 if (code==TAP_EVENT_WRITEBLOCK) {
  153.                     datalength= length;
  154.                     dataindex= 0;
  155.                     break;
  156.                 }
  157.  
  158.                 if (data!=NULL) {
  159.                     DosFreeMem(data);
  160.                     data= NULL;
  161.                 }
  162.             } 
  163.         }
  164.  
  165.         if (data==NULL) {
  166.             rc= EOF; // must have been CLOSE or seek
  167.         }
  168.         else {
  169.             rc= data[dataindex++];    // must have been WRITE
  170.         }
  171.     }
  172.  
  173.     return (rc);
  174. }
  175.  
  176.  
  177. /***********************************************************************
  178. *   Read a tap event from the queue                                    *
  179. *                                                                      *
  180. *                                                                      *
  181. ***********************************************************************/
  182. void
  183. ReadQueue(HQUEUE hq, VOID **ppdata, ULONG *plength, int *px)
  184. {
  185.     REQUESTDATA rqd;
  186.     BYTE prio;
  187.  
  188.     DosReadQueue(hq, 
  189.             &rqd,        // ulData-field for eventcode
  190.             plength,    // field for data length or offset
  191.             ppdata,        // field for data pointer
  192.             0,             // get first element from queue
  193.             0,            // wait synchronously for data
  194.             &prio,        // element priority
  195.             0);            // no semaphore
  196.  
  197.     *px= rqd.ulData;
  198.     return;
  199. }
  200.