/*
 *	filter [-tc] [-ic] [n]
 *
 * field utility, originally from Bourne pp.228-9.  Distributed on
 * USENET, July 1985.  Rewritten.
 */


/*)BUILD	$(TKBOPTIONS) = {
			TASK	= ...FLT
		}
*/

#ifdef DOCUMENTATION

title	filtr	Remove hi bits and control chars
index		Remove hi bits and control chars

synopsis

	filtr

description

	The filtr command reads stdin, strips off the high bit
	of every character, and emits to stdout. All control
	characters except \n, \r, \t, and \f (cr, lf, ff, and ht)
	are ignored and not sent through. The result is a "cleaned
	up" file that is for the most part guaranteed printable.
authors

	Glenn Everhart from filter by Martin Minow.
#endif

#include <stdio.h>
#ifdef vms
#include		<ssdef.h>
#include		<stsdef.h>
#define	IO_SUCCESS	(SS$_NORMAL | STS$M_INHIB_MSG)
#define	IO_ERROR	SS$_ABORT
#endif
/*
 * Note: IO_SUCCESS and IO_ERROR are defined in the Decus C stdio.h file
 */
#ifndef	IO_SUCCESS
#define	IO_SUCCESS	0
#endif
#ifndef	IO_ERROR
#define	IO_ERROR	1
#endif

main(argc,argv)
int		argc;
char		*argv[];
{
	register int	c;
	register char  ch;
#ifdef vms
	argc = getredirection(argc, argv);
#endif
	while (!feof(stdin)) {
	    while ((c = getchar()) != '\n' && c != EOF) {

/* emit the char after removing hi bit and ctl chars other than */
/* 9 (ht), 10 (lf), 13 (cr), and 12 (ff) */
	ch = c & 127;	/* mask off high bit */
/* emit it if not one of the permitted control chars */
     if ((ch >=' ')&&(ch != '\r' && ch != '\n' && ch != '\t' && ch != '\f')) {
/* Also throw out DEL characters */
		if (ch != '\0177')
		putchar(ch);
               }		
	    }
	    if (c == EOF)
		break;
	    if (c == '\n')
		putchar('\n');
	}
	return (IO_SUCCESS);
}
