Debian Codegraph @

generic/bltNsUtil.c

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