summaryrefslogtreecommitdiff
path: root/Build/source/texk/kpathsea/knj.c
diff options
context:
space:
mode:
authorTakuji Tanaka <KXD02663@nifty.ne.jp>2014-01-25 07:36:00 +0000
committerTakuji Tanaka <KXD02663@nifty.ne.jp>2014-01-25 07:36:00 +0000
commit8f37158e88f5a8ec913d12220b295dca907ed6b9 (patch)
treef7c4c387c2a1b866f693e1e1fdc22c3b2023f780 /Build/source/texk/kpathsea/knj.c
parentdce8da8c3072b543be8fb371e9060ad0a2d1c7c0 (diff)
kpathsea [win32]: Allow Unicode file name in kpsewhich, add new functions in knj.[ch]
git-svn-id: svn://tug.org/texlive/trunk@32783 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Build/source/texk/kpathsea/knj.c')
-rw-r--r--Build/source/texk/kpathsea/knj.c103
1 files changed, 100 insertions, 3 deletions
diff --git a/Build/source/texk/kpathsea/knj.c b/Build/source/texk/kpathsea/knj.c
index 52b4a43b9bc..cd9eb474129 100644
--- a/Build/source/texk/kpathsea/knj.c
+++ b/Build/source/texk/kpathsea/knj.c
@@ -84,7 +84,7 @@ get_mbstring_from_wstring(int cp, const wchar_t *wstr, char *mbstr)
len = WideCharToMultiByte(cp, 0, wstr, -1, mbstr, 0, NULL, NULL);
if (len==0) {
- FATAL("cannot convert string to wide string");
+ FATAL("cannot convert string to multibyte string");
}
if (mbstr==NULL) {
mbstr = xmalloc(len+1);
@@ -200,13 +200,13 @@ fsyscp_popen (const char *command, const char *mode)
return f;
}
-void
+int
get_command_line_args_utf8 (const_string enc, int *p_ac, char ***p_av)
{
int argc;
string *argv;
- if (!enc || !strncmp(enc,"",1)) return;
+ if (!enc || !strncmp(enc,"",1)) return 0;
#ifdef DEBUG
fprintf(stderr, "command_line_encoding (%s)\n", enc);
@@ -237,8 +237,10 @@ get_command_line_args_utf8 (const_string enc, int *p_ac, char ***p_av)
argv[argcw] = NULL;
*p_ac = argc;
*p_av = argv;
+ return file_system_codepage;
} else {
WARNING1("kpathsea: Ignoring unknown encoding `%s'", enc);
+ return 0;
}
}
@@ -331,3 +333,98 @@ fsyscp_system (const char *cmd)
free (av[2]);
return ret;
}
+
+static int getc_len;
+static int getc_buff[4];
+
+int win32_getc(FILE *fp)
+{
+ const int fd = fileno(fp);
+ HANDLE hStdin;
+ DWORD ret;
+ wchar_t wc[3];
+ char mbc[5];
+ int j;
+ static wchar_t wcbuf = L'\0';
+
+ if (!(fd == fileno(stdin) && _isatty(fd) && file_system_codepage == CP_UTF8))
+ return getc(fp);
+
+ if (getc_len == 0)
+ {
+ hStdin = GetStdHandle(STD_INPUT_HANDLE);
+ if (wcbuf) {
+ wc[0] = wcbuf;
+ wcbuf = L'\0';
+ }
+ else if (ReadConsoleW(hStdin, wc, 1, &ret, NULL) == 0)
+ return EOF;
+ if (0xd800<=wc[0] && wc[0]<0xdc00) {
+ if (ReadConsoleW(hStdin, wc+1, 1, &ret, NULL) == 0)
+ return EOF;
+ if (0xdc00<=wc[1] && wc[1]<0xe000) {
+ wc[2]=L'\0';
+ } else {
+ wcbuf=wc[1];
+ wc[0]=0xfffd; /* illegal surrogate pair */
+ wc[1]=L'\0';
+ }
+ } else if (0xdc00<=wc[0] && wc[0]<0xe000) {
+ wc[0]=0xfffd; /* illegal surrogate pair */
+ wc[1]=L'\0';
+ } else {
+ wc[1]=L'\0';
+ }
+ get_utf8_from_wstring(wc,mbc);
+ j=strlen(mbc)-1;
+ while(j>=0) {
+ getc_buff[getc_len++]=(int)mbc[j--];
+ }
+ }
+ return getc_buff[--getc_len];
+}
+
+int win32_ungetc(int c, FILE *fp)
+{
+ const int fd = fileno(fp);
+
+ if (!(fd == fileno(stdin) && _isatty(fd) && file_system_codepage == CP_UTF8))
+ return ungetc(c, fp);
+
+ assert(getc_len < 4);
+ return getc_buff[getc_len++] = c;
+}
+
+int win32_fputs(const char *str, FILE *fp)
+{
+ const int fd = fileno(fp);
+ HANDLE hStdout;
+ DWORD ret;
+ wchar_t *wstr;
+ int j;
+
+ if (!((fd == fileno(stdout) || fd == fileno(stderr)) && _isatty(fd)
+ && file_system_codepage == CP_UTF8))
+ return fputs(str, fp);
+
+ hStdout = (fd == fileno(stdout)) ?
+ GetStdHandle(STD_OUTPUT_HANDLE) : GetStdHandle(STD_ERROR_HANDLE);
+
+ wstr = get_wstring_from_utf8(str, wstr=NULL);
+
+ if (WriteConsoleW(hStdout, wstr, wcslen(wstr), &ret, NULL) == 0) {
+ free(wstr);
+ return EOF;
+ }
+
+ free(wstr);
+ return ret;
+}
+
+int win32_puts(const char *str)
+{
+ if (win32_fputs(str, stdout)==EOF) {
+ return EOF;
+ }
+ return puts("");
+}