File: pr_synch.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 (88 lines) | stat: -rw-r--r-- 2,299 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
/**************************************************************************
 * 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: pr_synch.c,v 2.1 1997/01/05 18:41:44 papowell Exp $
 */

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


/*
 * In the PJL manual, Chapter 9, Section: Determining if printer status
 * readback is available it states that if the application does not
 * receive a response within three seconds, then the application can 
 * assume printer status readback is currently unavailable.
 */

int pr_synch(int fd, int timeout)
{
	int   synchronised = 0, fftries = 0;
	char buf[MAXLINE+1];
	char sync[MAXLINE+1];
	char timed[MAXLINE+1];
	int len;
	int i = 0;
	long starttime, newtime, inittime;
	int delay = 1;

	if( wait_time <= 0 ) wait_time = 30;
	if( timeout <= 0 ) timeout = wait_time;

	log(2,"Synchronisation attempt, %d sec timeout", timeout);
	
	starttime = time( (time_t *)0 );
	inittime = starttime;
	query[i++]=UEL;
	query[i++]=PCLRESETSTR;
	query[i++]=UELPJL;
	query[i++]=USTPJLOFF;
	query[i++]=USTPJLDEV;
	query[i++]=USTPJLJOB;
	(void)plp_snprintf(timed,sizeof(timed)-1,USTPJLTIMED, wait_time);
	query[i++]=timed;
	(void)plp_snprintf(sync,sizeof(sync)-1,"Seq %d",getpid());
	(void)plp_snprintf(buf,sizeof(buf)-1,"@PJL ECHO %s\r\n",sync);
	query[i++]=buf;
	if( infostatus ) query[i++]=INFOSTATUS; /* job status on */

	delay = 1;
	do{
		pr_query(fd, i);
		log(2,"pr_synch: waiting %d, attempt %d", delay, ++fftries );
		len = readpipe((int *)0,delay);
		if( len < 0 ){
			break;
		}
		if( strstr(readprin,sync) ) {
			synchronised = TRUE;
		} else if( strstr(readprin, "@PJL ECHO") ) {
			log(2,"pr_synch: synch from previous job '%s'", readprin);
		}
		if( !synchronised ){
			/* increase the waiting time */
			if( delay < timeout ) delay += delay;
			newtime = time( (time_t *)0 );
			if( (newtime - starttime) > timeout ){
				starttime = newtime;
				len = newtime - inittime;
				log(0,"No response from printer for %d secs; offline?", len );
			}
		}
	} while( !synchronised );
	return( synchronised );
}

void header_info()
{
	int i = 0;
	query[i++]=UEL;
	query[i++]=PCLRESETSTR;
	query[i++]=UELPJL;
	pr_query(STDOUT, i);
}