Debian Codegraph @

pTk/mTk/tclGeneric/tclIndexObj.c

raw symbols SCIP
1 /*
2 * tclIndexObj.c --
3 *
4 * This file implements objects of type "index". This object type
5 * is used to lookup a keyword in a table of valid values and cache
6 * the index of the matching entry.
7 *
8 * Copyright (c) 1997 Sun Microsystems, Inc.
9 *
10 * See the file "license.terms" for information on usage and redistribution
11 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
12 *
13 * RCS: @(#) $Id: tclIndexObj.c,v 1.16 2002/02/28 05:11:25 dgp Exp $
14 */
15
16 #include "tkPort.h"
17 #include "Lang.h"
18
19 #if 0
20
21 /*
22 * Prototypes for procedures defined later in this file:
23 */
24
25 static int SetIndexFromAny _ANSI_ARGS_((Tcl_Interp *interp,
26 Tcl_Obj *objPtr));
27 static void UpdateStringOfIndex _ANSI_ARGS_((Tcl_Obj *objPtr));
28 static void DupIndex _ANSI_ARGS_((Tcl_Obj *srcPtr,
29 Tcl_Obj *dupPtr));
30 static void FreeIndex _ANSI_ARGS_((Tcl_Obj *objPtr));
31
32 /*
33 * The structure below defines the index Tcl object type by means of
34 * procedures that can be invoked by generic object code.
35 */
36
37 Tcl_ObjType tclIndexType = {
38 "index", /* name */
39 FreeIndex, /* freeIntRepProc */
40 DupIndex, /* dupIntRepProc */
41 UpdateStringOfIndex, /* updateStringProc */
42 SetIndexFromAny /* setFromAnyProc */
43 };
44
45 /*
46 * The definition of the internal representation of the "index"
47 * object; The internalRep.otherValuePtr field of an object of "index"
48 * type will be a pointer to one of these structures.
49 *
50 * Keep this structure declaration in sync with tclTestObj.c
51 */
52
53 typedef struct {
54 VOID *tablePtr; /* Pointer to the table of strings */
55 int offset; /* Offset between table entries */
56 int index; /* Selected index into table. */
57 } IndexRep;
58
59 /*
60 * The following macros greatly simplify moving through a table...
61 */
62 #define STRING_AT(table, offset, index) \
63 (*((CONST char * CONST *)(((char *)(table)) + ((offset) * (index)))))
64 #define NEXT_ENTRY(table, offset) \
65 (&(STRING_AT(table, offset, 1)))
66 #define EXPAND_OF(indexRep) \
67 STRING_AT((indexRep)->tablePtr, (indexRep)->offset, (indexRep)->index)
68
69
70 /*
71 *----------------------------------------------------------------------
72 *
73 * Tcl_GetIndexFromObj --
74 *
75 * This procedure looks up an object's value in a table of strings
76 * and returns the index of the matching string, if any.
77 *
78 * Results:
79 *
80 * If the value of objPtr is identical to or a unique abbreviation
81 * for one of the entries in objPtr, then the return value is
82 * TCL_OK and the index of the matching entry is stored at
83 * *indexPtr. If there isn't a proper match, then TCL_ERROR is
84 * returned and an error message is left in interp's result (unless
85 * interp is NULL). The msg argument is used in the error
86 * message; for example, if msg has the value "option" then the
87 * error message will say something flag 'bad option "foo": must be
88 * ...'
89 *
90 * Side effects:
91 * The result of the lookup is cached as the internal rep of
92 * objPtr, so that repeated lookups can be done quickly.
93 *
94 *----------------------------------------------------------------------
95 */
96
97 int
98 Tcl_GetIndexFromObj(interp, objPtr, tablePtr, msg, flags, indexPtr)
99 Tcl_Interp *interp; /* Used for error reporting if not NULL. */
100 Tcl_Obj *objPtr; /* Object containing the string to lookup. */
101 CONST char **tablePtr; /* Array of strings to compare against the
102 * value of objPtr; last entry must be NULL
103 * and there must not be duplicate entries. */
104 CONST char *msg; /* Identifying word to use in error messages. */
105 int flags; /* 0 or TCL_EXACT */
106 int *indexPtr; /* Place to store resulting integer index. */
107 {
108
109 /*
110 * See if there is a valid cached result from a previous lookup
111 * (doing the check here saves the overhead of calling
112 * Tcl_GetIndexFromObjStruct in the common case where the result
113 * is cached).
114 */
115
116 if (objPtr->typePtr == &tclIndexType) {
117 IndexRep *indexRep = (IndexRep *) objPtr->internalRep.otherValuePtr;
118 /*
119 * Here's hoping we don't get hit by unfortunate packing
120 * constraints on odd platforms like a Cray PVP...
121 */
122 if (indexRep->tablePtr == (VOID *)tablePtr &&
123 indexRep->offset == sizeof(char *)) {
124 *indexPtr = indexRep->index;
125 return TCL_OK;
126 }
127 }
128 return Tcl_GetIndexFromObjStruct(interp, objPtr, tablePtr, sizeof(char *),
129 msg, flags, indexPtr);
130 }
131
132 /*
133 *----------------------------------------------------------------------
134 *
135 * Tcl_GetIndexFromObjStruct --
136 *
137 * This procedure looks up an object's value given a starting
138 * string and an offset for the amount of space between strings.
139 * This is useful when the strings are embedded in some other
140 * kind of array.
141 *
142 * Results:
143 *
144 * If the value of objPtr is identical to or a unique abbreviation
145 * for one of the entries in objPtr, then the return value is
146 * TCL_OK and the index of the matching entry is stored at
147 * *indexPtr. If there isn't a proper match, then TCL_ERROR is
148 * returned and an error message is left in interp's result (unless
149 * interp is NULL). The msg argument is used in the error
150 * message; for example, if msg has the value "option" then the
151 * error message will say something flag 'bad option "foo": must be
152 * ...'
153 *
154 * Side effects:
155 * The result of the lookup is cached as the internal rep of
156 * objPtr, so that repeated lookups can be done quickly.
157 *
158 *----------------------------------------------------------------------
159 */
160
161 int
162 Tcl_GetIndexFromObjStruct(interp, objPtr, tablePtr, offset, msg, flags,
163 indexPtr)
164 Tcl_Interp *interp; /* Used for error reporting if not NULL. */
165 Tcl_Obj *objPtr; /* Object containing the string to lookup. */
166 CONST VOID *tablePtr; /* The first string in the table. The second
167 * string will be at this address plus the
168 * offset, the third plus the offset again,
169 * etc. The last entry must be NULL
170 * and there must not be duplicate entries. */
171 int offset; /* The number of bytes between entries */
172 CONST char *msg; /* Identifying word to use in error messages. */
173 int flags; /* 0 or TCL_EXACT */
174 int *indexPtr; /* Place to store resulting integer index. */
175 {
176 int index, length, i, numAbbrev;
177 char *key, *p1;
178 CONST char *p2;
179 CONST char * CONST *entryPtr;
180 Tcl_Obj *resultPtr;
181 IndexRep *indexRep;
182
183 /*
184 * See if there is a valid cached result from a previous lookup.
185 */
186
187 if (objPtr->typePtr == &tclIndexType) {
188 indexRep = (IndexRep *) objPtr->internalRep.otherValuePtr;
189 if (indexRep->tablePtr==tablePtr && indexRep->offset==offset) {
190 *indexPtr = indexRep->index;
191 return TCL_OK;
192 }
193 }
194
195 /*
196 * Lookup the value of the object in the table. Accept unique
197 * abbreviations unless TCL_EXACT is set in flags.
198 */
199
200 key = Tcl_GetStringFromObj(objPtr, &length);
201 index = -1;
202 numAbbrev = 0;
203
204 /*
205 * The key should not be empty, otherwise it's not a match.
206 */
207
208 if (key[0] == '\0') {
209 goto error;
210 }
211
212 /*
213 * Scan the table looking for one of:
214 * - An exact match (always preferred)
215 * - A single abbreviation (allowed depending on flags)
216 * - Several abbreviations (never allowed, but overridden by exact match)
217 */
218 for (entryPtr = tablePtr, i = 0; *entryPtr != NULL;
219 entryPtr = NEXT_ENTRY(entryPtr, offset), i++) {
220 for (p1 = key, p2 = *entryPtr; *p1 == *p2; p1++, p2++) {
221 if (*p1 == '\0') {
222 index = i;
223 goto done;
224 }
225 }
226 if (*p1 == '\0') {
227 /*
228 * The value is an abbreviation for this entry. Continue
229 * checking other entries to make sure it's unique. If we
230 * get more than one unique abbreviation, keep searching to
231 * see if there is an exact match, but remember the number
232 * of unique abbreviations and don't allow either.
233 */
234
235 numAbbrev++;
236 index = i;
237 }
238 }
239 /*
240 * Check if we were instructed to disallow abbreviations.
241 */
242 if ((flags & TCL_EXACT) || (numAbbrev != 1)) {
243 goto error;
244 }
245
246 done:
247 /*
248 * Cache the found representation. Note that we want to avoid
249 * allocating a new internal-rep if at all possible since that is
250 * potentially a slow operation.
251 */
252 if (objPtr->typePtr == &tclIndexType) {
253 indexRep = (IndexRep *) objPtr->internalRep.otherValuePtr;
254 } else {
255 if ((objPtr->typePtr != NULL)
256 && (objPtr->typePtr->freeIntRepProc != NULL)) {
257 objPtr->typePtr->freeIntRepProc(objPtr);
258 }
259 indexRep = (IndexRep *) ckalloc(sizeof(IndexRep));
260 objPtr->internalRep.otherValuePtr = (VOID *) indexRep;
261 objPtr->typePtr = &tclIndexType;
262 }
263 indexRep->tablePtr = (VOID*) tablePtr;
264 indexRep->offset = offset;
265 indexRep->index = index;
266
267 *indexPtr = index;
268 return TCL_OK;
269
270 error:
271 if (interp != NULL) {
272 /*
273 * Produce a fancy error message.
274 */
275 int count;
276 resultPtr = Tcl_GetObjResult(interp);
277 Tcl_AppendStringsToObj(resultPtr,
278 (numAbbrev > 1) ? "ambiguous " : "bad ", msg, " \"",
279 key, "\": must be ", STRING_AT(tablePtr,offset,0), (char*)NULL);
280 for (entryPtr = NEXT_ENTRY(tablePtr, offset), count = 0;
281 *entryPtr != NULL;
282 entryPtr = NEXT_ENTRY(entryPtr, offset), count++) {
283 if (*NEXT_ENTRY(entryPtr, offset) == NULL) {
284 Tcl_AppendStringsToObj(resultPtr,
285 (count > 0) ? ", or " : " or ", *entryPtr,
286 (char *) NULL);
287 } else {
288 Tcl_AppendStringsToObj(resultPtr, ", ", *entryPtr,
289 (char *) NULL);
290 }
291 }
292 }
293 return TCL_ERROR;
294 }
295
296 /*
297 *----------------------------------------------------------------------
298 *
299 * SetIndexFromAny --
300 *
301 * This procedure is called to convert a Tcl object to index
302 * internal form. However, this doesn't make sense (need to have a
303 * table of keywords in order to do the conversion) so the
304 * procedure always generates an error.
305 *
306 * Results:
307 * The return value is always TCL_ERROR, and an error message is
308 * left in interp's result if interp isn't NULL.
309 *
310 * Side effects:
311 * None.
312 *
313 *----------------------------------------------------------------------
314 */
315
316 static int
317 SetIndexFromAny(interp, objPtr)
318 Tcl_Interp *interp; /* Used for error reporting if not NULL. */
319 register Tcl_Obj *objPtr; /* The object to convert. */
320 {
321 Tcl_AppendToObj(Tcl_GetObjResult(interp),
322 "can't convert value to index except via Tcl_GetIndexFromObj API",
323 -1);
324 return TCL_ERROR;
325 }
326
327 /*
328 *----------------------------------------------------------------------
329 *
330 * UpdateStringOfIndex --
331 *
332 * This procedure is called to convert a Tcl object from index
333 * internal form to its string form. No abbreviation is ever
334 * generated.
335 *
336 * Results:
337 * None.
338 *
339 * Side effects:
340 * The string representation of the object is updated.
341 *
342 *----------------------------------------------------------------------
343 */
344
345 static void
346 UpdateStringOfIndex(objPtr)
347 Tcl_Obj *objPtr;
348 {
349 IndexRep *indexRep = (IndexRep *) objPtr->internalRep.otherValuePtr;
350 register char *buf;
351 register unsigned len;
352 register CONST char *indexStr = EXPAND_OF(indexRep);
353
354 len = strlen(indexStr);
355 buf = (char *) ckalloc(len + 1);
356 memcpy(buf, indexStr, len+1);
357 objPtr->bytes = buf;
358 objPtr->length = len;
359 }
360
361 /*
362 *----------------------------------------------------------------------
363 *
364 * DupIndex --
365 *
366 * This procedure is called to copy the internal rep of an index
367 * Tcl object from to another object.
368 *
369 * Results:
370 * None.
371 *
372 * Side effects:
373 * The internal representation of the target object is updated
374 * and the type is set.
375 *
376 *----------------------------------------------------------------------
377 */
378
379 static void
380 DupIndex(srcPtr, dupPtr)
381 Tcl_Obj *srcPtr, *dupPtr;
382 {
383 IndexRep *srcIndexRep = (IndexRep *) srcPtr->internalRep.otherValuePtr;
384 IndexRep *dupIndexRep = (IndexRep *) ckalloc(sizeof(IndexRep));
385
386 memcpy(dupIndexRep, srcIndexRep, sizeof(IndexRep));
387 dupPtr->internalRep.otherValuePtr = (VOID *) dupIndexRep;
388 dupPtr->typePtr = &tclIndexType;
389 }
390
391 /*
392 *----------------------------------------------------------------------
393 *
394 * FreeIndex --
395 *
396 * This procedure is called to delete the internal rep of an index
397 * Tcl object.
398 *
399 * Results:
400 * None.
401 *
402 * Side effects:
403 * The internal representation of the target object is deleted.
404 *
405 *----------------------------------------------------------------------
406 */
407
408 static void
409 FreeIndex(objPtr)
410 Tcl_Obj *objPtr;
411 {
412 ckfree((char *) objPtr->internalRep.otherValuePtr);
413 }
414
415 /*
416 *----------------------------------------------------------------------
417 *
418 * Tcl_WrongNumArgs --
419 *
420 * This procedure generates a "wrong # args" error message in an
421 * interpreter. It is used as a utility function by many command
422 * procedures.
423 *
424 * Results:
425 * None.
426 *
427 * Side effects:
428 * An error message is generated in interp's result object to
429 * indicate that a command was invoked with the wrong number of
430 * arguments. The message has the form
431 * wrong # args: should be "foo bar additional stuff"
432 * where "foo" and "bar" are the initial objects in objv (objc
433 * determines how many of these are printed) and "additional stuff"
434 * is the contents of the message argument.
435 *
436 *----------------------------------------------------------------------
437 */
438
439 void
440 Tcl_WrongNumArgs(interp, objc, objv, message)
441 Tcl_Interp *interp; /* Current interpreter. */
442 int objc; /* Number of arguments to print
443 * from objv. */
444 Tcl_Obj *CONST objv[]; /* Initial argument objects, which
445 * should be included in the error
446 * message. */
447 CONST char *message; /* Error message to print after the
448 * leading objects in objv. The
449 * message may be NULL. */
450 {
451 Tcl_Obj *objPtr;
452 int i;
453 register IndexRep *indexRep;
454
455 objPtr = Tcl_GetObjResult(interp);
456 Tcl_AppendToObj(objPtr, "wrong # args: should be \"", -1);
457 for (i = 0; i < objc; i++) {
458 /*
459 * If the object is an index type use the index table which allows
460 * for the correct error message even if the subcommand was
461 * abbreviated. Otherwise, just use the string rep.
462 */
463
464 if (objv[i]->typePtr == &tclIndexType) {
465 indexRep = (IndexRep *) objv[i]->internalRep.otherValuePtr;
466 Tcl_AppendStringsToObj(objPtr, EXPAND_OF(indexRep), (char *) NULL);
467 } else {
468 Tcl_AppendStringsToObj(objPtr, Tcl_GetString(objv[i]),
469 (char *) NULL);
470 }
471
472 /*
473 * Append a space character (" ") if there is more text to follow
474 * (either another element from objv, or the message string).
475 */
476 if ((i < (objc - 1)) || message) {
477 Tcl_AppendStringsToObj(objPtr, " ", (char *) NULL);
478 }
479 }
480
481 if (message) {
482 Tcl_AppendStringsToObj(objPtr, message, (char *) NULL);
483 }
484 Tcl_AppendStringsToObj(objPtr, "\"", (char *) NULL);
485 }
486