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