Fossil SCM
Update jimsh0.c to version 0.84, released on 2026-07-16.
Commit
ab59ac7f9f2fdaecd867ac0924bb71071e3a94bc643ca6a85e4225785af1f56a
Parent
030455006f3bb83…
1 file changed
+2226
-1174
+2226
-1174
| --- autosetup/jimsh0.c | ||
| +++ autosetup/jimsh0.c | ||
| @@ -1,7 +1,7 @@ | ||
| 1 | -/* This is single source file, bootstrap version of Jim Tcl. See http://jim.tcl.tk/ */ | |
| 2 | -#define JIM_TCL_COMPAT | |
| 1 | +/* This is single source file, bootstrap version of Jim Tcl. See http://jim.tcl-lang.org/ */ | |
| 2 | +#define JIM_COMPAT | |
| 3 | 3 | #define JIM_ANSIC |
| 4 | 4 | #define JIM_REGEXP |
| 5 | 5 | #define HAVE_NO_AUTOCONF |
| 6 | 6 | #define JIM_TINY |
| 7 | 7 | #define _JIMAUTOCONF_H |
| @@ -11,11 +11,10 @@ | ||
| 11 | 11 | #define jim_ext_readdir |
| 12 | 12 | #define jim_ext_regexp |
| 13 | 13 | #define jim_ext_file |
| 14 | 14 | #define jim_ext_glob |
| 15 | 15 | #define jim_ext_exec |
| 16 | -#define jim_ext_posix | |
| 17 | 16 | #define jim_ext_clock |
| 18 | 17 | #define jim_ext_array |
| 19 | 18 | #define jim_ext_stdlib |
| 20 | 19 | #define jim_ext_tclcompat |
| 21 | 20 | #if defined(_MSC_VER) |
| @@ -49,11 +48,15 @@ | ||
| 49 | 48 | #define vfork fork |
| 50 | 49 | #define _POSIX_SOURCE |
| 51 | 50 | #else |
| 52 | 51 | #define _GNU_SOURCE |
| 53 | 52 | #endif |
| 53 | +#ifndef __ixemul__ | |
| 54 | 54 | #define HAVE_FORK |
| 55 | +#else | |
| 56 | +#define HAVE_VFORK | |
| 57 | +#endif | |
| 55 | 58 | #define HAVE_WAITPID |
| 56 | 59 | #define HAVE_ISATTY |
| 57 | 60 | #define HAVE_MKSTEMP |
| 58 | 61 | #define HAVE_LINK |
| 59 | 62 | #define HAVE_SYS_TIME_H |
| @@ -61,11 +64,11 @@ | ||
| 61 | 64 | #define HAVE_UNISTD_H |
| 62 | 65 | #define HAVE_UMASK |
| 63 | 66 | #define HAVE_PIPE |
| 64 | 67 | #define _FILE_OFFSET_BITS 64 |
| 65 | 68 | #endif |
| 66 | -#define JIM_VERSION 83 | |
| 69 | +#define JIM_VERSION 84 | |
| 67 | 70 | #ifndef JIM_WIN32COMPAT_H |
| 68 | 71 | #define JIM_WIN32COMPAT_H |
| 69 | 72 | |
| 70 | 73 | |
| 71 | 74 | |
| @@ -94,10 +97,13 @@ | ||
| 94 | 97 | #pragma warning(disable:4146) |
| 95 | 98 | #endif |
| 96 | 99 | |
| 97 | 100 | #include <limits.h> |
| 98 | 101 | #define jim_wide _int64 |
| 102 | +#ifndef HAVE_LONG_LONG | |
| 103 | +#define HAVE_LONG_LONG | |
| 104 | +#endif | |
| 99 | 105 | #ifndef LLONG_MAX |
| 100 | 106 | #define LLONG_MAX 9223372036854775807I64 |
| 101 | 107 | #endif |
| 102 | 108 | #ifndef LLONG_MIN |
| 103 | 109 | #define LLONG_MIN (-LLONG_MAX - 1I64) |
| @@ -108,15 +114,11 @@ | ||
| 108 | 114 | #define strcasecmp _stricmp |
| 109 | 115 | #define strtoull _strtoui64 |
| 110 | 116 | |
| 111 | 117 | #include <io.h> |
| 112 | 118 | |
| 113 | -struct timeval { | |
| 114 | - long tv_sec; | |
| 115 | - long tv_usec; | |
| 116 | -}; | |
| 117 | - | |
| 119 | +#include <winsock.h> | |
| 118 | 120 | int gettimeofday(struct timeval *tv, void *unused); |
| 119 | 121 | |
| 120 | 122 | #define HAVE_OPENDIR |
| 121 | 123 | struct dirent { |
| 122 | 124 | char *d_name; |
| @@ -158,20 +160,18 @@ | ||
| 158 | 160 | #ifndef JIM_UTF8 |
| 159 | 161 | #include <ctype.h> |
| 160 | 162 | |
| 161 | 163 | |
| 162 | 164 | #define utf8_strlen(S, B) ((B) < 0 ? (int)strlen(S) : (B)) |
| 163 | -#define utf8_strwidth(S, B) utf8_strlen((S), (B)) | |
| 164 | 165 | #define utf8_tounicode(S, CP) (*(CP) = (unsigned char)*(S), 1) |
| 165 | 166 | #define utf8_getchars(CP, C) (*(CP) = (C), 1) |
| 166 | 167 | #define utf8_upper(C) toupper(C) |
| 167 | 168 | #define utf8_title(C) toupper(C) |
| 168 | 169 | #define utf8_lower(C) tolower(C) |
| 169 | 170 | #define utf8_index(C, I) (I) |
| 170 | 171 | #define utf8_charlen(C) 1 |
| 171 | 172 | #define utf8_prev_len(S, L) 1 |
| 172 | -#define utf8_width(C) 1 | |
| 173 | 173 | |
| 174 | 174 | #else |
| 175 | 175 | |
| 176 | 176 | #endif |
| 177 | 177 | |
| @@ -222,24 +222,28 @@ | ||
| 222 | 222 | # else |
| 223 | 223 | # define JIM_WIDE_MODIFIER "ld" |
| 224 | 224 | # define strtoull strtoul |
| 225 | 225 | # endif |
| 226 | 226 | #endif |
| 227 | + | |
| 227 | 228 | |
| 228 | 229 | #define UCHAR(c) ((unsigned char)(c)) |
| 229 | 230 | |
| 230 | 231 | |
| 231 | 232 | |
| 232 | -#define JIM_ABI_VERSION 101 | |
| 233 | +#define JIM_ABI_VERSION 102 | |
| 234 | + | |
| 233 | 235 | |
| 234 | 236 | #define JIM_OK 0 |
| 235 | 237 | #define JIM_ERR 1 |
| 236 | 238 | #define JIM_RETURN 2 |
| 237 | 239 | #define JIM_BREAK 3 |
| 238 | 240 | #define JIM_CONTINUE 4 |
| 239 | 241 | #define JIM_SIGNAL 5 |
| 240 | 242 | #define JIM_EXIT 6 |
| 243 | + | |
| 244 | +#define JIM_USAGE -1 | |
| 241 | 245 | |
| 242 | 246 | #define JIM_EVAL 7 |
| 243 | 247 | |
| 244 | 248 | #define JIM_MAX_CALLFRAME_DEPTH 1000 |
| 245 | 249 | #define JIM_MAX_EVAL_DEPTH 2000 |
| @@ -247,11 +251,10 @@ | ||
| 247 | 251 | |
| 248 | 252 | #define JIM_PRIV_FLAG_SHIFT 20 |
| 249 | 253 | |
| 250 | 254 | #define JIM_NONE 0 |
| 251 | 255 | #define JIM_ERRMSG 1 |
| 252 | -#define JIM_ENUM_ABBREV 2 | |
| 253 | 256 | #define JIM_UNSHARED 4 |
| 254 | 257 | #define JIM_MUSTEXIST 8 |
| 255 | 258 | #define JIM_NORESULT 16 |
| 256 | 259 | |
| 257 | 260 | |
| @@ -258,17 +261,26 @@ | ||
| 258 | 261 | #define JIM_SUBST_NOVAR 1 |
| 259 | 262 | #define JIM_SUBST_NOCMD 2 |
| 260 | 263 | #define JIM_SUBST_NOESC 4 |
| 261 | 264 | #define JIM_SUBST_FLAG 128 |
| 262 | 265 | |
| 266 | +#define JIM_TAINT_STD 1 /* The "normal" type of taint. Allows for multiple | |
| 267 | + * types of taint in the future | |
| 268 | + */ | |
| 269 | +#define JIM_TAINT_ANY ~0 | |
| 270 | + | |
| 271 | + | |
| 272 | +#define JIM_ENUM_ABBREV 2 | |
| 273 | + | |
| 263 | 274 | |
| 264 | 275 | #define JIM_CASESENS 0 |
| 265 | 276 | #define JIM_NOCASE 1 |
| 266 | 277 | #define JIM_OPT_END 2 |
| 267 | 278 | |
| 268 | 279 | |
| 269 | 280 | #define JIM_PATH_LEN 1024 |
| 281 | + | |
| 270 | 282 | |
| 271 | 283 | |
| 272 | 284 | #define JIM_NOTUSED(V) ((void) V) |
| 273 | 285 | |
| 274 | 286 | #define JIM_LIBPATH "auto_path" |
| @@ -277,10 +289,11 @@ | ||
| 277 | 289 | |
| 278 | 290 | typedef struct Jim_Stack { |
| 279 | 291 | int len; |
| 280 | 292 | int maxlen; |
| 281 | 293 | void **vector; |
| 294 | + void (*freefunc) (void *ptr); | |
| 282 | 295 | } Jim_Stack; |
| 283 | 296 | |
| 284 | 297 | |
| 285 | 298 | typedef struct Jim_HashEntry { |
| 286 | 299 | void *key; |
| @@ -318,55 +331,69 @@ | ||
| 318 | 331 | } Jim_HashTableIterator; |
| 319 | 332 | |
| 320 | 333 | |
| 321 | 334 | #define JIM_HT_INITIAL_SIZE 16 |
| 322 | 335 | |
| 336 | + | |
| 323 | 337 | |
| 324 | 338 | #define Jim_FreeEntryVal(ht, entry) \ |
| 325 | 339 | if ((ht)->type->valDestructor) \ |
| 326 | 340 | (ht)->type->valDestructor((ht)->privdata, (entry)->u.val) |
| 341 | + | |
| 327 | 342 | |
| 328 | 343 | #define Jim_SetHashVal(ht, entry, _val_) do { \ |
| 329 | 344 | if ((ht)->type->valDup) \ |
| 330 | 345 | (entry)->u.val = (ht)->type->valDup((ht)->privdata, (_val_)); \ |
| 331 | 346 | else \ |
| 332 | 347 | (entry)->u.val = (_val_); \ |
| 333 | 348 | } while(0) |
| 349 | + | |
| 334 | 350 | |
| 335 | 351 | #define Jim_SetHashIntVal(ht, entry, _val_) (entry)->u.intval = (_val_) |
| 352 | + | |
| 336 | 353 | |
| 337 | 354 | #define Jim_FreeEntryKey(ht, entry) \ |
| 338 | 355 | if ((ht)->type->keyDestructor) \ |
| 339 | 356 | (ht)->type->keyDestructor((ht)->privdata, (entry)->key) |
| 357 | + | |
| 340 | 358 | |
| 341 | 359 | #define Jim_SetHashKey(ht, entry, _key_) do { \ |
| 342 | 360 | if ((ht)->type->keyDup) \ |
| 343 | 361 | (entry)->key = (ht)->type->keyDup((ht)->privdata, (_key_)); \ |
| 344 | 362 | else \ |
| 345 | 363 | (entry)->key = (void *)(_key_); \ |
| 346 | 364 | } while(0) |
| 365 | + | |
| 347 | 366 | |
| 348 | 367 | #define Jim_CompareHashKeys(ht, key1, key2) \ |
| 349 | 368 | (((ht)->type->keyCompare) ? \ |
| 350 | 369 | (ht)->type->keyCompare((ht)->privdata, (key1), (key2)) : \ |
| 351 | 370 | (key1) == (key2)) |
| 371 | + | |
| 352 | 372 | |
| 353 | 373 | #define Jim_HashKey(ht, key) ((ht)->type->hashFunction(key) + (ht)->uniq) |
| 374 | + | |
| 354 | 375 | |
| 355 | 376 | #define Jim_GetHashEntryKey(he) ((he)->key) |
| 377 | + | |
| 356 | 378 | #define Jim_GetHashEntryVal(he) ((he)->u.val) |
| 379 | + | |
| 357 | 380 | #define Jim_GetHashEntryIntVal(he) ((he)->u.intval) |
| 381 | + | |
| 358 | 382 | #define Jim_GetHashTableCollisions(ht) ((ht)->collisions) |
| 383 | + | |
| 359 | 384 | #define Jim_GetHashTableSize(ht) ((ht)->size) |
| 385 | + | |
| 360 | 386 | #define Jim_GetHashTableUsed(ht) ((ht)->used) |
| 361 | 387 | |
| 362 | 388 | |
| 363 | 389 | typedef struct Jim_Obj { |
| 364 | 390 | char *bytes; |
| 365 | 391 | const struct Jim_ObjType *typePtr; |
| 366 | 392 | int refCount; |
| 367 | 393 | int length; |
| 394 | + unsigned taint; | |
| 368 | 395 | |
| 369 | 396 | union { |
| 370 | 397 | |
| 371 | 398 | jim_wide wideValue; |
| 372 | 399 | |
| @@ -433,27 +460,33 @@ | ||
| 433 | 460 | } internalRep; |
| 434 | 461 | struct Jim_Obj *prevObjPtr; |
| 435 | 462 | struct Jim_Obj *nextObjPtr; |
| 436 | 463 | } Jim_Obj; |
| 437 | 464 | |
| 465 | + | |
| 438 | 466 | |
| 439 | 467 | #define Jim_IncrRefCount(objPtr) \ |
| 440 | 468 | ++(objPtr)->refCount |
| 469 | + | |
| 441 | 470 | #define Jim_DecrRefCount(interp, objPtr) \ |
| 442 | 471 | if (--(objPtr)->refCount <= 0) Jim_FreeObj(interp, objPtr) |
| 472 | + | |
| 443 | 473 | #define Jim_IsShared(objPtr) \ |
| 444 | 474 | ((objPtr)->refCount > 1) |
| 445 | 475 | |
| 446 | 476 | #define Jim_FreeNewObj Jim_FreeObj |
| 477 | + | |
| 447 | 478 | |
| 448 | 479 | |
| 449 | 480 | #define Jim_FreeIntRep(i,o) \ |
| 450 | 481 | if ((o)->typePtr && (o)->typePtr->freeIntRepProc) \ |
| 451 | 482 | (o)->typePtr->freeIntRepProc(i, o) |
| 452 | 483 | |
| 484 | + | |
| 453 | 485 | |
| 454 | 486 | #define Jim_GetIntRepPtr(o) (o)->internalRep.ptr |
| 487 | + | |
| 455 | 488 | |
| 456 | 489 | |
| 457 | 490 | #define Jim_SetIntRepPtr(o, p) \ |
| 458 | 491 | (o)->internalRep.ptr = (p) |
| 459 | 492 | |
| @@ -490,12 +523,10 @@ | ||
| 490 | 523 | int argc; |
| 491 | 524 | Jim_Obj *procArgsObjPtr; |
| 492 | 525 | Jim_Obj *procBodyObjPtr; |
| 493 | 526 | struct Jim_CallFrame *next; |
| 494 | 527 | Jim_Obj *nsObj; |
| 495 | - Jim_Obj *unused_fileNameObj; | |
| 496 | - int unused_line; | |
| 497 | 528 | Jim_Stack *localCommands; |
| 498 | 529 | struct Jim_Obj *tailcallObj; |
| 499 | 530 | struct Jim_Cmd *tailcallCmd; |
| 500 | 531 | } Jim_CallFrame; |
| 501 | 532 | |
| @@ -534,21 +565,31 @@ | ||
| 534 | 565 | int len; |
| 535 | 566 | int maxLen; |
| 536 | 567 | unsigned int dummy; |
| 537 | 568 | } Jim_Dict; |
| 538 | 569 | |
| 570 | +#define JIM_CMD_ISPROC 1 | |
| 571 | +#define JIM_CMD_ISCHANNEL 2 | |
| 572 | +#define JIM_CMD_ISALIAS 4 | |
| 573 | + | |
| 574 | +#define JIM_CMD_NOTAINT 0x100 | |
| 575 | + | |
| 539 | 576 | typedef struct Jim_Cmd { |
| 540 | 577 | int inUse; |
| 541 | - int isproc; | |
| 578 | + int flags; | |
| 542 | 579 | struct Jim_Cmd *prevCmd; |
| 543 | 580 | Jim_Obj *cmdNameObj; |
| 544 | 581 | union { |
| 545 | 582 | struct { |
| 546 | 583 | |
| 547 | 584 | Jim_CmdProc *cmdProc; |
| 548 | 585 | Jim_DelCmdProc *delProc; |
| 549 | 586 | void *privData; |
| 587 | + const char *usage; | |
| 588 | + const char *help; | |
| 589 | + short minargs; | |
| 590 | + short maxargs; | |
| 550 | 591 | } native; |
| 551 | 592 | struct { |
| 552 | 593 | |
| 553 | 594 | Jim_Obj *argListObjPtr; |
| 554 | 595 | Jim_Obj *bodyObjPtr; |
| @@ -573,11 +614,10 @@ | ||
| 573 | 614 | unsigned int i, j; |
| 574 | 615 | } Jim_PrngState; |
| 575 | 616 | |
| 576 | 617 | typedef struct Jim_Interp { |
| 577 | 618 | Jim_Obj *result; |
| 578 | - int unused_errorLine; | |
| 579 | 619 | Jim_Obj *currentFilenameObj; |
| 580 | 620 | int break_level; |
| 581 | 621 | int maxCallFrameDepth; |
| 582 | 622 | int maxEvalDepth; |
| 583 | 623 | int evalDepth; |
| @@ -601,15 +641,13 @@ | ||
| 601 | 641 | int local; |
| 602 | 642 | int quitting; |
| 603 | 643 | int safeexpr; |
| 604 | 644 | Jim_Obj *liveList; |
| 605 | 645 | Jim_Obj *freeList; |
| 606 | - Jim_Obj *unused_currentScriptObj; | |
| 607 | 646 | Jim_EvalFrame topEvalFrame; |
| 608 | 647 | Jim_EvalFrame *evalFrame; |
| 609 | 648 | int procLevel; |
| 610 | - Jim_Obj * const *unused_argv; | |
| 611 | 649 | Jim_Obj *nullScriptObj; |
| 612 | 650 | Jim_Obj *emptyObj; |
| 613 | 651 | Jim_Obj *trueObj; |
| 614 | 652 | Jim_Obj *falseObj; |
| 615 | 653 | unsigned long referenceNextId; |
| @@ -624,11 +662,11 @@ | ||
| 624 | 662 | Jim_Obj *errorProc; |
| 625 | 663 | Jim_Obj *unknown; |
| 626 | 664 | Jim_Obj *defer; |
| 627 | 665 | Jim_Obj *traceCmdObj; |
| 628 | 666 | int unknown_called; |
| 629 | - int errorFlag; | |
| 667 | + int hasErrorStackTrace; | |
| 630 | 668 | void *cmdPrivData; /* Used to pass the private data pointer to |
| 631 | 669 | a command. It is set to what the user specified |
| 632 | 670 | via Jim_CreateCommand(). */ |
| 633 | 671 | |
| 634 | 672 | Jim_Cmd *oldCmdCache; |
| @@ -636,26 +674,35 @@ | ||
| 636 | 674 | struct Jim_CallFrame *freeFramesList; |
| 637 | 675 | struct Jim_HashTable assocData; |
| 638 | 676 | Jim_PrngState *prngState; |
| 639 | 677 | struct Jim_HashTable packages; |
| 640 | 678 | Jim_Stack *loadHandles; |
| 679 | + unsigned taint; | |
| 641 | 680 | } Jim_Interp; |
| 681 | + | |
| 642 | 682 | |
| 643 | 683 | #define Jim_SetResultString(i,s,l) Jim_SetResult(i, Jim_NewStringObj(i,s,l)) |
| 684 | + | |
| 644 | 685 | #define Jim_SetResultInt(i,intval) Jim_SetResult(i, Jim_NewIntObj(i,intval)) |
| 686 | + | |
| 645 | 687 | |
| 646 | 688 | #define Jim_SetResultBool(i,b) Jim_SetResultInt(i, b) |
| 689 | + | |
| 647 | 690 | #define Jim_SetEmptyResult(i) Jim_SetResult(i, (i)->emptyObj) |
| 691 | + | |
| 648 | 692 | #define Jim_GetResult(i) ((i)->result) |
| 693 | + | |
| 649 | 694 | #define Jim_CmdPrivData(i) ((i)->cmdPrivData) |
| 695 | + | |
| 650 | 696 | |
| 651 | 697 | #define Jim_SetResult(i,o) do { \ |
| 652 | 698 | Jim_Obj *_resultObjPtr_ = (o); \ |
| 653 | 699 | Jim_IncrRefCount(_resultObjPtr_); \ |
| 654 | 700 | Jim_DecrRefCount(i,(i)->result); \ |
| 655 | 701 | (i)->result = _resultObjPtr_; \ |
| 656 | 702 | } while(0) |
| 703 | + | |
| 657 | 704 | |
| 658 | 705 | |
| 659 | 706 | #define Jim_GetId(i) (++(i)->id) |
| 660 | 707 | |
| 661 | 708 | |
| @@ -665,29 +712,39 @@ | ||
| 665 | 712 | Jim_Obj *objPtr; |
| 666 | 713 | Jim_Obj *finalizerCmdNamePtr; |
| 667 | 714 | char tag[JIM_REFERENCE_TAGLEN+1]; |
| 668 | 715 | } Jim_Reference; |
| 669 | 716 | |
| 717 | + | |
| 670 | 718 | |
| 671 | 719 | #define Jim_NewEmptyStringObj(i) Jim_NewStringObj(i, "", 0) |
| 720 | + | |
| 672 | 721 | #define Jim_FreeHashTableIterator(iter) Jim_Free(iter) |
| 673 | 722 | |
| 674 | 723 | #define JIM_EXPORT extern |
| 675 | 724 | |
| 676 | 725 | |
| 677 | 726 | |
| 678 | 727 | JIM_EXPORT void *(*Jim_Allocator)(void *ptr, size_t size); |
| 728 | + | |
| 679 | 729 | |
| 680 | 730 | #define Jim_Free(P) Jim_Allocator((P), 0) |
| 731 | + | |
| 681 | 732 | #define Jim_Realloc(P, S) Jim_Allocator((P), (S)) |
| 733 | + | |
| 682 | 734 | #define Jim_Alloc(S) Jim_Allocator(NULL, (S)) |
| 735 | + | |
| 683 | 736 | JIM_EXPORT char * Jim_StrDup (const char *s); |
| 737 | + | |
| 684 | 738 | JIM_EXPORT char *Jim_StrDupLen(const char *s, int l); |
| 685 | 739 | |
| 740 | + | |
| 686 | 741 | |
| 687 | 742 | JIM_EXPORT char **Jim_GetEnviron(void); |
| 743 | + | |
| 688 | 744 | JIM_EXPORT void Jim_SetEnviron(char **env); |
| 745 | + | |
| 689 | 746 | JIM_EXPORT int Jim_MakeTempFile(Jim_Interp *interp, const char *filename_template, int unlink_file); |
| 690 | 747 | #ifndef CLOCK_REALTIME |
| 691 | 748 | # define CLOCK_REALTIME 0 |
| 692 | 749 | #endif |
| 693 | 750 | #ifndef CLOCK_MONOTONIC |
| @@ -694,314 +751,485 @@ | ||
| 694 | 751 | # define CLOCK_MONOTONIC 1 |
| 695 | 752 | #endif |
| 696 | 753 | #ifndef CLOCK_MONOTONIC_RAW |
| 697 | 754 | # define CLOCK_MONOTONIC_RAW CLOCK_MONOTONIC |
| 698 | 755 | #endif |
| 756 | + | |
| 699 | 757 | JIM_EXPORT jim_wide Jim_GetTimeUsec(unsigned type); |
| 758 | + | |
| 700 | 759 | |
| 701 | 760 | |
| 702 | 761 | JIM_EXPORT int Jim_Eval(Jim_Interp *interp, const char *script); |
| 703 | 762 | |
| 763 | + | |
| 704 | 764 | |
| 705 | 765 | JIM_EXPORT int Jim_EvalSource(Jim_Interp *interp, const char *filename, int lineno, const char *script); |
| 766 | + | |
| 706 | 767 | |
| 707 | 768 | #define Jim_Eval_Named(I, S, F, L) Jim_EvalSource((I), (F), (L), (S)) |
| 769 | + | |
| 708 | 770 | |
| 709 | 771 | JIM_EXPORT int Jim_EvalGlobal(Jim_Interp *interp, const char *script); |
| 772 | + | |
| 710 | 773 | JIM_EXPORT int Jim_EvalFile(Jim_Interp *interp, const char *filename); |
| 774 | + | |
| 711 | 775 | JIM_EXPORT int Jim_EvalFileGlobal(Jim_Interp *interp, const char *filename); |
| 776 | + | |
| 712 | 777 | JIM_EXPORT int Jim_EvalObj (Jim_Interp *interp, Jim_Obj *scriptObjPtr); |
| 778 | + | |
| 713 | 779 | JIM_EXPORT int Jim_EvalObjVector (Jim_Interp *interp, int objc, |
| 714 | 780 | Jim_Obj *const *objv); |
| 781 | + | |
| 715 | 782 | JIM_EXPORT int Jim_EvalObjList(Jim_Interp *interp, Jim_Obj *listObj); |
| 783 | + | |
| 716 | 784 | JIM_EXPORT int Jim_EvalObjPrefix(Jim_Interp *interp, Jim_Obj *prefix, |
| 717 | 785 | int objc, Jim_Obj *const *objv); |
| 786 | + | |
| 718 | 787 | #define Jim_EvalPrefix(i, p, oc, ov) Jim_EvalObjPrefix((i), Jim_NewStringObj((i), (p), -1), (oc), (ov)) |
| 788 | + | |
| 719 | 789 | JIM_EXPORT int Jim_EvalNamespace(Jim_Interp *interp, Jim_Obj *scriptObj, Jim_Obj *nsObj); |
| 720 | 790 | JIM_EXPORT int Jim_SubstObj (Jim_Interp *interp, Jim_Obj *substObjPtr, |
| 721 | 791 | Jim_Obj **resObjPtrPtr, int flags); |
| 722 | 792 | |
| 793 | + | |
| 723 | 794 | |
| 724 | 795 | JIM_EXPORT Jim_Obj *Jim_GetSourceInfo(Jim_Interp *interp, Jim_Obj *objPtr, |
| 725 | 796 | int *lineptr); |
| 797 | + | |
| 726 | 798 | |
| 727 | 799 | JIM_EXPORT void Jim_SetSourceInfo(Jim_Interp *interp, Jim_Obj *objPtr, |
| 728 | 800 | Jim_Obj *fileNameObj, int lineNumber); |
| 729 | 801 | |
| 730 | 802 | |
| 731 | 803 | |
| 732 | -JIM_EXPORT void Jim_InitStack(Jim_Stack *stack); | |
| 733 | -JIM_EXPORT void Jim_FreeStack(Jim_Stack *stack); | |
| 734 | -JIM_EXPORT int Jim_StackLen(Jim_Stack *stack); | |
| 804 | + | |
| 805 | +JIM_EXPORT void Jim_StackInit(Jim_Stack *stack, void (*freefunc) (void *ptr)); | |
| 806 | + | |
| 807 | +JIM_EXPORT void Jim_StackFree(Jim_Stack *stack); | |
| 808 | + | |
| 735 | 809 | JIM_EXPORT void Jim_StackPush(Jim_Stack *stack, void *element); |
| 736 | -JIM_EXPORT void * Jim_StackPop(Jim_Stack *stack); | |
| 737 | -JIM_EXPORT void * Jim_StackPeek(Jim_Stack *stack); | |
| 738 | -JIM_EXPORT void Jim_FreeStackElements(Jim_Stack *stack, void (*freeFunc)(void *ptr)); | |
| 810 | + | |
| 811 | +JIM_EXPORT void *Jim_StackPop(Jim_Stack *stack); | |
| 812 | + | |
| 739 | 813 | |
| 740 | 814 | |
| 741 | 815 | JIM_EXPORT int Jim_InitHashTable (Jim_HashTable *ht, |
| 742 | 816 | const Jim_HashTableType *type, void *privdata); |
| 817 | + | |
| 743 | 818 | JIM_EXPORT void Jim_ExpandHashTable (Jim_HashTable *ht, |
| 744 | 819 | unsigned int size); |
| 820 | + | |
| 745 | 821 | JIM_EXPORT int Jim_AddHashEntry (Jim_HashTable *ht, const void *key, |
| 746 | 822 | void *val); |
| 823 | + | |
| 747 | 824 | JIM_EXPORT int Jim_ReplaceHashEntry (Jim_HashTable *ht, |
| 748 | 825 | const void *key, void *val); |
| 826 | + | |
| 749 | 827 | JIM_EXPORT int Jim_DeleteHashEntry (Jim_HashTable *ht, |
| 750 | 828 | const void *key); |
| 829 | + | |
| 751 | 830 | JIM_EXPORT int Jim_FreeHashTable (Jim_HashTable *ht); |
| 831 | + | |
| 752 | 832 | JIM_EXPORT Jim_HashEntry * Jim_FindHashEntry (Jim_HashTable *ht, |
| 753 | 833 | const void *key); |
| 834 | + | |
| 754 | 835 | JIM_EXPORT Jim_HashTableIterator *Jim_GetHashTableIterator |
| 755 | 836 | (Jim_HashTable *ht); |
| 837 | + | |
| 756 | 838 | JIM_EXPORT Jim_HashEntry * Jim_NextHashEntry |
| 757 | 839 | (Jim_HashTableIterator *iter); |
| 758 | 840 | |
| 841 | + | |
| 759 | 842 | |
| 760 | 843 | JIM_EXPORT Jim_Obj * Jim_NewObj (Jim_Interp *interp); |
| 844 | + | |
| 761 | 845 | JIM_EXPORT void Jim_FreeObj (Jim_Interp *interp, Jim_Obj *objPtr); |
| 846 | + | |
| 762 | 847 | JIM_EXPORT void Jim_InvalidateStringRep (Jim_Obj *objPtr); |
| 848 | + | |
| 763 | 849 | JIM_EXPORT Jim_Obj * Jim_DuplicateObj (Jim_Interp *interp, |
| 764 | 850 | Jim_Obj *objPtr); |
| 851 | + | |
| 765 | 852 | JIM_EXPORT const char * Jim_GetString(Jim_Obj *objPtr, |
| 766 | 853 | int *lenPtr); |
| 854 | + | |
| 767 | 855 | JIM_EXPORT const char *Jim_String(Jim_Obj *objPtr); |
| 856 | + | |
| 768 | 857 | JIM_EXPORT int Jim_Length(Jim_Obj *objPtr); |
| 858 | + | |
| 769 | 859 | |
| 770 | 860 | |
| 771 | 861 | JIM_EXPORT Jim_Obj * Jim_NewStringObj (Jim_Interp *interp, |
| 772 | 862 | const char *s, int len); |
| 863 | + | |
| 773 | 864 | JIM_EXPORT Jim_Obj *Jim_NewStringObjUtf8(Jim_Interp *interp, |
| 774 | 865 | const char *s, int charlen); |
| 866 | + | |
| 775 | 867 | JIM_EXPORT Jim_Obj * Jim_NewStringObjNoAlloc (Jim_Interp *interp, |
| 776 | 868 | char *s, int len); |
| 869 | + | |
| 777 | 870 | JIM_EXPORT void Jim_AppendString (Jim_Interp *interp, Jim_Obj *objPtr, |
| 778 | 871 | const char *str, int len); |
| 872 | + | |
| 779 | 873 | JIM_EXPORT void Jim_AppendObj (Jim_Interp *interp, Jim_Obj *objPtr, |
| 780 | 874 | Jim_Obj *appendObjPtr); |
| 875 | + | |
| 781 | 876 | JIM_EXPORT void Jim_AppendStrings (Jim_Interp *interp, |
| 782 | 877 | Jim_Obj *objPtr, ...); |
| 878 | + | |
| 783 | 879 | JIM_EXPORT int Jim_StringEqObj(Jim_Obj *aObjPtr, Jim_Obj *bObjPtr); |
| 880 | + | |
| 784 | 881 | JIM_EXPORT int Jim_StringMatchObj (Jim_Interp *interp, Jim_Obj *patternObjPtr, |
| 785 | 882 | Jim_Obj *objPtr, int nocase); |
| 883 | + | |
| 786 | 884 | JIM_EXPORT Jim_Obj * Jim_StringRangeObj (Jim_Interp *interp, |
| 787 | 885 | Jim_Obj *strObjPtr, Jim_Obj *firstObjPtr, |
| 788 | 886 | Jim_Obj *lastObjPtr); |
| 887 | + | |
| 789 | 888 | JIM_EXPORT Jim_Obj * Jim_FormatString (Jim_Interp *interp, |
| 790 | 889 | Jim_Obj *fmtObjPtr, int objc, Jim_Obj *const *objv); |
| 890 | + | |
| 791 | 891 | JIM_EXPORT Jim_Obj * Jim_ScanString (Jim_Interp *interp, Jim_Obj *strObjPtr, |
| 792 | 892 | Jim_Obj *fmtObjPtr, int flags); |
| 893 | + | |
| 793 | 894 | JIM_EXPORT int Jim_CompareStringImmediate (Jim_Interp *interp, |
| 794 | 895 | Jim_Obj *objPtr, const char *str); |
| 896 | + | |
| 795 | 897 | JIM_EXPORT int Jim_StringCompareObj(Jim_Interp *interp, Jim_Obj *firstObjPtr, |
| 796 | 898 | Jim_Obj *secondObjPtr, int nocase); |
| 899 | + | |
| 797 | 900 | JIM_EXPORT int Jim_Utf8Length(Jim_Interp *interp, Jim_Obj *objPtr); |
| 901 | + | |
| 798 | 902 | |
| 799 | 903 | |
| 800 | 904 | JIM_EXPORT Jim_Obj * Jim_NewReference (Jim_Interp *interp, |
| 801 | 905 | Jim_Obj *objPtr, Jim_Obj *tagPtr, Jim_Obj *cmdNamePtr); |
| 906 | + | |
| 802 | 907 | JIM_EXPORT Jim_Reference * Jim_GetReference (Jim_Interp *interp, |
| 803 | 908 | Jim_Obj *objPtr); |
| 909 | + | |
| 804 | 910 | JIM_EXPORT int Jim_SetFinalizer (Jim_Interp *interp, Jim_Obj *objPtr, Jim_Obj *cmdNamePtr); |
| 911 | + | |
| 805 | 912 | JIM_EXPORT int Jim_GetFinalizer (Jim_Interp *interp, Jim_Obj *objPtr, Jim_Obj **cmdNamePtrPtr); |
| 806 | 913 | |
| 914 | + | |
| 807 | 915 | |
| 808 | 916 | JIM_EXPORT Jim_Interp * Jim_CreateInterp (void); |
| 917 | + | |
| 809 | 918 | JIM_EXPORT void Jim_FreeInterp (Jim_Interp *i); |
| 919 | + | |
| 810 | 920 | JIM_EXPORT int Jim_GetExitCode (Jim_Interp *interp); |
| 921 | + | |
| 811 | 922 | JIM_EXPORT const char *Jim_ReturnCode(int code); |
| 923 | + | |
| 812 | 924 | JIM_EXPORT void Jim_SetResultFormatted(Jim_Interp *interp, const char *format, ...); |
| 813 | 925 | |
| 926 | + | |
| 927 | +JIM_EXPORT Jim_Cmd *Jim_RegisterCommand(Jim_Interp *interp, Jim_Obj *cmdNameObj, | |
| 928 | + Jim_CmdProc *cmdProc, | |
| 929 | + Jim_DelCmdProc *delProc, | |
| 930 | + const char *usage, | |
| 931 | + const char *help, | |
| 932 | + short minargs, | |
| 933 | + short maxargs, | |
| 934 | + int flags, | |
| 935 | + void *privData); | |
| 936 | + | |
| 937 | +JIM_EXPORT int Jim_CreateCommand (Jim_Interp *interp, | |
| 938 | + const char *cmdName, Jim_CmdProc *cmdProc, void *privData, | |
| 939 | + Jim_DelCmdProc *delProc); | |
| 940 | +#define Jim_RegisterSimpleCmd(interp, name, usage, minargs, maxargs, cmdproc) \ | |
| 941 | + Jim_RegisterCommand(interp, Jim_NewStringObj(interp, name, -1), cmdproc, NULL, usage, NULL, minargs, maxargs, 0, NULL) | |
| 942 | + | |
| 943 | +#define Jim_RegisterCmd(interp, name, usage, minargs, maxargs, cmdproc, delproc, privdata, flags) \ | |
| 944 | + Jim_RegisterCommand(interp, Jim_NewStringObj(interp, name, -1), cmdproc, delproc, usage, NULL, minargs, maxargs, flags, privdata) | |
| 945 | + | |
| 814 | 946 | |
| 815 | 947 | JIM_EXPORT void Jim_RegisterCoreCommands (Jim_Interp *interp); |
| 948 | + | |
| 816 | 949 | JIM_EXPORT int Jim_CreateCommand (Jim_Interp *interp, |
| 817 | 950 | const char *cmdName, Jim_CmdProc *cmdProc, void *privData, |
| 818 | 951 | Jim_DelCmdProc *delProc); |
| 952 | + | |
| 819 | 953 | JIM_EXPORT int Jim_DeleteCommand (Jim_Interp *interp, |
| 820 | 954 | Jim_Obj *cmdNameObj); |
| 955 | + | |
| 821 | 956 | JIM_EXPORT int Jim_RenameCommand (Jim_Interp *interp, |
| 822 | 957 | Jim_Obj *oldNameObj, Jim_Obj *newNameObj); |
| 958 | + | |
| 823 | 959 | JIM_EXPORT Jim_Cmd * Jim_GetCommand (Jim_Interp *interp, |
| 824 | 960 | Jim_Obj *objPtr, int flags); |
| 961 | + | |
| 962 | + | |
| 825 | 963 | JIM_EXPORT int Jim_SetVariable (Jim_Interp *interp, |
| 826 | 964 | Jim_Obj *nameObjPtr, Jim_Obj *valObjPtr); |
| 965 | + | |
| 827 | 966 | JIM_EXPORT int Jim_SetVariableStr (Jim_Interp *interp, |
| 828 | 967 | const char *name, Jim_Obj *objPtr); |
| 968 | + | |
| 829 | 969 | JIM_EXPORT int Jim_SetGlobalVariableStr (Jim_Interp *interp, |
| 830 | 970 | const char *name, Jim_Obj *objPtr); |
| 971 | + | |
| 831 | 972 | JIM_EXPORT int Jim_SetVariableStrWithStr (Jim_Interp *interp, |
| 832 | 973 | const char *name, const char *val); |
| 974 | + | |
| 833 | 975 | JIM_EXPORT int Jim_SetVariableLink (Jim_Interp *interp, |
| 834 | 976 | Jim_Obj *nameObjPtr, Jim_Obj *targetNameObjPtr, |
| 835 | 977 | Jim_CallFrame *targetCallFrame); |
| 978 | + | |
| 836 | 979 | JIM_EXPORT Jim_Obj * Jim_MakeGlobalNamespaceName(Jim_Interp *interp, |
| 837 | 980 | Jim_Obj *nameObjPtr); |
| 838 | 981 | JIM_EXPORT Jim_Obj * Jim_GetVariable (Jim_Interp *interp, |
| 839 | 982 | Jim_Obj *nameObjPtr, int flags); |
| 983 | + | |
| 840 | 984 | JIM_EXPORT Jim_Obj * Jim_GetGlobalVariable (Jim_Interp *interp, |
| 841 | 985 | Jim_Obj *nameObjPtr, int flags); |
| 986 | + | |
| 842 | 987 | JIM_EXPORT Jim_Obj * Jim_GetVariableStr (Jim_Interp *interp, |
| 843 | 988 | const char *name, int flags); |
| 989 | + | |
| 844 | 990 | JIM_EXPORT Jim_Obj * Jim_GetGlobalVariableStr (Jim_Interp *interp, |
| 845 | 991 | const char *name, int flags); |
| 992 | + | |
| 846 | 993 | JIM_EXPORT int Jim_UnsetVariable (Jim_Interp *interp, |
| 847 | 994 | Jim_Obj *nameObjPtr, int flags); |
| 995 | + | |
| 848 | 996 | |
| 849 | 997 | |
| 850 | 998 | JIM_EXPORT Jim_CallFrame *Jim_GetCallFrameByLevel(Jim_Interp *interp, |
| 851 | 999 | Jim_Obj *levelObjPtr); |
| 852 | 1000 | |
| 1001 | + | |
| 853 | 1002 | |
| 854 | 1003 | JIM_EXPORT int Jim_Collect (Jim_Interp *interp); |
| 1004 | + | |
| 855 | 1005 | JIM_EXPORT void Jim_CollectIfNeeded (Jim_Interp *interp); |
| 1006 | + | |
| 856 | 1007 | |
| 857 | 1008 | |
| 858 | 1009 | JIM_EXPORT int Jim_GetIndex (Jim_Interp *interp, Jim_Obj *objPtr, |
| 859 | 1010 | int *indexPtr); |
| 860 | 1011 | |
| 1012 | + | |
| 861 | 1013 | |
| 862 | 1014 | JIM_EXPORT Jim_Obj * Jim_NewListObj (Jim_Interp *interp, |
| 863 | 1015 | Jim_Obj *const *elements, int len); |
| 1016 | + | |
| 864 | 1017 | JIM_EXPORT void Jim_ListInsertElements (Jim_Interp *interp, |
| 865 | 1018 | Jim_Obj *listPtr, int listindex, int objc, Jim_Obj *const *objVec); |
| 1019 | + | |
| 866 | 1020 | JIM_EXPORT void Jim_ListAppendElement (Jim_Interp *interp, |
| 867 | 1021 | Jim_Obj *listPtr, Jim_Obj *objPtr); |
| 1022 | + | |
| 868 | 1023 | JIM_EXPORT void Jim_ListAppendList (Jim_Interp *interp, |
| 869 | 1024 | Jim_Obj *listPtr, Jim_Obj *appendListPtr); |
| 1025 | + | |
| 870 | 1026 | JIM_EXPORT int Jim_ListLength (Jim_Interp *interp, Jim_Obj *objPtr); |
| 1027 | + | |
| 871 | 1028 | JIM_EXPORT int Jim_ListIndex (Jim_Interp *interp, Jim_Obj *listPrt, |
| 872 | 1029 | int listindex, Jim_Obj **objPtrPtr, int seterr); |
| 1030 | + | |
| 873 | 1031 | JIM_EXPORT Jim_Obj *Jim_ListGetIndex(Jim_Interp *interp, Jim_Obj *listPtr, int idx); |
| 1032 | + | |
| 874 | 1033 | JIM_EXPORT int Jim_SetListIndex (Jim_Interp *interp, |
| 875 | 1034 | Jim_Obj *varNamePtr, Jim_Obj *const *indexv, int indexc, |
| 876 | 1035 | Jim_Obj *newObjPtr); |
| 1036 | + | |
| 877 | 1037 | JIM_EXPORT Jim_Obj * Jim_ConcatObj (Jim_Interp *interp, int objc, |
| 878 | 1038 | Jim_Obj *const *objv); |
| 1039 | + | |
| 879 | 1040 | JIM_EXPORT Jim_Obj *Jim_ListJoin(Jim_Interp *interp, |
| 880 | 1041 | Jim_Obj *listObjPtr, const char *joinStr, int joinStrLen); |
| 881 | 1042 | |
| 1043 | + | |
| 882 | 1044 | |
| 883 | 1045 | JIM_EXPORT Jim_Obj * Jim_NewDictObj (Jim_Interp *interp, |
| 884 | 1046 | Jim_Obj *const *elements, int len); |
| 1047 | + | |
| 885 | 1048 | JIM_EXPORT int Jim_DictKey (Jim_Interp *interp, Jim_Obj *dictPtr, |
| 886 | 1049 | Jim_Obj *keyPtr, Jim_Obj **objPtrPtr, int flags); |
| 1050 | + | |
| 887 | 1051 | JIM_EXPORT int Jim_DictKeysVector (Jim_Interp *interp, |
| 888 | 1052 | Jim_Obj *dictPtr, Jim_Obj *const *keyv, int keyc, |
| 889 | 1053 | Jim_Obj **objPtrPtr, int flags); |
| 890 | 1054 | JIM_EXPORT int Jim_SetDictKeysVector (Jim_Interp *interp, |
| 891 | 1055 | Jim_Obj *varNamePtr, Jim_Obj *const *keyv, int keyc, |
| 892 | 1056 | Jim_Obj *newObjPtr, int flags); |
| 1057 | + | |
| 893 | 1058 | JIM_EXPORT Jim_Obj **Jim_DictPairs(Jim_Interp *interp, |
| 894 | 1059 | Jim_Obj *dictPtr, int *len); |
| 1060 | + | |
| 895 | 1061 | JIM_EXPORT int Jim_DictAddElement(Jim_Interp *interp, Jim_Obj *objPtr, |
| 896 | 1062 | Jim_Obj *keyObjPtr, Jim_Obj *valueObjPtr); |
| 897 | 1063 | |
| 898 | 1064 | #define JIM_DICTMATCH_KEYS 0x0001 |
| 899 | 1065 | #define JIM_DICTMATCH_VALUES 0x002 |
| 1066 | + | |
| 900 | 1067 | |
| 901 | 1068 | JIM_EXPORT int Jim_DictMatchTypes(Jim_Interp *interp, Jim_Obj *objPtr, Jim_Obj *patternObj, int match_type, int return_types); |
| 1069 | + | |
| 902 | 1070 | JIM_EXPORT int Jim_DictSize(Jim_Interp *interp, Jim_Obj *objPtr); |
| 1071 | + | |
| 903 | 1072 | JIM_EXPORT int Jim_DictInfo(Jim_Interp *interp, Jim_Obj *objPtr); |
| 1073 | + | |
| 904 | 1074 | JIM_EXPORT Jim_Obj *Jim_DictMerge(Jim_Interp *interp, int objc, Jim_Obj *const *objv); |
| 1075 | + | |
| 905 | 1076 | |
| 906 | 1077 | |
| 907 | 1078 | JIM_EXPORT int Jim_GetReturnCode (Jim_Interp *interp, Jim_Obj *objPtr, |
| 908 | 1079 | int *intPtr); |
| 909 | 1080 | |
| 1081 | + | |
| 910 | 1082 | |
| 911 | 1083 | JIM_EXPORT int Jim_EvalExpression (Jim_Interp *interp, |
| 912 | 1084 | Jim_Obj *exprObjPtr); |
| 1085 | + | |
| 913 | 1086 | JIM_EXPORT int Jim_GetBoolFromExpr (Jim_Interp *interp, |
| 914 | 1087 | Jim_Obj *exprObjPtr, int *boolPtr); |
| 1088 | + | |
| 915 | 1089 | |
| 916 | 1090 | |
| 917 | 1091 | JIM_EXPORT int Jim_GetBoolean(Jim_Interp *interp, Jim_Obj *objPtr, |
| 918 | 1092 | int *booleanPtr); |
| 919 | 1093 | |
| 1094 | + | |
| 920 | 1095 | |
| 921 | 1096 | JIM_EXPORT int Jim_GetWide (Jim_Interp *interp, Jim_Obj *objPtr, |
| 922 | 1097 | jim_wide *widePtr); |
| 1098 | + | |
| 923 | 1099 | JIM_EXPORT int Jim_GetWideExpr(Jim_Interp *interp, Jim_Obj *objPtr, |
| 924 | 1100 | jim_wide *widePtr); |
| 1101 | + | |
| 925 | 1102 | JIM_EXPORT int Jim_GetLong (Jim_Interp *interp, Jim_Obj *objPtr, |
| 926 | 1103 | long *longPtr); |
| 1104 | + | |
| 927 | 1105 | #define Jim_NewWideObj Jim_NewIntObj |
| 1106 | + | |
| 928 | 1107 | JIM_EXPORT Jim_Obj * Jim_NewIntObj (Jim_Interp *interp, |
| 929 | 1108 | jim_wide wideValue); |
| 930 | 1109 | |
| 1110 | + | |
| 931 | 1111 | |
| 932 | 1112 | JIM_EXPORT int Jim_GetDouble(Jim_Interp *interp, Jim_Obj *objPtr, |
| 933 | 1113 | double *doublePtr); |
| 1114 | + | |
| 934 | 1115 | JIM_EXPORT void Jim_SetDouble(Jim_Interp *interp, Jim_Obj *objPtr, |
| 935 | 1116 | double doubleValue); |
| 1117 | + | |
| 936 | 1118 | JIM_EXPORT Jim_Obj * Jim_NewDoubleObj(Jim_Interp *interp, double doubleValue); |
| 1119 | + | |
| 937 | 1120 | |
| 938 | 1121 | |
| 939 | 1122 | JIM_EXPORT void Jim_WrongNumArgs (Jim_Interp *interp, int argc, |
| 940 | 1123 | Jim_Obj *const *argv, const char *msg); |
| 941 | 1124 | JIM_EXPORT int Jim_GetEnum (Jim_Interp *interp, Jim_Obj *objPtr, |
| 942 | 1125 | const char * const *tablePtr, int *indexPtr, const char *name, int flags); |
| 1126 | + | |
| 943 | 1127 | JIM_EXPORT int Jim_CheckShowCommands(Jim_Interp *interp, Jim_Obj *objPtr, |
| 944 | 1128 | const char *const *tablePtr); |
| 1129 | + | |
| 945 | 1130 | JIM_EXPORT int Jim_ScriptIsComplete(Jim_Interp *interp, |
| 946 | 1131 | Jim_Obj *scriptObj, char *stateCharPtr); |
| 947 | 1132 | |
| 948 | 1133 | JIM_EXPORT int Jim_FindByName(const char *name, const char * const array[], size_t len); |
| 949 | 1134 | |
| 950 | 1135 | |
| 951 | 1136 | typedef void (Jim_InterpDeleteProc)(Jim_Interp *interp, void *data); |
| 1137 | + | |
| 952 | 1138 | JIM_EXPORT void * Jim_GetAssocData(Jim_Interp *interp, const char *key); |
| 1139 | + | |
| 953 | 1140 | JIM_EXPORT int Jim_SetAssocData(Jim_Interp *interp, const char *key, |
| 954 | 1141 | Jim_InterpDeleteProc *delProc, void *data); |
| 1142 | + | |
| 955 | 1143 | JIM_EXPORT int Jim_DeleteAssocData(Jim_Interp *interp, const char *key); |
| 1144 | + | |
| 956 | 1145 | JIM_EXPORT int Jim_CheckAbiVersion(Jim_Interp *interp, int abi_version); |
| 1146 | + | |
| 957 | 1147 | |
| 958 | 1148 | |
| 959 | 1149 | |
| 960 | 1150 | |
| 961 | 1151 | JIM_EXPORT int Jim_PackageProvide (Jim_Interp *interp, |
| 962 | 1152 | const char *name, const char *ver, int flags); |
| 1153 | + | |
| 963 | 1154 | JIM_EXPORT int Jim_PackageRequire (Jim_Interp *interp, |
| 964 | 1155 | const char *name, int flags); |
| 1156 | + | |
| 965 | 1157 | #define Jim_PackageProvideCheck(INTERP, NAME) \ |
| 966 | 1158 | if (Jim_CheckAbiVersion(INTERP, JIM_ABI_VERSION) == JIM_ERR || Jim_PackageProvide(INTERP, NAME, "1.0", JIM_ERRMSG)) \ |
| 967 | 1159 | return JIM_ERR |
| 1160 | + | |
| 968 | 1161 | |
| 969 | 1162 | |
| 970 | 1163 | JIM_EXPORT void Jim_MakeErrorMessage (Jim_Interp *interp); |
| 971 | 1164 | |
| 1165 | + | |
| 972 | 1166 | |
| 973 | 1167 | JIM_EXPORT int Jim_InteractivePrompt (Jim_Interp *interp); |
| 1168 | + | |
| 974 | 1169 | JIM_EXPORT void Jim_HistoryLoad(const char *filename); |
| 1170 | + | |
| 975 | 1171 | JIM_EXPORT void Jim_HistorySave(const char *filename); |
| 1172 | + | |
| 976 | 1173 | JIM_EXPORT char *Jim_HistoryGetline(Jim_Interp *interp, const char *prompt); |
| 1174 | + | |
| 977 | 1175 | JIM_EXPORT void Jim_HistorySetCompletion(Jim_Interp *interp, Jim_Obj *completionCommandObj); |
| 1176 | + | |
| 978 | 1177 | JIM_EXPORT void Jim_HistorySetHints(Jim_Interp *interp, Jim_Obj *hintsCommandObj); |
| 1178 | + | |
| 979 | 1179 | JIM_EXPORT void Jim_HistoryAdd(const char *line); |
| 1180 | + | |
| 980 | 1181 | JIM_EXPORT void Jim_HistoryShow(void); |
| 1182 | + | |
| 981 | 1183 | JIM_EXPORT void Jim_HistorySetMaxLen(int length); |
| 1184 | + | |
| 982 | 1185 | JIM_EXPORT int Jim_HistoryGetMaxLen(void); |
| 983 | 1186 | |
| 1187 | + | |
| 984 | 1188 | |
| 985 | 1189 | JIM_EXPORT int Jim_InitStaticExtensions(Jim_Interp *interp); |
| 1190 | + | |
| 986 | 1191 | JIM_EXPORT int Jim_StringToWide(const char *str, jim_wide *widePtr, int base); |
| 1192 | + | |
| 987 | 1193 | JIM_EXPORT int Jim_IsBigEndian(void); |
| 988 | 1194 | |
| 989 | 1195 | #define Jim_CheckSignal(i) ((i)->signal_level && (i)->sigmask) |
| 1196 | + | |
| 990 | 1197 | JIM_EXPORT void Jim_SignalSetIgnored(jim_wide mask); |
| 991 | 1198 | |
| 1199 | + | |
| 992 | 1200 | |
| 993 | 1201 | JIM_EXPORT int Jim_LoadLibrary(Jim_Interp *interp, const char *pathName); |
| 1202 | + | |
| 994 | 1203 | JIM_EXPORT void Jim_FreeLoadHandles(Jim_Interp *interp); |
| 1204 | + | |
| 995 | 1205 | |
| 996 | 1206 | |
| 997 | 1207 | JIM_EXPORT int Jim_AioFilehandle(Jim_Interp *interp, Jim_Obj *command); |
| 998 | 1208 | |
| 1209 | + | |
| 999 | 1210 | |
| 1000 | 1211 | JIM_EXPORT int Jim_IsDict(Jim_Obj *objPtr); |
| 1212 | + | |
| 1001 | 1213 | JIM_EXPORT int Jim_IsList(Jim_Obj *objPtr); |
| 1002 | 1214 | |
| 1215 | + | |
| 1216 | +JIM_EXPORT void Jim_SetTaintError(Jim_Interp *interp, int cmdargs, Jim_Obj *const *argv); | |
| 1217 | +JIM_EXPORT int Jim_CalcTaint(int argc, Jim_Obj *const *argv); | |
| 1218 | + | |
| 1219 | +#ifdef JIM_TAINT | |
| 1220 | +#define Jim_CheckTaint(i, t) ((i)->taint & (t)) | |
| 1221 | +#define Jim_TaintObj(o,t) (o)->taint |= (t) | |
| 1222 | +#define Jim_UntaintObj(o) (o)->taint = 0 | |
| 1223 | +#define Jim_GetObjTaint(o) (o)->taint | |
| 1224 | +#else | |
| 1225 | +#define Jim_CheckTaint(i, t) 0 | |
| 1226 | +#define Jim_TaintObj(o,t) | |
| 1227 | +#define Jim_UntaintObj(o) | |
| 1228 | +#define Jim_GetObjTaint(o) 0 | |
| 1229 | +#endif | |
| 1230 | + | |
| 1003 | 1231 | #ifdef __cplusplus |
| 1004 | 1232 | } |
| 1005 | 1233 | #endif |
| 1006 | 1234 | |
| 1007 | 1235 | #endif |
| @@ -1015,10 +1243,14 @@ | ||
| 1015 | 1243 | #endif |
| 1016 | 1244 | |
| 1017 | 1245 | |
| 1018 | 1246 | #define JIM_MODFLAG_HIDDEN 0x0001 |
| 1019 | 1247 | #define JIM_MODFLAG_FULLARGV 0x0002 |
| 1248 | +#define JIM_MODFLAG_NOTAINT 0x0004 | |
| 1249 | + | |
| 1250 | +#define JIM_SUBCMD_BADARGS -1 | |
| 1251 | +#define JIM_SUBCMD_TAINTED -2 | |
| 1020 | 1252 | |
| 1021 | 1253 | |
| 1022 | 1254 | |
| 1023 | 1255 | typedef int jim_subcmd_function(Jim_Interp *interp, int argc, Jim_Obj *const *argv); |
| 1024 | 1256 | |
| @@ -1040,10 +1272,13 @@ | ||
| 1040 | 1272 | int Jim_SubCmdProc(Jim_Interp *interp, int argc, Jim_Obj *const *argv); |
| 1041 | 1273 | |
| 1042 | 1274 | int Jim_CallSubCmd(Jim_Interp *interp, const jim_subcmd_type *ct, int argc, Jim_Obj *const *argv); |
| 1043 | 1275 | |
| 1044 | 1276 | void Jim_SubCmdArgError(Jim_Interp *interp, const jim_subcmd_type *ct, Jim_Obj *subcmd); |
| 1277 | + | |
| 1278 | +Jim_Cmd *Jim_RegisterSubCmd(Jim_Interp *interp, const char *cmdname, | |
| 1279 | + const jim_subcmd_type *command_table, Jim_DelCmdProc *delProc); | |
| 1045 | 1280 | |
| 1046 | 1281 | #ifdef __cplusplus |
| 1047 | 1282 | } |
| 1048 | 1283 | #endif |
| 1049 | 1284 | |
| @@ -1074,10 +1309,11 @@ | ||
| 1074 | 1309 | int regstart; |
| 1075 | 1310 | int reganch; |
| 1076 | 1311 | int regmust; |
| 1077 | 1312 | int regmlen; |
| 1078 | 1313 | int *program; |
| 1314 | + char *exp; | |
| 1079 | 1315 | |
| 1080 | 1316 | |
| 1081 | 1317 | const char *regparse; |
| 1082 | 1318 | int p; |
| 1083 | 1319 | int proglen; |
| @@ -1094,14 +1330,18 @@ | ||
| 1094 | 1330 | } regexp; |
| 1095 | 1331 | |
| 1096 | 1332 | typedef regexp regex_t; |
| 1097 | 1333 | |
| 1098 | 1334 | #define REG_EXTENDED 0 |
| 1099 | -#define REG_NEWLINE 1 | |
| 1100 | 1335 | #define REG_ICASE 2 |
| 1336 | +#define REG_NEWLINE_ANCHOR 4 | |
| 1337 | +#define REG_NEWLINE_STOP 8 | |
| 1338 | + | |
| 1339 | +#define REG_NEWLINE (REG_NEWLINE_ANCHOR | REG_NEWLINE_STOP) | |
| 1101 | 1340 | |
| 1102 | 1341 | #define REG_NOTBOL 16 |
| 1342 | +#define REG_EXPANDED 32 | |
| 1103 | 1343 | |
| 1104 | 1344 | enum { |
| 1105 | 1345 | REG_NOERROR, |
| 1106 | 1346 | REG_NOMATCH, |
| 1107 | 1347 | REG_BADPAT, |
| @@ -1162,11 +1402,11 @@ | ||
| 1162 | 1402 | |
| 1163 | 1403 | int Jim_OpenForWrite(const char *filename, int append); |
| 1164 | 1404 | |
| 1165 | 1405 | int Jim_OpenForRead(const char *filename); |
| 1166 | 1406 | |
| 1167 | -#if defined(__MINGW32__) | |
| 1407 | +#if defined(__MINGW32__) || defined(_WIN32) | |
| 1168 | 1408 | #ifndef STRICT |
| 1169 | 1409 | #define STRICT |
| 1170 | 1410 | #endif |
| 1171 | 1411 | #define WIN32_LEAN_AND_MEAN |
| 1172 | 1412 | #include <windows.h> |
| @@ -1197,10 +1437,16 @@ | ||
| 1197 | 1437 | |
| 1198 | 1438 | typedef struct __stat64 jim_stat_t; |
| 1199 | 1439 | #define Jim_Stat _stat64 |
| 1200 | 1440 | #define Jim_FileStat _fstat64 |
| 1201 | 1441 | #define Jim_Lseek _lseeki64 |
| 1442 | + #define O_TEXT _O_TEXT | |
| 1443 | + #define O_BINARY _O_BINARY | |
| 1444 | + #define Jim_SetMode _setmode | |
| 1445 | + #ifndef STDIN_FILENO | |
| 1446 | + #define STDIN_FILENO 0 | |
| 1447 | + #endif | |
| 1202 | 1448 | |
| 1203 | 1449 | #else |
| 1204 | 1450 | #if defined(HAVE_STAT64) |
| 1205 | 1451 | typedef struct stat64 jim_stat_t; |
| 1206 | 1452 | #define Jim_Stat stat64 |
| @@ -1239,15 +1485,24 @@ | ||
| 1239 | 1485 | |
| 1240 | 1486 | #ifndef HAVE_EXECVPE |
| 1241 | 1487 | #define execvpe(ARG0, ARGV, ENV) execvp(ARG0, ARGV) |
| 1242 | 1488 | #endif |
| 1243 | 1489 | #endif |
| 1490 | + | |
| 1491 | + #ifndef O_TEXT | |
| 1492 | + #define O_TEXT 0 | |
| 1493 | + #endif | |
| 1494 | + | |
| 1244 | 1495 | #endif |
| 1245 | 1496 | |
| 1246 | -#ifndef O_TEXT | |
| 1247 | -#define O_TEXT 0 | |
| 1248 | -#endif | |
| 1497 | +# ifndef MAXPATHLEN | |
| 1498 | +# ifdef PATH_MAX | |
| 1499 | +# define MAXPATHLEN PATH_MAX | |
| 1500 | +# else | |
| 1501 | +# define MAXPATHLEN JIM_PATH_LEN | |
| 1502 | +# endif | |
| 1503 | +# endif | |
| 1249 | 1504 | |
| 1250 | 1505 | |
| 1251 | 1506 | int Jim_FileStoreStatData(Jim_Interp *interp, Jim_Obj *varName, const jim_stat_t *sb); |
| 1252 | 1507 | |
| 1253 | 1508 | #endif |
| @@ -1291,12 +1546,18 @@ | ||
| 1291 | 1546 | "\n" |
| 1292 | 1547 | " if {[exists jim::argv0]} {\n" |
| 1293 | 1548 | " if {[string match \"*/*\" $jim::argv0]} {\n" |
| 1294 | 1549 | " set jim::exe [file join [pwd] $jim::argv0]\n" |
| 1295 | 1550 | " } else {\n" |
| 1296 | -" foreach path [split [env PATH \"\"] $tcl_platform(pathSeparator)] {\n" | |
| 1297 | -" set exec [file join [pwd] [string map {\\\\ /} $path] $jim::argv0]\n" | |
| 1551 | +" set jim::argv0 [file tail $jim::argv0]\n" | |
| 1552 | +" set path [split [env PATH \"\"] $tcl_platform(pathSeparator)]\n" | |
| 1553 | +" if {$tcl_platform(platform) eq \"windows\"} {\n" | |
| 1554 | +"\n" | |
| 1555 | +" set path [lmap p [list \"\" {*}$path] { string map {\\\\ /} $p }]\n" | |
| 1556 | +" }\n" | |
| 1557 | +" foreach p $path {\n" | |
| 1558 | +" set exec [file join [pwd] $p $jim::argv0]\n" | |
| 1298 | 1559 | " if {[file executable $exec]} {\n" |
| 1299 | 1560 | " set jim::exe $exec\n" |
| 1300 | 1561 | " break\n" |
| 1301 | 1562 | " }\n" |
| 1302 | 1563 | " }\n" |
| @@ -1387,12 +1648,12 @@ | ||
| 1387 | 1648 | "set tcl::stdhint_col $tcl::stdhint_cols(lcyan)\n" |
| 1388 | 1649 | "\n" |
| 1389 | 1650 | "\n" |
| 1390 | 1651 | "proc tcl::stdhint {string} {\n" |
| 1391 | 1652 | " set result \"\"\n" |
| 1653 | +" lassign $string cmd arg\n" | |
| 1392 | 1654 | " if {[llength $string] >= 2} {\n" |
| 1393 | -" lassign $string cmd arg\n" | |
| 1394 | 1655 | " if {$cmd in $::tcl::stdhint_commands || [info channel $cmd] ne \"\"} {\n" |
| 1395 | 1656 | " catch {\n" |
| 1396 | 1657 | " set help [$cmd -help $arg]\n" |
| 1397 | 1658 | " if {[string match \"Usage: $cmd *\" $help]} {\n" |
| 1398 | 1659 | " set n [llength $string]\n" |
| @@ -1410,10 +1671,27 @@ | ||
| 1410 | 1671 | " }\n" |
| 1411 | 1672 | " set result [list $prefix$hint {*}$::tcl::stdhint_col]\n" |
| 1412 | 1673 | " }\n" |
| 1413 | 1674 | " }\n" |
| 1414 | 1675 | " }\n" |
| 1676 | +" } else {\n" | |
| 1677 | +" catch {\n" | |
| 1678 | +" if {[exists -alias $cmd] && [llength [info alias $cmd]] == 1} {\n" | |
| 1679 | +"\n" | |
| 1680 | +"\n" | |
| 1681 | +"\n" | |
| 1682 | +" set help [info usage [info alias $cmd]]\n" | |
| 1683 | +" } else {\n" | |
| 1684 | +" set help [info usage $cmd]\n" | |
| 1685 | +" }\n" | |
| 1686 | +" set hint [join [lrange $help 1 end]]\n" | |
| 1687 | +" set prefix \" \"\n" | |
| 1688 | +" if {[string match \"* \" $string]} {\n" | |
| 1689 | +" set prefix \"\"\n" | |
| 1690 | +" }\n" | |
| 1691 | +" set result [list $prefix$hint {*}$::tcl::stdhint_col]\n" | |
| 1692 | +" }\n" | |
| 1415 | 1693 | " }\n" |
| 1416 | 1694 | " return $result\n" |
| 1417 | 1695 | "}\n" |
| 1418 | 1696 | "\n" |
| 1419 | 1697 | "_jimsh_init\n" |
| @@ -1856,11 +2134,11 @@ | ||
| 1856 | 2134 | " }\n" |
| 1857 | 2135 | " -bu* {\n" |
| 1858 | 2136 | " $f buffering $v\n" |
| 1859 | 2137 | " }\n" |
| 1860 | 2138 | " -tr* {\n" |
| 1861 | -"\n" | |
| 2139 | +" $f translation $v\n" | |
| 1862 | 2140 | " }\n" |
| 1863 | 2141 | " default {\n" |
| 1864 | 2142 | " return -code error \"fconfigure: unknown option $n\"\n" |
| 1865 | 2143 | " }\n" |
| 1866 | 2144 | " }\n" |
| @@ -1873,11 +2151,13 @@ | ||
| 1873 | 2151 | " tailcall {*}$args\n" |
| 1874 | 2152 | "}\n" |
| 1875 | 2153 | "\n" |
| 1876 | 2154 | "\n" |
| 1877 | 2155 | "\n" |
| 1878 | -"proc parray {arrayname {pattern *} {puts puts}} {\n" | |
| 2156 | +"\n" | |
| 2157 | +"\n" | |
| 2158 | +"proc parray {arrayname {pattern *} {puts puts} args} {\n" | |
| 1879 | 2159 | " upvar $arrayname a\n" |
| 1880 | 2160 | "\n" |
| 1881 | 2161 | " set max 0\n" |
| 1882 | 2162 | " foreach name [array names a $pattern]] {\n" |
| 1883 | 2163 | " if {[string length $name] > $max} {\n" |
| @@ -1885,11 +2165,11 @@ | ||
| 1885 | 2165 | " }\n" |
| 1886 | 2166 | " }\n" |
| 1887 | 2167 | " incr max [string length $arrayname]\n" |
| 1888 | 2168 | " incr max 2\n" |
| 1889 | 2169 | " foreach name [lsort [array names a $pattern]] {\n" |
| 1890 | -" $puts [format \"%-${max}s = %s\" $arrayname\\($name\\) $a($name)]\n" | |
| 2170 | +" $puts {*}$args [format \"%-${max}s = %s\" $arrayname\\($name\\) $a($name)]\n" | |
| 1891 | 2171 | " }\n" |
| 1892 | 2172 | "}\n" |
| 1893 | 2173 | "\n" |
| 1894 | 2174 | "\n" |
| 1895 | 2175 | "proc {file copy} {{force {}} source target} {\n" |
| @@ -1945,13 +2225,10 @@ | ||
| 1945 | 2225 | " }\n" |
| 1946 | 2226 | " lambda {cmd args} {f pids} {\n" |
| 1947 | 2227 | " if {$cmd eq \"pid\"} {\n" |
| 1948 | 2228 | " return $pids\n" |
| 1949 | 2229 | " }\n" |
| 1950 | -" if {$cmd eq \"getfd\"} {\n" | |
| 1951 | -" $f getfd\n" | |
| 1952 | -" }\n" | |
| 1953 | 2230 | " if {$cmd eq \"close\"} {\n" |
| 1954 | 2231 | " $f close\n" |
| 1955 | 2232 | "\n" |
| 1956 | 2233 | " set retopts {}\n" |
| 1957 | 2234 | " foreach p $pids {\n" |
| @@ -2047,18 +2324,21 @@ | ||
| 2047 | 2324 | #ifdef HAVE_TERMIOS_H |
| 2048 | 2325 | #endif |
| 2049 | 2326 | |
| 2050 | 2327 | |
| 2051 | 2328 | #define AIO_CMD_LEN 32 |
| 2052 | -#define AIO_BUF_LEN 256 | |
| 2053 | -#define AIO_WBUF_FULL_SIZE (64 * 1024) | |
| 2329 | +#define AIO_DEFAULT_RBUF_LEN 256 | |
| 2330 | +#define AIO_DEFAULT_WBUF_LIMIT (64 * 1024) | |
| 2054 | 2331 | |
| 2055 | 2332 | #define AIO_KEEPOPEN 1 |
| 2056 | 2333 | #define AIO_NODELETE 2 |
| 2057 | 2334 | #define AIO_EOF 4 |
| 2058 | 2335 | #define AIO_WBUF_NONE 8 |
| 2059 | 2336 | #define AIO_NONBLOCK 16 |
| 2337 | +#define AIO_NOTAINT 32 | |
| 2338 | + | |
| 2339 | +#define AIO_ONEREAD 32 | |
| 2060 | 2340 | |
| 2061 | 2341 | enum wbuftype { |
| 2062 | 2342 | WBUF_OPT_NONE, |
| 2063 | 2343 | WBUF_OPT_LINE, |
| 2064 | 2344 | WBUF_OPT_FULL, |
| @@ -2076,14 +2356,10 @@ | ||
| 2076 | 2356 | #define UNIX_SOCKETS 1 |
| 2077 | 2357 | #else |
| 2078 | 2358 | #define UNIX_SOCKETS 0 |
| 2079 | 2359 | #endif |
| 2080 | 2360 | |
| 2081 | -#ifndef MAXPATHLEN | |
| 2082 | -#define MAXPATHLEN JIM_PATH_LEN | |
| 2083 | -#endif | |
| 2084 | - | |
| 2085 | 2361 | |
| 2086 | 2362 | |
| 2087 | 2363 | |
| 2088 | 2364 | static int JimReadableTimeout(int fd, long ms) |
| 2089 | 2365 | { |
| @@ -2115,30 +2391,40 @@ | ||
| 2115 | 2391 | typedef struct { |
| 2116 | 2392 | int (*writer)(struct AioFile *af, const char *buf, int len); |
| 2117 | 2393 | int (*reader)(struct AioFile *af, char *buf, int len, int pending); |
| 2118 | 2394 | int (*error)(const struct AioFile *af); |
| 2119 | 2395 | const char *(*strerror)(struct AioFile *af); |
| 2120 | - int (*verify)(struct AioFile *af); | |
| 2121 | 2396 | } JimAioFopsType; |
| 2122 | 2397 | |
| 2123 | 2398 | typedef struct AioFile |
| 2124 | 2399 | { |
| 2125 | 2400 | Jim_Obj *filename; |
| 2126 | 2401 | int wbuft; |
| 2127 | 2402 | int flags; |
| 2128 | 2403 | long timeout; |
| 2129 | 2404 | int fd; |
| 2405 | + unsigned taintsource; | |
| 2406 | + unsigned taintsink; | |
| 2130 | 2407 | int addr_family; |
| 2131 | 2408 | void *ssl; |
| 2132 | 2409 | const JimAioFopsType *fops; |
| 2133 | 2410 | Jim_Obj *readbuf; |
| 2134 | 2411 | Jim_Obj *writebuf; |
| 2412 | + char *rbuf; | |
| 2413 | + size_t rbuf_len; | |
| 2414 | + size_t wbuf_limit; | |
| 2135 | 2415 | } AioFile; |
| 2416 | + | |
| 2417 | +static void aio_consume(Jim_Obj *objPtr, int n); | |
| 2136 | 2418 | |
| 2137 | 2419 | static int stdio_writer(struct AioFile *af, const char *buf, int len) |
| 2138 | 2420 | { |
| 2139 | - return write(af->fd, buf, len); | |
| 2421 | + int ret = write(af->fd, buf, len); | |
| 2422 | + if (ret < 0 && errno == EPIPE) { | |
| 2423 | + aio_consume(af->writebuf, Jim_Length(af->writebuf)); | |
| 2424 | + } | |
| 2425 | + return ret; | |
| 2140 | 2426 | } |
| 2141 | 2427 | |
| 2142 | 2428 | static int stdio_reader(struct AioFile *af, char *buf, int len, int nb) |
| 2143 | 2429 | { |
| 2144 | 2430 | if (nb || af->timeout == 0 || JimReadableTimeout(af->fd, af->timeout) == JIM_OK) { |
| @@ -2186,11 +2472,10 @@ | ||
| 2186 | 2472 | static const JimAioFopsType stdio_fops = { |
| 2187 | 2473 | stdio_writer, |
| 2188 | 2474 | stdio_reader, |
| 2189 | 2475 | stdio_error, |
| 2190 | 2476 | stdio_strerror, |
| 2191 | - NULL, | |
| 2192 | 2477 | }; |
| 2193 | 2478 | |
| 2194 | 2479 | |
| 2195 | 2480 | static void aio_set_nonblocking(AioFile *af, int nb) |
| 2196 | 2481 | { |
| @@ -2219,10 +2504,11 @@ | ||
| 2219 | 2504 | } |
| 2220 | 2505 | return old; |
| 2221 | 2506 | } |
| 2222 | 2507 | |
| 2223 | 2508 | static int JimAioSubCmdProc(Jim_Interp *interp, int argc, Jim_Obj *const *argv); |
| 2509 | +static void JimAioSetTaint(AioFile *af, int taintsource, int taintsink); | |
| 2224 | 2510 | static AioFile *JimMakeChannel(Jim_Interp *interp, int fd, Jim_Obj *filename, |
| 2225 | 2511 | const char *hdlfmt, int family, int flags); |
| 2226 | 2512 | |
| 2227 | 2513 | |
| 2228 | 2514 | static const char *JimAioErrorString(AioFile *af) |
| @@ -2271,11 +2557,26 @@ | ||
| 2271 | 2557 | memmove(objPtr->bytes, objPtr->bytes + n, objPtr->length - n + 1); |
| 2272 | 2558 | objPtr->length -= n; |
| 2273 | 2559 | } |
| 2274 | 2560 | |
| 2275 | 2561 | |
| 2276 | -static int aio_autoflush(Jim_Interp *interp, void *clientData, int mask); | |
| 2562 | +static int aio_flush(Jim_Interp *interp, AioFile *af); | |
| 2563 | + | |
| 2564 | +#ifdef jim_ext_eventloop | |
| 2565 | +static int aio_autoflush(Jim_Interp *interp, void *clientData, int mask) | |
| 2566 | +{ | |
| 2567 | + AioFile *af = clientData; | |
| 2568 | + | |
| 2569 | + aio_flush(interp, af); | |
| 2570 | + if (Jim_Length(af->writebuf) == 0) { | |
| 2571 | + | |
| 2572 | + return -1; | |
| 2573 | + } | |
| 2574 | + return 0; | |
| 2575 | +} | |
| 2576 | +#endif | |
| 2577 | + | |
| 2277 | 2578 | |
| 2278 | 2579 | static int aio_flush(Jim_Interp *interp, AioFile *af) |
| 2279 | 2580 | { |
| 2280 | 2581 | int len; |
| 2281 | 2582 | const char *pt = Jim_GetString(af->writebuf, &len); |
| @@ -2306,23 +2607,11 @@ | ||
| 2306 | 2607 | } |
| 2307 | 2608 | } |
| 2308 | 2609 | return JIM_OK; |
| 2309 | 2610 | } |
| 2310 | 2611 | |
| 2311 | -static int aio_autoflush(Jim_Interp *interp, void *clientData, int mask) | |
| 2312 | -{ | |
| 2313 | - AioFile *af = clientData; | |
| 2314 | - | |
| 2315 | - aio_flush(interp, af); | |
| 2316 | - if (Jim_Length(af->writebuf) == 0) { | |
| 2317 | - | |
| 2318 | - return -1; | |
| 2319 | - } | |
| 2320 | - return 0; | |
| 2321 | -} | |
| 2322 | - | |
| 2323 | -static int aio_read_len(Jim_Interp *interp, AioFile *af, int nb, char *buf, size_t buflen, int neededLen) | |
| 2612 | +static int aio_read_len(Jim_Interp *interp, AioFile *af, unsigned flags, int neededLen) | |
| 2324 | 2613 | { |
| 2325 | 2614 | if (!af->readbuf) { |
| 2326 | 2615 | af->readbuf = Jim_NewStringObj(interp, NULL, 0); |
| 2327 | 2616 | } |
| 2328 | 2617 | |
| @@ -2336,24 +2625,33 @@ | ||
| 2336 | 2625 | while (neededLen && !aio_eof(af)) { |
| 2337 | 2626 | int retval; |
| 2338 | 2627 | int readlen; |
| 2339 | 2628 | |
| 2340 | 2629 | if (neededLen == -1) { |
| 2341 | - readlen = AIO_BUF_LEN; | |
| 2630 | + readlen = af->rbuf_len; | |
| 2342 | 2631 | } |
| 2343 | 2632 | else { |
| 2344 | - readlen = (neededLen > AIO_BUF_LEN ? AIO_BUF_LEN : neededLen); | |
| 2633 | + readlen = (neededLen > af->rbuf_len ? af->rbuf_len : neededLen); | |
| 2345 | 2634 | } |
| 2346 | - retval = af->fops->reader(af, buf, readlen, nb); | |
| 2635 | + | |
| 2636 | + if (!af->rbuf) { | |
| 2637 | + af->rbuf = Jim_Alloc(af->rbuf_len); | |
| 2638 | + } | |
| 2639 | + retval = af->fops->reader(af, af->rbuf, readlen, flags & AIO_NONBLOCK); | |
| 2347 | 2640 | if (retval > 0) { |
| 2348 | - Jim_AppendString(interp, af->readbuf, buf, retval); | |
| 2641 | + if (retval) { | |
| 2642 | + Jim_AppendString(interp, af->readbuf, af->rbuf, retval); | |
| 2643 | + } | |
| 2349 | 2644 | if (neededLen != -1) { |
| 2350 | 2645 | neededLen -= retval; |
| 2351 | 2646 | } |
| 2647 | + if (flags & AIO_ONEREAD) { | |
| 2648 | + return JIM_OK; | |
| 2649 | + } | |
| 2352 | 2650 | continue; |
| 2353 | 2651 | } |
| 2354 | - if (JimCheckStreamError(interp, af)) { | |
| 2652 | + if ((flags & AIO_ONEREAD) || JimCheckStreamError(interp, af)) { | |
| 2355 | 2653 | return JIM_ERR; |
| 2356 | 2654 | } |
| 2357 | 2655 | break; |
| 2358 | 2656 | } |
| 2359 | 2657 | |
| @@ -2420,10 +2718,11 @@ | ||
| 2420 | 2718 | } |
| 2421 | 2719 | if (af->readbuf) { |
| 2422 | 2720 | Jim_FreeNewObj(interp, af->readbuf); |
| 2423 | 2721 | } |
| 2424 | 2722 | |
| 2723 | + Jim_Free(af->rbuf); | |
| 2425 | 2724 | Jim_Free(af); |
| 2426 | 2725 | } |
| 2427 | 2726 | |
| 2428 | 2727 | static int aio_cmd_read(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 2429 | 2728 | { |
| @@ -2433,11 +2732,10 @@ | ||
| 2433 | 2732 | static const char * const options[] = { "-pending", "-nonewline", NULL }; |
| 2434 | 2733 | enum { OPT_PENDING, OPT_NONEWLINE }; |
| 2435 | 2734 | int option; |
| 2436 | 2735 | int nb; |
| 2437 | 2736 | Jim_Obj *objPtr; |
| 2438 | - char buf[AIO_BUF_LEN]; | |
| 2439 | 2737 | |
| 2440 | 2738 | if (argc) { |
| 2441 | 2739 | if (*Jim_String(argv[0]) == '-') { |
| 2442 | 2740 | if (Jim_GetEnum(interp, argv[0], options, &option, NULL, JIM_ERRMSG) != JIM_OK) { |
| 2443 | 2741 | return JIM_ERR; |
| @@ -2461,21 +2759,22 @@ | ||
| 2461 | 2759 | } |
| 2462 | 2760 | argc--; |
| 2463 | 2761 | argv++; |
| 2464 | 2762 | } |
| 2465 | 2763 | if (argc) { |
| 2466 | - return -1; | |
| 2764 | + return JIM_USAGE; | |
| 2467 | 2765 | } |
| 2468 | 2766 | |
| 2469 | 2767 | |
| 2470 | 2768 | nb = aio_start_nonblocking(af); |
| 2471 | 2769 | |
| 2472 | - if (aio_read_len(interp, af, nb, buf, sizeof(buf), neededLen) != JIM_OK) { | |
| 2770 | + if (aio_read_len(interp, af, nb ? AIO_NONBLOCK : 0, neededLen) != JIM_OK) { | |
| 2473 | 2771 | aio_set_nonblocking(af, nb); |
| 2474 | 2772 | return JIM_ERR; |
| 2475 | 2773 | } |
| 2476 | 2774 | objPtr = aio_read_consume(interp, af, neededLen); |
| 2775 | + Jim_TaintObj(objPtr, af->taintsource); | |
| 2477 | 2776 | |
| 2478 | 2777 | aio_set_nonblocking(af, nb); |
| 2479 | 2778 | |
| 2480 | 2779 | if (objPtr) { |
| 2481 | 2780 | if (nonewline) { |
| @@ -2498,11 +2797,11 @@ | ||
| 2498 | 2797 | int Jim_AioFilehandle(Jim_Interp *interp, Jim_Obj *command) |
| 2499 | 2798 | { |
| 2500 | 2799 | Jim_Cmd *cmdPtr = Jim_GetCommand(interp, command, JIM_ERRMSG); |
| 2501 | 2800 | |
| 2502 | 2801 | |
| 2503 | - if (cmdPtr && !cmdPtr->isproc && cmdPtr->u.native.cmdProc == JimAioSubCmdProc) { | |
| 2802 | + if (cmdPtr && !(cmdPtr->flags & JIM_CMD_ISPROC) && cmdPtr->u.native.cmdProc == JimAioSubCmdProc) { | |
| 2504 | 2803 | return ((AioFile *) cmdPtr->u.native.privData)->fd; |
| 2505 | 2804 | } |
| 2506 | 2805 | Jim_SetResultFormatted(interp, "Not a filehandle: \"%#s\"", command); |
| 2507 | 2806 | return -1; |
| 2508 | 2807 | } |
| @@ -2516,29 +2815,62 @@ | ||
| 2516 | 2815 | |
| 2517 | 2816 | Jim_SetResultInt(interp, af->fd); |
| 2518 | 2817 | |
| 2519 | 2818 | return JIM_OK; |
| 2520 | 2819 | } |
| 2820 | + | |
| 2821 | +static int aio_cmd_gettaint(Jim_Interp *interp, int argc, Jim_Obj *const *argv) | |
| 2822 | +{ | |
| 2823 | + AioFile *af = Jim_CmdPrivData(interp); | |
| 2824 | + | |
| 2825 | + static const char * const options[] = { "-source", "-sink", NULL }; | |
| 2826 | + enum { OPT_SOURCE, OPT_SINK }; | |
| 2827 | + int option; | |
| 2828 | + | |
| 2829 | + if (Jim_GetEnum(interp, argv[0], options, &option, NULL, JIM_ERRMSG) != JIM_OK) { | |
| 2830 | + return JIM_ERR; | |
| 2831 | + } | |
| 2832 | + switch (option) { | |
| 2833 | + case OPT_SOURCE: | |
| 2834 | + Jim_SetResultInt(interp, af->taintsource); | |
| 2835 | + break; | |
| 2836 | + | |
| 2837 | + case OPT_SINK: | |
| 2838 | + Jim_SetResultInt(interp, af->taintsink); | |
| 2839 | + break; | |
| 2840 | + } | |
| 2841 | + | |
| 2842 | + return JIM_OK; | |
| 2843 | +} | |
| 2521 | 2844 | |
| 2522 | 2845 | static int aio_cmd_copy(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 2523 | 2846 | { |
| 2524 | 2847 | AioFile *af = Jim_CmdPrivData(interp); |
| 2525 | 2848 | jim_wide count = 0; |
| 2526 | 2849 | jim_wide maxlen = JIM_WIDE_MAX; |
| 2527 | - | |
| 2528 | - char buf[AIO_BUF_LEN]; | |
| 2529 | - | |
| 2530 | - char *bufp = buf; | |
| 2531 | - int buflen = sizeof(buf); | |
| 2532 | 2850 | int ok = 1; |
| 2533 | 2851 | Jim_Obj *objv[4]; |
| 2852 | + long taintsink; | |
| 2534 | 2853 | |
| 2535 | 2854 | if (argc == 2) { |
| 2536 | 2855 | if (Jim_GetWide(interp, argv[1], &maxlen) != JIM_OK) { |
| 2537 | 2856 | return JIM_ERR; |
| 2538 | 2857 | } |
| 2539 | 2858 | } |
| 2859 | + | |
| 2860 | + objv[0] = argv[0]; | |
| 2861 | + objv[1] = Jim_NewStringObj(interp, "gettaint", -1); | |
| 2862 | + objv[2] = Jim_NewStringObj(interp, "-sink", -1); | |
| 2863 | + if (Jim_EvalObjVector(interp, 3, objv) != JIM_OK || Jim_GetLong(interp, Jim_GetResult(interp), &taintsink) != JIM_OK) { | |
| 2864 | + Jim_SetResultFormatted(interp, "Not a filehandle: \"%#s\"", argv[0]); | |
| 2865 | + return JIM_ERR; | |
| 2866 | + } | |
| 2867 | + | |
| 2868 | + if (af->taintsource & taintsink) { | |
| 2869 | + Jim_SetResultString(interp, "copying tainted source", -1); | |
| 2870 | + return JIM_ERR; | |
| 2871 | + } | |
| 2540 | 2872 | |
| 2541 | 2873 | objv[0] = argv[0]; |
| 2542 | 2874 | objv[1] = Jim_NewStringObj(interp, "flush", -1); |
| 2543 | 2875 | if (Jim_EvalObjVector(interp, 2, objv) != JIM_OK) { |
| 2544 | 2876 | Jim_SetResultFormatted(interp, "Not a filehandle: \"%#s\"", argv[0]); |
| @@ -2552,14 +2884,14 @@ | ||
| 2552 | 2884 | Jim_IncrRefCount(objv[1]); |
| 2553 | 2885 | Jim_IncrRefCount(objv[2]); |
| 2554 | 2886 | |
| 2555 | 2887 | while (count < maxlen) { |
| 2556 | 2888 | jim_wide len = maxlen - count; |
| 2557 | - if (len > buflen) { | |
| 2558 | - len = buflen; | |
| 2889 | + if (len > af->rbuf_len) { | |
| 2890 | + len = af->rbuf_len; | |
| 2559 | 2891 | } |
| 2560 | - if (aio_read_len(interp, af, 0, bufp, buflen, len) != JIM_OK) { | |
| 2892 | + if (aio_read_len(interp, af, 0, len) != JIM_OK) { | |
| 2561 | 2893 | ok = 0; |
| 2562 | 2894 | break; |
| 2563 | 2895 | } |
| 2564 | 2896 | objv[3] = aio_read_consume(interp, af, len); |
| 2565 | 2897 | count += Jim_Length(objv[3]); |
| @@ -2568,19 +2900,15 @@ | ||
| 2568 | 2900 | break; |
| 2569 | 2901 | } |
| 2570 | 2902 | if (aio_eof(af)) { |
| 2571 | 2903 | break; |
| 2572 | 2904 | } |
| 2573 | - if (count >= 16384 && bufp == buf) { | |
| 2574 | - | |
| 2575 | - buflen = 65536; | |
| 2576 | - bufp = Jim_Alloc(buflen); | |
| 2577 | - } | |
| 2578 | - } | |
| 2579 | - | |
| 2580 | - if (bufp != buf) { | |
| 2581 | - Jim_Free(bufp); | |
| 2905 | + if (count >= 16384 && af->rbuf_len < 65536) { | |
| 2906 | + | |
| 2907 | + af->rbuf_len = 65536; | |
| 2908 | + af->rbuf = Jim_Realloc(af->rbuf, af->rbuf_len); | |
| 2909 | + } | |
| 2582 | 2910 | } |
| 2583 | 2911 | |
| 2584 | 2912 | Jim_DecrRefCount(interp, objv[1]); |
| 2585 | 2913 | Jim_DecrRefCount(interp, objv[2]); |
| 2586 | 2914 | |
| @@ -2591,62 +2919,107 @@ | ||
| 2591 | 2919 | Jim_SetResultInt(interp, count); |
| 2592 | 2920 | |
| 2593 | 2921 | return JIM_OK; |
| 2594 | 2922 | } |
| 2595 | 2923 | |
| 2924 | + | |
| 2925 | +static const char *jim_strstr(const char *haystack, int haylen, const char *needle, int needlen) | |
| 2926 | +{ | |
| 2927 | + if (needlen == 1) { | |
| 2928 | + return (const char *)memchr(haystack, needle[0], haylen); | |
| 2929 | + } | |
| 2930 | + return strstr(haystack, needle); | |
| 2931 | +} | |
| 2932 | + | |
| 2596 | 2933 | static int aio_cmd_gets(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 2597 | 2934 | { |
| 2598 | 2935 | AioFile *af = Jim_CmdPrivData(interp); |
| 2599 | - char buf[AIO_BUF_LEN]; | |
| 2600 | 2936 | Jim_Obj *objPtr = NULL; |
| 2601 | 2937 | int len; |
| 2602 | 2938 | int nb; |
| 2603 | - char *nl = NULL; | |
| 2939 | + unsigned flags = AIO_ONEREAD; | |
| 2940 | + const char *nl = NULL; | |
| 2604 | 2941 | int offset = 0; |
| 2942 | + long keepnl = 0; | |
| 2943 | + const char *nlstr = "\n"; | |
| 2944 | + int nlstrlen = 1; | |
| 2945 | + | |
| 2946 | + while (argc >= 2) { | |
| 2947 | + enum {OPT_EOL, OPT_KEEP}; | |
| 2948 | + static const char * const options[] = { | |
| 2949 | + "-eol", | |
| 2950 | + "-keep", | |
| 2951 | + NULL | |
| 2952 | + }; | |
| 2953 | + int opt; | |
| 2954 | + | |
| 2955 | + | |
| 2956 | + if (*Jim_String(argv[0]) != '-') { | |
| 2957 | + return JIM_USAGE; | |
| 2958 | + } | |
| 2959 | + | |
| 2960 | + if (Jim_GetEnum(interp, argv[0], options, &opt, NULL, JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK) { | |
| 2961 | + return JIM_ERR; | |
| 2962 | + } | |
| 2963 | + | |
| 2964 | + switch (opt) { | |
| 2965 | + case OPT_EOL: | |
| 2966 | + nlstr = Jim_GetString(argv[1], &nlstrlen); | |
| 2967 | + break; | |
| 2968 | + | |
| 2969 | + case OPT_KEEP: | |
| 2970 | + if (Jim_GetLong(interp, argv[1], &keepnl) != JIM_OK) { | |
| 2971 | + return JIM_ERR; | |
| 2972 | + } | |
| 2973 | + break; | |
| 2974 | + } | |
| 2975 | + argc -= 2; | |
| 2976 | + argv += 2; | |
| 2977 | + } | |
| 2605 | 2978 | |
| 2606 | 2979 | errno = 0; |
| 2607 | 2980 | |
| 2608 | 2981 | |
| 2609 | 2982 | nb = aio_start_nonblocking(af); |
| 2610 | - | |
| 2611 | - if (!af->readbuf) { | |
| 2612 | - af->readbuf = Jim_NewStringObj(interp, NULL, 0); | |
| 2983 | + if (nb) { | |
| 2984 | + flags |= AIO_NONBLOCK; | |
| 2613 | 2985 | } |
| 2614 | 2986 | |
| 2615 | 2987 | while (!aio_eof(af)) { |
| 2616 | - const char *pt = Jim_GetString(af->readbuf, &len); | |
| 2617 | - nl = memchr(pt + offset, '\n', len - offset); | |
| 2618 | - if (nl) { | |
| 2619 | - | |
| 2620 | - objPtr = Jim_NewStringObj(interp, pt, nl - pt); | |
| 2621 | - | |
| 2622 | - aio_consume(af->readbuf, nl - pt + 1); | |
| 2623 | - break; | |
| 2624 | - } | |
| 2625 | - | |
| 2626 | - offset = len; | |
| 2627 | - len = af->fops->reader(af, buf, AIO_BUF_LEN, nb); | |
| 2628 | - if (len <= 0) { | |
| 2629 | - break; | |
| 2630 | - } | |
| 2631 | - Jim_AppendString(interp, af->readbuf, buf, len); | |
| 2988 | + if (af->readbuf) { | |
| 2989 | + const char *pt = Jim_GetString(af->readbuf, &len); | |
| 2990 | + nl = jim_strstr(pt + offset, len - offset, nlstr, nlstrlen); | |
| 2991 | + if (nl) { | |
| 2992 | + | |
| 2993 | + objPtr = Jim_NewStringObj(interp, pt, nl - pt + (keepnl ? nlstrlen : 0)); | |
| 2994 | + | |
| 2995 | + aio_consume(af->readbuf, nl - pt + nlstrlen); | |
| 2996 | + break; | |
| 2997 | + } | |
| 2998 | + offset = len; | |
| 2999 | + } | |
| 3000 | + | |
| 3001 | + | |
| 3002 | + if (aio_read_len(interp, af, flags, -1) != JIM_OK) { | |
| 3003 | + break; | |
| 3004 | + } | |
| 2632 | 3005 | } |
| 2633 | 3006 | |
| 2634 | 3007 | aio_set_nonblocking(af, nb); |
| 2635 | 3008 | |
| 2636 | - if (!nl && aio_eof(af)) { | |
| 3009 | + if (!nl && aio_eof(af) && af->readbuf) { | |
| 2637 | 3010 | |
| 2638 | 3011 | objPtr = af->readbuf; |
| 2639 | 3012 | af->readbuf = NULL; |
| 2640 | 3013 | } |
| 2641 | 3014 | else if (!objPtr) { |
| 2642 | 3015 | objPtr = Jim_NewStringObj(interp, NULL, 0); |
| 2643 | 3016 | } |
| 3017 | + Jim_TaintObj(objPtr, af->taintsource); | |
| 2644 | 3018 | |
| 2645 | 3019 | if (argc) { |
| 2646 | 3020 | if (Jim_SetVariable(interp, argv[0], objPtr) != JIM_OK) { |
| 2647 | - Jim_FreeNewObj(interp, objPtr); | |
| 2648 | 3021 | return JIM_ERR; |
| 2649 | 3022 | } |
| 2650 | 3023 | |
| 2651 | 3024 | len = Jim_Length(objPtr); |
| 2652 | 3025 | |
| @@ -2668,22 +3041,34 @@ | ||
| 2668 | 3041 | int wlen; |
| 2669 | 3042 | const char *wdata; |
| 2670 | 3043 | Jim_Obj *strObj; |
| 2671 | 3044 | int wnow = 0; |
| 2672 | 3045 | int nl = 1; |
| 3046 | + | |
| 3047 | + if (Jim_CheckTaint(interp, af->taintsink)) { | |
| 3048 | + Jim_SetResultString(interp, "puts: tainted data", -1); | |
| 3049 | + return JIM_ERR; | |
| 3050 | + } | |
| 2673 | 3051 | |
| 2674 | 3052 | if (argc == 2) { |
| 2675 | 3053 | if (!Jim_CompareStringImmediate(interp, argv[0], "-nonewline")) { |
| 2676 | - return -1; | |
| 3054 | + return JIM_USAGE; | |
| 2677 | 3055 | } |
| 2678 | 3056 | strObj = argv[1]; |
| 2679 | 3057 | nl = 0; |
| 2680 | 3058 | } |
| 2681 | 3059 | else { |
| 2682 | 3060 | strObj = argv[0]; |
| 2683 | 3061 | } |
| 2684 | 3062 | |
| 3063 | +#ifdef JIM_MAINTAINER | |
| 3064 | + if (Jim_IsShared(af->writebuf)) { | |
| 3065 | + Jim_DecrRefCount(interp, af->writebuf); | |
| 3066 | + af->writebuf = Jim_DuplicateObj(interp, af->writebuf); | |
| 3067 | + Jim_IncrRefCount(af->writebuf); | |
| 3068 | + } | |
| 3069 | +#endif | |
| 2685 | 3070 | Jim_AppendObj(interp, af->writebuf, strObj); |
| 2686 | 3071 | if (nl) { |
| 2687 | 3072 | Jim_AppendString(interp, af->writebuf, "\n", 1); |
| 2688 | 3073 | } |
| 2689 | 3074 | |
| @@ -2701,11 +3086,11 @@ | ||
| 2701 | 3086 | wnow = 1; |
| 2702 | 3087 | } |
| 2703 | 3088 | break; |
| 2704 | 3089 | |
| 2705 | 3090 | case WBUF_OPT_FULL: |
| 2706 | - if (wlen >= AIO_WBUF_FULL_SIZE) { | |
| 3091 | + if (wlen >= af->wbuf_limit) { | |
| 2707 | 3092 | wnow = 1; |
| 2708 | 3093 | } |
| 2709 | 3094 | break; |
| 2710 | 3095 | } |
| 2711 | 3096 | |
| @@ -2797,11 +3182,11 @@ | ||
| 2797 | 3182 | else if (Jim_CompareStringImmediate(interp, argv[1], "current")) |
| 2798 | 3183 | orig = SEEK_CUR; |
| 2799 | 3184 | else if (Jim_CompareStringImmediate(interp, argv[1], "end")) |
| 2800 | 3185 | orig = SEEK_END; |
| 2801 | 3186 | else { |
| 2802 | - return -1; | |
| 3187 | + return JIM_USAGE; | |
| 2803 | 3188 | } |
| 2804 | 3189 | } |
| 2805 | 3190 | if (Jim_GetWide(interp, argv[0], &offset) != JIM_OK) { |
| 2806 | 3191 | return JIM_ERR; |
| 2807 | 3192 | } |
| @@ -2834,10 +3219,49 @@ | ||
| 2834 | 3219 | AioFile *af = Jim_CmdPrivData(interp); |
| 2835 | 3220 | |
| 2836 | 3221 | Jim_SetResult(interp, af->filename); |
| 2837 | 3222 | return JIM_OK; |
| 2838 | 3223 | } |
| 3224 | + | |
| 3225 | +#ifdef JIM_TAINT | |
| 3226 | +static int aio_cmd_taint(Jim_Interp *interp, int argc, Jim_Obj *const *argv) | |
| 3227 | +{ | |
| 3228 | + AioFile *af = Jim_CmdPrivData(interp); | |
| 3229 | + static const char * const types[] = { | |
| 3230 | + "sink", | |
| 3231 | + "source", | |
| 3232 | + NULL | |
| 3233 | + }; | |
| 3234 | + enum | |
| 3235 | + { | |
| 3236 | + TAINT_TYPE_SINK, | |
| 3237 | + TAINT_TYPE_SOURCE, | |
| 3238 | + }; | |
| 3239 | + int type; | |
| 3240 | + long taint; | |
| 3241 | + | |
| 3242 | + if (Jim_GetEnum(interp, argv[0], types, &type, NULL, JIM_ERRMSG) != JIM_OK) | |
| 3243 | + return JIM_ERR; | |
| 3244 | + | |
| 3245 | + if (argc == 1) { | |
| 3246 | + Jim_SetResultInt(interp, type == TAINT_TYPE_SINK ? af->taintsink : af->taintsource); | |
| 3247 | + return JIM_OK; | |
| 3248 | + } | |
| 3249 | + else if (Jim_GetLong(interp, argv[1], &taint) == JIM_OK) { | |
| 3250 | + if (type == TAINT_TYPE_SINK) { | |
| 3251 | + af->taintsink = taint; | |
| 3252 | + } | |
| 3253 | + else { | |
| 3254 | + af->taintsource = taint; | |
| 3255 | + } | |
| 3256 | + return JIM_OK; | |
| 3257 | + } | |
| 3258 | + else { | |
| 3259 | + return JIM_ERR; | |
| 3260 | + } | |
| 3261 | +} | |
| 3262 | +#endif | |
| 2839 | 3263 | |
| 2840 | 3264 | #ifdef O_NDELAY |
| 2841 | 3265 | static int aio_cmd_ndelay(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 2842 | 3266 | { |
| 2843 | 3267 | AioFile *af = Jim_CmdPrivData(interp); |
| @@ -2870,29 +3294,92 @@ | ||
| 2870 | 3294 | #endif |
| 2871 | 3295 | |
| 2872 | 3296 | static int aio_cmd_buffering(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 2873 | 3297 | { |
| 2874 | 3298 | AioFile *af = Jim_CmdPrivData(interp); |
| 3299 | + Jim_Obj *resultObj; | |
| 2875 | 3300 | |
| 2876 | 3301 | static const char * const options[] = { |
| 2877 | 3302 | "none", |
| 2878 | 3303 | "line", |
| 2879 | 3304 | "full", |
| 2880 | 3305 | NULL |
| 2881 | 3306 | }; |
| 2882 | 3307 | |
| 2883 | - if (Jim_GetEnum(interp, argv[0], options, &af->wbuft, NULL, JIM_ERRMSG) != JIM_OK) { | |
| 2884 | - return JIM_ERR; | |
| 3308 | + if (argc) { | |
| 3309 | + if (Jim_GetEnum(interp, argv[0], options, &af->wbuft, NULL, JIM_ERRMSG) != JIM_OK) { | |
| 3310 | + return JIM_ERR; | |
| 3311 | + } | |
| 3312 | + | |
| 3313 | + if (af->wbuft == WBUF_OPT_FULL && argc == 2) { | |
| 3314 | + long l; | |
| 3315 | + if (Jim_GetLong(interp, argv[1], &l) != JIM_OK || l <= 0) { | |
| 3316 | + return JIM_ERR; | |
| 3317 | + } | |
| 3318 | + af->wbuf_limit = l; | |
| 3319 | + } | |
| 3320 | + | |
| 3321 | + if (af->wbuft == WBUF_OPT_NONE) { | |
| 3322 | + if (aio_flush(interp, af) != JIM_OK) { | |
| 3323 | + return JIM_ERR; | |
| 3324 | + } | |
| 3325 | + } | |
| 3326 | + | |
| 2885 | 3327 | } |
| 2886 | 3328 | |
| 2887 | - if (af->wbuft == WBUF_OPT_NONE) { | |
| 2888 | - return aio_flush(interp, af); | |
| 3329 | + resultObj = Jim_NewListObj(interp, NULL, 0); | |
| 3330 | + Jim_ListAppendElement(interp, resultObj, Jim_NewStringObj(interp, options[af->wbuft], -1)); | |
| 3331 | + if (af->wbuft == WBUF_OPT_FULL) { | |
| 3332 | + Jim_ListAppendElement(interp, resultObj, Jim_NewIntObj(interp, af->wbuf_limit)); | |
| 2889 | 3333 | } |
| 3334 | + Jim_SetResult(interp, resultObj); | |
| 2890 | 3335 | |
| 2891 | 3336 | return JIM_OK; |
| 2892 | 3337 | } |
| 2893 | 3338 | |
| 3339 | +static int aio_cmd_translation(Jim_Interp *interp, int argc, Jim_Obj *const *argv) | |
| 3340 | +{ | |
| 3341 | + enum {OPT_BINARY, OPT_TEXT}; | |
| 3342 | + static const char * const options[] = { | |
| 3343 | + "binary", | |
| 3344 | + "text", | |
| 3345 | + NULL | |
| 3346 | + }; | |
| 3347 | + int opt; | |
| 3348 | + | |
| 3349 | + if (Jim_GetEnum(interp, argv[0], options, &opt, NULL, JIM_ERRMSG) != JIM_OK) { | |
| 3350 | + return JIM_ERR; | |
| 3351 | + } | |
| 3352 | +#if defined(Jim_SetMode) | |
| 3353 | + else { | |
| 3354 | + AioFile *af = Jim_CmdPrivData(interp); | |
| 3355 | + Jim_SetMode(af->fd, opt == OPT_BINARY ? O_BINARY : O_TEXT); | |
| 3356 | + } | |
| 3357 | +#endif | |
| 3358 | + return JIM_OK; | |
| 3359 | +} | |
| 3360 | + | |
| 3361 | +static int aio_cmd_readsize(Jim_Interp *interp, int argc, Jim_Obj *const *argv) | |
| 3362 | +{ | |
| 3363 | + AioFile *af = Jim_CmdPrivData(interp); | |
| 3364 | + | |
| 3365 | + if (argc) { | |
| 3366 | + long l; | |
| 3367 | + if (Jim_GetLong(interp, argv[0], &l) != JIM_OK || l <= 0) { | |
| 3368 | + return JIM_ERR; | |
| 3369 | + } | |
| 3370 | + af->rbuf_len = l; | |
| 3371 | + if (af->rbuf) { | |
| 3372 | + af->rbuf = Jim_Realloc(af->rbuf, af->rbuf_len); | |
| 3373 | + } | |
| 3374 | + } | |
| 3375 | + Jim_SetResultInt(interp, af->rbuf_len); | |
| 3376 | + | |
| 3377 | + return JIM_OK; | |
| 3378 | +} | |
| 3379 | + | |
| 3380 | +#ifdef jim_ext_eventloop | |
| 2894 | 3381 | static int aio_cmd_timeout(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 2895 | 3382 | { |
| 2896 | 3383 | #ifdef HAVE_SELECT |
| 2897 | 3384 | AioFile *af = Jim_CmdPrivData(interp); |
| 2898 | 3385 | if (argc == 1) { |
| @@ -2906,11 +3393,10 @@ | ||
| 2906 | 3393 | Jim_SetResultString(interp, "timeout not supported", -1); |
| 2907 | 3394 | return JIM_ERR; |
| 2908 | 3395 | #endif |
| 2909 | 3396 | } |
| 2910 | 3397 | |
| 2911 | -#ifdef jim_ext_eventloop | |
| 2912 | 3398 | static int aio_eventinfo(Jim_Interp *interp, AioFile * af, unsigned mask, |
| 2913 | 3399 | int argc, Jim_Obj * const *argv) |
| 2914 | 3400 | { |
| 2915 | 3401 | if (argc == 0) { |
| 2916 | 3402 | |
| @@ -2989,17 +3475,26 @@ | ||
| 2989 | 3475 | { "getfd", |
| 2990 | 3476 | NULL, |
| 2991 | 3477 | aio_cmd_getfd, |
| 2992 | 3478 | 0, |
| 2993 | 3479 | 0, |
| 3480 | + JIM_MODFLAG_HIDDEN, | |
| 3481 | + | |
| 3482 | + }, | |
| 3483 | + { "gettaint", | |
| 3484 | + "?-source|-sink?", | |
| 3485 | + aio_cmd_gettaint, | |
| 3486 | + 1, | |
| 3487 | + 1, | |
| 3488 | + JIM_MODFLAG_HIDDEN, | |
| 2994 | 3489 | |
| 2995 | 3490 | }, |
| 2996 | 3491 | { "gets", |
| 2997 | - "?var?", | |
| 3492 | + "?-eol <str>? ?-keep 0|1? ?var?", | |
| 2998 | 3493 | aio_cmd_gets, |
| 2999 | 3494 | 0, |
| 3000 | - 1, | |
| 3495 | + -1, | |
| 3001 | 3496 | |
| 3002 | 3497 | }, |
| 3003 | 3498 | { "puts", |
| 3004 | 3499 | "?-nonewline? str", |
| 3005 | 3500 | aio_cmd_puts, |
| @@ -3055,10 +3550,19 @@ | ||
| 3055 | 3550 | aio_cmd_filename, |
| 3056 | 3551 | 0, |
| 3057 | 3552 | 0, |
| 3058 | 3553 | |
| 3059 | 3554 | }, |
| 3555 | +#ifdef JIM_TAINT | |
| 3556 | + { "taint", | |
| 3557 | + "source|sink ?0|n?", | |
| 3558 | + aio_cmd_taint, | |
| 3559 | + 1, | |
| 3560 | + 2, | |
| 3561 | + | |
| 3562 | + }, | |
| 3563 | +#endif | |
| 3060 | 3564 | #ifdef O_NDELAY |
| 3061 | 3565 | { "ndelay", |
| 3062 | 3566 | "?0|1?", |
| 3063 | 3567 | aio_cmd_ndelay, |
| 3064 | 3568 | 0, |
| @@ -3074,13 +3578,27 @@ | ||
| 3074 | 3578 | 0, |
| 3075 | 3579 | |
| 3076 | 3580 | }, |
| 3077 | 3581 | #endif |
| 3078 | 3582 | { "buffering", |
| 3079 | - "none|line|full", | |
| 3583 | + "?none|line|full? ?size?", | |
| 3080 | 3584 | aio_cmd_buffering, |
| 3585 | + 0, | |
| 3586 | + 2, | |
| 3587 | + | |
| 3588 | + }, | |
| 3589 | + { "translation", | |
| 3590 | + "binary|text", | |
| 3591 | + aio_cmd_translation, | |
| 3592 | + 1, | |
| 3081 | 3593 | 1, |
| 3594 | + | |
| 3595 | + }, | |
| 3596 | + { "readsize", | |
| 3597 | + "?size?", | |
| 3598 | + aio_cmd_readsize, | |
| 3599 | + 0, | |
| 3082 | 3600 | 1, |
| 3083 | 3601 | |
| 3084 | 3602 | }, |
| 3085 | 3603 | #if defined(jim_ext_file) && defined(Jim_FileStat) |
| 3086 | 3604 | { "stat", |
| @@ -3222,23 +3740,29 @@ | ||
| 3222 | 3740 | |
| 3223 | 3741 | static int JimAioOpenCommand(Jim_Interp *interp, int argc, |
| 3224 | 3742 | Jim_Obj *const *argv) |
| 3225 | 3743 | { |
| 3226 | 3744 | int openflags; |
| 3745 | + AioFile *af; | |
| 3227 | 3746 | const char *filename; |
| 3228 | 3747 | int fd = -1; |
| 3229 | 3748 | int n = 0; |
| 3230 | - int flags = 0; | |
| 3749 | + | |
| 3750 | + int flags = AIO_NOTAINT; | |
| 3231 | 3751 | |
| 3232 | 3752 | if (argc > 2 && Jim_CompareStringImmediate(interp, argv[2], "-noclose")) { |
| 3233 | 3753 | flags = AIO_KEEPOPEN; |
| 3234 | 3754 | n++; |
| 3235 | 3755 | } |
| 3236 | - if (argc < 2 || argc > 3 + n) { | |
| 3237 | - Jim_WrongNumArgs(interp, 1, argv, "filename ?-noclose? ?mode?"); | |
| 3756 | + | |
| 3757 | + if (Jim_CheckTaint(interp, JIM_TAINT_ANY)) { | |
| 3758 | + Jim_SetTaintError(interp, 1, argv); | |
| 3238 | 3759 | return JIM_ERR; |
| 3239 | 3760 | } |
| 3761 | + if (argc > 3 + n) { | |
| 3762 | + return JIM_USAGE; | |
| 3763 | + } | |
| 3240 | 3764 | |
| 3241 | 3765 | filename = Jim_String(argv[1]); |
| 3242 | 3766 | |
| 3243 | 3767 | #ifdef jim_ext_tclcompat |
| 3244 | 3768 | { |
| @@ -3265,19 +3789,32 @@ | ||
| 3265 | 3789 | } |
| 3266 | 3790 | } |
| 3267 | 3791 | else { |
| 3268 | 3792 | openflags = O_RDONLY; |
| 3269 | 3793 | } |
| 3794 | + | |
| 3270 | 3795 | fd = open(filename, openflags, 0666); |
| 3271 | 3796 | if (fd < 0) { |
| 3272 | 3797 | JimAioSetError(interp, argv[1]); |
| 3273 | 3798 | return JIM_ERR; |
| 3274 | 3799 | } |
| 3275 | 3800 | |
| 3276 | - return JimMakeChannel(interp, fd, argv[1], "aio.handle%ld", 0, flags) ? JIM_OK : JIM_ERR; | |
| 3801 | + af = JimMakeChannel(interp, fd, argv[1], "aio.handle%ld", 0, flags); | |
| 3802 | + if (af) { | |
| 3803 | + | |
| 3804 | + JimAioSetTaint(af, 0, 0); | |
| 3805 | + return JIM_OK; | |
| 3806 | + } | |
| 3807 | + return JIM_ERR; | |
| 3277 | 3808 | } |
| 3278 | 3809 | |
| 3810 | + | |
| 3811 | +static void JimAioSetTaint(AioFile *af, int taintsource, int taintsink) | |
| 3812 | +{ | |
| 3813 | + af->taintsource = taintsource; | |
| 3814 | + af->taintsink = taintsink; | |
| 3815 | +} | |
| 3279 | 3816 | |
| 3280 | 3817 | static AioFile *JimMakeChannel(Jim_Interp *interp, int fd, Jim_Obj *filename, |
| 3281 | 3818 | const char *hdlfmt, int family, int flags) |
| 3282 | 3819 | { |
| 3283 | 3820 | AioFile *af; |
| @@ -3319,12 +3856,23 @@ | ||
| 3319 | 3856 | |
| 3320 | 3857 | af->flags |= flags; |
| 3321 | 3858 | |
| 3322 | 3859 | af->writebuf = Jim_NewStringObj(interp, NULL, 0); |
| 3323 | 3860 | Jim_IncrRefCount(af->writebuf); |
| 3861 | + af->wbuf_limit = AIO_DEFAULT_WBUF_LIMIT; | |
| 3862 | + af->rbuf_len = AIO_DEFAULT_RBUF_LEN; | |
| 3324 | 3863 | |
| 3325 | - Jim_CreateCommand(interp, buf, JimAioSubCmdProc, af, JimAioDelProc); | |
| 3864 | + | |
| 3865 | + | |
| 3866 | + if (!(flags & AIO_NOTAINT)) { | |
| 3867 | + JimAioSetTaint(af, JIM_TAINT_STD, JIM_TAINT_STD); | |
| 3868 | + } | |
| 3869 | + Jim_RegisterCommand(interp, cmdname, | |
| 3870 | + JimAioSubCmdProc, JimAioDelProc, | |
| 3871 | + NULL, | |
| 3872 | + NULL, | |
| 3873 | + 0, -1, JIM_CMD_ISCHANNEL, af); | |
| 3326 | 3874 | |
| 3327 | 3875 | Jim_SetResult(interp, Jim_MakeGlobalNamespaceName(interp, cmdname)); |
| 3328 | 3876 | |
| 3329 | 3877 | return af; |
| 3330 | 3878 | } |
| @@ -3365,14 +3913,10 @@ | ||
| 3365 | 3913 | } |
| 3366 | 3914 | |
| 3367 | 3915 | |
| 3368 | 3916 | static int JimAioPipeCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 3369 | 3917 | { |
| 3370 | - if (argc != 1) { | |
| 3371 | - Jim_WrongNumArgs(interp, 1, argv, ""); | |
| 3372 | - return JIM_ERR; | |
| 3373 | - } | |
| 3374 | 3918 | return JimCreatePipe(interp, argv[0], 0); |
| 3375 | 3919 | } |
| 3376 | 3920 | #endif |
| 3377 | 3921 | |
| 3378 | 3922 | #ifdef HAVE_OPENPTY |
| @@ -3379,15 +3923,10 @@ | ||
| 3379 | 3923 | static int JimAioOpenPtyCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 3380 | 3924 | { |
| 3381 | 3925 | int p[2]; |
| 3382 | 3926 | char path[MAXPATHLEN]; |
| 3383 | 3927 | |
| 3384 | - if (argc != 1) { | |
| 3385 | - Jim_WrongNumArgs(interp, 1, argv, ""); | |
| 3386 | - return JIM_ERR; | |
| 3387 | - } | |
| 3388 | - | |
| 3389 | 3928 | if (openpty(&p[0], &p[1], path, NULL, NULL) != 0) { |
| 3390 | 3929 | JimAioSetError(interp, NULL); |
| 3391 | 3930 | return JIM_ERR; |
| 3392 | 3931 | } |
| 3393 | 3932 | |
| @@ -3397,31 +3936,37 @@ | ||
| 3397 | 3936 | } |
| 3398 | 3937 | #endif |
| 3399 | 3938 | |
| 3400 | 3939 | |
| 3401 | 3940 | |
| 3941 | + | |
| 3942 | +static void JimMakeStdioChannel(Jim_Interp *interp, FILE *fh, const char *name, unsigned flags) | |
| 3943 | +{ | |
| 3944 | + | |
| 3945 | + AioFile *af = JimMakeChannel(interp, fileno(fh), NULL, name, 0, AIO_KEEPOPEN | flags); | |
| 3946 | + JimAioSetTaint(af, 0, 0); | |
| 3947 | +} | |
| 3948 | + | |
| 3402 | 3949 | int Jim_aioInit(Jim_Interp *interp) |
| 3403 | 3950 | { |
| 3404 | - if (Jim_PackageProvide(interp, "aio", "1.0", JIM_ERRMSG)) | |
| 3405 | - return JIM_ERR; | |
| 3406 | - | |
| 3951 | + Jim_PackageProvideCheck(interp, "aio"); | |
| 3407 | 3952 | #if defined(JIM_SSL) |
| 3408 | - Jim_CreateCommand(interp, "load_ssl_certs", JimAioLoadSSLCertsCommand, NULL, NULL); | |
| 3953 | + Jim_RegisterSimpleCmd(interp, "load_ssl_certs", "dir", 1, 1, JimAioLoadSSLCertsCommand); | |
| 3409 | 3954 | #endif |
| 3410 | 3955 | |
| 3411 | - Jim_CreateCommand(interp, "open", JimAioOpenCommand, NULL, NULL); | |
| 3956 | + Jim_RegisterCmd(interp, "open", "filename ?-noclose? ?mode?", 1, 3, JimAioOpenCommand, NULL, NULL, JIM_CMD_NOTAINT); | |
| 3412 | 3957 | #ifdef HAVE_SOCKETS |
| 3413 | - Jim_CreateCommand(interp, "socket", JimAioSockCommand, NULL, NULL); | |
| 3958 | + Jim_RegisterCmd(interp, "socket", "?-async? ?-ipv6? socktype ?address?", 1, 4, JimAioSockCommand, NULL, NULL, JIM_CMD_NOTAINT); | |
| 3414 | 3959 | #endif |
| 3415 | 3960 | #ifdef HAVE_PIPE |
| 3416 | - Jim_CreateCommand(interp, "pipe", JimAioPipeCommand, NULL, NULL); | |
| 3961 | + Jim_RegisterSimpleCmd(interp, "pipe", "", 0, 0, JimAioPipeCommand); | |
| 3417 | 3962 | #endif |
| 3418 | 3963 | |
| 3419 | 3964 | |
| 3420 | - JimMakeChannel(interp, fileno(stdin), NULL, "stdin", 0, AIO_KEEPOPEN); | |
| 3421 | - JimMakeChannel(interp, fileno(stdout), NULL, "stdout", 0, AIO_KEEPOPEN); | |
| 3422 | - JimMakeChannel(interp, fileno(stderr), NULL, "stderr", 0, AIO_KEEPOPEN | AIO_WBUF_NONE); | |
| 3965 | + JimMakeStdioChannel(interp, stdin, "stdin", 0); | |
| 3966 | + JimMakeStdioChannel(interp, stdout, "stdout", 0); | |
| 3967 | + JimMakeStdioChannel(interp, stderr, "stderr", AIO_WBUF_NONE); | |
| 3423 | 3968 | |
| 3424 | 3969 | return JIM_OK; |
| 3425 | 3970 | } |
| 3426 | 3971 | |
| 3427 | 3972 | #include <errno.h> |
| @@ -3442,12 +3987,11 @@ | ||
| 3442 | 3987 | |
| 3443 | 3988 | if (argc == 3 && Jim_CompareStringImmediate(interp, argv[1], "-nocomplain")) { |
| 3444 | 3989 | nocomplain = 1; |
| 3445 | 3990 | } |
| 3446 | 3991 | if (argc != 2 && !nocomplain) { |
| 3447 | - Jim_WrongNumArgs(interp, 1, argv, "?-nocomplain? dirPath"); | |
| 3448 | - return JIM_ERR; | |
| 3992 | + return JIM_USAGE; | |
| 3449 | 3993 | } |
| 3450 | 3994 | |
| 3451 | 3995 | dirPath = Jim_String(argv[1 + nocomplain]); |
| 3452 | 3996 | |
| 3453 | 3997 | dirPtr = opendir(dirPath); |
| @@ -3480,11 +4024,11 @@ | ||
| 3480 | 4024 | } |
| 3481 | 4025 | |
| 3482 | 4026 | int Jim_readdirInit(Jim_Interp *interp) |
| 3483 | 4027 | { |
| 3484 | 4028 | Jim_PackageProvideCheck(interp, "readdir"); |
| 3485 | - Jim_CreateCommand(interp, "readdir", Jim_ReaddirCmd, NULL, NULL); | |
| 4029 | + Jim_RegisterSimpleCmd(interp, "readdir", "?-nocomplain? dirPath", 1, 2, Jim_ReaddirCmd); | |
| 3486 | 4030 | return JIM_OK; |
| 3487 | 4031 | } |
| 3488 | 4032 | |
| 3489 | 4033 | #include <stdlib.h> |
| 3490 | 4034 | #include <string.h> |
| @@ -3553,10 +4097,11 @@ | ||
| 3553 | 4097 | int Jim_RegexpCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 3554 | 4098 | { |
| 3555 | 4099 | int opt_indices = 0; |
| 3556 | 4100 | int opt_all = 0; |
| 3557 | 4101 | int opt_inline = 0; |
| 4102 | + int opt_lineanchor = 0; | |
| 3558 | 4103 | regex_t *regex; |
| 3559 | 4104 | int match, i, j; |
| 3560 | 4105 | int offset = 0; |
| 3561 | 4106 | regmatch_t *pmatch = NULL; |
| 3562 | 4107 | int source_len; |
| @@ -3568,30 +4113,23 @@ | ||
| 3568 | 4113 | Jim_Obj *resultListObj = NULL; |
| 3569 | 4114 | int regcomp_flags = 0; |
| 3570 | 4115 | int eflags = 0; |
| 3571 | 4116 | int option; |
| 3572 | 4117 | enum { |
| 3573 | - OPT_INDICES, OPT_NOCASE, OPT_LINE, OPT_ALL, OPT_INLINE, OPT_START, OPT_END | |
| 4118 | + OPT_INDICES, OPT_NOCASE, OPT_LINE, OPT_LINESTOP, OPT_LINEANCHOR, OPT_ALL, OPT_INLINE, OPT_START, OPT_EXPANDED, OPT_END | |
| 3574 | 4119 | }; |
| 3575 | 4120 | static const char * const options[] = { |
| 3576 | - "-indices", "-nocase", "-line", "-all", "-inline", "-start", "--", NULL | |
| 4121 | + "-indices", "-nocase", "-line", "-linestop", "-lineanchor", "-all", "-inline", "-start", "-expanded", "--", NULL | |
| 3577 | 4122 | }; |
| 3578 | 4123 | |
| 3579 | - if (argc < 3) { | |
| 3580 | - wrongNumArgs: | |
| 3581 | - Jim_WrongNumArgs(interp, 1, argv, | |
| 3582 | - "?-switch ...? exp string ?matchVar? ?subMatchVar ...?"); | |
| 3583 | - return JIM_ERR; | |
| 3584 | - } | |
| 3585 | - | |
| 3586 | 4124 | for (i = 1; i < argc; i++) { |
| 3587 | 4125 | const char *opt = Jim_String(argv[i]); |
| 3588 | 4126 | |
| 3589 | 4127 | if (*opt != '-') { |
| 3590 | 4128 | break; |
| 3591 | 4129 | } |
| 3592 | - if (Jim_GetEnum(interp, argv[i], options, &option, "switch", JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK) { | |
| 4130 | + if (Jim_GetEnum(interp, argv[i], options, &option, "option", JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK) { | |
| 3593 | 4131 | return JIM_ERR; |
| 3594 | 4132 | } |
| 3595 | 4133 | if (option == OPT_END) { |
| 3596 | 4134 | i++; |
| 3597 | 4135 | break; |
| @@ -3605,12 +4143,24 @@ | ||
| 3605 | 4143 | regcomp_flags |= REG_ICASE; |
| 3606 | 4144 | break; |
| 3607 | 4145 | |
| 3608 | 4146 | case OPT_LINE: |
| 3609 | 4147 | regcomp_flags |= REG_NEWLINE; |
| 4148 | + opt_lineanchor = 1; | |
| 3610 | 4149 | break; |
| 3611 | 4150 | |
| 4151 | +#ifdef REG_NEWLINE_STOP | |
| 4152 | + case OPT_LINESTOP: | |
| 4153 | + regcomp_flags |= REG_NEWLINE_STOP; | |
| 4154 | + break; | |
| 4155 | +#endif | |
| 4156 | +#ifdef REG_NEWLINE_ANCHOR | |
| 4157 | + case OPT_LINEANCHOR: | |
| 4158 | + regcomp_flags |= REG_NEWLINE_ANCHOR; | |
| 4159 | + opt_lineanchor = 1; | |
| 4160 | + break; | |
| 4161 | +#endif | |
| 3612 | 4162 | case OPT_ALL: |
| 3613 | 4163 | opt_all = 1; |
| 3614 | 4164 | break; |
| 3615 | 4165 | |
| 3616 | 4166 | case OPT_INLINE: |
| @@ -3617,20 +4167,30 @@ | ||
| 3617 | 4167 | opt_inline = 1; |
| 3618 | 4168 | break; |
| 3619 | 4169 | |
| 3620 | 4170 | case OPT_START: |
| 3621 | 4171 | if (++i == argc) { |
| 3622 | - goto wrongNumArgs; | |
| 4172 | + return JIM_USAGE; | |
| 3623 | 4173 | } |
| 3624 | 4174 | if (Jim_GetIndex(interp, argv[i], &offset) != JIM_OK) { |
| 3625 | 4175 | return JIM_ERR; |
| 3626 | 4176 | } |
| 3627 | 4177 | break; |
| 4178 | + | |
| 4179 | +#ifdef REG_EXPANDED | |
| 4180 | + case OPT_EXPANDED: | |
| 4181 | + regcomp_flags |= REG_EXPANDED; | |
| 4182 | + break; | |
| 4183 | +#endif | |
| 4184 | + default: | |
| 4185 | + | |
| 4186 | + Jim_SetResultFormatted(interp, "not supported: %#s", argv[i]); | |
| 4187 | + return JIM_ERR; | |
| 3628 | 4188 | } |
| 3629 | 4189 | } |
| 3630 | 4190 | if (argc - i < 2) { |
| 3631 | - goto wrongNumArgs; | |
| 4191 | + return JIM_USAGE; | |
| 3632 | 4192 | } |
| 3633 | 4193 | |
| 3634 | 4194 | regex = SetRegexpFromAny(interp, argv[i], regcomp_flags); |
| 3635 | 4195 | if (!regex) { |
| 3636 | 4196 | return JIM_ERR; |
| @@ -3685,18 +4245,14 @@ | ||
| 3685 | 4245 | goto done; |
| 3686 | 4246 | } |
| 3687 | 4247 | |
| 3688 | 4248 | num_matches++; |
| 3689 | 4249 | |
| 3690 | - if (opt_all && !opt_inline) { | |
| 3691 | - | |
| 3692 | - goto try_next_match; | |
| 3693 | - } | |
| 3694 | 4250 | |
| 3695 | 4251 | |
| 3696 | 4252 | j = 0; |
| 3697 | - for (i += 2; opt_inline ? j < num_vars : i < argc; i++, j++) { | |
| 4253 | + for (j = 0; j < num_vars; j++) { | |
| 3698 | 4254 | Jim_Obj *resultObj; |
| 3699 | 4255 | |
| 3700 | 4256 | if (opt_indices) { |
| 3701 | 4257 | resultObj = Jim_NewListObj(interp, NULL, 0); |
| 3702 | 4258 | } |
| @@ -3726,21 +4282,19 @@ | ||
| 3726 | 4282 | if (opt_inline) { |
| 3727 | 4283 | Jim_ListAppendElement(interp, resultListObj, resultObj); |
| 3728 | 4284 | } |
| 3729 | 4285 | else { |
| 3730 | 4286 | |
| 3731 | - result = Jim_SetVariable(interp, argv[i], resultObj); | |
| 4287 | + result = Jim_SetVariable(interp, argv[i + 2 + j], resultObj); | |
| 3732 | 4288 | |
| 3733 | 4289 | if (result != JIM_OK) { |
| 3734 | - Jim_FreeObj(interp, resultObj); | |
| 3735 | 4290 | break; |
| 3736 | 4291 | } |
| 3737 | 4292 | } |
| 3738 | 4293 | } |
| 3739 | 4294 | |
| 3740 | - try_next_match: | |
| 3741 | - if (opt_all && (pattern[0] != '^' || (regcomp_flags & REG_NEWLINE)) && *source_str) { | |
| 4295 | + if (opt_all && (pattern[0] != '^' || opt_lineanchor) && *source_str) { | |
| 3742 | 4296 | if (pmatch[0].rm_eo) { |
| 3743 | 4297 | offset += utf8_strlen(source_str, pmatch[0].rm_eo); |
| 3744 | 4298 | source_str += pmatch[0].rm_eo; |
| 3745 | 4299 | } |
| 3746 | 4300 | else { |
| @@ -3792,30 +4346,23 @@ | ||
| 3792 | 4346 | const char *replace_str = NULL; |
| 3793 | 4347 | int replace_len; |
| 3794 | 4348 | const char *pattern; |
| 3795 | 4349 | int option; |
| 3796 | 4350 | enum { |
| 3797 | - OPT_NOCASE, OPT_LINE, OPT_ALL, OPT_START, OPT_COMMAND, OPT_END | |
| 4351 | + OPT_NOCASE, OPT_LINE, OPT_LINESTOP, OPT_LINEANCHOR, OPT_ALL, OPT_START, OPT_COMMAND, OPT_EXPANDED, OPT_END | |
| 3798 | 4352 | }; |
| 3799 | 4353 | static const char * const options[] = { |
| 3800 | - "-nocase", "-line", "-all", "-start", "-command", "--", NULL | |
| 4354 | + "-nocase", "-line", "-linestop", "-lineanchor", "-all", "-start", "-command", "-expanded", "--", NULL | |
| 3801 | 4355 | }; |
| 3802 | 4356 | |
| 3803 | - if (argc < 4) { | |
| 3804 | - wrongNumArgs: | |
| 3805 | - Jim_WrongNumArgs(interp, 1, argv, | |
| 3806 | - "?-switch ...? exp string subSpec ?varName?"); | |
| 3807 | - return JIM_ERR; | |
| 3808 | - } | |
| 3809 | - | |
| 3810 | 4357 | for (i = 1; i < argc; i++) { |
| 3811 | 4358 | const char *opt = Jim_String(argv[i]); |
| 3812 | 4359 | |
| 3813 | 4360 | if (*opt != '-') { |
| 3814 | 4361 | break; |
| 3815 | 4362 | } |
| 3816 | - if (Jim_GetEnum(interp, argv[i], options, &option, "switch", JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK) { | |
| 4363 | + if (Jim_GetEnum(interp, argv[i], options, &option, "option", JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK) { | |
| 3817 | 4364 | return JIM_ERR; |
| 3818 | 4365 | } |
| 3819 | 4366 | if (option == OPT_END) { |
| 3820 | 4367 | i++; |
| 3821 | 4368 | break; |
| @@ -3827,48 +4374,69 @@ | ||
| 3827 | 4374 | |
| 3828 | 4375 | case OPT_LINE: |
| 3829 | 4376 | regcomp_flags |= REG_NEWLINE; |
| 3830 | 4377 | break; |
| 3831 | 4378 | |
| 4379 | +#ifdef REG_NEWLINE_STOP | |
| 4380 | + case OPT_LINESTOP: | |
| 4381 | + regcomp_flags |= REG_NEWLINE_STOP; | |
| 4382 | + break; | |
| 4383 | +#endif | |
| 4384 | +#ifdef REG_NEWLINE_ANCHOR | |
| 4385 | + case OPT_LINEANCHOR: | |
| 4386 | + regcomp_flags |= REG_NEWLINE_ANCHOR; | |
| 4387 | + break; | |
| 4388 | +#endif | |
| 3832 | 4389 | case OPT_ALL: |
| 3833 | 4390 | opt_all = 1; |
| 3834 | 4391 | break; |
| 3835 | 4392 | |
| 3836 | 4393 | case OPT_START: |
| 3837 | 4394 | if (++i == argc) { |
| 3838 | - goto wrongNumArgs; | |
| 4395 | + return JIM_USAGE; | |
| 3839 | 4396 | } |
| 3840 | 4397 | if (Jim_GetIndex(interp, argv[i], &offset) != JIM_OK) { |
| 3841 | 4398 | return JIM_ERR; |
| 3842 | 4399 | } |
| 3843 | 4400 | break; |
| 3844 | 4401 | |
| 3845 | 4402 | case OPT_COMMAND: |
| 3846 | 4403 | opt_command = 1; |
| 3847 | 4404 | break; |
| 4405 | + | |
| 4406 | +#ifdef REG_EXPANDED | |
| 4407 | + case OPT_EXPANDED: | |
| 4408 | + regcomp_flags |= REG_EXPANDED; | |
| 4409 | + break; | |
| 4410 | +#endif | |
| 4411 | + | |
| 4412 | + default: | |
| 4413 | + | |
| 4414 | + Jim_SetResultFormatted(interp, "not supported: %#s", argv[i]); | |
| 4415 | + return JIM_ERR; | |
| 3848 | 4416 | } |
| 3849 | 4417 | } |
| 3850 | 4418 | if (argc - i != 3 && argc - i != 4) { |
| 3851 | - goto wrongNumArgs; | |
| 4419 | + return JIM_USAGE; | |
| 3852 | 4420 | } |
| 3853 | 4421 | |
| 3854 | 4422 | |
| 3855 | 4423 | regcomp_obj = Jim_DuplicateObj(interp, argv[i]); |
| 3856 | - Jim_IncrRefCount(regcomp_obj); | |
| 4424 | + Jim_IncrRefCount(regcomp_obj); | |
| 3857 | 4425 | regex = SetRegexpFromAny(interp, regcomp_obj, regcomp_flags); |
| 3858 | 4426 | if (!regex) { |
| 3859 | - Jim_DecrRefCount(interp, regcomp_obj); | |
| 4427 | + Jim_DecrRefCount(interp, regcomp_obj); | |
| 3860 | 4428 | return JIM_ERR; |
| 3861 | 4429 | } |
| 3862 | 4430 | pattern = Jim_String(argv[i]); |
| 3863 | 4431 | |
| 3864 | 4432 | source_str = Jim_GetString(argv[i + 1], &source_len); |
| 3865 | 4433 | if (opt_command) { |
| 3866 | 4434 | cmd_prefix = argv[i + 2]; |
| 3867 | 4435 | if (Jim_ListLength(interp, cmd_prefix) == 0) { |
| 3868 | 4436 | Jim_SetResultString(interp, "command prefix must be a list of at least one element", -1); |
| 3869 | - Jim_DecrRefCount(interp, regcomp_obj); | |
| 4437 | + Jim_DecrRefCount(interp, regcomp_obj); | |
| 3870 | 4438 | return JIM_ERR; |
| 3871 | 4439 | } |
| 3872 | 4440 | Jim_IncrRefCount(cmd_prefix); |
| 3873 | 4441 | } |
| 3874 | 4442 | else { |
| @@ -3897,11 +4465,12 @@ | ||
| 3897 | 4465 | Jim_AppendString(interp, resultObj, source_str, offset); |
| 3898 | 4466 | |
| 3899 | 4467 | |
| 3900 | 4468 | n = source_len - offset; |
| 3901 | 4469 | p = source_str + offset; |
| 3902 | - do { | |
| 4470 | + | |
| 4471 | + while (n || pattern[0]) { | |
| 3903 | 4472 | int match = jim_regexec(regex, p, MAX_SUB_MATCHES, pmatch, regexec_flags); |
| 3904 | 4473 | |
| 3905 | 4474 | if (match >= REG_BADPAT) { |
| 3906 | 4475 | char buf[100]; |
| 3907 | 4476 | |
| @@ -3978,32 +4547,26 @@ | ||
| 3978 | 4547 | |
| 3979 | 4548 | if (!opt_all || n == 0) { |
| 3980 | 4549 | break; |
| 3981 | 4550 | } |
| 3982 | 4551 | |
| 3983 | - | |
| 3984 | - if ((regcomp_flags & REG_NEWLINE) == 0 && pattern[0] == '^') { | |
| 3985 | - break; | |
| 3986 | - } | |
| 3987 | - | |
| 3988 | - | |
| 3989 | - if (pattern[0] == '\0' && n) { | |
| 3990 | - | |
| 3991 | - Jim_AppendString(interp, resultObj, p, 1); | |
| 3992 | - p++; | |
| 3993 | - n--; | |
| 3994 | - } | |
| 3995 | - | |
| 4552 | + regexec_flags = 0; | |
| 3996 | 4553 | if (pmatch[0].rm_eo == pmatch[0].rm_so) { |
| 3997 | 4554 | |
| 3998 | - regexec_flags = REG_NOTBOL; | |
| 4555 | + if (pattern[0] == '^') { | |
| 4556 | + | |
| 4557 | + regexec_flags = REG_NOTBOL; | |
| 4558 | + } | |
| 4559 | + else { | |
| 4560 | + | |
| 4561 | + int charlen = utf8_charlen(p[0]); | |
| 4562 | + Jim_AppendString(interp, resultObj, p, charlen); | |
| 4563 | + p += charlen; | |
| 4564 | + n -= charlen; | |
| 4565 | + } | |
| 3999 | 4566 | } |
| 4000 | - else { | |
| 4001 | - regexec_flags = 0; | |
| 4002 | - } | |
| 4003 | - | |
| 4004 | - } while (n); | |
| 4567 | + } | |
| 4005 | 4568 | |
| 4006 | 4569 | Jim_AppendString(interp, resultObj, p, -1); |
| 4007 | 4570 | |
| 4008 | 4571 | cmd_error: |
| 4009 | 4572 | if (result == JIM_OK) { |
| @@ -4012,13 +4575,10 @@ | ||
| 4012 | 4575 | result = Jim_SetVariable(interp, varname, resultObj); |
| 4013 | 4576 | |
| 4014 | 4577 | if (result == JIM_OK) { |
| 4015 | 4578 | Jim_SetResultInt(interp, num_matches); |
| 4016 | 4579 | } |
| 4017 | - else { | |
| 4018 | - Jim_FreeObj(interp, resultObj); | |
| 4019 | - } | |
| 4020 | 4580 | } |
| 4021 | 4581 | else { |
| 4022 | 4582 | Jim_SetResult(interp, resultObj); |
| 4023 | 4583 | result = JIM_OK; |
| 4024 | 4584 | } |
| @@ -4029,20 +4589,20 @@ | ||
| 4029 | 4589 | |
| 4030 | 4590 | if (opt_command) { |
| 4031 | 4591 | Jim_DecrRefCount(interp, cmd_prefix); |
| 4032 | 4592 | } |
| 4033 | 4593 | |
| 4034 | - Jim_DecrRefCount(interp, regcomp_obj); | |
| 4594 | + Jim_DecrRefCount(interp, regcomp_obj); | |
| 4035 | 4595 | |
| 4036 | 4596 | return result; |
| 4037 | 4597 | } |
| 4038 | 4598 | |
| 4039 | 4599 | int Jim_regexpInit(Jim_Interp *interp) |
| 4040 | 4600 | { |
| 4041 | 4601 | Jim_PackageProvideCheck(interp, "regexp"); |
| 4042 | - Jim_CreateCommand(interp, "regexp", Jim_RegexpCmd, NULL, NULL); | |
| 4043 | - Jim_CreateCommand(interp, "regsub", Jim_RegsubCmd, NULL, NULL); | |
| 4602 | + Jim_RegisterSimpleCmd(interp, "regexp", "?-option ...? exp string ?matchVar? ?subMatchVar ...?", 2, -1, Jim_RegexpCmd); | |
| 4603 | + Jim_RegisterSimpleCmd(interp, "regsub", "?-option ...? exp string subSpec ?varName?", 3, -1, Jim_RegsubCmd); | |
| 4044 | 4604 | return JIM_OK; |
| 4045 | 4605 | } |
| 4046 | 4606 | |
| 4047 | 4607 | #include <limits.h> |
| 4048 | 4608 | #include <stdlib.h> |
| @@ -4063,18 +4623,10 @@ | ||
| 4063 | 4623 | #define R_OK 4 |
| 4064 | 4624 | #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) |
| 4065 | 4625 | #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) |
| 4066 | 4626 | #endif |
| 4067 | 4627 | |
| 4068 | -# ifndef MAXPATHLEN | |
| 4069 | -# ifdef PATH_MAX | |
| 4070 | -# define MAXPATHLEN PATH_MAX | |
| 4071 | -# else | |
| 4072 | -# define MAXPATHLEN JIM_PATH_LEN | |
| 4073 | -# endif | |
| 4074 | -# endif | |
| 4075 | - | |
| 4076 | 4628 | #if defined(__MINGW32__) || defined(__MSYS__) || defined(_MSC_VER) |
| 4077 | 4629 | #define ISWINDOWS 1 |
| 4078 | 4630 | |
| 4079 | 4631 | #undef HAVE_SYMLINK |
| 4080 | 4632 | #else |
| @@ -4514,11 +5066,11 @@ | ||
| 4514 | 5066 | if (errno == ENOENT) { |
| 4515 | 5067 | |
| 4516 | 5068 | continue; |
| 4517 | 5069 | } |
| 4518 | 5070 | |
| 4519 | - if (errno == EEXIST) { | |
| 5071 | + if (errno == EEXIST || errno == ENOTDIR) { | |
| 4520 | 5072 | jim_stat_t sb; |
| 4521 | 5073 | |
| 4522 | 5074 | if (Jim_Stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) { |
| 4523 | 5075 | return 0; |
| 4524 | 5076 | } |
| @@ -4949,31 +5501,35 @@ | ||
| 4949 | 5501 | { "delete", |
| 4950 | 5502 | "?-force|--? name ...", |
| 4951 | 5503 | file_cmd_delete, |
| 4952 | 5504 | 1, |
| 4953 | 5505 | -1, |
| 5506 | + JIM_MODFLAG_NOTAINT, | |
| 4954 | 5507 | |
| 4955 | 5508 | }, |
| 4956 | 5509 | { "mkdir", |
| 4957 | 5510 | "dir ...", |
| 4958 | 5511 | file_cmd_mkdir, |
| 4959 | 5512 | 1, |
| 4960 | 5513 | -1, |
| 5514 | + JIM_MODFLAG_NOTAINT, | |
| 4961 | 5515 | |
| 4962 | 5516 | }, |
| 4963 | 5517 | { "tempfile", |
| 4964 | 5518 | "?template?", |
| 4965 | 5519 | file_cmd_tempfile, |
| 4966 | 5520 | 0, |
| 4967 | 5521 | 1, |
| 5522 | + JIM_MODFLAG_NOTAINT, | |
| 4968 | 5523 | |
| 4969 | 5524 | }, |
| 4970 | 5525 | { "rename", |
| 4971 | 5526 | "?-force? source dest", |
| 4972 | 5527 | file_cmd_rename, |
| 4973 | 5528 | 2, |
| 4974 | 5529 | 3, |
| 5530 | + JIM_MODFLAG_NOTAINT, | |
| 4975 | 5531 | |
| 4976 | 5532 | }, |
| 4977 | 5533 | #if defined(HAVE_LINK) && defined(HAVE_SYMLINK) |
| 4978 | 5534 | { "link", |
| 4979 | 5535 | "?-symbolic|-hard? newname target", |
| @@ -5048,18 +5604,11 @@ | ||
| 5048 | 5604 | } |
| 5049 | 5605 | }; |
| 5050 | 5606 | |
| 5051 | 5607 | static int Jim_CdCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 5052 | 5608 | { |
| 5053 | - const char *path; | |
| 5054 | - | |
| 5055 | - if (argc != 2) { | |
| 5056 | - Jim_WrongNumArgs(interp, 1, argv, "dirname"); | |
| 5057 | - return JIM_ERR; | |
| 5058 | - } | |
| 5059 | - | |
| 5060 | - path = Jim_String(argv[1]); | |
| 5609 | + const char *path = Jim_String(argv[1]); | |
| 5061 | 5610 | |
| 5062 | 5611 | if (chdir(path) != 0) { |
| 5063 | 5612 | Jim_SetResultFormatted(interp, "couldn't change working directory to \"%s\": %s", path, |
| 5064 | 5613 | strerror(errno)); |
| 5065 | 5614 | return JIM_ERR; |
| @@ -5084,13 +5633,13 @@ | ||
| 5084 | 5633 | } |
| 5085 | 5634 | |
| 5086 | 5635 | int Jim_fileInit(Jim_Interp *interp) |
| 5087 | 5636 | { |
| 5088 | 5637 | Jim_PackageProvideCheck(interp, "file"); |
| 5089 | - Jim_CreateCommand(interp, "file", Jim_SubCmdProc, (void *)file_command_table, NULL); | |
| 5090 | - Jim_CreateCommand(interp, "pwd", Jim_PwdCmd, NULL, NULL); | |
| 5091 | - Jim_CreateCommand(interp, "cd", Jim_CdCmd, NULL, NULL); | |
| 5638 | + Jim_RegisterSubCmd(interp, "file", file_command_table, NULL); | |
| 5639 | + Jim_RegisterSimpleCmd(interp, "pwd", "", 0, 0, Jim_PwdCmd); | |
| 5640 | + Jim_RegisterCmd(interp, "cd", "dirname", 1, 1, Jim_CdCmd, NULL, NULL, JIM_CMD_NOTAINT); | |
| 5092 | 5641 | return JIM_OK; |
| 5093 | 5642 | } |
| 5094 | 5643 | |
| 5095 | 5644 | #include <string.h> |
| 5096 | 5645 | #include <ctype.h> |
| @@ -5143,11 +5692,11 @@ | ||
| 5143 | 5692 | } |
| 5144 | 5693 | |
| 5145 | 5694 | int Jim_execInit(Jim_Interp *interp) |
| 5146 | 5695 | { |
| 5147 | 5696 | Jim_PackageProvideCheck(interp, "exec"); |
| 5148 | - Jim_CreateCommand(interp, "exec", Jim_ExecCmd, NULL, NULL); | |
| 5697 | + Jim_RegisterSimpleCmd(interp, "exec", "arg ?arg ...? ?&?", 1, -1, Jim_ExecCmd); | |
| 5149 | 5698 | return JIM_OK; |
| 5150 | 5699 | } |
| 5151 | 5700 | #else |
| 5152 | 5701 | |
| 5153 | 5702 | |
| @@ -5158,12 +5707,12 @@ | ||
| 5158 | 5707 | struct WaitInfoTable; |
| 5159 | 5708 | |
| 5160 | 5709 | static char **JimOriginalEnviron(void); |
| 5161 | 5710 | static char **JimSaveEnv(char **env); |
| 5162 | 5711 | static void JimRestoreEnv(char **env); |
| 5163 | -static int JimCreatePipeline(Jim_Interp *interp, int argc, Jim_Obj *const *argv, | |
| 5164 | - phandle_t **pidArrayPtr, int *inPipePtr, int *outPipePtr, int *errFilePtr); | |
| 5712 | +static int JimCreatePipeline(Jim_Interp *interp, int argc, Jim_Obj *const *argv, phandle_t **pidArrayPtr, | |
| 5713 | + int *outPipePtr, int *errFilePtr); | |
| 5165 | 5714 | static void JimDetachPids(struct WaitInfoTable *table, int numPids, const phandle_t *pidPtr); |
| 5166 | 5715 | static int JimCleanupChildren(Jim_Interp *interp, int numPids, phandle_t *pidPtr, Jim_Obj *errStrObj); |
| 5167 | 5716 | static int Jim_WaitCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv); |
| 5168 | 5717 | |
| 5169 | 5718 | #if defined(__MINGW32__) |
| @@ -5341,11 +5890,11 @@ | ||
| 5341 | 5890 | static struct WaitInfoTable *JimAllocWaitInfoTable(void) |
| 5342 | 5891 | { |
| 5343 | 5892 | struct WaitInfoTable *table = Jim_Alloc(sizeof(*table)); |
| 5344 | 5893 | table->info = NULL; |
| 5345 | 5894 | table->size = table->used = 0; |
| 5346 | - table->refcount = 1; | |
| 5895 | + table->refcount = 0; | |
| 5347 | 5896 | |
| 5348 | 5897 | return table; |
| 5349 | 5898 | } |
| 5350 | 5899 | |
| 5351 | 5900 | static int JimWaitRemove(struct WaitInfoTable *table, phandle_t phandle) |
| @@ -5379,11 +5928,11 @@ | ||
| 5379 | 5928 | if (argc > 1 && Jim_CompareStringImmediate(interp, argv[argc - 1], "&")) { |
| 5380 | 5929 | Jim_Obj *listObj; |
| 5381 | 5930 | int i; |
| 5382 | 5931 | |
| 5383 | 5932 | argc--; |
| 5384 | - numPids = JimCreatePipeline(interp, argc - 1, argv + 1, &pidPtr, NULL, NULL, NULL); | |
| 5933 | + numPids = JimCreatePipeline(interp, argc - 1, argv + 1, &pidPtr, NULL, NULL); | |
| 5385 | 5934 | if (numPids < 0) { |
| 5386 | 5935 | return JIM_ERR; |
| 5387 | 5936 | } |
| 5388 | 5937 | |
| 5389 | 5938 | listObj = Jim_NewListObj(interp, NULL, 0); |
| @@ -5395,11 +5944,11 @@ | ||
| 5395 | 5944 | Jim_Free(pidPtr); |
| 5396 | 5945 | return JIM_OK; |
| 5397 | 5946 | } |
| 5398 | 5947 | |
| 5399 | 5948 | numPids = |
| 5400 | - JimCreatePipeline(interp, argc - 1, argv + 1, &pidPtr, NULL, &outputId, &errorId); | |
| 5949 | + JimCreatePipeline(interp, argc - 1, argv + 1, &pidPtr, &outputId, &errorId); | |
| 5401 | 5950 | |
| 5402 | 5951 | if (numPids < 0) { |
| 5403 | 5952 | return JIM_ERR; |
| 5404 | 5953 | } |
| 5405 | 5954 | |
| @@ -5487,11 +6036,11 @@ | ||
| 5487 | 6036 | objv[1] = Jim_NewStringObj(interp, "getfd", -1); |
| 5488 | 6037 | |
| 5489 | 6038 | if (Jim_EvalObjVector(interp, 2, objv) == JIM_OK) { |
| 5490 | 6039 | jim_wide fd; |
| 5491 | 6040 | if (Jim_GetWide(interp, Jim_GetResult(interp), &fd) == JIM_OK) { |
| 5492 | - return fd; | |
| 6041 | + return dup(fd); | |
| 5493 | 6042 | } |
| 5494 | 6043 | } |
| 5495 | 6044 | return -1; |
| 5496 | 6045 | } |
| 5497 | 6046 | |
| @@ -5541,12 +6090,11 @@ | ||
| 5541 | 6090 | |
| 5542 | 6091 | if (argc > 1 && Jim_CompareStringImmediate(interp, argv[1], "-nohang")) { |
| 5543 | 6092 | nohang = 1; |
| 5544 | 6093 | } |
| 5545 | 6094 | if (argc != nohang + 2) { |
| 5546 | - Jim_WrongNumArgs(interp, 1, argv, "?-nohang? ?pid?"); | |
| 5547 | - return JIM_ERR; | |
| 6095 | + return JIM_USAGE; | |
| 5548 | 6096 | } |
| 5549 | 6097 | if (Jim_GetLong(interp, argv[nohang + 1], &pid) != JIM_OK) { |
| 5550 | 6098 | return JIM_ERR; |
| 5551 | 6099 | } |
| 5552 | 6100 | |
| @@ -5556,10 +6104,14 @@ | ||
| 5556 | 6104 | pid = -1; |
| 5557 | 6105 | } |
| 5558 | 6106 | #ifndef __MINGW32__ |
| 5559 | 6107 | else if (pid < 0) { |
| 5560 | 6108 | pid = phandle; |
| 6109 | + } | |
| 6110 | + else if (phandle == 0) { | |
| 6111 | + | |
| 6112 | + pid = 0; | |
| 5561 | 6113 | } |
| 5562 | 6114 | #endif |
| 5563 | 6115 | |
| 5564 | 6116 | errCodeObj = JimMakeErrorCode(interp, pid, status, NULL); |
| 5565 | 6117 | |
| @@ -5571,22 +6123,192 @@ | ||
| 5571 | 6123 | return JIM_OK; |
| 5572 | 6124 | } |
| 5573 | 6125 | |
| 5574 | 6126 | static int Jim_PidCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 5575 | 6127 | { |
| 5576 | - if (argc != 1) { | |
| 5577 | - Jim_WrongNumArgs(interp, 1, argv, ""); | |
| 5578 | - return JIM_ERR; | |
| 5579 | - } | |
| 5580 | - | |
| 5581 | 6128 | Jim_SetResultInt(interp, (jim_wide)getpid()); |
| 5582 | 6129 | return JIM_OK; |
| 5583 | 6130 | } |
| 5584 | 6131 | |
| 5585 | -static int | |
| 5586 | -JimCreatePipeline(Jim_Interp *interp, int argc, Jim_Obj *const *argv, phandle_t **pidArrayPtr, | |
| 5587 | - int *inPipePtr, int *outPipePtr, int *errFilePtr) | |
| 6132 | +#define JIM_ETT_IN 0x0001 | |
| 6133 | +#define JIM_ETT_OUT 0x0002 | |
| 6134 | +#define JIM_ETT_ERR 0x0004 | |
| 6135 | +#define JIM_ETT_PIPE 0x0008 | |
| 6136 | + | |
| 6137 | +#define JIM_ETT_NOARG 0x0010 | |
| 6138 | +#define JIM_ETT_APPEND 0x0020 | |
| 6139 | +#define JIM_ETT_STR 0x0040 | |
| 6140 | +#define JIM_ETT_DUPERR 0x0080 | |
| 6141 | +#define JIM_ETT_HANDLE 0x0100 | |
| 6142 | + | |
| 6143 | +#define JIM_ETT_CMD 0xF000 | |
| 6144 | +#define JIM_ETT_BAD 0xF001 | |
| 6145 | + | |
| 6146 | +struct redir_type_t { | |
| 6147 | + const char *prefix; | |
| 6148 | + unsigned flags; | |
| 6149 | +}; | |
| 6150 | + | |
| 6151 | + | |
| 6152 | +static const struct redir_type_t redir_types[] = { | |
| 6153 | + { "<<@", JIM_ETT_IN | JIM_ETT_HANDLE | JIM_ETT_STR }, | |
| 6154 | + { "<<", JIM_ETT_IN | JIM_ETT_STR }, | |
| 6155 | + { "<@", JIM_ETT_IN | JIM_ETT_HANDLE }, | |
| 6156 | + { "<", JIM_ETT_IN }, | |
| 6157 | + | |
| 6158 | + { "2>>", JIM_ETT_ERR | JIM_ETT_APPEND }, | |
| 6159 | + { "2>@", JIM_ETT_ERR | JIM_ETT_HANDLE }, | |
| 6160 | + { "2>", JIM_ETT_ERR }, | |
| 6161 | + | |
| 6162 | + { ">>&", JIM_ETT_OUT | JIM_ETT_APPEND | JIM_ETT_DUPERR }, | |
| 6163 | + { ">>", JIM_ETT_OUT | JIM_ETT_APPEND }, | |
| 6164 | + { ">&@", JIM_ETT_OUT | JIM_ETT_HANDLE | JIM_ETT_DUPERR }, | |
| 6165 | + { ">@", JIM_ETT_OUT | JIM_ETT_HANDLE }, | |
| 6166 | + { ">&", JIM_ETT_OUT | JIM_ETT_DUPERR }, | |
| 6167 | + { ">", JIM_ETT_OUT }, | |
| 6168 | + | |
| 6169 | + { "|&", JIM_ETT_PIPE | JIM_ETT_DUPERR }, | |
| 6170 | + { "|", JIM_ETT_PIPE }, | |
| 6171 | + { NULL } | |
| 6172 | +}; | |
| 6173 | + | |
| 6174 | +static unsigned JimExecClassifyArg(const char *arg) | |
| 6175 | +{ | |
| 6176 | + int i; | |
| 6177 | + for (i = 0; redir_types[i].prefix; i++) { | |
| 6178 | + int len = strlen(redir_types[i].prefix); | |
| 6179 | + if (strncmp(arg, redir_types[i].prefix, len) == 0) { | |
| 6180 | + if (strlen(arg) > len) { | |
| 6181 | + if (redir_types[i].flags & JIM_ETT_NOARG) { | |
| 6182 | + | |
| 6183 | + return JIM_ETT_BAD; | |
| 6184 | + } | |
| 6185 | + return redir_types[i].flags; | |
| 6186 | + } | |
| 6187 | + | |
| 6188 | + return redir_types[i].flags | JIM_ETT_NOARG; | |
| 6189 | + } | |
| 6190 | + } | |
| 6191 | + return JIM_ETT_CMD; | |
| 6192 | +} | |
| 6193 | + | |
| 6194 | +static int JimParsePipeline(Jim_Interp *interp, int argc, Jim_Obj *const *argv, Jim_Obj *cmdList, Jim_Obj *redirectList) | |
| 6195 | +{ | |
| 6196 | + int i; | |
| 6197 | + | |
| 6198 | + int first = 1; | |
| 6199 | + const char *arg = NULL; | |
| 6200 | + | |
| 6201 | + for (i = 0; i < argc; i++) { | |
| 6202 | + unsigned ett; | |
| 6203 | + if (first) { | |
| 6204 | + if (Jim_ListLength(interp, argv[i]) == 0) { | |
| 6205 | + Jim_SetResultString(interp, "empty command list", -1); | |
| 6206 | + return JIM_ERR; | |
| 6207 | + } | |
| 6208 | + Jim_ListAppendElement(interp, cmdList, argv[i]); | |
| 6209 | + first = 0; | |
| 6210 | + continue; | |
| 6211 | + } | |
| 6212 | + | |
| 6213 | + arg = Jim_String(argv[i]); | |
| 6214 | + ett = JimExecClassifyArg(arg); | |
| 6215 | + if (ett == JIM_ETT_BAD || ett == JIM_ETT_CMD) { | |
| 6216 | + Jim_SetResultFormatted(interp, "invalid redirection %s", arg); | |
| 6217 | + return JIM_ERR; | |
| 6218 | + } | |
| 6219 | + if (ett & JIM_ETT_PIPE) { | |
| 6220 | + Jim_ListAppendElement(interp, cmdList, argv[i]); | |
| 6221 | + first = 1; | |
| 6222 | + continue; | |
| 6223 | + } | |
| 6224 | + Jim_ListAppendElement(interp, redirectList, argv[i]); | |
| 6225 | + if ((ett & JIM_ETT_NOARG)) { | |
| 6226 | + | |
| 6227 | + if (i >= argc - 1) { | |
| 6228 | + | |
| 6229 | + Jim_SetResultFormatted(interp, "can't specify \"%#s\" as last word in command", argv[i]); | |
| 6230 | + return -1; | |
| 6231 | + } | |
| 6232 | + i++; | |
| 6233 | + Jim_ListAppendElement(interp, redirectList, argv[i]); | |
| 6234 | + } | |
| 6235 | + } | |
| 6236 | + | |
| 6237 | + if (first) { | |
| 6238 | + if (Jim_ListLength(interp, cmdList)) { | |
| 6239 | + Jim_SetResultFormatted(interp, "cmdlist required after %s", arg); | |
| 6240 | + } | |
| 6241 | + else { | |
| 6242 | + Jim_SetResultString(interp, "cmdlist is required", -1); | |
| 6243 | + } | |
| 6244 | + return JIM_ERR; | |
| 6245 | + } | |
| 6246 | + | |
| 6247 | + return JIM_OK; | |
| 6248 | +} | |
| 6249 | + | |
| 6250 | +static int JimParsePipelineLegacy(Jim_Interp *interp, int argc, Jim_Obj *const *argv, Jim_Obj *cmdList, Jim_Obj *redirectList) | |
| 6251 | +{ | |
| 6252 | + int i; | |
| 6253 | + | |
| 6254 | + Jim_Obj *cmdObj = Jim_NewListObj(interp, NULL, 0); | |
| 6255 | + Jim_ListAppendElement(interp, cmdList, cmdObj); | |
| 6256 | + const char *arg = NULL; | |
| 6257 | + | |
| 6258 | + for (i = 0; i < argc; i++) { | |
| 6259 | + arg = Jim_String(argv[i]); | |
| 6260 | + unsigned ett = JimExecClassifyArg(arg); | |
| 6261 | + if (ett == JIM_ETT_BAD) { | |
| 6262 | + Jim_SetResultFormatted(interp, "invalid: %s", arg); | |
| 6263 | + return JIM_ERR; | |
| 6264 | + } | |
| 6265 | + if (ett == JIM_ETT_CMD) { | |
| 6266 | + | |
| 6267 | + Jim_ListAppendElement(interp, cmdObj, argv[i]); | |
| 6268 | + continue; | |
| 6269 | + } | |
| 6270 | + if (ett & JIM_ETT_PIPE) { | |
| 6271 | + if (Jim_ListLength(interp, cmdObj) == 0) { | |
| 6272 | + goto missing_cmd; | |
| 6273 | + } | |
| 6274 | + | |
| 6275 | + Jim_ListAppendElement(interp, cmdList, argv[i]); | |
| 6276 | + | |
| 6277 | + cmdObj = Jim_NewListObj(interp, NULL, 0); | |
| 6278 | + Jim_ListAppendElement(interp, cmdList, cmdObj); | |
| 6279 | + continue; | |
| 6280 | + } | |
| 6281 | + Jim_ListAppendElement(interp, redirectList, argv[i]); | |
| 6282 | + if ((ett & JIM_ETT_NOARG)) { | |
| 6283 | + | |
| 6284 | + if (i >= argc - 1) { | |
| 6285 | + | |
| 6286 | + Jim_SetResultFormatted(interp, "can't specify \"%#s\" as last word in command", argv[i]); | |
| 6287 | + return -1; | |
| 6288 | + } | |
| 6289 | + i++; | |
| 6290 | + Jim_ListAppendElement(interp, redirectList, argv[i]); | |
| 6291 | + } | |
| 6292 | + } | |
| 6293 | + | |
| 6294 | + if (Jim_ListLength(interp, cmdObj) == 0) { | |
| 6295 | +missing_cmd: | |
| 6296 | + if (arg && *arg == '|') { | |
| 6297 | + Jim_SetResultString(interp, "illegal use of | or |& in command", -1); | |
| 6298 | + } | |
| 6299 | + else { | |
| 6300 | + Jim_SetResultString(interp, "didn't specify command to execute", -1); | |
| 6301 | + } | |
| 6302 | + return JIM_ERR; | |
| 6303 | + } | |
| 6304 | + | |
| 6305 | + return JIM_OK; | |
| 6306 | +} | |
| 6307 | + | |
| 6308 | +static int JimExecPipeline(Jim_Interp *interp, Jim_Obj *cmdList, Jim_Obj *redirectList, | |
| 6309 | + phandle_t **pidArrayPtr, int *outPipePtr, int *errFilePtr) | |
| 5588 | 6310 | { |
| 5589 | 6311 | phandle_t *pidPtr = NULL; /* Points to alloc-ed array holding all |
| 5590 | 6312 | * the pids of child processes. */ |
| 5591 | 6313 | int numPids = 0; /* Actual number of processes that exist |
| 5592 | 6314 | * at *pidPtr right now. */ |
| @@ -5624,126 +6346,88 @@ | ||
| 5624 | 6346 | int inputId = -1; |
| 5625 | 6347 | int outputId = -1; |
| 5626 | 6348 | int errorId = -1; |
| 5627 | 6349 | int lastOutputId = -1; |
| 5628 | 6350 | int pipeIds[2]; |
| 5629 | - int firstArg, lastArg; /* Indexes of first and last arguments in | |
| 5630 | - * current command. */ | |
| 5631 | - int lastBar; | |
| 5632 | 6351 | int i; |
| 5633 | 6352 | phandle_t phandle; |
| 5634 | 6353 | char **save_environ; |
| 5635 | 6354 | #if defined(HAVE_EXECVPE) && !defined(__MINGW32__) |
| 5636 | 6355 | char **child_environ; |
| 5637 | 6356 | #endif |
| 5638 | 6357 | struct WaitInfoTable *table = Jim_CmdPrivData(interp); |
| 5639 | 6358 | |
| 5640 | - | |
| 5641 | - char **arg_array = Jim_Alloc(sizeof(*arg_array) * (argc + 1)); | |
| 5642 | - int arg_count = 0; | |
| 5643 | - | |
| 5644 | - if (inPipePtr != NULL) { | |
| 5645 | - *inPipePtr = -1; | |
| 5646 | - } | |
| 5647 | 6359 | if (outPipePtr != NULL) { |
| 5648 | 6360 | *outPipePtr = -1; |
| 5649 | 6361 | } |
| 5650 | 6362 | if (errFilePtr != NULL) { |
| 5651 | 6363 | *errFilePtr = -1; |
| 5652 | 6364 | } |
| 5653 | 6365 | pipeIds[0] = pipeIds[1] = -1; |
| 5654 | 6366 | |
| 5655 | - cmdCount = 1; | |
| 5656 | - lastBar = -1; | |
| 5657 | - for (i = 0; i < argc; i++) { | |
| 5658 | - const char *arg = Jim_String(argv[i]); | |
| 5659 | - | |
| 5660 | - if (arg[0] == '<') { | |
| 5661 | - inputFile = FILE_NAME; | |
| 5662 | - input = arg + 1; | |
| 5663 | - if (*input == '<') { | |
| 5664 | - inputFile = FILE_TEXT; | |
| 5665 | - input_len = Jim_Length(argv[i]) - 2; | |
| 5666 | - input++; | |
| 5667 | - } | |
| 5668 | - else if (*input == '@') { | |
| 5669 | - inputFile = FILE_HANDLE; | |
| 5670 | - input++; | |
| 5671 | - } | |
| 5672 | - | |
| 5673 | - if (!*input && ++i < argc) { | |
| 5674 | - input = Jim_GetString(argv[i], &input_len); | |
| 5675 | - } | |
| 5676 | - } | |
| 5677 | - else if (arg[0] == '>') { | |
| 5678 | - int dup_error = 0; | |
| 5679 | - | |
| 5680 | - outputFile = FILE_NAME; | |
| 5681 | - | |
| 5682 | - output = arg + 1; | |
| 5683 | - if (*output == '>') { | |
| 5684 | - outputFile = FILE_APPEND; | |
| 5685 | - output++; | |
| 5686 | - } | |
| 5687 | - if (*output == '&') { | |
| 5688 | - | |
| 5689 | - output++; | |
| 5690 | - dup_error = 1; | |
| 5691 | - } | |
| 5692 | - if (*output == '@') { | |
| 5693 | - outputFile = FILE_HANDLE; | |
| 5694 | - output++; | |
| 5695 | - } | |
| 5696 | - if (!*output && ++i < argc) { | |
| 5697 | - output = Jim_String(argv[i]); | |
| 5698 | - } | |
| 5699 | - if (dup_error) { | |
| 5700 | - errorFile = outputFile; | |
| 6367 | + int redir_len = Jim_ListLength(interp, redirectList); | |
| 6368 | + for (i = 0; i < redir_len; i++) { | |
| 6369 | + int len; | |
| 6370 | + int item_len; | |
| 6371 | + Jim_Obj *redirObj = Jim_ListGetIndex(interp, redirectList, i); | |
| 6372 | + const char *arg = Jim_GetString(redirObj, &len); | |
| 6373 | + unsigned ett = JimExecClassifyArg(arg); | |
| 6374 | + const char *item; | |
| 6375 | + int type = FILE_NAME; | |
| 6376 | + if ((ett & JIM_ETT_NOARG) == 0) { | |
| 6377 | + | |
| 6378 | + item = arg + 1; | |
| 6379 | + if (ett & JIM_ETT_HANDLE) { | |
| 6380 | + item++; | |
| 6381 | + } | |
| 6382 | + if (ett & JIM_ETT_APPEND) { | |
| 6383 | + item++; | |
| 6384 | + } | |
| 6385 | + if (ett & JIM_ETT_DUPERR) { | |
| 6386 | + item++; | |
| 6387 | + } | |
| 6388 | + if (ett & JIM_ETT_ERR) { | |
| 6389 | + item++; | |
| 6390 | + } | |
| 6391 | + if (ett & JIM_ETT_STR) { | |
| 6392 | + type = FILE_TEXT; | |
| 6393 | + item++; | |
| 6394 | + } | |
| 6395 | + item_len = len - (item - arg); | |
| 6396 | + } | |
| 6397 | + else { | |
| 6398 | + | |
| 6399 | + i++; | |
| 6400 | + item = Jim_GetString(Jim_ListGetIndex(interp, redirectList, i), &item_len); | |
| 6401 | + } | |
| 6402 | + | |
| 6403 | + if (ett & JIM_ETT_HANDLE) { | |
| 6404 | + type = FILE_HANDLE; | |
| 6405 | + } | |
| 6406 | + if (ett & JIM_ETT_APPEND) { | |
| 6407 | + type = FILE_APPEND; | |
| 6408 | + } | |
| 6409 | + if (ett & JIM_ETT_STR) { | |
| 6410 | + type = FILE_TEXT; | |
| 6411 | + } | |
| 6412 | + if (ett & JIM_ETT_IN) { | |
| 6413 | + input = item; | |
| 6414 | + input_len = item_len; | |
| 6415 | + inputFile = type; | |
| 6416 | + } | |
| 6417 | + else if (ett & JIM_ETT_OUT) { | |
| 6418 | + output = item; | |
| 6419 | + outputFile = type; | |
| 6420 | + if (ett & JIM_ETT_DUPERR) { | |
| 5701 | 6421 | error = output; |
| 5702 | - } | |
| 5703 | - } | |
| 5704 | - else if (arg[0] == '2' && arg[1] == '>') { | |
| 5705 | - error = arg + 2; | |
| 5706 | - errorFile = FILE_NAME; | |
| 5707 | - | |
| 5708 | - if (*error == '@') { | |
| 5709 | - errorFile = FILE_HANDLE; | |
| 5710 | - error++; | |
| 5711 | - } | |
| 5712 | - else if (*error == '>') { | |
| 5713 | - errorFile = FILE_APPEND; | |
| 5714 | - error++; | |
| 5715 | - } | |
| 5716 | - if (!*error && ++i < argc) { | |
| 5717 | - error = Jim_String(argv[i]); | |
| 5718 | - } | |
| 5719 | - } | |
| 5720 | - else { | |
| 5721 | - if (strcmp(arg, "|") == 0 || strcmp(arg, "|&") == 0) { | |
| 5722 | - if (i == lastBar + 1 || i == argc - 1) { | |
| 5723 | - Jim_SetResultString(interp, "illegal use of | or |& in command", -1); | |
| 5724 | - goto badargs; | |
| 5725 | - } | |
| 5726 | - lastBar = i; | |
| 5727 | - cmdCount++; | |
| 5728 | - } | |
| 5729 | - | |
| 5730 | - arg_array[arg_count++] = (char *)arg; | |
| 5731 | - continue; | |
| 5732 | - } | |
| 5733 | - | |
| 5734 | - if (i >= argc) { | |
| 5735 | - Jim_SetResultFormatted(interp, "can't specify \"%s\" as last word in command", arg); | |
| 5736 | - goto badargs; | |
| 5737 | - } | |
| 5738 | - } | |
| 5739 | - | |
| 5740 | - if (arg_count == 0) { | |
| 5741 | - Jim_SetResultString(interp, "didn't specify command to execute", -1); | |
| 5742 | -badargs: | |
| 5743 | - Jim_Free(arg_array); | |
| 5744 | - return -1; | |
| 6422 | + errorFile = outputFile; | |
| 6423 | + } | |
| 6424 | + } | |
| 6425 | + else if (ett & JIM_ETT_ERR) { | |
| 6426 | + error = item; | |
| 6427 | + errorFile = type; | |
| 6428 | + } | |
| 5745 | 6429 | } |
| 5746 | 6430 | |
| 5747 | 6431 | |
| 5748 | 6432 | save_environ = JimSaveEnv(JimBuildEnv(interp)); |
| 5749 | 6433 | |
| @@ -5759,42 +6443,30 @@ | ||
| 5759 | 6443 | goto error; |
| 5760 | 6444 | } |
| 5761 | 6445 | Jim_Lseek(inputId, 0L, SEEK_SET); |
| 5762 | 6446 | } |
| 5763 | 6447 | else if (inputFile == FILE_HANDLE) { |
| 5764 | - int fd = JimGetChannelFd(interp, input); | |
| 5765 | - | |
| 5766 | - if (fd < 0) { | |
| 6448 | + inputId = JimGetChannelFd(interp, input); | |
| 6449 | + if (inputId < 0) { | |
| 5767 | 6450 | goto error; |
| 5768 | 6451 | } |
| 5769 | - inputId = dup(fd); | |
| 5770 | 6452 | } |
| 5771 | 6453 | else { |
| 5772 | 6454 | inputId = Jim_OpenForRead(input); |
| 5773 | 6455 | if (inputId == -1) { |
| 5774 | 6456 | Jim_SetResultFormatted(interp, "couldn't read file \"%s\": %s", input, strerror(Jim_Errno())); |
| 5775 | 6457 | goto error; |
| 5776 | 6458 | } |
| 5777 | 6459 | } |
| 5778 | 6460 | } |
| 5779 | - else if (inPipePtr != NULL) { | |
| 5780 | - if (pipe(pipeIds) != 0) { | |
| 5781 | - Jim_SetResultErrno(interp, "couldn't create input pipe for command"); | |
| 5782 | - goto error; | |
| 5783 | - } | |
| 5784 | - inputId = pipeIds[0]; | |
| 5785 | - *inPipePtr = pipeIds[1]; | |
| 5786 | - pipeIds[0] = pipeIds[1] = -1; | |
| 5787 | - } | |
| 5788 | 6461 | |
| 5789 | 6462 | if (output != NULL) { |
| 5790 | 6463 | if (outputFile == FILE_HANDLE) { |
| 5791 | - int fd = JimGetChannelFd(interp, output); | |
| 5792 | - if (fd < 0) { | |
| 6464 | + lastOutputId = JimGetChannelFd(interp, output); | |
| 6465 | + if (lastOutputId < 0) { | |
| 5793 | 6466 | goto error; |
| 5794 | 6467 | } |
| 5795 | - lastOutputId = dup(fd); | |
| 5796 | 6468 | } |
| 5797 | 6469 | else { |
| 5798 | 6470 | lastOutputId = Jim_OpenForWrite(output, outputFile == FILE_APPEND); |
| 5799 | 6471 | if (lastOutputId == -1) { |
| 5800 | 6472 | Jim_SetResultFormatted(interp, "couldn't write file \"%s\": %s", output, strerror(Jim_Errno())); |
| @@ -5809,10 +6481,11 @@ | ||
| 5809 | 6481 | } |
| 5810 | 6482 | lastOutputId = pipeIds[1]; |
| 5811 | 6483 | *outPipePtr = pipeIds[0]; |
| 5812 | 6484 | pipeIds[0] = pipeIds[1] = -1; |
| 5813 | 6485 | } |
| 6486 | + | |
| 5814 | 6487 | |
| 5815 | 6488 | if (error != NULL) { |
| 5816 | 6489 | if (errorFile == FILE_HANDLE) { |
| 5817 | 6490 | if (strcmp(error, "1") == 0) { |
| 5818 | 6491 | |
| @@ -5823,15 +6496,14 @@ | ||
| 5823 | 6496 | |
| 5824 | 6497 | error = "stdout"; |
| 5825 | 6498 | } |
| 5826 | 6499 | } |
| 5827 | 6500 | if (errorId == -1) { |
| 5828 | - int fd = JimGetChannelFd(interp, error); | |
| 5829 | - if (fd < 0) { | |
| 6501 | + errorId = JimGetChannelFd(interp, error); | |
| 6502 | + if (errorId < 0) { | |
| 5830 | 6503 | goto error; |
| 5831 | 6504 | } |
| 5832 | - errorId = dup(fd); | |
| 5833 | 6505 | } |
| 5834 | 6506 | } |
| 5835 | 6507 | else { |
| 5836 | 6508 | errorId = Jim_OpenForWrite(error, errorFile == FILE_APPEND); |
| 5837 | 6509 | if (errorId == -1) { |
| @@ -5846,60 +6518,60 @@ | ||
| 5846 | 6518 | goto error; |
| 5847 | 6519 | } |
| 5848 | 6520 | *errFilePtr = dup(errorId); |
| 5849 | 6521 | } |
| 5850 | 6522 | |
| 5851 | - | |
| 6523 | + int cmd_list_size = Jim_ListLength(interp, cmdList); | |
| 6524 | + cmdCount = (cmd_list_size + 1) / 2; | |
| 5852 | 6525 | pidPtr = Jim_Alloc(cmdCount * sizeof(*pidPtr)); |
| 5853 | - for (firstArg = 0; firstArg < arg_count; numPids++, firstArg = lastArg + 1) { | |
| 5854 | - int pipe_dup_err = 0; | |
| 6526 | + | |
| 6527 | + for (i = 0; i < cmd_list_size; ) { | |
| 6528 | + char **arg_array; | |
| 6529 | + int j; | |
| 5855 | 6530 | int origErrorId = errorId; |
| 5856 | - | |
| 5857 | - for (lastArg = firstArg; lastArg < arg_count; lastArg++) { | |
| 5858 | - if (strcmp(arg_array[lastArg], "|") == 0) { | |
| 5859 | - break; | |
| 5860 | - } | |
| 5861 | - if (strcmp(arg_array[lastArg], "|&") == 0) { | |
| 5862 | - pipe_dup_err = 1; | |
| 5863 | - break; | |
| 5864 | - } | |
| 5865 | - } | |
| 5866 | - | |
| 5867 | - if (lastArg == firstArg) { | |
| 5868 | - Jim_SetResultString(interp, "missing command to exec", -1); | |
| 5869 | - goto error; | |
| 6531 | + Jim_Obj *cmdObj = Jim_ListGetIndex(interp, cmdList, i++); | |
| 6532 | + int cmd_len = Jim_ListLength(interp, cmdObj); | |
| 6533 | + Jim_Obj *sepObj = NULL; | |
| 6534 | + if (i < cmd_list_size - 1) { | |
| 6535 | + sepObj = Jim_ListGetIndex(interp, cmdList, i++); | |
| 5870 | 6536 | } |
| 5871 | 6537 | |
| 5872 | 6538 | |
| 5873 | - arg_array[lastArg] = NULL; | |
| 5874 | - if (lastArg == arg_count) { | |
| 6539 | + arg_array = Jim_Alloc((cmd_len + 1) * sizeof(*arg_array)); | |
| 6540 | + for (j = 0; j < cmd_len; j++) { | |
| 6541 | + arg_array[j] = (char *)Jim_String(Jim_ListGetIndex(interp, cmdObj, j)); | |
| 6542 | + } | |
| 6543 | + arg_array[j] = NULL; | |
| 6544 | + | |
| 6545 | + if (sepObj == NULL) { | |
| 5875 | 6546 | outputId = lastOutputId; |
| 5876 | 6547 | lastOutputId = -1; |
| 5877 | 6548 | } |
| 5878 | 6549 | else { |
| 5879 | 6550 | if (pipe(pipeIds) != 0) { |
| 5880 | 6551 | Jim_SetResultErrno(interp, "couldn't create pipe"); |
| 6552 | + Jim_Free(arg_array); | |
| 5881 | 6553 | goto error; |
| 5882 | 6554 | } |
| 5883 | 6555 | outputId = pipeIds[1]; |
| 5884 | 6556 | } |
| 5885 | 6557 | |
| 5886 | 6558 | |
| 5887 | - if (pipe_dup_err) { | |
| 6559 | + if (sepObj && Jim_CompareStringImmediate(interp, sepObj, "|&")) { | |
| 5888 | 6560 | errorId = outputId; |
| 5889 | 6561 | } |
| 5890 | 6562 | |
| 5891 | 6563 | |
| 5892 | 6564 | |
| 5893 | 6565 | #ifdef __MINGW32__ |
| 5894 | - phandle = JimStartWinProcess(interp, &arg_array[firstArg], save_environ, inputId, outputId, errorId); | |
| 6566 | + phandle = JimStartWinProcess(interp, &arg_array[0], save_environ, inputId, outputId, errorId); | |
| 5895 | 6567 | if (phandle == JIM_BAD_PHANDLE) { |
| 5896 | - Jim_SetResultFormatted(interp, "couldn't exec \"%s\"", arg_array[firstArg]); | |
| 6568 | + Jim_SetResultFormatted(interp, "couldn't exec \"%s\"", arg_array[0]); | |
| 5897 | 6569 | goto error; |
| 5898 | 6570 | } |
| 5899 | 6571 | #else |
| 5900 | - i = strlen(arg_array[firstArg]); | |
| 6572 | + int argv0_len = strlen(arg_array[0]); | |
| 5901 | 6573 | |
| 5902 | 6574 | #ifdef HAVE_EXECVPE |
| 5903 | 6575 | child_environ = Jim_GetEnviron(); |
| 5904 | 6576 | #endif |
| 5905 | 6577 | #ifdef HAVE_VFORK |
| @@ -5940,14 +6612,14 @@ | ||
| 5940 | 6612 | } |
| 5941 | 6613 | if (lastOutputId != -1) { |
| 5942 | 6614 | close(lastOutputId); |
| 5943 | 6615 | } |
| 5944 | 6616 | |
| 5945 | - execvpe(arg_array[firstArg], &arg_array[firstArg], child_environ); | |
| 6617 | + execvpe(arg_array[0], arg_array, child_environ); | |
| 5946 | 6618 | |
| 5947 | 6619 | if (write(fileno(stderr), "couldn't exec \"", 15) && |
| 5948 | - write(fileno(stderr), arg_array[firstArg], i) && | |
| 6620 | + write(fileno(stderr), arg_array[0], argv0_len) && | |
| 5949 | 6621 | write(fileno(stderr), "\"\n", 2)) { |
| 5950 | 6622 | |
| 5951 | 6623 | } |
| 5952 | 6624 | #ifdef JIM_MAINTAINER |
| 5953 | 6625 | { |
| @@ -5959,10 +6631,11 @@ | ||
| 5959 | 6631 | _exit(127); |
| 5960 | 6632 | } |
| 5961 | 6633 | #endif |
| 5962 | 6634 | |
| 5963 | 6635 | |
| 6636 | + Jim_Free(arg_array); | |
| 5964 | 6637 | |
| 5965 | 6638 | if (table->used == table->size) { |
| 5966 | 6639 | table->size += WAIT_TABLE_GROW_BY; |
| 5967 | 6640 | table->info = Jim_Realloc(table->info, table->size * sizeof(*table->info)); |
| 5968 | 6641 | } |
| @@ -5969,11 +6642,11 @@ | ||
| 5969 | 6642 | |
| 5970 | 6643 | table->info[table->used].phandle = phandle; |
| 5971 | 6644 | table->info[table->used].flags = 0; |
| 5972 | 6645 | table->used++; |
| 5973 | 6646 | |
| 5974 | - pidPtr[numPids] = phandle; | |
| 6647 | + pidPtr[numPids++] = phandle; | |
| 5975 | 6648 | |
| 5976 | 6649 | |
| 5977 | 6650 | errorId = origErrorId; |
| 5978 | 6651 | |
| 5979 | 6652 | |
| @@ -5997,22 +6670,17 @@ | ||
| 5997 | 6670 | close(lastOutputId); |
| 5998 | 6671 | } |
| 5999 | 6672 | if (errorId != -1) { |
| 6000 | 6673 | close(errorId); |
| 6001 | 6674 | } |
| 6002 | - Jim_Free(arg_array); | |
| 6003 | 6675 | |
| 6004 | 6676 | JimRestoreEnv(save_environ); |
| 6005 | 6677 | |
| 6006 | 6678 | return numPids; |
| 6007 | 6679 | |
| 6008 | 6680 | |
| 6009 | 6681 | error: |
| 6010 | - if ((inPipePtr != NULL) && (*inPipePtr != -1)) { | |
| 6011 | - close(*inPipePtr); | |
| 6012 | - *inPipePtr = -1; | |
| 6013 | - } | |
| 6014 | 6682 | if ((outPipePtr != NULL) && (*outPipePtr != -1)) { |
| 6015 | 6683 | close(*outPipePtr); |
| 6016 | 6684 | *outPipePtr = -1; |
| 6017 | 6685 | } |
| 6018 | 6686 | if ((errFilePtr != NULL) && (*errFilePtr != -1)) { |
| @@ -6034,10 +6702,39 @@ | ||
| 6034 | 6702 | Jim_Free(pidPtr); |
| 6035 | 6703 | } |
| 6036 | 6704 | numPids = -1; |
| 6037 | 6705 | goto cleanup; |
| 6038 | 6706 | } |
| 6707 | + | |
| 6708 | +static int | |
| 6709 | +JimCreatePipeline(Jim_Interp *interp, int argc, Jim_Obj *const *argv, phandle_t **pidArrayPtr, | |
| 6710 | + int *outPipePtr, int *errFilePtr) | |
| 6711 | +{ | |
| 6712 | + int rc = -1; | |
| 6713 | + int ret; | |
| 6714 | + | |
| 6715 | + Jim_Obj *cmdList = Jim_NewListObj(interp, NULL, 0); | |
| 6716 | + Jim_Obj *redirectList = Jim_NewListObj(interp, NULL, 0); | |
| 6717 | + Jim_IncrRefCount(cmdList); | |
| 6718 | + Jim_IncrRefCount(redirectList); | |
| 6719 | + | |
| 6720 | + if (argc > 1 && Jim_CompareStringImmediate(interp, argv[0], "|")) { | |
| 6721 | + | |
| 6722 | + ret = JimParsePipeline(interp, argc - 1, argv + 1, cmdList, redirectList); | |
| 6723 | + } | |
| 6724 | + else { | |
| 6725 | + | |
| 6726 | + ret = JimParsePipelineLegacy(interp, argc, argv, cmdList, redirectList); | |
| 6727 | + } | |
| 6728 | + if (ret == JIM_OK) { | |
| 6729 | + | |
| 6730 | + rc = JimExecPipeline(interp, cmdList, redirectList, pidArrayPtr, outPipePtr, errFilePtr); | |
| 6731 | + } | |
| 6732 | + Jim_DecrRefCount(interp, cmdList); | |
| 6733 | + Jim_DecrRefCount(interp, redirectList); | |
| 6734 | + return rc; | |
| 6735 | +} | |
| 6039 | 6736 | |
| 6040 | 6737 | |
| 6041 | 6738 | static int JimCleanupChildren(Jim_Interp *interp, int numPids, phandle_t *pidPtr, Jim_Obj *errStrObj) |
| 6042 | 6739 | { |
| 6043 | 6740 | struct WaitInfoTable *table = Jim_CmdPrivData(interp); |
| @@ -6064,14 +6761,15 @@ | ||
| 6064 | 6761 | struct WaitInfoTable *waitinfo; |
| 6065 | 6762 | |
| 6066 | 6763 | Jim_PackageProvideCheck(interp, "exec"); |
| 6067 | 6764 | |
| 6068 | 6765 | waitinfo = JimAllocWaitInfoTable(); |
| 6069 | - Jim_CreateCommand(interp, "exec", Jim_ExecCmd, waitinfo, JimFreeWaitInfoTable); | |
| 6766 | + Jim_RegisterCmd(interp, "exec", "arg ?arg ...? ?&?", 1, -1, Jim_ExecCmd, JimFreeWaitInfoTable, waitinfo, JIM_CMD_NOTAINT); | |
| 6767 | + waitinfo->refcount++; | |
| 6768 | + Jim_RegisterCmd(interp, "wait", "?-nohang? ?pid?", 0, 2, Jim_WaitCommand, JimFreeWaitInfoTable, waitinfo, 0); | |
| 6070 | 6769 | waitinfo->refcount++; |
| 6071 | - Jim_CreateCommand(interp, "wait", Jim_WaitCommand, waitinfo, JimFreeWaitInfoTable); | |
| 6072 | - Jim_CreateCommand(interp, "pid", Jim_PidCommand, 0, 0); | |
| 6770 | + Jim_RegisterSimpleCmd(interp, "pid", "", 0, 0, Jim_PidCommand); | |
| 6073 | 6771 | |
| 6074 | 6772 | return JIM_OK; |
| 6075 | 6773 | } |
| 6076 | 6774 | |
| 6077 | 6775 | #if defined(__MINGW32__) |
| @@ -6494,11 +7192,11 @@ | ||
| 6494 | 7192 | }; |
| 6495 | 7193 | |
| 6496 | 7194 | int Jim_clockInit(Jim_Interp *interp) |
| 6497 | 7195 | { |
| 6498 | 7196 | Jim_PackageProvideCheck(interp, "clock"); |
| 6499 | - Jim_CreateCommand(interp, "clock", Jim_SubCmdProc, (void *)clock_command_table, NULL); | |
| 7197 | + Jim_RegisterSubCmd(interp, "clock", clock_command_table, NULL); | |
| 6500 | 7198 | return JIM_OK; |
| 6501 | 7199 | } |
| 6502 | 7200 | |
| 6503 | 7201 | #include <limits.h> |
| 6504 | 7202 | #include <stdlib.h> |
| @@ -6714,58 +7412,11 @@ | ||
| 6714 | 7412 | }; |
| 6715 | 7413 | |
| 6716 | 7414 | int Jim_arrayInit(Jim_Interp *interp) |
| 6717 | 7415 | { |
| 6718 | 7416 | Jim_PackageProvideCheck(interp, "array"); |
| 6719 | - Jim_CreateCommand(interp, "array", Jim_SubCmdProc, (void *)array_command_table, NULL); | |
| 6720 | - return JIM_OK; | |
| 6721 | -} | |
| 6722 | - | |
| 6723 | -#include <sys/types.h> | |
| 6724 | -#include <sys/time.h> | |
| 6725 | -#include <sys/wait.h> | |
| 6726 | -#include <unistd.h> | |
| 6727 | -#include <string.h> | |
| 6728 | -#include <errno.h> | |
| 6729 | - | |
| 6730 | - | |
| 6731 | -#ifdef HAVE_SYS_SYSINFO_H | |
| 6732 | -#include <sys/sysinfo.h> | |
| 6733 | -#endif | |
| 6734 | - | |
| 6735 | -static void Jim_PosixSetError(Jim_Interp *interp) | |
| 6736 | -{ | |
| 6737 | - Jim_SetResultString(interp, strerror(errno), -1); | |
| 6738 | -} | |
| 6739 | - | |
| 6740 | -#if defined(HAVE_FORK) | |
| 6741 | -static int Jim_PosixForkCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) | |
| 6742 | -{ | |
| 6743 | - pid_t pid; | |
| 6744 | - | |
| 6745 | - JIM_NOTUSED(argv); | |
| 6746 | - | |
| 6747 | - if (argc != 1) { | |
| 6748 | - Jim_WrongNumArgs(interp, 1, argv, ""); | |
| 6749 | - return JIM_ERR; | |
| 6750 | - } | |
| 6751 | - if ((pid = fork()) == -1) { | |
| 6752 | - Jim_PosixSetError(interp); | |
| 6753 | - return JIM_ERR; | |
| 6754 | - } | |
| 6755 | - Jim_SetResultInt(interp, (jim_wide) pid); | |
| 6756 | - return JIM_OK; | |
| 6757 | -} | |
| 6758 | -#endif | |
| 6759 | - | |
| 6760 | - | |
| 6761 | -int Jim_posixInit(Jim_Interp *interp) | |
| 6762 | -{ | |
| 6763 | - Jim_PackageProvideCheck(interp, "posix"); | |
| 6764 | -#ifdef HAVE_FORK | |
| 6765 | - Jim_CreateCommand(interp, "os.fork", Jim_PosixForkCommand, NULL, NULL); | |
| 6766 | -#endif | |
| 7417 | + Jim_RegisterSubCmd(interp, "array", array_command_table, NULL); | |
| 6767 | 7418 | return JIM_OK; |
| 6768 | 7419 | } |
| 6769 | 7420 | int Jim_InitStaticExtensions(Jim_Interp *interp) |
| 6770 | 7421 | { |
| 6771 | 7422 | extern int Jim_bootstrapInit(Jim_Interp *); |
| @@ -6773,11 +7424,10 @@ | ||
| 6773 | 7424 | extern int Jim_readdirInit(Jim_Interp *); |
| 6774 | 7425 | extern int Jim_regexpInit(Jim_Interp *); |
| 6775 | 7426 | extern int Jim_fileInit(Jim_Interp *); |
| 6776 | 7427 | extern int Jim_globInit(Jim_Interp *); |
| 6777 | 7428 | extern int Jim_execInit(Jim_Interp *); |
| 6778 | -extern int Jim_posixInit(Jim_Interp *); | |
| 6779 | 7429 | extern int Jim_clockInit(Jim_Interp *); |
| 6780 | 7430 | extern int Jim_arrayInit(Jim_Interp *); |
| 6781 | 7431 | extern int Jim_stdlibInit(Jim_Interp *); |
| 6782 | 7432 | extern int Jim_tclcompatInit(Jim_Interp *); |
| 6783 | 7433 | Jim_bootstrapInit(interp); |
| @@ -6785,11 +7435,10 @@ | ||
| 6785 | 7435 | Jim_readdirInit(interp); |
| 6786 | 7436 | Jim_regexpInit(interp); |
| 6787 | 7437 | Jim_fileInit(interp); |
| 6788 | 7438 | Jim_globInit(interp); |
| 6789 | 7439 | Jim_execInit(interp); |
| 6790 | -Jim_posixInit(interp); | |
| 6791 | 7440 | Jim_clockInit(interp); |
| 6792 | 7441 | Jim_arrayInit(interp); |
| 6793 | 7442 | Jim_stdlibInit(interp); |
| 6794 | 7443 | Jim_tclcompatInit(interp); |
| 6795 | 7444 | return JIM_OK; |
| @@ -6854,11 +7503,12 @@ | ||
| 6854 | 7503 | |
| 6855 | 7504 | |
| 6856 | 7505 | |
| 6857 | 7506 | #define JIM_INTEGER_SPACE 24 |
| 6858 | 7507 | |
| 6859 | -#if defined(DEBUG_SHOW_SCRIPT) || defined(DEBUG_SHOW_SCRIPT_TOKENS) || defined(JIM_DEBUG_COMMAND) || defined(DEBUG_SHOW_SUBST) | |
| 7508 | +#if defined(DEBUG_SHOW_SCRIPT) || defined(DEBUG_SHOW_SCRIPT_TOKENS) || defined(JIM_DEBUG_COMMAND) || defined(DEBUG_SHOW_EXPR_TOKENS) || defined(DEBUG_SHOW_EXPR) | |
| 7509 | +#define JIM_TT_NAME | |
| 6860 | 7510 | static const char *jim_tt_name(int type); |
| 6861 | 7511 | #endif |
| 6862 | 7512 | |
| 6863 | 7513 | #ifdef JIM_DEBUG_PANIC |
| 6864 | 7514 | static void JimPanicDump(int fail_condition, const char *fmt, ...); |
| @@ -6893,19 +7543,21 @@ | ||
| 6893 | 7543 | static void JimPrngSeed(Jim_Interp *interp, unsigned char *seed, int seedLen); |
| 6894 | 7544 | static void JimRandomBytes(Jim_Interp *interp, void *dest, unsigned int len); |
| 6895 | 7545 | static int JimSetNewVariable(Jim_HashTable *ht, Jim_Obj *nameObjPtr, Jim_VarVal *vv); |
| 6896 | 7546 | static Jim_VarVal *JimFindVariable(Jim_HashTable *ht, Jim_Obj *nameObjPtr); |
| 6897 | 7547 | static int SetVariableFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr); |
| 7548 | +static int JimCallNative(Jim_Interp *interp, Jim_Cmd *cmd, int argc, Jim_Obj *const *argv); | |
| 6898 | 7549 | |
| 6899 | 7550 | #define JIM_DICT_SUGAR 100 |
| 6900 | 7551 | |
| 6901 | 7552 | |
| 6902 | 7553 | |
| 6903 | 7554 | |
| 6904 | 7555 | #define JimWideValue(objPtr) (objPtr)->internalRep.wideValue |
| 6905 | 7556 | |
| 6906 | 7557 | #define JimObjTypeName(O) ((O)->typePtr ? (O)->typePtr->name : "none") |
| 7558 | + | |
| 6907 | 7559 | |
| 6908 | 7560 | static int utf8_tounicode_case(const char *s, int *uc, int upper) |
| 6909 | 7561 | { |
| 6910 | 7562 | int l = utf8_tounicode(s, uc); |
| 6911 | 7563 | if (upper) { |
| @@ -7111,11 +7763,11 @@ | ||
| 7111 | 7763 | static int JimStringFirst(const char *s1, int l1, const char *s2, int l2, int idx) |
| 7112 | 7764 | { |
| 7113 | 7765 | int i; |
| 7114 | 7766 | int l1bytelen; |
| 7115 | 7767 | |
| 7116 | - if (!l1 || !l2 || l1 > l2) { | |
| 7768 | + if (!l1 || !l2 || l1 > l2 || idx > l2) { | |
| 7117 | 7769 | return -1; |
| 7118 | 7770 | } |
| 7119 | 7771 | if (idx < 0) |
| 7120 | 7772 | idx = 0; |
| 7121 | 7773 | s2 += utf8_index(s2, idx); |
| @@ -7283,10 +7935,11 @@ | ||
| 7283 | 7935 | |
| 7284 | 7936 | *doublePtr = strtod(str, &endptr); |
| 7285 | 7937 | |
| 7286 | 7938 | return JimCheckConversion(str, endptr); |
| 7287 | 7939 | } |
| 7940 | + | |
| 7288 | 7941 | |
| 7289 | 7942 | static jim_wide JimPowWide(jim_wide b, jim_wide e) |
| 7290 | 7943 | { |
| 7291 | 7944 | jim_wide res = 1; |
| 7292 | 7945 | |
| @@ -7311,10 +7964,11 @@ | ||
| 7311 | 7964 | } |
| 7312 | 7965 | return res; |
| 7313 | 7966 | } |
| 7314 | 7967 | |
| 7315 | 7968 | #ifdef JIM_DEBUG_PANIC |
| 7969 | + | |
| 7316 | 7970 | static void JimPanicDump(int condition, const char *fmt, ...) |
| 7317 | 7971 | { |
| 7318 | 7972 | va_list ap; |
| 7319 | 7973 | |
| 7320 | 7974 | if (!condition) { |
| @@ -7447,10 +8101,11 @@ | ||
| 7447 | 8101 | ht->uniq = (rand() ^ time(NULL) ^ clock()); |
| 7448 | 8102 | #else |
| 7449 | 8103 | ht->uniq = 0; |
| 7450 | 8104 | #endif |
| 7451 | 8105 | } |
| 8106 | + | |
| 7452 | 8107 | |
| 7453 | 8108 | static void JimInitHashTableIterator(Jim_HashTable *ht, Jim_HashTableIterator *iter) |
| 7454 | 8109 | { |
| 7455 | 8110 | iter->ht = ht; |
| 7456 | 8111 | iter->index = -1; |
| @@ -7716,20 +8371,23 @@ | ||
| 7716 | 8371 | |
| 7717 | 8372 | static unsigned int JimStringCopyHTHashFunction(const void *key) |
| 7718 | 8373 | { |
| 7719 | 8374 | return Jim_GenHashFunction(key, strlen(key)); |
| 7720 | 8375 | } |
| 8376 | + | |
| 7721 | 8377 | |
| 7722 | 8378 | static void *JimStringCopyHTDup(void *privdata, const void *key) |
| 7723 | 8379 | { |
| 7724 | 8380 | return Jim_StrDup(key); |
| 7725 | 8381 | } |
| 8382 | + | |
| 7726 | 8383 | |
| 7727 | 8384 | static int JimStringCopyHTKeyCompare(void *privdata, const void *key1, const void *key2) |
| 7728 | 8385 | { |
| 7729 | 8386 | return strcmp(key1, key2) == 0; |
| 7730 | 8387 | } |
| 8388 | + | |
| 7731 | 8389 | |
| 7732 | 8390 | static void JimStringCopyHTKeyDestructor(void *privdata, void *key) |
| 7733 | 8391 | { |
| 7734 | 8392 | Jim_Free(key); |
| 7735 | 8393 | } |
| @@ -7746,10 +8404,11 @@ | ||
| 7746 | 8404 | typedef struct AssocDataValue |
| 7747 | 8405 | { |
| 7748 | 8406 | Jim_InterpDeleteProc *delProc; |
| 7749 | 8407 | void *data; |
| 7750 | 8408 | } AssocDataValue; |
| 8409 | + | |
| 7751 | 8410 | |
| 7752 | 8411 | static void JimAssocDataHashTableValueDestructor(void *privdata, void *data) |
| 7753 | 8412 | { |
| 7754 | 8413 | AssocDataValue *assocPtr = (AssocDataValue *) data; |
| 7755 | 8414 | |
| @@ -7765,25 +8424,28 @@ | ||
| 7765 | 8424 | JimStringCopyHTKeyCompare, |
| 7766 | 8425 | JimStringCopyHTKeyDestructor, |
| 7767 | 8426 | JimAssocDataHashTableValueDestructor |
| 7768 | 8427 | }; |
| 7769 | 8428 | |
| 7770 | -void Jim_InitStack(Jim_Stack *stack) | |
| 8429 | +void Jim_StackInit(Jim_Stack *stack, void (*freefunc) (void *ptr)) | |
| 7771 | 8430 | { |
| 7772 | 8431 | stack->len = 0; |
| 7773 | 8432 | stack->maxlen = 0; |
| 7774 | 8433 | stack->vector = NULL; |
| 8434 | + stack->freefunc = freefunc; | |
| 7775 | 8435 | } |
| 7776 | 8436 | |
| 7777 | -void Jim_FreeStack(Jim_Stack *stack) | |
| 7778 | -{ | |
| 7779 | - Jim_Free(stack->vector); | |
| 7780 | -} | |
| 7781 | - | |
| 7782 | -int Jim_StackLen(Jim_Stack *stack) | |
| 7783 | -{ | |
| 7784 | - return stack->len; | |
| 8437 | +void Jim_StackFree(Jim_Stack *stack) | |
| 8438 | +{ | |
| 8439 | + int i; | |
| 8440 | + | |
| 8441 | + if (stack->freefunc) { | |
| 8442 | + for (i = 0; i < stack->len; i++) { | |
| 8443 | + stack->freefunc(stack->vector[i]); | |
| 8444 | + } | |
| 8445 | + } | |
| 8446 | + Jim_Free(stack->vector); | |
| 7785 | 8447 | } |
| 7786 | 8448 | |
| 7787 | 8449 | void Jim_StackPush(Jim_Stack *stack, void *element) |
| 7788 | 8450 | { |
| 7789 | 8451 | int neededLen = stack->len + 1; |
| @@ -7802,25 +8464,10 @@ | ||
| 7802 | 8464 | return NULL; |
| 7803 | 8465 | stack->len--; |
| 7804 | 8466 | return stack->vector[stack->len]; |
| 7805 | 8467 | } |
| 7806 | 8468 | |
| 7807 | -void *Jim_StackPeek(Jim_Stack *stack) | |
| 7808 | -{ | |
| 7809 | - if (stack->len == 0) | |
| 7810 | - return NULL; | |
| 7811 | - return stack->vector[stack->len - 1]; | |
| 7812 | -} | |
| 7813 | - | |
| 7814 | -void Jim_FreeStackElements(Jim_Stack *stack, void (*freeFunc) (void *ptr)) | |
| 7815 | -{ | |
| 7816 | - int i; | |
| 7817 | - | |
| 7818 | - for (i = 0; i < stack->len; i++) | |
| 7819 | - freeFunc(stack->vector[i]); | |
| 7820 | -} | |
| 7821 | - | |
| 7822 | 8469 | |
| 7823 | 8470 | |
| 7824 | 8471 | #define JIM_TT_NONE 0 |
| 7825 | 8472 | #define JIM_TT_STR 1 |
| 7826 | 8473 | #define JIM_TT_ESC 2 |
| @@ -7901,10 +8548,11 @@ | ||
| 7901 | 8548 | pc->linenr = linenr; |
| 7902 | 8549 | pc->comment = 1; |
| 7903 | 8550 | pc->missing.ch = ' '; |
| 7904 | 8551 | pc->missing.line = linenr; |
| 7905 | 8552 | } |
| 8553 | + | |
| 7906 | 8554 | |
| 7907 | 8555 | static int JimParseScript(struct JimParserCtx *pc) |
| 7908 | 8556 | { |
| 7909 | 8557 | while (1) { |
| 7910 | 8558 | if (!pc->len) { |
| @@ -7962,10 +8610,11 @@ | ||
| 7962 | 8610 | return JimParseStr(pc); |
| 7963 | 8611 | } |
| 7964 | 8612 | return JIM_OK; |
| 7965 | 8613 | } |
| 7966 | 8614 | } |
| 8615 | + | |
| 7967 | 8616 | |
| 7968 | 8617 | static int JimParseSep(struct JimParserCtx *pc) |
| 7969 | 8618 | { |
| 7970 | 8619 | pc->tstart = pc->p; |
| 7971 | 8620 | pc->tline = pc->linenr; |
| @@ -7983,10 +8632,11 @@ | ||
| 7983 | 8632 | } |
| 7984 | 8633 | pc->tend = pc->p - 1; |
| 7985 | 8634 | pc->tt = JIM_TT_SEP; |
| 7986 | 8635 | return JIM_OK; |
| 7987 | 8636 | } |
| 8637 | + | |
| 7988 | 8638 | |
| 7989 | 8639 | static int JimParseEol(struct JimParserCtx *pc) |
| 7990 | 8640 | { |
| 7991 | 8641 | pc->tstart = pc->p; |
| 7992 | 8642 | pc->tline = pc->linenr; |
| @@ -8151,36 +8801,40 @@ | ||
| 8151 | 8801 | } |
| 8152 | 8802 | pc->missing.ch = '['; |
| 8153 | 8803 | pc->missing.line = line; |
| 8154 | 8804 | pc->tend = pc->p - 1; |
| 8155 | 8805 | } |
| 8806 | + | |
| 8156 | 8807 | |
| 8157 | 8808 | static int JimParseBrace(struct JimParserCtx *pc) |
| 8158 | 8809 | { |
| 8159 | 8810 | pc->tstart = pc->p + 1; |
| 8160 | 8811 | pc->tline = pc->linenr; |
| 8161 | 8812 | pc->tt = JIM_TT_STR; |
| 8162 | 8813 | JimParseSubBrace(pc); |
| 8163 | 8814 | return JIM_OK; |
| 8164 | 8815 | } |
| 8816 | + | |
| 8165 | 8817 | |
| 8166 | 8818 | static int JimParseCmd(struct JimParserCtx *pc) |
| 8167 | 8819 | { |
| 8168 | 8820 | pc->tstart = pc->p + 1; |
| 8169 | 8821 | pc->tline = pc->linenr; |
| 8170 | 8822 | pc->tt = JIM_TT_CMD; |
| 8171 | 8823 | JimParseSubCmd(pc); |
| 8172 | 8824 | return JIM_OK; |
| 8173 | 8825 | } |
| 8826 | + | |
| 8174 | 8827 | |
| 8175 | 8828 | static int JimParseQuote(struct JimParserCtx *pc) |
| 8176 | 8829 | { |
| 8177 | 8830 | pc->tstart = pc->p + 1; |
| 8178 | 8831 | pc->tline = pc->linenr; |
| 8179 | 8832 | pc->tt = JimParseSubQuote(pc); |
| 8180 | 8833 | return JIM_OK; |
| 8181 | 8834 | } |
| 8835 | + | |
| 8182 | 8836 | |
| 8183 | 8837 | static int JimParseVar(struct JimParserCtx *pc) |
| 8184 | 8838 | { |
| 8185 | 8839 | |
| 8186 | 8840 | pc->p++; |
| @@ -8278,10 +8932,11 @@ | ||
| 8278 | 8932 | pc->len++; |
| 8279 | 8933 | return JIM_ERR; |
| 8280 | 8934 | } |
| 8281 | 8935 | return JIM_OK; |
| 8282 | 8936 | } |
| 8937 | + | |
| 8283 | 8938 | |
| 8284 | 8939 | static int JimParseStr(struct JimParserCtx *pc) |
| 8285 | 8940 | { |
| 8286 | 8941 | if (pc->tt == JIM_TT_SEP || pc->tt == JIM_TT_EOL || |
| 8287 | 8942 | pc->tt == JIM_TT_NONE || pc->tt == JIM_TT_STR) { |
| @@ -8381,10 +9036,11 @@ | ||
| 8381 | 9036 | pc->p++; |
| 8382 | 9037 | pc->len--; |
| 8383 | 9038 | } |
| 8384 | 9039 | return JIM_OK; |
| 8385 | 9040 | } |
| 9041 | + | |
| 8386 | 9042 | |
| 8387 | 9043 | static int JimParseComment(struct JimParserCtx *pc) |
| 8388 | 9044 | { |
| 8389 | 9045 | while (*pc->p) { |
| 8390 | 9046 | if (*pc->p == '\\') { |
| @@ -8419,10 +9075,11 @@ | ||
| 8419 | 9075 | return c - 'a' + 10; |
| 8420 | 9076 | if (c >= 'A' && c <= 'F') |
| 8421 | 9077 | return c - 'A' + 10; |
| 8422 | 9078 | return -1; |
| 8423 | 9079 | } |
| 9080 | + | |
| 8424 | 9081 | |
| 8425 | 9082 | static int odigitval(int c) |
| 8426 | 9083 | { |
| 8427 | 9084 | if (c >= '0' && c <= '7') |
| 8428 | 9085 | return c - '0'; |
| @@ -8609,10 +9266,11 @@ | ||
| 8609 | 9266 | } |
| 8610 | 9267 | |
| 8611 | 9268 | static int JimParseListSep(struct JimParserCtx *pc); |
| 8612 | 9269 | static int JimParseListStr(struct JimParserCtx *pc); |
| 8613 | 9270 | static int JimParseListQuote(struct JimParserCtx *pc); |
| 9271 | + | |
| 8614 | 9272 | |
| 8615 | 9273 | static int JimParseList(struct JimParserCtx *pc) |
| 8616 | 9274 | { |
| 8617 | 9275 | if (isspace(UCHAR(*pc->p))) { |
| 8618 | 9276 | return JimParseListSep(pc); |
| @@ -8635,10 +9293,11 @@ | ||
| 8635 | 9293 | pc->tline = pc->linenr; |
| 8636 | 9294 | pc->tt = JIM_TT_EOL; |
| 8637 | 9295 | pc->eof = 1; |
| 8638 | 9296 | return JIM_OK; |
| 8639 | 9297 | } |
| 9298 | + | |
| 8640 | 9299 | |
| 8641 | 9300 | static int JimParseListSep(struct JimParserCtx *pc) |
| 8642 | 9301 | { |
| 8643 | 9302 | pc->tstart = pc->p; |
| 8644 | 9303 | pc->tline = pc->linenr; |
| @@ -8651,10 +9310,11 @@ | ||
| 8651 | 9310 | } |
| 8652 | 9311 | pc->tend = pc->p - 1; |
| 8653 | 9312 | pc->tt = JIM_TT_SEP; |
| 8654 | 9313 | return JIM_OK; |
| 8655 | 9314 | } |
| 9315 | + | |
| 8656 | 9316 | |
| 8657 | 9317 | static int JimParseListQuote(struct JimParserCtx *pc) |
| 8658 | 9318 | { |
| 8659 | 9319 | pc->p++; |
| 8660 | 9320 | pc->len--; |
| @@ -8688,10 +9348,11 @@ | ||
| 8688 | 9348 | } |
| 8689 | 9349 | |
| 8690 | 9350 | pc->tend = pc->p - 1; |
| 8691 | 9351 | return JIM_OK; |
| 8692 | 9352 | } |
| 9353 | + | |
| 8693 | 9354 | |
| 8694 | 9355 | static int JimParseListStr(struct JimParserCtx *pc) |
| 8695 | 9356 | { |
| 8696 | 9357 | pc->tstart = pc->p; |
| 8697 | 9358 | pc->tline = pc->linenr; |
| @@ -8734,10 +9395,11 @@ | ||
| 8734 | 9395 | |
| 8735 | 9396 | objPtr = Jim_Alloc(sizeof(*objPtr)); |
| 8736 | 9397 | } |
| 8737 | 9398 | |
| 8738 | 9399 | objPtr->refCount = 0; |
| 9400 | + objPtr->taint = interp->taint; | |
| 8739 | 9401 | |
| 8740 | 9402 | |
| 8741 | 9403 | objPtr->prevObjPtr = NULL; |
| 8742 | 9404 | objPtr->nextObjPtr = interp->liveList; |
| 8743 | 9405 | if (interp->liveList) |
| @@ -8794,10 +9456,12 @@ | ||
| 8794 | 9456 | Jim_Obj *Jim_DuplicateObj(Jim_Interp *interp, Jim_Obj *objPtr) |
| 8795 | 9457 | { |
| 8796 | 9458 | Jim_Obj *dupPtr; |
| 8797 | 9459 | |
| 8798 | 9460 | dupPtr = Jim_NewObj(interp); |
| 9461 | + dupPtr->taint = objPtr->taint; | |
| 9462 | + | |
| 8799 | 9463 | if (objPtr->bytes == NULL) { |
| 8800 | 9464 | |
| 8801 | 9465 | dupPtr->bytes = NULL; |
| 8802 | 9466 | } |
| 8803 | 9467 | else if (objPtr->length == 0) { |
| @@ -8856,10 +9520,11 @@ | ||
| 8856 | 9520 | |
| 8857 | 9521 | Jim_GetString(objPtr, NULL); |
| 8858 | 9522 | } |
| 8859 | 9523 | return objPtr->bytes; |
| 8860 | 9524 | } |
| 9525 | + | |
| 8861 | 9526 | |
| 8862 | 9527 | static void JimSetStringBytes(Jim_Obj *objPtr, const char *str) |
| 8863 | 9528 | { |
| 8864 | 9529 | objPtr->bytes = Jim_StrDup(str); |
| 8865 | 9530 | objPtr->length = strlen(str); |
| @@ -8884,15 +9549,17 @@ | ||
| 8884 | 9549 | FreeInterpolatedInternalRep, |
| 8885 | 9550 | DupInterpolatedInternalRep, |
| 8886 | 9551 | NULL, |
| 8887 | 9552 | JIM_TYPE_NONE, |
| 8888 | 9553 | }; |
| 9554 | + | |
| 8889 | 9555 | |
| 8890 | 9556 | static void FreeInterpolatedInternalRep(Jim_Interp *interp, Jim_Obj *objPtr) |
| 8891 | 9557 | { |
| 8892 | 9558 | Jim_DecrRefCount(interp, objPtr->internalRep.dictSubstValue.indexObjPtr); |
| 8893 | 9559 | } |
| 9560 | + | |
| 8894 | 9561 | |
| 8895 | 9562 | static void DupInterpolatedInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr) |
| 8896 | 9563 | { |
| 8897 | 9564 | |
| 8898 | 9565 | dupPtr->internalRep = srcPtr->internalRep; |
| @@ -8908,18 +9575,20 @@ | ||
| 8908 | 9575 | NULL, |
| 8909 | 9576 | DupStringInternalRep, |
| 8910 | 9577 | NULL, |
| 8911 | 9578 | JIM_TYPE_REFERENCES, |
| 8912 | 9579 | }; |
| 9580 | + | |
| 8913 | 9581 | |
| 8914 | 9582 | static void DupStringInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr) |
| 8915 | 9583 | { |
| 8916 | 9584 | JIM_NOTUSED(interp); |
| 8917 | 9585 | |
| 8918 | 9586 | dupPtr->internalRep.strValue.maxLength = srcPtr->length; |
| 8919 | 9587 | dupPtr->internalRep.strValue.charLength = srcPtr->internalRep.strValue.charLength; |
| 8920 | 9588 | } |
| 9589 | + | |
| 8921 | 9590 | |
| 8922 | 9591 | static int SetStringFromAny(Jim_Interp *interp, Jim_Obj *objPtr) |
| 8923 | 9592 | { |
| 8924 | 9593 | if (objPtr->typePtr != &stringObjType) { |
| 8925 | 9594 | |
| @@ -9047,10 +9716,11 @@ | ||
| 9047 | 9716 | void Jim_AppendObj(Jim_Interp *interp, Jim_Obj *objPtr, Jim_Obj *appendObjPtr) |
| 9048 | 9717 | { |
| 9049 | 9718 | int len; |
| 9050 | 9719 | const char *str = Jim_GetString(appendObjPtr, &len); |
| 9051 | 9720 | Jim_AppendString(interp, objPtr, str, len); |
| 9721 | + objPtr->taint |= appendObjPtr->taint; | |
| 9052 | 9722 | } |
| 9053 | 9723 | |
| 9054 | 9724 | void Jim_AppendStrings(Jim_Interp *interp, Jim_Obj *objPtr, ...) |
| 9055 | 9725 | { |
| 9056 | 9726 | va_list ap; |
| @@ -9075,11 +9745,11 @@ | ||
| 9075 | 9745 | else { |
| 9076 | 9746 | int Alen, Blen; |
| 9077 | 9747 | const char *sA = Jim_GetString(aObjPtr, &Alen); |
| 9078 | 9748 | const char *sB = Jim_GetString(bObjPtr, &Blen); |
| 9079 | 9749 | |
| 9080 | - return Alen == Blen && memcmp(sA, sB, Alen) == 0; | |
| 9750 | + return Alen == Blen && *sA == *sB && memcmp(sA, sB, Alen) == 0; | |
| 9081 | 9751 | } |
| 9082 | 9752 | } |
| 9083 | 9753 | |
| 9084 | 9754 | int Jim_StringMatchObj(Jim_Interp *interp, Jim_Obj *patternObjPtr, Jim_Obj *objPtr, int nocase) |
| 9085 | 9755 | { |
| @@ -9128,10 +9798,11 @@ | ||
| 9128 | 9798 | if (rangeLen < 0) |
| 9129 | 9799 | rangeLen = 0; |
| 9130 | 9800 | |
| 9131 | 9801 | *rangeLenPtr = rangeLen; |
| 9132 | 9802 | } |
| 9803 | + | |
| 9133 | 9804 | |
| 9134 | 9805 | static int JimStringGetRange(Jim_Interp *interp, Jim_Obj *firstObjPtr, Jim_Obj *lastObjPtr, |
| 9135 | 9806 | int len, int *first, int *last, int *range) |
| 9136 | 9807 | { |
| 9137 | 9808 | if (Jim_GetIndex(interp, firstObjPtr, first) != JIM_OK) { |
| @@ -9349,10 +10020,11 @@ | ||
| 9349 | 10020 | } |
| 9350 | 10021 | |
| 9351 | 10022 | static const char default_trim_chars[] = " \t\n\r"; |
| 9352 | 10023 | |
| 9353 | 10024 | static int default_trim_chars_len = sizeof(default_trim_chars); |
| 10025 | + | |
| 9354 | 10026 | |
| 9355 | 10027 | static Jim_Obj *JimStringTrimLeft(Jim_Interp *interp, Jim_Obj *strObjPtr, Jim_Obj *trimcharsObjPtr) |
| 9356 | 10028 | { |
| 9357 | 10029 | int len; |
| 9358 | 10030 | const char *str = Jim_GetString(strObjPtr, &len); |
| @@ -9369,10 +10041,11 @@ | ||
| 9369 | 10041 | return strObjPtr; |
| 9370 | 10042 | } |
| 9371 | 10043 | |
| 9372 | 10044 | return Jim_NewStringObj(interp, newstr, len - (newstr - str)); |
| 9373 | 10045 | } |
| 10046 | + | |
| 9374 | 10047 | |
| 9375 | 10048 | static Jim_Obj *JimStringTrimRight(Jim_Interp *interp, Jim_Obj *strObjPtr, Jim_Obj *trimcharsObjPtr) |
| 9376 | 10049 | { |
| 9377 | 10050 | int len; |
| 9378 | 10051 | const char *trimchars = default_trim_chars; |
| @@ -9406,10 +10079,11 @@ | ||
| 9406 | 10079 | strObjPtr->length = (nontrim - strObjPtr->bytes); |
| 9407 | 10080 | } |
| 9408 | 10081 | |
| 9409 | 10082 | return strObjPtr; |
| 9410 | 10083 | } |
| 10084 | + | |
| 9411 | 10085 | |
| 9412 | 10086 | static Jim_Obj *JimStringTrim(Jim_Interp *interp, Jim_Obj *strObjPtr, Jim_Obj *trimcharsObjPtr) |
| 9413 | 10087 | { |
| 9414 | 10088 | |
| 9415 | 10089 | Jim_Obj *objPtr = JimStringTrimLeft(interp, strObjPtr, trimcharsObjPtr); |
| @@ -9428,15 +10102,17 @@ | ||
| 9428 | 10102 | |
| 9429 | 10103 | |
| 9430 | 10104 | #ifdef HAVE_ISASCII |
| 9431 | 10105 | #define jim_isascii isascii |
| 9432 | 10106 | #else |
| 10107 | + | |
| 9433 | 10108 | static int jim_isascii(int c) |
| 9434 | 10109 | { |
| 9435 | 10110 | return !(c & ~0x7f); |
| 9436 | 10111 | } |
| 9437 | 10112 | #endif |
| 10113 | + | |
| 9438 | 10114 | |
| 9439 | 10115 | static int JimStringIs(Jim_Interp *interp, Jim_Obj *strObjPtr, Jim_Obj *strClass, int strict) |
| 9440 | 10116 | { |
| 9441 | 10117 | static const char * const strclassnames[] = { |
| 9442 | 10118 | "integer", "alpha", "alnum", "ascii", "digit", |
| @@ -9538,16 +10214,27 @@ | ||
| 9538 | 10214 | } |
| 9539 | 10215 | objPtr->internalRep.ptr = (char *)str; |
| 9540 | 10216 | return 1; |
| 9541 | 10217 | } |
| 9542 | 10218 | } |
| 10219 | + | |
| 10220 | + | |
| 9543 | 10221 | |
| 9544 | 10222 | static int qsortCompareStringPointers(const void *a, const void *b) |
| 9545 | 10223 | { |
| 9546 | 10224 | char *const *sa = (char *const *)a; |
| 9547 | 10225 | char *const *sb = (char *const *)b; |
| 9548 | 10226 | |
| 10227 | + | |
| 10228 | + if (strcmp(*sa, "--") == 0) { | |
| 10229 | + return 1; | |
| 10230 | + } | |
| 10231 | + if (strcmp(*sb, "--") == 0) { | |
| 10232 | + | |
| 10233 | + return -1; | |
| 10234 | + } | |
| 10235 | + | |
| 9549 | 10236 | return strcmp(*sa, *sb); |
| 9550 | 10237 | } |
| 9551 | 10238 | |
| 9552 | 10239 | |
| 9553 | 10240 | |
| @@ -9578,10 +10265,11 @@ | ||
| 9578 | 10265 | NULL, |
| 9579 | 10266 | NULL, |
| 9580 | 10267 | NULL, |
| 9581 | 10268 | JIM_NONE, |
| 9582 | 10269 | }; |
| 10270 | + | |
| 9583 | 10271 | |
| 9584 | 10272 | static Jim_Obj *JimNewScriptLineObj(Jim_Interp *interp, int argc, int line) |
| 9585 | 10273 | { |
| 9586 | 10274 | Jim_Obj *objPtr; |
| 9587 | 10275 | |
| @@ -9672,17 +10360,19 @@ | ||
| 9672 | 10360 | ParseToken *list; |
| 9673 | 10361 | int size; |
| 9674 | 10362 | int count; |
| 9675 | 10363 | ParseToken static_list[20]; |
| 9676 | 10364 | } ParseTokenList; |
| 10365 | + | |
| 9677 | 10366 | |
| 9678 | 10367 | static void ScriptTokenListInit(ParseTokenList *tokenlist) |
| 9679 | 10368 | { |
| 9680 | 10369 | tokenlist->list = tokenlist->static_list; |
| 9681 | 10370 | tokenlist->size = sizeof(tokenlist->static_list) / sizeof(ParseToken); |
| 9682 | 10371 | tokenlist->count = 0; |
| 9683 | 10372 | } |
| 10373 | + | |
| 9684 | 10374 | |
| 9685 | 10375 | static void ScriptTokenListFree(ParseTokenList *tokenlist) |
| 9686 | 10376 | { |
| 9687 | 10377 | if (tokenlist->list != tokenlist->static_list) { |
| 9688 | 10378 | Jim_Free(tokenlist->list); |
| @@ -9970,10 +10660,11 @@ | ||
| 9970 | 10660 | struct JimParserCtx parser; |
| 9971 | 10661 | struct ScriptObj *script; |
| 9972 | 10662 | ParseTokenList tokenlist; |
| 9973 | 10663 | Jim_Obj *fileNameObj; |
| 9974 | 10664 | int line; |
| 10665 | + int oldtaint; | |
| 9975 | 10666 | |
| 9976 | 10667 | |
| 9977 | 10668 | fileNameObj = Jim_GetSourceInfo(interp, objPtr, &line); |
| 9978 | 10669 | |
| 9979 | 10670 | |
| @@ -9988,10 +10679,15 @@ | ||
| 9988 | 10679 | |
| 9989 | 10680 | |
| 9990 | 10681 | ScriptAddToken(&tokenlist, scriptText + scriptTextLen, 0, JIM_TT_EOF, 0); |
| 9991 | 10682 | |
| 9992 | 10683 | |
| 10684 | + | |
| 10685 | + | |
| 10686 | + oldtaint = interp->taint; | |
| 10687 | + interp->taint = objPtr->taint; | |
| 10688 | + | |
| 9993 | 10689 | script = Jim_Alloc(sizeof(*script)); |
| 9994 | 10690 | memset(script, 0, sizeof(*script)); |
| 9995 | 10691 | script->inUse = 1; |
| 9996 | 10692 | script->fileNameObj = fileNameObj; |
| 9997 | 10693 | Jim_IncrRefCount(script->fileNameObj); |
| @@ -10005,10 +10701,12 @@ | ||
| 10005 | 10701 | |
| 10006 | 10702 | |
| 10007 | 10703 | Jim_FreeIntRep(interp, objPtr); |
| 10008 | 10704 | Jim_SetIntRepPtr(objPtr, script); |
| 10009 | 10705 | objPtr->typePtr = &scriptObjType; |
| 10706 | + | |
| 10707 | + interp->taint = oldtaint; | |
| 10010 | 10708 | } |
| 10011 | 10709 | |
| 10012 | 10710 | static ScriptObj *JimGetScript(Jim_Interp *interp, Jim_Obj *objPtr) |
| 10013 | 10711 | { |
| 10014 | 10712 | if (objPtr == interp->emptyObj) { |
| @@ -10033,20 +10731,22 @@ | ||
| 10033 | 10731 | Jim_Free(interp->oldCmdCache); |
| 10034 | 10732 | interp->oldCmdCache = next; |
| 10035 | 10733 | } |
| 10036 | 10734 | interp->oldCmdCacheSize = 0; |
| 10037 | 10735 | } |
| 10736 | + | |
| 10038 | 10737 | |
| 10039 | 10738 | static void JimIncrCmdRefCount(Jim_Cmd *cmdPtr) |
| 10040 | 10739 | { |
| 10041 | 10740 | cmdPtr->inUse++; |
| 10042 | 10741 | } |
| 10742 | + | |
| 10043 | 10743 | |
| 10044 | 10744 | static void JimDecrCmdRefCount(Jim_Interp *interp, Jim_Cmd *cmdPtr) |
| 10045 | 10745 | { |
| 10046 | 10746 | if (--cmdPtr->inUse == 0) { |
| 10047 | - if (cmdPtr->isproc) { | |
| 10747 | + if (cmdPtr->flags & JIM_CMD_ISPROC) { | |
| 10048 | 10748 | Jim_DecrRefCount(interp, cmdPtr->u.proc.argListObjPtr); |
| 10049 | 10749 | Jim_DecrRefCount(interp, cmdPtr->u.proc.bodyObjPtr); |
| 10050 | 10750 | Jim_DecrRefCount(interp, cmdPtr->u.proc.nsObj); |
| 10051 | 10751 | if (cmdPtr->u.proc.staticVars) { |
| 10052 | 10752 | Jim_FreeHashTable(cmdPtr->u.proc.staticVars); |
| @@ -10074,10 +10774,11 @@ | ||
| 10074 | 10774 | |
| 10075 | 10775 | static void JimIncrVarRef(Jim_VarVal *vv) |
| 10076 | 10776 | { |
| 10077 | 10777 | vv->refCount++; |
| 10078 | 10778 | } |
| 10779 | + | |
| 10079 | 10780 | |
| 10080 | 10781 | static void JimDecrVarRef(Jim_Interp *interp, Jim_VarVal *vv) |
| 10081 | 10782 | { |
| 10082 | 10783 | assert(vv->refCount > 0); |
| 10083 | 10784 | if (--vv->refCount == 0) { |
| @@ -10085,15 +10786,17 @@ | ||
| 10085 | 10786 | Jim_DecrRefCount(interp, vv->objPtr); |
| 10086 | 10787 | } |
| 10087 | 10788 | Jim_Free(vv); |
| 10088 | 10789 | } |
| 10089 | 10790 | } |
| 10791 | + | |
| 10090 | 10792 | |
| 10091 | 10793 | static void JimVariablesHTValDestructor(void *interp, void *val) |
| 10092 | 10794 | { |
| 10093 | 10795 | JimDecrVarRef(interp, val); |
| 10094 | 10796 | } |
| 10797 | + | |
| 10095 | 10798 | |
| 10096 | 10799 | static unsigned int JimObjectHTHashFunction(const void *key) |
| 10097 | 10800 | { |
| 10098 | 10801 | Jim_Obj *keyObj = (Jim_Obj *)key; |
| 10099 | 10802 | int length; |
| @@ -10125,10 +10828,11 @@ | ||
| 10125 | 10828 | } |
| 10126 | 10829 | #endif |
| 10127 | 10830 | string = Jim_GetString(keyObj, &length); |
| 10128 | 10831 | return Jim_GenHashFunction((const unsigned char *)string, length); |
| 10129 | 10832 | } |
| 10833 | + | |
| 10130 | 10834 | |
| 10131 | 10835 | static int JimObjectHTKeyCompare(void *privdata, const void *key1, const void *key2) |
| 10132 | 10836 | { |
| 10133 | 10837 | return Jim_StringEqObj((Jim_Obj *)key1, (Jim_Obj *)key2); |
| 10134 | 10838 | } |
| @@ -10136,10 +10840,11 @@ | ||
| 10136 | 10840 | static void *JimObjectHTKeyValDup(void *privdata, const void *val) |
| 10137 | 10841 | { |
| 10138 | 10842 | Jim_IncrRefCount((Jim_Obj *)val); |
| 10139 | 10843 | return (void *)val; |
| 10140 | 10844 | } |
| 10845 | + | |
| 10141 | 10846 | |
| 10142 | 10847 | static void JimObjectHTKeyValDestructor(void *interp, void *val) |
| 10143 | 10848 | { |
| 10144 | 10849 | Jim_DecrRefCount(interp, (Jim_Obj *)val); |
| 10145 | 10850 | } |
| @@ -10172,25 +10877,28 @@ | ||
| 10172 | 10877 | } |
| 10173 | 10878 | } |
| 10174 | 10879 | *length = len; |
| 10175 | 10880 | return str; |
| 10176 | 10881 | } |
| 10882 | + | |
| 10177 | 10883 | |
| 10178 | 10884 | static unsigned int JimCommandsHT_HashFunction(const void *key) |
| 10179 | 10885 | { |
| 10180 | 10886 | int len; |
| 10181 | 10887 | const char *str = Jim_GetStringNoQualifier((Jim_Obj *)key, &len); |
| 10182 | 10888 | return Jim_GenHashFunction((const unsigned char *)str, len); |
| 10183 | 10889 | } |
| 10890 | + | |
| 10184 | 10891 | |
| 10185 | 10892 | static int JimCommandsHT_KeyCompare(void *privdata, const void *key1, const void *key2) |
| 10186 | 10893 | { |
| 10187 | 10894 | int len1, len2; |
| 10188 | 10895 | const char *str1 = Jim_GetStringNoQualifier((Jim_Obj *)key1, &len1); |
| 10189 | 10896 | const char *str2 = Jim_GetStringNoQualifier((Jim_Obj *)key2, &len2); |
| 10190 | - return len1 == len2 && memcmp(str1, str2, len1) == 0; | |
| 10897 | + return len1 == len2 && *str1 == *str2 && memcmp(str1, str2, len1) == 0; | |
| 10191 | 10898 | } |
| 10899 | + | |
| 10192 | 10900 | |
| 10193 | 10901 | static void JimCommandsHT_ValDestructor(void *interp, void *val) |
| 10194 | 10902 | { |
| 10195 | 10903 | JimDecrCmdRefCount(interp, val); |
| 10196 | 10904 | } |
| @@ -10262,35 +10970,50 @@ | ||
| 10262 | 10970 | |
| 10263 | 10971 | |
| 10264 | 10972 | Jim_ReplaceHashEntry(&interp->commands, nameObjPtr, cmd); |
| 10265 | 10973 | } |
| 10266 | 10974 | |
| 10267 | -int Jim_CreateCommandObj(Jim_Interp *interp, Jim_Obj *cmdNameObj, | |
| 10268 | - Jim_CmdProc *cmdProc, void *privData, Jim_DelCmdProc *delProc) | |
| 10975 | + | |
| 10976 | +Jim_Cmd *Jim_RegisterCommand(Jim_Interp *interp, Jim_Obj *cmdNameObj, | |
| 10977 | + Jim_CmdProc *cmdProc, | |
| 10978 | + Jim_DelCmdProc *delProc, | |
| 10979 | + const char *usage, | |
| 10980 | + const char *help, | |
| 10981 | + short minargs, | |
| 10982 | + short maxargs, | |
| 10983 | + int flags, | |
| 10984 | + void *privData) | |
| 10269 | 10985 | { |
| 10986 | + JimPanic(((flags & JIM_CMD_ISPROC), "Jim_RegisterCommand called with JIM_CMD_ISPROC flag")); | |
| 10270 | 10987 | Jim_Cmd *cmdPtr = Jim_Alloc(sizeof(*cmdPtr)); |
| 10271 | 10988 | |
| 10272 | 10989 | |
| 10273 | 10990 | memset(cmdPtr, 0, sizeof(*cmdPtr)); |
| 10274 | 10991 | cmdPtr->inUse = 1; |
| 10992 | + cmdPtr->flags = flags; | |
| 10275 | 10993 | cmdPtr->u.native.delProc = delProc; |
| 10276 | 10994 | cmdPtr->u.native.cmdProc = cmdProc; |
| 10995 | + cmdPtr->u.native.usage = usage; | |
| 10996 | + cmdPtr->u.native.help = help; | |
| 10997 | + cmdPtr->u.native.minargs = minargs; | |
| 10998 | + cmdPtr->u.native.maxargs = maxargs; | |
| 10277 | 10999 | cmdPtr->u.native.privData = privData; |
| 10278 | 11000 | |
| 10279 | 11001 | Jim_IncrRefCount(cmdNameObj); |
| 10280 | 11002 | JimCreateCommand(interp, cmdNameObj, cmdPtr); |
| 10281 | 11003 | Jim_DecrRefCount(interp, cmdNameObj); |
| 10282 | 11004 | |
| 10283 | - return JIM_OK; | |
| 11005 | + return cmdPtr; | |
| 10284 | 11006 | } |
| 10285 | - | |
| 10286 | 11007 | |
| 10287 | 11008 | int Jim_CreateCommand(Jim_Interp *interp, const char *cmdNameStr, |
| 10288 | 11009 | Jim_CmdProc *cmdProc, void *privData, Jim_DelCmdProc *delProc) |
| 10289 | 11010 | { |
| 10290 | - return Jim_CreateCommandObj(interp, Jim_NewStringObj(interp, cmdNameStr, -1), cmdProc, privData, delProc); | |
| 11011 | + Jim_RegisterCmd(interp, cmdNameStr, NULL, 0, -1, cmdProc, delProc, privData, 0); | |
| 11012 | + return JIM_OK; | |
| 10291 | 11013 | } |
| 11014 | + | |
| 10292 | 11015 | |
| 10293 | 11016 | static int JimCreateProcedureStatics(Jim_Interp *interp, Jim_Cmd *cmdPtr, Jim_Obj *staticsListObjPtr) |
| 10294 | 11017 | { |
| 10295 | 11018 | int len, i; |
| 10296 | 11019 | |
| @@ -10347,10 +11070,15 @@ | ||
| 10347 | 11070 | if (byref) { |
| 10348 | 11071 | vv = nameObjPtr->internalRep.varValue.vv; |
| 10349 | 11072 | } |
| 10350 | 11073 | else { |
| 10351 | 11074 | initObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_NONE); |
| 11075 | + if (!initObjPtr) { | |
| 11076 | + Jim_SetResultFormatted(interp, "Could not resolve upvar \"%#s\"'s value", nameObjPtr); | |
| 11077 | + Jim_DecrRefCount(interp, nameObjPtr); | |
| 11078 | + return JIM_ERR; | |
| 11079 | + } | |
| 10352 | 11080 | } |
| 10353 | 11081 | break; |
| 10354 | 11082 | |
| 10355 | 11083 | case JIM_ERR: |
| 10356 | 11084 | |
| @@ -10402,11 +11130,11 @@ | ||
| 10402 | 11130 | #endif |
| 10403 | 11131 | |
| 10404 | 11132 | static void JimUpdateProcNamespace(Jim_Interp *interp, Jim_Cmd *cmdPtr, Jim_Obj *nameObjPtr) |
| 10405 | 11133 | { |
| 10406 | 11134 | #ifdef jim_ext_namespace |
| 10407 | - if (cmdPtr->isproc) { | |
| 11135 | + if (cmdPtr->flags & JIM_CMD_ISPROC) { | |
| 10408 | 11136 | int len; |
| 10409 | 11137 | const char *cmdname = Jim_GetStringNoQualifier(nameObjPtr, &len); |
| 10410 | 11138 | |
| 10411 | 11139 | const char *pt = Jim_memrchr(cmdname, ':', len); |
| 10412 | 11140 | if (pt && pt != cmdname && pt[-1] == ':') { |
| @@ -10438,11 +11166,11 @@ | ||
| 10438 | 11166 | |
| 10439 | 11167 | cmdPtr = Jim_Alloc(sizeof(*cmdPtr) + sizeof(struct Jim_ProcArg) * argListLen); |
| 10440 | 11168 | assert(cmdPtr); |
| 10441 | 11169 | memset(cmdPtr, 0, sizeof(*cmdPtr)); |
| 10442 | 11170 | cmdPtr->inUse = 1; |
| 10443 | - cmdPtr->isproc = 1; | |
| 11171 | + cmdPtr->flags = JIM_CMD_ISPROC; | |
| 10444 | 11172 | cmdPtr->u.proc.argListObjPtr = argListObjPtr; |
| 10445 | 11173 | cmdPtr->u.proc.argListLen = argListLen; |
| 10446 | 11174 | cmdPtr->u.proc.bodyObjPtr = bodyObjPtr; |
| 10447 | 11175 | cmdPtr->u.proc.argsPos = -1; |
| 10448 | 11176 | cmdPtr->u.proc.arglist = (struct Jim_ProcArg *)(cmdPtr + 1); |
| @@ -10581,10 +11309,11 @@ | ||
| 10581 | 11309 | |
| 10582 | 11310 | static void FreeCommandInternalRep(Jim_Interp *interp, Jim_Obj *objPtr) |
| 10583 | 11311 | { |
| 10584 | 11312 | Jim_DecrRefCount(interp, objPtr->internalRep.cmdValue.nsObj); |
| 10585 | 11313 | } |
| 11314 | + | |
| 10586 | 11315 | |
| 10587 | 11316 | static void DupCommandInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr) |
| 10588 | 11317 | { |
| 10589 | 11318 | dupPtr->internalRep.cmdValue = srcPtr->internalRep.cmdValue; |
| 10590 | 11319 | dupPtr->typePtr = srcPtr->typePtr; |
| @@ -10638,11 +11367,11 @@ | ||
| 10638 | 11367 | objPtr->internalRep.cmdValue.cmdPtr = cmd; |
| 10639 | 11368 | objPtr->internalRep.cmdValue.nsObj = interp->framePtr->nsObj; |
| 10640 | 11369 | Jim_IncrRefCount(interp->framePtr->nsObj); |
| 10641 | 11370 | Jim_DecrRefCount(interp, qualifiedNameObj); |
| 10642 | 11371 | } |
| 10643 | - while (cmd->u.proc.upcall) { | |
| 11372 | + while ((cmd->flags & JIM_CMD_ISPROC) && cmd->u.proc.upcall) { | |
| 10644 | 11373 | cmd = cmd->prevCmd; |
| 10645 | 11374 | } |
| 10646 | 11375 | return cmd; |
| 10647 | 11376 | } |
| 10648 | 11377 | |
| @@ -10720,30 +11449,34 @@ | ||
| 10720 | 11449 | return JIM_OK; |
| 10721 | 11450 | } |
| 10722 | 11451 | |
| 10723 | 11452 | |
| 10724 | 11453 | static int JimDictSugarSet(Jim_Interp *interp, Jim_Obj *ObjPtr, Jim_Obj *valObjPtr); |
| 10725 | -static Jim_Obj *JimDictSugarGet(Jim_Interp *interp, Jim_Obj *ObjPtr, int flags); | |
| 11454 | +static Jim_Obj *JimDictSugarGet(Jim_Interp *interp, Jim_Obj *objPtr, int flags); | |
| 11455 | + | |
| 10726 | 11456 | |
| 10727 | 11457 | static int JimSetNewVariable(Jim_HashTable *ht, Jim_Obj *nameObjPtr, Jim_VarVal *vv) |
| 10728 | 11458 | { |
| 10729 | 11459 | return Jim_AddHashEntry(ht, nameObjPtr, vv); |
| 10730 | 11460 | } |
| 11461 | + | |
| 10731 | 11462 | |
| 10732 | 11463 | static Jim_VarVal *JimFindVariable(Jim_HashTable *ht, Jim_Obj *nameObjPtr) |
| 10733 | 11464 | { |
| 10734 | 11465 | Jim_HashEntry *he = Jim_FindHashEntry(ht, nameObjPtr); |
| 10735 | 11466 | if (he) { |
| 10736 | 11467 | return (Jim_VarVal *)Jim_GetHashEntryVal(he); |
| 10737 | 11468 | } |
| 10738 | 11469 | return NULL; |
| 10739 | 11470 | } |
| 11471 | + | |
| 10740 | 11472 | |
| 10741 | 11473 | static int JimUnsetVariable(Jim_HashTable *ht, Jim_Obj *nameObjPtr) |
| 10742 | 11474 | { |
| 10743 | 11475 | return Jim_DeleteHashEntry(ht, nameObjPtr); |
| 10744 | 11476 | } |
| 11477 | + | |
| 10745 | 11478 | |
| 10746 | 11479 | static Jim_VarVal *JimCreateVariable(Jim_Interp *interp, Jim_Obj *nameObjPtr, Jim_Obj *valObjPtr) |
| 10747 | 11480 | { |
| 10748 | 11481 | const char *name; |
| 10749 | 11482 | Jim_CallFrame *framePtr; |
| @@ -10784,19 +11517,22 @@ | ||
| 10784 | 11517 | return vv; |
| 10785 | 11518 | } |
| 10786 | 11519 | |
| 10787 | 11520 | int Jim_SetVariable(Jim_Interp *interp, Jim_Obj *nameObjPtr, Jim_Obj *valObjPtr) |
| 10788 | 11521 | { |
| 10789 | - int err; | |
| 11522 | + int ret = JIM_OK; | |
| 10790 | 11523 | Jim_VarVal *vv; |
| 10791 | 11524 | |
| 10792 | 11525 | switch (SetVariableFromAny(interp, nameObjPtr)) { |
| 10793 | 11526 | case JIM_DICT_SUGAR: |
| 10794 | - return JimDictSugarSet(interp, nameObjPtr, valObjPtr); | |
| 11527 | + ret = JimDictSugarSet(interp, nameObjPtr, valObjPtr); | |
| 11528 | + break; | |
| 10795 | 11529 | |
| 10796 | 11530 | case JIM_ERR: |
| 10797 | - JimCreateVariable(interp, nameObjPtr, valObjPtr); | |
| 11531 | + if (JimCreateVariable(interp, nameObjPtr, valObjPtr) == NULL) { | |
| 11532 | + ret = JIM_ERR; | |
| 11533 | + } | |
| 10798 | 11534 | break; |
| 10799 | 11535 | |
| 10800 | 11536 | case JIM_OK: |
| 10801 | 11537 | vv = nameObjPtr->internalRep.varValue.vv; |
| 10802 | 11538 | if (vv->linkFramePtr == NULL) { |
| @@ -10807,17 +11543,20 @@ | ||
| 10807 | 11543 | else { |
| 10808 | 11544 | Jim_CallFrame *savedCallFrame; |
| 10809 | 11545 | |
| 10810 | 11546 | savedCallFrame = interp->framePtr; |
| 10811 | 11547 | interp->framePtr = vv->linkFramePtr; |
| 10812 | - err = Jim_SetVariable(interp, vv->objPtr, valObjPtr); | |
| 11548 | + ret = Jim_SetVariable(interp, vv->objPtr, valObjPtr); | |
| 10813 | 11549 | interp->framePtr = savedCallFrame; |
| 10814 | - if (err != JIM_OK) | |
| 10815 | - return err; | |
| 10816 | 11550 | } |
| 11551 | + break; | |
| 10817 | 11552 | } |
| 10818 | - return JIM_OK; | |
| 11553 | + if (ret != JIM_OK && valObjPtr->refCount == 0) { | |
| 11554 | + | |
| 11555 | + Jim_FreeNewObj(interp, valObjPtr); | |
| 11556 | + } | |
| 11557 | + return ret; | |
| 10819 | 11558 | } |
| 10820 | 11559 | |
| 10821 | 11560 | int Jim_SetVariableStr(Jim_Interp *interp, const char *name, Jim_Obj *objPtr) |
| 10822 | 11561 | { |
| 10823 | 11562 | Jim_Obj *nameObjPtr; |
| @@ -11087,19 +11826,21 @@ | ||
| 11087 | 11826 | |
| 11088 | 11827 | p = strchr(str, '('); |
| 11089 | 11828 | JimPanic((p == NULL, "JimDictSugarParseVarKey() called for non-dict-sugar (%s)", str)); |
| 11090 | 11829 | |
| 11091 | 11830 | varObjPtr = Jim_NewStringObj(interp, str, p - str); |
| 11831 | + varObjPtr->taint = objPtr->taint; | |
| 11092 | 11832 | |
| 11093 | 11833 | p++; |
| 11094 | 11834 | keyLen = (str + len) - p; |
| 11095 | 11835 | if (str[len - 1] == ')') { |
| 11096 | 11836 | keyLen--; |
| 11097 | 11837 | } |
| 11098 | 11838 | |
| 11099 | 11839 | |
| 11100 | 11840 | keyObjPtr = Jim_NewStringObj(interp, p, keyLen); |
| 11841 | + keyObjPtr->taint = objPtr->taint; | |
| 11101 | 11842 | |
| 11102 | 11843 | Jim_IncrRefCount(varObjPtr); |
| 11103 | 11844 | Jim_IncrRefCount(keyObjPtr); |
| 11104 | 11845 | *varPtrPtr = varObjPtr; |
| 11105 | 11846 | *keyPtrPtr = keyObjPtr; |
| @@ -11175,10 +11916,11 @@ | ||
| 11175 | 11916 | void FreeDictSubstInternalRep(Jim_Interp *interp, Jim_Obj *objPtr) |
| 11176 | 11917 | { |
| 11177 | 11918 | Jim_DecrRefCount(interp, objPtr->internalRep.dictSubstValue.varNameObjPtr); |
| 11178 | 11919 | Jim_DecrRefCount(interp, objPtr->internalRep.dictSubstValue.indexObjPtr); |
| 11179 | 11920 | } |
| 11921 | + | |
| 11180 | 11922 | |
| 11181 | 11923 | static void DupDictSubstInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr) |
| 11182 | 11924 | { |
| 11183 | 11925 | |
| 11184 | 11926 | dupPtr->internalRep = srcPtr->internalRep; |
| @@ -11270,10 +12012,11 @@ | ||
| 11270 | 12012 | cf->nsObj = nsObj; |
| 11271 | 12013 | Jim_IncrRefCount(nsObj); |
| 11272 | 12014 | |
| 11273 | 12015 | return cf; |
| 11274 | 12016 | } |
| 12017 | + | |
| 11275 | 12018 | |
| 11276 | 12019 | static int JimDeleteLocalProcs(Jim_Interp *interp, Jim_Stack *localCommands) |
| 11277 | 12020 | { |
| 11278 | 12021 | |
| 11279 | 12022 | if (localCommands) { |
| @@ -11298,11 +12041,11 @@ | ||
| 11298 | 12041 | Jim_DeleteHashEntry(ht, cmdNameObj); |
| 11299 | 12042 | } |
| 11300 | 12043 | } |
| 11301 | 12044 | Jim_DecrRefCount(interp, cmdNameObj); |
| 11302 | 12045 | } |
| 11303 | - Jim_FreeStack(localCommands); | |
| 12046 | + Jim_StackFree(localCommands); | |
| 11304 | 12047 | Jim_Free(localCommands); |
| 11305 | 12048 | } |
| 11306 | 12049 | return JIM_OK; |
| 11307 | 12050 | } |
| 11308 | 12051 | |
| @@ -11352,10 +12095,11 @@ | ||
| 11352 | 12095 | return retcode; |
| 11353 | 12096 | } |
| 11354 | 12097 | |
| 11355 | 12098 | #define JIM_FCF_FULL 0 |
| 11356 | 12099 | #define JIM_FCF_REUSE 1 |
| 12100 | + | |
| 11357 | 12101 | static void JimFreeCallFrame(Jim_Interp *interp, Jim_CallFrame *cf, int action) |
| 11358 | 12102 | { |
| 11359 | 12103 | JimDeleteLocalProcs(interp, cf->localCommands); |
| 11360 | 12104 | |
| 11361 | 12105 | if (cf->procArgsObjPtr) |
| @@ -11456,10 +12200,13 @@ | ||
| 11456 | 12200 | JimInvokeDefer(i, JIM_OK); |
| 11457 | 12201 | cfx = cf->parent; |
| 11458 | 12202 | JimFreeCallFrame(i, cf, JIM_FCF_FULL); |
| 11459 | 12203 | } |
| 11460 | 12204 | |
| 12205 | + | |
| 12206 | + Jim_FreeHashTable(&i->commands); | |
| 12207 | + | |
| 11461 | 12208 | Jim_DecrRefCount(i, i->emptyObj); |
| 11462 | 12209 | Jim_DecrRefCount(i, i->trueObj); |
| 11463 | 12210 | Jim_DecrRefCount(i, i->falseObj); |
| 11464 | 12211 | Jim_DecrRefCount(i, i->result); |
| 11465 | 12212 | Jim_DecrRefCount(i, i->stackTrace); |
| @@ -11467,12 +12214,10 @@ | ||
| 11467 | 12214 | Jim_DecrRefCount(i, i->unknown); |
| 11468 | 12215 | Jim_DecrRefCount(i, i->defer); |
| 11469 | 12216 | Jim_DecrRefCount(i, i->nullScriptObj); |
| 11470 | 12217 | Jim_DecrRefCount(i, i->currentFilenameObj); |
| 11471 | 12218 | |
| 11472 | - Jim_FreeHashTable(&i->commands); | |
| 11473 | - | |
| 11474 | 12219 | |
| 11475 | 12220 | Jim_InterpIncrProcEpoch(i); |
| 11476 | 12221 | |
| 11477 | 12222 | #ifdef JIM_REFERENCES |
| 11478 | 12223 | Jim_FreeHashTable(&i->references); |
| @@ -11602,10 +12347,11 @@ | ||
| 11602 | 12347 | } |
| 11603 | 12348 | } |
| 11604 | 12349 | } |
| 11605 | 12350 | return NULL; |
| 11606 | 12351 | } |
| 12352 | + | |
| 11607 | 12353 | |
| 11608 | 12354 | static Jim_EvalFrame *JimGetEvalFrameByProcLevel(Jim_Interp *interp, int proclevel) |
| 11609 | 12355 | { |
| 11610 | 12356 | Jim_EvalFrame *evalFrame; |
| 11611 | 12357 | |
| @@ -11632,11 +12378,11 @@ | ||
| 11632 | 12378 | static Jim_Obj *JimProcForEvalFrame(Jim_Interp *interp, Jim_EvalFrame *frame) |
| 11633 | 12379 | { |
| 11634 | 12380 | if (frame == interp->evalFrame || (frame->cmd && frame->cmd->cmdNameObj)) { |
| 11635 | 12381 | Jim_EvalFrame *e; |
| 11636 | 12382 | for (e = frame->parent; e; e = e->parent) { |
| 11637 | - if (e->cmd && e->cmd->isproc && e->cmd->cmdNameObj) { | |
| 12383 | + if (e->cmd && (e->cmd->flags & JIM_CMD_ISPROC) && e->cmd->cmdNameObj) { | |
| 11638 | 12384 | break; |
| 11639 | 12385 | } |
| 11640 | 12386 | } |
| 11641 | 12387 | if (e && e->cmd && e->cmd->cmdNameObj) { |
| 11642 | 12388 | return e->cmd->cmdNameObj; |
| @@ -11660,23 +12406,25 @@ | ||
| 11660 | 12406 | Jim_ListAppendElement(interp, listObj, procNameObj ? procNameObj : interp->emptyObj); |
| 11661 | 12407 | Jim_ListAppendElement(interp, listObj, fileNameObj); |
| 11662 | 12408 | Jim_ListAppendElement(interp, listObj, Jim_NewIntObj(interp, linenr)); |
| 11663 | 12409 | Jim_ListAppendElement(interp, listObj, Jim_NewListObj(interp, frame->argv, frame->argc)); |
| 11664 | 12410 | } |
| 12411 | + | |
| 11665 | 12412 | |
| 11666 | 12413 | static void JimSetStackTrace(Jim_Interp *interp, Jim_Obj *stackTraceObj) |
| 11667 | 12414 | { |
| 11668 | 12415 | |
| 11669 | 12416 | Jim_IncrRefCount(stackTraceObj); |
| 11670 | 12417 | Jim_DecrRefCount(interp, interp->stackTrace); |
| 11671 | 12418 | interp->stackTrace = stackTraceObj; |
| 11672 | - interp->errorFlag = 1; | |
| 12419 | + interp->hasErrorStackTrace = 1; | |
| 11673 | 12420 | } |
| 12421 | + | |
| 11674 | 12422 | |
| 11675 | 12423 | static void JimSetErrorStack(Jim_Interp *interp, ScriptObj *script) |
| 11676 | 12424 | { |
| 11677 | - if (!interp->errorFlag) { | |
| 12425 | + if (!interp->hasErrorStackTrace) { | |
| 11678 | 12426 | int i; |
| 11679 | 12427 | Jim_Obj *stackTrace = Jim_NewListObj(interp, NULL, 0); |
| 11680 | 12428 | |
| 11681 | 12429 | if (interp->procLevel == 0 && script) { |
| 11682 | 12430 | Jim_ListAppendElement(interp, stackTrace, interp->emptyObj); |
| @@ -11744,10 +12492,11 @@ | ||
| 11744 | 12492 | NULL, |
| 11745 | 12493 | UpdateStringOfInt, |
| 11746 | 12494 | JIM_TYPE_NONE, |
| 11747 | 12495 | }; |
| 11748 | 12496 | |
| 12497 | + | |
| 11749 | 12498 | |
| 11750 | 12499 | static void UpdateStringOfInt(struct Jim_Obj *objPtr) |
| 11751 | 12500 | { |
| 11752 | 12501 | char buf[JIM_INTEGER_SPACE + 1]; |
| 11753 | 12502 | jim_wide wideValue = JimWideValue(objPtr); |
| @@ -11779,10 +12528,11 @@ | ||
| 11779 | 12528 | } |
| 11780 | 12529 | buf[pos] = 0; |
| 11781 | 12530 | |
| 11782 | 12531 | JimSetStringBytes(objPtr, buf); |
| 11783 | 12532 | } |
| 12533 | + | |
| 11784 | 12534 | |
| 11785 | 12535 | static int SetIntFromAny(Jim_Interp *interp, Jim_Obj *objPtr, int flags) |
| 11786 | 12536 | { |
| 11787 | 12537 | jim_wide wideValue; |
| 11788 | 12538 | const char *str; |
| @@ -11812,10 +12562,11 @@ | ||
| 11812 | 12562 | objPtr->internalRep.wideValue = wideValue; |
| 11813 | 12563 | return JIM_OK; |
| 11814 | 12564 | } |
| 11815 | 12565 | |
| 11816 | 12566 | #ifdef JIM_OPTIMIZATION |
| 12567 | + | |
| 11817 | 12568 | static int JimIsWide(Jim_Obj *objPtr) |
| 11818 | 12569 | { |
| 11819 | 12570 | return objPtr->typePtr == &intObjType; |
| 11820 | 12571 | } |
| 11821 | 12572 | #endif |
| @@ -11881,10 +12632,11 @@ | ||
| 11881 | 12632 | Jim_Obj *objPtr; |
| 11882 | 12633 | |
| 11883 | 12634 | objPtr = Jim_NewObj(interp); |
| 11884 | 12635 | objPtr->typePtr = &intObjType; |
| 11885 | 12636 | objPtr->bytes = NULL; |
| 12637 | + objPtr->taint = 0; | |
| 11886 | 12638 | objPtr->internalRep.wideValue = wideValue; |
| 11887 | 12639 | return objPtr; |
| 11888 | 12640 | } |
| 11889 | 12641 | |
| 11890 | 12642 | #define JIM_DOUBLE_SPACE 30 |
| @@ -11906,10 +12658,11 @@ | ||
| 11906 | 12658 | #endif |
| 11907 | 12659 | #if !HAVE_DECL_ISINF |
| 11908 | 12660 | #undef isinf |
| 11909 | 12661 | #define isinf(X) (1.0 / (X) == 0.0) |
| 11910 | 12662 | #endif |
| 12663 | + | |
| 11911 | 12664 | |
| 11912 | 12665 | static void UpdateStringOfDouble(struct Jim_Obj *objPtr) |
| 11913 | 12666 | { |
| 11914 | 12667 | double value = objPtr->internalRep.doubleValue; |
| 11915 | 12668 | |
| @@ -11951,10 +12704,11 @@ | ||
| 11951 | 12704 | buf[i] = '\0'; |
| 11952 | 12705 | } |
| 11953 | 12706 | JimSetStringBytes(objPtr, buf); |
| 11954 | 12707 | } |
| 11955 | 12708 | } |
| 12709 | + | |
| 11956 | 12710 | |
| 11957 | 12711 | static int SetDoubleFromAny(Jim_Interp *interp, Jim_Obj *objPtr) |
| 11958 | 12712 | { |
| 11959 | 12713 | double doubleValue; |
| 11960 | 12714 | jim_wide wideValue; |
| @@ -12020,10 +12774,11 @@ | ||
| 12020 | 12774 | Jim_Obj *objPtr; |
| 12021 | 12775 | |
| 12022 | 12776 | objPtr = Jim_NewObj(interp); |
| 12023 | 12777 | objPtr->typePtr = &doubleObjType; |
| 12024 | 12778 | objPtr->bytes = NULL; |
| 12779 | + objPtr->taint = 0; | |
| 12025 | 12780 | objPtr->internalRep.doubleValue = doubleValue; |
| 12026 | 12781 | return objPtr; |
| 12027 | 12782 | } |
| 12028 | 12783 | |
| 12029 | 12784 | static int SetBooleanFromAny(Jim_Interp *interp, Jim_Obj *objPtr, int flags); |
| @@ -12043,10 +12798,11 @@ | ||
| 12043 | 12798 | |
| 12044 | 12799 | static const int jim_true_false_lens[8] = { |
| 12045 | 12800 | 1, 4, 3, 2, |
| 12046 | 12801 | 1, 5, 2, 3, |
| 12047 | 12802 | }; |
| 12803 | + | |
| 12048 | 12804 | |
| 12049 | 12805 | static int SetBooleanFromAny(Jim_Interp *interp, Jim_Obj *objPtr, int flags) |
| 12050 | 12806 | { |
| 12051 | 12807 | int index = Jim_FindByName(Jim_String(objPtr), jim_true_false_strings, |
| 12052 | 12808 | sizeof(jim_true_false_strings) / sizeof(*jim_true_false_strings)); |
| @@ -12109,10 +12865,11 @@ | ||
| 12109 | 12865 | } |
| 12110 | 12866 | |
| 12111 | 12867 | #define JIM_ELESTR_SIMPLE 0 |
| 12112 | 12868 | #define JIM_ELESTR_BRACE 1 |
| 12113 | 12869 | #define JIM_ELESTR_QUOTE 2 |
| 12870 | + | |
| 12114 | 12871 | static unsigned char ListElementQuotingType(const char *s, int len) |
| 12115 | 12872 | { |
| 12116 | 12873 | int i, level, blevel, trySimple = 1; |
| 12117 | 12874 | |
| 12118 | 12875 | |
| @@ -12255,10 +13012,11 @@ | ||
| 12255 | 13012 | } |
| 12256 | 13013 | *p = '\0'; |
| 12257 | 13014 | |
| 12258 | 13015 | return p - q; |
| 12259 | 13016 | } |
| 13017 | + | |
| 12260 | 13018 | |
| 12261 | 13019 | static void JimMakeListStringRep(Jim_Obj *objPtr, Jim_Obj **objv, int objc) |
| 12262 | 13020 | { |
| 12263 | 13021 | #define STATIC_QUOTING_LEN 32 |
| 12264 | 13022 | int i, bufLen, realLength; |
| @@ -12341,15 +13099,17 @@ | ||
| 12341 | 13099 | |
| 12342 | 13100 | if (quotingType != staticQuoting) { |
| 12343 | 13101 | Jim_Free(quotingType); |
| 12344 | 13102 | } |
| 12345 | 13103 | } |
| 13104 | + | |
| 12346 | 13105 | |
| 12347 | 13106 | static void UpdateStringOfList(struct Jim_Obj *objPtr) |
| 12348 | 13107 | { |
| 12349 | 13108 | JimMakeListStringRep(objPtr, objPtr->internalRep.listValue.ele, objPtr->internalRep.listValue.len); |
| 12350 | 13109 | } |
| 13110 | + | |
| 12351 | 13111 | |
| 12352 | 13112 | static int SetListFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr) |
| 12353 | 13113 | { |
| 12354 | 13114 | struct JimParserCtx parser; |
| 12355 | 13115 | const char *str; |
| @@ -12401,10 +13161,11 @@ | ||
| 12401 | 13161 | JimParseList(&parser); |
| 12402 | 13162 | if (parser.tt != JIM_TT_STR && parser.tt != JIM_TT_ESC) |
| 12403 | 13163 | continue; |
| 12404 | 13164 | elementPtr = JimParserGetTokenObj(interp, &parser); |
| 12405 | 13165 | Jim_SetSourceInfo(interp, elementPtr, fileNameObj, parser.tline); |
| 13166 | + elementPtr->taint = objPtr->taint; | |
| 12406 | 13167 | ListAppendElement(objPtr, elementPtr); |
| 12407 | 13168 | } |
| 12408 | 13169 | } |
| 12409 | 13170 | Jim_DecrRefCount(interp, fileNameObj); |
| 12410 | 13171 | return JIM_OK; |
| @@ -12415,10 +13176,11 @@ | ||
| 12415 | 13176 | Jim_Obj *objPtr; |
| 12416 | 13177 | |
| 12417 | 13178 | objPtr = Jim_NewObj(interp); |
| 12418 | 13179 | objPtr->typePtr = &listObjType; |
| 12419 | 13180 | objPtr->bytes = NULL; |
| 13181 | + objPtr->taint = 0; | |
| 12420 | 13182 | objPtr->internalRep.listValue.ele = NULL; |
| 12421 | 13183 | objPtr->internalRep.listValue.len = 0; |
| 12422 | 13184 | objPtr->internalRep.listValue.maxLen = 0; |
| 12423 | 13185 | |
| 12424 | 13186 | if (len) { |
| @@ -12466,10 +13228,11 @@ | ||
| 12466 | 13228 | int unique; |
| 12467 | 13229 | int (*subfn)(Jim_Obj **, Jim_Obj **); |
| 12468 | 13230 | }; |
| 12469 | 13231 | |
| 12470 | 13232 | static struct lsort_info *sort_info; |
| 13233 | + | |
| 12471 | 13234 | |
| 12472 | 13235 | static int ListSortIndexHelper(Jim_Obj **lhsObj, Jim_Obj **rhsObj) |
| 12473 | 13236 | { |
| 12474 | 13237 | Jim_Obj *lObj, *rObj; |
| 12475 | 13238 | |
| @@ -12483,15 +13246,17 @@ | ||
| 12483 | 13246 | |
| 12484 | 13247 | static int ListSortString(Jim_Obj **lhsObj, Jim_Obj **rhsObj) |
| 12485 | 13248 | { |
| 12486 | 13249 | return Jim_StringCompareObj(sort_info->interp, *lhsObj, *rhsObj, 0) * sort_info->order; |
| 12487 | 13250 | } |
| 13251 | + | |
| 12488 | 13252 | |
| 12489 | 13253 | static int ListSortStringNoCase(Jim_Obj **lhsObj, Jim_Obj **rhsObj) |
| 12490 | 13254 | { |
| 12491 | 13255 | return Jim_StringCompareObj(sort_info->interp, *lhsObj, *rhsObj, 1) * sort_info->order; |
| 12492 | 13256 | } |
| 13257 | + | |
| 12493 | 13258 | |
| 12494 | 13259 | static int ListSortDict(Jim_Obj **lhsObj, Jim_Obj **rhsObj) |
| 12495 | 13260 | { |
| 12496 | 13261 | |
| 12497 | 13262 | const char *left = Jim_String(*lhsObj); |
| @@ -12525,10 +13290,11 @@ | ||
| 12525 | 13290 | return Jim_StringCompareObj(sort_info->interp, *lhsObj, *rhsObj, 0) * sort_info->order; |
| 12526 | 13291 | } |
| 12527 | 13292 | } |
| 12528 | 13293 | } |
| 12529 | 13294 | } |
| 13295 | + | |
| 12530 | 13296 | |
| 12531 | 13297 | static int ListSortInteger(Jim_Obj **lhsObj, Jim_Obj **rhsObj) |
| 12532 | 13298 | { |
| 12533 | 13299 | jim_wide lhs = 0, rhs = 0; |
| 12534 | 13300 | |
| @@ -12537,10 +13303,11 @@ | ||
| 12537 | 13303 | longjmp(sort_info->jmpbuf, JIM_ERR); |
| 12538 | 13304 | } |
| 12539 | 13305 | |
| 12540 | 13306 | return JimSign(lhs - rhs) * sort_info->order; |
| 12541 | 13307 | } |
| 13308 | + | |
| 12542 | 13309 | |
| 12543 | 13310 | static int ListSortReal(Jim_Obj **lhsObj, Jim_Obj **rhsObj) |
| 12544 | 13311 | { |
| 12545 | 13312 | double lhs = 0, rhs = 0; |
| 12546 | 13313 | |
| @@ -12554,10 +13321,11 @@ | ||
| 12554 | 13321 | if (lhs > rhs) { |
| 12555 | 13322 | return sort_info->order; |
| 12556 | 13323 | } |
| 12557 | 13324 | return -sort_info->order; |
| 12558 | 13325 | } |
| 13326 | + | |
| 12559 | 13327 | |
| 12560 | 13328 | static int ListSortCommand(Jim_Obj **lhsObj, Jim_Obj **rhsObj) |
| 12561 | 13329 | { |
| 12562 | 13330 | Jim_Obj *compare_script; |
| 12563 | 13331 | int rc; |
| @@ -12711,10 +13479,11 @@ | ||
| 12711 | 13479 | } |
| 12712 | 13480 | point = listPtr->internalRep.listValue.ele + idx; |
| 12713 | 13481 | memmove(point + elemc, point, (currentLen - idx) * sizeof(Jim_Obj *)); |
| 12714 | 13482 | for (i = 0; i < elemc; ++i) { |
| 12715 | 13483 | point[i] = elemVec[i]; |
| 13484 | + listPtr->taint |= point[i]->taint; | |
| 12716 | 13485 | Jim_IncrRefCount(point[i]); |
| 12717 | 13486 | } |
| 12718 | 13487 | listPtr->internalRep.listValue.len += elemc; |
| 12719 | 13488 | } |
| 12720 | 13489 | |
| @@ -12827,10 +13596,11 @@ | ||
| 12827 | 13596 | err: |
| 12828 | 13597 | if (idxes != static_idxes) |
| 12829 | 13598 | Jim_Free(idxes); |
| 12830 | 13599 | return ret; |
| 12831 | 13600 | } |
| 13601 | + | |
| 12832 | 13602 | |
| 12833 | 13603 | static int ListSetIndex(Jim_Interp *interp, Jim_Obj *listPtr, int idx, |
| 12834 | 13604 | Jim_Obj *newObjPtr, int flags) |
| 12835 | 13605 | { |
| 12836 | 13606 | SetListFromAny(interp, listPtr); |
| @@ -12843,10 +13613,11 @@ | ||
| 12843 | 13613 | } |
| 12844 | 13614 | if (idx < 0) |
| 12845 | 13615 | idx = listPtr->internalRep.listValue.len + idx; |
| 12846 | 13616 | Jim_DecrRefCount(interp, listPtr->internalRep.listValue.ele[idx]); |
| 12847 | 13617 | listPtr->internalRep.listValue.ele[idx] = newObjPtr; |
| 13618 | + listPtr->taint |= newObjPtr->taint; | |
| 12848 | 13619 | Jim_IncrRefCount(newObjPtr); |
| 12849 | 13620 | return JIM_OK; |
| 12850 | 13621 | } |
| 12851 | 13622 | |
| 12852 | 13623 | int Jim_ListSetIndex(Jim_Interp *interp, Jim_Obj *varNamePtr, |
| @@ -12880,12 +13651,13 @@ | ||
| 12880 | 13651 | goto err; |
| 12881 | 13652 | if (ListSetIndex(interp, objPtr, idx, newObjPtr, JIM_ERRMSG) == JIM_ERR) |
| 12882 | 13653 | goto err; |
| 12883 | 13654 | Jim_InvalidateStringRep(objPtr); |
| 12884 | 13655 | Jim_InvalidateStringRep(varObjPtr); |
| 12885 | - if (Jim_SetVariable(interp, varNamePtr, varObjPtr) != JIM_OK) | |
| 12886 | - goto err; | |
| 13656 | + if (Jim_SetVariable(interp, varNamePtr, varObjPtr) != JIM_OK) { | |
| 13657 | + return JIM_ERR; | |
| 13658 | + } | |
| 12887 | 13659 | Jim_SetResult(interp, varObjPtr); |
| 12888 | 13660 | return JIM_OK; |
| 12889 | 13661 | err: |
| 12890 | 13662 | if (shared) { |
| 12891 | 13663 | Jim_FreeNewObj(interp, varObjPtr); |
| @@ -13134,15 +13906,17 @@ | ||
| 13134 | 13906 | #ifdef JIM_RANDOMISE_HASH |
| 13135 | 13907 | dict->uniq = (rand() ^ time(NULL) ^ clock()); |
| 13136 | 13908 | #endif |
| 13137 | 13909 | return dict; |
| 13138 | 13910 | } |
| 13911 | + | |
| 13139 | 13912 | |
| 13140 | 13913 | static void FreeDictInternalRep(Jim_Interp *interp, Jim_Obj *objPtr) |
| 13141 | 13914 | { |
| 13142 | 13915 | JimFreeDict(interp, objPtr->internalRep.dictValue); |
| 13143 | 13916 | } |
| 13917 | + | |
| 13144 | 13918 | |
| 13145 | 13919 | static void DupDictInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr) |
| 13146 | 13920 | { |
| 13147 | 13921 | Jim_Dict *oldDict = srcPtr->internalRep.dictValue; |
| 13148 | 13922 | int i; |
| @@ -13164,15 +13938,17 @@ | ||
| 13164 | 13938 | memcpy(newDict->ht, oldDict->ht, sizeof(*oldDict->ht) * oldDict->size); |
| 13165 | 13939 | |
| 13166 | 13940 | dupPtr->internalRep.dictValue = newDict; |
| 13167 | 13941 | dupPtr->typePtr = &dictObjType; |
| 13168 | 13942 | } |
| 13943 | + | |
| 13169 | 13944 | |
| 13170 | 13945 | static void UpdateStringOfDict(struct Jim_Obj *objPtr) |
| 13171 | 13946 | { |
| 13172 | 13947 | JimMakeListStringRep(objPtr, objPtr->internalRep.dictValue->table, objPtr->internalRep.dictValue->len); |
| 13173 | 13948 | } |
| 13949 | + | |
| 13174 | 13950 | |
| 13175 | 13951 | static int SetDictFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr) |
| 13176 | 13952 | { |
| 13177 | 13953 | int listlen; |
| 13178 | 13954 | |
| @@ -13279,10 +14055,11 @@ | ||
| 13279 | 14055 | |
| 13280 | 14056 | dict->table[dict->len++] = keyObjPtr; |
| 13281 | 14057 | dict->table[dict->len++] = valueObjPtr; |
| 13282 | 14058 | |
| 13283 | 14059 | } |
| 14060 | + objPtr->taint |= keyObjPtr->taint | valueObjPtr->taint; | |
| 13284 | 14061 | return JIM_OK; |
| 13285 | 14062 | } |
| 13286 | 14063 | } |
| 13287 | 14064 | |
| 13288 | 14065 | int Jim_DictAddElement(Jim_Interp *interp, Jim_Obj *objPtr, |
| @@ -13390,11 +14167,10 @@ | ||
| 13390 | 14167 | |
| 13391 | 14168 | return JIM_ERR; |
| 13392 | 14169 | } |
| 13393 | 14170 | varObjPtr = objPtr = Jim_NewDictObj(interp, NULL, 0); |
| 13394 | 14171 | if (Jim_SetVariable(interp, varNamePtr, objPtr) != JIM_OK) { |
| 13395 | - Jim_FreeNewObj(interp, varObjPtr); | |
| 13396 | 14172 | return JIM_ERR; |
| 13397 | 14173 | } |
| 13398 | 14174 | } |
| 13399 | 14175 | if ((shared = Jim_IsShared(objPtr))) |
| 13400 | 14176 | varObjPtr = objPtr = Jim_DuplicateObj(interp, objPtr); |
| @@ -13411,10 +14187,13 @@ | ||
| 13411 | 14187 | if (Jim_DictAddElement(interp, objPtr, keyv[keyc - 1], newObjPtr) != JIM_OK) { |
| 13412 | 14188 | if (newObjPtr || (flags & JIM_MUSTEXIST)) { |
| 13413 | 14189 | goto err; |
| 13414 | 14190 | } |
| 13415 | 14191 | } |
| 14192 | + if (newObjPtr) { | |
| 14193 | + varObjPtr->taint |= newObjPtr->taint; | |
| 14194 | + } | |
| 13416 | 14195 | break; |
| 13417 | 14196 | } |
| 13418 | 14197 | |
| 13419 | 14198 | |
| 13420 | 14199 | Jim_InvalidateStringRep(dictObjPtr); |
| @@ -13435,11 +14214,11 @@ | ||
| 13435 | 14214 | } |
| 13436 | 14215 | |
| 13437 | 14216 | Jim_InvalidateStringRep(objPtr); |
| 13438 | 14217 | Jim_InvalidateStringRep(varObjPtr); |
| 13439 | 14218 | if (Jim_SetVariable(interp, varNamePtr, varObjPtr) != JIM_OK) { |
| 13440 | - goto err; | |
| 14219 | + return JIM_ERR; | |
| 13441 | 14220 | } |
| 13442 | 14221 | |
| 13443 | 14222 | if (!(flags & JIM_NORESULT)) { |
| 13444 | 14223 | Jim_SetResult(interp, varObjPtr); |
| 13445 | 14224 | } |
| @@ -13459,10 +14238,11 @@ | ||
| 13459 | 14238 | NULL, |
| 13460 | 14239 | NULL, |
| 13461 | 14240 | UpdateStringOfIndex, |
| 13462 | 14241 | JIM_TYPE_NONE, |
| 13463 | 14242 | }; |
| 14243 | + | |
| 13464 | 14244 | |
| 13465 | 14245 | static void UpdateStringOfIndex(struct Jim_Obj *objPtr) |
| 13466 | 14246 | { |
| 13467 | 14247 | if (objPtr->internalRep.intValue == -1) { |
| 13468 | 14248 | JimSetStringBytes(objPtr, "end"); |
| @@ -13477,10 +14257,11 @@ | ||
| 13477 | 14257 | sprintf(buf, "end%d", objPtr->internalRep.intValue + 1); |
| 13478 | 14258 | } |
| 13479 | 14259 | JimSetStringBytes(objPtr, buf); |
| 13480 | 14260 | } |
| 13481 | 14261 | } |
| 14262 | + | |
| 13482 | 14263 | |
| 13483 | 14264 | static int SetIndexFromAny(Jim_Interp *interp, Jim_Obj *objPtr) |
| 13484 | 14265 | { |
| 13485 | 14266 | jim_wide idx; |
| 13486 | 14267 | int end = 0; |
| @@ -13530,10 +14311,13 @@ | ||
| 13530 | 14311 | idx--; |
| 13531 | 14312 | } |
| 13532 | 14313 | } |
| 13533 | 14314 | else if (idx < 0) { |
| 13534 | 14315 | idx = -INT_MAX; |
| 14316 | + } | |
| 14317 | + else if (idx > INT_MAX) { | |
| 14318 | + goto badindex; | |
| 13535 | 14319 | } |
| 13536 | 14320 | |
| 13537 | 14321 | |
| 13538 | 14322 | Jim_FreeIntRep(interp, objPtr); |
| 13539 | 14323 | objPtr->typePtr = &indexObjType; |
| @@ -13597,10 +14381,11 @@ | ||
| 13597 | 14381 | } |
| 13598 | 14382 | else { |
| 13599 | 14383 | return jimReturnCodes[code]; |
| 13600 | 14384 | } |
| 13601 | 14385 | } |
| 14386 | + | |
| 13602 | 14387 | |
| 13603 | 14388 | static int SetReturnCodeFromAny(Jim_Interp *interp, Jim_Obj *objPtr) |
| 13604 | 14389 | { |
| 13605 | 14390 | int returnCode; |
| 13606 | 14391 | jim_wide wideValue; |
| @@ -13663,10 +14448,13 @@ | ||
| 13663 | 14448 | JIM_EXPROP_POW, |
| 13664 | 14449 | |
| 13665 | 14450 | |
| 13666 | 14451 | JIM_EXPROP_STREQ, |
| 13667 | 14452 | JIM_EXPROP_STRNE, |
| 14453 | + JIM_EXPROP_STRGLOB, | |
| 14454 | + JIM_EXPROP_STRRE, | |
| 14455 | + | |
| 13668 | 14456 | JIM_EXPROP_STRIN, |
| 13669 | 14457 | JIM_EXPROP_STRNI, |
| 13670 | 14458 | JIM_EXPROP_STRLT, |
| 13671 | 14459 | JIM_EXPROP_STRGT, |
| 13672 | 14460 | JIM_EXPROP_STRLE, |
| @@ -13730,10 +14518,11 @@ | ||
| 13730 | 14518 | } Jim_ExprOperator; |
| 13731 | 14519 | |
| 13732 | 14520 | static int JimExprGetTerm(Jim_Interp *interp, struct JimExprNode *node, Jim_Obj **objPtrPtr); |
| 13733 | 14521 | static int JimExprGetTermBoolean(Jim_Interp *interp, struct JimExprNode *node); |
| 13734 | 14522 | static int JimExprEvalTermNode(Jim_Interp *interp, struct JimExprNode *node); |
| 14523 | + | |
| 13735 | 14524 | |
| 13736 | 14525 | static int JimExprOpNumUnary(Jim_Interp *interp, struct JimExprNode *node) |
| 13737 | 14526 | { |
| 13738 | 14527 | int intresult = 1; |
| 13739 | 14528 | int rc, bA = 0; |
| @@ -13806,10 +14595,17 @@ | ||
| 13806 | 14595 | else if ((rc = Jim_GetBoolean(interp, A, &bA)) == JIM_OK) { |
| 13807 | 14596 | switch (node->type) { |
| 13808 | 14597 | case JIM_EXPROP_NOT: |
| 13809 | 14598 | wC = !bA; |
| 13810 | 14599 | break; |
| 14600 | + case JIM_EXPROP_UNARYPLUS: | |
| 14601 | + case JIM_EXPROP_UNARYMINUS: | |
| 14602 | + rc = JIM_ERR; | |
| 14603 | + Jim_SetResultFormatted(interp, | |
| 14604 | + "can't use non-numeric string as operand of \"%s\"", | |
| 14605 | + node->type == JIM_EXPROP_UNARYPLUS ? "+" : "-"); | |
| 14606 | + break; | |
| 13811 | 14607 | default: |
| 13812 | 14608 | abort(); |
| 13813 | 14609 | } |
| 13814 | 14610 | } |
| 13815 | 14611 | |
| @@ -13824,18 +14620,20 @@ | ||
| 13824 | 14620 | |
| 13825 | 14621 | Jim_DecrRefCount(interp, A); |
| 13826 | 14622 | |
| 13827 | 14623 | return rc; |
| 13828 | 14624 | } |
| 14625 | + | |
| 13829 | 14626 | |
| 13830 | 14627 | static double JimRandDouble(Jim_Interp *interp) |
| 13831 | 14628 | { |
| 13832 | 14629 | unsigned long x; |
| 13833 | 14630 | JimRandomBytes(interp, &x, sizeof(x)); |
| 13834 | 14631 | |
| 13835 | 14632 | return (double)x / (double)~0UL; |
| 13836 | 14633 | } |
| 14634 | + | |
| 13837 | 14635 | |
| 13838 | 14636 | static int JimExprOpIntUnary(Jim_Interp *interp, struct JimExprNode *node) |
| 13839 | 14637 | { |
| 13840 | 14638 | jim_wide wA; |
| 13841 | 14639 | Jim_Obj *A; |
| @@ -13862,10 +14660,11 @@ | ||
| 13862 | 14660 | |
| 13863 | 14661 | Jim_DecrRefCount(interp, A); |
| 13864 | 14662 | |
| 13865 | 14663 | return rc; |
| 13866 | 14664 | } |
| 14665 | + | |
| 13867 | 14666 | |
| 13868 | 14667 | static int JimExprOpNone(Jim_Interp *interp, struct JimExprNode *node) |
| 13869 | 14668 | { |
| 13870 | 14669 | JimPanic((node->type != JIM_EXPROP_FUNC_RAND, "JimExprOpNone only support rand()")); |
| 13871 | 14670 | |
| @@ -13873,10 +14672,11 @@ | ||
| 13873 | 14672 | |
| 13874 | 14673 | return JIM_OK; |
| 13875 | 14674 | } |
| 13876 | 14675 | |
| 13877 | 14676 | #ifdef JIM_MATH_FUNCTIONS |
| 14677 | + | |
| 13878 | 14678 | static int JimExprOpDoubleUnary(Jim_Interp *interp, struct JimExprNode *node) |
| 13879 | 14679 | { |
| 13880 | 14680 | int rc; |
| 13881 | 14681 | double dA, dC; |
| 13882 | 14682 | Jim_Obj *A; |
| @@ -14218,10 +15018,11 @@ | ||
| 14218 | 15018 | goto done; |
| 14219 | 15019 | doubleresult: |
| 14220 | 15020 | Jim_SetResult(interp, Jim_NewDoubleObj(interp, dC)); |
| 14221 | 15021 | goto done; |
| 14222 | 15022 | } |
| 15023 | + | |
| 14223 | 15024 | |
| 14224 | 15025 | static int JimSearchList(Jim_Interp *interp, Jim_Obj *listObjPtr, Jim_Obj *valObj) |
| 14225 | 15026 | { |
| 14226 | 15027 | int listlen; |
| 14227 | 15028 | int i; |
| @@ -14233,10 +15034,29 @@ | ||
| 14233 | 15034 | } |
| 14234 | 15035 | } |
| 14235 | 15036 | return 0; |
| 14236 | 15037 | } |
| 14237 | 15038 | |
| 15039 | +static int JimRegexpMatch(Jim_Interp *interp, Jim_Obj *patternObj, Jim_Obj *objPtr) | |
| 15040 | +{ | |
| 15041 | + Jim_Obj *argv[3]; | |
| 15042 | + int argc = 0; | |
| 15043 | + long eq; | |
| 15044 | + int rc; | |
| 15045 | + | |
| 15046 | + argv[argc++] = Jim_NewStringObj(interp, "regexp", -1); | |
| 15047 | + argv[argc++] = patternObj; | |
| 15048 | + argv[argc++] = objPtr; | |
| 15049 | + | |
| 15050 | + rc = Jim_EvalObjVector(interp, argc, argv); | |
| 15051 | + | |
| 15052 | + if (rc != JIM_OK || Jim_GetLong(interp, Jim_GetResult(interp), &eq) != JIM_OK) { | |
| 15053 | + eq = -rc; | |
| 15054 | + } | |
| 15055 | + | |
| 15056 | + return eq; | |
| 15057 | +} | |
| 14238 | 15058 | |
| 14239 | 15059 | |
| 14240 | 15060 | static int JimExprOpStrBin(Jim_Interp *interp, struct JimExprNode *node) |
| 14241 | 15061 | { |
| 14242 | 15062 | Jim_Obj *A, *B; |
| @@ -14277,21 +15097,33 @@ | ||
| 14277 | 15097 | case JIM_EXPROP_STRIN: |
| 14278 | 15098 | wC = JimSearchList(interp, B, A); |
| 14279 | 15099 | break; |
| 14280 | 15100 | case JIM_EXPROP_STRNI: |
| 14281 | 15101 | wC = !JimSearchList(interp, B, A); |
| 15102 | + break; | |
| 15103 | + case JIM_EXPROP_STRGLOB: | |
| 15104 | + wC = Jim_StringMatchObj(interp, B, A, 0); | |
| 15105 | + break; | |
| 15106 | + case JIM_EXPROP_STRRE: | |
| 15107 | + wC = JimRegexpMatch(interp, B, A); | |
| 15108 | + if (wC < 0) { | |
| 15109 | + rc = JIM_ERR; | |
| 15110 | + goto error; | |
| 15111 | + } | |
| 14282 | 15112 | break; |
| 14283 | 15113 | default: |
| 14284 | 15114 | abort(); |
| 14285 | 15115 | } |
| 14286 | 15116 | Jim_SetResultInt(interp, wC); |
| 14287 | 15117 | |
| 15118 | +error: | |
| 14288 | 15119 | Jim_DecrRefCount(interp, A); |
| 14289 | 15120 | Jim_DecrRefCount(interp, B); |
| 14290 | 15121 | |
| 14291 | 15122 | return rc; |
| 14292 | 15123 | } |
| 15124 | + | |
| 14293 | 15125 | |
| 14294 | 15126 | static int ExprBool(Jim_Interp *interp, Jim_Obj *obj) |
| 14295 | 15127 | { |
| 14296 | 15128 | long l; |
| 14297 | 15129 | double d; |
| @@ -14312,10 +15144,11 @@ | ||
| 14312 | 15144 | } |
| 14313 | 15145 | |
| 14314 | 15146 | Jim_DecrRefCount(interp, obj); |
| 14315 | 15147 | return ret; |
| 14316 | 15148 | } |
| 15149 | + | |
| 14317 | 15150 | |
| 14318 | 15151 | static int JimExprOpAnd(Jim_Interp *interp, struct JimExprNode *node) |
| 14319 | 15152 | { |
| 14320 | 15153 | |
| 14321 | 15154 | int result = JimExprGetTermBoolean(interp, node->left); |
| @@ -14328,10 +15161,11 @@ | ||
| 14328 | 15161 | return JIM_ERR; |
| 14329 | 15162 | } |
| 14330 | 15163 | Jim_SetResultInt(interp, result); |
| 14331 | 15164 | return JIM_OK; |
| 14332 | 15165 | } |
| 15166 | + | |
| 14333 | 15167 | |
| 14334 | 15168 | static int JimExprOpOr(Jim_Interp *interp, struct JimExprNode *node) |
| 14335 | 15169 | { |
| 14336 | 15170 | |
| 14337 | 15171 | int result = JimExprGetTermBoolean(interp, node->left); |
| @@ -14344,10 +15178,11 @@ | ||
| 14344 | 15178 | return JIM_ERR; |
| 14345 | 15179 | } |
| 14346 | 15180 | Jim_SetResultInt(interp, result); |
| 14347 | 15181 | return JIM_OK; |
| 14348 | 15182 | } |
| 15183 | + | |
| 14349 | 15184 | |
| 14350 | 15185 | static int JimExprOpTernary(Jim_Interp *interp, struct JimExprNode *node) |
| 14351 | 15186 | { |
| 14352 | 15187 | |
| 14353 | 15188 | int result = JimExprGetTermBoolean(interp, node->left); |
| @@ -14407,10 +15242,12 @@ | ||
| 14407 | 15242 | |
| 14408 | 15243 | OPRINIT_ATTR("**", 120, 2, JimExprOpBin, OP_RIGHT_ASSOC), |
| 14409 | 15244 | |
| 14410 | 15245 | OPRINIT("eq", 60, 2, JimExprOpStrBin), |
| 14411 | 15246 | OPRINIT("ne", 60, 2, JimExprOpStrBin), |
| 15247 | + OPRINIT("=*", 60, 2, JimExprOpStrBin), | |
| 15248 | + OPRINIT("=~", 60, 2, JimExprOpStrBin), | |
| 14412 | 15249 | |
| 14413 | 15250 | OPRINIT("in", 55, 2, JimExprOpStrBin), |
| 14414 | 15251 | OPRINIT("ni", 55, 2, JimExprOpStrBin), |
| 14415 | 15252 | |
| 14416 | 15253 | OPRINIT("lt", 75, 2, JimExprOpStrBin), |
| @@ -14458,10 +15295,11 @@ | ||
| 14458 | 15295 | #undef OPRINIT |
| 14459 | 15296 | #undef OPRINIT_ATTR |
| 14460 | 15297 | |
| 14461 | 15298 | #define JIM_EXPR_OPERATORS_NUM \ |
| 14462 | 15299 | (sizeof(Jim_ExprOperators)/sizeof(struct Jim_ExprOperator)) |
| 15300 | + | |
| 14463 | 15301 | |
| 14464 | 15302 | static int JimParseExpression(struct JimParserCtx *pc) |
| 14465 | 15303 | { |
| 14466 | 15304 | pc->errmsg = NULL; |
| 14467 | 15305 | |
| @@ -14557,10 +15395,11 @@ | ||
| 14557 | 15395 | return JimParseExprOperator(pc); |
| 14558 | 15396 | break; |
| 14559 | 15397 | } |
| 14560 | 15398 | return JIM_OK; |
| 14561 | 15399 | } |
| 15400 | + | |
| 14562 | 15401 | |
| 14563 | 15402 | static int JimParseExprNumber(struct JimParserCtx *pc) |
| 14564 | 15403 | { |
| 14565 | 15404 | char *end; |
| 14566 | 15405 | |
| @@ -14581,10 +15420,11 @@ | ||
| 14581 | 15420 | } |
| 14582 | 15421 | pc->tend = pc->p - 1; |
| 14583 | 15422 | pc->len -= (pc->p - pc->tstart); |
| 14584 | 15423 | return JIM_OK; |
| 14585 | 15424 | } |
| 15425 | + | |
| 14586 | 15426 | |
| 14587 | 15427 | static int JimParseExprIrrational(struct JimParserCtx *pc) |
| 14588 | 15428 | { |
| 14589 | 15429 | const char *irrationals[] = { "NaN", "nan", "NAN", "Inf", "inf", "INF", NULL }; |
| 14590 | 15430 | int i; |
| @@ -14600,10 +15440,11 @@ | ||
| 14600 | 15440 | return JIM_OK; |
| 14601 | 15441 | } |
| 14602 | 15442 | } |
| 14603 | 15443 | return JIM_ERR; |
| 14604 | 15444 | } |
| 15445 | + | |
| 14605 | 15446 | |
| 14606 | 15447 | static int JimParseExprBoolean(struct JimParserCtx *pc) |
| 14607 | 15448 | { |
| 14608 | 15449 | int i; |
| 14609 | 15450 | for (i = 0; i < sizeof(jim_true_false_strings) / sizeof(*jim_true_false_strings); i++) { |
| @@ -14624,10 +15465,11 @@ | ||
| 14624 | 15465 | if (opcode < JIM_TT_EXPR_OP) { |
| 14625 | 15466 | return &dummy_op; |
| 14626 | 15467 | } |
| 14627 | 15468 | return &Jim_ExprOperators[opcode - JIM_TT_EXPR_OP]; |
| 14628 | 15469 | } |
| 15470 | + | |
| 14629 | 15471 | |
| 14630 | 15472 | static int JimParseExprOperator(struct JimParserCtx *pc) |
| 14631 | 15473 | { |
| 14632 | 15474 | int i; |
| 14633 | 15475 | const struct Jim_ExprOperator *bestOp = NULL; |
| @@ -14691,10 +15533,11 @@ | ||
| 14691 | 15533 | struct JimExprNode *expr; |
| 14692 | 15534 | struct JimExprNode *nodes; |
| 14693 | 15535 | int len; |
| 14694 | 15536 | int inUse; |
| 14695 | 15537 | }; |
| 15538 | + | |
| 14696 | 15539 | |
| 14697 | 15540 | static void ExprTreeFreeNodes(Jim_Interp *interp, struct JimExprNode *nodes, int num) |
| 14698 | 15541 | { |
| 14699 | 15542 | int i; |
| 14700 | 15543 | for (i = 0; i < num; i++) { |
| @@ -14702,16 +15545,18 @@ | ||
| 14702 | 15545 | Jim_DecrRefCount(interp, nodes[i].objPtr); |
| 14703 | 15546 | } |
| 14704 | 15547 | } |
| 14705 | 15548 | Jim_Free(nodes); |
| 14706 | 15549 | } |
| 15550 | + | |
| 14707 | 15551 | |
| 14708 | 15552 | static void ExprTreeFree(Jim_Interp *interp, struct ExprTree *expr) |
| 14709 | 15553 | { |
| 14710 | 15554 | ExprTreeFreeNodes(interp, expr->nodes, expr->len); |
| 14711 | 15555 | Jim_Free(expr); |
| 14712 | 15556 | } |
| 15557 | + | |
| 14713 | 15558 | |
| 14714 | 15559 | static void FreeExprInternalRep(Jim_Interp *interp, Jim_Obj *objPtr) |
| 14715 | 15560 | { |
| 14716 | 15561 | struct ExprTree *expr = (void *)objPtr->internalRep.ptr; |
| 14717 | 15562 | |
| @@ -14721,10 +15566,11 @@ | ||
| 14721 | 15566 | } |
| 14722 | 15567 | |
| 14723 | 15568 | ExprTreeFree(interp, expr); |
| 14724 | 15569 | } |
| 14725 | 15570 | } |
| 15571 | + | |
| 14726 | 15572 | |
| 14727 | 15573 | static void DupExprInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr) |
| 14728 | 15574 | { |
| 14729 | 15575 | JIM_NOTUSED(interp); |
| 14730 | 15576 | JIM_NOTUSED(srcPtr); |
| @@ -14744,10 +15590,11 @@ | ||
| 14744 | 15590 | struct JimExprNode *nodes; |
| 14745 | 15591 | struct JimExprNode *next; |
| 14746 | 15592 | }; |
| 14747 | 15593 | |
| 14748 | 15594 | #ifdef DEBUG_SHOW_EXPR |
| 15595 | + | |
| 14749 | 15596 | static void JimShowExprNode(struct JimExprNode *node, int level) |
| 14750 | 15597 | { |
| 14751 | 15598 | int i; |
| 14752 | 15599 | for (i = 0; i < level; i++) { |
| 14753 | 15600 | printf(" "); |
| @@ -15029,11 +15876,11 @@ | ||
| 15029 | 15876 | builder.fileNameObj = fileNameObj; |
| 15030 | 15877 | |
| 15031 | 15878 | builder.nodes = Jim_Alloc(sizeof(struct JimExprNode) * (tokenlist->count - 1)); |
| 15032 | 15879 | memset(builder.nodes, 0, sizeof(struct JimExprNode) * (tokenlist->count - 1)); |
| 15033 | 15880 | builder.next = builder.nodes; |
| 15034 | - Jim_InitStack(&builder.stack); | |
| 15881 | + Jim_StackInit(&builder.stack, NULL); | |
| 15035 | 15882 | |
| 15036 | 15883 | rc = ExprTreeBuildTree(interp, &builder, 0, 0, 1); |
| 15037 | 15884 | |
| 15038 | 15885 | if (rc == JIM_OK) { |
| 15039 | 15886 | top = Jim_StackPop(&builder.stack); |
| @@ -15043,11 +15890,11 @@ | ||
| 15043 | 15890 | rc = JIM_ERR; |
| 15044 | 15891 | } |
| 15045 | 15892 | } |
| 15046 | 15893 | |
| 15047 | 15894 | |
| 15048 | - Jim_FreeStack(&builder.stack); | |
| 15895 | + Jim_StackFree(&builder.stack); | |
| 15049 | 15896 | |
| 15050 | 15897 | if (rc != JIM_OK) { |
| 15051 | 15898 | ExprTreeFreeNodes(interp, builder.nodes, builder.next - builder.nodes); |
| 15052 | 15899 | return NULL; |
| 15053 | 15900 | } |
| @@ -15078,10 +15925,13 @@ | ||
| 15078 | 15925 | fileNameObj = Jim_GetSourceInfo(interp, objPtr, &line); |
| 15079 | 15926 | Jim_IncrRefCount(fileNameObj); |
| 15080 | 15927 | |
| 15081 | 15928 | exprText = Jim_GetString(objPtr, &exprTextLen); |
| 15082 | 15929 | |
| 15930 | + int oldtaint = interp->taint; | |
| 15931 | + interp->taint = objPtr->taint; | |
| 15932 | + | |
| 15083 | 15933 | |
| 15084 | 15934 | ScriptTokenListInit(&tokenlist); |
| 15085 | 15935 | |
| 15086 | 15936 | JimParserInit(&parser, exprText, exprTextLen, line); |
| 15087 | 15937 | while (!parser.eof) { |
| @@ -15144,10 +15994,13 @@ | ||
| 15144 | 15994 | |
| 15145 | 15995 | Jim_DecrRefCount(interp, fileNameObj); |
| 15146 | 15996 | Jim_FreeIntRep(interp, objPtr); |
| 15147 | 15997 | Jim_SetIntRepPtr(objPtr, expr); |
| 15148 | 15998 | objPtr->typePtr = &exprObjType; |
| 15999 | + | |
| 16000 | + interp->taint = oldtaint; | |
| 16001 | + | |
| 15149 | 16002 | return rc; |
| 15150 | 16003 | } |
| 15151 | 16004 | |
| 15152 | 16005 | static struct ExprTree *JimGetExpression(Jim_Interp *interp, Jim_Obj *objPtr) |
| 15153 | 16006 | { |
| @@ -15228,10 +16081,11 @@ | ||
| 15228 | 16081 | |
| 15229 | 16082 | return JIM_ERR; |
| 15230 | 16083 | } |
| 15231 | 16084 | } |
| 15232 | 16085 | } |
| 16086 | + | |
| 15233 | 16087 | |
| 15234 | 16088 | static int JimExprGetTerm(Jim_Interp *interp, struct JimExprNode *node, Jim_Obj **objPtrPtr) |
| 15235 | 16089 | { |
| 15236 | 16090 | int rc = JimExprEvalTermNode(interp, node); |
| 15237 | 16091 | if (rc == JIM_OK) { |
| @@ -15238,10 +16092,11 @@ | ||
| 15238 | 16092 | *objPtrPtr = Jim_GetResult(interp); |
| 15239 | 16093 | Jim_IncrRefCount(*objPtrPtr); |
| 15240 | 16094 | } |
| 15241 | 16095 | return rc; |
| 15242 | 16096 | } |
| 16097 | + | |
| 15243 | 16098 | |
| 15244 | 16099 | static int JimExprGetTermBoolean(Jim_Interp *interp, struct JimExprNode *node) |
| 15245 | 16100 | { |
| 15246 | 16101 | if (JimExprEvalTermNode(interp, node) == JIM_OK) { |
| 15247 | 16102 | return ExprBool(interp, Jim_GetResult(interp)); |
| @@ -15418,10 +16273,11 @@ | ||
| 15418 | 16273 | JIM_NOTUSED(interp); |
| 15419 | 16274 | memcpy(newVec, srcPtr->internalRep.ptr, size); |
| 15420 | 16275 | dupPtr->internalRep.ptr = newVec; |
| 15421 | 16276 | dupPtr->typePtr = &scanFmtStringObjType; |
| 15422 | 16277 | } |
| 16278 | + | |
| 15423 | 16279 | |
| 15424 | 16280 | static void UpdateStringOfScanFmt(Jim_Obj *objPtr) |
| 15425 | 16281 | { |
| 15426 | 16282 | JimSetStringBytes(objPtr, ((ScanFmtStringObj *) objPtr->internalRep.ptr)->stringRep); |
| 15427 | 16283 | } |
| @@ -15920,14 +16776,10 @@ | ||
| 15920 | 16776 | static int Jim_IncrCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 15921 | 16777 | { |
| 15922 | 16778 | jim_wide wideValue, increment = 1; |
| 15923 | 16779 | Jim_Obj *intObjPtr; |
| 15924 | 16780 | |
| 15925 | - if (argc != 2 && argc != 3) { | |
| 15926 | - Jim_WrongNumArgs(interp, 1, argv, "varName ?increment?"); | |
| 15927 | - return JIM_ERR; | |
| 15928 | - } | |
| 15929 | 16781 | if (argc == 3) { |
| 15930 | 16782 | if (Jim_GetWideExpr(interp, argv[2], &increment) != JIM_OK) |
| 15931 | 16783 | return JIM_ERR; |
| 15932 | 16784 | } |
| 15933 | 16785 | intObjPtr = Jim_GetVariable(interp, argv[1], JIM_UNSHARED); |
| @@ -15939,11 +16791,10 @@ | ||
| 15939 | 16791 | return JIM_ERR; |
| 15940 | 16792 | } |
| 15941 | 16793 | if (!intObjPtr || Jim_IsShared(intObjPtr)) { |
| 15942 | 16794 | intObjPtr = Jim_NewIntObj(interp, wideValue + increment); |
| 15943 | 16795 | if (Jim_SetVariable(interp, argv[1], intObjPtr) != JIM_OK) { |
| 15944 | - Jim_FreeNewObj(interp, intObjPtr); | |
| 15945 | 16796 | return JIM_ERR; |
| 15946 | 16797 | } |
| 15947 | 16798 | } |
| 15948 | 16799 | else { |
| 15949 | 16800 | |
| @@ -16026,10 +16877,11 @@ | ||
| 16026 | 16877 | retcode = Jim_EvalObjPrefix(interp, interp->unknown, argc, argv); |
| 16027 | 16878 | interp->unknown_called--; |
| 16028 | 16879 | |
| 16029 | 16880 | return retcode; |
| 16030 | 16881 | } |
| 16882 | + | |
| 16031 | 16883 | |
| 16032 | 16884 | static void JimPushEvalFrame(Jim_Interp *interp, Jim_EvalFrame *frame, Jim_Obj *scriptObj) |
| 16033 | 16885 | { |
| 16034 | 16886 | memset(frame, 0, sizeof(*frame)); |
| 16035 | 16887 | frame->parent = interp->evalFrame; |
| @@ -16047,10 +16899,11 @@ | ||
| 16047 | 16899 | if (frame->scriptObj) { |
| 16048 | 16900 | printf("script: %.*s\n", 20, Jim_String(frame->scriptObj)); |
| 16049 | 16901 | } |
| 16050 | 16902 | #endif |
| 16051 | 16903 | } |
| 16904 | + | |
| 16052 | 16905 | |
| 16053 | 16906 | static void JimPopEvalFrame(Jim_Interp *interp) |
| 16054 | 16907 | { |
| 16055 | 16908 | interp->evalFrame = interp->evalFrame->parent; |
| 16056 | 16909 | } |
| @@ -16094,16 +16947,21 @@ | ||
| 16094 | 16947 | |
| 16095 | 16948 | if (!interp->traceCmdObj || |
| 16096 | 16949 | (retcode = JimTraceCallback(interp, "cmd", objc, objv)) == JIM_OK) { |
| 16097 | 16950 | |
| 16098 | 16951 | Jim_SetEmptyResult(interp); |
| 16099 | - if (cmdPtr->isproc) { | |
| 16952 | + interp->taint = Jim_CalcTaint(objc, objv); | |
| 16953 | + if (cmdPtr->flags & JIM_CMD_ISPROC) { | |
| 16100 | 16954 | retcode = JimCallProcedure(interp, cmdPtr, objc, objv); |
| 16101 | 16955 | } |
| 16956 | + else if ((cmdPtr->flags & JIM_CMD_NOTAINT) && Jim_CheckTaint(interp, JIM_TAINT_ANY)) { | |
| 16957 | + Jim_SetTaintError(interp, 1, objv); | |
| 16958 | + retcode = JIM_ERR; | |
| 16959 | + } | |
| 16102 | 16960 | else { |
| 16103 | - interp->cmdPrivData = cmdPtr->u.native.privData; | |
| 16104 | - retcode = cmdPtr->u.native.cmdProc(interp, objc, objv); | |
| 16961 | + retcode = JimCallNative(interp, cmdPtr, objc, objv); | |
| 16962 | + | |
| 16105 | 16963 | } |
| 16106 | 16964 | if (retcode == JIM_ERR) { |
| 16107 | 16965 | JimSetErrorStack(interp, NULL); |
| 16108 | 16966 | } |
| 16109 | 16967 | } |
| @@ -16184,10 +17042,11 @@ | ||
| 16184 | 17042 | memcpy(&nargv[1], &objv[0], sizeof(nargv[0]) * objc); |
| 16185 | 17043 | ret = Jim_EvalObjVector(interp, objc + 1, nargv); |
| 16186 | 17044 | Jim_Free(nargv); |
| 16187 | 17045 | return ret; |
| 16188 | 17046 | } |
| 17047 | + | |
| 16189 | 17048 | |
| 16190 | 17049 | static int JimSubstOneToken(Jim_Interp *interp, const ScriptToken *token, Jim_Obj **objPtrPtr) |
| 16191 | 17050 | { |
| 16192 | 17051 | Jim_Obj *objPtr; |
| 16193 | 17052 | int ret = JIM_ERR; |
| @@ -16239,10 +17098,12 @@ | ||
| 16239 | 17098 | int totlen = 0, i; |
| 16240 | 17099 | Jim_Obj **intv; |
| 16241 | 17100 | Jim_Obj *sintv[JIM_EVAL_SINTV_LEN]; |
| 16242 | 17101 | Jim_Obj *objPtr; |
| 16243 | 17102 | char *s; |
| 17103 | + int taint = 0; | |
| 17104 | + const char *error_action = NULL; | |
| 16244 | 17105 | |
| 16245 | 17106 | if (tokens <= JIM_EVAL_SINTV_LEN) |
| 16246 | 17107 | intv = sintv; |
| 16247 | 17108 | else |
| 16248 | 17109 | intv = Jim_Alloc(sizeof(Jim_Obj *) * tokens); |
| @@ -16256,28 +17117,34 @@ | ||
| 16256 | 17117 | if (flags & JIM_SUBST_FLAG) { |
| 16257 | 17118 | |
| 16258 | 17119 | tokens = i; |
| 16259 | 17120 | continue; |
| 16260 | 17121 | } |
| 16261 | - | |
| 17122 | + error_action = "break"; | |
| 16262 | 17123 | |
| 16263 | 17124 | case JIM_CONTINUE: |
| 16264 | 17125 | if (flags & JIM_SUBST_FLAG) { |
| 16265 | 17126 | intv[i] = NULL; |
| 16266 | 17127 | continue; |
| 16267 | 17128 | } |
| 16268 | - | |
| 17129 | + if (!error_action) { | |
| 17130 | + error_action = "continue"; | |
| 17131 | + } | |
| 16269 | 17132 | |
| 16270 | 17133 | default: |
| 16271 | 17134 | while (i--) { |
| 16272 | 17135 | Jim_DecrRefCount(interp, intv[i]); |
| 16273 | 17136 | } |
| 16274 | 17137 | if (intv != sintv) { |
| 16275 | 17138 | Jim_Free(intv); |
| 16276 | 17139 | } |
| 17140 | + if (error_action) { | |
| 17141 | + Jim_SetResultFormatted(interp, "invoked \"%s\" outside of a loop", error_action); | |
| 17142 | + } | |
| 16277 | 17143 | return NULL; |
| 16278 | 17144 | } |
| 17145 | + taint |= intv[i]->taint; | |
| 16279 | 17146 | Jim_IncrRefCount(intv[i]); |
| 16280 | 17147 | Jim_String(intv[i]); |
| 16281 | 17148 | totlen += intv[i]->length; |
| 16282 | 17149 | } |
| 16283 | 17150 | |
| @@ -16287,10 +17154,11 @@ | ||
| 16287 | 17154 | intv[0]->refCount--; |
| 16288 | 17155 | return intv[0]; |
| 16289 | 17156 | } |
| 16290 | 17157 | |
| 16291 | 17158 | objPtr = Jim_NewStringObjNoAlloc(interp, NULL, 0); |
| 17159 | + objPtr->taint = taint; | |
| 16292 | 17160 | |
| 16293 | 17161 | if (tokens == 4 && token[0].type == JIM_TT_ESC && token[1].type == JIM_TT_ESC |
| 16294 | 17162 | && token[2].type == JIM_TT_VAR) { |
| 16295 | 17163 | |
| 16296 | 17164 | objPtr->typePtr = &interpolatedObjType; |
| @@ -16322,10 +17190,111 @@ | ||
| 16322 | 17190 | } |
| 16323 | 17191 | |
| 16324 | 17192 | return objPtr; |
| 16325 | 17193 | } |
| 16326 | 17194 | |
| 17195 | +#define JIM_LSUBST_LINE 0x0001 | |
| 17196 | + | |
| 17197 | +static int JimListSubstObj(Jim_Interp *interp, struct Jim_Obj *objPtr, unsigned flags) | |
| 17198 | +{ | |
| 17199 | + int i; | |
| 17200 | + ScriptObj *script; | |
| 17201 | + ScriptToken *token; | |
| 17202 | + Jim_Obj *resultListObj; | |
| 17203 | + int retcode = JIM_OK; | |
| 17204 | + | |
| 17205 | + Jim_IncrRefCount(objPtr); | |
| 17206 | + script = JimGetScript(interp, objPtr); | |
| 17207 | + if (JimParseCheckMissing(interp, script->missing) == JIM_ERR) { | |
| 17208 | + JimSetErrorStack(interp, script); | |
| 17209 | + Jim_DecrRefCount(interp, objPtr); | |
| 17210 | + return JIM_ERR; | |
| 17211 | + } | |
| 17212 | + | |
| 17213 | + token = script->token; | |
| 17214 | + | |
| 17215 | + script->inUse++; | |
| 17216 | + | |
| 17217 | + | |
| 17218 | + resultListObj = Jim_NewListObj(interp, NULL, 0); | |
| 17219 | + | |
| 17220 | + | |
| 17221 | + for (i = 0; i < script->len && retcode == JIM_OK; ) { | |
| 17222 | + int argc; | |
| 17223 | + int j; | |
| 17224 | + Jim_Obj *lineListObj = resultListObj; | |
| 17225 | + | |
| 17226 | + | |
| 17227 | + argc = token[i].objPtr->internalRep.scriptLineValue.argc; | |
| 17228 | + script->linenr = token[i].objPtr->internalRep.scriptLineValue.line; | |
| 17229 | + | |
| 17230 | + | |
| 17231 | + i++; | |
| 17232 | + | |
| 17233 | + if (flags & JIM_LSUBST_LINE) { | |
| 17234 | + lineListObj = Jim_NewListObj(interp, NULL, 0); | |
| 17235 | + } | |
| 17236 | + | |
| 17237 | + | |
| 17238 | + for (j = 0; j < argc; j++) { | |
| 17239 | + long wordtokens = 1; | |
| 17240 | + int expand = 0; | |
| 17241 | + Jim_Obj *wordObjPtr = NULL; | |
| 17242 | + | |
| 17243 | + if (token[i].type == JIM_TT_WORD) { | |
| 17244 | + wordtokens = JimWideValue(token[i++].objPtr); | |
| 17245 | + if (wordtokens < 0) { | |
| 17246 | + expand = 1; | |
| 17247 | + wordtokens = -wordtokens; | |
| 17248 | + } | |
| 17249 | + } | |
| 17250 | + | |
| 17251 | + | |
| 17252 | + wordObjPtr = JimInterpolateTokens(interp, token + i, wordtokens, JIM_NONE); | |
| 17253 | + | |
| 17254 | + if (!wordObjPtr) { | |
| 17255 | + if (retcode == JIM_OK) { | |
| 17256 | + retcode = JIM_ERR; | |
| 17257 | + } | |
| 17258 | + break; | |
| 17259 | + } | |
| 17260 | + | |
| 17261 | + Jim_IncrRefCount(wordObjPtr); | |
| 17262 | + i += wordtokens; | |
| 17263 | + | |
| 17264 | + if (!expand) { | |
| 17265 | + Jim_ListAppendElement(interp, lineListObj, wordObjPtr); | |
| 17266 | + } | |
| 17267 | + else { | |
| 17268 | + int k; | |
| 17269 | + | |
| 17270 | + for (k = 0; k < Jim_ListLength(interp, wordObjPtr); k++) { | |
| 17271 | + Jim_ListAppendElement(interp, lineListObj, Jim_ListGetIndex(interp, wordObjPtr, k)); | |
| 17272 | + } | |
| 17273 | + } | |
| 17274 | + Jim_DecrRefCount(interp, wordObjPtr); | |
| 17275 | + } | |
| 17276 | + | |
| 17277 | + if (flags & JIM_LSUBST_LINE) { | |
| 17278 | + Jim_ListAppendElement(interp, resultListObj, lineListObj); | |
| 17279 | + } | |
| 17280 | + } | |
| 17281 | + | |
| 17282 | + Jim_FreeIntRep(interp, objPtr); | |
| 17283 | + objPtr->typePtr = &scriptObjType; | |
| 17284 | + Jim_SetIntRepPtr(objPtr, script); | |
| 17285 | + Jim_DecrRefCount(interp, objPtr); | |
| 17286 | + | |
| 17287 | + if (retcode == JIM_OK) { | |
| 17288 | + Jim_SetResult(interp, resultListObj); | |
| 17289 | + } | |
| 17290 | + else { | |
| 17291 | + Jim_FreeNewObj(interp, resultListObj); | |
| 17292 | + } | |
| 17293 | + | |
| 17294 | + return retcode; | |
| 17295 | +} | |
| 16327 | 17296 | |
| 16328 | 17297 | static int JimEvalObjList(Jim_Interp *interp, Jim_Obj *listPtr) |
| 16329 | 17298 | { |
| 16330 | 17299 | int retcode = JIM_OK; |
| 16331 | 17300 | Jim_EvalFrame frame; |
| @@ -16383,11 +17352,11 @@ | ||
| 16383 | 17352 | Jim_DecrRefCount(interp, scriptObjPtr); |
| 16384 | 17353 | return JIM_OK; |
| 16385 | 17354 | } |
| 16386 | 17355 | if (script->len == 3 |
| 16387 | 17356 | && token[1].objPtr->typePtr == &commandObjType |
| 16388 | - && token[1].objPtr->internalRep.cmdValue.cmdPtr->isproc == 0 | |
| 17357 | + && (token[1].objPtr->internalRep.cmdValue.cmdPtr->flags & JIM_CMD_ISPROC) == 0 | |
| 16389 | 17358 | && token[1].objPtr->internalRep.cmdValue.cmdPtr->u.native.cmdProc == Jim_IncrCoreCommand |
| 16390 | 17359 | && token[2].objPtr->typePtr == &variableObjType) { |
| 16391 | 17360 | |
| 16392 | 17361 | Jim_Obj *objPtr = Jim_GetVariable(interp, token[2].objPtr, JIM_NONE); |
| 16393 | 17362 | |
| @@ -16404,11 +17373,11 @@ | ||
| 16404 | 17373 | script->inUse++; |
| 16405 | 17374 | |
| 16406 | 17375 | JimPushEvalFrame(interp, &frame, scriptObjPtr); |
| 16407 | 17376 | |
| 16408 | 17377 | |
| 16409 | - interp->errorFlag = 0; | |
| 17378 | + interp->hasErrorStackTrace = 0; | |
| 16410 | 17379 | argv = sargv; |
| 16411 | 17380 | |
| 16412 | 17381 | for (i = 0; i < script->len && retcode == JIM_OK; ) { |
| 16413 | 17382 | int argc; |
| 16414 | 17383 | int j; |
| @@ -16520,10 +17489,11 @@ | ||
| 16520 | 17489 | } |
| 16521 | 17490 | |
| 16522 | 17491 | if (retcode == JIM_OK && argc) { |
| 16523 | 17492 | |
| 16524 | 17493 | retcode = JimInvokeCommand(interp, argc, argv); |
| 17494 | + interp->taint = 0; | |
| 16525 | 17495 | |
| 16526 | 17496 | if (Jim_CheckSignal(interp)) { |
| 16527 | 17497 | retcode = JIM_SIGNAL; |
| 16528 | 17498 | } |
| 16529 | 17499 | } |
| @@ -16551,10 +17521,11 @@ | ||
| 16551 | 17521 | Jim_SetIntRepPtr(scriptObjPtr, script); |
| 16552 | 17522 | Jim_DecrRefCount(interp, scriptObjPtr); |
| 16553 | 17523 | |
| 16554 | 17524 | return retcode; |
| 16555 | 17525 | } |
| 17526 | + | |
| 16556 | 17527 | |
| 16557 | 17528 | static int JimSetProcArg(Jim_Interp *interp, Jim_Obj *argNameObj, Jim_Obj *argValObj) |
| 16558 | 17529 | { |
| 16559 | 17530 | int retcode; |
| 16560 | 17531 | |
| @@ -16580,48 +17551,63 @@ | ||
| 16580 | 17551 | else { |
| 16581 | 17552 | retcode = Jim_SetVariable(interp, argNameObj, argValObj); |
| 16582 | 17553 | } |
| 16583 | 17554 | return retcode; |
| 16584 | 17555 | } |
| 17556 | + | |
| 17557 | +static Jim_Obj *JimCmdUsage(Jim_Interp *interp, Jim_Obj *cmdNameObj, Jim_Cmd *cmd) | |
| 17558 | +{ | |
| 17559 | + Jim_Obj *usage = Jim_DuplicateObj(interp, cmdNameObj); | |
| 17560 | + | |
| 17561 | + if (cmd->flags & JIM_CMD_ISPROC) { | |
| 17562 | + int i; | |
| 17563 | + for (i = 0; i < cmd->u.proc.argListLen; i++) { | |
| 17564 | + Jim_AppendString(interp, usage, " ", 1); | |
| 17565 | + | |
| 17566 | + if (i == cmd->u.proc.argsPos) { | |
| 17567 | + if (cmd->u.proc.arglist[i].defaultObjPtr) { | |
| 17568 | + | |
| 17569 | + Jim_AppendString(interp, usage, "?", 1); | |
| 17570 | + Jim_AppendObj(interp, usage, cmd->u.proc.arglist[i].defaultObjPtr); | |
| 17571 | + Jim_AppendString(interp, usage, " ...?", -1); | |
| 17572 | + } | |
| 17573 | + else { | |
| 17574 | + | |
| 17575 | + Jim_AppendString(interp, usage, "?arg ...?", -1); | |
| 17576 | + } | |
| 17577 | + } | |
| 17578 | + else { | |
| 17579 | + if (cmd->u.proc.arglist[i].defaultObjPtr) { | |
| 17580 | + Jim_AppendString(interp, usage, "?", 1); | |
| 17581 | + Jim_AppendObj(interp, usage, cmd->u.proc.arglist[i].nameObjPtr); | |
| 17582 | + Jim_AppendString(interp, usage, "?", 1); | |
| 17583 | + } | |
| 17584 | + else { | |
| 17585 | + const char *arg = Jim_String(cmd->u.proc.arglist[i].nameObjPtr); | |
| 17586 | + if (*arg == '&') { | |
| 17587 | + arg++; | |
| 17588 | + } | |
| 17589 | + Jim_AppendString(interp, usage, arg, -1); | |
| 17590 | + } | |
| 17591 | + } | |
| 17592 | + } | |
| 17593 | + } | |
| 17594 | + else if (cmd->u.native.usage) { | |
| 17595 | + if (*cmd->u.native.usage) { | |
| 17596 | + Jim_AppendStrings(interp, usage, " ", cmd->u.native.usage, NULL); | |
| 17597 | + } | |
| 17598 | + } | |
| 17599 | + else { | |
| 17600 | + Jim_AppendString(interp, usage, " ...", -1); | |
| 17601 | + } | |
| 17602 | + | |
| 17603 | + return usage; | |
| 17604 | +} | |
| 16585 | 17605 | |
| 16586 | 17606 | static void JimSetProcWrongArgs(Jim_Interp *interp, Jim_Obj *procNameObj, Jim_Cmd *cmd) |
| 16587 | 17607 | { |
| 16588 | - | |
| 16589 | - Jim_Obj *argmsg = Jim_NewStringObj(interp, "", 0); | |
| 16590 | - int i; | |
| 16591 | - | |
| 16592 | - for (i = 0; i < cmd->u.proc.argListLen; i++) { | |
| 16593 | - Jim_AppendString(interp, argmsg, " ", 1); | |
| 16594 | - | |
| 16595 | - if (i == cmd->u.proc.argsPos) { | |
| 16596 | - if (cmd->u.proc.arglist[i].defaultObjPtr) { | |
| 16597 | - | |
| 16598 | - Jim_AppendString(interp, argmsg, "?", 1); | |
| 16599 | - Jim_AppendObj(interp, argmsg, cmd->u.proc.arglist[i].defaultObjPtr); | |
| 16600 | - Jim_AppendString(interp, argmsg, " ...?", -1); | |
| 16601 | - } | |
| 16602 | - else { | |
| 16603 | - | |
| 16604 | - Jim_AppendString(interp, argmsg, "?arg ...?", -1); | |
| 16605 | - } | |
| 16606 | - } | |
| 16607 | - else { | |
| 16608 | - if (cmd->u.proc.arglist[i].defaultObjPtr) { | |
| 16609 | - Jim_AppendString(interp, argmsg, "?", 1); | |
| 16610 | - Jim_AppendObj(interp, argmsg, cmd->u.proc.arglist[i].nameObjPtr); | |
| 16611 | - Jim_AppendString(interp, argmsg, "?", 1); | |
| 16612 | - } | |
| 16613 | - else { | |
| 16614 | - const char *arg = Jim_String(cmd->u.proc.arglist[i].nameObjPtr); | |
| 16615 | - if (*arg == '&') { | |
| 16616 | - arg++; | |
| 16617 | - } | |
| 16618 | - Jim_AppendString(interp, argmsg, arg, -1); | |
| 16619 | - } | |
| 16620 | - } | |
| 16621 | - } | |
| 16622 | - Jim_SetResultFormatted(interp, "wrong # args: should be \"%#s%#s\"", procNameObj, argmsg); | |
| 17608 | + Jim_SetResultFormatted(interp, "wrong # args: should be \"%#s\"", JimCmdUsage(interp, procNameObj, cmd)); | |
| 16623 | 17609 | } |
| 16624 | 17610 | |
| 16625 | 17611 | #ifdef jim_ext_namespace |
| 16626 | 17612 | int Jim_EvalNamespace(Jim_Interp *interp, Jim_Obj *scriptObj, Jim_Obj *nsObj) |
| 16627 | 17613 | { |
| @@ -16758,10 +17744,40 @@ | ||
| 16758 | 17744 | } |
| 16759 | 17745 | interp->procLevel--; |
| 16760 | 17746 | |
| 16761 | 17747 | return retcode; |
| 16762 | 17748 | } |
| 17749 | + | |
| 17750 | +static int JimCallNative(Jim_Interp *interp, Jim_Cmd *cmd, int argc, Jim_Obj *const *argv) | |
| 17751 | +{ | |
| 17752 | + int argsok = 1; | |
| 17753 | + int ret; | |
| 17754 | + | |
| 17755 | + | |
| 17756 | + if (argc - 1 < cmd->u.native.minargs) { | |
| 17757 | + argsok = 0; | |
| 17758 | + } | |
| 17759 | + else if (cmd->u.native.maxargs >= 0 && argc - 1 > cmd->u.native.maxargs) { | |
| 17760 | + argsok = 0; | |
| 17761 | + } | |
| 17762 | + else if (cmd->u.native.maxargs < -1 && (argc - 1) % -cmd->u.native.maxargs != 0) { | |
| 17763 | + | |
| 17764 | + argsok = 0; | |
| 17765 | + } | |
| 17766 | + if (argsok) { | |
| 17767 | + interp->cmdPrivData = cmd->u.native.privData; | |
| 17768 | + ret = cmd->u.native.cmdProc(interp, argc, argv); | |
| 17769 | + if (ret != JIM_USAGE) { | |
| 17770 | + return ret; | |
| 17771 | + } | |
| 17772 | + | |
| 17773 | + } | |
| 17774 | + | |
| 17775 | + //printf("Wrong args for %s, argc=%d, minargs=%d, maxargs=%d\n", Jim_String(argv[0]), argc, cmd->u.native.minargs, cmd->u.native.maxargs); | |
| 17776 | + Jim_SetResultFormatted(interp, "wrong # args: should be \"%#s\"", JimCmdUsage(interp, argv[0], cmd)); | |
| 17777 | + return JIM_ERR; | |
| 17778 | +} | |
| 16763 | 17779 | |
| 16764 | 17780 | int Jim_EvalSource(Jim_Interp *interp, const char *filename, int lineno, const char *script) |
| 16765 | 17781 | { |
| 16766 | 17782 | int retval; |
| 16767 | 17783 | Jim_Obj *scriptObjPtr; |
| @@ -16805,56 +17821,43 @@ | ||
| 16805 | 17821 | |
| 16806 | 17822 | return retval; |
| 16807 | 17823 | } |
| 16808 | 17824 | |
| 16809 | 17825 | #include <sys/stat.h> |
| 16810 | - | |
| 16811 | -static Jim_Obj *JimReadTextFile(Jim_Interp *interp, const char *filename) | |
| 16812 | -{ | |
| 16813 | - jim_stat_t sb; | |
| 16814 | - int fd; | |
| 16815 | - char *buf; | |
| 16816 | - int readlen; | |
| 16817 | - | |
| 16818 | - if (Jim_Stat(filename, &sb) == -1 || (fd = open(filename, O_RDONLY | O_TEXT, 0666)) < 0) { | |
| 16819 | - Jim_SetResultFormatted(interp, "couldn't read file \"%s\": %s", filename, strerror(errno)); | |
| 16820 | - return NULL; | |
| 16821 | - } | |
| 16822 | - buf = Jim_Alloc(sb.st_size + 1); | |
| 16823 | - readlen = read(fd, buf, sb.st_size); | |
| 16824 | - close(fd); | |
| 16825 | - if (readlen < 0) { | |
| 16826 | - Jim_Free(buf); | |
| 16827 | - Jim_SetResultFormatted(interp, "failed to load file \"%s\": %s", filename, strerror(errno)); | |
| 16828 | - return NULL; | |
| 16829 | - } | |
| 16830 | - else { | |
| 16831 | - Jim_Obj *objPtr; | |
| 16832 | - buf[readlen] = 0; | |
| 16833 | - | |
| 16834 | - objPtr = Jim_NewStringObjNoAlloc(interp, buf, readlen); | |
| 16835 | - | |
| 16836 | - return objPtr; | |
| 16837 | - } | |
| 16838 | -} | |
| 16839 | - | |
| 16840 | 17826 | |
| 16841 | 17827 | int Jim_EvalFile(Jim_Interp *interp, const char *filename) |
| 16842 | 17828 | { |
| 16843 | - Jim_Obj *filenameObj; | |
| 16844 | - Jim_Obj *oldFilenameObj; | |
| 17829 | + FILE *fp; | |
| 17830 | + char *buf; | |
| 16845 | 17831 | Jim_Obj *scriptObjPtr; |
| 16846 | - int retcode; | |
| 17832 | + Jim_Obj *filenameObj, *oldFilenameObj; | |
| 17833 | + int retcode = JIM_ERR; | |
| 17834 | + int readlen; | |
| 17835 | +#define READ_BUF_SIZE 256 | |
| 16847 | 17836 | |
| 16848 | - scriptObjPtr = JimReadTextFile(interp, filename); | |
| 16849 | - if (!scriptObjPtr) { | |
| 17837 | + if ((fp = fopen(filename, "rt")) == NULL) { | |
| 17838 | + Jim_SetResultFormatted(interp, "couldn't read file \"%s\": %s", filename, strerror(errno)); | |
| 16850 | 17839 | return JIM_ERR; |
| 16851 | 17840 | } |
| 17841 | + scriptObjPtr = Jim_NewStringObj(interp, NULL, 0); | |
| 17842 | + | |
| 17843 | + buf = Jim_Alloc(READ_BUF_SIZE); | |
| 17844 | + while ((readlen = fread(buf, 1, READ_BUF_SIZE, fp)) > 0) { | |
| 17845 | + Jim_AppendString(interp, scriptObjPtr, buf, readlen); | |
| 17846 | + } | |
| 17847 | + Jim_Free(buf); | |
| 17848 | + if (ferror(fp)) { | |
| 17849 | + fclose(fp); | |
| 17850 | + Jim_SetResultFormatted(interp, "failed to load file \"%s\": %s", filename, strerror(errno)); | |
| 17851 | + Jim_FreeNewObj(interp, scriptObjPtr); | |
| 17852 | + return retcode; | |
| 17853 | + } | |
| 17854 | + fclose(fp); | |
| 17855 | + | |
| 16852 | 17856 | |
| 16853 | 17857 | filenameObj = Jim_NewStringObj(interp, filename, -1); |
| 16854 | 17858 | Jim_SetSourceInfo(interp, scriptObjPtr, filenameObj, 1); |
| 16855 | - | |
| 16856 | 17859 | oldFilenameObj = JimPushInterpObj(interp->currentFilenameObj, filenameObj); |
| 16857 | 17860 | |
| 16858 | 17861 | retcode = Jim_EvalObj(interp, scriptObjPtr); |
| 16859 | 17862 | |
| 16860 | 17863 | JimPopInterpObj(interp, interp->currentFilenameObj, oldFilenameObj); |
| @@ -16920,10 +17923,11 @@ | ||
| 16920 | 17923 | int scriptTextLen; |
| 16921 | 17924 | const char *scriptText = Jim_GetString(objPtr, &scriptTextLen); |
| 16922 | 17925 | struct JimParserCtx parser; |
| 16923 | 17926 | struct ScriptObj *script = Jim_Alloc(sizeof(*script)); |
| 16924 | 17927 | ParseTokenList tokenlist; |
| 17928 | + int oldtaint; | |
| 16925 | 17929 | |
| 16926 | 17930 | |
| 16927 | 17931 | ScriptTokenListInit(&tokenlist); |
| 16928 | 17932 | |
| 16929 | 17933 | JimParserInit(&parser, scriptText, scriptTextLen, 1); |
| @@ -16935,10 +17939,13 @@ | ||
| 16935 | 17939 | } |
| 16936 | 17940 | ScriptAddToken(&tokenlist, parser.tstart, parser.tend - parser.tstart + 1, parser.tt, |
| 16937 | 17941 | parser.tline); |
| 16938 | 17942 | } |
| 16939 | 17943 | |
| 17944 | + oldtaint = interp->taint; | |
| 17945 | + interp->taint = objPtr->taint; | |
| 17946 | + | |
| 16940 | 17947 | |
| 16941 | 17948 | script->inUse = 1; |
| 16942 | 17949 | script->substFlags = flags; |
| 16943 | 17950 | script->fileNameObj = interp->emptyObj; |
| 16944 | 17951 | Jim_IncrRefCount(script->fileNameObj); |
| @@ -16961,10 +17968,11 @@ | ||
| 16961 | 17968 | |
| 16962 | 17969 | |
| 16963 | 17970 | Jim_FreeIntRep(interp, objPtr); |
| 16964 | 17971 | Jim_SetIntRepPtr(objPtr, script); |
| 16965 | 17972 | objPtr->typePtr = &scriptObjType; |
| 17973 | + interp->taint = oldtaint; | |
| 16966 | 17974 | return JIM_OK; |
| 16967 | 17975 | } |
| 16968 | 17976 | |
| 16969 | 17977 | static ScriptObj *Jim_GetSubst(Jim_Interp *interp, Jim_Obj *objPtr, int flags) |
| 16970 | 17978 | { |
| @@ -16992,27 +18000,59 @@ | ||
| 16992 | 18000 | return JIM_ERR; |
| 16993 | 18001 | } |
| 16994 | 18002 | return JIM_OK; |
| 16995 | 18003 | } |
| 16996 | 18004 | |
| 16997 | -void Jim_WrongNumArgs(Jim_Interp *interp, int argc, Jim_Obj *const *argv, const char *msg) | |
| 18005 | +static Jim_Obj *JimJoinCmdArgs(Jim_Interp *interp, int argc, Jim_Obj *const *argv) | |
| 16998 | 18006 | { |
| 16999 | 18007 | Jim_Obj *objPtr; |
| 17000 | 18008 | Jim_Obj *listObjPtr; |
| 17001 | 18009 | |
| 17002 | - JimPanic((argc == 0, "Jim_WrongNumArgs() called with argc=0")); | |
| 17003 | - | |
| 17004 | 18010 | listObjPtr = Jim_NewListObj(interp, argv, argc); |
| 17005 | 18011 | |
| 17006 | - if (msg && *msg) { | |
| 17007 | - Jim_ListAppendElement(interp, listObjPtr, Jim_NewStringObj(interp, msg, -1)); | |
| 17008 | - } | |
| 17009 | 18012 | Jim_IncrRefCount(listObjPtr); |
| 17010 | 18013 | objPtr = Jim_ListJoin(interp, listObjPtr, " ", 1); |
| 17011 | 18014 | Jim_DecrRefCount(interp, listObjPtr); |
| 18015 | + Jim_IncrRefCount(objPtr); | |
| 17012 | 18016 | |
| 17013 | - Jim_SetResultFormatted(interp, "wrong # args: should be \"%#s\"", objPtr); | |
| 18017 | + return objPtr; | |
| 18018 | +} | |
| 18019 | + | |
| 18020 | +void Jim_WrongNumArgs(Jim_Interp *interp, int argc, Jim_Obj *const *argv, const char *msg) | |
| 18021 | +{ | |
| 18022 | + Jim_Obj *objPtr = JimJoinCmdArgs(interp, argc, argv); | |
| 18023 | + if (*msg) { | |
| 18024 | + Jim_SetResultFormatted(interp, "wrong # args: should be \"%#s %s\"", objPtr, msg); | |
| 18025 | + } | |
| 18026 | + else { | |
| 18027 | + Jim_SetResultFormatted(interp, "wrong # args: should be \"%#s\"", objPtr); | |
| 18028 | + } | |
| 18029 | + Jim_DecrRefCount(interp, objPtr); | |
| 18030 | +} | |
| 18031 | + | |
| 18032 | +int Jim_CalcTaint(int argc, Jim_Obj *const *argv) | |
| 18033 | +{ | |
| 18034 | + int taint = 0; | |
| 18035 | +#ifdef JIM_TAINT | |
| 18036 | + int i; | |
| 18037 | + for (i = 0; i < argc; i++) { | |
| 18038 | + if (argv[i]) { | |
| 18039 | + taint |= argv[i]->taint; | |
| 18040 | + } | |
| 18041 | + } | |
| 18042 | +#endif | |
| 18043 | + return taint; | |
| 18044 | +} | |
| 18045 | + | |
| 18046 | +void Jim_SetTaintError(Jim_Interp *interp, int cmdargs, Jim_Obj *const *argv) | |
| 18047 | +{ | |
| 18048 | +#ifdef JIM_TAINT | |
| 18049 | + Jim_Obj *objPtr = JimJoinCmdArgs(interp, cmdargs, argv); | |
| 18050 | + Jim_SetResultFormatted(interp, "%#s: tainted data", objPtr); | |
| 18051 | + Jim_DecrRefCount(interp, objPtr); | |
| 18052 | + Jim_SetGlobalVariableStr(interp, "errorCode", Jim_NewStringObj(interp, "TAINTED", -1)); | |
| 18053 | +#endif | |
| 17014 | 18054 | } |
| 17015 | 18055 | |
| 17016 | 18056 | typedef void JimHashtableIteratorCallbackType(Jim_Interp *interp, Jim_Obj *listObjPtr, |
| 17017 | 18057 | Jim_Obj *keyObjPtr, void *value, Jim_Obj *patternObjPtr, int type); |
| 17018 | 18058 | |
| @@ -17042,42 +18082,55 @@ | ||
| 17042 | 18082 | } |
| 17043 | 18083 | return listObjPtr; |
| 17044 | 18084 | } |
| 17045 | 18085 | |
| 17046 | 18086 | |
| 17047 | -#define JIM_CMDLIST_COMMANDS 0 | |
| 17048 | -#define JIM_CMDLIST_PROCS 1 | |
| 17049 | -#define JIM_CMDLIST_CHANNELS 2 | |
| 18087 | +#define JIM_CMDLIST_COMMANDS 1 | |
| 18088 | +#define JIM_CMDLIST_PROCS 2 | |
| 18089 | +#define JIM_CMDLIST_ALIASES 4 | |
| 18090 | +#define JIM_CMDLIST_CHANNELS 8 | |
| 18091 | + | |
| 18092 | +#define JIM_CMDLIST_ALL 0x1000 | |
| 17050 | 18093 | |
| 17051 | 18094 | static void JimCommandMatch(Jim_Interp *interp, Jim_Obj *listObjPtr, |
| 17052 | 18095 | Jim_Obj *keyObj, void *value, Jim_Obj *patternObj, int type) |
| 17053 | 18096 | { |
| 17054 | 18097 | Jim_Cmd *cmdPtr = (Jim_Cmd *)value; |
| 18098 | + int match = 1; | |
| 17055 | 18099 | |
| 17056 | - if (type == JIM_CMDLIST_PROCS && !cmdPtr->isproc) { | |
| 18100 | + if ((type & JIM_CMDLIST_PROCS) && !(cmdPtr->flags & JIM_CMD_ISPROC)) { | |
| 18101 | + | |
| 18102 | + return; | |
| 18103 | + } | |
| 18104 | + if ((type & JIM_CMDLIST_CHANNELS) && !(cmdPtr->flags & JIM_CMD_ISCHANNEL)) { | |
| 18105 | + | |
| 18106 | + return; | |
| 18107 | + } | |
| 18108 | + if ((type & JIM_CMDLIST_ALIASES) && !(cmdPtr->flags & JIM_CMD_ISALIAS)) { | |
| 18109 | + | |
| 18110 | + return; | |
| 18111 | + } | |
| 18112 | + if (!(type & JIM_CMDLIST_ALL) && strchr(Jim_String(keyObj), ' ')) { | |
| 17057 | 18113 | |
| 17058 | 18114 | return; |
| 17059 | 18115 | } |
| 17060 | 18116 | |
| 17061 | 18117 | Jim_IncrRefCount(keyObj); |
| 17062 | 18118 | |
| 17063 | - if (type != JIM_CMDLIST_CHANNELS || Jim_AioFilehandle(interp, keyObj) >= 0) { | |
| 17064 | - int match = 1; | |
| 17065 | - if (patternObj) { | |
| 17066 | - int plen, slen; | |
| 17067 | - const char *pattern = Jim_GetStringNoQualifier(patternObj, &plen); | |
| 17068 | - const char *str = Jim_GetStringNoQualifier(keyObj, &slen); | |
| 18119 | + if (patternObj) { | |
| 18120 | + int plen, slen; | |
| 18121 | + const char *pattern = Jim_GetStringNoQualifier(patternObj, &plen); | |
| 18122 | + const char *str = Jim_GetStringNoQualifier(keyObj, &slen); | |
| 17069 | 18123 | #ifdef JIM_NO_INTROSPECTION |
| 17070 | 18124 | |
| 17071 | - match = (JimStringCompareUtf8(pattern, plen, str, slen, 0) == 0); | |
| 18125 | + match = (JimStringCompareUtf8(pattern, plen, str, slen, 0) == 0); | |
| 17072 | 18126 | #else |
| 17073 | - match = JimGlobMatch(pattern, plen, str, slen, 0); | |
| 18127 | + match = JimGlobMatch(pattern, plen, str, slen, 0); | |
| 17074 | 18128 | #endif |
| 17075 | - } | |
| 17076 | - if (match) { | |
| 17077 | - Jim_ListAppendElement(interp, listObjPtr, keyObj); | |
| 17078 | - } | |
| 18129 | + } | |
| 18130 | + if (match) { | |
| 18131 | + Jim_ListAppendElement(interp, listObjPtr, keyObj); | |
| 17079 | 18132 | } |
| 17080 | 18133 | Jim_DecrRefCount(interp, keyObj); |
| 17081 | 18134 | } |
| 17082 | 18135 | |
| 17083 | 18136 | static Jim_Obj *JimCommandsList(Jim_Interp *interp, Jim_Obj *patternObjPtr, int type) |
| @@ -17084,23 +18137,22 @@ | ||
| 17084 | 18137 | { |
| 17085 | 18138 | return JimHashtablePatternMatch(interp, &interp->commands, patternObjPtr, JimCommandMatch, type); |
| 17086 | 18139 | } |
| 17087 | 18140 | |
| 17088 | 18141 | |
| 17089 | -#define JIM_VARLIST_GLOBALS 0 | |
| 17090 | -#define JIM_VARLIST_LOCALS 1 | |
| 17091 | -#define JIM_VARLIST_VARS 2 | |
| 17092 | -#define JIM_VARLIST_MASK 0x000f | |
| 18142 | +#define JIM_VARLIST_GLOBALS 1 | |
| 18143 | +#define JIM_VARLIST_LOCALS 2 | |
| 18144 | +#define JIM_VARLIST_VARS 4 | |
| 17093 | 18145 | |
| 17094 | 18146 | #define JIM_VARLIST_VALUES 0x1000 |
| 17095 | 18147 | |
| 17096 | 18148 | static void JimVariablesMatch(Jim_Interp *interp, Jim_Obj *listObjPtr, |
| 17097 | 18149 | Jim_Obj *keyObj, void *value, Jim_Obj *patternObj, int type) |
| 17098 | 18150 | { |
| 17099 | 18151 | Jim_VarVal *vv = (Jim_VarVal *)value; |
| 17100 | 18152 | |
| 17101 | - if ((type & JIM_VARLIST_MASK) != JIM_VARLIST_LOCALS || vv->linkFramePtr == NULL) { | |
| 18153 | + if (!(type & JIM_VARLIST_LOCALS) || vv->linkFramePtr == NULL) { | |
| 17102 | 18154 | if (patternObj == NULL || Jim_StringMatchObj(interp, patternObj, keyObj, 0)) { |
| 17103 | 18155 | Jim_ListAppendElement(interp, listObjPtr, keyObj); |
| 17104 | 18156 | if (type & JIM_VARLIST_VALUES) { |
| 17105 | 18157 | Jim_ListAppendElement(interp, listObjPtr, vv->objPtr); |
| 17106 | 18158 | } |
| @@ -17109,19 +18161,20 @@ | ||
| 17109 | 18161 | } |
| 17110 | 18162 | |
| 17111 | 18163 | |
| 17112 | 18164 | static Jim_Obj *JimVariablesList(Jim_Interp *interp, Jim_Obj *patternObjPtr, int mode) |
| 17113 | 18165 | { |
| 17114 | - if (mode == JIM_VARLIST_LOCALS && interp->framePtr == interp->topFramePtr) { | |
| 18166 | + if ((mode & JIM_VARLIST_LOCALS) && interp->framePtr == interp->topFramePtr) { | |
| 17115 | 18167 | return interp->emptyObj; |
| 17116 | 18168 | } |
| 17117 | 18169 | else { |
| 17118 | - Jim_CallFrame *framePtr = (mode == JIM_VARLIST_GLOBALS) ? interp->topFramePtr : interp->framePtr; | |
| 18170 | + Jim_CallFrame *framePtr = (mode & JIM_VARLIST_GLOBALS) ? interp->topFramePtr : interp->framePtr; | |
| 17119 | 18171 | return JimHashtablePatternMatch(interp, &framePtr->vars, patternObjPtr, JimVariablesMatch, |
| 17120 | 18172 | mode); |
| 17121 | 18173 | } |
| 17122 | 18174 | } |
| 18175 | + | |
| 17123 | 18176 | |
| 17124 | 18177 | static int JimInfoLevel(Jim_Interp *interp, Jim_Obj *levelObjPtr, Jim_Obj **objPtrPtr) |
| 17125 | 18178 | { |
| 17126 | 18179 | long level; |
| 17127 | 18180 | |
| @@ -17138,10 +18191,11 @@ | ||
| 17138 | 18191 | } |
| 17139 | 18192 | } |
| 17140 | 18193 | Jim_SetResultFormatted(interp, "bad level \"%#s\"", levelObjPtr); |
| 17141 | 18194 | return JIM_ERR; |
| 17142 | 18195 | } |
| 18196 | + | |
| 17143 | 18197 | |
| 17144 | 18198 | static int JimInfoFrame(Jim_Interp *interp, Jim_Obj *levelObjPtr, Jim_Obj **objPtrPtr) |
| 17145 | 18199 | { |
| 17146 | 18200 | long level; |
| 17147 | 18201 | |
| @@ -17186,14 +18240,10 @@ | ||
| 17186 | 18240 | } |
| 17187 | 18241 | |
| 17188 | 18242 | |
| 17189 | 18243 | static int Jim_PutsCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 17190 | 18244 | { |
| 17191 | - if (argc != 2 && argc != 3) { | |
| 17192 | - Jim_WrongNumArgs(interp, 1, argv, "?-nonewline? string"); | |
| 17193 | - return JIM_ERR; | |
| 17194 | - } | |
| 17195 | 18245 | if (argc == 3) { |
| 17196 | 18246 | if (!Jim_CompareStringImmediate(interp, argv[1], "-nonewline")) { |
| 17197 | 18247 | Jim_SetResultString(interp, "The second argument must " "be -nonewline", -1); |
| 17198 | 18248 | return JIM_ERR; |
| 17199 | 18249 | } |
| @@ -17245,15 +18295,11 @@ | ||
| 17245 | 18295 | { |
| 17246 | 18296 | jim_wide wideValue, res = 0; |
| 17247 | 18297 | double doubleValue, doubleRes = 0; |
| 17248 | 18298 | int i = 2; |
| 17249 | 18299 | |
| 17250 | - if (argc < 2) { | |
| 17251 | - Jim_WrongNumArgs(interp, 1, argv, "number ?number ... number?"); | |
| 17252 | - return JIM_ERR; | |
| 17253 | - } | |
| 17254 | - else if (argc == 2) { | |
| 18300 | + if (argc == 2) { | |
| 17255 | 18301 | if (Jim_GetWide(interp, argv[1], &wideValue) != JIM_OK) { |
| 17256 | 18302 | if (Jim_GetDouble(interp, argv[1], &doubleValue) != JIM_OK) { |
| 17257 | 18303 | return JIM_ERR; |
| 17258 | 18304 | } |
| 17259 | 18305 | else { |
| @@ -17342,14 +18388,10 @@ | ||
| 17342 | 18388 | } |
| 17343 | 18389 | |
| 17344 | 18390 | |
| 17345 | 18391 | static int Jim_SetCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 17346 | 18392 | { |
| 17347 | - if (argc != 2 && argc != 3) { | |
| 17348 | - Jim_WrongNumArgs(interp, 1, argv, "varName ?newValue?"); | |
| 17349 | - return JIM_ERR; | |
| 17350 | - } | |
| 17351 | 18393 | if (argc == 2) { |
| 17352 | 18394 | Jim_Obj *objPtr; |
| 17353 | 18395 | |
| 17354 | 18396 | objPtr = Jim_GetVariable(interp, argv[1], JIM_ERRMSG); |
| 17355 | 18397 | if (!objPtr) |
| @@ -17405,15 +18447,10 @@ | ||
| 17405 | 18447 | } |
| 17406 | 18448 | |
| 17407 | 18449 | |
| 17408 | 18450 | static int Jim_WhileCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 17409 | 18451 | { |
| 17410 | - if (argc != 3) { | |
| 17411 | - Jim_WrongNumArgs(interp, 1, argv, "condition body"); | |
| 17412 | - return JIM_ERR; | |
| 17413 | - } | |
| 17414 | - | |
| 17415 | 18452 | |
| 17416 | 18453 | while (1) { |
| 17417 | 18454 | int boolean = 0, retval; |
| 17418 | 18455 | |
| 17419 | 18456 | if ((retval = Jim_GetBoolFromExpr(interp, argv[1], &boolean)) != JIM_OK) |
| @@ -17447,15 +18484,10 @@ | ||
| 17447 | 18484 | int boolean = 1; |
| 17448 | 18485 | int immediate = 0; |
| 17449 | 18486 | Jim_Obj *varNamePtr = NULL; |
| 17450 | 18487 | Jim_Obj *stopVarNamePtr = NULL; |
| 17451 | 18488 | |
| 17452 | - if (argc != 5) { | |
| 17453 | - Jim_WrongNumArgs(interp, 1, argv, "start test next body"); | |
| 17454 | - return JIM_ERR; | |
| 17455 | - } | |
| 17456 | - | |
| 17457 | 18489 | |
| 17458 | 18490 | if ((retval = Jim_EvalObj(interp, argv[1])) != JIM_OK) { |
| 17459 | 18491 | return retval; |
| 17460 | 18492 | } |
| 17461 | 18493 | |
| @@ -17623,19 +18655,14 @@ | ||
| 17623 | 18655 | |
| 17624 | 18656 | static int Jim_LoopCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 17625 | 18657 | { |
| 17626 | 18658 | int retval; |
| 17627 | 18659 | jim_wide i; |
| 17628 | - jim_wide limit; | |
| 18660 | + jim_wide limit = 0; | |
| 17629 | 18661 | jim_wide incr = 1; |
| 17630 | 18662 | Jim_Obj *bodyObjPtr; |
| 17631 | 18663 | |
| 17632 | - if (argc < 4 || argc > 6) { | |
| 17633 | - Jim_WrongNumArgs(interp, 1, argv, "var ?first? limit ?incr? body"); | |
| 17634 | - return JIM_ERR; | |
| 17635 | - } | |
| 17636 | - | |
| 17637 | 18664 | retval = Jim_GetWideExpr(interp, argv[2], &i); |
| 17638 | 18665 | if (argc > 4 && retval == JIM_OK) { |
| 17639 | 18666 | retval = Jim_GetWideExpr(interp, argv[3], &limit); |
| 17640 | 18667 | } |
| 17641 | 18668 | if (argc > 5 && retval == JIM_OK) { |
| @@ -17682,13 +18709,10 @@ | ||
| 17682 | 18709 | } |
| 17683 | 18710 | } |
| 17684 | 18711 | else { |
| 17685 | 18712 | objPtr = Jim_NewIntObj(interp, i); |
| 17686 | 18713 | retval = Jim_SetVariable(interp, argv[1], objPtr); |
| 17687 | - if (retval != JIM_OK) { | |
| 17688 | - Jim_FreeNewObj(interp, objPtr); | |
| 17689 | - } | |
| 17690 | 18714 | } |
| 17691 | 18715 | } |
| 17692 | 18716 | } |
| 17693 | 18717 | |
| 17694 | 18718 | if (retval == JIM_OK || retval == JIM_CONTINUE || retval == JIM_BREAK) { |
| @@ -17730,13 +18754,12 @@ | ||
| 17730 | 18754 | Jim_ListIter twoiters[2]; |
| 17731 | 18755 | Jim_ListIter *iters; |
| 17732 | 18756 | Jim_Obj *script; |
| 17733 | 18757 | Jim_Obj *resultObj; |
| 17734 | 18758 | |
| 17735 | - if (argc < 4 || argc % 2 != 0) { | |
| 17736 | - Jim_WrongNumArgs(interp, 1, argv, "varList list ?varList list ...? script"); | |
| 17737 | - return JIM_ERR; | |
| 18759 | + if (argc % 2 != 0) { | |
| 18760 | + return JIM_USAGE; | |
| 17738 | 18761 | } |
| 17739 | 18762 | script = argv[argc - 1]; |
| 17740 | 18763 | numargs = (argc - 1 - 1); |
| 17741 | 18764 | |
| 17742 | 18765 | if (numargs == 2) { |
| @@ -17786,14 +18809,14 @@ | ||
| 17786 | 18809 | Jim_Obj *valObj = JimListIterNext(interp, &iters[i + 1]); |
| 17787 | 18810 | if (!valObj) { |
| 17788 | 18811 | |
| 17789 | 18812 | valObj = interp->emptyObj; |
| 17790 | 18813 | } |
| 17791 | - | |
| 17792 | - Jim_IncrRefCount(valObj); | |
| 18814 | + // XXX | |
| 18815 | + //Jim_IncrRefCount(valObj); | |
| 17793 | 18816 | result = Jim_SetVariable(interp, varName, valObj); |
| 17794 | - Jim_DecrRefCount(interp, valObj); | |
| 18817 | + //Jim_DecrRefCount(interp, valObj); | |
| 17795 | 18818 | if (result != JIM_OK) { |
| 17796 | 18819 | goto err; |
| 17797 | 18820 | } |
| 17798 | 18821 | } |
| 17799 | 18822 | } |
| @@ -17845,15 +18868,10 @@ | ||
| 17845 | 18868 | int result = JIM_ERR; |
| 17846 | 18869 | int i; |
| 17847 | 18870 | Jim_ListIter iter; |
| 17848 | 18871 | Jim_Obj *resultObj; |
| 17849 | 18872 | |
| 17850 | - if (argc < 2) { | |
| 17851 | - Jim_WrongNumArgs(interp, 1, argv, "varList list ?varName ...?"); | |
| 17852 | - return JIM_ERR; | |
| 17853 | - } | |
| 17854 | - | |
| 17855 | 18873 | JimListIterInit(&iter, argv[1]); |
| 17856 | 18874 | |
| 17857 | 18875 | for (i = 2; i < argc; i++) { |
| 17858 | 18876 | Jim_Obj *valObj = JimListIterNext(interp, &iter); |
| 17859 | 18877 | result = Jim_SetVariable(interp, argv[i], valObj ? valObj : interp->emptyObj); |
| @@ -17875,56 +18893,56 @@ | ||
| 17875 | 18893 | |
| 17876 | 18894 | static int Jim_IfCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 17877 | 18895 | { |
| 17878 | 18896 | int boolean, retval, current = 1, falsebody = 0; |
| 17879 | 18897 | |
| 17880 | - if (argc >= 3) { | |
| 17881 | - while (1) { | |
| 17882 | - | |
| 17883 | - if (current >= argc) | |
| 17884 | - goto err; | |
| 17885 | - if ((retval = Jim_GetBoolFromExpr(interp, argv[current++], &boolean)) | |
| 17886 | - != JIM_OK) | |
| 17887 | - return retval; | |
| 17888 | - | |
| 17889 | - if (current >= argc) | |
| 17890 | - goto err; | |
| 17891 | - if (Jim_CompareStringImmediate(interp, argv[current], "then")) | |
| 17892 | - current++; | |
| 17893 | - | |
| 17894 | - if (current >= argc) | |
| 17895 | - goto err; | |
| 17896 | - if (boolean) | |
| 17897 | - return Jim_EvalObj(interp, argv[current]); | |
| 17898 | - | |
| 17899 | - if (++current >= argc) { | |
| 17900 | - Jim_SetResult(interp, Jim_NewEmptyStringObj(interp)); | |
| 17901 | - return JIM_OK; | |
| 17902 | - } | |
| 17903 | - falsebody = current++; | |
| 17904 | - if (Jim_CompareStringImmediate(interp, argv[falsebody], "else")) { | |
| 17905 | - | |
| 17906 | - if (current != argc - 1) | |
| 17907 | - goto err; | |
| 17908 | - return Jim_EvalObj(interp, argv[current]); | |
| 17909 | - } | |
| 17910 | - else if (Jim_CompareStringImmediate(interp, argv[falsebody], "elseif")) | |
| 17911 | - continue; | |
| 17912 | - | |
| 17913 | - else if (falsebody != argc - 1) | |
| 17914 | - goto err; | |
| 17915 | - return Jim_EvalObj(interp, argv[falsebody]); | |
| 17916 | - } | |
| 17917 | - return JIM_OK; | |
| 17918 | - } | |
| 17919 | - err: | |
| 17920 | - Jim_WrongNumArgs(interp, 1, argv, "condition ?then? trueBody ?elseif ...? ?else? falseBody"); | |
| 17921 | - return JIM_ERR; | |
| 17922 | -} | |
| 17923 | - | |
| 17924 | - | |
| 17925 | -int Jim_CommandMatchObj(Jim_Interp *interp, Jim_Obj *commandObj, Jim_Obj *patternObj, | |
| 18898 | + while (1) { | |
| 18899 | + | |
| 18900 | + if (current >= argc) { | |
| 18901 | + return JIM_USAGE; | |
| 18902 | + } | |
| 18903 | + if ((retval = Jim_GetBoolFromExpr(interp, argv[current++], &boolean)) | |
| 18904 | + != JIM_OK) | |
| 18905 | + return retval; | |
| 18906 | + | |
| 18907 | + if (current >= argc) { | |
| 18908 | + return JIM_USAGE; | |
| 18909 | + } | |
| 18910 | + if (Jim_CompareStringImmediate(interp, argv[current], "then")) | |
| 18911 | + current++; | |
| 18912 | + | |
| 18913 | + if (current >= argc) { | |
| 18914 | + return JIM_USAGE; | |
| 18915 | + } | |
| 18916 | + if (boolean) | |
| 18917 | + return Jim_EvalObj(interp, argv[current]); | |
| 18918 | + | |
| 18919 | + if (++current >= argc) { | |
| 18920 | + Jim_SetResult(interp, Jim_NewEmptyStringObj(interp)); | |
| 18921 | + return JIM_OK; | |
| 18922 | + } | |
| 18923 | + falsebody = current++; | |
| 18924 | + if (Jim_CompareStringImmediate(interp, argv[falsebody], "else")) { | |
| 18925 | + | |
| 18926 | + if (current != argc - 1) { | |
| 18927 | + return JIM_USAGE; | |
| 18928 | + } | |
| 18929 | + return Jim_EvalObj(interp, argv[current]); | |
| 18930 | + } | |
| 18931 | + else if (Jim_CompareStringImmediate(interp, argv[falsebody], "elseif")) | |
| 18932 | + continue; | |
| 18933 | + | |
| 18934 | + else if (falsebody != argc - 1) { | |
| 18935 | + return JIM_USAGE; | |
| 18936 | + } | |
| 18937 | + return Jim_EvalObj(interp, argv[falsebody]); | |
| 18938 | + } | |
| 18939 | + return JIM_OK; | |
| 18940 | +} | |
| 18941 | + | |
| 18942 | + | |
| 18943 | +static int JimCommandMatchObj(Jim_Interp *interp, Jim_Obj *commandObj, Jim_Obj *patternObj, | |
| 17926 | 18944 | Jim_Obj *stringObj, int flags) |
| 17927 | 18945 | { |
| 17928 | 18946 | Jim_Obj *parms[5]; |
| 17929 | 18947 | int argc = 0; |
| 17930 | 18948 | long eq; |
| @@ -17956,16 +18974,10 @@ | ||
| 17956 | 18974 | int matchOpt = SWITCH_EXACT, opt = 1, patCount, i; |
| 17957 | 18975 | int match_flags = 0; |
| 17958 | 18976 | Jim_Obj *command = NULL, *scriptObj = NULL, *strObj; |
| 17959 | 18977 | Jim_Obj **caseList; |
| 17960 | 18978 | |
| 17961 | - if (argc < 3) { | |
| 17962 | - wrongnumargs: | |
| 17963 | - Jim_WrongNumArgs(interp, 1, argv, "?options? string " | |
| 17964 | - "pattern body ... ?default body? or " "{pattern body ?pattern body ...?}"); | |
| 17965 | - return JIM_ERR; | |
| 17966 | - } | |
| 17967 | 18979 | for (opt = 1; opt < argc; ++opt) { |
| 17968 | 18980 | const char *option = Jim_String(argv[opt]); |
| 17969 | 18981 | |
| 17970 | 18982 | if (*option != '-') |
| 17971 | 18983 | break; |
| @@ -17982,31 +18994,31 @@ | ||
| 17982 | 18994 | match_flags |= JIM_OPT_END; |
| 17983 | 18995 | } |
| 17984 | 18996 | else if (strncmp(option, "-command", 2) == 0) { |
| 17985 | 18997 | matchOpt = SWITCH_CMD; |
| 17986 | 18998 | if ((argc - opt) < 2) |
| 17987 | - goto wrongnumargs; | |
| 18999 | + return JIM_USAGE; | |
| 17988 | 19000 | command = argv[++opt]; |
| 17989 | 19001 | } |
| 17990 | 19002 | else { |
| 17991 | 19003 | Jim_SetResultFormatted(interp, |
| 17992 | 19004 | "bad option \"%#s\": must be -exact, -glob, -regexp, -command procname or --", |
| 17993 | 19005 | argv[opt]); |
| 17994 | 19006 | return JIM_ERR; |
| 17995 | 19007 | } |
| 17996 | 19008 | if ((argc - opt) < 2) |
| 17997 | - goto wrongnumargs; | |
| 19009 | + return JIM_USAGE; | |
| 17998 | 19010 | } |
| 17999 | 19011 | strObj = argv[opt++]; |
| 18000 | 19012 | patCount = argc - opt; |
| 18001 | 19013 | if (patCount == 1) { |
| 18002 | 19014 | JimListGetElements(interp, argv[opt], &patCount, &caseList); |
| 18003 | 19015 | } |
| 18004 | 19016 | else |
| 18005 | 19017 | caseList = (Jim_Obj **)&argv[opt]; |
| 18006 | 19018 | if (patCount == 0 || patCount % 2 != 0) |
| 18007 | - goto wrongnumargs; | |
| 19019 | + return JIM_USAGE; | |
| 18008 | 19020 | for (i = 0; scriptObj == NULL && i < patCount; i += 2) { |
| 18009 | 19021 | Jim_Obj *patObj = caseList[i]; |
| 18010 | 19022 | |
| 18011 | 19023 | if (!Jim_CompareStringImmediate(interp, patObj, "default") |
| 18012 | 19024 | || i < (patCount - 2)) { |
| @@ -18021,11 +19033,11 @@ | ||
| 18021 | 19033 | break; |
| 18022 | 19034 | case SWITCH_RE: |
| 18023 | 19035 | command = Jim_NewStringObj(interp, "regexp", -1); |
| 18024 | 19036 | |
| 18025 | 19037 | case SWITCH_CMD:{ |
| 18026 | - int rc = Jim_CommandMatchObj(interp, command, patObj, strObj, match_flags); | |
| 19038 | + int rc = JimCommandMatchObj(interp, command, patObj, strObj, match_flags); | |
| 18027 | 19039 | |
| 18028 | 19040 | if (argc - opt == 1) { |
| 18029 | 19041 | JimListGetElements(interp, argv[opt], &patCount, &caseList); |
| 18030 | 19042 | } |
| 18031 | 19043 | |
| @@ -18069,14 +19081,10 @@ | ||
| 18069 | 19081 | static int Jim_LindexCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18070 | 19082 | { |
| 18071 | 19083 | Jim_Obj *objPtr; |
| 18072 | 19084 | int ret; |
| 18073 | 19085 | |
| 18074 | - if (argc < 2) { | |
| 18075 | - Jim_WrongNumArgs(interp, 1, argv, "list ?index ...?"); | |
| 18076 | - return JIM_ERR; | |
| 18077 | - } | |
| 18078 | 19086 | ret = Jim_ListIndices(interp, argv[1], argv + 2, argc - 2, &objPtr, JIM_NONE); |
| 18079 | 19087 | if (ret < 0) { |
| 18080 | 19088 | ret = JIM_OK; |
| 18081 | 19089 | Jim_SetEmptyResult(interp); |
| 18082 | 19090 | } |
| @@ -18087,14 +19095,10 @@ | ||
| 18087 | 19095 | } |
| 18088 | 19096 | |
| 18089 | 19097 | |
| 18090 | 19098 | static int Jim_LlengthCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18091 | 19099 | { |
| 18092 | - if (argc != 2) { | |
| 18093 | - Jim_WrongNumArgs(interp, 1, argv, "list"); | |
| 18094 | - return JIM_ERR; | |
| 18095 | - } | |
| 18096 | 19100 | Jim_SetResultInt(interp, Jim_ListLength(interp, argv[1])); |
| 18097 | 19101 | return JIM_OK; |
| 18098 | 19102 | } |
| 18099 | 19103 | |
| 18100 | 19104 | |
| @@ -18119,17 +19123,10 @@ | ||
| 18119 | 19123 | Jim_Obj *commandObj = NULL; |
| 18120 | 19124 | Jim_Obj *indexObj = NULL; |
| 18121 | 19125 | int match_flags = 0; |
| 18122 | 19126 | long stride = 1; |
| 18123 | 19127 | |
| 18124 | - if (argc < 3) { | |
| 18125 | - wrongargs: | |
| 18126 | - Jim_WrongNumArgs(interp, 1, argv, | |
| 18127 | - "?-exact|-glob|-regexp|-command 'command'? ?-bool|-inline? ?-not? ?-nocase? ?-all? ?-stride len? ?-index val? list value"); | |
| 18128 | - return JIM_ERR; | |
| 18129 | - } | |
| 18130 | - | |
| 18131 | 19128 | for (i = 1; i < argc - 2; i++) { |
| 18132 | 19129 | int option; |
| 18133 | 19130 | |
| 18134 | 19131 | if (Jim_GetEnum(interp, argv[i], options, &option, NULL, JIM_ERRMSG) != JIM_OK) { |
| 18135 | 19132 | return JIM_ERR; |
| @@ -18156,27 +19153,27 @@ | ||
| 18156 | 19153 | opt_match = option; |
| 18157 | 19154 | match_flags |= JIM_OPT_END; |
| 18158 | 19155 | break; |
| 18159 | 19156 | case OPT_COMMAND: |
| 18160 | 19157 | if (i >= argc - 2) { |
| 18161 | - goto wrongargs; | |
| 19158 | + return JIM_USAGE; | |
| 18162 | 19159 | } |
| 18163 | 19160 | commandObj = argv[++i]; |
| 18164 | 19161 | |
| 18165 | 19162 | case OPT_EXACT: |
| 18166 | 19163 | case OPT_GLOB: |
| 18167 | 19164 | opt_match = option; |
| 18168 | 19165 | break; |
| 18169 | 19166 | case OPT_INDEX: |
| 18170 | 19167 | if (i >= argc - 2) { |
| 18171 | - goto wrongargs; | |
| 19168 | + return JIM_USAGE; | |
| 18172 | 19169 | } |
| 18173 | 19170 | indexObj = argv[++i]; |
| 18174 | 19171 | break; |
| 18175 | 19172 | case OPT_STRIDE: |
| 18176 | 19173 | if (i >= argc - 2) { |
| 18177 | - goto wrongargs; | |
| 19174 | + return JIM_USAGE; | |
| 18178 | 19175 | } |
| 18179 | 19176 | if (Jim_GetLong(interp, argv[++i], &stride) != JIM_OK) { |
| 18180 | 19177 | return JIM_ERR; |
| 18181 | 19178 | } |
| 18182 | 19179 | if (stride < 1) { |
| @@ -18187,11 +19184,11 @@ | ||
| 18187 | 19184 | } |
| 18188 | 19185 | } |
| 18189 | 19186 | |
| 18190 | 19187 | argc -= i; |
| 18191 | 19188 | if (argc < 2) { |
| 18192 | - goto wrongargs; | |
| 19189 | + return JIM_USAGE; | |
| 18193 | 19190 | } |
| 18194 | 19191 | argv += i; |
| 18195 | 19192 | |
| 18196 | 19193 | listlen = Jim_ListLength(interp, argv[0]); |
| 18197 | 19194 | if (listlen % stride) { |
| @@ -18250,11 +19247,11 @@ | ||
| 18250 | 19247 | eq = Jim_StringMatchObj(interp, argv[1], objPtr, match_flags); |
| 18251 | 19248 | break; |
| 18252 | 19249 | |
| 18253 | 19250 | case OPT_REGEXP: |
| 18254 | 19251 | case OPT_COMMAND: |
| 18255 | - eq = Jim_CommandMatchObj(interp, commandObj, argv[1], objPtr, match_flags); | |
| 19252 | + eq = JimCommandMatchObj(interp, commandObj, argv[1], objPtr, match_flags); | |
| 18256 | 19253 | if (eq < 0) { |
| 18257 | 19254 | Jim_DecrRefCount(interp, searchListObj); |
| 18258 | 19255 | rc = JIM_ERR; |
| 18259 | 19256 | goto done; |
| 18260 | 19257 | } |
| @@ -18326,32 +19323,23 @@ | ||
| 18326 | 19323 | |
| 18327 | 19324 | |
| 18328 | 19325 | static int Jim_LappendCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18329 | 19326 | { |
| 18330 | 19327 | Jim_Obj *listObjPtr; |
| 18331 | - int new_obj = 0; | |
| 18332 | 19328 | int i; |
| 18333 | 19329 | |
| 18334 | - if (argc < 2) { | |
| 18335 | - Jim_WrongNumArgs(interp, 1, argv, "varName ?value value ...?"); | |
| 18336 | - return JIM_ERR; | |
| 18337 | - } | |
| 18338 | 19330 | listObjPtr = Jim_GetVariable(interp, argv[1], JIM_UNSHARED); |
| 18339 | 19331 | if (!listObjPtr) { |
| 18340 | 19332 | |
| 18341 | 19333 | listObjPtr = Jim_NewListObj(interp, NULL, 0); |
| 18342 | - new_obj = 1; | |
| 18343 | 19334 | } |
| 18344 | 19335 | else if (Jim_IsShared(listObjPtr)) { |
| 18345 | 19336 | listObjPtr = Jim_DuplicateObj(interp, listObjPtr); |
| 18346 | - new_obj = 1; | |
| 18347 | 19337 | } |
| 18348 | 19338 | for (i = 2; i < argc; i++) |
| 18349 | 19339 | Jim_ListAppendElement(interp, listObjPtr, argv[i]); |
| 18350 | 19340 | if (Jim_SetVariable(interp, argv[1], listObjPtr) != JIM_OK) { |
| 18351 | - if (new_obj) | |
| 18352 | - Jim_FreeNewObj(interp, listObjPtr); | |
| 18353 | 19341 | return JIM_ERR; |
| 18354 | 19342 | } |
| 18355 | 19343 | Jim_SetResult(interp, listObjPtr); |
| 18356 | 19344 | return JIM_OK; |
| 18357 | 19345 | } |
| @@ -18360,14 +19348,10 @@ | ||
| 18360 | 19348 | static int Jim_LinsertCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18361 | 19349 | { |
| 18362 | 19350 | int idx, len; |
| 18363 | 19351 | Jim_Obj *listPtr; |
| 18364 | 19352 | |
| 18365 | - if (argc < 3) { | |
| 18366 | - Jim_WrongNumArgs(interp, 1, argv, "list index ?element ...?"); | |
| 18367 | - return JIM_ERR; | |
| 18368 | - } | |
| 18369 | 19353 | listPtr = argv[1]; |
| 18370 | 19354 | if (Jim_IsShared(listPtr)) |
| 18371 | 19355 | listPtr = Jim_DuplicateObj(interp, listPtr); |
| 18372 | 19356 | if (Jim_GetIndex(interp, argv[2], &idx) != JIM_OK) |
| 18373 | 19357 | goto err; |
| @@ -18391,14 +19375,10 @@ | ||
| 18391 | 19375 | { |
| 18392 | 19376 | int first, last, len, rangeLen; |
| 18393 | 19377 | Jim_Obj *listObj; |
| 18394 | 19378 | Jim_Obj *newListObj; |
| 18395 | 19379 | |
| 18396 | - if (argc < 4) { | |
| 18397 | - Jim_WrongNumArgs(interp, 1, argv, "list first last ?element ...?"); | |
| 18398 | - return JIM_ERR; | |
| 18399 | - } | |
| 18400 | 19380 | if (Jim_GetIndex(interp, argv[2], &first) != JIM_OK || |
| 18401 | 19381 | Jim_GetIndex(interp, argv[3], &last) != JIM_OK) { |
| 18402 | 19382 | return JIM_ERR; |
| 18403 | 19383 | } |
| 18404 | 19384 | |
| @@ -18428,15 +19408,11 @@ | ||
| 18428 | 19408 | } |
| 18429 | 19409 | |
| 18430 | 19410 | |
| 18431 | 19411 | static int Jim_LsetCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18432 | 19412 | { |
| 18433 | - if (argc < 3) { | |
| 18434 | - Jim_WrongNumArgs(interp, 1, argv, "listVar ?index ...? value"); | |
| 18435 | - return JIM_ERR; | |
| 18436 | - } | |
| 18437 | - else if (argc == 3) { | |
| 19413 | + if (argc == 3) { | |
| 18438 | 19414 | |
| 18439 | 19415 | if (Jim_SetVariable(interp, argv[1], argv[2]) != JIM_OK) |
| 18440 | 19416 | return JIM_ERR; |
| 18441 | 19417 | Jim_SetResult(interp, argv[2]); |
| 18442 | 19418 | return JIM_OK; |
| @@ -18463,16 +19439,10 @@ | ||
| 18463 | 19439 | Jim_Obj **elements; |
| 18464 | 19440 | int listlen; |
| 18465 | 19441 | |
| 18466 | 19442 | struct lsort_info info; |
| 18467 | 19443 | |
| 18468 | - if (argc < 2) { | |
| 18469 | -wrongargs: | |
| 18470 | - Jim_WrongNumArgs(interp, 1, argv, "?options? list"); | |
| 18471 | - return JIM_ERR; | |
| 18472 | - } | |
| 18473 | - | |
| 18474 | 19444 | info.type = JIM_LSORT_ASCII; |
| 18475 | 19445 | info.order = 1; |
| 18476 | 19446 | info.indexc = 0; |
| 18477 | 19447 | info.unique = 0; |
| 18478 | 19448 | info.command = NULL; |
| @@ -18518,11 +19488,11 @@ | ||
| 18518 | 19488 | info.command = argv[i + 1]; |
| 18519 | 19489 | i++; |
| 18520 | 19490 | break; |
| 18521 | 19491 | case OPT_STRIDE: |
| 18522 | 19492 | if (i >= argc - 2) { |
| 18523 | - goto wrongargs; | |
| 19493 | + return JIM_USAGE; | |
| 18524 | 19494 | } |
| 18525 | 19495 | if (Jim_GetLong(interp, argv[++i], &stride) != JIM_OK) { |
| 18526 | 19496 | return JIM_ERR; |
| 18527 | 19497 | } |
| 18528 | 19498 | if (stride < 2) { |
| @@ -18596,38 +19566,28 @@ | ||
| 18596 | 19566 | static int Jim_AppendCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18597 | 19567 | { |
| 18598 | 19568 | Jim_Obj *stringObjPtr; |
| 18599 | 19569 | int i; |
| 18600 | 19570 | |
| 18601 | - if (argc < 2) { | |
| 18602 | - Jim_WrongNumArgs(interp, 1, argv, "varName ?value ...?"); | |
| 18603 | - return JIM_ERR; | |
| 18604 | - } | |
| 18605 | 19571 | if (argc == 2) { |
| 18606 | 19572 | stringObjPtr = Jim_GetVariable(interp, argv[1], JIM_ERRMSG); |
| 18607 | 19573 | if (!stringObjPtr) |
| 18608 | 19574 | return JIM_ERR; |
| 18609 | 19575 | } |
| 18610 | 19576 | else { |
| 18611 | - int new_obj = 0; | |
| 18612 | 19577 | stringObjPtr = Jim_GetVariable(interp, argv[1], JIM_UNSHARED); |
| 18613 | 19578 | if (!stringObjPtr) { |
| 18614 | 19579 | |
| 18615 | 19580 | stringObjPtr = Jim_NewEmptyStringObj(interp); |
| 18616 | - new_obj = 1; | |
| 18617 | 19581 | } |
| 18618 | 19582 | else if (Jim_IsShared(stringObjPtr)) { |
| 18619 | - new_obj = 1; | |
| 18620 | 19583 | stringObjPtr = Jim_DuplicateObj(interp, stringObjPtr); |
| 18621 | 19584 | } |
| 18622 | 19585 | for (i = 2; i < argc; i++) { |
| 18623 | 19586 | Jim_AppendObj(interp, stringObjPtr, argv[i]); |
| 18624 | 19587 | } |
| 18625 | 19588 | if (Jim_SetVariable(interp, argv[1], stringObjPtr) != JIM_OK) { |
| 18626 | - if (new_obj) { | |
| 18627 | - Jim_FreeNewObj(interp, stringObjPtr); | |
| 18628 | - } | |
| 18629 | 19589 | return JIM_ERR; |
| 18630 | 19590 | } |
| 18631 | 19591 | } |
| 18632 | 19592 | Jim_SetResult(interp, stringObjPtr); |
| 18633 | 19593 | return JIM_OK; |
| @@ -18639,15 +19599,10 @@ | ||
| 18639 | 19599 | |
| 18640 | 19600 | static int Jim_EvalCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18641 | 19601 | { |
| 18642 | 19602 | int rc; |
| 18643 | 19603 | |
| 18644 | - if (argc < 2) { | |
| 18645 | - Jim_WrongNumArgs(interp, 1, argv, "arg ?arg ...?"); | |
| 18646 | - return JIM_ERR; | |
| 18647 | - } | |
| 18648 | - | |
| 18649 | 19604 | if (argc == 2) { |
| 18650 | 19605 | rc = Jim_EvalObj(interp, argv[1]); |
| 18651 | 19606 | } |
| 18652 | 19607 | else { |
| 18653 | 19608 | rc = Jim_EvalObj(interp, Jim_ConcatObj(interp, argc - 1, argv + 1)); |
| @@ -18657,88 +19612,67 @@ | ||
| 18657 | 19612 | } |
| 18658 | 19613 | |
| 18659 | 19614 | |
| 18660 | 19615 | static int Jim_UplevelCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18661 | 19616 | { |
| 18662 | - if (argc >= 2) { | |
| 18663 | - int retcode; | |
| 18664 | - Jim_CallFrame *savedCallFrame, *targetCallFrame; | |
| 18665 | - const char *str; | |
| 18666 | - | |
| 18667 | - | |
| 18668 | - savedCallFrame = interp->framePtr; | |
| 18669 | - | |
| 18670 | - | |
| 18671 | - str = Jim_String(argv[1]); | |
| 18672 | - if ((str[0] >= '0' && str[0] <= '9') || str[0] == '#') { | |
| 18673 | - targetCallFrame = Jim_GetCallFrameByLevel(interp, argv[1]); | |
| 18674 | - argc--; | |
| 18675 | - argv++; | |
| 18676 | - } | |
| 18677 | - else { | |
| 18678 | - targetCallFrame = Jim_GetCallFrameByLevel(interp, NULL); | |
| 18679 | - } | |
| 18680 | - if (targetCallFrame == NULL) { | |
| 18681 | - return JIM_ERR; | |
| 18682 | - } | |
| 18683 | - if (argc < 2) { | |
| 18684 | - Jim_WrongNumArgs(interp, 1, argv - 1, "?level? command ?arg ...?"); | |
| 18685 | - return JIM_ERR; | |
| 18686 | - } | |
| 18687 | - | |
| 18688 | - interp->framePtr = targetCallFrame; | |
| 18689 | - if (argc == 2) { | |
| 18690 | - retcode = Jim_EvalObj(interp, argv[1]); | |
| 18691 | - } | |
| 18692 | - else { | |
| 18693 | - retcode = Jim_EvalObj(interp, Jim_ConcatObj(interp, argc - 1, argv + 1)); | |
| 18694 | - } | |
| 18695 | - interp->framePtr = savedCallFrame; | |
| 18696 | - return retcode; | |
| 18697 | - } | |
| 18698 | - else { | |
| 18699 | - Jim_WrongNumArgs(interp, 1, argv, "?level? command ?arg ...?"); | |
| 18700 | - return JIM_ERR; | |
| 18701 | - } | |
| 19617 | + int retcode; | |
| 19618 | + Jim_CallFrame *savedCallFrame, *targetCallFrame; | |
| 19619 | + const char *str; | |
| 19620 | + | |
| 19621 | + | |
| 19622 | + savedCallFrame = interp->framePtr; | |
| 19623 | + | |
| 19624 | + | |
| 19625 | + str = Jim_String(argv[1]); | |
| 19626 | + if ((str[0] >= '0' && str[0] <= '9') || str[0] == '#') { | |
| 19627 | + targetCallFrame = Jim_GetCallFrameByLevel(interp, argv[1]); | |
| 19628 | + argc--; | |
| 19629 | + argv++; | |
| 19630 | + } | |
| 19631 | + else { | |
| 19632 | + targetCallFrame = Jim_GetCallFrameByLevel(interp, NULL); | |
| 19633 | + } | |
| 19634 | + if (targetCallFrame == NULL) { | |
| 19635 | + return JIM_ERR; | |
| 19636 | + } | |
| 19637 | + if (argc < 2) { | |
| 19638 | + return JIM_USAGE; | |
| 19639 | + } | |
| 19640 | + | |
| 19641 | + interp->framePtr = targetCallFrame; | |
| 19642 | + if (argc == 2) { | |
| 19643 | + retcode = Jim_EvalObj(interp, argv[1]); | |
| 19644 | + } | |
| 19645 | + else { | |
| 19646 | + retcode = Jim_EvalObj(interp, Jim_ConcatObj(interp, argc - 1, argv + 1)); | |
| 19647 | + } | |
| 19648 | + interp->framePtr = savedCallFrame; | |
| 19649 | + return retcode; | |
| 18702 | 19650 | } |
| 18703 | 19651 | |
| 18704 | 19652 | |
| 18705 | 19653 | static int Jim_ExprCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18706 | 19654 | { |
| 18707 | - int retcode; | |
| 18708 | - | |
| 18709 | - if (argc == 2) { | |
| 18710 | - retcode = Jim_EvalExpression(interp, argv[1]); | |
| 18711 | - } | |
| 18712 | -#ifndef JIM_COMPAT | |
| 18713 | - else { | |
| 18714 | - Jim_WrongNumArgs(interp, 1, argv, "expression"); | |
| 18715 | - retcode = JIM_ERR; | |
| 18716 | - } | |
| 18717 | -#else | |
| 18718 | - else if (argc > 2) { | |
| 19655 | +#ifdef JIM_COMPAT | |
| 19656 | + if (argc > 2) { | |
| 19657 | + int retcode; | |
| 18719 | 19658 | Jim_Obj *objPtr; |
| 18720 | 19659 | |
| 18721 | 19660 | objPtr = Jim_ConcatObj(interp, argc - 1, argv + 1); |
| 18722 | 19661 | Jim_IncrRefCount(objPtr); |
| 18723 | 19662 | retcode = Jim_EvalExpression(interp, objPtr); |
| 18724 | 19663 | Jim_DecrRefCount(interp, objPtr); |
| 18725 | - } | |
| 18726 | - else { | |
| 18727 | - Jim_WrongNumArgs(interp, 1, argv, "expression ?...?"); | |
| 18728 | - return JIM_ERR; | |
| 19664 | + | |
| 19665 | + return retcode; | |
| 18729 | 19666 | } |
| 18730 | 19667 | #endif |
| 18731 | - return retcode; | |
| 19668 | + return Jim_EvalExpression(interp, argv[1]); | |
| 18732 | 19669 | } |
| 19670 | + | |
| 18733 | 19671 | |
| 18734 | 19672 | static int JimBreakContinueHelper(Jim_Interp *interp, int argc, Jim_Obj *const *argv, int retcode) |
| 18735 | 19673 | { |
| 18736 | - if (argc != 1 && argc != 2) { | |
| 18737 | - Jim_WrongNumArgs(interp, 1, argv, "?level?"); | |
| 18738 | - return JIM_ERR; | |
| 18739 | - } | |
| 18740 | 19674 | if (argc == 2) { |
| 18741 | 19675 | long level; |
| 18742 | 19676 | int ret = Jim_GetLong(interp, argv[1], &level); |
| 18743 | 19677 | if (ret != JIM_OK) { |
| 18744 | 19678 | return ret; |
| @@ -18822,12 +19756,11 @@ | ||
| 18822 | 19756 | break; |
| 18823 | 19757 | } |
| 18824 | 19758 | } |
| 18825 | 19759 | |
| 18826 | 19760 | if (i != argc - 1 && i != argc) { |
| 18827 | - Jim_WrongNumArgs(interp, 1, argv, | |
| 18828 | - "?-code code? ?-errorinfo stacktrace? ?-level level? ?result?"); | |
| 19761 | + return JIM_USAGE; | |
| 18829 | 19762 | } |
| 18830 | 19763 | |
| 18831 | 19764 | |
| 18832 | 19765 | if (stackTraceObj && returnCode == JIM_ERR) { |
| 18833 | 19766 | JimSetStackTrace(interp, stackTraceObj); |
| @@ -18876,10 +19809,11 @@ | ||
| 18876 | 19809 | |
| 18877 | 19810 | return JIM_EVAL; |
| 18878 | 19811 | } |
| 18879 | 19812 | return JIM_OK; |
| 18880 | 19813 | } |
| 19814 | + | |
| 18881 | 19815 | |
| 18882 | 19816 | static int JimAliasCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18883 | 19817 | { |
| 18884 | 19818 | Jim_Obj *cmdList; |
| 18885 | 19819 | Jim_Obj *prefixListObj = Jim_CmdPrivData(interp); |
| @@ -18888,43 +19822,34 @@ | ||
| 18888 | 19822 | cmdList = Jim_DuplicateObj(interp, prefixListObj); |
| 18889 | 19823 | Jim_ListInsertElements(interp, cmdList, Jim_ListLength(interp, cmdList), argc - 1, argv + 1); |
| 18890 | 19824 | |
| 18891 | 19825 | return JimEvalObjList(interp, cmdList); |
| 18892 | 19826 | } |
| 19827 | + | |
| 18893 | 19828 | |
| 18894 | 19829 | static void JimAliasCmdDelete(Jim_Interp *interp, void *privData) |
| 18895 | 19830 | { |
| 18896 | 19831 | Jim_Obj *prefixListObj = privData; |
| 18897 | 19832 | Jim_DecrRefCount(interp, prefixListObj); |
| 18898 | 19833 | } |
| 19834 | + | |
| 18899 | 19835 | |
| 18900 | 19836 | static int Jim_AliasCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18901 | 19837 | { |
| 18902 | - Jim_Obj *prefixListObj; | |
| 18903 | - | |
| 18904 | - if (argc < 3) { | |
| 18905 | - Jim_WrongNumArgs(interp, 1, argv, "newname command ?args ...?"); | |
| 18906 | - return JIM_ERR; | |
| 18907 | - } | |
| 18908 | - | |
| 18909 | - prefixListObj = Jim_NewListObj(interp, argv + 2, argc - 2); | |
| 19838 | + Jim_Obj *prefixListObj = Jim_NewListObj(interp, argv + 2, argc - 2); | |
| 18910 | 19839 | Jim_IncrRefCount(prefixListObj); |
| 18911 | 19840 | Jim_SetResult(interp, argv[1]); |
| 18912 | 19841 | |
| 18913 | - return Jim_CreateCommandObj(interp, argv[1], JimAliasCmd, prefixListObj, JimAliasCmdDelete); | |
| 19842 | + Jim_RegisterCommand(interp, argv[1], JimAliasCmd, JimAliasCmdDelete, NULL, NULL, 0, -1, JIM_CMD_ISALIAS, prefixListObj); | |
| 19843 | + return JIM_OK; | |
| 18914 | 19844 | } |
| 18915 | 19845 | |
| 18916 | 19846 | |
| 18917 | 19847 | static int Jim_ProcCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18918 | 19848 | { |
| 18919 | 19849 | Jim_Cmd *cmd; |
| 18920 | 19850 | |
| 18921 | - if (argc != 4 && argc != 5) { | |
| 18922 | - Jim_WrongNumArgs(interp, 1, argv, "name arglist ?statics? body"); | |
| 18923 | - return JIM_ERR; | |
| 18924 | - } | |
| 18925 | - | |
| 18926 | 19851 | if (argc == 4) { |
| 18927 | 19852 | cmd = JimCreateProcedureCmd(interp, argv[2], NULL, argv[3], NULL); |
| 18928 | 19853 | } |
| 18929 | 19854 | else { |
| 18930 | 19855 | cmd = JimCreateProcedureCmd(interp, argv[2], argv[3], argv[4], NULL); |
| @@ -18947,15 +19872,10 @@ | ||
| 18947 | 19872 | } |
| 18948 | 19873 | |
| 18949 | 19874 | |
| 18950 | 19875 | static int Jim_XtraceCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18951 | 19876 | { |
| 18952 | - if (argc != 2) { | |
| 18953 | - Jim_WrongNumArgs(interp, 1, argv, "callback"); | |
| 18954 | - return JIM_ERR; | |
| 18955 | - } | |
| 18956 | - | |
| 18957 | 19877 | if (interp->traceCmdObj) { |
| 18958 | 19878 | Jim_DecrRefCount(interp, interp->traceCmdObj); |
| 18959 | 19879 | interp->traceCmdObj = NULL; |
| 18960 | 19880 | } |
| 18961 | 19881 | |
| @@ -18970,15 +19890,10 @@ | ||
| 18970 | 19890 | |
| 18971 | 19891 | static int Jim_LocalCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18972 | 19892 | { |
| 18973 | 19893 | int retcode; |
| 18974 | 19894 | |
| 18975 | - if (argc < 2) { | |
| 18976 | - Jim_WrongNumArgs(interp, 1, argv, "cmd ?args ...?"); | |
| 18977 | - return JIM_ERR; | |
| 18978 | - } | |
| 18979 | - | |
| 18980 | 19895 | |
| 18981 | 19896 | interp->local++; |
| 18982 | 19897 | retcode = Jim_EvalObjVector(interp, argc - 1, argv + 1); |
| 18983 | 19898 | interp->local--; |
| 18984 | 19899 | |
| @@ -18990,11 +19905,11 @@ | ||
| 18990 | 19905 | if (Jim_GetCommand(interp, cmdNameObj, JIM_ERRMSG) == NULL) { |
| 18991 | 19906 | return JIM_ERR; |
| 18992 | 19907 | } |
| 18993 | 19908 | if (interp->framePtr->localCommands == NULL) { |
| 18994 | 19909 | interp->framePtr->localCommands = Jim_Alloc(sizeof(*interp->framePtr->localCommands)); |
| 18995 | - Jim_InitStack(interp->framePtr->localCommands); | |
| 19910 | + Jim_StackInit(interp->framePtr->localCommands, NULL); | |
| 18996 | 19911 | } |
| 18997 | 19912 | Jim_IncrRefCount(cmdNameObj); |
| 18998 | 19913 | Jim_StackPush(interp->framePtr->localCommands, cmdNameObj); |
| 18999 | 19914 | } |
| 19000 | 19915 | |
| @@ -19002,89 +19917,129 @@ | ||
| 19002 | 19917 | } |
| 19003 | 19918 | |
| 19004 | 19919 | |
| 19005 | 19920 | static int Jim_UpcallCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 19006 | 19921 | { |
| 19007 | - if (argc < 2) { | |
| 19008 | - Jim_WrongNumArgs(interp, 1, argv, "cmd ?args ...?"); | |
| 19009 | - return JIM_ERR; | |
| 19010 | - } | |
| 19011 | - else { | |
| 19012 | - int retcode; | |
| 19013 | - | |
| 19014 | - Jim_Cmd *cmdPtr = Jim_GetCommand(interp, argv[1], JIM_ERRMSG); | |
| 19015 | - if (cmdPtr == NULL || !cmdPtr->isproc || !cmdPtr->prevCmd) { | |
| 19016 | - Jim_SetResultFormatted(interp, "no previous command: \"%#s\"", argv[1]); | |
| 19017 | - return JIM_ERR; | |
| 19018 | - } | |
| 19019 | - | |
| 19020 | - cmdPtr->u.proc.upcall++; | |
| 19021 | - JimIncrCmdRefCount(cmdPtr); | |
| 19022 | - | |
| 19023 | - | |
| 19024 | - retcode = Jim_EvalObjVector(interp, argc - 1, argv + 1); | |
| 19025 | - | |
| 19026 | - | |
| 19027 | - cmdPtr->u.proc.upcall--; | |
| 19028 | - JimDecrCmdRefCount(interp, cmdPtr); | |
| 19029 | - | |
| 19030 | - return retcode; | |
| 19031 | - } | |
| 19922 | + int retcode; | |
| 19923 | + | |
| 19924 | + Jim_Cmd *cmdPtr = Jim_GetCommand(interp, argv[1], JIM_ERRMSG); | |
| 19925 | + if (cmdPtr == NULL || !(cmdPtr->flags & JIM_CMD_ISPROC) || !cmdPtr->prevCmd) { | |
| 19926 | + Jim_SetResultFormatted(interp, "no previous command: \"%#s\"", argv[1]); | |
| 19927 | + return JIM_ERR; | |
| 19928 | + } | |
| 19929 | + | |
| 19930 | + cmdPtr->u.proc.upcall++; | |
| 19931 | + JimIncrCmdRefCount(cmdPtr); | |
| 19932 | + | |
| 19933 | + | |
| 19934 | + retcode = Jim_EvalObjVector(interp, argc - 1, argv + 1); | |
| 19935 | + | |
| 19936 | + | |
| 19937 | + cmdPtr->u.proc.upcall--; | |
| 19938 | + JimDecrCmdRefCount(interp, cmdPtr); | |
| 19939 | + | |
| 19940 | + return retcode; | |
| 19032 | 19941 | } |
| 19033 | 19942 | |
| 19034 | 19943 | |
| 19035 | 19944 | static int Jim_ApplyCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 19036 | 19945 | { |
| 19037 | - if (argc < 2) { | |
| 19038 | - Jim_WrongNumArgs(interp, 1, argv, "lambdaExpr ?arg ...?"); | |
| 19039 | - return JIM_ERR; | |
| 19040 | - } | |
| 19041 | - else { | |
| 19042 | - int ret; | |
| 19043 | - Jim_Cmd *cmd; | |
| 19044 | - Jim_Obj *argListObjPtr; | |
| 19045 | - Jim_Obj *bodyObjPtr; | |
| 19046 | - Jim_Obj *nsObj = NULL; | |
| 19047 | - Jim_Obj **nargv; | |
| 19048 | - | |
| 19049 | - int len = Jim_ListLength(interp, argv[1]); | |
| 19050 | - if (len != 2 && len != 3) { | |
| 19051 | - Jim_SetResultFormatted(interp, "can't interpret \"%#s\" as a lambda expression", argv[1]); | |
| 19052 | - return JIM_ERR; | |
| 19053 | - } | |
| 19054 | - | |
| 19055 | - if (len == 3) { | |
| 19946 | + int ret; | |
| 19947 | + Jim_Cmd *cmd; | |
| 19948 | + Jim_Obj *argListObjPtr; | |
| 19949 | + Jim_Obj *bodyObjPtr; | |
| 19950 | + Jim_Obj *nsObj = NULL; | |
| 19951 | + Jim_Obj **nargv; | |
| 19952 | + | |
| 19953 | + int len = Jim_ListLength(interp, argv[1]); | |
| 19954 | + if (len != 2 && len != 3) { | |
| 19955 | + Jim_SetResultFormatted(interp, "can't interpret \"%#s\" as a lambda expression", argv[1]); | |
| 19956 | + return JIM_ERR; | |
| 19957 | + } | |
| 19958 | + | |
| 19959 | + if (len == 3) { | |
| 19056 | 19960 | #ifdef jim_ext_namespace |
| 19057 | 19961 | |
| 19058 | - nsObj = Jim_ListGetIndex(interp, argv[1], 2); | |
| 19059 | -#else | |
| 19060 | - Jim_SetResultString(interp, "namespaces not enabled", -1); | |
| 19061 | - return JIM_ERR; | |
| 19062 | -#endif | |
| 19063 | - } | |
| 19064 | - argListObjPtr = Jim_ListGetIndex(interp, argv[1], 0); | |
| 19065 | - bodyObjPtr = Jim_ListGetIndex(interp, argv[1], 1); | |
| 19066 | - | |
| 19067 | - cmd = JimCreateProcedureCmd(interp, argListObjPtr, NULL, bodyObjPtr, nsObj); | |
| 19068 | - | |
| 19069 | - if (cmd) { | |
| 19070 | - | |
| 19071 | - nargv = Jim_Alloc((argc - 2 + 1) * sizeof(*nargv)); | |
| 19072 | - nargv[0] = Jim_NewStringObj(interp, "apply lambdaExpr", -1); | |
| 19073 | - Jim_IncrRefCount(nargv[0]); | |
| 19074 | - memcpy(&nargv[1], argv + 2, (argc - 2) * sizeof(*nargv)); | |
| 19075 | - ret = JimCallProcedure(interp, cmd, argc - 2 + 1, nargv); | |
| 19076 | - Jim_DecrRefCount(interp, nargv[0]); | |
| 19077 | - Jim_Free(nargv); | |
| 19078 | - | |
| 19079 | - JimDecrCmdRefCount(interp, cmd); | |
| 19080 | - return ret; | |
| 19081 | - } | |
| 19082 | - return JIM_ERR; | |
| 19083 | - } | |
| 19084 | -} | |
| 19085 | - | |
| 19962 | + nsObj = Jim_ListGetIndex(interp, argv[1], 2); | |
| 19963 | +#else | |
| 19964 | + Jim_SetResultString(interp, "namespaces not enabled", -1); | |
| 19965 | + return JIM_ERR; | |
| 19966 | +#endif | |
| 19967 | + } | |
| 19968 | + argListObjPtr = Jim_ListGetIndex(interp, argv[1], 0); | |
| 19969 | + bodyObjPtr = Jim_ListGetIndex(interp, argv[1], 1); | |
| 19970 | + | |
| 19971 | + cmd = JimCreateProcedureCmd(interp, argListObjPtr, NULL, bodyObjPtr, nsObj); | |
| 19972 | + | |
| 19973 | + if (cmd) { | |
| 19974 | + | |
| 19975 | + nargv = Jim_Alloc((argc - 2 + 1) * sizeof(*nargv)); | |
| 19976 | + nargv[0] = Jim_NewStringObj(interp, "apply lambdaExpr", -1); | |
| 19977 | + Jim_IncrRefCount(nargv[0]); | |
| 19978 | + memcpy(&nargv[1], argv + 2, (argc - 2) * sizeof(*nargv)); | |
| 19979 | + ret = JimCallProcedure(interp, cmd, argc - 2 + 1, nargv); | |
| 19980 | + Jim_DecrRefCount(interp, nargv[0]); | |
| 19981 | + Jim_Free(nargv); | |
| 19982 | + | |
| 19983 | + JimDecrCmdRefCount(interp, cmd); | |
| 19984 | + return ret; | |
| 19985 | + } | |
| 19986 | + return JIM_ERR; | |
| 19987 | +} | |
| 19988 | + | |
| 19989 | +#ifdef JIM_TAINT | |
| 19990 | +static int JimTaintVariable(Jim_Interp *interp, Jim_Obj *nameObjPtr, int taint) | |
| 19991 | +{ | |
| 19992 | + Jim_Obj *valueObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG | JIM_UNSHARED); | |
| 19993 | + | |
| 19994 | + if (valueObjPtr == NULL) { | |
| 19995 | + return JIM_ERR; | |
| 19996 | + } | |
| 19997 | + | |
| 19998 | + if (Jim_IsShared(valueObjPtr)) { | |
| 19999 | + valueObjPtr = Jim_DuplicateObj(interp, valueObjPtr); | |
| 20000 | + Jim_SetVariable(interp, nameObjPtr, valueObjPtr); | |
| 20001 | + } | |
| 20002 | + | |
| 20003 | + if (taint && nameObjPtr->typePtr == &dictSubstObjType) { | |
| 20004 | + | |
| 20005 | + Jim_Obj *objPtr = Jim_GetVariable(interp, nameObjPtr->internalRep.dictSubstValue.varNameObjPtr, JIM_NONE); | |
| 20006 | + if (objPtr) { | |
| 20007 | + SetStringFromAny(interp, valueObjPtr); | |
| 20008 | + valueObjPtr->taint = taint; | |
| 20009 | + objPtr->taint |= taint; | |
| 20010 | + } | |
| 20011 | + } | |
| 20012 | + | |
| 20013 | + | |
| 20014 | + SetStringFromAny(interp, valueObjPtr); | |
| 20015 | + valueObjPtr->taint = taint; | |
| 20016 | + | |
| 20017 | + //printf("taint of %s is %d\n", valueObjPtr->bytes, valueObjPtr->taint); | |
| 20018 | + return JIM_OK; | |
| 20019 | +} | |
| 20020 | + | |
| 20021 | + | |
| 20022 | +static int Jim_TaintCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) | |
| 20023 | +{ | |
| 20024 | + if (argc != 2) { | |
| 20025 | + Jim_WrongNumArgs(interp, 1, argv, "varname"); | |
| 20026 | + return JIM_ERR; | |
| 20027 | + } | |
| 20028 | + return JimTaintVariable(interp, argv[1], 1); | |
| 20029 | +} | |
| 20030 | + | |
| 20031 | + | |
| 20032 | +static int Jim_UntaintCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) | |
| 20033 | +{ | |
| 20034 | + if (argc != 2) { | |
| 20035 | + Jim_WrongNumArgs(interp, 1, argv, "varname"); | |
| 20036 | + return JIM_ERR; | |
| 20037 | + } | |
| 20038 | + return JimTaintVariable(interp, argv[1], 0); | |
| 20039 | +} | |
| 20040 | +#endif | |
| 19086 | 20041 | |
| 19087 | 20042 | |
| 19088 | 20043 | static int Jim_ConcatCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 19089 | 20044 | { |
| 19090 | 20045 | Jim_SetResult(interp, Jim_ConcatObj(interp, argc - 1, argv + 1)); |
| @@ -19110,12 +20065,11 @@ | ||
| 19110 | 20065 | return JIM_ERR; |
| 19111 | 20066 | } |
| 19112 | 20067 | |
| 19113 | 20068 | |
| 19114 | 20069 | if (argc < 3) { |
| 19115 | - Jim_WrongNumArgs(interp, 1, argv, "?level? otherVar localVar ?otherVar localVar ...?"); | |
| 19116 | - return JIM_ERR; | |
| 20070 | + return JIM_USAGE; | |
| 19117 | 20071 | } |
| 19118 | 20072 | |
| 19119 | 20073 | |
| 19120 | 20074 | for (i = 1; i < argc; i += 2) { |
| 19121 | 20075 | if (Jim_SetVariableLink(interp, argv[i + 1], argv[i], targetCallFrame) != JIM_OK) |
| @@ -19127,14 +20081,10 @@ | ||
| 19127 | 20081 | |
| 19128 | 20082 | static int Jim_GlobalCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 19129 | 20083 | { |
| 19130 | 20084 | int i; |
| 19131 | 20085 | |
| 19132 | - if (argc < 2) { | |
| 19133 | - Jim_WrongNumArgs(interp, 1, argv, "varName ?varName ...?"); | |
| 19134 | - return JIM_ERR; | |
| 19135 | - } | |
| 19136 | 20086 | |
| 19137 | 20087 | if (interp->framePtr->level == 0) |
| 19138 | 20088 | return JIM_OK; |
| 19139 | 20089 | for (i = 1; i < argc; i++) { |
| 19140 | 20090 | |
| @@ -19244,11 +20194,11 @@ | ||
| 19244 | 20194 | OPT_COUNT |
| 19245 | 20195 | }; |
| 19246 | 20196 | static const jim_subcmd_type cmds[OPT_COUNT + 1] = { |
| 19247 | 20197 | JIM_DEF_SUBCMD("bytelength", "string", 1, 1), |
| 19248 | 20198 | JIM_DEF_SUBCMD("byterange", "string first last", 3, 3), |
| 19249 | - JIM_DEF_SUBCMD("cat", "?...?", 0, -1), | |
| 20199 | + JIM_DEF_SUBCMD("cat", "?string ...?", 0, -1), | |
| 19250 | 20200 | JIM_DEF_SUBCMD("compare", "?-nocase? ?-length int? string1 string2", 2, 5), |
| 19251 | 20201 | JIM_DEF_SUBCMD("equal", "?-nocase? ?-length int? string1 string2", 2, 5), |
| 19252 | 20202 | JIM_DEF_SUBCMD("first", "subString string ?index?", 2, 3), |
| 19253 | 20203 | JIM_DEF_SUBCMD("index", "string index", 2, 2), |
| 19254 | 20204 | JIM_DEF_SUBCMD("is", "class ?-strict? str", 2, 3), |
| @@ -19264,11 +20214,11 @@ | ||
| 19264 | 20214 | JIM_DEF_SUBCMD("totitle", "string", 1, 1), |
| 19265 | 20215 | JIM_DEF_SUBCMD("toupper", "string", 1, 1), |
| 19266 | 20216 | JIM_DEF_SUBCMD("trim", "string ?trimchars?", 1, 2), |
| 19267 | 20217 | JIM_DEF_SUBCMD("trimleft", "string ?trimchars?", 1, 2), |
| 19268 | 20218 | JIM_DEF_SUBCMD("trimright", "string ?trimchars?", 1, 2), |
| 19269 | - { } | |
| 20219 | + { NULL } | |
| 19270 | 20220 | }; |
| 19271 | 20221 | const jim_subcmd_type *ct = Jim_ParseSubCmd(interp, cmds, argc, argv); |
| 19272 | 20222 | if (!ct) { |
| 19273 | 20223 | return JIM_ERR; |
| 19274 | 20224 | } |
| @@ -19562,14 +20512,10 @@ | ||
| 19562 | 20512 | static int Jim_TimeCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 19563 | 20513 | { |
| 19564 | 20514 | long i, count = 1; |
| 19565 | 20515 | jim_wide start, elapsed; |
| 19566 | 20516 | |
| 19567 | - if (argc < 2) { | |
| 19568 | - Jim_WrongNumArgs(interp, 1, argv, "script ?count?"); | |
| 19569 | - return JIM_ERR; | |
| 19570 | - } | |
| 19571 | 20517 | if (argc == 3) { |
| 19572 | 20518 | if (Jim_GetLong(interp, argv[2], &count) != JIM_OK) |
| 19573 | 20519 | return JIM_ERR; |
| 19574 | 20520 | } |
| 19575 | 20521 | if (count < 0) |
| @@ -19594,23 +20540,19 @@ | ||
| 19594 | 20540 | Jim_AppendString(interp, Jim_GetResult(interp)," microseconds per iteration", -1); |
| 19595 | 20541 | return JIM_OK; |
| 19596 | 20542 | } |
| 19597 | 20543 | |
| 19598 | 20544 | |
| 19599 | -static int Jim_TimeRateCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) | |
| 20545 | +static int Jim_TimerateCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) | |
| 19600 | 20546 | { |
| 19601 | 20547 | long us = 0; |
| 19602 | 20548 | jim_wide start, delta, overhead; |
| 19603 | 20549 | Jim_Obj *objPtr; |
| 19604 | 20550 | double us_per_iter; |
| 19605 | 20551 | int count; |
| 19606 | 20552 | int n; |
| 19607 | 20553 | |
| 19608 | - if (argc < 2) { | |
| 19609 | - Jim_WrongNumArgs(interp, 1, argv, "script ?milliseconds?"); | |
| 19610 | - return JIM_ERR; | |
| 19611 | - } | |
| 19612 | 20554 | if (argc == 3) { |
| 19613 | 20555 | if (Jim_GetLong(interp, argv[2], &us) != JIM_OK) |
| 19614 | 20556 | return JIM_ERR; |
| 19615 | 20557 | us *= 1000; |
| 19616 | 20558 | } |
| @@ -19663,22 +20605,19 @@ | ||
| 19663 | 20605 | |
| 19664 | 20606 | static int Jim_ExitCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 19665 | 20607 | { |
| 19666 | 20608 | long exitCode = 0; |
| 19667 | 20609 | |
| 19668 | - if (argc > 2) { | |
| 19669 | - Jim_WrongNumArgs(interp, 1, argv, "?exitCode?"); | |
| 19670 | - return JIM_ERR; | |
| 19671 | - } | |
| 19672 | 20610 | if (argc == 2) { |
| 19673 | 20611 | if (Jim_GetLong(interp, argv[1], &exitCode) != JIM_OK) |
| 19674 | 20612 | return JIM_ERR; |
| 19675 | 20613 | Jim_SetResult(interp, argv[1]); |
| 19676 | 20614 | } |
| 19677 | 20615 | interp->exitCode = exitCode; |
| 19678 | 20616 | return JIM_EXIT; |
| 19679 | 20617 | } |
| 20618 | + | |
| 19680 | 20619 | |
| 19681 | 20620 | static int JimMatchReturnCodes(Jim_Interp *interp, Jim_Obj *retcodeListObj, int rc) |
| 19682 | 20621 | { |
| 19683 | 20622 | int len = Jim_ListLength(interp, retcodeListObj); |
| 19684 | 20623 | int i; |
| @@ -19695,14 +20634,10 @@ | ||
| 19695 | 20634 | } |
| 19696 | 20635 | |
| 19697 | 20636 | |
| 19698 | 20637 | static int JimCatchTryHelper(Jim_Interp *interp, int istry, int argc, Jim_Obj *const *argv) |
| 19699 | 20638 | { |
| 19700 | - static const char * const wrongargs_catchtry[2] = { | |
| 19701 | - "?-?no?code ... --? script ?resultVarName? ?optionVarName?", | |
| 19702 | - "?-?no?code ... --? script ?on|trap codes vars script? ... ?finally script?" | |
| 19703 | - }; | |
| 19704 | 20639 | int exitCode = 0; |
| 19705 | 20640 | int i; |
| 19706 | 20641 | int sig = 0; |
| 19707 | 20642 | int ok; |
| 19708 | 20643 | Jim_Obj *finallyScriptObj = NULL; |
| @@ -19748,11 +20683,11 @@ | ||
| 19748 | 20683 | } |
| 19749 | 20684 | if (option < 0) { |
| 19750 | 20685 | option = Jim_FindByName(arg, jimReturnCodes, jimReturnCodesSize); |
| 19751 | 20686 | } |
| 19752 | 20687 | if (option < 0) { |
| 19753 | - goto wrongargs; | |
| 20688 | + return JIM_USAGE; | |
| 19754 | 20689 | } |
| 19755 | 20690 | |
| 19756 | 20691 | if (ignore) { |
| 19757 | 20692 | ignore_mask |= ((jim_wide)1 << option); |
| 19758 | 20693 | } |
| @@ -19762,13 +20697,11 @@ | ||
| 19762 | 20697 | } |
| 19763 | 20698 | |
| 19764 | 20699 | idx = i; |
| 19765 | 20700 | |
| 19766 | 20701 | if (argc - idx < 1) { |
| 19767 | -wrongargs: | |
| 19768 | - Jim_WrongNumArgs(interp, 1, argv, wrongargs_catchtry[istry]); | |
| 19769 | - return JIM_ERR; | |
| 20702 | + return JIM_USAGE; | |
| 19770 | 20703 | } |
| 19771 | 20704 | |
| 19772 | 20705 | if ((ignore_mask & (1 << JIM_SIGNAL)) == 0) { |
| 19773 | 20706 | sig++; |
| 19774 | 20707 | } |
| @@ -19779,11 +20712,11 @@ | ||
| 19779 | 20712 | exitCode = JIM_SIGNAL; |
| 19780 | 20713 | } |
| 19781 | 20714 | else { |
| 19782 | 20715 | exitCode = Jim_EvalObj(interp, argv[idx]); |
| 19783 | 20716 | |
| 19784 | - interp->errorFlag = 0; | |
| 20717 | + interp->hasErrorStackTrace = 0; | |
| 19785 | 20718 | } |
| 19786 | 20719 | interp->signal_level -= sig; |
| 19787 | 20720 | |
| 19788 | 20721 | errorCodeObj = Jim_GetGlobalVariableStr(interp, "errorCode", JIM_NONE); |
| 19789 | 20722 | |
| @@ -19800,30 +20733,36 @@ | ||
| 19800 | 20733 | } |
| 19801 | 20734 | switch (option) { |
| 19802 | 20735 | case TRY_ON: |
| 19803 | 20736 | case TRY_TRAP: |
| 19804 | 20737 | if (idx + 4 > argc) { |
| 19805 | - goto wrongargs; | |
| 20738 | + return JIM_USAGE; | |
| 19806 | 20739 | } |
| 19807 | 20740 | if (option == TRY_ON) { |
| 19808 | 20741 | ret = JimMatchReturnCodes(interp, argv[idx + 1], exitCode); |
| 19809 | 20742 | if (ret > JIM_OK) { |
| 19810 | - goto wrongargs; | |
| 20743 | + return JIM_USAGE; | |
| 19811 | 20744 | } |
| 19812 | 20745 | } |
| 19813 | 20746 | else if (errorCodeObj) { |
| 19814 | 20747 | int len = Jim_ListLength(interp, argv[idx + 1]); |
| 19815 | - int i; | |
| 19816 | - | |
| 19817 | - ret = JIM_OK; | |
| 19818 | - | |
| 19819 | - for (i = 0; i < len; i++) { | |
| 19820 | - Jim_Obj *matchObj = Jim_ListGetIndex(interp, argv[idx + 1], i); | |
| 19821 | - Jim_Obj *objPtr = Jim_ListGetIndex(interp, errorCodeObj, i); | |
| 19822 | - if (Jim_StringCompareObj(interp, matchObj, objPtr, 0) != 0) { | |
| 19823 | - ret = -1; | |
| 19824 | - break; | |
| 20748 | + | |
| 20749 | + if (len > Jim_ListLength(interp, errorCodeObj)) { | |
| 20750 | + | |
| 20751 | + ret = -1; | |
| 20752 | + } | |
| 20753 | + else { | |
| 20754 | + int i; | |
| 20755 | + ret = JIM_OK; | |
| 20756 | + | |
| 20757 | + for (i = 0; i < len; i++) { | |
| 20758 | + Jim_Obj *matchObj = Jim_ListGetIndex(interp, argv[idx + 1], i); | |
| 20759 | + Jim_Obj *objPtr = Jim_ListGetIndex(interp, errorCodeObj, i); | |
| 20760 | + if (Jim_StringCompareObj(interp, matchObj, objPtr, 0) != 0) { | |
| 20761 | + ret = -1; | |
| 20762 | + break; | |
| 20763 | + } | |
| 19825 | 20764 | } |
| 19826 | 20765 | } |
| 19827 | 20766 | } |
| 19828 | 20767 | else { |
| 19829 | 20768 | |
| @@ -19837,11 +20776,11 @@ | ||
| 19837 | 20776 | } |
| 19838 | 20777 | idx += 4; |
| 19839 | 20778 | break; |
| 19840 | 20779 | case TRY_FINALLY: |
| 19841 | 20780 | if (idx + 2 != argc) { |
| 19842 | - goto wrongargs; | |
| 20781 | + return JIM_USAGE; | |
| 19843 | 20782 | } |
| 19844 | 20783 | finallyScriptObj = argv[idx + 1]; |
| 19845 | 20784 | idx += 2; |
| 19846 | 20785 | break; |
| 19847 | 20786 | } |
| @@ -19945,15 +20884,10 @@ | ||
| 19945 | 20884 | |
| 19946 | 20885 | |
| 19947 | 20886 | |
| 19948 | 20887 | static int Jim_RenameCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 19949 | 20888 | { |
| 19950 | - if (argc != 3) { | |
| 19951 | - Jim_WrongNumArgs(interp, 1, argv, "oldName newName"); | |
| 19952 | - return JIM_ERR; | |
| 19953 | - } | |
| 19954 | - | |
| 19955 | 20889 | return Jim_RenameCommand(interp, argv[1], argv[2]); |
| 19956 | 20890 | } |
| 19957 | 20891 | |
| 19958 | 20892 | #define JIM_DICTMATCH_KEYS 0x0001 |
| 19959 | 20893 | #define JIM_DICTMATCH_VALUES 0x002 |
| @@ -20043,10 +20977,11 @@ | ||
| 20043 | 20977 | snprintf(buffer, sizeof(buffer), "%d entries in table, %d buckets", dict->len, dict->size); |
| 20044 | 20978 | output = Jim_NewStringObj(interp, buffer, -1); |
| 20045 | 20979 | Jim_SetResult(interp, output); |
| 20046 | 20980 | return JIM_OK; |
| 20047 | 20981 | } |
| 20982 | + | |
| 20048 | 20983 | |
| 20049 | 20984 | static int Jim_EvalEnsemble(Jim_Interp *interp, const char *basecmd, const char *subcmd, int argc, Jim_Obj *const *argv) |
| 20050 | 20985 | { |
| 20051 | 20986 | Jim_Obj *prefixObj = Jim_NewStringObj(interp, basecmd, -1); |
| 20052 | 20987 | |
| @@ -20157,11 +21092,11 @@ | ||
| 20157 | 21092 | JIM_DEF_SUBCMD("remove", "dictionary ?key ...?", 1, -1), |
| 20158 | 21093 | JIM_DEF_SUBCMD("values", "dictionary ?pattern?", 1, 2), |
| 20159 | 21094 | JIM_DEF_SUBCMD("for", "vars dictionary script", 3, 3), |
| 20160 | 21095 | JIM_DEF_SUBCMD("replace", "dictionary ?key value ...?", 1, -1), |
| 20161 | 21096 | JIM_DEF_SUBCMD("update", "varName ?arg ...? script", 2, -1), |
| 20162 | - { } | |
| 21097 | + { NULL } | |
| 20163 | 21098 | }; |
| 20164 | 21099 | const jim_subcmd_type *ct = Jim_ParseSubCmd(interp, cmds, argc, argv); |
| 20165 | 21100 | if (!ct) { |
| 20166 | 21101 | return JIM_ERR; |
| 20167 | 21102 | } |
| @@ -20195,11 +21130,11 @@ | ||
| 20195 | 21130 | } |
| 20196 | 21131 | return JIM_OK; |
| 20197 | 21132 | } |
| 20198 | 21133 | |
| 20199 | 21134 | case OPT_SET: |
| 20200 | - return Jim_SetDictKeysVector(interp, argv[2], argv + 3, argc - 4, argv[argc - 1], JIM_ERRMSG); | |
| 21135 | + return Jim_SetDictKeysVector(interp, argv[2], argv + 3, argc - 4, argv[argc - 1], JIM_ERRMSG | JIM_UNSHARED); | |
| 20201 | 21136 | |
| 20202 | 21137 | case OPT_EXISTS:{ |
| 20203 | 21138 | int rc = Jim_DictKeysVector(interp, argv[2], argv + 3, argc - 3, &objPtr, JIM_NONE); |
| 20204 | 21139 | if (rc < 0) { |
| 20205 | 21140 | return JIM_ERR; |
| @@ -20207,11 +21142,11 @@ | ||
| 20207 | 21142 | Jim_SetResultBool(interp, rc == JIM_OK); |
| 20208 | 21143 | return JIM_OK; |
| 20209 | 21144 | } |
| 20210 | 21145 | |
| 20211 | 21146 | case OPT_UNSET: |
| 20212 | - if (Jim_SetDictKeysVector(interp, argv[2], argv + 3, argc - 3, NULL, JIM_NONE) != JIM_OK) { | |
| 21147 | + if (Jim_SetDictKeysVector(interp, argv[2], argv + 3, argc - 3, NULL, JIM_UNSHARED) != JIM_OK) { | |
| 20213 | 21148 | return JIM_ERR; |
| 20214 | 21149 | } |
| 20215 | 21150 | return JIM_OK; |
| 20216 | 21151 | |
| 20217 | 21152 | case OPT_VALUES: |
| @@ -20270,14 +21205,10 @@ | ||
| 20270 | 21205 | { OPT_NOBACKSLASHES, OPT_NOCOMMANDS, OPT_NOVARIABLES }; |
| 20271 | 21206 | int i; |
| 20272 | 21207 | int flags = JIM_SUBST_FLAG; |
| 20273 | 21208 | Jim_Obj *objPtr; |
| 20274 | 21209 | |
| 20275 | - if (argc < 2) { | |
| 20276 | - Jim_WrongNumArgs(interp, 1, argv, "?options? string"); | |
| 20277 | - return JIM_ERR; | |
| 20278 | - } | |
| 20279 | 21210 | for (i = 1; i < (argc - 1); i++) { |
| 20280 | 21211 | int option; |
| 20281 | 21212 | |
| 20282 | 21213 | if (Jim_GetEnum(interp, argv[i], options, &option, NULL, |
| 20283 | 21214 | JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK) { |
| @@ -20300,11 +21231,24 @@ | ||
| 20300 | 21231 | } |
| 20301 | 21232 | Jim_SetResult(interp, objPtr); |
| 20302 | 21233 | return JIM_OK; |
| 20303 | 21234 | } |
| 20304 | 21235 | |
| 21236 | + | |
| 21237 | +static int Jim_LsubstCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) | |
| 21238 | +{ | |
| 21239 | + if (argc == 3) { | |
| 21240 | + if (Jim_CompareStringImmediate(interp, argv[1], "-line")) { | |
| 21241 | + return JimListSubstObj(interp, argv[2], JIM_LSUBST_LINE); | |
| 21242 | + } | |
| 21243 | + return JIM_USAGE; | |
| 21244 | + } | |
| 21245 | + return JimListSubstObj(interp, argv[1], 0); | |
| 21246 | +} | |
| 21247 | + | |
| 20305 | 21248 | #ifdef jim_ext_namespace |
| 21249 | + | |
| 20306 | 21250 | static int JimIsGlobalNamespace(Jim_Obj *objPtr) |
| 20307 | 21251 | { |
| 20308 | 21252 | int len; |
| 20309 | 21253 | const char *str = Jim_GetString(objPtr, &len); |
| 20310 | 21254 | return len >= 2 && str[0] == ':' && str[1] == ':'; |
| @@ -20313,23 +21257,25 @@ | ||
| 20313 | 21257 | |
| 20314 | 21258 | |
| 20315 | 21259 | static int Jim_InfoCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 20316 | 21260 | { |
| 20317 | 21261 | Jim_Obj *objPtr; |
| 20318 | - int mode = 0; | |
| 21262 | + int mode = 1; | |
| 20319 | 21263 | |
| 20320 | 21264 | |
| 20321 | 21265 | enum { |
| 20322 | 21266 | INFO_ALIAS, |
| 21267 | + INFO_ALIASES, | |
| 20323 | 21268 | INFO_ARGS, |
| 20324 | 21269 | INFO_BODY, |
| 20325 | 21270 | INFO_CHANNELS, |
| 20326 | 21271 | INFO_COMMANDS, |
| 20327 | 21272 | INFO_COMPLETE, |
| 20328 | 21273 | INFO_EXISTS, |
| 20329 | 21274 | INFO_FRAME, |
| 20330 | 21275 | INFO_GLOBALS, |
| 21276 | + INFO_HELP, | |
| 20331 | 21277 | INFO_HOSTNAME, |
| 20332 | 21278 | INFO_LEVEL, |
| 20333 | 21279 | INFO_LOCALS, |
| 20334 | 21280 | INFO_NAMEOFEXECUTABLE, |
| 20335 | 21281 | INFO_PATCHLEVEL, |
| @@ -20338,39 +21284,45 @@ | ||
| 20338 | 21284 | INFO_RETURNCODES, |
| 20339 | 21285 | INFO_SCRIPT, |
| 20340 | 21286 | INFO_SOURCE, |
| 20341 | 21287 | INFO_STACKTRACE, |
| 20342 | 21288 | INFO_STATICS, |
| 21289 | + INFO_TAINTED, | |
| 21290 | + INFO_USAGE, | |
| 20343 | 21291 | INFO_VARS, |
| 20344 | 21292 | INFO_VERSION, |
| 20345 | 21293 | INFO_COUNT |
| 20346 | 21294 | }; |
| 20347 | 21295 | static const jim_subcmd_type cmds[INFO_COUNT + 1] = { |
| 20348 | 21296 | JIM_DEF_SUBCMD("alias", "command", 1, 1), |
| 21297 | + JIM_DEF_SUBCMD("aliases", "?-all? ?pattern?", 0, 2), | |
| 20349 | 21298 | JIM_DEF_SUBCMD("args", "procname", 1, 1), |
| 20350 | 21299 | JIM_DEF_SUBCMD("body", "procname", 1, 1), |
| 20351 | - JIM_DEF_SUBCMD("channels", "?pattern?", 0, 1), | |
| 20352 | - JIM_DEF_SUBCMD("commands", "?pattern?", 0, 1), | |
| 21300 | + JIM_DEF_SUBCMD("channels", "?-all? ?pattern?", 0, 2), | |
| 21301 | + JIM_DEF_SUBCMD("commands", "?-all? ?pattern?", 0, 2), | |
| 20353 | 21302 | JIM_DEF_SUBCMD("complete", "script ?missing?", 1, 2), |
| 20354 | 21303 | JIM_DEF_SUBCMD("exists", "varName", 1, 1), |
| 20355 | 21304 | JIM_DEF_SUBCMD("frame", "?levelNum?", 0, 1), |
| 20356 | 21305 | JIM_DEF_SUBCMD("globals", "?pattern?", 0, 1), |
| 21306 | + JIM_DEF_SUBCMD("help", "command", 1, 1), | |
| 20357 | 21307 | JIM_DEF_SUBCMD("hostname", NULL, 0, 0), |
| 20358 | 21308 | JIM_DEF_SUBCMD("level", "?levelNum?", 0, 1), |
| 20359 | 21309 | JIM_DEF_SUBCMD("locals", "?pattern?", 0, 1), |
| 20360 | 21310 | JIM_DEF_SUBCMD("nameofexecutable", NULL, 0, 0), |
| 20361 | 21311 | JIM_DEF_SUBCMD("patchlevel", NULL, 0, 0), |
| 20362 | - JIM_DEF_SUBCMD("procs", "?pattern?", 0, 1), | |
| 21312 | + JIM_DEF_SUBCMD("procs", "?-all? ?pattern?", 0, 2), | |
| 20363 | 21313 | JIM_DEF_SUBCMD("references", NULL, 0, 0), |
| 20364 | 21314 | JIM_DEF_SUBCMD("returncodes", "?code?", 0, 1), |
| 20365 | 21315 | JIM_DEF_SUBCMD("script", "?filename?", 0, 1), |
| 20366 | 21316 | JIM_DEF_SUBCMD("source", "source ?filename line?", 1, 3), |
| 20367 | 21317 | JIM_DEF_SUBCMD("stacktrace", NULL, 0, 0), |
| 20368 | 21318 | JIM_DEF_SUBCMD("statics", "procname", 1, 1), |
| 21319 | + JIM_DEF_SUBCMD("tainted", "value", 1, 1), | |
| 21320 | + JIM_DEF_SUBCMD("usage", "command", 1, 1), | |
| 20369 | 21321 | JIM_DEF_SUBCMD("vars", "?pattern?", 0, 1), |
| 20370 | 21322 | JIM_DEF_SUBCMD("version", NULL, 0, 0), |
| 20371 | - { } | |
| 21323 | + { NULL } | |
| 20372 | 21324 | }; |
| 20373 | 21325 | const jim_subcmd_type *ct; |
| 20374 | 21326 | #ifdef jim_ext_namespace |
| 20375 | 21327 | int nons = 0; |
| 20376 | 21328 | |
| @@ -20401,45 +21353,66 @@ | ||
| 20401 | 21353 | Jim_Cmd *cmdPtr; |
| 20402 | 21354 | |
| 20403 | 21355 | if ((cmdPtr = Jim_GetCommand(interp, argv[2], JIM_ERRMSG)) == NULL) { |
| 20404 | 21356 | return JIM_ERR; |
| 20405 | 21357 | } |
| 20406 | - if (cmdPtr->isproc || cmdPtr->u.native.cmdProc != JimAliasCmd) { | |
| 21358 | + if ((cmdPtr->flags & JIM_CMD_ISALIAS) == 0) { | |
| 20407 | 21359 | Jim_SetResultFormatted(interp, "command \"%#s\" is not an alias", argv[2]); |
| 20408 | 21360 | return JIM_ERR; |
| 20409 | 21361 | } |
| 20410 | 21362 | Jim_SetResult(interp, (Jim_Obj *)cmdPtr->u.native.privData); |
| 20411 | 21363 | return JIM_OK; |
| 20412 | 21364 | } |
| 20413 | 21365 | |
| 21366 | + case INFO_TAINTED: | |
| 21367 | + if (argc != 3) { | |
| 21368 | + Jim_WrongNumArgs(interp, 2, argv, "value"); | |
| 21369 | + return JIM_ERR; | |
| 21370 | + } | |
| 21371 | + Jim_SetResultBool(interp, argv[2]->taint != 0); | |
| 21372 | + return JIM_OK; | |
| 20414 | 21373 | case INFO_CHANNELS: |
| 20415 | - mode++; | |
| 21374 | + mode <<= 1; | |
| 20416 | 21375 | #ifndef jim_ext_aio |
| 20417 | 21376 | Jim_SetResultString(interp, "aio not enabled", -1); |
| 20418 | 21377 | return JIM_ERR; |
| 20419 | 21378 | #endif |
| 21379 | + | |
| 21380 | + case INFO_ALIASES: | |
| 21381 | + mode <<= 1; | |
| 20420 | 21382 | |
| 20421 | 21383 | case INFO_PROCS: |
| 20422 | - mode++; | |
| 21384 | + mode <<= 1; | |
| 20423 | 21385 | |
| 20424 | - case INFO_COMMANDS: | |
| 21386 | + case INFO_COMMANDS:{ | |
| 21387 | + int n = 0; | |
| 20425 | 21388 | |
| 21389 | + if (argc > 2 && Jim_CompareStringImmediate(interp, argv[2], "-all")) { | |
| 21390 | + mode |= JIM_CMDLIST_ALL; | |
| 21391 | + n++; | |
| 21392 | + } | |
| 21393 | + if (argc < 2 + n || argc > 3 + n) { | |
| 21394 | + Jim_SetResultFormatted(interp, "wrong # args: should be \"info %#s %s\"", argv[1], cmds[option].args); | |
| 21395 | + return JIM_ERR; | |
| 21396 | + } | |
| 20426 | 21397 | #ifdef jim_ext_namespace |
| 20427 | 21398 | if (!nons) { |
| 21399 | + | |
| 20428 | 21400 | if (Jim_Length(interp->framePtr->nsObj) || (argc == 3 && JimIsGlobalNamespace(argv[2]))) { |
| 20429 | 21401 | return Jim_EvalPrefix(interp, "namespace info", argc - 1, argv + 1); |
| 20430 | 21402 | } |
| 20431 | 21403 | } |
| 20432 | 21404 | #endif |
| 20433 | - Jim_SetResult(interp, JimCommandsList(interp, (argc == 3) ? argv[2] : NULL, mode)); | |
| 21405 | + Jim_SetResult(interp, JimCommandsList(interp, (argc == 3 + n) ? argv[2 + n] : NULL, mode)); | |
| 20434 | 21406 | return JIM_OK; |
| 21407 | + } | |
| 20435 | 21408 | |
| 20436 | 21409 | case INFO_VARS: |
| 20437 | - mode++; | |
| 21410 | + mode <<= 1; | |
| 20438 | 21411 | |
| 20439 | 21412 | case INFO_LOCALS: |
| 20440 | - mode++; | |
| 21413 | + mode <<= 1; | |
| 20441 | 21414 | |
| 20442 | 21415 | case INFO_GLOBALS: |
| 20443 | 21416 | |
| 20444 | 21417 | #ifdef jim_ext_namespace |
| 20445 | 21418 | if (!nons) { |
| @@ -20512,20 +21485,41 @@ | ||
| 20512 | 21485 | return JIM_ERR; |
| 20513 | 21486 | } |
| 20514 | 21487 | Jim_SetResult(interp, objPtr); |
| 20515 | 21488 | } |
| 20516 | 21489 | return JIM_OK; |
| 21490 | + | |
| 21491 | + case INFO_USAGE: | |
| 21492 | + case INFO_HELP:{ | |
| 21493 | + Jim_Cmd *cmdPtr = Jim_GetCommand(interp, argv[2], JIM_ERRMSG); | |
| 21494 | + if (!cmdPtr) { | |
| 21495 | + return JIM_ERR; | |
| 21496 | + } | |
| 21497 | + if (option == INFO_USAGE) { | |
| 21498 | + Jim_SetResult(interp, JimCmdUsage(interp, argv[2], cmdPtr)); | |
| 21499 | + return JIM_OK; | |
| 21500 | + } | |
| 21501 | + else if ((cmdPtr->flags & JIM_CMD_ISPROC) == 0) { | |
| 21502 | + if (cmdPtr->u.native.help) { | |
| 21503 | + Jim_SetResultString(interp, cmdPtr->u.native.help, -1); | |
| 21504 | + return JIM_OK; | |
| 21505 | + } | |
| 21506 | + } | |
| 21507 | + | |
| 21508 | + Jim_SetResultFormatted(interp, "No help available for command \"%#s\"", argv[2]); | |
| 21509 | + return JIM_OK; | |
| 21510 | + } | |
| 20517 | 21511 | |
| 20518 | 21512 | case INFO_BODY: |
| 20519 | 21513 | case INFO_STATICS: |
| 20520 | 21514 | case INFO_ARGS:{ |
| 20521 | 21515 | Jim_Cmd *cmdPtr; |
| 20522 | 21516 | |
| 20523 | 21517 | if ((cmdPtr = Jim_GetCommand(interp, argv[2], JIM_ERRMSG)) == NULL) { |
| 20524 | 21518 | return JIM_ERR; |
| 20525 | 21519 | } |
| 20526 | - if (!cmdPtr->isproc) { | |
| 21520 | + if ((cmdPtr->flags & JIM_CMD_ISPROC) == 0) { | |
| 20527 | 21521 | Jim_SetResultFormatted(interp, "command \"%#s\" is not a procedure", argv[2]); |
| 20528 | 21522 | return JIM_ERR; |
| 20529 | 21523 | } |
| 20530 | 21524 | switch (option) { |
| 20531 | 21525 | #ifdef JIM_NO_INTROSPECTION |
| @@ -20548,16 +21542,21 @@ | ||
| 20548 | 21542 | break; |
| 20549 | 21543 | } |
| 20550 | 21544 | return JIM_OK; |
| 20551 | 21545 | } |
| 20552 | 21546 | |
| 20553 | - case INFO_VERSION: | |
| 20554 | - case INFO_PATCHLEVEL:{ | |
| 20555 | - char buf[(JIM_INTEGER_SPACE * 2) + 1]; | |
| 21547 | + case INFO_PATCHLEVEL: | |
| 20556 | 21548 | |
| 20557 | - sprintf(buf, "%d.%d", JIM_VERSION / 100, JIM_VERSION % 100); | |
| 20558 | - Jim_SetResultString(interp, buf, -1); | |
| 21549 | +#ifdef JIM_GITVERSION | |
| 21550 | + Jim_SetResultString(interp, JIM_GITVERSION, -1); | |
| 21551 | + return JIM_OK; | |
| 21552 | +#endif | |
| 21553 | + | |
| 21554 | + case INFO_VERSION:{ | |
| 21555 | + char versionbuf[64]; | |
| 21556 | + snprintf(versionbuf, sizeof(versionbuf), "%d.%d", JIM_VERSION / 100, JIM_VERSION % 100); | |
| 21557 | + Jim_SetResultString(interp, versionbuf, -1); | |
| 20559 | 21558 | return JIM_OK; |
| 20560 | 21559 | } |
| 20561 | 21560 | |
| 20562 | 21561 | case INFO_COMPLETE: { |
| 20563 | 21562 | char missing; |
| @@ -20623,15 +21622,15 @@ | ||
| 20623 | 21622 | { |
| 20624 | 21623 | Jim_Obj *objPtr; |
| 20625 | 21624 | int result = 0; |
| 20626 | 21625 | |
| 20627 | 21626 | static const char * const options[] = { |
| 20628 | - "-command", "-proc", "-alias", "-var", NULL | |
| 21627 | + "-command", "-proc", "-alias", "-channel", "-var", NULL | |
| 20629 | 21628 | }; |
| 20630 | 21629 | enum |
| 20631 | 21630 | { |
| 20632 | - OPT_COMMAND, OPT_PROC, OPT_ALIAS, OPT_VAR | |
| 21631 | + OPT_COMMAND, OPT_PROC, OPT_ALIAS, OPT_CHANNEL, OPT_VAR | |
| 20633 | 21632 | }; |
| 20634 | 21633 | int option; |
| 20635 | 21634 | |
| 20636 | 21635 | if (argc == 2) { |
| 20637 | 21636 | option = OPT_VAR; |
| @@ -20642,12 +21641,11 @@ | ||
| 20642 | 21641 | return JIM_ERR; |
| 20643 | 21642 | } |
| 20644 | 21643 | objPtr = argv[2]; |
| 20645 | 21644 | } |
| 20646 | 21645 | else { |
| 20647 | - Jim_WrongNumArgs(interp, 1, argv, "?option? name"); | |
| 20648 | - return JIM_ERR; | |
| 21646 | + return JIM_USAGE; | |
| 20649 | 21647 | } |
| 20650 | 21648 | |
| 20651 | 21649 | if (option == OPT_VAR) { |
| 20652 | 21650 | result = Jim_GetVariable(interp, objPtr, 0) != NULL; |
| 20653 | 21651 | } |
| @@ -20660,15 +21658,19 @@ | ||
| 20660 | 21658 | case OPT_COMMAND: |
| 20661 | 21659 | result = 1; |
| 20662 | 21660 | break; |
| 20663 | 21661 | |
| 20664 | 21662 | case OPT_ALIAS: |
| 20665 | - result = cmd->isproc == 0 && cmd->u.native.cmdProc == JimAliasCmd; | |
| 21663 | + result = (cmd->flags & JIM_CMD_ISALIAS) != 0; | |
| 20666 | 21664 | break; |
| 20667 | 21665 | |
| 20668 | 21666 | case OPT_PROC: |
| 20669 | - result = cmd->isproc; | |
| 21667 | + result = (cmd->flags & JIM_CMD_ISPROC) != 0; | |
| 21668 | + break; | |
| 21669 | + | |
| 21670 | + case OPT_CHANNEL: | |
| 21671 | + result = (cmd->flags & JIM_CMD_ISCHANNEL) != 0; | |
| 20670 | 21672 | break; |
| 20671 | 21673 | } |
| 20672 | 21674 | } |
| 20673 | 21675 | } |
| 20674 | 21676 | Jim_SetResultBool(interp, result); |
| @@ -20682,15 +21684,10 @@ | ||
| 20682 | 21684 | int splitLen, strLen; |
| 20683 | 21685 | Jim_Obj *resObjPtr; |
| 20684 | 21686 | int c; |
| 20685 | 21687 | int len; |
| 20686 | 21688 | |
| 20687 | - if (argc != 2 && argc != 3) { | |
| 20688 | - Jim_WrongNumArgs(interp, 1, argv, "string ?splitChars?"); | |
| 20689 | - return JIM_ERR; | |
| 20690 | - } | |
| 20691 | - | |
| 20692 | 21689 | str = Jim_GetString(argv[1], &len); |
| 20693 | 21690 | if (len == 0) { |
| 20694 | 21691 | return JIM_OK; |
| 20695 | 21692 | } |
| 20696 | 21693 | strLen = Jim_Utf8Length(interp, argv[1]); |
| @@ -20765,14 +21762,10 @@ | ||
| 20765 | 21762 | static int Jim_JoinCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 20766 | 21763 | { |
| 20767 | 21764 | const char *joinStr; |
| 20768 | 21765 | int joinStrLen; |
| 20769 | 21766 | |
| 20770 | - if (argc != 2 && argc != 3) { | |
| 20771 | - Jim_WrongNumArgs(interp, 1, argv, "list ?joinString?"); | |
| 20772 | - return JIM_ERR; | |
| 20773 | - } | |
| 20774 | 21767 | |
| 20775 | 21768 | if (argc == 2) { |
| 20776 | 21769 | joinStr = " "; |
| 20777 | 21770 | joinStrLen = 1; |
| 20778 | 21771 | } |
| @@ -20786,14 +21779,10 @@ | ||
| 20786 | 21779 | |
| 20787 | 21780 | static int Jim_FormatCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 20788 | 21781 | { |
| 20789 | 21782 | Jim_Obj *objPtr; |
| 20790 | 21783 | |
| 20791 | - if (argc < 2) { | |
| 20792 | - Jim_WrongNumArgs(interp, 1, argv, "formatString ?arg arg ...?"); | |
| 20793 | - return JIM_ERR; | |
| 20794 | - } | |
| 20795 | 21784 | objPtr = Jim_FormatString(interp, argv[1], argc - 2, argv + 2); |
| 20796 | 21785 | if (objPtr == NULL) |
| 20797 | 21786 | return JIM_ERR; |
| 20798 | 21787 | Jim_SetResult(interp, objPtr); |
| 20799 | 21788 | return JIM_OK; |
| @@ -20803,14 +21792,10 @@ | ||
| 20803 | 21792 | static int Jim_ScanCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 20804 | 21793 | { |
| 20805 | 21794 | Jim_Obj *listPtr, **outVec; |
| 20806 | 21795 | int outc, i; |
| 20807 | 21796 | |
| 20808 | - if (argc < 3) { | |
| 20809 | - Jim_WrongNumArgs(interp, 1, argv, "string format ?varName varName ...?"); | |
| 20810 | - return JIM_ERR; | |
| 20811 | - } | |
| 20812 | 21797 | if (argv[2]->typePtr != &scanFmtStringObjType) |
| 20813 | 21798 | SetScanFmtFromAny(interp, argv[2]); |
| 20814 | 21799 | if (FormatGetError(argv[2]) != 0) { |
| 20815 | 21800 | Jim_SetResultString(interp, FormatGetError(argv[2]), -1); |
| 20816 | 21801 | return JIM_ERR; |
| @@ -20876,14 +21861,10 @@ | ||
| 20876 | 21861 | } |
| 20877 | 21862 | |
| 20878 | 21863 | |
| 20879 | 21864 | static int Jim_ErrorCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 20880 | 21865 | { |
| 20881 | - if (argc != 2 && argc != 3) { | |
| 20882 | - Jim_WrongNumArgs(interp, 1, argv, "message ?stacktrace?"); | |
| 20883 | - return JIM_ERR; | |
| 20884 | - } | |
| 20885 | 21866 | Jim_SetResult(interp, argv[1]); |
| 20886 | 21867 | if (argc == 3) { |
| 20887 | 21868 | JimSetStackTrace(interp, argv[2]); |
| 20888 | 21869 | return JIM_ERR; |
| 20889 | 21870 | } |
| @@ -20893,14 +21874,10 @@ | ||
| 20893 | 21874 | |
| 20894 | 21875 | static int Jim_LrangeCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 20895 | 21876 | { |
| 20896 | 21877 | Jim_Obj *objPtr; |
| 20897 | 21878 | |
| 20898 | - if (argc != 4) { | |
| 20899 | - Jim_WrongNumArgs(interp, 1, argv, "list first last"); | |
| 20900 | - return JIM_ERR; | |
| 20901 | - } | |
| 20902 | 21879 | if ((objPtr = Jim_ListRange(interp, argv[1], argv[2], argv[3])) == NULL) |
| 20903 | 21880 | return JIM_ERR; |
| 20904 | 21881 | Jim_SetResult(interp, objPtr); |
| 20905 | 21882 | return JIM_OK; |
| 20906 | 21883 | } |
| @@ -20910,12 +21887,11 @@ | ||
| 20910 | 21887 | { |
| 20911 | 21888 | Jim_Obj *objPtr; |
| 20912 | 21889 | jim_wide count; |
| 20913 | 21890 | |
| 20914 | 21891 | if (argc < 2 || Jim_GetWideExpr(interp, argv[1], &count) != JIM_OK || count < 0) { |
| 20915 | - Jim_WrongNumArgs(interp, 1, argv, "count ?value ...?"); | |
| 20916 | - return JIM_ERR; | |
| 21892 | + return JIM_USAGE; | |
| 20917 | 21893 | } |
| 20918 | 21894 | if (count == 0 || argc == 2) { |
| 20919 | 21895 | Jim_SetEmptyResult(interp); |
| 20920 | 21896 | return JIM_OK; |
| 20921 | 21897 | } |
| @@ -20935,23 +21911,26 @@ | ||
| 20935 | 21911 | |
| 20936 | 21912 | char **Jim_GetEnviron(void) |
| 20937 | 21913 | { |
| 20938 | 21914 | #if defined(HAVE__NSGETENVIRON) |
| 20939 | 21915 | return *_NSGetEnviron(); |
| 21916 | +#elif defined(_environ) | |
| 21917 | + return _environ; | |
| 20940 | 21918 | #else |
| 20941 | 21919 | #if !defined(NO_ENVIRON_EXTERN) |
| 20942 | 21920 | extern char **environ; |
| 20943 | 21921 | #endif |
| 20944 | - | |
| 20945 | 21922 | return environ; |
| 20946 | 21923 | #endif |
| 20947 | 21924 | } |
| 20948 | 21925 | |
| 20949 | 21926 | void Jim_SetEnviron(char **env) |
| 20950 | 21927 | { |
| 20951 | 21928 | #if defined(HAVE__NSGETENVIRON) |
| 20952 | 21929 | *_NSGetEnviron() = env; |
| 21930 | +#elif defined(_environ) | |
| 21931 | + _environ = env; | |
| 20953 | 21932 | #else |
| 20954 | 21933 | #if !defined(NO_ENVIRON_EXTERN) |
| 20955 | 21934 | extern char **environ; |
| 20956 | 21935 | #endif |
| 20957 | 21936 | |
| @@ -20983,14 +21962,10 @@ | ||
| 20983 | 21962 | |
| 20984 | 21963 | Jim_SetResult(interp, listObjPtr); |
| 20985 | 21964 | return JIM_OK; |
| 20986 | 21965 | } |
| 20987 | 21966 | |
| 20988 | - if (argc > 3) { | |
| 20989 | - Jim_WrongNumArgs(interp, 1, argv, "varName ?default?"); | |
| 20990 | - return JIM_ERR; | |
| 20991 | - } | |
| 20992 | 21967 | key = Jim_String(argv[1]); |
| 20993 | 21968 | val = getenv(key); |
| 20994 | 21969 | if (val == NULL) { |
| 20995 | 21970 | if (argc < 3) { |
| 20996 | 21971 | Jim_SetResultFormatted(interp, "environment variable \"%#s\" does not exist", argv[1]); |
| @@ -21003,41 +21978,31 @@ | ||
| 21003 | 21978 | } |
| 21004 | 21979 | |
| 21005 | 21980 | |
| 21006 | 21981 | static int Jim_SourceCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 21007 | 21982 | { |
| 21008 | - int retval; | |
| 21009 | - | |
| 21010 | - if (argc != 2) { | |
| 21011 | - Jim_WrongNumArgs(interp, 1, argv, "fileName"); | |
| 21012 | - return JIM_ERR; | |
| 21013 | - } | |
| 21014 | - retval = Jim_EvalFile(interp, Jim_String(argv[1])); | |
| 21015 | - if (retval == JIM_RETURN) | |
| 21016 | - return JIM_OK; | |
| 21017 | - return retval; | |
| 21983 | + int retval = Jim_EvalFile(interp, Jim_String(argv[1])); | |
| 21984 | + | |
| 21985 | + return retval == JIM_RETURN ? JIM_OK : retval; | |
| 21018 | 21986 | } |
| 21019 | 21987 | |
| 21020 | 21988 | |
| 21021 | 21989 | static int Jim_LreverseCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 21022 | 21990 | { |
| 21023 | 21991 | Jim_Obj *revObjPtr, **ele; |
| 21024 | 21992 | int len; |
| 21025 | 21993 | |
| 21026 | - if (argc != 2) { | |
| 21027 | - Jim_WrongNumArgs(interp, 1, argv, "list"); | |
| 21028 | - return JIM_ERR; | |
| 21029 | - } | |
| 21030 | 21994 | JimListGetElements(interp, argv[1], &len, &ele); |
| 21031 | 21995 | revObjPtr = Jim_NewListObj(interp, NULL, 0); |
| 21032 | 21996 | ListEnsureLength(revObjPtr, len); |
| 21033 | 21997 | len--; |
| 21034 | 21998 | while (len >= 0) |
| 21035 | 21999 | ListAppendElement(revObjPtr, ele[len--]); |
| 21036 | 22000 | Jim_SetResult(interp, revObjPtr); |
| 21037 | 22001 | return JIM_OK; |
| 21038 | 22002 | } |
| 22003 | + | |
| 21039 | 22004 | |
| 21040 | 22005 | static int JimRangeLen(jim_wide start, jim_wide end, jim_wide step) |
| 21041 | 22006 | { |
| 21042 | 22007 | jim_wide len; |
| 21043 | 22008 | |
| @@ -21065,14 +22030,10 @@ | ||
| 21065 | 22030 | { |
| 21066 | 22031 | jim_wide start = 0, end, step = 1; |
| 21067 | 22032 | int len, i; |
| 21068 | 22033 | Jim_Obj *objPtr; |
| 21069 | 22034 | |
| 21070 | - if (argc < 2 || argc > 4) { | |
| 21071 | - Jim_WrongNumArgs(interp, 1, argv, "?start? end ?step?"); | |
| 21072 | - return JIM_ERR; | |
| 21073 | - } | |
| 21074 | 22035 | if (argc == 2) { |
| 21075 | 22036 | if (Jim_GetWideExpr(interp, argv[1], &end) != JIM_OK) |
| 21076 | 22037 | return JIM_ERR; |
| 21077 | 22038 | } |
| 21078 | 22039 | else { |
| @@ -21097,14 +22058,10 @@ | ||
| 21097 | 22058 | |
| 21098 | 22059 | static int Jim_RandCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 21099 | 22060 | { |
| 21100 | 22061 | jim_wide min = 0, max = 0, len, maxMul; |
| 21101 | 22062 | |
| 21102 | - if (argc < 1 || argc > 3) { | |
| 21103 | - Jim_WrongNumArgs(interp, 1, argv, "?min? max"); | |
| 21104 | - return JIM_ERR; | |
| 21105 | - } | |
| 21106 | 22063 | if (argc == 1) { |
| 21107 | 22064 | max = JIM_WIDE_MAX; |
| 21108 | 22065 | } else if (argc == 2) { |
| 21109 | 22066 | if (Jim_GetWideExpr(interp, argv[1], &max) != JIM_OK) |
| 21110 | 22067 | return JIM_ERR; |
| @@ -21128,98 +22085,111 @@ | ||
| 21128 | 22085 | Jim_SetResultInt(interp, min+r); |
| 21129 | 22086 | return JIM_OK; |
| 21130 | 22087 | } |
| 21131 | 22088 | } |
| 21132 | 22089 | |
| 21133 | -static const struct { | |
| 22090 | +static const struct jim_core_cmd_def_t { | |
| 21134 | 22091 | const char *name; |
| 21135 | 22092 | Jim_CmdProc *cmdProc; |
| 22093 | + short minargs; | |
| 22094 | + short maxargs; | |
| 22095 | + const char *usage; | |
| 22096 | + int flags; | |
| 21136 | 22097 | } Jim_CoreCommandsTable[] = { |
| 21137 | - {"alias", Jim_AliasCoreCommand}, | |
| 21138 | - {"set", Jim_SetCoreCommand}, | |
| 21139 | - {"unset", Jim_UnsetCoreCommand}, | |
| 21140 | - {"puts", Jim_PutsCoreCommand}, | |
| 21141 | - {"+", Jim_AddCoreCommand}, | |
| 21142 | - {"*", Jim_MulCoreCommand}, | |
| 21143 | - {"-", Jim_SubCoreCommand}, | |
| 21144 | - {"/", Jim_DivCoreCommand}, | |
| 21145 | - {"incr", Jim_IncrCoreCommand}, | |
| 21146 | - {"while", Jim_WhileCoreCommand}, | |
| 21147 | - {"loop", Jim_LoopCoreCommand}, | |
| 21148 | - {"for", Jim_ForCoreCommand}, | |
| 21149 | - {"foreach", Jim_ForeachCoreCommand}, | |
| 21150 | - {"lmap", Jim_LmapCoreCommand}, | |
| 21151 | - {"lassign", Jim_LassignCoreCommand}, | |
| 21152 | - {"if", Jim_IfCoreCommand}, | |
| 21153 | - {"switch", Jim_SwitchCoreCommand}, | |
| 21154 | - {"list", Jim_ListCoreCommand}, | |
| 21155 | - {"lindex", Jim_LindexCoreCommand}, | |
| 21156 | - {"lset", Jim_LsetCoreCommand}, | |
| 21157 | - {"lsearch", Jim_LsearchCoreCommand}, | |
| 21158 | - {"llength", Jim_LlengthCoreCommand}, | |
| 21159 | - {"lappend", Jim_LappendCoreCommand}, | |
| 21160 | - {"linsert", Jim_LinsertCoreCommand}, | |
| 21161 | - {"lreplace", Jim_LreplaceCoreCommand}, | |
| 21162 | - {"lsort", Jim_LsortCoreCommand}, | |
| 21163 | - {"append", Jim_AppendCoreCommand}, | |
| 21164 | - {"eval", Jim_EvalCoreCommand}, | |
| 21165 | - {"uplevel", Jim_UplevelCoreCommand}, | |
| 21166 | - {"expr", Jim_ExprCoreCommand}, | |
| 21167 | - {"break", Jim_BreakCoreCommand}, | |
| 21168 | - {"continue", Jim_ContinueCoreCommand}, | |
| 21169 | - {"proc", Jim_ProcCoreCommand}, | |
| 21170 | - {"xtrace", Jim_XtraceCoreCommand}, | |
| 21171 | - {"concat", Jim_ConcatCoreCommand}, | |
| 21172 | - {"return", Jim_ReturnCoreCommand}, | |
| 21173 | - {"upvar", Jim_UpvarCoreCommand}, | |
| 21174 | - {"global", Jim_GlobalCoreCommand}, | |
| 21175 | - {"string", Jim_StringCoreCommand}, | |
| 21176 | - {"time", Jim_TimeCoreCommand}, | |
| 21177 | - {"timerate", Jim_TimeRateCoreCommand}, | |
| 21178 | - {"exit", Jim_ExitCoreCommand}, | |
| 21179 | - {"catch", Jim_CatchCoreCommand}, | |
| 21180 | - {"try", Jim_TryCoreCommand}, | |
| 22098 | + {"*", Jim_MulCoreCommand, 0, -1, "?number ...?" }, | |
| 22099 | + {"+", Jim_AddCoreCommand, 0, -1, "?number ...?" }, | |
| 22100 | + {"-", Jim_SubCoreCommand, 1, -1, "number ?number ...?" }, | |
| 22101 | + {"/", Jim_DivCoreCommand, 1, -1, "number ?number ...?" }, | |
| 22102 | + {"alias", Jim_AliasCoreCommand, 2, -1, "newname command ?args ...?" }, | |
| 22103 | + {"append", Jim_AppendCoreCommand, 1, -1, "varName ?value ...?" }, | |
| 22104 | + {"apply", Jim_ApplyCoreCommand, 1, -1, "lambdaExpr ?arg ...?" }, | |
| 22105 | + {"break", Jim_BreakCoreCommand, 0, 1, "?level?" }, | |
| 22106 | + {"catch", Jim_CatchCoreCommand, 1, -1, "?-?no?code ... --? script ?resultVarName? ?optionVarName?" }, | |
| 22107 | + {"concat", Jim_ConcatCoreCommand, 0, -1, "?arg ...?" }, | |
| 22108 | + {"continue", Jim_ContinueCoreCommand, 0, 1, "?level?" }, | |
| 22109 | + {"dict", Jim_DictCoreCommand, 1, -1, "subcommand ?arg ...?"}, | |
| 22110 | + {"env", Jim_EnvCoreCommand, 0, 2, "?varName? ?default?" }, | |
| 22111 | + {"error", Jim_ErrorCoreCommand, 1, 2, "message ?stacktrace?" }, | |
| 22112 | + {"eval", Jim_EvalCoreCommand, 1, -1, "arg ?arg ...?" }, | |
| 22113 | + {"exists", Jim_ExistsCoreCommand, 1, 2, "?-command|-proc|-alias|-channel|-var? name" }, | |
| 22114 | + {"exit", Jim_ExitCoreCommand, 0, 1, "?exitCode?" }, | |
| 22115 | +#ifdef JIM_COMPAT | |
| 22116 | + {"expr", Jim_ExprCoreCommand, 1, -1, "expression ?...?" }, | |
| 22117 | +#else | |
| 22118 | + {"expr", Jim_ExprCoreCommand, 1, 1, "expression" }, | |
| 22119 | +#endif | |
| 22120 | + {"for", Jim_ForCoreCommand, 4, 4, "start test next body" }, | |
| 22121 | + {"foreach", Jim_ForeachCoreCommand, 3, -1, "varList list ?varList list ...? script" }, | |
| 22122 | + {"format", Jim_FormatCoreCommand, 1, -1, "formatString ?arg arg ...?" }, | |
| 22123 | + {"global", Jim_GlobalCoreCommand, 1, -1, "varName ?varName ...?" }, | |
| 22124 | + {"if", Jim_IfCoreCommand, 2, -1, "condition ?then? trueBody ?elseif ...? ?else? ?falseBody?" }, | |
| 22125 | + {"incr", Jim_IncrCoreCommand, 1, 2, "varName ?increment?" }, | |
| 22126 | + {"info", Jim_InfoCoreCommand, 1, -1, "subcommand ?arg ...?"}, | |
| 22127 | + {"join", Jim_JoinCoreCommand, 1, 2, "list ?joinString?" }, | |
| 22128 | + {"lappend", Jim_LappendCoreCommand, 1, -1, "varName ?value value ...?" }, | |
| 22129 | + {"lassign", Jim_LassignCoreCommand, 2, -1, "varList list ?varName ...?" }, | |
| 22130 | + {"lindex", Jim_LindexCoreCommand, 1, -1, "list ?index ...?" }, | |
| 22131 | + {"linsert", Jim_LinsertCoreCommand, 2, -1, "list index ?element ...?" }, | |
| 22132 | + {"list", Jim_ListCoreCommand, 0, -1, "?arg ...?" }, | |
| 22133 | + {"llength", Jim_LlengthCoreCommand, 1, 1, "list" }, | |
| 22134 | + {"lmap", Jim_LmapCoreCommand, 3, -1, "varList list ?varList list ...? script" }, | |
| 22135 | + {"local", Jim_LocalCoreCommand, 1, -1, "cmd ?args ...?" }, | |
| 22136 | + {"loop", Jim_LoopCoreCommand, 3, 5, "var ?first? limit ?incr? body" }, | |
| 22137 | + {"lrange", Jim_LrangeCoreCommand, 3, 3, "list first last" }, | |
| 22138 | + {"lrepeat", Jim_LrepeatCoreCommand, 1, -1, "count ?value ...?" }, | |
| 22139 | + {"lreplace", Jim_LreplaceCoreCommand, 3, -1, "list first last ?element ...?" }, | |
| 22140 | + {"lreverse", Jim_LreverseCoreCommand, 1, 1, "list" }, | |
| 22141 | + {"lsearch", Jim_LsearchCoreCommand, 2, -1, "?-exact|-glob|-regexp|-command 'command'? ?-bool|-inline? ?-not? ?-nocase? ?-all? ?-stride len? ?-index val? list value" }, | |
| 22142 | + {"lset", Jim_LsetCoreCommand, 2, -1, "listVar ?index ...? value" }, | |
| 22143 | + {"lsort", Jim_LsortCoreCommand, 1, -1, "?options? list" }, | |
| 22144 | + {"lsubst", Jim_LsubstCoreCommand, 1, 2, "?-line? string" }, | |
| 22145 | + {"proc", Jim_ProcCoreCommand, 3, 4, "name arglist ?statics? body" }, | |
| 22146 | + {"puts", Jim_PutsCoreCommand, 1, 2, "?-nonewline? string" }, | |
| 22147 | + {"rand", Jim_RandCoreCommand, 0, 2, "?min? ?max?" }, | |
| 22148 | + {"range", Jim_RangeCoreCommand, 1, 3, "?start? end ?step?" }, | |
| 22149 | + {"rename", Jim_RenameCoreCommand, 2, 2, "oldName newName" }, | |
| 22150 | + {"return", Jim_ReturnCoreCommand, 0, -1, "?-code code? ?-errorinfo stacktrace? ?-level level? ?result?" }, | |
| 22151 | + {"scan", Jim_ScanCoreCommand, 2, -1, "string format ?varName varName ...?" }, | |
| 22152 | + {"set", Jim_SetCoreCommand, 1, 2, "varName ?newValue?" }, | |
| 22153 | + {"source", Jim_SourceCoreCommand, 1, 1, "fileName", JIM_CMD_NOTAINT }, | |
| 22154 | + {"split", Jim_SplitCoreCommand, 1, 2, "string ?splitChars?" }, | |
| 22155 | + {"stacktrace", Jim_StacktraceCoreCommand, 0, 2, "?firstlevel? ?lastlevel?" }, | |
| 22156 | + {"string", Jim_StringCoreCommand, 1, -1, "subcommand ?arg ...?" }, | |
| 22157 | + {"subst", Jim_SubstCoreCommand, 1, 4, "?options? string" }, | |
| 22158 | + {"switch", Jim_SwitchCoreCommand, 2, -1, "?options? string pattern body ... ?default body? or pattern body ?pattern body ...?" }, | |
| 22159 | + {"tailcall", Jim_TailcallCoreCommand, 0, -1, "?cmd arg ...?" }, | |
| 22160 | + {"time", Jim_TimeCoreCommand, 1, 2, "script ?count?" }, | |
| 22161 | + {"timerate", Jim_TimerateCoreCommand, 1, 2, "script ?milliseconds?" }, | |
| 22162 | + {"try", Jim_TryCoreCommand, 1, -1, "?-?no?code ... --? script ?on|trap codes vars script? ... ?finally script?" }, | |
| 22163 | + {"unset", Jim_UnsetCoreCommand, 0, -1, "?-nocomplain? ?--? ?varName ...?"}, | |
| 22164 | + {"upcall", Jim_UpcallCoreCommand, 1, -1, "cmd ?args ...?" }, | |
| 22165 | + {"uplevel", Jim_UplevelCoreCommand, 1, -1, "?level? command ?arg ...?" }, | |
| 22166 | + {"upvar", Jim_UpvarCoreCommand, 2, -1, "?level? otherVar myVar ?otherVar myVar ...?"}, | |
| 22167 | + {"while", Jim_WhileCoreCommand, 2, 2, "condition body" }, | |
| 22168 | + {"xtrace", Jim_XtraceCoreCommand, 1, 1, "callback" }, | |
| 21181 | 22169 | #ifdef JIM_REFERENCES |
| 21182 | - {"ref", Jim_RefCoreCommand}, | |
| 21183 | - {"getref", Jim_GetrefCoreCommand}, | |
| 21184 | - {"setref", Jim_SetrefCoreCommand}, | |
| 21185 | - {"finalize", Jim_FinalizeCoreCommand}, | |
| 21186 | - {"collect", Jim_CollectCoreCommand}, | |
| 22170 | + {"collect", Jim_CollectCoreCommand, 0, 0, "" }, | |
| 22171 | + {"finalize", Jim_FinalizeCoreCommand, 1, 2, "reference ?finalizerProc?" }, | |
| 22172 | + {"getref", Jim_GetrefCoreCommand, 1, 1, "reference" }, | |
| 22173 | + {"ref", Jim_RefCoreCommand, 2, 3, "string tag ?finalizer?" }, | |
| 22174 | + {"setref", Jim_SetrefCoreCommand, 2, 2, "reference newValue" }, | |
| 21187 | 22175 | #endif |
| 21188 | - {"rename", Jim_RenameCoreCommand}, | |
| 21189 | - {"dict", Jim_DictCoreCommand}, | |
| 21190 | - {"subst", Jim_SubstCoreCommand}, | |
| 21191 | - {"info", Jim_InfoCoreCommand}, | |
| 21192 | - {"exists", Jim_ExistsCoreCommand}, | |
| 21193 | - {"split", Jim_SplitCoreCommand}, | |
| 21194 | - {"join", Jim_JoinCoreCommand}, | |
| 21195 | - {"format", Jim_FormatCoreCommand}, | |
| 21196 | - {"scan", Jim_ScanCoreCommand}, | |
| 21197 | - {"error", Jim_ErrorCoreCommand}, | |
| 21198 | - {"lrange", Jim_LrangeCoreCommand}, | |
| 21199 | - {"lrepeat", Jim_LrepeatCoreCommand}, | |
| 21200 | - {"env", Jim_EnvCoreCommand}, | |
| 21201 | - {"source", Jim_SourceCoreCommand}, | |
| 21202 | - {"lreverse", Jim_LreverseCoreCommand}, | |
| 21203 | - {"range", Jim_RangeCoreCommand}, | |
| 21204 | - {"rand", Jim_RandCoreCommand}, | |
| 21205 | - {"tailcall", Jim_TailcallCoreCommand}, | |
| 21206 | - {"local", Jim_LocalCoreCommand}, | |
| 21207 | - {"upcall", Jim_UpcallCoreCommand}, | |
| 21208 | - {"apply", Jim_ApplyCoreCommand}, | |
| 21209 | - {"stacktrace", Jim_StacktraceCoreCommand}, | |
| 22176 | +#ifdef JIM_TAINT | |
| 22177 | + {"taint", Jim_TaintCoreCommand, 1, 1, "varname"}, | |
| 22178 | + {"untaint", Jim_UntaintCoreCommand, 1, 1, "varname"}, | |
| 22179 | +#endif | |
| 21210 | 22180 | {NULL, NULL}, |
| 21211 | 22181 | }; |
| 21212 | 22182 | |
| 21213 | 22183 | void Jim_RegisterCoreCommands(Jim_Interp *interp) |
| 21214 | 22184 | { |
| 21215 | - int i = 0; | |
| 22185 | + const struct jim_core_cmd_def_t *c; | |
| 21216 | 22186 | |
| 21217 | - while (Jim_CoreCommandsTable[i].name != NULL) { | |
| 21218 | - Jim_CreateCommand(interp, | |
| 21219 | - Jim_CoreCommandsTable[i].name, Jim_CoreCommandsTable[i].cmdProc, NULL, NULL); | |
| 21220 | - i++; | |
| 22187 | + for (c = Jim_CoreCommandsTable; c->name; c++) { | |
| 22188 | + | |
| 22189 | + assert(c->usage); | |
| 22190 | + Jim_RegisterCmd(interp, c->name, c->usage, c->minargs, c->maxargs, c->cmdProc, NULL, NULL, c->flags); | |
| 21221 | 22191 | } |
| 21222 | 22192 | } |
| 21223 | 22193 | |
| 21224 | 22194 | void Jim_MakeErrorMessage(Jim_Interp *interp) |
| 21225 | 22195 | { |
| @@ -21662,18 +22632,26 @@ | ||
| 21662 | 22632 | int Jim_CallSubCmd(Jim_Interp *interp, const jim_subcmd_type * ct, int argc, Jim_Obj *const *argv) |
| 21663 | 22633 | { |
| 21664 | 22634 | int ret = JIM_ERR; |
| 21665 | 22635 | |
| 21666 | 22636 | if (ct) { |
| 21667 | - if (ct->flags & JIM_MODFLAG_FULLARGV) { | |
| 22637 | + if ((ct->flags & JIM_MODFLAG_NOTAINT) && Jim_CheckTaint(interp, JIM_TAINT_ANY)) { | |
| 22638 | + ret = JIM_SUBCMD_TAINTED; | |
| 22639 | + } | |
| 22640 | + else if (ct->flags & JIM_MODFLAG_FULLARGV) { | |
| 21668 | 22641 | ret = ct->function(interp, argc, argv); |
| 21669 | 22642 | } |
| 21670 | 22643 | else { |
| 21671 | 22644 | ret = ct->function(interp, argc - 2, argv + 2); |
| 21672 | 22645 | } |
| 21673 | 22646 | if (ret < 0) { |
| 21674 | - Jim_SubCmdArgError(interp, ct, argv[0]); | |
| 22647 | + if (ret == JIM_SUBCMD_TAINTED) { | |
| 22648 | + Jim_SetTaintError(interp, 2, argv); | |
| 22649 | + } | |
| 22650 | + else { | |
| 22651 | + Jim_SubCmdArgError(interp, ct, argv[0]); | |
| 22652 | + } | |
| 21675 | 22653 | ret = JIM_ERR; |
| 21676 | 22654 | } |
| 21677 | 22655 | } |
| 21678 | 22656 | return ret; |
| 21679 | 22657 | } |
| @@ -21683,10 +22661,22 @@ | ||
| 21683 | 22661 | const jim_subcmd_type *ct = |
| 21684 | 22662 | Jim_ParseSubCmd(interp, (const jim_subcmd_type *)Jim_CmdPrivData(interp), argc, argv); |
| 21685 | 22663 | |
| 21686 | 22664 | return Jim_CallSubCmd(interp, ct, argc, argv); |
| 21687 | 22665 | } |
| 22666 | + | |
| 22667 | +Jim_Cmd *Jim_RegisterSubCmd(Jim_Interp *interp, const char *cmdname, | |
| 22668 | + const jim_subcmd_type *command_table, Jim_DelCmdProc *delProc) | |
| 22669 | +{ | |
| 22670 | + return Jim_RegisterCmd(interp, cmdname, | |
| 22671 | + "subcommand ?arg ...?", | |
| 22672 | + 1, -1, | |
| 22673 | + Jim_SubCmdProc, | |
| 22674 | + delProc, | |
| 22675 | + (void *)command_table, | |
| 22676 | + 0); | |
| 22677 | +} | |
| 21688 | 22678 | |
| 21689 | 22679 | #include <ctype.h> |
| 21690 | 22680 | #include <stdlib.h> |
| 21691 | 22681 | #include <string.h> |
| 21692 | 22682 | #include <stdio.h> |
| @@ -21873,11 +22863,11 @@ | ||
| 21873 | 22863 | gotPrecision = 1; |
| 21874 | 22864 | format += step; |
| 21875 | 22865 | step = utf8_tounicode(format, &ch); |
| 21876 | 22866 | } |
| 21877 | 22867 | if (isdigit(ch)) { |
| 21878 | - precision = strtoul(format, &end, 10); | |
| 22868 | + precision = strtol(format, &end, 10); | |
| 21879 | 22869 | format = end; |
| 21880 | 22870 | step = utf8_tounicode(format, &ch); |
| 21881 | 22871 | } else if (ch == '*') { |
| 21882 | 22872 | if (objIndex >= objc - 1) { |
| 21883 | 22873 | msg = badIndex[gotXpg]; |
| @@ -22160,12 +23150,11 @@ | ||
| 22160 | 23150 | |
| 22161 | 23151 | |
| 22162 | 23152 | |
| 22163 | 23153 | |
| 22164 | 23154 | #define FAIL(R,M) { (R)->err = (M); return (M); } |
| 22165 | -#define ISMULT(c) ((c) == '*' || (c) == '+' || (c) == '?' || (c) == '{') | |
| 22166 | -#define META "^$.[()|?{+*" | |
| 23155 | +#define META "^$.[()|" | |
| 22167 | 23156 | |
| 22168 | 23157 | #define HASWIDTH 1 |
| 22169 | 23158 | #define SIMPLE 2 |
| 22170 | 23159 | #define SPSTART 4 |
| 22171 | 23160 | #define WORST 0 |
| @@ -22193,10 +23182,26 @@ | ||
| 22193 | 23182 | static int regnarrate = 0; |
| 22194 | 23183 | static void regdump(regex_t *preg); |
| 22195 | 23184 | static const char *regprop( int op ); |
| 22196 | 23185 | #endif |
| 22197 | 23186 | |
| 23187 | + | |
| 23188 | +static int str_is_mult(const char *s) | |
| 23189 | +{ | |
| 23190 | + switch (*s) { | |
| 23191 | + case '*': | |
| 23192 | + case '+': | |
| 23193 | + case '?': | |
| 23194 | + return 1; | |
| 23195 | + case '{': | |
| 23196 | + if (isdigit(UCHAR(s[1]))) { | |
| 23197 | + return 1; | |
| 23198 | + } | |
| 23199 | + break; | |
| 23200 | + } | |
| 23201 | + return 0; | |
| 23202 | +} | |
| 22198 | 23203 | |
| 22199 | 23204 | static int str_int_len(const int *seq) |
| 22200 | 23205 | { |
| 22201 | 23206 | int n = 0; |
| 22202 | 23207 | while (*seq++) { |
| @@ -22203,10 +23208,45 @@ | ||
| 22203 | 23208 | n++; |
| 22204 | 23209 | } |
| 22205 | 23210 | return n; |
| 22206 | 23211 | } |
| 22207 | 23212 | |
| 23213 | + | |
| 23214 | +static char *reg_expanded_new_pattern(const char *exp) | |
| 23215 | +{ | |
| 23216 | + | |
| 23217 | + char *newexp = strdup(exp); | |
| 23218 | + char *d = newexp; | |
| 23219 | + const char *s = exp; | |
| 23220 | + int escape = 0; | |
| 23221 | + | |
| 23222 | + while (*s) { | |
| 23223 | + if (escape) { | |
| 23224 | + escape = 0; | |
| 23225 | + continue; | |
| 23226 | + } | |
| 23227 | + else if (*s == '\\') { | |
| 23228 | + escape = 1; | |
| 23229 | + } | |
| 23230 | + else if (strchr(" \t\r\n\f\v", *s)) { | |
| 23231 | + s++; | |
| 23232 | + continue; | |
| 23233 | + } | |
| 23234 | + else if (*s == '#') { | |
| 23235 | + | |
| 23236 | + s++; | |
| 23237 | + while (*s && *s != '\n') { | |
| 23238 | + s++; | |
| 23239 | + } | |
| 23240 | + continue; | |
| 23241 | + } | |
| 23242 | + *d++ = *s++; | |
| 23243 | + } | |
| 23244 | + *d++ = '\0'; | |
| 23245 | + return newexp; | |
| 23246 | +} | |
| 23247 | + | |
| 22208 | 23248 | int jim_regcomp(regex_t *preg, const char *exp, int cflags) |
| 22209 | 23249 | { |
| 22210 | 23250 | int scan; |
| 22211 | 23251 | int longest; |
| 22212 | 23252 | unsigned len; |
| @@ -22217,10 +23257,15 @@ | ||
| 22217 | 23257 | #endif |
| 22218 | 23258 | memset(preg, 0, sizeof(*preg)); |
| 22219 | 23259 | |
| 22220 | 23260 | if (exp == NULL) |
| 22221 | 23261 | FAIL(preg, REG_ERR_NULL_ARGUMENT); |
| 23262 | + | |
| 23263 | + if (cflags & REG_EXPANDED) { | |
| 23264 | + preg->exp = reg_expanded_new_pattern(exp); | |
| 23265 | + exp = preg->exp; | |
| 23266 | + } | |
| 22222 | 23267 | |
| 22223 | 23268 | |
| 22224 | 23269 | preg->cflags = cflags; |
| 22225 | 23270 | preg->regparse = exp; |
| 22226 | 23271 | |
| @@ -22392,16 +23437,16 @@ | ||
| 22392 | 23437 | |
| 22393 | 23438 | ret = regatom(preg, &flags); |
| 22394 | 23439 | if (ret == 0) |
| 22395 | 23440 | return 0; |
| 22396 | 23441 | |
| 22397 | - op = *preg->regparse; | |
| 22398 | - if (!ISMULT(op)) { | |
| 23442 | + if (!str_is_mult(preg->regparse)) { | |
| 22399 | 23443 | *flagp = flags; |
| 22400 | 23444 | return(ret); |
| 22401 | 23445 | } |
| 22402 | 23446 | |
| 23447 | + op = *preg->regparse; | |
| 22403 | 23448 | if (!(flags&HASWIDTH) && op != '?') { |
| 22404 | 23449 | preg->err = REG_ERR_OPERAND_COULD_BE_EMPTY; |
| 22405 | 23450 | return 0; |
| 22406 | 23451 | } |
| 22407 | 23452 | |
| @@ -22466,11 +23511,11 @@ | ||
| 22466 | 23511 | regtail(preg, back, ret); |
| 22467 | 23512 | regtail(preg, next, back); |
| 22468 | 23513 | } |
| 22469 | 23514 | |
| 22470 | 23515 | preg->regparse++; |
| 22471 | - if (ISMULT(*preg->regparse)) { | |
| 23516 | + if (str_is_mult(preg->regparse)) { | |
| 22472 | 23517 | preg->err = REG_ERR_NESTED_COUNT; |
| 22473 | 23518 | return 0; |
| 22474 | 23519 | } |
| 22475 | 23520 | |
| 22476 | 23521 | return ret; |
| @@ -22775,16 +23820,10 @@ | ||
| 22775 | 23820 | case '\0': |
| 22776 | 23821 | case '|': |
| 22777 | 23822 | case ')': |
| 22778 | 23823 | preg->err = REG_ERR_INTERNAL; |
| 22779 | 23824 | return 0; |
| 22780 | - case '?': | |
| 22781 | - case '+': | |
| 22782 | - case '*': | |
| 22783 | - case '{': | |
| 22784 | - preg->err = REG_ERR_COUNT_FOLLOWS_NOTHING; | |
| 22785 | - return 0; | |
| 22786 | 23825 | case '\\': |
| 22787 | 23826 | ch = *preg->regparse++; |
| 22788 | 23827 | switch (ch) { |
| 22789 | 23828 | case '\0': |
| 22790 | 23829 | preg->err = REG_ERR_INVALID_ESCAPE; |
| @@ -22841,16 +23880,21 @@ | ||
| 22841 | 23880 | default: { |
| 22842 | 23881 | int added = 0; |
| 22843 | 23882 | |
| 22844 | 23883 | |
| 22845 | 23884 | preg->regparse -= n; |
| 23885 | + | |
| 23886 | + if (str_is_mult(preg->regparse)) { | |
| 23887 | + preg->err = REG_ERR_COUNT_FOLLOWS_NOTHING; | |
| 23888 | + return 0; | |
| 23889 | + } | |
| 22846 | 23890 | |
| 22847 | 23891 | ret = regnode(preg, EXACTLY); |
| 22848 | 23892 | |
| 22849 | 23893 | |
| 22850 | 23894 | |
| 22851 | - while (*preg->regparse && strchr(META, *preg->regparse) == NULL) { | |
| 23895 | + while (*preg->regparse && strchr(META, *preg->regparse) == NULL && !str_is_mult(preg->regparse)) { | |
| 22852 | 23896 | n = reg_utf8_tounicode_case(preg->regparse, &ch, (preg->cflags & REG_ICASE)); |
| 22853 | 23897 | if (ch == '\\' && preg->regparse[n]) { |
| 22854 | 23898 | if (strchr("<>mMwWdDsSAZ", preg->regparse[n])) { |
| 22855 | 23899 | |
| 22856 | 23900 | break; |
| @@ -22861,11 +23905,11 @@ | ||
| 22861 | 23905 | return 0; |
| 22862 | 23906 | } |
| 22863 | 23907 | } |
| 22864 | 23908 | |
| 22865 | 23909 | |
| 22866 | - if (ISMULT(preg->regparse[n])) { | |
| 23910 | + if (str_is_mult(&preg->regparse[n])) { | |
| 22867 | 23911 | |
| 22868 | 23912 | if (added) { |
| 22869 | 23913 | |
| 22870 | 23914 | break; |
| 22871 | 23915 | } |
| @@ -23034,11 +24078,11 @@ | ||
| 23034 | 24078 | if (regtry(preg, string)) { |
| 23035 | 24079 | return REG_NOERROR; |
| 23036 | 24080 | } |
| 23037 | 24081 | if (*string) { |
| 23038 | 24082 | nextline: |
| 23039 | - if (preg->cflags & REG_NEWLINE) { | |
| 24083 | + if (preg->cflags & REG_NEWLINE_ANCHOR) { | |
| 23040 | 24084 | |
| 23041 | 24085 | string = strchr(string, '\n'); |
| 23042 | 24086 | if (string) { |
| 23043 | 24087 | preg->regbol = ++string; |
| 23044 | 24088 | continue; |
| @@ -23144,11 +24188,11 @@ | ||
| 23144 | 24188 | return NULL; |
| 23145 | 24189 | } |
| 23146 | 24190 | |
| 23147 | 24191 | static int reg_iseol(regex_t *preg, int ch) |
| 23148 | 24192 | { |
| 23149 | - if (preg->cflags & REG_NEWLINE) { | |
| 24193 | + if (preg->cflags & REG_NEWLINE_STOP) { | |
| 23150 | 24194 | return ch == '\0' || ch == '\n'; |
| 23151 | 24195 | } |
| 23152 | 24196 | else { |
| 23153 | 24197 | return ch == '\0'; |
| 23154 | 24198 | } |
| @@ -23555,10 +24599,11 @@ | ||
| 23555 | 24599 | return snprintf(errbuf, errbuf_size, "%s", err); |
| 23556 | 24600 | } |
| 23557 | 24601 | |
| 23558 | 24602 | void jim_regfree(regex_t *preg) |
| 23559 | 24603 | { |
| 24604 | + free(preg->exp); | |
| 23560 | 24605 | free(preg->program); |
| 23561 | 24606 | } |
| 23562 | 24607 | |
| 23563 | 24608 | #endif |
| 23564 | 24609 | #include <string.h> |
| @@ -23566,11 +24611,11 @@ | ||
| 23566 | 24611 | void Jim_SetResultErrno(Jim_Interp *interp, const char *msg) |
| 23567 | 24612 | { |
| 23568 | 24613 | Jim_SetResultFormatted(interp, "%s: %s", msg, strerror(Jim_Errno())); |
| 23569 | 24614 | } |
| 23570 | 24615 | |
| 23571 | -#if defined(__MINGW32__) | |
| 24616 | +#if defined(_WIN32) || defined(WIN32) | |
| 23572 | 24617 | #include <sys/stat.h> |
| 23573 | 24618 | |
| 23574 | 24619 | int Jim_Errno(void) |
| 23575 | 24620 | { |
| 23576 | 24621 | switch (GetLastError()) { |
| @@ -23743,11 +24788,13 @@ | ||
| 23743 | 24788 | |
| 23744 | 24789 | |
| 23745 | 24790 | int Jim_MakeTempFile(Jim_Interp *interp, const char *filename_template, int unlink_file) |
| 23746 | 24791 | { |
| 23747 | 24792 | int fd; |
| 24793 | +#ifdef HAVE_UMASK | |
| 23748 | 24794 | mode_t mask; |
| 24795 | +#endif | |
| 23749 | 24796 | Jim_Obj *filenameObj; |
| 23750 | 24797 | |
| 23751 | 24798 | if (filename_template == NULL) { |
| 23752 | 24799 | const char *tmpdir = getenv("TMPDIR"); |
| 23753 | 24800 | if (tmpdir == NULL || *tmpdir == '\0' || access(tmpdir, W_OK) != 0) { |
| @@ -24394,10 +25441,14 @@ | ||
| 24394 | 25441 | if (retcode == JIM_ERR) { |
| 24395 | 25442 | JimPrintErrorMessage(interp); |
| 24396 | 25443 | } |
| 24397 | 25444 | if (retcode != JIM_EXIT) { |
| 24398 | 25445 | JimSetArgv(interp, 0, NULL); |
| 25446 | + if (!isatty(STDIN_FILENO)) { | |
| 25447 | + | |
| 25448 | + goto eval_stdin; | |
| 25449 | + } | |
| 24399 | 25450 | retcode = Jim_InteractivePrompt(interp); |
| 24400 | 25451 | } |
| 24401 | 25452 | } |
| 24402 | 25453 | else { |
| 24403 | 25454 | |
| @@ -24416,10 +25467,11 @@ | ||
| 24416 | 25467 | } |
| 24417 | 25468 | else { |
| 24418 | 25469 | Jim_SetVariableStr(interp, "argv0", Jim_NewStringObj(interp, argv[1], -1)); |
| 24419 | 25470 | JimSetArgv(interp, argc - 2, argv + 2); |
| 24420 | 25471 | if (strcmp(argv[1], "-") == 0) { |
| 25472 | +eval_stdin: | |
| 24421 | 25473 | retcode = Jim_Eval(interp, "eval [info source [stdin read] stdin 1]"); |
| 24422 | 25474 | } else { |
| 24423 | 25475 | retcode = Jim_EvalFile(interp, argv[1]); |
| 24424 | 25476 | } |
| 24425 | 25477 | } |
| 24426 | 25478 |
| --- autosetup/jimsh0.c | |
| +++ autosetup/jimsh0.c | |
| @@ -1,7 +1,7 @@ | |
| 1 | /* This is single source file, bootstrap version of Jim Tcl. See http://jim.tcl.tk/ */ |
| 2 | #define JIM_TCL_COMPAT |
| 3 | #define JIM_ANSIC |
| 4 | #define JIM_REGEXP |
| 5 | #define HAVE_NO_AUTOCONF |
| 6 | #define JIM_TINY |
| 7 | #define _JIMAUTOCONF_H |
| @@ -11,11 +11,10 @@ | |
| 11 | #define jim_ext_readdir |
| 12 | #define jim_ext_regexp |
| 13 | #define jim_ext_file |
| 14 | #define jim_ext_glob |
| 15 | #define jim_ext_exec |
| 16 | #define jim_ext_posix |
| 17 | #define jim_ext_clock |
| 18 | #define jim_ext_array |
| 19 | #define jim_ext_stdlib |
| 20 | #define jim_ext_tclcompat |
| 21 | #if defined(_MSC_VER) |
| @@ -49,11 +48,15 @@ | |
| 49 | #define vfork fork |
| 50 | #define _POSIX_SOURCE |
| 51 | #else |
| 52 | #define _GNU_SOURCE |
| 53 | #endif |
| 54 | #define HAVE_FORK |
| 55 | #define HAVE_WAITPID |
| 56 | #define HAVE_ISATTY |
| 57 | #define HAVE_MKSTEMP |
| 58 | #define HAVE_LINK |
| 59 | #define HAVE_SYS_TIME_H |
| @@ -61,11 +64,11 @@ | |
| 61 | #define HAVE_UNISTD_H |
| 62 | #define HAVE_UMASK |
| 63 | #define HAVE_PIPE |
| 64 | #define _FILE_OFFSET_BITS 64 |
| 65 | #endif |
| 66 | #define JIM_VERSION 83 |
| 67 | #ifndef JIM_WIN32COMPAT_H |
| 68 | #define JIM_WIN32COMPAT_H |
| 69 | |
| 70 | |
| 71 | |
| @@ -94,10 +97,13 @@ | |
| 94 | #pragma warning(disable:4146) |
| 95 | #endif |
| 96 | |
| 97 | #include <limits.h> |
| 98 | #define jim_wide _int64 |
| 99 | #ifndef LLONG_MAX |
| 100 | #define LLONG_MAX 9223372036854775807I64 |
| 101 | #endif |
| 102 | #ifndef LLONG_MIN |
| 103 | #define LLONG_MIN (-LLONG_MAX - 1I64) |
| @@ -108,15 +114,11 @@ | |
| 108 | #define strcasecmp _stricmp |
| 109 | #define strtoull _strtoui64 |
| 110 | |
| 111 | #include <io.h> |
| 112 | |
| 113 | struct timeval { |
| 114 | long tv_sec; |
| 115 | long tv_usec; |
| 116 | }; |
| 117 | |
| 118 | int gettimeofday(struct timeval *tv, void *unused); |
| 119 | |
| 120 | #define HAVE_OPENDIR |
| 121 | struct dirent { |
| 122 | char *d_name; |
| @@ -158,20 +160,18 @@ | |
| 158 | #ifndef JIM_UTF8 |
| 159 | #include <ctype.h> |
| 160 | |
| 161 | |
| 162 | #define utf8_strlen(S, B) ((B) < 0 ? (int)strlen(S) : (B)) |
| 163 | #define utf8_strwidth(S, B) utf8_strlen((S), (B)) |
| 164 | #define utf8_tounicode(S, CP) (*(CP) = (unsigned char)*(S), 1) |
| 165 | #define utf8_getchars(CP, C) (*(CP) = (C), 1) |
| 166 | #define utf8_upper(C) toupper(C) |
| 167 | #define utf8_title(C) toupper(C) |
| 168 | #define utf8_lower(C) tolower(C) |
| 169 | #define utf8_index(C, I) (I) |
| 170 | #define utf8_charlen(C) 1 |
| 171 | #define utf8_prev_len(S, L) 1 |
| 172 | #define utf8_width(C) 1 |
| 173 | |
| 174 | #else |
| 175 | |
| 176 | #endif |
| 177 | |
| @@ -222,24 +222,28 @@ | |
| 222 | # else |
| 223 | # define JIM_WIDE_MODIFIER "ld" |
| 224 | # define strtoull strtoul |
| 225 | # endif |
| 226 | #endif |
| 227 | |
| 228 | #define UCHAR(c) ((unsigned char)(c)) |
| 229 | |
| 230 | |
| 231 | |
| 232 | #define JIM_ABI_VERSION 101 |
| 233 | |
| 234 | #define JIM_OK 0 |
| 235 | #define JIM_ERR 1 |
| 236 | #define JIM_RETURN 2 |
| 237 | #define JIM_BREAK 3 |
| 238 | #define JIM_CONTINUE 4 |
| 239 | #define JIM_SIGNAL 5 |
| 240 | #define JIM_EXIT 6 |
| 241 | |
| 242 | #define JIM_EVAL 7 |
| 243 | |
| 244 | #define JIM_MAX_CALLFRAME_DEPTH 1000 |
| 245 | #define JIM_MAX_EVAL_DEPTH 2000 |
| @@ -247,11 +251,10 @@ | |
| 247 | |
| 248 | #define JIM_PRIV_FLAG_SHIFT 20 |
| 249 | |
| 250 | #define JIM_NONE 0 |
| 251 | #define JIM_ERRMSG 1 |
| 252 | #define JIM_ENUM_ABBREV 2 |
| 253 | #define JIM_UNSHARED 4 |
| 254 | #define JIM_MUSTEXIST 8 |
| 255 | #define JIM_NORESULT 16 |
| 256 | |
| 257 | |
| @@ -258,17 +261,26 @@ | |
| 258 | #define JIM_SUBST_NOVAR 1 |
| 259 | #define JIM_SUBST_NOCMD 2 |
| 260 | #define JIM_SUBST_NOESC 4 |
| 261 | #define JIM_SUBST_FLAG 128 |
| 262 | |
| 263 | |
| 264 | #define JIM_CASESENS 0 |
| 265 | #define JIM_NOCASE 1 |
| 266 | #define JIM_OPT_END 2 |
| 267 | |
| 268 | |
| 269 | #define JIM_PATH_LEN 1024 |
| 270 | |
| 271 | |
| 272 | #define JIM_NOTUSED(V) ((void) V) |
| 273 | |
| 274 | #define JIM_LIBPATH "auto_path" |
| @@ -277,10 +289,11 @@ | |
| 277 | |
| 278 | typedef struct Jim_Stack { |
| 279 | int len; |
| 280 | int maxlen; |
| 281 | void **vector; |
| 282 | } Jim_Stack; |
| 283 | |
| 284 | |
| 285 | typedef struct Jim_HashEntry { |
| 286 | void *key; |
| @@ -318,55 +331,69 @@ | |
| 318 | } Jim_HashTableIterator; |
| 319 | |
| 320 | |
| 321 | #define JIM_HT_INITIAL_SIZE 16 |
| 322 | |
| 323 | |
| 324 | #define Jim_FreeEntryVal(ht, entry) \ |
| 325 | if ((ht)->type->valDestructor) \ |
| 326 | (ht)->type->valDestructor((ht)->privdata, (entry)->u.val) |
| 327 | |
| 328 | #define Jim_SetHashVal(ht, entry, _val_) do { \ |
| 329 | if ((ht)->type->valDup) \ |
| 330 | (entry)->u.val = (ht)->type->valDup((ht)->privdata, (_val_)); \ |
| 331 | else \ |
| 332 | (entry)->u.val = (_val_); \ |
| 333 | } while(0) |
| 334 | |
| 335 | #define Jim_SetHashIntVal(ht, entry, _val_) (entry)->u.intval = (_val_) |
| 336 | |
| 337 | #define Jim_FreeEntryKey(ht, entry) \ |
| 338 | if ((ht)->type->keyDestructor) \ |
| 339 | (ht)->type->keyDestructor((ht)->privdata, (entry)->key) |
| 340 | |
| 341 | #define Jim_SetHashKey(ht, entry, _key_) do { \ |
| 342 | if ((ht)->type->keyDup) \ |
| 343 | (entry)->key = (ht)->type->keyDup((ht)->privdata, (_key_)); \ |
| 344 | else \ |
| 345 | (entry)->key = (void *)(_key_); \ |
| 346 | } while(0) |
| 347 | |
| 348 | #define Jim_CompareHashKeys(ht, key1, key2) \ |
| 349 | (((ht)->type->keyCompare) ? \ |
| 350 | (ht)->type->keyCompare((ht)->privdata, (key1), (key2)) : \ |
| 351 | (key1) == (key2)) |
| 352 | |
| 353 | #define Jim_HashKey(ht, key) ((ht)->type->hashFunction(key) + (ht)->uniq) |
| 354 | |
| 355 | #define Jim_GetHashEntryKey(he) ((he)->key) |
| 356 | #define Jim_GetHashEntryVal(he) ((he)->u.val) |
| 357 | #define Jim_GetHashEntryIntVal(he) ((he)->u.intval) |
| 358 | #define Jim_GetHashTableCollisions(ht) ((ht)->collisions) |
| 359 | #define Jim_GetHashTableSize(ht) ((ht)->size) |
| 360 | #define Jim_GetHashTableUsed(ht) ((ht)->used) |
| 361 | |
| 362 | |
| 363 | typedef struct Jim_Obj { |
| 364 | char *bytes; |
| 365 | const struct Jim_ObjType *typePtr; |
| 366 | int refCount; |
| 367 | int length; |
| 368 | |
| 369 | union { |
| 370 | |
| 371 | jim_wide wideValue; |
| 372 | |
| @@ -433,27 +460,33 @@ | |
| 433 | } internalRep; |
| 434 | struct Jim_Obj *prevObjPtr; |
| 435 | struct Jim_Obj *nextObjPtr; |
| 436 | } Jim_Obj; |
| 437 | |
| 438 | |
| 439 | #define Jim_IncrRefCount(objPtr) \ |
| 440 | ++(objPtr)->refCount |
| 441 | #define Jim_DecrRefCount(interp, objPtr) \ |
| 442 | if (--(objPtr)->refCount <= 0) Jim_FreeObj(interp, objPtr) |
| 443 | #define Jim_IsShared(objPtr) \ |
| 444 | ((objPtr)->refCount > 1) |
| 445 | |
| 446 | #define Jim_FreeNewObj Jim_FreeObj |
| 447 | |
| 448 | |
| 449 | #define Jim_FreeIntRep(i,o) \ |
| 450 | if ((o)->typePtr && (o)->typePtr->freeIntRepProc) \ |
| 451 | (o)->typePtr->freeIntRepProc(i, o) |
| 452 | |
| 453 | |
| 454 | #define Jim_GetIntRepPtr(o) (o)->internalRep.ptr |
| 455 | |
| 456 | |
| 457 | #define Jim_SetIntRepPtr(o, p) \ |
| 458 | (o)->internalRep.ptr = (p) |
| 459 | |
| @@ -490,12 +523,10 @@ | |
| 490 | int argc; |
| 491 | Jim_Obj *procArgsObjPtr; |
| 492 | Jim_Obj *procBodyObjPtr; |
| 493 | struct Jim_CallFrame *next; |
| 494 | Jim_Obj *nsObj; |
| 495 | Jim_Obj *unused_fileNameObj; |
| 496 | int unused_line; |
| 497 | Jim_Stack *localCommands; |
| 498 | struct Jim_Obj *tailcallObj; |
| 499 | struct Jim_Cmd *tailcallCmd; |
| 500 | } Jim_CallFrame; |
| 501 | |
| @@ -534,21 +565,31 @@ | |
| 534 | int len; |
| 535 | int maxLen; |
| 536 | unsigned int dummy; |
| 537 | } Jim_Dict; |
| 538 | |
| 539 | typedef struct Jim_Cmd { |
| 540 | int inUse; |
| 541 | int isproc; |
| 542 | struct Jim_Cmd *prevCmd; |
| 543 | Jim_Obj *cmdNameObj; |
| 544 | union { |
| 545 | struct { |
| 546 | |
| 547 | Jim_CmdProc *cmdProc; |
| 548 | Jim_DelCmdProc *delProc; |
| 549 | void *privData; |
| 550 | } native; |
| 551 | struct { |
| 552 | |
| 553 | Jim_Obj *argListObjPtr; |
| 554 | Jim_Obj *bodyObjPtr; |
| @@ -573,11 +614,10 @@ | |
| 573 | unsigned int i, j; |
| 574 | } Jim_PrngState; |
| 575 | |
| 576 | typedef struct Jim_Interp { |
| 577 | Jim_Obj *result; |
| 578 | int unused_errorLine; |
| 579 | Jim_Obj *currentFilenameObj; |
| 580 | int break_level; |
| 581 | int maxCallFrameDepth; |
| 582 | int maxEvalDepth; |
| 583 | int evalDepth; |
| @@ -601,15 +641,13 @@ | |
| 601 | int local; |
| 602 | int quitting; |
| 603 | int safeexpr; |
| 604 | Jim_Obj *liveList; |
| 605 | Jim_Obj *freeList; |
| 606 | Jim_Obj *unused_currentScriptObj; |
| 607 | Jim_EvalFrame topEvalFrame; |
| 608 | Jim_EvalFrame *evalFrame; |
| 609 | int procLevel; |
| 610 | Jim_Obj * const *unused_argv; |
| 611 | Jim_Obj *nullScriptObj; |
| 612 | Jim_Obj *emptyObj; |
| 613 | Jim_Obj *trueObj; |
| 614 | Jim_Obj *falseObj; |
| 615 | unsigned long referenceNextId; |
| @@ -624,11 +662,11 @@ | |
| 624 | Jim_Obj *errorProc; |
| 625 | Jim_Obj *unknown; |
| 626 | Jim_Obj *defer; |
| 627 | Jim_Obj *traceCmdObj; |
| 628 | int unknown_called; |
| 629 | int errorFlag; |
| 630 | void *cmdPrivData; /* Used to pass the private data pointer to |
| 631 | a command. It is set to what the user specified |
| 632 | via Jim_CreateCommand(). */ |
| 633 | |
| 634 | Jim_Cmd *oldCmdCache; |
| @@ -636,26 +674,35 @@ | |
| 636 | struct Jim_CallFrame *freeFramesList; |
| 637 | struct Jim_HashTable assocData; |
| 638 | Jim_PrngState *prngState; |
| 639 | struct Jim_HashTable packages; |
| 640 | Jim_Stack *loadHandles; |
| 641 | } Jim_Interp; |
| 642 | |
| 643 | #define Jim_SetResultString(i,s,l) Jim_SetResult(i, Jim_NewStringObj(i,s,l)) |
| 644 | #define Jim_SetResultInt(i,intval) Jim_SetResult(i, Jim_NewIntObj(i,intval)) |
| 645 | |
| 646 | #define Jim_SetResultBool(i,b) Jim_SetResultInt(i, b) |
| 647 | #define Jim_SetEmptyResult(i) Jim_SetResult(i, (i)->emptyObj) |
| 648 | #define Jim_GetResult(i) ((i)->result) |
| 649 | #define Jim_CmdPrivData(i) ((i)->cmdPrivData) |
| 650 | |
| 651 | #define Jim_SetResult(i,o) do { \ |
| 652 | Jim_Obj *_resultObjPtr_ = (o); \ |
| 653 | Jim_IncrRefCount(_resultObjPtr_); \ |
| 654 | Jim_DecrRefCount(i,(i)->result); \ |
| 655 | (i)->result = _resultObjPtr_; \ |
| 656 | } while(0) |
| 657 | |
| 658 | |
| 659 | #define Jim_GetId(i) (++(i)->id) |
| 660 | |
| 661 | |
| @@ -665,29 +712,39 @@ | |
| 665 | Jim_Obj *objPtr; |
| 666 | Jim_Obj *finalizerCmdNamePtr; |
| 667 | char tag[JIM_REFERENCE_TAGLEN+1]; |
| 668 | } Jim_Reference; |
| 669 | |
| 670 | |
| 671 | #define Jim_NewEmptyStringObj(i) Jim_NewStringObj(i, "", 0) |
| 672 | #define Jim_FreeHashTableIterator(iter) Jim_Free(iter) |
| 673 | |
| 674 | #define JIM_EXPORT extern |
| 675 | |
| 676 | |
| 677 | |
| 678 | JIM_EXPORT void *(*Jim_Allocator)(void *ptr, size_t size); |
| 679 | |
| 680 | #define Jim_Free(P) Jim_Allocator((P), 0) |
| 681 | #define Jim_Realloc(P, S) Jim_Allocator((P), (S)) |
| 682 | #define Jim_Alloc(S) Jim_Allocator(NULL, (S)) |
| 683 | JIM_EXPORT char * Jim_StrDup (const char *s); |
| 684 | JIM_EXPORT char *Jim_StrDupLen(const char *s, int l); |
| 685 | |
| 686 | |
| 687 | JIM_EXPORT char **Jim_GetEnviron(void); |
| 688 | JIM_EXPORT void Jim_SetEnviron(char **env); |
| 689 | JIM_EXPORT int Jim_MakeTempFile(Jim_Interp *interp, const char *filename_template, int unlink_file); |
| 690 | #ifndef CLOCK_REALTIME |
| 691 | # define CLOCK_REALTIME 0 |
| 692 | #endif |
| 693 | #ifndef CLOCK_MONOTONIC |
| @@ -694,314 +751,485 @@ | |
| 694 | # define CLOCK_MONOTONIC 1 |
| 695 | #endif |
| 696 | #ifndef CLOCK_MONOTONIC_RAW |
| 697 | # define CLOCK_MONOTONIC_RAW CLOCK_MONOTONIC |
| 698 | #endif |
| 699 | JIM_EXPORT jim_wide Jim_GetTimeUsec(unsigned type); |
| 700 | |
| 701 | |
| 702 | JIM_EXPORT int Jim_Eval(Jim_Interp *interp, const char *script); |
| 703 | |
| 704 | |
| 705 | JIM_EXPORT int Jim_EvalSource(Jim_Interp *interp, const char *filename, int lineno, const char *script); |
| 706 | |
| 707 | #define Jim_Eval_Named(I, S, F, L) Jim_EvalSource((I), (F), (L), (S)) |
| 708 | |
| 709 | JIM_EXPORT int Jim_EvalGlobal(Jim_Interp *interp, const char *script); |
| 710 | JIM_EXPORT int Jim_EvalFile(Jim_Interp *interp, const char *filename); |
| 711 | JIM_EXPORT int Jim_EvalFileGlobal(Jim_Interp *interp, const char *filename); |
| 712 | JIM_EXPORT int Jim_EvalObj (Jim_Interp *interp, Jim_Obj *scriptObjPtr); |
| 713 | JIM_EXPORT int Jim_EvalObjVector (Jim_Interp *interp, int objc, |
| 714 | Jim_Obj *const *objv); |
| 715 | JIM_EXPORT int Jim_EvalObjList(Jim_Interp *interp, Jim_Obj *listObj); |
| 716 | JIM_EXPORT int Jim_EvalObjPrefix(Jim_Interp *interp, Jim_Obj *prefix, |
| 717 | int objc, Jim_Obj *const *objv); |
| 718 | #define Jim_EvalPrefix(i, p, oc, ov) Jim_EvalObjPrefix((i), Jim_NewStringObj((i), (p), -1), (oc), (ov)) |
| 719 | JIM_EXPORT int Jim_EvalNamespace(Jim_Interp *interp, Jim_Obj *scriptObj, Jim_Obj *nsObj); |
| 720 | JIM_EXPORT int Jim_SubstObj (Jim_Interp *interp, Jim_Obj *substObjPtr, |
| 721 | Jim_Obj **resObjPtrPtr, int flags); |
| 722 | |
| 723 | |
| 724 | JIM_EXPORT Jim_Obj *Jim_GetSourceInfo(Jim_Interp *interp, Jim_Obj *objPtr, |
| 725 | int *lineptr); |
| 726 | |
| 727 | JIM_EXPORT void Jim_SetSourceInfo(Jim_Interp *interp, Jim_Obj *objPtr, |
| 728 | Jim_Obj *fileNameObj, int lineNumber); |
| 729 | |
| 730 | |
| 731 | |
| 732 | JIM_EXPORT void Jim_InitStack(Jim_Stack *stack); |
| 733 | JIM_EXPORT void Jim_FreeStack(Jim_Stack *stack); |
| 734 | JIM_EXPORT int Jim_StackLen(Jim_Stack *stack); |
| 735 | JIM_EXPORT void Jim_StackPush(Jim_Stack *stack, void *element); |
| 736 | JIM_EXPORT void * Jim_StackPop(Jim_Stack *stack); |
| 737 | JIM_EXPORT void * Jim_StackPeek(Jim_Stack *stack); |
| 738 | JIM_EXPORT void Jim_FreeStackElements(Jim_Stack *stack, void (*freeFunc)(void *ptr)); |
| 739 | |
| 740 | |
| 741 | JIM_EXPORT int Jim_InitHashTable (Jim_HashTable *ht, |
| 742 | const Jim_HashTableType *type, void *privdata); |
| 743 | JIM_EXPORT void Jim_ExpandHashTable (Jim_HashTable *ht, |
| 744 | unsigned int size); |
| 745 | JIM_EXPORT int Jim_AddHashEntry (Jim_HashTable *ht, const void *key, |
| 746 | void *val); |
| 747 | JIM_EXPORT int Jim_ReplaceHashEntry (Jim_HashTable *ht, |
| 748 | const void *key, void *val); |
| 749 | JIM_EXPORT int Jim_DeleteHashEntry (Jim_HashTable *ht, |
| 750 | const void *key); |
| 751 | JIM_EXPORT int Jim_FreeHashTable (Jim_HashTable *ht); |
| 752 | JIM_EXPORT Jim_HashEntry * Jim_FindHashEntry (Jim_HashTable *ht, |
| 753 | const void *key); |
| 754 | JIM_EXPORT Jim_HashTableIterator *Jim_GetHashTableIterator |
| 755 | (Jim_HashTable *ht); |
| 756 | JIM_EXPORT Jim_HashEntry * Jim_NextHashEntry |
| 757 | (Jim_HashTableIterator *iter); |
| 758 | |
| 759 | |
| 760 | JIM_EXPORT Jim_Obj * Jim_NewObj (Jim_Interp *interp); |
| 761 | JIM_EXPORT void Jim_FreeObj (Jim_Interp *interp, Jim_Obj *objPtr); |
| 762 | JIM_EXPORT void Jim_InvalidateStringRep (Jim_Obj *objPtr); |
| 763 | JIM_EXPORT Jim_Obj * Jim_DuplicateObj (Jim_Interp *interp, |
| 764 | Jim_Obj *objPtr); |
| 765 | JIM_EXPORT const char * Jim_GetString(Jim_Obj *objPtr, |
| 766 | int *lenPtr); |
| 767 | JIM_EXPORT const char *Jim_String(Jim_Obj *objPtr); |
| 768 | JIM_EXPORT int Jim_Length(Jim_Obj *objPtr); |
| 769 | |
| 770 | |
| 771 | JIM_EXPORT Jim_Obj * Jim_NewStringObj (Jim_Interp *interp, |
| 772 | const char *s, int len); |
| 773 | JIM_EXPORT Jim_Obj *Jim_NewStringObjUtf8(Jim_Interp *interp, |
| 774 | const char *s, int charlen); |
| 775 | JIM_EXPORT Jim_Obj * Jim_NewStringObjNoAlloc (Jim_Interp *interp, |
| 776 | char *s, int len); |
| 777 | JIM_EXPORT void Jim_AppendString (Jim_Interp *interp, Jim_Obj *objPtr, |
| 778 | const char *str, int len); |
| 779 | JIM_EXPORT void Jim_AppendObj (Jim_Interp *interp, Jim_Obj *objPtr, |
| 780 | Jim_Obj *appendObjPtr); |
| 781 | JIM_EXPORT void Jim_AppendStrings (Jim_Interp *interp, |
| 782 | Jim_Obj *objPtr, ...); |
| 783 | JIM_EXPORT int Jim_StringEqObj(Jim_Obj *aObjPtr, Jim_Obj *bObjPtr); |
| 784 | JIM_EXPORT int Jim_StringMatchObj (Jim_Interp *interp, Jim_Obj *patternObjPtr, |
| 785 | Jim_Obj *objPtr, int nocase); |
| 786 | JIM_EXPORT Jim_Obj * Jim_StringRangeObj (Jim_Interp *interp, |
| 787 | Jim_Obj *strObjPtr, Jim_Obj *firstObjPtr, |
| 788 | Jim_Obj *lastObjPtr); |
| 789 | JIM_EXPORT Jim_Obj * Jim_FormatString (Jim_Interp *interp, |
| 790 | Jim_Obj *fmtObjPtr, int objc, Jim_Obj *const *objv); |
| 791 | JIM_EXPORT Jim_Obj * Jim_ScanString (Jim_Interp *interp, Jim_Obj *strObjPtr, |
| 792 | Jim_Obj *fmtObjPtr, int flags); |
| 793 | JIM_EXPORT int Jim_CompareStringImmediate (Jim_Interp *interp, |
| 794 | Jim_Obj *objPtr, const char *str); |
| 795 | JIM_EXPORT int Jim_StringCompareObj(Jim_Interp *interp, Jim_Obj *firstObjPtr, |
| 796 | Jim_Obj *secondObjPtr, int nocase); |
| 797 | JIM_EXPORT int Jim_Utf8Length(Jim_Interp *interp, Jim_Obj *objPtr); |
| 798 | |
| 799 | |
| 800 | JIM_EXPORT Jim_Obj * Jim_NewReference (Jim_Interp *interp, |
| 801 | Jim_Obj *objPtr, Jim_Obj *tagPtr, Jim_Obj *cmdNamePtr); |
| 802 | JIM_EXPORT Jim_Reference * Jim_GetReference (Jim_Interp *interp, |
| 803 | Jim_Obj *objPtr); |
| 804 | JIM_EXPORT int Jim_SetFinalizer (Jim_Interp *interp, Jim_Obj *objPtr, Jim_Obj *cmdNamePtr); |
| 805 | JIM_EXPORT int Jim_GetFinalizer (Jim_Interp *interp, Jim_Obj *objPtr, Jim_Obj **cmdNamePtrPtr); |
| 806 | |
| 807 | |
| 808 | JIM_EXPORT Jim_Interp * Jim_CreateInterp (void); |
| 809 | JIM_EXPORT void Jim_FreeInterp (Jim_Interp *i); |
| 810 | JIM_EXPORT int Jim_GetExitCode (Jim_Interp *interp); |
| 811 | JIM_EXPORT const char *Jim_ReturnCode(int code); |
| 812 | JIM_EXPORT void Jim_SetResultFormatted(Jim_Interp *interp, const char *format, ...); |
| 813 | |
| 814 | |
| 815 | JIM_EXPORT void Jim_RegisterCoreCommands (Jim_Interp *interp); |
| 816 | JIM_EXPORT int Jim_CreateCommand (Jim_Interp *interp, |
| 817 | const char *cmdName, Jim_CmdProc *cmdProc, void *privData, |
| 818 | Jim_DelCmdProc *delProc); |
| 819 | JIM_EXPORT int Jim_DeleteCommand (Jim_Interp *interp, |
| 820 | Jim_Obj *cmdNameObj); |
| 821 | JIM_EXPORT int Jim_RenameCommand (Jim_Interp *interp, |
| 822 | Jim_Obj *oldNameObj, Jim_Obj *newNameObj); |
| 823 | JIM_EXPORT Jim_Cmd * Jim_GetCommand (Jim_Interp *interp, |
| 824 | Jim_Obj *objPtr, int flags); |
| 825 | JIM_EXPORT int Jim_SetVariable (Jim_Interp *interp, |
| 826 | Jim_Obj *nameObjPtr, Jim_Obj *valObjPtr); |
| 827 | JIM_EXPORT int Jim_SetVariableStr (Jim_Interp *interp, |
| 828 | const char *name, Jim_Obj *objPtr); |
| 829 | JIM_EXPORT int Jim_SetGlobalVariableStr (Jim_Interp *interp, |
| 830 | const char *name, Jim_Obj *objPtr); |
| 831 | JIM_EXPORT int Jim_SetVariableStrWithStr (Jim_Interp *interp, |
| 832 | const char *name, const char *val); |
| 833 | JIM_EXPORT int Jim_SetVariableLink (Jim_Interp *interp, |
| 834 | Jim_Obj *nameObjPtr, Jim_Obj *targetNameObjPtr, |
| 835 | Jim_CallFrame *targetCallFrame); |
| 836 | JIM_EXPORT Jim_Obj * Jim_MakeGlobalNamespaceName(Jim_Interp *interp, |
| 837 | Jim_Obj *nameObjPtr); |
| 838 | JIM_EXPORT Jim_Obj * Jim_GetVariable (Jim_Interp *interp, |
| 839 | Jim_Obj *nameObjPtr, int flags); |
| 840 | JIM_EXPORT Jim_Obj * Jim_GetGlobalVariable (Jim_Interp *interp, |
| 841 | Jim_Obj *nameObjPtr, int flags); |
| 842 | JIM_EXPORT Jim_Obj * Jim_GetVariableStr (Jim_Interp *interp, |
| 843 | const char *name, int flags); |
| 844 | JIM_EXPORT Jim_Obj * Jim_GetGlobalVariableStr (Jim_Interp *interp, |
| 845 | const char *name, int flags); |
| 846 | JIM_EXPORT int Jim_UnsetVariable (Jim_Interp *interp, |
| 847 | Jim_Obj *nameObjPtr, int flags); |
| 848 | |
| 849 | |
| 850 | JIM_EXPORT Jim_CallFrame *Jim_GetCallFrameByLevel(Jim_Interp *interp, |
| 851 | Jim_Obj *levelObjPtr); |
| 852 | |
| 853 | |
| 854 | JIM_EXPORT int Jim_Collect (Jim_Interp *interp); |
| 855 | JIM_EXPORT void Jim_CollectIfNeeded (Jim_Interp *interp); |
| 856 | |
| 857 | |
| 858 | JIM_EXPORT int Jim_GetIndex (Jim_Interp *interp, Jim_Obj *objPtr, |
| 859 | int *indexPtr); |
| 860 | |
| 861 | |
| 862 | JIM_EXPORT Jim_Obj * Jim_NewListObj (Jim_Interp *interp, |
| 863 | Jim_Obj *const *elements, int len); |
| 864 | JIM_EXPORT void Jim_ListInsertElements (Jim_Interp *interp, |
| 865 | Jim_Obj *listPtr, int listindex, int objc, Jim_Obj *const *objVec); |
| 866 | JIM_EXPORT void Jim_ListAppendElement (Jim_Interp *interp, |
| 867 | Jim_Obj *listPtr, Jim_Obj *objPtr); |
| 868 | JIM_EXPORT void Jim_ListAppendList (Jim_Interp *interp, |
| 869 | Jim_Obj *listPtr, Jim_Obj *appendListPtr); |
| 870 | JIM_EXPORT int Jim_ListLength (Jim_Interp *interp, Jim_Obj *objPtr); |
| 871 | JIM_EXPORT int Jim_ListIndex (Jim_Interp *interp, Jim_Obj *listPrt, |
| 872 | int listindex, Jim_Obj **objPtrPtr, int seterr); |
| 873 | JIM_EXPORT Jim_Obj *Jim_ListGetIndex(Jim_Interp *interp, Jim_Obj *listPtr, int idx); |
| 874 | JIM_EXPORT int Jim_SetListIndex (Jim_Interp *interp, |
| 875 | Jim_Obj *varNamePtr, Jim_Obj *const *indexv, int indexc, |
| 876 | Jim_Obj *newObjPtr); |
| 877 | JIM_EXPORT Jim_Obj * Jim_ConcatObj (Jim_Interp *interp, int objc, |
| 878 | Jim_Obj *const *objv); |
| 879 | JIM_EXPORT Jim_Obj *Jim_ListJoin(Jim_Interp *interp, |
| 880 | Jim_Obj *listObjPtr, const char *joinStr, int joinStrLen); |
| 881 | |
| 882 | |
| 883 | JIM_EXPORT Jim_Obj * Jim_NewDictObj (Jim_Interp *interp, |
| 884 | Jim_Obj *const *elements, int len); |
| 885 | JIM_EXPORT int Jim_DictKey (Jim_Interp *interp, Jim_Obj *dictPtr, |
| 886 | Jim_Obj *keyPtr, Jim_Obj **objPtrPtr, int flags); |
| 887 | JIM_EXPORT int Jim_DictKeysVector (Jim_Interp *interp, |
| 888 | Jim_Obj *dictPtr, Jim_Obj *const *keyv, int keyc, |
| 889 | Jim_Obj **objPtrPtr, int flags); |
| 890 | JIM_EXPORT int Jim_SetDictKeysVector (Jim_Interp *interp, |
| 891 | Jim_Obj *varNamePtr, Jim_Obj *const *keyv, int keyc, |
| 892 | Jim_Obj *newObjPtr, int flags); |
| 893 | JIM_EXPORT Jim_Obj **Jim_DictPairs(Jim_Interp *interp, |
| 894 | Jim_Obj *dictPtr, int *len); |
| 895 | JIM_EXPORT int Jim_DictAddElement(Jim_Interp *interp, Jim_Obj *objPtr, |
| 896 | Jim_Obj *keyObjPtr, Jim_Obj *valueObjPtr); |
| 897 | |
| 898 | #define JIM_DICTMATCH_KEYS 0x0001 |
| 899 | #define JIM_DICTMATCH_VALUES 0x002 |
| 900 | |
| 901 | JIM_EXPORT int Jim_DictMatchTypes(Jim_Interp *interp, Jim_Obj *objPtr, Jim_Obj *patternObj, int match_type, int return_types); |
| 902 | JIM_EXPORT int Jim_DictSize(Jim_Interp *interp, Jim_Obj *objPtr); |
| 903 | JIM_EXPORT int Jim_DictInfo(Jim_Interp *interp, Jim_Obj *objPtr); |
| 904 | JIM_EXPORT Jim_Obj *Jim_DictMerge(Jim_Interp *interp, int objc, Jim_Obj *const *objv); |
| 905 | |
| 906 | |
| 907 | JIM_EXPORT int Jim_GetReturnCode (Jim_Interp *interp, Jim_Obj *objPtr, |
| 908 | int *intPtr); |
| 909 | |
| 910 | |
| 911 | JIM_EXPORT int Jim_EvalExpression (Jim_Interp *interp, |
| 912 | Jim_Obj *exprObjPtr); |
| 913 | JIM_EXPORT int Jim_GetBoolFromExpr (Jim_Interp *interp, |
| 914 | Jim_Obj *exprObjPtr, int *boolPtr); |
| 915 | |
| 916 | |
| 917 | JIM_EXPORT int Jim_GetBoolean(Jim_Interp *interp, Jim_Obj *objPtr, |
| 918 | int *booleanPtr); |
| 919 | |
| 920 | |
| 921 | JIM_EXPORT int Jim_GetWide (Jim_Interp *interp, Jim_Obj *objPtr, |
| 922 | jim_wide *widePtr); |
| 923 | JIM_EXPORT int Jim_GetWideExpr(Jim_Interp *interp, Jim_Obj *objPtr, |
| 924 | jim_wide *widePtr); |
| 925 | JIM_EXPORT int Jim_GetLong (Jim_Interp *interp, Jim_Obj *objPtr, |
| 926 | long *longPtr); |
| 927 | #define Jim_NewWideObj Jim_NewIntObj |
| 928 | JIM_EXPORT Jim_Obj * Jim_NewIntObj (Jim_Interp *interp, |
| 929 | jim_wide wideValue); |
| 930 | |
| 931 | |
| 932 | JIM_EXPORT int Jim_GetDouble(Jim_Interp *interp, Jim_Obj *objPtr, |
| 933 | double *doublePtr); |
| 934 | JIM_EXPORT void Jim_SetDouble(Jim_Interp *interp, Jim_Obj *objPtr, |
| 935 | double doubleValue); |
| 936 | JIM_EXPORT Jim_Obj * Jim_NewDoubleObj(Jim_Interp *interp, double doubleValue); |
| 937 | |
| 938 | |
| 939 | JIM_EXPORT void Jim_WrongNumArgs (Jim_Interp *interp, int argc, |
| 940 | Jim_Obj *const *argv, const char *msg); |
| 941 | JIM_EXPORT int Jim_GetEnum (Jim_Interp *interp, Jim_Obj *objPtr, |
| 942 | const char * const *tablePtr, int *indexPtr, const char *name, int flags); |
| 943 | JIM_EXPORT int Jim_CheckShowCommands(Jim_Interp *interp, Jim_Obj *objPtr, |
| 944 | const char *const *tablePtr); |
| 945 | JIM_EXPORT int Jim_ScriptIsComplete(Jim_Interp *interp, |
| 946 | Jim_Obj *scriptObj, char *stateCharPtr); |
| 947 | |
| 948 | JIM_EXPORT int Jim_FindByName(const char *name, const char * const array[], size_t len); |
| 949 | |
| 950 | |
| 951 | typedef void (Jim_InterpDeleteProc)(Jim_Interp *interp, void *data); |
| 952 | JIM_EXPORT void * Jim_GetAssocData(Jim_Interp *interp, const char *key); |
| 953 | JIM_EXPORT int Jim_SetAssocData(Jim_Interp *interp, const char *key, |
| 954 | Jim_InterpDeleteProc *delProc, void *data); |
| 955 | JIM_EXPORT int Jim_DeleteAssocData(Jim_Interp *interp, const char *key); |
| 956 | JIM_EXPORT int Jim_CheckAbiVersion(Jim_Interp *interp, int abi_version); |
| 957 | |
| 958 | |
| 959 | |
| 960 | |
| 961 | JIM_EXPORT int Jim_PackageProvide (Jim_Interp *interp, |
| 962 | const char *name, const char *ver, int flags); |
| 963 | JIM_EXPORT int Jim_PackageRequire (Jim_Interp *interp, |
| 964 | const char *name, int flags); |
| 965 | #define Jim_PackageProvideCheck(INTERP, NAME) \ |
| 966 | if (Jim_CheckAbiVersion(INTERP, JIM_ABI_VERSION) == JIM_ERR || Jim_PackageProvide(INTERP, NAME, "1.0", JIM_ERRMSG)) \ |
| 967 | return JIM_ERR |
| 968 | |
| 969 | |
| 970 | JIM_EXPORT void Jim_MakeErrorMessage (Jim_Interp *interp); |
| 971 | |
| 972 | |
| 973 | JIM_EXPORT int Jim_InteractivePrompt (Jim_Interp *interp); |
| 974 | JIM_EXPORT void Jim_HistoryLoad(const char *filename); |
| 975 | JIM_EXPORT void Jim_HistorySave(const char *filename); |
| 976 | JIM_EXPORT char *Jim_HistoryGetline(Jim_Interp *interp, const char *prompt); |
| 977 | JIM_EXPORT void Jim_HistorySetCompletion(Jim_Interp *interp, Jim_Obj *completionCommandObj); |
| 978 | JIM_EXPORT void Jim_HistorySetHints(Jim_Interp *interp, Jim_Obj *hintsCommandObj); |
| 979 | JIM_EXPORT void Jim_HistoryAdd(const char *line); |
| 980 | JIM_EXPORT void Jim_HistoryShow(void); |
| 981 | JIM_EXPORT void Jim_HistorySetMaxLen(int length); |
| 982 | JIM_EXPORT int Jim_HistoryGetMaxLen(void); |
| 983 | |
| 984 | |
| 985 | JIM_EXPORT int Jim_InitStaticExtensions(Jim_Interp *interp); |
| 986 | JIM_EXPORT int Jim_StringToWide(const char *str, jim_wide *widePtr, int base); |
| 987 | JIM_EXPORT int Jim_IsBigEndian(void); |
| 988 | |
| 989 | #define Jim_CheckSignal(i) ((i)->signal_level && (i)->sigmask) |
| 990 | JIM_EXPORT void Jim_SignalSetIgnored(jim_wide mask); |
| 991 | |
| 992 | |
| 993 | JIM_EXPORT int Jim_LoadLibrary(Jim_Interp *interp, const char *pathName); |
| 994 | JIM_EXPORT void Jim_FreeLoadHandles(Jim_Interp *interp); |
| 995 | |
| 996 | |
| 997 | JIM_EXPORT int Jim_AioFilehandle(Jim_Interp *interp, Jim_Obj *command); |
| 998 | |
| 999 | |
| 1000 | JIM_EXPORT int Jim_IsDict(Jim_Obj *objPtr); |
| 1001 | JIM_EXPORT int Jim_IsList(Jim_Obj *objPtr); |
| 1002 | |
| 1003 | #ifdef __cplusplus |
| 1004 | } |
| 1005 | #endif |
| 1006 | |
| 1007 | #endif |
| @@ -1015,10 +1243,14 @@ | |
| 1015 | #endif |
| 1016 | |
| 1017 | |
| 1018 | #define JIM_MODFLAG_HIDDEN 0x0001 |
| 1019 | #define JIM_MODFLAG_FULLARGV 0x0002 |
| 1020 | |
| 1021 | |
| 1022 | |
| 1023 | typedef int jim_subcmd_function(Jim_Interp *interp, int argc, Jim_Obj *const *argv); |
| 1024 | |
| @@ -1040,10 +1272,13 @@ | |
| 1040 | int Jim_SubCmdProc(Jim_Interp *interp, int argc, Jim_Obj *const *argv); |
| 1041 | |
| 1042 | int Jim_CallSubCmd(Jim_Interp *interp, const jim_subcmd_type *ct, int argc, Jim_Obj *const *argv); |
| 1043 | |
| 1044 | void Jim_SubCmdArgError(Jim_Interp *interp, const jim_subcmd_type *ct, Jim_Obj *subcmd); |
| 1045 | |
| 1046 | #ifdef __cplusplus |
| 1047 | } |
| 1048 | #endif |
| 1049 | |
| @@ -1074,10 +1309,11 @@ | |
| 1074 | int regstart; |
| 1075 | int reganch; |
| 1076 | int regmust; |
| 1077 | int regmlen; |
| 1078 | int *program; |
| 1079 | |
| 1080 | |
| 1081 | const char *regparse; |
| 1082 | int p; |
| 1083 | int proglen; |
| @@ -1094,14 +1330,18 @@ | |
| 1094 | } regexp; |
| 1095 | |
| 1096 | typedef regexp regex_t; |
| 1097 | |
| 1098 | #define REG_EXTENDED 0 |
| 1099 | #define REG_NEWLINE 1 |
| 1100 | #define REG_ICASE 2 |
| 1101 | |
| 1102 | #define REG_NOTBOL 16 |
| 1103 | |
| 1104 | enum { |
| 1105 | REG_NOERROR, |
| 1106 | REG_NOMATCH, |
| 1107 | REG_BADPAT, |
| @@ -1162,11 +1402,11 @@ | |
| 1162 | |
| 1163 | int Jim_OpenForWrite(const char *filename, int append); |
| 1164 | |
| 1165 | int Jim_OpenForRead(const char *filename); |
| 1166 | |
| 1167 | #if defined(__MINGW32__) |
| 1168 | #ifndef STRICT |
| 1169 | #define STRICT |
| 1170 | #endif |
| 1171 | #define WIN32_LEAN_AND_MEAN |
| 1172 | #include <windows.h> |
| @@ -1197,10 +1437,16 @@ | |
| 1197 | |
| 1198 | typedef struct __stat64 jim_stat_t; |
| 1199 | #define Jim_Stat _stat64 |
| 1200 | #define Jim_FileStat _fstat64 |
| 1201 | #define Jim_Lseek _lseeki64 |
| 1202 | |
| 1203 | #else |
| 1204 | #if defined(HAVE_STAT64) |
| 1205 | typedef struct stat64 jim_stat_t; |
| 1206 | #define Jim_Stat stat64 |
| @@ -1239,15 +1485,24 @@ | |
| 1239 | |
| 1240 | #ifndef HAVE_EXECVPE |
| 1241 | #define execvpe(ARG0, ARGV, ENV) execvp(ARG0, ARGV) |
| 1242 | #endif |
| 1243 | #endif |
| 1244 | #endif |
| 1245 | |
| 1246 | #ifndef O_TEXT |
| 1247 | #define O_TEXT 0 |
| 1248 | #endif |
| 1249 | |
| 1250 | |
| 1251 | int Jim_FileStoreStatData(Jim_Interp *interp, Jim_Obj *varName, const jim_stat_t *sb); |
| 1252 | |
| 1253 | #endif |
| @@ -1291,12 +1546,18 @@ | |
| 1291 | "\n" |
| 1292 | " if {[exists jim::argv0]} {\n" |
| 1293 | " if {[string match \"*/*\" $jim::argv0]} {\n" |
| 1294 | " set jim::exe [file join [pwd] $jim::argv0]\n" |
| 1295 | " } else {\n" |
| 1296 | " foreach path [split [env PATH \"\"] $tcl_platform(pathSeparator)] {\n" |
| 1297 | " set exec [file join [pwd] [string map {\\\\ /} $path] $jim::argv0]\n" |
| 1298 | " if {[file executable $exec]} {\n" |
| 1299 | " set jim::exe $exec\n" |
| 1300 | " break\n" |
| 1301 | " }\n" |
| 1302 | " }\n" |
| @@ -1387,12 +1648,12 @@ | |
| 1387 | "set tcl::stdhint_col $tcl::stdhint_cols(lcyan)\n" |
| 1388 | "\n" |
| 1389 | "\n" |
| 1390 | "proc tcl::stdhint {string} {\n" |
| 1391 | " set result \"\"\n" |
| 1392 | " if {[llength $string] >= 2} {\n" |
| 1393 | " lassign $string cmd arg\n" |
| 1394 | " if {$cmd in $::tcl::stdhint_commands || [info channel $cmd] ne \"\"} {\n" |
| 1395 | " catch {\n" |
| 1396 | " set help [$cmd -help $arg]\n" |
| 1397 | " if {[string match \"Usage: $cmd *\" $help]} {\n" |
| 1398 | " set n [llength $string]\n" |
| @@ -1410,10 +1671,27 @@ | |
| 1410 | " }\n" |
| 1411 | " set result [list $prefix$hint {*}$::tcl::stdhint_col]\n" |
| 1412 | " }\n" |
| 1413 | " }\n" |
| 1414 | " }\n" |
| 1415 | " }\n" |
| 1416 | " return $result\n" |
| 1417 | "}\n" |
| 1418 | "\n" |
| 1419 | "_jimsh_init\n" |
| @@ -1856,11 +2134,11 @@ | |
| 1856 | " }\n" |
| 1857 | " -bu* {\n" |
| 1858 | " $f buffering $v\n" |
| 1859 | " }\n" |
| 1860 | " -tr* {\n" |
| 1861 | "\n" |
| 1862 | " }\n" |
| 1863 | " default {\n" |
| 1864 | " return -code error \"fconfigure: unknown option $n\"\n" |
| 1865 | " }\n" |
| 1866 | " }\n" |
| @@ -1873,11 +2151,13 @@ | |
| 1873 | " tailcall {*}$args\n" |
| 1874 | "}\n" |
| 1875 | "\n" |
| 1876 | "\n" |
| 1877 | "\n" |
| 1878 | "proc parray {arrayname {pattern *} {puts puts}} {\n" |
| 1879 | " upvar $arrayname a\n" |
| 1880 | "\n" |
| 1881 | " set max 0\n" |
| 1882 | " foreach name [array names a $pattern]] {\n" |
| 1883 | " if {[string length $name] > $max} {\n" |
| @@ -1885,11 +2165,11 @@ | |
| 1885 | " }\n" |
| 1886 | " }\n" |
| 1887 | " incr max [string length $arrayname]\n" |
| 1888 | " incr max 2\n" |
| 1889 | " foreach name [lsort [array names a $pattern]] {\n" |
| 1890 | " $puts [format \"%-${max}s = %s\" $arrayname\\($name\\) $a($name)]\n" |
| 1891 | " }\n" |
| 1892 | "}\n" |
| 1893 | "\n" |
| 1894 | "\n" |
| 1895 | "proc {file copy} {{force {}} source target} {\n" |
| @@ -1945,13 +2225,10 @@ | |
| 1945 | " }\n" |
| 1946 | " lambda {cmd args} {f pids} {\n" |
| 1947 | " if {$cmd eq \"pid\"} {\n" |
| 1948 | " return $pids\n" |
| 1949 | " }\n" |
| 1950 | " if {$cmd eq \"getfd\"} {\n" |
| 1951 | " $f getfd\n" |
| 1952 | " }\n" |
| 1953 | " if {$cmd eq \"close\"} {\n" |
| 1954 | " $f close\n" |
| 1955 | "\n" |
| 1956 | " set retopts {}\n" |
| 1957 | " foreach p $pids {\n" |
| @@ -2047,18 +2324,21 @@ | |
| 2047 | #ifdef HAVE_TERMIOS_H |
| 2048 | #endif |
| 2049 | |
| 2050 | |
| 2051 | #define AIO_CMD_LEN 32 |
| 2052 | #define AIO_BUF_LEN 256 |
| 2053 | #define AIO_WBUF_FULL_SIZE (64 * 1024) |
| 2054 | |
| 2055 | #define AIO_KEEPOPEN 1 |
| 2056 | #define AIO_NODELETE 2 |
| 2057 | #define AIO_EOF 4 |
| 2058 | #define AIO_WBUF_NONE 8 |
| 2059 | #define AIO_NONBLOCK 16 |
| 2060 | |
| 2061 | enum wbuftype { |
| 2062 | WBUF_OPT_NONE, |
| 2063 | WBUF_OPT_LINE, |
| 2064 | WBUF_OPT_FULL, |
| @@ -2076,14 +2356,10 @@ | |
| 2076 | #define UNIX_SOCKETS 1 |
| 2077 | #else |
| 2078 | #define UNIX_SOCKETS 0 |
| 2079 | #endif |
| 2080 | |
| 2081 | #ifndef MAXPATHLEN |
| 2082 | #define MAXPATHLEN JIM_PATH_LEN |
| 2083 | #endif |
| 2084 | |
| 2085 | |
| 2086 | |
| 2087 | |
| 2088 | static int JimReadableTimeout(int fd, long ms) |
| 2089 | { |
| @@ -2115,30 +2391,40 @@ | |
| 2115 | typedef struct { |
| 2116 | int (*writer)(struct AioFile *af, const char *buf, int len); |
| 2117 | int (*reader)(struct AioFile *af, char *buf, int len, int pending); |
| 2118 | int (*error)(const struct AioFile *af); |
| 2119 | const char *(*strerror)(struct AioFile *af); |
| 2120 | int (*verify)(struct AioFile *af); |
| 2121 | } JimAioFopsType; |
| 2122 | |
| 2123 | typedef struct AioFile |
| 2124 | { |
| 2125 | Jim_Obj *filename; |
| 2126 | int wbuft; |
| 2127 | int flags; |
| 2128 | long timeout; |
| 2129 | int fd; |
| 2130 | int addr_family; |
| 2131 | void *ssl; |
| 2132 | const JimAioFopsType *fops; |
| 2133 | Jim_Obj *readbuf; |
| 2134 | Jim_Obj *writebuf; |
| 2135 | } AioFile; |
| 2136 | |
| 2137 | static int stdio_writer(struct AioFile *af, const char *buf, int len) |
| 2138 | { |
| 2139 | return write(af->fd, buf, len); |
| 2140 | } |
| 2141 | |
| 2142 | static int stdio_reader(struct AioFile *af, char *buf, int len, int nb) |
| 2143 | { |
| 2144 | if (nb || af->timeout == 0 || JimReadableTimeout(af->fd, af->timeout) == JIM_OK) { |
| @@ -2186,11 +2472,10 @@ | |
| 2186 | static const JimAioFopsType stdio_fops = { |
| 2187 | stdio_writer, |
| 2188 | stdio_reader, |
| 2189 | stdio_error, |
| 2190 | stdio_strerror, |
| 2191 | NULL, |
| 2192 | }; |
| 2193 | |
| 2194 | |
| 2195 | static void aio_set_nonblocking(AioFile *af, int nb) |
| 2196 | { |
| @@ -2219,10 +2504,11 @@ | |
| 2219 | } |
| 2220 | return old; |
| 2221 | } |
| 2222 | |
| 2223 | static int JimAioSubCmdProc(Jim_Interp *interp, int argc, Jim_Obj *const *argv); |
| 2224 | static AioFile *JimMakeChannel(Jim_Interp *interp, int fd, Jim_Obj *filename, |
| 2225 | const char *hdlfmt, int family, int flags); |
| 2226 | |
| 2227 | |
| 2228 | static const char *JimAioErrorString(AioFile *af) |
| @@ -2271,11 +2557,26 @@ | |
| 2271 | memmove(objPtr->bytes, objPtr->bytes + n, objPtr->length - n + 1); |
| 2272 | objPtr->length -= n; |
| 2273 | } |
| 2274 | |
| 2275 | |
| 2276 | static int aio_autoflush(Jim_Interp *interp, void *clientData, int mask); |
| 2277 | |
| 2278 | static int aio_flush(Jim_Interp *interp, AioFile *af) |
| 2279 | { |
| 2280 | int len; |
| 2281 | const char *pt = Jim_GetString(af->writebuf, &len); |
| @@ -2306,23 +2607,11 @@ | |
| 2306 | } |
| 2307 | } |
| 2308 | return JIM_OK; |
| 2309 | } |
| 2310 | |
| 2311 | static int aio_autoflush(Jim_Interp *interp, void *clientData, int mask) |
| 2312 | { |
| 2313 | AioFile *af = clientData; |
| 2314 | |
| 2315 | aio_flush(interp, af); |
| 2316 | if (Jim_Length(af->writebuf) == 0) { |
| 2317 | |
| 2318 | return -1; |
| 2319 | } |
| 2320 | return 0; |
| 2321 | } |
| 2322 | |
| 2323 | static int aio_read_len(Jim_Interp *interp, AioFile *af, int nb, char *buf, size_t buflen, int neededLen) |
| 2324 | { |
| 2325 | if (!af->readbuf) { |
| 2326 | af->readbuf = Jim_NewStringObj(interp, NULL, 0); |
| 2327 | } |
| 2328 | |
| @@ -2336,24 +2625,33 @@ | |
| 2336 | while (neededLen && !aio_eof(af)) { |
| 2337 | int retval; |
| 2338 | int readlen; |
| 2339 | |
| 2340 | if (neededLen == -1) { |
| 2341 | readlen = AIO_BUF_LEN; |
| 2342 | } |
| 2343 | else { |
| 2344 | readlen = (neededLen > AIO_BUF_LEN ? AIO_BUF_LEN : neededLen); |
| 2345 | } |
| 2346 | retval = af->fops->reader(af, buf, readlen, nb); |
| 2347 | if (retval > 0) { |
| 2348 | Jim_AppendString(interp, af->readbuf, buf, retval); |
| 2349 | if (neededLen != -1) { |
| 2350 | neededLen -= retval; |
| 2351 | } |
| 2352 | continue; |
| 2353 | } |
| 2354 | if (JimCheckStreamError(interp, af)) { |
| 2355 | return JIM_ERR; |
| 2356 | } |
| 2357 | break; |
| 2358 | } |
| 2359 | |
| @@ -2420,10 +2718,11 @@ | |
| 2420 | } |
| 2421 | if (af->readbuf) { |
| 2422 | Jim_FreeNewObj(interp, af->readbuf); |
| 2423 | } |
| 2424 | |
| 2425 | Jim_Free(af); |
| 2426 | } |
| 2427 | |
| 2428 | static int aio_cmd_read(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 2429 | { |
| @@ -2433,11 +2732,10 @@ | |
| 2433 | static const char * const options[] = { "-pending", "-nonewline", NULL }; |
| 2434 | enum { OPT_PENDING, OPT_NONEWLINE }; |
| 2435 | int option; |
| 2436 | int nb; |
| 2437 | Jim_Obj *objPtr; |
| 2438 | char buf[AIO_BUF_LEN]; |
| 2439 | |
| 2440 | if (argc) { |
| 2441 | if (*Jim_String(argv[0]) == '-') { |
| 2442 | if (Jim_GetEnum(interp, argv[0], options, &option, NULL, JIM_ERRMSG) != JIM_OK) { |
| 2443 | return JIM_ERR; |
| @@ -2461,21 +2759,22 @@ | |
| 2461 | } |
| 2462 | argc--; |
| 2463 | argv++; |
| 2464 | } |
| 2465 | if (argc) { |
| 2466 | return -1; |
| 2467 | } |
| 2468 | |
| 2469 | |
| 2470 | nb = aio_start_nonblocking(af); |
| 2471 | |
| 2472 | if (aio_read_len(interp, af, nb, buf, sizeof(buf), neededLen) != JIM_OK) { |
| 2473 | aio_set_nonblocking(af, nb); |
| 2474 | return JIM_ERR; |
| 2475 | } |
| 2476 | objPtr = aio_read_consume(interp, af, neededLen); |
| 2477 | |
| 2478 | aio_set_nonblocking(af, nb); |
| 2479 | |
| 2480 | if (objPtr) { |
| 2481 | if (nonewline) { |
| @@ -2498,11 +2797,11 @@ | |
| 2498 | int Jim_AioFilehandle(Jim_Interp *interp, Jim_Obj *command) |
| 2499 | { |
| 2500 | Jim_Cmd *cmdPtr = Jim_GetCommand(interp, command, JIM_ERRMSG); |
| 2501 | |
| 2502 | |
| 2503 | if (cmdPtr && !cmdPtr->isproc && cmdPtr->u.native.cmdProc == JimAioSubCmdProc) { |
| 2504 | return ((AioFile *) cmdPtr->u.native.privData)->fd; |
| 2505 | } |
| 2506 | Jim_SetResultFormatted(interp, "Not a filehandle: \"%#s\"", command); |
| 2507 | return -1; |
| 2508 | } |
| @@ -2516,29 +2815,62 @@ | |
| 2516 | |
| 2517 | Jim_SetResultInt(interp, af->fd); |
| 2518 | |
| 2519 | return JIM_OK; |
| 2520 | } |
| 2521 | |
| 2522 | static int aio_cmd_copy(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 2523 | { |
| 2524 | AioFile *af = Jim_CmdPrivData(interp); |
| 2525 | jim_wide count = 0; |
| 2526 | jim_wide maxlen = JIM_WIDE_MAX; |
| 2527 | |
| 2528 | char buf[AIO_BUF_LEN]; |
| 2529 | |
| 2530 | char *bufp = buf; |
| 2531 | int buflen = sizeof(buf); |
| 2532 | int ok = 1; |
| 2533 | Jim_Obj *objv[4]; |
| 2534 | |
| 2535 | if (argc == 2) { |
| 2536 | if (Jim_GetWide(interp, argv[1], &maxlen) != JIM_OK) { |
| 2537 | return JIM_ERR; |
| 2538 | } |
| 2539 | } |
| 2540 | |
| 2541 | objv[0] = argv[0]; |
| 2542 | objv[1] = Jim_NewStringObj(interp, "flush", -1); |
| 2543 | if (Jim_EvalObjVector(interp, 2, objv) != JIM_OK) { |
| 2544 | Jim_SetResultFormatted(interp, "Not a filehandle: \"%#s\"", argv[0]); |
| @@ -2552,14 +2884,14 @@ | |
| 2552 | Jim_IncrRefCount(objv[1]); |
| 2553 | Jim_IncrRefCount(objv[2]); |
| 2554 | |
| 2555 | while (count < maxlen) { |
| 2556 | jim_wide len = maxlen - count; |
| 2557 | if (len > buflen) { |
| 2558 | len = buflen; |
| 2559 | } |
| 2560 | if (aio_read_len(interp, af, 0, bufp, buflen, len) != JIM_OK) { |
| 2561 | ok = 0; |
| 2562 | break; |
| 2563 | } |
| 2564 | objv[3] = aio_read_consume(interp, af, len); |
| 2565 | count += Jim_Length(objv[3]); |
| @@ -2568,19 +2900,15 @@ | |
| 2568 | break; |
| 2569 | } |
| 2570 | if (aio_eof(af)) { |
| 2571 | break; |
| 2572 | } |
| 2573 | if (count >= 16384 && bufp == buf) { |
| 2574 | |
| 2575 | buflen = 65536; |
| 2576 | bufp = Jim_Alloc(buflen); |
| 2577 | } |
| 2578 | } |
| 2579 | |
| 2580 | if (bufp != buf) { |
| 2581 | Jim_Free(bufp); |
| 2582 | } |
| 2583 | |
| 2584 | Jim_DecrRefCount(interp, objv[1]); |
| 2585 | Jim_DecrRefCount(interp, objv[2]); |
| 2586 | |
| @@ -2591,62 +2919,107 @@ | |
| 2591 | Jim_SetResultInt(interp, count); |
| 2592 | |
| 2593 | return JIM_OK; |
| 2594 | } |
| 2595 | |
| 2596 | static int aio_cmd_gets(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 2597 | { |
| 2598 | AioFile *af = Jim_CmdPrivData(interp); |
| 2599 | char buf[AIO_BUF_LEN]; |
| 2600 | Jim_Obj *objPtr = NULL; |
| 2601 | int len; |
| 2602 | int nb; |
| 2603 | char *nl = NULL; |
| 2604 | int offset = 0; |
| 2605 | |
| 2606 | errno = 0; |
| 2607 | |
| 2608 | |
| 2609 | nb = aio_start_nonblocking(af); |
| 2610 | |
| 2611 | if (!af->readbuf) { |
| 2612 | af->readbuf = Jim_NewStringObj(interp, NULL, 0); |
| 2613 | } |
| 2614 | |
| 2615 | while (!aio_eof(af)) { |
| 2616 | const char *pt = Jim_GetString(af->readbuf, &len); |
| 2617 | nl = memchr(pt + offset, '\n', len - offset); |
| 2618 | if (nl) { |
| 2619 | |
| 2620 | objPtr = Jim_NewStringObj(interp, pt, nl - pt); |
| 2621 | |
| 2622 | aio_consume(af->readbuf, nl - pt + 1); |
| 2623 | break; |
| 2624 | } |
| 2625 | |
| 2626 | offset = len; |
| 2627 | len = af->fops->reader(af, buf, AIO_BUF_LEN, nb); |
| 2628 | if (len <= 0) { |
| 2629 | break; |
| 2630 | } |
| 2631 | Jim_AppendString(interp, af->readbuf, buf, len); |
| 2632 | } |
| 2633 | |
| 2634 | aio_set_nonblocking(af, nb); |
| 2635 | |
| 2636 | if (!nl && aio_eof(af)) { |
| 2637 | |
| 2638 | objPtr = af->readbuf; |
| 2639 | af->readbuf = NULL; |
| 2640 | } |
| 2641 | else if (!objPtr) { |
| 2642 | objPtr = Jim_NewStringObj(interp, NULL, 0); |
| 2643 | } |
| 2644 | |
| 2645 | if (argc) { |
| 2646 | if (Jim_SetVariable(interp, argv[0], objPtr) != JIM_OK) { |
| 2647 | Jim_FreeNewObj(interp, objPtr); |
| 2648 | return JIM_ERR; |
| 2649 | } |
| 2650 | |
| 2651 | len = Jim_Length(objPtr); |
| 2652 | |
| @@ -2668,22 +3041,34 @@ | |
| 2668 | int wlen; |
| 2669 | const char *wdata; |
| 2670 | Jim_Obj *strObj; |
| 2671 | int wnow = 0; |
| 2672 | int nl = 1; |
| 2673 | |
| 2674 | if (argc == 2) { |
| 2675 | if (!Jim_CompareStringImmediate(interp, argv[0], "-nonewline")) { |
| 2676 | return -1; |
| 2677 | } |
| 2678 | strObj = argv[1]; |
| 2679 | nl = 0; |
| 2680 | } |
| 2681 | else { |
| 2682 | strObj = argv[0]; |
| 2683 | } |
| 2684 | |
| 2685 | Jim_AppendObj(interp, af->writebuf, strObj); |
| 2686 | if (nl) { |
| 2687 | Jim_AppendString(interp, af->writebuf, "\n", 1); |
| 2688 | } |
| 2689 | |
| @@ -2701,11 +3086,11 @@ | |
| 2701 | wnow = 1; |
| 2702 | } |
| 2703 | break; |
| 2704 | |
| 2705 | case WBUF_OPT_FULL: |
| 2706 | if (wlen >= AIO_WBUF_FULL_SIZE) { |
| 2707 | wnow = 1; |
| 2708 | } |
| 2709 | break; |
| 2710 | } |
| 2711 | |
| @@ -2797,11 +3182,11 @@ | |
| 2797 | else if (Jim_CompareStringImmediate(interp, argv[1], "current")) |
| 2798 | orig = SEEK_CUR; |
| 2799 | else if (Jim_CompareStringImmediate(interp, argv[1], "end")) |
| 2800 | orig = SEEK_END; |
| 2801 | else { |
| 2802 | return -1; |
| 2803 | } |
| 2804 | } |
| 2805 | if (Jim_GetWide(interp, argv[0], &offset) != JIM_OK) { |
| 2806 | return JIM_ERR; |
| 2807 | } |
| @@ -2834,10 +3219,49 @@ | |
| 2834 | AioFile *af = Jim_CmdPrivData(interp); |
| 2835 | |
| 2836 | Jim_SetResult(interp, af->filename); |
| 2837 | return JIM_OK; |
| 2838 | } |
| 2839 | |
| 2840 | #ifdef O_NDELAY |
| 2841 | static int aio_cmd_ndelay(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 2842 | { |
| 2843 | AioFile *af = Jim_CmdPrivData(interp); |
| @@ -2870,29 +3294,92 @@ | |
| 2870 | #endif |
| 2871 | |
| 2872 | static int aio_cmd_buffering(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 2873 | { |
| 2874 | AioFile *af = Jim_CmdPrivData(interp); |
| 2875 | |
| 2876 | static const char * const options[] = { |
| 2877 | "none", |
| 2878 | "line", |
| 2879 | "full", |
| 2880 | NULL |
| 2881 | }; |
| 2882 | |
| 2883 | if (Jim_GetEnum(interp, argv[0], options, &af->wbuft, NULL, JIM_ERRMSG) != JIM_OK) { |
| 2884 | return JIM_ERR; |
| 2885 | } |
| 2886 | |
| 2887 | if (af->wbuft == WBUF_OPT_NONE) { |
| 2888 | return aio_flush(interp, af); |
| 2889 | } |
| 2890 | |
| 2891 | return JIM_OK; |
| 2892 | } |
| 2893 | |
| 2894 | static int aio_cmd_timeout(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 2895 | { |
| 2896 | #ifdef HAVE_SELECT |
| 2897 | AioFile *af = Jim_CmdPrivData(interp); |
| 2898 | if (argc == 1) { |
| @@ -2906,11 +3393,10 @@ | |
| 2906 | Jim_SetResultString(interp, "timeout not supported", -1); |
| 2907 | return JIM_ERR; |
| 2908 | #endif |
| 2909 | } |
| 2910 | |
| 2911 | #ifdef jim_ext_eventloop |
| 2912 | static int aio_eventinfo(Jim_Interp *interp, AioFile * af, unsigned mask, |
| 2913 | int argc, Jim_Obj * const *argv) |
| 2914 | { |
| 2915 | if (argc == 0) { |
| 2916 | |
| @@ -2989,17 +3475,26 @@ | |
| 2989 | { "getfd", |
| 2990 | NULL, |
| 2991 | aio_cmd_getfd, |
| 2992 | 0, |
| 2993 | 0, |
| 2994 | |
| 2995 | }, |
| 2996 | { "gets", |
| 2997 | "?var?", |
| 2998 | aio_cmd_gets, |
| 2999 | 0, |
| 3000 | 1, |
| 3001 | |
| 3002 | }, |
| 3003 | { "puts", |
| 3004 | "?-nonewline? str", |
| 3005 | aio_cmd_puts, |
| @@ -3055,10 +3550,19 @@ | |
| 3055 | aio_cmd_filename, |
| 3056 | 0, |
| 3057 | 0, |
| 3058 | |
| 3059 | }, |
| 3060 | #ifdef O_NDELAY |
| 3061 | { "ndelay", |
| 3062 | "?0|1?", |
| 3063 | aio_cmd_ndelay, |
| 3064 | 0, |
| @@ -3074,13 +3578,27 @@ | |
| 3074 | 0, |
| 3075 | |
| 3076 | }, |
| 3077 | #endif |
| 3078 | { "buffering", |
| 3079 | "none|line|full", |
| 3080 | aio_cmd_buffering, |
| 3081 | 1, |
| 3082 | 1, |
| 3083 | |
| 3084 | }, |
| 3085 | #if defined(jim_ext_file) && defined(Jim_FileStat) |
| 3086 | { "stat", |
| @@ -3222,23 +3740,29 @@ | |
| 3222 | |
| 3223 | static int JimAioOpenCommand(Jim_Interp *interp, int argc, |
| 3224 | Jim_Obj *const *argv) |
| 3225 | { |
| 3226 | int openflags; |
| 3227 | const char *filename; |
| 3228 | int fd = -1; |
| 3229 | int n = 0; |
| 3230 | int flags = 0; |
| 3231 | |
| 3232 | if (argc > 2 && Jim_CompareStringImmediate(interp, argv[2], "-noclose")) { |
| 3233 | flags = AIO_KEEPOPEN; |
| 3234 | n++; |
| 3235 | } |
| 3236 | if (argc < 2 || argc > 3 + n) { |
| 3237 | Jim_WrongNumArgs(interp, 1, argv, "filename ?-noclose? ?mode?"); |
| 3238 | return JIM_ERR; |
| 3239 | } |
| 3240 | |
| 3241 | filename = Jim_String(argv[1]); |
| 3242 | |
| 3243 | #ifdef jim_ext_tclcompat |
| 3244 | { |
| @@ -3265,19 +3789,32 @@ | |
| 3265 | } |
| 3266 | } |
| 3267 | else { |
| 3268 | openflags = O_RDONLY; |
| 3269 | } |
| 3270 | fd = open(filename, openflags, 0666); |
| 3271 | if (fd < 0) { |
| 3272 | JimAioSetError(interp, argv[1]); |
| 3273 | return JIM_ERR; |
| 3274 | } |
| 3275 | |
| 3276 | return JimMakeChannel(interp, fd, argv[1], "aio.handle%ld", 0, flags) ? JIM_OK : JIM_ERR; |
| 3277 | } |
| 3278 | |
| 3279 | |
| 3280 | static AioFile *JimMakeChannel(Jim_Interp *interp, int fd, Jim_Obj *filename, |
| 3281 | const char *hdlfmt, int family, int flags) |
| 3282 | { |
| 3283 | AioFile *af; |
| @@ -3319,12 +3856,23 @@ | |
| 3319 | |
| 3320 | af->flags |= flags; |
| 3321 | |
| 3322 | af->writebuf = Jim_NewStringObj(interp, NULL, 0); |
| 3323 | Jim_IncrRefCount(af->writebuf); |
| 3324 | |
| 3325 | Jim_CreateCommand(interp, buf, JimAioSubCmdProc, af, JimAioDelProc); |
| 3326 | |
| 3327 | Jim_SetResult(interp, Jim_MakeGlobalNamespaceName(interp, cmdname)); |
| 3328 | |
| 3329 | return af; |
| 3330 | } |
| @@ -3365,14 +3913,10 @@ | |
| 3365 | } |
| 3366 | |
| 3367 | |
| 3368 | static int JimAioPipeCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 3369 | { |
| 3370 | if (argc != 1) { |
| 3371 | Jim_WrongNumArgs(interp, 1, argv, ""); |
| 3372 | return JIM_ERR; |
| 3373 | } |
| 3374 | return JimCreatePipe(interp, argv[0], 0); |
| 3375 | } |
| 3376 | #endif |
| 3377 | |
| 3378 | #ifdef HAVE_OPENPTY |
| @@ -3379,15 +3923,10 @@ | |
| 3379 | static int JimAioOpenPtyCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 3380 | { |
| 3381 | int p[2]; |
| 3382 | char path[MAXPATHLEN]; |
| 3383 | |
| 3384 | if (argc != 1) { |
| 3385 | Jim_WrongNumArgs(interp, 1, argv, ""); |
| 3386 | return JIM_ERR; |
| 3387 | } |
| 3388 | |
| 3389 | if (openpty(&p[0], &p[1], path, NULL, NULL) != 0) { |
| 3390 | JimAioSetError(interp, NULL); |
| 3391 | return JIM_ERR; |
| 3392 | } |
| 3393 | |
| @@ -3397,31 +3936,37 @@ | |
| 3397 | } |
| 3398 | #endif |
| 3399 | |
| 3400 | |
| 3401 | |
| 3402 | int Jim_aioInit(Jim_Interp *interp) |
| 3403 | { |
| 3404 | if (Jim_PackageProvide(interp, "aio", "1.0", JIM_ERRMSG)) |
| 3405 | return JIM_ERR; |
| 3406 | |
| 3407 | #if defined(JIM_SSL) |
| 3408 | Jim_CreateCommand(interp, "load_ssl_certs", JimAioLoadSSLCertsCommand, NULL, NULL); |
| 3409 | #endif |
| 3410 | |
| 3411 | Jim_CreateCommand(interp, "open", JimAioOpenCommand, NULL, NULL); |
| 3412 | #ifdef HAVE_SOCKETS |
| 3413 | Jim_CreateCommand(interp, "socket", JimAioSockCommand, NULL, NULL); |
| 3414 | #endif |
| 3415 | #ifdef HAVE_PIPE |
| 3416 | Jim_CreateCommand(interp, "pipe", JimAioPipeCommand, NULL, NULL); |
| 3417 | #endif |
| 3418 | |
| 3419 | |
| 3420 | JimMakeChannel(interp, fileno(stdin), NULL, "stdin", 0, AIO_KEEPOPEN); |
| 3421 | JimMakeChannel(interp, fileno(stdout), NULL, "stdout", 0, AIO_KEEPOPEN); |
| 3422 | JimMakeChannel(interp, fileno(stderr), NULL, "stderr", 0, AIO_KEEPOPEN | AIO_WBUF_NONE); |
| 3423 | |
| 3424 | return JIM_OK; |
| 3425 | } |
| 3426 | |
| 3427 | #include <errno.h> |
| @@ -3442,12 +3987,11 @@ | |
| 3442 | |
| 3443 | if (argc == 3 && Jim_CompareStringImmediate(interp, argv[1], "-nocomplain")) { |
| 3444 | nocomplain = 1; |
| 3445 | } |
| 3446 | if (argc != 2 && !nocomplain) { |
| 3447 | Jim_WrongNumArgs(interp, 1, argv, "?-nocomplain? dirPath"); |
| 3448 | return JIM_ERR; |
| 3449 | } |
| 3450 | |
| 3451 | dirPath = Jim_String(argv[1 + nocomplain]); |
| 3452 | |
| 3453 | dirPtr = opendir(dirPath); |
| @@ -3480,11 +4024,11 @@ | |
| 3480 | } |
| 3481 | |
| 3482 | int Jim_readdirInit(Jim_Interp *interp) |
| 3483 | { |
| 3484 | Jim_PackageProvideCheck(interp, "readdir"); |
| 3485 | Jim_CreateCommand(interp, "readdir", Jim_ReaddirCmd, NULL, NULL); |
| 3486 | return JIM_OK; |
| 3487 | } |
| 3488 | |
| 3489 | #include <stdlib.h> |
| 3490 | #include <string.h> |
| @@ -3553,10 +4097,11 @@ | |
| 3553 | int Jim_RegexpCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 3554 | { |
| 3555 | int opt_indices = 0; |
| 3556 | int opt_all = 0; |
| 3557 | int opt_inline = 0; |
| 3558 | regex_t *regex; |
| 3559 | int match, i, j; |
| 3560 | int offset = 0; |
| 3561 | regmatch_t *pmatch = NULL; |
| 3562 | int source_len; |
| @@ -3568,30 +4113,23 @@ | |
| 3568 | Jim_Obj *resultListObj = NULL; |
| 3569 | int regcomp_flags = 0; |
| 3570 | int eflags = 0; |
| 3571 | int option; |
| 3572 | enum { |
| 3573 | OPT_INDICES, OPT_NOCASE, OPT_LINE, OPT_ALL, OPT_INLINE, OPT_START, OPT_END |
| 3574 | }; |
| 3575 | static const char * const options[] = { |
| 3576 | "-indices", "-nocase", "-line", "-all", "-inline", "-start", "--", NULL |
| 3577 | }; |
| 3578 | |
| 3579 | if (argc < 3) { |
| 3580 | wrongNumArgs: |
| 3581 | Jim_WrongNumArgs(interp, 1, argv, |
| 3582 | "?-switch ...? exp string ?matchVar? ?subMatchVar ...?"); |
| 3583 | return JIM_ERR; |
| 3584 | } |
| 3585 | |
| 3586 | for (i = 1; i < argc; i++) { |
| 3587 | const char *opt = Jim_String(argv[i]); |
| 3588 | |
| 3589 | if (*opt != '-') { |
| 3590 | break; |
| 3591 | } |
| 3592 | if (Jim_GetEnum(interp, argv[i], options, &option, "switch", JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK) { |
| 3593 | return JIM_ERR; |
| 3594 | } |
| 3595 | if (option == OPT_END) { |
| 3596 | i++; |
| 3597 | break; |
| @@ -3605,12 +4143,24 @@ | |
| 3605 | regcomp_flags |= REG_ICASE; |
| 3606 | break; |
| 3607 | |
| 3608 | case OPT_LINE: |
| 3609 | regcomp_flags |= REG_NEWLINE; |
| 3610 | break; |
| 3611 | |
| 3612 | case OPT_ALL: |
| 3613 | opt_all = 1; |
| 3614 | break; |
| 3615 | |
| 3616 | case OPT_INLINE: |
| @@ -3617,20 +4167,30 @@ | |
| 3617 | opt_inline = 1; |
| 3618 | break; |
| 3619 | |
| 3620 | case OPT_START: |
| 3621 | if (++i == argc) { |
| 3622 | goto wrongNumArgs; |
| 3623 | } |
| 3624 | if (Jim_GetIndex(interp, argv[i], &offset) != JIM_OK) { |
| 3625 | return JIM_ERR; |
| 3626 | } |
| 3627 | break; |
| 3628 | } |
| 3629 | } |
| 3630 | if (argc - i < 2) { |
| 3631 | goto wrongNumArgs; |
| 3632 | } |
| 3633 | |
| 3634 | regex = SetRegexpFromAny(interp, argv[i], regcomp_flags); |
| 3635 | if (!regex) { |
| 3636 | return JIM_ERR; |
| @@ -3685,18 +4245,14 @@ | |
| 3685 | goto done; |
| 3686 | } |
| 3687 | |
| 3688 | num_matches++; |
| 3689 | |
| 3690 | if (opt_all && !opt_inline) { |
| 3691 | |
| 3692 | goto try_next_match; |
| 3693 | } |
| 3694 | |
| 3695 | |
| 3696 | j = 0; |
| 3697 | for (i += 2; opt_inline ? j < num_vars : i < argc; i++, j++) { |
| 3698 | Jim_Obj *resultObj; |
| 3699 | |
| 3700 | if (opt_indices) { |
| 3701 | resultObj = Jim_NewListObj(interp, NULL, 0); |
| 3702 | } |
| @@ -3726,21 +4282,19 @@ | |
| 3726 | if (opt_inline) { |
| 3727 | Jim_ListAppendElement(interp, resultListObj, resultObj); |
| 3728 | } |
| 3729 | else { |
| 3730 | |
| 3731 | result = Jim_SetVariable(interp, argv[i], resultObj); |
| 3732 | |
| 3733 | if (result != JIM_OK) { |
| 3734 | Jim_FreeObj(interp, resultObj); |
| 3735 | break; |
| 3736 | } |
| 3737 | } |
| 3738 | } |
| 3739 | |
| 3740 | try_next_match: |
| 3741 | if (opt_all && (pattern[0] != '^' || (regcomp_flags & REG_NEWLINE)) && *source_str) { |
| 3742 | if (pmatch[0].rm_eo) { |
| 3743 | offset += utf8_strlen(source_str, pmatch[0].rm_eo); |
| 3744 | source_str += pmatch[0].rm_eo; |
| 3745 | } |
| 3746 | else { |
| @@ -3792,30 +4346,23 @@ | |
| 3792 | const char *replace_str = NULL; |
| 3793 | int replace_len; |
| 3794 | const char *pattern; |
| 3795 | int option; |
| 3796 | enum { |
| 3797 | OPT_NOCASE, OPT_LINE, OPT_ALL, OPT_START, OPT_COMMAND, OPT_END |
| 3798 | }; |
| 3799 | static const char * const options[] = { |
| 3800 | "-nocase", "-line", "-all", "-start", "-command", "--", NULL |
| 3801 | }; |
| 3802 | |
| 3803 | if (argc < 4) { |
| 3804 | wrongNumArgs: |
| 3805 | Jim_WrongNumArgs(interp, 1, argv, |
| 3806 | "?-switch ...? exp string subSpec ?varName?"); |
| 3807 | return JIM_ERR; |
| 3808 | } |
| 3809 | |
| 3810 | for (i = 1; i < argc; i++) { |
| 3811 | const char *opt = Jim_String(argv[i]); |
| 3812 | |
| 3813 | if (*opt != '-') { |
| 3814 | break; |
| 3815 | } |
| 3816 | if (Jim_GetEnum(interp, argv[i], options, &option, "switch", JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK) { |
| 3817 | return JIM_ERR; |
| 3818 | } |
| 3819 | if (option == OPT_END) { |
| 3820 | i++; |
| 3821 | break; |
| @@ -3827,48 +4374,69 @@ | |
| 3827 | |
| 3828 | case OPT_LINE: |
| 3829 | regcomp_flags |= REG_NEWLINE; |
| 3830 | break; |
| 3831 | |
| 3832 | case OPT_ALL: |
| 3833 | opt_all = 1; |
| 3834 | break; |
| 3835 | |
| 3836 | case OPT_START: |
| 3837 | if (++i == argc) { |
| 3838 | goto wrongNumArgs; |
| 3839 | } |
| 3840 | if (Jim_GetIndex(interp, argv[i], &offset) != JIM_OK) { |
| 3841 | return JIM_ERR; |
| 3842 | } |
| 3843 | break; |
| 3844 | |
| 3845 | case OPT_COMMAND: |
| 3846 | opt_command = 1; |
| 3847 | break; |
| 3848 | } |
| 3849 | } |
| 3850 | if (argc - i != 3 && argc - i != 4) { |
| 3851 | goto wrongNumArgs; |
| 3852 | } |
| 3853 | |
| 3854 | |
| 3855 | regcomp_obj = Jim_DuplicateObj(interp, argv[i]); |
| 3856 | Jim_IncrRefCount(regcomp_obj); |
| 3857 | regex = SetRegexpFromAny(interp, regcomp_obj, regcomp_flags); |
| 3858 | if (!regex) { |
| 3859 | Jim_DecrRefCount(interp, regcomp_obj); |
| 3860 | return JIM_ERR; |
| 3861 | } |
| 3862 | pattern = Jim_String(argv[i]); |
| 3863 | |
| 3864 | source_str = Jim_GetString(argv[i + 1], &source_len); |
| 3865 | if (opt_command) { |
| 3866 | cmd_prefix = argv[i + 2]; |
| 3867 | if (Jim_ListLength(interp, cmd_prefix) == 0) { |
| 3868 | Jim_SetResultString(interp, "command prefix must be a list of at least one element", -1); |
| 3869 | Jim_DecrRefCount(interp, regcomp_obj); |
| 3870 | return JIM_ERR; |
| 3871 | } |
| 3872 | Jim_IncrRefCount(cmd_prefix); |
| 3873 | } |
| 3874 | else { |
| @@ -3897,11 +4465,12 @@ | |
| 3897 | Jim_AppendString(interp, resultObj, source_str, offset); |
| 3898 | |
| 3899 | |
| 3900 | n = source_len - offset; |
| 3901 | p = source_str + offset; |
| 3902 | do { |
| 3903 | int match = jim_regexec(regex, p, MAX_SUB_MATCHES, pmatch, regexec_flags); |
| 3904 | |
| 3905 | if (match >= REG_BADPAT) { |
| 3906 | char buf[100]; |
| 3907 | |
| @@ -3978,32 +4547,26 @@ | |
| 3978 | |
| 3979 | if (!opt_all || n == 0) { |
| 3980 | break; |
| 3981 | } |
| 3982 | |
| 3983 | |
| 3984 | if ((regcomp_flags & REG_NEWLINE) == 0 && pattern[0] == '^') { |
| 3985 | break; |
| 3986 | } |
| 3987 | |
| 3988 | |
| 3989 | if (pattern[0] == '\0' && n) { |
| 3990 | |
| 3991 | Jim_AppendString(interp, resultObj, p, 1); |
| 3992 | p++; |
| 3993 | n--; |
| 3994 | } |
| 3995 | |
| 3996 | if (pmatch[0].rm_eo == pmatch[0].rm_so) { |
| 3997 | |
| 3998 | regexec_flags = REG_NOTBOL; |
| 3999 | } |
| 4000 | else { |
| 4001 | regexec_flags = 0; |
| 4002 | } |
| 4003 | |
| 4004 | } while (n); |
| 4005 | |
| 4006 | Jim_AppendString(interp, resultObj, p, -1); |
| 4007 | |
| 4008 | cmd_error: |
| 4009 | if (result == JIM_OK) { |
| @@ -4012,13 +4575,10 @@ | |
| 4012 | result = Jim_SetVariable(interp, varname, resultObj); |
| 4013 | |
| 4014 | if (result == JIM_OK) { |
| 4015 | Jim_SetResultInt(interp, num_matches); |
| 4016 | } |
| 4017 | else { |
| 4018 | Jim_FreeObj(interp, resultObj); |
| 4019 | } |
| 4020 | } |
| 4021 | else { |
| 4022 | Jim_SetResult(interp, resultObj); |
| 4023 | result = JIM_OK; |
| 4024 | } |
| @@ -4029,20 +4589,20 @@ | |
| 4029 | |
| 4030 | if (opt_command) { |
| 4031 | Jim_DecrRefCount(interp, cmd_prefix); |
| 4032 | } |
| 4033 | |
| 4034 | Jim_DecrRefCount(interp, regcomp_obj); |
| 4035 | |
| 4036 | return result; |
| 4037 | } |
| 4038 | |
| 4039 | int Jim_regexpInit(Jim_Interp *interp) |
| 4040 | { |
| 4041 | Jim_PackageProvideCheck(interp, "regexp"); |
| 4042 | Jim_CreateCommand(interp, "regexp", Jim_RegexpCmd, NULL, NULL); |
| 4043 | Jim_CreateCommand(interp, "regsub", Jim_RegsubCmd, NULL, NULL); |
| 4044 | return JIM_OK; |
| 4045 | } |
| 4046 | |
| 4047 | #include <limits.h> |
| 4048 | #include <stdlib.h> |
| @@ -4063,18 +4623,10 @@ | |
| 4063 | #define R_OK 4 |
| 4064 | #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) |
| 4065 | #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) |
| 4066 | #endif |
| 4067 | |
| 4068 | # ifndef MAXPATHLEN |
| 4069 | # ifdef PATH_MAX |
| 4070 | # define MAXPATHLEN PATH_MAX |
| 4071 | # else |
| 4072 | # define MAXPATHLEN JIM_PATH_LEN |
| 4073 | # endif |
| 4074 | # endif |
| 4075 | |
| 4076 | #if defined(__MINGW32__) || defined(__MSYS__) || defined(_MSC_VER) |
| 4077 | #define ISWINDOWS 1 |
| 4078 | |
| 4079 | #undef HAVE_SYMLINK |
| 4080 | #else |
| @@ -4514,11 +5066,11 @@ | |
| 4514 | if (errno == ENOENT) { |
| 4515 | |
| 4516 | continue; |
| 4517 | } |
| 4518 | |
| 4519 | if (errno == EEXIST) { |
| 4520 | jim_stat_t sb; |
| 4521 | |
| 4522 | if (Jim_Stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) { |
| 4523 | return 0; |
| 4524 | } |
| @@ -4949,31 +5501,35 @@ | |
| 4949 | { "delete", |
| 4950 | "?-force|--? name ...", |
| 4951 | file_cmd_delete, |
| 4952 | 1, |
| 4953 | -1, |
| 4954 | |
| 4955 | }, |
| 4956 | { "mkdir", |
| 4957 | "dir ...", |
| 4958 | file_cmd_mkdir, |
| 4959 | 1, |
| 4960 | -1, |
| 4961 | |
| 4962 | }, |
| 4963 | { "tempfile", |
| 4964 | "?template?", |
| 4965 | file_cmd_tempfile, |
| 4966 | 0, |
| 4967 | 1, |
| 4968 | |
| 4969 | }, |
| 4970 | { "rename", |
| 4971 | "?-force? source dest", |
| 4972 | file_cmd_rename, |
| 4973 | 2, |
| 4974 | 3, |
| 4975 | |
| 4976 | }, |
| 4977 | #if defined(HAVE_LINK) && defined(HAVE_SYMLINK) |
| 4978 | { "link", |
| 4979 | "?-symbolic|-hard? newname target", |
| @@ -5048,18 +5604,11 @@ | |
| 5048 | } |
| 5049 | }; |
| 5050 | |
| 5051 | static int Jim_CdCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 5052 | { |
| 5053 | const char *path; |
| 5054 | |
| 5055 | if (argc != 2) { |
| 5056 | Jim_WrongNumArgs(interp, 1, argv, "dirname"); |
| 5057 | return JIM_ERR; |
| 5058 | } |
| 5059 | |
| 5060 | path = Jim_String(argv[1]); |
| 5061 | |
| 5062 | if (chdir(path) != 0) { |
| 5063 | Jim_SetResultFormatted(interp, "couldn't change working directory to \"%s\": %s", path, |
| 5064 | strerror(errno)); |
| 5065 | return JIM_ERR; |
| @@ -5084,13 +5633,13 @@ | |
| 5084 | } |
| 5085 | |
| 5086 | int Jim_fileInit(Jim_Interp *interp) |
| 5087 | { |
| 5088 | Jim_PackageProvideCheck(interp, "file"); |
| 5089 | Jim_CreateCommand(interp, "file", Jim_SubCmdProc, (void *)file_command_table, NULL); |
| 5090 | Jim_CreateCommand(interp, "pwd", Jim_PwdCmd, NULL, NULL); |
| 5091 | Jim_CreateCommand(interp, "cd", Jim_CdCmd, NULL, NULL); |
| 5092 | return JIM_OK; |
| 5093 | } |
| 5094 | |
| 5095 | #include <string.h> |
| 5096 | #include <ctype.h> |
| @@ -5143,11 +5692,11 @@ | |
| 5143 | } |
| 5144 | |
| 5145 | int Jim_execInit(Jim_Interp *interp) |
| 5146 | { |
| 5147 | Jim_PackageProvideCheck(interp, "exec"); |
| 5148 | Jim_CreateCommand(interp, "exec", Jim_ExecCmd, NULL, NULL); |
| 5149 | return JIM_OK; |
| 5150 | } |
| 5151 | #else |
| 5152 | |
| 5153 | |
| @@ -5158,12 +5707,12 @@ | |
| 5158 | struct WaitInfoTable; |
| 5159 | |
| 5160 | static char **JimOriginalEnviron(void); |
| 5161 | static char **JimSaveEnv(char **env); |
| 5162 | static void JimRestoreEnv(char **env); |
| 5163 | static int JimCreatePipeline(Jim_Interp *interp, int argc, Jim_Obj *const *argv, |
| 5164 | phandle_t **pidArrayPtr, int *inPipePtr, int *outPipePtr, int *errFilePtr); |
| 5165 | static void JimDetachPids(struct WaitInfoTable *table, int numPids, const phandle_t *pidPtr); |
| 5166 | static int JimCleanupChildren(Jim_Interp *interp, int numPids, phandle_t *pidPtr, Jim_Obj *errStrObj); |
| 5167 | static int Jim_WaitCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv); |
| 5168 | |
| 5169 | #if defined(__MINGW32__) |
| @@ -5341,11 +5890,11 @@ | |
| 5341 | static struct WaitInfoTable *JimAllocWaitInfoTable(void) |
| 5342 | { |
| 5343 | struct WaitInfoTable *table = Jim_Alloc(sizeof(*table)); |
| 5344 | table->info = NULL; |
| 5345 | table->size = table->used = 0; |
| 5346 | table->refcount = 1; |
| 5347 | |
| 5348 | return table; |
| 5349 | } |
| 5350 | |
| 5351 | static int JimWaitRemove(struct WaitInfoTable *table, phandle_t phandle) |
| @@ -5379,11 +5928,11 @@ | |
| 5379 | if (argc > 1 && Jim_CompareStringImmediate(interp, argv[argc - 1], "&")) { |
| 5380 | Jim_Obj *listObj; |
| 5381 | int i; |
| 5382 | |
| 5383 | argc--; |
| 5384 | numPids = JimCreatePipeline(interp, argc - 1, argv + 1, &pidPtr, NULL, NULL, NULL); |
| 5385 | if (numPids < 0) { |
| 5386 | return JIM_ERR; |
| 5387 | } |
| 5388 | |
| 5389 | listObj = Jim_NewListObj(interp, NULL, 0); |
| @@ -5395,11 +5944,11 @@ | |
| 5395 | Jim_Free(pidPtr); |
| 5396 | return JIM_OK; |
| 5397 | } |
| 5398 | |
| 5399 | numPids = |
| 5400 | JimCreatePipeline(interp, argc - 1, argv + 1, &pidPtr, NULL, &outputId, &errorId); |
| 5401 | |
| 5402 | if (numPids < 0) { |
| 5403 | return JIM_ERR; |
| 5404 | } |
| 5405 | |
| @@ -5487,11 +6036,11 @@ | |
| 5487 | objv[1] = Jim_NewStringObj(interp, "getfd", -1); |
| 5488 | |
| 5489 | if (Jim_EvalObjVector(interp, 2, objv) == JIM_OK) { |
| 5490 | jim_wide fd; |
| 5491 | if (Jim_GetWide(interp, Jim_GetResult(interp), &fd) == JIM_OK) { |
| 5492 | return fd; |
| 5493 | } |
| 5494 | } |
| 5495 | return -1; |
| 5496 | } |
| 5497 | |
| @@ -5541,12 +6090,11 @@ | |
| 5541 | |
| 5542 | if (argc > 1 && Jim_CompareStringImmediate(interp, argv[1], "-nohang")) { |
| 5543 | nohang = 1; |
| 5544 | } |
| 5545 | if (argc != nohang + 2) { |
| 5546 | Jim_WrongNumArgs(interp, 1, argv, "?-nohang? ?pid?"); |
| 5547 | return JIM_ERR; |
| 5548 | } |
| 5549 | if (Jim_GetLong(interp, argv[nohang + 1], &pid) != JIM_OK) { |
| 5550 | return JIM_ERR; |
| 5551 | } |
| 5552 | |
| @@ -5556,10 +6104,14 @@ | |
| 5556 | pid = -1; |
| 5557 | } |
| 5558 | #ifndef __MINGW32__ |
| 5559 | else if (pid < 0) { |
| 5560 | pid = phandle; |
| 5561 | } |
| 5562 | #endif |
| 5563 | |
| 5564 | errCodeObj = JimMakeErrorCode(interp, pid, status, NULL); |
| 5565 | |
| @@ -5571,22 +6123,192 @@ | |
| 5571 | return JIM_OK; |
| 5572 | } |
| 5573 | |
| 5574 | static int Jim_PidCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 5575 | { |
| 5576 | if (argc != 1) { |
| 5577 | Jim_WrongNumArgs(interp, 1, argv, ""); |
| 5578 | return JIM_ERR; |
| 5579 | } |
| 5580 | |
| 5581 | Jim_SetResultInt(interp, (jim_wide)getpid()); |
| 5582 | return JIM_OK; |
| 5583 | } |
| 5584 | |
| 5585 | static int |
| 5586 | JimCreatePipeline(Jim_Interp *interp, int argc, Jim_Obj *const *argv, phandle_t **pidArrayPtr, |
| 5587 | int *inPipePtr, int *outPipePtr, int *errFilePtr) |
| 5588 | { |
| 5589 | phandle_t *pidPtr = NULL; /* Points to alloc-ed array holding all |
| 5590 | * the pids of child processes. */ |
| 5591 | int numPids = 0; /* Actual number of processes that exist |
| 5592 | * at *pidPtr right now. */ |
| @@ -5624,126 +6346,88 @@ | |
| 5624 | int inputId = -1; |
| 5625 | int outputId = -1; |
| 5626 | int errorId = -1; |
| 5627 | int lastOutputId = -1; |
| 5628 | int pipeIds[2]; |
| 5629 | int firstArg, lastArg; /* Indexes of first and last arguments in |
| 5630 | * current command. */ |
| 5631 | int lastBar; |
| 5632 | int i; |
| 5633 | phandle_t phandle; |
| 5634 | char **save_environ; |
| 5635 | #if defined(HAVE_EXECVPE) && !defined(__MINGW32__) |
| 5636 | char **child_environ; |
| 5637 | #endif |
| 5638 | struct WaitInfoTable *table = Jim_CmdPrivData(interp); |
| 5639 | |
| 5640 | |
| 5641 | char **arg_array = Jim_Alloc(sizeof(*arg_array) * (argc + 1)); |
| 5642 | int arg_count = 0; |
| 5643 | |
| 5644 | if (inPipePtr != NULL) { |
| 5645 | *inPipePtr = -1; |
| 5646 | } |
| 5647 | if (outPipePtr != NULL) { |
| 5648 | *outPipePtr = -1; |
| 5649 | } |
| 5650 | if (errFilePtr != NULL) { |
| 5651 | *errFilePtr = -1; |
| 5652 | } |
| 5653 | pipeIds[0] = pipeIds[1] = -1; |
| 5654 | |
| 5655 | cmdCount = 1; |
| 5656 | lastBar = -1; |
| 5657 | for (i = 0; i < argc; i++) { |
| 5658 | const char *arg = Jim_String(argv[i]); |
| 5659 | |
| 5660 | if (arg[0] == '<') { |
| 5661 | inputFile = FILE_NAME; |
| 5662 | input = arg + 1; |
| 5663 | if (*input == '<') { |
| 5664 | inputFile = FILE_TEXT; |
| 5665 | input_len = Jim_Length(argv[i]) - 2; |
| 5666 | input++; |
| 5667 | } |
| 5668 | else if (*input == '@') { |
| 5669 | inputFile = FILE_HANDLE; |
| 5670 | input++; |
| 5671 | } |
| 5672 | |
| 5673 | if (!*input && ++i < argc) { |
| 5674 | input = Jim_GetString(argv[i], &input_len); |
| 5675 | } |
| 5676 | } |
| 5677 | else if (arg[0] == '>') { |
| 5678 | int dup_error = 0; |
| 5679 | |
| 5680 | outputFile = FILE_NAME; |
| 5681 | |
| 5682 | output = arg + 1; |
| 5683 | if (*output == '>') { |
| 5684 | outputFile = FILE_APPEND; |
| 5685 | output++; |
| 5686 | } |
| 5687 | if (*output == '&') { |
| 5688 | |
| 5689 | output++; |
| 5690 | dup_error = 1; |
| 5691 | } |
| 5692 | if (*output == '@') { |
| 5693 | outputFile = FILE_HANDLE; |
| 5694 | output++; |
| 5695 | } |
| 5696 | if (!*output && ++i < argc) { |
| 5697 | output = Jim_String(argv[i]); |
| 5698 | } |
| 5699 | if (dup_error) { |
| 5700 | errorFile = outputFile; |
| 5701 | error = output; |
| 5702 | } |
| 5703 | } |
| 5704 | else if (arg[0] == '2' && arg[1] == '>') { |
| 5705 | error = arg + 2; |
| 5706 | errorFile = FILE_NAME; |
| 5707 | |
| 5708 | if (*error == '@') { |
| 5709 | errorFile = FILE_HANDLE; |
| 5710 | error++; |
| 5711 | } |
| 5712 | else if (*error == '>') { |
| 5713 | errorFile = FILE_APPEND; |
| 5714 | error++; |
| 5715 | } |
| 5716 | if (!*error && ++i < argc) { |
| 5717 | error = Jim_String(argv[i]); |
| 5718 | } |
| 5719 | } |
| 5720 | else { |
| 5721 | if (strcmp(arg, "|") == 0 || strcmp(arg, "|&") == 0) { |
| 5722 | if (i == lastBar + 1 || i == argc - 1) { |
| 5723 | Jim_SetResultString(interp, "illegal use of | or |& in command", -1); |
| 5724 | goto badargs; |
| 5725 | } |
| 5726 | lastBar = i; |
| 5727 | cmdCount++; |
| 5728 | } |
| 5729 | |
| 5730 | arg_array[arg_count++] = (char *)arg; |
| 5731 | continue; |
| 5732 | } |
| 5733 | |
| 5734 | if (i >= argc) { |
| 5735 | Jim_SetResultFormatted(interp, "can't specify \"%s\" as last word in command", arg); |
| 5736 | goto badargs; |
| 5737 | } |
| 5738 | } |
| 5739 | |
| 5740 | if (arg_count == 0) { |
| 5741 | Jim_SetResultString(interp, "didn't specify command to execute", -1); |
| 5742 | badargs: |
| 5743 | Jim_Free(arg_array); |
| 5744 | return -1; |
| 5745 | } |
| 5746 | |
| 5747 | |
| 5748 | save_environ = JimSaveEnv(JimBuildEnv(interp)); |
| 5749 | |
| @@ -5759,42 +6443,30 @@ | |
| 5759 | goto error; |
| 5760 | } |
| 5761 | Jim_Lseek(inputId, 0L, SEEK_SET); |
| 5762 | } |
| 5763 | else if (inputFile == FILE_HANDLE) { |
| 5764 | int fd = JimGetChannelFd(interp, input); |
| 5765 | |
| 5766 | if (fd < 0) { |
| 5767 | goto error; |
| 5768 | } |
| 5769 | inputId = dup(fd); |
| 5770 | } |
| 5771 | else { |
| 5772 | inputId = Jim_OpenForRead(input); |
| 5773 | if (inputId == -1) { |
| 5774 | Jim_SetResultFormatted(interp, "couldn't read file \"%s\": %s", input, strerror(Jim_Errno())); |
| 5775 | goto error; |
| 5776 | } |
| 5777 | } |
| 5778 | } |
| 5779 | else if (inPipePtr != NULL) { |
| 5780 | if (pipe(pipeIds) != 0) { |
| 5781 | Jim_SetResultErrno(interp, "couldn't create input pipe for command"); |
| 5782 | goto error; |
| 5783 | } |
| 5784 | inputId = pipeIds[0]; |
| 5785 | *inPipePtr = pipeIds[1]; |
| 5786 | pipeIds[0] = pipeIds[1] = -1; |
| 5787 | } |
| 5788 | |
| 5789 | if (output != NULL) { |
| 5790 | if (outputFile == FILE_HANDLE) { |
| 5791 | int fd = JimGetChannelFd(interp, output); |
| 5792 | if (fd < 0) { |
| 5793 | goto error; |
| 5794 | } |
| 5795 | lastOutputId = dup(fd); |
| 5796 | } |
| 5797 | else { |
| 5798 | lastOutputId = Jim_OpenForWrite(output, outputFile == FILE_APPEND); |
| 5799 | if (lastOutputId == -1) { |
| 5800 | Jim_SetResultFormatted(interp, "couldn't write file \"%s\": %s", output, strerror(Jim_Errno())); |
| @@ -5809,10 +6481,11 @@ | |
| 5809 | } |
| 5810 | lastOutputId = pipeIds[1]; |
| 5811 | *outPipePtr = pipeIds[0]; |
| 5812 | pipeIds[0] = pipeIds[1] = -1; |
| 5813 | } |
| 5814 | |
| 5815 | if (error != NULL) { |
| 5816 | if (errorFile == FILE_HANDLE) { |
| 5817 | if (strcmp(error, "1") == 0) { |
| 5818 | |
| @@ -5823,15 +6496,14 @@ | |
| 5823 | |
| 5824 | error = "stdout"; |
| 5825 | } |
| 5826 | } |
| 5827 | if (errorId == -1) { |
| 5828 | int fd = JimGetChannelFd(interp, error); |
| 5829 | if (fd < 0) { |
| 5830 | goto error; |
| 5831 | } |
| 5832 | errorId = dup(fd); |
| 5833 | } |
| 5834 | } |
| 5835 | else { |
| 5836 | errorId = Jim_OpenForWrite(error, errorFile == FILE_APPEND); |
| 5837 | if (errorId == -1) { |
| @@ -5846,60 +6518,60 @@ | |
| 5846 | goto error; |
| 5847 | } |
| 5848 | *errFilePtr = dup(errorId); |
| 5849 | } |
| 5850 | |
| 5851 | |
| 5852 | pidPtr = Jim_Alloc(cmdCount * sizeof(*pidPtr)); |
| 5853 | for (firstArg = 0; firstArg < arg_count; numPids++, firstArg = lastArg + 1) { |
| 5854 | int pipe_dup_err = 0; |
| 5855 | int origErrorId = errorId; |
| 5856 | |
| 5857 | for (lastArg = firstArg; lastArg < arg_count; lastArg++) { |
| 5858 | if (strcmp(arg_array[lastArg], "|") == 0) { |
| 5859 | break; |
| 5860 | } |
| 5861 | if (strcmp(arg_array[lastArg], "|&") == 0) { |
| 5862 | pipe_dup_err = 1; |
| 5863 | break; |
| 5864 | } |
| 5865 | } |
| 5866 | |
| 5867 | if (lastArg == firstArg) { |
| 5868 | Jim_SetResultString(interp, "missing command to exec", -1); |
| 5869 | goto error; |
| 5870 | } |
| 5871 | |
| 5872 | |
| 5873 | arg_array[lastArg] = NULL; |
| 5874 | if (lastArg == arg_count) { |
| 5875 | outputId = lastOutputId; |
| 5876 | lastOutputId = -1; |
| 5877 | } |
| 5878 | else { |
| 5879 | if (pipe(pipeIds) != 0) { |
| 5880 | Jim_SetResultErrno(interp, "couldn't create pipe"); |
| 5881 | goto error; |
| 5882 | } |
| 5883 | outputId = pipeIds[1]; |
| 5884 | } |
| 5885 | |
| 5886 | |
| 5887 | if (pipe_dup_err) { |
| 5888 | errorId = outputId; |
| 5889 | } |
| 5890 | |
| 5891 | |
| 5892 | |
| 5893 | #ifdef __MINGW32__ |
| 5894 | phandle = JimStartWinProcess(interp, &arg_array[firstArg], save_environ, inputId, outputId, errorId); |
| 5895 | if (phandle == JIM_BAD_PHANDLE) { |
| 5896 | Jim_SetResultFormatted(interp, "couldn't exec \"%s\"", arg_array[firstArg]); |
| 5897 | goto error; |
| 5898 | } |
| 5899 | #else |
| 5900 | i = strlen(arg_array[firstArg]); |
| 5901 | |
| 5902 | #ifdef HAVE_EXECVPE |
| 5903 | child_environ = Jim_GetEnviron(); |
| 5904 | #endif |
| 5905 | #ifdef HAVE_VFORK |
| @@ -5940,14 +6612,14 @@ | |
| 5940 | } |
| 5941 | if (lastOutputId != -1) { |
| 5942 | close(lastOutputId); |
| 5943 | } |
| 5944 | |
| 5945 | execvpe(arg_array[firstArg], &arg_array[firstArg], child_environ); |
| 5946 | |
| 5947 | if (write(fileno(stderr), "couldn't exec \"", 15) && |
| 5948 | write(fileno(stderr), arg_array[firstArg], i) && |
| 5949 | write(fileno(stderr), "\"\n", 2)) { |
| 5950 | |
| 5951 | } |
| 5952 | #ifdef JIM_MAINTAINER |
| 5953 | { |
| @@ -5959,10 +6631,11 @@ | |
| 5959 | _exit(127); |
| 5960 | } |
| 5961 | #endif |
| 5962 | |
| 5963 | |
| 5964 | |
| 5965 | if (table->used == table->size) { |
| 5966 | table->size += WAIT_TABLE_GROW_BY; |
| 5967 | table->info = Jim_Realloc(table->info, table->size * sizeof(*table->info)); |
| 5968 | } |
| @@ -5969,11 +6642,11 @@ | |
| 5969 | |
| 5970 | table->info[table->used].phandle = phandle; |
| 5971 | table->info[table->used].flags = 0; |
| 5972 | table->used++; |
| 5973 | |
| 5974 | pidPtr[numPids] = phandle; |
| 5975 | |
| 5976 | |
| 5977 | errorId = origErrorId; |
| 5978 | |
| 5979 | |
| @@ -5997,22 +6670,17 @@ | |
| 5997 | close(lastOutputId); |
| 5998 | } |
| 5999 | if (errorId != -1) { |
| 6000 | close(errorId); |
| 6001 | } |
| 6002 | Jim_Free(arg_array); |
| 6003 | |
| 6004 | JimRestoreEnv(save_environ); |
| 6005 | |
| 6006 | return numPids; |
| 6007 | |
| 6008 | |
| 6009 | error: |
| 6010 | if ((inPipePtr != NULL) && (*inPipePtr != -1)) { |
| 6011 | close(*inPipePtr); |
| 6012 | *inPipePtr = -1; |
| 6013 | } |
| 6014 | if ((outPipePtr != NULL) && (*outPipePtr != -1)) { |
| 6015 | close(*outPipePtr); |
| 6016 | *outPipePtr = -1; |
| 6017 | } |
| 6018 | if ((errFilePtr != NULL) && (*errFilePtr != -1)) { |
| @@ -6034,10 +6702,39 @@ | |
| 6034 | Jim_Free(pidPtr); |
| 6035 | } |
| 6036 | numPids = -1; |
| 6037 | goto cleanup; |
| 6038 | } |
| 6039 | |
| 6040 | |
| 6041 | static int JimCleanupChildren(Jim_Interp *interp, int numPids, phandle_t *pidPtr, Jim_Obj *errStrObj) |
| 6042 | { |
| 6043 | struct WaitInfoTable *table = Jim_CmdPrivData(interp); |
| @@ -6064,14 +6761,15 @@ | |
| 6064 | struct WaitInfoTable *waitinfo; |
| 6065 | |
| 6066 | Jim_PackageProvideCheck(interp, "exec"); |
| 6067 | |
| 6068 | waitinfo = JimAllocWaitInfoTable(); |
| 6069 | Jim_CreateCommand(interp, "exec", Jim_ExecCmd, waitinfo, JimFreeWaitInfoTable); |
| 6070 | waitinfo->refcount++; |
| 6071 | Jim_CreateCommand(interp, "wait", Jim_WaitCommand, waitinfo, JimFreeWaitInfoTable); |
| 6072 | Jim_CreateCommand(interp, "pid", Jim_PidCommand, 0, 0); |
| 6073 | |
| 6074 | return JIM_OK; |
| 6075 | } |
| 6076 | |
| 6077 | #if defined(__MINGW32__) |
| @@ -6494,11 +7192,11 @@ | |
| 6494 | }; |
| 6495 | |
| 6496 | int Jim_clockInit(Jim_Interp *interp) |
| 6497 | { |
| 6498 | Jim_PackageProvideCheck(interp, "clock"); |
| 6499 | Jim_CreateCommand(interp, "clock", Jim_SubCmdProc, (void *)clock_command_table, NULL); |
| 6500 | return JIM_OK; |
| 6501 | } |
| 6502 | |
| 6503 | #include <limits.h> |
| 6504 | #include <stdlib.h> |
| @@ -6714,58 +7412,11 @@ | |
| 6714 | }; |
| 6715 | |
| 6716 | int Jim_arrayInit(Jim_Interp *interp) |
| 6717 | { |
| 6718 | Jim_PackageProvideCheck(interp, "array"); |
| 6719 | Jim_CreateCommand(interp, "array", Jim_SubCmdProc, (void *)array_command_table, NULL); |
| 6720 | return JIM_OK; |
| 6721 | } |
| 6722 | |
| 6723 | #include <sys/types.h> |
| 6724 | #include <sys/time.h> |
| 6725 | #include <sys/wait.h> |
| 6726 | #include <unistd.h> |
| 6727 | #include <string.h> |
| 6728 | #include <errno.h> |
| 6729 | |
| 6730 | |
| 6731 | #ifdef HAVE_SYS_SYSINFO_H |
| 6732 | #include <sys/sysinfo.h> |
| 6733 | #endif |
| 6734 | |
| 6735 | static void Jim_PosixSetError(Jim_Interp *interp) |
| 6736 | { |
| 6737 | Jim_SetResultString(interp, strerror(errno), -1); |
| 6738 | } |
| 6739 | |
| 6740 | #if defined(HAVE_FORK) |
| 6741 | static int Jim_PosixForkCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 6742 | { |
| 6743 | pid_t pid; |
| 6744 | |
| 6745 | JIM_NOTUSED(argv); |
| 6746 | |
| 6747 | if (argc != 1) { |
| 6748 | Jim_WrongNumArgs(interp, 1, argv, ""); |
| 6749 | return JIM_ERR; |
| 6750 | } |
| 6751 | if ((pid = fork()) == -1) { |
| 6752 | Jim_PosixSetError(interp); |
| 6753 | return JIM_ERR; |
| 6754 | } |
| 6755 | Jim_SetResultInt(interp, (jim_wide) pid); |
| 6756 | return JIM_OK; |
| 6757 | } |
| 6758 | #endif |
| 6759 | |
| 6760 | |
| 6761 | int Jim_posixInit(Jim_Interp *interp) |
| 6762 | { |
| 6763 | Jim_PackageProvideCheck(interp, "posix"); |
| 6764 | #ifdef HAVE_FORK |
| 6765 | Jim_CreateCommand(interp, "os.fork", Jim_PosixForkCommand, NULL, NULL); |
| 6766 | #endif |
| 6767 | return JIM_OK; |
| 6768 | } |
| 6769 | int Jim_InitStaticExtensions(Jim_Interp *interp) |
| 6770 | { |
| 6771 | extern int Jim_bootstrapInit(Jim_Interp *); |
| @@ -6773,11 +7424,10 @@ | |
| 6773 | extern int Jim_readdirInit(Jim_Interp *); |
| 6774 | extern int Jim_regexpInit(Jim_Interp *); |
| 6775 | extern int Jim_fileInit(Jim_Interp *); |
| 6776 | extern int Jim_globInit(Jim_Interp *); |
| 6777 | extern int Jim_execInit(Jim_Interp *); |
| 6778 | extern int Jim_posixInit(Jim_Interp *); |
| 6779 | extern int Jim_clockInit(Jim_Interp *); |
| 6780 | extern int Jim_arrayInit(Jim_Interp *); |
| 6781 | extern int Jim_stdlibInit(Jim_Interp *); |
| 6782 | extern int Jim_tclcompatInit(Jim_Interp *); |
| 6783 | Jim_bootstrapInit(interp); |
| @@ -6785,11 +7435,10 @@ | |
| 6785 | Jim_readdirInit(interp); |
| 6786 | Jim_regexpInit(interp); |
| 6787 | Jim_fileInit(interp); |
| 6788 | Jim_globInit(interp); |
| 6789 | Jim_execInit(interp); |
| 6790 | Jim_posixInit(interp); |
| 6791 | Jim_clockInit(interp); |
| 6792 | Jim_arrayInit(interp); |
| 6793 | Jim_stdlibInit(interp); |
| 6794 | Jim_tclcompatInit(interp); |
| 6795 | return JIM_OK; |
| @@ -6854,11 +7503,12 @@ | |
| 6854 | |
| 6855 | |
| 6856 | |
| 6857 | #define JIM_INTEGER_SPACE 24 |
| 6858 | |
| 6859 | #if defined(DEBUG_SHOW_SCRIPT) || defined(DEBUG_SHOW_SCRIPT_TOKENS) || defined(JIM_DEBUG_COMMAND) || defined(DEBUG_SHOW_SUBST) |
| 6860 | static const char *jim_tt_name(int type); |
| 6861 | #endif |
| 6862 | |
| 6863 | #ifdef JIM_DEBUG_PANIC |
| 6864 | static void JimPanicDump(int fail_condition, const char *fmt, ...); |
| @@ -6893,19 +7543,21 @@ | |
| 6893 | static void JimPrngSeed(Jim_Interp *interp, unsigned char *seed, int seedLen); |
| 6894 | static void JimRandomBytes(Jim_Interp *interp, void *dest, unsigned int len); |
| 6895 | static int JimSetNewVariable(Jim_HashTable *ht, Jim_Obj *nameObjPtr, Jim_VarVal *vv); |
| 6896 | static Jim_VarVal *JimFindVariable(Jim_HashTable *ht, Jim_Obj *nameObjPtr); |
| 6897 | static int SetVariableFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr); |
| 6898 | |
| 6899 | #define JIM_DICT_SUGAR 100 |
| 6900 | |
| 6901 | |
| 6902 | |
| 6903 | |
| 6904 | #define JimWideValue(objPtr) (objPtr)->internalRep.wideValue |
| 6905 | |
| 6906 | #define JimObjTypeName(O) ((O)->typePtr ? (O)->typePtr->name : "none") |
| 6907 | |
| 6908 | static int utf8_tounicode_case(const char *s, int *uc, int upper) |
| 6909 | { |
| 6910 | int l = utf8_tounicode(s, uc); |
| 6911 | if (upper) { |
| @@ -7111,11 +7763,11 @@ | |
| 7111 | static int JimStringFirst(const char *s1, int l1, const char *s2, int l2, int idx) |
| 7112 | { |
| 7113 | int i; |
| 7114 | int l1bytelen; |
| 7115 | |
| 7116 | if (!l1 || !l2 || l1 > l2) { |
| 7117 | return -1; |
| 7118 | } |
| 7119 | if (idx < 0) |
| 7120 | idx = 0; |
| 7121 | s2 += utf8_index(s2, idx); |
| @@ -7283,10 +7935,11 @@ | |
| 7283 | |
| 7284 | *doublePtr = strtod(str, &endptr); |
| 7285 | |
| 7286 | return JimCheckConversion(str, endptr); |
| 7287 | } |
| 7288 | |
| 7289 | static jim_wide JimPowWide(jim_wide b, jim_wide e) |
| 7290 | { |
| 7291 | jim_wide res = 1; |
| 7292 | |
| @@ -7311,10 +7964,11 @@ | |
| 7311 | } |
| 7312 | return res; |
| 7313 | } |
| 7314 | |
| 7315 | #ifdef JIM_DEBUG_PANIC |
| 7316 | static void JimPanicDump(int condition, const char *fmt, ...) |
| 7317 | { |
| 7318 | va_list ap; |
| 7319 | |
| 7320 | if (!condition) { |
| @@ -7447,10 +8101,11 @@ | |
| 7447 | ht->uniq = (rand() ^ time(NULL) ^ clock()); |
| 7448 | #else |
| 7449 | ht->uniq = 0; |
| 7450 | #endif |
| 7451 | } |
| 7452 | |
| 7453 | static void JimInitHashTableIterator(Jim_HashTable *ht, Jim_HashTableIterator *iter) |
| 7454 | { |
| 7455 | iter->ht = ht; |
| 7456 | iter->index = -1; |
| @@ -7716,20 +8371,23 @@ | |
| 7716 | |
| 7717 | static unsigned int JimStringCopyHTHashFunction(const void *key) |
| 7718 | { |
| 7719 | return Jim_GenHashFunction(key, strlen(key)); |
| 7720 | } |
| 7721 | |
| 7722 | static void *JimStringCopyHTDup(void *privdata, const void *key) |
| 7723 | { |
| 7724 | return Jim_StrDup(key); |
| 7725 | } |
| 7726 | |
| 7727 | static int JimStringCopyHTKeyCompare(void *privdata, const void *key1, const void *key2) |
| 7728 | { |
| 7729 | return strcmp(key1, key2) == 0; |
| 7730 | } |
| 7731 | |
| 7732 | static void JimStringCopyHTKeyDestructor(void *privdata, void *key) |
| 7733 | { |
| 7734 | Jim_Free(key); |
| 7735 | } |
| @@ -7746,10 +8404,11 @@ | |
| 7746 | typedef struct AssocDataValue |
| 7747 | { |
| 7748 | Jim_InterpDeleteProc *delProc; |
| 7749 | void *data; |
| 7750 | } AssocDataValue; |
| 7751 | |
| 7752 | static void JimAssocDataHashTableValueDestructor(void *privdata, void *data) |
| 7753 | { |
| 7754 | AssocDataValue *assocPtr = (AssocDataValue *) data; |
| 7755 | |
| @@ -7765,25 +8424,28 @@ | |
| 7765 | JimStringCopyHTKeyCompare, |
| 7766 | JimStringCopyHTKeyDestructor, |
| 7767 | JimAssocDataHashTableValueDestructor |
| 7768 | }; |
| 7769 | |
| 7770 | void Jim_InitStack(Jim_Stack *stack) |
| 7771 | { |
| 7772 | stack->len = 0; |
| 7773 | stack->maxlen = 0; |
| 7774 | stack->vector = NULL; |
| 7775 | } |
| 7776 | |
| 7777 | void Jim_FreeStack(Jim_Stack *stack) |
| 7778 | { |
| 7779 | Jim_Free(stack->vector); |
| 7780 | } |
| 7781 | |
| 7782 | int Jim_StackLen(Jim_Stack *stack) |
| 7783 | { |
| 7784 | return stack->len; |
| 7785 | } |
| 7786 | |
| 7787 | void Jim_StackPush(Jim_Stack *stack, void *element) |
| 7788 | { |
| 7789 | int neededLen = stack->len + 1; |
| @@ -7802,25 +8464,10 @@ | |
| 7802 | return NULL; |
| 7803 | stack->len--; |
| 7804 | return stack->vector[stack->len]; |
| 7805 | } |
| 7806 | |
| 7807 | void *Jim_StackPeek(Jim_Stack *stack) |
| 7808 | { |
| 7809 | if (stack->len == 0) |
| 7810 | return NULL; |
| 7811 | return stack->vector[stack->len - 1]; |
| 7812 | } |
| 7813 | |
| 7814 | void Jim_FreeStackElements(Jim_Stack *stack, void (*freeFunc) (void *ptr)) |
| 7815 | { |
| 7816 | int i; |
| 7817 | |
| 7818 | for (i = 0; i < stack->len; i++) |
| 7819 | freeFunc(stack->vector[i]); |
| 7820 | } |
| 7821 | |
| 7822 | |
| 7823 | |
| 7824 | #define JIM_TT_NONE 0 |
| 7825 | #define JIM_TT_STR 1 |
| 7826 | #define JIM_TT_ESC 2 |
| @@ -7901,10 +8548,11 @@ | |
| 7901 | pc->linenr = linenr; |
| 7902 | pc->comment = 1; |
| 7903 | pc->missing.ch = ' '; |
| 7904 | pc->missing.line = linenr; |
| 7905 | } |
| 7906 | |
| 7907 | static int JimParseScript(struct JimParserCtx *pc) |
| 7908 | { |
| 7909 | while (1) { |
| 7910 | if (!pc->len) { |
| @@ -7962,10 +8610,11 @@ | |
| 7962 | return JimParseStr(pc); |
| 7963 | } |
| 7964 | return JIM_OK; |
| 7965 | } |
| 7966 | } |
| 7967 | |
| 7968 | static int JimParseSep(struct JimParserCtx *pc) |
| 7969 | { |
| 7970 | pc->tstart = pc->p; |
| 7971 | pc->tline = pc->linenr; |
| @@ -7983,10 +8632,11 @@ | |
| 7983 | } |
| 7984 | pc->tend = pc->p - 1; |
| 7985 | pc->tt = JIM_TT_SEP; |
| 7986 | return JIM_OK; |
| 7987 | } |
| 7988 | |
| 7989 | static int JimParseEol(struct JimParserCtx *pc) |
| 7990 | { |
| 7991 | pc->tstart = pc->p; |
| 7992 | pc->tline = pc->linenr; |
| @@ -8151,36 +8801,40 @@ | |
| 8151 | } |
| 8152 | pc->missing.ch = '['; |
| 8153 | pc->missing.line = line; |
| 8154 | pc->tend = pc->p - 1; |
| 8155 | } |
| 8156 | |
| 8157 | static int JimParseBrace(struct JimParserCtx *pc) |
| 8158 | { |
| 8159 | pc->tstart = pc->p + 1; |
| 8160 | pc->tline = pc->linenr; |
| 8161 | pc->tt = JIM_TT_STR; |
| 8162 | JimParseSubBrace(pc); |
| 8163 | return JIM_OK; |
| 8164 | } |
| 8165 | |
| 8166 | static int JimParseCmd(struct JimParserCtx *pc) |
| 8167 | { |
| 8168 | pc->tstart = pc->p + 1; |
| 8169 | pc->tline = pc->linenr; |
| 8170 | pc->tt = JIM_TT_CMD; |
| 8171 | JimParseSubCmd(pc); |
| 8172 | return JIM_OK; |
| 8173 | } |
| 8174 | |
| 8175 | static int JimParseQuote(struct JimParserCtx *pc) |
| 8176 | { |
| 8177 | pc->tstart = pc->p + 1; |
| 8178 | pc->tline = pc->linenr; |
| 8179 | pc->tt = JimParseSubQuote(pc); |
| 8180 | return JIM_OK; |
| 8181 | } |
| 8182 | |
| 8183 | static int JimParseVar(struct JimParserCtx *pc) |
| 8184 | { |
| 8185 | |
| 8186 | pc->p++; |
| @@ -8278,10 +8932,11 @@ | |
| 8278 | pc->len++; |
| 8279 | return JIM_ERR; |
| 8280 | } |
| 8281 | return JIM_OK; |
| 8282 | } |
| 8283 | |
| 8284 | static int JimParseStr(struct JimParserCtx *pc) |
| 8285 | { |
| 8286 | if (pc->tt == JIM_TT_SEP || pc->tt == JIM_TT_EOL || |
| 8287 | pc->tt == JIM_TT_NONE || pc->tt == JIM_TT_STR) { |
| @@ -8381,10 +9036,11 @@ | |
| 8381 | pc->p++; |
| 8382 | pc->len--; |
| 8383 | } |
| 8384 | return JIM_OK; |
| 8385 | } |
| 8386 | |
| 8387 | static int JimParseComment(struct JimParserCtx *pc) |
| 8388 | { |
| 8389 | while (*pc->p) { |
| 8390 | if (*pc->p == '\\') { |
| @@ -8419,10 +9075,11 @@ | |
| 8419 | return c - 'a' + 10; |
| 8420 | if (c >= 'A' && c <= 'F') |
| 8421 | return c - 'A' + 10; |
| 8422 | return -1; |
| 8423 | } |
| 8424 | |
| 8425 | static int odigitval(int c) |
| 8426 | { |
| 8427 | if (c >= '0' && c <= '7') |
| 8428 | return c - '0'; |
| @@ -8609,10 +9266,11 @@ | |
| 8609 | } |
| 8610 | |
| 8611 | static int JimParseListSep(struct JimParserCtx *pc); |
| 8612 | static int JimParseListStr(struct JimParserCtx *pc); |
| 8613 | static int JimParseListQuote(struct JimParserCtx *pc); |
| 8614 | |
| 8615 | static int JimParseList(struct JimParserCtx *pc) |
| 8616 | { |
| 8617 | if (isspace(UCHAR(*pc->p))) { |
| 8618 | return JimParseListSep(pc); |
| @@ -8635,10 +9293,11 @@ | |
| 8635 | pc->tline = pc->linenr; |
| 8636 | pc->tt = JIM_TT_EOL; |
| 8637 | pc->eof = 1; |
| 8638 | return JIM_OK; |
| 8639 | } |
| 8640 | |
| 8641 | static int JimParseListSep(struct JimParserCtx *pc) |
| 8642 | { |
| 8643 | pc->tstart = pc->p; |
| 8644 | pc->tline = pc->linenr; |
| @@ -8651,10 +9310,11 @@ | |
| 8651 | } |
| 8652 | pc->tend = pc->p - 1; |
| 8653 | pc->tt = JIM_TT_SEP; |
| 8654 | return JIM_OK; |
| 8655 | } |
| 8656 | |
| 8657 | static int JimParseListQuote(struct JimParserCtx *pc) |
| 8658 | { |
| 8659 | pc->p++; |
| 8660 | pc->len--; |
| @@ -8688,10 +9348,11 @@ | |
| 8688 | } |
| 8689 | |
| 8690 | pc->tend = pc->p - 1; |
| 8691 | return JIM_OK; |
| 8692 | } |
| 8693 | |
| 8694 | static int JimParseListStr(struct JimParserCtx *pc) |
| 8695 | { |
| 8696 | pc->tstart = pc->p; |
| 8697 | pc->tline = pc->linenr; |
| @@ -8734,10 +9395,11 @@ | |
| 8734 | |
| 8735 | objPtr = Jim_Alloc(sizeof(*objPtr)); |
| 8736 | } |
| 8737 | |
| 8738 | objPtr->refCount = 0; |
| 8739 | |
| 8740 | |
| 8741 | objPtr->prevObjPtr = NULL; |
| 8742 | objPtr->nextObjPtr = interp->liveList; |
| 8743 | if (interp->liveList) |
| @@ -8794,10 +9456,12 @@ | |
| 8794 | Jim_Obj *Jim_DuplicateObj(Jim_Interp *interp, Jim_Obj *objPtr) |
| 8795 | { |
| 8796 | Jim_Obj *dupPtr; |
| 8797 | |
| 8798 | dupPtr = Jim_NewObj(interp); |
| 8799 | if (objPtr->bytes == NULL) { |
| 8800 | |
| 8801 | dupPtr->bytes = NULL; |
| 8802 | } |
| 8803 | else if (objPtr->length == 0) { |
| @@ -8856,10 +9520,11 @@ | |
| 8856 | |
| 8857 | Jim_GetString(objPtr, NULL); |
| 8858 | } |
| 8859 | return objPtr->bytes; |
| 8860 | } |
| 8861 | |
| 8862 | static void JimSetStringBytes(Jim_Obj *objPtr, const char *str) |
| 8863 | { |
| 8864 | objPtr->bytes = Jim_StrDup(str); |
| 8865 | objPtr->length = strlen(str); |
| @@ -8884,15 +9549,17 @@ | |
| 8884 | FreeInterpolatedInternalRep, |
| 8885 | DupInterpolatedInternalRep, |
| 8886 | NULL, |
| 8887 | JIM_TYPE_NONE, |
| 8888 | }; |
| 8889 | |
| 8890 | static void FreeInterpolatedInternalRep(Jim_Interp *interp, Jim_Obj *objPtr) |
| 8891 | { |
| 8892 | Jim_DecrRefCount(interp, objPtr->internalRep.dictSubstValue.indexObjPtr); |
| 8893 | } |
| 8894 | |
| 8895 | static void DupInterpolatedInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr) |
| 8896 | { |
| 8897 | |
| 8898 | dupPtr->internalRep = srcPtr->internalRep; |
| @@ -8908,18 +9575,20 @@ | |
| 8908 | NULL, |
| 8909 | DupStringInternalRep, |
| 8910 | NULL, |
| 8911 | JIM_TYPE_REFERENCES, |
| 8912 | }; |
| 8913 | |
| 8914 | static void DupStringInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr) |
| 8915 | { |
| 8916 | JIM_NOTUSED(interp); |
| 8917 | |
| 8918 | dupPtr->internalRep.strValue.maxLength = srcPtr->length; |
| 8919 | dupPtr->internalRep.strValue.charLength = srcPtr->internalRep.strValue.charLength; |
| 8920 | } |
| 8921 | |
| 8922 | static int SetStringFromAny(Jim_Interp *interp, Jim_Obj *objPtr) |
| 8923 | { |
| 8924 | if (objPtr->typePtr != &stringObjType) { |
| 8925 | |
| @@ -9047,10 +9716,11 @@ | |
| 9047 | void Jim_AppendObj(Jim_Interp *interp, Jim_Obj *objPtr, Jim_Obj *appendObjPtr) |
| 9048 | { |
| 9049 | int len; |
| 9050 | const char *str = Jim_GetString(appendObjPtr, &len); |
| 9051 | Jim_AppendString(interp, objPtr, str, len); |
| 9052 | } |
| 9053 | |
| 9054 | void Jim_AppendStrings(Jim_Interp *interp, Jim_Obj *objPtr, ...) |
| 9055 | { |
| 9056 | va_list ap; |
| @@ -9075,11 +9745,11 @@ | |
| 9075 | else { |
| 9076 | int Alen, Blen; |
| 9077 | const char *sA = Jim_GetString(aObjPtr, &Alen); |
| 9078 | const char *sB = Jim_GetString(bObjPtr, &Blen); |
| 9079 | |
| 9080 | return Alen == Blen && memcmp(sA, sB, Alen) == 0; |
| 9081 | } |
| 9082 | } |
| 9083 | |
| 9084 | int Jim_StringMatchObj(Jim_Interp *interp, Jim_Obj *patternObjPtr, Jim_Obj *objPtr, int nocase) |
| 9085 | { |
| @@ -9128,10 +9798,11 @@ | |
| 9128 | if (rangeLen < 0) |
| 9129 | rangeLen = 0; |
| 9130 | |
| 9131 | *rangeLenPtr = rangeLen; |
| 9132 | } |
| 9133 | |
| 9134 | static int JimStringGetRange(Jim_Interp *interp, Jim_Obj *firstObjPtr, Jim_Obj *lastObjPtr, |
| 9135 | int len, int *first, int *last, int *range) |
| 9136 | { |
| 9137 | if (Jim_GetIndex(interp, firstObjPtr, first) != JIM_OK) { |
| @@ -9349,10 +10020,11 @@ | |
| 9349 | } |
| 9350 | |
| 9351 | static const char default_trim_chars[] = " \t\n\r"; |
| 9352 | |
| 9353 | static int default_trim_chars_len = sizeof(default_trim_chars); |
| 9354 | |
| 9355 | static Jim_Obj *JimStringTrimLeft(Jim_Interp *interp, Jim_Obj *strObjPtr, Jim_Obj *trimcharsObjPtr) |
| 9356 | { |
| 9357 | int len; |
| 9358 | const char *str = Jim_GetString(strObjPtr, &len); |
| @@ -9369,10 +10041,11 @@ | |
| 9369 | return strObjPtr; |
| 9370 | } |
| 9371 | |
| 9372 | return Jim_NewStringObj(interp, newstr, len - (newstr - str)); |
| 9373 | } |
| 9374 | |
| 9375 | static Jim_Obj *JimStringTrimRight(Jim_Interp *interp, Jim_Obj *strObjPtr, Jim_Obj *trimcharsObjPtr) |
| 9376 | { |
| 9377 | int len; |
| 9378 | const char *trimchars = default_trim_chars; |
| @@ -9406,10 +10079,11 @@ | |
| 9406 | strObjPtr->length = (nontrim - strObjPtr->bytes); |
| 9407 | } |
| 9408 | |
| 9409 | return strObjPtr; |
| 9410 | } |
| 9411 | |
| 9412 | static Jim_Obj *JimStringTrim(Jim_Interp *interp, Jim_Obj *strObjPtr, Jim_Obj *trimcharsObjPtr) |
| 9413 | { |
| 9414 | |
| 9415 | Jim_Obj *objPtr = JimStringTrimLeft(interp, strObjPtr, trimcharsObjPtr); |
| @@ -9428,15 +10102,17 @@ | |
| 9428 | |
| 9429 | |
| 9430 | #ifdef HAVE_ISASCII |
| 9431 | #define jim_isascii isascii |
| 9432 | #else |
| 9433 | static int jim_isascii(int c) |
| 9434 | { |
| 9435 | return !(c & ~0x7f); |
| 9436 | } |
| 9437 | #endif |
| 9438 | |
| 9439 | static int JimStringIs(Jim_Interp *interp, Jim_Obj *strObjPtr, Jim_Obj *strClass, int strict) |
| 9440 | { |
| 9441 | static const char * const strclassnames[] = { |
| 9442 | "integer", "alpha", "alnum", "ascii", "digit", |
| @@ -9538,16 +10214,27 @@ | |
| 9538 | } |
| 9539 | objPtr->internalRep.ptr = (char *)str; |
| 9540 | return 1; |
| 9541 | } |
| 9542 | } |
| 9543 | |
| 9544 | static int qsortCompareStringPointers(const void *a, const void *b) |
| 9545 | { |
| 9546 | char *const *sa = (char *const *)a; |
| 9547 | char *const *sb = (char *const *)b; |
| 9548 | |
| 9549 | return strcmp(*sa, *sb); |
| 9550 | } |
| 9551 | |
| 9552 | |
| 9553 | |
| @@ -9578,10 +10265,11 @@ | |
| 9578 | NULL, |
| 9579 | NULL, |
| 9580 | NULL, |
| 9581 | JIM_NONE, |
| 9582 | }; |
| 9583 | |
| 9584 | static Jim_Obj *JimNewScriptLineObj(Jim_Interp *interp, int argc, int line) |
| 9585 | { |
| 9586 | Jim_Obj *objPtr; |
| 9587 | |
| @@ -9672,17 +10360,19 @@ | |
| 9672 | ParseToken *list; |
| 9673 | int size; |
| 9674 | int count; |
| 9675 | ParseToken static_list[20]; |
| 9676 | } ParseTokenList; |
| 9677 | |
| 9678 | static void ScriptTokenListInit(ParseTokenList *tokenlist) |
| 9679 | { |
| 9680 | tokenlist->list = tokenlist->static_list; |
| 9681 | tokenlist->size = sizeof(tokenlist->static_list) / sizeof(ParseToken); |
| 9682 | tokenlist->count = 0; |
| 9683 | } |
| 9684 | |
| 9685 | static void ScriptTokenListFree(ParseTokenList *tokenlist) |
| 9686 | { |
| 9687 | if (tokenlist->list != tokenlist->static_list) { |
| 9688 | Jim_Free(tokenlist->list); |
| @@ -9970,10 +10660,11 @@ | |
| 9970 | struct JimParserCtx parser; |
| 9971 | struct ScriptObj *script; |
| 9972 | ParseTokenList tokenlist; |
| 9973 | Jim_Obj *fileNameObj; |
| 9974 | int line; |
| 9975 | |
| 9976 | |
| 9977 | fileNameObj = Jim_GetSourceInfo(interp, objPtr, &line); |
| 9978 | |
| 9979 | |
| @@ -9988,10 +10679,15 @@ | |
| 9988 | |
| 9989 | |
| 9990 | ScriptAddToken(&tokenlist, scriptText + scriptTextLen, 0, JIM_TT_EOF, 0); |
| 9991 | |
| 9992 | |
| 9993 | script = Jim_Alloc(sizeof(*script)); |
| 9994 | memset(script, 0, sizeof(*script)); |
| 9995 | script->inUse = 1; |
| 9996 | script->fileNameObj = fileNameObj; |
| 9997 | Jim_IncrRefCount(script->fileNameObj); |
| @@ -10005,10 +10701,12 @@ | |
| 10005 | |
| 10006 | |
| 10007 | Jim_FreeIntRep(interp, objPtr); |
| 10008 | Jim_SetIntRepPtr(objPtr, script); |
| 10009 | objPtr->typePtr = &scriptObjType; |
| 10010 | } |
| 10011 | |
| 10012 | static ScriptObj *JimGetScript(Jim_Interp *interp, Jim_Obj *objPtr) |
| 10013 | { |
| 10014 | if (objPtr == interp->emptyObj) { |
| @@ -10033,20 +10731,22 @@ | |
| 10033 | Jim_Free(interp->oldCmdCache); |
| 10034 | interp->oldCmdCache = next; |
| 10035 | } |
| 10036 | interp->oldCmdCacheSize = 0; |
| 10037 | } |
| 10038 | |
| 10039 | static void JimIncrCmdRefCount(Jim_Cmd *cmdPtr) |
| 10040 | { |
| 10041 | cmdPtr->inUse++; |
| 10042 | } |
| 10043 | |
| 10044 | static void JimDecrCmdRefCount(Jim_Interp *interp, Jim_Cmd *cmdPtr) |
| 10045 | { |
| 10046 | if (--cmdPtr->inUse == 0) { |
| 10047 | if (cmdPtr->isproc) { |
| 10048 | Jim_DecrRefCount(interp, cmdPtr->u.proc.argListObjPtr); |
| 10049 | Jim_DecrRefCount(interp, cmdPtr->u.proc.bodyObjPtr); |
| 10050 | Jim_DecrRefCount(interp, cmdPtr->u.proc.nsObj); |
| 10051 | if (cmdPtr->u.proc.staticVars) { |
| 10052 | Jim_FreeHashTable(cmdPtr->u.proc.staticVars); |
| @@ -10074,10 +10774,11 @@ | |
| 10074 | |
| 10075 | static void JimIncrVarRef(Jim_VarVal *vv) |
| 10076 | { |
| 10077 | vv->refCount++; |
| 10078 | } |
| 10079 | |
| 10080 | static void JimDecrVarRef(Jim_Interp *interp, Jim_VarVal *vv) |
| 10081 | { |
| 10082 | assert(vv->refCount > 0); |
| 10083 | if (--vv->refCount == 0) { |
| @@ -10085,15 +10786,17 @@ | |
| 10085 | Jim_DecrRefCount(interp, vv->objPtr); |
| 10086 | } |
| 10087 | Jim_Free(vv); |
| 10088 | } |
| 10089 | } |
| 10090 | |
| 10091 | static void JimVariablesHTValDestructor(void *interp, void *val) |
| 10092 | { |
| 10093 | JimDecrVarRef(interp, val); |
| 10094 | } |
| 10095 | |
| 10096 | static unsigned int JimObjectHTHashFunction(const void *key) |
| 10097 | { |
| 10098 | Jim_Obj *keyObj = (Jim_Obj *)key; |
| 10099 | int length; |
| @@ -10125,10 +10828,11 @@ | |
| 10125 | } |
| 10126 | #endif |
| 10127 | string = Jim_GetString(keyObj, &length); |
| 10128 | return Jim_GenHashFunction((const unsigned char *)string, length); |
| 10129 | } |
| 10130 | |
| 10131 | static int JimObjectHTKeyCompare(void *privdata, const void *key1, const void *key2) |
| 10132 | { |
| 10133 | return Jim_StringEqObj((Jim_Obj *)key1, (Jim_Obj *)key2); |
| 10134 | } |
| @@ -10136,10 +10840,11 @@ | |
| 10136 | static void *JimObjectHTKeyValDup(void *privdata, const void *val) |
| 10137 | { |
| 10138 | Jim_IncrRefCount((Jim_Obj *)val); |
| 10139 | return (void *)val; |
| 10140 | } |
| 10141 | |
| 10142 | static void JimObjectHTKeyValDestructor(void *interp, void *val) |
| 10143 | { |
| 10144 | Jim_DecrRefCount(interp, (Jim_Obj *)val); |
| 10145 | } |
| @@ -10172,25 +10877,28 @@ | |
| 10172 | } |
| 10173 | } |
| 10174 | *length = len; |
| 10175 | return str; |
| 10176 | } |
| 10177 | |
| 10178 | static unsigned int JimCommandsHT_HashFunction(const void *key) |
| 10179 | { |
| 10180 | int len; |
| 10181 | const char *str = Jim_GetStringNoQualifier((Jim_Obj *)key, &len); |
| 10182 | return Jim_GenHashFunction((const unsigned char *)str, len); |
| 10183 | } |
| 10184 | |
| 10185 | static int JimCommandsHT_KeyCompare(void *privdata, const void *key1, const void *key2) |
| 10186 | { |
| 10187 | int len1, len2; |
| 10188 | const char *str1 = Jim_GetStringNoQualifier((Jim_Obj *)key1, &len1); |
| 10189 | const char *str2 = Jim_GetStringNoQualifier((Jim_Obj *)key2, &len2); |
| 10190 | return len1 == len2 && memcmp(str1, str2, len1) == 0; |
| 10191 | } |
| 10192 | |
| 10193 | static void JimCommandsHT_ValDestructor(void *interp, void *val) |
| 10194 | { |
| 10195 | JimDecrCmdRefCount(interp, val); |
| 10196 | } |
| @@ -10262,35 +10970,50 @@ | |
| 10262 | |
| 10263 | |
| 10264 | Jim_ReplaceHashEntry(&interp->commands, nameObjPtr, cmd); |
| 10265 | } |
| 10266 | |
| 10267 | int Jim_CreateCommandObj(Jim_Interp *interp, Jim_Obj *cmdNameObj, |
| 10268 | Jim_CmdProc *cmdProc, void *privData, Jim_DelCmdProc *delProc) |
| 10269 | { |
| 10270 | Jim_Cmd *cmdPtr = Jim_Alloc(sizeof(*cmdPtr)); |
| 10271 | |
| 10272 | |
| 10273 | memset(cmdPtr, 0, sizeof(*cmdPtr)); |
| 10274 | cmdPtr->inUse = 1; |
| 10275 | cmdPtr->u.native.delProc = delProc; |
| 10276 | cmdPtr->u.native.cmdProc = cmdProc; |
| 10277 | cmdPtr->u.native.privData = privData; |
| 10278 | |
| 10279 | Jim_IncrRefCount(cmdNameObj); |
| 10280 | JimCreateCommand(interp, cmdNameObj, cmdPtr); |
| 10281 | Jim_DecrRefCount(interp, cmdNameObj); |
| 10282 | |
| 10283 | return JIM_OK; |
| 10284 | } |
| 10285 | |
| 10286 | |
| 10287 | int Jim_CreateCommand(Jim_Interp *interp, const char *cmdNameStr, |
| 10288 | Jim_CmdProc *cmdProc, void *privData, Jim_DelCmdProc *delProc) |
| 10289 | { |
| 10290 | return Jim_CreateCommandObj(interp, Jim_NewStringObj(interp, cmdNameStr, -1), cmdProc, privData, delProc); |
| 10291 | } |
| 10292 | |
| 10293 | static int JimCreateProcedureStatics(Jim_Interp *interp, Jim_Cmd *cmdPtr, Jim_Obj *staticsListObjPtr) |
| 10294 | { |
| 10295 | int len, i; |
| 10296 | |
| @@ -10347,10 +11070,15 @@ | |
| 10347 | if (byref) { |
| 10348 | vv = nameObjPtr->internalRep.varValue.vv; |
| 10349 | } |
| 10350 | else { |
| 10351 | initObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_NONE); |
| 10352 | } |
| 10353 | break; |
| 10354 | |
| 10355 | case JIM_ERR: |
| 10356 | |
| @@ -10402,11 +11130,11 @@ | |
| 10402 | #endif |
| 10403 | |
| 10404 | static void JimUpdateProcNamespace(Jim_Interp *interp, Jim_Cmd *cmdPtr, Jim_Obj *nameObjPtr) |
| 10405 | { |
| 10406 | #ifdef jim_ext_namespace |
| 10407 | if (cmdPtr->isproc) { |
| 10408 | int len; |
| 10409 | const char *cmdname = Jim_GetStringNoQualifier(nameObjPtr, &len); |
| 10410 | |
| 10411 | const char *pt = Jim_memrchr(cmdname, ':', len); |
| 10412 | if (pt && pt != cmdname && pt[-1] == ':') { |
| @@ -10438,11 +11166,11 @@ | |
| 10438 | |
| 10439 | cmdPtr = Jim_Alloc(sizeof(*cmdPtr) + sizeof(struct Jim_ProcArg) * argListLen); |
| 10440 | assert(cmdPtr); |
| 10441 | memset(cmdPtr, 0, sizeof(*cmdPtr)); |
| 10442 | cmdPtr->inUse = 1; |
| 10443 | cmdPtr->isproc = 1; |
| 10444 | cmdPtr->u.proc.argListObjPtr = argListObjPtr; |
| 10445 | cmdPtr->u.proc.argListLen = argListLen; |
| 10446 | cmdPtr->u.proc.bodyObjPtr = bodyObjPtr; |
| 10447 | cmdPtr->u.proc.argsPos = -1; |
| 10448 | cmdPtr->u.proc.arglist = (struct Jim_ProcArg *)(cmdPtr + 1); |
| @@ -10581,10 +11309,11 @@ | |
| 10581 | |
| 10582 | static void FreeCommandInternalRep(Jim_Interp *interp, Jim_Obj *objPtr) |
| 10583 | { |
| 10584 | Jim_DecrRefCount(interp, objPtr->internalRep.cmdValue.nsObj); |
| 10585 | } |
| 10586 | |
| 10587 | static void DupCommandInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr) |
| 10588 | { |
| 10589 | dupPtr->internalRep.cmdValue = srcPtr->internalRep.cmdValue; |
| 10590 | dupPtr->typePtr = srcPtr->typePtr; |
| @@ -10638,11 +11367,11 @@ | |
| 10638 | objPtr->internalRep.cmdValue.cmdPtr = cmd; |
| 10639 | objPtr->internalRep.cmdValue.nsObj = interp->framePtr->nsObj; |
| 10640 | Jim_IncrRefCount(interp->framePtr->nsObj); |
| 10641 | Jim_DecrRefCount(interp, qualifiedNameObj); |
| 10642 | } |
| 10643 | while (cmd->u.proc.upcall) { |
| 10644 | cmd = cmd->prevCmd; |
| 10645 | } |
| 10646 | return cmd; |
| 10647 | } |
| 10648 | |
| @@ -10720,30 +11449,34 @@ | |
| 10720 | return JIM_OK; |
| 10721 | } |
| 10722 | |
| 10723 | |
| 10724 | static int JimDictSugarSet(Jim_Interp *interp, Jim_Obj *ObjPtr, Jim_Obj *valObjPtr); |
| 10725 | static Jim_Obj *JimDictSugarGet(Jim_Interp *interp, Jim_Obj *ObjPtr, int flags); |
| 10726 | |
| 10727 | static int JimSetNewVariable(Jim_HashTable *ht, Jim_Obj *nameObjPtr, Jim_VarVal *vv) |
| 10728 | { |
| 10729 | return Jim_AddHashEntry(ht, nameObjPtr, vv); |
| 10730 | } |
| 10731 | |
| 10732 | static Jim_VarVal *JimFindVariable(Jim_HashTable *ht, Jim_Obj *nameObjPtr) |
| 10733 | { |
| 10734 | Jim_HashEntry *he = Jim_FindHashEntry(ht, nameObjPtr); |
| 10735 | if (he) { |
| 10736 | return (Jim_VarVal *)Jim_GetHashEntryVal(he); |
| 10737 | } |
| 10738 | return NULL; |
| 10739 | } |
| 10740 | |
| 10741 | static int JimUnsetVariable(Jim_HashTable *ht, Jim_Obj *nameObjPtr) |
| 10742 | { |
| 10743 | return Jim_DeleteHashEntry(ht, nameObjPtr); |
| 10744 | } |
| 10745 | |
| 10746 | static Jim_VarVal *JimCreateVariable(Jim_Interp *interp, Jim_Obj *nameObjPtr, Jim_Obj *valObjPtr) |
| 10747 | { |
| 10748 | const char *name; |
| 10749 | Jim_CallFrame *framePtr; |
| @@ -10784,19 +11517,22 @@ | |
| 10784 | return vv; |
| 10785 | } |
| 10786 | |
| 10787 | int Jim_SetVariable(Jim_Interp *interp, Jim_Obj *nameObjPtr, Jim_Obj *valObjPtr) |
| 10788 | { |
| 10789 | int err; |
| 10790 | Jim_VarVal *vv; |
| 10791 | |
| 10792 | switch (SetVariableFromAny(interp, nameObjPtr)) { |
| 10793 | case JIM_DICT_SUGAR: |
| 10794 | return JimDictSugarSet(interp, nameObjPtr, valObjPtr); |
| 10795 | |
| 10796 | case JIM_ERR: |
| 10797 | JimCreateVariable(interp, nameObjPtr, valObjPtr); |
| 10798 | break; |
| 10799 | |
| 10800 | case JIM_OK: |
| 10801 | vv = nameObjPtr->internalRep.varValue.vv; |
| 10802 | if (vv->linkFramePtr == NULL) { |
| @@ -10807,17 +11543,20 @@ | |
| 10807 | else { |
| 10808 | Jim_CallFrame *savedCallFrame; |
| 10809 | |
| 10810 | savedCallFrame = interp->framePtr; |
| 10811 | interp->framePtr = vv->linkFramePtr; |
| 10812 | err = Jim_SetVariable(interp, vv->objPtr, valObjPtr); |
| 10813 | interp->framePtr = savedCallFrame; |
| 10814 | if (err != JIM_OK) |
| 10815 | return err; |
| 10816 | } |
| 10817 | } |
| 10818 | return JIM_OK; |
| 10819 | } |
| 10820 | |
| 10821 | int Jim_SetVariableStr(Jim_Interp *interp, const char *name, Jim_Obj *objPtr) |
| 10822 | { |
| 10823 | Jim_Obj *nameObjPtr; |
| @@ -11087,19 +11826,21 @@ | |
| 11087 | |
| 11088 | p = strchr(str, '('); |
| 11089 | JimPanic((p == NULL, "JimDictSugarParseVarKey() called for non-dict-sugar (%s)", str)); |
| 11090 | |
| 11091 | varObjPtr = Jim_NewStringObj(interp, str, p - str); |
| 11092 | |
| 11093 | p++; |
| 11094 | keyLen = (str + len) - p; |
| 11095 | if (str[len - 1] == ')') { |
| 11096 | keyLen--; |
| 11097 | } |
| 11098 | |
| 11099 | |
| 11100 | keyObjPtr = Jim_NewStringObj(interp, p, keyLen); |
| 11101 | |
| 11102 | Jim_IncrRefCount(varObjPtr); |
| 11103 | Jim_IncrRefCount(keyObjPtr); |
| 11104 | *varPtrPtr = varObjPtr; |
| 11105 | *keyPtrPtr = keyObjPtr; |
| @@ -11175,10 +11916,11 @@ | |
| 11175 | void FreeDictSubstInternalRep(Jim_Interp *interp, Jim_Obj *objPtr) |
| 11176 | { |
| 11177 | Jim_DecrRefCount(interp, objPtr->internalRep.dictSubstValue.varNameObjPtr); |
| 11178 | Jim_DecrRefCount(interp, objPtr->internalRep.dictSubstValue.indexObjPtr); |
| 11179 | } |
| 11180 | |
| 11181 | static void DupDictSubstInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr) |
| 11182 | { |
| 11183 | |
| 11184 | dupPtr->internalRep = srcPtr->internalRep; |
| @@ -11270,10 +12012,11 @@ | |
| 11270 | cf->nsObj = nsObj; |
| 11271 | Jim_IncrRefCount(nsObj); |
| 11272 | |
| 11273 | return cf; |
| 11274 | } |
| 11275 | |
| 11276 | static int JimDeleteLocalProcs(Jim_Interp *interp, Jim_Stack *localCommands) |
| 11277 | { |
| 11278 | |
| 11279 | if (localCommands) { |
| @@ -11298,11 +12041,11 @@ | |
| 11298 | Jim_DeleteHashEntry(ht, cmdNameObj); |
| 11299 | } |
| 11300 | } |
| 11301 | Jim_DecrRefCount(interp, cmdNameObj); |
| 11302 | } |
| 11303 | Jim_FreeStack(localCommands); |
| 11304 | Jim_Free(localCommands); |
| 11305 | } |
| 11306 | return JIM_OK; |
| 11307 | } |
| 11308 | |
| @@ -11352,10 +12095,11 @@ | |
| 11352 | return retcode; |
| 11353 | } |
| 11354 | |
| 11355 | #define JIM_FCF_FULL 0 |
| 11356 | #define JIM_FCF_REUSE 1 |
| 11357 | static void JimFreeCallFrame(Jim_Interp *interp, Jim_CallFrame *cf, int action) |
| 11358 | { |
| 11359 | JimDeleteLocalProcs(interp, cf->localCommands); |
| 11360 | |
| 11361 | if (cf->procArgsObjPtr) |
| @@ -11456,10 +12200,13 @@ | |
| 11456 | JimInvokeDefer(i, JIM_OK); |
| 11457 | cfx = cf->parent; |
| 11458 | JimFreeCallFrame(i, cf, JIM_FCF_FULL); |
| 11459 | } |
| 11460 | |
| 11461 | Jim_DecrRefCount(i, i->emptyObj); |
| 11462 | Jim_DecrRefCount(i, i->trueObj); |
| 11463 | Jim_DecrRefCount(i, i->falseObj); |
| 11464 | Jim_DecrRefCount(i, i->result); |
| 11465 | Jim_DecrRefCount(i, i->stackTrace); |
| @@ -11467,12 +12214,10 @@ | |
| 11467 | Jim_DecrRefCount(i, i->unknown); |
| 11468 | Jim_DecrRefCount(i, i->defer); |
| 11469 | Jim_DecrRefCount(i, i->nullScriptObj); |
| 11470 | Jim_DecrRefCount(i, i->currentFilenameObj); |
| 11471 | |
| 11472 | Jim_FreeHashTable(&i->commands); |
| 11473 | |
| 11474 | |
| 11475 | Jim_InterpIncrProcEpoch(i); |
| 11476 | |
| 11477 | #ifdef JIM_REFERENCES |
| 11478 | Jim_FreeHashTable(&i->references); |
| @@ -11602,10 +12347,11 @@ | |
| 11602 | } |
| 11603 | } |
| 11604 | } |
| 11605 | return NULL; |
| 11606 | } |
| 11607 | |
| 11608 | static Jim_EvalFrame *JimGetEvalFrameByProcLevel(Jim_Interp *interp, int proclevel) |
| 11609 | { |
| 11610 | Jim_EvalFrame *evalFrame; |
| 11611 | |
| @@ -11632,11 +12378,11 @@ | |
| 11632 | static Jim_Obj *JimProcForEvalFrame(Jim_Interp *interp, Jim_EvalFrame *frame) |
| 11633 | { |
| 11634 | if (frame == interp->evalFrame || (frame->cmd && frame->cmd->cmdNameObj)) { |
| 11635 | Jim_EvalFrame *e; |
| 11636 | for (e = frame->parent; e; e = e->parent) { |
| 11637 | if (e->cmd && e->cmd->isproc && e->cmd->cmdNameObj) { |
| 11638 | break; |
| 11639 | } |
| 11640 | } |
| 11641 | if (e && e->cmd && e->cmd->cmdNameObj) { |
| 11642 | return e->cmd->cmdNameObj; |
| @@ -11660,23 +12406,25 @@ | |
| 11660 | Jim_ListAppendElement(interp, listObj, procNameObj ? procNameObj : interp->emptyObj); |
| 11661 | Jim_ListAppendElement(interp, listObj, fileNameObj); |
| 11662 | Jim_ListAppendElement(interp, listObj, Jim_NewIntObj(interp, linenr)); |
| 11663 | Jim_ListAppendElement(interp, listObj, Jim_NewListObj(interp, frame->argv, frame->argc)); |
| 11664 | } |
| 11665 | |
| 11666 | static void JimSetStackTrace(Jim_Interp *interp, Jim_Obj *stackTraceObj) |
| 11667 | { |
| 11668 | |
| 11669 | Jim_IncrRefCount(stackTraceObj); |
| 11670 | Jim_DecrRefCount(interp, interp->stackTrace); |
| 11671 | interp->stackTrace = stackTraceObj; |
| 11672 | interp->errorFlag = 1; |
| 11673 | } |
| 11674 | |
| 11675 | static void JimSetErrorStack(Jim_Interp *interp, ScriptObj *script) |
| 11676 | { |
| 11677 | if (!interp->errorFlag) { |
| 11678 | int i; |
| 11679 | Jim_Obj *stackTrace = Jim_NewListObj(interp, NULL, 0); |
| 11680 | |
| 11681 | if (interp->procLevel == 0 && script) { |
| 11682 | Jim_ListAppendElement(interp, stackTrace, interp->emptyObj); |
| @@ -11744,10 +12492,11 @@ | |
| 11744 | NULL, |
| 11745 | UpdateStringOfInt, |
| 11746 | JIM_TYPE_NONE, |
| 11747 | }; |
| 11748 | |
| 11749 | |
| 11750 | static void UpdateStringOfInt(struct Jim_Obj *objPtr) |
| 11751 | { |
| 11752 | char buf[JIM_INTEGER_SPACE + 1]; |
| 11753 | jim_wide wideValue = JimWideValue(objPtr); |
| @@ -11779,10 +12528,11 @@ | |
| 11779 | } |
| 11780 | buf[pos] = 0; |
| 11781 | |
| 11782 | JimSetStringBytes(objPtr, buf); |
| 11783 | } |
| 11784 | |
| 11785 | static int SetIntFromAny(Jim_Interp *interp, Jim_Obj *objPtr, int flags) |
| 11786 | { |
| 11787 | jim_wide wideValue; |
| 11788 | const char *str; |
| @@ -11812,10 +12562,11 @@ | |
| 11812 | objPtr->internalRep.wideValue = wideValue; |
| 11813 | return JIM_OK; |
| 11814 | } |
| 11815 | |
| 11816 | #ifdef JIM_OPTIMIZATION |
| 11817 | static int JimIsWide(Jim_Obj *objPtr) |
| 11818 | { |
| 11819 | return objPtr->typePtr == &intObjType; |
| 11820 | } |
| 11821 | #endif |
| @@ -11881,10 +12632,11 @@ | |
| 11881 | Jim_Obj *objPtr; |
| 11882 | |
| 11883 | objPtr = Jim_NewObj(interp); |
| 11884 | objPtr->typePtr = &intObjType; |
| 11885 | objPtr->bytes = NULL; |
| 11886 | objPtr->internalRep.wideValue = wideValue; |
| 11887 | return objPtr; |
| 11888 | } |
| 11889 | |
| 11890 | #define JIM_DOUBLE_SPACE 30 |
| @@ -11906,10 +12658,11 @@ | |
| 11906 | #endif |
| 11907 | #if !HAVE_DECL_ISINF |
| 11908 | #undef isinf |
| 11909 | #define isinf(X) (1.0 / (X) == 0.0) |
| 11910 | #endif |
| 11911 | |
| 11912 | static void UpdateStringOfDouble(struct Jim_Obj *objPtr) |
| 11913 | { |
| 11914 | double value = objPtr->internalRep.doubleValue; |
| 11915 | |
| @@ -11951,10 +12704,11 @@ | |
| 11951 | buf[i] = '\0'; |
| 11952 | } |
| 11953 | JimSetStringBytes(objPtr, buf); |
| 11954 | } |
| 11955 | } |
| 11956 | |
| 11957 | static int SetDoubleFromAny(Jim_Interp *interp, Jim_Obj *objPtr) |
| 11958 | { |
| 11959 | double doubleValue; |
| 11960 | jim_wide wideValue; |
| @@ -12020,10 +12774,11 @@ | |
| 12020 | Jim_Obj *objPtr; |
| 12021 | |
| 12022 | objPtr = Jim_NewObj(interp); |
| 12023 | objPtr->typePtr = &doubleObjType; |
| 12024 | objPtr->bytes = NULL; |
| 12025 | objPtr->internalRep.doubleValue = doubleValue; |
| 12026 | return objPtr; |
| 12027 | } |
| 12028 | |
| 12029 | static int SetBooleanFromAny(Jim_Interp *interp, Jim_Obj *objPtr, int flags); |
| @@ -12043,10 +12798,11 @@ | |
| 12043 | |
| 12044 | static const int jim_true_false_lens[8] = { |
| 12045 | 1, 4, 3, 2, |
| 12046 | 1, 5, 2, 3, |
| 12047 | }; |
| 12048 | |
| 12049 | static int SetBooleanFromAny(Jim_Interp *interp, Jim_Obj *objPtr, int flags) |
| 12050 | { |
| 12051 | int index = Jim_FindByName(Jim_String(objPtr), jim_true_false_strings, |
| 12052 | sizeof(jim_true_false_strings) / sizeof(*jim_true_false_strings)); |
| @@ -12109,10 +12865,11 @@ | |
| 12109 | } |
| 12110 | |
| 12111 | #define JIM_ELESTR_SIMPLE 0 |
| 12112 | #define JIM_ELESTR_BRACE 1 |
| 12113 | #define JIM_ELESTR_QUOTE 2 |
| 12114 | static unsigned char ListElementQuotingType(const char *s, int len) |
| 12115 | { |
| 12116 | int i, level, blevel, trySimple = 1; |
| 12117 | |
| 12118 | |
| @@ -12255,10 +13012,11 @@ | |
| 12255 | } |
| 12256 | *p = '\0'; |
| 12257 | |
| 12258 | return p - q; |
| 12259 | } |
| 12260 | |
| 12261 | static void JimMakeListStringRep(Jim_Obj *objPtr, Jim_Obj **objv, int objc) |
| 12262 | { |
| 12263 | #define STATIC_QUOTING_LEN 32 |
| 12264 | int i, bufLen, realLength; |
| @@ -12341,15 +13099,17 @@ | |
| 12341 | |
| 12342 | if (quotingType != staticQuoting) { |
| 12343 | Jim_Free(quotingType); |
| 12344 | } |
| 12345 | } |
| 12346 | |
| 12347 | static void UpdateStringOfList(struct Jim_Obj *objPtr) |
| 12348 | { |
| 12349 | JimMakeListStringRep(objPtr, objPtr->internalRep.listValue.ele, objPtr->internalRep.listValue.len); |
| 12350 | } |
| 12351 | |
| 12352 | static int SetListFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr) |
| 12353 | { |
| 12354 | struct JimParserCtx parser; |
| 12355 | const char *str; |
| @@ -12401,10 +13161,11 @@ | |
| 12401 | JimParseList(&parser); |
| 12402 | if (parser.tt != JIM_TT_STR && parser.tt != JIM_TT_ESC) |
| 12403 | continue; |
| 12404 | elementPtr = JimParserGetTokenObj(interp, &parser); |
| 12405 | Jim_SetSourceInfo(interp, elementPtr, fileNameObj, parser.tline); |
| 12406 | ListAppendElement(objPtr, elementPtr); |
| 12407 | } |
| 12408 | } |
| 12409 | Jim_DecrRefCount(interp, fileNameObj); |
| 12410 | return JIM_OK; |
| @@ -12415,10 +13176,11 @@ | |
| 12415 | Jim_Obj *objPtr; |
| 12416 | |
| 12417 | objPtr = Jim_NewObj(interp); |
| 12418 | objPtr->typePtr = &listObjType; |
| 12419 | objPtr->bytes = NULL; |
| 12420 | objPtr->internalRep.listValue.ele = NULL; |
| 12421 | objPtr->internalRep.listValue.len = 0; |
| 12422 | objPtr->internalRep.listValue.maxLen = 0; |
| 12423 | |
| 12424 | if (len) { |
| @@ -12466,10 +13228,11 @@ | |
| 12466 | int unique; |
| 12467 | int (*subfn)(Jim_Obj **, Jim_Obj **); |
| 12468 | }; |
| 12469 | |
| 12470 | static struct lsort_info *sort_info; |
| 12471 | |
| 12472 | static int ListSortIndexHelper(Jim_Obj **lhsObj, Jim_Obj **rhsObj) |
| 12473 | { |
| 12474 | Jim_Obj *lObj, *rObj; |
| 12475 | |
| @@ -12483,15 +13246,17 @@ | |
| 12483 | |
| 12484 | static int ListSortString(Jim_Obj **lhsObj, Jim_Obj **rhsObj) |
| 12485 | { |
| 12486 | return Jim_StringCompareObj(sort_info->interp, *lhsObj, *rhsObj, 0) * sort_info->order; |
| 12487 | } |
| 12488 | |
| 12489 | static int ListSortStringNoCase(Jim_Obj **lhsObj, Jim_Obj **rhsObj) |
| 12490 | { |
| 12491 | return Jim_StringCompareObj(sort_info->interp, *lhsObj, *rhsObj, 1) * sort_info->order; |
| 12492 | } |
| 12493 | |
| 12494 | static int ListSortDict(Jim_Obj **lhsObj, Jim_Obj **rhsObj) |
| 12495 | { |
| 12496 | |
| 12497 | const char *left = Jim_String(*lhsObj); |
| @@ -12525,10 +13290,11 @@ | |
| 12525 | return Jim_StringCompareObj(sort_info->interp, *lhsObj, *rhsObj, 0) * sort_info->order; |
| 12526 | } |
| 12527 | } |
| 12528 | } |
| 12529 | } |
| 12530 | |
| 12531 | static int ListSortInteger(Jim_Obj **lhsObj, Jim_Obj **rhsObj) |
| 12532 | { |
| 12533 | jim_wide lhs = 0, rhs = 0; |
| 12534 | |
| @@ -12537,10 +13303,11 @@ | |
| 12537 | longjmp(sort_info->jmpbuf, JIM_ERR); |
| 12538 | } |
| 12539 | |
| 12540 | return JimSign(lhs - rhs) * sort_info->order; |
| 12541 | } |
| 12542 | |
| 12543 | static int ListSortReal(Jim_Obj **lhsObj, Jim_Obj **rhsObj) |
| 12544 | { |
| 12545 | double lhs = 0, rhs = 0; |
| 12546 | |
| @@ -12554,10 +13321,11 @@ | |
| 12554 | if (lhs > rhs) { |
| 12555 | return sort_info->order; |
| 12556 | } |
| 12557 | return -sort_info->order; |
| 12558 | } |
| 12559 | |
| 12560 | static int ListSortCommand(Jim_Obj **lhsObj, Jim_Obj **rhsObj) |
| 12561 | { |
| 12562 | Jim_Obj *compare_script; |
| 12563 | int rc; |
| @@ -12711,10 +13479,11 @@ | |
| 12711 | } |
| 12712 | point = listPtr->internalRep.listValue.ele + idx; |
| 12713 | memmove(point + elemc, point, (currentLen - idx) * sizeof(Jim_Obj *)); |
| 12714 | for (i = 0; i < elemc; ++i) { |
| 12715 | point[i] = elemVec[i]; |
| 12716 | Jim_IncrRefCount(point[i]); |
| 12717 | } |
| 12718 | listPtr->internalRep.listValue.len += elemc; |
| 12719 | } |
| 12720 | |
| @@ -12827,10 +13596,11 @@ | |
| 12827 | err: |
| 12828 | if (idxes != static_idxes) |
| 12829 | Jim_Free(idxes); |
| 12830 | return ret; |
| 12831 | } |
| 12832 | |
| 12833 | static int ListSetIndex(Jim_Interp *interp, Jim_Obj *listPtr, int idx, |
| 12834 | Jim_Obj *newObjPtr, int flags) |
| 12835 | { |
| 12836 | SetListFromAny(interp, listPtr); |
| @@ -12843,10 +13613,11 @@ | |
| 12843 | } |
| 12844 | if (idx < 0) |
| 12845 | idx = listPtr->internalRep.listValue.len + idx; |
| 12846 | Jim_DecrRefCount(interp, listPtr->internalRep.listValue.ele[idx]); |
| 12847 | listPtr->internalRep.listValue.ele[idx] = newObjPtr; |
| 12848 | Jim_IncrRefCount(newObjPtr); |
| 12849 | return JIM_OK; |
| 12850 | } |
| 12851 | |
| 12852 | int Jim_ListSetIndex(Jim_Interp *interp, Jim_Obj *varNamePtr, |
| @@ -12880,12 +13651,13 @@ | |
| 12880 | goto err; |
| 12881 | if (ListSetIndex(interp, objPtr, idx, newObjPtr, JIM_ERRMSG) == JIM_ERR) |
| 12882 | goto err; |
| 12883 | Jim_InvalidateStringRep(objPtr); |
| 12884 | Jim_InvalidateStringRep(varObjPtr); |
| 12885 | if (Jim_SetVariable(interp, varNamePtr, varObjPtr) != JIM_OK) |
| 12886 | goto err; |
| 12887 | Jim_SetResult(interp, varObjPtr); |
| 12888 | return JIM_OK; |
| 12889 | err: |
| 12890 | if (shared) { |
| 12891 | Jim_FreeNewObj(interp, varObjPtr); |
| @@ -13134,15 +13906,17 @@ | |
| 13134 | #ifdef JIM_RANDOMISE_HASH |
| 13135 | dict->uniq = (rand() ^ time(NULL) ^ clock()); |
| 13136 | #endif |
| 13137 | return dict; |
| 13138 | } |
| 13139 | |
| 13140 | static void FreeDictInternalRep(Jim_Interp *interp, Jim_Obj *objPtr) |
| 13141 | { |
| 13142 | JimFreeDict(interp, objPtr->internalRep.dictValue); |
| 13143 | } |
| 13144 | |
| 13145 | static void DupDictInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr) |
| 13146 | { |
| 13147 | Jim_Dict *oldDict = srcPtr->internalRep.dictValue; |
| 13148 | int i; |
| @@ -13164,15 +13938,17 @@ | |
| 13164 | memcpy(newDict->ht, oldDict->ht, sizeof(*oldDict->ht) * oldDict->size); |
| 13165 | |
| 13166 | dupPtr->internalRep.dictValue = newDict; |
| 13167 | dupPtr->typePtr = &dictObjType; |
| 13168 | } |
| 13169 | |
| 13170 | static void UpdateStringOfDict(struct Jim_Obj *objPtr) |
| 13171 | { |
| 13172 | JimMakeListStringRep(objPtr, objPtr->internalRep.dictValue->table, objPtr->internalRep.dictValue->len); |
| 13173 | } |
| 13174 | |
| 13175 | static int SetDictFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr) |
| 13176 | { |
| 13177 | int listlen; |
| 13178 | |
| @@ -13279,10 +14055,11 @@ | |
| 13279 | |
| 13280 | dict->table[dict->len++] = keyObjPtr; |
| 13281 | dict->table[dict->len++] = valueObjPtr; |
| 13282 | |
| 13283 | } |
| 13284 | return JIM_OK; |
| 13285 | } |
| 13286 | } |
| 13287 | |
| 13288 | int Jim_DictAddElement(Jim_Interp *interp, Jim_Obj *objPtr, |
| @@ -13390,11 +14167,10 @@ | |
| 13390 | |
| 13391 | return JIM_ERR; |
| 13392 | } |
| 13393 | varObjPtr = objPtr = Jim_NewDictObj(interp, NULL, 0); |
| 13394 | if (Jim_SetVariable(interp, varNamePtr, objPtr) != JIM_OK) { |
| 13395 | Jim_FreeNewObj(interp, varObjPtr); |
| 13396 | return JIM_ERR; |
| 13397 | } |
| 13398 | } |
| 13399 | if ((shared = Jim_IsShared(objPtr))) |
| 13400 | varObjPtr = objPtr = Jim_DuplicateObj(interp, objPtr); |
| @@ -13411,10 +14187,13 @@ | |
| 13411 | if (Jim_DictAddElement(interp, objPtr, keyv[keyc - 1], newObjPtr) != JIM_OK) { |
| 13412 | if (newObjPtr || (flags & JIM_MUSTEXIST)) { |
| 13413 | goto err; |
| 13414 | } |
| 13415 | } |
| 13416 | break; |
| 13417 | } |
| 13418 | |
| 13419 | |
| 13420 | Jim_InvalidateStringRep(dictObjPtr); |
| @@ -13435,11 +14214,11 @@ | |
| 13435 | } |
| 13436 | |
| 13437 | Jim_InvalidateStringRep(objPtr); |
| 13438 | Jim_InvalidateStringRep(varObjPtr); |
| 13439 | if (Jim_SetVariable(interp, varNamePtr, varObjPtr) != JIM_OK) { |
| 13440 | goto err; |
| 13441 | } |
| 13442 | |
| 13443 | if (!(flags & JIM_NORESULT)) { |
| 13444 | Jim_SetResult(interp, varObjPtr); |
| 13445 | } |
| @@ -13459,10 +14238,11 @@ | |
| 13459 | NULL, |
| 13460 | NULL, |
| 13461 | UpdateStringOfIndex, |
| 13462 | JIM_TYPE_NONE, |
| 13463 | }; |
| 13464 | |
| 13465 | static void UpdateStringOfIndex(struct Jim_Obj *objPtr) |
| 13466 | { |
| 13467 | if (objPtr->internalRep.intValue == -1) { |
| 13468 | JimSetStringBytes(objPtr, "end"); |
| @@ -13477,10 +14257,11 @@ | |
| 13477 | sprintf(buf, "end%d", objPtr->internalRep.intValue + 1); |
| 13478 | } |
| 13479 | JimSetStringBytes(objPtr, buf); |
| 13480 | } |
| 13481 | } |
| 13482 | |
| 13483 | static int SetIndexFromAny(Jim_Interp *interp, Jim_Obj *objPtr) |
| 13484 | { |
| 13485 | jim_wide idx; |
| 13486 | int end = 0; |
| @@ -13530,10 +14311,13 @@ | |
| 13530 | idx--; |
| 13531 | } |
| 13532 | } |
| 13533 | else if (idx < 0) { |
| 13534 | idx = -INT_MAX; |
| 13535 | } |
| 13536 | |
| 13537 | |
| 13538 | Jim_FreeIntRep(interp, objPtr); |
| 13539 | objPtr->typePtr = &indexObjType; |
| @@ -13597,10 +14381,11 @@ | |
| 13597 | } |
| 13598 | else { |
| 13599 | return jimReturnCodes[code]; |
| 13600 | } |
| 13601 | } |
| 13602 | |
| 13603 | static int SetReturnCodeFromAny(Jim_Interp *interp, Jim_Obj *objPtr) |
| 13604 | { |
| 13605 | int returnCode; |
| 13606 | jim_wide wideValue; |
| @@ -13663,10 +14448,13 @@ | |
| 13663 | JIM_EXPROP_POW, |
| 13664 | |
| 13665 | |
| 13666 | JIM_EXPROP_STREQ, |
| 13667 | JIM_EXPROP_STRNE, |
| 13668 | JIM_EXPROP_STRIN, |
| 13669 | JIM_EXPROP_STRNI, |
| 13670 | JIM_EXPROP_STRLT, |
| 13671 | JIM_EXPROP_STRGT, |
| 13672 | JIM_EXPROP_STRLE, |
| @@ -13730,10 +14518,11 @@ | |
| 13730 | } Jim_ExprOperator; |
| 13731 | |
| 13732 | static int JimExprGetTerm(Jim_Interp *interp, struct JimExprNode *node, Jim_Obj **objPtrPtr); |
| 13733 | static int JimExprGetTermBoolean(Jim_Interp *interp, struct JimExprNode *node); |
| 13734 | static int JimExprEvalTermNode(Jim_Interp *interp, struct JimExprNode *node); |
| 13735 | |
| 13736 | static int JimExprOpNumUnary(Jim_Interp *interp, struct JimExprNode *node) |
| 13737 | { |
| 13738 | int intresult = 1; |
| 13739 | int rc, bA = 0; |
| @@ -13806,10 +14595,17 @@ | |
| 13806 | else if ((rc = Jim_GetBoolean(interp, A, &bA)) == JIM_OK) { |
| 13807 | switch (node->type) { |
| 13808 | case JIM_EXPROP_NOT: |
| 13809 | wC = !bA; |
| 13810 | break; |
| 13811 | default: |
| 13812 | abort(); |
| 13813 | } |
| 13814 | } |
| 13815 | |
| @@ -13824,18 +14620,20 @@ | |
| 13824 | |
| 13825 | Jim_DecrRefCount(interp, A); |
| 13826 | |
| 13827 | return rc; |
| 13828 | } |
| 13829 | |
| 13830 | static double JimRandDouble(Jim_Interp *interp) |
| 13831 | { |
| 13832 | unsigned long x; |
| 13833 | JimRandomBytes(interp, &x, sizeof(x)); |
| 13834 | |
| 13835 | return (double)x / (double)~0UL; |
| 13836 | } |
| 13837 | |
| 13838 | static int JimExprOpIntUnary(Jim_Interp *interp, struct JimExprNode *node) |
| 13839 | { |
| 13840 | jim_wide wA; |
| 13841 | Jim_Obj *A; |
| @@ -13862,10 +14660,11 @@ | |
| 13862 | |
| 13863 | Jim_DecrRefCount(interp, A); |
| 13864 | |
| 13865 | return rc; |
| 13866 | } |
| 13867 | |
| 13868 | static int JimExprOpNone(Jim_Interp *interp, struct JimExprNode *node) |
| 13869 | { |
| 13870 | JimPanic((node->type != JIM_EXPROP_FUNC_RAND, "JimExprOpNone only support rand()")); |
| 13871 | |
| @@ -13873,10 +14672,11 @@ | |
| 13873 | |
| 13874 | return JIM_OK; |
| 13875 | } |
| 13876 | |
| 13877 | #ifdef JIM_MATH_FUNCTIONS |
| 13878 | static int JimExprOpDoubleUnary(Jim_Interp *interp, struct JimExprNode *node) |
| 13879 | { |
| 13880 | int rc; |
| 13881 | double dA, dC; |
| 13882 | Jim_Obj *A; |
| @@ -14218,10 +15018,11 @@ | |
| 14218 | goto done; |
| 14219 | doubleresult: |
| 14220 | Jim_SetResult(interp, Jim_NewDoubleObj(interp, dC)); |
| 14221 | goto done; |
| 14222 | } |
| 14223 | |
| 14224 | static int JimSearchList(Jim_Interp *interp, Jim_Obj *listObjPtr, Jim_Obj *valObj) |
| 14225 | { |
| 14226 | int listlen; |
| 14227 | int i; |
| @@ -14233,10 +15034,29 @@ | |
| 14233 | } |
| 14234 | } |
| 14235 | return 0; |
| 14236 | } |
| 14237 | |
| 14238 | |
| 14239 | |
| 14240 | static int JimExprOpStrBin(Jim_Interp *interp, struct JimExprNode *node) |
| 14241 | { |
| 14242 | Jim_Obj *A, *B; |
| @@ -14277,21 +15097,33 @@ | |
| 14277 | case JIM_EXPROP_STRIN: |
| 14278 | wC = JimSearchList(interp, B, A); |
| 14279 | break; |
| 14280 | case JIM_EXPROP_STRNI: |
| 14281 | wC = !JimSearchList(interp, B, A); |
| 14282 | break; |
| 14283 | default: |
| 14284 | abort(); |
| 14285 | } |
| 14286 | Jim_SetResultInt(interp, wC); |
| 14287 | |
| 14288 | Jim_DecrRefCount(interp, A); |
| 14289 | Jim_DecrRefCount(interp, B); |
| 14290 | |
| 14291 | return rc; |
| 14292 | } |
| 14293 | |
| 14294 | static int ExprBool(Jim_Interp *interp, Jim_Obj *obj) |
| 14295 | { |
| 14296 | long l; |
| 14297 | double d; |
| @@ -14312,10 +15144,11 @@ | |
| 14312 | } |
| 14313 | |
| 14314 | Jim_DecrRefCount(interp, obj); |
| 14315 | return ret; |
| 14316 | } |
| 14317 | |
| 14318 | static int JimExprOpAnd(Jim_Interp *interp, struct JimExprNode *node) |
| 14319 | { |
| 14320 | |
| 14321 | int result = JimExprGetTermBoolean(interp, node->left); |
| @@ -14328,10 +15161,11 @@ | |
| 14328 | return JIM_ERR; |
| 14329 | } |
| 14330 | Jim_SetResultInt(interp, result); |
| 14331 | return JIM_OK; |
| 14332 | } |
| 14333 | |
| 14334 | static int JimExprOpOr(Jim_Interp *interp, struct JimExprNode *node) |
| 14335 | { |
| 14336 | |
| 14337 | int result = JimExprGetTermBoolean(interp, node->left); |
| @@ -14344,10 +15178,11 @@ | |
| 14344 | return JIM_ERR; |
| 14345 | } |
| 14346 | Jim_SetResultInt(interp, result); |
| 14347 | return JIM_OK; |
| 14348 | } |
| 14349 | |
| 14350 | static int JimExprOpTernary(Jim_Interp *interp, struct JimExprNode *node) |
| 14351 | { |
| 14352 | |
| 14353 | int result = JimExprGetTermBoolean(interp, node->left); |
| @@ -14407,10 +15242,12 @@ | |
| 14407 | |
| 14408 | OPRINIT_ATTR("**", 120, 2, JimExprOpBin, OP_RIGHT_ASSOC), |
| 14409 | |
| 14410 | OPRINIT("eq", 60, 2, JimExprOpStrBin), |
| 14411 | OPRINIT("ne", 60, 2, JimExprOpStrBin), |
| 14412 | |
| 14413 | OPRINIT("in", 55, 2, JimExprOpStrBin), |
| 14414 | OPRINIT("ni", 55, 2, JimExprOpStrBin), |
| 14415 | |
| 14416 | OPRINIT("lt", 75, 2, JimExprOpStrBin), |
| @@ -14458,10 +15295,11 @@ | |
| 14458 | #undef OPRINIT |
| 14459 | #undef OPRINIT_ATTR |
| 14460 | |
| 14461 | #define JIM_EXPR_OPERATORS_NUM \ |
| 14462 | (sizeof(Jim_ExprOperators)/sizeof(struct Jim_ExprOperator)) |
| 14463 | |
| 14464 | static int JimParseExpression(struct JimParserCtx *pc) |
| 14465 | { |
| 14466 | pc->errmsg = NULL; |
| 14467 | |
| @@ -14557,10 +15395,11 @@ | |
| 14557 | return JimParseExprOperator(pc); |
| 14558 | break; |
| 14559 | } |
| 14560 | return JIM_OK; |
| 14561 | } |
| 14562 | |
| 14563 | static int JimParseExprNumber(struct JimParserCtx *pc) |
| 14564 | { |
| 14565 | char *end; |
| 14566 | |
| @@ -14581,10 +15420,11 @@ | |
| 14581 | } |
| 14582 | pc->tend = pc->p - 1; |
| 14583 | pc->len -= (pc->p - pc->tstart); |
| 14584 | return JIM_OK; |
| 14585 | } |
| 14586 | |
| 14587 | static int JimParseExprIrrational(struct JimParserCtx *pc) |
| 14588 | { |
| 14589 | const char *irrationals[] = { "NaN", "nan", "NAN", "Inf", "inf", "INF", NULL }; |
| 14590 | int i; |
| @@ -14600,10 +15440,11 @@ | |
| 14600 | return JIM_OK; |
| 14601 | } |
| 14602 | } |
| 14603 | return JIM_ERR; |
| 14604 | } |
| 14605 | |
| 14606 | static int JimParseExprBoolean(struct JimParserCtx *pc) |
| 14607 | { |
| 14608 | int i; |
| 14609 | for (i = 0; i < sizeof(jim_true_false_strings) / sizeof(*jim_true_false_strings); i++) { |
| @@ -14624,10 +15465,11 @@ | |
| 14624 | if (opcode < JIM_TT_EXPR_OP) { |
| 14625 | return &dummy_op; |
| 14626 | } |
| 14627 | return &Jim_ExprOperators[opcode - JIM_TT_EXPR_OP]; |
| 14628 | } |
| 14629 | |
| 14630 | static int JimParseExprOperator(struct JimParserCtx *pc) |
| 14631 | { |
| 14632 | int i; |
| 14633 | const struct Jim_ExprOperator *bestOp = NULL; |
| @@ -14691,10 +15533,11 @@ | |
| 14691 | struct JimExprNode *expr; |
| 14692 | struct JimExprNode *nodes; |
| 14693 | int len; |
| 14694 | int inUse; |
| 14695 | }; |
| 14696 | |
| 14697 | static void ExprTreeFreeNodes(Jim_Interp *interp, struct JimExprNode *nodes, int num) |
| 14698 | { |
| 14699 | int i; |
| 14700 | for (i = 0; i < num; i++) { |
| @@ -14702,16 +15545,18 @@ | |
| 14702 | Jim_DecrRefCount(interp, nodes[i].objPtr); |
| 14703 | } |
| 14704 | } |
| 14705 | Jim_Free(nodes); |
| 14706 | } |
| 14707 | |
| 14708 | static void ExprTreeFree(Jim_Interp *interp, struct ExprTree *expr) |
| 14709 | { |
| 14710 | ExprTreeFreeNodes(interp, expr->nodes, expr->len); |
| 14711 | Jim_Free(expr); |
| 14712 | } |
| 14713 | |
| 14714 | static void FreeExprInternalRep(Jim_Interp *interp, Jim_Obj *objPtr) |
| 14715 | { |
| 14716 | struct ExprTree *expr = (void *)objPtr->internalRep.ptr; |
| 14717 | |
| @@ -14721,10 +15566,11 @@ | |
| 14721 | } |
| 14722 | |
| 14723 | ExprTreeFree(interp, expr); |
| 14724 | } |
| 14725 | } |
| 14726 | |
| 14727 | static void DupExprInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr) |
| 14728 | { |
| 14729 | JIM_NOTUSED(interp); |
| 14730 | JIM_NOTUSED(srcPtr); |
| @@ -14744,10 +15590,11 @@ | |
| 14744 | struct JimExprNode *nodes; |
| 14745 | struct JimExprNode *next; |
| 14746 | }; |
| 14747 | |
| 14748 | #ifdef DEBUG_SHOW_EXPR |
| 14749 | static void JimShowExprNode(struct JimExprNode *node, int level) |
| 14750 | { |
| 14751 | int i; |
| 14752 | for (i = 0; i < level; i++) { |
| 14753 | printf(" "); |
| @@ -15029,11 +15876,11 @@ | |
| 15029 | builder.fileNameObj = fileNameObj; |
| 15030 | |
| 15031 | builder.nodes = Jim_Alloc(sizeof(struct JimExprNode) * (tokenlist->count - 1)); |
| 15032 | memset(builder.nodes, 0, sizeof(struct JimExprNode) * (tokenlist->count - 1)); |
| 15033 | builder.next = builder.nodes; |
| 15034 | Jim_InitStack(&builder.stack); |
| 15035 | |
| 15036 | rc = ExprTreeBuildTree(interp, &builder, 0, 0, 1); |
| 15037 | |
| 15038 | if (rc == JIM_OK) { |
| 15039 | top = Jim_StackPop(&builder.stack); |
| @@ -15043,11 +15890,11 @@ | |
| 15043 | rc = JIM_ERR; |
| 15044 | } |
| 15045 | } |
| 15046 | |
| 15047 | |
| 15048 | Jim_FreeStack(&builder.stack); |
| 15049 | |
| 15050 | if (rc != JIM_OK) { |
| 15051 | ExprTreeFreeNodes(interp, builder.nodes, builder.next - builder.nodes); |
| 15052 | return NULL; |
| 15053 | } |
| @@ -15078,10 +15925,13 @@ | |
| 15078 | fileNameObj = Jim_GetSourceInfo(interp, objPtr, &line); |
| 15079 | Jim_IncrRefCount(fileNameObj); |
| 15080 | |
| 15081 | exprText = Jim_GetString(objPtr, &exprTextLen); |
| 15082 | |
| 15083 | |
| 15084 | ScriptTokenListInit(&tokenlist); |
| 15085 | |
| 15086 | JimParserInit(&parser, exprText, exprTextLen, line); |
| 15087 | while (!parser.eof) { |
| @@ -15144,10 +15994,13 @@ | |
| 15144 | |
| 15145 | Jim_DecrRefCount(interp, fileNameObj); |
| 15146 | Jim_FreeIntRep(interp, objPtr); |
| 15147 | Jim_SetIntRepPtr(objPtr, expr); |
| 15148 | objPtr->typePtr = &exprObjType; |
| 15149 | return rc; |
| 15150 | } |
| 15151 | |
| 15152 | static struct ExprTree *JimGetExpression(Jim_Interp *interp, Jim_Obj *objPtr) |
| 15153 | { |
| @@ -15228,10 +16081,11 @@ | |
| 15228 | |
| 15229 | return JIM_ERR; |
| 15230 | } |
| 15231 | } |
| 15232 | } |
| 15233 | |
| 15234 | static int JimExprGetTerm(Jim_Interp *interp, struct JimExprNode *node, Jim_Obj **objPtrPtr) |
| 15235 | { |
| 15236 | int rc = JimExprEvalTermNode(interp, node); |
| 15237 | if (rc == JIM_OK) { |
| @@ -15238,10 +16092,11 @@ | |
| 15238 | *objPtrPtr = Jim_GetResult(interp); |
| 15239 | Jim_IncrRefCount(*objPtrPtr); |
| 15240 | } |
| 15241 | return rc; |
| 15242 | } |
| 15243 | |
| 15244 | static int JimExprGetTermBoolean(Jim_Interp *interp, struct JimExprNode *node) |
| 15245 | { |
| 15246 | if (JimExprEvalTermNode(interp, node) == JIM_OK) { |
| 15247 | return ExprBool(interp, Jim_GetResult(interp)); |
| @@ -15418,10 +16273,11 @@ | |
| 15418 | JIM_NOTUSED(interp); |
| 15419 | memcpy(newVec, srcPtr->internalRep.ptr, size); |
| 15420 | dupPtr->internalRep.ptr = newVec; |
| 15421 | dupPtr->typePtr = &scanFmtStringObjType; |
| 15422 | } |
| 15423 | |
| 15424 | static void UpdateStringOfScanFmt(Jim_Obj *objPtr) |
| 15425 | { |
| 15426 | JimSetStringBytes(objPtr, ((ScanFmtStringObj *) objPtr->internalRep.ptr)->stringRep); |
| 15427 | } |
| @@ -15920,14 +16776,10 @@ | |
| 15920 | static int Jim_IncrCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 15921 | { |
| 15922 | jim_wide wideValue, increment = 1; |
| 15923 | Jim_Obj *intObjPtr; |
| 15924 | |
| 15925 | if (argc != 2 && argc != 3) { |
| 15926 | Jim_WrongNumArgs(interp, 1, argv, "varName ?increment?"); |
| 15927 | return JIM_ERR; |
| 15928 | } |
| 15929 | if (argc == 3) { |
| 15930 | if (Jim_GetWideExpr(interp, argv[2], &increment) != JIM_OK) |
| 15931 | return JIM_ERR; |
| 15932 | } |
| 15933 | intObjPtr = Jim_GetVariable(interp, argv[1], JIM_UNSHARED); |
| @@ -15939,11 +16791,10 @@ | |
| 15939 | return JIM_ERR; |
| 15940 | } |
| 15941 | if (!intObjPtr || Jim_IsShared(intObjPtr)) { |
| 15942 | intObjPtr = Jim_NewIntObj(interp, wideValue + increment); |
| 15943 | if (Jim_SetVariable(interp, argv[1], intObjPtr) != JIM_OK) { |
| 15944 | Jim_FreeNewObj(interp, intObjPtr); |
| 15945 | return JIM_ERR; |
| 15946 | } |
| 15947 | } |
| 15948 | else { |
| 15949 | |
| @@ -16026,10 +16877,11 @@ | |
| 16026 | retcode = Jim_EvalObjPrefix(interp, interp->unknown, argc, argv); |
| 16027 | interp->unknown_called--; |
| 16028 | |
| 16029 | return retcode; |
| 16030 | } |
| 16031 | |
| 16032 | static void JimPushEvalFrame(Jim_Interp *interp, Jim_EvalFrame *frame, Jim_Obj *scriptObj) |
| 16033 | { |
| 16034 | memset(frame, 0, sizeof(*frame)); |
| 16035 | frame->parent = interp->evalFrame; |
| @@ -16047,10 +16899,11 @@ | |
| 16047 | if (frame->scriptObj) { |
| 16048 | printf("script: %.*s\n", 20, Jim_String(frame->scriptObj)); |
| 16049 | } |
| 16050 | #endif |
| 16051 | } |
| 16052 | |
| 16053 | static void JimPopEvalFrame(Jim_Interp *interp) |
| 16054 | { |
| 16055 | interp->evalFrame = interp->evalFrame->parent; |
| 16056 | } |
| @@ -16094,16 +16947,21 @@ | |
| 16094 | |
| 16095 | if (!interp->traceCmdObj || |
| 16096 | (retcode = JimTraceCallback(interp, "cmd", objc, objv)) == JIM_OK) { |
| 16097 | |
| 16098 | Jim_SetEmptyResult(interp); |
| 16099 | if (cmdPtr->isproc) { |
| 16100 | retcode = JimCallProcedure(interp, cmdPtr, objc, objv); |
| 16101 | } |
| 16102 | else { |
| 16103 | interp->cmdPrivData = cmdPtr->u.native.privData; |
| 16104 | retcode = cmdPtr->u.native.cmdProc(interp, objc, objv); |
| 16105 | } |
| 16106 | if (retcode == JIM_ERR) { |
| 16107 | JimSetErrorStack(interp, NULL); |
| 16108 | } |
| 16109 | } |
| @@ -16184,10 +17042,11 @@ | |
| 16184 | memcpy(&nargv[1], &objv[0], sizeof(nargv[0]) * objc); |
| 16185 | ret = Jim_EvalObjVector(interp, objc + 1, nargv); |
| 16186 | Jim_Free(nargv); |
| 16187 | return ret; |
| 16188 | } |
| 16189 | |
| 16190 | static int JimSubstOneToken(Jim_Interp *interp, const ScriptToken *token, Jim_Obj **objPtrPtr) |
| 16191 | { |
| 16192 | Jim_Obj *objPtr; |
| 16193 | int ret = JIM_ERR; |
| @@ -16239,10 +17098,12 @@ | |
| 16239 | int totlen = 0, i; |
| 16240 | Jim_Obj **intv; |
| 16241 | Jim_Obj *sintv[JIM_EVAL_SINTV_LEN]; |
| 16242 | Jim_Obj *objPtr; |
| 16243 | char *s; |
| 16244 | |
| 16245 | if (tokens <= JIM_EVAL_SINTV_LEN) |
| 16246 | intv = sintv; |
| 16247 | else |
| 16248 | intv = Jim_Alloc(sizeof(Jim_Obj *) * tokens); |
| @@ -16256,28 +17117,34 @@ | |
| 16256 | if (flags & JIM_SUBST_FLAG) { |
| 16257 | |
| 16258 | tokens = i; |
| 16259 | continue; |
| 16260 | } |
| 16261 | |
| 16262 | |
| 16263 | case JIM_CONTINUE: |
| 16264 | if (flags & JIM_SUBST_FLAG) { |
| 16265 | intv[i] = NULL; |
| 16266 | continue; |
| 16267 | } |
| 16268 | |
| 16269 | |
| 16270 | default: |
| 16271 | while (i--) { |
| 16272 | Jim_DecrRefCount(interp, intv[i]); |
| 16273 | } |
| 16274 | if (intv != sintv) { |
| 16275 | Jim_Free(intv); |
| 16276 | } |
| 16277 | return NULL; |
| 16278 | } |
| 16279 | Jim_IncrRefCount(intv[i]); |
| 16280 | Jim_String(intv[i]); |
| 16281 | totlen += intv[i]->length; |
| 16282 | } |
| 16283 | |
| @@ -16287,10 +17154,11 @@ | |
| 16287 | intv[0]->refCount--; |
| 16288 | return intv[0]; |
| 16289 | } |
| 16290 | |
| 16291 | objPtr = Jim_NewStringObjNoAlloc(interp, NULL, 0); |
| 16292 | |
| 16293 | if (tokens == 4 && token[0].type == JIM_TT_ESC && token[1].type == JIM_TT_ESC |
| 16294 | && token[2].type == JIM_TT_VAR) { |
| 16295 | |
| 16296 | objPtr->typePtr = &interpolatedObjType; |
| @@ -16322,10 +17190,111 @@ | |
| 16322 | } |
| 16323 | |
| 16324 | return objPtr; |
| 16325 | } |
| 16326 | |
| 16327 | |
| 16328 | static int JimEvalObjList(Jim_Interp *interp, Jim_Obj *listPtr) |
| 16329 | { |
| 16330 | int retcode = JIM_OK; |
| 16331 | Jim_EvalFrame frame; |
| @@ -16383,11 +17352,11 @@ | |
| 16383 | Jim_DecrRefCount(interp, scriptObjPtr); |
| 16384 | return JIM_OK; |
| 16385 | } |
| 16386 | if (script->len == 3 |
| 16387 | && token[1].objPtr->typePtr == &commandObjType |
| 16388 | && token[1].objPtr->internalRep.cmdValue.cmdPtr->isproc == 0 |
| 16389 | && token[1].objPtr->internalRep.cmdValue.cmdPtr->u.native.cmdProc == Jim_IncrCoreCommand |
| 16390 | && token[2].objPtr->typePtr == &variableObjType) { |
| 16391 | |
| 16392 | Jim_Obj *objPtr = Jim_GetVariable(interp, token[2].objPtr, JIM_NONE); |
| 16393 | |
| @@ -16404,11 +17373,11 @@ | |
| 16404 | script->inUse++; |
| 16405 | |
| 16406 | JimPushEvalFrame(interp, &frame, scriptObjPtr); |
| 16407 | |
| 16408 | |
| 16409 | interp->errorFlag = 0; |
| 16410 | argv = sargv; |
| 16411 | |
| 16412 | for (i = 0; i < script->len && retcode == JIM_OK; ) { |
| 16413 | int argc; |
| 16414 | int j; |
| @@ -16520,10 +17489,11 @@ | |
| 16520 | } |
| 16521 | |
| 16522 | if (retcode == JIM_OK && argc) { |
| 16523 | |
| 16524 | retcode = JimInvokeCommand(interp, argc, argv); |
| 16525 | |
| 16526 | if (Jim_CheckSignal(interp)) { |
| 16527 | retcode = JIM_SIGNAL; |
| 16528 | } |
| 16529 | } |
| @@ -16551,10 +17521,11 @@ | |
| 16551 | Jim_SetIntRepPtr(scriptObjPtr, script); |
| 16552 | Jim_DecrRefCount(interp, scriptObjPtr); |
| 16553 | |
| 16554 | return retcode; |
| 16555 | } |
| 16556 | |
| 16557 | static int JimSetProcArg(Jim_Interp *interp, Jim_Obj *argNameObj, Jim_Obj *argValObj) |
| 16558 | { |
| 16559 | int retcode; |
| 16560 | |
| @@ -16580,48 +17551,63 @@ | |
| 16580 | else { |
| 16581 | retcode = Jim_SetVariable(interp, argNameObj, argValObj); |
| 16582 | } |
| 16583 | return retcode; |
| 16584 | } |
| 16585 | |
| 16586 | static void JimSetProcWrongArgs(Jim_Interp *interp, Jim_Obj *procNameObj, Jim_Cmd *cmd) |
| 16587 | { |
| 16588 | |
| 16589 | Jim_Obj *argmsg = Jim_NewStringObj(interp, "", 0); |
| 16590 | int i; |
| 16591 | |
| 16592 | for (i = 0; i < cmd->u.proc.argListLen; i++) { |
| 16593 | Jim_AppendString(interp, argmsg, " ", 1); |
| 16594 | |
| 16595 | if (i == cmd->u.proc.argsPos) { |
| 16596 | if (cmd->u.proc.arglist[i].defaultObjPtr) { |
| 16597 | |
| 16598 | Jim_AppendString(interp, argmsg, "?", 1); |
| 16599 | Jim_AppendObj(interp, argmsg, cmd->u.proc.arglist[i].defaultObjPtr); |
| 16600 | Jim_AppendString(interp, argmsg, " ...?", -1); |
| 16601 | } |
| 16602 | else { |
| 16603 | |
| 16604 | Jim_AppendString(interp, argmsg, "?arg ...?", -1); |
| 16605 | } |
| 16606 | } |
| 16607 | else { |
| 16608 | if (cmd->u.proc.arglist[i].defaultObjPtr) { |
| 16609 | Jim_AppendString(interp, argmsg, "?", 1); |
| 16610 | Jim_AppendObj(interp, argmsg, cmd->u.proc.arglist[i].nameObjPtr); |
| 16611 | Jim_AppendString(interp, argmsg, "?", 1); |
| 16612 | } |
| 16613 | else { |
| 16614 | const char *arg = Jim_String(cmd->u.proc.arglist[i].nameObjPtr); |
| 16615 | if (*arg == '&') { |
| 16616 | arg++; |
| 16617 | } |
| 16618 | Jim_AppendString(interp, argmsg, arg, -1); |
| 16619 | } |
| 16620 | } |
| 16621 | } |
| 16622 | Jim_SetResultFormatted(interp, "wrong # args: should be \"%#s%#s\"", procNameObj, argmsg); |
| 16623 | } |
| 16624 | |
| 16625 | #ifdef jim_ext_namespace |
| 16626 | int Jim_EvalNamespace(Jim_Interp *interp, Jim_Obj *scriptObj, Jim_Obj *nsObj) |
| 16627 | { |
| @@ -16758,10 +17744,40 @@ | |
| 16758 | } |
| 16759 | interp->procLevel--; |
| 16760 | |
| 16761 | return retcode; |
| 16762 | } |
| 16763 | |
| 16764 | int Jim_EvalSource(Jim_Interp *interp, const char *filename, int lineno, const char *script) |
| 16765 | { |
| 16766 | int retval; |
| 16767 | Jim_Obj *scriptObjPtr; |
| @@ -16805,56 +17821,43 @@ | |
| 16805 | |
| 16806 | return retval; |
| 16807 | } |
| 16808 | |
| 16809 | #include <sys/stat.h> |
| 16810 | |
| 16811 | static Jim_Obj *JimReadTextFile(Jim_Interp *interp, const char *filename) |
| 16812 | { |
| 16813 | jim_stat_t sb; |
| 16814 | int fd; |
| 16815 | char *buf; |
| 16816 | int readlen; |
| 16817 | |
| 16818 | if (Jim_Stat(filename, &sb) == -1 || (fd = open(filename, O_RDONLY | O_TEXT, 0666)) < 0) { |
| 16819 | Jim_SetResultFormatted(interp, "couldn't read file \"%s\": %s", filename, strerror(errno)); |
| 16820 | return NULL; |
| 16821 | } |
| 16822 | buf = Jim_Alloc(sb.st_size + 1); |
| 16823 | readlen = read(fd, buf, sb.st_size); |
| 16824 | close(fd); |
| 16825 | if (readlen < 0) { |
| 16826 | Jim_Free(buf); |
| 16827 | Jim_SetResultFormatted(interp, "failed to load file \"%s\": %s", filename, strerror(errno)); |
| 16828 | return NULL; |
| 16829 | } |
| 16830 | else { |
| 16831 | Jim_Obj *objPtr; |
| 16832 | buf[readlen] = 0; |
| 16833 | |
| 16834 | objPtr = Jim_NewStringObjNoAlloc(interp, buf, readlen); |
| 16835 | |
| 16836 | return objPtr; |
| 16837 | } |
| 16838 | } |
| 16839 | |
| 16840 | |
| 16841 | int Jim_EvalFile(Jim_Interp *interp, const char *filename) |
| 16842 | { |
| 16843 | Jim_Obj *filenameObj; |
| 16844 | Jim_Obj *oldFilenameObj; |
| 16845 | Jim_Obj *scriptObjPtr; |
| 16846 | int retcode; |
| 16847 | |
| 16848 | scriptObjPtr = JimReadTextFile(interp, filename); |
| 16849 | if (!scriptObjPtr) { |
| 16850 | return JIM_ERR; |
| 16851 | } |
| 16852 | |
| 16853 | filenameObj = Jim_NewStringObj(interp, filename, -1); |
| 16854 | Jim_SetSourceInfo(interp, scriptObjPtr, filenameObj, 1); |
| 16855 | |
| 16856 | oldFilenameObj = JimPushInterpObj(interp->currentFilenameObj, filenameObj); |
| 16857 | |
| 16858 | retcode = Jim_EvalObj(interp, scriptObjPtr); |
| 16859 | |
| 16860 | JimPopInterpObj(interp, interp->currentFilenameObj, oldFilenameObj); |
| @@ -16920,10 +17923,11 @@ | |
| 16920 | int scriptTextLen; |
| 16921 | const char *scriptText = Jim_GetString(objPtr, &scriptTextLen); |
| 16922 | struct JimParserCtx parser; |
| 16923 | struct ScriptObj *script = Jim_Alloc(sizeof(*script)); |
| 16924 | ParseTokenList tokenlist; |
| 16925 | |
| 16926 | |
| 16927 | ScriptTokenListInit(&tokenlist); |
| 16928 | |
| 16929 | JimParserInit(&parser, scriptText, scriptTextLen, 1); |
| @@ -16935,10 +17939,13 @@ | |
| 16935 | } |
| 16936 | ScriptAddToken(&tokenlist, parser.tstart, parser.tend - parser.tstart + 1, parser.tt, |
| 16937 | parser.tline); |
| 16938 | } |
| 16939 | |
| 16940 | |
| 16941 | script->inUse = 1; |
| 16942 | script->substFlags = flags; |
| 16943 | script->fileNameObj = interp->emptyObj; |
| 16944 | Jim_IncrRefCount(script->fileNameObj); |
| @@ -16961,10 +17968,11 @@ | |
| 16961 | |
| 16962 | |
| 16963 | Jim_FreeIntRep(interp, objPtr); |
| 16964 | Jim_SetIntRepPtr(objPtr, script); |
| 16965 | objPtr->typePtr = &scriptObjType; |
| 16966 | return JIM_OK; |
| 16967 | } |
| 16968 | |
| 16969 | static ScriptObj *Jim_GetSubst(Jim_Interp *interp, Jim_Obj *objPtr, int flags) |
| 16970 | { |
| @@ -16992,27 +18000,59 @@ | |
| 16992 | return JIM_ERR; |
| 16993 | } |
| 16994 | return JIM_OK; |
| 16995 | } |
| 16996 | |
| 16997 | void Jim_WrongNumArgs(Jim_Interp *interp, int argc, Jim_Obj *const *argv, const char *msg) |
| 16998 | { |
| 16999 | Jim_Obj *objPtr; |
| 17000 | Jim_Obj *listObjPtr; |
| 17001 | |
| 17002 | JimPanic((argc == 0, "Jim_WrongNumArgs() called with argc=0")); |
| 17003 | |
| 17004 | listObjPtr = Jim_NewListObj(interp, argv, argc); |
| 17005 | |
| 17006 | if (msg && *msg) { |
| 17007 | Jim_ListAppendElement(interp, listObjPtr, Jim_NewStringObj(interp, msg, -1)); |
| 17008 | } |
| 17009 | Jim_IncrRefCount(listObjPtr); |
| 17010 | objPtr = Jim_ListJoin(interp, listObjPtr, " ", 1); |
| 17011 | Jim_DecrRefCount(interp, listObjPtr); |
| 17012 | |
| 17013 | Jim_SetResultFormatted(interp, "wrong # args: should be \"%#s\"", objPtr); |
| 17014 | } |
| 17015 | |
| 17016 | typedef void JimHashtableIteratorCallbackType(Jim_Interp *interp, Jim_Obj *listObjPtr, |
| 17017 | Jim_Obj *keyObjPtr, void *value, Jim_Obj *patternObjPtr, int type); |
| 17018 | |
| @@ -17042,42 +18082,55 @@ | |
| 17042 | } |
| 17043 | return listObjPtr; |
| 17044 | } |
| 17045 | |
| 17046 | |
| 17047 | #define JIM_CMDLIST_COMMANDS 0 |
| 17048 | #define JIM_CMDLIST_PROCS 1 |
| 17049 | #define JIM_CMDLIST_CHANNELS 2 |
| 17050 | |
| 17051 | static void JimCommandMatch(Jim_Interp *interp, Jim_Obj *listObjPtr, |
| 17052 | Jim_Obj *keyObj, void *value, Jim_Obj *patternObj, int type) |
| 17053 | { |
| 17054 | Jim_Cmd *cmdPtr = (Jim_Cmd *)value; |
| 17055 | |
| 17056 | if (type == JIM_CMDLIST_PROCS && !cmdPtr->isproc) { |
| 17057 | |
| 17058 | return; |
| 17059 | } |
| 17060 | |
| 17061 | Jim_IncrRefCount(keyObj); |
| 17062 | |
| 17063 | if (type != JIM_CMDLIST_CHANNELS || Jim_AioFilehandle(interp, keyObj) >= 0) { |
| 17064 | int match = 1; |
| 17065 | if (patternObj) { |
| 17066 | int plen, slen; |
| 17067 | const char *pattern = Jim_GetStringNoQualifier(patternObj, &plen); |
| 17068 | const char *str = Jim_GetStringNoQualifier(keyObj, &slen); |
| 17069 | #ifdef JIM_NO_INTROSPECTION |
| 17070 | |
| 17071 | match = (JimStringCompareUtf8(pattern, plen, str, slen, 0) == 0); |
| 17072 | #else |
| 17073 | match = JimGlobMatch(pattern, plen, str, slen, 0); |
| 17074 | #endif |
| 17075 | } |
| 17076 | if (match) { |
| 17077 | Jim_ListAppendElement(interp, listObjPtr, keyObj); |
| 17078 | } |
| 17079 | } |
| 17080 | Jim_DecrRefCount(interp, keyObj); |
| 17081 | } |
| 17082 | |
| 17083 | static Jim_Obj *JimCommandsList(Jim_Interp *interp, Jim_Obj *patternObjPtr, int type) |
| @@ -17084,23 +18137,22 @@ | |
| 17084 | { |
| 17085 | return JimHashtablePatternMatch(interp, &interp->commands, patternObjPtr, JimCommandMatch, type); |
| 17086 | } |
| 17087 | |
| 17088 | |
| 17089 | #define JIM_VARLIST_GLOBALS 0 |
| 17090 | #define JIM_VARLIST_LOCALS 1 |
| 17091 | #define JIM_VARLIST_VARS 2 |
| 17092 | #define JIM_VARLIST_MASK 0x000f |
| 17093 | |
| 17094 | #define JIM_VARLIST_VALUES 0x1000 |
| 17095 | |
| 17096 | static void JimVariablesMatch(Jim_Interp *interp, Jim_Obj *listObjPtr, |
| 17097 | Jim_Obj *keyObj, void *value, Jim_Obj *patternObj, int type) |
| 17098 | { |
| 17099 | Jim_VarVal *vv = (Jim_VarVal *)value; |
| 17100 | |
| 17101 | if ((type & JIM_VARLIST_MASK) != JIM_VARLIST_LOCALS || vv->linkFramePtr == NULL) { |
| 17102 | if (patternObj == NULL || Jim_StringMatchObj(interp, patternObj, keyObj, 0)) { |
| 17103 | Jim_ListAppendElement(interp, listObjPtr, keyObj); |
| 17104 | if (type & JIM_VARLIST_VALUES) { |
| 17105 | Jim_ListAppendElement(interp, listObjPtr, vv->objPtr); |
| 17106 | } |
| @@ -17109,19 +18161,20 @@ | |
| 17109 | } |
| 17110 | |
| 17111 | |
| 17112 | static Jim_Obj *JimVariablesList(Jim_Interp *interp, Jim_Obj *patternObjPtr, int mode) |
| 17113 | { |
| 17114 | if (mode == JIM_VARLIST_LOCALS && interp->framePtr == interp->topFramePtr) { |
| 17115 | return interp->emptyObj; |
| 17116 | } |
| 17117 | else { |
| 17118 | Jim_CallFrame *framePtr = (mode == JIM_VARLIST_GLOBALS) ? interp->topFramePtr : interp->framePtr; |
| 17119 | return JimHashtablePatternMatch(interp, &framePtr->vars, patternObjPtr, JimVariablesMatch, |
| 17120 | mode); |
| 17121 | } |
| 17122 | } |
| 17123 | |
| 17124 | static int JimInfoLevel(Jim_Interp *interp, Jim_Obj *levelObjPtr, Jim_Obj **objPtrPtr) |
| 17125 | { |
| 17126 | long level; |
| 17127 | |
| @@ -17138,10 +18191,11 @@ | |
| 17138 | } |
| 17139 | } |
| 17140 | Jim_SetResultFormatted(interp, "bad level \"%#s\"", levelObjPtr); |
| 17141 | return JIM_ERR; |
| 17142 | } |
| 17143 | |
| 17144 | static int JimInfoFrame(Jim_Interp *interp, Jim_Obj *levelObjPtr, Jim_Obj **objPtrPtr) |
| 17145 | { |
| 17146 | long level; |
| 17147 | |
| @@ -17186,14 +18240,10 @@ | |
| 17186 | } |
| 17187 | |
| 17188 | |
| 17189 | static int Jim_PutsCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 17190 | { |
| 17191 | if (argc != 2 && argc != 3) { |
| 17192 | Jim_WrongNumArgs(interp, 1, argv, "?-nonewline? string"); |
| 17193 | return JIM_ERR; |
| 17194 | } |
| 17195 | if (argc == 3) { |
| 17196 | if (!Jim_CompareStringImmediate(interp, argv[1], "-nonewline")) { |
| 17197 | Jim_SetResultString(interp, "The second argument must " "be -nonewline", -1); |
| 17198 | return JIM_ERR; |
| 17199 | } |
| @@ -17245,15 +18295,11 @@ | |
| 17245 | { |
| 17246 | jim_wide wideValue, res = 0; |
| 17247 | double doubleValue, doubleRes = 0; |
| 17248 | int i = 2; |
| 17249 | |
| 17250 | if (argc < 2) { |
| 17251 | Jim_WrongNumArgs(interp, 1, argv, "number ?number ... number?"); |
| 17252 | return JIM_ERR; |
| 17253 | } |
| 17254 | else if (argc == 2) { |
| 17255 | if (Jim_GetWide(interp, argv[1], &wideValue) != JIM_OK) { |
| 17256 | if (Jim_GetDouble(interp, argv[1], &doubleValue) != JIM_OK) { |
| 17257 | return JIM_ERR; |
| 17258 | } |
| 17259 | else { |
| @@ -17342,14 +18388,10 @@ | |
| 17342 | } |
| 17343 | |
| 17344 | |
| 17345 | static int Jim_SetCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 17346 | { |
| 17347 | if (argc != 2 && argc != 3) { |
| 17348 | Jim_WrongNumArgs(interp, 1, argv, "varName ?newValue?"); |
| 17349 | return JIM_ERR; |
| 17350 | } |
| 17351 | if (argc == 2) { |
| 17352 | Jim_Obj *objPtr; |
| 17353 | |
| 17354 | objPtr = Jim_GetVariable(interp, argv[1], JIM_ERRMSG); |
| 17355 | if (!objPtr) |
| @@ -17405,15 +18447,10 @@ | |
| 17405 | } |
| 17406 | |
| 17407 | |
| 17408 | static int Jim_WhileCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 17409 | { |
| 17410 | if (argc != 3) { |
| 17411 | Jim_WrongNumArgs(interp, 1, argv, "condition body"); |
| 17412 | return JIM_ERR; |
| 17413 | } |
| 17414 | |
| 17415 | |
| 17416 | while (1) { |
| 17417 | int boolean = 0, retval; |
| 17418 | |
| 17419 | if ((retval = Jim_GetBoolFromExpr(interp, argv[1], &boolean)) != JIM_OK) |
| @@ -17447,15 +18484,10 @@ | |
| 17447 | int boolean = 1; |
| 17448 | int immediate = 0; |
| 17449 | Jim_Obj *varNamePtr = NULL; |
| 17450 | Jim_Obj *stopVarNamePtr = NULL; |
| 17451 | |
| 17452 | if (argc != 5) { |
| 17453 | Jim_WrongNumArgs(interp, 1, argv, "start test next body"); |
| 17454 | return JIM_ERR; |
| 17455 | } |
| 17456 | |
| 17457 | |
| 17458 | if ((retval = Jim_EvalObj(interp, argv[1])) != JIM_OK) { |
| 17459 | return retval; |
| 17460 | } |
| 17461 | |
| @@ -17623,19 +18655,14 @@ | |
| 17623 | |
| 17624 | static int Jim_LoopCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 17625 | { |
| 17626 | int retval; |
| 17627 | jim_wide i; |
| 17628 | jim_wide limit; |
| 17629 | jim_wide incr = 1; |
| 17630 | Jim_Obj *bodyObjPtr; |
| 17631 | |
| 17632 | if (argc < 4 || argc > 6) { |
| 17633 | Jim_WrongNumArgs(interp, 1, argv, "var ?first? limit ?incr? body"); |
| 17634 | return JIM_ERR; |
| 17635 | } |
| 17636 | |
| 17637 | retval = Jim_GetWideExpr(interp, argv[2], &i); |
| 17638 | if (argc > 4 && retval == JIM_OK) { |
| 17639 | retval = Jim_GetWideExpr(interp, argv[3], &limit); |
| 17640 | } |
| 17641 | if (argc > 5 && retval == JIM_OK) { |
| @@ -17682,13 +18709,10 @@ | |
| 17682 | } |
| 17683 | } |
| 17684 | else { |
| 17685 | objPtr = Jim_NewIntObj(interp, i); |
| 17686 | retval = Jim_SetVariable(interp, argv[1], objPtr); |
| 17687 | if (retval != JIM_OK) { |
| 17688 | Jim_FreeNewObj(interp, objPtr); |
| 17689 | } |
| 17690 | } |
| 17691 | } |
| 17692 | } |
| 17693 | |
| 17694 | if (retval == JIM_OK || retval == JIM_CONTINUE || retval == JIM_BREAK) { |
| @@ -17730,13 +18754,12 @@ | |
| 17730 | Jim_ListIter twoiters[2]; |
| 17731 | Jim_ListIter *iters; |
| 17732 | Jim_Obj *script; |
| 17733 | Jim_Obj *resultObj; |
| 17734 | |
| 17735 | if (argc < 4 || argc % 2 != 0) { |
| 17736 | Jim_WrongNumArgs(interp, 1, argv, "varList list ?varList list ...? script"); |
| 17737 | return JIM_ERR; |
| 17738 | } |
| 17739 | script = argv[argc - 1]; |
| 17740 | numargs = (argc - 1 - 1); |
| 17741 | |
| 17742 | if (numargs == 2) { |
| @@ -17786,14 +18809,14 @@ | |
| 17786 | Jim_Obj *valObj = JimListIterNext(interp, &iters[i + 1]); |
| 17787 | if (!valObj) { |
| 17788 | |
| 17789 | valObj = interp->emptyObj; |
| 17790 | } |
| 17791 | |
| 17792 | Jim_IncrRefCount(valObj); |
| 17793 | result = Jim_SetVariable(interp, varName, valObj); |
| 17794 | Jim_DecrRefCount(interp, valObj); |
| 17795 | if (result != JIM_OK) { |
| 17796 | goto err; |
| 17797 | } |
| 17798 | } |
| 17799 | } |
| @@ -17845,15 +18868,10 @@ | |
| 17845 | int result = JIM_ERR; |
| 17846 | int i; |
| 17847 | Jim_ListIter iter; |
| 17848 | Jim_Obj *resultObj; |
| 17849 | |
| 17850 | if (argc < 2) { |
| 17851 | Jim_WrongNumArgs(interp, 1, argv, "varList list ?varName ...?"); |
| 17852 | return JIM_ERR; |
| 17853 | } |
| 17854 | |
| 17855 | JimListIterInit(&iter, argv[1]); |
| 17856 | |
| 17857 | for (i = 2; i < argc; i++) { |
| 17858 | Jim_Obj *valObj = JimListIterNext(interp, &iter); |
| 17859 | result = Jim_SetVariable(interp, argv[i], valObj ? valObj : interp->emptyObj); |
| @@ -17875,56 +18893,56 @@ | |
| 17875 | |
| 17876 | static int Jim_IfCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 17877 | { |
| 17878 | int boolean, retval, current = 1, falsebody = 0; |
| 17879 | |
| 17880 | if (argc >= 3) { |
| 17881 | while (1) { |
| 17882 | |
| 17883 | if (current >= argc) |
| 17884 | goto err; |
| 17885 | if ((retval = Jim_GetBoolFromExpr(interp, argv[current++], &boolean)) |
| 17886 | != JIM_OK) |
| 17887 | return retval; |
| 17888 | |
| 17889 | if (current >= argc) |
| 17890 | goto err; |
| 17891 | if (Jim_CompareStringImmediate(interp, argv[current], "then")) |
| 17892 | current++; |
| 17893 | |
| 17894 | if (current >= argc) |
| 17895 | goto err; |
| 17896 | if (boolean) |
| 17897 | return Jim_EvalObj(interp, argv[current]); |
| 17898 | |
| 17899 | if (++current >= argc) { |
| 17900 | Jim_SetResult(interp, Jim_NewEmptyStringObj(interp)); |
| 17901 | return JIM_OK; |
| 17902 | } |
| 17903 | falsebody = current++; |
| 17904 | if (Jim_CompareStringImmediate(interp, argv[falsebody], "else")) { |
| 17905 | |
| 17906 | if (current != argc - 1) |
| 17907 | goto err; |
| 17908 | return Jim_EvalObj(interp, argv[current]); |
| 17909 | } |
| 17910 | else if (Jim_CompareStringImmediate(interp, argv[falsebody], "elseif")) |
| 17911 | continue; |
| 17912 | |
| 17913 | else if (falsebody != argc - 1) |
| 17914 | goto err; |
| 17915 | return Jim_EvalObj(interp, argv[falsebody]); |
| 17916 | } |
| 17917 | return JIM_OK; |
| 17918 | } |
| 17919 | err: |
| 17920 | Jim_WrongNumArgs(interp, 1, argv, "condition ?then? trueBody ?elseif ...? ?else? falseBody"); |
| 17921 | return JIM_ERR; |
| 17922 | } |
| 17923 | |
| 17924 | |
| 17925 | int Jim_CommandMatchObj(Jim_Interp *interp, Jim_Obj *commandObj, Jim_Obj *patternObj, |
| 17926 | Jim_Obj *stringObj, int flags) |
| 17927 | { |
| 17928 | Jim_Obj *parms[5]; |
| 17929 | int argc = 0; |
| 17930 | long eq; |
| @@ -17956,16 +18974,10 @@ | |
| 17956 | int matchOpt = SWITCH_EXACT, opt = 1, patCount, i; |
| 17957 | int match_flags = 0; |
| 17958 | Jim_Obj *command = NULL, *scriptObj = NULL, *strObj; |
| 17959 | Jim_Obj **caseList; |
| 17960 | |
| 17961 | if (argc < 3) { |
| 17962 | wrongnumargs: |
| 17963 | Jim_WrongNumArgs(interp, 1, argv, "?options? string " |
| 17964 | "pattern body ... ?default body? or " "{pattern body ?pattern body ...?}"); |
| 17965 | return JIM_ERR; |
| 17966 | } |
| 17967 | for (opt = 1; opt < argc; ++opt) { |
| 17968 | const char *option = Jim_String(argv[opt]); |
| 17969 | |
| 17970 | if (*option != '-') |
| 17971 | break; |
| @@ -17982,31 +18994,31 @@ | |
| 17982 | match_flags |= JIM_OPT_END; |
| 17983 | } |
| 17984 | else if (strncmp(option, "-command", 2) == 0) { |
| 17985 | matchOpt = SWITCH_CMD; |
| 17986 | if ((argc - opt) < 2) |
| 17987 | goto wrongnumargs; |
| 17988 | command = argv[++opt]; |
| 17989 | } |
| 17990 | else { |
| 17991 | Jim_SetResultFormatted(interp, |
| 17992 | "bad option \"%#s\": must be -exact, -glob, -regexp, -command procname or --", |
| 17993 | argv[opt]); |
| 17994 | return JIM_ERR; |
| 17995 | } |
| 17996 | if ((argc - opt) < 2) |
| 17997 | goto wrongnumargs; |
| 17998 | } |
| 17999 | strObj = argv[opt++]; |
| 18000 | patCount = argc - opt; |
| 18001 | if (patCount == 1) { |
| 18002 | JimListGetElements(interp, argv[opt], &patCount, &caseList); |
| 18003 | } |
| 18004 | else |
| 18005 | caseList = (Jim_Obj **)&argv[opt]; |
| 18006 | if (patCount == 0 || patCount % 2 != 0) |
| 18007 | goto wrongnumargs; |
| 18008 | for (i = 0; scriptObj == NULL && i < patCount; i += 2) { |
| 18009 | Jim_Obj *patObj = caseList[i]; |
| 18010 | |
| 18011 | if (!Jim_CompareStringImmediate(interp, patObj, "default") |
| 18012 | || i < (patCount - 2)) { |
| @@ -18021,11 +19033,11 @@ | |
| 18021 | break; |
| 18022 | case SWITCH_RE: |
| 18023 | command = Jim_NewStringObj(interp, "regexp", -1); |
| 18024 | |
| 18025 | case SWITCH_CMD:{ |
| 18026 | int rc = Jim_CommandMatchObj(interp, command, patObj, strObj, match_flags); |
| 18027 | |
| 18028 | if (argc - opt == 1) { |
| 18029 | JimListGetElements(interp, argv[opt], &patCount, &caseList); |
| 18030 | } |
| 18031 | |
| @@ -18069,14 +19081,10 @@ | |
| 18069 | static int Jim_LindexCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18070 | { |
| 18071 | Jim_Obj *objPtr; |
| 18072 | int ret; |
| 18073 | |
| 18074 | if (argc < 2) { |
| 18075 | Jim_WrongNumArgs(interp, 1, argv, "list ?index ...?"); |
| 18076 | return JIM_ERR; |
| 18077 | } |
| 18078 | ret = Jim_ListIndices(interp, argv[1], argv + 2, argc - 2, &objPtr, JIM_NONE); |
| 18079 | if (ret < 0) { |
| 18080 | ret = JIM_OK; |
| 18081 | Jim_SetEmptyResult(interp); |
| 18082 | } |
| @@ -18087,14 +19095,10 @@ | |
| 18087 | } |
| 18088 | |
| 18089 | |
| 18090 | static int Jim_LlengthCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18091 | { |
| 18092 | if (argc != 2) { |
| 18093 | Jim_WrongNumArgs(interp, 1, argv, "list"); |
| 18094 | return JIM_ERR; |
| 18095 | } |
| 18096 | Jim_SetResultInt(interp, Jim_ListLength(interp, argv[1])); |
| 18097 | return JIM_OK; |
| 18098 | } |
| 18099 | |
| 18100 | |
| @@ -18119,17 +19123,10 @@ | |
| 18119 | Jim_Obj *commandObj = NULL; |
| 18120 | Jim_Obj *indexObj = NULL; |
| 18121 | int match_flags = 0; |
| 18122 | long stride = 1; |
| 18123 | |
| 18124 | if (argc < 3) { |
| 18125 | wrongargs: |
| 18126 | Jim_WrongNumArgs(interp, 1, argv, |
| 18127 | "?-exact|-glob|-regexp|-command 'command'? ?-bool|-inline? ?-not? ?-nocase? ?-all? ?-stride len? ?-index val? list value"); |
| 18128 | return JIM_ERR; |
| 18129 | } |
| 18130 | |
| 18131 | for (i = 1; i < argc - 2; i++) { |
| 18132 | int option; |
| 18133 | |
| 18134 | if (Jim_GetEnum(interp, argv[i], options, &option, NULL, JIM_ERRMSG) != JIM_OK) { |
| 18135 | return JIM_ERR; |
| @@ -18156,27 +19153,27 @@ | |
| 18156 | opt_match = option; |
| 18157 | match_flags |= JIM_OPT_END; |
| 18158 | break; |
| 18159 | case OPT_COMMAND: |
| 18160 | if (i >= argc - 2) { |
| 18161 | goto wrongargs; |
| 18162 | } |
| 18163 | commandObj = argv[++i]; |
| 18164 | |
| 18165 | case OPT_EXACT: |
| 18166 | case OPT_GLOB: |
| 18167 | opt_match = option; |
| 18168 | break; |
| 18169 | case OPT_INDEX: |
| 18170 | if (i >= argc - 2) { |
| 18171 | goto wrongargs; |
| 18172 | } |
| 18173 | indexObj = argv[++i]; |
| 18174 | break; |
| 18175 | case OPT_STRIDE: |
| 18176 | if (i >= argc - 2) { |
| 18177 | goto wrongargs; |
| 18178 | } |
| 18179 | if (Jim_GetLong(interp, argv[++i], &stride) != JIM_OK) { |
| 18180 | return JIM_ERR; |
| 18181 | } |
| 18182 | if (stride < 1) { |
| @@ -18187,11 +19184,11 @@ | |
| 18187 | } |
| 18188 | } |
| 18189 | |
| 18190 | argc -= i; |
| 18191 | if (argc < 2) { |
| 18192 | goto wrongargs; |
| 18193 | } |
| 18194 | argv += i; |
| 18195 | |
| 18196 | listlen = Jim_ListLength(interp, argv[0]); |
| 18197 | if (listlen % stride) { |
| @@ -18250,11 +19247,11 @@ | |
| 18250 | eq = Jim_StringMatchObj(interp, argv[1], objPtr, match_flags); |
| 18251 | break; |
| 18252 | |
| 18253 | case OPT_REGEXP: |
| 18254 | case OPT_COMMAND: |
| 18255 | eq = Jim_CommandMatchObj(interp, commandObj, argv[1], objPtr, match_flags); |
| 18256 | if (eq < 0) { |
| 18257 | Jim_DecrRefCount(interp, searchListObj); |
| 18258 | rc = JIM_ERR; |
| 18259 | goto done; |
| 18260 | } |
| @@ -18326,32 +19323,23 @@ | |
| 18326 | |
| 18327 | |
| 18328 | static int Jim_LappendCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18329 | { |
| 18330 | Jim_Obj *listObjPtr; |
| 18331 | int new_obj = 0; |
| 18332 | int i; |
| 18333 | |
| 18334 | if (argc < 2) { |
| 18335 | Jim_WrongNumArgs(interp, 1, argv, "varName ?value value ...?"); |
| 18336 | return JIM_ERR; |
| 18337 | } |
| 18338 | listObjPtr = Jim_GetVariable(interp, argv[1], JIM_UNSHARED); |
| 18339 | if (!listObjPtr) { |
| 18340 | |
| 18341 | listObjPtr = Jim_NewListObj(interp, NULL, 0); |
| 18342 | new_obj = 1; |
| 18343 | } |
| 18344 | else if (Jim_IsShared(listObjPtr)) { |
| 18345 | listObjPtr = Jim_DuplicateObj(interp, listObjPtr); |
| 18346 | new_obj = 1; |
| 18347 | } |
| 18348 | for (i = 2; i < argc; i++) |
| 18349 | Jim_ListAppendElement(interp, listObjPtr, argv[i]); |
| 18350 | if (Jim_SetVariable(interp, argv[1], listObjPtr) != JIM_OK) { |
| 18351 | if (new_obj) |
| 18352 | Jim_FreeNewObj(interp, listObjPtr); |
| 18353 | return JIM_ERR; |
| 18354 | } |
| 18355 | Jim_SetResult(interp, listObjPtr); |
| 18356 | return JIM_OK; |
| 18357 | } |
| @@ -18360,14 +19348,10 @@ | |
| 18360 | static int Jim_LinsertCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18361 | { |
| 18362 | int idx, len; |
| 18363 | Jim_Obj *listPtr; |
| 18364 | |
| 18365 | if (argc < 3) { |
| 18366 | Jim_WrongNumArgs(interp, 1, argv, "list index ?element ...?"); |
| 18367 | return JIM_ERR; |
| 18368 | } |
| 18369 | listPtr = argv[1]; |
| 18370 | if (Jim_IsShared(listPtr)) |
| 18371 | listPtr = Jim_DuplicateObj(interp, listPtr); |
| 18372 | if (Jim_GetIndex(interp, argv[2], &idx) != JIM_OK) |
| 18373 | goto err; |
| @@ -18391,14 +19375,10 @@ | |
| 18391 | { |
| 18392 | int first, last, len, rangeLen; |
| 18393 | Jim_Obj *listObj; |
| 18394 | Jim_Obj *newListObj; |
| 18395 | |
| 18396 | if (argc < 4) { |
| 18397 | Jim_WrongNumArgs(interp, 1, argv, "list first last ?element ...?"); |
| 18398 | return JIM_ERR; |
| 18399 | } |
| 18400 | if (Jim_GetIndex(interp, argv[2], &first) != JIM_OK || |
| 18401 | Jim_GetIndex(interp, argv[3], &last) != JIM_OK) { |
| 18402 | return JIM_ERR; |
| 18403 | } |
| 18404 | |
| @@ -18428,15 +19408,11 @@ | |
| 18428 | } |
| 18429 | |
| 18430 | |
| 18431 | static int Jim_LsetCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18432 | { |
| 18433 | if (argc < 3) { |
| 18434 | Jim_WrongNumArgs(interp, 1, argv, "listVar ?index ...? value"); |
| 18435 | return JIM_ERR; |
| 18436 | } |
| 18437 | else if (argc == 3) { |
| 18438 | |
| 18439 | if (Jim_SetVariable(interp, argv[1], argv[2]) != JIM_OK) |
| 18440 | return JIM_ERR; |
| 18441 | Jim_SetResult(interp, argv[2]); |
| 18442 | return JIM_OK; |
| @@ -18463,16 +19439,10 @@ | |
| 18463 | Jim_Obj **elements; |
| 18464 | int listlen; |
| 18465 | |
| 18466 | struct lsort_info info; |
| 18467 | |
| 18468 | if (argc < 2) { |
| 18469 | wrongargs: |
| 18470 | Jim_WrongNumArgs(interp, 1, argv, "?options? list"); |
| 18471 | return JIM_ERR; |
| 18472 | } |
| 18473 | |
| 18474 | info.type = JIM_LSORT_ASCII; |
| 18475 | info.order = 1; |
| 18476 | info.indexc = 0; |
| 18477 | info.unique = 0; |
| 18478 | info.command = NULL; |
| @@ -18518,11 +19488,11 @@ | |
| 18518 | info.command = argv[i + 1]; |
| 18519 | i++; |
| 18520 | break; |
| 18521 | case OPT_STRIDE: |
| 18522 | if (i >= argc - 2) { |
| 18523 | goto wrongargs; |
| 18524 | } |
| 18525 | if (Jim_GetLong(interp, argv[++i], &stride) != JIM_OK) { |
| 18526 | return JIM_ERR; |
| 18527 | } |
| 18528 | if (stride < 2) { |
| @@ -18596,38 +19566,28 @@ | |
| 18596 | static int Jim_AppendCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18597 | { |
| 18598 | Jim_Obj *stringObjPtr; |
| 18599 | int i; |
| 18600 | |
| 18601 | if (argc < 2) { |
| 18602 | Jim_WrongNumArgs(interp, 1, argv, "varName ?value ...?"); |
| 18603 | return JIM_ERR; |
| 18604 | } |
| 18605 | if (argc == 2) { |
| 18606 | stringObjPtr = Jim_GetVariable(interp, argv[1], JIM_ERRMSG); |
| 18607 | if (!stringObjPtr) |
| 18608 | return JIM_ERR; |
| 18609 | } |
| 18610 | else { |
| 18611 | int new_obj = 0; |
| 18612 | stringObjPtr = Jim_GetVariable(interp, argv[1], JIM_UNSHARED); |
| 18613 | if (!stringObjPtr) { |
| 18614 | |
| 18615 | stringObjPtr = Jim_NewEmptyStringObj(interp); |
| 18616 | new_obj = 1; |
| 18617 | } |
| 18618 | else if (Jim_IsShared(stringObjPtr)) { |
| 18619 | new_obj = 1; |
| 18620 | stringObjPtr = Jim_DuplicateObj(interp, stringObjPtr); |
| 18621 | } |
| 18622 | for (i = 2; i < argc; i++) { |
| 18623 | Jim_AppendObj(interp, stringObjPtr, argv[i]); |
| 18624 | } |
| 18625 | if (Jim_SetVariable(interp, argv[1], stringObjPtr) != JIM_OK) { |
| 18626 | if (new_obj) { |
| 18627 | Jim_FreeNewObj(interp, stringObjPtr); |
| 18628 | } |
| 18629 | return JIM_ERR; |
| 18630 | } |
| 18631 | } |
| 18632 | Jim_SetResult(interp, stringObjPtr); |
| 18633 | return JIM_OK; |
| @@ -18639,15 +19599,10 @@ | |
| 18639 | |
| 18640 | static int Jim_EvalCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18641 | { |
| 18642 | int rc; |
| 18643 | |
| 18644 | if (argc < 2) { |
| 18645 | Jim_WrongNumArgs(interp, 1, argv, "arg ?arg ...?"); |
| 18646 | return JIM_ERR; |
| 18647 | } |
| 18648 | |
| 18649 | if (argc == 2) { |
| 18650 | rc = Jim_EvalObj(interp, argv[1]); |
| 18651 | } |
| 18652 | else { |
| 18653 | rc = Jim_EvalObj(interp, Jim_ConcatObj(interp, argc - 1, argv + 1)); |
| @@ -18657,88 +19612,67 @@ | |
| 18657 | } |
| 18658 | |
| 18659 | |
| 18660 | static int Jim_UplevelCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18661 | { |
| 18662 | if (argc >= 2) { |
| 18663 | int retcode; |
| 18664 | Jim_CallFrame *savedCallFrame, *targetCallFrame; |
| 18665 | const char *str; |
| 18666 | |
| 18667 | |
| 18668 | savedCallFrame = interp->framePtr; |
| 18669 | |
| 18670 | |
| 18671 | str = Jim_String(argv[1]); |
| 18672 | if ((str[0] >= '0' && str[0] <= '9') || str[0] == '#') { |
| 18673 | targetCallFrame = Jim_GetCallFrameByLevel(interp, argv[1]); |
| 18674 | argc--; |
| 18675 | argv++; |
| 18676 | } |
| 18677 | else { |
| 18678 | targetCallFrame = Jim_GetCallFrameByLevel(interp, NULL); |
| 18679 | } |
| 18680 | if (targetCallFrame == NULL) { |
| 18681 | return JIM_ERR; |
| 18682 | } |
| 18683 | if (argc < 2) { |
| 18684 | Jim_WrongNumArgs(interp, 1, argv - 1, "?level? command ?arg ...?"); |
| 18685 | return JIM_ERR; |
| 18686 | } |
| 18687 | |
| 18688 | interp->framePtr = targetCallFrame; |
| 18689 | if (argc == 2) { |
| 18690 | retcode = Jim_EvalObj(interp, argv[1]); |
| 18691 | } |
| 18692 | else { |
| 18693 | retcode = Jim_EvalObj(interp, Jim_ConcatObj(interp, argc - 1, argv + 1)); |
| 18694 | } |
| 18695 | interp->framePtr = savedCallFrame; |
| 18696 | return retcode; |
| 18697 | } |
| 18698 | else { |
| 18699 | Jim_WrongNumArgs(interp, 1, argv, "?level? command ?arg ...?"); |
| 18700 | return JIM_ERR; |
| 18701 | } |
| 18702 | } |
| 18703 | |
| 18704 | |
| 18705 | static int Jim_ExprCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18706 | { |
| 18707 | int retcode; |
| 18708 | |
| 18709 | if (argc == 2) { |
| 18710 | retcode = Jim_EvalExpression(interp, argv[1]); |
| 18711 | } |
| 18712 | #ifndef JIM_COMPAT |
| 18713 | else { |
| 18714 | Jim_WrongNumArgs(interp, 1, argv, "expression"); |
| 18715 | retcode = JIM_ERR; |
| 18716 | } |
| 18717 | #else |
| 18718 | else if (argc > 2) { |
| 18719 | Jim_Obj *objPtr; |
| 18720 | |
| 18721 | objPtr = Jim_ConcatObj(interp, argc - 1, argv + 1); |
| 18722 | Jim_IncrRefCount(objPtr); |
| 18723 | retcode = Jim_EvalExpression(interp, objPtr); |
| 18724 | Jim_DecrRefCount(interp, objPtr); |
| 18725 | } |
| 18726 | else { |
| 18727 | Jim_WrongNumArgs(interp, 1, argv, "expression ?...?"); |
| 18728 | return JIM_ERR; |
| 18729 | } |
| 18730 | #endif |
| 18731 | return retcode; |
| 18732 | } |
| 18733 | |
| 18734 | static int JimBreakContinueHelper(Jim_Interp *interp, int argc, Jim_Obj *const *argv, int retcode) |
| 18735 | { |
| 18736 | if (argc != 1 && argc != 2) { |
| 18737 | Jim_WrongNumArgs(interp, 1, argv, "?level?"); |
| 18738 | return JIM_ERR; |
| 18739 | } |
| 18740 | if (argc == 2) { |
| 18741 | long level; |
| 18742 | int ret = Jim_GetLong(interp, argv[1], &level); |
| 18743 | if (ret != JIM_OK) { |
| 18744 | return ret; |
| @@ -18822,12 +19756,11 @@ | |
| 18822 | break; |
| 18823 | } |
| 18824 | } |
| 18825 | |
| 18826 | if (i != argc - 1 && i != argc) { |
| 18827 | Jim_WrongNumArgs(interp, 1, argv, |
| 18828 | "?-code code? ?-errorinfo stacktrace? ?-level level? ?result?"); |
| 18829 | } |
| 18830 | |
| 18831 | |
| 18832 | if (stackTraceObj && returnCode == JIM_ERR) { |
| 18833 | JimSetStackTrace(interp, stackTraceObj); |
| @@ -18876,10 +19809,11 @@ | |
| 18876 | |
| 18877 | return JIM_EVAL; |
| 18878 | } |
| 18879 | return JIM_OK; |
| 18880 | } |
| 18881 | |
| 18882 | static int JimAliasCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18883 | { |
| 18884 | Jim_Obj *cmdList; |
| 18885 | Jim_Obj *prefixListObj = Jim_CmdPrivData(interp); |
| @@ -18888,43 +19822,34 @@ | |
| 18888 | cmdList = Jim_DuplicateObj(interp, prefixListObj); |
| 18889 | Jim_ListInsertElements(interp, cmdList, Jim_ListLength(interp, cmdList), argc - 1, argv + 1); |
| 18890 | |
| 18891 | return JimEvalObjList(interp, cmdList); |
| 18892 | } |
| 18893 | |
| 18894 | static void JimAliasCmdDelete(Jim_Interp *interp, void *privData) |
| 18895 | { |
| 18896 | Jim_Obj *prefixListObj = privData; |
| 18897 | Jim_DecrRefCount(interp, prefixListObj); |
| 18898 | } |
| 18899 | |
| 18900 | static int Jim_AliasCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18901 | { |
| 18902 | Jim_Obj *prefixListObj; |
| 18903 | |
| 18904 | if (argc < 3) { |
| 18905 | Jim_WrongNumArgs(interp, 1, argv, "newname command ?args ...?"); |
| 18906 | return JIM_ERR; |
| 18907 | } |
| 18908 | |
| 18909 | prefixListObj = Jim_NewListObj(interp, argv + 2, argc - 2); |
| 18910 | Jim_IncrRefCount(prefixListObj); |
| 18911 | Jim_SetResult(interp, argv[1]); |
| 18912 | |
| 18913 | return Jim_CreateCommandObj(interp, argv[1], JimAliasCmd, prefixListObj, JimAliasCmdDelete); |
| 18914 | } |
| 18915 | |
| 18916 | |
| 18917 | static int Jim_ProcCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18918 | { |
| 18919 | Jim_Cmd *cmd; |
| 18920 | |
| 18921 | if (argc != 4 && argc != 5) { |
| 18922 | Jim_WrongNumArgs(interp, 1, argv, "name arglist ?statics? body"); |
| 18923 | return JIM_ERR; |
| 18924 | } |
| 18925 | |
| 18926 | if (argc == 4) { |
| 18927 | cmd = JimCreateProcedureCmd(interp, argv[2], NULL, argv[3], NULL); |
| 18928 | } |
| 18929 | else { |
| 18930 | cmd = JimCreateProcedureCmd(interp, argv[2], argv[3], argv[4], NULL); |
| @@ -18947,15 +19872,10 @@ | |
| 18947 | } |
| 18948 | |
| 18949 | |
| 18950 | static int Jim_XtraceCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18951 | { |
| 18952 | if (argc != 2) { |
| 18953 | Jim_WrongNumArgs(interp, 1, argv, "callback"); |
| 18954 | return JIM_ERR; |
| 18955 | } |
| 18956 | |
| 18957 | if (interp->traceCmdObj) { |
| 18958 | Jim_DecrRefCount(interp, interp->traceCmdObj); |
| 18959 | interp->traceCmdObj = NULL; |
| 18960 | } |
| 18961 | |
| @@ -18970,15 +19890,10 @@ | |
| 18970 | |
| 18971 | static int Jim_LocalCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18972 | { |
| 18973 | int retcode; |
| 18974 | |
| 18975 | if (argc < 2) { |
| 18976 | Jim_WrongNumArgs(interp, 1, argv, "cmd ?args ...?"); |
| 18977 | return JIM_ERR; |
| 18978 | } |
| 18979 | |
| 18980 | |
| 18981 | interp->local++; |
| 18982 | retcode = Jim_EvalObjVector(interp, argc - 1, argv + 1); |
| 18983 | interp->local--; |
| 18984 | |
| @@ -18990,11 +19905,11 @@ | |
| 18990 | if (Jim_GetCommand(interp, cmdNameObj, JIM_ERRMSG) == NULL) { |
| 18991 | return JIM_ERR; |
| 18992 | } |
| 18993 | if (interp->framePtr->localCommands == NULL) { |
| 18994 | interp->framePtr->localCommands = Jim_Alloc(sizeof(*interp->framePtr->localCommands)); |
| 18995 | Jim_InitStack(interp->framePtr->localCommands); |
| 18996 | } |
| 18997 | Jim_IncrRefCount(cmdNameObj); |
| 18998 | Jim_StackPush(interp->framePtr->localCommands, cmdNameObj); |
| 18999 | } |
| 19000 | |
| @@ -19002,89 +19917,129 @@ | |
| 19002 | } |
| 19003 | |
| 19004 | |
| 19005 | static int Jim_UpcallCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 19006 | { |
| 19007 | if (argc < 2) { |
| 19008 | Jim_WrongNumArgs(interp, 1, argv, "cmd ?args ...?"); |
| 19009 | return JIM_ERR; |
| 19010 | } |
| 19011 | else { |
| 19012 | int retcode; |
| 19013 | |
| 19014 | Jim_Cmd *cmdPtr = Jim_GetCommand(interp, argv[1], JIM_ERRMSG); |
| 19015 | if (cmdPtr == NULL || !cmdPtr->isproc || !cmdPtr->prevCmd) { |
| 19016 | Jim_SetResultFormatted(interp, "no previous command: \"%#s\"", argv[1]); |
| 19017 | return JIM_ERR; |
| 19018 | } |
| 19019 | |
| 19020 | cmdPtr->u.proc.upcall++; |
| 19021 | JimIncrCmdRefCount(cmdPtr); |
| 19022 | |
| 19023 | |
| 19024 | retcode = Jim_EvalObjVector(interp, argc - 1, argv + 1); |
| 19025 | |
| 19026 | |
| 19027 | cmdPtr->u.proc.upcall--; |
| 19028 | JimDecrCmdRefCount(interp, cmdPtr); |
| 19029 | |
| 19030 | return retcode; |
| 19031 | } |
| 19032 | } |
| 19033 | |
| 19034 | |
| 19035 | static int Jim_ApplyCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 19036 | { |
| 19037 | if (argc < 2) { |
| 19038 | Jim_WrongNumArgs(interp, 1, argv, "lambdaExpr ?arg ...?"); |
| 19039 | return JIM_ERR; |
| 19040 | } |
| 19041 | else { |
| 19042 | int ret; |
| 19043 | Jim_Cmd *cmd; |
| 19044 | Jim_Obj *argListObjPtr; |
| 19045 | Jim_Obj *bodyObjPtr; |
| 19046 | Jim_Obj *nsObj = NULL; |
| 19047 | Jim_Obj **nargv; |
| 19048 | |
| 19049 | int len = Jim_ListLength(interp, argv[1]); |
| 19050 | if (len != 2 && len != 3) { |
| 19051 | Jim_SetResultFormatted(interp, "can't interpret \"%#s\" as a lambda expression", argv[1]); |
| 19052 | return JIM_ERR; |
| 19053 | } |
| 19054 | |
| 19055 | if (len == 3) { |
| 19056 | #ifdef jim_ext_namespace |
| 19057 | |
| 19058 | nsObj = Jim_ListGetIndex(interp, argv[1], 2); |
| 19059 | #else |
| 19060 | Jim_SetResultString(interp, "namespaces not enabled", -1); |
| 19061 | return JIM_ERR; |
| 19062 | #endif |
| 19063 | } |
| 19064 | argListObjPtr = Jim_ListGetIndex(interp, argv[1], 0); |
| 19065 | bodyObjPtr = Jim_ListGetIndex(interp, argv[1], 1); |
| 19066 | |
| 19067 | cmd = JimCreateProcedureCmd(interp, argListObjPtr, NULL, bodyObjPtr, nsObj); |
| 19068 | |
| 19069 | if (cmd) { |
| 19070 | |
| 19071 | nargv = Jim_Alloc((argc - 2 + 1) * sizeof(*nargv)); |
| 19072 | nargv[0] = Jim_NewStringObj(interp, "apply lambdaExpr", -1); |
| 19073 | Jim_IncrRefCount(nargv[0]); |
| 19074 | memcpy(&nargv[1], argv + 2, (argc - 2) * sizeof(*nargv)); |
| 19075 | ret = JimCallProcedure(interp, cmd, argc - 2 + 1, nargv); |
| 19076 | Jim_DecrRefCount(interp, nargv[0]); |
| 19077 | Jim_Free(nargv); |
| 19078 | |
| 19079 | JimDecrCmdRefCount(interp, cmd); |
| 19080 | return ret; |
| 19081 | } |
| 19082 | return JIM_ERR; |
| 19083 | } |
| 19084 | } |
| 19085 | |
| 19086 | |
| 19087 | |
| 19088 | static int Jim_ConcatCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 19089 | { |
| 19090 | Jim_SetResult(interp, Jim_ConcatObj(interp, argc - 1, argv + 1)); |
| @@ -19110,12 +20065,11 @@ | |
| 19110 | return JIM_ERR; |
| 19111 | } |
| 19112 | |
| 19113 | |
| 19114 | if (argc < 3) { |
| 19115 | Jim_WrongNumArgs(interp, 1, argv, "?level? otherVar localVar ?otherVar localVar ...?"); |
| 19116 | return JIM_ERR; |
| 19117 | } |
| 19118 | |
| 19119 | |
| 19120 | for (i = 1; i < argc; i += 2) { |
| 19121 | if (Jim_SetVariableLink(interp, argv[i + 1], argv[i], targetCallFrame) != JIM_OK) |
| @@ -19127,14 +20081,10 @@ | |
| 19127 | |
| 19128 | static int Jim_GlobalCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 19129 | { |
| 19130 | int i; |
| 19131 | |
| 19132 | if (argc < 2) { |
| 19133 | Jim_WrongNumArgs(interp, 1, argv, "varName ?varName ...?"); |
| 19134 | return JIM_ERR; |
| 19135 | } |
| 19136 | |
| 19137 | if (interp->framePtr->level == 0) |
| 19138 | return JIM_OK; |
| 19139 | for (i = 1; i < argc; i++) { |
| 19140 | |
| @@ -19244,11 +20194,11 @@ | |
| 19244 | OPT_COUNT |
| 19245 | }; |
| 19246 | static const jim_subcmd_type cmds[OPT_COUNT + 1] = { |
| 19247 | JIM_DEF_SUBCMD("bytelength", "string", 1, 1), |
| 19248 | JIM_DEF_SUBCMD("byterange", "string first last", 3, 3), |
| 19249 | JIM_DEF_SUBCMD("cat", "?...?", 0, -1), |
| 19250 | JIM_DEF_SUBCMD("compare", "?-nocase? ?-length int? string1 string2", 2, 5), |
| 19251 | JIM_DEF_SUBCMD("equal", "?-nocase? ?-length int? string1 string2", 2, 5), |
| 19252 | JIM_DEF_SUBCMD("first", "subString string ?index?", 2, 3), |
| 19253 | JIM_DEF_SUBCMD("index", "string index", 2, 2), |
| 19254 | JIM_DEF_SUBCMD("is", "class ?-strict? str", 2, 3), |
| @@ -19264,11 +20214,11 @@ | |
| 19264 | JIM_DEF_SUBCMD("totitle", "string", 1, 1), |
| 19265 | JIM_DEF_SUBCMD("toupper", "string", 1, 1), |
| 19266 | JIM_DEF_SUBCMD("trim", "string ?trimchars?", 1, 2), |
| 19267 | JIM_DEF_SUBCMD("trimleft", "string ?trimchars?", 1, 2), |
| 19268 | JIM_DEF_SUBCMD("trimright", "string ?trimchars?", 1, 2), |
| 19269 | { } |
| 19270 | }; |
| 19271 | const jim_subcmd_type *ct = Jim_ParseSubCmd(interp, cmds, argc, argv); |
| 19272 | if (!ct) { |
| 19273 | return JIM_ERR; |
| 19274 | } |
| @@ -19562,14 +20512,10 @@ | |
| 19562 | static int Jim_TimeCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 19563 | { |
| 19564 | long i, count = 1; |
| 19565 | jim_wide start, elapsed; |
| 19566 | |
| 19567 | if (argc < 2) { |
| 19568 | Jim_WrongNumArgs(interp, 1, argv, "script ?count?"); |
| 19569 | return JIM_ERR; |
| 19570 | } |
| 19571 | if (argc == 3) { |
| 19572 | if (Jim_GetLong(interp, argv[2], &count) != JIM_OK) |
| 19573 | return JIM_ERR; |
| 19574 | } |
| 19575 | if (count < 0) |
| @@ -19594,23 +20540,19 @@ | |
| 19594 | Jim_AppendString(interp, Jim_GetResult(interp)," microseconds per iteration", -1); |
| 19595 | return JIM_OK; |
| 19596 | } |
| 19597 | |
| 19598 | |
| 19599 | static int Jim_TimeRateCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 19600 | { |
| 19601 | long us = 0; |
| 19602 | jim_wide start, delta, overhead; |
| 19603 | Jim_Obj *objPtr; |
| 19604 | double us_per_iter; |
| 19605 | int count; |
| 19606 | int n; |
| 19607 | |
| 19608 | if (argc < 2) { |
| 19609 | Jim_WrongNumArgs(interp, 1, argv, "script ?milliseconds?"); |
| 19610 | return JIM_ERR; |
| 19611 | } |
| 19612 | if (argc == 3) { |
| 19613 | if (Jim_GetLong(interp, argv[2], &us) != JIM_OK) |
| 19614 | return JIM_ERR; |
| 19615 | us *= 1000; |
| 19616 | } |
| @@ -19663,22 +20605,19 @@ | |
| 19663 | |
| 19664 | static int Jim_ExitCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 19665 | { |
| 19666 | long exitCode = 0; |
| 19667 | |
| 19668 | if (argc > 2) { |
| 19669 | Jim_WrongNumArgs(interp, 1, argv, "?exitCode?"); |
| 19670 | return JIM_ERR; |
| 19671 | } |
| 19672 | if (argc == 2) { |
| 19673 | if (Jim_GetLong(interp, argv[1], &exitCode) != JIM_OK) |
| 19674 | return JIM_ERR; |
| 19675 | Jim_SetResult(interp, argv[1]); |
| 19676 | } |
| 19677 | interp->exitCode = exitCode; |
| 19678 | return JIM_EXIT; |
| 19679 | } |
| 19680 | |
| 19681 | static int JimMatchReturnCodes(Jim_Interp *interp, Jim_Obj *retcodeListObj, int rc) |
| 19682 | { |
| 19683 | int len = Jim_ListLength(interp, retcodeListObj); |
| 19684 | int i; |
| @@ -19695,14 +20634,10 @@ | |
| 19695 | } |
| 19696 | |
| 19697 | |
| 19698 | static int JimCatchTryHelper(Jim_Interp *interp, int istry, int argc, Jim_Obj *const *argv) |
| 19699 | { |
| 19700 | static const char * const wrongargs_catchtry[2] = { |
| 19701 | "?-?no?code ... --? script ?resultVarName? ?optionVarName?", |
| 19702 | "?-?no?code ... --? script ?on|trap codes vars script? ... ?finally script?" |
| 19703 | }; |
| 19704 | int exitCode = 0; |
| 19705 | int i; |
| 19706 | int sig = 0; |
| 19707 | int ok; |
| 19708 | Jim_Obj *finallyScriptObj = NULL; |
| @@ -19748,11 +20683,11 @@ | |
| 19748 | } |
| 19749 | if (option < 0) { |
| 19750 | option = Jim_FindByName(arg, jimReturnCodes, jimReturnCodesSize); |
| 19751 | } |
| 19752 | if (option < 0) { |
| 19753 | goto wrongargs; |
| 19754 | } |
| 19755 | |
| 19756 | if (ignore) { |
| 19757 | ignore_mask |= ((jim_wide)1 << option); |
| 19758 | } |
| @@ -19762,13 +20697,11 @@ | |
| 19762 | } |
| 19763 | |
| 19764 | idx = i; |
| 19765 | |
| 19766 | if (argc - idx < 1) { |
| 19767 | wrongargs: |
| 19768 | Jim_WrongNumArgs(interp, 1, argv, wrongargs_catchtry[istry]); |
| 19769 | return JIM_ERR; |
| 19770 | } |
| 19771 | |
| 19772 | if ((ignore_mask & (1 << JIM_SIGNAL)) == 0) { |
| 19773 | sig++; |
| 19774 | } |
| @@ -19779,11 +20712,11 @@ | |
| 19779 | exitCode = JIM_SIGNAL; |
| 19780 | } |
| 19781 | else { |
| 19782 | exitCode = Jim_EvalObj(interp, argv[idx]); |
| 19783 | |
| 19784 | interp->errorFlag = 0; |
| 19785 | } |
| 19786 | interp->signal_level -= sig; |
| 19787 | |
| 19788 | errorCodeObj = Jim_GetGlobalVariableStr(interp, "errorCode", JIM_NONE); |
| 19789 | |
| @@ -19800,30 +20733,36 @@ | |
| 19800 | } |
| 19801 | switch (option) { |
| 19802 | case TRY_ON: |
| 19803 | case TRY_TRAP: |
| 19804 | if (idx + 4 > argc) { |
| 19805 | goto wrongargs; |
| 19806 | } |
| 19807 | if (option == TRY_ON) { |
| 19808 | ret = JimMatchReturnCodes(interp, argv[idx + 1], exitCode); |
| 19809 | if (ret > JIM_OK) { |
| 19810 | goto wrongargs; |
| 19811 | } |
| 19812 | } |
| 19813 | else if (errorCodeObj) { |
| 19814 | int len = Jim_ListLength(interp, argv[idx + 1]); |
| 19815 | int i; |
| 19816 | |
| 19817 | ret = JIM_OK; |
| 19818 | |
| 19819 | for (i = 0; i < len; i++) { |
| 19820 | Jim_Obj *matchObj = Jim_ListGetIndex(interp, argv[idx + 1], i); |
| 19821 | Jim_Obj *objPtr = Jim_ListGetIndex(interp, errorCodeObj, i); |
| 19822 | if (Jim_StringCompareObj(interp, matchObj, objPtr, 0) != 0) { |
| 19823 | ret = -1; |
| 19824 | break; |
| 19825 | } |
| 19826 | } |
| 19827 | } |
| 19828 | else { |
| 19829 | |
| @@ -19837,11 +20776,11 @@ | |
| 19837 | } |
| 19838 | idx += 4; |
| 19839 | break; |
| 19840 | case TRY_FINALLY: |
| 19841 | if (idx + 2 != argc) { |
| 19842 | goto wrongargs; |
| 19843 | } |
| 19844 | finallyScriptObj = argv[idx + 1]; |
| 19845 | idx += 2; |
| 19846 | break; |
| 19847 | } |
| @@ -19945,15 +20884,10 @@ | |
| 19945 | |
| 19946 | |
| 19947 | |
| 19948 | static int Jim_RenameCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 19949 | { |
| 19950 | if (argc != 3) { |
| 19951 | Jim_WrongNumArgs(interp, 1, argv, "oldName newName"); |
| 19952 | return JIM_ERR; |
| 19953 | } |
| 19954 | |
| 19955 | return Jim_RenameCommand(interp, argv[1], argv[2]); |
| 19956 | } |
| 19957 | |
| 19958 | #define JIM_DICTMATCH_KEYS 0x0001 |
| 19959 | #define JIM_DICTMATCH_VALUES 0x002 |
| @@ -20043,10 +20977,11 @@ | |
| 20043 | snprintf(buffer, sizeof(buffer), "%d entries in table, %d buckets", dict->len, dict->size); |
| 20044 | output = Jim_NewStringObj(interp, buffer, -1); |
| 20045 | Jim_SetResult(interp, output); |
| 20046 | return JIM_OK; |
| 20047 | } |
| 20048 | |
| 20049 | static int Jim_EvalEnsemble(Jim_Interp *interp, const char *basecmd, const char *subcmd, int argc, Jim_Obj *const *argv) |
| 20050 | { |
| 20051 | Jim_Obj *prefixObj = Jim_NewStringObj(interp, basecmd, -1); |
| 20052 | |
| @@ -20157,11 +21092,11 @@ | |
| 20157 | JIM_DEF_SUBCMD("remove", "dictionary ?key ...?", 1, -1), |
| 20158 | JIM_DEF_SUBCMD("values", "dictionary ?pattern?", 1, 2), |
| 20159 | JIM_DEF_SUBCMD("for", "vars dictionary script", 3, 3), |
| 20160 | JIM_DEF_SUBCMD("replace", "dictionary ?key value ...?", 1, -1), |
| 20161 | JIM_DEF_SUBCMD("update", "varName ?arg ...? script", 2, -1), |
| 20162 | { } |
| 20163 | }; |
| 20164 | const jim_subcmd_type *ct = Jim_ParseSubCmd(interp, cmds, argc, argv); |
| 20165 | if (!ct) { |
| 20166 | return JIM_ERR; |
| 20167 | } |
| @@ -20195,11 +21130,11 @@ | |
| 20195 | } |
| 20196 | return JIM_OK; |
| 20197 | } |
| 20198 | |
| 20199 | case OPT_SET: |
| 20200 | return Jim_SetDictKeysVector(interp, argv[2], argv + 3, argc - 4, argv[argc - 1], JIM_ERRMSG); |
| 20201 | |
| 20202 | case OPT_EXISTS:{ |
| 20203 | int rc = Jim_DictKeysVector(interp, argv[2], argv + 3, argc - 3, &objPtr, JIM_NONE); |
| 20204 | if (rc < 0) { |
| 20205 | return JIM_ERR; |
| @@ -20207,11 +21142,11 @@ | |
| 20207 | Jim_SetResultBool(interp, rc == JIM_OK); |
| 20208 | return JIM_OK; |
| 20209 | } |
| 20210 | |
| 20211 | case OPT_UNSET: |
| 20212 | if (Jim_SetDictKeysVector(interp, argv[2], argv + 3, argc - 3, NULL, JIM_NONE) != JIM_OK) { |
| 20213 | return JIM_ERR; |
| 20214 | } |
| 20215 | return JIM_OK; |
| 20216 | |
| 20217 | case OPT_VALUES: |
| @@ -20270,14 +21205,10 @@ | |
| 20270 | { OPT_NOBACKSLASHES, OPT_NOCOMMANDS, OPT_NOVARIABLES }; |
| 20271 | int i; |
| 20272 | int flags = JIM_SUBST_FLAG; |
| 20273 | Jim_Obj *objPtr; |
| 20274 | |
| 20275 | if (argc < 2) { |
| 20276 | Jim_WrongNumArgs(interp, 1, argv, "?options? string"); |
| 20277 | return JIM_ERR; |
| 20278 | } |
| 20279 | for (i = 1; i < (argc - 1); i++) { |
| 20280 | int option; |
| 20281 | |
| 20282 | if (Jim_GetEnum(interp, argv[i], options, &option, NULL, |
| 20283 | JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK) { |
| @@ -20300,11 +21231,24 @@ | |
| 20300 | } |
| 20301 | Jim_SetResult(interp, objPtr); |
| 20302 | return JIM_OK; |
| 20303 | } |
| 20304 | |
| 20305 | #ifdef jim_ext_namespace |
| 20306 | static int JimIsGlobalNamespace(Jim_Obj *objPtr) |
| 20307 | { |
| 20308 | int len; |
| 20309 | const char *str = Jim_GetString(objPtr, &len); |
| 20310 | return len >= 2 && str[0] == ':' && str[1] == ':'; |
| @@ -20313,23 +21257,25 @@ | |
| 20313 | |
| 20314 | |
| 20315 | static int Jim_InfoCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 20316 | { |
| 20317 | Jim_Obj *objPtr; |
| 20318 | int mode = 0; |
| 20319 | |
| 20320 | |
| 20321 | enum { |
| 20322 | INFO_ALIAS, |
| 20323 | INFO_ARGS, |
| 20324 | INFO_BODY, |
| 20325 | INFO_CHANNELS, |
| 20326 | INFO_COMMANDS, |
| 20327 | INFO_COMPLETE, |
| 20328 | INFO_EXISTS, |
| 20329 | INFO_FRAME, |
| 20330 | INFO_GLOBALS, |
| 20331 | INFO_HOSTNAME, |
| 20332 | INFO_LEVEL, |
| 20333 | INFO_LOCALS, |
| 20334 | INFO_NAMEOFEXECUTABLE, |
| 20335 | INFO_PATCHLEVEL, |
| @@ -20338,39 +21284,45 @@ | |
| 20338 | INFO_RETURNCODES, |
| 20339 | INFO_SCRIPT, |
| 20340 | INFO_SOURCE, |
| 20341 | INFO_STACKTRACE, |
| 20342 | INFO_STATICS, |
| 20343 | INFO_VARS, |
| 20344 | INFO_VERSION, |
| 20345 | INFO_COUNT |
| 20346 | }; |
| 20347 | static const jim_subcmd_type cmds[INFO_COUNT + 1] = { |
| 20348 | JIM_DEF_SUBCMD("alias", "command", 1, 1), |
| 20349 | JIM_DEF_SUBCMD("args", "procname", 1, 1), |
| 20350 | JIM_DEF_SUBCMD("body", "procname", 1, 1), |
| 20351 | JIM_DEF_SUBCMD("channels", "?pattern?", 0, 1), |
| 20352 | JIM_DEF_SUBCMD("commands", "?pattern?", 0, 1), |
| 20353 | JIM_DEF_SUBCMD("complete", "script ?missing?", 1, 2), |
| 20354 | JIM_DEF_SUBCMD("exists", "varName", 1, 1), |
| 20355 | JIM_DEF_SUBCMD("frame", "?levelNum?", 0, 1), |
| 20356 | JIM_DEF_SUBCMD("globals", "?pattern?", 0, 1), |
| 20357 | JIM_DEF_SUBCMD("hostname", NULL, 0, 0), |
| 20358 | JIM_DEF_SUBCMD("level", "?levelNum?", 0, 1), |
| 20359 | JIM_DEF_SUBCMD("locals", "?pattern?", 0, 1), |
| 20360 | JIM_DEF_SUBCMD("nameofexecutable", NULL, 0, 0), |
| 20361 | JIM_DEF_SUBCMD("patchlevel", NULL, 0, 0), |
| 20362 | JIM_DEF_SUBCMD("procs", "?pattern?", 0, 1), |
| 20363 | JIM_DEF_SUBCMD("references", NULL, 0, 0), |
| 20364 | JIM_DEF_SUBCMD("returncodes", "?code?", 0, 1), |
| 20365 | JIM_DEF_SUBCMD("script", "?filename?", 0, 1), |
| 20366 | JIM_DEF_SUBCMD("source", "source ?filename line?", 1, 3), |
| 20367 | JIM_DEF_SUBCMD("stacktrace", NULL, 0, 0), |
| 20368 | JIM_DEF_SUBCMD("statics", "procname", 1, 1), |
| 20369 | JIM_DEF_SUBCMD("vars", "?pattern?", 0, 1), |
| 20370 | JIM_DEF_SUBCMD("version", NULL, 0, 0), |
| 20371 | { } |
| 20372 | }; |
| 20373 | const jim_subcmd_type *ct; |
| 20374 | #ifdef jim_ext_namespace |
| 20375 | int nons = 0; |
| 20376 | |
| @@ -20401,45 +21353,66 @@ | |
| 20401 | Jim_Cmd *cmdPtr; |
| 20402 | |
| 20403 | if ((cmdPtr = Jim_GetCommand(interp, argv[2], JIM_ERRMSG)) == NULL) { |
| 20404 | return JIM_ERR; |
| 20405 | } |
| 20406 | if (cmdPtr->isproc || cmdPtr->u.native.cmdProc != JimAliasCmd) { |
| 20407 | Jim_SetResultFormatted(interp, "command \"%#s\" is not an alias", argv[2]); |
| 20408 | return JIM_ERR; |
| 20409 | } |
| 20410 | Jim_SetResult(interp, (Jim_Obj *)cmdPtr->u.native.privData); |
| 20411 | return JIM_OK; |
| 20412 | } |
| 20413 | |
| 20414 | case INFO_CHANNELS: |
| 20415 | mode++; |
| 20416 | #ifndef jim_ext_aio |
| 20417 | Jim_SetResultString(interp, "aio not enabled", -1); |
| 20418 | return JIM_ERR; |
| 20419 | #endif |
| 20420 | |
| 20421 | case INFO_PROCS: |
| 20422 | mode++; |
| 20423 | |
| 20424 | case INFO_COMMANDS: |
| 20425 | |
| 20426 | #ifdef jim_ext_namespace |
| 20427 | if (!nons) { |
| 20428 | if (Jim_Length(interp->framePtr->nsObj) || (argc == 3 && JimIsGlobalNamespace(argv[2]))) { |
| 20429 | return Jim_EvalPrefix(interp, "namespace info", argc - 1, argv + 1); |
| 20430 | } |
| 20431 | } |
| 20432 | #endif |
| 20433 | Jim_SetResult(interp, JimCommandsList(interp, (argc == 3) ? argv[2] : NULL, mode)); |
| 20434 | return JIM_OK; |
| 20435 | |
| 20436 | case INFO_VARS: |
| 20437 | mode++; |
| 20438 | |
| 20439 | case INFO_LOCALS: |
| 20440 | mode++; |
| 20441 | |
| 20442 | case INFO_GLOBALS: |
| 20443 | |
| 20444 | #ifdef jim_ext_namespace |
| 20445 | if (!nons) { |
| @@ -20512,20 +21485,41 @@ | |
| 20512 | return JIM_ERR; |
| 20513 | } |
| 20514 | Jim_SetResult(interp, objPtr); |
| 20515 | } |
| 20516 | return JIM_OK; |
| 20517 | |
| 20518 | case INFO_BODY: |
| 20519 | case INFO_STATICS: |
| 20520 | case INFO_ARGS:{ |
| 20521 | Jim_Cmd *cmdPtr; |
| 20522 | |
| 20523 | if ((cmdPtr = Jim_GetCommand(interp, argv[2], JIM_ERRMSG)) == NULL) { |
| 20524 | return JIM_ERR; |
| 20525 | } |
| 20526 | if (!cmdPtr->isproc) { |
| 20527 | Jim_SetResultFormatted(interp, "command \"%#s\" is not a procedure", argv[2]); |
| 20528 | return JIM_ERR; |
| 20529 | } |
| 20530 | switch (option) { |
| 20531 | #ifdef JIM_NO_INTROSPECTION |
| @@ -20548,16 +21542,21 @@ | |
| 20548 | break; |
| 20549 | } |
| 20550 | return JIM_OK; |
| 20551 | } |
| 20552 | |
| 20553 | case INFO_VERSION: |
| 20554 | case INFO_PATCHLEVEL:{ |
| 20555 | char buf[(JIM_INTEGER_SPACE * 2) + 1]; |
| 20556 | |
| 20557 | sprintf(buf, "%d.%d", JIM_VERSION / 100, JIM_VERSION % 100); |
| 20558 | Jim_SetResultString(interp, buf, -1); |
| 20559 | return JIM_OK; |
| 20560 | } |
| 20561 | |
| 20562 | case INFO_COMPLETE: { |
| 20563 | char missing; |
| @@ -20623,15 +21622,15 @@ | |
| 20623 | { |
| 20624 | Jim_Obj *objPtr; |
| 20625 | int result = 0; |
| 20626 | |
| 20627 | static const char * const options[] = { |
| 20628 | "-command", "-proc", "-alias", "-var", NULL |
| 20629 | }; |
| 20630 | enum |
| 20631 | { |
| 20632 | OPT_COMMAND, OPT_PROC, OPT_ALIAS, OPT_VAR |
| 20633 | }; |
| 20634 | int option; |
| 20635 | |
| 20636 | if (argc == 2) { |
| 20637 | option = OPT_VAR; |
| @@ -20642,12 +21641,11 @@ | |
| 20642 | return JIM_ERR; |
| 20643 | } |
| 20644 | objPtr = argv[2]; |
| 20645 | } |
| 20646 | else { |
| 20647 | Jim_WrongNumArgs(interp, 1, argv, "?option? name"); |
| 20648 | return JIM_ERR; |
| 20649 | } |
| 20650 | |
| 20651 | if (option == OPT_VAR) { |
| 20652 | result = Jim_GetVariable(interp, objPtr, 0) != NULL; |
| 20653 | } |
| @@ -20660,15 +21658,19 @@ | |
| 20660 | case OPT_COMMAND: |
| 20661 | result = 1; |
| 20662 | break; |
| 20663 | |
| 20664 | case OPT_ALIAS: |
| 20665 | result = cmd->isproc == 0 && cmd->u.native.cmdProc == JimAliasCmd; |
| 20666 | break; |
| 20667 | |
| 20668 | case OPT_PROC: |
| 20669 | result = cmd->isproc; |
| 20670 | break; |
| 20671 | } |
| 20672 | } |
| 20673 | } |
| 20674 | Jim_SetResultBool(interp, result); |
| @@ -20682,15 +21684,10 @@ | |
| 20682 | int splitLen, strLen; |
| 20683 | Jim_Obj *resObjPtr; |
| 20684 | int c; |
| 20685 | int len; |
| 20686 | |
| 20687 | if (argc != 2 && argc != 3) { |
| 20688 | Jim_WrongNumArgs(interp, 1, argv, "string ?splitChars?"); |
| 20689 | return JIM_ERR; |
| 20690 | } |
| 20691 | |
| 20692 | str = Jim_GetString(argv[1], &len); |
| 20693 | if (len == 0) { |
| 20694 | return JIM_OK; |
| 20695 | } |
| 20696 | strLen = Jim_Utf8Length(interp, argv[1]); |
| @@ -20765,14 +21762,10 @@ | |
| 20765 | static int Jim_JoinCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 20766 | { |
| 20767 | const char *joinStr; |
| 20768 | int joinStrLen; |
| 20769 | |
| 20770 | if (argc != 2 && argc != 3) { |
| 20771 | Jim_WrongNumArgs(interp, 1, argv, "list ?joinString?"); |
| 20772 | return JIM_ERR; |
| 20773 | } |
| 20774 | |
| 20775 | if (argc == 2) { |
| 20776 | joinStr = " "; |
| 20777 | joinStrLen = 1; |
| 20778 | } |
| @@ -20786,14 +21779,10 @@ | |
| 20786 | |
| 20787 | static int Jim_FormatCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 20788 | { |
| 20789 | Jim_Obj *objPtr; |
| 20790 | |
| 20791 | if (argc < 2) { |
| 20792 | Jim_WrongNumArgs(interp, 1, argv, "formatString ?arg arg ...?"); |
| 20793 | return JIM_ERR; |
| 20794 | } |
| 20795 | objPtr = Jim_FormatString(interp, argv[1], argc - 2, argv + 2); |
| 20796 | if (objPtr == NULL) |
| 20797 | return JIM_ERR; |
| 20798 | Jim_SetResult(interp, objPtr); |
| 20799 | return JIM_OK; |
| @@ -20803,14 +21792,10 @@ | |
| 20803 | static int Jim_ScanCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 20804 | { |
| 20805 | Jim_Obj *listPtr, **outVec; |
| 20806 | int outc, i; |
| 20807 | |
| 20808 | if (argc < 3) { |
| 20809 | Jim_WrongNumArgs(interp, 1, argv, "string format ?varName varName ...?"); |
| 20810 | return JIM_ERR; |
| 20811 | } |
| 20812 | if (argv[2]->typePtr != &scanFmtStringObjType) |
| 20813 | SetScanFmtFromAny(interp, argv[2]); |
| 20814 | if (FormatGetError(argv[2]) != 0) { |
| 20815 | Jim_SetResultString(interp, FormatGetError(argv[2]), -1); |
| 20816 | return JIM_ERR; |
| @@ -20876,14 +21861,10 @@ | |
| 20876 | } |
| 20877 | |
| 20878 | |
| 20879 | static int Jim_ErrorCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 20880 | { |
| 20881 | if (argc != 2 && argc != 3) { |
| 20882 | Jim_WrongNumArgs(interp, 1, argv, "message ?stacktrace?"); |
| 20883 | return JIM_ERR; |
| 20884 | } |
| 20885 | Jim_SetResult(interp, argv[1]); |
| 20886 | if (argc == 3) { |
| 20887 | JimSetStackTrace(interp, argv[2]); |
| 20888 | return JIM_ERR; |
| 20889 | } |
| @@ -20893,14 +21874,10 @@ | |
| 20893 | |
| 20894 | static int Jim_LrangeCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 20895 | { |
| 20896 | Jim_Obj *objPtr; |
| 20897 | |
| 20898 | if (argc != 4) { |
| 20899 | Jim_WrongNumArgs(interp, 1, argv, "list first last"); |
| 20900 | return JIM_ERR; |
| 20901 | } |
| 20902 | if ((objPtr = Jim_ListRange(interp, argv[1], argv[2], argv[3])) == NULL) |
| 20903 | return JIM_ERR; |
| 20904 | Jim_SetResult(interp, objPtr); |
| 20905 | return JIM_OK; |
| 20906 | } |
| @@ -20910,12 +21887,11 @@ | |
| 20910 | { |
| 20911 | Jim_Obj *objPtr; |
| 20912 | jim_wide count; |
| 20913 | |
| 20914 | if (argc < 2 || Jim_GetWideExpr(interp, argv[1], &count) != JIM_OK || count < 0) { |
| 20915 | Jim_WrongNumArgs(interp, 1, argv, "count ?value ...?"); |
| 20916 | return JIM_ERR; |
| 20917 | } |
| 20918 | if (count == 0 || argc == 2) { |
| 20919 | Jim_SetEmptyResult(interp); |
| 20920 | return JIM_OK; |
| 20921 | } |
| @@ -20935,23 +21911,26 @@ | |
| 20935 | |
| 20936 | char **Jim_GetEnviron(void) |
| 20937 | { |
| 20938 | #if defined(HAVE__NSGETENVIRON) |
| 20939 | return *_NSGetEnviron(); |
| 20940 | #else |
| 20941 | #if !defined(NO_ENVIRON_EXTERN) |
| 20942 | extern char **environ; |
| 20943 | #endif |
| 20944 | |
| 20945 | return environ; |
| 20946 | #endif |
| 20947 | } |
| 20948 | |
| 20949 | void Jim_SetEnviron(char **env) |
| 20950 | { |
| 20951 | #if defined(HAVE__NSGETENVIRON) |
| 20952 | *_NSGetEnviron() = env; |
| 20953 | #else |
| 20954 | #if !defined(NO_ENVIRON_EXTERN) |
| 20955 | extern char **environ; |
| 20956 | #endif |
| 20957 | |
| @@ -20983,14 +21962,10 @@ | |
| 20983 | |
| 20984 | Jim_SetResult(interp, listObjPtr); |
| 20985 | return JIM_OK; |
| 20986 | } |
| 20987 | |
| 20988 | if (argc > 3) { |
| 20989 | Jim_WrongNumArgs(interp, 1, argv, "varName ?default?"); |
| 20990 | return JIM_ERR; |
| 20991 | } |
| 20992 | key = Jim_String(argv[1]); |
| 20993 | val = getenv(key); |
| 20994 | if (val == NULL) { |
| 20995 | if (argc < 3) { |
| 20996 | Jim_SetResultFormatted(interp, "environment variable \"%#s\" does not exist", argv[1]); |
| @@ -21003,41 +21978,31 @@ | |
| 21003 | } |
| 21004 | |
| 21005 | |
| 21006 | static int Jim_SourceCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 21007 | { |
| 21008 | int retval; |
| 21009 | |
| 21010 | if (argc != 2) { |
| 21011 | Jim_WrongNumArgs(interp, 1, argv, "fileName"); |
| 21012 | return JIM_ERR; |
| 21013 | } |
| 21014 | retval = Jim_EvalFile(interp, Jim_String(argv[1])); |
| 21015 | if (retval == JIM_RETURN) |
| 21016 | return JIM_OK; |
| 21017 | return retval; |
| 21018 | } |
| 21019 | |
| 21020 | |
| 21021 | static int Jim_LreverseCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 21022 | { |
| 21023 | Jim_Obj *revObjPtr, **ele; |
| 21024 | int len; |
| 21025 | |
| 21026 | if (argc != 2) { |
| 21027 | Jim_WrongNumArgs(interp, 1, argv, "list"); |
| 21028 | return JIM_ERR; |
| 21029 | } |
| 21030 | JimListGetElements(interp, argv[1], &len, &ele); |
| 21031 | revObjPtr = Jim_NewListObj(interp, NULL, 0); |
| 21032 | ListEnsureLength(revObjPtr, len); |
| 21033 | len--; |
| 21034 | while (len >= 0) |
| 21035 | ListAppendElement(revObjPtr, ele[len--]); |
| 21036 | Jim_SetResult(interp, revObjPtr); |
| 21037 | return JIM_OK; |
| 21038 | } |
| 21039 | |
| 21040 | static int JimRangeLen(jim_wide start, jim_wide end, jim_wide step) |
| 21041 | { |
| 21042 | jim_wide len; |
| 21043 | |
| @@ -21065,14 +22030,10 @@ | |
| 21065 | { |
| 21066 | jim_wide start = 0, end, step = 1; |
| 21067 | int len, i; |
| 21068 | Jim_Obj *objPtr; |
| 21069 | |
| 21070 | if (argc < 2 || argc > 4) { |
| 21071 | Jim_WrongNumArgs(interp, 1, argv, "?start? end ?step?"); |
| 21072 | return JIM_ERR; |
| 21073 | } |
| 21074 | if (argc == 2) { |
| 21075 | if (Jim_GetWideExpr(interp, argv[1], &end) != JIM_OK) |
| 21076 | return JIM_ERR; |
| 21077 | } |
| 21078 | else { |
| @@ -21097,14 +22058,10 @@ | |
| 21097 | |
| 21098 | static int Jim_RandCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 21099 | { |
| 21100 | jim_wide min = 0, max = 0, len, maxMul; |
| 21101 | |
| 21102 | if (argc < 1 || argc > 3) { |
| 21103 | Jim_WrongNumArgs(interp, 1, argv, "?min? max"); |
| 21104 | return JIM_ERR; |
| 21105 | } |
| 21106 | if (argc == 1) { |
| 21107 | max = JIM_WIDE_MAX; |
| 21108 | } else if (argc == 2) { |
| 21109 | if (Jim_GetWideExpr(interp, argv[1], &max) != JIM_OK) |
| 21110 | return JIM_ERR; |
| @@ -21128,98 +22085,111 @@ | |
| 21128 | Jim_SetResultInt(interp, min+r); |
| 21129 | return JIM_OK; |
| 21130 | } |
| 21131 | } |
| 21132 | |
| 21133 | static const struct { |
| 21134 | const char *name; |
| 21135 | Jim_CmdProc *cmdProc; |
| 21136 | } Jim_CoreCommandsTable[] = { |
| 21137 | {"alias", Jim_AliasCoreCommand}, |
| 21138 | {"set", Jim_SetCoreCommand}, |
| 21139 | {"unset", Jim_UnsetCoreCommand}, |
| 21140 | {"puts", Jim_PutsCoreCommand}, |
| 21141 | {"+", Jim_AddCoreCommand}, |
| 21142 | {"*", Jim_MulCoreCommand}, |
| 21143 | {"-", Jim_SubCoreCommand}, |
| 21144 | {"/", Jim_DivCoreCommand}, |
| 21145 | {"incr", Jim_IncrCoreCommand}, |
| 21146 | {"while", Jim_WhileCoreCommand}, |
| 21147 | {"loop", Jim_LoopCoreCommand}, |
| 21148 | {"for", Jim_ForCoreCommand}, |
| 21149 | {"foreach", Jim_ForeachCoreCommand}, |
| 21150 | {"lmap", Jim_LmapCoreCommand}, |
| 21151 | {"lassign", Jim_LassignCoreCommand}, |
| 21152 | {"if", Jim_IfCoreCommand}, |
| 21153 | {"switch", Jim_SwitchCoreCommand}, |
| 21154 | {"list", Jim_ListCoreCommand}, |
| 21155 | {"lindex", Jim_LindexCoreCommand}, |
| 21156 | {"lset", Jim_LsetCoreCommand}, |
| 21157 | {"lsearch", Jim_LsearchCoreCommand}, |
| 21158 | {"llength", Jim_LlengthCoreCommand}, |
| 21159 | {"lappend", Jim_LappendCoreCommand}, |
| 21160 | {"linsert", Jim_LinsertCoreCommand}, |
| 21161 | {"lreplace", Jim_LreplaceCoreCommand}, |
| 21162 | {"lsort", Jim_LsortCoreCommand}, |
| 21163 | {"append", Jim_AppendCoreCommand}, |
| 21164 | {"eval", Jim_EvalCoreCommand}, |
| 21165 | {"uplevel", Jim_UplevelCoreCommand}, |
| 21166 | {"expr", Jim_ExprCoreCommand}, |
| 21167 | {"break", Jim_BreakCoreCommand}, |
| 21168 | {"continue", Jim_ContinueCoreCommand}, |
| 21169 | {"proc", Jim_ProcCoreCommand}, |
| 21170 | {"xtrace", Jim_XtraceCoreCommand}, |
| 21171 | {"concat", Jim_ConcatCoreCommand}, |
| 21172 | {"return", Jim_ReturnCoreCommand}, |
| 21173 | {"upvar", Jim_UpvarCoreCommand}, |
| 21174 | {"global", Jim_GlobalCoreCommand}, |
| 21175 | {"string", Jim_StringCoreCommand}, |
| 21176 | {"time", Jim_TimeCoreCommand}, |
| 21177 | {"timerate", Jim_TimeRateCoreCommand}, |
| 21178 | {"exit", Jim_ExitCoreCommand}, |
| 21179 | {"catch", Jim_CatchCoreCommand}, |
| 21180 | {"try", Jim_TryCoreCommand}, |
| 21181 | #ifdef JIM_REFERENCES |
| 21182 | {"ref", Jim_RefCoreCommand}, |
| 21183 | {"getref", Jim_GetrefCoreCommand}, |
| 21184 | {"setref", Jim_SetrefCoreCommand}, |
| 21185 | {"finalize", Jim_FinalizeCoreCommand}, |
| 21186 | {"collect", Jim_CollectCoreCommand}, |
| 21187 | #endif |
| 21188 | {"rename", Jim_RenameCoreCommand}, |
| 21189 | {"dict", Jim_DictCoreCommand}, |
| 21190 | {"subst", Jim_SubstCoreCommand}, |
| 21191 | {"info", Jim_InfoCoreCommand}, |
| 21192 | {"exists", Jim_ExistsCoreCommand}, |
| 21193 | {"split", Jim_SplitCoreCommand}, |
| 21194 | {"join", Jim_JoinCoreCommand}, |
| 21195 | {"format", Jim_FormatCoreCommand}, |
| 21196 | {"scan", Jim_ScanCoreCommand}, |
| 21197 | {"error", Jim_ErrorCoreCommand}, |
| 21198 | {"lrange", Jim_LrangeCoreCommand}, |
| 21199 | {"lrepeat", Jim_LrepeatCoreCommand}, |
| 21200 | {"env", Jim_EnvCoreCommand}, |
| 21201 | {"source", Jim_SourceCoreCommand}, |
| 21202 | {"lreverse", Jim_LreverseCoreCommand}, |
| 21203 | {"range", Jim_RangeCoreCommand}, |
| 21204 | {"rand", Jim_RandCoreCommand}, |
| 21205 | {"tailcall", Jim_TailcallCoreCommand}, |
| 21206 | {"local", Jim_LocalCoreCommand}, |
| 21207 | {"upcall", Jim_UpcallCoreCommand}, |
| 21208 | {"apply", Jim_ApplyCoreCommand}, |
| 21209 | {"stacktrace", Jim_StacktraceCoreCommand}, |
| 21210 | {NULL, NULL}, |
| 21211 | }; |
| 21212 | |
| 21213 | void Jim_RegisterCoreCommands(Jim_Interp *interp) |
| 21214 | { |
| 21215 | int i = 0; |
| 21216 | |
| 21217 | while (Jim_CoreCommandsTable[i].name != NULL) { |
| 21218 | Jim_CreateCommand(interp, |
| 21219 | Jim_CoreCommandsTable[i].name, Jim_CoreCommandsTable[i].cmdProc, NULL, NULL); |
| 21220 | i++; |
| 21221 | } |
| 21222 | } |
| 21223 | |
| 21224 | void Jim_MakeErrorMessage(Jim_Interp *interp) |
| 21225 | { |
| @@ -21662,18 +22632,26 @@ | |
| 21662 | int Jim_CallSubCmd(Jim_Interp *interp, const jim_subcmd_type * ct, int argc, Jim_Obj *const *argv) |
| 21663 | { |
| 21664 | int ret = JIM_ERR; |
| 21665 | |
| 21666 | if (ct) { |
| 21667 | if (ct->flags & JIM_MODFLAG_FULLARGV) { |
| 21668 | ret = ct->function(interp, argc, argv); |
| 21669 | } |
| 21670 | else { |
| 21671 | ret = ct->function(interp, argc - 2, argv + 2); |
| 21672 | } |
| 21673 | if (ret < 0) { |
| 21674 | Jim_SubCmdArgError(interp, ct, argv[0]); |
| 21675 | ret = JIM_ERR; |
| 21676 | } |
| 21677 | } |
| 21678 | return ret; |
| 21679 | } |
| @@ -21683,10 +22661,22 @@ | |
| 21683 | const jim_subcmd_type *ct = |
| 21684 | Jim_ParseSubCmd(interp, (const jim_subcmd_type *)Jim_CmdPrivData(interp), argc, argv); |
| 21685 | |
| 21686 | return Jim_CallSubCmd(interp, ct, argc, argv); |
| 21687 | } |
| 21688 | |
| 21689 | #include <ctype.h> |
| 21690 | #include <stdlib.h> |
| 21691 | #include <string.h> |
| 21692 | #include <stdio.h> |
| @@ -21873,11 +22863,11 @@ | |
| 21873 | gotPrecision = 1; |
| 21874 | format += step; |
| 21875 | step = utf8_tounicode(format, &ch); |
| 21876 | } |
| 21877 | if (isdigit(ch)) { |
| 21878 | precision = strtoul(format, &end, 10); |
| 21879 | format = end; |
| 21880 | step = utf8_tounicode(format, &ch); |
| 21881 | } else if (ch == '*') { |
| 21882 | if (objIndex >= objc - 1) { |
| 21883 | msg = badIndex[gotXpg]; |
| @@ -22160,12 +23150,11 @@ | |
| 22160 | |
| 22161 | |
| 22162 | |
| 22163 | |
| 22164 | #define FAIL(R,M) { (R)->err = (M); return (M); } |
| 22165 | #define ISMULT(c) ((c) == '*' || (c) == '+' || (c) == '?' || (c) == '{') |
| 22166 | #define META "^$.[()|?{+*" |
| 22167 | |
| 22168 | #define HASWIDTH 1 |
| 22169 | #define SIMPLE 2 |
| 22170 | #define SPSTART 4 |
| 22171 | #define WORST 0 |
| @@ -22193,10 +23182,26 @@ | |
| 22193 | static int regnarrate = 0; |
| 22194 | static void regdump(regex_t *preg); |
| 22195 | static const char *regprop( int op ); |
| 22196 | #endif |
| 22197 | |
| 22198 | |
| 22199 | static int str_int_len(const int *seq) |
| 22200 | { |
| 22201 | int n = 0; |
| 22202 | while (*seq++) { |
| @@ -22203,10 +23208,45 @@ | |
| 22203 | n++; |
| 22204 | } |
| 22205 | return n; |
| 22206 | } |
| 22207 | |
| 22208 | int jim_regcomp(regex_t *preg, const char *exp, int cflags) |
| 22209 | { |
| 22210 | int scan; |
| 22211 | int longest; |
| 22212 | unsigned len; |
| @@ -22217,10 +23257,15 @@ | |
| 22217 | #endif |
| 22218 | memset(preg, 0, sizeof(*preg)); |
| 22219 | |
| 22220 | if (exp == NULL) |
| 22221 | FAIL(preg, REG_ERR_NULL_ARGUMENT); |
| 22222 | |
| 22223 | |
| 22224 | preg->cflags = cflags; |
| 22225 | preg->regparse = exp; |
| 22226 | |
| @@ -22392,16 +23437,16 @@ | |
| 22392 | |
| 22393 | ret = regatom(preg, &flags); |
| 22394 | if (ret == 0) |
| 22395 | return 0; |
| 22396 | |
| 22397 | op = *preg->regparse; |
| 22398 | if (!ISMULT(op)) { |
| 22399 | *flagp = flags; |
| 22400 | return(ret); |
| 22401 | } |
| 22402 | |
| 22403 | if (!(flags&HASWIDTH) && op != '?') { |
| 22404 | preg->err = REG_ERR_OPERAND_COULD_BE_EMPTY; |
| 22405 | return 0; |
| 22406 | } |
| 22407 | |
| @@ -22466,11 +23511,11 @@ | |
| 22466 | regtail(preg, back, ret); |
| 22467 | regtail(preg, next, back); |
| 22468 | } |
| 22469 | |
| 22470 | preg->regparse++; |
| 22471 | if (ISMULT(*preg->regparse)) { |
| 22472 | preg->err = REG_ERR_NESTED_COUNT; |
| 22473 | return 0; |
| 22474 | } |
| 22475 | |
| 22476 | return ret; |
| @@ -22775,16 +23820,10 @@ | |
| 22775 | case '\0': |
| 22776 | case '|': |
| 22777 | case ')': |
| 22778 | preg->err = REG_ERR_INTERNAL; |
| 22779 | return 0; |
| 22780 | case '?': |
| 22781 | case '+': |
| 22782 | case '*': |
| 22783 | case '{': |
| 22784 | preg->err = REG_ERR_COUNT_FOLLOWS_NOTHING; |
| 22785 | return 0; |
| 22786 | case '\\': |
| 22787 | ch = *preg->regparse++; |
| 22788 | switch (ch) { |
| 22789 | case '\0': |
| 22790 | preg->err = REG_ERR_INVALID_ESCAPE; |
| @@ -22841,16 +23880,21 @@ | |
| 22841 | default: { |
| 22842 | int added = 0; |
| 22843 | |
| 22844 | |
| 22845 | preg->regparse -= n; |
| 22846 | |
| 22847 | ret = regnode(preg, EXACTLY); |
| 22848 | |
| 22849 | |
| 22850 | |
| 22851 | while (*preg->regparse && strchr(META, *preg->regparse) == NULL) { |
| 22852 | n = reg_utf8_tounicode_case(preg->regparse, &ch, (preg->cflags & REG_ICASE)); |
| 22853 | if (ch == '\\' && preg->regparse[n]) { |
| 22854 | if (strchr("<>mMwWdDsSAZ", preg->regparse[n])) { |
| 22855 | |
| 22856 | break; |
| @@ -22861,11 +23905,11 @@ | |
| 22861 | return 0; |
| 22862 | } |
| 22863 | } |
| 22864 | |
| 22865 | |
| 22866 | if (ISMULT(preg->regparse[n])) { |
| 22867 | |
| 22868 | if (added) { |
| 22869 | |
| 22870 | break; |
| 22871 | } |
| @@ -23034,11 +24078,11 @@ | |
| 23034 | if (regtry(preg, string)) { |
| 23035 | return REG_NOERROR; |
| 23036 | } |
| 23037 | if (*string) { |
| 23038 | nextline: |
| 23039 | if (preg->cflags & REG_NEWLINE) { |
| 23040 | |
| 23041 | string = strchr(string, '\n'); |
| 23042 | if (string) { |
| 23043 | preg->regbol = ++string; |
| 23044 | continue; |
| @@ -23144,11 +24188,11 @@ | |
| 23144 | return NULL; |
| 23145 | } |
| 23146 | |
| 23147 | static int reg_iseol(regex_t *preg, int ch) |
| 23148 | { |
| 23149 | if (preg->cflags & REG_NEWLINE) { |
| 23150 | return ch == '\0' || ch == '\n'; |
| 23151 | } |
| 23152 | else { |
| 23153 | return ch == '\0'; |
| 23154 | } |
| @@ -23555,10 +24599,11 @@ | |
| 23555 | return snprintf(errbuf, errbuf_size, "%s", err); |
| 23556 | } |
| 23557 | |
| 23558 | void jim_regfree(regex_t *preg) |
| 23559 | { |
| 23560 | free(preg->program); |
| 23561 | } |
| 23562 | |
| 23563 | #endif |
| 23564 | #include <string.h> |
| @@ -23566,11 +24611,11 @@ | |
| 23566 | void Jim_SetResultErrno(Jim_Interp *interp, const char *msg) |
| 23567 | { |
| 23568 | Jim_SetResultFormatted(interp, "%s: %s", msg, strerror(Jim_Errno())); |
| 23569 | } |
| 23570 | |
| 23571 | #if defined(__MINGW32__) |
| 23572 | #include <sys/stat.h> |
| 23573 | |
| 23574 | int Jim_Errno(void) |
| 23575 | { |
| 23576 | switch (GetLastError()) { |
| @@ -23743,11 +24788,13 @@ | |
| 23743 | |
| 23744 | |
| 23745 | int Jim_MakeTempFile(Jim_Interp *interp, const char *filename_template, int unlink_file) |
| 23746 | { |
| 23747 | int fd; |
| 23748 | mode_t mask; |
| 23749 | Jim_Obj *filenameObj; |
| 23750 | |
| 23751 | if (filename_template == NULL) { |
| 23752 | const char *tmpdir = getenv("TMPDIR"); |
| 23753 | if (tmpdir == NULL || *tmpdir == '\0' || access(tmpdir, W_OK) != 0) { |
| @@ -24394,10 +25441,14 @@ | |
| 24394 | if (retcode == JIM_ERR) { |
| 24395 | JimPrintErrorMessage(interp); |
| 24396 | } |
| 24397 | if (retcode != JIM_EXIT) { |
| 24398 | JimSetArgv(interp, 0, NULL); |
| 24399 | retcode = Jim_InteractivePrompt(interp); |
| 24400 | } |
| 24401 | } |
| 24402 | else { |
| 24403 | |
| @@ -24416,10 +25467,11 @@ | |
| 24416 | } |
| 24417 | else { |
| 24418 | Jim_SetVariableStr(interp, "argv0", Jim_NewStringObj(interp, argv[1], -1)); |
| 24419 | JimSetArgv(interp, argc - 2, argv + 2); |
| 24420 | if (strcmp(argv[1], "-") == 0) { |
| 24421 | retcode = Jim_Eval(interp, "eval [info source [stdin read] stdin 1]"); |
| 24422 | } else { |
| 24423 | retcode = Jim_EvalFile(interp, argv[1]); |
| 24424 | } |
| 24425 | } |
| 24426 |
| --- autosetup/jimsh0.c | |
| +++ autosetup/jimsh0.c | |
| @@ -1,7 +1,7 @@ | |
| 1 | /* This is single source file, bootstrap version of Jim Tcl. See http://jim.tcl-lang.org/ */ |
| 2 | #define JIM_COMPAT |
| 3 | #define JIM_ANSIC |
| 4 | #define JIM_REGEXP |
| 5 | #define HAVE_NO_AUTOCONF |
| 6 | #define JIM_TINY |
| 7 | #define _JIMAUTOCONF_H |
| @@ -11,11 +11,10 @@ | |
| 11 | #define jim_ext_readdir |
| 12 | #define jim_ext_regexp |
| 13 | #define jim_ext_file |
| 14 | #define jim_ext_glob |
| 15 | #define jim_ext_exec |
| 16 | #define jim_ext_clock |
| 17 | #define jim_ext_array |
| 18 | #define jim_ext_stdlib |
| 19 | #define jim_ext_tclcompat |
| 20 | #if defined(_MSC_VER) |
| @@ -49,11 +48,15 @@ | |
| 48 | #define vfork fork |
| 49 | #define _POSIX_SOURCE |
| 50 | #else |
| 51 | #define _GNU_SOURCE |
| 52 | #endif |
| 53 | #ifndef __ixemul__ |
| 54 | #define HAVE_FORK |
| 55 | #else |
| 56 | #define HAVE_VFORK |
| 57 | #endif |
| 58 | #define HAVE_WAITPID |
| 59 | #define HAVE_ISATTY |
| 60 | #define HAVE_MKSTEMP |
| 61 | #define HAVE_LINK |
| 62 | #define HAVE_SYS_TIME_H |
| @@ -61,11 +64,11 @@ | |
| 64 | #define HAVE_UNISTD_H |
| 65 | #define HAVE_UMASK |
| 66 | #define HAVE_PIPE |
| 67 | #define _FILE_OFFSET_BITS 64 |
| 68 | #endif |
| 69 | #define JIM_VERSION 84 |
| 70 | #ifndef JIM_WIN32COMPAT_H |
| 71 | #define JIM_WIN32COMPAT_H |
| 72 | |
| 73 | |
| 74 | |
| @@ -94,10 +97,13 @@ | |
| 97 | #pragma warning(disable:4146) |
| 98 | #endif |
| 99 | |
| 100 | #include <limits.h> |
| 101 | #define jim_wide _int64 |
| 102 | #ifndef HAVE_LONG_LONG |
| 103 | #define HAVE_LONG_LONG |
| 104 | #endif |
| 105 | #ifndef LLONG_MAX |
| 106 | #define LLONG_MAX 9223372036854775807I64 |
| 107 | #endif |
| 108 | #ifndef LLONG_MIN |
| 109 | #define LLONG_MIN (-LLONG_MAX - 1I64) |
| @@ -108,15 +114,11 @@ | |
| 114 | #define strcasecmp _stricmp |
| 115 | #define strtoull _strtoui64 |
| 116 | |
| 117 | #include <io.h> |
| 118 | |
| 119 | #include <winsock.h> |
| 120 | int gettimeofday(struct timeval *tv, void *unused); |
| 121 | |
| 122 | #define HAVE_OPENDIR |
| 123 | struct dirent { |
| 124 | char *d_name; |
| @@ -158,20 +160,18 @@ | |
| 160 | #ifndef JIM_UTF8 |
| 161 | #include <ctype.h> |
| 162 | |
| 163 | |
| 164 | #define utf8_strlen(S, B) ((B) < 0 ? (int)strlen(S) : (B)) |
| 165 | #define utf8_tounicode(S, CP) (*(CP) = (unsigned char)*(S), 1) |
| 166 | #define utf8_getchars(CP, C) (*(CP) = (C), 1) |
| 167 | #define utf8_upper(C) toupper(C) |
| 168 | #define utf8_title(C) toupper(C) |
| 169 | #define utf8_lower(C) tolower(C) |
| 170 | #define utf8_index(C, I) (I) |
| 171 | #define utf8_charlen(C) 1 |
| 172 | #define utf8_prev_len(S, L) 1 |
| 173 | |
| 174 | #else |
| 175 | |
| 176 | #endif |
| 177 | |
| @@ -222,24 +222,28 @@ | |
| 222 | # else |
| 223 | # define JIM_WIDE_MODIFIER "ld" |
| 224 | # define strtoull strtoul |
| 225 | # endif |
| 226 | #endif |
| 227 | |
| 228 | |
| 229 | #define UCHAR(c) ((unsigned char)(c)) |
| 230 | |
| 231 | |
| 232 | |
| 233 | #define JIM_ABI_VERSION 102 |
| 234 | |
| 235 | |
| 236 | #define JIM_OK 0 |
| 237 | #define JIM_ERR 1 |
| 238 | #define JIM_RETURN 2 |
| 239 | #define JIM_BREAK 3 |
| 240 | #define JIM_CONTINUE 4 |
| 241 | #define JIM_SIGNAL 5 |
| 242 | #define JIM_EXIT 6 |
| 243 | |
| 244 | #define JIM_USAGE -1 |
| 245 | |
| 246 | #define JIM_EVAL 7 |
| 247 | |
| 248 | #define JIM_MAX_CALLFRAME_DEPTH 1000 |
| 249 | #define JIM_MAX_EVAL_DEPTH 2000 |
| @@ -247,11 +251,10 @@ | |
| 251 | |
| 252 | #define JIM_PRIV_FLAG_SHIFT 20 |
| 253 | |
| 254 | #define JIM_NONE 0 |
| 255 | #define JIM_ERRMSG 1 |
| 256 | #define JIM_UNSHARED 4 |
| 257 | #define JIM_MUSTEXIST 8 |
| 258 | #define JIM_NORESULT 16 |
| 259 | |
| 260 | |
| @@ -258,17 +261,26 @@ | |
| 261 | #define JIM_SUBST_NOVAR 1 |
| 262 | #define JIM_SUBST_NOCMD 2 |
| 263 | #define JIM_SUBST_NOESC 4 |
| 264 | #define JIM_SUBST_FLAG 128 |
| 265 | |
| 266 | #define JIM_TAINT_STD 1 /* The "normal" type of taint. Allows for multiple |
| 267 | * types of taint in the future |
| 268 | */ |
| 269 | #define JIM_TAINT_ANY ~0 |
| 270 | |
| 271 | |
| 272 | #define JIM_ENUM_ABBREV 2 |
| 273 | |
| 274 | |
| 275 | #define JIM_CASESENS 0 |
| 276 | #define JIM_NOCASE 1 |
| 277 | #define JIM_OPT_END 2 |
| 278 | |
| 279 | |
| 280 | #define JIM_PATH_LEN 1024 |
| 281 | |
| 282 | |
| 283 | |
| 284 | #define JIM_NOTUSED(V) ((void) V) |
| 285 | |
| 286 | #define JIM_LIBPATH "auto_path" |
| @@ -277,10 +289,11 @@ | |
| 289 | |
| 290 | typedef struct Jim_Stack { |
| 291 | int len; |
| 292 | int maxlen; |
| 293 | void **vector; |
| 294 | void (*freefunc) (void *ptr); |
| 295 | } Jim_Stack; |
| 296 | |
| 297 | |
| 298 | typedef struct Jim_HashEntry { |
| 299 | void *key; |
| @@ -318,55 +331,69 @@ | |
| 331 | } Jim_HashTableIterator; |
| 332 | |
| 333 | |
| 334 | #define JIM_HT_INITIAL_SIZE 16 |
| 335 | |
| 336 | |
| 337 | |
| 338 | #define Jim_FreeEntryVal(ht, entry) \ |
| 339 | if ((ht)->type->valDestructor) \ |
| 340 | (ht)->type->valDestructor((ht)->privdata, (entry)->u.val) |
| 341 | |
| 342 | |
| 343 | #define Jim_SetHashVal(ht, entry, _val_) do { \ |
| 344 | if ((ht)->type->valDup) \ |
| 345 | (entry)->u.val = (ht)->type->valDup((ht)->privdata, (_val_)); \ |
| 346 | else \ |
| 347 | (entry)->u.val = (_val_); \ |
| 348 | } while(0) |
| 349 | |
| 350 | |
| 351 | #define Jim_SetHashIntVal(ht, entry, _val_) (entry)->u.intval = (_val_) |
| 352 | |
| 353 | |
| 354 | #define Jim_FreeEntryKey(ht, entry) \ |
| 355 | if ((ht)->type->keyDestructor) \ |
| 356 | (ht)->type->keyDestructor((ht)->privdata, (entry)->key) |
| 357 | |
| 358 | |
| 359 | #define Jim_SetHashKey(ht, entry, _key_) do { \ |
| 360 | if ((ht)->type->keyDup) \ |
| 361 | (entry)->key = (ht)->type->keyDup((ht)->privdata, (_key_)); \ |
| 362 | else \ |
| 363 | (entry)->key = (void *)(_key_); \ |
| 364 | } while(0) |
| 365 | |
| 366 | |
| 367 | #define Jim_CompareHashKeys(ht, key1, key2) \ |
| 368 | (((ht)->type->keyCompare) ? \ |
| 369 | (ht)->type->keyCompare((ht)->privdata, (key1), (key2)) : \ |
| 370 | (key1) == (key2)) |
| 371 | |
| 372 | |
| 373 | #define Jim_HashKey(ht, key) ((ht)->type->hashFunction(key) + (ht)->uniq) |
| 374 | |
| 375 | |
| 376 | #define Jim_GetHashEntryKey(he) ((he)->key) |
| 377 | |
| 378 | #define Jim_GetHashEntryVal(he) ((he)->u.val) |
| 379 | |
| 380 | #define Jim_GetHashEntryIntVal(he) ((he)->u.intval) |
| 381 | |
| 382 | #define Jim_GetHashTableCollisions(ht) ((ht)->collisions) |
| 383 | |
| 384 | #define Jim_GetHashTableSize(ht) ((ht)->size) |
| 385 | |
| 386 | #define Jim_GetHashTableUsed(ht) ((ht)->used) |
| 387 | |
| 388 | |
| 389 | typedef struct Jim_Obj { |
| 390 | char *bytes; |
| 391 | const struct Jim_ObjType *typePtr; |
| 392 | int refCount; |
| 393 | int length; |
| 394 | unsigned taint; |
| 395 | |
| 396 | union { |
| 397 | |
| 398 | jim_wide wideValue; |
| 399 | |
| @@ -433,27 +460,33 @@ | |
| 460 | } internalRep; |
| 461 | struct Jim_Obj *prevObjPtr; |
| 462 | struct Jim_Obj *nextObjPtr; |
| 463 | } Jim_Obj; |
| 464 | |
| 465 | |
| 466 | |
| 467 | #define Jim_IncrRefCount(objPtr) \ |
| 468 | ++(objPtr)->refCount |
| 469 | |
| 470 | #define Jim_DecrRefCount(interp, objPtr) \ |
| 471 | if (--(objPtr)->refCount <= 0) Jim_FreeObj(interp, objPtr) |
| 472 | |
| 473 | #define Jim_IsShared(objPtr) \ |
| 474 | ((objPtr)->refCount > 1) |
| 475 | |
| 476 | #define Jim_FreeNewObj Jim_FreeObj |
| 477 | |
| 478 | |
| 479 | |
| 480 | #define Jim_FreeIntRep(i,o) \ |
| 481 | if ((o)->typePtr && (o)->typePtr->freeIntRepProc) \ |
| 482 | (o)->typePtr->freeIntRepProc(i, o) |
| 483 | |
| 484 | |
| 485 | |
| 486 | #define Jim_GetIntRepPtr(o) (o)->internalRep.ptr |
| 487 | |
| 488 | |
| 489 | |
| 490 | #define Jim_SetIntRepPtr(o, p) \ |
| 491 | (o)->internalRep.ptr = (p) |
| 492 | |
| @@ -490,12 +523,10 @@ | |
| 523 | int argc; |
| 524 | Jim_Obj *procArgsObjPtr; |
| 525 | Jim_Obj *procBodyObjPtr; |
| 526 | struct Jim_CallFrame *next; |
| 527 | Jim_Obj *nsObj; |
| 528 | Jim_Stack *localCommands; |
| 529 | struct Jim_Obj *tailcallObj; |
| 530 | struct Jim_Cmd *tailcallCmd; |
| 531 | } Jim_CallFrame; |
| 532 | |
| @@ -534,21 +565,31 @@ | |
| 565 | int len; |
| 566 | int maxLen; |
| 567 | unsigned int dummy; |
| 568 | } Jim_Dict; |
| 569 | |
| 570 | #define JIM_CMD_ISPROC 1 |
| 571 | #define JIM_CMD_ISCHANNEL 2 |
| 572 | #define JIM_CMD_ISALIAS 4 |
| 573 | |
| 574 | #define JIM_CMD_NOTAINT 0x100 |
| 575 | |
| 576 | typedef struct Jim_Cmd { |
| 577 | int inUse; |
| 578 | int flags; |
| 579 | struct Jim_Cmd *prevCmd; |
| 580 | Jim_Obj *cmdNameObj; |
| 581 | union { |
| 582 | struct { |
| 583 | |
| 584 | Jim_CmdProc *cmdProc; |
| 585 | Jim_DelCmdProc *delProc; |
| 586 | void *privData; |
| 587 | const char *usage; |
| 588 | const char *help; |
| 589 | short minargs; |
| 590 | short maxargs; |
| 591 | } native; |
| 592 | struct { |
| 593 | |
| 594 | Jim_Obj *argListObjPtr; |
| 595 | Jim_Obj *bodyObjPtr; |
| @@ -573,11 +614,10 @@ | |
| 614 | unsigned int i, j; |
| 615 | } Jim_PrngState; |
| 616 | |
| 617 | typedef struct Jim_Interp { |
| 618 | Jim_Obj *result; |
| 619 | Jim_Obj *currentFilenameObj; |
| 620 | int break_level; |
| 621 | int maxCallFrameDepth; |
| 622 | int maxEvalDepth; |
| 623 | int evalDepth; |
| @@ -601,15 +641,13 @@ | |
| 641 | int local; |
| 642 | int quitting; |
| 643 | int safeexpr; |
| 644 | Jim_Obj *liveList; |
| 645 | Jim_Obj *freeList; |
| 646 | Jim_EvalFrame topEvalFrame; |
| 647 | Jim_EvalFrame *evalFrame; |
| 648 | int procLevel; |
| 649 | Jim_Obj *nullScriptObj; |
| 650 | Jim_Obj *emptyObj; |
| 651 | Jim_Obj *trueObj; |
| 652 | Jim_Obj *falseObj; |
| 653 | unsigned long referenceNextId; |
| @@ -624,11 +662,11 @@ | |
| 662 | Jim_Obj *errorProc; |
| 663 | Jim_Obj *unknown; |
| 664 | Jim_Obj *defer; |
| 665 | Jim_Obj *traceCmdObj; |
| 666 | int unknown_called; |
| 667 | int hasErrorStackTrace; |
| 668 | void *cmdPrivData; /* Used to pass the private data pointer to |
| 669 | a command. It is set to what the user specified |
| 670 | via Jim_CreateCommand(). */ |
| 671 | |
| 672 | Jim_Cmd *oldCmdCache; |
| @@ -636,26 +674,35 @@ | |
| 674 | struct Jim_CallFrame *freeFramesList; |
| 675 | struct Jim_HashTable assocData; |
| 676 | Jim_PrngState *prngState; |
| 677 | struct Jim_HashTable packages; |
| 678 | Jim_Stack *loadHandles; |
| 679 | unsigned taint; |
| 680 | } Jim_Interp; |
| 681 | |
| 682 | |
| 683 | #define Jim_SetResultString(i,s,l) Jim_SetResult(i, Jim_NewStringObj(i,s,l)) |
| 684 | |
| 685 | #define Jim_SetResultInt(i,intval) Jim_SetResult(i, Jim_NewIntObj(i,intval)) |
| 686 | |
| 687 | |
| 688 | #define Jim_SetResultBool(i,b) Jim_SetResultInt(i, b) |
| 689 | |
| 690 | #define Jim_SetEmptyResult(i) Jim_SetResult(i, (i)->emptyObj) |
| 691 | |
| 692 | #define Jim_GetResult(i) ((i)->result) |
| 693 | |
| 694 | #define Jim_CmdPrivData(i) ((i)->cmdPrivData) |
| 695 | |
| 696 | |
| 697 | #define Jim_SetResult(i,o) do { \ |
| 698 | Jim_Obj *_resultObjPtr_ = (o); \ |
| 699 | Jim_IncrRefCount(_resultObjPtr_); \ |
| 700 | Jim_DecrRefCount(i,(i)->result); \ |
| 701 | (i)->result = _resultObjPtr_; \ |
| 702 | } while(0) |
| 703 | |
| 704 | |
| 705 | |
| 706 | #define Jim_GetId(i) (++(i)->id) |
| 707 | |
| 708 | |
| @@ -665,29 +712,39 @@ | |
| 712 | Jim_Obj *objPtr; |
| 713 | Jim_Obj *finalizerCmdNamePtr; |
| 714 | char tag[JIM_REFERENCE_TAGLEN+1]; |
| 715 | } Jim_Reference; |
| 716 | |
| 717 | |
| 718 | |
| 719 | #define Jim_NewEmptyStringObj(i) Jim_NewStringObj(i, "", 0) |
| 720 | |
| 721 | #define Jim_FreeHashTableIterator(iter) Jim_Free(iter) |
| 722 | |
| 723 | #define JIM_EXPORT extern |
| 724 | |
| 725 | |
| 726 | |
| 727 | JIM_EXPORT void *(*Jim_Allocator)(void *ptr, size_t size); |
| 728 | |
| 729 | |
| 730 | #define Jim_Free(P) Jim_Allocator((P), 0) |
| 731 | |
| 732 | #define Jim_Realloc(P, S) Jim_Allocator((P), (S)) |
| 733 | |
| 734 | #define Jim_Alloc(S) Jim_Allocator(NULL, (S)) |
| 735 | |
| 736 | JIM_EXPORT char * Jim_StrDup (const char *s); |
| 737 | |
| 738 | JIM_EXPORT char *Jim_StrDupLen(const char *s, int l); |
| 739 | |
| 740 | |
| 741 | |
| 742 | JIM_EXPORT char **Jim_GetEnviron(void); |
| 743 | |
| 744 | JIM_EXPORT void Jim_SetEnviron(char **env); |
| 745 | |
| 746 | JIM_EXPORT int Jim_MakeTempFile(Jim_Interp *interp, const char *filename_template, int unlink_file); |
| 747 | #ifndef CLOCK_REALTIME |
| 748 | # define CLOCK_REALTIME 0 |
| 749 | #endif |
| 750 | #ifndef CLOCK_MONOTONIC |
| @@ -694,314 +751,485 @@ | |
| 751 | # define CLOCK_MONOTONIC 1 |
| 752 | #endif |
| 753 | #ifndef CLOCK_MONOTONIC_RAW |
| 754 | # define CLOCK_MONOTONIC_RAW CLOCK_MONOTONIC |
| 755 | #endif |
| 756 | |
| 757 | JIM_EXPORT jim_wide Jim_GetTimeUsec(unsigned type); |
| 758 | |
| 759 | |
| 760 | |
| 761 | JIM_EXPORT int Jim_Eval(Jim_Interp *interp, const char *script); |
| 762 | |
| 763 | |
| 764 | |
| 765 | JIM_EXPORT int Jim_EvalSource(Jim_Interp *interp, const char *filename, int lineno, const char *script); |
| 766 | |
| 767 | |
| 768 | #define Jim_Eval_Named(I, S, F, L) Jim_EvalSource((I), (F), (L), (S)) |
| 769 | |
| 770 | |
| 771 | JIM_EXPORT int Jim_EvalGlobal(Jim_Interp *interp, const char *script); |
| 772 | |
| 773 | JIM_EXPORT int Jim_EvalFile(Jim_Interp *interp, const char *filename); |
| 774 | |
| 775 | JIM_EXPORT int Jim_EvalFileGlobal(Jim_Interp *interp, const char *filename); |
| 776 | |
| 777 | JIM_EXPORT int Jim_EvalObj (Jim_Interp *interp, Jim_Obj *scriptObjPtr); |
| 778 | |
| 779 | JIM_EXPORT int Jim_EvalObjVector (Jim_Interp *interp, int objc, |
| 780 | Jim_Obj *const *objv); |
| 781 | |
| 782 | JIM_EXPORT int Jim_EvalObjList(Jim_Interp *interp, Jim_Obj *listObj); |
| 783 | |
| 784 | JIM_EXPORT int Jim_EvalObjPrefix(Jim_Interp *interp, Jim_Obj *prefix, |
| 785 | int objc, Jim_Obj *const *objv); |
| 786 | |
| 787 | #define Jim_EvalPrefix(i, p, oc, ov) Jim_EvalObjPrefix((i), Jim_NewStringObj((i), (p), -1), (oc), (ov)) |
| 788 | |
| 789 | JIM_EXPORT int Jim_EvalNamespace(Jim_Interp *interp, Jim_Obj *scriptObj, Jim_Obj *nsObj); |
| 790 | JIM_EXPORT int Jim_SubstObj (Jim_Interp *interp, Jim_Obj *substObjPtr, |
| 791 | Jim_Obj **resObjPtrPtr, int flags); |
| 792 | |
| 793 | |
| 794 | |
| 795 | JIM_EXPORT Jim_Obj *Jim_GetSourceInfo(Jim_Interp *interp, Jim_Obj *objPtr, |
| 796 | int *lineptr); |
| 797 | |
| 798 | |
| 799 | JIM_EXPORT void Jim_SetSourceInfo(Jim_Interp *interp, Jim_Obj *objPtr, |
| 800 | Jim_Obj *fileNameObj, int lineNumber); |
| 801 | |
| 802 | |
| 803 | |
| 804 | |
| 805 | JIM_EXPORT void Jim_StackInit(Jim_Stack *stack, void (*freefunc) (void *ptr)); |
| 806 | |
| 807 | JIM_EXPORT void Jim_StackFree(Jim_Stack *stack); |
| 808 | |
| 809 | JIM_EXPORT void Jim_StackPush(Jim_Stack *stack, void *element); |
| 810 | |
| 811 | JIM_EXPORT void *Jim_StackPop(Jim_Stack *stack); |
| 812 | |
| 813 | |
| 814 | |
| 815 | JIM_EXPORT int Jim_InitHashTable (Jim_HashTable *ht, |
| 816 | const Jim_HashTableType *type, void *privdata); |
| 817 | |
| 818 | JIM_EXPORT void Jim_ExpandHashTable (Jim_HashTable *ht, |
| 819 | unsigned int size); |
| 820 | |
| 821 | JIM_EXPORT int Jim_AddHashEntry (Jim_HashTable *ht, const void *key, |
| 822 | void *val); |
| 823 | |
| 824 | JIM_EXPORT int Jim_ReplaceHashEntry (Jim_HashTable *ht, |
| 825 | const void *key, void *val); |
| 826 | |
| 827 | JIM_EXPORT int Jim_DeleteHashEntry (Jim_HashTable *ht, |
| 828 | const void *key); |
| 829 | |
| 830 | JIM_EXPORT int Jim_FreeHashTable (Jim_HashTable *ht); |
| 831 | |
| 832 | JIM_EXPORT Jim_HashEntry * Jim_FindHashEntry (Jim_HashTable *ht, |
| 833 | const void *key); |
| 834 | |
| 835 | JIM_EXPORT Jim_HashTableIterator *Jim_GetHashTableIterator |
| 836 | (Jim_HashTable *ht); |
| 837 | |
| 838 | JIM_EXPORT Jim_HashEntry * Jim_NextHashEntry |
| 839 | (Jim_HashTableIterator *iter); |
| 840 | |
| 841 | |
| 842 | |
| 843 | JIM_EXPORT Jim_Obj * Jim_NewObj (Jim_Interp *interp); |
| 844 | |
| 845 | JIM_EXPORT void Jim_FreeObj (Jim_Interp *interp, Jim_Obj *objPtr); |
| 846 | |
| 847 | JIM_EXPORT void Jim_InvalidateStringRep (Jim_Obj *objPtr); |
| 848 | |
| 849 | JIM_EXPORT Jim_Obj * Jim_DuplicateObj (Jim_Interp *interp, |
| 850 | Jim_Obj *objPtr); |
| 851 | |
| 852 | JIM_EXPORT const char * Jim_GetString(Jim_Obj *objPtr, |
| 853 | int *lenPtr); |
| 854 | |
| 855 | JIM_EXPORT const char *Jim_String(Jim_Obj *objPtr); |
| 856 | |
| 857 | JIM_EXPORT int Jim_Length(Jim_Obj *objPtr); |
| 858 | |
| 859 | |
| 860 | |
| 861 | JIM_EXPORT Jim_Obj * Jim_NewStringObj (Jim_Interp *interp, |
| 862 | const char *s, int len); |
| 863 | |
| 864 | JIM_EXPORT Jim_Obj *Jim_NewStringObjUtf8(Jim_Interp *interp, |
| 865 | const char *s, int charlen); |
| 866 | |
| 867 | JIM_EXPORT Jim_Obj * Jim_NewStringObjNoAlloc (Jim_Interp *interp, |
| 868 | char *s, int len); |
| 869 | |
| 870 | JIM_EXPORT void Jim_AppendString (Jim_Interp *interp, Jim_Obj *objPtr, |
| 871 | const char *str, int len); |
| 872 | |
| 873 | JIM_EXPORT void Jim_AppendObj (Jim_Interp *interp, Jim_Obj *objPtr, |
| 874 | Jim_Obj *appendObjPtr); |
| 875 | |
| 876 | JIM_EXPORT void Jim_AppendStrings (Jim_Interp *interp, |
| 877 | Jim_Obj *objPtr, ...); |
| 878 | |
| 879 | JIM_EXPORT int Jim_StringEqObj(Jim_Obj *aObjPtr, Jim_Obj *bObjPtr); |
| 880 | |
| 881 | JIM_EXPORT int Jim_StringMatchObj (Jim_Interp *interp, Jim_Obj *patternObjPtr, |
| 882 | Jim_Obj *objPtr, int nocase); |
| 883 | |
| 884 | JIM_EXPORT Jim_Obj * Jim_StringRangeObj (Jim_Interp *interp, |
| 885 | Jim_Obj *strObjPtr, Jim_Obj *firstObjPtr, |
| 886 | Jim_Obj *lastObjPtr); |
| 887 | |
| 888 | JIM_EXPORT Jim_Obj * Jim_FormatString (Jim_Interp *interp, |
| 889 | Jim_Obj *fmtObjPtr, int objc, Jim_Obj *const *objv); |
| 890 | |
| 891 | JIM_EXPORT Jim_Obj * Jim_ScanString (Jim_Interp *interp, Jim_Obj *strObjPtr, |
| 892 | Jim_Obj *fmtObjPtr, int flags); |
| 893 | |
| 894 | JIM_EXPORT int Jim_CompareStringImmediate (Jim_Interp *interp, |
| 895 | Jim_Obj *objPtr, const char *str); |
| 896 | |
| 897 | JIM_EXPORT int Jim_StringCompareObj(Jim_Interp *interp, Jim_Obj *firstObjPtr, |
| 898 | Jim_Obj *secondObjPtr, int nocase); |
| 899 | |
| 900 | JIM_EXPORT int Jim_Utf8Length(Jim_Interp *interp, Jim_Obj *objPtr); |
| 901 | |
| 902 | |
| 903 | |
| 904 | JIM_EXPORT Jim_Obj * Jim_NewReference (Jim_Interp *interp, |
| 905 | Jim_Obj *objPtr, Jim_Obj *tagPtr, Jim_Obj *cmdNamePtr); |
| 906 | |
| 907 | JIM_EXPORT Jim_Reference * Jim_GetReference (Jim_Interp *interp, |
| 908 | Jim_Obj *objPtr); |
| 909 | |
| 910 | JIM_EXPORT int Jim_SetFinalizer (Jim_Interp *interp, Jim_Obj *objPtr, Jim_Obj *cmdNamePtr); |
| 911 | |
| 912 | JIM_EXPORT int Jim_GetFinalizer (Jim_Interp *interp, Jim_Obj *objPtr, Jim_Obj **cmdNamePtrPtr); |
| 913 | |
| 914 | |
| 915 | |
| 916 | JIM_EXPORT Jim_Interp * Jim_CreateInterp (void); |
| 917 | |
| 918 | JIM_EXPORT void Jim_FreeInterp (Jim_Interp *i); |
| 919 | |
| 920 | JIM_EXPORT int Jim_GetExitCode (Jim_Interp *interp); |
| 921 | |
| 922 | JIM_EXPORT const char *Jim_ReturnCode(int code); |
| 923 | |
| 924 | JIM_EXPORT void Jim_SetResultFormatted(Jim_Interp *interp, const char *format, ...); |
| 925 | |
| 926 | |
| 927 | JIM_EXPORT Jim_Cmd *Jim_RegisterCommand(Jim_Interp *interp, Jim_Obj *cmdNameObj, |
| 928 | Jim_CmdProc *cmdProc, |
| 929 | Jim_DelCmdProc *delProc, |
| 930 | const char *usage, |
| 931 | const char *help, |
| 932 | short minargs, |
| 933 | short maxargs, |
| 934 | int flags, |
| 935 | void *privData); |
| 936 | |
| 937 | JIM_EXPORT int Jim_CreateCommand (Jim_Interp *interp, |
| 938 | const char *cmdName, Jim_CmdProc *cmdProc, void *privData, |
| 939 | Jim_DelCmdProc *delProc); |
| 940 | #define Jim_RegisterSimpleCmd(interp, name, usage, minargs, maxargs, cmdproc) \ |
| 941 | Jim_RegisterCommand(interp, Jim_NewStringObj(interp, name, -1), cmdproc, NULL, usage, NULL, minargs, maxargs, 0, NULL) |
| 942 | |
| 943 | #define Jim_RegisterCmd(interp, name, usage, minargs, maxargs, cmdproc, delproc, privdata, flags) \ |
| 944 | Jim_RegisterCommand(interp, Jim_NewStringObj(interp, name, -1), cmdproc, delproc, usage, NULL, minargs, maxargs, flags, privdata) |
| 945 | |
| 946 | |
| 947 | JIM_EXPORT void Jim_RegisterCoreCommands (Jim_Interp *interp); |
| 948 | |
| 949 | JIM_EXPORT int Jim_CreateCommand (Jim_Interp *interp, |
| 950 | const char *cmdName, Jim_CmdProc *cmdProc, void *privData, |
| 951 | Jim_DelCmdProc *delProc); |
| 952 | |
| 953 | JIM_EXPORT int Jim_DeleteCommand (Jim_Interp *interp, |
| 954 | Jim_Obj *cmdNameObj); |
| 955 | |
| 956 | JIM_EXPORT int Jim_RenameCommand (Jim_Interp *interp, |
| 957 | Jim_Obj *oldNameObj, Jim_Obj *newNameObj); |
| 958 | |
| 959 | JIM_EXPORT Jim_Cmd * Jim_GetCommand (Jim_Interp *interp, |
| 960 | Jim_Obj *objPtr, int flags); |
| 961 | |
| 962 | |
| 963 | JIM_EXPORT int Jim_SetVariable (Jim_Interp *interp, |
| 964 | Jim_Obj *nameObjPtr, Jim_Obj *valObjPtr); |
| 965 | |
| 966 | JIM_EXPORT int Jim_SetVariableStr (Jim_Interp *interp, |
| 967 | const char *name, Jim_Obj *objPtr); |
| 968 | |
| 969 | JIM_EXPORT int Jim_SetGlobalVariableStr (Jim_Interp *interp, |
| 970 | const char *name, Jim_Obj *objPtr); |
| 971 | |
| 972 | JIM_EXPORT int Jim_SetVariableStrWithStr (Jim_Interp *interp, |
| 973 | const char *name, const char *val); |
| 974 | |
| 975 | JIM_EXPORT int Jim_SetVariableLink (Jim_Interp *interp, |
| 976 | Jim_Obj *nameObjPtr, Jim_Obj *targetNameObjPtr, |
| 977 | Jim_CallFrame *targetCallFrame); |
| 978 | |
| 979 | JIM_EXPORT Jim_Obj * Jim_MakeGlobalNamespaceName(Jim_Interp *interp, |
| 980 | Jim_Obj *nameObjPtr); |
| 981 | JIM_EXPORT Jim_Obj * Jim_GetVariable (Jim_Interp *interp, |
| 982 | Jim_Obj *nameObjPtr, int flags); |
| 983 | |
| 984 | JIM_EXPORT Jim_Obj * Jim_GetGlobalVariable (Jim_Interp *interp, |
| 985 | Jim_Obj *nameObjPtr, int flags); |
| 986 | |
| 987 | JIM_EXPORT Jim_Obj * Jim_GetVariableStr (Jim_Interp *interp, |
| 988 | const char *name, int flags); |
| 989 | |
| 990 | JIM_EXPORT Jim_Obj * Jim_GetGlobalVariableStr (Jim_Interp *interp, |
| 991 | const char *name, int flags); |
| 992 | |
| 993 | JIM_EXPORT int Jim_UnsetVariable (Jim_Interp *interp, |
| 994 | Jim_Obj *nameObjPtr, int flags); |
| 995 | |
| 996 | |
| 997 | |
| 998 | JIM_EXPORT Jim_CallFrame *Jim_GetCallFrameByLevel(Jim_Interp *interp, |
| 999 | Jim_Obj *levelObjPtr); |
| 1000 | |
| 1001 | |
| 1002 | |
| 1003 | JIM_EXPORT int Jim_Collect (Jim_Interp *interp); |
| 1004 | |
| 1005 | JIM_EXPORT void Jim_CollectIfNeeded (Jim_Interp *interp); |
| 1006 | |
| 1007 | |
| 1008 | |
| 1009 | JIM_EXPORT int Jim_GetIndex (Jim_Interp *interp, Jim_Obj *objPtr, |
| 1010 | int *indexPtr); |
| 1011 | |
| 1012 | |
| 1013 | |
| 1014 | JIM_EXPORT Jim_Obj * Jim_NewListObj (Jim_Interp *interp, |
| 1015 | Jim_Obj *const *elements, int len); |
| 1016 | |
| 1017 | JIM_EXPORT void Jim_ListInsertElements (Jim_Interp *interp, |
| 1018 | Jim_Obj *listPtr, int listindex, int objc, Jim_Obj *const *objVec); |
| 1019 | |
| 1020 | JIM_EXPORT void Jim_ListAppendElement (Jim_Interp *interp, |
| 1021 | Jim_Obj *listPtr, Jim_Obj *objPtr); |
| 1022 | |
| 1023 | JIM_EXPORT void Jim_ListAppendList (Jim_Interp *interp, |
| 1024 | Jim_Obj *listPtr, Jim_Obj *appendListPtr); |
| 1025 | |
| 1026 | JIM_EXPORT int Jim_ListLength (Jim_Interp *interp, Jim_Obj *objPtr); |
| 1027 | |
| 1028 | JIM_EXPORT int Jim_ListIndex (Jim_Interp *interp, Jim_Obj *listPrt, |
| 1029 | int listindex, Jim_Obj **objPtrPtr, int seterr); |
| 1030 | |
| 1031 | JIM_EXPORT Jim_Obj *Jim_ListGetIndex(Jim_Interp *interp, Jim_Obj *listPtr, int idx); |
| 1032 | |
| 1033 | JIM_EXPORT int Jim_SetListIndex (Jim_Interp *interp, |
| 1034 | Jim_Obj *varNamePtr, Jim_Obj *const *indexv, int indexc, |
| 1035 | Jim_Obj *newObjPtr); |
| 1036 | |
| 1037 | JIM_EXPORT Jim_Obj * Jim_ConcatObj (Jim_Interp *interp, int objc, |
| 1038 | Jim_Obj *const *objv); |
| 1039 | |
| 1040 | JIM_EXPORT Jim_Obj *Jim_ListJoin(Jim_Interp *interp, |
| 1041 | Jim_Obj *listObjPtr, const char *joinStr, int joinStrLen); |
| 1042 | |
| 1043 | |
| 1044 | |
| 1045 | JIM_EXPORT Jim_Obj * Jim_NewDictObj (Jim_Interp *interp, |
| 1046 | Jim_Obj *const *elements, int len); |
| 1047 | |
| 1048 | JIM_EXPORT int Jim_DictKey (Jim_Interp *interp, Jim_Obj *dictPtr, |
| 1049 | Jim_Obj *keyPtr, Jim_Obj **objPtrPtr, int flags); |
| 1050 | |
| 1051 | JIM_EXPORT int Jim_DictKeysVector (Jim_Interp *interp, |
| 1052 | Jim_Obj *dictPtr, Jim_Obj *const *keyv, int keyc, |
| 1053 | Jim_Obj **objPtrPtr, int flags); |
| 1054 | JIM_EXPORT int Jim_SetDictKeysVector (Jim_Interp *interp, |
| 1055 | Jim_Obj *varNamePtr, Jim_Obj *const *keyv, int keyc, |
| 1056 | Jim_Obj *newObjPtr, int flags); |
| 1057 | |
| 1058 | JIM_EXPORT Jim_Obj **Jim_DictPairs(Jim_Interp *interp, |
| 1059 | Jim_Obj *dictPtr, int *len); |
| 1060 | |
| 1061 | JIM_EXPORT int Jim_DictAddElement(Jim_Interp *interp, Jim_Obj *objPtr, |
| 1062 | Jim_Obj *keyObjPtr, Jim_Obj *valueObjPtr); |
| 1063 | |
| 1064 | #define JIM_DICTMATCH_KEYS 0x0001 |
| 1065 | #define JIM_DICTMATCH_VALUES 0x002 |
| 1066 | |
| 1067 | |
| 1068 | JIM_EXPORT int Jim_DictMatchTypes(Jim_Interp *interp, Jim_Obj *objPtr, Jim_Obj *patternObj, int match_type, int return_types); |
| 1069 | |
| 1070 | JIM_EXPORT int Jim_DictSize(Jim_Interp *interp, Jim_Obj *objPtr); |
| 1071 | |
| 1072 | JIM_EXPORT int Jim_DictInfo(Jim_Interp *interp, Jim_Obj *objPtr); |
| 1073 | |
| 1074 | JIM_EXPORT Jim_Obj *Jim_DictMerge(Jim_Interp *interp, int objc, Jim_Obj *const *objv); |
| 1075 | |
| 1076 | |
| 1077 | |
| 1078 | JIM_EXPORT int Jim_GetReturnCode (Jim_Interp *interp, Jim_Obj *objPtr, |
| 1079 | int *intPtr); |
| 1080 | |
| 1081 | |
| 1082 | |
| 1083 | JIM_EXPORT int Jim_EvalExpression (Jim_Interp *interp, |
| 1084 | Jim_Obj *exprObjPtr); |
| 1085 | |
| 1086 | JIM_EXPORT int Jim_GetBoolFromExpr (Jim_Interp *interp, |
| 1087 | Jim_Obj *exprObjPtr, int *boolPtr); |
| 1088 | |
| 1089 | |
| 1090 | |
| 1091 | JIM_EXPORT int Jim_GetBoolean(Jim_Interp *interp, Jim_Obj *objPtr, |
| 1092 | int *booleanPtr); |
| 1093 | |
| 1094 | |
| 1095 | |
| 1096 | JIM_EXPORT int Jim_GetWide (Jim_Interp *interp, Jim_Obj *objPtr, |
| 1097 | jim_wide *widePtr); |
| 1098 | |
| 1099 | JIM_EXPORT int Jim_GetWideExpr(Jim_Interp *interp, Jim_Obj *objPtr, |
| 1100 | jim_wide *widePtr); |
| 1101 | |
| 1102 | JIM_EXPORT int Jim_GetLong (Jim_Interp *interp, Jim_Obj *objPtr, |
| 1103 | long *longPtr); |
| 1104 | |
| 1105 | #define Jim_NewWideObj Jim_NewIntObj |
| 1106 | |
| 1107 | JIM_EXPORT Jim_Obj * Jim_NewIntObj (Jim_Interp *interp, |
| 1108 | jim_wide wideValue); |
| 1109 | |
| 1110 | |
| 1111 | |
| 1112 | JIM_EXPORT int Jim_GetDouble(Jim_Interp *interp, Jim_Obj *objPtr, |
| 1113 | double *doublePtr); |
| 1114 | |
| 1115 | JIM_EXPORT void Jim_SetDouble(Jim_Interp *interp, Jim_Obj *objPtr, |
| 1116 | double doubleValue); |
| 1117 | |
| 1118 | JIM_EXPORT Jim_Obj * Jim_NewDoubleObj(Jim_Interp *interp, double doubleValue); |
| 1119 | |
| 1120 | |
| 1121 | |
| 1122 | JIM_EXPORT void Jim_WrongNumArgs (Jim_Interp *interp, int argc, |
| 1123 | Jim_Obj *const *argv, const char *msg); |
| 1124 | JIM_EXPORT int Jim_GetEnum (Jim_Interp *interp, Jim_Obj *objPtr, |
| 1125 | const char * const *tablePtr, int *indexPtr, const char *name, int flags); |
| 1126 | |
| 1127 | JIM_EXPORT int Jim_CheckShowCommands(Jim_Interp *interp, Jim_Obj *objPtr, |
| 1128 | const char *const *tablePtr); |
| 1129 | |
| 1130 | JIM_EXPORT int Jim_ScriptIsComplete(Jim_Interp *interp, |
| 1131 | Jim_Obj *scriptObj, char *stateCharPtr); |
| 1132 | |
| 1133 | JIM_EXPORT int Jim_FindByName(const char *name, const char * const array[], size_t len); |
| 1134 | |
| 1135 | |
| 1136 | typedef void (Jim_InterpDeleteProc)(Jim_Interp *interp, void *data); |
| 1137 | |
| 1138 | JIM_EXPORT void * Jim_GetAssocData(Jim_Interp *interp, const char *key); |
| 1139 | |
| 1140 | JIM_EXPORT int Jim_SetAssocData(Jim_Interp *interp, const char *key, |
| 1141 | Jim_InterpDeleteProc *delProc, void *data); |
| 1142 | |
| 1143 | JIM_EXPORT int Jim_DeleteAssocData(Jim_Interp *interp, const char *key); |
| 1144 | |
| 1145 | JIM_EXPORT int Jim_CheckAbiVersion(Jim_Interp *interp, int abi_version); |
| 1146 | |
| 1147 | |
| 1148 | |
| 1149 | |
| 1150 | |
| 1151 | JIM_EXPORT int Jim_PackageProvide (Jim_Interp *interp, |
| 1152 | const char *name, const char *ver, int flags); |
| 1153 | |
| 1154 | JIM_EXPORT int Jim_PackageRequire (Jim_Interp *interp, |
| 1155 | const char *name, int flags); |
| 1156 | |
| 1157 | #define Jim_PackageProvideCheck(INTERP, NAME) \ |
| 1158 | if (Jim_CheckAbiVersion(INTERP, JIM_ABI_VERSION) == JIM_ERR || Jim_PackageProvide(INTERP, NAME, "1.0", JIM_ERRMSG)) \ |
| 1159 | return JIM_ERR |
| 1160 | |
| 1161 | |
| 1162 | |
| 1163 | JIM_EXPORT void Jim_MakeErrorMessage (Jim_Interp *interp); |
| 1164 | |
| 1165 | |
| 1166 | |
| 1167 | JIM_EXPORT int Jim_InteractivePrompt (Jim_Interp *interp); |
| 1168 | |
| 1169 | JIM_EXPORT void Jim_HistoryLoad(const char *filename); |
| 1170 | |
| 1171 | JIM_EXPORT void Jim_HistorySave(const char *filename); |
| 1172 | |
| 1173 | JIM_EXPORT char *Jim_HistoryGetline(Jim_Interp *interp, const char *prompt); |
| 1174 | |
| 1175 | JIM_EXPORT void Jim_HistorySetCompletion(Jim_Interp *interp, Jim_Obj *completionCommandObj); |
| 1176 | |
| 1177 | JIM_EXPORT void Jim_HistorySetHints(Jim_Interp *interp, Jim_Obj *hintsCommandObj); |
| 1178 | |
| 1179 | JIM_EXPORT void Jim_HistoryAdd(const char *line); |
| 1180 | |
| 1181 | JIM_EXPORT void Jim_HistoryShow(void); |
| 1182 | |
| 1183 | JIM_EXPORT void Jim_HistorySetMaxLen(int length); |
| 1184 | |
| 1185 | JIM_EXPORT int Jim_HistoryGetMaxLen(void); |
| 1186 | |
| 1187 | |
| 1188 | |
| 1189 | JIM_EXPORT int Jim_InitStaticExtensions(Jim_Interp *interp); |
| 1190 | |
| 1191 | JIM_EXPORT int Jim_StringToWide(const char *str, jim_wide *widePtr, int base); |
| 1192 | |
| 1193 | JIM_EXPORT int Jim_IsBigEndian(void); |
| 1194 | |
| 1195 | #define Jim_CheckSignal(i) ((i)->signal_level && (i)->sigmask) |
| 1196 | |
| 1197 | JIM_EXPORT void Jim_SignalSetIgnored(jim_wide mask); |
| 1198 | |
| 1199 | |
| 1200 | |
| 1201 | JIM_EXPORT int Jim_LoadLibrary(Jim_Interp *interp, const char *pathName); |
| 1202 | |
| 1203 | JIM_EXPORT void Jim_FreeLoadHandles(Jim_Interp *interp); |
| 1204 | |
| 1205 | |
| 1206 | |
| 1207 | JIM_EXPORT int Jim_AioFilehandle(Jim_Interp *interp, Jim_Obj *command); |
| 1208 | |
| 1209 | |
| 1210 | |
| 1211 | JIM_EXPORT int Jim_IsDict(Jim_Obj *objPtr); |
| 1212 | |
| 1213 | JIM_EXPORT int Jim_IsList(Jim_Obj *objPtr); |
| 1214 | |
| 1215 | |
| 1216 | JIM_EXPORT void Jim_SetTaintError(Jim_Interp *interp, int cmdargs, Jim_Obj *const *argv); |
| 1217 | JIM_EXPORT int Jim_CalcTaint(int argc, Jim_Obj *const *argv); |
| 1218 | |
| 1219 | #ifdef JIM_TAINT |
| 1220 | #define Jim_CheckTaint(i, t) ((i)->taint & (t)) |
| 1221 | #define Jim_TaintObj(o,t) (o)->taint |= (t) |
| 1222 | #define Jim_UntaintObj(o) (o)->taint = 0 |
| 1223 | #define Jim_GetObjTaint(o) (o)->taint |
| 1224 | #else |
| 1225 | #define Jim_CheckTaint(i, t) 0 |
| 1226 | #define Jim_TaintObj(o,t) |
| 1227 | #define Jim_UntaintObj(o) |
| 1228 | #define Jim_GetObjTaint(o) 0 |
| 1229 | #endif |
| 1230 | |
| 1231 | #ifdef __cplusplus |
| 1232 | } |
| 1233 | #endif |
| 1234 | |
| 1235 | #endif |
| @@ -1015,10 +1243,14 @@ | |
| 1243 | #endif |
| 1244 | |
| 1245 | |
| 1246 | #define JIM_MODFLAG_HIDDEN 0x0001 |
| 1247 | #define JIM_MODFLAG_FULLARGV 0x0002 |
| 1248 | #define JIM_MODFLAG_NOTAINT 0x0004 |
| 1249 | |
| 1250 | #define JIM_SUBCMD_BADARGS -1 |
| 1251 | #define JIM_SUBCMD_TAINTED -2 |
| 1252 | |
| 1253 | |
| 1254 | |
| 1255 | typedef int jim_subcmd_function(Jim_Interp *interp, int argc, Jim_Obj *const *argv); |
| 1256 | |
| @@ -1040,10 +1272,13 @@ | |
| 1272 | int Jim_SubCmdProc(Jim_Interp *interp, int argc, Jim_Obj *const *argv); |
| 1273 | |
| 1274 | int Jim_CallSubCmd(Jim_Interp *interp, const jim_subcmd_type *ct, int argc, Jim_Obj *const *argv); |
| 1275 | |
| 1276 | void Jim_SubCmdArgError(Jim_Interp *interp, const jim_subcmd_type *ct, Jim_Obj *subcmd); |
| 1277 | |
| 1278 | Jim_Cmd *Jim_RegisterSubCmd(Jim_Interp *interp, const char *cmdname, |
| 1279 | const jim_subcmd_type *command_table, Jim_DelCmdProc *delProc); |
| 1280 | |
| 1281 | #ifdef __cplusplus |
| 1282 | } |
| 1283 | #endif |
| 1284 | |
| @@ -1074,10 +1309,11 @@ | |
| 1309 | int regstart; |
| 1310 | int reganch; |
| 1311 | int regmust; |
| 1312 | int regmlen; |
| 1313 | int *program; |
| 1314 | char *exp; |
| 1315 | |
| 1316 | |
| 1317 | const char *regparse; |
| 1318 | int p; |
| 1319 | int proglen; |
| @@ -1094,14 +1330,18 @@ | |
| 1330 | } regexp; |
| 1331 | |
| 1332 | typedef regexp regex_t; |
| 1333 | |
| 1334 | #define REG_EXTENDED 0 |
| 1335 | #define REG_ICASE 2 |
| 1336 | #define REG_NEWLINE_ANCHOR 4 |
| 1337 | #define REG_NEWLINE_STOP 8 |
| 1338 | |
| 1339 | #define REG_NEWLINE (REG_NEWLINE_ANCHOR | REG_NEWLINE_STOP) |
| 1340 | |
| 1341 | #define REG_NOTBOL 16 |
| 1342 | #define REG_EXPANDED 32 |
| 1343 | |
| 1344 | enum { |
| 1345 | REG_NOERROR, |
| 1346 | REG_NOMATCH, |
| 1347 | REG_BADPAT, |
| @@ -1162,11 +1402,11 @@ | |
| 1402 | |
| 1403 | int Jim_OpenForWrite(const char *filename, int append); |
| 1404 | |
| 1405 | int Jim_OpenForRead(const char *filename); |
| 1406 | |
| 1407 | #if defined(__MINGW32__) || defined(_WIN32) |
| 1408 | #ifndef STRICT |
| 1409 | #define STRICT |
| 1410 | #endif |
| 1411 | #define WIN32_LEAN_AND_MEAN |
| 1412 | #include <windows.h> |
| @@ -1197,10 +1437,16 @@ | |
| 1437 | |
| 1438 | typedef struct __stat64 jim_stat_t; |
| 1439 | #define Jim_Stat _stat64 |
| 1440 | #define Jim_FileStat _fstat64 |
| 1441 | #define Jim_Lseek _lseeki64 |
| 1442 | #define O_TEXT _O_TEXT |
| 1443 | #define O_BINARY _O_BINARY |
| 1444 | #define Jim_SetMode _setmode |
| 1445 | #ifndef STDIN_FILENO |
| 1446 | #define STDIN_FILENO 0 |
| 1447 | #endif |
| 1448 | |
| 1449 | #else |
| 1450 | #if defined(HAVE_STAT64) |
| 1451 | typedef struct stat64 jim_stat_t; |
| 1452 | #define Jim_Stat stat64 |
| @@ -1239,15 +1485,24 @@ | |
| 1485 | |
| 1486 | #ifndef HAVE_EXECVPE |
| 1487 | #define execvpe(ARG0, ARGV, ENV) execvp(ARG0, ARGV) |
| 1488 | #endif |
| 1489 | #endif |
| 1490 | |
| 1491 | #ifndef O_TEXT |
| 1492 | #define O_TEXT 0 |
| 1493 | #endif |
| 1494 | |
| 1495 | #endif |
| 1496 | |
| 1497 | # ifndef MAXPATHLEN |
| 1498 | # ifdef PATH_MAX |
| 1499 | # define MAXPATHLEN PATH_MAX |
| 1500 | # else |
| 1501 | # define MAXPATHLEN JIM_PATH_LEN |
| 1502 | # endif |
| 1503 | # endif |
| 1504 | |
| 1505 | |
| 1506 | int Jim_FileStoreStatData(Jim_Interp *interp, Jim_Obj *varName, const jim_stat_t *sb); |
| 1507 | |
| 1508 | #endif |
| @@ -1291,12 +1546,18 @@ | |
| 1546 | "\n" |
| 1547 | " if {[exists jim::argv0]} {\n" |
| 1548 | " if {[string match \"*/*\" $jim::argv0]} {\n" |
| 1549 | " set jim::exe [file join [pwd] $jim::argv0]\n" |
| 1550 | " } else {\n" |
| 1551 | " set jim::argv0 [file tail $jim::argv0]\n" |
| 1552 | " set path [split [env PATH \"\"] $tcl_platform(pathSeparator)]\n" |
| 1553 | " if {$tcl_platform(platform) eq \"windows\"} {\n" |
| 1554 | "\n" |
| 1555 | " set path [lmap p [list \"\" {*}$path] { string map {\\\\ /} $p }]\n" |
| 1556 | " }\n" |
| 1557 | " foreach p $path {\n" |
| 1558 | " set exec [file join [pwd] $p $jim::argv0]\n" |
| 1559 | " if {[file executable $exec]} {\n" |
| 1560 | " set jim::exe $exec\n" |
| 1561 | " break\n" |
| 1562 | " }\n" |
| 1563 | " }\n" |
| @@ -1387,12 +1648,12 @@ | |
| 1648 | "set tcl::stdhint_col $tcl::stdhint_cols(lcyan)\n" |
| 1649 | "\n" |
| 1650 | "\n" |
| 1651 | "proc tcl::stdhint {string} {\n" |
| 1652 | " set result \"\"\n" |
| 1653 | " lassign $string cmd arg\n" |
| 1654 | " if {[llength $string] >= 2} {\n" |
| 1655 | " if {$cmd in $::tcl::stdhint_commands || [info channel $cmd] ne \"\"} {\n" |
| 1656 | " catch {\n" |
| 1657 | " set help [$cmd -help $arg]\n" |
| 1658 | " if {[string match \"Usage: $cmd *\" $help]} {\n" |
| 1659 | " set n [llength $string]\n" |
| @@ -1410,10 +1671,27 @@ | |
| 1671 | " }\n" |
| 1672 | " set result [list $prefix$hint {*}$::tcl::stdhint_col]\n" |
| 1673 | " }\n" |
| 1674 | " }\n" |
| 1675 | " }\n" |
| 1676 | " } else {\n" |
| 1677 | " catch {\n" |
| 1678 | " if {[exists -alias $cmd] && [llength [info alias $cmd]] == 1} {\n" |
| 1679 | "\n" |
| 1680 | "\n" |
| 1681 | "\n" |
| 1682 | " set help [info usage [info alias $cmd]]\n" |
| 1683 | " } else {\n" |
| 1684 | " set help [info usage $cmd]\n" |
| 1685 | " }\n" |
| 1686 | " set hint [join [lrange $help 1 end]]\n" |
| 1687 | " set prefix \" \"\n" |
| 1688 | " if {[string match \"* \" $string]} {\n" |
| 1689 | " set prefix \"\"\n" |
| 1690 | " }\n" |
| 1691 | " set result [list $prefix$hint {*}$::tcl::stdhint_col]\n" |
| 1692 | " }\n" |
| 1693 | " }\n" |
| 1694 | " return $result\n" |
| 1695 | "}\n" |
| 1696 | "\n" |
| 1697 | "_jimsh_init\n" |
| @@ -1856,11 +2134,11 @@ | |
| 2134 | " }\n" |
| 2135 | " -bu* {\n" |
| 2136 | " $f buffering $v\n" |
| 2137 | " }\n" |
| 2138 | " -tr* {\n" |
| 2139 | " $f translation $v\n" |
| 2140 | " }\n" |
| 2141 | " default {\n" |
| 2142 | " return -code error \"fconfigure: unknown option $n\"\n" |
| 2143 | " }\n" |
| 2144 | " }\n" |
| @@ -1873,11 +2151,13 @@ | |
| 2151 | " tailcall {*}$args\n" |
| 2152 | "}\n" |
| 2153 | "\n" |
| 2154 | "\n" |
| 2155 | "\n" |
| 2156 | "\n" |
| 2157 | "\n" |
| 2158 | "proc parray {arrayname {pattern *} {puts puts} args} {\n" |
| 2159 | " upvar $arrayname a\n" |
| 2160 | "\n" |
| 2161 | " set max 0\n" |
| 2162 | " foreach name [array names a $pattern]] {\n" |
| 2163 | " if {[string length $name] > $max} {\n" |
| @@ -1885,11 +2165,11 @@ | |
| 2165 | " }\n" |
| 2166 | " }\n" |
| 2167 | " incr max [string length $arrayname]\n" |
| 2168 | " incr max 2\n" |
| 2169 | " foreach name [lsort [array names a $pattern]] {\n" |
| 2170 | " $puts {*}$args [format \"%-${max}s = %s\" $arrayname\\($name\\) $a($name)]\n" |
| 2171 | " }\n" |
| 2172 | "}\n" |
| 2173 | "\n" |
| 2174 | "\n" |
| 2175 | "proc {file copy} {{force {}} source target} {\n" |
| @@ -1945,13 +2225,10 @@ | |
| 2225 | " }\n" |
| 2226 | " lambda {cmd args} {f pids} {\n" |
| 2227 | " if {$cmd eq \"pid\"} {\n" |
| 2228 | " return $pids\n" |
| 2229 | " }\n" |
| 2230 | " if {$cmd eq \"close\"} {\n" |
| 2231 | " $f close\n" |
| 2232 | "\n" |
| 2233 | " set retopts {}\n" |
| 2234 | " foreach p $pids {\n" |
| @@ -2047,18 +2324,21 @@ | |
| 2324 | #ifdef HAVE_TERMIOS_H |
| 2325 | #endif |
| 2326 | |
| 2327 | |
| 2328 | #define AIO_CMD_LEN 32 |
| 2329 | #define AIO_DEFAULT_RBUF_LEN 256 |
| 2330 | #define AIO_DEFAULT_WBUF_LIMIT (64 * 1024) |
| 2331 | |
| 2332 | #define AIO_KEEPOPEN 1 |
| 2333 | #define AIO_NODELETE 2 |
| 2334 | #define AIO_EOF 4 |
| 2335 | #define AIO_WBUF_NONE 8 |
| 2336 | #define AIO_NONBLOCK 16 |
| 2337 | #define AIO_NOTAINT 32 |
| 2338 | |
| 2339 | #define AIO_ONEREAD 32 |
| 2340 | |
| 2341 | enum wbuftype { |
| 2342 | WBUF_OPT_NONE, |
| 2343 | WBUF_OPT_LINE, |
| 2344 | WBUF_OPT_FULL, |
| @@ -2076,14 +2356,10 @@ | |
| 2356 | #define UNIX_SOCKETS 1 |
| 2357 | #else |
| 2358 | #define UNIX_SOCKETS 0 |
| 2359 | #endif |
| 2360 | |
| 2361 | |
| 2362 | |
| 2363 | |
| 2364 | static int JimReadableTimeout(int fd, long ms) |
| 2365 | { |
| @@ -2115,30 +2391,40 @@ | |
| 2391 | typedef struct { |
| 2392 | int (*writer)(struct AioFile *af, const char *buf, int len); |
| 2393 | int (*reader)(struct AioFile *af, char *buf, int len, int pending); |
| 2394 | int (*error)(const struct AioFile *af); |
| 2395 | const char *(*strerror)(struct AioFile *af); |
| 2396 | } JimAioFopsType; |
| 2397 | |
| 2398 | typedef struct AioFile |
| 2399 | { |
| 2400 | Jim_Obj *filename; |
| 2401 | int wbuft; |
| 2402 | int flags; |
| 2403 | long timeout; |
| 2404 | int fd; |
| 2405 | unsigned taintsource; |
| 2406 | unsigned taintsink; |
| 2407 | int addr_family; |
| 2408 | void *ssl; |
| 2409 | const JimAioFopsType *fops; |
| 2410 | Jim_Obj *readbuf; |
| 2411 | Jim_Obj *writebuf; |
| 2412 | char *rbuf; |
| 2413 | size_t rbuf_len; |
| 2414 | size_t wbuf_limit; |
| 2415 | } AioFile; |
| 2416 | |
| 2417 | static void aio_consume(Jim_Obj *objPtr, int n); |
| 2418 | |
| 2419 | static int stdio_writer(struct AioFile *af, const char *buf, int len) |
| 2420 | { |
| 2421 | int ret = write(af->fd, buf, len); |
| 2422 | if (ret < 0 && errno == EPIPE) { |
| 2423 | aio_consume(af->writebuf, Jim_Length(af->writebuf)); |
| 2424 | } |
| 2425 | return ret; |
| 2426 | } |
| 2427 | |
| 2428 | static int stdio_reader(struct AioFile *af, char *buf, int len, int nb) |
| 2429 | { |
| 2430 | if (nb || af->timeout == 0 || JimReadableTimeout(af->fd, af->timeout) == JIM_OK) { |
| @@ -2186,11 +2472,10 @@ | |
| 2472 | static const JimAioFopsType stdio_fops = { |
| 2473 | stdio_writer, |
| 2474 | stdio_reader, |
| 2475 | stdio_error, |
| 2476 | stdio_strerror, |
| 2477 | }; |
| 2478 | |
| 2479 | |
| 2480 | static void aio_set_nonblocking(AioFile *af, int nb) |
| 2481 | { |
| @@ -2219,10 +2504,11 @@ | |
| 2504 | } |
| 2505 | return old; |
| 2506 | } |
| 2507 | |
| 2508 | static int JimAioSubCmdProc(Jim_Interp *interp, int argc, Jim_Obj *const *argv); |
| 2509 | static void JimAioSetTaint(AioFile *af, int taintsource, int taintsink); |
| 2510 | static AioFile *JimMakeChannel(Jim_Interp *interp, int fd, Jim_Obj *filename, |
| 2511 | const char *hdlfmt, int family, int flags); |
| 2512 | |
| 2513 | |
| 2514 | static const char *JimAioErrorString(AioFile *af) |
| @@ -2271,11 +2557,26 @@ | |
| 2557 | memmove(objPtr->bytes, objPtr->bytes + n, objPtr->length - n + 1); |
| 2558 | objPtr->length -= n; |
| 2559 | } |
| 2560 | |
| 2561 | |
| 2562 | static int aio_flush(Jim_Interp *interp, AioFile *af); |
| 2563 | |
| 2564 | #ifdef jim_ext_eventloop |
| 2565 | static int aio_autoflush(Jim_Interp *interp, void *clientData, int mask) |
| 2566 | { |
| 2567 | AioFile *af = clientData; |
| 2568 | |
| 2569 | aio_flush(interp, af); |
| 2570 | if (Jim_Length(af->writebuf) == 0) { |
| 2571 | |
| 2572 | return -1; |
| 2573 | } |
| 2574 | return 0; |
| 2575 | } |
| 2576 | #endif |
| 2577 | |
| 2578 | |
| 2579 | static int aio_flush(Jim_Interp *interp, AioFile *af) |
| 2580 | { |
| 2581 | int len; |
| 2582 | const char *pt = Jim_GetString(af->writebuf, &len); |
| @@ -2306,23 +2607,11 @@ | |
| 2607 | } |
| 2608 | } |
| 2609 | return JIM_OK; |
| 2610 | } |
| 2611 | |
| 2612 | static int aio_read_len(Jim_Interp *interp, AioFile *af, unsigned flags, int neededLen) |
| 2613 | { |
| 2614 | if (!af->readbuf) { |
| 2615 | af->readbuf = Jim_NewStringObj(interp, NULL, 0); |
| 2616 | } |
| 2617 | |
| @@ -2336,24 +2625,33 @@ | |
| 2625 | while (neededLen && !aio_eof(af)) { |
| 2626 | int retval; |
| 2627 | int readlen; |
| 2628 | |
| 2629 | if (neededLen == -1) { |
| 2630 | readlen = af->rbuf_len; |
| 2631 | } |
| 2632 | else { |
| 2633 | readlen = (neededLen > af->rbuf_len ? af->rbuf_len : neededLen); |
| 2634 | } |
| 2635 | |
| 2636 | if (!af->rbuf) { |
| 2637 | af->rbuf = Jim_Alloc(af->rbuf_len); |
| 2638 | } |
| 2639 | retval = af->fops->reader(af, af->rbuf, readlen, flags & AIO_NONBLOCK); |
| 2640 | if (retval > 0) { |
| 2641 | if (retval) { |
| 2642 | Jim_AppendString(interp, af->readbuf, af->rbuf, retval); |
| 2643 | } |
| 2644 | if (neededLen != -1) { |
| 2645 | neededLen -= retval; |
| 2646 | } |
| 2647 | if (flags & AIO_ONEREAD) { |
| 2648 | return JIM_OK; |
| 2649 | } |
| 2650 | continue; |
| 2651 | } |
| 2652 | if ((flags & AIO_ONEREAD) || JimCheckStreamError(interp, af)) { |
| 2653 | return JIM_ERR; |
| 2654 | } |
| 2655 | break; |
| 2656 | } |
| 2657 | |
| @@ -2420,10 +2718,11 @@ | |
| 2718 | } |
| 2719 | if (af->readbuf) { |
| 2720 | Jim_FreeNewObj(interp, af->readbuf); |
| 2721 | } |
| 2722 | |
| 2723 | Jim_Free(af->rbuf); |
| 2724 | Jim_Free(af); |
| 2725 | } |
| 2726 | |
| 2727 | static int aio_cmd_read(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 2728 | { |
| @@ -2433,11 +2732,10 @@ | |
| 2732 | static const char * const options[] = { "-pending", "-nonewline", NULL }; |
| 2733 | enum { OPT_PENDING, OPT_NONEWLINE }; |
| 2734 | int option; |
| 2735 | int nb; |
| 2736 | Jim_Obj *objPtr; |
| 2737 | |
| 2738 | if (argc) { |
| 2739 | if (*Jim_String(argv[0]) == '-') { |
| 2740 | if (Jim_GetEnum(interp, argv[0], options, &option, NULL, JIM_ERRMSG) != JIM_OK) { |
| 2741 | return JIM_ERR; |
| @@ -2461,21 +2759,22 @@ | |
| 2759 | } |
| 2760 | argc--; |
| 2761 | argv++; |
| 2762 | } |
| 2763 | if (argc) { |
| 2764 | return JIM_USAGE; |
| 2765 | } |
| 2766 | |
| 2767 | |
| 2768 | nb = aio_start_nonblocking(af); |
| 2769 | |
| 2770 | if (aio_read_len(interp, af, nb ? AIO_NONBLOCK : 0, neededLen) != JIM_OK) { |
| 2771 | aio_set_nonblocking(af, nb); |
| 2772 | return JIM_ERR; |
| 2773 | } |
| 2774 | objPtr = aio_read_consume(interp, af, neededLen); |
| 2775 | Jim_TaintObj(objPtr, af->taintsource); |
| 2776 | |
| 2777 | aio_set_nonblocking(af, nb); |
| 2778 | |
| 2779 | if (objPtr) { |
| 2780 | if (nonewline) { |
| @@ -2498,11 +2797,11 @@ | |
| 2797 | int Jim_AioFilehandle(Jim_Interp *interp, Jim_Obj *command) |
| 2798 | { |
| 2799 | Jim_Cmd *cmdPtr = Jim_GetCommand(interp, command, JIM_ERRMSG); |
| 2800 | |
| 2801 | |
| 2802 | if (cmdPtr && !(cmdPtr->flags & JIM_CMD_ISPROC) && cmdPtr->u.native.cmdProc == JimAioSubCmdProc) { |
| 2803 | return ((AioFile *) cmdPtr->u.native.privData)->fd; |
| 2804 | } |
| 2805 | Jim_SetResultFormatted(interp, "Not a filehandle: \"%#s\"", command); |
| 2806 | return -1; |
| 2807 | } |
| @@ -2516,29 +2815,62 @@ | |
| 2815 | |
| 2816 | Jim_SetResultInt(interp, af->fd); |
| 2817 | |
| 2818 | return JIM_OK; |
| 2819 | } |
| 2820 | |
| 2821 | static int aio_cmd_gettaint(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 2822 | { |
| 2823 | AioFile *af = Jim_CmdPrivData(interp); |
| 2824 | |
| 2825 | static const char * const options[] = { "-source", "-sink", NULL }; |
| 2826 | enum { OPT_SOURCE, OPT_SINK }; |
| 2827 | int option; |
| 2828 | |
| 2829 | if (Jim_GetEnum(interp, argv[0], options, &option, NULL, JIM_ERRMSG) != JIM_OK) { |
| 2830 | return JIM_ERR; |
| 2831 | } |
| 2832 | switch (option) { |
| 2833 | case OPT_SOURCE: |
| 2834 | Jim_SetResultInt(interp, af->taintsource); |
| 2835 | break; |
| 2836 | |
| 2837 | case OPT_SINK: |
| 2838 | Jim_SetResultInt(interp, af->taintsink); |
| 2839 | break; |
| 2840 | } |
| 2841 | |
| 2842 | return JIM_OK; |
| 2843 | } |
| 2844 | |
| 2845 | static int aio_cmd_copy(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 2846 | { |
| 2847 | AioFile *af = Jim_CmdPrivData(interp); |
| 2848 | jim_wide count = 0; |
| 2849 | jim_wide maxlen = JIM_WIDE_MAX; |
| 2850 | int ok = 1; |
| 2851 | Jim_Obj *objv[4]; |
| 2852 | long taintsink; |
| 2853 | |
| 2854 | if (argc == 2) { |
| 2855 | if (Jim_GetWide(interp, argv[1], &maxlen) != JIM_OK) { |
| 2856 | return JIM_ERR; |
| 2857 | } |
| 2858 | } |
| 2859 | |
| 2860 | objv[0] = argv[0]; |
| 2861 | objv[1] = Jim_NewStringObj(interp, "gettaint", -1); |
| 2862 | objv[2] = Jim_NewStringObj(interp, "-sink", -1); |
| 2863 | if (Jim_EvalObjVector(interp, 3, objv) != JIM_OK || Jim_GetLong(interp, Jim_GetResult(interp), &taintsink) != JIM_OK) { |
| 2864 | Jim_SetResultFormatted(interp, "Not a filehandle: \"%#s\"", argv[0]); |
| 2865 | return JIM_ERR; |
| 2866 | } |
| 2867 | |
| 2868 | if (af->taintsource & taintsink) { |
| 2869 | Jim_SetResultString(interp, "copying tainted source", -1); |
| 2870 | return JIM_ERR; |
| 2871 | } |
| 2872 | |
| 2873 | objv[0] = argv[0]; |
| 2874 | objv[1] = Jim_NewStringObj(interp, "flush", -1); |
| 2875 | if (Jim_EvalObjVector(interp, 2, objv) != JIM_OK) { |
| 2876 | Jim_SetResultFormatted(interp, "Not a filehandle: \"%#s\"", argv[0]); |
| @@ -2552,14 +2884,14 @@ | |
| 2884 | Jim_IncrRefCount(objv[1]); |
| 2885 | Jim_IncrRefCount(objv[2]); |
| 2886 | |
| 2887 | while (count < maxlen) { |
| 2888 | jim_wide len = maxlen - count; |
| 2889 | if (len > af->rbuf_len) { |
| 2890 | len = af->rbuf_len; |
| 2891 | } |
| 2892 | if (aio_read_len(interp, af, 0, len) != JIM_OK) { |
| 2893 | ok = 0; |
| 2894 | break; |
| 2895 | } |
| 2896 | objv[3] = aio_read_consume(interp, af, len); |
| 2897 | count += Jim_Length(objv[3]); |
| @@ -2568,19 +2900,15 @@ | |
| 2900 | break; |
| 2901 | } |
| 2902 | if (aio_eof(af)) { |
| 2903 | break; |
| 2904 | } |
| 2905 | if (count >= 16384 && af->rbuf_len < 65536) { |
| 2906 | |
| 2907 | af->rbuf_len = 65536; |
| 2908 | af->rbuf = Jim_Realloc(af->rbuf, af->rbuf_len); |
| 2909 | } |
| 2910 | } |
| 2911 | |
| 2912 | Jim_DecrRefCount(interp, objv[1]); |
| 2913 | Jim_DecrRefCount(interp, objv[2]); |
| 2914 | |
| @@ -2591,62 +2919,107 @@ | |
| 2919 | Jim_SetResultInt(interp, count); |
| 2920 | |
| 2921 | return JIM_OK; |
| 2922 | } |
| 2923 | |
| 2924 | |
| 2925 | static const char *jim_strstr(const char *haystack, int haylen, const char *needle, int needlen) |
| 2926 | { |
| 2927 | if (needlen == 1) { |
| 2928 | return (const char *)memchr(haystack, needle[0], haylen); |
| 2929 | } |
| 2930 | return strstr(haystack, needle); |
| 2931 | } |
| 2932 | |
| 2933 | static int aio_cmd_gets(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 2934 | { |
| 2935 | AioFile *af = Jim_CmdPrivData(interp); |
| 2936 | Jim_Obj *objPtr = NULL; |
| 2937 | int len; |
| 2938 | int nb; |
| 2939 | unsigned flags = AIO_ONEREAD; |
| 2940 | const char *nl = NULL; |
| 2941 | int offset = 0; |
| 2942 | long keepnl = 0; |
| 2943 | const char *nlstr = "\n"; |
| 2944 | int nlstrlen = 1; |
| 2945 | |
| 2946 | while (argc >= 2) { |
| 2947 | enum {OPT_EOL, OPT_KEEP}; |
| 2948 | static const char * const options[] = { |
| 2949 | "-eol", |
| 2950 | "-keep", |
| 2951 | NULL |
| 2952 | }; |
| 2953 | int opt; |
| 2954 | |
| 2955 | |
| 2956 | if (*Jim_String(argv[0]) != '-') { |
| 2957 | return JIM_USAGE; |
| 2958 | } |
| 2959 | |
| 2960 | if (Jim_GetEnum(interp, argv[0], options, &opt, NULL, JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK) { |
| 2961 | return JIM_ERR; |
| 2962 | } |
| 2963 | |
| 2964 | switch (opt) { |
| 2965 | case OPT_EOL: |
| 2966 | nlstr = Jim_GetString(argv[1], &nlstrlen); |
| 2967 | break; |
| 2968 | |
| 2969 | case OPT_KEEP: |
| 2970 | if (Jim_GetLong(interp, argv[1], &keepnl) != JIM_OK) { |
| 2971 | return JIM_ERR; |
| 2972 | } |
| 2973 | break; |
| 2974 | } |
| 2975 | argc -= 2; |
| 2976 | argv += 2; |
| 2977 | } |
| 2978 | |
| 2979 | errno = 0; |
| 2980 | |
| 2981 | |
| 2982 | nb = aio_start_nonblocking(af); |
| 2983 | if (nb) { |
| 2984 | flags |= AIO_NONBLOCK; |
| 2985 | } |
| 2986 | |
| 2987 | while (!aio_eof(af)) { |
| 2988 | if (af->readbuf) { |
| 2989 | const char *pt = Jim_GetString(af->readbuf, &len); |
| 2990 | nl = jim_strstr(pt + offset, len - offset, nlstr, nlstrlen); |
| 2991 | if (nl) { |
| 2992 | |
| 2993 | objPtr = Jim_NewStringObj(interp, pt, nl - pt + (keepnl ? nlstrlen : 0)); |
| 2994 | |
| 2995 | aio_consume(af->readbuf, nl - pt + nlstrlen); |
| 2996 | break; |
| 2997 | } |
| 2998 | offset = len; |
| 2999 | } |
| 3000 | |
| 3001 | |
| 3002 | if (aio_read_len(interp, af, flags, -1) != JIM_OK) { |
| 3003 | break; |
| 3004 | } |
| 3005 | } |
| 3006 | |
| 3007 | aio_set_nonblocking(af, nb); |
| 3008 | |
| 3009 | if (!nl && aio_eof(af) && af->readbuf) { |
| 3010 | |
| 3011 | objPtr = af->readbuf; |
| 3012 | af->readbuf = NULL; |
| 3013 | } |
| 3014 | else if (!objPtr) { |
| 3015 | objPtr = Jim_NewStringObj(interp, NULL, 0); |
| 3016 | } |
| 3017 | Jim_TaintObj(objPtr, af->taintsource); |
| 3018 | |
| 3019 | if (argc) { |
| 3020 | if (Jim_SetVariable(interp, argv[0], objPtr) != JIM_OK) { |
| 3021 | return JIM_ERR; |
| 3022 | } |
| 3023 | |
| 3024 | len = Jim_Length(objPtr); |
| 3025 | |
| @@ -2668,22 +3041,34 @@ | |
| 3041 | int wlen; |
| 3042 | const char *wdata; |
| 3043 | Jim_Obj *strObj; |
| 3044 | int wnow = 0; |
| 3045 | int nl = 1; |
| 3046 | |
| 3047 | if (Jim_CheckTaint(interp, af->taintsink)) { |
| 3048 | Jim_SetResultString(interp, "puts: tainted data", -1); |
| 3049 | return JIM_ERR; |
| 3050 | } |
| 3051 | |
| 3052 | if (argc == 2) { |
| 3053 | if (!Jim_CompareStringImmediate(interp, argv[0], "-nonewline")) { |
| 3054 | return JIM_USAGE; |
| 3055 | } |
| 3056 | strObj = argv[1]; |
| 3057 | nl = 0; |
| 3058 | } |
| 3059 | else { |
| 3060 | strObj = argv[0]; |
| 3061 | } |
| 3062 | |
| 3063 | #ifdef JIM_MAINTAINER |
| 3064 | if (Jim_IsShared(af->writebuf)) { |
| 3065 | Jim_DecrRefCount(interp, af->writebuf); |
| 3066 | af->writebuf = Jim_DuplicateObj(interp, af->writebuf); |
| 3067 | Jim_IncrRefCount(af->writebuf); |
| 3068 | } |
| 3069 | #endif |
| 3070 | Jim_AppendObj(interp, af->writebuf, strObj); |
| 3071 | if (nl) { |
| 3072 | Jim_AppendString(interp, af->writebuf, "\n", 1); |
| 3073 | } |
| 3074 | |
| @@ -2701,11 +3086,11 @@ | |
| 3086 | wnow = 1; |
| 3087 | } |
| 3088 | break; |
| 3089 | |
| 3090 | case WBUF_OPT_FULL: |
| 3091 | if (wlen >= af->wbuf_limit) { |
| 3092 | wnow = 1; |
| 3093 | } |
| 3094 | break; |
| 3095 | } |
| 3096 | |
| @@ -2797,11 +3182,11 @@ | |
| 3182 | else if (Jim_CompareStringImmediate(interp, argv[1], "current")) |
| 3183 | orig = SEEK_CUR; |
| 3184 | else if (Jim_CompareStringImmediate(interp, argv[1], "end")) |
| 3185 | orig = SEEK_END; |
| 3186 | else { |
| 3187 | return JIM_USAGE; |
| 3188 | } |
| 3189 | } |
| 3190 | if (Jim_GetWide(interp, argv[0], &offset) != JIM_OK) { |
| 3191 | return JIM_ERR; |
| 3192 | } |
| @@ -2834,10 +3219,49 @@ | |
| 3219 | AioFile *af = Jim_CmdPrivData(interp); |
| 3220 | |
| 3221 | Jim_SetResult(interp, af->filename); |
| 3222 | return JIM_OK; |
| 3223 | } |
| 3224 | |
| 3225 | #ifdef JIM_TAINT |
| 3226 | static int aio_cmd_taint(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 3227 | { |
| 3228 | AioFile *af = Jim_CmdPrivData(interp); |
| 3229 | static const char * const types[] = { |
| 3230 | "sink", |
| 3231 | "source", |
| 3232 | NULL |
| 3233 | }; |
| 3234 | enum |
| 3235 | { |
| 3236 | TAINT_TYPE_SINK, |
| 3237 | TAINT_TYPE_SOURCE, |
| 3238 | }; |
| 3239 | int type; |
| 3240 | long taint; |
| 3241 | |
| 3242 | if (Jim_GetEnum(interp, argv[0], types, &type, NULL, JIM_ERRMSG) != JIM_OK) |
| 3243 | return JIM_ERR; |
| 3244 | |
| 3245 | if (argc == 1) { |
| 3246 | Jim_SetResultInt(interp, type == TAINT_TYPE_SINK ? af->taintsink : af->taintsource); |
| 3247 | return JIM_OK; |
| 3248 | } |
| 3249 | else if (Jim_GetLong(interp, argv[1], &taint) == JIM_OK) { |
| 3250 | if (type == TAINT_TYPE_SINK) { |
| 3251 | af->taintsink = taint; |
| 3252 | } |
| 3253 | else { |
| 3254 | af->taintsource = taint; |
| 3255 | } |
| 3256 | return JIM_OK; |
| 3257 | } |
| 3258 | else { |
| 3259 | return JIM_ERR; |
| 3260 | } |
| 3261 | } |
| 3262 | #endif |
| 3263 | |
| 3264 | #ifdef O_NDELAY |
| 3265 | static int aio_cmd_ndelay(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 3266 | { |
| 3267 | AioFile *af = Jim_CmdPrivData(interp); |
| @@ -2870,29 +3294,92 @@ | |
| 3294 | #endif |
| 3295 | |
| 3296 | static int aio_cmd_buffering(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 3297 | { |
| 3298 | AioFile *af = Jim_CmdPrivData(interp); |
| 3299 | Jim_Obj *resultObj; |
| 3300 | |
| 3301 | static const char * const options[] = { |
| 3302 | "none", |
| 3303 | "line", |
| 3304 | "full", |
| 3305 | NULL |
| 3306 | }; |
| 3307 | |
| 3308 | if (argc) { |
| 3309 | if (Jim_GetEnum(interp, argv[0], options, &af->wbuft, NULL, JIM_ERRMSG) != JIM_OK) { |
| 3310 | return JIM_ERR; |
| 3311 | } |
| 3312 | |
| 3313 | if (af->wbuft == WBUF_OPT_FULL && argc == 2) { |
| 3314 | long l; |
| 3315 | if (Jim_GetLong(interp, argv[1], &l) != JIM_OK || l <= 0) { |
| 3316 | return JIM_ERR; |
| 3317 | } |
| 3318 | af->wbuf_limit = l; |
| 3319 | } |
| 3320 | |
| 3321 | if (af->wbuft == WBUF_OPT_NONE) { |
| 3322 | if (aio_flush(interp, af) != JIM_OK) { |
| 3323 | return JIM_ERR; |
| 3324 | } |
| 3325 | } |
| 3326 | |
| 3327 | } |
| 3328 | |
| 3329 | resultObj = Jim_NewListObj(interp, NULL, 0); |
| 3330 | Jim_ListAppendElement(interp, resultObj, Jim_NewStringObj(interp, options[af->wbuft], -1)); |
| 3331 | if (af->wbuft == WBUF_OPT_FULL) { |
| 3332 | Jim_ListAppendElement(interp, resultObj, Jim_NewIntObj(interp, af->wbuf_limit)); |
| 3333 | } |
| 3334 | Jim_SetResult(interp, resultObj); |
| 3335 | |
| 3336 | return JIM_OK; |
| 3337 | } |
| 3338 | |
| 3339 | static int aio_cmd_translation(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 3340 | { |
| 3341 | enum {OPT_BINARY, OPT_TEXT}; |
| 3342 | static const char * const options[] = { |
| 3343 | "binary", |
| 3344 | "text", |
| 3345 | NULL |
| 3346 | }; |
| 3347 | int opt; |
| 3348 | |
| 3349 | if (Jim_GetEnum(interp, argv[0], options, &opt, NULL, JIM_ERRMSG) != JIM_OK) { |
| 3350 | return JIM_ERR; |
| 3351 | } |
| 3352 | #if defined(Jim_SetMode) |
| 3353 | else { |
| 3354 | AioFile *af = Jim_CmdPrivData(interp); |
| 3355 | Jim_SetMode(af->fd, opt == OPT_BINARY ? O_BINARY : O_TEXT); |
| 3356 | } |
| 3357 | #endif |
| 3358 | return JIM_OK; |
| 3359 | } |
| 3360 | |
| 3361 | static int aio_cmd_readsize(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 3362 | { |
| 3363 | AioFile *af = Jim_CmdPrivData(interp); |
| 3364 | |
| 3365 | if (argc) { |
| 3366 | long l; |
| 3367 | if (Jim_GetLong(interp, argv[0], &l) != JIM_OK || l <= 0) { |
| 3368 | return JIM_ERR; |
| 3369 | } |
| 3370 | af->rbuf_len = l; |
| 3371 | if (af->rbuf) { |
| 3372 | af->rbuf = Jim_Realloc(af->rbuf, af->rbuf_len); |
| 3373 | } |
| 3374 | } |
| 3375 | Jim_SetResultInt(interp, af->rbuf_len); |
| 3376 | |
| 3377 | return JIM_OK; |
| 3378 | } |
| 3379 | |
| 3380 | #ifdef jim_ext_eventloop |
| 3381 | static int aio_cmd_timeout(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 3382 | { |
| 3383 | #ifdef HAVE_SELECT |
| 3384 | AioFile *af = Jim_CmdPrivData(interp); |
| 3385 | if (argc == 1) { |
| @@ -2906,11 +3393,10 @@ | |
| 3393 | Jim_SetResultString(interp, "timeout not supported", -1); |
| 3394 | return JIM_ERR; |
| 3395 | #endif |
| 3396 | } |
| 3397 | |
| 3398 | static int aio_eventinfo(Jim_Interp *interp, AioFile * af, unsigned mask, |
| 3399 | int argc, Jim_Obj * const *argv) |
| 3400 | { |
| 3401 | if (argc == 0) { |
| 3402 | |
| @@ -2989,17 +3475,26 @@ | |
| 3475 | { "getfd", |
| 3476 | NULL, |
| 3477 | aio_cmd_getfd, |
| 3478 | 0, |
| 3479 | 0, |
| 3480 | JIM_MODFLAG_HIDDEN, |
| 3481 | |
| 3482 | }, |
| 3483 | { "gettaint", |
| 3484 | "?-source|-sink?", |
| 3485 | aio_cmd_gettaint, |
| 3486 | 1, |
| 3487 | 1, |
| 3488 | JIM_MODFLAG_HIDDEN, |
| 3489 | |
| 3490 | }, |
| 3491 | { "gets", |
| 3492 | "?-eol <str>? ?-keep 0|1? ?var?", |
| 3493 | aio_cmd_gets, |
| 3494 | 0, |
| 3495 | -1, |
| 3496 | |
| 3497 | }, |
| 3498 | { "puts", |
| 3499 | "?-nonewline? str", |
| 3500 | aio_cmd_puts, |
| @@ -3055,10 +3550,19 @@ | |
| 3550 | aio_cmd_filename, |
| 3551 | 0, |
| 3552 | 0, |
| 3553 | |
| 3554 | }, |
| 3555 | #ifdef JIM_TAINT |
| 3556 | { "taint", |
| 3557 | "source|sink ?0|n?", |
| 3558 | aio_cmd_taint, |
| 3559 | 1, |
| 3560 | 2, |
| 3561 | |
| 3562 | }, |
| 3563 | #endif |
| 3564 | #ifdef O_NDELAY |
| 3565 | { "ndelay", |
| 3566 | "?0|1?", |
| 3567 | aio_cmd_ndelay, |
| 3568 | 0, |
| @@ -3074,13 +3578,27 @@ | |
| 3578 | 0, |
| 3579 | |
| 3580 | }, |
| 3581 | #endif |
| 3582 | { "buffering", |
| 3583 | "?none|line|full? ?size?", |
| 3584 | aio_cmd_buffering, |
| 3585 | 0, |
| 3586 | 2, |
| 3587 | |
| 3588 | }, |
| 3589 | { "translation", |
| 3590 | "binary|text", |
| 3591 | aio_cmd_translation, |
| 3592 | 1, |
| 3593 | 1, |
| 3594 | |
| 3595 | }, |
| 3596 | { "readsize", |
| 3597 | "?size?", |
| 3598 | aio_cmd_readsize, |
| 3599 | 0, |
| 3600 | 1, |
| 3601 | |
| 3602 | }, |
| 3603 | #if defined(jim_ext_file) && defined(Jim_FileStat) |
| 3604 | { "stat", |
| @@ -3222,23 +3740,29 @@ | |
| 3740 | |
| 3741 | static int JimAioOpenCommand(Jim_Interp *interp, int argc, |
| 3742 | Jim_Obj *const *argv) |
| 3743 | { |
| 3744 | int openflags; |
| 3745 | AioFile *af; |
| 3746 | const char *filename; |
| 3747 | int fd = -1; |
| 3748 | int n = 0; |
| 3749 | |
| 3750 | int flags = AIO_NOTAINT; |
| 3751 | |
| 3752 | if (argc > 2 && Jim_CompareStringImmediate(interp, argv[2], "-noclose")) { |
| 3753 | flags = AIO_KEEPOPEN; |
| 3754 | n++; |
| 3755 | } |
| 3756 | |
| 3757 | if (Jim_CheckTaint(interp, JIM_TAINT_ANY)) { |
| 3758 | Jim_SetTaintError(interp, 1, argv); |
| 3759 | return JIM_ERR; |
| 3760 | } |
| 3761 | if (argc > 3 + n) { |
| 3762 | return JIM_USAGE; |
| 3763 | } |
| 3764 | |
| 3765 | filename = Jim_String(argv[1]); |
| 3766 | |
| 3767 | #ifdef jim_ext_tclcompat |
| 3768 | { |
| @@ -3265,19 +3789,32 @@ | |
| 3789 | } |
| 3790 | } |
| 3791 | else { |
| 3792 | openflags = O_RDONLY; |
| 3793 | } |
| 3794 | |
| 3795 | fd = open(filename, openflags, 0666); |
| 3796 | if (fd < 0) { |
| 3797 | JimAioSetError(interp, argv[1]); |
| 3798 | return JIM_ERR; |
| 3799 | } |
| 3800 | |
| 3801 | af = JimMakeChannel(interp, fd, argv[1], "aio.handle%ld", 0, flags); |
| 3802 | if (af) { |
| 3803 | |
| 3804 | JimAioSetTaint(af, 0, 0); |
| 3805 | return JIM_OK; |
| 3806 | } |
| 3807 | return JIM_ERR; |
| 3808 | } |
| 3809 | |
| 3810 | |
| 3811 | static void JimAioSetTaint(AioFile *af, int taintsource, int taintsink) |
| 3812 | { |
| 3813 | af->taintsource = taintsource; |
| 3814 | af->taintsink = taintsink; |
| 3815 | } |
| 3816 | |
| 3817 | static AioFile *JimMakeChannel(Jim_Interp *interp, int fd, Jim_Obj *filename, |
| 3818 | const char *hdlfmt, int family, int flags) |
| 3819 | { |
| 3820 | AioFile *af; |
| @@ -3319,12 +3856,23 @@ | |
| 3856 | |
| 3857 | af->flags |= flags; |
| 3858 | |
| 3859 | af->writebuf = Jim_NewStringObj(interp, NULL, 0); |
| 3860 | Jim_IncrRefCount(af->writebuf); |
| 3861 | af->wbuf_limit = AIO_DEFAULT_WBUF_LIMIT; |
| 3862 | af->rbuf_len = AIO_DEFAULT_RBUF_LEN; |
| 3863 | |
| 3864 | |
| 3865 | |
| 3866 | if (!(flags & AIO_NOTAINT)) { |
| 3867 | JimAioSetTaint(af, JIM_TAINT_STD, JIM_TAINT_STD); |
| 3868 | } |
| 3869 | Jim_RegisterCommand(interp, cmdname, |
| 3870 | JimAioSubCmdProc, JimAioDelProc, |
| 3871 | NULL, |
| 3872 | NULL, |
| 3873 | 0, -1, JIM_CMD_ISCHANNEL, af); |
| 3874 | |
| 3875 | Jim_SetResult(interp, Jim_MakeGlobalNamespaceName(interp, cmdname)); |
| 3876 | |
| 3877 | return af; |
| 3878 | } |
| @@ -3365,14 +3913,10 @@ | |
| 3913 | } |
| 3914 | |
| 3915 | |
| 3916 | static int JimAioPipeCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 3917 | { |
| 3918 | return JimCreatePipe(interp, argv[0], 0); |
| 3919 | } |
| 3920 | #endif |
| 3921 | |
| 3922 | #ifdef HAVE_OPENPTY |
| @@ -3379,15 +3923,10 @@ | |
| 3923 | static int JimAioOpenPtyCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 3924 | { |
| 3925 | int p[2]; |
| 3926 | char path[MAXPATHLEN]; |
| 3927 | |
| 3928 | if (openpty(&p[0], &p[1], path, NULL, NULL) != 0) { |
| 3929 | JimAioSetError(interp, NULL); |
| 3930 | return JIM_ERR; |
| 3931 | } |
| 3932 | |
| @@ -3397,31 +3936,37 @@ | |
| 3936 | } |
| 3937 | #endif |
| 3938 | |
| 3939 | |
| 3940 | |
| 3941 | |
| 3942 | static void JimMakeStdioChannel(Jim_Interp *interp, FILE *fh, const char *name, unsigned flags) |
| 3943 | { |
| 3944 | |
| 3945 | AioFile *af = JimMakeChannel(interp, fileno(fh), NULL, name, 0, AIO_KEEPOPEN | flags); |
| 3946 | JimAioSetTaint(af, 0, 0); |
| 3947 | } |
| 3948 | |
| 3949 | int Jim_aioInit(Jim_Interp *interp) |
| 3950 | { |
| 3951 | Jim_PackageProvideCheck(interp, "aio"); |
| 3952 | #if defined(JIM_SSL) |
| 3953 | Jim_RegisterSimpleCmd(interp, "load_ssl_certs", "dir", 1, 1, JimAioLoadSSLCertsCommand); |
| 3954 | #endif |
| 3955 | |
| 3956 | Jim_RegisterCmd(interp, "open", "filename ?-noclose? ?mode?", 1, 3, JimAioOpenCommand, NULL, NULL, JIM_CMD_NOTAINT); |
| 3957 | #ifdef HAVE_SOCKETS |
| 3958 | Jim_RegisterCmd(interp, "socket", "?-async? ?-ipv6? socktype ?address?", 1, 4, JimAioSockCommand, NULL, NULL, JIM_CMD_NOTAINT); |
| 3959 | #endif |
| 3960 | #ifdef HAVE_PIPE |
| 3961 | Jim_RegisterSimpleCmd(interp, "pipe", "", 0, 0, JimAioPipeCommand); |
| 3962 | #endif |
| 3963 | |
| 3964 | |
| 3965 | JimMakeStdioChannel(interp, stdin, "stdin", 0); |
| 3966 | JimMakeStdioChannel(interp, stdout, "stdout", 0); |
| 3967 | JimMakeStdioChannel(interp, stderr, "stderr", AIO_WBUF_NONE); |
| 3968 | |
| 3969 | return JIM_OK; |
| 3970 | } |
| 3971 | |
| 3972 | #include <errno.h> |
| @@ -3442,12 +3987,11 @@ | |
| 3987 | |
| 3988 | if (argc == 3 && Jim_CompareStringImmediate(interp, argv[1], "-nocomplain")) { |
| 3989 | nocomplain = 1; |
| 3990 | } |
| 3991 | if (argc != 2 && !nocomplain) { |
| 3992 | return JIM_USAGE; |
| 3993 | } |
| 3994 | |
| 3995 | dirPath = Jim_String(argv[1 + nocomplain]); |
| 3996 | |
| 3997 | dirPtr = opendir(dirPath); |
| @@ -3480,11 +4024,11 @@ | |
| 4024 | } |
| 4025 | |
| 4026 | int Jim_readdirInit(Jim_Interp *interp) |
| 4027 | { |
| 4028 | Jim_PackageProvideCheck(interp, "readdir"); |
| 4029 | Jim_RegisterSimpleCmd(interp, "readdir", "?-nocomplain? dirPath", 1, 2, Jim_ReaddirCmd); |
| 4030 | return JIM_OK; |
| 4031 | } |
| 4032 | |
| 4033 | #include <stdlib.h> |
| 4034 | #include <string.h> |
| @@ -3553,10 +4097,11 @@ | |
| 4097 | int Jim_RegexpCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 4098 | { |
| 4099 | int opt_indices = 0; |
| 4100 | int opt_all = 0; |
| 4101 | int opt_inline = 0; |
| 4102 | int opt_lineanchor = 0; |
| 4103 | regex_t *regex; |
| 4104 | int match, i, j; |
| 4105 | int offset = 0; |
| 4106 | regmatch_t *pmatch = NULL; |
| 4107 | int source_len; |
| @@ -3568,30 +4113,23 @@ | |
| 4113 | Jim_Obj *resultListObj = NULL; |
| 4114 | int regcomp_flags = 0; |
| 4115 | int eflags = 0; |
| 4116 | int option; |
| 4117 | enum { |
| 4118 | OPT_INDICES, OPT_NOCASE, OPT_LINE, OPT_LINESTOP, OPT_LINEANCHOR, OPT_ALL, OPT_INLINE, OPT_START, OPT_EXPANDED, OPT_END |
| 4119 | }; |
| 4120 | static const char * const options[] = { |
| 4121 | "-indices", "-nocase", "-line", "-linestop", "-lineanchor", "-all", "-inline", "-start", "-expanded", "--", NULL |
| 4122 | }; |
| 4123 | |
| 4124 | for (i = 1; i < argc; i++) { |
| 4125 | const char *opt = Jim_String(argv[i]); |
| 4126 | |
| 4127 | if (*opt != '-') { |
| 4128 | break; |
| 4129 | } |
| 4130 | if (Jim_GetEnum(interp, argv[i], options, &option, "option", JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK) { |
| 4131 | return JIM_ERR; |
| 4132 | } |
| 4133 | if (option == OPT_END) { |
| 4134 | i++; |
| 4135 | break; |
| @@ -3605,12 +4143,24 @@ | |
| 4143 | regcomp_flags |= REG_ICASE; |
| 4144 | break; |
| 4145 | |
| 4146 | case OPT_LINE: |
| 4147 | regcomp_flags |= REG_NEWLINE; |
| 4148 | opt_lineanchor = 1; |
| 4149 | break; |
| 4150 | |
| 4151 | #ifdef REG_NEWLINE_STOP |
| 4152 | case OPT_LINESTOP: |
| 4153 | regcomp_flags |= REG_NEWLINE_STOP; |
| 4154 | break; |
| 4155 | #endif |
| 4156 | #ifdef REG_NEWLINE_ANCHOR |
| 4157 | case OPT_LINEANCHOR: |
| 4158 | regcomp_flags |= REG_NEWLINE_ANCHOR; |
| 4159 | opt_lineanchor = 1; |
| 4160 | break; |
| 4161 | #endif |
| 4162 | case OPT_ALL: |
| 4163 | opt_all = 1; |
| 4164 | break; |
| 4165 | |
| 4166 | case OPT_INLINE: |
| @@ -3617,20 +4167,30 @@ | |
| 4167 | opt_inline = 1; |
| 4168 | break; |
| 4169 | |
| 4170 | case OPT_START: |
| 4171 | if (++i == argc) { |
| 4172 | return JIM_USAGE; |
| 4173 | } |
| 4174 | if (Jim_GetIndex(interp, argv[i], &offset) != JIM_OK) { |
| 4175 | return JIM_ERR; |
| 4176 | } |
| 4177 | break; |
| 4178 | |
| 4179 | #ifdef REG_EXPANDED |
| 4180 | case OPT_EXPANDED: |
| 4181 | regcomp_flags |= REG_EXPANDED; |
| 4182 | break; |
| 4183 | #endif |
| 4184 | default: |
| 4185 | |
| 4186 | Jim_SetResultFormatted(interp, "not supported: %#s", argv[i]); |
| 4187 | return JIM_ERR; |
| 4188 | } |
| 4189 | } |
| 4190 | if (argc - i < 2) { |
| 4191 | return JIM_USAGE; |
| 4192 | } |
| 4193 | |
| 4194 | regex = SetRegexpFromAny(interp, argv[i], regcomp_flags); |
| 4195 | if (!regex) { |
| 4196 | return JIM_ERR; |
| @@ -3685,18 +4245,14 @@ | |
| 4245 | goto done; |
| 4246 | } |
| 4247 | |
| 4248 | num_matches++; |
| 4249 | |
| 4250 | |
| 4251 | |
| 4252 | j = 0; |
| 4253 | for (j = 0; j < num_vars; j++) { |
| 4254 | Jim_Obj *resultObj; |
| 4255 | |
| 4256 | if (opt_indices) { |
| 4257 | resultObj = Jim_NewListObj(interp, NULL, 0); |
| 4258 | } |
| @@ -3726,21 +4282,19 @@ | |
| 4282 | if (opt_inline) { |
| 4283 | Jim_ListAppendElement(interp, resultListObj, resultObj); |
| 4284 | } |
| 4285 | else { |
| 4286 | |
| 4287 | result = Jim_SetVariable(interp, argv[i + 2 + j], resultObj); |
| 4288 | |
| 4289 | if (result != JIM_OK) { |
| 4290 | break; |
| 4291 | } |
| 4292 | } |
| 4293 | } |
| 4294 | |
| 4295 | if (opt_all && (pattern[0] != '^' || opt_lineanchor) && *source_str) { |
| 4296 | if (pmatch[0].rm_eo) { |
| 4297 | offset += utf8_strlen(source_str, pmatch[0].rm_eo); |
| 4298 | source_str += pmatch[0].rm_eo; |
| 4299 | } |
| 4300 | else { |
| @@ -3792,30 +4346,23 @@ | |
| 4346 | const char *replace_str = NULL; |
| 4347 | int replace_len; |
| 4348 | const char *pattern; |
| 4349 | int option; |
| 4350 | enum { |
| 4351 | OPT_NOCASE, OPT_LINE, OPT_LINESTOP, OPT_LINEANCHOR, OPT_ALL, OPT_START, OPT_COMMAND, OPT_EXPANDED, OPT_END |
| 4352 | }; |
| 4353 | static const char * const options[] = { |
| 4354 | "-nocase", "-line", "-linestop", "-lineanchor", "-all", "-start", "-command", "-expanded", "--", NULL |
| 4355 | }; |
| 4356 | |
| 4357 | for (i = 1; i < argc; i++) { |
| 4358 | const char *opt = Jim_String(argv[i]); |
| 4359 | |
| 4360 | if (*opt != '-') { |
| 4361 | break; |
| 4362 | } |
| 4363 | if (Jim_GetEnum(interp, argv[i], options, &option, "option", JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK) { |
| 4364 | return JIM_ERR; |
| 4365 | } |
| 4366 | if (option == OPT_END) { |
| 4367 | i++; |
| 4368 | break; |
| @@ -3827,48 +4374,69 @@ | |
| 4374 | |
| 4375 | case OPT_LINE: |
| 4376 | regcomp_flags |= REG_NEWLINE; |
| 4377 | break; |
| 4378 | |
| 4379 | #ifdef REG_NEWLINE_STOP |
| 4380 | case OPT_LINESTOP: |
| 4381 | regcomp_flags |= REG_NEWLINE_STOP; |
| 4382 | break; |
| 4383 | #endif |
| 4384 | #ifdef REG_NEWLINE_ANCHOR |
| 4385 | case OPT_LINEANCHOR: |
| 4386 | regcomp_flags |= REG_NEWLINE_ANCHOR; |
| 4387 | break; |
| 4388 | #endif |
| 4389 | case OPT_ALL: |
| 4390 | opt_all = 1; |
| 4391 | break; |
| 4392 | |
| 4393 | case OPT_START: |
| 4394 | if (++i == argc) { |
| 4395 | return JIM_USAGE; |
| 4396 | } |
| 4397 | if (Jim_GetIndex(interp, argv[i], &offset) != JIM_OK) { |
| 4398 | return JIM_ERR; |
| 4399 | } |
| 4400 | break; |
| 4401 | |
| 4402 | case OPT_COMMAND: |
| 4403 | opt_command = 1; |
| 4404 | break; |
| 4405 | |
| 4406 | #ifdef REG_EXPANDED |
| 4407 | case OPT_EXPANDED: |
| 4408 | regcomp_flags |= REG_EXPANDED; |
| 4409 | break; |
| 4410 | #endif |
| 4411 | |
| 4412 | default: |
| 4413 | |
| 4414 | Jim_SetResultFormatted(interp, "not supported: %#s", argv[i]); |
| 4415 | return JIM_ERR; |
| 4416 | } |
| 4417 | } |
| 4418 | if (argc - i != 3 && argc - i != 4) { |
| 4419 | return JIM_USAGE; |
| 4420 | } |
| 4421 | |
| 4422 | |
| 4423 | regcomp_obj = Jim_DuplicateObj(interp, argv[i]); |
| 4424 | Jim_IncrRefCount(regcomp_obj); |
| 4425 | regex = SetRegexpFromAny(interp, regcomp_obj, regcomp_flags); |
| 4426 | if (!regex) { |
| 4427 | Jim_DecrRefCount(interp, regcomp_obj); |
| 4428 | return JIM_ERR; |
| 4429 | } |
| 4430 | pattern = Jim_String(argv[i]); |
| 4431 | |
| 4432 | source_str = Jim_GetString(argv[i + 1], &source_len); |
| 4433 | if (opt_command) { |
| 4434 | cmd_prefix = argv[i + 2]; |
| 4435 | if (Jim_ListLength(interp, cmd_prefix) == 0) { |
| 4436 | Jim_SetResultString(interp, "command prefix must be a list of at least one element", -1); |
| 4437 | Jim_DecrRefCount(interp, regcomp_obj); |
| 4438 | return JIM_ERR; |
| 4439 | } |
| 4440 | Jim_IncrRefCount(cmd_prefix); |
| 4441 | } |
| 4442 | else { |
| @@ -3897,11 +4465,12 @@ | |
| 4465 | Jim_AppendString(interp, resultObj, source_str, offset); |
| 4466 | |
| 4467 | |
| 4468 | n = source_len - offset; |
| 4469 | p = source_str + offset; |
| 4470 | |
| 4471 | while (n || pattern[0]) { |
| 4472 | int match = jim_regexec(regex, p, MAX_SUB_MATCHES, pmatch, regexec_flags); |
| 4473 | |
| 4474 | if (match >= REG_BADPAT) { |
| 4475 | char buf[100]; |
| 4476 | |
| @@ -3978,32 +4547,26 @@ | |
| 4547 | |
| 4548 | if (!opt_all || n == 0) { |
| 4549 | break; |
| 4550 | } |
| 4551 | |
| 4552 | regexec_flags = 0; |
| 4553 | if (pmatch[0].rm_eo == pmatch[0].rm_so) { |
| 4554 | |
| 4555 | if (pattern[0] == '^') { |
| 4556 | |
| 4557 | regexec_flags = REG_NOTBOL; |
| 4558 | } |
| 4559 | else { |
| 4560 | |
| 4561 | int charlen = utf8_charlen(p[0]); |
| 4562 | Jim_AppendString(interp, resultObj, p, charlen); |
| 4563 | p += charlen; |
| 4564 | n -= charlen; |
| 4565 | } |
| 4566 | } |
| 4567 | } |
| 4568 | |
| 4569 | Jim_AppendString(interp, resultObj, p, -1); |
| 4570 | |
| 4571 | cmd_error: |
| 4572 | if (result == JIM_OK) { |
| @@ -4012,13 +4575,10 @@ | |
| 4575 | result = Jim_SetVariable(interp, varname, resultObj); |
| 4576 | |
| 4577 | if (result == JIM_OK) { |
| 4578 | Jim_SetResultInt(interp, num_matches); |
| 4579 | } |
| 4580 | } |
| 4581 | else { |
| 4582 | Jim_SetResult(interp, resultObj); |
| 4583 | result = JIM_OK; |
| 4584 | } |
| @@ -4029,20 +4589,20 @@ | |
| 4589 | |
| 4590 | if (opt_command) { |
| 4591 | Jim_DecrRefCount(interp, cmd_prefix); |
| 4592 | } |
| 4593 | |
| 4594 | Jim_DecrRefCount(interp, regcomp_obj); |
| 4595 | |
| 4596 | return result; |
| 4597 | } |
| 4598 | |
| 4599 | int Jim_regexpInit(Jim_Interp *interp) |
| 4600 | { |
| 4601 | Jim_PackageProvideCheck(interp, "regexp"); |
| 4602 | Jim_RegisterSimpleCmd(interp, "regexp", "?-option ...? exp string ?matchVar? ?subMatchVar ...?", 2, -1, Jim_RegexpCmd); |
| 4603 | Jim_RegisterSimpleCmd(interp, "regsub", "?-option ...? exp string subSpec ?varName?", 3, -1, Jim_RegsubCmd); |
| 4604 | return JIM_OK; |
| 4605 | } |
| 4606 | |
| 4607 | #include <limits.h> |
| 4608 | #include <stdlib.h> |
| @@ -4063,18 +4623,10 @@ | |
| 4623 | #define R_OK 4 |
| 4624 | #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) |
| 4625 | #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) |
| 4626 | #endif |
| 4627 | |
| 4628 | #if defined(__MINGW32__) || defined(__MSYS__) || defined(_MSC_VER) |
| 4629 | #define ISWINDOWS 1 |
| 4630 | |
| 4631 | #undef HAVE_SYMLINK |
| 4632 | #else |
| @@ -4514,11 +5066,11 @@ | |
| 5066 | if (errno == ENOENT) { |
| 5067 | |
| 5068 | continue; |
| 5069 | } |
| 5070 | |
| 5071 | if (errno == EEXIST || errno == ENOTDIR) { |
| 5072 | jim_stat_t sb; |
| 5073 | |
| 5074 | if (Jim_Stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) { |
| 5075 | return 0; |
| 5076 | } |
| @@ -4949,31 +5501,35 @@ | |
| 5501 | { "delete", |
| 5502 | "?-force|--? name ...", |
| 5503 | file_cmd_delete, |
| 5504 | 1, |
| 5505 | -1, |
| 5506 | JIM_MODFLAG_NOTAINT, |
| 5507 | |
| 5508 | }, |
| 5509 | { "mkdir", |
| 5510 | "dir ...", |
| 5511 | file_cmd_mkdir, |
| 5512 | 1, |
| 5513 | -1, |
| 5514 | JIM_MODFLAG_NOTAINT, |
| 5515 | |
| 5516 | }, |
| 5517 | { "tempfile", |
| 5518 | "?template?", |
| 5519 | file_cmd_tempfile, |
| 5520 | 0, |
| 5521 | 1, |
| 5522 | JIM_MODFLAG_NOTAINT, |
| 5523 | |
| 5524 | }, |
| 5525 | { "rename", |
| 5526 | "?-force? source dest", |
| 5527 | file_cmd_rename, |
| 5528 | 2, |
| 5529 | 3, |
| 5530 | JIM_MODFLAG_NOTAINT, |
| 5531 | |
| 5532 | }, |
| 5533 | #if defined(HAVE_LINK) && defined(HAVE_SYMLINK) |
| 5534 | { "link", |
| 5535 | "?-symbolic|-hard? newname target", |
| @@ -5048,18 +5604,11 @@ | |
| 5604 | } |
| 5605 | }; |
| 5606 | |
| 5607 | static int Jim_CdCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 5608 | { |
| 5609 | const char *path = Jim_String(argv[1]); |
| 5610 | |
| 5611 | if (chdir(path) != 0) { |
| 5612 | Jim_SetResultFormatted(interp, "couldn't change working directory to \"%s\": %s", path, |
| 5613 | strerror(errno)); |
| 5614 | return JIM_ERR; |
| @@ -5084,13 +5633,13 @@ | |
| 5633 | } |
| 5634 | |
| 5635 | int Jim_fileInit(Jim_Interp *interp) |
| 5636 | { |
| 5637 | Jim_PackageProvideCheck(interp, "file"); |
| 5638 | Jim_RegisterSubCmd(interp, "file", file_command_table, NULL); |
| 5639 | Jim_RegisterSimpleCmd(interp, "pwd", "", 0, 0, Jim_PwdCmd); |
| 5640 | Jim_RegisterCmd(interp, "cd", "dirname", 1, 1, Jim_CdCmd, NULL, NULL, JIM_CMD_NOTAINT); |
| 5641 | return JIM_OK; |
| 5642 | } |
| 5643 | |
| 5644 | #include <string.h> |
| 5645 | #include <ctype.h> |
| @@ -5143,11 +5692,11 @@ | |
| 5692 | } |
| 5693 | |
| 5694 | int Jim_execInit(Jim_Interp *interp) |
| 5695 | { |
| 5696 | Jim_PackageProvideCheck(interp, "exec"); |
| 5697 | Jim_RegisterSimpleCmd(interp, "exec", "arg ?arg ...? ?&?", 1, -1, Jim_ExecCmd); |
| 5698 | return JIM_OK; |
| 5699 | } |
| 5700 | #else |
| 5701 | |
| 5702 | |
| @@ -5158,12 +5707,12 @@ | |
| 5707 | struct WaitInfoTable; |
| 5708 | |
| 5709 | static char **JimOriginalEnviron(void); |
| 5710 | static char **JimSaveEnv(char **env); |
| 5711 | static void JimRestoreEnv(char **env); |
| 5712 | static int JimCreatePipeline(Jim_Interp *interp, int argc, Jim_Obj *const *argv, phandle_t **pidArrayPtr, |
| 5713 | int *outPipePtr, int *errFilePtr); |
| 5714 | static void JimDetachPids(struct WaitInfoTable *table, int numPids, const phandle_t *pidPtr); |
| 5715 | static int JimCleanupChildren(Jim_Interp *interp, int numPids, phandle_t *pidPtr, Jim_Obj *errStrObj); |
| 5716 | static int Jim_WaitCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv); |
| 5717 | |
| 5718 | #if defined(__MINGW32__) |
| @@ -5341,11 +5890,11 @@ | |
| 5890 | static struct WaitInfoTable *JimAllocWaitInfoTable(void) |
| 5891 | { |
| 5892 | struct WaitInfoTable *table = Jim_Alloc(sizeof(*table)); |
| 5893 | table->info = NULL; |
| 5894 | table->size = table->used = 0; |
| 5895 | table->refcount = 0; |
| 5896 | |
| 5897 | return table; |
| 5898 | } |
| 5899 | |
| 5900 | static int JimWaitRemove(struct WaitInfoTable *table, phandle_t phandle) |
| @@ -5379,11 +5928,11 @@ | |
| 5928 | if (argc > 1 && Jim_CompareStringImmediate(interp, argv[argc - 1], "&")) { |
| 5929 | Jim_Obj *listObj; |
| 5930 | int i; |
| 5931 | |
| 5932 | argc--; |
| 5933 | numPids = JimCreatePipeline(interp, argc - 1, argv + 1, &pidPtr, NULL, NULL); |
| 5934 | if (numPids < 0) { |
| 5935 | return JIM_ERR; |
| 5936 | } |
| 5937 | |
| 5938 | listObj = Jim_NewListObj(interp, NULL, 0); |
| @@ -5395,11 +5944,11 @@ | |
| 5944 | Jim_Free(pidPtr); |
| 5945 | return JIM_OK; |
| 5946 | } |
| 5947 | |
| 5948 | numPids = |
| 5949 | JimCreatePipeline(interp, argc - 1, argv + 1, &pidPtr, &outputId, &errorId); |
| 5950 | |
| 5951 | if (numPids < 0) { |
| 5952 | return JIM_ERR; |
| 5953 | } |
| 5954 | |
| @@ -5487,11 +6036,11 @@ | |
| 6036 | objv[1] = Jim_NewStringObj(interp, "getfd", -1); |
| 6037 | |
| 6038 | if (Jim_EvalObjVector(interp, 2, objv) == JIM_OK) { |
| 6039 | jim_wide fd; |
| 6040 | if (Jim_GetWide(interp, Jim_GetResult(interp), &fd) == JIM_OK) { |
| 6041 | return dup(fd); |
| 6042 | } |
| 6043 | } |
| 6044 | return -1; |
| 6045 | } |
| 6046 | |
| @@ -5541,12 +6090,11 @@ | |
| 6090 | |
| 6091 | if (argc > 1 && Jim_CompareStringImmediate(interp, argv[1], "-nohang")) { |
| 6092 | nohang = 1; |
| 6093 | } |
| 6094 | if (argc != nohang + 2) { |
| 6095 | return JIM_USAGE; |
| 6096 | } |
| 6097 | if (Jim_GetLong(interp, argv[nohang + 1], &pid) != JIM_OK) { |
| 6098 | return JIM_ERR; |
| 6099 | } |
| 6100 | |
| @@ -5556,10 +6104,14 @@ | |
| 6104 | pid = -1; |
| 6105 | } |
| 6106 | #ifndef __MINGW32__ |
| 6107 | else if (pid < 0) { |
| 6108 | pid = phandle; |
| 6109 | } |
| 6110 | else if (phandle == 0) { |
| 6111 | |
| 6112 | pid = 0; |
| 6113 | } |
| 6114 | #endif |
| 6115 | |
| 6116 | errCodeObj = JimMakeErrorCode(interp, pid, status, NULL); |
| 6117 | |
| @@ -5571,22 +6123,192 @@ | |
| 6123 | return JIM_OK; |
| 6124 | } |
| 6125 | |
| 6126 | static int Jim_PidCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 6127 | { |
| 6128 | Jim_SetResultInt(interp, (jim_wide)getpid()); |
| 6129 | return JIM_OK; |
| 6130 | } |
| 6131 | |
| 6132 | #define JIM_ETT_IN 0x0001 |
| 6133 | #define JIM_ETT_OUT 0x0002 |
| 6134 | #define JIM_ETT_ERR 0x0004 |
| 6135 | #define JIM_ETT_PIPE 0x0008 |
| 6136 | |
| 6137 | #define JIM_ETT_NOARG 0x0010 |
| 6138 | #define JIM_ETT_APPEND 0x0020 |
| 6139 | #define JIM_ETT_STR 0x0040 |
| 6140 | #define JIM_ETT_DUPERR 0x0080 |
| 6141 | #define JIM_ETT_HANDLE 0x0100 |
| 6142 | |
| 6143 | #define JIM_ETT_CMD 0xF000 |
| 6144 | #define JIM_ETT_BAD 0xF001 |
| 6145 | |
| 6146 | struct redir_type_t { |
| 6147 | const char *prefix; |
| 6148 | unsigned flags; |
| 6149 | }; |
| 6150 | |
| 6151 | |
| 6152 | static const struct redir_type_t redir_types[] = { |
| 6153 | { "<<@", JIM_ETT_IN | JIM_ETT_HANDLE | JIM_ETT_STR }, |
| 6154 | { "<<", JIM_ETT_IN | JIM_ETT_STR }, |
| 6155 | { "<@", JIM_ETT_IN | JIM_ETT_HANDLE }, |
| 6156 | { "<", JIM_ETT_IN }, |
| 6157 | |
| 6158 | { "2>>", JIM_ETT_ERR | JIM_ETT_APPEND }, |
| 6159 | { "2>@", JIM_ETT_ERR | JIM_ETT_HANDLE }, |
| 6160 | { "2>", JIM_ETT_ERR }, |
| 6161 | |
| 6162 | { ">>&", JIM_ETT_OUT | JIM_ETT_APPEND | JIM_ETT_DUPERR }, |
| 6163 | { ">>", JIM_ETT_OUT | JIM_ETT_APPEND }, |
| 6164 | { ">&@", JIM_ETT_OUT | JIM_ETT_HANDLE | JIM_ETT_DUPERR }, |
| 6165 | { ">@", JIM_ETT_OUT | JIM_ETT_HANDLE }, |
| 6166 | { ">&", JIM_ETT_OUT | JIM_ETT_DUPERR }, |
| 6167 | { ">", JIM_ETT_OUT }, |
| 6168 | |
| 6169 | { "|&", JIM_ETT_PIPE | JIM_ETT_DUPERR }, |
| 6170 | { "|", JIM_ETT_PIPE }, |
| 6171 | { NULL } |
| 6172 | }; |
| 6173 | |
| 6174 | static unsigned JimExecClassifyArg(const char *arg) |
| 6175 | { |
| 6176 | int i; |
| 6177 | for (i = 0; redir_types[i].prefix; i++) { |
| 6178 | int len = strlen(redir_types[i].prefix); |
| 6179 | if (strncmp(arg, redir_types[i].prefix, len) == 0) { |
| 6180 | if (strlen(arg) > len) { |
| 6181 | if (redir_types[i].flags & JIM_ETT_NOARG) { |
| 6182 | |
| 6183 | return JIM_ETT_BAD; |
| 6184 | } |
| 6185 | return redir_types[i].flags; |
| 6186 | } |
| 6187 | |
| 6188 | return redir_types[i].flags | JIM_ETT_NOARG; |
| 6189 | } |
| 6190 | } |
| 6191 | return JIM_ETT_CMD; |
| 6192 | } |
| 6193 | |
| 6194 | static int JimParsePipeline(Jim_Interp *interp, int argc, Jim_Obj *const *argv, Jim_Obj *cmdList, Jim_Obj *redirectList) |
| 6195 | { |
| 6196 | int i; |
| 6197 | |
| 6198 | int first = 1; |
| 6199 | const char *arg = NULL; |
| 6200 | |
| 6201 | for (i = 0; i < argc; i++) { |
| 6202 | unsigned ett; |
| 6203 | if (first) { |
| 6204 | if (Jim_ListLength(interp, argv[i]) == 0) { |
| 6205 | Jim_SetResultString(interp, "empty command list", -1); |
| 6206 | return JIM_ERR; |
| 6207 | } |
| 6208 | Jim_ListAppendElement(interp, cmdList, argv[i]); |
| 6209 | first = 0; |
| 6210 | continue; |
| 6211 | } |
| 6212 | |
| 6213 | arg = Jim_String(argv[i]); |
| 6214 | ett = JimExecClassifyArg(arg); |
| 6215 | if (ett == JIM_ETT_BAD || ett == JIM_ETT_CMD) { |
| 6216 | Jim_SetResultFormatted(interp, "invalid redirection %s", arg); |
| 6217 | return JIM_ERR; |
| 6218 | } |
| 6219 | if (ett & JIM_ETT_PIPE) { |
| 6220 | Jim_ListAppendElement(interp, cmdList, argv[i]); |
| 6221 | first = 1; |
| 6222 | continue; |
| 6223 | } |
| 6224 | Jim_ListAppendElement(interp, redirectList, argv[i]); |
| 6225 | if ((ett & JIM_ETT_NOARG)) { |
| 6226 | |
| 6227 | if (i >= argc - 1) { |
| 6228 | |
| 6229 | Jim_SetResultFormatted(interp, "can't specify \"%#s\" as last word in command", argv[i]); |
| 6230 | return -1; |
| 6231 | } |
| 6232 | i++; |
| 6233 | Jim_ListAppendElement(interp, redirectList, argv[i]); |
| 6234 | } |
| 6235 | } |
| 6236 | |
| 6237 | if (first) { |
| 6238 | if (Jim_ListLength(interp, cmdList)) { |
| 6239 | Jim_SetResultFormatted(interp, "cmdlist required after %s", arg); |
| 6240 | } |
| 6241 | else { |
| 6242 | Jim_SetResultString(interp, "cmdlist is required", -1); |
| 6243 | } |
| 6244 | return JIM_ERR; |
| 6245 | } |
| 6246 | |
| 6247 | return JIM_OK; |
| 6248 | } |
| 6249 | |
| 6250 | static int JimParsePipelineLegacy(Jim_Interp *interp, int argc, Jim_Obj *const *argv, Jim_Obj *cmdList, Jim_Obj *redirectList) |
| 6251 | { |
| 6252 | int i; |
| 6253 | |
| 6254 | Jim_Obj *cmdObj = Jim_NewListObj(interp, NULL, 0); |
| 6255 | Jim_ListAppendElement(interp, cmdList, cmdObj); |
| 6256 | const char *arg = NULL; |
| 6257 | |
| 6258 | for (i = 0; i < argc; i++) { |
| 6259 | arg = Jim_String(argv[i]); |
| 6260 | unsigned ett = JimExecClassifyArg(arg); |
| 6261 | if (ett == JIM_ETT_BAD) { |
| 6262 | Jim_SetResultFormatted(interp, "invalid: %s", arg); |
| 6263 | return JIM_ERR; |
| 6264 | } |
| 6265 | if (ett == JIM_ETT_CMD) { |
| 6266 | |
| 6267 | Jim_ListAppendElement(interp, cmdObj, argv[i]); |
| 6268 | continue; |
| 6269 | } |
| 6270 | if (ett & JIM_ETT_PIPE) { |
| 6271 | if (Jim_ListLength(interp, cmdObj) == 0) { |
| 6272 | goto missing_cmd; |
| 6273 | } |
| 6274 | |
| 6275 | Jim_ListAppendElement(interp, cmdList, argv[i]); |
| 6276 | |
| 6277 | cmdObj = Jim_NewListObj(interp, NULL, 0); |
| 6278 | Jim_ListAppendElement(interp, cmdList, cmdObj); |
| 6279 | continue; |
| 6280 | } |
| 6281 | Jim_ListAppendElement(interp, redirectList, argv[i]); |
| 6282 | if ((ett & JIM_ETT_NOARG)) { |
| 6283 | |
| 6284 | if (i >= argc - 1) { |
| 6285 | |
| 6286 | Jim_SetResultFormatted(interp, "can't specify \"%#s\" as last word in command", argv[i]); |
| 6287 | return -1; |
| 6288 | } |
| 6289 | i++; |
| 6290 | Jim_ListAppendElement(interp, redirectList, argv[i]); |
| 6291 | } |
| 6292 | } |
| 6293 | |
| 6294 | if (Jim_ListLength(interp, cmdObj) == 0) { |
| 6295 | missing_cmd: |
| 6296 | if (arg && *arg == '|') { |
| 6297 | Jim_SetResultString(interp, "illegal use of | or |& in command", -1); |
| 6298 | } |
| 6299 | else { |
| 6300 | Jim_SetResultString(interp, "didn't specify command to execute", -1); |
| 6301 | } |
| 6302 | return JIM_ERR; |
| 6303 | } |
| 6304 | |
| 6305 | return JIM_OK; |
| 6306 | } |
| 6307 | |
| 6308 | static int JimExecPipeline(Jim_Interp *interp, Jim_Obj *cmdList, Jim_Obj *redirectList, |
| 6309 | phandle_t **pidArrayPtr, int *outPipePtr, int *errFilePtr) |
| 6310 | { |
| 6311 | phandle_t *pidPtr = NULL; /* Points to alloc-ed array holding all |
| 6312 | * the pids of child processes. */ |
| 6313 | int numPids = 0; /* Actual number of processes that exist |
| 6314 | * at *pidPtr right now. */ |
| @@ -5624,126 +6346,88 @@ | |
| 6346 | int inputId = -1; |
| 6347 | int outputId = -1; |
| 6348 | int errorId = -1; |
| 6349 | int lastOutputId = -1; |
| 6350 | int pipeIds[2]; |
| 6351 | int i; |
| 6352 | phandle_t phandle; |
| 6353 | char **save_environ; |
| 6354 | #if defined(HAVE_EXECVPE) && !defined(__MINGW32__) |
| 6355 | char **child_environ; |
| 6356 | #endif |
| 6357 | struct WaitInfoTable *table = Jim_CmdPrivData(interp); |
| 6358 | |
| 6359 | if (outPipePtr != NULL) { |
| 6360 | *outPipePtr = -1; |
| 6361 | } |
| 6362 | if (errFilePtr != NULL) { |
| 6363 | *errFilePtr = -1; |
| 6364 | } |
| 6365 | pipeIds[0] = pipeIds[1] = -1; |
| 6366 | |
| 6367 | int redir_len = Jim_ListLength(interp, redirectList); |
| 6368 | for (i = 0; i < redir_len; i++) { |
| 6369 | int len; |
| 6370 | int item_len; |
| 6371 | Jim_Obj *redirObj = Jim_ListGetIndex(interp, redirectList, i); |
| 6372 | const char *arg = Jim_GetString(redirObj, &len); |
| 6373 | unsigned ett = JimExecClassifyArg(arg); |
| 6374 | const char *item; |
| 6375 | int type = FILE_NAME; |
| 6376 | if ((ett & JIM_ETT_NOARG) == 0) { |
| 6377 | |
| 6378 | item = arg + 1; |
| 6379 | if (ett & JIM_ETT_HANDLE) { |
| 6380 | item++; |
| 6381 | } |
| 6382 | if (ett & JIM_ETT_APPEND) { |
| 6383 | item++; |
| 6384 | } |
| 6385 | if (ett & JIM_ETT_DUPERR) { |
| 6386 | item++; |
| 6387 | } |
| 6388 | if (ett & JIM_ETT_ERR) { |
| 6389 | item++; |
| 6390 | } |
| 6391 | if (ett & JIM_ETT_STR) { |
| 6392 | type = FILE_TEXT; |
| 6393 | item++; |
| 6394 | } |
| 6395 | item_len = len - (item - arg); |
| 6396 | } |
| 6397 | else { |
| 6398 | |
| 6399 | i++; |
| 6400 | item = Jim_GetString(Jim_ListGetIndex(interp, redirectList, i), &item_len); |
| 6401 | } |
| 6402 | |
| 6403 | if (ett & JIM_ETT_HANDLE) { |
| 6404 | type = FILE_HANDLE; |
| 6405 | } |
| 6406 | if (ett & JIM_ETT_APPEND) { |
| 6407 | type = FILE_APPEND; |
| 6408 | } |
| 6409 | if (ett & JIM_ETT_STR) { |
| 6410 | type = FILE_TEXT; |
| 6411 | } |
| 6412 | if (ett & JIM_ETT_IN) { |
| 6413 | input = item; |
| 6414 | input_len = item_len; |
| 6415 | inputFile = type; |
| 6416 | } |
| 6417 | else if (ett & JIM_ETT_OUT) { |
| 6418 | output = item; |
| 6419 | outputFile = type; |
| 6420 | if (ett & JIM_ETT_DUPERR) { |
| 6421 | error = output; |
| 6422 | errorFile = outputFile; |
| 6423 | } |
| 6424 | } |
| 6425 | else if (ett & JIM_ETT_ERR) { |
| 6426 | error = item; |
| 6427 | errorFile = type; |
| 6428 | } |
| 6429 | } |
| 6430 | |
| 6431 | |
| 6432 | save_environ = JimSaveEnv(JimBuildEnv(interp)); |
| 6433 | |
| @@ -5759,42 +6443,30 @@ | |
| 6443 | goto error; |
| 6444 | } |
| 6445 | Jim_Lseek(inputId, 0L, SEEK_SET); |
| 6446 | } |
| 6447 | else if (inputFile == FILE_HANDLE) { |
| 6448 | inputId = JimGetChannelFd(interp, input); |
| 6449 | if (inputId < 0) { |
| 6450 | goto error; |
| 6451 | } |
| 6452 | } |
| 6453 | else { |
| 6454 | inputId = Jim_OpenForRead(input); |
| 6455 | if (inputId == -1) { |
| 6456 | Jim_SetResultFormatted(interp, "couldn't read file \"%s\": %s", input, strerror(Jim_Errno())); |
| 6457 | goto error; |
| 6458 | } |
| 6459 | } |
| 6460 | } |
| 6461 | |
| 6462 | if (output != NULL) { |
| 6463 | if (outputFile == FILE_HANDLE) { |
| 6464 | lastOutputId = JimGetChannelFd(interp, output); |
| 6465 | if (lastOutputId < 0) { |
| 6466 | goto error; |
| 6467 | } |
| 6468 | } |
| 6469 | else { |
| 6470 | lastOutputId = Jim_OpenForWrite(output, outputFile == FILE_APPEND); |
| 6471 | if (lastOutputId == -1) { |
| 6472 | Jim_SetResultFormatted(interp, "couldn't write file \"%s\": %s", output, strerror(Jim_Errno())); |
| @@ -5809,10 +6481,11 @@ | |
| 6481 | } |
| 6482 | lastOutputId = pipeIds[1]; |
| 6483 | *outPipePtr = pipeIds[0]; |
| 6484 | pipeIds[0] = pipeIds[1] = -1; |
| 6485 | } |
| 6486 | |
| 6487 | |
| 6488 | if (error != NULL) { |
| 6489 | if (errorFile == FILE_HANDLE) { |
| 6490 | if (strcmp(error, "1") == 0) { |
| 6491 | |
| @@ -5823,15 +6496,14 @@ | |
| 6496 | |
| 6497 | error = "stdout"; |
| 6498 | } |
| 6499 | } |
| 6500 | if (errorId == -1) { |
| 6501 | errorId = JimGetChannelFd(interp, error); |
| 6502 | if (errorId < 0) { |
| 6503 | goto error; |
| 6504 | } |
| 6505 | } |
| 6506 | } |
| 6507 | else { |
| 6508 | errorId = Jim_OpenForWrite(error, errorFile == FILE_APPEND); |
| 6509 | if (errorId == -1) { |
| @@ -5846,60 +6518,60 @@ | |
| 6518 | goto error; |
| 6519 | } |
| 6520 | *errFilePtr = dup(errorId); |
| 6521 | } |
| 6522 | |
| 6523 | int cmd_list_size = Jim_ListLength(interp, cmdList); |
| 6524 | cmdCount = (cmd_list_size + 1) / 2; |
| 6525 | pidPtr = Jim_Alloc(cmdCount * sizeof(*pidPtr)); |
| 6526 | |
| 6527 | for (i = 0; i < cmd_list_size; ) { |
| 6528 | char **arg_array; |
| 6529 | int j; |
| 6530 | int origErrorId = errorId; |
| 6531 | Jim_Obj *cmdObj = Jim_ListGetIndex(interp, cmdList, i++); |
| 6532 | int cmd_len = Jim_ListLength(interp, cmdObj); |
| 6533 | Jim_Obj *sepObj = NULL; |
| 6534 | if (i < cmd_list_size - 1) { |
| 6535 | sepObj = Jim_ListGetIndex(interp, cmdList, i++); |
| 6536 | } |
| 6537 | |
| 6538 | |
| 6539 | arg_array = Jim_Alloc((cmd_len + 1) * sizeof(*arg_array)); |
| 6540 | for (j = 0; j < cmd_len; j++) { |
| 6541 | arg_array[j] = (char *)Jim_String(Jim_ListGetIndex(interp, cmdObj, j)); |
| 6542 | } |
| 6543 | arg_array[j] = NULL; |
| 6544 | |
| 6545 | if (sepObj == NULL) { |
| 6546 | outputId = lastOutputId; |
| 6547 | lastOutputId = -1; |
| 6548 | } |
| 6549 | else { |
| 6550 | if (pipe(pipeIds) != 0) { |
| 6551 | Jim_SetResultErrno(interp, "couldn't create pipe"); |
| 6552 | Jim_Free(arg_array); |
| 6553 | goto error; |
| 6554 | } |
| 6555 | outputId = pipeIds[1]; |
| 6556 | } |
| 6557 | |
| 6558 | |
| 6559 | if (sepObj && Jim_CompareStringImmediate(interp, sepObj, "|&")) { |
| 6560 | errorId = outputId; |
| 6561 | } |
| 6562 | |
| 6563 | |
| 6564 | |
| 6565 | #ifdef __MINGW32__ |
| 6566 | phandle = JimStartWinProcess(interp, &arg_array[0], save_environ, inputId, outputId, errorId); |
| 6567 | if (phandle == JIM_BAD_PHANDLE) { |
| 6568 | Jim_SetResultFormatted(interp, "couldn't exec \"%s\"", arg_array[0]); |
| 6569 | goto error; |
| 6570 | } |
| 6571 | #else |
| 6572 | int argv0_len = strlen(arg_array[0]); |
| 6573 | |
| 6574 | #ifdef HAVE_EXECVPE |
| 6575 | child_environ = Jim_GetEnviron(); |
| 6576 | #endif |
| 6577 | #ifdef HAVE_VFORK |
| @@ -5940,14 +6612,14 @@ | |
| 6612 | } |
| 6613 | if (lastOutputId != -1) { |
| 6614 | close(lastOutputId); |
| 6615 | } |
| 6616 | |
| 6617 | execvpe(arg_array[0], arg_array, child_environ); |
| 6618 | |
| 6619 | if (write(fileno(stderr), "couldn't exec \"", 15) && |
| 6620 | write(fileno(stderr), arg_array[0], argv0_len) && |
| 6621 | write(fileno(stderr), "\"\n", 2)) { |
| 6622 | |
| 6623 | } |
| 6624 | #ifdef JIM_MAINTAINER |
| 6625 | { |
| @@ -5959,10 +6631,11 @@ | |
| 6631 | _exit(127); |
| 6632 | } |
| 6633 | #endif |
| 6634 | |
| 6635 | |
| 6636 | Jim_Free(arg_array); |
| 6637 | |
| 6638 | if (table->used == table->size) { |
| 6639 | table->size += WAIT_TABLE_GROW_BY; |
| 6640 | table->info = Jim_Realloc(table->info, table->size * sizeof(*table->info)); |
| 6641 | } |
| @@ -5969,11 +6642,11 @@ | |
| 6642 | |
| 6643 | table->info[table->used].phandle = phandle; |
| 6644 | table->info[table->used].flags = 0; |
| 6645 | table->used++; |
| 6646 | |
| 6647 | pidPtr[numPids++] = phandle; |
| 6648 | |
| 6649 | |
| 6650 | errorId = origErrorId; |
| 6651 | |
| 6652 | |
| @@ -5997,22 +6670,17 @@ | |
| 6670 | close(lastOutputId); |
| 6671 | } |
| 6672 | if (errorId != -1) { |
| 6673 | close(errorId); |
| 6674 | } |
| 6675 | |
| 6676 | JimRestoreEnv(save_environ); |
| 6677 | |
| 6678 | return numPids; |
| 6679 | |
| 6680 | |
| 6681 | error: |
| 6682 | if ((outPipePtr != NULL) && (*outPipePtr != -1)) { |
| 6683 | close(*outPipePtr); |
| 6684 | *outPipePtr = -1; |
| 6685 | } |
| 6686 | if ((errFilePtr != NULL) && (*errFilePtr != -1)) { |
| @@ -6034,10 +6702,39 @@ | |
| 6702 | Jim_Free(pidPtr); |
| 6703 | } |
| 6704 | numPids = -1; |
| 6705 | goto cleanup; |
| 6706 | } |
| 6707 | |
| 6708 | static int |
| 6709 | JimCreatePipeline(Jim_Interp *interp, int argc, Jim_Obj *const *argv, phandle_t **pidArrayPtr, |
| 6710 | int *outPipePtr, int *errFilePtr) |
| 6711 | { |
| 6712 | int rc = -1; |
| 6713 | int ret; |
| 6714 | |
| 6715 | Jim_Obj *cmdList = Jim_NewListObj(interp, NULL, 0); |
| 6716 | Jim_Obj *redirectList = Jim_NewListObj(interp, NULL, 0); |
| 6717 | Jim_IncrRefCount(cmdList); |
| 6718 | Jim_IncrRefCount(redirectList); |
| 6719 | |
| 6720 | if (argc > 1 && Jim_CompareStringImmediate(interp, argv[0], "|")) { |
| 6721 | |
| 6722 | ret = JimParsePipeline(interp, argc - 1, argv + 1, cmdList, redirectList); |
| 6723 | } |
| 6724 | else { |
| 6725 | |
| 6726 | ret = JimParsePipelineLegacy(interp, argc, argv, cmdList, redirectList); |
| 6727 | } |
| 6728 | if (ret == JIM_OK) { |
| 6729 | |
| 6730 | rc = JimExecPipeline(interp, cmdList, redirectList, pidArrayPtr, outPipePtr, errFilePtr); |
| 6731 | } |
| 6732 | Jim_DecrRefCount(interp, cmdList); |
| 6733 | Jim_DecrRefCount(interp, redirectList); |
| 6734 | return rc; |
| 6735 | } |
| 6736 | |
| 6737 | |
| 6738 | static int JimCleanupChildren(Jim_Interp *interp, int numPids, phandle_t *pidPtr, Jim_Obj *errStrObj) |
| 6739 | { |
| 6740 | struct WaitInfoTable *table = Jim_CmdPrivData(interp); |
| @@ -6064,14 +6761,15 @@ | |
| 6761 | struct WaitInfoTable *waitinfo; |
| 6762 | |
| 6763 | Jim_PackageProvideCheck(interp, "exec"); |
| 6764 | |
| 6765 | waitinfo = JimAllocWaitInfoTable(); |
| 6766 | Jim_RegisterCmd(interp, "exec", "arg ?arg ...? ?&?", 1, -1, Jim_ExecCmd, JimFreeWaitInfoTable, waitinfo, JIM_CMD_NOTAINT); |
| 6767 | waitinfo->refcount++; |
| 6768 | Jim_RegisterCmd(interp, "wait", "?-nohang? ?pid?", 0, 2, Jim_WaitCommand, JimFreeWaitInfoTable, waitinfo, 0); |
| 6769 | waitinfo->refcount++; |
| 6770 | Jim_RegisterSimpleCmd(interp, "pid", "", 0, 0, Jim_PidCommand); |
| 6771 | |
| 6772 | return JIM_OK; |
| 6773 | } |
| 6774 | |
| 6775 | #if defined(__MINGW32__) |
| @@ -6494,11 +7192,11 @@ | |
| 7192 | }; |
| 7193 | |
| 7194 | int Jim_clockInit(Jim_Interp *interp) |
| 7195 | { |
| 7196 | Jim_PackageProvideCheck(interp, "clock"); |
| 7197 | Jim_RegisterSubCmd(interp, "clock", clock_command_table, NULL); |
| 7198 | return JIM_OK; |
| 7199 | } |
| 7200 | |
| 7201 | #include <limits.h> |
| 7202 | #include <stdlib.h> |
| @@ -6714,58 +7412,11 @@ | |
| 7412 | }; |
| 7413 | |
| 7414 | int Jim_arrayInit(Jim_Interp *interp) |
| 7415 | { |
| 7416 | Jim_PackageProvideCheck(interp, "array"); |
| 7417 | Jim_RegisterSubCmd(interp, "array", array_command_table, NULL); |
| 7418 | return JIM_OK; |
| 7419 | } |
| 7420 | int Jim_InitStaticExtensions(Jim_Interp *interp) |
| 7421 | { |
| 7422 | extern int Jim_bootstrapInit(Jim_Interp *); |
| @@ -6773,11 +7424,10 @@ | |
| 7424 | extern int Jim_readdirInit(Jim_Interp *); |
| 7425 | extern int Jim_regexpInit(Jim_Interp *); |
| 7426 | extern int Jim_fileInit(Jim_Interp *); |
| 7427 | extern int Jim_globInit(Jim_Interp *); |
| 7428 | extern int Jim_execInit(Jim_Interp *); |
| 7429 | extern int Jim_clockInit(Jim_Interp *); |
| 7430 | extern int Jim_arrayInit(Jim_Interp *); |
| 7431 | extern int Jim_stdlibInit(Jim_Interp *); |
| 7432 | extern int Jim_tclcompatInit(Jim_Interp *); |
| 7433 | Jim_bootstrapInit(interp); |
| @@ -6785,11 +7435,10 @@ | |
| 7435 | Jim_readdirInit(interp); |
| 7436 | Jim_regexpInit(interp); |
| 7437 | Jim_fileInit(interp); |
| 7438 | Jim_globInit(interp); |
| 7439 | Jim_execInit(interp); |
| 7440 | Jim_clockInit(interp); |
| 7441 | Jim_arrayInit(interp); |
| 7442 | Jim_stdlibInit(interp); |
| 7443 | Jim_tclcompatInit(interp); |
| 7444 | return JIM_OK; |
| @@ -6854,11 +7503,12 @@ | |
| 7503 | |
| 7504 | |
| 7505 | |
| 7506 | #define JIM_INTEGER_SPACE 24 |
| 7507 | |
| 7508 | #if defined(DEBUG_SHOW_SCRIPT) || defined(DEBUG_SHOW_SCRIPT_TOKENS) || defined(JIM_DEBUG_COMMAND) || defined(DEBUG_SHOW_EXPR_TOKENS) || defined(DEBUG_SHOW_EXPR) |
| 7509 | #define JIM_TT_NAME |
| 7510 | static const char *jim_tt_name(int type); |
| 7511 | #endif |
| 7512 | |
| 7513 | #ifdef JIM_DEBUG_PANIC |
| 7514 | static void JimPanicDump(int fail_condition, const char *fmt, ...); |
| @@ -6893,19 +7543,21 @@ | |
| 7543 | static void JimPrngSeed(Jim_Interp *interp, unsigned char *seed, int seedLen); |
| 7544 | static void JimRandomBytes(Jim_Interp *interp, void *dest, unsigned int len); |
| 7545 | static int JimSetNewVariable(Jim_HashTable *ht, Jim_Obj *nameObjPtr, Jim_VarVal *vv); |
| 7546 | static Jim_VarVal *JimFindVariable(Jim_HashTable *ht, Jim_Obj *nameObjPtr); |
| 7547 | static int SetVariableFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr); |
| 7548 | static int JimCallNative(Jim_Interp *interp, Jim_Cmd *cmd, int argc, Jim_Obj *const *argv); |
| 7549 | |
| 7550 | #define JIM_DICT_SUGAR 100 |
| 7551 | |
| 7552 | |
| 7553 | |
| 7554 | |
| 7555 | #define JimWideValue(objPtr) (objPtr)->internalRep.wideValue |
| 7556 | |
| 7557 | #define JimObjTypeName(O) ((O)->typePtr ? (O)->typePtr->name : "none") |
| 7558 | |
| 7559 | |
| 7560 | static int utf8_tounicode_case(const char *s, int *uc, int upper) |
| 7561 | { |
| 7562 | int l = utf8_tounicode(s, uc); |
| 7563 | if (upper) { |
| @@ -7111,11 +7763,11 @@ | |
| 7763 | static int JimStringFirst(const char *s1, int l1, const char *s2, int l2, int idx) |
| 7764 | { |
| 7765 | int i; |
| 7766 | int l1bytelen; |
| 7767 | |
| 7768 | if (!l1 || !l2 || l1 > l2 || idx > l2) { |
| 7769 | return -1; |
| 7770 | } |
| 7771 | if (idx < 0) |
| 7772 | idx = 0; |
| 7773 | s2 += utf8_index(s2, idx); |
| @@ -7283,10 +7935,11 @@ | |
| 7935 | |
| 7936 | *doublePtr = strtod(str, &endptr); |
| 7937 | |
| 7938 | return JimCheckConversion(str, endptr); |
| 7939 | } |
| 7940 | |
| 7941 | |
| 7942 | static jim_wide JimPowWide(jim_wide b, jim_wide e) |
| 7943 | { |
| 7944 | jim_wide res = 1; |
| 7945 | |
| @@ -7311,10 +7964,11 @@ | |
| 7964 | } |
| 7965 | return res; |
| 7966 | } |
| 7967 | |
| 7968 | #ifdef JIM_DEBUG_PANIC |
| 7969 | |
| 7970 | static void JimPanicDump(int condition, const char *fmt, ...) |
| 7971 | { |
| 7972 | va_list ap; |
| 7973 | |
| 7974 | if (!condition) { |
| @@ -7447,10 +8101,11 @@ | |
| 8101 | ht->uniq = (rand() ^ time(NULL) ^ clock()); |
| 8102 | #else |
| 8103 | ht->uniq = 0; |
| 8104 | #endif |
| 8105 | } |
| 8106 | |
| 8107 | |
| 8108 | static void JimInitHashTableIterator(Jim_HashTable *ht, Jim_HashTableIterator *iter) |
| 8109 | { |
| 8110 | iter->ht = ht; |
| 8111 | iter->index = -1; |
| @@ -7716,20 +8371,23 @@ | |
| 8371 | |
| 8372 | static unsigned int JimStringCopyHTHashFunction(const void *key) |
| 8373 | { |
| 8374 | return Jim_GenHashFunction(key, strlen(key)); |
| 8375 | } |
| 8376 | |
| 8377 | |
| 8378 | static void *JimStringCopyHTDup(void *privdata, const void *key) |
| 8379 | { |
| 8380 | return Jim_StrDup(key); |
| 8381 | } |
| 8382 | |
| 8383 | |
| 8384 | static int JimStringCopyHTKeyCompare(void *privdata, const void *key1, const void *key2) |
| 8385 | { |
| 8386 | return strcmp(key1, key2) == 0; |
| 8387 | } |
| 8388 | |
| 8389 | |
| 8390 | static void JimStringCopyHTKeyDestructor(void *privdata, void *key) |
| 8391 | { |
| 8392 | Jim_Free(key); |
| 8393 | } |
| @@ -7746,10 +8404,11 @@ | |
| 8404 | typedef struct AssocDataValue |
| 8405 | { |
| 8406 | Jim_InterpDeleteProc *delProc; |
| 8407 | void *data; |
| 8408 | } AssocDataValue; |
| 8409 | |
| 8410 | |
| 8411 | static void JimAssocDataHashTableValueDestructor(void *privdata, void *data) |
| 8412 | { |
| 8413 | AssocDataValue *assocPtr = (AssocDataValue *) data; |
| 8414 | |
| @@ -7765,25 +8424,28 @@ | |
| 8424 | JimStringCopyHTKeyCompare, |
| 8425 | JimStringCopyHTKeyDestructor, |
| 8426 | JimAssocDataHashTableValueDestructor |
| 8427 | }; |
| 8428 | |
| 8429 | void Jim_StackInit(Jim_Stack *stack, void (*freefunc) (void *ptr)) |
| 8430 | { |
| 8431 | stack->len = 0; |
| 8432 | stack->maxlen = 0; |
| 8433 | stack->vector = NULL; |
| 8434 | stack->freefunc = freefunc; |
| 8435 | } |
| 8436 | |
| 8437 | void Jim_StackFree(Jim_Stack *stack) |
| 8438 | { |
| 8439 | int i; |
| 8440 | |
| 8441 | if (stack->freefunc) { |
| 8442 | for (i = 0; i < stack->len; i++) { |
| 8443 | stack->freefunc(stack->vector[i]); |
| 8444 | } |
| 8445 | } |
| 8446 | Jim_Free(stack->vector); |
| 8447 | } |
| 8448 | |
| 8449 | void Jim_StackPush(Jim_Stack *stack, void *element) |
| 8450 | { |
| 8451 | int neededLen = stack->len + 1; |
| @@ -7802,25 +8464,10 @@ | |
| 8464 | return NULL; |
| 8465 | stack->len--; |
| 8466 | return stack->vector[stack->len]; |
| 8467 | } |
| 8468 | |
| 8469 | |
| 8470 | |
| 8471 | #define JIM_TT_NONE 0 |
| 8472 | #define JIM_TT_STR 1 |
| 8473 | #define JIM_TT_ESC 2 |
| @@ -7901,10 +8548,11 @@ | |
| 8548 | pc->linenr = linenr; |
| 8549 | pc->comment = 1; |
| 8550 | pc->missing.ch = ' '; |
| 8551 | pc->missing.line = linenr; |
| 8552 | } |
| 8553 | |
| 8554 | |
| 8555 | static int JimParseScript(struct JimParserCtx *pc) |
| 8556 | { |
| 8557 | while (1) { |
| 8558 | if (!pc->len) { |
| @@ -7962,10 +8610,11 @@ | |
| 8610 | return JimParseStr(pc); |
| 8611 | } |
| 8612 | return JIM_OK; |
| 8613 | } |
| 8614 | } |
| 8615 | |
| 8616 | |
| 8617 | static int JimParseSep(struct JimParserCtx *pc) |
| 8618 | { |
| 8619 | pc->tstart = pc->p; |
| 8620 | pc->tline = pc->linenr; |
| @@ -7983,10 +8632,11 @@ | |
| 8632 | } |
| 8633 | pc->tend = pc->p - 1; |
| 8634 | pc->tt = JIM_TT_SEP; |
| 8635 | return JIM_OK; |
| 8636 | } |
| 8637 | |
| 8638 | |
| 8639 | static int JimParseEol(struct JimParserCtx *pc) |
| 8640 | { |
| 8641 | pc->tstart = pc->p; |
| 8642 | pc->tline = pc->linenr; |
| @@ -8151,36 +8801,40 @@ | |
| 8801 | } |
| 8802 | pc->missing.ch = '['; |
| 8803 | pc->missing.line = line; |
| 8804 | pc->tend = pc->p - 1; |
| 8805 | } |
| 8806 | |
| 8807 | |
| 8808 | static int JimParseBrace(struct JimParserCtx *pc) |
| 8809 | { |
| 8810 | pc->tstart = pc->p + 1; |
| 8811 | pc->tline = pc->linenr; |
| 8812 | pc->tt = JIM_TT_STR; |
| 8813 | JimParseSubBrace(pc); |
| 8814 | return JIM_OK; |
| 8815 | } |
| 8816 | |
| 8817 | |
| 8818 | static int JimParseCmd(struct JimParserCtx *pc) |
| 8819 | { |
| 8820 | pc->tstart = pc->p + 1; |
| 8821 | pc->tline = pc->linenr; |
| 8822 | pc->tt = JIM_TT_CMD; |
| 8823 | JimParseSubCmd(pc); |
| 8824 | return JIM_OK; |
| 8825 | } |
| 8826 | |
| 8827 | |
| 8828 | static int JimParseQuote(struct JimParserCtx *pc) |
| 8829 | { |
| 8830 | pc->tstart = pc->p + 1; |
| 8831 | pc->tline = pc->linenr; |
| 8832 | pc->tt = JimParseSubQuote(pc); |
| 8833 | return JIM_OK; |
| 8834 | } |
| 8835 | |
| 8836 | |
| 8837 | static int JimParseVar(struct JimParserCtx *pc) |
| 8838 | { |
| 8839 | |
| 8840 | pc->p++; |
| @@ -8278,10 +8932,11 @@ | |
| 8932 | pc->len++; |
| 8933 | return JIM_ERR; |
| 8934 | } |
| 8935 | return JIM_OK; |
| 8936 | } |
| 8937 | |
| 8938 | |
| 8939 | static int JimParseStr(struct JimParserCtx *pc) |
| 8940 | { |
| 8941 | if (pc->tt == JIM_TT_SEP || pc->tt == JIM_TT_EOL || |
| 8942 | pc->tt == JIM_TT_NONE || pc->tt == JIM_TT_STR) { |
| @@ -8381,10 +9036,11 @@ | |
| 9036 | pc->p++; |
| 9037 | pc->len--; |
| 9038 | } |
| 9039 | return JIM_OK; |
| 9040 | } |
| 9041 | |
| 9042 | |
| 9043 | static int JimParseComment(struct JimParserCtx *pc) |
| 9044 | { |
| 9045 | while (*pc->p) { |
| 9046 | if (*pc->p == '\\') { |
| @@ -8419,10 +9075,11 @@ | |
| 9075 | return c - 'a' + 10; |
| 9076 | if (c >= 'A' && c <= 'F') |
| 9077 | return c - 'A' + 10; |
| 9078 | return -1; |
| 9079 | } |
| 9080 | |
| 9081 | |
| 9082 | static int odigitval(int c) |
| 9083 | { |
| 9084 | if (c >= '0' && c <= '7') |
| 9085 | return c - '0'; |
| @@ -8609,10 +9266,11 @@ | |
| 9266 | } |
| 9267 | |
| 9268 | static int JimParseListSep(struct JimParserCtx *pc); |
| 9269 | static int JimParseListStr(struct JimParserCtx *pc); |
| 9270 | static int JimParseListQuote(struct JimParserCtx *pc); |
| 9271 | |
| 9272 | |
| 9273 | static int JimParseList(struct JimParserCtx *pc) |
| 9274 | { |
| 9275 | if (isspace(UCHAR(*pc->p))) { |
| 9276 | return JimParseListSep(pc); |
| @@ -8635,10 +9293,11 @@ | |
| 9293 | pc->tline = pc->linenr; |
| 9294 | pc->tt = JIM_TT_EOL; |
| 9295 | pc->eof = 1; |
| 9296 | return JIM_OK; |
| 9297 | } |
| 9298 | |
| 9299 | |
| 9300 | static int JimParseListSep(struct JimParserCtx *pc) |
| 9301 | { |
| 9302 | pc->tstart = pc->p; |
| 9303 | pc->tline = pc->linenr; |
| @@ -8651,10 +9310,11 @@ | |
| 9310 | } |
| 9311 | pc->tend = pc->p - 1; |
| 9312 | pc->tt = JIM_TT_SEP; |
| 9313 | return JIM_OK; |
| 9314 | } |
| 9315 | |
| 9316 | |
| 9317 | static int JimParseListQuote(struct JimParserCtx *pc) |
| 9318 | { |
| 9319 | pc->p++; |
| 9320 | pc->len--; |
| @@ -8688,10 +9348,11 @@ | |
| 9348 | } |
| 9349 | |
| 9350 | pc->tend = pc->p - 1; |
| 9351 | return JIM_OK; |
| 9352 | } |
| 9353 | |
| 9354 | |
| 9355 | static int JimParseListStr(struct JimParserCtx *pc) |
| 9356 | { |
| 9357 | pc->tstart = pc->p; |
| 9358 | pc->tline = pc->linenr; |
| @@ -8734,10 +9395,11 @@ | |
| 9395 | |
| 9396 | objPtr = Jim_Alloc(sizeof(*objPtr)); |
| 9397 | } |
| 9398 | |
| 9399 | objPtr->refCount = 0; |
| 9400 | objPtr->taint = interp->taint; |
| 9401 | |
| 9402 | |
| 9403 | objPtr->prevObjPtr = NULL; |
| 9404 | objPtr->nextObjPtr = interp->liveList; |
| 9405 | if (interp->liveList) |
| @@ -8794,10 +9456,12 @@ | |
| 9456 | Jim_Obj *Jim_DuplicateObj(Jim_Interp *interp, Jim_Obj *objPtr) |
| 9457 | { |
| 9458 | Jim_Obj *dupPtr; |
| 9459 | |
| 9460 | dupPtr = Jim_NewObj(interp); |
| 9461 | dupPtr->taint = objPtr->taint; |
| 9462 | |
| 9463 | if (objPtr->bytes == NULL) { |
| 9464 | |
| 9465 | dupPtr->bytes = NULL; |
| 9466 | } |
| 9467 | else if (objPtr->length == 0) { |
| @@ -8856,10 +9520,11 @@ | |
| 9520 | |
| 9521 | Jim_GetString(objPtr, NULL); |
| 9522 | } |
| 9523 | return objPtr->bytes; |
| 9524 | } |
| 9525 | |
| 9526 | |
| 9527 | static void JimSetStringBytes(Jim_Obj *objPtr, const char *str) |
| 9528 | { |
| 9529 | objPtr->bytes = Jim_StrDup(str); |
| 9530 | objPtr->length = strlen(str); |
| @@ -8884,15 +9549,17 @@ | |
| 9549 | FreeInterpolatedInternalRep, |
| 9550 | DupInterpolatedInternalRep, |
| 9551 | NULL, |
| 9552 | JIM_TYPE_NONE, |
| 9553 | }; |
| 9554 | |
| 9555 | |
| 9556 | static void FreeInterpolatedInternalRep(Jim_Interp *interp, Jim_Obj *objPtr) |
| 9557 | { |
| 9558 | Jim_DecrRefCount(interp, objPtr->internalRep.dictSubstValue.indexObjPtr); |
| 9559 | } |
| 9560 | |
| 9561 | |
| 9562 | static void DupInterpolatedInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr) |
| 9563 | { |
| 9564 | |
| 9565 | dupPtr->internalRep = srcPtr->internalRep; |
| @@ -8908,18 +9575,20 @@ | |
| 9575 | NULL, |
| 9576 | DupStringInternalRep, |
| 9577 | NULL, |
| 9578 | JIM_TYPE_REFERENCES, |
| 9579 | }; |
| 9580 | |
| 9581 | |
| 9582 | static void DupStringInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr) |
| 9583 | { |
| 9584 | JIM_NOTUSED(interp); |
| 9585 | |
| 9586 | dupPtr->internalRep.strValue.maxLength = srcPtr->length; |
| 9587 | dupPtr->internalRep.strValue.charLength = srcPtr->internalRep.strValue.charLength; |
| 9588 | } |
| 9589 | |
| 9590 | |
| 9591 | static int SetStringFromAny(Jim_Interp *interp, Jim_Obj *objPtr) |
| 9592 | { |
| 9593 | if (objPtr->typePtr != &stringObjType) { |
| 9594 | |
| @@ -9047,10 +9716,11 @@ | |
| 9716 | void Jim_AppendObj(Jim_Interp *interp, Jim_Obj *objPtr, Jim_Obj *appendObjPtr) |
| 9717 | { |
| 9718 | int len; |
| 9719 | const char *str = Jim_GetString(appendObjPtr, &len); |
| 9720 | Jim_AppendString(interp, objPtr, str, len); |
| 9721 | objPtr->taint |= appendObjPtr->taint; |
| 9722 | } |
| 9723 | |
| 9724 | void Jim_AppendStrings(Jim_Interp *interp, Jim_Obj *objPtr, ...) |
| 9725 | { |
| 9726 | va_list ap; |
| @@ -9075,11 +9745,11 @@ | |
| 9745 | else { |
| 9746 | int Alen, Blen; |
| 9747 | const char *sA = Jim_GetString(aObjPtr, &Alen); |
| 9748 | const char *sB = Jim_GetString(bObjPtr, &Blen); |
| 9749 | |
| 9750 | return Alen == Blen && *sA == *sB && memcmp(sA, sB, Alen) == 0; |
| 9751 | } |
| 9752 | } |
| 9753 | |
| 9754 | int Jim_StringMatchObj(Jim_Interp *interp, Jim_Obj *patternObjPtr, Jim_Obj *objPtr, int nocase) |
| 9755 | { |
| @@ -9128,10 +9798,11 @@ | |
| 9798 | if (rangeLen < 0) |
| 9799 | rangeLen = 0; |
| 9800 | |
| 9801 | *rangeLenPtr = rangeLen; |
| 9802 | } |
| 9803 | |
| 9804 | |
| 9805 | static int JimStringGetRange(Jim_Interp *interp, Jim_Obj *firstObjPtr, Jim_Obj *lastObjPtr, |
| 9806 | int len, int *first, int *last, int *range) |
| 9807 | { |
| 9808 | if (Jim_GetIndex(interp, firstObjPtr, first) != JIM_OK) { |
| @@ -9349,10 +10020,11 @@ | |
| 10020 | } |
| 10021 | |
| 10022 | static const char default_trim_chars[] = " \t\n\r"; |
| 10023 | |
| 10024 | static int default_trim_chars_len = sizeof(default_trim_chars); |
| 10025 | |
| 10026 | |
| 10027 | static Jim_Obj *JimStringTrimLeft(Jim_Interp *interp, Jim_Obj *strObjPtr, Jim_Obj *trimcharsObjPtr) |
| 10028 | { |
| 10029 | int len; |
| 10030 | const char *str = Jim_GetString(strObjPtr, &len); |
| @@ -9369,10 +10041,11 @@ | |
| 10041 | return strObjPtr; |
| 10042 | } |
| 10043 | |
| 10044 | return Jim_NewStringObj(interp, newstr, len - (newstr - str)); |
| 10045 | } |
| 10046 | |
| 10047 | |
| 10048 | static Jim_Obj *JimStringTrimRight(Jim_Interp *interp, Jim_Obj *strObjPtr, Jim_Obj *trimcharsObjPtr) |
| 10049 | { |
| 10050 | int len; |
| 10051 | const char *trimchars = default_trim_chars; |
| @@ -9406,10 +10079,11 @@ | |
| 10079 | strObjPtr->length = (nontrim - strObjPtr->bytes); |
| 10080 | } |
| 10081 | |
| 10082 | return strObjPtr; |
| 10083 | } |
| 10084 | |
| 10085 | |
| 10086 | static Jim_Obj *JimStringTrim(Jim_Interp *interp, Jim_Obj *strObjPtr, Jim_Obj *trimcharsObjPtr) |
| 10087 | { |
| 10088 | |
| 10089 | Jim_Obj *objPtr = JimStringTrimLeft(interp, strObjPtr, trimcharsObjPtr); |
| @@ -9428,15 +10102,17 @@ | |
| 10102 | |
| 10103 | |
| 10104 | #ifdef HAVE_ISASCII |
| 10105 | #define jim_isascii isascii |
| 10106 | #else |
| 10107 | |
| 10108 | static int jim_isascii(int c) |
| 10109 | { |
| 10110 | return !(c & ~0x7f); |
| 10111 | } |
| 10112 | #endif |
| 10113 | |
| 10114 | |
| 10115 | static int JimStringIs(Jim_Interp *interp, Jim_Obj *strObjPtr, Jim_Obj *strClass, int strict) |
| 10116 | { |
| 10117 | static const char * const strclassnames[] = { |
| 10118 | "integer", "alpha", "alnum", "ascii", "digit", |
| @@ -9538,16 +10214,27 @@ | |
| 10214 | } |
| 10215 | objPtr->internalRep.ptr = (char *)str; |
| 10216 | return 1; |
| 10217 | } |
| 10218 | } |
| 10219 | |
| 10220 | |
| 10221 | |
| 10222 | static int qsortCompareStringPointers(const void *a, const void *b) |
| 10223 | { |
| 10224 | char *const *sa = (char *const *)a; |
| 10225 | char *const *sb = (char *const *)b; |
| 10226 | |
| 10227 | |
| 10228 | if (strcmp(*sa, "--") == 0) { |
| 10229 | return 1; |
| 10230 | } |
| 10231 | if (strcmp(*sb, "--") == 0) { |
| 10232 | |
| 10233 | return -1; |
| 10234 | } |
| 10235 | |
| 10236 | return strcmp(*sa, *sb); |
| 10237 | } |
| 10238 | |
| 10239 | |
| 10240 | |
| @@ -9578,10 +10265,11 @@ | |
| 10265 | NULL, |
| 10266 | NULL, |
| 10267 | NULL, |
| 10268 | JIM_NONE, |
| 10269 | }; |
| 10270 | |
| 10271 | |
| 10272 | static Jim_Obj *JimNewScriptLineObj(Jim_Interp *interp, int argc, int line) |
| 10273 | { |
| 10274 | Jim_Obj *objPtr; |
| 10275 | |
| @@ -9672,17 +10360,19 @@ | |
| 10360 | ParseToken *list; |
| 10361 | int size; |
| 10362 | int count; |
| 10363 | ParseToken static_list[20]; |
| 10364 | } ParseTokenList; |
| 10365 | |
| 10366 | |
| 10367 | static void ScriptTokenListInit(ParseTokenList *tokenlist) |
| 10368 | { |
| 10369 | tokenlist->list = tokenlist->static_list; |
| 10370 | tokenlist->size = sizeof(tokenlist->static_list) / sizeof(ParseToken); |
| 10371 | tokenlist->count = 0; |
| 10372 | } |
| 10373 | |
| 10374 | |
| 10375 | static void ScriptTokenListFree(ParseTokenList *tokenlist) |
| 10376 | { |
| 10377 | if (tokenlist->list != tokenlist->static_list) { |
| 10378 | Jim_Free(tokenlist->list); |
| @@ -9970,10 +10660,11 @@ | |
| 10660 | struct JimParserCtx parser; |
| 10661 | struct ScriptObj *script; |
| 10662 | ParseTokenList tokenlist; |
| 10663 | Jim_Obj *fileNameObj; |
| 10664 | int line; |
| 10665 | int oldtaint; |
| 10666 | |
| 10667 | |
| 10668 | fileNameObj = Jim_GetSourceInfo(interp, objPtr, &line); |
| 10669 | |
| 10670 | |
| @@ -9988,10 +10679,15 @@ | |
| 10679 | |
| 10680 | |
| 10681 | ScriptAddToken(&tokenlist, scriptText + scriptTextLen, 0, JIM_TT_EOF, 0); |
| 10682 | |
| 10683 | |
| 10684 | |
| 10685 | |
| 10686 | oldtaint = interp->taint; |
| 10687 | interp->taint = objPtr->taint; |
| 10688 | |
| 10689 | script = Jim_Alloc(sizeof(*script)); |
| 10690 | memset(script, 0, sizeof(*script)); |
| 10691 | script->inUse = 1; |
| 10692 | script->fileNameObj = fileNameObj; |
| 10693 | Jim_IncrRefCount(script->fileNameObj); |
| @@ -10005,10 +10701,12 @@ | |
| 10701 | |
| 10702 | |
| 10703 | Jim_FreeIntRep(interp, objPtr); |
| 10704 | Jim_SetIntRepPtr(objPtr, script); |
| 10705 | objPtr->typePtr = &scriptObjType; |
| 10706 | |
| 10707 | interp->taint = oldtaint; |
| 10708 | } |
| 10709 | |
| 10710 | static ScriptObj *JimGetScript(Jim_Interp *interp, Jim_Obj *objPtr) |
| 10711 | { |
| 10712 | if (objPtr == interp->emptyObj) { |
| @@ -10033,20 +10731,22 @@ | |
| 10731 | Jim_Free(interp->oldCmdCache); |
| 10732 | interp->oldCmdCache = next; |
| 10733 | } |
| 10734 | interp->oldCmdCacheSize = 0; |
| 10735 | } |
| 10736 | |
| 10737 | |
| 10738 | static void JimIncrCmdRefCount(Jim_Cmd *cmdPtr) |
| 10739 | { |
| 10740 | cmdPtr->inUse++; |
| 10741 | } |
| 10742 | |
| 10743 | |
| 10744 | static void JimDecrCmdRefCount(Jim_Interp *interp, Jim_Cmd *cmdPtr) |
| 10745 | { |
| 10746 | if (--cmdPtr->inUse == 0) { |
| 10747 | if (cmdPtr->flags & JIM_CMD_ISPROC) { |
| 10748 | Jim_DecrRefCount(interp, cmdPtr->u.proc.argListObjPtr); |
| 10749 | Jim_DecrRefCount(interp, cmdPtr->u.proc.bodyObjPtr); |
| 10750 | Jim_DecrRefCount(interp, cmdPtr->u.proc.nsObj); |
| 10751 | if (cmdPtr->u.proc.staticVars) { |
| 10752 | Jim_FreeHashTable(cmdPtr->u.proc.staticVars); |
| @@ -10074,10 +10774,11 @@ | |
| 10774 | |
| 10775 | static void JimIncrVarRef(Jim_VarVal *vv) |
| 10776 | { |
| 10777 | vv->refCount++; |
| 10778 | } |
| 10779 | |
| 10780 | |
| 10781 | static void JimDecrVarRef(Jim_Interp *interp, Jim_VarVal *vv) |
| 10782 | { |
| 10783 | assert(vv->refCount > 0); |
| 10784 | if (--vv->refCount == 0) { |
| @@ -10085,15 +10786,17 @@ | |
| 10786 | Jim_DecrRefCount(interp, vv->objPtr); |
| 10787 | } |
| 10788 | Jim_Free(vv); |
| 10789 | } |
| 10790 | } |
| 10791 | |
| 10792 | |
| 10793 | static void JimVariablesHTValDestructor(void *interp, void *val) |
| 10794 | { |
| 10795 | JimDecrVarRef(interp, val); |
| 10796 | } |
| 10797 | |
| 10798 | |
| 10799 | static unsigned int JimObjectHTHashFunction(const void *key) |
| 10800 | { |
| 10801 | Jim_Obj *keyObj = (Jim_Obj *)key; |
| 10802 | int length; |
| @@ -10125,10 +10828,11 @@ | |
| 10828 | } |
| 10829 | #endif |
| 10830 | string = Jim_GetString(keyObj, &length); |
| 10831 | return Jim_GenHashFunction((const unsigned char *)string, length); |
| 10832 | } |
| 10833 | |
| 10834 | |
| 10835 | static int JimObjectHTKeyCompare(void *privdata, const void *key1, const void *key2) |
| 10836 | { |
| 10837 | return Jim_StringEqObj((Jim_Obj *)key1, (Jim_Obj *)key2); |
| 10838 | } |
| @@ -10136,10 +10840,11 @@ | |
| 10840 | static void *JimObjectHTKeyValDup(void *privdata, const void *val) |
| 10841 | { |
| 10842 | Jim_IncrRefCount((Jim_Obj *)val); |
| 10843 | return (void *)val; |
| 10844 | } |
| 10845 | |
| 10846 | |
| 10847 | static void JimObjectHTKeyValDestructor(void *interp, void *val) |
| 10848 | { |
| 10849 | Jim_DecrRefCount(interp, (Jim_Obj *)val); |
| 10850 | } |
| @@ -10172,25 +10877,28 @@ | |
| 10877 | } |
| 10878 | } |
| 10879 | *length = len; |
| 10880 | return str; |
| 10881 | } |
| 10882 | |
| 10883 | |
| 10884 | static unsigned int JimCommandsHT_HashFunction(const void *key) |
| 10885 | { |
| 10886 | int len; |
| 10887 | const char *str = Jim_GetStringNoQualifier((Jim_Obj *)key, &len); |
| 10888 | return Jim_GenHashFunction((const unsigned char *)str, len); |
| 10889 | } |
| 10890 | |
| 10891 | |
| 10892 | static int JimCommandsHT_KeyCompare(void *privdata, const void *key1, const void *key2) |
| 10893 | { |
| 10894 | int len1, len2; |
| 10895 | const char *str1 = Jim_GetStringNoQualifier((Jim_Obj *)key1, &len1); |
| 10896 | const char *str2 = Jim_GetStringNoQualifier((Jim_Obj *)key2, &len2); |
| 10897 | return len1 == len2 && *str1 == *str2 && memcmp(str1, str2, len1) == 0; |
| 10898 | } |
| 10899 | |
| 10900 | |
| 10901 | static void JimCommandsHT_ValDestructor(void *interp, void *val) |
| 10902 | { |
| 10903 | JimDecrCmdRefCount(interp, val); |
| 10904 | } |
| @@ -10262,35 +10970,50 @@ | |
| 10970 | |
| 10971 | |
| 10972 | Jim_ReplaceHashEntry(&interp->commands, nameObjPtr, cmd); |
| 10973 | } |
| 10974 | |
| 10975 | |
| 10976 | Jim_Cmd *Jim_RegisterCommand(Jim_Interp *interp, Jim_Obj *cmdNameObj, |
| 10977 | Jim_CmdProc *cmdProc, |
| 10978 | Jim_DelCmdProc *delProc, |
| 10979 | const char *usage, |
| 10980 | const char *help, |
| 10981 | short minargs, |
| 10982 | short maxargs, |
| 10983 | int flags, |
| 10984 | void *privData) |
| 10985 | { |
| 10986 | JimPanic(((flags & JIM_CMD_ISPROC), "Jim_RegisterCommand called with JIM_CMD_ISPROC flag")); |
| 10987 | Jim_Cmd *cmdPtr = Jim_Alloc(sizeof(*cmdPtr)); |
| 10988 | |
| 10989 | |
| 10990 | memset(cmdPtr, 0, sizeof(*cmdPtr)); |
| 10991 | cmdPtr->inUse = 1; |
| 10992 | cmdPtr->flags = flags; |
| 10993 | cmdPtr->u.native.delProc = delProc; |
| 10994 | cmdPtr->u.native.cmdProc = cmdProc; |
| 10995 | cmdPtr->u.native.usage = usage; |
| 10996 | cmdPtr->u.native.help = help; |
| 10997 | cmdPtr->u.native.minargs = minargs; |
| 10998 | cmdPtr->u.native.maxargs = maxargs; |
| 10999 | cmdPtr->u.native.privData = privData; |
| 11000 | |
| 11001 | Jim_IncrRefCount(cmdNameObj); |
| 11002 | JimCreateCommand(interp, cmdNameObj, cmdPtr); |
| 11003 | Jim_DecrRefCount(interp, cmdNameObj); |
| 11004 | |
| 11005 | return cmdPtr; |
| 11006 | } |
| 11007 | |
| 11008 | int Jim_CreateCommand(Jim_Interp *interp, const char *cmdNameStr, |
| 11009 | Jim_CmdProc *cmdProc, void *privData, Jim_DelCmdProc *delProc) |
| 11010 | { |
| 11011 | Jim_RegisterCmd(interp, cmdNameStr, NULL, 0, -1, cmdProc, delProc, privData, 0); |
| 11012 | return JIM_OK; |
| 11013 | } |
| 11014 | |
| 11015 | |
| 11016 | static int JimCreateProcedureStatics(Jim_Interp *interp, Jim_Cmd *cmdPtr, Jim_Obj *staticsListObjPtr) |
| 11017 | { |
| 11018 | int len, i; |
| 11019 | |
| @@ -10347,10 +11070,15 @@ | |
| 11070 | if (byref) { |
| 11071 | vv = nameObjPtr->internalRep.varValue.vv; |
| 11072 | } |
| 11073 | else { |
| 11074 | initObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_NONE); |
| 11075 | if (!initObjPtr) { |
| 11076 | Jim_SetResultFormatted(interp, "Could not resolve upvar \"%#s\"'s value", nameObjPtr); |
| 11077 | Jim_DecrRefCount(interp, nameObjPtr); |
| 11078 | return JIM_ERR; |
| 11079 | } |
| 11080 | } |
| 11081 | break; |
| 11082 | |
| 11083 | case JIM_ERR: |
| 11084 | |
| @@ -10402,11 +11130,11 @@ | |
| 11130 | #endif |
| 11131 | |
| 11132 | static void JimUpdateProcNamespace(Jim_Interp *interp, Jim_Cmd *cmdPtr, Jim_Obj *nameObjPtr) |
| 11133 | { |
| 11134 | #ifdef jim_ext_namespace |
| 11135 | if (cmdPtr->flags & JIM_CMD_ISPROC) { |
| 11136 | int len; |
| 11137 | const char *cmdname = Jim_GetStringNoQualifier(nameObjPtr, &len); |
| 11138 | |
| 11139 | const char *pt = Jim_memrchr(cmdname, ':', len); |
| 11140 | if (pt && pt != cmdname && pt[-1] == ':') { |
| @@ -10438,11 +11166,11 @@ | |
| 11166 | |
| 11167 | cmdPtr = Jim_Alloc(sizeof(*cmdPtr) + sizeof(struct Jim_ProcArg) * argListLen); |
| 11168 | assert(cmdPtr); |
| 11169 | memset(cmdPtr, 0, sizeof(*cmdPtr)); |
| 11170 | cmdPtr->inUse = 1; |
| 11171 | cmdPtr->flags = JIM_CMD_ISPROC; |
| 11172 | cmdPtr->u.proc.argListObjPtr = argListObjPtr; |
| 11173 | cmdPtr->u.proc.argListLen = argListLen; |
| 11174 | cmdPtr->u.proc.bodyObjPtr = bodyObjPtr; |
| 11175 | cmdPtr->u.proc.argsPos = -1; |
| 11176 | cmdPtr->u.proc.arglist = (struct Jim_ProcArg *)(cmdPtr + 1); |
| @@ -10581,10 +11309,11 @@ | |
| 11309 | |
| 11310 | static void FreeCommandInternalRep(Jim_Interp *interp, Jim_Obj *objPtr) |
| 11311 | { |
| 11312 | Jim_DecrRefCount(interp, objPtr->internalRep.cmdValue.nsObj); |
| 11313 | } |
| 11314 | |
| 11315 | |
| 11316 | static void DupCommandInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr) |
| 11317 | { |
| 11318 | dupPtr->internalRep.cmdValue = srcPtr->internalRep.cmdValue; |
| 11319 | dupPtr->typePtr = srcPtr->typePtr; |
| @@ -10638,11 +11367,11 @@ | |
| 11367 | objPtr->internalRep.cmdValue.cmdPtr = cmd; |
| 11368 | objPtr->internalRep.cmdValue.nsObj = interp->framePtr->nsObj; |
| 11369 | Jim_IncrRefCount(interp->framePtr->nsObj); |
| 11370 | Jim_DecrRefCount(interp, qualifiedNameObj); |
| 11371 | } |
| 11372 | while ((cmd->flags & JIM_CMD_ISPROC) && cmd->u.proc.upcall) { |
| 11373 | cmd = cmd->prevCmd; |
| 11374 | } |
| 11375 | return cmd; |
| 11376 | } |
| 11377 | |
| @@ -10720,30 +11449,34 @@ | |
| 11449 | return JIM_OK; |
| 11450 | } |
| 11451 | |
| 11452 | |
| 11453 | static int JimDictSugarSet(Jim_Interp *interp, Jim_Obj *ObjPtr, Jim_Obj *valObjPtr); |
| 11454 | static Jim_Obj *JimDictSugarGet(Jim_Interp *interp, Jim_Obj *objPtr, int flags); |
| 11455 | |
| 11456 | |
| 11457 | static int JimSetNewVariable(Jim_HashTable *ht, Jim_Obj *nameObjPtr, Jim_VarVal *vv) |
| 11458 | { |
| 11459 | return Jim_AddHashEntry(ht, nameObjPtr, vv); |
| 11460 | } |
| 11461 | |
| 11462 | |
| 11463 | static Jim_VarVal *JimFindVariable(Jim_HashTable *ht, Jim_Obj *nameObjPtr) |
| 11464 | { |
| 11465 | Jim_HashEntry *he = Jim_FindHashEntry(ht, nameObjPtr); |
| 11466 | if (he) { |
| 11467 | return (Jim_VarVal *)Jim_GetHashEntryVal(he); |
| 11468 | } |
| 11469 | return NULL; |
| 11470 | } |
| 11471 | |
| 11472 | |
| 11473 | static int JimUnsetVariable(Jim_HashTable *ht, Jim_Obj *nameObjPtr) |
| 11474 | { |
| 11475 | return Jim_DeleteHashEntry(ht, nameObjPtr); |
| 11476 | } |
| 11477 | |
| 11478 | |
| 11479 | static Jim_VarVal *JimCreateVariable(Jim_Interp *interp, Jim_Obj *nameObjPtr, Jim_Obj *valObjPtr) |
| 11480 | { |
| 11481 | const char *name; |
| 11482 | Jim_CallFrame *framePtr; |
| @@ -10784,19 +11517,22 @@ | |
| 11517 | return vv; |
| 11518 | } |
| 11519 | |
| 11520 | int Jim_SetVariable(Jim_Interp *interp, Jim_Obj *nameObjPtr, Jim_Obj *valObjPtr) |
| 11521 | { |
| 11522 | int ret = JIM_OK; |
| 11523 | Jim_VarVal *vv; |
| 11524 | |
| 11525 | switch (SetVariableFromAny(interp, nameObjPtr)) { |
| 11526 | case JIM_DICT_SUGAR: |
| 11527 | ret = JimDictSugarSet(interp, nameObjPtr, valObjPtr); |
| 11528 | break; |
| 11529 | |
| 11530 | case JIM_ERR: |
| 11531 | if (JimCreateVariable(interp, nameObjPtr, valObjPtr) == NULL) { |
| 11532 | ret = JIM_ERR; |
| 11533 | } |
| 11534 | break; |
| 11535 | |
| 11536 | case JIM_OK: |
| 11537 | vv = nameObjPtr->internalRep.varValue.vv; |
| 11538 | if (vv->linkFramePtr == NULL) { |
| @@ -10807,17 +11543,20 @@ | |
| 11543 | else { |
| 11544 | Jim_CallFrame *savedCallFrame; |
| 11545 | |
| 11546 | savedCallFrame = interp->framePtr; |
| 11547 | interp->framePtr = vv->linkFramePtr; |
| 11548 | ret = Jim_SetVariable(interp, vv->objPtr, valObjPtr); |
| 11549 | interp->framePtr = savedCallFrame; |
| 11550 | } |
| 11551 | break; |
| 11552 | } |
| 11553 | if (ret != JIM_OK && valObjPtr->refCount == 0) { |
| 11554 | |
| 11555 | Jim_FreeNewObj(interp, valObjPtr); |
| 11556 | } |
| 11557 | return ret; |
| 11558 | } |
| 11559 | |
| 11560 | int Jim_SetVariableStr(Jim_Interp *interp, const char *name, Jim_Obj *objPtr) |
| 11561 | { |
| 11562 | Jim_Obj *nameObjPtr; |
| @@ -11087,19 +11826,21 @@ | |
| 11826 | |
| 11827 | p = strchr(str, '('); |
| 11828 | JimPanic((p == NULL, "JimDictSugarParseVarKey() called for non-dict-sugar (%s)", str)); |
| 11829 | |
| 11830 | varObjPtr = Jim_NewStringObj(interp, str, p - str); |
| 11831 | varObjPtr->taint = objPtr->taint; |
| 11832 | |
| 11833 | p++; |
| 11834 | keyLen = (str + len) - p; |
| 11835 | if (str[len - 1] == ')') { |
| 11836 | keyLen--; |
| 11837 | } |
| 11838 | |
| 11839 | |
| 11840 | keyObjPtr = Jim_NewStringObj(interp, p, keyLen); |
| 11841 | keyObjPtr->taint = objPtr->taint; |
| 11842 | |
| 11843 | Jim_IncrRefCount(varObjPtr); |
| 11844 | Jim_IncrRefCount(keyObjPtr); |
| 11845 | *varPtrPtr = varObjPtr; |
| 11846 | *keyPtrPtr = keyObjPtr; |
| @@ -11175,10 +11916,11 @@ | |
| 11916 | void FreeDictSubstInternalRep(Jim_Interp *interp, Jim_Obj *objPtr) |
| 11917 | { |
| 11918 | Jim_DecrRefCount(interp, objPtr->internalRep.dictSubstValue.varNameObjPtr); |
| 11919 | Jim_DecrRefCount(interp, objPtr->internalRep.dictSubstValue.indexObjPtr); |
| 11920 | } |
| 11921 | |
| 11922 | |
| 11923 | static void DupDictSubstInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr) |
| 11924 | { |
| 11925 | |
| 11926 | dupPtr->internalRep = srcPtr->internalRep; |
| @@ -11270,10 +12012,11 @@ | |
| 12012 | cf->nsObj = nsObj; |
| 12013 | Jim_IncrRefCount(nsObj); |
| 12014 | |
| 12015 | return cf; |
| 12016 | } |
| 12017 | |
| 12018 | |
| 12019 | static int JimDeleteLocalProcs(Jim_Interp *interp, Jim_Stack *localCommands) |
| 12020 | { |
| 12021 | |
| 12022 | if (localCommands) { |
| @@ -11298,11 +12041,11 @@ | |
| 12041 | Jim_DeleteHashEntry(ht, cmdNameObj); |
| 12042 | } |
| 12043 | } |
| 12044 | Jim_DecrRefCount(interp, cmdNameObj); |
| 12045 | } |
| 12046 | Jim_StackFree(localCommands); |
| 12047 | Jim_Free(localCommands); |
| 12048 | } |
| 12049 | return JIM_OK; |
| 12050 | } |
| 12051 | |
| @@ -11352,10 +12095,11 @@ | |
| 12095 | return retcode; |
| 12096 | } |
| 12097 | |
| 12098 | #define JIM_FCF_FULL 0 |
| 12099 | #define JIM_FCF_REUSE 1 |
| 12100 | |
| 12101 | static void JimFreeCallFrame(Jim_Interp *interp, Jim_CallFrame *cf, int action) |
| 12102 | { |
| 12103 | JimDeleteLocalProcs(interp, cf->localCommands); |
| 12104 | |
| 12105 | if (cf->procArgsObjPtr) |
| @@ -11456,10 +12200,13 @@ | |
| 12200 | JimInvokeDefer(i, JIM_OK); |
| 12201 | cfx = cf->parent; |
| 12202 | JimFreeCallFrame(i, cf, JIM_FCF_FULL); |
| 12203 | } |
| 12204 | |
| 12205 | |
| 12206 | Jim_FreeHashTable(&i->commands); |
| 12207 | |
| 12208 | Jim_DecrRefCount(i, i->emptyObj); |
| 12209 | Jim_DecrRefCount(i, i->trueObj); |
| 12210 | Jim_DecrRefCount(i, i->falseObj); |
| 12211 | Jim_DecrRefCount(i, i->result); |
| 12212 | Jim_DecrRefCount(i, i->stackTrace); |
| @@ -11467,12 +12214,10 @@ | |
| 12214 | Jim_DecrRefCount(i, i->unknown); |
| 12215 | Jim_DecrRefCount(i, i->defer); |
| 12216 | Jim_DecrRefCount(i, i->nullScriptObj); |
| 12217 | Jim_DecrRefCount(i, i->currentFilenameObj); |
| 12218 | |
| 12219 | |
| 12220 | Jim_InterpIncrProcEpoch(i); |
| 12221 | |
| 12222 | #ifdef JIM_REFERENCES |
| 12223 | Jim_FreeHashTable(&i->references); |
| @@ -11602,10 +12347,11 @@ | |
| 12347 | } |
| 12348 | } |
| 12349 | } |
| 12350 | return NULL; |
| 12351 | } |
| 12352 | |
| 12353 | |
| 12354 | static Jim_EvalFrame *JimGetEvalFrameByProcLevel(Jim_Interp *interp, int proclevel) |
| 12355 | { |
| 12356 | Jim_EvalFrame *evalFrame; |
| 12357 | |
| @@ -11632,11 +12378,11 @@ | |
| 12378 | static Jim_Obj *JimProcForEvalFrame(Jim_Interp *interp, Jim_EvalFrame *frame) |
| 12379 | { |
| 12380 | if (frame == interp->evalFrame || (frame->cmd && frame->cmd->cmdNameObj)) { |
| 12381 | Jim_EvalFrame *e; |
| 12382 | for (e = frame->parent; e; e = e->parent) { |
| 12383 | if (e->cmd && (e->cmd->flags & JIM_CMD_ISPROC) && e->cmd->cmdNameObj) { |
| 12384 | break; |
| 12385 | } |
| 12386 | } |
| 12387 | if (e && e->cmd && e->cmd->cmdNameObj) { |
| 12388 | return e->cmd->cmdNameObj; |
| @@ -11660,23 +12406,25 @@ | |
| 12406 | Jim_ListAppendElement(interp, listObj, procNameObj ? procNameObj : interp->emptyObj); |
| 12407 | Jim_ListAppendElement(interp, listObj, fileNameObj); |
| 12408 | Jim_ListAppendElement(interp, listObj, Jim_NewIntObj(interp, linenr)); |
| 12409 | Jim_ListAppendElement(interp, listObj, Jim_NewListObj(interp, frame->argv, frame->argc)); |
| 12410 | } |
| 12411 | |
| 12412 | |
| 12413 | static void JimSetStackTrace(Jim_Interp *interp, Jim_Obj *stackTraceObj) |
| 12414 | { |
| 12415 | |
| 12416 | Jim_IncrRefCount(stackTraceObj); |
| 12417 | Jim_DecrRefCount(interp, interp->stackTrace); |
| 12418 | interp->stackTrace = stackTraceObj; |
| 12419 | interp->hasErrorStackTrace = 1; |
| 12420 | } |
| 12421 | |
| 12422 | |
| 12423 | static void JimSetErrorStack(Jim_Interp *interp, ScriptObj *script) |
| 12424 | { |
| 12425 | if (!interp->hasErrorStackTrace) { |
| 12426 | int i; |
| 12427 | Jim_Obj *stackTrace = Jim_NewListObj(interp, NULL, 0); |
| 12428 | |
| 12429 | if (interp->procLevel == 0 && script) { |
| 12430 | Jim_ListAppendElement(interp, stackTrace, interp->emptyObj); |
| @@ -11744,10 +12492,11 @@ | |
| 12492 | NULL, |
| 12493 | UpdateStringOfInt, |
| 12494 | JIM_TYPE_NONE, |
| 12495 | }; |
| 12496 | |
| 12497 | |
| 12498 | |
| 12499 | static void UpdateStringOfInt(struct Jim_Obj *objPtr) |
| 12500 | { |
| 12501 | char buf[JIM_INTEGER_SPACE + 1]; |
| 12502 | jim_wide wideValue = JimWideValue(objPtr); |
| @@ -11779,10 +12528,11 @@ | |
| 12528 | } |
| 12529 | buf[pos] = 0; |
| 12530 | |
| 12531 | JimSetStringBytes(objPtr, buf); |
| 12532 | } |
| 12533 | |
| 12534 | |
| 12535 | static int SetIntFromAny(Jim_Interp *interp, Jim_Obj *objPtr, int flags) |
| 12536 | { |
| 12537 | jim_wide wideValue; |
| 12538 | const char *str; |
| @@ -11812,10 +12562,11 @@ | |
| 12562 | objPtr->internalRep.wideValue = wideValue; |
| 12563 | return JIM_OK; |
| 12564 | } |
| 12565 | |
| 12566 | #ifdef JIM_OPTIMIZATION |
| 12567 | |
| 12568 | static int JimIsWide(Jim_Obj *objPtr) |
| 12569 | { |
| 12570 | return objPtr->typePtr == &intObjType; |
| 12571 | } |
| 12572 | #endif |
| @@ -11881,10 +12632,11 @@ | |
| 12632 | Jim_Obj *objPtr; |
| 12633 | |
| 12634 | objPtr = Jim_NewObj(interp); |
| 12635 | objPtr->typePtr = &intObjType; |
| 12636 | objPtr->bytes = NULL; |
| 12637 | objPtr->taint = 0; |
| 12638 | objPtr->internalRep.wideValue = wideValue; |
| 12639 | return objPtr; |
| 12640 | } |
| 12641 | |
| 12642 | #define JIM_DOUBLE_SPACE 30 |
| @@ -11906,10 +12658,11 @@ | |
| 12658 | #endif |
| 12659 | #if !HAVE_DECL_ISINF |
| 12660 | #undef isinf |
| 12661 | #define isinf(X) (1.0 / (X) == 0.0) |
| 12662 | #endif |
| 12663 | |
| 12664 | |
| 12665 | static void UpdateStringOfDouble(struct Jim_Obj *objPtr) |
| 12666 | { |
| 12667 | double value = objPtr->internalRep.doubleValue; |
| 12668 | |
| @@ -11951,10 +12704,11 @@ | |
| 12704 | buf[i] = '\0'; |
| 12705 | } |
| 12706 | JimSetStringBytes(objPtr, buf); |
| 12707 | } |
| 12708 | } |
| 12709 | |
| 12710 | |
| 12711 | static int SetDoubleFromAny(Jim_Interp *interp, Jim_Obj *objPtr) |
| 12712 | { |
| 12713 | double doubleValue; |
| 12714 | jim_wide wideValue; |
| @@ -12020,10 +12774,11 @@ | |
| 12774 | Jim_Obj *objPtr; |
| 12775 | |
| 12776 | objPtr = Jim_NewObj(interp); |
| 12777 | objPtr->typePtr = &doubleObjType; |
| 12778 | objPtr->bytes = NULL; |
| 12779 | objPtr->taint = 0; |
| 12780 | objPtr->internalRep.doubleValue = doubleValue; |
| 12781 | return objPtr; |
| 12782 | } |
| 12783 | |
| 12784 | static int SetBooleanFromAny(Jim_Interp *interp, Jim_Obj *objPtr, int flags); |
| @@ -12043,10 +12798,11 @@ | |
| 12798 | |
| 12799 | static const int jim_true_false_lens[8] = { |
| 12800 | 1, 4, 3, 2, |
| 12801 | 1, 5, 2, 3, |
| 12802 | }; |
| 12803 | |
| 12804 | |
| 12805 | static int SetBooleanFromAny(Jim_Interp *interp, Jim_Obj *objPtr, int flags) |
| 12806 | { |
| 12807 | int index = Jim_FindByName(Jim_String(objPtr), jim_true_false_strings, |
| 12808 | sizeof(jim_true_false_strings) / sizeof(*jim_true_false_strings)); |
| @@ -12109,10 +12865,11 @@ | |
| 12865 | } |
| 12866 | |
| 12867 | #define JIM_ELESTR_SIMPLE 0 |
| 12868 | #define JIM_ELESTR_BRACE 1 |
| 12869 | #define JIM_ELESTR_QUOTE 2 |
| 12870 | |
| 12871 | static unsigned char ListElementQuotingType(const char *s, int len) |
| 12872 | { |
| 12873 | int i, level, blevel, trySimple = 1; |
| 12874 | |
| 12875 | |
| @@ -12255,10 +13012,11 @@ | |
| 13012 | } |
| 13013 | *p = '\0'; |
| 13014 | |
| 13015 | return p - q; |
| 13016 | } |
| 13017 | |
| 13018 | |
| 13019 | static void JimMakeListStringRep(Jim_Obj *objPtr, Jim_Obj **objv, int objc) |
| 13020 | { |
| 13021 | #define STATIC_QUOTING_LEN 32 |
| 13022 | int i, bufLen, realLength; |
| @@ -12341,15 +13099,17 @@ | |
| 13099 | |
| 13100 | if (quotingType != staticQuoting) { |
| 13101 | Jim_Free(quotingType); |
| 13102 | } |
| 13103 | } |
| 13104 | |
| 13105 | |
| 13106 | static void UpdateStringOfList(struct Jim_Obj *objPtr) |
| 13107 | { |
| 13108 | JimMakeListStringRep(objPtr, objPtr->internalRep.listValue.ele, objPtr->internalRep.listValue.len); |
| 13109 | } |
| 13110 | |
| 13111 | |
| 13112 | static int SetListFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr) |
| 13113 | { |
| 13114 | struct JimParserCtx parser; |
| 13115 | const char *str; |
| @@ -12401,10 +13161,11 @@ | |
| 13161 | JimParseList(&parser); |
| 13162 | if (parser.tt != JIM_TT_STR && parser.tt != JIM_TT_ESC) |
| 13163 | continue; |
| 13164 | elementPtr = JimParserGetTokenObj(interp, &parser); |
| 13165 | Jim_SetSourceInfo(interp, elementPtr, fileNameObj, parser.tline); |
| 13166 | elementPtr->taint = objPtr->taint; |
| 13167 | ListAppendElement(objPtr, elementPtr); |
| 13168 | } |
| 13169 | } |
| 13170 | Jim_DecrRefCount(interp, fileNameObj); |
| 13171 | return JIM_OK; |
| @@ -12415,10 +13176,11 @@ | |
| 13176 | Jim_Obj *objPtr; |
| 13177 | |
| 13178 | objPtr = Jim_NewObj(interp); |
| 13179 | objPtr->typePtr = &listObjType; |
| 13180 | objPtr->bytes = NULL; |
| 13181 | objPtr->taint = 0; |
| 13182 | objPtr->internalRep.listValue.ele = NULL; |
| 13183 | objPtr->internalRep.listValue.len = 0; |
| 13184 | objPtr->internalRep.listValue.maxLen = 0; |
| 13185 | |
| 13186 | if (len) { |
| @@ -12466,10 +13228,11 @@ | |
| 13228 | int unique; |
| 13229 | int (*subfn)(Jim_Obj **, Jim_Obj **); |
| 13230 | }; |
| 13231 | |
| 13232 | static struct lsort_info *sort_info; |
| 13233 | |
| 13234 | |
| 13235 | static int ListSortIndexHelper(Jim_Obj **lhsObj, Jim_Obj **rhsObj) |
| 13236 | { |
| 13237 | Jim_Obj *lObj, *rObj; |
| 13238 | |
| @@ -12483,15 +13246,17 @@ | |
| 13246 | |
| 13247 | static int ListSortString(Jim_Obj **lhsObj, Jim_Obj **rhsObj) |
| 13248 | { |
| 13249 | return Jim_StringCompareObj(sort_info->interp, *lhsObj, *rhsObj, 0) * sort_info->order; |
| 13250 | } |
| 13251 | |
| 13252 | |
| 13253 | static int ListSortStringNoCase(Jim_Obj **lhsObj, Jim_Obj **rhsObj) |
| 13254 | { |
| 13255 | return Jim_StringCompareObj(sort_info->interp, *lhsObj, *rhsObj, 1) * sort_info->order; |
| 13256 | } |
| 13257 | |
| 13258 | |
| 13259 | static int ListSortDict(Jim_Obj **lhsObj, Jim_Obj **rhsObj) |
| 13260 | { |
| 13261 | |
| 13262 | const char *left = Jim_String(*lhsObj); |
| @@ -12525,10 +13290,11 @@ | |
| 13290 | return Jim_StringCompareObj(sort_info->interp, *lhsObj, *rhsObj, 0) * sort_info->order; |
| 13291 | } |
| 13292 | } |
| 13293 | } |
| 13294 | } |
| 13295 | |
| 13296 | |
| 13297 | static int ListSortInteger(Jim_Obj **lhsObj, Jim_Obj **rhsObj) |
| 13298 | { |
| 13299 | jim_wide lhs = 0, rhs = 0; |
| 13300 | |
| @@ -12537,10 +13303,11 @@ | |
| 13303 | longjmp(sort_info->jmpbuf, JIM_ERR); |
| 13304 | } |
| 13305 | |
| 13306 | return JimSign(lhs - rhs) * sort_info->order; |
| 13307 | } |
| 13308 | |
| 13309 | |
| 13310 | static int ListSortReal(Jim_Obj **lhsObj, Jim_Obj **rhsObj) |
| 13311 | { |
| 13312 | double lhs = 0, rhs = 0; |
| 13313 | |
| @@ -12554,10 +13321,11 @@ | |
| 13321 | if (lhs > rhs) { |
| 13322 | return sort_info->order; |
| 13323 | } |
| 13324 | return -sort_info->order; |
| 13325 | } |
| 13326 | |
| 13327 | |
| 13328 | static int ListSortCommand(Jim_Obj **lhsObj, Jim_Obj **rhsObj) |
| 13329 | { |
| 13330 | Jim_Obj *compare_script; |
| 13331 | int rc; |
| @@ -12711,10 +13479,11 @@ | |
| 13479 | } |
| 13480 | point = listPtr->internalRep.listValue.ele + idx; |
| 13481 | memmove(point + elemc, point, (currentLen - idx) * sizeof(Jim_Obj *)); |
| 13482 | for (i = 0; i < elemc; ++i) { |
| 13483 | point[i] = elemVec[i]; |
| 13484 | listPtr->taint |= point[i]->taint; |
| 13485 | Jim_IncrRefCount(point[i]); |
| 13486 | } |
| 13487 | listPtr->internalRep.listValue.len += elemc; |
| 13488 | } |
| 13489 | |
| @@ -12827,10 +13596,11 @@ | |
| 13596 | err: |
| 13597 | if (idxes != static_idxes) |
| 13598 | Jim_Free(idxes); |
| 13599 | return ret; |
| 13600 | } |
| 13601 | |
| 13602 | |
| 13603 | static int ListSetIndex(Jim_Interp *interp, Jim_Obj *listPtr, int idx, |
| 13604 | Jim_Obj *newObjPtr, int flags) |
| 13605 | { |
| 13606 | SetListFromAny(interp, listPtr); |
| @@ -12843,10 +13613,11 @@ | |
| 13613 | } |
| 13614 | if (idx < 0) |
| 13615 | idx = listPtr->internalRep.listValue.len + idx; |
| 13616 | Jim_DecrRefCount(interp, listPtr->internalRep.listValue.ele[idx]); |
| 13617 | listPtr->internalRep.listValue.ele[idx] = newObjPtr; |
| 13618 | listPtr->taint |= newObjPtr->taint; |
| 13619 | Jim_IncrRefCount(newObjPtr); |
| 13620 | return JIM_OK; |
| 13621 | } |
| 13622 | |
| 13623 | int Jim_ListSetIndex(Jim_Interp *interp, Jim_Obj *varNamePtr, |
| @@ -12880,12 +13651,13 @@ | |
| 13651 | goto err; |
| 13652 | if (ListSetIndex(interp, objPtr, idx, newObjPtr, JIM_ERRMSG) == JIM_ERR) |
| 13653 | goto err; |
| 13654 | Jim_InvalidateStringRep(objPtr); |
| 13655 | Jim_InvalidateStringRep(varObjPtr); |
| 13656 | if (Jim_SetVariable(interp, varNamePtr, varObjPtr) != JIM_OK) { |
| 13657 | return JIM_ERR; |
| 13658 | } |
| 13659 | Jim_SetResult(interp, varObjPtr); |
| 13660 | return JIM_OK; |
| 13661 | err: |
| 13662 | if (shared) { |
| 13663 | Jim_FreeNewObj(interp, varObjPtr); |
| @@ -13134,15 +13906,17 @@ | |
| 13906 | #ifdef JIM_RANDOMISE_HASH |
| 13907 | dict->uniq = (rand() ^ time(NULL) ^ clock()); |
| 13908 | #endif |
| 13909 | return dict; |
| 13910 | } |
| 13911 | |
| 13912 | |
| 13913 | static void FreeDictInternalRep(Jim_Interp *interp, Jim_Obj *objPtr) |
| 13914 | { |
| 13915 | JimFreeDict(interp, objPtr->internalRep.dictValue); |
| 13916 | } |
| 13917 | |
| 13918 | |
| 13919 | static void DupDictInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr) |
| 13920 | { |
| 13921 | Jim_Dict *oldDict = srcPtr->internalRep.dictValue; |
| 13922 | int i; |
| @@ -13164,15 +13938,17 @@ | |
| 13938 | memcpy(newDict->ht, oldDict->ht, sizeof(*oldDict->ht) * oldDict->size); |
| 13939 | |
| 13940 | dupPtr->internalRep.dictValue = newDict; |
| 13941 | dupPtr->typePtr = &dictObjType; |
| 13942 | } |
| 13943 | |
| 13944 | |
| 13945 | static void UpdateStringOfDict(struct Jim_Obj *objPtr) |
| 13946 | { |
| 13947 | JimMakeListStringRep(objPtr, objPtr->internalRep.dictValue->table, objPtr->internalRep.dictValue->len); |
| 13948 | } |
| 13949 | |
| 13950 | |
| 13951 | static int SetDictFromAny(Jim_Interp *interp, struct Jim_Obj *objPtr) |
| 13952 | { |
| 13953 | int listlen; |
| 13954 | |
| @@ -13279,10 +14055,11 @@ | |
| 14055 | |
| 14056 | dict->table[dict->len++] = keyObjPtr; |
| 14057 | dict->table[dict->len++] = valueObjPtr; |
| 14058 | |
| 14059 | } |
| 14060 | objPtr->taint |= keyObjPtr->taint | valueObjPtr->taint; |
| 14061 | return JIM_OK; |
| 14062 | } |
| 14063 | } |
| 14064 | |
| 14065 | int Jim_DictAddElement(Jim_Interp *interp, Jim_Obj *objPtr, |
| @@ -13390,11 +14167,10 @@ | |
| 14167 | |
| 14168 | return JIM_ERR; |
| 14169 | } |
| 14170 | varObjPtr = objPtr = Jim_NewDictObj(interp, NULL, 0); |
| 14171 | if (Jim_SetVariable(interp, varNamePtr, objPtr) != JIM_OK) { |
| 14172 | return JIM_ERR; |
| 14173 | } |
| 14174 | } |
| 14175 | if ((shared = Jim_IsShared(objPtr))) |
| 14176 | varObjPtr = objPtr = Jim_DuplicateObj(interp, objPtr); |
| @@ -13411,10 +14187,13 @@ | |
| 14187 | if (Jim_DictAddElement(interp, objPtr, keyv[keyc - 1], newObjPtr) != JIM_OK) { |
| 14188 | if (newObjPtr || (flags & JIM_MUSTEXIST)) { |
| 14189 | goto err; |
| 14190 | } |
| 14191 | } |
| 14192 | if (newObjPtr) { |
| 14193 | varObjPtr->taint |= newObjPtr->taint; |
| 14194 | } |
| 14195 | break; |
| 14196 | } |
| 14197 | |
| 14198 | |
| 14199 | Jim_InvalidateStringRep(dictObjPtr); |
| @@ -13435,11 +14214,11 @@ | |
| 14214 | } |
| 14215 | |
| 14216 | Jim_InvalidateStringRep(objPtr); |
| 14217 | Jim_InvalidateStringRep(varObjPtr); |
| 14218 | if (Jim_SetVariable(interp, varNamePtr, varObjPtr) != JIM_OK) { |
| 14219 | return JIM_ERR; |
| 14220 | } |
| 14221 | |
| 14222 | if (!(flags & JIM_NORESULT)) { |
| 14223 | Jim_SetResult(interp, varObjPtr); |
| 14224 | } |
| @@ -13459,10 +14238,11 @@ | |
| 14238 | NULL, |
| 14239 | NULL, |
| 14240 | UpdateStringOfIndex, |
| 14241 | JIM_TYPE_NONE, |
| 14242 | }; |
| 14243 | |
| 14244 | |
| 14245 | static void UpdateStringOfIndex(struct Jim_Obj *objPtr) |
| 14246 | { |
| 14247 | if (objPtr->internalRep.intValue == -1) { |
| 14248 | JimSetStringBytes(objPtr, "end"); |
| @@ -13477,10 +14257,11 @@ | |
| 14257 | sprintf(buf, "end%d", objPtr->internalRep.intValue + 1); |
| 14258 | } |
| 14259 | JimSetStringBytes(objPtr, buf); |
| 14260 | } |
| 14261 | } |
| 14262 | |
| 14263 | |
| 14264 | static int SetIndexFromAny(Jim_Interp *interp, Jim_Obj *objPtr) |
| 14265 | { |
| 14266 | jim_wide idx; |
| 14267 | int end = 0; |
| @@ -13530,10 +14311,13 @@ | |
| 14311 | idx--; |
| 14312 | } |
| 14313 | } |
| 14314 | else if (idx < 0) { |
| 14315 | idx = -INT_MAX; |
| 14316 | } |
| 14317 | else if (idx > INT_MAX) { |
| 14318 | goto badindex; |
| 14319 | } |
| 14320 | |
| 14321 | |
| 14322 | Jim_FreeIntRep(interp, objPtr); |
| 14323 | objPtr->typePtr = &indexObjType; |
| @@ -13597,10 +14381,11 @@ | |
| 14381 | } |
| 14382 | else { |
| 14383 | return jimReturnCodes[code]; |
| 14384 | } |
| 14385 | } |
| 14386 | |
| 14387 | |
| 14388 | static int SetReturnCodeFromAny(Jim_Interp *interp, Jim_Obj *objPtr) |
| 14389 | { |
| 14390 | int returnCode; |
| 14391 | jim_wide wideValue; |
| @@ -13663,10 +14448,13 @@ | |
| 14448 | JIM_EXPROP_POW, |
| 14449 | |
| 14450 | |
| 14451 | JIM_EXPROP_STREQ, |
| 14452 | JIM_EXPROP_STRNE, |
| 14453 | JIM_EXPROP_STRGLOB, |
| 14454 | JIM_EXPROP_STRRE, |
| 14455 | |
| 14456 | JIM_EXPROP_STRIN, |
| 14457 | JIM_EXPROP_STRNI, |
| 14458 | JIM_EXPROP_STRLT, |
| 14459 | JIM_EXPROP_STRGT, |
| 14460 | JIM_EXPROP_STRLE, |
| @@ -13730,10 +14518,11 @@ | |
| 14518 | } Jim_ExprOperator; |
| 14519 | |
| 14520 | static int JimExprGetTerm(Jim_Interp *interp, struct JimExprNode *node, Jim_Obj **objPtrPtr); |
| 14521 | static int JimExprGetTermBoolean(Jim_Interp *interp, struct JimExprNode *node); |
| 14522 | static int JimExprEvalTermNode(Jim_Interp *interp, struct JimExprNode *node); |
| 14523 | |
| 14524 | |
| 14525 | static int JimExprOpNumUnary(Jim_Interp *interp, struct JimExprNode *node) |
| 14526 | { |
| 14527 | int intresult = 1; |
| 14528 | int rc, bA = 0; |
| @@ -13806,10 +14595,17 @@ | |
| 14595 | else if ((rc = Jim_GetBoolean(interp, A, &bA)) == JIM_OK) { |
| 14596 | switch (node->type) { |
| 14597 | case JIM_EXPROP_NOT: |
| 14598 | wC = !bA; |
| 14599 | break; |
| 14600 | case JIM_EXPROP_UNARYPLUS: |
| 14601 | case JIM_EXPROP_UNARYMINUS: |
| 14602 | rc = JIM_ERR; |
| 14603 | Jim_SetResultFormatted(interp, |
| 14604 | "can't use non-numeric string as operand of \"%s\"", |
| 14605 | node->type == JIM_EXPROP_UNARYPLUS ? "+" : "-"); |
| 14606 | break; |
| 14607 | default: |
| 14608 | abort(); |
| 14609 | } |
| 14610 | } |
| 14611 | |
| @@ -13824,18 +14620,20 @@ | |
| 14620 | |
| 14621 | Jim_DecrRefCount(interp, A); |
| 14622 | |
| 14623 | return rc; |
| 14624 | } |
| 14625 | |
| 14626 | |
| 14627 | static double JimRandDouble(Jim_Interp *interp) |
| 14628 | { |
| 14629 | unsigned long x; |
| 14630 | JimRandomBytes(interp, &x, sizeof(x)); |
| 14631 | |
| 14632 | return (double)x / (double)~0UL; |
| 14633 | } |
| 14634 | |
| 14635 | |
| 14636 | static int JimExprOpIntUnary(Jim_Interp *interp, struct JimExprNode *node) |
| 14637 | { |
| 14638 | jim_wide wA; |
| 14639 | Jim_Obj *A; |
| @@ -13862,10 +14660,11 @@ | |
| 14660 | |
| 14661 | Jim_DecrRefCount(interp, A); |
| 14662 | |
| 14663 | return rc; |
| 14664 | } |
| 14665 | |
| 14666 | |
| 14667 | static int JimExprOpNone(Jim_Interp *interp, struct JimExprNode *node) |
| 14668 | { |
| 14669 | JimPanic((node->type != JIM_EXPROP_FUNC_RAND, "JimExprOpNone only support rand()")); |
| 14670 | |
| @@ -13873,10 +14672,11 @@ | |
| 14672 | |
| 14673 | return JIM_OK; |
| 14674 | } |
| 14675 | |
| 14676 | #ifdef JIM_MATH_FUNCTIONS |
| 14677 | |
| 14678 | static int JimExprOpDoubleUnary(Jim_Interp *interp, struct JimExprNode *node) |
| 14679 | { |
| 14680 | int rc; |
| 14681 | double dA, dC; |
| 14682 | Jim_Obj *A; |
| @@ -14218,10 +15018,11 @@ | |
| 15018 | goto done; |
| 15019 | doubleresult: |
| 15020 | Jim_SetResult(interp, Jim_NewDoubleObj(interp, dC)); |
| 15021 | goto done; |
| 15022 | } |
| 15023 | |
| 15024 | |
| 15025 | static int JimSearchList(Jim_Interp *interp, Jim_Obj *listObjPtr, Jim_Obj *valObj) |
| 15026 | { |
| 15027 | int listlen; |
| 15028 | int i; |
| @@ -14233,10 +15034,29 @@ | |
| 15034 | } |
| 15035 | } |
| 15036 | return 0; |
| 15037 | } |
| 15038 | |
| 15039 | static int JimRegexpMatch(Jim_Interp *interp, Jim_Obj *patternObj, Jim_Obj *objPtr) |
| 15040 | { |
| 15041 | Jim_Obj *argv[3]; |
| 15042 | int argc = 0; |
| 15043 | long eq; |
| 15044 | int rc; |
| 15045 | |
| 15046 | argv[argc++] = Jim_NewStringObj(interp, "regexp", -1); |
| 15047 | argv[argc++] = patternObj; |
| 15048 | argv[argc++] = objPtr; |
| 15049 | |
| 15050 | rc = Jim_EvalObjVector(interp, argc, argv); |
| 15051 | |
| 15052 | if (rc != JIM_OK || Jim_GetLong(interp, Jim_GetResult(interp), &eq) != JIM_OK) { |
| 15053 | eq = -rc; |
| 15054 | } |
| 15055 | |
| 15056 | return eq; |
| 15057 | } |
| 15058 | |
| 15059 | |
| 15060 | static int JimExprOpStrBin(Jim_Interp *interp, struct JimExprNode *node) |
| 15061 | { |
| 15062 | Jim_Obj *A, *B; |
| @@ -14277,21 +15097,33 @@ | |
| 15097 | case JIM_EXPROP_STRIN: |
| 15098 | wC = JimSearchList(interp, B, A); |
| 15099 | break; |
| 15100 | case JIM_EXPROP_STRNI: |
| 15101 | wC = !JimSearchList(interp, B, A); |
| 15102 | break; |
| 15103 | case JIM_EXPROP_STRGLOB: |
| 15104 | wC = Jim_StringMatchObj(interp, B, A, 0); |
| 15105 | break; |
| 15106 | case JIM_EXPROP_STRRE: |
| 15107 | wC = JimRegexpMatch(interp, B, A); |
| 15108 | if (wC < 0) { |
| 15109 | rc = JIM_ERR; |
| 15110 | goto error; |
| 15111 | } |
| 15112 | break; |
| 15113 | default: |
| 15114 | abort(); |
| 15115 | } |
| 15116 | Jim_SetResultInt(interp, wC); |
| 15117 | |
| 15118 | error: |
| 15119 | Jim_DecrRefCount(interp, A); |
| 15120 | Jim_DecrRefCount(interp, B); |
| 15121 | |
| 15122 | return rc; |
| 15123 | } |
| 15124 | |
| 15125 | |
| 15126 | static int ExprBool(Jim_Interp *interp, Jim_Obj *obj) |
| 15127 | { |
| 15128 | long l; |
| 15129 | double d; |
| @@ -14312,10 +15144,11 @@ | |
| 15144 | } |
| 15145 | |
| 15146 | Jim_DecrRefCount(interp, obj); |
| 15147 | return ret; |
| 15148 | } |
| 15149 | |
| 15150 | |
| 15151 | static int JimExprOpAnd(Jim_Interp *interp, struct JimExprNode *node) |
| 15152 | { |
| 15153 | |
| 15154 | int result = JimExprGetTermBoolean(interp, node->left); |
| @@ -14328,10 +15161,11 @@ | |
| 15161 | return JIM_ERR; |
| 15162 | } |
| 15163 | Jim_SetResultInt(interp, result); |
| 15164 | return JIM_OK; |
| 15165 | } |
| 15166 | |
| 15167 | |
| 15168 | static int JimExprOpOr(Jim_Interp *interp, struct JimExprNode *node) |
| 15169 | { |
| 15170 | |
| 15171 | int result = JimExprGetTermBoolean(interp, node->left); |
| @@ -14344,10 +15178,11 @@ | |
| 15178 | return JIM_ERR; |
| 15179 | } |
| 15180 | Jim_SetResultInt(interp, result); |
| 15181 | return JIM_OK; |
| 15182 | } |
| 15183 | |
| 15184 | |
| 15185 | static int JimExprOpTernary(Jim_Interp *interp, struct JimExprNode *node) |
| 15186 | { |
| 15187 | |
| 15188 | int result = JimExprGetTermBoolean(interp, node->left); |
| @@ -14407,10 +15242,12 @@ | |
| 15242 | |
| 15243 | OPRINIT_ATTR("**", 120, 2, JimExprOpBin, OP_RIGHT_ASSOC), |
| 15244 | |
| 15245 | OPRINIT("eq", 60, 2, JimExprOpStrBin), |
| 15246 | OPRINIT("ne", 60, 2, JimExprOpStrBin), |
| 15247 | OPRINIT("=*", 60, 2, JimExprOpStrBin), |
| 15248 | OPRINIT("=~", 60, 2, JimExprOpStrBin), |
| 15249 | |
| 15250 | OPRINIT("in", 55, 2, JimExprOpStrBin), |
| 15251 | OPRINIT("ni", 55, 2, JimExprOpStrBin), |
| 15252 | |
| 15253 | OPRINIT("lt", 75, 2, JimExprOpStrBin), |
| @@ -14458,10 +15295,11 @@ | |
| 15295 | #undef OPRINIT |
| 15296 | #undef OPRINIT_ATTR |
| 15297 | |
| 15298 | #define JIM_EXPR_OPERATORS_NUM \ |
| 15299 | (sizeof(Jim_ExprOperators)/sizeof(struct Jim_ExprOperator)) |
| 15300 | |
| 15301 | |
| 15302 | static int JimParseExpression(struct JimParserCtx *pc) |
| 15303 | { |
| 15304 | pc->errmsg = NULL; |
| 15305 | |
| @@ -14557,10 +15395,11 @@ | |
| 15395 | return JimParseExprOperator(pc); |
| 15396 | break; |
| 15397 | } |
| 15398 | return JIM_OK; |
| 15399 | } |
| 15400 | |
| 15401 | |
| 15402 | static int JimParseExprNumber(struct JimParserCtx *pc) |
| 15403 | { |
| 15404 | char *end; |
| 15405 | |
| @@ -14581,10 +15420,11 @@ | |
| 15420 | } |
| 15421 | pc->tend = pc->p - 1; |
| 15422 | pc->len -= (pc->p - pc->tstart); |
| 15423 | return JIM_OK; |
| 15424 | } |
| 15425 | |
| 15426 | |
| 15427 | static int JimParseExprIrrational(struct JimParserCtx *pc) |
| 15428 | { |
| 15429 | const char *irrationals[] = { "NaN", "nan", "NAN", "Inf", "inf", "INF", NULL }; |
| 15430 | int i; |
| @@ -14600,10 +15440,11 @@ | |
| 15440 | return JIM_OK; |
| 15441 | } |
| 15442 | } |
| 15443 | return JIM_ERR; |
| 15444 | } |
| 15445 | |
| 15446 | |
| 15447 | static int JimParseExprBoolean(struct JimParserCtx *pc) |
| 15448 | { |
| 15449 | int i; |
| 15450 | for (i = 0; i < sizeof(jim_true_false_strings) / sizeof(*jim_true_false_strings); i++) { |
| @@ -14624,10 +15465,11 @@ | |
| 15465 | if (opcode < JIM_TT_EXPR_OP) { |
| 15466 | return &dummy_op; |
| 15467 | } |
| 15468 | return &Jim_ExprOperators[opcode - JIM_TT_EXPR_OP]; |
| 15469 | } |
| 15470 | |
| 15471 | |
| 15472 | static int JimParseExprOperator(struct JimParserCtx *pc) |
| 15473 | { |
| 15474 | int i; |
| 15475 | const struct Jim_ExprOperator *bestOp = NULL; |
| @@ -14691,10 +15533,11 @@ | |
| 15533 | struct JimExprNode *expr; |
| 15534 | struct JimExprNode *nodes; |
| 15535 | int len; |
| 15536 | int inUse; |
| 15537 | }; |
| 15538 | |
| 15539 | |
| 15540 | static void ExprTreeFreeNodes(Jim_Interp *interp, struct JimExprNode *nodes, int num) |
| 15541 | { |
| 15542 | int i; |
| 15543 | for (i = 0; i < num; i++) { |
| @@ -14702,16 +15545,18 @@ | |
| 15545 | Jim_DecrRefCount(interp, nodes[i].objPtr); |
| 15546 | } |
| 15547 | } |
| 15548 | Jim_Free(nodes); |
| 15549 | } |
| 15550 | |
| 15551 | |
| 15552 | static void ExprTreeFree(Jim_Interp *interp, struct ExprTree *expr) |
| 15553 | { |
| 15554 | ExprTreeFreeNodes(interp, expr->nodes, expr->len); |
| 15555 | Jim_Free(expr); |
| 15556 | } |
| 15557 | |
| 15558 | |
| 15559 | static void FreeExprInternalRep(Jim_Interp *interp, Jim_Obj *objPtr) |
| 15560 | { |
| 15561 | struct ExprTree *expr = (void *)objPtr->internalRep.ptr; |
| 15562 | |
| @@ -14721,10 +15566,11 @@ | |
| 15566 | } |
| 15567 | |
| 15568 | ExprTreeFree(interp, expr); |
| 15569 | } |
| 15570 | } |
| 15571 | |
| 15572 | |
| 15573 | static void DupExprInternalRep(Jim_Interp *interp, Jim_Obj *srcPtr, Jim_Obj *dupPtr) |
| 15574 | { |
| 15575 | JIM_NOTUSED(interp); |
| 15576 | JIM_NOTUSED(srcPtr); |
| @@ -14744,10 +15590,11 @@ | |
| 15590 | struct JimExprNode *nodes; |
| 15591 | struct JimExprNode *next; |
| 15592 | }; |
| 15593 | |
| 15594 | #ifdef DEBUG_SHOW_EXPR |
| 15595 | |
| 15596 | static void JimShowExprNode(struct JimExprNode *node, int level) |
| 15597 | { |
| 15598 | int i; |
| 15599 | for (i = 0; i < level; i++) { |
| 15600 | printf(" "); |
| @@ -15029,11 +15876,11 @@ | |
| 15876 | builder.fileNameObj = fileNameObj; |
| 15877 | |
| 15878 | builder.nodes = Jim_Alloc(sizeof(struct JimExprNode) * (tokenlist->count - 1)); |
| 15879 | memset(builder.nodes, 0, sizeof(struct JimExprNode) * (tokenlist->count - 1)); |
| 15880 | builder.next = builder.nodes; |
| 15881 | Jim_StackInit(&builder.stack, NULL); |
| 15882 | |
| 15883 | rc = ExprTreeBuildTree(interp, &builder, 0, 0, 1); |
| 15884 | |
| 15885 | if (rc == JIM_OK) { |
| 15886 | top = Jim_StackPop(&builder.stack); |
| @@ -15043,11 +15890,11 @@ | |
| 15890 | rc = JIM_ERR; |
| 15891 | } |
| 15892 | } |
| 15893 | |
| 15894 | |
| 15895 | Jim_StackFree(&builder.stack); |
| 15896 | |
| 15897 | if (rc != JIM_OK) { |
| 15898 | ExprTreeFreeNodes(interp, builder.nodes, builder.next - builder.nodes); |
| 15899 | return NULL; |
| 15900 | } |
| @@ -15078,10 +15925,13 @@ | |
| 15925 | fileNameObj = Jim_GetSourceInfo(interp, objPtr, &line); |
| 15926 | Jim_IncrRefCount(fileNameObj); |
| 15927 | |
| 15928 | exprText = Jim_GetString(objPtr, &exprTextLen); |
| 15929 | |
| 15930 | int oldtaint = interp->taint; |
| 15931 | interp->taint = objPtr->taint; |
| 15932 | |
| 15933 | |
| 15934 | ScriptTokenListInit(&tokenlist); |
| 15935 | |
| 15936 | JimParserInit(&parser, exprText, exprTextLen, line); |
| 15937 | while (!parser.eof) { |
| @@ -15144,10 +15994,13 @@ | |
| 15994 | |
| 15995 | Jim_DecrRefCount(interp, fileNameObj); |
| 15996 | Jim_FreeIntRep(interp, objPtr); |
| 15997 | Jim_SetIntRepPtr(objPtr, expr); |
| 15998 | objPtr->typePtr = &exprObjType; |
| 15999 | |
| 16000 | interp->taint = oldtaint; |
| 16001 | |
| 16002 | return rc; |
| 16003 | } |
| 16004 | |
| 16005 | static struct ExprTree *JimGetExpression(Jim_Interp *interp, Jim_Obj *objPtr) |
| 16006 | { |
| @@ -15228,10 +16081,11 @@ | |
| 16081 | |
| 16082 | return JIM_ERR; |
| 16083 | } |
| 16084 | } |
| 16085 | } |
| 16086 | |
| 16087 | |
| 16088 | static int JimExprGetTerm(Jim_Interp *interp, struct JimExprNode *node, Jim_Obj **objPtrPtr) |
| 16089 | { |
| 16090 | int rc = JimExprEvalTermNode(interp, node); |
| 16091 | if (rc == JIM_OK) { |
| @@ -15238,10 +16092,11 @@ | |
| 16092 | *objPtrPtr = Jim_GetResult(interp); |
| 16093 | Jim_IncrRefCount(*objPtrPtr); |
| 16094 | } |
| 16095 | return rc; |
| 16096 | } |
| 16097 | |
| 16098 | |
| 16099 | static int JimExprGetTermBoolean(Jim_Interp *interp, struct JimExprNode *node) |
| 16100 | { |
| 16101 | if (JimExprEvalTermNode(interp, node) == JIM_OK) { |
| 16102 | return ExprBool(interp, Jim_GetResult(interp)); |
| @@ -15418,10 +16273,11 @@ | |
| 16273 | JIM_NOTUSED(interp); |
| 16274 | memcpy(newVec, srcPtr->internalRep.ptr, size); |
| 16275 | dupPtr->internalRep.ptr = newVec; |
| 16276 | dupPtr->typePtr = &scanFmtStringObjType; |
| 16277 | } |
| 16278 | |
| 16279 | |
| 16280 | static void UpdateStringOfScanFmt(Jim_Obj *objPtr) |
| 16281 | { |
| 16282 | JimSetStringBytes(objPtr, ((ScanFmtStringObj *) objPtr->internalRep.ptr)->stringRep); |
| 16283 | } |
| @@ -15920,14 +16776,10 @@ | |
| 16776 | static int Jim_IncrCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 16777 | { |
| 16778 | jim_wide wideValue, increment = 1; |
| 16779 | Jim_Obj *intObjPtr; |
| 16780 | |
| 16781 | if (argc == 3) { |
| 16782 | if (Jim_GetWideExpr(interp, argv[2], &increment) != JIM_OK) |
| 16783 | return JIM_ERR; |
| 16784 | } |
| 16785 | intObjPtr = Jim_GetVariable(interp, argv[1], JIM_UNSHARED); |
| @@ -15939,11 +16791,10 @@ | |
| 16791 | return JIM_ERR; |
| 16792 | } |
| 16793 | if (!intObjPtr || Jim_IsShared(intObjPtr)) { |
| 16794 | intObjPtr = Jim_NewIntObj(interp, wideValue + increment); |
| 16795 | if (Jim_SetVariable(interp, argv[1], intObjPtr) != JIM_OK) { |
| 16796 | return JIM_ERR; |
| 16797 | } |
| 16798 | } |
| 16799 | else { |
| 16800 | |
| @@ -16026,10 +16877,11 @@ | |
| 16877 | retcode = Jim_EvalObjPrefix(interp, interp->unknown, argc, argv); |
| 16878 | interp->unknown_called--; |
| 16879 | |
| 16880 | return retcode; |
| 16881 | } |
| 16882 | |
| 16883 | |
| 16884 | static void JimPushEvalFrame(Jim_Interp *interp, Jim_EvalFrame *frame, Jim_Obj *scriptObj) |
| 16885 | { |
| 16886 | memset(frame, 0, sizeof(*frame)); |
| 16887 | frame->parent = interp->evalFrame; |
| @@ -16047,10 +16899,11 @@ | |
| 16899 | if (frame->scriptObj) { |
| 16900 | printf("script: %.*s\n", 20, Jim_String(frame->scriptObj)); |
| 16901 | } |
| 16902 | #endif |
| 16903 | } |
| 16904 | |
| 16905 | |
| 16906 | static void JimPopEvalFrame(Jim_Interp *interp) |
| 16907 | { |
| 16908 | interp->evalFrame = interp->evalFrame->parent; |
| 16909 | } |
| @@ -16094,16 +16947,21 @@ | |
| 16947 | |
| 16948 | if (!interp->traceCmdObj || |
| 16949 | (retcode = JimTraceCallback(interp, "cmd", objc, objv)) == JIM_OK) { |
| 16950 | |
| 16951 | Jim_SetEmptyResult(interp); |
| 16952 | interp->taint = Jim_CalcTaint(objc, objv); |
| 16953 | if (cmdPtr->flags & JIM_CMD_ISPROC) { |
| 16954 | retcode = JimCallProcedure(interp, cmdPtr, objc, objv); |
| 16955 | } |
| 16956 | else if ((cmdPtr->flags & JIM_CMD_NOTAINT) && Jim_CheckTaint(interp, JIM_TAINT_ANY)) { |
| 16957 | Jim_SetTaintError(interp, 1, objv); |
| 16958 | retcode = JIM_ERR; |
| 16959 | } |
| 16960 | else { |
| 16961 | retcode = JimCallNative(interp, cmdPtr, objc, objv); |
| 16962 | |
| 16963 | } |
| 16964 | if (retcode == JIM_ERR) { |
| 16965 | JimSetErrorStack(interp, NULL); |
| 16966 | } |
| 16967 | } |
| @@ -16184,10 +17042,11 @@ | |
| 17042 | memcpy(&nargv[1], &objv[0], sizeof(nargv[0]) * objc); |
| 17043 | ret = Jim_EvalObjVector(interp, objc + 1, nargv); |
| 17044 | Jim_Free(nargv); |
| 17045 | return ret; |
| 17046 | } |
| 17047 | |
| 17048 | |
| 17049 | static int JimSubstOneToken(Jim_Interp *interp, const ScriptToken *token, Jim_Obj **objPtrPtr) |
| 17050 | { |
| 17051 | Jim_Obj *objPtr; |
| 17052 | int ret = JIM_ERR; |
| @@ -16239,10 +17098,12 @@ | |
| 17098 | int totlen = 0, i; |
| 17099 | Jim_Obj **intv; |
| 17100 | Jim_Obj *sintv[JIM_EVAL_SINTV_LEN]; |
| 17101 | Jim_Obj *objPtr; |
| 17102 | char *s; |
| 17103 | int taint = 0; |
| 17104 | const char *error_action = NULL; |
| 17105 | |
| 17106 | if (tokens <= JIM_EVAL_SINTV_LEN) |
| 17107 | intv = sintv; |
| 17108 | else |
| 17109 | intv = Jim_Alloc(sizeof(Jim_Obj *) * tokens); |
| @@ -16256,28 +17117,34 @@ | |
| 17117 | if (flags & JIM_SUBST_FLAG) { |
| 17118 | |
| 17119 | tokens = i; |
| 17120 | continue; |
| 17121 | } |
| 17122 | error_action = "break"; |
| 17123 | |
| 17124 | case JIM_CONTINUE: |
| 17125 | if (flags & JIM_SUBST_FLAG) { |
| 17126 | intv[i] = NULL; |
| 17127 | continue; |
| 17128 | } |
| 17129 | if (!error_action) { |
| 17130 | error_action = "continue"; |
| 17131 | } |
| 17132 | |
| 17133 | default: |
| 17134 | while (i--) { |
| 17135 | Jim_DecrRefCount(interp, intv[i]); |
| 17136 | } |
| 17137 | if (intv != sintv) { |
| 17138 | Jim_Free(intv); |
| 17139 | } |
| 17140 | if (error_action) { |
| 17141 | Jim_SetResultFormatted(interp, "invoked \"%s\" outside of a loop", error_action); |
| 17142 | } |
| 17143 | return NULL; |
| 17144 | } |
| 17145 | taint |= intv[i]->taint; |
| 17146 | Jim_IncrRefCount(intv[i]); |
| 17147 | Jim_String(intv[i]); |
| 17148 | totlen += intv[i]->length; |
| 17149 | } |
| 17150 | |
| @@ -16287,10 +17154,11 @@ | |
| 17154 | intv[0]->refCount--; |
| 17155 | return intv[0]; |
| 17156 | } |
| 17157 | |
| 17158 | objPtr = Jim_NewStringObjNoAlloc(interp, NULL, 0); |
| 17159 | objPtr->taint = taint; |
| 17160 | |
| 17161 | if (tokens == 4 && token[0].type == JIM_TT_ESC && token[1].type == JIM_TT_ESC |
| 17162 | && token[2].type == JIM_TT_VAR) { |
| 17163 | |
| 17164 | objPtr->typePtr = &interpolatedObjType; |
| @@ -16322,10 +17190,111 @@ | |
| 17190 | } |
| 17191 | |
| 17192 | return objPtr; |
| 17193 | } |
| 17194 | |
| 17195 | #define JIM_LSUBST_LINE 0x0001 |
| 17196 | |
| 17197 | static int JimListSubstObj(Jim_Interp *interp, struct Jim_Obj *objPtr, unsigned flags) |
| 17198 | { |
| 17199 | int i; |
| 17200 | ScriptObj *script; |
| 17201 | ScriptToken *token; |
| 17202 | Jim_Obj *resultListObj; |
| 17203 | int retcode = JIM_OK; |
| 17204 | |
| 17205 | Jim_IncrRefCount(objPtr); |
| 17206 | script = JimGetScript(interp, objPtr); |
| 17207 | if (JimParseCheckMissing(interp, script->missing) == JIM_ERR) { |
| 17208 | JimSetErrorStack(interp, script); |
| 17209 | Jim_DecrRefCount(interp, objPtr); |
| 17210 | return JIM_ERR; |
| 17211 | } |
| 17212 | |
| 17213 | token = script->token; |
| 17214 | |
| 17215 | script->inUse++; |
| 17216 | |
| 17217 | |
| 17218 | resultListObj = Jim_NewListObj(interp, NULL, 0); |
| 17219 | |
| 17220 | |
| 17221 | for (i = 0; i < script->len && retcode == JIM_OK; ) { |
| 17222 | int argc; |
| 17223 | int j; |
| 17224 | Jim_Obj *lineListObj = resultListObj; |
| 17225 | |
| 17226 | |
| 17227 | argc = token[i].objPtr->internalRep.scriptLineValue.argc; |
| 17228 | script->linenr = token[i].objPtr->internalRep.scriptLineValue.line; |
| 17229 | |
| 17230 | |
| 17231 | i++; |
| 17232 | |
| 17233 | if (flags & JIM_LSUBST_LINE) { |
| 17234 | lineListObj = Jim_NewListObj(interp, NULL, 0); |
| 17235 | } |
| 17236 | |
| 17237 | |
| 17238 | for (j = 0; j < argc; j++) { |
| 17239 | long wordtokens = 1; |
| 17240 | int expand = 0; |
| 17241 | Jim_Obj *wordObjPtr = NULL; |
| 17242 | |
| 17243 | if (token[i].type == JIM_TT_WORD) { |
| 17244 | wordtokens = JimWideValue(token[i++].objPtr); |
| 17245 | if (wordtokens < 0) { |
| 17246 | expand = 1; |
| 17247 | wordtokens = -wordtokens; |
| 17248 | } |
| 17249 | } |
| 17250 | |
| 17251 | |
| 17252 | wordObjPtr = JimInterpolateTokens(interp, token + i, wordtokens, JIM_NONE); |
| 17253 | |
| 17254 | if (!wordObjPtr) { |
| 17255 | if (retcode == JIM_OK) { |
| 17256 | retcode = JIM_ERR; |
| 17257 | } |
| 17258 | break; |
| 17259 | } |
| 17260 | |
| 17261 | Jim_IncrRefCount(wordObjPtr); |
| 17262 | i += wordtokens; |
| 17263 | |
| 17264 | if (!expand) { |
| 17265 | Jim_ListAppendElement(interp, lineListObj, wordObjPtr); |
| 17266 | } |
| 17267 | else { |
| 17268 | int k; |
| 17269 | |
| 17270 | for (k = 0; k < Jim_ListLength(interp, wordObjPtr); k++) { |
| 17271 | Jim_ListAppendElement(interp, lineListObj, Jim_ListGetIndex(interp, wordObjPtr, k)); |
| 17272 | } |
| 17273 | } |
| 17274 | Jim_DecrRefCount(interp, wordObjPtr); |
| 17275 | } |
| 17276 | |
| 17277 | if (flags & JIM_LSUBST_LINE) { |
| 17278 | Jim_ListAppendElement(interp, resultListObj, lineListObj); |
| 17279 | } |
| 17280 | } |
| 17281 | |
| 17282 | Jim_FreeIntRep(interp, objPtr); |
| 17283 | objPtr->typePtr = &scriptObjType; |
| 17284 | Jim_SetIntRepPtr(objPtr, script); |
| 17285 | Jim_DecrRefCount(interp, objPtr); |
| 17286 | |
| 17287 | if (retcode == JIM_OK) { |
| 17288 | Jim_SetResult(interp, resultListObj); |
| 17289 | } |
| 17290 | else { |
| 17291 | Jim_FreeNewObj(interp, resultListObj); |
| 17292 | } |
| 17293 | |
| 17294 | return retcode; |
| 17295 | } |
| 17296 | |
| 17297 | static int JimEvalObjList(Jim_Interp *interp, Jim_Obj *listPtr) |
| 17298 | { |
| 17299 | int retcode = JIM_OK; |
| 17300 | Jim_EvalFrame frame; |
| @@ -16383,11 +17352,11 @@ | |
| 17352 | Jim_DecrRefCount(interp, scriptObjPtr); |
| 17353 | return JIM_OK; |
| 17354 | } |
| 17355 | if (script->len == 3 |
| 17356 | && token[1].objPtr->typePtr == &commandObjType |
| 17357 | && (token[1].objPtr->internalRep.cmdValue.cmdPtr->flags & JIM_CMD_ISPROC) == 0 |
| 17358 | && token[1].objPtr->internalRep.cmdValue.cmdPtr->u.native.cmdProc == Jim_IncrCoreCommand |
| 17359 | && token[2].objPtr->typePtr == &variableObjType) { |
| 17360 | |
| 17361 | Jim_Obj *objPtr = Jim_GetVariable(interp, token[2].objPtr, JIM_NONE); |
| 17362 | |
| @@ -16404,11 +17373,11 @@ | |
| 17373 | script->inUse++; |
| 17374 | |
| 17375 | JimPushEvalFrame(interp, &frame, scriptObjPtr); |
| 17376 | |
| 17377 | |
| 17378 | interp->hasErrorStackTrace = 0; |
| 17379 | argv = sargv; |
| 17380 | |
| 17381 | for (i = 0; i < script->len && retcode == JIM_OK; ) { |
| 17382 | int argc; |
| 17383 | int j; |
| @@ -16520,10 +17489,11 @@ | |
| 17489 | } |
| 17490 | |
| 17491 | if (retcode == JIM_OK && argc) { |
| 17492 | |
| 17493 | retcode = JimInvokeCommand(interp, argc, argv); |
| 17494 | interp->taint = 0; |
| 17495 | |
| 17496 | if (Jim_CheckSignal(interp)) { |
| 17497 | retcode = JIM_SIGNAL; |
| 17498 | } |
| 17499 | } |
| @@ -16551,10 +17521,11 @@ | |
| 17521 | Jim_SetIntRepPtr(scriptObjPtr, script); |
| 17522 | Jim_DecrRefCount(interp, scriptObjPtr); |
| 17523 | |
| 17524 | return retcode; |
| 17525 | } |
| 17526 | |
| 17527 | |
| 17528 | static int JimSetProcArg(Jim_Interp *interp, Jim_Obj *argNameObj, Jim_Obj *argValObj) |
| 17529 | { |
| 17530 | int retcode; |
| 17531 | |
| @@ -16580,48 +17551,63 @@ | |
| 17551 | else { |
| 17552 | retcode = Jim_SetVariable(interp, argNameObj, argValObj); |
| 17553 | } |
| 17554 | return retcode; |
| 17555 | } |
| 17556 | |
| 17557 | static Jim_Obj *JimCmdUsage(Jim_Interp *interp, Jim_Obj *cmdNameObj, Jim_Cmd *cmd) |
| 17558 | { |
| 17559 | Jim_Obj *usage = Jim_DuplicateObj(interp, cmdNameObj); |
| 17560 | |
| 17561 | if (cmd->flags & JIM_CMD_ISPROC) { |
| 17562 | int i; |
| 17563 | for (i = 0; i < cmd->u.proc.argListLen; i++) { |
| 17564 | Jim_AppendString(interp, usage, " ", 1); |
| 17565 | |
| 17566 | if (i == cmd->u.proc.argsPos) { |
| 17567 | if (cmd->u.proc.arglist[i].defaultObjPtr) { |
| 17568 | |
| 17569 | Jim_AppendString(interp, usage, "?", 1); |
| 17570 | Jim_AppendObj(interp, usage, cmd->u.proc.arglist[i].defaultObjPtr); |
| 17571 | Jim_AppendString(interp, usage, " ...?", -1); |
| 17572 | } |
| 17573 | else { |
| 17574 | |
| 17575 | Jim_AppendString(interp, usage, "?arg ...?", -1); |
| 17576 | } |
| 17577 | } |
| 17578 | else { |
| 17579 | if (cmd->u.proc.arglist[i].defaultObjPtr) { |
| 17580 | Jim_AppendString(interp, usage, "?", 1); |
| 17581 | Jim_AppendObj(interp, usage, cmd->u.proc.arglist[i].nameObjPtr); |
| 17582 | Jim_AppendString(interp, usage, "?", 1); |
| 17583 | } |
| 17584 | else { |
| 17585 | const char *arg = Jim_String(cmd->u.proc.arglist[i].nameObjPtr); |
| 17586 | if (*arg == '&') { |
| 17587 | arg++; |
| 17588 | } |
| 17589 | Jim_AppendString(interp, usage, arg, -1); |
| 17590 | } |
| 17591 | } |
| 17592 | } |
| 17593 | } |
| 17594 | else if (cmd->u.native.usage) { |
| 17595 | if (*cmd->u.native.usage) { |
| 17596 | Jim_AppendStrings(interp, usage, " ", cmd->u.native.usage, NULL); |
| 17597 | } |
| 17598 | } |
| 17599 | else { |
| 17600 | Jim_AppendString(interp, usage, " ...", -1); |
| 17601 | } |
| 17602 | |
| 17603 | return usage; |
| 17604 | } |
| 17605 | |
| 17606 | static void JimSetProcWrongArgs(Jim_Interp *interp, Jim_Obj *procNameObj, Jim_Cmd *cmd) |
| 17607 | { |
| 17608 | Jim_SetResultFormatted(interp, "wrong # args: should be \"%#s\"", JimCmdUsage(interp, procNameObj, cmd)); |
| 17609 | } |
| 17610 | |
| 17611 | #ifdef jim_ext_namespace |
| 17612 | int Jim_EvalNamespace(Jim_Interp *interp, Jim_Obj *scriptObj, Jim_Obj *nsObj) |
| 17613 | { |
| @@ -16758,10 +17744,40 @@ | |
| 17744 | } |
| 17745 | interp->procLevel--; |
| 17746 | |
| 17747 | return retcode; |
| 17748 | } |
| 17749 | |
| 17750 | static int JimCallNative(Jim_Interp *interp, Jim_Cmd *cmd, int argc, Jim_Obj *const *argv) |
| 17751 | { |
| 17752 | int argsok = 1; |
| 17753 | int ret; |
| 17754 | |
| 17755 | |
| 17756 | if (argc - 1 < cmd->u.native.minargs) { |
| 17757 | argsok = 0; |
| 17758 | } |
| 17759 | else if (cmd->u.native.maxargs >= 0 && argc - 1 > cmd->u.native.maxargs) { |
| 17760 | argsok = 0; |
| 17761 | } |
| 17762 | else if (cmd->u.native.maxargs < -1 && (argc - 1) % -cmd->u.native.maxargs != 0) { |
| 17763 | |
| 17764 | argsok = 0; |
| 17765 | } |
| 17766 | if (argsok) { |
| 17767 | interp->cmdPrivData = cmd->u.native.privData; |
| 17768 | ret = cmd->u.native.cmdProc(interp, argc, argv); |
| 17769 | if (ret != JIM_USAGE) { |
| 17770 | return ret; |
| 17771 | } |
| 17772 | |
| 17773 | } |
| 17774 | |
| 17775 | //printf("Wrong args for %s, argc=%d, minargs=%d, maxargs=%d\n", Jim_String(argv[0]), argc, cmd->u.native.minargs, cmd->u.native.maxargs); |
| 17776 | Jim_SetResultFormatted(interp, "wrong # args: should be \"%#s\"", JimCmdUsage(interp, argv[0], cmd)); |
| 17777 | return JIM_ERR; |
| 17778 | } |
| 17779 | |
| 17780 | int Jim_EvalSource(Jim_Interp *interp, const char *filename, int lineno, const char *script) |
| 17781 | { |
| 17782 | int retval; |
| 17783 | Jim_Obj *scriptObjPtr; |
| @@ -16805,56 +17821,43 @@ | |
| 17821 | |
| 17822 | return retval; |
| 17823 | } |
| 17824 | |
| 17825 | #include <sys/stat.h> |
| 17826 | |
| 17827 | int Jim_EvalFile(Jim_Interp *interp, const char *filename) |
| 17828 | { |
| 17829 | FILE *fp; |
| 17830 | char *buf; |
| 17831 | Jim_Obj *scriptObjPtr; |
| 17832 | Jim_Obj *filenameObj, *oldFilenameObj; |
| 17833 | int retcode = JIM_ERR; |
| 17834 | int readlen; |
| 17835 | #define READ_BUF_SIZE 256 |
| 17836 | |
| 17837 | if ((fp = fopen(filename, "rt")) == NULL) { |
| 17838 | Jim_SetResultFormatted(interp, "couldn't read file \"%s\": %s", filename, strerror(errno)); |
| 17839 | return JIM_ERR; |
| 17840 | } |
| 17841 | scriptObjPtr = Jim_NewStringObj(interp, NULL, 0); |
| 17842 | |
| 17843 | buf = Jim_Alloc(READ_BUF_SIZE); |
| 17844 | while ((readlen = fread(buf, 1, READ_BUF_SIZE, fp)) > 0) { |
| 17845 | Jim_AppendString(interp, scriptObjPtr, buf, readlen); |
| 17846 | } |
| 17847 | Jim_Free(buf); |
| 17848 | if (ferror(fp)) { |
| 17849 | fclose(fp); |
| 17850 | Jim_SetResultFormatted(interp, "failed to load file \"%s\": %s", filename, strerror(errno)); |
| 17851 | Jim_FreeNewObj(interp, scriptObjPtr); |
| 17852 | return retcode; |
| 17853 | } |
| 17854 | fclose(fp); |
| 17855 | |
| 17856 | |
| 17857 | filenameObj = Jim_NewStringObj(interp, filename, -1); |
| 17858 | Jim_SetSourceInfo(interp, scriptObjPtr, filenameObj, 1); |
| 17859 | oldFilenameObj = JimPushInterpObj(interp->currentFilenameObj, filenameObj); |
| 17860 | |
| 17861 | retcode = Jim_EvalObj(interp, scriptObjPtr); |
| 17862 | |
| 17863 | JimPopInterpObj(interp, interp->currentFilenameObj, oldFilenameObj); |
| @@ -16920,10 +17923,11 @@ | |
| 17923 | int scriptTextLen; |
| 17924 | const char *scriptText = Jim_GetString(objPtr, &scriptTextLen); |
| 17925 | struct JimParserCtx parser; |
| 17926 | struct ScriptObj *script = Jim_Alloc(sizeof(*script)); |
| 17927 | ParseTokenList tokenlist; |
| 17928 | int oldtaint; |
| 17929 | |
| 17930 | |
| 17931 | ScriptTokenListInit(&tokenlist); |
| 17932 | |
| 17933 | JimParserInit(&parser, scriptText, scriptTextLen, 1); |
| @@ -16935,10 +17939,13 @@ | |
| 17939 | } |
| 17940 | ScriptAddToken(&tokenlist, parser.tstart, parser.tend - parser.tstart + 1, parser.tt, |
| 17941 | parser.tline); |
| 17942 | } |
| 17943 | |
| 17944 | oldtaint = interp->taint; |
| 17945 | interp->taint = objPtr->taint; |
| 17946 | |
| 17947 | |
| 17948 | script->inUse = 1; |
| 17949 | script->substFlags = flags; |
| 17950 | script->fileNameObj = interp->emptyObj; |
| 17951 | Jim_IncrRefCount(script->fileNameObj); |
| @@ -16961,10 +17968,11 @@ | |
| 17968 | |
| 17969 | |
| 17970 | Jim_FreeIntRep(interp, objPtr); |
| 17971 | Jim_SetIntRepPtr(objPtr, script); |
| 17972 | objPtr->typePtr = &scriptObjType; |
| 17973 | interp->taint = oldtaint; |
| 17974 | return JIM_OK; |
| 17975 | } |
| 17976 | |
| 17977 | static ScriptObj *Jim_GetSubst(Jim_Interp *interp, Jim_Obj *objPtr, int flags) |
| 17978 | { |
| @@ -16992,27 +18000,59 @@ | |
| 18000 | return JIM_ERR; |
| 18001 | } |
| 18002 | return JIM_OK; |
| 18003 | } |
| 18004 | |
| 18005 | static Jim_Obj *JimJoinCmdArgs(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18006 | { |
| 18007 | Jim_Obj *objPtr; |
| 18008 | Jim_Obj *listObjPtr; |
| 18009 | |
| 18010 | listObjPtr = Jim_NewListObj(interp, argv, argc); |
| 18011 | |
| 18012 | Jim_IncrRefCount(listObjPtr); |
| 18013 | objPtr = Jim_ListJoin(interp, listObjPtr, " ", 1); |
| 18014 | Jim_DecrRefCount(interp, listObjPtr); |
| 18015 | Jim_IncrRefCount(objPtr); |
| 18016 | |
| 18017 | return objPtr; |
| 18018 | } |
| 18019 | |
| 18020 | void Jim_WrongNumArgs(Jim_Interp *interp, int argc, Jim_Obj *const *argv, const char *msg) |
| 18021 | { |
| 18022 | Jim_Obj *objPtr = JimJoinCmdArgs(interp, argc, argv); |
| 18023 | if (*msg) { |
| 18024 | Jim_SetResultFormatted(interp, "wrong # args: should be \"%#s %s\"", objPtr, msg); |
| 18025 | } |
| 18026 | else { |
| 18027 | Jim_SetResultFormatted(interp, "wrong # args: should be \"%#s\"", objPtr); |
| 18028 | } |
| 18029 | Jim_DecrRefCount(interp, objPtr); |
| 18030 | } |
| 18031 | |
| 18032 | int Jim_CalcTaint(int argc, Jim_Obj *const *argv) |
| 18033 | { |
| 18034 | int taint = 0; |
| 18035 | #ifdef JIM_TAINT |
| 18036 | int i; |
| 18037 | for (i = 0; i < argc; i++) { |
| 18038 | if (argv[i]) { |
| 18039 | taint |= argv[i]->taint; |
| 18040 | } |
| 18041 | } |
| 18042 | #endif |
| 18043 | return taint; |
| 18044 | } |
| 18045 | |
| 18046 | void Jim_SetTaintError(Jim_Interp *interp, int cmdargs, Jim_Obj *const *argv) |
| 18047 | { |
| 18048 | #ifdef JIM_TAINT |
| 18049 | Jim_Obj *objPtr = JimJoinCmdArgs(interp, cmdargs, argv); |
| 18050 | Jim_SetResultFormatted(interp, "%#s: tainted data", objPtr); |
| 18051 | Jim_DecrRefCount(interp, objPtr); |
| 18052 | Jim_SetGlobalVariableStr(interp, "errorCode", Jim_NewStringObj(interp, "TAINTED", -1)); |
| 18053 | #endif |
| 18054 | } |
| 18055 | |
| 18056 | typedef void JimHashtableIteratorCallbackType(Jim_Interp *interp, Jim_Obj *listObjPtr, |
| 18057 | Jim_Obj *keyObjPtr, void *value, Jim_Obj *patternObjPtr, int type); |
| 18058 | |
| @@ -17042,42 +18082,55 @@ | |
| 18082 | } |
| 18083 | return listObjPtr; |
| 18084 | } |
| 18085 | |
| 18086 | |
| 18087 | #define JIM_CMDLIST_COMMANDS 1 |
| 18088 | #define JIM_CMDLIST_PROCS 2 |
| 18089 | #define JIM_CMDLIST_ALIASES 4 |
| 18090 | #define JIM_CMDLIST_CHANNELS 8 |
| 18091 | |
| 18092 | #define JIM_CMDLIST_ALL 0x1000 |
| 18093 | |
| 18094 | static void JimCommandMatch(Jim_Interp *interp, Jim_Obj *listObjPtr, |
| 18095 | Jim_Obj *keyObj, void *value, Jim_Obj *patternObj, int type) |
| 18096 | { |
| 18097 | Jim_Cmd *cmdPtr = (Jim_Cmd *)value; |
| 18098 | int match = 1; |
| 18099 | |
| 18100 | if ((type & JIM_CMDLIST_PROCS) && !(cmdPtr->flags & JIM_CMD_ISPROC)) { |
| 18101 | |
| 18102 | return; |
| 18103 | } |
| 18104 | if ((type & JIM_CMDLIST_CHANNELS) && !(cmdPtr->flags & JIM_CMD_ISCHANNEL)) { |
| 18105 | |
| 18106 | return; |
| 18107 | } |
| 18108 | if ((type & JIM_CMDLIST_ALIASES) && !(cmdPtr->flags & JIM_CMD_ISALIAS)) { |
| 18109 | |
| 18110 | return; |
| 18111 | } |
| 18112 | if (!(type & JIM_CMDLIST_ALL) && strchr(Jim_String(keyObj), ' ')) { |
| 18113 | |
| 18114 | return; |
| 18115 | } |
| 18116 | |
| 18117 | Jim_IncrRefCount(keyObj); |
| 18118 | |
| 18119 | if (patternObj) { |
| 18120 | int plen, slen; |
| 18121 | const char *pattern = Jim_GetStringNoQualifier(patternObj, &plen); |
| 18122 | const char *str = Jim_GetStringNoQualifier(keyObj, &slen); |
| 18123 | #ifdef JIM_NO_INTROSPECTION |
| 18124 | |
| 18125 | match = (JimStringCompareUtf8(pattern, plen, str, slen, 0) == 0); |
| 18126 | #else |
| 18127 | match = JimGlobMatch(pattern, plen, str, slen, 0); |
| 18128 | #endif |
| 18129 | } |
| 18130 | if (match) { |
| 18131 | Jim_ListAppendElement(interp, listObjPtr, keyObj); |
| 18132 | } |
| 18133 | Jim_DecrRefCount(interp, keyObj); |
| 18134 | } |
| 18135 | |
| 18136 | static Jim_Obj *JimCommandsList(Jim_Interp *interp, Jim_Obj *patternObjPtr, int type) |
| @@ -17084,23 +18137,22 @@ | |
| 18137 | { |
| 18138 | return JimHashtablePatternMatch(interp, &interp->commands, patternObjPtr, JimCommandMatch, type); |
| 18139 | } |
| 18140 | |
| 18141 | |
| 18142 | #define JIM_VARLIST_GLOBALS 1 |
| 18143 | #define JIM_VARLIST_LOCALS 2 |
| 18144 | #define JIM_VARLIST_VARS 4 |
| 18145 | |
| 18146 | #define JIM_VARLIST_VALUES 0x1000 |
| 18147 | |
| 18148 | static void JimVariablesMatch(Jim_Interp *interp, Jim_Obj *listObjPtr, |
| 18149 | Jim_Obj *keyObj, void *value, Jim_Obj *patternObj, int type) |
| 18150 | { |
| 18151 | Jim_VarVal *vv = (Jim_VarVal *)value; |
| 18152 | |
| 18153 | if (!(type & JIM_VARLIST_LOCALS) || vv->linkFramePtr == NULL) { |
| 18154 | if (patternObj == NULL || Jim_StringMatchObj(interp, patternObj, keyObj, 0)) { |
| 18155 | Jim_ListAppendElement(interp, listObjPtr, keyObj); |
| 18156 | if (type & JIM_VARLIST_VALUES) { |
| 18157 | Jim_ListAppendElement(interp, listObjPtr, vv->objPtr); |
| 18158 | } |
| @@ -17109,19 +18161,20 @@ | |
| 18161 | } |
| 18162 | |
| 18163 | |
| 18164 | static Jim_Obj *JimVariablesList(Jim_Interp *interp, Jim_Obj *patternObjPtr, int mode) |
| 18165 | { |
| 18166 | if ((mode & JIM_VARLIST_LOCALS) && interp->framePtr == interp->topFramePtr) { |
| 18167 | return interp->emptyObj; |
| 18168 | } |
| 18169 | else { |
| 18170 | Jim_CallFrame *framePtr = (mode & JIM_VARLIST_GLOBALS) ? interp->topFramePtr : interp->framePtr; |
| 18171 | return JimHashtablePatternMatch(interp, &framePtr->vars, patternObjPtr, JimVariablesMatch, |
| 18172 | mode); |
| 18173 | } |
| 18174 | } |
| 18175 | |
| 18176 | |
| 18177 | static int JimInfoLevel(Jim_Interp *interp, Jim_Obj *levelObjPtr, Jim_Obj **objPtrPtr) |
| 18178 | { |
| 18179 | long level; |
| 18180 | |
| @@ -17138,10 +18191,11 @@ | |
| 18191 | } |
| 18192 | } |
| 18193 | Jim_SetResultFormatted(interp, "bad level \"%#s\"", levelObjPtr); |
| 18194 | return JIM_ERR; |
| 18195 | } |
| 18196 | |
| 18197 | |
| 18198 | static int JimInfoFrame(Jim_Interp *interp, Jim_Obj *levelObjPtr, Jim_Obj **objPtrPtr) |
| 18199 | { |
| 18200 | long level; |
| 18201 | |
| @@ -17186,14 +18240,10 @@ | |
| 18240 | } |
| 18241 | |
| 18242 | |
| 18243 | static int Jim_PutsCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18244 | { |
| 18245 | if (argc == 3) { |
| 18246 | if (!Jim_CompareStringImmediate(interp, argv[1], "-nonewline")) { |
| 18247 | Jim_SetResultString(interp, "The second argument must " "be -nonewline", -1); |
| 18248 | return JIM_ERR; |
| 18249 | } |
| @@ -17245,15 +18295,11 @@ | |
| 18295 | { |
| 18296 | jim_wide wideValue, res = 0; |
| 18297 | double doubleValue, doubleRes = 0; |
| 18298 | int i = 2; |
| 18299 | |
| 18300 | if (argc == 2) { |
| 18301 | if (Jim_GetWide(interp, argv[1], &wideValue) != JIM_OK) { |
| 18302 | if (Jim_GetDouble(interp, argv[1], &doubleValue) != JIM_OK) { |
| 18303 | return JIM_ERR; |
| 18304 | } |
| 18305 | else { |
| @@ -17342,14 +18388,10 @@ | |
| 18388 | } |
| 18389 | |
| 18390 | |
| 18391 | static int Jim_SetCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18392 | { |
| 18393 | if (argc == 2) { |
| 18394 | Jim_Obj *objPtr; |
| 18395 | |
| 18396 | objPtr = Jim_GetVariable(interp, argv[1], JIM_ERRMSG); |
| 18397 | if (!objPtr) |
| @@ -17405,15 +18447,10 @@ | |
| 18447 | } |
| 18448 | |
| 18449 | |
| 18450 | static int Jim_WhileCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18451 | { |
| 18452 | |
| 18453 | while (1) { |
| 18454 | int boolean = 0, retval; |
| 18455 | |
| 18456 | if ((retval = Jim_GetBoolFromExpr(interp, argv[1], &boolean)) != JIM_OK) |
| @@ -17447,15 +18484,10 @@ | |
| 18484 | int boolean = 1; |
| 18485 | int immediate = 0; |
| 18486 | Jim_Obj *varNamePtr = NULL; |
| 18487 | Jim_Obj *stopVarNamePtr = NULL; |
| 18488 | |
| 18489 | |
| 18490 | if ((retval = Jim_EvalObj(interp, argv[1])) != JIM_OK) { |
| 18491 | return retval; |
| 18492 | } |
| 18493 | |
| @@ -17623,19 +18655,14 @@ | |
| 18655 | |
| 18656 | static int Jim_LoopCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18657 | { |
| 18658 | int retval; |
| 18659 | jim_wide i; |
| 18660 | jim_wide limit = 0; |
| 18661 | jim_wide incr = 1; |
| 18662 | Jim_Obj *bodyObjPtr; |
| 18663 | |
| 18664 | retval = Jim_GetWideExpr(interp, argv[2], &i); |
| 18665 | if (argc > 4 && retval == JIM_OK) { |
| 18666 | retval = Jim_GetWideExpr(interp, argv[3], &limit); |
| 18667 | } |
| 18668 | if (argc > 5 && retval == JIM_OK) { |
| @@ -17682,13 +18709,10 @@ | |
| 18709 | } |
| 18710 | } |
| 18711 | else { |
| 18712 | objPtr = Jim_NewIntObj(interp, i); |
| 18713 | retval = Jim_SetVariable(interp, argv[1], objPtr); |
| 18714 | } |
| 18715 | } |
| 18716 | } |
| 18717 | |
| 18718 | if (retval == JIM_OK || retval == JIM_CONTINUE || retval == JIM_BREAK) { |
| @@ -17730,13 +18754,12 @@ | |
| 18754 | Jim_ListIter twoiters[2]; |
| 18755 | Jim_ListIter *iters; |
| 18756 | Jim_Obj *script; |
| 18757 | Jim_Obj *resultObj; |
| 18758 | |
| 18759 | if (argc % 2 != 0) { |
| 18760 | return JIM_USAGE; |
| 18761 | } |
| 18762 | script = argv[argc - 1]; |
| 18763 | numargs = (argc - 1 - 1); |
| 18764 | |
| 18765 | if (numargs == 2) { |
| @@ -17786,14 +18809,14 @@ | |
| 18809 | Jim_Obj *valObj = JimListIterNext(interp, &iters[i + 1]); |
| 18810 | if (!valObj) { |
| 18811 | |
| 18812 | valObj = interp->emptyObj; |
| 18813 | } |
| 18814 | // XXX |
| 18815 | //Jim_IncrRefCount(valObj); |
| 18816 | result = Jim_SetVariable(interp, varName, valObj); |
| 18817 | //Jim_DecrRefCount(interp, valObj); |
| 18818 | if (result != JIM_OK) { |
| 18819 | goto err; |
| 18820 | } |
| 18821 | } |
| 18822 | } |
| @@ -17845,15 +18868,10 @@ | |
| 18868 | int result = JIM_ERR; |
| 18869 | int i; |
| 18870 | Jim_ListIter iter; |
| 18871 | Jim_Obj *resultObj; |
| 18872 | |
| 18873 | JimListIterInit(&iter, argv[1]); |
| 18874 | |
| 18875 | for (i = 2; i < argc; i++) { |
| 18876 | Jim_Obj *valObj = JimListIterNext(interp, &iter); |
| 18877 | result = Jim_SetVariable(interp, argv[i], valObj ? valObj : interp->emptyObj); |
| @@ -17875,56 +18893,56 @@ | |
| 18893 | |
| 18894 | static int Jim_IfCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 18895 | { |
| 18896 | int boolean, retval, current = 1, falsebody = 0; |
| 18897 | |
| 18898 | while (1) { |
| 18899 | |
| 18900 | if (current >= argc) { |
| 18901 | return JIM_USAGE; |
| 18902 | } |
| 18903 | if ((retval = Jim_GetBoolFromExpr(interp, argv[current++], &boolean)) |
| 18904 | != JIM_OK) |
| 18905 | return retval; |
| 18906 | |
| 18907 | if (current >= argc) { |
| 18908 | return JIM_USAGE; |
| 18909 | } |
| 18910 | if (Jim_CompareStringImmediate(interp, argv[current], "then")) |
| 18911 | current++; |
| 18912 | |
| 18913 | if (current >= argc) { |
| 18914 | return JIM_USAGE; |
| 18915 | } |
| 18916 | if (boolean) |
| 18917 | return Jim_EvalObj(interp, argv[current]); |
| 18918 | |
| 18919 | if (++current >= argc) { |
| 18920 | Jim_SetResult(interp, Jim_NewEmptyStringObj(interp)); |
| 18921 | return JIM_OK; |
| 18922 | } |
| 18923 | falsebody = current++; |
| 18924 | if (Jim_CompareStringImmediate(interp, argv[falsebody], "else")) { |
| 18925 | |
| 18926 | if (current != argc - 1) { |
| 18927 | return JIM_USAGE; |
| 18928 | } |
| 18929 | return Jim_EvalObj(interp, argv[current]); |
| 18930 | } |
| 18931 | else if (Jim_CompareStringImmediate(interp, argv[falsebody], "elseif")) |
| 18932 | continue; |
| 18933 | |
| 18934 | else if (falsebody != argc - 1) { |
| 18935 | return JIM_USAGE; |
| 18936 | } |
| 18937 | return Jim_EvalObj(interp, argv[falsebody]); |
| 18938 | } |
| 18939 | return JIM_OK; |
| 18940 | } |
| 18941 | |
| 18942 | |
| 18943 | static int JimCommandMatchObj(Jim_Interp *interp, Jim_Obj *commandObj, Jim_Obj *patternObj, |
| 18944 | Jim_Obj *stringObj, int flags) |
| 18945 | { |
| 18946 | Jim_Obj *parms[5]; |
| 18947 | int argc = 0; |
| 18948 | long eq; |
| @@ -17956,16 +18974,10 @@ | |
| 18974 | int matchOpt = SWITCH_EXACT, opt = 1, patCount, i; |
| 18975 | int match_flags = 0; |
| 18976 | Jim_Obj *command = NULL, *scriptObj = NULL, *strObj; |
| 18977 | Jim_Obj **caseList; |
| 18978 | |
| 18979 | for (opt = 1; opt < argc; ++opt) { |
| 18980 | const char *option = Jim_String(argv[opt]); |
| 18981 | |
| 18982 | if (*option != '-') |
| 18983 | break; |
| @@ -17982,31 +18994,31 @@ | |
| 18994 | match_flags |= JIM_OPT_END; |
| 18995 | } |
| 18996 | else if (strncmp(option, "-command", 2) == 0) { |
| 18997 | matchOpt = SWITCH_CMD; |
| 18998 | if ((argc - opt) < 2) |
| 18999 | return JIM_USAGE; |
| 19000 | command = argv[++opt]; |
| 19001 | } |
| 19002 | else { |
| 19003 | Jim_SetResultFormatted(interp, |
| 19004 | "bad option \"%#s\": must be -exact, -glob, -regexp, -command procname or --", |
| 19005 | argv[opt]); |
| 19006 | return JIM_ERR; |
| 19007 | } |
| 19008 | if ((argc - opt) < 2) |
| 19009 | return JIM_USAGE; |
| 19010 | } |
| 19011 | strObj = argv[opt++]; |
| 19012 | patCount = argc - opt; |
| 19013 | if (patCount == 1) { |
| 19014 | JimListGetElements(interp, argv[opt], &patCount, &caseList); |
| 19015 | } |
| 19016 | else |
| 19017 | caseList = (Jim_Obj **)&argv[opt]; |
| 19018 | if (patCount == 0 || patCount % 2 != 0) |
| 19019 | return JIM_USAGE; |
| 19020 | for (i = 0; scriptObj == NULL && i < patCount; i += 2) { |
| 19021 | Jim_Obj *patObj = caseList[i]; |
| 19022 | |
| 19023 | if (!Jim_CompareStringImmediate(interp, patObj, "default") |
| 19024 | || i < (patCount - 2)) { |
| @@ -18021,11 +19033,11 @@ | |
| 19033 | break; |
| 19034 | case SWITCH_RE: |
| 19035 | command = Jim_NewStringObj(interp, "regexp", -1); |
| 19036 | |
| 19037 | case SWITCH_CMD:{ |
| 19038 | int rc = JimCommandMatchObj(interp, command, patObj, strObj, match_flags); |
| 19039 | |
| 19040 | if (argc - opt == 1) { |
| 19041 | JimListGetElements(interp, argv[opt], &patCount, &caseList); |
| 19042 | } |
| 19043 | |
| @@ -18069,14 +19081,10 @@ | |
| 19081 | static int Jim_LindexCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 19082 | { |
| 19083 | Jim_Obj *objPtr; |
| 19084 | int ret; |
| 19085 | |
| 19086 | ret = Jim_ListIndices(interp, argv[1], argv + 2, argc - 2, &objPtr, JIM_NONE); |
| 19087 | if (ret < 0) { |
| 19088 | ret = JIM_OK; |
| 19089 | Jim_SetEmptyResult(interp); |
| 19090 | } |
| @@ -18087,14 +19095,10 @@ | |
| 19095 | } |
| 19096 | |
| 19097 | |
| 19098 | static int Jim_LlengthCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 19099 | { |
| 19100 | Jim_SetResultInt(interp, Jim_ListLength(interp, argv[1])); |
| 19101 | return JIM_OK; |
| 19102 | } |
| 19103 | |
| 19104 | |
| @@ -18119,17 +19123,10 @@ | |
| 19123 | Jim_Obj *commandObj = NULL; |
| 19124 | Jim_Obj *indexObj = NULL; |
| 19125 | int match_flags = 0; |
| 19126 | long stride = 1; |
| 19127 | |
| 19128 | for (i = 1; i < argc - 2; i++) { |
| 19129 | int option; |
| 19130 | |
| 19131 | if (Jim_GetEnum(interp, argv[i], options, &option, NULL, JIM_ERRMSG) != JIM_OK) { |
| 19132 | return JIM_ERR; |
| @@ -18156,27 +19153,27 @@ | |
| 19153 | opt_match = option; |
| 19154 | match_flags |= JIM_OPT_END; |
| 19155 | break; |
| 19156 | case OPT_COMMAND: |
| 19157 | if (i >= argc - 2) { |
| 19158 | return JIM_USAGE; |
| 19159 | } |
| 19160 | commandObj = argv[++i]; |
| 19161 | |
| 19162 | case OPT_EXACT: |
| 19163 | case OPT_GLOB: |
| 19164 | opt_match = option; |
| 19165 | break; |
| 19166 | case OPT_INDEX: |
| 19167 | if (i >= argc - 2) { |
| 19168 | return JIM_USAGE; |
| 19169 | } |
| 19170 | indexObj = argv[++i]; |
| 19171 | break; |
| 19172 | case OPT_STRIDE: |
| 19173 | if (i >= argc - 2) { |
| 19174 | return JIM_USAGE; |
| 19175 | } |
| 19176 | if (Jim_GetLong(interp, argv[++i], &stride) != JIM_OK) { |
| 19177 | return JIM_ERR; |
| 19178 | } |
| 19179 | if (stride < 1) { |
| @@ -18187,11 +19184,11 @@ | |
| 19184 | } |
| 19185 | } |
| 19186 | |
| 19187 | argc -= i; |
| 19188 | if (argc < 2) { |
| 19189 | return JIM_USAGE; |
| 19190 | } |
| 19191 | argv += i; |
| 19192 | |
| 19193 | listlen = Jim_ListLength(interp, argv[0]); |
| 19194 | if (listlen % stride) { |
| @@ -18250,11 +19247,11 @@ | |
| 19247 | eq = Jim_StringMatchObj(interp, argv[1], objPtr, match_flags); |
| 19248 | break; |
| 19249 | |
| 19250 | case OPT_REGEXP: |
| 19251 | case OPT_COMMAND: |
| 19252 | eq = JimCommandMatchObj(interp, commandObj, argv[1], objPtr, match_flags); |
| 19253 | if (eq < 0) { |
| 19254 | Jim_DecrRefCount(interp, searchListObj); |
| 19255 | rc = JIM_ERR; |
| 19256 | goto done; |
| 19257 | } |
| @@ -18326,32 +19323,23 @@ | |
| 19323 | |
| 19324 | |
| 19325 | static int Jim_LappendCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 19326 | { |
| 19327 | Jim_Obj *listObjPtr; |
| 19328 | int i; |
| 19329 | |
| 19330 | listObjPtr = Jim_GetVariable(interp, argv[1], JIM_UNSHARED); |
| 19331 | if (!listObjPtr) { |
| 19332 | |
| 19333 | listObjPtr = Jim_NewListObj(interp, NULL, 0); |
| 19334 | } |
| 19335 | else if (Jim_IsShared(listObjPtr)) { |
| 19336 | listObjPtr = Jim_DuplicateObj(interp, listObjPtr); |
| 19337 | } |
| 19338 | for (i = 2; i < argc; i++) |
| 19339 | Jim_ListAppendElement(interp, listObjPtr, argv[i]); |
| 19340 | if (Jim_SetVariable(interp, argv[1], listObjPtr) != JIM_OK) { |
| 19341 | return JIM_ERR; |
| 19342 | } |
| 19343 | Jim_SetResult(interp, listObjPtr); |
| 19344 | return JIM_OK; |
| 19345 | } |
| @@ -18360,14 +19348,10 @@ | |
| 19348 | static int Jim_LinsertCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 19349 | { |
| 19350 | int idx, len; |
| 19351 | Jim_Obj *listPtr; |
| 19352 | |
| 19353 | listPtr = argv[1]; |
| 19354 | if (Jim_IsShared(listPtr)) |
| 19355 | listPtr = Jim_DuplicateObj(interp, listPtr); |
| 19356 | if (Jim_GetIndex(interp, argv[2], &idx) != JIM_OK) |
| 19357 | goto err; |
| @@ -18391,14 +19375,10 @@ | |
| 19375 | { |
| 19376 | int first, last, len, rangeLen; |
| 19377 | Jim_Obj *listObj; |
| 19378 | Jim_Obj *newListObj; |
| 19379 | |
| 19380 | if (Jim_GetIndex(interp, argv[2], &first) != JIM_OK || |
| 19381 | Jim_GetIndex(interp, argv[3], &last) != JIM_OK) { |
| 19382 | return JIM_ERR; |
| 19383 | } |
| 19384 | |
| @@ -18428,15 +19408,11 @@ | |
| 19408 | } |
| 19409 | |
| 19410 | |
| 19411 | static int Jim_LsetCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 19412 | { |
| 19413 | if (argc == 3) { |
| 19414 | |
| 19415 | if (Jim_SetVariable(interp, argv[1], argv[2]) != JIM_OK) |
| 19416 | return JIM_ERR; |
| 19417 | Jim_SetResult(interp, argv[2]); |
| 19418 | return JIM_OK; |
| @@ -18463,16 +19439,10 @@ | |
| 19439 | Jim_Obj **elements; |
| 19440 | int listlen; |
| 19441 | |
| 19442 | struct lsort_info info; |
| 19443 | |
| 19444 | info.type = JIM_LSORT_ASCII; |
| 19445 | info.order = 1; |
| 19446 | info.indexc = 0; |
| 19447 | info.unique = 0; |
| 19448 | info.command = NULL; |
| @@ -18518,11 +19488,11 @@ | |
| 19488 | info.command = argv[i + 1]; |
| 19489 | i++; |
| 19490 | break; |
| 19491 | case OPT_STRIDE: |
| 19492 | if (i >= argc - 2) { |
| 19493 | return JIM_USAGE; |
| 19494 | } |
| 19495 | if (Jim_GetLong(interp, argv[++i], &stride) != JIM_OK) { |
| 19496 | return JIM_ERR; |
| 19497 | } |
| 19498 | if (stride < 2) { |
| @@ -18596,38 +19566,28 @@ | |
| 19566 | static int Jim_AppendCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 19567 | { |
| 19568 | Jim_Obj *stringObjPtr; |
| 19569 | int i; |
| 19570 | |
| 19571 | if (argc == 2) { |
| 19572 | stringObjPtr = Jim_GetVariable(interp, argv[1], JIM_ERRMSG); |
| 19573 | if (!stringObjPtr) |
| 19574 | return JIM_ERR; |
| 19575 | } |
| 19576 | else { |
| 19577 | stringObjPtr = Jim_GetVariable(interp, argv[1], JIM_UNSHARED); |
| 19578 | if (!stringObjPtr) { |
| 19579 | |
| 19580 | stringObjPtr = Jim_NewEmptyStringObj(interp); |
| 19581 | } |
| 19582 | else if (Jim_IsShared(stringObjPtr)) { |
| 19583 | stringObjPtr = Jim_DuplicateObj(interp, stringObjPtr); |
| 19584 | } |
| 19585 | for (i = 2; i < argc; i++) { |
| 19586 | Jim_AppendObj(interp, stringObjPtr, argv[i]); |
| 19587 | } |
| 19588 | if (Jim_SetVariable(interp, argv[1], stringObjPtr) != JIM_OK) { |
| 19589 | return JIM_ERR; |
| 19590 | } |
| 19591 | } |
| 19592 | Jim_SetResult(interp, stringObjPtr); |
| 19593 | return JIM_OK; |
| @@ -18639,15 +19599,10 @@ | |
| 19599 | |
| 19600 | static int Jim_EvalCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 19601 | { |
| 19602 | int rc; |
| 19603 | |
| 19604 | if (argc == 2) { |
| 19605 | rc = Jim_EvalObj(interp, argv[1]); |
| 19606 | } |
| 19607 | else { |
| 19608 | rc = Jim_EvalObj(interp, Jim_ConcatObj(interp, argc - 1, argv + 1)); |
| @@ -18657,88 +19612,67 @@ | |
| 19612 | } |
| 19613 | |
| 19614 | |
| 19615 | static int Jim_UplevelCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 19616 | { |
| 19617 | int retcode; |
| 19618 | Jim_CallFrame *savedCallFrame, *targetCallFrame; |
| 19619 | const char *str; |
| 19620 | |
| 19621 | |
| 19622 | savedCallFrame = interp->framePtr; |
| 19623 | |
| 19624 | |
| 19625 | str = Jim_String(argv[1]); |
| 19626 | if ((str[0] >= '0' && str[0] <= '9') || str[0] == '#') { |
| 19627 | targetCallFrame = Jim_GetCallFrameByLevel(interp, argv[1]); |
| 19628 | argc--; |
| 19629 | argv++; |
| 19630 | } |
| 19631 | else { |
| 19632 | targetCallFrame = Jim_GetCallFrameByLevel(interp, NULL); |
| 19633 | } |
| 19634 | if (targetCallFrame == NULL) { |
| 19635 | return JIM_ERR; |
| 19636 | } |
| 19637 | if (argc < 2) { |
| 19638 | return JIM_USAGE; |
| 19639 | } |
| 19640 | |
| 19641 | interp->framePtr = targetCallFrame; |
| 19642 | if (argc == 2) { |
| 19643 | retcode = Jim_EvalObj(interp, argv[1]); |
| 19644 | } |
| 19645 | else { |
| 19646 | retcode = Jim_EvalObj(interp, Jim_ConcatObj(interp, argc - 1, argv + 1)); |
| 19647 | } |
| 19648 | interp->framePtr = savedCallFrame; |
| 19649 | return retcode; |
| 19650 | } |
| 19651 | |
| 19652 | |
| 19653 | static int Jim_ExprCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 19654 | { |
| 19655 | #ifdef JIM_COMPAT |
| 19656 | if (argc > 2) { |
| 19657 | int retcode; |
| 19658 | Jim_Obj *objPtr; |
| 19659 | |
| 19660 | objPtr = Jim_ConcatObj(interp, argc - 1, argv + 1); |
| 19661 | Jim_IncrRefCount(objPtr); |
| 19662 | retcode = Jim_EvalExpression(interp, objPtr); |
| 19663 | Jim_DecrRefCount(interp, objPtr); |
| 19664 | |
| 19665 | return retcode; |
| 19666 | } |
| 19667 | #endif |
| 19668 | return Jim_EvalExpression(interp, argv[1]); |
| 19669 | } |
| 19670 | |
| 19671 | |
| 19672 | static int JimBreakContinueHelper(Jim_Interp *interp, int argc, Jim_Obj *const *argv, int retcode) |
| 19673 | { |
| 19674 | if (argc == 2) { |
| 19675 | long level; |
| 19676 | int ret = Jim_GetLong(interp, argv[1], &level); |
| 19677 | if (ret != JIM_OK) { |
| 19678 | return ret; |
| @@ -18822,12 +19756,11 @@ | |
| 19756 | break; |
| 19757 | } |
| 19758 | } |
| 19759 | |
| 19760 | if (i != argc - 1 && i != argc) { |
| 19761 | return JIM_USAGE; |
| 19762 | } |
| 19763 | |
| 19764 | |
| 19765 | if (stackTraceObj && returnCode == JIM_ERR) { |
| 19766 | JimSetStackTrace(interp, stackTraceObj); |
| @@ -18876,10 +19809,11 @@ | |
| 19809 | |
| 19810 | return JIM_EVAL; |
| 19811 | } |
| 19812 | return JIM_OK; |
| 19813 | } |
| 19814 | |
| 19815 | |
| 19816 | static int JimAliasCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 19817 | { |
| 19818 | Jim_Obj *cmdList; |
| 19819 | Jim_Obj *prefixListObj = Jim_CmdPrivData(interp); |
| @@ -18888,43 +19822,34 @@ | |
| 19822 | cmdList = Jim_DuplicateObj(interp, prefixListObj); |
| 19823 | Jim_ListInsertElements(interp, cmdList, Jim_ListLength(interp, cmdList), argc - 1, argv + 1); |
| 19824 | |
| 19825 | return JimEvalObjList(interp, cmdList); |
| 19826 | } |
| 19827 | |
| 19828 | |
| 19829 | static void JimAliasCmdDelete(Jim_Interp *interp, void *privData) |
| 19830 | { |
| 19831 | Jim_Obj *prefixListObj = privData; |
| 19832 | Jim_DecrRefCount(interp, prefixListObj); |
| 19833 | } |
| 19834 | |
| 19835 | |
| 19836 | static int Jim_AliasCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 19837 | { |
| 19838 | Jim_Obj *prefixListObj = Jim_NewListObj(interp, argv + 2, argc - 2); |
| 19839 | Jim_IncrRefCount(prefixListObj); |
| 19840 | Jim_SetResult(interp, argv[1]); |
| 19841 | |
| 19842 | Jim_RegisterCommand(interp, argv[1], JimAliasCmd, JimAliasCmdDelete, NULL, NULL, 0, -1, JIM_CMD_ISALIAS, prefixListObj); |
| 19843 | return JIM_OK; |
| 19844 | } |
| 19845 | |
| 19846 | |
| 19847 | static int Jim_ProcCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 19848 | { |
| 19849 | Jim_Cmd *cmd; |
| 19850 | |
| 19851 | if (argc == 4) { |
| 19852 | cmd = JimCreateProcedureCmd(interp, argv[2], NULL, argv[3], NULL); |
| 19853 | } |
| 19854 | else { |
| 19855 | cmd = JimCreateProcedureCmd(interp, argv[2], argv[3], argv[4], NULL); |
| @@ -18947,15 +19872,10 @@ | |
| 19872 | } |
| 19873 | |
| 19874 | |
| 19875 | static int Jim_XtraceCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 19876 | { |
| 19877 | if (interp->traceCmdObj) { |
| 19878 | Jim_DecrRefCount(interp, interp->traceCmdObj); |
| 19879 | interp->traceCmdObj = NULL; |
| 19880 | } |
| 19881 | |
| @@ -18970,15 +19890,10 @@ | |
| 19890 | |
| 19891 | static int Jim_LocalCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 19892 | { |
| 19893 | int retcode; |
| 19894 | |
| 19895 | |
| 19896 | interp->local++; |
| 19897 | retcode = Jim_EvalObjVector(interp, argc - 1, argv + 1); |
| 19898 | interp->local--; |
| 19899 | |
| @@ -18990,11 +19905,11 @@ | |
| 19905 | if (Jim_GetCommand(interp, cmdNameObj, JIM_ERRMSG) == NULL) { |
| 19906 | return JIM_ERR; |
| 19907 | } |
| 19908 | if (interp->framePtr->localCommands == NULL) { |
| 19909 | interp->framePtr->localCommands = Jim_Alloc(sizeof(*interp->framePtr->localCommands)); |
| 19910 | Jim_StackInit(interp->framePtr->localCommands, NULL); |
| 19911 | } |
| 19912 | Jim_IncrRefCount(cmdNameObj); |
| 19913 | Jim_StackPush(interp->framePtr->localCommands, cmdNameObj); |
| 19914 | } |
| 19915 | |
| @@ -19002,89 +19917,129 @@ | |
| 19917 | } |
| 19918 | |
| 19919 | |
| 19920 | static int Jim_UpcallCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 19921 | { |
| 19922 | int retcode; |
| 19923 | |
| 19924 | Jim_Cmd *cmdPtr = Jim_GetCommand(interp, argv[1], JIM_ERRMSG); |
| 19925 | if (cmdPtr == NULL || !(cmdPtr->flags & JIM_CMD_ISPROC) || !cmdPtr->prevCmd) { |
| 19926 | Jim_SetResultFormatted(interp, "no previous command: \"%#s\"", argv[1]); |
| 19927 | return JIM_ERR; |
| 19928 | } |
| 19929 | |
| 19930 | cmdPtr->u.proc.upcall++; |
| 19931 | JimIncrCmdRefCount(cmdPtr); |
| 19932 | |
| 19933 | |
| 19934 | retcode = Jim_EvalObjVector(interp, argc - 1, argv + 1); |
| 19935 | |
| 19936 | |
| 19937 | cmdPtr->u.proc.upcall--; |
| 19938 | JimDecrCmdRefCount(interp, cmdPtr); |
| 19939 | |
| 19940 | return retcode; |
| 19941 | } |
| 19942 | |
| 19943 | |
| 19944 | static int Jim_ApplyCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 19945 | { |
| 19946 | int ret; |
| 19947 | Jim_Cmd *cmd; |
| 19948 | Jim_Obj *argListObjPtr; |
| 19949 | Jim_Obj *bodyObjPtr; |
| 19950 | Jim_Obj *nsObj = NULL; |
| 19951 | Jim_Obj **nargv; |
| 19952 | |
| 19953 | int len = Jim_ListLength(interp, argv[1]); |
| 19954 | if (len != 2 && len != 3) { |
| 19955 | Jim_SetResultFormatted(interp, "can't interpret \"%#s\" as a lambda expression", argv[1]); |
| 19956 | return JIM_ERR; |
| 19957 | } |
| 19958 | |
| 19959 | if (len == 3) { |
| 19960 | #ifdef jim_ext_namespace |
| 19961 | |
| 19962 | nsObj = Jim_ListGetIndex(interp, argv[1], 2); |
| 19963 | #else |
| 19964 | Jim_SetResultString(interp, "namespaces not enabled", -1); |
| 19965 | return JIM_ERR; |
| 19966 | #endif |
| 19967 | } |
| 19968 | argListObjPtr = Jim_ListGetIndex(interp, argv[1], 0); |
| 19969 | bodyObjPtr = Jim_ListGetIndex(interp, argv[1], 1); |
| 19970 | |
| 19971 | cmd = JimCreateProcedureCmd(interp, argListObjPtr, NULL, bodyObjPtr, nsObj); |
| 19972 | |
| 19973 | if (cmd) { |
| 19974 | |
| 19975 | nargv = Jim_Alloc((argc - 2 + 1) * sizeof(*nargv)); |
| 19976 | nargv[0] = Jim_NewStringObj(interp, "apply lambdaExpr", -1); |
| 19977 | Jim_IncrRefCount(nargv[0]); |
| 19978 | memcpy(&nargv[1], argv + 2, (argc - 2) * sizeof(*nargv)); |
| 19979 | ret = JimCallProcedure(interp, cmd, argc - 2 + 1, nargv); |
| 19980 | Jim_DecrRefCount(interp, nargv[0]); |
| 19981 | Jim_Free(nargv); |
| 19982 | |
| 19983 | JimDecrCmdRefCount(interp, cmd); |
| 19984 | return ret; |
| 19985 | } |
| 19986 | return JIM_ERR; |
| 19987 | } |
| 19988 | |
| 19989 | #ifdef JIM_TAINT |
| 19990 | static int JimTaintVariable(Jim_Interp *interp, Jim_Obj *nameObjPtr, int taint) |
| 19991 | { |
| 19992 | Jim_Obj *valueObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG | JIM_UNSHARED); |
| 19993 | |
| 19994 | if (valueObjPtr == NULL) { |
| 19995 | return JIM_ERR; |
| 19996 | } |
| 19997 | |
| 19998 | if (Jim_IsShared(valueObjPtr)) { |
| 19999 | valueObjPtr = Jim_DuplicateObj(interp, valueObjPtr); |
| 20000 | Jim_SetVariable(interp, nameObjPtr, valueObjPtr); |
| 20001 | } |
| 20002 | |
| 20003 | if (taint && nameObjPtr->typePtr == &dictSubstObjType) { |
| 20004 | |
| 20005 | Jim_Obj *objPtr = Jim_GetVariable(interp, nameObjPtr->internalRep.dictSubstValue.varNameObjPtr, JIM_NONE); |
| 20006 | if (objPtr) { |
| 20007 | SetStringFromAny(interp, valueObjPtr); |
| 20008 | valueObjPtr->taint = taint; |
| 20009 | objPtr->taint |= taint; |
| 20010 | } |
| 20011 | } |
| 20012 | |
| 20013 | |
| 20014 | SetStringFromAny(interp, valueObjPtr); |
| 20015 | valueObjPtr->taint = taint; |
| 20016 | |
| 20017 | //printf("taint of %s is %d\n", valueObjPtr->bytes, valueObjPtr->taint); |
| 20018 | return JIM_OK; |
| 20019 | } |
| 20020 | |
| 20021 | |
| 20022 | static int Jim_TaintCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 20023 | { |
| 20024 | if (argc != 2) { |
| 20025 | Jim_WrongNumArgs(interp, 1, argv, "varname"); |
| 20026 | return JIM_ERR; |
| 20027 | } |
| 20028 | return JimTaintVariable(interp, argv[1], 1); |
| 20029 | } |
| 20030 | |
| 20031 | |
| 20032 | static int Jim_UntaintCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 20033 | { |
| 20034 | if (argc != 2) { |
| 20035 | Jim_WrongNumArgs(interp, 1, argv, "varname"); |
| 20036 | return JIM_ERR; |
| 20037 | } |
| 20038 | return JimTaintVariable(interp, argv[1], 0); |
| 20039 | } |
| 20040 | #endif |
| 20041 | |
| 20042 | |
| 20043 | static int Jim_ConcatCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 20044 | { |
| 20045 | Jim_SetResult(interp, Jim_ConcatObj(interp, argc - 1, argv + 1)); |
| @@ -19110,12 +20065,11 @@ | |
| 20065 | return JIM_ERR; |
| 20066 | } |
| 20067 | |
| 20068 | |
| 20069 | if (argc < 3) { |
| 20070 | return JIM_USAGE; |
| 20071 | } |
| 20072 | |
| 20073 | |
| 20074 | for (i = 1; i < argc; i += 2) { |
| 20075 | if (Jim_SetVariableLink(interp, argv[i + 1], argv[i], targetCallFrame) != JIM_OK) |
| @@ -19127,14 +20081,10 @@ | |
| 20081 | |
| 20082 | static int Jim_GlobalCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 20083 | { |
| 20084 | int i; |
| 20085 | |
| 20086 | |
| 20087 | if (interp->framePtr->level == 0) |
| 20088 | return JIM_OK; |
| 20089 | for (i = 1; i < argc; i++) { |
| 20090 | |
| @@ -19244,11 +20194,11 @@ | |
| 20194 | OPT_COUNT |
| 20195 | }; |
| 20196 | static const jim_subcmd_type cmds[OPT_COUNT + 1] = { |
| 20197 | JIM_DEF_SUBCMD("bytelength", "string", 1, 1), |
| 20198 | JIM_DEF_SUBCMD("byterange", "string first last", 3, 3), |
| 20199 | JIM_DEF_SUBCMD("cat", "?string ...?", 0, -1), |
| 20200 | JIM_DEF_SUBCMD("compare", "?-nocase? ?-length int? string1 string2", 2, 5), |
| 20201 | JIM_DEF_SUBCMD("equal", "?-nocase? ?-length int? string1 string2", 2, 5), |
| 20202 | JIM_DEF_SUBCMD("first", "subString string ?index?", 2, 3), |
| 20203 | JIM_DEF_SUBCMD("index", "string index", 2, 2), |
| 20204 | JIM_DEF_SUBCMD("is", "class ?-strict? str", 2, 3), |
| @@ -19264,11 +20214,11 @@ | |
| 20214 | JIM_DEF_SUBCMD("totitle", "string", 1, 1), |
| 20215 | JIM_DEF_SUBCMD("toupper", "string", 1, 1), |
| 20216 | JIM_DEF_SUBCMD("trim", "string ?trimchars?", 1, 2), |
| 20217 | JIM_DEF_SUBCMD("trimleft", "string ?trimchars?", 1, 2), |
| 20218 | JIM_DEF_SUBCMD("trimright", "string ?trimchars?", 1, 2), |
| 20219 | { NULL } |
| 20220 | }; |
| 20221 | const jim_subcmd_type *ct = Jim_ParseSubCmd(interp, cmds, argc, argv); |
| 20222 | if (!ct) { |
| 20223 | return JIM_ERR; |
| 20224 | } |
| @@ -19562,14 +20512,10 @@ | |
| 20512 | static int Jim_TimeCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 20513 | { |
| 20514 | long i, count = 1; |
| 20515 | jim_wide start, elapsed; |
| 20516 | |
| 20517 | if (argc == 3) { |
| 20518 | if (Jim_GetLong(interp, argv[2], &count) != JIM_OK) |
| 20519 | return JIM_ERR; |
| 20520 | } |
| 20521 | if (count < 0) |
| @@ -19594,23 +20540,19 @@ | |
| 20540 | Jim_AppendString(interp, Jim_GetResult(interp)," microseconds per iteration", -1); |
| 20541 | return JIM_OK; |
| 20542 | } |
| 20543 | |
| 20544 | |
| 20545 | static int Jim_TimerateCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 20546 | { |
| 20547 | long us = 0; |
| 20548 | jim_wide start, delta, overhead; |
| 20549 | Jim_Obj *objPtr; |
| 20550 | double us_per_iter; |
| 20551 | int count; |
| 20552 | int n; |
| 20553 | |
| 20554 | if (argc == 3) { |
| 20555 | if (Jim_GetLong(interp, argv[2], &us) != JIM_OK) |
| 20556 | return JIM_ERR; |
| 20557 | us *= 1000; |
| 20558 | } |
| @@ -19663,22 +20605,19 @@ | |
| 20605 | |
| 20606 | static int Jim_ExitCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 20607 | { |
| 20608 | long exitCode = 0; |
| 20609 | |
| 20610 | if (argc == 2) { |
| 20611 | if (Jim_GetLong(interp, argv[1], &exitCode) != JIM_OK) |
| 20612 | return JIM_ERR; |
| 20613 | Jim_SetResult(interp, argv[1]); |
| 20614 | } |
| 20615 | interp->exitCode = exitCode; |
| 20616 | return JIM_EXIT; |
| 20617 | } |
| 20618 | |
| 20619 | |
| 20620 | static int JimMatchReturnCodes(Jim_Interp *interp, Jim_Obj *retcodeListObj, int rc) |
| 20621 | { |
| 20622 | int len = Jim_ListLength(interp, retcodeListObj); |
| 20623 | int i; |
| @@ -19695,14 +20634,10 @@ | |
| 20634 | } |
| 20635 | |
| 20636 | |
| 20637 | static int JimCatchTryHelper(Jim_Interp *interp, int istry, int argc, Jim_Obj *const *argv) |
| 20638 | { |
| 20639 | int exitCode = 0; |
| 20640 | int i; |
| 20641 | int sig = 0; |
| 20642 | int ok; |
| 20643 | Jim_Obj *finallyScriptObj = NULL; |
| @@ -19748,11 +20683,11 @@ | |
| 20683 | } |
| 20684 | if (option < 0) { |
| 20685 | option = Jim_FindByName(arg, jimReturnCodes, jimReturnCodesSize); |
| 20686 | } |
| 20687 | if (option < 0) { |
| 20688 | return JIM_USAGE; |
| 20689 | } |
| 20690 | |
| 20691 | if (ignore) { |
| 20692 | ignore_mask |= ((jim_wide)1 << option); |
| 20693 | } |
| @@ -19762,13 +20697,11 @@ | |
| 20697 | } |
| 20698 | |
| 20699 | idx = i; |
| 20700 | |
| 20701 | if (argc - idx < 1) { |
| 20702 | return JIM_USAGE; |
| 20703 | } |
| 20704 | |
| 20705 | if ((ignore_mask & (1 << JIM_SIGNAL)) == 0) { |
| 20706 | sig++; |
| 20707 | } |
| @@ -19779,11 +20712,11 @@ | |
| 20712 | exitCode = JIM_SIGNAL; |
| 20713 | } |
| 20714 | else { |
| 20715 | exitCode = Jim_EvalObj(interp, argv[idx]); |
| 20716 | |
| 20717 | interp->hasErrorStackTrace = 0; |
| 20718 | } |
| 20719 | interp->signal_level -= sig; |
| 20720 | |
| 20721 | errorCodeObj = Jim_GetGlobalVariableStr(interp, "errorCode", JIM_NONE); |
| 20722 | |
| @@ -19800,30 +20733,36 @@ | |
| 20733 | } |
| 20734 | switch (option) { |
| 20735 | case TRY_ON: |
| 20736 | case TRY_TRAP: |
| 20737 | if (idx + 4 > argc) { |
| 20738 | return JIM_USAGE; |
| 20739 | } |
| 20740 | if (option == TRY_ON) { |
| 20741 | ret = JimMatchReturnCodes(interp, argv[idx + 1], exitCode); |
| 20742 | if (ret > JIM_OK) { |
| 20743 | return JIM_USAGE; |
| 20744 | } |
| 20745 | } |
| 20746 | else if (errorCodeObj) { |
| 20747 | int len = Jim_ListLength(interp, argv[idx + 1]); |
| 20748 | |
| 20749 | if (len > Jim_ListLength(interp, errorCodeObj)) { |
| 20750 | |
| 20751 | ret = -1; |
| 20752 | } |
| 20753 | else { |
| 20754 | int i; |
| 20755 | ret = JIM_OK; |
| 20756 | |
| 20757 | for (i = 0; i < len; i++) { |
| 20758 | Jim_Obj *matchObj = Jim_ListGetIndex(interp, argv[idx + 1], i); |
| 20759 | Jim_Obj *objPtr = Jim_ListGetIndex(interp, errorCodeObj, i); |
| 20760 | if (Jim_StringCompareObj(interp, matchObj, objPtr, 0) != 0) { |
| 20761 | ret = -1; |
| 20762 | break; |
| 20763 | } |
| 20764 | } |
| 20765 | } |
| 20766 | } |
| 20767 | else { |
| 20768 | |
| @@ -19837,11 +20776,11 @@ | |
| 20776 | } |
| 20777 | idx += 4; |
| 20778 | break; |
| 20779 | case TRY_FINALLY: |
| 20780 | if (idx + 2 != argc) { |
| 20781 | return JIM_USAGE; |
| 20782 | } |
| 20783 | finallyScriptObj = argv[idx + 1]; |
| 20784 | idx += 2; |
| 20785 | break; |
| 20786 | } |
| @@ -19945,15 +20884,10 @@ | |
| 20884 | |
| 20885 | |
| 20886 | |
| 20887 | static int Jim_RenameCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 20888 | { |
| 20889 | return Jim_RenameCommand(interp, argv[1], argv[2]); |
| 20890 | } |
| 20891 | |
| 20892 | #define JIM_DICTMATCH_KEYS 0x0001 |
| 20893 | #define JIM_DICTMATCH_VALUES 0x002 |
| @@ -20043,10 +20977,11 @@ | |
| 20977 | snprintf(buffer, sizeof(buffer), "%d entries in table, %d buckets", dict->len, dict->size); |
| 20978 | output = Jim_NewStringObj(interp, buffer, -1); |
| 20979 | Jim_SetResult(interp, output); |
| 20980 | return JIM_OK; |
| 20981 | } |
| 20982 | |
| 20983 | |
| 20984 | static int Jim_EvalEnsemble(Jim_Interp *interp, const char *basecmd, const char *subcmd, int argc, Jim_Obj *const *argv) |
| 20985 | { |
| 20986 | Jim_Obj *prefixObj = Jim_NewStringObj(interp, basecmd, -1); |
| 20987 | |
| @@ -20157,11 +21092,11 @@ | |
| 21092 | JIM_DEF_SUBCMD("remove", "dictionary ?key ...?", 1, -1), |
| 21093 | JIM_DEF_SUBCMD("values", "dictionary ?pattern?", 1, 2), |
| 21094 | JIM_DEF_SUBCMD("for", "vars dictionary script", 3, 3), |
| 21095 | JIM_DEF_SUBCMD("replace", "dictionary ?key value ...?", 1, -1), |
| 21096 | JIM_DEF_SUBCMD("update", "varName ?arg ...? script", 2, -1), |
| 21097 | { NULL } |
| 21098 | }; |
| 21099 | const jim_subcmd_type *ct = Jim_ParseSubCmd(interp, cmds, argc, argv); |
| 21100 | if (!ct) { |
| 21101 | return JIM_ERR; |
| 21102 | } |
| @@ -20195,11 +21130,11 @@ | |
| 21130 | } |
| 21131 | return JIM_OK; |
| 21132 | } |
| 21133 | |
| 21134 | case OPT_SET: |
| 21135 | return Jim_SetDictKeysVector(interp, argv[2], argv + 3, argc - 4, argv[argc - 1], JIM_ERRMSG | JIM_UNSHARED); |
| 21136 | |
| 21137 | case OPT_EXISTS:{ |
| 21138 | int rc = Jim_DictKeysVector(interp, argv[2], argv + 3, argc - 3, &objPtr, JIM_NONE); |
| 21139 | if (rc < 0) { |
| 21140 | return JIM_ERR; |
| @@ -20207,11 +21142,11 @@ | |
| 21142 | Jim_SetResultBool(interp, rc == JIM_OK); |
| 21143 | return JIM_OK; |
| 21144 | } |
| 21145 | |
| 21146 | case OPT_UNSET: |
| 21147 | if (Jim_SetDictKeysVector(interp, argv[2], argv + 3, argc - 3, NULL, JIM_UNSHARED) != JIM_OK) { |
| 21148 | return JIM_ERR; |
| 21149 | } |
| 21150 | return JIM_OK; |
| 21151 | |
| 21152 | case OPT_VALUES: |
| @@ -20270,14 +21205,10 @@ | |
| 21205 | { OPT_NOBACKSLASHES, OPT_NOCOMMANDS, OPT_NOVARIABLES }; |
| 21206 | int i; |
| 21207 | int flags = JIM_SUBST_FLAG; |
| 21208 | Jim_Obj *objPtr; |
| 21209 | |
| 21210 | for (i = 1; i < (argc - 1); i++) { |
| 21211 | int option; |
| 21212 | |
| 21213 | if (Jim_GetEnum(interp, argv[i], options, &option, NULL, |
| 21214 | JIM_ERRMSG | JIM_ENUM_ABBREV) != JIM_OK) { |
| @@ -20300,11 +21231,24 @@ | |
| 21231 | } |
| 21232 | Jim_SetResult(interp, objPtr); |
| 21233 | return JIM_OK; |
| 21234 | } |
| 21235 | |
| 21236 | |
| 21237 | static int Jim_LsubstCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 21238 | { |
| 21239 | if (argc == 3) { |
| 21240 | if (Jim_CompareStringImmediate(interp, argv[1], "-line")) { |
| 21241 | return JimListSubstObj(interp, argv[2], JIM_LSUBST_LINE); |
| 21242 | } |
| 21243 | return JIM_USAGE; |
| 21244 | } |
| 21245 | return JimListSubstObj(interp, argv[1], 0); |
| 21246 | } |
| 21247 | |
| 21248 | #ifdef jim_ext_namespace |
| 21249 | |
| 21250 | static int JimIsGlobalNamespace(Jim_Obj *objPtr) |
| 21251 | { |
| 21252 | int len; |
| 21253 | const char *str = Jim_GetString(objPtr, &len); |
| 21254 | return len >= 2 && str[0] == ':' && str[1] == ':'; |
| @@ -20313,23 +21257,25 @@ | |
| 21257 | |
| 21258 | |
| 21259 | static int Jim_InfoCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 21260 | { |
| 21261 | Jim_Obj *objPtr; |
| 21262 | int mode = 1; |
| 21263 | |
| 21264 | |
| 21265 | enum { |
| 21266 | INFO_ALIAS, |
| 21267 | INFO_ALIASES, |
| 21268 | INFO_ARGS, |
| 21269 | INFO_BODY, |
| 21270 | INFO_CHANNELS, |
| 21271 | INFO_COMMANDS, |
| 21272 | INFO_COMPLETE, |
| 21273 | INFO_EXISTS, |
| 21274 | INFO_FRAME, |
| 21275 | INFO_GLOBALS, |
| 21276 | INFO_HELP, |
| 21277 | INFO_HOSTNAME, |
| 21278 | INFO_LEVEL, |
| 21279 | INFO_LOCALS, |
| 21280 | INFO_NAMEOFEXECUTABLE, |
| 21281 | INFO_PATCHLEVEL, |
| @@ -20338,39 +21284,45 @@ | |
| 21284 | INFO_RETURNCODES, |
| 21285 | INFO_SCRIPT, |
| 21286 | INFO_SOURCE, |
| 21287 | INFO_STACKTRACE, |
| 21288 | INFO_STATICS, |
| 21289 | INFO_TAINTED, |
| 21290 | INFO_USAGE, |
| 21291 | INFO_VARS, |
| 21292 | INFO_VERSION, |
| 21293 | INFO_COUNT |
| 21294 | }; |
| 21295 | static const jim_subcmd_type cmds[INFO_COUNT + 1] = { |
| 21296 | JIM_DEF_SUBCMD("alias", "command", 1, 1), |
| 21297 | JIM_DEF_SUBCMD("aliases", "?-all? ?pattern?", 0, 2), |
| 21298 | JIM_DEF_SUBCMD("args", "procname", 1, 1), |
| 21299 | JIM_DEF_SUBCMD("body", "procname", 1, 1), |
| 21300 | JIM_DEF_SUBCMD("channels", "?-all? ?pattern?", 0, 2), |
| 21301 | JIM_DEF_SUBCMD("commands", "?-all? ?pattern?", 0, 2), |
| 21302 | JIM_DEF_SUBCMD("complete", "script ?missing?", 1, 2), |
| 21303 | JIM_DEF_SUBCMD("exists", "varName", 1, 1), |
| 21304 | JIM_DEF_SUBCMD("frame", "?levelNum?", 0, 1), |
| 21305 | JIM_DEF_SUBCMD("globals", "?pattern?", 0, 1), |
| 21306 | JIM_DEF_SUBCMD("help", "command", 1, 1), |
| 21307 | JIM_DEF_SUBCMD("hostname", NULL, 0, 0), |
| 21308 | JIM_DEF_SUBCMD("level", "?levelNum?", 0, 1), |
| 21309 | JIM_DEF_SUBCMD("locals", "?pattern?", 0, 1), |
| 21310 | JIM_DEF_SUBCMD("nameofexecutable", NULL, 0, 0), |
| 21311 | JIM_DEF_SUBCMD("patchlevel", NULL, 0, 0), |
| 21312 | JIM_DEF_SUBCMD("procs", "?-all? ?pattern?", 0, 2), |
| 21313 | JIM_DEF_SUBCMD("references", NULL, 0, 0), |
| 21314 | JIM_DEF_SUBCMD("returncodes", "?code?", 0, 1), |
| 21315 | JIM_DEF_SUBCMD("script", "?filename?", 0, 1), |
| 21316 | JIM_DEF_SUBCMD("source", "source ?filename line?", 1, 3), |
| 21317 | JIM_DEF_SUBCMD("stacktrace", NULL, 0, 0), |
| 21318 | JIM_DEF_SUBCMD("statics", "procname", 1, 1), |
| 21319 | JIM_DEF_SUBCMD("tainted", "value", 1, 1), |
| 21320 | JIM_DEF_SUBCMD("usage", "command", 1, 1), |
| 21321 | JIM_DEF_SUBCMD("vars", "?pattern?", 0, 1), |
| 21322 | JIM_DEF_SUBCMD("version", NULL, 0, 0), |
| 21323 | { NULL } |
| 21324 | }; |
| 21325 | const jim_subcmd_type *ct; |
| 21326 | #ifdef jim_ext_namespace |
| 21327 | int nons = 0; |
| 21328 | |
| @@ -20401,45 +21353,66 @@ | |
| 21353 | Jim_Cmd *cmdPtr; |
| 21354 | |
| 21355 | if ((cmdPtr = Jim_GetCommand(interp, argv[2], JIM_ERRMSG)) == NULL) { |
| 21356 | return JIM_ERR; |
| 21357 | } |
| 21358 | if ((cmdPtr->flags & JIM_CMD_ISALIAS) == 0) { |
| 21359 | Jim_SetResultFormatted(interp, "command \"%#s\" is not an alias", argv[2]); |
| 21360 | return JIM_ERR; |
| 21361 | } |
| 21362 | Jim_SetResult(interp, (Jim_Obj *)cmdPtr->u.native.privData); |
| 21363 | return JIM_OK; |
| 21364 | } |
| 21365 | |
| 21366 | case INFO_TAINTED: |
| 21367 | if (argc != 3) { |
| 21368 | Jim_WrongNumArgs(interp, 2, argv, "value"); |
| 21369 | return JIM_ERR; |
| 21370 | } |
| 21371 | Jim_SetResultBool(interp, argv[2]->taint != 0); |
| 21372 | return JIM_OK; |
| 21373 | case INFO_CHANNELS: |
| 21374 | mode <<= 1; |
| 21375 | #ifndef jim_ext_aio |
| 21376 | Jim_SetResultString(interp, "aio not enabled", -1); |
| 21377 | return JIM_ERR; |
| 21378 | #endif |
| 21379 | |
| 21380 | case INFO_ALIASES: |
| 21381 | mode <<= 1; |
| 21382 | |
| 21383 | case INFO_PROCS: |
| 21384 | mode <<= 1; |
| 21385 | |
| 21386 | case INFO_COMMANDS:{ |
| 21387 | int n = 0; |
| 21388 | |
| 21389 | if (argc > 2 && Jim_CompareStringImmediate(interp, argv[2], "-all")) { |
| 21390 | mode |= JIM_CMDLIST_ALL; |
| 21391 | n++; |
| 21392 | } |
| 21393 | if (argc < 2 + n || argc > 3 + n) { |
| 21394 | Jim_SetResultFormatted(interp, "wrong # args: should be \"info %#s %s\"", argv[1], cmds[option].args); |
| 21395 | return JIM_ERR; |
| 21396 | } |
| 21397 | #ifdef jim_ext_namespace |
| 21398 | if (!nons) { |
| 21399 | |
| 21400 | if (Jim_Length(interp->framePtr->nsObj) || (argc == 3 && JimIsGlobalNamespace(argv[2]))) { |
| 21401 | return Jim_EvalPrefix(interp, "namespace info", argc - 1, argv + 1); |
| 21402 | } |
| 21403 | } |
| 21404 | #endif |
| 21405 | Jim_SetResult(interp, JimCommandsList(interp, (argc == 3 + n) ? argv[2 + n] : NULL, mode)); |
| 21406 | return JIM_OK; |
| 21407 | } |
| 21408 | |
| 21409 | case INFO_VARS: |
| 21410 | mode <<= 1; |
| 21411 | |
| 21412 | case INFO_LOCALS: |
| 21413 | mode <<= 1; |
| 21414 | |
| 21415 | case INFO_GLOBALS: |
| 21416 | |
| 21417 | #ifdef jim_ext_namespace |
| 21418 | if (!nons) { |
| @@ -20512,20 +21485,41 @@ | |
| 21485 | return JIM_ERR; |
| 21486 | } |
| 21487 | Jim_SetResult(interp, objPtr); |
| 21488 | } |
| 21489 | return JIM_OK; |
| 21490 | |
| 21491 | case INFO_USAGE: |
| 21492 | case INFO_HELP:{ |
| 21493 | Jim_Cmd *cmdPtr = Jim_GetCommand(interp, argv[2], JIM_ERRMSG); |
| 21494 | if (!cmdPtr) { |
| 21495 | return JIM_ERR; |
| 21496 | } |
| 21497 | if (option == INFO_USAGE) { |
| 21498 | Jim_SetResult(interp, JimCmdUsage(interp, argv[2], cmdPtr)); |
| 21499 | return JIM_OK; |
| 21500 | } |
| 21501 | else if ((cmdPtr->flags & JIM_CMD_ISPROC) == 0) { |
| 21502 | if (cmdPtr->u.native.help) { |
| 21503 | Jim_SetResultString(interp, cmdPtr->u.native.help, -1); |
| 21504 | return JIM_OK; |
| 21505 | } |
| 21506 | } |
| 21507 | |
| 21508 | Jim_SetResultFormatted(interp, "No help available for command \"%#s\"", argv[2]); |
| 21509 | return JIM_OK; |
| 21510 | } |
| 21511 | |
| 21512 | case INFO_BODY: |
| 21513 | case INFO_STATICS: |
| 21514 | case INFO_ARGS:{ |
| 21515 | Jim_Cmd *cmdPtr; |
| 21516 | |
| 21517 | if ((cmdPtr = Jim_GetCommand(interp, argv[2], JIM_ERRMSG)) == NULL) { |
| 21518 | return JIM_ERR; |
| 21519 | } |
| 21520 | if ((cmdPtr->flags & JIM_CMD_ISPROC) == 0) { |
| 21521 | Jim_SetResultFormatted(interp, "command \"%#s\" is not a procedure", argv[2]); |
| 21522 | return JIM_ERR; |
| 21523 | } |
| 21524 | switch (option) { |
| 21525 | #ifdef JIM_NO_INTROSPECTION |
| @@ -20548,16 +21542,21 @@ | |
| 21542 | break; |
| 21543 | } |
| 21544 | return JIM_OK; |
| 21545 | } |
| 21546 | |
| 21547 | case INFO_PATCHLEVEL: |
| 21548 | |
| 21549 | #ifdef JIM_GITVERSION |
| 21550 | Jim_SetResultString(interp, JIM_GITVERSION, -1); |
| 21551 | return JIM_OK; |
| 21552 | #endif |
| 21553 | |
| 21554 | case INFO_VERSION:{ |
| 21555 | char versionbuf[64]; |
| 21556 | snprintf(versionbuf, sizeof(versionbuf), "%d.%d", JIM_VERSION / 100, JIM_VERSION % 100); |
| 21557 | Jim_SetResultString(interp, versionbuf, -1); |
| 21558 | return JIM_OK; |
| 21559 | } |
| 21560 | |
| 21561 | case INFO_COMPLETE: { |
| 21562 | char missing; |
| @@ -20623,15 +21622,15 @@ | |
| 21622 | { |
| 21623 | Jim_Obj *objPtr; |
| 21624 | int result = 0; |
| 21625 | |
| 21626 | static const char * const options[] = { |
| 21627 | "-command", "-proc", "-alias", "-channel", "-var", NULL |
| 21628 | }; |
| 21629 | enum |
| 21630 | { |
| 21631 | OPT_COMMAND, OPT_PROC, OPT_ALIAS, OPT_CHANNEL, OPT_VAR |
| 21632 | }; |
| 21633 | int option; |
| 21634 | |
| 21635 | if (argc == 2) { |
| 21636 | option = OPT_VAR; |
| @@ -20642,12 +21641,11 @@ | |
| 21641 | return JIM_ERR; |
| 21642 | } |
| 21643 | objPtr = argv[2]; |
| 21644 | } |
| 21645 | else { |
| 21646 | return JIM_USAGE; |
| 21647 | } |
| 21648 | |
| 21649 | if (option == OPT_VAR) { |
| 21650 | result = Jim_GetVariable(interp, objPtr, 0) != NULL; |
| 21651 | } |
| @@ -20660,15 +21658,19 @@ | |
| 21658 | case OPT_COMMAND: |
| 21659 | result = 1; |
| 21660 | break; |
| 21661 | |
| 21662 | case OPT_ALIAS: |
| 21663 | result = (cmd->flags & JIM_CMD_ISALIAS) != 0; |
| 21664 | break; |
| 21665 | |
| 21666 | case OPT_PROC: |
| 21667 | result = (cmd->flags & JIM_CMD_ISPROC) != 0; |
| 21668 | break; |
| 21669 | |
| 21670 | case OPT_CHANNEL: |
| 21671 | result = (cmd->flags & JIM_CMD_ISCHANNEL) != 0; |
| 21672 | break; |
| 21673 | } |
| 21674 | } |
| 21675 | } |
| 21676 | Jim_SetResultBool(interp, result); |
| @@ -20682,15 +21684,10 @@ | |
| 21684 | int splitLen, strLen; |
| 21685 | Jim_Obj *resObjPtr; |
| 21686 | int c; |
| 21687 | int len; |
| 21688 | |
| 21689 | str = Jim_GetString(argv[1], &len); |
| 21690 | if (len == 0) { |
| 21691 | return JIM_OK; |
| 21692 | } |
| 21693 | strLen = Jim_Utf8Length(interp, argv[1]); |
| @@ -20765,14 +21762,10 @@ | |
| 21762 | static int Jim_JoinCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 21763 | { |
| 21764 | const char *joinStr; |
| 21765 | int joinStrLen; |
| 21766 | |
| 21767 | |
| 21768 | if (argc == 2) { |
| 21769 | joinStr = " "; |
| 21770 | joinStrLen = 1; |
| 21771 | } |
| @@ -20786,14 +21779,10 @@ | |
| 21779 | |
| 21780 | static int Jim_FormatCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 21781 | { |
| 21782 | Jim_Obj *objPtr; |
| 21783 | |
| 21784 | objPtr = Jim_FormatString(interp, argv[1], argc - 2, argv + 2); |
| 21785 | if (objPtr == NULL) |
| 21786 | return JIM_ERR; |
| 21787 | Jim_SetResult(interp, objPtr); |
| 21788 | return JIM_OK; |
| @@ -20803,14 +21792,10 @@ | |
| 21792 | static int Jim_ScanCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 21793 | { |
| 21794 | Jim_Obj *listPtr, **outVec; |
| 21795 | int outc, i; |
| 21796 | |
| 21797 | if (argv[2]->typePtr != &scanFmtStringObjType) |
| 21798 | SetScanFmtFromAny(interp, argv[2]); |
| 21799 | if (FormatGetError(argv[2]) != 0) { |
| 21800 | Jim_SetResultString(interp, FormatGetError(argv[2]), -1); |
| 21801 | return JIM_ERR; |
| @@ -20876,14 +21861,10 @@ | |
| 21861 | } |
| 21862 | |
| 21863 | |
| 21864 | static int Jim_ErrorCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 21865 | { |
| 21866 | Jim_SetResult(interp, argv[1]); |
| 21867 | if (argc == 3) { |
| 21868 | JimSetStackTrace(interp, argv[2]); |
| 21869 | return JIM_ERR; |
| 21870 | } |
| @@ -20893,14 +21874,10 @@ | |
| 21874 | |
| 21875 | static int Jim_LrangeCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 21876 | { |
| 21877 | Jim_Obj *objPtr; |
| 21878 | |
| 21879 | if ((objPtr = Jim_ListRange(interp, argv[1], argv[2], argv[3])) == NULL) |
| 21880 | return JIM_ERR; |
| 21881 | Jim_SetResult(interp, objPtr); |
| 21882 | return JIM_OK; |
| 21883 | } |
| @@ -20910,12 +21887,11 @@ | |
| 21887 | { |
| 21888 | Jim_Obj *objPtr; |
| 21889 | jim_wide count; |
| 21890 | |
| 21891 | if (argc < 2 || Jim_GetWideExpr(interp, argv[1], &count) != JIM_OK || count < 0) { |
| 21892 | return JIM_USAGE; |
| 21893 | } |
| 21894 | if (count == 0 || argc == 2) { |
| 21895 | Jim_SetEmptyResult(interp); |
| 21896 | return JIM_OK; |
| 21897 | } |
| @@ -20935,23 +21911,26 @@ | |
| 21911 | |
| 21912 | char **Jim_GetEnviron(void) |
| 21913 | { |
| 21914 | #if defined(HAVE__NSGETENVIRON) |
| 21915 | return *_NSGetEnviron(); |
| 21916 | #elif defined(_environ) |
| 21917 | return _environ; |
| 21918 | #else |
| 21919 | #if !defined(NO_ENVIRON_EXTERN) |
| 21920 | extern char **environ; |
| 21921 | #endif |
| 21922 | return environ; |
| 21923 | #endif |
| 21924 | } |
| 21925 | |
| 21926 | void Jim_SetEnviron(char **env) |
| 21927 | { |
| 21928 | #if defined(HAVE__NSGETENVIRON) |
| 21929 | *_NSGetEnviron() = env; |
| 21930 | #elif defined(_environ) |
| 21931 | _environ = env; |
| 21932 | #else |
| 21933 | #if !defined(NO_ENVIRON_EXTERN) |
| 21934 | extern char **environ; |
| 21935 | #endif |
| 21936 | |
| @@ -20983,14 +21962,10 @@ | |
| 21962 | |
| 21963 | Jim_SetResult(interp, listObjPtr); |
| 21964 | return JIM_OK; |
| 21965 | } |
| 21966 | |
| 21967 | key = Jim_String(argv[1]); |
| 21968 | val = getenv(key); |
| 21969 | if (val == NULL) { |
| 21970 | if (argc < 3) { |
| 21971 | Jim_SetResultFormatted(interp, "environment variable \"%#s\" does not exist", argv[1]); |
| @@ -21003,41 +21978,31 @@ | |
| 21978 | } |
| 21979 | |
| 21980 | |
| 21981 | static int Jim_SourceCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 21982 | { |
| 21983 | int retval = Jim_EvalFile(interp, Jim_String(argv[1])); |
| 21984 | |
| 21985 | return retval == JIM_RETURN ? JIM_OK : retval; |
| 21986 | } |
| 21987 | |
| 21988 | |
| 21989 | static int Jim_LreverseCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 21990 | { |
| 21991 | Jim_Obj *revObjPtr, **ele; |
| 21992 | int len; |
| 21993 | |
| 21994 | JimListGetElements(interp, argv[1], &len, &ele); |
| 21995 | revObjPtr = Jim_NewListObj(interp, NULL, 0); |
| 21996 | ListEnsureLength(revObjPtr, len); |
| 21997 | len--; |
| 21998 | while (len >= 0) |
| 21999 | ListAppendElement(revObjPtr, ele[len--]); |
| 22000 | Jim_SetResult(interp, revObjPtr); |
| 22001 | return JIM_OK; |
| 22002 | } |
| 22003 | |
| 22004 | |
| 22005 | static int JimRangeLen(jim_wide start, jim_wide end, jim_wide step) |
| 22006 | { |
| 22007 | jim_wide len; |
| 22008 | |
| @@ -21065,14 +22030,10 @@ | |
| 22030 | { |
| 22031 | jim_wide start = 0, end, step = 1; |
| 22032 | int len, i; |
| 22033 | Jim_Obj *objPtr; |
| 22034 | |
| 22035 | if (argc == 2) { |
| 22036 | if (Jim_GetWideExpr(interp, argv[1], &end) != JIM_OK) |
| 22037 | return JIM_ERR; |
| 22038 | } |
| 22039 | else { |
| @@ -21097,14 +22058,10 @@ | |
| 22058 | |
| 22059 | static int Jim_RandCoreCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv) |
| 22060 | { |
| 22061 | jim_wide min = 0, max = 0, len, maxMul; |
| 22062 | |
| 22063 | if (argc == 1) { |
| 22064 | max = JIM_WIDE_MAX; |
| 22065 | } else if (argc == 2) { |
| 22066 | if (Jim_GetWideExpr(interp, argv[1], &max) != JIM_OK) |
| 22067 | return JIM_ERR; |
| @@ -21128,98 +22085,111 @@ | |
| 22085 | Jim_SetResultInt(interp, min+r); |
| 22086 | return JIM_OK; |
| 22087 | } |
| 22088 | } |
| 22089 | |
| 22090 | static const struct jim_core_cmd_def_t { |
| 22091 | const char *name; |
| 22092 | Jim_CmdProc *cmdProc; |
| 22093 | short minargs; |
| 22094 | short maxargs; |
| 22095 | const char *usage; |
| 22096 | int flags; |
| 22097 | } Jim_CoreCommandsTable[] = { |
| 22098 | {"*", Jim_MulCoreCommand, 0, -1, "?number ...?" }, |
| 22099 | {"+", Jim_AddCoreCommand, 0, -1, "?number ...?" }, |
| 22100 | {"-", Jim_SubCoreCommand, 1, -1, "number ?number ...?" }, |
| 22101 | {"/", Jim_DivCoreCommand, 1, -1, "number ?number ...?" }, |
| 22102 | {"alias", Jim_AliasCoreCommand, 2, -1, "newname command ?args ...?" }, |
| 22103 | {"append", Jim_AppendCoreCommand, 1, -1, "varName ?value ...?" }, |
| 22104 | {"apply", Jim_ApplyCoreCommand, 1, -1, "lambdaExpr ?arg ...?" }, |
| 22105 | {"break", Jim_BreakCoreCommand, 0, 1, "?level?" }, |
| 22106 | {"catch", Jim_CatchCoreCommand, 1, -1, "?-?no?code ... --? script ?resultVarName? ?optionVarName?" }, |
| 22107 | {"concat", Jim_ConcatCoreCommand, 0, -1, "?arg ...?" }, |
| 22108 | {"continue", Jim_ContinueCoreCommand, 0, 1, "?level?" }, |
| 22109 | {"dict", Jim_DictCoreCommand, 1, -1, "subcommand ?arg ...?"}, |
| 22110 | {"env", Jim_EnvCoreCommand, 0, 2, "?varName? ?default?" }, |
| 22111 | {"error", Jim_ErrorCoreCommand, 1, 2, "message ?stacktrace?" }, |
| 22112 | {"eval", Jim_EvalCoreCommand, 1, -1, "arg ?arg ...?" }, |
| 22113 | {"exists", Jim_ExistsCoreCommand, 1, 2, "?-command|-proc|-alias|-channel|-var? name" }, |
| 22114 | {"exit", Jim_ExitCoreCommand, 0, 1, "?exitCode?" }, |
| 22115 | #ifdef JIM_COMPAT |
| 22116 | {"expr", Jim_ExprCoreCommand, 1, -1, "expression ?...?" }, |
| 22117 | #else |
| 22118 | {"expr", Jim_ExprCoreCommand, 1, 1, "expression" }, |
| 22119 | #endif |
| 22120 | {"for", Jim_ForCoreCommand, 4, 4, "start test next body" }, |
| 22121 | {"foreach", Jim_ForeachCoreCommand, 3, -1, "varList list ?varList list ...? script" }, |
| 22122 | {"format", Jim_FormatCoreCommand, 1, -1, "formatString ?arg arg ...?" }, |
| 22123 | {"global", Jim_GlobalCoreCommand, 1, -1, "varName ?varName ...?" }, |
| 22124 | {"if", Jim_IfCoreCommand, 2, -1, "condition ?then? trueBody ?elseif ...? ?else? ?falseBody?" }, |
| 22125 | {"incr", Jim_IncrCoreCommand, 1, 2, "varName ?increment?" }, |
| 22126 | {"info", Jim_InfoCoreCommand, 1, -1, "subcommand ?arg ...?"}, |
| 22127 | {"join", Jim_JoinCoreCommand, 1, 2, "list ?joinString?" }, |
| 22128 | {"lappend", Jim_LappendCoreCommand, 1, -1, "varName ?value value ...?" }, |
| 22129 | {"lassign", Jim_LassignCoreCommand, 2, -1, "varList list ?varName ...?" }, |
| 22130 | {"lindex", Jim_LindexCoreCommand, 1, -1, "list ?index ...?" }, |
| 22131 | {"linsert", Jim_LinsertCoreCommand, 2, -1, "list index ?element ...?" }, |
| 22132 | {"list", Jim_ListCoreCommand, 0, -1, "?arg ...?" }, |
| 22133 | {"llength", Jim_LlengthCoreCommand, 1, 1, "list" }, |
| 22134 | {"lmap", Jim_LmapCoreCommand, 3, -1, "varList list ?varList list ...? script" }, |
| 22135 | {"local", Jim_LocalCoreCommand, 1, -1, "cmd ?args ...?" }, |
| 22136 | {"loop", Jim_LoopCoreCommand, 3, 5, "var ?first? limit ?incr? body" }, |
| 22137 | {"lrange", Jim_LrangeCoreCommand, 3, 3, "list first last" }, |
| 22138 | {"lrepeat", Jim_LrepeatCoreCommand, 1, -1, "count ?value ...?" }, |
| 22139 | {"lreplace", Jim_LreplaceCoreCommand, 3, -1, "list first last ?element ...?" }, |
| 22140 | {"lreverse", Jim_LreverseCoreCommand, 1, 1, "list" }, |
| 22141 | {"lsearch", Jim_LsearchCoreCommand, 2, -1, "?-exact|-glob|-regexp|-command 'command'? ?-bool|-inline? ?-not? ?-nocase? ?-all? ?-stride len? ?-index val? list value" }, |
| 22142 | {"lset", Jim_LsetCoreCommand, 2, -1, "listVar ?index ...? value" }, |
| 22143 | {"lsort", Jim_LsortCoreCommand, 1, -1, "?options? list" }, |
| 22144 | {"lsubst", Jim_LsubstCoreCommand, 1, 2, "?-line? string" }, |
| 22145 | {"proc", Jim_ProcCoreCommand, 3, 4, "name arglist ?statics? body" }, |
| 22146 | {"puts", Jim_PutsCoreCommand, 1, 2, "?-nonewline? string" }, |
| 22147 | {"rand", Jim_RandCoreCommand, 0, 2, "?min? ?max?" }, |
| 22148 | {"range", Jim_RangeCoreCommand, 1, 3, "?start? end ?step?" }, |
| 22149 | {"rename", Jim_RenameCoreCommand, 2, 2, "oldName newName" }, |
| 22150 | {"return", Jim_ReturnCoreCommand, 0, -1, "?-code code? ?-errorinfo stacktrace? ?-level level? ?result?" }, |
| 22151 | {"scan", Jim_ScanCoreCommand, 2, -1, "string format ?varName varName ...?" }, |
| 22152 | {"set", Jim_SetCoreCommand, 1, 2, "varName ?newValue?" }, |
| 22153 | {"source", Jim_SourceCoreCommand, 1, 1, "fileName", JIM_CMD_NOTAINT }, |
| 22154 | {"split", Jim_SplitCoreCommand, 1, 2, "string ?splitChars?" }, |
| 22155 | {"stacktrace", Jim_StacktraceCoreCommand, 0, 2, "?firstlevel? ?lastlevel?" }, |
| 22156 | {"string", Jim_StringCoreCommand, 1, -1, "subcommand ?arg ...?" }, |
| 22157 | {"subst", Jim_SubstCoreCommand, 1, 4, "?options? string" }, |
| 22158 | {"switch", Jim_SwitchCoreCommand, 2, -1, "?options? string pattern body ... ?default body? or pattern body ?pattern body ...?" }, |
| 22159 | {"tailcall", Jim_TailcallCoreCommand, 0, -1, "?cmd arg ...?" }, |
| 22160 | {"time", Jim_TimeCoreCommand, 1, 2, "script ?count?" }, |
| 22161 | {"timerate", Jim_TimerateCoreCommand, 1, 2, "script ?milliseconds?" }, |
| 22162 | {"try", Jim_TryCoreCommand, 1, -1, "?-?no?code ... --? script ?on|trap codes vars script? ... ?finally script?" }, |
| 22163 | {"unset", Jim_UnsetCoreCommand, 0, -1, "?-nocomplain? ?--? ?varName ...?"}, |
| 22164 | {"upcall", Jim_UpcallCoreCommand, 1, -1, "cmd ?args ...?" }, |
| 22165 | {"uplevel", Jim_UplevelCoreCommand, 1, -1, "?level? command ?arg ...?" }, |
| 22166 | {"upvar", Jim_UpvarCoreCommand, 2, -1, "?level? otherVar myVar ?otherVar myVar ...?"}, |
| 22167 | {"while", Jim_WhileCoreCommand, 2, 2, "condition body" }, |
| 22168 | {"xtrace", Jim_XtraceCoreCommand, 1, 1, "callback" }, |
| 22169 | #ifdef JIM_REFERENCES |
| 22170 | {"collect", Jim_CollectCoreCommand, 0, 0, "" }, |
| 22171 | {"finalize", Jim_FinalizeCoreCommand, 1, 2, "reference ?finalizerProc?" }, |
| 22172 | {"getref", Jim_GetrefCoreCommand, 1, 1, "reference" }, |
| 22173 | {"ref", Jim_RefCoreCommand, 2, 3, "string tag ?finalizer?" }, |
| 22174 | {"setref", Jim_SetrefCoreCommand, 2, 2, "reference newValue" }, |
| 22175 | #endif |
| 22176 | #ifdef JIM_TAINT |
| 22177 | {"taint", Jim_TaintCoreCommand, 1, 1, "varname"}, |
| 22178 | {"untaint", Jim_UntaintCoreCommand, 1, 1, "varname"}, |
| 22179 | #endif |
| 22180 | {NULL, NULL}, |
| 22181 | }; |
| 22182 | |
| 22183 | void Jim_RegisterCoreCommands(Jim_Interp *interp) |
| 22184 | { |
| 22185 | const struct jim_core_cmd_def_t *c; |
| 22186 | |
| 22187 | for (c = Jim_CoreCommandsTable; c->name; c++) { |
| 22188 | |
| 22189 | assert(c->usage); |
| 22190 | Jim_RegisterCmd(interp, c->name, c->usage, c->minargs, c->maxargs, c->cmdProc, NULL, NULL, c->flags); |
| 22191 | } |
| 22192 | } |
| 22193 | |
| 22194 | void Jim_MakeErrorMessage(Jim_Interp *interp) |
| 22195 | { |
| @@ -21662,18 +22632,26 @@ | |
| 22632 | int Jim_CallSubCmd(Jim_Interp *interp, const jim_subcmd_type * ct, int argc, Jim_Obj *const *argv) |
| 22633 | { |
| 22634 | int ret = JIM_ERR; |
| 22635 | |
| 22636 | if (ct) { |
| 22637 | if ((ct->flags & JIM_MODFLAG_NOTAINT) && Jim_CheckTaint(interp, JIM_TAINT_ANY)) { |
| 22638 | ret = JIM_SUBCMD_TAINTED; |
| 22639 | } |
| 22640 | else if (ct->flags & JIM_MODFLAG_FULLARGV) { |
| 22641 | ret = ct->function(interp, argc, argv); |
| 22642 | } |
| 22643 | else { |
| 22644 | ret = ct->function(interp, argc - 2, argv + 2); |
| 22645 | } |
| 22646 | if (ret < 0) { |
| 22647 | if (ret == JIM_SUBCMD_TAINTED) { |
| 22648 | Jim_SetTaintError(interp, 2, argv); |
| 22649 | } |
| 22650 | else { |
| 22651 | Jim_SubCmdArgError(interp, ct, argv[0]); |
| 22652 | } |
| 22653 | ret = JIM_ERR; |
| 22654 | } |
| 22655 | } |
| 22656 | return ret; |
| 22657 | } |
| @@ -21683,10 +22661,22 @@ | |
| 22661 | const jim_subcmd_type *ct = |
| 22662 | Jim_ParseSubCmd(interp, (const jim_subcmd_type *)Jim_CmdPrivData(interp), argc, argv); |
| 22663 | |
| 22664 | return Jim_CallSubCmd(interp, ct, argc, argv); |
| 22665 | } |
| 22666 | |
| 22667 | Jim_Cmd *Jim_RegisterSubCmd(Jim_Interp *interp, const char *cmdname, |
| 22668 | const jim_subcmd_type *command_table, Jim_DelCmdProc *delProc) |
| 22669 | { |
| 22670 | return Jim_RegisterCmd(interp, cmdname, |
| 22671 | "subcommand ?arg ...?", |
| 22672 | 1, -1, |
| 22673 | Jim_SubCmdProc, |
| 22674 | delProc, |
| 22675 | (void *)command_table, |
| 22676 | 0); |
| 22677 | } |
| 22678 | |
| 22679 | #include <ctype.h> |
| 22680 | #include <stdlib.h> |
| 22681 | #include <string.h> |
| 22682 | #include <stdio.h> |
| @@ -21873,11 +22863,11 @@ | |
| 22863 | gotPrecision = 1; |
| 22864 | format += step; |
| 22865 | step = utf8_tounicode(format, &ch); |
| 22866 | } |
| 22867 | if (isdigit(ch)) { |
| 22868 | precision = strtol(format, &end, 10); |
| 22869 | format = end; |
| 22870 | step = utf8_tounicode(format, &ch); |
| 22871 | } else if (ch == '*') { |
| 22872 | if (objIndex >= objc - 1) { |
| 22873 | msg = badIndex[gotXpg]; |
| @@ -22160,12 +23150,11 @@ | |
| 23150 | |
| 23151 | |
| 23152 | |
| 23153 | |
| 23154 | #define FAIL(R,M) { (R)->err = (M); return (M); } |
| 23155 | #define META "^$.[()|" |
| 23156 | |
| 23157 | #define HASWIDTH 1 |
| 23158 | #define SIMPLE 2 |
| 23159 | #define SPSTART 4 |
| 23160 | #define WORST 0 |
| @@ -22193,10 +23182,26 @@ | |
| 23182 | static int regnarrate = 0; |
| 23183 | static void regdump(regex_t *preg); |
| 23184 | static const char *regprop( int op ); |
| 23185 | #endif |
| 23186 | |
| 23187 | |
| 23188 | static int str_is_mult(const char *s) |
| 23189 | { |
| 23190 | switch (*s) { |
| 23191 | case '*': |
| 23192 | case '+': |
| 23193 | case '?': |
| 23194 | return 1; |
| 23195 | case '{': |
| 23196 | if (isdigit(UCHAR(s[1]))) { |
| 23197 | return 1; |
| 23198 | } |
| 23199 | break; |
| 23200 | } |
| 23201 | return 0; |
| 23202 | } |
| 23203 | |
| 23204 | static int str_int_len(const int *seq) |
| 23205 | { |
| 23206 | int n = 0; |
| 23207 | while (*seq++) { |
| @@ -22203,10 +23208,45 @@ | |
| 23208 | n++; |
| 23209 | } |
| 23210 | return n; |
| 23211 | } |
| 23212 | |
| 23213 | |
| 23214 | static char *reg_expanded_new_pattern(const char *exp) |
| 23215 | { |
| 23216 | |
| 23217 | char *newexp = strdup(exp); |
| 23218 | char *d = newexp; |
| 23219 | const char *s = exp; |
| 23220 | int escape = 0; |
| 23221 | |
| 23222 | while (*s) { |
| 23223 | if (escape) { |
| 23224 | escape = 0; |
| 23225 | continue; |
| 23226 | } |
| 23227 | else if (*s == '\\') { |
| 23228 | escape = 1; |
| 23229 | } |
| 23230 | else if (strchr(" \t\r\n\f\v", *s)) { |
| 23231 | s++; |
| 23232 | continue; |
| 23233 | } |
| 23234 | else if (*s == '#') { |
| 23235 | |
| 23236 | s++; |
| 23237 | while (*s && *s != '\n') { |
| 23238 | s++; |
| 23239 | } |
| 23240 | continue; |
| 23241 | } |
| 23242 | *d++ = *s++; |
| 23243 | } |
| 23244 | *d++ = '\0'; |
| 23245 | return newexp; |
| 23246 | } |
| 23247 | |
| 23248 | int jim_regcomp(regex_t *preg, const char *exp, int cflags) |
| 23249 | { |
| 23250 | int scan; |
| 23251 | int longest; |
| 23252 | unsigned len; |
| @@ -22217,10 +23257,15 @@ | |
| 23257 | #endif |
| 23258 | memset(preg, 0, sizeof(*preg)); |
| 23259 | |
| 23260 | if (exp == NULL) |
| 23261 | FAIL(preg, REG_ERR_NULL_ARGUMENT); |
| 23262 | |
| 23263 | if (cflags & REG_EXPANDED) { |
| 23264 | preg->exp = reg_expanded_new_pattern(exp); |
| 23265 | exp = preg->exp; |
| 23266 | } |
| 23267 | |
| 23268 | |
| 23269 | preg->cflags = cflags; |
| 23270 | preg->regparse = exp; |
| 23271 | |
| @@ -22392,16 +23437,16 @@ | |
| 23437 | |
| 23438 | ret = regatom(preg, &flags); |
| 23439 | if (ret == 0) |
| 23440 | return 0; |
| 23441 | |
| 23442 | if (!str_is_mult(preg->regparse)) { |
| 23443 | *flagp = flags; |
| 23444 | return(ret); |
| 23445 | } |
| 23446 | |
| 23447 | op = *preg->regparse; |
| 23448 | if (!(flags&HASWIDTH) && op != '?') { |
| 23449 | preg->err = REG_ERR_OPERAND_COULD_BE_EMPTY; |
| 23450 | return 0; |
| 23451 | } |
| 23452 | |
| @@ -22466,11 +23511,11 @@ | |
| 23511 | regtail(preg, back, ret); |
| 23512 | regtail(preg, next, back); |
| 23513 | } |
| 23514 | |
| 23515 | preg->regparse++; |
| 23516 | if (str_is_mult(preg->regparse)) { |
| 23517 | preg->err = REG_ERR_NESTED_COUNT; |
| 23518 | return 0; |
| 23519 | } |
| 23520 | |
| 23521 | return ret; |
| @@ -22775,16 +23820,10 @@ | |
| 23820 | case '\0': |
| 23821 | case '|': |
| 23822 | case ')': |
| 23823 | preg->err = REG_ERR_INTERNAL; |
| 23824 | return 0; |
| 23825 | case '\\': |
| 23826 | ch = *preg->regparse++; |
| 23827 | switch (ch) { |
| 23828 | case '\0': |
| 23829 | preg->err = REG_ERR_INVALID_ESCAPE; |
| @@ -22841,16 +23880,21 @@ | |
| 23880 | default: { |
| 23881 | int added = 0; |
| 23882 | |
| 23883 | |
| 23884 | preg->regparse -= n; |
| 23885 | |
| 23886 | if (str_is_mult(preg->regparse)) { |
| 23887 | preg->err = REG_ERR_COUNT_FOLLOWS_NOTHING; |
| 23888 | return 0; |
| 23889 | } |
| 23890 | |
| 23891 | ret = regnode(preg, EXACTLY); |
| 23892 | |
| 23893 | |
| 23894 | |
| 23895 | while (*preg->regparse && strchr(META, *preg->regparse) == NULL && !str_is_mult(preg->regparse)) { |
| 23896 | n = reg_utf8_tounicode_case(preg->regparse, &ch, (preg->cflags & REG_ICASE)); |
| 23897 | if (ch == '\\' && preg->regparse[n]) { |
| 23898 | if (strchr("<>mMwWdDsSAZ", preg->regparse[n])) { |
| 23899 | |
| 23900 | break; |
| @@ -22861,11 +23905,11 @@ | |
| 23905 | return 0; |
| 23906 | } |
| 23907 | } |
| 23908 | |
| 23909 | |
| 23910 | if (str_is_mult(&preg->regparse[n])) { |
| 23911 | |
| 23912 | if (added) { |
| 23913 | |
| 23914 | break; |
| 23915 | } |
| @@ -23034,11 +24078,11 @@ | |
| 24078 | if (regtry(preg, string)) { |
| 24079 | return REG_NOERROR; |
| 24080 | } |
| 24081 | if (*string) { |
| 24082 | nextline: |
| 24083 | if (preg->cflags & REG_NEWLINE_ANCHOR) { |
| 24084 | |
| 24085 | string = strchr(string, '\n'); |
| 24086 | if (string) { |
| 24087 | preg->regbol = ++string; |
| 24088 | continue; |
| @@ -23144,11 +24188,11 @@ | |
| 24188 | return NULL; |
| 24189 | } |
| 24190 | |
| 24191 | static int reg_iseol(regex_t *preg, int ch) |
| 24192 | { |
| 24193 | if (preg->cflags & REG_NEWLINE_STOP) { |
| 24194 | return ch == '\0' || ch == '\n'; |
| 24195 | } |
| 24196 | else { |
| 24197 | return ch == '\0'; |
| 24198 | } |
| @@ -23555,10 +24599,11 @@ | |
| 24599 | return snprintf(errbuf, errbuf_size, "%s", err); |
| 24600 | } |
| 24601 | |
| 24602 | void jim_regfree(regex_t *preg) |
| 24603 | { |
| 24604 | free(preg->exp); |
| 24605 | free(preg->program); |
| 24606 | } |
| 24607 | |
| 24608 | #endif |
| 24609 | #include <string.h> |
| @@ -23566,11 +24611,11 @@ | |
| 24611 | void Jim_SetResultErrno(Jim_Interp *interp, const char *msg) |
| 24612 | { |
| 24613 | Jim_SetResultFormatted(interp, "%s: %s", msg, strerror(Jim_Errno())); |
| 24614 | } |
| 24615 | |
| 24616 | #if defined(_WIN32) || defined(WIN32) |
| 24617 | #include <sys/stat.h> |
| 24618 | |
| 24619 | int Jim_Errno(void) |
| 24620 | { |
| 24621 | switch (GetLastError()) { |
| @@ -23743,11 +24788,13 @@ | |
| 24788 | |
| 24789 | |
| 24790 | int Jim_MakeTempFile(Jim_Interp *interp, const char *filename_template, int unlink_file) |
| 24791 | { |
| 24792 | int fd; |
| 24793 | #ifdef HAVE_UMASK |
| 24794 | mode_t mask; |
| 24795 | #endif |
| 24796 | Jim_Obj *filenameObj; |
| 24797 | |
| 24798 | if (filename_template == NULL) { |
| 24799 | const char *tmpdir = getenv("TMPDIR"); |
| 24800 | if (tmpdir == NULL || *tmpdir == '\0' || access(tmpdir, W_OK) != 0) { |
| @@ -24394,10 +25441,14 @@ | |
| 25441 | if (retcode == JIM_ERR) { |
| 25442 | JimPrintErrorMessage(interp); |
| 25443 | } |
| 25444 | if (retcode != JIM_EXIT) { |
| 25445 | JimSetArgv(interp, 0, NULL); |
| 25446 | if (!isatty(STDIN_FILENO)) { |
| 25447 | |
| 25448 | goto eval_stdin; |
| 25449 | } |
| 25450 | retcode = Jim_InteractivePrompt(interp); |
| 25451 | } |
| 25452 | } |
| 25453 | else { |
| 25454 | |
| @@ -24416,10 +25467,11 @@ | |
| 25467 | } |
| 25468 | else { |
| 25469 | Jim_SetVariableStr(interp, "argv0", Jim_NewStringObj(interp, argv[1], -1)); |
| 25470 | JimSetArgv(interp, argc - 2, argv + 2); |
| 25471 | if (strcmp(argv[1], "-") == 0) { |
| 25472 | eval_stdin: |
| 25473 | retcode = Jim_Eval(interp, "eval [info source [stdin read] stdin 1]"); |
| 25474 | } else { |
| 25475 | retcode = Jim_EvalFile(interp, argv[1]); |
| 25476 | } |
| 25477 | } |
| 25478 |