Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[6565f5]: perl / pl-CYG08-b722195e_CYG17_utf8_paths.patch Maximize Restore History

Download this file

pl-CYG08-b722195e_CYG17_utf8_paths.patch    376 lines (352 with data), 11.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
From: Reini Urban <rurban@x-ray.at>
Subject: [PATCH] CYG17 utf8 paths
Author: Reini Urban <rurban@x-ray.at>
Date: Tue Apr 12 12:19:36 2011 +0200
commit b722195e24a6c67310eb30e8f18601966a96ba78
CYG17 utf8 paths
Use cygwin_conv_path since cygwin-1.7 and support native utf-8 paths.
diff --git a/cygwin/cygwin.c b/cygwin/cygwin.c
index aa6938d..3206f91 100644
--- a/cygwin/cygwin.c
+++ b/cygwin/cygwin.c
@@ -10,9 +10,13 @@
#include <unistd.h>
#include <process.h>
#include <sys/cygwin.h>
+#include <cygwin/version.h>
#include <mntent.h>
#include <alloca.h>
#include <dlfcn.h>
+#if (CYGWIN_VERSION_API_MINOR >= 181)
+#include <wchar.h>
+#endif
/*
* pp_system() implemented via spawn()
@@ -125,7 +129,7 @@ do_spawn (char *cmd)
return do_spawnvp("sh",command);
}
- Newx (PL_Argv,(s-cmd)/2+2,const char*);
+ Newx (PL_Argv,(s-cmd)/2+2,char*);
PL_Cmd=savepvn (cmd,s-cmd);
a=PL_Argv;
for (s=PL_Cmd; *s;) {
@@ -143,6 +147,44 @@ do_spawn (char *cmd)
return do_spawnvp(PL_Argv[0],(const char * const *)PL_Argv);
}
+#if (CYGWIN_VERSION_API_MINOR >= 181)
+char*
+wide_to_utf8(const wchar_t *wbuf)
+{
+ char *buf;
+ int wlen = 0;
+ char *oldlocale = setlocale(LC_CTYPE, NULL);
+ setlocale(LC_CTYPE, "utf-8");
+
+ /* uvuni_to_utf8(buf, chr) or Encoding::_bytes_to_utf8(sv, "UCS-2BE"); */
+ wlen = wcsrtombs(NULL, (const wchar_t **)&wbuf, wlen, NULL);
+ buf = (char *) safemalloc(wlen+1);
+ wcsrtombs(buf, (const wchar_t **)&wbuf, wlen, NULL);
+
+ if (oldlocale) setlocale(LC_CTYPE, oldlocale);
+ else setlocale(LC_CTYPE, "C");
+ return buf;
+}
+
+wchar_t*
+utf8_to_wide(const char *buf)
+{
+ wchar_t *wbuf;
+ mbstate_t mbs;
+ char *oldlocale = setlocale(LC_CTYPE, NULL);
+ int wlen = sizeof(wchar_t)*strlen(buf);
+
+ setlocale(LC_CTYPE, "utf-8");
+ wbuf = (wchar_t *) safemalloc(wlen);
+ /* utf8_to_uvuni(pathname, wpath) or Encoding::_utf8_to_bytes(sv, "UCS-2BE"); */
+ wlen = mbsrtowcs(wbuf, (const char**)&buf, wlen, &mbs);
+
+ if (oldlocale) setlocale(LC_CTYPE, oldlocale);
+ else setlocale(LC_CTYPE, "C");
+ return wbuf;
+}
+#endif /* cygwin 1.7 */
+
/* see also Cwd.pm */
XS(Cygwin_cwd)
{
@@ -194,7 +236,12 @@ XS(XS_Cygwin_winpid_to_pid)
pid = (pid_t)SvIV(ST(0));
- if ((RETVAL = cygwin32_winpid_to_pid(pid)) > 0) {
+#if (CYGWIN_VERSION_API_MINOR >= 181)
+ RETVAL = cygwin_winpid_to_pid(pid);
+#else
+ RETVAL = cygwin32_winpid_to_pid(pid);
+#endif
+ if (RETVAL > 0) {
XSprePUSH; PUSHi((IV)RETVAL);
XSRETURN(1);
}
@@ -202,34 +249,91 @@ XS(XS_Cygwin_winpid_to_pid)
}
XS(XS_Cygwin_win_to_posix_path)
+
{
dXSARGS;
int absolute_flag = 0;
STRLEN len;
int err;
- char *pathname, *buf;
+ char *src_path;
+ char *posix_path;
+ int isutf8 = 0;
if (items < 1 || items > 2)
Perl_croak(aTHX_ "Usage: Cygwin::win_to_posix_path(pathname, [absolute])");
- pathname = SvPV(ST(0), len);
+ src_path = SvPV(ST(0), len);
if (items == 2)
absolute_flag = SvTRUE(ST(1));
if (!len)
Perl_croak(aTHX_ "can't convert empty path");
- buf = (char *) safemalloc (len + 260 + 1001);
+ isutf8 = SvUTF8(ST(0));
+#if (CYGWIN_VERSION_API_MINOR >= 181)
+ /* Check utf8 flag and use wide api then.
+ Size calculation: On overflow let cygwin_conv_path calculate the final size.
+ */
+ if (isutf8) {
+ int what = absolute_flag ? CCP_WIN_W_TO_POSIX : CCP_WIN_W_TO_POSIX | CCP_RELATIVE;
+ int wlen = sizeof(wchar_t)*(len + 260 + 1001);
+ wchar_t *wpath = (wchar_t *) safemalloc(sizeof(wchar_t)*len);
+ wchar_t *wbuf = (wchar_t *) safemalloc(wlen);
+ if (!IN_BYTES) {
+ mbstate_t mbs;
+ char *oldlocale = setlocale(LC_CTYPE, NULL);
+ setlocale(LC_CTYPE, "utf-8");
+ /* utf8_to_uvuni(src_path, wpath) or Encoding::_utf8_to_bytes(sv, "UCS-2BE"); */
+ wlen = mbsrtowcs(wpath, (const char**)&src_path, wlen, &mbs);
+ if (wlen > 0)
+ err = cygwin_conv_path(what, wpath, wbuf, wlen);
+ if (oldlocale) setlocale(LC_CTYPE, oldlocale);
+ else setlocale(LC_CTYPE, "C");
+ } else { /* use bytes; assume already ucs-2 encoded bytestream */
+ err = cygwin_conv_path(what, src_path, wbuf, wlen);
+ }
+ if (err == ENOSPC) { /* our space assumption was wrong, not enough space */
+ int newlen = cygwin_conv_path(what, wpath, wbuf, 0);
+ wbuf = (wchar_t *) realloc(&wbuf, newlen);
+ err = cygwin_conv_path(what, wpath, wbuf, newlen);
+ wlen = newlen;
+ }
+ /* utf16_to_utf8(*p, *d, bytlen, *newlen) */
+ posix_path = (char *) safemalloc(wlen*3);
+ Perl_utf16_to_utf8(aTHX_ (U8*)&wpath, (U8*)posix_path, (I32)wlen*2, (I32*)&len);
+ /*
+ wlen = wcsrtombs(NULL, (const wchar_t **)&wbuf, wlen, NULL);
+ posix_path = (char *) safemalloc(wlen+1);
+ wcsrtombs(posix_path, (const wchar_t **)&wbuf, wlen, NULL);
+ */
+ } else {
+ int what = absolute_flag ? CCP_WIN_A_TO_POSIX : CCP_WIN_A_TO_POSIX | CCP_RELATIVE;
+ posix_path = (char *) safemalloc (len + 260 + 1001);
+ err = cygwin_conv_path(what, src_path, posix_path, len + 260 + 1001);
+ if (err == ENOSPC) { /* our space assumption was wrong, not enough space */
+ int newlen = cygwin_conv_path(what, src_path, posix_path, 0);
+ posix_path = (char *) realloc(&posix_path, newlen);
+ err = cygwin_conv_path(what, src_path, posix_path, newlen);
+ }
+ }
+#else
+ posix_path = (char *) safemalloc (len + 260 + 1001);
if (absolute_flag)
- err = cygwin_conv_to_full_posix_path(pathname, buf);
+ err = cygwin_conv_to_full_posix_path(src_path, posix_path);
else
- err = cygwin_conv_to_posix_path(pathname, buf);
+ err = cygwin_conv_to_posix_path(src_path, posix_path);
+#endif
if (!err) {
- ST(0) = sv_2mortal(newSVpv(buf, 0));
- safefree(buf);
- XSRETURN(1);
+ EXTEND(SP, 1);
+ ST(0) = sv_2mortal(newSVpv(posix_path, 0));
+ if (isutf8) { /* src was utf-8, so result should also */
+ /* TODO: convert ANSI (local windows encoding) to utf-8 on cygwin-1.5 */
+ SvUTF8_on(ST(0));
+ }
+ safefree(posix_path);
+ XSRETURN(1);
} else {
- safefree(buf);
+ safefree(posix_path);
XSRETURN_UNDEF;
}
}
@@ -240,29 +344,80 @@ XS(XS_Cygwin_posix_to_win_path)
int absolute_flag = 0;
STRLEN len;
int err;
- char *pathname, *buf;
+ char *src_path, *win_path;
+ int isutf8 = 0;
if (items < 1 || items > 2)
Perl_croak(aTHX_ "Usage: Cygwin::posix_to_win_path(pathname, [absolute])");
- pathname = SvPV(ST(0), len);
+ src_path = SvPVx(ST(0), len);
if (items == 2)
absolute_flag = SvTRUE(ST(1));
if (!len)
Perl_croak(aTHX_ "can't convert empty path");
- buf = (char *) safemalloc(len + 260 + 1001);
-
+ isutf8 = SvUTF8(ST(0));
+#if (CYGWIN_VERSION_API_MINOR >= 181)
+ /* Check utf8 flag and use wide api then.
+ Size calculation: On overflow let cygwin_conv_path calculate the final size.
+ */
+ if (isutf8) {
+ int what = absolute_flag ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_W | CCP_RELATIVE;
+ int wlen = sizeof(wchar_t)*(len + 260 + 1001);
+ wchar_t *wpath = (wchar_t *) safemalloc(sizeof(wchar_t)*len);
+ wchar_t *wbuf = (wchar_t *) safemalloc(wlen);
+ char *oldlocale = setlocale(LC_CTYPE, NULL);
+ setlocale(LC_CTYPE, "utf-8");
+ if (!IN_BYTES) {
+ mbstate_t mbs;
+ /* utf8_to_uvuni(src_path, wpath) or Encoding::_utf8_to_bytes(sv, "UCS-2BE"); */
+ wlen = mbsrtowcs(wpath, (const char**)&src_path, wlen, &mbs);
+ if (wlen > 0)
+ err = cygwin_conv_path(what, wpath, wbuf, wlen);
+ } else { /* use bytes; assume already ucs-2 encoded bytestream */
+ err = cygwin_conv_path(what, src_path, wbuf, wlen);
+ }
+ if (err == ENOSPC) { /* our space assumption was wrong, not enough space */
+ int newlen = cygwin_conv_path(what, wpath, wbuf, 0);
+ wbuf = (wchar_t *) realloc(&wbuf, newlen);
+ err = cygwin_conv_path(what, wpath, wbuf, newlen);
+ wlen = newlen;
+ }
+ /* also see utf8.c: Perl_utf16_to_utf8() or Encoding::_bytes_to_utf8(sv, "UCS-2BE"); */
+ wlen = wcsrtombs(NULL, (const wchar_t **)&wbuf, wlen, NULL);
+ win_path = (char *) safemalloc(wlen+1);
+ wcsrtombs(win_path, (const wchar_t **)&wbuf, wlen, NULL);
+ if (oldlocale) setlocale(LC_CTYPE, oldlocale);
+ else setlocale(LC_CTYPE, "C");
+ } else {
+ int what = absolute_flag ? CCP_POSIX_TO_WIN_A : CCP_POSIX_TO_WIN_A | CCP_RELATIVE;
+ win_path = (char *) safemalloc(len + 260 + 1001);
+ err = cygwin_conv_path(what, src_path, win_path, len + 260 + 1001);
+ if (err == ENOSPC) { /* our space assumption was wrong, not enough space */
+ int newlen = cygwin_conv_path(what, src_path, win_path, 0);
+ win_path = (char *) realloc(&win_path, newlen);
+ err = cygwin_conv_path(what, src_path, win_path, newlen);
+ }
+ }
+#else
+ if (isutf8)
+ Perl_warn(aTHX_ "can't convert utf8 path");
+ win_path = (char *) safemalloc(len + 260 + 1001);
if (absolute_flag)
- err = cygwin_conv_to_full_win32_path(pathname, buf);
+ err = cygwin_conv_to_full_win32_path(src_path, win_path);
else
- err = cygwin_conv_to_win32_path(pathname, buf);
+ err = cygwin_conv_to_win32_path(src_path, win_path);
+#endif
if (!err) {
- ST(0) = sv_2mortal(newSVpv(buf, 0));
- safefree(buf);
- XSRETURN(1);
+ EXTEND(SP, 1);
+ ST(0) = sv_2mortal(newSVpv(win_path, 0));
+ if (isutf8) {
+ SvUTF8_on(ST(0));
+ }
+ safefree(win_path);
+ XSRETURN(1);
} else {
- safefree(buf);
+ safefree(win_path);
XSRETURN_UNDEF;
}
}
@@ -293,24 +448,22 @@ XS(XS_Cygwin_mount_flags)
{
dXSARGS;
char *pathname;
- char flags[260];
+ char flags[PATH_MAX];
+ flags[0] = '\0';
if (items != 1)
- Perl_croak(aTHX_ "Usage: Cygwin::mount_flags(mnt_dir|'/cygwin')");
+ Perl_croak(aTHX_ "Usage: Cygwin::mount_flags( mnt_dir | '/cygdrive' )");
pathname = SvPV_nolen(ST(0));
- /* TODO: Check for cygdrive registry setting,
- * and then use CW_GET_CYGDRIVE_INFO
- */
if (!strcmp(pathname, "/cygdrive")) {
- char user[260];
- char system[260];
- char user_flags[260];
- char system_flags[260];
+ char user[PATH_MAX];
+ char system[PATH_MAX];
+ char user_flags[PATH_MAX];
+ char system_flags[PATH_MAX];
- cygwin_internal (CW_GET_CYGDRIVE_INFO, user, system, user_flags,
- system_flags);
+ cygwin_internal (CW_GET_CYGDRIVE_INFO, user, system,
+ user_flags, system_flags);
if (strlen(user) > 0) {
sprintf(flags, "%s,cygdrive,%s", user_flags, user);
@@ -323,6 +476,7 @@ XS(XS_Cygwin_mount_flags)
} else {
struct mntent *mnt;
+ int found = 0;
setmntent (0, 0);
while ((mnt = getmntent (0))) {
if (!strcmp(pathname, mnt->mnt_dir)) {
@@ -331,12 +485,42 @@ XS(XS_Cygwin_mount_flags)
strcat(flags, ",");
strcat(flags, mnt->mnt_opts);
}
+ found++;
break;
}
}
endmntent (0);
- ST(0) = sv_2mortal(newSVpv(flags, 0));
- XSRETURN(1);
+
+ /* Check if arg is the current volume moint point if not default,
+ * and then use CW_GET_CYGDRIVE_INFO also.
+ */
+ if (!found) {
+ char user[PATH_MAX];
+ char system[PATH_MAX];
+ char user_flags[PATH_MAX];
+ char system_flags[PATH_MAX];
+
+ cygwin_internal (CW_GET_CYGDRIVE_INFO, user, system,
+ user_flags, system_flags);
+
+ if (strlen(user) > 0) {
+ if (strcmp(user,pathname)) {
+ sprintf(flags, "%s,cygdrive,%s", user_flags, user);
+ found++;
+ }
+ } else {
+ if (strcmp(user,pathname)) {
+ sprintf(flags, "%s,cygdrive,%s", system_flags, system);
+ found++;
+ }
+ }
+ }
+ if (found) {
+ ST(0) = sv_2mortal(newSVpv(flags, 0));
+ XSRETURN(1);
+ } else {
+ XSRETURN_UNDEF;
+ }
}
}