Debian Codegraph @

generic/tclConfig.c

raw
1 /*
2 * tclConfig.c --
3 *
4 * This file provides the facilities which allow Tcl and other packages
5 * to embed configuration information into their binary libraries.
6 *
7 * Copyright (c) 2002 Andreas Kupries <[email protected]>
8 *
9 * See the file "license.terms" for information on usage and redistribution of
10 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 */
12
13 #include "tclInt.h"
14
15 /*
16 * Internal structure to hold embedded configuration information.
17 *
18 * Our structure is a two-level dictionary associated with the 'interp'. The
19 * first level is keyed with the package name and maps to the dictionary for
20 * that package. The package dictionary is keyed with metadata keys and maps
21 * to the metadata value for that key. This is package specific. The metadata
22 * values are in UTF-8, converted from the external representation given to us
23 * by the caller.
24 */
25
26 #define ASSOC_KEY "tclPackageAboutDict"
27
28 /*
29 * A ClientData struct for the QueryConfig command. Store the three bits
30 * of data we need; the package name for which we store a config dict,
31 * the (Tcl_Interp *) in which it is stored, and the encoding.
32 */
33
34 typedef struct QCCD {
35 Tcl_Obj *pkg;
36 Tcl_Interp *interp;
37 char *encoding;
38 } QCCD;
39
40 /*
41 * Static functions in this file:
42 */
43
44 static Tcl_ObjCmdProc QueryConfigObjCmd;
45 static void QueryConfigDelete(ClientData clientData);
46 static Tcl_Obj * GetConfigDict(Tcl_Interp *interp);
47 static void ConfigDictDeleteProc(ClientData clientData,
48 Tcl_Interp *interp);
49
50 /*
51 *----------------------------------------------------------------------
52 *
53 * Tcl_RegisterConfig --
54 *
55 * See TIP#59 for details on what this function does.
56 *
57 * Results:
58 * None.
59 *
60 * Side effects:
61 * Creates namespace and cfg query command in it as per TIP #59.
62 *
63 *----------------------------------------------------------------------
64 */
65
66 void
67 Tcl_RegisterConfig(
68 Tcl_Interp *interp, /* Interpreter the configuration command is
69 * registered in. */
70 const char *pkgName, /* Name of the package registering the
71 * embedded configuration. ASCII, thus in
72 * UTF-8 too. */
73 const Tcl_Config *configuration, /* Embedded configuration. */
74 const char *valEncoding) /* Name of the encoding used to store the
75 * configuration values, ASCII, thus UTF-8. */
76 {
77 Tcl_Obj *pDB, *pkgDict;
78 Tcl_DString cmdName;
79 const Tcl_Config *cfg;
80 QCCD *cdPtr = (QCCD *)ckalloc(sizeof(QCCD));
81
82 cdPtr->interp = interp;
83 if (valEncoding) {
84 cdPtr->encoding = (char *)ckalloc(strlen(valEncoding)+1);
85 strcpy(cdPtr->encoding, valEncoding);
86 } else {
87 cdPtr->encoding = NULL;
88 }
89 cdPtr->pkg = Tcl_NewStringObj(pkgName, -1);
90
91 /*
92 * Phase I: Adding the provided information to the internal database of
93 * package meta data.
94 *
95 * Phase II: Create a command for querying this database, specific to the
96 * package registering its configuration. This is the approved interface
97 * in TIP 59. In the future a more general interface should be done, as
98 * follow-up to TIP 59. Simply because our database is now general across
99 * packages, and not a structure tied to one package.
100 *
101 * Note, the created command will have a reference through its clientdata.
102 */
103
104 Tcl_IncrRefCount(cdPtr->pkg);
105
106 /*
107 * For venc == NULL aka bogus encoding we skip the step setting up the
108 * dictionaries visible at Tcl level. I.e. they are not filled
109 */
110
111 pDB = GetConfigDict(interp);
112
113 /*
114 * Retrieve package specific configuration...
115 */
116
117 if (Tcl_DictObjGet(interp, pDB, cdPtr->pkg, &pkgDict) != TCL_OK
118 || (pkgDict == NULL)) {
119 pkgDict = Tcl_NewDictObj();
120 } else if (Tcl_IsShared(pkgDict)) {
121 pkgDict = Tcl_DuplicateObj(pkgDict);
122 }
123
124 /*
125 * Extend the package configuration...
126 * We cannot assume that the encodings are initialized, therefore
127 * store the value as-is in a byte array. See Bug [9b2e636361].
128 */
129
130 for (cfg=configuration ; cfg->key!=NULL && cfg->key[0]!='\0' ; cfg++) {
131 TclDictPut(interp, pkgDict, cfg->key,
132 Tcl_NewByteArrayObj((unsigned char *)cfg->value, strlen(cfg->value)));
133 }
134
135 /*
136 * Write the changes back into the overall database.
137 */
138
139 Tcl_DictObjPut(interp, pDB, cdPtr->pkg, pkgDict);
140
141 /*
142 * Now create the interface command for retrieval of the package
143 * information.
144 */
145
146 Tcl_DStringInit(&cmdName);
147 TclDStringAppendLiteral(&cmdName, "::");
148 Tcl_DStringAppend(&cmdName, pkgName, -1);
149
150 /*
151 * The incomplete command name is the name of the namespace to place it
152 * in.
153 */
154
155 if (Tcl_FindNamespace(interp, Tcl_DStringValue(&cmdName), NULL,
156 TCL_GLOBAL_ONLY) == NULL) {
157 if (Tcl_CreateNamespace(interp, Tcl_DStringValue(&cmdName),
158 NULL, NULL) == NULL) {
159 Tcl_Panic("%s.\n%s: %s",
160 Tcl_GetStringResult(interp), "Tcl_RegisterConfig",
161 "Unable to create namespace for package configuration.");
162 }
163 }
164
165 TclDStringAppendLiteral(&cmdName, "::pkgconfig");
166
167 if (Tcl_CreateObjCommand(interp, Tcl_DStringValue(&cmdName),
168 QueryConfigObjCmd, cdPtr, QueryConfigDelete) == NULL) {
169 Tcl_Panic("%s: %s", "Tcl_RegisterConfig",
170 "Unable to create query command for package configuration");
171 }
172
173 Tcl_DStringFree(&cmdName);
174 }
175
176 /*
177 *----------------------------------------------------------------------
178 *
179 * QueryConfigObjCmd --
180 *
181 * Implementation of "::<package>::pkgconfig", the command to query
182 * configuration information embedded into a binary library.
183 *
184 * Results:
185 * A standard Tcl result.
186 *
187 * Side effects:
188 * See the manual for what this command does.
189 *
190 *----------------------------------------------------------------------
191 */
192
193 static int
194 QueryConfigObjCmd(
195 ClientData clientData,
196 Tcl_Interp *interp,
197 int objc,
198 Tcl_Obj *const *objv)
199 {
200 QCCD *cdPtr = (QCCD *)clientData;
201 Tcl_Obj *pkgName = cdPtr->pkg;
202 Tcl_Obj *pDB, *pkgDict, *val, *listPtr;
203 int n, index;
204 static const char *const subcmdStrings[] = {
205 "get", "list", NULL
206 };
207 enum subcmds {
208 CFG_GET, CFG_LIST
209 };
210 Tcl_DString conv;
211 Tcl_Encoding venc = NULL;
212 const char *value;
213
214 if ((objc < 2) || (objc > 3)) {
215 Tcl_WrongNumArgs(interp, 1, objv, "subcommand ?arg?");
216 return TCL_ERROR;
217 }
218 if (Tcl_GetIndexFromObj(interp, objv[1], subcmdStrings, "subcommand", 0,
219 &index) != TCL_OK) {
220 return TCL_ERROR;
221 }
222
223 pDB = GetConfigDict(interp);
224 if (Tcl_DictObjGet(interp, pDB, pkgName, &pkgDict) != TCL_OK
225 || pkgDict == NULL) {
226 /*
227 * Maybe a Tcl_Panic is better, because the package data has to be
228 * present.
229 */
230
231 Tcl_SetObjResult(interp, Tcl_NewStringObj("package not known", -1));
232 Tcl_SetErrorCode(interp, "TCL", "FATAL", "PKGCFG_BASE",
233 Tcl_GetString(pkgName), (char *)NULL);
234 return TCL_ERROR;
235 }
236
237 switch ((enum subcmds) index) {
238 case CFG_GET:
239 if (objc != 3) {
240 Tcl_WrongNumArgs(interp, 2, objv, "key");
241 return TCL_ERROR;
242 }
243
244 if (Tcl_DictObjGet(interp, pkgDict, objv[2], &val) != TCL_OK
245 || val == NULL) {
246 Tcl_SetObjResult(interp, Tcl_NewStringObj("key not known", -1));
247 Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "CONFIG",
248 Tcl_GetString(objv[2]), (char *)NULL);
249 return TCL_ERROR;
250 }
251
252 if (cdPtr->encoding) {
253 venc = Tcl_GetEncoding(interp, cdPtr->encoding);
254 if (!venc) {
255 return TCL_ERROR;
256 }
257 }
258 /*
259 * Value is stored as-is in a byte array, see Bug [9b2e636361],
260 * so we have to decode it first.
261 */
262 value = (const char *) Tcl_GetByteArrayFromObj(val, &n);
263 value = Tcl_ExternalToUtfDString(venc, value, n, &conv);
264 Tcl_SetObjResult(interp, Tcl_NewStringObj(value,
265 Tcl_DStringLength(&conv)));
266 Tcl_DStringFree(&conv);
267 return TCL_OK;
268
269 case CFG_LIST:
270 if (objc != 2) {
271 Tcl_WrongNumArgs(interp, 2, objv, NULL);
272 return TCL_ERROR;
273 }
274
275 Tcl_DictObjSize(interp, pkgDict, &n);
276 listPtr = Tcl_NewListObj(n, NULL);
277
278 if (!listPtr) {
279 Tcl_SetObjResult(interp, Tcl_NewStringObj(
280 "insufficient memory to create list", -1));
281 Tcl_SetErrorCode(interp, "TCL", "MEMORY", (char *)NULL);
282 return TCL_ERROR;
283 }
284
285 if (n) {
286 Tcl_DictSearch s;
287 Tcl_Obj *key;
288 int done;
289
290 for (Tcl_DictObjFirst(interp, pkgDict, &s, &key, NULL, &done);
291 !done; Tcl_DictObjNext(&s, &key, NULL, &done)) {
292 Tcl_ListObjAppendElement(NULL, listPtr, key);
293 }
294 }
295
296 Tcl_SetObjResult(interp, listPtr);
297 return TCL_OK;
298
299 default:
300 Tcl_Panic("QueryConfigObjCmd: Unknown subcommand to 'pkgconfig'. This can't happen");
301 break;
302 }
303 return TCL_ERROR;
304 }
305
306 /*
307 *-------------------------------------------------------------------------
308 *
309 * QueryConfigDelete --
310 *
311 * Command delete function. Cleans up after the configuration query
312 * command when it is deleted by the user or during finalization.
313 *
314 * Results:
315 * None.
316 *
317 * Side effects:
318 * Deallocates all non-transient memory allocated by Tcl_RegisterConfig.
319 *
320 *-------------------------------------------------------------------------
321 */
322
323 static void
324 QueryConfigDelete(
325 ClientData clientData)
326 {
327 QCCD *cdPtr = (QCCD *)clientData;
328 Tcl_Obj *pkgName = cdPtr->pkg;
329 Tcl_Obj *pDB = GetConfigDict(cdPtr->interp);
330
331 Tcl_DictObjRemove(NULL, pDB, pkgName);
332 Tcl_DecrRefCount(pkgName);
333 if (cdPtr->encoding) {
334 ckfree((char *)cdPtr->encoding);
335 }
336 ckfree((char *)cdPtr);
337 }
338
339 /*
340 *-------------------------------------------------------------------------
341 *
342 * GetConfigDict --
343 *
344 * Retrieve the package metadata database from the interpreter.
345 * Initializes it, if not present yet.
346 *
347 * Results:
348 * A Tcl_Obj reference
349 *
350 * Side effects:
351 * May allocate a Tcl_Obj.
352 *
353 *-------------------------------------------------------------------------
354 */
355
356 static Tcl_Obj *
357 GetConfigDict(
358 Tcl_Interp *interp)
359 {
360 Tcl_Obj *pDB = (Tcl_Obj *)Tcl_GetAssocData(interp, ASSOC_KEY, NULL);
361
362 if (pDB == NULL) {
363 pDB = Tcl_NewDictObj();
364 Tcl_IncrRefCount(pDB);
365 Tcl_SetAssocData(interp, ASSOC_KEY, ConfigDictDeleteProc, pDB);
366 }
367
368 return pDB;
369 }
370
371 /*
372 *----------------------------------------------------------------------
373 *
374 * ConfigDictDeleteProc --
375 *
376 * This function is associated with the "Package About dict" assoc data
377 * for an interpreter; it is invoked when the interpreter is deleted in
378 * order to free the information associated with any pending error
379 * reports.
380 *
381 * Results:
382 * None.
383 *
384 * Side effects:
385 * The package metadata database is freed.
386 *
387 *----------------------------------------------------------------------
388 */
389
390 static void
391 ConfigDictDeleteProc(
392 ClientData clientData, /* Pointer to Tcl_Obj. */
393 Tcl_Interp *interp) /* Interpreter being deleted. */
394 {
395 Tcl_Obj *pDB = (Tcl_Obj *)clientData;
396
397 Tcl_DecrRefCount(pDB);
398 }
399
400 /*
401 * Local Variables:
402 * mode: c
403 * c-basic-offset: 4
404 * fill-column: 78
405 * End:
406 */