Fossil SCM
Trim trailing whitespace in TH1 and align some comments.
Commit
5a9f9ba61dd6a36ada37347612e10aab4db5860b
Parent
cde4759d5e8c30d…
3 files changed
+126
-126
+22
-22
+113
-113
M
src/th.c
+126
-126
| --- src/th.c | ||
| +++ src/th.c | ||
| @@ -1,8 +1,8 @@ | ||
| 1 | 1 | |
| 2 | 2 | /* |
| 3 | -** The implementation of the TH core. This file contains the parser, and | |
| 3 | +** The implementation of the TH core. This file contains the parser, and | |
| 4 | 4 | ** the implementation of the interface in th.h. |
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | 7 | #include "config.h" |
| 8 | 8 | #include "th.h" |
| @@ -16,11 +16,11 @@ | ||
| 16 | 16 | /* |
| 17 | 17 | ** Interpreter structure. |
| 18 | 18 | */ |
| 19 | 19 | struct Th_Interp { |
| 20 | 20 | Th_Vtab *pVtab; /* Copy of the argument passed to Th_CreateInterp() */ |
| 21 | - char *zResult; /* Current interpreter result (Th_Malloc()ed) */ | |
| 21 | + char *zResult; /* Current interpreter result (Th_Malloc()ed) */ | |
| 22 | 22 | int nResult; /* number of bytes in zResult */ |
| 23 | 23 | Th_Hash *paCmd; /* Table of registered commands */ |
| 24 | 24 | Th_Frame *pFrame; /* Current execution frame */ |
| 25 | 25 | int isListMode; /* True if thSplitList() should operate in "list" mode */ |
| 26 | 26 | }; |
| @@ -42,25 +42,25 @@ | ||
| 42 | 42 | ** are stored in the Th_Frame.paVar hash table member of the associated |
| 43 | 43 | ** stack frame object. |
| 44 | 44 | ** |
| 45 | 45 | ** When an interpreter is created, a single Th_Frame structure is also |
| 46 | 46 | ** allocated - the global variable scope. Th_Interp.pFrame (the current |
| 47 | -** interpreter frame) is initialised to point to this Th_Frame. It is | |
| 48 | -** not deleted for the lifetime of the interpreter (because the global | |
| 47 | +** interpreter frame) is initialised to point to this Th_Frame. It is | |
| 48 | +** not deleted for the lifetime of the interpreter (because the global | |
| 49 | 49 | ** frame never goes out of scope). |
| 50 | 50 | ** |
| 51 | 51 | ** New stack frames are created by the Th_InFrame() function. Before |
| 52 | 52 | ** invoking its callback function, Th_InFrame() allocates a new Th_Frame |
| 53 | 53 | ** structure with pCaller set to the current frame (Th_Interp.pFrame), |
| 54 | 54 | ** and sets the current frame to the new frame object. After the callback |
| 55 | 55 | ** has been invoked, the allocated Th_Frame is deleted and the value |
| 56 | 56 | ** of the current frame pointer restored. |
| 57 | -** | |
| 58 | -** By default, the Th_SetVar(), Th_UnsetVar() and Th_GetVar() functions | |
| 59 | -** access variable values in the current frame. If they need to access | |
| 57 | +** | |
| 58 | +** By default, the Th_SetVar(), Th_UnsetVar() and Th_GetVar() functions | |
| 59 | +** access variable values in the current frame. If they need to access | |
| 60 | 60 | ** the global frame, they do so by traversing the pCaller pointer list. |
| 61 | -** Likewise, the Th_LinkVar() function uses the pCaller pointers to | |
| 61 | +** Likewise, the Th_LinkVar() function uses the pCaller pointers to | |
| 62 | 62 | ** link to variables located in the global or other stack frames. |
| 63 | 63 | */ |
| 64 | 64 | struct Th_Frame { |
| 65 | 65 | Th_Hash *paVar; /* Variables defined in this scope */ |
| 66 | 66 | Th_Frame *pCaller; /* Calling frame */ |
| @@ -84,11 +84,11 @@ | ||
| 84 | 84 | ** value. |
| 85 | 85 | */ |
| 86 | 86 | struct Th_Variable { |
| 87 | 87 | int nRef; /* Number of references to this structure */ |
| 88 | 88 | int nData; /* Number of bytes at Th_Variable.zData */ |
| 89 | - char *zData; /* Data for scalar variables */ | |
| 89 | + char *zData; /* Data for scalar variables */ | |
| 90 | 90 | Th_Hash *pHash; /* Data for array variables */ |
| 91 | 91 | }; |
| 92 | 92 | |
| 93 | 93 | /* |
| 94 | 94 | ** Hash table API: |
| @@ -110,19 +110,19 @@ | ||
| 110 | 110 | static void thFreeVariable(Th_HashEntry*, void*); |
| 111 | 111 | static void thFreeCommand(Th_HashEntry*, void*); |
| 112 | 112 | |
| 113 | 113 | /* |
| 114 | 114 | ** The following are used by both the expression and language parsers. |
| 115 | -** Given that the start of the input string (z, n) is a language | |
| 115 | +** Given that the start of the input string (z, n) is a language | |
| 116 | 116 | ** construct of the relevant type (a command enclosed in [], an escape |
| 117 | 117 | ** sequence etc.), these functions determine the number of bytes |
| 118 | 118 | ** of the input consumed by the construct. For example: |
| 119 | 119 | ** |
| 120 | 120 | ** int nByte; |
| 121 | 121 | ** thNextCommand(interp, "[expr $a+1] $nIter", 18, &nByte); |
| 122 | 122 | ** |
| 123 | -** results in variable nByte being set to 11. Or, | |
| 123 | +** results in variable nByte being set to 11. Or, | |
| 124 | 124 | ** |
| 125 | 125 | ** thNextVarname(interp, "$a+1", 4, &nByte); |
| 126 | 126 | ** |
| 127 | 127 | ** results in nByte being set to 2. |
| 128 | 128 | */ |
| @@ -132,24 +132,24 @@ | ||
| 132 | 132 | static int thNextNumber (Th_Interp*, const char *z, int n, int *pN); |
| 133 | 133 | static int thNextSpace (Th_Interp*, const char *z, int n, int *pN); |
| 134 | 134 | |
| 135 | 135 | /* |
| 136 | 136 | ** Given that the input string (z, n) contains a language construct of |
| 137 | -** the relevant type (a command enclosed in [], an escape sequence | |
| 137 | +** the relevant type (a command enclosed in [], an escape sequence | |
| 138 | 138 | ** like "\xFF" or a variable reference like "${varname}", perform |
| 139 | 139 | ** substitution on the string and store the resulting string in |
| 140 | 140 | ** the interpreter result. |
| 141 | 141 | */ |
| 142 | 142 | static int thSubstCommand(Th_Interp*, const char *z, int n); |
| 143 | 143 | static int thSubstEscape (Th_Interp*, const char *z, int n); |
| 144 | 144 | static int thSubstVarname(Th_Interp*, const char *z, int n); |
| 145 | 145 | |
| 146 | 146 | /* |
| 147 | -** Given that there is a th1 word located at the start of the input | |
| 147 | +** Given that there is a th1 word located at the start of the input | |
| 148 | 148 | ** string (z, n), determine the length in bytes of that word. If the |
| 149 | -** isCmd argument is non-zero, then an unescaped ";" byte not | |
| 150 | -** located inside of a block or quoted string is considered to mark | |
| 149 | +** isCmd argument is non-zero, then an unescaped ";" byte not | |
| 150 | +** located inside of a block or quoted string is considered to mark | |
| 151 | 151 | ** the end of the word. |
| 152 | 152 | */ |
| 153 | 153 | static int thNextWord(Th_Interp*, const char *z, int n, int *pN, int isCmd); |
| 154 | 154 | |
| 155 | 155 | /* |
| @@ -176,13 +176,13 @@ | ||
| 176 | 176 | ** Append nAdd bytes of content copied from zAdd to the end of buffer |
| 177 | 177 | ** pBuffer. If there is not enough space currently allocated, resize |
| 178 | 178 | ** the allocation to make space. |
| 179 | 179 | */ |
| 180 | 180 | static int thBufferWrite( |
| 181 | - Th_Interp *interp, | |
| 182 | - Buffer *pBuffer, | |
| 183 | - const char *zAdd, | |
| 181 | + Th_Interp *interp, | |
| 182 | + Buffer *pBuffer, | |
| 183 | + const char *zAdd, | |
| 184 | 184 | int nAdd |
| 185 | 185 | ){ |
| 186 | 186 | int nReq; |
| 187 | 187 | |
| 188 | 188 | if( nAdd<0 ){ |
| @@ -311,19 +311,19 @@ | ||
| 311 | 311 | Th_HashDelete(interp, pFrame->paVar); |
| 312 | 312 | interp->pFrame = pFrame->pCaller; |
| 313 | 313 | } |
| 314 | 314 | |
| 315 | 315 | /* |
| 316 | -** The first part of the string (zInput,nInput) contains an escape | |
| 316 | +** The first part of the string (zInput,nInput) contains an escape | |
| 317 | 317 | ** sequence. Set *pnEscape to the number of bytes in the escape sequence. |
| 318 | 318 | ** If there is a parse error, return TH_ERROR and set the interpreter |
| 319 | 319 | ** result to an error message. Otherwise return TH_OK. |
| 320 | 320 | */ |
| 321 | 321 | static int thNextEscape( |
| 322 | 322 | Th_Interp *interp, |
| 323 | - const char *zInput, | |
| 324 | - int nInput, | |
| 323 | + const char *zInput, | |
| 324 | + int nInput, | |
| 325 | 325 | int *pnEscape |
| 326 | 326 | ){ |
| 327 | 327 | int i = 2; |
| 328 | 328 | |
| 329 | 329 | assert(nInput>0); |
| @@ -344,18 +344,18 @@ | ||
| 344 | 344 | return TH_OK; |
| 345 | 345 | } |
| 346 | 346 | |
| 347 | 347 | /* |
| 348 | 348 | ** The first part of the string (zInput,nInput) contains a variable |
| 349 | -** reference. Set *pnVarname to the number of bytes in the variable | |
| 350 | -** reference. If there is a parse error, return TH_ERROR and set the | |
| 349 | +** reference. Set *pnVarname to the number of bytes in the variable | |
| 350 | +** reference. If there is a parse error, return TH_ERROR and set the | |
| 351 | 351 | ** interpreter result to an error message. Otherwise return TH_OK. |
| 352 | 352 | */ |
| 353 | 353 | int thNextVarname( |
| 354 | 354 | Th_Interp *interp, |
| 355 | - const char *zInput, | |
| 356 | - int nInput, | |
| 355 | + const char *zInput, | |
| 356 | + int nInput, | |
| 357 | 357 | int *pnVarname |
| 358 | 358 | ){ |
| 359 | 359 | int i; |
| 360 | 360 | |
| 361 | 361 | assert(nInput>0); |
| @@ -401,19 +401,19 @@ | ||
| 401 | 401 | return TH_OK; |
| 402 | 402 | } |
| 403 | 403 | |
| 404 | 404 | /* |
| 405 | 405 | ** The first part of the string (zInput,nInput) contains a command |
| 406 | -** enclosed in a "[]" block. Set *pnCommand to the number of bytes in | |
| 407 | -** the variable reference. If there is a parse error, return TH_ERROR | |
| 408 | -** and set the interpreter result to an error message. Otherwise return | |
| 406 | +** enclosed in a "[]" block. Set *pnCommand to the number of bytes in | |
| 407 | +** the variable reference. If there is a parse error, return TH_ERROR | |
| 408 | +** and set the interpreter result to an error message. Otherwise return | |
| 409 | 409 | ** TH_OK. |
| 410 | 410 | */ |
| 411 | 411 | int thNextCommand( |
| 412 | 412 | Th_Interp *interp, |
| 413 | - const char *zInput, | |
| 414 | - int nInput, | |
| 413 | + const char *zInput, | |
| 414 | + int nInput, | |
| 415 | 415 | int *pnCommand |
| 416 | 416 | ){ |
| 417 | 417 | int nBrace = 0; |
| 418 | 418 | int nSquare = 0; |
| 419 | 419 | int i; |
| @@ -438,17 +438,17 @@ | ||
| 438 | 438 | |
| 439 | 439 | return TH_OK; |
| 440 | 440 | } |
| 441 | 441 | |
| 442 | 442 | /* |
| 443 | -** Set *pnSpace to the number of whitespace bytes at the start of | |
| 443 | +** Set *pnSpace to the number of whitespace bytes at the start of | |
| 444 | 444 | ** input string (zInput, nInput). Always return TH_OK. |
| 445 | 445 | */ |
| 446 | 446 | int thNextSpace( |
| 447 | 447 | Th_Interp *interp, |
| 448 | - const char *zInput, | |
| 449 | - int nInput, | |
| 448 | + const char *zInput, | |
| 449 | + int nInput, | |
| 450 | 450 | int *pnSpace |
| 451 | 451 | ){ |
| 452 | 452 | int i; |
| 453 | 453 | for(i=0; i<nInput && th_isspace(zInput[i]); i++); |
| 454 | 454 | *pnSpace = i; |
| @@ -457,21 +457,21 @@ | ||
| 457 | 457 | |
| 458 | 458 | /* |
| 459 | 459 | ** The first byte of the string (zInput,nInput) is not white-space. |
| 460 | 460 | ** Set *pnWord to the number of bytes in the th1 word that starts |
| 461 | 461 | ** with this byte. If a complete word cannot be parsed or some other |
| 462 | -** error occurs, return TH_ERROR and set the interpreter result to | |
| 462 | +** error occurs, return TH_ERROR and set the interpreter result to | |
| 463 | 463 | ** an error message. Otherwise return TH_OK. |
| 464 | 464 | ** |
| 465 | -** If the isCmd argument is non-zero, then an unescaped ";" byte not | |
| 466 | -** located inside of a block or quoted string is considered to mark | |
| 465 | +** If the isCmd argument is non-zero, then an unescaped ";" byte not | |
| 466 | +** located inside of a block or quoted string is considered to mark | |
| 467 | 467 | ** the end of the word. |
| 468 | 468 | */ |
| 469 | 469 | static int thNextWord( |
| 470 | 470 | Th_Interp *interp, |
| 471 | - const char *zInput, | |
| 472 | - int nInput, | |
| 471 | + const char *zInput, | |
| 472 | + int nInput, | |
| 473 | 473 | int *pnWord, |
| 474 | 474 | int isCmd |
| 475 | 475 | ){ |
| 476 | 476 | int iEnd = 0; |
| 477 | 477 | |
| @@ -531,12 +531,12 @@ | ||
| 531 | 531 | return thEvalLocal(interp, &zWord[1], nWord-2); |
| 532 | 532 | } |
| 533 | 533 | |
| 534 | 534 | /* |
| 535 | 535 | ** The input string (zWord, nWord) contains a th1 variable reference |
| 536 | -** (a '$' byte followed by a variable name). Perform substitution on | |
| 537 | -** the input string and store the resulting string in the interpreter | |
| 536 | +** (a '$' byte followed by a variable name). Perform substitution on | |
| 537 | +** the input string and store the resulting string in the interpreter | |
| 538 | 538 | ** result. |
| 539 | 539 | */ |
| 540 | 540 | static int thSubstVarname( |
| 541 | 541 | Th_Interp *interp, |
| 542 | 542 | const char *zWord, |
| @@ -572,11 +572,11 @@ | ||
| 572 | 572 | return Th_GetVar(interp, &zWord[1], nWord-1); |
| 573 | 573 | } |
| 574 | 574 | |
| 575 | 575 | /* |
| 576 | 576 | ** The input string (zWord, nWord) contains a th1 escape sequence. |
| 577 | -** Perform substitution on the input string and store the resulting | |
| 577 | +** Perform substitution on the input string and store the resulting | |
| 578 | 578 | ** string in the interpreter result. |
| 579 | 579 | */ |
| 580 | 580 | static int thSubstEscape( |
| 581 | 581 | Th_Interp *interp, |
| 582 | 582 | const char *zWord, |
| @@ -608,11 +608,11 @@ | ||
| 608 | 608 | return TH_OK; |
| 609 | 609 | } |
| 610 | 610 | |
| 611 | 611 | /* |
| 612 | 612 | ** The input string (zWord, nWord) contains a th1 word. Perform |
| 613 | -** substitution on the input string and store the resulting | |
| 613 | +** substitution on the input string and store the resulting | |
| 614 | 614 | ** string in the interpreter result. |
| 615 | 615 | */ |
| 616 | 616 | static int thSubstWord( |
| 617 | 617 | Th_Interp *interp, |
| 618 | 618 | const char *zWord, |
| @@ -640,20 +640,20 @@ | ||
| 640 | 640 | int (*xGet)(Th_Interp *, const char*, int, int *) = 0; |
| 641 | 641 | int (*xSubst)(Th_Interp *, const char*, int) = 0; |
| 642 | 642 | |
| 643 | 643 | switch( zWord[i] ){ |
| 644 | 644 | case '\\': |
| 645 | - xGet = thNextEscape; xSubst = thSubstEscape; | |
| 645 | + xGet = thNextEscape; xSubst = thSubstEscape; | |
| 646 | 646 | break; |
| 647 | 647 | case '[': |
| 648 | 648 | if( !interp->isListMode ){ |
| 649 | - xGet = thNextCommand; xSubst = thSubstCommand; | |
| 649 | + xGet = thNextCommand; xSubst = thSubstCommand; | |
| 650 | 650 | break; |
| 651 | 651 | } |
| 652 | 652 | case '$': |
| 653 | 653 | if( !interp->isListMode ){ |
| 654 | - xGet = thNextVarname; xSubst = thSubstVarname; | |
| 654 | + xGet = thNextVarname; xSubst = thSubstVarname; | |
| 655 | 655 | break; |
| 656 | 656 | } |
| 657 | 657 | default: { |
| 658 | 658 | thBufferWrite(interp, &output, &zWord[i], 1); |
| 659 | 659 | continue; /* Go to the next iteration of the for(...) loop */ |
| @@ -685,11 +685,11 @@ | ||
| 685 | 685 | ** Return true if one of the following is true of the buffer pointed |
| 686 | 686 | ** to by zInput, length nInput: |
| 687 | 687 | ** |
| 688 | 688 | ** + It is empty, or |
| 689 | 689 | ** + It contains nothing but white-space, or |
| 690 | -** + It contains no non-white-space characters before the first | |
| 690 | +** + It contains no non-white-space characters before the first | |
| 691 | 691 | ** newline character. |
| 692 | 692 | ** |
| 693 | 693 | ** Otherwise return false. |
| 694 | 694 | */ |
| 695 | 695 | static int thEndOfLine(const char *zInput, int nInput){ |
| @@ -725,16 +725,16 @@ | ||
| 725 | 725 | ** // Free all memory allocated by Th_SplitList(). The arrays pointed |
| 726 | 726 | ** // to by argv and argl are invalidated by this call. |
| 727 | 727 | ** // |
| 728 | 728 | ** Th_Free(interp, argv); |
| 729 | 729 | ** |
| 730 | -*/ | |
| 730 | +*/ | |
| 731 | 731 | static int thSplitList( |
| 732 | 732 | Th_Interp *interp, /* Interpreter context */ |
| 733 | - const char *zList, /* Pointer to buffer containing input list */ | |
| 733 | + const char *zList, /* Pointer to buffer containing input list */ | |
| 734 | 734 | int nList, /* Size of buffer pointed to by zList */ |
| 735 | - char ***pazElem, /* OUT: Array of list elements */ | |
| 735 | + char ***pazElem, /* OUT: Array of list elements */ | |
| 736 | 736 | int **panElem, /* OUT: Lengths of each list element */ |
| 737 | 737 | int *pnCount /* OUT: Number of list elements */ |
| 738 | 738 | ){ |
| 739 | 739 | int rc = TH_OK; |
| 740 | 740 | |
| @@ -774,14 +774,14 @@ | ||
| 774 | 774 | assert((lenbuf.nBuf/sizeof(int))==nCount); |
| 775 | 775 | |
| 776 | 776 | assert((pazElem && panElem) || (!pazElem && !panElem)); |
| 777 | 777 | if( pazElem && rc==TH_OK ){ |
| 778 | 778 | int i; |
| 779 | - char *zElem; | |
| 779 | + char *zElem; | |
| 780 | 780 | int *anElem; |
| 781 | 781 | char **azElem = Th_Malloc(interp, |
| 782 | - sizeof(char*) * nCount + /* azElem */ | |
| 782 | + sizeof(char*) * nCount + /* azElem */ | |
| 783 | 783 | sizeof(int) * nCount + /* anElem */ |
| 784 | 784 | strbuf.nBuf /* space for list element strings */ |
| 785 | 785 | ); |
| 786 | 786 | anElem = (int *)&azElem[nCount]; |
| 787 | 787 | zElem = (char *)&anElem[nCount]; |
| @@ -795,11 +795,11 @@ | ||
| 795 | 795 | *panElem = anElem; |
| 796 | 796 | } |
| 797 | 797 | if( pnCount ){ |
| 798 | 798 | *pnCount = nCount; |
| 799 | 799 | } |
| 800 | - | |
| 800 | + | |
| 801 | 801 | finish: |
| 802 | 802 | thBufferFree(interp, &strbuf); |
| 803 | 803 | thBufferFree(interp, &lenbuf); |
| 804 | 804 | return rc; |
| 805 | 805 | } |
| @@ -876,18 +876,18 @@ | ||
| 876 | 876 | if( rc==TH_OK ){ |
| 877 | 877 | Th_Command *p = (Th_Command *)(pEntry->pData); |
| 878 | 878 | const char **azArg = (const char **)argv; |
| 879 | 879 | rc = p->xProc(interp, p->pContext, argc, azArg, argl); |
| 880 | 880 | } |
| 881 | - | |
| 881 | + | |
| 882 | 882 | /* If an error occurred, add this command to the stack trace report. */ |
| 883 | 883 | if( rc==TH_ERROR ){ |
| 884 | 884 | char *zRes; |
| 885 | 885 | int nRes; |
| 886 | 886 | char *zStack = 0; |
| 887 | 887 | int nStack = 0; |
| 888 | - | |
| 888 | + | |
| 889 | 889 | zRes = Th_TakeResult(interp, &nRes); |
| 890 | 890 | if( TH_OK==Th_GetVar(interp, (char *)"::th_stack_trace", -1) ){ |
| 891 | 891 | zStack = Th_TakeResult(interp, &nStack); |
| 892 | 892 | } |
| 893 | 893 | Th_ListAppend(interp, &zStack, &nStack, zFirst, zInput-zFirst); |
| @@ -912,15 +912,15 @@ | ||
| 912 | 912 | ** |
| 913 | 913 | ** Argument iFrame is interpreted as follows: |
| 914 | 914 | ** |
| 915 | 915 | ** * If iFrame is 0, this means the current frame. |
| 916 | 916 | ** |
| 917 | -** * If iFrame is negative, then the nth frame up the stack, where | |
| 918 | -** n is the absolute value of iFrame. A value of -1 means the | |
| 917 | +** * If iFrame is negative, then the nth frame up the stack, where | |
| 918 | +** n is the absolute value of iFrame. A value of -1 means the | |
| 919 | 919 | ** calling procedure. |
| 920 | 920 | ** |
| 921 | -** * If iFrame is +ve, then the nth frame from the bottom of the | |
| 921 | +** * If iFrame is +ve, then the nth frame from the bottom of the | |
| 922 | 922 | ** stack. An iFrame value of 1 means the toplevel (global) frame. |
| 923 | 923 | */ |
| 924 | 924 | static Th_Frame *getFrame(Th_Interp *interp, int iFrame){ |
| 925 | 925 | Th_Frame *p = interp->pFrame; |
| 926 | 926 | int i; |
| @@ -948,28 +948,28 @@ | ||
| 948 | 948 | |
| 949 | 949 | |
| 950 | 950 | /* |
| 951 | 951 | ** Evaluate th1 script (zProgram, nProgram) in the frame identified by |
| 952 | 952 | ** argument iFrame. Leave either an error message or a result in the |
| 953 | -** interpreter result and return a th1 error code (TH_OK, TH_ERROR, | |
| 953 | +** interpreter result and return a th1 error code (TH_OK, TH_ERROR, | |
| 954 | 954 | ** TH_RETURN, TH_CONTINUE or TH_BREAK). |
| 955 | 955 | */ |
| 956 | 956 | int Th_Eval(Th_Interp *interp, int iFrame, const char *zProgram, int nProgram){ |
| 957 | 957 | int rc = TH_OK; |
| 958 | 958 | Th_Frame *pSavedFrame = interp->pFrame; |
| 959 | 959 | |
| 960 | - /* Set Th_Interp.pFrame to the frame that this script is to be | |
| 960 | + /* Set Th_Interp.pFrame to the frame that this script is to be | |
| 961 | 961 | ** evaluated in. The current frame is saved in pSavedFrame and will |
| 962 | 962 | ** be restored before this function returns. |
| 963 | 963 | */ |
| 964 | 964 | interp->pFrame = getFrame(interp, iFrame); |
| 965 | 965 | |
| 966 | 966 | if( !interp->pFrame ){ |
| 967 | 967 | rc = TH_ERROR; |
| 968 | 968 | }else{ |
| 969 | 969 | int nInput = nProgram; |
| 970 | - | |
| 970 | + | |
| 971 | 971 | if( nInput<0 ){ |
| 972 | 972 | nInput = th_strlen(zProgram); |
| 973 | 973 | } |
| 974 | 974 | rc = thEvalLocal(interp, zProgram, nInput); |
| 975 | 975 | } |
| @@ -995,13 +995,13 @@ | ||
| 995 | 995 | ** array key name. |
| 996 | 996 | */ |
| 997 | 997 | static int thAnalyseVarname( |
| 998 | 998 | const char *zVarname, |
| 999 | 999 | int nVarname, |
| 1000 | - const char **pzOuter, /* OUT: Pointer to scalar/array name */ | |
| 1000 | + const char **pzOuter, /* OUT: Pointer to scalar/array name */ | |
| 1001 | 1001 | int *pnOuter, /* OUT: Number of bytes at *pzOuter */ |
| 1002 | - const char **pzInner, /* OUT: Pointer to array key (or null) */ | |
| 1002 | + const char **pzInner, /* OUT: Pointer to array key (or null) */ | |
| 1003 | 1003 | int *pnInner, /* OUT: Number of bytes at *pzInner */ |
| 1004 | 1004 | int *pisGlobal /* OUT: Set to true if this is a global ref */ |
| 1005 | 1005 | ){ |
| 1006 | 1006 | const char *zOuter = zVarname; |
| 1007 | 1007 | int nOuter; |
| @@ -1044,11 +1044,11 @@ | ||
| 1044 | 1044 | return TH_OK; |
| 1045 | 1045 | } |
| 1046 | 1046 | |
| 1047 | 1047 | /* |
| 1048 | 1048 | ** Input string (zVar, nVar) contains a variable name. This function locates |
| 1049 | -** the Th_Variable structure associated with the named variable. The | |
| 1049 | +** the Th_Variable structure associated with the named variable. The | |
| 1050 | 1050 | ** variable name may be a global or local scalar or array variable |
| 1051 | 1051 | ** |
| 1052 | 1052 | ** If the create argument is non-zero and the named variable does not exist |
| 1053 | 1053 | ** it is created. Otherwise, an error is left in the interpreter result |
| 1054 | 1054 | ** and NULL returned. |
| @@ -1057,11 +1057,11 @@ | ||
| 1057 | 1057 | ** an error is left in the interpreter result and NULL returned. If |
| 1058 | 1058 | ** arrayok is true an array name is Ok. |
| 1059 | 1059 | */ |
| 1060 | 1060 | static Th_Variable *thFindValue( |
| 1061 | 1061 | Th_Interp *interp, |
| 1062 | - const char *zVar, /* Pointer to variable name */ | |
| 1062 | + const char *zVar, /* Pointer to variable name */ | |
| 1063 | 1063 | int nVar, /* Number of bytes at nVar */ |
| 1064 | 1064 | int create, /* If true, create the variable if not found */ |
| 1065 | 1065 | int arrayok, /* If true, an array is Ok. Otherwise array==error */ |
| 1066 | 1066 | int noerror /* If false, set interpreter result to error message */ |
| 1067 | 1067 | ){ |
| @@ -1135,12 +1135,12 @@ | ||
| 1135 | 1135 | } |
| 1136 | 1136 | return 0; |
| 1137 | 1137 | } |
| 1138 | 1138 | |
| 1139 | 1139 | /* |
| 1140 | -** String (zVar, nVar) must contain the name of a scalar variable or | |
| 1141 | -** array member. Look up the variable, store its current value in | |
| 1140 | +** String (zVar, nVar) must contain the name of a scalar variable or | |
| 1141 | +** array member. Look up the variable, store its current value in | |
| 1142 | 1142 | ** the interpreter result and return TH_OK. |
| 1143 | 1143 | ** |
| 1144 | 1144 | ** If the named variable does not exist, return TH_ERROR and leave |
| 1145 | 1145 | ** an error message in the interpreter result. |
| 1146 | 1146 | */ |
| @@ -1174,12 +1174,12 @@ | ||
| 1174 | 1174 | ** |
| 1175 | 1175 | ** If (zVar, nVar) refers to an existing array, TH_ERROR is returned |
| 1176 | 1176 | ** and an error message left in the interpreter result. |
| 1177 | 1177 | */ |
| 1178 | 1178 | int Th_SetVar( |
| 1179 | - Th_Interp *interp, | |
| 1180 | - const char *zVar, | |
| 1179 | + Th_Interp *interp, | |
| 1180 | + const char *zVar, | |
| 1181 | 1181 | int nVar, |
| 1182 | 1182 | const char *zValue, |
| 1183 | 1183 | int nValue |
| 1184 | 1184 | ){ |
| 1185 | 1185 | Th_Variable *pValue; |
| @@ -1210,13 +1210,13 @@ | ||
| 1210 | 1210 | ** Create a variable link so that accessing variable (zLocal, nLocal) is |
| 1211 | 1211 | ** the same as accessing variable (zLink, nLink) in stack frame iFrame. |
| 1212 | 1212 | */ |
| 1213 | 1213 | int Th_LinkVar( |
| 1214 | 1214 | Th_Interp *interp, /* Interpreter */ |
| 1215 | - const char *zLocal, int nLocal, /* Local varname */ | |
| 1215 | + const char *zLocal, int nLocal, /* Local varname */ | |
| 1216 | 1216 | int iFrame, /* Stack frame of linked var */ |
| 1217 | - const char *zLink, int nLink /* Linked varname */ | |
| 1217 | + const char *zLink, int nLink /* Linked varname */ | |
| 1218 | 1218 | ){ |
| 1219 | 1219 | Th_Frame *pSavedFrame = interp->pFrame; |
| 1220 | 1220 | Th_Frame *pFrame; |
| 1221 | 1221 | Th_HashEntry *pEntry; |
| 1222 | 1222 | Th_Variable *pValue; |
| @@ -1299,11 +1299,11 @@ | ||
| 1299 | 1299 | if( interp ){ |
| 1300 | 1300 | char *zRes = 0; |
| 1301 | 1301 | int nRes = 0; |
| 1302 | 1302 | |
| 1303 | 1303 | Th_SetVar(interp, (char *)"::th_stack_trace", -1, 0, 0); |
| 1304 | - | |
| 1304 | + | |
| 1305 | 1305 | Th_StringAppend(interp, &zRes, &nRes, zPre, -1); |
| 1306 | 1306 | if( zRes[nRes-1]=='"' ){ |
| 1307 | 1307 | Th_StringAppend(interp, &zRes, &nRes, z, n); |
| 1308 | 1308 | Th_StringAppend(interp, &zRes, &nRes, (const char *)"\"", 1); |
| 1309 | 1309 | }else{ |
| @@ -1381,12 +1381,12 @@ | ||
| 1381 | 1381 | return (char *)Th_Malloc(pInterp, 1); |
| 1382 | 1382 | } |
| 1383 | 1383 | } |
| 1384 | 1384 | |
| 1385 | 1385 | |
| 1386 | -/* | |
| 1387 | -** Wrappers around the supplied malloc() and free() | |
| 1386 | +/* | |
| 1387 | +** Wrappers around the supplied malloc() and free() | |
| 1388 | 1388 | */ |
| 1389 | 1389 | void *Th_Malloc(Th_Interp *pInterp, int nByte){ |
| 1390 | 1390 | void *p = pInterp->pVtab->xMalloc(nByte); |
| 1391 | 1391 | if( p ){ |
| 1392 | 1392 | memset(p, 0, nByte); |
| @@ -1398,16 +1398,16 @@ | ||
| 1398 | 1398 | pInterp->pVtab->xFree(z); |
| 1399 | 1399 | } |
| 1400 | 1400 | } |
| 1401 | 1401 | |
| 1402 | 1402 | /* |
| 1403 | -** Install a new th1 command. | |
| 1403 | +** Install a new th1 command. | |
| 1404 | 1404 | ** |
| 1405 | 1405 | ** If a command of the same name already exists, it is deleted automatically. |
| 1406 | 1406 | */ |
| 1407 | 1407 | int Th_CreateCommand( |
| 1408 | - Th_Interp *interp, | |
| 1408 | + Th_Interp *interp, | |
| 1409 | 1409 | const char *zName, /* New command name */ |
| 1410 | 1410 | Th_CommandProc xProc, /* Command callback proc */ |
| 1411 | 1411 | void *pContext, /* Value to pass as second arg to xProc */ |
| 1412 | 1412 | void (*xDel)(Th_Interp *, void *) /* Command destructor callback */ |
| 1413 | 1413 | ){ |
| @@ -1425,27 +1425,27 @@ | ||
| 1425 | 1425 | } |
| 1426 | 1426 | pCommand->xProc = xProc; |
| 1427 | 1427 | pCommand->pContext = pContext; |
| 1428 | 1428 | pCommand->xDel = xDel; |
| 1429 | 1429 | pEntry->pData = (void *)pCommand; |
| 1430 | - | |
| 1430 | + | |
| 1431 | 1431 | return TH_OK; |
| 1432 | 1432 | } |
| 1433 | 1433 | |
| 1434 | 1434 | /* |
| 1435 | -** Rename the existing command (zName, nName) to (zNew, nNew). If nNew is 0, | |
| 1435 | +** Rename the existing command (zName, nName) to (zNew, nNew). If nNew is 0, | |
| 1436 | 1436 | ** the command is deleted instead of renamed. |
| 1437 | 1437 | ** |
| 1438 | 1438 | ** If successful, TH_OK is returned. If command zName does not exist, or |
| 1439 | -** if command zNew already exists, an error message is left in the | |
| 1439 | +** if command zNew already exists, an error message is left in the | |
| 1440 | 1440 | ** interpreter result and TH_ERROR is returned. |
| 1441 | 1441 | */ |
| 1442 | 1442 | int Th_RenameCommand( |
| 1443 | - Th_Interp *interp, | |
| 1444 | - const char *zName, /* Existing command name */ | |
| 1443 | + Th_Interp *interp, | |
| 1444 | + const char *zName, /* Existing command name */ | |
| 1445 | 1445 | int nName, /* Number of bytes at zName */ |
| 1446 | - const char *zNew, /* New command name */ | |
| 1446 | + const char *zNew, /* New command name */ | |
| 1447 | 1447 | int nNew /* Number of bytes at zNew */ |
| 1448 | 1448 | ){ |
| 1449 | 1449 | Th_HashEntry *pEntry; |
| 1450 | 1450 | Th_HashEntry *pNewEntry; |
| 1451 | 1451 | |
| @@ -1499,11 +1499,11 @@ | ||
| 1499 | 1499 | ** If an error occurs (if (zList, nList) is not a valid list) an error |
| 1500 | 1500 | ** message is left in the interpreter result and TH_ERROR returned. |
| 1501 | 1501 | ** |
| 1502 | 1502 | ** If successful, *pnCount is set to the number of elements in the list. |
| 1503 | 1503 | ** panElem is set to point at an array of *pnCount integers - the lengths |
| 1504 | -** of the element values. *pazElem is set to point at an array of | |
| 1504 | +** of the element values. *pazElem is set to point at an array of | |
| 1505 | 1505 | ** pointers to buffers containing the array element's data. |
| 1506 | 1506 | ** |
| 1507 | 1507 | ** To free the arrays allocated at *pazElem and *panElem, the caller |
| 1508 | 1508 | ** should call Th_Free() on *pazElem only. Exactly one such call to |
| 1509 | 1509 | ** Th_Free() must be made per call to Th_SplitList(). |
| @@ -1525,13 +1525,13 @@ | ||
| 1525 | 1525 | ** Th_Free(interp, azElem); |
| 1526 | 1526 | ** |
| 1527 | 1527 | */ |
| 1528 | 1528 | int Th_SplitList( |
| 1529 | 1529 | Th_Interp *interp, |
| 1530 | - const char *zList, /* Pointer to buffer containing list */ | |
| 1530 | + const char *zList, /* Pointer to buffer containing list */ | |
| 1531 | 1531 | int nList, /* Number of bytes at zList */ |
| 1532 | - char ***pazElem, /* OUT: Array of pointers to element data */ | |
| 1532 | + char ***pazElem, /* OUT: Array of pointers to element data */ | |
| 1533 | 1533 | int **panElem, /* OUT: Array of element data lengths */ |
| 1534 | 1534 | int *pnCount /* OUT: Number of elements in list */ |
| 1535 | 1535 | ){ |
| 1536 | 1536 | int rc; |
| 1537 | 1537 | interp->isListMode = 1; |
| @@ -1542,16 +1542,16 @@ | ||
| 1542 | 1542 | } |
| 1543 | 1543 | return rc; |
| 1544 | 1544 | } |
| 1545 | 1545 | |
| 1546 | 1546 | /* |
| 1547 | -** Append a new element to an existing th1 list. The element to append | |
| 1547 | +** Append a new element to an existing th1 list. The element to append | |
| 1548 | 1548 | ** to the list is (zElem, nElem). |
| 1549 | 1549 | ** |
| 1550 | 1550 | ** A pointer to the existing list must be stored at *pzList when this |
| 1551 | -** function is called. The length must be stored in *pnList. The value | |
| 1552 | -** of *pzList must either be NULL (in which case *pnList must be 0), or | |
| 1551 | +** function is called. The length must be stored in *pnList. The value | |
| 1552 | +** of *pzList must either be NULL (in which case *pnList must be 0), or | |
| 1553 | 1553 | ** a pointer to memory obtained from Th_Malloc(). |
| 1554 | 1554 | ** |
| 1555 | 1555 | ** This function calls Th_Free() to free the buffer at *pzList and sets |
| 1556 | 1556 | ** *pzList to point to a new buffer containing the new list value. *pnList |
| 1557 | 1557 | ** is similarly updated before returning. The return value is always TH_OK. |
| @@ -1568,13 +1568,13 @@ | ||
| 1568 | 1568 | ** Th_Free(interp, zList); |
| 1569 | 1569 | ** |
| 1570 | 1570 | */ |
| 1571 | 1571 | int Th_ListAppend( |
| 1572 | 1572 | Th_Interp *interp, /* Interpreter context */ |
| 1573 | - char **pzList, /* IN/OUT: Ptr to ptr to list */ | |
| 1573 | + char **pzList, /* IN/OUT: Ptr to ptr to list */ | |
| 1574 | 1574 | int *pnList, /* IN/OUT: Current length of *pzList */ |
| 1575 | - const char *zElem, /* Data to append */ | |
| 1575 | + const char *zElem, /* Data to append */ | |
| 1576 | 1576 | int nElem /* Length of nElem */ |
| 1577 | 1577 | ){ |
| 1578 | 1578 | Buffer output; |
| 1579 | 1579 | int i; |
| 1580 | 1580 | |
| @@ -1623,13 +1623,13 @@ | ||
| 1623 | 1623 | ** Append a new element to an existing th1 string. This function uses |
| 1624 | 1624 | ** the same interface as the Th_ListAppend() function. |
| 1625 | 1625 | */ |
| 1626 | 1626 | int Th_StringAppend( |
| 1627 | 1627 | Th_Interp *interp, /* Interpreter context */ |
| 1628 | - char **pzStr, /* IN/OUT: Ptr to ptr to list */ | |
| 1628 | + char **pzStr, /* IN/OUT: Ptr to ptr to list */ | |
| 1629 | 1629 | int *pnStr, /* IN/OUT: Current length of *pzStr */ |
| 1630 | - const char *zElem, /* Data to append */ | |
| 1630 | + const char *zElem, /* Data to append */ | |
| 1631 | 1631 | int nElem /* Length of nElem */ |
| 1632 | 1632 | ){ |
| 1633 | 1633 | char *zNew; |
| 1634 | 1634 | int nNew; |
| 1635 | 1635 | |
| @@ -1647,11 +1647,11 @@ | ||
| 1647 | 1647 | *pnStr = nNew; |
| 1648 | 1648 | |
| 1649 | 1649 | return TH_OK; |
| 1650 | 1650 | } |
| 1651 | 1651 | |
| 1652 | -/* | |
| 1652 | +/* | |
| 1653 | 1653 | ** Delete an interpreter. |
| 1654 | 1654 | */ |
| 1655 | 1655 | void Th_DeleteInterp(Th_Interp *interp){ |
| 1656 | 1656 | assert(interp->pFrame); |
| 1657 | 1657 | assert(0==interp->pFrame->pCaller); |
| @@ -1668,11 +1668,11 @@ | ||
| 1668 | 1668 | |
| 1669 | 1669 | /* Delete the interpreter structure itself. */ |
| 1670 | 1670 | Th_Free(interp, (void *)interp); |
| 1671 | 1671 | } |
| 1672 | 1672 | |
| 1673 | -/* | |
| 1673 | +/* | |
| 1674 | 1674 | ** Create a new interpreter. |
| 1675 | 1675 | */ |
| 1676 | 1676 | Th_Interp * Th_CreateInterp(Th_Vtab *pVtab){ |
| 1677 | 1677 | Th_Interp *p; |
| 1678 | 1678 | |
| @@ -1702,11 +1702,11 @@ | ||
| 1702 | 1702 | Operator *pOp; |
| 1703 | 1703 | Expr *pParent; |
| 1704 | 1704 | Expr *pLeft; |
| 1705 | 1705 | Expr *pRight; |
| 1706 | 1706 | |
| 1707 | - char *zValue; /* Pointer to literal value */ | |
| 1707 | + char *zValue; /* Pointer to literal value */ | |
| 1708 | 1708 | int nValue; /* Length of literal value buffer */ |
| 1709 | 1709 | }; |
| 1710 | 1710 | |
| 1711 | 1711 | /* Unary operators */ |
| 1712 | 1712 | #define OP_UNARY_MINUS 2 |
| @@ -1758,11 +1758,11 @@ | ||
| 1758 | 1758 | {"+", OP_UNARY_PLUS, 1, ARG_NUMBER}, |
| 1759 | 1759 | {"~", OP_BITWISE_NOT, 1, ARG_INTEGER}, |
| 1760 | 1760 | {"!", OP_LOGICAL_NOT, 1, ARG_INTEGER}, |
| 1761 | 1761 | |
| 1762 | 1762 | /* Binary operators. It is important to the parsing in Th_Expr() that |
| 1763 | - * the two-character symbols ("==") appear before the one-character | |
| 1763 | + * the two-character symbols ("==") appear before the one-character | |
| 1764 | 1764 | * ones ("="). And that the priorities of all binary operators are |
| 1765 | 1765 | * integers between 2 and 12. |
| 1766 | 1766 | */ |
| 1767 | 1767 | {"<<", OP_LEFTSHIFT, 4, ARG_INTEGER}, |
| 1768 | 1768 | {">>", OP_RIGHTSHIFT, 4, ARG_INTEGER}, |
| @@ -1789,16 +1789,16 @@ | ||
| 1789 | 1789 | {0,0,0,0} |
| 1790 | 1790 | }; |
| 1791 | 1791 | |
| 1792 | 1792 | /* |
| 1793 | 1793 | ** The first part of the string (zInput,nInput) contains a number. |
| 1794 | -** Set *pnVarname to the number of bytes in the numeric string. | |
| 1794 | +** Set *pnVarname to the number of bytes in the numeric string. | |
| 1795 | 1795 | */ |
| 1796 | 1796 | static int thNextNumber( |
| 1797 | - Th_Interp *interp, | |
| 1798 | - const char *zInput, | |
| 1799 | - int nInput, | |
| 1797 | + Th_Interp *interp, | |
| 1798 | + const char *zInput, | |
| 1799 | + int nInput, | |
| 1800 | 1800 | int *pnLiteral |
| 1801 | 1801 | ){ |
| 1802 | 1802 | int i; |
| 1803 | 1803 | int seenDot = 0; |
| 1804 | 1804 | for(i=0; i<nInput; i++){ |
| @@ -1864,11 +1864,11 @@ | ||
| 1864 | 1864 | if( eArgType==ARG_NUMBER ){ |
| 1865 | 1865 | if( (zLeft==0 || TH_OK==Th_ToInt(0, zLeft, nLeft, &iLeft)) |
| 1866 | 1866 | && (zRight==0 || TH_OK==Th_ToInt(0, zRight, nRight, &iRight)) |
| 1867 | 1867 | ){ |
| 1868 | 1868 | eArgType = ARG_INTEGER; |
| 1869 | - }else if( | |
| 1869 | + }else if( | |
| 1870 | 1870 | (zLeft && TH_OK!=Th_ToDouble(interp, zLeft, nLeft, &fLeft)) || |
| 1871 | 1871 | (zRight && TH_OK!=Th_ToDouble(interp, zRight, nRight, &fRight)) |
| 1872 | 1872 | ){ |
| 1873 | 1873 | /* A type error. */ |
| 1874 | 1874 | rc = TH_ERROR; |
| @@ -1876,11 +1876,11 @@ | ||
| 1876 | 1876 | }else if( eArgType==ARG_INTEGER ){ |
| 1877 | 1877 | rc = Th_ToInt(interp, zLeft, nLeft, &iLeft); |
| 1878 | 1878 | if( rc==TH_OK && zRight ){ |
| 1879 | 1879 | rc = Th_ToInt(interp, zRight, nRight, &iRight); |
| 1880 | 1880 | } |
| 1881 | - } | |
| 1881 | + } | |
| 1882 | 1882 | } |
| 1883 | 1883 | |
| 1884 | 1884 | if( rc==TH_OK && eArgType==ARG_INTEGER ){ |
| 1885 | 1885 | int iRes = 0; |
| 1886 | 1886 | switch( pExpr->pOp->eOp ) { |
| @@ -1978,11 +1978,11 @@ | ||
| 1978 | 1978 | #define ISTERM(x) (apToken[x] && (!apToken[x]->pOp || apToken[x]->pLeft)) |
| 1979 | 1979 | |
| 1980 | 1980 | for(jj=0; jj<nToken; jj++){ |
| 1981 | 1981 | if( apToken[jj]->pOp && apToken[jj]->pOp->eOp==OP_OPEN_BRACKET ){ |
| 1982 | 1982 | int nNest = 1; |
| 1983 | - int iLeft = jj; | |
| 1983 | + int iLeft = jj; | |
| 1984 | 1984 | |
| 1985 | 1985 | for(jj++; jj<nToken; jj++){ |
| 1986 | 1986 | Operator *pOp = apToken[jj]->pOp; |
| 1987 | 1987 | if( pOp && pOp->eOp==OP_OPEN_BRACKET ) nNest++; |
| 1988 | 1988 | if( pOp && pOp->eOp==OP_CLOSE_BRACKET ) nNest--; |
| @@ -2052,11 +2052,11 @@ | ||
| 2052 | 2052 | /* |
| 2053 | 2053 | ** Parse a string containing a TH expression to a list of tokens. |
| 2054 | 2054 | */ |
| 2055 | 2055 | static int exprParse( |
| 2056 | 2056 | Th_Interp *interp, /* Interpreter to leave error message in */ |
| 2057 | - const char *zExpr, /* Pointer to input string */ | |
| 2057 | + const char *zExpr, /* Pointer to input string */ | |
| 2058 | 2058 | int nExpr, /* Number of bytes at zExpr */ |
| 2059 | 2059 | Expr ***papToken, /* OUT: Array of tokens. */ |
| 2060 | 2060 | int *pnToken /* OUT: Size of token array */ |
| 2061 | 2061 | ){ |
| 2062 | 2062 | int i; |
| @@ -2127,11 +2127,11 @@ | ||
| 2127 | 2127 | memcpy(pNew->zValue, z, pNew->nValue); |
| 2128 | 2128 | i += pNew->nValue; |
| 2129 | 2129 | } |
| 2130 | 2130 | if( (nToken%16)==0 ){ |
| 2131 | 2131 | /* Grow the apToken array. */ |
| 2132 | - Expr **apTokenOld = apToken; | |
| 2132 | + Expr **apTokenOld = apToken; | |
| 2133 | 2133 | apToken = Th_Malloc(interp, sizeof(Expr *)*(nToken+16)); |
| 2134 | 2134 | memcpy(apToken, apTokenOld, sizeof(Expr *)*nToken); |
| 2135 | 2135 | } |
| 2136 | 2136 | |
| 2137 | 2137 | /* Put the new token at the end of the apToken array */ |
| @@ -2152,11 +2152,11 @@ | ||
| 2152 | 2152 | /* |
| 2153 | 2153 | ** Evaluate the string (zExpr, nExpr) as a Th expression. Store |
| 2154 | 2154 | ** the result in the interpreter interp and return TH_OK if |
| 2155 | 2155 | ** successful. If an error occurs, store an error message in |
| 2156 | 2156 | ** the interpreter result and return an error code. |
| 2157 | -*/ | |
| 2157 | +*/ | |
| 2158 | 2158 | int Th_Expr(Th_Interp *interp, const char *zExpr, int nExpr){ |
| 2159 | 2159 | int rc; /* Return Code */ |
| 2160 | 2160 | int i; /* Loop counter */ |
| 2161 | 2161 | |
| 2162 | 2162 | int nToken = 0; |
| @@ -2169,11 +2169,11 @@ | ||
| 2169 | 2169 | /* Parse the expression to a list of tokens. */ |
| 2170 | 2170 | rc = exprParse(interp, zExpr, nExpr, &apToken, &nToken); |
| 2171 | 2171 | |
| 2172 | 2172 | /* If the parsing was successful, create an expression tree from |
| 2173 | 2173 | ** the parsed list of tokens. If successful, apToken[0] is set |
| 2174 | - ** to point to the root of the expression tree. | |
| 2174 | + ** to point to the root of the expression tree. | |
| 2175 | 2175 | */ |
| 2176 | 2176 | if( rc==TH_OK ){ |
| 2177 | 2177 | rc = exprMakeTree(interp, apToken, nToken); |
| 2178 | 2178 | } |
| 2179 | 2179 | |
| @@ -2210,11 +2210,11 @@ | ||
| 2210 | 2210 | ** the callback function xCallback for each entry. The second argument |
| 2211 | 2211 | ** passed to xCallback is a copy of the fourth argument passed to this |
| 2212 | 2212 | ** function. |
| 2213 | 2213 | */ |
| 2214 | 2214 | void Th_HashIterate( |
| 2215 | - Th_Interp *interp, | |
| 2215 | + Th_Interp *interp, | |
| 2216 | 2216 | Th_Hash *pHash, |
| 2217 | 2217 | void (*xCallback)(Th_HashEntry *pEntry, void *pContext), |
| 2218 | 2218 | void *pContext |
| 2219 | 2219 | ){ |
| 2220 | 2220 | int i; |
| @@ -2244,14 +2244,14 @@ | ||
| 2244 | 2244 | Th_Free(interp, pHash); |
| 2245 | 2245 | } |
| 2246 | 2246 | } |
| 2247 | 2247 | |
| 2248 | 2248 | /* |
| 2249 | -** This function is used to insert or delete hash table items, or to | |
| 2249 | +** This function is used to insert or delete hash table items, or to | |
| 2250 | 2250 | ** query a hash table for an existing item. |
| 2251 | 2251 | ** |
| 2252 | -** If parameter op is less than zero, then the hash-table element | |
| 2252 | +** If parameter op is less than zero, then the hash-table element | |
| 2253 | 2253 | ** identified by (zKey, nKey) is removed from the hash-table if it |
| 2254 | 2254 | ** exists. NULL is returned. |
| 2255 | 2255 | ** |
| 2256 | 2256 | ** Otherwise, if the hash-table contains an item with key (zKey, nKey), |
| 2257 | 2257 | ** a pointer to the associated Th_HashEntry is returned. If parameter |
| @@ -2258,11 +2258,11 @@ | ||
| 2258 | 2258 | ** op is greater than zero, then a new entry is added if one cannot |
| 2259 | 2259 | ** be found. If op is zero, then NULL is returned if the item is |
| 2260 | 2260 | ** not already present in the hash-table. |
| 2261 | 2261 | */ |
| 2262 | 2262 | Th_HashEntry *Th_HashFind( |
| 2263 | - Th_Interp *interp, | |
| 2263 | + Th_Interp *interp, | |
| 2264 | 2264 | Th_Hash *pHash, |
| 2265 | 2265 | const char *zKey, |
| 2266 | 2266 | int nKey, |
| 2267 | 2267 | int op /* -ve = delete, 0 = find, +ve = insert */ |
| 2268 | 2268 | ){ |
| @@ -2326,11 +2326,11 @@ | ||
| 2326 | 2326 | ** '\f' 0x0C |
| 2327 | 2327 | ** '\r' 0x0D |
| 2328 | 2328 | ** |
| 2329 | 2329 | ** Whitespace characters have the 0x01 flag set. Decimal digits have the |
| 2330 | 2330 | ** 0x2 flag set. Single byte printable characters have the 0x4 flag set. |
| 2331 | -** Alphabet characters have the 0x8 bit set. | |
| 2331 | +** Alphabet characters have the 0x8 bit set. | |
| 2332 | 2332 | ** |
| 2333 | 2333 | ** The special list characters have the 0x10 flag set |
| 2334 | 2334 | ** |
| 2335 | 2335 | ** { } [ ] \ ; ' " |
| 2336 | 2336 | ** |
| @@ -2477,14 +2477,14 @@ | ||
| 2477 | 2477 | return z - zBegin; |
| 2478 | 2478 | } |
| 2479 | 2479 | |
| 2480 | 2480 | /* |
| 2481 | 2481 | ** Try to convert the string passed as arguments (z, n) to an integer. |
| 2482 | -** If successful, store the result in *piOut and return TH_OK. | |
| 2482 | +** If successful, store the result in *piOut and return TH_OK. | |
| 2483 | 2483 | ** |
| 2484 | -** If the string cannot be converted to an integer, return TH_ERROR. | |
| 2485 | -** If the interp argument is not NULL, leave an error message in the | |
| 2484 | +** If the string cannot be converted to an integer, return TH_ERROR. | |
| 2485 | +** If the interp argument is not NULL, leave an error message in the | |
| 2486 | 2486 | ** interpreter result too. |
| 2487 | 2487 | */ |
| 2488 | 2488 | int Th_ToInt(Th_Interp *interp, const char *z, int n, int *piOut){ |
| 2489 | 2489 | int i = 0; |
| 2490 | 2490 | int iOut = 0; |
| @@ -2512,20 +2512,20 @@ | ||
| 2512 | 2512 | return TH_OK; |
| 2513 | 2513 | } |
| 2514 | 2514 | |
| 2515 | 2515 | /* |
| 2516 | 2516 | ** Try to convert the string passed as arguments (z, n) to a double. |
| 2517 | -** If successful, store the result in *pfOut and return TH_OK. | |
| 2517 | +** If successful, store the result in *pfOut and return TH_OK. | |
| 2518 | 2518 | ** |
| 2519 | -** If the string cannot be converted to a double, return TH_ERROR. | |
| 2520 | -** If the interp argument is not NULL, leave an error message in the | |
| 2519 | +** If the string cannot be converted to a double, return TH_ERROR. | |
| 2520 | +** If the interp argument is not NULL, leave an error message in the | |
| 2521 | 2521 | ** interpreter result too. |
| 2522 | 2522 | */ |
| 2523 | 2523 | int Th_ToDouble( |
| 2524 | - Th_Interp *interp, | |
| 2525 | - const char *z, | |
| 2526 | - int n, | |
| 2524 | + Th_Interp *interp, | |
| 2525 | + const char *z, | |
| 2526 | + int n, | |
| 2527 | 2527 | double *pfOut |
| 2528 | 2528 | ){ |
| 2529 | 2529 | if( !sqlite3IsNumber((const char *)z, 0) ){ |
| 2530 | 2530 | Th_ErrorMessage(interp, "expected number, got: \"", z, n); |
| 2531 | 2531 | return TH_ERROR; |
| @@ -2566,33 +2566,33 @@ | ||
| 2566 | 2566 | ** the double fVal and return TH_OK. |
| 2567 | 2567 | */ |
| 2568 | 2568 | int Th_SetResultDouble(Th_Interp *interp, double fVal){ |
| 2569 | 2569 | int i; /* Iterator variable */ |
| 2570 | 2570 | double v = fVal; /* Input value */ |
| 2571 | - char zBuf[128]; /* Output buffer */ | |
| 2572 | - char *z = zBuf; /* Output cursor */ | |
| 2571 | + char zBuf[128]; /* Output buffer */ | |
| 2572 | + char *z = zBuf; /* Output cursor */ | |
| 2573 | 2573 | int iDot = 0; /* Digit after which to place decimal point */ |
| 2574 | 2574 | int iExp = 0; /* Exponent (NN in eNN) */ |
| 2575 | - const char *zExp; /* String representation of iExp */ | |
| 2575 | + const char *zExp; /* String representation of iExp */ | |
| 2576 | 2576 | |
| 2577 | 2577 | /* Precision: */ |
| 2578 | 2578 | #define INSIGNIFICANT 0.000000000001 |
| 2579 | 2579 | #define ROUNDER 0.0000000000005 |
| 2580 | 2580 | double insignificant = INSIGNIFICANT; |
| 2581 | 2581 | |
| 2582 | 2582 | /* If the real value is negative, write a '-' character to the |
| 2583 | 2583 | * output and transform v to the corresponding positive number. |
| 2584 | - */ | |
| 2584 | + */ | |
| 2585 | 2585 | if( v<0.0 ){ |
| 2586 | 2586 | *z++ = '-'; |
| 2587 | 2587 | v *= -1.0; |
| 2588 | 2588 | } |
| 2589 | 2589 | |
| 2590 | - /* Normalize v to a value between 1.0 and 10.0. Integer | |
| 2590 | + /* Normalize v to a value between 1.0 and 10.0. Integer | |
| 2591 | 2591 | * variable iExp is set to the exponent. i.e the original |
| 2592 | 2592 | * value is (v * 10^iExp) (or the negative thereof). |
| 2593 | - */ | |
| 2593 | + */ | |
| 2594 | 2594 | if( v>0.0 ){ |
| 2595 | 2595 | while( (v+ROUNDER)>=10.0 ) { iExp++; v *= 0.1; } |
| 2596 | 2596 | while( (v+ROUNDER)<1.0 ) { iExp--; v *= 10.0; } |
| 2597 | 2597 | } |
| 2598 | 2598 | v += ROUNDER; |
| 2599 | 2599 |
| --- src/th.c | |
| +++ src/th.c | |
| @@ -1,8 +1,8 @@ | |
| 1 | |
| 2 | /* |
| 3 | ** The implementation of the TH core. This file contains the parser, and |
| 4 | ** the implementation of the interface in th.h. |
| 5 | */ |
| 6 | |
| 7 | #include "config.h" |
| 8 | #include "th.h" |
| @@ -16,11 +16,11 @@ | |
| 16 | /* |
| 17 | ** Interpreter structure. |
| 18 | */ |
| 19 | struct Th_Interp { |
| 20 | Th_Vtab *pVtab; /* Copy of the argument passed to Th_CreateInterp() */ |
| 21 | char *zResult; /* Current interpreter result (Th_Malloc()ed) */ |
| 22 | int nResult; /* number of bytes in zResult */ |
| 23 | Th_Hash *paCmd; /* Table of registered commands */ |
| 24 | Th_Frame *pFrame; /* Current execution frame */ |
| 25 | int isListMode; /* True if thSplitList() should operate in "list" mode */ |
| 26 | }; |
| @@ -42,25 +42,25 @@ | |
| 42 | ** are stored in the Th_Frame.paVar hash table member of the associated |
| 43 | ** stack frame object. |
| 44 | ** |
| 45 | ** When an interpreter is created, a single Th_Frame structure is also |
| 46 | ** allocated - the global variable scope. Th_Interp.pFrame (the current |
| 47 | ** interpreter frame) is initialised to point to this Th_Frame. It is |
| 48 | ** not deleted for the lifetime of the interpreter (because the global |
| 49 | ** frame never goes out of scope). |
| 50 | ** |
| 51 | ** New stack frames are created by the Th_InFrame() function. Before |
| 52 | ** invoking its callback function, Th_InFrame() allocates a new Th_Frame |
| 53 | ** structure with pCaller set to the current frame (Th_Interp.pFrame), |
| 54 | ** and sets the current frame to the new frame object. After the callback |
| 55 | ** has been invoked, the allocated Th_Frame is deleted and the value |
| 56 | ** of the current frame pointer restored. |
| 57 | ** |
| 58 | ** By default, the Th_SetVar(), Th_UnsetVar() and Th_GetVar() functions |
| 59 | ** access variable values in the current frame. If they need to access |
| 60 | ** the global frame, they do so by traversing the pCaller pointer list. |
| 61 | ** Likewise, the Th_LinkVar() function uses the pCaller pointers to |
| 62 | ** link to variables located in the global or other stack frames. |
| 63 | */ |
| 64 | struct Th_Frame { |
| 65 | Th_Hash *paVar; /* Variables defined in this scope */ |
| 66 | Th_Frame *pCaller; /* Calling frame */ |
| @@ -84,11 +84,11 @@ | |
| 84 | ** value. |
| 85 | */ |
| 86 | struct Th_Variable { |
| 87 | int nRef; /* Number of references to this structure */ |
| 88 | int nData; /* Number of bytes at Th_Variable.zData */ |
| 89 | char *zData; /* Data for scalar variables */ |
| 90 | Th_Hash *pHash; /* Data for array variables */ |
| 91 | }; |
| 92 | |
| 93 | /* |
| 94 | ** Hash table API: |
| @@ -110,19 +110,19 @@ | |
| 110 | static void thFreeVariable(Th_HashEntry*, void*); |
| 111 | static void thFreeCommand(Th_HashEntry*, void*); |
| 112 | |
| 113 | /* |
| 114 | ** The following are used by both the expression and language parsers. |
| 115 | ** Given that the start of the input string (z, n) is a language |
| 116 | ** construct of the relevant type (a command enclosed in [], an escape |
| 117 | ** sequence etc.), these functions determine the number of bytes |
| 118 | ** of the input consumed by the construct. For example: |
| 119 | ** |
| 120 | ** int nByte; |
| 121 | ** thNextCommand(interp, "[expr $a+1] $nIter", 18, &nByte); |
| 122 | ** |
| 123 | ** results in variable nByte being set to 11. Or, |
| 124 | ** |
| 125 | ** thNextVarname(interp, "$a+1", 4, &nByte); |
| 126 | ** |
| 127 | ** results in nByte being set to 2. |
| 128 | */ |
| @@ -132,24 +132,24 @@ | |
| 132 | static int thNextNumber (Th_Interp*, const char *z, int n, int *pN); |
| 133 | static int thNextSpace (Th_Interp*, const char *z, int n, int *pN); |
| 134 | |
| 135 | /* |
| 136 | ** Given that the input string (z, n) contains a language construct of |
| 137 | ** the relevant type (a command enclosed in [], an escape sequence |
| 138 | ** like "\xFF" or a variable reference like "${varname}", perform |
| 139 | ** substitution on the string and store the resulting string in |
| 140 | ** the interpreter result. |
| 141 | */ |
| 142 | static int thSubstCommand(Th_Interp*, const char *z, int n); |
| 143 | static int thSubstEscape (Th_Interp*, const char *z, int n); |
| 144 | static int thSubstVarname(Th_Interp*, const char *z, int n); |
| 145 | |
| 146 | /* |
| 147 | ** Given that there is a th1 word located at the start of the input |
| 148 | ** string (z, n), determine the length in bytes of that word. If the |
| 149 | ** isCmd argument is non-zero, then an unescaped ";" byte not |
| 150 | ** located inside of a block or quoted string is considered to mark |
| 151 | ** the end of the word. |
| 152 | */ |
| 153 | static int thNextWord(Th_Interp*, const char *z, int n, int *pN, int isCmd); |
| 154 | |
| 155 | /* |
| @@ -176,13 +176,13 @@ | |
| 176 | ** Append nAdd bytes of content copied from zAdd to the end of buffer |
| 177 | ** pBuffer. If there is not enough space currently allocated, resize |
| 178 | ** the allocation to make space. |
| 179 | */ |
| 180 | static int thBufferWrite( |
| 181 | Th_Interp *interp, |
| 182 | Buffer *pBuffer, |
| 183 | const char *zAdd, |
| 184 | int nAdd |
| 185 | ){ |
| 186 | int nReq; |
| 187 | |
| 188 | if( nAdd<0 ){ |
| @@ -311,19 +311,19 @@ | |
| 311 | Th_HashDelete(interp, pFrame->paVar); |
| 312 | interp->pFrame = pFrame->pCaller; |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | ** The first part of the string (zInput,nInput) contains an escape |
| 317 | ** sequence. Set *pnEscape to the number of bytes in the escape sequence. |
| 318 | ** If there is a parse error, return TH_ERROR and set the interpreter |
| 319 | ** result to an error message. Otherwise return TH_OK. |
| 320 | */ |
| 321 | static int thNextEscape( |
| 322 | Th_Interp *interp, |
| 323 | const char *zInput, |
| 324 | int nInput, |
| 325 | int *pnEscape |
| 326 | ){ |
| 327 | int i = 2; |
| 328 | |
| 329 | assert(nInput>0); |
| @@ -344,18 +344,18 @@ | |
| 344 | return TH_OK; |
| 345 | } |
| 346 | |
| 347 | /* |
| 348 | ** The first part of the string (zInput,nInput) contains a variable |
| 349 | ** reference. Set *pnVarname to the number of bytes in the variable |
| 350 | ** reference. If there is a parse error, return TH_ERROR and set the |
| 351 | ** interpreter result to an error message. Otherwise return TH_OK. |
| 352 | */ |
| 353 | int thNextVarname( |
| 354 | Th_Interp *interp, |
| 355 | const char *zInput, |
| 356 | int nInput, |
| 357 | int *pnVarname |
| 358 | ){ |
| 359 | int i; |
| 360 | |
| 361 | assert(nInput>0); |
| @@ -401,19 +401,19 @@ | |
| 401 | return TH_OK; |
| 402 | } |
| 403 | |
| 404 | /* |
| 405 | ** The first part of the string (zInput,nInput) contains a command |
| 406 | ** enclosed in a "[]" block. Set *pnCommand to the number of bytes in |
| 407 | ** the variable reference. If there is a parse error, return TH_ERROR |
| 408 | ** and set the interpreter result to an error message. Otherwise return |
| 409 | ** TH_OK. |
| 410 | */ |
| 411 | int thNextCommand( |
| 412 | Th_Interp *interp, |
| 413 | const char *zInput, |
| 414 | int nInput, |
| 415 | int *pnCommand |
| 416 | ){ |
| 417 | int nBrace = 0; |
| 418 | int nSquare = 0; |
| 419 | int i; |
| @@ -438,17 +438,17 @@ | |
| 438 | |
| 439 | return TH_OK; |
| 440 | } |
| 441 | |
| 442 | /* |
| 443 | ** Set *pnSpace to the number of whitespace bytes at the start of |
| 444 | ** input string (zInput, nInput). Always return TH_OK. |
| 445 | */ |
| 446 | int thNextSpace( |
| 447 | Th_Interp *interp, |
| 448 | const char *zInput, |
| 449 | int nInput, |
| 450 | int *pnSpace |
| 451 | ){ |
| 452 | int i; |
| 453 | for(i=0; i<nInput && th_isspace(zInput[i]); i++); |
| 454 | *pnSpace = i; |
| @@ -457,21 +457,21 @@ | |
| 457 | |
| 458 | /* |
| 459 | ** The first byte of the string (zInput,nInput) is not white-space. |
| 460 | ** Set *pnWord to the number of bytes in the th1 word that starts |
| 461 | ** with this byte. If a complete word cannot be parsed or some other |
| 462 | ** error occurs, return TH_ERROR and set the interpreter result to |
| 463 | ** an error message. Otherwise return TH_OK. |
| 464 | ** |
| 465 | ** If the isCmd argument is non-zero, then an unescaped ";" byte not |
| 466 | ** located inside of a block or quoted string is considered to mark |
| 467 | ** the end of the word. |
| 468 | */ |
| 469 | static int thNextWord( |
| 470 | Th_Interp *interp, |
| 471 | const char *zInput, |
| 472 | int nInput, |
| 473 | int *pnWord, |
| 474 | int isCmd |
| 475 | ){ |
| 476 | int iEnd = 0; |
| 477 | |
| @@ -531,12 +531,12 @@ | |
| 531 | return thEvalLocal(interp, &zWord[1], nWord-2); |
| 532 | } |
| 533 | |
| 534 | /* |
| 535 | ** The input string (zWord, nWord) contains a th1 variable reference |
| 536 | ** (a '$' byte followed by a variable name). Perform substitution on |
| 537 | ** the input string and store the resulting string in the interpreter |
| 538 | ** result. |
| 539 | */ |
| 540 | static int thSubstVarname( |
| 541 | Th_Interp *interp, |
| 542 | const char *zWord, |
| @@ -572,11 +572,11 @@ | |
| 572 | return Th_GetVar(interp, &zWord[1], nWord-1); |
| 573 | } |
| 574 | |
| 575 | /* |
| 576 | ** The input string (zWord, nWord) contains a th1 escape sequence. |
| 577 | ** Perform substitution on the input string and store the resulting |
| 578 | ** string in the interpreter result. |
| 579 | */ |
| 580 | static int thSubstEscape( |
| 581 | Th_Interp *interp, |
| 582 | const char *zWord, |
| @@ -608,11 +608,11 @@ | |
| 608 | return TH_OK; |
| 609 | } |
| 610 | |
| 611 | /* |
| 612 | ** The input string (zWord, nWord) contains a th1 word. Perform |
| 613 | ** substitution on the input string and store the resulting |
| 614 | ** string in the interpreter result. |
| 615 | */ |
| 616 | static int thSubstWord( |
| 617 | Th_Interp *interp, |
| 618 | const char *zWord, |
| @@ -640,20 +640,20 @@ | |
| 640 | int (*xGet)(Th_Interp *, const char*, int, int *) = 0; |
| 641 | int (*xSubst)(Th_Interp *, const char*, int) = 0; |
| 642 | |
| 643 | switch( zWord[i] ){ |
| 644 | case '\\': |
| 645 | xGet = thNextEscape; xSubst = thSubstEscape; |
| 646 | break; |
| 647 | case '[': |
| 648 | if( !interp->isListMode ){ |
| 649 | xGet = thNextCommand; xSubst = thSubstCommand; |
| 650 | break; |
| 651 | } |
| 652 | case '$': |
| 653 | if( !interp->isListMode ){ |
| 654 | xGet = thNextVarname; xSubst = thSubstVarname; |
| 655 | break; |
| 656 | } |
| 657 | default: { |
| 658 | thBufferWrite(interp, &output, &zWord[i], 1); |
| 659 | continue; /* Go to the next iteration of the for(...) loop */ |
| @@ -685,11 +685,11 @@ | |
| 685 | ** Return true if one of the following is true of the buffer pointed |
| 686 | ** to by zInput, length nInput: |
| 687 | ** |
| 688 | ** + It is empty, or |
| 689 | ** + It contains nothing but white-space, or |
| 690 | ** + It contains no non-white-space characters before the first |
| 691 | ** newline character. |
| 692 | ** |
| 693 | ** Otherwise return false. |
| 694 | */ |
| 695 | static int thEndOfLine(const char *zInput, int nInput){ |
| @@ -725,16 +725,16 @@ | |
| 725 | ** // Free all memory allocated by Th_SplitList(). The arrays pointed |
| 726 | ** // to by argv and argl are invalidated by this call. |
| 727 | ** // |
| 728 | ** Th_Free(interp, argv); |
| 729 | ** |
| 730 | */ |
| 731 | static int thSplitList( |
| 732 | Th_Interp *interp, /* Interpreter context */ |
| 733 | const char *zList, /* Pointer to buffer containing input list */ |
| 734 | int nList, /* Size of buffer pointed to by zList */ |
| 735 | char ***pazElem, /* OUT: Array of list elements */ |
| 736 | int **panElem, /* OUT: Lengths of each list element */ |
| 737 | int *pnCount /* OUT: Number of list elements */ |
| 738 | ){ |
| 739 | int rc = TH_OK; |
| 740 | |
| @@ -774,14 +774,14 @@ | |
| 774 | assert((lenbuf.nBuf/sizeof(int))==nCount); |
| 775 | |
| 776 | assert((pazElem && panElem) || (!pazElem && !panElem)); |
| 777 | if( pazElem && rc==TH_OK ){ |
| 778 | int i; |
| 779 | char *zElem; |
| 780 | int *anElem; |
| 781 | char **azElem = Th_Malloc(interp, |
| 782 | sizeof(char*) * nCount + /* azElem */ |
| 783 | sizeof(int) * nCount + /* anElem */ |
| 784 | strbuf.nBuf /* space for list element strings */ |
| 785 | ); |
| 786 | anElem = (int *)&azElem[nCount]; |
| 787 | zElem = (char *)&anElem[nCount]; |
| @@ -795,11 +795,11 @@ | |
| 795 | *panElem = anElem; |
| 796 | } |
| 797 | if( pnCount ){ |
| 798 | *pnCount = nCount; |
| 799 | } |
| 800 | |
| 801 | finish: |
| 802 | thBufferFree(interp, &strbuf); |
| 803 | thBufferFree(interp, &lenbuf); |
| 804 | return rc; |
| 805 | } |
| @@ -876,18 +876,18 @@ | |
| 876 | if( rc==TH_OK ){ |
| 877 | Th_Command *p = (Th_Command *)(pEntry->pData); |
| 878 | const char **azArg = (const char **)argv; |
| 879 | rc = p->xProc(interp, p->pContext, argc, azArg, argl); |
| 880 | } |
| 881 | |
| 882 | /* If an error occurred, add this command to the stack trace report. */ |
| 883 | if( rc==TH_ERROR ){ |
| 884 | char *zRes; |
| 885 | int nRes; |
| 886 | char *zStack = 0; |
| 887 | int nStack = 0; |
| 888 | |
| 889 | zRes = Th_TakeResult(interp, &nRes); |
| 890 | if( TH_OK==Th_GetVar(interp, (char *)"::th_stack_trace", -1) ){ |
| 891 | zStack = Th_TakeResult(interp, &nStack); |
| 892 | } |
| 893 | Th_ListAppend(interp, &zStack, &nStack, zFirst, zInput-zFirst); |
| @@ -912,15 +912,15 @@ | |
| 912 | ** |
| 913 | ** Argument iFrame is interpreted as follows: |
| 914 | ** |
| 915 | ** * If iFrame is 0, this means the current frame. |
| 916 | ** |
| 917 | ** * If iFrame is negative, then the nth frame up the stack, where |
| 918 | ** n is the absolute value of iFrame. A value of -1 means the |
| 919 | ** calling procedure. |
| 920 | ** |
| 921 | ** * If iFrame is +ve, then the nth frame from the bottom of the |
| 922 | ** stack. An iFrame value of 1 means the toplevel (global) frame. |
| 923 | */ |
| 924 | static Th_Frame *getFrame(Th_Interp *interp, int iFrame){ |
| 925 | Th_Frame *p = interp->pFrame; |
| 926 | int i; |
| @@ -948,28 +948,28 @@ | |
| 948 | |
| 949 | |
| 950 | /* |
| 951 | ** Evaluate th1 script (zProgram, nProgram) in the frame identified by |
| 952 | ** argument iFrame. Leave either an error message or a result in the |
| 953 | ** interpreter result and return a th1 error code (TH_OK, TH_ERROR, |
| 954 | ** TH_RETURN, TH_CONTINUE or TH_BREAK). |
| 955 | */ |
| 956 | int Th_Eval(Th_Interp *interp, int iFrame, const char *zProgram, int nProgram){ |
| 957 | int rc = TH_OK; |
| 958 | Th_Frame *pSavedFrame = interp->pFrame; |
| 959 | |
| 960 | /* Set Th_Interp.pFrame to the frame that this script is to be |
| 961 | ** evaluated in. The current frame is saved in pSavedFrame and will |
| 962 | ** be restored before this function returns. |
| 963 | */ |
| 964 | interp->pFrame = getFrame(interp, iFrame); |
| 965 | |
| 966 | if( !interp->pFrame ){ |
| 967 | rc = TH_ERROR; |
| 968 | }else{ |
| 969 | int nInput = nProgram; |
| 970 | |
| 971 | if( nInput<0 ){ |
| 972 | nInput = th_strlen(zProgram); |
| 973 | } |
| 974 | rc = thEvalLocal(interp, zProgram, nInput); |
| 975 | } |
| @@ -995,13 +995,13 @@ | |
| 995 | ** array key name. |
| 996 | */ |
| 997 | static int thAnalyseVarname( |
| 998 | const char *zVarname, |
| 999 | int nVarname, |
| 1000 | const char **pzOuter, /* OUT: Pointer to scalar/array name */ |
| 1001 | int *pnOuter, /* OUT: Number of bytes at *pzOuter */ |
| 1002 | const char **pzInner, /* OUT: Pointer to array key (or null) */ |
| 1003 | int *pnInner, /* OUT: Number of bytes at *pzInner */ |
| 1004 | int *pisGlobal /* OUT: Set to true if this is a global ref */ |
| 1005 | ){ |
| 1006 | const char *zOuter = zVarname; |
| 1007 | int nOuter; |
| @@ -1044,11 +1044,11 @@ | |
| 1044 | return TH_OK; |
| 1045 | } |
| 1046 | |
| 1047 | /* |
| 1048 | ** Input string (zVar, nVar) contains a variable name. This function locates |
| 1049 | ** the Th_Variable structure associated with the named variable. The |
| 1050 | ** variable name may be a global or local scalar or array variable |
| 1051 | ** |
| 1052 | ** If the create argument is non-zero and the named variable does not exist |
| 1053 | ** it is created. Otherwise, an error is left in the interpreter result |
| 1054 | ** and NULL returned. |
| @@ -1057,11 +1057,11 @@ | |
| 1057 | ** an error is left in the interpreter result and NULL returned. If |
| 1058 | ** arrayok is true an array name is Ok. |
| 1059 | */ |
| 1060 | static Th_Variable *thFindValue( |
| 1061 | Th_Interp *interp, |
| 1062 | const char *zVar, /* Pointer to variable name */ |
| 1063 | int nVar, /* Number of bytes at nVar */ |
| 1064 | int create, /* If true, create the variable if not found */ |
| 1065 | int arrayok, /* If true, an array is Ok. Otherwise array==error */ |
| 1066 | int noerror /* If false, set interpreter result to error message */ |
| 1067 | ){ |
| @@ -1135,12 +1135,12 @@ | |
| 1135 | } |
| 1136 | return 0; |
| 1137 | } |
| 1138 | |
| 1139 | /* |
| 1140 | ** String (zVar, nVar) must contain the name of a scalar variable or |
| 1141 | ** array member. Look up the variable, store its current value in |
| 1142 | ** the interpreter result and return TH_OK. |
| 1143 | ** |
| 1144 | ** If the named variable does not exist, return TH_ERROR and leave |
| 1145 | ** an error message in the interpreter result. |
| 1146 | */ |
| @@ -1174,12 +1174,12 @@ | |
| 1174 | ** |
| 1175 | ** If (zVar, nVar) refers to an existing array, TH_ERROR is returned |
| 1176 | ** and an error message left in the interpreter result. |
| 1177 | */ |
| 1178 | int Th_SetVar( |
| 1179 | Th_Interp *interp, |
| 1180 | const char *zVar, |
| 1181 | int nVar, |
| 1182 | const char *zValue, |
| 1183 | int nValue |
| 1184 | ){ |
| 1185 | Th_Variable *pValue; |
| @@ -1210,13 +1210,13 @@ | |
| 1210 | ** Create a variable link so that accessing variable (zLocal, nLocal) is |
| 1211 | ** the same as accessing variable (zLink, nLink) in stack frame iFrame. |
| 1212 | */ |
| 1213 | int Th_LinkVar( |
| 1214 | Th_Interp *interp, /* Interpreter */ |
| 1215 | const char *zLocal, int nLocal, /* Local varname */ |
| 1216 | int iFrame, /* Stack frame of linked var */ |
| 1217 | const char *zLink, int nLink /* Linked varname */ |
| 1218 | ){ |
| 1219 | Th_Frame *pSavedFrame = interp->pFrame; |
| 1220 | Th_Frame *pFrame; |
| 1221 | Th_HashEntry *pEntry; |
| 1222 | Th_Variable *pValue; |
| @@ -1299,11 +1299,11 @@ | |
| 1299 | if( interp ){ |
| 1300 | char *zRes = 0; |
| 1301 | int nRes = 0; |
| 1302 | |
| 1303 | Th_SetVar(interp, (char *)"::th_stack_trace", -1, 0, 0); |
| 1304 | |
| 1305 | Th_StringAppend(interp, &zRes, &nRes, zPre, -1); |
| 1306 | if( zRes[nRes-1]=='"' ){ |
| 1307 | Th_StringAppend(interp, &zRes, &nRes, z, n); |
| 1308 | Th_StringAppend(interp, &zRes, &nRes, (const char *)"\"", 1); |
| 1309 | }else{ |
| @@ -1381,12 +1381,12 @@ | |
| 1381 | return (char *)Th_Malloc(pInterp, 1); |
| 1382 | } |
| 1383 | } |
| 1384 | |
| 1385 | |
| 1386 | /* |
| 1387 | ** Wrappers around the supplied malloc() and free() |
| 1388 | */ |
| 1389 | void *Th_Malloc(Th_Interp *pInterp, int nByte){ |
| 1390 | void *p = pInterp->pVtab->xMalloc(nByte); |
| 1391 | if( p ){ |
| 1392 | memset(p, 0, nByte); |
| @@ -1398,16 +1398,16 @@ | |
| 1398 | pInterp->pVtab->xFree(z); |
| 1399 | } |
| 1400 | } |
| 1401 | |
| 1402 | /* |
| 1403 | ** Install a new th1 command. |
| 1404 | ** |
| 1405 | ** If a command of the same name already exists, it is deleted automatically. |
| 1406 | */ |
| 1407 | int Th_CreateCommand( |
| 1408 | Th_Interp *interp, |
| 1409 | const char *zName, /* New command name */ |
| 1410 | Th_CommandProc xProc, /* Command callback proc */ |
| 1411 | void *pContext, /* Value to pass as second arg to xProc */ |
| 1412 | void (*xDel)(Th_Interp *, void *) /* Command destructor callback */ |
| 1413 | ){ |
| @@ -1425,27 +1425,27 @@ | |
| 1425 | } |
| 1426 | pCommand->xProc = xProc; |
| 1427 | pCommand->pContext = pContext; |
| 1428 | pCommand->xDel = xDel; |
| 1429 | pEntry->pData = (void *)pCommand; |
| 1430 | |
| 1431 | return TH_OK; |
| 1432 | } |
| 1433 | |
| 1434 | /* |
| 1435 | ** Rename the existing command (zName, nName) to (zNew, nNew). If nNew is 0, |
| 1436 | ** the command is deleted instead of renamed. |
| 1437 | ** |
| 1438 | ** If successful, TH_OK is returned. If command zName does not exist, or |
| 1439 | ** if command zNew already exists, an error message is left in the |
| 1440 | ** interpreter result and TH_ERROR is returned. |
| 1441 | */ |
| 1442 | int Th_RenameCommand( |
| 1443 | Th_Interp *interp, |
| 1444 | const char *zName, /* Existing command name */ |
| 1445 | int nName, /* Number of bytes at zName */ |
| 1446 | const char *zNew, /* New command name */ |
| 1447 | int nNew /* Number of bytes at zNew */ |
| 1448 | ){ |
| 1449 | Th_HashEntry *pEntry; |
| 1450 | Th_HashEntry *pNewEntry; |
| 1451 | |
| @@ -1499,11 +1499,11 @@ | |
| 1499 | ** If an error occurs (if (zList, nList) is not a valid list) an error |
| 1500 | ** message is left in the interpreter result and TH_ERROR returned. |
| 1501 | ** |
| 1502 | ** If successful, *pnCount is set to the number of elements in the list. |
| 1503 | ** panElem is set to point at an array of *pnCount integers - the lengths |
| 1504 | ** of the element values. *pazElem is set to point at an array of |
| 1505 | ** pointers to buffers containing the array element's data. |
| 1506 | ** |
| 1507 | ** To free the arrays allocated at *pazElem and *panElem, the caller |
| 1508 | ** should call Th_Free() on *pazElem only. Exactly one such call to |
| 1509 | ** Th_Free() must be made per call to Th_SplitList(). |
| @@ -1525,13 +1525,13 @@ | |
| 1525 | ** Th_Free(interp, azElem); |
| 1526 | ** |
| 1527 | */ |
| 1528 | int Th_SplitList( |
| 1529 | Th_Interp *interp, |
| 1530 | const char *zList, /* Pointer to buffer containing list */ |
| 1531 | int nList, /* Number of bytes at zList */ |
| 1532 | char ***pazElem, /* OUT: Array of pointers to element data */ |
| 1533 | int **panElem, /* OUT: Array of element data lengths */ |
| 1534 | int *pnCount /* OUT: Number of elements in list */ |
| 1535 | ){ |
| 1536 | int rc; |
| 1537 | interp->isListMode = 1; |
| @@ -1542,16 +1542,16 @@ | |
| 1542 | } |
| 1543 | return rc; |
| 1544 | } |
| 1545 | |
| 1546 | /* |
| 1547 | ** Append a new element to an existing th1 list. The element to append |
| 1548 | ** to the list is (zElem, nElem). |
| 1549 | ** |
| 1550 | ** A pointer to the existing list must be stored at *pzList when this |
| 1551 | ** function is called. The length must be stored in *pnList. The value |
| 1552 | ** of *pzList must either be NULL (in which case *pnList must be 0), or |
| 1553 | ** a pointer to memory obtained from Th_Malloc(). |
| 1554 | ** |
| 1555 | ** This function calls Th_Free() to free the buffer at *pzList and sets |
| 1556 | ** *pzList to point to a new buffer containing the new list value. *pnList |
| 1557 | ** is similarly updated before returning. The return value is always TH_OK. |
| @@ -1568,13 +1568,13 @@ | |
| 1568 | ** Th_Free(interp, zList); |
| 1569 | ** |
| 1570 | */ |
| 1571 | int Th_ListAppend( |
| 1572 | Th_Interp *interp, /* Interpreter context */ |
| 1573 | char **pzList, /* IN/OUT: Ptr to ptr to list */ |
| 1574 | int *pnList, /* IN/OUT: Current length of *pzList */ |
| 1575 | const char *zElem, /* Data to append */ |
| 1576 | int nElem /* Length of nElem */ |
| 1577 | ){ |
| 1578 | Buffer output; |
| 1579 | int i; |
| 1580 | |
| @@ -1623,13 +1623,13 @@ | |
| 1623 | ** Append a new element to an existing th1 string. This function uses |
| 1624 | ** the same interface as the Th_ListAppend() function. |
| 1625 | */ |
| 1626 | int Th_StringAppend( |
| 1627 | Th_Interp *interp, /* Interpreter context */ |
| 1628 | char **pzStr, /* IN/OUT: Ptr to ptr to list */ |
| 1629 | int *pnStr, /* IN/OUT: Current length of *pzStr */ |
| 1630 | const char *zElem, /* Data to append */ |
| 1631 | int nElem /* Length of nElem */ |
| 1632 | ){ |
| 1633 | char *zNew; |
| 1634 | int nNew; |
| 1635 | |
| @@ -1647,11 +1647,11 @@ | |
| 1647 | *pnStr = nNew; |
| 1648 | |
| 1649 | return TH_OK; |
| 1650 | } |
| 1651 | |
| 1652 | /* |
| 1653 | ** Delete an interpreter. |
| 1654 | */ |
| 1655 | void Th_DeleteInterp(Th_Interp *interp){ |
| 1656 | assert(interp->pFrame); |
| 1657 | assert(0==interp->pFrame->pCaller); |
| @@ -1668,11 +1668,11 @@ | |
| 1668 | |
| 1669 | /* Delete the interpreter structure itself. */ |
| 1670 | Th_Free(interp, (void *)interp); |
| 1671 | } |
| 1672 | |
| 1673 | /* |
| 1674 | ** Create a new interpreter. |
| 1675 | */ |
| 1676 | Th_Interp * Th_CreateInterp(Th_Vtab *pVtab){ |
| 1677 | Th_Interp *p; |
| 1678 | |
| @@ -1702,11 +1702,11 @@ | |
| 1702 | Operator *pOp; |
| 1703 | Expr *pParent; |
| 1704 | Expr *pLeft; |
| 1705 | Expr *pRight; |
| 1706 | |
| 1707 | char *zValue; /* Pointer to literal value */ |
| 1708 | int nValue; /* Length of literal value buffer */ |
| 1709 | }; |
| 1710 | |
| 1711 | /* Unary operators */ |
| 1712 | #define OP_UNARY_MINUS 2 |
| @@ -1758,11 +1758,11 @@ | |
| 1758 | {"+", OP_UNARY_PLUS, 1, ARG_NUMBER}, |
| 1759 | {"~", OP_BITWISE_NOT, 1, ARG_INTEGER}, |
| 1760 | {"!", OP_LOGICAL_NOT, 1, ARG_INTEGER}, |
| 1761 | |
| 1762 | /* Binary operators. It is important to the parsing in Th_Expr() that |
| 1763 | * the two-character symbols ("==") appear before the one-character |
| 1764 | * ones ("="). And that the priorities of all binary operators are |
| 1765 | * integers between 2 and 12. |
| 1766 | */ |
| 1767 | {"<<", OP_LEFTSHIFT, 4, ARG_INTEGER}, |
| 1768 | {">>", OP_RIGHTSHIFT, 4, ARG_INTEGER}, |
| @@ -1789,16 +1789,16 @@ | |
| 1789 | {0,0,0,0} |
| 1790 | }; |
| 1791 | |
| 1792 | /* |
| 1793 | ** The first part of the string (zInput,nInput) contains a number. |
| 1794 | ** Set *pnVarname to the number of bytes in the numeric string. |
| 1795 | */ |
| 1796 | static int thNextNumber( |
| 1797 | Th_Interp *interp, |
| 1798 | const char *zInput, |
| 1799 | int nInput, |
| 1800 | int *pnLiteral |
| 1801 | ){ |
| 1802 | int i; |
| 1803 | int seenDot = 0; |
| 1804 | for(i=0; i<nInput; i++){ |
| @@ -1864,11 +1864,11 @@ | |
| 1864 | if( eArgType==ARG_NUMBER ){ |
| 1865 | if( (zLeft==0 || TH_OK==Th_ToInt(0, zLeft, nLeft, &iLeft)) |
| 1866 | && (zRight==0 || TH_OK==Th_ToInt(0, zRight, nRight, &iRight)) |
| 1867 | ){ |
| 1868 | eArgType = ARG_INTEGER; |
| 1869 | }else if( |
| 1870 | (zLeft && TH_OK!=Th_ToDouble(interp, zLeft, nLeft, &fLeft)) || |
| 1871 | (zRight && TH_OK!=Th_ToDouble(interp, zRight, nRight, &fRight)) |
| 1872 | ){ |
| 1873 | /* A type error. */ |
| 1874 | rc = TH_ERROR; |
| @@ -1876,11 +1876,11 @@ | |
| 1876 | }else if( eArgType==ARG_INTEGER ){ |
| 1877 | rc = Th_ToInt(interp, zLeft, nLeft, &iLeft); |
| 1878 | if( rc==TH_OK && zRight ){ |
| 1879 | rc = Th_ToInt(interp, zRight, nRight, &iRight); |
| 1880 | } |
| 1881 | } |
| 1882 | } |
| 1883 | |
| 1884 | if( rc==TH_OK && eArgType==ARG_INTEGER ){ |
| 1885 | int iRes = 0; |
| 1886 | switch( pExpr->pOp->eOp ) { |
| @@ -1978,11 +1978,11 @@ | |
| 1978 | #define ISTERM(x) (apToken[x] && (!apToken[x]->pOp || apToken[x]->pLeft)) |
| 1979 | |
| 1980 | for(jj=0; jj<nToken; jj++){ |
| 1981 | if( apToken[jj]->pOp && apToken[jj]->pOp->eOp==OP_OPEN_BRACKET ){ |
| 1982 | int nNest = 1; |
| 1983 | int iLeft = jj; |
| 1984 | |
| 1985 | for(jj++; jj<nToken; jj++){ |
| 1986 | Operator *pOp = apToken[jj]->pOp; |
| 1987 | if( pOp && pOp->eOp==OP_OPEN_BRACKET ) nNest++; |
| 1988 | if( pOp && pOp->eOp==OP_CLOSE_BRACKET ) nNest--; |
| @@ -2052,11 +2052,11 @@ | |
| 2052 | /* |
| 2053 | ** Parse a string containing a TH expression to a list of tokens. |
| 2054 | */ |
| 2055 | static int exprParse( |
| 2056 | Th_Interp *interp, /* Interpreter to leave error message in */ |
| 2057 | const char *zExpr, /* Pointer to input string */ |
| 2058 | int nExpr, /* Number of bytes at zExpr */ |
| 2059 | Expr ***papToken, /* OUT: Array of tokens. */ |
| 2060 | int *pnToken /* OUT: Size of token array */ |
| 2061 | ){ |
| 2062 | int i; |
| @@ -2127,11 +2127,11 @@ | |
| 2127 | memcpy(pNew->zValue, z, pNew->nValue); |
| 2128 | i += pNew->nValue; |
| 2129 | } |
| 2130 | if( (nToken%16)==0 ){ |
| 2131 | /* Grow the apToken array. */ |
| 2132 | Expr **apTokenOld = apToken; |
| 2133 | apToken = Th_Malloc(interp, sizeof(Expr *)*(nToken+16)); |
| 2134 | memcpy(apToken, apTokenOld, sizeof(Expr *)*nToken); |
| 2135 | } |
| 2136 | |
| 2137 | /* Put the new token at the end of the apToken array */ |
| @@ -2152,11 +2152,11 @@ | |
| 2152 | /* |
| 2153 | ** Evaluate the string (zExpr, nExpr) as a Th expression. Store |
| 2154 | ** the result in the interpreter interp and return TH_OK if |
| 2155 | ** successful. If an error occurs, store an error message in |
| 2156 | ** the interpreter result and return an error code. |
| 2157 | */ |
| 2158 | int Th_Expr(Th_Interp *interp, const char *zExpr, int nExpr){ |
| 2159 | int rc; /* Return Code */ |
| 2160 | int i; /* Loop counter */ |
| 2161 | |
| 2162 | int nToken = 0; |
| @@ -2169,11 +2169,11 @@ | |
| 2169 | /* Parse the expression to a list of tokens. */ |
| 2170 | rc = exprParse(interp, zExpr, nExpr, &apToken, &nToken); |
| 2171 | |
| 2172 | /* If the parsing was successful, create an expression tree from |
| 2173 | ** the parsed list of tokens. If successful, apToken[0] is set |
| 2174 | ** to point to the root of the expression tree. |
| 2175 | */ |
| 2176 | if( rc==TH_OK ){ |
| 2177 | rc = exprMakeTree(interp, apToken, nToken); |
| 2178 | } |
| 2179 | |
| @@ -2210,11 +2210,11 @@ | |
| 2210 | ** the callback function xCallback for each entry. The second argument |
| 2211 | ** passed to xCallback is a copy of the fourth argument passed to this |
| 2212 | ** function. |
| 2213 | */ |
| 2214 | void Th_HashIterate( |
| 2215 | Th_Interp *interp, |
| 2216 | Th_Hash *pHash, |
| 2217 | void (*xCallback)(Th_HashEntry *pEntry, void *pContext), |
| 2218 | void *pContext |
| 2219 | ){ |
| 2220 | int i; |
| @@ -2244,14 +2244,14 @@ | |
| 2244 | Th_Free(interp, pHash); |
| 2245 | } |
| 2246 | } |
| 2247 | |
| 2248 | /* |
| 2249 | ** This function is used to insert or delete hash table items, or to |
| 2250 | ** query a hash table for an existing item. |
| 2251 | ** |
| 2252 | ** If parameter op is less than zero, then the hash-table element |
| 2253 | ** identified by (zKey, nKey) is removed from the hash-table if it |
| 2254 | ** exists. NULL is returned. |
| 2255 | ** |
| 2256 | ** Otherwise, if the hash-table contains an item with key (zKey, nKey), |
| 2257 | ** a pointer to the associated Th_HashEntry is returned. If parameter |
| @@ -2258,11 +2258,11 @@ | |
| 2258 | ** op is greater than zero, then a new entry is added if one cannot |
| 2259 | ** be found. If op is zero, then NULL is returned if the item is |
| 2260 | ** not already present in the hash-table. |
| 2261 | */ |
| 2262 | Th_HashEntry *Th_HashFind( |
| 2263 | Th_Interp *interp, |
| 2264 | Th_Hash *pHash, |
| 2265 | const char *zKey, |
| 2266 | int nKey, |
| 2267 | int op /* -ve = delete, 0 = find, +ve = insert */ |
| 2268 | ){ |
| @@ -2326,11 +2326,11 @@ | |
| 2326 | ** '\f' 0x0C |
| 2327 | ** '\r' 0x0D |
| 2328 | ** |
| 2329 | ** Whitespace characters have the 0x01 flag set. Decimal digits have the |
| 2330 | ** 0x2 flag set. Single byte printable characters have the 0x4 flag set. |
| 2331 | ** Alphabet characters have the 0x8 bit set. |
| 2332 | ** |
| 2333 | ** The special list characters have the 0x10 flag set |
| 2334 | ** |
| 2335 | ** { } [ ] \ ; ' " |
| 2336 | ** |
| @@ -2477,14 +2477,14 @@ | |
| 2477 | return z - zBegin; |
| 2478 | } |
| 2479 | |
| 2480 | /* |
| 2481 | ** Try to convert the string passed as arguments (z, n) to an integer. |
| 2482 | ** If successful, store the result in *piOut and return TH_OK. |
| 2483 | ** |
| 2484 | ** If the string cannot be converted to an integer, return TH_ERROR. |
| 2485 | ** If the interp argument is not NULL, leave an error message in the |
| 2486 | ** interpreter result too. |
| 2487 | */ |
| 2488 | int Th_ToInt(Th_Interp *interp, const char *z, int n, int *piOut){ |
| 2489 | int i = 0; |
| 2490 | int iOut = 0; |
| @@ -2512,20 +2512,20 @@ | |
| 2512 | return TH_OK; |
| 2513 | } |
| 2514 | |
| 2515 | /* |
| 2516 | ** Try to convert the string passed as arguments (z, n) to a double. |
| 2517 | ** If successful, store the result in *pfOut and return TH_OK. |
| 2518 | ** |
| 2519 | ** If the string cannot be converted to a double, return TH_ERROR. |
| 2520 | ** If the interp argument is not NULL, leave an error message in the |
| 2521 | ** interpreter result too. |
| 2522 | */ |
| 2523 | int Th_ToDouble( |
| 2524 | Th_Interp *interp, |
| 2525 | const char *z, |
| 2526 | int n, |
| 2527 | double *pfOut |
| 2528 | ){ |
| 2529 | if( !sqlite3IsNumber((const char *)z, 0) ){ |
| 2530 | Th_ErrorMessage(interp, "expected number, got: \"", z, n); |
| 2531 | return TH_ERROR; |
| @@ -2566,33 +2566,33 @@ | |
| 2566 | ** the double fVal and return TH_OK. |
| 2567 | */ |
| 2568 | int Th_SetResultDouble(Th_Interp *interp, double fVal){ |
| 2569 | int i; /* Iterator variable */ |
| 2570 | double v = fVal; /* Input value */ |
| 2571 | char zBuf[128]; /* Output buffer */ |
| 2572 | char *z = zBuf; /* Output cursor */ |
| 2573 | int iDot = 0; /* Digit after which to place decimal point */ |
| 2574 | int iExp = 0; /* Exponent (NN in eNN) */ |
| 2575 | const char *zExp; /* String representation of iExp */ |
| 2576 | |
| 2577 | /* Precision: */ |
| 2578 | #define INSIGNIFICANT 0.000000000001 |
| 2579 | #define ROUNDER 0.0000000000005 |
| 2580 | double insignificant = INSIGNIFICANT; |
| 2581 | |
| 2582 | /* If the real value is negative, write a '-' character to the |
| 2583 | * output and transform v to the corresponding positive number. |
| 2584 | */ |
| 2585 | if( v<0.0 ){ |
| 2586 | *z++ = '-'; |
| 2587 | v *= -1.0; |
| 2588 | } |
| 2589 | |
| 2590 | /* Normalize v to a value between 1.0 and 10.0. Integer |
| 2591 | * variable iExp is set to the exponent. i.e the original |
| 2592 | * value is (v * 10^iExp) (or the negative thereof). |
| 2593 | */ |
| 2594 | if( v>0.0 ){ |
| 2595 | while( (v+ROUNDER)>=10.0 ) { iExp++; v *= 0.1; } |
| 2596 | while( (v+ROUNDER)<1.0 ) { iExp--; v *= 10.0; } |
| 2597 | } |
| 2598 | v += ROUNDER; |
| 2599 |
| --- src/th.c | |
| +++ src/th.c | |
| @@ -1,8 +1,8 @@ | |
| 1 | |
| 2 | /* |
| 3 | ** The implementation of the TH core. This file contains the parser, and |
| 4 | ** the implementation of the interface in th.h. |
| 5 | */ |
| 6 | |
| 7 | #include "config.h" |
| 8 | #include "th.h" |
| @@ -16,11 +16,11 @@ | |
| 16 | /* |
| 17 | ** Interpreter structure. |
| 18 | */ |
| 19 | struct Th_Interp { |
| 20 | Th_Vtab *pVtab; /* Copy of the argument passed to Th_CreateInterp() */ |
| 21 | char *zResult; /* Current interpreter result (Th_Malloc()ed) */ |
| 22 | int nResult; /* number of bytes in zResult */ |
| 23 | Th_Hash *paCmd; /* Table of registered commands */ |
| 24 | Th_Frame *pFrame; /* Current execution frame */ |
| 25 | int isListMode; /* True if thSplitList() should operate in "list" mode */ |
| 26 | }; |
| @@ -42,25 +42,25 @@ | |
| 42 | ** are stored in the Th_Frame.paVar hash table member of the associated |
| 43 | ** stack frame object. |
| 44 | ** |
| 45 | ** When an interpreter is created, a single Th_Frame structure is also |
| 46 | ** allocated - the global variable scope. Th_Interp.pFrame (the current |
| 47 | ** interpreter frame) is initialised to point to this Th_Frame. It is |
| 48 | ** not deleted for the lifetime of the interpreter (because the global |
| 49 | ** frame never goes out of scope). |
| 50 | ** |
| 51 | ** New stack frames are created by the Th_InFrame() function. Before |
| 52 | ** invoking its callback function, Th_InFrame() allocates a new Th_Frame |
| 53 | ** structure with pCaller set to the current frame (Th_Interp.pFrame), |
| 54 | ** and sets the current frame to the new frame object. After the callback |
| 55 | ** has been invoked, the allocated Th_Frame is deleted and the value |
| 56 | ** of the current frame pointer restored. |
| 57 | ** |
| 58 | ** By default, the Th_SetVar(), Th_UnsetVar() and Th_GetVar() functions |
| 59 | ** access variable values in the current frame. If they need to access |
| 60 | ** the global frame, they do so by traversing the pCaller pointer list. |
| 61 | ** Likewise, the Th_LinkVar() function uses the pCaller pointers to |
| 62 | ** link to variables located in the global or other stack frames. |
| 63 | */ |
| 64 | struct Th_Frame { |
| 65 | Th_Hash *paVar; /* Variables defined in this scope */ |
| 66 | Th_Frame *pCaller; /* Calling frame */ |
| @@ -84,11 +84,11 @@ | |
| 84 | ** value. |
| 85 | */ |
| 86 | struct Th_Variable { |
| 87 | int nRef; /* Number of references to this structure */ |
| 88 | int nData; /* Number of bytes at Th_Variable.zData */ |
| 89 | char *zData; /* Data for scalar variables */ |
| 90 | Th_Hash *pHash; /* Data for array variables */ |
| 91 | }; |
| 92 | |
| 93 | /* |
| 94 | ** Hash table API: |
| @@ -110,19 +110,19 @@ | |
| 110 | static void thFreeVariable(Th_HashEntry*, void*); |
| 111 | static void thFreeCommand(Th_HashEntry*, void*); |
| 112 | |
| 113 | /* |
| 114 | ** The following are used by both the expression and language parsers. |
| 115 | ** Given that the start of the input string (z, n) is a language |
| 116 | ** construct of the relevant type (a command enclosed in [], an escape |
| 117 | ** sequence etc.), these functions determine the number of bytes |
| 118 | ** of the input consumed by the construct. For example: |
| 119 | ** |
| 120 | ** int nByte; |
| 121 | ** thNextCommand(interp, "[expr $a+1] $nIter", 18, &nByte); |
| 122 | ** |
| 123 | ** results in variable nByte being set to 11. Or, |
| 124 | ** |
| 125 | ** thNextVarname(interp, "$a+1", 4, &nByte); |
| 126 | ** |
| 127 | ** results in nByte being set to 2. |
| 128 | */ |
| @@ -132,24 +132,24 @@ | |
| 132 | static int thNextNumber (Th_Interp*, const char *z, int n, int *pN); |
| 133 | static int thNextSpace (Th_Interp*, const char *z, int n, int *pN); |
| 134 | |
| 135 | /* |
| 136 | ** Given that the input string (z, n) contains a language construct of |
| 137 | ** the relevant type (a command enclosed in [], an escape sequence |
| 138 | ** like "\xFF" or a variable reference like "${varname}", perform |
| 139 | ** substitution on the string and store the resulting string in |
| 140 | ** the interpreter result. |
| 141 | */ |
| 142 | static int thSubstCommand(Th_Interp*, const char *z, int n); |
| 143 | static int thSubstEscape (Th_Interp*, const char *z, int n); |
| 144 | static int thSubstVarname(Th_Interp*, const char *z, int n); |
| 145 | |
| 146 | /* |
| 147 | ** Given that there is a th1 word located at the start of the input |
| 148 | ** string (z, n), determine the length in bytes of that word. If the |
| 149 | ** isCmd argument is non-zero, then an unescaped ";" byte not |
| 150 | ** located inside of a block or quoted string is considered to mark |
| 151 | ** the end of the word. |
| 152 | */ |
| 153 | static int thNextWord(Th_Interp*, const char *z, int n, int *pN, int isCmd); |
| 154 | |
| 155 | /* |
| @@ -176,13 +176,13 @@ | |
| 176 | ** Append nAdd bytes of content copied from zAdd to the end of buffer |
| 177 | ** pBuffer. If there is not enough space currently allocated, resize |
| 178 | ** the allocation to make space. |
| 179 | */ |
| 180 | static int thBufferWrite( |
| 181 | Th_Interp *interp, |
| 182 | Buffer *pBuffer, |
| 183 | const char *zAdd, |
| 184 | int nAdd |
| 185 | ){ |
| 186 | int nReq; |
| 187 | |
| 188 | if( nAdd<0 ){ |
| @@ -311,19 +311,19 @@ | |
| 311 | Th_HashDelete(interp, pFrame->paVar); |
| 312 | interp->pFrame = pFrame->pCaller; |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | ** The first part of the string (zInput,nInput) contains an escape |
| 317 | ** sequence. Set *pnEscape to the number of bytes in the escape sequence. |
| 318 | ** If there is a parse error, return TH_ERROR and set the interpreter |
| 319 | ** result to an error message. Otherwise return TH_OK. |
| 320 | */ |
| 321 | static int thNextEscape( |
| 322 | Th_Interp *interp, |
| 323 | const char *zInput, |
| 324 | int nInput, |
| 325 | int *pnEscape |
| 326 | ){ |
| 327 | int i = 2; |
| 328 | |
| 329 | assert(nInput>0); |
| @@ -344,18 +344,18 @@ | |
| 344 | return TH_OK; |
| 345 | } |
| 346 | |
| 347 | /* |
| 348 | ** The first part of the string (zInput,nInput) contains a variable |
| 349 | ** reference. Set *pnVarname to the number of bytes in the variable |
| 350 | ** reference. If there is a parse error, return TH_ERROR and set the |
| 351 | ** interpreter result to an error message. Otherwise return TH_OK. |
| 352 | */ |
| 353 | int thNextVarname( |
| 354 | Th_Interp *interp, |
| 355 | const char *zInput, |
| 356 | int nInput, |
| 357 | int *pnVarname |
| 358 | ){ |
| 359 | int i; |
| 360 | |
| 361 | assert(nInput>0); |
| @@ -401,19 +401,19 @@ | |
| 401 | return TH_OK; |
| 402 | } |
| 403 | |
| 404 | /* |
| 405 | ** The first part of the string (zInput,nInput) contains a command |
| 406 | ** enclosed in a "[]" block. Set *pnCommand to the number of bytes in |
| 407 | ** the variable reference. If there is a parse error, return TH_ERROR |
| 408 | ** and set the interpreter result to an error message. Otherwise return |
| 409 | ** TH_OK. |
| 410 | */ |
| 411 | int thNextCommand( |
| 412 | Th_Interp *interp, |
| 413 | const char *zInput, |
| 414 | int nInput, |
| 415 | int *pnCommand |
| 416 | ){ |
| 417 | int nBrace = 0; |
| 418 | int nSquare = 0; |
| 419 | int i; |
| @@ -438,17 +438,17 @@ | |
| 438 | |
| 439 | return TH_OK; |
| 440 | } |
| 441 | |
| 442 | /* |
| 443 | ** Set *pnSpace to the number of whitespace bytes at the start of |
| 444 | ** input string (zInput, nInput). Always return TH_OK. |
| 445 | */ |
| 446 | int thNextSpace( |
| 447 | Th_Interp *interp, |
| 448 | const char *zInput, |
| 449 | int nInput, |
| 450 | int *pnSpace |
| 451 | ){ |
| 452 | int i; |
| 453 | for(i=0; i<nInput && th_isspace(zInput[i]); i++); |
| 454 | *pnSpace = i; |
| @@ -457,21 +457,21 @@ | |
| 457 | |
| 458 | /* |
| 459 | ** The first byte of the string (zInput,nInput) is not white-space. |
| 460 | ** Set *pnWord to the number of bytes in the th1 word that starts |
| 461 | ** with this byte. If a complete word cannot be parsed or some other |
| 462 | ** error occurs, return TH_ERROR and set the interpreter result to |
| 463 | ** an error message. Otherwise return TH_OK. |
| 464 | ** |
| 465 | ** If the isCmd argument is non-zero, then an unescaped ";" byte not |
| 466 | ** located inside of a block or quoted string is considered to mark |
| 467 | ** the end of the word. |
| 468 | */ |
| 469 | static int thNextWord( |
| 470 | Th_Interp *interp, |
| 471 | const char *zInput, |
| 472 | int nInput, |
| 473 | int *pnWord, |
| 474 | int isCmd |
| 475 | ){ |
| 476 | int iEnd = 0; |
| 477 | |
| @@ -531,12 +531,12 @@ | |
| 531 | return thEvalLocal(interp, &zWord[1], nWord-2); |
| 532 | } |
| 533 | |
| 534 | /* |
| 535 | ** The input string (zWord, nWord) contains a th1 variable reference |
| 536 | ** (a '$' byte followed by a variable name). Perform substitution on |
| 537 | ** the input string and store the resulting string in the interpreter |
| 538 | ** result. |
| 539 | */ |
| 540 | static int thSubstVarname( |
| 541 | Th_Interp *interp, |
| 542 | const char *zWord, |
| @@ -572,11 +572,11 @@ | |
| 572 | return Th_GetVar(interp, &zWord[1], nWord-1); |
| 573 | } |
| 574 | |
| 575 | /* |
| 576 | ** The input string (zWord, nWord) contains a th1 escape sequence. |
| 577 | ** Perform substitution on the input string and store the resulting |
| 578 | ** string in the interpreter result. |
| 579 | */ |
| 580 | static int thSubstEscape( |
| 581 | Th_Interp *interp, |
| 582 | const char *zWord, |
| @@ -608,11 +608,11 @@ | |
| 608 | return TH_OK; |
| 609 | } |
| 610 | |
| 611 | /* |
| 612 | ** The input string (zWord, nWord) contains a th1 word. Perform |
| 613 | ** substitution on the input string and store the resulting |
| 614 | ** string in the interpreter result. |
| 615 | */ |
| 616 | static int thSubstWord( |
| 617 | Th_Interp *interp, |
| 618 | const char *zWord, |
| @@ -640,20 +640,20 @@ | |
| 640 | int (*xGet)(Th_Interp *, const char*, int, int *) = 0; |
| 641 | int (*xSubst)(Th_Interp *, const char*, int) = 0; |
| 642 | |
| 643 | switch( zWord[i] ){ |
| 644 | case '\\': |
| 645 | xGet = thNextEscape; xSubst = thSubstEscape; |
| 646 | break; |
| 647 | case '[': |
| 648 | if( !interp->isListMode ){ |
| 649 | xGet = thNextCommand; xSubst = thSubstCommand; |
| 650 | break; |
| 651 | } |
| 652 | case '$': |
| 653 | if( !interp->isListMode ){ |
| 654 | xGet = thNextVarname; xSubst = thSubstVarname; |
| 655 | break; |
| 656 | } |
| 657 | default: { |
| 658 | thBufferWrite(interp, &output, &zWord[i], 1); |
| 659 | continue; /* Go to the next iteration of the for(...) loop */ |
| @@ -685,11 +685,11 @@ | |
| 685 | ** Return true if one of the following is true of the buffer pointed |
| 686 | ** to by zInput, length nInput: |
| 687 | ** |
| 688 | ** + It is empty, or |
| 689 | ** + It contains nothing but white-space, or |
| 690 | ** + It contains no non-white-space characters before the first |
| 691 | ** newline character. |
| 692 | ** |
| 693 | ** Otherwise return false. |
| 694 | */ |
| 695 | static int thEndOfLine(const char *zInput, int nInput){ |
| @@ -725,16 +725,16 @@ | |
| 725 | ** // Free all memory allocated by Th_SplitList(). The arrays pointed |
| 726 | ** // to by argv and argl are invalidated by this call. |
| 727 | ** // |
| 728 | ** Th_Free(interp, argv); |
| 729 | ** |
| 730 | */ |
| 731 | static int thSplitList( |
| 732 | Th_Interp *interp, /* Interpreter context */ |
| 733 | const char *zList, /* Pointer to buffer containing input list */ |
| 734 | int nList, /* Size of buffer pointed to by zList */ |
| 735 | char ***pazElem, /* OUT: Array of list elements */ |
| 736 | int **panElem, /* OUT: Lengths of each list element */ |
| 737 | int *pnCount /* OUT: Number of list elements */ |
| 738 | ){ |
| 739 | int rc = TH_OK; |
| 740 | |
| @@ -774,14 +774,14 @@ | |
| 774 | assert((lenbuf.nBuf/sizeof(int))==nCount); |
| 775 | |
| 776 | assert((pazElem && panElem) || (!pazElem && !panElem)); |
| 777 | if( pazElem && rc==TH_OK ){ |
| 778 | int i; |
| 779 | char *zElem; |
| 780 | int *anElem; |
| 781 | char **azElem = Th_Malloc(interp, |
| 782 | sizeof(char*) * nCount + /* azElem */ |
| 783 | sizeof(int) * nCount + /* anElem */ |
| 784 | strbuf.nBuf /* space for list element strings */ |
| 785 | ); |
| 786 | anElem = (int *)&azElem[nCount]; |
| 787 | zElem = (char *)&anElem[nCount]; |
| @@ -795,11 +795,11 @@ | |
| 795 | *panElem = anElem; |
| 796 | } |
| 797 | if( pnCount ){ |
| 798 | *pnCount = nCount; |
| 799 | } |
| 800 | |
| 801 | finish: |
| 802 | thBufferFree(interp, &strbuf); |
| 803 | thBufferFree(interp, &lenbuf); |
| 804 | return rc; |
| 805 | } |
| @@ -876,18 +876,18 @@ | |
| 876 | if( rc==TH_OK ){ |
| 877 | Th_Command *p = (Th_Command *)(pEntry->pData); |
| 878 | const char **azArg = (const char **)argv; |
| 879 | rc = p->xProc(interp, p->pContext, argc, azArg, argl); |
| 880 | } |
| 881 | |
| 882 | /* If an error occurred, add this command to the stack trace report. */ |
| 883 | if( rc==TH_ERROR ){ |
| 884 | char *zRes; |
| 885 | int nRes; |
| 886 | char *zStack = 0; |
| 887 | int nStack = 0; |
| 888 | |
| 889 | zRes = Th_TakeResult(interp, &nRes); |
| 890 | if( TH_OK==Th_GetVar(interp, (char *)"::th_stack_trace", -1) ){ |
| 891 | zStack = Th_TakeResult(interp, &nStack); |
| 892 | } |
| 893 | Th_ListAppend(interp, &zStack, &nStack, zFirst, zInput-zFirst); |
| @@ -912,15 +912,15 @@ | |
| 912 | ** |
| 913 | ** Argument iFrame is interpreted as follows: |
| 914 | ** |
| 915 | ** * If iFrame is 0, this means the current frame. |
| 916 | ** |
| 917 | ** * If iFrame is negative, then the nth frame up the stack, where |
| 918 | ** n is the absolute value of iFrame. A value of -1 means the |
| 919 | ** calling procedure. |
| 920 | ** |
| 921 | ** * If iFrame is +ve, then the nth frame from the bottom of the |
| 922 | ** stack. An iFrame value of 1 means the toplevel (global) frame. |
| 923 | */ |
| 924 | static Th_Frame *getFrame(Th_Interp *interp, int iFrame){ |
| 925 | Th_Frame *p = interp->pFrame; |
| 926 | int i; |
| @@ -948,28 +948,28 @@ | |
| 948 | |
| 949 | |
| 950 | /* |
| 951 | ** Evaluate th1 script (zProgram, nProgram) in the frame identified by |
| 952 | ** argument iFrame. Leave either an error message or a result in the |
| 953 | ** interpreter result and return a th1 error code (TH_OK, TH_ERROR, |
| 954 | ** TH_RETURN, TH_CONTINUE or TH_BREAK). |
| 955 | */ |
| 956 | int Th_Eval(Th_Interp *interp, int iFrame, const char *zProgram, int nProgram){ |
| 957 | int rc = TH_OK; |
| 958 | Th_Frame *pSavedFrame = interp->pFrame; |
| 959 | |
| 960 | /* Set Th_Interp.pFrame to the frame that this script is to be |
| 961 | ** evaluated in. The current frame is saved in pSavedFrame and will |
| 962 | ** be restored before this function returns. |
| 963 | */ |
| 964 | interp->pFrame = getFrame(interp, iFrame); |
| 965 | |
| 966 | if( !interp->pFrame ){ |
| 967 | rc = TH_ERROR; |
| 968 | }else{ |
| 969 | int nInput = nProgram; |
| 970 | |
| 971 | if( nInput<0 ){ |
| 972 | nInput = th_strlen(zProgram); |
| 973 | } |
| 974 | rc = thEvalLocal(interp, zProgram, nInput); |
| 975 | } |
| @@ -995,13 +995,13 @@ | |
| 995 | ** array key name. |
| 996 | */ |
| 997 | static int thAnalyseVarname( |
| 998 | const char *zVarname, |
| 999 | int nVarname, |
| 1000 | const char **pzOuter, /* OUT: Pointer to scalar/array name */ |
| 1001 | int *pnOuter, /* OUT: Number of bytes at *pzOuter */ |
| 1002 | const char **pzInner, /* OUT: Pointer to array key (or null) */ |
| 1003 | int *pnInner, /* OUT: Number of bytes at *pzInner */ |
| 1004 | int *pisGlobal /* OUT: Set to true if this is a global ref */ |
| 1005 | ){ |
| 1006 | const char *zOuter = zVarname; |
| 1007 | int nOuter; |
| @@ -1044,11 +1044,11 @@ | |
| 1044 | return TH_OK; |
| 1045 | } |
| 1046 | |
| 1047 | /* |
| 1048 | ** Input string (zVar, nVar) contains a variable name. This function locates |
| 1049 | ** the Th_Variable structure associated with the named variable. The |
| 1050 | ** variable name may be a global or local scalar or array variable |
| 1051 | ** |
| 1052 | ** If the create argument is non-zero and the named variable does not exist |
| 1053 | ** it is created. Otherwise, an error is left in the interpreter result |
| 1054 | ** and NULL returned. |
| @@ -1057,11 +1057,11 @@ | |
| 1057 | ** an error is left in the interpreter result and NULL returned. If |
| 1058 | ** arrayok is true an array name is Ok. |
| 1059 | */ |
| 1060 | static Th_Variable *thFindValue( |
| 1061 | Th_Interp *interp, |
| 1062 | const char *zVar, /* Pointer to variable name */ |
| 1063 | int nVar, /* Number of bytes at nVar */ |
| 1064 | int create, /* If true, create the variable if not found */ |
| 1065 | int arrayok, /* If true, an array is Ok. Otherwise array==error */ |
| 1066 | int noerror /* If false, set interpreter result to error message */ |
| 1067 | ){ |
| @@ -1135,12 +1135,12 @@ | |
| 1135 | } |
| 1136 | return 0; |
| 1137 | } |
| 1138 | |
| 1139 | /* |
| 1140 | ** String (zVar, nVar) must contain the name of a scalar variable or |
| 1141 | ** array member. Look up the variable, store its current value in |
| 1142 | ** the interpreter result and return TH_OK. |
| 1143 | ** |
| 1144 | ** If the named variable does not exist, return TH_ERROR and leave |
| 1145 | ** an error message in the interpreter result. |
| 1146 | */ |
| @@ -1174,12 +1174,12 @@ | |
| 1174 | ** |
| 1175 | ** If (zVar, nVar) refers to an existing array, TH_ERROR is returned |
| 1176 | ** and an error message left in the interpreter result. |
| 1177 | */ |
| 1178 | int Th_SetVar( |
| 1179 | Th_Interp *interp, |
| 1180 | const char *zVar, |
| 1181 | int nVar, |
| 1182 | const char *zValue, |
| 1183 | int nValue |
| 1184 | ){ |
| 1185 | Th_Variable *pValue; |
| @@ -1210,13 +1210,13 @@ | |
| 1210 | ** Create a variable link so that accessing variable (zLocal, nLocal) is |
| 1211 | ** the same as accessing variable (zLink, nLink) in stack frame iFrame. |
| 1212 | */ |
| 1213 | int Th_LinkVar( |
| 1214 | Th_Interp *interp, /* Interpreter */ |
| 1215 | const char *zLocal, int nLocal, /* Local varname */ |
| 1216 | int iFrame, /* Stack frame of linked var */ |
| 1217 | const char *zLink, int nLink /* Linked varname */ |
| 1218 | ){ |
| 1219 | Th_Frame *pSavedFrame = interp->pFrame; |
| 1220 | Th_Frame *pFrame; |
| 1221 | Th_HashEntry *pEntry; |
| 1222 | Th_Variable *pValue; |
| @@ -1299,11 +1299,11 @@ | |
| 1299 | if( interp ){ |
| 1300 | char *zRes = 0; |
| 1301 | int nRes = 0; |
| 1302 | |
| 1303 | Th_SetVar(interp, (char *)"::th_stack_trace", -1, 0, 0); |
| 1304 | |
| 1305 | Th_StringAppend(interp, &zRes, &nRes, zPre, -1); |
| 1306 | if( zRes[nRes-1]=='"' ){ |
| 1307 | Th_StringAppend(interp, &zRes, &nRes, z, n); |
| 1308 | Th_StringAppend(interp, &zRes, &nRes, (const char *)"\"", 1); |
| 1309 | }else{ |
| @@ -1381,12 +1381,12 @@ | |
| 1381 | return (char *)Th_Malloc(pInterp, 1); |
| 1382 | } |
| 1383 | } |
| 1384 | |
| 1385 | |
| 1386 | /* |
| 1387 | ** Wrappers around the supplied malloc() and free() |
| 1388 | */ |
| 1389 | void *Th_Malloc(Th_Interp *pInterp, int nByte){ |
| 1390 | void *p = pInterp->pVtab->xMalloc(nByte); |
| 1391 | if( p ){ |
| 1392 | memset(p, 0, nByte); |
| @@ -1398,16 +1398,16 @@ | |
| 1398 | pInterp->pVtab->xFree(z); |
| 1399 | } |
| 1400 | } |
| 1401 | |
| 1402 | /* |
| 1403 | ** Install a new th1 command. |
| 1404 | ** |
| 1405 | ** If a command of the same name already exists, it is deleted automatically. |
| 1406 | */ |
| 1407 | int Th_CreateCommand( |
| 1408 | Th_Interp *interp, |
| 1409 | const char *zName, /* New command name */ |
| 1410 | Th_CommandProc xProc, /* Command callback proc */ |
| 1411 | void *pContext, /* Value to pass as second arg to xProc */ |
| 1412 | void (*xDel)(Th_Interp *, void *) /* Command destructor callback */ |
| 1413 | ){ |
| @@ -1425,27 +1425,27 @@ | |
| 1425 | } |
| 1426 | pCommand->xProc = xProc; |
| 1427 | pCommand->pContext = pContext; |
| 1428 | pCommand->xDel = xDel; |
| 1429 | pEntry->pData = (void *)pCommand; |
| 1430 | |
| 1431 | return TH_OK; |
| 1432 | } |
| 1433 | |
| 1434 | /* |
| 1435 | ** Rename the existing command (zName, nName) to (zNew, nNew). If nNew is 0, |
| 1436 | ** the command is deleted instead of renamed. |
| 1437 | ** |
| 1438 | ** If successful, TH_OK is returned. If command zName does not exist, or |
| 1439 | ** if command zNew already exists, an error message is left in the |
| 1440 | ** interpreter result and TH_ERROR is returned. |
| 1441 | */ |
| 1442 | int Th_RenameCommand( |
| 1443 | Th_Interp *interp, |
| 1444 | const char *zName, /* Existing command name */ |
| 1445 | int nName, /* Number of bytes at zName */ |
| 1446 | const char *zNew, /* New command name */ |
| 1447 | int nNew /* Number of bytes at zNew */ |
| 1448 | ){ |
| 1449 | Th_HashEntry *pEntry; |
| 1450 | Th_HashEntry *pNewEntry; |
| 1451 | |
| @@ -1499,11 +1499,11 @@ | |
| 1499 | ** If an error occurs (if (zList, nList) is not a valid list) an error |
| 1500 | ** message is left in the interpreter result and TH_ERROR returned. |
| 1501 | ** |
| 1502 | ** If successful, *pnCount is set to the number of elements in the list. |
| 1503 | ** panElem is set to point at an array of *pnCount integers - the lengths |
| 1504 | ** of the element values. *pazElem is set to point at an array of |
| 1505 | ** pointers to buffers containing the array element's data. |
| 1506 | ** |
| 1507 | ** To free the arrays allocated at *pazElem and *panElem, the caller |
| 1508 | ** should call Th_Free() on *pazElem only. Exactly one such call to |
| 1509 | ** Th_Free() must be made per call to Th_SplitList(). |
| @@ -1525,13 +1525,13 @@ | |
| 1525 | ** Th_Free(interp, azElem); |
| 1526 | ** |
| 1527 | */ |
| 1528 | int Th_SplitList( |
| 1529 | Th_Interp *interp, |
| 1530 | const char *zList, /* Pointer to buffer containing list */ |
| 1531 | int nList, /* Number of bytes at zList */ |
| 1532 | char ***pazElem, /* OUT: Array of pointers to element data */ |
| 1533 | int **panElem, /* OUT: Array of element data lengths */ |
| 1534 | int *pnCount /* OUT: Number of elements in list */ |
| 1535 | ){ |
| 1536 | int rc; |
| 1537 | interp->isListMode = 1; |
| @@ -1542,16 +1542,16 @@ | |
| 1542 | } |
| 1543 | return rc; |
| 1544 | } |
| 1545 | |
| 1546 | /* |
| 1547 | ** Append a new element to an existing th1 list. The element to append |
| 1548 | ** to the list is (zElem, nElem). |
| 1549 | ** |
| 1550 | ** A pointer to the existing list must be stored at *pzList when this |
| 1551 | ** function is called. The length must be stored in *pnList. The value |
| 1552 | ** of *pzList must either be NULL (in which case *pnList must be 0), or |
| 1553 | ** a pointer to memory obtained from Th_Malloc(). |
| 1554 | ** |
| 1555 | ** This function calls Th_Free() to free the buffer at *pzList and sets |
| 1556 | ** *pzList to point to a new buffer containing the new list value. *pnList |
| 1557 | ** is similarly updated before returning. The return value is always TH_OK. |
| @@ -1568,13 +1568,13 @@ | |
| 1568 | ** Th_Free(interp, zList); |
| 1569 | ** |
| 1570 | */ |
| 1571 | int Th_ListAppend( |
| 1572 | Th_Interp *interp, /* Interpreter context */ |
| 1573 | char **pzList, /* IN/OUT: Ptr to ptr to list */ |
| 1574 | int *pnList, /* IN/OUT: Current length of *pzList */ |
| 1575 | const char *zElem, /* Data to append */ |
| 1576 | int nElem /* Length of nElem */ |
| 1577 | ){ |
| 1578 | Buffer output; |
| 1579 | int i; |
| 1580 | |
| @@ -1623,13 +1623,13 @@ | |
| 1623 | ** Append a new element to an existing th1 string. This function uses |
| 1624 | ** the same interface as the Th_ListAppend() function. |
| 1625 | */ |
| 1626 | int Th_StringAppend( |
| 1627 | Th_Interp *interp, /* Interpreter context */ |
| 1628 | char **pzStr, /* IN/OUT: Ptr to ptr to list */ |
| 1629 | int *pnStr, /* IN/OUT: Current length of *pzStr */ |
| 1630 | const char *zElem, /* Data to append */ |
| 1631 | int nElem /* Length of nElem */ |
| 1632 | ){ |
| 1633 | char *zNew; |
| 1634 | int nNew; |
| 1635 | |
| @@ -1647,11 +1647,11 @@ | |
| 1647 | *pnStr = nNew; |
| 1648 | |
| 1649 | return TH_OK; |
| 1650 | } |
| 1651 | |
| 1652 | /* |
| 1653 | ** Delete an interpreter. |
| 1654 | */ |
| 1655 | void Th_DeleteInterp(Th_Interp *interp){ |
| 1656 | assert(interp->pFrame); |
| 1657 | assert(0==interp->pFrame->pCaller); |
| @@ -1668,11 +1668,11 @@ | |
| 1668 | |
| 1669 | /* Delete the interpreter structure itself. */ |
| 1670 | Th_Free(interp, (void *)interp); |
| 1671 | } |
| 1672 | |
| 1673 | /* |
| 1674 | ** Create a new interpreter. |
| 1675 | */ |
| 1676 | Th_Interp * Th_CreateInterp(Th_Vtab *pVtab){ |
| 1677 | Th_Interp *p; |
| 1678 | |
| @@ -1702,11 +1702,11 @@ | |
| 1702 | Operator *pOp; |
| 1703 | Expr *pParent; |
| 1704 | Expr *pLeft; |
| 1705 | Expr *pRight; |
| 1706 | |
| 1707 | char *zValue; /* Pointer to literal value */ |
| 1708 | int nValue; /* Length of literal value buffer */ |
| 1709 | }; |
| 1710 | |
| 1711 | /* Unary operators */ |
| 1712 | #define OP_UNARY_MINUS 2 |
| @@ -1758,11 +1758,11 @@ | |
| 1758 | {"+", OP_UNARY_PLUS, 1, ARG_NUMBER}, |
| 1759 | {"~", OP_BITWISE_NOT, 1, ARG_INTEGER}, |
| 1760 | {"!", OP_LOGICAL_NOT, 1, ARG_INTEGER}, |
| 1761 | |
| 1762 | /* Binary operators. It is important to the parsing in Th_Expr() that |
| 1763 | * the two-character symbols ("==") appear before the one-character |
| 1764 | * ones ("="). And that the priorities of all binary operators are |
| 1765 | * integers between 2 and 12. |
| 1766 | */ |
| 1767 | {"<<", OP_LEFTSHIFT, 4, ARG_INTEGER}, |
| 1768 | {">>", OP_RIGHTSHIFT, 4, ARG_INTEGER}, |
| @@ -1789,16 +1789,16 @@ | |
| 1789 | {0,0,0,0} |
| 1790 | }; |
| 1791 | |
| 1792 | /* |
| 1793 | ** The first part of the string (zInput,nInput) contains a number. |
| 1794 | ** Set *pnVarname to the number of bytes in the numeric string. |
| 1795 | */ |
| 1796 | static int thNextNumber( |
| 1797 | Th_Interp *interp, |
| 1798 | const char *zInput, |
| 1799 | int nInput, |
| 1800 | int *pnLiteral |
| 1801 | ){ |
| 1802 | int i; |
| 1803 | int seenDot = 0; |
| 1804 | for(i=0; i<nInput; i++){ |
| @@ -1864,11 +1864,11 @@ | |
| 1864 | if( eArgType==ARG_NUMBER ){ |
| 1865 | if( (zLeft==0 || TH_OK==Th_ToInt(0, zLeft, nLeft, &iLeft)) |
| 1866 | && (zRight==0 || TH_OK==Th_ToInt(0, zRight, nRight, &iRight)) |
| 1867 | ){ |
| 1868 | eArgType = ARG_INTEGER; |
| 1869 | }else if( |
| 1870 | (zLeft && TH_OK!=Th_ToDouble(interp, zLeft, nLeft, &fLeft)) || |
| 1871 | (zRight && TH_OK!=Th_ToDouble(interp, zRight, nRight, &fRight)) |
| 1872 | ){ |
| 1873 | /* A type error. */ |
| 1874 | rc = TH_ERROR; |
| @@ -1876,11 +1876,11 @@ | |
| 1876 | }else if( eArgType==ARG_INTEGER ){ |
| 1877 | rc = Th_ToInt(interp, zLeft, nLeft, &iLeft); |
| 1878 | if( rc==TH_OK && zRight ){ |
| 1879 | rc = Th_ToInt(interp, zRight, nRight, &iRight); |
| 1880 | } |
| 1881 | } |
| 1882 | } |
| 1883 | |
| 1884 | if( rc==TH_OK && eArgType==ARG_INTEGER ){ |
| 1885 | int iRes = 0; |
| 1886 | switch( pExpr->pOp->eOp ) { |
| @@ -1978,11 +1978,11 @@ | |
| 1978 | #define ISTERM(x) (apToken[x] && (!apToken[x]->pOp || apToken[x]->pLeft)) |
| 1979 | |
| 1980 | for(jj=0; jj<nToken; jj++){ |
| 1981 | if( apToken[jj]->pOp && apToken[jj]->pOp->eOp==OP_OPEN_BRACKET ){ |
| 1982 | int nNest = 1; |
| 1983 | int iLeft = jj; |
| 1984 | |
| 1985 | for(jj++; jj<nToken; jj++){ |
| 1986 | Operator *pOp = apToken[jj]->pOp; |
| 1987 | if( pOp && pOp->eOp==OP_OPEN_BRACKET ) nNest++; |
| 1988 | if( pOp && pOp->eOp==OP_CLOSE_BRACKET ) nNest--; |
| @@ -2052,11 +2052,11 @@ | |
| 2052 | /* |
| 2053 | ** Parse a string containing a TH expression to a list of tokens. |
| 2054 | */ |
| 2055 | static int exprParse( |
| 2056 | Th_Interp *interp, /* Interpreter to leave error message in */ |
| 2057 | const char *zExpr, /* Pointer to input string */ |
| 2058 | int nExpr, /* Number of bytes at zExpr */ |
| 2059 | Expr ***papToken, /* OUT: Array of tokens. */ |
| 2060 | int *pnToken /* OUT: Size of token array */ |
| 2061 | ){ |
| 2062 | int i; |
| @@ -2127,11 +2127,11 @@ | |
| 2127 | memcpy(pNew->zValue, z, pNew->nValue); |
| 2128 | i += pNew->nValue; |
| 2129 | } |
| 2130 | if( (nToken%16)==0 ){ |
| 2131 | /* Grow the apToken array. */ |
| 2132 | Expr **apTokenOld = apToken; |
| 2133 | apToken = Th_Malloc(interp, sizeof(Expr *)*(nToken+16)); |
| 2134 | memcpy(apToken, apTokenOld, sizeof(Expr *)*nToken); |
| 2135 | } |
| 2136 | |
| 2137 | /* Put the new token at the end of the apToken array */ |
| @@ -2152,11 +2152,11 @@ | |
| 2152 | /* |
| 2153 | ** Evaluate the string (zExpr, nExpr) as a Th expression. Store |
| 2154 | ** the result in the interpreter interp and return TH_OK if |
| 2155 | ** successful. If an error occurs, store an error message in |
| 2156 | ** the interpreter result and return an error code. |
| 2157 | */ |
| 2158 | int Th_Expr(Th_Interp *interp, const char *zExpr, int nExpr){ |
| 2159 | int rc; /* Return Code */ |
| 2160 | int i; /* Loop counter */ |
| 2161 | |
| 2162 | int nToken = 0; |
| @@ -2169,11 +2169,11 @@ | |
| 2169 | /* Parse the expression to a list of tokens. */ |
| 2170 | rc = exprParse(interp, zExpr, nExpr, &apToken, &nToken); |
| 2171 | |
| 2172 | /* If the parsing was successful, create an expression tree from |
| 2173 | ** the parsed list of tokens. If successful, apToken[0] is set |
| 2174 | ** to point to the root of the expression tree. |
| 2175 | */ |
| 2176 | if( rc==TH_OK ){ |
| 2177 | rc = exprMakeTree(interp, apToken, nToken); |
| 2178 | } |
| 2179 | |
| @@ -2210,11 +2210,11 @@ | |
| 2210 | ** the callback function xCallback for each entry. The second argument |
| 2211 | ** passed to xCallback is a copy of the fourth argument passed to this |
| 2212 | ** function. |
| 2213 | */ |
| 2214 | void Th_HashIterate( |
| 2215 | Th_Interp *interp, |
| 2216 | Th_Hash *pHash, |
| 2217 | void (*xCallback)(Th_HashEntry *pEntry, void *pContext), |
| 2218 | void *pContext |
| 2219 | ){ |
| 2220 | int i; |
| @@ -2244,14 +2244,14 @@ | |
| 2244 | Th_Free(interp, pHash); |
| 2245 | } |
| 2246 | } |
| 2247 | |
| 2248 | /* |
| 2249 | ** This function is used to insert or delete hash table items, or to |
| 2250 | ** query a hash table for an existing item. |
| 2251 | ** |
| 2252 | ** If parameter op is less than zero, then the hash-table element |
| 2253 | ** identified by (zKey, nKey) is removed from the hash-table if it |
| 2254 | ** exists. NULL is returned. |
| 2255 | ** |
| 2256 | ** Otherwise, if the hash-table contains an item with key (zKey, nKey), |
| 2257 | ** a pointer to the associated Th_HashEntry is returned. If parameter |
| @@ -2258,11 +2258,11 @@ | |
| 2258 | ** op is greater than zero, then a new entry is added if one cannot |
| 2259 | ** be found. If op is zero, then NULL is returned if the item is |
| 2260 | ** not already present in the hash-table. |
| 2261 | */ |
| 2262 | Th_HashEntry *Th_HashFind( |
| 2263 | Th_Interp *interp, |
| 2264 | Th_Hash *pHash, |
| 2265 | const char *zKey, |
| 2266 | int nKey, |
| 2267 | int op /* -ve = delete, 0 = find, +ve = insert */ |
| 2268 | ){ |
| @@ -2326,11 +2326,11 @@ | |
| 2326 | ** '\f' 0x0C |
| 2327 | ** '\r' 0x0D |
| 2328 | ** |
| 2329 | ** Whitespace characters have the 0x01 flag set. Decimal digits have the |
| 2330 | ** 0x2 flag set. Single byte printable characters have the 0x4 flag set. |
| 2331 | ** Alphabet characters have the 0x8 bit set. |
| 2332 | ** |
| 2333 | ** The special list characters have the 0x10 flag set |
| 2334 | ** |
| 2335 | ** { } [ ] \ ; ' " |
| 2336 | ** |
| @@ -2477,14 +2477,14 @@ | |
| 2477 | return z - zBegin; |
| 2478 | } |
| 2479 | |
| 2480 | /* |
| 2481 | ** Try to convert the string passed as arguments (z, n) to an integer. |
| 2482 | ** If successful, store the result in *piOut and return TH_OK. |
| 2483 | ** |
| 2484 | ** If the string cannot be converted to an integer, return TH_ERROR. |
| 2485 | ** If the interp argument is not NULL, leave an error message in the |
| 2486 | ** interpreter result too. |
| 2487 | */ |
| 2488 | int Th_ToInt(Th_Interp *interp, const char *z, int n, int *piOut){ |
| 2489 | int i = 0; |
| 2490 | int iOut = 0; |
| @@ -2512,20 +2512,20 @@ | |
| 2512 | return TH_OK; |
| 2513 | } |
| 2514 | |
| 2515 | /* |
| 2516 | ** Try to convert the string passed as arguments (z, n) to a double. |
| 2517 | ** If successful, store the result in *pfOut and return TH_OK. |
| 2518 | ** |
| 2519 | ** If the string cannot be converted to a double, return TH_ERROR. |
| 2520 | ** If the interp argument is not NULL, leave an error message in the |
| 2521 | ** interpreter result too. |
| 2522 | */ |
| 2523 | int Th_ToDouble( |
| 2524 | Th_Interp *interp, |
| 2525 | const char *z, |
| 2526 | int n, |
| 2527 | double *pfOut |
| 2528 | ){ |
| 2529 | if( !sqlite3IsNumber((const char *)z, 0) ){ |
| 2530 | Th_ErrorMessage(interp, "expected number, got: \"", z, n); |
| 2531 | return TH_ERROR; |
| @@ -2566,33 +2566,33 @@ | |
| 2566 | ** the double fVal and return TH_OK. |
| 2567 | */ |
| 2568 | int Th_SetResultDouble(Th_Interp *interp, double fVal){ |
| 2569 | int i; /* Iterator variable */ |
| 2570 | double v = fVal; /* Input value */ |
| 2571 | char zBuf[128]; /* Output buffer */ |
| 2572 | char *z = zBuf; /* Output cursor */ |
| 2573 | int iDot = 0; /* Digit after which to place decimal point */ |
| 2574 | int iExp = 0; /* Exponent (NN in eNN) */ |
| 2575 | const char *zExp; /* String representation of iExp */ |
| 2576 | |
| 2577 | /* Precision: */ |
| 2578 | #define INSIGNIFICANT 0.000000000001 |
| 2579 | #define ROUNDER 0.0000000000005 |
| 2580 | double insignificant = INSIGNIFICANT; |
| 2581 | |
| 2582 | /* If the real value is negative, write a '-' character to the |
| 2583 | * output and transform v to the corresponding positive number. |
| 2584 | */ |
| 2585 | if( v<0.0 ){ |
| 2586 | *z++ = '-'; |
| 2587 | v *= -1.0; |
| 2588 | } |
| 2589 | |
| 2590 | /* Normalize v to a value between 1.0 and 10.0. Integer |
| 2591 | * variable iExp is set to the exponent. i.e the original |
| 2592 | * value is (v * 10^iExp) (or the negative thereof). |
| 2593 | */ |
| 2594 | if( v>0.0 ){ |
| 2595 | while( (v+ROUNDER)>=10.0 ) { iExp++; v *= 0.1; } |
| 2596 | while( (v+ROUNDER)<1.0 ) { iExp--; v *= 10.0; } |
| 2597 | } |
| 2598 | v += ROUNDER; |
| 2599 |
M
src/th.h
+22
-22
| --- src/th.h | ||
| +++ src/th.h | ||
| @@ -18,70 +18,70 @@ | ||
| 18 | 18 | /* |
| 19 | 19 | ** Opaque handle for interpeter. |
| 20 | 20 | */ |
| 21 | 21 | typedef struct Th_Interp Th_Interp; |
| 22 | 22 | |
| 23 | -/* | |
| 24 | -** Create and delete interpreters. | |
| 23 | +/* | |
| 24 | +** Create and delete interpreters. | |
| 25 | 25 | */ |
| 26 | 26 | Th_Interp * Th_CreateInterp(Th_Vtab *pVtab); |
| 27 | 27 | void Th_DeleteInterp(Th_Interp *); |
| 28 | 28 | |
| 29 | -/* | |
| 29 | +/* | |
| 30 | 30 | ** Evaluate an TH program in the stack frame identified by parameter |
| 31 | 31 | ** iFrame, according to the following rules: |
| 32 | 32 | ** |
| 33 | 33 | ** * If iFrame is 0, this means the current frame. |
| 34 | 34 | ** |
| 35 | -** * If iFrame is negative, then the nth frame up the stack, where n is | |
| 35 | +** * If iFrame is negative, then the nth frame up the stack, where n is | |
| 36 | 36 | ** the absolute value of iFrame. A value of -1 means the calling |
| 37 | 37 | ** procedure. |
| 38 | 38 | ** |
| 39 | 39 | ** * If iFrame is +ve, then the nth frame from the bottom of the stack. |
| 40 | 40 | ** An iFrame value of 1 means the toplevel (global) frame. |
| 41 | 41 | */ |
| 42 | 42 | int Th_Eval(Th_Interp *interp, int iFrame, const char *zProg, int nProg); |
| 43 | 43 | |
| 44 | 44 | /* |
| 45 | -** Evaluate a TH expression. The result is stored in the | |
| 45 | +** Evaluate a TH expression. The result is stored in the | |
| 46 | 46 | ** interpreter result. |
| 47 | 47 | */ |
| 48 | 48 | int Th_Expr(Th_Interp *interp, const char *, int); |
| 49 | 49 | |
| 50 | -/* | |
| 50 | +/* | |
| 51 | 51 | ** Access TH variables in the current stack frame. If the variable name |
| 52 | -** begins with "::", the lookup is in the top level (global) frame. | |
| 52 | +** begins with "::", the lookup is in the top level (global) frame. | |
| 53 | 53 | */ |
| 54 | 54 | int Th_ExistsVar(Th_Interp *, const char *, int); |
| 55 | 55 | int Th_GetVar(Th_Interp *, const char *, int); |
| 56 | 56 | int Th_SetVar(Th_Interp *, const char *, int, const char *, int); |
| 57 | 57 | int Th_LinkVar(Th_Interp *, const char *, int, int, const char *, int); |
| 58 | 58 | int Th_UnsetVar(Th_Interp *, const char *, int); |
| 59 | 59 | |
| 60 | 60 | typedef int (*Th_CommandProc)(Th_Interp *, void *, int, const char **, int *); |
| 61 | 61 | |
| 62 | -/* | |
| 63 | -** Register new commands. | |
| 62 | +/* | |
| 63 | +** Register new commands. | |
| 64 | 64 | */ |
| 65 | 65 | int Th_CreateCommand( |
| 66 | - Th_Interp *interp, | |
| 67 | - const char *zName, | |
| 66 | + Th_Interp *interp, | |
| 67 | + const char *zName, | |
| 68 | 68 | /* int (*xProc)(Th_Interp *, void *, int, const char **, int *), */ |
| 69 | 69 | Th_CommandProc xProc, |
| 70 | 70 | void *pContext, |
| 71 | 71 | void (*xDel)(Th_Interp *, void *) |
| 72 | 72 | ); |
| 73 | 73 | |
| 74 | -/* | |
| 74 | +/* | |
| 75 | 75 | ** Delete or rename commands. |
| 76 | 76 | */ |
| 77 | 77 | int Th_RenameCommand(Th_Interp *, const char *, int, const char *, int); |
| 78 | 78 | |
| 79 | -/* | |
| 80 | -** Push a new stack frame (local variable context) onto the interpreter | |
| 81 | -** stack, call the function supplied as parameter xCall with the two | |
| 82 | -** context arguments, | |
| 79 | +/* | |
| 80 | +** Push a new stack frame (local variable context) onto the interpreter | |
| 81 | +** stack, call the function supplied as parameter xCall with the two | |
| 82 | +** context arguments, | |
| 83 | 83 | ** |
| 84 | 84 | ** xCall(interp, pContext1, pContext2) |
| 85 | 85 | ** |
| 86 | 86 | ** , then pop the frame off of the interpreter stack. The value returned |
| 87 | 87 | ** by the xCall() function is returned as the result of this function. |
| @@ -93,21 +93,21 @@ | ||
| 93 | 93 | int (*xCall)(Th_Interp *, void *pContext1, void *pContext2), |
| 94 | 94 | void *pContext1, |
| 95 | 95 | void *pContext2 |
| 96 | 96 | ); |
| 97 | 97 | |
| 98 | -/* | |
| 98 | +/* | |
| 99 | 99 | ** Valid return codes for xProc callbacks. |
| 100 | 100 | */ |
| 101 | 101 | #define TH_OK 0 |
| 102 | 102 | #define TH_ERROR 1 |
| 103 | 103 | #define TH_BREAK 2 |
| 104 | 104 | #define TH_RETURN 3 |
| 105 | 105 | #define TH_CONTINUE 4 |
| 106 | 106 | |
| 107 | -/* | |
| 108 | -** Set and get the interpreter result. | |
| 107 | +/* | |
| 108 | +** Set and get the interpreter result. | |
| 109 | 109 | */ |
| 110 | 110 | int Th_SetResult(Th_Interp *, const char *, int); |
| 111 | 111 | const char *Th_GetResult(Th_Interp *, int *); |
| 112 | 112 | char *Th_TakeResult(Th_Interp *, int *); |
| 113 | 113 | |
| @@ -115,26 +115,26 @@ | ||
| 115 | 115 | ** Set an error message as the interpreter result. This also |
| 116 | 116 | ** sets the global stack-trace variable $::th_stack_trace. |
| 117 | 117 | */ |
| 118 | 118 | int Th_ErrorMessage(Th_Interp *, const char *, const char *, int); |
| 119 | 119 | |
| 120 | -/* | |
| 120 | +/* | |
| 121 | 121 | ** Access the memory management functions associated with the specified |
| 122 | 122 | ** interpreter. |
| 123 | 123 | */ |
| 124 | 124 | void *Th_Malloc(Th_Interp *, int); |
| 125 | 125 | void Th_Free(Th_Interp *, void *); |
| 126 | 126 | |
| 127 | -/* | |
| 127 | +/* | |
| 128 | 128 | ** Functions for handling TH lists. |
| 129 | 129 | */ |
| 130 | 130 | int Th_ListAppend(Th_Interp *, char **, int *, const char *, int); |
| 131 | 131 | int Th_SplitList(Th_Interp *, const char *, int, char ***, int **, int *); |
| 132 | 132 | |
| 133 | 133 | int Th_StringAppend(Th_Interp *, char **, int *, const char *, int); |
| 134 | 134 | |
| 135 | -/* | |
| 135 | +/* | |
| 136 | 136 | ** Functions for handling numbers and pointers. |
| 137 | 137 | */ |
| 138 | 138 | int Th_ToInt(Th_Interp *, const char *, int, int *); |
| 139 | 139 | int Th_ToDouble(Th_Interp *, const char *, int, double *); |
| 140 | 140 | int Th_SetResultInt(Th_Interp *, int); |
| 141 | 141 |
| --- src/th.h | |
| +++ src/th.h | |
| @@ -18,70 +18,70 @@ | |
| 18 | /* |
| 19 | ** Opaque handle for interpeter. |
| 20 | */ |
| 21 | typedef struct Th_Interp Th_Interp; |
| 22 | |
| 23 | /* |
| 24 | ** Create and delete interpreters. |
| 25 | */ |
| 26 | Th_Interp * Th_CreateInterp(Th_Vtab *pVtab); |
| 27 | void Th_DeleteInterp(Th_Interp *); |
| 28 | |
| 29 | /* |
| 30 | ** Evaluate an TH program in the stack frame identified by parameter |
| 31 | ** iFrame, according to the following rules: |
| 32 | ** |
| 33 | ** * If iFrame is 0, this means the current frame. |
| 34 | ** |
| 35 | ** * If iFrame is negative, then the nth frame up the stack, where n is |
| 36 | ** the absolute value of iFrame. A value of -1 means the calling |
| 37 | ** procedure. |
| 38 | ** |
| 39 | ** * If iFrame is +ve, then the nth frame from the bottom of the stack. |
| 40 | ** An iFrame value of 1 means the toplevel (global) frame. |
| 41 | */ |
| 42 | int Th_Eval(Th_Interp *interp, int iFrame, const char *zProg, int nProg); |
| 43 | |
| 44 | /* |
| 45 | ** Evaluate a TH expression. The result is stored in the |
| 46 | ** interpreter result. |
| 47 | */ |
| 48 | int Th_Expr(Th_Interp *interp, const char *, int); |
| 49 | |
| 50 | /* |
| 51 | ** Access TH variables in the current stack frame. If the variable name |
| 52 | ** begins with "::", the lookup is in the top level (global) frame. |
| 53 | */ |
| 54 | int Th_ExistsVar(Th_Interp *, const char *, int); |
| 55 | int Th_GetVar(Th_Interp *, const char *, int); |
| 56 | int Th_SetVar(Th_Interp *, const char *, int, const char *, int); |
| 57 | int Th_LinkVar(Th_Interp *, const char *, int, int, const char *, int); |
| 58 | int Th_UnsetVar(Th_Interp *, const char *, int); |
| 59 | |
| 60 | typedef int (*Th_CommandProc)(Th_Interp *, void *, int, const char **, int *); |
| 61 | |
| 62 | /* |
| 63 | ** Register new commands. |
| 64 | */ |
| 65 | int Th_CreateCommand( |
| 66 | Th_Interp *interp, |
| 67 | const char *zName, |
| 68 | /* int (*xProc)(Th_Interp *, void *, int, const char **, int *), */ |
| 69 | Th_CommandProc xProc, |
| 70 | void *pContext, |
| 71 | void (*xDel)(Th_Interp *, void *) |
| 72 | ); |
| 73 | |
| 74 | /* |
| 75 | ** Delete or rename commands. |
| 76 | */ |
| 77 | int Th_RenameCommand(Th_Interp *, const char *, int, const char *, int); |
| 78 | |
| 79 | /* |
| 80 | ** Push a new stack frame (local variable context) onto the interpreter |
| 81 | ** stack, call the function supplied as parameter xCall with the two |
| 82 | ** context arguments, |
| 83 | ** |
| 84 | ** xCall(interp, pContext1, pContext2) |
| 85 | ** |
| 86 | ** , then pop the frame off of the interpreter stack. The value returned |
| 87 | ** by the xCall() function is returned as the result of this function. |
| @@ -93,21 +93,21 @@ | |
| 93 | int (*xCall)(Th_Interp *, void *pContext1, void *pContext2), |
| 94 | void *pContext1, |
| 95 | void *pContext2 |
| 96 | ); |
| 97 | |
| 98 | /* |
| 99 | ** Valid return codes for xProc callbacks. |
| 100 | */ |
| 101 | #define TH_OK 0 |
| 102 | #define TH_ERROR 1 |
| 103 | #define TH_BREAK 2 |
| 104 | #define TH_RETURN 3 |
| 105 | #define TH_CONTINUE 4 |
| 106 | |
| 107 | /* |
| 108 | ** Set and get the interpreter result. |
| 109 | */ |
| 110 | int Th_SetResult(Th_Interp *, const char *, int); |
| 111 | const char *Th_GetResult(Th_Interp *, int *); |
| 112 | char *Th_TakeResult(Th_Interp *, int *); |
| 113 | |
| @@ -115,26 +115,26 @@ | |
| 115 | ** Set an error message as the interpreter result. This also |
| 116 | ** sets the global stack-trace variable $::th_stack_trace. |
| 117 | */ |
| 118 | int Th_ErrorMessage(Th_Interp *, const char *, const char *, int); |
| 119 | |
| 120 | /* |
| 121 | ** Access the memory management functions associated with the specified |
| 122 | ** interpreter. |
| 123 | */ |
| 124 | void *Th_Malloc(Th_Interp *, int); |
| 125 | void Th_Free(Th_Interp *, void *); |
| 126 | |
| 127 | /* |
| 128 | ** Functions for handling TH lists. |
| 129 | */ |
| 130 | int Th_ListAppend(Th_Interp *, char **, int *, const char *, int); |
| 131 | int Th_SplitList(Th_Interp *, const char *, int, char ***, int **, int *); |
| 132 | |
| 133 | int Th_StringAppend(Th_Interp *, char **, int *, const char *, int); |
| 134 | |
| 135 | /* |
| 136 | ** Functions for handling numbers and pointers. |
| 137 | */ |
| 138 | int Th_ToInt(Th_Interp *, const char *, int, int *); |
| 139 | int Th_ToDouble(Th_Interp *, const char *, int, double *); |
| 140 | int Th_SetResultInt(Th_Interp *, int); |
| 141 |
| --- src/th.h | |
| +++ src/th.h | |
| @@ -18,70 +18,70 @@ | |
| 18 | /* |
| 19 | ** Opaque handle for interpeter. |
| 20 | */ |
| 21 | typedef struct Th_Interp Th_Interp; |
| 22 | |
| 23 | /* |
| 24 | ** Create and delete interpreters. |
| 25 | */ |
| 26 | Th_Interp * Th_CreateInterp(Th_Vtab *pVtab); |
| 27 | void Th_DeleteInterp(Th_Interp *); |
| 28 | |
| 29 | /* |
| 30 | ** Evaluate an TH program in the stack frame identified by parameter |
| 31 | ** iFrame, according to the following rules: |
| 32 | ** |
| 33 | ** * If iFrame is 0, this means the current frame. |
| 34 | ** |
| 35 | ** * If iFrame is negative, then the nth frame up the stack, where n is |
| 36 | ** the absolute value of iFrame. A value of -1 means the calling |
| 37 | ** procedure. |
| 38 | ** |
| 39 | ** * If iFrame is +ve, then the nth frame from the bottom of the stack. |
| 40 | ** An iFrame value of 1 means the toplevel (global) frame. |
| 41 | */ |
| 42 | int Th_Eval(Th_Interp *interp, int iFrame, const char *zProg, int nProg); |
| 43 | |
| 44 | /* |
| 45 | ** Evaluate a TH expression. The result is stored in the |
| 46 | ** interpreter result. |
| 47 | */ |
| 48 | int Th_Expr(Th_Interp *interp, const char *, int); |
| 49 | |
| 50 | /* |
| 51 | ** Access TH variables in the current stack frame. If the variable name |
| 52 | ** begins with "::", the lookup is in the top level (global) frame. |
| 53 | */ |
| 54 | int Th_ExistsVar(Th_Interp *, const char *, int); |
| 55 | int Th_GetVar(Th_Interp *, const char *, int); |
| 56 | int Th_SetVar(Th_Interp *, const char *, int, const char *, int); |
| 57 | int Th_LinkVar(Th_Interp *, const char *, int, int, const char *, int); |
| 58 | int Th_UnsetVar(Th_Interp *, const char *, int); |
| 59 | |
| 60 | typedef int (*Th_CommandProc)(Th_Interp *, void *, int, const char **, int *); |
| 61 | |
| 62 | /* |
| 63 | ** Register new commands. |
| 64 | */ |
| 65 | int Th_CreateCommand( |
| 66 | Th_Interp *interp, |
| 67 | const char *zName, |
| 68 | /* int (*xProc)(Th_Interp *, void *, int, const char **, int *), */ |
| 69 | Th_CommandProc xProc, |
| 70 | void *pContext, |
| 71 | void (*xDel)(Th_Interp *, void *) |
| 72 | ); |
| 73 | |
| 74 | /* |
| 75 | ** Delete or rename commands. |
| 76 | */ |
| 77 | int Th_RenameCommand(Th_Interp *, const char *, int, const char *, int); |
| 78 | |
| 79 | /* |
| 80 | ** Push a new stack frame (local variable context) onto the interpreter |
| 81 | ** stack, call the function supplied as parameter xCall with the two |
| 82 | ** context arguments, |
| 83 | ** |
| 84 | ** xCall(interp, pContext1, pContext2) |
| 85 | ** |
| 86 | ** , then pop the frame off of the interpreter stack. The value returned |
| 87 | ** by the xCall() function is returned as the result of this function. |
| @@ -93,21 +93,21 @@ | |
| 93 | int (*xCall)(Th_Interp *, void *pContext1, void *pContext2), |
| 94 | void *pContext1, |
| 95 | void *pContext2 |
| 96 | ); |
| 97 | |
| 98 | /* |
| 99 | ** Valid return codes for xProc callbacks. |
| 100 | */ |
| 101 | #define TH_OK 0 |
| 102 | #define TH_ERROR 1 |
| 103 | #define TH_BREAK 2 |
| 104 | #define TH_RETURN 3 |
| 105 | #define TH_CONTINUE 4 |
| 106 | |
| 107 | /* |
| 108 | ** Set and get the interpreter result. |
| 109 | */ |
| 110 | int Th_SetResult(Th_Interp *, const char *, int); |
| 111 | const char *Th_GetResult(Th_Interp *, int *); |
| 112 | char *Th_TakeResult(Th_Interp *, int *); |
| 113 | |
| @@ -115,26 +115,26 @@ | |
| 115 | ** Set an error message as the interpreter result. This also |
| 116 | ** sets the global stack-trace variable $::th_stack_trace. |
| 117 | */ |
| 118 | int Th_ErrorMessage(Th_Interp *, const char *, const char *, int); |
| 119 | |
| 120 | /* |
| 121 | ** Access the memory management functions associated with the specified |
| 122 | ** interpreter. |
| 123 | */ |
| 124 | void *Th_Malloc(Th_Interp *, int); |
| 125 | void Th_Free(Th_Interp *, void *); |
| 126 | |
| 127 | /* |
| 128 | ** Functions for handling TH lists. |
| 129 | */ |
| 130 | int Th_ListAppend(Th_Interp *, char **, int *, const char *, int); |
| 131 | int Th_SplitList(Th_Interp *, const char *, int, char ***, int **, int *); |
| 132 | |
| 133 | int Th_StringAppend(Th_Interp *, char **, int *, const char *, int); |
| 134 | |
| 135 | /* |
| 136 | ** Functions for handling numbers and pointers. |
| 137 | */ |
| 138 | int Th_ToInt(Th_Interp *, const char *, int, int *); |
| 139 | int Th_ToDouble(Th_Interp *, const char *, int, double *); |
| 140 | int Th_SetResultInt(Th_Interp *, int); |
| 141 |
+113
-113
| --- src/th_lang.c | ||
| +++ src/th_lang.c | ||
| @@ -1,12 +1,12 @@ | ||
| 1 | 1 | |
| 2 | 2 | /* |
| 3 | -** This file contains the implementation of all of the TH language | |
| 4 | -** built-in commands. | |
| 3 | +** This file contains the implementation of all of the TH language | |
| 4 | +** built-in commands. | |
| 5 | 5 | ** |
| 6 | -** All built-in commands are implemented using the public interface | |
| 7 | -** declared in th.h, so this file serves as both a part of the language | |
| 6 | +** All built-in commands are implemented using the public interface | |
| 7 | +** declared in th.h, so this file serves as both a part of the language | |
| 8 | 8 | ** implementation and an example of how to extend the language with |
| 9 | 9 | ** new commands. |
| 10 | 10 | */ |
| 11 | 11 | |
| 12 | 12 | #include "config.h" |
| @@ -18,19 +18,19 @@ | ||
| 18 | 18 | Th_ErrorMessage(interp, "wrong # args: should be \"", zMsg, -1); |
| 19 | 19 | return TH_ERROR; |
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | /* |
| 23 | -** Syntax: | |
| 23 | +** Syntax: | |
| 24 | 24 | ** |
| 25 | 25 | ** catch script ?varname? |
| 26 | 26 | */ |
| 27 | 27 | static int catch_command( |
| 28 | - Th_Interp *interp, | |
| 29 | - void *ctx, | |
| 30 | - int argc, | |
| 31 | - const char **argv, | |
| 28 | + Th_Interp *interp, | |
| 29 | + void *ctx, | |
| 30 | + int argc, | |
| 31 | + const char **argv, | |
| 32 | 32 | int *argl |
| 33 | 33 | ){ |
| 34 | 34 | int rc; |
| 35 | 35 | |
| 36 | 36 | if( argc!=2 && argc!=3 ){ |
| @@ -47,19 +47,19 @@ | ||
| 47 | 47 | Th_SetResultInt(interp, rc); |
| 48 | 48 | return TH_OK; |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | /* |
| 52 | -** TH Syntax: | |
| 52 | +** TH Syntax: | |
| 53 | 53 | ** |
| 54 | 54 | ** if expr1 body1 ?elseif expr2 body2? ? ?else? bodyN? |
| 55 | 55 | */ |
| 56 | 56 | static int if_command( |
| 57 | - Th_Interp *interp, | |
| 58 | - void *ctx, | |
| 59 | - int argc, | |
| 60 | - const char **argv, | |
| 57 | + Th_Interp *interp, | |
| 58 | + void *ctx, | |
| 59 | + int argc, | |
| 60 | + const char **argv, | |
| 61 | 61 | int *argl |
| 62 | 62 | ){ |
| 63 | 63 | int rc = TH_OK; |
| 64 | 64 | |
| 65 | 65 | int iCond; /* Result of evaluating expression */ |
| @@ -94,19 +94,19 @@ | ||
| 94 | 94 | wrong_args: |
| 95 | 95 | return Th_WrongNumArgs(interp, "if ..."); |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | /* |
| 99 | -** TH Syntax: | |
| 99 | +** TH Syntax: | |
| 100 | 100 | ** |
| 101 | 101 | ** expr expr |
| 102 | 102 | */ |
| 103 | 103 | static int expr_command( |
| 104 | - Th_Interp *interp, | |
| 105 | - void *ctx, | |
| 106 | - int argc, | |
| 107 | - const char **argv, | |
| 104 | + Th_Interp *interp, | |
| 105 | + void *ctx, | |
| 106 | + int argc, | |
| 107 | + const char **argv, | |
| 108 | 108 | int *argl |
| 109 | 109 | ){ |
| 110 | 110 | if( argc!=2 ){ |
| 111 | 111 | return Th_WrongNumArgs(interp, "expr expression"); |
| 112 | 112 | } |
| @@ -113,11 +113,11 @@ | ||
| 113 | 113 | |
| 114 | 114 | return Th_Expr(interp, argv[1], argl[1]); |
| 115 | 115 | } |
| 116 | 116 | |
| 117 | 117 | /* |
| 118 | -** Evaluate the th1 script (zBody, nBody) in the local stack frame. | |
| 118 | +** Evaluate the th1 script (zBody, nBody) in the local stack frame. | |
| 119 | 119 | ** Return the result of the evaluation, except if the result |
| 120 | 120 | ** is TH_CONTINUE, return TH_OK instead. |
| 121 | 121 | */ |
| 122 | 122 | static int eval_loopbody(Th_Interp *interp, const char *zBody, int nBody){ |
| 123 | 123 | int rc = Th_Eval(interp, 0, zBody, nBody); |
| @@ -126,19 +126,19 @@ | ||
| 126 | 126 | } |
| 127 | 127 | return rc; |
| 128 | 128 | } |
| 129 | 129 | |
| 130 | 130 | /* |
| 131 | -** TH Syntax: | |
| 131 | +** TH Syntax: | |
| 132 | 132 | ** |
| 133 | 133 | ** for init condition incr script |
| 134 | 134 | */ |
| 135 | 135 | static int for_command( |
| 136 | - Th_Interp *interp, | |
| 137 | - void *ctx, | |
| 138 | - int argc, | |
| 139 | - const char **argv, | |
| 136 | + Th_Interp *interp, | |
| 137 | + void *ctx, | |
| 138 | + int argc, | |
| 139 | + const char **argv, | |
| 140 | 140 | int *argl |
| 141 | 141 | ){ |
| 142 | 142 | int rc; |
| 143 | 143 | int iCond; |
| 144 | 144 | |
| @@ -147,11 +147,11 @@ | ||
| 147 | 147 | } |
| 148 | 148 | |
| 149 | 149 | /* Evaluate the 'init' script */ |
| 150 | 150 | rc = Th_Eval(interp, 0, argv[1], -1); |
| 151 | 151 | |
| 152 | - while( rc==TH_OK | |
| 152 | + while( rc==TH_OK | |
| 153 | 153 | && TH_OK==(rc = Th_Expr(interp, argv[2], -1)) |
| 154 | 154 | && TH_OK==(rc = Th_ToInt(interp, Th_GetResult(interp, 0), -1, &iCond)) |
| 155 | 155 | && iCond |
| 156 | 156 | && TH_OK==(rc = eval_loopbody(interp, argv[4], argl[4])) |
| 157 | 157 | ){ |
| @@ -161,45 +161,45 @@ | ||
| 161 | 161 | if( rc==TH_BREAK ) rc = TH_OK; |
| 162 | 162 | return rc; |
| 163 | 163 | } |
| 164 | 164 | |
| 165 | 165 | /* |
| 166 | -** TH Syntax: | |
| 166 | +** TH Syntax: | |
| 167 | 167 | ** |
| 168 | 168 | ** list ?arg1 ?arg2? ...? |
| 169 | 169 | */ |
| 170 | 170 | static int list_command( |
| 171 | - Th_Interp *interp, | |
| 172 | - void *ctx, | |
| 173 | - int argc, | |
| 174 | - const char **argv, | |
| 171 | + Th_Interp *interp, | |
| 172 | + void *ctx, | |
| 173 | + int argc, | |
| 174 | + const char **argv, | |
| 175 | 175 | int *argl |
| 176 | 176 | ){ |
| 177 | 177 | char *zList = 0; |
| 178 | 178 | int nList = 0; |
| 179 | 179 | int i; |
| 180 | 180 | |
| 181 | 181 | for(i=1; i<argc; i++){ |
| 182 | 182 | Th_ListAppend(interp, &zList, &nList, argv[i], argl[i]); |
| 183 | 183 | } |
| 184 | - | |
| 184 | + | |
| 185 | 185 | Th_SetResult(interp, zList, nList); |
| 186 | 186 | Th_Free(interp, zList); |
| 187 | 187 | |
| 188 | 188 | return TH_OK; |
| 189 | 189 | } |
| 190 | 190 | |
| 191 | 191 | /* |
| 192 | -** TH Syntax: | |
| 192 | +** TH Syntax: | |
| 193 | 193 | ** |
| 194 | 194 | ** lindex list index |
| 195 | 195 | */ |
| 196 | 196 | static int lindex_command( |
| 197 | - Th_Interp *interp, | |
| 198 | - void *ctx, | |
| 199 | - int argc, | |
| 200 | - const char **argv, | |
| 197 | + Th_Interp *interp, | |
| 198 | + void *ctx, | |
| 199 | + int argc, | |
| 200 | + const char **argv, | |
| 201 | 201 | int *argl |
| 202 | 202 | ){ |
| 203 | 203 | int iElem; |
| 204 | 204 | int rc; |
| 205 | 205 | |
| @@ -227,19 +227,19 @@ | ||
| 227 | 227 | |
| 228 | 228 | return rc; |
| 229 | 229 | } |
| 230 | 230 | |
| 231 | 231 | /* |
| 232 | -** TH Syntax: | |
| 232 | +** TH Syntax: | |
| 233 | 233 | ** |
| 234 | 234 | ** llength list |
| 235 | 235 | */ |
| 236 | 236 | static int llength_command( |
| 237 | - Th_Interp *interp, | |
| 238 | - void *ctx, | |
| 239 | - int argc, | |
| 240 | - const char **argv, | |
| 237 | + Th_Interp *interp, | |
| 238 | + void *ctx, | |
| 239 | + int argc, | |
| 240 | + const char **argv, | |
| 241 | 241 | int *argl |
| 242 | 242 | ){ |
| 243 | 243 | int nElem; |
| 244 | 244 | int rc; |
| 245 | 245 | |
| @@ -254,19 +254,19 @@ | ||
| 254 | 254 | |
| 255 | 255 | return rc; |
| 256 | 256 | } |
| 257 | 257 | |
| 258 | 258 | /* |
| 259 | -** TH Syntax: | |
| 259 | +** TH Syntax: | |
| 260 | 260 | ** |
| 261 | 261 | ** set varname ?value? |
| 262 | 262 | */ |
| 263 | 263 | static int set_command( |
| 264 | - Th_Interp *interp, | |
| 265 | - void *ctx, | |
| 266 | - int argc, | |
| 267 | - const char **argv, | |
| 264 | + Th_Interp *interp, | |
| 265 | + void *ctx, | |
| 266 | + int argc, | |
| 267 | + const char **argv, | |
| 268 | 268 | int *argl |
| 269 | 269 | ){ |
| 270 | 270 | if( argc!=2 && argc!=3 ){ |
| 271 | 271 | return Th_WrongNumArgs(interp, "set varname ?value?"); |
| 272 | 272 | } |
| @@ -277,30 +277,30 @@ | ||
| 277 | 277 | return Th_GetVar(interp, argv[1], argl[1]); |
| 278 | 278 | } |
| 279 | 279 | |
| 280 | 280 | /* |
| 281 | 281 | ** When a new command is created using the built-in [proc] command, an |
| 282 | -** instance of the following structure is allocated and populated. A | |
| 283 | -** pointer to the structure is passed as the context (second) argument | |
| 282 | +** instance of the following structure is allocated and populated. A | |
| 283 | +** pointer to the structure is passed as the context (second) argument | |
| 284 | 284 | ** to function proc_call1() when the new command is executed. |
| 285 | 285 | */ |
| 286 | 286 | typedef struct ProcDefn ProcDefn; |
| 287 | 287 | struct ProcDefn { |
| 288 | 288 | int nParam; /* Number of formal (non "args") parameters */ |
| 289 | - char **azParam; /* Parameter names */ | |
| 289 | + char **azParam; /* Parameter names */ | |
| 290 | 290 | int *anParam; /* Lengths of parameter names */ |
| 291 | - char **azDefault; /* Default values */ | |
| 291 | + char **azDefault; /* Default values */ | |
| 292 | 292 | int *anDefault; /* Lengths of default values */ |
| 293 | 293 | int hasArgs; /* True if there is an "args" parameter */ |
| 294 | - char *zProgram; /* Body of proc */ | |
| 294 | + char *zProgram; /* Body of proc */ | |
| 295 | 295 | int nProgram; /* Number of bytes at zProgram */ |
| 296 | - char *zUsage; /* Usage message */ | |
| 296 | + char *zUsage; /* Usage message */ | |
| 297 | 297 | int nUsage; /* Number of bytes at zUsage */ |
| 298 | 298 | }; |
| 299 | 299 | |
| 300 | -/* This structure is used to temporarily store arguments passed to an | |
| 301 | -** invocation of a command created using [proc]. A pointer to an | |
| 300 | +/* This structure is used to temporarily store arguments passed to an | |
| 301 | +** invocation of a command created using [proc]. A pointer to an | |
| 302 | 302 | ** instance is passed as the second argument to the proc_call2() function. |
| 303 | 303 | */ |
| 304 | 304 | typedef struct ProcArgs ProcArgs; |
| 305 | 305 | struct ProcArgs { |
| 306 | 306 | int argc; |
| @@ -323,11 +323,11 @@ | ||
| 323 | 323 | ProcArgs *pArgs = (ProcArgs *)pContext2; |
| 324 | 324 | |
| 325 | 325 | /* Check if there are the right number of arguments. If there are |
| 326 | 326 | ** not, generate a usage message for the command. |
| 327 | 327 | */ |
| 328 | - if( (pArgs->argc>(p->nParam+1) && !p->hasArgs) | |
| 328 | + if( (pArgs->argc>(p->nParam+1) && !p->hasArgs) | |
| 329 | 329 | || (pArgs->argc<=(p->nParam) && !p->azDefault[pArgs->argc-1]) |
| 330 | 330 | ){ |
| 331 | 331 | char *zUsage = 0; |
| 332 | 332 | int nUsage = 0; |
| 333 | 333 | Th_StringAppend(interp, &zUsage, &nUsage, pArgs->argv[0], pArgs->argl[0]); |
| @@ -374,12 +374,12 @@ | ||
| 374 | 374 | ** created using the [proc] command. The second argument, pContext, |
| 375 | 375 | ** is a pointer to the associated ProcDefn structure. |
| 376 | 376 | */ |
| 377 | 377 | static int proc_call1( |
| 378 | 378 | Th_Interp *interp, |
| 379 | - void *pContext, | |
| 380 | - int argc, | |
| 379 | + void *pContext, | |
| 380 | + int argc, | |
| 381 | 381 | const char **argv, |
| 382 | 382 | int *argl |
| 383 | 383 | ){ |
| 384 | 384 | int rc; |
| 385 | 385 | |
| @@ -400,32 +400,32 @@ | ||
| 400 | 400 | } |
| 401 | 401 | return rc; |
| 402 | 402 | } |
| 403 | 403 | |
| 404 | 404 | /* |
| 405 | -** This function is registered as the delete callback for all commands | |
| 406 | -** created using the built-in [proc] command. It is called automatically | |
| 407 | -** when a command created using [proc] is deleted. | |
| 405 | +** This function is registered as the delete callback for all commands | |
| 406 | +** created using the built-in [proc] command. It is called automatically | |
| 407 | +** when a command created using [proc] is deleted. | |
| 408 | 408 | ** |
| 409 | 409 | ** It frees the ProcDefn structure allocated when the command was created. |
| 410 | -*/ | |
| 410 | +*/ | |
| 411 | 411 | static void proc_del(Th_Interp *interp, void *pContext){ |
| 412 | 412 | ProcDefn *p = (ProcDefn *)pContext; |
| 413 | 413 | Th_Free(interp, (void *)p->zUsage); |
| 414 | 414 | Th_Free(interp, (void *)p); |
| 415 | 415 | } |
| 416 | 416 | |
| 417 | 417 | /* |
| 418 | -** TH Syntax: | |
| 418 | +** TH Syntax: | |
| 419 | 419 | ** |
| 420 | 420 | ** proc name arglist code |
| 421 | 421 | */ |
| 422 | 422 | static int proc_command( |
| 423 | - Th_Interp *interp, | |
| 424 | - void *ctx, | |
| 423 | + Th_Interp *interp, | |
| 424 | + void *ctx, | |
| 425 | 425 | int argc, |
| 426 | - const char **argv, | |
| 426 | + const char **argv, | |
| 427 | 427 | int *argl |
| 428 | 428 | ){ |
| 429 | 429 | int rc; |
| 430 | 430 | char *zName; |
| 431 | 431 | |
| @@ -436,11 +436,11 @@ | ||
| 436 | 436 | |
| 437 | 437 | char **azParam; |
| 438 | 438 | int *anParam; |
| 439 | 439 | int nParam; |
| 440 | 440 | |
| 441 | - char *zUsage = 0; /* Build up a usage message here */ | |
| 441 | + char *zUsage = 0; /* Build up a usage message here */ | |
| 442 | 442 | int nUsage = 0; /* Number of bytes at zUsage */ |
| 443 | 443 | |
| 444 | 444 | if( argc!=4 ){ |
| 445 | 445 | return Th_WrongNumArgs(interp, "proc name arglist code"); |
| 446 | 446 | } |
| @@ -448,14 +448,14 @@ | ||
| 448 | 448 | return TH_ERROR; |
| 449 | 449 | } |
| 450 | 450 | |
| 451 | 451 | /* Allocate the new ProcDefn structure. */ |
| 452 | 452 | nByte = sizeof(ProcDefn) + /* ProcDefn structure */ |
| 453 | - (sizeof(char *) + sizeof(int)) * nParam + /* azParam, anParam */ | |
| 454 | - (sizeof(char *) + sizeof(int)) * nParam + /* azDefault, anDefault */ | |
| 453 | + (sizeof(char *) + sizeof(int)) * nParam + /* azParam, anParam */ | |
| 454 | + (sizeof(char *) + sizeof(int)) * nParam + /* azDefault, anDefault */ | |
| 455 | 455 | argl[3] + /* zProgram */ |
| 456 | - argl[2]; /* Space for copies of parameter names and default values */ | |
| 456 | + argl[2]; /* Space for copies of parameter names and default values */ | |
| 457 | 457 | p = (ProcDefn *)Th_Malloc(interp, nByte); |
| 458 | 458 | |
| 459 | 459 | /* If the last parameter in the parameter list is "args", then set the |
| 460 | 460 | ** ProcDefn.hasArgs flag. The "args" parameter does not require an |
| 461 | 461 | ** entry in the ProcDefn.azParam[] or ProcDefn.azDefault[] arrays. |
| @@ -472,11 +472,11 @@ | ||
| 472 | 472 | p->anDefault = (int *)&p->azDefault[nParam]; |
| 473 | 473 | p->zProgram = (char *)&p->anDefault[nParam]; |
| 474 | 474 | memcpy(p->zProgram, argv[3], argl[3]); |
| 475 | 475 | p->nProgram = argl[3]; |
| 476 | 476 | zSpace = &p->zProgram[p->nProgram]; |
| 477 | - | |
| 477 | + | |
| 478 | 478 | for(i=0; i<nParam; i++){ |
| 479 | 479 | char **az; |
| 480 | 480 | int *an; |
| 481 | 481 | int n; |
| 482 | 482 | if( Th_SplitList(interp, azParam[i], anParam[i], &az, &an, &n) ){ |
| @@ -537,40 +537,40 @@ | ||
| 537 | 537 | Th_Free(interp, zUsage); |
| 538 | 538 | return TH_ERROR; |
| 539 | 539 | } |
| 540 | 540 | |
| 541 | 541 | /* |
| 542 | -** TH Syntax: | |
| 542 | +** TH Syntax: | |
| 543 | 543 | ** |
| 544 | 544 | ** rename oldcmd newcmd |
| 545 | 545 | */ |
| 546 | 546 | static int rename_command( |
| 547 | - Th_Interp *interp, | |
| 548 | - void *ctx, | |
| 547 | + Th_Interp *interp, | |
| 548 | + void *ctx, | |
| 549 | 549 | int argc, |
| 550 | - const char **argv, | |
| 550 | + const char **argv, | |
| 551 | 551 | int *argl |
| 552 | 552 | ){ |
| 553 | 553 | if( argc!=3 ){ |
| 554 | 554 | return Th_WrongNumArgs(interp, "rename oldcmd newcmd"); |
| 555 | 555 | } |
| 556 | 556 | return Th_RenameCommand(interp, argv[1], argl[1], argv[2], argl[2]); |
| 557 | 557 | } |
| 558 | 558 | |
| 559 | 559 | /* |
| 560 | -** TH Syntax: | |
| 560 | +** TH Syntax: | |
| 561 | 561 | ** |
| 562 | 562 | ** break ?value...? |
| 563 | 563 | ** continue ?value...? |
| 564 | 564 | ** ok ?value...? |
| 565 | 565 | ** error ?value...? |
| 566 | 566 | */ |
| 567 | 567 | static int simple_command( |
| 568 | - Th_Interp *interp, | |
| 569 | - void *ctx, | |
| 570 | - int argc, | |
| 571 | - const char **argv, | |
| 568 | + Th_Interp *interp, | |
| 569 | + void *ctx, | |
| 570 | + int argc, | |
| 571 | + const char **argv, | |
| 572 | 572 | int *argl |
| 573 | 573 | ){ |
| 574 | 574 | if( argc!=1 && argc!=2 ){ |
| 575 | 575 | return Th_WrongNumArgs(interp, "return ?value?"); |
| 576 | 576 | } |
| @@ -579,19 +579,19 @@ | ||
| 579 | 579 | } |
| 580 | 580 | return FOSSIL_PTR_TO_INT(ctx); |
| 581 | 581 | } |
| 582 | 582 | |
| 583 | 583 | /* |
| 584 | -** TH Syntax: | |
| 584 | +** TH Syntax: | |
| 585 | 585 | ** |
| 586 | 586 | ** return ?-code code? ?value? |
| 587 | 587 | */ |
| 588 | 588 | static int return_command( |
| 589 | - Th_Interp *interp, | |
| 590 | - void *ctx, | |
| 591 | - int argc, | |
| 592 | - const char **argv, | |
| 589 | + Th_Interp *interp, | |
| 590 | + void *ctx, | |
| 591 | + int argc, | |
| 592 | + const char **argv, | |
| 593 | 593 | int *argl |
| 594 | 594 | ){ |
| 595 | 595 | int iCode = TH_RETURN; |
| 596 | 596 | if( argc<1 || argc>4 ){ |
| 597 | 597 | return Th_WrongNumArgs(interp, "return ?-code code? ?value?"); |
| @@ -638,11 +638,11 @@ | ||
| 638 | 638 | iRes = nLeft-nRight; |
| 639 | 639 | } |
| 640 | 640 | |
| 641 | 641 | if( iRes<0 ) iRes = -1; |
| 642 | 642 | if( iRes>0 ) iRes = 1; |
| 643 | - | |
| 643 | + | |
| 644 | 644 | return Th_SetResultInt(interp, iRes); |
| 645 | 645 | } |
| 646 | 646 | |
| 647 | 647 | /* |
| 648 | 648 | ** TH Syntax: |
| @@ -672,11 +672,11 @@ | ||
| 672 | 672 | if( 0==memcmp(zNeedle, &zHaystack[i], nNeedle) ){ |
| 673 | 673 | iRes = i; |
| 674 | 674 | break; |
| 675 | 675 | } |
| 676 | 676 | } |
| 677 | - | |
| 677 | + | |
| 678 | 678 | return Th_SetResultInt(interp, iRes); |
| 679 | 679 | } |
| 680 | 680 | |
| 681 | 681 | /* |
| 682 | 682 | ** TH Syntax: |
| @@ -733,11 +733,11 @@ | ||
| 733 | 733 | if( 0==memcmp(zNeedle, &zHaystack[i], nNeedle) ){ |
| 734 | 734 | iRes = i; |
| 735 | 735 | break; |
| 736 | 736 | } |
| 737 | 737 | } |
| 738 | - | |
| 738 | + | |
| 739 | 739 | return Th_SetResultInt(interp, iRes); |
| 740 | 740 | } |
| 741 | 741 | |
| 742 | 742 | /* |
| 743 | 743 | ** TH Syntax: |
| @@ -867,11 +867,11 @@ | ||
| 867 | 867 | ** TH Syntax: |
| 868 | 868 | ** |
| 869 | 869 | ** unset VAR |
| 870 | 870 | */ |
| 871 | 871 | static int unset_command( |
| 872 | - Th_Interp *interp, | |
| 872 | + Th_Interp *interp, | |
| 873 | 873 | void *ctx, |
| 874 | 874 | int argc, |
| 875 | 875 | const char **argv, |
| 876 | 876 | int *argl |
| 877 | 877 | ){ |
| @@ -880,11 +880,11 @@ | ||
| 880 | 880 | } |
| 881 | 881 | return Th_UnsetVar(interp, argv[1], argl[1]); |
| 882 | 882 | } |
| 883 | 883 | |
| 884 | 884 | int Th_CallSubCommand( |
| 885 | - Th_Interp *interp, | |
| 885 | + Th_Interp *interp, | |
| 886 | 886 | void *ctx, |
| 887 | 887 | int argc, |
| 888 | 888 | const char **argv, |
| 889 | 889 | int *argl, |
| 890 | 890 | Th_SubCommand *aSub |
| @@ -916,11 +916,11 @@ | ||
| 916 | 916 | ** string length STRING |
| 917 | 917 | ** string range STRING FIRST LAST |
| 918 | 918 | ** string repeat STRING COUNT |
| 919 | 919 | */ |
| 920 | 920 | static int string_command( |
| 921 | - Th_Interp *interp, | |
| 921 | + Th_Interp *interp, | |
| 922 | 922 | void *ctx, |
| 923 | 923 | int argc, |
| 924 | 924 | const char **argv, |
| 925 | 925 | int *argl |
| 926 | 926 | ){ |
| @@ -944,11 +944,11 @@ | ||
| 944 | 944 | ** TH Syntax: |
| 945 | 945 | ** |
| 946 | 946 | ** info exists VARNAME |
| 947 | 947 | */ |
| 948 | 948 | static int info_command( |
| 949 | - Th_Interp *interp, | |
| 949 | + Th_Interp *interp, | |
| 950 | 950 | void *ctx, |
| 951 | 951 | int argc, |
| 952 | 952 | const char **argv, |
| 953 | 953 | int *argl |
| 954 | 954 | ){ |
| @@ -958,20 +958,20 @@ | ||
| 958 | 958 | }; |
| 959 | 959 | return Th_CallSubCommand(interp, ctx, argc, argv, argl, aSub); |
| 960 | 960 | } |
| 961 | 961 | |
| 962 | 962 | /* |
| 963 | -** Convert the script level frame specification (used by the commands | |
| 964 | -** [uplevel] and [upvar]) in (zFrame, nFrame) to an integer frame as | |
| 963 | +** Convert the script level frame specification (used by the commands | |
| 964 | +** [uplevel] and [upvar]) in (zFrame, nFrame) to an integer frame as | |
| 965 | 965 | ** used by Th_LinkVar() and Th_Eval(). If successful, write the integer |
| 966 | 966 | ** frame level to *piFrame and return TH_OK. Otherwise, return TH_ERROR |
| 967 | 967 | ** and leave an error message in the interpreter result. |
| 968 | 968 | */ |
| 969 | 969 | static int thToFrame( |
| 970 | - Th_Interp *interp, | |
| 971 | - const char *zFrame, | |
| 972 | - int nFrame, | |
| 970 | + Th_Interp *interp, | |
| 971 | + const char *zFrame, | |
| 972 | + int nFrame, | |
| 973 | 973 | int *piFrame |
| 974 | 974 | ){ |
| 975 | 975 | int iFrame; |
| 976 | 976 | if( th_isdigit(zFrame[0]) ){ |
| 977 | 977 | int rc = Th_ToInt(interp, zFrame, nFrame, &iFrame); |
| @@ -992,11 +992,11 @@ | ||
| 992 | 992 | ** TH Syntax: |
| 993 | 993 | ** |
| 994 | 994 | ** uplevel ?LEVEL? SCRIPT |
| 995 | 995 | */ |
| 996 | 996 | static int uplevel_command( |
| 997 | - Th_Interp *interp, | |
| 997 | + Th_Interp *interp, | |
| 998 | 998 | void *ctx, |
| 999 | 999 | int argc, |
| 1000 | 1000 | const char **argv, |
| 1001 | 1001 | int *argl |
| 1002 | 1002 | ){ |
| @@ -1010,19 +1010,19 @@ | ||
| 1010 | 1010 | } |
| 1011 | 1011 | return Th_Eval(interp, iFrame, argv[argc-1], -1); |
| 1012 | 1012 | } |
| 1013 | 1013 | |
| 1014 | 1014 | /* |
| 1015 | -** TH Syntax: | |
| 1015 | +** TH Syntax: | |
| 1016 | 1016 | ** |
| 1017 | 1017 | ** upvar ?FRAME? OTHERVAR MYVAR ?OTHERVAR MYVAR ...? |
| 1018 | 1018 | */ |
| 1019 | 1019 | static int upvar_command( |
| 1020 | - Th_Interp *interp, | |
| 1021 | - void *ctx, | |
| 1022 | - int argc, | |
| 1023 | - const char **argv, | |
| 1020 | + Th_Interp *interp, | |
| 1021 | + void *ctx, | |
| 1022 | + int argc, | |
| 1023 | + const char **argv, | |
| 1024 | 1024 | int *argl |
| 1025 | 1025 | ){ |
| 1026 | 1026 | int iVar = 1; |
| 1027 | 1027 | int iFrame = -1; |
| 1028 | 1028 | int rc = TH_OK; |
| @@ -1030,32 +1030,32 @@ | ||
| 1030 | 1030 | |
| 1031 | 1031 | if( TH_OK==thToFrame(0, argv[1], argl[1], &iFrame) ){ |
| 1032 | 1032 | iVar++; |
| 1033 | 1033 | } |
| 1034 | 1034 | if( argc==iVar || (argc-iVar)%2 ){ |
| 1035 | - return Th_WrongNumArgs(interp, | |
| 1035 | + return Th_WrongNumArgs(interp, | |
| 1036 | 1036 | "upvar frame othervar myvar ?othervar myvar...?"); |
| 1037 | 1037 | } |
| 1038 | 1038 | for(i=iVar; rc==TH_OK && i<argc; i=i+2){ |
| 1039 | 1039 | rc = Th_LinkVar(interp, argv[i+1], argl[i+1], iFrame, argv[i], argl[i]); |
| 1040 | 1040 | } |
| 1041 | 1041 | return rc; |
| 1042 | 1042 | } |
| 1043 | 1043 | |
| 1044 | 1044 | /* |
| 1045 | -** TH Syntax: | |
| 1045 | +** TH Syntax: | |
| 1046 | 1046 | ** |
| 1047 | 1047 | ** breakpoint ARGS |
| 1048 | 1048 | ** |
| 1049 | 1049 | ** This command does nothing at all. Its purpose in life is to serve |
| 1050 | 1050 | ** as a point for setting breakpoints in a debugger. |
| 1051 | 1051 | */ |
| 1052 | 1052 | static int breakpoint_command( |
| 1053 | - Th_Interp *interp, | |
| 1054 | - void *ctx, | |
| 1055 | - int argc, | |
| 1056 | - const char **argv, | |
| 1053 | + Th_Interp *interp, | |
| 1054 | + void *ctx, | |
| 1055 | + int argc, | |
| 1056 | + const char **argv, | |
| 1057 | 1057 | int *argl |
| 1058 | 1058 | ){ |
| 1059 | 1059 | int cnt = 0; |
| 1060 | 1060 | cnt++; |
| 1061 | 1061 | return TH_OK; |
| @@ -1078,11 +1078,11 @@ | ||
| 1078 | 1078 | {"if", if_command, 0}, |
| 1079 | 1079 | {"info", info_command, 0}, |
| 1080 | 1080 | {"lindex", lindex_command, 0}, |
| 1081 | 1081 | {"list", list_command, 0}, |
| 1082 | 1082 | {"llength", llength_command, 0}, |
| 1083 | - {"proc", proc_command, 0}, | |
| 1083 | + {"proc", proc_command, 0}, | |
| 1084 | 1084 | {"rename", rename_command, 0}, |
| 1085 | 1085 | {"set", set_command, 0}, |
| 1086 | 1086 | {"string", string_command, 0}, |
| 1087 | 1087 | {"unset", unset_command, 0}, |
| 1088 | 1088 | {"uplevel", uplevel_command, 0}, |
| @@ -1089,13 +1089,13 @@ | ||
| 1089 | 1089 | {"upvar", upvar_command, 0}, |
| 1090 | 1090 | |
| 1091 | 1091 | {"breakpoint", breakpoint_command, 0}, |
| 1092 | 1092 | |
| 1093 | 1093 | {"return", return_command, 0}, |
| 1094 | - {"break", simple_command, (void *)TH_BREAK}, | |
| 1095 | - {"continue", simple_command, (void *)TH_CONTINUE}, | |
| 1096 | - {"error", simple_command, (void *)TH_ERROR}, | |
| 1094 | + {"break", simple_command, (void *)TH_BREAK}, | |
| 1095 | + {"continue", simple_command, (void *)TH_CONTINUE}, | |
| 1096 | + {"error", simple_command, (void *)TH_ERROR}, | |
| 1097 | 1097 | |
| 1098 | 1098 | {0, 0, 0} |
| 1099 | 1099 | }; |
| 1100 | 1100 | int i; |
| 1101 | 1101 | |
| 1102 | 1102 |
| --- src/th_lang.c | |
| +++ src/th_lang.c | |
| @@ -1,12 +1,12 @@ | |
| 1 | |
| 2 | /* |
| 3 | ** This file contains the implementation of all of the TH language |
| 4 | ** built-in commands. |
| 5 | ** |
| 6 | ** All built-in commands are implemented using the public interface |
| 7 | ** declared in th.h, so this file serves as both a part of the language |
| 8 | ** implementation and an example of how to extend the language with |
| 9 | ** new commands. |
| 10 | */ |
| 11 | |
| 12 | #include "config.h" |
| @@ -18,19 +18,19 @@ | |
| 18 | Th_ErrorMessage(interp, "wrong # args: should be \"", zMsg, -1); |
| 19 | return TH_ERROR; |
| 20 | } |
| 21 | |
| 22 | /* |
| 23 | ** Syntax: |
| 24 | ** |
| 25 | ** catch script ?varname? |
| 26 | */ |
| 27 | static int catch_command( |
| 28 | Th_Interp *interp, |
| 29 | void *ctx, |
| 30 | int argc, |
| 31 | const char **argv, |
| 32 | int *argl |
| 33 | ){ |
| 34 | int rc; |
| 35 | |
| 36 | if( argc!=2 && argc!=3 ){ |
| @@ -47,19 +47,19 @@ | |
| 47 | Th_SetResultInt(interp, rc); |
| 48 | return TH_OK; |
| 49 | } |
| 50 | |
| 51 | /* |
| 52 | ** TH Syntax: |
| 53 | ** |
| 54 | ** if expr1 body1 ?elseif expr2 body2? ? ?else? bodyN? |
| 55 | */ |
| 56 | static int if_command( |
| 57 | Th_Interp *interp, |
| 58 | void *ctx, |
| 59 | int argc, |
| 60 | const char **argv, |
| 61 | int *argl |
| 62 | ){ |
| 63 | int rc = TH_OK; |
| 64 | |
| 65 | int iCond; /* Result of evaluating expression */ |
| @@ -94,19 +94,19 @@ | |
| 94 | wrong_args: |
| 95 | return Th_WrongNumArgs(interp, "if ..."); |
| 96 | } |
| 97 | |
| 98 | /* |
| 99 | ** TH Syntax: |
| 100 | ** |
| 101 | ** expr expr |
| 102 | */ |
| 103 | static int expr_command( |
| 104 | Th_Interp *interp, |
| 105 | void *ctx, |
| 106 | int argc, |
| 107 | const char **argv, |
| 108 | int *argl |
| 109 | ){ |
| 110 | if( argc!=2 ){ |
| 111 | return Th_WrongNumArgs(interp, "expr expression"); |
| 112 | } |
| @@ -113,11 +113,11 @@ | |
| 113 | |
| 114 | return Th_Expr(interp, argv[1], argl[1]); |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | ** Evaluate the th1 script (zBody, nBody) in the local stack frame. |
| 119 | ** Return the result of the evaluation, except if the result |
| 120 | ** is TH_CONTINUE, return TH_OK instead. |
| 121 | */ |
| 122 | static int eval_loopbody(Th_Interp *interp, const char *zBody, int nBody){ |
| 123 | int rc = Th_Eval(interp, 0, zBody, nBody); |
| @@ -126,19 +126,19 @@ | |
| 126 | } |
| 127 | return rc; |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | ** TH Syntax: |
| 132 | ** |
| 133 | ** for init condition incr script |
| 134 | */ |
| 135 | static int for_command( |
| 136 | Th_Interp *interp, |
| 137 | void *ctx, |
| 138 | int argc, |
| 139 | const char **argv, |
| 140 | int *argl |
| 141 | ){ |
| 142 | int rc; |
| 143 | int iCond; |
| 144 | |
| @@ -147,11 +147,11 @@ | |
| 147 | } |
| 148 | |
| 149 | /* Evaluate the 'init' script */ |
| 150 | rc = Th_Eval(interp, 0, argv[1], -1); |
| 151 | |
| 152 | while( rc==TH_OK |
| 153 | && TH_OK==(rc = Th_Expr(interp, argv[2], -1)) |
| 154 | && TH_OK==(rc = Th_ToInt(interp, Th_GetResult(interp, 0), -1, &iCond)) |
| 155 | && iCond |
| 156 | && TH_OK==(rc = eval_loopbody(interp, argv[4], argl[4])) |
| 157 | ){ |
| @@ -161,45 +161,45 @@ | |
| 161 | if( rc==TH_BREAK ) rc = TH_OK; |
| 162 | return rc; |
| 163 | } |
| 164 | |
| 165 | /* |
| 166 | ** TH Syntax: |
| 167 | ** |
| 168 | ** list ?arg1 ?arg2? ...? |
| 169 | */ |
| 170 | static int list_command( |
| 171 | Th_Interp *interp, |
| 172 | void *ctx, |
| 173 | int argc, |
| 174 | const char **argv, |
| 175 | int *argl |
| 176 | ){ |
| 177 | char *zList = 0; |
| 178 | int nList = 0; |
| 179 | int i; |
| 180 | |
| 181 | for(i=1; i<argc; i++){ |
| 182 | Th_ListAppend(interp, &zList, &nList, argv[i], argl[i]); |
| 183 | } |
| 184 | |
| 185 | Th_SetResult(interp, zList, nList); |
| 186 | Th_Free(interp, zList); |
| 187 | |
| 188 | return TH_OK; |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | ** TH Syntax: |
| 193 | ** |
| 194 | ** lindex list index |
| 195 | */ |
| 196 | static int lindex_command( |
| 197 | Th_Interp *interp, |
| 198 | void *ctx, |
| 199 | int argc, |
| 200 | const char **argv, |
| 201 | int *argl |
| 202 | ){ |
| 203 | int iElem; |
| 204 | int rc; |
| 205 | |
| @@ -227,19 +227,19 @@ | |
| 227 | |
| 228 | return rc; |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | ** TH Syntax: |
| 233 | ** |
| 234 | ** llength list |
| 235 | */ |
| 236 | static int llength_command( |
| 237 | Th_Interp *interp, |
| 238 | void *ctx, |
| 239 | int argc, |
| 240 | const char **argv, |
| 241 | int *argl |
| 242 | ){ |
| 243 | int nElem; |
| 244 | int rc; |
| 245 | |
| @@ -254,19 +254,19 @@ | |
| 254 | |
| 255 | return rc; |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | ** TH Syntax: |
| 260 | ** |
| 261 | ** set varname ?value? |
| 262 | */ |
| 263 | static int set_command( |
| 264 | Th_Interp *interp, |
| 265 | void *ctx, |
| 266 | int argc, |
| 267 | const char **argv, |
| 268 | int *argl |
| 269 | ){ |
| 270 | if( argc!=2 && argc!=3 ){ |
| 271 | return Th_WrongNumArgs(interp, "set varname ?value?"); |
| 272 | } |
| @@ -277,30 +277,30 @@ | |
| 277 | return Th_GetVar(interp, argv[1], argl[1]); |
| 278 | } |
| 279 | |
| 280 | /* |
| 281 | ** When a new command is created using the built-in [proc] command, an |
| 282 | ** instance of the following structure is allocated and populated. A |
| 283 | ** pointer to the structure is passed as the context (second) argument |
| 284 | ** to function proc_call1() when the new command is executed. |
| 285 | */ |
| 286 | typedef struct ProcDefn ProcDefn; |
| 287 | struct ProcDefn { |
| 288 | int nParam; /* Number of formal (non "args") parameters */ |
| 289 | char **azParam; /* Parameter names */ |
| 290 | int *anParam; /* Lengths of parameter names */ |
| 291 | char **azDefault; /* Default values */ |
| 292 | int *anDefault; /* Lengths of default values */ |
| 293 | int hasArgs; /* True if there is an "args" parameter */ |
| 294 | char *zProgram; /* Body of proc */ |
| 295 | int nProgram; /* Number of bytes at zProgram */ |
| 296 | char *zUsage; /* Usage message */ |
| 297 | int nUsage; /* Number of bytes at zUsage */ |
| 298 | }; |
| 299 | |
| 300 | /* This structure is used to temporarily store arguments passed to an |
| 301 | ** invocation of a command created using [proc]. A pointer to an |
| 302 | ** instance is passed as the second argument to the proc_call2() function. |
| 303 | */ |
| 304 | typedef struct ProcArgs ProcArgs; |
| 305 | struct ProcArgs { |
| 306 | int argc; |
| @@ -323,11 +323,11 @@ | |
| 323 | ProcArgs *pArgs = (ProcArgs *)pContext2; |
| 324 | |
| 325 | /* Check if there are the right number of arguments. If there are |
| 326 | ** not, generate a usage message for the command. |
| 327 | */ |
| 328 | if( (pArgs->argc>(p->nParam+1) && !p->hasArgs) |
| 329 | || (pArgs->argc<=(p->nParam) && !p->azDefault[pArgs->argc-1]) |
| 330 | ){ |
| 331 | char *zUsage = 0; |
| 332 | int nUsage = 0; |
| 333 | Th_StringAppend(interp, &zUsage, &nUsage, pArgs->argv[0], pArgs->argl[0]); |
| @@ -374,12 +374,12 @@ | |
| 374 | ** created using the [proc] command. The second argument, pContext, |
| 375 | ** is a pointer to the associated ProcDefn structure. |
| 376 | */ |
| 377 | static int proc_call1( |
| 378 | Th_Interp *interp, |
| 379 | void *pContext, |
| 380 | int argc, |
| 381 | const char **argv, |
| 382 | int *argl |
| 383 | ){ |
| 384 | int rc; |
| 385 | |
| @@ -400,32 +400,32 @@ | |
| 400 | } |
| 401 | return rc; |
| 402 | } |
| 403 | |
| 404 | /* |
| 405 | ** This function is registered as the delete callback for all commands |
| 406 | ** created using the built-in [proc] command. It is called automatically |
| 407 | ** when a command created using [proc] is deleted. |
| 408 | ** |
| 409 | ** It frees the ProcDefn structure allocated when the command was created. |
| 410 | */ |
| 411 | static void proc_del(Th_Interp *interp, void *pContext){ |
| 412 | ProcDefn *p = (ProcDefn *)pContext; |
| 413 | Th_Free(interp, (void *)p->zUsage); |
| 414 | Th_Free(interp, (void *)p); |
| 415 | } |
| 416 | |
| 417 | /* |
| 418 | ** TH Syntax: |
| 419 | ** |
| 420 | ** proc name arglist code |
| 421 | */ |
| 422 | static int proc_command( |
| 423 | Th_Interp *interp, |
| 424 | void *ctx, |
| 425 | int argc, |
| 426 | const char **argv, |
| 427 | int *argl |
| 428 | ){ |
| 429 | int rc; |
| 430 | char *zName; |
| 431 | |
| @@ -436,11 +436,11 @@ | |
| 436 | |
| 437 | char **azParam; |
| 438 | int *anParam; |
| 439 | int nParam; |
| 440 | |
| 441 | char *zUsage = 0; /* Build up a usage message here */ |
| 442 | int nUsage = 0; /* Number of bytes at zUsage */ |
| 443 | |
| 444 | if( argc!=4 ){ |
| 445 | return Th_WrongNumArgs(interp, "proc name arglist code"); |
| 446 | } |
| @@ -448,14 +448,14 @@ | |
| 448 | return TH_ERROR; |
| 449 | } |
| 450 | |
| 451 | /* Allocate the new ProcDefn structure. */ |
| 452 | nByte = sizeof(ProcDefn) + /* ProcDefn structure */ |
| 453 | (sizeof(char *) + sizeof(int)) * nParam + /* azParam, anParam */ |
| 454 | (sizeof(char *) + sizeof(int)) * nParam + /* azDefault, anDefault */ |
| 455 | argl[3] + /* zProgram */ |
| 456 | argl[2]; /* Space for copies of parameter names and default values */ |
| 457 | p = (ProcDefn *)Th_Malloc(interp, nByte); |
| 458 | |
| 459 | /* If the last parameter in the parameter list is "args", then set the |
| 460 | ** ProcDefn.hasArgs flag. The "args" parameter does not require an |
| 461 | ** entry in the ProcDefn.azParam[] or ProcDefn.azDefault[] arrays. |
| @@ -472,11 +472,11 @@ | |
| 472 | p->anDefault = (int *)&p->azDefault[nParam]; |
| 473 | p->zProgram = (char *)&p->anDefault[nParam]; |
| 474 | memcpy(p->zProgram, argv[3], argl[3]); |
| 475 | p->nProgram = argl[3]; |
| 476 | zSpace = &p->zProgram[p->nProgram]; |
| 477 | |
| 478 | for(i=0; i<nParam; i++){ |
| 479 | char **az; |
| 480 | int *an; |
| 481 | int n; |
| 482 | if( Th_SplitList(interp, azParam[i], anParam[i], &az, &an, &n) ){ |
| @@ -537,40 +537,40 @@ | |
| 537 | Th_Free(interp, zUsage); |
| 538 | return TH_ERROR; |
| 539 | } |
| 540 | |
| 541 | /* |
| 542 | ** TH Syntax: |
| 543 | ** |
| 544 | ** rename oldcmd newcmd |
| 545 | */ |
| 546 | static int rename_command( |
| 547 | Th_Interp *interp, |
| 548 | void *ctx, |
| 549 | int argc, |
| 550 | const char **argv, |
| 551 | int *argl |
| 552 | ){ |
| 553 | if( argc!=3 ){ |
| 554 | return Th_WrongNumArgs(interp, "rename oldcmd newcmd"); |
| 555 | } |
| 556 | return Th_RenameCommand(interp, argv[1], argl[1], argv[2], argl[2]); |
| 557 | } |
| 558 | |
| 559 | /* |
| 560 | ** TH Syntax: |
| 561 | ** |
| 562 | ** break ?value...? |
| 563 | ** continue ?value...? |
| 564 | ** ok ?value...? |
| 565 | ** error ?value...? |
| 566 | */ |
| 567 | static int simple_command( |
| 568 | Th_Interp *interp, |
| 569 | void *ctx, |
| 570 | int argc, |
| 571 | const char **argv, |
| 572 | int *argl |
| 573 | ){ |
| 574 | if( argc!=1 && argc!=2 ){ |
| 575 | return Th_WrongNumArgs(interp, "return ?value?"); |
| 576 | } |
| @@ -579,19 +579,19 @@ | |
| 579 | } |
| 580 | return FOSSIL_PTR_TO_INT(ctx); |
| 581 | } |
| 582 | |
| 583 | /* |
| 584 | ** TH Syntax: |
| 585 | ** |
| 586 | ** return ?-code code? ?value? |
| 587 | */ |
| 588 | static int return_command( |
| 589 | Th_Interp *interp, |
| 590 | void *ctx, |
| 591 | int argc, |
| 592 | const char **argv, |
| 593 | int *argl |
| 594 | ){ |
| 595 | int iCode = TH_RETURN; |
| 596 | if( argc<1 || argc>4 ){ |
| 597 | return Th_WrongNumArgs(interp, "return ?-code code? ?value?"); |
| @@ -638,11 +638,11 @@ | |
| 638 | iRes = nLeft-nRight; |
| 639 | } |
| 640 | |
| 641 | if( iRes<0 ) iRes = -1; |
| 642 | if( iRes>0 ) iRes = 1; |
| 643 | |
| 644 | return Th_SetResultInt(interp, iRes); |
| 645 | } |
| 646 | |
| 647 | /* |
| 648 | ** TH Syntax: |
| @@ -672,11 +672,11 @@ | |
| 672 | if( 0==memcmp(zNeedle, &zHaystack[i], nNeedle) ){ |
| 673 | iRes = i; |
| 674 | break; |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | return Th_SetResultInt(interp, iRes); |
| 679 | } |
| 680 | |
| 681 | /* |
| 682 | ** TH Syntax: |
| @@ -733,11 +733,11 @@ | |
| 733 | if( 0==memcmp(zNeedle, &zHaystack[i], nNeedle) ){ |
| 734 | iRes = i; |
| 735 | break; |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | return Th_SetResultInt(interp, iRes); |
| 740 | } |
| 741 | |
| 742 | /* |
| 743 | ** TH Syntax: |
| @@ -867,11 +867,11 @@ | |
| 867 | ** TH Syntax: |
| 868 | ** |
| 869 | ** unset VAR |
| 870 | */ |
| 871 | static int unset_command( |
| 872 | Th_Interp *interp, |
| 873 | void *ctx, |
| 874 | int argc, |
| 875 | const char **argv, |
| 876 | int *argl |
| 877 | ){ |
| @@ -880,11 +880,11 @@ | |
| 880 | } |
| 881 | return Th_UnsetVar(interp, argv[1], argl[1]); |
| 882 | } |
| 883 | |
| 884 | int Th_CallSubCommand( |
| 885 | Th_Interp *interp, |
| 886 | void *ctx, |
| 887 | int argc, |
| 888 | const char **argv, |
| 889 | int *argl, |
| 890 | Th_SubCommand *aSub |
| @@ -916,11 +916,11 @@ | |
| 916 | ** string length STRING |
| 917 | ** string range STRING FIRST LAST |
| 918 | ** string repeat STRING COUNT |
| 919 | */ |
| 920 | static int string_command( |
| 921 | Th_Interp *interp, |
| 922 | void *ctx, |
| 923 | int argc, |
| 924 | const char **argv, |
| 925 | int *argl |
| 926 | ){ |
| @@ -944,11 +944,11 @@ | |
| 944 | ** TH Syntax: |
| 945 | ** |
| 946 | ** info exists VARNAME |
| 947 | */ |
| 948 | static int info_command( |
| 949 | Th_Interp *interp, |
| 950 | void *ctx, |
| 951 | int argc, |
| 952 | const char **argv, |
| 953 | int *argl |
| 954 | ){ |
| @@ -958,20 +958,20 @@ | |
| 958 | }; |
| 959 | return Th_CallSubCommand(interp, ctx, argc, argv, argl, aSub); |
| 960 | } |
| 961 | |
| 962 | /* |
| 963 | ** Convert the script level frame specification (used by the commands |
| 964 | ** [uplevel] and [upvar]) in (zFrame, nFrame) to an integer frame as |
| 965 | ** used by Th_LinkVar() and Th_Eval(). If successful, write the integer |
| 966 | ** frame level to *piFrame and return TH_OK. Otherwise, return TH_ERROR |
| 967 | ** and leave an error message in the interpreter result. |
| 968 | */ |
| 969 | static int thToFrame( |
| 970 | Th_Interp *interp, |
| 971 | const char *zFrame, |
| 972 | int nFrame, |
| 973 | int *piFrame |
| 974 | ){ |
| 975 | int iFrame; |
| 976 | if( th_isdigit(zFrame[0]) ){ |
| 977 | int rc = Th_ToInt(interp, zFrame, nFrame, &iFrame); |
| @@ -992,11 +992,11 @@ | |
| 992 | ** TH Syntax: |
| 993 | ** |
| 994 | ** uplevel ?LEVEL? SCRIPT |
| 995 | */ |
| 996 | static int uplevel_command( |
| 997 | Th_Interp *interp, |
| 998 | void *ctx, |
| 999 | int argc, |
| 1000 | const char **argv, |
| 1001 | int *argl |
| 1002 | ){ |
| @@ -1010,19 +1010,19 @@ | |
| 1010 | } |
| 1011 | return Th_Eval(interp, iFrame, argv[argc-1], -1); |
| 1012 | } |
| 1013 | |
| 1014 | /* |
| 1015 | ** TH Syntax: |
| 1016 | ** |
| 1017 | ** upvar ?FRAME? OTHERVAR MYVAR ?OTHERVAR MYVAR ...? |
| 1018 | */ |
| 1019 | static int upvar_command( |
| 1020 | Th_Interp *interp, |
| 1021 | void *ctx, |
| 1022 | int argc, |
| 1023 | const char **argv, |
| 1024 | int *argl |
| 1025 | ){ |
| 1026 | int iVar = 1; |
| 1027 | int iFrame = -1; |
| 1028 | int rc = TH_OK; |
| @@ -1030,32 +1030,32 @@ | |
| 1030 | |
| 1031 | if( TH_OK==thToFrame(0, argv[1], argl[1], &iFrame) ){ |
| 1032 | iVar++; |
| 1033 | } |
| 1034 | if( argc==iVar || (argc-iVar)%2 ){ |
| 1035 | return Th_WrongNumArgs(interp, |
| 1036 | "upvar frame othervar myvar ?othervar myvar...?"); |
| 1037 | } |
| 1038 | for(i=iVar; rc==TH_OK && i<argc; i=i+2){ |
| 1039 | rc = Th_LinkVar(interp, argv[i+1], argl[i+1], iFrame, argv[i], argl[i]); |
| 1040 | } |
| 1041 | return rc; |
| 1042 | } |
| 1043 | |
| 1044 | /* |
| 1045 | ** TH Syntax: |
| 1046 | ** |
| 1047 | ** breakpoint ARGS |
| 1048 | ** |
| 1049 | ** This command does nothing at all. Its purpose in life is to serve |
| 1050 | ** as a point for setting breakpoints in a debugger. |
| 1051 | */ |
| 1052 | static int breakpoint_command( |
| 1053 | Th_Interp *interp, |
| 1054 | void *ctx, |
| 1055 | int argc, |
| 1056 | const char **argv, |
| 1057 | int *argl |
| 1058 | ){ |
| 1059 | int cnt = 0; |
| 1060 | cnt++; |
| 1061 | return TH_OK; |
| @@ -1078,11 +1078,11 @@ | |
| 1078 | {"if", if_command, 0}, |
| 1079 | {"info", info_command, 0}, |
| 1080 | {"lindex", lindex_command, 0}, |
| 1081 | {"list", list_command, 0}, |
| 1082 | {"llength", llength_command, 0}, |
| 1083 | {"proc", proc_command, 0}, |
| 1084 | {"rename", rename_command, 0}, |
| 1085 | {"set", set_command, 0}, |
| 1086 | {"string", string_command, 0}, |
| 1087 | {"unset", unset_command, 0}, |
| 1088 | {"uplevel", uplevel_command, 0}, |
| @@ -1089,13 +1089,13 @@ | |
| 1089 | {"upvar", upvar_command, 0}, |
| 1090 | |
| 1091 | {"breakpoint", breakpoint_command, 0}, |
| 1092 | |
| 1093 | {"return", return_command, 0}, |
| 1094 | {"break", simple_command, (void *)TH_BREAK}, |
| 1095 | {"continue", simple_command, (void *)TH_CONTINUE}, |
| 1096 | {"error", simple_command, (void *)TH_ERROR}, |
| 1097 | |
| 1098 | {0, 0, 0} |
| 1099 | }; |
| 1100 | int i; |
| 1101 | |
| 1102 |
| --- src/th_lang.c | |
| +++ src/th_lang.c | |
| @@ -1,12 +1,12 @@ | |
| 1 | |
| 2 | /* |
| 3 | ** This file contains the implementation of all of the TH language |
| 4 | ** built-in commands. |
| 5 | ** |
| 6 | ** All built-in commands are implemented using the public interface |
| 7 | ** declared in th.h, so this file serves as both a part of the language |
| 8 | ** implementation and an example of how to extend the language with |
| 9 | ** new commands. |
| 10 | */ |
| 11 | |
| 12 | #include "config.h" |
| @@ -18,19 +18,19 @@ | |
| 18 | Th_ErrorMessage(interp, "wrong # args: should be \"", zMsg, -1); |
| 19 | return TH_ERROR; |
| 20 | } |
| 21 | |
| 22 | /* |
| 23 | ** Syntax: |
| 24 | ** |
| 25 | ** catch script ?varname? |
| 26 | */ |
| 27 | static int catch_command( |
| 28 | Th_Interp *interp, |
| 29 | void *ctx, |
| 30 | int argc, |
| 31 | const char **argv, |
| 32 | int *argl |
| 33 | ){ |
| 34 | int rc; |
| 35 | |
| 36 | if( argc!=2 && argc!=3 ){ |
| @@ -47,19 +47,19 @@ | |
| 47 | Th_SetResultInt(interp, rc); |
| 48 | return TH_OK; |
| 49 | } |
| 50 | |
| 51 | /* |
| 52 | ** TH Syntax: |
| 53 | ** |
| 54 | ** if expr1 body1 ?elseif expr2 body2? ? ?else? bodyN? |
| 55 | */ |
| 56 | static int if_command( |
| 57 | Th_Interp *interp, |
| 58 | void *ctx, |
| 59 | int argc, |
| 60 | const char **argv, |
| 61 | int *argl |
| 62 | ){ |
| 63 | int rc = TH_OK; |
| 64 | |
| 65 | int iCond; /* Result of evaluating expression */ |
| @@ -94,19 +94,19 @@ | |
| 94 | wrong_args: |
| 95 | return Th_WrongNumArgs(interp, "if ..."); |
| 96 | } |
| 97 | |
| 98 | /* |
| 99 | ** TH Syntax: |
| 100 | ** |
| 101 | ** expr expr |
| 102 | */ |
| 103 | static int expr_command( |
| 104 | Th_Interp *interp, |
| 105 | void *ctx, |
| 106 | int argc, |
| 107 | const char **argv, |
| 108 | int *argl |
| 109 | ){ |
| 110 | if( argc!=2 ){ |
| 111 | return Th_WrongNumArgs(interp, "expr expression"); |
| 112 | } |
| @@ -113,11 +113,11 @@ | |
| 113 | |
| 114 | return Th_Expr(interp, argv[1], argl[1]); |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | ** Evaluate the th1 script (zBody, nBody) in the local stack frame. |
| 119 | ** Return the result of the evaluation, except if the result |
| 120 | ** is TH_CONTINUE, return TH_OK instead. |
| 121 | */ |
| 122 | static int eval_loopbody(Th_Interp *interp, const char *zBody, int nBody){ |
| 123 | int rc = Th_Eval(interp, 0, zBody, nBody); |
| @@ -126,19 +126,19 @@ | |
| 126 | } |
| 127 | return rc; |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | ** TH Syntax: |
| 132 | ** |
| 133 | ** for init condition incr script |
| 134 | */ |
| 135 | static int for_command( |
| 136 | Th_Interp *interp, |
| 137 | void *ctx, |
| 138 | int argc, |
| 139 | const char **argv, |
| 140 | int *argl |
| 141 | ){ |
| 142 | int rc; |
| 143 | int iCond; |
| 144 | |
| @@ -147,11 +147,11 @@ | |
| 147 | } |
| 148 | |
| 149 | /* Evaluate the 'init' script */ |
| 150 | rc = Th_Eval(interp, 0, argv[1], -1); |
| 151 | |
| 152 | while( rc==TH_OK |
| 153 | && TH_OK==(rc = Th_Expr(interp, argv[2], -1)) |
| 154 | && TH_OK==(rc = Th_ToInt(interp, Th_GetResult(interp, 0), -1, &iCond)) |
| 155 | && iCond |
| 156 | && TH_OK==(rc = eval_loopbody(interp, argv[4], argl[4])) |
| 157 | ){ |
| @@ -161,45 +161,45 @@ | |
| 161 | if( rc==TH_BREAK ) rc = TH_OK; |
| 162 | return rc; |
| 163 | } |
| 164 | |
| 165 | /* |
| 166 | ** TH Syntax: |
| 167 | ** |
| 168 | ** list ?arg1 ?arg2? ...? |
| 169 | */ |
| 170 | static int list_command( |
| 171 | Th_Interp *interp, |
| 172 | void *ctx, |
| 173 | int argc, |
| 174 | const char **argv, |
| 175 | int *argl |
| 176 | ){ |
| 177 | char *zList = 0; |
| 178 | int nList = 0; |
| 179 | int i; |
| 180 | |
| 181 | for(i=1; i<argc; i++){ |
| 182 | Th_ListAppend(interp, &zList, &nList, argv[i], argl[i]); |
| 183 | } |
| 184 | |
| 185 | Th_SetResult(interp, zList, nList); |
| 186 | Th_Free(interp, zList); |
| 187 | |
| 188 | return TH_OK; |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | ** TH Syntax: |
| 193 | ** |
| 194 | ** lindex list index |
| 195 | */ |
| 196 | static int lindex_command( |
| 197 | Th_Interp *interp, |
| 198 | void *ctx, |
| 199 | int argc, |
| 200 | const char **argv, |
| 201 | int *argl |
| 202 | ){ |
| 203 | int iElem; |
| 204 | int rc; |
| 205 | |
| @@ -227,19 +227,19 @@ | |
| 227 | |
| 228 | return rc; |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | ** TH Syntax: |
| 233 | ** |
| 234 | ** llength list |
| 235 | */ |
| 236 | static int llength_command( |
| 237 | Th_Interp *interp, |
| 238 | void *ctx, |
| 239 | int argc, |
| 240 | const char **argv, |
| 241 | int *argl |
| 242 | ){ |
| 243 | int nElem; |
| 244 | int rc; |
| 245 | |
| @@ -254,19 +254,19 @@ | |
| 254 | |
| 255 | return rc; |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | ** TH Syntax: |
| 260 | ** |
| 261 | ** set varname ?value? |
| 262 | */ |
| 263 | static int set_command( |
| 264 | Th_Interp *interp, |
| 265 | void *ctx, |
| 266 | int argc, |
| 267 | const char **argv, |
| 268 | int *argl |
| 269 | ){ |
| 270 | if( argc!=2 && argc!=3 ){ |
| 271 | return Th_WrongNumArgs(interp, "set varname ?value?"); |
| 272 | } |
| @@ -277,30 +277,30 @@ | |
| 277 | return Th_GetVar(interp, argv[1], argl[1]); |
| 278 | } |
| 279 | |
| 280 | /* |
| 281 | ** When a new command is created using the built-in [proc] command, an |
| 282 | ** instance of the following structure is allocated and populated. A |
| 283 | ** pointer to the structure is passed as the context (second) argument |
| 284 | ** to function proc_call1() when the new command is executed. |
| 285 | */ |
| 286 | typedef struct ProcDefn ProcDefn; |
| 287 | struct ProcDefn { |
| 288 | int nParam; /* Number of formal (non "args") parameters */ |
| 289 | char **azParam; /* Parameter names */ |
| 290 | int *anParam; /* Lengths of parameter names */ |
| 291 | char **azDefault; /* Default values */ |
| 292 | int *anDefault; /* Lengths of default values */ |
| 293 | int hasArgs; /* True if there is an "args" parameter */ |
| 294 | char *zProgram; /* Body of proc */ |
| 295 | int nProgram; /* Number of bytes at zProgram */ |
| 296 | char *zUsage; /* Usage message */ |
| 297 | int nUsage; /* Number of bytes at zUsage */ |
| 298 | }; |
| 299 | |
| 300 | /* This structure is used to temporarily store arguments passed to an |
| 301 | ** invocation of a command created using [proc]. A pointer to an |
| 302 | ** instance is passed as the second argument to the proc_call2() function. |
| 303 | */ |
| 304 | typedef struct ProcArgs ProcArgs; |
| 305 | struct ProcArgs { |
| 306 | int argc; |
| @@ -323,11 +323,11 @@ | |
| 323 | ProcArgs *pArgs = (ProcArgs *)pContext2; |
| 324 | |
| 325 | /* Check if there are the right number of arguments. If there are |
| 326 | ** not, generate a usage message for the command. |
| 327 | */ |
| 328 | if( (pArgs->argc>(p->nParam+1) && !p->hasArgs) |
| 329 | || (pArgs->argc<=(p->nParam) && !p->azDefault[pArgs->argc-1]) |
| 330 | ){ |
| 331 | char *zUsage = 0; |
| 332 | int nUsage = 0; |
| 333 | Th_StringAppend(interp, &zUsage, &nUsage, pArgs->argv[0], pArgs->argl[0]); |
| @@ -374,12 +374,12 @@ | |
| 374 | ** created using the [proc] command. The second argument, pContext, |
| 375 | ** is a pointer to the associated ProcDefn structure. |
| 376 | */ |
| 377 | static int proc_call1( |
| 378 | Th_Interp *interp, |
| 379 | void *pContext, |
| 380 | int argc, |
| 381 | const char **argv, |
| 382 | int *argl |
| 383 | ){ |
| 384 | int rc; |
| 385 | |
| @@ -400,32 +400,32 @@ | |
| 400 | } |
| 401 | return rc; |
| 402 | } |
| 403 | |
| 404 | /* |
| 405 | ** This function is registered as the delete callback for all commands |
| 406 | ** created using the built-in [proc] command. It is called automatically |
| 407 | ** when a command created using [proc] is deleted. |
| 408 | ** |
| 409 | ** It frees the ProcDefn structure allocated when the command was created. |
| 410 | */ |
| 411 | static void proc_del(Th_Interp *interp, void *pContext){ |
| 412 | ProcDefn *p = (ProcDefn *)pContext; |
| 413 | Th_Free(interp, (void *)p->zUsage); |
| 414 | Th_Free(interp, (void *)p); |
| 415 | } |
| 416 | |
| 417 | /* |
| 418 | ** TH Syntax: |
| 419 | ** |
| 420 | ** proc name arglist code |
| 421 | */ |
| 422 | static int proc_command( |
| 423 | Th_Interp *interp, |
| 424 | void *ctx, |
| 425 | int argc, |
| 426 | const char **argv, |
| 427 | int *argl |
| 428 | ){ |
| 429 | int rc; |
| 430 | char *zName; |
| 431 | |
| @@ -436,11 +436,11 @@ | |
| 436 | |
| 437 | char **azParam; |
| 438 | int *anParam; |
| 439 | int nParam; |
| 440 | |
| 441 | char *zUsage = 0; /* Build up a usage message here */ |
| 442 | int nUsage = 0; /* Number of bytes at zUsage */ |
| 443 | |
| 444 | if( argc!=4 ){ |
| 445 | return Th_WrongNumArgs(interp, "proc name arglist code"); |
| 446 | } |
| @@ -448,14 +448,14 @@ | |
| 448 | return TH_ERROR; |
| 449 | } |
| 450 | |
| 451 | /* Allocate the new ProcDefn structure. */ |
| 452 | nByte = sizeof(ProcDefn) + /* ProcDefn structure */ |
| 453 | (sizeof(char *) + sizeof(int)) * nParam + /* azParam, anParam */ |
| 454 | (sizeof(char *) + sizeof(int)) * nParam + /* azDefault, anDefault */ |
| 455 | argl[3] + /* zProgram */ |
| 456 | argl[2]; /* Space for copies of parameter names and default values */ |
| 457 | p = (ProcDefn *)Th_Malloc(interp, nByte); |
| 458 | |
| 459 | /* If the last parameter in the parameter list is "args", then set the |
| 460 | ** ProcDefn.hasArgs flag. The "args" parameter does not require an |
| 461 | ** entry in the ProcDefn.azParam[] or ProcDefn.azDefault[] arrays. |
| @@ -472,11 +472,11 @@ | |
| 472 | p->anDefault = (int *)&p->azDefault[nParam]; |
| 473 | p->zProgram = (char *)&p->anDefault[nParam]; |
| 474 | memcpy(p->zProgram, argv[3], argl[3]); |
| 475 | p->nProgram = argl[3]; |
| 476 | zSpace = &p->zProgram[p->nProgram]; |
| 477 | |
| 478 | for(i=0; i<nParam; i++){ |
| 479 | char **az; |
| 480 | int *an; |
| 481 | int n; |
| 482 | if( Th_SplitList(interp, azParam[i], anParam[i], &az, &an, &n) ){ |
| @@ -537,40 +537,40 @@ | |
| 537 | Th_Free(interp, zUsage); |
| 538 | return TH_ERROR; |
| 539 | } |
| 540 | |
| 541 | /* |
| 542 | ** TH Syntax: |
| 543 | ** |
| 544 | ** rename oldcmd newcmd |
| 545 | */ |
| 546 | static int rename_command( |
| 547 | Th_Interp *interp, |
| 548 | void *ctx, |
| 549 | int argc, |
| 550 | const char **argv, |
| 551 | int *argl |
| 552 | ){ |
| 553 | if( argc!=3 ){ |
| 554 | return Th_WrongNumArgs(interp, "rename oldcmd newcmd"); |
| 555 | } |
| 556 | return Th_RenameCommand(interp, argv[1], argl[1], argv[2], argl[2]); |
| 557 | } |
| 558 | |
| 559 | /* |
| 560 | ** TH Syntax: |
| 561 | ** |
| 562 | ** break ?value...? |
| 563 | ** continue ?value...? |
| 564 | ** ok ?value...? |
| 565 | ** error ?value...? |
| 566 | */ |
| 567 | static int simple_command( |
| 568 | Th_Interp *interp, |
| 569 | void *ctx, |
| 570 | int argc, |
| 571 | const char **argv, |
| 572 | int *argl |
| 573 | ){ |
| 574 | if( argc!=1 && argc!=2 ){ |
| 575 | return Th_WrongNumArgs(interp, "return ?value?"); |
| 576 | } |
| @@ -579,19 +579,19 @@ | |
| 579 | } |
| 580 | return FOSSIL_PTR_TO_INT(ctx); |
| 581 | } |
| 582 | |
| 583 | /* |
| 584 | ** TH Syntax: |
| 585 | ** |
| 586 | ** return ?-code code? ?value? |
| 587 | */ |
| 588 | static int return_command( |
| 589 | Th_Interp *interp, |
| 590 | void *ctx, |
| 591 | int argc, |
| 592 | const char **argv, |
| 593 | int *argl |
| 594 | ){ |
| 595 | int iCode = TH_RETURN; |
| 596 | if( argc<1 || argc>4 ){ |
| 597 | return Th_WrongNumArgs(interp, "return ?-code code? ?value?"); |
| @@ -638,11 +638,11 @@ | |
| 638 | iRes = nLeft-nRight; |
| 639 | } |
| 640 | |
| 641 | if( iRes<0 ) iRes = -1; |
| 642 | if( iRes>0 ) iRes = 1; |
| 643 | |
| 644 | return Th_SetResultInt(interp, iRes); |
| 645 | } |
| 646 | |
| 647 | /* |
| 648 | ** TH Syntax: |
| @@ -672,11 +672,11 @@ | |
| 672 | if( 0==memcmp(zNeedle, &zHaystack[i], nNeedle) ){ |
| 673 | iRes = i; |
| 674 | break; |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | return Th_SetResultInt(interp, iRes); |
| 679 | } |
| 680 | |
| 681 | /* |
| 682 | ** TH Syntax: |
| @@ -733,11 +733,11 @@ | |
| 733 | if( 0==memcmp(zNeedle, &zHaystack[i], nNeedle) ){ |
| 734 | iRes = i; |
| 735 | break; |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | return Th_SetResultInt(interp, iRes); |
| 740 | } |
| 741 | |
| 742 | /* |
| 743 | ** TH Syntax: |
| @@ -867,11 +867,11 @@ | |
| 867 | ** TH Syntax: |
| 868 | ** |
| 869 | ** unset VAR |
| 870 | */ |
| 871 | static int unset_command( |
| 872 | Th_Interp *interp, |
| 873 | void *ctx, |
| 874 | int argc, |
| 875 | const char **argv, |
| 876 | int *argl |
| 877 | ){ |
| @@ -880,11 +880,11 @@ | |
| 880 | } |
| 881 | return Th_UnsetVar(interp, argv[1], argl[1]); |
| 882 | } |
| 883 | |
| 884 | int Th_CallSubCommand( |
| 885 | Th_Interp *interp, |
| 886 | void *ctx, |
| 887 | int argc, |
| 888 | const char **argv, |
| 889 | int *argl, |
| 890 | Th_SubCommand *aSub |
| @@ -916,11 +916,11 @@ | |
| 916 | ** string length STRING |
| 917 | ** string range STRING FIRST LAST |
| 918 | ** string repeat STRING COUNT |
| 919 | */ |
| 920 | static int string_command( |
| 921 | Th_Interp *interp, |
| 922 | void *ctx, |
| 923 | int argc, |
| 924 | const char **argv, |
| 925 | int *argl |
| 926 | ){ |
| @@ -944,11 +944,11 @@ | |
| 944 | ** TH Syntax: |
| 945 | ** |
| 946 | ** info exists VARNAME |
| 947 | */ |
| 948 | static int info_command( |
| 949 | Th_Interp *interp, |
| 950 | void *ctx, |
| 951 | int argc, |
| 952 | const char **argv, |
| 953 | int *argl |
| 954 | ){ |
| @@ -958,20 +958,20 @@ | |
| 958 | }; |
| 959 | return Th_CallSubCommand(interp, ctx, argc, argv, argl, aSub); |
| 960 | } |
| 961 | |
| 962 | /* |
| 963 | ** Convert the script level frame specification (used by the commands |
| 964 | ** [uplevel] and [upvar]) in (zFrame, nFrame) to an integer frame as |
| 965 | ** used by Th_LinkVar() and Th_Eval(). If successful, write the integer |
| 966 | ** frame level to *piFrame and return TH_OK. Otherwise, return TH_ERROR |
| 967 | ** and leave an error message in the interpreter result. |
| 968 | */ |
| 969 | static int thToFrame( |
| 970 | Th_Interp *interp, |
| 971 | const char *zFrame, |
| 972 | int nFrame, |
| 973 | int *piFrame |
| 974 | ){ |
| 975 | int iFrame; |
| 976 | if( th_isdigit(zFrame[0]) ){ |
| 977 | int rc = Th_ToInt(interp, zFrame, nFrame, &iFrame); |
| @@ -992,11 +992,11 @@ | |
| 992 | ** TH Syntax: |
| 993 | ** |
| 994 | ** uplevel ?LEVEL? SCRIPT |
| 995 | */ |
| 996 | static int uplevel_command( |
| 997 | Th_Interp *interp, |
| 998 | void *ctx, |
| 999 | int argc, |
| 1000 | const char **argv, |
| 1001 | int *argl |
| 1002 | ){ |
| @@ -1010,19 +1010,19 @@ | |
| 1010 | } |
| 1011 | return Th_Eval(interp, iFrame, argv[argc-1], -1); |
| 1012 | } |
| 1013 | |
| 1014 | /* |
| 1015 | ** TH Syntax: |
| 1016 | ** |
| 1017 | ** upvar ?FRAME? OTHERVAR MYVAR ?OTHERVAR MYVAR ...? |
| 1018 | */ |
| 1019 | static int upvar_command( |
| 1020 | Th_Interp *interp, |
| 1021 | void *ctx, |
| 1022 | int argc, |
| 1023 | const char **argv, |
| 1024 | int *argl |
| 1025 | ){ |
| 1026 | int iVar = 1; |
| 1027 | int iFrame = -1; |
| 1028 | int rc = TH_OK; |
| @@ -1030,32 +1030,32 @@ | |
| 1030 | |
| 1031 | if( TH_OK==thToFrame(0, argv[1], argl[1], &iFrame) ){ |
| 1032 | iVar++; |
| 1033 | } |
| 1034 | if( argc==iVar || (argc-iVar)%2 ){ |
| 1035 | return Th_WrongNumArgs(interp, |
| 1036 | "upvar frame othervar myvar ?othervar myvar...?"); |
| 1037 | } |
| 1038 | for(i=iVar; rc==TH_OK && i<argc; i=i+2){ |
| 1039 | rc = Th_LinkVar(interp, argv[i+1], argl[i+1], iFrame, argv[i], argl[i]); |
| 1040 | } |
| 1041 | return rc; |
| 1042 | } |
| 1043 | |
| 1044 | /* |
| 1045 | ** TH Syntax: |
| 1046 | ** |
| 1047 | ** breakpoint ARGS |
| 1048 | ** |
| 1049 | ** This command does nothing at all. Its purpose in life is to serve |
| 1050 | ** as a point for setting breakpoints in a debugger. |
| 1051 | */ |
| 1052 | static int breakpoint_command( |
| 1053 | Th_Interp *interp, |
| 1054 | void *ctx, |
| 1055 | int argc, |
| 1056 | const char **argv, |
| 1057 | int *argl |
| 1058 | ){ |
| 1059 | int cnt = 0; |
| 1060 | cnt++; |
| 1061 | return TH_OK; |
| @@ -1078,11 +1078,11 @@ | |
| 1078 | {"if", if_command, 0}, |
| 1079 | {"info", info_command, 0}, |
| 1080 | {"lindex", lindex_command, 0}, |
| 1081 | {"list", list_command, 0}, |
| 1082 | {"llength", llength_command, 0}, |
| 1083 | {"proc", proc_command, 0}, |
| 1084 | {"rename", rename_command, 0}, |
| 1085 | {"set", set_command, 0}, |
| 1086 | {"string", string_command, 0}, |
| 1087 | {"unset", unset_command, 0}, |
| 1088 | {"uplevel", uplevel_command, 0}, |
| @@ -1089,13 +1089,13 @@ | |
| 1089 | {"upvar", upvar_command, 0}, |
| 1090 | |
| 1091 | {"breakpoint", breakpoint_command, 0}, |
| 1092 | |
| 1093 | {"return", return_command, 0}, |
| 1094 | {"break", simple_command, (void *)TH_BREAK}, |
| 1095 | {"continue", simple_command, (void *)TH_CONTINUE}, |
| 1096 | {"error", simple_command, (void *)TH_ERROR}, |
| 1097 | |
| 1098 | {0, 0, 0} |
| 1099 | }; |
| 1100 | int i; |
| 1101 | |
| 1102 |