#include <ascii.h>
#include <xfbuf.h>
#include <fcntl.h>
#include <sys\types.h>
#include <sys\stat.h>

char *strip_path();

/*
	Convert a text file to plain ASCII text. Tabs are converted
	to the right number of spaces, CRs are ignored, LFs and soft-CRs
	are changed to CR/LFs, PMATE soft-hyphen changed to  -CR/LF.

	Tom Jennings
	21 July 91
*/

#define WORKFILE "ASCIIZE$.$$$"

main(argc,argv)
int argc;
char **argv;
{
struct _xfbuf xfbuf;
char buff[80];
int filelist,i;

	filelist= -1;					/* no work file yet */
	i= xfbuf.s_attrib= 0;				/* normal files only */
	while (_find(argv[1],i,&xfbuf)) {
		++i;
		if (strcmp(xfbuf.name - 2,WORKFILE) == 0) continue;
		strip_path(buff,argv[1]);		/* put in the path, */
		strcat(buff,xfbuf.name - 2);		/* the file name, */
		strcat(buff,"\r\n");			/* and a cr/lf for rline() */
		if (filelist == -1) 
			filelist= open(WORKFILE,O_CREAT | O_TRUNC | O_BINARY | O_RDWR,S_IREAD | S_IWRITE); 
		if (filelist == -1) error("Can't create work file %s\r\n",WORKFILE);
		write(filelist,buff,strlen(buff));
	}
	close(filelist);

	if (i) {
		filelist= open(WORKFILE,O_BINARY | O_RDWR,S_IREAD | S_IWRITE); 
		while (rline(filelist,buff,sizeof(buff))) {
			process(buff);
		}
		close(filelist);
	}
	unlink(WORKFILE);

	if (i == 0) printf("File missing or spelled wrong. Try:\r\n    ASCIIZE <filename.ext>\r\n    (wildcards allowed)\r\n    Tom Jennings 2 June 91\r\n");
	else printf("ASCIIZEd %u files.\r\n",i);
	exit(i == 0 ? 1 : 0);
}

static process(fn)
char *fn;		/* input filename */
{
int column;				/* current column */
int width;				/* maximum width */
int count;
char *cp;
char word[80];				/* word we build before output */
int i;					/* index (length) of word */
char c,lastc;				/* current and previous character */
char rptchar;				/* repeat last char (TAB expansion) */
unsigned input_count,output_count;
int in,out;

	printf("File: %s",fn);
	in= open(fn,O_BINARY | O_RDWR,S_IREAD | S_IWRITE); 
	if (in == -1) return;

	out= open("ASCIIZE.$$$",O_CREAT | O_TRUNC | O_BINARY | O_RDWR,S_IREAD | S_IWRITE); 
	if (out == -1) error("Can't create temporary file ASCIIZE.$$$\r\n");

/* Now process the input file. */

	width= 70;
	column= 0;					/* set initial states */
	rptchar= 0;
	output_count= input_count= 0;

	c= CR;

	while (1) {
		word[i= 0]= NUL;			/* word is empty */
		while (i < width) {			/* word at a time */
			lastc= c;			/* remember last character */
			while (1) {
				if (rptchar != 0) {
					--rptchar;

				} else {		/* read a character, */
					count= read(in,&c,1);
					if (! count) break;/* EOF */
					input_count += count;
				}
				switch (c) {
					case 173: c= '-'; break; /* PMATE soft-hyphen */
					case SUB: c= NUL; count= 0; break;
					case CR: break;
					case CR+128: c= CR; break;

/* (Deletes soft-CRs)			case CR + 128: if (lastc == ' ') c= NUL;
						else c= ' '; break;
*/
					case LF: c= NUL; break; /* LFs ignored */
					case TAB: break;
					default:
						if (c < ' ') c= NUL;
						else if (c > 128) c &= 127;
						break;
				}
				if (c) break;		/* got a real one or CR */
			}
			if (c == TAB) {
				rptchar= 8 - ((column + i) % 8);
				c= ' ';			/* set the character, */
				continue;		/* then go do it */
			}
			if (count == 0) break;		/* end of file, word too */
			if (c == CR) break;		/* end of (the) wor(l)d */

			word[i++]= c;			/* build the word */
			word[i]= NUL;			/* for output */
			if (c == ' ') break;
		}

/* If this word would exceed the width, wrap it now. */

		if (column + i > width) {
			if (write(out,"\r\n",2) != 2) error("r\nDISK FULL!!\r\n");
			output_count += 2;
			column= 0;
		}
		if (write(out,word,i) != i)		/* output the word */
			error("\r\nDISK FULL!!\r\n");
		output_count += i;

		if (c == CR) {				/* if a hard CR, */
			if (write(out,"\r\n",2) != 2)	/* do a CR LF */
				error("\r\nDISK FULL!!\r\n");
			output_count += 2;
			column= 0;
		}
		if (count == 0) break;			/* check EOF */
	}
	close(in); close(out);
	unlink(fn);					/* kill original */
	rename("ASCIIZE.$$$",fn);			/* rename */
	printf(", read %u characters, wrote %u\r\n",
	    input_count,output_count);
}

error(s)
char *s;
{
	printf(s);
	exit(1);
}

