| 1 |
/* |
| 2 |
* tclCmdIL.c -- |
| 3 |
* |
| 4 |
* This file contains the top-level command routines for most of the Tcl |
| 5 |
* built-in commands whose names begin with the letters I through L. It |
| 6 |
* contains only commands in the generic core (i.e., those that don't |
| 7 |
* 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 |
* Copyright (c) 2005 Donal K. Fellows. |
| 15 |
* |
| 16 |
* See the file "license.terms" for information on usage and redistribution of |
| 17 |
* this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "tclInt.h" |
| 21 |
#include "tclRegexp.h" |
| 22 |
|
| 23 |
/* |
| 24 |
* During execution of the "lsort" command, structures of the following type |
| 25 |
* are used to arrange the objects being sorted into a collection of linked |
| 26 |
* lists. |
| 27 |
*/ |
| 28 |
|
| 29 |
typedef struct SortElement { |
| 30 |
union { /* The value that we sorting by. */ |
| 31 |
const char *strValuePtr; |
| 32 |
Tcl_WideInt wideValue; |
| 33 |
double doubleValue; |
| 34 |
Tcl_Obj *objValuePtr; |
| 35 |
} collationKey; |
| 36 |
union { /* Object being sorted, or its index. */ |
| 37 |
Tcl_Obj *objPtr; |
| 38 |
int index; |
| 39 |
} payload; |
| 40 |
struct SortElement *nextPtr;/* Next element in the list, or NULL for end |
| 41 |
* of list. */ |
| 42 |
} SortElement; |
| 43 |
|
| 44 |
/* |
| 45 |
* These function pointer types are used with the "lsearch" and "lsort" |
| 46 |
* commands to facilitate the "-nocase" option. |
| 47 |
*/ |
| 48 |
|
| 49 |
typedef int (*SortStrCmpFn_t) (const char *, const char *); |
| 50 |
typedef int (*SortMemCmpFn_t) (const void *, const void *, size_t); |
| 51 |
|
| 52 |
/* |
| 53 |
* The "lsort" command needs to pass certain information down to the function |
| 54 |
* that compares two list elements, and the comparison function needs to pass |
| 55 |
* success or failure information back up to the top-level "lsort" command. |
| 56 |
* The following structure is used to pass this information. |
| 57 |
*/ |
| 58 |
|
| 59 |
typedef struct SortInfo { |
| 60 |
int isIncreasing; /* Nonzero means sort in increasing order. */ |
| 61 |
int sortMode; /* The sort mode. One of SORTMODE_* values |
| 62 |
* defined below. */ |
| 63 |
Tcl_Obj *compareCmdPtr; /* The Tcl comparison command when sortMode is |
| 64 |
* SORTMODE_COMMAND. Preinitialized to hold |
| 65 |
* base of command. */ |
| 66 |
int *indexv; /* If the -index option was specified, this |
| 67 |
* holds an encoding of the indexes contained |
| 68 |
* in the list supplied as an argument to |
| 69 |
* that option. |
| 70 |
* NULL if no indexes supplied, and points to |
| 71 |
* singleIndex field when only one |
| 72 |
* supplied. */ |
| 73 |
int indexc; /* Number of indexes in indexv array. */ |
| 74 |
int singleIndex; /* Static space for common index case. */ |
| 75 |
int unique; |
| 76 |
int numElements; |
| 77 |
Tcl_Interp *interp; /* The interpreter in which the sort is being |
| 78 |
* done. */ |
| 79 |
int resultCode; /* Completion code for the lsort command. If |
| 80 |
* an error occurs during the sort this is |
| 81 |
* changed from TCL_OK to TCL_ERROR. */ |
| 82 |
} SortInfo; |
| 83 |
|
| 84 |
/* |
| 85 |
* The "sortMode" field of the SortInfo structure can take on any of the |
| 86 |
* following values. |
| 87 |
*/ |
| 88 |
|
| 89 |
#define SORTMODE_ASCII 0 |
| 90 |
#define SORTMODE_INTEGER 1 |
| 91 |
#define SORTMODE_REAL 2 |
| 92 |
#define SORTMODE_COMMAND 3 |
| 93 |
#define SORTMODE_DICTIONARY 4 |
| 94 |
#define SORTMODE_ASCII_NC 8 |
| 95 |
|
| 96 |
/* |
| 97 |
* Forward declarations for procedures defined in this file: |
| 98 |
*/ |
| 99 |
|
| 100 |
static int DictionaryCompare(const char *left, const char *right); |
| 101 |
static Tcl_NRPostProc IfConditionCallback; |
| 102 |
static int InfoArgsCmd(ClientData dummy, Tcl_Interp *interp, |
| 103 |
int objc, Tcl_Obj *const objv[]); |
| 104 |
static int InfoBodyCmd(ClientData dummy, Tcl_Interp *interp, |
| 105 |
int objc, Tcl_Obj *const objv[]); |
| 106 |
static int InfoCmdCountCmd(ClientData dummy, Tcl_Interp *interp, |
| 107 |
int objc, Tcl_Obj *const objv[]); |
| 108 |
static int InfoCommandsCmd(ClientData dummy, Tcl_Interp *interp, |
| 109 |
int objc, Tcl_Obj *const objv[]); |
| 110 |
static int InfoCompleteCmd(ClientData dummy, Tcl_Interp *interp, |
| 111 |
int objc, Tcl_Obj *const objv[]); |
| 112 |
static int InfoDefaultCmd(ClientData dummy, Tcl_Interp *interp, |
| 113 |
int objc, Tcl_Obj *const objv[]); |
| 114 |
/* TIP #348 - New 'info' subcommand 'errorstack' */ |
| 115 |
static int InfoErrorStackCmd(ClientData dummy, Tcl_Interp *interp, |
| 116 |
int objc, Tcl_Obj *const objv[]); |
| 117 |
/* TIP #280 - New 'info' subcommand 'frame' */ |
| 118 |
static int InfoFrameCmd(ClientData dummy, Tcl_Interp *interp, |
| 119 |
int objc, Tcl_Obj *const objv[]); |
| 120 |
static int InfoFunctionsCmd(ClientData dummy, Tcl_Interp *interp, |
| 121 |
int objc, Tcl_Obj *const objv[]); |
| 122 |
static int InfoHostnameCmd(ClientData dummy, Tcl_Interp *interp, |
| 123 |
int objc, Tcl_Obj *const objv[]); |
| 124 |
static int InfoLevelCmd(ClientData dummy, Tcl_Interp *interp, |
| 125 |
int objc, Tcl_Obj *const objv[]); |
| 126 |
static int InfoLibraryCmd(ClientData dummy, Tcl_Interp *interp, |
| 127 |
int objc, Tcl_Obj *const objv[]); |
| 128 |
static int InfoLoadedCmd(ClientData dummy, Tcl_Interp *interp, |
| 129 |
int objc, Tcl_Obj *const objv[]); |
| 130 |
static int InfoNameOfExecutableCmd(ClientData dummy, |
| 131 |
Tcl_Interp *interp, int objc, |
| 132 |
Tcl_Obj *const objv[]); |
| 133 |
static int InfoPatchLevelCmd(ClientData dummy, Tcl_Interp *interp, |
| 134 |
int objc, Tcl_Obj *const objv[]); |
| 135 |
static int InfoProcsCmd(ClientData dummy, Tcl_Interp *interp, |
| 136 |
int objc, Tcl_Obj *const objv[]); |
| 137 |
static int InfoScriptCmd(ClientData dummy, Tcl_Interp *interp, |
| 138 |
int objc, Tcl_Obj *const objv[]); |
| 139 |
static int InfoSharedlibCmd(ClientData dummy, Tcl_Interp *interp, |
| 140 |
int objc, Tcl_Obj *const objv[]); |
| 141 |
static int InfoTclVersionCmd(ClientData dummy, Tcl_Interp *interp, |
| 142 |
int objc, Tcl_Obj *const objv[]); |
| 143 |
static SortElement * MergeLists(SortElement *leftPtr, SortElement *rightPtr, |
| 144 |
SortInfo *infoPtr); |
| 145 |
static int SortCompare(SortElement *firstPtr, SortElement *second, |
| 146 |
SortInfo *infoPtr); |
| 147 |
static Tcl_Obj * SelectObjFromSublist(Tcl_Obj *firstPtr, |
| 148 |
SortInfo *infoPtr); |
| 149 |
|
| 150 |
/* |
| 151 |
* Array of values describing how to implement each standard subcommand of the |
| 152 |
* "info" command. |
| 153 |
*/ |
| 154 |
|
| 155 |
static const EnsembleImplMap defaultInfoMap[] = { |
| 156 |
{"args", InfoArgsCmd, TclCompileBasic1ArgCmd, NULL, NULL, 0}, |
| 157 |
{"body", InfoBodyCmd, TclCompileBasic1ArgCmd, NULL, NULL, 0}, |
| 158 |
{"cmdcount", InfoCmdCountCmd, TclCompileBasic0ArgCmd, NULL, NULL, 0}, |
| 159 |
{"commands", InfoCommandsCmd, TclCompileInfoCommandsCmd, NULL, NULL, 0}, |
| 160 |
{"complete", InfoCompleteCmd, TclCompileBasic1ArgCmd, NULL, NULL, 0}, |
| 161 |
{"coroutine", TclInfoCoroutineCmd, TclCompileInfoCoroutineCmd, NULL, NULL, 0}, |
| 162 |
{"default", InfoDefaultCmd, TclCompileBasic3ArgCmd, NULL, NULL, 0}, |
| 163 |
{"errorstack", InfoErrorStackCmd, TclCompileBasic0Or1ArgCmd, NULL, NULL, 0}, |
| 164 |
{"exists", TclInfoExistsCmd, TclCompileInfoExistsCmd, NULL, NULL, 0}, |
| 165 |
{"frame", InfoFrameCmd, TclCompileBasic0Or1ArgCmd, NULL, NULL, 0}, |
| 166 |
{"functions", InfoFunctionsCmd, TclCompileBasic0Or1ArgCmd, NULL, NULL, 0}, |
| 167 |
{"globals", TclInfoGlobalsCmd, TclCompileBasic0Or1ArgCmd, NULL, NULL, 0}, |
| 168 |
{"hostname", InfoHostnameCmd, TclCompileBasic0ArgCmd, NULL, NULL, 0}, |
| 169 |
{"level", InfoLevelCmd, TclCompileInfoLevelCmd, NULL, NULL, 0}, |
| 170 |
{"library", InfoLibraryCmd, TclCompileBasic0ArgCmd, NULL, NULL, 0}, |
| 171 |
{"loaded", InfoLoadedCmd, TclCompileBasic0Or1ArgCmd, NULL, NULL, 0}, |
| 172 |
{"locals", TclInfoLocalsCmd, TclCompileBasic0Or1ArgCmd, NULL, NULL, 0}, |
| 173 |
{"nameofexecutable", InfoNameOfExecutableCmd, TclCompileBasic0ArgCmd, NULL, NULL, 0}, |
| 174 |
{"patchlevel", InfoPatchLevelCmd, TclCompileBasic0ArgCmd, NULL, NULL, 0}, |
| 175 |
{"procs", InfoProcsCmd, TclCompileBasic0Or1ArgCmd, NULL, NULL, 0}, |
| 176 |
{"script", InfoScriptCmd, TclCompileBasic0Or1ArgCmd, NULL, NULL, 0}, |
| 177 |
{"sharedlibextension", InfoSharedlibCmd, TclCompileBasic0ArgCmd, NULL, NULL, 0}, |
| 178 |
{"tclversion", InfoTclVersionCmd, TclCompileBasic0ArgCmd, NULL, NULL, 0}, |
| 179 |
{"vars", TclInfoVarsCmd, TclCompileBasic0Or1ArgCmd, NULL, NULL, 0}, |
| 180 |
{NULL, NULL, NULL, NULL, NULL, 0} |
| 181 |
}; |
| 182 |
|
| 183 |
/* |
| 184 |
*---------------------------------------------------------------------- |
| 185 |
* |
| 186 |
* Tcl_IfObjCmd -- |
| 187 |
* |
| 188 |
* This procedure is invoked to process the "if" Tcl command. See the |
| 189 |
* user documentation for details on what it does. |
| 190 |
* |
| 191 |
* With the bytecode compiler, this procedure is only called when a |
| 192 |
* command name is computed at runtime, and is "if" or the name to which |
| 193 |
* "if" was renamed: e.g., "set z if; $z 1 {puts foo}" |
| 194 |
* |
| 195 |
* Results: |
| 196 |
* A standard Tcl result. |
| 197 |
* |
| 198 |
* Side effects: |
| 199 |
* See the user documentation. |
| 200 |
* |
| 201 |
*---------------------------------------------------------------------- |
| 202 |
*/ |
| 203 |
|
| 204 |
int |
| 205 |
Tcl_IfObjCmd( |
| 206 |
ClientData dummy, /* Not used. */ |
| 207 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 208 |
int objc, /* Number of arguments. */ |
| 209 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 210 |
{ |
| 211 |
return Tcl_NRCallObjProc(interp, TclNRIfObjCmd, dummy, objc, objv); |
| 212 |
} |
| 213 |
|
| 214 |
int |
| 215 |
TclNRIfObjCmd( |
| 216 |
ClientData dummy, /* Not used. */ |
| 217 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 218 |
int objc, /* Number of arguments. */ |
| 219 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 220 |
{ |
| 221 |
Tcl_Obj *boolObj; |
| 222 |
|
| 223 |
if (objc <= 1) { |
| 224 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 225 |
"wrong # args: no expression after \"%s\" argument", |
| 226 |
TclGetString(objv[0]))); |
| 227 |
Tcl_SetErrorCode(interp, "TCL", "WRONGARGS", (char *)NULL); |
| 228 |
return TCL_ERROR; |
| 229 |
} |
| 230 |
|
| 231 |
/* |
| 232 |
* At this point, objv[1] refers to the main expression to test. The |
| 233 |
* arguments after the expression must be "then" (optional) and a script |
| 234 |
* to execute if the expression is true. |
| 235 |
*/ |
| 236 |
|
| 237 |
TclNewObj(boolObj); |
| 238 |
Tcl_NRAddCallback(interp, IfConditionCallback, INT2PTR(objc), |
| 239 |
(ClientData)objv, INT2PTR(1), boolObj); |
| 240 |
return Tcl_NRExprObj(interp, objv[1], boolObj); |
| 241 |
} |
| 242 |
|
| 243 |
static int |
| 244 |
IfConditionCallback( |
| 245 |
ClientData data[], |
| 246 |
Tcl_Interp *interp, |
| 247 |
int result) |
| 248 |
{ |
| 249 |
Interp *iPtr = (Interp *) interp; |
| 250 |
int objc = PTR2INT(data[0]); |
| 251 |
Tcl_Obj *const *objv = (Tcl_Obj *const *)data[1]; |
| 252 |
int i = PTR2INT(data[2]); |
| 253 |
Tcl_Obj *boolObj = (Tcl_Obj *)data[3]; |
| 254 |
int value, thenScriptIndex = 0; |
| 255 |
const char *clause; |
| 256 |
|
| 257 |
if (result != TCL_OK) { |
| 258 |
TclDecrRefCount(boolObj); |
| 259 |
return result; |
| 260 |
} |
| 261 |
if (Tcl_GetBooleanFromObj(interp, boolObj, &value) != TCL_OK) { |
| 262 |
TclDecrRefCount(boolObj); |
| 263 |
return TCL_ERROR; |
| 264 |
} |
| 265 |
TclDecrRefCount(boolObj); |
| 266 |
|
| 267 |
while (1) { |
| 268 |
i++; |
| 269 |
if (i >= objc) { |
| 270 |
goto missingScript; |
| 271 |
} |
| 272 |
clause = TclGetString(objv[i]); |
| 273 |
if ((i < objc) && (strcmp(clause, "then") == 0)) { |
| 274 |
i++; |
| 275 |
} |
| 276 |
if (i >= objc) { |
| 277 |
goto missingScript; |
| 278 |
} |
| 279 |
if (value) { |
| 280 |
thenScriptIndex = i; |
| 281 |
value = 0; |
| 282 |
} |
| 283 |
|
| 284 |
/* |
| 285 |
* The expression evaluated to false. Skip the command, then see if |
| 286 |
* there is an "else" or "elseif" clause. |
| 287 |
*/ |
| 288 |
|
| 289 |
i++; |
| 290 |
if (i >= objc) { |
| 291 |
if (thenScriptIndex) { |
| 292 |
/* |
| 293 |
* TIP #280. Make invoking context available to branch. |
| 294 |
*/ |
| 295 |
|
| 296 |
return TclNREvalObjEx(interp, objv[thenScriptIndex], 0, |
| 297 |
iPtr->cmdFramePtr, thenScriptIndex); |
| 298 |
} |
| 299 |
return TCL_OK; |
| 300 |
} |
| 301 |
clause = TclGetString(objv[i]); |
| 302 |
if ((clause[0] != 'e') || (strcmp(clause, "elseif") != 0)) { |
| 303 |
break; |
| 304 |
} |
| 305 |
i++; |
| 306 |
|
| 307 |
/* |
| 308 |
* At this point in the loop, objv and objc refer to an expression to |
| 309 |
* test, either for the main expression or an expression following an |
| 310 |
* "elseif". The arguments after the expression must be "then" |
| 311 |
* (optional) and a script to execute if the expression is true. |
| 312 |
*/ |
| 313 |
|
| 314 |
if (i >= objc) { |
| 315 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 316 |
"wrong # args: no expression after \"%s\" argument", |
| 317 |
clause)); |
| 318 |
Tcl_SetErrorCode(interp, "TCL", "WRONGARGS", (char *)NULL); |
| 319 |
return TCL_ERROR; |
| 320 |
} |
| 321 |
if (!thenScriptIndex) { |
| 322 |
TclNewObj(boolObj); |
| 323 |
Tcl_NRAddCallback(interp, IfConditionCallback, data[0], data[1], |
| 324 |
INT2PTR(i), boolObj); |
| 325 |
return Tcl_NRExprObj(interp, objv[i], boolObj); |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
/* |
| 330 |
* Couldn't find a "then" or "elseif" clause to execute. Check now for an |
| 331 |
* "else" clause. We know that there's at least one more argument when we |
| 332 |
* get here. |
| 333 |
*/ |
| 334 |
|
| 335 |
if (strcmp(clause, "else") == 0) { |
| 336 |
i++; |
| 337 |
if (i >= objc) { |
| 338 |
goto missingScript; |
| 339 |
} |
| 340 |
} |
| 341 |
if (i < objc - 1) { |
| 342 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 343 |
"wrong # args: extra words after \"else\" clause in \"if\" command", |
| 344 |
-1)); |
| 345 |
Tcl_SetErrorCode(interp, "TCL", "WRONGARGS", (char *)NULL); |
| 346 |
return TCL_ERROR; |
| 347 |
} |
| 348 |
if (thenScriptIndex) { |
| 349 |
/* |
| 350 |
* TIP #280. Make invoking context available to branch/else. |
| 351 |
*/ |
| 352 |
|
| 353 |
return TclNREvalObjEx(interp, objv[thenScriptIndex], 0, |
| 354 |
iPtr->cmdFramePtr, thenScriptIndex); |
| 355 |
} |
| 356 |
return TclNREvalObjEx(interp, objv[i], 0, iPtr->cmdFramePtr, i); |
| 357 |
|
| 358 |
missingScript: |
| 359 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 360 |
"wrong # args: no script following \"%s\" argument", |
| 361 |
TclGetString(objv[i-1]))); |
| 362 |
Tcl_SetErrorCode(interp, "TCL", "WRONGARGS", (char *)NULL); |
| 363 |
return TCL_ERROR; |
| 364 |
} |
| 365 |
|
| 366 |
/* |
| 367 |
*---------------------------------------------------------------------- |
| 368 |
* |
| 369 |
* Tcl_IncrObjCmd -- |
| 370 |
* |
| 371 |
* This procedure is invoked to process the "incr" Tcl command. See the |
| 372 |
* user documentation for details on what it does. |
| 373 |
* |
| 374 |
* With the bytecode compiler, this procedure is only called when a |
| 375 |
* command name is computed at runtime, and is "incr" or the name to |
| 376 |
* which "incr" was renamed: e.g., "set z incr; $z i -1" |
| 377 |
* |
| 378 |
* Results: |
| 379 |
* A standard Tcl result. |
| 380 |
* |
| 381 |
* Side effects: |
| 382 |
* See the user documentation. |
| 383 |
* |
| 384 |
*---------------------------------------------------------------------- |
| 385 |
*/ |
| 386 |
|
| 387 |
int |
| 388 |
Tcl_IncrObjCmd( |
| 389 |
ClientData dummy, /* Not used. */ |
| 390 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 391 |
int objc, /* Number of arguments. */ |
| 392 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 393 |
{ |
| 394 |
Tcl_Obj *newValuePtr, *incrPtr; |
| 395 |
|
| 396 |
if ((objc != 2) && (objc != 3)) { |
| 397 |
Tcl_WrongNumArgs(interp, 1, objv, "varName ?increment?"); |
| 398 |
return TCL_ERROR; |
| 399 |
} |
| 400 |
|
| 401 |
if (objc == 3) { |
| 402 |
incrPtr = objv[2]; |
| 403 |
} else { |
| 404 |
TclNewIntObj(incrPtr, 1); |
| 405 |
} |
| 406 |
Tcl_IncrRefCount(incrPtr); |
| 407 |
newValuePtr = TclIncrObjVar2(interp, objv[1], NULL, |
| 408 |
incrPtr, TCL_LEAVE_ERR_MSG); |
| 409 |
Tcl_DecrRefCount(incrPtr); |
| 410 |
|
| 411 |
if (newValuePtr == NULL) { |
| 412 |
return TCL_ERROR; |
| 413 |
} |
| 414 |
|
| 415 |
/* |
| 416 |
* Set the interpreter's object result to refer to the variable's new |
| 417 |
* value object. |
| 418 |
*/ |
| 419 |
|
| 420 |
Tcl_SetObjResult(interp, newValuePtr); |
| 421 |
return TCL_OK; |
| 422 |
} |
| 423 |
|
| 424 |
/* |
| 425 |
*---------------------------------------------------------------------- |
| 426 |
* |
| 427 |
* TclInitInfoCmd -- |
| 428 |
* |
| 429 |
* This function is called to create the "info" Tcl command. See the user |
| 430 |
* documentation for details on what it does. |
| 431 |
* |
| 432 |
* Results: |
| 433 |
* Handle for the info command, or NULL on failure. |
| 434 |
* |
| 435 |
* Side effects: |
| 436 |
* none |
| 437 |
* |
| 438 |
*---------------------------------------------------------------------- |
| 439 |
*/ |
| 440 |
|
| 441 |
Tcl_Command |
| 442 |
TclInitInfoCmd( |
| 443 |
Tcl_Interp *interp) /* Current interpreter. */ |
| 444 |
{ |
| 445 |
return TclMakeEnsemble(interp, "info", defaultInfoMap); |
| 446 |
} |
| 447 |
|
| 448 |
/* |
| 449 |
*---------------------------------------------------------------------- |
| 450 |
* |
| 451 |
* InfoArgsCmd -- |
| 452 |
* |
| 453 |
* Called to implement the "info args" command that returns the argument |
| 454 |
* list for a procedure. Handles the following syntax: |
| 455 |
* |
| 456 |
* info args procName |
| 457 |
* |
| 458 |
* Results: |
| 459 |
* Returns TCL_OK if successful and TCL_ERROR if there is an error. |
| 460 |
* |
| 461 |
* Side effects: |
| 462 |
* Returns a result in the interpreter's result object. If there is an |
| 463 |
* error, the result is an error message. |
| 464 |
* |
| 465 |
*---------------------------------------------------------------------- |
| 466 |
*/ |
| 467 |
|
| 468 |
static int |
| 469 |
InfoArgsCmd( |
| 470 |
ClientData dummy, /* Not used. */ |
| 471 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 472 |
int objc, /* Number of arguments. */ |
| 473 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 474 |
{ |
| 475 |
Interp *iPtr = (Interp *) interp; |
| 476 |
const char *name; |
| 477 |
Proc *procPtr; |
| 478 |
CompiledLocal *localPtr; |
| 479 |
Tcl_Obj *listObjPtr; |
| 480 |
|
| 481 |
if (objc != 2) { |
| 482 |
Tcl_WrongNumArgs(interp, 1, objv, "procname"); |
| 483 |
return TCL_ERROR; |
| 484 |
} |
| 485 |
|
| 486 |
name = TclGetString(objv[1]); |
| 487 |
procPtr = TclFindProc(iPtr, name); |
| 488 |
if (procPtr == NULL) { |
| 489 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 490 |
"\"%s\" isn't a procedure", name)); |
| 491 |
Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "PROCEDURE", name, (char *)NULL); |
| 492 |
return TCL_ERROR; |
| 493 |
} |
| 494 |
|
| 495 |
/* |
| 496 |
* Build a return list containing the arguments. |
| 497 |
*/ |
| 498 |
|
| 499 |
listObjPtr = Tcl_NewListObj(0, NULL); |
| 500 |
for (localPtr = procPtr->firstLocalPtr; localPtr != NULL; |
| 501 |
localPtr = localPtr->nextPtr) { |
| 502 |
if (TclIsVarArgument(localPtr)) { |
| 503 |
Tcl_ListObjAppendElement(interp, listObjPtr, |
| 504 |
Tcl_NewStringObj(localPtr->name, -1)); |
| 505 |
} |
| 506 |
} |
| 507 |
Tcl_SetObjResult(interp, listObjPtr); |
| 508 |
return TCL_OK; |
| 509 |
} |
| 510 |
|
| 511 |
/* |
| 512 |
*---------------------------------------------------------------------- |
| 513 |
* |
| 514 |
* InfoBodyCmd -- |
| 515 |
* |
| 516 |
* Called to implement the "info body" command that returns the body for |
| 517 |
* a procedure. Handles the following syntax: |
| 518 |
* |
| 519 |
* info body procName |
| 520 |
* |
| 521 |
* Results: |
| 522 |
* Returns TCL_OK if successful and TCL_ERROR if there is an error. |
| 523 |
* |
| 524 |
* Side effects: |
| 525 |
* Returns a result in the interpreter's result object. If there is an |
| 526 |
* error, the result is an error message. |
| 527 |
* |
| 528 |
*---------------------------------------------------------------------- |
| 529 |
*/ |
| 530 |
|
| 531 |
static int |
| 532 |
InfoBodyCmd( |
| 533 |
ClientData dummy, /* Not used. */ |
| 534 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 535 |
int objc, /* Number of arguments. */ |
| 536 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 537 |
{ |
| 538 |
Interp *iPtr = (Interp *) interp; |
| 539 |
const char *name; |
| 540 |
Proc *procPtr; |
| 541 |
Tcl_Obj *bodyPtr, *resultPtr; |
| 542 |
|
| 543 |
if (objc != 2) { |
| 544 |
Tcl_WrongNumArgs(interp, 1, objv, "procname"); |
| 545 |
return TCL_ERROR; |
| 546 |
} |
| 547 |
|
| 548 |
name = TclGetString(objv[1]); |
| 549 |
procPtr = TclFindProc(iPtr, name); |
| 550 |
if (procPtr == NULL) { |
| 551 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 552 |
"\"%s\" isn't a procedure", name)); |
| 553 |
Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "PROCEDURE", name, (char *)NULL); |
| 554 |
return TCL_ERROR; |
| 555 |
} |
| 556 |
|
| 557 |
/* |
| 558 |
* Here we used to return procPtr->bodyPtr, except when the body was |
| 559 |
* bytecompiled - in that case, the return was a copy of the body's string |
| 560 |
* rep. In order to better isolate the implementation details of the |
| 561 |
* compiler/engine subsystem, we now always return a copy of the string |
| 562 |
* rep. It is important to return a copy so that later manipulations of |
| 563 |
* the object do not invalidate the internal rep. |
| 564 |
*/ |
| 565 |
|
| 566 |
bodyPtr = procPtr->bodyPtr; |
| 567 |
if (bodyPtr->bytes == NULL) { |
| 568 |
/* |
| 569 |
* The string rep might not be valid if the procedure has never been |
| 570 |
* run before. [Bug #545644] |
| 571 |
*/ |
| 572 |
|
| 573 |
TclGetString(bodyPtr); |
| 574 |
} |
| 575 |
resultPtr = Tcl_NewStringObj(bodyPtr->bytes, bodyPtr->length); |
| 576 |
|
| 577 |
Tcl_SetObjResult(interp, resultPtr); |
| 578 |
return TCL_OK; |
| 579 |
} |
| 580 |
|
| 581 |
/* |
| 582 |
*---------------------------------------------------------------------- |
| 583 |
* |
| 584 |
* InfoCmdCountCmd -- |
| 585 |
* |
| 586 |
* Called to implement the "info cmdcount" command that returns the |
| 587 |
* number of commands that have been executed. Handles the following |
| 588 |
* syntax: |
| 589 |
* |
| 590 |
* info cmdcount |
| 591 |
* |
| 592 |
* Results: |
| 593 |
* Returns TCL_OK if successful and TCL_ERROR if there is an error. |
| 594 |
* |
| 595 |
* Side effects: |
| 596 |
* Returns a result in the interpreter's result object. If there is an |
| 597 |
* error, the result is an error message. |
| 598 |
* |
| 599 |
*---------------------------------------------------------------------- |
| 600 |
*/ |
| 601 |
|
| 602 |
static int |
| 603 |
InfoCmdCountCmd( |
| 604 |
ClientData dummy, /* Not used. */ |
| 605 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 606 |
int objc, /* Number of arguments. */ |
| 607 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 608 |
{ |
| 609 |
Interp *iPtr = (Interp *) interp; |
| 610 |
|
| 611 |
if (objc != 1) { |
| 612 |
Tcl_WrongNumArgs(interp, 1, objv, NULL); |
| 613 |
return TCL_ERROR; |
| 614 |
} |
| 615 |
|
| 616 |
Tcl_SetObjResult(interp, Tcl_NewIntObj(iPtr->cmdCount)); |
| 617 |
return TCL_OK; |
| 618 |
} |
| 619 |
|
| 620 |
/* |
| 621 |
*---------------------------------------------------------------------- |
| 622 |
* |
| 623 |
* InfoCommandsCmd -- |
| 624 |
* |
| 625 |
* Called to implement the "info commands" command that returns the list |
| 626 |
* of commands in the interpreter that match an optional pattern. The |
| 627 |
* pattern, if any, consists of an optional sequence of namespace names |
| 628 |
* separated by "::" qualifiers, which is followed by a glob-style |
| 629 |
* pattern that restricts which commands are returned. Handles the |
| 630 |
* following syntax: |
| 631 |
* |
| 632 |
* info commands ?pattern? |
| 633 |
* |
| 634 |
* Results: |
| 635 |
* Returns TCL_OK if successful and TCL_ERROR if there is an error. |
| 636 |
* |
| 637 |
* Side effects: |
| 638 |
* Returns a result in the interpreter's result object. If there is an |
| 639 |
* error, the result is an error message. |
| 640 |
* |
| 641 |
*---------------------------------------------------------------------- |
| 642 |
*/ |
| 643 |
|
| 644 |
static int |
| 645 |
InfoCommandsCmd( |
| 646 |
ClientData dummy, /* Not used. */ |
| 647 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 648 |
int objc, /* Number of arguments. */ |
| 649 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 650 |
{ |
| 651 |
const char *cmdName, *pattern; |
| 652 |
const char *simplePattern; |
| 653 |
Tcl_HashEntry *entryPtr; |
| 654 |
Tcl_HashSearch search; |
| 655 |
Namespace *nsPtr; |
| 656 |
Namespace *globalNsPtr = (Namespace *) Tcl_GetGlobalNamespace(interp); |
| 657 |
Namespace *currNsPtr = (Namespace *) Tcl_GetCurrentNamespace(interp); |
| 658 |
Tcl_Obj *listPtr, *elemObjPtr; |
| 659 |
int specificNsInPattern = 0;/* Init. to avoid compiler warning. */ |
| 660 |
Tcl_Command cmd; |
| 661 |
int i; |
| 662 |
|
| 663 |
/* |
| 664 |
* Get the pattern and find the "effective namespace" in which to list |
| 665 |
* commands. |
| 666 |
*/ |
| 667 |
|
| 668 |
if (objc == 1) { |
| 669 |
simplePattern = NULL; |
| 670 |
nsPtr = currNsPtr; |
| 671 |
specificNsInPattern = 0; |
| 672 |
} else if (objc == 2) { |
| 673 |
/* |
| 674 |
* From the pattern, get the effective namespace and the simple |
| 675 |
* pattern (no namespace qualifiers or ::'s) at the end. If an error |
| 676 |
* was found while parsing the pattern, return it. Otherwise, if the |
| 677 |
* namespace wasn't found, just leave nsPtr NULL: we will return an |
| 678 |
* empty list since no commands there can be found. |
| 679 |
*/ |
| 680 |
|
| 681 |
Namespace *dummy1NsPtr, *dummy2NsPtr; |
| 682 |
|
| 683 |
pattern = TclGetString(objv[1]); |
| 684 |
TclGetNamespaceForQualName(interp, pattern, NULL, 0, &nsPtr, |
| 685 |
&dummy1NsPtr, &dummy2NsPtr, &simplePattern); |
| 686 |
|
| 687 |
if (nsPtr != NULL) { /* We successfully found the pattern's ns. */ |
| 688 |
specificNsInPattern = (strcmp(simplePattern, pattern) != 0); |
| 689 |
} |
| 690 |
} else { |
| 691 |
Tcl_WrongNumArgs(interp, 1, objv, "?pattern?"); |
| 692 |
return TCL_ERROR; |
| 693 |
} |
| 694 |
|
| 695 |
/* |
| 696 |
* Exit as quickly as possible if we couldn't find the namespace. |
| 697 |
*/ |
| 698 |
|
| 699 |
if (nsPtr == NULL) { |
| 700 |
return TCL_OK; |
| 701 |
} |
| 702 |
|
| 703 |
/* |
| 704 |
* Scan through the effective namespace's command table and create a list |
| 705 |
* with all commands that match the pattern. If a specific namespace was |
| 706 |
* requested in the pattern, qualify the command names with the namespace |
| 707 |
* name. |
| 708 |
*/ |
| 709 |
|
| 710 |
listPtr = Tcl_NewListObj(0, NULL); |
| 711 |
|
| 712 |
if (simplePattern != NULL && TclMatchIsTrivial(simplePattern)) { |
| 713 |
/* |
| 714 |
* Special case for when the pattern doesn't include any of glob's |
| 715 |
* special characters. This lets us avoid scans of any hash tables. |
| 716 |
*/ |
| 717 |
|
| 718 |
entryPtr = Tcl_FindHashEntry(&nsPtr->cmdTable, simplePattern); |
| 719 |
if (entryPtr != NULL) { |
| 720 |
if (specificNsInPattern) { |
| 721 |
cmd = (Tcl_Command)Tcl_GetHashValue(entryPtr); |
| 722 |
TclNewObj(elemObjPtr); |
| 723 |
Tcl_GetCommandFullName(interp, cmd, elemObjPtr); |
| 724 |
} else { |
| 725 |
cmdName = (const char *)Tcl_GetHashKey(&nsPtr->cmdTable, entryPtr); |
| 726 |
elemObjPtr = Tcl_NewStringObj(cmdName, -1); |
| 727 |
} |
| 728 |
Tcl_ListObjAppendElement(interp, listPtr, elemObjPtr); |
| 729 |
Tcl_SetObjResult(interp, listPtr); |
| 730 |
return TCL_OK; |
| 731 |
} |
| 732 |
if ((nsPtr != globalNsPtr) && !specificNsInPattern) { |
| 733 |
Tcl_HashTable *tablePtr = NULL; /* Quell warning. */ |
| 734 |
|
| 735 |
for (i=0 ; i<nsPtr->commandPathLength ; i++) { |
| 736 |
Namespace *pathNsPtr = nsPtr->commandPathArray[i].nsPtr; |
| 737 |
|
| 738 |
if (pathNsPtr == NULL) { |
| 739 |
continue; |
| 740 |
} |
| 741 |
tablePtr = &pathNsPtr->cmdTable; |
| 742 |
entryPtr = Tcl_FindHashEntry(tablePtr, simplePattern); |
| 743 |
if (entryPtr != NULL) { |
| 744 |
break; |
| 745 |
} |
| 746 |
} |
| 747 |
if (entryPtr == NULL) { |
| 748 |
tablePtr = &globalNsPtr->cmdTable; |
| 749 |
entryPtr = Tcl_FindHashEntry(tablePtr, simplePattern); |
| 750 |
} |
| 751 |
if (entryPtr != NULL) { |
| 752 |
cmdName = (const char *)Tcl_GetHashKey(tablePtr, entryPtr); |
| 753 |
Tcl_ListObjAppendElement(interp, listPtr, |
| 754 |
Tcl_NewStringObj(cmdName, -1)); |
| 755 |
Tcl_SetObjResult(interp, listPtr); |
| 756 |
return TCL_OK; |
| 757 |
} |
| 758 |
} |
| 759 |
} else if (nsPtr->commandPathLength == 0 || specificNsInPattern) { |
| 760 |
/* |
| 761 |
* The pattern is non-trivial, but either there is no explicit path or |
| 762 |
* there is an explicit namespace in the pattern. In both cases, the |
| 763 |
* old matching scheme is perfect. |
| 764 |
*/ |
| 765 |
|
| 766 |
entryPtr = Tcl_FirstHashEntry(&nsPtr->cmdTable, &search); |
| 767 |
while (entryPtr != NULL) { |
| 768 |
cmdName = (const char *)Tcl_GetHashKey(&nsPtr->cmdTable, entryPtr); |
| 769 |
if ((simplePattern == NULL) |
| 770 |
|| Tcl_StringMatch(cmdName, simplePattern)) { |
| 771 |
if (specificNsInPattern) { |
| 772 |
cmd = (Tcl_Command)Tcl_GetHashValue(entryPtr); |
| 773 |
TclNewObj(elemObjPtr); |
| 774 |
Tcl_GetCommandFullName(interp, cmd, elemObjPtr); |
| 775 |
} else { |
| 776 |
elemObjPtr = Tcl_NewStringObj(cmdName, -1); |
| 777 |
} |
| 778 |
Tcl_ListObjAppendElement(interp, listPtr, elemObjPtr); |
| 779 |
} |
| 780 |
entryPtr = Tcl_NextHashEntry(&search); |
| 781 |
} |
| 782 |
|
| 783 |
/* |
| 784 |
* If the effective namespace isn't the global :: namespace, and a |
| 785 |
* specific namespace wasn't requested in the pattern, then add in all |
| 786 |
* global :: commands that match the simple pattern. Of course, we add |
| 787 |
* in only those commands that aren't hidden by a command in the |
| 788 |
* effective namespace. |
| 789 |
*/ |
| 790 |
|
| 791 |
if ((nsPtr != globalNsPtr) && !specificNsInPattern) { |
| 792 |
entryPtr = Tcl_FirstHashEntry(&globalNsPtr->cmdTable, &search); |
| 793 |
while (entryPtr != NULL) { |
| 794 |
cmdName = (const char *)Tcl_GetHashKey(&globalNsPtr->cmdTable, entryPtr); |
| 795 |
if ((simplePattern == NULL) |
| 796 |
|| Tcl_StringMatch(cmdName, simplePattern)) { |
| 797 |
if (Tcl_FindHashEntry(&nsPtr->cmdTable,cmdName) == NULL) { |
| 798 |
Tcl_ListObjAppendElement(interp, listPtr, |
| 799 |
Tcl_NewStringObj(cmdName, -1)); |
| 800 |
} |
| 801 |
} |
| 802 |
entryPtr = Tcl_NextHashEntry(&search); |
| 803 |
} |
| 804 |
} |
| 805 |
} else { |
| 806 |
/* |
| 807 |
* The pattern is non-trivial (can match more than one command name), |
| 808 |
* there is an explicit path, and there is no explicit namespace in |
| 809 |
* the pattern. This means that we have to traverse the path to |
| 810 |
* discover all the commands defined. |
| 811 |
*/ |
| 812 |
|
| 813 |
Tcl_HashTable addedCommandsTable; |
| 814 |
int isNew; |
| 815 |
int foundGlobal = (nsPtr == globalNsPtr); |
| 816 |
|
| 817 |
/* |
| 818 |
* We keep a hash of the objects already added to the result list. |
| 819 |
*/ |
| 820 |
|
| 821 |
Tcl_InitObjHashTable(&addedCommandsTable); |
| 822 |
|
| 823 |
entryPtr = Tcl_FirstHashEntry(&nsPtr->cmdTable, &search); |
| 824 |
while (entryPtr != NULL) { |
| 825 |
cmdName = (const char *)Tcl_GetHashKey(&nsPtr->cmdTable, entryPtr); |
| 826 |
if ((simplePattern == NULL) |
| 827 |
|| Tcl_StringMatch(cmdName, simplePattern)) { |
| 828 |
elemObjPtr = Tcl_NewStringObj(cmdName, -1); |
| 829 |
Tcl_ListObjAppendElement(interp, listPtr, elemObjPtr); |
| 830 |
(void) Tcl_CreateHashEntry(&addedCommandsTable, |
| 831 |
elemObjPtr, &isNew); |
| 832 |
} |
| 833 |
entryPtr = Tcl_NextHashEntry(&search); |
| 834 |
} |
| 835 |
|
| 836 |
/* |
| 837 |
* Search the path next. |
| 838 |
*/ |
| 839 |
|
| 840 |
for (i=0 ; i<nsPtr->commandPathLength ; i++) { |
| 841 |
Namespace *pathNsPtr = nsPtr->commandPathArray[i].nsPtr; |
| 842 |
|
| 843 |
if (pathNsPtr == NULL) { |
| 844 |
continue; |
| 845 |
} |
| 846 |
if (pathNsPtr == globalNsPtr) { |
| 847 |
foundGlobal = 1; |
| 848 |
} |
| 849 |
entryPtr = Tcl_FirstHashEntry(&pathNsPtr->cmdTable, &search); |
| 850 |
while (entryPtr != NULL) { |
| 851 |
cmdName = (const char *)Tcl_GetHashKey(&pathNsPtr->cmdTable, entryPtr); |
| 852 |
if ((simplePattern == NULL) |
| 853 |
|| Tcl_StringMatch(cmdName, simplePattern)) { |
| 854 |
elemObjPtr = Tcl_NewStringObj(cmdName, -1); |
| 855 |
(void) Tcl_CreateHashEntry(&addedCommandsTable, |
| 856 |
elemObjPtr, &isNew); |
| 857 |
if (isNew) { |
| 858 |
Tcl_ListObjAppendElement(interp, listPtr, elemObjPtr); |
| 859 |
} else { |
| 860 |
TclDecrRefCount(elemObjPtr); |
| 861 |
} |
| 862 |
} |
| 863 |
entryPtr = Tcl_NextHashEntry(&search); |
| 864 |
} |
| 865 |
} |
| 866 |
|
| 867 |
/* |
| 868 |
* If the effective namespace isn't the global :: namespace, and a |
| 869 |
* specific namespace wasn't requested in the pattern, then add in all |
| 870 |
* global :: commands that match the simple pattern. Of course, we add |
| 871 |
* in only those commands that aren't hidden by a command in the |
| 872 |
* effective namespace. |
| 873 |
*/ |
| 874 |
|
| 875 |
if (!foundGlobal) { |
| 876 |
entryPtr = Tcl_FirstHashEntry(&globalNsPtr->cmdTable, &search); |
| 877 |
while (entryPtr != NULL) { |
| 878 |
cmdName = (const char *)Tcl_GetHashKey(&globalNsPtr->cmdTable, entryPtr); |
| 879 |
if ((simplePattern == NULL) |
| 880 |
|| Tcl_StringMatch(cmdName, simplePattern)) { |
| 881 |
elemObjPtr = Tcl_NewStringObj(cmdName, -1); |
| 882 |
if (Tcl_FindHashEntry(&addedCommandsTable, |
| 883 |
(char *) elemObjPtr) == NULL) { |
| 884 |
Tcl_ListObjAppendElement(interp, listPtr, elemObjPtr); |
| 885 |
} else { |
| 886 |
TclDecrRefCount(elemObjPtr); |
| 887 |
} |
| 888 |
} |
| 889 |
entryPtr = Tcl_NextHashEntry(&search); |
| 890 |
} |
| 891 |
} |
| 892 |
|
| 893 |
Tcl_DeleteHashTable(&addedCommandsTable); |
| 894 |
} |
| 895 |
|
| 896 |
Tcl_SetObjResult(interp, listPtr); |
| 897 |
return TCL_OK; |
| 898 |
} |
| 899 |
|
| 900 |
/* |
| 901 |
*---------------------------------------------------------------------- |
| 902 |
* |
| 903 |
* InfoCompleteCmd -- |
| 904 |
* |
| 905 |
* Called to implement the "info complete" command that determines |
| 906 |
* whether a string is a complete Tcl command. Handles the following |
| 907 |
* syntax: |
| 908 |
* |
| 909 |
* info complete command |
| 910 |
* |
| 911 |
* Results: |
| 912 |
* Returns TCL_OK if successful and TCL_ERROR if there is an error. |
| 913 |
* |
| 914 |
* Side effects: |
| 915 |
* Returns a result in the interpreter's result object. If there is an |
| 916 |
* error, the result is an error message. |
| 917 |
* |
| 918 |
*---------------------------------------------------------------------- |
| 919 |
*/ |
| 920 |
|
| 921 |
static int |
| 922 |
InfoCompleteCmd( |
| 923 |
ClientData dummy, /* Not used. */ |
| 924 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 925 |
int objc, /* Number of arguments. */ |
| 926 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 927 |
{ |
| 928 |
if (objc != 2) { |
| 929 |
Tcl_WrongNumArgs(interp, 1, objv, "command"); |
| 930 |
return TCL_ERROR; |
| 931 |
} |
| 932 |
|
| 933 |
Tcl_SetObjResult(interp, Tcl_NewBooleanObj( |
| 934 |
TclObjCommandComplete(objv[1]))); |
| 935 |
return TCL_OK; |
| 936 |
} |
| 937 |
|
| 938 |
/* |
| 939 |
*---------------------------------------------------------------------- |
| 940 |
* |
| 941 |
* InfoDefaultCmd -- |
| 942 |
* |
| 943 |
* Called to implement the "info default" command that returns the |
| 944 |
* default value for a procedure argument. Handles the following syntax: |
| 945 |
* |
| 946 |
* info default procName arg varName |
| 947 |
* |
| 948 |
* Results: |
| 949 |
* Returns TCL_OK if successful and TCL_ERROR if there is an error. |
| 950 |
* |
| 951 |
* Side effects: |
| 952 |
* Returns a result in the interpreter's result object. If there is an |
| 953 |
* error, the result is an error message. |
| 954 |
* |
| 955 |
*---------------------------------------------------------------------- |
| 956 |
*/ |
| 957 |
|
| 958 |
static int |
| 959 |
InfoDefaultCmd( |
| 960 |
ClientData dummy, /* Not used. */ |
| 961 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 962 |
int objc, /* Number of arguments. */ |
| 963 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 964 |
{ |
| 965 |
Interp *iPtr = (Interp *) interp; |
| 966 |
const char *procName, *argName; |
| 967 |
Proc *procPtr; |
| 968 |
CompiledLocal *localPtr; |
| 969 |
Tcl_Obj *valueObjPtr; |
| 970 |
|
| 971 |
if (objc != 4) { |
| 972 |
Tcl_WrongNumArgs(interp, 1, objv, "procname arg varname"); |
| 973 |
return TCL_ERROR; |
| 974 |
} |
| 975 |
|
| 976 |
procName = TclGetString(objv[1]); |
| 977 |
argName = TclGetString(objv[2]); |
| 978 |
|
| 979 |
procPtr = TclFindProc(iPtr, procName); |
| 980 |
if (procPtr == NULL) { |
| 981 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 982 |
"\"%s\" isn't a procedure", procName)); |
| 983 |
Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "PROCEDURE", procName, |
| 984 |
(char *)NULL); |
| 985 |
return TCL_ERROR; |
| 986 |
} |
| 987 |
|
| 988 |
for (localPtr = procPtr->firstLocalPtr; localPtr != NULL; |
| 989 |
localPtr = localPtr->nextPtr) { |
| 990 |
if (TclIsVarArgument(localPtr) |
| 991 |
&& (strcmp(argName, localPtr->name) == 0)) { |
| 992 |
if (localPtr->defValuePtr != NULL) { |
| 993 |
valueObjPtr = Tcl_ObjSetVar2(interp, objv[3], NULL, |
| 994 |
localPtr->defValuePtr, TCL_LEAVE_ERR_MSG); |
| 995 |
if (valueObjPtr == NULL) { |
| 996 |
return TCL_ERROR; |
| 997 |
} |
| 998 |
Tcl_SetObjResult(interp, Tcl_NewBooleanObj(1)); |
| 999 |
} else { |
| 1000 |
Tcl_Obj *nullObjPtr; |
| 1001 |
|
| 1002 |
TclNewObj(nullObjPtr); |
| 1003 |
valueObjPtr = Tcl_ObjSetVar2(interp, objv[3], NULL, |
| 1004 |
nullObjPtr, TCL_LEAVE_ERR_MSG); |
| 1005 |
if (valueObjPtr == NULL) { |
| 1006 |
return TCL_ERROR; |
| 1007 |
} |
| 1008 |
Tcl_SetObjResult(interp, Tcl_NewBooleanObj(0)); |
| 1009 |
} |
| 1010 |
return TCL_OK; |
| 1011 |
} |
| 1012 |
} |
| 1013 |
|
| 1014 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1015 |
"procedure \"%s\" doesn't have an argument \"%s\"", |
| 1016 |
procName, argName)); |
| 1017 |
Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "ARGUMENT", argName, (char *)NULL); |
| 1018 |
return TCL_ERROR; |
| 1019 |
} |
| 1020 |
|
| 1021 |
/* |
| 1022 |
*---------------------------------------------------------------------- |
| 1023 |
* |
| 1024 |
* InfoErrorStackCmd -- |
| 1025 |
* |
| 1026 |
* Called to implement the "info errorstack" command that returns information |
| 1027 |
* about the last error's call stack. Handles the following syntax: |
| 1028 |
* |
| 1029 |
* info errorstack ?interp? |
| 1030 |
* |
| 1031 |
* Results: |
| 1032 |
* Returns TCL_OK if successful and TCL_ERROR if there is an error. |
| 1033 |
* |
| 1034 |
* Side effects: |
| 1035 |
* Returns a result in the interpreter's result object. If there is an |
| 1036 |
* error, the result is an error message. |
| 1037 |
* |
| 1038 |
*---------------------------------------------------------------------- |
| 1039 |
*/ |
| 1040 |
|
| 1041 |
static int |
| 1042 |
InfoErrorStackCmd( |
| 1043 |
ClientData dummy, /* Not used. */ |
| 1044 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 1045 |
int objc, /* Number of arguments. */ |
| 1046 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 1047 |
{ |
| 1048 |
Tcl_Interp *target; |
| 1049 |
Interp *iPtr; |
| 1050 |
|
| 1051 |
if ((objc != 1) && (objc != 2)) { |
| 1052 |
Tcl_WrongNumArgs(interp, 1, objv, "?interp?"); |
| 1053 |
return TCL_ERROR; |
| 1054 |
} |
| 1055 |
|
| 1056 |
target = interp; |
| 1057 |
if (objc == 2) { |
| 1058 |
target = Tcl_GetChild(interp, TclGetString(objv[1])); |
| 1059 |
if (target == NULL) { |
| 1060 |
return TCL_ERROR; |
| 1061 |
} |
| 1062 |
} |
| 1063 |
|
| 1064 |
iPtr = (Interp *) target; |
| 1065 |
Tcl_SetObjResult(interp, iPtr->errorStack); |
| 1066 |
|
| 1067 |
return TCL_OK; |
| 1068 |
} |
| 1069 |
|
| 1070 |
/* |
| 1071 |
*---------------------------------------------------------------------- |
| 1072 |
* |
| 1073 |
* TclInfoExistsCmd -- |
| 1074 |
* |
| 1075 |
* Called to implement the "info exists" command that determines whether |
| 1076 |
* a variable exists. Handles the following syntax: |
| 1077 |
* |
| 1078 |
* info exists varName |
| 1079 |
* |
| 1080 |
* Results: |
| 1081 |
* Returns TCL_OK if successful and TCL_ERROR if there is an error. |
| 1082 |
* |
| 1083 |
* Side effects: |
| 1084 |
* Returns a result in the interpreter's result object. If there is an |
| 1085 |
* error, the result is an error message. |
| 1086 |
* |
| 1087 |
*---------------------------------------------------------------------- |
| 1088 |
*/ |
| 1089 |
|
| 1090 |
int |
| 1091 |
TclInfoExistsCmd( |
| 1092 |
ClientData dummy, /* Not used. */ |
| 1093 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 1094 |
int objc, /* Number of arguments. */ |
| 1095 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 1096 |
{ |
| 1097 |
const char *varName; |
| 1098 |
Var *varPtr; |
| 1099 |
|
| 1100 |
if (objc != 2) { |
| 1101 |
Tcl_WrongNumArgs(interp, 1, objv, "varName"); |
| 1102 |
return TCL_ERROR; |
| 1103 |
} |
| 1104 |
|
| 1105 |
varName = TclGetString(objv[1]); |
| 1106 |
varPtr = TclVarTraceExists(interp, varName); |
| 1107 |
|
| 1108 |
Tcl_SetObjResult(interp, |
| 1109 |
Tcl_NewBooleanObj(varPtr && varPtr->value.objPtr)); |
| 1110 |
return TCL_OK; |
| 1111 |
} |
| 1112 |
|
| 1113 |
/* |
| 1114 |
*---------------------------------------------------------------------- |
| 1115 |
* |
| 1116 |
* InfoFrameCmd -- |
| 1117 |
* TIP #280 |
| 1118 |
* |
| 1119 |
* Called to implement the "info frame" command that returns the location |
| 1120 |
* of either the currently executing command, or its caller. Handles the |
| 1121 |
* following syntax: |
| 1122 |
* |
| 1123 |
* info frame ?number? |
| 1124 |
* |
| 1125 |
* Results: |
| 1126 |
* Returns TCL_OK if successful and TCL_ERROR if there is an error. |
| 1127 |
* |
| 1128 |
* Side effects: |
| 1129 |
* Returns a result in the interpreter's result object. If there is an |
| 1130 |
* error, the result is an error message. |
| 1131 |
* |
| 1132 |
*---------------------------------------------------------------------- |
| 1133 |
*/ |
| 1134 |
|
| 1135 |
static int |
| 1136 |
InfoFrameCmd( |
| 1137 |
ClientData dummy, /* Not used. */ |
| 1138 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 1139 |
int objc, /* Number of arguments. */ |
| 1140 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 1141 |
{ |
| 1142 |
Interp *iPtr = (Interp *) interp; |
| 1143 |
int level, code = TCL_OK; |
| 1144 |
CmdFrame *framePtr, **cmdFramePtrPtr = &iPtr->cmdFramePtr; |
| 1145 |
CoroutineData *corPtr = iPtr->execEnvPtr->corPtr; |
| 1146 |
int topLevel = 0; |
| 1147 |
|
| 1148 |
if (objc > 2) { |
| 1149 |
Tcl_WrongNumArgs(interp, 1, objv, "?number?"); |
| 1150 |
return TCL_ERROR; |
| 1151 |
} |
| 1152 |
|
| 1153 |
while (corPtr) { |
| 1154 |
while (*cmdFramePtrPtr) { |
| 1155 |
topLevel++; |
| 1156 |
cmdFramePtrPtr = &((*cmdFramePtrPtr)->nextPtr); |
| 1157 |
} |
| 1158 |
if (corPtr->caller.cmdFramePtr) { |
| 1159 |
*cmdFramePtrPtr = corPtr->caller.cmdFramePtr; |
| 1160 |
} |
| 1161 |
corPtr = corPtr->callerEEPtr->corPtr; |
| 1162 |
} |
| 1163 |
topLevel += *cmdFramePtrPtr ? (*cmdFramePtrPtr)->level : 1; |
| 1164 |
|
| 1165 |
if (iPtr->cmdFramePtr && topLevel != iPtr->cmdFramePtr->level) { |
| 1166 |
framePtr = iPtr->cmdFramePtr; |
| 1167 |
while (framePtr) { |
| 1168 |
framePtr->level = topLevel--; |
| 1169 |
framePtr = framePtr->nextPtr; |
| 1170 |
} |
| 1171 |
if (topLevel) { |
| 1172 |
Tcl_Panic("Broken frame level calculation"); |
| 1173 |
} |
| 1174 |
topLevel = iPtr->cmdFramePtr->level; |
| 1175 |
} |
| 1176 |
|
| 1177 |
if (objc == 1) { |
| 1178 |
/* |
| 1179 |
* Just "info frame". |
| 1180 |
*/ |
| 1181 |
|
| 1182 |
Tcl_SetObjResult(interp, Tcl_NewIntObj(topLevel)); |
| 1183 |
goto done; |
| 1184 |
} |
| 1185 |
|
| 1186 |
/* |
| 1187 |
* We've got "info frame level" and must parse the level first. |
| 1188 |
*/ |
| 1189 |
|
| 1190 |
if (TclGetIntFromObj(interp, objv[1], &level) != TCL_OK) { |
| 1191 |
code = TCL_ERROR; |
| 1192 |
goto done; |
| 1193 |
} |
| 1194 |
|
| 1195 |
if ((level > topLevel) || (level <= - topLevel)) { |
| 1196 |
levelError: |
| 1197 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1198 |
"bad level \"%s\"", TclGetString(objv[1]))); |
| 1199 |
Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "LEVEL", |
| 1200 |
TclGetString(objv[1]), (char *)NULL); |
| 1201 |
code = TCL_ERROR; |
| 1202 |
goto done; |
| 1203 |
} |
| 1204 |
|
| 1205 |
/* |
| 1206 |
* Let us convert to relative so that we know how many levels to go back |
| 1207 |
*/ |
| 1208 |
|
| 1209 |
if (level > 0) { |
| 1210 |
level -= topLevel; |
| 1211 |
} |
| 1212 |
|
| 1213 |
framePtr = iPtr->cmdFramePtr; |
| 1214 |
while (++level <= 0) { |
| 1215 |
framePtr = framePtr->nextPtr; |
| 1216 |
if (!framePtr) { |
| 1217 |
goto levelError; |
| 1218 |
} |
| 1219 |
} |
| 1220 |
|
| 1221 |
Tcl_SetObjResult(interp, TclInfoFrame(interp, framePtr)); |
| 1222 |
|
| 1223 |
done: |
| 1224 |
cmdFramePtrPtr = &iPtr->cmdFramePtr; |
| 1225 |
corPtr = iPtr->execEnvPtr->corPtr; |
| 1226 |
while (corPtr) { |
| 1227 |
CmdFrame *endPtr = corPtr->caller.cmdFramePtr; |
| 1228 |
|
| 1229 |
if (endPtr) { |
| 1230 |
if (*cmdFramePtrPtr == endPtr) { |
| 1231 |
*cmdFramePtrPtr = NULL; |
| 1232 |
} else { |
| 1233 |
CmdFrame *runPtr = *cmdFramePtrPtr; |
| 1234 |
|
| 1235 |
while (runPtr->nextPtr != endPtr) { |
| 1236 |
runPtr->level -= endPtr->level; |
| 1237 |
runPtr = runPtr->nextPtr; |
| 1238 |
} |
| 1239 |
runPtr->level = 1; |
| 1240 |
runPtr->nextPtr = NULL; |
| 1241 |
} |
| 1242 |
cmdFramePtrPtr = &corPtr->caller.cmdFramePtr; |
| 1243 |
} |
| 1244 |
corPtr = corPtr->callerEEPtr->corPtr; |
| 1245 |
} |
| 1246 |
return code; |
| 1247 |
} |
| 1248 |
|
| 1249 |
/* |
| 1250 |
*---------------------------------------------------------------------- |
| 1251 |
* |
| 1252 |
* TclInfoFrame -- |
| 1253 |
* |
| 1254 |
* Core of InfoFrameCmd, returns TIP280 dict for a given frame. |
| 1255 |
* |
| 1256 |
* Results: |
| 1257 |
* Returns TIP280 dict. |
| 1258 |
* |
| 1259 |
* Side effects: |
| 1260 |
* None. |
| 1261 |
* |
| 1262 |
*---------------------------------------------------------------------- |
| 1263 |
*/ |
| 1264 |
|
| 1265 |
Tcl_Obj * |
| 1266 |
TclInfoFrame( |
| 1267 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 1268 |
CmdFrame *framePtr) /* Frame to get info for. */ |
| 1269 |
{ |
| 1270 |
Interp *iPtr = (Interp *) interp; |
| 1271 |
Tcl_Obj *tmpObj; |
| 1272 |
Tcl_Obj *lv[20] = {NULL}; /* Keep uptodate when more keys are added to |
| 1273 |
* the dict. */ |
| 1274 |
int lc = 0; |
| 1275 |
/* |
| 1276 |
* This array is indexed by the TCL_LOCATION_... values, except |
| 1277 |
* for _LAST. |
| 1278 |
*/ |
| 1279 |
static const char *const typeString[TCL_LOCATION_LAST] = { |
| 1280 |
"eval", "eval", "eval", "precompiled", "source", "proc" |
| 1281 |
}; |
| 1282 |
Proc *procPtr = NULL; |
| 1283 |
int needsFree = -1; |
| 1284 |
|
| 1285 |
if (!framePtr) { |
| 1286 |
goto precompiled; |
| 1287 |
} |
| 1288 |
procPtr = framePtr->framePtr ? framePtr->framePtr->procPtr : NULL; |
| 1289 |
|
| 1290 |
/* |
| 1291 |
* Pull the information and construct the dictionary to return, as list. |
| 1292 |
* Regarding use of the CmdFrame fields see tclInt.h, and its definition. |
| 1293 |
*/ |
| 1294 |
|
| 1295 |
#define ADD_PAIR(name, value) \ |
| 1296 |
TclNewLiteralStringObj(tmpObj, name); \ |
| 1297 |
lv[lc++] = tmpObj; \ |
| 1298 |
lv[lc++] = (value) |
| 1299 |
|
| 1300 |
switch (framePtr->type) { |
| 1301 |
case TCL_LOCATION_EVAL: |
| 1302 |
/* |
| 1303 |
* Evaluation, dynamic script. Type, line, cmd, the latter through |
| 1304 |
* str. |
| 1305 |
*/ |
| 1306 |
|
| 1307 |
ADD_PAIR("type", Tcl_NewStringObj(typeString[framePtr->type], -1)); |
| 1308 |
if (framePtr->line) { |
| 1309 |
ADD_PAIR("line", Tcl_NewIntObj(framePtr->line[0])); |
| 1310 |
} else { |
| 1311 |
ADD_PAIR("line", Tcl_NewIntObj(1)); |
| 1312 |
} |
| 1313 |
ADD_PAIR("cmd", TclGetSourceFromFrame(framePtr, 0, NULL)); |
| 1314 |
break; |
| 1315 |
|
| 1316 |
case TCL_LOCATION_PREBC: |
| 1317 |
precompiled: |
| 1318 |
/* |
| 1319 |
* Precompiled. Result contains the type as signal, nothing else. |
| 1320 |
*/ |
| 1321 |
ADD_PAIR("type", Tcl_NewStringObj(typeString[TCL_LOCATION_PREBC], -1)); |
| 1322 |
break; |
| 1323 |
|
| 1324 |
case TCL_LOCATION_BC: { |
| 1325 |
/* |
| 1326 |
* Execution of bytecode. Talk to the BC engine to fill out the frame. |
| 1327 |
*/ |
| 1328 |
|
| 1329 |
CmdFrame *fPtr = (CmdFrame *)TclStackAlloc(interp, sizeof(CmdFrame)); |
| 1330 |
|
| 1331 |
*fPtr = *framePtr; |
| 1332 |
|
| 1333 |
/* |
| 1334 |
* Note: |
| 1335 |
* Type BC => f.data.eval.path is not used. |
| 1336 |
* f.data.tebc.codePtr is used instead. |
| 1337 |
*/ |
| 1338 |
|
| 1339 |
TclGetSrcInfoForPc(fPtr); |
| 1340 |
|
| 1341 |
/* |
| 1342 |
* Now filled: cmd.str.(cmd,len), line |
| 1343 |
* Possibly modified: type, path! |
| 1344 |
*/ |
| 1345 |
|
| 1346 |
ADD_PAIR("type", Tcl_NewStringObj(typeString[fPtr->type], -1)); |
| 1347 |
if (fPtr->line) { |
| 1348 |
ADD_PAIR("line", Tcl_NewIntObj(fPtr->line[0])); |
| 1349 |
} |
| 1350 |
|
| 1351 |
if (fPtr->type == TCL_LOCATION_SOURCE) { |
| 1352 |
ADD_PAIR("file", fPtr->data.eval.path); |
| 1353 |
|
| 1354 |
/* |
| 1355 |
* Death of reference by TclGetSrcInfoForPc. |
| 1356 |
*/ |
| 1357 |
|
| 1358 |
Tcl_DecrRefCount(fPtr->data.eval.path); |
| 1359 |
} |
| 1360 |
|
| 1361 |
ADD_PAIR("cmd", TclGetSourceFromFrame(fPtr, 0, NULL)); |
| 1362 |
if (fPtr->cmdObj && framePtr->cmdObj == NULL) { |
| 1363 |
needsFree = lc - 1; |
| 1364 |
} |
| 1365 |
TclStackFree(interp, fPtr); |
| 1366 |
break; |
| 1367 |
} |
| 1368 |
|
| 1369 |
case TCL_LOCATION_SOURCE: |
| 1370 |
/* |
| 1371 |
* Evaluation of a script file. |
| 1372 |
*/ |
| 1373 |
|
| 1374 |
ADD_PAIR("type", Tcl_NewStringObj(typeString[framePtr->type], -1)); |
| 1375 |
ADD_PAIR("line", Tcl_NewIntObj(framePtr->line[0])); |
| 1376 |
ADD_PAIR("file", framePtr->data.eval.path); |
| 1377 |
|
| 1378 |
/* |
| 1379 |
* Refcount framePtr->data.eval.path goes up when lv is converted into |
| 1380 |
* the result list object. |
| 1381 |
*/ |
| 1382 |
|
| 1383 |
ADD_PAIR("cmd", TclGetSourceFromFrame(framePtr, 0, NULL)); |
| 1384 |
break; |
| 1385 |
|
| 1386 |
case TCL_LOCATION_PROC: |
| 1387 |
Tcl_Panic("TCL_LOCATION_PROC found in standard frame"); |
| 1388 |
break; |
| 1389 |
} |
| 1390 |
|
| 1391 |
/* |
| 1392 |
* 'proc'. Common to all frame types. Conditional on having an associated |
| 1393 |
* Procedure CallFrame. |
| 1394 |
*/ |
| 1395 |
|
| 1396 |
if (procPtr != NULL) { |
| 1397 |
Tcl_HashEntry *namePtr = procPtr->cmdPtr->hPtr; |
| 1398 |
|
| 1399 |
if (namePtr) { |
| 1400 |
Tcl_Obj *procNameObj; |
| 1401 |
|
| 1402 |
/* |
| 1403 |
* This is a regular command. |
| 1404 |
*/ |
| 1405 |
|
| 1406 |
TclNewObj(procNameObj); |
| 1407 |
Tcl_GetCommandFullName(interp, (Tcl_Command) procPtr->cmdPtr, |
| 1408 |
procNameObj); |
| 1409 |
ADD_PAIR("proc", procNameObj); |
| 1410 |
} else if (procPtr->cmdPtr->clientData) { |
| 1411 |
ExtraFrameInfo *efiPtr = (ExtraFrameInfo *)procPtr->cmdPtr->clientData; |
| 1412 |
int i; |
| 1413 |
|
| 1414 |
/* |
| 1415 |
* This is a non-standard command. Luckily, it's told us how to |
| 1416 |
* render extra information about its frame. |
| 1417 |
*/ |
| 1418 |
|
| 1419 |
for (i=0 ; i<efiPtr->length ; i++) { |
| 1420 |
lv[lc++] = Tcl_NewStringObj(efiPtr->fields[i].name, -1); |
| 1421 |
if (efiPtr->fields[i].proc) { |
| 1422 |
lv[lc++] = |
| 1423 |
efiPtr->fields[i].proc(efiPtr->fields[i].clientData); |
| 1424 |
} else { |
| 1425 |
lv[lc++] = (Tcl_Obj *)efiPtr->fields[i].clientData; |
| 1426 |
} |
| 1427 |
} |
| 1428 |
} |
| 1429 |
} |
| 1430 |
|
| 1431 |
/* |
| 1432 |
* 'level'. Common to all frame types. Conditional on having an associated |
| 1433 |
* _visible_ CallFrame. |
| 1434 |
*/ |
| 1435 |
|
| 1436 |
if (framePtr && (framePtr->framePtr != NULL) && (iPtr->varFramePtr != NULL)) { |
| 1437 |
CallFrame *current = framePtr->framePtr; |
| 1438 |
CallFrame *top = iPtr->varFramePtr; |
| 1439 |
CallFrame *idx; |
| 1440 |
|
| 1441 |
for (idx=top ; idx!=NULL ; idx=idx->callerVarPtr) { |
| 1442 |
if (idx == current) { |
| 1443 |
int c = framePtr->framePtr->level; |
| 1444 |
int t = iPtr->varFramePtr->level; |
| 1445 |
|
| 1446 |
ADD_PAIR("level", Tcl_NewIntObj(t - c)); |
| 1447 |
break; |
| 1448 |
} |
| 1449 |
} |
| 1450 |
} |
| 1451 |
|
| 1452 |
tmpObj = Tcl_NewListObj(lc, lv); |
| 1453 |
if (needsFree >= 0) { |
| 1454 |
Tcl_DecrRefCount(lv[needsFree]); |
| 1455 |
} |
| 1456 |
return tmpObj; |
| 1457 |
} |
| 1458 |
|
| 1459 |
/* |
| 1460 |
*---------------------------------------------------------------------- |
| 1461 |
* |
| 1462 |
* InfoFunctionsCmd -- |
| 1463 |
* |
| 1464 |
* Called to implement the "info functions" command that returns the list |
| 1465 |
* of math functions matching an optional pattern. Handles the following |
| 1466 |
* syntax: |
| 1467 |
* |
| 1468 |
* info functions ?pattern? |
| 1469 |
* |
| 1470 |
* Results: |
| 1471 |
* Returns TCL_OK if successful and TCL_ERROR if there is an error. |
| 1472 |
* |
| 1473 |
* Side effects: |
| 1474 |
* Returns a result in the interpreter's result object. If there is an |
| 1475 |
* error, the result is an error message. |
| 1476 |
* |
| 1477 |
*---------------------------------------------------------------------- |
| 1478 |
*/ |
| 1479 |
|
| 1480 |
static int |
| 1481 |
InfoFunctionsCmd( |
| 1482 |
ClientData dummy, /* Not used. */ |
| 1483 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 1484 |
int objc, /* Number of arguments. */ |
| 1485 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 1486 |
{ |
| 1487 |
Tcl_Obj *script; |
| 1488 |
int code; |
| 1489 |
|
| 1490 |
if (objc > 2) { |
| 1491 |
Tcl_WrongNumArgs(interp, 1, objv, "?pattern?"); |
| 1492 |
return TCL_ERROR; |
| 1493 |
} |
| 1494 |
|
| 1495 |
script = Tcl_NewStringObj( |
| 1496 |
" ::apply [::list {{pattern *}} {\n" |
| 1497 |
" ::set cmds {}\n" |
| 1498 |
" ::foreach cmd [::info commands ::tcl::mathfunc::$pattern] {\n" |
| 1499 |
" ::lappend cmds [::namespace tail $cmd]\n" |
| 1500 |
" }\n" |
| 1501 |
" ::foreach cmd [::info commands tcl::mathfunc::$pattern] {\n" |
| 1502 |
" ::set cmd [::namespace tail $cmd]\n" |
| 1503 |
" ::if {$cmd ni $cmds} {\n" |
| 1504 |
" ::lappend cmds $cmd\n" |
| 1505 |
" }\n" |
| 1506 |
" }\n" |
| 1507 |
" ::return $cmds\n" |
| 1508 |
" } [::namespace current]] ", -1); |
| 1509 |
|
| 1510 |
if (objc == 2) { |
| 1511 |
Tcl_Obj *arg = Tcl_NewListObj(1, &(objv[1])); |
| 1512 |
|
| 1513 |
Tcl_AppendObjToObj(script, arg); |
| 1514 |
Tcl_DecrRefCount(arg); |
| 1515 |
} |
| 1516 |
|
| 1517 |
Tcl_IncrRefCount(script); |
| 1518 |
code = Tcl_EvalObjEx(interp, script, 0); |
| 1519 |
|
| 1520 |
Tcl_DecrRefCount(script); |
| 1521 |
|
| 1522 |
return code; |
| 1523 |
} |
| 1524 |
|
| 1525 |
/* |
| 1526 |
*---------------------------------------------------------------------- |
| 1527 |
* |
| 1528 |
* InfoHostnameCmd -- |
| 1529 |
* |
| 1530 |
* Called to implement the "info hostname" command that returns the host |
| 1531 |
* name. Handles the following syntax: |
| 1532 |
* |
| 1533 |
* info hostname |
| 1534 |
* |
| 1535 |
* Results: |
| 1536 |
* Returns TCL_OK if successful and TCL_ERROR if there is an error. |
| 1537 |
* |
| 1538 |
* Side effects: |
| 1539 |
* Returns a result in the interpreter's result object. If there is an |
| 1540 |
* error, the result is an error message. |
| 1541 |
* |
| 1542 |
*---------------------------------------------------------------------- |
| 1543 |
*/ |
| 1544 |
|
| 1545 |
static int |
| 1546 |
InfoHostnameCmd( |
| 1547 |
ClientData dummy, /* Not used. */ |
| 1548 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 1549 |
int objc, /* Number of arguments. */ |
| 1550 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 1551 |
{ |
| 1552 |
const char *name; |
| 1553 |
|
| 1554 |
if (objc != 1) { |
| 1555 |
Tcl_WrongNumArgs(interp, 1, objv, NULL); |
| 1556 |
return TCL_ERROR; |
| 1557 |
} |
| 1558 |
|
| 1559 |
name = Tcl_GetHostName(); |
| 1560 |
if (name) { |
| 1561 |
Tcl_SetObjResult(interp, Tcl_NewStringObj(name, -1)); |
| 1562 |
return TCL_OK; |
| 1563 |
} |
| 1564 |
|
| 1565 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 1566 |
"unable to determine name of host", -1)); |
| 1567 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "HOSTNAME", "UNKNOWN", (char *)NULL); |
| 1568 |
return TCL_ERROR; |
| 1569 |
} |
| 1570 |
|
| 1571 |
/* |
| 1572 |
*---------------------------------------------------------------------- |
| 1573 |
* |
| 1574 |
* InfoLevelCmd -- |
| 1575 |
* |
| 1576 |
* Called to implement the "info level" command that returns information |
| 1577 |
* about the call stack. Handles the following syntax: |
| 1578 |
* |
| 1579 |
* info level ?number? |
| 1580 |
* |
| 1581 |
* Results: |
| 1582 |
* Returns TCL_OK if successful and TCL_ERROR if there is an error. |
| 1583 |
* |
| 1584 |
* Side effects: |
| 1585 |
* Returns a result in the interpreter's result object. If there is an |
| 1586 |
* error, the result is an error message. |
| 1587 |
* |
| 1588 |
*---------------------------------------------------------------------- |
| 1589 |
*/ |
| 1590 |
|
| 1591 |
static int |
| 1592 |
InfoLevelCmd( |
| 1593 |
ClientData dummy, /* Not used. */ |
| 1594 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 1595 |
int objc, /* Number of arguments. */ |
| 1596 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 1597 |
{ |
| 1598 |
Interp *iPtr = (Interp *) interp; |
| 1599 |
|
| 1600 |
if (objc == 1) { /* Just "info level" */ |
| 1601 |
Tcl_SetObjResult(interp, Tcl_NewIntObj(iPtr->varFramePtr->level)); |
| 1602 |
return TCL_OK; |
| 1603 |
} |
| 1604 |
|
| 1605 |
if (objc == 2) { |
| 1606 |
int level; |
| 1607 |
CallFrame *framePtr, *rootFramePtr = iPtr->rootFramePtr; |
| 1608 |
|
| 1609 |
if (TclGetIntFromObj(interp, objv[1], &level) != TCL_OK) { |
| 1610 |
return TCL_ERROR; |
| 1611 |
} |
| 1612 |
if (level <= 0) { |
| 1613 |
if (iPtr->varFramePtr == rootFramePtr) { |
| 1614 |
goto levelError; |
| 1615 |
} |
| 1616 |
level += iPtr->varFramePtr->level; |
| 1617 |
} |
| 1618 |
for (framePtr=iPtr->varFramePtr ; framePtr!=rootFramePtr; |
| 1619 |
framePtr=framePtr->callerVarPtr) { |
| 1620 |
if (framePtr->level == level) { |
| 1621 |
break; |
| 1622 |
} |
| 1623 |
} |
| 1624 |
if (framePtr == rootFramePtr) { |
| 1625 |
goto levelError; |
| 1626 |
} |
| 1627 |
|
| 1628 |
Tcl_SetObjResult(interp, |
| 1629 |
Tcl_NewListObj(framePtr->objc, framePtr->objv)); |
| 1630 |
return TCL_OK; |
| 1631 |
} |
| 1632 |
|
| 1633 |
Tcl_WrongNumArgs(interp, 1, objv, "?number?"); |
| 1634 |
return TCL_ERROR; |
| 1635 |
|
| 1636 |
levelError: |
| 1637 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1638 |
"bad level \"%s\"", TclGetString(objv[1]))); |
| 1639 |
Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "LEVEL", |
| 1640 |
TclGetString(objv[1]), (char *)NULL); |
| 1641 |
return TCL_ERROR; |
| 1642 |
} |
| 1643 |
|
| 1644 |
/* |
| 1645 |
*---------------------------------------------------------------------- |
| 1646 |
* |
| 1647 |
* InfoLibraryCmd -- |
| 1648 |
* |
| 1649 |
* Called to implement the "info library" command that returns the |
| 1650 |
* library directory for the Tcl installation. Handles the following |
| 1651 |
* syntax: |
| 1652 |
* |
| 1653 |
* info library |
| 1654 |
* |
| 1655 |
* Results: |
| 1656 |
* Returns TCL_OK if successful and TCL_ERROR if there is an error. |
| 1657 |
* |
| 1658 |
* Side effects: |
| 1659 |
* Returns a result in the interpreter's result object. If there is an |
| 1660 |
* error, the result is an error message. |
| 1661 |
* |
| 1662 |
*---------------------------------------------------------------------- |
| 1663 |
*/ |
| 1664 |
|
| 1665 |
static int |
| 1666 |
InfoLibraryCmd( |
| 1667 |
ClientData dummy, /* Not used. */ |
| 1668 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 1669 |
int objc, /* Number of arguments. */ |
| 1670 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 1671 |
{ |
| 1672 |
const char *libDirName; |
| 1673 |
|
| 1674 |
if (objc != 1) { |
| 1675 |
Tcl_WrongNumArgs(interp, 1, objv, NULL); |
| 1676 |
return TCL_ERROR; |
| 1677 |
} |
| 1678 |
|
| 1679 |
libDirName = Tcl_GetVar(interp, "tcl_library", TCL_GLOBAL_ONLY); |
| 1680 |
if (libDirName != NULL) { |
| 1681 |
Tcl_SetObjResult(interp, Tcl_NewStringObj(libDirName, -1)); |
| 1682 |
return TCL_OK; |
| 1683 |
} |
| 1684 |
|
| 1685 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 1686 |
"no library has been specified for Tcl", -1)); |
| 1687 |
Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "VARIABLE", "tcl_library", (char *)NULL); |
| 1688 |
return TCL_ERROR; |
| 1689 |
} |
| 1690 |
|
| 1691 |
/* |
| 1692 |
*---------------------------------------------------------------------- |
| 1693 |
* |
| 1694 |
* InfoLoadedCmd -- |
| 1695 |
* |
| 1696 |
* Called to implement the "info loaded" command that returns the |
| 1697 |
* packages that have been loaded into an interpreter. Handles the |
| 1698 |
* following syntax: |
| 1699 |
* |
| 1700 |
* info loaded ?interp? |
| 1701 |
* |
| 1702 |
* Results: |
| 1703 |
* Returns TCL_OK if successful and TCL_ERROR if there is an error. |
| 1704 |
* |
| 1705 |
* Side effects: |
| 1706 |
* Returns a result in the interpreter's result object. If there is an |
| 1707 |
* error, the result is an error message. |
| 1708 |
* |
| 1709 |
*---------------------------------------------------------------------- |
| 1710 |
*/ |
| 1711 |
|
| 1712 |
static int |
| 1713 |
InfoLoadedCmd( |
| 1714 |
ClientData dummy, /* Not used. */ |
| 1715 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 1716 |
int objc, /* Number of arguments. */ |
| 1717 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 1718 |
{ |
| 1719 |
const char *interpName; |
| 1720 |
|
| 1721 |
if ((objc != 1) && (objc != 2)) { |
| 1722 |
Tcl_WrongNumArgs(interp, 1, objv, "?interp?"); |
| 1723 |
return TCL_ERROR; |
| 1724 |
} |
| 1725 |
|
| 1726 |
if (objc == 1) { /* Get loaded pkgs in all interpreters. */ |
| 1727 |
interpName = NULL; |
| 1728 |
} else { /* Get pkgs just in specified interp. */ |
| 1729 |
interpName = TclGetString(objv[1]); |
| 1730 |
} |
| 1731 |
return TclGetLoadedPackages(interp, interpName); |
| 1732 |
} |
| 1733 |
|
| 1734 |
/* |
| 1735 |
*---------------------------------------------------------------------- |
| 1736 |
* |
| 1737 |
* InfoNameOfExecutableCmd -- |
| 1738 |
* |
| 1739 |
* Called to implement the "info nameofexecutable" command that returns |
| 1740 |
* the name of the binary file running this application. Handles the |
| 1741 |
* following syntax: |
| 1742 |
* |
| 1743 |
* info nameofexecutable |
| 1744 |
* |
| 1745 |
* Results: |
| 1746 |
* Returns TCL_OK if successful and TCL_ERROR if there is an error. |
| 1747 |
* |
| 1748 |
* Side effects: |
| 1749 |
* Returns a result in the interpreter's result object. If there is an |
| 1750 |
* error, the result is an error message. |
| 1751 |
* |
| 1752 |
*---------------------------------------------------------------------- |
| 1753 |
*/ |
| 1754 |
|
| 1755 |
static int |
| 1756 |
InfoNameOfExecutableCmd( |
| 1757 |
ClientData dummy, /* Not used. */ |
| 1758 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 1759 |
int objc, /* Number of arguments. */ |
| 1760 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 1761 |
{ |
| 1762 |
if (objc != 1) { |
| 1763 |
Tcl_WrongNumArgs(interp, 1, objv, NULL); |
| 1764 |
return TCL_ERROR; |
| 1765 |
} |
| 1766 |
Tcl_SetObjResult(interp, TclGetObjNameOfExecutable()); |
| 1767 |
return TCL_OK; |
| 1768 |
} |
| 1769 |
|
| 1770 |
/* |
| 1771 |
*---------------------------------------------------------------------- |
| 1772 |
* |
| 1773 |
* InfoPatchLevelCmd -- |
| 1774 |
* |
| 1775 |
* Called to implement the "info patchlevel" command that returns the |
| 1776 |
* default value for an argument to a procedure. Handles the following |
| 1777 |
* syntax: |
| 1778 |
* |
| 1779 |
* info patchlevel |
| 1780 |
* |
| 1781 |
* Results: |
| 1782 |
* Returns TCL_OK if successful and TCL_ERROR if there is an error. |
| 1783 |
* |
| 1784 |
* Side effects: |
| 1785 |
* Returns a result in the interpreter's result object. If there is an |
| 1786 |
* error, the result is an error message. |
| 1787 |
* |
| 1788 |
*---------------------------------------------------------------------- |
| 1789 |
*/ |
| 1790 |
|
| 1791 |
static int |
| 1792 |
InfoPatchLevelCmd( |
| 1793 |
ClientData dummy, /* Not used. */ |
| 1794 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 1795 |
int objc, /* Number of arguments. */ |
| 1796 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 1797 |
{ |
| 1798 |
const char *patchlevel; |
| 1799 |
|
| 1800 |
if (objc != 1) { |
| 1801 |
Tcl_WrongNumArgs(interp, 1, objv, NULL); |
| 1802 |
return TCL_ERROR; |
| 1803 |
} |
| 1804 |
|
| 1805 |
patchlevel = Tcl_GetVar(interp, "tcl_patchLevel", |
| 1806 |
(TCL_GLOBAL_ONLY | TCL_LEAVE_ERR_MSG)); |
| 1807 |
if (patchlevel != NULL) { |
| 1808 |
Tcl_SetObjResult(interp, Tcl_NewStringObj(patchlevel, -1)); |
| 1809 |
return TCL_OK; |
| 1810 |
} |
| 1811 |
return TCL_ERROR; |
| 1812 |
} |
| 1813 |
|
| 1814 |
/* |
| 1815 |
*---------------------------------------------------------------------- |
| 1816 |
* |
| 1817 |
* InfoProcsCmd -- |
| 1818 |
* |
| 1819 |
* Called to implement the "info procs" command that returns the list of |
| 1820 |
* procedures in the interpreter that match an optional pattern. The |
| 1821 |
* pattern, if any, consists of an optional sequence of namespace names |
| 1822 |
* separated by "::" qualifiers, which is followed by a glob-style |
| 1823 |
* pattern that restricts which commands are returned. Handles the |
| 1824 |
* following syntax: |
| 1825 |
* |
| 1826 |
* info procs ?pattern? |
| 1827 |
* |
| 1828 |
* Results: |
| 1829 |
* Returns TCL_OK if successful and TCL_ERROR if there is an error. |
| 1830 |
* |
| 1831 |
* Side effects: |
| 1832 |
* Returns a result in the interpreter's result object. If there is an |
| 1833 |
* error, the result is an error message. |
| 1834 |
* |
| 1835 |
*---------------------------------------------------------------------- |
| 1836 |
*/ |
| 1837 |
|
| 1838 |
static int |
| 1839 |
InfoProcsCmd( |
| 1840 |
ClientData dummy, /* Not used. */ |
| 1841 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 1842 |
int objc, /* Number of arguments. */ |
| 1843 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 1844 |
{ |
| 1845 |
const char *cmdName, *pattern; |
| 1846 |
const char *simplePattern; |
| 1847 |
Namespace *nsPtr; |
| 1848 |
#ifdef INFO_PROCS_SEARCH_GLOBAL_NS |
| 1849 |
Namespace *globalNsPtr = (Namespace *) Tcl_GetGlobalNamespace(interp); |
| 1850 |
#endif |
| 1851 |
Namespace *currNsPtr = (Namespace *) Tcl_GetCurrentNamespace(interp); |
| 1852 |
Tcl_Obj *listPtr, *elemObjPtr; |
| 1853 |
int specificNsInPattern = 0;/* Init. to avoid compiler warning. */ |
| 1854 |
Tcl_HashEntry *entryPtr; |
| 1855 |
Tcl_HashSearch search; |
| 1856 |
Command *cmdPtr, *realCmdPtr; |
| 1857 |
|
| 1858 |
/* |
| 1859 |
* Get the pattern and find the "effective namespace" in which to list |
| 1860 |
* procs. |
| 1861 |
*/ |
| 1862 |
|
| 1863 |
if (objc == 1) { |
| 1864 |
simplePattern = NULL; |
| 1865 |
nsPtr = currNsPtr; |
| 1866 |
specificNsInPattern = 0; |
| 1867 |
} else if (objc == 2) { |
| 1868 |
/* |
| 1869 |
* From the pattern, get the effective namespace and the simple |
| 1870 |
* pattern (no namespace qualifiers or ::'s) at the end. If an error |
| 1871 |
* was found while parsing the pattern, return it. Otherwise, if the |
| 1872 |
* namespace wasn't found, just leave nsPtr NULL: we will return an |
| 1873 |
* empty list since no commands there can be found. |
| 1874 |
*/ |
| 1875 |
|
| 1876 |
Namespace *dummy1NsPtr, *dummy2NsPtr; |
| 1877 |
|
| 1878 |
pattern = TclGetString(objv[1]); |
| 1879 |
TclGetNamespaceForQualName(interp, pattern, NULL, /*flags*/ 0, &nsPtr, |
| 1880 |
&dummy1NsPtr, &dummy2NsPtr, &simplePattern); |
| 1881 |
|
| 1882 |
if (nsPtr != NULL) { /* We successfully found the pattern's ns. */ |
| 1883 |
specificNsInPattern = (strcmp(simplePattern, pattern) != 0); |
| 1884 |
} |
| 1885 |
} else { |
| 1886 |
Tcl_WrongNumArgs(interp, 1, objv, "?pattern?"); |
| 1887 |
return TCL_ERROR; |
| 1888 |
} |
| 1889 |
|
| 1890 |
if (nsPtr == NULL) { |
| 1891 |
return TCL_OK; |
| 1892 |
} |
| 1893 |
|
| 1894 |
/* |
| 1895 |
* Scan through the effective namespace's command table and create a list |
| 1896 |
* with all procs that match the pattern. If a specific namespace was |
| 1897 |
* requested in the pattern, qualify the command names with the namespace |
| 1898 |
* name. |
| 1899 |
*/ |
| 1900 |
|
| 1901 |
listPtr = Tcl_NewListObj(0, NULL); |
| 1902 |
#ifndef INFO_PROCS_SEARCH_GLOBAL_NS |
| 1903 |
if (simplePattern != NULL && TclMatchIsTrivial(simplePattern)) { |
| 1904 |
entryPtr = Tcl_FindHashEntry(&nsPtr->cmdTable, simplePattern); |
| 1905 |
if (entryPtr != NULL) { |
| 1906 |
cmdPtr = (Command *)Tcl_GetHashValue(entryPtr); |
| 1907 |
|
| 1908 |
if (!TclIsProc(cmdPtr)) { |
| 1909 |
realCmdPtr = (Command *) |
| 1910 |
TclGetOriginalCommand((Tcl_Command) cmdPtr); |
| 1911 |
if (realCmdPtr != NULL && TclIsProc(realCmdPtr)) { |
| 1912 |
goto simpleProcOK; |
| 1913 |
} |
| 1914 |
} else { |
| 1915 |
simpleProcOK: |
| 1916 |
if (specificNsInPattern) { |
| 1917 |
TclNewObj(elemObjPtr); |
| 1918 |
Tcl_GetCommandFullName(interp, (Tcl_Command) cmdPtr, |
| 1919 |
elemObjPtr); |
| 1920 |
} else { |
| 1921 |
elemObjPtr = Tcl_NewStringObj(simplePattern, -1); |
| 1922 |
} |
| 1923 |
Tcl_ListObjAppendElement(interp, listPtr, elemObjPtr); |
| 1924 |
} |
| 1925 |
} |
| 1926 |
} else |
| 1927 |
#endif /* !INFO_PROCS_SEARCH_GLOBAL_NS */ |
| 1928 |
{ |
| 1929 |
entryPtr = Tcl_FirstHashEntry(&nsPtr->cmdTable, &search); |
| 1930 |
while (entryPtr != NULL) { |
| 1931 |
cmdName = (const char *)Tcl_GetHashKey(&nsPtr->cmdTable, entryPtr); |
| 1932 |
if ((simplePattern == NULL) |
| 1933 |
|| Tcl_StringMatch(cmdName, simplePattern)) { |
| 1934 |
cmdPtr = (Command *)Tcl_GetHashValue(entryPtr); |
| 1935 |
|
| 1936 |
if (!TclIsProc(cmdPtr)) { |
| 1937 |
realCmdPtr = (Command *) |
| 1938 |
TclGetOriginalCommand((Tcl_Command) cmdPtr); |
| 1939 |
if (realCmdPtr != NULL && TclIsProc(realCmdPtr)) { |
| 1940 |
goto procOK; |
| 1941 |
} |
| 1942 |
} else { |
| 1943 |
procOK: |
| 1944 |
if (specificNsInPattern) { |
| 1945 |
TclNewObj(elemObjPtr); |
| 1946 |
Tcl_GetCommandFullName(interp, (Tcl_Command) cmdPtr, |
| 1947 |
elemObjPtr); |
| 1948 |
} else { |
| 1949 |
elemObjPtr = Tcl_NewStringObj(cmdName, -1); |
| 1950 |
} |
| 1951 |
Tcl_ListObjAppendElement(interp, listPtr, elemObjPtr); |
| 1952 |
} |
| 1953 |
} |
| 1954 |
entryPtr = Tcl_NextHashEntry(&search); |
| 1955 |
} |
| 1956 |
|
| 1957 |
/* |
| 1958 |
* If the effective namespace isn't the global :: namespace, and a |
| 1959 |
* specific namespace wasn't requested in the pattern, then add in all |
| 1960 |
* global :: procs that match the simple pattern. Of course, we add in |
| 1961 |
* only those procs that aren't hidden by a proc in the effective |
| 1962 |
* namespace. |
| 1963 |
*/ |
| 1964 |
|
| 1965 |
#ifdef INFO_PROCS_SEARCH_GLOBAL_NS |
| 1966 |
/* |
| 1967 |
* If "info procs" worked like "info commands", returning the commands |
| 1968 |
* also seen in the global namespace, then you would include this |
| 1969 |
* code. As this could break backwards compatibility with 8.0-8.2, we |
| 1970 |
* decided not to "fix" it in 8.3, leaving the behavior slightly |
| 1971 |
* different. |
| 1972 |
*/ |
| 1973 |
|
| 1974 |
if ((nsPtr != globalNsPtr) && !specificNsInPattern) { |
| 1975 |
entryPtr = Tcl_FirstHashEntry(&globalNsPtr->cmdTable, &search); |
| 1976 |
while (entryPtr != NULL) { |
| 1977 |
cmdName = Tcl_GetHashKey(&globalNsPtr->cmdTable, entryPtr); |
| 1978 |
if ((simplePattern == NULL) |
| 1979 |
|| Tcl_StringMatch(cmdName, simplePattern)) { |
| 1980 |
if (Tcl_FindHashEntry(&nsPtr->cmdTable,cmdName) == NULL) { |
| 1981 |
cmdPtr = Tcl_GetHashValue(entryPtr); |
| 1982 |
realCmdPtr = (Command *) TclGetOriginalCommand( |
| 1983 |
(Tcl_Command) cmdPtr); |
| 1984 |
|
| 1985 |
if (TclIsProc(cmdPtr) || ((realCmdPtr != NULL) |
| 1986 |
&& TclIsProc(realCmdPtr))) { |
| 1987 |
Tcl_ListObjAppendElement(interp, listPtr, |
| 1988 |
Tcl_NewStringObj(cmdName, -1)); |
| 1989 |
} |
| 1990 |
} |
| 1991 |
} |
| 1992 |
entryPtr = Tcl_NextHashEntry(&search); |
| 1993 |
} |
| 1994 |
} |
| 1995 |
#endif |
| 1996 |
} |
| 1997 |
|
| 1998 |
Tcl_SetObjResult(interp, listPtr); |
| 1999 |
return TCL_OK; |
| 2000 |
} |
| 2001 |
|
| 2002 |
/* |
| 2003 |
*---------------------------------------------------------------------- |
| 2004 |
* |
| 2005 |
* InfoScriptCmd -- |
| 2006 |
* |
| 2007 |
* Called to implement the "info script" command that returns the script |
| 2008 |
* file that is currently being evaluated. Handles the following syntax: |
| 2009 |
* |
| 2010 |
* info script ?newName? |
| 2011 |
* |
| 2012 |
* If newName is specified, it will set that as the internal name. |
| 2013 |
* |
| 2014 |
* Results: |
| 2015 |
* Returns TCL_OK if successful and TCL_ERROR if there is an error. |
| 2016 |
* |
| 2017 |
* Side effects: |
| 2018 |
* Returns a result in the interpreter's result object. If there is an |
| 2019 |
* error, the result is an error message. It may change the internal |
| 2020 |
* script filename. |
| 2021 |
* |
| 2022 |
*---------------------------------------------------------------------- |
| 2023 |
*/ |
| 2024 |
|
| 2025 |
static int |
| 2026 |
InfoScriptCmd( |
| 2027 |
ClientData dummy, /* Not used. */ |
| 2028 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 2029 |
int objc, /* Number of arguments. */ |
| 2030 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 2031 |
{ |
| 2032 |
Interp *iPtr = (Interp *) interp; |
| 2033 |
if ((objc != 1) && (objc != 2)) { |
| 2034 |
Tcl_WrongNumArgs(interp, 1, objv, "?filename?"); |
| 2035 |
return TCL_ERROR; |
| 2036 |
} |
| 2037 |
|
| 2038 |
if (objc == 2) { |
| 2039 |
if (iPtr->scriptFile != NULL) { |
| 2040 |
Tcl_DecrRefCount(iPtr->scriptFile); |
| 2041 |
} |
| 2042 |
iPtr->scriptFile = objv[1]; |
| 2043 |
Tcl_IncrRefCount(iPtr->scriptFile); |
| 2044 |
} |
| 2045 |
if (iPtr->scriptFile != NULL) { |
| 2046 |
Tcl_SetObjResult(interp, iPtr->scriptFile); |
| 2047 |
} |
| 2048 |
return TCL_OK; |
| 2049 |
} |
| 2050 |
|
| 2051 |
/* |
| 2052 |
*---------------------------------------------------------------------- |
| 2053 |
* |
| 2054 |
* InfoSharedlibCmd -- |
| 2055 |
* |
| 2056 |
* Called to implement the "info sharedlibextension" command that returns |
| 2057 |
* the file extension used for shared libraries. Handles the following |
| 2058 |
* syntax: |
| 2059 |
* |
| 2060 |
* info sharedlibextension |
| 2061 |
* |
| 2062 |
* Results: |
| 2063 |
* Returns TCL_OK if successful and TCL_ERROR if there is an error. |
| 2064 |
* |
| 2065 |
* Side effects: |
| 2066 |
* Returns a result in the interpreter's result object. If there is an |
| 2067 |
* error, the result is an error message. |
| 2068 |
* |
| 2069 |
*---------------------------------------------------------------------- |
| 2070 |
*/ |
| 2071 |
|
| 2072 |
static int |
| 2073 |
InfoSharedlibCmd( |
| 2074 |
ClientData dummy, /* Not used. */ |
| 2075 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 2076 |
int objc, /* Number of arguments. */ |
| 2077 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 2078 |
{ |
| 2079 |
if (objc != 1) { |
| 2080 |
Tcl_WrongNumArgs(interp, 1, objv, NULL); |
| 2081 |
return TCL_ERROR; |
| 2082 |
} |
| 2083 |
|
| 2084 |
#ifdef TCL_SHLIB_EXT |
| 2085 |
Tcl_SetObjResult(interp, Tcl_NewStringObj(TCL_SHLIB_EXT, -1)); |
| 2086 |
#endif |
| 2087 |
return TCL_OK; |
| 2088 |
} |
| 2089 |
|
| 2090 |
/* |
| 2091 |
*---------------------------------------------------------------------- |
| 2092 |
* |
| 2093 |
* InfoTclVersionCmd -- |
| 2094 |
* |
| 2095 |
* Called to implement the "info tclversion" command that returns the |
| 2096 |
* version number for this Tcl library. Handles the following syntax: |
| 2097 |
* |
| 2098 |
* info tclversion |
| 2099 |
* |
| 2100 |
* Results: |
| 2101 |
* Returns TCL_OK if successful and TCL_ERROR if there is an error. |
| 2102 |
* |
| 2103 |
* Side effects: |
| 2104 |
* Returns a result in the interpreter's result object. If there is an |
| 2105 |
* error, the result is an error message. |
| 2106 |
* |
| 2107 |
*---------------------------------------------------------------------- |
| 2108 |
*/ |
| 2109 |
|
| 2110 |
static int |
| 2111 |
InfoTclVersionCmd( |
| 2112 |
ClientData dummy, /* Not used. */ |
| 2113 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 2114 |
int objc, /* Number of arguments. */ |
| 2115 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 2116 |
{ |
| 2117 |
Tcl_Obj *version; |
| 2118 |
|
| 2119 |
if (objc != 1) { |
| 2120 |
Tcl_WrongNumArgs(interp, 1, objv, NULL); |
| 2121 |
return TCL_ERROR; |
| 2122 |
} |
| 2123 |
|
| 2124 |
version = Tcl_GetVar2Ex(interp, "tcl_version", NULL, |
| 2125 |
(TCL_GLOBAL_ONLY | TCL_LEAVE_ERR_MSG)); |
| 2126 |
if (version != NULL) { |
| 2127 |
Tcl_SetObjResult(interp, version); |
| 2128 |
return TCL_OK; |
| 2129 |
} |
| 2130 |
return TCL_ERROR; |
| 2131 |
} |
| 2132 |
|
| 2133 |
/* |
| 2134 |
*---------------------------------------------------------------------- |
| 2135 |
* |
| 2136 |
* Tcl_JoinObjCmd -- |
| 2137 |
* |
| 2138 |
* This procedure is invoked to process the "join" Tcl command. See the |
| 2139 |
* user documentation for details on what it does. |
| 2140 |
* |
| 2141 |
* Results: |
| 2142 |
* A standard Tcl object result. |
| 2143 |
* |
| 2144 |
* Side effects: |
| 2145 |
* See the user documentation. |
| 2146 |
* |
| 2147 |
*---------------------------------------------------------------------- |
| 2148 |
*/ |
| 2149 |
|
| 2150 |
int |
| 2151 |
Tcl_JoinObjCmd( |
| 2152 |
ClientData dummy, /* Not used. */ |
| 2153 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 2154 |
int objc, /* Number of arguments. */ |
| 2155 |
Tcl_Obj *const objv[]) /* The argument objects. */ |
| 2156 |
{ |
| 2157 |
int listLen, i; |
| 2158 |
Tcl_Obj *resObjPtr, *joinObjPtr, **elemPtrs; |
| 2159 |
|
| 2160 |
if ((objc < 2) || (objc > 3)) { |
| 2161 |
Tcl_WrongNumArgs(interp, 1, objv, "list ?joinString?"); |
| 2162 |
return TCL_ERROR; |
| 2163 |
} |
| 2164 |
|
| 2165 |
/* |
| 2166 |
* Make sure the list argument is a list object and get its length and a |
| 2167 |
* pointer to its array of element pointers. |
| 2168 |
*/ |
| 2169 |
|
| 2170 |
if (TclListObjGetElements(interp, objv[1], &listLen, |
| 2171 |
&elemPtrs) != TCL_OK) { |
| 2172 |
return TCL_ERROR; |
| 2173 |
} |
| 2174 |
|
| 2175 |
joinObjPtr = (objc == 2) ? Tcl_NewStringObj(" ", 1) : objv[2]; |
| 2176 |
Tcl_IncrRefCount(joinObjPtr); |
| 2177 |
|
| 2178 |
TclNewObj(resObjPtr); |
| 2179 |
for (i = 0; i < listLen; i++) { |
| 2180 |
if (i > 0) { |
| 2181 |
|
| 2182 |
/* |
| 2183 |
* NOTE: This code is relying on Tcl_AppendObjToObj() **NOT** |
| 2184 |
* to shimmer joinObjPtr. If it did, then the case where |
| 2185 |
* objv[1] and objv[2] are the same value would not be safe. |
| 2186 |
* Accessing elemPtrs would crash. |
| 2187 |
*/ |
| 2188 |
|
| 2189 |
Tcl_AppendObjToObj(resObjPtr, joinObjPtr); |
| 2190 |
} |
| 2191 |
Tcl_AppendObjToObj(resObjPtr, elemPtrs[i]); |
| 2192 |
} |
| 2193 |
Tcl_DecrRefCount(joinObjPtr); |
| 2194 |
Tcl_SetObjResult(interp, resObjPtr); |
| 2195 |
return TCL_OK; |
| 2196 |
} |
| 2197 |
|
| 2198 |
/* |
| 2199 |
*---------------------------------------------------------------------- |
| 2200 |
* |
| 2201 |
* Tcl_LassignObjCmd -- |
| 2202 |
* |
| 2203 |
* This object-based procedure is invoked to process the "lassign" Tcl |
| 2204 |
* command. See the user documentation for details on what it does. |
| 2205 |
* |
| 2206 |
* Results: |
| 2207 |
* A standard Tcl object result. |
| 2208 |
* |
| 2209 |
* Side effects: |
| 2210 |
* See the user documentation. |
| 2211 |
* |
| 2212 |
*---------------------------------------------------------------------- |
| 2213 |
*/ |
| 2214 |
|
| 2215 |
int |
| 2216 |
Tcl_LassignObjCmd( |
| 2217 |
ClientData dummy, /* Not used. */ |
| 2218 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 2219 |
int objc, /* Number of arguments. */ |
| 2220 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 2221 |
{ |
| 2222 |
Tcl_Obj *listCopyPtr; |
| 2223 |
Tcl_Obj **listObjv; /* The contents of the list. */ |
| 2224 |
int listObjc; /* The length of the list. */ |
| 2225 |
int code = TCL_OK; |
| 2226 |
|
| 2227 |
if (objc < 2) { |
| 2228 |
Tcl_WrongNumArgs(interp, 1, objv, "list ?varName ...?"); |
| 2229 |
return TCL_ERROR; |
| 2230 |
} |
| 2231 |
|
| 2232 |
listCopyPtr = TclListObjCopy(interp, objv[1]); |
| 2233 |
if (listCopyPtr == NULL) { |
| 2234 |
return TCL_ERROR; |
| 2235 |
} |
| 2236 |
|
| 2237 |
TclListObjGetElements(NULL, listCopyPtr, &listObjc, &listObjv); |
| 2238 |
|
| 2239 |
objc -= 2; |
| 2240 |
objv += 2; |
| 2241 |
while (code == TCL_OK && objc > 0 && listObjc > 0) { |
| 2242 |
if (Tcl_ObjSetVar2(interp, *objv++, NULL, *listObjv++, |
| 2243 |
TCL_LEAVE_ERR_MSG) == NULL) { |
| 2244 |
code = TCL_ERROR; |
| 2245 |
} |
| 2246 |
objc--; |
| 2247 |
listObjc--; |
| 2248 |
} |
| 2249 |
|
| 2250 |
if (code == TCL_OK && objc > 0) { |
| 2251 |
Tcl_Obj *emptyObj; |
| 2252 |
|
| 2253 |
TclNewObj(emptyObj); |
| 2254 |
Tcl_IncrRefCount(emptyObj); |
| 2255 |
while (code == TCL_OK && objc-- > 0) { |
| 2256 |
if (Tcl_ObjSetVar2(interp, *objv++, NULL, emptyObj, |
| 2257 |
TCL_LEAVE_ERR_MSG) == NULL) { |
| 2258 |
code = TCL_ERROR; |
| 2259 |
} |
| 2260 |
} |
| 2261 |
Tcl_DecrRefCount(emptyObj); |
| 2262 |
} |
| 2263 |
|
| 2264 |
if (code == TCL_OK && listObjc > 0) { |
| 2265 |
Tcl_SetObjResult(interp, Tcl_NewListObj(listObjc, listObjv)); |
| 2266 |
} |
| 2267 |
|
| 2268 |
Tcl_DecrRefCount(listCopyPtr); |
| 2269 |
return code; |
| 2270 |
} |
| 2271 |
|
| 2272 |
/* |
| 2273 |
*---------------------------------------------------------------------- |
| 2274 |
* |
| 2275 |
* Tcl_LindexObjCmd -- |
| 2276 |
* |
| 2277 |
* This object-based procedure is invoked to process the "lindex" Tcl |
| 2278 |
* command. See the user documentation for details on what it does. |
| 2279 |
* |
| 2280 |
* Results: |
| 2281 |
* A standard Tcl object result. |
| 2282 |
* |
| 2283 |
* Side effects: |
| 2284 |
* See the user documentation. |
| 2285 |
* |
| 2286 |
*---------------------------------------------------------------------- |
| 2287 |
*/ |
| 2288 |
|
| 2289 |
int |
| 2290 |
Tcl_LindexObjCmd( |
| 2291 |
ClientData dummy, /* Not used. */ |
| 2292 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 2293 |
int objc, /* Number of arguments. */ |
| 2294 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 2295 |
{ |
| 2296 |
|
| 2297 |
Tcl_Obj *elemPtr; /* Pointer to the element being extracted. */ |
| 2298 |
|
| 2299 |
if (objc < 2) { |
| 2300 |
Tcl_WrongNumArgs(interp, 1, objv, "list ?index ...?"); |
| 2301 |
return TCL_ERROR; |
| 2302 |
} |
| 2303 |
|
| 2304 |
/* |
| 2305 |
* If objc==3, then objv[2] may be either a single index or a list of |
| 2306 |
* indices: go to TclLindexList to determine which. If objc>=4, or |
| 2307 |
* objc==2, then objv[2 .. objc-2] are all single indices and processed as |
| 2308 |
* such in TclLindexFlat. |
| 2309 |
*/ |
| 2310 |
|
| 2311 |
if (objc == 3) { |
| 2312 |
elemPtr = TclLindexList(interp, objv[1], objv[2]); |
| 2313 |
} else { |
| 2314 |
elemPtr = TclLindexFlat(interp, objv[1], objc-2, objv+2); |
| 2315 |
} |
| 2316 |
|
| 2317 |
/* |
| 2318 |
* Set the interpreter's object result to the last element extracted. |
| 2319 |
*/ |
| 2320 |
|
| 2321 |
if (elemPtr == NULL) { |
| 2322 |
return TCL_ERROR; |
| 2323 |
} |
| 2324 |
|
| 2325 |
Tcl_SetObjResult(interp, elemPtr); |
| 2326 |
Tcl_DecrRefCount(elemPtr); |
| 2327 |
return TCL_OK; |
| 2328 |
} |
| 2329 |
|
| 2330 |
/* |
| 2331 |
*---------------------------------------------------------------------- |
| 2332 |
* |
| 2333 |
* Tcl_LinsertObjCmd -- |
| 2334 |
* |
| 2335 |
* This object-based procedure is invoked to process the "linsert" Tcl |
| 2336 |
* command. See the user documentation for details on what it does. |
| 2337 |
* |
| 2338 |
* Results: |
| 2339 |
* A new Tcl list object formed by inserting zero or more elements into a |
| 2340 |
* list. |
| 2341 |
* |
| 2342 |
* Side effects: |
| 2343 |
* See the user documentation. |
| 2344 |
* |
| 2345 |
*---------------------------------------------------------------------- |
| 2346 |
*/ |
| 2347 |
|
| 2348 |
int |
| 2349 |
Tcl_LinsertObjCmd( |
| 2350 |
ClientData dummy, /* Not used. */ |
| 2351 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 2352 |
int objc, /* Number of arguments. */ |
| 2353 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 2354 |
{ |
| 2355 |
Tcl_Obj *listPtr; |
| 2356 |
int index, len, result; |
| 2357 |
|
| 2358 |
if (objc < 3) { |
| 2359 |
Tcl_WrongNumArgs(interp, 1, objv, "list index ?element ...?"); |
| 2360 |
return TCL_ERROR; |
| 2361 |
} |
| 2362 |
|
| 2363 |
result = TclListObjLength(interp, objv[1], &len); |
| 2364 |
if (result != TCL_OK) { |
| 2365 |
return result; |
| 2366 |
} |
| 2367 |
|
| 2368 |
/* |
| 2369 |
* Get the index. "end" is interpreted to be the index after the last |
| 2370 |
* element, such that using it will cause any inserted elements to be |
| 2371 |
* appended to the list. |
| 2372 |
*/ |
| 2373 |
|
| 2374 |
result = TclGetIntForIndexM(interp, objv[2], /*end*/ len, &index); |
| 2375 |
if (result != TCL_OK) { |
| 2376 |
return result; |
| 2377 |
} |
| 2378 |
if (index > len) { |
| 2379 |
index = len; |
| 2380 |
} |
| 2381 |
|
| 2382 |
/* |
| 2383 |
* If the list object is unshared we can modify it directly. Otherwise we |
| 2384 |
* create a copy to modify: this is "copy on write". |
| 2385 |
*/ |
| 2386 |
|
| 2387 |
listPtr = objv[1]; |
| 2388 |
if (Tcl_IsShared(listPtr)) { |
| 2389 |
listPtr = TclListObjCopy(NULL, listPtr); |
| 2390 |
} |
| 2391 |
|
| 2392 |
if ((objc == 4) && (index == len)) { |
| 2393 |
/* |
| 2394 |
* Special case: insert one element at the end of the list. |
| 2395 |
*/ |
| 2396 |
|
| 2397 |
Tcl_ListObjAppendElement(NULL, listPtr, objv[3]); |
| 2398 |
} else { |
| 2399 |
if (TCL_OK != Tcl_ListObjReplace(interp, listPtr, index, 0, |
| 2400 |
(objc-3), &(objv[3]))) { |
| 2401 |
return TCL_ERROR; |
| 2402 |
} |
| 2403 |
} |
| 2404 |
|
| 2405 |
/* |
| 2406 |
* Set the interpreter's object result. |
| 2407 |
*/ |
| 2408 |
|
| 2409 |
Tcl_SetObjResult(interp, listPtr); |
| 2410 |
return TCL_OK; |
| 2411 |
} |
| 2412 |
|
| 2413 |
/* |
| 2414 |
*---------------------------------------------------------------------- |
| 2415 |
* |
| 2416 |
* Tcl_ListObjCmd -- |
| 2417 |
* |
| 2418 |
* This procedure is invoked to process the "list" Tcl command. See the |
| 2419 |
* user documentation for details on what it does. |
| 2420 |
* |
| 2421 |
* Results: |
| 2422 |
* A standard Tcl object result. |
| 2423 |
* |
| 2424 |
* Side effects: |
| 2425 |
* See the user documentation. |
| 2426 |
* |
| 2427 |
*---------------------------------------------------------------------- |
| 2428 |
*/ |
| 2429 |
|
| 2430 |
int |
| 2431 |
Tcl_ListObjCmd( |
| 2432 |
ClientData dummy, /* Not used. */ |
| 2433 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 2434 |
int objc, /* Number of arguments. */ |
| 2435 |
Tcl_Obj *const objv[]) |
| 2436 |
/* The argument objects. */ |
| 2437 |
{ |
| 2438 |
/* |
| 2439 |
* If there are no list elements, the result is an empty object. |
| 2440 |
* Otherwise set the interpreter's result object to be a list object. |
| 2441 |
*/ |
| 2442 |
|
| 2443 |
if (objc > 1) { |
| 2444 |
Tcl_SetObjResult(interp, Tcl_NewListObj(objc-1, &objv[1])); |
| 2445 |
} |
| 2446 |
return TCL_OK; |
| 2447 |
} |
| 2448 |
|
| 2449 |
/* |
| 2450 |
*---------------------------------------------------------------------- |
| 2451 |
* |
| 2452 |
* Tcl_LlengthObjCmd -- |
| 2453 |
* |
| 2454 |
* This object-based procedure is invoked to process the "llength" Tcl |
| 2455 |
* command. See the user documentation for details on what it does. |
| 2456 |
* |
| 2457 |
* Results: |
| 2458 |
* A standard Tcl object result. |
| 2459 |
* |
| 2460 |
* Side effects: |
| 2461 |
* See the user documentation. |
| 2462 |
* |
| 2463 |
*---------------------------------------------------------------------- |
| 2464 |
*/ |
| 2465 |
|
| 2466 |
int |
| 2467 |
Tcl_LlengthObjCmd( |
| 2468 |
ClientData dummy, /* Not used. */ |
| 2469 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 2470 |
int objc, /* Number of arguments. */ |
| 2471 |
Tcl_Obj *const objv[]) |
| 2472 |
/* Argument objects. */ |
| 2473 |
{ |
| 2474 |
int listLen, result; |
| 2475 |
|
| 2476 |
if (objc != 2) { |
| 2477 |
Tcl_WrongNumArgs(interp, 1, objv, "list"); |
| 2478 |
return TCL_ERROR; |
| 2479 |
} |
| 2480 |
|
| 2481 |
result = TclListObjLength(interp, objv[1], &listLen); |
| 2482 |
if (result != TCL_OK) { |
| 2483 |
return result; |
| 2484 |
} |
| 2485 |
|
| 2486 |
/* |
| 2487 |
* Set the interpreter's object result to an integer object holding the |
| 2488 |
* length. |
| 2489 |
*/ |
| 2490 |
|
| 2491 |
Tcl_SetObjResult(interp, Tcl_NewIntObj(listLen)); |
| 2492 |
return TCL_OK; |
| 2493 |
} |
| 2494 |
|
| 2495 |
/* |
| 2496 |
*---------------------------------------------------------------------- |
| 2497 |
* |
| 2498 |
* Tcl_LrangeObjCmd -- |
| 2499 |
* |
| 2500 |
* This procedure is invoked to process the "lrange" Tcl command. See the |
| 2501 |
* user documentation for details on what it does. |
| 2502 |
* |
| 2503 |
* Results: |
| 2504 |
* A standard Tcl object result. |
| 2505 |
* |
| 2506 |
* Side effects: |
| 2507 |
* See the user documentation. |
| 2508 |
* |
| 2509 |
*---------------------------------------------------------------------- |
| 2510 |
*/ |
| 2511 |
|
| 2512 |
int |
| 2513 |
Tcl_LrangeObjCmd( |
| 2514 |
ClientData notUsed, /* Not used. */ |
| 2515 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 2516 |
int objc, /* Number of arguments. */ |
| 2517 |
Tcl_Obj *const objv[]) |
| 2518 |
/* Argument objects. */ |
| 2519 |
{ |
| 2520 |
Tcl_Obj **elemPtrs; |
| 2521 |
int listLen, first, last, result; |
| 2522 |
|
| 2523 |
if (objc != 4) { |
| 2524 |
Tcl_WrongNumArgs(interp, 1, objv, "list first last"); |
| 2525 |
return TCL_ERROR; |
| 2526 |
} |
| 2527 |
|
| 2528 |
result = TclListObjLength(interp, objv[1], &listLen); |
| 2529 |
if (result != TCL_OK) { |
| 2530 |
return result; |
| 2531 |
} |
| 2532 |
|
| 2533 |
result = TclGetIntForIndexM(interp, objv[2], /*endValue*/ listLen - 1, |
| 2534 |
&first); |
| 2535 |
if (result != TCL_OK) { |
| 2536 |
return result; |
| 2537 |
} |
| 2538 |
if (first < 0) { |
| 2539 |
first = 0; |
| 2540 |
} |
| 2541 |
|
| 2542 |
result = TclGetIntForIndexM(interp, objv[3], /*endValue*/ listLen - 1, |
| 2543 |
&last); |
| 2544 |
if (result != TCL_OK) { |
| 2545 |
return result; |
| 2546 |
} |
| 2547 |
if (last >= listLen) { |
| 2548 |
last = listLen - 1; |
| 2549 |
} |
| 2550 |
|
| 2551 |
if (first > last) { |
| 2552 |
/* |
| 2553 |
* Returning an empty list is easy. |
| 2554 |
*/ |
| 2555 |
|
| 2556 |
return TCL_OK; |
| 2557 |
} |
| 2558 |
|
| 2559 |
result = TclListObjGetElements(interp, objv[1], &listLen, &elemPtrs); |
| 2560 |
if (result != TCL_OK) { |
| 2561 |
return result; |
| 2562 |
} |
| 2563 |
|
| 2564 |
if (Tcl_IsShared(objv[1]) || |
| 2565 |
((ListRepPtr(objv[1])->refCount > 1))) { |
| 2566 |
Tcl_SetObjResult(interp, Tcl_NewListObj(last - first + 1, |
| 2567 |
&elemPtrs[first])); |
| 2568 |
} else { |
| 2569 |
/* |
| 2570 |
* In-place is possible. |
| 2571 |
*/ |
| 2572 |
|
| 2573 |
if (last < (listLen - 1)) { |
| 2574 |
Tcl_ListObjReplace(interp, objv[1], last + 1, listLen - 1 - last, |
| 2575 |
0, NULL); |
| 2576 |
} |
| 2577 |
|
| 2578 |
/* |
| 2579 |
* This one is not conditioned on (first > 0) in order to preserve the |
| 2580 |
* string-canonizing effect of [lrange 0 end]. |
| 2581 |
*/ |
| 2582 |
|
| 2583 |
Tcl_ListObjReplace(interp, objv[1], 0, first, 0, NULL); |
| 2584 |
Tcl_SetObjResult(interp, objv[1]); |
| 2585 |
} |
| 2586 |
|
| 2587 |
return TCL_OK; |
| 2588 |
} |
| 2589 |
|
| 2590 |
/* |
| 2591 |
*---------------------------------------------------------------------- |
| 2592 |
* |
| 2593 |
* Tcl_LrepeatObjCmd -- |
| 2594 |
* |
| 2595 |
* This procedure is invoked to process the "lrepeat" Tcl command. See |
| 2596 |
* the user documentation for details on what it does. |
| 2597 |
* |
| 2598 |
* Results: |
| 2599 |
* A standard Tcl object result. |
| 2600 |
* |
| 2601 |
* Side effects: |
| 2602 |
* See the user documentation. |
| 2603 |
* |
| 2604 |
*---------------------------------------------------------------------- |
| 2605 |
*/ |
| 2606 |
|
| 2607 |
int |
| 2608 |
Tcl_LrepeatObjCmd( |
| 2609 |
ClientData dummy, /* Not used. */ |
| 2610 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 2611 |
int objc, /* Number of arguments. */ |
| 2612 |
Tcl_Obj *const objv[]) |
| 2613 |
/* The argument objects. */ |
| 2614 |
{ |
| 2615 |
int elementCount, i, totalElems; |
| 2616 |
Tcl_Obj *listPtr, **dataArray = NULL; |
| 2617 |
|
| 2618 |
/* |
| 2619 |
* Check arguments for legality: |
| 2620 |
* lrepeat count ?value ...? |
| 2621 |
*/ |
| 2622 |
|
| 2623 |
if (objc < 2) { |
| 2624 |
Tcl_WrongNumArgs(interp, 1, objv, "count ?value ...?"); |
| 2625 |
return TCL_ERROR; |
| 2626 |
} |
| 2627 |
if (TCL_OK != TclGetIntFromObj(interp, objv[1], &elementCount)) { |
| 2628 |
return TCL_ERROR; |
| 2629 |
} |
| 2630 |
if (elementCount < 0) { |
| 2631 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 2632 |
"bad count \"%d\": must be integer >= 0", elementCount)); |
| 2633 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "LREPEAT", "NEGARG", |
| 2634 |
(char *)NULL); |
| 2635 |
return TCL_ERROR; |
| 2636 |
} |
| 2637 |
|
| 2638 |
/* |
| 2639 |
* Skip forward to the interesting arguments now we've finished parsing. |
| 2640 |
*/ |
| 2641 |
|
| 2642 |
objc -= 2; |
| 2643 |
objv += 2; |
| 2644 |
|
| 2645 |
/* Final sanity check. Do not exceed limits on max list length. */ |
| 2646 |
|
| 2647 |
if (elementCount && objc > LIST_MAX/elementCount) { |
| 2648 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 2649 |
"max length of a Tcl list (%d elements) exceeded", LIST_MAX)); |
| 2650 |
Tcl_SetErrorCode(interp, "TCL", "MEMORY", (char *)NULL); |
| 2651 |
return TCL_ERROR; |
| 2652 |
} |
| 2653 |
totalElems = objc * elementCount; |
| 2654 |
|
| 2655 |
/* |
| 2656 |
* Get an empty list object that is allocated large enough to hold each |
| 2657 |
* init value elementCount times. |
| 2658 |
*/ |
| 2659 |
|
| 2660 |
listPtr = Tcl_NewListObj(totalElems, NULL); |
| 2661 |
if (totalElems) { |
| 2662 |
List *listRepPtr = ListRepPtr(listPtr); |
| 2663 |
|
| 2664 |
listRepPtr->elemCount = elementCount*objc; |
| 2665 |
dataArray = &listRepPtr->elements; |
| 2666 |
} |
| 2667 |
|
| 2668 |
/* |
| 2669 |
* Set the elements. Note that we handle the common degenerate case of a |
| 2670 |
* single value being repeated separately to permit the compiler as much |
| 2671 |
* room as possible to optimize a loop that might be run a very large |
| 2672 |
* number of times. |
| 2673 |
*/ |
| 2674 |
|
| 2675 |
CLANG_ASSERT(dataArray || totalElems == 0 ); |
| 2676 |
if (objc == 1) { |
| 2677 |
Tcl_Obj *tmpPtr = objv[0]; |
| 2678 |
|
| 2679 |
tmpPtr->refCount += elementCount; |
| 2680 |
for (i=0 ; i<elementCount ; i++) { |
| 2681 |
dataArray[i] = tmpPtr; |
| 2682 |
} |
| 2683 |
} else { |
| 2684 |
int j, k = 0; |
| 2685 |
|
| 2686 |
for (i=0 ; i<elementCount ; i++) { |
| 2687 |
for (j=0 ; j<objc ; j++) { |
| 2688 |
Tcl_IncrRefCount(objv[j]); |
| 2689 |
dataArray[k++] = objv[j]; |
| 2690 |
} |
| 2691 |
} |
| 2692 |
} |
| 2693 |
|
| 2694 |
Tcl_SetObjResult(interp, listPtr); |
| 2695 |
return TCL_OK; |
| 2696 |
} |
| 2697 |
|
| 2698 |
/* |
| 2699 |
*---------------------------------------------------------------------- |
| 2700 |
* |
| 2701 |
* Tcl_LreplaceObjCmd -- |
| 2702 |
* |
| 2703 |
* This object-based procedure is invoked to process the "lreplace" Tcl |
| 2704 |
* command. See the user documentation for details on what it does. |
| 2705 |
* |
| 2706 |
* Results: |
| 2707 |
* A new Tcl list object formed by replacing zero or more elements of a |
| 2708 |
* list. |
| 2709 |
* |
| 2710 |
* Side effects: |
| 2711 |
* See the user documentation. |
| 2712 |
* |
| 2713 |
*---------------------------------------------------------------------- |
| 2714 |
*/ |
| 2715 |
|
| 2716 |
int |
| 2717 |
Tcl_LreplaceObjCmd( |
| 2718 |
ClientData dummy, /* Not used. */ |
| 2719 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 2720 |
int objc, /* Number of arguments. */ |
| 2721 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 2722 |
{ |
| 2723 |
Tcl_Obj *listPtr; |
| 2724 |
int first, last, listLen, numToDelete, result; |
| 2725 |
|
| 2726 |
if (objc < 4) { |
| 2727 |
Tcl_WrongNumArgs(interp, 1, objv, |
| 2728 |
"list first last ?element ...?"); |
| 2729 |
return TCL_ERROR; |
| 2730 |
} |
| 2731 |
|
| 2732 |
result = TclListObjLength(interp, objv[1], &listLen); |
| 2733 |
if (result != TCL_OK) { |
| 2734 |
return result; |
| 2735 |
} |
| 2736 |
|
| 2737 |
/* |
| 2738 |
* Get the first and last indexes. "end" is interpreted to be the index |
| 2739 |
* for the last element, such that using it will cause that element to be |
| 2740 |
* included for deletion. |
| 2741 |
*/ |
| 2742 |
|
| 2743 |
result = TclGetIntForIndexM(interp, objv[2], /*end*/ listLen-1, &first); |
| 2744 |
if (result != TCL_OK) { |
| 2745 |
return result; |
| 2746 |
} |
| 2747 |
|
| 2748 |
result = TclGetIntForIndexM(interp, objv[3], /*end*/ listLen-1, &last); |
| 2749 |
if (result != TCL_OK) { |
| 2750 |
return result; |
| 2751 |
} |
| 2752 |
|
| 2753 |
if (first < 0) { |
| 2754 |
first = 0; |
| 2755 |
} |
| 2756 |
if (first > listLen) { |
| 2757 |
first = listLen; |
| 2758 |
} |
| 2759 |
|
| 2760 |
if (last >= listLen) { |
| 2761 |
last = listLen - 1; |
| 2762 |
} |
| 2763 |
if (first <= last) { |
| 2764 |
numToDelete = last - first + 1; |
| 2765 |
} else { |
| 2766 |
numToDelete = 0; |
| 2767 |
} |
| 2768 |
|
| 2769 |
/* |
| 2770 |
* If the list object is unshared we can modify it directly, otherwise we |
| 2771 |
* create a copy to modify: this is "copy on write". |
| 2772 |
*/ |
| 2773 |
|
| 2774 |
listPtr = objv[1]; |
| 2775 |
if (Tcl_IsShared(listPtr)) { |
| 2776 |
listPtr = TclListObjCopy(NULL, listPtr); |
| 2777 |
} |
| 2778 |
|
| 2779 |
/* |
| 2780 |
* Note that we call Tcl_ListObjReplace even when numToDelete == 0 and |
| 2781 |
* objc == 4. In this case, the list value of listPtr is not changed (no |
| 2782 |
* elements are removed or added), but by making the call we are assured |
| 2783 |
* we end up with a list in canonical form. Resist any temptation to |
| 2784 |
* optimize this case away. |
| 2785 |
*/ |
| 2786 |
|
| 2787 |
if (TCL_OK != Tcl_ListObjReplace(interp, listPtr, first, numToDelete, |
| 2788 |
objc-4, objv+4)) { |
| 2789 |
return TCL_ERROR; |
| 2790 |
} |
| 2791 |
|
| 2792 |
/* |
| 2793 |
* Set the interpreter's object result. |
| 2794 |
*/ |
| 2795 |
|
| 2796 |
Tcl_SetObjResult(interp, listPtr); |
| 2797 |
return TCL_OK; |
| 2798 |
} |
| 2799 |
|
| 2800 |
/* |
| 2801 |
*---------------------------------------------------------------------- |
| 2802 |
* |
| 2803 |
* Tcl_LreverseObjCmd -- |
| 2804 |
* |
| 2805 |
* This procedure is invoked to process the "lreverse" Tcl command. See |
| 2806 |
* the user documentation for details on what it does. |
| 2807 |
* |
| 2808 |
* Results: |
| 2809 |
* A standard Tcl result. |
| 2810 |
* |
| 2811 |
* Side effects: |
| 2812 |
* See the user documentation. |
| 2813 |
* |
| 2814 |
*---------------------------------------------------------------------- |
| 2815 |
*/ |
| 2816 |
|
| 2817 |
int |
| 2818 |
Tcl_LreverseObjCmd( |
| 2819 |
ClientData clientData, /* Not used. */ |
| 2820 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 2821 |
int objc, /* Number of arguments. */ |
| 2822 |
Tcl_Obj *const objv[]) /* Argument values. */ |
| 2823 |
{ |
| 2824 |
Tcl_Obj **elemv; |
| 2825 |
int elemc, i, j; |
| 2826 |
|
| 2827 |
if (objc != 2) { |
| 2828 |
Tcl_WrongNumArgs(interp, 1, objv, "list"); |
| 2829 |
return TCL_ERROR; |
| 2830 |
} |
| 2831 |
if (TclListObjGetElements(interp, objv[1], &elemc, &elemv) != TCL_OK) { |
| 2832 |
return TCL_ERROR; |
| 2833 |
} |
| 2834 |
|
| 2835 |
/* |
| 2836 |
* If the list is empty, just return it. [Bug 1876793] |
| 2837 |
*/ |
| 2838 |
|
| 2839 |
if (!elemc) { |
| 2840 |
Tcl_SetObjResult(interp, objv[1]); |
| 2841 |
return TCL_OK; |
| 2842 |
} |
| 2843 |
|
| 2844 |
if (Tcl_IsShared(objv[1]) |
| 2845 |
|| (ListRepPtr(objv[1])->refCount > 1)) { /* Bug 1675044 */ |
| 2846 |
Tcl_Obj *resultObj, **dataArray; |
| 2847 |
List *listRepPtr; |
| 2848 |
|
| 2849 |
resultObj = Tcl_NewListObj(elemc, NULL); |
| 2850 |
listRepPtr = ListRepPtr(resultObj); |
| 2851 |
listRepPtr->elemCount = elemc; |
| 2852 |
dataArray = &listRepPtr->elements; |
| 2853 |
|
| 2854 |
for (i=0,j=elemc-1 ; i<elemc ; i++,j--) { |
| 2855 |
dataArray[j] = elemv[i]; |
| 2856 |
Tcl_IncrRefCount(elemv[i]); |
| 2857 |
} |
| 2858 |
|
| 2859 |
Tcl_SetObjResult(interp, resultObj); |
| 2860 |
} else { |
| 2861 |
|
| 2862 |
/* |
| 2863 |
* Not shared, so swap "in place". This relies on Tcl_LOGE above |
| 2864 |
* returning a pointer to the live array of Tcl_Obj values. |
| 2865 |
*/ |
| 2866 |
|
| 2867 |
for (i=0,j=elemc-1 ; i<j ; i++,j--) { |
| 2868 |
Tcl_Obj *tmp = elemv[i]; |
| 2869 |
|
| 2870 |
elemv[i] = elemv[j]; |
| 2871 |
elemv[j] = tmp; |
| 2872 |
} |
| 2873 |
TclInvalidateStringRep(objv[1]); |
| 2874 |
Tcl_SetObjResult(interp, objv[1]); |
| 2875 |
} |
| 2876 |
return TCL_OK; |
| 2877 |
} |
| 2878 |
|
| 2879 |
/* |
| 2880 |
*---------------------------------------------------------------------- |
| 2881 |
* |
| 2882 |
* Tcl_LsearchObjCmd -- |
| 2883 |
* |
| 2884 |
* This procedure is invoked to process the "lsearch" Tcl command. See |
| 2885 |
* the user documentation for details on what it does. |
| 2886 |
* |
| 2887 |
* Results: |
| 2888 |
* A standard Tcl result. |
| 2889 |
* |
| 2890 |
* Side effects: |
| 2891 |
* See the user documentation. |
| 2892 |
* |
| 2893 |
*---------------------------------------------------------------------- |
| 2894 |
*/ |
| 2895 |
|
| 2896 |
int |
| 2897 |
Tcl_LsearchObjCmd( |
| 2898 |
ClientData clientData, /* Not used. */ |
| 2899 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 2900 |
int objc, /* Number of arguments. */ |
| 2901 |
Tcl_Obj *const objv[]) /* Argument values. */ |
| 2902 |
{ |
| 2903 |
const char *bytes, *patternBytes; |
| 2904 |
int i, match, index, result=TCL_OK, listc, length, elemLen, bisect; |
| 2905 |
int dataType, isIncreasing, lower, upper, offset; |
| 2906 |
Tcl_WideInt patWide, objWide; |
| 2907 |
int allMatches, inlineReturn, negatedMatch, returnSubindices, noCase; |
| 2908 |
double patDouble, objDouble; |
| 2909 |
SortInfo sortInfo; |
| 2910 |
Tcl_Obj *patObj, **listv, *listPtr, *startPtr, *itemPtr; |
| 2911 |
SortStrCmpFn_t strCmpFn = strcmp; |
| 2912 |
Tcl_RegExp regexp = NULL; |
| 2913 |
static const char *const options[] = { |
| 2914 |
"-all", "-ascii", "-bisect", "-decreasing", "-dictionary", |
| 2915 |
"-exact", "-glob", "-increasing", "-index", |
| 2916 |
"-inline", "-integer", "-nocase", "-not", |
| 2917 |
"-real", "-regexp", "-sorted", "-start", |
| 2918 |
"-subindices", NULL |
| 2919 |
}; |
| 2920 |
enum options { |
| 2921 |
LSEARCH_ALL, LSEARCH_ASCII, LSEARCH_BISECT, LSEARCH_DECREASING, |
| 2922 |
LSEARCH_DICTIONARY, LSEARCH_EXACT, LSEARCH_GLOB, LSEARCH_INCREASING, |
| 2923 |
LSEARCH_INDEX, LSEARCH_INLINE, LSEARCH_INTEGER, LSEARCH_NOCASE, |
| 2924 |
LSEARCH_NOT, LSEARCH_REAL, LSEARCH_REGEXP, LSEARCH_SORTED, |
| 2925 |
LSEARCH_START, LSEARCH_SUBINDICES |
| 2926 |
}; |
| 2927 |
enum datatypes { |
| 2928 |
ASCII, DICTIONARY, INTEGER, REAL |
| 2929 |
}; |
| 2930 |
enum modes { |
| 2931 |
EXACT, GLOB, REGEXP, SORTED |
| 2932 |
}; |
| 2933 |
enum modes mode; |
| 2934 |
|
| 2935 |
mode = GLOB; |
| 2936 |
dataType = ASCII; |
| 2937 |
isIncreasing = 1; |
| 2938 |
allMatches = 0; |
| 2939 |
inlineReturn = 0; |
| 2940 |
returnSubindices = 0; |
| 2941 |
negatedMatch = 0; |
| 2942 |
bisect = 0; |
| 2943 |
listPtr = NULL; |
| 2944 |
startPtr = NULL; |
| 2945 |
offset = 0; |
| 2946 |
noCase = 0; |
| 2947 |
sortInfo.compareCmdPtr = NULL; |
| 2948 |
sortInfo.isIncreasing = 1; |
| 2949 |
sortInfo.sortMode = 0; |
| 2950 |
sortInfo.interp = interp; |
| 2951 |
sortInfo.resultCode = TCL_OK; |
| 2952 |
sortInfo.indexv = NULL; |
| 2953 |
sortInfo.indexc = 0; |
| 2954 |
|
| 2955 |
if (objc < 3) { |
| 2956 |
Tcl_WrongNumArgs(interp, 1, objv, "?-option value ...? list pattern"); |
| 2957 |
return TCL_ERROR; |
| 2958 |
} |
| 2959 |
|
| 2960 |
for (i = 1; i < objc-2; i++) { |
| 2961 |
if (Tcl_GetIndexFromObj(interp, objv[i], options, "option", 0, &index) |
| 2962 |
!= TCL_OK) { |
| 2963 |
if (startPtr != NULL) { |
| 2964 |
Tcl_DecrRefCount(startPtr); |
| 2965 |
} |
| 2966 |
result = TCL_ERROR; |
| 2967 |
goto done; |
| 2968 |
} |
| 2969 |
switch ((enum options) index) { |
| 2970 |
case LSEARCH_ALL: /* -all */ |
| 2971 |
allMatches = 1; |
| 2972 |
break; |
| 2973 |
case LSEARCH_ASCII: /* -ascii */ |
| 2974 |
dataType = ASCII; |
| 2975 |
break; |
| 2976 |
case LSEARCH_BISECT: /* -bisect */ |
| 2977 |
mode = SORTED; |
| 2978 |
bisect = 1; |
| 2979 |
break; |
| 2980 |
case LSEARCH_DECREASING: /* -decreasing */ |
| 2981 |
isIncreasing = 0; |
| 2982 |
sortInfo.isIncreasing = 0; |
| 2983 |
break; |
| 2984 |
case LSEARCH_DICTIONARY: /* -dictionary */ |
| 2985 |
dataType = DICTIONARY; |
| 2986 |
break; |
| 2987 |
case LSEARCH_EXACT: /* -increasing */ |
| 2988 |
mode = EXACT; |
| 2989 |
break; |
| 2990 |
case LSEARCH_GLOB: /* -glob */ |
| 2991 |
mode = GLOB; |
| 2992 |
break; |
| 2993 |
case LSEARCH_INCREASING: /* -increasing */ |
| 2994 |
isIncreasing = 1; |
| 2995 |
sortInfo.isIncreasing = 1; |
| 2996 |
break; |
| 2997 |
case LSEARCH_INLINE: /* -inline */ |
| 2998 |
inlineReturn = 1; |
| 2999 |
break; |
| 3000 |
case LSEARCH_INTEGER: /* -integer */ |
| 3001 |
dataType = INTEGER; |
| 3002 |
break; |
| 3003 |
case LSEARCH_NOCASE: /* -nocase */ |
| 3004 |
strCmpFn = TclUtfCasecmp; |
| 3005 |
noCase = 1; |
| 3006 |
break; |
| 3007 |
case LSEARCH_NOT: /* -not */ |
| 3008 |
negatedMatch = 1; |
| 3009 |
break; |
| 3010 |
case LSEARCH_REAL: /* -real */ |
| 3011 |
dataType = REAL; |
| 3012 |
break; |
| 3013 |
case LSEARCH_REGEXP: /* -regexp */ |
| 3014 |
mode = REGEXP; |
| 3015 |
break; |
| 3016 |
case LSEARCH_SORTED: /* -sorted */ |
| 3017 |
mode = SORTED; |
| 3018 |
break; |
| 3019 |
case LSEARCH_SUBINDICES: /* -subindices */ |
| 3020 |
returnSubindices = 1; |
| 3021 |
break; |
| 3022 |
case LSEARCH_START: /* -start */ |
| 3023 |
/* |
| 3024 |
* If there was a previous -start option, release its saved index |
| 3025 |
* because it will either be replaced or there will be an error. |
| 3026 |
*/ |
| 3027 |
|
| 3028 |
if (startPtr != NULL) { |
| 3029 |
Tcl_DecrRefCount(startPtr); |
| 3030 |
} |
| 3031 |
if (i > objc-4) { |
| 3032 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 3033 |
"missing starting index", -1)); |
| 3034 |
Tcl_SetErrorCode(interp, "TCL", "ARGUMENT", "MISSING", (char *)NULL); |
| 3035 |
result = TCL_ERROR; |
| 3036 |
goto done; |
| 3037 |
} |
| 3038 |
i++; |
| 3039 |
if (objv[i] == objv[objc - 2]) { |
| 3040 |
/* |
| 3041 |
* Take copy to prevent shimmering problems. Note that it does |
| 3042 |
* not matter if the index obj is also a component of the list |
| 3043 |
* being searched. We only need to copy where the list and the |
| 3044 |
* index are one-and-the-same. |
| 3045 |
*/ |
| 3046 |
|
| 3047 |
startPtr = Tcl_DuplicateObj(objv[i]); |
| 3048 |
} else { |
| 3049 |
startPtr = objv[i]; |
| 3050 |
Tcl_IncrRefCount(startPtr); |
| 3051 |
} |
| 3052 |
break; |
| 3053 |
case LSEARCH_INDEX: { /* -index */ |
| 3054 |
Tcl_Obj **indices; |
| 3055 |
int j; |
| 3056 |
|
| 3057 |
if (sortInfo.indexc > 1) { |
| 3058 |
TclStackFree(interp, sortInfo.indexv); |
| 3059 |
} |
| 3060 |
if (i > objc-4) { |
| 3061 |
if (startPtr != NULL) { |
| 3062 |
Tcl_DecrRefCount(startPtr); |
| 3063 |
} |
| 3064 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 3065 |
"\"-index\" option must be followed by list index", |
| 3066 |
-1)); |
| 3067 |
Tcl_SetErrorCode(interp, "TCL", "ARGUMENT", "MISSING", (char *)NULL); |
| 3068 |
return TCL_ERROR; |
| 3069 |
} |
| 3070 |
|
| 3071 |
/* |
| 3072 |
* Store the extracted indices for processing by sublist |
| 3073 |
* extraction. Note that we don't do this using objects because |
| 3074 |
* that has shimmering problems. |
| 3075 |
*/ |
| 3076 |
|
| 3077 |
i++; |
| 3078 |
if (TclListObjGetElements(interp, objv[i], |
| 3079 |
&sortInfo.indexc, &indices) != TCL_OK) { |
| 3080 |
if (startPtr != NULL) { |
| 3081 |
Tcl_DecrRefCount(startPtr); |
| 3082 |
} |
| 3083 |
return TCL_ERROR; |
| 3084 |
} |
| 3085 |
switch (sortInfo.indexc) { |
| 3086 |
case 0: |
| 3087 |
sortInfo.indexv = NULL; |
| 3088 |
break; |
| 3089 |
case 1: |
| 3090 |
sortInfo.indexv = &sortInfo.singleIndex; |
| 3091 |
break; |
| 3092 |
default: |
| 3093 |
sortInfo.indexv = (int *) |
| 3094 |
TclStackAlloc(interp, sizeof(int) * sortInfo.indexc); |
| 3095 |
} |
| 3096 |
|
| 3097 |
/* |
| 3098 |
* Fill the array by parsing each index. We don't know whether |
| 3099 |
* their scale is sensible yet, but we at least perform the |
| 3100 |
* syntactic check here. |
| 3101 |
*/ |
| 3102 |
|
| 3103 |
for (j=0 ; j<sortInfo.indexc ; j++) { |
| 3104 |
int encoded = 0; |
| 3105 |
if (TclIndexEncode(interp, indices[j], TCL_INDEX_BEFORE, |
| 3106 |
TCL_INDEX_AFTER, &encoded) != TCL_OK) { |
| 3107 |
result = TCL_ERROR; |
| 3108 |
} |
| 3109 |
if ((encoded == TCL_INDEX_BEFORE) |
| 3110 |
|| (encoded == TCL_INDEX_AFTER)) { |
| 3111 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 3112 |
"index \"%s\" cannot select an element " |
| 3113 |
"from any list", TclGetString(indices[j]))); |
| 3114 |
Tcl_SetErrorCode(interp, "TCL", "VALUE", "INDEX" |
| 3115 |
"OUTOFRANGE", (char *)NULL); |
| 3116 |
result = TCL_ERROR; |
| 3117 |
} |
| 3118 |
if (result == TCL_ERROR) { |
| 3119 |
Tcl_AppendObjToErrorInfo(interp, Tcl_ObjPrintf( |
| 3120 |
"\n (-index option item number %d)", j)); |
| 3121 |
goto done; |
| 3122 |
} |
| 3123 |
sortInfo.indexv[j] = encoded; |
| 3124 |
} |
| 3125 |
break; |
| 3126 |
} |
| 3127 |
} |
| 3128 |
} |
| 3129 |
|
| 3130 |
/* |
| 3131 |
* Subindices only make sense if asked for with -index option set. |
| 3132 |
*/ |
| 3133 |
|
| 3134 |
if (returnSubindices && sortInfo.indexc==0) { |
| 3135 |
if (startPtr != NULL) { |
| 3136 |
Tcl_DecrRefCount(startPtr); |
| 3137 |
} |
| 3138 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 3139 |
"-subindices cannot be used without -index option", -1)); |
| 3140 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "LSEARCH", |
| 3141 |
"BAD_OPTION_MIX", (char *)NULL); |
| 3142 |
return TCL_ERROR; |
| 3143 |
} |
| 3144 |
|
| 3145 |
if (bisect && (allMatches || negatedMatch)) { |
| 3146 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 3147 |
"-bisect is not compatible with -all or -not", -1)); |
| 3148 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "LSEARCH", |
| 3149 |
"BAD_OPTION_MIX", (char *)NULL); |
| 3150 |
return TCL_ERROR; |
| 3151 |
} |
| 3152 |
|
| 3153 |
if (mode == REGEXP) { |
| 3154 |
/* |
| 3155 |
* We can shimmer regexp/list if listv[i] == pattern, so get the |
| 3156 |
* regexp rep before the list rep. First time round, omit the interp |
| 3157 |
* and hope that the compilation will succeed. If it fails, we'll |
| 3158 |
* recompile in "expensive" mode with a place to put error messages. |
| 3159 |
*/ |
| 3160 |
|
| 3161 |
regexp = Tcl_GetRegExpFromObj(NULL, objv[objc - 1], |
| 3162 |
TCL_REG_ADVANCED | TCL_REG_NOSUB | |
| 3163 |
(noCase ? TCL_REG_NOCASE : 0)); |
| 3164 |
if (regexp == NULL) { |
| 3165 |
/* |
| 3166 |
* Failed to compile the RE. Try again without the TCL_REG_NOSUB |
| 3167 |
* flag in case the RE had sub-expressions in it [Bug 1366683]. If |
| 3168 |
* this fails, an error message will be left in the interpreter. |
| 3169 |
*/ |
| 3170 |
|
| 3171 |
regexp = Tcl_GetRegExpFromObj(interp, objv[objc - 1], |
| 3172 |
TCL_REG_ADVANCED | (noCase ? TCL_REG_NOCASE : 0)); |
| 3173 |
} |
| 3174 |
|
| 3175 |
if (regexp == NULL) { |
| 3176 |
if (startPtr != NULL) { |
| 3177 |
Tcl_DecrRefCount(startPtr); |
| 3178 |
} |
| 3179 |
result = TCL_ERROR; |
| 3180 |
goto done; |
| 3181 |
} |
| 3182 |
} |
| 3183 |
|
| 3184 |
/* |
| 3185 |
* Make sure the list argument is a list object and get its length and a |
| 3186 |
* pointer to its array of element pointers. |
| 3187 |
*/ |
| 3188 |
|
| 3189 |
result = TclListObjGetElements(interp, objv[objc - 2], &listc, &listv); |
| 3190 |
if (result != TCL_OK) { |
| 3191 |
if (startPtr != NULL) { |
| 3192 |
Tcl_DecrRefCount(startPtr); |
| 3193 |
} |
| 3194 |
goto done; |
| 3195 |
} |
| 3196 |
|
| 3197 |
/* |
| 3198 |
* Get the user-specified start offset. |
| 3199 |
*/ |
| 3200 |
|
| 3201 |
if (startPtr) { |
| 3202 |
result = TclGetIntForIndexM(interp, startPtr, listc-1, &offset); |
| 3203 |
Tcl_DecrRefCount(startPtr); |
| 3204 |
if (result != TCL_OK) { |
| 3205 |
goto done; |
| 3206 |
} |
| 3207 |
if (offset < 0) { |
| 3208 |
offset = 0; |
| 3209 |
} |
| 3210 |
|
| 3211 |
/* |
| 3212 |
* If the search started past the end of the list, we just return a |
| 3213 |
* "did not match anything at all" result straight away. [Bug 1374778] |
| 3214 |
*/ |
| 3215 |
|
| 3216 |
if (offset > listc-1) { |
| 3217 |
if (sortInfo.indexc > 1) { |
| 3218 |
TclStackFree(interp, sortInfo.indexv); |
| 3219 |
} |
| 3220 |
if (allMatches || inlineReturn) { |
| 3221 |
Tcl_ResetResult(interp); |
| 3222 |
} else { |
| 3223 |
Tcl_SetObjResult(interp, Tcl_NewIntObj(-1)); |
| 3224 |
} |
| 3225 |
return TCL_OK; |
| 3226 |
} |
| 3227 |
} |
| 3228 |
|
| 3229 |
patObj = objv[objc - 1]; |
| 3230 |
patternBytes = NULL; |
| 3231 |
if (mode == EXACT || mode == SORTED) { |
| 3232 |
switch ((enum datatypes) dataType) { |
| 3233 |
case ASCII: |
| 3234 |
case DICTIONARY: |
| 3235 |
patternBytes = TclGetStringFromObj(patObj, &length); |
| 3236 |
break; |
| 3237 |
case INTEGER: |
| 3238 |
result = TclGetWideIntFromObj(interp, patObj, &patWide); |
| 3239 |
if (result != TCL_OK) { |
| 3240 |
goto done; |
| 3241 |
} |
| 3242 |
|
| 3243 |
/* |
| 3244 |
* List representation might have been shimmered; restore it. [Bug |
| 3245 |
* 1844789] |
| 3246 |
*/ |
| 3247 |
|
| 3248 |
TclListObjGetElements(NULL, objv[objc - 2], &listc, &listv); |
| 3249 |
break; |
| 3250 |
case REAL: |
| 3251 |
result = Tcl_GetDoubleFromObj(interp, patObj, &patDouble); |
| 3252 |
if (result != TCL_OK) { |
| 3253 |
goto done; |
| 3254 |
} |
| 3255 |
|
| 3256 |
/* |
| 3257 |
* List representation might have been shimmered; restore it. [Bug |
| 3258 |
* 1844789] |
| 3259 |
*/ |
| 3260 |
|
| 3261 |
TclListObjGetElements(NULL, objv[objc - 2], &listc, &listv); |
| 3262 |
break; |
| 3263 |
} |
| 3264 |
} else { |
| 3265 |
patternBytes = TclGetStringFromObj(patObj, &length); |
| 3266 |
} |
| 3267 |
|
| 3268 |
/* |
| 3269 |
* Set default index value to -1, indicating failure; if we find the item |
| 3270 |
* in the course of our search, index will be set to the correct value. |
| 3271 |
*/ |
| 3272 |
|
| 3273 |
index = -1; |
| 3274 |
match = 0; |
| 3275 |
|
| 3276 |
if (mode == SORTED && !allMatches && !negatedMatch) { |
| 3277 |
/* |
| 3278 |
* If the data is sorted, we can do a more intelligent search. Note |
| 3279 |
* that there is no point in being smart when -all was specified; in |
| 3280 |
* that case, we have to look at all items anyway, and there is no |
| 3281 |
* sense in doing this when the match sense is inverted. |
| 3282 |
*/ |
| 3283 |
|
| 3284 |
lower = offset - 1; |
| 3285 |
upper = listc; |
| 3286 |
while (lower + 1 != upper && sortInfo.resultCode == TCL_OK) { |
| 3287 |
i = (lower + upper)/2; |
| 3288 |
if (sortInfo.indexc != 0) { |
| 3289 |
itemPtr = SelectObjFromSublist(listv[i], &sortInfo); |
| 3290 |
if (sortInfo.resultCode != TCL_OK) { |
| 3291 |
result = sortInfo.resultCode; |
| 3292 |
goto done; |
| 3293 |
} |
| 3294 |
} else { |
| 3295 |
itemPtr = listv[i]; |
| 3296 |
} |
| 3297 |
switch ((enum datatypes) dataType) { |
| 3298 |
case ASCII: |
| 3299 |
bytes = TclGetString(itemPtr); |
| 3300 |
match = strCmpFn(patternBytes, bytes); |
| 3301 |
break; |
| 3302 |
case DICTIONARY: |
| 3303 |
bytes = TclGetString(itemPtr); |
| 3304 |
match = DictionaryCompare(patternBytes, bytes); |
| 3305 |
break; |
| 3306 |
case INTEGER: |
| 3307 |
result = TclGetWideIntFromObj(interp, itemPtr, &objWide); |
| 3308 |
if (result != TCL_OK) { |
| 3309 |
goto done; |
| 3310 |
} |
| 3311 |
if (patWide == objWide) { |
| 3312 |
match = 0; |
| 3313 |
} else if (patWide < objWide) { |
| 3314 |
match = -1; |
| 3315 |
} else { |
| 3316 |
match = 1; |
| 3317 |
} |
| 3318 |
break; |
| 3319 |
case REAL: |
| 3320 |
result = Tcl_GetDoubleFromObj(interp, itemPtr, &objDouble); |
| 3321 |
if (result != TCL_OK) { |
| 3322 |
goto done; |
| 3323 |
} |
| 3324 |
if (patDouble == objDouble) { |
| 3325 |
match = 0; |
| 3326 |
} else if (patDouble < objDouble) { |
| 3327 |
match = -1; |
| 3328 |
} else { |
| 3329 |
match = 1; |
| 3330 |
} |
| 3331 |
break; |
| 3332 |
} |
| 3333 |
if (match == 0) { |
| 3334 |
/* |
| 3335 |
* Normally, binary search is written to stop when it finds a |
| 3336 |
* match. If there are duplicates of an element in the list, |
| 3337 |
* our first match might not be the first occurrence. |
| 3338 |
* Consider: 0 0 0 1 1 1 2 2 2 |
| 3339 |
* |
| 3340 |
* To maintain consistency with standard lsearch semantics, we |
| 3341 |
* must find the leftmost occurrence of the pattern in the |
| 3342 |
* list. Thus we don't just stop searching here. This |
| 3343 |
* variation means that a search always makes log n |
| 3344 |
* comparisons (normal binary search might "get lucky" with an |
| 3345 |
* early comparison). |
| 3346 |
* |
| 3347 |
* In bisect mode though, we want the last of equals. |
| 3348 |
*/ |
| 3349 |
|
| 3350 |
index = i; |
| 3351 |
if (bisect) { |
| 3352 |
lower = i; |
| 3353 |
} else { |
| 3354 |
upper = i; |
| 3355 |
} |
| 3356 |
} else if (match > 0) { |
| 3357 |
if (isIncreasing) { |
| 3358 |
lower = i; |
| 3359 |
} else { |
| 3360 |
upper = i; |
| 3361 |
} |
| 3362 |
} else { |
| 3363 |
if (isIncreasing) { |
| 3364 |
upper = i; |
| 3365 |
} else { |
| 3366 |
lower = i; |
| 3367 |
} |
| 3368 |
} |
| 3369 |
} |
| 3370 |
if (bisect && index < 0) { |
| 3371 |
index = lower; |
| 3372 |
} |
| 3373 |
} else { |
| 3374 |
/* |
| 3375 |
* We need to do a linear search, because (at least one) of: |
| 3376 |
* - our matcher can only tell equal vs. not equal |
| 3377 |
* - our matching sense is negated |
| 3378 |
* - we're building a list of all matched items |
| 3379 |
*/ |
| 3380 |
|
| 3381 |
if (allMatches) { |
| 3382 |
listPtr = Tcl_NewListObj(0, NULL); |
| 3383 |
} |
| 3384 |
for (i = offset; i < listc; i++) { |
| 3385 |
match = 0; |
| 3386 |
if (sortInfo.indexc != 0) { |
| 3387 |
itemPtr = SelectObjFromSublist(listv[i], &sortInfo); |
| 3388 |
if (sortInfo.resultCode != TCL_OK) { |
| 3389 |
if (listPtr != NULL) { |
| 3390 |
Tcl_DecrRefCount(listPtr); |
| 3391 |
} |
| 3392 |
result = sortInfo.resultCode; |
| 3393 |
goto done; |
| 3394 |
} |
| 3395 |
} else { |
| 3396 |
itemPtr = listv[i]; |
| 3397 |
} |
| 3398 |
|
| 3399 |
switch (mode) { |
| 3400 |
case SORTED: |
| 3401 |
case EXACT: |
| 3402 |
switch ((enum datatypes) dataType) { |
| 3403 |
case ASCII: |
| 3404 |
bytes = TclGetStringFromObj(itemPtr, &elemLen); |
| 3405 |
if (length == elemLen) { |
| 3406 |
/* |
| 3407 |
* This split allows for more optimal compilation of |
| 3408 |
* memcmp/strcasecmp. |
| 3409 |
*/ |
| 3410 |
|
| 3411 |
if (noCase) { |
| 3412 |
match = (TclUtfCasecmp(bytes, patternBytes) == 0); |
| 3413 |
} else { |
| 3414 |
match = (memcmp(bytes, patternBytes, |
| 3415 |
(size_t) length) == 0); |
| 3416 |
} |
| 3417 |
} |
| 3418 |
break; |
| 3419 |
|
| 3420 |
case DICTIONARY: |
| 3421 |
bytes = TclGetString(itemPtr); |
| 3422 |
match = (DictionaryCompare(bytes, patternBytes) == 0); |
| 3423 |
break; |
| 3424 |
|
| 3425 |
case INTEGER: |
| 3426 |
result = TclGetWideIntFromObj(interp, itemPtr, &objWide); |
| 3427 |
if (result != TCL_OK) { |
| 3428 |
if (listPtr != NULL) { |
| 3429 |
Tcl_DecrRefCount(listPtr); |
| 3430 |
} |
| 3431 |
goto done; |
| 3432 |
} |
| 3433 |
match = (objWide == patWide); |
| 3434 |
break; |
| 3435 |
|
| 3436 |
case REAL: |
| 3437 |
result = Tcl_GetDoubleFromObj(interp,itemPtr, &objDouble); |
| 3438 |
if (result != TCL_OK) { |
| 3439 |
if (listPtr) { |
| 3440 |
Tcl_DecrRefCount(listPtr); |
| 3441 |
} |
| 3442 |
goto done; |
| 3443 |
} |
| 3444 |
match = (objDouble == patDouble); |
| 3445 |
break; |
| 3446 |
} |
| 3447 |
break; |
| 3448 |
|
| 3449 |
case GLOB: |
| 3450 |
match = Tcl_StringCaseMatch(TclGetString(itemPtr), |
| 3451 |
patternBytes, noCase); |
| 3452 |
break; |
| 3453 |
|
| 3454 |
case REGEXP: |
| 3455 |
match = Tcl_RegExpExecObj(interp, regexp, itemPtr, 0, 0, 0); |
| 3456 |
if (match < 0) { |
| 3457 |
Tcl_DecrRefCount(patObj); |
| 3458 |
if (listPtr != NULL) { |
| 3459 |
Tcl_DecrRefCount(listPtr); |
| 3460 |
} |
| 3461 |
result = TCL_ERROR; |
| 3462 |
goto done; |
| 3463 |
} |
| 3464 |
break; |
| 3465 |
} |
| 3466 |
|
| 3467 |
/* |
| 3468 |
* Invert match condition for -not. |
| 3469 |
*/ |
| 3470 |
|
| 3471 |
if (negatedMatch) { |
| 3472 |
match = !match; |
| 3473 |
} |
| 3474 |
if (!match) { |
| 3475 |
continue; |
| 3476 |
} |
| 3477 |
if (!allMatches) { |
| 3478 |
index = i; |
| 3479 |
break; |
| 3480 |
} else if (inlineReturn) { |
| 3481 |
/* |
| 3482 |
* Note that these appends are not expected to fail. |
| 3483 |
*/ |
| 3484 |
|
| 3485 |
if (returnSubindices && (sortInfo.indexc != 0)) { |
| 3486 |
itemPtr = SelectObjFromSublist(listv[i], &sortInfo); |
| 3487 |
} else { |
| 3488 |
itemPtr = listv[i]; |
| 3489 |
} |
| 3490 |
Tcl_ListObjAppendElement(interp, listPtr, itemPtr); |
| 3491 |
} else if (returnSubindices) { |
| 3492 |
int j; |
| 3493 |
|
| 3494 |
TclNewIntObj(itemPtr, i); |
| 3495 |
for (j=0 ; j<sortInfo.indexc ; j++) { |
| 3496 |
Tcl_ListObjAppendElement(interp, itemPtr, Tcl_NewIntObj( |
| 3497 |
TclIndexDecode(sortInfo.indexv[j], listc))); |
| 3498 |
} |
| 3499 |
Tcl_ListObjAppendElement(interp, listPtr, itemPtr); |
| 3500 |
} else { |
| 3501 |
Tcl_ListObjAppendElement(interp, listPtr, Tcl_NewIntObj(i)); |
| 3502 |
} |
| 3503 |
} |
| 3504 |
} |
| 3505 |
|
| 3506 |
/* |
| 3507 |
* Return everything or a single value. |
| 3508 |
*/ |
| 3509 |
|
| 3510 |
if (allMatches) { |
| 3511 |
Tcl_SetObjResult(interp, listPtr); |
| 3512 |
} else if (!inlineReturn) { |
| 3513 |
if (returnSubindices) { |
| 3514 |
int j; |
| 3515 |
|
| 3516 |
TclNewIntObj(itemPtr, index); |
| 3517 |
for (j=0 ; j<sortInfo.indexc ; j++) { |
| 3518 |
Tcl_ListObjAppendElement(interp, itemPtr, Tcl_NewIntObj( |
| 3519 |
TclIndexDecode(sortInfo.indexv[j], listc))); |
| 3520 |
} |
| 3521 |
Tcl_SetObjResult(interp, itemPtr); |
| 3522 |
} else { |
| 3523 |
Tcl_SetObjResult(interp, Tcl_NewIntObj(index)); |
| 3524 |
} |
| 3525 |
} else if (index < 0) { |
| 3526 |
/* |
| 3527 |
* Is this superfluous? The result should be a blank object by |
| 3528 |
* default... |
| 3529 |
*/ |
| 3530 |
|
| 3531 |
Tcl_SetObjResult(interp, Tcl_NewObj()); |
| 3532 |
} else { |
| 3533 |
Tcl_SetObjResult(interp, listv[index]); |
| 3534 |
} |
| 3535 |
result = TCL_OK; |
| 3536 |
|
| 3537 |
/* |
| 3538 |
* Cleanup the index list array. |
| 3539 |
*/ |
| 3540 |
|
| 3541 |
done: |
| 3542 |
if (sortInfo.indexc > 1) { |
| 3543 |
TclStackFree(interp, sortInfo.indexv); |
| 3544 |
} |
| 3545 |
return result; |
| 3546 |
} |
| 3547 |
|
| 3548 |
/* |
| 3549 |
*---------------------------------------------------------------------- |
| 3550 |
* |
| 3551 |
* Tcl_LsetObjCmd -- |
| 3552 |
* |
| 3553 |
* This procedure is invoked to process the "lset" Tcl command. See the |
| 3554 |
* user documentation for details on what it does. |
| 3555 |
* |
| 3556 |
* Results: |
| 3557 |
* A standard Tcl result. |
| 3558 |
* |
| 3559 |
* Side effects: |
| 3560 |
* See the user documentation. |
| 3561 |
* |
| 3562 |
*---------------------------------------------------------------------- |
| 3563 |
*/ |
| 3564 |
|
| 3565 |
int |
| 3566 |
Tcl_LsetObjCmd( |
| 3567 |
ClientData clientData, /* Not used. */ |
| 3568 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 3569 |
int objc, /* Number of arguments. */ |
| 3570 |
Tcl_Obj *const objv[]) /* Argument values. */ |
| 3571 |
{ |
| 3572 |
Tcl_Obj *listPtr; /* Pointer to the list being altered. */ |
| 3573 |
Tcl_Obj *finalValuePtr; /* Value finally assigned to the variable. */ |
| 3574 |
|
| 3575 |
/* |
| 3576 |
* Check parameter count. |
| 3577 |
*/ |
| 3578 |
|
| 3579 |
if (objc < 3) { |
| 3580 |
Tcl_WrongNumArgs(interp, 1, objv, |
| 3581 |
"listVar ?index? ?index ...? value"); |
| 3582 |
return TCL_ERROR; |
| 3583 |
} |
| 3584 |
|
| 3585 |
/* |
| 3586 |
* Look up the list variable's value. |
| 3587 |
*/ |
| 3588 |
|
| 3589 |
listPtr = Tcl_ObjGetVar2(interp, objv[1], NULL, TCL_LEAVE_ERR_MSG); |
| 3590 |
if (listPtr == NULL) { |
| 3591 |
return TCL_ERROR; |
| 3592 |
} |
| 3593 |
|
| 3594 |
/* |
| 3595 |
* Substitute the value in the value. Return either the value or else an |
| 3596 |
* unshared copy of it. |
| 3597 |
*/ |
| 3598 |
|
| 3599 |
if (objc == 4) { |
| 3600 |
finalValuePtr = TclLsetList(interp, listPtr, objv[2], objv[3]); |
| 3601 |
} else { |
| 3602 |
finalValuePtr = TclLsetFlat(interp, listPtr, objc-3, objv+2, |
| 3603 |
objv[objc-1]); |
| 3604 |
} |
| 3605 |
|
| 3606 |
/* |
| 3607 |
* If substitution has failed, bail out. |
| 3608 |
*/ |
| 3609 |
|
| 3610 |
if (finalValuePtr == NULL) { |
| 3611 |
return TCL_ERROR; |
| 3612 |
} |
| 3613 |
|
| 3614 |
/* |
| 3615 |
* Finally, update the variable so that traces fire. |
| 3616 |
*/ |
| 3617 |
|
| 3618 |
listPtr = Tcl_ObjSetVar2(interp, objv[1], NULL, finalValuePtr, |
| 3619 |
TCL_LEAVE_ERR_MSG); |
| 3620 |
Tcl_DecrRefCount(finalValuePtr); |
| 3621 |
if (listPtr == NULL) { |
| 3622 |
return TCL_ERROR; |
| 3623 |
} |
| 3624 |
|
| 3625 |
/* |
| 3626 |
* Return the new value of the variable as the interpreter result. |
| 3627 |
*/ |
| 3628 |
|
| 3629 |
Tcl_SetObjResult(interp, listPtr); |
| 3630 |
return TCL_OK; |
| 3631 |
} |
| 3632 |
|
| 3633 |
/* |
| 3634 |
*---------------------------------------------------------------------- |
| 3635 |
* |
| 3636 |
* Tcl_LsortObjCmd -- |
| 3637 |
* |
| 3638 |
* This procedure is invoked to process the "lsort" Tcl command. See the |
| 3639 |
* user documentation for details on what it does. |
| 3640 |
* |
| 3641 |
* Results: |
| 3642 |
* A standard Tcl result. |
| 3643 |
* |
| 3644 |
* Side effects: |
| 3645 |
* See the user documentation. |
| 3646 |
* |
| 3647 |
*---------------------------------------------------------------------- |
| 3648 |
*/ |
| 3649 |
|
| 3650 |
int |
| 3651 |
Tcl_LsortObjCmd( |
| 3652 |
ClientData clientData, /* Not used. */ |
| 3653 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 3654 |
int objc, /* Number of arguments. */ |
| 3655 |
Tcl_Obj *const objv[]) /* Argument values. */ |
| 3656 |
{ |
| 3657 |
int i, j, index, indices, length, nocase = 0, indexc; |
| 3658 |
int sortMode = SORTMODE_ASCII; |
| 3659 |
int group, groupSize, groupOffset, idx, allocatedIndexVector = 0; |
| 3660 |
Tcl_Obj *resultPtr, *cmdPtr, **listObjPtrs, *listObj, *indexPtr; |
| 3661 |
size_t elmArrSize; |
| 3662 |
SortElement *elementArray = NULL, *elementPtr; |
| 3663 |
SortInfo sortInfo; /* Information about this sort that needs to |
| 3664 |
* be passed to the comparison function. */ |
| 3665 |
# define MAXCALLOC 1024000 |
| 3666 |
# define NUM_LISTS 30 |
| 3667 |
SortElement *subList[NUM_LISTS+1]; |
| 3668 |
/* This array holds pointers to temporary |
| 3669 |
* lists built during the merge sort. Element |
| 3670 |
* i of the array holds a list of length |
| 3671 |
* 2**i. */ |
| 3672 |
static const char *const switches[] = { |
| 3673 |
"-ascii", "-command", "-decreasing", "-dictionary", "-increasing", |
| 3674 |
"-index", "-indices", "-integer", "-nocase", "-real", "-stride", |
| 3675 |
"-unique", NULL |
| 3676 |
}; |
| 3677 |
enum Lsort_Switches { |
| 3678 |
LSORT_ASCII, LSORT_COMMAND, LSORT_DECREASING, LSORT_DICTIONARY, |
| 3679 |
LSORT_INCREASING, LSORT_INDEX, LSORT_INDICES, LSORT_INTEGER, |
| 3680 |
LSORT_NOCASE, LSORT_REAL, LSORT_STRIDE, LSORT_UNIQUE |
| 3681 |
}; |
| 3682 |
|
| 3683 |
if (objc < 2) { |
| 3684 |
Tcl_WrongNumArgs(interp, 1, objv, "?-option value ...? list"); |
| 3685 |
return TCL_ERROR; |
| 3686 |
} |
| 3687 |
|
| 3688 |
/* |
| 3689 |
* Parse arguments to set up the mode for the sort. |
| 3690 |
*/ |
| 3691 |
|
| 3692 |
sortInfo.isIncreasing = 1; |
| 3693 |
sortInfo.sortMode = SORTMODE_ASCII; |
| 3694 |
sortInfo.indexv = NULL; |
| 3695 |
sortInfo.indexc = 0; |
| 3696 |
sortInfo.unique = 0; |
| 3697 |
sortInfo.interp = interp; |
| 3698 |
sortInfo.resultCode = TCL_OK; |
| 3699 |
cmdPtr = NULL; |
| 3700 |
indices = 0; |
| 3701 |
group = 0; |
| 3702 |
groupSize = 1; |
| 3703 |
groupOffset = 0; |
| 3704 |
indexPtr = NULL; |
| 3705 |
for (i = 1; i < objc-1; i++) { |
| 3706 |
if (Tcl_GetIndexFromObj(interp, objv[i], switches, "option", 0, |
| 3707 |
&index) != TCL_OK) { |
| 3708 |
sortInfo.resultCode = TCL_ERROR; |
| 3709 |
goto done; |
| 3710 |
} |
| 3711 |
switch ((enum Lsort_Switches) index) { |
| 3712 |
case LSORT_ASCII: |
| 3713 |
sortInfo.sortMode = SORTMODE_ASCII; |
| 3714 |
break; |
| 3715 |
case LSORT_COMMAND: |
| 3716 |
if (i == objc-2) { |
| 3717 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 3718 |
"\"-command\" option must be followed " |
| 3719 |
"by comparison command", -1)); |
| 3720 |
Tcl_SetErrorCode(interp, "TCL", "ARGUMENT", "MISSING", (char *)NULL); |
| 3721 |
sortInfo.resultCode = TCL_ERROR; |
| 3722 |
goto done; |
| 3723 |
} |
| 3724 |
sortInfo.sortMode = SORTMODE_COMMAND; |
| 3725 |
cmdPtr = objv[i+1]; |
| 3726 |
i++; |
| 3727 |
break; |
| 3728 |
case LSORT_DECREASING: |
| 3729 |
sortInfo.isIncreasing = 0; |
| 3730 |
break; |
| 3731 |
case LSORT_DICTIONARY: |
| 3732 |
sortInfo.sortMode = SORTMODE_DICTIONARY; |
| 3733 |
break; |
| 3734 |
case LSORT_INCREASING: |
| 3735 |
sortInfo.isIncreasing = 1; |
| 3736 |
break; |
| 3737 |
case LSORT_INDEX: { |
| 3738 |
int sortindex; |
| 3739 |
Tcl_Obj **indexv; |
| 3740 |
|
| 3741 |
if (i == objc-2) { |
| 3742 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 3743 |
"\"-index\" option must be followed by list index", |
| 3744 |
-1)); |
| 3745 |
Tcl_SetErrorCode(interp, "TCL", "ARGUMENT", "MISSING", (char *)NULL); |
| 3746 |
sortInfo.resultCode = TCL_ERROR; |
| 3747 |
goto done; |
| 3748 |
} |
| 3749 |
if (TclListObjGetElements(interp, objv[i+1], &sortindex, |
| 3750 |
&indexv) != TCL_OK) { |
| 3751 |
sortInfo.resultCode = TCL_ERROR; |
| 3752 |
goto done; |
| 3753 |
} |
| 3754 |
|
| 3755 |
/* |
| 3756 |
* Check each of the indices for syntactic correctness. Note that |
| 3757 |
* we do not store the converted values here because we do not |
| 3758 |
* know if this is the only -index option yet and so we can't |
| 3759 |
* allocate any space; that happens after the scan through all the |
| 3760 |
* options is done. |
| 3761 |
*/ |
| 3762 |
|
| 3763 |
for (j=0 ; j<sortindex ; j++) { |
| 3764 |
int encoded = 0; |
| 3765 |
int result = TclIndexEncode(interp, indexv[j], |
| 3766 |
TCL_INDEX_BEFORE, TCL_INDEX_AFTER, &encoded); |
| 3767 |
|
| 3768 |
if ((result == TCL_OK) && ((encoded == TCL_INDEX_BEFORE) |
| 3769 |
|| (encoded == TCL_INDEX_AFTER))) { |
| 3770 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 3771 |
"index \"%s\" cannot select an element " |
| 3772 |
"from any list", TclGetString(indexv[j]))); |
| 3773 |
Tcl_SetErrorCode(interp, "TCL", "VALUE", "INDEX" |
| 3774 |
"OUTOFRANGE", (char *)NULL); |
| 3775 |
result = TCL_ERROR; |
| 3776 |
} |
| 3777 |
if (result == TCL_ERROR) { |
| 3778 |
Tcl_AppendObjToErrorInfo(interp, Tcl_ObjPrintf( |
| 3779 |
"\n (-index option item number %d)", j)); |
| 3780 |
sortInfo.resultCode = TCL_ERROR; |
| 3781 |
goto done; |
| 3782 |
} |
| 3783 |
} |
| 3784 |
indexPtr = objv[i+1]; |
| 3785 |
i++; |
| 3786 |
break; |
| 3787 |
} |
| 3788 |
case LSORT_INTEGER: |
| 3789 |
sortInfo.sortMode = SORTMODE_INTEGER; |
| 3790 |
break; |
| 3791 |
case LSORT_NOCASE: |
| 3792 |
nocase = 1; |
| 3793 |
break; |
| 3794 |
case LSORT_REAL: |
| 3795 |
sortInfo.sortMode = SORTMODE_REAL; |
| 3796 |
break; |
| 3797 |
case LSORT_UNIQUE: |
| 3798 |
sortInfo.unique = 1; |
| 3799 |
break; |
| 3800 |
case LSORT_INDICES: |
| 3801 |
indices = 1; |
| 3802 |
break; |
| 3803 |
case LSORT_STRIDE: |
| 3804 |
if (i == objc-2) { |
| 3805 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 3806 |
"\"-stride\" option must be " |
| 3807 |
"followed by stride length", -1)); |
| 3808 |
Tcl_SetErrorCode(interp, "TCL", "ARGUMENT", "MISSING", (char *)NULL); |
| 3809 |
sortInfo.resultCode = TCL_ERROR; |
| 3810 |
goto done; |
| 3811 |
} |
| 3812 |
if (Tcl_GetIntFromObj(interp, objv[i+1], &groupSize) != TCL_OK) { |
| 3813 |
sortInfo.resultCode = TCL_ERROR; |
| 3814 |
goto done; |
| 3815 |
} |
| 3816 |
if (groupSize < 2) { |
| 3817 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 3818 |
"stride length must be at least 2", -1)); |
| 3819 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "LSORT", |
| 3820 |
"BADSTRIDE", (char *)NULL); |
| 3821 |
sortInfo.resultCode = TCL_ERROR; |
| 3822 |
goto done; |
| 3823 |
} |
| 3824 |
group = 1; |
| 3825 |
i++; |
| 3826 |
break; |
| 3827 |
} |
| 3828 |
} |
| 3829 |
if (nocase && (sortInfo.sortMode == SORTMODE_ASCII)) { |
| 3830 |
sortInfo.sortMode = SORTMODE_ASCII_NC; |
| 3831 |
} |
| 3832 |
|
| 3833 |
/* |
| 3834 |
* Now extract the -index list for real, if present. No failures are |
| 3835 |
* expected here; the values are all of the right type or convertible to |
| 3836 |
* it. |
| 3837 |
*/ |
| 3838 |
|
| 3839 |
if (indexPtr) { |
| 3840 |
Tcl_Obj **indexv; |
| 3841 |
|
| 3842 |
TclListObjGetElements(interp, indexPtr, &sortInfo.indexc, &indexv); |
| 3843 |
switch (sortInfo.indexc) { |
| 3844 |
case 0: |
| 3845 |
sortInfo.indexv = NULL; |
| 3846 |
break; |
| 3847 |
case 1: |
| 3848 |
sortInfo.indexv = &sortInfo.singleIndex; |
| 3849 |
break; |
| 3850 |
default: |
| 3851 |
sortInfo.indexv = (int *) |
| 3852 |
TclStackAlloc(interp, sizeof(int) * sortInfo.indexc); |
| 3853 |
allocatedIndexVector = 1; /* Cannot use indexc field, as it |
| 3854 |
* might be decreased by 1 later. */ |
| 3855 |
} |
| 3856 |
for (j=0 ; j<sortInfo.indexc ; j++) { |
| 3857 |
/* Prescreened values, no errors or out of range possible */ |
| 3858 |
TclIndexEncode(NULL, indexv[j], 0, 0, &sortInfo.indexv[j]); |
| 3859 |
} |
| 3860 |
} |
| 3861 |
|
| 3862 |
listObj = objv[objc-1]; |
| 3863 |
|
| 3864 |
if (sortInfo.sortMode == SORTMODE_COMMAND) { |
| 3865 |
Tcl_Obj *newCommandPtr, *newObjPtr; |
| 3866 |
|
| 3867 |
/* |
| 3868 |
* When sorting using a command, we are reentrant and therefore might |
| 3869 |
* have the representation of the list being sorted shimmered out from |
| 3870 |
* underneath our feet. Take a copy (cheap) to prevent this. [Bug |
| 3871 |
* 1675116] |
| 3872 |
*/ |
| 3873 |
|
| 3874 |
listObj = TclListObjCopy(interp, listObj); |
| 3875 |
if (listObj == NULL) { |
| 3876 |
sortInfo.resultCode = TCL_ERROR; |
| 3877 |
goto done; |
| 3878 |
} |
| 3879 |
|
| 3880 |
/* |
| 3881 |
* The existing command is a list. We want to flatten it, append two |
| 3882 |
* dummy arguments on the end, and replace these arguments later. |
| 3883 |
*/ |
| 3884 |
|
| 3885 |
newCommandPtr = Tcl_DuplicateObj(cmdPtr); |
| 3886 |
TclNewObj(newObjPtr); |
| 3887 |
Tcl_IncrRefCount(newCommandPtr); |
| 3888 |
if (Tcl_ListObjAppendElement(interp, newCommandPtr, newObjPtr) |
| 3889 |
!= TCL_OK) { |
| 3890 |
TclDecrRefCount(newCommandPtr); |
| 3891 |
TclDecrRefCount(listObj); |
| 3892 |
Tcl_IncrRefCount(newObjPtr); |
| 3893 |
TclDecrRefCount(newObjPtr); |
| 3894 |
sortInfo.resultCode = TCL_ERROR; |
| 3895 |
goto done; |
| 3896 |
} |
| 3897 |
Tcl_ListObjAppendElement(interp, newCommandPtr, Tcl_NewObj()); |
| 3898 |
sortInfo.compareCmdPtr = newCommandPtr; |
| 3899 |
} |
| 3900 |
|
| 3901 |
sortInfo.resultCode = TclListObjGetElements(interp, listObj, |
| 3902 |
&length, &listObjPtrs); |
| 3903 |
if (sortInfo.resultCode != TCL_OK || length <= 0) { |
| 3904 |
goto done; |
| 3905 |
} |
| 3906 |
|
| 3907 |
/* |
| 3908 |
* Check for sanity when grouping elements of the overall list together |
| 3909 |
* because of the -stride option. [TIP #326] |
| 3910 |
*/ |
| 3911 |
|
| 3912 |
if (group) { |
| 3913 |
if (length % groupSize) { |
| 3914 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 3915 |
"list size must be a multiple of the stride length", |
| 3916 |
-1)); |
| 3917 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "LSORT", "BADSTRIDE", |
| 3918 |
(char *)NULL); |
| 3919 |
sortInfo.resultCode = TCL_ERROR; |
| 3920 |
goto done; |
| 3921 |
} |
| 3922 |
length = length / groupSize; |
| 3923 |
if (sortInfo.indexc > 0) { |
| 3924 |
/* |
| 3925 |
* Use the first value in the list supplied to -index as the |
| 3926 |
* offset of the element within each group by which to sort. |
| 3927 |
*/ |
| 3928 |
|
| 3929 |
groupOffset = TclIndexDecode(sortInfo.indexv[0], groupSize - 1); |
| 3930 |
if (groupOffset < 0 || groupOffset >= groupSize) { |
| 3931 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 3932 |
"when used with \"-stride\", the leading \"-index\"" |
| 3933 |
" value must be within the group", -1)); |
| 3934 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "LSORT", |
| 3935 |
"BADINDEX", (char *)NULL); |
| 3936 |
sortInfo.resultCode = TCL_ERROR; |
| 3937 |
goto done; |
| 3938 |
} |
| 3939 |
if (sortInfo.indexc == 1) { |
| 3940 |
sortInfo.indexc = 0; |
| 3941 |
sortInfo.indexv = NULL; |
| 3942 |
} else { |
| 3943 |
sortInfo.indexc--; |
| 3944 |
|
| 3945 |
/* |
| 3946 |
* Do not shrink the actual memory block used; that doesn't |
| 3947 |
* work with TclStackAlloc-allocated memory. [Bug 2918962] |
| 3948 |
* |
| 3949 |
* TODO: Consider a pointer increment to replace this |
| 3950 |
* array shift. |
| 3951 |
*/ |
| 3952 |
|
| 3953 |
for (i = 0; i < sortInfo.indexc; i++) { |
| 3954 |
sortInfo.indexv[i] = sortInfo.indexv[i+1]; |
| 3955 |
} |
| 3956 |
} |
| 3957 |
} |
| 3958 |
} |
| 3959 |
|
| 3960 |
sortInfo.numElements = length; |
| 3961 |
|
| 3962 |
indexc = sortInfo.indexc; |
| 3963 |
sortMode = sortInfo.sortMode; |
| 3964 |
if ((sortMode == SORTMODE_ASCII_NC) |
| 3965 |
|| (sortMode == SORTMODE_DICTIONARY)) { |
| 3966 |
/* |
| 3967 |
* For this function's purpose all string-based modes are equivalent |
| 3968 |
*/ |
| 3969 |
|
| 3970 |
sortMode = SORTMODE_ASCII; |
| 3971 |
} |
| 3972 |
|
| 3973 |
/* |
| 3974 |
* Initialize the sublists. After the following loop, subList[i] will |
| 3975 |
* contain a sorted sublist of length 2**i. Use one extra subList at the |
| 3976 |
* end, always at NULL, to indicate the end of the lists. |
| 3977 |
*/ |
| 3978 |
|
| 3979 |
for (j=0 ; j<=NUM_LISTS ; j++) { |
| 3980 |
subList[j] = NULL; |
| 3981 |
} |
| 3982 |
|
| 3983 |
/* |
| 3984 |
* The following loop creates a SortElement for each list element and |
| 3985 |
* begins sorting it into the sublists as it appears. |
| 3986 |
*/ |
| 3987 |
|
| 3988 |
elmArrSize = length * sizeof(SortElement); |
| 3989 |
if (elmArrSize <= MAXCALLOC) { |
| 3990 |
elementArray = (SortElement *)ckalloc(elmArrSize); |
| 3991 |
} else { |
| 3992 |
elementArray = (SortElement *)malloc(elmArrSize); |
| 3993 |
} |
| 3994 |
if (!elementArray) { |
| 3995 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 3996 |
"no enough memory to proccess sort of %d items", length)); |
| 3997 |
Tcl_SetErrorCode(interp, "TCL", "MEMORY", (char *)NULL); |
| 3998 |
sortInfo.resultCode = TCL_ERROR; |
| 3999 |
goto done; |
| 4000 |
} |
| 4001 |
|
| 4002 |
for (i=0; i < length; i++){ |
| 4003 |
idx = groupSize * i + groupOffset; |
| 4004 |
if (indexc) { |
| 4005 |
/* |
| 4006 |
* If this is an indexed sort, retrieve the corresponding element |
| 4007 |
*/ |
| 4008 |
indexPtr = SelectObjFromSublist(listObjPtrs[idx], &sortInfo); |
| 4009 |
if (sortInfo.resultCode != TCL_OK) { |
| 4010 |
goto done; |
| 4011 |
} |
| 4012 |
} else { |
| 4013 |
indexPtr = listObjPtrs[idx]; |
| 4014 |
} |
| 4015 |
|
| 4016 |
/* |
| 4017 |
* Determine the "value" of this object for sorting purposes |
| 4018 |
*/ |
| 4019 |
|
| 4020 |
if (sortMode == SORTMODE_ASCII) { |
| 4021 |
elementArray[i].collationKey.strValuePtr = TclGetString(indexPtr); |
| 4022 |
} else if (sortMode == SORTMODE_INTEGER) { |
| 4023 |
Tcl_WideInt a; |
| 4024 |
|
| 4025 |
if (TclGetWideIntFromObj(sortInfo.interp, indexPtr, &a) != TCL_OK) { |
| 4026 |
sortInfo.resultCode = TCL_ERROR; |
| 4027 |
goto done; |
| 4028 |
} |
| 4029 |
elementArray[i].collationKey.wideValue = a; |
| 4030 |
} else if (sortMode == SORTMODE_REAL) { |
| 4031 |
double a; |
| 4032 |
|
| 4033 |
if (Tcl_GetDoubleFromObj(sortInfo.interp, indexPtr, |
| 4034 |
&a) != TCL_OK) { |
| 4035 |
sortInfo.resultCode = TCL_ERROR; |
| 4036 |
goto done; |
| 4037 |
} |
| 4038 |
elementArray[i].collationKey.doubleValue = a; |
| 4039 |
} else { |
| 4040 |
elementArray[i].collationKey.objValuePtr = indexPtr; |
| 4041 |
} |
| 4042 |
|
| 4043 |
/* |
| 4044 |
* Determine the representation of this element in the result: either |
| 4045 |
* the objPtr itself, or its index in the original list. |
| 4046 |
*/ |
| 4047 |
|
| 4048 |
if (indices || group) { |
| 4049 |
elementArray[i].payload.index = idx; |
| 4050 |
} else { |
| 4051 |
elementArray[i].payload.objPtr = listObjPtrs[idx]; |
| 4052 |
} |
| 4053 |
|
| 4054 |
/* |
| 4055 |
* Merge this element in the preexisting sublists (and merge together |
| 4056 |
* sublists when we have two of the same size). |
| 4057 |
*/ |
| 4058 |
|
| 4059 |
elementArray[i].nextPtr = NULL; |
| 4060 |
elementPtr = &elementArray[i]; |
| 4061 |
for (j=0 ; subList[j] ; j++) { |
| 4062 |
elementPtr = MergeLists(subList[j], elementPtr, &sortInfo); |
| 4063 |
subList[j] = NULL; |
| 4064 |
} |
| 4065 |
if (j >= NUM_LISTS) { |
| 4066 |
j = NUM_LISTS-1; |
| 4067 |
} |
| 4068 |
subList[j] = elementPtr; |
| 4069 |
} |
| 4070 |
|
| 4071 |
/* |
| 4072 |
* Merge all sublists |
| 4073 |
*/ |
| 4074 |
|
| 4075 |
elementPtr = subList[0]; |
| 4076 |
for (j=1 ; j<NUM_LISTS ; j++) { |
| 4077 |
elementPtr = MergeLists(subList[j], elementPtr, &sortInfo); |
| 4078 |
} |
| 4079 |
|
| 4080 |
/* |
| 4081 |
* Now store the sorted elements in the result list. |
| 4082 |
*/ |
| 4083 |
|
| 4084 |
if (sortInfo.resultCode == TCL_OK) { |
| 4085 |
List *listRepPtr; |
| 4086 |
Tcl_Obj **newArray, *objPtr; |
| 4087 |
|
| 4088 |
resultPtr = Tcl_NewListObj(sortInfo.numElements * groupSize, NULL); |
| 4089 |
listRepPtr = ListRepPtr(resultPtr); |
| 4090 |
newArray = &listRepPtr->elements; |
| 4091 |
if (group) { |
| 4092 |
for (i=0; elementPtr!=NULL ; elementPtr=elementPtr->nextPtr) { |
| 4093 |
idx = elementPtr->payload.index; |
| 4094 |
for (j = 0; j < groupSize; j++) { |
| 4095 |
if (indices) { |
| 4096 |
TclNewIntObj(objPtr, idx + j - groupOffset); |
| 4097 |
newArray[i++] = objPtr; |
| 4098 |
Tcl_IncrRefCount(objPtr); |
| 4099 |
} else { |
| 4100 |
objPtr = listObjPtrs[idx + j - groupOffset]; |
| 4101 |
newArray[i++] = objPtr; |
| 4102 |
Tcl_IncrRefCount(objPtr); |
| 4103 |
} |
| 4104 |
} |
| 4105 |
} |
| 4106 |
} else if (indices) { |
| 4107 |
for (i=0; elementPtr != NULL ; elementPtr = elementPtr->nextPtr) { |
| 4108 |
TclNewIntObj(objPtr, elementPtr->payload.index); |
| 4109 |
newArray[i++] = objPtr; |
| 4110 |
Tcl_IncrRefCount(objPtr); |
| 4111 |
} |
| 4112 |
} else { |
| 4113 |
for (i=0; elementPtr != NULL ; elementPtr = elementPtr->nextPtr) { |
| 4114 |
objPtr = elementPtr->payload.objPtr; |
| 4115 |
newArray[i++] = objPtr; |
| 4116 |
Tcl_IncrRefCount(objPtr); |
| 4117 |
} |
| 4118 |
} |
| 4119 |
listRepPtr->elemCount = i; |
| 4120 |
Tcl_SetObjResult(interp, resultPtr); |
| 4121 |
} |
| 4122 |
|
| 4123 |
done: |
| 4124 |
if (sortMode == SORTMODE_COMMAND) { |
| 4125 |
TclDecrRefCount(sortInfo.compareCmdPtr); |
| 4126 |
TclDecrRefCount(listObj); |
| 4127 |
sortInfo.compareCmdPtr = NULL; |
| 4128 |
} |
| 4129 |
if (allocatedIndexVector) { |
| 4130 |
TclStackFree(interp, sortInfo.indexv); |
| 4131 |
} |
| 4132 |
if (elementArray) { |
| 4133 |
if (elmArrSize <= MAXCALLOC) { |
| 4134 |
ckfree((char *)elementArray); |
| 4135 |
} else { |
| 4136 |
free((char *)elementArray); |
| 4137 |
} |
| 4138 |
} |
| 4139 |
return sortInfo.resultCode; |
| 4140 |
} |
| 4141 |
|
| 4142 |
/* |
| 4143 |
*---------------------------------------------------------------------- |
| 4144 |
* |
| 4145 |
* MergeLists - |
| 4146 |
* |
| 4147 |
* This procedure combines two sorted lists of SortElement structures |
| 4148 |
* into a single sorted list. |
| 4149 |
* |
| 4150 |
* Results: |
| 4151 |
* The unified list of SortElement structures. |
| 4152 |
* |
| 4153 |
* Side effects: |
| 4154 |
* If infoPtr->unique is set then infoPtr->numElements may be updated. |
| 4155 |
* Possibly others, if a user-defined comparison command does something |
| 4156 |
* weird. |
| 4157 |
* |
| 4158 |
* Note: |
| 4159 |
* If infoPtr->unique is set, the merge assumes that there are no |
| 4160 |
* "repeated" elements in each of the left and right lists. In that case, |
| 4161 |
* if any element of the left list is equivalent to one in the right list |
| 4162 |
* it is omitted from the merged list. |
| 4163 |
* |
| 4164 |
* This simplified mechanism works because of the special way our |
| 4165 |
* MergeSort creates the sublists to be merged and will fail to eliminate |
| 4166 |
* all repeats in the general case where they are already present in |
| 4167 |
* either the left or right list. A general code would need to skip |
| 4168 |
* adjacent initial repeats in the left and right lists before comparing |
| 4169 |
* their initial elements, at each step. |
| 4170 |
* |
| 4171 |
*---------------------------------------------------------------------- |
| 4172 |
*/ |
| 4173 |
|
| 4174 |
static SortElement * |
| 4175 |
MergeLists( |
| 4176 |
SortElement *leftPtr, /* First list to be merged; may be NULL. */ |
| 4177 |
SortElement *rightPtr, /* Second list to be merged; may be NULL. */ |
| 4178 |
SortInfo *infoPtr) /* Information needed by the comparison |
| 4179 |
* operator. */ |
| 4180 |
{ |
| 4181 |
SortElement *headPtr, *tailPtr; |
| 4182 |
int cmp; |
| 4183 |
|
| 4184 |
if (leftPtr == NULL) { |
| 4185 |
return rightPtr; |
| 4186 |
} |
| 4187 |
if (rightPtr == NULL) { |
| 4188 |
return leftPtr; |
| 4189 |
} |
| 4190 |
cmp = SortCompare(leftPtr, rightPtr, infoPtr); |
| 4191 |
if (cmp > 0 || (cmp == 0 && infoPtr->unique)) { |
| 4192 |
if (cmp == 0) { |
| 4193 |
infoPtr->numElements--; |
| 4194 |
leftPtr = leftPtr->nextPtr; |
| 4195 |
} |
| 4196 |
tailPtr = rightPtr; |
| 4197 |
rightPtr = rightPtr->nextPtr; |
| 4198 |
} else { |
| 4199 |
tailPtr = leftPtr; |
| 4200 |
leftPtr = leftPtr->nextPtr; |
| 4201 |
} |
| 4202 |
headPtr = tailPtr; |
| 4203 |
if (!infoPtr->unique) { |
| 4204 |
while ((leftPtr != NULL) && (rightPtr != NULL)) { |
| 4205 |
cmp = SortCompare(leftPtr, rightPtr, infoPtr); |
| 4206 |
if (cmp > 0) { |
| 4207 |
tailPtr->nextPtr = rightPtr; |
| 4208 |
tailPtr = rightPtr; |
| 4209 |
rightPtr = rightPtr->nextPtr; |
| 4210 |
} else { |
| 4211 |
tailPtr->nextPtr = leftPtr; |
| 4212 |
tailPtr = leftPtr; |
| 4213 |
leftPtr = leftPtr->nextPtr; |
| 4214 |
} |
| 4215 |
} |
| 4216 |
} else { |
| 4217 |
while ((leftPtr != NULL) && (rightPtr != NULL)) { |
| 4218 |
cmp = SortCompare(leftPtr, rightPtr, infoPtr); |
| 4219 |
if (cmp >= 0) { |
| 4220 |
if (cmp == 0) { |
| 4221 |
infoPtr->numElements--; |
| 4222 |
leftPtr = leftPtr->nextPtr; |
| 4223 |
} |
| 4224 |
tailPtr->nextPtr = rightPtr; |
| 4225 |
tailPtr = rightPtr; |
| 4226 |
rightPtr = rightPtr->nextPtr; |
| 4227 |
} else { |
| 4228 |
tailPtr->nextPtr = leftPtr; |
| 4229 |
tailPtr = leftPtr; |
| 4230 |
leftPtr = leftPtr->nextPtr; |
| 4231 |
} |
| 4232 |
} |
| 4233 |
} |
| 4234 |
if (leftPtr != NULL) { |
| 4235 |
tailPtr->nextPtr = leftPtr; |
| 4236 |
} else { |
| 4237 |
tailPtr->nextPtr = rightPtr; |
| 4238 |
} |
| 4239 |
return headPtr; |
| 4240 |
} |
| 4241 |
|
| 4242 |
/* |
| 4243 |
*---------------------------------------------------------------------- |
| 4244 |
* |
| 4245 |
* SortCompare -- |
| 4246 |
* |
| 4247 |
* This procedure is invoked by MergeLists to determine the proper |
| 4248 |
* ordering between two elements. |
| 4249 |
* |
| 4250 |
* Results: |
| 4251 |
* A negative results means the first element comes before the |
| 4252 |
* second, and a positive results means that the second element should |
| 4253 |
* come first. A result of zero means the two elements are equal and it |
| 4254 |
* doesn't matter which comes first. |
| 4255 |
* |
| 4256 |
* Side effects: |
| 4257 |
* None, unless a user-defined comparison command does something weird. |
| 4258 |
* |
| 4259 |
*---------------------------------------------------------------------- |
| 4260 |
*/ |
| 4261 |
|
| 4262 |
static int |
| 4263 |
SortCompare( |
| 4264 |
SortElement *elemPtr1, SortElement *elemPtr2, |
| 4265 |
/* Values to be compared. */ |
| 4266 |
SortInfo *infoPtr) /* Information passed from the top-level |
| 4267 |
* "lsort" command. */ |
| 4268 |
{ |
| 4269 |
int order = 0; |
| 4270 |
|
| 4271 |
if (infoPtr->sortMode == SORTMODE_ASCII) { |
| 4272 |
order = strcmp(elemPtr1->collationKey.strValuePtr, |
| 4273 |
elemPtr2->collationKey.strValuePtr); |
| 4274 |
} else if (infoPtr->sortMode == SORTMODE_ASCII_NC) { |
| 4275 |
order = TclUtfCasecmp(elemPtr1->collationKey.strValuePtr, |
| 4276 |
elemPtr2->collationKey.strValuePtr); |
| 4277 |
} else if (infoPtr->sortMode == SORTMODE_DICTIONARY) { |
| 4278 |
order = DictionaryCompare(elemPtr1->collationKey.strValuePtr, |
| 4279 |
elemPtr2->collationKey.strValuePtr); |
| 4280 |
} else if (infoPtr->sortMode == SORTMODE_INTEGER) { |
| 4281 |
Tcl_WideInt a, b; |
| 4282 |
|
| 4283 |
a = elemPtr1->collationKey.wideValue; |
| 4284 |
b = elemPtr2->collationKey.wideValue; |
| 4285 |
order = ((a >= b) - (a <= b)); |
| 4286 |
} else if (infoPtr->sortMode == SORTMODE_REAL) { |
| 4287 |
double a, b; |
| 4288 |
|
| 4289 |
a = elemPtr1->collationKey.doubleValue; |
| 4290 |
b = elemPtr2->collationKey.doubleValue; |
| 4291 |
order = ((a >= b) - (a <= b)); |
| 4292 |
} else { |
| 4293 |
Tcl_Obj **objv, *paramObjv[2]; |
| 4294 |
int objc; |
| 4295 |
Tcl_Obj *objPtr1, *objPtr2; |
| 4296 |
|
| 4297 |
if (infoPtr->resultCode != TCL_OK) { |
| 4298 |
/* |
| 4299 |
* Once an error has occurred, skip any future comparisons so as |
| 4300 |
* to preserve the error message in sortInterp->result. |
| 4301 |
*/ |
| 4302 |
|
| 4303 |
return 0; |
| 4304 |
} |
| 4305 |
|
| 4306 |
|
| 4307 |
objPtr1 = elemPtr1->collationKey.objValuePtr; |
| 4308 |
objPtr2 = elemPtr2->collationKey.objValuePtr; |
| 4309 |
|
| 4310 |
paramObjv[0] = objPtr1; |
| 4311 |
paramObjv[1] = objPtr2; |
| 4312 |
|
| 4313 |
/* |
| 4314 |
* We made space in the command list for the two things to compare. |
| 4315 |
* Replace them and evaluate the result. |
| 4316 |
*/ |
| 4317 |
|
| 4318 |
TclListObjLength(infoPtr->interp, infoPtr->compareCmdPtr, &objc); |
| 4319 |
Tcl_ListObjReplace(infoPtr->interp, infoPtr->compareCmdPtr, objc - 2, |
| 4320 |
2, 2, paramObjv); |
| 4321 |
TclListObjGetElements(infoPtr->interp, infoPtr->compareCmdPtr, |
| 4322 |
&objc, &objv); |
| 4323 |
|
| 4324 |
infoPtr->resultCode = Tcl_EvalObjv(infoPtr->interp, objc, objv, 0); |
| 4325 |
|
| 4326 |
if (infoPtr->resultCode != TCL_OK) { |
| 4327 |
Tcl_AddErrorInfo(infoPtr->interp, "\n (-compare command)"); |
| 4328 |
return 0; |
| 4329 |
} |
| 4330 |
|
| 4331 |
/* |
| 4332 |
* Parse the result of the command. |
| 4333 |
*/ |
| 4334 |
|
| 4335 |
if (TclGetIntFromObj(infoPtr->interp, |
| 4336 |
Tcl_GetObjResult(infoPtr->interp), &order) != TCL_OK) { |
| 4337 |
Tcl_SetObjResult(infoPtr->interp, Tcl_NewStringObj( |
| 4338 |
"-compare command returned non-integer result", -1)); |
| 4339 |
Tcl_SetErrorCode(infoPtr->interp, "TCL", "OPERATION", "LSORT", |
| 4340 |
"COMPARISONFAILED", (char *)NULL); |
| 4341 |
infoPtr->resultCode = TCL_ERROR; |
| 4342 |
return 0; |
| 4343 |
} |
| 4344 |
} |
| 4345 |
if (!infoPtr->isIncreasing) { |
| 4346 |
order = -order; |
| 4347 |
} |
| 4348 |
return order; |
| 4349 |
} |
| 4350 |
|
| 4351 |
/* |
| 4352 |
*---------------------------------------------------------------------- |
| 4353 |
* |
| 4354 |
* DictionaryCompare |
| 4355 |
* |
| 4356 |
* This function compares two strings as if they were being used in an |
| 4357 |
* index or card catalog. The case of alphabetic characters is ignored, |
| 4358 |
* except to break ties. Thus "B" comes before "b" but after "a". Also, |
| 4359 |
* integers embedded in the strings compare in numerical order. In other |
| 4360 |
* words, "x10y" comes after "x9y", not * before it as it would when |
| 4361 |
* using strcmp(). |
| 4362 |
* |
| 4363 |
* Results: |
| 4364 |
* A negative result means that the first element comes before the |
| 4365 |
* second, and a positive result means that the second element should |
| 4366 |
* come first. A result of zero means the two elements are equal and it |
| 4367 |
* doesn't matter which comes first. |
| 4368 |
* |
| 4369 |
* Side effects: |
| 4370 |
* None. |
| 4371 |
* |
| 4372 |
*---------------------------------------------------------------------- |
| 4373 |
*/ |
| 4374 |
|
| 4375 |
static int |
| 4376 |
DictionaryCompare( |
| 4377 |
const char *left, const char *right) /* The strings to compare. */ |
| 4378 |
{ |
| 4379 |
int uniLeft = 0, uniRight = 0, uniLeftLower, uniRightLower; |
| 4380 |
int diff, zeros; |
| 4381 |
int secondaryDiff = 0; |
| 4382 |
|
| 4383 |
while (1) { |
| 4384 |
if (isdigit(UCHAR(*right)) /* INTL: digit */ |
| 4385 |
&& isdigit(UCHAR(*left))) { /* INTL: digit */ |
| 4386 |
/* |
| 4387 |
* There are decimal numbers embedded in the two strings. Compare |
| 4388 |
* them as numbers, rather than strings. If one number has more |
| 4389 |
* leading zeros than the other, the number with more leading |
| 4390 |
* zeros sorts later, but only as a secondary choice. |
| 4391 |
*/ |
| 4392 |
|
| 4393 |
zeros = 0; |
| 4394 |
while ((*right == '0') && isdigit(UCHAR(right[1]))) { |
| 4395 |
right++; |
| 4396 |
zeros--; |
| 4397 |
} |
| 4398 |
while ((*left == '0') && isdigit(UCHAR(left[1]))) { |
| 4399 |
left++; |
| 4400 |
zeros++; |
| 4401 |
} |
| 4402 |
if (secondaryDiff == 0) { |
| 4403 |
secondaryDiff = zeros; |
| 4404 |
} |
| 4405 |
|
| 4406 |
/* |
| 4407 |
* The code below compares the numbers in the two strings without |
| 4408 |
* ever converting them to integers. It does this by first |
| 4409 |
* comparing the lengths of the numbers and then comparing the |
| 4410 |
* digit values. |
| 4411 |
*/ |
| 4412 |
|
| 4413 |
diff = 0; |
| 4414 |
while (1) { |
| 4415 |
if (diff == 0) { |
| 4416 |
diff = UCHAR(*left) - UCHAR(*right); |
| 4417 |
} |
| 4418 |
right++; |
| 4419 |
left++; |
| 4420 |
if (!isdigit(UCHAR(*right))) { /* INTL: digit */ |
| 4421 |
if (isdigit(UCHAR(*left))) { /* INTL: digit */ |
| 4422 |
return 1; |
| 4423 |
} else { |
| 4424 |
/* |
| 4425 |
* The two numbers have the same length. See if their |
| 4426 |
* values are different. |
| 4427 |
*/ |
| 4428 |
|
| 4429 |
if (diff != 0) { |
| 4430 |
return diff; |
| 4431 |
} |
| 4432 |
break; |
| 4433 |
} |
| 4434 |
} else if (!isdigit(UCHAR(*left))) { /* INTL: digit */ |
| 4435 |
return -1; |
| 4436 |
} |
| 4437 |
} |
| 4438 |
continue; |
| 4439 |
} |
| 4440 |
|
| 4441 |
/* |
| 4442 |
* Convert character to Unicode for comparison purposes. If either |
| 4443 |
* string is at the terminating null, do a byte-wise comparison and |
| 4444 |
* bail out immediately. |
| 4445 |
*/ |
| 4446 |
|
| 4447 |
if ((*left != '\0') && (*right != '\0')) { |
| 4448 |
left += TclUtfToUCS4(left, &uniLeft); |
| 4449 |
right += TclUtfToUCS4(right, &uniRight); |
| 4450 |
|
| 4451 |
/* |
| 4452 |
* Convert both chars to lower for the comparison, because |
| 4453 |
* dictionary sorts are case-insensitive. Covert to lower, not |
| 4454 |
* upper, so chars between Z and a will sort before A (where most |
| 4455 |
* other interesting punctuations occur). |
| 4456 |
*/ |
| 4457 |
|
| 4458 |
uniLeftLower = TclUCS4ToLower(uniLeft); |
| 4459 |
uniRightLower = TclUCS4ToLower(uniRight); |
| 4460 |
} else { |
| 4461 |
diff = UCHAR(*left) - UCHAR(*right); |
| 4462 |
break; |
| 4463 |
} |
| 4464 |
|
| 4465 |
diff = uniLeftLower - uniRightLower; |
| 4466 |
if (diff) { |
| 4467 |
return diff; |
| 4468 |
} |
| 4469 |
if (secondaryDiff == 0) { |
| 4470 |
if (Tcl_UniCharIsUpper(uniLeft) && Tcl_UniCharIsLower(uniRight)) { |
| 4471 |
secondaryDiff = -1; |
| 4472 |
} else if (Tcl_UniCharIsUpper(uniRight) |
| 4473 |
&& Tcl_UniCharIsLower(uniLeft)) { |
| 4474 |
secondaryDiff = 1; |
| 4475 |
} |
| 4476 |
} |
| 4477 |
} |
| 4478 |
if (diff == 0) { |
| 4479 |
diff = secondaryDiff; |
| 4480 |
} |
| 4481 |
return diff; |
| 4482 |
} |
| 4483 |
|
| 4484 |
/* |
| 4485 |
*---------------------------------------------------------------------- |
| 4486 |
* |
| 4487 |
* SelectObjFromSublist -- |
| 4488 |
* |
| 4489 |
* This procedure is invoked from lsearch and SortCompare. It is used for |
| 4490 |
* implementing the -index option, for the lsort and lsearch commands. |
| 4491 |
* |
| 4492 |
* Results: |
| 4493 |
* Returns NULL if a failure occurs, and sets the result in the infoPtr. |
| 4494 |
* Otherwise returns the Tcl_Obj* to the item. |
| 4495 |
* |
| 4496 |
* Side effects: |
| 4497 |
* None. |
| 4498 |
* |
| 4499 |
* Note: |
| 4500 |
* No reference counting is done, as the result is only used internally |
| 4501 |
* and never passed directly to user code. |
| 4502 |
* |
| 4503 |
*---------------------------------------------------------------------- |
| 4504 |
*/ |
| 4505 |
|
| 4506 |
static Tcl_Obj * |
| 4507 |
SelectObjFromSublist( |
| 4508 |
Tcl_Obj *objPtr, /* Obj to select sublist from. */ |
| 4509 |
SortInfo *infoPtr) /* Information passed from the top-level |
| 4510 |
* "lsearch" or "lsort" command. */ |
| 4511 |
{ |
| 4512 |
int i; |
| 4513 |
|
| 4514 |
/* |
| 4515 |
* Quick check for case when no "-index" option is there. |
| 4516 |
*/ |
| 4517 |
|
| 4518 |
if (infoPtr->indexc == 0) { |
| 4519 |
return objPtr; |
| 4520 |
} |
| 4521 |
|
| 4522 |
/* |
| 4523 |
* Iterate over the indices, traversing through the nested sublists as we |
| 4524 |
* go. |
| 4525 |
*/ |
| 4526 |
|
| 4527 |
for (i=0 ; i<infoPtr->indexc ; i++) { |
| 4528 |
int listLen, index; |
| 4529 |
Tcl_Obj *currentObj; |
| 4530 |
|
| 4531 |
if (TclListObjLength(infoPtr->interp, objPtr, &listLen) != TCL_OK) { |
| 4532 |
infoPtr->resultCode = TCL_ERROR; |
| 4533 |
return NULL; |
| 4534 |
} |
| 4535 |
|
| 4536 |
index = TclIndexDecode(infoPtr->indexv[i], listLen - 1); |
| 4537 |
|
| 4538 |
if (Tcl_ListObjIndex(infoPtr->interp, objPtr, index, |
| 4539 |
¤tObj) != TCL_OK) { |
| 4540 |
infoPtr->resultCode = TCL_ERROR; |
| 4541 |
return NULL; |
| 4542 |
} |
| 4543 |
if (currentObj == NULL) { |
| 4544 |
Tcl_SetObjResult(infoPtr->interp, Tcl_ObjPrintf( |
| 4545 |
"element %d missing from sublist \"%s\"", |
| 4546 |
index, TclGetString(objPtr))); |
| 4547 |
Tcl_SetErrorCode(infoPtr->interp, "TCL", "OPERATION", "LSORT", |
| 4548 |
"INDEXFAILED", (char *)NULL); |
| 4549 |
infoPtr->resultCode = TCL_ERROR; |
| 4550 |
return NULL; |
| 4551 |
} |
| 4552 |
objPtr = currentObj; |
| 4553 |
} |
| 4554 |
return objPtr; |
| 4555 |
} |
| 4556 |
|
| 4557 |
/* |
| 4558 |
* Local Variables: |
| 4559 |
* mode: c |
| 4560 |
* c-basic-offset: 4 |
| 4561 |
* fill-column: 78 |
| 4562 |
* tab-width: 8 |
| 4563 |
* End: |
| 4564 |
*/ |