home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 17 / CD_ASCQ_17_101194.iso / vrac_os2 / pmon2.zip / PRINTMON.C < prev    next >
C/C++ Source or Header  |  1994-02-25  |  4KB  |  198 lines

  1. /* printmon.c
  2.  *
  3.  * printer redirection monitor
  4.  *
  5.  * Author:  Kai Uwe Rommel <rommel@jonas>
  6.  * Created: Sat Dec 24 1993
  7.  *
  8.  * Compile with MS C 6.00 (cl -G2s -O printmon.c -Lp -F 8000 -link /pm:vio)
  9.  * or with emx+gcc using the Makefile.
  10.  */
  11.  
  12. static char *rcsid =
  13. "$Id: printmon.c,v 1.4 1994/02/25 16:16:47 rommel Exp $";
  14. static char *rcsrev = "$Revision: 1.4 $";
  15.  
  16. /* $Log: printmon.c,v $
  17.  * Revision 1.4  1994/02/25 16:16:47  rommel
  18.  * changes for emx 0.8h fix 5
  19.  *
  20.  * Revision 1.3  1993/12/29 11:22:01  rommel
  21.  * corrected error messages
  22.  *
  23.  * Revision 1.2  1993/12/25  12:27:53  rommel
  24.  * made emx compatible
  25.  *
  26.  * Revision 1.1  1993/12/24  19:23:23  rommel
  27.  * Initial revision
  28.  *
  29.  * */
  30.  
  31. #define   INCL_DOSFILEMGR
  32. #define   INCL_DOSMONITORS
  33. #define   INCL_DOSPROCESS
  34. #include <os2.h>
  35.  
  36. #include <stdio.h>
  37. #include <string.h>
  38. #include <process.h>
  39. #include <malloc.h>
  40.  
  41. #ifdef M_I86
  42. #define popen _popen
  43. #define pclose _pclose
  44. #endif
  45.  
  46. #ifdef DEBUG
  47. #define dprintf(f, p) printf(f, p)
  48. #else
  49. #define dprintf(f, p)
  50. #endif
  51.  
  52. #pragma pack(1)
  53.  
  54. struct buffer
  55. {
  56.   short length;
  57.   char data[158];
  58. };
  59.  
  60. struct packet
  61. {
  62.   unsigned char mflag;
  63.   unsigned char dflag;
  64.   unsigned short sysfilenum;
  65.   char wdata[128];
  66. };
  67.  
  68. #pragma pack()
  69.  
  70.  
  71. void main(int argc, char **argv)
  72. {
  73.   USHORT usRC, cbData;
  74.   ULONG cbWritten;
  75.   HMONITOR mon;
  76.   struct buffer ibuffer, obuffer;
  77.   struct packet wbuffer;
  78.   char port[16], command[BUFSIZ]; 
  79.   FILE *pipe;
  80.  
  81.   printf("\nPRINTMON: printer queue redirection device monitor\n");
  82.  
  83.   if (argc < 3)
  84.   {
  85.     printf("\nUsage:\tPRINTMON <port> <command>\n\n"
  86.        "\t<port>    = port set up as PostScript (with NO printer attached)\n"
  87.        "\t<command> = command to pipe the jobs into\n");
  88.     exit(1);
  89.   }
  90.  
  91.   argc--; argv++;
  92.   strcpy(port, *argv);
  93.   argc--; argv++;
  94.  
  95.   command[0] = 0;
  96.   while (argc--)
  97.     strcat(strcat(command, *argv++), " ");
  98.  
  99.   usRC = DosMonOpen(port, &mon);
  100.  
  101.   if (usRC != 0)
  102.   {
  103.     printf("DosMonOpen error code: %u\n", usRC);
  104.     exit(1);
  105.   }
  106.  
  107.   ibuffer.length = obuffer.length = sizeof(struct buffer);
  108.   usRC = DosMonReg(mon, (PBYTE) &ibuffer, (PBYTE) &obuffer, MONITOR_END, 1);
  109.  
  110.   if (usRC)
  111.   {
  112.     printf("DosMonReg error code: %u\n", usRC);
  113.     exit(1);
  114.   }
  115.  
  116.   while (usRC == 0)
  117.   {
  118.     cbData = sizeof(struct packet);
  119.     usRC = DosMonRead((PBYTE) &ibuffer, 0, (PBYTE) &wbuffer, &cbData);
  120.  
  121.     if (usRC)
  122.     {
  123.       printf("DosMonRead error code: %u\n", usRC);
  124.       break;
  125.     }
  126.  
  127.     if (wbuffer.mflag & 1)
  128.     {
  129.       dprintf("monitor: open\n", 0);
  130.  
  131.       pipe = popen(command, "wb");
  132.  
  133.       if (pipe == NULL)
  134.       {
  135.     printf("cannot open output pipe\n");
  136.     break;
  137.       }
  138.  
  139.       cbWritten = 0;
  140.       continue;
  141.     }
  142.  
  143.     if (wbuffer.mflag & 2)
  144.     {
  145.       dprintf("\nmonitor: close\n", 0);
  146.  
  147.       pclose(pipe);
  148.  
  149.       continue;
  150.     }
  151.  
  152.     if (wbuffer.mflag & 4)
  153.     {
  154.       dprintf("monitor: flush\n", 0);
  155.  
  156.       usRC = fwrite(wbuffer.wdata, cbData - 4, 1, pipe);
  157.  
  158.       if (usRC < 1)
  159.       {
  160.     printf("cannot write to output queue\n");
  161.     break;
  162.       }
  163.  
  164.       continue;
  165.     }
  166.  
  167.     if (wbuffer.dflag == 32)
  168.     {
  169.       dprintf("monitor: job id '%s'\n", wbuffer.wdata + sizeof(short));
  170.       continue;
  171.     }
  172.  
  173.     if (wbuffer.dflag == 0)
  174.     {
  175.       cbWritten += cbData - 4;
  176.       dprintf("\rmonitor: %lu data bytes", cbWritten);
  177.  
  178.       usRC = (fwrite(wbuffer.wdata, cbData - 4, 1, pipe) < 1);
  179.  
  180.       if (usRC)
  181.       {
  182.     printf("cannot write to output queue\n");
  183.     break;
  184.       }
  185.  
  186.       continue;
  187.     }
  188.   }
  189.  
  190.   dprintf("monitor: closing\n", 0);
  191.  
  192.   DosMonClose(mon);
  193.  
  194.   exit(0);
  195. }
  196.  
  197. /* end of printmon.c */
  198.