koi2855.c revision 116076
1/*
2 * Copyright (C) 1999 by Andrey A. Chernov, Moscow, Russia.
3 * (C) 18 Sep 1999, Alex G. Bulushev (bag@demos.su)
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * $FreeBSD: head/usr.sbin/lpr/filters.ru/koi2855/koi2855.c 116076 2003-06-09 06:23:10Z imp $
28 */
29
30/*
31 * KOI8-R -> CP855 conversion filter (Russian character sets)
32 *
33 * $FreeBSD: head/usr.sbin/lpr/filters.ru/koi2855/koi2855.c 116076 2003-06-09 06:23:10Z imp $
34 */
35
36#include <sys/types.h>
37#include <signal.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <unistd.h>
41
42int length = 66;
43int lines;
44
45unsigned char koi2855 [] = {
460xc4, 0xb3, 0xda, 0xbf, 0xc0, 0xd9, 0xc3, 0xb4,
470xc2, 0xc1, 0xc5, 0xdf, 0xdc, 0xdb, 0xdb, 0xdb,
480xb0, 0xb1, 0xb2, 0xda, 0xfe, 0x2e, 0xad, 0x3d,
490xae, 0xaf, 0xff, 0xd9, 0xcf, 0x32, 0x2e, 0x25,
500xcd, 0xba, 0xc9, 0x84, 0xc9, 0xc9, 0xbb, 0xbb,
510xbb, 0xc8, 0xc8, 0xc8, 0xbc, 0xbc, 0xbc, 0xcc,
520xcc, 0xcc, 0xb9, 0x85, 0xb9, 0xb9, 0xcb, 0xcb,
530xcb, 0xca, 0xca, 0xca, 0xce, 0xce, 0xce, 0x43,
540x9c, 0xa0, 0xa2, 0xa4, 0xa6, 0xa8, 0xaa, 0xac,
550xb5, 0xb7, 0xbd, 0xc6, 0xd0, 0xd2, 0xd4, 0xd6,
560xd8, 0xde, 0xe1, 0xe3, 0xe5, 0xe7, 0xe9, 0xeb,
570xed, 0xf1, 0xf3, 0xf5, 0xf7, 0xf9, 0xfb, 0x9e,
580x9d, 0xa1, 0xa3, 0xa5, 0xa7, 0xa9, 0xab, 0xad,
590xb6, 0xb8, 0xbe, 0xc7, 0xd1, 0xd3, 0xd5, 0xd7,
600xdd, 0xe0, 0xe2, 0xe4, 0xe6, 0xe8, 0xea, 0xec,
610xee, 0xf2, 0xf4, 0xf6, 0xf8, 0xfa, 0xfc, 0x9f
62};
63
64int main(int argc, char *argv[])
65{
66	int c, i;
67	char *cp;
68
69	while (--argc) {
70		if (*(cp = *++argv) == '-') {
71			switch (*++cp) {
72			case 'l':
73				if ((i = atoi(++cp)) > 0)
74					length = i;
75				break;
76			}
77		}
78	}
79
80	while ((c = getchar()) != EOF) {
81		if (c == '\031') {
82			if ((c = getchar()) == '\1') {
83				lines = 0;
84				fflush(stdout);
85				kill(getpid(), SIGSTOP);
86				continue;
87			} else {
88				ungetc(c, stdin);
89				c = '\031';
90			}
91		} else if (c & 0x80) {
92			putchar(koi2855[c & 0x7F]);
93			continue;
94		} else if (c == '\n')
95			lines++;
96		else if (c == '\f')
97			lines = length;
98		putchar(c);
99		if (lines >= length) {
100			lines = 0;
101			fflush(stdout);
102		}
103	}
104	return 0;
105}
106