/* stripbin.c
   22 July 1988

   Copyright (c) 1988 by Fabbian G. Dufoe, III
   All rights reserved.

   Permission is granted to redistribute this program provided the source
   code is included in the distribution and this copyright notice is
   unchanged.

   This program reads its standard input, deletes any binary characters,
   and writes to standard output.  The user may optionally have binary codes
   translated to spaces (0x20) or represented in hexadecimal notation.

   Binary characters are all the control characters (less than 0x20) except
   NULL (0x00), HORIZONTAL TAB (0x09), LINE FEED (0x0a), FORM FEED (0x0c),
   CARRIAGE RETURN (0x0d), and ESCAPE (0x1b).  Also, high ASCII characters
   from 0x7f to 0x9f are binary.  In other words, binary characters are
   those which the AmigaDOS screen editor Ed will refuse to process.

   The following command line options determine how binary characters in the
   input stream will be treated:

      -s    translate binary characters to spaces.
      -h    represent binary characters in hexadecimal notation.

*/

#include <stdio.h>
#include <fcntl.h>

int
main(argc, argv)
int argc;
char **argv;
{
   int c;
   void puthex(int);

   c = getchar();
   while (c != EOF)
   {
      if (((c > 0x00) && (c < 0x09)) ||
          (c == 0x0b) ||
          ((c > 0x0d) && (c < 0x1b)) ||
          ((c > 0x1b) && (c < 0x20)) ||
          ((c > 0x7e) && (c < 0xa0)))
      {
         if (argv[1][1] == 's')
            putchar(0x20);
         else if (argv[1][1] == 'h')
            puthex(c);
      }
      else
         putchar(c);
      c = getchar();
   }
   return(0);
}

void
puthex(c)
int c;
{
   int h;
   int l;
   static char list[] = "0123456789abcdef";

   h = c / 16;
   l = c % 16;
   putchar('0');
   putchar('x');
   putchar(list[h]);
   putchar(list[l]);
   return;
}
