[comp.unix.xenix] ANSI filter for Xenix

davidsen@steinmetz.steinmetz.UUCP (William E. Davidsen Jr) (01/13/88)

I have had dozens of requests for the ANSI filter which allows Xenix to
work with (most) sequences designed for ansi.sys or VT100. Here it is
complete with a man page. As usual, noncommercial use is permitted.

:
#!/bin/sh
# shar+ created from directory /usr2/davidsen/tests
# 12:17 on Thu Jan 07, 1988 by davidsen
echo 'x - afilter.1 (text)'
sed << 'E!O!F' 's/^X//' > afilter.1
X'\" @(#)skeleton	3.3 - 12/21/83
X'\" [c][e][t] (only if preprocessing by cw, eqn, and/or tbl required)
X.\.TH name section_number paren_item heading_center
X.TH afilter 1 "*IX BBS"
X'\" Heading: name(sect)    center (paren)    name(sect)
X.SH NAME
X.\name [, name] ... \- brief description on a single line of \s-1INPUT\s+1
Xafilter \- filter ANSI sequences for the Xenix ANSI console driver
X.SH SYNOPSIS
X.B afilter
Xreads standard input (usually a pipe from a terminal emulator program)
Xand modifies the output to display most things correctly on the Xenix
XANSI console driver.
X.SH DESCRIPTION
XThe standard ANSI filter does not handle multiple attribute changes
Xcorrectly. It will accept the form
X.I "ESC value m
Xbut will not handle the sequence
X.I "ESC value ; value m
Xbeyond the first value. Since the VT100 and IBM PC ANSI.SYS drivers will
Xaccept this sequence, many systems generate it.
X.SS Translation examples
X.in +.5i
X.DS
XInput		Output		(\e means ESCAPE or \033)
X\e30;47m	\e30m\e47m	black on white
X\e0;1m		\e0m\e1m	clear and set bold
X\e1;31;44m	\e1m\e31m\e44m	bold red on green
X.DE
X.in -.5i
X.SS Using the filter
XThe filter is normally used on the end of a pipe driven by a terminal
Xemulator. It may also be used on a file, with output going to stdout.
X.SS Other display issues
XMany public access systems generate graphics characters using characters
Xin the range 128-255.  These generate graphics characters on an IBM PC.
XIf you aren't running Xenix on a PC compatible you probably won't be
Xable to display these characters. If you are using a PC and still don't
Xget graphics, you are probably getting the 8th bit removed. This can be
Xmodified in the source (such as for Kermit), or you can try options in
Xyour emulator.
X.SH EXAMPLES
X.in +.5i
X cu -s 2400 18005551212 | afilter
X
X kermit | afilter
X
X afilter < rawfile
X.in -.5i
X.SH SEE ALSO
XDocumentation on your terminal emulator.
X.SH LIMITATIONS
XThis version will not accept a filename on the command line, but must
Xalways be used as a filter.
X.SH AUTHOR
XBill Davidsen (davidsen@crdos1.uucp, davidsen@ge-crd.arpa)
X.SH COPYRIGHT
XCopyright (c) 1987 by Bill Davidsen. This documentation and software may
Xbe used by any person for any purpose as long as the original author is
Xcredited.
X'\" For more details, see man(7), as well as man(1), manroff(1), and mmt(1)
E!O!F
newsize=`wc -c < afilter.1`
if [ $newsize -ne 2308 ]
then echo "File afilter.1 was $newsize bytes, 2308 expected"
fi
echo 'x - afilter.c (text)'
sed << 'E!O!F' 's/^X//' > afilter.c
X/*
X *  afilter - filter ANSI sequences
X *
X *  Xenix 2.1.3 does not handle ANSI color change sequences correctly.
X *  Sequences with several attributes (ex: ^[[32;40m) don't work as
X *  expected. This filter catches and corrects that.
X *
X *  This filter can be used as a post processor for comm programs
X *  by commands like "kermit | afilter".
X */
X 
X#include <stdio.h>
X#include <ctype.h>
X
X#define ESC     '\033'
X#define EOS     '\0'
X
Xstatic char SCCSid[] = {"@(#)afilter 1.2, (c) 1987 bill davidsen"};
X
Xmain()
X{
X    char line[80];          /* buffer for line */
X    char *ptr1, *ptr2;      /* buffer control pointers */
X    register int ch;        /* current char */
X
X    /* set unbuffered input */
X    setbuf (stdin, NULL);
X
X    /* look for an escape inducer */
X    while ((ch = getc(stdin)) != EOF)
X    { /* check sequences */
X        if (ch == ESC)
X	{ /* check an escape sequence */
X	    putchar(ESC);
X	    putchar (ch = getchar());
X	    if (ch != '[') continue;
X	    
X            /* copy data */
X	    ptr1 = line;
X            while ((ch = getchar()) == ';' || isdigit(ch))
X                *(ptr1++) = ch;
X	    *(ptr1++) = ch;
X	    *ptr1 = EOS;
X
X            if (ch == 'm')
X            { /* this is what we need to fix */
X	        *ptr1 = EOS;
X                ptr2 = line;
X                while (ch = *(ptr2++))
X                { /* break up sequences */
X                    if (ch == ';')
X                        fputs("m\033[", stdout);
X                    else putchar(ch);
X                }
X            }
X            else
X            { /* some other sequence, don't change */
X                fputs(line, stdout);
X            }
X        }
X	else putchar(ch);	/* normal char */
X    }
X}
E!O!F
newsize=`wc -c < afilter.c`
if [ $newsize -ne 1688 ]
then echo "File afilter.c was $newsize bytes, 1688 expected"
fi
exit 0

-- 
	bill davidsen		(wedu@ge-crd.arpa)
  {uunet | philabs | seismo}!steinmetz!crdos1!davidsen
"Stupidity, like virtue, is its own reward" -me