koi2alt.c revision 116075
133088Sache/*
233088Sache * Copyright (C) 1993-98 by Andrey A. Chernov, Moscow, Russia.
333088Sache * All rights reserved.
433088Sache *
533088Sache * Redistribution and use in source and binary forms, with or without
633088Sache * modification, are permitted provided that the following conditions
733088Sache * are met:
833088Sache * 1. Redistributions of source code must retain the above copyright
933088Sache *    notice, this list of conditions and the following disclaimer.
1033088Sache * 2. Redistributions in binary form must reproduce the above copyright
1133088Sache *    notice, this list of conditions and the following disclaimer in the
1233088Sache *    documentation and/or other materials provided with the distribution.
1333088Sache *
1433088Sache * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
1533088Sache * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1633088Sache * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1733088Sache * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1833088Sache * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1933088Sache * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2033088Sache * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2133088Sache * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2233088Sache * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2333088Sache * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2433088Sache * SUCH DAMAGE.
25116075Simp *
26116075Simp * $FreeBSD: head/usr.sbin/lpr/filters.ru/koi2alt/koi2alt.c 116075 2003-06-09 06:22:01Z imp $
2733088Sache */
2833088Sache
2933088Sache/*
3033088Sache * KOI8-R -> CP866 conversion filter (Russian character sets)
3133088Sache */
3233088Sache
3333088Sache#include <sys/types.h>
3433088Sache#include <signal.h>
3533088Sache#include <stdio.h>
36116075Simp#include <stdlib.h>
3733088Sache#include <unistd.h>
3833088Sache
3933088Sacheint length = 66;
4033088Sacheint lines;
4133088Sache
4233088Sachechar *koi2alt[] = {
4333088Sache/*         0      1      2      3      4      5      6      7   */
4433088Sache/*         8      9      A      B      C      D      E      F   */
4533088Sache/* 8 */ "\xc4","\xb3","\xda","\xbf","\xc0","\xd9","\xc3","\xb4",
4633088Sache	"\xc2","\xc1","\xc5","\xdf","\xdc","\xdb","\xdd","\xde",
4733088Sache/* 9 */ "\xb0","\xb1","\xb2","\xb3","\xfe","\xf9","\xfb","-\b~",
4833088Sache	"<\b_",">\b_","\xff","\xb3","\xf8","2\b-","\xfa",":\b-",
4933088Sache/* A */ "\xcd","\xba","\xd5","\xf1","\xd6","\xc9","\xb8","\xb7",
5033088Sache	"\xbb","\xd4","\xd3","\xc8","\xbe","\xbd","\xbc","\xc6",
5133088Sache/* B */ "\xc7","\xcc","\xb5","\xf0","\xb6","\xb9","\xd1","\xd2",
5233088Sache	"\xcb","\xcf","\xd0","\xca","\xd8","\xd7","\xce","c\b_",
5333088Sache/* C */ "\xee","\xa0","\xa1","\xe6","\xa4","\xa5","\xe4","\xa3",
5433088Sache	"\xe5","\xa8","\xa9","\xaa","\xab","\xac","\xad","\xae",
5533088Sache/* D */ "\xaf","\xef","\xe0","\xe1","\xe2","\xe3","\xa6","\xa2",
5633088Sache	"\xec","\xeb","\xa7","\xe8","\xed","\xe9","\xe7","\xea",
5733088Sache/* E */ "\x9e","\x80","\x81","\x96","\x84","\x85","\x94","\x83",
5833088Sache	"\x95","\x88","\x89","\x8a","\x8b","\x8c","\x8d","\x8e",
5933088Sache/* F */ "\x8f","\x9f","\x90","\x91","\x92","\x93","\x86","\x82",
6033088Sache	"\x9c","\x9b","\x87","\x98","\x9d","\x99","\x97","\x9a"
6133088Sache};
6233088Sache
6333088Sacheint main(int argc, char *argv[])
6433088Sache{
6533088Sache	int c, i;
6633088Sache	char *cp;
6733088Sache
6833088Sache	while (--argc) {
6933088Sache		if (*(cp = *++argv) == '-') {
7033088Sache			switch (*++cp) {
7133088Sache			case 'l':
7233088Sache				if ((i = atoi(++cp)) > 0)
7333088Sache					length = i;
7433088Sache				break;
7533088Sache			}
7633088Sache		}
7733088Sache	}
7833088Sache
7933088Sache	while ((c = getchar()) != EOF) {
8033088Sache		if (c == '\031') {
8133088Sache			if ((c = getchar()) == '\1') {
8233088Sache				lines = 0;
8333088Sache				fflush(stdout);
8433088Sache				kill(getpid(), SIGSTOP);
8533088Sache				continue;
8633088Sache			} else {
8733088Sache				ungetc(c, stdin);
8833088Sache				c = '\031';
8933088Sache			}
9033088Sache		} else if (c & 0x80) {
9133088Sache			fputs(koi2alt[c & 0x7F], stdout);
9233088Sache			continue;
9333088Sache		} else if (c == '\n')
9433088Sache			lines++;
9533088Sache		else if (c == '\f')
9633088Sache			lines = length;
9733088Sache		putchar(c);
9833088Sache		if (lines >= length) {
9933088Sache			lines = 0;
10033088Sache			fflush(stdout);
10133088Sache		}
10233088Sache	}
10333088Sache	return 0;
10433088Sache}
105