Debian Codegraph @

macosx/tclMacOSXBundle.c

raw
1 /*
2 * tclMacOSXBundle.c --
3 *
4 * This file implements functions that inspect CFBundle structures on
5 * MacOS X.
6 *
7 * Copyright 2001-2009, Apple Inc.
8 * Copyright (c) 2003-2009 Daniel A. Steffen <[email protected]>
9 *
10 * See the file "license.terms" for information on usage and redistribution of
11 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
12 */
13
14 #include "tclPort.h"
15
16 #ifdef HAVE_COREFOUNDATION
17 #include <CoreFoundation/CoreFoundation.h>
18
19 #ifndef TCL_DYLD_USE_DLFCN
20 /*
21 * Use preferred dlfcn API on 10.4 and later
22 */
23 # if !defined(NO_DLFCN_H) && MAC_OS_X_VERSION_MAX_ALLOWED >= 1040
24 # define TCL_DYLD_USE_DLFCN 1
25 # else
26 # define TCL_DYLD_USE_DLFCN 0
27 # endif
28 #endif /* TCL_DYLD_USE_DLFCN */
29
30 #ifndef TCL_DYLD_USE_NSMODULE
31 /*
32 * Use deprecated NSModule API only to support 10.3 and earlier:
33 */
34 # if MAC_OS_X_VERSION_MIN_REQUIRED < 1040
35 # define TCL_DYLD_USE_NSMODULE 1
36 # else
37 # define TCL_DYLD_USE_NSMODULE 0
38 # endif
39 #endif /* TCL_DYLD_USE_NSMODULE */
40
41 #if TCL_DYLD_USE_DLFCN
42 #include <dlfcn.h>
43 #if defined(HAVE_WEAK_IMPORT) && MAC_OS_X_VERSION_MIN_REQUIRED < 1040
44 /*
45 * Support for weakly importing dlfcn API.
46 */
47 extern void * dlsym(void *handle, const char *symbol)
48 WEAK_IMPORT_ATTRIBUTE;
49 extern char * dlerror(void) WEAK_IMPORT_ATTRIBUTE;
50 #endif
51 #endif /* TCL_DYLD_USE_DLFCN */
52
53 #if TCL_DYLD_USE_NSMODULE
54 #include <mach-o/dyld.h>
55 #endif
56
57 #if (TCL_DYLD_USE_DLFCN && MAC_OS_X_VERSION_MIN_REQUIRED < 1040) || \
58 (MAC_OS_X_VERSION_MIN_REQUIRED < 1050)
59 MODULE_SCOPE long tclMacOSXDarwinRelease;
60 #endif
61
62 #ifdef TCL_DEBUG_LOAD
63 #define TclLoadDbgMsg(m, ...) \
64 do { \
65 fprintf(stderr, "%s:%d: %s(): " m ".\n", \
66 strrchr(__FILE__, '/')+1, __LINE__, __func__, \
67 ##__VA_ARGS__); \
68 } while (0)
69 #else
70 #define TclLoadDbgMsg(m, ...)
71 #endif /* TCL_DEBUG_LOAD */
72
73 /*
74 * Forward declaration of functions defined in this file:
75 */
76
77 static short OpenResourceMap(CFBundleRef bundleRef);
78
79 #endif /* HAVE_COREFOUNDATION */
80
81 /*
82 *----------------------------------------------------------------------
83 *
84 * OpenResourceMap --
85 *
86 * Wrapper that dynamically acquires the address for the function
87 * CFBundleOpenBundleResourceMap before calling it, since it is only
88 * present in full CoreFoundation on Mac OS X and not in CFLite on pure
89 * Darwin. Factored out because it is moderately ugly code.
90 *
91 *----------------------------------------------------------------------
92 */
93
94 #ifdef HAVE_COREFOUNDATION
95
96 static short
97 OpenResourceMap(
98 CFBundleRef bundleRef)
99 {
100 static int initialized = FALSE;
101 static short (*openresourcemap)(CFBundleRef) = NULL;
102
103 if (!initialized) {
104 #if TCL_DYLD_USE_DLFCN
105 #if MAC_OS_X_VERSION_MIN_REQUIRED < 1040
106 if (tclMacOSXDarwinRelease >= 8)
107 #endif
108 {
109 openresourcemap = (short (*)(CFBundleRef))dlsym(RTLD_NEXT,
110 "CFBundleOpenBundleResourceMap");
111 #ifdef TCL_DEBUG_LOAD
112 if (!openresourcemap) {
113 const char *errMsg = dlerror();
114
115 TclLoadDbgMsg("dlsym() failed: %s", errMsg);
116 }
117 #endif /* TCL_DEBUG_LOAD */
118 }
119 if (!openresourcemap)
120 #endif /* TCL_DYLD_USE_DLFCN */
121 {
122 #if TCL_DYLD_USE_NSMODULE
123 if (NSIsSymbolNameDefinedWithHint(
124 "_CFBundleOpenBundleResourceMap", "CoreFoundation")) {
125 NSSymbol nsSymbol = NSLookupAndBindSymbolWithHint(
126 "_CFBundleOpenBundleResourceMap", "CoreFoundation");
127
128 if (nsSymbol) {
129 openresourcemap = NSAddressOfSymbol(nsSymbol);
130 }
131 }
132 #endif /* TCL_DYLD_USE_NSMODULE */
133 }
134 initialized = TRUE;
135 }
136
137 if (openresourcemap) {
138 return openresourcemap(bundleRef);
139 }
140 return -1;
141 }
142
143 #endif /* HAVE_COREFOUNDATION */
144
145 /*
146 *----------------------------------------------------------------------
147 *
148 * Tcl_MacOSXOpenBundleResources --
149 *
150 * Given the bundle name for a shared library, this routine sets
151 * libraryPath to the Resources/Scripts directory in the framework
152 * package. If hasResourceFile is true, it will also open the main
153 * resource file for the bundle.
154 *
155 * Results:
156 * TCL_OK if the bundle could be opened, and the Scripts folder found.
157 * TCL_ERROR otherwise.
158 *
159 * Side effects:
160 * libraryVariableName may be set, and the resource file opened.
161 *
162 *----------------------------------------------------------------------
163 */
164
165 #undef Tcl_MacOSXOpenBundleResources
166 int
167 Tcl_MacOSXOpenBundleResources(
168 Tcl_Interp *interp,
169 const char *bundleName,
170 int hasResourceFile,
171 int maxPathLen,
172 char *libraryPath)
173 {
174 return Tcl_MacOSXOpenVersionedBundleResources(interp, bundleName, NULL,
175 hasResourceFile, maxPathLen, libraryPath);
176 }
177
178 /*
179 *----------------------------------------------------------------------
180 *
181 * Tcl_MacOSXOpenVersionedBundleResources --
182 *
183 * Given the bundle and version name for a shared library (version name
184 * can be NULL to indicate latest version), this routine sets libraryPath
185 * to the Resources/Scripts directory in the framework package. If
186 * hasResourceFile is true, it will also open the main resource file for
187 * the bundle.
188 *
189 * Results:
190 * TCL_OK if the bundle could be opened, and the Scripts folder found.
191 * TCL_ERROR otherwise.
192 *
193 * Side effects:
194 * libraryVariableName may be set, and the resource file opened.
195 *
196 *----------------------------------------------------------------------
197 */
198
199 int
200 Tcl_MacOSXOpenVersionedBundleResources(
201 Tcl_Interp *interp,
202 const char *bundleName,
203 const char *bundleVersion,
204 int hasResourceFile,
205 int maxPathLen,
206 char *libraryPath)
207 {
208 #ifdef HAVE_COREFOUNDATION
209 CFBundleRef bundleRef, versionedBundleRef = NULL;
210 CFStringRef bundleNameRef;
211 CFURLRef libURL;
212
213 libraryPath[0] = '\0';
214
215 bundleNameRef = CFStringCreateWithCString(NULL, bundleName,
216 kCFStringEncodingUTF8);
217
218 bundleRef = CFBundleGetBundleWithIdentifier(bundleNameRef);
219 CFRelease(bundleNameRef);
220
221 if (bundleVersion && bundleRef) {
222 /*
223 * Create bundle from bundleVersion subdirectory of 'Versions'.
224 */
225
226 CFURLRef bundleURL = CFBundleCopyBundleURL(bundleRef);
227
228 if (bundleURL) {
229 CFStringRef bundleVersionRef = CFStringCreateWithCString(NULL,
230 bundleVersion, kCFStringEncodingUTF8);
231
232 if (bundleVersionRef) {
233 CFComparisonResult versionComparison = kCFCompareLessThan;
234 CFStringRef bundleTailRef = CFURLCopyLastPathComponent(
235 bundleURL);
236
237 if (bundleTailRef) {
238 versionComparison = CFStringCompare(bundleTailRef,
239 bundleVersionRef, 0);
240 CFRelease(bundleTailRef);
241 }
242 if (versionComparison != kCFCompareEqualTo) {
243 CFURLRef versURL = CFURLCreateCopyAppendingPathComponent(
244 NULL, bundleURL, CFSTR("Versions"), TRUE);
245
246 if (versURL) {
247 CFURLRef versionedBundleURL =
248 CFURLCreateCopyAppendingPathComponent(
249 NULL, versURL, bundleVersionRef, TRUE);
250
251 if (versionedBundleURL) {
252 versionedBundleRef = CFBundleCreate(NULL,
253 versionedBundleURL);
254 if (versionedBundleRef) {
255 bundleRef = versionedBundleRef;
256 }
257 CFRelease(versionedBundleURL);
258 }
259 CFRelease(versURL);
260 }
261 }
262 CFRelease(bundleVersionRef);
263 }
264 CFRelease(bundleURL);
265 }
266 }
267
268 if (bundleRef) {
269 if (hasResourceFile) {
270 (void) OpenResourceMap(bundleRef);
271 }
272
273 libURL = CFBundleCopyResourceURL(bundleRef, CFSTR("Scripts"),
274 NULL, NULL);
275
276 if (libURL) {
277 /*
278 * FIXME: This is a quick fix, it is probably not right for
279 * internationalization.
280 */
281
282 CFURLGetFileSystemRepresentation(libURL, TRUE,
283 (unsigned char *) libraryPath, maxPathLen);
284 CFRelease(libURL);
285 }
286 if (versionedBundleRef) {
287 #if MAC_OS_X_VERSION_MIN_REQUIRED < 1050
288 /*
289 * Workaround CFBundle bug in Tiger and earlier. [Bug 2569449]
290 */
291
292 if (tclMacOSXDarwinRelease >= 9)
293 #endif
294 {
295 CFRelease(versionedBundleRef);
296 }
297 }
298 }
299
300 if (libraryPath[0]) {
301 return TCL_OK;
302 }
303 #endif /* HAVE_COREFOUNDATION */
304 return TCL_ERROR;
305 }
306
307 /*
308 * Local Variables:
309 * mode: c
310 * c-basic-offset: 4
311 * fill-column: 78
312 * End:
313 */