| 1 |
/* |
| 2 |
* tclResult.c -- |
| 3 |
* |
| 4 |
* This file contains code to manage the interpreter result. |
| 5 |
* |
| 6 |
* Copyright (c) 1997 Sun Microsystems, Inc. |
| 7 |
* |
| 8 |
* See the file "license.terms" for information on usage and redistribution of |
| 9 |
* this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 10 |
*/ |
| 11 |
|
| 12 |
#include "tclInt.h" |
| 13 |
|
| 14 |
/* |
| 15 |
* Indices of the standard return options dictionary keys. |
| 16 |
*/ |
| 17 |
|
| 18 |
enum returnKeys { |
| 19 |
KEY_CODE, KEY_ERRORCODE, KEY_ERRORINFO, KEY_ERRORLINE, |
| 20 |
KEY_LEVEL, KEY_OPTIONS, KEY_ERRORSTACK, KEY_LAST |
| 21 |
}; |
| 22 |
|
| 23 |
/* |
| 24 |
* Function prototypes for local functions in this file: |
| 25 |
*/ |
| 26 |
|
| 27 |
static Tcl_Obj ** GetKeys(void); |
| 28 |
static void ReleaseKeys(ClientData clientData); |
| 29 |
static void ResetObjResult(Interp *iPtr); |
| 30 |
static void SetupAppendBuffer(Interp *iPtr, int newSpace); |
| 31 |
|
| 32 |
/* |
| 33 |
* This structure is used to take a snapshot of the interpreter state in |
| 34 |
* Tcl_SaveInterpState. You can snapshot the state, execute a command, and |
| 35 |
* then back up to the result or the error that was previously in progress. |
| 36 |
*/ |
| 37 |
|
| 38 |
typedef struct InterpState { |
| 39 |
int status; /* return code status */ |
| 40 |
int flags; /* Each remaining field saves the */ |
| 41 |
int returnLevel; /* corresponding field of the Interp */ |
| 42 |
int returnCode; /* struct. These fields taken together are */ |
| 43 |
Tcl_Obj *errorInfo; /* the "state" of the interp. */ |
| 44 |
Tcl_Obj *errorCode; |
| 45 |
Tcl_Obj *returnOpts; |
| 46 |
Tcl_Obj *objResult; |
| 47 |
Tcl_Obj *errorStack; |
| 48 |
int resetErrorStack; |
| 49 |
} InterpState; |
| 50 |
|
| 51 |
/* |
| 52 |
*---------------------------------------------------------------------- |
| 53 |
* |
| 54 |
* Tcl_SaveInterpState -- |
| 55 |
* |
| 56 |
* Fills a token with a snapshot of the current state of the interpreter. |
| 57 |
* The snapshot can be restored at any point by Tcl_RestoreInterpState. |
| 58 |
* |
| 59 |
* The token returned must be eventually passed to one of the routines |
| 60 |
* Tcl_RestoreInterpState or Tcl_DiscardInterpState, or there will be a |
| 61 |
* memory leak. |
| 62 |
* |
| 63 |
* Results: |
| 64 |
* Returns a token representing the interp state. |
| 65 |
* |
| 66 |
* Side effects: |
| 67 |
* None. |
| 68 |
* |
| 69 |
*---------------------------------------------------------------------- |
| 70 |
*/ |
| 71 |
|
| 72 |
Tcl_InterpState |
| 73 |
Tcl_SaveInterpState( |
| 74 |
Tcl_Interp *interp, /* Interpreter's state to be saved */ |
| 75 |
int status) /* status code for current operation */ |
| 76 |
{ |
| 77 |
Interp *iPtr = (Interp *) interp; |
| 78 |
InterpState *statePtr = (InterpState *)ckalloc(sizeof(InterpState)); |
| 79 |
|
| 80 |
statePtr->status = status; |
| 81 |
statePtr->flags = iPtr->flags & ERR_ALREADY_LOGGED; |
| 82 |
statePtr->returnLevel = iPtr->returnLevel; |
| 83 |
statePtr->returnCode = iPtr->returnCode; |
| 84 |
statePtr->errorInfo = iPtr->errorInfo; |
| 85 |
statePtr->errorStack = iPtr->errorStack; |
| 86 |
statePtr->resetErrorStack = iPtr->resetErrorStack; |
| 87 |
if (statePtr->errorInfo) { |
| 88 |
Tcl_IncrRefCount(statePtr->errorInfo); |
| 89 |
} |
| 90 |
statePtr->errorCode = iPtr->errorCode; |
| 91 |
if (statePtr->errorCode) { |
| 92 |
Tcl_IncrRefCount(statePtr->errorCode); |
| 93 |
} |
| 94 |
statePtr->returnOpts = iPtr->returnOpts; |
| 95 |
if (statePtr->returnOpts) { |
| 96 |
Tcl_IncrRefCount(statePtr->returnOpts); |
| 97 |
} |
| 98 |
if (statePtr->errorStack) { |
| 99 |
Tcl_IncrRefCount(statePtr->errorStack); |
| 100 |
} |
| 101 |
statePtr->objResult = Tcl_GetObjResult(interp); |
| 102 |
Tcl_IncrRefCount(statePtr->objResult); |
| 103 |
return (Tcl_InterpState) statePtr; |
| 104 |
} |
| 105 |
|
| 106 |
/* |
| 107 |
*---------------------------------------------------------------------- |
| 108 |
* |
| 109 |
* Tcl_RestoreInterpState -- |
| 110 |
* |
| 111 |
* Accepts an interp and a token previously returned by |
| 112 |
* Tcl_SaveInterpState. Restore the state of the interp to what it was at |
| 113 |
* the time of the Tcl_SaveInterpState call. |
| 114 |
* |
| 115 |
* Results: |
| 116 |
* Returns the status value originally passed in to Tcl_SaveInterpState. |
| 117 |
* |
| 118 |
* Side effects: |
| 119 |
* Restores the interp state and frees memory held by token. |
| 120 |
* |
| 121 |
*---------------------------------------------------------------------- |
| 122 |
*/ |
| 123 |
|
| 124 |
int |
| 125 |
Tcl_RestoreInterpState( |
| 126 |
Tcl_Interp *interp, /* Interpreter's state to be restored. */ |
| 127 |
Tcl_InterpState state) /* Saved interpreter state. */ |
| 128 |
{ |
| 129 |
Interp *iPtr = (Interp *) interp; |
| 130 |
InterpState *statePtr = (InterpState *) state; |
| 131 |
int status = statePtr->status; |
| 132 |
|
| 133 |
iPtr->flags &= ~ERR_ALREADY_LOGGED; |
| 134 |
iPtr->flags |= (statePtr->flags & ERR_ALREADY_LOGGED); |
| 135 |
|
| 136 |
iPtr->returnLevel = statePtr->returnLevel; |
| 137 |
iPtr->returnCode = statePtr->returnCode; |
| 138 |
iPtr->resetErrorStack = statePtr->resetErrorStack; |
| 139 |
if (iPtr->errorInfo) { |
| 140 |
Tcl_DecrRefCount(iPtr->errorInfo); |
| 141 |
} |
| 142 |
iPtr->errorInfo = statePtr->errorInfo; |
| 143 |
if (iPtr->errorInfo) { |
| 144 |
Tcl_IncrRefCount(iPtr->errorInfo); |
| 145 |
} |
| 146 |
if (iPtr->errorCode) { |
| 147 |
Tcl_DecrRefCount(iPtr->errorCode); |
| 148 |
} |
| 149 |
iPtr->errorCode = statePtr->errorCode; |
| 150 |
if (iPtr->errorCode) { |
| 151 |
Tcl_IncrRefCount(iPtr->errorCode); |
| 152 |
} |
| 153 |
if (iPtr->errorStack) { |
| 154 |
Tcl_DecrRefCount(iPtr->errorStack); |
| 155 |
} |
| 156 |
iPtr->errorStack = statePtr->errorStack; |
| 157 |
if (iPtr->errorStack) { |
| 158 |
Tcl_IncrRefCount(iPtr->errorStack); |
| 159 |
} |
| 160 |
if (iPtr->returnOpts) { |
| 161 |
Tcl_DecrRefCount(iPtr->returnOpts); |
| 162 |
} |
| 163 |
iPtr->returnOpts = statePtr->returnOpts; |
| 164 |
if (iPtr->returnOpts) { |
| 165 |
Tcl_IncrRefCount(iPtr->returnOpts); |
| 166 |
} |
| 167 |
Tcl_SetObjResult(interp, statePtr->objResult); |
| 168 |
Tcl_DiscardInterpState(state); |
| 169 |
return status; |
| 170 |
} |
| 171 |
|
| 172 |
/* |
| 173 |
*---------------------------------------------------------------------- |
| 174 |
* |
| 175 |
* Tcl_DiscardInterpState -- |
| 176 |
* |
| 177 |
* Accepts a token previously returned by Tcl_SaveInterpState. Frees the |
| 178 |
* memory it uses. |
| 179 |
* |
| 180 |
* Results: |
| 181 |
* None. |
| 182 |
* |
| 183 |
* Side effects: |
| 184 |
* Frees memory. |
| 185 |
* |
| 186 |
*---------------------------------------------------------------------- |
| 187 |
*/ |
| 188 |
|
| 189 |
void |
| 190 |
Tcl_DiscardInterpState( |
| 191 |
Tcl_InterpState state) /* saved interpreter state */ |
| 192 |
{ |
| 193 |
InterpState *statePtr = (InterpState *) state; |
| 194 |
|
| 195 |
if (statePtr->errorInfo) { |
| 196 |
Tcl_DecrRefCount(statePtr->errorInfo); |
| 197 |
} |
| 198 |
if (statePtr->errorCode) { |
| 199 |
Tcl_DecrRefCount(statePtr->errorCode); |
| 200 |
} |
| 201 |
if (statePtr->returnOpts) { |
| 202 |
Tcl_DecrRefCount(statePtr->returnOpts); |
| 203 |
} |
| 204 |
if (statePtr->errorStack) { |
| 205 |
Tcl_DecrRefCount(statePtr->errorStack); |
| 206 |
} |
| 207 |
Tcl_DecrRefCount(statePtr->objResult); |
| 208 |
ckfree(statePtr); |
| 209 |
} |
| 210 |
|
| 211 |
/* |
| 212 |
*---------------------------------------------------------------------- |
| 213 |
* |
| 214 |
* Tcl_SaveResult -- |
| 215 |
* |
| 216 |
* Takes a snapshot of the current result state of the interpreter. The |
| 217 |
* snapshot can be restored at any point by Tcl_RestoreResult. Note that |
| 218 |
* this routine does not preserve the errorCode, errorInfo, or flags |
| 219 |
* fields so it should not be used if an error is in progress. |
| 220 |
* |
| 221 |
* Once a snapshot is saved, it must be restored by calling |
| 222 |
* Tcl_RestoreResult, or discarded by calling Tcl_DiscardResult. |
| 223 |
* |
| 224 |
* Results: |
| 225 |
* None. |
| 226 |
* |
| 227 |
* Side effects: |
| 228 |
* Resets the interpreter result. |
| 229 |
* |
| 230 |
*---------------------------------------------------------------------- |
| 231 |
*/ |
| 232 |
|
| 233 |
#undef Tcl_SaveResult |
| 234 |
void |
| 235 |
Tcl_SaveResult( |
| 236 |
Tcl_Interp *interp, /* Interpreter to save. */ |
| 237 |
Tcl_SavedResult *statePtr) /* Pointer to state structure. */ |
| 238 |
{ |
| 239 |
Interp *iPtr = (Interp *) interp; |
| 240 |
|
| 241 |
/* |
| 242 |
* Move the result object into the save state. Note that we don't need to |
| 243 |
* change its refcount because we're moving it, not adding a new |
| 244 |
* reference. Put an empty object into the interpreter. |
| 245 |
*/ |
| 246 |
|
| 247 |
statePtr->objResultPtr = iPtr->objResultPtr; |
| 248 |
TclNewObj(iPtr->objResultPtr); |
| 249 |
Tcl_IncrRefCount(iPtr->objResultPtr); |
| 250 |
|
| 251 |
/* |
| 252 |
* Save the string result. |
| 253 |
*/ |
| 254 |
|
| 255 |
statePtr->freeProc = iPtr->freeProc; |
| 256 |
if (iPtr->result == iPtr->resultSpace) { |
| 257 |
/* |
| 258 |
* Copy the static string data out of the interp buffer. |
| 259 |
*/ |
| 260 |
|
| 261 |
statePtr->result = statePtr->resultSpace; |
| 262 |
strcpy(statePtr->result, iPtr->result); |
| 263 |
statePtr->appendResult = NULL; |
| 264 |
} else if (iPtr->result == iPtr->appendResult) { |
| 265 |
/* |
| 266 |
* Move the append buffer out of the interp. |
| 267 |
*/ |
| 268 |
|
| 269 |
statePtr->appendResult = iPtr->appendResult; |
| 270 |
statePtr->appendAvl = iPtr->appendAvl; |
| 271 |
statePtr->appendUsed = iPtr->appendUsed; |
| 272 |
statePtr->result = statePtr->appendResult; |
| 273 |
iPtr->appendResult = NULL; |
| 274 |
iPtr->appendAvl = 0; |
| 275 |
iPtr->appendUsed = 0; |
| 276 |
} else { |
| 277 |
/* |
| 278 |
* Move the dynamic or static string out of the interpreter. |
| 279 |
*/ |
| 280 |
|
| 281 |
statePtr->result = iPtr->result; |
| 282 |
statePtr->appendResult = NULL; |
| 283 |
} |
| 284 |
|
| 285 |
iPtr->result = iPtr->resultSpace; |
| 286 |
iPtr->resultSpace[0] = 0; |
| 287 |
iPtr->freeProc = 0; |
| 288 |
} |
| 289 |
|
| 290 |
/* |
| 291 |
*---------------------------------------------------------------------- |
| 292 |
* |
| 293 |
* Tcl_RestoreResult -- |
| 294 |
* |
| 295 |
* Restores the state of the interpreter to a snapshot taken by |
| 296 |
* Tcl_SaveResult. After this call, the token for the interpreter state |
| 297 |
* is no longer valid. |
| 298 |
* |
| 299 |
* Results: |
| 300 |
* None. |
| 301 |
* |
| 302 |
* Side effects: |
| 303 |
* Restores the interpreter result. |
| 304 |
* |
| 305 |
*---------------------------------------------------------------------- |
| 306 |
*/ |
| 307 |
|
| 308 |
#undef Tcl_RestoreResult |
| 309 |
void |
| 310 |
Tcl_RestoreResult( |
| 311 |
Tcl_Interp *interp, /* Interpreter being restored. */ |
| 312 |
Tcl_SavedResult *statePtr) /* State returned by Tcl_SaveResult. */ |
| 313 |
{ |
| 314 |
Interp *iPtr = (Interp *) interp; |
| 315 |
|
| 316 |
Tcl_ResetResult(interp); |
| 317 |
|
| 318 |
/* |
| 319 |
* Restore the string result. |
| 320 |
*/ |
| 321 |
|
| 322 |
iPtr->freeProc = statePtr->freeProc; |
| 323 |
if (statePtr->result == statePtr->resultSpace) { |
| 324 |
/* |
| 325 |
* Copy the static string data into the interp buffer. |
| 326 |
*/ |
| 327 |
|
| 328 |
iPtr->result = iPtr->resultSpace; |
| 329 |
strcpy(iPtr->result, statePtr->result); |
| 330 |
} else if (statePtr->result == statePtr->appendResult) { |
| 331 |
/* |
| 332 |
* Move the append buffer back into the interp. |
| 333 |
*/ |
| 334 |
|
| 335 |
if (iPtr->appendResult != NULL) { |
| 336 |
ckfree(iPtr->appendResult); |
| 337 |
} |
| 338 |
|
| 339 |
iPtr->appendResult = statePtr->appendResult; |
| 340 |
iPtr->appendAvl = statePtr->appendAvl; |
| 341 |
iPtr->appendUsed = statePtr->appendUsed; |
| 342 |
iPtr->result = iPtr->appendResult; |
| 343 |
} else { |
| 344 |
/* |
| 345 |
* Move the dynamic or static string back into the interpreter. |
| 346 |
*/ |
| 347 |
|
| 348 |
iPtr->result = statePtr->result; |
| 349 |
} |
| 350 |
|
| 351 |
/* |
| 352 |
* Restore the object result. |
| 353 |
*/ |
| 354 |
|
| 355 |
Tcl_DecrRefCount(iPtr->objResultPtr); |
| 356 |
iPtr->objResultPtr = statePtr->objResultPtr; |
| 357 |
} |
| 358 |
|
| 359 |
/* |
| 360 |
*---------------------------------------------------------------------- |
| 361 |
* |
| 362 |
* Tcl_DiscardResult -- |
| 363 |
* |
| 364 |
* Frees the memory associated with an interpreter snapshot taken by |
| 365 |
* Tcl_SaveResult. If the snapshot is not restored, this function must be |
| 366 |
* called to discard it, or the memory will be lost. |
| 367 |
* |
| 368 |
* Results: |
| 369 |
* None. |
| 370 |
* |
| 371 |
* Side effects: |
| 372 |
* None. |
| 373 |
* |
| 374 |
*---------------------------------------------------------------------- |
| 375 |
*/ |
| 376 |
|
| 377 |
#undef Tcl_DiscardResult |
| 378 |
void |
| 379 |
Tcl_DiscardResult( |
| 380 |
Tcl_SavedResult *statePtr) /* State returned by Tcl_SaveResult. */ |
| 381 |
{ |
| 382 |
TclDecrRefCount(statePtr->objResultPtr); |
| 383 |
|
| 384 |
if (statePtr->result == statePtr->appendResult) { |
| 385 |
ckfree(statePtr->appendResult); |
| 386 |
} else if (statePtr->freeProc == TCL_DYNAMIC) { |
| 387 |
ckfree(statePtr->result); |
| 388 |
} else if (statePtr->freeProc) { |
| 389 |
statePtr->freeProc(statePtr->result); |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
/* |
| 394 |
*---------------------------------------------------------------------- |
| 395 |
* |
| 396 |
* Tcl_SetResult -- |
| 397 |
* |
| 398 |
* Arrange for "result" to be the Tcl return value. |
| 399 |
* |
| 400 |
* Results: |
| 401 |
* None. |
| 402 |
* |
| 403 |
* Side effects: |
| 404 |
* interp->result is left pointing either to "result" or to a copy of it. |
| 405 |
* Also, the object result is reset. |
| 406 |
* |
| 407 |
*---------------------------------------------------------------------- |
| 408 |
*/ |
| 409 |
|
| 410 |
void |
| 411 |
Tcl_SetResult( |
| 412 |
Tcl_Interp *interp, /* Interpreter with which to associate the |
| 413 |
* return value. */ |
| 414 |
char *result, /* Value to be returned. If NULL, the result |
| 415 |
* is set to an empty string. */ |
| 416 |
Tcl_FreeProc *freeProc) /* Gives information about the string: |
| 417 |
* TCL_STATIC, TCL_VOLATILE, or the address of |
| 418 |
* a Tcl_FreeProc such as free. */ |
| 419 |
{ |
| 420 |
Interp *iPtr = (Interp *) interp; |
| 421 |
Tcl_FreeProc *oldFreeProc = iPtr->freeProc; |
| 422 |
char *oldResult = iPtr->result; |
| 423 |
|
| 424 |
if (result == NULL) { |
| 425 |
iPtr->resultSpace[0] = 0; |
| 426 |
iPtr->result = iPtr->resultSpace; |
| 427 |
iPtr->freeProc = 0; |
| 428 |
} else if (freeProc == TCL_VOLATILE) { |
| 429 |
int length = strlen(result); |
| 430 |
|
| 431 |
if (length > TCL_RESULT_SIZE) { |
| 432 |
iPtr->result = ckalloc(length + 1); |
| 433 |
iPtr->freeProc = TCL_DYNAMIC; |
| 434 |
} else { |
| 435 |
iPtr->result = iPtr->resultSpace; |
| 436 |
iPtr->freeProc = 0; |
| 437 |
} |
| 438 |
memcpy(iPtr->result, result, length+1); |
| 439 |
} else { |
| 440 |
iPtr->result = (char *) result; |
| 441 |
iPtr->freeProc = freeProc; |
| 442 |
} |
| 443 |
|
| 444 |
/* |
| 445 |
* If the old result was dynamically-allocated, free it up. Do it here, |
| 446 |
* rather than at the beginning, in case the new result value was part of |
| 447 |
* the old result value. |
| 448 |
*/ |
| 449 |
|
| 450 |
if (oldFreeProc != 0) { |
| 451 |
if (oldFreeProc == TCL_DYNAMIC) { |
| 452 |
ckfree(oldResult); |
| 453 |
} else { |
| 454 |
oldFreeProc(oldResult); |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
/* |
| 459 |
* Reset the object result since we just set the string result. |
| 460 |
*/ |
| 461 |
|
| 462 |
ResetObjResult(iPtr); |
| 463 |
} |
| 464 |
|
| 465 |
/* |
| 466 |
*---------------------------------------------------------------------- |
| 467 |
* |
| 468 |
* Tcl_GetStringResult -- |
| 469 |
* |
| 470 |
* Returns an interpreter's result value as a string. |
| 471 |
* |
| 472 |
* Results: |
| 473 |
* The interpreter's result as a string. |
| 474 |
* |
| 475 |
* Side effects: |
| 476 |
* If the string result is empty, the object result is moved to the |
| 477 |
* string result, then the object result is reset. |
| 478 |
* |
| 479 |
*---------------------------------------------------------------------- |
| 480 |
*/ |
| 481 |
|
| 482 |
const char * |
| 483 |
Tcl_GetStringResult( |
| 484 |
Tcl_Interp *interp)/* Interpreter whose result to return. */ |
| 485 |
{ |
| 486 |
/* |
| 487 |
* If the string result is empty, move the object result to the string |
| 488 |
* result, then reset the object result. |
| 489 |
*/ |
| 490 |
|
| 491 |
Interp *iPtr = (Interp *) interp; |
| 492 |
|
| 493 |
if (*(iPtr->result) == 0) { |
| 494 |
Tcl_SetResult(interp, Tcl_GetString(Tcl_GetObjResult(interp)), |
| 495 |
TCL_VOLATILE); |
| 496 |
} |
| 497 |
return iPtr->result; |
| 498 |
} |
| 499 |
|
| 500 |
/* |
| 501 |
*---------------------------------------------------------------------- |
| 502 |
* |
| 503 |
* Tcl_SetObjResult -- |
| 504 |
* |
| 505 |
* Arrange for objPtr to be an interpreter's result value. |
| 506 |
* |
| 507 |
* Results: |
| 508 |
* None. |
| 509 |
* |
| 510 |
* Side effects: |
| 511 |
* interp->objResultPtr is left pointing to the object referenced by |
| 512 |
* objPtr. The object's reference count is incremented since there is now |
| 513 |
* a new reference to it. The reference count for any old objResultPtr |
| 514 |
* value is decremented. Also, the string result is reset. |
| 515 |
* |
| 516 |
*---------------------------------------------------------------------- |
| 517 |
*/ |
| 518 |
|
| 519 |
void |
| 520 |
Tcl_SetObjResult( |
| 521 |
Tcl_Interp *interp, /* Interpreter with which to associate the |
| 522 |
* return object value. */ |
| 523 |
Tcl_Obj *objPtr) /* Tcl object to be returned. If NULL, the obj |
| 524 |
* result is made an empty string object. */ |
| 525 |
{ |
| 526 |
Interp *iPtr = (Interp *) interp; |
| 527 |
Tcl_Obj *oldObjResult = iPtr->objResultPtr; |
| 528 |
|
| 529 |
iPtr->objResultPtr = objPtr; |
| 530 |
Tcl_IncrRefCount(objPtr); /* since interp result is a reference */ |
| 531 |
|
| 532 |
/* |
| 533 |
* We wait until the end to release the old object result, in case we are |
| 534 |
* setting the result to itself. |
| 535 |
*/ |
| 536 |
|
| 537 |
TclDecrRefCount(oldObjResult); |
| 538 |
|
| 539 |
/* |
| 540 |
* Reset the string result since we just set the result object. |
| 541 |
*/ |
| 542 |
|
| 543 |
if (iPtr->freeProc != NULL) { |
| 544 |
if (iPtr->freeProc == TCL_DYNAMIC) { |
| 545 |
ckfree(iPtr->result); |
| 546 |
} else { |
| 547 |
iPtr->freeProc(iPtr->result); |
| 548 |
} |
| 549 |
iPtr->freeProc = 0; |
| 550 |
} |
| 551 |
iPtr->result = iPtr->resultSpace; |
| 552 |
iPtr->resultSpace[0] = 0; |
| 553 |
} |
| 554 |
|
| 555 |
/* |
| 556 |
*---------------------------------------------------------------------- |
| 557 |
* |
| 558 |
* Tcl_GetObjResult -- |
| 559 |
* |
| 560 |
* Returns an interpreter's result value as a Tcl object. The object's |
| 561 |
* reference count is not modified; the caller must do that if it needs |
| 562 |
* to hold on to a long-term reference to it. |
| 563 |
* |
| 564 |
* Results: |
| 565 |
* The interpreter's result as an object. |
| 566 |
* |
| 567 |
* Side effects: |
| 568 |
* If the interpreter has a non-empty string result, the result object is |
| 569 |
* either empty or stale because some function set interp->result |
| 570 |
* directly. If so, the string result is moved to the result object then |
| 571 |
* the string result is reset. |
| 572 |
* |
| 573 |
*---------------------------------------------------------------------- |
| 574 |
*/ |
| 575 |
|
| 576 |
Tcl_Obj * |
| 577 |
Tcl_GetObjResult( |
| 578 |
Tcl_Interp *interp) /* Interpreter whose result to return. */ |
| 579 |
{ |
| 580 |
Interp *iPtr = (Interp *) interp; |
| 581 |
Tcl_Obj *objResultPtr; |
| 582 |
int length; |
| 583 |
|
| 584 |
/* |
| 585 |
* If the string result is non-empty, move the string result to the object |
| 586 |
* result, then reset the string result. |
| 587 |
*/ |
| 588 |
|
| 589 |
if (iPtr->result[0] != 0) { |
| 590 |
ResetObjResult(iPtr); |
| 591 |
|
| 592 |
objResultPtr = iPtr->objResultPtr; |
| 593 |
length = strlen(iPtr->result); |
| 594 |
TclInitStringRep(objResultPtr, iPtr->result, length); |
| 595 |
|
| 596 |
if (iPtr->freeProc != NULL) { |
| 597 |
if (iPtr->freeProc == TCL_DYNAMIC) { |
| 598 |
ckfree(iPtr->result); |
| 599 |
} else { |
| 600 |
iPtr->freeProc(iPtr->result); |
| 601 |
} |
| 602 |
iPtr->freeProc = 0; |
| 603 |
} |
| 604 |
iPtr->result = iPtr->resultSpace; |
| 605 |
iPtr->result[0] = 0; |
| 606 |
} |
| 607 |
return iPtr->objResultPtr; |
| 608 |
} |
| 609 |
|
| 610 |
/* |
| 611 |
*---------------------------------------------------------------------- |
| 612 |
* |
| 613 |
* Tcl_AppendResultVA -- |
| 614 |
* |
| 615 |
* Append a variable number of strings onto the interpreter's result. |
| 616 |
* |
| 617 |
* Results: |
| 618 |
* None. |
| 619 |
* |
| 620 |
* Side effects: |
| 621 |
* The result of the interpreter given by the first argument is extended |
| 622 |
* by the strings in the va_list (up to a terminating NULL argument). |
| 623 |
* |
| 624 |
* If the string result is non-empty, the object result forced to be a |
| 625 |
* duplicate of it first. There will be a string result afterwards. |
| 626 |
* |
| 627 |
*---------------------------------------------------------------------- |
| 628 |
*/ |
| 629 |
|
| 630 |
void |
| 631 |
Tcl_AppendResultVA( |
| 632 |
Tcl_Interp *interp, /* Interpreter with which to associate the |
| 633 |
* return value. */ |
| 634 |
va_list argList) /* Variable argument list. */ |
| 635 |
{ |
| 636 |
Tcl_Obj *objPtr = Tcl_GetObjResult(interp); |
| 637 |
|
| 638 |
if (Tcl_IsShared(objPtr)) { |
| 639 |
objPtr = Tcl_DuplicateObj(objPtr); |
| 640 |
} |
| 641 |
Tcl_AppendStringsToObjVA(objPtr, argList); |
| 642 |
Tcl_SetObjResult(interp, objPtr); |
| 643 |
|
| 644 |
/* |
| 645 |
* Strictly we should call Tcl_GetStringResult(interp) here to make sure |
| 646 |
* that interp->result is correct according to the old contract, but that |
| 647 |
* makes the performance of much code (e.g. in Tk) absolutely awful. So we |
| 648 |
* leave it out; code that really wants interp->result can just insert the |
| 649 |
* calls to Tcl_GetStringResult() itself. [Patch 1041072 discussion] |
| 650 |
*/ |
| 651 |
|
| 652 |
#ifdef USE_INTERP_RESULT |
| 653 |
/* |
| 654 |
* Ensure that the interp->result is legal so old Tcl 7.* code still |
| 655 |
* works. There's still embarrasingly much of it about... |
| 656 |
*/ |
| 657 |
|
| 658 |
(void) Tcl_GetStringResult(interp); |
| 659 |
#endif /* USE_INTERP_RESULT */ |
| 660 |
} |
| 661 |
|
| 662 |
/* |
| 663 |
*---------------------------------------------------------------------- |
| 664 |
* |
| 665 |
* Tcl_AppendResult -- |
| 666 |
* |
| 667 |
* Append a variable number of strings onto the interpreter's result. |
| 668 |
* |
| 669 |
* Results: |
| 670 |
* None. |
| 671 |
* |
| 672 |
* Side effects: |
| 673 |
* The result of the interpreter given by the first argument is extended |
| 674 |
* by the strings given by the second and following arguments (up to a |
| 675 |
* terminating NULL argument). |
| 676 |
* |
| 677 |
* If the string result is non-empty, the object result forced to be a |
| 678 |
* duplicate of it first. There will be a string result afterwards. |
| 679 |
* |
| 680 |
*---------------------------------------------------------------------- |
| 681 |
*/ |
| 682 |
|
| 683 |
void |
| 684 |
Tcl_AppendResult( |
| 685 |
Tcl_Interp *interp, ...) |
| 686 |
{ |
| 687 |
va_list argList; |
| 688 |
|
| 689 |
va_start(argList, interp); |
| 690 |
Tcl_AppendResultVA(interp, argList); |
| 691 |
va_end(argList); |
| 692 |
} |
| 693 |
|
| 694 |
/* |
| 695 |
*---------------------------------------------------------------------- |
| 696 |
* |
| 697 |
* Tcl_AppendElement -- |
| 698 |
* |
| 699 |
* Convert a string to a valid Tcl list element and append it to the |
| 700 |
* result (which is ostensibly a list). |
| 701 |
* |
| 702 |
* Results: |
| 703 |
* None. |
| 704 |
* |
| 705 |
* Side effects: |
| 706 |
* The result in the interpreter given by the first argument is extended |
| 707 |
* with a list element converted from string. A separator space is added |
| 708 |
* before the converted list element unless the current result is empty, |
| 709 |
* contains the single character "{", or ends in " {". |
| 710 |
* |
| 711 |
* If the string result is empty, the object result is moved to the |
| 712 |
* string result, then the object result is reset. |
| 713 |
* |
| 714 |
*---------------------------------------------------------------------- |
| 715 |
*/ |
| 716 |
|
| 717 |
void |
| 718 |
Tcl_AppendElement( |
| 719 |
Tcl_Interp *interp, /* Interpreter whose result is to be |
| 720 |
* extended. */ |
| 721 |
const char *element) /* String to convert to list element and add |
| 722 |
* to result. */ |
| 723 |
{ |
| 724 |
Interp *iPtr = (Interp *) interp; |
| 725 |
char *dst; |
| 726 |
int size; |
| 727 |
int flags; |
| 728 |
int quoteHash = 1; |
| 729 |
|
| 730 |
/* |
| 731 |
* If the string result is empty, move the object result to the string |
| 732 |
* result, then reset the object result. |
| 733 |
*/ |
| 734 |
|
| 735 |
(void) Tcl_GetStringResult(interp); |
| 736 |
|
| 737 |
/* |
| 738 |
* See how much space is needed, and grow the append buffer if needed to |
| 739 |
* accommodate the list element. |
| 740 |
*/ |
| 741 |
|
| 742 |
size = Tcl_ScanElement(element, &flags) + 1; |
| 743 |
if ((iPtr->result != iPtr->appendResult) |
| 744 |
|| (iPtr->appendResult[iPtr->appendUsed] != 0) |
| 745 |
|| ((size + iPtr->appendUsed) >= iPtr->appendAvl)) { |
| 746 |
SetupAppendBuffer(iPtr, size+iPtr->appendUsed); |
| 747 |
} |
| 748 |
|
| 749 |
/* |
| 750 |
* Convert the string into a list element and copy it to the buffer that's |
| 751 |
* forming, with a space separator if needed. |
| 752 |
*/ |
| 753 |
|
| 754 |
dst = iPtr->appendResult + iPtr->appendUsed; |
| 755 |
if (TclNeedSpace(iPtr->appendResult, dst)) { |
| 756 |
iPtr->appendUsed++; |
| 757 |
*dst = ' '; |
| 758 |
dst++; |
| 759 |
|
| 760 |
/* |
| 761 |
* If we need a space to separate this element from preceding stuff, |
| 762 |
* then this element will not lead a list, and need not have it's |
| 763 |
* leading '#' quoted. |
| 764 |
*/ |
| 765 |
quoteHash = 0; |
| 766 |
} else { |
| 767 |
while ((--dst >= iPtr->appendResult) && TclIsSpaceProcM(*dst)) { |
| 768 |
} |
| 769 |
quoteHash = !TclNeedSpace(iPtr->appendResult, dst+1); |
| 770 |
} |
| 771 |
dst = iPtr->appendResult + iPtr->appendUsed; |
| 772 |
if (!quoteHash) { |
| 773 |
flags |= TCL_DONT_QUOTE_HASH; |
| 774 |
} |
| 775 |
|
| 776 |
iPtr->appendUsed += Tcl_ConvertElement(element, dst, flags); |
| 777 |
} |
| 778 |
|
| 779 |
/* |
| 780 |
*---------------------------------------------------------------------- |
| 781 |
* |
| 782 |
* SetupAppendBuffer -- |
| 783 |
* |
| 784 |
* This function makes sure that there is an append buffer properly |
| 785 |
* initialized, if necessary, from the interpreter's result, and that it |
| 786 |
* has at least enough room to accommodate newSpace new bytes of |
| 787 |
* information. |
| 788 |
* |
| 789 |
* Results: |
| 790 |
* None. |
| 791 |
* |
| 792 |
* Side effects: |
| 793 |
* None. |
| 794 |
* |
| 795 |
*---------------------------------------------------------------------- |
| 796 |
*/ |
| 797 |
|
| 798 |
static void |
| 799 |
SetupAppendBuffer( |
| 800 |
Interp *iPtr, /* Interpreter whose result is being set up. */ |
| 801 |
int newSpace) /* Make sure that at least this many bytes of |
| 802 |
* new information may be added. */ |
| 803 |
{ |
| 804 |
int totalSpace; |
| 805 |
|
| 806 |
/* |
| 807 |
* Make the append buffer larger, if that's necessary, then copy the |
| 808 |
* result into the append buffer and make the append buffer the official |
| 809 |
* Tcl result. |
| 810 |
*/ |
| 811 |
|
| 812 |
if (iPtr->result != iPtr->appendResult) { |
| 813 |
/* |
| 814 |
* If an oversized buffer was used recently, then free it up so we go |
| 815 |
* back to a smaller buffer. This avoids tying up memory forever after |
| 816 |
* a large operation. |
| 817 |
*/ |
| 818 |
|
| 819 |
if (iPtr->appendAvl > 500) { |
| 820 |
ckfree(iPtr->appendResult); |
| 821 |
iPtr->appendResult = NULL; |
| 822 |
iPtr->appendAvl = 0; |
| 823 |
} |
| 824 |
iPtr->appendUsed = strlen(iPtr->result); |
| 825 |
} else if (iPtr->result[iPtr->appendUsed] != 0) { |
| 826 |
/* |
| 827 |
* Most likely someone has modified a result created by |
| 828 |
* Tcl_AppendResult et al. so that it has a different size. Just |
| 829 |
* recompute the size. |
| 830 |
*/ |
| 831 |
|
| 832 |
iPtr->appendUsed = strlen(iPtr->result); |
| 833 |
} |
| 834 |
|
| 835 |
totalSpace = newSpace + iPtr->appendUsed; |
| 836 |
if (totalSpace >= iPtr->appendAvl) { |
| 837 |
char *new; |
| 838 |
|
| 839 |
if (totalSpace < 100) { |
| 840 |
totalSpace = 200; |
| 841 |
} else { |
| 842 |
totalSpace *= 2; |
| 843 |
} |
| 844 |
new = ckalloc(totalSpace); |
| 845 |
strcpy(new, iPtr->result); |
| 846 |
if (iPtr->appendResult != NULL) { |
| 847 |
ckfree(iPtr->appendResult); |
| 848 |
} |
| 849 |
iPtr->appendResult = new; |
| 850 |
iPtr->appendAvl = totalSpace; |
| 851 |
} else if (iPtr->result != iPtr->appendResult) { |
| 852 |
strcpy(iPtr->appendResult, iPtr->result); |
| 853 |
} |
| 854 |
|
| 855 |
Tcl_FreeResult((Tcl_Interp *) iPtr); |
| 856 |
iPtr->result = iPtr->appendResult; |
| 857 |
} |
| 858 |
|
| 859 |
/* |
| 860 |
*---------------------------------------------------------------------- |
| 861 |
* |
| 862 |
* Tcl_FreeResult -- |
| 863 |
* |
| 864 |
* This function frees up the memory associated with an interpreter's |
| 865 |
* string result. It also resets the interpreter's result object. |
| 866 |
* Tcl_FreeResult is most commonly used when a function is about to |
| 867 |
* replace one result value with another. |
| 868 |
* |
| 869 |
* Results: |
| 870 |
* None. |
| 871 |
* |
| 872 |
* Side effects: |
| 873 |
* Frees the memory associated with interp's string result and sets |
| 874 |
* interp->freeProc to zero, but does not change interp->result or clear |
| 875 |
* error state. Resets interp's result object to an unshared empty |
| 876 |
* object. |
| 877 |
* |
| 878 |
*---------------------------------------------------------------------- |
| 879 |
*/ |
| 880 |
|
| 881 |
void |
| 882 |
Tcl_FreeResult( |
| 883 |
Tcl_Interp *interp)/* Interpreter for which to free result. */ |
| 884 |
{ |
| 885 |
Interp *iPtr = (Interp *) interp; |
| 886 |
|
| 887 |
if (iPtr->freeProc != NULL) { |
| 888 |
if (iPtr->freeProc == TCL_DYNAMIC) { |
| 889 |
ckfree(iPtr->result); |
| 890 |
} else { |
| 891 |
iPtr->freeProc(iPtr->result); |
| 892 |
} |
| 893 |
iPtr->freeProc = 0; |
| 894 |
} |
| 895 |
|
| 896 |
ResetObjResult(iPtr); |
| 897 |
} |
| 898 |
|
| 899 |
/* |
| 900 |
*---------------------------------------------------------------------- |
| 901 |
* |
| 902 |
* Tcl_ResetResult -- |
| 903 |
* |
| 904 |
* This function resets both the interpreter's string and object results. |
| 905 |
* |
| 906 |
* Results: |
| 907 |
* None. |
| 908 |
* |
| 909 |
* Side effects: |
| 910 |
* It resets the result object to an unshared empty object. It then |
| 911 |
* restores the interpreter's string result area to its default |
| 912 |
* initialized state, freeing up any memory that may have been allocated. |
| 913 |
* It also clears any error information for the interpreter. |
| 914 |
* |
| 915 |
*---------------------------------------------------------------------- |
| 916 |
*/ |
| 917 |
|
| 918 |
void |
| 919 |
Tcl_ResetResult( |
| 920 |
Tcl_Interp *interp) /* Interpreter for which to clear result. */ |
| 921 |
{ |
| 922 |
Interp *iPtr = (Interp *) interp; |
| 923 |
|
| 924 |
ResetObjResult(iPtr); |
| 925 |
if (iPtr->freeProc != NULL) { |
| 926 |
if (iPtr->freeProc == TCL_DYNAMIC) { |
| 927 |
ckfree(iPtr->result); |
| 928 |
} else { |
| 929 |
iPtr->freeProc(iPtr->result); |
| 930 |
} |
| 931 |
iPtr->freeProc = 0; |
| 932 |
} |
| 933 |
iPtr->result = iPtr->resultSpace; |
| 934 |
iPtr->resultSpace[0] = 0; |
| 935 |
if (iPtr->errorCode) { |
| 936 |
/* Legacy support */ |
| 937 |
if (iPtr->flags & ERR_LEGACY_COPY) { |
| 938 |
Tcl_ObjSetVar2(interp, iPtr->ecVar, NULL, |
| 939 |
iPtr->errorCode, TCL_GLOBAL_ONLY); |
| 940 |
} |
| 941 |
Tcl_DecrRefCount(iPtr->errorCode); |
| 942 |
iPtr->errorCode = NULL; |
| 943 |
} |
| 944 |
if (iPtr->errorInfo) { |
| 945 |
/* Legacy support */ |
| 946 |
if (iPtr->flags & ERR_LEGACY_COPY) { |
| 947 |
Tcl_ObjSetVar2(interp, iPtr->eiVar, NULL, |
| 948 |
iPtr->errorInfo, TCL_GLOBAL_ONLY); |
| 949 |
} |
| 950 |
Tcl_DecrRefCount(iPtr->errorInfo); |
| 951 |
iPtr->errorInfo = NULL; |
| 952 |
} |
| 953 |
iPtr->resetErrorStack = 1; |
| 954 |
iPtr->returnLevel = 1; |
| 955 |
iPtr->returnCode = TCL_OK; |
| 956 |
if (iPtr->returnOpts) { |
| 957 |
Tcl_DecrRefCount(iPtr->returnOpts); |
| 958 |
iPtr->returnOpts = NULL; |
| 959 |
} |
| 960 |
iPtr->flags &= ~(ERR_ALREADY_LOGGED | ERR_LEGACY_COPY); |
| 961 |
} |
| 962 |
|
| 963 |
/* |
| 964 |
*---------------------------------------------------------------------- |
| 965 |
* |
| 966 |
* ResetObjResult -- |
| 967 |
* |
| 968 |
* Function used to reset an interpreter's Tcl result object. |
| 969 |
* |
| 970 |
* Results: |
| 971 |
* None. |
| 972 |
* |
| 973 |
* Side effects: |
| 974 |
* Resets the interpreter's result object to an unshared empty string |
| 975 |
* object with ref count one. It does not clear any error information in |
| 976 |
* the interpreter. |
| 977 |
* |
| 978 |
*---------------------------------------------------------------------- |
| 979 |
*/ |
| 980 |
|
| 981 |
static void |
| 982 |
ResetObjResult( |
| 983 |
Interp *iPtr) /* Points to the interpreter whose result |
| 984 |
* object should be reset. */ |
| 985 |
{ |
| 986 |
Tcl_Obj *objResultPtr = iPtr->objResultPtr; |
| 987 |
|
| 988 |
if (Tcl_IsShared(objResultPtr)) { |
| 989 |
TclDecrRefCount(objResultPtr); |
| 990 |
TclNewObj(objResultPtr); |
| 991 |
Tcl_IncrRefCount(objResultPtr); |
| 992 |
iPtr->objResultPtr = objResultPtr; |
| 993 |
} else { |
| 994 |
if (objResultPtr->bytes != tclEmptyStringRep) { |
| 995 |
if (objResultPtr->bytes) { |
| 996 |
ckfree(objResultPtr->bytes); |
| 997 |
} |
| 998 |
objResultPtr->bytes = tclEmptyStringRep; |
| 999 |
objResultPtr->length = 0; |
| 1000 |
} |
| 1001 |
TclFreeIntRep(objResultPtr); |
| 1002 |
} |
| 1003 |
} |
| 1004 |
|
| 1005 |
/* |
| 1006 |
*---------------------------------------------------------------------- |
| 1007 |
* |
| 1008 |
* Tcl_SetErrorCodeVA -- |
| 1009 |
* |
| 1010 |
* This function is called to record machine-readable information about |
| 1011 |
* an error that is about to be returned. |
| 1012 |
* |
| 1013 |
* Results: |
| 1014 |
* None. |
| 1015 |
* |
| 1016 |
* Side effects: |
| 1017 |
* The errorCode field of the interp is modified to hold all of the |
| 1018 |
* arguments to this function, in a list form with each argument becoming |
| 1019 |
* one element of the list. |
| 1020 |
* |
| 1021 |
*---------------------------------------------------------------------- |
| 1022 |
*/ |
| 1023 |
|
| 1024 |
void |
| 1025 |
Tcl_SetErrorCodeVA( |
| 1026 |
Tcl_Interp *interp, /* Interpreter in which to set errorCode */ |
| 1027 |
va_list argList) /* Variable argument list. */ |
| 1028 |
{ |
| 1029 |
Tcl_Obj *errorObj; |
| 1030 |
|
| 1031 |
TclNewObj(errorObj); |
| 1032 |
|
| 1033 |
/* |
| 1034 |
* Scan through the arguments one at a time, appending them to the |
| 1035 |
* errorCode field as list elements. |
| 1036 |
*/ |
| 1037 |
|
| 1038 |
while (1) { |
| 1039 |
char *elem = va_arg(argList, char *); |
| 1040 |
|
| 1041 |
if (elem == NULL) { |
| 1042 |
break; |
| 1043 |
} |
| 1044 |
Tcl_ListObjAppendElement(NULL, errorObj, Tcl_NewStringObj(elem, -1)); |
| 1045 |
} |
| 1046 |
Tcl_SetObjErrorCode(interp, errorObj); |
| 1047 |
} |
| 1048 |
|
| 1049 |
/* |
| 1050 |
*---------------------------------------------------------------------- |
| 1051 |
* |
| 1052 |
* Tcl_SetErrorCode -- |
| 1053 |
* |
| 1054 |
* This function is called to record machine-readable information about |
| 1055 |
* an error that is about to be returned. |
| 1056 |
* |
| 1057 |
* Results: |
| 1058 |
* None. |
| 1059 |
* |
| 1060 |
* Side effects: |
| 1061 |
* The errorCode field of the interp is modified to hold all of the |
| 1062 |
* arguments to this function, in a list form with each argument becoming |
| 1063 |
* one element of the list. |
| 1064 |
* |
| 1065 |
*---------------------------------------------------------------------- |
| 1066 |
*/ |
| 1067 |
|
| 1068 |
void |
| 1069 |
Tcl_SetErrorCode( |
| 1070 |
Tcl_Interp *interp, ...) |
| 1071 |
{ |
| 1072 |
va_list argList; |
| 1073 |
|
| 1074 |
/* |
| 1075 |
* Scan through the arguments one at a time, appending them to the |
| 1076 |
* errorCode field as list elements. |
| 1077 |
*/ |
| 1078 |
|
| 1079 |
va_start(argList, interp); |
| 1080 |
Tcl_SetErrorCodeVA(interp, argList); |
| 1081 |
va_end(argList); |
| 1082 |
} |
| 1083 |
|
| 1084 |
/* |
| 1085 |
*---------------------------------------------------------------------- |
| 1086 |
* |
| 1087 |
* Tcl_SetObjErrorCode -- |
| 1088 |
* |
| 1089 |
* This function is called to record machine-readable information about |
| 1090 |
* an error that is about to be returned. The caller should build a list |
| 1091 |
* object up and pass it to this routine. |
| 1092 |
* |
| 1093 |
* Results: |
| 1094 |
* None. |
| 1095 |
* |
| 1096 |
* Side effects: |
| 1097 |
* The errorCode field of the interp is set to the new value. |
| 1098 |
* |
| 1099 |
*---------------------------------------------------------------------- |
| 1100 |
*/ |
| 1101 |
|
| 1102 |
void |
| 1103 |
Tcl_SetObjErrorCode( |
| 1104 |
Tcl_Interp *interp, |
| 1105 |
Tcl_Obj *errorObjPtr) |
| 1106 |
{ |
| 1107 |
Interp *iPtr = (Interp *) interp; |
| 1108 |
|
| 1109 |
if (iPtr->errorCode) { |
| 1110 |
Tcl_DecrRefCount(iPtr->errorCode); |
| 1111 |
} |
| 1112 |
iPtr->errorCode = errorObjPtr; |
| 1113 |
Tcl_IncrRefCount(iPtr->errorCode); |
| 1114 |
} |
| 1115 |
|
| 1116 |
/* |
| 1117 |
*---------------------------------------------------------------------- |
| 1118 |
* |
| 1119 |
* Tcl_GetErrorLine -- |
| 1120 |
* |
| 1121 |
* Returns the line number associated with the current error. |
| 1122 |
* |
| 1123 |
*---------------------------------------------------------------------- |
| 1124 |
*/ |
| 1125 |
|
| 1126 |
#undef Tcl_GetErrorLine |
| 1127 |
int |
| 1128 |
Tcl_GetErrorLine( |
| 1129 |
Tcl_Interp *interp) |
| 1130 |
{ |
| 1131 |
return ((Interp *) interp)->errorLine; |
| 1132 |
} |
| 1133 |
|
| 1134 |
/* |
| 1135 |
*---------------------------------------------------------------------- |
| 1136 |
* |
| 1137 |
* Tcl_SetErrorLine -- |
| 1138 |
* |
| 1139 |
* Sets the line number associated with the current error. |
| 1140 |
* |
| 1141 |
*---------------------------------------------------------------------- |
| 1142 |
*/ |
| 1143 |
|
| 1144 |
#undef Tcl_SetErrorLine |
| 1145 |
void |
| 1146 |
Tcl_SetErrorLine( |
| 1147 |
Tcl_Interp *interp, |
| 1148 |
int value) |
| 1149 |
{ |
| 1150 |
((Interp *) interp)->errorLine = value; |
| 1151 |
} |
| 1152 |
|
| 1153 |
/* |
| 1154 |
*---------------------------------------------------------------------- |
| 1155 |
* |
| 1156 |
* GetKeys -- |
| 1157 |
* |
| 1158 |
* Returns a Tcl_Obj * array of the standard keys used in the return |
| 1159 |
* options dictionary. |
| 1160 |
* |
| 1161 |
* Broadly sharing one copy of these key values helps with both memory |
| 1162 |
* efficiency and dictionary lookup times. |
| 1163 |
* |
| 1164 |
* Results: |
| 1165 |
* A Tcl_Obj * array. |
| 1166 |
* |
| 1167 |
* Side effects: |
| 1168 |
* First time called in a thread, creates the keys (allocating memory) |
| 1169 |
* and arranges for their cleanup at thread exit. |
| 1170 |
* |
| 1171 |
*---------------------------------------------------------------------- |
| 1172 |
*/ |
| 1173 |
|
| 1174 |
static Tcl_Obj ** |
| 1175 |
GetKeys(void) |
| 1176 |
{ |
| 1177 |
static Tcl_ThreadDataKey returnKeysKey; |
| 1178 |
Tcl_Obj **keys = (Tcl_Obj **)Tcl_GetThreadData(&returnKeysKey, |
| 1179 |
KEY_LAST * sizeof(Tcl_Obj *)); |
| 1180 |
|
| 1181 |
if (keys[0] == NULL) { |
| 1182 |
/* |
| 1183 |
* First call in this thread, create the keys... |
| 1184 |
*/ |
| 1185 |
|
| 1186 |
int i; |
| 1187 |
|
| 1188 |
TclNewLiteralStringObj(keys[KEY_CODE], "-code"); |
| 1189 |
TclNewLiteralStringObj(keys[KEY_ERRORCODE], "-errorcode"); |
| 1190 |
TclNewLiteralStringObj(keys[KEY_ERRORINFO], "-errorinfo"); |
| 1191 |
TclNewLiteralStringObj(keys[KEY_ERRORLINE], "-errorline"); |
| 1192 |
TclNewLiteralStringObj(keys[KEY_ERRORSTACK],"-errorstack"); |
| 1193 |
TclNewLiteralStringObj(keys[KEY_LEVEL], "-level"); |
| 1194 |
TclNewLiteralStringObj(keys[KEY_OPTIONS], "-options"); |
| 1195 |
|
| 1196 |
for (i = KEY_CODE; i < KEY_LAST; i++) { |
| 1197 |
Tcl_IncrRefCount(keys[i]); |
| 1198 |
} |
| 1199 |
|
| 1200 |
/* |
| 1201 |
* ... and arrange for their clenaup. |
| 1202 |
*/ |
| 1203 |
|
| 1204 |
Tcl_CreateThreadExitHandler(ReleaseKeys, keys); |
| 1205 |
} |
| 1206 |
return keys; |
| 1207 |
} |
| 1208 |
|
| 1209 |
/* |
| 1210 |
*---------------------------------------------------------------------- |
| 1211 |
* |
| 1212 |
* ReleaseKeys -- |
| 1213 |
* |
| 1214 |
* Called as a thread exit handler to cleanup return options dictionary |
| 1215 |
* keys. |
| 1216 |
* |
| 1217 |
* Results: |
| 1218 |
* None. |
| 1219 |
* |
| 1220 |
* Side effects: |
| 1221 |
* Frees memory. |
| 1222 |
* |
| 1223 |
*---------------------------------------------------------------------- |
| 1224 |
*/ |
| 1225 |
|
| 1226 |
static void |
| 1227 |
ReleaseKeys( |
| 1228 |
ClientData clientData) |
| 1229 |
{ |
| 1230 |
Tcl_Obj **keys = (Tcl_Obj **)clientData; |
| 1231 |
int i; |
| 1232 |
|
| 1233 |
for (i = KEY_CODE; i < KEY_LAST; i++) { |
| 1234 |
Tcl_DecrRefCount(keys[i]); |
| 1235 |
keys[i] = NULL; |
| 1236 |
} |
| 1237 |
} |
| 1238 |
|
| 1239 |
/* |
| 1240 |
*---------------------------------------------------------------------- |
| 1241 |
* |
| 1242 |
* TclProcessReturn -- |
| 1243 |
* |
| 1244 |
* Does the work of the [return] command based on the code, level, and |
| 1245 |
* returnOpts arguments. Note that the code argument must agree with the |
| 1246 |
* -code entry in returnOpts and the level argument must agree with the |
| 1247 |
* -level entry in returnOpts, as is the case for values returned from |
| 1248 |
* TclMergeReturnOptions. |
| 1249 |
* |
| 1250 |
* Results: |
| 1251 |
* Returns the return code the [return] command should return. |
| 1252 |
* |
| 1253 |
* Side effects: |
| 1254 |
* None. |
| 1255 |
* |
| 1256 |
*---------------------------------------------------------------------- |
| 1257 |
*/ |
| 1258 |
|
| 1259 |
int |
| 1260 |
TclProcessReturn( |
| 1261 |
Tcl_Interp *interp, |
| 1262 |
int code, |
| 1263 |
int level, |
| 1264 |
Tcl_Obj *returnOpts) |
| 1265 |
{ |
| 1266 |
Interp *iPtr = (Interp *) interp; |
| 1267 |
Tcl_Obj *valuePtr; |
| 1268 |
Tcl_Obj **keys = GetKeys(); |
| 1269 |
|
| 1270 |
/* |
| 1271 |
* Store the merged return options. |
| 1272 |
*/ |
| 1273 |
|
| 1274 |
if (iPtr->returnOpts != returnOpts) { |
| 1275 |
if (iPtr->returnOpts) { |
| 1276 |
Tcl_DecrRefCount(iPtr->returnOpts); |
| 1277 |
} |
| 1278 |
iPtr->returnOpts = returnOpts; |
| 1279 |
Tcl_IncrRefCount(iPtr->returnOpts); |
| 1280 |
} |
| 1281 |
|
| 1282 |
if (code == TCL_ERROR) { |
| 1283 |
if (iPtr->errorInfo) { |
| 1284 |
Tcl_DecrRefCount(iPtr->errorInfo); |
| 1285 |
iPtr->errorInfo = NULL; |
| 1286 |
} |
| 1287 |
Tcl_DictObjGet(NULL, iPtr->returnOpts, keys[KEY_ERRORINFO], |
| 1288 |
&valuePtr); |
| 1289 |
if (valuePtr != NULL) { |
| 1290 |
int length; |
| 1291 |
|
| 1292 |
(void)TclGetStringFromObj(valuePtr, &length); |
| 1293 |
if (length) { |
| 1294 |
iPtr->errorInfo = valuePtr; |
| 1295 |
Tcl_IncrRefCount(iPtr->errorInfo); |
| 1296 |
iPtr->flags |= ERR_ALREADY_LOGGED; |
| 1297 |
} |
| 1298 |
} |
| 1299 |
Tcl_DictObjGet(NULL, iPtr->returnOpts, keys[KEY_ERRORSTACK], |
| 1300 |
&valuePtr); |
| 1301 |
if (valuePtr != NULL) { |
| 1302 |
int len, valueObjc; |
| 1303 |
Tcl_Obj **valueObjv; |
| 1304 |
|
| 1305 |
if (Tcl_IsShared(iPtr->errorStack)) { |
| 1306 |
Tcl_Obj *newObj; |
| 1307 |
|
| 1308 |
newObj = Tcl_DuplicateObj(iPtr->errorStack); |
| 1309 |
Tcl_DecrRefCount(iPtr->errorStack); |
| 1310 |
Tcl_IncrRefCount(newObj); |
| 1311 |
iPtr->errorStack = newObj; |
| 1312 |
} |
| 1313 |
|
| 1314 |
/* |
| 1315 |
* List extraction done after duplication to avoid moving the rug |
| 1316 |
* if someone does [return -errorstack [info errorstack]] |
| 1317 |
*/ |
| 1318 |
|
| 1319 |
if (TclListObjGetElements(interp, valuePtr, &valueObjc, |
| 1320 |
&valueObjv) == TCL_ERROR) { |
| 1321 |
return TCL_ERROR; |
| 1322 |
} |
| 1323 |
iPtr->resetErrorStack = 0; |
| 1324 |
TclListObjLength(interp, iPtr->errorStack, &len); |
| 1325 |
|
| 1326 |
/* |
| 1327 |
* Reset while keeping the list internalrep as much as possible. |
| 1328 |
*/ |
| 1329 |
|
| 1330 |
Tcl_ListObjReplace(interp, iPtr->errorStack, 0, len, valueObjc, |
| 1331 |
valueObjv); |
| 1332 |
} |
| 1333 |
Tcl_DictObjGet(NULL, iPtr->returnOpts, keys[KEY_ERRORCODE], |
| 1334 |
&valuePtr); |
| 1335 |
if (valuePtr != NULL) { |
| 1336 |
Tcl_SetObjErrorCode(interp, valuePtr); |
| 1337 |
} else { |
| 1338 |
Tcl_SetErrorCode(interp, "NONE", (char *)NULL); |
| 1339 |
} |
| 1340 |
|
| 1341 |
Tcl_DictObjGet(NULL, iPtr->returnOpts, keys[KEY_ERRORLINE], |
| 1342 |
&valuePtr); |
| 1343 |
if (valuePtr != NULL) { |
| 1344 |
TclGetIntFromObj(NULL, valuePtr, &iPtr->errorLine); |
| 1345 |
} |
| 1346 |
} |
| 1347 |
if (level != 0) { |
| 1348 |
iPtr->returnLevel = level; |
| 1349 |
iPtr->returnCode = code; |
| 1350 |
return TCL_RETURN; |
| 1351 |
} |
| 1352 |
if (code == TCL_ERROR) { |
| 1353 |
iPtr->flags |= ERR_LEGACY_COPY; |
| 1354 |
} |
| 1355 |
return code; |
| 1356 |
} |
| 1357 |
|
| 1358 |
/* |
| 1359 |
*---------------------------------------------------------------------- |
| 1360 |
* |
| 1361 |
* TclMergeReturnOptions -- |
| 1362 |
* |
| 1363 |
* Parses, checks, and stores the options to the [return] command. |
| 1364 |
* |
| 1365 |
* The number of arguments (objc) must be even, with the corresponding |
| 1366 |
* objv holding values to be processed as key value .... key value. |
| 1367 |
* |
| 1368 |
* Results: |
| 1369 |
* Returns TCL_ERROR if any of the option values are invalid. Otherwise, |
| 1370 |
* returns TCL_OK, and writes the returnOpts, code, and level values to |
| 1371 |
* the pointers provided. |
| 1372 |
* |
| 1373 |
* Side effects: |
| 1374 |
* None. |
| 1375 |
* |
| 1376 |
*---------------------------------------------------------------------- |
| 1377 |
*/ |
| 1378 |
|
| 1379 |
static int |
| 1380 |
ExpandedOptions( |
| 1381 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 1382 |
Tcl_Obj **keys, /* Built-in keys (per thread) */ |
| 1383 |
Tcl_Obj *returnOpts, /* Options dict we are building */ |
| 1384 |
int objc, /* Number of arguments. */ |
| 1385 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 1386 |
{ |
| 1387 |
for (; objc > 1; objv += 2, objc -= 2) { |
| 1388 |
const char *opt = TclGetString(objv[0]); |
| 1389 |
const char *compare = TclGetString(keys[KEY_OPTIONS]); |
| 1390 |
|
| 1391 |
if ((objv[0]->length == keys[KEY_OPTIONS]->length) |
| 1392 |
&& (memcmp(opt, compare, objv[0]->length) == 0)) { |
| 1393 |
/* Process the -options switch to emulate {*} expansion. |
| 1394 |
* |
| 1395 |
* Use lists so duplicate keys are not lost. |
| 1396 |
*/ |
| 1397 |
|
| 1398 |
int nestc; |
| 1399 |
Tcl_Obj **nestv; |
| 1400 |
|
| 1401 |
if (TCL_ERROR == TclListObjGetElements(interp, objv[1], |
| 1402 |
&nestc, &nestv) || (nestc % 2)) { |
| 1403 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1404 |
"bad -options value: expected dictionary but got" |
| 1405 |
" \"%s\"", TclGetString(objv[1]))); |
| 1406 |
Tcl_SetErrorCode(interp, "TCL", "RESULT", "ILLEGAL_OPTIONS", |
| 1407 |
(char *)NULL); |
| 1408 |
return TCL_ERROR; |
| 1409 |
} |
| 1410 |
|
| 1411 |
if (TCL_ERROR == |
| 1412 |
ExpandedOptions(interp, keys, returnOpts, nestc, nestv)) { |
| 1413 |
return TCL_ERROR; |
| 1414 |
} |
| 1415 |
} else { |
| 1416 |
Tcl_DictObjPut(NULL, returnOpts, objv[0], objv[1]); |
| 1417 |
} |
| 1418 |
} |
| 1419 |
return TCL_OK; |
| 1420 |
} |
| 1421 |
|
| 1422 |
int |
| 1423 |
TclMergeReturnOptions( |
| 1424 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 1425 |
int objc, /* Number of arguments. */ |
| 1426 |
Tcl_Obj *const objv[], /* Argument objects. */ |
| 1427 |
Tcl_Obj **optionsPtrPtr, /* If not NULL, points to space for a (Tcl_Obj |
| 1428 |
* *) where the pointer to the merged return |
| 1429 |
* options dictionary should be written. */ |
| 1430 |
int *codePtr, /* If not NULL, points to space where the |
| 1431 |
* -code value should be written. */ |
| 1432 |
int *levelPtr) /* If not NULL, points to space where the |
| 1433 |
* -level value should be written. */ |
| 1434 |
{ |
| 1435 |
int code = TCL_OK; |
| 1436 |
int level = 1; |
| 1437 |
Tcl_Obj *valuePtr; |
| 1438 |
Tcl_Obj *returnOpts; |
| 1439 |
Tcl_Obj **keys = GetKeys(); |
| 1440 |
|
| 1441 |
/* All callers are expected to pass an even value for objc. */ |
| 1442 |
|
| 1443 |
TclNewObj(returnOpts); |
| 1444 |
if (TCL_ERROR == ExpandedOptions(interp, keys, returnOpts, objc, objv)) { |
| 1445 |
goto error; |
| 1446 |
} |
| 1447 |
|
| 1448 |
/* |
| 1449 |
* Check for bogus -code value. |
| 1450 |
*/ |
| 1451 |
|
| 1452 |
Tcl_DictObjGet(NULL, returnOpts, keys[KEY_CODE], &valuePtr); |
| 1453 |
if (valuePtr != NULL) { |
| 1454 |
if (TclGetCompletionCodeFromObj(interp, valuePtr, |
| 1455 |
&code) == TCL_ERROR) { |
| 1456 |
goto error; |
| 1457 |
} |
| 1458 |
Tcl_DictObjRemove(NULL, returnOpts, keys[KEY_CODE]); |
| 1459 |
} |
| 1460 |
|
| 1461 |
/* |
| 1462 |
* Check for bogus -level value. |
| 1463 |
*/ |
| 1464 |
|
| 1465 |
Tcl_DictObjGet(NULL, returnOpts, keys[KEY_LEVEL], &valuePtr); |
| 1466 |
if (valuePtr != NULL) { |
| 1467 |
if ((TCL_ERROR == TclGetIntFromObj(NULL, valuePtr, &level)) |
| 1468 |
|| (level < 0)) { |
| 1469 |
/* |
| 1470 |
* Value is not a legal level. |
| 1471 |
*/ |
| 1472 |
|
| 1473 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1474 |
"bad -level value: expected non-negative integer but got" |
| 1475 |
" \"%s\"", TclGetString(valuePtr))); |
| 1476 |
Tcl_SetErrorCode(interp, "TCL", "RESULT", "ILLEGAL_LEVEL", (char *)NULL); |
| 1477 |
goto error; |
| 1478 |
} |
| 1479 |
Tcl_DictObjRemove(NULL, returnOpts, keys[KEY_LEVEL]); |
| 1480 |
} |
| 1481 |
|
| 1482 |
/* |
| 1483 |
* Check for bogus -errorcode value. |
| 1484 |
*/ |
| 1485 |
|
| 1486 |
Tcl_DictObjGet(NULL, returnOpts, keys[KEY_ERRORCODE], &valuePtr); |
| 1487 |
if (valuePtr != NULL) { |
| 1488 |
int length; |
| 1489 |
|
| 1490 |
if (TCL_ERROR == TclListObjLength(NULL, valuePtr, &length )) { |
| 1491 |
/* |
| 1492 |
* Value is not a list, which is illegal for -errorcode. |
| 1493 |
*/ |
| 1494 |
|
| 1495 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1496 |
"bad -errorcode value: expected a list but got \"%s\"", |
| 1497 |
TclGetString(valuePtr))); |
| 1498 |
Tcl_SetErrorCode(interp, "TCL", "RESULT", "ILLEGAL_ERRORCODE", |
| 1499 |
(char *)NULL); |
| 1500 |
goto error; |
| 1501 |
} |
| 1502 |
} |
| 1503 |
|
| 1504 |
/* |
| 1505 |
* Check for bogus -errorstack value. |
| 1506 |
*/ |
| 1507 |
|
| 1508 |
Tcl_DictObjGet(NULL, returnOpts, keys[KEY_ERRORSTACK], &valuePtr); |
| 1509 |
if (valuePtr != NULL) { |
| 1510 |
int length; |
| 1511 |
|
| 1512 |
if (TCL_ERROR == TclListObjLength(NULL, valuePtr, &length)) { |
| 1513 |
/* |
| 1514 |
* Value is not a list, which is illegal for -errorstack. |
| 1515 |
*/ |
| 1516 |
|
| 1517 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1518 |
"bad -errorstack value: expected a list but got \"%s\"", |
| 1519 |
TclGetString(valuePtr))); |
| 1520 |
Tcl_SetErrorCode(interp, "TCL", "RESULT", "NONLIST_ERRORSTACK", |
| 1521 |
(char *)NULL); |
| 1522 |
goto error; |
| 1523 |
} |
| 1524 |
if (length % 2) { |
| 1525 |
/* |
| 1526 |
* Errorstack must always be an even-sized list |
| 1527 |
*/ |
| 1528 |
|
| 1529 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1530 |
"forbidden odd-sized list for -errorstack: \"%s\"", |
| 1531 |
TclGetString(valuePtr))); |
| 1532 |
Tcl_SetErrorCode(interp, "TCL", "RESULT", |
| 1533 |
"ODDSIZEDLIST_ERRORSTACK", (char *)NULL); |
| 1534 |
goto error; |
| 1535 |
} |
| 1536 |
} |
| 1537 |
|
| 1538 |
/* |
| 1539 |
* Convert [return -code return -level X] to [return -code ok -level X+1] |
| 1540 |
*/ |
| 1541 |
|
| 1542 |
if (code == TCL_RETURN) { |
| 1543 |
level++; |
| 1544 |
code = TCL_OK; |
| 1545 |
} |
| 1546 |
|
| 1547 |
if (codePtr != NULL) { |
| 1548 |
*codePtr = code; |
| 1549 |
} |
| 1550 |
if (levelPtr != NULL) { |
| 1551 |
*levelPtr = level; |
| 1552 |
} |
| 1553 |
|
| 1554 |
if (optionsPtrPtr == NULL) { |
| 1555 |
/* |
| 1556 |
* Not passing back the options (?!), so clean them up. |
| 1557 |
*/ |
| 1558 |
|
| 1559 |
Tcl_DecrRefCount(returnOpts); |
| 1560 |
} else { |
| 1561 |
*optionsPtrPtr = returnOpts; |
| 1562 |
} |
| 1563 |
return TCL_OK; |
| 1564 |
|
| 1565 |
error: |
| 1566 |
Tcl_DecrRefCount(returnOpts); |
| 1567 |
return TCL_ERROR; |
| 1568 |
} |
| 1569 |
|
| 1570 |
/* |
| 1571 |
*------------------------------------------------------------------------- |
| 1572 |
* |
| 1573 |
* Tcl_GetReturnOptions -- |
| 1574 |
* |
| 1575 |
* Packs up the interp state into a dictionary of return options. |
| 1576 |
* |
| 1577 |
* Results: |
| 1578 |
* A dictionary of return options. |
| 1579 |
* |
| 1580 |
* Side effects: |
| 1581 |
* None. |
| 1582 |
* |
| 1583 |
*------------------------------------------------------------------------- |
| 1584 |
*/ |
| 1585 |
|
| 1586 |
Tcl_Obj * |
| 1587 |
Tcl_GetReturnOptions( |
| 1588 |
Tcl_Interp *interp, |
| 1589 |
int result) |
| 1590 |
{ |
| 1591 |
Interp *iPtr = (Interp *) interp; |
| 1592 |
Tcl_Obj *options; |
| 1593 |
Tcl_Obj **keys = GetKeys(); |
| 1594 |
|
| 1595 |
if (iPtr->returnOpts) { |
| 1596 |
options = Tcl_DuplicateObj(iPtr->returnOpts); |
| 1597 |
} else { |
| 1598 |
TclNewObj(options); |
| 1599 |
} |
| 1600 |
|
| 1601 |
if (result == TCL_RETURN) { |
| 1602 |
Tcl_DictObjPut(NULL, options, keys[KEY_CODE], |
| 1603 |
Tcl_NewIntObj(iPtr->returnCode)); |
| 1604 |
Tcl_DictObjPut(NULL, options, keys[KEY_LEVEL], |
| 1605 |
Tcl_NewIntObj(iPtr->returnLevel)); |
| 1606 |
} else { |
| 1607 |
Tcl_DictObjPut(NULL, options, keys[KEY_CODE], |
| 1608 |
Tcl_NewIntObj(result)); |
| 1609 |
Tcl_DictObjPut(NULL, options, keys[KEY_LEVEL], |
| 1610 |
Tcl_NewIntObj(0)); |
| 1611 |
} |
| 1612 |
|
| 1613 |
if (result == TCL_ERROR) { |
| 1614 |
if (!iPtr->errorInfo) { |
| 1615 |
/* |
| 1616 |
* No errorLine without errorInfo, e. g. (re)thrown only message, |
| 1617 |
* this shall also avoid transfer of errorLine (if goes to child |
| 1618 |
* interp), because we have anyway nothing excepting message |
| 1619 |
* in the backtrace. |
| 1620 |
*/ |
| 1621 |
iPtr->errorLine = 1; |
| 1622 |
} |
| 1623 |
Tcl_AddErrorInfo(interp, ""); |
| 1624 |
Tcl_DictObjPut(NULL, options, keys[KEY_ERRORSTACK], iPtr->errorStack); |
| 1625 |
} |
| 1626 |
if (iPtr->errorCode) { |
| 1627 |
Tcl_DictObjPut(NULL, options, keys[KEY_ERRORCODE], iPtr->errorCode); |
| 1628 |
} |
| 1629 |
if (iPtr->errorInfo) { |
| 1630 |
Tcl_DictObjPut(NULL, options, keys[KEY_ERRORINFO], iPtr->errorInfo); |
| 1631 |
Tcl_DictObjPut(NULL, options, keys[KEY_ERRORLINE], |
| 1632 |
Tcl_NewIntObj(iPtr->errorLine)); |
| 1633 |
} |
| 1634 |
return options; |
| 1635 |
} |
| 1636 |
|
| 1637 |
/* |
| 1638 |
*------------------------------------------------------------------------- |
| 1639 |
* |
| 1640 |
* TclNoErrorStack -- |
| 1641 |
* |
| 1642 |
* Removes the -errorstack entry from an options dict to avoid reference |
| 1643 |
* cycles. |
| 1644 |
* |
| 1645 |
* Results: |
| 1646 |
* The (unshared) argument options dict, modified in -place. |
| 1647 |
* |
| 1648 |
*------------------------------------------------------------------------- |
| 1649 |
*/ |
| 1650 |
|
| 1651 |
Tcl_Obj * |
| 1652 |
TclNoErrorStack( |
| 1653 |
Tcl_Interp *interp, |
| 1654 |
Tcl_Obj *options) |
| 1655 |
{ |
| 1656 |
Tcl_Obj **keys = GetKeys(); |
| 1657 |
|
| 1658 |
Tcl_DictObjRemove(interp, options, keys[KEY_ERRORSTACK]); |
| 1659 |
return options; |
| 1660 |
} |
| 1661 |
|
| 1662 |
/* |
| 1663 |
*------------------------------------------------------------------------- |
| 1664 |
* |
| 1665 |
* Tcl_SetReturnOptions -- |
| 1666 |
* |
| 1667 |
* Accepts an interp and a dictionary of return options, and sets the |
| 1668 |
* return options of the interp to match the dictionary. |
| 1669 |
* |
| 1670 |
* Results: |
| 1671 |
* A standard status code. Usually TCL_OK, but TCL_ERROR if an invalid |
| 1672 |
* option value was found in the dictionary. If a -level value of 0 is in |
| 1673 |
* the dictionary, then the -code value in the dictionary will be |
| 1674 |
* returned (TCL_OK default). |
| 1675 |
* |
| 1676 |
* Side effects: |
| 1677 |
* Sets the state of the interp. |
| 1678 |
* |
| 1679 |
*------------------------------------------------------------------------- |
| 1680 |
*/ |
| 1681 |
|
| 1682 |
int |
| 1683 |
Tcl_SetReturnOptions( |
| 1684 |
Tcl_Interp *interp, |
| 1685 |
Tcl_Obj *options) |
| 1686 |
{ |
| 1687 |
int objc; |
| 1688 |
int level, code; |
| 1689 |
Tcl_Obj **objv, *mergedOpts; |
| 1690 |
|
| 1691 |
Tcl_IncrRefCount(options); |
| 1692 |
if (TCL_ERROR == TclListObjGetElements(interp, options, &objc, &objv) |
| 1693 |
|| (objc % 2)) { |
| 1694 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1695 |
"expected dict but got \"%s\"", TclGetString(options))); |
| 1696 |
Tcl_SetErrorCode(interp, "TCL", "RESULT", "ILLEGAL_OPTIONS", (char *)NULL); |
| 1697 |
code = TCL_ERROR; |
| 1698 |
} else if (TCL_ERROR == TclMergeReturnOptions(interp, objc, objv, |
| 1699 |
&mergedOpts, &code, &level)) { |
| 1700 |
code = TCL_ERROR; |
| 1701 |
} else { |
| 1702 |
code = TclProcessReturn(interp, code, level, mergedOpts); |
| 1703 |
} |
| 1704 |
|
| 1705 |
Tcl_DecrRefCount(options); |
| 1706 |
return code; |
| 1707 |
} |
| 1708 |
|
| 1709 |
/* |
| 1710 |
*------------------------------------------------------------------------- |
| 1711 |
* |
| 1712 |
* Tcl_TransferResult -- |
| 1713 |
* |
| 1714 |
* Transfer the result (and error information) from one interp to another. |
| 1715 |
* Used when one interp has caused another interp to evaluate a script |
| 1716 |
* and then wants to transfer the results back to itself. |
| 1717 |
* |
| 1718 |
* Results: |
| 1719 |
* The result of targetInterp is set to the result read from sourceInterp. |
| 1720 |
* The return options dictionary of sourceInterp is transferred to |
| 1721 |
* targetInterp as appropriate for the return code value code. |
| 1722 |
* |
| 1723 |
* Side effects: |
| 1724 |
* None. |
| 1725 |
* |
| 1726 |
*------------------------------------------------------------------------- |
| 1727 |
*/ |
| 1728 |
|
| 1729 |
void |
| 1730 |
Tcl_TransferResult( |
| 1731 |
Tcl_Interp *sourceInterp, /* Interp whose result and return options |
| 1732 |
* should be moved to the target interp. |
| 1733 |
* After moving result, this interp's result |
| 1734 |
* is reset. */ |
| 1735 |
int code, /* The return code value active in |
| 1736 |
* sourceInterp. Controls how the return options |
| 1737 |
* dictionary is retrieved from sourceInterp, |
| 1738 |
* same as in Tcl_GetReturnOptions, to then be |
| 1739 |
* transferred to targetInterp. */ |
| 1740 |
Tcl_Interp *targetInterp) /* Interp where result and return options |
| 1741 |
* should be stored. If source and target are |
| 1742 |
* the same, nothing is done. */ |
| 1743 |
{ |
| 1744 |
Interp *tiPtr = (Interp *) targetInterp; |
| 1745 |
Interp *siPtr = (Interp *) sourceInterp; |
| 1746 |
|
| 1747 |
if (sourceInterp == targetInterp) { |
| 1748 |
return; |
| 1749 |
} |
| 1750 |
|
| 1751 |
if (code == TCL_OK && siPtr->returnOpts == NULL) { |
| 1752 |
/* |
| 1753 |
* Special optimization for the common case of normal command return |
| 1754 |
* code and no explicit return options. |
| 1755 |
*/ |
| 1756 |
|
| 1757 |
if (tiPtr->returnOpts) { |
| 1758 |
Tcl_DecrRefCount(tiPtr->returnOpts); |
| 1759 |
tiPtr->returnOpts = NULL; |
| 1760 |
} |
| 1761 |
} else { |
| 1762 |
Tcl_SetReturnOptions(targetInterp, |
| 1763 |
Tcl_GetReturnOptions(sourceInterp, code)); |
| 1764 |
/* |
| 1765 |
* Add line number if needed: not in line 1 and info contains no number |
| 1766 |
* yet at end of the stack (e. g. proc etc), to avoid double reporting |
| 1767 |
*/ |
| 1768 |
if (tiPtr->errorLine > 1 && tiPtr->errorInfo && |
| 1769 |
tiPtr->errorInfo->length && |
| 1770 |
tiPtr->errorInfo->bytes[tiPtr->errorInfo->length-1] != ')' |
| 1771 |
) { |
| 1772 |
Tcl_AppendObjToErrorInfo(targetInterp, Tcl_ObjPrintf( |
| 1773 |
"\n (\"interp eval\" body line %d)", tiPtr->errorLine)); |
| 1774 |
} |
| 1775 |
tiPtr->flags &= ~(ERR_ALREADY_LOGGED); |
| 1776 |
} |
| 1777 |
Tcl_SetObjResult(targetInterp, Tcl_GetObjResult(sourceInterp)); |
| 1778 |
Tcl_ResetResult(sourceInterp); |
| 1779 |
} |
| 1780 |
|
| 1781 |
/* |
| 1782 |
* Local Variables: |
| 1783 |
* mode: c |
| 1784 |
* c-basic-offset: 4 |
| 1785 |
* fill-column: 78 |
| 1786 |
* tab-width: 8 |
| 1787 |
* indent-tabs-mode: nil |
| 1788 |
* End: |
| 1789 |
*/ |