4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
|
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
|
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
* errors. */
Tcl_Token *tokenPtr, /* Pointer to first in an array of tokens to
* evaluate and concatenate. */
int count) /* Number of tokens to consider at tokenPtr.
* Must be at least 1. */
{
return TclSubstTokens(interp, tokenPtr, count, /* numLeftPtr */ NULL, 1,
NULL, NULL);
NULL, NULL, 0);
}
/*
*----------------------------------------------------------------------
*
* TclEvalScriptTokens --
*
*
* Results:
*
* Side effects:
*
* TIP #280 : Keep public API, internally extended API.
*----------------------------------------------------------------------
*/
int
TclEvalScriptTokens(
Tcl_Interp *interp,
Tcl_Token *tokenPtr,
int length,
int flags,
int line,
int* clNextOuter, /* Information about an outer context for */
const char* outerScript) /* continuation line data. This is set only in
* TclSubstTokens(), to properly handle
* [...]-nested commands. The 'outerScript'
* refers to the most-outer script containing
* the embedded command, which is refered to
* by 'script'. The 'clNextOuter' refers to
* the current entry in the table of
* continuation lines in this "master script",
* and the character offsets are relative to
* the 'outerScript' as well.
*
* If outerScript == script, then this call is
* for the outer-most script/command. See
* Tcl_EvalEx() and TclEvalObjEx() for places
* generating arguments for which this is true.
*/
{
int numCommands = tokenPtr->numComponents;
Tcl_Token *scriptTokenPtr = tokenPtr;
Interp *iPtr = (Interp *) interp;
int code = TCL_OK;
unsigned int objLength = 20;
int *expand, *expandStack, *lines, *lineSpace, *linesStack;
Tcl_Obj **objvSpace, **stackObjArray;
const char *cmdString = scriptTokenPtr->start;
int cmdSize = scriptTokenPtr->size;
CmdFrame *eeFramePtr; /* TIP #280 Structures for tracking of command
* locations. */
int allowExceptions = 1;
int *clNext = NULL; /* Pointer for the tracking of invisible
* continuation lines. Initialized only if the
* caller gave us a table of locations to
* track, via scriptCLLocPtr. It always refers
* to the table entry holding the location of
* the next invisible continuation line to
* look for, while parsing the script. */
if (iPtr->scriptCLLocPtr) {
if (clNextOuter) {
clNext = clNextOuter;
} else {
clNext = &iPtr->scriptCLLocPtr->loc[0];
}
}
if (iPtr->numLevels == 0) {
allowExceptions = iPtr->evalFlags & TCL_ALLOW_EXCEPTIONS;
}
if (length == 0) {
Tcl_Panic("EvalScriptTokens: can't eval zero tokens");
}
if (tokenPtr->type != TCL_TOKEN_SCRIPT) {
Tcl_Panic("EvalScriptTokens: invalid token array, expected script");
}
tokenPtr++; length--;
if (numCommands) {
TclAdvanceLines(&line, scriptTokenPtr->start, tokenPtr->start);
TclAdvanceContinuations(&line, &clNext, tokenPtr->start - outerScript);
}
if (length == 0) {
return TclInterpReady(interp);
}
/*
* TIP #280 Initialize tracking. Do not push on the frame stack yet.
*
* We open a new context, either for a sourced script, or 'eval'.
* For sourced files we always have a path object, even if nothing was
* specified in the interp itself. That makes code using it simpler as
* NULL checks can be left out. Sourced file without path in the
* 'scriptFile' is possible during Tcl initialization.
*/
eeFramePtr = TclStackAlloc(interp, sizeof(CmdFrame));
if (iPtr->evalFlags & TCL_EVAL_FILE) {
/*
* Set up for a sourced file.
*/
eeFramePtr->type = TCL_LOCATION_SOURCE;
if (iPtr->scriptFile) {
/*
* Normalization here, to have the correct pwd. Should have
* negligible impact on performance, as the norm should have been
* done already by the 'source' invoking us, and it caches the
* result.
*/
Tcl_Obj *norm = Tcl_FSGetNormalizedPath(interp, iPtr->scriptFile);
if (norm == NULL) {
/*
* Error message in the interp result.
*/
TclStackFree(interp, eeFramePtr);
return TCL_ERROR;
}
eeFramePtr->data.eval.path = norm;
} else {
TclNewLiteralStringObj(eeFramePtr->data.eval.path, "");
}
Tcl_IncrRefCount(eeFramePtr->data.eval.path);
} else {
/*
* Set up for plain eval.
*/
eeFramePtr->type = TCL_LOCATION_EVAL;
eeFramePtr->data.eval.path = NULL;
}
eeFramePtr->level = iPtr->cmdFramePtr ? iPtr->cmdFramePtr->level + 1 : 1;
eeFramePtr->framePtr = iPtr->framePtr;
eeFramePtr->nextPtr = iPtr->cmdFramePtr;
eeFramePtr->nline = 0;
eeFramePtr->line = NULL;
eeFramePtr->cmdObj = NULL;
iPtr->cmdFramePtr = eeFramePtr;
iPtr->evalFlags = 0;
objvSpace = stackObjArray =
TclStackAlloc(interp, objLength * sizeof(Tcl_Obj *));
expand = expandStack =
TclStackAlloc(interp, objLength * sizeof(int));
lineSpace = linesStack =
TclStackAlloc(interp, objLength * sizeof(int));
while (numCommands-- && (code == TCL_OK)) {
unsigned int objc, expandRequested = 0;
unsigned int objectsNeeded = 0;
unsigned int numWords = tokenPtr->numComponents;
Tcl_Obj **objv;
Tcl_Token *commandTokenPtr = tokenPtr;
/*
* TIP #280. Track lines within the words of the current command.
* We use a separate pointer into the table of continuation line
* locations to not lose our position for the per-command parsing.
*/
int wordLine = line;
const char *wordStart = commandTokenPtr->start;
int *wordCLNext = clNext;
if (length == 0) {
Tcl_Panic("EvalScriptTokens: overran token array");
}
if (tokenPtr->type != TCL_TOKEN_CMD) {
Tcl_Panic("EvalScriptTokens: invalid token array, expected cmd");
}
tokenPtr++; length--;
if (numWords == 0) continue;
if (numWords > objLength) {
if (expand != expandStack) {
ckfree(expand);
}
expand = ckalloc(numWords * sizeof(int));
if (objvSpace != stackObjArray) {
ckfree(objvSpace);
}
objvSpace = ckalloc(numWords * sizeof(Tcl_Obj *));
if (lineSpace != linesStack) {
ckfree(lineSpace);
}
lineSpace = ckalloc(numWords * sizeof(int));
objLength = numWords;
}
objv = objvSpace;
lines = lineSpace;
iPtr->cmdFramePtr = eeFramePtr->nextPtr;
for (objc = 0; objc < numWords;
objc++, length -= (tokenPtr->numComponents + 1),
tokenPtr += tokenPtr->numComponents+1) {
if (length == 0) {
Tcl_Panic("EvalScriptTokens: overran token array");
}
if (!(tokenPtr->type & (TCL_TOKEN_WORD
| TCL_TOKEN_SIMPLE_WORD | TCL_TOKEN_EXPAND_WORD))) {
Tcl_Panic("EvalScriptTokens: invalid token array, expected word: %d: %.*s", tokenPtr->type, tokenPtr->size, tokenPtr->start);
}
if (length < tokenPtr->numComponents + 1) {
Tcl_Panic("EvalScriptTokens: overran token array");
}
/*
* TIP #280. Track lines to current word. Save the information
* on a per-word basis, signaling dynamic words as needed.
* Make the information available to the recursively called
* evaluator as well, including the type of context (source
* vs. eval).
*/
TclAdvanceLines(&wordLine, wordStart, tokenPtr->start);
TclAdvanceContinuations (&wordLine, &wordCLNext,
tokenPtr->start - outerScript);
wordStart = tokenPtr->start;
lines[objc] = TclWordKnownAtCompileTime(tokenPtr, NULL)
? wordLine : -1;
if (eeFramePtr->type == TCL_LOCATION_SOURCE) {
iPtr->evalFlags |= TCL_EVAL_FILE;
}
code = TclSubstTokens(interp, tokenPtr+1, tokenPtr->numComponents,
NULL, wordLine, wordCLNext, outerScript, flags);
iPtr->evalFlags = 0;
if (code != TCL_OK) {
break;
}
objv[objc] = Tcl_GetObjResult(interp);
Tcl_IncrRefCount(objv[objc]);
if (tokenPtr->type == TCL_TOKEN_EXPAND_WORD) {
int numElements;
code = TclListObjLength(interp, objv[objc], &numElements);
if (code == TCL_ERROR) {
/*
* Attempt to expand a non-list
*/
Tcl_AppendObjToErrorInfo(interp, Tcl_ObjPrintf(
"\n (expanding word %d)", objc));
objc++;
break;
}
expandRequested = 1;
expand[objc] = 1;
objectsNeeded += (numElements ? numElements : 1);
} else {
expand[objc] = 0;
objectsNeeded++;
}
if (wordCLNext) {
TclContinuationsEnterDerived (objv[objc],
wordStart - outerScript, wordCLNext);
}
}
iPtr->cmdFramePtr = eeFramePtr;
if (code != TCL_OK) {
goto error;
}
if (expandRequested) {
/* Some word expansion was requested. Check for objv resize */
Tcl_Obj **copy = objvSpace;
int *lcopy = lineSpace;
int wordIdx = numWords;
int objIdx = objectsNeeded - 1;
int inPlaceCopy = 1;
if (objectsNeeded > objLength) {
inPlaceCopy = 0;
objv = objvSpace = ckalloc(objectsNeeded * sizeof(Tcl_Obj*));
lines = lineSpace = ckalloc(objectsNeeded * sizeof (int));
}
objc = 0;
while (wordIdx--) {
if (expand[wordIdx]) {
int numElements;
Tcl_Obj **elements, *temp = copy[wordIdx];
Tcl_ListObjGetElements(NULL, temp, &numElements,
&elements);
objc += numElements;
while (numElements--) {
lines[objIdx] = -1;
objv[objIdx--] = elements[numElements];
Tcl_IncrRefCount(elements[numElements]);
}
Tcl_DecrRefCount(temp);
} else {
lines[objIdx] = lcopy[wordIdx];
objv[objIdx--] = copy[wordIdx];
objc++;
}
}
objv += objIdx+1;
if (!inPlaceCopy && (copy != stackObjArray)) {
ckfree(copy);
ckfree(lcopy);
}
}
/*
* Execute the command and free the objects for its words.
*
* TIP #280: Remember the command itself for 'info frame'.
* Here is where we put our frame on the stack of frames too.
* _After_ the nested commands have been executed.
*/
eeFramePtr->cmd = commandTokenPtr->start;
eeFramePtr->len = commandTokenPtr->size;
eeFramePtr->nline = objc;
eeFramePtr->line = lines;
TclArgumentEnter(interp, objv, objc, eeFramePtr);
code = Tcl_EvalObjv(interp, objc, objv,
flags|TCL_EVAL_NOERR|TCL_EVAL_SOURCE_IN_FRAME);
TclArgumentRelease(interp, objv, objc);
eeFramePtr->line = NULL;
eeFramePtr->nline = 0;
if (eeFramePtr->cmdObj) {
Tcl_DecrRefCount(eeFramePtr->cmdObj);
eeFramePtr->cmdObj = NULL;
}
error:
while (objc > 0) {
Tcl_DecrRefCount(objv[--objc]);
}
cmdString = commandTokenPtr->start;
cmdSize = commandTokenPtr->size;
/*
* TIP #280 Track Lines. Now we track how many lines were in the
* executed command.
*/
if (numCommands) {
TclAdvanceLines(&line, commandTokenPtr->start, tokenPtr->start);
}
}
if (length && (code == TCL_OK)) {
code = TclSubstTokens(interp, tokenPtr, length, NULL, line, clNext,
outerScript, flags);
}
if ((code == TCL_ERROR) && !(iPtr->flags & ERR_ALREADY_LOGGED)) {
Tcl_LogCommandInfo(interp, scriptTokenPtr->start, cmdString, cmdSize);
}
iPtr->flags &= ~ERR_ALREADY_LOGGED;
if (lineSpace != linesStack) {
ckfree(lineSpace);
}
TclStackFree(interp, linesStack);
if (expand != expandStack) {
ckfree(expand);
}
TclStackFree(interp, expandStack);
if (objvSpace != stackObjArray) {
ckfree(objvSpace);
}
TclStackFree(interp, stackObjArray);
if (iPtr->numLevels == 0) {
if (code == TCL_RETURN) {
code = TclUpdateReturnInfo(iPtr);
}
if ((code != TCL_OK) && (code != TCL_ERROR) && !allowExceptions) {
ProcessUnexpectedResult(interp, code);
code = TCL_ERROR;
Tcl_LogCommandInfo(interp, scriptTokenPtr->start,
cmdString, cmdSize);
}
}
/*
* TIP #280. Release the local CmdFrame, and its contents.
*/
iPtr->cmdFramePtr = iPtr->cmdFramePtr->nextPtr;
if (eeFramePtr->type == TCL_LOCATION_SOURCE) {
Tcl_DecrRefCount(eeFramePtr->data.eval.path);
}
TclStackFree(interp, eeFramePtr);
return code;
}
/*
*----------------------------------------------------------------------
*
* Tcl_EvalEx, TclEvalEx --
*
|
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
|
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
|
-
-
-
-
+
+
+
+
+
-
-
-
-
-
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
+
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
+
-
-
-
-
-
-
+
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
int
TclEvalEx(
Tcl_Interp *interp, /* Interpreter in which to evaluate the
* script. Also used for error reporting. */
const char *script, /* First character of script to evaluate. */
int numBytes, /* Number of bytes in script. If < 0, the
* script consists of all bytes up to the
* first NUL character. */
int flags, /* Collection of OR-ed bits that control the
* evaluation of the script. Only
* TCL_EVAL_GLOBAL is currently supported. */
* first null character. */
int flags, /* Collection of OR-ed bits that control
* the evaluation of the script. Only
* TCL_EVAL_GLOBAL is currently
* supported. */
int line, /* The line the script starts on. */
int *clNextOuter, /* Information about an outer context for */
const char *outerScript) /* continuation line data. This is set only in
* TclSubstTokens(), to properly handle
* [...]-nested commands. The 'outerScript'
* refers to the most-outer script containing
int* clNextOuter, /* Information about an outer context for */
const char* outerScript) /* continuation line data. This is set only in
* EvalTokensStandard(), to properly handle
* [...]-nested commands. The 'outerScript'
* refers to the most-outer script containing the
* the embedded command, which is refered to
* by 'script'. The 'clNextOuter' refers to
* the current entry in the table of
* continuation lines in this "master script",
* and the character offsets are relative to
* the 'outerScript' as well.
*
* If outerScript == script, then this call is
* for the outer-most script/command. See
* Tcl_EvalEx() and TclEvalObjEx() for places
* generating arguments for which this is
* true. */
{
Interp *iPtr = (Interp *) interp;
const char *p, *next;
const unsigned int minObjs = 20;
Tcl_Obj **objv, **objvSpace;
int *expand, *lines, *lineSpace;
Tcl_Token *tokenPtr;
int commandLength, bytesLeft, expandRequested, code = TCL_OK;
CallFrame *savedVarFramePtr;/* Saves old copy of iPtr->varFramePtr in case
* TCL_EVAL_GLOBAL was set. */
int allowExceptions = (iPtr->evalFlags & TCL_ALLOW_EXCEPTIONS);
int gotParse = 0;
unsigned int i, objectsUsed = 0;
/* These variables keep track of how much
* state has been allocated while evaluating
* the script, so that it can be freed
* properly if an error occurs. */
Tcl_Parse *parsePtr = TclStackAlloc(interp, sizeof(Tcl_Parse));
CmdFrame *eeFramePtr = TclStackAlloc(interp, sizeof(CmdFrame));
Tcl_Obj **stackObjArray =
TclStackAlloc(interp, minObjs * sizeof(Tcl_Obj *));
int *expandStack = TclStackAlloc(interp, minObjs * sizeof(int));
int *linesStack = TclStackAlloc(interp, minObjs * sizeof(int));
/* TIP #280 Structures for tracking of command
* locations. */
int *clNext = NULL; /* Pointer for the tracking of invisible
* continuation lines. Initialized only if the
* caller gave us a table of locations to
* track, via scriptCLLocPtr. It always refers
* to the table entry holding the location of
* the next invisible continuation line to
* look for, while parsing the script. */
if (iPtr->scriptCLLocPtr) {
if (clNextOuter) {
clNext = clNextOuter;
} else {
clNext = &iPtr->scriptCLLocPtr->loc[0];
}
}
if (numBytes < 0) {
numBytes = strlen(script);
}
Tcl_ResetResult(interp);
savedVarFramePtr = iPtr->varFramePtr;
if (flags & TCL_EVAL_GLOBAL) {
iPtr->varFramePtr = iPtr->rootFramePtr;
}
/*
* Each iteration through the following loop parses the next command from
* the script and then executes it.
*/
objv = objvSpace = stackObjArray;
lines = lineSpace = linesStack;
expand = expandStack;
p = script;
bytesLeft = numBytes;
/*
* TIP #280 Initialize tracking. Do not push on the frame stack yet.
*
* We open a new context, either for a sourced script, or 'eval'.
* For sourced files we always have a path object, even if nothing was
* specified in the interp itself. That makes code using it simpler as
* NULL checks can be left out. Sourced file without path in the
* 'scriptFile' is possible during Tcl initialization.
*/
eeFramePtr->level = iPtr->cmdFramePtr ? iPtr->cmdFramePtr->level + 1 : 1;
eeFramePtr->framePtr = iPtr->framePtr;
eeFramePtr->nextPtr = iPtr->cmdFramePtr;
eeFramePtr->nline = 0;
eeFramePtr->line = NULL;
eeFramePtr->cmdObj = NULL;
iPtr->cmdFramePtr = eeFramePtr;
if (iPtr->evalFlags & TCL_EVAL_FILE) {
/*
* Set up for a sourced file.
*/
eeFramePtr->type = TCL_LOCATION_SOURCE;
if (iPtr->scriptFile) {
/*
* Normalization here, to have the correct pwd. Should have
* negligible impact on performance, as the norm should have been
* done already by the 'source' invoking us, and it caches the
* result.
*/
* embedded command, which is refered to by
Tcl_Obj *norm = Tcl_FSGetNormalizedPath(interp, iPtr->scriptFile);
* 'script'. The 'clNextOuter' refers to the
if (norm == NULL) {
/*
* Error message in the interp result.
*/
* current entry in the table of continuation
code = TCL_ERROR;
goto error;
}
eeFramePtr->data.eval.path = norm;
} else {
TclNewLiteralStringObj(eeFramePtr->data.eval.path, "");
}
Tcl_IncrRefCount(eeFramePtr->data.eval.path);
} else {
/*
* Set up for plain eval.
*/
* lines in this "master script", and the
eeFramePtr->type = TCL_LOCATION_EVAL;
eeFramePtr->data.eval.path = NULL;
}
* character offsets are relative to the
iPtr->evalFlags = 0;
do {
if (Tcl_ParseCommand(interp, p, bytesLeft, 0, parsePtr) != TCL_OK) {
code = TCL_ERROR;
Tcl_LogCommandInfo(interp, script, parsePtr->commandStart,
parsePtr->term + 1 - parsePtr->commandStart);
goto posterror;
}
/*
* TIP #280 Track lines. The parser may have skipped text till it
* found the command we are now at. We have to count the lines in this
* block, and do not forget invisible continuation lines.
*/
TclAdvanceLines(&line, p, parsePtr->commandStart);
TclAdvanceContinuations(&line, &clNext,
parsePtr->commandStart - outerScript);
gotParse = 1;
if (parsePtr->numWords > 0) {
/*
* TIP #280. Track lines within the words of the current
* command. We use a separate pointer into the table of
* continuation line locations to not lose our position for the
* per-command parsing.
*/
int wordLine = line;
const char *wordStart = parsePtr->commandStart;
int *wordCLNext = clNext;
unsigned int objectsNeeded = 0;
unsigned int numWords = parsePtr->numWords;
/*
* Generate an array of objects for the words of the command.
*/
if (numWords > minObjs) {
expand = ckalloc(numWords * sizeof(int));
objvSpace = ckalloc(numWords * sizeof(Tcl_Obj *));
lineSpace = ckalloc(numWords * sizeof(int));
}
expandRequested = 0;
objv = objvSpace;
lines = lineSpace;
iPtr->cmdFramePtr = eeFramePtr->nextPtr;
for (objectsUsed = 0, tokenPtr = parsePtr->tokenPtr;
objectsUsed < numWords;
objectsUsed++, tokenPtr += tokenPtr->numComponents+1) {
/*
* TIP #280. Track lines to current word. Save the information
* on a per-word basis, signaling dynamic words as needed.
* Make the information available to the recursively called
* evaluator as well, including the type of context (source
* vs. eval).
*/
TclAdvanceLines(&wordLine, wordStart, tokenPtr->start);
TclAdvanceContinuations(&wordLine, &wordCLNext,
tokenPtr->start - outerScript);
wordStart = tokenPtr->start;
lines[objectsUsed] = TclWordKnownAtCompileTime(tokenPtr, NULL)
? wordLine : -1;
if (eeFramePtr->type == TCL_LOCATION_SOURCE) {
iPtr->evalFlags |= TCL_EVAL_FILE;
}
code = TclSubstTokens(interp, tokenPtr+1,
tokenPtr->numComponents, NULL, wordLine,
wordCLNext, outerScript);
* 'outerScript' as well.
iPtr->evalFlags = 0;
if (code != TCL_OK) {
break;
}
objv[objectsUsed] = Tcl_GetObjResult(interp);
Tcl_IncrRefCount(objv[objectsUsed]);
if (tokenPtr->type == TCL_TOKEN_EXPAND_WORD) {
int numElements;
*
code = TclListObjLength(interp, objv[objectsUsed],
&numElements);
if (code == TCL_ERROR) {
/*
* Attempt to expand a non-list.
*/
* If outerScript == script, then this call is
Tcl_AppendObjToErrorInfo(interp, Tcl_ObjPrintf(
"\n (expanding word %d)", objectsUsed));
Tcl_DecrRefCount(objv[objectsUsed]);
break;
}
expandRequested = 1;
expand[objectsUsed] = 1;
* for the outer-most script/command. See
objectsNeeded += (numElements ? numElements : 1);
} else {
expand[objectsUsed] = 0;
objectsNeeded++;
}
* Tcl_EvalEx() and TclEvalObjEx() for places
if (wordCLNext) {
TclContinuationsEnterDerived(objv[objectsUsed],
wordStart - outerScript, wordCLNext);
}
} /* for loop */
iPtr->cmdFramePtr = eeFramePtr;
if (code != TCL_OK) {
goto error;
}
if (expandRequested) {
/*
* Some word expansion was requested. Check for objv resize.
*/
* generating arguments for which this is true.
Tcl_Obj **copy = objvSpace;
int *lcopy = lineSpace;
int wordIdx = numWords;
int objIdx = objectsNeeded - 1;
if ((numWords > minObjs) || (objectsNeeded > minObjs)) {
objv = objvSpace =
ckalloc(objectsNeeded * sizeof(Tcl_Obj *));
lines = lineSpace = ckalloc(objectsNeeded * sizeof(int));
}
objectsUsed = 0;
while (wordIdx--) {
if (expand[wordIdx]) {
int numElements;
Tcl_Obj **elements, *temp = copy[wordIdx];
Tcl_ListObjGetElements(NULL, temp, &numElements,
&elements);
objectsUsed += numElements;
while (numElements--) {
lines[objIdx] = -1;
objv[objIdx--] = elements[numElements];
Tcl_IncrRefCount(elements[numElements]);
}
Tcl_DecrRefCount(temp);
} else {
lines[objIdx] = lcopy[wordIdx];
objv[objIdx--] = copy[wordIdx];
objectsUsed++;
}
}
objv += objIdx+1;
if (copy != stackObjArray) {
ckfree(copy);
}
if (lcopy != linesStack) {
ckfree(lcopy);
}
}
/*
* Execute the command and free the objects for its words.
*
* TIP #280: Remember the command itself for 'info frame'. We
* shorten the visible command by one char to exclude the
* termination character, if necessary. Here is where we put our
* frame on the stack of frames too. _After_ the nested commands
* have been executed.
*/
*/
eeFramePtr->cmd = parsePtr->commandStart;
eeFramePtr->len = parsePtr->commandSize;
if (parsePtr->term ==
parsePtr->commandStart + parsePtr->commandSize - 1) {
eeFramePtr->len--;
}
eeFramePtr->nline = objectsUsed;
eeFramePtr->line = lines;
TclArgumentEnter(interp, objv, objectsUsed, eeFramePtr);
code = Tcl_EvalObjv(interp, objectsUsed, objv,
TCL_EVAL_NOERR | TCL_EVAL_SOURCE_IN_FRAME);
TclArgumentRelease(interp, objv, objectsUsed);
eeFramePtr->line = NULL;
eeFramePtr->nline = 0;
if (eeFramePtr->cmdObj) {
Tcl_DecrRefCount(eeFramePtr->cmdObj);
eeFramePtr->cmdObj = NULL;
}
{
if (code != TCL_OK) {
goto error;
}
for (i = 0; i < objectsUsed; i++) {
Tcl_DecrRefCount(objv[i]);
}
objectsUsed = 0;
if (objvSpace != stackObjArray) {
ckfree(objvSpace);
objvSpace = stackObjArray;
ckfree(lineSpace);
lineSpace = linesStack;
}
Tcl_Token *lastTokenPtr, *tokensPtr = TclParseScript(interp,
/*
* Free expand separately since objvSpace could have been
* reallocated above.
*/
script, numBytes, /* flags */ 0, &lastTokenPtr, NULL);
if (expand != expandStack) {
ckfree(expand);
expand = expandStack;
}
}
int code = TclEvalScriptTokens(interp, tokensPtr,
/*
* Advance to the next command in the script.
*
* TIP #280 Track Lines. Now we track how many lines were in the
* executed command.
*/
1 + (int)(lastTokenPtr - tokensPtr), flags, line,
next = parsePtr->commandStart + parsePtr->commandSize;
bytesLeft -= next - p;
p = next;
TclAdvanceLines(&line, parsePtr->commandStart, p);
Tcl_FreeParse(parsePtr);
gotParse = 0;
} while (bytesLeft > 0);
iPtr->varFramePtr = savedVarFramePtr;
code = TCL_OK;
goto cleanup_return;
clNextOuter, outerScript);
error:
/*
* Generate and log various pieces of error information.
*/
if (iPtr->numLevels == 0) {
if (code == TCL_RETURN) {
code = TclUpdateReturnInfo(iPtr);
}
if ((code != TCL_OK) && (code != TCL_ERROR) && !allowExceptions) {
ProcessUnexpectedResult(interp, code);
code = TCL_ERROR;
}
}
if ((code == TCL_ERROR) && !(iPtr->flags & ERR_ALREADY_LOGGED)) {
commandLength = parsePtr->commandSize;
if (parsePtr->term == parsePtr->commandStart + commandLength - 1) {
/*
* The terminator character (such as ; or ]) of the command where
* the error occurred is the last character in the parsed command.
* Reduce the length by one so that the error message doesn't
* include the terminator character.
*/
commandLength -= 1;
}
Tcl_LogCommandInfo(interp, script, parsePtr->commandStart,
commandLength);
}
posterror:
iPtr->flags &= ~ERR_ALREADY_LOGGED;
/*
* Then free resources that had been allocated to the command.
*/
for (i = 0; i < objectsUsed; i++) {
Tcl_DecrRefCount(objv[i]);
}
if (gotParse) {
Tcl_FreeParse(parsePtr);
}
if (objvSpace != stackObjArray) {
ckfree(objvSpace);
ckfree(lineSpace);
}
if (expand != expandStack) {
ckfree(expand);
ckfree(tokensPtr);
}
iPtr->varFramePtr = savedVarFramePtr;
cleanup_return:
/*
* TIP #280. Release the local CmdFrame, and its contents.
*/
iPtr->cmdFramePtr = iPtr->cmdFramePtr->nextPtr;
if (eeFramePtr->type == TCL_LOCATION_SOURCE) {
Tcl_DecrRefCount(eeFramePtr->data.eval.path);
}
TclStackFree(interp, linesStack);
TclStackFree(interp, expandStack);
TclStackFree(interp, stackObjArray);
TclStackFree(interp, eeFramePtr);
TclStackFree(interp, parsePtr);
return code;
}
/*
*----------------------------------------------------------------------
*
* TclAdvanceLines --
|
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
|
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
|
-
+
-
-
+
-
-
+
+
+
+
-
+
+
|
{
/*
* We're not supposed to use the compiler or byte-code
* interpreter. Let Tcl_EvalEx evaluate the command directly (and
* probably more slowly).
*/
Tcl_Token *lastTokenPtr, *tokensPtr;
const char *script;
int numSrcBytes;
/*
* Now we check if we have data about invisible continuation lines for
* the script, and make it available to the direct script parser and
* evaluator we are about to call, if so.
*
* It may be possible that the script Tcl_Obj* can be free'd while the
* evaluator is using it, leading to the release of the associated
* ContLineLoc structure as well. To ensure that the latter doesn't
* happen we set a lock on it. We release this lock later in this
* function, after the evaluator is done. The relevant "lineCLPtr"
* hashtable is managed in the file "tclObj.c".
*
* Another important action is to save (and later restore) the
* continuation line information of the caller, in case we are
* executing nested commands in the eval/direct path.
*/
ContLineLoc *saveCLLocPtr = iPtr->scriptCLLocPtr;
Tcl_Obj *copyPtr = TclTokensCopy(objPtr);
assert(invoker == NULL);
iPtr->scriptCLLocPtr = TclContinuationsGet(objPtr);
Tcl_IncrRefCount(objPtr);
script = TclGetStringFromObj(objPtr, &numSrcBytes);
result = Tcl_EvalEx(interp, script, numSrcBytes, flags);
tokensPtr = TclGetTokensFromObj(copyPtr, &lastTokenPtr);
result = TclEvalScriptTokens(interp, tokensPtr,
1 + (int)(lastTokenPtr - tokensPtr), flags, 1, NULL,
tokensPtr[0].start);
TclDecrRefCount(objPtr);
Tcl_DecrRefCount(objPtr);
Tcl_DecrRefCount(copyPtr);
iPtr->scriptCLLocPtr = saveCLLocPtr;
return result;
}
}
static int
|