home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cdisk.zip / DEVMON / DEVMON16.C next >
C/C++ Source or Header  |  1992-12-11  |  6KB  |  249 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define INCL_BASE
  6. #define    INCL_DOS
  7. #define INCL_DOSDEVIOCTL
  8. #define INCL_DOSMONITORS
  9. #include <os2.h>
  10.  
  11. typedef SHORT HFILE16;
  12. typedef HFILE16 *PHFILE16;
  13.  
  14. #define    PLACE_DONT_CARE        0
  15. #define    PLACE_BEGIN_CHAIN    1
  16. #define    PLACE_END_CHAIN        2
  17.  
  18. #define    PARALLEL_DATA_CHAIN    1
  19. #define    PARALLEL_CODE_CHAIN    2
  20.  
  21. #define    WAIT_ENABLE            0
  22. #define    WAIT_DISABLE        1
  23.  
  24. #define    MONFLAG_JOB_TITLE    0x2000
  25. #define    MONFLAG_RESERVED    0x1000
  26. #define    MONFLAG_STATUS        0x0800
  27. #define    MONFLAG_CODE_PAGE    0x0400
  28. #define    MONFLAG_BUFCMD        0x0200
  29. #define    MONFLAG_FONT        0x0100
  30. #define    MONFLAG_FLUSH        0x0004
  31. #define    MONFLAG_CLOSE        0x0002
  32. #define    MONFLAG_OPEN        0x0001
  33.  
  34. typedef struct
  35. {
  36.     USHORT    usMonitorFlag;
  37.     USHORT    usProcessId;
  38.     UCHAR    uchMonitorData[4096];
  39. } MONITORPACKET, *PMONITORPACKET;
  40.  
  41. UCHAR    szPipeFileName[128];
  42. UCHAR    szPrintFileName[128];
  43. UCHAR    szSpoolFileName[128];
  44. UCHAR    uchChainInput[4096];
  45. UCHAR    uchChainOutput[4096];
  46. UCHAR    uchSpoolReply[128];
  47. ULONG    ulSpoolFileId;
  48. ULONG    ulVerbose;
  49.  
  50. MONITORPACKET MonitorPacket;
  51.  
  52. APIRET16 APIENTRY16 Dos16MonOpen(PSZ,PHFILE16);
  53. APIRET16 APIENTRY16 Dos16MonClose(HFILE16);
  54. APIRET16 APIENTRY16 Dos16MonReg(HFILE16,PUCHAR,PUCHAR,USHORT,USHORT);
  55. APIRET16 APIENTRY16 Dos16MonRead(PUCHAR,USHORT,PMONITORPACKET,PUSHORT);
  56. APIRET16 APIENTRY16 Dos16MonWrite(PUCHAR,PMONITORPACKET,PUSHORT);
  57.  
  58. main(int argc,char **argv)
  59. {
  60.     HFILE        hSpool;
  61.     HFILE16        hMonitor16;
  62.     PUCHAR        pMonitorData;
  63.     APIRET        rcStatus;
  64.     APIRET16    rcStatus16;
  65.     ULONG        ulAction;
  66.     ULONG        ulTemp;
  67.     USHORT        usBytesRead;
  68.  
  69.     /* process command line arguments */
  70.  
  71.     for (ulTemp = 1; ulTemp < argc; ulTemp++)
  72.         switch (argv[ulTemp][0])
  73.         {
  74.         case '-' :
  75.             switch (argv[ulTemp][1])
  76.             {
  77.             case 'v' :
  78.                 sscanf(&argv[ulTemp][2],"%lu",&ulVerbose);
  79.                 break;
  80.             default :
  81.                 fputs("Usage : devmon16 [-v#] [input port] [output pipe]\n",stderr);
  82.                 DosExit(EXIT_PROCESS,1);
  83.                 break;
  84.             }
  85.             break;
  86.  
  87.         default :
  88.             if (szPrintFileName[0])
  89.                 strcpy(szPipeFileName,argv[ulTemp]);
  90.             else
  91.                 strcpy(szPrintFileName,argv[ulTemp]);
  92.             break;
  93.         }
  94.  
  95.     /* copy default arguments */
  96.  
  97.     if (!szPipeFileName[0])
  98.         strcpy(szPipeFileName,"\\PIPE\\SPOOL.PIP");
  99.  
  100.     if (!szPrintFileName[0])
  101.         strcpy(szPrintFileName,"LPT1");
  102.  
  103.     /* open device monitor */
  104.  
  105.     rcStatus16 = Dos16MonOpen(szPrintFileName,&hMonitor16);
  106.  
  107.     if (rcStatus16)
  108.     {
  109.         printf("DosMonOpen failure rcStatus16 %u szFileName %s\n",rcStatus16,szPrintFileName);
  110.         DosExit(EXIT_PROCESS,1);
  111.     }
  112.  
  113.     if (ulVerbose)
  114.         printf("DosMonOpen success hMonitor16 %lu\n",hMonitor16);
  115.  
  116.     /* register device monitor */
  117.  
  118.     *(USHORT *)uchChainInput = sizeof(uchChainInput);
  119.     *(USHORT *)uchChainOutput = sizeof(uchChainOutput);
  120.  
  121.     rcStatus16 = Dos16MonReg(hMonitor16,uchChainInput,uchChainOutput,PLACE_END_CHAIN,PARALLEL_DATA_CHAIN);
  122.  
  123.     if (rcStatus16)
  124.     {
  125.         printf("DosMonReg failure rcStatus16 %u\n",rcStatus16);
  126.         DosExit(EXIT_PROCESS,1);
  127.     }
  128.  
  129.     if (ulVerbose)
  130.         printf("DosMonReg success\n");
  131.  
  132.     while (1)
  133.     {
  134.         /* read device monitor */
  135.  
  136.         usBytesRead = sizeof(MONITORPACKET);
  137.  
  138.         rcStatus16 = Dos16MonRead(uchChainInput,WAIT_ENABLE,&MonitorPacket,&usBytesRead);
  139.  
  140.         if (rcStatus16)
  141.         {
  142.             printf("DosMonRead failure rcStatus16 %u\n",rcStatus16);
  143.             DosExit(EXIT_PROCESS,1);
  144.         }
  145.  
  146.         if (ulVerbose)
  147.             printf("DosMonRead success usBytesRead %u usMonitorFlag %X usProcessId %u\n",
  148.                 usBytesRead,
  149.                 MonitorPacket.usMonitorFlag,
  150.                 MonitorPacket.usProcessId);
  151.  
  152.         /* if monitor open packet */
  153.  
  154.         if (MonitorPacket.usMonitorFlag & MONFLAG_OPEN)
  155.         {
  156.             if (hSpool)
  157.             {
  158.                 DosClose(hSpool);
  159.                 hSpool = 0;
  160.             }
  161.  
  162.             sprintf(szSpoolFileName,"\\SPOOL\\FAX\\%lu.tmp",ulSpoolFileId++);
  163.  
  164.             rcStatus = DosOpen(
  165.                         szSpoolFileName,            /* pszFileName            */
  166.                         &hSpool,                    /* ppFileHandle            */
  167.                         &ulAction,                    /* pActionTaken            */
  168.                         0,                            /* ulFileSize            */
  169.                         FILE_NORMAL,                /* ulFileAttribute        */
  170.                         OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_REPLACE_IF_EXISTS,
  171.                                                     /* ulOpenFlag            */
  172.                         OPEN_ACCESS_WRITEONLY | OPEN_SHARE_DENYWRITE,
  173.                                                     /* ulOpenMode            */
  174.                         NULL);                        /* ppEABuf                */
  175.  
  176.             if (rcStatus)
  177.             {
  178.                 printf("DosOpen failure rcStatus %lu szFileName %s\n",rcStatus,szSpoolFileName);
  179.                 DosExit(EXIT_PROCESS,1);
  180.             }
  181.  
  182.             if (ulVerbose)
  183.                 printf("Spool begin %s\n",szSpoolFileName);
  184.         }
  185.  
  186.         /* if monitor close packet */
  187.  
  188.         if (MonitorPacket.usMonitorFlag & MONFLAG_CLOSE)
  189.         {
  190.             if (hSpool)
  191.             {
  192.                 DosClose(hSpool);
  193.                 hSpool = 0;
  194.             }
  195.  
  196.             if (ulVerbose)
  197.                 printf("Spool complete %s\n",szSpoolFileName);
  198.  
  199.             rcStatus = DosCallNPipe(
  200.                         szPipeFileName,                    /* pszFileName        */
  201.                         (PVOID)szSpoolFileName,            /* pInBuffer        */
  202.                         sizeof(szSpoolFileName),        /* ulInBufferLen    */
  203.                         (PVOID)uchSpoolReply,            /* pOutBuffer        */
  204.                         sizeof(uchSpoolReply),            /* ulOutBufferLen    */
  205.                         &ulTemp,                        /* pBytesOut        */
  206.                         180*1000);                        /* ulTimeOut        */
  207.  
  208.             if (rcStatus)
  209.                 printf("DosCallNPipe failure rcStatus %lu szFileName %s\n",rcStatus,szPipeFileName);
  210.         }
  211.  
  212.         /* if monitor data packet */
  213.  
  214.         if (MonitorPacket.usMonitorFlag == 0 && usBytesRead > 4)
  215.         {
  216.             if (ulVerbose > 1)
  217.             {
  218.                 for (ulTemp = 4, pMonitorData = MonitorPacket.uchMonitorData; ulTemp < usBytesRead; ulTemp++, pMonitorData++)
  219.                     printf("%02X ",*pMonitorData);
  220.                 putchar('\n');
  221.             }
  222.  
  223.             if (hSpool)
  224.             {
  225.                 rcStatus = DosWrite(hSpool,MonitorPacket.uchMonitorData,usBytesRead-4,&ulTemp);
  226.  
  227.                 if (rcStatus)
  228.                 {
  229.                     printf("DosWrite failure rcStatus %lu ulBufferLength %lu ulBytesWritten %lu\n",rcStatus,usBytesRead-4,ulTemp);
  230.                     DosExit(EXIT_PROCESS,1);
  231.                 }
  232.             }
  233.         }
  234.     }
  235.  
  236.     /* close device monitor */
  237.  
  238.     rcStatus16 = Dos16MonClose(hMonitor16);
  239.  
  240.     if (rcStatus16)
  241.     {
  242.         printf("DosMonClose failure rcStatus16 %u\n",rcStatus16);
  243.         DosExit(EXIT_PROCESS,1);
  244.     }
  245.  
  246.     if (ulVerbose)
  247.         printf("DosMonClose success\n");
  248. }
  249.