Debian Codegraph @

pTk/mTk/tclGeneric/tclCmdIL.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 * tclCmdIL.c --
3 *
4 * This file contains the top-level command routines for most of
5 * the Tcl built-in commands whose names begin with the letters
6 * I through L. It contains only commands in the generic core
7 * (i.e. those that don't depend much upon UNIX facilities).
8 *
9 * Copyright (c) 1987-1993 The Regents of the University of California.
10 * Copyright (c) 1993-1997 Lucent Technologies.
11 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
12 * Copyright (c) 1998-1999 by Scriptics Corporation.
13 * Copyright (c) 2001 by Kevin B. Kenny. All rights reserved.
14 *
15 * See the file "license.terms" for information on usage and redistribution
16 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
17 *
18 * RCS: @(#) $Id: tclCmdIL.c,v 1.47.2.2 2003/07/15 15:44:52 dkf Exp $
19 */
20
21 #include "tclInt.h"
22 #include "tclPort.h"
23 #include "tclRegexp.h"
24
25 /*
26 * During execution of the "lsort" command, structures of the following
27 * type are used to arrange the objects being sorted into a collection
28 * of linked lists.
29 */
30
31 typedef struct SortElement {
32 Tcl_Obj *objPtr; /* Object being sorted. */
33 int count; /* number of same elements in list */
34 struct SortElement *nextPtr; /* Next element in the list, or
35 * NULL for end of list. */
36 } SortElement;
37
38 /*
39 * The "lsort" command needs to pass certain information down to the
40 * function that compares two list elements, and the comparison function
41 * needs to pass success or failure information back up to the top-level
42 * "lsort" command. The following structure is used to pass this
43 * information.
44 */
45
46 typedef struct SortInfo {
47 int isIncreasing; /* Nonzero means sort in increasing order. */
48 int sortMode; /* The sort mode. One of SORTMODE_*
49 * values defined below */
50 Tcl_Obj *compareCmdPtr; /* The Tcl comparison command when sortMode
51 * is SORTMODE_COMMAND. Pre-initialized to
52 * hold base of command.*/
53 int index; /* If the -index option was specified, this
54 * holds the index of the list element
55 * to extract for comparison. If -index
56 * wasn't specified, this is -1. */
57 Tcl_Interp *interp; /* The interpreter in which the sortis
58 * being done. */
59 int resultCode; /* Completion code for the lsort command.
60 * If an error occurs during the sort this
61 * is changed from TCL_OK to TCL_ERROR. */
62 } SortInfo;
63
64 /*
65 * The "sortMode" field of the SortInfo structure can take on any of the
66 * following values.
67 */
68
69 #define SORTMODE_ASCII 0
70 #define SORTMODE_INTEGER 1
71 #define SORTMODE_REAL 2
72 #define SORTMODE_COMMAND 3
73 #define SORTMODE_DICTIONARY 4
74
75 /*
76 * Magic values for the index field of the SortInfo structure.
77 * Note that the index "end-1" will be translated to SORTIDX_END-1, etc.
78 */
79 #define SORTIDX_NONE -1 /* Not indexed; use whole value. */
80 #define SORTIDX_END -2 /* Indexed from end. */
81
82 /*
83 * Forward declarations for procedures defined in this file:
84 */
85
86 static void AppendLocals _ANSI_ARGS_((Tcl_Interp *interp,
87 Tcl_Obj *listPtr, CONST char *pattern,
88 int includeLinks));
89 static int DictionaryCompare _ANSI_ARGS_((char *left,
90 char *right));
91 static int InfoArgsCmd _ANSI_ARGS_((ClientData dummy,
92 Tcl_Interp *interp, int objc,
93 Tcl_Obj *CONST objv[]));
94 static int InfoBodyCmd _ANSI_ARGS_((ClientData dummy,
95 Tcl_Interp *interp, int objc,
96 Tcl_Obj *CONST objv[]));
97 static int InfoCmdCountCmd _ANSI_ARGS_((ClientData dummy,
98 Tcl_Interp *interp, int objc,
99 Tcl_Obj *CONST objv[]));
100 static int InfoCommandsCmd _ANSI_ARGS_((ClientData dummy,
101 Tcl_Interp *interp, int objc,
102 Tcl_Obj *CONST objv[]));
103 static int InfoCompleteCmd _ANSI_ARGS_((ClientData dummy,
104 Tcl_Interp *interp, int objc,
105 Tcl_Obj *CONST objv[]));
106 static int InfoDefaultCmd _ANSI_ARGS_((ClientData dummy,
107 Tcl_Interp *interp, int objc,
108 Tcl_Obj *CONST objv[]));
109 static int InfoExistsCmd _ANSI_ARGS_((ClientData dummy,
110 Tcl_Interp *interp, int objc,
111 Tcl_Obj *CONST objv[]));
112 static int InfoFunctionsCmd _ANSI_ARGS_((ClientData dummy,
113 Tcl_Interp *interp, int objc,
114 Tcl_Obj *CONST objv[]));
115 static int InfoGlobalsCmd _ANSI_ARGS_((ClientData dummy,
116 Tcl_Interp *interp, int objc,
117 Tcl_Obj *CONST objv[]));
118 static int InfoHostnameCmd _ANSI_ARGS_((ClientData dummy,
119 Tcl_Interp *interp, int objc,
120 Tcl_Obj *CONST objv[]));
121 static int InfoLevelCmd _ANSI_ARGS_((ClientData dummy,
122 Tcl_Interp *interp, int objc,
123 Tcl_Obj *CONST objv[]));
124 static int InfoLibraryCmd _ANSI_ARGS_((ClientData dummy,
125 Tcl_Interp *interp, int objc,
126 Tcl_Obj *CONST objv[]));
127 static int InfoLoadedCmd _ANSI_ARGS_((ClientData dummy,
128 Tcl_Interp *interp, int objc,
129 Tcl_Obj *CONST objv[]));
130 static int InfoLocalsCmd _ANSI_ARGS_((ClientData dummy,
131 Tcl_Interp *interp, int objc,
132 Tcl_Obj *CONST objv[]));
133 static int InfoNameOfExecutableCmd _ANSI_ARGS_((
134 ClientData dummy, Tcl_Interp *interp, int objc,
135 Tcl_Obj *CONST objv[]));
136 static int InfoPatchLevelCmd _ANSI_ARGS_((ClientData dummy,
137 Tcl_Interp *interp, int objc,
138 Tcl_Obj *CONST objv[]));
139 static int InfoProcsCmd _ANSI_ARGS_((ClientData dummy,
140 Tcl_Interp *interp, int objc,
141 Tcl_Obj *CONST objv[]));
142 static int InfoScriptCmd _ANSI_ARGS_((ClientData dummy,
143 Tcl_Interp *interp, int objc,
144 Tcl_Obj *CONST objv[]));
145 static int InfoSharedlibCmd _ANSI_ARGS_((ClientData dummy,
146 Tcl_Interp *interp, int objc,
147 Tcl_Obj *CONST objv[]));
148 static int InfoTclVersionCmd _ANSI_ARGS_((ClientData dummy,
149 Tcl_Interp *interp, int objc,
150 Tcl_Obj *CONST objv[]));
151 static int InfoVarsCmd _ANSI_ARGS_((ClientData dummy,
152 Tcl_Interp *interp, int objc,
153 Tcl_Obj *CONST objv[]));
154 static SortElement * MergeSort _ANSI_ARGS_((SortElement *headPt,
155 SortInfo *infoPtr));
156 static SortElement * MergeLists _ANSI_ARGS_((SortElement *leftPtr,
157 SortElement *rightPtr, SortInfo *infoPtr));
158 static int SortCompare _ANSI_ARGS_((Tcl_Obj *firstPtr,
159 Tcl_Obj *second, SortInfo *infoPtr));
160
161 /*
162 *----------------------------------------------------------------------
163 *
164 * Tcl_IfObjCmd --
165 *
166 * This procedure is invoked to process the "if" Tcl command.
167 * See the user documentation for details on what it does.
168 *
169 * With the bytecode compiler, this procedure is only called when
170 * a command name is computed at runtime, and is "if" or the name
171 * to which "if" was renamed: e.g., "set z if; $z 1 {puts foo}"
172 *
173 * Results:
174 * A standard Tcl result.
175 *
176 * Side effects:
177 * See the user documentation.
178 *
179 *----------------------------------------------------------------------
180 */
181
182 /* ARGSUSED */
183 int
184 Tcl_IfObjCmd(dummy, interp, objc, objv)
185 ClientData dummy; /* Not used. */
186 Tcl_Interp *interp; /* Current interpreter. */
187 int objc; /* Number of arguments. */
188 Tcl_Obj *CONST objv[]; /* Argument objects. */
189 {
190 int thenScriptIndex = 0; /* then script to be evaled after syntax check */
191 int i, result, value;
192 char *clause;
193 i = 1;
194 while (1) {
195 /*
196 * At this point in the loop, objv and objc refer to an expression
197 * to test, either for the main expression or an expression
198 * following an "elseif". The arguments after the expression must
199 * be "then" (optional) and a script to execute if the expression is
200 * true.
201 */
202
203 if (i >= objc) {
204 clause = Tcl_GetString(objv[i-1]);
205 Tcl_AppendResult(interp, "wrong # args: no expression after \"",
206 clause, "\" argument", (char *) NULL);
207 return TCL_ERROR;
208 }
209 if (!thenScriptIndex) {
210 result = Tcl_ExprBooleanObj(interp, objv[i], &value);
211 if (result != TCL_OK) {
212 return result;
213 }
214 }
215 i++;
216 if (i >= objc) {
217 missingScript:
218 clause = Tcl_GetString(objv[i-1]);
219 Tcl_AppendResult(interp, "wrong # args: no script following \"",
220 clause, "\" argument", (char *) NULL);
221 return TCL_ERROR;
222 }
223 clause = Tcl_GetString(objv[i]);
224 if ((i < objc) && (strcmp(clause, "then") == 0)) {
225 i++;
226 }
227 if (i >= objc) {
228 goto missingScript;
229 }
230 if (value) {
231 thenScriptIndex = i;
232 value = 0;
233 }
234
235 /*
236 * The expression evaluated to false. Skip the command, then
237 * see if there is an "else" or "elseif" clause.
238 */
239
240 i++;
241 if (i >= objc) {
242 if (thenScriptIndex) {
243 return Tcl_EvalObjEx(interp, objv[thenScriptIndex], 0);
244 }
245 return TCL_OK;
246 }
247 clause = Tcl_GetString(objv[i]);
248 if ((clause[0] == 'e') && (strcmp(clause, "elseif") == 0)) {
249 i++;
250 continue;
251 }
252 break;
253 }
254
255 /*
256 * Couldn't find a "then" or "elseif" clause to execute. Check now
257 * for an "else" clause. We know that there's at least one more
258 * argument when we get here.
259 */
260
261 if (strcmp(clause, "else") == 0) {
262 i++;
263 if (i >= objc) {
264 Tcl_AppendResult(interp,
265 "wrong # args: no script following \"else\" argument",
266 (char *) NULL);
267 return TCL_ERROR;
268 }
269 }
270 if (i < objc - 1) {
271 Tcl_AppendResult(interp,
272 "wrong # args: extra words after \"else\" clause in \"if\" command",
273 (char *) NULL);
274 return TCL_ERROR;
275 }
276 if (thenScriptIndex) {
277 return Tcl_EvalObjEx(interp, objv[thenScriptIndex], 0);
278 }
279 return Tcl_EvalObjEx(interp, objv[i], 0);
280 }
281
282 /*
283 *----------------------------------------------------------------------
284 *
285 * Tcl_IncrObjCmd --
286 *
287 * This procedure is invoked to process the "incr" Tcl command.
288 * See the user documentation for details on what it does.
289 *
290 * With the bytecode compiler, this procedure is only called when
291 * a command name is computed at runtime, and is "incr" or the name
292 * to which "incr" was renamed: e.g., "set z incr; $z i -1"
293 *
294 * Results:
295 * A standard Tcl result.
296 *
297 * Side effects:
298 * See the user documentation.
299 *
300 *----------------------------------------------------------------------
301 */
302
303 /* ARGSUSED */
304 int
305 Tcl_IncrObjCmd(dummy, interp, objc, objv)
306 ClientData dummy; /* Not used. */
307 Tcl_Interp *interp; /* Current interpreter. */
308 int objc; /* Number of arguments. */
309 Tcl_Obj *CONST objv[]; /* Argument objects. */
310 {
311 long incrAmount;
312 Tcl_Obj *newValuePtr;
313
314 if ((objc != 2) && (objc != 3)) {
315 Tcl_WrongNumArgs(interp, 1, objv, "varName ?increment?");
316 return TCL_ERROR;
317 }
318
319 /*
320 * Calculate the amount to increment by.
321 */
322
323 if (objc == 2) {
324 incrAmount = 1;
325 } else {
326 if (Tcl_GetLongFromObj(interp, objv[2], &incrAmount) != TCL_OK) {
327 Tcl_AddErrorInfo(interp, "\n (reading increment)");
328 return TCL_ERROR;
329 }
330 /*
331 * Need to be a bit cautious to ensure that [expr]-like rules
332 * are enforced for interpretation of wide integers, despite
333 * the fact that the underlying API itself is a 'long' only one.
334 */
335 if (objv[2]->typePtr == &tclIntType) {
336 incrAmount = objv[2]->internalRep.longValue;
337 } else if (objv[2]->typePtr == &tclWideIntType) {
338 TclGetLongFromWide(incrAmount,objv[2]);
339 } else {
340 Tcl_WideInt wide;
341
342 if (Tcl_GetWideIntFromObj(interp, objv[2], &wide) != TCL_OK) {
343 Tcl_AddErrorInfo(interp, "\n (reading increment)");
344 return TCL_ERROR;
345 }
346 incrAmount = Tcl_WideAsLong(wide);
347 if ((wide <= Tcl_LongAsWide(LONG_MAX))
348 && (wide >= Tcl_LongAsWide(LONG_MIN))) {
349 objv[2]->typePtr = &tclIntType;
350 objv[2]->internalRep.longValue = incrAmount;
351 }
352 }
353 }
354
355 /*
356 * Increment the variable's value.
357 */
358
359 newValuePtr = TclIncrVar2(interp, objv[1], (Tcl_Obj *) NULL, incrAmount,
360 TCL_LEAVE_ERR_MSG);
361 if (newValuePtr == NULL) {
362 return TCL_ERROR;
363 }
364
365 /*
366 * Set the interpreter's object result to refer to the variable's new
367 * value object.
368 */
369
370 Tcl_SetObjResult(interp, newValuePtr);
371 return TCL_OK;
372 }
373
374 /*
375 *----------------------------------------------------------------------
376 *
377 * Tcl_InfoObjCmd --
378 *
379 * This procedure is invoked to process the "info" Tcl command.
380 * See the user documentation for details on what it does.
381 *
382 * Results:
383 * A standard Tcl result.
384 *
385 * Side effects:
386 * See the user documentation.
387 *
388 *----------------------------------------------------------------------
389 */
390
391 /* ARGSUSED */
392 int
393 Tcl_InfoObjCmd(clientData, interp, objc, objv)
394 ClientData clientData; /* Arbitrary value passed to the command. */
395 Tcl_Interp *interp; /* Current interpreter. */
396 int objc; /* Number of arguments. */
397 Tcl_Obj *CONST objv[]; /* Argument objects. */
398 {
399 static CONST char *subCmds[] = {
400 "args", "body", "cmdcount", "commands",
401 "complete", "default", "exists", "functions", "globals",
402 "hostname", "level", "library", "loaded",
403 "locals", "nameofexecutable", "patchlevel", "procs",
404 "script", "sharedlibextension", "tclversion", "vars",
405 (char *) NULL};
406 enum ISubCmdIdx {
407 IArgsIdx, IBodyIdx, ICmdCountIdx, ICommandsIdx,
408 ICompleteIdx, IDefaultIdx, IExistsIdx, IFunctionsIdx, IGlobalsIdx,
409 IHostnameIdx, ILevelIdx, ILibraryIdx, ILoadedIdx,
410 ILocalsIdx, INameOfExecutableIdx, IPatchLevelIdx, IProcsIdx,
411 IScriptIdx, ISharedLibExtensionIdx, ITclVersionIdx, IVarsIdx
412 };
413 int index, result;
414
415 if (objc < 2) {
416 Tcl_WrongNumArgs(interp, 1, objv, "option ?arg arg ...?");
417 return TCL_ERROR;
418 }
419
420 result = Tcl_GetIndexFromObj(interp, objv[1], subCmds, "option", 0,
421 (int *) &index);
422 if (result != TCL_OK) {
423 return result;
424 }
425
426 switch (index) {
427 case IArgsIdx:
428 result = InfoArgsCmd(clientData, interp, objc, objv);
429 break;
430 case IBodyIdx:
431 result = InfoBodyCmd(clientData, interp, objc, objv);
432 break;
433 case ICmdCountIdx:
434 result = InfoCmdCountCmd(clientData, interp, objc, objv);
435 break;
436 case ICommandsIdx:
437 result = InfoCommandsCmd(clientData, interp, objc, objv);
438 break;
439 case ICompleteIdx:
440 result = InfoCompleteCmd(clientData, interp, objc, objv);
441 break;
442 case IDefaultIdx:
443 result = InfoDefaultCmd(clientData, interp, objc, objv);
444 break;
445 case IExistsIdx:
446 result = InfoExistsCmd(clientData, interp, objc, objv);
447 break;
448 case IFunctionsIdx:
449 result = InfoFunctionsCmd(clientData, interp, objc, objv);
450 break;
451 case IGlobalsIdx:
452 result = InfoGlobalsCmd(clientData, interp, objc, objv);
453 break;
454 case IHostnameIdx:
455 result = InfoHostnameCmd(clientData, interp, objc, objv);
456 break;
457 case ILevelIdx:
458 result = InfoLevelCmd(clientData, interp, objc, objv);
459 break;
460 case ILibraryIdx:
461 result = InfoLibraryCmd(clientData, interp, objc, objv);
462 break;
463 case ILoadedIdx:
464 result = InfoLoadedCmd(clientData, interp, objc, objv);
465 break;
466 case ILocalsIdx:
467 result = InfoLocalsCmd(clientData, interp, objc, objv);
468 break;
469 case INameOfExecutableIdx:
470 result = InfoNameOfExecutableCmd(clientData, interp, objc, objv);
471 break;
472 case IPatchLevelIdx:
473 result = InfoPatchLevelCmd(clientData, interp, objc, objv);
474 break;
475 case IProcsIdx:
476 result = InfoProcsCmd(clientData, interp, objc, objv);
477 break;
478 case IScriptIdx:
479 result = InfoScriptCmd(clientData, interp, objc, objv);
480 break;
481 case ISharedLibExtensionIdx:
482 result = InfoSharedlibCmd(clientData, interp, objc, objv);
483 break;
484 case ITclVersionIdx:
485 result = InfoTclVersionCmd(clientData, interp, objc, objv);
486 break;
487 case IVarsIdx:
488 result = InfoVarsCmd(clientData, interp, objc, objv);
489 break;
490 }
491 return result;
492 }
493
494 /*
495 *----------------------------------------------------------------------
496 *
497 * InfoArgsCmd --
498 *
499 * Called to implement the "info args" command that returns the
500 * argument list for a procedure. Handles the following syntax:
501 *
502 * info args procName
503 *
504 * Results:
505 * Returns TCL_OK if successful and TCL_ERROR if there is an error.
506 *
507 * Side effects:
508 * Returns a result in the interpreter's result object. If there is
509 * an error, the result is an error message.
510 *
511 *----------------------------------------------------------------------
512 */
513
514 static int
515 InfoArgsCmd(dummy, interp, objc, objv)
516 ClientData dummy; /* Not used. */
517 Tcl_Interp *interp; /* Current interpreter. */
518 int objc; /* Number of arguments. */
519 Tcl_Obj *CONST objv[]; /* Argument objects. */
520 {
521 register Interp *iPtr = (Interp *) interp;
522 char *name;
523 Proc *procPtr;
524 CompiledLocal *localPtr;
525 Tcl_Obj *listObjPtr;
526
527 if (objc != 3) {
528 Tcl_WrongNumArgs(interp, 2, objv, "procname");
529 return TCL_ERROR;
530 }
531
532 name = Tcl_GetString(objv[2]);
533 procPtr = TclFindProc(iPtr, name);
534 if (procPtr == NULL) {
535 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
536 "\"", name, "\" isn't a procedure", (char *) NULL);
537 return TCL_ERROR;
538 }
539
540 /*
541 * Build a return list containing the arguments.
542 */
543
544 listObjPtr = Tcl_NewListObj(0, (Tcl_Obj **) NULL);
545 for (localPtr = procPtr->firstLocalPtr; localPtr != NULL;
546 localPtr = localPtr->nextPtr) {
547 if (TclIsVarArgument(localPtr)) {
548 Tcl_ListObjAppendElement(interp, listObjPtr,
549 Tcl_NewStringObj(localPtr->name, -1));
550 }
551 }
552 Tcl_SetObjResult(interp, listObjPtr);
553 return TCL_OK;
554 }
555
556 /*
557 *----------------------------------------------------------------------
558 *
559 * InfoBodyCmd --
560 *
561 * Called to implement the "info body" command that returns the body
562 * for a procedure. Handles the following syntax:
563 *
564 * info body procName
565 *
566 * Results:
567 * Returns TCL_OK if successful and TCL_ERROR if there is an error.
568 *
569 * Side effects:
570 * Returns a result in the interpreter's result object. If there is
571 * an error, the result is an error message.
572 *
573 *----------------------------------------------------------------------
574 */
575
576 static int
577 InfoBodyCmd(dummy, interp, objc, objv)
578 ClientData dummy; /* Not used. */
579 Tcl_Interp *interp; /* Current interpreter. */
580 int objc; /* Number of arguments. */
581 Tcl_Obj *CONST objv[]; /* Argument objects. */
582 {
583 register Interp *iPtr = (Interp *) interp;
584 char *name;
585 Proc *procPtr;
586 Tcl_Obj *bodyPtr, *resultPtr;
587
588 if (objc != 3) {
589 Tcl_WrongNumArgs(interp, 2, objv, "procname");
590 return TCL_ERROR;
591 }
592
593 name = Tcl_GetString(objv[2]);
594 procPtr = TclFindProc(iPtr, name);
595 if (procPtr == NULL) {
596 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
597 "\"", name, "\" isn't a procedure", (char *) NULL);
598 return TCL_ERROR;
599 }
600
601 /*
602 * Here we used to return procPtr->bodyPtr, except when the body was
603 * bytecompiled - in that case, the return was a copy of the body's
604 * string rep. In order to better isolate the implementation details
605 * of the compiler/engine subsystem, we now always return a copy of
606 * the string rep. It is important to return a copy so that later
607 * manipulations of the object do not invalidate the internal rep.
608 */
609
610 bodyPtr = procPtr->bodyPtr;
611 if (bodyPtr->bytes == NULL) {
612 /*
613 * The string rep might not be valid if the procedure has
614 * never been run before. [Bug #545644]
615 */
616 (void) Tcl_GetString(bodyPtr);
617 }
618 resultPtr = Tcl_NewStringObj(bodyPtr->bytes, bodyPtr->length);
619
620 Tcl_SetObjResult(interp, resultPtr);
621 return TCL_OK;
622 }
623
624 /*
625 *----------------------------------------------------------------------
626 *
627 * InfoCmdCountCmd --
628 *
629 * Called to implement the "info cmdcount" command that returns the
630 * number of commands that have been executed. Handles the following
631 * syntax:
632 *
633 * info cmdcount
634 *
635 * Results:
636 * Returns TCL_OK if successful and TCL_ERROR if there is an error.
637 *
638 * Side effects:
639 * Returns a result in the interpreter's result object. If there is
640 * an error, the result is an error message.
641 *
642 *----------------------------------------------------------------------
643 */
644
645 static int
646 InfoCmdCountCmd(dummy, interp, objc, objv)
647 ClientData dummy; /* Not used. */
648 Tcl_Interp *interp; /* Current interpreter. */
649 int objc; /* Number of arguments. */
650 Tcl_Obj *CONST objv[]; /* Argument objects. */
651 {
652 Interp *iPtr = (Interp *) interp;
653
654 if (objc != 2) {
655 Tcl_WrongNumArgs(interp, 2, objv, NULL);
656 return TCL_ERROR;
657 }
658
659 Tcl_SetIntObj(Tcl_GetObjResult(interp), iPtr->cmdCount);
660 return TCL_OK;
661 }
662
663 /*
664 *----------------------------------------------------------------------
665 *
666 * InfoCommandsCmd --
667 *
668 * Called to implement the "info commands" command that returns the
669 * list of commands in the interpreter that match an optional pattern.
670 * The pattern, if any, consists of an optional sequence of namespace
671 * names separated by "::" qualifiers, which is followed by a
672 * glob-style pattern that restricts which commands are returned.
673 * Handles the following syntax:
674 *
675 * info commands ?pattern?
676 *
677 * Results:
678 * Returns TCL_OK if successful and TCL_ERROR if there is an error.
679 *
680 * Side effects:
681 * Returns a result in the interpreter's result object. If there is
682 * an error, the result is an error message.
683 *
684 *----------------------------------------------------------------------
685 */
686
687 static int
688 InfoCommandsCmd(dummy, interp, objc, objv)
689 ClientData dummy; /* Not used. */
690 Tcl_Interp *interp; /* Current interpreter. */
691 int objc; /* Number of arguments. */
692 Tcl_Obj *CONST objv[]; /* Argument objects. */
693 {
694 char *cmdName, *pattern;
695 CONST char *simplePattern;
696 register Tcl_HashEntry *entryPtr;
697 Tcl_HashSearch search;
698 Namespace *nsPtr;
699 Namespace *globalNsPtr = (Namespace *) Tcl_GetGlobalNamespace(interp);
700 Namespace *currNsPtr = (Namespace *) Tcl_GetCurrentNamespace(interp);
701 Tcl_Obj *listPtr, *elemObjPtr;
702 int specificNsInPattern = 0; /* Init. to avoid compiler warning. */
703 Tcl_Command cmd;
704
705 /*
706 * Get the pattern and find the "effective namespace" in which to
707 * list commands.
708 */
709
710 if (objc == 2) {
711 simplePattern = NULL;
712 nsPtr = currNsPtr;
713 specificNsInPattern = 0;
714 } else if (objc == 3) {
715 /*
716 * From the pattern, get the effective namespace and the simple
717 * pattern (no namespace qualifiers or ::'s) at the end. If an
718 * error was found while parsing the pattern, return it. Otherwise,
719 * if the namespace wasn't found, just leave nsPtr NULL: we will
720 * return an empty list since no commands there can be found.
721 */
722
723 Namespace *dummy1NsPtr, *dummy2NsPtr;
724
725
726 pattern = Tcl_GetString(objv[2]);
727 TclGetNamespaceForQualName(interp, pattern, (Namespace *) NULL,
728 /*flags*/ 0, &nsPtr, &dummy1NsPtr, &dummy2NsPtr, &simplePattern);
729
730 if (nsPtr != NULL) { /* we successfully found the pattern's ns */
731 specificNsInPattern = (strcmp(simplePattern, pattern) != 0);
732 }
733 } else {
734 Tcl_WrongNumArgs(interp, 2, objv, "?pattern?");
735 return TCL_ERROR;
736 }
737
738 /*
739 * Scan through the effective namespace's command table and create a
740 * list with all commands that match the pattern. If a specific
741 * namespace was requested in the pattern, qualify the command names
742 * with the namespace name.
743 */
744
745 listPtr = Tcl_NewListObj(0, (Tcl_Obj **) NULL);
746
747 if (nsPtr != NULL) {
748 entryPtr = Tcl_FirstHashEntry(&nsPtr->cmdTable, &search);
749 while (entryPtr != NULL) {
750 cmdName = Tcl_GetHashKey(&nsPtr->cmdTable, entryPtr);
751 if ((simplePattern == NULL)
752 || Tcl_StringMatch(cmdName, simplePattern)) {
753 if (specificNsInPattern) {
754 cmd = (Tcl_Command) Tcl_GetHashValue(entryPtr);
755 elemObjPtr = Tcl_NewObj();
756 Tcl_GetCommandFullName(interp, cmd, elemObjPtr);
757 } else {
758 elemObjPtr = Tcl_NewStringObj(cmdName, -1);
759 }
760 Tcl_ListObjAppendElement(interp, listPtr, elemObjPtr);
761 }
762 entryPtr = Tcl_NextHashEntry(&search);
763 }
764
765 /*
766 * If the effective namespace isn't the global :: namespace, and a
767 * specific namespace wasn't requested in the pattern, then add in
768 * all global :: commands that match the simple pattern. Of course,
769 * we add in only those commands that aren't hidden by a command in
770 * the effective namespace.
771 */
772
773 if ((nsPtr != globalNsPtr) && !specificNsInPattern) {
774 entryPtr = Tcl_FirstHashEntry(&globalNsPtr->cmdTable, &search);
775 while (entryPtr != NULL) {
776 cmdName = Tcl_GetHashKey(&globalNsPtr->cmdTable, entryPtr);
777 if ((simplePattern == NULL)
778 || Tcl_StringMatch(cmdName, simplePattern)) {
779 if (Tcl_FindHashEntry(&nsPtr->cmdTable, cmdName) == NULL) {
780 Tcl_ListObjAppendElement(interp, listPtr,
781 Tcl_NewStringObj(cmdName, -1));
782 }
783 }
784 entryPtr = Tcl_NextHashEntry(&search);
785 }
786 }
787 }
788
789 Tcl_SetObjResult(interp, listPtr);
790 return TCL_OK;
791 }
792
793 /*
794 *----------------------------------------------------------------------
795 *
796 * InfoCompleteCmd --
797 *
798 * Called to implement the "info complete" command that determines
799 * whether a string is a complete Tcl command. Handles the following
800 * syntax:
801 *
802 * info complete command
803 *
804 * Results:
805 * Returns TCL_OK if successful and TCL_ERROR if there is an error.
806 *
807 * Side effects:
808 * Returns a result in the interpreter's result object. If there is
809 * an error, the result is an error message.
810 *
811 *----------------------------------------------------------------------
812 */
813
814 static int
815 InfoCompleteCmd(dummy, interp, objc, objv)
816 ClientData dummy; /* Not used. */
817 Tcl_Interp *interp; /* Current interpreter. */
818 int objc; /* Number of arguments. */
819 Tcl_Obj *CONST objv[]; /* Argument objects. */
820 {
821 if (objc != 3) {
822 Tcl_WrongNumArgs(interp, 2, objv, "command");
823 return TCL_ERROR;
824 }
825
826 if (TclObjCommandComplete(objv[2])) {
827 Tcl_SetIntObj(Tcl_GetObjResult(interp), 1);
828 } else {
829 Tcl_SetIntObj(Tcl_GetObjResult(interp), 0);
830 }
831
832 return TCL_OK;
833 }
834
835 /*
836 *----------------------------------------------------------------------
837 *
838 * InfoDefaultCmd --
839 *
840 * Called to implement the "info default" command that returns the
841 * default value for a procedure argument. Handles the following
842 * syntax:
843 *
844 * info default procName arg varName
845 *
846 * Results:
847 * Returns TCL_OK if successful and TCL_ERROR if there is an error.
848 *
849 * Side effects:
850 * Returns a result in the interpreter's result object. If there is
851 * an error, the result is an error message.
852 *
853 *----------------------------------------------------------------------
854 */
855
856 static int
857 InfoDefaultCmd(dummy, interp, objc, objv)
858 ClientData dummy; /* Not used. */
859 Tcl_Interp *interp; /* Current interpreter. */
860 int objc; /* Number of arguments. */
861 Tcl_Obj *CONST objv[]; /* Argument objects. */
862 {
863 Interp *iPtr = (Interp *) interp;
864 char *procName, *argName, *varName;
865 Proc *procPtr;
866 CompiledLocal *localPtr;
867 Tcl_Obj *valueObjPtr;
868
869 if (objc != 5) {
870 Tcl_WrongNumArgs(interp, 2, objv, "procname arg varname");
871 return TCL_ERROR;
872 }
873
874 procName = Tcl_GetString(objv[2]);
875 argName = Tcl_GetString(objv[3]);
876
877 procPtr = TclFindProc(iPtr, procName);
878 if (procPtr == NULL) {
879 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
880 "\"", procName, "\" isn't a procedure", (char *) NULL);
881 return TCL_ERROR;
882 }
883
884 for (localPtr = procPtr->firstLocalPtr; localPtr != NULL;
885 localPtr = localPtr->nextPtr) {
886 if (TclIsVarArgument(localPtr)
887 && (strcmp(argName, localPtr->name) == 0)) {
888 if (localPtr->defValuePtr != NULL) {
889 valueObjPtr = Tcl_ObjSetVar2(interp, objv[4], NULL,
890 localPtr->defValuePtr, 0);
891 if (valueObjPtr == NULL) {
892 defStoreError:
893 varName = Tcl_GetString(objv[4]);
894 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
895 "couldn't store default value in variable \"",
896 varName, "\"", (char *) NULL);
897 return TCL_ERROR;
898 }
899 Tcl_SetIntObj(Tcl_GetObjResult(interp), 1);
900 } else {
901 Tcl_Obj *nullObjPtr = Tcl_NewObj();
902 valueObjPtr = Tcl_ObjSetVar2(interp, objv[4], NULL,
903 nullObjPtr, 0);
904 if (valueObjPtr == NULL) {
905 Tcl_DecrRefCount(nullObjPtr); /* free unneeded obj */
906 goto defStoreError;
907 }
908 Tcl_SetIntObj(Tcl_GetObjResult(interp), 0);
909 }
910 return TCL_OK;
911 }
912 }
913
914 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
915 "procedure \"", procName, "\" doesn't have an argument \"",
916 argName, "\"", (char *) NULL);
917 return TCL_ERROR;
918 }
919
920 /*
921 *----------------------------------------------------------------------
922 *
923 * InfoExistsCmd --
924 *
925 * Called to implement the "info exists" command that determines
926 * whether a variable exists. Handles the following syntax:
927 *
928 * info exists varName
929 *
930 * Results:
931 * Returns TCL_OK if successful and TCL_ERROR if there is an error.
932 *
933 * Side effects:
934 * Returns a result in the interpreter's result object. If there is
935 * an error, the result is an error message.
936 *
937 *----------------------------------------------------------------------
938 */
939
940 static int
941 InfoExistsCmd(dummy, interp, objc, objv)
942 ClientData dummy; /* Not used. */
943 Tcl_Interp *interp; /* Current interpreter. */
944 int objc; /* Number of arguments. */
945 Tcl_Obj *CONST objv[]; /* Argument objects. */
946 {
947 char *varName;
948 Var *varPtr;
949
950 if (objc != 3) {
951 Tcl_WrongNumArgs(interp, 2, objv, "varName");
952 return TCL_ERROR;
953 }
954
955 varName = Tcl_GetString(objv[2]);
956 varPtr = TclVarTraceExists(interp, varName);
957 if ((varPtr != NULL) && !TclIsVarUndefined(varPtr)) {
958 Tcl_SetIntObj(Tcl_GetObjResult(interp), 1);
959 } else {
960 Tcl_SetIntObj(Tcl_GetObjResult(interp), 0);
961 }
962 return TCL_OK;
963 }
964
965 /*
966 *----------------------------------------------------------------------
967 *
968 * InfoFunctionsCmd --
969 *
970 * Called to implement the "info functions" command that returns the
971 * list of math functions matching an optional pattern. Handles the
972 * following syntax:
973 *
974 * info functions ?pattern?
975 *
976 * Results:
977 * Returns TCL_OK if successful and TCL_ERROR if there is an error.
978 *
979 * Side effects:
980 * Returns a result in the interpreter's result object. If there is
981 * an error, the result is an error message.
982 *
983 *----------------------------------------------------------------------
984 */
985
986 static int
987 InfoFunctionsCmd(dummy, interp, objc, objv)
988 ClientData dummy; /* Not used. */
989 Tcl_Interp *interp; /* Current interpreter. */
990 int objc; /* Number of arguments. */
991 Tcl_Obj *CONST objv[]; /* Argument objects. */
992 {
993 char *pattern;
994 Tcl_Obj *listPtr;
995
996 if (objc == 2) {
997 pattern = NULL;
998 } else if (objc == 3) {
999 pattern = Tcl_GetString(objv[2]);
1000 } else {
1001 Tcl_WrongNumArgs(interp, 2, objv, "?pattern?");
1002 return TCL_ERROR;
1003 }
1004
1005 listPtr = Tcl_ListMathFuncs(interp, pattern);
1006 if (listPtr == NULL) {
1007 return TCL_ERROR;
1008 }
1009 Tcl_SetObjResult(interp, listPtr);
1010 return TCL_OK;
1011 }
1012
1013 /*
1014 *----------------------------------------------------------------------
1015 *
1016 * InfoGlobalsCmd --
1017 *
1018 * Called to implement the "info globals" command that returns the list
1019 * of global variables matching an optional pattern. Handles the
1020 * following syntax:
1021 *
1022 * info globals ?pattern?
1023 *
1024 * Results:
1025 * Returns TCL_OK if successful and TCL_ERROR if there is an error.
1026 *
1027 * Side effects:
1028 * Returns a result in the interpreter's result object. If there is
1029 * an error, the result is an error message.
1030 *
1031 *----------------------------------------------------------------------
1032 */
1033
1034 static int
1035 InfoGlobalsCmd(dummy, interp, objc, objv)
1036 ClientData dummy; /* Not used. */
1037 Tcl_Interp *interp; /* Current interpreter. */
1038 int objc; /* Number of arguments. */
1039 Tcl_Obj *CONST objv[]; /* Argument objects. */
1040 {
1041 char *varName, *pattern;
1042 Namespace *globalNsPtr = (Namespace *) Tcl_GetGlobalNamespace(interp);
1043 register Tcl_HashEntry *entryPtr;
1044 Tcl_HashSearch search;
1045 Var *varPtr;
1046 Tcl_Obj *listPtr;
1047
1048 if (objc == 2) {
1049 pattern = NULL;
1050 } else if (objc == 3) {
1051 pattern = Tcl_GetString(objv[2]);
1052 } else {
1053 Tcl_WrongNumArgs(interp, 2, objv, "?pattern?");
1054 return TCL_ERROR;
1055 }
1056
1057 /*
1058 * Scan through the global :: namespace's variable table and create a
1059 * list of all global variables that match the pattern.
1060 */
1061
1062 listPtr = Tcl_NewListObj(0, (Tcl_Obj **) NULL);
1063 for (entryPtr = Tcl_FirstHashEntry(&globalNsPtr->varTable, &search);
1064 entryPtr != NULL;
1065 entryPtr = Tcl_NextHashEntry(&search)) {
1066 varPtr = (Var *) Tcl_GetHashValue(entryPtr);
1067 if (TclIsVarUndefined(varPtr)) {
1068 continue;
1069 }
1070 varName = Tcl_GetHashKey(&globalNsPtr->varTable, entryPtr);
1071 if ((pattern == NULL) || Tcl_StringMatch(varName, pattern)) {
1072 Tcl_ListObjAppendElement(interp, listPtr,
1073 Tcl_NewStringObj(varName, -1));
1074 }
1075 }
1076 Tcl_SetObjResult(interp, listPtr);
1077 return TCL_OK;
1078 }
1079
1080 /*
1081 *----------------------------------------------------------------------
1082 *
1083 * InfoHostnameCmd --
1084 *
1085 * Called to implement the "info hostname" command that returns the
1086 * host name. Handles the following syntax:
1087 *
1088 * info hostname
1089 *
1090 * Results:
1091 * Returns TCL_OK if successful and TCL_ERROR if there is an error.
1092 *
1093 * Side effects:
1094 * Returns a result in the interpreter's result object. If there is
1095 * an error, the result is an error message.
1096 *
1097 *----------------------------------------------------------------------
1098 */
1099
1100 static int
1101 InfoHostnameCmd(dummy, interp, objc, objv)
1102 ClientData dummy; /* Not used. */
1103 Tcl_Interp *interp; /* Current interpreter. */
1104 int objc; /* Number of arguments. */
1105 Tcl_Obj *CONST objv[]; /* Argument objects. */
1106 {
1107 CONST char *name;
1108 if (objc != 2) {
1109 Tcl_WrongNumArgs(interp, 2, objv, NULL);
1110 return TCL_ERROR;
1111 }
1112
1113 name = Tcl_GetHostName();
1114 if (name) {
1115 Tcl_SetStringObj(Tcl_GetObjResult(interp), name, -1);
1116 return TCL_OK;
1117 } else {
1118 Tcl_SetStringObj(Tcl_GetObjResult(interp),
1119 "unable to determine name of host", -1);
1120 return TCL_ERROR;
1121 }
1122 }
1123
1124 /*
1125 *----------------------------------------------------------------------
1126 *
1127 * InfoLevelCmd --
1128 *
1129 * Called to implement the "info level" command that returns
1130 * information about the call stack. Handles the following syntax:
1131 *
1132 * info level ?number?
1133 *
1134 * Results:
1135 * Returns TCL_OK if successful and TCL_ERROR if there is an error.
1136 *
1137 * Side effects:
1138 * Returns a result in the interpreter's result object. If there is
1139 * an error, the result is an error message.
1140 *
1141 *----------------------------------------------------------------------
1142 */
1143
1144 static int
1145 InfoLevelCmd(dummy, interp, objc, objv)
1146 ClientData dummy; /* Not used. */
1147 Tcl_Interp *interp; /* Current interpreter. */
1148 int objc; /* Number of arguments. */
1149 Tcl_Obj *CONST objv[]; /* Argument objects. */
1150 {
1151 Interp *iPtr = (Interp *) interp;
1152 int level;
1153 CallFrame *framePtr;
1154 Tcl_Obj *listPtr;
1155
1156 if (objc == 2) { /* just "info level" */
1157 if (iPtr->varFramePtr == NULL) {
1158 Tcl_SetIntObj(Tcl_GetObjResult(interp), 0);
1159 } else {
1160 Tcl_SetIntObj(Tcl_GetObjResult(interp), iPtr->varFramePtr->level);
1161 }
1162 return TCL_OK;
1163 } else if (objc == 3) {
1164 if (Tcl_GetIntFromObj(interp, objv[2], &level) != TCL_OK) {
1165 return TCL_ERROR;
1166 }
1167 if (level <= 0) {
1168 if (iPtr->varFramePtr == NULL) {
1169 levelError:
1170 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
1171 "bad level \"",
1172 Tcl_GetString(objv[2]),
1173 "\"", (char *) NULL);
1174 return TCL_ERROR;
1175 }
1176 level += iPtr->varFramePtr->level;
1177 }
1178 for (framePtr = iPtr->varFramePtr; framePtr != NULL;
1179 framePtr = framePtr->callerVarPtr) {
1180 if (framePtr->level == level) {
1181 break;
1182 }
1183 }
1184 if (framePtr == NULL) {
1185 goto levelError;
1186 }
1187
1188 listPtr = Tcl_NewListObj(framePtr->objc, framePtr->objv);
1189 Tcl_SetObjResult(interp, listPtr);
1190 return TCL_OK;
1191 }
1192
1193 Tcl_WrongNumArgs(interp, 2, objv, "?number?");
1194 return TCL_ERROR;
1195 }
1196
1197 /*
1198 *----------------------------------------------------------------------
1199 *
1200 * InfoLibraryCmd --
1201 *
1202 * Called to implement the "info library" command that returns the
1203 * library directory for the Tcl installation. Handles the following
1204 * syntax:
1205 *
1206 * info library
1207 *
1208 * Results:
1209 * Returns TCL_OK if successful and TCL_ERROR if there is an error.
1210 *
1211 * Side effects:
1212 * Returns a result in the interpreter's result object. If there is
1213 * an error, the result is an error message.
1214 *
1215 *----------------------------------------------------------------------
1216 */
1217
1218 static int
1219 InfoLibraryCmd(dummy, interp, objc, objv)
1220 ClientData dummy; /* Not used. */
1221 Tcl_Interp *interp; /* Current interpreter. */
1222 int objc; /* Number of arguments. */
1223 Tcl_Obj *CONST objv[]; /* Argument objects. */
1224 {
1225 CONST char *libDirName;
1226
1227 if (objc != 2) {
1228 Tcl_WrongNumArgs(interp, 2, objv, NULL);
1229 return TCL_ERROR;
1230 }
1231
1232 libDirName = Tcl_GetVar(interp, "tcl_library", TCL_GLOBAL_ONLY);
1233 if (libDirName != NULL) {
1234 Tcl_SetStringObj(Tcl_GetObjResult(interp), libDirName, -1);
1235 return TCL_OK;
1236 }
1237 Tcl_SetStringObj(Tcl_GetObjResult(interp),
1238 "no library has been specified for Tcl", -1);
1239 return TCL_ERROR;
1240 }
1241
1242 /*
1243 *----------------------------------------------------------------------
1244 *
1245 * InfoLoadedCmd --
1246 *
1247 * Called to implement the "info loaded" command that returns the
1248 * packages that have been loaded into an interpreter. Handles the
1249 * following syntax:
1250 *
1251 * info loaded ?interp?
1252 *
1253 * Results:
1254 * Returns TCL_OK if successful and TCL_ERROR if there is an error.
1255 *
1256 * Side effects:
1257 * Returns a result in the interpreter's result object. If there is
1258 * an error, the result is an error message.
1259 *
1260 *----------------------------------------------------------------------
1261 */
1262
1263 static int
1264 InfoLoadedCmd(dummy, interp, objc, objv)
1265 ClientData dummy; /* Not used. */
1266 Tcl_Interp *interp; /* Current interpreter. */
1267 int objc; /* Number of arguments. */
1268 Tcl_Obj *CONST objv[]; /* Argument objects. */
1269 {
1270 char *interpName;
1271 int result;
1272
1273 if ((objc != 2) && (objc != 3)) {
1274 Tcl_WrongNumArgs(interp, 2, objv, "?interp?");
1275 return TCL_ERROR;
1276 }
1277
1278 if (objc == 2) { /* get loaded pkgs in all interpreters */
1279 interpName = NULL;
1280 } else { /* get pkgs just in specified interp */
1281 interpName = Tcl_GetString(objv[2]);
1282 }
1283 result = TclGetLoadedPackages(interp, interpName);
1284 return result;
1285 }
1286
1287 /*
1288 *----------------------------------------------------------------------
1289 *
1290 * InfoLocalsCmd --
1291 *
1292 * Called to implement the "info locals" command to return a list of
1293 * local variables that match an optional pattern. Handles the
1294 * following syntax:
1295 *
1296 * info locals ?pattern?
1297 *
1298 * Results:
1299 * Returns TCL_OK if successful and TCL_ERROR if there is an error.
1300 *
1301 * Side effects:
1302 * Returns a result in the interpreter's result object. If there is
1303 * an error, the result is an error message.
1304 *
1305 *----------------------------------------------------------------------
1306 */
1307
1308 static int
1309 InfoLocalsCmd(dummy, interp, objc, objv)
1310 ClientData dummy; /* Not used. */
1311 Tcl_Interp *interp; /* Current interpreter. */
1312 int objc; /* Number of arguments. */
1313 Tcl_Obj *CONST objv[]; /* Argument objects. */
1314 {
1315 Interp *iPtr = (Interp *) interp;
1316 char *pattern;
1317 Tcl_Obj *listPtr;
1318
1319 if (objc == 2) {
1320 pattern = NULL;
1321 } else if (objc == 3) {
1322 pattern = Tcl_GetString(objv[2]);
1323 } else {
1324 Tcl_WrongNumArgs(interp, 2, objv, "?pattern?");
1325 return TCL_ERROR;
1326 }
1327
1328 if (iPtr->varFramePtr == NULL || !iPtr->varFramePtr->isProcCallFrame) {
1329 return TCL_OK;
1330 }
1331
1332 /*
1333 * Return a list containing names of first the compiled locals (i.e. the
1334 * ones stored in the call frame), then the variables in the local hash
1335 * table (if one exists).
1336 */
1337
1338 listPtr = Tcl_NewListObj(0, (Tcl_Obj **) NULL);
1339 AppendLocals(interp, listPtr, pattern, 0);
1340 Tcl_SetObjResult(interp, listPtr);
1341 return TCL_OK;
1342 }
1343
1344 /*
1345 *----------------------------------------------------------------------
1346 *
1347 * AppendLocals --
1348 *
1349 * Append the local variables for the current frame to the
1350 * specified list object.
1351 *
1352 * Results:
1353 * None.
1354 *
1355 * Side effects:
1356 * None.
1357 *
1358 *----------------------------------------------------------------------
1359 */
1360
1361 static void
1362 AppendLocals(interp, listPtr, pattern, includeLinks)
1363 Tcl_Interp *interp; /* Current interpreter. */
1364 Tcl_Obj *listPtr; /* List object to append names to. */
1365 CONST char *pattern; /* Pattern to match against. */
1366 int includeLinks; /* 1 if upvars should be included, else 0. */
1367 {
1368 Interp *iPtr = (Interp *) interp;
1369 CompiledLocal *localPtr;
1370 Var *varPtr;
1371 int i, localVarCt;
1372 char *varName;
1373 Tcl_HashTable *localVarTablePtr;
1374 register Tcl_HashEntry *entryPtr;
1375 Tcl_HashSearch search;
1376
1377 localPtr = iPtr->varFramePtr->procPtr->firstLocalPtr;
1378 localVarCt = iPtr->varFramePtr->numCompiledLocals;
1379 varPtr = iPtr->varFramePtr->compiledLocals;
1380 localVarTablePtr = iPtr->varFramePtr->varTablePtr;
1381
1382 for (i = 0; i < localVarCt; i++) {
1383 /*
1384 * Skip nameless (temporary) variables and undefined variables
1385 */
1386
1387 if (!TclIsVarTemporary(localPtr) && !TclIsVarUndefined(varPtr)
1388 && (includeLinks || !TclIsVarLink(varPtr))) {
1389 varName = varPtr->name;
1390 if ((pattern == NULL) || Tcl_StringMatch(varName, pattern)) {
1391 Tcl_ListObjAppendElement(interp, listPtr,
1392 Tcl_NewStringObj(varName, -1));
1393 }
1394 }
1395 varPtr++;
1396 localPtr = localPtr->nextPtr;
1397 }
1398
1399 if (localVarTablePtr != NULL) {
1400 for (entryPtr = Tcl_FirstHashEntry(localVarTablePtr, &search);
1401 entryPtr != NULL;
1402 entryPtr = Tcl_NextHashEntry(&search)) {
1403 varPtr = (Var *) Tcl_GetHashValue(entryPtr);
1404 if (!TclIsVarUndefined(varPtr)
1405 && (includeLinks || !TclIsVarLink(varPtr))) {
1406 varName = Tcl_GetHashKey(localVarTablePtr, entryPtr);
1407 if ((pattern == NULL)
1408 || Tcl_StringMatch(varName, pattern)) {
1409 Tcl_ListObjAppendElement(interp, listPtr,
1410 Tcl_NewStringObj(varName, -1));
1411 }
1412 }
1413 }
1414 }
1415 }
1416
1417 /*
1418 *----------------------------------------------------------------------
1419 *
1420 * InfoNameOfExecutableCmd --
1421 *
1422 * Called to implement the "info nameofexecutable" command that returns
1423 * the name of the binary file running this application. Handles the
1424 * following syntax:
1425 *
1426 * info nameofexecutable
1427 *
1428 * Results:
1429 * Returns TCL_OK if successful and TCL_ERROR if there is an error.
1430 *
1431 * Side effects:
1432 * Returns a result in the interpreter's result object. If there is
1433 * an error, the result is an error message.
1434 *
1435 *----------------------------------------------------------------------
1436 */
1437
1438 static int
1439 InfoNameOfExecutableCmd(dummy, interp, objc, objv)
1440 ClientData dummy; /* Not used. */
1441 Tcl_Interp *interp; /* Current interpreter. */
1442 int objc; /* Number of arguments. */
1443 Tcl_Obj *CONST objv[]; /* Argument objects. */
1444 {
1445 CONST char *nameOfExecutable;
1446
1447 if (objc != 2) {
1448 Tcl_WrongNumArgs(interp, 2, objv, NULL);
1449 return TCL_ERROR;
1450 }
1451
1452 nameOfExecutable = Tcl_GetNameOfExecutable();
1453
1454 if (nameOfExecutable != NULL) {
1455 Tcl_SetStringObj(Tcl_GetObjResult(interp), nameOfExecutable, -1);
1456 }
1457 return TCL_OK;
1458 }
1459
1460 /*
1461 *----------------------------------------------------------------------
1462 *
1463 * InfoPatchLevelCmd --
1464 *
1465 * Called to implement the "info patchlevel" command that returns the
1466 * default value for an argument to a procedure. Handles the following
1467 * syntax:
1468 *
1469 * info patchlevel
1470 *
1471 * Results:
1472 * Returns TCL_OK if successful and TCL_ERROR if there is an error.
1473 *
1474 * Side effects:
1475 * Returns a result in the interpreter's result object. If there is
1476 * an error, the result is an error message.
1477 *
1478 *----------------------------------------------------------------------
1479 */
1480
1481 static int
1482 InfoPatchLevelCmd(dummy, interp, objc, objv)
1483 ClientData dummy; /* Not used. */
1484 Tcl_Interp *interp; /* Current interpreter. */
1485 int objc; /* Number of arguments. */
1486 Tcl_Obj *CONST objv[]; /* Argument objects. */
1487 {
1488 CONST char *patchlevel;
1489
1490 if (objc != 2) {
1491 Tcl_WrongNumArgs(interp, 2, objv, NULL);
1492 return TCL_ERROR;
1493 }
1494
1495 patchlevel = Tcl_GetVar(interp, "tcl_patchLevel",
1496 (TCL_GLOBAL_ONLY | TCL_LEAVE_ERR_MSG));
1497 if (patchlevel != NULL) {
1498 Tcl_SetStringObj(Tcl_GetObjResult(interp), patchlevel, -1);
1499 return TCL_OK;
1500 }
1501 return TCL_ERROR;
1502 }
1503
1504 /*
1505 *----------------------------------------------------------------------
1506 *
1507 * InfoProcsCmd --
1508 *
1509 * Called to implement the "info procs" command that returns the
1510 * list of procedures in the interpreter that match an optional pattern.
1511 * The pattern, if any, consists of an optional sequence of namespace
1512 * names separated by "::" qualifiers, which is followed by a
1513 * glob-style pattern that restricts which commands are returned.
1514 * Handles the following syntax:
1515 *
1516 * info procs ?pattern?
1517 *
1518 * Results:
1519 * Returns TCL_OK if successful and TCL_ERROR if there is an error.
1520 *
1521 * Side effects:
1522 * Returns a result in the interpreter's result object. If there is
1523 * an error, the result is an error message.
1524 *
1525 *----------------------------------------------------------------------
1526 */
1527
1528 static int
1529 InfoProcsCmd(dummy, interp, objc, objv)
1530 ClientData dummy; /* Not used. */
1531 Tcl_Interp *interp; /* Current interpreter. */
1532 int objc; /* Number of arguments. */
1533 Tcl_Obj *CONST objv[]; /* Argument objects. */
1534 {
1535 char *cmdName, *pattern;
1536 CONST char *simplePattern;
1537 Namespace *nsPtr;
1538 #ifdef INFO_PROCS_SEARCH_GLOBAL_NS
1539 Namespace *globalNsPtr = (Namespace *) Tcl_GetGlobalNamespace(interp);
1540 #endif
1541 Namespace *currNsPtr = (Namespace *) Tcl_GetCurrentNamespace(interp);
1542 Tcl_Obj *listPtr, *elemObjPtr;
1543 int specificNsInPattern = 0; /* Init. to avoid compiler warning. */
1544 register Tcl_HashEntry *entryPtr;
1545 Tcl_HashSearch search;
1546 Command *cmdPtr, *realCmdPtr;
1547
1548 /*
1549 * Get the pattern and find the "effective namespace" in which to
1550 * list procs.
1551 */
1552
1553 if (objc == 2) {
1554 simplePattern = NULL;
1555 nsPtr = currNsPtr;
1556 specificNsInPattern = 0;
1557 } else if (objc == 3) {
1558 /*
1559 * From the pattern, get the effective namespace and the simple
1560 * pattern (no namespace qualifiers or ::'s) at the end. If an
1561 * error was found while parsing the pattern, return it. Otherwise,
1562 * if the namespace wasn't found, just leave nsPtr NULL: we will
1563 * return an empty list since no commands there can be found.
1564 */
1565
1566 Namespace *dummy1NsPtr, *dummy2NsPtr;
1567
1568 pattern = Tcl_GetString(objv[2]);
1569 TclGetNamespaceForQualName(interp, pattern, (Namespace *) NULL,
1570 /*flags*/ 0, &nsPtr, &dummy1NsPtr, &dummy2NsPtr,
1571 &simplePattern);
1572
1573 if (nsPtr != NULL) { /* we successfully found the pattern's ns */
1574 specificNsInPattern = (strcmp(simplePattern, pattern) != 0);
1575 }
1576 } else {
1577 Tcl_WrongNumArgs(interp, 2, objv, "?pattern?");
1578 return TCL_ERROR;
1579 }
1580
1581 /*
1582 * Scan through the effective namespace's command table and create a
1583 * list with all procs that match the pattern. If a specific
1584 * namespace was requested in the pattern, qualify the command names
1585 * with the namespace name.
1586 */
1587
1588 listPtr = Tcl_NewListObj(0, (Tcl_Obj **) NULL);
1589 if (nsPtr != NULL) {
1590 entryPtr = Tcl_FirstHashEntry(&nsPtr->cmdTable, &search);
1591 while (entryPtr != NULL) {
1592 cmdName = Tcl_GetHashKey(&nsPtr->cmdTable, entryPtr);
1593 if ((simplePattern == NULL)
1594 || Tcl_StringMatch(cmdName, simplePattern)) {
1595 cmdPtr = (Command *) Tcl_GetHashValue(entryPtr);
1596
1597 realCmdPtr = (Command *)
1598 TclGetOriginalCommand((Tcl_Command) cmdPtr);
1599
1600 if (TclIsProc(cmdPtr)
1601 || ((realCmdPtr != NULL) && TclIsProc(realCmdPtr))) {
1602 if (specificNsInPattern) {
1603 elemObjPtr = Tcl_NewObj();
1604 Tcl_GetCommandFullName(interp, (Tcl_Command) cmdPtr,
1605 elemObjPtr);
1606 } else {
1607 elemObjPtr = Tcl_NewStringObj(cmdName, -1);
1608 }
1609
1610 Tcl_ListObjAppendElement(interp, listPtr, elemObjPtr);
1611 }
1612 }
1613 entryPtr = Tcl_NextHashEntry(&search);
1614 }
1615
1616 /*
1617 * If the effective namespace isn't the global :: namespace, and a
1618 * specific namespace wasn't requested in the pattern, then add in
1619 * all global :: procs that match the simple pattern. Of course,
1620 * we add in only those procs that aren't hidden by a proc in
1621 * the effective namespace.
1622 */
1623
1624 #ifdef INFO_PROCS_SEARCH_GLOBAL_NS
1625 /*
1626 * If "info procs" worked like "info commands", returning the
1627 * commands also seen in the global namespace, then you would
•1628 * include this code. As this could break backwards compatibility
1629 * with 8.0-8.2, we decided not to "fix" it in 8.3, leaving the
1630 * behavior slightly different.
1631 */
1632 if ((nsPtr != globalNsPtr) && !specificNsInPattern) {
1633 entryPtr = Tcl_FirstHashEntry(&globalNsPtr->cmdTable, &search);
1634 while (entryPtr != NULL) {
1635 cmdName = Tcl_GetHashKey(&globalNsPtr->cmdTable, entryPtr);
1636 if ((simplePattern == NULL)
1637 || Tcl_StringMatch(cmdName, simplePattern)) {
1638 if (Tcl_FindHashEntry(&nsPtr->cmdTable, cmdName) == NULL) {
1639 cmdPtr = (Command *) Tcl_GetHashValue(entryPtr);
1640 realCmdPtr = (Command *) TclGetOriginalCommand(
1641 (Tcl_Command) cmdPtr);
1642
1643 if (TclIsProc(cmdPtr) || ((realCmdPtr != NULL)
1644 && TclIsProc(realCmdPtr))) {
1645 Tcl_ListObjAppendElement(interp, listPtr,
1646 Tcl_NewStringObj(cmdName, -1));
1647 }
1648 }
1649 }
1650 entryPtr = Tcl_NextHashEntry(&search);
1651 }
1652 }
1653 #endif
1654 }
1655
1656 Tcl_SetObjResult(interp, listPtr);
1657 return TCL_OK;
1658 }
1659
1660 /*
1661 *----------------------------------------------------------------------
1662 *
1663 * InfoScriptCmd --
1664 *
1665 * Called to implement the "info script" command that returns the
1666 * script file that is currently being evaluated. Handles the
1667 * following syntax:
1668 *
1669 * info script ?newName?
1670 *
1671 * If newName is specified, it will set that as the internal name.
1672 *
1673 * Results:
1674 * Returns TCL_OK if successful and TCL_ERROR if there is an error.
1675 *
1676 * Side effects:
1677 * Returns a result in the interpreter's result object. If there is
1678 * an error, the result is an error message. It may change the
1679 * internal script filename.
1680 *
1681 *----------------------------------------------------------------------
1682 */
1683
1684 static int
1685 InfoScriptCmd(dummy, interp, objc, objv)
1686 ClientData dummy; /* Not used. */
1687 Tcl_Interp *interp; /* Current interpreter. */
1688 int objc; /* Number of arguments. */
1689 Tcl_Obj *CONST objv[]; /* Argument objects. */
1690 {
1691 Interp *iPtr = (Interp *) interp;
1692 if ((objc != 2) && (objc != 3)) {
1693 Tcl_WrongNumArgs(interp, 2, objv, "?filename?");
1694 return TCL_ERROR;
1695 }
1696
1697 if (objc == 3) {
1698 if (iPtr->scriptFile != NULL) {
1699 Tcl_DecrRefCount(iPtr->scriptFile);
1700 }
1701 iPtr->scriptFile = objv[2];
1702 Tcl_IncrRefCount(iPtr->scriptFile);
1703 }
1704 if (iPtr->scriptFile != NULL) {
1705 Tcl_SetObjResult(interp, iPtr->scriptFile);
1706 }
1707 return TCL_OK;
1708 }
1709
1710 /*
1711 *----------------------------------------------------------------------
1712 *
1713 * InfoSharedlibCmd --
1714 *
1715 * Called to implement the "info sharedlibextension" command that
1716 * returns the file extension used for shared libraries. Handles the
1717 * following syntax:
1718 *
1719 * info sharedlibextension
1720 *
1721 * Results:
1722 * Returns TCL_OK if successful and TCL_ERROR if there is an error.
1723 *
1724 * Side effects:
1725 * Returns a result in the interpreter's result object. If there is
1726 * an error, the result is an error message.
1727 *
1728 *----------------------------------------------------------------------
1729 */
1730
1731 static int
1732 InfoSharedlibCmd(dummy, interp, objc, objv)
1733 ClientData dummy; /* Not used. */
1734 Tcl_Interp *interp; /* Current interpreter. */
1735 int objc; /* Number of arguments. */
1736 Tcl_Obj *CONST objv[]; /* Argument objects. */
1737 {
1738 if (objc != 2) {
1739 Tcl_WrongNumArgs(interp, 2, objv, NULL);
1740 return TCL_ERROR;
1741 }
1742
1743 #ifdef TCL_SHLIB_EXT
1744 Tcl_SetStringObj(Tcl_GetObjResult(interp), TCL_SHLIB_EXT, -1);
1745 #endif
1746 return TCL_OK;
1747 }
1748
1749 /*
1750 *----------------------------------------------------------------------
1751 *
1752 * InfoTclVersionCmd --
1753 *
1754 * Called to implement the "info tclversion" command that returns the
1755 * version number for this Tcl library. Handles the following syntax:
1756 *
1757 * info tclversion
1758 *
1759 * Results:
1760 * Returns TCL_OK if successful and TCL_ERROR if there is an error.
1761 *
1762 * Side effects:
1763 * Returns a result in the interpreter's result object. If there is
1764 * an error, the result is an error message.
1765 *
1766 *----------------------------------------------------------------------
1767 */
1768
1769 static int
1770 InfoTclVersionCmd(dummy, interp, objc, objv)
1771 ClientData dummy; /* Not used. */
1772 Tcl_Interp *interp; /* Current interpreter. */
1773 int objc; /* Number of arguments. */
1774 Tcl_Obj *CONST objv[]; /* Argument objects. */
1775 {
1776 CONST char *version;
1777
1778 if (objc != 2) {
1779 Tcl_WrongNumArgs(interp, 2, objv, NULL);
1780 return TCL_ERROR;
1781 }
1782
1783 version = Tcl_GetVar(interp, "tcl_version",
1784 (TCL_GLOBAL_ONLY | TCL_LEAVE_ERR_MSG));
1785 if (version != NULL) {
1786 Tcl_SetStringObj(Tcl_GetObjResult(interp), version, -1);
1787 return TCL_OK;
1788 }
1789 return TCL_ERROR;
1790 }
1791
1792 /*
1793 *----------------------------------------------------------------------
1794 *
1795 * InfoVarsCmd --
1796 *
1797 * Called to implement the "info vars" command that returns the
1798 * list of variables in the interpreter that match an optional pattern.
1799 * The pattern, if any, consists of an optional sequence of namespace
1800 * names separated by "::" qualifiers, which is followed by a
1801 * glob-style pattern that restricts which variables are returned.
1802 * Handles the following syntax:
1803 *
1804 * info vars ?pattern?
1805 *
1806 * Results:
1807 * Returns TCL_OK if successful and TCL_ERROR if there is an error.
1808 *
1809 * Side effects:
1810 * Returns a result in the interpreter's result object. If there is
1811 * an error, the result is an error message.
1812 *
1813 *----------------------------------------------------------------------
1814 */
1815
1816 static int
1817 InfoVarsCmd(dummy, interp, objc, objv)
1818 ClientData dummy; /* Not used. */
1819 Tcl_Interp *interp; /* Current interpreter. */
1820 int objc; /* Number of arguments. */
1821 Tcl_Obj *CONST objv[]; /* Argument objects. */
1822 {
1823 Interp *iPtr = (Interp *) interp;
1824 char *varName, *pattern;
1825 CONST char *simplePattern;
1826 register Tcl_HashEntry *entryPtr;
1827 Tcl_HashSearch search;
1828 Var *varPtr;
1829 Namespace *nsPtr;
1830 Namespace *globalNsPtr = (Namespace *) Tcl_GetGlobalNamespace(interp);
1831 Namespace *currNsPtr = (Namespace *) Tcl_GetCurrentNamespace(interp);
1832 Tcl_Obj *listPtr, *elemObjPtr;
1833 int specificNsInPattern = 0; /* Init. to avoid compiler warning. */
1834
1835 /*
1836 * Get the pattern and find the "effective namespace" in which to
1837 * list variables. We only use this effective namespace if there's
1838 * no active Tcl procedure frame.
1839 */
1840
1841 if (objc == 2) {
1842 simplePattern = NULL;
1843 nsPtr = currNsPtr;
1844 specificNsInPattern = 0;
1845 } else if (objc == 3) {
1846 /*
1847 * From the pattern, get the effective namespace and the simple
1848 * pattern (no namespace qualifiers or ::'s) at the end. If an
1849 * error was found while parsing the pattern, return it. Otherwise,
1850 * if the namespace wasn't found, just leave nsPtr NULL: we will
1851 * return an empty list since no variables there can be found.
1852 */
1853
1854 Namespace *dummy1NsPtr, *dummy2NsPtr;
1855
1856 pattern = Tcl_GetString(objv[2]);
1857 TclGetNamespaceForQualName(interp, pattern, (Namespace *) NULL,
1858 /*flags*/ 0, &nsPtr, &dummy1NsPtr, &dummy2NsPtr,
1859 &simplePattern);
1860
1861 if (nsPtr != NULL) { /* we successfully found the pattern's ns */
1862 specificNsInPattern = (strcmp(simplePattern, pattern) != 0);
1863 }
1864 } else {
1865 Tcl_WrongNumArgs(interp, 2, objv, "?pattern?");
1866 return TCL_ERROR;
1867 }
1868
1869 /*
1870 * If the namespace specified in the pattern wasn't found, just return.
1871 */
1872
1873 if (nsPtr == NULL) {
1874 return TCL_OK;
1875 }
1876
1877 listPtr = Tcl_NewListObj(0, (Tcl_Obj **) NULL);
1878
1879 if ((iPtr->varFramePtr == NULL)
1880 || !iPtr->varFramePtr->isProcCallFrame
1881 || specificNsInPattern) {
1882 /*
1883 * There is no frame pointer, the frame pointer was pushed only
1884 * to activate a namespace, or we are in a procedure call frame
1885 * but a specific namespace was specified. Create a list containing
1886 * only the variables in the effective namespace's variable table.
1887 */
1888
1889 entryPtr = Tcl_FirstHashEntry(&nsPtr->varTable, &search);
1890 while (entryPtr != NULL) {
1891 varPtr = (Var *) Tcl_GetHashValue(entryPtr);
1892 if (!TclIsVarUndefined(varPtr)
1893 || (varPtr->flags & VAR_NAMESPACE_VAR)) {
1894 varName = Tcl_GetHashKey(&nsPtr->varTable, entryPtr);
1895 if ((simplePattern == NULL)
1896 || Tcl_StringMatch(varName, simplePattern)) {
1897 if (specificNsInPattern) {
1898 elemObjPtr = Tcl_NewObj();
1899 Tcl_GetVariableFullName(interp, (Tcl_Var) varPtr,
1900 elemObjPtr);
1901 } else {
1902 elemObjPtr = Tcl_NewStringObj(varName, -1);
1903 }
1904 Tcl_ListObjAppendElement(interp, listPtr, elemObjPtr);
1905 }
1906 }
1907 entryPtr = Tcl_NextHashEntry(&search);
1908 }
1909
1910 /*
1911 * If the effective namespace isn't the global :: namespace, and a
1912 * specific namespace wasn't requested in the pattern (i.e., the
1913 * pattern only specifies variable names), then add in all global ::
1914 * variables that match the simple pattern. Of course, add in only
1915 * those variables that aren't hidden by a variable in the effective
1916 * namespace.
1917 */
1918
1919 if ((nsPtr != globalNsPtr) && !specificNsInPattern) {
1920 entryPtr = Tcl_FirstHashEntry(&globalNsPtr->varTable, &search);
1921 while (entryPtr != NULL) {
1922 varPtr = (Var *) Tcl_GetHashValue(entryPtr);
1923 if (!TclIsVarUndefined(varPtr)
1924 || (varPtr->flags & VAR_NAMESPACE_VAR)) {
1925 varName = Tcl_GetHashKey(&globalNsPtr->varTable,
1926 entryPtr);
1927 if ((simplePattern == NULL)
1928 || Tcl_StringMatch(varName, simplePattern)) {
1929 if (Tcl_FindHashEntry(&nsPtr->varTable, varName) == NULL) {
1930 Tcl_ListObjAppendElement(interp, listPtr,
1931 Tcl_NewStringObj(varName, -1));
1932 }
1933 }
1934 }
1935 entryPtr = Tcl_NextHashEntry(&search);
1936 }
1937 }
1938 } else if (((Interp *)interp)->varFramePtr->procPtr != NULL) {
1939 AppendLocals(interp, listPtr, simplePattern, 1);
1940 }
1941
1942 Tcl_SetObjResult(interp, listPtr);
1943 return TCL_OK;
1944 }
1945
1946 /*
1947 *----------------------------------------------------------------------
1948 *
1949 * Tcl_JoinObjCmd --
1950 *
1951 * This procedure is invoked to process the "join" Tcl command.
1952 * See the user documentation for details on what it does.
1953 *
1954 * Results:
1955 * A standard Tcl object result.
1956 *
1957 * Side effects:
1958 * See the user documentation.
1959 *
1960 *----------------------------------------------------------------------
1961 */
1962
1963 /* ARGSUSED */
1964 int
1965 Tcl_JoinObjCmd(dummy, interp, objc, objv)
1966 ClientData dummy; /* Not used. */
1967 Tcl_Interp *interp; /* Current interpreter. */
1968 int objc; /* Number of arguments. */
1969 Tcl_Obj *CONST objv[]; /* The argument objects. */
1970 {
1971 char *joinString, *bytes;
1972 int joinLength, listLen, length, i, result;
1973 Tcl_Obj **elemPtrs;
1974 Tcl_Obj *resObjPtr;
1975
1976 if (objc == 2) {
1977 joinString = " ";
1978 joinLength = 1;
1979 } else if (objc == 3) {
1980 joinString = Tcl_GetStringFromObj(objv[2], &joinLength);
1981 } else {
1982 Tcl_WrongNumArgs(interp, 1, objv, "list ?joinString?");
1983 return TCL_ERROR;
1984 }
1985
1986 /*
1987 * Make sure the list argument is a list object and get its length and
1988 * a pointer to its array of element pointers.
1989 */
1990
1991 result = Tcl_ListObjGetElements(interp, objv[1], &listLen, &elemPtrs);
1992 if (result != TCL_OK) {
1993 return result;
1994 }
1995
1996 /*
1997 * Now concatenate strings to form the "joined" result. We append
1998 * directly into the interpreter's result object.
1999 */
2000
2001 resObjPtr = Tcl_GetObjResult(interp);
2002
2003 for (i = 0; i < listLen; i++) {
2004 bytes = Tcl_GetStringFromObj(elemPtrs[i], &length);
2005 if (i > 0) {
2006 Tcl_AppendToObj(resObjPtr, joinString, joinLength);
2007 }
2008 Tcl_AppendToObj(resObjPtr, bytes, length);
2009 }
2010 return TCL_OK;
2011 }
2012
2013 /*
2014 *----------------------------------------------------------------------
2015 *
2016 * Tcl_LindexObjCmd --
2017 *
2018 * This object-based procedure is invoked to process the "lindex" Tcl
2019 * command. See the user documentation for details on what it does.
2020 *
2021 * Results:
2022 * A standard Tcl object result.
2023 *
2024 * Side effects:
2025 * See the user documentation.
2026 *
2027 *----------------------------------------------------------------------
2028 */
2029
2030 /* ARGSUSED */
2031 int
2032 Tcl_LindexObjCmd(dummy, interp, objc, objv)
2033 ClientData dummy; /* Not used. */
2034 Tcl_Interp *interp; /* Current interpreter. */
2035 int objc; /* Number of arguments. */
2036 Tcl_Obj *CONST objv[]; /* Argument objects. */
2037 {
2038
2039 Tcl_Obj *elemPtr; /* Pointer to the element being extracted */
2040
2041 if (objc < 2) {
2042 Tcl_WrongNumArgs(interp, 1, objv, "list ?index...?");
2043 return TCL_ERROR;
2044 }
2045
2046 /*
2047 * If objc == 3, then objv[ 2 ] may be either a single index or
2048 * a list of indices: go to TclLindexList to determine which.
2049 * If objc >= 4, or objc == 2, then objv[ 2 .. objc-2 ] are all
2050 * single indices and processed as such in TclLindexFlat.
2051 */
2052
2053 if ( objc == 3 ) {
2054
2055 elemPtr = TclLindexList( interp, objv[ 1 ], objv[ 2 ] );
2056
2057 } else {
2058
2059 elemPtr = TclLindexFlat( interp, objv[ 1 ], objc-2, objv+2 );
2060
2061 }
2062
2063 /*
2064 * Set the interpreter's object result to the last element extracted
2065 */
2066
2067 if ( elemPtr == NULL ) {
2068 return TCL_ERROR;
2069 } else {
2070 Tcl_SetObjResult(interp, elemPtr);
2071 Tcl_DecrRefCount( elemPtr );
2072 return TCL_OK;
2073 }
2074 }
2075
2076 /*
2077 *----------------------------------------------------------------------
2078 *
2079 * TclLindexList --
2080 *
2081 * This procedure handles the 'lindex' command when objc==3.
2082 *
2083 * Results:
2084 * Returns a pointer to the object extracted, or NULL if an
2085 * error occurred.
2086 *
2087 * Side effects:
2088 * None.
2089 *
2090 * If objv[1] can be parsed as a list, TclLindexList handles extraction
2091 * of the desired element locally. Otherwise, it invokes
2092 * TclLindexFlat to treat objv[1] as a scalar.
2093 *
2094 * The reference count of the returned object includes one reference
2095 * corresponding to the pointer returned. Thus, the calling code will
2096 * usually do something like:
2097 * Tcl_SetObjResult( interp, result );
2098 * Tcl_DecrRefCount( result );
2099 *
2100 *----------------------------------------------------------------------
2101 */
2102
2103 Tcl_Obj *
2104 TclLindexList( interp, listPtr, argPtr )
2105 Tcl_Interp* interp; /* Tcl interpreter */
2106 Tcl_Obj* listPtr; /* List being unpacked */
2107 Tcl_Obj* argPtr; /* Index or index list */
2108 {
2109
2110 Tcl_Obj **elemPtrs; /* Elements of the list being manipulated. */
2111 int listLen; /* Length of the list being manipulated. */
2112 int index; /* Index into the list */
2113 int result; /* Result returned from a Tcl library call */
2114 int i; /* Current index number */
2115 Tcl_Obj** indices; /* Array of list indices */
2116 int indexCount; /* Size of the array of list indices */
2117 Tcl_Obj* oldListPtr; /* Temp location to preserve the list
2118 * pointer when replacing it with a sublist */
2119
2120 /*
2121 * Determine whether argPtr designates a list or a single index.
2122 * We have to be careful about the order of the checks to avoid
2123 * repeated shimmering; see TIP#22 and TIP#33 for the details.
2124 */
2125
2126 if ( argPtr->typePtr != &tclListType
2127 && TclGetIntForIndex( NULL , argPtr, 0, &index ) == TCL_OK ) {
2128
2129 /*
2130 * argPtr designates a single index.
2131 */
2132
2133 return TclLindexFlat( interp, listPtr, 1, &argPtr );
2134
2135 } else if ( Tcl_ListObjGetElements( NULL, argPtr, &indexCount, &indices )
2136 != TCL_OK ) {
2137
2138 /*
2139 * argPtr designates something that is neither an index nor a
2140 * well-formed list. Report the error via TclLindexFlat.
2141 */
2142
2143 return TclLindexFlat( interp, listPtr, 1, &argPtr );
2144 }
2145
2146 /*
2147 * Record the reference to the list that we are maintaining in
2148 * the activation record.
2149 */
2150
2151 Tcl_IncrRefCount( listPtr );
2152
2153 /*
2154 * argPtr designates a list, and the 'else if' above has parsed it
2155 * into indexCount and indices.
2156 */
2157
2158 for ( i = 0; i < indexCount; ++i ) {
2159
2160 /*
2161 * Convert the current listPtr to a list if necessary.
2162 */
2163
2164 result = Tcl_ListObjGetElements( interp, listPtr,
2165 &listLen, &elemPtrs);
2166 if (result != TCL_OK) {
2167 Tcl_DecrRefCount( listPtr );
2168 return NULL;
2169 }
2170
2171 /*
2172 * Get the index from indices[ i ]
2173 */
2174
2175 result = TclGetIntForIndex( interp, indices[ i ],
2176 /*endValue*/ (listLen - 1),
2177 &index );
2178 if ( result != TCL_OK ) {
2179 /*
2180 * Index could not be parsed
2181 */
2182
2183 Tcl_DecrRefCount( listPtr );
2184 return NULL;
2185
2186 } else if ( index < 0
2187 || index >= listLen ) {
2188 /*
2189 * Index is out of range
2190 */
2191 Tcl_DecrRefCount( listPtr );
2192 listPtr = Tcl_NewObj();
2193 Tcl_IncrRefCount( listPtr );
2194 return listPtr;
2195 }
2196
2197 /*
2198 * Make sure listPtr still refers to a list object.
2199 * If it shared a Tcl_Obj structure with the arguments, then
2200 * it might have just been converted to something else.
2201 */
2202
2203 if (listPtr->typePtr != &tclListType) {
2204 result = Tcl_ListObjGetElements(interp, listPtr, &listLen,
2205 &elemPtrs);
2206 if (result != TCL_OK) {
2207 Tcl_DecrRefCount( listPtr );
2208 return NULL;
2209 }
2210 }
2211
2212 /*
2213 * Extract the pointer to the appropriate element
2214 */
2215
2216 oldListPtr = listPtr;
2217 listPtr = elemPtrs[ index ];
2218 Tcl_IncrRefCount( listPtr );
2219 Tcl_DecrRefCount( oldListPtr );
2220
2221 /*
2222 * The work we did above may have caused the internal rep
2223 * of *argPtr to change to something else. Get it back.
2224 */
2225
2226 result = Tcl_ListObjGetElements( interp, argPtr,
2227 &indexCount, &indices );
2228 if ( result != TCL_OK ) {
2229 /*
2230 * This can't happen unless some extension corrupted a Tcl_Obj.
2231 */
2232 Tcl_DecrRefCount( listPtr );
2233 return NULL;
2234 }
2235
2236 } /* end for */
2237
2238 /*
2239 * Return the last object extracted. Its reference count will include
2240 * the reference being returned.
2241 */
2242
2243 return listPtr;
2244 }
2245
2246 /*
2247 *----------------------------------------------------------------------
2248 *
2249 * TclLindexFlat --
2250 *
2251 * This procedure handles the 'lindex' command, given that the
2252 * arguments to the command are known to be a flat list.
2253 *
2254 * Results:
2255 * Returns a standard Tcl result.
2256 *
2257 * Side effects:
2258 * None.
2259 *
2260 * This procedure is called from either tclExecute.c or
2261 * Tcl_LindexObjCmd whenever either is presented with
2262 * objc == 2 or objc >= 4. It is also called from TclLindexList
2263 * for the objc==3 case once it is determined that objv[2] cannot
2264 * be parsed as a list.
2265 *
2266 *----------------------------------------------------------------------
2267 */
2268
2269 Tcl_Obj *
2270 TclLindexFlat( interp, listPtr, indexCount, indexArray )
2271 Tcl_Interp* interp; /* Tcl interpreter */
2272 Tcl_Obj* listPtr; /* Tcl object representing the list */
2273 int indexCount; /* Count of indices */
2274 Tcl_Obj* CONST indexArray[];
2275 /* Array of pointers to Tcl objects
2276 * representing the indices in the
2277 * list */
2278 {
2279
2280 int i; /* Current list index */
2281 int result; /* Result of Tcl library calls */
2282 int listLen; /* Length of the current list being
2283 * processed */
2284 Tcl_Obj** elemPtrs; /* Array of pointers to the elements
2285 * of the current list */
2286 int index; /* Parsed version of the current element
2287 * of indexArray */
2288 Tcl_Obj* oldListPtr; /* Temporary to hold listPtr so that
2289 * its ref count can be decremented. */
2290
2291 /*
2292 * Record the reference to the 'listPtr' object that we are
2293 * maintaining in the C activation record.
2294 */
2295
2296 Tcl_IncrRefCount( listPtr );
2297
2298 for ( i = 0; i < indexCount; ++i ) {
2299
2300 /*
2301 * Convert the current listPtr to a list if necessary.
2302 */
2303
2304 result = Tcl_ListObjGetElements(interp, listPtr,
2305 &listLen, &elemPtrs);
2306 if (result != TCL_OK) {
2307 Tcl_DecrRefCount( listPtr );
2308 return NULL;
2309 }
2310
2311 /*
2312 * Get the index from objv[i]
2313 */
2314
2315 result = TclGetIntForIndex( interp, indexArray[ i ],
2316 /*endValue*/ (listLen - 1),
2317 &index );
2318 if ( result != TCL_OK ) {
2319
2320 /* Index could not be parsed */
2321
2322 Tcl_DecrRefCount( listPtr );
2323 return NULL;
2324
2325 } else if ( index < 0
2326 || index >= listLen ) {
2327
2328 /*
2329 * Index is out of range
2330 */
2331
2332 Tcl_DecrRefCount( listPtr );
2333 listPtr = Tcl_NewObj();
2334 Tcl_IncrRefCount( listPtr );
2335 return listPtr;
2336 }
2337
2338 /*
2339 * Make sure listPtr still refers to a list object.
2340 * It might have been converted to something else above
2341 * if objv[1] overlaps with one of the other parameters.
2342 */
2343
2344 if (listPtr->typePtr != &tclListType) {
2345 result = Tcl_ListObjGetElements(interp, listPtr, &listLen,
2346 &elemPtrs);
2347 if (result != TCL_OK) {
2348 Tcl_DecrRefCount( listPtr );
2349 return NULL;
2350 }
2351 }
2352
2353 /*
2354 * Extract the pointer to the appropriate element
2355 */
2356
2357 oldListPtr = listPtr;
2358 listPtr = elemPtrs[ index ];
2359 Tcl_IncrRefCount( listPtr );
2360 Tcl_DecrRefCount( oldListPtr );
2361
2362 }
2363
2364 return listPtr;
2365
2366 }
2367
2368 /*
2369 *----------------------------------------------------------------------
2370 *
2371 * Tcl_LinsertObjCmd --
2372 *
2373 * This object-based procedure is invoked to process the "linsert" Tcl
2374 * command. See the user documentation for details on what it does.
2375 *
2376 * Results:
2377 * A new Tcl list object formed by inserting zero or more elements
2378 * into a list.
2379 *
2380 * Side effects:
2381 * See the user documentation.
2382 *
2383 *----------------------------------------------------------------------
2384 */
2385
2386 /* ARGSUSED */
2387 int
2388 Tcl_LinsertObjCmd(dummy, interp, objc, objv)
2389 ClientData dummy; /* Not used. */
2390 Tcl_Interp *interp; /* Current interpreter. */
2391 register int objc; /* Number of arguments. */
2392 Tcl_Obj *CONST objv[]; /* Argument objects. */
2393 {
2394 Tcl_Obj *listPtr;
2395 int index, isDuplicate, len, result;
2396
2397 if (objc < 4) {
2398 Tcl_WrongNumArgs(interp, 1, objv, "list index element ?element ...?");
2399 return TCL_ERROR;
2400 }
2401
2402 result = Tcl_ListObjLength(interp, objv[1], &len);
2403 if (result != TCL_OK) {
2404 return result;
2405 }
2406
2407 /*
2408 * Get the index. "end" is interpreted to be the index after the last
2409 * element, such that using it will cause any inserted elements to be
2410 * appended to the list.
2411 */
2412
2413 result = TclGetIntForIndex(interp, objv[2], /*end*/ len, &index);
2414 if (result != TCL_OK) {
2415 return result;
2416 }
2417 if (index > len) {
2418 index = len;
2419 }
2420
2421 /*
2422 * If the list object is unshared we can modify it directly. Otherwise
2423 * we create a copy to modify: this is "copy on write".
2424 */
2425
2426 listPtr = objv[1];
2427 isDuplicate = 0;
2428 if (Tcl_IsShared(listPtr)) {
2429 listPtr = Tcl_DuplicateObj(listPtr);
2430 isDuplicate = 1;
2431 }
2432
2433 if ((objc == 4) && (index == len)) {
2434 /*
2435 * Special case: insert one element at the end of the list.
2436 */
2437 result = Tcl_ListObjAppendElement(interp, listPtr, objv[3]);
2438 } else if (objc > 3) {
2439 result = Tcl_ListObjReplace(interp, listPtr, index, 0,
2440 (objc-3), &(objv[3]));
2441 }
2442 if (result != TCL_OK) {
2443 if (isDuplicate) {
2444 Tcl_DecrRefCount(listPtr); /* free unneeded obj */
2445 }
2446 return result;
2447 }
2448
2449 /*
2450 * Set the interpreter's object result.
2451 */
2452
2453 Tcl_SetObjResult(interp, listPtr);
2454 return TCL_OK;
2455 }
2456
2457 /*
2458 *----------------------------------------------------------------------
2459 *
2460 * Tcl_ListObjCmd --
2461 *
2462 * This procedure is invoked to process the "list" Tcl command.
2463 * See the user documentation for details on what it does.
2464 *
2465 * Results:
2466 * A standard Tcl object result.
2467 *
2468 * Side effects:
2469 * See the user documentation.
2470 *
2471 *----------------------------------------------------------------------
2472 */
2473
2474 /* ARGSUSED */
2475 int
2476 Tcl_ListObjCmd(dummy, interp, objc, objv)
2477 ClientData dummy; /* Not used. */
2478 Tcl_Interp *interp; /* Current interpreter. */
2479 register int objc; /* Number of arguments. */
2480 register Tcl_Obj *CONST objv[]; /* The argument objects. */
2481 {
2482 /*
2483 * If there are no list elements, the result is an empty object.
2484 * Otherwise modify the interpreter's result object to be a list object.
2485 */
2486
2487 if (objc > 1) {
2488 Tcl_SetListObj(Tcl_GetObjResult(interp), (objc-1), &(objv[1]));
2489 }
2490 return TCL_OK;
2491 }
2492
2493 /*
2494 *----------------------------------------------------------------------
2495 *
2496 * Tcl_LlengthObjCmd --
2497 *
2498 * This object-based procedure is invoked to process the "llength" Tcl
2499 * command. See the user documentation for details on what it does.
2500 *
2501 * Results:
2502 * A standard Tcl object result.
2503 *
2504 * Side effects:
2505 * See the user documentation.
2506 *
2507 *----------------------------------------------------------------------
2508 */
2509
2510 /* ARGSUSED */
2511 int
2512 Tcl_LlengthObjCmd(dummy, interp, objc, objv)
2513 ClientData dummy; /* Not used. */
2514 Tcl_Interp *interp; /* Current interpreter. */
2515 int objc; /* Number of arguments. */
2516 register Tcl_Obj *CONST objv[]; /* Argument objects. */
2517 {
2518 int listLen, result;
2519
2520 if (objc != 2) {
2521 Tcl_WrongNumArgs(interp, 1, objv, "list");
2522 return TCL_ERROR;
2523 }
2524
2525 result = Tcl_ListObjLength(interp, objv[1], &listLen);
2526 if (result != TCL_OK) {
2527 return result;
2528 }
2529
2530 /*
2531 * Set the interpreter's object result to an integer object holding the
2532 * length.
2533 */
2534
2535 Tcl_SetIntObj(Tcl_GetObjResult(interp), listLen);
2536 return TCL_OK;
2537 }
2538
2539 /*
2540 *----------------------------------------------------------------------
2541 *
2542 * Tcl_LrangeObjCmd --
2543 *
2544 * This procedure is invoked to process the "lrange" Tcl command.
2545 * See the user documentation for details on what it does.
2546 *
2547 * Results:
2548 * A standard Tcl object result.
2549 *
2550 * Side effects:
2551 * See the user documentation.
2552 *
2553 *----------------------------------------------------------------------
2554 */
2555
2556 /* ARGSUSED */
2557 int
2558 Tcl_LrangeObjCmd(notUsed, interp, objc, objv)
2559 ClientData notUsed; /* Not used. */
2560 Tcl_Interp *interp; /* Current interpreter. */
2561 int objc; /* Number of arguments. */
2562 register Tcl_Obj *CONST objv[]; /* Argument objects. */
2563 {
2564 Tcl_Obj *listPtr;
2565 Tcl_Obj **elemPtrs;
2566 int listLen, first, last, numElems, result;
2567
2568 if (objc != 4) {
2569 Tcl_WrongNumArgs(interp, 1, objv, "list first last");
2570 return TCL_ERROR;
2571 }
2572
2573 /*
2574 * Make sure the list argument is a list object and get its length and
2575 * a pointer to its array of element pointers.
2576 */
2577
2578 listPtr = objv[1];
2579 result = Tcl_ListObjGetElements(interp, listPtr, &listLen, &elemPtrs);
2580 if (result != TCL_OK) {
2581 return result;
2582 }
2583
2584 /*
2585 * Get the first and last indexes.
2586 */
2587
2588 result = TclGetIntForIndex(interp, objv[2], /*endValue*/ (listLen - 1),
2589 &first);
2590 if (result != TCL_OK) {
2591 return result;
2592 }
2593 if (first < 0) {
2594 first = 0;
2595 }
2596
2597 result = TclGetIntForIndex(interp, objv[3], /*endValue*/ (listLen - 1),
2598 &last);
2599 if (result != TCL_OK) {
2600 return result;
2601 }
2602 if (last >= listLen) {
2603 last = (listLen - 1);
2604 }
2605
2606 if (first > last) {
2607 return TCL_OK; /* the result is an empty object */
2608 }
2609
2610 /*
2611 * Make sure listPtr still refers to a list object. It might have been
2612 * converted to an int above if the argument objects were shared.
2613 */
2614
2615 if (listPtr->typePtr != &tclListType) {
2616 result = Tcl_ListObjGetElements(interp, listPtr, &listLen,
2617 &elemPtrs);
2618 if (result != TCL_OK) {
2619 return result;
2620 }
2621 }
2622
2623 /*
2624 * Extract a range of fields. We modify the interpreter's result object
2625 * to be a list object containing the specified elements.
2626 */
2627
2628 numElems = (last - first + 1);
2629 Tcl_SetListObj(Tcl_GetObjResult(interp), numElems, &(elemPtrs[first]));
2630 return TCL_OK;
2631 }
2632
2633 /*
2634 *----------------------------------------------------------------------
2635 *
2636 * Tcl_LreplaceObjCmd --
2637 *
2638 * This object-based procedure is invoked to process the "lreplace"
2639 * Tcl command. See the user documentation for details on what it does.
2640 *
2641 * Results:
2642 * A new Tcl list object formed by replacing zero or more elements of
2643 * a list.
2644 *
2645 * Side effects:
2646 * See the user documentation.
2647 *
2648 *----------------------------------------------------------------------
2649 */
2650
2651 /* ARGSUSED */
2652 int
2653 Tcl_LreplaceObjCmd(dummy, interp, objc, objv)
2654 ClientData dummy; /* Not used. */
2655 Tcl_Interp *interp; /* Current interpreter. */
2656 int objc; /* Number of arguments. */
2657 Tcl_Obj *CONST objv[]; /* Argument objects. */
2658 {
2659 register Tcl_Obj *listPtr;
2660 int isDuplicate, first, last, listLen, numToDelete, result;
2661
2662 if (objc < 4) {
2663 Tcl_WrongNumArgs(interp, 1, objv,
2664 "list first last ?element element ...?");
2665 return TCL_ERROR;
2666 }
2667
2668 result = Tcl_ListObjLength(interp, objv[1], &listLen);
2669 if (result != TCL_OK) {
2670 return result;
2671 }
2672
2673 /*
2674 * Get the first and last indexes. "end" is interpreted to be the index
2675 * for the last element, such that using it will cause that element to
2676 * be included for deletion.
2677 */
2678
2679 result = TclGetIntForIndex(interp, objv[2], /*end*/ (listLen - 1), &first);
2680 if (result != TCL_OK) {
2681 return result;
2682 }
2683
2684 result = TclGetIntForIndex(interp, objv[3], /*end*/ (listLen - 1), &last);
2685 if (result != TCL_OK) {
2686 return result;
2687 }
2688
2689 if (first < 0) {
2690 first = 0;
2691 }
2692
2693 /*
2694 * Complain if the user asked for a start element that is greater than the
2695 * list length. This won't ever trigger for the "end*" case as that will
2696 * be properly constrained by TclGetIntForIndex because we use listLen-1
2697 * (to allow for replacing the last elem).
2698 */
2699
2700 if ((first >= listLen) && (listLen > 0)) {
2701 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
2702 "list doesn't contain element ",
2703 Tcl_GetString(objv[2]), (int *) NULL);
2704 return TCL_ERROR;
2705 }
2706 if (last >= listLen) {
2707 last = (listLen - 1);
2708 }
2709 if (first <= last) {
2710 numToDelete = (last - first + 1);
2711 } else {
2712 numToDelete = 0;
2713 }
2714
2715 /*
2716 * If the list object is unshared we can modify it directly, otherwise
2717 * we create a copy to modify: this is "copy on write".
2718 */
2719
2720 listPtr = objv[1];
2721 isDuplicate = 0;
2722 if (Tcl_IsShared(listPtr)) {
2723 listPtr = Tcl_DuplicateObj(listPtr);
2724 isDuplicate = 1;
2725 }
2726 if (objc > 4) {
2727 result = Tcl_ListObjReplace(interp, listPtr, first, numToDelete,
2728 (objc-4), &(objv[4]));
2729 } else {
2730 result = Tcl_ListObjReplace(interp, listPtr, first, numToDelete,
2731 0, NULL);
2732 }
2733 if (result != TCL_OK) {
2734 if (isDuplicate) {
2735 Tcl_DecrRefCount(listPtr); /* free unneeded obj */
2736 }
2737 return result;
2738 }
2739
2740 /*
2741 * Set the interpreter's object result.
2742 */
2743
2744 Tcl_SetObjResult(interp, listPtr);
2745 return TCL_OK;
2746 }
2747
2748 /*
2749 *----------------------------------------------------------------------
2750 *
2751 * Tcl_LsearchObjCmd --
2752 *
2753 * This procedure is invoked to process the "lsearch" Tcl command.
2754 * See the user documentation for details on what it does.
2755 *
2756 * Results:
2757 * A standard Tcl result.
2758 *
2759 * Side effects:
2760 * See the user documentation.
2761 *
2762 *----------------------------------------------------------------------
2763 */
2764
2765 int
2766 Tcl_LsearchObjCmd(clientData, interp, objc, objv)
2767 ClientData clientData; /* Not used. */
2768 Tcl_Interp *interp; /* Current interpreter. */
2769 int objc; /* Number of arguments. */
2770 Tcl_Obj *CONST objv[]; /* Argument values. */
2771 {
2772 char *bytes, *patternBytes;
2773 int i, match, mode, index, result, listc, length, elemLen;
2774 int dataType, isIncreasing, lower, upper, patInt, objInt;
2775 int offset, allMatches, inlineReturn, negatedMatch;
2776 double patDouble, objDouble;
2777 Tcl_Obj *patObj, **listv, *listPtr, *startPtr;
2778 Tcl_RegExp regexp = NULL;
2779 static CONST char *options[] = {
2780 "-all", "-ascii", "-decreasing", "-dictionary",
2781 "-exact", "-glob", "-increasing", "-inline",
2782 "-integer", "-not", "-real", "-regexp",
2783 "-sorted", "-start", NULL
2784 };
2785 enum options {
2786 LSEARCH_ALL, LSEARCH_ASCII, LSEARCH_DECREASING, LSEARCH_DICTIONARY,
2787 LSEARCH_EXACT, LSEARCH_GLOB, LSEARCH_INCREASING, LSEARCH_INLINE,
2788 LSEARCH_INTEGER, LSEARCH_NOT, LSEARCH_REAL, LSEARCH_REGEXP,
2789 LSEARCH_SORTED, LSEARCH_START
2790 };
2791 enum datatypes {
2792 ASCII, DICTIONARY, INTEGER, REAL
2793 };
2794 enum modes {
2795 EXACT, GLOB, REGEXP, SORTED
2796 };
2797
2798 mode = GLOB;
2799 dataType = ASCII;
2800 isIncreasing = 1;
2801 allMatches = 0;
2802 inlineReturn = 0;
2803 negatedMatch = 0;
2804 listPtr = NULL;
2805 startPtr = NULL;
2806 offset = 0;
2807
2808 if (objc < 3) {
2809 Tcl_WrongNumArgs(interp, 1, objv, "?options? list pattern");
2810 return TCL_ERROR;
2811 }
2812
2813 for (i = 1; i < objc-2; i++) {
2814 if (Tcl_GetIndexFromObj(interp, objv[i], options, "option", 0, &index)
2815 != TCL_OK) {
2816 if (startPtr) {
2817 Tcl_DecrRefCount(startPtr);
2818 }
2819 return TCL_ERROR;
2820 }
2821 switch ((enum options) index) {
2822 case LSEARCH_ALL: /* -all */
2823 allMatches = 1;
2824 break;
2825 case LSEARCH_ASCII: /* -ascii */
2826 dataType = ASCII;
2827 break;
2828 case LSEARCH_DECREASING: /* -decreasing */
2829 isIncreasing = 0;
2830 break;
2831 case LSEARCH_DICTIONARY: /* -dictionary */
2832 dataType = DICTIONARY;
2833 break;
2834 case LSEARCH_EXACT: /* -increasing */
2835 mode = EXACT;
2836 break;
2837 case LSEARCH_GLOB: /* -glob */
2838 mode = GLOB;
2839 break;
2840 case LSEARCH_INCREASING: /* -increasing */
2841 isIncreasing = 1;
2842 break;
2843 case LSEARCH_INLINE: /* -inline */
2844 inlineReturn = 1;
2845 break;
2846 case LSEARCH_INTEGER: /* -integer */
2847 dataType = INTEGER;
2848 break;
2849 case LSEARCH_NOT: /* -not */
2850 negatedMatch = 1;
2851 break;
2852 case LSEARCH_REAL: /* -real */
2853 dataType = REAL;
2854 break;
2855 case LSEARCH_REGEXP: /* -regexp */
2856 mode = REGEXP;
2857 break;
2858 case LSEARCH_SORTED: /* -sorted */
2859 mode = SORTED;
2860 break;
2861 case LSEARCH_START: /* -start */
2862 /*
2863 * If there was a previous -start option, release its saved
2864 * index because it will either be replaced or there will be
2865 * an error.
2866 */
2867 if (startPtr) {
2868 Tcl_DecrRefCount(startPtr);
2869 }
2870 if (i > objc-4) {
2871 Tcl_AppendResult(interp, "missing starting index", NULL);
2872 return TCL_ERROR;
2873 }
2874 i++;
2875 if (objv[i] == objv[objc - 2]) {
2876 /*
2877 * Take copy to prevent shimmering problems. Note
2878 * that it does not matter if the index obj is also a
2879 * component of the list being searched. We only need
2880 * to copy where the list and the index are
2881 * one-and-the-same.
2882 */
2883 startPtr = Tcl_DuplicateObj(objv[i]);
2884 } else {
2885 startPtr = objv[i];
2886 Tcl_IncrRefCount(startPtr);
2887 }
2888 }
2889 }
2890
2891 if ((enum modes) mode == REGEXP) {
2892 /*
2893 * We can shimmer regexp/list if listv[i] == pattern, so get the
2894 * regexp rep before the list rep.
2895 */
2896 regexp = Tcl_GetRegExpFromObj(interp, objv[objc - 1],
2897 TCL_REG_ADVANCED | TCL_REG_NOSUB);
2898 if (regexp == NULL) {
2899 if (startPtr) {
2900 Tcl_DecrRefCount(startPtr);
2901 }
2902 return TCL_ERROR;
2903 }
2904 }
2905
2906 /*
2907 * Make sure the list argument is a list object and get its length and
2908 * a pointer to its array of element pointers.
2909 */
2910
2911 result = Tcl_ListObjGetElements(interp, objv[objc - 2], &listc, &listv);
2912 if (result != TCL_OK) {
2913 if (startPtr) {
2914 Tcl_DecrRefCount(startPtr);
2915 }
2916 return result;
2917 }
2918
2919 /*
2920 * Get the user-specified start offset.
2921 */
2922 if (startPtr) {
2923 result = TclGetIntForIndex(interp, startPtr, listc-1, &offset);
2924 Tcl_DecrRefCount(startPtr);
2925 if (result != TCL_OK) {
2926 return result;
2927 }
2928 if (offset > listc-1) {
2929 offset = listc-1;
2930 }
2931 if (offset < 0) {
2932 offset = 0;
2933 }
2934 }
2935
2936 patObj = objv[objc - 1];
2937 patternBytes = NULL;
2938 if ((enum modes) mode == EXACT || (enum modes) mode == SORTED) {
2939 switch ((enum datatypes) dataType) {
2940 case ASCII:
2941 case DICTIONARY:
2942 patternBytes = Tcl_GetStringFromObj(patObj, &length);
2943 break;
2944 case INTEGER:
2945 result = Tcl_GetIntFromObj(interp, patObj, &patInt);
2946 if (result != TCL_OK) {
2947 return result;
2948 }
2949 break;
2950 case REAL:
2951 result = Tcl_GetDoubleFromObj(interp, patObj, &patDouble);
2952 if (result != TCL_OK) {
2953 return result;
2954 }
2955 break;
2956 }
2957 } else {
2958 patternBytes = Tcl_GetStringFromObj(patObj, &length);
2959 }
2960
2961 /*
2962 * Set default index value to -1, indicating failure; if we find the
2963 * item in the course of our search, index will be set to the correct
2964 * value.
2965 */
2966 index = -1;
2967 match = 0;
2968
2969 if ((enum modes) mode == SORTED && !allMatches && !negatedMatch) {
2970 /*
2971 * If the data is sorted, we can do a more intelligent search.
2972 * Note that there is no point in being smart when -all was
2973 * specified; in that case, we have to look at all items anyway,
2974 * and there is no sense in doing this when the match sense is
2975 * inverted.
2976 */
2977 lower = offset - 1;
2978 upper = listc;
2979 while (lower + 1 != upper) {
2980 i = (lower + upper)/2;
2981 switch ((enum datatypes) dataType) {
2982 case ASCII:
2983 bytes = Tcl_GetString(listv[i]);
2984 match = strcmp(patternBytes, bytes);
2985 break;
2986 case DICTIONARY:
2987 bytes = Tcl_GetString(listv[i]);
2988 match = DictionaryCompare(patternBytes, bytes);
2989 break;
2990 case INTEGER:
2991 result = Tcl_GetIntFromObj(interp, listv[i], &objInt);
2992 if (result != TCL_OK) {
2993 return result;
2994 }
2995 if (patInt == objInt) {
2996 match = 0;
2997 } else if (patInt < objInt) {
2998 match = -1;
2999 } else {
3000 match = 1;
3001 }
3002 break;
3003 case REAL:
3004 result = Tcl_GetDoubleFromObj(interp, listv[i], &objDouble);
3005 if (result != TCL_OK) {
3006 return result;
3007 }
3008 if (patDouble == objDouble) {
3009 match = 0;
3010 } else if (patDouble < objDouble) {
3011 match = -1;
3012 } else {
3013 match = 1;
3014 }
3015 break;
3016 }
3017 if (match == 0) {
3018 /*
3019 * Normally, binary search is written to stop when it
3020 * finds a match. If there are duplicates of an element in
3021 * the list, our first match might not be the first occurance.
3022 * Consider: 0 0 0 1 1 1 2 2 2
3023 * To maintain consistancy with standard lsearch semantics,
3024 * we must find the leftmost occurance of the pattern in the
3025 * list. Thus we don't just stop searching here. This
3026 * variation means that a search always makes log n
3027 * comparisons (normal binary search might "get lucky" with
3028 * an early comparison).
3029 */
3030 index = i;
3031 upper = i;
3032 } else if (match > 0) {
3033 if (isIncreasing) {
3034 lower = i;
3035 } else {
3036 upper = i;
3037 }
3038 } else {
3039 if (isIncreasing) {
3040 upper = i;
3041 } else {
3042 lower = i;
3043 }
3044 }
3045 }
3046
3047 } else {
3048 /*
3049 * We need to do a linear search, because (at least one) of:
3050 * - our matcher can only tell equal vs. not equal
3051 * - our matching sense is negated
3052 * - we're building a list of all matched items
3053 */
3054 if (allMatches) {
3055 listPtr = Tcl_NewListObj(0, (Tcl_Obj **) NULL);
3056 }
3057 for (i = offset; i < listc; i++) {
3058 match = 0;
3059 switch ((enum modes) mode) {
3060 case SORTED:
3061 case EXACT:
3062 switch ((enum datatypes) dataType) {
3063 case ASCII:
3064 bytes = Tcl_GetStringFromObj(listv[i], &elemLen);
3065 if (length == elemLen) {
3066 match = (memcmp(bytes, patternBytes,
3067 (size_t) length) == 0);
3068 }
3069 break;
3070 case DICTIONARY:
3071 bytes = Tcl_GetString(listv[i]);
3072 match = (DictionaryCompare(bytes, patternBytes) == 0);
3073 break;
3074 case INTEGER:
3075 result = Tcl_GetIntFromObj(interp, listv[i], &objInt);
3076 if (result != TCL_OK) {
3077 if (listPtr) {
3078 Tcl_DecrRefCount(listPtr);
3079 }
3080 return result;
3081 }
3082 match = (objInt == patInt);
3083 break;
3084 case REAL:
3085 result = Tcl_GetDoubleFromObj(interp, listv[i],
3086 &objDouble);
3087 if (result != TCL_OK) {
3088 if (listPtr) {
3089 Tcl_DecrRefCount(listPtr);
3090 }
3091 return result;
3092 }
3093 match = (objDouble == patDouble);
3094 break;
3095 }
3096 break;
3097 case GLOB:
3098 match = Tcl_StringMatch(Tcl_GetString(listv[i]),
3099 patternBytes);
3100 break;
3101 case REGEXP:
3102 match = Tcl_RegExpExecObj(interp, regexp, listv[i], 0, 0, 0);
3103 if (match < 0) {
3104 Tcl_DecrRefCount(patObj);
3105 if (listPtr) {
3106 Tcl_DecrRefCount(listPtr);
3107 }
3108 return TCL_ERROR;
3109 }
3110 break;
3111 }
3112 /*
3113 * Invert match condition for -not
3114 */
3115 if (negatedMatch) {
3116 match = !match;
3117 }
3118 if (match != 0) {
3119 if (!allMatches) {
3120 index = i;
3121 break;
3122 } else if (inlineReturn) {
3123 /*
3124 * Note that these appends are not expected to fail.
3125 */
3126 Tcl_ListObjAppendElement(interp, listPtr, listv[i]);
3127 } else {
3128 Tcl_ListObjAppendElement(interp, listPtr,
3129 Tcl_NewIntObj(i));
3130 }
3131 }
3132 }
3133 }
3134
3135 /*
3136 * Return everything or a single value.
3137 */
3138 if (allMatches) {
3139 Tcl_SetObjResult(interp, listPtr);
3140 } else if (!inlineReturn) {
3141 Tcl_SetIntObj(Tcl_GetObjResult(interp), index);
3142 } else if (index < 0) {
3143 /*
3144 * Is this superfluous? The result should be a blank object
3145 * by default...
3146 */
3147 Tcl_SetObjResult(interp, Tcl_NewObj());
3148 } else {
3149 Tcl_SetObjResult(interp, listv[index]);
3150 }
3151 return TCL_OK;
3152 }
3153
3154 /*
3155 *----------------------------------------------------------------------
3156 *
3157 * Tcl_LsetObjCmd --
3158 *
3159 * This procedure is invoked to process the "lset" Tcl command.
3160 * See the user documentation for details on what it does.
3161 *
3162 * Results:
3163 * A standard Tcl result.
3164 *
3165 * Side effects:
3166 * See the user documentation.
3167 *
3168 *----------------------------------------------------------------------
3169 */
3170
3171 int
3172 Tcl_LsetObjCmd( clientData, interp, objc, objv )
3173 ClientData clientData; /* Not used. */
3174 Tcl_Interp *interp; /* Current interpreter. */
3175 int objc; /* Number of arguments. */
3176 Tcl_Obj *CONST objv[]; /* Argument values. */
3177 {
3178
3179 Tcl_Obj* listPtr; /* Pointer to the list being altered. */
3180 Tcl_Obj* finalValuePtr; /* Value finally assigned to the variable */
3181
3182 /* Check parameter count */
3183
3184 if ( objc < 3 ) {
3185 Tcl_WrongNumArgs( interp, 1, objv, "listVar index ?index...? value" );
3186 return TCL_ERROR;
3187 }
3188
3189 /* Look up the list variable's value */
3190
3191 listPtr = Tcl_ObjGetVar2( interp, objv[ 1 ], (Tcl_Obj*) NULL,
3192 TCL_LEAVE_ERR_MSG );
3193 if ( listPtr == NULL ) {
3194 return TCL_ERROR;
3195 }
3196
3197 /*
3198 * Substitute the value in the value. Return either the value or
3199 * else an unshared copy of it.
3200 */
3201
3202 if ( objc == 4 ) {
3203 finalValuePtr = TclLsetList( interp, listPtr,
3204 objv[ 2 ], objv[ 3 ] );
3205 } else {
3206 finalValuePtr = TclLsetFlat( interp, listPtr,
3207 objc-3, objv+2, objv[ objc-1 ] );
3208 }
3209
3210 /*
3211 * If substitution has failed, bail out.
3212 */
3213
3214 if ( finalValuePtr == NULL ) {
3215 return TCL_ERROR;
3216 }
3217
3218 /* Finally, update the variable so that traces fire. */
3219
3220 listPtr = Tcl_ObjSetVar2( interp, objv[1], NULL, finalValuePtr,
3221 TCL_LEAVE_ERR_MSG );
3222 Tcl_DecrRefCount( finalValuePtr );
3223 if ( listPtr == NULL ) {
3224 return TCL_ERROR;
3225 }
3226
3227 /* Return the new value of the variable as the interpreter result. */
3228
3229 Tcl_SetObjResult( interp, listPtr );
3230 return TCL_OK;
3231
3232 }
3233
3234 /*
3235 *----------------------------------------------------------------------
3236 *
3237 * Tcl_LsortObjCmd --
3238 *
3239 * This procedure is invoked to process the "lsort" Tcl command.
3240 * See the user documentation for details on what it does.
3241 *
3242 * Results:
3243 * A standard Tcl result.
3244 *
3245 * Side effects:
3246 * See the user documentation.
3247 *
3248 *----------------------------------------------------------------------
3249 */
3250
3251 int
3252 Tcl_LsortObjCmd(clientData, interp, objc, objv)
3253 ClientData clientData; /* Not used. */
3254 Tcl_Interp *interp; /* Current interpreter. */
3255 int objc; /* Number of arguments. */
3256 Tcl_Obj *CONST objv[]; /* Argument values. */
3257 {
3258 int i, index, unique;
3259 Tcl_Obj *resultPtr;
3260 int length;
3261 Tcl_Obj *cmdPtr, **listObjPtrs;
3262 SortElement *elementArray;
3263 SortElement *elementPtr;
3264 SortInfo sortInfo; /* Information about this sort that
3265 * needs to be passed to the
3266 * comparison function */
3267 static CONST char *switches[] = {
3268 "-ascii", "-command", "-decreasing", "-dictionary", "-increasing",
3269 "-index", "-integer", "-real", "-unique", (char *) NULL
3270 };
3271
3272 resultPtr = Tcl_GetObjResult(interp);
3273 if (objc < 2) {
3274 Tcl_WrongNumArgs(interp, 1, objv, "?options? list");
3275 return TCL_ERROR;
3276 }
3277
3278 /*
3279 * Parse arguments to set up the mode for the sort.
3280 */
3281
3282 sortInfo.isIncreasing = 1;
3283 sortInfo.sortMode = SORTMODE_ASCII;
3284 sortInfo.index = SORTIDX_NONE;
3285 sortInfo.interp = interp;
3286 sortInfo.resultCode = TCL_OK;
3287 cmdPtr = NULL;
3288 unique = 0;
3289 for (i = 1; i < objc-1; i++) {
3290 if (Tcl_GetIndexFromObj(interp, objv[i], switches, "option", 0, &index)
3291 != TCL_OK) {
3292 return TCL_ERROR;
3293 }
3294 switch (index) {
3295 case 0: /* -ascii */
3296 sortInfo.sortMode = SORTMODE_ASCII;
3297 break;
3298 case 1: /* -command */
3299 if (i == (objc-2)) {
3300 Tcl_AppendToObj(resultPtr,
3301 "\"-command\" option must be followed by comparison command",
3302 -1);
3303 return TCL_ERROR;
3304 }
3305 sortInfo.sortMode = SORTMODE_COMMAND;
3306 cmdPtr = objv[i+1];
3307 i++;
3308 break;
3309 case 2: /* -decreasing */
3310 sortInfo.isIncreasing = 0;
3311 break;
3312 case 3: /* -dictionary */
3313 sortInfo.sortMode = SORTMODE_DICTIONARY;
3314 break;
3315 case 4: /* -increasing */
3316 sortInfo.isIncreasing = 1;
3317 break;
3318 case 5: /* -index */
3319 if (i == (objc-2)) {
3320 Tcl_AppendToObj(resultPtr,
3321 "\"-index\" option must be followed by list index",
3322 -1);
3323 return TCL_ERROR;
3324 }
3325 if (TclGetIntForIndex(interp, objv[i+1], SORTIDX_END,
3326 &sortInfo.index) != TCL_OK) {
3327 return TCL_ERROR;
3328 }
3329 i++;
3330 break;
3331 case 6: /* -integer */
3332 sortInfo.sortMode = SORTMODE_INTEGER;
3333 break;
3334 case 7: /* -real */
3335 sortInfo.sortMode = SORTMODE_REAL;
3336 break;
3337 case 8: /* -unique */
3338 unique = 1;
3339 break;
3340 }
3341 }
3342 if (sortInfo.sortMode == SORTMODE_COMMAND) {
3343 /*
3344 * The existing command is a list. We want to flatten it, append
3345 * two dummy arguments on the end, and replace these arguments
3346 * later.
3347 */
3348
3349 Tcl_Obj *newCommandPtr = Tcl_DuplicateObj(cmdPtr);
3350 Tcl_Obj *newObjPtr = Tcl_NewObj();
3351
3352 Tcl_IncrRefCount(newCommandPtr);
3353 if (Tcl_ListObjAppendElement(interp, newCommandPtr, newObjPtr)
3354 != TCL_OK) {
3355 Tcl_DecrRefCount(newCommandPtr);
3356 Tcl_IncrRefCount(newObjPtr);
3357 Tcl_DecrRefCount(newObjPtr);
3358 return TCL_ERROR;
3359 }
3360 Tcl_ListObjAppendElement(interp, newCommandPtr, Tcl_NewObj());
3361 sortInfo.compareCmdPtr = newCommandPtr;
3362 }
3363
3364 sortInfo.resultCode = Tcl_ListObjGetElements(interp, objv[objc-1],
3365 &length, &listObjPtrs);
3366 if (sortInfo.resultCode != TCL_OK || length <= 0) {
3367 goto done;
3368 }
3369 elementArray = (SortElement *) ckalloc(length * sizeof(SortElement));
3370 for (i=0; i < length; i++){
3371 elementArray[i].objPtr = listObjPtrs[i];
3372 elementArray[i].count = 0;
3373 elementArray[i].nextPtr = &elementArray[i+1];
3374 }
3375 elementArray[length-1].nextPtr = NULL;
3376 elementPtr = MergeSort(elementArray, &sortInfo);
3377 if (sortInfo.resultCode == TCL_OK) {
3378 /*
3379 * Note: must clear the interpreter's result object: it could
3380 * have been set by the -command script.
3381 */
3382
3383 Tcl_ResetResult(interp);
3384 resultPtr = Tcl_GetObjResult(interp);
3385 if (unique) {
3386 for (; elementPtr != NULL; elementPtr = elementPtr->nextPtr){
3387 if (elementPtr->count == 0) {
3388 Tcl_ListObjAppendElement(interp, resultPtr,
3389 elementPtr->objPtr);
3390 }
3391 }
3392 } else {
3393 for (; elementPtr != NULL; elementPtr = elementPtr->nextPtr){
3394 Tcl_ListObjAppendElement(interp, resultPtr,
3395 elementPtr->objPtr);
3396 }
3397 }
3398 }
3399 ckfree((char*) elementArray);
3400
3401 done:
3402 if (sortInfo.sortMode == SORTMODE_COMMAND) {
3403 Tcl_DecrRefCount(sortInfo.compareCmdPtr);
3404 sortInfo.compareCmdPtr = NULL;
3405 }
3406 return sortInfo.resultCode;
3407 }
3408
3409 /*
3410 *----------------------------------------------------------------------
3411 *
3412 * MergeSort -
3413 *
3414 * This procedure sorts a linked list of SortElement structures
3415 * use the merge-sort algorithm.
3416 *
3417 * Results:
3418 * A pointer to the head of the list after sorting is returned.
3419 *
3420 * Side effects:
3421 * None, unless a user-defined comparison command does something
3422 * weird.
3423 *
3424 *----------------------------------------------------------------------
3425 */
3426
3427 static SortElement *
3428 MergeSort(headPtr, infoPtr)
3429 SortElement *headPtr; /* First element on the list */
3430 SortInfo *infoPtr; /* Information needed by the
3431 * comparison operator */
3432 {
3433 /*
3434 * The subList array below holds pointers to temporary lists built
3435 * during the merge sort. Element i of the array holds a list of
3436 * length 2**i.
3437 */
3438
3439 # define NUM_LISTS 30
3440 SortElement *subList[NUM_LISTS];
3441 SortElement *elementPtr;
3442 int i;
3443
3444 for(i = 0; i < NUM_LISTS; i++){
3445 subList[i] = NULL;
3446 }
3447 while (headPtr != NULL) {
3448 elementPtr = headPtr;
3449 headPtr = headPtr->nextPtr;
3450 elementPtr->nextPtr = 0;
3451 for (i = 0; (i < NUM_LISTS) && (subList[i] != NULL); i++){
3452 elementPtr = MergeLists(subList[i], elementPtr, infoPtr);
3453 subList[i] = NULL;
3454 }
3455 if (i >= NUM_LISTS) {
3456 i = NUM_LISTS-1;
3457 }
3458 subList[i] = elementPtr;
3459 }
3460 elementPtr = NULL;
3461 for (i = 0; i < NUM_LISTS; i++){
3462 elementPtr = MergeLists(subList[i], elementPtr, infoPtr);
3463 }
3464 return elementPtr;
3465 }
3466
3467 /*
3468 *----------------------------------------------------------------------
3469 *
3470 * MergeLists -
3471 *
3472 * This procedure combines two sorted lists of SortElement structures
3473 * into a single sorted list.
3474 *
3475 * Results:
3476 * The unified list of SortElement structures.
3477 *
3478 * Side effects:
3479 * None, unless a user-defined comparison command does something
3480 * weird.
3481 *
3482 *----------------------------------------------------------------------
3483 */
3484
3485 static SortElement *
3486 MergeLists(leftPtr, rightPtr, infoPtr)
3487 SortElement *leftPtr; /* First list to be merged; may be
3488 * NULL. */
3489 SortElement *rightPtr; /* Second list to be merged; may be
3490 * NULL. */
3491 SortInfo *infoPtr; /* Information needed by the
3492 * comparison operator. */
3493 {
3494 SortElement *headPtr;
3495 SortElement *tailPtr;
3496 int cmp;
3497
3498 if (leftPtr == NULL) {
3499 return rightPtr;
3500 }
3501 if (rightPtr == NULL) {
3502 return leftPtr;
3503 }
3504 cmp = SortCompare(leftPtr->objPtr, rightPtr->objPtr, infoPtr);
3505 if (cmp > 0) {
3506 tailPtr = rightPtr;
3507 rightPtr = rightPtr->nextPtr;
3508 } else {
3509 if (cmp == 0) {
3510 leftPtr->count++;
3511 }
3512 tailPtr = leftPtr;
3513 leftPtr = leftPtr->nextPtr;
3514 }
3515 headPtr = tailPtr;
3516 while ((leftPtr != NULL) && (rightPtr != NULL)) {
3517 cmp = SortCompare(leftPtr->objPtr, rightPtr->objPtr, infoPtr);
3518 if (cmp > 0) {
3519 tailPtr->nextPtr = rightPtr;
3520 tailPtr = rightPtr;
3521 rightPtr = rightPtr->nextPtr;
3522 } else {
3523 if (cmp == 0) {
3524 leftPtr->count++;
3525 }
3526 tailPtr->nextPtr = leftPtr;
3527 tailPtr = leftPtr;
3528 leftPtr = leftPtr->nextPtr;
3529 }
3530 }
3531 if (leftPtr != NULL) {
3532 tailPtr->nextPtr = leftPtr;
3533 } else {
3534 tailPtr->nextPtr = rightPtr;
3535 }
3536 return headPtr;
3537 }
3538
3539 /*
3540 *----------------------------------------------------------------------
3541 *
3542 * SortCompare --
3543 *
3544 * This procedure is invoked by MergeLists to determine the proper
3545 * ordering between two elements.
3546 *
3547 * Results:
3548 * A negative results means the the first element comes before the
3549 * second, and a positive results means that the second element
3550 * should come first. A result of zero means the two elements
3551 * are equal and it doesn't matter which comes first.
3552 *
3553 * Side effects:
3554 * None, unless a user-defined comparison command does something
3555 * weird.
3556 *
3557 *----------------------------------------------------------------------
3558 */
3559
3560 static int
3561 SortCompare(objPtr1, objPtr2, infoPtr)
3562 Tcl_Obj *objPtr1, *objPtr2; /* Values to be compared. */
3563 SortInfo *infoPtr; /* Information passed from the
3564 * top-level "lsort" command */
3565 {
3566 int order, listLen, index;
3567 Tcl_Obj *objPtr;
3568 char buffer[TCL_INTEGER_SPACE];
3569
3570 order = 0;
3571 if (infoPtr->resultCode != TCL_OK) {
3572 /*
3573 * Once an error has occurred, skip any future comparisons
3574 * so as to preserve the error message in sortInterp->result.
3575 */
3576
3577 return order;
3578 }
3579 if (infoPtr->index != SORTIDX_NONE) {
3580 /*
3581 * The "-index" option was specified. Treat each object as a
3582 * list, extract the requested element from each list, and
3583 * compare the elements, not the lists. "end"-relative indices
3584 * are signaled here with large negative values.
3585 */
3586
3587 if (Tcl_ListObjLength(infoPtr->interp, objPtr1, &listLen) != TCL_OK) {
3588 infoPtr->resultCode = TCL_ERROR;
3589 return order;
3590 }
3591 if (infoPtr->index < SORTIDX_NONE) {
3592 index = listLen + infoPtr->index + 1;
3593 } else {
3594 index = infoPtr->index;
3595 }
3596
3597 if (Tcl_ListObjIndex(infoPtr->interp, objPtr1, index, &objPtr)
3598 != TCL_OK) {
3599 infoPtr->resultCode = TCL_ERROR;
3600 return order;
3601 }
3602 if (objPtr == NULL) {
3603 objPtr = objPtr1;
3604 missingElement:
3605 TclFormatInt(buffer, infoPtr->index);
3606 Tcl_AppendStringsToObj(Tcl_GetObjResult(infoPtr->interp),
3607 "element ", buffer, " missing from sublist \"",
3608 Tcl_GetString(objPtr), "\"", (char *) NULL);
3609 infoPtr->resultCode = TCL_ERROR;
3610 return order;
3611 }
3612 objPtr1 = objPtr;
3613
3614 if (Tcl_ListObjLength(infoPtr->interp, objPtr2, &listLen) != TCL_OK) {
3615 infoPtr->resultCode = TCL_ERROR;
3616 return order;
3617 }
3618 if (infoPtr->index < SORTIDX_NONE) {
3619 index = listLen + infoPtr->index + 1;
3620 } else {
3621 index = infoPtr->index;
3622 }
3623
3624 if (Tcl_ListObjIndex(infoPtr->interp, objPtr2, index, &objPtr)
3625 != TCL_OK) {
3626 infoPtr->resultCode = TCL_ERROR;
3627 return order;
3628 }
3629 if (objPtr == NULL) {
3630 objPtr = objPtr2;
3631 goto missingElement;
3632 }
3633 objPtr2 = objPtr;
3634 }
3635 if (infoPtr->sortMode == SORTMODE_ASCII) {
3636 order = strcmp(Tcl_GetString(objPtr1), Tcl_GetString(objPtr2));
3637 } else if (infoPtr->sortMode == SORTMODE_DICTIONARY) {
3638 order = DictionaryCompare(
3639 Tcl_GetString(objPtr1), Tcl_GetString(objPtr2));
3640 } else if (infoPtr->sortMode == SORTMODE_INTEGER) {
3641 long a, b;
3642
3643 if ((Tcl_GetLongFromObj(infoPtr->interp, objPtr1, &a) != TCL_OK)
3644 || (Tcl_GetLongFromObj(infoPtr->interp, objPtr2, &b)
3645 != TCL_OK)) {
3646 infoPtr->resultCode = TCL_ERROR;
3647 return order;
3648 }
3649 if (a > b) {
3650 order = 1;
3651 } else if (b > a) {
3652 order = -1;
3653 }
3654 } else if (infoPtr->sortMode == SORTMODE_REAL) {
3655 double a, b;
3656
3657 if ((Tcl_GetDoubleFromObj(infoPtr->interp, objPtr1, &a) != TCL_OK)
3658 || (Tcl_GetDoubleFromObj(infoPtr->interp, objPtr2, &b)
3659 != TCL_OK)) {
3660 infoPtr->resultCode = TCL_ERROR;
3661 return order;
3662 }
3663 if (a > b) {
3664 order = 1;
3665 } else if (b > a) {
3666 order = -1;
3667 }
3668 } else {
3669 Tcl_Obj **objv, *paramObjv[2];
3670 int objc;
3671
3672 paramObjv[0] = objPtr1;
3673 paramObjv[1] = objPtr2;
3674
3675 /*
3676 * We made space in the command list for the two things to
3677 * compare. Replace them and evaluate the result.
3678 */
3679
3680 Tcl_ListObjLength(infoPtr->interp, infoPtr->compareCmdPtr, &objc);
3681 Tcl_ListObjReplace(infoPtr->interp, infoPtr->compareCmdPtr, objc - 2,
3682 2, 2, paramObjv);
3683 Tcl_ListObjGetElements(infoPtr->interp, infoPtr->compareCmdPtr,
3684 &objc, &objv);
3685
3686 infoPtr->resultCode = Tcl_EvalObjv(infoPtr->interp, objc, objv, 0);
3687
3688 if (infoPtr->resultCode != TCL_OK) {
3689 Tcl_AddErrorInfo(infoPtr->interp,
3690 "\n (-compare command)");
3691 return order;
3692 }
3693
3694 /*
3695 * Parse the result of the command.
3696 */
3697
3698 if (Tcl_GetIntFromObj(infoPtr->interp,
3699 Tcl_GetObjResult(infoPtr->interp), &order) != TCL_OK) {
3700 Tcl_ResetResult(infoPtr->interp);
3701 Tcl_AppendToObj(Tcl_GetObjResult(infoPtr->interp),
3702 "-compare command returned non-integer result", -1);
3703 infoPtr->resultCode = TCL_ERROR;
3704 return order;
3705 }
3706 }
3707 if (!infoPtr->isIncreasing) {
3708 order = -order;
3709 }
3710 return order;
3711 }
3712
3713 /*
3714 *----------------------------------------------------------------------
3715 *
3716 * DictionaryCompare
3717 *
3718 * This function compares two strings as if they were being used in
3719 * an index or card catalog. The case of alphabetic characters is
3720 * ignored, except to break ties. Thus "B" comes before "b" but
3721 * after "a". Also, integers embedded in the strings compare in
3722 * numerical order. In other words, "x10y" comes after "x9y", not
3723 * before it as it would when using strcmp().
3724 *
3725 * Results:
3726 * A negative result means that the first element comes before the
3727 * second, and a positive result means that the second element
3728 * should come first. A result of zero means the two elements
3729 * are equal and it doesn't matter which comes first.
3730 *
3731 * Side effects:
3732 * None.
3733 *
3734 *----------------------------------------------------------------------
3735 */
3736
3737 static int
3738 DictionaryCompare(left, right)
3739 char *left, *right; /* The strings to compare */
3740 {
3741 Tcl_UniChar uniLeft, uniRight, uniLeftLower, uniRightLower;
3742 int diff, zeros;
3743 int secondaryDiff = 0;
3744
3745 while (1) {
3746 if (isdigit(UCHAR(*right)) /* INTL: digit */
3747 && isdigit(UCHAR(*left))) { /* INTL: digit */
3748 /*
3749 * There are decimal numbers embedded in the two
3750 * strings. Compare them as numbers, rather than
3751 * strings. If one number has more leading zeros than
3752 * the other, the number with more leading zeros sorts
3753 * later, but only as a secondary choice.
3754 */
3755
3756 zeros = 0;
3757 while ((*right == '0') && (isdigit(UCHAR(right[1])))) {
3758 right++;
3759 zeros--;
3760 }
3761 while ((*left == '0') && (isdigit(UCHAR(left[1])))) {
3762 left++;
3763 zeros++;
3764 }
3765 if (secondaryDiff == 0) {
3766 secondaryDiff = zeros;
3767 }
3768
3769 /*
3770 * The code below compares the numbers in the two
3771 * strings without ever converting them to integers. It
3772 * does this by first comparing the lengths of the
3773 * numbers and then comparing the digit values.
3774 */
3775
3776 diff = 0;
3777 while (1) {
3778 if (diff == 0) {
3779 diff = UCHAR(*left) - UCHAR(*right);
3780 }
3781 right++;
3782 left++;
3783 if (!isdigit(UCHAR(*right))) { /* INTL: digit */
3784 if (isdigit(UCHAR(*left))) { /* INTL: digit */
3785 return 1;
3786 } else {
3787 /*
3788 * The two numbers have the same length. See
3789 * if their values are different.
3790 */
3791
3792 if (diff != 0) {
3793 return diff;
3794 }
3795 break;
3796 }
3797 } else if (!isdigit(UCHAR(*left))) { /* INTL: digit */
3798 return -1;
3799 }
3800 }
3801 continue;
3802 }
3803
3804 /*
3805 * Convert character to Unicode for comparison purposes. If either
3806 * string is at the terminating null, do a byte-wise comparison and
3807 * bail out immediately.
3808 */
3809
3810 if ((*left != '\0') && (*right != '\0')) {
3811 left += Tcl_UtfToUniChar(left, &uniLeft);
3812 right += Tcl_UtfToUniChar(right, &uniRight);
3813 /*
3814 * Convert both chars to lower for the comparison, because
3815 * dictionary sorts are case insensitve. Covert to lower, not
3816 * upper, so chars between Z and a will sort before A (where most
3817 * other interesting punctuations occur)
3818 */
3819 uniLeftLower = Tcl_UniCharToLower(uniLeft);
3820 uniRightLower = Tcl_UniCharToLower(uniRight);
3821 } else {
3822 diff = UCHAR(*left) - UCHAR(*right);
3823 break;
3824 }
3825
3826 diff = uniLeftLower - uniRightLower;
3827 if (diff) {
3828 return diff;
3829 } else if (secondaryDiff == 0) {
3830 if (Tcl_UniCharIsUpper(uniLeft) &&
3831 Tcl_UniCharIsLower(uniRight)) {
3832 secondaryDiff = -1;
3833 } else if (Tcl_UniCharIsUpper(uniRight)
3834 && Tcl_UniCharIsLower(uniLeft)) {
3835 secondaryDiff = 1;
3836 }
3837 }
3838 }
3839 if (diff == 0) {
3840 diff = secondaryDiff;
3841 }
3842 return diff;
3843 }