#include <std.h>
#include <rsx.h>
#include <pccom.ih>
#include <fc.h>

#define debug	0

extern	io_wvb;
#define IO_WVB (int)&io_wvb
#define		RCVD	(75  | 4 << 8 )
#define		SRDA	(107 | 2 << 8 )
#define		STOP	(131 | 1 << 8 )
#define		USTP	(133 | 3 << 8 )
#define		WTSE	( 41 | 2 << 8 )
#define		CNCT	(143 | 6 << 8 )
#define		GTIM	( 61 | 2 << 8 )

#define		IS_SUC	1
#define 	EXITEFN	1
#define		FILLUN	1		/* Lun for file system		*/
#define		FACC	FCSEQIO|FCRWEACC|FCNEWFIL	/* Sequential file with all accs */

AST eco_ast  = { 04137, &entrap, 0, &echo };

int  fdbaut[ SFDBW ]={0};		/* fdb			*/
int	riob[ 2 ];			/* io status block	*/

struct fcoptblk fob= {0}                    /* no special options */
static int esb[8] = {0};		/* exit status block	*/
static	long task = 0l;		/* current task name rad50	*/

struct {
	long	rtyp;	/* Same as taskname	*/
	int	year;	/* Year since 1900	*/
	int	month;	/* month	(1-12)	*/
	int	day;	/* Day		(1-31)	*/
	int	hour;	/* hour		(0-23)	*/
	int	minute;	/* minute	(0-59)	*/
	int	second;	/* seconds	(0-59)	*/
	int	tick;	/* partof sec		*/
	int	clock;	/* ticks per second	*/
	int	extsts;	/* Task exit status	*/
	int	filler[2]; /* record filler	*/
}filestat = {0};



struct	{
	long	tsknm;		/* task name in rad50	*/
	unsigned  sts;		/* status		*/
	unsigned  vec;		/* vector		*/
	unsigned  ep;		/* entry point		*/
	long	  seg;		/* segment name		*/
	long	  reg;		/* region name		*/
	int	  dsw;		/* Directive status word*/
	int	  efm[4];	/* event flags		*/
	int	  efn;		/* syncronizing flag	*/
	} recbuf = {0};


/*****
******
**
**      Function:       x
**
**      Usage:          x
**
**      Returns:        nothing
**
**      Calls:          none
**
**      Operation:
**
******
*****/

main()
{

static char line[80];
int	status;
char	dsw;
char *edmsg();

for (;;)
	{
	dsw =emt( SRDA, &eco_ast );
	if( dsw < 0 ){
		*edmsg( line,
			 "%NSpecify recieve data ast failed. Status = %D",
			&dsw) = '\0';
		emt( QIOW, IO_WVB, 5, 5, 0,0, line,lenstr(line),0,0,0,0);
	}
	emt( STOP );
	emt( WTSE, EXITEFN);
	gettim();
	status = fcclos( fdbaut );
/*
	*edmsg(line,"%NFile close status = %P",&status) = '\0';
	emt( QIOW, IO_WVB, 5, 5, 0,0, line,lenstr(line),0,0,0,0);
*/
	task = 0l;	/* we don't know from task */
	}
}


/*****
******
**
**      Function:       x
**
**      Usage:          x
**
**      Returns:        nothing
**
**      Calls:          none
**
**      Operation:
**
******
*****/

echo()
{

static	char tasknm[12] = {0};
char	*namcnv = "%2R.AUT;1";

char *edmsg();		/* the message formatter	*/
int	status;

for (;;)
	{
	if( emt( RCVD, NULL, NULL, &recbuf ) == 1){
		if( task == 0l){
			fob.rtyp = RFIX;	/* FIX length records	*/
			fob.ratt = NULL;	
			fob.rsiz = sizeof( recbuf );
			fob.inaloc = 1;
			fob.exaloc = 5;
			fob.contrl = NULL;
			edmsg( &tasknm, namcnv , &recbuf );
			emt( CNCT, recbuf.tsknm,EXITEFN, 0, &esb);
			if( fcopen( fdbaut,"LB:","[1,4]",&tasknm, FILLUN, FACC, &fob)<0){
				zdsetf( &recbuf.tsknm, recbuf.efn);
                                exit();
			}
			task = recbuf.tsknm;
			emt( USTP, 0l );
			gettim();
		}
		if( task == recbuf.tsknm)
			putrb( fdbaut, &recbuf, sizeof( recbuf),NORECNUM, &riob);
		zdsetf( &recbuf.tsknm, recbuf.efn);
		}
	else
		{
		break;
		}
	}

return;
}

/*****
******
**
**      Function:       x
**
**      Usage:          x
**
**      Returns:        nothing
**
**      Calls:          none
**
**      Operation:
**
******
*****/

gettim()
{
	filestat.rtyp	= 0l;
	emt( GTIM, &filestat.year);
	filestat.extsts = esb[0];	/* copy the offspring status	*/
	putrb( fdbaut, &filestat, sizeof( recbuf),NORECNUM, &riob);
}
