Debian Codegraph @

generic/bltTupleCmd.c

raw symbols SCIP
1
2 /*
3 *
4 * bltTupleCmd.c --
5 *
6 * Copyright 1998-1999 Lucent Technologies, Inc.
7 *
8 * Permission to use, copy, modify, and distribute this software and
9 * its documentation for any purpose and without fee is hereby
10 * granted, provided that the above copyright notice appear in all
11 * copies and that both that the copyright notice and warranty
12 * disclaimer appear in supporting documentation, and that the names
13 * of Lucent Technologies or any of their entities not be used in
14 * advertising or publicity pertaining to distribution of the software
15 * without specific, written prior permission.
16 *
17 * Lucent Technologies disclaims all warranties with regard to this
18 * software, including all implied warranties of merchantability and
19 * fitness. In no event shall Lucent Technologies be liable for any
20 * special, indirect or consequential damages or any damages
21 * whatsoever resulting from loss of use, data or profits, whether in
22 * an action of contract, negligence or other tortuous action, arising
23 * out of or in connection with the use or performance of this
24 * software.
25 *
26 * The "tuple" data object was created by George A. Howlett.
27 */
28
29 /*
30 tuple create t0 t1 t2
31 tuple names
32 t0 destroy
33 -or-
34 tuple destroy t0
35 tuple copy tuple@node tuple@node -recurse -tags
36
37 tuple move node after|before|into t2@node
38
39 $t apply -recurse $root command arg arg
40
41 $t attach tuplename
42
43 $t children $n
44 t0 copy node1 node2 node3 node4 node5 destName
45 $t delete $n...
46 $t delete 0
47 $t delete 0 10
48 $t delete all
49 $t depth $n
50 $t dump $root
51 $t dumpfile $root fileName
52 $t dup $t2
53 $t find $root -name pat -name pattern
54 $t firstchild $n
55 $t get $n $key
56 $t get $n $key(abc)
57 $t index $n
58 $t insert $parent $switches?
59 $t isancestor $n1 $n2
60 $t isbefore $n1 $n2
61 $t isleaf $n
62 $t lastchild $n
63 $t move $n1 after|before|into $n2
64 $t next $n
65 $t nextsibling $n
66 $t path $n1 $n2 $n3...
67 $t parent $n
68 $t previous $n
69 $t prevsibling $n
70 $t restore $root data -overwrite
71 $t root ?$n?
72
73 $t set $n $key $value ?$key $value?
74 $t size $n
75 $t slink $n $t2@$node ???
76 $t sort -recurse $root
77
78 $t tag delete tag1 tag2 tag3...
79 $t tag names
80 $t tag nodes $tag
81 $t tag set $n tag1 tag2 tag3...
82 $t tag unset $n tag1 tag2 tag3...
83
84 $t trace create $n $key how command
85 $t trace delete id1 id2 id3...
86 $t trace names
87 $t trace info $id
88
89 $t unset $n key1 key2 key3...
90
91 $t notify create -oncreate -ondelete -onmove command
92 $t notify create -oncreate -ondelete -onmove -onsort command arg arg arg
93 $t notify delete id1 id2 id3
94 $t notify names
95 $t notify info id
96
97 for { set n [$t firstchild $node] } { $n >= 0 } {
98 set n [$t nextsibling $n] } {
99 }
100 foreach n [$t children $node] {
101
102 }
103 set n [$t next $node]
104 set n [$t previous $node]
105
106 */
107
108 /*
109 * Rows as tokens.
110 *
111 */
112
113 #include <bltInt.h>
114
115 #ifndef NO_TUPLE
116
117 #include <bltTuple.h>
118 #include <bltHash.h>
119 #include <bltList.h>
120 #include "bltSwitch.h"
121 #include <ctype.h>
122
123 #define TUPLE_THREAD_KEY "BLT Tuple Command Data"
124 #define TUPLE_MAGIC ((unsigned int) 0x46170277)
125
126 enum TagTypes { TAG_TYPE_NONE, TAG_TYPE_ALL, TAG_TYPE_TAG };
127
128 typedef struct {
129 Blt_HashTable instTable; /* Hash table of tuples keyed by address. */
130 Tcl_Interp *interp;
131 } TupleCmdInterpData;
132
133 typedef struct {
134 Tcl_Interp *interp;
135 Blt_TupleTable table; /* Token representing the tuple
136 * table. */
137 Tcl_Command cmdToken; /* Token for tuple object's Tcl
138 * command. */
139
140 Tcl_Obj *emptyObjPtr; /* String representing an empty table
141 * value. */
142 Blt_HashEntry *hashPtr;
143 Blt_HashTable *instTablePtr;
144
145 int traceCounter; /* Used to generate trace id strings. */
146 Blt_HashTable traceTable; /* Table of active traces. Maps trace ids
147 * back to their TraceInfo records. */
148
149 int notifyCounter; /* Used to generate notify id strings. */
150 Blt_HashTable notifyTable; /* Table of event handlers. Maps notify ids
151 * back to their NotifyInfo records. */
152 } TupleCmd;
153
154 typedef struct {
155 Blt_TupleTrace trace;
156 TupleCmd *cmdPtr;
157 Tcl_Obj **objv;
158 int objc;
159 Blt_HashEntry *hashPtr;
160 } TraceInfo;
161
162 typedef struct {
163 Blt_TupleNotifier notifier;
164 TupleCmd *cmdPtr;
165 Tcl_Obj **objv;
166 int objc;
167 Blt_HashEntry *hashPtr;
168 } NotifyInfo;
169
170 typedef struct {
171 unsigned int column;
172 Tcl_Obj *objPtr;
173 } KeyValue;
174
175 typedef struct {
176 int mask;
177 } NotifyData;
178
179 static Blt_SwitchSpec notifySwitches[] =
180 {
181 {BLT_SWITCH_FLAG, "-create", Blt_Offset(NotifyData, mask), 0, 0,
182 TUPLE_NOTIFY_CREATE},
183 {BLT_SWITCH_FLAG, "-createrow", Blt_Offset(NotifyData, mask), 0, 0,
184 TUPLE_NOTIFY_CREATE_ROW},
185 {BLT_SWITCH_FLAG, "-createcolumn", Blt_Offset(NotifyData, mask), 0, 0,
186 TUPLE_NOTIFY_CREATE_COLUMN},
187 {BLT_SWITCH_FLAG, "-delete", Blt_Offset(NotifyData, mask), 0, 0,
188 TUPLE_NOTIFY_DELETE},
189 {BLT_SWITCH_FLAG, "-deleterow", Blt_Offset(NotifyData, mask), 0, 0,
190 TUPLE_NOTIFY_DELETE_ROW},
191 {BLT_SWITCH_FLAG, "-deletecolumn", Blt_Offset(NotifyData, mask), 0, 0,
192 TUPLE_NOTIFY_DELETE_COLUMN},
193 {BLT_SWITCH_FLAG, "-move", Blt_Offset(NotifyData, mask), 0, 0,
194 TUPLE_NOTIFY_MOVE},
195 {BLT_SWITCH_FLAG, "-sort", Blt_Offset(NotifyData, mask), 0, 0,
196 TUPLE_NOTIFY_SORT},
197 {BLT_SWITCH_FLAG, "-relabel", Blt_Offset(NotifyData, mask), 0, 0,
198 TUPLE_NOTIFY_RELABEL},
199 {BLT_SWITCH_FLAG, "-allevents", Blt_Offset(NotifyData, mask), 0, 0,
200 TUPLE_NOTIFY_ALL},
201 {BLT_SWITCH_FLAG, "-whenidle", Blt_Offset(NotifyData, mask), 0, 0,
202 TUPLE_NOTIFY_WHENIDLE},
203 {BLT_SWITCH_END, NULL, 0, 0}
204 };
205
206 #ifdef notdef
207
208 static Blt_SwitchParseProc StringToChild;
209 #define INSERT_BEFORE (ClientData)0
210 #define INSERT_AFTER (ClientData)1
211 static Blt_SwitchCustom beforeSwitch =
212 {
213 StringToChild, (Blt_SwitchFreeProc *)NULL, INSERT_BEFORE,
214 };
215 static Blt_SwitchCustom afterSwitch =
216 {
217 StringToChild, (Blt_SwitchFreeProc *)NULL, INSERT_AFTER,
218 };
219
220 typedef struct {
221 char *label;
222 int insertPos;
223 int inode;
224 char **tags;
225 char **dataPairs;
226 Blt_TupleNode parent;
227 } InsertData;
228
229 static Blt_SwitchSpec insertSwitches[] =
230 {
231 {BLT_SWITCH_CUSTOM, "-after", Blt_Offset(InsertData, insertPos), 0,
232 &afterSwitch},
233 {BLT_SWITCH_INT_NONNEGATIVE, "-at", Blt_Offset(InsertData, insertPos), 0},
234 {BLT_SWITCH_CUSTOM, "-before", Blt_Offset(InsertData, insertPos), 0,
235 &beforeSwitch},
236 {BLT_SWITCH_LIST, "-data", Blt_Offset(InsertData, dataPairs), 0},
237 {BLT_SWITCH_STRING, "-label", Blt_Offset(InsertData, label), 0},
238 {BLT_SWITCH_INT_NONNEGATIVE, "-node", Blt_Offset(InsertData, inode), 0},
239 {BLT_SWITCH_LIST, "-tags", Blt_Offset(InsertData, tags), 0},
240 {BLT_SWITCH_END, NULL, 0, 0}
241 };
242
243 #define PATTERN_EXACT (1)
244 #define PATTERN_GLOB (2)
245 #define PATTERN_MASK (0x3)
246 #define PATTERN_NONE (0)
247 #define PATTERN_REGEXP (3)
248 #define MATCH_INVERT (1<<8)
249 #define MATCH_LEAFONLY (1<<4)
250 #define MATCH_NOCASE (1<<5)
251 #define MATCH_PATHNAME (1<<6)
252
253 typedef struct {
254 TupleCmd *cmdPtr; /* Tuple to examine. */
255 Tcl_Obj *listObjPtr; /* List to accumulate the indices of
256 * matching nodes. */
257 Tcl_Obj **objv; /* Command converted into an array of
258 * Tcl_Obj's. */
259 int objc; /* Number of Tcl_Objs in above array. */
260
261 int nMatches; /* Current number of matches. */
262
263 unsigned int flags; /* See flags definitions above. */
264
265 /* Integer options. */
266 int maxMatches; /* If > 0, stop after this many matches. */
267 int maxDepth; /* If > 0, don't descend more than this
268 * many levels. */
269 int order; /* Order of search: Can be either
270 * TUPLE_PREORDER, TUPLE_POSTORDER,
271 * TUPLE_INORDER, TUPLE_BREADTHFIRST. */
272 /* String options. */
273 Blt_List patternList; /* List of patterns to compare with labels
274 * or values. */
275 char *addTag; /* If non-NULL, tag added to selected nodes. */
276
277 char **command; /* Command split into a Tcl list. */
278
279 Blt_List keyList; /* List of key name patterns. */
280 char *withTag;
281
282 } FindData;
283
284 static Blt_SwitchParseProc StringToOrder;
285 static Blt_SwitchCustom orderSwitch =
286 {
287 StringToOrder, (Blt_SwitchFreeProc *)NULL, (ClientData)0,
288 };
289
290 static Blt_SwitchParseProc StringToPattern;
291 static Blt_SwitchFreeProc FreePatterns;
292
293 static Blt_SwitchCustom regexpSwitch =
294 {
295 StringToPattern, FreePatterns, (ClientData)PATTERN_REGEXP,
296 };
297 static Blt_SwitchCustom globSwitch =
298 {
299 StringToPattern, FreePatterns, (ClientData)PATTERN_GLOB,
300 };
301 static Blt_SwitchCustom exactSwitch =
302 {
303 StringToPattern, FreePatterns, (ClientData)PATTERN_EXACT,
304 };
305
306
307 static Blt_SwitchSpec findSwitches[] =
308 {
309 {BLT_SWITCH_STRING, "-addtag", Blt_Offset(FindData, addTag), 0},
310 {BLT_SWITCH_INT_NONNEGATIVE, "-count", Blt_Offset(FindData, maxMatches), 0},
311 {BLT_SWITCH_INT_NONNEGATIVE, "-depth", Blt_Offset(FindData, maxDepth), 0},
312 {BLT_SWITCH_CUSTOM, "-exact", Blt_Offset(FindData, patternList), 0,
313 &exactSwitch},
314 {BLT_SWITCH_LIST, "-exec", Blt_Offset(FindData, command), 0},
315 {BLT_SWITCH_CUSTOM, "-glob", Blt_Offset(FindData, patternList), 0,
316 &globSwitch},
317 {BLT_SWITCH_FLAG, "-invert", Blt_Offset(FindData, flags), 0, 0,
318 MATCH_INVERT},
319 {BLT_SWITCH_CUSTOM, "-key", Blt_Offset(FindData, keyList), 0, &exactSwitch},
320 {BLT_SWITCH_CUSTOM, "-keyexact", Blt_Offset(FindData, keyList), 0,
321 &exactSwitch},
322 {BLT_SWITCH_CUSTOM, "-keyglob", Blt_Offset(FindData, keyList), 0,
323 &globSwitch},
324 {BLT_SWITCH_CUSTOM, "-keyregexp", Blt_Offset(FindData, keyList), 0,
325 &regexpSwitch},
326 {BLT_SWITCH_FLAG, "-leafonly", Blt_Offset(FindData, flags), 0, 0,
327 MATCH_LEAFONLY},
328 {BLT_SWITCH_FLAG, "-nocase", Blt_Offset(FindData, flags), 0, 0,
329 MATCH_NOCASE},
330 {BLT_SWITCH_CUSTOM, "-order", Blt_Offset(FindData, order), 0, &orderSwitch},
331 {BLT_SWITCH_FLAG, "-path", Blt_Offset(FindData, flags), 0, 0,
332 MATCH_PATHNAME},
333 {BLT_SWITCH_CUSTOM, "-regexp", Blt_Offset(FindData, patternList), 0,
334 &regexpSwitch},
335 {BLT_SWITCH_STRING, "-tag", Blt_Offset(FindData, withTag), 0},
336 {BLT_SWITCH_END, NULL, 0, 0}
337 };
338
339 static Blt_SwitchParseProc StringToNode;
340 static Blt_SwitchCustom nodeSwitch =
341 {
342 StringToNode, (Blt_SwitchFreeProc *)NULL, (ClientData)0,
343 };
344
345 typedef struct {
346 TupleCmd *cmdPtr; /* Tuple to move nodes. */
347 Blt_TupleNode node;
348 int movePos;
349 } MoveData;
350
351 static Blt_SwitchSpec moveSwitches[] =
352 {
353 {BLT_SWITCH_CUSTOM, "-after", Blt_Offset(MoveData, node), 0, &nodeSwitch},
354 {BLT_SWITCH_INT_NONNEGATIVE, "-at", Blt_Offset(MoveData, movePos), 0},
355 {BLT_SWITCH_CUSTOM, "-before", Blt_Offset(MoveData, node), 0, &nodeSwitch},
356 {BLT_SWITCH_END, NULL, 0, 0}
357 };
358
359 typedef struct {
360 Blt_TupleNode srcNode, destNode;
361 Blt_Tuple srcTuple, destTuple;
362 TupleCmd *srcPtr, *destPtr;
363 unsigned int flags;
364 char *label;
365 } CopyData;
366
367 #define COPY_RECURSE (1<<0)
368 #define COPY_TAGS (1<<1)
369 #define COPY_OVERWRITE (1<<2)
370
371 static Blt_SwitchSpec copySwitches[] =
372 {
373 {BLT_SWITCH_STRING, "-label", Blt_Offset(CopyData, label), 0},
374 {BLT_SWITCH_FLAG, "-recurse", Blt_Offset(CopyData, flags), 0, 0,
375 COPY_RECURSE},
376 {BLT_SWITCH_FLAG, "-tags", Blt_Offset(CopyData, flags), 0, 0,
377 COPY_TAGS},
378 {BLT_SWITCH_FLAG, "-overwrite", Blt_Offset(CopyData, flags), 0, 0,
379 COPY_OVERWRITE},
380 {BLT_SWITCH_END, NULL, 0, 0}
381 };
382
383 typedef struct {
384 TupleCmd *cmdPtr; /* Tuple to examine. */
385 Tcl_Obj **preObjv; /* Command converted into an array of
386 * Tcl_Obj's. */
387 int preObjc; /* Number of Tcl_Objs in above array. */
388
389 Tcl_Obj **postObjv; /* Command converted into an array of
390 * Tcl_Obj's. */
391 int postObjc; /* Number of Tcl_Objs in above array. */
392
393 unsigned int flags; /* See flags definitions above. */
394
395 int maxDepth; /* If > 0, don't descend more than this
396 * many levels. */
397 /* String options. */
398 Blt_List patternList; /* List of label or value patterns. */
399 char **preCmd; /* Pre-command split into a Tcl list. */
400 char **postCmd; /* Post-command split into a Tcl list. */
401
402 Blt_List keyList; /* List of key-name patterns. */
403 char *withTag;
404 } ApplyData;
405
406 static Blt_SwitchSpec applySwitches[] =
407 {
408 {BLT_SWITCH_LIST, "-precommand", Blt_Offset(ApplyData, preCmd), 0},
409 {BLT_SWITCH_LIST, "-postcommand", Blt_Offset(ApplyData, postCmd), 0},
410 {BLT_SWITCH_INT_NONNEGATIVE, "-depth", Blt_Offset(ApplyData, maxDepth), 0},
411 {BLT_SWITCH_CUSTOM, "-exact", Blt_Offset(ApplyData, patternList), 0,
412 &exactSwitch},
413 {BLT_SWITCH_CUSTOM, "-glob", Blt_Offset(ApplyData, patternList), 0,
414 &globSwitch},
415 {BLT_SWITCH_FLAG, "-invert", Blt_Offset(ApplyData, flags), 0, 0,
416 MATCH_INVERT},
417 {BLT_SWITCH_CUSTOM, "-key", Blt_Offset(ApplyData, keyList), 0,
418 &exactSwitch},
419 {BLT_SWITCH_CUSTOM, "-keyexact", Blt_Offset(ApplyData, keyList), 0,
420 &exactSwitch},
421 {BLT_SWITCH_CUSTOM, "-keyglob", Blt_Offset(ApplyData, keyList), 0,
422 &globSwitch},
423 {BLT_SWITCH_CUSTOM, "-keyregexp", Blt_Offset(ApplyData, keyList), 0,
424 &regexpSwitch},
425 {BLT_SWITCH_FLAG, "-leafonly", Blt_Offset(ApplyData, flags), 0, 0,
426 MATCH_LEAFONLY},
427 {BLT_SWITCH_FLAG, "-nocase", Blt_Offset(ApplyData, flags), 0, 0,
428 MATCH_NOCASE},
429 {BLT_SWITCH_FLAG, "-path", Blt_Offset(ApplyData, flags), 0, 0,
430 MATCH_PATHNAME},
431 {BLT_SWITCH_CUSTOM, "-regexp", Blt_Offset(ApplyData, patternList), 0,
432 &regexpSwitch},
433 {BLT_SWITCH_STRING, "-tag", Blt_Offset(ApplyData, withTag), 0},
434 {BLT_SWITCH_END, NULL, 0, 0}
435 };
436
437 typedef struct {
438 unsigned int flags;
439 Blt_HashTable idTable;
440 } RestoreData;
441
442 #define RESTORE_NO_TAGS (1<<0)
443 #define RESTORE_OVERWRITE (1<<1)
444
445 static Blt_SwitchSpec restoreSwitches[] =
446 {
447 {BLT_SWITCH_FLAG, "-notags", Blt_Offset(RestoreData, flags), 0, 0,
448 RESTORE_NO_TAGS},
449 {BLT_SWITCH_FLAG, "-overwrite", Blt_Offset(RestoreData, flags), 0, 0,
450 RESTORE_OVERWRITE},
451 {BLT_SWITCH_END, NULL, 0, 0}
452 };
453
454 static Blt_SwitchParseProc StringToFormat;
455 static Blt_SwitchCustom formatSwitch =
456 {
457 StringToFormat, (Blt_SwitchFreeProc *)NULL, (ClientData)0,
458 };
459
460 typedef struct {
461 int sort; /* If non-zero, sort the tuples. */
462 int withParent; /* If non-zero, add the parent node id
463 * to the output of the command.*/
464 int withId; /* If non-zero, echo the node id in the
465 * output of the command. */
466 } PositionData;
467
468 #define POSITION_SORTED (1<<0)
469
470 static Blt_SwitchSpec positionSwitches[] =
471 {
472 {BLT_SWITCH_FLAG, "-sort", Blt_Offset(PositionData, sort), 0, 0,
473 POSITION_SORTED},
474 {BLT_SWITCH_CUSTOM, "-format", 0, 0, &formatSwitch},
475 {BLT_SWITCH_END, NULL, 0, 0}
476 };
477
478 static Blt_TupleApplyProc MatchTupleProc, SortTupleProc;
479 static Blt_TupleApplyProc ApplyTupleProc;
480 static Blt_TupleTraceProc TupleTraceProc;
481 static Blt_TupleCompareTuplesProc CompareTuples;
482
483 static Tcl_ObjCmdProc CompareDictionaryCmd;
484 static Tcl_ObjCmdProc ExitCmd;
485 #endif
486
487 static Blt_TupleNotifyEventProc TupleEventProc;
488 static Tcl_CmdDeleteProc TupleInstDeleteProc;
489 static Tcl_InterpDeleteProc TupleInterpDeleteProc;
490 static Tcl_ObjCmdProc TupleInstObjCmd;
491 static Tcl_ObjCmdProc TupleObjCmd;
492
493 static int GetTuple(TupleCmd *cmdPtr, Tcl_Obj *objPtr, Blt_Tuple *tuplePtr);
494
495
496 /*
497 *----------------------------------------------------------------------
498 *
499 * NewTupleCmd --
500 *
501 * This is a helper routine used by TupleCreateOp. It create a
502 * new instance of a tuple command. Memory is allocated for the
503 * command structure and a new Tcl command is created (same as
504 * the instance name). All tuple commands have hash table
505 * entries in a global (interpreter-specific) registry.
506 *
507 * Results:
508 * Returns a pointer to the newly allocated tuple command structure.
509 *
510 * Side Effects:
511 * Memory is allocated for the structure and a hash table entry is
512 * added.
513 *
514 *----------------------------------------------------------------------
515 */
516 static TupleCmd *
517 NewTupleCmd(
518 Tcl_Interp *interp,
519 TupleCmdInterpData *interpDataPtr,
520 Blt_TupleTable table,
521 CONST char *instName)
522 {
523 int isNew;
524 TupleCmd *cmdPtr;
525
526 cmdPtr = Blt_Calloc(1, sizeof(TupleCmd));
527 assert(cmdPtr);
528 cmdPtr->table = table;
529 cmdPtr->interp = interp;
530
531 Blt_InitHashTable(&(cmdPtr->traceTable), BLT_STRING_KEYS);
532 Blt_InitHashTable(&(cmdPtr->notifyTable), BLT_STRING_KEYS);
533 cmdPtr->cmdToken = Tcl_CreateObjCommand(interp, (char *)instName,
534 (Tcl_ObjCmdProc *)TupleInstObjCmd, cmdPtr, TupleInstDeleteProc);
535 cmdPtr->instTablePtr = &interpDataPtr->instTable;
536 cmdPtr->hashPtr = Blt_CreateHashEntry(cmdPtr->instTablePtr, (char *)cmdPtr,
537 &isNew);
538 cmdPtr->emptyObjPtr = Tcl_NewStringObj("NA", -1);
539 Tcl_IncrRefCount(cmdPtr->emptyObjPtr);
540 Blt_SetHashValue(cmdPtr->hashPtr, cmdPtr);
541 return cmdPtr;
542 }
543
544
545 /*
546 *----------------------------------------------------------------------
547 *
548 * GetTupleCmdInterpData --
549 *
550 *----------------------------------------------------------------------
551 */
552 static TupleCmdInterpData *
553 GetTupleCmdInterpData(Tcl_Interp *interp)
554 {
555 TupleCmdInterpData *dataPtr;
556 Tcl_InterpDeleteProc *proc;
557
558 dataPtr = (TupleCmdInterpData *)
559 Tcl_GetAssocData(interp, TUPLE_THREAD_KEY, &proc);
560 if (dataPtr == NULL) {
561 dataPtr = Blt_Malloc(sizeof(TupleCmdInterpData));
562 assert(dataPtr);
563 dataPtr->interp = interp;
564 Tcl_SetAssocData(interp, TUPLE_THREAD_KEY, TupleInterpDeleteProc,
565 dataPtr);
566 Blt_InitHashTable(&dataPtr->instTable, BLT_ONE_WORD_KEYS);
567 }
568 return dataPtr;
569 }
570
571
572 /*
573 * ----------------------------------------------------------------------
574 *
575 * GenerateName --
576 *
577 * Generates an unique tuple command name. Tuple names are in
578 * the form "tupleN", where N is a non-negative integer. Check
579 * each name generated to see if it is already a tuple. We want
580 * to recycle names if possible.
581 *
582 * Results:
583 * Returns the unique name. The string itself is stored in the
584 * dynamic string passed into the routine.
585 *
586 * ----------------------------------------------------------------------
587 */
588 static CONST char *
589 GenerateName(
590 Tcl_Interp *interp,
591 CONST char *prefix,
592 CONST char *suffix,
593 Tcl_DString *resultPtr)
594 {
595
596 int n;
597 Tcl_Namespace *nsPtr;
598 char string[200];
599 Tcl_CmdInfo cmdInfo;
600 Tcl_DString dString;
601 CONST char *instName, *name;
602
603 /*
604 * Parse the command and put back so that it's in a consistent
605 * format.
606 *
607 * t1 <current namespace>::t1
608 * n1::t1 <current namespace>::n1::t1
609 * ::t1 ::t1
610 * ::n1::t1 ::n1::t1
611 */
612 instName = NULL; /* Suppress compiler warning. */
613 for (n = 0; n < INT_MAX; n++) {
614 Tcl_DStringInit(&dString);
615 Tcl_DStringAppend(&dString, prefix, -1);
616 sprintf(string, "tuple%d", n);
617 Tcl_DStringAppend(&dString, string, -1);
618 Tcl_DStringAppend(&dString, suffix, -1);
619 instName = Tcl_DStringValue(&dString);
620 if (Blt_ParseQualifiedName(interp, instName, &nsPtr, &name)
621 != TCL_OK) {
622 Tcl_AppendResult(interp, "can't find namespace in \"", instName,
623 "\"", (char *)NULL);
624 return NULL;
625 }
626 if (nsPtr == NULL) {
627 nsPtr = Tcl_GetCurrentNamespace(interp);
628 }
629 instName = Blt_GetQualifiedName(nsPtr, name, resultPtr);
630 /*
631 * Check if the command already exists.
632 */
633 if (Tcl_GetCommandInfo(interp, (char *)instName, &cmdInfo)) {
634 continue;
635 }
636 if (!Blt_TupleTableExists(interp, instName)) {
637 /*
638 * We want the name of the tuple command and the underlying
639 * tuple object to be the same. Check that the free command
640 * name isn't an already a tuple object name.
641 */
642 break;
643 }
644 }
645 return instName;
646 }
647
648
649 /*
650 *----------------------------------------------------------------------
651 *
652 * GetTupleCmd --
653 *
654 * Find the tuple command associated with the Tcl command "string".
655 *
656 * We have to perform multiple lookups to get this right.
657 *
658 * The first step is to generate a canonical command name. If an
659 * unqualified command name (i.e. no namespace qualifier) is
660 * given, we should search first the current namespace and then
661 * the global one. Most Tcl commands (like Tcl_GetCmdInfo) look
662 * only at the global namespace.
663 *
664 * Next check if the string is
665 * a) a Tcl command and
666 * b) really is a command for a tuple object.
667 * Tcl_GetCommandInfo will get us the objClientData field that
668 * should be a cmdPtr. We verify that by searching our hashtable
669 * of cmdPtr addresses.
670 *
671 * Results:
672 * A pointer to the tuple command. If no associated tuple command
673 * can be found, NULL is returned. It's up to the calling routines
674 * to generate an error message.
675 *
676 *----------------------------------------------------------------------
677 */
678 static TupleCmd *
679 GetTupleCmd(
680 TupleCmdInterpData *interpDataPtr,
681 Tcl_Interp *interp,
682 CONST char *string)
683 {
684 CONST char *name;
685 Tcl_Namespace *nsPtr;
686 Tcl_CmdInfo cmdInfo;
687 Blt_HashEntry *hPtr;
688 Tcl_DString dString;
689 char *qualName;
690 int result;
691
692 /* Put apart the tuple name and put is back together in a standard
693 * format. */
694 if (Blt_ParseQualifiedName(interp, string, &nsPtr, &name) != TCL_OK) {
695 return NULL; /* No such parent namespace. */
696 }
697 if (nsPtr == NULL) {
698 nsPtr = Tcl_GetCurrentNamespace(interp);
699 }
700 /* Rebuild the fully qualified name. */
701 qualName = Blt_GetQualifiedName(nsPtr, name, &dString);
702 result = Tcl_GetCommandInfo(interp, qualName, &cmdInfo);
703 Tcl_DStringFree(&dString);
704
705 if (!result) {
706 return NULL;
707 }
708 hPtr = Blt_FindHashEntry(&interpDataPtr->instTable,
709 (char *)(cmdInfo.objClientData));
710 if (hPtr == NULL) {
711 return NULL;
712 }
713 return Blt_GetHashValue(hPtr);
714 }
715
716
717 /*
718 *----------------------------------------------------------------------
719 *
720 * GetTraceFlags --
721 *
722 * Parses a string representation of the trace bit flags and
723 * returns the mask.
724 *
725 * Results:
726 * The trace mask is returned.
727 *
728 *----------------------------------------------------------------------
729 */
730 static int
731 GetTraceFlags(char *string)
732 {
733 register char *p;
734 unsigned int flags;
735
736 flags = 0;
737 for (p = string; *p != '\0'; p++) {
738 switch (toupper(*p)) {
739 case 'R':
740 flags |= TUPLE_TRACE_READ;
741 break;
742 case 'W':
743 flags |= TUPLE_TRACE_WRITE;
744 break;
745 case 'U':
746 flags |= TUPLE_TRACE_UNSET;
747 break;
748 case 'C':
749 flags |= TUPLE_TRACE_CREATE;
750 break;
751 default:
752 return -1;
753 }
754 }
755 return flags;
756 }
757
758 /*
759 *----------------------------------------------------------------------
760 *
761 * PrintTraceFlags --
762 *
763 * Generates a string representation of the trace bit flags.
764 * It's assumed that the provided string is at least 5 bytes.
765 *
766 * Results:
767 * None.
768 *
769 * Side Effects:
770 * The bitflag information is written to the provided string.
771 *
772 *----------------------------------------------------------------------
773 */
774 static void
775 PrintTraceFlags(unsigned int flags, char *string)
776 {
777 register char *p;
778
779 p = string;
780 if (flags & TUPLE_TRACE_READ) {
781 *p++ = 'r';
782 }
783 if (flags & TUPLE_TRACE_WRITE) {
784 *p++ = 'w';
785 }
786 if (flags & TUPLE_TRACE_UNSET) {
787 *p++ = 'u';
788 }
789 if (flags & TUPLE_TRACE_CREATE) {
790 *p++ = 'c';
791 }
792 *p = '\0';
793 }
794
795 /*
796 *----------------------------------------------------------------------
797 *
798 * PrintTuple --
799 *
800 * Generates a string representation of a tuple in the provided
801 * dynamic string (the dstring is assumed to be previously
802 * initialized).
803 *
804 * Results:
805 * None.
806 *
807 * Side Effects:
808 * The tuple information is appended to the dynamic string.
809 *
810 *----------------------------------------------------------------------
811 */
812 static void
813 PrintTuple(
814 TupleCmd *cmdPtr,
815 Blt_Tuple tuple,
816 Tcl_DString *resultPtr)
817 {
818 /* Tuple index */
819 Tcl_DStringAppendElement(resultPtr, Blt_Itoa(Blt_TupleRowIndex(tuple)));
820
821 /* Key-value pairs */
822 {
823 int column, nColumns;
824 CONST char *key;
825 Tcl_Obj *valueObjPtr;
826
827 Tcl_DStringStartSublist(resultPtr);
828 nColumns = Blt_TupleTableWidth(cmdPtr->table);
829 for (column = 0; column < nColumns; column++) {
830 key = Blt_TupleGetColumnKey(cmdPtr->table, column);
831 if (Blt_TupleGetValue((Tcl_Interp *)NULL, cmdPtr->table, tuple,
832 key, &valueObjPtr) == TCL_OK) {
833 Tcl_DStringAppendElement(resultPtr, key);
834 if (valueObjPtr != NULL) {
835 Tcl_DStringAppendElement(resultPtr,
836 Tcl_GetString(valueObjPtr));
837 } else {
838 Tcl_DStringAppendElement(resultPtr, "NA");
839 }
840 }
841 }
842 Tcl_DStringEndSublist(resultPtr);
843 }
844
845
846 /* Tags */
847 {
848 Blt_HashEntry *hPtr;
849 Blt_HashSearch cursor;
850 Blt_TupleTagEntry *tPtr;
851
852 Tcl_DStringStartSublist(resultPtr);
853 for (hPtr = Blt_TupleFirstTag(cmdPtr->table, &cursor); hPtr != NULL;
854 hPtr = Blt_NextHashEntry(&cursor)) {
855 tPtr = Blt_GetHashValue(hPtr);
856 Tcl_DStringAppendElement(resultPtr, tPtr->tagName);
857 }
858 Tcl_DStringEndSublist(resultPtr);
859 }
860 Tcl_DStringAppend(resultPtr, "\n", -1);
861 }
862
863 /*
864 *----------------------------------------------------------------------
865 *
866 * UnsetValues --
867 *
868 * This is a helper routine used by UnsetOp. It unsets one or
869 * more key value pairs for a given tuple.
870 *
871 * Results:
872 * A standard Tcl result. If an error occurred when unsetting
873 * the new value TCL_ERROR is returned and an error message is
874 * left in the interpreter result.
875 *
876 *----------------------------------------------------------------------
877 */
878 static int
879 UnsetValues(
880 TupleCmd *cmdPtr,
881 Blt_Tuple tuple,
882 int objc,
883 Tcl_Obj *CONST *objv)
884 {
885 register int i;
886
887 for (i = 0; i < objc; i ++) {
888 if (Blt_TupleUnsetValue(cmdPtr->interp, cmdPtr->table, tuple,
889 Tcl_GetString(objv[i])) != TCL_OK) {
890 return TCL_ERROR;
891 }
892 }
893 return TCL_OK;
894 }
895
896 /*
897 *----------------------------------------------------------------------
898 *
899 * DeleteNotifier --
900 *
901 * This is a helper routine used to delete notifiers. It
902 * releases the Tcl_Objs used in the notification callback
903 * command and the actual tuple notifier. Memory for the
904 * notifier is also freed.
905 *
906 * Results:
907 * None.
908 *
909 * Side Effects:
910 * Memory is deallocated and the notitifer is no longer
911 * active.
912 *
913 *----------------------------------------------------------------------
914 */
915 static void
916 DeleteNotifier(NotifyInfo *notifyPtr)
917 {
918 int i;
919
920 for (i = 0; i < notifyPtr->objc; i++) {
921 Tcl_DecrRefCount(notifyPtr->objv[i]);
922 }
923 Blt_TupleDeleteNotifier(notifyPtr->notifier);
924 Blt_Free(notifyPtr->objv);
925 Blt_Free(notifyPtr);
926 }
927
928 /*
929 *----------------------------------------------------------------------
930 *
931 * DeleteTrace --
932 *
933 * This is a helper routine used to delete traces. It releases
934 * the Tcl_Objs used in the trace callback command and the actual
935 * tuple trace. Memory for the trace is also freed.
936 *
937 * Results:
938 * None.
939 *
940 * Side Effects:
941 * Memory is deallocated and the trace is no longer active.
942 *
943 *----------------------------------------------------------------------
944 */
945 static void
946 DeleteTrace(TraceInfo *tracePtr)
947 {
948 int i;
949
950 for(i = 0; i < tracePtr->objc; i++) {
951 Tcl_DecrRefCount(tracePtr->objv[i]);
952 }
953 Blt_TupleDeleteTrace(tracePtr->trace);
954 Blt_Free(tracePtr->objv);
955 Blt_Free(tracePtr);
956 }
957
958 /*
959 *----------------------------------------------------------------------
960 *
961 * TupleTraceProc --
962 *
963 *----------------------------------------------------------------------
964 */
965 /*ARGSUSED*/
966 static int
967 TupleTraceProc(
968 ClientData clientData,
969 Tcl_Interp *interp,
970 unsigned int row, /* Tuple that has just been updated. */
971 unsigned int column, /* Field that's updated. */
972 unsigned int flags)
973 {
974 TraceInfo *tracePtr = clientData;
975 char string[5];
976 CONST char *key;
977 int result;
978 int i;
979
980 i = tracePtr->objc; /* Add extra command arguments
981 * starting at this index. */
982 tracePtr->objv[i] = Tcl_NewIntObj(row);
983 key = Blt_TupleGetColumnKey(tracePtr->cmdPtr->table, column);
984 tracePtr->objv[i+1] = Tcl_NewStringObj(key, -1);
985 PrintTraceFlags(flags, string);
986 tracePtr->objv[i+2] = Tcl_NewStringObj(string, -1);
987 Tcl_IncrRefCount(tracePtr->objv[i]);
988 Tcl_IncrRefCount(tracePtr->objv[i+1]);
989 Tcl_IncrRefCount(tracePtr->objv[i+2]);
990 result = Tcl_EvalObjv(interp, tracePtr->objc + 3, tracePtr->objv, 0);
991 Tcl_DecrRefCount(tracePtr->objv[i+2]);
992 Tcl_DecrRefCount(tracePtr->objv[i+1]);
993 Tcl_DecrRefCount(tracePtr->objv[i]);
994 return result;
995 }
996
997 /*
998 *----------------------------------------------------------------------
999 *
1000 * TupleEventProc --
1001 *
1002 *----------------------------------------------------------------------
1003 */
1004 static int
1005 TupleEventProc(ClientData clientData, Blt_TupleNotifyEvent *eventPtr)
1006 {
1007 NotifyInfo *notifyPtr = clientData;
1008 int result;
1009 int i;
1010 char *string;
1011
1012 switch (eventPtr->type) {
1013 case TUPLE_NOTIFY_CREATE_ROW:
1014 string = "-createrow";
1015 break;
1016
1017 case TUPLE_NOTIFY_CREATE_COLUMN:
1018 string = "-createcolumn";
1019 break;
1020
1021 case TUPLE_NOTIFY_DELETE_ROW:
1022 string = "-deleterow";
1023 break;
1024
1025 case TUPLE_NOTIFY_DELETE_COLUMN:
1026 string = "-deletecolumn";
1027 break;
1028
1029 case TUPLE_NOTIFY_MOVE:
1030 string = "-move";
1031 break;
1032
1033 case TUPLE_NOTIFY_SORT:
1034 string = "-sort";
1035 break;
1036
1037 case TUPLE_NOTIFY_RELABEL:
1038 string = "-relabel";
1039 break;
1040
1041 default:
1042 /* empty */
1043 string = "???";
1044 break;
1045 }
1046
1047 i = notifyPtr->objc;
1048 notifyPtr->objv[i] = Tcl_NewStringObj(string, -1);
1049 notifyPtr->objv[i+1] = Tcl_NewIntObj(eventPtr->row);
1050 Tcl_IncrRefCount(notifyPtr->objv[i]);
1051 Tcl_IncrRefCount(notifyPtr->objv[i+1]);
1052 result = Tcl_EvalObjv(notifyPtr->cmdPtr->interp, notifyPtr->objc + 3,
1053 notifyPtr->objv, 0);
1054 Tcl_DecrRefCount(notifyPtr->objv[i+1]);
1055 Tcl_DecrRefCount(notifyPtr->objv[i]);
1056 if (result != TCL_OK) {
1057 Tcl_BackgroundError(notifyPtr->cmdPtr->interp);
1058 return TCL_ERROR;
1059 }
1060 Tcl_ResetResult(notifyPtr->cmdPtr->interp);
1061 return TCL_OK;
1062 }
1063
1064 /*
1065 *----------------------------------------------------------------------
1066 *
1067 * GetTuple --
1068 *
1069 *----------------------------------------------------------------------
1070 */
1071 static int
1072 GetTuple(TupleCmd *cmdPtr, Tcl_Obj *objPtr, Blt_Tuple *tuplePtr)
1073 {
1074 char c;
1075 Blt_Tuple tuple;
1076 char *string;
1077
1078 string = Tcl_GetString(objPtr);
1079 c = string[0];
1080
1081 if (isdigit(UCHAR(c))) {
1082 int row;
1083
1084 if (Tcl_GetIntFromObj(cmdPtr->interp, objPtr, &row) != TCL_OK) {
1085 return TCL_ERROR;
1086 }
1087 tuple = Blt_TupleGetTupleByIndex(cmdPtr->table, row);
1088 if (tuple != NULL) {
1089 *tuplePtr = tuple;
1090 return TCL_OK;
1091 }
1092 } else if (cmdPtr != NULL) {
1093 if (strcmp(string, "all") == 0) {
1094 if (Blt_TupleTableLength(cmdPtr->table) > 1) {
1095 Tcl_AppendResult(cmdPtr->interp,
1096 "more than one node tagged as \"", string, "\"",
1097 (char *)NULL);
1098 return TCL_ERROR;
1099 }
1100 tuple = Blt_TupleGetTupleByIndex(cmdPtr->table, 0);
1101 } else {
1102 Blt_HashTable *tablePtr;
1103 Blt_HashSearch cursor;
1104 Blt_HashEntry *hPtr;
1105 int result;
1106
1107 tuple = NULL;
1108 result = TCL_ERROR;
1109 tablePtr = Blt_TupleTagHashTable(cmdPtr->table, string);
1110 if (tablePtr == NULL) {
1111 Tcl_AppendResult(cmdPtr->interp, "can't find tag or index \"",
1112 string, "\" in ", Blt_TupleTableName(cmdPtr->table),
1113 (char *)NULL);
1114 } else if (tablePtr->numEntries > 1) {
1115 Tcl_AppendResult(cmdPtr->interp,
1116 "more than one node tagged as \"", string, "\"",
1117 (char *)NULL);
1118 } else if (tablePtr->numEntries > 0) {
1119 hPtr = Blt_FirstHashEntry(tablePtr, &cursor);
1120 tuple = Blt_GetHashValue(hPtr);
1121 result = TCL_OK;
1122 }
1123 if (result == TCL_ERROR) {
1124 return TCL_ERROR;
1125 }
1126 }
1127 if (tuple != NULL) {
1128 *tuplePtr = tuple;
1129 return TCL_OK;
1130 }
1131 }
1132 Tcl_AppendResult(cmdPtr->interp, "can't find tag or index \"", string,
1133 "\" in ", Blt_TupleTableName(cmdPtr->table), (char *)NULL);
1134 return TCL_ERROR;
1135 }
1136
1137 /*
1138 *----------------------------------------------------------------------
1139 *
1140 * AttachOp --
1141 *
1142 *----------------------------------------------------------------------
1143 */
1144 static int
1145 AttachOp(
1146 TupleCmd *cmdPtr,
1147 Tcl_Interp *interp,
1148 int objc,
1149 Tcl_Obj *CONST *objv)
1150 {
1151 if (objc == 3) {
1152 CONST char *objName;
1153 CONST char *name;
1154 Blt_TupleTable table;
1155 Tcl_Namespace *nsPtr;
1156 Tcl_DString dString;
1157 int result;
1158
1159 objName = Tcl_GetString(objv[2]);
1160 if (Blt_ParseQualifiedName(interp, objName, &nsPtr, &name)
1161 != TCL_OK) {
1162 Tcl_AppendResult(interp, "can't find namespace in \"", objName,
1163 "\"", (char *)NULL);
1164 return TCL_ERROR;
1165 }
1166 if (nsPtr == NULL) {
1167 nsPtr = Tcl_GetCurrentNamespace(interp);
1168 }
1169 objName = Blt_GetQualifiedName(nsPtr, name, &dString);
1170 result = Blt_TupleGetTable(interp, objName, &table);
1171 Tcl_DStringFree(&dString);
1172 if (result != TCL_OK) {
1173 return TCL_ERROR;
1174 }
1175 if (cmdPtr->table != NULL) {
1176 Blt_HashEntry *hPtr;
1177 Blt_HashSearch cursor;
1178 TraceInfo *tracePtr;
1179 NotifyInfo *notifyPtr;
1180
1181 Blt_TupleReleaseTable(cmdPtr->table);
1182 /* Dump the current set of tags, traces, and notifiers. */
1183 for (hPtr = Blt_FirstHashEntry(&cmdPtr->traceTable, &cursor);
1184 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
1185 tracePtr = Blt_GetHashValue(hPtr);
1186 DeleteTrace(tracePtr);
1187 }
1188 Blt_DeleteHashTable(&cmdPtr->traceTable);
1189 Blt_InitHashTable(&cmdPtr->traceTable, TCL_STRING_KEYS);
1190 for (hPtr = Blt_FirstHashEntry(&cmdPtr->notifyTable, &cursor);
1191 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
1192 notifyPtr = Blt_GetHashValue(hPtr);
1193 DeleteNotifier(notifyPtr);
1194 }
1195 Blt_DeleteHashTable(&cmdPtr->notifyTable);
1196 Blt_InitHashTable(&cmdPtr->notifyTable, TCL_STRING_KEYS);
1197 }
1198 cmdPtr->table = table;
1199 }
1200 Tcl_SetResult(interp, (char *)Blt_TupleTableName(cmdPtr->table),
1201 TCL_VOLATILE);
1202 return TCL_OK;
1203 }
1204
1205 /*
1206 *----------------------------------------------------------------------
1207 *
1208 * ColumnOp --
1209 *
1210 * Parses the given command line and calls one of several
1211 * column-specific operations.
1212 *
1213 * Results:
1214 * Returns a standard Tcl result. It is the result of
1215 * operation called.
1216 *
1217 *----------------------------------------------------------------------
1218 */
1219 static Blt_OpSpec columnOps[] =
1220 {
1221 #ifdef notdef
1222 {"create", 1, (Blt_Op)ColumnCreateOp, 4, 0, "name ?switches?",},
1223 {"delete", 1, (Blt_Op)ColumnDeleteOp, 3, 0, "name...",},
1224 {"get", 1, (Blt_Op)ColumnGetOp, 4, 0, "name...",},
1225 {"info", 1, (Blt_Op)ColumnInfoOp, 4, 4, "name",},
1226 {"names", 1, (Blt_Op)ColumnNamesOp, 3, 3, "",},
1227 {"set", 1, (Blt_Op)ColumnSetOp, 4, 0, "name values ?name values?...",},
1228 {"type", 1, (Blt_Op)ColumnTypeOp, 4, 0, "?newType?",},
1229 {"unset", 1, (Blt_Op)ColumnUnsetOp, 4, 0, "name...",},
1230 {"type", 1, (Blt_Op)ColumnTypeOp, 4, 0, "?newType?",},
1231 #endif
1232 };
1233
1234 static int nColumnOps = sizeof(columnOps) / sizeof(Blt_OpSpec);
1235
1236 static int
1237 ColumnOp(
1238 TupleCmd *cmdPtr,
1239 Tcl_Interp *interp,
1240 int objc,
1241 Tcl_Obj *CONST *objv)
1242 {
1243 Blt_Op proc;
1244 int result;
1245
1246 proc = Blt_GetOpFromObj(interp, nColumnOps, columnOps, BLT_OP_ARG2, objc,
1247 objv, 0);
1248 if (proc == NULL) {
1249 return TCL_ERROR;
1250 }
1251 result = (*proc) (cmdPtr, interp, objc, objv);
1252 return result;
1253 }
1254
1255 /*
1256 *----------------------------------------------------------------------
1257 *
1258 * DeleteOp --
1259 *
1260 * d0 delete tagOrIdx ?tagOrIdx...?
1261 *
1262 * Deletes one or more tuples from the table. Tuples may be
1263 * specified by their index or a tag.
1264 *
1265 *----------------------------------------------------------------------
1266 */
1267 static int
1268 DeleteOp(
1269 TupleCmd *cmdPtr,
1270 Tcl_Interp *interp,
1271 int objc,
1272 Tcl_Obj *CONST *objv)
1273 {
1274 Blt_Tuple tuple;
1275 Blt_TupleTagSearch cursor;
1276 int i;
1277
1278 Blt_TupleStartDeleteRows(cmdPtr->table);
1279 for (i = 2; i < objc; i++) {
1280 /* A tag may designate more than one row. */
1281 tuple = Blt_TupleFirstTagged(interp, cmdPtr->table, objv[i], &cursor);
1282 if (tuple == NULL) {
1283 Blt_TupleEndDeleteRows(cmdPtr->table);
1284 return TCL_ERROR;
1285 }
1286 for (/* empty */; tuple != NULL; tuple = Blt_TupleNextTagged(&cursor)) {
1287 Blt_TupleDeleteTuple(cmdPtr->table, tuple);
1288 }
1289 }
1290 Blt_TupleEndDeleteRows(cmdPtr->table);
1291 return TCL_OK;
1292 }
1293
1294 /*
1295 *----------------------------------------------------------------------
1296 *
1297 * ExistsOp --
1298 *
1299 *----------------------------------------------------------------------
1300 */
1301 static int
1302 ExistsOp(
1303 TupleCmd *cmdPtr,
1304 Tcl_Interp *interp,
1305 int objc,
1306 Tcl_Obj *CONST *objv)
1307 {
1308 Blt_Tuple tuple;
1309 int bool;
1310
1311 bool = TRUE;
1312 if (GetTuple(cmdPtr, objv[2], &tuple) != TCL_OK) {
1313 bool = FALSE;
1314 } else if (objc == 4) {
1315 Tcl_Obj *valueObjPtr;
1316 char *key;
1317
1318 key = Tcl_GetString(objv[3]);
1319 if (Blt_TupleGetValue((Tcl_Interp *)NULL, cmdPtr->table, tuple,
1320 key, &valueObjPtr) != TCL_OK) {
1321 bool = FALSE;
1322 }
1323 bool = (valueObjPtr != NULL);
1324 }
1325 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(bool));
1326 return TCL_OK;
1327 }
1328
1329 /*
1330 *----------------------------------------------------------------------
1331 *
1332 * GetOp --
1333 *
1334 * Retrieves the value from a given tuple for a designated
1335 * column. It's an error if the column key or row tuple is
1336 * invalid. If no key argument is provided then, all key-value
1337 * pairs in the tuple are returned. If a value is empty (the
1338 * Tcl_Obj value is NULL), then the tuple command's "empty"
1339 * string representation will be used.
1340 *
1341 * A third argument may be provided as the default return value.
1342 * If no value exists for the given key, then this value is
1343 * returned.
1344 *
1345 * Results:
1346 * A standard Tcl result. If the tag or index is invalid,
1347 * TCL_ERROR is returned and an error message is left in the
1348 * interpreter result.
1349 *
1350 *
1351 *----------------------------------------------------------------------
1352 */
1353 static int
1354 GetOp(
1355 TupleCmd *cmdPtr,
1356 Tcl_Interp *interp,
1357 int objc,
1358 Tcl_Obj *CONST *objv)
1359 {
1360 Blt_Tuple tuple;
1361
1362 if (GetTuple(cmdPtr, objv[2], &tuple) != TCL_OK) {
1363 return TCL_ERROR;
1364 }
1365 if (objc == 3) {
1366 int row, column;
1367 Tcl_Obj *valueObjPtr, *listObjPtr;
1368
1369 /* Return all the key-value pairs in the tuple. Ignore columns
1370 * with empty values. */
1371 listObjPtr = Tcl_NewListObj(0, (Tcl_Obj **) NULL);
1372 row = Blt_TupleRowIndex(tuple);
1373 for (column = 0; column < Blt_TupleTableWidth(cmdPtr->table); column++){
1374 if (Blt_TupleGetValueByIndex((Tcl_Interp *)NULL, cmdPtr->table,
1375 row, column, &valueObjPtr) == TCL_OK) {
1376 if (valueObjPtr != NULL) {
1377 Tcl_Obj *objPtr;
1378 CONST char *key;
1379
1380 key = Blt_TupleGetColumnKey(cmdPtr->table, column);
1381 objPtr = Tcl_NewStringObj(key, -1);
1382 Tcl_ListObjAppendElement(interp, listObjPtr, objPtr);
1383 Tcl_ListObjAppendElement(interp, listObjPtr, valueObjPtr);
1384 }
1385 }
1386 }
1387 Tcl_SetObjResult(interp, listObjPtr);
1388 return TCL_OK;
1389 } else {
1390 Tcl_Obj *valueObjPtr;
1391 char *key;
1392 int row, column;
1393
1394 row = Blt_TupleRowIndex(tuple);
1395
1396 key = Tcl_GetString(objv[3]);
1397 if (Blt_TupleGetColumnIndex(interp, cmdPtr->table, key, &column)
1398 != TCL_OK) {
1399 return TCL_ERROR;
1400 }
1401 if (Blt_TupleGetValueByIndex((Tcl_Interp *)NULL, cmdPtr->table, row,
1402 column, &valueObjPtr) != TCL_OK) {
1403 return TCL_ERROR;
1404 }
1405 if (valueObjPtr == NULL) {
1406 if (objc == 4) {
1407 Tcl_AppendResult(interp, "tuple "\", Tcl_GetString(objv[2]),
1408 "\" in table \"", Tcl_GetString(objv[0]),
1409 "\" is empty.", (char *)NULL);
1410 return TCL_ERROR;
1411 }
1412 valueObjPtr = objv[4];
1413 }
1414 Tcl_SetObjResult(interp, valueObjPtr);
1415 }
1416 return TCL_OK;
1417 }
1418
1419
1420 /*
1421 *----------------------------------------------------------------------
1422 *
1423 * NotifyCreateOp --
1424 *
1425 * Creates a notifier for this instance. Notifiers represent
1426 * a bitmask of events and a command prefix to be invoked
1427 * when a matching event occurs.
1428 *
1429 * The command prefix is parsed and saved in an array of
1430 * Tcl_Objs. Extra slots are allocated for the
1431 *
1432 * Results:
1433 * A standard Tcl result. The name of the new notifier is
1434 * returned in the interpreter result. Otherwise, if it failed
1435 * to parse a switch, then TCL_ERROR is returned and an error
1436 * message is left in the interpreter result.
1437 *
1438 * Example:
1439 * tuple0 notify create ?flags? command arg
1440 *
1441 *----------------------------------------------------------------------
1442 */
1443 static int
1444 NotifyCreateOp(
1445 TupleCmd *cmdPtr,
1446 Tcl_Interp *interp,
1447 int objc,
1448 Tcl_Obj *CONST *objv)
1449 {
1450 NotifyInfo *notifyPtr;
1451 NotifyData data;
1452 char *string;
1453 char idString[200];
1454 int isNew, nArgs;
1455 Blt_HashEntry *hPtr;
1456 int count;
1457 register int i;
1458
1459 count = 0;
1460 for (i = 3; i < objc; i++) {
1461 string = Tcl_GetString(objv[i]);
1462 if (string[0] != '-') {
1463 break;
1464 }
1465 count++;
1466 }
1467 data.mask = 0;
1468 /* Process switches */
1469 if (Blt_ProcessObjSwitches(interp, notifySwitches, count, objv + 3,
1470 (char *)&data, 0) < 0) {
1471 return TCL_ERROR;
1472 }
1473 notifyPtr = Blt_Malloc(sizeof(NotifyInfo));
1474
1475 nArgs = objc - i;
1476
1477 /* Stash away the command in structure and pass that to the notifier. */
1478 notifyPtr->objv = Blt_Malloc((nArgs + 2) * sizeof(Tcl_Obj *));
1479 for (count = 0; i < objc; i++, count++) {
1480 Tcl_IncrRefCount(objv[i]);
1481 notifyPtr->objv[count] = objv[i];
1482 }
1483 notifyPtr->objc = nArgs + 2;
1484 notifyPtr->cmdPtr = cmdPtr;
1485 if (data.mask == 0) {
1486 data.mask = TUPLE_NOTIFY_ALL;
1487 }
1488 sprintf(idString, "notify%d", cmdPtr->notifyCounter++);
1489 hPtr = Blt_CreateHashEntry(&cmdPtr->notifyTable, idString, &isNew);
1490 Blt_SetHashValue(hPtr, notifyPtr);
1491 notifyPtr->notifier = Blt_TupleCreateNotifier(cmdPtr->table,
1492 data.mask, TupleEventProc, notifyPtr);
1493 Tcl_SetStringObj(Tcl_GetObjResult(interp), idString, -1);
1494 return TCL_OK;
1495 }
1496
1497 /*
1498 *----------------------------------------------------------------------
1499 *
1500 * NotifyDeleteOp --
1501 *
1502 * Deletes one or more notifiers.
1503 *
1504 * Results:
1505 * A standard Tcl result. If a name given doesn't represent
1506 * a notifier, then TCL_ERROR is returned and an error message
1507 * is left in the interpreter result.
1508 *
1509 *----------------------------------------------------------------------
1510 */
1511 static int
1512 NotifyDeleteOp(
1513 TupleCmd *cmdPtr,
1514 Tcl_Interp *interp,
1515 int objc,
1516 Tcl_Obj *CONST *objv)
1517 {
1518 NotifyInfo *notifyPtr;
1519 Blt_HashEntry *hPtr;
1520
1521 register int i;
1522 char *string;
1523
1524 for (i = 3; i < objc; i++) {
1525 string = Tcl_GetString(objv[i]);
1526 hPtr = Blt_FindHashEntry(&(cmdPtr->notifyTable), string);
1527 if (hPtr == NULL) {
1528 Tcl_AppendResult(interp, "unknown notify name \"", string, "\"",
1529 (char *)NULL);
1530 return TCL_ERROR;
1531 }
1532 notifyPtr = Blt_GetHashValue(hPtr);
1533 Blt_DeleteHashEntry(&cmdPtr->notifyTable, hPtr);
1534 DeleteNotifier(notifyPtr);
1535 }
1536 return TCL_OK;
1537 }
1538
1539 /*
1540 *----------------------------------------------------------------------
1541 *
1542 * NotifyInfoOp --
1543 *
1544 * Returns the details for a given notifier. The name
1545 * of the notifier is passed as an argument. The details
1546 * are both the event mask and command prefix.
1547 *
1548 * Results:
1549 * A standard Tcl result. If the name given doesn't represent
1550 * a notifier, then TCL_ERROR is returned and an error message
1551 * is left in the interpreter result.
1552 *
1553 *----------------------------------------------------------------------
1554 */
1555 /*ARGSUSED*/
1556 static int
1557 NotifyInfoOp(
1558 TupleCmd *cmdPtr,
1559 Tcl_Interp *interp,
1560 int objc, /* Not used. */
1561 Tcl_Obj *CONST *objv)
1562 {
1563 NotifyInfo *notifyPtr;
1564 struct Blt_TupleNotifierStruct *nPtr;
1565 Blt_HashEntry *hPtr;
1566 Tcl_DString dString;
1567 char *string;
1568 int i;
1569
1570 string = Tcl_GetString(objv[3]);
1571 hPtr = Blt_FindHashEntry(&(cmdPtr->notifyTable), string);
1572 if (hPtr == NULL) {
1573 Tcl_AppendResult(interp, "unknown notify name \"", string, "\"",
1574 (char *)NULL);
1575 return TCL_ERROR;
1576 }
1577 notifyPtr = Blt_GetHashValue(hPtr);
1578
1579 Tcl_DStringInit(&dString);
1580 Tcl_DStringAppendElement(&dString, string); /* Copy notify Id */
1581 Tcl_DStringStartSublist(&dString);
1582 nPtr = notifyPtr->notifier;
1583 if (nPtr->mask & TUPLE_NOTIFY_CREATE) {
1584 Tcl_DStringAppendElement(&dString, "-create");
1585 }
1586 if (nPtr->mask & TUPLE_NOTIFY_DELETE) {
1587 Tcl_DStringAppendElement(&dString, "-delete");
1588 }
1589 if (nPtr->mask & TUPLE_NOTIFY_MOVE) {
1590 Tcl_DStringAppendElement(&dString, "-move");
1591 }
1592 if (nPtr->mask & TUPLE_NOTIFY_SORT) {
1593 Tcl_DStringAppendElement(&dString, "-sort");
1594 }
1595 if (nPtr->mask & TUPLE_NOTIFY_RELABEL) {
1596 Tcl_DStringAppendElement(&dString, "-relabel");
1597 }
1598 if (nPtr->mask & TUPLE_NOTIFY_WHENIDLE) {
1599 Tcl_DStringAppendElement(&dString, "-whenidle");
1600 }
1601 Tcl_DStringEndSublist(&dString);
1602 Tcl_DStringStartSublist(&dString);
1603 for (i = 0; i < notifyPtr->objc; i++) {
1604 Tcl_DStringAppendElement(&dString, Tcl_GetString(notifyPtr->objv[i]));
1605 }
1606 Tcl_DStringEndSublist(&dString);
1607 Tcl_DStringResult(interp, &dString);
1608 return TCL_OK;
1609 }
1610
1611 /*
1612 *----------------------------------------------------------------------
1613 *
1614 * NotifyNamesOp --
1615 *
1616 * Returns the names of all the notifiers in use by this
1617 * instance. Notifiers issues by other instances or object
1618 * clients are not reported.
1619 *
1620 * Results:
1621 * Always TCL_OK. A list of notifier names is left in the
1622 * interpreter result.
1623 *
1624 *----------------------------------------------------------------------
1625 */
1626 /*ARGSUSED*/
1627 static int
1628 NotifyNamesOp(
1629 TupleCmd *cmdPtr,
1630 Tcl_Interp *interp,
1631 int objc, /* Not used. */
1632 Tcl_Obj *CONST *objv)
1633 {
1634 Blt_HashEntry *hPtr;
1635 Blt_HashSearch cursor;
1636 Tcl_Obj *objPtr, *listObjPtr;
1637 char *idString;
1638
1639 listObjPtr = Tcl_NewListObj(0, (Tcl_Obj **) NULL);
1640 for (hPtr = Blt_FirstHashEntry(&(cmdPtr->notifyTable), &cursor);
1641 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
1642 idString = Blt_GetHashKey(&(cmdPtr->notifyTable), hPtr);
1643 objPtr = Tcl_NewStringObj(idString, -1);
1644 Tcl_ListObjAppendElement(interp, listObjPtr, objPtr);
1645 }
1646 Tcl_SetObjResult(interp, listObjPtr);
1647 return TCL_OK;
1648 }
1649
1650 /*
1651 *----------------------------------------------------------------------
1652 *
1653 * NotifyOp --
1654 *
1655 * Parses the given command line and calls one of several
1656 * notifier-specific operations.
1657 *
1658 * Results:
1659 * Returns a standard Tcl result. It is the result of
1660 * operation called.
1661 *
1662 *----------------------------------------------------------------------
1663 */
1664 static Blt_OpSpec notifyOps[] =
1665 {
1666 {"create", 1, (Blt_Op)NotifyCreateOp, 4, 0, "?flags? command",},
1667 {"delete", 1, (Blt_Op)NotifyDeleteOp, 3, 0, "notifyId...",},
1668 {"info", 1, (Blt_Op)NotifyInfoOp, 4, 4, "notifyId",},
1669 {"names", 1, (Blt_Op)NotifyNamesOp, 3, 3, "",},
1670 };
1671
1672 static int nNotifyOps = sizeof(notifyOps) / sizeof(Blt_OpSpec);
1673
1674 static int
1675 NotifyOp(
1676 TupleCmd *cmdPtr,
1677 Tcl_Interp *interp,
1678 int objc,
1679 Tcl_Obj *CONST *objv)
1680 {
1681 Blt_Op proc;
1682 int result;
1683
1684 proc = Blt_GetOpFromObj(interp, nNotifyOps, notifyOps, BLT_OP_ARG2, objc,
1685 objv, 0);
1686 if (proc == NULL) {
1687 return TCL_ERROR;
1688 }
1689 result = (*proc) (cmdPtr, interp, objc, objv);
1690 return result;
1691 }
1692
1693 /*
1694 *----------------------------------------------------------------------
1695 *
1696 * ColumnOp --
1697 *
1698 * Parses the given command line and calls one of several
1699 * column-specific operations.
1700 *
1701 * Results:
1702 * Returns a standard Tcl result. It is the result of
1703 * operation called.
1704 *
1705 *----------------------------------------------------------------------
1706 */
1707 static Blt_OpSpec columnOps[] =
1708 {
1709 #ifdef notdef
1710 {"create", 1, (Blt_Op)ColumnCreateOp, 4, 0, "name ?switches?",},
1711 {"cut", 1, (Blt_Op)ColumnDeleteOp, 3, 0, "name...",},
1712 {"dup", 1, (Blt_Op)ColumnGetOp, 4, 0, "name...",},
1713 {"pack", 1, (Blt_Op)ColumnNamesOp, 3, 3, "",},
1714 {"paste", 1, (Blt_Op)ColumnInfoOp, 4, 4, "name",},
1715 {"range", 1, (Blt_Op)ColumnSetOp, 4, 0, "name values ?name values?..",},
1716 {"type", 1, (Blt_Op)ColumnTypeOp, 4, 0, "?newType?",},
1717 {"unset", 1, (Blt_Op)ColumnUnsetOp, 4, 0, "name...",},
1718 {"type", 1, (Blt_Op)ColumnTypeOp, 4, 0, "?newType?",},
1719 #endif
1720 };
1721
1722 static int nColumnOps = sizeof(columnOps) / sizeof(Blt_OpSpec);
1723
1724 static int
1725 SelectOp(
1726 TupleCmd *cmdPtr,
1727 Tcl_Interp *interp,
1728 int objc,
1729 Tcl_Obj *CONST *objv)
1730 {
1731 Blt_Op proc;
1732 int result;
1733
1734 proc = Blt_GetOpFromObj(interp, nColumnOps, columnOps, BLT_OP_ARG2, objc,
1735 objv, 0);
1736 if (proc == NULL) {
1737 return TCL_ERROR;
1738 }
1739 result = (*proc) (cmdPtr, interp, objc, objv);
1740 return result;
1741 }
1742
1743 /*
1744 *----------------------------------------------------------------------
1745 *
1746 * SetOp --
1747 *
1748 * Sets one or more key-value pairs for tuples. One or more
1749 * tuples may be set. If any of the columns (keys) given don't
1750 * already exist, the columns will be automatically created. The
1751 * same holds true for rows. If a row index is beyond the end of
1752 * the table (tags are always in range), new rows are allocated.
1753 *
1754 * Results:
1755 * A standard Tcl result. If the tag or index is invalid,
1756 * TCL_ERROR is returned and an error message is left in the
1757 * interpreter result.
1758 *
1759 *----------------------------------------------------------------------
1760 */
1761 static int
1762 SetOp(
1763 TupleCmd *cmdPtr,
1764 Tcl_Interp *interp,
1765 int objc,
1766 Tcl_Obj *CONST *objv)
1767 {
1768 Blt_Tuple tuple;
1769 register int i;
1770 int isNew;
1771 char *string;
1772
1773 string = Tcl_GetString(objv[2]);
1774 if (isdigit(UCHAR(*string))) {
1775 int row, extra;
1776 CONST char *key;
1777 int column;
1778
1779 if (Tcl_GetIntFromObj(interp, objv[2], &row) != TCL_OK) {
1780 return TCL_ERROR;
1781 }
1782 /* Extend the array of tuples if necessary */
1783 extra = row - Blt_TupleTableLength(cmdPtr->table);
1784 if ((extra > 0) &&
1785 (Blt_TupleExtendRows(cmdPtr->table, extra) != TCL_OK)) {
1786 return TCL_ERROR;
1787 }
1788 /* Add the values, creating new keys if necessary. */
1789 for (i = 3; i < objc; i++) {
1790 key = Tcl_GetString(objv[i]);
1791 column = Blt_TupleAddColumn(cmdPtr->table, key, &isNew);
1792 i++;
1793 if (i == objc) {
1794 Tcl_AppendResult(cmdPtr->interp, "missing value for field \"",
1795 key, "\"", (char *)NULL);
1796 return TCL_ERROR;
1797 }
1798 if (Blt_TupleSetValue(interp, cmdPtr->table, tuple, key, objv[i])
1799 != TCL_OK) {
1800 return TCL_ERROR;
1801 }
1802 }
1803 return TCL_OK;
1804 } else {
1805 Blt_TupleTagSearch cursor;
1806 KeyValue *values;
1807 CONST char *key;
1808 int n;
1809
1810 tuple = Blt_TupleFirstTagged(interp, cmdPtr->table, objv[2], &cursor);
1811 if (tuple == NULL) {
1812 return TCL_ERROR;
1813 }
1814 values = Blt_Malloc(sizeof(KeyValue) * objc - 3);
1815 /* Create new keys if necessary. */
1816 n = 0;
1817 for (i = 3; i < objc; i++) {
1818 key = Tcl_GetString(objv[i]);
1819 values[n].column = Blt_TupleAddColumn(cmdPtr->table, key, &isNew);
1820 i++;
1821 if (i == objc) {
1822 Tcl_AppendResult(cmdPtr->interp, "missing value for field \"",
1823 key, "\"", (char *)NULL);
1824 goto error;
1825 }
1826 values[n].objPtr = objv[i];
1827 n++;
1828 }
1829 for (/* empty */; tuple != NULL; tuple = Blt_TupleNextTagged(&cursor)) {
1830 for (i = 0; i < n; i++) {
1831 if (Blt_TupleSetValueByIndex(cmdPtr->interp, cmdPtr->table,
1832 Blt_TupleRowIndex(tuple), values[i].column,
1833 values[i].objPtr) != TCL_OK) {
1834 goto error;
1835 }
1836 }
1837 }
1838 Blt_Free(values);
1839 return TCL_OK;
1840 error:
1841 Blt_Free(values);
1842 return TCL_ERROR;
1843 }
1844 }
1845
1846 /*
1847 *----------------------------------------------------------------------
1848 *
1849 * TagAddOp --
1850 *
1851 * Adds a tag to one or more tuples. A tuple may already have the
1852 * tag. Tag names can not start with a digit. It's also an error
1853 * to try to add the reserved tag "all".
1854 *
1855 * Results:
1856 * A standard Tcl result. If a tag isn't valid then TCL_ERROR is
1857 * returned and an error message is left in the interpreter
1858 * result.
1859 *
1860 *----------------------------------------------------------------------
1861 */
1862 static int
1863 TagAddOp(
1864 TupleCmd *cmdPtr,
1865 Tcl_Interp *interp,
1866 int objc,
1867 Tcl_Obj *CONST *objv)
1868 {
1869 Blt_Tuple tuple;
1870 register int i;
1871 char *tagName;
1872 Blt_TupleTagSearch cursor;
1873
1874 tagName = Tcl_GetString(objv[3]);
1875 if (isdigit(UCHAR(tagName[0]))) {
1876 Tcl_AppendResult(interp, "bad tag \"", tagName,
1877 "\": can't start with a digit", (char *)NULL);
1878 return TCL_ERROR;
1879 }
1880 if (strcmp(tagName, "all") == 0) {
1881 Tcl_AppendResult(cmdPtr->interp, "can't add reserved tag \"", tagName,
1882 "\"", (char *)NULL);
1883 return TCL_ERROR;
1884 }
1885 for (i = 4; i < objc; i++) {
1886 tuple = Blt_TupleFirstTagged(interp, cmdPtr->table, objv[i], &cursor);
1887 if (tuple == NULL) {
1888 return TCL_ERROR;
1889 }
1890 for (/* empty */; tuple != NULL; tuple = Blt_TupleNextTagged(&cursor)) {
1891 Blt_TupleAddTag(cmdPtr->table, tuple, tagName);
1892 }
1893 }
1894 return TCL_OK;
1895 }
1896
1897 /*
1898 *----------------------------------------------------------------------
1899 *
1900 * TagDeleteOp --
1901 *
1902 * Deletes a tag from one or more tuples. It's an error to try to
1903 * remove the tag "all". It's not an error is the tuple doesn't
1904 * currently have the tag.
1905 *
1906 * Results:
1907 * A standard Tcl result. If a tag isn't valid then TCL_ERROR is
1908 * returned and an error message is left in the interpreter
1909 * result.
1910 *
1911 *----------------------------------------------------------------------
1912 */
1913 static int
1914 TagDeleteOp(
1915 TupleCmd *cmdPtr,
1916 Tcl_Interp *interp,
1917 int objc,
1918 Tcl_Obj *CONST *objv)
1919 {
1920 char *tagName;
1921 Blt_Tuple tuple;
1922 int i;
1923 Blt_TupleTagSearch cursor;
1924
1925 tagName = Tcl_GetString(objv[3]);
1926 if (strcmp(tagName, "all") == 0) {
1927 Tcl_AppendResult(interp, "can't delete reserved tag \"", tagName, "\"",
1928 (char *)NULL);
1929 return TCL_ERROR;
1930 }
1931
1932 for (i = 4; i < objc; i++) {
1933 tuple = Blt_TupleFirstTagged(interp, cmdPtr->table, objv[i], &cursor);
1934 if (tuple == NULL) {
1935 return TCL_ERROR;
1936 }
1937 for (/* empty */; tuple != NULL; tuple = Blt_TupleNextTagged(&cursor)) {
1938 Blt_TupleRemoveTag(cmdPtr->table, tuple, tagName);
1939 }
1940 }
1941 return TCL_OK;
1942 }
1943
1944 /*
1945 *----------------------------------------------------------------------
1946 *
1947 * TagDumpOp --
1948 *
1949 * Reports information about the tuples tagged with the given
1950 * tags.
1951 *
1952 * Results:
1953 * A standard Tcl result. If a tag isn't valid then TCL_ERROR is
1954 * returned and an error message is left in the interpreter
1955 * result.
1956 *
1957 *----------------------------------------------------------------------
1958 */
1959 static int
1960 TagDumpOp(
1961 TupleCmd *cmdPtr,
1962 Tcl_Interp *interp,
1963 int objc,
1964 Tcl_Obj *CONST *objv)
1965 {
1966 Tcl_DString dString;
1967 Blt_TupleTagSearch cursor;
1968 Blt_Tuple tuple;
1969 register int i;
1970
1971 Tcl_DStringInit(&dString);
1972 for (i = 3; i < objc; i++) {
1973 tuple = Blt_TupleFirstTagged(interp, cmdPtr->table, objv[i], &cursor);
1974 if (tuple == NULL) {
1975 Tcl_DStringFree(&dString);
1976 return TCL_ERROR;
1977 }
1978 for (/* empty */; tuple != NULL; tuple = Blt_TupleNextTagged(&cursor)) {
1979 PrintTuple(cmdPtr, tuple, &dString);
1980 }
1981 }
1982 Tcl_DStringResult(interp, &dString);
1983 Tcl_DStringFree(&dString);
1984 return TCL_OK;
1985 }
1986
1987
1988 /*
1989 *----------------------------------------------------------------------
1990 *
1991 * TagForgetOp --
1992 *
1993 * Deletes one or more tags. The tuples currently associated
1994 * with the tag remain otherwise intact. It's not an error if
1995 * the tag doesn't currently exist.
1996 *
1997 * Results:
1998 * Always TCL_OK.
1999 *
2000 *----------------------------------------------------------------------
2001 */
2002 /*ARGSUSED*/
2003 static int
2004 TagForgetOp(
2005 TupleCmd *cmdPtr,
2006 Tcl_Interp *interp,
2007 int objc, /* Not used. */
2008 Tcl_Obj *CONST *objv)
2009 {
2010 register int i;
2011
2012 for (i = 3; i < objc; i++) {
2013 Blt_TupleForgetTag(cmdPtr->table, Tcl_GetString(objv[i]));
2014 }
2015 return TCL_OK;
2016 }
2017
2018 /*
2019 *----------------------------------------------------------------------
2020 *
2021 * TagNamesOp --
2022 *
2023 * Returns a list of the unique tags for the given tuples. If no
2024 * tuples are given, then all tags are reported.
2025 *
2026 * Results:
2027 * A standard Tcl result. If a tag or index of a tuple isn't
2028 * valid then TCL_ERROR is returned and an error message is left
2029 * in the interpreter result.
2030 *
2031 *----------------------------------------------------------------------
2032 */
2033 static int
2034 TagNamesOp(
2035 TupleCmd *cmdPtr,
2036 Tcl_Interp *interp,
2037 int objc,
2038 Tcl_Obj *CONST *objv)
2039 {
2040 Blt_HashSearch cursor;
2041 Blt_TupleTagEntry *tPtr;
2042 Tcl_Obj *listObjPtr, *objPtr;
2043
2044 listObjPtr = Tcl_NewListObj(0, (Tcl_Obj **) NULL);
2045 objPtr = Tcl_NewStringObj("all", -1);
2046 Tcl_ListObjAppendElement(interp, listObjPtr, objPtr);
2047 if (objc == 3) {
2048 Blt_HashEntry *hPtr;
2049
2050 for (hPtr = Blt_TupleFirstTag(cmdPtr->table, &cursor); hPtr != NULL;
2051 hPtr = Blt_NextHashEntry(&cursor)) {
2052 tPtr = Blt_GetHashValue(hPtr);
2053 objPtr = Tcl_NewStringObj(tPtr->tagName, -1);
2054 Tcl_ListObjAppendElement(interp, listObjPtr, objPtr);
2055 }
2056 } else {
2057 register int i;
2058 Blt_HashEntry *hPtr;
2059 Blt_HashTable uniqTable;
2060 int isNew;
2061 Blt_Tuple tuple;
2062
2063 Blt_InitHashTable(&uniqTable, BLT_STRING_KEYS);
2064 for (i = 3; i < objc; i++) {
2065 if (GetTuple(cmdPtr, objv[i], &tuple) != TCL_OK) {
2066 goto error;
2067 }
2068 for (hPtr = Blt_TupleFirstTag(cmdPtr->table, &cursor); hPtr != NULL;
2069 hPtr = Blt_NextHashEntry(&cursor)) {
2070 tPtr = Blt_GetHashValue(hPtr);
2071 if (Blt_TupleHasTag(cmdPtr->table, tuple, tPtr->tagName)) {
2072 Blt_CreateHashEntry(&uniqTable, tPtr->tagName, &isNew);
2073 }
2074 }
2075 }
2076 for (hPtr = Blt_FirstHashEntry(&uniqTable, &cursor); hPtr != NULL;
2077 hPtr = Blt_NextHashEntry(&cursor)) {
2078 objPtr = Tcl_NewStringObj(Blt_GetHashKey(&uniqTable, hPtr), -1);
2079 Tcl_ListObjAppendElement(interp, listObjPtr, objPtr);
2080 }
2081 Blt_DeleteHashTable(&uniqTable);
2082 }
2083 Tcl_SetObjResult(interp, listObjPtr);
2084 return TCL_OK;
2085 error:
2086 Tcl_DecrRefCount(listObjPtr);
2087 return TCL_ERROR;
2088 }
2089
2090
2091 /*
2092 *----------------------------------------------------------------------
2093 *
2094 * TagRangeOp --
2095 *
2096 * Adds a tag to a range of tuples. A tuple may already have the
2097 * tag. Tag names can not start with a digit. It's also an error
2098 * to try to add the reserved tag "all".
2099 *
2100 * Results:
2101 * A standard Tcl result. If a tag isn't valid then TCL_ERROR is
2102 * returned and an error message is left in the interpreter
2103 * result.
2104 *
2105 *----------------------------------------------------------------------
2106 */
2107 static int
2108 TagRangeOp(
2109 TupleCmd *cmdPtr,
2110 Tcl_Interp *interp,
2111 int objc,
2112 Tcl_Obj *CONST *objv)
2113 {
2114 char *tagName;
2115 Blt_Tuple first, last;
2116
2117 tagName = Tcl_GetString(objv[3]);
2118 if (isdigit(UCHAR(tagName[0]))) {
2119 Tcl_AppendResult(interp, "bad tag \"", tagName,
2120 "\": can't start with a digit", (char *)NULL);
2121 return TCL_ERROR;
2122 }
2123 if (strcmp(tagName, "all") == 0) {
2124 Tcl_AppendResult(cmdPtr->interp, "can't add reserved tag \"", tagName,
2125 "\"", (char *)NULL);
2126 return TCL_ERROR;
2127 }
2128 if ((GetTuple(cmdPtr, objv[4], &first) != TCL_OK) ||
2129 (GetTuple(cmdPtr, objv[5], &last) != TCL_OK)) {
2130 return TCL_ERROR;
2131 }
2132 if (Blt_TupleRowIndex(first) <= Blt_TupleRowIndex(last)) {
2133 Blt_Tuple tuple;
2134
2135 do {
2136 Blt_TupleAddTag(cmdPtr->table, tuple, tagName);
2137 tuple = Blt_TupleNextTuple(cmdPtr->table, tuple);
2138 } while(tuple != last);
2139 }
2140 return TCL_OK;
2141 }
2142
2143 /*
2144 *----------------------------------------------------------------------
2145 *
2146 * TagTuplesOp --
2147 *
2148 * Returns a list of unique tuple indices associated with the
2149 * tags given.
2150 *
2151 * Results:
2152 * A standard Tcl result. If a tag isn't valid then TCL_ERROR is
2153 * returned and an error message is left in the interpreter
2154 * result.
2155 *
2156 *----------------------------------------------------------------------
2157 */
2158 static int
2159 TagTuplesOp(
2160 TupleCmd *cmdPtr,
2161 Tcl_Interp *interp,
2162 int objc,
2163 Tcl_Obj *CONST *objv)
2164 {
2165 Blt_HashEntry *hPtr;
2166 Blt_HashSearch cursor;
2167 Blt_HashTable uniqTable;
2168 Blt_Tuple tuple;
2169 Tcl_Obj *listObjPtr;
2170 Tcl_Obj *objPtr;
2171 char *tagName;
2172 int isNew;
2173 register int i;
2174
2175 Blt_InitHashTable(&uniqTable, BLT_ONE_WORD_KEYS);
2176 for (i = 3; i < objc; i++) {
2177 tagName = Tcl_GetString(objv[i]);
2178 if (strcmp(tagName, "all") == 0) {
2179 break;
2180 } else {
2181 Blt_HashTable *tablePtr;
2182
2183 tablePtr = Blt_TupleTagHashTable(cmdPtr->table, tagName);
2184 if (tablePtr != NULL) {
2185 for (hPtr = Blt_FirstHashEntry(tablePtr, &cursor);
2186 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
2187 tuple = Blt_GetHashValue(hPtr);
2188 Blt_CreateHashEntry(&uniqTable, (char *)tuple, &isNew);
2189 }
2190 continue;
2191 }
2192 }
2193 Tcl_AppendResult(interp, "can't find a tag \"", tagName, "\"",
2194 (char *)NULL);
2195 goto error;
2196 }
2197 listObjPtr = Tcl_NewListObj(0, (Tcl_Obj **) NULL);
2198 for (hPtr = Blt_FirstHashEntry(&uniqTable, &cursor); hPtr != NULL;
2199 hPtr = Blt_NextHashEntry(&cursor)) {
2200 tuple = (Blt_Tuple)Blt_GetHashKey(&uniqTable, hPtr);
2201 objPtr = Tcl_NewIntObj(Blt_TupleRowIndex(tuple));
2202 Tcl_ListObjAppendElement(interp, listObjPtr, objPtr);
2203 }
2204 Tcl_SetObjResult(interp, listObjPtr);
2205 Blt_DeleteHashTable(&uniqTable);
2206 return TCL_OK;
2207
2208 error:
2209 Blt_DeleteHashTable(&uniqTable);
2210 return TCL_ERROR;
2211 }
2212
2213 /*
2214 *----------------------------------------------------------------------
2215 *
2216 * TagOp --
2217 *
2218 * This procedure is invoked to process tag operations.
2219 *
2220 * Results:
2221 * A standard Tcl result.
2222 *
2223 * Side effects:
2224 * See the user documentation.
2225 *
2226 *----------------------------------------------------------------------
2227 */
2228 static Blt_OpSpec tagOps[] = {
2229 {"add", 1, (Blt_Op)TagAddOp, 5, 0, "tag tagOrIdx...",},
2230 {"delete", 2, (Blt_Op)TagDeleteOp, 5, 0, "tag tagOrIdx...",},
2231 {"dump", 2, (Blt_Op)TagDumpOp, 4, 0, "tag...",},
2232 {"forget", 1, (Blt_Op)TagForgetOp, 4, 0, "tag...",},
2233 {"names", 1, (Blt_Op)TagNamesOp, 3, 0, "?tagOrIdx...?",},
2234 {"range", 1, (Blt_Op)TagRangeOp, 6, 6, "tag first last",},
2235 {"tuples", 1, (Blt_Op)TagTuplesOp, 4, 0, "tag ?tag...?",},
2236 };
2237
2238 static int nTagOps = sizeof(tagOps) / sizeof(Blt_OpSpec);
2239
2240 static int
2241 TagOp(
2242 TupleCmd *cmdPtr,
2243 Tcl_Interp *interp,
2244 int objc,
2245 Tcl_Obj *CONST *objv)
2246 {
2247 Blt_Op proc;
2248 int result;
2249
2250 proc = Blt_GetOpFromObj(interp, nTagOps, tagOps, BLT_OP_ARG2, objc, objv,
2251 0);
2252 if (proc == NULL) {
2253 return TCL_ERROR;
2254 }
2255 result = (*proc) (cmdPtr, interp, objc, objv);
2256 return result;
2257 }
2258
2259 /*
2260 *----------------------------------------------------------------------
2261 *
2262 * TraceCreateOp --
2263 *
2264 * Creates a trace for this instance. Traces represent
2265 * list of keys, a bitmask of trace flags, and a command prefix
2266 * to be invoked when a matching trace event occurs.
2267 *
2268 * The command prefix is parsed and saved in an array of
2269 * Tcl_Objs. The qualified name of the instance is saved
2270 * also.
2271 *
2272 * Results:
2273 * A standard Tcl result. The name of the new trace is
2274 * returned in the interpreter result. Otherwise, if it failed
2275 * to parse a switch, then TCL_ERROR is returned and an error
2276 * message is left in the interpreter result.
2277 *
2278 *----------------------------------------------------------------------
2279 */
2280 /*ARGSUSED*/
2281 static int
2282 TraceCreateOp(
2283 TupleCmd *cmdPtr,
2284 Tcl_Interp *interp,
2285 int objc, /* Not used. */
2286 Tcl_Obj *CONST *objv)
2287 {
2288 Blt_Tuple tuple;
2289 TraceInfo *tracePtr;
2290 char *keyList, *tagName, *string;
2291 char idString[200];
2292 int flags, isNew;
2293 struct Blt_TupleTraceStruct *tPtr;
2294 Tcl_Obj **args;
2295 int nArgs;
2296 int i;
2297
2298 tagName = Tcl_GetString(objv[3]);
2299 if (isdigit(UCHAR(*tagName))) {
2300 if (GetTuple(cmdPtr, objv[3], &tuple) != TCL_OK) {
2301 return TCL_ERROR;
2302 }
2303 tagName = NULL;
2304 } else {
2305 tagName = Blt_Strdup(tagName);
2306 tuple = NULL;
2307 }
2308 keyList = Tcl_GetString(objv[4]);
2309 string = Tcl_GetString(objv[5]);
2310 flags = GetTraceFlags(string);
2311 if (flags < 0) {
2312 Tcl_AppendResult(interp, "unknown flag in \"", string, "\"",
2313 (char *)NULL);
2314 return TCL_ERROR;
2315 }
2316 if (Tcl_ListObjGetElements(interp, objv[6], &nArgs, &args) != TCL_OK) {
2317 return TCL_ERROR;
2318 }
2319 tPtr = Blt_TupleCreateTrace(cmdPtr->table, tuple, keyList, tagName,
2320 flags, TupleTraceProc, tracePtr);
2321 if (tPtr == NULL) {
2322 return TCL_ERROR;
2323 }
2324 /* Stash away the command in structure and pass that to the trace. */
2325 tracePtr = Blt_Malloc(sizeof(TraceInfo));
2326 tracePtr->cmdPtr = cmdPtr;
2327 tracePtr->trace = tPtr;
2328 sprintf(idString, "trace%d", cmdPtr->traceCounter++);
2329 tracePtr->hashPtr = Blt_CreateHashEntry(&cmdPtr->traceTable, idString,
2330 &isNew);
2331 Blt_SetHashValue(tracePtr->hashPtr, tracePtr);
2332 Tcl_SetStringObj(Tcl_GetObjResult(interp), idString, -1);
2333
2334 tracePtr->objv = Blt_Calloc(nArgs + 1 + 3 + 1, sizeof(Tcl_Obj *));
2335 for(i = 0; i < nArgs; i++) {
2336 tracePtr->objv[i] = args[i];
2337 Tcl_IncrRefCount(tracePtr->objv[i]);
2338 }
2339 {
2340 Tcl_DString dString;
2341 char *qualName;
2342
2343 Tcl_DStringInit(&dString);
2344 qualName = Blt_GetQualifiedName(
2345 Blt_GetCommandNamespace(interp, cmdPtr->cmdToken),
2346 Tcl_GetCommandName(interp, cmdPtr->cmdToken), &dString);
2347 tracePtr->objv[i] = Tcl_NewStringObj(qualName, -1);
2348 Tcl_IncrRefCount(tracePtr->objv[i]);
2349 Tcl_DStringFree(&dString);
2350 tracePtr->objc = nArgs + 1;
2351 }
2352 return TCL_OK;
2353 }
2354
2355 /*
2356 *----------------------------------------------------------------------
2357 *
2358 * TraceDeleteOp --
2359 *
2360 * Deletes one or more traces.
2361 *
2362 * Results:
2363 * A standard Tcl result. If a name given doesn't represent
2364 * a trace, then TCL_ERROR is returned and an error message
2365 * is left in the interpreter result.
2366 *
2367 *----------------------------------------------------------------------
2368 */
2369 static int
2370 TraceDeleteOp(
2371 TupleCmd *cmdPtr,
2372 Tcl_Interp *interp,
2373 int objc,
2374 Tcl_Obj *CONST *objv)
2375 {
2376 TraceInfo *tracePtr;
2377 Blt_HashEntry *hPtr;
2378 register int i;
2379 char *name;
2380
2381 for (i = 3; i < objc; i++) {
2382 name = Tcl_GetString(objv[i]);
2383 hPtr = Blt_FindHashEntry(&cmdPtr->traceTable, name);
2384 if (hPtr == NULL) {
2385 Tcl_AppendResult(interp, "unknown trace \"", name, "\"",
2386 (char *)NULL);
2387 return TCL_ERROR;
2388 }
2389 tracePtr = Blt_GetHashValue(hPtr);
2390 Blt_DeleteHashEntry(&cmdPtr->traceTable, hPtr);
2391 DeleteTrace(tracePtr);
2392 }
2393 return TCL_OK;
2394 }
2395
2396 /*
2397 *----------------------------------------------------------------------
2398 *
2399 * TraceInfoOp --
2400 *
2401 * Returns the details for a given trace. The name of the trace
2402 * is passed as an argument. The details are returned as a list
2403 * of key-value pairs: trace name, tag, row index, keys, flags,
2404 * and the command prefix.
2405 *
2406 * Results:
2407 * A standard Tcl result. If the name given doesn't represent
2408 * a trace, then TCL_ERROR is returned and an error message
2409 * is left in the interpreter result.
2410 *
2411 *----------------------------------------------------------------------
2412 */
2413 /*ARGSUSED*/
2414 static int
2415 TraceInfoOp(
2416 TupleCmd *cmdPtr,
2417 Tcl_Interp *interp,
2418 int objc, /* Not used. */
2419 Tcl_Obj *CONST *objv)
2420 {
2421 TraceInfo *tracePtr;
2422 struct Blt_TupleTraceStruct *tPtr;
2423 Blt_HashEntry *hPtr;
2424 Tcl_DString dString;
2425 char string[5];
2426 char *withTag;
2427 char *idString;
2428 int i;
2429 CONST char **p;
2430
2431 idString = Tcl_GetString(objv[3]);
2432 hPtr = Blt_FindHashEntry(&cmdPtr->traceTable, idString);
2433 if (hPtr == NULL) {
2434 Tcl_AppendResult(interp, "unknown trace \"", idString, "\"",
2435 (char *)NULL);
2436 return TCL_ERROR;
2437 }
2438 Tcl_DStringInit(&dString);
2439 tPtr = tracePtr->trace;
2440 tracePtr = Blt_GetHashValue(hPtr);
2441 Tcl_DStringAppendElement(&dString, "id");
2442 Tcl_DStringAppendElement(&dString, idString);
2443
2444 Tcl_DStringAppendElement(&dString, "tag");
2445 withTag = tPtr->withTag;
2446 if (withTag == NULL) {
2447 withTag = "";
2448 }
2449 Tcl_DStringAppendElement(&dString, withTag);
2450 Tcl_DStringAppendElement(&dString, "row");
2451 Tcl_DStringAppendElement(&dString,
2452 Blt_Itoa(Blt_TupleRowIndex(tPtr->tuple)));
2453
2454 Tcl_DStringAppendElement(&dString, "keys");
2455 Tcl_DStringStartSublist(&dString);
2456 for (p = tPtr->keys; *p != NULL; p++) {
2457 Tcl_DStringAppendElement(&dString, *p);
2458 }
2459 Tcl_DStringEndSublist(&dString);
2460 Tcl_DStringAppendElement(&dString, "flags");
2461 PrintTraceFlags(tPtr->mask, string);
2462 Tcl_DStringAppendElement(&dString, string);
2463 Tcl_DStringAppendElement(&dString, "command");
2464 Tcl_DStringStartSublist(&dString);
2465 for (i = 0; i < tracePtr->objc; i++) {
2466 Tcl_DStringAppendElement(&dString, Tcl_GetString(tracePtr->objv[i]));
2467 }
2468 Tcl_DStringEndSublist(&dString);
2469 Tcl_DStringResult(interp, &dString);
2470 return TCL_OK;
2471 }
2472
2473 /*
2474 *----------------------------------------------------------------------
2475 *
2476 * TraceNamesOp --
2477 *
2478 * Returns the names of all the traces in use by this instance.
2479 * Traces created by other instances or object clients are not
2480 * reported.
2481 *
2482 * Results:
2483 * Always TCL_OK. A list of trace names is left in the
2484 * interpreter result.
2485 *
2486 *----------------------------------------------------------------------
2487 */
2488 /*ARGSUSED*/
2489 static int
2490 TraceNamesOp(
2491 TupleCmd *cmdPtr,
2492 Tcl_Interp *interp,
2493 int objc, /* Not used. */
2494 Tcl_Obj *CONST *objv)
2495 {
2496 Blt_HashEntry *hPtr;
2497 Blt_HashSearch cursor;
2498
2499 for (hPtr = Blt_FirstHashEntry(&cmdPtr->traceTable, &cursor);
2500 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
2501 Tcl_AppendElement(interp, Blt_GetHashKey(&cmdPtr->traceTable, hPtr));
2502 }
2503 return TCL_OK;
2504 }
2505
2506 /*
2507 *----------------------------------------------------------------------
2508 *
2509 * TraceOp --
2510 *
2511 * This procedure is invoked to process trace operations.
2512 *
2513 * Results:
2514 * A standard Tcl result.
2515 *
2516 * Side effects:
2517 * See the user documentation.
2518 *
2519 *----------------------------------------------------------------------
2520 */
2521 static Blt_OpSpec traceOps[] =
2522 {
2523 {"create", 1, (Blt_Op)TraceCreateOp, 7, 7, "tagOrIdx keyList how command",},
2524 {"delete", 1, (Blt_Op)TraceDeleteOp, 3, 0, "traceId...",},
2525 {"info", 1, (Blt_Op)TraceInfoOp, 4, 4, "traceId",},
2526 {"names", 1, (Blt_Op)TraceNamesOp, 3, 3, "",},
2527 };
2528
2529 static int nTraceOps = sizeof(traceOps) / sizeof(Blt_OpSpec);
2530
2531 static int
2532 TraceOp(
2533 TupleCmd *cmdPtr,
2534 Tcl_Interp *interp,
2535 int objc,
2536 Tcl_Obj *CONST *objv)
2537 {
2538 Blt_Op proc;
2539 int result;
2540
2541 proc = Blt_GetOpFromObj(interp, nTraceOps, traceOps, BLT_OP_ARG2, objc,
2542 objv, 0);
2543 if (proc == NULL) {
2544 return TCL_ERROR;
2545 }
2546 result = (*proc) (cmdPtr, interp, objc, objv);
2547 return result;
2548 }
2549
2550
2551 /*
2552 *----------------------------------------------------------------------
2553 *
2554 * UnsetOp --
2555 *
2556 * Unsets one or more values. One or more tuples may be unset
2557 * (using tags). It's not an error if one of the key names
2558 * (columns) doesn't exist. The same holds true for rows. If a
2559 * row index is beyond the end of the table (tags are always in
2560 * range), it is also ignored.
2561 *
2562 * Results:
2563 * A standard Tcl result. If the tag or index is invalid,
2564 * TCL_ERROR is returned and an error message is left in the
2565 * interpreter result.
2566 *
2567 *----------------------------------------------------------------------
2568 */
2569 static int
2570 UnsetOp(
2571 TupleCmd *cmdPtr,
2572 Tcl_Interp *interp,
2573 int objc,
2574 Tcl_Obj *CONST *objv)
2575 {
2576 Blt_Tuple tuple;
2577 char *string;
2578
2579 string = Tcl_GetString(objv[2]);
2580 if (isdigit(UCHAR(*string))) {
2581 if (GetTuple(cmdPtr, objv[2], &tuple) != TCL_OK) {
2582 return TCL_ERROR;
2583 }
2584 if (UnsetValues(cmdPtr, tuple, objc - 3, objv + 3) != TCL_OK) {
2585 return TCL_ERROR;
2586 }
2587 } else {
2588 Blt_TupleTagSearch cursor;
2589
2590 tuple = Blt_TupleFirstTagged(interp, cmdPtr->table, objv[2], &cursor);
2591 if (tuple == NULL) {
2592 return TCL_ERROR;
2593 }
2594 for (/* empty */; tuple != NULL; tuple = Blt_TupleNextTagged(&cursor)) {
2595 if (UnsetValues(cmdPtr, tuple, objc - 3, objv + 3) != TCL_OK) {
2596 return TCL_ERROR;
2597 }
2598 }
2599 }
2600 return TCL_OK;
2601 }
2602
2603 /*
2604 * --------------------------------------------------------------
2605 *
2606 * TupleInstObjCmd --
2607 *
2608 * This procedure is invoked to process commands on behalf of
2609 * the instance of the tuple-object.
2610 *
2611 * Results:
2612 * A standard Tcl result.
2613 *
2614 * Side effects:
2615 * See the user documentation.
2616 *
2617 * --------------------------------------------------------------
2618 */
2619 static Blt_OpSpec tupleOps[] =
2620 {
2621 {"attach", 2, (Blt_Op)AttachOp, 2, 3, "?tupleCmd?",},
2622 {"delete", 2, (Blt_Op)DeleteOp, 3, 0, "tagOrIdx ?tagOrIdx...?",},
2623 {"exists", 1, (Blt_Op)ExistsOp, 3, 4, "tagOrIdx ?key?",},
2624 {"get", 1, (Blt_Op)GetOp, 3, 5, "tagOrIdx ?key? ?defaultValue?",},
2625 {"notify", 2, (Blt_Op)NotifyOp, 2, 0, "args...",},
2626 {"set", 3, (Blt_Op)SetOp, 3, 0, "tagOrIdx ?key value...?",},
2627 {"tag", 2, (Blt_Op)TagOp, 3, 0, "args...",},
2628 {"trace", 2, (Blt_Op)TraceOp, 2, 0, "args...",},
2629 {"unset", 3, (Blt_Op)UnsetOp, 3, 0, "tagOrIdx ?key...?",},
2630 #ifdef notdef
2631 {"-ancestor", 2, (Blt_Op)AncestorOp, 4, 4, "node1 node2",},
2632 {"-apply", 1, (Blt_Op)ApplyOp, 3, 0, "first last ?switches?",},
2633 {"column", 2, (Blt_Op)ColumnOp, 2, 0, "args...", },
2634 {"copy", 2, (Blt_Op)CopyOp, 4, 0,
2635 "srcNode ?destTuple? destNode ?switches?",},
2636 {"dump", 4, (Blt_Op)DumpOp, 3, 3, "first ?last?",},
2637 {"dumpfile", 5, (Blt_Op)DumpfileOp, 4, 4, "first ?last? fileName",},
2638 {"find", 4, (Blt_Op)FindOp, 3, 0, "node ?switches?",},
2639 {"index", 3, (Blt_Op)IndexOp, 3, 3, "name",},
2640 {"insert", 3, (Blt_Op)InsertOp, 3, 0, "parent ?switches?",},
2641 {"-is", 2, (Blt_Op)IsOp, 2, 0, "oper args...",},
2642 {"keys", 1, (Blt_Op)KeysOp, 3, 0, "node ?node...?",},
2643 {"-label", 3, (Blt_Op)LabelOp, 3, 4, "node ?newLabel?",},
2644 {"move", 1, (Blt_Op)MoveOp, 4, 0, "node newParent ?switches?",},
2645 {"-next", 4, (Blt_Op)NextOp, 3, 3, "node",},
2646 {"-position", 2, (Blt_Op)PositionOp, 3, 0, "?switches? node...",},
2647 {"-previous", 5, (Blt_Op)PreviousOp, 3, 3, "node",},
2648 {"range", 2, (Blt_Op)ChildrenOp, 4, 4, "first last",},
2649 {"restore", 7, (Blt_Op)RestoreOp, 4, 4, "node dataString",},
2650 {"restorefile", 8, (Blt_Op)RestorefileOp, 4, 4, "node fileName",},
2651 {"width", 2, (Blt_Op)SizeOp, 3, 3, "?newWidth?",},
2652 {"length", 2, (Blt_Op)SizeOp, 3, 3, "?newLength?",},
2653 {"sort", 2, (Blt_Op)SortOp, 3, 0, "?flags...?",},
2654 {"type", 2, (Blt_Op)TypeOp, 4, 4, "key",},
2655 #endif
2656 };
2657
2658 static int nTupleOps = sizeof(tupleOps) / sizeof(Blt_OpSpec);
2659
2660 static int
2661 TupleInstObjCmd(
2662 ClientData clientData, /* Pointer to the command structure. */
2663 Tcl_Interp *interp, /* Interpreter to report errors back to. */
2664 int objc, /* Number of arguments. */
2665 Tcl_Obj *CONST *objv) /* Vector of argument strings. */
2666 {
2667 Blt_Op proc;
2668 TupleCmd *cmdPtr = clientData;
2669 int result;
2670
2671 proc = Blt_GetOpFromObj(interp, nTupleOps, tupleOps, BLT_OP_ARG1, objc,
2672 objv, BLT_OP_LINEAR_SEARCH);
2673 if (proc == NULL) {
2674 return TCL_ERROR;
2675 }
2676 Tcl_Preserve(cmdPtr);
2677 result = (*proc) (cmdPtr, interp, objc, objv);
2678 Tcl_Release(cmdPtr);
2679 return result;
2680 }
2681
2682 /*
2683 * ----------------------------------------------------------------------
2684 *
2685 * TupleInstDeleteProc --
2686 *
2687 * Deletes the command associated with the tuple. This is
2688 * called only when the command associated with the tuple is
2689 * destroyed.
2690 *
2691 * Results:
2692 * None.
2693 *
2694 * Side Effects:
2695 * The tuple object is released and bookkeeping data for traces
2696 * and notifiers are freed.
2697 *
2698 * ----------------------------------------------------------------------
2699 */
2700 static void
2701 TupleInstDeleteProc(ClientData clientData)
2702 {
2703 TupleCmd *cmdPtr = clientData;
2704 Blt_HashEntry *hPtr;
2705 Blt_HashSearch cursor;
2706 TraceInfo *tracePtr;
2707 NotifyInfo *notifyPtr;
2708
2709 if (cmdPtr->hashPtr != NULL) {
2710 Blt_DeleteHashEntry(cmdPtr->instTablePtr, cmdPtr->hashPtr);
2711 }
2712 for (hPtr = Blt_FirstHashEntry(&cmdPtr->traceTable, &cursor); hPtr != NULL;
2713 hPtr = Blt_NextHashEntry(&cursor)) {
2714 tracePtr = Blt_GetHashValue(hPtr);
2715 DeleteTrace(tracePtr);
2716 }
2717 Blt_DeleteHashTable(&cmdPtr->traceTable);
2718 for (hPtr = Blt_FirstHashEntry(&cmdPtr->notifyTable, &cursor); hPtr != NULL;
2719 hPtr = Blt_NextHashEntry(&cursor)) {
2720 notifyPtr = Blt_GetHashValue(hPtr);
2721 DeleteNotifier(notifyPtr);
2722 }
2723 Tcl_DecrRefCount(cmdPtr->emptyObjPtr);
2724 Blt_DeleteHashTable(&cmdPtr->notifyTable);
2725 Blt_TupleReleaseTable(cmdPtr->table);
2726 Blt_Free(cmdPtr);
2727 }
2728 /*
2729 *----------------------------------------------------------------------
2730 *
2731 * TupleCreateOp --
2732 *
2733 * Creates a new instance of a tuple object. This routine
2734 * ensures that instance and object names are the same. For
2735 * example, you can't create an instance with the name of an
2736 * object that already exists. And because each instance has a
2737 * Tcl command associated with it that is used to access the
2738 * object, we also check more generally that is it also not the
2739 * name of an existing Tcl command.
2740 *
2741 * Instance names as namespace-qualified. If no namespace is
2742 * designated, it is assumed that instance is to be created in
2743 * the current namespace (not the global namespace).
2744 *
2745 * Results:
2746 * A standard Tcl result. If the instance is successfully created,
2747 * the namespace-qualified name of the instance is returned. If not,
2748 * then TCL_ERROR is returned and an error message is left in the
2749 * interpreter result.
2750 *
2751 *----------------------------------------------------------------------
2752 */
2753 /*ARGSUSED*/
2754 static int
2755 TupleCreateOp(
2756 ClientData clientData, /* Interpreter-specific data. */
2757 Tcl_Interp *interp,
2758 int objc,
2759 Tcl_Obj *CONST *objv)
2760 {
2761 TupleCmdInterpData *interpDataPtr = clientData;
2762 CONST char *instName;
2763 Tcl_DString dString;
2764 Blt_TupleTable table;
2765
2766 instName = NULL;
2767 if (objc == 3) {
2768 instName = Tcl_GetString(objv[2]);
2769 }
2770 Tcl_DStringInit(&dString);
2771 if (instName == NULL) {
2772 instName = GenerateName(interp, "", "", &dString);
2773 } else {
2774 char *p;
2775
2776 p = strstr(instName, "#auto");
2777 if (p != NULL) {
2778 *p = '\0';
2779 instName = GenerateName(interp, instName, p + 5, &dString);
2780 *p = '#';
2781 } else {
2782 CONST char *name;
2783 Tcl_CmdInfo cmdInfo;
2784 Tcl_Namespace *nsPtr;
2785
2786 nsPtr = NULL;
2787 /*
2788 * Parse the command and put back so that it's in a consistent
2789 * format.
2790 *
2791 * t1 <current namespace>::t1
2792 * n1::t1 <current namespace>::n1::t1
2793 * ::t1 ::t1
2794 * ::n1::t1 ::n1::t1
2795 */
2796 if (Blt_ParseQualifiedName(interp, instName, &nsPtr, &name)
2797 != TCL_OK) {
2798 Tcl_AppendResult(interp, "can't find namespace in \"", instName,
2799 "\"", (char *)NULL);
2800 return TCL_ERROR;
2801 }
2802 if (nsPtr == NULL) {
2803 nsPtr = Tcl_GetCurrentNamespace(interp);
2804 }
2805 instName = Blt_GetQualifiedName(nsPtr, name, &dString);
2806 /*
2807 * Check if the command already exists.
2808 */
2809 if (Tcl_GetCommandInfo(interp, (char *)instName, &cmdInfo)) {
2810 Tcl_AppendResult(interp, "a command \"", instName,
2811 "\" already exists", (char *)NULL);
2812 goto error;
2813 }
2814 if (Blt_TupleTableExists(interp, instName)) {
2815 Tcl_AppendResult(interp, "a tuple \"", instName,
2816 "\" already exists", (char *)NULL);
2817 goto error;
2818 }
2819 }
2820 }
2821 if (instName == NULL) {
2822 goto error;
2823 }
2824 if (Blt_TupleCreateTable(interp, instName, &table) == TCL_OK) {
2825 TupleCmd *cmdPtr;
2826
2827 cmdPtr = NewTupleCmd(interp, interpDataPtr, table, instName);
2828 Tcl_SetResult(interp, (char *)instName, TCL_VOLATILE);
2829 Tcl_DStringFree(&dString);
2830 return TCL_OK;
2831 }
2832 error:
2833 Tcl_DStringFree(&dString);
2834 return TCL_ERROR;
2835 }
2836
2837 /*
2838 *----------------------------------------------------------------------
2839 *
2840 * TupleDestroyOp --
2841 *
2842 * Deletes one or more instances of tuple objects. The deletion
2843 * process is done by deleting the Tcl command associated with
2844 * the object.
2845 *
2846 * Results:
2847 * A standard Tcl result. If one of the names given doesn't
2848 * represent an instance, TCL_ERROR is returned and an error
2849 * message is left in the interpreter result.
2850 *
2851 *----------------------------------------------------------------------
2852 */
2853 static int
2854 TupleDestroyOp(
2855 ClientData clientData, /* Interpreter-specific data. */
2856 Tcl_Interp *interp,
2857 int objc,
2858 Tcl_Obj *CONST *objv)
2859 {
2860 TupleCmdInterpData *interpDataPtr = clientData;
2861 TupleCmd *cmdPtr;
2862 char *string;
2863 register int i;
2864
2865 for (i = 2; i < objc; i++) {
2866 string = Tcl_GetString(objv[i]);
2867 cmdPtr = GetTupleCmd(interpDataPtr, interp, string);
2868 if (cmdPtr == NULL) {
2869 Tcl_AppendResult(interp, "can't find a tuple named \"", string,
2870 "\"", (char *)NULL);
2871 return TCL_ERROR;
2872 }
2873 Tcl_DeleteCommandFromToken(interp, cmdPtr->cmdToken);
2874 }
2875 return TCL_OK;
2876 }
2877
2878 /*
2879 *----------------------------------------------------------------------
2880 *
2881 * TupleNamesOp --
2882 *
2883 * Returns the names of all the tuple-object instances matching a
2884 * given pattern. If no pattern argument is provided, then all
2885 * object names are returned. The names returned are namespace
2886 * qualified.
2887 *
2888 * Results:
2889 * Always returns TCL_OK. The names of the matching objects is
2890 * returned via the interpreter result.
2891 *
2892 *----------------------------------------------------------------------
2893 */
2894 /*ARGSUSED*/
2895 static int
2896 TupleNamesOp(
2897 ClientData clientData, /* Interpreter-specific data. */
2898 Tcl_Interp *interp,
2899 int objc,
2900 Tcl_Obj *CONST *objv)
2901 {
2902 TupleCmdInterpData *interpDataPtr = clientData;
2903 TupleCmd *cmdPtr;
2904 Blt_HashEntry *hPtr;
2905 Blt_HashSearch cursor;
2906 Tcl_Obj *objPtr, *listObjPtr;
2907 Tcl_DString dString;
2908 char *qualName;
2909
2910 Tcl_DStringInit(&dString);
2911 listObjPtr = Tcl_NewListObj(0, (Tcl_Obj **) NULL);
2912 for (hPtr = Blt_FirstHashEntry(&interpDataPtr->instTable, &cursor);
2913 hPtr != NULL; hPtr = Blt_NextHashEntry(&cursor)) {
2914 cmdPtr = Blt_GetHashValue(hPtr);
2915 qualName = Blt_GetQualifiedName(
2916 Blt_GetCommandNamespace(interp, cmdPtr->cmdToken),
2917 Tcl_GetCommandName(interp, cmdPtr->cmdToken), &dString);
2918 if (objc == 3) {
2919 if (!Tcl_StringMatch(qualName, Tcl_GetString(objv[2]))) {
2920 continue;
2921 }
2922 }
2923 objPtr = Tcl_NewStringObj(qualName, -1);
2924 Tcl_ListObjAppendElement(interp, listObjPtr, objPtr);
2925 }
2926 Tcl_SetObjResult(interp, listObjPtr);
2927 Tcl_DStringFree(&dString);
2928 return TCL_OK;
2929 }
2930
2931 /*
2932 *----------------------------------------------------------------------
2933 *
2934 * TupleObjCmd --
2935 *
2936 *----------------------------------------------------------------------
2937 */
2938 static Blt_OpSpec tupleCmdOps[] =
2939 {
2940 {"create", 1, (Blt_Op)TupleCreateOp, 2, 3, "?name?",},
2941 {"destroy", 1, (Blt_Op)TupleDestroyOp, 3, 0, "name...",},
2942 {"names", 1, (Blt_Op)TupleNamesOp, 2, 3, "?pattern?...",},
2943 };
2944
2945 static int nCmdOps = sizeof(tupleCmdOps) / sizeof(Blt_OpSpec);
2946
2947 /*ARGSUSED*/
2948 static int
2949 TupleObjCmd(
2950 ClientData clientData, /* Interpreter-specific data. */
2951 Tcl_Interp *interp,
2952 int objc,
2953 Tcl_Obj *CONST *objv)
2954 {
2955 Blt_Op proc;
2956
2957 proc = Blt_GetOpFromObj(interp, nCmdOps, tupleCmdOps, BLT_OP_ARG1, objc,
2958 objv, 0);
2959 if (proc == NULL) {
2960 return TCL_ERROR;
2961 }
2962 return (*proc) (clientData, interp, objc, objv);
2963 }
2964
2965 /*
2966 * -----------------------------------------------------------------------
2967 *
2968 * TupleInterpDeleteProc --
2969 *
2970 * This is called when the interpreter hosting the "tuple" command
2971 * is deleted.
2972 *
2973 * Results:
2974 * None.
2975 *
2976 * Side effects:
2977 * Removes the hash table managing all tuple names.
2978 *
2979 * ------------------------------------------------------------------------
2980 */
2981 /* ARGSUSED */
2982 static void
2983 TupleInterpDeleteProc(
2984 ClientData clientData, /* Interpreter-specific data. */
2985 Tcl_Interp *interp)
2986 {
2987 TupleCmdInterpData *interpDataPtr = clientData;
2988
2989 /* All tuple instances should already have been destroyed when
2990 * their respective Tcl commands were deleted. */
2991 Blt_DeleteHashTable(&interpDataPtr->instTable);
2992 Tcl_DeleteAssocData(interp, TUPLE_THREAD_KEY);
2993 Blt_Free(interpDataPtr);
2994 }
2995
2996
2997 /*
2998 * -----------------------------------------------------------------------
2999 *
3000 * Blt_TupleInit --
3001 *
3002 * This procedure is invoked to initialize the "tuple" command.
3003 *
3004 * Results:
3005 * None.
3006 *
3007 * Side effects:
3008 * Creates the new command and adds a new entry into a global Tcl
3009 * associative array.
3010 *
3011 * ------------------------------------------------------------------------
3012 */
3013 int
3014 Blt_TupleInit(Tcl_Interp *interp)
3015 {
3016 TupleCmdInterpData *interpDataPtr; /* Interpreter-specific data. */
3017 static Blt_ObjCmdSpec cmdSpec = {
3018 "tuple", TupleObjCmd,
3019 };
3020 interpDataPtr = GetTupleCmdInterpData(interp);
3021 cmdSpec.clientData = interpDataPtr;
3022 if (Blt_InitObjCmd(interp, "blt", &cmdSpec) == NULL) {
3023 return TCL_ERROR;
3024 }
3025 return TCL_OK;
3026 }
3027
3028 int
3029 Blt_TupleCmdGetToken(
3030 Tcl_Interp *interp,
3031 CONST char *string,
3032 Blt_TupleTable *tablePtr)
3033 {
3034 TupleCmdInterpData *interpDataPtr;
3035 TupleCmd *cmdPtr;
3036
3037 interpDataPtr = GetTupleCmdInterpData(interp);
3038 cmdPtr = GetTupleCmd(interpDataPtr, interp, string);
3039 if (cmdPtr == NULL) {
3040 Tcl_AppendResult(interp, "can't find a tuple object associated with \"",
3041 string, "\"", (char *)NULL);
3042 return TCL_ERROR;
3043 }
3044 *tablePtr = cmdPtr->table;
3045 return TCL_OK;
3046 }
3047
3048 /* Dump tuple to dbm */
3049 /* Convert node data to datablock */
3050
3051 #endif /* NO_TUPLE */