#include <stdio.h>
#include <ctype.h>

/*
 *	dtty: convert ditroff intermediate code into coarse unadjusted text
 */

main(argc,argv)
char **argv;
{
	register FILE *fp;
	register int i;

	if (argc == 1) {
		conv( stdin );
	} else {
		for (i=1; i<argc; i++) {
			if ( (fp = fopen(argv[i],"r")) == NULL ) {
				fprintf(stderr,"dtty: Cannot open %s\n", argv[i]);
				exit(1);
			} else {
				conv(fp);
}	}	}	}

conv(fp)
FILE *fp;
{
	static int firstfile = 1;
	register int c, i;
	int n;
	char str[100];

	if (!firstfile) {
		putchar('\f');
	} else {
		firstfile = 0;
	}
	while ((c = getc(fp)) != EOF) {
		switch (c) {
		case '\n':	/* when input is text */
		case ' ':
		case 0:		/* occasional noise creeps in */
			break;
		case '0': case '1': case '2': case '3': case '4':
		case '5': case '6': case '7': case '8': case '9':
			/* two motion digits plus a character */
			(void) getc(fp);	/* the 2nd digit */
			(void) putchar(getc(fp));
			break;
		case 'c':	/* single ascii character */
			(void) putchar(getc(fp));
			break;
		case 'C':
			(void) fscanf(fp, "%s", str);
			printf("\\(%s",str);
			break;
		case 's':
			(void) fscanf(fp, "%d", &n);	/* ignore fractional sizes */
			break;
		case 'f':
			(void) fscanf(fp, "%s", str);
			break;
		case 'H':	/* absolute horizontal motion */
			while (isdigit(c = getc(fp)));
			(void) ungetc(c, fp);
			break;
		case 'h':	/* relative horizontal motion */
			while (isdigit(c = getc(fp)));
			(void) ungetc(c, fp);
			break;
		case 'w':	/* word space */
			(void) putchar(' ');
			break;
		case 'V':
			(void) fscanf(fp, "%d", &n);
			break;
		case 'v':
			(void) fscanf(fp, "%d", &n);
			break;
		case 'p':	/* new page */
			(void) fscanf(fp, "%d", &n);
			for (i=0; i<79; i++) (void) putchar('-');
			putchar('\n');
			break;
		case 'n':	/* end of line */
			while (getc(fp) != '\n')
				;
			(void) putchar('\n');
			break;
		case 'D':	/* Drawing function */
		case '#':	/* comment */
		case 'x':	/* device control */
			while(getc(fp) != '\n')
				;
			break;
		default:
			fprintf(stderr, "dtty: unknown input character %o '%c'\n", c, c);
			exit(1);
		}
	}
	for (i=0; i<79; i++) (void) putchar('-');
	putchar('\n');
}
