Debian Codegraph @

generic/tclStubLib.c

raw
1 /*
2 * tclStubLib.c --
3 *
4 * Stub object that will be statically linked into extensions that want
5 * to access Tcl.
6 *
7 * Copyright (c) 1998-1999 Scriptics Corporation.
8 * Copyright (c) 1998 Paul Duffin.
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 "tclInt.h"
15
16 MODULE_SCOPE const TclStubs *tclStubsPtr;
17 MODULE_SCOPE const TclPlatStubs *tclPlatStubsPtr;
18 MODULE_SCOPE const TclIntStubs *tclIntStubsPtr;
19 MODULE_SCOPE const TclIntPlatStubs *tclIntPlatStubsPtr;
20
21 const TclStubs *tclStubsPtr = NULL;
22 const TclPlatStubs *tclPlatStubsPtr = NULL;
23 const TclIntStubs *tclIntStubsPtr = NULL;
24 const TclIntPlatStubs *tclIntPlatStubsPtr = NULL;
25
26 /*
27 * Use our own ISDIGIT to avoid linking to libc on windows
28 */
29
30 #define ISDIGIT(c) (((unsigned)((c)-'0')) <= 9)
31
32 /*
33 *----------------------------------------------------------------------
34 *
35 * Tcl_InitStubs --
36 *
37 * Tries to initialise the stub table pointers and ensures that the
38 * correct version of Tcl is loaded.
39 *
40 * Results:
41 * The actual version of Tcl that satisfies the request, or NULL to
42 * indicate that an error occurred.
43 *
44 * Side effects:
45 * Sets the stub table pointers.
46 *
47 *----------------------------------------------------------------------
48 */
49 #undef Tcl_InitStubs
50 MODULE_SCOPE const char *
51 Tcl_InitStubs(
52 Tcl_Interp *interp,
53 const char *version,
54 int exact)
55 {
56 Interp *iPtr = (Interp *)interp;
57 const char *actualVersion = NULL;
58 void *pkgData = NULL;
59 const TclStubs *stubsPtr = iPtr->stubTable;
60
61 /*
62 * We can't optimize this check by caching tclStubsPtr because that
63 * prevents apps from being able to load/unload Tcl dynamically multiple
64 * times. [Bug 615304]
65 */
66
67 if (!stubsPtr || (stubsPtr->magic != TCL_STUB_MAGIC)) {
68 exact &= 0xFFFF00; /* Filter out minor/major Tcl version */
69 if (!exact) {
70 exact = 0x060800;
71 }
72 if (stubsPtr && (stubsPtr->magic == ((int)0xFCA3BACB + (int)sizeof(void *)))
73 && ((exact|0x010000) == 0x070800)) {
74 /* We are running in Tcl 9.x, but extension is compiled with 8.6 */
75 stubsPtr->tcl_SetObjResult(interp, stubsPtr->tcl_ObjPrintf(
76 "this extension is compiled for Tcl %d.%d",
77 (exact & 0x0FF00)>>8, (exact & 0x0FF0000)>>16));
78 } else {
79 iPtr->result = (char *)"interpreter uses an incompatible stubs mechanism";
80 iPtr->freeProc = TCL_STATIC;
81 }
82 return NULL;
83 }
84
85 actualVersion = stubsPtr->tcl_PkgRequireEx(interp, "Tcl", version, 0, &pkgData);
86 if (actualVersion == NULL) {
87 /* Even when the Tcl version does not match, the caller should at least be
88 * able to use Tcl_GetObjResult/Tcl_GetString/Tcl_Panic for error-handling */
89 tclStubsPtr = stubsPtr; /* See: [fd8341e496] */
90 return NULL;
91 }
92 if (exact&1) {
93 const char *p = version;
94 int count = 0;
95
96 while (*p) {
97 count += !ISDIGIT(*p++);
98 }
99 if (count == 1) {
100 const char *q = actualVersion;
101
102 p = version;
103 while (*p && (*p == *q)) {
104 p++; q++;
105 }
106 if (*p || ISDIGIT(*q)) {
107 /* Construct error message */
108 stubsPtr->tcl_PkgRequireEx(interp, "Tcl", version, 1, NULL);
109 return NULL;
110 }
111 } else {
112 actualVersion = stubsPtr->tcl_PkgRequireEx(interp, "Tcl", version, 1, NULL);
113 if (actualVersion == NULL) {
114 return NULL;
115 }
116 }
117 }
118 tclStubsPtr = (TclStubs *)pkgData;
119
120 if (tclStubsPtr->hooks) {
121 tclPlatStubsPtr = tclStubsPtr->hooks->tclPlatStubs;
122 tclIntStubsPtr = tclStubsPtr->hooks->tclIntStubs;
123 tclIntPlatStubsPtr = tclStubsPtr->hooks->tclIntPlatStubs;
124 } else {
125 tclPlatStubsPtr = NULL;
126 tclIntStubsPtr = NULL;
127 tclIntPlatStubsPtr = NULL;
128 }
129
130 return actualVersion;
131 }
132
133 /*
134 * Local Variables:
135 * mode: c
136 * c-basic-offset: 4
137 * fill-column: 78
138 * End:
139 */