/*								*/
/*	Copyright Notice					*/
/*								*/
/*								*/
/*	C version Copyright (C) 1984 Change Software, Inc.	*/
/*	Copyright (C) 1978 1979 1980 1981 1982 1983 1984 by	*/
/*		      Change Software, Inc.			*/
/*								*/
/*								*/
/*	This software is furnished under a license and may	*/
/*	be  used  and  copied  only in accordance with the	*/
/*	terms of such license and with  the  inclusion  of	*/
/*	the  above copyright notice.  This software or any	*/
/*	other copies thereof may not be provided or other-	*/
/*	wise made available to any other person.  No title	*/
/*	to and ownership of the software is hereby  trans-	*/
/*	ferred.							*/
/*								*/

#include	"SYSDEF.H"
#define	then



static int handler ;

inistats()
{
	lib$init_timer(&handler) ;

}
	
accstats()
{
	lib$show_timer(&handler) ;
	lib$init_timer(&handler) ;

}




#include	<jpidef>

chkrmt()
{
struct jpilst {
		short buffer_len ;
		short item_code ;
		char *buffer_address ;
		int  *return_len ;
		} jpi[2] ;


	int len ;
	char ttname[17] ;

	jpi[0].buffer_len = 16 ;
	jpi[0].item_code = JPI$_TERMINAL ;
	jpi[0].buffer_address = ttname ;
	jpi[0].return_len = &len ;
	jpi[1].buffer_len = 0 ;
	jpi[1].item_code = 0 ;
	if ( sys$getjpiw(0,0,0,&jpi,0,0,0) & 1 )
	  	{
		ttname[len&0377] = 0 ;
		if ( strncmp(ttname,"RT",2) == 0 )
		  printf("\nWarning - editing on a DECNET remote line\n");
		} ;

	return(1) ;
}



czap(s,eraseflag)
char *s ;
int eraseflag ;
{
	char cmd[_FILESIZE+20] ;

	if (eraseflag)
	  then	strcpy(cmd,"DEL/LOG/ERA ") ;
	  else	strcpy(cmd,"DEL/LOG ") ;
	strcat(cmd,s) ;
	dclcommand(cmd) ;
	putbin("\015\012",0) ;
}


cdir(s)
char *s ;
{
	char cmd[_FILESIZE+20] ;

	strcpy(cmd,"DIR/DAT/SIZ ") ;
	strcat(cmd,s) ;
	dclcommand(cmd) ;
	putbin("\015\012",0) ;
}

cpurge(s)
char *s ;
{
	char cmd[_FILESIZE+20] ;

	strcpy(cmd,"PUR/LOG ") ;
	strcat(cmd,s) ;
	dclcommand(cmd) ;
	putbin("\015\012",0) ;
}

#ifdef	COMSPAWN

static int spawndone = 0 ;

#include <descrip>
#include <iodef>

#define then

struct dsc	{
		int len ;
		char *addr ;
		} ;

#define	BUFSIZE		255

int spawnpid = 0 ;
static sp_flags = 0 ;
static int mbin_lun,mbout_lun,tt_lun,status ;

static char mbin_name[64] ;
static struct dsc mbin_phyname =  { 63, &mbin_name } ;
$DESCRIPTOR(mbin_lnam,"TED_INMBX") ;
$DESCRIPTOR(mbinputfile,"TED_HELP:TEDSUBPRO.COM") ;


delspawn()
{
	if ( spawnpid && spawndone )
	  then sys$delprc( &spawnpid,0 ) ;
	return(1) ;
}


static dospawn()
{
	int sts ;

	if ( flookup(mbinputfile.dsc$a_pointer) == 0 )
	  then	{
		printf("Can not find %s\n",mbinputfile.dsc$a_pointer) ;
		suspend(1) ;
		return(0) ;
		} ;
	if (((sts=sys$crembx(0,&mbin_lun,BUFSIZE,0,0,0,&mbin_lnam)) & 1 )==0)
	  then return( sts ) ;
	if (((sts=sys$trnlog(&mbin_lnam,0,&mbin_phyname,0,0,0)) & 1 ) == 0 )
		then return( sts ) ;

	return( lib$spawn(0,&mbinputfile,0,&sp_flags,0,&spawnpid) ) ;
	
}

dclcommand(cmd)
char *cmd ;
{
	int status ;

	if ( spawndone == 0 )
	  then if ( ( (status = dospawn()) & 1 ) == 0 )
	    then return(0) ;
	    else spawndone = 1 ;
	status = sys$qio(0,mbin_lun,IO$_WRITEVBLK+IO$M_NOW,0,0,0,
		cmd,strlen(cmd),0,0,0,0) ;
	if ( status & 1 ) then status = lib$attach(&spawnpid) ;
	if ( ( status & 1 ) == 0 ) then syseprint(status) ;
	return(1) ;
}

#else
delspawn()
{
}
#endif

static syseprint(val)
int val ;
{
	char s[81] ;
	sys_error(val,s) ;
	printf("%s\n",s) ;
}

gterm()
{
	extern int termtype ;
	return( termtype ) ;
}


mapcache(w)
int w ;
{
	return(1) ;
}

unmapcache(w)
int w ;
{
	return(1) ;
}

mapwindow(w)
int w ;
{
	return(1) ;
}








#define	TTBUFSIZE	1024

static char ttbuffer[TTBUFSIZE+2] ;
static int dobuffering = 0 ;
static int next = 0 ;

static int qios_under_20 = 0 ;
static int qios_silo = 0 ;
static int qios_dma = 0 ;


enabuffering()
{
	ttbuffer[0] = 0 ;
	dobuffering = 1 ;
	next = 0 ;
}

disbuffering()
{
	if ( dobuffering && next )
	  then 	{
		tedputbin(ttbuffer,next) ;
		if ( next < 21 )
		  then qios_under_20++ ;
		  else if ( next > 199 )
		    then qios_dma++ ;
		    else qios_silo++ ;
		} ;
	dobuffering = 0 ;
	ttbuffer[0] = 0 ;
	next = 0 ;
}

putbin(s,len)
char *s ;
int len ;
{
	int size ;
	size = ( len ) ? len:strlen(s) ;
	if ( dobuffering == 0 )
	  then	tedputbin(s,size) ;
	  else	{
		if ( size+next > TTBUFSIZE )
		  then	{
			qios_dma++ ;
			tedputbin(ttbuffer,next) ;
			next = 0 ;
			ttbuffer[0] = 0 ;
			} ;
		while ( size-- )  ttbuffer[next++] = *s++ ;
		ttbuffer[next] = 0 ;
		return(1) ;
		} ;
	return(1) ;
}


qiostats()
{
	movel24() ;
	ceol24() ;
	printf("QIO's int %d   silo %d   dma %d\n",qios_under_20,
		qios_silo,qios_dma) ;
	goback() ;
}


cvtdd(s,n)
char *s ;
int *n ;
{
	sprintf(s,"%s",n) ;
}





static char macrobuffer[_NMACROS*_MACSIZE] ;

mapmacro(i)
{
	return(&macrobuffer[_MACSIZE*i]) ;
}


