annotate sbr/ml_codeconv.c @ 7:c20e4181370f

utf-8 input assumption in case of base64/utf-8
author kono
date Sun, 04 Dec 2005 02:30:39 +0900
parents d802748a597d
children 77780b728543
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
bce86c4163a3 Initial revision
kono
parents:
diff changeset
1 /* ml_codeconv.c - (multilingual) code conversion */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
2 /* by takada@seraph.ntt.jp */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
3 /* arranged by MH-plus project */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
4
bce86c4163a3 Initial revision
kono
parents:
diff changeset
5 #ifdef JAPAN
bce86c4163a3 Initial revision
kono
parents:
diff changeset
6
bce86c4163a3 Initial revision
kono
parents:
diff changeset
7 #include "../h/mh.h"
bce86c4163a3 Initial revision
kono
parents:
diff changeset
8 #include <ctype.h>
bce86c4163a3 Initial revision
kono
parents:
diff changeset
9 #include <stdio.h>
bce86c4163a3 Initial revision
kono
parents:
diff changeset
10 #include <sys/types.h>
bce86c4163a3 Initial revision
kono
parents:
diff changeset
11 #include <sys/stat.h>
bce86c4163a3 Initial revision
kono
parents:
diff changeset
12
bce86c4163a3 Initial revision
kono
parents:
diff changeset
13 /* coding system */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
14 typedef int coding_system_t;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
15 #define CS_DEFAULT 0
bce86c4163a3 Initial revision
kono
parents:
diff changeset
16 #define CS_JIS7 1
bce86c4163a3 Initial revision
kono
parents:
diff changeset
17 #define CS_JEUC 2
bce86c4163a3 Initial revision
kono
parents:
diff changeset
18 #define CS_SJIS 3
2
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
19 #define CS_UTF8 4
0
bce86c4163a3 Initial revision
kono
parents:
diff changeset
20 #define CS_NOCONV 99
bce86c4163a3 Initial revision
kono
parents:
diff changeset
21
bce86c4163a3 Initial revision
kono
parents:
diff changeset
22 /* coding system list */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
23 #define CSL_SIZE 3
bce86c4163a3 Initial revision
kono
parents:
diff changeset
24 #define CSL_DISPLAY(csl) ((csl)[0])
bce86c4163a3 Initial revision
kono
parents:
diff changeset
25 #define CSL_FILE(csl) ((csl)[1])
bce86c4163a3 Initial revision
kono
parents:
diff changeset
26 #define CSL_PROCESS(csl) ((csl)[2])
bce86c4163a3 Initial revision
kono
parents:
diff changeset
27
bce86c4163a3 Initial revision
kono
parents:
diff changeset
28 /* codeset status */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
29 #define ASCII 0
bce86c4163a3 Initial revision
kono
parents:
diff changeset
30 #define JISX0208 1
bce86c4163a3 Initial revision
kono
parents:
diff changeset
31
bce86c4163a3 Initial revision
kono
parents:
diff changeset
32 #define IS_JIS7(c) ((0x21 <= (c)) && ((c) <= 0x7e))
bce86c4163a3 Initial revision
kono
parents:
diff changeset
33 #define IS_JEUC(c) ((0xa1 <= (c)) && ((c) <= 0xfe))
bce86c4163a3 Initial revision
kono
parents:
diff changeset
34 #define IS_SJIS1(c) (((0x81 <= (c)) && ((c) <= 0x9f)) || \
bce86c4163a3 Initial revision
kono
parents:
diff changeset
35 ((0xe0 <= (c)) && ((c) <= 0xfc)))
bce86c4163a3 Initial revision
kono
parents:
diff changeset
36 #define IS_SJIS2(c) ((0x40 <= (c)) && ((c) <= 0xfc) && ((c) != 0x7f))
bce86c4163a3 Initial revision
kono
parents:
diff changeset
37
5
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
38 /* encoding (mhn) */
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
39
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
40 #define CE_UNKNOWN 0x00
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
41 #define CE_BASE64 0x01
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
42 #define CE_QUOTED 0x02
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
43 #define CE_8BIT 0x03
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
44 #define CE_7BIT 0x04
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
45 #define CE_BINARY 0x05
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
46 #define CE_EXTENSION 0x06
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
47 #define CE_EXTERNAL 0x07 /* for external-body */
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
48
0
bce86c4163a3 Initial revision
kono
parents:
diff changeset
49 /* hold coding system information */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
50 static coding_system_t ml_coding_info[CSL_SIZE] =
bce86c4163a3 Initial revision
kono
parents:
diff changeset
51 { CS_DEFAULT, CS_DEFAULT, CS_DEFAULT };
bce86c4163a3 Initial revision
kono
parents:
diff changeset
52 int japan_environ;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
53
bce86c4163a3 Initial revision
kono
parents:
diff changeset
54 /* private functions */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
55 static void read_profile();
bce86c4163a3 Initial revision
kono
parents:
diff changeset
56 static coding_system_t coding_to_cs();
bce86c4163a3 Initial revision
kono
parents:
diff changeset
57 static coding_system_t select_coding_system();
bce86c4163a3 Initial revision
kono
parents:
diff changeset
58 static void ml_fputs_sbr();
bce86c4163a3 Initial revision
kono
parents:
diff changeset
59 static void jeuc_fputs();
bce86c4163a3 Initial revision
kono
parents:
diff changeset
60 static void sjis_fputs();
bce86c4163a3 Initial revision
kono
parents:
diff changeset
61 static void jis7_fputs();
2
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
62 static void utf8_fputs();
0
bce86c4163a3 Initial revision
kono
parents:
diff changeset
63 static void cntrl_putc();
bce86c4163a3 Initial revision
kono
parents:
diff changeset
64 static void ml_conv_sbr();
bce86c4163a3 Initial revision
kono
parents:
diff changeset
65
2
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
66 /* NKF Input/Output pointers */
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
67
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
68 static unsigned char *nkf_output;
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
69 static unsigned char *nkf_input;
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
70 static int nkf_input_ctr;
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
71 static int nkf_output_ctr;
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
72 static int nkf_limit;
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
73 static int nkf_ilimit;
5
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
74 static void (*nkf_flush)(unsigned char *,int);
2
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
75
5
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
76 static void nkf_open(unsigned char *opt, unsigned char *in,int ilimit,
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
77 unsigned char *out,int limit,void (*flush)(unsigned char *,int),FILE *fp);
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
78 static void nkf_continue(unsigned char *in,int ilimit) ;
2
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
79 static void nkf_end() ;
5
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
80 static void none(unsigned char *a,int b);
2
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
81
5
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
82 static void my_flush(unsigned char *out,int out_count);
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
83 static void extend(unsigned char *out,int out_count);
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
84 static void my_pretty(unsigned char *out,int out_count);
2
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
85
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
86 static FILE *nkf_file;
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
87
0
bce86c4163a3 Initial revision
kono
parents:
diff changeset
88
bce86c4163a3 Initial revision
kono
parents:
diff changeset
89 /*
bce86c4163a3 Initial revision
kono
parents:
diff changeset
90 * Initialize: holding coding system information
bce86c4163a3 Initial revision
kono
parents:
diff changeset
91 */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
92 void
bce86c4163a3 Initial revision
kono
parents:
diff changeset
93 ml_init()
bce86c4163a3 Initial revision
kono
parents:
diff changeset
94 {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
95 read_profile(ml_coding_info);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
96 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
97
bce86c4163a3 Initial revision
kono
parents:
diff changeset
98 /* get coding system list from profile and environment variable */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
99 static void
bce86c4163a3 Initial revision
kono
parents:
diff changeset
100 read_profile(csl)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
101 coding_system_t csl[];
bce86c4163a3 Initial revision
kono
parents:
diff changeset
102 {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
103 char *s, *default_coding;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
104
bce86c4163a3 Initial revision
kono
parents:
diff changeset
105 #if 0 /* We won't refer $LANG nor $LC_CTYPE */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
106 if ((default_coding = getenv("LC_CTYPE")) == NULL)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
107 default_coding = getenv("LANG");
bce86c4163a3 Initial revision
kono
parents:
diff changeset
108 #else
bce86c4163a3 Initial revision
kono
parents:
diff changeset
109 default_coding = ""; /* for CS_DEFAULT */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
110 #endif
bce86c4163a3 Initial revision
kono
parents:
diff changeset
111
bce86c4163a3 Initial revision
kono
parents:
diff changeset
112 if ((s = getenv("MH_DISPLAY_CODING")) == NULL)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
113 if ((s = m_find("display-coding")) == NULL)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
114 s = default_coding;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
115 CSL_DISPLAY(csl) = coding_to_cs(s);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
116
bce86c4163a3 Initial revision
kono
parents:
diff changeset
117 if ((s = getenv("MH_FILE_CODING")) == NULL)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
118 if ((s = m_find("file-coding")) == NULL)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
119 s = default_coding;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
120 CSL_FILE(csl) = coding_to_cs(s);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
121
bce86c4163a3 Initial revision
kono
parents:
diff changeset
122 if ((s = getenv("MH_PROCESS_CODING")) == NULL)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
123 if ((s = m_find("process-coding")) == NULL)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
124 s = default_coding;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
125 CSL_PROCESS(csl) = coding_to_cs(s);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
126
bce86c4163a3 Initial revision
kono
parents:
diff changeset
127 if (CSL_FILE(ml_coding_info) == CS_NOCONV)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
128 japan_environ = 0;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
129 else
bce86c4163a3 Initial revision
kono
parents:
diff changeset
130 japan_environ = 1;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
131 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
132
bce86c4163a3 Initial revision
kono
parents:
diff changeset
133 static coding_system_t
bce86c4163a3 Initial revision
kono
parents:
diff changeset
134 coding_to_cs(coding)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
135 char *coding;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
136 {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
137 if (*coding == '\0')
bce86c4163a3 Initial revision
kono
parents:
diff changeset
138 return CS_DEFAULT;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
139 else if (uleq(coding, "ja_JP.JIS7"))
bce86c4163a3 Initial revision
kono
parents:
diff changeset
140 return CS_JIS7;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
141 else if (uleq(coding, "ja_JP.EUC"))
bce86c4163a3 Initial revision
kono
parents:
diff changeset
142 return CS_JEUC;
2
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
143 else if (uleq(coding, "ja_JP.EUCjp"))
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
144 return CS_JEUC;
0
bce86c4163a3 Initial revision
kono
parents:
diff changeset
145 else if (uleq(coding, "ja_JP.SJIS"))
bce86c4163a3 Initial revision
kono
parents:
diff changeset
146 return CS_SJIS;
2
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
147 else if (uleq(coding, "ja_JP.UTF-8"))
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
148 return CS_UTF8;
0
bce86c4163a3 Initial revision
kono
parents:
diff changeset
149 else if (uleq(coding, "C"))
bce86c4163a3 Initial revision
kono
parents:
diff changeset
150 return CS_NOCONV;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
151
bce86c4163a3 Initial revision
kono
parents:
diff changeset
152 /* for backward compatibility */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
153 else if (uleq(coding,"japanese") || uleq(coding,"ja_JP.jis8")
bce86c4163a3 Initial revision
kono
parents:
diff changeset
154 || uleq(coding,"ja_JP.pjis") || uleq(coding,"ja_JP.jis")
bce86c4163a3 Initial revision
kono
parents:
diff changeset
155 || uleq(coding,"wr_WR.ct") || uleq(coding,"wr_WR.junet")) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
156 return(CS_JIS7);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
157 } else if (uleq(coding,"ja_JP.ujis")) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
158 return(CS_JEUC);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
159 } else if (uleq(coding,"ja_JP.mscode")) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
160 return(CS_SJIS);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
161 } else if (uleq(coding,"noconv")) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
162 return(CS_NOCONV);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
163 } else {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
164 return(CS_DEFAULT);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
165 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
166 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
167
bce86c4163a3 Initial revision
kono
parents:
diff changeset
168 static coding_system_t
bce86c4163a3 Initial revision
kono
parents:
diff changeset
169 select_coding_system(stream)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
170 FILE *stream;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
171 {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
172 struct stat buf;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
173
bce86c4163a3 Initial revision
kono
parents:
diff changeset
174 if (fstat(fileno(stream), &buf)) adios (NULLCP, "unable to fstat stream");
bce86c4163a3 Initial revision
kono
parents:
diff changeset
175 switch (buf.st_mode & S_IFMT) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
176 case S_IFREG:
bce86c4163a3 Initial revision
kono
parents:
diff changeset
177 return(CSL_FILE(ml_coding_info));
bce86c4163a3 Initial revision
kono
parents:
diff changeset
178 case S_IFIFO:
bce86c4163a3 Initial revision
kono
parents:
diff changeset
179 case 0: /* some system returns zero-filled stat for pipe */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
180 return(CSL_PROCESS(ml_coding_info));
bce86c4163a3 Initial revision
kono
parents:
diff changeset
181 case S_IFCHR:
bce86c4163a3 Initial revision
kono
parents:
diff changeset
182 default:
bce86c4163a3 Initial revision
kono
parents:
diff changeset
183 return(CSL_DISPLAY(ml_coding_info));
bce86c4163a3 Initial revision
kono
parents:
diff changeset
184 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
185 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
186
bce86c4163a3 Initial revision
kono
parents:
diff changeset
187
bce86c4163a3 Initial revision
kono
parents:
diff changeset
188 /*
bce86c4163a3 Initial revision
kono
parents:
diff changeset
189 *
bce86c4163a3 Initial revision
kono
parents:
diff changeset
190 */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
191 int
bce86c4163a3 Initial revision
kono
parents:
diff changeset
192 ml_ismlchar(c)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
193 unsigned char c;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
194 {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
195 return japan_environ ? IS_JEUC(c) : 0;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
196 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
197
bce86c4163a3 Initial revision
kono
parents:
diff changeset
198 int
bce86c4163a3 Initial revision
kono
parents:
diff changeset
199 ml_ismlptr(p)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
200 unsigned char *p;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
201 {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
202 return japan_environ ? (IS_JEUC(*p) && IS_JEUC(*(p+1))) : 0;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
203 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
204
bce86c4163a3 Initial revision
kono
parents:
diff changeset
205
bce86c4163a3 Initial revision
kono
parents:
diff changeset
206 /*
bce86c4163a3 Initial revision
kono
parents:
diff changeset
207 * Output:
bce86c4163a3 Initial revision
kono
parents:
diff changeset
208 */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
209 void
bce86c4163a3 Initial revision
kono
parents:
diff changeset
210 ml_fputs(scanlk, stream)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
211 char *scanlk;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
212 FILE *stream;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
213 {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
214 ml_fputs_sbr(scanlk, stream, 0);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
215 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
216
bce86c4163a3 Initial revision
kono
parents:
diff changeset
217 void
bce86c4163a3 Initial revision
kono
parents:
diff changeset
218 ml_pretty_fputs(scanlk, stream)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
219 char *scanlk;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
220 FILE *stream;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
221 {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
222 ml_fputs_sbr(scanlk, stream, 1);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
223 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
224
bce86c4163a3 Initial revision
kono
parents:
diff changeset
225 void
bce86c4163a3 Initial revision
kono
parents:
diff changeset
226 junet_fputs(scanlk, stream)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
227 char *scanlk;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
228 FILE *stream;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
229 {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
230 jis7_fputs(scanlk, stream, 0);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
231 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
232
bce86c4163a3 Initial revision
kono
parents:
diff changeset
233
bce86c4163a3 Initial revision
kono
parents:
diff changeset
234 static void
bce86c4163a3 Initial revision
kono
parents:
diff changeset
235 ml_fputs_sbr(scanlk, stream, pretty)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
236 char *scanlk;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
237 FILE *stream;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
238 int pretty;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
239 {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
240 switch (select_coding_system(stream)) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
241 case CS_NOCONV:
bce86c4163a3 Initial revision
kono
parents:
diff changeset
242 fputs(scanlk, stream);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
243 break;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
244 case CS_SJIS:
bce86c4163a3 Initial revision
kono
parents:
diff changeset
245 sjis_fputs(scanlk, stream, pretty);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
246 break;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
247 case CS_JEUC:
bce86c4163a3 Initial revision
kono
parents:
diff changeset
248 jeuc_fputs(scanlk, stream, pretty);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
249 break;
2
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
250 case CS_UTF8:
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
251 utf8_fputs(scanlk, stream, pretty);
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
252 break;
0
bce86c4163a3 Initial revision
kono
parents:
diff changeset
253 case CS_JIS7:
bce86c4163a3 Initial revision
kono
parents:
diff changeset
254 case CS_DEFAULT:
bce86c4163a3 Initial revision
kono
parents:
diff changeset
255 default:
bce86c4163a3 Initial revision
kono
parents:
diff changeset
256 jis7_fputs(scanlk, stream, pretty);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
257 break;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
258 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
259 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
260
bce86c4163a3 Initial revision
kono
parents:
diff changeset
261
bce86c4163a3 Initial revision
kono
parents:
diff changeset
262 /*
bce86c4163a3 Initial revision
kono
parents:
diff changeset
263 * Output routines with code conversion
bce86c4163a3 Initial revision
kono
parents:
diff changeset
264 */
2
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
265
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
266 char buf[BUFSIZ];
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
267
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
268 static void
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
269 utf8_fputs(scanlk, stream, pretty)
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
270 char *scanlk;
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
271 FILE *stream;
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
272 int pretty;
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
273 {
5
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
274 nkf_open((unsigned char *)"-w80",(unsigned char *)scanlk,-1,(unsigned char *)buf,BUFSIZ,pretty?my_pretty: my_flush,stream);
2
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
275 nkf_end();
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
276 }
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
277
0
bce86c4163a3 Initial revision
kono
parents:
diff changeset
278 static void
bce86c4163a3 Initial revision
kono
parents:
diff changeset
279 jeuc_fputs(scanlk, stream, pretty)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
280 char *scanlk;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
281 FILE *stream;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
282 int pretty;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
283 {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
284 unsigned char u1, u2;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
285
bce86c4163a3 Initial revision
kono
parents:
diff changeset
286 while (u1 = *scanlk++) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
287 if (IS_JEUC(u1)) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
288 u2 = *scanlk;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
289 if (IS_JEUC(u2)) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
290 scanlk++;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
291 putc(u1, stream); putc(u2, stream);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
292 continue;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
293 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
294 putc(' ', stream);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
295 } else if (u1 & 0x80) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
296 putc(' ', stream);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
297 } else if (pretty && iscntrl(u1)) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
298 cntrl_putc(u1, stream);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
299 } else {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
300 putc(u1, stream);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
301 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
302 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
303 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
304
bce86c4163a3 Initial revision
kono
parents:
diff changeset
305 #define E2S(i1, i2, o1, o2) {\
bce86c4163a3 Initial revision
kono
parents:
diff changeset
306 (i1) &= 0x7f;\
bce86c4163a3 Initial revision
kono
parents:
diff changeset
307 (i2) &= 0x7f;\
bce86c4163a3 Initial revision
kono
parents:
diff changeset
308 (o1) = ((i1) - 0x21) / 2 + 0x81;\
bce86c4163a3 Initial revision
kono
parents:
diff changeset
309 if ((o1) > 0x9f) { (o1) += (0xe0 - 0xa0); }\
bce86c4163a3 Initial revision
kono
parents:
diff changeset
310 if ((i1) & 1) {\
bce86c4163a3 Initial revision
kono
parents:
diff changeset
311 (o2) = (i2) + (0x40 - 0x21);\
bce86c4163a3 Initial revision
kono
parents:
diff changeset
312 if ((o2) > 0x7e) (o2)++;\
bce86c4163a3 Initial revision
kono
parents:
diff changeset
313 } else {\
bce86c4163a3 Initial revision
kono
parents:
diff changeset
314 (o2) = (i2) + (0xfc - 0x7e);\
bce86c4163a3 Initial revision
kono
parents:
diff changeset
315 }\
bce86c4163a3 Initial revision
kono
parents:
diff changeset
316 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
317
bce86c4163a3 Initial revision
kono
parents:
diff changeset
318 static void
bce86c4163a3 Initial revision
kono
parents:
diff changeset
319 sjis_fputs(scanlk, stream, pretty)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
320 char *scanlk;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
321 FILE *stream;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
322 int pretty;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
323 {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
324 unsigned char u1, u2, s1, s2;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
325
bce86c4163a3 Initial revision
kono
parents:
diff changeset
326 while (u1 = *scanlk++) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
327 if (IS_JEUC(u1)) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
328 u2 = *scanlk;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
329 if (IS_JEUC(u2)) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
330 scanlk++;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
331 E2S(u1, u2, s1, s2);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
332 putc(s1, stream); putc(s2, stream);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
333 continue;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
334 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
335 putc(' ', stream);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
336 } else if (u1 & 0x80) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
337 putc(' ', stream);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
338 } else if (pretty && iscntrl(u1)) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
339 cntrl_putc(u1, stream);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
340 } else {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
341 putc(u1, stream);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
342 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
343 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
344 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
345
bce86c4163a3 Initial revision
kono
parents:
diff changeset
346 #define DSGNT_JISX0208(stream, status) {\
bce86c4163a3 Initial revision
kono
parents:
diff changeset
347 if (kanji_pos == ASCII) {\
bce86c4163a3 Initial revision
kono
parents:
diff changeset
348 fputs("\033$B", (stream)); (status) = JISX0208;\
bce86c4163a3 Initial revision
kono
parents:
diff changeset
349 }}\
bce86c4163a3 Initial revision
kono
parents:
diff changeset
350
bce86c4163a3 Initial revision
kono
parents:
diff changeset
351 #define DSGNT_ASCII(stream, status) {\
bce86c4163a3 Initial revision
kono
parents:
diff changeset
352 if (kanji_pos == JISX0208) {\
bce86c4163a3 Initial revision
kono
parents:
diff changeset
353 fputs("\033(B", (stream)); (status) = ASCII;\
bce86c4163a3 Initial revision
kono
parents:
diff changeset
354 }}\
bce86c4163a3 Initial revision
kono
parents:
diff changeset
355
bce86c4163a3 Initial revision
kono
parents:
diff changeset
356 static void
bce86c4163a3 Initial revision
kono
parents:
diff changeset
357 jis7_fputs(scanlk, stream, pretty)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
358 char *scanlk;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
359 FILE *stream;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
360 int pretty;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
361 {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
362 int kanji_pos; /* ASCII or JISX0208 */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
363 unsigned char u1, u2;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
364
bce86c4163a3 Initial revision
kono
parents:
diff changeset
365 kanji_pos = ASCII;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
366 while (u1 = *scanlk++) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
367 if (IS_JEUC(u1)) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
368 u2 = *scanlk;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
369 if (IS_JEUC(u2)) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
370 scanlk++;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
371 DSGNT_JISX0208(stream, kanji_pos);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
372 putc(u1 & 0x7f, stream); putc(u2 & 0x7f, stream);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
373 continue;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
374 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
375 DSGNT_ASCII(stream, kanji_pos);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
376 putc(' ', stream);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
377 } else if (u1 & 0x80) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
378 DSGNT_ASCII(stream, kanji_pos);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
379 putc(' ', stream);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
380 } else if (pretty && iscntrl(u1)) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
381 DSGNT_ASCII(stream, kanji_pos);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
382 cntrl_putc(u1, stream);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
383 } else {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
384 DSGNT_ASCII(stream, kanji_pos);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
385 putc(u1, stream);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
386 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
387 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
388 DSGNT_ASCII(stream, kanji_pos);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
389 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
390
bce86c4163a3 Initial revision
kono
parents:
diff changeset
391 static void
bce86c4163a3 Initial revision
kono
parents:
diff changeset
392 cntrl_putc(c, stream)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
393 char c;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
394 FILE *stream;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
395 {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
396 switch (c) {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
397 case '\b': putc('\\', stream); putc('b', stream); break;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
398 case '\f': putc('\\', stream); putc('f', stream); break;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
399 case '\n': putc('\\', stream); putc('n', stream); break;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
400 case '\r': putc('\\', stream); putc('r', stream); break;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
401 case '\t': putc('\\', stream); putc('t', stream); break;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
402 default: putc('^', stream); putc(c ^ 0x40, stream); break;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
403 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
404 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
405
bce86c4163a3 Initial revision
kono
parents:
diff changeset
406
bce86c4163a3 Initial revision
kono
parents:
diff changeset
407 /*
bce86c4163a3 Initial revision
kono
parents:
diff changeset
408 * Input:
bce86c4163a3 Initial revision
kono
parents:
diff changeset
409 */
bce86c4163a3 Initial revision
kono
parents:
diff changeset
410 char *
bce86c4163a3 Initial revision
kono
parents:
diff changeset
411 ml_conv(s)
bce86c4163a3 Initial revision
kono
parents:
diff changeset
412 char *s;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
413 {
bce86c4163a3 Initial revision
kono
parents:
diff changeset
414 coding_system_t coding;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
415
bce86c4163a3 Initial revision
kono
parents:
diff changeset
416 if ((s == NULL) || ((coding = CSL_FILE(ml_coding_info)) == CS_NOCONV))
bce86c4163a3 Initial revision
kono
parents:
diff changeset
417 return(s);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
418
7
c20e4181370f utf-8 input assumption in case of base64/utf-8
kono
parents: 6
diff changeset
419 ml_conv_sbr(s, coding, CE_UNKNOWN, 0);
5
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
420 return(s);
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
421 }
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
422
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
423 char *
7
c20e4181370f utf-8 input assumption in case of base64/utf-8
kono
parents: 6
diff changeset
424 ml_conv_decode(s,encode,charset)
5
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
425 char *s;
7
c20e4181370f utf-8 input assumption in case of base64/utf-8
kono
parents: 6
diff changeset
426 int encode,charset;
5
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
427 {
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
428 coding_system_t coding;
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
429
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
430 if ((s == NULL) || ((coding = CSL_FILE(ml_coding_info)) == CS_NOCONV))
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
431 return(s);
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
432
7
c20e4181370f utf-8 input assumption in case of base64/utf-8
kono
parents: 6
diff changeset
433 ml_conv_sbr(s, coding, encode, charset);
0
bce86c4163a3 Initial revision
kono
parents:
diff changeset
434 return(s);
bce86c4163a3 Initial revision
kono
parents:
diff changeset
435 }
bce86c4163a3 Initial revision
kono
parents:
diff changeset
436
2
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
437 /*
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
438 Convert to EUC
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
439 shirinking only (?)
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
440 */
0
bce86c4163a3 Initial revision
kono
parents:
diff changeset
441
2
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
442 static char *
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
443 cs_output_opt(int cs)
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
444 {
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
445 switch(cs) {
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
446 case CS_JIS7: return "-j";
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
447 case CS_JEUC: return "-e";
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
448 case CS_SJIS: return "-s";
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
449 case CS_UTF8: return "-w80";
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
450 case CS_NOCONV: return "-t";
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
451 }
5
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
452 return "-t";
2
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
453 }
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
454
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
455 static char *
7
c20e4181370f utf-8 input assumption in case of base64/utf-8
kono
parents: 6
diff changeset
456 cs_input_opt(int cs, int encode, int input_charset)
2
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
457 {
5
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
458 switch(encode) {
7
c20e4181370f utf-8 input assumption in case of base64/utf-8
kono
parents: 6
diff changeset
459 case CE_BASE64:
c20e4181370f utf-8 input assumption in case of base64/utf-8
kono
parents: 6
diff changeset
460 if (input_charset==CS_UTF8)
c20e4181370f utf-8 input assumption in case of base64/utf-8
kono
parents: 6
diff changeset
461 return "-emBW8";
c20e4181370f utf-8 input assumption in case of base64/utf-8
kono
parents: 6
diff changeset
462 return "-emB";
c20e4181370f utf-8 input assumption in case of base64/utf-8
kono
parents: 6
diff changeset
463 case CE_QUOTED:
c20e4181370f utf-8 input assumption in case of base64/utf-8
kono
parents: 6
diff changeset
464 if (input_charset==CS_UTF8)
c20e4181370f utf-8 input assumption in case of base64/utf-8
kono
parents: 6
diff changeset
465 return "-emQW8";
c20e4181370f utf-8 input assumption in case of base64/utf-8
kono
parents: 6
diff changeset
466 return "-emQ";
5
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
467 }
2
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
468 switch(cs) {
3
kono
parents: 2
diff changeset
469 case CS_JIS7: return "-Je";
kono
parents: 2
diff changeset
470 case CS_JEUC: return "-Ee";
kono
parents: 2
diff changeset
471 case CS_SJIS: return "-Se";
kono
parents: 2
diff changeset
472 case CS_UTF8: return "-W8e";
2
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
473 case CS_NOCONV: return "-t";
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
474 }
5
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
475 return "-e";
2
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
476 }
0
bce86c4163a3 Initial revision
kono
parents:
diff changeset
477
bce86c4163a3 Initial revision
kono
parents:
diff changeset
478 static void
7
c20e4181370f utf-8 input assumption in case of base64/utf-8
kono
parents: 6
diff changeset
479 ml_conv_sbr(in, cs, encode, input_charset)
0
bce86c4163a3 Initial revision
kono
parents:
diff changeset
480 char *in;
bce86c4163a3 Initial revision
kono
parents:
diff changeset
481 coding_system_t cs;
7
c20e4181370f utf-8 input assumption in case of base64/utf-8
kono
parents: 6
diff changeset
482 int encode, input_charset;
0
bce86c4163a3 Initial revision
kono
parents:
diff changeset
483 {
7
c20e4181370f utf-8 input assumption in case of base64/utf-8
kono
parents: 6
diff changeset
484 char *opt = cs_input_opt(cs,encode,input_charset);
2
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
485 int len = strlen(in);
5
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
486 nkf_open((unsigned char *)opt,(unsigned char *)in,len,(unsigned char *)in,len,extend,0);
2
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
487 nkf_end();
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
488 nkf_output[nkf_output_ctr]=0;
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
489 }
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
490
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
491 extern void
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
492 mime_convert(char *ptr)
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
493 {
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
494 int len = strlen(ptr);
5
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
495 nkf_open((unsigned char *)"-me",(unsigned char *)ptr,len,(unsigned char *)ptr,len,extend,0);
2
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
496 nkf_end();
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
497 nkf_output[nkf_output_ctr]=0;
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
498 }
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
499
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
500 #undef CR
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
501 #undef LIMIT
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
502 #undef PROTO
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
503
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
504 #undef getc
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
505 #undef ungetc
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
506
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
507 #define getc(f) nkf_getc(f)
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
508 #define ungetc(c,f) nkf_ungetc(c,f)
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
509
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
510 static int
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
511 nkf_getc(FILE *f) {
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
512 if (nkf_ilimit==-1) {
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
513 int c = nkf_input[nkf_input_ctr++];
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
514 if (c==0) {
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
515 nkf_input_ctr--; return -1;
0
bce86c4163a3 Initial revision
kono
parents:
diff changeset
516 }
2
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
517 return c;
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
518 }
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
519 return (nkf_input_ctr>=nkf_ilimit?-1:nkf_input[nkf_input_ctr++]);
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
520 }
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
521
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
522 static int
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
523 nkf_ungetc(int c,FILE *f) {
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
524 nkf_input_ctr--;
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
525 return c;
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
526 }
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
527
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
528
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
529 #undef putchar
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
530 #undef TRUE
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
531 #undef FALSE
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
532 #define putchar(c) nkf_putchar(c)
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
533
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
534 #define debug nkf_debug
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
535
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
536 static
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
537 int
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
538 nkf_putchar(unsigned int c)
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
539 {
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
540 /* string length is enough? */
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
541 if(nkf_output_ctr<nkf_limit && c!='\n') {
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
542 return nkf_output[nkf_output_ctr++] = c;
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
543 } else {
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
544 nkf_output[nkf_output_ctr++] = c;
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
545 nkf_flush(nkf_output,nkf_output_ctr);
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
546 nkf_output_ctr = 0;
0
bce86c4163a3 Initial revision
kono
parents:
diff changeset
547 }
2
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
548 return c;
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
549 }
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
550
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
551
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
552 /* Include kanji filter main part */
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
553 /* getchar and putchar will be replaced during inclusion */
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
554
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
555 #define version NKF_version
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
556 #define PERL_XS 1
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
557 #include "../nkf-utf8/utf8tbl.c"
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
558 #include "../nkf-utf8/nkf.c"
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
559
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
560 /*
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
561 using opt ( "-w8" etc... )
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
562 convert *in into *out
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
563 when limit or nkf_end flush is called
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
564
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
565 nkf_continue can be use to change input ptr
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
566 */
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
567
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
568 static void
5
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
569 nkf_open(unsigned char *opt, unsigned char *in,int ilimit,
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
570 unsigned char *out,int limit,void (*flush)(unsigned char *,int),FILE *fp) {
2
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
571 /* Flags are reset at each call. */
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
572 reinit();
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
573
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
574 /* Process flags except the last once */
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
575 options(opt);
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
576
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
577 nkf_input_ctr = 0;
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
578 nkf_input = in;
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
579 nkf_ilimit = ilimit;
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
580
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
581 nkf_file = fp;
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
582
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
583 nkf_output_ctr = 0;
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
584 nkf_output = out;
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
585
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
586 nkf_limit = limit;
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
587 nkf_flush = flush;
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
588
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
589 /* Convestion */
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
590 kanji_convert(NULL);
0
bce86c4163a3 Initial revision
kono
parents:
diff changeset
591 }
2
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
592
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
593 static void
5
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
594 nkf_continue(unsigned char *in,int ilimit) {
2
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
595 nkf_input_ctr = 0;
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
596 nkf_input = in;
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
597 nkf_ilimit = ilimit;
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
598 /* Convestion */
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
599 kanji_convert(NULL);
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
600 }
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
601
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
602 void
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
603 nkf_end() {
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
604 if (nkf_output_ctr) {
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
605 nkf_flush(nkf_output,nkf_output_ctr);
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
606 nkf_output_ctr = 0;
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
607 }
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
608 }
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
609
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
610 static void
5
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
611 extend(unsigned char *out,int out_count)
2
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
612 {
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
613 nkf_output += nkf_output_ctr;
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
614 }
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
615
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
616 static void
5
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
617 none(unsigned char *a,int b)
2
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
618 {
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
619 }
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
620
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
621 static void
5
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
622 my_flush(unsigned char *out,int out_count)
2
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
623 {
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
624 fwrite(out,out_count,1,nkf_file);
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
625 }
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
626
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
627 static void
5
47d488958a4c support contents-encoding in mhl.
kono
parents: 3
diff changeset
628 my_pretty(unsigned char *out,int out_count)
2
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
629 {
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
630 int c;
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
631 FILE *fp = nkf_file;
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
632 while(out_count-->0) {
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
633 c = *out++;
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
634 if (iscntrl(c))
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
635 cntrl_putc(c, fp);
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
636 else
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
637 putc(c, fp);
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
638 }
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
639 }
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
640
0
bce86c4163a3 Initial revision
kono
parents:
diff changeset
641 #endif /* JAPAN */
2
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
642
442dbbf0ac7d utf first try
kono
parents: 0
diff changeset
643 /* end */