Debian Codegraph @

generic/bltNsUtil.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 /*
2 * bltNsUtil.c --
3 *
4 * This module implements utility procedures for namespaces
5 * in the BLT toolkit.
6 *
7 * Copyright 1991-1998 Lucent Technologies, Inc.
8 *
9 * Permission to use, copy, modify, and distribute this software and
10 * its documentation for any purpose and without fee is hereby
11 * granted, provided that the above copyright notice appear in all
12 * copies and that both that the copyright notice and warranty
13 * disclaimer appear in supporting documentation, and that the names
14 * of Lucent Technologies any of their entities not be used in
15 * advertising or publicity pertaining to distribution of the software
16 * without specific, written prior permission.
17 *
18 * Lucent Technologies disclaims all warranties with regard to this
19 * software, including all implied warranties of merchantability and
20 * fitness. In no event shall Lucent Technologies be liable for any
21 * special, indirect or consequential damages or any damages
22 * whatsoever resulting from loss of use, data or profits, whether in
23 * an action of contract, negligence or other tortuous action, arising
24 * out of or in connection with the use or performance of this
25 * software.
26 */
27
28 #include "bltInt.h"
29 #include "bltList.h"
30
31 /* Namespace related routines */
32
33 /*
34 * ----------------------------------------------------------------------
35 *
36 * Blt_GetVariableNamespace --
37 *
38 * Returns the namespace context of the vector variable. If NULL,
39 * this indicates that the variable is local to the call frame.
40 *
41 * Note the ever-dangerous manner in which we get this information.
42 * All of these structures are "private". Now who's calling Tcl
43 * an "extension" language?
44 *
45 * Results:
46 * Returns the context of the namespace in an opaque type.
47 *
48 * ----------------------------------------------------------------------
49 */
50
51
52 /*
53 * A Command structure exists for each command in a namespace. The
54 * Tcl_Command opaque type actually refers to these structures.
55 */
56
57 Tcl_Namespace *
58 Blt_GetVariableNamespace(interp, name)
59 Tcl_Interp *interp;
60 CONST char *name;
61 {
62 Tcl_Var varPtr;
63 Tcl_Namespace *nsPtr;
64 Tcl_Obj *objPtr;
65 char *str, *cp;
66
67 varPtr = Tcl_FindNamespaceVar(interp, (char *)name,
68 (Tcl_Namespace *)NULL, 0);
69 if (varPtr == NULL) {
70 return NULL;
71 }
72 objPtr = Tcl_NewObj();
73 Tcl_GetVariableFullName(interp, varPtr, objPtr);
74 str = Tcl_GetString(objPtr);
75 if (Blt_ParseQualifiedName(interp, str, &nsPtr, &cp) == NULL) {
76 nsPtr = NULL;
77 }
78 Tcl_DecrRefCount(objPtr);
79 return nsPtr;
80 }
81
82 /*ARGSUSED*/
83 Tcl_Namespace *
84 Blt_GetCommandNamespace(interp, cmdToken)
85 Tcl_Interp *interp; /* Not used. */
86 Tcl_Command cmdToken;
87 {
88 Tcl_CmdInfo info;
89
90 if (Tcl_GetCommandInfoFromToken(cmdToken, &info) == 0) {
91 return NULL;
92 }
93 return info.namespacePtr;
94 }
95
96 Tcl_CallFrame *
97 Blt_EnterNamespace(interp, nsPtr)
98 Tcl_Interp *interp;
99 Tcl_Namespace *nsPtr;
100 {
101 Tcl_CallFrame *framePtr;
102
103 framePtr = Blt_Malloc(sizeof(Tcl_CallFrame));
104 assert(framePtr);
105 if (Tcl_PushCallFrame(interp, framePtr, (Tcl_Namespace *)nsPtr, 0)
106 != TCL_OK) {
107 Blt_Free(framePtr);
108 return NULL;
109 }
110 return framePtr;
111 }
112
113 void
114 Blt_LeaveNamespace(interp, framePtr)
115 Tcl_Interp *interp;
116 Tcl_CallFrame *framePtr;
117 {
118 Tcl_PopCallFrame(interp);
119 Blt_Free(framePtr);
120 }
121
122 int
123 Blt_ParseQualifiedName(interp, qualName, nsPtrPtr, namePtrPtr)
124 Tcl_Interp *interp;
125 CONST char *qualName;
126 Tcl_Namespace **nsPtrPtr;
127 CONST char **namePtrPtr;
128 {
129 register char *p, *colon;
130 Tcl_Namespace *nsPtr;
131
132 colon = NULL;
133 p = (char *)(qualName + strlen(qualName));
134 while (--p > qualName) {
135 if ((*p == ':') && (*(p - 1) == ':')) {
136 p++; /* just after the last "::" */
137 colon = p - 2;
138 break;
139 }
140 }
141 if (colon == NULL) {
142 *nsPtrPtr = NULL;
143 *namePtrPtr = (char *)qualName;
144 return TCL_OK;
145 }
146 *colon = '\0';
147 if (qualName[0] == '\0') {
148 nsPtr = Tcl_GetGlobalNamespace(interp);
149 } else {
150 nsPtr = Tcl_FindNamespace(interp, (char *)qualName,
151 (Tcl_Namespace *)NULL, 0);
152 }
153 *colon = ':';
154 if (nsPtr == NULL) {
155 return TCL_ERROR;
156 }
157 *nsPtrPtr = nsPtr;
158 *namePtrPtr = p;
159 return TCL_OK;
160 }
161
162 char *
163 Blt_GetQualifiedName(nsPtr, name, resultPtr)
164 Tcl_Namespace *nsPtr;
165 CONST char *name;
166 Tcl_DString *resultPtr;
167 {
168 Tcl_DStringInit(resultPtr);
169 if ((nsPtr->fullName[0] != ':') || (nsPtr->fullName[1] != ':') ||
170 (nsPtr->fullName[2] != '\0')) {
171 Tcl_DStringAppend(resultPtr, nsPtr->fullName, -1);
172 }
173 Tcl_DStringAppend(resultPtr, "::", -1);
174 Tcl_DStringAppend(resultPtr, (char *)name, -1);
175 return Tcl_DStringValue(resultPtr);
176 }
177
178
179 typedef struct {
180 Tcl_HashTable clientTable;
181
182 /* Original clientdata and delete procedure. */
183 ClientData origClientData;
184 Tcl_NamespaceDeleteProc *origDeleteProc;
185
186 } Callback;
187
188 static Tcl_CmdProc NamespaceDeleteCmd;
189 static Tcl_NamespaceDeleteProc NamespaceDeleteNotify;
190
191 #define NS_DELETE_CMD "#NamespaceDeleteNotifier"
192
193 /*ARGSUSED*/
194 static int
195 NamespaceDeleteCmd(clientData, interp, argc, argv)
196 ClientData clientData; /* Not used. */
197 Tcl_Interp *interp; /* */
198 int argc;
199 char **argv;
200 {
201 Tcl_AppendResult(interp, "command \"", argv[0], "\" shouldn't be invoked",
202 (char *)NULL);
203 return TCL_ERROR;
204 }
205
206 static void
207 NamespaceDeleteNotify(clientData)
208 ClientData clientData;
209 {
210 Blt_List list;
211 Blt_ListNode node;
212 Tcl_CmdDeleteProc *deleteProc;
213
214 list = (Blt_List)clientData;
215 for (node = Blt_ListFirstNode(list); node != NULL;
216 node = Blt_ListNextNode(node)) {
217 deleteProc = (Tcl_CmdDeleteProc *)Blt_ListGetValue(node);
218 clientData = (ClientData)Blt_ListGetKey(node);
219 (*deleteProc) (clientData);
220 }
221 Blt_ListDestroy(list);
222 }
223
224 void
225 Blt_DestroyNsDeleteNotify(interp, nsPtr, clientData)
226 Tcl_Interp *interp;
227 Tcl_Namespace *nsPtr;
228 ClientData clientData;
229 {
230 Blt_List list;
231 Blt_ListNode node;
232 char *string;
233 Tcl_CmdInfo cmdInfo;
234
235 string = Blt_Malloc(sizeof(nsPtr->fullName) + strlen(NS_DELETE_CMD) + 4);
236 strcpy(string, nsPtr->fullName);
237 strcat(string, "::");
238 strcat(string, NS_DELETE_CMD);
239 if (!Tcl_GetCommandInfo(interp, string, &cmdInfo)) {
240 goto done;
241 }
242 list = (Blt_List)cmdInfo.clientData;
243 node = Blt_ListGetNode(list, clientData);
244 if (node != NULL) {
245 Blt_ListDeleteNode(node);
246 }
247 done:
248 Blt_Free(string);
249 }
250
251 int
252 Blt_CreateNsDeleteNotify(interp, nsPtr, clientData, deleteProc)
253 Tcl_Interp *interp;
254 Tcl_Namespace *nsPtr;
255 ClientData clientData;
256 Tcl_CmdDeleteProc *deleteProc;
257 {
258 Blt_List list;
259 char *string;
260 Tcl_CmdInfo cmdInfo;
261
262 string = Blt_Malloc(sizeof(nsPtr->fullName) + strlen(NS_DELETE_CMD) + 4);
263 strcpy(string, nsPtr->fullName);
264 strcat(string, "::");
265 strcat(string, NS_DELETE_CMD);
266 if (!Tcl_GetCommandInfo(interp, string, &cmdInfo)) {
267 list = Blt_ListCreate(BLT_ONE_WORD_KEYS);
268 Blt_CreateCommand(interp, string, NamespaceDeleteCmd, list,
269 NamespaceDeleteNotify);
270 } else {
271 list = (Blt_List)cmdInfo.clientData;
272 }
273 Blt_Free(string);
274 Blt_ListAppend(list, clientData, (ClientData)deleteProc);
275 return TCL_OK;
276 }
277
278 /*
279 *----------------------------------------------------------------------
280 *
281 * Blt_CreateCommand --
282 *
283 * Like Tcl_CreateCommand, but creates command in current namespace
284 * instead of global, if one isn't defined. Not a problem with
285 * [incr Tcl] namespaces.
286 *
287 * Results:
288 * The return value is a token for the command, which can
289 * be used in future calls to Tcl_GetCommandName.
290 *
291 *----------------------------------------------------------------------
292 */
293 Tcl_Command
294 Blt_CreateCommand(interp, cmdName, proc, clientData, deleteProc)
295 Tcl_Interp *interp; /* Token for command interpreter returned by
296 * a previous call to Tcl_CreateInterp. */
297 CONST char *cmdName; /* Name of command. If it contains namespace
298 * qualifiers, the new command is put in the
299 * specified namespace; otherwise it is put
300 * in the global namespace. */
301 Tcl_CmdProc *proc; /* Procedure to associate with cmdName. */
302 ClientData clientData; /* Arbitrary value passed to string proc. */
303 Tcl_CmdDeleteProc *deleteProc;
304 /* If not NULL, gives a procedure to call
305
306 * when this command is deleted. */
307 {
308 register CONST char *p;
309
310 p = cmdName + strlen(cmdName);
311 while (--p > cmdName) {
312 if ((*p == ':') && (*(p - 1) == ':')) {
313 p++; /* just after the last "::" */
314 break;
315 }
316 }
317 if (cmdName == p) {
318 Tcl_DString dString;
319 Tcl_Namespace *nsPtr;
320 Tcl_Command cmdToken;
321
322 Tcl_DStringInit(&dString);
323 nsPtr = Tcl_GetCurrentNamespace(interp);
324 Tcl_DStringAppend(&dString, nsPtr->fullName, -1);
325 Tcl_DStringAppend(&dString, "::", -1);
326 Tcl_DStringAppend(&dString, cmdName, -1);
327 cmdToken = Tcl_CreateCommand(interp, Tcl_DStringValue(&dString), proc,
328 clientData, deleteProc);
329 Tcl_DStringFree(&dString);
330 return cmdToken;
331 }
332 return Tcl_CreateCommand(interp, (char *)cmdName, proc, clientData,
333 deleteProc);
334 }
335
336 /*
337 *----------------------------------------------------------------------
338 *
339 * Blt_CreateCommandObj --
340 *
341 * Like Tcl_CreateCommand, but creates command in current namespace
342 * instead of global, if one isn't defined. Not a problem with
343 * [incr Tcl] namespaces.
344 *
345 * Results:
346 * The return value is a token for the command, which can
347 * be used in future calls to Tcl_GetCommandName.
348 *
349 *----------------------------------------------------------------------
350 */
351 Tcl_Command
352 Blt_CreateCommandObj(interp, cmdName, proc, clientData, deleteProc)
353 Tcl_Interp *interp; /* Token for command interpreter returned by
354 * a previous call to Tcl_CreateInterp. */
355 CONST char *cmdName; /* Name of command. If it contains namespace
356 * qualifiers, the new command is put in the
357 * specified namespace; otherwise it is put
358 * in the global namespace. */
359 Tcl_ObjCmdProc *proc; /* Procedure to associate with cmdName. */
360 ClientData clientData; /* Arbitrary value passed to string proc. */
361 Tcl_CmdDeleteProc *deleteProc;
362 /* If not NULL, gives a procedure to call
363 * when this command is deleted. */
364 {
365 register CONST char *p;
366
367 p = cmdName + strlen(cmdName);
368 while (--p > cmdName) {
369 if ((*p == ':') && (*(p - 1) == ':')) {
370 p++; /* just after the last "::" */
371 break;
372 }
373 }
374 if (cmdName == p) {
375 Tcl_DString dString;
376 Tcl_Namespace *nsPtr;
377 Tcl_Command cmdToken;
378
379 Tcl_DStringInit(&dString);
380 nsPtr = Tcl_GetCurrentNamespace(interp);
381 Tcl_DStringAppend(&dString, nsPtr->fullName, -1);
382 Tcl_DStringAppend(&dString, "::", -1);
383 Tcl_DStringAppend(&dString, cmdName, -1);
384 cmdToken = Tcl_CreateObjCommand(interp, Tcl_DStringValue(&dString),
385 proc, clientData, deleteProc);
386 Tcl_DStringFree(&dString);
387 return cmdToken;
388 }
389 return Tcl_CreateObjCommand(interp, (char *)cmdName, proc, clientData,
390 deleteProc);
391 }