/* TX.C		Transmits binary files via console output
		Usage:	TX -options filename [filename...]
		Options:   -v	veto before transfer
			   -n	do not signal End of Transmission
		or:     TX -e
			signal only End of Transmission (no file transfer)
		Note:	filename can be an ambiguous file name
			Up to 32 file names can be given

		TX.C must be linked with 
		- GFN.REL  to get the (first or next, if ambiguous) file name
		- TX1.REL  to transmit one file

06 Feb 1988	Andreas Herrmann
*/

#include "stdio.h"

#define TRUE	1
#define FALSE	0

#define FMAX	32	/* max. number of input file names	*/
#define	FNLEN	15	/* max. file name length		*/


main(argc,argv)
int argc;
char *argv[];
{
	char	names[FNLEN][FMAX];	/* save copies of file names	*/
	char	*filename;		/* pointers to file name copy	*/
	char	*fname;			/* pointer to current file name	*/
	int	nf;			/* number of file names		*/
	FILE	*fp;			/* file pointer			*/
	char	*s;			/* string pointer		*/
	char	ch;			/* input char			*/
	int	veto = FALSE;		/* TRUE iff veto before transmit*/
	int	eot = TRUE;		/* TRUE iff to signal End Of Tr.*/
	int	sndeot = FALSE;		/* TRUE iff only to signal EOT	*/
	int	quit = FALSE;		/* TRUE iff to abort transm.	*/
	int	txmit;			/* TRUE iff file to be transm'd	*/

	char	*getffn(), *getnfn();	/* gets first, next file name	*/
	char	gc();			/* gets console character	*/
	int	tx1();			/* transmits one file		*/

	while (--argc > 0 && (*++argv)[0] == '-')
		for (s = argv[0]+1; *s != '\0'; s++)
			switch (*s) {
			case 'V':	veto = TRUE;
					break;
			case 'N':	eot = FALSE;
					break;
			case 'E':	sndeot = TRUE;
					break;
			default:
				printf("Illegal option %c\n", *s);
				argc = 0;
				sndeot = FALSE;
				break;
			}

	if (argc == 0) {
		if (sndeot)	eot=TRUE;
		else {
			printf("Usage: TX -v -n filename [filename...]\n");
			printf("or:    TX -e\n");
			exit(1);
		}
	}
	nf = argc;
	filename = names;
	while (argc--) 
		filename = strcpy(filename,*argv++) + FNLEN;
	filename = names;
	while (!quit && nf--) {
	    fname = getffn(filename);
	    do {
		if ((fp = fopen(fname, "r")) == NULL)
			printf("Can't open file %s\n", filename);
		else {
			fclose(fp);
			txmit = TRUE;
			if (veto) {
askveto:			printf("\nTransmit %s ? (Yes/No/Quit/All) [N] "
					,fname);
				switch (ch=gc('N')) {
				case 'Q':	quit=TRUE;	/* fall thru */
				case 'N':	txmit=FALSE;
						break;
				case 'A':	veto = FALSE;
				case 'Y':	break;
				default:	goto askveto;
				}
			}
			if (txmit) {
				printf("\nTransmitting %s\n",fname);
				tx1(fname);
			}
		}
	    } while (!quit && (fname=getnfn()) );
	    filename += FNLEN;
	} 
	if (eot) {
		tx1("");
		printf("\nEOT signalled");
	}
}


char gc(ch)	/* gets character directly from console	*/
char ch;	/* ch is default char on CR		*/
{
	int c;

	while (!( c = _abdos(6,0xFF) )) ;	/* wait for char	*/
	if (c==0x0D) c=ch;			/* on CR: default	*/
	_bdos(6,(c=toupper(c)));		/* echo in upper case	*/
	return c;
}
