1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (C) 1993-98 by Andrey A. Chernov, Moscow, Russia.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include "lp.cdefs.h"		/* A cross-platform version of <sys/cdefs.h> */
30 __FBSDID("$FreeBSD$");
31 
32 /*
33  * KOI8-R -> CP866 conversion filter (Russian character sets)
34  */
35 
36 #include <sys/types.h>
37 #include <signal.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 
42 int length = 66;
43 int lines;
44 
45 char *koi2alt[] = {
46 /*         0      1      2      3      4      5      6      7   */
47 /*         8      9      A      B      C      D      E      F   */
48 /* 8 */ "\xc4","\xb3","\xda","\xbf","\xc0","\xd9","\xc3","\xb4",
49 	"\xc2","\xc1","\xc5","\xdf","\xdc","\xdb","\xdd","\xde",
50 /* 9 */ "\xb0","\xb1","\xb2","\xb3","\xfe","\xf9","\xfb","-\b~",
51 	"<\b_",">\b_","\xff","\xb3","\xf8","2\b-","\xfa",":\b-",
52 /* A */ "\xcd","\xba","\xd5","\xf1","\xd6","\xc9","\xb8","\xb7",
53 	"\xbb","\xd4","\xd3","\xc8","\xbe","\xbd","\xbc","\xc6",
54 /* B */ "\xc7","\xcc","\xb5","\xf0","\xb6","\xb9","\xd1","\xd2",
55 	"\xcb","\xcf","\xd0","\xca","\xd8","\xd7","\xce","c\b_",
56 /* C */ "\xee","\xa0","\xa1","\xe6","\xa4","\xa5","\xe4","\xa3",
57 	"\xe5","\xa8","\xa9","\xaa","\xab","\xac","\xad","\xae",
58 /* D */ "\xaf","\xef","\xe0","\xe1","\xe2","\xe3","\xa6","\xa2",
59 	"\xec","\xeb","\xa7","\xe8","\xed","\xe9","\xe7","\xea",
60 /* E */ "\x9e","\x80","\x81","\x96","\x84","\x85","\x94","\x83",
61 	"\x95","\x88","\x89","\x8a","\x8b","\x8c","\x8d","\x8e",
62 /* F */ "\x8f","\x9f","\x90","\x91","\x92","\x93","\x86","\x82",
63 	"\x9c","\x9b","\x87","\x98","\x9d","\x99","\x97","\x9a"
64 };
65 
66 int main(int argc, char *argv[])
67 {
68 	int c, i;
69 	char *cp;
70 
71 	while (--argc) {
72 		if (*(cp = *++argv) == '-') {
73 			switch (*++cp) {
74 			case 'l':
75 				if ((i = atoi(++cp)) > 0)
76 					length = i;
77 				break;
78 			}
79 		}
80 	}
81 
82 	while ((c = getchar()) != EOF) {
83 		if (c == '\031') {
84 			if ((c = getchar()) == '\1') {
85 				lines = 0;
86 				fflush(stdout);
87 				kill(getpid(), SIGSTOP);
88 				continue;
89 			} else {
90 				ungetc(c, stdin);
91 				c = '\031';
92 			}
93 		} else if (c & 0x80) {
94 			fputs(koi2alt[c & 0x7F], stdout);
95 			continue;
96 		} else if (c == '\n')
97 			lines++;
98 		else if (c == '\f')
99 			lines = length;
100 		putchar(c);
101 		if (lines >= length) {
102 			lines = 0;
103 			fflush(stdout);
104 		}
105 	}
106 	return 0;
107 }
108