File: do_monitor.c

package info (click to toggle)
cti-ifhp 2.1.8-2
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 932 kB
  • ctags: 845
  • sloc: ansic: 6,036; sh: 1,524; makefile: 318
file content (242 lines) | stat: -rw-r--r-- 5,780 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/**************************************************************************
 * LPRng IFHP Filter
 * Copyright 1994-1997 Patrick Powell, San Diego, CA <papowell@sdsu.edu>
 *
 * Based on the CTI printer filters.
 *  See COPYRIGHT for details.
 *
 * $Id: do_monitor.c,v 2.3 1997/01/21 04:07:06 papowell Exp $
 */

#include "portable.h"
#include "hp4.h"
#include "common.h"

/*
 * set up the child process to do the monitoring and synchronizaiton
 */


void pr_ustatus(fd,pjlstr)
int fd;
char *pjlstr;
{
	int i = 0;
	query[i++]=pjlstr;
	pr_query(fd, i);
}

void do_monitor()
{
	int printer_code;
	int n, err;
	char buf[MAXLINE+1];
	int i = 0;

	header_info();
	printer_ok = TRUE;
	if( get_status ){
		/*
		* Try to synchronise with printer; use 60 secs as good default
		*/
		if( sync_printer ){
			if( pr_synch(STDOUT, 60 )==FALSE ){
				log(2,"Failed to synchronise with printer");
				fexit(FILTRETRY);
			} else {
				log(2,"Synchronised with printer");
			}
			/* send the start of job string */
			i = 0;
			plp_snprintf(buf,sizeof(buf)-1,"@PJL JOB %s\r\n", job_start);
			query[i++] = buf;
			log(4, "do_monitor: starting");
			pr_query(STDOUT, i);
		}

	   /*
		* Check printer status
		*/
		if( infostatus ){
			do {
				printer_ok = FALSE;
				printer_code = 0;
				pr_ustatus(STDOUT, INFOSTATUS);	/* job status on */
				err = readpipe( &printer_code,4);
				n = printer_code/1000; /* code group */
				if( err < 0 ){
					log (2, "EOF reading from printer");
					fexit(FILTABORT);
				} else if( err != 0
					&& (n == 10 || n == 11 || printer_code == 35078 ) ){
					/*
					* These printer codes indicate that it is
					* safe to print:
					*
					* 10xxx: Informational messages.
					* 11xyy: Background Paper Mount (paper is out in some
					*        tray, but the same format is available in
					*        another tray).
					* 35078: Entered powersave mode.
					*/
					printer_ok = TRUE;
				}
			} while( printer_ok==FALSE );
		}
		/* get initial page count */
		if( pagecount ){
			initialpagecount = pr_pagecount(STDOUT);
			if( initialpagecount == 0 ){
				log (2, "Pagecount 0 using PJL INFO.");
			}
		}
		if( initialpagecount == 0 || forcepagecount ) {
			initialpagecount = pr_pspagecount(STDOUT);
			if (initialpagecount == 0){
				log (2, "Pagecount 0 using PostScript");
			}
		}
		/* set up reading of status information */
		if( infostatus ){
			/* pr_ustatus(STDOUT,"@PJL USTATUS DEVICE = VERBOSE\r\n"); */
			monitpid = fork();
			log(4,"Right after fork with monitpid=%d", monitpid);
			if( monitpid == -1 ){
				logerr(1,"Cannot fork process");
				fexit(FILTABORT);
			} else if( monitpid == 0 ){
				log(4,"Child with pid=%d",getpid());
				do{
					n = readpipe((int *)0, 30 );
				}while( n >= 0 );
				exit(0);
			}
			log(4,"Father pid=%d, child %d",getpid(), monitpid);
		}
	}

	log(2,"Initial page count %d", initialpagecount );
	doaccnt(1);

}
/**************************************************************************
 * LPRng IFHP Filter
 * Copyright 1994-1997 Patrick Powell, San Diego, CA <papowell@sdsu.edu>
 *
 * Based on the CTI printer filters.
 *  See COPYRIGHT for details.
 *
 * $Id: do_monitor.c,v 2.3 1997/01/21 04:07:06 papowell Exp $
 */

#include "portable.h"
#include "hp4.h"
#include "common.h"

/*
 * set up the child process to do the monitoring and synchronizaiton
 */


void pr_ustatus(fd,pjlstr)
int fd;
char *pjlstr;
{
	int i = 0;
	query[i++]=pjlstr;
	pr_query(fd, i);
}

void do_monitor()
{
	int printer_code;
	int n, err;
	char buf[MAXLINE+1];
	int i = 0;

	header_info();
	printer_ok = TRUE;
	if( get_status ){
		/*
		* Try to synchronise with printer; use 60 secs as good default
		*/
		if( sync_printer ){
			if( pr_synch(STDOUT, 60 )==FALSE ){
				log(2,"Failed to synchronise with printer");
				fexit(FILTRETRY);
			} else {
				log(2,"Synchronised with printer");
			}
			/* send the start of job string */
			i = 0;
			plp_snprintf(buf,sizeof(buf)-1,"@PJL JOB %s\r\n", job_start);
			query[i++] = buf;
			log(4, "do_monitor: starting");
			pr_query(STDOUT, i);
		}

	   /*
		* Check printer status
		*/
		if( infostatus ){
			do {
				printer_ok = FALSE;
				printer_code = 0;
				pr_ustatus(STDOUT, INFOSTATUS);	/* job status on */
				err = readpipe( &printer_code,4);
				n = printer_code/1000; /* code group */
				if( err < 0 ){
					log (2, "EOF reading from printer");
					fexit(FILTABORT);
				} else if( err != 0
					&& (n == 10 || n == 11 || printer_code == 35078 ) ){
					/*
					* These printer codes indicate that it is
					* safe to print:
					*
					* 10xxx: Informational messages.
					* 11xyy: Background Paper Mount (paper is out in some
					*        tray, but the same format is available in
					*        another tray).
					* 35078: Entered powersave mode.
					*/
					printer_ok = TRUE;
				}
			} while( printer_ok==FALSE );
		}
		/* get initial page count */
		if( pagecount ){
			initialpagecount = pr_pagecount(STDOUT);
			if( initialpagecount == 0 ){
				log (2, "Pagecount 0 using PJL INFO.");
			}
		}
		if( initialpagecount == 0 || forcepagecount ) {
			initialpagecount = pr_pspagecount(STDOUT);
			if (initialpagecount == 0){
				log (2, "Pagecount 0 using PostScript");
			}
		}
		/* set up reading of status information */
		if( infostatus ){
			/* pr_ustatus(STDOUT,"@PJL USTATUS DEVICE = VERBOSE\r\n"); */
			monitpid = fork();
			log(4,"Right after fork with monitpid=%d", monitpid);
			if( monitpid == -1 ){
				logerr(1,"Cannot fork process");
				fexit(FILTABORT);
			} else if( monitpid == 0 ){
				log(4,"Child with pid=%d",getpid());
				do{
					n = readpipe((int *)0, 30 );
				}while( n >= 0 );
				exit(0);
			}
			log(4,"Father pid=%d, child %d",getpid(), monitpid);
		}
	}

	log(2,"Initial page count %d", initialpagecount );
	doaccnt(1);

}