Debian Codegraph @

unix/dltest/pkgua.c

raw
1 /*
2 * pkgua.c --
3 *
4 * This file contains a simple Tcl package "pkgua" that is intended for
5 * testing the Tcl dynamic unloading facilities.
6 *
7 * Copyright (c) 1995 Sun Microsystems, Inc.
8 * Copyright (c) 2004 Georgios Petasis
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 #undef STATIC_BUILD
15 #include "tcl.h"
16
17 #if TCL_MAJOR_VERSION < 9
18 # define Tcl_Size int
19 #endif
20
21 /*
22 * In the following hash table we are going to store a struct that holds all
23 * the command tokens created by Tcl_CreateObjCommand in an interpreter,
24 * indexed by the interpreter. In this way, we can find which command tokens
25 * we have registered in a specific interpreter, in order to unload them. We
26 * need to keep the various command tokens we have registered, as they are the
27 * only safe way to unregister our registered commands, even if they have been
28 * renamed.
29 */
30
31 typedef struct ThreadSpecificData {
32 int interpTokenMapInitialised;
33 Tcl_HashTable interpTokenMap;
34 } ThreadSpecificData;
35 static Tcl_ThreadDataKey dataKey;
36 #define MAX_REGISTERED_COMMANDS 2
37
38 static void
39 CommandDeleted(void *clientData)
40 {
41 Tcl_Command *cmdToken = (Tcl_Command *)clientData;
42 *cmdToken = NULL;
43 }
44
45 static void
46 PkguaInitTokensHashTable(void)
47 {
48 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)Tcl_GetThreadData((&dataKey), sizeof(ThreadSpecificData));
49
50 if (tsdPtr->interpTokenMapInitialised) {
51 return;
52 }
53 Tcl_InitHashTable(&tsdPtr->interpTokenMap, TCL_ONE_WORD_KEYS);
54 tsdPtr->interpTokenMapInitialised = 1;
55 }
56
57 static void
58 PkguaFreeTokensHashTable(void)
59 {
60 Tcl_HashSearch search;
61 Tcl_HashEntry *entryPtr;
62 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)Tcl_GetThreadData((&dataKey), sizeof(ThreadSpecificData));
63
64 for (entryPtr = Tcl_FirstHashEntry(&tsdPtr->interpTokenMap, &search);
65 entryPtr != NULL; entryPtr = Tcl_NextHashEntry(&search)) {
66 Tcl_Free((char *) Tcl_GetHashValue(entryPtr));
67 }
68 tsdPtr->interpTokenMapInitialised = 0;
69 }
70
71 static Tcl_Command *
72 PkguaInterpToTokens(
73 Tcl_Interp *interp)
74 {
75 int newEntry;
76 Tcl_Command *cmdTokens;
77 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)Tcl_GetThreadData((&dataKey), sizeof(ThreadSpecificData));
78 Tcl_HashEntry *entryPtr =
79 Tcl_CreateHashEntry(&tsdPtr->interpTokenMap, (char *) interp, &newEntry);
80
81 if (newEntry) {
82 cmdTokens = (Tcl_Command *)
83 Tcl_Alloc(sizeof(Tcl_Command) * (MAX_REGISTERED_COMMANDS));
84 for (newEntry=0 ; newEntry<MAX_REGISTERED_COMMANDS ; ++newEntry) {
85 cmdTokens[newEntry] = NULL;
86 }
87 Tcl_SetHashValue(entryPtr, cmdTokens);
88 } else {
89 cmdTokens = (Tcl_Command *) Tcl_GetHashValue(entryPtr);
90 }
91 return cmdTokens;
92 }
93
94 static void
95 PkguaDeleteTokens(
96 Tcl_Interp *interp)
97 {
98 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)Tcl_GetThreadData((&dataKey), sizeof(ThreadSpecificData));
99 Tcl_HashEntry *entryPtr =
100 Tcl_FindHashEntry(&tsdPtr->interpTokenMap, (char *) interp);
101
102 if (entryPtr) {
103 Tcl_Free((char *) Tcl_GetHashValue(entryPtr));
104 Tcl_DeleteHashEntry(entryPtr);
105 }
106 }
107
108 /*
109 *----------------------------------------------------------------------
110 *
111 * PkguaEqObjCmd --
112 *
113 * This procedure is invoked to process the "pkgua_eq" Tcl command. It
114 * expects two arguments and returns 1 if they are the same, 0 if they
115 * are different.
116 *
117 * Results:
118 * A standard Tcl result.
119 *
120 * Side effects:
121 * See the user documentation.
122 *
123 *----------------------------------------------------------------------
124 */
125
126 static int
127 PkguaEqObjCmd(
128 void *dummy, /* Not used. */
129 Tcl_Interp *interp, /* Current interpreter. */
130 int objc, /* Number of arguments. */
131 Tcl_Obj *const objv[]) /* Argument objects. */
132 {
133 int result;
134 const char *str1, *str2;
135 Tcl_Size len1, len2;
136 (void)dummy;
137
138 if (objc != 3) {
139 Tcl_WrongNumArgs(interp, 1, objv, "string1 string2");
140 return TCL_ERROR;
141 }
142
143 str1 = Tcl_GetStringFromObj(objv[1], &len1);
144 str2 = Tcl_GetStringFromObj(objv[2], &len2);
145 len1 = Tcl_NumUtfChars(str1, len1);
146 len2 = Tcl_NumUtfChars(str2, len2);
147 if (len1 == len2) {
148 result = (Tcl_UtfNcmp(str1, str2, (size_t)len1) == 0);
149 } else {
150 result = 0;
151 }
152 Tcl_SetObjResult(interp, Tcl_NewIntObj(result));
153 return TCL_OK;
154 }
155
156 /*
157 *----------------------------------------------------------------------
158 *
159 * PkguaQuoteObjCmd --
160 *
161 * This procedure is invoked to process the "pkgua_quote" Tcl command. It
162 * expects one argument, which it returns as result.
163 *
164 * Results:
165 * A standard Tcl result.
166 *
167 * Side effects:
168 * See the user documentation.
169 *
170 *----------------------------------------------------------------------
171 */
172
173 static int
174 PkguaQuoteObjCmd(
175 void *dummy, /* Not used. */
176 Tcl_Interp *interp, /* Current interpreter. */
177 int objc, /* Number of arguments. */
178 Tcl_Obj *const objv[]) /* Argument strings. */
179 {
180 (void)dummy;
181
182 if (objc != 2) {
183 Tcl_WrongNumArgs(interp, 1, objv, "value");
184 return TCL_ERROR;
185 }
186 Tcl_SetObjResult(interp, objv[1]);
187 return TCL_OK;
188 }
189
190 /*
191 *----------------------------------------------------------------------
192 *
193 * Pkgua_Init --
194 *
195 * This is a package initialization procedure, which is called by Tcl
196 * when this package is to be added to an interpreter.
197 *
198 * Results:
199 * None.
200 *
201 * Side effects:
202 * None.
203 *
204 *----------------------------------------------------------------------
205 */
206
207 DLLEXPORT int
208 Pkgua_Init(
209 Tcl_Interp *interp) /* Interpreter in which the package is to be
210 * made available. */
211 {
212 int code;
213 Tcl_Command *cmdTokens;
214
215 if (Tcl_InitStubs(interp, "8.5-", 0) == NULL) {
216 return TCL_ERROR;
217 }
218
219 /*
220 * Initialize our Hash table, where we store the registered command tokens
221 * for each interpreter.
222 */
223
224 PkguaInitTokensHashTable();
225
226 code = Tcl_PkgProvide(interp, "pkgua", "1.0");
227 if (code != TCL_OK) {
228 return code;
229 }
230
231 Tcl_SetVar2(interp, "::pkgua_loaded", NULL, ".", TCL_APPEND_VALUE);
232
233 cmdTokens = PkguaInterpToTokens(interp);
234 cmdTokens[0] =
235 Tcl_CreateObjCommand(interp, "pkgua_eq", PkguaEqObjCmd, &cmdTokens[0],
236 CommandDeleted);
237 cmdTokens[1] =
238 Tcl_CreateObjCommand(interp, "pkgua_quote", PkguaQuoteObjCmd,
239 &cmdTokens[1], CommandDeleted);
240 return TCL_OK;
241 }
242
243 /*
244 *----------------------------------------------------------------------
245 *
246 * Pkgua_SafeInit --
247 *
248 * This is a package initialization procedure, which is called by Tcl
249 * when this package is to be added to a safe interpreter.
250 *
251 * Results:
252 * None.
253 *
254 * Side effects:
255 * None.
256 *
257 *----------------------------------------------------------------------
258 */
259
260 DLLEXPORT int
261 Pkgua_SafeInit(
262 Tcl_Interp *interp) /* Interpreter in which the package is to be
263 * made available. */
264 {
265 return Pkgua_Init(interp);
266 }
267
268 /*
269 *----------------------------------------------------------------------
270 *
271 * Pkgua_Unload --
272 *
273 * This is a package unloading initialization procedure, which is called
274 * by Tcl when this package is to be unloaded from an interpreter.
275 *
276 * Results:
277 * None.
278 *
279 * Side effects:
280 * None.
281 *
282 *----------------------------------------------------------------------
283 */
284
285 DLLEXPORT int
286 Pkgua_Unload(
287 Tcl_Interp *interp, /* Interpreter from which the package is to be
288 * unloaded. */
289 int flags) /* Flags passed by the unloading mechanism */
290 {
291 int code, cmdIndex;
292 Tcl_Command *cmdTokens = PkguaInterpToTokens(interp);
293
294 for (cmdIndex=0 ; cmdIndex<MAX_REGISTERED_COMMANDS ; cmdIndex++) {
295 if (cmdTokens[cmdIndex] == NULL) {
296 continue;
297 }
298 code = Tcl_DeleteCommandFromToken(interp, cmdTokens[cmdIndex]);
299 if (code != TCL_OK) {
300 return code;
301 }
302 }
303
304 PkguaDeleteTokens(interp);
305
306 Tcl_SetVar2(interp, "::pkgua_detached", NULL, ".", TCL_APPEND_VALUE);
307
308 if (flags == TCL_UNLOAD_DETACH_FROM_PROCESS) {
309 /*
310 * Tcl is ready to detach this library from the running application.
311 * We should free all the memory that is not related to any
312 * interpreter.
313 */
314
315 PkguaFreeTokensHashTable();
316 Tcl_SetVar2(interp, "::pkgua_unloaded", NULL, ".", TCL_APPEND_VALUE);
317 }
318 return TCL_OK;
319 }
320
321 /*
322 *----------------------------------------------------------------------
323 *
324 * Pkgua_SafeUnload --
325 *
326 * This is a package unloading initialization procedure, which is called
327 * by Tcl when this package is to be unloaded from an interpreter.
328 *
329 * Results:
330 * None.
331 *
332 * Side effects:
333 * None.
334 *
335 *----------------------------------------------------------------------
336 */
337
338 DLLEXPORT int
339 Pkgua_SafeUnload(
340 Tcl_Interp *interp, /* Interpreter from which the package is to be
341 * unloaded. */
342 int flags) /* Flags passed by the unloading mechanism */
343 {
344 return Pkgua_Unload(interp, flags);
345 }