| 1 |
/* |
| 2 |
* KANJI Code conversion routines. |
| 3 |
*/ |
| 4 |
|
| 5 |
#include <kpathsea/config.h> |
| 6 |
#include <kpathsea/c-memstr.h> |
| 7 |
#include <kpathsea/variable.h> |
| 8 |
#include <kpathsea/readable.h> |
| 9 |
#include <kpathsea/c-limits.h> |
| 10 |
#include <kpathsea/c-pathmx.h> |
| 11 |
|
| 12 |
#include <ptexenc/c-auto.h> |
| 13 |
#include <ptexenc/ptexenc.h> |
| 14 |
#include <ptexenc/kanjicnv.h> |
| 15 |
#include <ptexenc/unicode.h> |
| 16 |
#include <ptexenc/unicode-jp.h> |
| 17 |
|
| 18 |
#include <ctype.h> |
| 19 |
#include <sys/stat.h> |
| 20 |
|
| 21 |
#define ENC_UNKNOWN 0 |
| 22 |
#define ENC_JIS 1 |
| 23 |
#define ENC_EUC 2 |
| 24 |
#define ENC_SJIS 3 |
| 25 |
#define ENC_UTF8 4 |
| 26 |
#define ENC_UPTEX 5 |
| 27 |
|
| 28 |
static int default_kanji_enc; |
| 29 |
static boolean UPTEX_enabled; |
| 30 |
static boolean ptex_mode = false; |
| 31 |
static boolean prior_file_enc = false; |
| 32 |
|
| 33 |
#define ESC '\033' |
| 34 |
|
| 35 |
#ifndef NOFILE |
| 36 |
# ifndef OPEN_MAX |
| 37 |
# define OPEN_MAX 132 /* sup_max_in_open(127) +alpha */ |
| 38 |
# endif |
| 39 |
# define NOFILE OPEN_MAX |
| 40 |
#endif |
| 41 |
|
| 42 |
const char *ptexenc_version_string = PTEXENCVERSION; |
| 43 |
#if defined(WIN32) |
| 44 |
FILE *Poptr; |
| 45 |
#endif |
| 46 |
int infile_enc_auto = 2; |
| 47 |
/* 0: guess disabled, 1: guess enabled, 2: unspecified */ |
| 48 |
|
| 49 |
static int file_enc = ENC_UNKNOWN; |
| 50 |
static int internal_enc = ENC_UNKNOWN; |
| 51 |
static int terminal_enc = ENC_UNKNOWN; |
| 52 |
static int guess_enc = ENC_UNKNOWN; |
| 53 |
|
| 54 |
const_string enc_to_string(int enc) |
| 55 |
{ |
| 56 |
switch (enc) { |
| 57 |
case ENC_JIS: return "jis"; |
| 58 |
case ENC_EUC: return "euc"; |
| 59 |
case ENC_SJIS: return "sjis"; |
| 60 |
case ENC_UTF8: return "utf8"; |
| 61 |
case ENC_UPTEX: if (UPTEX_enabled) return "uptex"; |
| 62 |
default: return "?"; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
static int string_to_enc(const_string str) |
| 67 |
{ |
| 68 |
if (str == NULL) return ENC_UNKNOWN; |
| 69 |
if (strcasecmp(str, "default")== 0) return default_kanji_enc; |
| 70 |
if (strcasecmp(str, "jis") == 0) return ENC_JIS; |
| 71 |
if (strcasecmp(str, "euc") == 0) return ENC_EUC; |
| 72 |
if (strcasecmp(str, "sjis") == 0) return ENC_SJIS; |
| 73 |
if (strcasecmp(str, "utf8") == 0) return ENC_UTF8; |
| 74 |
if (UPTEX_enabled && strcasecmp(str, "uptex") == 0) return ENC_UPTEX; |
| 75 |
|
| 76 |
if (strncasecmp(str, "ASCII", 5)== 0) return file_enc; |
| 77 |
if (strncasecmp(str, "AMBIGUOUS", 9) == 0) return guess_enc; |
| 78 |
if (strncasecmp(str, "BINARY", 6) == 0) return ENC_JIS; |
| 79 |
if (strncasecmp(str, "ISO-2022-JP", 11) == 0) return ENC_JIS; |
| 80 |
if (strncasecmp(str, "EUC-JP", 6) == 0) return ENC_EUC; |
| 81 |
if (strncasecmp(str, "Shift_JIS", 9) == 0) return ENC_SJIS; |
| 82 |
if (strncasecmp(str, "UTF-8", 5) == 0) return ENC_UTF8; |
| 83 |
if (strncasecmp(str, "ISO-8859", 8) == 0) return ENC_JIS; |
| 84 |
return -1; /* error */ |
| 85 |
} |
| 86 |
|
| 87 |
static int get_default_enc(void) |
| 88 |
{ |
| 89 |
/* kpse_var_value("PTEX_KANJI_ENC") aborts |
| 90 |
if 'kpse_program_name' is empty. It typically occurs |
| 91 |
when 'ptex' and 'jmpost' print version messages. */ |
| 92 |
string var = getenv("PTEX_KANJI_ENC"); |
| 93 |
int enc = string_to_enc(var); |
| 94 |
if (enc < 0) { |
| 95 |
fprintf(stderr, "Warning: Unknown environment value " |
| 96 |
"PTEX_KANJI_ENC='%s'\n", var); |
| 97 |
} else if (enc != ENC_UNKNOWN) { |
| 98 |
return enc; |
| 99 |
} |
| 100 |
return default_kanji_enc; |
| 101 |
} |
| 102 |
|
| 103 |
static void set_file_enc(int enc) |
| 104 |
{ |
| 105 |
if (enc == ENC_UPTEX) file_enc = ENC_UTF8; |
| 106 |
else /* rest */ file_enc = enc; |
| 107 |
} |
| 108 |
|
| 109 |
static void set_internal_enc(int enc) |
| 110 |
{ |
| 111 |
if (enc == ENC_SJIS) internal_enc = ENC_SJIS; |
| 112 |
else if (UPTEX_enabled && enc == ENC_UPTEX) internal_enc = ENC_UPTEX; |
| 113 |
else /* EUC, JIS, UTF8 */ internal_enc = ENC_EUC; |
| 114 |
} |
| 115 |
|
| 116 |
static int get_file_enc(void) |
| 117 |
{ |
| 118 |
if (file_enc == ENC_UNKNOWN) set_file_enc(get_default_enc()); |
| 119 |
return file_enc; |
| 120 |
} |
| 121 |
|
| 122 |
int get_internal_enc(void) |
| 123 |
{ |
| 124 |
if (internal_enc == ENC_UNKNOWN) set_internal_enc(get_default_enc()); |
| 125 |
return internal_enc; |
| 126 |
} |
| 127 |
|
| 128 |
static int get_terminal_enc(void) |
| 129 |
{ |
| 130 |
if (terminal_enc == ENC_UNKNOWN) { |
| 131 |
char lang[16]; /* enough large space */ |
| 132 |
const char *s = getenv("LC_ALL"); |
| 133 |
if (s == NULL) s = getenv("LC_MESSAGES"); |
| 134 |
if (s == NULL) s = getenv("LANG"); |
| 135 |
if (s == NULL) s = getenv("LANGUAGE"); |
| 136 |
if (s == NULL) s = ""; |
| 137 |
if (strrchr(s, '.') != NULL) s = strrchr(s, '.') + 1; |
| 138 |
strncpy(lang, s, sizeof(lang) - 1); |
| 139 |
lang[sizeof(lang) - 1] = '\0'; |
| 140 |
if (strcasecmp(lang, "euc") == 0) terminal_enc = ENC_EUC; |
| 141 |
else if (strcasecmp(lang, "eucJP")== 0) terminal_enc = ENC_EUC; |
| 142 |
else if (strcasecmp(lang, "ujis") == 0) terminal_enc = ENC_EUC; |
| 143 |
else if (strcasecmp(lang, "sjis") == 0) terminal_enc = ENC_SJIS; |
| 144 |
else if (strcasecmp(lang, "utf8") == 0) terminal_enc = ENC_UTF8; |
| 145 |
else if (strcasecmp(lang, "UTF-8")== 0) terminal_enc = ENC_UTF8; |
| 146 |
else if (strcasecmp(lang, "jis") == 0) terminal_enc = ENC_JIS; |
| 147 |
else if (strcasecmp(lang, "ISO-2022-JP")== 0) terminal_enc = ENC_JIS; |
| 148 |
else terminal_enc = get_file_enc(); |
| 149 |
} |
| 150 |
return terminal_enc; |
| 151 |
} |
| 152 |
|
| 153 |
void set_guess_file_enc(boolean enable) |
| 154 |
{ |
| 155 |
infile_enc_auto = (int) enable; |
| 156 |
} |
| 157 |
|
| 158 |
/* enable ptex mode (use flag 0x100 for Japanese char) */ |
| 159 |
void ptenc_ptex_mode (const boolean enable) |
| 160 |
{ |
| 161 |
//fprintf(stderr, "ptenc_ptex_mode is called! (%d)\n", enable); |
| 162 |
ptex_mode = enable; |
| 163 |
} |
| 164 |
|
| 165 |
/* enable/disable UPTEX */ |
| 166 |
void enable_UPTEX (boolean enable) |
| 167 |
{ |
| 168 |
UPTEX_enabled = enable; |
| 169 |
if (enable) { |
| 170 |
default_kanji_enc = ENC_UPTEX; |
| 171 |
internal_enc = ENC_UPTEX; |
| 172 |
} else { |
| 173 |
#ifdef WIN32 |
| 174 |
default_kanji_enc = ENC_UTF8; |
| 175 |
internal_enc = ENC_SJIS; |
| 176 |
#else |
| 177 |
default_kanji_enc = ENC_UTF8; |
| 178 |
internal_enc = ENC_EUC; |
| 179 |
#endif |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
void set_prior_file_enc(void) |
| 184 |
{ |
| 185 |
prior_file_enc = true; |
| 186 |
} |
| 187 |
|
| 188 |
const_string get_enc_string(void) |
| 189 |
{ |
| 190 |
static char buffer[20]; /* enough large space */ |
| 191 |
|
| 192 |
if (get_file_enc() == get_internal_enc()) { |
| 193 |
return enc_to_string(get_file_enc()); |
| 194 |
} else { |
| 195 |
sprintf(buffer, "%s.%s", |
| 196 |
enc_to_string(get_file_enc()), |
| 197 |
enc_to_string(get_internal_enc())); |
| 198 |
return buffer; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
boolean set_enc_string(const_string file_str, const_string internal_str) |
| 203 |
{ |
| 204 |
int file = string_to_enc(file_str); |
| 205 |
int internal = string_to_enc(internal_str); |
| 206 |
|
| 207 |
if (file < 0 || internal < 0) return false; /* error */ |
| 208 |
if (file != ENC_UNKNOWN) { |
| 209 |
set_file_enc(file); |
| 210 |
#if !defined(WIN32) |
| 211 |
nkf_disable(); |
| 212 |
#endif |
| 213 |
} |
| 214 |
if (internal != ENC_UNKNOWN) set_internal_enc(internal); |
| 215 |
return true; |
| 216 |
} |
| 217 |
|
| 218 |
boolean is_internalSJIS(void) |
| 219 |
{ |
| 220 |
return (internal_enc == ENC_SJIS); |
| 221 |
} |
| 222 |
|
| 223 |
boolean is_internalEUC(void) |
| 224 |
{ |
| 225 |
return (internal_enc == ENC_EUC); |
| 226 |
} |
| 227 |
|
| 228 |
boolean is_internalUPTEX(void) |
| 229 |
{ |
| 230 |
return (internal_enc == ENC_UPTEX); |
| 231 |
} |
| 232 |
|
| 233 |
boolean is_terminalUTF8(void) |
| 234 |
{ |
| 235 |
#ifdef WIN32 |
| 236 |
return false; |
| 237 |
#else |
| 238 |
get_terminal_enc(); return (terminal_enc == ENC_UTF8); |
| 239 |
#endif |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
/* check char range */ |
| 244 |
boolean ismultichr (int length, int nth, int c) |
| 245 |
{ |
| 246 |
if (is_internalUPTEX()) return isUTF8(length, nth, c); |
| 247 |
if (length == 2) { |
| 248 |
if (nth == 1) { |
| 249 |
if (is_internalSJIS()) return isSJISkanji1(c); |
| 250 |
/* EUC */ return isEUCkanji1(c); |
| 251 |
} else if (nth == 2) { |
| 252 |
if (is_internalSJIS()) return isSJISkanji2(c); |
| 253 |
/* EUC */ return isEUCkanji2(c); |
| 254 |
} |
| 255 |
} |
| 256 |
if ((length == 3 || length == 4) && |
| 257 |
(0 < nth && nth <= length)) return false; |
| 258 |
fprintf(stderr, "ismultichr: unexpected param length=%d, nth=%d\n", |
| 259 |
length, nth); |
| 260 |
return false; |
| 261 |
} |
| 262 |
|
| 263 |
/* check char range (kanji 1st) */ |
| 264 |
boolean iskanji1(int c) |
| 265 |
{ |
| 266 |
if (is_internalUPTEX()) return (isUTF8(2,1,c) || |
| 267 |
isUTF8(3,1,c) || |
| 268 |
isUTF8(4,1,c)); |
| 269 |
if (is_internalSJIS()) return isSJISkanji1(c); |
| 270 |
/* EUC */ return isEUCkanji1(c); |
| 271 |
} |
| 272 |
|
| 273 |
/* check char range (kanji 2nd) */ |
| 274 |
boolean iskanji2(int c) |
| 275 |
{ |
| 276 |
if (is_internalSJIS()) return isSJISkanji2(c); |
| 277 |
/* EUC */ return isEUCkanji2(c); |
| 278 |
} |
| 279 |
|
| 280 |
/* multi-byte char length in s[pos] */ |
| 281 |
#define DEFINE_MULTISTRLEN(SUFF,TYPE) \ |
| 282 |
int multistrlen ## SUFF(TYPE *s, int len, int pos) \ |
| 283 |
{ \ |
| 284 |
s += pos; len -= pos; \ |
| 285 |
if (is_internalUPTEX()) { \ |
| 286 |
int ret = UTF8Slength ## SUFF(s, len); \ |
| 287 |
if (ret < 0) return 1; \ |
| 288 |
return ret; \ |
| 289 |
} \ |
| 290 |
if (len < 2) return 1; \ |
| 291 |
if (is_internalSJIS()) { \ |
| 292 |
if (isSJISkanji1(s[0]) && isSJISkanji2(s[1])) return 2; \ |
| 293 |
} else { /* EUC */ \ |
| 294 |
if (isEUCkanji1(s[0]) && isEUCkanji2(s[1])) return 2; \ |
| 295 |
} \ |
| 296 |
return 1; \ |
| 297 |
} |
| 298 |
DEFINE_MULTISTRLEN(,unsigned char); |
| 299 |
DEFINE_MULTISTRLEN(short,unsigned short); |
| 300 |
|
| 301 |
/* for outputting filename (*s) to the terminal */ |
| 302 |
int multistrlenfilename(unsigned short *s, int len, int pos) |
| 303 |
{ |
| 304 |
s += pos; len -= pos; |
| 305 |
if (terminal_enc == ENC_UTF8) { |
| 306 |
int ret = UTF8Slengthshort(s, len); |
| 307 |
if (ret < 0) return 1; |
| 308 |
return ret; |
| 309 |
} |
| 310 |
if (len < 2) return 1; |
| 311 |
if (terminal_enc == ENC_SJIS) { |
| 312 |
if (isSJISkanji1(s[0]) && isSJISkanji2(s[1])) return 2; |
| 313 |
} else { /* EUC */ |
| 314 |
if (isEUCkanji1(s[0]) && isEUCkanji2(s[1])) return 2; |
| 315 |
} |
| 316 |
return 1; |
| 317 |
} |
| 318 |
|
| 319 |
/* with not so strict range check */ |
| 320 |
int multibytelen (int first_byte) |
| 321 |
{ |
| 322 |
if (is_internalUPTEX()) { |
| 323 |
return UTF8length(first_byte); |
| 324 |
} else if (is_internalSJIS()) { |
| 325 |
if (isSJISkanji1(first_byte)) return 2; |
| 326 |
} else { /* EUC */ |
| 327 |
if (isEUCkanji1(first_byte)) return 2; |
| 328 |
} |
| 329 |
return 1; |
| 330 |
} |
| 331 |
|
| 332 |
/* buffer (EUC/SJIS/UTF-8) to internal (EUC/SJIS/UPTEX) code conversion */ |
| 333 |
long fromBUFF(unsigned char *s, int len, int pos) |
| 334 |
{ |
| 335 |
s += pos; len -= pos; |
| 336 |
if (is_internalUPTEX()) { |
| 337 |
if (UTF8Slength(s, len) < 0) return s[0]; |
| 338 |
return UCStoUPTEX(UTF8StoUCS(s)); |
| 339 |
} |
| 340 |
if (len < 2) return s[0]; |
| 341 |
if (is_internalSJIS()) { |
| 342 |
if (isSJISkanji1(s[0]) && isSJISkanji2(s[1])) return HILO(s[0], s[1]); |
| 343 |
} else { /* EUC */ |
| 344 |
if (isEUCkanji1(s[0]) && isEUCkanji2(s[1])) return HILO(s[0], s[1]); |
| 345 |
} |
| 346 |
return s[0]; |
| 347 |
} |
| 348 |
|
| 349 |
long fromBUFFshort(unsigned short *s, int len, int pos) |
| 350 |
{ |
| 351 |
int i; |
| 352 |
unsigned char sc[6]; |
| 353 |
s += pos; len -= pos; |
| 354 |
for (i=0;i<(len<6 ? len : 6);i++) sc[i]=0xFF&s[i]; |
| 355 |
return fromBUFF(sc, (len<6 ? len : 6), 0); |
| 356 |
} |
| 357 |
|
| 358 |
/* internal (EUC/SJIS/UPTEX) to buffer (EUC/SJIS/UTF-8) code conversion */ |
| 359 |
long toBUFF(long kcode) |
| 360 |
{ |
| 361 |
if (is_internalUPTEX()) kcode = UCStoUTF8(UPTEXtoUCS(kcode)); |
| 362 |
return kcode; |
| 363 |
} |
| 364 |
|
| 365 |
/* DVI (JIS/UCS) to internal (EUC/SJIS/UPTEX) code conversion */ |
| 366 |
long fromDVI (long kcode) |
| 367 |
{ |
| 368 |
if (is_internalUPTEX()) return UCStoUPTEX(kcode); |
| 369 |
if (is_internalSJIS()) return JIStoSJIS(kcode); |
| 370 |
/* EUC */ return JIStoEUC(kcode); |
| 371 |
} |
| 372 |
|
| 373 |
#define UCS_MAX 0x110000L |
| 374 |
|
| 375 |
/* internal (EUC/SJIS/UPTEX) to DVI (JIS/UCS) code conversion */ |
| 376 |
long toDVI (long kcode) |
| 377 |
{ |
| 378 |
if (kcode >= UCS_MAX) { |
| 379 |
if (kcode < UCS_MAX*2) /* for Japanese OTF package */ |
| 380 |
return kcode % UCS_MAX; |
| 381 |
else /* for upTeX internal encoding */ |
| 382 |
return kcode; |
| 383 |
} |
| 384 |
if (is_internalUPTEX()) return UPTEXtoUCS(kcode); |
| 385 |
if (is_internalSJIS()) return SJIStoJIS(kcode); |
| 386 |
/* EUC */ return EUCtoJIS(kcode); |
| 387 |
} |
| 388 |
|
| 389 |
/* JIS to internal (EUC/SJIS/UPTEX) code conversion */ |
| 390 |
long fromJIS(long kcode) |
| 391 |
{ |
| 392 |
if (is_internalUPTEX()) return UCStoUPTEX(JIStoUCS2(kcode)); |
| 393 |
if (is_internalSJIS()) return JIStoSJIS(kcode); |
| 394 |
/* EUC */ return JIStoEUC(kcode); |
| 395 |
} |
| 396 |
|
| 397 |
/* internal (EUC/SJIS/UPTEX) to JIS code conversion */ |
| 398 |
long toJIS(long kcode) |
| 399 |
{ |
| 400 |
if (is_internalUPTEX()) return UCS2toJIS(UPTEXtoUCS(kcode)); |
| 401 |
if (is_internalSJIS()) return SJIStoJIS(kcode); |
| 402 |
/* EUC */ return EUCtoJIS(kcode); |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
/* EUC to internal (EUC/SJIS/UPTEX) code conversion */ |
| 407 |
long fromEUC(long kcode) |
| 408 |
{ |
| 409 |
if (!is_internalUPTEX() && !is_internalSJIS()) return kcode; |
| 410 |
return fromJIS(EUCtoJIS(kcode)); |
| 411 |
} |
| 412 |
|
| 413 |
/* internal (EUC/SJIS/UPTEX) to EUC code conversion */ |
| 414 |
static long toEUC(long kcode) |
| 415 |
{ |
| 416 |
if (!is_internalUPTEX() && !is_internalSJIS()) return kcode; |
| 417 |
return JIStoEUC(toJIS(kcode)); |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
/* SJIS to internal (EUC/SJIS/UPTEX) code conversion */ |
| 422 |
long fromSJIS(long kcode) |
| 423 |
{ |
| 424 |
if (is_internalSJIS()) return kcode; |
| 425 |
return fromJIS(SJIStoJIS(kcode)); |
| 426 |
} |
| 427 |
|
| 428 |
/* internal (EUC/SJIS/UPTEX) to SJIS code conversion */ |
| 429 |
static long toSJIS(long kcode) |
| 430 |
{ |
| 431 |
if (is_internalSJIS()) return kcode; |
| 432 |
return JIStoSJIS(toJIS(kcode)); |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
/* KUTEN to internal (EUC/SJIS/UPTEX) code conversion */ |
| 437 |
long fromKUTEN(long kcode) |
| 438 |
{ |
| 439 |
return fromJIS(KUTENtoJIS(kcode)); |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
/* UCS to internal (EUC/SJIS/UPTEX) code conversion */ |
| 444 |
long fromUCS(long kcode) |
| 445 |
{ |
| 446 |
if (is_internalUPTEX()) return UCStoUPTEX(kcode); |
| 447 |
kcode = UCS2toJIS(kcode); |
| 448 |
if (kcode == 0) return 0; |
| 449 |
return fromJIS(kcode); |
| 450 |
} |
| 451 |
|
| 452 |
/* internal (EUC/SJIS/UPTEX) to UCS code conversion */ |
| 453 |
long toUCS(long kcode) |
| 454 |
{ |
| 455 |
if (is_internalUPTEX()) return UPTEXtoUCS(kcode); |
| 456 |
return JIStoUCS2(toJIS(kcode)); |
| 457 |
} |
| 458 |
|
| 459 |
/* internal (EUC/SJIS/UPTEX) to UTF-8 code conversion */ |
| 460 |
static long toUTF8 (long kcode) |
| 461 |
{ |
| 462 |
return UCStoUTF8(toUCS(kcode)); |
| 463 |
} |
| 464 |
|
| 465 |
/* internal (EUC/SJIS/UPTEX) to 'enc' code conversion */ |
| 466 |
static long toENC(long kcode, int enc) |
| 467 |
{ |
| 468 |
switch (enc) { |
| 469 |
case ENC_UTF8: return toUTF8(kcode); |
| 470 |
case ENC_JIS: return toJIS(kcode); |
| 471 |
case ENC_EUC: return toEUC(kcode); |
| 472 |
case ENC_SJIS: return toSJIS(kcode); |
| 473 |
default: |
| 474 |
fprintf(stderr, "toENC: unknown enc (%d).\n", enc); |
| 475 |
return 0; |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
#define KANJI_IN LONG(0, ESC, '$', 'B') |
| 480 |
#define KANJI_OUT LONG(0, ESC, '(', 'B') |
| 481 |
|
| 482 |
static int put_multibyte(long c, FILE *fp) { |
| 483 |
#ifdef WIN32 |
| 484 |
const int fd = fileno(fp); |
| 485 |
|
| 486 |
if ((fd == fileno(stdout) || fd == fileno(stderr)) && _isatty(fd)) { |
| 487 |
HANDLE hStdout; |
| 488 |
DWORD ret, wclen; |
| 489 |
UINT cp; |
| 490 |
wchar_t buff[2]; |
| 491 |
char str[4]; |
| 492 |
int mblen; |
| 493 |
|
| 494 |
if (fd == fileno(stdout)) |
| 495 |
hStdout = GetStdHandle(STD_OUTPUT_HANDLE); |
| 496 |
else |
| 497 |
hStdout = GetStdHandle(STD_ERROR_HANDLE); |
| 498 |
|
| 499 |
mblen=0; |
| 500 |
if (BYTE1(c) != 0) str[mblen++]=BYTE1(c); |
| 501 |
if (BYTE2(c) != 0) str[mblen++]=BYTE2(c); |
| 502 |
if (BYTE3(c) != 0) str[mblen++]=BYTE3(c); |
| 503 |
/* always */ str[mblen++]=BYTE4(c); |
| 504 |
|
| 505 |
#define CP_UTF8 65001 |
| 506 |
|
| 507 |
cp = CP_UTF8; |
| 508 |
if (MultiByteToWideChar(cp, 0, str, mblen, buff, 2) == 0) |
| 509 |
return EOF; |
| 510 |
|
| 511 |
wclen = mblen > 3 ? 2 : 1; |
| 512 |
if (WriteConsoleW(hStdout, buff, wclen, &ret, NULL) == 0) |
| 513 |
return EOF; |
| 514 |
|
| 515 |
return BYTE4(c); |
| 516 |
} |
| 517 |
#endif |
| 518 |
|
| 519 |
if (BYTE1(c) != 0 && putc(BYTE1(c), fp) == EOF) return EOF; |
| 520 |
if (BYTE2(c) != 0 && putc(BYTE2(c), fp) == EOF) return EOF; |
| 521 |
if (BYTE3(c) != 0 && putc(BYTE3(c), fp) == EOF) return EOF; |
| 522 |
/* always */ return putc(BYTE4(c), fp); |
| 523 |
} |
| 524 |
|
| 525 |
static int flush (unsigned char *buff, int num, FILE *fp) |
| 526 |
{ |
| 527 |
int i, ret = EOF; |
| 528 |
|
| 529 |
/* fprintf(stderr, "putc2: unexpected chars. ( "); |
| 530 |
for (i=0; i<num; i++) fprintf(stderr, "%02X ", buff[i]); |
| 531 |
fprintf(stderr, ")\n"); |
| 532 |
*/ |
| 533 |
for (i=0; i<num; i++) ret = putc(buff[i], fp); |
| 534 |
return ret; |
| 535 |
} |
| 536 |
|
| 537 |
/* putc() with code conversion */ |
| 538 |
int putc2(int c, FILE *fp) |
| 539 |
/* |
| 540 |
c in [0,255]: writes the character c, without code conversion |
| 541 |
c in [256,511]: writes the character c-256, with code conversion |
| 542 |
*/ |
| 543 |
{ |
| 544 |
static int num[NOFILE]; |
| 545 |
/* 0 : not in Kanji |
| 546 |
1..4 : in JIS Kanji and num[] bytes are in store[][] |
| 547 |
-1 : in JIS Kanji and store[][] is empty */ |
| 548 |
static unsigned char store[NOFILE][4]; |
| 549 |
const int fd = fileno(fp); |
| 550 |
int ret = c, output_enc; |
| 551 |
#ifdef WIN32 |
| 552 |
if ((fp == stdout || fp == stderr) && (_isatty(fd) || !prior_file_enc)) { |
| 553 |
output_enc = ENC_UTF8; |
| 554 |
} else |
| 555 |
output_enc = get_file_enc(); |
| 556 |
#else |
| 557 |
if ((fp == stdout || fp == stderr) && !prior_file_enc) { |
| 558 |
output_enc = get_terminal_enc(); |
| 559 |
} else |
| 560 |
output_enc = get_file_enc(); |
| 561 |
#endif |
| 562 |
if (ptex_mode && (c<256)) { |
| 563 |
if (num[fd] < 0 && output_enc == ENC_JIS) { |
| 564 |
put_multibyte(KANJI_OUT, fp); |
| 565 |
} |
| 566 |
ret = putc(c, fp); |
| 567 |
num[fd] = 0; |
| 568 |
} else { |
| 569 |
c &= 0xFF; |
| 570 |
if (num[fd] > 0) { /* multi-byte char */ |
| 571 |
if (is_internalUPTEX() && iskanji1(c)) { /* error */ |
| 572 |
ret = flush(store[fd], num[fd], fp); |
| 573 |
num[fd] = 0; |
| 574 |
} |
| 575 |
store[fd][num[fd]] = c; |
| 576 |
num[fd]++; |
| 577 |
if (multistrlen(store[fd], num[fd], 0) == num[fd]) { |
| 578 |
long i = fromBUFF(store[fd], num[fd], 0); |
| 579 |
ret = put_multibyte(toENC(i, output_enc), fp); |
| 580 |
num[fd] = -1; |
| 581 |
} else if ((is_internalUPTEX() && num[fd] == 4) || |
| 582 |
(!is_internalUPTEX() && num[fd] == 2)) { /* error */ |
| 583 |
ret = flush(store[fd], num[fd], fp); |
| 584 |
num[fd] = -1; |
| 585 |
} |
| 586 |
} else if (iskanji1(c)) { /* first multi-byte char */ |
| 587 |
if (num[fd] == 0 && output_enc == ENC_JIS) { |
| 588 |
ret = put_multibyte(KANJI_IN, fp); |
| 589 |
} |
| 590 |
store[fd][0] = c; |
| 591 |
num[fd] = 1; |
| 592 |
} else { /* ASCII */ |
| 593 |
if (num[fd] < 0 && output_enc == ENC_JIS) { |
| 594 |
put_multibyte(KANJI_OUT, fp); |
| 595 |
} |
| 596 |
ret = putc(c, fp); |
| 597 |
num[fd] = 0; |
| 598 |
} |
| 599 |
} |
| 600 |
return ret; |
| 601 |
} |
| 602 |
|
| 603 |
/* fputs() with code conversion */ |
| 604 |
int fputs2(const char *s, FILE *fp) |
| 605 |
{ |
| 606 |
while (*s != '\0') { |
| 607 |
int ret = putc2((unsigned char)*s, fp); |
| 608 |
if (ret == EOF) return EOF; |
| 609 |
s++; |
| 610 |
} |
| 611 |
return 1; |
| 612 |
} |
| 613 |
|
| 614 |
|
| 615 |
static struct unget_st { |
| 616 |
int size; |
| 617 |
int buff[4]; |
| 618 |
} ungetbuff[NOFILE]; |
| 619 |
|
| 620 |
static int getc4(FILE *fp) |
| 621 |
{ |
| 622 |
struct unget_st *p = &ungetbuff[fileno(fp)]; |
| 623 |
|
| 624 |
if (p->size == 0) |
| 625 |
#ifdef WIN32 |
| 626 |
{ |
| 627 |
const int fd = fileno(fp); |
| 628 |
HANDLE hStdin; |
| 629 |
DWORD ret; |
| 630 |
wchar_t wc[2]; |
| 631 |
long c; |
| 632 |
static wchar_t wcbuf = L'\0'; |
| 633 |
|
| 634 |
if (!(fd == fileno(stdin) && _isatty(fd) && is_internalUPTEX())) |
| 635 |
return getc(fp); |
| 636 |
|
| 637 |
hStdin = GetStdHandle(STD_INPUT_HANDLE); |
| 638 |
if (wcbuf) { |
| 639 |
wc[0] = wcbuf; |
| 640 |
wcbuf = L'\0'; |
| 641 |
} |
| 642 |
else if (ReadConsoleW(hStdin, wc, 1, &ret, NULL) == 0) |
| 643 |
return EOF; |
| 644 |
if (0xd800<=wc[0] && wc[0]<0xdc00) { |
| 645 |
if (ReadConsoleW(hStdin, wc+1, 1, &ret, NULL) == 0) |
| 646 |
return EOF; |
| 647 |
if (0xdc00<=wc[1] && wc[1]<0xe000) { |
| 648 |
c = UTF16StoUTF32(wc[0], wc[1]); |
| 649 |
} else { |
| 650 |
wcbuf = wc[1]; |
| 651 |
c = U_REPLACEMENT_CHARACTER; /* illegal upper surrogate pair */ |
| 652 |
} |
| 653 |
} else if (0xdc00<=wc[0] && wc[0]<0xe000) { |
| 654 |
c = U_REPLACEMENT_CHARACTER; /* illegal lower surrogate pair */ |
| 655 |
} else { |
| 656 |
c = wc[0]; |
| 657 |
} |
| 658 |
c = UCStoUTF8(c); |
| 659 |
/* always */ p->buff[p->size++]=BYTE4(c); |
| 660 |
if (BYTE3(c) != 0) p->buff[p->size++]=BYTE3(c); |
| 661 |
if (BYTE2(c) != 0) p->buff[p->size++]=BYTE2(c); |
| 662 |
if (BYTE1(c) != 0) p->buff[p->size++]=BYTE1(c); |
| 663 |
} |
| 664 |
#else |
| 665 |
return getc(fp); |
| 666 |
#endif |
| 667 |
return p->buff[--p->size]; |
| 668 |
} |
| 669 |
|
| 670 |
static int ungetc4(int c, FILE *fp) |
| 671 |
{ |
| 672 |
struct unget_st *p = &ungetbuff[fileno(fp)]; |
| 673 |
|
| 674 |
if (p->size >= 4) return EOF; |
| 675 |
return p->buff[p->size++] = c; |
| 676 |
} |
| 677 |
|
| 678 |
|
| 679 |
static unsigned char *buffer; |
| 680 |
static long first, last; |
| 681 |
static boolean combin_voiced_sound(boolean semi) |
| 682 |
{ |
| 683 |
int i, mblen; |
| 684 |
|
| 685 |
mblen = is_internalUPTEX() ? 3 : 2; |
| 686 |
if (last-mblen < first) return false; |
| 687 |
if (multistrlen(buffer,last,last-mblen) != mblen) return false; |
| 688 |
i = toUCS(fromBUFF(buffer,last,last-mblen)); |
| 689 |
i = get_voiced_sound(i, semi); |
| 690 |
if (i == 0) return false; |
| 691 |
i = toBUFF(fromUCS(i)); |
| 692 |
if (BYTE2(i) != 0) buffer[last-3] = BYTE2(i); |
| 693 |
/* always */ buffer[last-2] = BYTE3(i); |
| 694 |
/* always */ buffer[last-1] = BYTE4(i); |
| 695 |
return true; |
| 696 |
} |
| 697 |
|
| 698 |
static void write_multibyte(long i) |
| 699 |
{ |
| 700 |
if (BYTE1(i) != 0) buffer[last++] = BYTE1(i); |
| 701 |
if (BYTE2(i) != 0) buffer[last++] = BYTE2(i); |
| 702 |
/* always */ buffer[last++] = BYTE3(i); |
| 703 |
/* always */ buffer[last++] = BYTE4(i); |
| 704 |
} |
| 705 |
|
| 706 |
static void write_hex(int i) |
| 707 |
{ |
| 708 |
sprintf((char *) buffer + last, "^^%02x", i); |
| 709 |
last += 4; |
| 710 |
} |
| 711 |
|
| 712 |
/* getc() with check of broken encoding of UTF-8 */ |
| 713 |
static int getcUTF8(FILE *fp) |
| 714 |
{ |
| 715 |
int c = getc4(fp); |
| 716 |
|
| 717 |
if (isUTF8(2,2,c)) return c; |
| 718 |
ungetc4(c, fp); |
| 719 |
return EOF; |
| 720 |
} |
| 721 |
|
| 722 |
static void get_utf8(int i, FILE *fp) |
| 723 |
{ |
| 724 |
long u = 0, j; |
| 725 |
int i2 = EOF, i3 = EOF, i4 = EOF; |
| 726 |
|
| 727 |
switch (UTF8length(i)) { |
| 728 |
case 2: |
| 729 |
i2 = getcUTF8(fp); if (i2 == EOF) break; |
| 730 |
u = UTF8BtoUCS(i, i2); |
| 731 |
break; |
| 732 |
case 3: |
| 733 |
i2 = getcUTF8(fp); if (i2 == EOF) break; |
| 734 |
i3 = getcUTF8(fp); if (i3 == EOF) break; |
| 735 |
u = UTF8CtoUCS(i, i2, i3); |
| 736 |
if (u == U_BOM) return; /* just ignore */ |
| 737 |
if (u == U_VOICED && combin_voiced_sound(false)) return; |
| 738 |
if (u == U_SEMI_VOICED && combin_voiced_sound(true)) return; |
| 739 |
break; |
| 740 |
case 4: |
| 741 |
i2 = getcUTF8(fp); if (i2 == EOF) break; |
| 742 |
i3 = getcUTF8(fp); if (i3 == EOF) break; |
| 743 |
i4 = getcUTF8(fp); if (i4 == EOF) break; |
| 744 |
u = UTF8DtoUCS(i, i2, i3, i4); |
| 745 |
break; |
| 746 |
default: |
| 747 |
u = U_REPLACEMENT_CHARACTER; |
| 748 |
break; |
| 749 |
} |
| 750 |
|
| 751 |
j = (u != 0) ? toBUFF(fromUCS(u)) : 0; |
| 752 |
if (j == 0) { /* can't represent (typically umlaut o in EUC) */ |
| 753 |
write_hex(i); |
| 754 |
if (i2 != EOF) write_hex(i2); |
| 755 |
if (i3 != EOF) write_hex(i3); |
| 756 |
if (i4 != EOF) write_hex(i4); |
| 757 |
} else { |
| 758 |
write_multibyte(j); |
| 759 |
} |
| 760 |
} |
| 761 |
|
| 762 |
static void get_euc(int i, FILE *fp) |
| 763 |
{ |
| 764 |
int j = getc4(fp); |
| 765 |
|
| 766 |
if (isEUCkanji2(j)) { |
| 767 |
write_multibyte(toBUFF(fromEUC(HILO(i,j)))); |
| 768 |
} else { |
| 769 |
buffer[last++] = i; |
| 770 |
ungetc4(j, fp); |
| 771 |
} |
| 772 |
} |
| 773 |
|
| 774 |
static void get_sjis(int i, FILE *fp) |
| 775 |
{ |
| 776 |
int j = getc4(fp); |
| 777 |
|
| 778 |
if (isSJISkanji2(j)) { |
| 779 |
write_multibyte(toBUFF(fromSJIS(HILO(i,j)))); |
| 780 |
} else { |
| 781 |
buffer[last++] = i; |
| 782 |
ungetc4(j, fp); |
| 783 |
} |
| 784 |
} |
| 785 |
|
| 786 |
static boolean is_tail(long *c, FILE *fp) |
| 787 |
{ |
| 788 |
if (*c == EOF) return true; |
| 789 |
if (*c == '\n') return true; |
| 790 |
if (*c == '\r') { |
| 791 |
int d = getc4(fp); |
| 792 |
if (d == '\n') *c = d; |
| 793 |
else ungetc4(d, fp); |
| 794 |
return true; |
| 795 |
} |
| 796 |
return false; |
| 797 |
} |
| 798 |
|
| 799 |
#define MARK_LEN 4 |
| 800 |
static int bom_u[MARK_LEN] = { 0xEF, 0xBB, 0xBF, 0x7E }; |
| 801 |
static int bom_l[MARK_LEN] = { 0xEF, 0xBB, 0xBF, 0x01 }; |
| 802 |
|
| 803 |
/* if stream begins with BOM + 7bit char */ |
| 804 |
static boolean isUTF8Nstream(FILE *fp) |
| 805 |
{ |
| 806 |
int i; |
| 807 |
int c[MARK_LEN]; |
| 808 |
|
| 809 |
for (i=0; i<MARK_LEN; i++) { |
| 810 |
c[i] = getc4(fp); |
| 811 |
if (!(bom_l[i] <= c[i] && c[i] <= bom_u[i])) { |
| 812 |
do { ungetc4(c[i], fp); } while (i-- > 0); |
| 813 |
return false; |
| 814 |
} |
| 815 |
} |
| 816 |
ungetc4(c[MARK_LEN-1], fp); |
| 817 |
return true; |
| 818 |
} |
| 819 |
|
| 820 |
static int infile_enc[NOFILE]; /* ENC_UNKNOWN (=0): not determined |
| 821 |
other: determined */ |
| 822 |
|
| 823 |
/* guess file encoding */ |
| 824 |
/* |
| 825 |
assumption: |
| 826 |
No halfwidth katakana in Shift_JIS |
| 827 |
No SS2 nor SS3 in EUC-JP |
| 828 |
JIS X 0208 only and no platform dependent characters in Shift_JIS, EUC-JP |
| 829 |
ISO-8859 may have 0xA0..0xFF, may not have 0x80..0x9F |
| 830 |
*/ |
| 831 |
char *ptenc_guess_enc(FILE *fp, boolean chk_bom, boolean chk_nl) |
| 832 |
{ |
| 833 |
char *enc; |
| 834 |
int k0, k1, k2, cdb[2], cu8[4], len_utf8; |
| 835 |
int nl0=0, nl_cr=0, nl_lf=0, nl_crlf=0; |
| 836 |
int is_ascii=1, lbyte=0; |
| 837 |
int maybe_sjis=1, maybe_euc=1, maybe_utf8=1, maybe_iso8859=1, pos_db=0, pos_utf8=0; |
| 838 |
int ch_sjis=0, ch_euc=0, ch_utf8=0, ch_iso8859=0, bom=0; |
| 839 |
#ifdef DEBUG |
| 840 |
int i; |
| 841 |
unsigned char str0[5]; |
| 842 |
#endif /* DEBUG */ |
| 843 |
enc = xmalloc(sizeof(char)*32); |
| 844 |
|
| 845 |
while ((k0 = fgetc(fp)) != EOF && |
| 846 |
(maybe_sjis+maybe_euc+maybe_utf8+maybe_iso8859>1 || pos_db || pos_utf8 |
| 847 |
|| lbyte<320 || k0=='\r')) { |
| 848 |
if (chk_nl) { |
| 849 |
if (k0 == '\r') nl_cr++; |
| 850 |
if (k0 == '\n') { |
| 851 |
if (nl0 == '\r') { nl_cr--; nl_crlf++; } |
| 852 |
else { nl_lf++; } |
| 853 |
} |
| 854 |
} |
| 855 |
if (maybe_iso8859 && maybe_sjis+maybe_euc+maybe_utf8==1 && !pos_db && !pos_utf8 |
| 856 |
&& ch_iso8859>=2000 && k0!='\r') { |
| 857 |
break; |
| 858 |
} |
| 859 |
nl0 = k0; |
| 860 |
if (chk_bom && lbyte<4 && bom_l[lbyte] <= k0 && k0 <= bom_u[lbyte]) bom++; |
| 861 |
lbyte++; |
| 862 |
if (k0==ESC) { |
| 863 |
k0 = fgetc(fp); |
| 864 |
if (k0=='$') { |
| 865 |
k0 = fgetc(fp); |
| 866 |
if (k0=='@' || k0=='B') { |
| 867 |
strcpy(enc,"ISO-2022-JP"); |
| 868 |
goto post_process; |
| 869 |
} |
| 870 |
} |
| 871 |
if (k0>0x7F) { |
| 872 |
strcpy(enc,"BINARY"); |
| 873 |
goto post_process; |
| 874 |
} else if (k0==EOF) { |
| 875 |
break; |
| 876 |
} |
| 877 |
continue; |
| 878 |
} else if (k0==0x00) { |
| 879 |
strcpy(enc,"BINARY"); |
| 880 |
goto post_process; |
| 881 |
} else if (k0<0x80) { |
| 882 |
if (pos_utf8>0) { |
| 883 |
maybe_utf8 = 0; |
| 884 |
pos_utf8 = 0; |
| 885 |
} |
| 886 |
if (pos_db==1) { |
| 887 |
maybe_euc = 0; |
| 888 |
pos_db = 0; |
| 889 |
if (maybe_sjis) { |
| 890 |
cdb[1] = k0; |
| 891 |
k1 = JIStoUCS2(SJIStoJIS(HILO(cdb[0],cdb[1]))); |
| 892 |
#ifdef DEBUG |
| 893 |
fprintf(stderr, "Character for guess encoding: 0x%02X%02X", cdb[0], cdb[1]); |
| 894 |
if (k1) { |
| 895 |
i = UCStoUTF8S(k1, str0); |
| 896 |
str0[i] = '\0'; |
| 897 |
fprintf(stderr, " sjis (%s)\n", str0); |
| 898 |
} else { |
| 899 |
fprintf(stderr, " not sjis\n"); |
| 900 |
} |
| 901 |
#endif /* DEBUG */ |
| 902 |
if (k1) { |
| 903 |
ch_sjis++; |
| 904 |
continue; |
| 905 |
} |
| 906 |
} |
| 907 |
maybe_sjis = 0; |
| 908 |
} |
| 909 |
if (is_ascii && lbyte>10000) { |
| 910 |
/* guess ASCII if we did not find 8bit chars in head 10000 bytes */ |
| 911 |
strcpy(enc,"ASCII"); |
| 912 |
goto post_process; |
| 913 |
} |
| 914 |
continue; |
| 915 |
} |
| 916 |
|
| 917 |
if (!isISO8859(k0)) |
| 918 |
maybe_iso8859 = 0; |
| 919 |
else |
| 920 |
ch_iso8859++; |
| 921 |
is_ascii = 0; |
| 922 |
if (pos_db==0) { |
| 923 |
cdb[0] = k0; |
| 924 |
cdb[1] = 0; |
| 925 |
pos_db = 1; |
| 926 |
} |
| 927 |
else if (pos_db==1 && (maybe_sjis || maybe_euc)) { |
| 928 |
cdb[1] = k0; |
| 929 |
k1 = JIStoUCS2(SJIStoJIS(HILO(cdb[0],cdb[1]))); |
| 930 |
k2 = JIStoUCS2(EUCtoJIS(HILO(cdb[0],cdb[1]))); |
| 931 |
if (maybe_sjis) { |
| 932 |
if (!k1) |
| 933 |
maybe_sjis = 0; |
| 934 |
else |
| 935 |
ch_sjis++; |
| 936 |
} |
| 937 |
if (maybe_euc) { |
| 938 |
if (!k2) |
| 939 |
maybe_euc = 0; |
| 940 |
else |
| 941 |
ch_euc++; |
| 942 |
} |
| 943 |
pos_db = 0; |
| 944 |
#ifdef DEBUG |
| 945 |
fprintf(stderr, "Character for guess encoding: 0x%02X%02X", cdb[0], cdb[1]); |
| 946 |
if (maybe_sjis || maybe_euc) { |
| 947 |
if (maybe_sjis) { |
| 948 |
i = UCStoUTF8S(k1, str0); |
| 949 |
str0[i] = '\0'; |
| 950 |
fprintf(stderr, " sjis (%s)", str0); |
| 951 |
} |
| 952 |
if (maybe_euc) { |
| 953 |
i = UCStoUTF8S(k2, str0); |
| 954 |
str0[i] = '\0'; |
| 955 |
fprintf(stderr, " euc (%s)", str0); |
| 956 |
} |
| 957 |
fprintf(stderr, "\n"); |
| 958 |
} else { |
| 959 |
fprintf(stderr, " not sjis nor euc\n"); |
| 960 |
} |
| 961 |
#endif /* DEBUG */ |
| 962 |
} |
| 963 |
if (pos_utf8==0) { |
| 964 |
len_utf8 = UTF8length(k0); |
| 965 |
if (len_utf8<2) { |
| 966 |
maybe_utf8 = 0; |
| 967 |
pos_utf8 = 0; |
| 968 |
continue; |
| 969 |
} |
| 970 |
cu8[0] = k0; |
| 971 |
pos_utf8 = 1; |
| 972 |
} |
| 973 |
else if (pos_utf8>0 && maybe_utf8) { |
| 974 |
if (k0>0xBF) { |
| 975 |
maybe_utf8 = 0; |
| 976 |
pos_utf8 = 0; |
| 977 |
continue; |
| 978 |
} |
| 979 |
cu8[pos_utf8] = k0; |
| 980 |
if (pos_utf8==1) { |
| 981 |
if ((cu8[0]==0xE0 && cu8[1]<0xA0) || |
| 982 |
(cu8[0]==0xED && cu8[1]>0x9F) || |
| 983 |
(cu8[0]==0xF0 && cu8[1]<0x90) || |
| 984 |
(cu8[0]==0xF4 && cu8[1]>0x8F)) { /* illegal combination in UTF-8 */ |
| 985 |
maybe_utf8 = 0; |
| 986 |
pos_utf8 = 0; |
| 987 |
continue; |
| 988 |
} |
| 989 |
} |
| 990 |
pos_utf8++; |
| 991 |
if (pos_utf8==len_utf8) { |
| 992 |
#ifdef DEBUG |
| 993 |
for (i=0; i<len_utf8; i++) str0[i] = cu8[i]; |
| 994 |
str0[i] = '\0'; |
| 995 |
fprintf(stderr, "Character for guess encoding: 0x"); |
| 996 |
for (i=0; i<len_utf8; i++) fprintf(stderr, "%02X", cu8[i]); |
| 997 |
fprintf(stderr, " U+%06lX (%s)\n", UTF8StoUCS(str0), str0); |
| 998 |
#endif /* DEBUG */ |
| 999 |
ch_utf8++; |
| 1000 |
len_utf8 = 0; |
| 1001 |
pos_utf8 = 0; |
| 1002 |
cu8[0]=cu8[1]=cu8[2]=cu8[3]=0; |
| 1003 |
} |
| 1004 |
} |
| 1005 |
} |
| 1006 |
|
| 1007 |
if (k0==EOF) { |
| 1008 |
if (pos_db) maybe_sjis = maybe_euc = 0; |
| 1009 |
if (pos_utf8) maybe_utf8 = 0; |
| 1010 |
} |
| 1011 |
#ifdef DEBUG |
| 1012 |
if (maybe_sjis+maybe_euc+maybe_utf8+maybe_iso8859>1) { |
| 1013 |
fprintf(stderr, |
| 1014 |
"Maybe(%d, %d, %d, %d), Multibyte characters(%d, %d, %d, %d), lbyte(%d)\n", |
| 1015 |
maybe_sjis, maybe_euc, maybe_utf8, maybe_iso8859, ch_sjis, ch_euc, ch_utf8, ch_iso8859, lbyte); |
| 1016 |
} |
| 1017 |
#endif /* DEBUG */ |
| 1018 |
if (maybe_iso8859) { |
| 1019 |
/* The threshold of ch_* to judge ISO-8859 or not is heuristic, not strict */ |
| 1020 |
if ((maybe_sjis && ch_sjis>=16 && ch_sjis*1.3<=ch_iso8859) || |
| 1021 |
(maybe_euc && ch_euc >=8 && ch_euc *2 ==ch_iso8859) || |
| 1022 |
(maybe_utf8 && ch_utf8>=8 && ch_utf8*2 <=ch_iso8859) || |
| 1023 |
bom==4) |
| 1024 |
maybe_iso8859 = 0; |
| 1025 |
} |
| 1026 |
if (is_ascii) |
| 1027 |
strcpy(enc,"ASCII"); |
| 1028 |
else if (maybe_sjis+maybe_euc+maybe_utf8+maybe_iso8859>1) { |
| 1029 |
guess_enc = ENC_UNKNOWN; |
| 1030 |
strcpy(enc,"AMBIGUOUS("); |
| 1031 |
if (maybe_sjis) { |
| 1032 |
enc = strcat(enc,"s"); |
| 1033 |
if (file_enc == ENC_SJIS) guess_enc = ENC_SJIS; |
| 1034 |
} |
| 1035 |
if (maybe_euc) { |
| 1036 |
enc = strcat(enc, maybe_sjis ? ",e" : "e"); |
| 1037 |
if (file_enc == ENC_EUC) guess_enc = ENC_EUC; |
| 1038 |
} |
| 1039 |
if (maybe_utf8) { |
| 1040 |
enc = strcat(enc, maybe_sjis || maybe_euc ? ",u" : "u"); |
| 1041 |
if (file_enc == ENC_UTF8) guess_enc = ENC_UTF8; |
| 1042 |
} |
| 1043 |
if (maybe_iso8859) |
| 1044 |
enc = strcat(enc, ",i"); |
| 1045 |
enc = strcat(enc,")"); |
| 1046 |
if (maybe_iso8859 && maybe_euc+maybe_sjis+maybe_utf8==1) { |
| 1047 |
if (maybe_sjis) guess_enc = ENC_SJIS; |
| 1048 |
if (maybe_euc) guess_enc = ENC_EUC; |
| 1049 |
if (maybe_utf8) guess_enc = ENC_UTF8; |
| 1050 |
} |
| 1051 |
#ifdef WIN32 |
| 1052 |
if (guess_enc == ENC_UNKNOWN) guess_enc = ENC_UTF8; |
| 1053 |
#else |
| 1054 |
if (guess_enc == ENC_UNKNOWN) guess_enc = get_terminal_enc(); |
| 1055 |
#endif |
| 1056 |
} |
| 1057 |
else if (maybe_sjis) |
| 1058 |
strcpy(enc,"Shift_JIS"); |
| 1059 |
else if (maybe_euc) |
| 1060 |
strcpy(enc,"EUC-JP"); |
| 1061 |
else if (maybe_utf8) { |
| 1062 |
strcpy(enc,"UTF-8"); |
| 1063 |
if (bom>=3) strcat(enc," (BOM)"); |
| 1064 |
} |
| 1065 |
else if (maybe_iso8859) |
| 1066 |
strcpy(enc,"ISO-8859"); |
| 1067 |
else |
| 1068 |
strcpy(enc,"BINARY"); |
| 1069 |
post_process: |
| 1070 |
if (chk_nl && (nl_cr || nl_lf || nl_crlf)) { |
| 1071 |
if (nl_lf+nl_crlf==0) strcat(enc," (CR)"); |
| 1072 |
else if (nl_cr+nl_crlf==0) strcat(enc," (LF)"); |
| 1073 |
else if (nl_cr+nl_lf ==0) strcat(enc," (CRLF)"); |
| 1074 |
else strcat(enc," (MIXED NL)"); |
| 1075 |
} |
| 1076 |
rewind (fp); |
| 1077 |
return enc; |
| 1078 |
} |
| 1079 |
|
| 1080 |
void ptenc_set_infile_enc_auto(void) |
| 1081 |
{ |
| 1082 |
char *p; |
| 1083 |
if (infile_enc_auto == 2) { |
| 1084 |
p = kpse_var_value ("guess_input_kanji_encoding"); |
| 1085 |
if (p) { |
| 1086 |
if (*p == '1' || *p == 'y' || *p == 't') infile_enc_auto = 1; |
| 1087 |
free(p); |
| 1088 |
} |
| 1089 |
} |
| 1090 |
if (infile_enc_auto == 2) infile_enc_auto = 0; |
| 1091 |
} |
| 1092 |
|
| 1093 |
/* input line with encoding conversion */ |
| 1094 |
long input_line2(FILE *fp, unsigned char *buff, unsigned char *buff2, |
| 1095 |
long pos, const long buffsize, int *lastchar) |
| 1096 |
{ |
| 1097 |
long i = 0; |
| 1098 |
static boolean injis = false; |
| 1099 |
const int fd = fileno(fp); |
| 1100 |
|
| 1101 |
buffer = buff; |
| 1102 |
first = last = pos; |
| 1103 |
|
| 1104 |
if (infile_enc[fd] == ENC_UNKNOWN) { /* just after opened */ |
| 1105 |
ungetbuff[fd].size = 0; |
| 1106 |
if (isUTF8Nstream(fp)) { |
| 1107 |
infile_enc[fd] = ENC_UTF8; |
| 1108 |
#ifdef DEBUG |
| 1109 |
fprintf(stderr, "Detect UTF-8 with BOM #%d\n", fd); |
| 1110 |
#endif /* DEBUG */ |
| 1111 |
} |
| 1112 |
else { |
| 1113 |
struct stat st; |
| 1114 |
if (infile_enc_auto == 2) ptenc_set_infile_enc_auto(); |
| 1115 |
#ifdef DEBUG |
| 1116 |
if (infile_enc_auto) { |
| 1117 |
fprintf(stderr, "\nInput fd: %d, stdin?: %d, pipe?: %d\n", fd, |
| 1118 |
fd==fileno(stdin), (fstat(fd, &st)==0 && S_ISFIFO(st.st_mode))); |
| 1119 |
} |
| 1120 |
#endif /* DEBUG */ |
| 1121 |
if (infile_enc_auto && fd != fileno(stdin) |
| 1122 |
&& !(fstat(fd, &st)==0 && S_ISFIFO(st.st_mode))) { |
| 1123 |
char *enc; |
| 1124 |
getc4(fp); |
| 1125 |
getc4(fp); |
| 1126 |
getc4(fp); |
| 1127 |
getc4(fp); |
| 1128 |
rewind(fp); |
| 1129 |
enc = ptenc_guess_enc(fp, 0, 0); |
| 1130 |
if (string_to_enc(enc) > 0) { |
| 1131 |
infile_enc[fd] = string_to_enc(enc); |
| 1132 |
fprintf(stderr, "(guessed encoding #%d: %s = %s)", fd, enc, enc_to_string(infile_enc[fd])); |
| 1133 |
} else { |
| 1134 |
infile_enc[fd] = get_file_enc(); |
| 1135 |
} |
| 1136 |
if (enc) free(enc); |
| 1137 |
} |
| 1138 |
else infile_enc[fd] = get_file_enc(); |
| 1139 |
} |
| 1140 |
} |
| 1141 |
|
| 1142 |
while (last < buffsize-30 && (i=getc4(fp)) != EOF && i!='\n' && i!='\r') { |
| 1143 |
/* 30 is enough large size for one char */ |
| 1144 |
/* attention: 4 times of write_hex() eats 16byte */ |
| 1145 |
#ifdef WIN32 |
| 1146 |
if (i == 0x1a && first == last && |
| 1147 |
fd == fileno(stdin) && _isatty(fd)) { /* Ctrl+Z on console */ |
| 1148 |
i = EOF; |
| 1149 |
break; |
| 1150 |
} else |
| 1151 |
#endif |
| 1152 |
if (i == ESC) { |
| 1153 |
if ((i=getc4(fp)) == '$') { /* ESC '$' (Kanji-in) */ |
| 1154 |
i = getc4(fp); |
| 1155 |
if (i == '@' || i == 'B') { |
| 1156 |
injis = true; |
| 1157 |
} else { /* broken Kanji-in */ |
| 1158 |
buffer[last++] = ESC; |
| 1159 |
buffer[last++] = '$'; |
| 1160 |
if (is_tail(&i, fp)) break; |
| 1161 |
buffer[last++] = i; |
| 1162 |
} |
| 1163 |
} else if (i == '(') { /* ESC '(' (Kanji-out) */ |
| 1164 |
i = getc4(fp); |
| 1165 |
if (i == 'J' || i == 'B' || i == 'H') { |
| 1166 |
injis = false; |
| 1167 |
} else { /* broken Kanji-out */ |
| 1168 |
buffer[last++] = ESC; |
| 1169 |
buffer[last++] = '('; |
| 1170 |
if (is_tail(&i, fp)) break; |
| 1171 |
buffer[last++] = i; |
| 1172 |
} |
| 1173 |
} else { /* broken ESC */ |
| 1174 |
buffer[last++] = ESC; |
| 1175 |
if (is_tail(&i, fp)) break; |
| 1176 |
buffer[last++] = i; |
| 1177 |
} |
| 1178 |
} else { /* rather than ESC */ |
| 1179 |
if (injis) { /* in JIS */ |
| 1180 |
long j = getc4(fp); |
| 1181 |
if (is_tail(&j, fp)) { |
| 1182 |
buffer[last++] = i; |
| 1183 |
i = j; |
| 1184 |
break; |
| 1185 |
} else { /* JIS encoding */ |
| 1186 |
i = fromJIS(HILO(i,j)); |
| 1187 |
if (i == 0) i = fromUCS(U_REPLACEMENT_CHARACTER); |
| 1188 |
write_multibyte(toBUFF(i)); |
| 1189 |
} |
| 1190 |
} else { /* normal */ |
| 1191 |
if (infile_enc[fd] == ENC_SJIS && isSJISkanji1(i)) { |
| 1192 |
get_sjis(i, fp); |
| 1193 |
} else if (infile_enc[fd] == ENC_EUC && isEUCkanji1(i)) { |
| 1194 |
get_euc(i, fp); |
| 1195 |
} else if (infile_enc[fd] == ENC_UTF8 && UTF8length(i) > 1) { |
| 1196 |
get_utf8(i, fp); |
| 1197 |
} else { |
| 1198 |
buffer[last++] = i; |
| 1199 |
} |
| 1200 |
} |
| 1201 |
} |
| 1202 |
} |
| 1203 |
|
| 1204 |
if (i != EOF || first != last) buffer[last] = '\0'; |
| 1205 |
if (i == EOF || i == '\n' || i == '\r') injis = false; |
| 1206 |
if (lastchar != NULL) *lastchar = i; |
| 1207 |
|
| 1208 |
if (buff2!= NULL) for (i=pos; i<=last; i++) buff2[i] = 0; |
| 1209 |
/* buff2 is initialized */ |
| 1210 |
|
| 1211 |
return last; |
| 1212 |
} |
| 1213 |
|
| 1214 |
/* set encode of stdin if fp = NULL */ |
| 1215 |
boolean setinfileenc(FILE *fp, const char *str) |
| 1216 |
{ |
| 1217 |
int enc; |
| 1218 |
enc = string_to_enc(str); |
| 1219 |
if (enc < 0) return false; |
| 1220 |
infile_enc[fileno(fp)] = enc; |
| 1221 |
return true; |
| 1222 |
} |
| 1223 |
|
| 1224 |
boolean setstdinenc(const char *str) |
| 1225 |
{ |
| 1226 |
int enc; |
| 1227 |
enc = string_to_enc(str); |
| 1228 |
if (enc < 0) return false; |
| 1229 |
infile_enc[fileno(stdin)] = enc; |
| 1230 |
return true; |
| 1231 |
} |
| 1232 |
|
| 1233 |
boolean setfileenc(const char *str) |
| 1234 |
{ |
| 1235 |
int enc; |
| 1236 |
enc = string_to_enc(str); |
| 1237 |
if (enc < 0) return false; |
| 1238 |
file_enc = enc; |
| 1239 |
return true; |
| 1240 |
} |
| 1241 |
|
| 1242 |
#ifdef WIN32 |
| 1243 |
void clear_infile_enc(FILE *fp) |
| 1244 |
{ |
| 1245 |
infile_enc[fileno(fp)] = ENC_UNKNOWN; |
| 1246 |
} |
| 1247 |
long ptenc_conv_first_line(long pos, long last, unsigned char *buff, const long buffsize) |
| 1248 |
{ |
| 1249 |
return last; |
| 1250 |
} |
| 1251 |
#else /* !WIN32 */ |
| 1252 |
static const_string in_filter = NULL; |
| 1253 |
static FILE *piped_fp[NOFILE]; |
| 1254 |
static int piped_num = 0; |
| 1255 |
|
| 1256 |
void nkf_disable(void) |
| 1257 |
{ |
| 1258 |
in_filter = ""; |
| 1259 |
} |
| 1260 |
|
| 1261 |
#ifdef NKF_TEST |
| 1262 |
static void nkf_check(void) |
| 1263 |
{ |
| 1264 |
if (piped_num > 0) { |
| 1265 |
fprintf(stderr, "nkf_check: %d nkf_open() did not closed.\n", |
| 1266 |
piped_num); |
| 1267 |
} else { |
| 1268 |
fprintf(stderr, "nkf_check: nkf_open() OK.\n"); |
| 1269 |
} |
| 1270 |
} |
| 1271 |
#endif /* NKF_TEST */ |
| 1272 |
|
| 1273 |
/* 'mode' must be read */ |
| 1274 |
FILE *nkf_open(const char *path, const char *mode) { |
| 1275 |
char buff[PATH_MAX * 2 + 20]; /* 20 is enough gaps */ |
| 1276 |
char *name; |
| 1277 |
FILE *fp; |
| 1278 |
|
| 1279 |
if (in_filter == NULL) { |
| 1280 |
in_filter = kpse_var_value("PTEX_IN_FILTER"); |
| 1281 |
if (in_filter == NULL || strcasecmp(in_filter, "no") == 0) { |
| 1282 |
nkf_disable(); |
| 1283 |
} |
| 1284 |
#ifdef NKF_TEST |
| 1285 |
atexit(nkf_check); |
| 1286 |
#endif /* NKF_TEST */ |
| 1287 |
} |
| 1288 |
|
| 1289 |
if (in_filter[0] == '\0') return fopen(path, mode); |
| 1290 |
name = xstrdup(path); |
| 1291 |
if (kpse_readable_file(name) == NULL) { |
| 1292 |
free(name); |
| 1293 |
return NULL; /* can't read */ |
| 1294 |
} |
| 1295 |
|
| 1296 |
sprintf(buff, "%.*s < '%.*s'", PATH_MAX, in_filter, PATH_MAX, path); |
| 1297 |
free(name); |
| 1298 |
/* fprintf(stderr, "\n`%s`", buff); */ |
| 1299 |
fp = popen(buff , "r"); |
| 1300 |
if (piped_num < NOFILE) piped_fp[piped_num++] = fp; |
| 1301 |
return fp; |
| 1302 |
} |
| 1303 |
|
| 1304 |
/* we must close in stack order (FILO) or in queue order (FIFO) */ |
| 1305 |
int nkf_close(FILE *fp) { |
| 1306 |
infile_enc[fileno(fp)] = ENC_UNKNOWN; |
| 1307 |
if (piped_num > 0) { |
| 1308 |
if (fp == piped_fp[piped_num-1]) { /* for FILO */ |
| 1309 |
piped_num--; |
| 1310 |
return pclose(fp); |
| 1311 |
} |
| 1312 |
if (fp == piped_fp[0]) { /* for FIFO */ |
| 1313 |
int i; |
| 1314 |
piped_num--; |
| 1315 |
for (i=0; i<piped_num; i++) piped_fp[i] = piped_fp[i+1]; |
| 1316 |
return pclose(fp); |
| 1317 |
} |
| 1318 |
} |
| 1319 |
return fclose(fp); |
| 1320 |
} |
| 1321 |
|
| 1322 |
#define break_if_bad_utf8_second(k) if ((k<0x80)||(k>0xBF)) { i--; k='\0'; break; } |
| 1323 |
#define write_hex_if_not_ascii(c) \ |
| 1324 |
if ((c>=0x20)&&(c<=0x7E)) buffer[last++]=c; else write_hex(c); |
| 1325 |
unsigned char *ptenc_from_utf8_string_to_internal_enc(const unsigned char *is) |
| 1326 |
{ |
| 1327 |
int i; |
| 1328 |
long u = 0, j, len; |
| 1329 |
int i1, i2, i3, i4; |
| 1330 |
unsigned char *buf, *buf_bak; |
| 1331 |
long first_bak, last_bak; |
| 1332 |
|
| 1333 |
if (terminal_enc != ENC_UTF8 || is_internalUPTEX()) return NULL; |
| 1334 |
buf_bak = buffer; |
| 1335 |
first_bak = first; |
| 1336 |
last_bak = last; |
| 1337 |
|
| 1338 |
len = strlen(is)+1; |
| 1339 |
buffer = buf = xmalloc(len); |
| 1340 |
first = last = 0; |
| 1341 |
|
| 1342 |
for (i=0; i<strlen(is); i++) { |
| 1343 |
i1 = is[i]; i2 = i3 = i4 = '\0'; |
| 1344 |
switch (UTF8length(i1)) { |
| 1345 |
case 1: |
| 1346 |
buffer[last++] = i1; /* ASCII */ |
| 1347 |
if (i1 == '\0') goto end; |
| 1348 |
continue; |
| 1349 |
case 2: |
| 1350 |
i2 = is[++i]; break_if_bad_utf8_second(i2); |
| 1351 |
u = UTF8BtoUCS(i1, i2); |
| 1352 |
break; |
| 1353 |
case 3: |
| 1354 |
i2 = is[++i]; break_if_bad_utf8_second(i2); |
| 1355 |
i3 = is[++i]; break_if_bad_utf8_second(i3); |
| 1356 |
u = UTF8CtoUCS(i1, i2, i3); |
| 1357 |
if (u == U_BOM) continue; /* just ignore */ |
| 1358 |
if (u == U_VOICED && combin_voiced_sound(false)) continue; |
| 1359 |
if (u == U_SEMI_VOICED && combin_voiced_sound(true)) continue; |
| 1360 |
break; |
| 1361 |
case 4: |
| 1362 |
i2 = is[++i]; break_if_bad_utf8_second(i2); |
| 1363 |
i3 = is[++i]; break_if_bad_utf8_second(i3); |
| 1364 |
i4 = is[++i]; break_if_bad_utf8_second(i4); |
| 1365 |
u = UTF8DtoUCS(i1, i2, i3, i4); |
| 1366 |
break; |
| 1367 |
default: |
| 1368 |
u = U_REPLACEMENT_CHARACTER; |
| 1369 |
break; |
| 1370 |
} |
| 1371 |
|
| 1372 |
j = (u != 0) ? toBUFF(fromUCS(u)) : 0; |
| 1373 |
if (j == 0) { /* can't represent in EUC/SJIS */ |
| 1374 |
if (last+16>=len) buffer = buf = xrealloc(buffer, len=last+64); |
| 1375 |
write_hex_if_not_ascii(i1); |
| 1376 |
if (i2 != '\0') write_hex_if_not_ascii(i2); |
| 1377 |
if (i3 != '\0') write_hex_if_not_ascii(i3); |
| 1378 |
if (i4 != '\0') write_hex_if_not_ascii(i4); |
| 1379 |
} else { |
| 1380 |
write_multibyte(j); |
| 1381 |
} |
| 1382 |
} |
| 1383 |
buffer[last] = '\0'; |
| 1384 |
end: |
| 1385 |
buffer = buf_bak; |
| 1386 |
first = first_bak; |
| 1387 |
last = last_bak; |
| 1388 |
return buf; |
| 1389 |
} |
| 1390 |
|
| 1391 |
unsigned char *ptenc_from_internal_enc_string_to_utf8(const unsigned char *is) |
| 1392 |
{ |
| 1393 |
int i; |
| 1394 |
long u = 0, len; |
| 1395 |
int i1 = EOF, i2 = EOF; |
| 1396 |
unsigned char *buf, *buf_bak; |
| 1397 |
long first_bak, last_bak; |
| 1398 |
|
| 1399 |
if (terminal_enc != ENC_UTF8 || is_internalUPTEX()) return NULL; |
| 1400 |
buf_bak = buffer; |
| 1401 |
first_bak = first; |
| 1402 |
last_bak = last; |
| 1403 |
|
| 1404 |
len = strlen(is)+1; |
| 1405 |
buffer = buf = xmalloc(len*4); |
| 1406 |
first = last = 0; |
| 1407 |
|
| 1408 |
for (i=0; i<strlen(is); i++) { |
| 1409 |
i1 = is[i]; |
| 1410 |
switch (multibytelen(i1)) { |
| 1411 |
case 1: |
| 1412 |
buffer[last++] = i1; /* ASCII */ |
| 1413 |
if (i1 == '\0') goto end; |
| 1414 |
continue; |
| 1415 |
case 2: /* i1: not ASCII */ |
| 1416 |
i2 = is[++i]; |
| 1417 |
if (i2 == '\0') { |
| 1418 |
write_hex(i1); continue; |
| 1419 |
} else { |
| 1420 |
u = JIStoUCS2(toJIS(HILO(i1,i2))); |
| 1421 |
if (u==0) { |
| 1422 |
write_hex(i1); write_hex_if_not_ascii(i2); continue; |
| 1423 |
} |
| 1424 |
} |
| 1425 |
break; |
| 1426 |
default: /* reachable only if internal code is uptex */ |
| 1427 |
u = U_REPLACEMENT_CHARACTER; |
| 1428 |
break; |
| 1429 |
} |
| 1430 |
write_multibyte(UCStoUTF8(u)); |
| 1431 |
} |
| 1432 |
buffer[last] = '\0'; |
| 1433 |
end: |
| 1434 |
buffer = buf_bak; |
| 1435 |
first = first_bak; |
| 1436 |
last = last_bak; |
| 1437 |
return buf; |
| 1438 |
} |
| 1439 |
|
| 1440 |
long ptenc_conv_first_line(long pos, long last, unsigned char *buff, const long buffsize) |
| 1441 |
/* return new last */ |
| 1442 |
{ |
| 1443 |
unsigned char *old, *new_buf; long new_last, i; |
| 1444 |
if (internal_enc==ENC_UPTEX) return last; /* no conversion needed */ |
| 1445 |
old = xmalloc(last-pos+2); |
| 1446 |
if (old==NULL) return last; |
| 1447 |
strncpy(old, buff+pos, last-pos+1); old[last-pos+1]='\0'; |
| 1448 |
new_buf = ptenc_from_utf8_string_to_internal_enc(old); |
| 1449 |
if (new_buf==NULL) { free(old); return last; } |
| 1450 |
new_last=pos+strlen(new_buf)-1; |
| 1451 |
if (new_last>=buffsize) new_last=buffsize-1; |
| 1452 |
for (i=0;i<strlen(new_buf); i++) buff[pos+i]=new_buf[i]; |
| 1453 |
free(old); free(new_buf); |
| 1454 |
return new_last; |
| 1455 |
} |
| 1456 |
|
| 1457 |
#endif /* !WIN32 */ |