/* r.c: suck mightily */
/* please assign fu: to the terminal */
#include <stdio.h>
#include <cx.h>
#include <qiofun.h>
#include <qioret.h>
#include <qiottd.h>
#include <spcio.h>
#define EFN 1
#define LUN 6
helpless()
{puts("fuck off");
 exit();
}
main(argc)
int		argc;
{int		dev;		/* device name in ascii */
 int		dsw;		/* directive status word */
 char		getachar();	/* suck one char from TL: */
 char		fromtl;		/* the char from TL: */
 char		tooutput;	/* the char we output */
 int		endoffile;	/* TRUE when tape end hit */
 if	(argc>1)	helpless();
 dev = 'F' | 'U'<<8;
 dsw = alun(LUN,dev,0);
 if	(dsw!=IS_SUC)
	{error("\7Can't attach to terminal %c%c:   dsw=%o#",dev,dev>>8,dsw);
	}
 ;
/*
	assume FU: is
	^Q
	SET /WRAP=FU:
	SET /RPA=FU:
	SET /FDX=FU:
	SET /FORMFEED=FU:
	SET /HFILL=FU:0
	SET /HHT=FU:
	SET /ECHO=FU:
	SET /TYPEAHEAD=FU:
	SET /NOCRT=FU:
	SET /SLAVE=FU:
	SET /LOWER=FU:
	SET /TERM=FU:ASR33
	SET /NOVFILL=FU:
	SET /EBC=FU:
*/
/* we could achieve above with a SF.SMC QIO - but the task would then need
   priviledge: so trust boot to set up these parameters */
/* endoffile is also set if there is a timeout */
 endoffile = FALSE;
 while ( (fromtl=getachar(&endoffile)) , endoffile )
	endoffile = FALSE;
	/* ignore timeouts until at least 1 char read */
 while	(endoffile==FALSE)
	{ if	(endoffile==FALSE)
		{putchar(tooutput=fromtl);
		}
	 ;
	 fromtl = getachar(&endoffile);
	}
 ;
}

char	getachar(endoffile)	/* get a char from FU: */
int		*endoffile;	/* set TRUE if timeout occurs */
{
 int		qiow();
 int		dsw;		/* directive status word */
 int		isb[2];		/* io status block */
 int		prl[6];		/* QIOW parameter list */
 char		buffer[2];	/* a small buffer */
 int		i;
 prl[0] = buffer;		/* buffer address: we never read into it,
				   so use mask as buffer */
 prl[1] = 1;			/* give it 1 byte so FDX term driver is happy */
 prl[2] = 1;			/* wait ten seconds until time-out (end-of-file) */
 dsw = qiow(IO_RLB|TF_RAL|TF_RNE|TF_TMO,LUN,EFN,isb,0,prl);
/* expect in isb:
	byte 0:		directive status code:	1 for success
*/
/* check for return code 2: timeout */
 if	( (dsw & 0xFF) == IS_SUC && (isb[0] & 0xFF) == IS_TMO )
	{/* timeout */
	 *endoffile = TRUE;
	 isb[0] = IS_SUC;	/* lies, damned lies and comments */
	}
 ;
 if	( (dsw & 0xFF) != IS_SUC || (isb[0] & 0xFF) != IS_SUC )
	{error("\7Can't read from TL: dsw=%xx isb=%xx %xx",dsw,isb[0],isb[1]);
	}
 ;
 return (buffer[0]);	/* yield 8 bits */
}
