/*
 * sucker.c: suck UNIX tape???? Stolen from T2B.C
 */
#include <stdio.h>
#include <cx.h>
#include <qiofun.h>
#include <qioret.h>
#define EFN 1
#define LUN 6
#define BUFSIZ 10320		/* from [5,1]rdmt.bas */
extern int $$ferr;
char		b[BUFSIZ];
char		ascii[256];	/* ebcdic->ascii table */
char		outfile[40];	/* output file name basis if any */
char		filename[50];	/* build output file name here */
unsigned	quantum;	/* non-zero: quantum to fake tapemark after */
unsigned	filenum;	/* last file number used when outfile<>"" */
FILE		*ioptr;		/* the output file when outfile<>"" */
unsigned	recspat;	/* incremented each record output this file */
helpless()
{	error("\7WHAT THE HELL ARE YOU RUNNING THIS FOR???\7\n");
	exit();
}
main(argc,argv)
char **argv;
{unsigned	u;
 char		c;
 int		i;
 char		*p;
 int		toupper();
 int		ids;		/* directive status word */
 int		isb[2];		/* qio status block */
 unsigned	got;
 char		dnam[3];	/* magtape device name (ASCII) */
 int		dnum;		/* magtape unit number */
 int		dev;		/* word-aligned device name */
 int		dsw;		/* directive status word */
 int		eof1;		/* TRUE if just seen eof */
 unsigned	rmtrec();	/* read a magtape record (or tape mark) */
 int		morz;		/* maximum output record size */
 long int	fro;		/* first record ordinal */
 long int	lro;		/* last record ordinal */
 long int	ro;		/* current record ordinal */
 int		e2a;		/* TRUE if ebcdic to ascii conversion */

 if	(argc==0)
	{helpless();
	}
 ;
 dsw=alunx(LUN,"MT",0);
 if	(dsw!=IS_SUC)
	{error("\7Can't attach to %s dsw=%o",argv[1],dsw&0xFF);
	}
 ;
 fro=0;
 lro=1000000000L;
 eof1=FALSE;
 ro=0;
 while	((got=rmtrec(LUN,BUFSIZ,b,EFN,isb,&ids)) || isb[0]==IE_EOF )
	{
/*	 printf("got=%d.\n",got);
	 for	(u=0;	u<got;	u++)
		{printf("%02x  ",b[u]&0xFF);
		}
	 ;
	 putchar('\n');
*/
	 if	(isb[0]==IE_EOF)
		{if	(fro <= ro && ro <= lro)
			{/* null record :: tape mark */
			 if	(*outfile)
				{last();
				 nextfile();
				}
			 else
				{spew(b,0,ioptr);
				}
			 ;
			}
		 ;
		 if	(eof1)
			{quit();
			}
		 else
			{eof1=TRUE;
			}
		 ;
		}
	 else
		{if	(fro <= ro && ro <= lro)
			{if	(e2a)
				{for	(u=0; u<got; u++)
					{i=b[u] & 0xFF;	/* loose sign extend */
					 b[u]=ascii[i];
					}
				 ;
				}
			 ;
			 if	(morz)
				{p=b;
				 i=0;
				 while	(i<got)
					{u=got-i;
					 if	(u>morz)	u=morz;
					 spew(p,u,ioptr);
					 p+=u;
					 i+=u;
					}
				 ;
				}
			 else
				{spew(b,got,ioptr);
				}
			 ;
			}
		 ;
		 eof1=FALSE;
		}
	 ;
	 ro++;	/* advance record counter */
	 if	(ro > lro)
		{quit();
		}
	 ;
	}
 error("\7Can't read record isb[0]=%oo isb[1]=%oo ids=%oo\n",isb[0],isb[1],ids);
}
last()
{
 fflush(ioptr);
 if	(fclose(ioptr))	error("\7Can't close %s $$ferr=%oo\n",filename,$$ferr);
}
quit()
{last();
 exit();
}
spew(buf,len,ptr)
char		*buf;	/* source of record */
unsigned	len;	/* length of record */
FILE		*ptr;	/* destination of record */
{
 fput(buf,len,ptr);
 if	(ferror(ptr))	error("\7len=%d. $$ferr=%oo\n",len,$$ferr);
 recspat++;
 if	(quantum && recspat>=quantum)
	{last();
	 nextfile();
	}
 ;
}
unsigned rmtrec(lun,bufsiz,buf,efn,isb,ids)	/* read a magtape record */
int	lun;		/* lun MT: lives on */
int	bufsiz;		/* buffer size for QIO */
char	*buf;		/* buffer to read data into */
int	efn;		/* event flag number to use */
int	isb[2];		/* i/o status block for qiow$ */
int	*ids;		/* directive status word returned */
{
 int	got;		/* return 0 or number of bytes read from record */
 int	prl[6];		/* parameter block for qiow$ */
 int	noast;		/*			 */
 int	qiow();
 noast=0;
 prl[0]=buf;
 prl[1]=bufsiz;
 *ids=qiow(IO_RLB,lun,efn,isb,noast,prl);
 if	(isb[0]==IS_SUC)
	{got=isb[1];
	}
 else
	{got=0;	/* some error */
	}
 ;
 return (got);
}
