Plan 9 from Bell Labs’s /usr/web/sources/contrib/boyd/sys/src/cmd/ralph.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include <u.h>
#include <libc.h>
#include <bio.h>
#include <ctype.h>

/*
 * ralph
 *
 * A filter to read stdin and write latin-1 diacriticals as
 * quasi troff to stdout.
 *
 * © Boyd Roberts 2004
 */

/* map latin-1 to troff diacriticals */
static Rune	*tab[256]	=
{
	[0xe0] L"`a", L"'a", L"^a", 0, 0, 0,     0, L"ç",
	[0xe8] L"`e", L"'e", L"^e", 0, 0, 0, L"^i",    0,
	[0xf0]     0,     0,     0, 0, 0, 0, L":o",    0,
};

/* troff string [diacritical] prefix */
char		*tp		= "\\*";

void
main(int argc, char *argv[])
{
	Biobuf	bin;
	Biobuf	bout;
	Biobuf	*in;
	Biobuf	*out;
	int	c;
	int	e;

	USED(argc);
	USED(argv);

	Binit(in = &bin, 0, OREAD);
	Binit(out = &bout, 1, OWRITE);
	e = 0;

	while ((c = Bgetc(in)) >= 0) {
		Rune	*r;

		if (isascii(c)) {
			/* damn windows' CRs */
			if (c != '\r' && Bputc(out, c) < 0) {
				e = 1;
				break;
			}

			continue;
		}

		if ((r = tab[c]) == 0) {
			fprint(2, "ralph: unknown latin-1 0x%02x\n", c);
			exits("update latin-1 table");
		}

		/* single Rune or troff diacritical */
		if (runestrlen(r) == 1) {
			if (Bprint(out, "%C", *r) == Beof)
				e = 1;
		}
		else if (Bprint(out, "%s%S", tp, r) == Beof)
				e = 1;

		if (e)
			break;
	}

	if (!e && Bflush(out) < 0)
		e = 1;

	exits(e ? "i/o error" : "");
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.