| 1 |
/* knj.c: check for 2-Byte Kanji (CP 932, SJIS) codes. |
| 2 |
|
| 3 |
Copyright 2010, 2016, 2018 Akira Kakuto. |
| 4 |
Copyright 2013, 2016, 2020 TANAKA Takuji. |
| 5 |
|
| 6 |
This library is free software; you can redistribute it and/or |
| 7 |
modify it under the terms of the GNU Lesser General Public |
| 8 |
License as published by the Free Software Foundation; either |
| 9 |
version 2.1 of the License, or (at your option) any later version. |
| 10 |
|
| 11 |
This library is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
Lesser General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU Lesser General Public License |
| 17 |
along with this library; if not, see <http://www.gnu.org/licenses/>. */ |
| 18 |
|
| 19 |
#include <kpathsea/config.h> |
| 20 |
#include <kpathsea/debug.h> |
| 21 |
#include <wchar.h> |
| 22 |
|
| 23 |
static int |
| 24 |
is_include_space(const char *s) |
| 25 |
{ |
| 26 |
char *p; |
| 27 |
p = strchr(s, ' '); |
| 28 |
if(p) return 1; |
| 29 |
p = strchr(s, '\t'); |
| 30 |
if(p) return 1; |
| 31 |
return 0; |
| 32 |
} |
| 33 |
|
| 34 |
/* |
| 35 |
Get wide string from multibyte string. |
| 36 |
*/ |
| 37 |
wchar_t * |
| 38 |
get_wstring_from_mbstring(int cp, const char *mbstr, wchar_t *wstr) |
| 39 |
{ |
| 40 |
int len; |
| 41 |
|
| 42 |
len = MultiByteToWideChar(cp, 0, mbstr, -1, wstr, 0); |
| 43 |
if (len==0) { |
| 44 |
FATAL("cannot convert string to wide string"); |
| 45 |
} |
| 46 |
if (wstr==NULL) { |
| 47 |
wstr = xmalloc(sizeof(wchar_t)*(len+1)); |
| 48 |
} |
| 49 |
len = MultiByteToWideChar(cp, 0, mbstr, -1, wstr, len+1); |
| 50 |
if (len==0) { |
| 51 |
FATAL("cannot convert multibyte string to wide string"); |
| 52 |
} |
| 53 |
return wstr; |
| 54 |
} |
| 55 |
|
| 56 |
/* |
| 57 |
Get multibyte string from wide string. |
| 58 |
*/ |
| 59 |
char * |
| 60 |
get_mbstring_from_wstring(int cp, const wchar_t *wstr, char *mbstr) |
| 61 |
{ |
| 62 |
int len; |
| 63 |
|
| 64 |
len = WideCharToMultiByte(cp, 0, wstr, -1, mbstr, 0, NULL, NULL); |
| 65 |
if (len==0) { |
| 66 |
FATAL("cannot convert string to multibyte string"); |
| 67 |
} |
| 68 |
if (mbstr==NULL) { |
| 69 |
mbstr = xmalloc(len+1); |
| 70 |
} |
| 71 |
len = WideCharToMultiByte(cp, 0, wstr, -1, mbstr, len+1, NULL, NULL); |
| 72 |
if (len==0) { |
| 73 |
FATAL("cannot convert wide string to multibyte string"); |
| 74 |
} |
| 75 |
return mbstr; |
| 76 |
} |
| 77 |
|
| 78 |
int |
| 79 |
kpathsea_isknj(kpathsea kpse, int c) |
| 80 |
{ |
| 81 |
c &= 0xff; |
| 82 |
switch (kpse->Is_cp932_system) { |
| 83 |
case 932: |
| 84 |
return((c>=0x81 && c<=0x9f) || (c>=0xe0 && c<=0xfc)); |
| 85 |
case 936: |
| 86 |
return(c>=0x81 && c<=0xfe); |
| 87 |
case 950: |
| 88 |
return((c>=0xa1 && c<=0xc6) || (c>=0xc9 && c<=0xf9)); |
| 89 |
default: |
| 90 |
return(0); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
int |
| 95 |
kpathsea_isknj2(kpathsea kpse, int c) |
| 96 |
{ |
| 97 |
c &= 0xff; |
| 98 |
switch (kpse->Is_cp932_system) { |
| 99 |
case 932: |
| 100 |
return(c>=0x40 && c<=0xfc && c!=0x7f); |
| 101 |
case 936: |
| 102 |
return(c>=0x40 && c<=0xfe && c!=0x7f); |
| 103 |
case 950: |
| 104 |
return((c>=0x40 && c<=0x7e) || (c>=0xa1 && c<=0xfe)); |
| 105 |
default: |
| 106 |
return(0); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
/* |
| 111 |
xfopen by file system codepage |
| 112 |
*/ |
| 113 |
FILE * |
| 114 |
kpathsea_fsyscp_xfopen (kpathsea kpse, const char *filename, const char *mode) |
| 115 |
{ |
| 116 |
FILE *f; |
| 117 |
wchar_t *fnamew, modew[4]; |
| 118 |
int i; |
| 119 |
unsigned char *fnn; |
| 120 |
unsigned char *p = NULL; |
| 121 |
size_t len; |
| 122 |
|
| 123 |
assert(filename && mode); |
| 124 |
len = strlen(filename); |
| 125 |
/* |
| 126 |
Support very long input path name, longer than _MAX_PATH for |
| 127 |
Windows, if it really exists and input name is given in |
| 128 |
full-absolute path in a command line. |
| 129 |
/./ , /../, \.\, \..\ should be excluded. (2020/06/06) |
| 130 |
More than one adjacent directory separators should be |
| 131 |
excluded. (2020/10/24) |
| 132 |
The "nul" device should be excluded. (2021/04/07). |
| 133 |
*/ |
| 134 |
fnn = xmalloc(len + 10); |
| 135 |
|
| 136 |
if (stricmp(filename + len - 3, "nul") == 0) |
| 137 |
p = (unsigned char *)filename; |
| 138 |
else if (stricmp(filename + len - 4, "nul:") == 0) |
| 139 |
p = (unsigned char *)filename; |
| 140 |
else if (stricmp(filename + len - 7, "nul.tex") == 0) |
| 141 |
p = (unsigned char *)filename; |
| 142 |
else if (stricmp(filename + len - 8, "nul:.tex") == 0) |
| 143 |
p = (unsigned char *)filename; |
| 144 |
|
| 145 |
if (!p) { |
| 146 |
p = strstr(filename, ".\\"); |
| 147 |
} |
| 148 |
if (!p) { |
| 149 |
p = strstr(filename, "./"); |
| 150 |
} |
| 151 |
if (!p && len > 2) { |
| 152 |
p = strstr(filename + 2, "//"); |
| 153 |
} |
| 154 |
if (!p && len > 2) { |
| 155 |
p = strstr(filename + 2, "\\\\"); |
| 156 |
} |
| 157 |
if (!p && len > 2) { |
| 158 |
p = strstr(filename + 2, "\\/"); |
| 159 |
} |
| 160 |
if (!p && len > 2) { |
| 161 |
p = strstr(filename + 2, "/\\"); |
| 162 |
} |
| 163 |
if (!p && len > 2 && ((filename[0] == '/' && filename[1] == '/') || |
| 164 |
(filename[0] == '\\' && filename[1] == '\\' && |
| 165 |
filename[2] != '?'))) { |
| 166 |
filename += 2; |
| 167 |
strcpy (fnn, "\\\\?\\UNC\\"); |
| 168 |
strcat (fnn, filename); |
| 169 |
} else if (!p && len > 2 && filename[1] == ':') { |
| 170 |
strcpy (fnn, "\\\\?\\"); |
| 171 |
strcat (fnn, filename); |
| 172 |
} else { |
| 173 |
strcpy (fnn, filename); |
| 174 |
} |
| 175 |
|
| 176 |
for (p = fnn; *p; p++) { |
| 177 |
if (*p == '/') |
| 178 |
*p = '\\'; |
| 179 |
} |
| 180 |
|
| 181 |
fnamew = get_wstring_from_mbstring(kpse->File_system_codepage, fnn, fnamew=NULL); |
| 182 |
for(i=0; (modew[i]=(wchar_t)mode[i]); i++) {} /* mode[i] must be ASCII */ |
| 183 |
f = _wfopen(fnamew, modew); |
| 184 |
free (fnn); |
| 185 |
if (f == NULL) |
| 186 |
FATAL_PERROR(filename); |
| 187 |
if (KPATHSEA_DEBUG_P (KPSE_DEBUG_FOPEN)) { |
| 188 |
DEBUGF_START (); |
| 189 |
fprintf (stderr, "fsyscp_xfopen(%s [", filename); |
| 190 |
WriteConsoleW( GetStdHandle( STD_ERROR_HANDLE ), fnamew, wcslen( fnamew ), NULL, NULL ); |
| 191 |
#if defined(_WIN64) |
| 192 |
fprintf (stderr, "], %s) => 0x%I64x\n", mode, (unsigned __int64) f); |
| 193 |
#else |
| 194 |
fprintf (stderr, "], %s) => 0x%lx\n", mode, (unsigned long) f); |
| 195 |
#endif |
| 196 |
DEBUGF_END (); |
| 197 |
} |
| 198 |
free(fnamew); |
| 199 |
|
| 200 |
return f; |
| 201 |
} |
| 202 |
|
| 203 |
/* |
| 204 |
fopen by file system codepage |
| 205 |
*/ |
| 206 |
FILE * |
| 207 |
kpathsea_fsyscp_fopen (kpathsea kpse, const char *filename, const char *mode) |
| 208 |
{ |
| 209 |
FILE *f; |
| 210 |
wchar_t *fnamew, modew[4]; |
| 211 |
int i; |
| 212 |
unsigned char *fnn; |
| 213 |
unsigned char *p = NULL; |
| 214 |
size_t len; |
| 215 |
|
| 216 |
assert(filename && mode); |
| 217 |
len = strlen(filename); |
| 218 |
/* |
| 219 |
Support very long input path name, longer than _MAX_PATH for |
| 220 |
Windows, if it really exists and input name is given in |
| 221 |
full-absolute path in a command line. |
| 222 |
/./ , /../, \.\, \..\ should be excluded. (2020/06/06) |
| 223 |
More than one adjacent directory separators should be |
| 224 |
excluded. (2020/10/24) |
| 225 |
The "nul" device should be excluded. (2021/04/07). |
| 226 |
*/ |
| 227 |
fnn = xmalloc(len + 10); |
| 228 |
|
| 229 |
if (stricmp(filename + len - 3, "nul") == 0) |
| 230 |
p = (unsigned char *)filename; |
| 231 |
else if (stricmp(filename + len - 4, "nul:") == 0) |
| 232 |
p = (unsigned char *)filename; |
| 233 |
else if (stricmp(filename + len - 7, "nul.tex") == 0) |
| 234 |
p = (unsigned char *)filename; |
| 235 |
else if (stricmp(filename + len - 8, "nul:.tex") == 0) |
| 236 |
p = (unsigned char *)filename; |
| 237 |
|
| 238 |
if (!p) { |
| 239 |
p = strstr(filename, ".\\"); |
| 240 |
} |
| 241 |
if (!p) { |
| 242 |
p = strstr(filename, "./"); |
| 243 |
} |
| 244 |
if (!p && len > 2) { |
| 245 |
p = strstr(filename + 2, "//"); |
| 246 |
} |
| 247 |
if (!p && len > 2) { |
| 248 |
p = strstr(filename + 2, "\\\\"); |
| 249 |
} |
| 250 |
if (!p && len > 2) { |
| 251 |
p = strstr(filename + 2, "\\/"); |
| 252 |
} |
| 253 |
if (!p && len > 2) { |
| 254 |
p = strstr(filename + 2, "/\\"); |
| 255 |
} |
| 256 |
if (!p && len > 2 && ((filename[0] == '/' && filename[1] == '/') || |
| 257 |
(filename[0] == '\\' && filename[1] == '\\' && |
| 258 |
filename[2] != '?'))) { |
| 259 |
filename += 2; |
| 260 |
strcpy (fnn, "\\\\?\\UNC\\"); |
| 261 |
strcat (fnn, filename); |
| 262 |
} else if (!p && len > 2 && filename[1] == ':') { |
| 263 |
strcpy (fnn, "\\\\?\\"); |
| 264 |
strcat (fnn, filename); |
| 265 |
} else { |
| 266 |
strcpy (fnn, filename); |
| 267 |
} |
| 268 |
|
| 269 |
for (p = fnn; *p; p++) { |
| 270 |
if (*p == '/') |
| 271 |
*p = '\\'; |
| 272 |
} |
| 273 |
|
| 274 |
fnamew = get_wstring_from_mbstring(kpse->File_system_codepage, fnn, fnamew=NULL); |
| 275 |
for(i=0; (modew[i]=(wchar_t)mode[i]); i++) {} /* mode[i] must be ASCII */ |
| 276 |
f = _wfopen(fnamew, modew); |
| 277 |
free (fnn); |
| 278 |
if (f != NULL) { |
| 279 |
if (KPATHSEA_DEBUG_P (KPSE_DEBUG_FOPEN)) { |
| 280 |
DEBUGF_START (); |
| 281 |
fprintf (stderr, "fsyscp_fopen(%s [", filename); |
| 282 |
WriteConsoleW( GetStdHandle( STD_ERROR_HANDLE ), fnamew, wcslen( fnamew ), NULL, NULL ); |
| 283 |
#if defined(_WIN64) |
| 284 |
fprintf (stderr, "], %s) => 0x%I64x\n", mode, (unsigned __int64) f); |
| 285 |
#else |
| 286 |
fprintf (stderr, "], %s) => 0x%lx\n", mode, (unsigned long) f); |
| 287 |
#endif |
| 288 |
DEBUGF_END (); |
| 289 |
} |
| 290 |
} |
| 291 |
free(fnamew); |
| 292 |
|
| 293 |
return f; |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
FILE * |
| 298 |
kpathsea_fsyscp_popen (kpathsea kpse, const char *command, const char *mode) |
| 299 |
{ |
| 300 |
FILE *f; |
| 301 |
wchar_t *commandw, modew[4]; |
| 302 |
int i; |
| 303 |
|
| 304 |
assert(command && mode); |
| 305 |
|
| 306 |
if (is_include_space (command)) { |
| 307 |
const char *p; |
| 308 |
char *command2, *q; |
| 309 |
command2 = xmalloc (strlen (command) + 3); |
| 310 |
p = command; |
| 311 |
q = command2; |
| 312 |
*q++ = '\"'; |
| 313 |
while (*p) |
| 314 |
*q++ = *p++; |
| 315 |
*q++ = '\"'; |
| 316 |
*q = '\0'; |
| 317 |
commandw = get_wstring_from_mbstring(kpse->File_system_codepage,command2, commandw=NULL); |
| 318 |
free (command2); |
| 319 |
} else { |
| 320 |
commandw = get_wstring_from_mbstring(kpse->File_system_codepage,command, commandw=NULL); |
| 321 |
} |
| 322 |
for(i=0; (modew[i]=(wchar_t)mode[i]); i++) {} /* mode[i] must be ASCII */ |
| 323 |
f = _wpopen(commandw, modew); |
| 324 |
|
| 325 |
if (f != NULL) { |
| 326 |
if (KPATHSEA_DEBUG_P (KPSE_DEBUG_FOPEN)) { |
| 327 |
DEBUGF_START (); |
| 328 |
fprintf (stderr, "fsyscp_popen(%s [", command); |
| 329 |
WriteConsoleW( GetStdHandle( STD_ERROR_HANDLE ), commandw, wcslen( commandw ), NULL, NULL ); |
| 330 |
#if defined(_WIN64) |
| 331 |
fprintf (stderr, "], %s) => 0x%I64x\n", mode, (unsigned __int64) f); |
| 332 |
#else |
| 333 |
fprintf (stderr, "], %s) => 0x%lx\n", mode, (unsigned long) f); |
| 334 |
#endif |
| 335 |
DEBUGF_END (); |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
free (commandw); |
| 340 |
/* We use always binary mode on Windows */ |
| 341 |
if(f) _setmode (fileno (f), _O_BINARY); |
| 342 |
|
| 343 |
return f; |
| 344 |
} |
| 345 |
|
| 346 |
int |
| 347 |
kpathsea_get_command_line_args_utf8 (kpathsea kpse, const_string enc, int *p_ac, char ***p_av) |
| 348 |
{ |
| 349 |
int argc; |
| 350 |
string *argv; |
| 351 |
|
| 352 |
if (!enc || !strncmp(enc,"",1)) return 0; |
| 353 |
|
| 354 |
#ifdef DEBUG |
| 355 |
fprintf(stderr, "command_line_encoding (%s)\n", enc); |
| 356 |
#endif /* DEBUG */ |
| 357 |
if (!(strncmp(enc,"utf8",5) && strncmp(enc,"utf-8",6))) { |
| 358 |
LPWSTR *argvw; |
| 359 |
INT argcw, i; |
| 360 |
string s; |
| 361 |
#ifdef DEBUG |
| 362 |
DWORD ret; |
| 363 |
HANDLE hStderr; |
| 364 |
hStderr = GetStdHandle( STD_ERROR_HANDLE ); |
| 365 |
#endif /* DEBUG */ |
| 366 |
kpse->File_system_codepage = CP_UTF8; |
| 367 |
/* |
| 368 |
IS_KANJI() in the CP932-like system seems to be necessary to |
| 369 |
support non-ascii values for variables in the case of |
| 370 |
command_line_encoding = utf-8. |
| 371 |
kpse->Is_cp932_system = 0; |
| 372 |
*/ |
| 373 |
argvw = CommandLineToArgvW(GetCommandLineW(), &argcw); |
| 374 |
argc = argcw; |
| 375 |
argv = xmalloc(sizeof(char *)*(argcw+1)); |
| 376 |
for (i=0; i<argcw; i++) { |
| 377 |
s = get_utf8_from_wstring(argvw[i], s=NULL); |
| 378 |
argv[i] = s; |
| 379 |
#ifdef DEBUG |
| 380 |
fprintf(stderr, "Commandline arguments %d:(%s) [", i, argv[i]); |
| 381 |
WriteConsoleW( hStderr, argvw[i], wcslen(argvw[i]), &ret, NULL); |
| 382 |
fprintf(stderr, "]\n"); |
| 383 |
#endif /* DEBUG */ |
| 384 |
} |
| 385 |
argv[argcw] = NULL; |
| 386 |
*p_ac = argc; |
| 387 |
*p_av = argv; |
| 388 |
return kpse->File_system_codepage; |
| 389 |
} else { |
| 390 |
return 0; |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
/* |
| 395 |
spawnvp by file system codepage |
| 396 |
*/ |
| 397 |
int |
| 398 |
kpathsea_fsyscp_spawnvp (kpathsea kpse, int mode, const char *command, const char* const *argv) |
| 399 |
{ |
| 400 |
int ret; |
| 401 |
wchar_t *commandw, **argvw, **pw; |
| 402 |
int i; |
| 403 |
const char* const *p; |
| 404 |
|
| 405 |
assert(command && argv); |
| 406 |
for (i = 0, p = argv; *p; p++) |
| 407 |
i++; |
| 408 |
argvw = xcalloc (i + 3, sizeof (wchar_t *)); |
| 409 |
commandw = get_wstring_from_mbstring(kpse->File_system_codepage, command, commandw=NULL); |
| 410 |
p = argv; |
| 411 |
pw = argvw; |
| 412 |
while (*p) { |
| 413 |
*pw = get_wstring_from_mbstring(kpse->File_system_codepage, *p, *pw=NULL); |
| 414 |
p++; |
| 415 |
pw++; |
| 416 |
} |
| 417 |
*pw = NULL; |
| 418 |
ret = _wspawnvp (mode, (const wchar_t *)commandw, (const wchar_t* const*) argvw); |
| 419 |
if(commandw) free(commandw); |
| 420 |
if (argvw) { |
| 421 |
pw = argvw; |
| 422 |
while (*pw) { |
| 423 |
free (*pw); |
| 424 |
pw++; |
| 425 |
} |
| 426 |
free (argvw); |
| 427 |
} |
| 428 |
|
| 429 |
return ret; |
| 430 |
} |
| 431 |
|
| 432 |
/* |
| 433 |
system by file system codepage |
| 434 |
*/ |
| 435 |
int |
| 436 |
kpathsea_fsyscp_system (kpathsea kpse, const char *cmd) |
| 437 |
{ |
| 438 |
const char *p; |
| 439 |
char *q; |
| 440 |
char *av[4]; |
| 441 |
int len, ret; |
| 442 |
int spacep = 0; |
| 443 |
|
| 444 |
if (cmd == NULL) |
| 445 |
return 1; |
| 446 |
|
| 447 |
av[0] = xstrdup ("cmd.exe"); |
| 448 |
av[1] = xstrdup ("/c"); |
| 449 |
|
| 450 |
len = strlen (cmd) + 3; |
| 451 |
spacep = is_include_space (cmd); |
| 452 |
av[2] = xmalloc (len); |
| 453 |
q = av[2]; |
| 454 |
if (spacep) |
| 455 |
*q++ = '"'; |
| 456 |
for (p = cmd; *p; p++, q++) { |
| 457 |
if (*p == '\'') |
| 458 |
*q = '"'; |
| 459 |
else |
| 460 |
*q = *p; |
| 461 |
} |
| 462 |
if (spacep) |
| 463 |
*q++ = '"'; |
| 464 |
*q = '\0'; |
| 465 |
av[3] = NULL; |
| 466 |
ret = kpathsea_fsyscp_spawnvp (kpse, _P_WAIT, av[0], (const char* const*) av); |
| 467 |
free (av[0]); |
| 468 |
free (av[1]); |
| 469 |
free (av[2]); |
| 470 |
return ret; |
| 471 |
} |
| 472 |
|
| 473 |
int |
| 474 |
kpathsea_win32_getc(kpathsea kpse, FILE *fp) |
| 475 |
{ |
| 476 |
const int fd = fileno(fp); |
| 477 |
HANDLE hStdin; |
| 478 |
DWORD ret; |
| 479 |
wchar_t wc[3]; |
| 480 |
char mbc[5]; |
| 481 |
int j; |
| 482 |
|
| 483 |
if (!(fd == fileno(stdin) && _isatty(fd) && kpse->File_system_codepage == CP_UTF8)) |
| 484 |
return getc(fp); |
| 485 |
|
| 486 |
if (kpse->getc_len == 0) |
| 487 |
{ |
| 488 |
hStdin = GetStdHandle(STD_INPUT_HANDLE); |
| 489 |
if (kpse->wcbuf) { |
| 490 |
wc[0] = kpse->wcbuf; |
| 491 |
kpse->wcbuf = L'\0'; |
| 492 |
} |
| 493 |
else if (ReadConsoleW(hStdin, wc, 1, &ret, NULL) == 0) |
| 494 |
return EOF; |
| 495 |
if (0xd800<=wc[0] && wc[0]<0xdc00) { |
| 496 |
if (ReadConsoleW(hStdin, wc+1, 1, &ret, NULL) == 0) |
| 497 |
return EOF; |
| 498 |
if (0xdc00<=wc[1] && wc[1]<0xe000) { |
| 499 |
wc[2]=L'\0'; |
| 500 |
} else { |
| 501 |
kpse->wcbuf=wc[1]; |
| 502 |
wc[0]=0xfffd; /* illegal surrogate pair */ |
| 503 |
wc[1]=L'\0'; |
| 504 |
} |
| 505 |
} else if (0xdc00<=wc[0] && wc[0]<0xe000) { |
| 506 |
wc[0]=0xfffd; /* illegal surrogate pair */ |
| 507 |
wc[1]=L'\0'; |
| 508 |
} else { |
| 509 |
wc[1]=L'\0'; |
| 510 |
} |
| 511 |
get_utf8_from_wstring(wc,mbc); |
| 512 |
j=strlen(mbc)-1; |
| 513 |
while(j>=0) { |
| 514 |
kpse->getc_buff[kpse->getc_len++]=(int)mbc[j--]; |
| 515 |
} |
| 516 |
} |
| 517 |
return kpse->getc_buff[--kpse->getc_len]; |
| 518 |
} |
| 519 |
|
| 520 |
int |
| 521 |
kpathsea_win32_ungetc(kpathsea kpse, int c, FILE *fp) |
| 522 |
{ |
| 523 |
const int fd = fileno(fp); |
| 524 |
|
| 525 |
if (!(fd == fileno(stdin) && _isatty(fd) && kpse->File_system_codepage == CP_UTF8)) |
| 526 |
return ungetc(c, fp); |
| 527 |
|
| 528 |
assert(kpse->getc_len < 4); |
| 529 |
return kpse->getc_buff[kpse->getc_len++] = c; |
| 530 |
} |
| 531 |
|
| 532 |
static int __win32_fputs(const char *str, HANDLE hStdout) |
| 533 |
{ |
| 534 |
DWORD ret; |
| 535 |
wchar_t *wstr; |
| 536 |
|
| 537 |
wstr = get_wstring_from_utf8(str, wstr=NULL); |
| 538 |
|
| 539 |
if (WriteConsoleW(hStdout, wstr, wcslen(wstr), &ret, NULL) == 0) { |
| 540 |
free(wstr); |
| 541 |
return EOF; |
| 542 |
} |
| 543 |
|
| 544 |
free(wstr); |
| 545 |
return ret; |
| 546 |
} |
| 547 |
|
| 548 |
int |
| 549 |
kpathsea_win32_fputs(kpathsea kpse, const char *str, FILE *fp) |
| 550 |
{ |
| 551 |
const int fd = fileno(fp); |
| 552 |
HANDLE hStdout; |
| 553 |
|
| 554 |
if (!((fd == fileno(stdout) || fd == fileno(stderr)) && _isatty(fd) |
| 555 |
&& kpse->File_system_codepage == CP_UTF8)) |
| 556 |
return fputs(str, fp); |
| 557 |
|
| 558 |
hStdout = (fd == fileno(stdout)) ? |
| 559 |
GetStdHandle(STD_OUTPUT_HANDLE) : GetStdHandle(STD_ERROR_HANDLE); |
| 560 |
|
| 561 |
return __win32_fputs(str, hStdout); |
| 562 |
} |
| 563 |
|
| 564 |
#define MAX_PROMPT_STR_SIZE 8192 |
| 565 |
|
| 566 |
int |
| 567 |
kpathsea_win32_vfprintf(kpathsea kpse, FILE *fp, const char *format, va_list argp) |
| 568 |
{ |
| 569 |
const int fd = fileno(fp); |
| 570 |
HANDLE hStdout; |
| 571 |
char buff[MAX_PROMPT_STR_SIZE]; |
| 572 |
int ret; |
| 573 |
|
| 574 |
if (!((fd == fileno(stdout) || fd == fileno(stderr)) && _isatty(fd) |
| 575 |
&& kpse->File_system_codepage == CP_UTF8)) |
| 576 |
return vfprintf(fp, format, argp); |
| 577 |
|
| 578 |
hStdout = (fd == fileno(stdout)) ? |
| 579 |
GetStdHandle(STD_OUTPUT_HANDLE) : GetStdHandle(STD_ERROR_HANDLE); |
| 580 |
|
| 581 |
ret = _vsnprintf(buff, sizeof(buff), format, argp); |
| 582 |
if (__win32_fputs(buff, hStdout)==EOF) { |
| 583 |
return EOF; |
| 584 |
} |
| 585 |
return ret; |
| 586 |
} |
| 587 |
|
| 588 |
int |
| 589 |
kpathsea_win32_fprintf(kpathsea kpse, FILE *fp, const char *format, ...) |
| 590 |
{ |
| 591 |
int ret, count; |
| 592 |
va_list argp; |
| 593 |
|
| 594 |
count = 0; |
| 595 |
va_start(argp, format); |
| 596 |
ret = kpathsea_win32_vfprintf(kpse, fp, format, argp); |
| 597 |
if (ret==EOF) { |
| 598 |
return EOF; |
| 599 |
} |
| 600 |
count += ret; |
| 601 |
va_end(argp); |
| 602 |
return count; |
| 603 |
} |
| 604 |
|
| 605 |
int |
| 606 |
kpathsea_win32_puts(kpathsea kpse, const char *str) |
| 607 |
{ |
| 608 |
if (kpathsea_win32_fputs(kpse, str, stdout)==EOF) { |
| 609 |
return EOF; |
| 610 |
} |
| 611 |
return puts(""); |
| 612 |
} |
| 613 |
|
| 614 |
int |
| 615 |
kpathsea_win32_putc(kpathsea kpse, int c, FILE *fp) |
| 616 |
{ |
| 617 |
const int fd = fileno(fp); |
| 618 |
HANDLE hStdout; |
| 619 |
DWORD ret; |
| 620 |
wchar_t wstr[3]; |
| 621 |
|
| 622 |
if (!((fd == fileno(stdout) || fd == fileno(stderr)) && _isatty(fd) |
| 623 |
&& kpse->File_system_codepage == CP_UTF8)) |
| 624 |
return putc(c, fp); |
| 625 |
|
| 626 |
hStdout = (fd == fileno(stdout)) ? |
| 627 |
GetStdHandle(STD_OUTPUT_HANDLE) : GetStdHandle(STD_ERROR_HANDLE); |
| 628 |
|
| 629 |
c &= 0xff; |
| 630 |
|
| 631 |
if (c < 0x80) { |
| 632 |
kpse->st_str = kpse->st_buff; |
| 633 |
kpse->st_len = 1; |
| 634 |
} |
| 635 |
if (c < 0xc0) { /* ASCII or trailer */ |
| 636 |
*(kpse->st_str)++ = c; |
| 637 |
kpse->st_len--; |
| 638 |
if (kpse->st_len == 0) { |
| 639 |
*(kpse->st_str) = '\0'; |
| 640 |
get_wstring_from_utf8(kpse->st_buff, wstr); |
| 641 |
if (WriteConsoleW(hStdout, wstr, wcslen(wstr), &ret, NULL) == 0) { |
| 642 |
kpse->st_len = 0; |
| 643 |
return EOF; |
| 644 |
} |
| 645 |
} |
| 646 |
else if (kpse->st_len < 0) return EOF; |
| 647 |
return c; |
| 648 |
} |
| 649 |
else if (c < 0xc2) { kpse->st_len = 0; return EOF; } /* illegal */ |
| 650 |
else if (c < 0xe0) kpse->st_len = 2; |
| 651 |
else if (c < 0xf0) kpse->st_len = 3; |
| 652 |
else if (c < 0xf5) kpse->st_len = 4; |
| 653 |
else { kpse->st_len = 0; return EOF; } |
| 654 |
|
| 655 |
kpse->st_str = kpse->st_buff; |
| 656 |
*(kpse->st_str)++ = c; |
| 657 |
kpse->st_len--; |
| 658 |
return c; |
| 659 |
} |
| 660 |
|
| 661 |
void |
| 662 |
kpathsea_win32_perror(kpathsea kpse, const char *str) |
| 663 |
{ |
| 664 |
wchar_t *wstr; |
| 665 |
|
| 666 |
if (kpse->File_system_codepage != CP_UTF8) { |
| 667 |
perror(str); |
| 668 |
return; |
| 669 |
} |
| 670 |
|
| 671 |
wstr = get_wstring_from_utf8(str, wstr=NULL); |
| 672 |
_wperror(wstr); |
| 673 |
free(wstr); |
| 674 |
} |
| 675 |
|
| 676 |
int |
| 677 |
kpathsea_IS_KANJI(kpathsea kpse, const char *p) |
| 678 |
{ |
| 679 |
int ret; |
| 680 |
|
| 681 |
ret = kpse->Is_cp932_system && kpathsea_isknj(kpse, *(p)) && |
| 682 |
kpathsea_isknj2(kpse, *(p+1)); |
| 683 |
return ret; |
| 684 |
} |
| 685 |
|
| 686 |
char * |
| 687 |
kpathsea_get_fsyscp_from_wstring(kpathsea kpse, const wchar_t *w,char *mb) |
| 688 |
{ |
| 689 |
return get_mbstring_from_wstring(kpse->File_system_codepage, w, mb); |
| 690 |
} |
| 691 |
|
| 692 |
wchar_t * |
| 693 |
kpathsea_get_wstring_from_fsyscp(kpathsea kpse, const char *mb,wchar_t *w) |
| 694 |
{ |
| 695 |
return get_wstring_from_mbstring(kpse->File_system_codepage, mb, w); |
| 696 |
} |
| 697 |
|
| 698 |
#if defined (KPSE_COMPAT_API) |
| 699 |
|
| 700 |
int |
| 701 |
isknj(int c) |
| 702 |
{ |
| 703 |
return kpathsea_isknj(kpse_def, c); |
| 704 |
} |
| 705 |
|
| 706 |
int |
| 707 |
isknj2(int c) |
| 708 |
{ |
| 709 |
return kpathsea_isknj2(kpse_def, c); |
| 710 |
} |
| 711 |
|
| 712 |
FILE * |
| 713 |
fsyscp_xfopen (const char *filename, const char *mode) |
| 714 |
{ |
| 715 |
return kpathsea_fsyscp_xfopen (kpse_def, filename, mode); |
| 716 |
} |
| 717 |
|
| 718 |
FILE * |
| 719 |
fsyscp_fopen (const char *filename, const char *mode) |
| 720 |
{ |
| 721 |
return kpathsea_fsyscp_fopen (kpse_def, filename, mode); |
| 722 |
} |
| 723 |
|
| 724 |
FILE * |
| 725 |
fsyscp_popen (const char *command, const char *mode) |
| 726 |
{ |
| 727 |
return kpathsea_fsyscp_popen (kpse_def, command, mode); |
| 728 |
} |
| 729 |
|
| 730 |
int |
| 731 |
get_command_line_args_utf8 (const_string enc, int *p_ac, char ***p_av) |
| 732 |
{ |
| 733 |
return kpathsea_get_command_line_args_utf8 (kpse_def, enc, p_ac, p_av); |
| 734 |
} |
| 735 |
|
| 736 |
int |
| 737 |
fsyscp_spawnvp (int mode, const char *command, const char* const *argv) |
| 738 |
{ |
| 739 |
return kpathsea_fsyscp_spawnvp (kpse_def, mode, command, argv); |
| 740 |
} |
| 741 |
|
| 742 |
int |
| 743 |
fsyscp_system (const char *cmd) |
| 744 |
{ |
| 745 |
return kpathsea_fsyscp_system (kpse_def, cmd); |
| 746 |
} |
| 747 |
|
| 748 |
int |
| 749 |
win32_getc(FILE *fp) |
| 750 |
{ |
| 751 |
return kpathsea_win32_getc(kpse_def, fp); |
| 752 |
} |
| 753 |
|
| 754 |
int |
| 755 |
win32_ungetc(int c, FILE *fp) |
| 756 |
{ |
| 757 |
return kpathsea_win32_ungetc(kpse_def, c, fp); |
| 758 |
} |
| 759 |
|
| 760 |
int |
| 761 |
win32_fputs(const char *str, FILE *fp) |
| 762 |
{ |
| 763 |
return kpathsea_win32_fputs(kpse_def, str, fp); |
| 764 |
} |
| 765 |
|
| 766 |
int |
| 767 |
win32_vfprintf(FILE *fp, const char *format, va_list argp) |
| 768 |
{ |
| 769 |
return kpathsea_win32_vfprintf(kpse_def, fp, format, argp); |
| 770 |
} |
| 771 |
|
| 772 |
int |
| 773 |
win32_fprintf(FILE *fp, const char *format, ...) |
| 774 |
{ |
| 775 |
int ret, count; |
| 776 |
va_list argp; |
| 777 |
|
| 778 |
count = 0; |
| 779 |
va_start(argp, format); |
| 780 |
ret = kpathsea_win32_vfprintf(kpse_def, fp, format, argp); |
| 781 |
if (ret==EOF) { |
| 782 |
return EOF; |
| 783 |
} |
| 784 |
count += ret; |
| 785 |
va_end(argp); |
| 786 |
return count; |
| 787 |
} |
| 788 |
|
| 789 |
int |
| 790 |
win32_puts(const char *str) |
| 791 |
{ |
| 792 |
return kpathsea_win32_puts(kpse_def, str); |
| 793 |
} |
| 794 |
|
| 795 |
int |
| 796 |
win32_putc(int c, FILE *fp) |
| 797 |
{ |
| 798 |
return kpathsea_win32_putc(kpse_def, c, fp); |
| 799 |
} |
| 800 |
|
| 801 |
void |
| 802 |
win32_perror(const char *str) |
| 803 |
{ |
| 804 |
kpathsea_win32_perror(kpse_def, str); |
| 805 |
} |
| 806 |
|
| 807 |
int |
| 808 |
IS_KANJI(const char *p) |
| 809 |
{ |
| 810 |
return kpathsea_IS_KANJI(kpse_def, p); |
| 811 |
} |
| 812 |
|
| 813 |
char * |
| 814 |
get_fsyscp_from_wstring(const wchar_t *w,char *mb) |
| 815 |
{ |
| 816 |
return kpathsea_get_fsyscp_from_wstring(kpse_def, w, mb); |
| 817 |
} |
| 818 |
|
| 819 |
wchar_t * |
| 820 |
get_wstring_from_fsyscp(const char *mb,wchar_t *w) |
| 821 |
{ |
| 822 |
return kpathsea_get_wstring_from_fsyscp(kpse_def, mb, w); |
| 823 |
} |
| 824 |
#endif |