/*--------------------------------------------------------------------------*/
/* Program (file) name:  BSfilter.c         Copyright  1997  Miroslav Kolar */
static char *last_mod = "(C) 1997, M. Kolar (Apr. 21, 97 version)";
/* Purpose: Under HP Unix, man pages use repetitive overprinting to produce */
/*  highligted text (interpreted as inverse video by the more command on    */
/*  the screen). If a character is overprinted by a different one, on the   */
/*  screen only the top character shows. Apparently due to this feature,    */
/*  authors didn't bother to remove from some of the HP man pages (e.g.	    */
/*  from the one for ksh) characters that are invisible on the screen, but  */
/*  show up in the output printed by printPS. The purpose of this filter is */
/*  to remove such unwanted characters, that may create confusion in the    */
/*  printed output. Underline characters are never removed even if	    */
/*  "overprinted" by something else.					    */
/*--------------------------------------------------------------------------*/
/* Author: http://mkolar.org/						    */
/*--------------------------------------------------------------------------*/
/* Compilation:								    */
/*    cc -O -o BSfilter BSfilter.c					    */
/*--------------------------------------------------------------------------*/

#include <stdlib.h>
#include <stdio.h>

/* Usage:								     */
/*  BSfilter  < input_file > output_file				     */


#define PE	(void)fprintf(stderr,
#define PC(x)	(void)fputc(x,stdout)
#define GC	fgetc(stdin)
#define DUMP	for(i=0; i<cnt; i++) { PC(c0); PC(8); }


main(argc,argv) int argc; char *argv[];
/*===================================*/
{
  int  i, c0, c, cnt;

if(argc > 1) {
 PE"\n Program: %s       %s\n\n", __FILE__, last_mod);
 PE" Purpose: To remove characters that would be overprinted by others.\n");
 PE"          Underlines are not removed!\n");
 PE"          It is assumed that there are no consecutive backspaces!\n");
 PE"          Possible use: to preprocess HP Unix man pages for printPS.\n\n");
 PE" Usage:  man ... | %s | printPS -port2 -noh -bxoff -lines66 - | lpr ...\n", argv[0]);
 PE" or simply:  %s < input_file > output_file\n", argv[0]);
 exit(1);
}

cnt = 0;
if ((c0 = GC) ==  EOF ) exit(0);
while ((c = GC) !=  EOF) {
  if(c != 8) { PC(c0); c0 = c; }
  else {
	cnt = 1;
	for( ; ; ) {
		if((c = GC) == EOF) { DUMP   exit(0); }
		if(c != c0) {
			if(c0 == 95 || c == 95 || c == 10) DUMP
			c0 = c; cnt = 0; break; }
		else {
		  c = GC;
		  if(c != 8) {
			DUMP
			PC(c0);
			if(c == EOF) exit(0);
			c0 = c;
			cnt = 0;
			break;
		  } else cnt++;
		}
	}
  }
}
PC(c0);

}

