home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 11 Util
/
11-Util.zip
/
PRNMON.ZIP
/
PRNMON.C
next >
Wrap
C/C++ Source or Header
|
1989-02-15
|
7KB
|
245 lines
/*****************************************************************************
HPMON2A.C
- changed setting up of threads
- semaphores now set in main proc instead of waiting to setpu sem
- main thread now sleeps until a key is hit, then closes out
- added some new defines to main source
- otherwise, just like HPMON2.C
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <process.h>
#include <memory.h>
#include <malloc.h>
#include "prnmon.h"
#define TRUE 1
#define FALSE 0
#define STK_SIZE 1024
#define NUM_4201_ESC_PKT 6
#define EMPTY_PKT_SIZE 4
unsigned prn_mon_handle;
int prn_mon_binsiz;
int prn_mon_boutsiz;
int prn_mon_bmidsiz;
struct PrnMonBuffer far *prn_mon_bin;
struct PrnMonBuffer far *prn_mon_bout;
char prn_mon_wrkbin [PRNMONBUFSIZE];
char prn_mon_wrkbout [PRNMONBUFSIZE];
char prn_mon_wrkbmid [PRNMONBUFSIZE];
unsigned mon_reg_ret;
unsigned long got_data;
unsigned long moved_data_in;
unsigned long moved_data_out;
unsigned long sent_data;
unsigned load_rc, load_tid;
unsigned proc_rc, proc_tid;
unsigned outp_rc, outp_tid;
char far *load_stack;
char far *proc_stack;
char far *outp_stack;
static void far load_prn_mon_data (void);
static void far process_prn_mon_data (void);
static void far output_prn_mon_data (void);
/****************************************************************************/
main ()
{
/* get handle to monitor printer LPT1 */
printf("Setting up Monitor\n");
DosMonOpen ("LPT1", &prn_mon_handle);
if (!prn_mon_handle)
{
printf ("Unable to open printer monitor\n");
exit (-1);
}
else
{
printf ("Printer monitor opened\n");
}
/* allocate storage for in/out buffers */
prn_mon_bin = (struct PrnMonBuffer far *) malloc (sizeof (struct PrnMonBuffer));
if (!prn_mon_bin)
{
printf ("Unable to allocate memory for input buffer\n");
exit (-1);
}
prn_mon_bout = (struct PrnMonBuffer far *) malloc (sizeof (struct PrnMonBuffer));
if (!prn_mon_bout)
{
printf ("Unable to allocate memory for output buffer\n");
exit (-1);
}
/* initialize size of monitor packet */
prn_mon_bin -> size = sizeof (struct PrnMonBuffer);
prn_mon_bout -> size = sizeof (struct PrnMonBuffer);
/* register buffers and monitor */
mon_reg_ret = DosMonReg (prn_mon_handle, prn_mon_bin, prn_mon_bout, 0x0002, 0x0001);
if (mon_reg_ret)
{
printf ("Unable to register printer monitor\n");
exit (-1);
}
else
{
printf ("Printer monitor registered\n");
}
printf("Setting up Threads\n");
/* set up the semaphores */
DosSemSet (&moved_data_in);
DosSemSet (&moved_data_out);
DosSemSet (&got_data);
DosSemClear (&sent_data);
/* allocate stacks for threads */
if ((load_stack = malloc (STK_SIZE)) == NULL)
{
printf("No memory for load stack\n");
exit (-1);
}
if ((proc_stack = malloc (STK_SIZE)) == NULL)
{
printf("No memory for proc stack\n");
exit (-1);
}
if ((outp_stack = malloc (STK_SIZE)) == NULL)
{
printf("No memory for output stack\n");
exit (-1);
}
printf ("Stacks allocated\n");
/* create the threads */
load_rc = DosCreateThread (load_prn_mon_data, &load_tid, load_stack + STK_SIZE);
if (load_rc == 0)
{
printf ("Load ID = %d\n", load_tid);
}
else
{
printf ("Load thread error code = %d\n", load_rc);
exit (-1);
}
proc_rc = DosCreateThread (process_prn_mon_data, &proc_tid,proc_stack + STK_SIZE);
if (proc_rc == 0)
{
printf ("Process ID = %d\n", proc_tid);
}
else
{
printf ("Process thread error code = %d\n", proc_rc);
exit (-1);
}
outp_rc = DosCreateThread (output_prn_mon_data, &outp_tid,outp_stack + STK_SIZE);
if (outp_rc == 0)
{
printf ("Outp ID = %d\n", outp_tid);
}
else
{
printf ("Outp thread error code = %d\n", outp_rc);
exit (-1);
}
printf ("Threads dispatched\n");
printf ("Hit any key to close printer monitor >");
/* wait for a keystroke and close monitor */
while (!kbhit ())
{
DosSleep (255L);
}
DosMonClose (prn_mon_handle);
exit (0);
}
/****************************************************************************/
static void far load_prn_mon_data (void)
{
for (;;)
{
prn_mon_binsiz = PRNMONBUFSIZE;
DosMonRead (prn_mon_bin, 0, prn_mon_wrkbin, &prn_mon_binsiz);
DosSemClear ( &got_data);
DosSemWait ( &moved_data_in, -1L);
DosSemSet ( &moved_data_in);
}
}
/****************************************************************************/
static void far process_prn_mon_data (void)
{
static int new_print;
static int new_print_packet_count;
new_print = FALSE;
for (;;)
{
DosSemWait ( &got_data, -1L);
DosSemSet ( &got_data);
prn_mon_bmidsiz = prn_mon_binsiz;
memcpy (prn_mon_wrkbmid, prn_mon_wrkbin, prn_mon_bmidsiz);
DosSemClear ( &moved_data_in);
if (prn_mon_bmidsiz == EMPTY_PKT_SIZE) /* if control packet */
{
new_print = TRUE; /* signal new doc to print */
if (prn_mon_wrkbmid [1] != 0) /* if cancelled job packet */
{
new_print_packet_count = NUM_4201_ESC_PKT;
} /* pass packet right away */
else
{
new_print_packet_count = 0; /* else should be new doc still */
}
}
if (new_print && new_print_packet_count < NUM_4201_ESC_PKT)
{ /* if new doc assumed */
prn_mon_bmidsiz = EMPTY_PKT_SIZE; /* trim esc seq data */
new_print_packet_count += 1;
}
else if (new_print && new_print_packet_count > (NUM_4201_ESC_PKT - 1))
{
new_print = FALSE;
}
DosSemWait ( &sent_data, -1L);
DosSemSet ( &sent_data);
prn_mon_boutsiz = prn_mon_bmidsiz;
memcpy (prn_mon_wrkbout, prn_mon_wrkbmid, prn_mon_boutsiz);
DosSemClear ( &moved_data_out);
}
}
/****************************************************************************/
static void far output_prn_mon_data (void)
{
for (;;)
{
DosSemWait (&moved_data_out, -1L);
DosSemSet (&moved_data_out);
DosMonWrite (prn_mon_bout, prn_mon_wrkbout, prn_mon_boutsiz);
DosSemClear (&sent_data);
}
}
/****************************************************************************/