include lpd.h

unsigned char			specification[80] = [Sys]<$lpr>;
unsigned char			remotegps_directory[43] = [Sys]<$lpd>;

unsigned				exchanges[2];

unsigned				fdevelopemente = FALSE;

char					lpd_version[50] = Lpd (version ;

TRB						timer_rq = { 0, 0, 0, 0, 0, OPENRTCLOCK};

unsigned long			tcp_ipaddr;
unsigned				tcp_mss;
unsigned				tcp_window;
unsigned				tcp_ttl;
unsigned char			tcp_node[64+1];
unsigned				tcp_snode;

unsigned				lpd_port;

unsigned				clstr = FALSE;
unsigned				sp_os = FALSE;
unsigned				srp = FALSE;
unsigned				protected = FALSE;

unsigned				lpd_converted = FALSE;
unsigned				lpd_usernumber;

unsigned				lpd_nticks = NTICKS;

unsigned char			lpd_message[125] = '\0';
LOG 					lpd_log_record;

unsigned char			fdevice = TRUE;

extern char				init_before;
extern char				init_erc;
extern char				init_ok;
extern char				init_part;
extern char				init_req;

*======================.============<  >===================================*
unsigned get_process( )
{
	unsigned			process;
	unsigned			usernum;
	unsigned			* pcb;
	unsigned			i;

	getusernumber( usernum);
	queryprocessnumber( process);
	getpstructure( 0x28c, usernum, pcb);

	i = process  *pcb;
	i = 18;
	return( i);
}

*======================.============<  >===================================*
void log_ctos( pb, erc)
char					* pb;
{
	unsigned			cb;
	unsigned			process;

	lpd_message[0] = '\0';
	strcat( lpd_message, lpd_version);
	process = get_process( );
	strcat( lpd_message,  process );
	strcat( lpd_message, utoa( process));
	strcat( lpd_message, , );
	strcat( lpd_message, pb);
	if( erc = ERC_OK) {
		strcat( lpd_message,  ERC = );
		strcat( lpd_message, utoa( erc));
	}
	strcat( lpd_message, .\n);
	cb = min( strlen( lpd_message), 124);
	lpd_log_record.type = LOG_USER_TYPE;
	lpd_log_record.cbmessage = cb;
	strncpy( lpd_log_record.message[0], lpd_message, cb);
	writelog( lpd_log_record, cb + 4);
	if( srp  lpd_converted) {
		outputtovid0( lpd_message, cb);
	}
	if( erc = 0) {
		setpartitionlock( 0);
		if( lpd_converted) {
			agent_abort_all( erc);
			server_abort_all( erc);
		}
		errorexit( erc);
	}
}

*======================.============<  >===================================*
void lpd_checkerc( erc)
unsigned				erc;
{
	static unsigned		exiting = FALSE;

	if( erc = ERC_OK) {
		if( exiting) {
			exiting = TRUE;
			if( fdevelopemente) {
				debug( );
			}
			log_ctos(  error exiting with , erc);
		}
	}
}

*======================.============<  >===================================*
void lpd_initialize( )
{
	PDB					pdb;
	unsigned			kbytes = ALLOC_DEFAULT;
	unsigned			paragraphs;
	unsigned			erc;
	PBCB				pbcb;
	unsigned			u;
	int					i;
	char				tversion[20];
	unsigned char		fconvert = TRUE;
	unsigned char		* memory;
	SCB					* scb;
	unsigned			ticks;
	unsigned char		fprotectedmode( );

	protected = fprotectedmode( );
	
	for( i = sbverrun[0], tversion[i] = '\0'; i > 0; i)
		tversion[i1] = sbverrun[i];

	strcat( lpd_version, tversion);
	strcat( lpd_version, ));

	getusernumber( lpd_usernumber);

	lpd_checkerc( getpstructure( SCB_CODE, lpd_usernumber, scb));

	if( scb>hardwaretype >= 0x0a) {
		srp = TRUE;
	}
	if( scb>fmultipartition == 0) {
		sp_os = TRUE;
	}
	if( scb>clusterconfiguration == 1) {
		clstr = TRUE;
	}
										* get alloc size  *
	get_param_unsigned( 1, 0, kbytes);
										* get dead timer size  *
	get_param_unsigned( 2, 0, lpd_nticks);

	lpd_nticks *= TICKS;
										* get Convert to Sys  *
	get_param_yes_no( 3, 0, fconvert);

	get_param_string( 4, 0, specification, sizeof( specification));

	get_param_string( 5, 0, remotegps_directory, sizeof( remotegps_directory));

	get_param_yes_no( 6, 0, fdevice);

	erc = tcpgetsetup( tcp_ipaddr, tcp_mss, tcp_window, tcp_ttl,
			tcp_node, 64, tcp_snode);
	if( erc == 31) {
		log_ctos( init_req, erc);
	} else if( erc == 33) {
		log_ctos( init_before, erc);
	} else if( erc = 0) {
		log_ctos( init_erc, erc);
	}

	lpd_port = 515;

	lpd_checkerc( allocexch( exchanges[SERVER]));
	lpd_checkerc( allocexch( exchanges[AGENT]));

	resetmemoryll( );
											* Allocate and free some memory *
											* so that it's in use when *
											* and if we convert to sys. *
	if( ( memory = calloc( kbytes, 1024)) == NULLCHAR) {
		lpd_checkerc( 400);
	}
	free( memory);

	ticks = ( 10  TICKS);
	timer_rq.exchresp = exchanges[AGENT];

	timer_rq.counter = 0;
	timer_rq.counterreload = 0;
	timer_rq.cevents = 0;
	lpd_checkerc( openrtclock( timer_rq));
	timer_rq.counterreload = ticks;
	timer_rq.counter = ticks;

	pdb.pentry = server_process;
	pdb.sadata = ds_reg;
	pdb.saextra = ds_reg;
	pdb.sastack = ds_reg;
	pdb.ostackinit = server_stack;
	pdb.priority = PRIORITY_SERVER;
	pdb.fsys = FALSE;
	lpd_checkerc( allocexch( pdb.defaultrespexch));
	pdb.fdebug = FALSE;

	lpd_checkerc( changepriority( PRIORITY_AGENT));

	if( sp_os) {
		lpd_checkerc( createprocess( pdb));
		log_ctos( init_ok, 0);
		if( fconvert) {
			lpd_checkerc( converttosys( ));
			exit( );
		}
		lpd_converted = TRUE;
		getusernumber( lpd_usernumber);
		setpartitionname( lpd_usernumber, init_part, strlen( init_part));
		setswapdisable( 1);
	} else {
		log_ctos( init_ok, 0);
		if( fconvert) {
			lpd_checkerc( converttosys( ));
			exit( );
		}
		lpd_converted = TRUE;
		getusernumber( lpd_usernumber);
		setpartitionname( lpd_usernumber, init_part, strlen( init_part));
		setswapdisable( 1);
		lpd_checkerc( createprocess( pdb));
	}
	tcp_initialize( );
}

igned char			tcp_node[64+1];
unsigned				tcp_snode;

unsigned				lpd_port;

unsigned				clstr = FALSE;
unsigned				sp_os = FA