home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / PRNMON.ZIP / PRNMON.C next >
C/C++ Source or Header  |  1989-02-15  |  7KB  |  245 lines

  1. /*****************************************************************************
  2.                 HPMON2A.C
  3.  - changed setting up of threads
  4.  - semaphores now set in main proc instead of waiting to setpu sem
  5.  - main thread now sleeps until a key is hit, then closes out
  6.  - added some new defines to main source
  7.  - otherwise, just like HPMON2.C
  8.  
  9.  
  10. *****************************************************************************/
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <conio.h>
  14. #include <process.h>
  15. #include <memory.h>
  16. #include <malloc.h>
  17. #include "prnmon.h"
  18.  
  19. #define TRUE             1
  20. #define FALSE            0
  21. #define STK_SIZE         1024
  22.  
  23. #define NUM_4201_ESC_PKT 6
  24. #define EMPTY_PKT_SIZE   4
  25.  
  26. unsigned                 prn_mon_handle;
  27. int                      prn_mon_binsiz;
  28. int                      prn_mon_boutsiz;
  29. int                      prn_mon_bmidsiz;
  30. struct PrnMonBuffer far  *prn_mon_bin;
  31. struct PrnMonBuffer far  *prn_mon_bout;
  32. char                     prn_mon_wrkbin [PRNMONBUFSIZE];
  33. char                     prn_mon_wrkbout [PRNMONBUFSIZE];
  34. char                     prn_mon_wrkbmid [PRNMONBUFSIZE];
  35. unsigned                 mon_reg_ret;
  36.  
  37. unsigned long            got_data;
  38. unsigned long            moved_data_in;
  39. unsigned long            moved_data_out;
  40. unsigned long            sent_data;
  41. unsigned                 load_rc, load_tid;
  42. unsigned                 proc_rc, proc_tid;
  43. unsigned                 outp_rc, outp_tid;
  44. char far                 *load_stack;
  45. char far                 *proc_stack;
  46. char far                 *outp_stack;
  47.  
  48. static void far          load_prn_mon_data (void);
  49. static void far          process_prn_mon_data (void);
  50. static void far          output_prn_mon_data (void);
  51.  
  52. /****************************************************************************/
  53. main ()
  54. {
  55.  
  56. /* get handle to monitor printer LPT1                                       */
  57.  
  58. printf("Setting up Monitor\n");
  59. DosMonOpen ("LPT1", &prn_mon_handle);
  60. if (!prn_mon_handle)
  61.    {
  62.    printf ("Unable to open printer monitor\n");
  63.    exit (-1);
  64.    }
  65. else
  66.    {
  67.    printf ("Printer monitor opened\n");
  68.    }
  69.  
  70. /* allocate storage for in/out buffers                                      */
  71.  
  72. prn_mon_bin  = (struct PrnMonBuffer far *) malloc (sizeof (struct PrnMonBuffer));
  73. if (!prn_mon_bin)
  74.    {
  75.    printf ("Unable to allocate memory for input buffer\n");
  76.    exit (-1);
  77.    }
  78. prn_mon_bout = (struct PrnMonBuffer far *) malloc (sizeof (struct PrnMonBuffer));
  79. if (!prn_mon_bout)
  80.    {
  81.    printf ("Unable to allocate memory for output buffer\n");
  82.    exit (-1);
  83.    }
  84.  
  85. /* initialize size of monitor packet                                        */
  86.  
  87. prn_mon_bin  -> size = sizeof (struct PrnMonBuffer);
  88. prn_mon_bout -> size = sizeof (struct PrnMonBuffer);
  89.  
  90. /* register buffers and monitor                                             */
  91.  
  92. mon_reg_ret = DosMonReg (prn_mon_handle, prn_mon_bin, prn_mon_bout, 0x0002, 0x0001);
  93. if (mon_reg_ret)
  94.    {
  95.    printf ("Unable to register printer monitor\n");
  96.    exit (-1);
  97.    }
  98. else
  99.    {
  100.    printf ("Printer monitor registered\n");
  101.    }
  102.  
  103. printf("Setting up Threads\n");
  104.  
  105. /* set up the semaphores                                                    */
  106.  
  107. DosSemSet   (&moved_data_in);
  108. DosSemSet   (&moved_data_out);
  109. DosSemSet   (&got_data);
  110. DosSemClear (&sent_data);
  111.  
  112. /* allocate stacks for threads                                              */
  113.  
  114. if ((load_stack = malloc (STK_SIZE)) == NULL)
  115.    {
  116.    printf("No memory for load stack\n");
  117.    exit (-1);
  118.    }
  119. if ((proc_stack = malloc (STK_SIZE)) == NULL)
  120.    {
  121.    printf("No memory for proc stack\n");
  122.    exit (-1);
  123.    }
  124. if ((outp_stack = malloc (STK_SIZE)) == NULL)
  125.    {
  126.    printf("No memory for output stack\n");
  127.    exit (-1);
  128.    }
  129. printf ("Stacks allocated\n");
  130.  
  131. /* create the threads                                                       */
  132.  
  133. load_rc = DosCreateThread (load_prn_mon_data, &load_tid, load_stack + STK_SIZE);
  134. if (load_rc == 0)
  135.    {
  136.    printf ("Load ID = %d\n", load_tid);
  137.    }
  138. else
  139.    {
  140.    printf ("Load thread error code = %d\n", load_rc);
  141.    exit (-1);
  142.    }
  143.  
  144. proc_rc = DosCreateThread (process_prn_mon_data, &proc_tid,proc_stack + STK_SIZE);
  145. if (proc_rc == 0)
  146.    {
  147.    printf ("Process ID = %d\n", proc_tid);
  148.    }
  149. else
  150.    {
  151.    printf ("Process thread error code = %d\n", proc_rc);
  152.    exit (-1);
  153.    }
  154.  
  155. outp_rc = DosCreateThread (output_prn_mon_data, &outp_tid,outp_stack + STK_SIZE);
  156. if (outp_rc == 0)
  157.    {
  158.    printf ("Outp ID = %d\n", outp_tid);
  159.    }
  160. else
  161.    {
  162.    printf ("Outp thread error code = %d\n", outp_rc);
  163.    exit (-1);
  164.    }
  165. printf ("Threads dispatched\n");
  166. printf ("Hit any key to close printer monitor >");
  167.  
  168. /* wait for a keystroke and close monitor                                   */
  169.  
  170. while (!kbhit ())
  171.    {
  172.    DosSleep (255L);
  173.    }
  174. DosMonClose (prn_mon_handle);
  175. exit (0);
  176. }
  177. /****************************************************************************/
  178. static void far  load_prn_mon_data (void)
  179. {
  180. for (;;)
  181.    {
  182.    prn_mon_binsiz = PRNMONBUFSIZE;
  183.    DosMonRead (prn_mon_bin, 0, prn_mon_wrkbin, &prn_mon_binsiz);
  184.    DosSemClear ( &got_data);
  185.    DosSemWait ( &moved_data_in, -1L);
  186.    DosSemSet ( &moved_data_in);
  187.    }
  188. }
  189. /****************************************************************************/
  190. static void far  process_prn_mon_data (void)
  191. {
  192. static int     new_print;
  193. static int     new_print_packet_count;
  194.  
  195.  
  196. new_print = FALSE;
  197. for (;;)
  198.    {
  199.    DosSemWait ( &got_data, -1L);
  200.    DosSemSet ( &got_data);
  201.    prn_mon_bmidsiz = prn_mon_binsiz;
  202.    memcpy (prn_mon_wrkbmid, prn_mon_wrkbin, prn_mon_bmidsiz);
  203.    DosSemClear ( &moved_data_in);
  204.    if (prn_mon_bmidsiz == EMPTY_PKT_SIZE)  /* if control packet             */
  205.       {
  206.       new_print = TRUE;                    /* signal new doc to print       */
  207.       if (prn_mon_wrkbmid [1] != 0)        /* if cancelled job packet       */
  208.      {
  209.      new_print_packet_count = NUM_4201_ESC_PKT;
  210.      }                                 /* pass packet right away        */
  211.       else
  212.      {
  213.      new_print_packet_count = 0;       /* else should be new doc still  */
  214.      }
  215.       }
  216.    if (new_print && new_print_packet_count < NUM_4201_ESC_PKT)
  217.       {                                    /* if new doc assumed            */
  218.       prn_mon_bmidsiz = EMPTY_PKT_SIZE;    /* trim esc seq data             */
  219.       new_print_packet_count += 1;
  220.       }
  221.    else if (new_print && new_print_packet_count > (NUM_4201_ESC_PKT - 1))
  222.       {
  223.       new_print = FALSE;
  224.       }
  225.    DosSemWait ( &sent_data, -1L);
  226.    DosSemSet ( &sent_data);
  227.    prn_mon_boutsiz = prn_mon_bmidsiz;
  228.    memcpy (prn_mon_wrkbout, prn_mon_wrkbmid, prn_mon_boutsiz);
  229.    DosSemClear ( &moved_data_out);
  230.    }
  231. }
  232.  
  233. /****************************************************************************/
  234. static void far  output_prn_mon_data (void)
  235. {
  236. for (;;)
  237.    {
  238.    DosSemWait (&moved_data_out, -1L);
  239.    DosSemSet (&moved_data_out);
  240.    DosMonWrite (prn_mon_bout, prn_mon_wrkbout, prn_mon_boutsiz);
  241.    DosSemClear (&sent_data);
  242.    }
  243. }
  244. /****************************************************************************/
  245.