[comp.sources.misc] v03i048: Trigraph converter

jpn@teddy.UUCP (John P. Nelson) (06/11/88)

comp.sources.misc: Volume 3, Issue 48
Submitted-By: "John P. Nelson" <jpn@teddy.UUCP>
Archive-Name: totri

#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create the files:
#	README
#	makefile
#	totri.1
#	totri.c
#	untri.1
#	untri.c
# This archive created: Fri Jun 10 14:33:50 1988
export PATH; PATH=/bin:$PATH
echo shar: extracting "'README'" '(1098 characters)'
if test -f 'README'
then
	echo shar: will not over-write existing file "'README'"
else
sed 's/^X//' << \SHAR_EOF > 'README'
XThe DRAFT ANSI C standard specifies that all conforming compilers must
Xsupport "trigraphs" as an alternate means of writing certain characters
X(which are absent from the international ISO 646 repertoire).
XPersonally, I think that trigraphs are UGLY, and absolutely the WRONG
Xway to solve this problem.  Certainly I believe that trigraph processing
Xdoes not belong in the C compiler, but would be better specified as
Xan external facility.
X
XIn any case, since the standard specifies that the conversion from
Xtrigraphs takes place in the very first processing phase, it is
Xeasy to implement trigraphs as a simple preprocessor.  I have written
Xtwo programs:  one that removes (translates) all trigraph sequences
Xinto the more usual ASCII representation, and the second, which
Xdoes the reverse (converts US-ASCII into trigraphs).
X
XI hope someone finds these useful.  I have donated the source code
Xto the public domain.  Both programs are implemented as filters,
Xbut it would be easy to change the interface by rewriting main.
XThe functions that do the work take input and outfile "FILE *"
Xdescriptors.
SHAR_EOF
if test 1098 -ne "`wc -c < 'README'`"
then
	echo shar: error transmitting "'README'" '(should have been 1098 characters)'
fi
fi
echo shar: extracting "'makefile'" '(90 characters)'
if test -f 'makefile'
then
	echo shar: will not over-write existing file "'makefile'"
else
sed 's/^X//' << \SHAR_EOF > 'makefile'
Xall: totri untri
X
Xtotri: totri.c
X	$(CC) -o $@ totri.c
Xuntri: untri.c
X	$(CC) -o $@ untri.c
SHAR_EOF
if test 90 -ne "`wc -c < 'makefile'`"
then
	echo shar: error transmitting "'makefile'" '(should have been 90 characters)'
fi
fi
echo shar: extracting "'totri.1'" '(534 characters)'
if test -f 'totri.1'
then
	echo shar: will not over-write existing file "'totri.1'"
else
sed 's/^X//' << \SHAR_EOF > 'totri.1'
X.TH totri 1   "10-Jun-88 13:59 jpn"
X.SH NAME
Xtotri \- convert a C program to ANSI C trigraphs.
X.SH SYNTAX
Xtotri < input > output
X.SH DESCRIPTION
XThis program converts all characters in the input that are representable
Xby ANSI C three-character trigraph sequences, into trigraphs.  Any existing
Xtrigraph sequences remain, unmodified.
X.PP
XPresumably this program would be used by someone with a non-US ASCII terminal
Xthat represents certain C punctuation characters as language-specific
Xalphabetic characters.
X.SH AUTHOR
XJohn P. Nelson
SHAR_EOF
if test 534 -ne "`wc -c < 'totri.1'`"
then
	echo shar: error transmitting "'totri.1'" '(should have been 534 characters)'
fi
fi
echo shar: extracting "'totri.c'" '(691 characters)'
if test -f 'totri.c'
then
	echo shar: will not over-write existing file "'totri.c'"
else
sed 's/^X//' << \SHAR_EOF > 'totri.c'
X/* totri - convert all "funny" characters to ANSI C trigraph sequences.
X *         currently implemented as a filter, but a rewritten main
X *         could allow a more sophisticated interface.
X *
X *  This source donated to the public domain by John P. Nelson 1988
X */
X
X#include <stdio.h>
X#include <strings.h>
X
Xchar *trichar = "=(/)'<!>-";
Xchar *translate = "#[\\]^{|}~";
Xmain()
X    {
X    process(stdin, stdout);
X    }
X
Xprocess(in, out)
XFILE *in, *out;
X    {
X    int c;
X    char *ptr;
X
X    while ((c = getchar(in)) != EOF)
X	{
X	if (ptr = strchr(translate, c))
X	    {
X	    putc('?', out);
X	    putc('?', out);
X	    putc(trichar[ptr - translate], out);
X	    }
X	else
X	    putc(c, out);
X	}
X    }
SHAR_EOF
if test 691 -ne "`wc -c < 'totri.c'`"
then
	echo shar: error transmitting "'totri.c'" '(should have been 691 characters)'
fi
fi
echo shar: extracting "'untri.1'" '(380 characters)'
if test -f 'untri.1'
then
	echo shar: will not over-write existing file "'untri.1'"
else
sed 's/^X//' << \SHAR_EOF > 'untri.1'
X.TH untri 1   "10-Jun-88 13:54 jpn"
X.SH NAME
Xuntri \- filter to remove ANSI C trigraph sequences.
X.SH SYNTAX
Xuntri < input > output
X.SH DESCRIPTION
XThis program converts the three character trigraph sequences defined
Xby ANSI C into the more usual single character sequences.  All other
Xcharacters are passed through unchanged.
X.SH "SEE ALSO"
Xtotri (1)
X.SH "Author"
XJohn P. Nelson
SHAR_EOF
if test 380 -ne "`wc -c < 'untri.1'`"
then
	echo shar: error transmitting "'untri.1'" '(should have been 380 characters)'
fi
fi
echo shar: extracting "'untri.c'" '(1236 characters)'
if test -f 'untri.c'
then
	echo shar: will not over-write existing file "'untri.c'"
else
sed 's/^X//' << \SHAR_EOF > 'untri.c'
X/* untri - convert ANSI C trigraph sequences to single characters.
X *         currently implemented as a filter, but a rewritten main
X *         could allow a more sophisticated interface.
X *
X *  This source donated to the public domain by John P. Nelson 1988
X */
X
X#include <stdio.h>
X#include <strings.h>
X
Xchar *trichar = "=(/)'<!>-";
Xchar *translate = "#[\\]^{|}~";
Xmain()
X    {
X    process(stdin, stdout);
X    }
X
X/*
X * Note:  I used a goto in this function, because we are essentially
X *     performing a two character lookahead, but unputc is only guaranteed
X *     to be able to push back one character.  Otherwise, the goto would be
X *     unnecessary.
X */
Xprocess(in, out)
XFILE *in, *out;
X    {
X    int c;
X    char *ptr;
X
X    while ((c = getchar(in)) != EOF)
X	{
Xreprocess:
X	if (c == '?')
X	    {
X	    if ((c = getc(in)) != '?')
X		{
X		if (c != EOF)
X		    ungetc(c, in);
X		putc('?', out);
X		continue;
X		}
X	    c = getc(in);
X	    if (c != EOF)
X		{
X		if (ptr = strchr(trichar, c))
X		    {
X		    /* yup, it's a trigraph */
X		    putc(translate[ptr - trichar], out);
X		    continue;
X		    }
X		ungetc(c, in);
X		}
X	    putc('?', out);
X	    c = '?';
X	    /* ungetc('?', in); continue; */
X	    goto reprocess;
X	    }
X	putc(c, out);
X	}
X    }
SHAR_EOF
if test 1236 -ne "`wc -c < 'untri.c'`"
then
	echo shar: error transmitting "'untri.c'" '(should have been 1236 characters)'
fi
fi
exit 0
#	End of shell archive
-- 
     john nelson

UUCP:	{decvax,mit-eddie}!genrad!teddy!jpn
smail:	jpn@genrad.com