1 #ifndef lint
2 static char *rcsid = "$Id: jcode.c,v 2.3 1993/09/22 11:04:10 nao Exp $";
3 #endif
4 /*
5  * Copyright 1991 Sony Corporation
6  *
7  * Permission to use, copy, modify, distribute, and sell this software and its
8  * documentation for any purpose is hereby granted without fee, provided that
9  * the above copyright notice appear in all copies and that both that
10  * copyright notice and this permission notice appear in supporting
11  * documentation, and that the name of Sony not be used in advertising or
12  * publicity pertaining to distribution of the software without specific,
13  * written prior permission.  Sony makes no representations about the
14  * suitability of this software for any purpose.  It is provided "as is"
15  * without express or implied warranty.
16  *
17  * SONY DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL SONY
19  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
21  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23  */
24 /*
25  * Author: Naoshi Suzuki, SONY Corporation.  (nao@sm.sony.co.jp)
26  */
27 
28 #include <stdio.h>
29 #ifdef X_LOCALE
30 #include <X11/Xlocale.h>
31 #else /* X_LOCALE */
32 #include <locale.h>
33 #endif /* X_LOCALE */
34 #include "common.h"
35 
36 int code,   kana;
37 enum { ASCII, KANJI, GAIJI } g0;
38 
main(argc,argv)39 main(argc, argv)
40     int     argc;
41     char  **argv;
42 {
43     char           *locale;
44     register FILE  *in,    *out;
45     unsigned char   ibuf[BUFSIZ],   obuf[BUFSIZ];
46 
47 #ifdef X_LOCALE
48     if (locale = _Xsetlocale(LC_CTYPE, ""))
49 #else /* X_LOCALE */
50     if (locale = setlocale(LC_CTYPE, ""))
51 #endif /* X_LOCALE */
52         if (!strcmp(locale, "ja_JP.SJIS")||!strcmp(locale, "ja_JP.mscode"))
53             code = JP_SJIS;
54         else if (!strcmp(locale, "ja_JP.jis7")) code = JP_JIS7;
55         else if (!strcmp(locale, "ja_JP.jis8")) code = JP_JIS8;
56         else code = JP_EUC;
57     else code = JP_EUC;
58 #ifdef FORCE_SJIS
59     code = JP_SJIS;
60 #endif
61 #ifdef FORCE_JIS8
62     code = JP_JIS8;
63 #endif
64 #ifdef FORCE_JIS7
65     code = JP_JIS7;
66 #endif
67     if (argc < 2) {
68         in = stdin;
69         out = stdout;
70     } else {
71         if ((in = fopen (*(++argv), "r")) == NULL) {
72             perror (*argv);
73             exit (1);
74         }
75         out = stdout;
76     }
77     if (code == JP_EUC)
78         while (fgets((char *)ibuf, BUFSIZ, in) != NULL)
79             fputs((char *)ibuf, out);
80     else {
81         g0 = ASCII;
82         kana = OFF;
83         while (fgets((char *)ibuf, BUFSIZ, in) != NULL) {
84             conv(ibuf, obuf);
85             fputs((char *)obuf, out);
86             putc('\n', out);
87         }
88     }
89     exit(0);
90 }
91 
92 int
conv(p,q)93 conv(p, q)
94     register unsigned char *p, *q;
95 {
96     wchar                   c;
97 
98     while (*p != '\n' && *p != '\0') {
99         if (*p & 0x80) {
100             if (*p == SS2) {
101                 p++;
102                 if (code != JP_SJIS && g0 != ASCII) {
103                     g0 = ASCII;
104                     *q++ = ESC; *q++ = '('; *q++ = 'J';
105                 }
106                 if (code == JP_JIS7) {
107                     if (!kana) {
108                         *q++ = SO;
109                         kana++;
110                     }
111                     *q++ = *p++ & MASK;
112                 } else {
113                     *q++ = *p++;
114                 }
115             } else if (*p == SS3) {
116                 if (code == JP_JIS7 && kana) {
117                     *q++ = SI;
118                     kana = OFF;
119                 }
120                 if (code != JP_SJIS) {
121                     if (g0 != GAIJI) {
122                         g0 = GAIJI;
123                         *q++ = ESC; *q++ = '$'; *q++ = '('; *q++ = 'D';
124                     }
125                     *q++ = (*p++ & MASK); *q++ = (*p++ & MASK);
126                 } else {
127                     if (*p < 0x3b) {
128                         c = (*p << 8) | *(p + 1); p += 2;
129                         c = _Xsj3ceuc2sjis(c);
130                         *q++ = (c >> 8) + 0x6f; *q++ = c & 0xff;
131                     } else {
132                         *q++ = 0xfc; *q++ = 0xfc;
133                     }
134                 }
135             } else {
136                 if (code == JP_JIS7 && kana) {
137                     *q++ = SI;
138                     kana = OFF;
139                 }
140                 if (code != JP_SJIS) {
141                     if (g0 != KANJI) {
142                         g0 = KANJI;
143                         *q++ = ESC; *q++ = '$'; *q++ = 'B';
144                     }
145                     *q++ = (*p++ & MASK); *q++ = (*p++ & MASK);
146                 } else {
147                     c = (*p << 8) | *(p + 1); p += 2;
148                     c = _Xsj3ceuc2sjis(c);
149                     *q++ = c >> 8; *q++ = c & 0xff;
150                 }
151             }
152         } else {
153             if (code == JP_JIS7 && kana) {
154                 *q++ = SI;
155                 kana = OFF;
156             }
157             if (code != JP_SJIS && g0 != ASCII) {
158                     g0 = ASCII;
159                     *q++ = ESC; *q++ = '('; *q++ = 'J';
160             }
161             *q++ = *p++;
162         }
163     }
164     if (code == JP_JIS7 && kana) {
165         *q++ = SI;
166         kana = OFF;
167     }
168     if (code != JP_SJIS && g0 != ASCII) {
169         g0 = ASCII;
170         *q++ = ESC; *q++ = '('; *q++ = 'J';
171     }
172     *q = '\0';
173 }
174