/*	nroff -man foo.8 | stripman > foo.d
*
* This program strips out assorted control  chars  that  cause  problems  with
* manual  pages.  The main problem is all the _^Hx trigraphs that are produced
* for underlining.  Another problem is an assortment of ^L  and  ^[7  and  ^[9
* sequences,  which we just drop.  The intent is to produce a version that can
* be dumped to any dumb ASCII terminal or printer and give a readable text. We
* also strip out any nulls.
*/
#include "V.h"

main()
{	int   c, c0=0, c1=0, c2=0;

	while ((c = getchar()) != EOF) {
		if (c0) putchar(c0);
		c0 = c1;
		c1 = c2;
		c2 = c;
		switch (c1) {
		  case 0x08:	/* ^H Backspace */
			if      (c0 == '_') c0 = c1 = 0;	/* _^HX underscore */
			else if (c2 == '_') c1 = c2 = 0;	/* X^H_ underscore */
			else                c0 = c1 = 0;	/* X^HX bold */
			break;
		  case 0x0c:	/* ^L Page eject */
			c1 = 0;
			break;
		  case 0x1B:	/* ESC */
			switch (c2) {
			  case 0x37:	/* ESC-7 */
				c1 = c2 = 0;
				break;
			  case 0x39:	/* ESC-9 */
				c1 = c2 = 0;
				break;
			  case 0x5B:	/* ESC-[ */
				c1 = c2 = 0;
				while (c = getchar()) {	/* Gobble chars: */
					if (isdigit(c)) continue;	/* Eat string of digits */
					if (c == 'm') break;	/* Eat up a terminating 'm' */
					ungetc(c,stdin);	/* Anything else is preserved */
					break;
				}
				break;
			}
		}
	}
	if (c2 == 0x0c) c2 = 0;	/* Strip final page eject */
	if (c0) putchar(c0);
	if (c1) putchar(c1);
	if (c2) putchar(c2);
	exit(0);
}
