Debian Codegraph @

pTk/mTk/tclWin/tclWin32Dll.c

raw
1 /*
2 * tclWin32Dll.c --
3 *
4 * This file contains the DLL entry point.
5 *
6 * Copyright (c) 1995-1996 Sun Microsystems, Inc.
7 * Copyright (c) 1998-2000 Scriptics Corporation.
8 *
9 * See the file "license.terms" for information on usage and redistribution
10 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 *
12 * RCS: @(#) $Id: tclWin32Dll.c,v 1.24.2.1 2003/04/14 15:45:59 vincentdarley Exp $
13 */
14
15 #include "tkWinInt.h"
16
17 typedef DWORD (WINAPI * UT32PROC)(LPVOID lpBuff, DWORD dwUserDefined,
18 LPVOID *lpTranslationList);
19
20 typedef BOOL (WINAPI * PUTREGISTER)(HANDLE hModule, LPCSTR SixteenBitDLL,
21 LPCSTR InitName, LPCSTR ProcName, UT32PROC* ThirtyTwoBitThunk,
22 FARPROC UT32Callback, LPVOID Buff);
23
24 typedef VOID (WINAPI * PUTUNREGISTER)(HANDLE hModule);
25
26 static PUTUNREGISTER UTUnRegister = NULL;
27 static int tclProcessesAttached = 0;
28
29 /*
30 * The following data structures are used when loading the thunking
31 * library for execing child processes under Win32s.
32 */
33
34 typedef DWORD (WINAPI UT32PROC)(LPVOID lpBuff, DWORD dwUserDefined,
35 LPVOID *lpTranslationList);
36
37 typedef BOOL (WINAPI UTREGISTER)(HANDLE hModule, LPCSTR SixteenBitDLL,
38 LPCSTR InitName, LPCSTR ProcName, UT32PROC **ThirtyTwoBitThunk,
39 FARPROC UT32Callback, LPVOID Buff);
40
41 typedef VOID (WINAPI UTUNREGISTER)(HANDLE hModule);
42
43 /*
44 * The following variables keep track of information about this DLL
45 * on a per-instance basis. Each time this DLL is loaded, it gets its own
46 * new data segment with its own copy of all static and global information.
47 */
48
49 static HINSTANCE hInstance; /* HINSTANCE of this DLL. */
50 static int platformId; /* Running under NT, or 95/98? */
51
52 #if defined(HAVE_NO_SEH) && defined(TCL_MEM_DEBUG)
53 static void *INITIAL_ESP,
54 *INITIAL_EBP,
55 *INITIAL_HANDLER,
56 *RESTORED_ESP,
57 *RESTORED_EBP,
58 *RESTORED_HANDLER;
59 #endif /* HAVE_NO_SEH && TCL_MEM_DEBUG */
60
61 /*
62 * The following function tables are used to dispatch to either the
63 * wide-character or multi-byte versions of the operating system calls,
64 * depending on whether the Unicode calls are available.
65 */
66
67 static TclWinProcs asciiProcs = {
68 0,
69
70 (BOOL (WINAPI *)(CONST TCHAR *, LPDCB)) BuildCommDCBA,
71 (TCHAR *(WINAPI *)(TCHAR *)) CharLowerA,
72 (BOOL (WINAPI *)(CONST TCHAR *, CONST TCHAR *, BOOL)) CopyFileA,
73 (BOOL (WINAPI *)(CONST TCHAR *, LPSECURITY_ATTRIBUTES)) CreateDirectoryA,
74 (HANDLE (WINAPI *)(CONST TCHAR *, DWORD, DWORD, SECURITY_ATTRIBUTES *,
75 DWORD, DWORD, HANDLE)) CreateFileA,
76 (BOOL (WINAPI *)(CONST TCHAR *, TCHAR *, LPSECURITY_ATTRIBUTES,
77 LPSECURITY_ATTRIBUTES, BOOL, DWORD, LPVOID, CONST TCHAR *,
78 LPSTARTUPINFOA, LPPROCESS_INFORMATION)) CreateProcessA,
79 (BOOL (WINAPI *)(CONST TCHAR *)) DeleteFileA,
80 (HANDLE (WINAPI *)(CONST TCHAR *, WIN32_FIND_DATAT *)) FindFirstFileA,
81 (BOOL (WINAPI *)(HANDLE, WIN32_FIND_DATAT *)) FindNextFileA,
82 (BOOL (WINAPI *)(WCHAR *, LPDWORD)) GetComputerNameA,
83 (DWORD (WINAPI *)(DWORD, WCHAR *)) GetCurrentDirectoryA,
84 (DWORD (WINAPI *)(CONST TCHAR *)) GetFileAttributesA,
85 (DWORD (WINAPI *)(CONST TCHAR *, DWORD nBufferLength, WCHAR *,
86 TCHAR **)) GetFullPathNameA,
87 (DWORD (WINAPI *)(HMODULE, WCHAR *, int)) GetModuleFileNameA,
88 (DWORD (WINAPI *)(CONST TCHAR *, WCHAR *, DWORD)) GetShortPathNameA,
89 (UINT (WINAPI *)(CONST TCHAR *, CONST TCHAR *, UINT uUnique,
90 WCHAR *)) GetTempFileNameA,
91 (DWORD (WINAPI *)(DWORD, WCHAR *)) GetTempPathA,
92 (BOOL (WINAPI *)(CONST TCHAR *, WCHAR *, DWORD, LPDWORD, LPDWORD, LPDWORD,
93 WCHAR *, DWORD)) GetVolumeInformationA,
94 (HINSTANCE (WINAPI *)(CONST TCHAR *)) LoadLibraryA,
95 (TCHAR (WINAPI *)(WCHAR *, CONST TCHAR *)) lstrcpyA,
96 (BOOL (WINAPI *)(CONST TCHAR *, CONST TCHAR *)) MoveFileA,
97 (BOOL (WINAPI *)(CONST TCHAR *)) RemoveDirectoryA,
98 (DWORD (WINAPI *)(CONST TCHAR *, CONST TCHAR *, CONST TCHAR *, DWORD,
99 WCHAR *, TCHAR **)) SearchPathA,
100 (BOOL (WINAPI *)(CONST TCHAR *)) SetCurrentDirectoryA,
101 (BOOL (WINAPI *)(CONST TCHAR *, DWORD)) SetFileAttributesA,
102 /*
103 * The three NULL function pointers will only be set when
104 * Tcl_FindExecutable is called. If you don't ever call that
105 * function, the application will crash whenever WinTcl tries to call
106 * functions through these null pointers. That is not a bug in Tcl
107 * -- Tcl_FindExecutable is obligatory in recent Tcl releases.
108 */
109 NULL,
110 NULL,
111 (int (__cdecl*)(CONST TCHAR *, struct _utimbuf *)) _utime,
112 NULL,
113 NULL,
114 };
115
116 static TclWinProcs unicodeProcs = {
117 1,
118
119 (BOOL (WINAPI *)(CONST TCHAR *, LPDCB)) BuildCommDCBW,
120 (TCHAR *(WINAPI *)(TCHAR *)) CharLowerW,
121 (BOOL (WINAPI *)(CONST TCHAR *, CONST TCHAR *, BOOL)) CopyFileW,
122 (BOOL (WINAPI *)(CONST TCHAR *, LPSECURITY_ATTRIBUTES)) CreateDirectoryW,
123 (HANDLE (WINAPI *)(CONST TCHAR *, DWORD, DWORD, SECURITY_ATTRIBUTES *,
124 DWORD, DWORD, HANDLE)) CreateFileW,
125 (BOOL (WINAPI *)(CONST TCHAR *, TCHAR *, LPSECURITY_ATTRIBUTES,
126 LPSECURITY_ATTRIBUTES, BOOL, DWORD, LPVOID, CONST TCHAR *,
127 LPSTARTUPINFOA, LPPROCESS_INFORMATION)) CreateProcessW,
128 (BOOL (WINAPI *)(CONST TCHAR *)) DeleteFileW,
129 (HANDLE (WINAPI *)(CONST TCHAR *, WIN32_FIND_DATAT *)) FindFirstFileW,
130 (BOOL (WINAPI *)(HANDLE, WIN32_FIND_DATAT *)) FindNextFileW,
131 (BOOL (WINAPI *)(WCHAR *, LPDWORD)) GetComputerNameW,
132 (DWORD (WINAPI *)(DWORD, WCHAR *)) GetCurrentDirectoryW,
133 (DWORD (WINAPI *)(CONST TCHAR *)) GetFileAttributesW,
134 (DWORD (WINAPI *)(CONST TCHAR *, DWORD nBufferLength, WCHAR *,
135 TCHAR **)) GetFullPathNameW,
136 (DWORD (WINAPI *)(HMODULE, WCHAR *, int)) GetModuleFileNameW,
137 (DWORD (WINAPI *)(CONST TCHAR *, WCHAR *, DWORD)) GetShortPathNameW,
138 (UINT (WINAPI *)(CONST TCHAR *, CONST TCHAR *, UINT uUnique,
139 WCHAR *)) GetTempFileNameW,
140 (DWORD (WINAPI *)(DWORD, WCHAR *)) GetTempPathW,
141 (BOOL (WINAPI *)(CONST TCHAR *, WCHAR *, DWORD, LPDWORD, LPDWORD, LPDWORD,
142 WCHAR *, DWORD)) GetVolumeInformationW,
143 (HINSTANCE (WINAPI *)(CONST TCHAR *)) LoadLibraryW,
144 (TCHAR (WINAPI *)(WCHAR *, CONST TCHAR *)) lstrcpyW,
145 (BOOL (WINAPI *)(CONST TCHAR *, CONST TCHAR *)) MoveFileW,
146 (BOOL (WINAPI *)(CONST TCHAR *)) RemoveDirectoryW,
147 (DWORD (WINAPI *)(CONST TCHAR *, CONST TCHAR *, CONST TCHAR *, DWORD,
148 WCHAR *, TCHAR **)) SearchPathW,
149 (BOOL (WINAPI *)(CONST TCHAR *)) SetCurrentDirectoryW,
150 (BOOL (WINAPI *)(CONST TCHAR *, DWORD)) SetFileAttributesW,
151 /*
152 * The three NULL function pointers will only be set when
153 * Tcl_FindExecutable is called. If you don't ever call that
154 * function, the application will crash whenever WinTcl tries to call
155 * functions through these null pointers. That is not a bug in Tcl
156 * -- Tcl_FindExecutable is obligatory in recent Tcl releases.
157 */
158 NULL,
159 NULL,
160 (int (__cdecl*)(CONST TCHAR *, struct _utimbuf *)) _wutime,
161 NULL,
162 NULL,
163 };
164
165 TclWinProcs *tclWinProcs;
166 static Tcl_Encoding tclWinTCharEncoding;
167
168 /*
169 * The following declaration is for the VC++ DLL entry point.
170 */
171
172 BOOL APIENTRY DllMain(HINSTANCE hInst, DWORD reason,
173 LPVOID reserved);
174
175 /*
176 * The following structure and linked list is to allow us to map between
177 * volume mount points and drive letters on the fly (no Win API exists
178 * for this).
179 */
180 typedef struct MountPointMap {
181 CONST WCHAR* volumeName; /* Native wide string volume name */
182 char driveLetter; /* Drive letter corresponding to
183 * the volume name. */
184 struct MountPointMap* nextPtr; /* Pointer to next structure in list,
185 * or NULL */
186 } MountPointMap;
187
188 /*
189 * This is the head of the linked list, which is protected by the
190 * mutex which follows, for thread-enabled builds.
191 */
192 MountPointMap *driveLetterLookup = NULL;
193 TCL_DECLARE_MUTEX(mountPointMap)
194
195 /* We will need this below */
196 extern Tcl_FSDupInternalRepProc NativeDupInternalRep;
197
198 #ifdef __WIN32__
199 #ifndef STATIC_BUILD
200
201
202 /*
203 *----------------------------------------------------------------------
204 *
205 * DllEntryPoint --
206 *
207 * This wrapper function is used by Borland to invoke the
208 * initialization code for Tcl. It simply calls the DllMain
209 * routine.
210 *
211 * Results:
212 * See DllMain.
213 *
214 * Side effects:
215 * See DllMain.
216 *
217 *----------------------------------------------------------------------
218 */
219
220 BOOL APIENTRY
221 DllEntryPoint(hInst, reason, reserved)
222 HINSTANCE hInst; /* Library instance handle. */
223 DWORD reason; /* Reason this function is being called. */
224 LPVOID reserved; /* Not used. */
225 {
226 return DllMain(hInst, reason, reserved);
227 }
228
229 /*
230 *----------------------------------------------------------------------
231 *
232 * DllMain --
233 *
234 * This routine is called by the VC++ C run time library init
235 * code, or the DllEntryPoint routine. It is responsible for
236 * initializing various dynamically loaded libraries.
237 *
238 * Results:
239 * TRUE on sucess, FALSE on failure.
240 *
241 * Side effects:
242 * Establishes 32-to-16 bit thunk and initializes sockets library.
243 *
244 *----------------------------------------------------------------------
245 */
246 BOOL APIENTRY
247 DllMain(hInst, reason, reserved)
248 HINSTANCE hInst; /* Library instance handle. */
249 DWORD reason; /* Reason this function is being called. */
250 LPVOID reserved; /* Not used. */
251 {
252 switch (reason) {
253 case DLL_PROCESS_ATTACH:
254 TclWinInit(hInst);
255 return TRUE;
256
257 case DLL_PROCESS_DETACH:
258 if (hInst == hInstance) {
259 Tcl_Finalize();
260 }
261 break;
262 }
263
264 return TRUE;
265 }
266
267 #endif /* !STATIC_BUILD */
268 #endif /* __WIN32__ */
269
270 /*
271 *----------------------------------------------------------------------
272 *
273 * TclWinGetTclInstance --
274 *
275 * Retrieves the global library instance handle.
276 *
277 * Results:
278 * Returns the global library instance handle.
279 *
280 * Side effects:
281 * None.
282 *
283 *----------------------------------------------------------------------
284 */
285
286 HINSTANCE
287 TclWinGetTclInstance()
288 {
289 return hInstance;
290 }
291
292 /*
293 *----------------------------------------------------------------------
294 *
295 * TclWinInit --
296 *
297 * This function initializes the internal state of the tcl library.
298 *
299 * Results:
300 * None.
301 *
302 * Side effects:
303 * Initializes the tclPlatformId variable.
304 *
305 *----------------------------------------------------------------------
306 */
307
308 void
309 TclWinInit(hInst)
310 HINSTANCE hInst; /* Library instance handle. */
311 {
312 OSVERSIONINFO os;
313
314 hInstance = hInst;
315 os.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
316 GetVersionEx(&os);
317 platformId = os.dwPlatformId;
318
319 /*
320 * We no longer support Win32s, so just in case someone manages to
321 * get a runtime there, make sure they know that.
322 */
323
324 if (platformId == VER_PLATFORM_WIN32s) {
325 panic("Win32s is not a supported platform");
326 }
327
328 tclWinProcs = &asciiProcs;
329 }
330
331 /*
332 *----------------------------------------------------------------------
333 *
334 * TclWinGetPlatformId --
335 *
336 * Determines whether running under NT, 95, or Win32s, to allow
337 * runtime conditional code.
338 *
339 * Results:
340 * The return value is one of:
341 * VER_PLATFORM_WIN32s Win32s on Windows 3.1. (not supported)
342 * VER_PLATFORM_WIN32_WINDOWS Win32 on Windows 95.
343 * VER_PLATFORM_WIN32_NT Win32 on Windows NT
344 *
345 * Side effects:
346 * None.
347 *
348 *----------------------------------------------------------------------
349 */
350
351 int
352 TclWinGetPlatformId()
353 {
354 return platformId;
355 }
356
357 #if 0
358 /*
359 *-------------------------------------------------------------------------
360 *
361 * TclWinNoBackslash --
362 *
363 * We're always iterating through a string in Windows, changing the
364 * backslashes to slashes for use in Tcl.
365 *
366 * Results:
367 * All backslashes in given string are changed to slashes.
368 *
369 * Side effects:
370 * None.
371 *
372 *-------------------------------------------------------------------------
373 */
374
375 char *
376 TclWinNoBackslash(
377 char *path) /* String to change. */
378 {
379 char *p;
380
381 for (p = path; *p != '\0'; p++) {
382 if (*p == '\\') {
383 *p = '/';
384 }
385 }
386 return path;
387 }
388
389 /*
390 *----------------------------------------------------------------------
391 *
392 * TclpCheckStackSpace --
393 *
394 * Detect if we are about to blow the stack. Called before an
395 * evaluation can happen when nesting depth is checked.
396 *
397 * Results:
398 * 1 if there is enough stack space to continue; 0 if not.
399 *
400 * Side effects:
401 * None.
402 *
403 *----------------------------------------------------------------------
404 */
405
406 int
407 TclpCheckStackSpace()
408 {
409 int retval = 0;
410
411 /*
412 * We can recurse only if there is at least TCL_WIN_STACK_THRESHOLD
413 * bytes of stack space left. alloca() is cheap on windows; basically
414 * it just subtracts from the stack pointer causing the OS to throw an
415 * exception if the stack pointer is set below the bottom of the stack.
416 */
417
418 #ifdef HAVE_NO_SEH
419 # ifdef TCL_MEM_DEBUG
420 __asm__ __volatile__ (
421 "movl %%esp, %0" "\n\t"
422 "movl %%ebp, %1" "\n\t"
423 "movl %%fs:0, %2" "\n\t"
424 : "=m"(INITIAL_ESP),
425 "=m"(INITIAL_EBP),
426 "=r"(INITIAL_HANDLER) );
427 # endif /* TCL_MEM_DEBUG */
428
429 __asm__ __volatile__ (
430 "pushl %ebp" "\n\t"
431 "pushl $__except_checkstackspace_handler" "\n\t"
432 "pushl %fs:0" "\n\t"
433 "movl %esp, %fs:0");
434 #else
435 __try {
436 #endif /* HAVE_NO_SEH */
437 #ifdef HAVE_ALLOCA_GCC_INLINE
438 __asm__ __volatile__ (
439 "movl %0, %%eax" "\n\t"
440 "call __alloca" "\n\t"
441 :
442 : "i"(TCL_WIN_STACK_THRESHOLD)
443 : "%eax");
444 #else
445 alloca(TCL_WIN_STACK_THRESHOLD);
446 #endif /* HAVE_ALLOCA_GCC_INLINE */
447 retval = 1;
448 #ifdef HAVE_NO_SEH
449 __asm__ __volatile__ (
450 "movl %%fs:0, %%esp" "\n\t"
451 "jmp checkstackspace_pop" "\n"
452 "checkstackspace_reentry:" "\n\t"
453 "movl %%fs:0, %%eax" "\n\t"
454 "movl 0x8(%%eax), %%esp" "\n\t"
455 "movl 0x8(%%esp), %%ebp" "\n"
456 "checkstackspace_pop:" "\n\t"
457 "movl (%%esp), %%eax" "\n\t"
458 "movl %%eax, %%fs:0" "\n\t"
459 "add $12, %%esp" "\n\t"
460 :
461 :
462 : "%eax");
463
464 # ifdef TCL_MEM_DEBUG
465 __asm__ __volatile__ (
466 "movl %%esp, %0" "\n\t"
467 "movl %%ebp, %1" "\n\t"
468 "movl %%fs:0, %2" "\n\t"
469 : "=m"(RESTORED_ESP),
470 "=m"(RESTORED_EBP),
471 "=r"(RESTORED_HANDLER) );
472
473 if (INITIAL_ESP != RESTORED_ESP)
474 panic("ESP restored incorrectly");
475 if (INITIAL_EBP != RESTORED_EBP)
476 panic("EBP restored incorrectly");
477 if (INITIAL_HANDLER != RESTORED_HANDLER)
478 panic("HANDLER restored incorrectly");
479 # endif /* TCL_MEM_DEBUG */
480 #else
481 } __except (EXCEPTION_EXECUTE_HANDLER) {}
482 #endif /* HAVE_NO_SEH */
483
484 /*
485 * Avoid using control flow statements in the SEH guarded block!
486 */
487 return retval;
488 }
489 #ifdef HAVE_NO_SEH
490 static
491 __attribute__ ((cdecl))
492 EXCEPTION_DISPOSITION
493 _except_checkstackspace_handler(
494 struct _EXCEPTION_RECORD *ExceptionRecord,
495 void *EstablisherFrame,
496 struct _CONTEXT *ContextRecord,
497 void *DispatcherContext)
498 {
499 __asm__ __volatile__ (
500 "jmp checkstackspace_reentry");
501 /* Nuke compiler warning about unused static function */
502 _except_checkstackspace_handler(NULL, NULL, NULL, NULL);
503 return 0; /* Function does not return */
504 }
505 #endif /* HAVE_NO_SEH */
506
507 /*
508 *----------------------------------------------------------------------
509 *
510 * TclWinGetPlatform --
511 *
512 * This is a kludge that allows the test library to get access
513 * the internal tclPlatform variable.
514 *
515 * Results:
516 * Returns a pointer to the tclPlatform variable.
517 *
518 * Side effects:
519 * None.
520 *
521 *----------------------------------------------------------------------
522 */
523
524 TclPlatformType *
525 TclWinGetPlatform()
526 {
527 return &tclPlatform;
528 }
529
530 #endif
531 /*
532 *---------------------------------------------------------------------------
533 *
534 * TclWinSetInterfaces --
535 *
536 * A helper proc that allows the test library to change the
537 * tclWinProcs structure to dispatch to either the wide-character
538 * or multi-byte versions of the operating system calls, depending
539 * on whether Unicode is the system encoding.
540 *
541 * As well as this, we can also try to load in some additional
542 * procs which may/may not be present depending on the current
543 * Windows version (e.g. Win95 will not have the procs below).
544 *
545 * Results:
546 * None.
547 *
548 * Side effects:
549 * None.
550 *
551 *---------------------------------------------------------------------------
552 */
553
554 void
555 TclWinSetInterfaces(
556 int wide) /* Non-zero to use wide interfaces, 0
557 * otherwise. */
558 {
559 Tcl_FreeEncoding(tclWinTCharEncoding);
560
561 if (wide) {
562 tclWinProcs = &unicodeProcs;
563 tclWinTCharEncoding = Tcl_GetEncoding(NULL, "unicode");
564 if (tclWinProcs->getFileAttributesExProc == NULL) {
565 HINSTANCE hInstance = LoadLibraryA("kernel32");
566 if (hInstance != NULL) {
567 tclWinProcs->getFileAttributesExProc =
568 (BOOL (WINAPI *)(CONST TCHAR *, GET_FILEEX_INFO_LEVELS,
569 LPVOID)) GetProcAddress(hInstance, "GetFileAttributesExW");
570 tclWinProcs->createHardLinkProc =
571 (BOOL (WINAPI *)(CONST TCHAR *, CONST TCHAR*,
572 LPSECURITY_ATTRIBUTES)) GetProcAddress(hInstance,
573 "CreateHardLinkW");
574 tclWinProcs->findFirstFileExProc =
575 (HANDLE (WINAPI *)(CONST TCHAR*, UINT,
576 LPVOID, UINT, LPVOID, DWORD)) GetProcAddress(hInstance,
577 "FindFirstFileExW");
578 tclWinProcs->getVolumeNameForVMPProc =
579 (BOOL (WINAPI *)(CONST TCHAR*, TCHAR*,
580 DWORD)) GetProcAddress(hInstance,
581 "GetVolumeNameForVolumeMountPointW");
582 FreeLibrary(hInstance);
583 }
584 }
585 } else {
586 tclWinProcs = &asciiProcs;
587 tclWinTCharEncoding = NULL;
588 if (tclWinProcs->getFileAttributesExProc == NULL) {
589 HINSTANCE hInstance = LoadLibraryA("kernel32");
590 if (hInstance != NULL) {
591 tclWinProcs->getFileAttributesExProc =
592 (BOOL (WINAPI *)(CONST TCHAR *, GET_FILEEX_INFO_LEVELS,
593 LPVOID)) GetProcAddress(hInstance, "GetFileAttributesExA");
594 tclWinProcs->createHardLinkProc =
595 (BOOL (WINAPI *)(CONST TCHAR *, CONST TCHAR*,
596 LPSECURITY_ATTRIBUTES)) GetProcAddress(hInstance,
597 "CreateHardLinkA");
598 tclWinProcs->findFirstFileExProc =
599 (HANDLE (WINAPI *)(CONST TCHAR*, UINT,
600 LPVOID, UINT, LPVOID, DWORD)) GetProcAddress(hInstance,
601 "FindFirstFileExA");
602 tclWinProcs->getVolumeNameForVMPProc =
603 (BOOL (WINAPI *)(CONST TCHAR*, TCHAR*,
604 DWORD)) GetProcAddress(hInstance,
605 "GetVolumeNameForVolumeMountPointA");
606 FreeLibrary(hInstance);
607 }
608 }
609 }
610 }
611
612 /*
613 *---------------------------------------------------------------------------
614 *
615 * TclWinResetInterfaceEncodings --
616 *
617 * Called during finalization to free up any encodings we use.
618 * The tclWinProcs-> look up table is still ok to use after
619 * this call, provided no encoding conversion is required.
620 *
621 * We also clean up any memory allocated in our mount point
622 * map which is used to follow certain kinds of symlinks.
623 * That code should never be used once encodings are taken
624 * down.
625 *
626 * Results:
627 * None.
628 *
629 * Side effects:
630 * None.
631 *
632 *---------------------------------------------------------------------------
633 */
634 void
635 TclWinResetInterfaceEncodings()
636 {
637 MountPointMap *dlIter, *dlIter2;
638 if (tclWinTCharEncoding != NULL) {
639 Tcl_FreeEncoding(tclWinTCharEncoding);
640 tclWinTCharEncoding = NULL;
641 }
642 /* Clean up the mount point map */
643 Tcl_MutexLock(&mountPointMap);
644 dlIter = driveLetterLookup;
645 while (dlIter != NULL) {
646 dlIter2 = dlIter->nextPtr;
647 ckfree((char*)dlIter->volumeName);
648 ckfree((char*)dlIter);
649 dlIter = dlIter2;
650 }
651 Tcl_MutexUnlock(&mountPointMap);
652 }
653
654 /*
655 *---------------------------------------------------------------------------
656 *
657 * TclWinResetInterfaces --
658 *
659 * Called during finalization to reset us to a safe state for reuse.
660 * After this call, it is best not to use the tclWinProcs-> look
661 * up table since it is likely to be different to what is expected.
662 *
663 * Results:
664 * None.
665 *
666 * Side effects:
667 * None.
668 *
669 *---------------------------------------------------------------------------
670 */
671 void
672 TclWinResetInterfaces()
673 {
674 tclWinProcs = &asciiProcs;
675 }
676
677 /*
678 *--------------------------------------------------------------------
679 *
680 * TclWinDriveLetterForVolMountPoint
681 *
682 * Unfortunately, Windows provides no easy way at all to get hold
683 * of the drive letter for a volume mount point, but we need that
684 * information to understand paths correctly. So, we have to
685 * build an associated array to find these correctly, and allow
686 * quick and easy lookup from volume mount points to drive letters.
687 *
688 * We assume here that we are running on a system for which the wide
689 * character interfaces are used, which is valid for Win 2000 and WinXP
690 * which are the only systems on which this function will ever be called.
691 *
692 * Result: the drive letter, or -1 if no drive letter corresponds to
693 * the given mount point.
694 *
695 *--------------------------------------------------------------------
696 */
697 char
698 TclWinDriveLetterForVolMountPoint(CONST WCHAR *mountPoint)
699 {
700 MountPointMap *dlIter, *dlPtr2;
701 WCHAR Target[55]; /* Target of mount at mount point */
702 WCHAR drive[4] = { L'A', L':', L'\\', L'\0' };
703
704 /*
705 * Detect the volume mounted there. Unfortunately, there is no
706 * simple way to map a unique volume name to a DOS drive letter.
707 * So, we have to build an associative array.
708 */
709
710 Tcl_MutexLock(&mountPointMap);
711 dlIter = driveLetterLookup;
712 while (dlIter != NULL) {
713 if (wcscmp(dlIter->volumeName, mountPoint) == 0) {
714 /*
715 * We need to check whether this information is
716 * still valid, since either the user or various
717 * programs could have adjusted the mount points on
718 * the fly.
719 */
720 drive[0] = L'A' + (dlIter->driveLetter - 'A');
721 /* Try to read the volume mount point and see where it points */
722 if ((*tclWinProcs->getVolumeNameForVMPProc)((TCHAR*)drive,
723 (TCHAR*)Target, 55) != 0) {
724 if (wcscmp((WCHAR*)dlIter->volumeName, Target) == 0) {
725 /* Nothing has changed */
726 Tcl_MutexUnlock(&mountPointMap);
727 return dlIter->driveLetter;
728 }
729 }
730 /*
731 * If we reach here, unfortunately, this mount point is
732 * no longer valid at all
733 */
734 if (driveLetterLookup == dlIter) {
735 dlPtr2 = dlIter;
736 driveLetterLookup = dlIter->nextPtr;
737 } else {
738 for (dlPtr2 = driveLetterLookup;
739 dlPtr2 != NULL; dlPtr2 = dlPtr2->nextPtr) {
740 if (dlPtr2->nextPtr == dlIter) {
741 dlPtr2->nextPtr = dlIter->nextPtr;
742 dlPtr2 = dlIter;
743 break;
744 }
745 }
746 }
747 /* Now dlPtr2 points to the structure to free */
748 ckfree((char*)dlPtr2->volumeName);
749 ckfree((char*)dlPtr2);
750 /*
751 * Restart the loop --- we could try to be clever
752 * and continue half way through, but the logic is a
753 * bit messy, so it's cleanest just to restart
754 */
755 dlIter = driveLetterLookup;
756 continue;
757 }
758 dlIter = dlIter->nextPtr;
759 }
760
761 /* We couldn't find it, so we must iterate over the letters */
762
763 for (drive[0] = L'A'; drive[0] <= L'Z'; drive[0]++) {
764 /* Try to read the volume mount point and see where it points */
765 if ((*tclWinProcs->getVolumeNameForVMPProc)((TCHAR*)drive,
766 (TCHAR*)Target, 55) != 0) {
767 int alreadyStored = 0;
768 for (dlIter = driveLetterLookup; dlIter != NULL;
769 dlIter = dlIter->nextPtr) {
770 if (wcscmp((WCHAR*)dlIter->volumeName, Target) == 0) {
771 alreadyStored = 1;
772 break;
773 }
774 }
775 if (!alreadyStored) {
776 dlPtr2 = (MountPointMap*) ckalloc(sizeof(MountPointMap));
777 dlPtr2->volumeName = NativeDupInternalRep(Target);
778 dlPtr2->driveLetter = 'A' + (drive[0] - L'A');
779 dlPtr2->nextPtr = driveLetterLookup;
780 driveLetterLookup = dlPtr2;
781 }
782 }
783 }
784 /* Try again */
785 for (dlIter = driveLetterLookup; dlIter != NULL;
786 dlIter = dlIter->nextPtr) {
787 if (wcscmp(dlIter->volumeName, mountPoint) == 0) {
788 Tcl_MutexUnlock(&mountPointMap);
789 return dlIter->driveLetter;
790 }
791 }
792 /*
793 * The volume doesn't appear to correspond to a drive letter -- we
794 * remember that fact and store '-1' so we don't have to look it
795 * up each time.
796 */
797 dlPtr2 = (MountPointMap*) ckalloc(sizeof(MountPointMap));
798 dlPtr2->volumeName = NativeDupInternalRep((ClientData)mountPoint);
799 dlPtr2->driveLetter = -1;
800 dlPtr2->nextPtr = driveLetterLookup;
801 driveLetterLookup = dlPtr2;
802 Tcl_MutexUnlock(&mountPointMap);
803 return -1;
804 }
805
806 /*
807 *---------------------------------------------------------------------------
808 *
809 * Tcl_WinUtfToTChar, Tcl_WinTCharToUtf --
810 *
811 * Convert between UTF-8 and Unicode when running Windows NT or
812 * the current ANSI code page when running Windows 95.
813 *
814 * On Mac, Unix, and Windows 95, all strings exchanged between Tcl
815 * and the OS are "char" oriented. We need only one Tcl_Encoding to
816 * convert between UTF-8 and the system's native encoding. We use
817 * NULL to represent that encoding.
818 *
819 * On NT, some strings exchanged between Tcl and the OS are "char"
820 * oriented, while others are in Unicode. We need two Tcl_Encoding
821 * APIs depending on whether we are targeting a "char" or Unicode
822 * interface.
823 *
824 * Calling Tcl_UtfToExternal() or Tcl_ExternalToUtf() with an
825 * encoding of NULL should always used to convert between UTF-8
826 * and the system's "char" oriented encoding. The following two
827 * functions are used in Windows-specific code to convert between
828 * UTF-8 and Unicode strings (NT) or "char" strings(95). This saves
829 * you the trouble of writing the following type of fragment over and
830 * over:
831 *
832 * if (running NT) {
833 * encoding <- Tcl_GetEncoding("unicode");
834 * nativeBuffer <- UtfToExternal(encoding, utfBuffer);
835 * Tcl_FreeEncoding(encoding);
836 * } else {
837 * nativeBuffer <- UtfToExternal(NULL, utfBuffer);
838 * }
839 *
840 * By convention, in Windows a TCHAR is a character in the ANSI code
841 * page on Windows 95, a Unicode character on Windows NT. If you
842 * plan on targeting a Unicode interfaces when running on NT and a
843 * "char" oriented interface while running on 95, these functions
844 * should be used. If you plan on targetting the same "char"
845 * oriented function on both 95 and NT, use Tcl_UtfToExternal()
846 * with an encoding of NULL.
847 *
848 * Results:
849 * The result is a pointer to the string in the desired target
850 * encoding. Storage for the result string is allocated in
851 * dsPtr; the caller must call Tcl_DStringFree() when the result
852 * is no longer needed.
853 *
854 * Side effects:
855 * None.
856 *
857 *---------------------------------------------------------------------------
858 */
859
860 TCHAR *
861 Tcl_WinUtfToTChar(string, len, dsPtr)
862 CONST char *string; /* Source string in UTF-8. */
863 int len; /* Source string length in bytes, or < 0 for
864 * strlen(). */
865 Tcl_DString *dsPtr; /* Uninitialized or free DString in which
866 * the converted string is stored. */
867 {
868 return (TCHAR *) Tcl_UtfToExternalDString(tclWinTCharEncoding,
869 string, len, dsPtr);
870 }
871
872 char *
873 Tcl_WinTCharToUtf(string, len, dsPtr)
874 CONST TCHAR *string; /* Source string in Unicode when running
875 * NT, ANSI when running 95. */
876 int len; /* Source string length in bytes, or < 0 for
877 * platform-specific string length. */
878 Tcl_DString *dsPtr; /* Uninitialized or free DString in which
879 * the converted string is stored. */
880 {
881 return Tcl_ExternalToUtfDString(tclWinTCharEncoding,
882 (CONST char *) string, len, dsPtr);
883 }
884